LLVM 21.0.0git
SelectionDAG.cpp
Go to the documentation of this file.
1//===- SelectionDAG.cpp - Implement the SelectionDAG data structures ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This implements the SelectionDAG class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "SDNodeDbgValue.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/APSInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/Twine.h"
51#include "llvm/IR/Constant.h"
52#include "llvm/IR/Constants.h"
53#include "llvm/IR/DataLayout.h"
55#include "llvm/IR/DebugLoc.h"
57#include "llvm/IR/Function.h"
58#include "llvm/IR/GlobalValue.h"
59#include "llvm/IR/Metadata.h"
60#include "llvm/IR/Type.h"
64#include "llvm/Support/Debug.h"
68#include "llvm/Support/Mutex.h"
74#include <algorithm>
75#include <cassert>
76#include <cstdint>
77#include <cstdlib>
78#include <deque>
79#include <limits>
80#include <optional>
81#include <set>
82#include <string>
83#include <utility>
84#include <vector>
85
86using namespace llvm;
87using namespace llvm::SDPatternMatch;
88
89/// makeVTList - Return an instance of the SDVTList struct initialized with the
90/// specified members.
91static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
92 SDVTList Res = {VTs, NumVTs};
93 return Res;
94}
95
96// Default null implementations of the callbacks.
100
101void SelectionDAG::DAGNodeDeletedListener::anchor() {}
102void SelectionDAG::DAGNodeInsertedListener::anchor() {}
103
104#define DEBUG_TYPE "selectiondag"
105
106static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
107 cl::Hidden, cl::init(true),
108 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
109
110static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
111 cl::desc("Number limit for gluing ld/st of memcpy."),
112 cl::Hidden, cl::init(0));
113
115 MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192),
116 cl::desc("DAG combiner limit number of steps when searching DAG "
117 "for predecessor nodes"));
118
120 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
121}
122
124
125//===----------------------------------------------------------------------===//
126// ConstantFPSDNode Class
127//===----------------------------------------------------------------------===//
128
129/// isExactlyValue - We don't rely on operator== working on double values, as
130/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
131/// As such, this method can be used to do an exact bit-for-bit comparison of
132/// two floating point values.
134 return getValueAPF().bitwiseIsEqual(V);
135}
136
138 const APFloat& Val) {
139 assert(VT.isFloatingPoint() && "Can only convert between FP types");
140
141 // convert modifies in place, so make a copy.
142 APFloat Val2 = APFloat(Val);
143 bool losesInfo;
145 &losesInfo);
146 return !losesInfo;
147}
148
149//===----------------------------------------------------------------------===//
150// ISD Namespace
151//===----------------------------------------------------------------------===//
152
153bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
154 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
155 unsigned EltSize =
156 N->getValueType(0).getVectorElementType().getSizeInBits();
157 if (auto *Op0 = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
158 SplatVal = Op0->getAPIntValue().trunc(EltSize);
159 return true;
160 }
161 if (auto *Op0 = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) {
162 SplatVal = Op0->getValueAPF().bitcastToAPInt().trunc(EltSize);
163 return true;
164 }
165 }
166
167 auto *BV = dyn_cast<BuildVectorSDNode>(N);
168 if (!BV)
169 return false;
170
171 APInt SplatUndef;
172 unsigned SplatBitSize;
173 bool HasUndefs;
174 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
175 // Endianness does not matter here. We are checking for a splat given the
176 // element size of the vector, and if we find such a splat for little endian
177 // layout, then that should be valid also for big endian (as the full vector
178 // size is known to be a multiple of the element size).
179 const bool IsBigEndian = false;
180 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
181 EltSize, IsBigEndian) &&
182 EltSize == SplatBitSize;
183}
184
185// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
186// specializations of the more general isConstantSplatVector()?
187
188bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
189 // Look through a bit convert.
190 while (N->getOpcode() == ISD::BITCAST)
191 N = N->getOperand(0).getNode();
192
193 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
194 APInt SplatVal;
195 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
196 }
197
198 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
199
200 unsigned i = 0, e = N->getNumOperands();
201
202 // Skip over all of the undef values.
203 while (i != e && N->getOperand(i).isUndef())
204 ++i;
205
206 // Do not accept an all-undef vector.
207 if (i == e) return false;
208
209 // Do not accept build_vectors that aren't all constants or which have non-~0
210 // elements. We have to be a bit careful here, as the type of the constant
211 // may not be the same as the type of the vector elements due to type
212 // legalization (the elements are promoted to a legal type for the target and
213 // a vector of a type may be legal when the base element type is not).
214 // We only want to check enough bits to cover the vector elements, because
215 // we care if the resultant vector is all ones, not whether the individual
216 // constants are.
217 SDValue NotZero = N->getOperand(i);
218 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
219 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
220 if (CN->getAPIntValue().countr_one() < EltSize)
221 return false;
222 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
223 if (CFPN->getValueAPF().bitcastToAPInt().countr_one() < EltSize)
224 return false;
225 } else
226 return false;
227
228 // Okay, we have at least one ~0 value, check to see if the rest match or are
229 // undefs. Even with the above element type twiddling, this should be OK, as
230 // the same type legalization should have applied to all the elements.
231 for (++i; i != e; ++i)
232 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
233 return false;
234 return true;
235}
236
237bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
238 // Look through a bit convert.
239 while (N->getOpcode() == ISD::BITCAST)
240 N = N->getOperand(0).getNode();
241
242 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
243 APInt SplatVal;
244 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
245 }
246
247 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
248
249 bool IsAllUndef = true;
250 for (const SDValue &Op : N->op_values()) {
251 if (Op.isUndef())
252 continue;
253 IsAllUndef = false;
254 // Do not accept build_vectors that aren't all constants or which have non-0
255 // elements. We have to be a bit careful here, as the type of the constant
256 // may not be the same as the type of the vector elements due to type
257 // legalization (the elements are promoted to a legal type for the target
258 // and a vector of a type may be legal when the base element type is not).
259 // We only want to check enough bits to cover the vector elements, because
260 // we care if the resultant vector is all zeros, not whether the individual
261 // constants are.
262 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
263 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
264 if (CN->getAPIntValue().countr_zero() < EltSize)
265 return false;
266 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
267 if (CFPN->getValueAPF().bitcastToAPInt().countr_zero() < EltSize)
268 return false;
269 } else
270 return false;
271 }
272
273 // Do not accept an all-undef vector.
274 if (IsAllUndef)
275 return false;
276 return true;
277}
278
280 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
281}
282
284 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
285}
286
288 if (N->getOpcode() != ISD::BUILD_VECTOR)
289 return false;
290
291 for (const SDValue &Op : N->op_values()) {
292 if (Op.isUndef())
293 continue;
294 if (!isa<ConstantSDNode>(Op))
295 return false;
296 }
297 return true;
298}
299
301 if (N->getOpcode() != ISD::BUILD_VECTOR)
302 return false;
303
304 for (const SDValue &Op : N->op_values()) {
305 if (Op.isUndef())
306 continue;
307 if (!isa<ConstantFPSDNode>(Op))
308 return false;
309 }
310 return true;
311}
312
313bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
314 bool Signed) {
315 assert(N->getValueType(0).isVector() && "Expected a vector!");
316
317 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
318 if (EltSize <= NewEltSize)
319 return false;
320
321 if (N->getOpcode() == ISD::ZERO_EXTEND) {
322 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
323 NewEltSize) &&
324 !Signed;
325 }
326 if (N->getOpcode() == ISD::SIGN_EXTEND) {
327 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
328 NewEltSize) &&
329 Signed;
330 }
331 if (N->getOpcode() != ISD::BUILD_VECTOR)
332 return false;
333
334 for (const SDValue &Op : N->op_values()) {
335 if (Op.isUndef())
336 continue;
337 if (!isa<ConstantSDNode>(Op))
338 return false;
339
340 APInt C = Op->getAsAPIntVal().trunc(EltSize);
341 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
342 return false;
343 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
344 return false;
345 }
346
347 return true;
348}
349
351 // Return false if the node has no operands.
352 // This is "logically inconsistent" with the definition of "all" but
353 // is probably the desired behavior.
354 if (N->getNumOperands() == 0)
355 return false;
356 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
357}
358
360 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
361}
362
363template <typename ConstNodeType>
365 std::function<bool(ConstNodeType *)> Match,
366 bool AllowUndefs, bool AllowTruncation) {
367 // FIXME: Add support for scalar UNDEF cases?
368 if (auto *C = dyn_cast<ConstNodeType>(Op))
369 return Match(C);
370
371 // FIXME: Add support for vector UNDEF cases?
372 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
373 ISD::SPLAT_VECTOR != Op.getOpcode())
374 return false;
375
376 EVT SVT = Op.getValueType().getScalarType();
377 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
378 if (AllowUndefs && Op.getOperand(i).isUndef()) {
379 if (!Match(nullptr))
380 return false;
381 continue;
382 }
383
384 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
385 if (!Cst || (!AllowTruncation && Cst->getValueType(0) != SVT) ||
386 !Match(Cst))
387 return false;
388 }
389 return true;
390}
391// Build used template types.
392template bool ISD::matchUnaryPredicateImpl<ConstantSDNode>(
393 SDValue, std::function<bool(ConstantSDNode *)>, bool, bool);
394template bool ISD::matchUnaryPredicateImpl<ConstantFPSDNode>(
395 SDValue, std::function<bool(ConstantFPSDNode *)>, bool, bool);
396
398 SDValue LHS, SDValue RHS,
399 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
400 bool AllowUndefs, bool AllowTypeMismatch) {
401 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
402 return false;
403
404 // TODO: Add support for scalar UNDEF cases?
405 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
406 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
407 return Match(LHSCst, RHSCst);
408
409 // TODO: Add support for vector UNDEF cases?
410 if (LHS.getOpcode() != RHS.getOpcode() ||
411 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
412 LHS.getOpcode() != ISD::SPLAT_VECTOR))
413 return false;
414
415 EVT SVT = LHS.getValueType().getScalarType();
416 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
417 SDValue LHSOp = LHS.getOperand(i);
418 SDValue RHSOp = RHS.getOperand(i);
419 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
420 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
421 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
422 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
423 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
424 return false;
425 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
426 LHSOp.getValueType() != RHSOp.getValueType()))
427 return false;
428 if (!Match(LHSCst, RHSCst))
429 return false;
430 }
431 return true;
432}
433
435 switch (MinMaxOpc) {
436 default:
437 llvm_unreachable("unrecognized opcode");
438 case ISD::UMIN:
439 return ISD::UMAX;
440 case ISD::UMAX:
441 return ISD::UMIN;
442 case ISD::SMIN:
443 return ISD::SMAX;
444 case ISD::SMAX:
445 return ISD::SMIN;
446 }
447}
448
450 switch (VecReduceOpcode) {
451 default:
452 llvm_unreachable("Expected VECREDUCE opcode");
455 case ISD::VP_REDUCE_FADD:
456 case ISD::VP_REDUCE_SEQ_FADD:
457 return ISD::FADD;
460 case ISD::VP_REDUCE_FMUL:
461 case ISD::VP_REDUCE_SEQ_FMUL:
462 return ISD::FMUL;
464 case ISD::VP_REDUCE_ADD:
465 return ISD::ADD;
467 case ISD::VP_REDUCE_MUL:
468 return ISD::MUL;
470 case ISD::VP_REDUCE_AND:
471 return ISD::AND;
473 case ISD::VP_REDUCE_OR:
474 return ISD::OR;
476 case ISD::VP_REDUCE_XOR:
477 return ISD::XOR;
479 case ISD::VP_REDUCE_SMAX:
480 return ISD::SMAX;
482 case ISD::VP_REDUCE_SMIN:
483 return ISD::SMIN;
485 case ISD::VP_REDUCE_UMAX:
486 return ISD::UMAX;
488 case ISD::VP_REDUCE_UMIN:
489 return ISD::UMIN;
491 case ISD::VP_REDUCE_FMAX:
492 return ISD::FMAXNUM;
494 case ISD::VP_REDUCE_FMIN:
495 return ISD::FMINNUM;
497 case ISD::VP_REDUCE_FMAXIMUM:
498 return ISD::FMAXIMUM;
500 case ISD::VP_REDUCE_FMINIMUM:
501 return ISD::FMINIMUM;
502 }
503}
504
505bool ISD::isVPOpcode(unsigned Opcode) {
506 switch (Opcode) {
507 default:
508 return false;
509#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
510 case ISD::VPSD: \
511 return true;
512#include "llvm/IR/VPIntrinsics.def"
513 }
514}
515
516bool ISD::isVPBinaryOp(unsigned Opcode) {
517 switch (Opcode) {
518 default:
519 break;
520#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
521#define VP_PROPERTY_BINARYOP return true;
522#define END_REGISTER_VP_SDNODE(VPSD) break;
523#include "llvm/IR/VPIntrinsics.def"
524 }
525 return false;
526}
527
528bool ISD::isVPReduction(unsigned Opcode) {
529 switch (Opcode) {
530 default:
531 return false;
532 case ISD::VP_REDUCE_ADD:
533 case ISD::VP_REDUCE_MUL:
534 case ISD::VP_REDUCE_AND:
535 case ISD::VP_REDUCE_OR:
536 case ISD::VP_REDUCE_XOR:
537 case ISD::VP_REDUCE_SMAX:
538 case ISD::VP_REDUCE_SMIN:
539 case ISD::VP_REDUCE_UMAX:
540 case ISD::VP_REDUCE_UMIN:
541 case ISD::VP_REDUCE_FMAX:
542 case ISD::VP_REDUCE_FMIN:
543 case ISD::VP_REDUCE_FMAXIMUM:
544 case ISD::VP_REDUCE_FMINIMUM:
545 case ISD::VP_REDUCE_FADD:
546 case ISD::VP_REDUCE_FMUL:
547 case ISD::VP_REDUCE_SEQ_FADD:
548 case ISD::VP_REDUCE_SEQ_FMUL:
549 return true;
550 }
551}
552
553/// The operand position of the vector mask.
554std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
555 switch (Opcode) {
556 default:
557 return std::nullopt;
558#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
559 case ISD::VPSD: \
560 return MASKPOS;
561#include "llvm/IR/VPIntrinsics.def"
562 }
563}
564
565/// The operand position of the explicit vector length parameter.
566std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
567 switch (Opcode) {
568 default:
569 return std::nullopt;
570#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
571 case ISD::VPSD: \
572 return EVLPOS;
573#include "llvm/IR/VPIntrinsics.def"
574 }
575}
576
577std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
578 bool hasFPExcept) {
579 // FIXME: Return strict opcodes in case of fp exceptions.
580 switch (VPOpcode) {
581 default:
582 return std::nullopt;
583#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
584#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
585#define END_REGISTER_VP_SDNODE(VPOPC) break;
586#include "llvm/IR/VPIntrinsics.def"
587 }
588 return std::nullopt;
589}
590
591std::optional<unsigned> ISD::getVPForBaseOpcode(unsigned Opcode) {
592 switch (Opcode) {
593 default:
594 return std::nullopt;
595#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
596#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
597#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
598#include "llvm/IR/VPIntrinsics.def"
599 }
600}
601
603 switch (ExtType) {
604 case ISD::EXTLOAD:
605 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
606 case ISD::SEXTLOAD:
607 return ISD::SIGN_EXTEND;
608 case ISD::ZEXTLOAD:
609 return ISD::ZERO_EXTEND;
610 default:
611 break;
612 }
613
614 llvm_unreachable("Invalid LoadExtType");
615}
616
618 // To perform this operation, we just need to swap the L and G bits of the
619 // operation.
620 unsigned OldL = (Operation >> 2) & 1;
621 unsigned OldG = (Operation >> 1) & 1;
622 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
623 (OldL << 1) | // New G bit
624 (OldG << 2)); // New L bit.
625}
626
628 unsigned Operation = Op;
629 if (isIntegerLike)
630 Operation ^= 7; // Flip L, G, E bits, but not U.
631 else
632 Operation ^= 15; // Flip all of the condition bits.
633
635 Operation &= ~8; // Don't let N and U bits get set.
636
637 return ISD::CondCode(Operation);
638}
639
641 return getSetCCInverseImpl(Op, Type.isInteger());
642}
643
645 bool isIntegerLike) {
646 return getSetCCInverseImpl(Op, isIntegerLike);
647}
648
649/// For an integer comparison, return 1 if the comparison is a signed operation
650/// and 2 if the result is an unsigned comparison. Return zero if the operation
651/// does not depend on the sign of the input (setne and seteq).
652static int isSignedOp(ISD::CondCode Opcode) {
653 switch (Opcode) {
654 default: llvm_unreachable("Illegal integer setcc operation!");
655 case ISD::SETEQ:
656 case ISD::SETNE: return 0;
657 case ISD::SETLT:
658 case ISD::SETLE:
659 case ISD::SETGT:
660 case ISD::SETGE: return 1;
661 case ISD::SETULT:
662 case ISD::SETULE:
663 case ISD::SETUGT:
664 case ISD::SETUGE: return 2;
665 }
666}
667
669 EVT Type) {
670 bool IsInteger = Type.isInteger();
671 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
672 // Cannot fold a signed integer setcc with an unsigned integer setcc.
673 return ISD::SETCC_INVALID;
674
675 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
676
677 // If the N and U bits get set, then the resultant comparison DOES suddenly
678 // care about orderedness, and it is true when ordered.
679 if (Op > ISD::SETTRUE2)
680 Op &= ~16; // Clear the U bit if the N bit is set.
681
682 // Canonicalize illegal integer setcc's.
683 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
684 Op = ISD::SETNE;
685
686 return ISD::CondCode(Op);
687}
688
690 EVT Type) {
691 bool IsInteger = Type.isInteger();
692 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
693 // Cannot fold a signed setcc with an unsigned setcc.
694 return ISD::SETCC_INVALID;
695
696 // Combine all of the condition bits.
697 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
698
699 // Canonicalize illegal integer setcc's.
700 if (IsInteger) {
701 switch (Result) {
702 default: break;
703 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
704 case ISD::SETOEQ: // SETEQ & SETU[LG]E
705 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
706 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
707 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
708 }
709 }
710
711 return Result;
712}
713
714//===----------------------------------------------------------------------===//
715// SDNode Profile Support
716//===----------------------------------------------------------------------===//
717
718/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
719static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
720 ID.AddInteger(OpC);
721}
722
723/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
724/// solely with their pointer.
726 ID.AddPointer(VTList.VTs);
727}
728
729/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
731 ArrayRef<SDValue> Ops) {
732 for (const auto &Op : Ops) {
733 ID.AddPointer(Op.getNode());
734 ID.AddInteger(Op.getResNo());
735 }
736}
737
738/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
740 ArrayRef<SDUse> Ops) {
741 for (const auto &Op : Ops) {
742 ID.AddPointer(Op.getNode());
743 ID.AddInteger(Op.getResNo());
744 }
745}
746
747static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
748 SDVTList VTList, ArrayRef<SDValue> OpList) {
749 AddNodeIDOpcode(ID, OpC);
750 AddNodeIDValueTypes(ID, VTList);
751 AddNodeIDOperands(ID, OpList);
752}
753
754/// If this is an SDNode with special info, add this info to the NodeID data.
756 switch (N->getOpcode()) {
759 case ISD::MCSymbol:
760 llvm_unreachable("Should only be used on nodes with operands");
761 default: break; // Normal nodes don't need extra info.
763 case ISD::Constant: {
764 const ConstantSDNode *C = cast<ConstantSDNode>(N);
765 ID.AddPointer(C->getConstantIntValue());
766 ID.AddBoolean(C->isOpaque());
767 break;
768 }
770 case ISD::ConstantFP:
771 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
772 break;
777 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
778 ID.AddPointer(GA->getGlobal());
779 ID.AddInteger(GA->getOffset());
780 ID.AddInteger(GA->getTargetFlags());
781 break;
782 }
783 case ISD::BasicBlock:
784 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
785 break;
786 case ISD::Register:
787 ID.AddInteger(cast<RegisterSDNode>(N)->getReg().id());
788 break;
790 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
791 break;
792 case ISD::SRCVALUE:
793 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
794 break;
795 case ISD::FrameIndex:
797 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
798 break;
801 if (cast<LifetimeSDNode>(N)->hasOffset()) {
802 ID.AddInteger(cast<LifetimeSDNode>(N)->getSize());
803 ID.AddInteger(cast<LifetimeSDNode>(N)->getOffset());
804 }
805 break;
807 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
808 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
809 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
810 break;
811 case ISD::JumpTable:
813 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
814 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
815 break;
818 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
819 ID.AddInteger(CP->getAlign().value());
820 ID.AddInteger(CP->getOffset());
821 if (CP->isMachineConstantPoolEntry())
822 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
823 else
824 ID.AddPointer(CP->getConstVal());
825 ID.AddInteger(CP->getTargetFlags());
826 break;
827 }
828 case ISD::TargetIndex: {
829 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
830 ID.AddInteger(TI->getIndex());
831 ID.AddInteger(TI->getOffset());
832 ID.AddInteger(TI->getTargetFlags());
833 break;
834 }
835 case ISD::LOAD: {
836 const LoadSDNode *LD = cast<LoadSDNode>(N);
837 ID.AddInteger(LD->getMemoryVT().getRawBits());
838 ID.AddInteger(LD->getRawSubclassData());
839 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
840 ID.AddInteger(LD->getMemOperand()->getFlags());
841 break;
842 }
843 case ISD::STORE: {
844 const StoreSDNode *ST = cast<StoreSDNode>(N);
845 ID.AddInteger(ST->getMemoryVT().getRawBits());
846 ID.AddInteger(ST->getRawSubclassData());
847 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
848 ID.AddInteger(ST->getMemOperand()->getFlags());
849 break;
850 }
851 case ISD::VP_LOAD: {
852 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
853 ID.AddInteger(ELD->getMemoryVT().getRawBits());
854 ID.AddInteger(ELD->getRawSubclassData());
855 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
856 ID.AddInteger(ELD->getMemOperand()->getFlags());
857 break;
858 }
859 case ISD::VP_STORE: {
860 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
861 ID.AddInteger(EST->getMemoryVT().getRawBits());
862 ID.AddInteger(EST->getRawSubclassData());
863 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
864 ID.AddInteger(EST->getMemOperand()->getFlags());
865 break;
866 }
867 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
868 const VPStridedLoadSDNode *SLD = cast<VPStridedLoadSDNode>(N);
869 ID.AddInteger(SLD->getMemoryVT().getRawBits());
870 ID.AddInteger(SLD->getRawSubclassData());
871 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
872 break;
873 }
874 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
875 const VPStridedStoreSDNode *SST = cast<VPStridedStoreSDNode>(N);
876 ID.AddInteger(SST->getMemoryVT().getRawBits());
877 ID.AddInteger(SST->getRawSubclassData());
878 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
879 break;
880 }
881 case ISD::VP_GATHER: {
882 const VPGatherSDNode *EG = cast<VPGatherSDNode>(N);
883 ID.AddInteger(EG->getMemoryVT().getRawBits());
884 ID.AddInteger(EG->getRawSubclassData());
885 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
886 ID.AddInteger(EG->getMemOperand()->getFlags());
887 break;
888 }
889 case ISD::VP_SCATTER: {
890 const VPScatterSDNode *ES = cast<VPScatterSDNode>(N);
891 ID.AddInteger(ES->getMemoryVT().getRawBits());
892 ID.AddInteger(ES->getRawSubclassData());
893 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
894 ID.AddInteger(ES->getMemOperand()->getFlags());
895 break;
896 }
897 case ISD::MLOAD: {
898 const MaskedLoadSDNode *MLD = cast<MaskedLoadSDNode>(N);
899 ID.AddInteger(MLD->getMemoryVT().getRawBits());
900 ID.AddInteger(MLD->getRawSubclassData());
901 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
902 ID.AddInteger(MLD->getMemOperand()->getFlags());
903 break;
904 }
905 case ISD::MSTORE: {
906 const MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
907 ID.AddInteger(MST->getMemoryVT().getRawBits());
908 ID.AddInteger(MST->getRawSubclassData());
909 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
910 ID.AddInteger(MST->getMemOperand()->getFlags());
911 break;
912 }
913 case ISD::MGATHER: {
914 const MaskedGatherSDNode *MG = cast<MaskedGatherSDNode>(N);
915 ID.AddInteger(MG->getMemoryVT().getRawBits());
916 ID.AddInteger(MG->getRawSubclassData());
917 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
918 ID.AddInteger(MG->getMemOperand()->getFlags());
919 break;
920 }
921 case ISD::MSCATTER: {
922 const MaskedScatterSDNode *MS = cast<MaskedScatterSDNode>(N);
923 ID.AddInteger(MS->getMemoryVT().getRawBits());
924 ID.AddInteger(MS->getRawSubclassData());
925 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
926 ID.AddInteger(MS->getMemOperand()->getFlags());
927 break;
928 }
931 case ISD::ATOMIC_SWAP:
943 case ISD::ATOMIC_LOAD:
944 case ISD::ATOMIC_STORE: {
945 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
946 ID.AddInteger(AT->getMemoryVT().getRawBits());
947 ID.AddInteger(AT->getRawSubclassData());
948 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
949 ID.AddInteger(AT->getMemOperand()->getFlags());
950 break;
951 }
952 case ISD::VECTOR_SHUFFLE: {
953 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
954 for (int M : Mask)
955 ID.AddInteger(M);
956 break;
957 }
958 case ISD::ADDRSPACECAST: {
959 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(N);
960 ID.AddInteger(ASC->getSrcAddressSpace());
961 ID.AddInteger(ASC->getDestAddressSpace());
962 break;
963 }
965 case ISD::BlockAddress: {
966 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
967 ID.AddPointer(BA->getBlockAddress());
968 ID.AddInteger(BA->getOffset());
969 ID.AddInteger(BA->getTargetFlags());
970 break;
971 }
972 case ISD::AssertAlign:
973 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
974 break;
975 case ISD::PREFETCH:
978 // Handled by MemIntrinsicSDNode check after the switch.
979 break;
980 } // end switch (N->getOpcode())
981
982 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
983 // to check.
984 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
985 ID.AddInteger(MN->getRawSubclassData());
986 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
987 ID.AddInteger(MN->getMemOperand()->getFlags());
988 ID.AddInteger(MN->getMemoryVT().getRawBits());
989 }
990}
991
992/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
993/// data.
994static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
995 AddNodeIDOpcode(ID, N->getOpcode());
996 // Add the return value info.
997 AddNodeIDValueTypes(ID, N->getVTList());
998 // Add the operand info.
999 AddNodeIDOperands(ID, N->ops());
1000
1001 // Handle SDNode leafs with special info.
1003}
1004
1005//===----------------------------------------------------------------------===//
1006// SelectionDAG Class
1007//===----------------------------------------------------------------------===//
1008
1009/// doNotCSE - Return true if CSE should not be performed for this node.
1010static bool doNotCSE(SDNode *N) {
1011 if (N->getValueType(0) == MVT::Glue)
1012 return true; // Never CSE anything that produces a glue result.
1013
1014 switch (N->getOpcode()) {
1015 default: break;
1016 case ISD::HANDLENODE:
1017 case ISD::EH_LABEL:
1018 return true; // Never CSE these nodes.
1019 }
1020
1021 // Check that remaining values produced are not flags.
1022 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
1023 if (N->getValueType(i) == MVT::Glue)
1024 return true; // Never CSE anything that produces a glue result.
1025
1026 return false;
1027}
1028
1029/// RemoveDeadNodes - This method deletes all unreachable nodes in the
1030/// SelectionDAG.
1032 // Create a dummy node (which is not added to allnodes), that adds a reference
1033 // to the root node, preventing it from being deleted.
1034 HandleSDNode Dummy(getRoot());
1035
1036 SmallVector<SDNode*, 128> DeadNodes;
1037
1038 // Add all obviously-dead nodes to the DeadNodes worklist.
1039 for (SDNode &Node : allnodes())
1040 if (Node.use_empty())
1041 DeadNodes.push_back(&Node);
1042
1043 RemoveDeadNodes(DeadNodes);
1044
1045 // If the root changed (e.g. it was a dead load, update the root).
1046 setRoot(Dummy.getValue());
1047}
1048
1049/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1050/// given list, and any nodes that become unreachable as a result.
1052
1053 // Process the worklist, deleting the nodes and adding their uses to the
1054 // worklist.
1055 while (!DeadNodes.empty()) {
1056 SDNode *N = DeadNodes.pop_back_val();
1057 // Skip to next node if we've already managed to delete the node. This could
1058 // happen if replacing a node causes a node previously added to the node to
1059 // be deleted.
1060 if (N->getOpcode() == ISD::DELETED_NODE)
1061 continue;
1062
1063 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1064 DUL->NodeDeleted(N, nullptr);
1065
1066 // Take the node out of the appropriate CSE map.
1067 RemoveNodeFromCSEMaps(N);
1068
1069 // Next, brutally remove the operand list. This is safe to do, as there are
1070 // no cycles in the graph.
1071 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1072 SDUse &Use = *I++;
1073 SDNode *Operand = Use.getNode();
1074 Use.set(SDValue());
1075
1076 // Now that we removed this operand, see if there are no uses of it left.
1077 if (Operand->use_empty())
1078 DeadNodes.push_back(Operand);
1079 }
1080
1081 DeallocateNode(N);
1082 }
1083}
1084
1086 SmallVector<SDNode*, 16> DeadNodes(1, N);
1087
1088 // Create a dummy node that adds a reference to the root node, preventing
1089 // it from being deleted. (This matters if the root is an operand of the
1090 // dead node.)
1091 HandleSDNode Dummy(getRoot());
1092
1093 RemoveDeadNodes(DeadNodes);
1094}
1095
1097 // First take this out of the appropriate CSE map.
1098 RemoveNodeFromCSEMaps(N);
1099
1100 // Finally, remove uses due to operands of this node, remove from the
1101 // AllNodes list, and delete the node.
1102 DeleteNodeNotInCSEMaps(N);
1103}
1104
1105void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1106 assert(N->getIterator() != AllNodes.begin() &&
1107 "Cannot delete the entry node!");
1108 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1109
1110 // Drop all of the operands and decrement used node's use counts.
1111 N->DropOperands();
1112
1113 DeallocateNode(N);
1114}
1115
1116void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1117 assert(!(V->isVariadic() && isParameter));
1118 if (isParameter)
1119 ByvalParmDbgValues.push_back(V);
1120 else
1121 DbgValues.push_back(V);
1122 for (const SDNode *Node : V->getSDNodes())
1123 if (Node)
1124 DbgValMap[Node].push_back(V);
1125}
1126
1127void SDDbgInfo::erase(const SDNode *Node) {
1128 DbgValMapType::iterator I = DbgValMap.find(Node);
1129 if (I == DbgValMap.end())
1130 return;
1131 for (auto &Val: I->second)
1132 Val->setIsInvalidated();
1133 DbgValMap.erase(I);
1134}
1135
1136void SelectionDAG::DeallocateNode(SDNode *N) {
1137 // If we have operands, deallocate them.
1138 removeOperands(N);
1139
1140 NodeAllocator.Deallocate(AllNodes.remove(N));
1141
1142 // Set the opcode to DELETED_NODE to help catch bugs when node
1143 // memory is reallocated.
1144 // FIXME: There are places in SDag that have grown a dependency on the opcode
1145 // value in the released node.
1146 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1147 N->NodeType = ISD::DELETED_NODE;
1148
1149 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1150 // them and forget about that node.
1151 DbgInfo->erase(N);
1152
1153 // Invalidate extra info.
1154 SDEI.erase(N);
1155}
1156
1157#ifndef NDEBUG
1158/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1159static void VerifySDNode(SDNode *N, const TargetLowering *TLI) {
1160 switch (N->getOpcode()) {
1161 default:
1162 if (N->getOpcode() > ISD::BUILTIN_OP_END)
1163 TLI->verifyTargetSDNode(N);
1164 break;
1165 case ISD::BUILD_PAIR: {
1166 EVT VT = N->getValueType(0);
1167 assert(N->getNumValues() == 1 && "Too many results!");
1168 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1169 "Wrong return type!");
1170 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1171 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1172 "Mismatched operand types!");
1173 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1174 "Wrong operand type!");
1175 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1176 "Wrong return type size");
1177 break;
1178 }
1179 case ISD::BUILD_VECTOR: {
1180 assert(N->getNumValues() == 1 && "Too many results!");
1181 assert(N->getValueType(0).isVector() && "Wrong return type!");
1182 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1183 "Wrong number of operands!");
1184 EVT EltVT = N->getValueType(0).getVectorElementType();
1185 for (const SDUse &Op : N->ops()) {
1186 assert((Op.getValueType() == EltVT ||
1187 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1188 EltVT.bitsLE(Op.getValueType()))) &&
1189 "Wrong operand type!");
1190 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1191 "Operands must all have the same type");
1192 }
1193 break;
1194 }
1195 }
1196}
1197#endif // NDEBUG
1198
1199/// Insert a newly allocated node into the DAG.
1200///
1201/// Handles insertion into the all nodes list and CSE map, as well as
1202/// verification and other common operations when a new node is allocated.
1203void SelectionDAG::InsertNode(SDNode *N) {
1204 AllNodes.push_back(N);
1205#ifndef NDEBUG
1206 N->PersistentId = NextPersistentId++;
1207 VerifySDNode(N, TLI);
1208#endif
1209 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1210 DUL->NodeInserted(N);
1211}
1212
1213/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1214/// correspond to it. This is useful when we're about to delete or repurpose
1215/// the node. We don't want future request for structurally identical nodes
1216/// to return N anymore.
1217bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1218 bool Erased = false;
1219 switch (N->getOpcode()) {
1220 case ISD::HANDLENODE: return false; // noop.
1221 case ISD::CONDCODE:
1222 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1223 "Cond code doesn't exist!");
1224 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1225 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1226 break;
1228 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1229 break;
1231 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1232 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1233 ESN->getSymbol(), ESN->getTargetFlags()));
1234 break;
1235 }
1236 case ISD::MCSymbol: {
1237 auto *MCSN = cast<MCSymbolSDNode>(N);
1238 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1239 break;
1240 }
1241 case ISD::VALUETYPE: {
1242 EVT VT = cast<VTSDNode>(N)->getVT();
1243 if (VT.isExtended()) {
1244 Erased = ExtendedValueTypeNodes.erase(VT);
1245 } else {
1246 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1247 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1248 }
1249 break;
1250 }
1251 default:
1252 // Remove it from the CSE Map.
1253 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1254 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1255 Erased = CSEMap.RemoveNode(N);
1256 break;
1257 }
1258#ifndef NDEBUG
1259 // Verify that the node was actually in one of the CSE maps, unless it has a
1260 // glue result (which cannot be CSE'd) or is one of the special cases that are
1261 // not subject to CSE.
1262 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1263 !N->isMachineOpcode() && !doNotCSE(N)) {
1264 N->dump(this);
1265 dbgs() << "\n";
1266 llvm_unreachable("Node is not in map!");
1267 }
1268#endif
1269 return Erased;
1270}
1271
1272/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1273/// maps and modified in place. Add it back to the CSE maps, unless an identical
1274/// node already exists, in which case transfer all its users to the existing
1275/// node. This transfer can potentially trigger recursive merging.
1276void
1277SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1278 // For node types that aren't CSE'd, just act as if no identical node
1279 // already exists.
1280 if (!doNotCSE(N)) {
1281 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1282 if (Existing != N) {
1283 // If there was already an existing matching node, use ReplaceAllUsesWith
1284 // to replace the dead one with the existing one. This can cause
1285 // recursive merging of other unrelated nodes down the line.
1286 Existing->intersectFlagsWith(N->getFlags());
1287 ReplaceAllUsesWith(N, Existing);
1288
1289 // N is now dead. Inform the listeners and delete it.
1290 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1291 DUL->NodeDeleted(N, Existing);
1292 DeleteNodeNotInCSEMaps(N);
1293 return;
1294 }
1295 }
1296
1297 // If the node doesn't already exist, we updated it. Inform listeners.
1298 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1299 DUL->NodeUpdated(N);
1300}
1301
1302/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1303/// were replaced with those specified. If this node is never memoized,
1304/// return null, otherwise return a pointer to the slot it would take. If a
1305/// node already exists with these operands, the slot will be non-null.
1306SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1307 void *&InsertPos) {
1308 if (doNotCSE(N))
1309 return nullptr;
1310
1311 SDValue Ops[] = { Op };
1313 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1315 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1316 if (Node)
1317 Node->intersectFlagsWith(N->getFlags());
1318 return Node;
1319}
1320
1321/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1322/// were replaced with those specified. If this node is never memoized,
1323/// return null, otherwise return a pointer to the slot it would take. If a
1324/// node already exists with these operands, the slot will be non-null.
1325SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1326 SDValue Op1, SDValue Op2,
1327 void *&InsertPos) {
1328 if (doNotCSE(N))
1329 return nullptr;
1330
1331 SDValue Ops[] = { Op1, Op2 };
1333 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1335 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1336 if (Node)
1337 Node->intersectFlagsWith(N->getFlags());
1338 return Node;
1339}
1340
1341/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1342/// were replaced with those specified. If this node is never memoized,
1343/// return null, otherwise return a pointer to the slot it would take. If a
1344/// node already exists with these operands, the slot will be non-null.
1345SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1346 void *&InsertPos) {
1347 if (doNotCSE(N))
1348 return nullptr;
1349
1351 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1353 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1354 if (Node)
1355 Node->intersectFlagsWith(N->getFlags());
1356 return Node;
1357}
1358
1360 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1361 : VT.getTypeForEVT(*getContext());
1362
1363 return getDataLayout().getABITypeAlign(Ty);
1364}
1365
1366// EntryNode could meaningfully have debug info if we can find it...
1368 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1369 getVTList(MVT::Other, MVT::Glue)),
1370 Root(getEntryNode()) {
1371 InsertNode(&EntryNode);
1372 DbgInfo = new SDDbgInfo();
1373}
1374
1376 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1377 const TargetLibraryInfo *LibraryInfo,
1378 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1380 FunctionVarLocs const *VarLocs) {
1381 MF = &NewMF;
1382 SDAGISelPass = PassPtr;
1383 ORE = &NewORE;
1386 LibInfo = LibraryInfo;
1387 Context = &MF->getFunction().getContext();
1388 UA = NewUA;
1389 PSI = PSIin;
1390 BFI = BFIin;
1391 MMI = &MMIin;
1392 FnVarLocs = VarLocs;
1393}
1394
1396 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1397 allnodes_clear();
1398 OperandRecycler.clear(OperandAllocator);
1399 delete DbgInfo;
1400}
1401
1403 return llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1404}
1405
1406void SelectionDAG::allnodes_clear() {
1407 assert(&*AllNodes.begin() == &EntryNode);
1408 AllNodes.remove(AllNodes.begin());
1409 while (!AllNodes.empty())
1410 DeallocateNode(&AllNodes.front());
1411#ifndef NDEBUG
1412 NextPersistentId = 0;
1413#endif
1414}
1415
1416SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1417 void *&InsertPos) {
1418 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1419 if (N) {
1420 switch (N->getOpcode()) {
1421 default: break;
1422 case ISD::Constant:
1423 case ISD::ConstantFP:
1424 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1425 "debug location. Use another overload.");
1426 }
1427 }
1428 return N;
1429}
1430
1431SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1432 const SDLoc &DL, void *&InsertPos) {
1433 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1434 if (N) {
1435 switch (N->getOpcode()) {
1436 case ISD::Constant:
1437 case ISD::ConstantFP:
1438 // Erase debug location from the node if the node is used at several
1439 // different places. Do not propagate one location to all uses as it
1440 // will cause a worse single stepping debugging experience.
1441 if (N->getDebugLoc() != DL.getDebugLoc())
1442 N->setDebugLoc(DebugLoc());
1443 break;
1444 default:
1445 // When the node's point of use is located earlier in the instruction
1446 // sequence than its prior point of use, update its debug info to the
1447 // earlier location.
1448 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1449 N->setDebugLoc(DL.getDebugLoc());
1450 break;
1451 }
1452 }
1453 return N;
1454}
1455
1457 allnodes_clear();
1458 OperandRecycler.clear(OperandAllocator);
1459 OperandAllocator.Reset();
1460 CSEMap.clear();
1461
1462 ExtendedValueTypeNodes.clear();
1463 ExternalSymbols.clear();
1464 TargetExternalSymbols.clear();
1465 MCSymbols.clear();
1466 SDEI.clear();
1467 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(), nullptr);
1468 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(), nullptr);
1469
1470 EntryNode.UseList = nullptr;
1471 InsertNode(&EntryNode);
1472 Root = getEntryNode();
1473 DbgInfo->clear();
1474}
1475
1477 return VT.bitsGT(Op.getValueType())
1478 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1479 : getNode(ISD::FP_ROUND, DL, VT, Op,
1480 getIntPtrConstant(0, DL, /*isTarget=*/true));
1481}
1482
1483std::pair<SDValue, SDValue>
1485 const SDLoc &DL, EVT VT) {
1486 assert(!VT.bitsEq(Op.getValueType()) &&
1487 "Strict no-op FP extend/round not allowed.");
1488 SDValue Res =
1489 VT.bitsGT(Op.getValueType())
1490 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1491 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1492 {Chain, Op, getIntPtrConstant(0, DL, /*isTarget=*/true)});
1493
1494 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1495}
1496
1498 return VT.bitsGT(Op.getValueType()) ?
1499 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1500 getNode(ISD::TRUNCATE, DL, VT, Op);
1501}
1502
1504 return VT.bitsGT(Op.getValueType()) ?
1505 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1506 getNode(ISD::TRUNCATE, DL, VT, Op);
1507}
1508
1510 return VT.bitsGT(Op.getValueType()) ?
1511 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1512 getNode(ISD::TRUNCATE, DL, VT, Op);
1513}
1514
1516 EVT VT) {
1517 assert(!VT.isVector());
1518 auto Type = Op.getValueType();
1519 SDValue DestOp;
1520 if (Type == VT)
1521 return Op;
1522 auto Size = Op.getValueSizeInBits();
1523 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1524 if (DestOp.getValueType() == VT)
1525 return DestOp;
1526
1527 return getAnyExtOrTrunc(DestOp, DL, VT);
1528}
1529
1531 EVT VT) {
1532 assert(!VT.isVector());
1533 auto Type = Op.getValueType();
1534 SDValue DestOp;
1535 if (Type == VT)
1536 return Op;
1537 auto Size = Op.getValueSizeInBits();
1538 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1539 if (DestOp.getValueType() == VT)
1540 return DestOp;
1541
1542 return getSExtOrTrunc(DestOp, DL, VT);
1543}
1544
1546 EVT VT) {
1547 assert(!VT.isVector());
1548 auto Type = Op.getValueType();
1549 SDValue DestOp;
1550 if (Type == VT)
1551 return Op;
1552 auto Size = Op.getValueSizeInBits();
1553 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1554 if (DestOp.getValueType() == VT)
1555 return DestOp;
1556
1557 return getZExtOrTrunc(DestOp, DL, VT);
1558}
1559
1561 EVT OpVT) {
1562 if (VT.bitsLE(Op.getValueType()))
1563 return getNode(ISD::TRUNCATE, SL, VT, Op);
1564
1566 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1567}
1568
1570 EVT OpVT = Op.getValueType();
1571 assert(VT.isInteger() && OpVT.isInteger() &&
1572 "Cannot getZeroExtendInReg FP types");
1573 assert(VT.isVector() == OpVT.isVector() &&
1574 "getZeroExtendInReg type should be vector iff the operand "
1575 "type is vector!");
1576 assert((!VT.isVector() ||
1578 "Vector element counts must match in getZeroExtendInReg");
1579 assert(VT.bitsLE(OpVT) && "Not extending!");
1580 if (OpVT == VT)
1581 return Op;
1583 VT.getScalarSizeInBits());
1584 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1585}
1586
1588 SDValue EVL, const SDLoc &DL,
1589 EVT VT) {
1590 EVT OpVT = Op.getValueType();
1591 assert(VT.isInteger() && OpVT.isInteger() &&
1592 "Cannot getVPZeroExtendInReg FP types");
1593 assert(VT.isVector() && OpVT.isVector() &&
1594 "getVPZeroExtendInReg type and operand type should be vector!");
1596 "Vector element counts must match in getZeroExtendInReg");
1597 assert(VT.bitsLE(OpVT) && "Not extending!");
1598 if (OpVT == VT)
1599 return Op;
1601 VT.getScalarSizeInBits());
1602 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1603 EVL);
1604}
1605
1607 // Only unsigned pointer semantics are supported right now. In the future this
1608 // might delegate to TLI to check pointer signedness.
1609 return getZExtOrTrunc(Op, DL, VT);
1610}
1611
1613 // Only unsigned pointer semantics are supported right now. In the future this
1614 // might delegate to TLI to check pointer signedness.
1615 return getZeroExtendInReg(Op, DL, VT);
1616}
1617
1619 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1620}
1621
1622/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1624 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1625}
1626
1628 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1629 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1630}
1631
1633 SDValue Mask, SDValue EVL, EVT VT) {
1634 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1635 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1636}
1637
1639 SDValue Mask, SDValue EVL) {
1640 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1641}
1642
1644 SDValue Mask, SDValue EVL) {
1645 if (VT.bitsGT(Op.getValueType()))
1646 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1647 if (VT.bitsLT(Op.getValueType()))
1648 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1649 return Op;
1650}
1651
1653 EVT OpVT) {
1654 if (!V)
1655 return getConstant(0, DL, VT);
1656
1657 switch (TLI->getBooleanContents(OpVT)) {
1660 return getConstant(1, DL, VT);
1662 return getAllOnesConstant(DL, VT);
1663 }
1664 llvm_unreachable("Unexpected boolean content enum!");
1665}
1666
1668 bool isT, bool isO) {
1669 return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
1670 DL, VT, isT, isO);
1671}
1672
1674 bool isT, bool isO) {
1675 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1676}
1677
1679 EVT VT, bool isT, bool isO) {
1680 assert(VT.isInteger() && "Cannot create FP integer constant!");
1681
1682 EVT EltVT = VT.getScalarType();
1683 const ConstantInt *Elt = &Val;
1684
1685 // Vector splats are explicit within the DAG, with ConstantSDNode holding the
1686 // to-be-splatted scalar ConstantInt.
1687 if (isa<VectorType>(Elt->getType()))
1688 Elt = ConstantInt::get(*getContext(), Elt->getValue());
1689
1690 // In some cases the vector type is legal but the element type is illegal and
1691 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1692 // inserted value (the type does not need to match the vector element type).
1693 // Any extra bits introduced will be truncated away.
1694 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1696 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1697 APInt NewVal;
1698 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1699 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1700 else
1701 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1702 Elt = ConstantInt::get(*getContext(), NewVal);
1703 }
1704 // In other cases the element type is illegal and needs to be expanded, for
1705 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1706 // the value into n parts and use a vector type with n-times the elements.
1707 // Then bitcast to the type requested.
1708 // Legalizing constants too early makes the DAGCombiner's job harder so we
1709 // only legalize if the DAG tells us we must produce legal types.
1710 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1711 TLI->getTypeAction(*getContext(), EltVT) ==
1713 const APInt &NewVal = Elt->getValue();
1714 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1715 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1716
1717 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1718 if (VT.isScalableVector() ||
1720 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1721 "Can only handle an even split!");
1722 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1723
1724 SmallVector<SDValue, 2> ScalarParts;
1725 for (unsigned i = 0; i != Parts; ++i)
1726 ScalarParts.push_back(getConstant(
1727 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1728 ViaEltVT, isT, isO));
1729
1730 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1731 }
1732
1733 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1734 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1735
1736 // Check the temporary vector is the correct size. If this fails then
1737 // getTypeToTransformTo() probably returned a type whose size (in bits)
1738 // isn't a power-of-2 factor of the requested type size.
1739 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1740
1741 SmallVector<SDValue, 2> EltParts;
1742 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1743 EltParts.push_back(getConstant(
1744 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1745 ViaEltVT, isT, isO));
1746
1747 // EltParts is currently in little endian order. If we actually want
1748 // big-endian order then reverse it now.
1749 if (getDataLayout().isBigEndian())
1750 std::reverse(EltParts.begin(), EltParts.end());
1751
1752 // The elements must be reversed when the element order is different
1753 // to the endianness of the elements (because the BITCAST is itself a
1754 // vector shuffle in this situation). However, we do not need any code to
1755 // perform this reversal because getConstant() is producing a vector
1756 // splat.
1757 // This situation occurs in MIPS MSA.
1758
1760 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1761 llvm::append_range(Ops, EltParts);
1762
1763 SDValue V =
1764 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1765 return V;
1766 }
1767
1768 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1769 "APInt size does not match type size!");
1770 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1771 SDVTList VTs = getVTList(EltVT);
1773 AddNodeIDNode(ID, Opc, VTs, {});
1774 ID.AddPointer(Elt);
1775 ID.AddBoolean(isO);
1776 void *IP = nullptr;
1777 SDNode *N = nullptr;
1778 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1779 if (!VT.isVector())
1780 return SDValue(N, 0);
1781
1782 if (!N) {
1783 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1784 CSEMap.InsertNode(N, IP);
1785 InsertNode(N);
1786 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1787 }
1788
1789 SDValue Result(N, 0);
1790 if (VT.isVector())
1791 Result = getSplat(VT, DL, Result);
1792 return Result;
1793}
1794
1796 bool isT, bool isO) {
1797 unsigned Size = VT.getScalarSizeInBits();
1798 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1799}
1800
1802 bool IsOpaque) {
1804 IsTarget, IsOpaque);
1805}
1806
1808 bool isTarget) {
1809 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1810}
1811
1813 const SDLoc &DL) {
1814 assert(VT.isInteger() && "Shift amount is not an integer type!");
1815 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1816 return getConstant(Val, DL, ShiftVT);
1817}
1818
1820 const SDLoc &DL) {
1821 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1822 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1823}
1824
1826 bool isTarget) {
1827 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1828}
1829
1831 bool isTarget) {
1832 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1833}
1834
1836 EVT VT, bool isTarget) {
1837 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1838
1839 EVT EltVT = VT.getScalarType();
1840 const ConstantFP *Elt = &V;
1841
1842 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1843 // the to-be-splatted scalar ConstantFP.
1844 if (isa<VectorType>(Elt->getType()))
1845 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1846
1847 // Do the map lookup using the actual bit pattern for the floating point
1848 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1849 // we don't have issues with SNANs.
1850 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1851 SDVTList VTs = getVTList(EltVT);
1853 AddNodeIDNode(ID, Opc, VTs, {});
1854 ID.AddPointer(Elt);
1855 void *IP = nullptr;
1856 SDNode *N = nullptr;
1857 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1858 if (!VT.isVector())
1859 return SDValue(N, 0);
1860
1861 if (!N) {
1862 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1863 CSEMap.InsertNode(N, IP);
1864 InsertNode(N);
1865 }
1866
1867 SDValue Result(N, 0);
1868 if (VT.isVector())
1869 Result = getSplat(VT, DL, Result);
1870 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1871 return Result;
1872}
1873
1875 bool isTarget) {
1876 EVT EltVT = VT.getScalarType();
1877 if (EltVT == MVT::f32)
1878 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1879 if (EltVT == MVT::f64)
1880 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1881 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1882 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1883 bool Ignored;
1884 APFloat APF = APFloat(Val);
1886 &Ignored);
1887 return getConstantFP(APF, DL, VT, isTarget);
1888 }
1889 llvm_unreachable("Unsupported type in getConstantFP");
1890}
1891
1893 EVT VT, int64_t Offset, bool isTargetGA,
1894 unsigned TargetFlags) {
1895 assert((TargetFlags == 0 || isTargetGA) &&
1896 "Cannot set target flags on target-independent globals");
1897
1898 // Truncate (with sign-extension) the offset value to the pointer size.
1900 if (BitWidth < 64)
1902
1903 unsigned Opc;
1904 if (GV->isThreadLocal())
1906 else
1907 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
1908
1909 SDVTList VTs = getVTList(VT);
1911 AddNodeIDNode(ID, Opc, VTs, {});
1912 ID.AddPointer(GV);
1913 ID.AddInteger(Offset);
1914 ID.AddInteger(TargetFlags);
1915 void *IP = nullptr;
1916 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1917 return SDValue(E, 0);
1918
1919 auto *N = newSDNode<GlobalAddressSDNode>(
1920 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1921 CSEMap.InsertNode(N, IP);
1922 InsertNode(N);
1923 return SDValue(N, 0);
1924}
1925
1926SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1927 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1928 SDVTList VTs = getVTList(VT);
1930 AddNodeIDNode(ID, Opc, VTs, {});
1931 ID.AddInteger(FI);
1932 void *IP = nullptr;
1933 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1934 return SDValue(E, 0);
1935
1936 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1937 CSEMap.InsertNode(N, IP);
1938 InsertNode(N);
1939 return SDValue(N, 0);
1940}
1941
1942SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1943 unsigned TargetFlags) {
1944 assert((TargetFlags == 0 || isTarget) &&
1945 "Cannot set target flags on target-independent jump tables");
1946 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1947 SDVTList VTs = getVTList(VT);
1949 AddNodeIDNode(ID, Opc, VTs, {});
1950 ID.AddInteger(JTI);
1951 ID.AddInteger(TargetFlags);
1952 void *IP = nullptr;
1953 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1954 return SDValue(E, 0);
1955
1956 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1957 CSEMap.InsertNode(N, IP);
1958 InsertNode(N);
1959 return SDValue(N, 0);
1960}
1961
1963 const SDLoc &DL) {
1965 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Glue, Chain,
1966 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1967}
1968
1970 MaybeAlign Alignment, int Offset,
1971 bool isTarget, unsigned TargetFlags) {
1972 assert((TargetFlags == 0 || isTarget) &&
1973 "Cannot set target flags on target-independent globals");
1974 if (!Alignment)
1975 Alignment = shouldOptForSize()
1976 ? getDataLayout().getABITypeAlign(C->getType())
1977 : getDataLayout().getPrefTypeAlign(C->getType());
1978 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1979 SDVTList VTs = getVTList(VT);
1981 AddNodeIDNode(ID, Opc, VTs, {});
1982 ID.AddInteger(Alignment->value());
1983 ID.AddInteger(Offset);
1984 ID.AddPointer(C);
1985 ID.AddInteger(TargetFlags);
1986 void *IP = nullptr;
1987 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1988 return SDValue(E, 0);
1989
1990 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1991 TargetFlags);
1992 CSEMap.InsertNode(N, IP);
1993 InsertNode(N);
1994 SDValue V = SDValue(N, 0);
1995 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
1996 return V;
1997}
1998
2000 MaybeAlign Alignment, int Offset,
2001 bool isTarget, unsigned TargetFlags) {
2002 assert((TargetFlags == 0 || isTarget) &&
2003 "Cannot set target flags on target-independent globals");
2004 if (!Alignment)
2005 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
2006 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2007 SDVTList VTs = getVTList(VT);
2009 AddNodeIDNode(ID, Opc, VTs, {});
2010 ID.AddInteger(Alignment->value());
2011 ID.AddInteger(Offset);
2012 C->addSelectionDAGCSEId(ID);
2013 ID.AddInteger(TargetFlags);
2014 void *IP = nullptr;
2015 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2016 return SDValue(E, 0);
2017
2018 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2019 TargetFlags);
2020 CSEMap.InsertNode(N, IP);
2021 InsertNode(N);
2022 return SDValue(N, 0);
2023}
2024
2027 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2028 ID.AddPointer(MBB);
2029 void *IP = nullptr;
2030 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2031 return SDValue(E, 0);
2032
2033 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2034 CSEMap.InsertNode(N, IP);
2035 InsertNode(N);
2036 return SDValue(N, 0);
2037}
2038
2040 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2041 ValueTypeNodes.size())
2042 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2043
2044 SDNode *&N = VT.isExtended() ?
2045 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2046
2047 if (N) return SDValue(N, 0);
2048 N = newSDNode<VTSDNode>(VT);
2049 InsertNode(N);
2050 return SDValue(N, 0);
2051}
2052
2054 SDNode *&N = ExternalSymbols[Sym];
2055 if (N) return SDValue(N, 0);
2056 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2057 InsertNode(N);
2058 return SDValue(N, 0);
2059}
2060
2062 SDNode *&N = MCSymbols[Sym];
2063 if (N)
2064 return SDValue(N, 0);
2065 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2066 InsertNode(N);
2067 return SDValue(N, 0);
2068}
2069
2071 unsigned TargetFlags) {
2072 SDNode *&N =
2073 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2074 if (N) return SDValue(N, 0);
2075 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2076 InsertNode(N);
2077 return SDValue(N, 0);
2078}
2079
2081 if ((unsigned)Cond >= CondCodeNodes.size())
2082 CondCodeNodes.resize(Cond+1);
2083
2084 if (!CondCodeNodes[Cond]) {
2085 auto *N = newSDNode<CondCodeSDNode>(Cond);
2086 CondCodeNodes[Cond] = N;
2087 InsertNode(N);
2088 }
2089
2090 return SDValue(CondCodeNodes[Cond], 0);
2091}
2092
2094 bool ConstantFold) {
2095 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2096 "APInt size does not match type size!");
2097
2098 if (MulImm == 0)
2099 return getConstant(0, DL, VT);
2100
2101 if (ConstantFold) {
2102 const MachineFunction &MF = getMachineFunction();
2103 const Function &F = MF.getFunction();
2104 ConstantRange CR = getVScaleRange(&F, 64);
2105 if (const APInt *C = CR.getSingleElement())
2106 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2107 }
2108
2109 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2110}
2111
2113 bool ConstantFold) {
2114 if (EC.isScalable())
2115 return getVScale(DL, VT,
2116 APInt(VT.getSizeInBits(), EC.getKnownMinValue()));
2117
2118 return getConstant(EC.getKnownMinValue(), DL, VT);
2119}
2120
2122 APInt One(ResVT.getScalarSizeInBits(), 1);
2123 return getStepVector(DL, ResVT, One);
2124}
2125
2127 const APInt &StepVal) {
2128 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2129 if (ResVT.isScalableVector())
2130 return getNode(
2131 ISD::STEP_VECTOR, DL, ResVT,
2132 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2133
2134 SmallVector<SDValue, 16> OpsStepConstants;
2135 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2136 OpsStepConstants.push_back(
2137 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2138 return getBuildVector(ResVT, DL, OpsStepConstants);
2139}
2140
2141/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2142/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2144 std::swap(N1, N2);
2146}
2147
2149 SDValue N2, ArrayRef<int> Mask) {
2150 assert(VT.getVectorNumElements() == Mask.size() &&
2151 "Must have the same number of vector elements as mask elements!");
2152 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2153 "Invalid VECTOR_SHUFFLE");
2154
2155 // Canonicalize shuffle undef, undef -> undef
2156 if (N1.isUndef() && N2.isUndef())
2157 return getUNDEF(VT);
2158
2159 // Validate that all indices in Mask are within the range of the elements
2160 // input to the shuffle.
2161 int NElts = Mask.size();
2162 assert(llvm::all_of(Mask,
2163 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2164 "Index out of range");
2165
2166 // Copy the mask so we can do any needed cleanup.
2167 SmallVector<int, 8> MaskVec(Mask);
2168
2169 // Canonicalize shuffle v, v -> v, undef
2170 if (N1 == N2) {
2171 N2 = getUNDEF(VT);
2172 for (int i = 0; i != NElts; ++i)
2173 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2174 }
2175
2176 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2177 if (N1.isUndef())
2178 commuteShuffle(N1, N2, MaskVec);
2179
2180 if (TLI->hasVectorBlend()) {
2181 // If shuffling a splat, try to blend the splat instead. We do this here so
2182 // that even when this arises during lowering we don't have to re-handle it.
2183 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2184 BitVector UndefElements;
2185 SDValue Splat = BV->getSplatValue(&UndefElements);
2186 if (!Splat)
2187 return;
2188
2189 for (int i = 0; i < NElts; ++i) {
2190 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2191 continue;
2192
2193 // If this input comes from undef, mark it as such.
2194 if (UndefElements[MaskVec[i] - Offset]) {
2195 MaskVec[i] = -1;
2196 continue;
2197 }
2198
2199 // If we can blend a non-undef lane, use that instead.
2200 if (!UndefElements[i])
2201 MaskVec[i] = i + Offset;
2202 }
2203 };
2204 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2205 BlendSplat(N1BV, 0);
2206 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2207 BlendSplat(N2BV, NElts);
2208 }
2209
2210 // Canonicalize all index into lhs, -> shuffle lhs, undef
2211 // Canonicalize all index into rhs, -> shuffle rhs, undef
2212 bool AllLHS = true, AllRHS = true;
2213 bool N2Undef = N2.isUndef();
2214 for (int i = 0; i != NElts; ++i) {
2215 if (MaskVec[i] >= NElts) {
2216 if (N2Undef)
2217 MaskVec[i] = -1;
2218 else
2219 AllLHS = false;
2220 } else if (MaskVec[i] >= 0) {
2221 AllRHS = false;
2222 }
2223 }
2224 if (AllLHS && AllRHS)
2225 return getUNDEF(VT);
2226 if (AllLHS && !N2Undef)
2227 N2 = getUNDEF(VT);
2228 if (AllRHS) {
2229 N1 = getUNDEF(VT);
2230 commuteShuffle(N1, N2, MaskVec);
2231 }
2232 // Reset our undef status after accounting for the mask.
2233 N2Undef = N2.isUndef();
2234 // Re-check whether both sides ended up undef.
2235 if (N1.isUndef() && N2Undef)
2236 return getUNDEF(VT);
2237
2238 // If Identity shuffle return that node.
2239 bool Identity = true, AllSame = true;
2240 for (int i = 0; i != NElts; ++i) {
2241 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2242 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2243 }
2244 if (Identity && NElts)
2245 return N1;
2246
2247 // Shuffling a constant splat doesn't change the result.
2248 if (N2Undef) {
2249 SDValue V = N1;
2250
2251 // Look through any bitcasts. We check that these don't change the number
2252 // (and size) of elements and just changes their types.
2253 while (V.getOpcode() == ISD::BITCAST)
2254 V = V->getOperand(0);
2255
2256 // A splat should always show up as a build vector node.
2257 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2258 BitVector UndefElements;
2259 SDValue Splat = BV->getSplatValue(&UndefElements);
2260 // If this is a splat of an undef, shuffling it is also undef.
2261 if (Splat && Splat.isUndef())
2262 return getUNDEF(VT);
2263
2264 bool SameNumElts =
2265 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2266
2267 // We only have a splat which can skip shuffles if there is a splatted
2268 // value and no undef lanes rearranged by the shuffle.
2269 if (Splat && UndefElements.none()) {
2270 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2271 // number of elements match or the value splatted is a zero constant.
2272 if (SameNumElts || isNullConstant(Splat))
2273 return N1;
2274 }
2275
2276 // If the shuffle itself creates a splat, build the vector directly.
2277 if (AllSame && SameNumElts) {
2278 EVT BuildVT = BV->getValueType(0);
2279 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2280 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2281
2282 // We may have jumped through bitcasts, so the type of the
2283 // BUILD_VECTOR may not match the type of the shuffle.
2284 if (BuildVT != VT)
2285 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2286 return NewBV;
2287 }
2288 }
2289 }
2290
2291 SDVTList VTs = getVTList(VT);
2293 SDValue Ops[2] = { N1, N2 };
2295 for (int i = 0; i != NElts; ++i)
2296 ID.AddInteger(MaskVec[i]);
2297
2298 void* IP = nullptr;
2299 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2300 return SDValue(E, 0);
2301
2302 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2303 // SDNode doesn't have access to it. This memory will be "leaked" when
2304 // the node is deallocated, but recovered when the NodeAllocator is released.
2305 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2306 llvm::copy(MaskVec, MaskAlloc);
2307
2308 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2309 dl.getDebugLoc(), MaskAlloc);
2310 createOperands(N, Ops);
2311
2312 CSEMap.InsertNode(N, IP);
2313 InsertNode(N);
2314 SDValue V = SDValue(N, 0);
2315 NewSDValueDbgMsg(V, "Creating new node: ", this);
2316 return V;
2317}
2318
2320 EVT VT = SV.getValueType(0);
2321 SmallVector<int, 8> MaskVec(SV.getMask());
2323
2324 SDValue Op0 = SV.getOperand(0);
2325 SDValue Op1 = SV.getOperand(1);
2326 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2327}
2328
2330 SDVTList VTs = getVTList(VT);
2332 AddNodeIDNode(ID, ISD::Register, VTs, {});
2333 ID.AddInteger(Reg.id());
2334 void *IP = nullptr;
2335 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2336 return SDValue(E, 0);
2337
2338 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2339 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2340 CSEMap.InsertNode(N, IP);
2341 InsertNode(N);
2342 return SDValue(N, 0);
2343}
2344
2347 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2348 ID.AddPointer(RegMask);
2349 void *IP = nullptr;
2350 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2351 return SDValue(E, 0);
2352
2353 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2354 CSEMap.InsertNode(N, IP);
2355 InsertNode(N);
2356 return SDValue(N, 0);
2357}
2358
2360 MCSymbol *Label) {
2361 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2362}
2363
2364SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2365 SDValue Root, MCSymbol *Label) {
2367 SDValue Ops[] = { Root };
2368 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2369 ID.AddPointer(Label);
2370 void *IP = nullptr;
2371 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2372 return SDValue(E, 0);
2373
2374 auto *N =
2375 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2376 createOperands(N, Ops);
2377
2378 CSEMap.InsertNode(N, IP);
2379 InsertNode(N);
2380 return SDValue(N, 0);
2381}
2382
2384 int64_t Offset, bool isTarget,
2385 unsigned TargetFlags) {
2386 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2387 SDVTList VTs = getVTList(VT);
2388
2390 AddNodeIDNode(ID, Opc, VTs, {});
2391 ID.AddPointer(BA);
2392 ID.AddInteger(Offset);
2393 ID.AddInteger(TargetFlags);
2394 void *IP = nullptr;
2395 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2396 return SDValue(E, 0);
2397
2398 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2399 CSEMap.InsertNode(N, IP);
2400 InsertNode(N);
2401 return SDValue(N, 0);
2402}
2403
2406 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2407 ID.AddPointer(V);
2408
2409 void *IP = nullptr;
2410 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2411 return SDValue(E, 0);
2412
2413 auto *N = newSDNode<SrcValueSDNode>(V);
2414 CSEMap.InsertNode(N, IP);
2415 InsertNode(N);
2416 return SDValue(N, 0);
2417}
2418
2421 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2422 ID.AddPointer(MD);
2423
2424 void *IP = nullptr;
2425 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2426 return SDValue(E, 0);
2427
2428 auto *N = newSDNode<MDNodeSDNode>(MD);
2429 CSEMap.InsertNode(N, IP);
2430 InsertNode(N);
2431 return SDValue(N, 0);
2432}
2433
2435 if (VT == V.getValueType())
2436 return V;
2437
2438 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2439}
2440
2442 unsigned SrcAS, unsigned DestAS) {
2443 SDVTList VTs = getVTList(VT);
2444 SDValue Ops[] = {Ptr};
2447 ID.AddInteger(SrcAS);
2448 ID.AddInteger(DestAS);
2449
2450 void *IP = nullptr;
2451 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2452 return SDValue(E, 0);
2453
2454 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2455 VTs, SrcAS, DestAS);
2456 createOperands(N, Ops);
2457
2458 CSEMap.InsertNode(N, IP);
2459 InsertNode(N);
2460 return SDValue(N, 0);
2461}
2462
2464 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2465}
2466
2467/// getShiftAmountOperand - Return the specified value casted to
2468/// the target's desired shift amount type.
2470 EVT OpTy = Op.getValueType();
2471 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2472 if (OpTy == ShTy || OpTy.isVector()) return Op;
2473
2474 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2475}
2476
2478 SDValue Op2) {
2479 EVT FullTy = Op2.getValueType();
2480
2481 unsigned Stride = ReducedTy.getVectorMinNumElements();
2482 unsigned ScaleFactor = FullTy.getVectorMinNumElements() / Stride;
2483
2484 // Collect all of the subvectors
2485 std::deque<SDValue> Subvectors = {Op1};
2486 for (unsigned I = 0; I < ScaleFactor; I++) {
2487 auto SourceIndex = getVectorIdxConstant(I * Stride, DL);
2488 Subvectors.push_back(
2489 getNode(ISD::EXTRACT_SUBVECTOR, DL, ReducedTy, {Op2, SourceIndex}));
2490 }
2491
2492 // Flatten the subvector tree
2493 while (Subvectors.size() > 1) {
2494 Subvectors.push_back(
2495 getNode(ISD::ADD, DL, ReducedTy, {Subvectors[0], Subvectors[1]}));
2496 Subvectors.pop_front();
2497 Subvectors.pop_front();
2498 }
2499
2500 assert(Subvectors.size() == 1 &&
2501 "There should only be one subvector after tree flattening");
2502
2503 return Subvectors[0];
2504}
2505
2506/// Given a store node \p StoreNode, return true if it is safe to fold that node
2507/// into \p FPNode, which expands to a library call with output pointers.
2509 SDNode *FPNode) {
2511 SmallVector<const SDNode *, 8> DeferredNodes;
2513
2514 // Skip FPNode use by StoreNode (that's the use we want to fold into FPNode).
2515 for (SDValue Op : StoreNode->ops())
2516 if (Op.getNode() != FPNode)
2517 Worklist.push_back(Op.getNode());
2518
2520 while (!Worklist.empty()) {
2521 const SDNode *Node = Worklist.pop_back_val();
2522 auto [_, Inserted] = Visited.insert(Node);
2523 if (!Inserted)
2524 continue;
2525
2526 if (MaxSteps > 0 && Visited.size() >= MaxSteps)
2527 return false;
2528
2529 // Reached the FPNode (would result in a cycle).
2530 // OR Reached CALLSEQ_START (would result in nested call sequences).
2531 if (Node == FPNode || Node->getOpcode() == ISD::CALLSEQ_START)
2532 return false;
2533
2534 if (Node->getOpcode() == ISD::CALLSEQ_END) {
2535 // Defer looking into call sequences (so we can check we're outside one).
2536 // We still need to look through these for the predecessor check.
2537 DeferredNodes.push_back(Node);
2538 continue;
2539 }
2540
2541 for (SDValue Op : Node->ops())
2542 Worklist.push_back(Op.getNode());
2543 }
2544
2545 // True if we're outside a call sequence and don't have the FPNode as a
2546 // predecessor. No cycles or nested call sequences possible.
2547 return !SDNode::hasPredecessorHelper(FPNode, Visited, DeferredNodes,
2548 MaxSteps);
2549}
2550
2553 std::optional<unsigned> CallRetResNo) {
2554 LLVMContext &Ctx = *getContext();
2555 EVT VT = Node->getValueType(0);
2556 unsigned NumResults = Node->getNumValues();
2557
2558 const char *LCName = TLI->getLibcallName(LC);
2559 if (!LC || !LCName)
2560 return false;
2561
2562 auto getVecDesc = [&]() -> VecDesc const * {
2563 for (bool Masked : {false, true}) {
2564 if (VecDesc const *VD = getLibInfo().getVectorMappingInfo(
2565 LCName, VT.getVectorElementCount(), Masked)) {
2566 return VD;
2567 }
2568 }
2569 return nullptr;
2570 };
2571
2572 // For vector types, we must find a vector mapping for the libcall.
2573 VecDesc const *VD = nullptr;
2574 if (VT.isVector() && !(VD = getVecDesc()))
2575 return false;
2576
2577 // Find users of the node that store the results (and share input chains). The
2578 // destination pointers can be used instead of creating stack allocations.
2579 SDValue StoresInChain;
2580 SmallVector<StoreSDNode *, 2> ResultStores(NumResults);
2581 for (SDNode *User : Node->users()) {
2583 continue;
2584 auto *ST = cast<StoreSDNode>(User);
2585 SDValue StoreValue = ST->getValue();
2586 unsigned ResNo = StoreValue.getResNo();
2587 // Ensure the store corresponds to an output pointer.
2588 if (CallRetResNo == ResNo)
2589 continue;
2590 // Ensure the store to the default address space and not atomic or volatile.
2591 if (!ST->isSimple() || ST->getAddressSpace() != 0)
2592 continue;
2593 // Ensure all store chains are the same (so they don't alias).
2594 if (StoresInChain && ST->getChain() != StoresInChain)
2595 continue;
2596 // Ensure the store is properly aligned.
2597 Type *StoreType = StoreValue.getValueType().getTypeForEVT(Ctx);
2598 if (ST->getAlign() <
2599 getDataLayout().getABITypeAlign(StoreType->getScalarType()))
2600 continue;
2601 // Avoid:
2602 // 1. Creating cyclic dependencies.
2603 // 2. Expanding the node to a call within a call sequence.
2605 continue;
2606 ResultStores[ResNo] = ST;
2607 StoresInChain = ST->getChain();
2608 }
2609
2611 auto AddArgListEntry = [&](SDValue Node, Type *Ty) {
2613 Entry.Ty = Ty;
2614 Entry.Node = Node;
2615 Args.push_back(Entry);
2616 };
2617
2618 // Pass the arguments.
2619 for (const SDValue &Op : Node->op_values()) {
2620 EVT ArgVT = Op.getValueType();
2621 Type *ArgTy = ArgVT.getTypeForEVT(Ctx);
2622 AddArgListEntry(Op, ArgTy);
2623 }
2624
2625 // Pass the output pointers.
2626 SmallVector<SDValue, 2> ResultPtrs(NumResults);
2628 for (auto [ResNo, ST] : llvm::enumerate(ResultStores)) {
2629 if (ResNo == CallRetResNo)
2630 continue;
2631 EVT ResVT = Node->getValueType(ResNo);
2632 SDValue ResultPtr = ST ? ST->getBasePtr() : CreateStackTemporary(ResVT);
2633 ResultPtrs[ResNo] = ResultPtr;
2634 AddArgListEntry(ResultPtr, PointerTy);
2635 }
2636
2637 SDLoc DL(Node);
2638
2639 // Pass the vector mask (if required).
2640 if (VD && VD->isMasked()) {
2641 EVT MaskVT = TLI->getSetCCResultType(getDataLayout(), Ctx, VT);
2642 SDValue Mask = getBoolConstant(true, DL, MaskVT, VT);
2643 AddArgListEntry(Mask, MaskVT.getTypeForEVT(Ctx));
2644 }
2645
2646 Type *RetType = CallRetResNo.has_value()
2647 ? Node->getValueType(*CallRetResNo).getTypeForEVT(Ctx)
2648 : Type::getVoidTy(Ctx);
2649 SDValue InChain = StoresInChain ? StoresInChain : getEntryNode();
2650 SDValue Callee = getExternalSymbol(VD ? VD->getVectorFnName().data() : LCName,
2651 TLI->getPointerTy(getDataLayout()));
2653 CLI.setDebugLoc(DL).setChain(InChain).setLibCallee(
2654 TLI->getLibcallCallingConv(LC), RetType, Callee, std::move(Args));
2655
2656 auto [Call, CallChain] = TLI->LowerCallTo(CLI);
2657
2658 for (auto [ResNo, ResultPtr] : llvm::enumerate(ResultPtrs)) {
2659 if (ResNo == CallRetResNo) {
2660 Results.push_back(Call);
2661 continue;
2662 }
2663 MachinePointerInfo PtrInfo;
2664 if (StoreSDNode *ST = ResultStores[ResNo]) {
2665 // Replace store with the library call.
2666 ReplaceAllUsesOfValueWith(SDValue(ST, 0), CallChain);
2667 PtrInfo = ST->getPointerInfo();
2668 } else {
2670 getMachineFunction(), cast<FrameIndexSDNode>(ResultPtr)->getIndex());
2671 }
2672 SDValue LoadResult =
2673 getLoad(Node->getValueType(ResNo), DL, CallChain, ResultPtr, PtrInfo);
2674 Results.push_back(LoadResult);
2675 }
2676
2677 if (CallRetResNo && !Node->hasAnyUseOfValue(*CallRetResNo)) {
2678 // FIXME: Find a way to avoid updating the root. This is needed for x86,
2679 // which uses a floating-point stack. If (for example) the node to be
2680 // expanded has two results one floating-point which is returned by the
2681 // call, and one integer result, returned via an output pointer. If only the
2682 // integer result is used then the `CopyFromReg` for the FP result may be
2683 // optimized out. This prevents an FP stack pop from being emitted for it.
2684 // Setting the root like this ensures there will be a use of the
2685 // `CopyFromReg` chain, and ensures the FP pop will be emitted.
2686 SDValue NewRoot =
2687 getNode(ISD::TokenFactor, DL, MVT::Other, getRoot(), CallChain);
2688 setRoot(NewRoot);
2689 // Ensure the new root is reachable from the results.
2690 Results[0] = getMergeValues({Results[0], NewRoot}, DL);
2691 }
2692
2693 return true;
2694}
2695
2697 SDLoc dl(Node);
2699 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2700 EVT VT = Node->getValueType(0);
2701 SDValue Tmp1 = Node->getOperand(0);
2702 SDValue Tmp2 = Node->getOperand(1);
2703 const MaybeAlign MA(Node->getConstantOperandVal(3));
2704
2705 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2706 Tmp2, MachinePointerInfo(V));
2707 SDValue VAList = VAListLoad;
2708
2709 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2710 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2711 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2712
2713 VAList = getNode(
2714 ISD::AND, dl, VAList.getValueType(), VAList,
2715 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2716 }
2717
2718 // Increment the pointer, VAList, to the next vaarg
2719 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2720 getConstant(getDataLayout().getTypeAllocSize(
2721 VT.getTypeForEVT(*getContext())),
2722 dl, VAList.getValueType()));
2723 // Store the incremented VAList to the legalized pointer
2724 Tmp1 =
2725 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2726 // Load the actual argument out of the pointer VAList
2727 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2728}
2729
2731 SDLoc dl(Node);
2733 // This defaults to loading a pointer from the input and storing it to the
2734 // output, returning the chain.
2735 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2736 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2737 SDValue Tmp1 =
2738 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2739 Node->getOperand(2), MachinePointerInfo(VS));
2740 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2741 MachinePointerInfo(VD));
2742}
2743
2745 const DataLayout &DL = getDataLayout();
2746 Type *Ty = VT.getTypeForEVT(*getContext());
2747 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2748
2749 if (TLI->isTypeLegal(VT) || !VT.isVector())
2750 return RedAlign;
2751
2753 const Align StackAlign = TFI->getStackAlign();
2754
2755 // See if we can choose a smaller ABI alignment in cases where it's an
2756 // illegal vector type that will get broken down.
2757 if (RedAlign > StackAlign) {
2758 EVT IntermediateVT;
2759 MVT RegisterVT;
2760 unsigned NumIntermediates;
2761 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2762 NumIntermediates, RegisterVT);
2763 Ty = IntermediateVT.getTypeForEVT(*getContext());
2764 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2765 if (RedAlign2 < RedAlign)
2766 RedAlign = RedAlign2;
2767
2768 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2769 // If the stack is not realignable, the alignment should be limited to the
2770 // StackAlignment
2771 RedAlign = std::min(RedAlign, StackAlign);
2772 }
2773
2774 return RedAlign;
2775}
2776
2778 MachineFrameInfo &MFI = MF->getFrameInfo();
2780 int StackID = 0;
2781 if (Bytes.isScalable())
2782 StackID = TFI->getStackIDForScalableVectors();
2783 // The stack id gives an indication of whether the object is scalable or
2784 // not, so it's safe to pass in the minimum size here.
2785 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2786 false, nullptr, StackID);
2787 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2788}
2789
2791 Type *Ty = VT.getTypeForEVT(*getContext());
2792 Align StackAlign =
2793 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2794 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2795}
2796
2798 TypeSize VT1Size = VT1.getStoreSize();
2799 TypeSize VT2Size = VT2.getStoreSize();
2800 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2801 "Don't know how to choose the maximum size when creating a stack "
2802 "temporary");
2803 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2804 ? VT1Size
2805 : VT2Size;
2806
2807 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2808 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2809 const DataLayout &DL = getDataLayout();
2810 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2811 return CreateStackTemporary(Bytes, Align);
2812}
2813
2815 ISD::CondCode Cond, const SDLoc &dl) {
2816 EVT OpVT = N1.getValueType();
2817
2818 auto GetUndefBooleanConstant = [&]() {
2819 if (VT.getScalarType() == MVT::i1 ||
2820 TLI->getBooleanContents(OpVT) ==
2822 return getUNDEF(VT);
2823 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2824 // so we cannot use getUNDEF(). Return zero instead.
2825 return getConstant(0, dl, VT);
2826 };
2827
2828 // These setcc operations always fold.
2829 switch (Cond) {
2830 default: break;
2831 case ISD::SETFALSE:
2832 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2833 case ISD::SETTRUE:
2834 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2835
2836 case ISD::SETOEQ:
2837 case ISD::SETOGT:
2838 case ISD::SETOGE:
2839 case ISD::SETOLT:
2840 case ISD::SETOLE:
2841 case ISD::SETONE:
2842 case ISD::SETO:
2843 case ISD::SETUO:
2844 case ISD::SETUEQ:
2845 case ISD::SETUNE:
2846 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2847 break;
2848 }
2849
2850 if (OpVT.isInteger()) {
2851 // For EQ and NE, we can always pick a value for the undef to make the
2852 // predicate pass or fail, so we can return undef.
2853 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2854 // icmp eq/ne X, undef -> undef.
2855 if ((N1.isUndef() || N2.isUndef()) &&
2856 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2857 return GetUndefBooleanConstant();
2858
2859 // If both operands are undef, we can return undef for int comparison.
2860 // icmp undef, undef -> undef.
2861 if (N1.isUndef() && N2.isUndef())
2862 return GetUndefBooleanConstant();
2863
2864 // icmp X, X -> true/false
2865 // icmp X, undef -> true/false because undef could be X.
2866 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2867 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2868 }
2869
2870 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2)) {
2871 const APInt &C2 = N2C->getAPIntValue();
2872 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
2873 const APInt &C1 = N1C->getAPIntValue();
2874
2876 dl, VT, OpVT);
2877 }
2878 }
2879
2880 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2881 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2882
2883 if (N1CFP && N2CFP) {
2884 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2885 switch (Cond) {
2886 default: break;
2887 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2888 return GetUndefBooleanConstant();
2889 [[fallthrough]];
2890 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2891 OpVT);
2892 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2893 return GetUndefBooleanConstant();
2894 [[fallthrough]];
2896 R==APFloat::cmpLessThan, dl, VT,
2897 OpVT);
2898 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2899 return GetUndefBooleanConstant();
2900 [[fallthrough]];
2901 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2902 OpVT);
2903 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2904 return GetUndefBooleanConstant();
2905 [[fallthrough]];
2907 VT, OpVT);
2908 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2909 return GetUndefBooleanConstant();
2910 [[fallthrough]];
2912 R==APFloat::cmpEqual, dl, VT,
2913 OpVT);
2914 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2915 return GetUndefBooleanConstant();
2916 [[fallthrough]];
2918 R==APFloat::cmpEqual, dl, VT, OpVT);
2919 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2920 OpVT);
2921 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2922 OpVT);
2924 R==APFloat::cmpEqual, dl, VT,
2925 OpVT);
2926 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2927 OpVT);
2929 R==APFloat::cmpLessThan, dl, VT,
2930 OpVT);
2932 R==APFloat::cmpUnordered, dl, VT,
2933 OpVT);
2935 VT, OpVT);
2936 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2937 OpVT);
2938 }
2939 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2940 // Ensure that the constant occurs on the RHS.
2942 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2943 return SDValue();
2944 return getSetCC(dl, VT, N2, N1, SwappedCond);
2945 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2946 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2947 // If an operand is known to be a nan (or undef that could be a nan), we can
2948 // fold it.
2949 // Choosing NaN for the undef will always make unordered comparison succeed
2950 // and ordered comparison fails.
2951 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2952 switch (ISD::getUnorderedFlavor(Cond)) {
2953 default:
2954 llvm_unreachable("Unknown flavor!");
2955 case 0: // Known false.
2956 return getBoolConstant(false, dl, VT, OpVT);
2957 case 1: // Known true.
2958 return getBoolConstant(true, dl, VT, OpVT);
2959 case 2: // Undefined.
2960 return GetUndefBooleanConstant();
2961 }
2962 }
2963
2964 // Could not fold it.
2965 return SDValue();
2966}
2967
2968/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2969/// use this predicate to simplify operations downstream.
2971 unsigned BitWidth = Op.getScalarValueSizeInBits();
2973}
2974
2975/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2976/// this predicate to simplify operations downstream. Mask is known to be zero
2977/// for bits that V cannot have.
2979 unsigned Depth) const {
2980 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2981}
2982
2983/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2984/// DemandedElts. We use this predicate to simplify operations downstream.
2985/// Mask is known to be zero for bits that V cannot have.
2987 const APInt &DemandedElts,
2988 unsigned Depth) const {
2989 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2990}
2991
2992/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2993/// DemandedElts. We use this predicate to simplify operations downstream.
2995 unsigned Depth /* = 0 */) const {
2996 return computeKnownBits(V, DemandedElts, Depth).isZero();
2997}
2998
2999/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
3001 unsigned Depth) const {
3002 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
3003}
3004
3006 const APInt &DemandedElts,
3007 unsigned Depth) const {
3008 EVT VT = Op.getValueType();
3009 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
3010
3011 unsigned NumElts = VT.getVectorNumElements();
3012 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
3013
3014 APInt KnownZeroElements = APInt::getZero(NumElts);
3015 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
3016 if (!DemandedElts[EltIdx])
3017 continue; // Don't query elements that are not demanded.
3018 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
3019 if (MaskedVectorIsZero(Op, Mask, Depth))
3020 KnownZeroElements.setBit(EltIdx);
3021 }
3022 return KnownZeroElements;
3023}
3024
3025/// isSplatValue - Return true if the vector V has the same value
3026/// across all DemandedElts. For scalable vectors, we don't know the
3027/// number of lanes at compile time. Instead, we use a 1 bit APInt
3028/// to represent a conservative value for all lanes; that is, that
3029/// one bit value is implicitly splatted across all lanes.
3030bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
3031 APInt &UndefElts, unsigned Depth) const {
3032 unsigned Opcode = V.getOpcode();
3033 EVT VT = V.getValueType();
3034 assert(VT.isVector() && "Vector type expected");
3035 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
3036 "scalable demanded bits are ignored");
3037
3038 if (!DemandedElts)
3039 return false; // No demanded elts, better to assume we don't know anything.
3040
3041 if (Depth >= MaxRecursionDepth)
3042 return false; // Limit search depth.
3043
3044 // Deal with some common cases here that work for both fixed and scalable
3045 // vector types.
3046 switch (Opcode) {
3047 case ISD::SPLAT_VECTOR:
3048 UndefElts = V.getOperand(0).isUndef()
3049 ? APInt::getAllOnes(DemandedElts.getBitWidth())
3050 : APInt(DemandedElts.getBitWidth(), 0);
3051 return true;
3052 case ISD::ADD:
3053 case ISD::SUB:
3054 case ISD::AND:
3055 case ISD::XOR:
3056 case ISD::OR: {
3057 APInt UndefLHS, UndefRHS;
3058 SDValue LHS = V.getOperand(0);
3059 SDValue RHS = V.getOperand(1);
3060 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
3061 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1)) {
3062 UndefElts = UndefLHS | UndefRHS;
3063 return true;
3064 }
3065 return false;
3066 }
3067 case ISD::ABS:
3068 case ISD::TRUNCATE:
3069 case ISD::SIGN_EXTEND:
3070 case ISD::ZERO_EXTEND:
3071 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
3072 default:
3073 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
3074 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
3075 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
3076 Depth);
3077 break;
3078}
3079
3080 // We don't support other cases than those above for scalable vectors at
3081 // the moment.
3082 if (VT.isScalableVector())
3083 return false;
3084
3085 unsigned NumElts = VT.getVectorNumElements();
3086 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
3087 UndefElts = APInt::getZero(NumElts);
3088
3089 switch (Opcode) {
3090 case ISD::BUILD_VECTOR: {
3091 SDValue Scl;
3092 for (unsigned i = 0; i != NumElts; ++i) {
3093 SDValue Op = V.getOperand(i);
3094 if (Op.isUndef()) {
3095 UndefElts.setBit(i);
3096 continue;
3097 }
3098 if (!DemandedElts[i])
3099 continue;
3100 if (Scl && Scl != Op)
3101 return false;
3102 Scl = Op;
3103 }
3104 return true;
3105 }
3106 case ISD::VECTOR_SHUFFLE: {
3107 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
3108 APInt DemandedLHS = APInt::getZero(NumElts);
3109 APInt DemandedRHS = APInt::getZero(NumElts);
3110 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
3111 for (int i = 0; i != (int)NumElts; ++i) {
3112 int M = Mask[i];
3113 if (M < 0) {
3114 UndefElts.setBit(i);
3115 continue;
3116 }
3117 if (!DemandedElts[i])
3118 continue;
3119 if (M < (int)NumElts)
3120 DemandedLHS.setBit(M);
3121 else
3122 DemandedRHS.setBit(M - NumElts);
3123 }
3124
3125 // If we aren't demanding either op, assume there's no splat.
3126 // If we are demanding both ops, assume there's no splat.
3127 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
3128 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
3129 return false;
3130
3131 // See if the demanded elts of the source op is a splat or we only demand
3132 // one element, which should always be a splat.
3133 // TODO: Handle source ops splats with undefs.
3134 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
3135 APInt SrcUndefs;
3136 return (SrcElts.popcount() == 1) ||
3137 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
3138 (SrcElts & SrcUndefs).isZero());
3139 };
3140 if (!DemandedLHS.isZero())
3141 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
3142 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
3143 }
3145 // Offset the demanded elts by the subvector index.
3146 SDValue Src = V.getOperand(0);
3147 // We don't support scalable vectors at the moment.
3148 if (Src.getValueType().isScalableVector())
3149 return false;
3150 uint64_t Idx = V.getConstantOperandVal(1);
3151 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3152 APInt UndefSrcElts;
3153 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3154 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3155 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3156 return true;
3157 }
3158 break;
3159 }
3163 // Widen the demanded elts by the src element count.
3164 SDValue Src = V.getOperand(0);
3165 // We don't support scalable vectors at the moment.
3166 if (Src.getValueType().isScalableVector())
3167 return false;
3168 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3169 APInt UndefSrcElts;
3170 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3171 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3172 UndefElts = UndefSrcElts.trunc(NumElts);
3173 return true;
3174 }
3175 break;
3176 }
3177 case ISD::BITCAST: {
3178 SDValue Src = V.getOperand(0);
3179 EVT SrcVT = Src.getValueType();
3180 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3181 unsigned BitWidth = VT.getScalarSizeInBits();
3182
3183 // Ignore bitcasts from unsupported types.
3184 // TODO: Add fp support?
3185 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3186 break;
3187
3188 // Bitcast 'small element' vector to 'large element' vector.
3189 if ((BitWidth % SrcBitWidth) == 0) {
3190 // See if each sub element is a splat.
3191 unsigned Scale = BitWidth / SrcBitWidth;
3192 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3193 APInt ScaledDemandedElts =
3194 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3195 for (unsigned I = 0; I != Scale; ++I) {
3196 APInt SubUndefElts;
3197 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3198 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3199 SubDemandedElts &= ScaledDemandedElts;
3200 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3201 return false;
3202 // TODO: Add support for merging sub undef elements.
3203 if (!SubUndefElts.isZero())
3204 return false;
3205 }
3206 return true;
3207 }
3208 break;
3209 }
3210 }
3211
3212 return false;
3213}
3214
3215/// Helper wrapper to main isSplatValue function.
3216bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3217 EVT VT = V.getValueType();
3218 assert(VT.isVector() && "Vector type expected");
3219
3220 APInt UndefElts;
3221 // Since the number of lanes in a scalable vector is unknown at compile time,
3222 // we track one bit which is implicitly broadcast to all lanes. This means
3223 // that all lanes in a scalable vector are considered demanded.
3224 APInt DemandedElts
3226 return isSplatValue(V, DemandedElts, UndefElts) &&
3227 (AllowUndefs || !UndefElts);
3228}
3229
3232
3233 EVT VT = V.getValueType();
3234 unsigned Opcode = V.getOpcode();
3235 switch (Opcode) {
3236 default: {
3237 APInt UndefElts;
3238 // Since the number of lanes in a scalable vector is unknown at compile time,
3239 // we track one bit which is implicitly broadcast to all lanes. This means
3240 // that all lanes in a scalable vector are considered demanded.
3241 APInt DemandedElts
3243
3244 if (isSplatValue(V, DemandedElts, UndefElts)) {
3245 if (VT.isScalableVector()) {
3246 // DemandedElts and UndefElts are ignored for scalable vectors, since
3247 // the only supported cases are SPLAT_VECTOR nodes.
3248 SplatIdx = 0;
3249 } else {
3250 // Handle case where all demanded elements are UNDEF.
3251 if (DemandedElts.isSubsetOf(UndefElts)) {
3252 SplatIdx = 0;
3253 return getUNDEF(VT);
3254 }
3255 SplatIdx = (UndefElts & DemandedElts).countr_one();
3256 }
3257 return V;
3258 }
3259 break;
3260 }
3261 case ISD::SPLAT_VECTOR:
3262 SplatIdx = 0;
3263 return V;
3264 case ISD::VECTOR_SHUFFLE: {
3265 assert(!VT.isScalableVector());
3266 // Check if this is a shuffle node doing a splat.
3267 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3268 // getTargetVShiftNode currently struggles without the splat source.
3269 auto *SVN = cast<ShuffleVectorSDNode>(V);
3270 if (!SVN->isSplat())
3271 break;
3272 int Idx = SVN->getSplatIndex();
3273 int NumElts = V.getValueType().getVectorNumElements();
3274 SplatIdx = Idx % NumElts;
3275 return V.getOperand(Idx / NumElts);
3276 }
3277 }
3278
3279 return SDValue();
3280}
3281
3283 int SplatIdx;
3284 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3285 EVT SVT = SrcVector.getValueType().getScalarType();
3286 EVT LegalSVT = SVT;
3287 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3288 if (!SVT.isInteger())
3289 return SDValue();
3290 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3291 if (LegalSVT.bitsLT(SVT))
3292 return SDValue();
3293 }
3294 return getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(V), LegalSVT, SrcVector,
3295 getVectorIdxConstant(SplatIdx, SDLoc(V)));
3296 }
3297 return SDValue();
3298}
3299
3300std::optional<ConstantRange>
3302 unsigned Depth) const {
3303 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3304 V.getOpcode() == ISD::SRA) &&
3305 "Unknown shift node");
3306 // Shifting more than the bitwidth is not valid.
3307 unsigned BitWidth = V.getScalarValueSizeInBits();
3308
3309 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3310 const APInt &ShAmt = Cst->getAPIntValue();
3311 if (ShAmt.uge(BitWidth))
3312 return std::nullopt;
3313 return ConstantRange(ShAmt);
3314 }
3315
3316 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3317 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3318 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3319 if (!DemandedElts[i])
3320 continue;
3321 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3322 if (!SA) {
3323 MinAmt = MaxAmt = nullptr;
3324 break;
3325 }
3326 const APInt &ShAmt = SA->getAPIntValue();
3327 if (ShAmt.uge(BitWidth))
3328 return std::nullopt;
3329 if (!MinAmt || MinAmt->ugt(ShAmt))
3330 MinAmt = &ShAmt;
3331 if (!MaxAmt || MaxAmt->ult(ShAmt))
3332 MaxAmt = &ShAmt;
3333 }
3334 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3335 "Failed to find matching min/max shift amounts");
3336 if (MinAmt && MaxAmt)
3337 return ConstantRange(*MinAmt, *MaxAmt + 1);
3338 }
3339
3340 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3341 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3342 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3343 if (KnownAmt.getMaxValue().ult(BitWidth))
3344 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3345
3346 return std::nullopt;
3347}
3348
3349std::optional<uint64_t>
3351 unsigned Depth) const {
3352 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3353 V.getOpcode() == ISD::SRA) &&
3354 "Unknown shift node");
3355 if (std::optional<ConstantRange> AmtRange =
3356 getValidShiftAmountRange(V, DemandedElts, Depth))
3357 if (const APInt *ShAmt = AmtRange->getSingleElement())
3358 return ShAmt->getZExtValue();
3359 return std::nullopt;
3360}
3361
3362std::optional<uint64_t>
3364 EVT VT = V.getValueType();
3365 APInt DemandedElts = VT.isFixedLengthVector()
3367 : APInt(1, 1);
3368 return getValidShiftAmount(V, DemandedElts, Depth);
3369}
3370
3371std::optional<uint64_t>
3373 unsigned Depth) const {
3374 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3375 V.getOpcode() == ISD::SRA) &&
3376 "Unknown shift node");
3377 if (std::optional<ConstantRange> AmtRange =
3378 getValidShiftAmountRange(V, DemandedElts, Depth))
3379 return AmtRange->getUnsignedMin().getZExtValue();
3380 return std::nullopt;
3381}
3382
3383std::optional<uint64_t>
3385 EVT VT = V.getValueType();
3386 APInt DemandedElts = VT.isFixedLengthVector()
3388 : APInt(1, 1);
3389 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3390}
3391
3392std::optional<uint64_t>
3394 unsigned Depth) const {
3395 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3396 V.getOpcode() == ISD::SRA) &&
3397 "Unknown shift node");
3398 if (std::optional<ConstantRange> AmtRange =
3399 getValidShiftAmountRange(V, DemandedElts, Depth))
3400 return AmtRange->getUnsignedMax().getZExtValue();
3401 return std::nullopt;
3402}
3403
3404std::optional<uint64_t>
3406 EVT VT = V.getValueType();
3407 APInt DemandedElts = VT.isFixedLengthVector()
3409 : APInt(1, 1);
3410 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3411}
3412
3413/// Determine which bits of Op are known to be either zero or one and return
3414/// them in Known. For vectors, the known bits are those that are shared by
3415/// every vector element.
3417 EVT VT = Op.getValueType();
3418
3419 // Since the number of lanes in a scalable vector is unknown at compile time,
3420 // we track one bit which is implicitly broadcast to all lanes. This means
3421 // that all lanes in a scalable vector are considered demanded.
3422 APInt DemandedElts = VT.isFixedLengthVector()
3424 : APInt(1, 1);
3425 return computeKnownBits(Op, DemandedElts, Depth);
3426}
3427
3428/// Determine which bits of Op are known to be either zero or one and return
3429/// them in Known. The DemandedElts argument allows us to only collect the known
3430/// bits that are shared by the requested vector elements.
3432 unsigned Depth) const {
3433 unsigned BitWidth = Op.getScalarValueSizeInBits();
3434
3435 KnownBits Known(BitWidth); // Don't know anything.
3436
3437 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
3438 // We know all of the bits for a constant!
3439 return KnownBits::makeConstant(C->getAPIntValue());
3440 }
3441 if (auto *C = dyn_cast<ConstantFPSDNode>(Op)) {
3442 // We know all of the bits for a constant fp!
3443 return KnownBits::makeConstant(C->getValueAPF().bitcastToAPInt());
3444 }
3445
3446 if (Depth >= MaxRecursionDepth)
3447 return Known; // Limit search depth.
3448
3449 KnownBits Known2;
3450 unsigned NumElts = DemandedElts.getBitWidth();
3451 assert((!Op.getValueType().isFixedLengthVector() ||
3452 NumElts == Op.getValueType().getVectorNumElements()) &&
3453 "Unexpected vector size");
3454
3455 if (!DemandedElts)
3456 return Known; // No demanded elts, better to assume we don't know anything.
3457
3458 unsigned Opcode = Op.getOpcode();
3459 switch (Opcode) {
3460 case ISD::MERGE_VALUES:
3461 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3462 Depth + 1);
3463 case ISD::SPLAT_VECTOR: {
3464 SDValue SrcOp = Op.getOperand(0);
3465 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3466 "Expected SPLAT_VECTOR implicit truncation");
3467 // Implicitly truncate the bits to match the official semantics of
3468 // SPLAT_VECTOR.
3469 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3470 break;
3471 }
3473 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3474 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3475 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3476 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3477 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3478 }
3479 break;
3480 }
3481 case ISD::STEP_VECTOR: {
3482 const APInt &Step = Op.getConstantOperandAPInt(0);
3483
3484 if (Step.isPowerOf2())
3485 Known.Zero.setLowBits(Step.logBase2());
3486
3488
3489 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3490 break;
3491 const APInt MinNumElts =
3492 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3493
3494 bool Overflow;
3495 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3497 .umul_ov(MinNumElts, Overflow);
3498 if (Overflow)
3499 break;
3500
3501 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3502 if (Overflow)
3503 break;
3504
3505 Known.Zero.setHighBits(MaxValue.countl_zero());
3506 break;
3507 }
3508 case ISD::BUILD_VECTOR:
3509 assert(!Op.getValueType().isScalableVector());
3510 // Collect the known bits that are shared by every demanded vector element.
3511 Known.Zero.setAllBits(); Known.One.setAllBits();
3512 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3513 if (!DemandedElts[i])
3514 continue;
3515
3516 SDValue SrcOp = Op.getOperand(i);
3517 Known2 = computeKnownBits(SrcOp, Depth + 1);
3518
3519 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3520 if (SrcOp.getValueSizeInBits() != BitWidth) {
3521 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3522 "Expected BUILD_VECTOR implicit truncation");
3523 Known2 = Known2.trunc(BitWidth);
3524 }
3525
3526 // Known bits are the values that are shared by every demanded element.
3527 Known = Known.intersectWith(Known2);
3528
3529 // If we don't know any bits, early out.
3530 if (Known.isUnknown())
3531 break;
3532 }
3533 break;
3534 case ISD::VECTOR_SHUFFLE: {
3535 assert(!Op.getValueType().isScalableVector());
3536 // Collect the known bits that are shared by every vector element referenced
3537 // by the shuffle.
3538 APInt DemandedLHS, DemandedRHS;
3539 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
3540 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3541 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3542 DemandedLHS, DemandedRHS))
3543 break;
3544
3545 // Known bits are the values that are shared by every demanded element.
3546 Known.Zero.setAllBits(); Known.One.setAllBits();
3547 if (!!DemandedLHS) {
3548 SDValue LHS = Op.getOperand(0);
3549 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3550 Known = Known.intersectWith(Known2);
3551 }
3552 // If we don't know any bits, early out.
3553 if (Known.isUnknown())
3554 break;
3555 if (!!DemandedRHS) {
3556 SDValue RHS = Op.getOperand(1);
3557 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3558 Known = Known.intersectWith(Known2);
3559 }
3560 break;
3561 }
3562 case ISD::VSCALE: {
3564 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3565 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3566 break;
3567 }
3568 case ISD::CONCAT_VECTORS: {
3569 if (Op.getValueType().isScalableVector())
3570 break;
3571 // Split DemandedElts and test each of the demanded subvectors.
3572 Known.Zero.setAllBits(); Known.One.setAllBits();
3573 EVT SubVectorVT = Op.getOperand(0).getValueType();
3574 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3575 unsigned NumSubVectors = Op.getNumOperands();
3576 for (unsigned i = 0; i != NumSubVectors; ++i) {
3577 APInt DemandedSub =
3578 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3579 if (!!DemandedSub) {
3580 SDValue Sub = Op.getOperand(i);
3581 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3582 Known = Known.intersectWith(Known2);
3583 }
3584 // If we don't know any bits, early out.
3585 if (Known.isUnknown())
3586 break;
3587 }
3588 break;
3589 }
3590 case ISD::INSERT_SUBVECTOR: {
3591 if (Op.getValueType().isScalableVector())
3592 break;
3593 // Demand any elements from the subvector and the remainder from the src its
3594 // inserted into.
3595 SDValue Src = Op.getOperand(0);
3596 SDValue Sub = Op.getOperand(1);
3597 uint64_t Idx = Op.getConstantOperandVal(2);
3598 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3599 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3600 APInt DemandedSrcElts = DemandedElts;
3601 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
3602
3603 Known.One.setAllBits();
3604 Known.Zero.setAllBits();
3605 if (!!DemandedSubElts) {
3606 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3607 if (Known.isUnknown())
3608 break; // early-out.
3609 }
3610 if (!!DemandedSrcElts) {
3611 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3612 Known = Known.intersectWith(Known2);
3613 }
3614 break;
3615 }
3617 // Offset the demanded elts by the subvector index.
3618 SDValue Src = Op.getOperand(0);
3619 // Bail until we can represent demanded elements for scalable vectors.
3620 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3621 break;
3622 uint64_t Idx = Op.getConstantOperandVal(1);
3623 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3624 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3625 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3626 break;
3627 }
3628 case ISD::SCALAR_TO_VECTOR: {
3629 if (Op.getValueType().isScalableVector())
3630 break;
3631 // We know about scalar_to_vector as much as we know about it source,
3632 // which becomes the first element of otherwise unknown vector.
3633 if (DemandedElts != 1)
3634 break;
3635
3636 SDValue N0 = Op.getOperand(0);
3637 Known = computeKnownBits(N0, Depth + 1);
3638 if (N0.getValueSizeInBits() != BitWidth)
3639 Known = Known.trunc(BitWidth);
3640
3641 break;
3642 }
3643 case ISD::BITCAST: {
3644 if (Op.getValueType().isScalableVector())
3645 break;
3646
3647 SDValue N0 = Op.getOperand(0);
3648 EVT SubVT = N0.getValueType();
3649 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3650
3651 // Ignore bitcasts from unsupported types.
3652 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3653 break;
3654
3655 // Fast handling of 'identity' bitcasts.
3656 if (BitWidth == SubBitWidth) {
3657 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3658 break;
3659 }
3660
3661 bool IsLE = getDataLayout().isLittleEndian();
3662
3663 // Bitcast 'small element' vector to 'large element' scalar/vector.
3664 if ((BitWidth % SubBitWidth) == 0) {
3665 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3666
3667 // Collect known bits for the (larger) output by collecting the known
3668 // bits from each set of sub elements and shift these into place.
3669 // We need to separately call computeKnownBits for each set of
3670 // sub elements as the knownbits for each is likely to be different.
3671 unsigned SubScale = BitWidth / SubBitWidth;
3672 APInt SubDemandedElts(NumElts * SubScale, 0);
3673 for (unsigned i = 0; i != NumElts; ++i)
3674 if (DemandedElts[i])
3675 SubDemandedElts.setBit(i * SubScale);
3676
3677 for (unsigned i = 0; i != SubScale; ++i) {
3678 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3679 Depth + 1);
3680 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3681 Known.insertBits(Known2, SubBitWidth * Shifts);
3682 }
3683 }
3684
3685 // Bitcast 'large element' scalar/vector to 'small element' vector.
3686 if ((SubBitWidth % BitWidth) == 0) {
3687 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3688
3689 // Collect known bits for the (smaller) output by collecting the known
3690 // bits from the overlapping larger input elements and extracting the
3691 // sub sections we actually care about.
3692 unsigned SubScale = SubBitWidth / BitWidth;
3693 APInt SubDemandedElts =
3694 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3695 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3696
3697 Known.Zero.setAllBits(); Known.One.setAllBits();
3698 for (unsigned i = 0; i != NumElts; ++i)
3699 if (DemandedElts[i]) {
3700 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3701 unsigned Offset = (Shifts % SubScale) * BitWidth;
3702 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3703 // If we don't know any bits, early out.
3704 if (Known.isUnknown())
3705 break;
3706 }
3707 }
3708 break;
3709 }
3710 case ISD::AND:
3711 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3712 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3713
3714 Known &= Known2;
3715 break;
3716 case ISD::OR:
3717 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3718 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3719
3720 Known |= Known2;
3721 break;
3722 case ISD::XOR:
3723 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3724 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3725
3726 Known ^= Known2;
3727 break;
3728 case ISD::MUL: {
3729 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3730 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3731 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3732 // TODO: SelfMultiply can be poison, but not undef.
3733 if (SelfMultiply)
3734 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3735 Op.getOperand(0), DemandedElts, false, Depth + 1);
3736 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3737
3738 // If the multiplication is known not to overflow, the product of a number
3739 // with itself is non-negative. Only do this if we didn't already computed
3740 // the opposite value for the sign bit.
3741 if (Op->getFlags().hasNoSignedWrap() &&
3742 Op.getOperand(0) == Op.getOperand(1) &&
3743 !Known.isNegative())
3744 Known.makeNonNegative();
3745 break;
3746 }
3747 case ISD::MULHU: {
3748 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3749 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3750 Known = KnownBits::mulhu(Known, Known2);
3751 break;
3752 }
3753 case ISD::MULHS: {
3754 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3755 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3756 Known = KnownBits::mulhs(Known, Known2);
3757 break;
3758 }
3759 case ISD::ABDU: {
3760 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3761 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3762 Known = KnownBits::abdu(Known, Known2);
3763 break;
3764 }
3765 case ISD::ABDS: {
3766 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3767 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3768 Known = KnownBits::abds(Known, Known2);
3769 unsigned SignBits1 =
3770 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3771 if (SignBits1 == 1)
3772 break;
3773 unsigned SignBits0 =
3774 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3775 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3776 break;
3777 }
3778 case ISD::UMUL_LOHI: {
3779 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3780 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3781 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3782 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3783 if (Op.getResNo() == 0)
3784 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3785 else
3786 Known = KnownBits::mulhu(Known, Known2);
3787 break;
3788 }
3789 case ISD::SMUL_LOHI: {
3790 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3791 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3792 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3793 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3794 if (Op.getResNo() == 0)
3795 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3796 else
3797 Known = KnownBits::mulhs(Known, Known2);
3798 break;
3799 }
3800 case ISD::AVGFLOORU: {
3801 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3802 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3803 Known = KnownBits::avgFloorU(Known, Known2);
3804 break;
3805 }
3806 case ISD::AVGCEILU: {
3807 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3808 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3809 Known = KnownBits::avgCeilU(Known, Known2);
3810 break;
3811 }
3812 case ISD::AVGFLOORS: {
3813 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3814 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3815 Known = KnownBits::avgFloorS(Known, Known2);
3816 break;
3817 }
3818 case ISD::AVGCEILS: {
3819 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3820 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3821 Known = KnownBits::avgCeilS(Known, Known2);
3822 break;
3823 }
3824 case ISD::SELECT:
3825 case ISD::VSELECT:
3826 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3827 // If we don't know any bits, early out.
3828 if (Known.isUnknown())
3829 break;
3830 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3831
3832 // Only known if known in both the LHS and RHS.
3833 Known = Known.intersectWith(Known2);
3834 break;
3835 case ISD::SELECT_CC:
3836 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3837 // If we don't know any bits, early out.
3838 if (Known.isUnknown())
3839 break;
3840 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3841
3842 // Only known if known in both the LHS and RHS.
3843 Known = Known.intersectWith(Known2);
3844 break;
3845 case ISD::SMULO:
3846 case ISD::UMULO:
3847 if (Op.getResNo() != 1)
3848 break;
3849 // The boolean result conforms to getBooleanContents.
3850 // If we know the result of a setcc has the top bits zero, use this info.
3851 // We know that we have an integer-based boolean since these operations
3852 // are only available for integer.
3853 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3855 BitWidth > 1)
3856 Known.Zero.setBitsFrom(1);
3857 break;
3858 case ISD::SETCC:
3859 case ISD::SETCCCARRY:
3860 case ISD::STRICT_FSETCC:
3861 case ISD::STRICT_FSETCCS: {
3862 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3863 // If we know the result of a setcc has the top bits zero, use this info.
3864 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3866 BitWidth > 1)
3867 Known.Zero.setBitsFrom(1);
3868 break;
3869 }
3870 case ISD::SHL: {
3871 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3872 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3873
3874 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3875 bool NSW = Op->getFlags().hasNoSignedWrap();
3876
3877 bool ShAmtNonZero = Known2.isNonZero();
3878
3879 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3880
3881 // Minimum shift low bits are known zero.
3882 if (std::optional<uint64_t> ShMinAmt =
3883 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3884 Known.Zero.setLowBits(*ShMinAmt);
3885 break;
3886 }
3887 case ISD::SRL:
3888 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3889 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3890 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3891 Op->getFlags().hasExact());
3892
3893 // Minimum shift high bits are known zero.
3894 if (std::optional<uint64_t> ShMinAmt =
3895 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3896 Known.Zero.setHighBits(*ShMinAmt);
3897 break;
3898 case ISD::SRA:
3899 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3900 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3901 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3902 Op->getFlags().hasExact());
3903 break;
3904 case ISD::FSHL:
3905 case ISD::FSHR:
3906 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3907 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3908
3909 // For fshl, 0-shift returns the 1st arg.
3910 // For fshr, 0-shift returns the 2nd arg.
3911 if (Amt == 0) {
3912 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3913 DemandedElts, Depth + 1);
3914 break;
3915 }
3916
3917 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3918 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3919 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3920 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3921 if (Opcode == ISD::FSHL) {
3922 Known.One <<= Amt;
3923 Known.Zero <<= Amt;
3924 Known2.One.lshrInPlace(BitWidth - Amt);
3925 Known2.Zero.lshrInPlace(BitWidth - Amt);
3926 } else {
3927 Known.One <<= BitWidth - Amt;
3928 Known.Zero <<= BitWidth - Amt;
3929 Known2.One.lshrInPlace(Amt);
3930 Known2.Zero.lshrInPlace(Amt);
3931 }
3932 Known = Known.unionWith(Known2);
3933 }
3934 break;
3935 case ISD::SHL_PARTS:
3936 case ISD::SRA_PARTS:
3937 case ISD::SRL_PARTS: {
3938 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3939
3940 // Collect lo/hi source values and concatenate.
3941 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3942 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3943 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3944 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3945 Known = Known2.concat(Known);
3946
3947 // Collect shift amount.
3948 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3949
3950 if (Opcode == ISD::SHL_PARTS)
3951 Known = KnownBits::shl(Known, Known2);
3952 else if (Opcode == ISD::SRA_PARTS)
3953 Known = KnownBits::ashr(Known, Known2);
3954 else // if (Opcode == ISD::SRL_PARTS)
3955 Known = KnownBits::lshr(Known, Known2);
3956
3957 // TODO: Minimum shift low/high bits are known zero.
3958
3959 if (Op.getResNo() == 0)
3960 Known = Known.extractBits(LoBits, 0);
3961 else
3962 Known = Known.extractBits(HiBits, LoBits);
3963 break;
3964 }
3966 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3967 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3968 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3969 break;
3970 }
3971 case ISD::CTTZ:
3972 case ISD::CTTZ_ZERO_UNDEF: {
3973 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3974 // If we have a known 1, its position is our upper bound.
3975 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3976 unsigned LowBits = llvm::bit_width(PossibleTZ);
3977 Known.Zero.setBitsFrom(LowBits);
3978 break;
3979 }
3980 case ISD::CTLZ:
3981 case ISD::CTLZ_ZERO_UNDEF: {
3982 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3983 // If we have a known 1, its position is our upper bound.
3984 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3985 unsigned LowBits = llvm::bit_width(PossibleLZ);
3986 Known.Zero.setBitsFrom(LowBits);
3987 break;
3988 }
3989 case ISD::CTPOP: {
3990 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3991 // If we know some of the bits are zero, they can't be one.
3992 unsigned PossibleOnes = Known2.countMaxPopulation();
3993 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3994 break;
3995 }
3996 case ISD::PARITY: {
3997 // Parity returns 0 everywhere but the LSB.
3998 Known.Zero.setBitsFrom(1);
3999 break;
4000 }
4001 case ISD::MGATHER:
4002 case ISD::MLOAD: {
4003 ISD::LoadExtType ETy =
4004 (Opcode == ISD::MGATHER)
4005 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
4006 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
4007 if (ETy == ISD::ZEXTLOAD) {
4008 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
4009 KnownBits Known0(MemVT.getScalarSizeInBits());
4010 return Known0.zext(BitWidth);
4011 }
4012 break;
4013 }
4014 case ISD::LOAD: {
4015 LoadSDNode *LD = cast<LoadSDNode>(Op);
4016 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
4017 if (ISD::isNON_EXTLoad(LD) && Cst) {
4018 // Determine any common known bits from the loaded constant pool value.
4019 Type *CstTy = Cst->getType();
4020 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
4021 !Op.getValueType().isScalableVector()) {
4022 // If its a vector splat, then we can (quickly) reuse the scalar path.
4023 // NOTE: We assume all elements match and none are UNDEF.
4024 if (CstTy->isVectorTy()) {
4025 if (const Constant *Splat = Cst->getSplatValue()) {
4026 Cst = Splat;
4027 CstTy = Cst->getType();
4028 }
4029 }
4030 // TODO - do we need to handle different bitwidths?
4031 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
4032 // Iterate across all vector elements finding common known bits.
4033 Known.One.setAllBits();
4034 Known.Zero.setAllBits();
4035 for (unsigned i = 0; i != NumElts; ++i) {
4036 if (!DemandedElts[i])
4037 continue;
4038 if (Constant *Elt = Cst->getAggregateElement(i)) {
4039 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
4040 const APInt &Value = CInt->getValue();
4041 Known.One &= Value;
4042 Known.Zero &= ~Value;
4043 continue;
4044 }
4045 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
4046 APInt Value = CFP->getValueAPF().bitcastToAPInt();
4047 Known.One &= Value;
4048 Known.Zero &= ~Value;
4049 continue;
4050 }
4051 }
4052 Known.One.clearAllBits();
4053 Known.Zero.clearAllBits();
4054 break;
4055 }
4056 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
4057 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
4058 Known = KnownBits::makeConstant(CInt->getValue());
4059 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
4060 Known =
4061 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
4062 }
4063 }
4064 }
4065 } else if (Op.getResNo() == 0) {
4066 KnownBits Known0(!LD->getMemoryVT().isScalableVT()
4067 ? LD->getMemoryVT().getFixedSizeInBits()
4068 : BitWidth);
4069 EVT VT = Op.getValueType();
4070 // Fill in any known bits from range information. There are 3 types being
4071 // used. The results VT (same vector elt size as BitWidth), the loaded
4072 // MemoryVT (which may or may not be vector) and the range VTs original
4073 // type. The range matadata needs the full range (i.e
4074 // MemoryVT().getSizeInBits()), which is truncated to the correct elt size
4075 // if it is know. These are then extended to the original VT sizes below.
4076 if (const MDNode *MD = LD->getRanges()) {
4078 if (VT.isVector()) {
4079 // Handle truncation to the first demanded element.
4080 // TODO: Figure out which demanded elements are covered
4081 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
4082 break;
4083 Known0 = Known0.trunc(BitWidth);
4084 }
4085 }
4086
4087 if (LD->getMemoryVT().isVector())
4088 Known0 = Known0.trunc(LD->getMemoryVT().getScalarSizeInBits());
4089
4090 // Extend the Known bits from memory to the size of the result.
4091 if (ISD::isZEXTLoad(Op.getNode()))
4092 Known = Known0.zext(BitWidth);
4093 else if (ISD::isSEXTLoad(Op.getNode()))
4094 Known = Known0.sext(BitWidth);
4095 else if (ISD::isEXTLoad(Op.getNode()))
4096 Known = Known0.anyext(BitWidth);
4097 else
4098 Known = Known0;
4099 assert(Known.getBitWidth() == BitWidth);
4100 return Known;
4101 }
4102 break;
4103 }
4105 if (Op.getValueType().isScalableVector())
4106 break;
4107 EVT InVT = Op.getOperand(0).getValueType();
4108 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4109 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4110 Known = Known.zext(BitWidth);
4111 break;
4112 }
4113 case ISD::ZERO_EXTEND: {
4114 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4115 Known = Known.zext(BitWidth);
4116 break;
4117 }
4119 if (Op.getValueType().isScalableVector())
4120 break;
4121 EVT InVT = Op.getOperand(0).getValueType();
4122 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4123 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4124 // If the sign bit is known to be zero or one, then sext will extend
4125 // it to the top bits, else it will just zext.
4126 Known = Known.sext(BitWidth);
4127 break;
4128 }
4129 case ISD::SIGN_EXTEND: {
4130 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4131 // If the sign bit is known to be zero or one, then sext will extend
4132 // it to the top bits, else it will just zext.
4133 Known = Known.sext(BitWidth);
4134 break;
4135 }
4137 if (Op.getValueType().isScalableVector())
4138 break;
4139 EVT InVT = Op.getOperand(0).getValueType();
4140 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4141 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4142 Known = Known.anyext(BitWidth);
4143 break;
4144 }
4145 case ISD::ANY_EXTEND: {
4146 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4147 Known = Known.anyext(BitWidth);
4148 break;
4149 }
4150 case ISD::TRUNCATE: {
4151 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4152 Known = Known.trunc(BitWidth);
4153 break;
4154 }
4155 case ISD::AssertZext: {
4156 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4158 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4159 Known.Zero |= (~InMask);
4160 Known.One &= (~Known.Zero);
4161 break;
4162 }
4163 case ISD::AssertAlign: {
4164 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4165 assert(LogOfAlign != 0);
4166
4167 // TODO: Should use maximum with source
4168 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4169 // well as clearing one bits.
4170 Known.Zero.setLowBits(LogOfAlign);
4171 Known.One.clearLowBits(LogOfAlign);
4172 break;
4173 }
4174 case ISD::FGETSIGN:
4175 // All bits are zero except the low bit.
4176 Known.Zero.setBitsFrom(1);
4177 break;
4178 case ISD::ADD:
4179 case ISD::SUB: {
4180 SDNodeFlags Flags = Op.getNode()->getFlags();
4181 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4182 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4184 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4185 Flags.hasNoUnsignedWrap(), Known, Known2);
4186 break;
4187 }
4188 case ISD::USUBO:
4189 case ISD::SSUBO:
4190 case ISD::USUBO_CARRY:
4191 case ISD::SSUBO_CARRY:
4192 if (Op.getResNo() == 1) {
4193 // If we know the result of a setcc has the top bits zero, use this info.
4194 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4196 BitWidth > 1)
4197 Known.Zero.setBitsFrom(1);
4198 break;
4199 }
4200 [[fallthrough]];
4201 case ISD::SUBC: {
4202 assert(Op.getResNo() == 0 &&
4203 "We only compute knownbits for the difference here.");
4204
4205 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4206 KnownBits Borrow(1);
4207 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4208 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4209 // Borrow has bit width 1
4210 Borrow = Borrow.trunc(1);
4211 } else {
4212 Borrow.setAllZero();
4213 }
4214
4215 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4216 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4217 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4218 break;
4219 }
4220 case ISD::UADDO:
4221 case ISD::SADDO:
4222 case ISD::UADDO_CARRY:
4223 case ISD::SADDO_CARRY:
4224 if (Op.getResNo() == 1) {
4225 // If we know the result of a setcc has the top bits zero, use this info.
4226 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4228 BitWidth > 1)
4229 Known.Zero.setBitsFrom(1);
4230 break;
4231 }
4232 [[fallthrough]];
4233 case ISD::ADDC:
4234 case ISD::ADDE: {
4235 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4236
4237 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4238 KnownBits Carry(1);
4239 if (Opcode == ISD::ADDE)
4240 // Can't track carry from glue, set carry to unknown.
4241 Carry.resetAll();
4242 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4243 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4244 // Carry has bit width 1
4245 Carry = Carry.trunc(1);
4246 } else {
4247 Carry.setAllZero();
4248 }
4249
4250 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4251 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4252 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4253 break;
4254 }
4255 case ISD::UDIV: {
4256 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4257 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4258 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4259 break;
4260 }
4261 case ISD::SDIV: {
4262 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4263 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4264 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4265 break;
4266 }
4267 case ISD::SREM: {
4268 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4269 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4270 Known = KnownBits::srem(Known, Known2);
4271 break;
4272 }
4273 case ISD::UREM: {
4274 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4275 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4276 Known = KnownBits::urem(Known, Known2);
4277 break;
4278 }
4279 case ISD::EXTRACT_ELEMENT: {
4280 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4281 const unsigned Index = Op.getConstantOperandVal(1);
4282 const unsigned EltBitWidth = Op.getValueSizeInBits();
4283
4284 // Remove low part of known bits mask
4285 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4286 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4287
4288 // Remove high part of known bit mask
4289 Known = Known.trunc(EltBitWidth);
4290 break;
4291 }
4293 SDValue InVec = Op.getOperand(0);
4294 SDValue EltNo = Op.getOperand(1);
4295 EVT VecVT = InVec.getValueType();
4296 // computeKnownBits not yet implemented for scalable vectors.
4297 if (VecVT.isScalableVector())
4298 break;
4299 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4300 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4301
4302 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4303 // anything about the extended bits.
4304 if (BitWidth > EltBitWidth)
4305 Known = Known.trunc(EltBitWidth);
4306
4307 // If we know the element index, just demand that vector element, else for
4308 // an unknown element index, ignore DemandedElts and demand them all.
4309 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4310 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4311 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4312 DemandedSrcElts =
4313 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4314
4315 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4316 if (BitWidth > EltBitWidth)
4317 Known = Known.anyext(BitWidth);
4318 break;
4319 }
4321 if (Op.getValueType().isScalableVector())
4322 break;
4323
4324 // If we know the element index, split the demand between the
4325 // source vector and the inserted element, otherwise assume we need
4326 // the original demanded vector elements and the value.
4327 SDValue InVec = Op.getOperand(0);
4328 SDValue InVal = Op.getOperand(1);
4329 SDValue EltNo = Op.getOperand(2);
4330 bool DemandedVal = true;
4331 APInt DemandedVecElts = DemandedElts;
4332 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4333 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4334 unsigned EltIdx = CEltNo->getZExtValue();
4335 DemandedVal = !!DemandedElts[EltIdx];
4336 DemandedVecElts.clearBit(EltIdx);
4337 }
4338 Known.One.setAllBits();
4339 Known.Zero.setAllBits();
4340 if (DemandedVal) {
4341 Known2 = computeKnownBits(InVal, Depth + 1);
4342 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4343 }
4344 if (!!DemandedVecElts) {
4345 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4346 Known = Known.intersectWith(Known2);
4347 }
4348 break;
4349 }
4350 case ISD::BITREVERSE: {
4351 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4352 Known = Known2.reverseBits();
4353 break;
4354 }
4355 case ISD::BSWAP: {
4356 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4357 Known = Known2.byteSwap();
4358 break;
4359 }
4360 case ISD::ABS: {
4361 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4362 Known = Known2.abs();
4363 Known.Zero.setHighBits(
4364 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4365 break;
4366 }
4367 case ISD::USUBSAT: {
4368 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4369 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4370 Known = KnownBits::usub_sat(Known, Known2);
4371 break;
4372 }
4373 case ISD::UMIN: {
4374 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4375 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4376 Known = KnownBits::umin(Known, Known2);
4377 break;
4378 }
4379 case ISD::UMAX: {
4380 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4381 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4382 Known = KnownBits::umax(Known, Known2);
4383 break;
4384 }
4385 case ISD::SMIN:
4386 case ISD::SMAX: {
4387 // If we have a clamp pattern, we know that the number of sign bits will be
4388 // the minimum of the clamp min/max range.
4389 bool IsMax = (Opcode == ISD::SMAX);
4390 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4391 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4392 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4393 CstHigh =
4394 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4395 if (CstLow && CstHigh) {
4396 if (!IsMax)
4397 std::swap(CstLow, CstHigh);
4398
4399 const APInt &ValueLow = CstLow->getAPIntValue();
4400 const APInt &ValueHigh = CstHigh->getAPIntValue();
4401 if (ValueLow.sle(ValueHigh)) {
4402 unsigned LowSignBits = ValueLow.getNumSignBits();
4403 unsigned HighSignBits = ValueHigh.getNumSignBits();
4404 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4405 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4406 Known.One.setHighBits(MinSignBits);
4407 break;
4408 }
4409 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4410 Known.Zero.setHighBits(MinSignBits);
4411 break;
4412 }
4413 }
4414 }
4415
4416 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4417 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4418 if (IsMax)
4419 Known = KnownBits::smax(Known, Known2);
4420 else
4421 Known = KnownBits::smin(Known, Known2);
4422
4423 // For SMAX, if CstLow is non-negative we know the result will be
4424 // non-negative and thus all sign bits are 0.
4425 // TODO: There's an equivalent of this for smin with negative constant for
4426 // known ones.
4427 if (IsMax && CstLow) {
4428 const APInt &ValueLow = CstLow->getAPIntValue();
4429 if (ValueLow.isNonNegative()) {
4430 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4431 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4432 }
4433 }
4434
4435 break;
4436 }
4437 case ISD::UINT_TO_FP: {
4438 Known.makeNonNegative();
4439 break;
4440 }
4441 case ISD::SINT_TO_FP: {
4442 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4443 if (Known2.isNonNegative())
4444 Known.makeNonNegative();
4445 else if (Known2.isNegative())
4446 Known.makeNegative();
4447 break;
4448 }
4449 case ISD::FP_TO_UINT_SAT: {
4450 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4451 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4453 break;
4454 }
4456 if (Op.getResNo() == 1) {
4457 // The boolean result conforms to getBooleanContents.
4458 // If we know the result of a setcc has the top bits zero, use this info.
4459 // We know that we have an integer-based boolean since these operations
4460 // are only available for integer.
4461 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4463 BitWidth > 1)
4464 Known.Zero.setBitsFrom(1);
4465 break;
4466 }
4467 [[fallthrough]];
4469 case ISD::ATOMIC_SWAP:
4481 case ISD::ATOMIC_LOAD: {
4482 unsigned MemBits =
4483 cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
4484 // If we are looking at the loaded value.
4485 if (Op.getResNo() == 0) {
4487 Known.Zero.setBitsFrom(MemBits);
4488 else if (Op->getOpcode() == ISD::ATOMIC_LOAD &&
4489 cast<AtomicSDNode>(Op)->getExtensionType() == ISD::ZEXTLOAD)
4490 Known.Zero.setBitsFrom(MemBits);
4491 }
4492 break;
4493 }
4494 case ISD::FrameIndex:
4496 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4497 Known, getMachineFunction());
4498 break;
4499
4500 default:
4501 if (Opcode < ISD::BUILTIN_OP_END)
4502 break;
4503 [[fallthrough]];
4507 // TODO: Probably okay to remove after audit; here to reduce change size
4508 // in initial enablement patch for scalable vectors
4509 if (Op.getValueType().isScalableVector())
4510 break;
4511
4512 // Allow the target to implement this method for its nodes.
4513 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4514 break;
4515 }
4516
4517 return Known;
4518}
4519
4520/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4522 switch (OR) {
4530 }
4531 llvm_unreachable("Unknown OverflowResult");
4532}
4533
4536 // X + 0 never overflow
4537 if (isNullConstant(N1))
4538 return OFK_Never;
4539
4540 // If both operands each have at least two sign bits, the addition
4541 // cannot overflow.
4542 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4543 return OFK_Never;
4544
4545 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4546 return OFK_Sometime;
4547}
4548
4551 // X + 0 never overflow
4552 if (isNullConstant(N1))
4553 return OFK_Never;
4554
4555 // mulhi + 1 never overflow
4556 KnownBits N1Known = computeKnownBits(N1);
4557 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4558 N1Known.getMaxValue().ult(2))
4559 return OFK_Never;
4560
4561 KnownBits N0Known = computeKnownBits(N0);
4562 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4563 N0Known.getMaxValue().ult(2))
4564 return OFK_Never;
4565
4566 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4567 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4568 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4569 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4570}
4571
4574 // X - 0 never overflow
4575 if (isNullConstant(N1))
4576 return OFK_Never;
4577
4578 // If both operands each have at least two sign bits, the subtraction
4579 // cannot overflow.
4580 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4581 return OFK_Never;
4582
4583 KnownBits N0Known = computeKnownBits(N0);
4584 KnownBits N1Known = computeKnownBits(N1);
4585 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4586 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4587 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4588}
4589
4592 // X - 0 never overflow
4593 if (isNullConstant(N1))
4594 return OFK_Never;
4595
4596 KnownBits N0Known = computeKnownBits(N0);
4597 KnownBits N1Known = computeKnownBits(N1);
4598 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4599 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4600 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4601}
4602
4605 // X * 0 and X * 1 never overflow.
4606 if (isNullConstant(N1) || isOneConstant(N1))
4607 return OFK_Never;
4608
4609 KnownBits N0Known = computeKnownBits(N0);
4610 KnownBits N1Known = computeKnownBits(N1);
4611 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4612 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4613 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4614}
4615
4618 // X * 0 and X * 1 never overflow.
4619 if (isNullConstant(N1) || isOneConstant(N1))
4620 return OFK_Never;
4621
4622 // Get the size of the result.
4623 unsigned BitWidth = N0.getScalarValueSizeInBits();
4624
4625 // Sum of the sign bits.
4626 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4627
4628 // If we have enough sign bits, then there's no overflow.
4629 if (SignBits > BitWidth + 1)
4630 return OFK_Never;
4631
4632 if (SignBits == BitWidth + 1) {
4633 // The overflow occurs when the true multiplication of the
4634 // the operands is the minimum negative number.
4635 KnownBits N0Known = computeKnownBits(N0);
4636 KnownBits N1Known = computeKnownBits(N1);
4637 // If one of the operands is non-negative, then there's no
4638 // overflow.
4639 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4640 return OFK_Never;
4641 }
4642
4643 return OFK_Sometime;
4644}
4645
4647 if (Depth >= MaxRecursionDepth)
4648 return false; // Limit search depth.
4649
4650 EVT OpVT = Val.getValueType();
4651 unsigned BitWidth = OpVT.getScalarSizeInBits();
4652
4653 // Is the constant a known power of 2?
4655 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4656 }))
4657 return true;
4658
4659 // A left-shift of a constant one will have exactly one bit set because
4660 // shifting the bit off the end is undefined.
4661 if (Val.getOpcode() == ISD::SHL) {
4662 auto *C = isConstOrConstSplat(Val.getOperand(0));
4663 if (C && C->getAPIntValue() == 1)
4664 return true;
4665 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4666 isKnownNeverZero(Val, Depth);
4667 }
4668
4669 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4670 // one bit set.
4671 if (Val.getOpcode() == ISD::SRL) {
4672 auto *C = isConstOrConstSplat(Val.getOperand(0));
4673 if (C && C->getAPIntValue().isSignMask())
4674 return true;
4675 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4676 isKnownNeverZero(Val, Depth);
4677 }
4678
4679 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4680 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4681
4682 // Are all operands of a build vector constant powers of two?
4683 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4684 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4685 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4686 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4687 return false;
4688 }))
4689 return true;
4690
4691 // Is the operand of a splat vector a constant power of two?
4692 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4693 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val->getOperand(0)))
4694 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4695 return true;
4696
4697 // vscale(power-of-two) is a power-of-two for some targets
4698 if (Val.getOpcode() == ISD::VSCALE &&
4699 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4701 return true;
4702
4703 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4704 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4705 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4707
4708 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4709 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4711
4712 // Looking for `x & -x` pattern:
4713 // If x == 0:
4714 // x & -x -> 0
4715 // If x != 0:
4716 // x & -x -> non-zero pow2
4717 // so if we find the pattern return whether we know `x` is non-zero.
4718 SDValue X;
4719 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4720 return isKnownNeverZero(X, Depth);
4721
4722 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4723 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4724
4725 // More could be done here, though the above checks are enough
4726 // to handle some common cases.
4727 return false;
4728}
4729
4731 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4732 return C1->getValueAPF().getExactLog2Abs() >= 0;
4733
4734 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4735 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4736
4737 return false;
4738}
4739
4741 EVT VT = Op.getValueType();
4742
4743 // Since the number of lanes in a scalable vector is unknown at compile time,
4744 // we track one bit which is implicitly broadcast to all lanes. This means
4745 // that all lanes in a scalable vector are considered demanded.
4746 APInt DemandedElts = VT.isFixedLengthVector()
4748 : APInt(1, 1);
4749 return ComputeNumSignBits(Op, DemandedElts, Depth);
4750}
4751
4752unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4753 unsigned Depth) const {
4754 EVT VT = Op.getValueType();
4755 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4756 unsigned VTBits = VT.getScalarSizeInBits();
4757 unsigned NumElts = DemandedElts.getBitWidth();
4758 unsigned Tmp, Tmp2;
4759 unsigned FirstAnswer = 1;
4760
4761 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4762 const APInt &Val = C->getAPIntValue();
4763 return Val.getNumSignBits();
4764 }
4765
4766 if (Depth >= MaxRecursionDepth)
4767 return 1; // Limit search depth.
4768
4769 if (!DemandedElts)
4770 return 1; // No demanded elts, better to assume we don't know anything.
4771
4772 unsigned Opcode = Op.getOpcode();
4773 switch (Opcode) {
4774 default: break;
4775 case ISD::AssertSext:
4776 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4777 return VTBits-Tmp+1;
4778 case ISD::AssertZext:
4779 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4780 return VTBits-Tmp;
4781 case ISD::MERGE_VALUES:
4782 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4783 Depth + 1);
4784 case ISD::SPLAT_VECTOR: {
4785 // Check if the sign bits of source go down as far as the truncated value.
4786 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4787 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4788 if (NumSrcSignBits > (NumSrcBits - VTBits))
4789 return NumSrcSignBits - (NumSrcBits - VTBits);
4790 break;
4791 }
4792 case ISD::BUILD_VECTOR:
4793 assert(!VT.isScalableVector());
4794 Tmp = VTBits;
4795 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4796 if (!DemandedElts[i])
4797 continue;
4798
4799 SDValue SrcOp = Op.getOperand(i);
4800 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4801 // for constant nodes to ensure we only look at the sign bits.
4802 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SrcOp)) {
4803 APInt T = C->getAPIntValue().trunc(VTBits);
4804 Tmp2 = T.getNumSignBits();
4805 } else {
4806 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4807
4808 if (SrcOp.getValueSizeInBits() != VTBits) {
4809 assert(SrcOp.getValueSizeInBits() > VTBits &&
4810 "Expected BUILD_VECTOR implicit truncation");
4811 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4812 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4813 }
4814 }
4815 Tmp = std::min(Tmp, Tmp2);
4816 }
4817 return Tmp;
4818
4819 case ISD::VECTOR_SHUFFLE: {
4820 // Collect the minimum number of sign bits that are shared by every vector
4821 // element referenced by the shuffle.
4822 APInt DemandedLHS, DemandedRHS;
4823 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
4824 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4825 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4826 DemandedLHS, DemandedRHS))
4827 return 1;
4828
4829 Tmp = std::numeric_limits<unsigned>::max();
4830 if (!!DemandedLHS)
4831 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4832 if (!!DemandedRHS) {
4833 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4834 Tmp = std::min(Tmp, Tmp2);
4835 }
4836 // If we don't know anything, early out and try computeKnownBits fall-back.
4837 if (Tmp == 1)
4838 break;
4839 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4840 return Tmp;
4841 }
4842
4843 case ISD::BITCAST: {
4844 if (VT.isScalableVector())
4845 break;
4846 SDValue N0 = Op.getOperand(0);
4847 EVT SrcVT = N0.getValueType();
4848 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4849
4850 // Ignore bitcasts from unsupported types..
4851 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4852 break;
4853
4854 // Fast handling of 'identity' bitcasts.
4855 if (VTBits == SrcBits)
4856 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4857
4858 bool IsLE = getDataLayout().isLittleEndian();
4859
4860 // Bitcast 'large element' scalar/vector to 'small element' vector.
4861 if ((SrcBits % VTBits) == 0) {
4862 assert(VT.isVector() && "Expected bitcast to vector");
4863
4864 unsigned Scale = SrcBits / VTBits;
4865 APInt SrcDemandedElts =
4866 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4867
4868 // Fast case - sign splat can be simply split across the small elements.
4869 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4870 if (Tmp == SrcBits)
4871 return VTBits;
4872
4873 // Slow case - determine how far the sign extends into each sub-element.
4874 Tmp2 = VTBits;
4875 for (unsigned i = 0; i != NumElts; ++i)
4876 if (DemandedElts[i]) {
4877 unsigned SubOffset = i % Scale;
4878 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4879 SubOffset = SubOffset * VTBits;
4880 if (Tmp <= SubOffset)
4881 return 1;
4882 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4883 }
4884 return Tmp2;
4885 }
4886 break;
4887 }
4888
4890 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4891 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4892 return VTBits - Tmp + 1;
4893 case ISD::SIGN_EXTEND:
4894 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4895 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4897 // Max of the input and what this extends.
4898 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4899 Tmp = VTBits-Tmp+1;
4900 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4901 return std::max(Tmp, Tmp2);
4903 if (VT.isScalableVector())
4904 break;
4905 SDValue Src = Op.getOperand(0);
4906 EVT SrcVT = Src.getValueType();
4907 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4908 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4909 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4910 }
4911 case ISD::SRA:
4912 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4913 // SRA X, C -> adds C sign bits.
4914 if (std::optional<uint64_t> ShAmt =
4915 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4916 Tmp = std::min<uint64_t>(Tmp + *ShAmt, VTBits);
4917 return Tmp;
4918 case ISD::SHL:
4919 if (std::optional<ConstantRange> ShAmtRange =
4920 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4921 uint64_t MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4922 uint64_t MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4923 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4924 // shifted out, then we can compute the number of sign bits for the
4925 // operand being extended. A future improvement could be to pass along the
4926 // "shifted left by" information in the recursive calls to
4927 // ComputeKnownSignBits. Allowing us to handle this more generically.
4928 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4929 SDValue Ext = Op.getOperand(0);
4930 EVT ExtVT = Ext.getValueType();
4931 SDValue Extendee = Ext.getOperand(0);
4932 EVT ExtendeeVT = Extendee.getValueType();
4933 uint64_t SizeDifference =
4934 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4935 if (SizeDifference <= MinShAmt) {
4936 Tmp = SizeDifference +
4937 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4938 if (MaxShAmt < Tmp)
4939 return Tmp - MaxShAmt;
4940 }
4941 }
4942 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4943 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4944 if (MaxShAmt < Tmp)
4945 return Tmp - MaxShAmt;
4946 }
4947 break;
4948 case ISD::AND:
4949 case ISD::OR:
4950 case ISD::XOR: // NOT is handled here.
4951 // Logical binary ops preserve the number of sign bits at the worst.
4952 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4953 if (Tmp != 1) {
4954 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4955 FirstAnswer = std::min(Tmp, Tmp2);
4956 // We computed what we know about the sign bits as our first
4957 // answer. Now proceed to the generic code that uses
4958 // computeKnownBits, and pick whichever answer is better.
4959 }
4960 break;
4961
4962 case ISD::SELECT:
4963 case ISD::VSELECT:
4964 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4965 if (Tmp == 1) return 1; // Early out.
4966 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4967 return std::min(Tmp, Tmp2);
4968 case ISD::SELECT_CC:
4969 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4970 if (Tmp == 1) return 1; // Early out.
4971 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4972 return std::min(Tmp, Tmp2);
4973
4974 case ISD::SMIN:
4975 case ISD::SMAX: {
4976 // If we have a clamp pattern, we know that the number of sign bits will be
4977 // the minimum of the clamp min/max range.
4978 bool IsMax = (Opcode == ISD::SMAX);
4979 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4980 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4981 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4982 CstHigh =
4983 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4984 if (CstLow && CstHigh) {
4985 if (!IsMax)
4986 std::swap(CstLow, CstHigh);
4987 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4988 Tmp = CstLow->getAPIntValue().getNumSignBits();
4989 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4990 return std::min(Tmp, Tmp2);
4991 }
4992 }
4993
4994 // Fallback - just get the minimum number of sign bits of the operands.
4995 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4996 if (Tmp == 1)
4997 return 1; // Early out.
4998 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4999 return std::min(Tmp, Tmp2);
5000 }
5001 case ISD::UMIN:
5002 case ISD::UMAX:
5003 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5004 if (Tmp == 1)
5005 return 1; // Early out.
5006 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5007 return std::min(Tmp, Tmp2);
5008 case ISD::SSUBO_CARRY:
5009 case ISD::USUBO_CARRY:
5010 // sub_carry(x,x,c) -> 0/-1 (sext carry)
5011 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
5012 return VTBits;
5013 [[fallthrough]];
5014 case ISD::SADDO:
5015 case ISD::UADDO:
5016 case ISD::SADDO_CARRY:
5017 case ISD::UADDO_CARRY:
5018 case ISD::SSUBO:
5019 case ISD::USUBO:
5020 case ISD::SMULO:
5021 case ISD::UMULO:
5022 if (Op.getResNo() != 1)
5023 break;
5024 // The boolean result conforms to getBooleanContents. Fall through.
5025 // If setcc returns 0/-1, all bits are sign bits.
5026 // We know that we have an integer-based boolean since these operations
5027 // are only available for integer.
5028 if (TLI->getBooleanContents(VT.isVector(), false) ==
5030 return VTBits;
5031 break;
5032 case ISD::SETCC:
5033 case ISD::SETCCCARRY:
5034 case ISD::STRICT_FSETCC:
5035 case ISD::STRICT_FSETCCS: {
5036 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
5037 // If setcc returns 0/-1, all bits are sign bits.
5038 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
5040 return VTBits;
5041 break;
5042 }
5043 case ISD::ROTL:
5044 case ISD::ROTR:
5045 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5046
5047 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
5048 if (Tmp == VTBits)
5049 return VTBits;
5050
5051 if (ConstantSDNode *C =
5052 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
5053 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
5054
5055 // Handle rotate right by N like a rotate left by 32-N.
5056 if (Opcode == ISD::ROTR)
5057 RotAmt = (VTBits - RotAmt) % VTBits;
5058
5059 // If we aren't rotating out all of the known-in sign bits, return the
5060 // number that are left. This handles rotl(sext(x), 1) for example.
5061 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
5062 }
5063 break;
5064 case ISD::ADD:
5065 case ISD::ADDC:
5066 // Add can have at most one carry bit. Thus we know that the output
5067 // is, at worst, one more bit than the inputs.
5068 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5069 if (Tmp == 1) return 1; // Early out.
5070
5071 // Special case decrementing a value (ADD X, -1):
5072 if (ConstantSDNode *CRHS =
5073 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5074 if (CRHS->isAllOnes()) {
5075 KnownBits Known =
5076 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5077
5078 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5079 // sign bits set.
5080 if ((Known.Zero | 1).isAllOnes())
5081 return VTBits;
5082
5083 // If we are subtracting one from a positive number, there is no carry
5084 // out of the result.
5085 if (Known.isNonNegative())
5086 return Tmp;
5087 }
5088
5089 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5090 if (Tmp2 == 1) return 1; // Early out.
5091 return std::min(Tmp, Tmp2) - 1;
5092 case ISD::SUB:
5093 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5094 if (Tmp2 == 1) return 1; // Early out.
5095
5096 // Handle NEG.
5097 if (ConstantSDNode *CLHS =
5098 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5099 if (CLHS->isZero()) {
5100 KnownBits Known =
5101 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5102 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5103 // sign bits set.
5104 if ((Known.Zero | 1).isAllOnes())
5105 return VTBits;
5106
5107 // If the input is known to be positive (the sign bit is known clear),
5108 // the output of the NEG has the same number of sign bits as the input.
5109 if (Known.isNonNegative())
5110 return Tmp2;
5111
5112 // Otherwise, we treat this like a SUB.
5113 }
5114
5115 // Sub can have at most one carry bit. Thus we know that the output
5116 // is, at worst, one more bit than the inputs.
5117 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5118 if (Tmp == 1) return 1; // Early out.
5119 return std::min(Tmp, Tmp2) - 1;
5120 case ISD::MUL: {
5121 // The output of the Mul can be at most twice the valid bits in the inputs.
5122 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5123 if (SignBitsOp0 == 1)
5124 break;
5125 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5126 if (SignBitsOp1 == 1)
5127 break;
5128 unsigned OutValidBits =
5129 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5130 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5131 }
5132 case ISD::AVGCEILS:
5133 case ISD::AVGFLOORS:
5134 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5135 if (Tmp == 1)
5136 return 1; // Early out.
5137 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5138 return std::min(Tmp, Tmp2);
5139 case ISD::SREM:
5140 // The sign bit is the LHS's sign bit, except when the result of the
5141 // remainder is zero. The magnitude of the result should be less than or
5142 // equal to the magnitude of the LHS. Therefore, the result should have
5143 // at least as many sign bits as the left hand side.
5144 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5145 case ISD::TRUNCATE: {
5146 // Check if the sign bits of source go down as far as the truncated value.
5147 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5148 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5149 if (NumSrcSignBits > (NumSrcBits - VTBits))
5150 return NumSrcSignBits - (NumSrcBits - VTBits);
5151 break;
5152 }
5153 case ISD::EXTRACT_ELEMENT: {
5154 if (VT.isScalableVector())
5155 break;
5156 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5157 const int BitWidth = Op.getValueSizeInBits();
5158 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5159
5160 // Get reverse index (starting from 1), Op1 value indexes elements from
5161 // little end. Sign starts at big end.
5162 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5163
5164 // If the sign portion ends in our element the subtraction gives correct
5165 // result. Otherwise it gives either negative or > bitwidth result
5166 return std::clamp(KnownSign - rIndex * BitWidth, 0, BitWidth);
5167 }
5169 if (VT.isScalableVector())
5170 break;
5171 // If we know the element index, split the demand between the
5172 // source vector and the inserted element, otherwise assume we need
5173 // the original demanded vector elements and the value.
5174 SDValue InVec = Op.getOperand(0);
5175 SDValue InVal = Op.getOperand(1);
5176 SDValue EltNo = Op.getOperand(2);
5177 bool DemandedVal = true;
5178 APInt DemandedVecElts = DemandedElts;
5179 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5180 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5181 unsigned EltIdx = CEltNo->getZExtValue();
5182 DemandedVal = !!DemandedElts[EltIdx];
5183 DemandedVecElts.clearBit(EltIdx);
5184 }
5185 Tmp = std::numeric_limits<unsigned>::max();
5186 if (DemandedVal) {
5187 // TODO - handle implicit truncation of inserted elements.
5188 if (InVal.getScalarValueSizeInBits() != VTBits)
5189 break;
5190 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5191 Tmp = std::min(Tmp, Tmp2);
5192 }
5193 if (!!DemandedVecElts) {
5194 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5195 Tmp = std::min(Tmp, Tmp2);
5196 }
5197 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5198 return Tmp;
5199 }
5201 assert(!VT.isScalableVector());
5202 SDValue InVec = Op.getOperand(0);
5203 SDValue EltNo = Op.getOperand(1);
5204 EVT VecVT = InVec.getValueType();
5205 // ComputeNumSignBits not yet implemented for scalable vectors.
5206 if (VecVT.isScalableVector())
5207 break;
5208 const unsigned BitWidth = Op.getValueSizeInBits();
5209 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5210 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5211
5212 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5213 // anything about sign bits. But if the sizes match we can derive knowledge
5214 // about sign bits from the vector operand.
5215 if (BitWidth != EltBitWidth)
5216 break;
5217
5218 // If we know the element index, just demand that vector element, else for
5219 // an unknown element index, ignore DemandedElts and demand them all.
5220 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5221 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5222 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5223 DemandedSrcElts =
5224 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5225
5226 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5227 }
5229 // Offset the demanded elts by the subvector index.
5230 SDValue Src = Op.getOperand(0);
5231 // Bail until we can represent demanded elements for scalable vectors.
5232 if (Src.getValueType().isScalableVector())
5233 break;
5234 uint64_t Idx = Op.getConstantOperandVal(1);
5235 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5236 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5237 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5238 }
5239 case ISD::CONCAT_VECTORS: {
5240 if (VT.isScalableVector())
5241 break;
5242 // Determine the minimum number of sign bits across all demanded
5243 // elts of the input vectors. Early out if the result is already 1.
5244 Tmp = std::numeric_limits<unsigned>::max();
5245 EVT SubVectorVT = Op.getOperand(0).getValueType();
5246 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5247 unsigned NumSubVectors = Op.getNumOperands();
5248 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5249 APInt DemandedSub =
5250 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5251 if (!DemandedSub)
5252 continue;
5253 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5254 Tmp = std::min(Tmp, Tmp2);
5255 }
5256 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5257 return Tmp;
5258 }
5259 case ISD::INSERT_SUBVECTOR: {
5260 if (VT.isScalableVector())
5261 break;
5262 // Demand any elements from the subvector and the remainder from the src its
5263 // inserted into.
5264 SDValue Src = Op.getOperand(0);
5265 SDValue Sub = Op.getOperand(1);
5266 uint64_t Idx = Op.getConstantOperandVal(2);
5267 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5268 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5269 APInt DemandedSrcElts = DemandedElts;
5270 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
5271
5272 Tmp = std::numeric_limits<unsigned>::max();
5273 if (!!DemandedSubElts) {
5274 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5275 if (Tmp == 1)
5276 return 1; // early-out
5277 }
5278 if (!!DemandedSrcElts) {
5279 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5280 Tmp = std::min(Tmp, Tmp2);
5281 }
5282 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5283 return Tmp;
5284 }
5285 case ISD::LOAD: {
5286 LoadSDNode *LD = cast<LoadSDNode>(Op);
5287 if (const MDNode *Ranges = LD->getRanges()) {
5288 if (DemandedElts != 1)
5289 break;
5290
5292 if (VTBits > CR.getBitWidth()) {
5293 switch (LD->getExtensionType()) {
5294 case ISD::SEXTLOAD:
5295 CR = CR.signExtend(VTBits);
5296 break;
5297 case ISD::ZEXTLOAD:
5298 CR = CR.zeroExtend(VTBits);
5299 break;
5300 default:
5301 break;
5302 }
5303 }
5304
5305 if (VTBits != CR.getBitWidth())
5306 break;
5307 return std::min(CR.getSignedMin().getNumSignBits(),
5309 }
5310
5311 break;
5312 }
5315 case ISD::ATOMIC_SWAP:
5327 case ISD::ATOMIC_LOAD: {
5328 Tmp = cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
5329 // If we are looking at the loaded value.
5330 if (Op.getResNo() == 0) {
5331 if (Tmp == VTBits)
5332 return 1; // early-out
5334 return VTBits - Tmp + 1;
5336 return VTBits - Tmp;
5337 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5338 ISD::LoadExtType ETy = cast<AtomicSDNode>(Op)->getExtensionType();
5339 if (ETy == ISD::SEXTLOAD)
5340 return VTBits - Tmp + 1;
5341 if (ETy == ISD::ZEXTLOAD)
5342 return VTBits - Tmp;
5343 }
5344 }
5345 break;
5346 }
5347 }
5348
5349 // If we are looking at the loaded value of the SDNode.
5350 if (Op.getResNo() == 0) {
5351 // Handle LOADX separately here. EXTLOAD case will fallthrough.
5352 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
5353 unsigned ExtType = LD->getExtensionType();
5354 switch (ExtType) {
5355 default: break;
5356 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5357 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5358 return VTBits - Tmp + 1;
5359 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5360 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5361 return VTBits - Tmp;
5362 case ISD::NON_EXTLOAD:
5363 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5364 // We only need to handle vectors - computeKnownBits should handle
5365 // scalar cases.
5366 Type *CstTy = Cst->getType();
5367 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5368 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5369 VTBits == CstTy->getScalarSizeInBits()) {
5370 Tmp = VTBits;
5371 for (unsigned i = 0; i != NumElts; ++i) {
5372 if (!DemandedElts[i])
5373 continue;
5374 if (Constant *Elt = Cst->getAggregateElement(i)) {
5375 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5376 const APInt &Value = CInt->getValue();
5377 Tmp = std::min(Tmp, Value.getNumSignBits());
5378 continue;
5379 }
5380 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5381 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5382 Tmp = std::min(Tmp, Value.getNumSignBits());
5383 continue;
5384 }
5385 }
5386 // Unknown type. Conservatively assume no bits match sign bit.
5387 return 1;
5388 }
5389 return Tmp;
5390 }
5391 }
5392 break;
5393 }
5394 }
5395 }
5396
5397 // Allow the target to implement this method for its nodes.
5398 if (Opcode >= ISD::BUILTIN_OP_END ||
5399 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5400 Opcode == ISD::INTRINSIC_W_CHAIN ||
5401 Opcode == ISD::INTRINSIC_VOID) {
5402 // TODO: This can probably be removed once target code is audited. This
5403 // is here purely to reduce patch size and review complexity.
5404 if (!VT.isScalableVector()) {
5405 unsigned NumBits =
5406 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5407 if (NumBits > 1)
5408 FirstAnswer = std::max(FirstAnswer, NumBits);
5409 }
5410 }
5411
5412 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5413 // use this information.
5414 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5415 return std::max(FirstAnswer, Known.countMinSignBits());
5416}
5417
5419 unsigned Depth) const {
5420 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5421 return Op.getScalarValueSizeInBits() - SignBits + 1;
5422}
5423
5425 const APInt &DemandedElts,
5426 unsigned Depth) const {
5427 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5428 return Op.getScalarValueSizeInBits() - SignBits + 1;
5429}
5430
5432 unsigned Depth) const {
5433 // Early out for FREEZE.
5434 if (Op.getOpcode() == ISD::FREEZE)
5435 return true;
5436
5437 EVT VT = Op.getValueType();
5438 APInt DemandedElts = VT.isFixedLengthVector()
5440 : APInt(1, 1);
5441 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5442}
5443
5445 const APInt &DemandedElts,
5446 bool PoisonOnly,
5447 unsigned Depth) const {
5448 unsigned Opcode = Op.getOpcode();
5449
5450 // Early out for FREEZE.
5451 if (Opcode == ISD::FREEZE)
5452 return true;
5453
5454 if (Depth >= MaxRecursionDepth)
5455 return false; // Limit search depth.
5456
5457 if (isIntOrFPConstant(Op))
5458 return true;
5459
5460 switch (Opcode) {
5461 case ISD::CONDCODE:
5462 case ISD::VALUETYPE:
5463 case ISD::FrameIndex:
5465 case ISD::CopyFromReg:
5466 return true;
5467
5468 case ISD::UNDEF:
5469 return PoisonOnly;
5470
5471 case ISD::BUILD_VECTOR:
5472 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5473 // this shouldn't affect the result.
5474 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5475 if (!DemandedElts[i])
5476 continue;
5478 Depth + 1))
5479 return false;
5480 }
5481 return true;
5482
5483 case ISD::SPLAT_VECTOR:
5484 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5485 Depth + 1);
5486
5487 case ISD::VECTOR_SHUFFLE: {
5488 APInt DemandedLHS, DemandedRHS;
5489 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5490 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5491 DemandedElts, DemandedLHS, DemandedRHS,
5492 /*AllowUndefElts=*/false))
5493 return false;
5494 if (!DemandedLHS.isZero() &&
5495 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5496 PoisonOnly, Depth + 1))
5497 return false;
5498 if (!DemandedRHS.isZero() &&
5499 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5500 PoisonOnly, Depth + 1))
5501 return false;
5502 return true;
5503 }
5504
5505 // TODO: Search for noundef attributes from library functions.
5506
5507 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5508
5509 default:
5510 // Allow the target to implement this method for its nodes.
5511 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5512 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5514 Op, DemandedElts, *this, PoisonOnly, Depth);
5515 break;
5516 }
5517
5518 // If Op can't create undef/poison and none of its operands are undef/poison
5519 // then Op is never undef/poison.
5520 // NOTE: TargetNodes can handle this in themselves in
5521 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5522 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5523 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5524 Depth) &&
5525 all_of(Op->ops(), [&](SDValue V) {
5526 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5527 });
5528}
5529
5531 bool ConsiderFlags,
5532 unsigned Depth) const {
5533 EVT VT = Op.getValueType();
5534 APInt DemandedElts = VT.isFixedLengthVector()
5536 : APInt(1, 1);
5537 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5538 Depth);
5539}
5540
5542 bool PoisonOnly, bool ConsiderFlags,
5543 unsigned Depth) const {
5544 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5545 return true;
5546
5547 unsigned Opcode = Op.getOpcode();
5548 switch (Opcode) {
5549 case ISD::FREEZE:
5552 case ISD::SADDSAT:
5553 case ISD::UADDSAT:
5554 case ISD::SSUBSAT:
5555 case ISD::USUBSAT:
5556 case ISD::MULHU:
5557 case ISD::MULHS:
5558 case ISD::SMIN:
5559 case ISD::SMAX:
5560 case ISD::UMIN:
5561 case ISD::UMAX:
5562 case ISD::AND:
5563 case ISD::XOR:
5564 case ISD::ROTL:
5565 case ISD::ROTR:
5566 case ISD::FSHL:
5567 case ISD::FSHR:
5568 case ISD::BSWAP:
5569 case ISD::CTPOP:
5570 case ISD::BITREVERSE:
5571 case ISD::PARITY:
5572 case ISD::SIGN_EXTEND:
5573 case ISD::TRUNCATE:
5577 case ISD::BITCAST:
5578 case ISD::BUILD_VECTOR:
5579 case ISD::BUILD_PAIR:
5580 case ISD::SPLAT_VECTOR:
5581 return false;
5582
5583 case ISD::SELECT_CC:
5584 case ISD::SETCC: {
5585 // Integer setcc cannot create undef or poison.
5586 if (Op.getOperand(0).getValueType().isInteger())
5587 return false;
5588
5589 // FP compares are more complicated. They can create poison for nan/infinity
5590 // based on options and flags. The options and flags also cause special
5591 // nonan condition codes to be used. Those condition codes may be preserved
5592 // even if the nonan flag is dropped somewhere.
5593 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5594 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5595 if (((unsigned)CCCode & 0x10U))
5596 return true;
5597
5599 return Options.NoNaNsFPMath || Options.NoInfsFPMath;
5600 }
5601
5602 case ISD::OR:
5603 case ISD::ZERO_EXTEND:
5604 case ISD::ADD:
5605 case ISD::SUB:
5606 case ISD::MUL:
5607 // No poison except from flags (which is handled above)
5608 return false;
5609
5610 case ISD::SHL:
5611 case ISD::SRL:
5612 case ISD::SRA:
5613 // If the max shift amount isn't in range, then the shift can
5614 // create poison.
5615 return !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedElts,
5616 PoisonOnly, Depth + 1) ||
5617 !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5618
5620 // Check if we demand any upper (undef) elements.
5621 return !PoisonOnly && DemandedElts.ugt(1);
5622
5625 // Ensure that the element index is in bounds.
5626 EVT VecVT = Op.getOperand(0).getValueType();
5627 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5629 Depth + 1)) {
5630 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5631 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5632 }
5633 return true;
5634 }
5635
5636 case ISD::VECTOR_SHUFFLE: {
5637 // Check for any demanded shuffle element that is undef.
5638 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5639 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5640 if (Elt < 0 && DemandedElts[Idx])
5641 return true;
5642 return false;
5643 }
5644
5645 default:
5646 // Allow the target to implement this method for its nodes.
5647 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5648 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5650 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5651 break;
5652 }
5653
5654 // Be conservative and return true.
5655 return true;
5656}
5657
5658bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5659 unsigned Opcode = Op.getOpcode();
5660 if (Opcode == ISD::OR)
5661 return Op->getFlags().hasDisjoint() ||
5662 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5663 if (Opcode == ISD::XOR)
5664 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5665 return false;
5666}
5667
5669 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5670 (Op.getOpcode() == ISD::ADD || isADDLike(Op));
5671}
5672
5673bool SelectionDAG::isKnownNeverNaN(SDValue Op, bool SNaN, unsigned Depth) const {
5674 // If we're told that NaNs won't happen, assume they won't.
5675 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5676 return true;
5677
5678 if (Depth >= MaxRecursionDepth)
5679 return false; // Limit search depth.
5680
5681 // If the value is a constant, we can obviously see if it is a NaN or not.
5682 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) {
5683 return !C->getValueAPF().isNaN() ||
5684 (SNaN && !C->getValueAPF().isSignaling());
5685 }
5686
5687 unsigned Opcode = Op.getOpcode();
5688 switch (Opcode) {
5689 case ISD::FADD:
5690 case ISD::FSUB:
5691 case ISD::FMUL:
5692 case ISD::FDIV:
5693 case ISD::FREM:
5694 case ISD::FSIN:
5695 case ISD::FCOS:
5696 case ISD::FTAN:
5697 case ISD::FASIN:
5698 case ISD::FACOS:
5699 case ISD::FATAN:
5700 case ISD::FATAN2:
5701 case ISD::FSINH:
5702 case ISD::FCOSH:
5703 case ISD::FTANH:
5704 case ISD::FMA:
5705 case ISD::FMAD: {
5706 if (SNaN)
5707 return true;
5708 // TODO: Need isKnownNeverInfinity
5709 return false;
5710 }
5711 case ISD::FCANONICALIZE:
5712 case ISD::FEXP:
5713 case ISD::FEXP2:
5714 case ISD::FEXP10:
5715 case ISD::FTRUNC:
5716 case ISD::FFLOOR:
5717 case ISD::FCEIL:
5718 case ISD::FROUND:
5719 case ISD::FROUNDEVEN:
5720 case ISD::LROUND:
5721 case ISD::LLROUND:
5722 case ISD::FRINT:
5723 case ISD::LRINT:
5724 case ISD::LLRINT:
5725 case ISD::FNEARBYINT:
5726 case ISD::FLDEXP: {
5727 if (SNaN)
5728 return true;
5729 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5730 }
5731 case ISD::FABS:
5732 case ISD::FNEG:
5733 case ISD::FCOPYSIGN: {
5734 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5735 }
5736 case ISD::SELECT:
5737 return isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) &&
5738 isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1);
5739 case ISD::FP_EXTEND:
5740 case ISD::FP_ROUND: {
5741 if (SNaN)
5742 return true;
5743 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5744 }
5745 case ISD::SINT_TO_FP:
5746 case ISD::UINT_TO_FP:
5747 return true;
5748 case ISD::FSQRT: // Need is known positive
5749 case ISD::FLOG:
5750 case ISD::FLOG2:
5751 case ISD::FLOG10:
5752 case ISD::FPOWI:
5753 case ISD::FPOW: {
5754 if (SNaN)
5755 return true;
5756 // TODO: Refine on operand
5757 return false;
5758 }
5759 case ISD::FMINNUM:
5760 case ISD::FMAXNUM:
5761 case ISD::FMINIMUMNUM:
5762 case ISD::FMAXIMUMNUM: {
5763 // Only one needs to be known not-nan, since it will be returned if the
5764 // other ends up being one.
5765 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) ||
5766 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5767 }
5768 case ISD::FMINNUM_IEEE:
5769 case ISD::FMAXNUM_IEEE: {
5770 if (SNaN)
5771 return true;
5772 // This can return a NaN if either operand is an sNaN, or if both operands
5773 // are NaN.
5774 return (isKnownNeverNaN(Op.getOperand(0), false, Depth + 1) &&
5775 isKnownNeverSNaN(Op.getOperand(1), Depth + 1)) ||
5776 (isKnownNeverNaN(Op.getOperand(1), false, Depth + 1) &&
5777 isKnownNeverSNaN(Op.getOperand(0), Depth + 1));
5778 }
5779 case ISD::FMINIMUM:
5780 case ISD::FMAXIMUM: {
5781 // TODO: Does this quiet or return the origina NaN as-is?
5782 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) &&
5783 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5784 }
5786 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5787 }
5788 case ISD::BUILD_VECTOR: {
5789 for (const SDValue &Opnd : Op->ops())
5790 if (!isKnownNeverNaN(Opnd, SNaN, Depth + 1))
5791 return false;
5792 return true;
5793 }
5794 default:
5795 if (Opcode >= ISD::BUILTIN_OP_END ||
5796 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5797 Opcode == ISD::INTRINSIC_W_CHAIN ||
5798 Opcode == ISD::INTRINSIC_VOID) {
5799 return TLI->isKnownNeverNaNForTargetNode(Op, *this, SNaN, Depth);
5800 }
5801
5802 return false;
5803 }
5804}
5805
5807 assert(Op.getValueType().isFloatingPoint() &&
5808 "Floating point type expected");
5809
5810 // If the value is a constant, we can obviously see if it is a zero or not.
5812 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
5813}
5814
5816 if (Depth >= MaxRecursionDepth)
5817 return false; // Limit search depth.
5818
5819 assert(!Op.getValueType().isFloatingPoint() &&
5820 "Floating point types unsupported - use isKnownNeverZeroFloat");
5821
5822 // If the value is a constant, we can obviously see if it is a zero or not.
5824 [](ConstantSDNode *C) { return !C->isZero(); }))
5825 return true;
5826
5827 // TODO: Recognize more cases here. Most of the cases are also incomplete to
5828 // some degree.
5829 switch (Op.getOpcode()) {
5830 default:
5831 break;
5832
5833 case ISD::OR:
5834 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5835 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5836
5837 case ISD::VSELECT:
5838 case ISD::SELECT:
5839 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5840 isKnownNeverZero(Op.getOperand(2), Depth + 1);
5841
5842 case ISD::SHL: {
5843 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5844 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5845 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5846 // 1 << X is never zero.
5847 if (ValKnown.One[0])
5848 return true;
5849 // If max shift cnt of known ones is non-zero, result is non-zero.
5850 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5851 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5852 !ValKnown.One.shl(MaxCnt).isZero())
5853 return true;
5854 break;
5855 }
5856 case ISD::UADDSAT:
5857 case ISD::UMAX:
5858 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5859 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5860
5861 // For smin/smax: If either operand is known negative/positive
5862 // respectively we don't need the other to be known at all.
5863 case ISD::SMAX: {
5864 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5865 if (Op1.isStrictlyPositive())
5866 return true;
5867
5868 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5869 if (Op0.isStrictlyPositive())
5870 return true;
5871
5872 if (Op1.isNonZero() && Op0.isNonZero())
5873 return true;
5874
5875 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5876 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5877 }
5878 case ISD::SMIN: {
5879 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5880 if (Op1.isNegative())
5881 return true;
5882
5883 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5884 if (Op0.isNegative())
5885 return true;
5886
5887 if (Op1.isNonZero() && Op0.isNonZero())
5888 return true;
5889
5890 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5891 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5892 }
5893 case ISD::UMIN:
5894 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5895 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5896
5897 case ISD::ROTL:
5898 case ISD::ROTR:
5899 case ISD::BITREVERSE:
5900 case ISD::BSWAP:
5901 case ISD::CTPOP:
5902 case ISD::ABS:
5903 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5904
5905 case ISD::SRA:
5906 case ISD::SRL: {
5907 if (Op->getFlags().hasExact())
5908 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5909 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5910 if (ValKnown.isNegative())
5911 return true;
5912 // If max shift cnt of known ones is non-zero, result is non-zero.
5913 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5914 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5915 !ValKnown.One.lshr(MaxCnt).isZero())
5916 return true;
5917 break;
5918 }
5919 case ISD::UDIV:
5920 case ISD::SDIV:
5921 // div exact can only produce a zero if the dividend is zero.
5922 // TODO: For udiv this is also true if Op1 u<= Op0
5923 if (Op->getFlags().hasExact())
5924 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5925 break;
5926
5927 case ISD::ADD:
5928 if (Op->getFlags().hasNoUnsignedWrap())
5929 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5930 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5931 return true;
5932 // TODO: There are a lot more cases we can prove for add.
5933 break;
5934
5935 case ISD::SUB: {
5936 if (isNullConstant(Op.getOperand(0)))
5937 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
5938
5939 std::optional<bool> ne =
5940 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
5941 computeKnownBits(Op.getOperand(1), Depth + 1));
5942 return ne && *ne;
5943 }
5944
5945 case ISD::MUL:
5946 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5947 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5948 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5949 return true;
5950 break;
5951
5952 case ISD::ZERO_EXTEND:
5953 case ISD::SIGN_EXTEND:
5954 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5955 case ISD::VSCALE: {
5957 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
5958 ConstantRange CR =
5959 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
5960 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
5961 return true;
5962 break;
5963 }
5964 }
5965
5967}
5968
5970 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
5971 return !C1->isNegative();
5972
5973 return Op.getOpcode() == ISD::FABS;
5974}
5975
5977 // Check the obvious case.
5978 if (A == B) return true;
5979
5980 // For negative and positive zero.
5981 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
5982 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
5983 if (CA->isZero() && CB->isZero()) return true;
5984
5985 // Otherwise they may not be equal.
5986 return false;
5987}
5988
5989// Only bits set in Mask must be negated, other bits may be arbitrary.
5991 if (isBitwiseNot(V, AllowUndefs))
5992 return V.getOperand(0);
5993
5994 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
5995 // bits in the non-extended part.
5996 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
5997 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
5998 return SDValue();
5999 SDValue ExtArg = V.getOperand(0);
6000 if (ExtArg.getScalarValueSizeInBits() >=
6001 MaskC->getAPIntValue().getActiveBits() &&
6002 isBitwiseNot(ExtArg, AllowUndefs) &&
6003 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6004 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6005 return ExtArg.getOperand(0).getOperand(0);
6006 return SDValue();
6007}
6008
6010 // Match masked merge pattern (X & ~M) op (Y & M)
6011 // Including degenerate case (X & ~M) op M
6012 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6013 SDValue Other) {
6014 if (SDValue NotOperand =
6015 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6016 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6017 NotOperand->getOpcode() == ISD::TRUNCATE)
6018 NotOperand = NotOperand->getOperand(0);
6019
6020 if (Other == NotOperand)
6021 return true;
6022 if (Other->getOpcode() == ISD::AND)
6023 return NotOperand == Other->getOperand(0) ||
6024 NotOperand == Other->getOperand(1);
6025 }
6026 return false;
6027 };
6028
6029 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6030 A = A->getOperand(0);
6031
6032 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6033 B = B->getOperand(0);
6034
6035 if (A->getOpcode() == ISD::AND)
6036 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6037 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6038 return false;
6039}
6040
6041// FIXME: unify with llvm::haveNoCommonBitsSet.
6043 assert(A.getValueType() == B.getValueType() &&
6044 "Values must have the same type");
6047 return true;
6050}
6051
6052static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6053 SelectionDAG &DAG) {
6054 if (cast<ConstantSDNode>(Step)->isZero())
6055 return DAG.getConstant(0, DL, VT);
6056
6057 return SDValue();
6058}
6059
6062 SelectionDAG &DAG) {
6063 int NumOps = Ops.size();
6064 assert(NumOps != 0 && "Can't build an empty vector!");
6065 assert(!VT.isScalableVector() &&
6066 "BUILD_VECTOR cannot be used with scalable types");
6067 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6068 "Incorrect element count in BUILD_VECTOR!");
6069
6070 // BUILD_VECTOR of UNDEFs is UNDEF.
6071 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6072 return DAG.getUNDEF(VT);
6073
6074 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6075 SDValue IdentitySrc;
6076 bool IsIdentity = true;
6077 for (int i = 0; i != NumOps; ++i) {
6078 if (Ops[i].getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
6079 Ops[i].getOperand(0).getValueType() != VT ||
6080 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6081 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6082 Ops[i].getConstantOperandAPInt(1) != i) {
6083 IsIdentity = false;
6084 break;
6085 }
6086 IdentitySrc = Ops[i].getOperand(0);
6087 }
6088 if (IsIdentity)
6089 return IdentitySrc;
6090
6091 return SDValue();
6092}
6093
6094/// Try to simplify vector concatenation to an input value, undef, or build
6095/// vector.
6098 SelectionDAG &DAG) {
6099 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6100 assert(llvm::all_of(Ops,
6101 [Ops](SDValue Op) {
6102 return Ops[0].getValueType() == Op.getValueType();
6103 }) &&
6104 "Concatenation of vectors with inconsistent value types!");
6105 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6106 VT.getVectorElementCount() &&
6107 "Incorrect element count in vector concatenation!");
6108
6109 if (Ops.size() == 1)
6110 return Ops[0];
6111
6112 // Concat of UNDEFs is UNDEF.
6113 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6114 return DAG.getUNDEF(VT);
6115
6116 // Scan the operands and look for extract operations from a single source
6117 // that correspond to insertion at the same location via this concatenation:
6118 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6119 SDValue IdentitySrc;
6120 bool IsIdentity = true;
6121 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6122 SDValue Op = Ops[i];
6123 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6124 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6125 Op.getOperand(0).getValueType() != VT ||
6126 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6127 Op.getConstantOperandVal(1) != IdentityIndex) {
6128 IsIdentity = false;
6129 break;
6130 }
6131 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6132 "Unexpected identity source vector for concat of extracts");
6133 IdentitySrc = Op.getOperand(0);
6134 }
6135 if (IsIdentity) {
6136 assert(IdentitySrc && "Failed to set source vector of extracts");
6137 return IdentitySrc;
6138 }
6139
6140 // The code below this point is only designed to work for fixed width
6141 // vectors, so we bail out for now.
6142 if (VT.isScalableVector())
6143 return SDValue();
6144
6145 // A CONCAT_VECTOR with all UNDEF/BUILD_VECTOR operands can be
6146 // simplified to one big BUILD_VECTOR.
6147 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6148 EVT SVT = VT.getScalarType();
6150 for (SDValue Op : Ops) {
6151 EVT OpVT = Op.getValueType();
6152 if (Op.isUndef())
6153 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6154 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6155 Elts.append(Op->op_begin(), Op->op_end());
6156 else
6157 return SDValue();
6158 }
6159
6160 // BUILD_VECTOR requires all inputs to be of the same type, find the
6161 // maximum type and extend them all.
6162 for (SDValue Op : Elts)
6163 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6164
6165 if (SVT.bitsGT(VT.getScalarType())) {
6166 for (SDValue &Op : Elts) {
6167 if (Op.isUndef())
6168 Op = DAG.getUNDEF(SVT);
6169 else
6170 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6171 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6172 : DAG.getSExtOrTrunc(Op, DL, SVT);
6173 }
6174 }
6175
6176 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6177 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6178 return V;
6179}
6180
6181/// Gets or creates the specified node.
6182SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6183 SDVTList VTs = getVTList(VT);
6185 AddNodeIDNode(ID, Opcode, VTs, {});
6186 void *IP = nullptr;
6187 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6188 return SDValue(E, 0);
6189
6190 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6191 CSEMap.InsertNode(N, IP);
6192
6193 InsertNode(N);
6194 SDValue V = SDValue(N, 0);
6195 NewSDValueDbgMsg(V, "Creating new node: ", this);
6196 return V;
6197}
6198
6199SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6200 SDValue N1) {
6201 SDNodeFlags Flags;
6202 if (Inserter)
6203 Flags = Inserter->getFlags();
6204 return getNode(Opcode, DL, VT, N1, Flags);
6205}
6206
6207SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6208 SDValue N1, const SDNodeFlags Flags) {
6209 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6210
6211 // Constant fold unary operations with a vector integer or float operand.
6212 switch (Opcode) {
6213 default:
6214 // FIXME: Entirely reasonable to perform folding of other unary
6215 // operations here as the need arises.
6216 break;
6217 case ISD::FNEG:
6218 case ISD::FABS:
6219 case ISD::FCEIL:
6220 case ISD::FTRUNC:
6221 case ISD::FFLOOR:
6222 case ISD::FP_EXTEND:
6223 case ISD::FP_TO_SINT:
6224 case ISD::FP_TO_UINT:
6225 case ISD::FP_TO_FP16:
6226 case ISD::FP_TO_BF16:
6227 case ISD::TRUNCATE:
6228 case ISD::ANY_EXTEND:
6229 case ISD::ZERO_EXTEND:
6230 case ISD::SIGN_EXTEND:
6231 case ISD::UINT_TO_FP:
6232 case ISD::SINT_TO_FP:
6233 case ISD::FP16_TO_FP:
6234 case ISD::BF16_TO_FP:
6235 case ISD::BITCAST:
6236 case ISD::ABS:
6237 case ISD::BITREVERSE:
6238 case ISD::BSWAP:
6239 case ISD::CTLZ:
6241 case ISD::CTTZ:
6243 case ISD::CTPOP:
6244 case ISD::STEP_VECTOR: {
6245 SDValue Ops = {N1};
6246 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6247 return Fold;
6248 }
6249 }
6250
6251 unsigned OpOpcode = N1.getNode()->getOpcode();
6252 switch (Opcode) {
6253 case ISD::STEP_VECTOR:
6254 assert(VT.isScalableVector() &&
6255 "STEP_VECTOR can only be used with scalable types");
6256 assert(OpOpcode == ISD::TargetConstant &&
6257 VT.getVectorElementType() == N1.getValueType() &&
6258 "Unexpected step operand");
6259 break;
6260 case ISD::FREEZE:
6261 assert(VT == N1.getValueType() && "Unexpected VT!");
6262 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly*/ false,
6263 /*Depth*/ 1))
6264 return N1;
6265 break;
6266 case ISD::TokenFactor:
6267 case ISD::MERGE_VALUES:
6269 return N1; // Factor, merge or concat of one node? No need.
6270 case ISD::BUILD_VECTOR: {
6271 // Attempt to simplify BUILD_VECTOR.
6272 SDValue Ops[] = {N1};
6273 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6274 return V;
6275 break;
6276 }
6277 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6278 case ISD::FP_EXTEND:
6280 "Invalid FP cast!");
6281 if (N1.getValueType() == VT) return N1; // noop conversion.
6282 assert((!VT.isVector() || VT.getVectorElementCount() ==
6284 "Vector element count mismatch!");
6285 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6286 if (N1.isUndef())
6287 return getUNDEF(VT);
6288 break;
6289 case ISD::FP_TO_SINT:
6290 case ISD::FP_TO_UINT:
6291 if (N1.isUndef())
6292 return getUNDEF(VT);
6293 break;
6294 case ISD::SINT_TO_FP:
6295 case ISD::UINT_TO_FP:
6296 // [us]itofp(undef) = 0, because the result value is bounded.
6297 if (N1.isUndef())
6298 return getConstantFP(0.0, DL, VT);
6299 break;
6300 case ISD::SIGN_EXTEND:
6301 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6302 "Invalid SIGN_EXTEND!");
6303 assert(VT.isVector() == N1.getValueType().isVector() &&
6304 "SIGN_EXTEND result type type should be vector iff the operand "
6305 "type is vector!");
6306 if (N1.getValueType() == VT) return N1; // noop extension
6307 assert((!VT.isVector() || VT.getVectorElementCount() ==
6309 "Vector element count mismatch!");
6310 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6311 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6312 SDNodeFlags Flags;
6313 if (OpOpcode == ISD::ZERO_EXTEND)
6314 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6315 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6316 }
6317 if (OpOpcode == ISD::UNDEF)
6318 // sext(undef) = 0, because the top bits will all be the same.
6319 return getConstant(0, DL, VT);
6320 break;
6321 case ISD::ZERO_EXTEND:
6322 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6323 "Invalid ZERO_EXTEND!");
6324 assert(VT.isVector() == N1.getValueType().isVector() &&
6325 "ZERO_EXTEND result type type should be vector iff the operand "
6326 "type is vector!");
6327 if (N1.getValueType() == VT) return N1; // noop extension
6328 assert((!VT.isVector() || VT.getVectorElementCount() ==
6330 "Vector element count mismatch!");
6331 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6332 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6333 SDNodeFlags Flags;
6334 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6335 return getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6336 }
6337 if (OpOpcode == ISD::UNDEF)
6338 // zext(undef) = 0, because the top bits will be zero.
6339 return getConstant(0, DL, VT);
6340
6341 // Skip unnecessary zext_inreg pattern:
6342 // (zext (trunc x)) -> x iff the upper bits are known zero.
6343 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6344 // use to recognise zext_inreg patterns.
6345 if (OpOpcode == ISD::TRUNCATE) {
6346 SDValue OpOp = N1.getOperand(0);
6347 if (OpOp.getValueType() == VT) {
6348 if (OpOp.getOpcode() != ISD::AND) {
6351 if (MaskedValueIsZero(OpOp, HiBits)) {
6352 transferDbgValues(N1, OpOp);
6353 return OpOp;
6354 }
6355 }
6356 }
6357 }
6358 break;
6359 case ISD::ANY_EXTEND:
6360 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6361 "Invalid ANY_EXTEND!");
6362 assert(VT.isVector() == N1.getValueType().isVector() &&
6363 "ANY_EXTEND result type type should be vector iff the operand "
6364 "type is vector!");
6365 if (N1.getValueType() == VT) return N1; // noop extension
6366 assert((!VT.isVector() || VT.getVectorElementCount() ==
6368 "Vector element count mismatch!");
6369 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6370
6371 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6372 OpOpcode == ISD::ANY_EXTEND) {
6373 SDNodeFlags Flags;
6374 if (OpOpcode == ISD::ZERO_EXTEND)
6375 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6376 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6377 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6378 }
6379 if (OpOpcode == ISD::UNDEF)
6380 return getUNDEF(VT);
6381
6382 // (ext (trunc x)) -> x
6383 if (OpOpcode == ISD::TRUNCATE) {
6384 SDValue OpOp = N1.getOperand(0);
6385 if (OpOp.getValueType() == VT) {
6386 transferDbgValues(N1, OpOp);
6387 return OpOp;
6388 }
6389 }
6390 break;
6391 case ISD::TRUNCATE:
6392 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6393 "Invalid TRUNCATE!");
6394 assert(VT.isVector() == N1.getValueType().isVector() &&
6395 "TRUNCATE result type type should be vector iff the operand "
6396 "type is vector!");
6397 if (N1.getValueType() == VT) return N1; // noop truncate
6398 assert((!VT.isVector() || VT.getVectorElementCount() ==
6400 "Vector element count mismatch!");
6401 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6402 if (OpOpcode == ISD::TRUNCATE)
6403 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6404 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6405 OpOpcode == ISD::ANY_EXTEND) {
6406 // If the source is smaller than the dest, we still need an extend.
6408 VT.getScalarType()))
6409 return getNode(OpOpcode, DL, VT, N1.getOperand(0));
6410 if (N1.getOperand(0).getValueType().bitsGT(VT))
6411 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6412 return N1.getOperand(0);
6413 }
6414 if (OpOpcode == ISD::UNDEF)
6415 return getUNDEF(VT);
6416 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6417 return getVScale(DL, VT,
6419 break;
6423 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6424 assert(N1.getValueType().bitsLE(VT) &&
6425 "The input must be the same size or smaller than the result.");
6428 "The destination vector type must have fewer lanes than the input.");
6429 break;
6430 case ISD::ABS:
6431 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6432 if (OpOpcode == ISD::UNDEF)
6433 return getConstant(0, DL, VT);
6434 break;
6435 case ISD::BSWAP:
6436 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6437 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6438 "BSWAP types must be a multiple of 16 bits!");
6439 if (OpOpcode == ISD::UNDEF)
6440 return getUNDEF(VT);
6441 // bswap(bswap(X)) -> X.
6442 if (OpOpcode == ISD::BSWAP)
6443 return N1.getOperand(0);
6444 break;
6445 case ISD::BITREVERSE:
6446 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6447 if (OpOpcode == ISD::UNDEF)
6448 return getUNDEF(VT);
6449 break;
6450 case ISD::BITCAST:
6452 "Cannot BITCAST between types of different sizes!");
6453 if (VT == N1.getValueType()) return N1; // noop conversion.
6454 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6455 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6456 if (OpOpcode == ISD::UNDEF)
6457 return getUNDEF(VT);
6458 break;
6460 assert(VT.isVector() && !N1.getValueType().isVector() &&
6461 (VT.getVectorElementType() == N1.getValueType() ||
6463 N1.getValueType().isInteger() &&
6465 "Illegal SCALAR_TO_VECTOR node!");
6466 if (OpOpcode == ISD::UNDEF)
6467 return getUNDEF(VT);
6468 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6469 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6470 isa<ConstantSDNode>(N1.getOperand(1)) &&
6471 N1.getConstantOperandVal(1) == 0 &&
6472 N1.getOperand(0).getValueType() == VT)
6473 return N1.getOperand(0);
6474 break;
6475 case ISD::FNEG:
6476 // Negation of an unknown bag of bits is still completely undefined.
6477 if (OpOpcode == ISD::UNDEF)
6478 return getUNDEF(VT);
6479
6480 if (OpOpcode == ISD::FNEG) // --X -> X
6481 return N1.getOperand(0);
6482 break;
6483 case ISD::FABS:
6484 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6485 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6486 break;
6487 case ISD::VSCALE:
6488 assert(VT == N1.getValueType() && "Unexpected VT!");
6489 break;
6490 case ISD::CTPOP:
6491 if (N1.getValueType().getScalarType() == MVT::i1)
6492 return N1;
6493 break;
6494 case ISD::CTLZ:
6495 case ISD::CTTZ:
6496 if (N1.getValueType().getScalarType() == MVT::i1)
6497 return getNOT(DL, N1, N1.getValueType());
6498 break;
6499 case ISD::VECREDUCE_ADD:
6500 if (N1.getValueType().getScalarType() == MVT::i1)
6501 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6502 break;
6505 if (N1.getValueType().getScalarType() == MVT::i1)
6506 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6507 break;
6510 if (N1.getValueType().getScalarType() == MVT::i1)
6511 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6512 break;
6513 case ISD::SPLAT_VECTOR:
6514 assert(VT.isVector() && "Wrong return type!");
6515 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6516 // that for now.
6518 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6520 N1.getValueType().isInteger() &&
6522 "Wrong operand type!");
6523 break;
6524 }
6525
6526 SDNode *N;
6527 SDVTList VTs = getVTList(VT);
6528 SDValue Ops[] = {N1};
6529 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6531 AddNodeIDNode(ID, Opcode, VTs, Ops);
6532 void *IP = nullptr;
6533 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6534 E->intersectFlagsWith(Flags);
6535 return SDValue(E, 0);
6536 }
6537
6538 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6539 N->setFlags(Flags);
6540 createOperands(N, Ops);
6541 CSEMap.InsertNode(N, IP);
6542 } else {
6543 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6544 createOperands(N, Ops);
6545 }
6546
6547 InsertNode(N);
6548 SDValue V = SDValue(N, 0);
6549 NewSDValueDbgMsg(V, "Creating new node: ", this);
6550 return V;
6551}
6552
6553static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6554 const APInt &C2) {
6555 switch (Opcode) {
6556 case ISD::ADD: return C1 + C2;
6557 case ISD::SUB: return C1 - C2;
6558 case ISD::MUL: return C1 * C2;
6559 case ISD::AND: return C1 & C2;
6560 case ISD::OR: return C1 | C2;
6561 case ISD::XOR: return C1 ^ C2;
6562 case ISD::SHL: return C1 << C2;
6563 case ISD::SRL: return C1.lshr(C2);
6564 case ISD::SRA: return C1.ashr(C2);
6565 case ISD::ROTL: return C1.rotl(C2);
6566 case ISD::ROTR: return C1.rotr(C2);
6567 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6568 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6569 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6570 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6571 case ISD::SADDSAT: return C1.sadd_sat(C2);
6572 case ISD::UADDSAT: return C1.uadd_sat(C2);
6573 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6574 case ISD::USUBSAT: return C1.usub_sat(C2);
6575 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6576 case ISD::USHLSAT: return C1.ushl_sat(C2);
6577 case ISD::UDIV:
6578 if (!C2.getBoolValue())
6579 break;
6580 return C1.udiv(C2);
6581 case ISD::UREM:
6582 if (!C2.getBoolValue())
6583 break;
6584 return C1.urem(C2);
6585 case ISD::SDIV:
6586 if (!C2.getBoolValue())
6587 break;
6588 return C1.sdiv(C2);
6589 case ISD::SREM:
6590 if (!C2.getBoolValue())
6591 break;
6592 return C1.srem(C2);
6593 case ISD::AVGFLOORS:
6594 return APIntOps::avgFloorS(C1, C2);
6595 case ISD::AVGFLOORU:
6596 return APIntOps::avgFloorU(C1, C2);
6597 case ISD::AVGCEILS:
6598 return APIntOps::avgCeilS(C1, C2);
6599 case ISD::AVGCEILU:
6600 return APIntOps::avgCeilU(C1, C2);
6601 case ISD::ABDS:
6602 return APIntOps::abds(C1, C2);
6603 case ISD::ABDU:
6604 return APIntOps::abdu(C1, C2);
6605 case ISD::MULHS:
6606 return APIntOps::mulhs(C1, C2);
6607 case ISD::MULHU:
6608 return APIntOps::mulhu(C1, C2);
6609 }
6610 return std::nullopt;
6611}
6612// Handle constant folding with UNDEF.
6613// TODO: Handle more cases.
6614static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6615 bool IsUndef1, const APInt &C2,
6616 bool IsUndef2) {
6617 if (!(IsUndef1 || IsUndef2))
6618 return FoldValue(Opcode, C1, C2);
6619
6620 // Fold and(x, undef) -> 0
6621 // Fold mul(x, undef) -> 0
6622 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6623 return APInt::getZero(C1.getBitWidth());
6624
6625 return std::nullopt;
6626}
6627
6629 const GlobalAddressSDNode *GA,
6630 const SDNode *N2) {
6631 if (GA->getOpcode() != ISD::GlobalAddress)
6632 return SDValue();
6633 if (!TLI->isOffsetFoldingLegal(GA))
6634 return SDValue();
6635 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6636 if (!C2)
6637 return SDValue();
6638 int64_t Offset = C2->getSExtValue();
6639 switch (Opcode) {
6640 case ISD::ADD: break;
6641 case ISD::SUB: Offset = -uint64_t(Offset); break;
6642 default: return SDValue();
6643 }
6644 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6645 GA->getOffset() + uint64_t(Offset));
6646}
6647
6648bool SelectionDAG::isUndef(unsigned Opcode, ArrayRef<SDValue> Ops) {
6649 switch (Opcode) {
6650 case ISD::SDIV:
6651 case ISD::UDIV:
6652 case ISD::SREM:
6653 case ISD::UREM: {
6654 // If a divisor is zero/undef or any element of a divisor vector is
6655 // zero/undef, the whole op is undef.
6656 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6657 SDValue Divisor = Ops[1];
6658 if (Divisor.isUndef() || isNullConstant(Divisor))
6659 return true;
6660
6661 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6662 llvm::any_of(Divisor->op_values(),
6663 [](SDValue V) { return V.isUndef() ||
6664 isNullConstant(V); });
6665 // TODO: Handle signed overflow.
6666 }
6667 // TODO: Handle oversized shifts.
6668 default:
6669 return false;
6670 }
6671}
6672
6674 EVT VT, ArrayRef<SDValue> Ops,
6675 SDNodeFlags Flags) {
6676 // If the opcode is a target-specific ISD node, there's nothing we can
6677 // do here and the operand rules may not line up with the below, so
6678 // bail early.
6679 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6680 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6681 // foldCONCAT_VECTORS in getNode before this is called.
6682 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6683 return SDValue();
6684
6685 unsigned NumOps = Ops.size();
6686 if (NumOps == 0)
6687 return SDValue();
6688
6689 if (isUndef(Opcode, Ops))
6690 return getUNDEF(VT);
6691
6692 // Handle unary special cases.
6693 if (NumOps == 1) {
6694 SDValue N1 = Ops[0];
6695
6696 // Constant fold unary operations with an integer constant operand. Even
6697 // opaque constant will be folded, because the folding of unary operations
6698 // doesn't create new constants with different values. Nevertheless, the
6699 // opaque flag is preserved during folding to prevent future folding with
6700 // other constants.
6701 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6702 const APInt &Val = C->getAPIntValue();
6703 switch (Opcode) {
6704 case ISD::SIGN_EXTEND:
6705 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6706 C->isTargetOpcode(), C->isOpaque());
6707 case ISD::TRUNCATE:
6708 if (C->isOpaque())
6709 break;
6710 [[fallthrough]];
6711 case ISD::ZERO_EXTEND:
6712 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6713 C->isTargetOpcode(), C->isOpaque());
6714 case ISD::ANY_EXTEND:
6715 // Some targets like RISCV prefer to sign extend some types.
6716 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
6717 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6718 C->isTargetOpcode(), C->isOpaque());
6719 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6720 C->isTargetOpcode(), C->isOpaque());
6721 case ISD::ABS:
6722 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
6723 C->isOpaque());
6724 case ISD::BITREVERSE:
6725 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
6726 C->isOpaque());
6727 case ISD::BSWAP:
6728 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
6729 C->isOpaque());
6730 case ISD::CTPOP:
6731 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
6732 C->isOpaque());
6733 case ISD::CTLZ:
6735 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
6736 C->isOpaque());
6737 case ISD::CTTZ:
6739 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
6740 C->isOpaque());
6741 case ISD::UINT_TO_FP:
6742 case ISD::SINT_TO_FP: {
6744 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
6746 return getConstantFP(FPV, DL, VT);
6747 }
6748 case ISD::FP16_TO_FP:
6749 case ISD::BF16_TO_FP: {
6750 bool Ignored;
6751 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
6752 : APFloat::BFloat(),
6753 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
6754
6755 // This can return overflow, underflow, or inexact; we don't care.
6756 // FIXME need to be more flexible about rounding mode.
6758 &Ignored);
6759 return getConstantFP(FPV, DL, VT);
6760 }
6761 case ISD::STEP_VECTOR:
6762 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
6763 return V;
6764 break;
6765 case ISD::BITCAST:
6766 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
6767 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
6768 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
6769 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
6770 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
6771 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
6772 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
6773 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
6774 break;
6775 }
6776 }
6777
6778 // Constant fold unary operations with a floating point constant operand.
6779 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
6780 APFloat V = C->getValueAPF(); // make copy
6781 switch (Opcode) {
6782 case ISD::FNEG:
6783 V.changeSign();
6784 return getConstantFP(V, DL, VT);
6785 case ISD::FABS:
6786 V.clearSign();
6787 return getConstantFP(V, DL, VT);
6788 case ISD::FCEIL: {
6789 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
6790 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6791 return getConstantFP(V, DL, VT);
6792 return SDValue();
6793 }
6794 case ISD::FTRUNC: {
6795 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
6796 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6797 return getConstantFP(V, DL, VT);
6798 return SDValue();
6799 }
6800 case ISD::FFLOOR: {
6801 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
6802 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6803 return getConstantFP(V, DL, VT);
6804 return SDValue();
6805 }
6806 case ISD::FP_EXTEND: {
6807 bool ignored;
6808 // This can return overflow, underflow, or inexact; we don't care.
6809 // FIXME need to be more flexible about rounding mode.
6810 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
6811 &ignored);
6812 return getConstantFP(V, DL, VT);
6813 }
6814 case ISD::FP_TO_SINT:
6815 case ISD::FP_TO_UINT: {
6816 bool ignored;
6817 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
6818 // FIXME need to be more flexible about rounding mode.
6820 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
6821 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
6822 break;
6823 return getConstant(IntVal, DL, VT);
6824 }
6825 case ISD::FP_TO_FP16:
6826 case ISD::FP_TO_BF16: {
6827 bool Ignored;
6828 // This can return overflow, underflow, or inexact; we don't care.
6829 // FIXME need to be more flexible about rounding mode.
6830 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
6831 : APFloat::BFloat(),
6833 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6834 }
6835 case ISD::BITCAST:
6836 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
6837 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6838 VT);
6839 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
6840 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6841 VT);
6842 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
6843 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
6844 VT);
6845 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
6846 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6847 break;
6848 }
6849 }
6850
6851 // Early-out if we failed to constant fold a bitcast.
6852 if (Opcode == ISD::BITCAST)
6853 return SDValue();
6854 }
6855
6856 // Handle binops special cases.
6857 if (NumOps == 2) {
6858 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
6859 return CFP;
6860
6861 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
6862 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
6863 if (C1->isOpaque() || C2->isOpaque())
6864 return SDValue();
6865
6866 std::optional<APInt> FoldAttempt =
6867 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
6868 if (!FoldAttempt)
6869 return SDValue();
6870
6871 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
6872 assert((!Folded || !VT.isVector()) &&
6873 "Can't fold vectors ops with scalar operands");
6874 return Folded;
6875 }
6876 }
6877
6878 // fold (add Sym, c) -> Sym+c
6879 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[0]))
6880 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
6881 if (TLI->isCommutativeBinOp(Opcode))
6882 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[1]))
6883 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
6884
6885 // fold (sext_in_reg c1) -> c2
6886 if (Opcode == ISD::SIGN_EXTEND_INREG) {
6887 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
6888
6889 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
6890 unsigned FromBits = EVT.getScalarSizeInBits();
6891 Val <<= Val.getBitWidth() - FromBits;
6892 Val.ashrInPlace(Val.getBitWidth() - FromBits);
6893 return getConstant(Val, DL, ConstantVT);
6894 };
6895
6896 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
6897 const APInt &Val = C1->getAPIntValue();
6898 return SignExtendInReg(Val, VT);
6899 }
6900
6902 SmallVector<SDValue, 8> ScalarOps;
6903 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
6904 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
6905 SDValue Op = Ops[0].getOperand(I);
6906 if (Op.isUndef()) {
6907 ScalarOps.push_back(getUNDEF(OpVT));
6908 continue;
6909 }
6910 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
6911 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
6912 }
6913 return getBuildVector(VT, DL, ScalarOps);
6914 }
6915
6916 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
6917 isa<ConstantSDNode>(Ops[0].getOperand(0)))
6918 return getNode(ISD::SPLAT_VECTOR, DL, VT,
6919 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
6920 Ops[0].getOperand(0).getValueType()));
6921 }
6922 }
6923
6924 // This is for vector folding only from here on.
6925 if (!VT.isVector())
6926 return SDValue();
6927
6928 ElementCount NumElts = VT.getVectorElementCount();
6929
6930 // See if we can fold through any bitcasted integer ops.
6931 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
6932 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
6933 (Ops[0].getOpcode() == ISD::BITCAST ||
6934 Ops[1].getOpcode() == ISD::BITCAST)) {
6935 SDValue N1 = peekThroughBitcasts(Ops[0]);
6936 SDValue N2 = peekThroughBitcasts(Ops[1]);
6937 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
6938 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
6939 if (BV1 && BV2 && N1.getValueType().isInteger() &&
6940 N2.getValueType().isInteger()) {
6941 bool IsLE = getDataLayout().isLittleEndian();
6942 unsigned EltBits = VT.getScalarSizeInBits();
6943 SmallVector<APInt> RawBits1, RawBits2;
6944 BitVector UndefElts1, UndefElts2;
6945 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
6946 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
6947 SmallVector<APInt> RawBits;
6948 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
6949 std::optional<APInt> Fold = FoldValueWithUndef(
6950 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
6951 if (!Fold)
6952 break;
6953 RawBits.push_back(*Fold);
6954 }
6955 if (RawBits.size() == NumElts.getFixedValue()) {
6956 // We have constant folded, but we might need to cast this again back
6957 // to the original (possibly legalized) type.
6958 EVT BVVT, BVEltVT;
6959 if (N1.getValueType() == VT) {
6960 BVVT = N1.getValueType();
6961 BVEltVT = BV1->getOperand(0).getValueType();
6962 } else {
6963 BVVT = N2.getValueType();
6964 BVEltVT = BV2->getOperand(0).getValueType();
6965 }
6966 unsigned BVEltBits = BVEltVT.getSizeInBits();
6967 SmallVector<APInt> DstBits;
6968 BitVector DstUndefs;
6970 DstBits, RawBits, DstUndefs,
6971 BitVector(RawBits.size(), false));
6972 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
6973 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
6974 if (DstUndefs[I])
6975 continue;
6976 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
6977 }
6978 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
6979 }
6980 }
6981 }
6982 }
6983
6984 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
6985 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
6986 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
6987 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
6988 APInt RHSVal;
6989 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
6990 APInt NewStep = Opcode == ISD::MUL
6991 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
6992 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
6993 return getStepVector(DL, VT, NewStep);
6994 }
6995 }
6996
6997 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
6998 return !Op.getValueType().isVector() ||
6999 Op.getValueType().getVectorElementCount() == NumElts;
7000 };
7001
7002 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7003 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7004 Op.getOpcode() == ISD::BUILD_VECTOR ||
7005 Op.getOpcode() == ISD::SPLAT_VECTOR;
7006 };
7007
7008 // All operands must be vector types with the same number of elements as
7009 // the result type and must be either UNDEF or a build/splat vector
7010 // or UNDEF scalars.
7011 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7012 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7013 return SDValue();
7014
7015 // If we are comparing vectors, then the result needs to be a i1 boolean that
7016 // is then extended back to the legal result type depending on how booleans
7017 // are represented.
7018 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7019 ISD::NodeType ExtendCode =
7020 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7023
7024 // Find legal integer scalar type for constant promotion and
7025 // ensure that its scalar size is at least as large as source.
7026 EVT LegalSVT = VT.getScalarType();
7027 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7028 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7029 if (LegalSVT.bitsLT(VT.getScalarType()))
7030 return SDValue();
7031 }
7032
7033 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7034 // only have one operand to check. For fixed-length vector types we may have
7035 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7036 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7037
7038 // Constant fold each scalar lane separately.
7039 SmallVector<SDValue, 4> ScalarResults;
7040 for (unsigned I = 0; I != NumVectorElts; I++) {
7041 SmallVector<SDValue, 4> ScalarOps;
7042 for (SDValue Op : Ops) {
7043 EVT InSVT = Op.getValueType().getScalarType();
7044 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7045 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7046 if (Op.isUndef())
7047 ScalarOps.push_back(getUNDEF(InSVT));
7048 else
7049 ScalarOps.push_back(Op);
7050 continue;
7051 }
7052
7053 SDValue ScalarOp =
7054 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7055 EVT ScalarVT = ScalarOp.getValueType();
7056
7057 // Build vector (integer) scalar operands may need implicit
7058 // truncation - do this before constant folding.
7059 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7060 // Don't create illegally-typed nodes unless they're constants or undef
7061 // - if we fail to constant fold we can't guarantee the (dead) nodes
7062 // we're creating will be cleaned up before being visited for
7063 // legalization.
7064 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7065 !isa<ConstantSDNode>(ScalarOp) &&
7066 TLI->getTypeAction(*getContext(), InSVT) !=
7068 return SDValue();
7069 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7070 }
7071
7072 ScalarOps.push_back(ScalarOp);
7073 }
7074
7075 // Constant fold the scalar operands.
7076 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7077
7078 // Scalar folding only succeeded if the result is a constant or UNDEF.
7079 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7080 ScalarResult.getOpcode() != ISD::ConstantFP)
7081 return SDValue();
7082
7083 // Legalize the (integer) scalar constant if necessary. We only do
7084 // this once we know the folding succeeded, since otherwise we would
7085 // get a node with illegal type which has a user.
7086 if (LegalSVT != SVT)
7087 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7088
7089 ScalarResults.push_back(ScalarResult);
7090 }
7091
7092 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7093 : getBuildVector(VT, DL, ScalarResults);
7094 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7095 return V;
7096}
7097
7099 EVT VT, ArrayRef<SDValue> Ops) {
7100 // TODO: Add support for unary/ternary fp opcodes.
7101 if (Ops.size() != 2)
7102 return SDValue();
7103
7104 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7105 // should. That will require dealing with a potentially non-default
7106 // rounding mode, checking the "opStatus" return value from the APFloat
7107 // math calculations, and possibly other variations.
7108 SDValue N1 = Ops[0];
7109 SDValue N2 = Ops[1];
7110 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7111 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7112 if (N1CFP && N2CFP) {
7113 APFloat C1 = N1CFP->getValueAPF(); // make copy
7114 const APFloat &C2 = N2CFP->getValueAPF();
7115 switch (Opcode) {
7116 case ISD::FADD:
7118 return getConstantFP(C1, DL, VT);
7119 case ISD::FSUB:
7121 return getConstantFP(C1, DL, VT);
7122 case ISD::FMUL:
7124 return getConstantFP(C1, DL, VT);
7125 case ISD::FDIV:
7127 return getConstantFP(C1, DL, VT);
7128 case ISD::FREM:
7129 C1.mod(C2);
7130 return getConstantFP(C1, DL, VT);
7131 case ISD::FCOPYSIGN:
7132 C1.copySign(C2);
7133 return getConstantFP(C1, DL, VT);
7134 case ISD::FMINNUM:
7135 return getConstantFP(minnum(C1, C2), DL, VT);
7136 case ISD::FMAXNUM:
7137 return getConstantFP(maxnum(C1, C2), DL, VT);
7138 case ISD::FMINIMUM:
7139 return getConstantFP(minimum(C1, C2), DL, VT);
7140 case ISD::FMAXIMUM:
7141 return getConstantFP(maximum(C1, C2), DL, VT);
7142 case ISD::FMINIMUMNUM:
7143 return getConstantFP(minimumnum(C1, C2), DL, VT);
7144 case ISD::FMAXIMUMNUM:
7145 return getConstantFP(maximumnum(C1, C2), DL, VT);
7146 default: break;
7147 }
7148 }
7149 if (N1CFP && Opcode == ISD::FP_ROUND) {
7150 APFloat C1 = N1CFP->getValueAPF(); // make copy
7151 bool Unused;
7152 // This can return overflow, underflow, or inexact; we don't care.
7153 // FIXME need to be more flexible about rounding mode.
7155 &Unused);
7156 return getConstantFP(C1, DL, VT);
7157 }
7158
7159 switch (Opcode) {
7160 case ISD::FSUB:
7161 // -0.0 - undef --> undef (consistent with "fneg undef")
7162 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7163 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7164 return getUNDEF(VT);
7165 [[fallthrough]];
7166
7167 case ISD::FADD:
7168 case ISD::FMUL:
7169 case ISD::FDIV:
7170 case ISD::FREM:
7171 // If both operands are undef, the result is undef. If 1 operand is undef,
7172 // the result is NaN. This should match the behavior of the IR optimizer.
7173 if (N1.isUndef() && N2.isUndef())
7174 return getUNDEF(VT);
7175 if (N1.isUndef() || N2.isUndef())
7177 }
7178 return SDValue();
7179}
7180
7182 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7183
7184 // There's no need to assert on a byte-aligned pointer. All pointers are at
7185 // least byte aligned.
7186 if (A == Align(1))
7187 return Val;
7188
7189 SDVTList VTs = getVTList(Val.getValueType());
7191 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7192 ID.AddInteger(A.value());
7193
7194 void *IP = nullptr;
7195 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7196 return SDValue(E, 0);
7197
7198 auto *N =
7199 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7200 createOperands(N, {Val});
7201
7202 CSEMap.InsertNode(N, IP);
7203 InsertNode(N);
7204
7205 SDValue V(N, 0);
7206 NewSDValueDbgMsg(V, "Creating new node: ", this);
7207 return V;
7208}
7209
7210SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7211 SDValue N1, SDValue N2) {
7212 SDNodeFlags Flags;
7213 if (Inserter)
7214 Flags = Inserter->getFlags();
7215 return getNode(Opcode, DL, VT, N1, N2, Flags);
7216}
7217
7219 SDValue &N2) const {
7220 if (!TLI->isCommutativeBinOp(Opcode))
7221 return;
7222
7223 // Canonicalize:
7224 // binop(const, nonconst) -> binop(nonconst, const)
7227 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7228 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7229 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7230 std::swap(N1, N2);
7231
7232 // Canonicalize:
7233 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7234 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7236 std::swap(N1, N2);
7237}
7238
7239SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7240 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7242 N2.getOpcode() != ISD::DELETED_NODE &&
7243 "Operand is DELETED_NODE!");
7244
7245 canonicalizeCommutativeBinop(Opcode, N1, N2);
7246
7247 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7248 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7249
7250 // Don't allow undefs in vector splats - we might be returning N2 when folding
7251 // to zero etc.
7252 ConstantSDNode *N2CV =
7253 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7254
7255 switch (Opcode) {
7256 default: break;
7257 case ISD::TokenFactor:
7258 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7259 N2.getValueType() == MVT::Other && "Invalid token factor!");
7260 // Fold trivial token factors.
7261 if (N1.getOpcode() == ISD::EntryToken) return N2;
7262 if (N2.getOpcode() == ISD::EntryToken) return N1;
7263 if (N1 == N2) return N1;
7264 break;
7265 case ISD::BUILD_VECTOR: {
7266 // Attempt to simplify BUILD_VECTOR.
7267 SDValue Ops[] = {N1, N2};
7268 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7269 return V;
7270 break;
7271 }
7272 case ISD::CONCAT_VECTORS: {
7273 SDValue Ops[] = {N1, N2};
7274 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7275 return V;
7276 break;
7277 }
7278 case ISD::AND:
7279 assert(VT.isInteger() && "This operator does not apply to FP types!");
7280 assert(N1.getValueType() == N2.getValueType() &&
7281 N1.getValueType() == VT && "Binary operator types must match!");
7282 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7283 // worth handling here.
7284 if (N2CV && N2CV->isZero())
7285 return N2;
7286 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7287 return N1;
7288 break;
7289 case ISD::OR:
7290 case ISD::XOR:
7291 case ISD::ADD:
7292 case ISD::SUB:
7293 assert(VT.isInteger() && "This operator does not apply to FP types!");
7294 assert(N1.getValueType() == N2.getValueType() &&
7295 N1.getValueType() == VT && "Binary operator types must match!");
7296 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7297 // it's worth handling here.
7298 if (N2CV && N2CV->isZero())
7299 return N1;
7300 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7301 VT.getScalarType() == MVT::i1)
7302 return getNode(ISD::XOR, DL, VT, N1, N2);
7303 break;
7304 case ISD::MUL:
7305 assert(VT.isInteger() && "This operator does not apply to FP types!");
7306 assert(N1.getValueType() == N2.getValueType() &&
7307 N1.getValueType() == VT && "Binary operator types must match!");
7308 if (VT.getScalarType() == MVT::i1)
7309 return getNode(ISD::AND, DL, VT, N1, N2);
7310 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7311 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7312 const APInt &N2CImm = N2C->getAPIntValue();
7313 return getVScale(DL, VT, MulImm * N2CImm);
7314 }
7315 break;
7316 case ISD::UDIV:
7317 case ISD::UREM:
7318 case ISD::MULHU:
7319 case ISD::MULHS:
7320 case ISD::SDIV:
7321 case ISD::SREM:
7322 case ISD::SADDSAT:
7323 case ISD::SSUBSAT:
7324 case ISD::UADDSAT:
7325 case ISD::USUBSAT:
7326 assert(VT.isInteger() && "This operator does not apply to FP types!");
7327 assert(N1.getValueType() == N2.getValueType() &&
7328 N1.getValueType() == VT && "Binary operator types must match!");
7329 if (VT.getScalarType() == MVT::i1) {
7330 // fold (add_sat x, y) -> (or x, y) for bool types.
7331 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7332 return getNode(ISD::OR, DL, VT, N1, N2);
7333 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7334 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7335 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7336 }
7337 break;
7338 case ISD::SCMP:
7339 case ISD::UCMP:
7340 assert(N1.getValueType() == N2.getValueType() &&
7341 "Types of operands of UCMP/SCMP must match");
7342 assert(N1.getValueType().isVector() == VT.isVector() &&
7343 "Operands and return type of must both be scalars or vectors");
7344 if (VT.isVector())
7347 "Result and operands must have the same number of elements");
7348 break;
7349 case ISD::AVGFLOORS:
7350 case ISD::AVGFLOORU:
7351 case ISD::AVGCEILS:
7352 case ISD::AVGCEILU:
7353 assert(VT.isInteger() && "This operator does not apply to FP types!");
7354 assert(N1.getValueType() == N2.getValueType() &&
7355 N1.getValueType() == VT && "Binary operator types must match!");
7356 break;
7357 case ISD::ABDS:
7358 case ISD::ABDU:
7359 assert(VT.isInteger() && "This operator does not apply to FP types!");
7360 assert(N1.getValueType() == N2.getValueType() &&
7361 N1.getValueType() == VT && "Binary operator types must match!");
7362 if (VT.getScalarType() == MVT::i1)
7363 return getNode(ISD::XOR, DL, VT, N1, N2);
7364 break;
7365 case ISD::SMIN:
7366 case ISD::UMAX:
7367 assert(VT.isInteger() && "This operator does not apply to FP types!");
7368 assert(N1.getValueType() == N2.getValueType() &&
7369 N1.getValueType() == VT && "Binary operator types must match!");
7370 if (VT.getScalarType() == MVT::i1)
7371 return getNode(ISD::OR, DL, VT, N1, N2);
7372 break;
7373 case ISD::SMAX:
7374 case ISD::UMIN:
7375 assert(VT.isInteger() && "This operator does not apply to FP types!");
7376 assert(N1.getValueType() == N2.getValueType() &&
7377 N1.getValueType() == VT && "Binary operator types must match!");
7378 if (VT.getScalarType() == MVT::i1)
7379 return getNode(ISD::AND, DL, VT, N1, N2);
7380 break;
7381 case ISD::FADD:
7382 case ISD::FSUB:
7383 case ISD::FMUL:
7384 case ISD::FDIV:
7385 case ISD::FREM:
7386 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7387 assert(N1.getValueType() == N2.getValueType() &&
7388 N1.getValueType() == VT && "Binary operator types must match!");
7389 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7390 return V;
7391 break;
7392 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7393 assert(N1.getValueType() == VT &&
7396 "Invalid FCOPYSIGN!");
7397 break;
7398 case ISD::SHL:
7399 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7400 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7401 const APInt &ShiftImm = N2C->getAPIntValue();
7402 return getVScale(DL, VT, MulImm << ShiftImm);
7403 }
7404 [[fallthrough]];
7405 case ISD::SRA:
7406 case ISD::SRL:
7407 if (SDValue V = simplifyShift(N1, N2))
7408 return V;
7409 [[fallthrough]];
7410 case ISD::ROTL:
7411 case ISD::ROTR:
7412 assert(VT == N1.getValueType() &&
7413 "Shift operators return type must be the same as their first arg");
7414 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7415 "Shifts only work on integers");
7416 assert((!VT.isVector() || VT == N2.getValueType()) &&
7417 "Vector shift amounts must be in the same as their first arg");
7418 // Verify that the shift amount VT is big enough to hold valid shift
7419 // amounts. This catches things like trying to shift an i1024 value by an
7420 // i8, which is easy to fall into in generic code that uses
7421 // TLI.getShiftAmount().
7424 "Invalid use of small shift amount with oversized value!");
7425
7426 // Always fold shifts of i1 values so the code generator doesn't need to
7427 // handle them. Since we know the size of the shift has to be less than the
7428 // size of the value, the shift/rotate count is guaranteed to be zero.
7429 if (VT == MVT::i1)
7430 return N1;
7431 if (N2CV && N2CV->isZero())
7432 return N1;
7433 break;
7434 case ISD::FP_ROUND:
7436 VT.bitsLE(N1.getValueType()) && N2C &&
7437 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7438 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7439 if (N1.getValueType() == VT) return N1; // noop conversion.
7440 break;
7441 case ISD::AssertSext:
7442 case ISD::AssertZext: {
7443 EVT EVT = cast<VTSDNode>(N2)->getVT();
7444 assert(VT == N1.getValueType() && "Not an inreg extend!");
7445 assert(VT.isInteger() && EVT.isInteger() &&
7446 "Cannot *_EXTEND_INREG FP types");
7447 assert(!EVT.isVector() &&
7448 "AssertSExt/AssertZExt type should be the vector element type "
7449 "rather than the vector type!");
7450 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7451 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7452 break;
7453 }
7455 EVT EVT = cast<VTSDNode>(N2)->getVT();
7456 assert(VT == N1.getValueType() && "Not an inreg extend!");
7457 assert(VT.isInteger() && EVT.isInteger() &&
7458 "Cannot *_EXTEND_INREG FP types");
7459 assert(EVT.isVector() == VT.isVector() &&
7460 "SIGN_EXTEND_INREG type should be vector iff the operand "
7461 "type is vector!");
7462 assert((!EVT.isVector() ||
7464 "Vector element counts must match in SIGN_EXTEND_INREG");
7465 assert(EVT.bitsLE(VT) && "Not extending!");
7466 if (EVT == VT) return N1; // Not actually extending
7467 break;
7468 }
7470 case ISD::FP_TO_UINT_SAT: {
7471 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7472 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7473 assert(N1.getValueType().isVector() == VT.isVector() &&
7474 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7475 "vector!");
7476 assert((!VT.isVector() || VT.getVectorElementCount() ==
7478 "Vector element counts must match in FP_TO_*INT_SAT");
7479 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7480 "Type to saturate to must be a scalar.");
7481 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7482 "Not extending!");
7483 break;
7484 }
7487 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7488 element type of the vector.");
7489
7490 // Extract from an undefined value or using an undefined index is undefined.
7491 if (N1.isUndef() || N2.isUndef())
7492 return getUNDEF(VT);
7493
7494 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7495 // vectors. For scalable vectors we will provide appropriate support for
7496 // dealing with arbitrary indices.
7497 if (N2C && N1.getValueType().isFixedLengthVector() &&
7498 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7499 return getUNDEF(VT);
7500
7501 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7502 // expanding copies of large vectors from registers. This only works for
7503 // fixed length vectors, since we need to know the exact number of
7504 // elements.
7505 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7507 unsigned Factor =
7510 N1.getOperand(N2C->getZExtValue() / Factor),
7511 getVectorIdxConstant(N2C->getZExtValue() % Factor, DL));
7512 }
7513
7514 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7515 // lowering is expanding large vector constants.
7516 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7517 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7520 "BUILD_VECTOR used for scalable vectors");
7521 unsigned Index =
7522 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7523 SDValue Elt = N1.getOperand(Index);
7524
7525 if (VT != Elt.getValueType())
7526 // If the vector element type is not legal, the BUILD_VECTOR operands
7527 // are promoted and implicitly truncated, and the result implicitly
7528 // extended. Make that explicit here.
7529 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7530
7531 return Elt;
7532 }
7533
7534 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7535 // operations are lowered to scalars.
7536 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7537 // If the indices are the same, return the inserted element else
7538 // if the indices are known different, extract the element from
7539 // the original vector.
7540 SDValue N1Op2 = N1.getOperand(2);
7541 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2);
7542
7543 if (N1Op2C && N2C) {
7544 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7545 if (VT == N1.getOperand(1).getValueType())
7546 return N1.getOperand(1);
7547 if (VT.isFloatingPoint()) {
7549 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7550 }
7551 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7552 }
7553 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7554 }
7555 }
7556
7557 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7558 // when vector types are scalarized and v1iX is legal.
7559 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7560 // Here we are completely ignoring the extract element index (N2),
7561 // which is fine for fixed width vectors, since any index other than 0
7562 // is undefined anyway. However, this cannot be ignored for scalable
7563 // vectors - in theory we could support this, but we don't want to do this
7564 // without a profitability check.
7565 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7567 N1.getValueType().getVectorNumElements() == 1) {
7568 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
7569 N1.getOperand(1));
7570 }
7571 break;
7573 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
7574 assert(!N1.getValueType().isVector() && !VT.isVector() &&
7575 (N1.getValueType().isInteger() == VT.isInteger()) &&
7576 N1.getValueType() != VT &&
7577 "Wrong types for EXTRACT_ELEMENT!");
7578
7579 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
7580 // 64-bit integers into 32-bit parts. Instead of building the extract of
7581 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
7582 if (N1.getOpcode() == ISD::BUILD_PAIR)
7583 return N1.getOperand(N2C->getZExtValue());
7584
7585 // EXTRACT_ELEMENT of a constant int is also very common.
7586 if (N1C) {
7587 unsigned ElementSize = VT.getSizeInBits();
7588 unsigned Shift = ElementSize * N2C->getZExtValue();
7589 const APInt &Val = N1C->getAPIntValue();
7590 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
7591 }
7592 break;
7594 EVT N1VT = N1.getValueType();
7595 assert(VT.isVector() && N1VT.isVector() &&
7596 "Extract subvector VTs must be vectors!");
7598 "Extract subvector VTs must have the same element type!");
7599 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
7600 "Cannot extract a scalable vector from a fixed length vector!");
7601 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7603 "Extract subvector must be from larger vector to smaller vector!");
7604 assert(N2C && "Extract subvector index must be a constant");
7605 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7606 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
7607 N1VT.getVectorMinNumElements()) &&
7608 "Extract subvector overflow!");
7609 assert(N2C->getAPIntValue().getBitWidth() ==
7610 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7611 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
7612
7613 // Trivial extraction.
7614 if (VT == N1VT)
7615 return N1;
7616
7617 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
7618 if (N1.isUndef())
7619 return getUNDEF(VT);
7620
7621 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
7622 // the concat have the same type as the extract.
7623 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
7624 VT == N1.getOperand(0).getValueType()) {
7625 unsigned Factor = VT.getVectorMinNumElements();
7626 return N1.getOperand(N2C->getZExtValue() / Factor);
7627 }
7628
7629 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
7630 // during shuffle legalization.
7631 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
7632 VT == N1.getOperand(1).getValueType())
7633 return N1.getOperand(1);
7634 break;
7635 }
7636 }
7637
7638 // Perform trivial constant folding.
7639 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
7640 return SV;
7641
7642 // Canonicalize an UNDEF to the RHS, even over a constant.
7643 if (N1.isUndef()) {
7644 if (TLI->isCommutativeBinOp(Opcode)) {
7645 std::swap(N1, N2);
7646 } else {
7647 switch (Opcode) {
7648 case ISD::SUB:
7649 return getUNDEF(VT); // fold op(undef, arg2) -> undef
7651 case ISD::UDIV:
7652 case ISD::SDIV:
7653 case ISD::UREM:
7654 case ISD::SREM:
7655 case ISD::SSUBSAT:
7656 case ISD::USUBSAT:
7657 return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0
7658 }
7659 }
7660 }
7661
7662 // Fold a bunch of operators when the RHS is undef.
7663 if (N2.isUndef()) {
7664 switch (Opcode) {
7665 case ISD::XOR:
7666 if (N1.isUndef())
7667 // Handle undef ^ undef -> 0 special case. This is a common
7668 // idiom (misuse).
7669 return getConstant(0, DL, VT);
7670 [[fallthrough]];
7671 case ISD::ADD:
7672 case ISD::SUB:
7673 case ISD::UDIV:
7674 case ISD::SDIV:
7675 case ISD::UREM:
7676 case ISD::SREM:
7677 return getUNDEF(VT); // fold op(arg1, undef) -> undef
7678 case ISD::MUL:
7679 case ISD::AND:
7680 case ISD::SSUBSAT:
7681 case ISD::USUBSAT:
7682 return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0
7683 case ISD::OR:
7684 case ISD::SADDSAT:
7685 case ISD::UADDSAT:
7686 return getAllOnesConstant(DL, VT);
7687 }
7688 }
7689
7690 // Memoize this node if possible.
7691 SDNode *N;
7692 SDVTList VTs = getVTList(VT);
7693 SDValue Ops[] = {N1, N2};
7694 if (VT != MVT::Glue) {
7696 AddNodeIDNode(ID, Opcode, VTs, Ops);
7697 void *IP = nullptr;
7698 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7699 E->intersectFlagsWith(Flags);
7700 return SDValue(E, 0);
7701 }
7702
7703 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7704 N->setFlags(Flags);
7705 createOperands(N, Ops);
7706 CSEMap.InsertNode(N, IP);
7707 } else {
7708 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7709 createOperands(N, Ops);
7710 }
7711
7712 InsertNode(N);
7713 SDValue V = SDValue(N, 0);
7714 NewSDValueDbgMsg(V, "Creating new node: ", this);
7715 return V;
7716}
7717
7718SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7719 SDValue N1, SDValue N2, SDValue N3) {
7720 SDNodeFlags Flags;
7721 if (Inserter)
7722 Flags = Inserter->getFlags();
7723 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
7724}
7725
7726SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7727 SDValue N1, SDValue N2, SDValue N3,
7728 const SDNodeFlags Flags) {
7730 N2.getOpcode() != ISD::DELETED_NODE &&
7731 N3.getOpcode() != ISD::DELETED_NODE &&
7732 "Operand is DELETED_NODE!");
7733 // Perform various simplifications.
7734 switch (Opcode) {
7735 case ISD::FMA:
7736 case ISD::FMAD: {
7737 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7738 assert(N1.getValueType() == VT && N2.getValueType() == VT &&
7739 N3.getValueType() == VT && "FMA types must match!");
7740 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
7741 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
7742 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
7743 if (N1CFP && N2CFP && N3CFP) {
7744 APFloat V1 = N1CFP->getValueAPF();
7745 const APFloat &V2 = N2CFP->getValueAPF();
7746 const APFloat &V3 = N3CFP->getValueAPF();
7747 if (Opcode == ISD::FMAD) {
7750 } else
7752 return getConstantFP(V1, DL, VT);
7753 }
7754 break;
7755 }
7756 case ISD::BUILD_VECTOR: {
7757 // Attempt to simplify BUILD_VECTOR.
7758 SDValue Ops[] = {N1, N2, N3};
7759 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7760 return V;
7761 break;
7762 }
7763 case ISD::CONCAT_VECTORS: {
7764 SDValue Ops[] = {N1, N2, N3};
7765 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7766 return V;
7767 break;
7768 }
7769 case ISD::SETCC: {
7770 assert(VT.isInteger() && "SETCC result type must be an integer!");
7771 assert(N1.getValueType() == N2.getValueType() &&
7772 "SETCC operands must have the same type!");
7773 assert(VT.isVector() == N1.getValueType().isVector() &&
7774 "SETCC type should be vector iff the operand type is vector!");
7775 assert((!VT.isVector() || VT.getVectorElementCount() ==
7777 "SETCC vector element counts must match!");
7778 // Use FoldSetCC to simplify SETCC's.
7779 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
7780 return V;
7781 // Vector constant folding.
7782 SDValue Ops[] = {N1, N2, N3};
7783 if (SDValue V = FoldConstantArithmetic(Opcode, DL, VT, Ops)) {
7784 NewSDValueDbgMsg(V, "New node vector constant folding: ", this);
7785 return V;
7786 }
7787 break;
7788 }
7789 case ISD::SELECT:
7790 case ISD::VSELECT:
7791 if (SDValue V = simplifySelect(N1, N2, N3))
7792 return V;
7793 break;
7795 llvm_unreachable("should use getVectorShuffle constructor!");
7796 case ISD::VECTOR_SPLICE: {
7797 if (cast<ConstantSDNode>(N3)->isZero())
7798 return N1;
7799 break;
7800 }
7802 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3);
7803 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
7804 // for scalable vectors where we will generate appropriate code to
7805 // deal with out-of-bounds cases correctly.
7806 if (N3C && N1.getValueType().isFixedLengthVector() &&
7808 return getUNDEF(VT);
7809
7810 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
7811 if (N3.isUndef())
7812 return getUNDEF(VT);
7813
7814 // If the inserted element is an UNDEF, just use the input vector.
7815 if (N2.isUndef())
7816 return N1;
7817
7818 break;
7819 }
7820 case ISD::INSERT_SUBVECTOR: {
7821 // Inserting undef into undef is still undef.
7822 if (N1.isUndef() && N2.isUndef())
7823 return getUNDEF(VT);
7824
7825 EVT N2VT = N2.getValueType();
7826 assert(VT == N1.getValueType() &&
7827 "Dest and insert subvector source types must match!");
7828 assert(VT.isVector() && N2VT.isVector() &&
7829 "Insert subvector VTs must be vectors!");
7831 "Insert subvector VTs must have the same element type!");
7832 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
7833 "Cannot insert a scalable vector into a fixed length vector!");
7834 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7836 "Insert subvector must be from smaller vector to larger vector!");
7837 assert(isa<ConstantSDNode>(N3) &&
7838 "Insert subvector index must be constant");
7839 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7840 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
7842 "Insert subvector overflow!");
7844 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7845 "Constant index for INSERT_SUBVECTOR has an invalid size");
7846
7847 // Trivial insertion.
7848 if (VT == N2VT)
7849 return N2;
7850
7851 // If this is an insert of an extracted vector into an undef vector, we
7852 // can just use the input to the extract.
7853 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7854 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT)
7855 return N2.getOperand(0);
7856 break;
7857 }
7858 case ISD::BITCAST:
7859 // Fold bit_convert nodes from a type to themselves.
7860 if (N1.getValueType() == VT)
7861 return N1;
7862 break;
7863 case ISD::VP_TRUNCATE:
7864 case ISD::VP_SIGN_EXTEND:
7865 case ISD::VP_ZERO_EXTEND:
7866 // Don't create noop casts.
7867 if (N1.getValueType() == VT)
7868 return N1;
7869 break;
7870 case ISD::VECTOR_COMPRESS: {
7871 [[maybe_unused]] EVT VecVT = N1.getValueType();
7872 [[maybe_unused]] EVT MaskVT = N2.getValueType();
7873 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
7874 assert(VT == VecVT && "Vector and result type don't match.");
7875 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
7876 "All inputs must be vectors.");
7877 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
7879 "Vector and mask must have same number of elements.");
7880
7881 if (N1.isUndef() || N2.isUndef())
7882 return N3;
7883
7884 break;
7885 }
7886 }
7887
7888 // Memoize node if it doesn't produce a glue result.
7889 SDNode *N;
7890 SDVTList VTs = getVTList(VT);
7891 SDValue Ops[] = {N1, N2, N3};
7892 if (VT != MVT::Glue) {
7894 AddNodeIDNode(ID, Opcode, VTs, Ops);
7895 void *IP = nullptr;
7896 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7897 E->intersectFlagsWith(Flags);
7898 return SDValue(E, 0);
7899 }
7900
7901 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7902 N->setFlags(Flags);
7903 createOperands(N, Ops);
7904 CSEMap.InsertNode(N, IP);
7905 } else {
7906 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7907 createOperands(N, Ops);
7908 }
7909
7910 InsertNode(N);
7911 SDValue V = SDValue(N, 0);
7912 NewSDValueDbgMsg(V, "Creating new node: ", this);
7913 return V;
7914}
7915
7916SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7917 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
7918 const SDNodeFlags Flags) {
7919 SDValue Ops[] = { N1, N2, N3, N4 };
7920 return getNode(Opcode, DL, VT, Ops, Flags);
7921}
7922
7923SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7924 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
7925 SDNodeFlags Flags;
7926 if (Inserter)
7927 Flags = Inserter->getFlags();
7928 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
7929}
7930
7931SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7932 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
7933 SDValue N5, const SDNodeFlags Flags) {
7934 SDValue Ops[] = { N1, N2, N3, N4, N5 };
7935 return getNode(Opcode, DL, VT, Ops, Flags);
7936}
7937
7938SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7939 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
7940 SDValue N5) {
7941 SDNodeFlags Flags;
7942 if (Inserter)
7943 Flags = Inserter->getFlags();
7944 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
7945}
7946
7947/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
7948/// the incoming stack arguments to be loaded from the stack.
7950 SmallVector<SDValue, 8> ArgChains;
7951
7952 // Include the original chain at the beginning of the list. When this is
7953 // used by target LowerCall hooks, this helps legalize find the
7954 // CALLSEQ_BEGIN node.
7955 ArgChains.push_back(Chain);
7956
7957 // Add a chain value for each stack argument.
7958 for (SDNode *U : getEntryNode().getNode()->users())
7959 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
7960 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
7961 if (FI->getIndex() < 0)
7962 ArgChains.push_back(SDValue(L, 1));
7963
7964 // Build a tokenfactor for all the chains.
7965 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
7966}
7967
7968/// getMemsetValue - Vectorized representation of the memset value
7969/// operand.
7971 const SDLoc &dl) {
7972 assert(!Value.isUndef());
7973
7974 unsigned NumBits = VT.getScalarSizeInBits();
7975 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
7976 assert(C->getAPIntValue().getBitWidth() == 8);
7977 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
7978 if (VT.isInteger()) {
7979 bool IsOpaque = VT.getSizeInBits() > 64 ||
7980 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
7981 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
7982 }
7983 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
7984 }
7985
7986 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
7987 EVT IntVT = VT.getScalarType();
7988 if (!IntVT.isInteger())
7989 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
7990
7991 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
7992 if (NumBits > 8) {
7993 // Use a multiplication with 0x010101... to extend the input to the
7994 // required length.
7995 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
7996 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
7997 DAG.getConstant(Magic, dl, IntVT));
7998 }
7999
8000 if (VT != Value.getValueType() && !VT.isInteger())
8001 Value = DAG.getBitcast(VT.getScalarType(), Value);
8002 if (VT != Value.getValueType())
8003 Value = DAG.getSplatBuildVector(VT, dl, Value);
8004
8005 return Value;
8006}
8007
8008/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8009/// used when a memcpy is turned into a memset when the source is a constant
8010/// string ptr.
8012 const TargetLowering &TLI,
8013 const ConstantDataArraySlice &Slice) {
8014 // Handle vector with all elements zero.
8015 if (Slice.Array == nullptr) {
8016 if (VT.isInteger())
8017 return DAG.getConstant(0, dl, VT);
8018 if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
8019 return DAG.getConstantFP(0.0, dl, VT);
8020 if (VT.isVector()) {
8021 unsigned NumElts = VT.getVectorNumElements();
8022 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
8023 return DAG.getNode(ISD::BITCAST, dl, VT,
8024 DAG.getConstant(0, dl,
8026 EltVT, NumElts)));
8027 }
8028 llvm_unreachable("Expected type!");
8029 }
8030
8031 assert(!VT.isVector() && "Can't handle vector type here!");
8032 unsigned NumVTBits = VT.getSizeInBits();
8033 unsigned NumVTBytes = NumVTBits / 8;
8034 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8035
8036 APInt Val(NumVTBits, 0);
8037 if (DAG.getDataLayout().isLittleEndian()) {
8038 for (unsigned i = 0; i != NumBytes; ++i)
8039 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8040 } else {
8041 for (unsigned i = 0; i != NumBytes; ++i)
8042 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8043 }
8044
8045 // If the "cost" of materializing the integer immediate is less than the cost
8046 // of a load, then it is cost effective to turn the load into the immediate.
8047 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8048 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8049 return DAG.getConstant(Val, dl, VT);
8050 return SDValue();
8051}
8052
8054 const SDLoc &DL,
8055 const SDNodeFlags Flags) {
8056 EVT VT = Base.getValueType();
8057 SDValue Index;
8058
8059 if (Offset.isScalable())
8060 Index = getVScale(DL, Base.getValueType(),
8061 APInt(Base.getValueSizeInBits().getFixedValue(),
8062 Offset.getKnownMinValue()));
8063 else
8064 Index = getConstant(Offset.getFixedValue(), DL, VT);
8065
8066 return getMemBasePlusOffset(Base, Index, DL, Flags);
8067}
8068
8070 const SDLoc &DL,
8071 const SDNodeFlags Flags) {
8072 assert(Offset.getValueType().isInteger());
8073 EVT BasePtrVT = Ptr.getValueType();
8074 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, Flags);
8075}
8076
8077/// Returns true if memcpy source is constant data.
8079 uint64_t SrcDelta = 0;
8080 GlobalAddressSDNode *G = nullptr;
8081 if (Src.getOpcode() == ISD::GlobalAddress)
8082 G = cast<GlobalAddressSDNode>(Src);
8083 else if (Src.getOpcode() == ISD::ADD &&
8084 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8085 Src.getOperand(1).getOpcode() == ISD::Constant) {
8086 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8087 SrcDelta = Src.getConstantOperandVal(1);
8088 }
8089 if (!G)
8090 return false;
8091
8092 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8093 SrcDelta + G->getOffset());
8094}
8095
8097 SelectionDAG &DAG) {
8098 // On Darwin, -Os means optimize for size without hurting performance, so
8099 // only really optimize for size when -Oz (MinSize) is used.
8101 return MF.getFunction().hasMinSize();
8102 return DAG.shouldOptForSize();
8103}
8104
8106 SmallVector<SDValue, 32> &OutChains, unsigned From,
8107 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8108 SmallVector<SDValue, 16> &OutStoreChains) {
8109 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8110 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8111 SmallVector<SDValue, 16> GluedLoadChains;
8112 for (unsigned i = From; i < To; ++i) {
8113 OutChains.push_back(OutLoadChains[i]);
8114 GluedLoadChains.push_back(OutLoadChains[i]);
8115 }
8116
8117 // Chain for all loads.
8118 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8119 GluedLoadChains);
8120
8121 for (unsigned i = From; i < To; ++i) {
8122 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8123 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8124 ST->getBasePtr(), ST->getMemoryVT(),
8125 ST->getMemOperand());
8126 OutChains.push_back(NewStore);
8127 }
8128}
8129
8131 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8132 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8133 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8134 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8135 // Turn a memcpy of undef to nop.
8136 // FIXME: We need to honor volatile even is Src is undef.
8137 if (Src.isUndef())
8138 return Chain;
8139
8140 // Expand memcpy to a series of load and store ops if the size operand falls
8141 // below a certain threshold.
8142 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8143 // rather than maybe a humongous number of loads and stores.
8144 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8145 const DataLayout &DL = DAG.getDataLayout();
8146 LLVMContext &C = *DAG.getContext();
8147 std::vector<EVT> MemOps;
8148 bool DstAlignCanChange = false;
8150 MachineFrameInfo &MFI = MF.getFrameInfo();
8151 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8152 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8153 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8154 DstAlignCanChange = true;
8155 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8156 if (!SrcAlign || Alignment > *SrcAlign)
8157 SrcAlign = Alignment;
8158 assert(SrcAlign && "SrcAlign must be set");
8160 // If marked as volatile, perform a copy even when marked as constant.
8161 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8162 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8163 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8164 const MemOp Op = isZeroConstant
8165 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8166 /*IsZeroMemset*/ true, isVol)
8167 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8168 *SrcAlign, isVol, CopyFromConstant);
8169 if (!TLI.findOptimalMemOpLowering(
8170 MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8171 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
8172 return SDValue();
8173
8174 if (DstAlignCanChange) {
8175 Type *Ty = MemOps[0].getTypeForEVT(C);
8176 Align NewAlign = DL.getABITypeAlign(Ty);
8177
8178 // Don't promote to an alignment that would require dynamic stack
8179 // realignment which may conflict with optimizations such as tail call
8180 // optimization.
8182 if (!TRI->hasStackRealignment(MF))
8183 if (MaybeAlign StackAlign = DL.getStackAlignment())
8184 NewAlign = std::min(NewAlign, *StackAlign);
8185
8186 if (NewAlign > Alignment) {
8187 // Give the stack frame object a larger alignment if needed.
8188 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8189 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8190 Alignment = NewAlign;
8191 }
8192 }
8193
8194 // Prepare AAInfo for loads/stores after lowering this memcpy.
8195 AAMDNodes NewAAInfo = AAInfo;
8196 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8197
8198 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8199 bool isConstant =
8200 BatchAA && SrcVal &&
8201 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8202
8203 MachineMemOperand::Flags MMOFlags =
8205 SmallVector<SDValue, 16> OutLoadChains;
8206 SmallVector<SDValue, 16> OutStoreChains;
8207 SmallVector<SDValue, 32> OutChains;
8208 unsigned NumMemOps = MemOps.size();
8209 uint64_t SrcOff = 0, DstOff = 0;
8210 for (unsigned i = 0; i != NumMemOps; ++i) {
8211 EVT VT = MemOps[i];
8212 unsigned VTSize = VT.getSizeInBits() / 8;
8213 SDValue Value, Store;
8214
8215 if (VTSize > Size) {
8216 // Issuing an unaligned load / store pair that overlaps with the previous
8217 // pair. Adjust the offset accordingly.
8218 assert(i == NumMemOps-1 && i != 0);
8219 SrcOff -= VTSize - Size;
8220 DstOff -= VTSize - Size;
8221 }
8222
8223 if (CopyFromConstant &&
8224 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8225 // It's unlikely a store of a vector immediate can be done in a single
8226 // instruction. It would require a load from a constantpool first.
8227 // We only handle zero vectors here.
8228 // FIXME: Handle other cases where store of vector immediate is done in
8229 // a single instruction.
8230 ConstantDataArraySlice SubSlice;
8231 if (SrcOff < Slice.Length) {
8232 SubSlice = Slice;
8233 SubSlice.move(SrcOff);
8234 } else {
8235 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8236 SubSlice.Array = nullptr;
8237 SubSlice.Offset = 0;
8238 SubSlice.Length = VTSize;
8239 }
8240 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8241 if (Value.getNode()) {
8242 Store = DAG.getStore(
8243 Chain, dl, Value,
8244 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8245 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8246 OutChains.push_back(Store);
8247 }
8248 }
8249
8250 if (!Store.getNode()) {
8251 // The type might not be legal for the target. This should only happen
8252 // if the type is smaller than a legal type, as on PPC, so the right
8253 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8254 // to Load/Store if NVT==VT.
8255 // FIXME does the case above also need this?
8256 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8257 assert(NVT.bitsGE(VT));
8258
8259 bool isDereferenceable =
8260 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8261 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8262 if (isDereferenceable)
8264 if (isConstant)
8265 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8266
8267 Value = DAG.getExtLoad(
8268 ISD::EXTLOAD, dl, NVT, Chain,
8269 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
8270 SrcPtrInfo.getWithOffset(SrcOff), VT,
8271 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8272 OutLoadChains.push_back(Value.getValue(1));
8273
8274 Store = DAG.getTruncStore(
8275 Chain, dl, Value,
8276 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8277 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8278 OutStoreChains.push_back(Store);
8279 }
8280 SrcOff += VTSize;
8281 DstOff += VTSize;
8282 Size -= VTSize;
8283 }
8284
8285 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8287 unsigned NumLdStInMemcpy = OutStoreChains.size();
8288
8289 if (NumLdStInMemcpy) {
8290 // It may be that memcpy might be converted to memset if it's memcpy
8291 // of constants. In such a case, we won't have loads and stores, but
8292 // just stores. In the absence of loads, there is nothing to gang up.
8293 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8294 // If target does not care, just leave as it.
8295 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8296 OutChains.push_back(OutLoadChains[i]);
8297 OutChains.push_back(OutStoreChains[i]);
8298 }
8299 } else {
8300 // Ld/St less than/equal limit set by target.
8301 if (NumLdStInMemcpy <= GluedLdStLimit) {
8302 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8303 NumLdStInMemcpy, OutLoadChains,
8304 OutStoreChains);
8305 } else {
8306 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8307 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8308 unsigned GlueIter = 0;
8309
8310 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8311 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
8312 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
8313
8314 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8315 OutLoadChains, OutStoreChains);
8316 GlueIter += GluedLdStLimit;
8317 }
8318
8319 // Residual ld/st.
8320 if (RemainingLdStInMemcpy) {
8321 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8322 RemainingLdStInMemcpy, OutLoadChains,
8323 OutStoreChains);
8324 }
8325 }
8326 }
8327 }
8328 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8329}
8330
8332 SDValue Chain, SDValue Dst, SDValue Src,
8333 uint64_t Size, Align Alignment,
8334 bool isVol, bool AlwaysInline,
8335 MachinePointerInfo DstPtrInfo,
8336 MachinePointerInfo SrcPtrInfo,
8337 const AAMDNodes &AAInfo) {
8338 // Turn a memmove of undef to nop.
8339 // FIXME: We need to honor volatile even is Src is undef.
8340 if (Src.isUndef())
8341 return Chain;
8342
8343 // Expand memmove to a series of load and store ops if the size operand falls
8344 // below a certain threshold.
8345 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8346 const DataLayout &DL = DAG.getDataLayout();
8347 LLVMContext &C = *DAG.getContext();
8348 std::vector<EVT> MemOps;
8349 bool DstAlignCanChange = false;
8351 MachineFrameInfo &MFI = MF.getFrameInfo();
8352 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8353 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8354 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8355 DstAlignCanChange = true;
8356 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8357 if (!SrcAlign || Alignment > *SrcAlign)
8358 SrcAlign = Alignment;
8359 assert(SrcAlign && "SrcAlign must be set");
8360 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8361 if (!TLI.findOptimalMemOpLowering(
8362 MemOps, Limit,
8363 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8364 /*IsVolatile*/ true),
8365 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8366 MF.getFunction().getAttributes()))
8367 return SDValue();
8368
8369 if (DstAlignCanChange) {
8370 Type *Ty = MemOps[0].getTypeForEVT(C);
8371 Align NewAlign = DL.getABITypeAlign(Ty);
8372
8373 // Don't promote to an alignment that would require dynamic stack
8374 // realignment which may conflict with optimizations such as tail call
8375 // optimization.
8377 if (!TRI->hasStackRealignment(MF))
8378 if (MaybeAlign StackAlign = DL.getStackAlignment())
8379 NewAlign = std::min(NewAlign, *StackAlign);
8380
8381 if (NewAlign > Alignment) {
8382 // Give the stack frame object a larger alignment if needed.
8383 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8384 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8385 Alignment = NewAlign;
8386 }
8387 }
8388
8389 // Prepare AAInfo for loads/stores after lowering this memmove.
8390 AAMDNodes NewAAInfo = AAInfo;
8391 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8392
8393 MachineMemOperand::Flags MMOFlags =
8395 uint64_t SrcOff = 0, DstOff = 0;
8396 SmallVector<SDValue, 8> LoadValues;
8397 SmallVector<SDValue, 8> LoadChains;
8398 SmallVector<SDValue, 8> OutChains;
8399 unsigned NumMemOps = MemOps.size();
8400 for (unsigned i = 0; i < NumMemOps; i++) {
8401 EVT VT = MemOps[i];
8402 unsigned VTSize = VT.getSizeInBits() / 8;
8403 SDValue Value;
8404
8405 bool isDereferenceable =
8406 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8407 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8408 if (isDereferenceable)
8410
8411 Value = DAG.getLoad(
8412 VT, dl, Chain,
8413 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
8414 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8415 LoadValues.push_back(Value);
8416 LoadChains.push_back(Value.getValue(1));
8417 SrcOff += VTSize;
8418 }
8419 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8420 OutChains.clear();
8421 for (unsigned i = 0; i < NumMemOps; i++) {
8422 EVT VT = MemOps[i];
8423 unsigned VTSize = VT.getSizeInBits() / 8;
8424 SDValue Store;
8425
8426 Store = DAG.getStore(
8427 Chain, dl, LoadValues[i],
8428 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8429 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8430 OutChains.push_back(Store);
8431 DstOff += VTSize;
8432 }
8433
8434 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8435}
8436
8437/// Lower the call to 'memset' intrinsic function into a series of store
8438/// operations.
8439///
8440/// \param DAG Selection DAG where lowered code is placed.
8441/// \param dl Link to corresponding IR location.
8442/// \param Chain Control flow dependency.
8443/// \param Dst Pointer to destination memory location.
8444/// \param Src Value of byte to write into the memory.
8445/// \param Size Number of bytes to write.
8446/// \param Alignment Alignment of the destination in bytes.
8447/// \param isVol True if destination is volatile.
8448/// \param AlwaysInline Makes sure no function call is generated.
8449/// \param DstPtrInfo IR information on the memory pointer.
8450/// \returns New head in the control flow, if lowering was successful, empty
8451/// SDValue otherwise.
8452///
8453/// The function tries to replace 'llvm.memset' intrinsic with several store
8454/// operations and value calculation code. This is usually profitable for small
8455/// memory size or when the semantic requires inlining.
8457 SDValue Chain, SDValue Dst, SDValue Src,
8458 uint64_t Size, Align Alignment, bool isVol,
8459 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
8460 const AAMDNodes &AAInfo) {
8461 // Turn a memset of undef to nop.
8462 // FIXME: We need to honor volatile even is Src is undef.
8463 if (Src.isUndef())
8464 return Chain;
8465
8466 // Expand memset to a series of load/store ops if the size operand
8467 // falls below a certain threshold.
8468 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8469 std::vector<EVT> MemOps;
8470 bool DstAlignCanChange = false;
8472 MachineFrameInfo &MFI = MF.getFrameInfo();
8473 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8474 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8475 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8476 DstAlignCanChange = true;
8477 bool IsZeroVal = isNullConstant(Src);
8478 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
8479
8480 if (!TLI.findOptimalMemOpLowering(
8481 MemOps, Limit,
8482 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
8483 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
8484 return SDValue();
8485
8486 if (DstAlignCanChange) {
8487 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
8488 const DataLayout &DL = DAG.getDataLayout();
8489 Align NewAlign = DL.getABITypeAlign(Ty);
8490
8491 // Don't promote to an alignment that would require dynamic stack
8492 // realignment which may conflict with optimizations such as tail call
8493 // optimization.
8495 if (!TRI->hasStackRealignment(MF))
8496 if (MaybeAlign StackAlign = DL.getStackAlignment())
8497 NewAlign = std::min(NewAlign, *StackAlign);
8498
8499 if (NewAlign > Alignment) {
8500 // Give the stack frame object a larger alignment if needed.
8501 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8502 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8503 Alignment = NewAlign;
8504 }
8505 }
8506
8507 SmallVector<SDValue, 8> OutChains;
8508 uint64_t DstOff = 0;
8509 unsigned NumMemOps = MemOps.size();
8510
8511 // Find the largest store and generate the bit pattern for it.
8512 EVT LargestVT = MemOps[0];
8513 for (unsigned i = 1; i < NumMemOps; i++)
8514 if (MemOps[i].bitsGT(LargestVT))
8515 LargestVT = MemOps[i];
8516 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
8517
8518 // Prepare AAInfo for loads/stores after lowering this memset.
8519 AAMDNodes NewAAInfo = AAInfo;
8520 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8521
8522 for (unsigned i = 0; i < NumMemOps; i++) {
8523 EVT VT = MemOps[i];
8524 unsigned VTSize = VT.getSizeInBits() / 8;
8525 if (VTSize > Size) {
8526 // Issuing an unaligned load / store pair that overlaps with the previous
8527 // pair. Adjust the offset accordingly.
8528 assert(i == NumMemOps-1 && i != 0);
8529 DstOff -= VTSize - Size;
8530 }
8531
8532 // If this store is smaller than the largest store see whether we can get
8533 // the smaller value for free with a truncate or extract vector element and
8534 // then store.
8535 SDValue Value = MemSetValue;
8536 if (VT.bitsLT(LargestVT)) {
8537 unsigned Index;
8538 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
8539 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
8540 if (!LargestVT.isVector() && !VT.isVector() &&
8541 TLI.isTruncateFree(LargestVT, VT))
8542 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
8543 else if (LargestVT.isVector() && !VT.isVector() &&
8545 LargestVT.getTypeForEVT(*DAG.getContext()),
8546 VT.getSizeInBits(), Index) &&
8547 TLI.isTypeLegal(SVT) &&
8548 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
8549 // Target which can combine store(extractelement VectorTy, Idx) can get
8550 // the smaller value for free.
8551 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
8552 Value = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, TailValue,
8553 DAG.getVectorIdxConstant(Index, dl));
8554 } else
8555 Value = getMemsetValue(Src, VT, DAG, dl);
8556 }
8557 assert(Value.getValueType() == VT && "Value with wrong type.");
8558 SDValue Store = DAG.getStore(
8559 Chain, dl, Value,
8560 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8561 DstPtrInfo.getWithOffset(DstOff), Alignment,
8563 NewAAInfo);
8564 OutChains.push_back(Store);
8565 DstOff += VT.getSizeInBits() / 8;
8566 Size -= VTSize;
8567 }
8568
8569 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8570}
8571
8573 unsigned AS) {
8574 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
8575 // pointer operands can be losslessly bitcasted to pointers of address space 0
8576 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
8577 report_fatal_error("cannot lower memory intrinsic in address space " +
8578 Twine(AS));
8579 }
8580}
8581
8583 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
8584 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
8585 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
8586 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
8587 BatchAAResults *BatchAA) {
8588 // Check to see if we should lower the memcpy to loads and stores first.
8589 // For cases within the target-specified limits, this is the best choice.
8590 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8591 if (ConstantSize) {
8592 // Memcpy with size zero? Just return the original chain.
8593 if (ConstantSize->isZero())
8594 return Chain;
8595
8597 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8598 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
8599 if (Result.getNode())
8600 return Result;
8601 }
8602
8603 // Then check to see if we should lower the memcpy with target-specific
8604 // code. If the target chooses to do this, this is the next best.
8605 if (TSI) {
8606 SDValue Result = TSI->EmitTargetCodeForMemcpy(
8607 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
8608 DstPtrInfo, SrcPtrInfo);
8609 if (Result.getNode())
8610 return Result;
8611 }
8612
8613 // If we really need inline code and the target declined to provide it,
8614 // use a (potentially long) sequence of loads and stores.
8615 if (AlwaysInline) {
8616 assert(ConstantSize && "AlwaysInline requires a constant size!");
8618 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8619 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
8620 }
8621
8624
8625 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
8626 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
8627 // respect volatile, so they may do things like read or write memory
8628 // beyond the given memory regions. But fixing this isn't easy, and most
8629 // people don't care.
8630
8631 // Emit a library call.
8634 Entry.Ty = PointerType::getUnqual(*getContext());
8635 Entry.Node = Dst; Args.push_back(Entry);
8636 Entry.Node = Src; Args.push_back(Entry);
8637
8638 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8639 Entry.Node = Size; Args.push_back(Entry);
8640 // FIXME: pass in SDLoc
8642 bool IsTailCall = false;
8643 if (OverrideTailCall.has_value()) {
8644 IsTailCall = *OverrideTailCall;
8645 } else {
8646 bool LowersToMemcpy =
8647 TLI->getLibcallName(RTLIB::MEMCPY) == StringRef("memcpy");
8648 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
8649 IsTailCall = CI && CI->isTailCall() &&
8651 ReturnsFirstArg && LowersToMemcpy);
8652 }
8653
8654 CLI.setDebugLoc(dl)
8655 .setChain(Chain)
8656 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
8657 Dst.getValueType().getTypeForEVT(*getContext()),
8658 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
8659 TLI->getPointerTy(getDataLayout())),
8660 std::move(Args))
8662 .setTailCall(IsTailCall);
8663
8664 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8665 return CallResult.second;
8666}
8667
8669 SDValue Dst, SDValue Src, SDValue Size,
8670 Type *SizeTy, unsigned ElemSz,
8671 bool isTailCall,
8672 MachinePointerInfo DstPtrInfo,
8673 MachinePointerInfo SrcPtrInfo) {
8674 // Emit a library call.
8677 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8678 Entry.Node = Dst;
8679 Args.push_back(Entry);
8680
8681 Entry.Node = Src;
8682 Args.push_back(Entry);
8683
8684 Entry.Ty = SizeTy;
8685 Entry.Node = Size;
8686 Args.push_back(Entry);
8687
8688 RTLIB::Libcall LibraryCall =
8690 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8691 report_fatal_error("Unsupported element size");
8692
8694 CLI.setDebugLoc(dl)
8695 .setChain(Chain)
8696 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8698 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8699 TLI->getPointerTy(getDataLayout())),
8700 std::move(Args))
8702 .setTailCall(isTailCall);
8703
8704 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8705 return CallResult.second;
8706}
8707
8709 SDValue Src, SDValue Size, Align Alignment,
8710 bool isVol, const CallInst *CI,
8711 std::optional<bool> OverrideTailCall,
8712 MachinePointerInfo DstPtrInfo,
8713 MachinePointerInfo SrcPtrInfo,
8714 const AAMDNodes &AAInfo,
8715 BatchAAResults *BatchAA) {
8716 // Check to see if we should lower the memmove to loads and stores first.
8717 // For cases within the target-specified limits, this is the best choice.
8718 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8719 if (ConstantSize) {
8720 // Memmove with size zero? Just return the original chain.
8721 if (ConstantSize->isZero())
8722 return Chain;
8723
8725 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8726 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
8727 if (Result.getNode())
8728 return Result;
8729 }
8730
8731 // Then check to see if we should lower the memmove with target-specific
8732 // code. If the target chooses to do this, this is the next best.
8733 if (TSI) {
8734 SDValue Result =
8735 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
8736 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
8737 if (Result.getNode())
8738 return Result;
8739 }
8740
8743
8744 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
8745 // not be safe. See memcpy above for more details.
8746
8747 // Emit a library call.
8750 Entry.Ty = PointerType::getUnqual(*getContext());
8751 Entry.Node = Dst; Args.push_back(Entry);
8752 Entry.Node = Src; Args.push_back(Entry);
8753
8754 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8755 Entry.Node = Size; Args.push_back(Entry);
8756 // FIXME: pass in SDLoc
8758
8759 bool IsTailCall = false;
8760 if (OverrideTailCall.has_value()) {
8761 IsTailCall = *OverrideTailCall;
8762 } else {
8763 bool LowersToMemmove =
8764 TLI->getLibcallName(RTLIB::MEMMOVE) == StringRef("memmove");
8765 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
8766 IsTailCall = CI && CI->isTailCall() &&
8768 ReturnsFirstArg && LowersToMemmove);
8769 }
8770
8771 CLI.setDebugLoc(dl)
8772 .setChain(Chain)
8773 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
8774 Dst.getValueType().getTypeForEVT(*getContext()),
8775 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
8776 TLI->getPointerTy(getDataLayout())),
8777 std::move(Args))
8779 .setTailCall(IsTailCall);
8780
8781 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8782 return CallResult.second;
8783}
8784
8786 SDValue Dst, SDValue Src, SDValue Size,
8787 Type *SizeTy, unsigned ElemSz,
8788 bool isTailCall,
8789 MachinePointerInfo DstPtrInfo,
8790 MachinePointerInfo SrcPtrInfo) {
8791 // Emit a library call.
8794 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8795 Entry.Node = Dst;
8796 Args.push_back(Entry);
8797
8798 Entry.Node = Src;
8799 Args.push_back(Entry);
8800
8801 Entry.Ty = SizeTy;
8802 Entry.Node = Size;
8803 Args.push_back(Entry);
8804
8805 RTLIB::Libcall LibraryCall =
8807 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8808 report_fatal_error("Unsupported element size");
8809
8811 CLI.setDebugLoc(dl)
8812 .setChain(Chain)
8813 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8815 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8816 TLI->getPointerTy(getDataLayout())),
8817 std::move(Args))
8819 .setTailCall(isTailCall);
8820
8821 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8822 return CallResult.second;
8823}
8824
8826 SDValue Src, SDValue Size, Align Alignment,
8827 bool isVol, bool AlwaysInline,
8828 const CallInst *CI,
8829 MachinePointerInfo DstPtrInfo,
8830 const AAMDNodes &AAInfo) {
8831 // Check to see if we should lower the memset to stores first.
8832 // For cases within the target-specified limits, this is the best choice.
8833 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8834 if (ConstantSize) {
8835 // Memset with size zero? Just return the original chain.
8836 if (ConstantSize->isZero())
8837 return Chain;
8838
8839 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8840 ConstantSize->getZExtValue(), Alignment,
8841 isVol, false, DstPtrInfo, AAInfo);
8842
8843 if (Result.getNode())
8844 return Result;
8845 }
8846
8847 // Then check to see if we should lower the memset with target-specific
8848 // code. If the target chooses to do this, this is the next best.
8849 if (TSI) {
8850 SDValue Result = TSI->EmitTargetCodeForMemset(
8851 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
8852 if (Result.getNode())
8853 return Result;
8854 }
8855
8856 // If we really need inline code and the target declined to provide it,
8857 // use a (potentially long) sequence of loads and stores.
8858 if (AlwaysInline) {
8859 assert(ConstantSize && "AlwaysInline requires a constant size!");
8860 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8861 ConstantSize->getZExtValue(), Alignment,
8862 isVol, true, DstPtrInfo, AAInfo);
8863 assert(Result &&
8864 "getMemsetStores must return a valid sequence when AlwaysInline");
8865 return Result;
8866 }
8867
8869
8870 // Emit a library call.
8871 auto &Ctx = *getContext();
8872 const auto& DL = getDataLayout();
8873
8875 // FIXME: pass in SDLoc
8876 CLI.setDebugLoc(dl).setChain(Chain);
8877
8878 const char *BzeroName = getTargetLoweringInfo().getLibcallName(RTLIB::BZERO);
8879
8880 // Helper function to create an Entry from Node and Type.
8881 const auto CreateEntry = [](SDValue Node, Type *Ty) {
8883 Entry.Node = Node;
8884 Entry.Ty = Ty;
8885 return Entry;
8886 };
8887
8888 bool UseBZero = isNullConstant(Src) && BzeroName;
8889 // If zeroing out and bzero is present, use it.
8890 if (UseBZero) {
8892 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8893 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8894 CLI.setLibCallee(
8895 TLI->getLibcallCallingConv(RTLIB::BZERO), Type::getVoidTy(Ctx),
8896 getExternalSymbol(BzeroName, TLI->getPointerTy(DL)), std::move(Args));
8897 } else {
8899 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8900 Args.push_back(CreateEntry(Src, Src.getValueType().getTypeForEVT(Ctx)));
8901 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8902 CLI.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
8903 Dst.getValueType().getTypeForEVT(Ctx),
8904 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
8905 TLI->getPointerTy(DL)),
8906 std::move(Args));
8907 }
8908 bool LowersToMemset =
8909 TLI->getLibcallName(RTLIB::MEMSET) == StringRef("memset");
8910 // If we're going to use bzero, make sure not to tail call unless the
8911 // subsequent return doesn't need a value, as bzero doesn't return the first
8912 // arg unlike memset.
8913 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
8914 bool IsTailCall =
8915 CI && CI->isTailCall() &&
8916 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
8917 CLI.setDiscardResult().setTailCall(IsTailCall);
8918
8919 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8920 return CallResult.second;
8921}
8922
8925 Type *SizeTy, unsigned ElemSz,
8926 bool isTailCall,
8927 MachinePointerInfo DstPtrInfo) {
8928 // Emit a library call.
8931 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8932 Entry.Node = Dst;
8933 Args.push_back(Entry);
8934
8935 Entry.Ty = Type::getInt8Ty(*getContext());
8936 Entry.Node = Value;
8937 Args.push_back(Entry);
8938
8939 Entry.Ty = SizeTy;
8940 Entry.Node = Size;
8941 Args.push_back(Entry);
8942
8943 RTLIB::Libcall LibraryCall =
8945 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8946 report_fatal_error("Unsupported element size");
8947
8949 CLI.setDebugLoc(dl)
8950 .setChain(Chain)
8951 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8953 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8954 TLI->getPointerTy(getDataLayout())),
8955 std::move(Args))
8957 .setTailCall(isTailCall);
8958
8959 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8960 return CallResult.second;
8961}
8962
8963SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8964 SDVTList VTList, ArrayRef<SDValue> Ops,
8965 MachineMemOperand *MMO) {
8967 ID.AddInteger(MemVT.getRawBits());
8968 AddNodeIDNode(ID, Opcode, VTList, Ops);
8969 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8970 ID.AddInteger(MMO->getFlags());
8971 void* IP = nullptr;
8972 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8973 cast<AtomicSDNode>(E)->refineAlignment(MMO);
8974 return SDValue(E, 0);
8975 }
8976
8977 auto *N = newSDNode<AtomicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8978 VTList, MemVT, MMO);
8979 createOperands(N, Ops);
8980
8981 CSEMap.InsertNode(N, IP);
8982 InsertNode(N);
8983 return SDValue(N, 0);
8984}
8985
8987 EVT MemVT, SDVTList VTs, SDValue Chain,
8988 SDValue Ptr, SDValue Cmp, SDValue Swp,
8989 MachineMemOperand *MMO) {
8990 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
8992 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
8993
8994 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
8995 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8996}
8997
8998SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8999 SDValue Chain, SDValue Ptr, SDValue Val,
9000 MachineMemOperand *MMO) {
9001 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9002 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9003 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9004 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9005 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9006 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9007 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9008 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9009 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9010 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9011 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9012 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9013 Opcode == ISD::ATOMIC_STORE) &&
9014 "Invalid Atomic Op");
9015
9016 EVT VT = Val.getValueType();
9017
9018 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9019 getVTList(VT, MVT::Other);
9020 SDValue Ops[] = {Chain, Ptr, Val};
9021 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9022}
9023
9024SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9025 EVT VT, SDValue Chain, SDValue Ptr,
9026 MachineMemOperand *MMO) {
9027 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
9028
9029 SDVTList VTs = getVTList(VT, MVT::Other);
9030 SDValue Ops[] = {Chain, Ptr};
9031 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9032}
9033
9034/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9036 if (Ops.size() == 1)
9037 return Ops[0];
9038
9040 VTs.reserve(Ops.size());
9041 for (const SDValue &Op : Ops)
9042 VTs.push_back(Op.getValueType());
9043 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9044}
9045
9047 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9048 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9050 const AAMDNodes &AAInfo) {
9051 if (Size.hasValue() && !Size.getValue())
9053
9055 MachineMemOperand *MMO =
9056 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9057
9058 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9059}
9060
9062 SDVTList VTList,
9063 ArrayRef<SDValue> Ops, EVT MemVT,
9064 MachineMemOperand *MMO) {
9065 assert(
9066 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9067 Opcode == ISD::PREFETCH ||
9068 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9069 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9070 "Opcode is not a memory-accessing opcode!");
9071
9072 // Memoize the node unless it returns a glue result.
9074 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9076 AddNodeIDNode(ID, Opcode, VTList, Ops);
9077 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9078 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9079 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9080 ID.AddInteger(MMO->getFlags());
9081 ID.AddInteger(MemVT.getRawBits());
9082 void *IP = nullptr;
9083 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9084 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9085 return SDValue(E, 0);
9086 }
9087
9088 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9089 VTList, MemVT, MMO);
9090 createOperands(N, Ops);
9091
9092 CSEMap.InsertNode(N, IP);
9093 } else {
9094 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9095 VTList, MemVT, MMO);
9096 createOperands(N, Ops);
9097 }
9098 InsertNode(N);
9099 SDValue V(N, 0);
9100 NewSDValueDbgMsg(V, "Creating new node: ", this);
9101 return V;
9102}
9103
9105 SDValue Chain, int FrameIndex,
9106 int64_t Size, int64_t Offset) {
9107 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9108 const auto VTs = getVTList(MVT::Other);
9109 SDValue Ops[2] = {
9110 Chain,
9111 getFrameIndex(FrameIndex,
9112 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9113 true)};
9114
9116 AddNodeIDNode(ID, Opcode, VTs, Ops);
9117 ID.AddInteger(FrameIndex);
9118 ID.AddInteger(Size);
9119 ID.AddInteger(Offset);
9120 void *IP = nullptr;
9121 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9122 return SDValue(E, 0);
9123
9124 LifetimeSDNode *N = newSDNode<LifetimeSDNode>(
9125 Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs, Size, Offset);
9126 createOperands(N, Ops);
9127 CSEMap.InsertNode(N, IP);
9128 InsertNode(N);
9129 SDValue V(N, 0);
9130 NewSDValueDbgMsg(V, "Creating new node: ", this);
9131 return V;
9132}
9133
9135 uint64_t Guid, uint64_t Index,
9136 uint32_t Attr) {
9137 const unsigned Opcode = ISD::PSEUDO_PROBE;
9138 const auto VTs = getVTList(MVT::Other);
9139 SDValue Ops[] = {Chain};
9141 AddNodeIDNode(ID, Opcode, VTs, Ops);
9142 ID.AddInteger(Guid);
9143 ID.AddInteger(Index);
9144 void *IP = nullptr;
9145 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9146 return SDValue(E, 0);
9147
9148 auto *N = newSDNode<PseudoProbeSDNode>(
9149 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9150 createOperands(N, Ops);
9151 CSEMap.InsertNode(N, IP);
9152 InsertNode(N);
9153 SDValue V(N, 0);
9154 NewSDValueDbgMsg(V, "Creating new node: ", this);
9155 return V;
9156}
9157
9158/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9159/// MachinePointerInfo record from it. This is particularly useful because the
9160/// code generator has many cases where it doesn't bother passing in a
9161/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9163 SelectionDAG &DAG, SDValue Ptr,
9164 int64_t Offset = 0) {
9165 // If this is FI+Offset, we can model it.
9166 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
9168 FI->getIndex(), Offset);
9169
9170 // If this is (FI+Offset1)+Offset2, we can model it.
9171 if (Ptr.getOpcode() != ISD::ADD ||
9172 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
9173 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
9174 return Info;
9175
9176 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9178 DAG.getMachineFunction(), FI,
9179 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9180}
9181
9182/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9183/// MachinePointerInfo record from it. This is particularly useful because the
9184/// code generator has many cases where it doesn't bother passing in a
9185/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9187 SelectionDAG &DAG, SDValue Ptr,
9188 SDValue OffsetOp) {
9189 // If the 'Offset' value isn't a constant, we can't handle this.
9190 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
9191 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9192 if (OffsetOp.isUndef())
9193 return InferPointerInfo(Info, DAG, Ptr);
9194 return Info;
9195}
9196
9198 EVT VT, const SDLoc &dl, SDValue Chain,
9200 MachinePointerInfo PtrInfo, EVT MemVT,
9201 Align Alignment,
9202 MachineMemOperand::Flags MMOFlags,
9203 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9204 assert(Chain.getValueType() == MVT::Other &&
9205 "Invalid chain type");
9206
9207 MMOFlags |= MachineMemOperand::MOLoad;
9208 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9209 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9210 // clients.
9211 if (PtrInfo.V.isNull())
9212 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9213
9216 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9217 Alignment, AAInfo, Ranges);
9218 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9219}
9220
9222 EVT VT, const SDLoc &dl, SDValue Chain,
9223 SDValue Ptr, SDValue Offset, EVT MemVT,
9224 MachineMemOperand *MMO) {
9225 if (VT == MemVT) {
9226 ExtType = ISD::NON_EXTLOAD;
9227 } else if (ExtType == ISD::NON_EXTLOAD) {
9228 assert(VT == MemVT && "Non-extending load from different memory type!");
9229 } else {
9230 // Extending load.
9231 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9232 "Should only be an extending load, not truncating!");
9233 assert(VT.isInteger() == MemVT.isInteger() &&
9234 "Cannot convert from FP to Int or Int -> FP!");
9235 assert(VT.isVector() == MemVT.isVector() &&
9236 "Cannot use an ext load to convert to or from a vector!");
9237 assert((!VT.isVector() ||
9239 "Cannot use an ext load to change the number of vector elements!");
9240 }
9241
9242 bool Indexed = AM != ISD::UNINDEXED;
9243 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9244
9245 SDVTList VTs = Indexed ?
9246 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9247 SDValue Ops[] = { Chain, Ptr, Offset };
9249 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9250 ID.AddInteger(MemVT.getRawBits());
9251 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9252 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9253 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9254 ID.AddInteger(MMO->getFlags());
9255 void *IP = nullptr;
9256 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9257 cast<LoadSDNode>(E)->refineAlignment(MMO);
9258 return SDValue(E, 0);
9259 }
9260 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9261 ExtType, MemVT, MMO);
9262 createOperands(N, Ops);
9263
9264 CSEMap.InsertNode(N, IP);
9265 InsertNode(N);
9266 SDValue V(N, 0);
9267 NewSDValueDbgMsg(V, "Creating new node: ", this);
9268 return V;
9269}
9270
9273 MaybeAlign Alignment,
9274 MachineMemOperand::Flags MMOFlags,
9275 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9276 SDValue Undef = getUNDEF(Ptr.getValueType());
9277 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9278 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
9279}
9280
9283 SDValue Undef = getUNDEF(Ptr.getValueType());
9284 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9285 VT, MMO);
9286}
9287
9289 EVT VT, SDValue Chain, SDValue Ptr,
9290 MachinePointerInfo PtrInfo, EVT MemVT,
9291 MaybeAlign Alignment,
9292 MachineMemOperand::Flags MMOFlags,
9293 const AAMDNodes &AAInfo) {
9294 SDValue Undef = getUNDEF(Ptr.getValueType());
9295 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
9296 MemVT, Alignment, MMOFlags, AAInfo);
9297}
9298
9300 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
9301 MachineMemOperand *MMO) {
9302 SDValue Undef = getUNDEF(Ptr.getValueType());
9303 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
9304 MemVT, MMO);
9305}
9306
9310 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
9311 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9312 // Don't propagate the invariant or dereferenceable flags.
9313 auto MMOFlags =
9314 LD->getMemOperand()->getFlags() &
9316 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9317 LD->getChain(), Base, Offset, LD->getPointerInfo(),
9318 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
9319}
9320
9323 Align Alignment,
9324 MachineMemOperand::Flags MMOFlags,
9325 const AAMDNodes &AAInfo) {
9326 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9327
9328 MMOFlags |= MachineMemOperand::MOStore;
9329 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9330
9331 if (PtrInfo.V.isNull())
9332 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9333
9336 MachineMemOperand *MMO =
9337 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
9338 return getStore(Chain, dl, Val, Ptr, MMO);
9339}
9340
9343 assert(Chain.getValueType() == MVT::Other &&
9344 "Invalid chain type");
9345 EVT VT = Val.getValueType();
9346 SDVTList VTs = getVTList(MVT::Other);
9347 SDValue Undef = getUNDEF(Ptr.getValueType());
9348 SDValue Ops[] = { Chain, Val, Ptr, Undef };
9350 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9351 ID.AddInteger(VT.getRawBits());
9352 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9353 dl.getIROrder(), VTs, ISD::UNINDEXED, false, VT, MMO));
9354 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9355 ID.AddInteger(MMO->getFlags());
9356 void *IP = nullptr;
9357 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9358 cast<StoreSDNode>(E)->refineAlignment(MMO);
9359 return SDValue(E, 0);
9360 }
9361 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9362 ISD::UNINDEXED, false, VT, MMO);
9363 createOperands(N, Ops);
9364
9365 CSEMap.InsertNode(N, IP);
9366 InsertNode(N);
9367 SDValue V(N, 0);
9368 NewSDValueDbgMsg(V, "Creating new node: ", this);
9369 return V;
9370}
9371
9374 EVT SVT, Align Alignment,
9375 MachineMemOperand::Flags MMOFlags,
9376 const AAMDNodes &AAInfo) {
9377 assert(Chain.getValueType() == MVT::Other &&
9378 "Invalid chain type");
9379
9380 MMOFlags |= MachineMemOperand::MOStore;
9381 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9382
9383 if (PtrInfo.V.isNull())
9384 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9385
9388 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
9389 AAInfo);
9390 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
9391}
9392
9394 SDValue Ptr, EVT SVT,
9395 MachineMemOperand *MMO) {
9396 EVT VT = Val.getValueType();
9397
9398 assert(Chain.getValueType() == MVT::Other &&
9399 "Invalid chain type");
9400 if (VT == SVT)
9401 return getStore(Chain, dl, Val, Ptr, MMO);
9402
9404 "Should only be a truncating store, not extending!");
9405 assert(VT.isInteger() == SVT.isInteger() &&
9406 "Can't do FP-INT conversion!");
9407 assert(VT.isVector() == SVT.isVector() &&
9408 "Cannot use trunc store to convert to or from a vector!");
9409 assert((!VT.isVector() ||
9411 "Cannot use trunc store to change the number of vector elements!");
9412
9413 SDVTList VTs = getVTList(MVT::Other);
9414 SDValue Undef = getUNDEF(Ptr.getValueType());
9415 SDValue Ops[] = { Chain, Val, Ptr, Undef };
9417 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9418 ID.AddInteger(SVT.getRawBits());
9419 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9420 dl.getIROrder(), VTs, ISD::UNINDEXED, true, SVT, MMO));
9421 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9422 ID.AddInteger(MMO->getFlags());
9423 void *IP = nullptr;
9424 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9425 cast<StoreSDNode>(E)->refineAlignment(MMO);
9426 return SDValue(E, 0);
9427 }
9428 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9429 ISD::UNINDEXED, true, SVT, MMO);
9430 createOperands(N, Ops);
9431
9432 CSEMap.InsertNode(N, IP);
9433 InsertNode(N);
9434 SDValue V(N, 0);
9435 NewSDValueDbgMsg(V, "Creating new node: ", this);
9436 return V;
9437}
9438
9442 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
9443 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
9444 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
9445 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
9447 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9448 ID.AddInteger(ST->getMemoryVT().getRawBits());
9449 ID.AddInteger(ST->getRawSubclassData());
9450 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
9451 ID.AddInteger(ST->getMemOperand()->getFlags());
9452 void *IP = nullptr;
9453 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9454 return SDValue(E, 0);
9455
9456 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9457 ST->isTruncatingStore(), ST->getMemoryVT(),
9458 ST->getMemOperand());
9459 createOperands(N, Ops);
9460
9461 CSEMap.InsertNode(N, IP);
9462 InsertNode(N);
9463 SDValue V(N, 0);
9464 NewSDValueDbgMsg(V, "Creating new node: ", this);
9465 return V;
9466}
9467
9469 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
9470 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
9471 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
9472 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
9473 const MDNode *Ranges, bool IsExpanding) {
9474 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9475
9476 MMOFlags |= MachineMemOperand::MOLoad;
9477 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9478 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9479 // clients.
9480 if (PtrInfo.V.isNull())
9481 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9482
9485 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9486 Alignment, AAInfo, Ranges);
9487 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
9488 MMO, IsExpanding);
9489}
9490
9492 ISD::LoadExtType ExtType, EVT VT,
9493 const SDLoc &dl, SDValue Chain, SDValue Ptr,
9494 SDValue Offset, SDValue Mask, SDValue EVL,
9495 EVT MemVT, MachineMemOperand *MMO,
9496 bool IsExpanding) {
9497 bool Indexed = AM != ISD::UNINDEXED;
9498 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9499
9500 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9501 : getVTList(VT, MVT::Other);
9502 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
9504 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
9505 ID.AddInteger(MemVT.getRawBits());
9506 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
9507 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9508 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9509 ID.AddInteger(MMO->getFlags());
9510 void *IP = nullptr;
9511 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9512 cast<VPLoadSDNode>(E)->refineAlignment(MMO);
9513 return SDValue(E, 0);
9514 }
9515 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9516 ExtType, IsExpanding, MemVT, MMO);
9517 createOperands(N, Ops);
9518
9519 CSEMap.InsertNode(N, IP);
9520 InsertNode(N);
9521 SDValue V(N, 0);
9522 NewSDValueDbgMsg(V, "Creating new node: ", this);
9523 return V;
9524}
9525
9527 SDValue Ptr, SDValue Mask, SDValue EVL,
9528 MachinePointerInfo PtrInfo,
9529 MaybeAlign Alignment,
9530 MachineMemOperand::Flags MMOFlags,
9531 const AAMDNodes &AAInfo, const MDNode *Ranges,
9532 bool IsExpanding) {
9533 SDValue Undef = getUNDEF(Ptr.getValueType());
9534 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9535 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
9536 IsExpanding);
9537}
9538
9540 SDValue Ptr, SDValue Mask, SDValue EVL,
9541 MachineMemOperand *MMO, bool IsExpanding) {
9542 SDValue Undef = getUNDEF(Ptr.getValueType());
9543 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9544 Mask, EVL, VT, MMO, IsExpanding);
9545}
9546
9548 EVT VT, SDValue Chain, SDValue Ptr,
9549 SDValue Mask, SDValue EVL,
9550 MachinePointerInfo PtrInfo, EVT MemVT,
9551 MaybeAlign Alignment,
9552 MachineMemOperand::Flags MMOFlags,
9553 const AAMDNodes &AAInfo, bool IsExpanding) {
9554 SDValue Undef = getUNDEF(Ptr.getValueType());
9555 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9556 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
9557 IsExpanding);
9558}
9559
9561 EVT VT, SDValue Chain, SDValue Ptr,
9562 SDValue Mask, SDValue EVL, EVT MemVT,
9563 MachineMemOperand *MMO, bool IsExpanding) {
9564 SDValue Undef = getUNDEF(Ptr.getValueType());
9565 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9566 EVL, MemVT, MMO, IsExpanding);
9567}
9568
9572 auto *LD = cast<VPLoadSDNode>(OrigLoad);
9573 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9574 // Don't propagate the invariant or dereferenceable flags.
9575 auto MMOFlags =
9576 LD->getMemOperand()->getFlags() &
9578 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9579 LD->getChain(), Base, Offset, LD->getMask(),
9580 LD->getVectorLength(), LD->getPointerInfo(),
9581 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
9582 nullptr, LD->isExpandingLoad());
9583}
9584
9587 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
9588 ISD::MemIndexedMode AM, bool IsTruncating,
9589 bool IsCompressing) {
9590 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9591 bool Indexed = AM != ISD::UNINDEXED;
9592 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9593 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9594 : getVTList(MVT::Other);
9595 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
9597 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9598 ID.AddInteger(MemVT.getRawBits());
9599 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9600 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9601 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9602 ID.AddInteger(MMO->getFlags());
9603 void *IP = nullptr;
9604 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9605 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9606 return SDValue(E, 0);
9607 }
9608 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9609 IsTruncating, IsCompressing, MemVT, MMO);
9610 createOperands(N, Ops);
9611
9612 CSEMap.InsertNode(N, IP);
9613 InsertNode(N);
9614 SDValue V(N, 0);
9615 NewSDValueDbgMsg(V, "Creating new node: ", this);
9616 return V;
9617}
9618
9620 SDValue Val, SDValue Ptr, SDValue Mask,
9621 SDValue EVL, MachinePointerInfo PtrInfo,
9622 EVT SVT, Align Alignment,
9623 MachineMemOperand::Flags MMOFlags,
9624 const AAMDNodes &AAInfo,
9625 bool IsCompressing) {
9626 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9627
9628 MMOFlags |= MachineMemOperand::MOStore;
9629 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9630
9631 if (PtrInfo.V.isNull())
9632 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9633
9636 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
9637 AAInfo);
9638 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
9639 IsCompressing);
9640}
9641
9643 SDValue Val, SDValue Ptr, SDValue Mask,
9644 SDValue EVL, EVT SVT,
9645 MachineMemOperand *MMO,
9646 bool IsCompressing) {
9647 EVT VT = Val.getValueType();
9648
9649 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9650 if (VT == SVT)
9651 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
9652 EVL, VT, MMO, ISD::UNINDEXED,
9653 /*IsTruncating*/ false, IsCompressing);
9654
9656 "Should only be a truncating store, not extending!");
9657 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9658 assert(VT.isVector() == SVT.isVector() &&
9659 "Cannot use trunc store to convert to or from a vector!");
9660 assert((!VT.isVector() ||
9662 "Cannot use trunc store to change the number of vector elements!");
9663
9664 SDVTList VTs = getVTList(MVT::Other);
9665 SDValue Undef = getUNDEF(Ptr.getValueType());
9666 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
9668 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9669 ID.AddInteger(SVT.getRawBits());
9670 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9671 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9672 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9673 ID.AddInteger(MMO->getFlags());
9674 void *IP = nullptr;
9675 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9676 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9677 return SDValue(E, 0);
9678 }
9679 auto *N =
9680 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9681 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
9682 createOperands(N, Ops);
9683
9684 CSEMap.InsertNode(N, IP);
9685 InsertNode(N);
9686 SDValue V(N, 0);
9687 NewSDValueDbgMsg(V, "Creating new node: ", this);
9688 return V;
9689}
9690
9694 auto *ST = cast<VPStoreSDNode>(OrigStore);
9695 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
9696 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
9697 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
9698 Offset, ST->getMask(), ST->getVectorLength()};
9700 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9701 ID.AddInteger(ST->getMemoryVT().getRawBits());
9702 ID.AddInteger(ST->getRawSubclassData());
9703 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
9704 ID.AddInteger(ST->getMemOperand()->getFlags());
9705 void *IP = nullptr;
9706 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9707 return SDValue(E, 0);
9708
9709 auto *N = newSDNode<VPStoreSDNode>(
9710 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
9711 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
9712 createOperands(N, Ops);
9713
9714 CSEMap.InsertNode(N, IP);
9715 InsertNode(N);
9716 SDValue V(N, 0);
9717 NewSDValueDbgMsg(V, "Creating new node: ", this);
9718 return V;
9719}
9720
9722 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
9723 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
9724 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
9725 bool Indexed = AM != ISD::UNINDEXED;
9726 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9727
9728 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
9729 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9730 : getVTList(VT, MVT::Other);
9732 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
9733 ID.AddInteger(VT.getRawBits());
9734 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
9735 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9736 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9737
9738 void *IP = nullptr;
9739 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9740 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
9741 return SDValue(E, 0);
9742 }
9743
9744 auto *N =
9745 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
9746 ExtType, IsExpanding, MemVT, MMO);
9747 createOperands(N, Ops);
9748 CSEMap.InsertNode(N, IP);
9749 InsertNode(N);
9750 SDValue V(N, 0);
9751 NewSDValueDbgMsg(V, "Creating new node: ", this);
9752 return V;
9753}
9754
9756 SDValue Ptr, SDValue Stride,
9757 SDValue Mask, SDValue EVL,
9758 MachineMemOperand *MMO,
9759 bool IsExpanding) {
9760 SDValue Undef = getUNDEF(Ptr.getValueType());
9762 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
9763}
9764
9766 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
9767 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
9768 MachineMemOperand *MMO, bool IsExpanding) {
9769 SDValue Undef = getUNDEF(Ptr.getValueType());
9770 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
9771 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
9772}
9773
9775 SDValue Val, SDValue Ptr,
9776 SDValue Offset, SDValue Stride,
9777 SDValue Mask, SDValue EVL, EVT MemVT,
9778 MachineMemOperand *MMO,
9780 bool IsTruncating, bool IsCompressing) {
9781 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9782 bool Indexed = AM != ISD::UNINDEXED;
9783 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9784 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9785 : getVTList(MVT::Other);
9786 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
9788 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9789 ID.AddInteger(MemVT.getRawBits());
9790 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9791 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9792 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9793 void *IP = nullptr;
9794 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9795 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9796 return SDValue(E, 0);
9797 }
9798 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9799 VTs, AM, IsTruncating,
9800 IsCompressing, MemVT, MMO);
9801 createOperands(N, Ops);
9802
9803 CSEMap.InsertNode(N, IP);
9804 InsertNode(N);
9805 SDValue V(N, 0);
9806 NewSDValueDbgMsg(V, "Creating new node: ", this);
9807 return V;
9808}
9809
9811 SDValue Val, SDValue Ptr,
9812 SDValue Stride, SDValue Mask,
9813 SDValue EVL, EVT SVT,
9814 MachineMemOperand *MMO,
9815 bool IsCompressing) {
9816 EVT VT = Val.getValueType();
9817
9818 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9819 if (VT == SVT)
9820 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
9821 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
9822 /*IsTruncating*/ false, IsCompressing);
9823
9825 "Should only be a truncating store, not extending!");
9826 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9827 assert(VT.isVector() == SVT.isVector() &&
9828 "Cannot use trunc store to convert to or from a vector!");
9829 assert((!VT.isVector() ||
9831 "Cannot use trunc store to change the number of vector elements!");
9832
9833 SDVTList VTs = getVTList(MVT::Other);
9834 SDValue Undef = getUNDEF(Ptr.getValueType());
9835 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
9837 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9838 ID.AddInteger(SVT.getRawBits());
9839 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9840 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9841 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9842 void *IP = nullptr;
9843 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9844 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9845 return SDValue(E, 0);
9846 }
9847 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9848 VTs, ISD::UNINDEXED, true,
9849 IsCompressing, SVT, MMO);
9850 createOperands(N, Ops);
9851
9852 CSEMap.InsertNode(N, IP);
9853 InsertNode(N);
9854 SDValue V(N, 0);
9855 NewSDValueDbgMsg(V, "Creating new node: ", this);
9856 return V;
9857}
9858
9861 ISD::MemIndexType IndexType) {
9862 assert(Ops.size() == 6 && "Incompatible number of operands");
9863
9865 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
9866 ID.AddInteger(VT.getRawBits());
9867 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
9868 dl.getIROrder(), VTs, VT, MMO, IndexType));
9869 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9870 ID.AddInteger(MMO->getFlags());
9871 void *IP = nullptr;
9872 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9873 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
9874 return SDValue(E, 0);
9875 }
9876
9877 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9878 VT, MMO, IndexType);
9879 createOperands(N, Ops);
9880
9881 assert(N->getMask().getValueType().getVectorElementCount() ==
9882 N->getValueType(0).getVectorElementCount() &&
9883 "Vector width mismatch between mask and data");
9884 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9885 N->getValueType(0).getVectorElementCount().isScalable() &&
9886 "Scalable flags of index and data do not match");
9888 N->getIndex().getValueType().getVectorElementCount(),
9889 N->getValueType(0).getVectorElementCount()) &&
9890 "Vector width mismatch between index and data");
9891 assert(isa<ConstantSDNode>(N->getScale()) &&
9892 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9893 "Scale should be a constant power of 2");
9894
9895 CSEMap.InsertNode(N, IP);
9896 InsertNode(N);
9897 SDValue V(N, 0);
9898 NewSDValueDbgMsg(V, "Creating new node: ", this);
9899 return V;
9900}
9901
9904 MachineMemOperand *MMO,
9905 ISD::MemIndexType IndexType) {
9906 assert(Ops.size() == 7 && "Incompatible number of operands");
9907
9909 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
9910 ID.AddInteger(VT.getRawBits());
9911 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
9912 dl.getIROrder(), VTs, VT, MMO, IndexType));
9913 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9914 ID.AddInteger(MMO->getFlags());
9915 void *IP = nullptr;
9916 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9917 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
9918 return SDValue(E, 0);
9919 }
9920 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9921 VT, MMO, IndexType);
9922 createOperands(N, Ops);
9923
9924 assert(N->getMask().getValueType().getVectorElementCount() ==
9925 N->getValue().getValueType().getVectorElementCount() &&
9926 "Vector width mismatch between mask and data");
9927 assert(
9928 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9929 N->getValue().getValueType().getVectorElementCount().isScalable() &&
9930 "Scalable flags of index and data do not match");
9932 N->getIndex().getValueType().getVectorElementCount(),
9933 N->getValue().getValueType().getVectorElementCount()) &&
9934 "Vector width mismatch between index and data");
9935 assert(isa<ConstantSDNode>(N->getScale()) &&
9936 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9937 "Scale should be a constant power of 2");
9938
9939 CSEMap.InsertNode(N, IP);
9940 InsertNode(N);
9941 SDValue V(N, 0);
9942 NewSDValueDbgMsg(V, "Creating new node: ", this);
9943 return V;
9944}
9945
9948 SDValue PassThru, EVT MemVT,
9949 MachineMemOperand *MMO,
9951 ISD::LoadExtType ExtTy, bool isExpanding) {
9952 bool Indexed = AM != ISD::UNINDEXED;
9953 assert((Indexed || Offset.isUndef()) &&
9954 "Unindexed masked load with an offset!");
9955 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
9956 : getVTList(VT, MVT::Other);
9957 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
9959 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
9960 ID.AddInteger(MemVT.getRawBits());
9961 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
9962 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
9963 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9964 ID.AddInteger(MMO->getFlags());
9965 void *IP = nullptr;
9966 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9967 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
9968 return SDValue(E, 0);
9969 }
9970 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9971 AM, ExtTy, isExpanding, MemVT, MMO);
9972 createOperands(N, Ops);
9973
9974 CSEMap.InsertNode(N, IP);
9975 InsertNode(N);
9976 SDValue V(N, 0);
9977 NewSDValueDbgMsg(V, "Creating new node: ", this);
9978 return V;
9979}
9980
9984 MaskedLoadSDNode *LD = cast<MaskedLoadSDNode>(OrigLoad);
9985 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
9986 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
9987 Offset, LD->getMask(), LD->getPassThru(),
9988 LD->getMemoryVT(), LD->getMemOperand(), AM,
9989 LD->getExtensionType(), LD->isExpandingLoad());
9990}
9991
9994 SDValue Mask, EVT MemVT,
9995 MachineMemOperand *MMO,
9996 ISD::MemIndexedMode AM, bool IsTruncating,
9997 bool IsCompressing) {
9998 assert(Chain.getValueType() == MVT::Other &&
9999 "Invalid chain type");
10000 bool Indexed = AM != ISD::UNINDEXED;
10001 assert((Indexed || Offset.isUndef()) &&
10002 "Unindexed masked store with an offset!");
10003 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10004 : getVTList(MVT::Other);
10005 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10007 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
10008 ID.AddInteger(MemVT.getRawBits());
10009 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10010 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10011 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10012 ID.AddInteger(MMO->getFlags());
10013 void *IP = nullptr;
10014 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10015 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10016 return SDValue(E, 0);
10017 }
10018 auto *N =
10019 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10020 IsTruncating, IsCompressing, MemVT, MMO);
10021 createOperands(N, Ops);
10022
10023 CSEMap.InsertNode(N, IP);
10024 InsertNode(N);
10025 SDValue V(N, 0);
10026 NewSDValueDbgMsg(V, "Creating new node: ", this);
10027 return V;
10028}
10029
10033 MaskedStoreSDNode *ST = cast<MaskedStoreSDNode>(OrigStore);
10034 assert(ST->getOffset().isUndef() &&
10035 "Masked store is already a indexed store!");
10036 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10037 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10038 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10039}
10040
10043 MachineMemOperand *MMO,
10044 ISD::MemIndexType IndexType,
10045 ISD::LoadExtType ExtTy) {
10046 assert(Ops.size() == 6 && "Incompatible number of operands");
10047
10049 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
10050 ID.AddInteger(MemVT.getRawBits());
10051 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10052 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10053 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10054 ID.AddInteger(MMO->getFlags());
10055 void *IP = nullptr;
10056 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10057 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10058 return SDValue(E, 0);
10059 }
10060
10061 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10062 VTs, MemVT, MMO, IndexType, ExtTy);
10063 createOperands(N, Ops);
10064
10065 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10066 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10067 assert(N->getMask().getValueType().getVectorElementCount() ==
10068 N->getValueType(0).getVectorElementCount() &&
10069 "Vector width mismatch between mask and data");
10070 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10071 N->getValueType(0).getVectorElementCount().isScalable() &&
10072 "Scalable flags of index and data do not match");
10074 N->getIndex().getValueType().getVectorElementCount(),
10075 N->getValueType(0).getVectorElementCount()) &&
10076 "Vector width mismatch between index and data");
10077 assert(isa<ConstantSDNode>(N->getScale()) &&
10078 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10079 "Scale should be a constant power of 2");
10080
10081 CSEMap.InsertNode(N, IP);
10082 InsertNode(N);
10083 SDValue V(N, 0);
10084 NewSDValueDbgMsg(V, "Creating new node: ", this);
10085 return V;
10086}
10087
10090 MachineMemOperand *MMO,
10091 ISD::MemIndexType IndexType,
10092 bool IsTrunc) {
10093 assert(Ops.size() == 6 && "Incompatible number of operands");
10094
10096 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
10097 ID.AddInteger(MemVT.getRawBits());
10098 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10099 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10100 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10101 ID.AddInteger(MMO->getFlags());
10102 void *IP = nullptr;
10103 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10104 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10105 return SDValue(E, 0);
10106 }
10107
10108 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10109 VTs, MemVT, MMO, IndexType, IsTrunc);
10110 createOperands(N, Ops);
10111
10112 assert(N->getMask().getValueType().getVectorElementCount() ==
10113 N->getValue().getValueType().getVectorElementCount() &&
10114 "Vector width mismatch between mask and data");
10115 assert(
10116 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10117 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10118 "Scalable flags of index and data do not match");
10120 N->getIndex().getValueType().getVectorElementCount(),
10121 N->getValue().getValueType().getVectorElementCount()) &&
10122 "Vector width mismatch between index and data");
10123 assert(isa<ConstantSDNode>(N->getScale()) &&
10124 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10125 "Scale should be a constant power of 2");
10126
10127 CSEMap.InsertNode(N, IP);
10128 InsertNode(N);
10129 SDValue V(N, 0);
10130 NewSDValueDbgMsg(V, "Creating new node: ", this);
10131 return V;
10132}
10133
10135 const SDLoc &dl, ArrayRef<SDValue> Ops,
10136 MachineMemOperand *MMO,
10137 ISD::MemIndexType IndexType) {
10138 assert(Ops.size() == 7 && "Incompatible number of operands");
10139
10142 ID.AddInteger(MemVT.getRawBits());
10143 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10144 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10145 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10146 ID.AddInteger(MMO->getFlags());
10147 void *IP = nullptr;
10148 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10149 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10150 return SDValue(E, 0);
10151 }
10152
10153 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10154 VTs, MemVT, MMO, IndexType);
10155 createOperands(N, Ops);
10156
10157 assert(N->getMask().getValueType().getVectorElementCount() ==
10158 N->getIndex().getValueType().getVectorElementCount() &&
10159 "Vector width mismatch between mask and data");
10160 assert(isa<ConstantSDNode>(N->getScale()) &&
10161 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10162 "Scale should be a constant power of 2");
10163 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10164
10165 CSEMap.InsertNode(N, IP);
10166 InsertNode(N);
10167 SDValue V(N, 0);
10168 NewSDValueDbgMsg(V, "Creating new node: ", this);
10169 return V;
10170}
10171
10173 EVT MemVT, MachineMemOperand *MMO) {
10174 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10175 SDVTList VTs = getVTList(MVT::Other);
10176 SDValue Ops[] = {Chain, Ptr};
10179 ID.AddInteger(MemVT.getRawBits());
10180 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10181 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10182 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10183 ID.AddInteger(MMO->getFlags());
10184 void *IP = nullptr;
10185 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10186 return SDValue(E, 0);
10187
10188 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10189 dl.getDebugLoc(), VTs, MemVT, MMO);
10190 createOperands(N, Ops);
10191
10192 CSEMap.InsertNode(N, IP);
10193 InsertNode(N);
10194 SDValue V(N, 0);
10195 NewSDValueDbgMsg(V, "Creating new node: ", this);
10196 return V;
10197}
10198
10200 EVT MemVT, MachineMemOperand *MMO) {
10201 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10202 SDVTList VTs = getVTList(MVT::Other);
10203 SDValue Ops[] = {Chain, Ptr};
10206 ID.AddInteger(MemVT.getRawBits());
10207 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10208 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10209 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10210 ID.AddInteger(MMO->getFlags());
10211 void *IP = nullptr;
10212 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10213 return SDValue(E, 0);
10214
10215 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10216 dl.getDebugLoc(), VTs, MemVT, MMO);
10217 createOperands(N, Ops);
10218
10219 CSEMap.InsertNode(N, IP);
10220 InsertNode(N);
10221 SDValue V(N, 0);
10222 NewSDValueDbgMsg(V, "Creating new node: ", this);
10223 return V;
10224}
10225
10227 // select undef, T, F --> T (if T is a constant), otherwise F
10228 // select, ?, undef, F --> F
10229 // select, ?, T, undef --> T
10230 if (Cond.isUndef())
10231 return isConstantValueOfAnyType(T) ? T : F;
10232 if (T.isUndef())
10233 return F;
10234 if (F.isUndef())
10235 return T;
10236
10237 // select true, T, F --> T
10238 // select false, T, F --> F
10239 if (auto C = isBoolConstant(Cond, /*AllowTruncation=*/true))
10240 return *C ? T : F;
10241
10242 // select ?, T, T --> T
10243 if (T == F)
10244 return T;
10245
10246 return SDValue();
10247}
10248
10250 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10251 if (X.isUndef())
10252 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10253 // shift X, undef --> undef (because it may shift by the bitwidth)
10254 if (Y.isUndef())
10255 return getUNDEF(X.getValueType());
10256
10257 // shift 0, Y --> 0
10258 // shift X, 0 --> X
10260 return X;
10261
10262 // shift X, C >= bitwidth(X) --> undef
10263 // All vector elements must be too big (or undef) to avoid partial undefs.
10264 auto isShiftTooBig = [X](ConstantSDNode *Val) {
10265 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
10266 };
10267 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
10268 return getUNDEF(X.getValueType());
10269
10270 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
10271 if (X.getValueType().getScalarType() == MVT::i1)
10272 return X;
10273
10274 return SDValue();
10275}
10276
10278 SDNodeFlags Flags) {
10279 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
10280 // (an undef operand can be chosen to be Nan/Inf), then the result of this
10281 // operation is poison. That result can be relaxed to undef.
10282 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10283 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10284 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10285 (YC && YC->getValueAPF().isNaN());
10286 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10287 (YC && YC->getValueAPF().isInfinity());
10288
10289 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10290 return getUNDEF(X.getValueType());
10291
10292 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10293 return getUNDEF(X.getValueType());
10294
10295 if (!YC)
10296 return SDValue();
10297
10298 // X + -0.0 --> X
10299 if (Opcode == ISD::FADD)
10300 if (YC->getValueAPF().isNegZero())
10301 return X;
10302
10303 // X - +0.0 --> X
10304 if (Opcode == ISD::FSUB)
10305 if (YC->getValueAPF().isPosZero())
10306 return X;
10307
10308 // X * 1.0 --> X
10309 // X / 1.0 --> X
10310 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
10311 if (YC->getValueAPF().isExactlyValue(1.0))
10312 return X;
10313
10314 // X * 0.0 --> 0.0
10315 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
10316 if (YC->getValueAPF().isZero())
10317 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
10318
10319 return SDValue();
10320}
10321
10323 SDValue Ptr, SDValue SV, unsigned Align) {
10324 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
10325 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
10326}
10327
10328SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10329 ArrayRef<SDUse> Ops) {
10330 switch (Ops.size()) {
10331 case 0: return getNode(Opcode, DL, VT);
10332 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
10333 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
10334 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
10335 default: break;
10336 }
10337
10338 // Copy from an SDUse array into an SDValue array for use with
10339 // the regular getNode logic.
10340 SmallVector<SDValue, 8> NewOps(Ops);
10341 return getNode(Opcode, DL, VT, NewOps);
10342}
10343
10344SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10345 ArrayRef<SDValue> Ops) {
10346 SDNodeFlags Flags;
10347 if (Inserter)
10348 Flags = Inserter->getFlags();
10349 return getNode(Opcode, DL, VT, Ops, Flags);
10350}
10351
10352SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10353 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10354 unsigned NumOps = Ops.size();
10355 switch (NumOps) {
10356 case 0: return getNode(Opcode, DL, VT);
10357 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
10358 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
10359 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
10360 default: break;
10361 }
10362
10363#ifndef NDEBUG
10364 for (const auto &Op : Ops)
10365 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10366 "Operand is DELETED_NODE!");
10367#endif
10368
10369 switch (Opcode) {
10370 default: break;
10371 case ISD::BUILD_VECTOR:
10372 // Attempt to simplify BUILD_VECTOR.
10373 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
10374 return V;
10375 break;
10377 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
10378 return V;
10379 break;
10380 case ISD::SELECT_CC:
10381 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
10382 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
10383 "LHS and RHS of condition must have same type!");
10384 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10385 "True and False arms of SelectCC must have same type!");
10386 assert(Ops[2].getValueType() == VT &&
10387 "select_cc node must be of same type as true and false value!");
10388 assert((!Ops[0].getValueType().isVector() ||
10389 Ops[0].getValueType().getVectorElementCount() ==
10390 VT.getVectorElementCount()) &&
10391 "Expected select_cc with vector result to have the same sized "
10392 "comparison type!");
10393 break;
10394 case ISD::BR_CC:
10395 assert(NumOps == 5 && "BR_CC takes 5 operands!");
10396 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10397 "LHS/RHS of comparison should match types!");
10398 break;
10399 case ISD::VP_ADD:
10400 case ISD::VP_SUB:
10401 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
10402 if (VT.getScalarType() == MVT::i1)
10403 Opcode = ISD::VP_XOR;
10404 break;
10405 case ISD::VP_MUL:
10406 // If it is VP_MUL mask operation then turn it to VP_AND
10407 if (VT.getScalarType() == MVT::i1)
10408 Opcode = ISD::VP_AND;
10409 break;
10410 case ISD::VP_REDUCE_MUL:
10411 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
10412 if (VT == MVT::i1)
10413 Opcode = ISD::VP_REDUCE_AND;
10414 break;
10415 case ISD::VP_REDUCE_ADD:
10416 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
10417 if (VT == MVT::i1)
10418 Opcode = ISD::VP_REDUCE_XOR;
10419 break;
10420 case ISD::VP_REDUCE_SMAX:
10421 case ISD::VP_REDUCE_UMIN:
10422 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
10423 // VP_REDUCE_AND.
10424 if (VT == MVT::i1)
10425 Opcode = ISD::VP_REDUCE_AND;
10426 break;
10427 case ISD::VP_REDUCE_SMIN:
10428 case ISD::VP_REDUCE_UMAX:
10429 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
10430 // VP_REDUCE_OR.
10431 if (VT == MVT::i1)
10432 Opcode = ISD::VP_REDUCE_OR;
10433 break;
10434 }
10435
10436 // Memoize nodes.
10437 SDNode *N;
10438 SDVTList VTs = getVTList(VT);
10439
10440 if (VT != MVT::Glue) {
10442 AddNodeIDNode(ID, Opcode, VTs, Ops);
10443 void *IP = nullptr;
10444
10445 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10446 E->intersectFlagsWith(Flags);
10447 return SDValue(E, 0);
10448 }
10449
10450 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10451 createOperands(N, Ops);
10452
10453 CSEMap.InsertNode(N, IP);
10454 } else {
10455 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10456 createOperands(N, Ops);
10457 }
10458
10459 N->setFlags(Flags);
10460 InsertNode(N);
10461 SDValue V(N, 0);
10462 NewSDValueDbgMsg(V, "Creating new node: ", this);
10463 return V;
10464}
10465
10466SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10467 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
10468 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
10469}
10470
10471SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10472 ArrayRef<SDValue> Ops) {
10473 SDNodeFlags Flags;
10474 if (Inserter)
10475 Flags = Inserter->getFlags();
10476 return getNode(Opcode, DL, VTList, Ops, Flags);
10477}
10478
10479SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10480 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10481 if (VTList.NumVTs == 1)
10482 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
10483
10484#ifndef NDEBUG
10485 for (const auto &Op : Ops)
10486 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10487 "Operand is DELETED_NODE!");
10488#endif
10489
10490 switch (Opcode) {
10491 case ISD::SADDO:
10492 case ISD::UADDO:
10493 case ISD::SSUBO:
10494 case ISD::USUBO: {
10495 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10496 "Invalid add/sub overflow op!");
10497 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
10498 Ops[0].getValueType() == Ops[1].getValueType() &&
10499 Ops[0].getValueType() == VTList.VTs[0] &&
10500 "Binary operator types must match!");
10501 SDValue N1 = Ops[0], N2 = Ops[1];
10502 canonicalizeCommutativeBinop(Opcode, N1, N2);
10503
10504 // (X +- 0) -> X with zero-overflow.
10505 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
10506 /*AllowTruncation*/ true);
10507 if (N2CV && N2CV->isZero()) {
10508 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
10509 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
10510 }
10511
10512 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
10513 VTList.VTs[1].getScalarType() == MVT::i1) {
10514 SDValue F1 = getFreeze(N1);
10515 SDValue F2 = getFreeze(N2);
10516 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
10517 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
10518 return getNode(ISD::MERGE_VALUES, DL, VTList,
10519 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
10520 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
10521 Flags);
10522 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
10523 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
10524 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
10525 return getNode(ISD::MERGE_VALUES, DL, VTList,
10526 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
10527 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
10528 Flags);
10529 }
10530 }
10531 break;
10532 }
10533 case ISD::SADDO_CARRY:
10534 case ISD::UADDO_CARRY:
10535 case ISD::SSUBO_CARRY:
10536 case ISD::USUBO_CARRY:
10537 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
10538 "Invalid add/sub overflow op!");
10539 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
10540 Ops[0].getValueType() == Ops[1].getValueType() &&
10541 Ops[0].getValueType() == VTList.VTs[0] &&
10542 Ops[2].getValueType() == VTList.VTs[1] &&
10543 "Binary operator types must match!");
10544 break;
10545 case ISD::SMUL_LOHI:
10546 case ISD::UMUL_LOHI: {
10547 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
10548 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
10549 VTList.VTs[0] == Ops[0].getValueType() &&
10550 VTList.VTs[0] == Ops[1].getValueType() &&
10551 "Binary operator types must match!");
10552 // Constant fold.
10553 ConstantSDNode *LHS = dyn_cast<ConstantSDNode>(Ops[0]);
10554 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ops[1]);
10555 if (LHS && RHS) {
10556 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
10557 unsigned OutWidth = Width * 2;
10558 APInt Val = LHS->getAPIntValue();
10559 APInt Mul = RHS->getAPIntValue();
10560 if (Opcode == ISD::SMUL_LOHI) {
10561 Val = Val.sext(OutWidth);
10562 Mul = Mul.sext(OutWidth);
10563 } else {
10564 Val = Val.zext(OutWidth);
10565 Mul = Mul.zext(OutWidth);
10566 }
10567 Val *= Mul;
10568
10569 SDValue Hi =
10570 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
10571 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
10572 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
10573 }
10574 break;
10575 }
10576 case ISD::FFREXP: {
10577 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
10578 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
10579 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
10580
10581 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Ops[0])) {
10582 int FrexpExp;
10583 APFloat FrexpMant =
10584 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
10585 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
10586 SDValue Result1 =
10587 getConstant(FrexpMant.isFinite() ? FrexpExp : 0, DL, VTList.VTs[1]);
10588 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
10589 }
10590
10591 break;
10592 }
10594 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10595 "Invalid STRICT_FP_EXTEND!");
10596 assert(VTList.VTs[0].isFloatingPoint() &&
10597 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
10598 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
10599 "STRICT_FP_EXTEND result type should be vector iff the operand "
10600 "type is vector!");
10601 assert((!VTList.VTs[0].isVector() ||
10602 VTList.VTs[0].getVectorElementCount() ==
10603 Ops[1].getValueType().getVectorElementCount()) &&
10604 "Vector element count mismatch!");
10605 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
10606 "Invalid fpext node, dst <= src!");
10607 break;
10609 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
10610 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
10611 "STRICT_FP_ROUND result type should be vector iff the operand "
10612 "type is vector!");
10613 assert((!VTList.VTs[0].isVector() ||
10614 VTList.VTs[0].getVectorElementCount() ==
10615 Ops[1].getValueType().getVectorElementCount()) &&
10616 "Vector element count mismatch!");
10617 assert(VTList.VTs[0].isFloatingPoint() &&
10618 Ops[1].getValueType().isFloatingPoint() &&
10619 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
10620 Ops[2].getOpcode() == ISD::TargetConstant &&
10621 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
10622 "Invalid STRICT_FP_ROUND!");
10623 break;
10624#if 0
10625 // FIXME: figure out how to safely handle things like
10626 // int foo(int x) { return 1 << (x & 255); }
10627 // int bar() { return foo(256); }
10628 case ISD::SRA_PARTS:
10629 case ISD::SRL_PARTS:
10630 case ISD::SHL_PARTS:
10631 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
10632 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
10633 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10634 else if (N3.getOpcode() == ISD::AND)
10635 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
10636 // If the and is only masking out bits that cannot effect the shift,
10637 // eliminate the and.
10638 unsigned NumBits = VT.getScalarSizeInBits()*2;
10639 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
10640 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10641 }
10642 break;
10643#endif
10644 }
10645
10646 // Memoize the node unless it returns a glue result.
10647 SDNode *N;
10648 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
10650 AddNodeIDNode(ID, Opcode, VTList, Ops);
10651 void *IP = nullptr;
10652 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10653 E->intersectFlagsWith(Flags);
10654 return SDValue(E, 0);
10655 }
10656
10657 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10658 createOperands(N, Ops);
10659 CSEMap.InsertNode(N, IP);
10660 } else {
10661 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10662 createOperands(N, Ops);
10663 }
10664
10665 N->setFlags(Flags);
10666 InsertNode(N);
10667 SDValue V(N, 0);
10668 NewSDValueDbgMsg(V, "Creating new node: ", this);
10669 return V;
10670}
10671
10672SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10673 SDVTList VTList) {
10674 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
10675}
10676
10677SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10678 SDValue N1) {
10679 SDValue Ops[] = { N1 };
10680 return getNode(Opcode, DL, VTList, Ops);
10681}
10682
10683SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10684 SDValue N1, SDValue N2) {
10685 SDValue Ops[] = { N1, N2 };
10686 return getNode(Opcode, DL, VTList, Ops);
10687}
10688
10689SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10690 SDValue N1, SDValue N2, SDValue N3) {
10691 SDValue Ops[] = { N1, N2, N3 };
10692 return getNode(Opcode, DL, VTList, Ops);
10693}
10694
10695SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10696 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
10697 SDValue Ops[] = { N1, N2, N3, N4 };
10698 return getNode(Opcode, DL, VTList, Ops);
10699}
10700
10701SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10702 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
10703 SDValue N5) {
10704 SDValue Ops[] = { N1, N2, N3, N4, N5 };
10705 return getNode(Opcode, DL, VTList, Ops);
10706}
10707
10709 if (!VT.isExtended())
10710 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
10711
10712 return makeVTList(&(*EVTs.insert(VT).first), 1);
10713}
10714
10717 ID.AddInteger(2U);
10718 ID.AddInteger(VT1.getRawBits());
10719 ID.AddInteger(VT2.getRawBits());
10720
10721 void *IP = nullptr;
10722 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10723 if (!Result) {
10724 EVT *Array = Allocator.Allocate<EVT>(2);
10725 Array[0] = VT1;
10726 Array[1] = VT2;
10727 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
10728 VTListMap.InsertNode(Result, IP);
10729 }
10730 return Result->getSDVTList();
10731}
10732
10735 ID.AddInteger(3U);
10736 ID.AddInteger(VT1.getRawBits());
10737 ID.AddInteger(VT2.getRawBits());
10738 ID.AddInteger(VT3.getRawBits());
10739
10740 void *IP = nullptr;
10741 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10742 if (!Result) {
10743 EVT *Array = Allocator.Allocate<EVT>(3);
10744 Array[0] = VT1;
10745 Array[1] = VT2;
10746 Array[2] = VT3;
10747 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
10748 VTListMap.InsertNode(Result, IP);
10749 }
10750 return Result->getSDVTList();
10751}
10752
10755 ID.AddInteger(4U);
10756 ID.AddInteger(VT1.getRawBits());
10757 ID.AddInteger(VT2.getRawBits());
10758 ID.AddInteger(VT3.getRawBits());
10759 ID.AddInteger(VT4.getRawBits());
10760
10761 void *IP = nullptr;
10762 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10763 if (!Result) {
10764 EVT *Array = Allocator.Allocate<EVT>(4);
10765 Array[0] = VT1;
10766 Array[1] = VT2;
10767 Array[2] = VT3;
10768 Array[3] = VT4;
10769 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
10770 VTListMap.InsertNode(Result, IP);
10771 }
10772 return Result->getSDVTList();
10773}
10774
10776 unsigned NumVTs = VTs.size();
10778 ID.AddInteger(NumVTs);
10779 for (unsigned index = 0; index < NumVTs; index++) {
10780 ID.AddInteger(VTs[index].getRawBits());
10781 }
10782
10783 void *IP = nullptr;
10784 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10785 if (!Result) {
10786 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
10787 llvm::copy(VTs, Array);
10788 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
10789 VTListMap.InsertNode(Result, IP);
10790 }
10791 return Result->getSDVTList();
10792}
10793
10794
10795/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
10796/// specified operands. If the resultant node already exists in the DAG,
10797/// this does not modify the specified node, instead it returns the node that
10798/// already exists. If the resultant node does not exist in the DAG, the
10799/// input node is returned. As a degenerate case, if you specify the same
10800/// input operands as the node already has, the input node is returned.
10802 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
10803
10804 // Check to see if there is no change.
10805 if (Op == N->getOperand(0)) return N;
10806
10807 // See if the modified node already exists.
10808 void *InsertPos = nullptr;
10809 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
10810 return Existing;
10811
10812 // Nope it doesn't. Remove the node from its current place in the maps.
10813 if (InsertPos)
10814 if (!RemoveNodeFromCSEMaps(N))
10815 InsertPos = nullptr;
10816
10817 // Now we update the operands.
10818 N->OperandList[0].set(Op);
10819
10821 // If this gets put into a CSE map, add it.
10822 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10823 return N;
10824}
10825
10827 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
10828
10829 // Check to see if there is no change.
10830 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
10831 return N; // No operands changed, just return the input node.
10832
10833 // See if the modified node already exists.
10834 void *InsertPos = nullptr;
10835 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
10836 return Existing;
10837
10838 // Nope it doesn't. Remove the node from its current place in the maps.
10839 if (InsertPos)
10840 if (!RemoveNodeFromCSEMaps(N))
10841 InsertPos = nullptr;
10842
10843 // Now we update the operands.
10844 if (N->OperandList[0] != Op1)
10845 N->OperandList[0].set(Op1);
10846 if (N->OperandList[1] != Op2)
10847 N->OperandList[1].set(Op2);
10848
10850 // If this gets put into a CSE map, add it.
10851 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10852 return N;
10853}
10854
10857 SDValue Ops[] = { Op1, Op2, Op3 };
10858 return UpdateNodeOperands(N, Ops);
10859}
10860
10863 SDValue Op3, SDValue Op4) {
10864 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
10865 return UpdateNodeOperands(N, Ops);
10866}
10867
10870 SDValue Op3, SDValue Op4, SDValue Op5) {
10871 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
10872 return UpdateNodeOperands(N, Ops);
10873}
10874
10877 unsigned NumOps = Ops.size();
10878 assert(N->getNumOperands() == NumOps &&
10879 "Update with wrong number of operands");
10880
10881 // If no operands changed just return the input node.
10882 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
10883 return N;
10884
10885 // See if the modified node already exists.
10886 void *InsertPos = nullptr;
10887 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
10888 return Existing;
10889
10890 // Nope it doesn't. Remove the node from its current place in the maps.
10891 if (InsertPos)
10892 if (!RemoveNodeFromCSEMaps(N))
10893 InsertPos = nullptr;
10894
10895 // Now we update the operands.
10896 for (unsigned i = 0; i != NumOps; ++i)
10897 if (N->OperandList[i] != Ops[i])
10898 N->OperandList[i].set(Ops[i]);
10899
10901 // If this gets put into a CSE map, add it.
10902 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10903 return N;
10904}
10905
10906/// DropOperands - Release the operands and set this node to have
10907/// zero operands.
10909 // Unlike the code in MorphNodeTo that does this, we don't need to
10910 // watch for dead nodes here.
10911 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
10912 SDUse &Use = *I++;
10913 Use.set(SDValue());
10914 }
10915}
10916
10918 ArrayRef<MachineMemOperand *> NewMemRefs) {
10919 if (NewMemRefs.empty()) {
10920 N->clearMemRefs();
10921 return;
10922 }
10923
10924 // Check if we can avoid allocating by storing a single reference directly.
10925 if (NewMemRefs.size() == 1) {
10926 N->MemRefs = NewMemRefs[0];
10927 N->NumMemRefs = 1;
10928 return;
10929 }
10930
10931 MachineMemOperand **MemRefsBuffer =
10932 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
10933 llvm::copy(NewMemRefs, MemRefsBuffer);
10934 N->MemRefs = MemRefsBuffer;
10935 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
10936}
10937
10938/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
10939/// machine opcode.
10940///
10942 EVT VT) {
10943 SDVTList VTs = getVTList(VT);
10944 return SelectNodeTo(N, MachineOpc, VTs, {});
10945}
10946
10948 EVT VT, SDValue Op1) {
10949 SDVTList VTs = getVTList(VT);
10950 SDValue Ops[] = { Op1 };
10951 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10952}
10953
10955 EVT VT, SDValue Op1,
10956 SDValue Op2) {
10957 SDVTList VTs = getVTList(VT);
10958 SDValue Ops[] = { Op1, Op2 };
10959 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10960}
10961
10963 EVT VT, SDValue Op1,
10964 SDValue Op2, SDValue Op3) {
10965 SDVTList VTs = getVTList(VT);
10966 SDValue Ops[] = { Op1, Op2, Op3 };
10967 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10968}
10969
10971 EVT VT, ArrayRef<SDValue> Ops) {
10972 SDVTList VTs = getVTList(VT);
10973 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10974}
10975
10977 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
10978 SDVTList VTs = getVTList(VT1, VT2);
10979 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10980}
10981
10983 EVT VT1, EVT VT2) {
10984 SDVTList VTs = getVTList(VT1, VT2);
10985 return SelectNodeTo(N, MachineOpc, VTs, {});
10986}
10987
10989 EVT VT1, EVT VT2, EVT VT3,
10990 ArrayRef<SDValue> Ops) {
10991 SDVTList VTs = getVTList(VT1, VT2, VT3);
10992 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10993}
10994
10996 EVT VT1, EVT VT2,
10997 SDValue Op1, SDValue Op2) {
10998 SDVTList VTs = getVTList(VT1, VT2);
10999 SDValue Ops[] = { Op1, Op2 };
11000 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11001}
11002
11004 SDVTList VTs,ArrayRef<SDValue> Ops) {
11005 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11006 // Reset the NodeID to -1.
11007 New->setNodeId(-1);
11008 if (New != N) {
11009 ReplaceAllUsesWith(N, New);
11011 }
11012 return New;
11013}
11014
11015/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11016/// the line number information on the merged node since it is not possible to
11017/// preserve the information that operation is associated with multiple lines.
11018/// This will make the debugger working better at -O0, were there is a higher
11019/// probability having other instructions associated with that line.
11020///
11021/// For IROrder, we keep the smaller of the two
11022SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11023 DebugLoc NLoc = N->getDebugLoc();
11024 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11025 N->setDebugLoc(DebugLoc());
11026 }
11027 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11028 N->setIROrder(Order);
11029 return N;
11030}
11031
11032/// MorphNodeTo - This *mutates* the specified node to have the specified
11033/// return type, opcode, and operands.
11034///
11035/// Note that MorphNodeTo returns the resultant node. If there is already a
11036/// node of the specified opcode and operands, it returns that node instead of
11037/// the current one. Note that the SDLoc need not be the same.
11038///
11039/// Using MorphNodeTo is faster than creating a new node and swapping it in
11040/// with ReplaceAllUsesWith both because it often avoids allocating a new
11041/// node, and because it doesn't require CSE recalculation for any of
11042/// the node's users.
11043///
11044/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11045/// As a consequence it isn't appropriate to use from within the DAG combiner or
11046/// the legalizer which maintain worklists that would need to be updated when
11047/// deleting things.
11049 SDVTList VTs, ArrayRef<SDValue> Ops) {
11050 // If an identical node already exists, use it.
11051 void *IP = nullptr;
11052 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11054 AddNodeIDNode(ID, Opc, VTs, Ops);
11055 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11056 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11057 }
11058
11059 if (!RemoveNodeFromCSEMaps(N))
11060 IP = nullptr;
11061
11062 // Start the morphing.
11063 N->NodeType = Opc;
11064 N->ValueList = VTs.VTs;
11065 N->NumValues = VTs.NumVTs;
11066
11067 // Clear the operands list, updating used nodes to remove this from their
11068 // use list. Keep track of any operands that become dead as a result.
11069 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11070 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11071 SDUse &Use = *I++;
11072 SDNode *Used = Use.getNode();
11073 Use.set(SDValue());
11074 if (Used->use_empty())
11075 DeadNodeSet.insert(Used);
11076 }
11077
11078 // For MachineNode, initialize the memory references information.
11079 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N))
11080 MN->clearMemRefs();
11081
11082 // Swap for an appropriately sized array from the recycler.
11083 removeOperands(N);
11084 createOperands(N, Ops);
11085
11086 // Delete any nodes that are still dead after adding the uses for the
11087 // new operands.
11088 if (!DeadNodeSet.empty()) {
11089 SmallVector<SDNode *, 16> DeadNodes;
11090 for (SDNode *N : DeadNodeSet)
11091 if (N->use_empty())
11092 DeadNodes.push_back(N);
11093 RemoveDeadNodes(DeadNodes);
11094 }
11095
11096 if (IP)
11097 CSEMap.InsertNode(N, IP); // Memoize the new node.
11098 return N;
11099}
11100
11102 unsigned OrigOpc = Node->getOpcode();
11103 unsigned NewOpc;
11104 switch (OrigOpc) {
11105 default:
11106 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11107#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11108 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11109#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11110 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11111#include "llvm/IR/ConstrainedOps.def"
11112 }
11113
11114 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11115
11116 // We're taking this node out of the chain, so we need to re-link things.
11117 SDValue InputChain = Node->getOperand(0);
11118 SDValue OutputChain = SDValue(Node, 1);
11119 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11120
11122 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11123 Ops.push_back(Node->getOperand(i));
11124
11125 SDVTList VTs = getVTList(Node->getValueType(0));
11126 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11127
11128 // MorphNodeTo can operate in two ways: if an existing node with the
11129 // specified operands exists, it can just return it. Otherwise, it
11130 // updates the node in place to have the requested operands.
11131 if (Res == Node) {
11132 // If we updated the node in place, reset the node ID. To the isel,
11133 // this should be just like a newly allocated machine node.
11134 Res->setNodeId(-1);
11135 } else {
11136 ReplaceAllUsesWith(Node, Res);
11137 RemoveDeadNode(Node);
11138 }
11139
11140 return Res;
11141}
11142
11143/// getMachineNode - These are used for target selectors to create a new node
11144/// with specified return type(s), MachineInstr opcode, and operands.
11145///
11146/// Note that getMachineNode returns the resultant node. If there is already a
11147/// node of the specified opcode and operands, it returns that node instead of
11148/// the current one.
11150 EVT VT) {
11151 SDVTList VTs = getVTList(VT);
11152 return getMachineNode(Opcode, dl, VTs, {});
11153}
11154
11156 EVT VT, SDValue Op1) {
11157 SDVTList VTs = getVTList(VT);
11158 SDValue Ops[] = { Op1 };
11159 return getMachineNode(Opcode, dl, VTs, Ops);
11160}
11161
11163 EVT VT, SDValue Op1, SDValue Op2) {
11164 SDVTList VTs = getVTList(VT);
11165 SDValue Ops[] = { Op1, Op2 };
11166 return getMachineNode(Opcode, dl, VTs, Ops);
11167}
11168
11170 EVT VT, SDValue Op1, SDValue Op2,
11171 SDValue Op3) {
11172 SDVTList VTs = getVTList(VT);
11173 SDValue Ops[] = { Op1, Op2, Op3 };
11174 return getMachineNode(Opcode, dl, VTs, Ops);
11175}
11176
11178 EVT VT, ArrayRef<SDValue> Ops) {
11179 SDVTList VTs = getVTList(VT);
11180 return getMachineNode(Opcode, dl, VTs, Ops);
11181}
11182
11184 EVT VT1, EVT VT2, SDValue Op1,
11185 SDValue Op2) {
11186 SDVTList VTs = getVTList(VT1, VT2);
11187 SDValue Ops[] = { Op1, Op2 };
11188 return getMachineNode(Opcode, dl, VTs, Ops);
11189}
11190
11192 EVT VT1, EVT VT2, SDValue Op1,
11193 SDValue Op2, SDValue Op3) {
11194 SDVTList VTs = getVTList(VT1, VT2);
11195 SDValue Ops[] = { Op1, Op2, Op3 };
11196 return getMachineNode(Opcode, dl, VTs, Ops);
11197}
11198
11200 EVT VT1, EVT VT2,
11201 ArrayRef<SDValue> Ops) {
11202 SDVTList VTs = getVTList(VT1, VT2);
11203 return getMachineNode(Opcode, dl, VTs, Ops);
11204}
11205
11207 EVT VT1, EVT VT2, EVT VT3,
11208 SDValue Op1, SDValue Op2) {
11209 SDVTList VTs = getVTList(VT1, VT2, VT3);
11210 SDValue Ops[] = { Op1, Op2 };
11211 return getMachineNode(Opcode, dl, VTs, Ops);
11212}
11213
11215 EVT VT1, EVT VT2, EVT VT3,
11216 SDValue Op1, SDValue Op2,
11217 SDValue Op3) {
11218 SDVTList VTs = getVTList(VT1, VT2, VT3);
11219 SDValue Ops[] = { Op1, Op2, Op3 };
11220 return getMachineNode(Opcode, dl, VTs, Ops);
11221}
11222
11224 EVT VT1, EVT VT2, EVT VT3,
11225 ArrayRef<SDValue> Ops) {
11226 SDVTList VTs = getVTList(VT1, VT2, VT3);
11227 return getMachineNode(Opcode, dl, VTs, Ops);
11228}
11229
11231 ArrayRef<EVT> ResultTys,
11232 ArrayRef<SDValue> Ops) {
11233 SDVTList VTs = getVTList(ResultTys);
11234 return getMachineNode(Opcode, dl, VTs, Ops);
11235}
11236
11238 SDVTList VTs,
11239 ArrayRef<SDValue> Ops) {
11240 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11242 void *IP = nullptr;
11243
11244 if (DoCSE) {
11246 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11247 IP = nullptr;
11248 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11249 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11250 }
11251 }
11252
11253 // Allocate a new MachineSDNode.
11254 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11255 createOperands(N, Ops);
11256
11257 if (DoCSE)
11258 CSEMap.InsertNode(N, IP);
11259
11260 InsertNode(N);
11261 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11262 return N;
11263}
11264
11265/// getTargetExtractSubreg - A convenience function for creating
11266/// TargetOpcode::EXTRACT_SUBREG nodes.
11268 SDValue Operand) {
11269 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11270 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
11271 VT, Operand, SRIdxVal);
11272 return SDValue(Subreg, 0);
11273}
11274
11275/// getTargetInsertSubreg - A convenience function for creating
11276/// TargetOpcode::INSERT_SUBREG nodes.
11278 SDValue Operand, SDValue Subreg) {
11279 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11280 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
11281 VT, Operand, Subreg, SRIdxVal);
11282 return SDValue(Result, 0);
11283}
11284
11285/// getNodeIfExists - Get the specified node if it's already available, or
11286/// else return NULL.
11288 ArrayRef<SDValue> Ops) {
11289 SDNodeFlags Flags;
11290 if (Inserter)
11291 Flags = Inserter->getFlags();
11292 return getNodeIfExists(Opcode, VTList, Ops, Flags);
11293}
11294
11297 const SDNodeFlags Flags) {
11298 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11300 AddNodeIDNode(ID, Opcode, VTList, Ops);
11301 void *IP = nullptr;
11302 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP)) {
11303 E->intersectFlagsWith(Flags);
11304 return E;
11305 }
11306 }
11307 return nullptr;
11308}
11309
11310/// doesNodeExist - Check if a node exists without modifying its flags.
11311bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
11312 ArrayRef<SDValue> Ops) {
11313 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11315 AddNodeIDNode(ID, Opcode, VTList, Ops);
11316 void *IP = nullptr;
11317 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
11318 return true;
11319 }
11320 return false;
11321}
11322
11323/// getDbgValue - Creates a SDDbgValue node.
11324///
11325/// SDNode
11327 SDNode *N, unsigned R, bool IsIndirect,
11328 const DebugLoc &DL, unsigned O) {
11329 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11330 "Expected inlined-at fields to agree");
11331 return new (DbgInfo->getAlloc())
11332 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
11333 {}, IsIndirect, DL, O,
11334 /*IsVariadic=*/false);
11335}
11336
11337/// Constant
11339 DIExpression *Expr,
11340 const Value *C,
11341 const DebugLoc &DL, unsigned O) {
11342 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11343 "Expected inlined-at fields to agree");
11344 return new (DbgInfo->getAlloc())
11345 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
11346 /*IsIndirect=*/false, DL, O,
11347 /*IsVariadic=*/false);
11348}
11349
11350/// FrameIndex
11352 DIExpression *Expr, unsigned FI,
11353 bool IsIndirect,
11354 const DebugLoc &DL,
11355 unsigned O) {
11356 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11357 "Expected inlined-at fields to agree");
11358 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
11359}
11360
11361/// FrameIndex with dependencies
11363 DIExpression *Expr, unsigned FI,
11364 ArrayRef<SDNode *> Dependencies,
11365 bool IsIndirect,
11366 const DebugLoc &DL,
11367 unsigned O) {
11368 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11369 "Expected inlined-at fields to agree");
11370 return new (DbgInfo->getAlloc())
11371 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
11372 Dependencies, IsIndirect, DL, O,
11373 /*IsVariadic=*/false);
11374}
11375
11376/// VReg
11378 unsigned VReg, bool IsIndirect,
11379 const DebugLoc &DL, unsigned O) {
11380 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11381 "Expected inlined-at fields to agree");
11382 return new (DbgInfo->getAlloc())
11383 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
11384 {}, IsIndirect, DL, O,
11385 /*IsVariadic=*/false);
11386}
11387
11390 ArrayRef<SDNode *> Dependencies,
11391 bool IsIndirect, const DebugLoc &DL,
11392 unsigned O, bool IsVariadic) {
11393 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11394 "Expected inlined-at fields to agree");
11395 return new (DbgInfo->getAlloc())
11396 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
11397 DL, O, IsVariadic);
11398}
11399
11401 unsigned OffsetInBits, unsigned SizeInBits,
11402 bool InvalidateDbg) {
11403 SDNode *FromNode = From.getNode();
11404 SDNode *ToNode = To.getNode();
11405 assert(FromNode && ToNode && "Can't modify dbg values");
11406
11407 // PR35338
11408 // TODO: assert(From != To && "Redundant dbg value transfer");
11409 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
11410 if (From == To || FromNode == ToNode)
11411 return;
11412
11413 if (!FromNode->getHasDebugValue())
11414 return;
11415
11416 SDDbgOperand FromLocOp =
11417 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
11419
11421 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
11422 if (Dbg->isInvalidated())
11423 continue;
11424
11425 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
11426
11427 // Create a new location ops vector that is equal to the old vector, but
11428 // with each instance of FromLocOp replaced with ToLocOp.
11429 bool Changed = false;
11430 auto NewLocOps = Dbg->copyLocationOps();
11431 std::replace_if(
11432 NewLocOps.begin(), NewLocOps.end(),
11433 [&Changed, FromLocOp](const SDDbgOperand &Op) {
11434 bool Match = Op == FromLocOp;
11435 Changed |= Match;
11436 return Match;
11437 },
11438 ToLocOp);
11439 // Ignore this SDDbgValue if we didn't find a matching location.
11440 if (!Changed)
11441 continue;
11442
11443 DIVariable *Var = Dbg->getVariable();
11444 auto *Expr = Dbg->getExpression();
11445 // If a fragment is requested, update the expression.
11446 if (SizeInBits) {
11447 // When splitting a larger (e.g., sign-extended) value whose
11448 // lower bits are described with an SDDbgValue, do not attempt
11449 // to transfer the SDDbgValue to the upper bits.
11450 if (auto FI = Expr->getFragmentInfo())
11451 if (OffsetInBits + SizeInBits > FI->SizeInBits)
11452 continue;
11453 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
11454 SizeInBits);
11455 if (!Fragment)
11456 continue;
11457 Expr = *Fragment;
11458 }
11459
11460 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
11461 // Clone the SDDbgValue and move it to To.
11462 SDDbgValue *Clone = getDbgValueList(
11463 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
11464 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
11465 Dbg->isVariadic());
11466 ClonedDVs.push_back(Clone);
11467
11468 if (InvalidateDbg) {
11469 // Invalidate value and indicate the SDDbgValue should not be emitted.
11470 Dbg->setIsInvalidated();
11471 Dbg->setIsEmitted();
11472 }
11473 }
11474
11475 for (SDDbgValue *Dbg : ClonedDVs) {
11476 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
11477 "Transferred DbgValues should depend on the new SDNode");
11478 AddDbgValue(Dbg, false);
11479 }
11480}
11481
11483 if (!N.getHasDebugValue())
11484 return;
11485
11486 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
11487 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
11488 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
11489 return SDDbgOperand::fromNode(Node, ResNo);
11490 };
11491
11493 for (auto *DV : GetDbgValues(&N)) {
11494 if (DV->isInvalidated())
11495 continue;
11496 switch (N.getOpcode()) {
11497 default:
11498 break;
11499 case ISD::ADD: {
11500 SDValue N0 = N.getOperand(0);
11501 SDValue N1 = N.getOperand(1);
11502 if (!isa<ConstantSDNode>(N0)) {
11503 bool RHSConstant = isa<ConstantSDNode>(N1);
11505 if (RHSConstant)
11506 Offset = N.getConstantOperandVal(1);
11507 // We are not allowed to turn indirect debug values variadic, so
11508 // don't salvage those.
11509 if (!RHSConstant && DV->isIndirect())
11510 continue;
11511
11512 // Rewrite an ADD constant node into a DIExpression. Since we are
11513 // performing arithmetic to compute the variable's *value* in the
11514 // DIExpression, we need to mark the expression with a
11515 // DW_OP_stack_value.
11516 auto *DIExpr = DV->getExpression();
11517 auto NewLocOps = DV->copyLocationOps();
11518 bool Changed = false;
11519 size_t OrigLocOpsSize = NewLocOps.size();
11520 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
11521 // We're not given a ResNo to compare against because the whole
11522 // node is going away. We know that any ISD::ADD only has one
11523 // result, so we can assume any node match is using the result.
11524 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
11525 NewLocOps[i].getSDNode() != &N)
11526 continue;
11527 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
11528 if (RHSConstant) {
11531 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
11532 } else {
11533 // Convert to a variadic expression (if not already).
11534 // convertToVariadicExpression() returns a const pointer, so we use
11535 // a temporary const variable here.
11536 const auto *TmpDIExpr =
11540 ExprOps.push_back(NewLocOps.size());
11541 ExprOps.push_back(dwarf::DW_OP_plus);
11544 NewLocOps.push_back(RHS);
11545 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
11546 }
11547 Changed = true;
11548 }
11549 (void)Changed;
11550 assert(Changed && "Salvage target doesn't use N");
11551
11552 bool IsVariadic =
11553 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
11554
11555 auto AdditionalDependencies = DV->getAdditionalDependencies();
11556 SDDbgValue *Clone = getDbgValueList(
11557 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
11558 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
11559 ClonedDVs.push_back(Clone);
11560 DV->setIsInvalidated();
11561 DV->setIsEmitted();
11562 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
11563 N0.getNode()->dumprFull(this);
11564 dbgs() << " into " << *DIExpr << '\n');
11565 }
11566 break;
11567 }
11568 case ISD::TRUNCATE: {
11569 SDValue N0 = N.getOperand(0);
11570 TypeSize FromSize = N0.getValueSizeInBits();
11571 TypeSize ToSize = N.getValueSizeInBits(0);
11572
11573 DIExpression *DbgExpression = DV->getExpression();
11574 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
11575 auto NewLocOps = DV->copyLocationOps();
11576 bool Changed = false;
11577 for (size_t i = 0; i < NewLocOps.size(); ++i) {
11578 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
11579 NewLocOps[i].getSDNode() != &N)
11580 continue;
11581
11582 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
11583 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
11584 Changed = true;
11585 }
11586 assert(Changed && "Salvage target doesn't use N");
11587 (void)Changed;
11588
11589 SDDbgValue *Clone =
11590 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
11591 DV->getAdditionalDependencies(), DV->isIndirect(),
11592 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
11593
11594 ClonedDVs.push_back(Clone);
11595 DV->setIsInvalidated();
11596 DV->setIsEmitted();
11597 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
11598 dbgs() << " into " << *DbgExpression << '\n');
11599 break;
11600 }
11601 }
11602 }
11603
11604 for (SDDbgValue *Dbg : ClonedDVs) {
11605 assert((!Dbg->getSDNodes().empty() ||
11606 llvm::any_of(Dbg->getLocationOps(),
11607 [&](const SDDbgOperand &Op) {
11608 return Op.getKind() == SDDbgOperand::FRAMEIX;
11609 })) &&
11610 "Salvaged DbgValue should depend on a new SDNode");
11611 AddDbgValue(Dbg, false);
11612 }
11613}
11614
11615/// Creates a SDDbgLabel node.
11617 const DebugLoc &DL, unsigned O) {
11618 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
11619 "Expected inlined-at fields to agree");
11620 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
11621}
11622
11623namespace {
11624
11625/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
11626/// pointed to by a use iterator is deleted, increment the use iterator
11627/// so that it doesn't dangle.
11628///
11629class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
11632
11633 void NodeDeleted(SDNode *N, SDNode *E) override {
11634 // Increment the iterator as needed.
11635 while (UI != UE && N == UI->getUser())
11636 ++UI;
11637 }
11638
11639public:
11640 RAUWUpdateListener(SelectionDAG &d,
11643 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
11644};
11645
11646} // end anonymous namespace
11647
11648/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11649/// This can cause recursive merging of nodes in the DAG.
11650///
11651/// This version assumes From has a single result value.
11652///
11654 SDNode *From = FromN.getNode();
11655 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
11656 "Cannot replace with this method!");
11657 assert(From != To.getNode() && "Cannot replace uses of with self");
11658
11659 // Preserve Debug Values
11660 transferDbgValues(FromN, To);
11661 // Preserve extra info.
11662 copyExtraInfo(From, To.getNode());
11663
11664 // Iterate over all the existing uses of From. New uses will be added
11665 // to the beginning of the use list, which we avoid visiting.
11666 // This specifically avoids visiting uses of From that arise while the
11667 // replacement is happening, because any such uses would be the result
11668 // of CSE: If an existing node looks like From after one of its operands
11669 // is replaced by To, we don't want to replace of all its users with To
11670 // too. See PR3018 for more info.
11671 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11672 RAUWUpdateListener Listener(*this, UI, UE);
11673 while (UI != UE) {
11674 SDNode *User = UI->getUser();
11675
11676 // This node is about to morph, remove its old self from the CSE maps.
11677 RemoveNodeFromCSEMaps(User);
11678
11679 // A user can appear in a use list multiple times, and when this
11680 // happens the uses are usually next to each other in the list.
11681 // To help reduce the number of CSE recomputations, process all
11682 // the uses of this user that we can find this way.
11683 do {
11684 SDUse &Use = *UI;
11685 ++UI;
11686 Use.set(To);
11687 if (To->isDivergent() != From->isDivergent())
11689 } while (UI != UE && UI->getUser() == User);
11690 // Now that we have modified User, add it back to the CSE maps. If it
11691 // already exists there, recursively merge the results together.
11692 AddModifiedNodeToCSEMaps(User);
11693 }
11694
11695 // If we just RAUW'd the root, take note.
11696 if (FromN == getRoot())
11697 setRoot(To);
11698}
11699
11700/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11701/// This can cause recursive merging of nodes in the DAG.
11702///
11703/// This version assumes that for each value of From, there is a
11704/// corresponding value in To in the same position with the same type.
11705///
11707#ifndef NDEBUG
11708 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11709 assert((!From->hasAnyUseOfValue(i) ||
11710 From->getValueType(i) == To->getValueType(i)) &&
11711 "Cannot use this version of ReplaceAllUsesWith!");
11712#endif
11713
11714 // Handle the trivial case.
11715 if (From == To)
11716 return;
11717
11718 // Preserve Debug Info. Only do this if there's a use.
11719 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11720 if (From->hasAnyUseOfValue(i)) {
11721 assert((i < To->getNumValues()) && "Invalid To location");
11723 }
11724 // Preserve extra info.
11725 copyExtraInfo(From, To);
11726
11727 // Iterate over just the existing users of From. See the comments in
11728 // the ReplaceAllUsesWith above.
11729 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11730 RAUWUpdateListener Listener(*this, UI, UE);
11731 while (UI != UE) {
11732 SDNode *User = UI->getUser();
11733
11734 // This node is about to morph, remove its old self from the CSE maps.
11735 RemoveNodeFromCSEMaps(User);
11736
11737 // A user can appear in a use list multiple times, and when this
11738 // happens the uses are usually next to each other in the list.
11739 // To help reduce the number of CSE recomputations, process all
11740 // the uses of this user that we can find this way.
11741 do {
11742 SDUse &Use = *UI;
11743 ++UI;
11744 Use.setNode(To);
11745 if (To->isDivergent() != From->isDivergent())
11747 } while (UI != UE && UI->getUser() == User);
11748
11749 // Now that we have modified User, add it back to the CSE maps. If it
11750 // already exists there, recursively merge the results together.
11751 AddModifiedNodeToCSEMaps(User);
11752 }
11753
11754 // If we just RAUW'd the root, take note.
11755 if (From == getRoot().getNode())
11756 setRoot(SDValue(To, getRoot().getResNo()));
11757}
11758
11759/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11760/// This can cause recursive merging of nodes in the DAG.
11761///
11762/// This version can replace From with any result values. To must match the
11763/// number and types of values returned by From.
11765 if (From->getNumValues() == 1) // Handle the simple case efficiently.
11766 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
11767
11768 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
11769 // Preserve Debug Info.
11770 transferDbgValues(SDValue(From, i), To[i]);
11771 // Preserve extra info.
11772 copyExtraInfo(From, To[i].getNode());
11773 }
11774
11775 // Iterate over just the existing users of From. See the comments in
11776 // the ReplaceAllUsesWith above.
11777 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11778 RAUWUpdateListener Listener(*this, UI, UE);
11779 while (UI != UE) {
11780 SDNode *User = UI->getUser();
11781
11782 // This node is about to morph, remove its old self from the CSE maps.
11783 RemoveNodeFromCSEMaps(User);
11784
11785 // A user can appear in a use list multiple times, and when this happens the
11786 // uses are usually next to each other in the list. To help reduce the
11787 // number of CSE and divergence recomputations, process all the uses of this
11788 // user that we can find this way.
11789 bool To_IsDivergent = false;
11790 do {
11791 SDUse &Use = *UI;
11792 const SDValue &ToOp = To[Use.getResNo()];
11793 ++UI;
11794 Use.set(ToOp);
11795 To_IsDivergent |= ToOp->isDivergent();
11796 } while (UI != UE && UI->getUser() == User);
11797
11798 if (To_IsDivergent != From->isDivergent())
11800
11801 // Now that we have modified User, add it back to the CSE maps. If it
11802 // already exists there, recursively merge the results together.
11803 AddModifiedNodeToCSEMaps(User);
11804 }
11805
11806 // If we just RAUW'd the root, take note.
11807 if (From == getRoot().getNode())
11808 setRoot(SDValue(To[getRoot().getResNo()]));
11809}
11810
11811/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
11812/// uses of other values produced by From.getNode() alone. The Deleted
11813/// vector is handled the same way as for ReplaceAllUsesWith.
11815 // Handle the really simple, really trivial case efficiently.
11816 if (From == To) return;
11817
11818 // Handle the simple, trivial, case efficiently.
11819 if (From.getNode()->getNumValues() == 1) {
11821 return;
11822 }
11823
11824 // Preserve Debug Info.
11826 copyExtraInfo(From.getNode(), To.getNode());
11827
11828 // Iterate over just the existing users of From. See the comments in
11829 // the ReplaceAllUsesWith above.
11830 SDNode::use_iterator UI = From.getNode()->use_begin(),
11831 UE = From.getNode()->use_end();
11832 RAUWUpdateListener Listener(*this, UI, UE);
11833 while (UI != UE) {
11834 SDNode *User = UI->getUser();
11835 bool UserRemovedFromCSEMaps = false;
11836
11837 // A user can appear in a use list multiple times, and when this
11838 // happens the uses are usually next to each other in the list.
11839 // To help reduce the number of CSE recomputations, process all
11840 // the uses of this user that we can find this way.
11841 do {
11842 SDUse &Use = *UI;
11843
11844 // Skip uses of different values from the same node.
11845 if (Use.getResNo() != From.getResNo()) {
11846 ++UI;
11847 continue;
11848 }
11849
11850 // If this node hasn't been modified yet, it's still in the CSE maps,
11851 // so remove its old self from the CSE maps.
11852 if (!UserRemovedFromCSEMaps) {
11853 RemoveNodeFromCSEMaps(User);
11854 UserRemovedFromCSEMaps = true;
11855 }
11856
11857 ++UI;
11858 Use.set(To);
11859 if (To->isDivergent() != From->isDivergent())
11861 } while (UI != UE && UI->getUser() == User);
11862 // We are iterating over all uses of the From node, so if a use
11863 // doesn't use the specific value, no changes are made.
11864 if (!UserRemovedFromCSEMaps)
11865 continue;
11866
11867 // Now that we have modified User, add it back to the CSE maps. If it
11868 // already exists there, recursively merge the results together.
11869 AddModifiedNodeToCSEMaps(User);
11870 }
11871
11872 // If we just RAUW'd the root, take note.
11873 if (From == getRoot())
11874 setRoot(To);
11875}
11876
11877namespace {
11878
11879/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
11880/// to record information about a use.
11881struct UseMemo {
11882 SDNode *User;
11883 unsigned Index;
11884 SDUse *Use;
11885};
11886
11887/// operator< - Sort Memos by User.
11888bool operator<(const UseMemo &L, const UseMemo &R) {
11889 return (intptr_t)L.User < (intptr_t)R.User;
11890}
11891
11892/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
11893/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
11894/// the node already has been taken care of recursively.
11895class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
11897
11898 void NodeDeleted(SDNode *N, SDNode *E) override {
11899 for (UseMemo &Memo : Uses)
11900 if (Memo.User == N)
11901 Memo.User = nullptr;
11902 }
11903
11904public:
11905 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
11906 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
11907};
11908
11909} // end anonymous namespace
11910
11911/// Return true if a glue output should propagate divergence information.
11913 switch (Node->getOpcode()) {
11914 case ISD::CopyFromReg:
11915 case ISD::CopyToReg:
11916 return false;
11917 default:
11918 return true;
11919 }
11920
11921 llvm_unreachable("covered opcode switch");
11922}
11923
11925 if (TLI->isSDNodeAlwaysUniform(N)) {
11926 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
11927 "Conflicting divergence information!");
11928 return false;
11929 }
11930 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
11931 return true;
11932 for (const auto &Op : N->ops()) {
11933 EVT VT = Op.getValueType();
11934
11935 // Skip Chain. It does not carry divergence.
11936 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
11937 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
11938 return true;
11939 }
11940 return false;
11941}
11942
11944 SmallVector<SDNode *, 16> Worklist(1, N);
11945 do {
11946 N = Worklist.pop_back_val();
11947 bool IsDivergent = calculateDivergence(N);
11948 if (N->SDNodeBits.IsDivergent != IsDivergent) {
11949 N->SDNodeBits.IsDivergent = IsDivergent;
11950 llvm::append_range(Worklist, N->users());
11951 }
11952 } while (!Worklist.empty());
11953}
11954
11955void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
11957 Order.reserve(AllNodes.size());
11958 for (auto &N : allnodes()) {
11959 unsigned NOps = N.getNumOperands();
11960 Degree[&N] = NOps;
11961 if (0 == NOps)
11962 Order.push_back(&N);
11963 }
11964 for (size_t I = 0; I != Order.size(); ++I) {
11965 SDNode *N = Order[I];
11966 for (auto *U : N->users()) {
11967 unsigned &UnsortedOps = Degree[U];
11968 if (0 == --UnsortedOps)
11969 Order.push_back(U);
11970 }
11971 }
11972}
11973
11974#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
11975void SelectionDAG::VerifyDAGDivergence() {
11976 std::vector<SDNode *> TopoOrder;
11977 CreateTopologicalOrder(TopoOrder);
11978 for (auto *N : TopoOrder) {
11979 assert(calculateDivergence(N) == N->isDivergent() &&
11980 "Divergence bit inconsistency detected");
11981 }
11982}
11983#endif
11984
11985/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
11986/// uses of other values produced by From.getNode() alone. The same value
11987/// may appear in both the From and To list. The Deleted vector is
11988/// handled the same way as for ReplaceAllUsesWith.
11990 const SDValue *To,
11991 unsigned Num){
11992 // Handle the simple, trivial case efficiently.
11993 if (Num == 1)
11994 return ReplaceAllUsesOfValueWith(*From, *To);
11995
11996 transferDbgValues(*From, *To);
11997 copyExtraInfo(From->getNode(), To->getNode());
11998
11999 // Read up all the uses and make records of them. This helps
12000 // processing new uses that are introduced during the
12001 // replacement process.
12003 for (unsigned i = 0; i != Num; ++i) {
12004 unsigned FromResNo = From[i].getResNo();
12005 SDNode *FromNode = From[i].getNode();
12006 for (SDUse &Use : FromNode->uses()) {
12007 if (Use.getResNo() == FromResNo) {
12008 UseMemo Memo = {Use.getUser(), i, &Use};
12009 Uses.push_back(Memo);
12010 }
12011 }
12012 }
12013
12014 // Sort the uses, so that all the uses from a given User are together.
12016 RAUOVWUpdateListener Listener(*this, Uses);
12017
12018 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12019 UseIndex != UseIndexEnd; ) {
12020 // We know that this user uses some value of From. If it is the right
12021 // value, update it.
12022 SDNode *User = Uses[UseIndex].User;
12023 // If the node has been deleted by recursive CSE updates when updating
12024 // another node, then just skip this entry.
12025 if (User == nullptr) {
12026 ++UseIndex;
12027 continue;
12028 }
12029
12030 // This node is about to morph, remove its old self from the CSE maps.
12031 RemoveNodeFromCSEMaps(User);
12032
12033 // The Uses array is sorted, so all the uses for a given User
12034 // are next to each other in the list.
12035 // To help reduce the number of CSE recomputations, process all
12036 // the uses of this user that we can find this way.
12037 do {
12038 unsigned i = Uses[UseIndex].Index;
12039 SDUse &Use = *Uses[UseIndex].Use;
12040 ++UseIndex;
12041
12042 Use.set(To[i]);
12043 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12044
12045 // Now that we have modified User, add it back to the CSE maps. If it
12046 // already exists there, recursively merge the results together.
12047 AddModifiedNodeToCSEMaps(User);
12048 }
12049}
12050
12051/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12052/// based on their topological order. It returns the maximum id and a vector
12053/// of the SDNodes* in assigned order by reference.
12055 unsigned DAGSize = 0;
12056
12057 // SortedPos tracks the progress of the algorithm. Nodes before it are
12058 // sorted, nodes after it are unsorted. When the algorithm completes
12059 // it is at the end of the list.
12060 allnodes_iterator SortedPos = allnodes_begin();
12061
12062 // Visit all the nodes. Move nodes with no operands to the front of
12063 // the list immediately. Annotate nodes that do have operands with their
12064 // operand count. Before we do this, the Node Id fields of the nodes
12065 // may contain arbitrary values. After, the Node Id fields for nodes
12066 // before SortedPos will contain the topological sort index, and the
12067 // Node Id fields for nodes At SortedPos and after will contain the
12068 // count of outstanding operands.
12070 checkForCycles(&N, this);
12071 unsigned Degree = N.getNumOperands();
12072 if (Degree == 0) {
12073 // A node with no uses, add it to the result array immediately.
12074 N.setNodeId(DAGSize++);
12075 allnodes_iterator Q(&N);
12076 if (Q != SortedPos)
12077 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12078 assert(SortedPos != AllNodes.end() && "Overran node list");
12079 ++SortedPos;
12080 } else {
12081 // Temporarily use the Node Id as scratch space for the degree count.
12082 N.setNodeId(Degree);
12083 }
12084 }
12085
12086 // Visit all the nodes. As we iterate, move nodes into sorted order,
12087 // such that by the time the end is reached all nodes will be sorted.
12088 for (SDNode &Node : allnodes()) {
12089 SDNode *N = &Node;
12090 checkForCycles(N, this);
12091 // N is in sorted position, so all its uses have one less operand
12092 // that needs to be sorted.
12093 for (SDNode *P : N->users()) {
12094 unsigned Degree = P->getNodeId();
12095 assert(Degree != 0 && "Invalid node degree");
12096 --Degree;
12097 if (Degree == 0) {
12098 // All of P's operands are sorted, so P may sorted now.
12099 P->setNodeId(DAGSize++);
12100 if (P->getIterator() != SortedPos)
12101 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12102 assert(SortedPos != AllNodes.end() && "Overran node list");
12103 ++SortedPos;
12104 } else {
12105 // Update P's outstanding operand count.
12106 P->setNodeId(Degree);
12107 }
12108 }
12109 if (Node.getIterator() == SortedPos) {
12110#ifndef NDEBUG
12112 SDNode *S = &*++I;
12113 dbgs() << "Overran sorted position:\n";
12114 S->dumprFull(this); dbgs() << "\n";
12115 dbgs() << "Checking if this is due to cycles\n";
12116 checkForCycles(this, true);
12117#endif
12118 llvm_unreachable(nullptr);
12119 }
12120 }
12121
12122 assert(SortedPos == AllNodes.end() &&
12123 "Topological sort incomplete!");
12124 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12125 "First node in topological sort is not the entry token!");
12126 assert(AllNodes.front().getNodeId() == 0 &&
12127 "First node in topological sort has non-zero id!");
12128 assert(AllNodes.front().getNumOperands() == 0 &&
12129 "First node in topological sort has operands!");
12130 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12131 "Last node in topologic sort has unexpected id!");
12132 assert(AllNodes.back().use_empty() &&
12133 "Last node in topologic sort has users!");
12134 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12135 return DAGSize;
12136}
12137
12138/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12139/// value is produced by SD.
12140void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12141 for (SDNode *SD : DB->getSDNodes()) {
12142 if (!SD)
12143 continue;
12144 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12145 SD->setHasDebugValue(true);
12146 }
12147 DbgInfo->add(DB, isParameter);
12148}
12149
12150void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12151
12153 SDValue NewMemOpChain) {
12154 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12155 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12156 // The new memory operation must have the same position as the old load in
12157 // terms of memory dependency. Create a TokenFactor for the old load and new
12158 // memory operation and update uses of the old load's output chain to use that
12159 // TokenFactor.
12160 if (OldChain == NewMemOpChain || OldChain.use_empty())
12161 return NewMemOpChain;
12162
12163 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12164 OldChain, NewMemOpChain);
12165 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12166 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12167 return TokenFactor;
12168}
12169
12171 SDValue NewMemOp) {
12172 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12173 SDValue OldChain = SDValue(OldLoad, 1);
12174 SDValue NewMemOpChain = NewMemOp.getValue(1);
12175 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12176}
12177
12179 Function **OutFunction) {
12180 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12181
12182 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12183 auto *Module = MF->getFunction().getParent();
12184 auto *Function = Module->getFunction(Symbol);
12185
12186 if (OutFunction != nullptr)
12187 *OutFunction = Function;
12188
12189 if (Function != nullptr) {
12190 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12191 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12192 }
12193
12194 std::string ErrorStr;
12195 raw_string_ostream ErrorFormatter(ErrorStr);
12196 ErrorFormatter << "Undefined external symbol ";
12197 ErrorFormatter << '"' << Symbol << '"';
12198 report_fatal_error(Twine(ErrorStr));
12199}
12200
12201//===----------------------------------------------------------------------===//
12202// SDNode Class
12203//===----------------------------------------------------------------------===//
12204
12206 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
12207 return Const != nullptr && Const->isZero();
12208}
12209
12211 return V.isUndef() || isNullConstant(V);
12212}
12213
12215 ConstantFPSDNode *Const = dyn_cast<ConstantFPSDNode>(V);
12216 return Const != nullptr && Const->isZero() && !Const->isNegative();
12217}
12218
12220 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
12221 return Const != nullptr && Const->isAllOnes();
12222}
12223
12225 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
12226 return Const != nullptr && Const->isOne();
12227}
12228
12230 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
12231 return Const != nullptr && Const->isMinSignedValue();
12232}
12233
12234bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
12235 unsigned OperandNo) {
12236 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
12237 // TODO: Target-specific opcodes could be added.
12238 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
12239 /*AllowTruncation*/ true)) {
12240 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
12241 switch (Opcode) {
12242 case ISD::ADD:
12243 case ISD::OR:
12244 case ISD::XOR:
12245 case ISD::UMAX:
12246 return Const.isZero();
12247 case ISD::MUL:
12248 return Const.isOne();
12249 case ISD::AND:
12250 case ISD::UMIN:
12251 return Const.isAllOnes();
12252 case ISD::SMAX:
12253 return Const.isMinSignedValue();
12254 case ISD::SMIN:
12255 return Const.isMaxSignedValue();
12256 case ISD::SUB:
12257 case ISD::SHL:
12258 case ISD::SRA:
12259 case ISD::SRL:
12260 return OperandNo == 1 && Const.isZero();
12261 case ISD::UDIV:
12262 case ISD::SDIV:
12263 return OperandNo == 1 && Const.isOne();
12264 }
12265 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
12266 switch (Opcode) {
12267 case ISD::FADD:
12268 return ConstFP->isZero() &&
12269 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
12270 case ISD::FSUB:
12271 return OperandNo == 1 && ConstFP->isZero() &&
12272 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
12273 case ISD::FMUL:
12274 return ConstFP->isExactlyValue(1.0);
12275 case ISD::FDIV:
12276 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
12277 case ISD::FMINNUM:
12278 case ISD::FMAXNUM: {
12279 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
12280 EVT VT = V.getValueType();
12281 const fltSemantics &Semantics = VT.getFltSemantics();
12282 APFloat NeutralAF = !Flags.hasNoNaNs()
12283 ? APFloat::getQNaN(Semantics)
12284 : !Flags.hasNoInfs()
12285 ? APFloat::getInf(Semantics)
12286 : APFloat::getLargest(Semantics);
12287 if (Opcode == ISD::FMAXNUM)
12288 NeutralAF.changeSign();
12289
12290 return ConstFP->isExactlyValue(NeutralAF);
12291 }
12292 }
12293 }
12294 return false;
12295}
12296
12298 while (V.getOpcode() == ISD::BITCAST)
12299 V = V.getOperand(0);
12300 return V;
12301}
12302
12304 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
12305 V = V.getOperand(0);
12306 return V;
12307}
12308
12310 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
12311 V = V.getOperand(0);
12312 return V;
12313}
12314
12316 while (V.getOpcode() == ISD::TRUNCATE)
12317 V = V.getOperand(0);
12318 return V;
12319}
12320
12321bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
12322 if (V.getOpcode() != ISD::XOR)
12323 return false;
12324 V = peekThroughBitcasts(V.getOperand(1));
12325 unsigned NumBits = V.getScalarValueSizeInBits();
12326 ConstantSDNode *C =
12327 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
12328 return C && (C->getAPIntValue().countr_one() >= NumBits);
12329}
12330
12332 bool AllowTruncation) {
12333 EVT VT = N.getValueType();
12334 APInt DemandedElts = VT.isFixedLengthVector()
12336 : APInt(1, 1);
12337 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
12338}
12339
12341 bool AllowUndefs,
12342 bool AllowTruncation) {
12343 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
12344 return CN;
12345
12346 // SplatVectors can truncate their operands. Ignore that case here unless
12347 // AllowTruncation is set.
12348 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
12349 EVT VecEltVT = N->getValueType(0).getVectorElementType();
12350 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
12351 EVT CVT = CN->getValueType(0);
12352 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
12353 if (AllowTruncation || CVT == VecEltVT)
12354 return CN;
12355 }
12356 }
12357
12358 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
12359 BitVector UndefElements;
12360 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
12361
12362 // BuildVectors can truncate their operands. Ignore that case here unless
12363 // AllowTruncation is set.
12364 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12365 if (CN && (UndefElements.none() || AllowUndefs)) {
12366 EVT CVT = CN->getValueType(0);
12367 EVT NSVT = N.getValueType().getScalarType();
12368 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
12369 if (AllowTruncation || (CVT == NSVT))
12370 return CN;
12371 }
12372 }
12373
12374 return nullptr;
12375}
12376
12378 EVT VT = N.getValueType();
12379 APInt DemandedElts = VT.isFixedLengthVector()
12381 : APInt(1, 1);
12382 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
12383}
12384
12386 const APInt &DemandedElts,
12387 bool AllowUndefs) {
12388 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
12389 return CN;
12390
12391 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
12392 BitVector UndefElements;
12393 ConstantFPSDNode *CN =
12394 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
12395 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12396 if (CN && (UndefElements.none() || AllowUndefs))
12397 return CN;
12398 }
12399
12400 if (N.getOpcode() == ISD::SPLAT_VECTOR)
12401 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
12402 return CN;
12403
12404 return nullptr;
12405}
12406
12407bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
12408 // TODO: may want to use peekThroughBitcast() here.
12409 ConstantSDNode *C =
12410 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
12411 return C && C->isZero();
12412}
12413
12414bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
12415 ConstantSDNode *C =
12416 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
12417 return C && C->isOne();
12418}
12419
12420bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
12422 unsigned BitWidth = N.getScalarValueSizeInBits();
12423 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
12424 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
12425}
12426
12428 DropOperands();
12429}
12430
12431MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
12432 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
12433 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
12434 MemSDNodeBits.IsVolatile = MMO->isVolatile();
12435 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
12436 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
12437 MemSDNodeBits.IsInvariant = MMO->isInvariant();
12438
12439 // We check here that the size of the memory operand fits within the size of
12440 // the MMO. This is because the MMO might indicate only a possible address
12441 // range instead of specifying the affected memory addresses precisely.
12442 assert(
12443 (!MMO->getType().isValid() ||
12445 "Size mismatch!");
12446}
12447
12448/// Profile - Gather unique data for the node.
12449///
12451 AddNodeIDNode(ID, this);
12452}
12453
12454namespace {
12455
12456 struct EVTArray {
12457 std::vector<EVT> VTs;
12458
12459 EVTArray() {
12460 VTs.reserve(MVT::VALUETYPE_SIZE);
12461 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
12462 VTs.push_back(MVT((MVT::SimpleValueType)i));
12463 }
12464 };
12465
12466} // end anonymous namespace
12467
12468/// getValueTypeList - Return a pointer to the specified value type.
12469///
12470const EVT *SDNode::getValueTypeList(MVT VT) {
12471 static EVTArray SimpleVTArray;
12472
12473 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
12474 return &SimpleVTArray.VTs[VT.SimpleTy];
12475}
12476
12477/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
12478/// indicated value. This method ignores uses of other values defined by this
12479/// operation.
12480bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
12481 assert(Value < getNumValues() && "Bad value!");
12482
12483 // TODO: Only iterate over uses of a given value of the node
12484 for (SDUse &U : uses()) {
12485 if (U.getResNo() == Value) {
12486 if (NUses == 0)
12487 return false;
12488 --NUses;
12489 }
12490 }
12491
12492 // Found exactly the right number of uses?
12493 return NUses == 0;
12494}
12495
12496/// hasAnyUseOfValue - Return true if there are any use of the indicated
12497/// value. This method ignores uses of other values defined by this operation.
12498bool SDNode::hasAnyUseOfValue(unsigned Value) const {
12499 assert(Value < getNumValues() && "Bad value!");
12500
12501 for (SDUse &U : uses())
12502 if (U.getResNo() == Value)
12503 return true;
12504
12505 return false;
12506}
12507
12508/// isOnlyUserOf - Return true if this node is the only use of N.
12509bool SDNode::isOnlyUserOf(const SDNode *N) const {
12510 bool Seen = false;
12511 for (const SDNode *User : N->users()) {
12512 if (User == this)
12513 Seen = true;
12514 else
12515 return false;
12516 }
12517
12518 return Seen;
12519}
12520
12521/// Return true if the only users of N are contained in Nodes.
12523 bool Seen = false;
12524 for (const SDNode *User : N->users()) {
12525 if (llvm::is_contained(Nodes, User))
12526 Seen = true;
12527 else
12528 return false;
12529 }
12530
12531 return Seen;
12532}
12533
12534/// isOperand - Return true if this node is an operand of N.
12535bool SDValue::isOperandOf(const SDNode *N) const {
12536 return is_contained(N->op_values(), *this);
12537}
12538
12539bool SDNode::isOperandOf(const SDNode *N) const {
12540 return any_of(N->op_values(),
12541 [this](SDValue Op) { return this == Op.getNode(); });
12542}
12543
12544/// reachesChainWithoutSideEffects - Return true if this operand (which must
12545/// be a chain) reaches the specified operand without crossing any
12546/// side-effecting instructions on any chain path. In practice, this looks
12547/// through token factors and non-volatile loads. In order to remain efficient,
12548/// this only looks a couple of nodes in, it does not do an exhaustive search.
12549///
12550/// Note that we only need to examine chains when we're searching for
12551/// side-effects; SelectionDAG requires that all side-effects are represented
12552/// by chains, even if another operand would force a specific ordering. This
12553/// constraint is necessary to allow transformations like splitting loads.
12555 unsigned Depth) const {
12556 if (*this == Dest) return true;
12557
12558 // Don't search too deeply, we just want to be able to see through
12559 // TokenFactor's etc.
12560 if (Depth == 0) return false;
12561
12562 // If this is a token factor, all inputs to the TF happen in parallel.
12563 if (getOpcode() == ISD::TokenFactor) {
12564 // First, try a shallow search.
12565 if (is_contained((*this)->ops(), Dest)) {
12566 // We found the chain we want as an operand of this TokenFactor.
12567 // Essentially, we reach the chain without side-effects if we could
12568 // serialize the TokenFactor into a simple chain of operations with
12569 // Dest as the last operation. This is automatically true if the
12570 // chain has one use: there are no other ordering constraints.
12571 // If the chain has more than one use, we give up: some other
12572 // use of Dest might force a side-effect between Dest and the current
12573 // node.
12574 if (Dest.hasOneUse())
12575 return true;
12576 }
12577 // Next, try a deep search: check whether every operand of the TokenFactor
12578 // reaches Dest.
12579 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
12580 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
12581 });
12582 }
12583
12584 // Loads don't have side effects, look through them.
12585 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
12586 if (Ld->isUnordered())
12587 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
12588 }
12589 return false;
12590}
12591
12592bool SDNode::hasPredecessor(const SDNode *N) const {
12595 Worklist.push_back(this);
12596 return hasPredecessorHelper(N, Visited, Worklist);
12597}
12598
12600 this->Flags &= Flags;
12601}
12602
12603SDValue
12605 ArrayRef<ISD::NodeType> CandidateBinOps,
12606 bool AllowPartials) {
12607 // The pattern must end in an extract from index 0.
12608 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
12609 !isNullConstant(Extract->getOperand(1)))
12610 return SDValue();
12611
12612 // Match against one of the candidate binary ops.
12613 SDValue Op = Extract->getOperand(0);
12614 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
12615 return Op.getOpcode() == unsigned(BinOp);
12616 }))
12617 return SDValue();
12618
12619 // Floating-point reductions may require relaxed constraints on the final step
12620 // of the reduction because they may reorder intermediate operations.
12621 unsigned CandidateBinOp = Op.getOpcode();
12622 if (Op.getValueType().isFloatingPoint()) {
12623 SDNodeFlags Flags = Op->getFlags();
12624 switch (CandidateBinOp) {
12625 case ISD::FADD:
12626 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
12627 return SDValue();
12628 break;
12629 default:
12630 llvm_unreachable("Unhandled FP opcode for binop reduction");
12631 }
12632 }
12633
12634 // Matching failed - attempt to see if we did enough stages that a partial
12635 // reduction from a subvector is possible.
12636 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
12637 if (!AllowPartials || !Op)
12638 return SDValue();
12639 EVT OpVT = Op.getValueType();
12640 EVT OpSVT = OpVT.getScalarType();
12641 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
12642 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
12643 return SDValue();
12644 BinOp = (ISD::NodeType)CandidateBinOp;
12645 return getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(Op), SubVT, Op,
12647 };
12648
12649 // At each stage, we're looking for something that looks like:
12650 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
12651 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
12652 // i32 undef, i32 undef, i32 undef, i32 undef>
12653 // %a = binop <8 x i32> %op, %s
12654 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
12655 // we expect something like:
12656 // <4,5,6,7,u,u,u,u>
12657 // <2,3,u,u,u,u,u,u>
12658 // <1,u,u,u,u,u,u,u>
12659 // While a partial reduction match would be:
12660 // <2,3,u,u,u,u,u,u>
12661 // <1,u,u,u,u,u,u,u>
12662 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
12663 SDValue PrevOp;
12664 for (unsigned i = 0; i < Stages; ++i) {
12665 unsigned MaskEnd = (1 << i);
12666
12667 if (Op.getOpcode() != CandidateBinOp)
12668 return PartialReduction(PrevOp, MaskEnd);
12669
12670 SDValue Op0 = Op.getOperand(0);
12671 SDValue Op1 = Op.getOperand(1);
12672
12673 ShuffleVectorSDNode *Shuffle = dyn_cast<ShuffleVectorSDNode>(Op0);
12674 if (Shuffle) {
12675 Op = Op1;
12676 } else {
12677 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
12678 Op = Op0;
12679 }
12680
12681 // The first operand of the shuffle should be the same as the other operand
12682 // of the binop.
12683 if (!Shuffle || Shuffle->getOperand(0) != Op)
12684 return PartialReduction(PrevOp, MaskEnd);
12685
12686 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
12687 for (int Index = 0; Index < (int)MaskEnd; ++Index)
12688 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
12689 return PartialReduction(PrevOp, MaskEnd);
12690
12691 PrevOp = Op;
12692 }
12693
12694 // Handle subvector reductions, which tend to appear after the shuffle
12695 // reduction stages.
12696 while (Op.getOpcode() == CandidateBinOp) {
12697 unsigned NumElts = Op.getValueType().getVectorNumElements();
12698 SDValue Op0 = Op.getOperand(0);
12699 SDValue Op1 = Op.getOperand(1);
12700 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
12702 Op0.getOperand(0) != Op1.getOperand(0))
12703 break;
12704 SDValue Src = Op0.getOperand(0);
12705 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
12706 if (NumSrcElts != (2 * NumElts))
12707 break;
12708 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
12709 Op1.getConstantOperandAPInt(1) == NumElts) &&
12710 !(Op1.getConstantOperandAPInt(1) == 0 &&
12711 Op0.getConstantOperandAPInt(1) == NumElts))
12712 break;
12713 Op = Src;
12714 }
12715
12716 BinOp = (ISD::NodeType)CandidateBinOp;
12717 return Op;
12718}
12719
12721 EVT VT = N->getValueType(0);
12722 EVT EltVT = VT.getVectorElementType();
12723 unsigned NE = VT.getVectorNumElements();
12724
12725 SDLoc dl(N);
12726
12727 // If ResNE is 0, fully unroll the vector op.
12728 if (ResNE == 0)
12729 ResNE = NE;
12730 else if (NE > ResNE)
12731 NE = ResNE;
12732
12733 if (N->getNumValues() == 2) {
12734 SmallVector<SDValue, 8> Scalars0, Scalars1;
12735 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12736 EVT VT1 = N->getValueType(1);
12737 EVT EltVT1 = VT1.getVectorElementType();
12738
12739 unsigned i;
12740 for (i = 0; i != NE; ++i) {
12741 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12742 SDValue Operand = N->getOperand(j);
12743 EVT OperandVT = Operand.getValueType();
12744
12745 // A vector operand; extract a single element.
12746 EVT OperandEltVT = OperandVT.getVectorElementType();
12747 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12748 Operand, getVectorIdxConstant(i, dl));
12749 }
12750
12751 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
12752 Scalars0.push_back(EltOp);
12753 Scalars1.push_back(EltOp.getValue(1));
12754 }
12755
12756 for (; i < ResNE; ++i) {
12757 Scalars0.push_back(getUNDEF(EltVT));
12758 Scalars1.push_back(getUNDEF(EltVT1));
12759 }
12760
12761 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
12762 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
12763 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
12764 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
12765 return getMergeValues({Vec0, Vec1}, dl);
12766 }
12767
12768 assert(N->getNumValues() == 1 &&
12769 "Can't unroll a vector with multiple results!");
12770
12772 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12773
12774 unsigned i;
12775 for (i= 0; i != NE; ++i) {
12776 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12777 SDValue Operand = N->getOperand(j);
12778 EVT OperandVT = Operand.getValueType();
12779 if (OperandVT.isVector()) {
12780 // A vector operand; extract a single element.
12781 EVT OperandEltVT = OperandVT.getVectorElementType();
12782 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12783 Operand, getVectorIdxConstant(i, dl));
12784 } else {
12785 // A scalar operand; just use it as is.
12786 Operands[j] = Operand;
12787 }
12788 }
12789
12790 switch (N->getOpcode()) {
12791 default: {
12792 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
12793 N->getFlags()));
12794 break;
12795 }
12796 case ISD::VSELECT:
12797 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
12798 break;
12799 case ISD::SHL:
12800 case ISD::SRA:
12801 case ISD::SRL:
12802 case ISD::ROTL:
12803 case ISD::ROTR:
12804 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
12806 Operands[1])));
12807 break;
12809 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
12810 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
12811 Operands[0],
12812 getValueType(ExtVT)));
12813 break;
12814 }
12815 case ISD::ADDRSPACECAST: {
12816 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
12817 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
12818 ASC->getSrcAddressSpace(),
12819 ASC->getDestAddressSpace()));
12820 break;
12821 }
12822 }
12823 }
12824
12825 for (; i < ResNE; ++i)
12826 Scalars.push_back(getUNDEF(EltVT));
12827
12828 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
12829 return getBuildVector(VecVT, dl, Scalars);
12830}
12831
12832std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
12833 SDNode *N, unsigned ResNE) {
12834 unsigned Opcode = N->getOpcode();
12835 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
12836 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
12837 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
12838 "Expected an overflow opcode");
12839
12840 EVT ResVT = N->getValueType(0);
12841 EVT OvVT = N->getValueType(1);
12842 EVT ResEltVT = ResVT.getVectorElementType();
12843 EVT OvEltVT = OvVT.getVectorElementType();
12844 SDLoc dl(N);
12845
12846 // If ResNE is 0, fully unroll the vector op.
12847 unsigned NE = ResVT.getVectorNumElements();
12848 if (ResNE == 0)
12849 ResNE = NE;
12850 else if (NE > ResNE)
12851 NE = ResNE;
12852
12853 SmallVector<SDValue, 8> LHSScalars;
12854 SmallVector<SDValue, 8> RHSScalars;
12855 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
12856 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
12857
12858 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
12859 SDVTList VTs = getVTList(ResEltVT, SVT);
12860 SmallVector<SDValue, 8> ResScalars;
12861 SmallVector<SDValue, 8> OvScalars;
12862 for (unsigned i = 0; i < NE; ++i) {
12863 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
12864 SDValue Ov =
12865 getSelect(dl, OvEltVT, Res.getValue(1),
12866 getBoolConstant(true, dl, OvEltVT, ResVT),
12867 getConstant(0, dl, OvEltVT));
12868
12869 ResScalars.push_back(Res);
12870 OvScalars.push_back(Ov);
12871 }
12872
12873 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
12874 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
12875
12876 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
12877 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
12878 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
12879 getBuildVector(NewOvVT, dl, OvScalars));
12880}
12881
12884 unsigned Bytes,
12885 int Dist) const {
12886 if (LD->isVolatile() || Base->isVolatile())
12887 return false;
12888 // TODO: probably too restrictive for atomics, revisit
12889 if (!LD->isSimple())
12890 return false;
12891 if (LD->isIndexed() || Base->isIndexed())
12892 return false;
12893 if (LD->getChain() != Base->getChain())
12894 return false;
12895 EVT VT = LD->getMemoryVT();
12896 if (VT.getSizeInBits() / 8 != Bytes)
12897 return false;
12898
12899 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
12900 auto LocDecomp = BaseIndexOffset::match(LD, *this);
12901
12902 int64_t Offset = 0;
12903 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
12904 return (Dist * (int64_t)Bytes == Offset);
12905 return false;
12906}
12907
12908/// InferPtrAlignment - Infer alignment of a load / store address. Return
12909/// std::nullopt if it cannot be inferred.
12911 // If this is a GlobalAddress + cst, return the alignment.
12912 const GlobalValue *GV = nullptr;
12913 int64_t GVOffset = 0;
12914 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
12915 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
12916 KnownBits Known(PtrWidth);
12918 unsigned AlignBits = Known.countMinTrailingZeros();
12919 if (AlignBits)
12920 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
12921 }
12922
12923 // If this is a direct reference to a stack slot, use information about the
12924 // stack slot's alignment.
12925 int FrameIdx = INT_MIN;
12926 int64_t FrameOffset = 0;
12927 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
12928 FrameIdx = FI->getIndex();
12929 } else if (isBaseWithConstantOffset(Ptr) &&
12930 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
12931 // Handle FI+Cst
12932 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
12933 FrameOffset = Ptr.getConstantOperandVal(1);
12934 }
12935
12936 if (FrameIdx != INT_MIN) {
12938 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
12939 }
12940
12941 return std::nullopt;
12942}
12943
12944/// Split the scalar node with EXTRACT_ELEMENT using the provided
12945/// VTs and return the low/high part.
12946std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
12947 const SDLoc &DL,
12948 const EVT &LoVT,
12949 const EVT &HiVT) {
12950 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
12951 "Split node must be a scalar type");
12952 SDValue Lo =
12954 SDValue Hi =
12956 return std::make_pair(Lo, Hi);
12957}
12958
12959/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
12960/// which is split (or expanded) into two not necessarily identical pieces.
12961std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
12962 // Currently all types are split in half.
12963 EVT LoVT, HiVT;
12964 if (!VT.isVector())
12965 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
12966 else
12967 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
12968
12969 return std::make_pair(LoVT, HiVT);
12970}
12971
12972/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
12973/// type, dependent on an enveloping VT that has been split into two identical
12974/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
12975std::pair<EVT, EVT>
12977 bool *HiIsEmpty) const {
12978 EVT EltTp = VT.getVectorElementType();
12979 // Examples:
12980 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
12981 // custom VL=9 with enveloping VL=8/8 yields 8/1
12982 // custom VL=10 with enveloping VL=8/8 yields 8/2
12983 // etc.
12984 ElementCount VTNumElts = VT.getVectorElementCount();
12985 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
12986 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
12987 "Mixing fixed width and scalable vectors when enveloping a type");
12988 EVT LoVT, HiVT;
12989 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
12990 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12991 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
12992 *HiIsEmpty = false;
12993 } else {
12994 // Flag that hi type has zero storage size, but return split envelop type
12995 // (this would be easier if vector types with zero elements were allowed).
12996 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
12997 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12998 *HiIsEmpty = true;
12999 }
13000 return std::make_pair(LoVT, HiVT);
13001}
13002
13003/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13004/// low/high part.
13005std::pair<SDValue, SDValue>
13006SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13007 const EVT &HiVT) {
13008 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13009 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13010 "Splitting vector with an invalid mixture of fixed and scalable "
13011 "vector types");
13013 N.getValueType().getVectorMinNumElements() &&
13014 "More vector elements requested than available!");
13015 SDValue Lo, Hi;
13016 Lo =
13018 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13019 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13020 // IDX with the runtime scaling factor of the result vector type. For
13021 // fixed-width result vectors, that runtime scaling factor is 1.
13024 return std::make_pair(Lo, Hi);
13025}
13026
13027std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13028 const SDLoc &DL) {
13029 // Split the vector length parameter.
13030 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13031 EVT VT = N.getValueType();
13033 "Expecting the mask to be an evenly-sized vector");
13034 unsigned HalfMinNumElts = VecVT.getVectorMinNumElements() / 2;
13035 SDValue HalfNumElts =
13036 VecVT.isFixedLengthVector()
13037 ? getConstant(HalfMinNumElts, DL, VT)
13038 : getVScale(DL, VT, APInt(VT.getScalarSizeInBits(), HalfMinNumElts));
13039 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13040 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13041 return std::make_pair(Lo, Hi);
13042}
13043
13044/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13046 EVT VT = N.getValueType();
13049 return getNode(ISD::INSERT_SUBVECTOR, DL, WideVT, getUNDEF(WideVT), N,
13051}
13052
13055 unsigned Start, unsigned Count,
13056 EVT EltVT) {
13057 EVT VT = Op.getValueType();
13058 if (Count == 0)
13059 Count = VT.getVectorNumElements();
13060 if (EltVT == EVT())
13061 EltVT = VT.getVectorElementType();
13062 SDLoc SL(Op);
13063 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13064 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Op,
13065 getVectorIdxConstant(i, SL)));
13066 }
13067}
13068
13069// getAddressSpace - Return the address space this GlobalAddress belongs to.
13071 return getGlobal()->getType()->getAddressSpace();
13072}
13073
13076 return Val.MachineCPVal->getType();
13077 return Val.ConstVal->getType();
13078}
13079
13080bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13081 unsigned &SplatBitSize,
13082 bool &HasAnyUndefs,
13083 unsigned MinSplatBits,
13084 bool IsBigEndian) const {
13085 EVT VT = getValueType(0);
13086 assert(VT.isVector() && "Expected a vector type");
13087 unsigned VecWidth = VT.getSizeInBits();
13088 if (MinSplatBits > VecWidth)
13089 return false;
13090
13091 // FIXME: The widths are based on this node's type, but build vectors can
13092 // truncate their operands.
13093 SplatValue = APInt(VecWidth, 0);
13094 SplatUndef = APInt(VecWidth, 0);
13095
13096 // Get the bits. Bits with undefined values (when the corresponding element
13097 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13098 // in SplatValue. If any of the values are not constant, give up and return
13099 // false.
13100 unsigned int NumOps = getNumOperands();
13101 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13102 unsigned EltWidth = VT.getScalarSizeInBits();
13103
13104 for (unsigned j = 0; j < NumOps; ++j) {
13105 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13106 SDValue OpVal = getOperand(i);
13107 unsigned BitPos = j * EltWidth;
13108
13109 if (OpVal.isUndef())
13110 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13111 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13112 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13113 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13114 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13115 else
13116 return false;
13117 }
13118
13119 // The build_vector is all constants or undefs. Find the smallest element
13120 // size that splats the vector.
13121 HasAnyUndefs = (SplatUndef != 0);
13122
13123 // FIXME: This does not work for vectors with elements less than 8 bits.
13124 while (VecWidth > 8) {
13125 // If we can't split in half, stop here.
13126 if (VecWidth & 1)
13127 break;
13128
13129 unsigned HalfSize = VecWidth / 2;
13130 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13131 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13132 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13133 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13134
13135 // If the two halves do not match (ignoring undef bits), stop here.
13136 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13137 MinSplatBits > HalfSize)
13138 break;
13139
13140 SplatValue = HighValue | LowValue;
13141 SplatUndef = HighUndef & LowUndef;
13142
13143 VecWidth = HalfSize;
13144 }
13145
13146 // FIXME: The loop above only tries to split in halves. But if the input
13147 // vector for example is <3 x i16> it wouldn't be able to detect a
13148 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13149 // optimizations. I guess that back in the days when this helper was created
13150 // vectors normally was power-of-2 sized.
13151
13152 SplatBitSize = VecWidth;
13153 return true;
13154}
13155
13157 BitVector *UndefElements) const {
13158 unsigned NumOps = getNumOperands();
13159 if (UndefElements) {
13160 UndefElements->clear();
13161 UndefElements->resize(NumOps);
13162 }
13163 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13164 if (!DemandedElts)
13165 return SDValue();
13166 SDValue Splatted;
13167 for (unsigned i = 0; i != NumOps; ++i) {
13168 if (!DemandedElts[i])
13169 continue;
13170 SDValue Op = getOperand(i);
13171 if (Op.isUndef()) {
13172 if (UndefElements)
13173 (*UndefElements)[i] = true;
13174 } else if (!Splatted) {
13175 Splatted = Op;
13176 } else if (Splatted != Op) {
13177 return SDValue();
13178 }
13179 }
13180
13181 if (!Splatted) {
13182 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13183 assert(getOperand(FirstDemandedIdx).isUndef() &&
13184 "Can only have a splat without a constant for all undefs.");
13185 return getOperand(FirstDemandedIdx);
13186 }
13187
13188 return Splatted;
13189}
13190
13192 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13193 return getSplatValue(DemandedElts, UndefElements);
13194}
13195
13197 SmallVectorImpl<SDValue> &Sequence,
13198 BitVector *UndefElements) const {
13199 unsigned NumOps = getNumOperands();
13200 Sequence.clear();
13201 if (UndefElements) {
13202 UndefElements->clear();
13203 UndefElements->resize(NumOps);
13204 }
13205 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13206 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
13207 return false;
13208
13209 // Set the undefs even if we don't find a sequence (like getSplatValue).
13210 if (UndefElements)
13211 for (unsigned I = 0; I != NumOps; ++I)
13212 if (DemandedElts[I] && getOperand(I).isUndef())
13213 (*UndefElements)[I] = true;
13214
13215 // Iteratively widen the sequence length looking for repetitions.
13216 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
13217 Sequence.append(SeqLen, SDValue());
13218 for (unsigned I = 0; I != NumOps; ++I) {
13219 if (!DemandedElts[I])
13220 continue;
13221 SDValue &SeqOp = Sequence[I % SeqLen];
13223 if (Op.isUndef()) {
13224 if (!SeqOp)
13225 SeqOp = Op;
13226 continue;
13227 }
13228 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
13229 Sequence.clear();
13230 break;
13231 }
13232 SeqOp = Op;
13233 }
13234 if (!Sequence.empty())
13235 return true;
13236 }
13237
13238 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
13239 return false;
13240}
13241
13243 BitVector *UndefElements) const {
13244 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13245 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
13246}
13247
13250 BitVector *UndefElements) const {
13251 return dyn_cast_or_null<ConstantSDNode>(
13252 getSplatValue(DemandedElts, UndefElements));
13253}
13254
13257 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
13258}
13259
13262 BitVector *UndefElements) const {
13263 return dyn_cast_or_null<ConstantFPSDNode>(
13264 getSplatValue(DemandedElts, UndefElements));
13265}
13266
13269 return dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements));
13270}
13271
13272int32_t
13274 uint32_t BitWidth) const {
13275 if (ConstantFPSDNode *CN =
13276 dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements))) {
13277 bool IsExact;
13278 APSInt IntVal(BitWidth);
13279 const APFloat &APF = CN->getValueAPF();
13280 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
13281 APFloat::opOK ||
13282 !IsExact)
13283 return -1;
13284
13285 return IntVal.exactLogBase2();
13286 }
13287 return -1;
13288}
13289
13291 bool IsLittleEndian, unsigned DstEltSizeInBits,
13292 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
13293 // Early-out if this contains anything but Undef/Constant/ConstantFP.
13294 if (!isConstant())
13295 return false;
13296
13297 unsigned NumSrcOps = getNumOperands();
13298 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
13299 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13300 "Invalid bitcast scale");
13301
13302 // Extract raw src bits.
13303 SmallVector<APInt> SrcBitElements(NumSrcOps,
13304 APInt::getZero(SrcEltSizeInBits));
13305 BitVector SrcUndeElements(NumSrcOps, false);
13306
13307 for (unsigned I = 0; I != NumSrcOps; ++I) {
13309 if (Op.isUndef()) {
13310 SrcUndeElements.set(I);
13311 continue;
13312 }
13313 auto *CInt = dyn_cast<ConstantSDNode>(Op);
13314 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
13315 assert((CInt || CFP) && "Unknown constant");
13316 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
13317 : CFP->getValueAPF().bitcastToAPInt();
13318 }
13319
13320 // Recast to dst width.
13321 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
13322 SrcBitElements, UndefElements, SrcUndeElements);
13323 return true;
13324}
13325
13326void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
13327 unsigned DstEltSizeInBits,
13328 SmallVectorImpl<APInt> &DstBitElements,
13329 ArrayRef<APInt> SrcBitElements,
13330 BitVector &DstUndefElements,
13331 const BitVector &SrcUndefElements) {
13332 unsigned NumSrcOps = SrcBitElements.size();
13333 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
13334 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13335 "Invalid bitcast scale");
13336 assert(NumSrcOps == SrcUndefElements.size() &&
13337 "Vector size mismatch");
13338
13339 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
13340 DstUndefElements.clear();
13341 DstUndefElements.resize(NumDstOps, false);
13342 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
13343
13344 // Concatenate src elements constant bits together into dst element.
13345 if (SrcEltSizeInBits <= DstEltSizeInBits) {
13346 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
13347 for (unsigned I = 0; I != NumDstOps; ++I) {
13348 DstUndefElements.set(I);
13349 APInt &DstBits = DstBitElements[I];
13350 for (unsigned J = 0; J != Scale; ++J) {
13351 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13352 if (SrcUndefElements[Idx])
13353 continue;
13354 DstUndefElements.reset(I);
13355 const APInt &SrcBits = SrcBitElements[Idx];
13356 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
13357 "Illegal constant bitwidths");
13358 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
13359 }
13360 }
13361 return;
13362 }
13363
13364 // Split src element constant bits into dst elements.
13365 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
13366 for (unsigned I = 0; I != NumSrcOps; ++I) {
13367 if (SrcUndefElements[I]) {
13368 DstUndefElements.set(I * Scale, (I + 1) * Scale);
13369 continue;
13370 }
13371 const APInt &SrcBits = SrcBitElements[I];
13372 for (unsigned J = 0; J != Scale; ++J) {
13373 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13374 APInt &DstBits = DstBitElements[Idx];
13375 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
13376 }
13377 }
13378}
13379
13381 for (const SDValue &Op : op_values()) {
13382 unsigned Opc = Op.getOpcode();
13383 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
13384 return false;
13385 }
13386 return true;
13387}
13388
13389std::optional<std::pair<APInt, APInt>>
13391 unsigned NumOps = getNumOperands();
13392 if (NumOps < 2)
13393 return std::nullopt;
13394
13395 if (!isa<ConstantSDNode>(getOperand(0)) ||
13396 !isa<ConstantSDNode>(getOperand(1)))
13397 return std::nullopt;
13398
13399 unsigned EltSize = getValueType(0).getScalarSizeInBits();
13400 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
13401 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
13402
13403 if (Stride.isZero())
13404 return std::nullopt;
13405
13406 for (unsigned i = 2; i < NumOps; ++i) {
13407 if (!isa<ConstantSDNode>(getOperand(i)))
13408 return std::nullopt;
13409
13410 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
13411 if (Val != (Start + (Stride * i)))
13412 return std::nullopt;
13413 }
13414
13415 return std::make_pair(Start, Stride);
13416}
13417
13418bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
13419 // Find the first non-undef value in the shuffle mask.
13420 unsigned i, e;
13421 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
13422 /* search */;
13423
13424 // If all elements are undefined, this shuffle can be considered a splat
13425 // (although it should eventually get simplified away completely).
13426 if (i == e)
13427 return true;
13428
13429 // Make sure all remaining elements are either undef or the same as the first
13430 // non-undef value.
13431 for (int Idx = Mask[i]; i != e; ++i)
13432 if (Mask[i] >= 0 && Mask[i] != Idx)
13433 return false;
13434 return true;
13435}
13436
13437// Returns true if it is a constant integer BuildVector or constant integer,
13438// possibly hidden by a bitcast.
13440 SDValue N, bool AllowOpaques) const {
13442
13443 if (auto *C = dyn_cast<ConstantSDNode>(N))
13444 return AllowOpaques || !C->isOpaque();
13445
13447 return true;
13448
13449 // Treat a GlobalAddress supporting constant offset folding as a
13450 // constant integer.
13451 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
13452 if (GA->getOpcode() == ISD::GlobalAddress &&
13453 TLI->isOffsetFoldingLegal(GA))
13454 return true;
13455
13456 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13457 isa<ConstantSDNode>(N.getOperand(0)))
13458 return true;
13459 return false;
13460}
13461
13462// Returns true if it is a constant float BuildVector or constant float.
13464 if (isa<ConstantFPSDNode>(N))
13465 return true;
13466
13468 return true;
13469
13470 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13471 isa<ConstantFPSDNode>(N.getOperand(0)))
13472 return true;
13473
13474 return false;
13475}
13476
13478 bool AllowTruncation) const {
13479 ConstantSDNode *Const = isConstOrConstSplat(N, false, AllowTruncation);
13480 if (!Const)
13481 return std::nullopt;
13482
13483 const APInt &CVal = Const->getAPIntValue();
13484 switch (TLI->getBooleanContents(N.getValueType())) {
13486 if (CVal.isOne())
13487 return true;
13488 if (CVal.isZero())
13489 return false;
13490 return std::nullopt;
13492 if (CVal.isAllOnes())
13493 return true;
13494 if (CVal.isZero())
13495 return false;
13496 return std::nullopt;
13498 return CVal[0];
13499 }
13500 llvm_unreachable("Unknown BooleanContent enum");
13501}
13502
13503void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
13504 assert(!Node->OperandList && "Node already has operands");
13506 "too many operands to fit into SDNode");
13507 SDUse *Ops = OperandRecycler.allocate(
13508 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
13509
13510 bool IsDivergent = false;
13511 for (unsigned I = 0; I != Vals.size(); ++I) {
13512 Ops[I].setUser(Node);
13513 Ops[I].setInitial(Vals[I]);
13514 EVT VT = Ops[I].getValueType();
13515
13516 // Skip Chain. It does not carry divergence.
13517 if (VT != MVT::Other &&
13518 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
13519 Ops[I].getNode()->isDivergent()) {
13520 IsDivergent = true;
13521 }
13522 }
13523 Node->NumOperands = Vals.size();
13524 Node->OperandList = Ops;
13525 if (!TLI->isSDNodeAlwaysUniform(Node)) {
13526 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
13527 Node->SDNodeBits.IsDivergent = IsDivergent;
13528 }
13530}
13531
13534 size_t Limit = SDNode::getMaxNumOperands();
13535 while (Vals.size() > Limit) {
13536 unsigned SliceIdx = Vals.size() - Limit;
13537 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
13538 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
13539 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
13540 Vals.emplace_back(NewTF);
13541 }
13542 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
13543}
13544
13546 EVT VT, SDNodeFlags Flags) {
13547 switch (Opcode) {
13548 default:
13549 return SDValue();
13550 case ISD::ADD:
13551 case ISD::OR:
13552 case ISD::XOR:
13553 case ISD::UMAX:
13554 return getConstant(0, DL, VT);
13555 case ISD::MUL:
13556 return getConstant(1, DL, VT);
13557 case ISD::AND:
13558 case ISD::UMIN:
13559 return getAllOnesConstant(DL, VT);
13560 case ISD::SMAX:
13562 case ISD::SMIN:
13564 case ISD::FADD:
13565 // If flags allow, prefer positive zero since it's generally cheaper
13566 // to materialize on most targets.
13567 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
13568 case ISD::FMUL:
13569 return getConstantFP(1.0, DL, VT);
13570 case ISD::FMINNUM:
13571 case ISD::FMAXNUM: {
13572 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13573 const fltSemantics &Semantics = VT.getFltSemantics();
13574 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
13575 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
13576 APFloat::getLargest(Semantics);
13577 if (Opcode == ISD::FMAXNUM)
13578 NeutralAF.changeSign();
13579
13580 return getConstantFP(NeutralAF, DL, VT);
13581 }
13582 case ISD::FMINIMUM:
13583 case ISD::FMAXIMUM: {
13584 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
13585 const fltSemantics &Semantics = VT.getFltSemantics();
13586 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
13587 : APFloat::getLargest(Semantics);
13588 if (Opcode == ISD::FMAXIMUM)
13589 NeutralAF.changeSign();
13590
13591 return getConstantFP(NeutralAF, DL, VT);
13592 }
13593
13594 }
13595}
13596
13597/// Helper used to make a call to a library function that has one argument of
13598/// pointer type.
13599///
13600/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
13601/// used to get or set floating-point state. They have one argument of pointer
13602/// type, which points to the memory region containing bits of the
13603/// floating-point state. The value returned by such function is ignored in the
13604/// created call.
13605///
13606/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
13607/// \param Ptr Pointer used to save/load state.
13608/// \param InChain Ingoing token chain.
13609/// \returns Outgoing chain token.
13611 SDValue InChain,
13612 const SDLoc &DLoc) {
13613 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
13616 Entry.Node = Ptr;
13617 Entry.Ty = Ptr.getValueType().getTypeForEVT(*getContext());
13618 Args.push_back(Entry);
13619 RTLIB::Libcall LC = static_cast<RTLIB::Libcall>(LibFunc);
13620 SDValue Callee = getExternalSymbol(TLI->getLibcallName(LC),
13621 TLI->getPointerTy(getDataLayout()));
13623 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
13624 TLI->getLibcallCallingConv(LC), Type::getVoidTy(*getContext()), Callee,
13625 std::move(Args));
13626 return TLI->LowerCallTo(CLI).second;
13627}
13628
13630 assert(From && To && "Invalid SDNode; empty source SDValue?");
13631 auto I = SDEI.find(From);
13632 if (I == SDEI.end())
13633 return;
13634
13635 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
13636 // the iterator, hence the need to make a copy to prevent a use-after-free.
13637 NodeExtraInfo NEI = I->second;
13638 if (LLVM_LIKELY(!NEI.PCSections)) {
13639 // No deep copy required for the types of extra info set.
13640 //
13641 // FIXME: Investigate if other types of extra info also need deep copy. This
13642 // depends on the types of nodes they can be attached to: if some extra info
13643 // is only ever attached to nodes where a replacement To node is always the
13644 // node where later use and propagation of the extra info has the intended
13645 // semantics, no deep copy is required.
13646 SDEI[To] = std::move(NEI);
13647 return;
13648 }
13649
13650 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
13651 // through the replacement of From with To. Otherwise, replacements of a node
13652 // (From) with more complex nodes (To and its operands) may result in lost
13653 // extra info where the root node (To) is insignificant in further propagating
13654 // and using extra info when further lowering to MIR.
13655 //
13656 // In the first step pre-populate the visited set with the nodes reachable
13657 // from the old From node. This avoids copying NodeExtraInfo to parts of the
13658 // DAG that is not new and should be left untouched.
13659 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
13660 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
13661 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
13662 if (MaxDepth == 0) {
13663 // Remember this node in case we need to increase MaxDepth and continue
13664 // populating FromReach from this node.
13665 Leafs.emplace_back(N);
13666 return;
13667 }
13668 if (!FromReach.insert(N).second)
13669 return;
13670 for (const SDValue &Op : N->op_values())
13671 Self(Self, Op.getNode(), MaxDepth - 1);
13672 };
13673
13674 // Copy extra info to To and all its transitive operands (that are new).
13676 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
13677 if (FromReach.contains(N))
13678 return true;
13679 if (!Visited.insert(N).second)
13680 return true;
13681 if (getEntryNode().getNode() == N)
13682 return false;
13683 for (const SDValue &Op : N->op_values()) {
13684 if (!Self(Self, Op.getNode()))
13685 return false;
13686 }
13687 // Copy only if entry node was not reached.
13688 SDEI[N] = NEI;
13689 return true;
13690 };
13691
13692 // We first try with a lower MaxDepth, assuming that the path to common
13693 // operands between From and To is relatively short. This significantly
13694 // improves performance in the common case. The initial MaxDepth is big
13695 // enough to avoid retry in the common case; the last MaxDepth is large
13696 // enough to avoid having to use the fallback below (and protects from
13697 // potential stack exhaustion from recursion).
13698 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
13699 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
13700 // StartFrom is the previous (or initial) set of leafs reachable at the
13701 // previous maximum depth.
13703 std::swap(StartFrom, Leafs);
13704 for (const SDNode *N : StartFrom)
13705 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
13706 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
13707 return;
13708 // This should happen very rarely (reached the entry node).
13709 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
13710 assert(!Leafs.empty());
13711 }
13712
13713 // This should not happen - but if it did, that means the subgraph reachable
13714 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
13715 // could not visit all reachable common operands. Consequently, we were able
13716 // to reach the entry node.
13717 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
13718 assert(false && "From subgraph too complex - increase max. MaxDepth?");
13719 // Best-effort fallback if assertions disabled.
13720 SDEI[To] = std::move(NEI);
13721}
13722
13723#ifndef NDEBUG
13724static void checkForCyclesHelper(const SDNode *N,
13727 const llvm::SelectionDAG *DAG) {
13728 // If this node has already been checked, don't check it again.
13729 if (Checked.count(N))
13730 return;
13731
13732 // If a node has already been visited on this depth-first walk, reject it as
13733 // a cycle.
13734 if (!Visited.insert(N).second) {
13735 errs() << "Detected cycle in SelectionDAG\n";
13736 dbgs() << "Offending node:\n";
13737 N->dumprFull(DAG); dbgs() << "\n";
13738 abort();
13739 }
13740
13741 for (const SDValue &Op : N->op_values())
13742 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
13743
13744 Checked.insert(N);
13745 Visited.erase(N);
13746}
13747#endif
13748
13750 const llvm::SelectionDAG *DAG,
13751 bool force) {
13752#ifndef NDEBUG
13753 bool check = force;
13754#ifdef EXPENSIVE_CHECKS
13755 check = true;
13756#endif // EXPENSIVE_CHECKS
13757 if (check) {
13758 assert(N && "Checking nonexistent SDNode");
13761 checkForCyclesHelper(N, visited, checked, DAG);
13762 }
13763#endif // !NDEBUG
13764}
13765
13766void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
13767 checkForCycles(DAG->getRoot().getNode(), DAG, force);
13768}
static bool isConstant(const MachineInstr &MI)
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
This file implements the BitVector class.
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
static std::optional< bool > isBigEndian(const SmallDenseMap< int64_t, int64_t, 8 > &MemOffset2Idx, int64_t LowestIdx)
Given a map from byte offsets in memory to indices in a load/store, determine if that map corresponds...
#define __asan_unpoison_memory_region(p, size)
Definition: Compiler.h:553
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:319
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(...)
Definition: Debug.h:106
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
uint32_t Index
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
This file defines a hash set that can be used to remove duplication of nodes in a graph.
#define _
iv users
Definition: IVUsers.cpp:48
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:557
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
Remove Loads Into Fake Uses
Contains matchers for matching SelectionDAG nodes and values.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static Type * getValueType(Value *V)
Returns the type of the given value/instruction V.
This file contains some templates that are useful if you are working with the STL at all.
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow)
static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo, BatchAAResults *BatchAA)
static SDValue getMemsetStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo)
Lower the call to 'memset' intrinsic function into a series of store operations.
static std::optional< APInt > FoldValueWithUndef(unsigned Opcode, const APInt &C1, bool IsUndef1, const APInt &C2, bool IsUndef2)
static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step, SelectionDAG &DAG)
static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC, SDVTList VTList, ArrayRef< SDValue > OpList)
static SDValue getMemsetStringVal(EVT VT, const SDLoc &dl, SelectionDAG &DAG, const TargetLowering &TLI, const ConstantDataArraySlice &Slice)
getMemsetStringVal - Similar to getMemsetValue.
static cl::opt< bool > EnableMemCpyDAGOpt("enable-memcpy-dag-opt", cl::Hidden, cl::init(true), cl::desc("Gang up loads and stores generated by inlining of memcpy"))
static bool haveNoCommonBitsSetCommutative(SDValue A, SDValue B)
static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList)
AddNodeIDValueTypes - Value type lists are intern'd so we can represent them solely with their pointe...
static void commuteShuffle(SDValue &N1, SDValue &N2, MutableArrayRef< int > M)
Swaps the values of N1 and N2.
static bool isMemSrcFromConstant(SDValue Src, ConstantDataArraySlice &Slice)
Returns true if memcpy source is constant data.
static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo)
static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC)
AddNodeIDOpcode - Add the node opcode to the NodeID data.
static ISD::CondCode getSetCCInverseImpl(ISD::CondCode Op, bool isIntegerLike)
static bool doNotCSE(SDNode *N)
doNotCSE - Return true if CSE should not be performed for this node.
static cl::opt< int > MaxLdStGlue("ldstmemcpy-glue-max", cl::desc("Number limit for gluing ld/st of memcpy."), cl::Hidden, cl::init(0))
static void AddNodeIDOperands(FoldingSetNodeID &ID, ArrayRef< SDValue > Ops)
AddNodeIDOperands - Various routines for adding operands to the NodeID data.
static bool canFoldStoreIntoLibCallOutputPointers(StoreSDNode *StoreNode, SDNode *FPNode)
Given a store node StoreNode, return true if it is safe to fold that node into FPNode,...
static SDValue foldCONCAT_VECTORS(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
Try to simplify vector concatenation to an input value, undef, or build vector.
static MachinePointerInfo InferPointerInfo(const MachinePointerInfo &Info, SelectionDAG &DAG, SDValue Ptr, int64_t Offset=0)
InferPointerInfo - If the specified ptr/offset is a frame index, infer a MachinePointerInfo record fr...
static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N)
If this is an SDNode with special info, add this info to the NodeID data.
static bool gluePropagatesDivergence(const SDNode *Node)
Return true if a glue output should propagate divergence information.
static void NewSDValueDbgMsg(SDValue V, StringRef Msg, SelectionDAG *G)
static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs)
makeVTList - Return an instance of the SDVTList struct initialized with the specified members.
static void 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 cl::opt< unsigned > MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192), cl::desc("DAG combiner limit number of steps when searching DAG " "for predecessor nodes"))
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
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:245
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
Value * RHS
Value * LHS
static unsigned getSize(unsigned Kind)
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition: APFloat.h:1122
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1210
void copySign(const APFloat &RHS)
Definition: APFloat.h:1304
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5463
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1192
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:1428
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1183
bool isFinite() const
Definition: APFloat.h:1450
bool isNaN() const
Definition: APFloat.h:1443
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1334
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1201
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition: APFloat.h:1237
bool isZero() const
Definition: APFloat.h:1441
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1140
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:1326
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:1100
opStatus mod(const APFloat &RHS)
Definition: APFloat.h:1228
bool isPosZero() const
Definition: APFloat.h:1456
bool isNegZero() const
Definition: APFloat.h:1457
void changeSign()
Definition: APFloat.h:1299
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition: APFloat.h:1111
bool isInfinity() const
Definition: APFloat.h:1442
Class for arbitrary precision integers.
Definition: APInt.h:78
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1945
APInt usub_sat(const APInt &RHS) const
Definition: APInt.cpp:2029
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1547
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:234
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1407
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:986
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:229
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1520
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition: APInt.h:1392
unsigned popcount() const
Count the number of bits set.
Definition: APInt.h:1649
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition: APInt.h:1386
APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition: APInt.cpp:612
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:1007
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1492
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:910
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition: APInt.h:1330
APInt abs() const
Get the absolute value.
Definition: APInt.h:1773
APInt sadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2000
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:371
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1182
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:380
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1640
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1468
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1111
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:209
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:329
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1618
void clearAllBits()
Set every bit to 0.
Definition: APInt.h:1397
APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition: APInt.cpp:1128
APInt reverseBits() const
Definition: APInt.cpp:741
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:834
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1166
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1618
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition: APInt.h:1607
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition: APInt.h:1577
static APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition: APInt.cpp:624
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:219
APInt sshl_sat(const APInt &RHS) const
Definition: APInt.cpp:2060
APInt ushl_sat(const APInt &RHS) const
Definition: APInt.cpp:2074
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1015
APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition: APInt.cpp:1115
void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition: APInt.cpp:370
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition: APInt.h:1417
unsigned logBase2() const
Definition: APInt.h:1739
APInt uadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2010
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:827
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1319
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition: APInt.cpp:1710
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:334
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1150
APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:959
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition: APInt.h:1367
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:873
APInt byteSwap() const
Definition: APInt.cpp:719
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition: APInt.h:1257
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:440
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:306
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:200
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1389
APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition: APInt.cpp:455
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition: APInt.h:1237
bool isOne() const
Determine if this is a value of 1.
Definition: APInt.h:389
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:286
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:239
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:858
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:851
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1221
APInt ssub_sat(const APInt &RHS) const
Definition: APInt.cpp:2019
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
unsigned getSrcAddressSpace() const
unsigned getDestAddressSpace() const
Recycle small arrays allocated from a BumpPtrAllocator.
Definition: ArrayRecycler.h:28
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:157
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:168
iterator begin() const
Definition: ArrayRef.h:156
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:163
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:198
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.
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
BitVector & reset()
Definition: BitVector.h:392
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition: BitVector.h:341
void clear()
clear - Removes all bits from the bitvector.
Definition: BitVector.h:335
BitVector & set()
Definition: BitVector.h:351
bool none() const
none - Returns true if none of the bits are set.
Definition: BitVector.h:188
size_type size() const
size - Returns the number of bits in this bitvector.
Definition: BitVector.h:159
unsigned getTargetFlags() const
const BlockAddress * getBlockAddress() const
The address of a basic block.
Definition: Constants.h:893
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
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
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:271
const APFloat & getValue() const
Definition: Constants.h:315
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition: Constants.h:151
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:148
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...
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
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:42
Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
Definition: Constants.cpp:1708
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:435
DWARF expression.
static 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:63
bool isLittleEndian() const
Layout endianness...
Definition: DataLayout.h:197
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:851
Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:843
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:743
Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Definition: DataLayout.cpp:847
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:278
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 hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition: Function.h:716
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:365
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:369
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:264
unsigned getAddressSpace() const
Definition: GlobalValue.h:206
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:657
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:295
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:1073
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 TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
LocationSize getSize() const
Return the size in bytes of the memory reference.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
const MachinePointerInfo & getPointerInfo() const
Flags getFlags() const
Return the raw flags of the source value,.
This class contains meta information specific to a module.
An SDNode that represents everything that will be needed to construct a MachineInstr.
This class is used to represent an MGATHER node.
This class is used to represent an MLOAD node.
This class is used to represent an MSCATTER node.
This class is used to represent an MSTORE node.
This SDNode is used for target intrinsics that touch memory and need an associated MachineMemOperand.
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:228
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:310
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:686
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:703
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.
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
Keeps track of dbg_value information through SDISel.
Definition: SelectionDAG.h:162
BumpPtrAllocator & getAlloc()
Definition: SelectionDAG.h:191
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:197
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.
iterator_range< use_iterator > uses()
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.
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
void DropOperands()
Release the operands and set this node to have zero operands.
Represents a use of a SDNode.
EVT getValueType() const
Convenience function for get().getValueType().
SDNode * getUser()
This returns the SDNode that contains this Use.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
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 bool isTargetMemoryOpcode(unsigned Opcode) const
Returns true if a node with the given target-specific opcode has a memory operand.
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:389
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:228
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:577
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:499
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.
SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
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.
SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=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 getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo=AAMDNodes())
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)
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...
bool isConstantIntBuildVectorOrConstantInt(SDValue N, bool AllowOpaques=true) const
Test whether the given value is a constant int or similar node.
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.
static unsigned getHasPredecessorMaxSteps()
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.
SDValue getRegister(Register Reg, EVT VT)
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.
SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
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:503
bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
Definition: SelectionDAG.h:458
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:397
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:557
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:857
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.
bool isConstantFPBuildVectorOrConstantFP(SDValue N) const
Test whether the given value is a constant FP or similar node.
const DataLayout & getDataLayout() const
Definition: SelectionDAG.h:497
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 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 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 getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
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:891
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.
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 getPartialReduceAdd(SDLoc DL, EVT ReducedTy, SDValue Op1, SDValue Op2)
Create the DAG equivalent of vector_partial_reduce where Op1 and Op2 are its operands and ReducedTY i...
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)
SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
Definition: SelectionDAG.h:498
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:569
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:565
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:701
const TargetLibraryInfo & getLibInfo() const
Definition: SelectionDAG.h:504
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 getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
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:492
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:874
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.
bool expandMultipleResultFPLibCall(RTLIB::Libcall LC, SDNode *Node, SmallVectorImpl< SDValue > &Results, std::optional< unsigned > CallRetResNo={})
Expands a node with multiple results to an FP or vector libcall.
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:510
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:586
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.
void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE, Pass *PassPtr, const TargetLibraryInfo *LibraryInfo, UniformityInfo *UA, ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin, MachineModuleInfo &MMI, FunctionVarLocs const *FnVarLocs)
Prepare this SelectionDAG to process code in the given MachineFunction.
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)
std::optional< bool > isBoolConstant(SDValue N, bool AllowTruncation=false) const
Check if a value \op N is a constant using the target's BooleanContent for its type.
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:580
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:907
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:560
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.
size_type size() const
Definition: SmallPtrSet.h:94
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:363
bool erase(PtrType Ptr)
Remove pointer from the set.
Definition: SmallPtrSet.h:401
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:452
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:384
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
bool empty() const
Definition: SmallVector.h:81
size_t size() const
Definition: SmallVector.h:78
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:704
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:937
void reserve(size_type N)
Definition: SmallVector.h:663
iterator erase(const_iterator CI)
Definition: SmallVector.h:737
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:683
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
This class is used to represent ISD::STORE nodes.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:144
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
Completely target-dependent object reference.
unsigned getTargetFlags() const
Provides information about what library functions are available for the current target.
const VecDesc * getVectorMappingInfo(StringRef F, const ElementCount &VF, bool Masked) const
CallingConv::ID getLibcallCallingConv(RTLIB::Libcall Call) const
Get the CallingConv that should be used for the specified libcall.
virtual bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const
Return true if it is beneficial to convert a load of a constant to just the constant itself.
virtual bool isSExtCheaperThanZExt(EVT FromTy, EVT ToTy) const
Return true if sign-extension from FromTy to ToTy is cheaper than zero-extension.
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,...
EVT getShiftAmountTy(EVT LHSTy, const DataLayout &DL) const
Returns the type for the shift amount of a shift opcode.
virtual bool isExtractSubvectorCheap(EVT ResVT, EVT SrcVT, unsigned Index) const
Return true if EXTRACT_SUBVECTOR is cheap for extracting this result type from this source type with ...
virtual bool shallExtractConstSplatVectorElementToStore(Type *VectorTy, unsigned ElemSizeInBits, unsigned &Index) const
Return true if the target shall perform extract vector element and store given that the vector is kno...
virtual bool isTruncateFree(Type *FromTy, Type *ToTy) const
Return true if it's free to truncate a value of type FromTy to type ToTy.
virtual EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const
Return the ValueType of the result of SETCC operations.
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
BooleanContent getBooleanContents(bool isVec, bool isFloat) const
For targets without i1 registers, this gives the nature of the high-bits of boolean values held in ty...
bool isCondCodeLegal(ISD::CondCode CC, MVT VT) const
Return true if the specified condition code is legal for a comparison of the specified types on this ...
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
BooleanContent
Enum that describes how the target represents true/false values.
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
Align getMinStackArgumentAlignment() const
Return the minimum stack alignment of an argument.
LegalizeTypeAction getTypeAction(LLVMContext &Context, EVT VT) const
Return how we should legalize values of this type, either it is already legal (return 'Legal') or we ...
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
std::vector< ArgListEntry > ArgListTy
virtual bool hasVectorBlend() const
Return true if the target has a vector blend instruction.
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
MVT getFrameIndexTy(const DataLayout &DL) const
Return the type for frame index, which is determined by the alloca address space specified through th...
virtual bool isLegalStoreImmediate(int64_t Value) const
Return true if the specified immediate is legal for the value input of a store instruction.
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:80
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:588
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:345
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:270
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.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:355
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
void set(Value *Val)
Definition: Value.h:892
User * getUser() const
Returns the User that contains this Use.
Definition: Use.h:64
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
Provides info so a possible vectorization of a function can be computed.
bool isMasked() const
StringRef getVectorFnName() const
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:213
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition: DenseSet.h:193
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:202
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:232
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:179
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:239
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
#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:3104
const APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition: APInt.h:2242
APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition: APInt.cpp:3091
APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition: APInt.cpp:3081
const APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition: APInt.h:2237
APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition: APInt.cpp:3096
APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition: APInt.cpp:2982
APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition: APInt.cpp:3076
APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition: APInt.cpp:3086
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
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:780
@ 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:753
@ 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:1242
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition: ISDOpcodes.h:491
@ ATOMIC_LOAD_FMAX
Definition: ISDOpcodes.h:1347
@ 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:1417
@ MLOAD
Masked load and store - consecutive vector load and store operations with additional mask operand tha...
Definition: ISDOpcodes.h:1360
@ VECREDUCE_SMIN
Definition: ISDOpcodes.h:1450
@ FGETSIGN
INT = FGETSIGN(FP) - Return the sign bit of the specified floating point value as an integer 0/1 valu...
Definition: ISDOpcodes.h:512
@ 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:1340
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition: ISDOpcodes.h:574
@ JUMP_TABLE_DEBUG_INFO
JUMP_TABLE_DEBUG_INFO - Jumptable debug info.
Definition: ISDOpcodes.h:1131
@ BSWAP
Byte Swap and Counting operators.
Definition: ISDOpcodes.h:744
@ TargetBlockAddress
Definition: ISDOpcodes.h:176
@ ConstantFP
Definition: ISDOpcodes.h:77
@ ATOMIC_LOAD_MAX
Definition: ISDOpcodes.h:1342
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
Definition: ISDOpcodes.h:1312
@ ATOMIC_LOAD_UMIN
Definition: ISDOpcodes.h:1343
@ 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:502
@ 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:1102
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:814
@ ATOMIC_LOAD_USUB_COND
Definition: ISDOpcodes.h:1351
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition: ISDOpcodes.h:498
@ FATAN2
FATAN2 - atan2, inspired by libm.
Definition: ISDOpcodes.h:999
@ 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:1325
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition: ISDOpcodes.h:841
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition: ISDOpcodes.h:558
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
Definition: ISDOpcodes.h:1435
@ 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:1439
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition: ISDOpcodes.h:717
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition: ISDOpcodes.h:871
@ VECREDUCE_SMAX
Definition: ISDOpcodes.h:1449
@ STRICT_FSETCCS
Definition: ISDOpcodes.h:492
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
Definition: ISDOpcodes.h:964
@ ATOMIC_LOAD_OR
Definition: ISDOpcodes.h:1338
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:954
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition: ISDOpcodes.h:236
@ ATOMIC_LOAD_XOR
Definition: ISDOpcodes.h:1339
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
Definition: ISDOpcodes.h:997
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
Definition: ISDOpcodes.h:1494
@ ATOMIC_LOAD_FADD
Definition: ISDOpcodes.h:1345
@ 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:1238
@ 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:1173
@ ATOMIC_LOAD_USUB_SAT
Definition: ISDOpcodes.h:1352
@ SIGN_EXTEND
Conversion operators.
Definition: ISDOpcodes.h:805
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition: ISDOpcodes.h:685
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition: ISDOpcodes.h:635
@ TargetExternalSymbol
Definition: ISDOpcodes.h:175
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
Definition: ISDOpcodes.h:1432
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition: ISDOpcodes.h:752
@ 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:1292
@ VECREDUCE_FMIN
Definition: ISDOpcodes.h:1436
@ 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:788
@ FNEG
Perform various unary floating-point operations inspired by libm.
Definition: ISDOpcodes.h:981
@ ATOMIC_LOAD_FSUB
Definition: ISDOpcodes.h:1346
@ BR_CC
BR_CC - Conditional branch.
Definition: ISDOpcodes.h:1148
@ SSUBO
Same for subtraction.
Definition: ISDOpcodes.h:334
@ ATOMIC_LOAD_MIN
Definition: ISDOpcodes.h:1341
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition: ISDOpcodes.h:661
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition: ISDOpcodes.h:515
@ 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:757
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
Definition: ISDOpcodes.h:1308
@ UNDEF
UNDEF - An undefined node.
Definition: ISDOpcodes.h:218
@ VECREDUCE_UMAX
Definition: ISDOpcodes.h:1451
@ 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:642
@ 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:1348
@ 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:1444
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition: ISDOpcodes.h:674
@ SHL
Shift and rotation operations.
Definition: ISDOpcodes.h:735
@ ATOMIC_LOAD_CLR
Definition: ISDOpcodes.h:1337
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition: ISDOpcodes.h:615
@ ATOMIC_LOAD_AND
Definition: ISDOpcodes.h:1336
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition: ISDOpcodes.h:588
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
Definition: ISDOpcodes.h:1044
@ 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:550
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition: ISDOpcodes.h:209
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:811
@ TargetConstantFP
Definition: ISDOpcodes.h:165
@ FP_TO_UINT_SAT
Definition: ISDOpcodes.h:907
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition: ISDOpcodes.h:772
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
Definition: ISDOpcodes.h:1407
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
Definition: ISDOpcodes.h:1319
@ ATOMIC_LOAD_UMAX
Definition: ISDOpcodes.h:1344
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
Definition: ISDOpcodes.h:1031
@ 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:860
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition: ISDOpcodes.h:849
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition: ISDOpcodes.h:697
@ LIFETIME_START
This corresponds to the llvm.lifetime.
Definition: ISDOpcodes.h:1377
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:939
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition: ISDOpcodes.h:766
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:310
@ MGATHER
Masked gather and scatter - load and store operations for a vector of random addresses with additiona...
Definition: ISDOpcodes.h:1372
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
Definition: ISDOpcodes.h:1262
@ VECREDUCE_UMIN
Definition: ISDOpcodes.h:1452
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
Definition: ISDOpcodes.h:973
@ ATOMIC_LOAD_UDEC_WRAP
Definition: ISDOpcodes.h:1350
@ ATOMIC_LOAD_ADD
Definition: ISDOpcodes.h:1334
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition: ISDOpcodes.h:480
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
Definition: ISDOpcodes.h:1050
@ ATOMIC_LOAD_SUB
Definition: ISDOpcodes.h:1335
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:887
@ 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:485
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:709
@ 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:1078
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
Definition: ISDOpcodes.h:1402
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition: ISDOpcodes.h:705
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition: ISDOpcodes.h:680
@ VECREDUCE_FMUL
Definition: ISDOpcodes.h:1433
@ 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:651
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition: ISDOpcodes.h:223
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition: ISDOpcodes.h:539
@ 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:627
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
Definition: ISDOpcodes.h:1333
@ ExternalSymbol
Definition: ISDOpcodes.h:83
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
Definition: ISDOpcodes.h:1004
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition: ISDOpcodes.h:920
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition: ISDOpcodes.h:669
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition: ISDOpcodes.h:882
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition: ISDOpcodes.h:958
@ EXPERIMENTAL_VECTOR_HISTOGRAM
Definition: ISDOpcodes.h:1481
@ 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:906
@ VECREDUCE_FMINIMUM
Definition: ISDOpcodes.h:1440
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:817
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
Definition: ISDOpcodes.h:1217
@ BlockAddress
Definition: ISDOpcodes.h:84
@ VECREDUCE_SEQ_FMUL
Definition: ISDOpcodes.h:1418
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition: ISDOpcodes.h:794
@ 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:1349
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition: ISDOpcodes.h:508
@ 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
@ CALLSEQ_START
CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of a call sequence,...
Definition: ISDOpcodes.h:1211
@ SET_FPENV_MEM
Sets the current floating point environment.
Definition: ISDOpcodes.h:1083
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
Definition: ISDOpcodes.h:1055
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition: ISDOpcodes.h:692
@ 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:530
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 isNormalStore(const SDNode *N)
Returns true if the specified node is a non-truncating and unindexed store.
bool isZEXTLoad(const SDNode *N)
Returns true if the specified node is a ZEXTLOAD.
bool matchUnaryFpPredicate(SDValue Op, std::function< bool(ConstantFPSDNode *)> Match, bool AllowUndefs=false)
Hook for matching ConstantFPSDNode predicate.
bool isExtOpcode(unsigned Opcode)
Definition: ISDOpcodes.h:1681
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 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:1668
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:1673
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).
std::optional< unsigned > getVPForBaseOpcode(unsigned Opcode)
Translate this non-VP Opcode to its corresponding VP Opcode.
MemIndexType
MemIndexType enum - This enum defines how to interpret MGATHER/SCATTER's index parameter when calcula...
Definition: ISDOpcodes.h:1572
bool isBuildVectorAllZeros(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are 0 or undef.
bool matchUnaryPredicateImpl(SDValue Op, std::function< bool(ConstNodeType *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant BUI...
bool isConstantSplatVector(const SDNode *N, APInt &SplatValue)
Node predicates.
NodeType getInverseMinMaxOpcode(unsigned MinMaxOpc)
Given a MinMaxOpc of ISD::(U|S)MIN or ISD::(U|S)MAX, returns ISD::(U|S)MAX and ISD::(U|S)MIN,...
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.
bool matchUnaryPredicate(SDValue Op, std::function< bool(ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Hook for matching ConstantSDNode predicate.
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
Definition: ISDOpcodes.h:1559
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:1610
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:1590
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:903
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
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:354
@ 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:1739
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:256
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:1565
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 tuples (A, B,...
Definition: STLExtras.h:2448
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition: bit.h:307
void * PointerTy
Definition: GenericValue.h:21
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:1604
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2115
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:657
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:1547
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:1526
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:1746
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:1563
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:341
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:292
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:1664
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition: APFloat.h:1590
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:1753
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:1549
@ 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...
bool isInTailCallPosition(const CallBase &Call, const TargetMachine &TM, bool ReturnsFirstArg=false)
Test if the given instruction is in a position to be optimized with a tail-call.
Definition: Analysis.cpp:535
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:1841
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:217
bool funcReturnsFirstArgOfCall(const CallInst &CI)
Returns true if the parent of CI returns CI's first argument after calling CI.
Definition: Analysis.cpp:715
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:1903
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:582
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:1577
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition: APFloat.h:1617
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:383
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:764
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition: Metadata.h:784
MDNode * TBAA
The tag for type-based alias analysis.
Definition: Metadata.h:781
static const fltSemantics & IEEEsingle() LLVM_READNONE
Definition: APFloat.cpp:257
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition: APFloat.h:292
static constexpr roundingMode rmTowardNegative
Definition: APFloat.h:305
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:302
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:306
static const fltSemantics & IEEEquad() LLVM_READNONE
Definition: APFloat.cpp:259
static const fltSemantics & IEEEdouble() LLVM_READNONE
Definition: APFloat.cpp:258
static const fltSemantics & IEEEhalf() LLVM_READNONE
Definition: APFloat.cpp:255
static constexpr roundingMode rmTowardPositive
Definition: APFloat.h:304
static const fltSemantics & BFloat() LLVM_READNONE
Definition: APFloat.cpp:256
opStatus
IEEE-754R 7: Default exception handling.
Definition: APFloat.h:318
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
void move(uint64_t Delta)
Moves the Offset and adjusts Length accordingly.
const ConstantDataArray * Array
ConstantDataArray pointer.
Extended Value Type.
Definition: ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition: ValueTypes.h:390
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition: ValueTypes.h:137
intptr_t getRawBits() const
Definition: ValueTypes.h:507
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition: ValueTypes.h:74
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition: ValueTypes.h:279
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition: ValueTypes.h:295
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition: ValueTypes.h:147
ElementCount getVectorElementCount() const
Definition: ValueTypes.h:345
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:368
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition: ValueTypes.h:354
uint64_t getScalarSizeInBits() const
Definition: ValueTypes.h:380
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:311
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition: ValueTypes.h:65
bool isFixedLengthVector() const
Definition: ValueTypes.h:181
bool isVector() const
Return true if this is a vector value type.
Definition: ValueTypes.h:168
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition: ValueTypes.h:318
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition: ValueTypes.h:287
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition: ValueTypes.h:251
Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:210
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition: ValueTypes.h:174
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition: ValueTypes.h:323
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition: ValueTypes.h:142
const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
Definition: ValueTypes.cpp:320
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition: ValueTypes.h:331
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition: ValueTypes.h:303
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition: ValueTypes.h:448
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition: ValueTypes.h:152
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition: KnownBits.h:293
KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
Definition: KnownBits.cpp:158
static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
Definition: KnownBits.cpp:909
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition: KnownBits.h:247
static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
Definition: KnownBits.cpp:211
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:100
bool isZero() const
Returns true if value is all zero.
Definition: KnownBits.h:79
void makeNonNegative()
Make this value non-negative.
Definition: KnownBits.h:116
static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
Definition: KnownBits.cpp:774
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:234
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:1049
bool isUnknown() const
Returns true if we don't know any bits.
Definition: KnownBits.h:65
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:266
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:111
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition: KnownBits.h:153
KnownBits byteSwap() const
Definition: KnownBits.h:471
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:281
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition: KnownBits.h:85
KnownBits reverseBits() const
Definition: KnownBits.h:475
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition: KnownBits.h:225
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:43
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
Definition: KnownBits.cpp:187
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition: KnownBits.h:164
void resetAll()
Resets the known state of all bits.
Definition: KnownBits.h:73
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:313
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:103
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:217
static KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorU.
Definition: KnownBits.cpp:792
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition: KnownBits.h:303
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition: KnownBits.h:172
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:146
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition: KnownBits.h:188
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:137
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:901
static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition: KnownBits.cpp:1066
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition: KnownBits.cpp:1009
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:60
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition: KnownBits.h:106
static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
Definition: KnownBits.cpp:953
static KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
Definition: KnownBits.cpp:788
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition: KnownBits.h:318
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:97
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:53
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition: KnownBits.h:272
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition: KnownBits.h:211
static KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
Definition: KnownBits.cpp:800
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:804
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:159
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:205
static KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
Definition: KnownBits.cpp:796
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.
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:317
DAGUpdateListener *const Next
Definition: SelectionDAG.h:318
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)