LLVM 23.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"
74#include <algorithm>
75#include <cassert>
76#include <cstdint>
77#include <cstdlib>
78#include <limits>
79#include <optional>
80#include <string>
81#include <utility>
82#include <vector>
83
84using namespace llvm;
85using namespace llvm::SDPatternMatch;
86
87/// makeVTList - Return an instance of the SDVTList struct initialized with the
88/// specified members.
89static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
90 SDVTList Res = {VTs, NumVTs};
91 return Res;
92}
93
94// Default null implementations of the callbacks.
98
99void SelectionDAG::DAGNodeDeletedListener::anchor() {}
100void SelectionDAG::DAGNodeInsertedListener::anchor() {}
101
102#define DEBUG_TYPE "selectiondag"
103
104static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
105 cl::Hidden, cl::init(true),
106 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
107
108static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
109 cl::desc("Number limit for gluing ld/st of memcpy."),
110 cl::Hidden, cl::init(0));
111
113 MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192),
114 cl::desc("DAG combiner limit number of steps when searching DAG "
115 "for predecessor nodes"));
116
118 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
119}
120
122
123//===----------------------------------------------------------------------===//
124// ConstantFPSDNode Class
125//===----------------------------------------------------------------------===//
126
127/// isExactlyValue - We don't rely on operator== working on double values, as
128/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
129/// As such, this method can be used to do an exact bit-for-bit comparison of
130/// two floating point values.
132 return getValueAPF().bitwiseIsEqual(V);
133}
134
136 const APFloat& Val) {
137 assert(VT.isFloatingPoint() && "Can only convert between FP types");
138
139 // convert modifies in place, so make a copy.
140 APFloat Val2 = APFloat(Val);
141 bool losesInfo;
143 &losesInfo);
144 return !losesInfo;
145}
146
147//===----------------------------------------------------------------------===//
148// ISD Namespace
149//===----------------------------------------------------------------------===//
150
151bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
152 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
153 if (auto OptAPInt = N->getOperand(0)->bitcastToAPInt()) {
154 unsigned EltSize =
155 N->getValueType(0).getVectorElementType().getSizeInBits();
156 SplatVal = OptAPInt->trunc(EltSize);
157 return true;
158 }
159 }
160
161 auto *BV = dyn_cast<BuildVectorSDNode>(N);
162 if (!BV)
163 return false;
164
165 APInt SplatUndef;
166 unsigned SplatBitSize;
167 bool HasUndefs;
168 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
169 // Endianness does not matter here. We are checking for a splat given the
170 // element size of the vector, and if we find such a splat for little endian
171 // layout, then that should be valid also for big endian (as the full vector
172 // size is known to be a multiple of the element size).
173 const bool IsBigEndian = false;
174 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
175 EltSize, IsBigEndian) &&
176 EltSize == SplatBitSize;
177}
178
179// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
180// specializations of the more general isConstantSplatVector()?
181
182bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
183 // Look through a bit convert.
184 while (N->getOpcode() == ISD::BITCAST)
185 N = N->getOperand(0).getNode();
186
187 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
188 APInt SplatVal;
189 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
190 }
191
192 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
193
194 unsigned i = 0, e = N->getNumOperands();
195
196 // Skip over all of the undef values.
197 while (i != e && N->getOperand(i).isUndef())
198 ++i;
199
200 // Do not accept an all-undef vector.
201 if (i == e) return false;
202
203 // Do not accept build_vectors that aren't all constants or which have non-~0
204 // elements. We have to be a bit careful here, as the type of the constant
205 // may not be the same as the type of the vector elements due to type
206 // legalization (the elements are promoted to a legal type for the target and
207 // a vector of a type may be legal when the base element type is not).
208 // We only want to check enough bits to cover the vector elements, because
209 // we care if the resultant vector is all ones, not whether the individual
210 // constants are.
211 SDValue NotZero = N->getOperand(i);
212 if (auto OptAPInt = NotZero->bitcastToAPInt()) {
213 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
214 if (OptAPInt->countr_one() < EltSize)
215 return false;
216 } else
217 return false;
218
219 // Okay, we have at least one ~0 value, check to see if the rest match or are
220 // undefs. Even with the above element type twiddling, this should be OK, as
221 // the same type legalization should have applied to all the elements.
222 for (++i; i != e; ++i)
223 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
224 return false;
225 return true;
226}
227
228bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
229 // Look through a bit convert.
230 while (N->getOpcode() == ISD::BITCAST)
231 N = N->getOperand(0).getNode();
232
233 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
234 APInt SplatVal;
235 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
236 }
237
238 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
239
240 bool IsAllUndef = true;
241 for (const SDValue &Op : N->op_values()) {
242 if (Op.isUndef())
243 continue;
244 IsAllUndef = false;
245 // Do not accept build_vectors that aren't all constants or which have non-0
246 // elements. We have to be a bit careful here, as the type of the constant
247 // may not be the same as the type of the vector elements due to type
248 // legalization (the elements are promoted to a legal type for the target
249 // and a vector of a type may be legal when the base element type is not).
250 // We only want to check enough bits to cover the vector elements, because
251 // we care if the resultant vector is all zeros, not whether the individual
252 // constants are.
253 if (auto OptAPInt = Op->bitcastToAPInt()) {
254 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
255 if (OptAPInt->countr_zero() < EltSize)
256 return false;
257 } else
258 return false;
259 }
260
261 // Do not accept an all-undef vector.
262 if (IsAllUndef)
263 return false;
264 return true;
265}
266
268 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
269}
270
272 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
273}
274
276 if (N->getOpcode() != ISD::BUILD_VECTOR)
277 return false;
278
279 for (const SDValue &Op : N->op_values()) {
280 if (Op.isUndef())
281 continue;
283 return false;
284 }
285 return true;
286}
287
289 if (N->getOpcode() != ISD::BUILD_VECTOR)
290 return false;
291
292 for (const SDValue &Op : N->op_values()) {
293 if (Op.isUndef())
294 continue;
296 return false;
297 }
298 return true;
299}
300
301bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
302 bool Signed) {
303 assert(N->getValueType(0).isVector() && "Expected a vector!");
304
305 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
306 if (EltSize <= NewEltSize)
307 return false;
308
309 if (N->getOpcode() == ISD::ZERO_EXTEND) {
310 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
311 NewEltSize) &&
312 !Signed;
313 }
314 if (N->getOpcode() == ISD::SIGN_EXTEND) {
315 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
316 NewEltSize) &&
317 Signed;
318 }
319 if (N->getOpcode() != ISD::BUILD_VECTOR)
320 return false;
321
322 for (const SDValue &Op : N->op_values()) {
323 if (Op.isUndef())
324 continue;
326 return false;
327
328 APInt C = Op->getAsAPIntVal().trunc(EltSize);
329 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
330 return false;
331 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
332 return false;
333 }
334
335 return true;
336}
337
339 // Return false if the node has no operands.
340 // This is "logically inconsistent" with the definition of "all" but
341 // is probably the desired behavior.
342 if (N->getNumOperands() == 0)
343 return false;
344 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
345}
346
348 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
349}
350
351template <typename ConstNodeType>
353 std::function<bool(ConstNodeType *)> Match,
354 bool AllowUndefs, bool AllowTruncation) {
355 // FIXME: Add support for scalar UNDEF cases?
356 if (auto *C = dyn_cast<ConstNodeType>(Op))
357 return Match(C);
358
359 // FIXME: Add support for vector UNDEF cases?
360 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
361 ISD::SPLAT_VECTOR != Op.getOpcode())
362 return false;
363
364 EVT SVT = Op.getValueType().getScalarType();
365 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
366 if (AllowUndefs && Op.getOperand(i).isUndef()) {
367 if (!Match(nullptr))
368 return false;
369 continue;
370 }
371
372 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
373 if (!Cst || (!AllowTruncation && Cst->getValueType(0) != SVT) ||
374 !Match(Cst))
375 return false;
376 }
377 return true;
378}
379// Build used template types.
381 SDValue, std::function<bool(ConstantSDNode *)>, bool, bool);
383 SDValue, std::function<bool(ConstantFPSDNode *)>, bool, bool);
384
386 SDValue LHS, SDValue RHS,
387 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
388 bool AllowUndefs, bool AllowTypeMismatch) {
389 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
390 return false;
391
392 // TODO: Add support for scalar UNDEF cases?
393 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
394 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
395 return Match(LHSCst, RHSCst);
396
397 // TODO: Add support for vector UNDEF cases?
398 if (LHS.getOpcode() != RHS.getOpcode() ||
399 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
400 LHS.getOpcode() != ISD::SPLAT_VECTOR))
401 return false;
402
403 EVT SVT = LHS.getValueType().getScalarType();
404 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
405 SDValue LHSOp = LHS.getOperand(i);
406 SDValue RHSOp = RHS.getOperand(i);
407 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
408 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
409 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
410 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
411 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
412 return false;
413 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
414 LHSOp.getValueType() != RHSOp.getValueType()))
415 return false;
416 if (!Match(LHSCst, RHSCst))
417 return false;
418 }
419 return true;
420}
421
423 switch (MinMaxOpc) {
424 default:
425 llvm_unreachable("unrecognized opcode");
426 case ISD::UMIN:
427 return ISD::UMAX;
428 case ISD::UMAX:
429 return ISD::UMIN;
430 case ISD::SMIN:
431 return ISD::SMAX;
432 case ISD::SMAX:
433 return ISD::SMIN;
434 }
435}
436
438 switch (MinMaxOpc) {
439 default:
440 llvm_unreachable("unrecognized min/max opcode");
441 case ISD::SMIN:
442 return ISD::UMIN;
443 case ISD::SMAX:
444 return ISD::UMAX;
445 case ISD::UMIN:
446 return ISD::SMIN;
447 case ISD::UMAX:
448 return ISD::SMAX;
449 }
450}
451
453 switch (VecReduceOpcode) {
454 default:
455 llvm_unreachable("Expected VECREDUCE opcode");
458 case ISD::VP_REDUCE_FADD:
459 case ISD::VP_REDUCE_SEQ_FADD:
460 return ISD::FADD;
463 case ISD::VP_REDUCE_FMUL:
464 case ISD::VP_REDUCE_SEQ_FMUL:
465 return ISD::FMUL;
467 case ISD::VP_REDUCE_ADD:
468 return ISD::ADD;
470 case ISD::VP_REDUCE_MUL:
471 return ISD::MUL;
473 case ISD::VP_REDUCE_AND:
474 return ISD::AND;
476 case ISD::VP_REDUCE_OR:
477 return ISD::OR;
479 case ISD::VP_REDUCE_XOR:
480 return ISD::XOR;
482 case ISD::VP_REDUCE_SMAX:
483 return ISD::SMAX;
485 case ISD::VP_REDUCE_SMIN:
486 return ISD::SMIN;
488 case ISD::VP_REDUCE_UMAX:
489 return ISD::UMAX;
491 case ISD::VP_REDUCE_UMIN:
492 return ISD::UMIN;
494 case ISD::VP_REDUCE_FMAX:
495 return ISD::FMAXNUM;
497 case ISD::VP_REDUCE_FMIN:
498 return ISD::FMINNUM;
500 case ISD::VP_REDUCE_FMAXIMUM:
501 return ISD::FMAXIMUM;
503 case ISD::VP_REDUCE_FMINIMUM:
504 return ISD::FMINIMUM;
505 }
506}
507
509 switch (MaskedOpc) {
510 case ISD::MASKED_UDIV:
511 return ISD::UDIV;
512 case ISD::MASKED_SDIV:
513 return ISD::SDIV;
514 case ISD::MASKED_UREM:
515 return ISD::UREM;
516 case ISD::MASKED_SREM:
517 return ISD::SREM;
518 default:
519 llvm_unreachable("Expected masked binop opcode");
520 }
521}
522
523bool ISD::isVPOpcode(unsigned Opcode) {
524 switch (Opcode) {
525 default:
526 return false;
527#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
528 case ISD::VPSD: \
529 return true;
530#include "llvm/IR/VPIntrinsics.def"
531 }
532}
533
534bool ISD::isVPBinaryOp(unsigned Opcode) {
535 switch (Opcode) {
536 default:
537 break;
538#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
539#define VP_PROPERTY_BINARYOP return true;
540#define END_REGISTER_VP_SDNODE(VPSD) break;
541#include "llvm/IR/VPIntrinsics.def"
542 }
543 return false;
544}
545
546bool ISD::isVPReduction(unsigned Opcode) {
547 switch (Opcode) {
548 default:
549 return false;
550 case ISD::VP_REDUCE_ADD:
551 case ISD::VP_REDUCE_MUL:
552 case ISD::VP_REDUCE_AND:
553 case ISD::VP_REDUCE_OR:
554 case ISD::VP_REDUCE_XOR:
555 case ISD::VP_REDUCE_SMAX:
556 case ISD::VP_REDUCE_SMIN:
557 case ISD::VP_REDUCE_UMAX:
558 case ISD::VP_REDUCE_UMIN:
559 case ISD::VP_REDUCE_FMAX:
560 case ISD::VP_REDUCE_FMIN:
561 case ISD::VP_REDUCE_FMAXIMUM:
562 case ISD::VP_REDUCE_FMINIMUM:
563 case ISD::VP_REDUCE_FADD:
564 case ISD::VP_REDUCE_FMUL:
565 case ISD::VP_REDUCE_SEQ_FADD:
566 case ISD::VP_REDUCE_SEQ_FMUL:
567 return true;
568 }
569}
570
571/// The operand position of the vector mask.
572std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
573 switch (Opcode) {
574 default:
575 return std::nullopt;
576#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
577 case ISD::VPSD: \
578 return MASKPOS;
579#include "llvm/IR/VPIntrinsics.def"
580 }
581}
582
583/// The operand position of the explicit vector length parameter.
584std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
585 switch (Opcode) {
586 default:
587 return std::nullopt;
588#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
589 case ISD::VPSD: \
590 return EVLPOS;
591#include "llvm/IR/VPIntrinsics.def"
592 }
593}
594
595std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
596 bool hasFPExcept) {
597 // FIXME: Return strict opcodes in case of fp exceptions.
598 switch (VPOpcode) {
599 default:
600 return std::nullopt;
601#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
602#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
603#define END_REGISTER_VP_SDNODE(VPOPC) break;
604#include "llvm/IR/VPIntrinsics.def"
605 }
606 return std::nullopt;
607}
608
609std::optional<unsigned> ISD::getVPForBaseOpcode(unsigned Opcode) {
610 switch (Opcode) {
611 default:
612 return std::nullopt;
613#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
614#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
615#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
616#include "llvm/IR/VPIntrinsics.def"
617 }
618}
619
621 switch (ExtType) {
622 case ISD::EXTLOAD:
623 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
624 case ISD::SEXTLOAD:
625 return ISD::SIGN_EXTEND;
626 case ISD::ZEXTLOAD:
627 return ISD::ZERO_EXTEND;
628 default:
629 break;
630 }
631
632 llvm_unreachable("Invalid LoadExtType");
633}
634
636 // To perform this operation, we just need to swap the L and G bits of the
637 // operation.
638 unsigned OldL = (Operation >> 2) & 1;
639 unsigned OldG = (Operation >> 1) & 1;
640 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
641 (OldL << 1) | // New G bit
642 (OldG << 2)); // New L bit.
643}
644
646 unsigned Operation = Op;
647 if (isIntegerLike)
648 Operation ^= 7; // Flip L, G, E bits, but not U.
649 else
650 Operation ^= 15; // Flip all of the condition bits.
651
653 Operation &= ~8; // Don't let N and U bits get set.
654
655 return ISD::CondCode(Operation);
656}
657
661
663 bool isIntegerLike) {
664 return getSetCCInverseImpl(Op, isIntegerLike);
665}
666
667/// For an integer comparison, return 1 if the comparison is a signed operation
668/// and 2 if the result is an unsigned comparison. Return zero if the operation
669/// does not depend on the sign of the input (setne and seteq).
670static int isSignedOp(ISD::CondCode Opcode) {
671 switch (Opcode) {
672 default: llvm_unreachable("Illegal integer setcc operation!");
673 case ISD::SETEQ:
674 case ISD::SETNE: return 0;
675 case ISD::SETLT:
676 case ISD::SETLE:
677 case ISD::SETGT:
678 case ISD::SETGE: return 1;
679 case ISD::SETULT:
680 case ISD::SETULE:
681 case ISD::SETUGT:
682 case ISD::SETUGE: return 2;
683 }
684}
685
687 EVT Type) {
688 bool IsInteger = Type.isInteger();
689 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
690 // Cannot fold a signed integer setcc with an unsigned integer setcc.
691 return ISD::SETCC_INVALID;
692
693 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
694
695 // If the N and U bits get set, then the resultant comparison DOES suddenly
696 // care about orderedness, and it is true when ordered.
697 if (Op > ISD::SETTRUE2)
698 Op &= ~16; // Clear the U bit if the N bit is set.
699
700 // Canonicalize illegal integer setcc's.
701 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
702 Op = ISD::SETNE;
703
704 return ISD::CondCode(Op);
705}
706
708 EVT Type) {
709 bool IsInteger = Type.isInteger();
710 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
711 // Cannot fold a signed setcc with an unsigned setcc.
712 return ISD::SETCC_INVALID;
713
714 // Combine all of the condition bits.
715 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
716
717 // Canonicalize illegal integer setcc's.
718 if (IsInteger) {
719 switch (Result) {
720 default: break;
721 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
722 case ISD::SETOEQ: // SETEQ & SETU[LG]E
723 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
724 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
725 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
726 }
727 }
728
729 return Result;
730}
731
732//===----------------------------------------------------------------------===//
733// SDNode Profile Support
734//===----------------------------------------------------------------------===//
735
736/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
737static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
738 ID.AddInteger(OpC);
739}
740
741/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
742/// solely with their pointer.
744 ID.AddPointer(VTList.VTs);
745}
746
747/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
750 for (const auto &Op : Ops) {
751 ID.AddPointer(Op.getNode());
752 ID.AddInteger(Op.getResNo());
753 }
754}
755
756/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
759 for (const auto &Op : Ops) {
760 ID.AddPointer(Op.getNode());
761 ID.AddInteger(Op.getResNo());
762 }
763}
764
765static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
766 SDVTList VTList, ArrayRef<SDValue> OpList) {
767 AddNodeIDOpcode(ID, OpC);
768 AddNodeIDValueTypes(ID, VTList);
769 AddNodeIDOperands(ID, OpList);
770}
771
772/// If this is an SDNode with special info, add this info to the NodeID data.
774 switch (N->getOpcode()) {
777 case ISD::MCSymbol:
778 llvm_unreachable("Should only be used on nodes with operands");
779 default: break; // Normal nodes don't need extra info.
781 case ISD::Constant: {
783 ID.AddPointer(C->getConstantIntValue());
784 ID.AddBoolean(C->isOpaque());
785 break;
786 }
788 case ISD::ConstantFP:
789 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
790 break;
796 ID.AddPointer(GA->getGlobal());
797 ID.AddInteger(GA->getOffset());
798 ID.AddInteger(GA->getTargetFlags());
799 break;
800 }
801 case ISD::BasicBlock:
803 break;
804 case ISD::Register:
805 ID.AddInteger(cast<RegisterSDNode>(N)->getReg().id());
806 break;
808 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
809 break;
810 case ISD::SRCVALUE:
811 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
812 break;
813 case ISD::FrameIndex:
815 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
816 break;
818 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
819 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
820 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
821 break;
822 case ISD::JumpTable:
824 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
825 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
826 break;
830 ID.AddInteger(CP->getAlign().value());
831 ID.AddInteger(CP->getOffset());
834 else
835 ID.AddPointer(CP->getConstVal());
836 ID.AddInteger(CP->getTargetFlags());
837 break;
838 }
839 case ISD::TargetIndex: {
841 ID.AddInteger(TI->getIndex());
842 ID.AddInteger(TI->getOffset());
843 ID.AddInteger(TI->getTargetFlags());
844 break;
845 }
846 case ISD::LOAD: {
847 const LoadSDNode *LD = cast<LoadSDNode>(N);
848 ID.AddInteger(LD->getMemoryVT().getRawBits());
849 ID.AddInteger(LD->getRawSubclassData());
850 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
851 ID.AddInteger(LD->getMemOperand()->getFlags());
852 break;
853 }
854 case ISD::STORE: {
855 const StoreSDNode *ST = cast<StoreSDNode>(N);
856 ID.AddInteger(ST->getMemoryVT().getRawBits());
857 ID.AddInteger(ST->getRawSubclassData());
858 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
859 ID.AddInteger(ST->getMemOperand()->getFlags());
860 break;
861 }
862 case ISD::VP_LOAD: {
863 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
864 ID.AddInteger(ELD->getMemoryVT().getRawBits());
865 ID.AddInteger(ELD->getRawSubclassData());
866 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
867 ID.AddInteger(ELD->getMemOperand()->getFlags());
868 break;
869 }
870 case ISD::VP_LOAD_FF: {
871 const auto *LD = cast<VPLoadFFSDNode>(N);
872 ID.AddInteger(LD->getMemoryVT().getRawBits());
873 ID.AddInteger(LD->getRawSubclassData());
874 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
875 ID.AddInteger(LD->getMemOperand()->getFlags());
876 break;
877 }
878 case ISD::VP_STORE: {
879 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
880 ID.AddInteger(EST->getMemoryVT().getRawBits());
881 ID.AddInteger(EST->getRawSubclassData());
882 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
883 ID.AddInteger(EST->getMemOperand()->getFlags());
884 break;
885 }
886 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
888 ID.AddInteger(SLD->getMemoryVT().getRawBits());
889 ID.AddInteger(SLD->getRawSubclassData());
890 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
891 break;
892 }
893 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
895 ID.AddInteger(SST->getMemoryVT().getRawBits());
896 ID.AddInteger(SST->getRawSubclassData());
897 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
898 break;
899 }
900 case ISD::VP_GATHER: {
902 ID.AddInteger(EG->getMemoryVT().getRawBits());
903 ID.AddInteger(EG->getRawSubclassData());
904 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
905 ID.AddInteger(EG->getMemOperand()->getFlags());
906 break;
907 }
908 case ISD::VP_SCATTER: {
910 ID.AddInteger(ES->getMemoryVT().getRawBits());
911 ID.AddInteger(ES->getRawSubclassData());
912 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
913 ID.AddInteger(ES->getMemOperand()->getFlags());
914 break;
915 }
916 case ISD::MLOAD: {
918 ID.AddInteger(MLD->getMemoryVT().getRawBits());
919 ID.AddInteger(MLD->getRawSubclassData());
920 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
921 ID.AddInteger(MLD->getMemOperand()->getFlags());
922 break;
923 }
924 case ISD::MSTORE: {
926 ID.AddInteger(MST->getMemoryVT().getRawBits());
927 ID.AddInteger(MST->getRawSubclassData());
928 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
929 ID.AddInteger(MST->getMemOperand()->getFlags());
930 break;
931 }
932 case ISD::MGATHER: {
934 ID.AddInteger(MG->getMemoryVT().getRawBits());
935 ID.AddInteger(MG->getRawSubclassData());
936 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
937 ID.AddInteger(MG->getMemOperand()->getFlags());
938 break;
939 }
940 case ISD::MSCATTER: {
942 ID.AddInteger(MS->getMemoryVT().getRawBits());
943 ID.AddInteger(MS->getRawSubclassData());
944 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
945 ID.AddInteger(MS->getMemOperand()->getFlags());
946 break;
947 }
950 case ISD::ATOMIC_SWAP:
962 case ISD::ATOMIC_LOAD:
963 case ISD::ATOMIC_STORE: {
964 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
965 ID.AddInteger(AT->getMemoryVT().getRawBits());
966 ID.AddInteger(AT->getRawSubclassData());
967 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
968 ID.AddInteger(AT->getMemOperand()->getFlags());
969 break;
970 }
971 case ISD::VECTOR_SHUFFLE: {
972 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
973 for (int M : Mask)
974 ID.AddInteger(M);
975 break;
976 }
977 case ISD::ADDRSPACECAST: {
979 ID.AddInteger(ASC->getSrcAddressSpace());
980 ID.AddInteger(ASC->getDestAddressSpace());
981 break;
982 }
984 case ISD::BlockAddress: {
986 ID.AddPointer(BA->getBlockAddress());
987 ID.AddInteger(BA->getOffset());
988 ID.AddInteger(BA->getTargetFlags());
989 break;
990 }
991 case ISD::AssertAlign:
992 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
993 break;
994 case ISD::PREFETCH:
997 // Handled by MemIntrinsicSDNode check after the switch.
998 break;
1000 ID.AddPointer(cast<MDNodeSDNode>(N)->getMD());
1001 break;
1002 } // end switch (N->getOpcode())
1003
1004 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
1005 // to check.
1006 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
1007 ID.AddInteger(MN->getRawSubclassData());
1008 ID.AddInteger(MN->getMemoryVT().getRawBits());
1009 for (const MachineMemOperand *MMO : MN->memoperands()) {
1010 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
1011 ID.AddInteger(MMO->getFlags());
1012 }
1013 }
1014}
1015
1016/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
1017/// data.
1019 AddNodeIDOpcode(ID, N->getOpcode());
1020 // Add the return value info.
1021 AddNodeIDValueTypes(ID, N->getVTList());
1022 // Add the operand info.
1023 AddNodeIDOperands(ID, N->ops());
1024
1025 // Handle SDNode leafs with special info.
1027}
1028
1029//===----------------------------------------------------------------------===//
1030// SelectionDAG Class
1031//===----------------------------------------------------------------------===//
1032
1033/// doNotCSE - Return true if CSE should not be performed for this node.
1034static bool doNotCSE(SDNode *N) {
1035 if (N->getValueType(0) == MVT::Glue)
1036 return true; // Never CSE anything that produces a glue result.
1037
1038 switch (N->getOpcode()) {
1039 default: break;
1040 case ISD::HANDLENODE:
1041 case ISD::EH_LABEL:
1042 return true; // Never CSE these nodes.
1043 }
1044
1045 // Check that remaining values produced are not flags.
1046 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
1047 if (N->getValueType(i) == MVT::Glue)
1048 return true; // Never CSE anything that produces a glue result.
1049
1050 return false;
1051}
1052
1053/// Construct a DemandedElts mask which demands all elements of \p V.
1054/// If \p V is not a fixed-length vector, then this will return a single bit.
1056 EVT VT = V.getValueType();
1057 // Since the number of lanes in a scalable vector is unknown at compile time,
1058 // we track one bit which is implicitly broadcast to all lanes. This means
1059 // that all lanes in a scalable vector are considered demanded.
1061 : APInt(1, 1);
1062}
1063
1064/// RemoveDeadNodes - This method deletes all unreachable nodes in the
1065/// SelectionDAG.
1067 // Create a dummy node (which is not added to allnodes), that adds a reference
1068 // to the root node, preventing it from being deleted.
1069 HandleSDNode Dummy(getRoot());
1070
1071 SmallVector<SDNode*, 128> DeadNodes;
1072
1073 // Add all obviously-dead nodes to the DeadNodes worklist.
1074 for (SDNode &Node : allnodes())
1075 if (Node.use_empty())
1076 DeadNodes.push_back(&Node);
1077
1078 RemoveDeadNodes(DeadNodes);
1079
1080 // If the root changed (e.g. it was a dead load, update the root).
1081 setRoot(Dummy.getValue());
1082}
1083
1084/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1085/// given list, and any nodes that become unreachable as a result.
1087
1088 // Process the worklist, deleting the nodes and adding their uses to the
1089 // worklist.
1090 while (!DeadNodes.empty()) {
1091 SDNode *N = DeadNodes.pop_back_val();
1092 // Skip to next node if we've already managed to delete the node. This could
1093 // happen if replacing a node causes a node previously added to the node to
1094 // be deleted.
1095 if (N->getOpcode() == ISD::DELETED_NODE)
1096 continue;
1097
1098 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1099 DUL->NodeDeleted(N, nullptr);
1100
1101 // Take the node out of the appropriate CSE map.
1102 RemoveNodeFromCSEMaps(N);
1103
1104 // Next, brutally remove the operand list. This is safe to do, as there are
1105 // no cycles in the graph.
1106 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1107 SDUse &Use = *I++;
1108 SDNode *Operand = Use.getNode();
1109 Use.set(SDValue());
1110
1111 // Now that we removed this operand, see if there are no uses of it left.
1112 if (Operand->use_empty())
1113 DeadNodes.push_back(Operand);
1114 }
1115
1116 DeallocateNode(N);
1117 }
1118}
1119
1121 SmallVector<SDNode*, 16> DeadNodes(1, N);
1122
1123 // Create a dummy node that adds a reference to the root node, preventing
1124 // it from being deleted. (This matters if the root is an operand of the
1125 // dead node.)
1126 HandleSDNode Dummy(getRoot());
1127
1128 RemoveDeadNodes(DeadNodes);
1129}
1130
1132 // First take this out of the appropriate CSE map.
1133 RemoveNodeFromCSEMaps(N);
1134
1135 // Finally, remove uses due to operands of this node, remove from the
1136 // AllNodes list, and delete the node.
1137 DeleteNodeNotInCSEMaps(N);
1138}
1139
1140void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1141 assert(N->getIterator() != AllNodes.begin() &&
1142 "Cannot delete the entry node!");
1143 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1144
1145 // Drop all of the operands and decrement used node's use counts.
1146 N->DropOperands();
1147
1148 DeallocateNode(N);
1149}
1150
1151void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1152 assert(!(V->isVariadic() && isParameter));
1153 if (isParameter)
1154 ByvalParmDbgValues.push_back(V);
1155 else
1156 DbgValues.push_back(V);
1157 for (const SDNode *Node : V->getSDNodes())
1158 if (Node)
1159 DbgValMap[Node].push_back(V);
1160}
1161
1163 DbgValMapType::iterator I = DbgValMap.find(Node);
1164 if (I == DbgValMap.end())
1165 return;
1166 for (auto &Val: I->second)
1167 Val->setIsInvalidated();
1168 DbgValMap.erase(I);
1169}
1170
1171void SelectionDAG::DeallocateNode(SDNode *N) {
1172 // If we have operands, deallocate them.
1174
1175 NodeAllocator.Deallocate(AllNodes.remove(N));
1176
1177 // Set the opcode to DELETED_NODE to help catch bugs when node
1178 // memory is reallocated.
1179 // FIXME: There are places in SDag that have grown a dependency on the opcode
1180 // value in the released node.
1181 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1182 N->NodeType = ISD::DELETED_NODE;
1183
1184 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1185 // them and forget about that node.
1186 DbgInfo->erase(N);
1187
1188 // Invalidate extra info.
1189 SDEI.erase(N);
1190}
1191
1192#ifndef NDEBUG
1193/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1194void SelectionDAG::verifyNode(SDNode *N) const {
1195 switch (N->getOpcode()) {
1196 default:
1197 if (N->isTargetOpcode())
1199 break;
1200 case ISD::BUILD_PAIR: {
1201 EVT VT = N->getValueType(0);
1202 assert(N->getNumValues() == 1 && "Too many results!");
1203 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1204 "Wrong return type!");
1205 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1206 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1207 "Mismatched operand types!");
1208 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1209 "Wrong operand type!");
1210 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1211 "Wrong return type size");
1212 break;
1213 }
1214 case ISD::BUILD_VECTOR: {
1215 assert(N->getNumValues() == 1 && "Too many results!");
1216 assert(N->getValueType(0).isVector() && "Wrong return type!");
1217 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1218 "Wrong number of operands!");
1219 EVT EltVT = N->getValueType(0).getVectorElementType();
1220 for (const SDUse &Op : N->ops()) {
1221 assert((Op.getValueType() == EltVT ||
1222 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1223 EltVT.bitsLE(Op.getValueType()))) &&
1224 "Wrong operand type!");
1225 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1226 "Operands must all have the same type");
1227 }
1228 break;
1229 }
1230 case ISD::SADDO:
1231 case ISD::UADDO:
1232 case ISD::SSUBO:
1233 case ISD::USUBO:
1234 assert(N->getNumValues() == 2 && "Wrong number of results!");
1235 assert(N->getVTList().NumVTs == 2 && N->getNumOperands() == 2 &&
1236 "Invalid add/sub overflow op!");
1237 assert(N->getVTList().VTs[0].isInteger() &&
1238 N->getVTList().VTs[1].isInteger() &&
1239 N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1240 N->getOperand(0).getValueType() == N->getVTList().VTs[0] &&
1241 "Binary operator types must match!");
1242 break;
1243 }
1244}
1245#endif // NDEBUG
1246
1247/// Insert a newly allocated node into the DAG.
1248///
1249/// Handles insertion into the all nodes list and CSE map, as well as
1250/// verification and other common operations when a new node is allocated.
1251void SelectionDAG::InsertNode(SDNode *N) {
1252 AllNodes.push_back(N);
1253#ifndef NDEBUG
1254 N->PersistentId = NextPersistentId++;
1255 verifyNode(N);
1256#endif
1257 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1258 DUL->NodeInserted(N);
1259}
1260
1261/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1262/// correspond to it. This is useful when we're about to delete or repurpose
1263/// the node. We don't want future request for structurally identical nodes
1264/// to return N anymore.
1265bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1266 bool Erased = false;
1267 switch (N->getOpcode()) {
1268 case ISD::HANDLENODE: return false; // noop.
1269 case ISD::CONDCODE:
1270 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1271 "Cond code doesn't exist!");
1272 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1273 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1274 break;
1276 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1277 break;
1279 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1280 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1281 ESN->getSymbol(), ESN->getTargetFlags()));
1282 break;
1283 }
1284 case ISD::MCSymbol: {
1285 auto *MCSN = cast<MCSymbolSDNode>(N);
1286 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1287 break;
1288 }
1289 case ISD::VALUETYPE: {
1290 EVT VT = cast<VTSDNode>(N)->getVT();
1291 if (VT.isExtended()) {
1292 Erased = ExtendedValueTypeNodes.erase(VT);
1293 } else {
1294 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1295 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1296 }
1297 break;
1298 }
1299 default:
1300 // Remove it from the CSE Map.
1301 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1302 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1303 Erased = CSEMap.RemoveNode(N);
1304 break;
1305 }
1306#ifndef NDEBUG
1307 // Verify that the node was actually in one of the CSE maps, unless it has a
1308 // glue result (which cannot be CSE'd) or is one of the special cases that are
1309 // not subject to CSE.
1310 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1311 !N->isMachineOpcode() && !doNotCSE(N)) {
1312 N->dump(this);
1313 dbgs() << "\n";
1314 llvm_unreachable("Node is not in map!");
1315 }
1316#endif
1317 return Erased;
1318}
1319
1320/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1321/// maps and modified in place. Add it back to the CSE maps, unless an identical
1322/// node already exists, in which case transfer all its users to the existing
1323/// node. This transfer can potentially trigger recursive merging.
1324void
1325SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1326 // For node types that aren't CSE'd, just act as if no identical node
1327 // already exists.
1328 if (!doNotCSE(N)) {
1329 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1330 if (Existing != N) {
1331 // If there was already an existing matching node, use ReplaceAllUsesWith
1332 // to replace the dead one with the existing one. This can cause
1333 // recursive merging of other unrelated nodes down the line.
1334 Existing->intersectFlagsWith(N->getFlags());
1335 if (auto *MemNode = dyn_cast<MemSDNode>(Existing))
1336 MemNode->refineRanges(cast<MemSDNode>(N)->memoperands());
1337 ReplaceAllUsesWith(N, Existing);
1338
1339 // N is now dead. Inform the listeners and delete it.
1340 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1341 DUL->NodeDeleted(N, Existing);
1342 DeleteNodeNotInCSEMaps(N);
1343 return;
1344 }
1345 }
1346
1347 // If the node doesn't already exist, we updated it. Inform listeners.
1348 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1349 DUL->NodeUpdated(N);
1350}
1351
1352/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1353/// were replaced with those specified. If this node is never memoized,
1354/// return null, otherwise return a pointer to the slot it would take. If a
1355/// node already exists with these operands, the slot will be non-null.
1356SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1357 void *&InsertPos) {
1358 if (doNotCSE(N))
1359 return nullptr;
1360
1361 SDValue Ops[] = { Op };
1362 FoldingSetNodeID ID;
1363 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1365 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1366 if (Node)
1367 Node->intersectFlagsWith(N->getFlags());
1368 return Node;
1369}
1370
1371/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1372/// were replaced with those specified. If this node is never memoized,
1373/// return null, otherwise return a pointer to the slot it would take. If a
1374/// node already exists with these operands, the slot will be non-null.
1375SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1376 SDValue Op1, SDValue Op2,
1377 void *&InsertPos) {
1378 if (doNotCSE(N))
1379 return nullptr;
1380
1381 SDValue Ops[] = { Op1, Op2 };
1382 FoldingSetNodeID ID;
1383 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1385 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1386 if (Node)
1387 Node->intersectFlagsWith(N->getFlags());
1388 return Node;
1389}
1390
1391/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1392/// were replaced with those specified. If this node is never memoized,
1393/// return null, otherwise return a pointer to the slot it would take. If a
1394/// node already exists with these operands, the slot will be non-null.
1395SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1396 void *&InsertPos) {
1397 if (doNotCSE(N))
1398 return nullptr;
1399
1400 FoldingSetNodeID ID;
1401 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1403 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1404 if (Node)
1405 Node->intersectFlagsWith(N->getFlags());
1406 return Node;
1407}
1408
1410 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1411 : VT.getTypeForEVT(*getContext());
1412
1413 return getDataLayout().getABITypeAlign(Ty);
1414}
1415
1416// EntryNode could meaningfully have debug info if we can find it...
1418 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1419 getVTList(MVT::Other, MVT::Glue)),
1420 Root(getEntryNode()) {
1421 InsertNode(&EntryNode);
1422 DbgInfo = new SDDbgInfo();
1423}
1424
1426 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1427 const TargetLibraryInfo *LibraryInfo,
1428 const LibcallLoweringInfo *LibcallsInfo,
1429 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1431 FunctionVarLocs const *VarLocs) {
1432 MF = &NewMF;
1433 SDAGISelPass = PassPtr;
1434 ORE = &NewORE;
1437 LibInfo = LibraryInfo;
1438 Libcalls = LibcallsInfo;
1439 Context = &MF->getFunction().getContext();
1440 UA = NewUA;
1441 PSI = PSIin;
1442 BFI = BFIin;
1443 MMI = &MMIin;
1444 FnVarLocs = VarLocs;
1445}
1446
1448 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1449 allnodes_clear();
1450 OperandRecycler.clear(OperandAllocator);
1451 delete DbgInfo;
1452}
1453
1455 return llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1456}
1457
1458void SelectionDAG::allnodes_clear() {
1459 assert(&*AllNodes.begin() == &EntryNode);
1460 AllNodes.remove(AllNodes.begin());
1461 while (!AllNodes.empty())
1462 DeallocateNode(&AllNodes.front());
1463#ifndef NDEBUG
1464 NextPersistentId = 0;
1465#endif
1466}
1467
1468SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1469 void *&InsertPos) {
1470 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1471 if (N) {
1472 switch (N->getOpcode()) {
1473 default: break;
1474 case ISD::Constant:
1475 case ISD::ConstantFP:
1476 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1477 "debug location. Use another overload.");
1478 }
1479 }
1480 return N;
1481}
1482
1483SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1484 const SDLoc &DL, void *&InsertPos) {
1485 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1486 if (N) {
1487 switch (N->getOpcode()) {
1488 case ISD::Constant:
1489 case ISD::ConstantFP:
1490 // Erase debug location from the node if the node is used at several
1491 // different places. Do not propagate one location to all uses as it
1492 // will cause a worse single stepping debugging experience.
1493 if (N->getDebugLoc() != DL.getDebugLoc())
1494 N->setDebugLoc(DebugLoc());
1495 break;
1496 default:
1497 // When the node's point of use is located earlier in the instruction
1498 // sequence than its prior point of use, update its debug info to the
1499 // earlier location.
1500 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1501 N->setDebugLoc(DL.getDebugLoc());
1502 break;
1503 }
1504 }
1505 return N;
1506}
1507
1509 allnodes_clear();
1510 OperandRecycler.clear(OperandAllocator);
1511 OperandAllocator.Reset();
1512 CSEMap.clear();
1513
1514 ExtendedValueTypeNodes.clear();
1515 ExternalSymbols.clear();
1516 TargetExternalSymbols.clear();
1517 MCSymbols.clear();
1518 SDEI.clear();
1519 llvm::fill(CondCodeNodes, nullptr);
1520 llvm::fill(ValueTypeNodes, nullptr);
1521
1522 EntryNode.UseList = nullptr;
1523 InsertNode(&EntryNode);
1524 Root = getEntryNode();
1525 DbgInfo->clear();
1526}
1527
1529 return VT.bitsGT(Op.getValueType())
1530 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1531 : getNode(ISD::FP_ROUND, DL, VT, Op,
1532 getIntPtrConstant(0, DL, /*isTarget=*/true));
1533}
1534
1535std::pair<SDValue, SDValue>
1537 const SDLoc &DL, EVT VT) {
1538 assert(!VT.bitsEq(Op.getValueType()) &&
1539 "Strict no-op FP extend/round not allowed.");
1540 SDValue Res =
1541 VT.bitsGT(Op.getValueType())
1542 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1543 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1544 {Chain, Op, getIntPtrConstant(0, DL, /*isTarget=*/true)});
1545
1546 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1547}
1548
1550 return VT.bitsGT(Op.getValueType()) ?
1551 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1552 getNode(ISD::TRUNCATE, DL, VT, Op);
1553}
1554
1556 return VT.bitsGT(Op.getValueType()) ?
1557 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1558 getNode(ISD::TRUNCATE, DL, VT, Op);
1559}
1560
1562 return VT.bitsGT(Op.getValueType()) ?
1563 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1564 getNode(ISD::TRUNCATE, DL, VT, Op);
1565}
1566
1568 EVT VT) {
1569 assert(!VT.isVector());
1570 auto Type = Op.getValueType();
1571 SDValue DestOp;
1572 if (Type == VT)
1573 return Op;
1574 auto Size = Op.getValueSizeInBits();
1575 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1576 if (DestOp.getValueType() == VT)
1577 return DestOp;
1578
1579 return getAnyExtOrTrunc(DestOp, DL, VT);
1580}
1581
1583 EVT VT) {
1584 assert(!VT.isVector());
1585 auto Type = Op.getValueType();
1586 SDValue DestOp;
1587 if (Type == VT)
1588 return Op;
1589 auto Size = Op.getValueSizeInBits();
1590 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1591 if (DestOp.getValueType() == VT)
1592 return DestOp;
1593
1594 return getSExtOrTrunc(DestOp, DL, VT);
1595}
1596
1598 EVT VT) {
1599 assert(!VT.isVector());
1600 auto Type = Op.getValueType();
1601 SDValue DestOp;
1602 if (Type == VT)
1603 return Op;
1604 auto Size = Op.getValueSizeInBits();
1605 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1606 if (DestOp.getValueType() == VT)
1607 return DestOp;
1608
1609 return getZExtOrTrunc(DestOp, DL, VT);
1610}
1611
1613 EVT OpVT) {
1614 if (VT.bitsLE(Op.getValueType()))
1615 return getNode(ISD::TRUNCATE, SL, VT, Op);
1616
1617 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1618 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1619}
1620
1622 EVT OpVT = Op.getValueType();
1623 assert(VT.isInteger() && OpVT.isInteger() &&
1624 "Cannot getZeroExtendInReg FP types");
1625 assert(VT.isVector() == OpVT.isVector() &&
1626 "getZeroExtendInReg type should be vector iff the operand "
1627 "type is vector!");
1628 assert((!VT.isVector() ||
1630 "Vector element counts must match in getZeroExtendInReg");
1631 assert(VT.getScalarType().bitsLE(OpVT.getScalarType()) && "Not extending!");
1632 if (OpVT == VT)
1633 return Op;
1634 // TODO: Use computeKnownBits instead of AssertZext.
1635 if (Op.getOpcode() == ISD::AssertZext && cast<VTSDNode>(Op.getOperand(1))
1636 ->getVT()
1637 .getScalarType()
1638 .bitsLE(VT.getScalarType()))
1639 return Op;
1641 VT.getScalarSizeInBits());
1642 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1643}
1644
1646 SDValue EVL, const SDLoc &DL,
1647 EVT VT) {
1648 EVT OpVT = Op.getValueType();
1649 assert(VT.isInteger() && OpVT.isInteger() &&
1650 "Cannot getVPZeroExtendInReg FP types");
1651 assert(VT.isVector() && OpVT.isVector() &&
1652 "getVPZeroExtendInReg type and operand type should be vector!");
1654 "Vector element counts must match in getZeroExtendInReg");
1655 assert(VT.getScalarType().bitsLE(OpVT.getScalarType()) && "Not extending!");
1656 if (OpVT == VT)
1657 return Op;
1659 VT.getScalarSizeInBits());
1660 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1661 EVL);
1662}
1663
1665 // Only unsigned pointer semantics are supported right now. In the future this
1666 // might delegate to TLI to check pointer signedness.
1667 return getZExtOrTrunc(Op, DL, VT);
1668}
1669
1671 // Only unsigned pointer semantics are supported right now. In the future this
1672 // might delegate to TLI to check pointer signedness.
1673 return getZeroExtendInReg(Op, DL, VT);
1674}
1675
1677 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1678}
1679
1680/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1682 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1683}
1684
1686 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1687 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1688}
1689
1691 SDValue Mask, SDValue EVL, EVT VT) {
1692 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1693 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1694}
1695
1697 SDValue Mask, SDValue EVL) {
1698 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1699}
1700
1702 SDValue Mask, SDValue EVL) {
1703 if (VT.bitsGT(Op.getValueType()))
1704 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1705 if (VT.bitsLT(Op.getValueType()))
1706 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1707 return Op;
1708}
1709
1711 EVT OpVT) {
1712 if (!V)
1713 return getConstant(0, DL, VT);
1714
1715 switch (TLI->getBooleanContents(OpVT)) {
1718 return getConstant(1, DL, VT);
1720 return getAllOnesConstant(DL, VT);
1721 }
1722 llvm_unreachable("Unexpected boolean content enum!");
1723}
1724
1726 bool isT, bool isO) {
1727 return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
1728 DL, VT, isT, isO);
1729}
1730
1732 bool isT, bool isO) {
1733 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1734}
1735
1737 EVT VT, bool isT, bool isO) {
1738 assert(VT.isInteger() && "Cannot create FP integer constant!");
1739
1740 EVT EltVT = VT.getScalarType();
1741 const ConstantInt *Elt = &Val;
1742
1743 // Vector splats are explicit within the DAG, with ConstantSDNode holding the
1744 // to-be-splatted scalar ConstantInt.
1745 if (isa<VectorType>(Elt->getType()))
1746 Elt = ConstantInt::get(*getContext(), Elt->getValue());
1747
1748 // In some cases the vector type is legal but the element type is illegal and
1749 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1750 // inserted value (the type does not need to match the vector element type).
1751 // Any extra bits introduced will be truncated away.
1752 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1754 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1755 APInt NewVal;
1756 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1757 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1758 else
1759 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1760 Elt = ConstantInt::get(*getContext(), NewVal);
1761 }
1762 // In other cases the element type is illegal and needs to be expanded, for
1763 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1764 // the value into n parts and use a vector type with n-times the elements.
1765 // Then bitcast to the type requested.
1766 // Legalizing constants too early makes the DAGCombiner's job harder so we
1767 // only legalize if the DAG tells us we must produce legal types.
1768 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1769 TLI->getTypeAction(*getContext(), EltVT) ==
1771 const APInt &NewVal = Elt->getValue();
1772 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1773 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1774
1775 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1776 if (VT.isScalableVector() ||
1777 TLI->isOperationLegal(ISD::SPLAT_VECTOR, VT)) {
1778 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1779 "Can only handle an even split!");
1780 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1781
1782 SmallVector<SDValue, 2> ScalarParts;
1783 for (unsigned i = 0; i != Parts; ++i)
1784 ScalarParts.push_back(getConstant(
1785 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1786 ViaEltVT, isT, isO));
1787
1788 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1789 }
1790
1791 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1792 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1793
1794 // Check the temporary vector is the correct size. If this fails then
1795 // getTypeToTransformTo() probably returned a type whose size (in bits)
1796 // isn't a power-of-2 factor of the requested type size.
1797 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1798
1799 SmallVector<SDValue, 2> EltParts;
1800 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1801 EltParts.push_back(getConstant(
1802 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1803 ViaEltVT, isT, isO));
1804
1805 // EltParts is currently in little endian order. If we actually want
1806 // big-endian order then reverse it now.
1807 if (getDataLayout().isBigEndian())
1808 std::reverse(EltParts.begin(), EltParts.end());
1809
1810 // The elements must be reversed when the element order is different
1811 // to the endianness of the elements (because the BITCAST is itself a
1812 // vector shuffle in this situation). However, we do not need any code to
1813 // perform this reversal because getConstant() is producing a vector
1814 // splat.
1815 // This situation occurs in MIPS MSA.
1816
1818 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1819 llvm::append_range(Ops, EltParts);
1820
1821 SDValue V =
1822 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1823 return V;
1824 }
1825
1826 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1827 "APInt size does not match type size!");
1828 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1829 SDVTList VTs = getVTList(EltVT);
1831 AddNodeIDNode(ID, Opc, VTs, {});
1832 ID.AddPointer(Elt);
1833 ID.AddBoolean(isO);
1834 void *IP = nullptr;
1835 SDNode *N = nullptr;
1836 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1837 if (!VT.isVector())
1838 return SDValue(N, 0);
1839
1840 if (!N) {
1841 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1842 if (!isT)
1843 N->setDebugLoc(DL.getDebugLoc());
1844 CSEMap.InsertNode(N, IP);
1845 InsertNode(N);
1846 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1847 }
1848
1849 SDValue Result(N, 0);
1850 if (VT.isVector())
1851 Result = getSplat(VT, DL, Result);
1852 return Result;
1853}
1854
1856 bool isT, bool isO) {
1857 unsigned Size = VT.getScalarSizeInBits();
1858 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1859}
1860
1862 bool IsOpaque) {
1864 IsTarget, IsOpaque);
1865}
1866
1868 bool isTarget) {
1869 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1870}
1871
1873 const SDLoc &DL) {
1874 assert(VT.isInteger() && "Shift amount is not an integer type!");
1875 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1876 return getConstant(Val, DL, ShiftVT);
1877}
1878
1880 const SDLoc &DL) {
1881 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1882 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1883}
1884
1886 bool isTarget) {
1887 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1888}
1889
1891 bool isTarget) {
1892 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1893}
1894
1896 EVT VT, bool isTarget) {
1897 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1898
1899 EVT EltVT = VT.getScalarType();
1900 const ConstantFP *Elt = &V;
1901
1902 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1903 // the to-be-splatted scalar ConstantFP.
1904 if (isa<VectorType>(Elt->getType()))
1905 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1906
1907 // Do the map lookup using the actual bit pattern for the floating point
1908 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1909 // we don't have issues with SNANs.
1910 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1911 SDVTList VTs = getVTList(EltVT);
1913 AddNodeIDNode(ID, Opc, VTs, {});
1914 ID.AddPointer(Elt);
1915 void *IP = nullptr;
1916 SDNode *N = nullptr;
1917 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1918 if (!VT.isVector())
1919 return SDValue(N, 0);
1920
1921 if (!N) {
1922 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1923 CSEMap.InsertNode(N, IP);
1924 InsertNode(N);
1925 }
1926
1927 SDValue Result(N, 0);
1928 if (VT.isVector())
1929 Result = getSplat(VT, DL, Result);
1930 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1931 return Result;
1932}
1933
1935 bool isTarget) {
1936 EVT EltVT = VT.getScalarType();
1937 if (EltVT == MVT::f32)
1938 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1939 if (EltVT == MVT::f64)
1940 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1941 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1942 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1943 bool Ignored;
1944 APFloat APF = APFloat(Val);
1946 &Ignored);
1947 return getConstantFP(APF, DL, VT, isTarget);
1948 }
1949 llvm_unreachable("Unsupported type in getConstantFP");
1950}
1951
1953 EVT VT, int64_t Offset, bool isTargetGA,
1954 unsigned TargetFlags) {
1955 assert((TargetFlags == 0 || isTargetGA) &&
1956 "Cannot set target flags on target-independent globals");
1957
1958 // Truncate (with sign-extension) the offset value to the pointer size.
1960 if (BitWidth < 64)
1962
1963 unsigned Opc;
1964 if (GV->isThreadLocal())
1966 else
1968
1969 SDVTList VTs = getVTList(VT);
1971 AddNodeIDNode(ID, Opc, VTs, {});
1972 ID.AddPointer(GV);
1973 ID.AddInteger(Offset);
1974 ID.AddInteger(TargetFlags);
1975 void *IP = nullptr;
1976 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1977 return SDValue(E, 0);
1978
1979 auto *N = newSDNode<GlobalAddressSDNode>(
1980 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1981 CSEMap.InsertNode(N, IP);
1982 InsertNode(N);
1983 return SDValue(N, 0);
1984}
1985
1987 SDVTList VTs = getVTList(MVT::Untyped);
1990 ID.AddPointer(GV);
1991 void *IP = nullptr;
1992 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP))
1993 return SDValue(E, 0);
1994
1995 auto *N = newSDNode<DeactivationSymbolSDNode>(GV, VTs);
1996 CSEMap.InsertNode(N, IP);
1997 InsertNode(N);
1998 return SDValue(N, 0);
1999}
2000
2001SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
2002 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
2003 SDVTList VTs = getVTList(VT);
2005 AddNodeIDNode(ID, Opc, VTs, {});
2006 ID.AddInteger(FI);
2007 void *IP = nullptr;
2008 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2009 return SDValue(E, 0);
2010
2011 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
2012 CSEMap.InsertNode(N, IP);
2013 InsertNode(N);
2014 return SDValue(N, 0);
2015}
2016
2017SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
2018 unsigned TargetFlags) {
2019 assert((TargetFlags == 0 || isTarget) &&
2020 "Cannot set target flags on target-independent jump tables");
2021 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
2022 SDVTList VTs = getVTList(VT);
2024 AddNodeIDNode(ID, Opc, VTs, {});
2025 ID.AddInteger(JTI);
2026 ID.AddInteger(TargetFlags);
2027 void *IP = nullptr;
2028 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2029 return SDValue(E, 0);
2030
2031 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
2032 CSEMap.InsertNode(N, IP);
2033 InsertNode(N);
2034 return SDValue(N, 0);
2035}
2036
2038 const SDLoc &DL) {
2040 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Other, Chain,
2041 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
2042}
2043
2045 MaybeAlign Alignment, int Offset,
2046 bool isTarget, unsigned TargetFlags) {
2047 assert((TargetFlags == 0 || isTarget) &&
2048 "Cannot set target flags on target-independent globals");
2049 if (!Alignment)
2050 Alignment = shouldOptForSize()
2051 ? getDataLayout().getABITypeAlign(C->getType())
2052 : getDataLayout().getPrefTypeAlign(C->getType());
2053 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2054 SDVTList VTs = getVTList(VT);
2056 AddNodeIDNode(ID, Opc, VTs, {});
2057 ID.AddInteger(Alignment->value());
2058 ID.AddInteger(Offset);
2059 ID.AddPointer(C);
2060 ID.AddInteger(TargetFlags);
2061 void *IP = nullptr;
2062 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2063 return SDValue(E, 0);
2064
2065 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2066 TargetFlags);
2067 CSEMap.InsertNode(N, IP);
2068 InsertNode(N);
2069 SDValue V = SDValue(N, 0);
2070 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
2071 return V;
2072}
2073
2075 MaybeAlign Alignment, int Offset,
2076 bool isTarget, unsigned TargetFlags) {
2077 assert((TargetFlags == 0 || isTarget) &&
2078 "Cannot set target flags on target-independent globals");
2079 if (!Alignment)
2080 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
2081 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2082 SDVTList VTs = getVTList(VT);
2084 AddNodeIDNode(ID, Opc, VTs, {});
2085 ID.AddInteger(Alignment->value());
2086 ID.AddInteger(Offset);
2087 C->addSelectionDAGCSEId(ID);
2088 ID.AddInteger(TargetFlags);
2089 void *IP = nullptr;
2090 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2091 return SDValue(E, 0);
2092
2093 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2094 TargetFlags);
2095 CSEMap.InsertNode(N, IP);
2096 InsertNode(N);
2097 return SDValue(N, 0);
2098}
2099
2102 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2103 ID.AddPointer(MBB);
2104 void *IP = nullptr;
2105 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2106 return SDValue(E, 0);
2107
2108 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2109 CSEMap.InsertNode(N, IP);
2110 InsertNode(N);
2111 return SDValue(N, 0);
2112}
2113
2115 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2116 ValueTypeNodes.size())
2117 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2118
2119 SDNode *&N = VT.isExtended() ?
2120 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2121
2122 if (N) return SDValue(N, 0);
2123 N = newSDNode<VTSDNode>(VT);
2124 InsertNode(N);
2125 return SDValue(N, 0);
2126}
2127
2129 SDNode *&N = ExternalSymbols[Sym];
2130 if (N) return SDValue(N, 0);
2131 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2132 InsertNode(N);
2133 return SDValue(N, 0);
2134}
2135
2136SDValue SelectionDAG::getExternalSymbol(RTLIB::LibcallImpl Libcall, EVT VT) {
2138 return getExternalSymbol(SymName.data(), VT);
2139}
2140
2142 SDNode *&N = MCSymbols[Sym];
2143 if (N)
2144 return SDValue(N, 0);
2145 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2146 InsertNode(N);
2147 return SDValue(N, 0);
2148}
2149
2151 unsigned TargetFlags) {
2152 SDNode *&N =
2153 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2154 if (N) return SDValue(N, 0);
2155 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2156 InsertNode(N);
2157 return SDValue(N, 0);
2158}
2159
2161 EVT VT, unsigned TargetFlags) {
2163 return getTargetExternalSymbol(SymName.data(), VT, TargetFlags);
2164}
2165
2167 if ((unsigned)Cond >= CondCodeNodes.size())
2168 CondCodeNodes.resize(Cond+1);
2169
2170 if (!CondCodeNodes[Cond]) {
2171 auto *N = newSDNode<CondCodeSDNode>(Cond);
2172 CondCodeNodes[Cond] = N;
2173 InsertNode(N);
2174 }
2175
2176 return SDValue(CondCodeNodes[Cond], 0);
2177}
2178
2180 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2181 "APInt size does not match type size!");
2182
2183 if (MulImm == 0)
2184 return getConstant(0, DL, VT);
2185
2186 const MachineFunction &MF = getMachineFunction();
2187 const Function &F = MF.getFunction();
2188 ConstantRange CR = getVScaleRange(&F, 64);
2189 if (const APInt *C = CR.getSingleElement())
2190 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2191
2192 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2193}
2194
2195/// \returns a value of type \p VT that represents the runtime value of \p
2196/// Quantity, i.e. scaled by vscale if it's scalable, or a fixed constant
2197/// otherwise. Quantity should be a FixedOrScalableQuantity, i.e. ElementCount
2198/// or TypeSize.
2199template <typename Ty>
2201 EVT VT, Ty Quantity) {
2202 if (Quantity.isScalable())
2203 return DAG.getVScale(
2204 DL, VT, APInt(VT.getSizeInBits(), Quantity.getKnownMinValue()));
2205
2206 return DAG.getConstant(Quantity.getKnownMinValue(), DL, VT);
2207}
2208
2210 ElementCount EC) {
2211 return getFixedOrScalableQuantity(*this, DL, VT, EC);
2212}
2213
2215 return getFixedOrScalableQuantity(*this, DL, VT, TS);
2216}
2217
2219 ElementCount EC) {
2220 EVT IdxVT = TLI->getVectorIdxTy(getDataLayout());
2221 EVT MaskVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), DataVT);
2222 return getNode(ISD::GET_ACTIVE_LANE_MASK, DL, MaskVT,
2223 getConstant(0, DL, IdxVT), getElementCount(DL, IdxVT, EC));
2224}
2225
2227 APInt One(ResVT.getScalarSizeInBits(), 1);
2228 return getStepVector(DL, ResVT, One);
2229}
2230
2232 const APInt &StepVal) {
2233 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2234 if (ResVT.isScalableVector())
2235 return getNode(
2236 ISD::STEP_VECTOR, DL, ResVT,
2237 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2238
2239 SmallVector<SDValue, 16> OpsStepConstants;
2240 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2241 OpsStepConstants.push_back(
2242 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2243 return getBuildVector(ResVT, DL, OpsStepConstants);
2244}
2245
2246/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2247/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2252
2254 SDValue N2, ArrayRef<int> Mask) {
2255 assert(VT.getVectorNumElements() == Mask.size() &&
2256 "Must have the same number of vector elements as mask elements!");
2257 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2258 "Invalid VECTOR_SHUFFLE");
2259
2260 // Canonicalize shuffle undef, undef -> undef
2261 if (N1.isUndef() && N2.isUndef())
2262 return getUNDEF(VT);
2263
2264 // Validate that all indices in Mask are within the range of the elements
2265 // input to the shuffle.
2266 int NElts = Mask.size();
2267 assert(llvm::all_of(Mask,
2268 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2269 "Index out of range");
2270
2271 // Copy the mask so we can do any needed cleanup.
2272 SmallVector<int, 8> MaskVec(Mask);
2273
2274 // Canonicalize shuffle v, v -> v, undef
2275 if (N1 == N2) {
2276 N2 = getUNDEF(VT);
2277 for (int i = 0; i != NElts; ++i)
2278 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2279 }
2280
2281 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2282 if (N1.isUndef())
2283 commuteShuffle(N1, N2, MaskVec);
2284
2285 if (TLI->hasVectorBlend()) {
2286 // If shuffling a splat, try to blend the splat instead. We do this here so
2287 // that even when this arises during lowering we don't have to re-handle it.
2288 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2289 BitVector UndefElements;
2290 SDValue Splat = BV->getSplatValue(&UndefElements);
2291 if (!Splat)
2292 return;
2293
2294 for (int i = 0; i < NElts; ++i) {
2295 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2296 continue;
2297
2298 // If this input comes from undef, mark it as such.
2299 if (UndefElements[MaskVec[i] - Offset]) {
2300 MaskVec[i] = -1;
2301 continue;
2302 }
2303
2304 // If we can blend a non-undef lane, use that instead.
2305 if (!UndefElements[i])
2306 MaskVec[i] = i + Offset;
2307 }
2308 };
2309 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2310 BlendSplat(N1BV, 0);
2311 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2312 BlendSplat(N2BV, NElts);
2313 }
2314
2315 // Canonicalize all index into lhs, -> shuffle lhs, undef
2316 // Canonicalize all index into rhs, -> shuffle rhs, undef
2317 bool AllLHS = true, AllRHS = true;
2318 bool N2Undef = N2.isUndef();
2319 for (int i = 0; i != NElts; ++i) {
2320 if (MaskVec[i] >= NElts) {
2321 if (N2Undef)
2322 MaskVec[i] = -1;
2323 else
2324 AllLHS = false;
2325 } else if (MaskVec[i] >= 0) {
2326 AllRHS = false;
2327 }
2328 }
2329 if (AllLHS && AllRHS)
2330 return getUNDEF(VT);
2331 if (AllLHS && !N2Undef)
2332 N2 = getUNDEF(VT);
2333 if (AllRHS) {
2334 N1 = getUNDEF(VT);
2335 commuteShuffle(N1, N2, MaskVec);
2336 }
2337 // Reset our undef status after accounting for the mask.
2338 N2Undef = N2.isUndef();
2339 // Re-check whether both sides ended up undef.
2340 if (N1.isUndef() && N2Undef)
2341 return getUNDEF(VT);
2342
2343 // If Identity shuffle return that node.
2344 bool Identity = true, AllSame = true;
2345 for (int i = 0; i != NElts; ++i) {
2346 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2347 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2348 }
2349 if (Identity && NElts)
2350 return N1;
2351
2352 // Shuffling a constant splat doesn't change the result.
2353 if (N2Undef) {
2354 SDValue V = N1;
2355
2356 // Look through any bitcasts. We check that these don't change the number
2357 // (and size) of elements and just changes their types.
2358 while (V.getOpcode() == ISD::BITCAST)
2359 V = V->getOperand(0);
2360
2361 // A splat should always show up as a build vector node.
2362 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2363 BitVector UndefElements;
2364 SDValue Splat = BV->getSplatValue(&UndefElements);
2365 // If this is a splat of an undef, shuffling it is also undef.
2366 if (Splat && Splat.isUndef())
2367 return getUNDEF(VT);
2368
2369 bool SameNumElts =
2370 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2371
2372 // We only have a splat which can skip shuffles if there is a splatted
2373 // value and no undef lanes rearranged by the shuffle.
2374 if (Splat && UndefElements.none()) {
2375 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2376 // number of elements match or the value splatted is a zero constant.
2377 if (SameNumElts || isNullConstant(Splat))
2378 return N1;
2379 }
2380
2381 // If the shuffle itself creates a splat, build the vector directly.
2382 if (AllSame && SameNumElts) {
2383 EVT BuildVT = BV->getValueType(0);
2384 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2385 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2386
2387 // We may have jumped through bitcasts, so the type of the
2388 // BUILD_VECTOR may not match the type of the shuffle.
2389 if (BuildVT != VT)
2390 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2391 return NewBV;
2392 }
2393 }
2394 }
2395
2396 SDVTList VTs = getVTList(VT);
2398 SDValue Ops[2] = { N1, N2 };
2400 for (int i = 0; i != NElts; ++i)
2401 ID.AddInteger(MaskVec[i]);
2402
2403 void* IP = nullptr;
2404 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2405 return SDValue(E, 0);
2406
2407 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2408 // SDNode doesn't have access to it. This memory will be "leaked" when
2409 // the node is deallocated, but recovered when the NodeAllocator is released.
2410 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2411 llvm::copy(MaskVec, MaskAlloc);
2412
2413 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2414 dl.getDebugLoc(), MaskAlloc);
2415 createOperands(N, Ops);
2416
2417 CSEMap.InsertNode(N, IP);
2418 InsertNode(N);
2419 SDValue V = SDValue(N, 0);
2420 NewSDValueDbgMsg(V, "Creating new node: ", this);
2421 return V;
2422}
2423
2425 EVT VT = SV.getValueType(0);
2426 SmallVector<int, 8> MaskVec(SV.getMask());
2428
2429 SDValue Op0 = SV.getOperand(0);
2430 SDValue Op1 = SV.getOperand(1);
2431 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2432}
2433
2435 SDVTList VTs = getVTList(VT);
2437 AddNodeIDNode(ID, ISD::Register, VTs, {});
2438 ID.AddInteger(Reg.id());
2439 void *IP = nullptr;
2440 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2441 return SDValue(E, 0);
2442
2443 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2444 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2445 CSEMap.InsertNode(N, IP);
2446 InsertNode(N);
2447 return SDValue(N, 0);
2448}
2449
2452 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2453 ID.AddPointer(RegMask);
2454 void *IP = nullptr;
2455 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2456 return SDValue(E, 0);
2457
2458 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2459 CSEMap.InsertNode(N, IP);
2460 InsertNode(N);
2461 return SDValue(N, 0);
2462}
2463
2465 MCSymbol *Label) {
2466 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2467}
2468
2469SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2470 SDValue Root, MCSymbol *Label) {
2472 SDValue Ops[] = { Root };
2473 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2474 ID.AddPointer(Label);
2475 void *IP = nullptr;
2476 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2477 return SDValue(E, 0);
2478
2479 auto *N =
2480 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2481 createOperands(N, Ops);
2482
2483 CSEMap.InsertNode(N, IP);
2484 InsertNode(N);
2485 return SDValue(N, 0);
2486}
2487
2489 int64_t Offset, bool isTarget,
2490 unsigned TargetFlags) {
2491 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2492 SDVTList VTs = getVTList(VT);
2493
2495 AddNodeIDNode(ID, Opc, VTs, {});
2496 ID.AddPointer(BA);
2497 ID.AddInteger(Offset);
2498 ID.AddInteger(TargetFlags);
2499 void *IP = nullptr;
2500 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2501 return SDValue(E, 0);
2502
2503 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2504 CSEMap.InsertNode(N, IP);
2505 InsertNode(N);
2506 return SDValue(N, 0);
2507}
2508
2511 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2512 ID.AddPointer(V);
2513
2514 void *IP = nullptr;
2515 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2516 return SDValue(E, 0);
2517
2518 auto *N = newSDNode<SrcValueSDNode>(V);
2519 CSEMap.InsertNode(N, IP);
2520 InsertNode(N);
2521 return SDValue(N, 0);
2522}
2523
2526 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2527 ID.AddPointer(MD);
2528
2529 void *IP = nullptr;
2530 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2531 return SDValue(E, 0);
2532
2533 auto *N = newSDNode<MDNodeSDNode>(MD);
2534 CSEMap.InsertNode(N, IP);
2535 InsertNode(N);
2536 return SDValue(N, 0);
2537}
2538
2540 if (VT == V.getValueType())
2541 return V;
2542
2543 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2544}
2545
2547 unsigned SrcAS, unsigned DestAS) {
2548 SDVTList VTs = getVTList(VT);
2549 SDValue Ops[] = {Ptr};
2552 ID.AddInteger(SrcAS);
2553 ID.AddInteger(DestAS);
2554
2555 void *IP = nullptr;
2556 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2557 return SDValue(E, 0);
2558
2559 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2560 VTs, SrcAS, DestAS);
2561 createOperands(N, Ops);
2562
2563 CSEMap.InsertNode(N, IP);
2564 InsertNode(N);
2565 return SDValue(N, 0);
2566}
2567
2569 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2570}
2571
2573 UndefPoisonKind Kind) {
2574 if (isGuaranteedNotToBeUndefOrPoison(V, DemandedElts, Kind))
2575 return V;
2576 return getFreeze(V);
2577}
2578
2579/// getShiftAmountOperand - Return the specified value casted to
2580/// the target's desired shift amount type.
2582 EVT OpTy = Op.getValueType();
2583 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2584 if (OpTy == ShTy || OpTy.isVector()) return Op;
2585
2586 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2587}
2588
2590 SDLoc dl(Node);
2592 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2593 EVT VT = Node->getValueType(0);
2594 SDValue Tmp1 = Node->getOperand(0);
2595 SDValue Tmp2 = Node->getOperand(1);
2596 const MaybeAlign MA(Node->getConstantOperandVal(3));
2597
2598 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2599 Tmp2, MachinePointerInfo(V));
2600 SDValue VAList = VAListLoad;
2601
2602 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2603 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2604 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2605
2606 VAList = getNode(
2607 ISD::AND, dl, VAList.getValueType(), VAList,
2608 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2609 }
2610
2611 // Increment the pointer, VAList, to the next vaarg
2612 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2613 getConstant(getDataLayout().getTypeAllocSize(
2614 VT.getTypeForEVT(*getContext())),
2615 dl, VAList.getValueType()));
2616 // Store the incremented VAList to the legalized pointer
2617 Tmp1 =
2618 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2619 // Load the actual argument out of the pointer VAList
2620 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2621}
2622
2624 SDLoc dl(Node);
2626 // This defaults to loading a pointer from the input and storing it to the
2627 // output, returning the chain.
2628 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2629 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2630 SDValue Tmp1 =
2631 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2632 Node->getOperand(2), MachinePointerInfo(VS));
2633 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2634 MachinePointerInfo(VD));
2635}
2636
2638 const DataLayout &DL = getDataLayout();
2639 Type *Ty = VT.getTypeForEVT(*getContext());
2640 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2641
2642 if (TLI->isTypeLegal(VT) || !VT.isVector())
2643 return RedAlign;
2644
2645 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2646 const Align StackAlign = TFI->getStackAlign();
2647
2648 // See if we can choose a smaller ABI alignment in cases where it's an
2649 // illegal vector type that will get broken down.
2650 if (RedAlign > StackAlign) {
2651 EVT IntermediateVT;
2652 MVT RegisterVT;
2653 unsigned NumIntermediates;
2654 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2655 NumIntermediates, RegisterVT);
2656 Ty = IntermediateVT.getTypeForEVT(*getContext());
2657 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2658 if (RedAlign2 < RedAlign)
2659 RedAlign = RedAlign2;
2660
2661 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2662 // If the stack is not realignable, the alignment should be limited to the
2663 // StackAlignment
2664 RedAlign = std::min(RedAlign, StackAlign);
2665 }
2666
2667 return RedAlign;
2668}
2669
2671 MachineFrameInfo &MFI = MF->getFrameInfo();
2672 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2673 int StackID = 0;
2674 if (Bytes.isScalable())
2675 StackID = TFI->getStackIDForScalableVectors();
2676 // The stack id gives an indication of whether the object is scalable or
2677 // not, so it's safe to pass in the minimum size here.
2678 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2679 false, nullptr, StackID);
2680 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2681}
2682
2684 Type *Ty = VT.getTypeForEVT(*getContext());
2685 Align StackAlign =
2686 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2687 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2688}
2689
2691 TypeSize VT1Size = VT1.getStoreSize();
2692 TypeSize VT2Size = VT2.getStoreSize();
2693 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2694 "Don't know how to choose the maximum size when creating a stack "
2695 "temporary");
2696 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2697 ? VT1Size
2698 : VT2Size;
2699
2700 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2701 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2702 const DataLayout &DL = getDataLayout();
2703 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2704 return CreateStackTemporary(Bytes, Align);
2705}
2706
2708 ISD::CondCode Cond, const SDLoc &dl,
2709 SDNodeFlags Flags) {
2710 EVT OpVT = N1.getValueType();
2711
2712 auto GetUndefBooleanConstant = [&]() {
2713 if (VT.getScalarType() == MVT::i1 ||
2714 TLI->getBooleanContents(OpVT) ==
2716 return getUNDEF(VT);
2717 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2718 // so we cannot use getUNDEF(). Return zero instead.
2719 return getConstant(0, dl, VT);
2720 };
2721
2722 // These setcc operations always fold.
2723 switch (Cond) {
2724 default: break;
2725 case ISD::SETFALSE:
2726 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2727 case ISD::SETTRUE:
2728 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2729
2730 case ISD::SETOEQ:
2731 case ISD::SETOGT:
2732 case ISD::SETOGE:
2733 case ISD::SETOLT:
2734 case ISD::SETOLE:
2735 case ISD::SETONE:
2736 case ISD::SETO:
2737 case ISD::SETUO:
2738 case ISD::SETUEQ:
2739 case ISD::SETUNE:
2740 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2741 break;
2742 }
2743
2744 if (OpVT.isInteger()) {
2745 // For EQ and NE, we can always pick a value for the undef to make the
2746 // predicate pass or fail, so we can return undef.
2747 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2748 // icmp eq/ne X, undef -> undef.
2749 if ((N1.isUndef() || N2.isUndef()) &&
2750 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2751 return GetUndefBooleanConstant();
2752
2753 // If both operands are undef, we can return undef for int comparison.
2754 // icmp undef, undef -> undef.
2755 if (N1.isUndef() && N2.isUndef())
2756 return GetUndefBooleanConstant();
2757
2758 // icmp X, X -> true/false
2759 // icmp X, undef -> true/false because undef could be X.
2760 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2761 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2762 }
2763
2765 const APInt &C2 = N2C->getAPIntValue();
2767 const APInt &C1 = N1C->getAPIntValue();
2768
2770 dl, VT, OpVT);
2771 }
2772 }
2773
2774 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2775 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2776
2777 if (N1CFP && N2CFP) {
2778 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2779 switch (Cond) {
2780 default: break;
2781 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2782 return GetUndefBooleanConstant();
2783 [[fallthrough]];
2784 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2785 OpVT);
2786 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2787 return GetUndefBooleanConstant();
2788 [[fallthrough]];
2790 R==APFloat::cmpLessThan, dl, VT,
2791 OpVT);
2792 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2793 return GetUndefBooleanConstant();
2794 [[fallthrough]];
2795 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2796 OpVT);
2797 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2798 return GetUndefBooleanConstant();
2799 [[fallthrough]];
2801 VT, OpVT);
2802 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2803 return GetUndefBooleanConstant();
2804 [[fallthrough]];
2806 R==APFloat::cmpEqual, dl, VT,
2807 OpVT);
2808 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2809 return GetUndefBooleanConstant();
2810 [[fallthrough]];
2812 R==APFloat::cmpEqual, dl, VT, OpVT);
2813 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2814 OpVT);
2815 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2816 OpVT);
2818 R==APFloat::cmpEqual, dl, VT,
2819 OpVT);
2820 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2821 OpVT);
2823 R==APFloat::cmpLessThan, dl, VT,
2824 OpVT);
2826 R==APFloat::cmpUnordered, dl, VT,
2827 OpVT);
2829 VT, OpVT);
2830 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2831 OpVT);
2832 }
2833 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2834 // Ensure that the constant occurs on the RHS.
2836 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2837 return SDValue();
2838 return getSetCC(dl, VT, N2, N1, SwappedCond, /*Chain=*/{},
2839 /*IsSignaling=*/false, Flags);
2840 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2841 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2842 // If an operand is known to be a nan (or undef that could be a nan), we can
2843 // fold it.
2844 // Choosing NaN for the undef will always make unordered comparison succeed
2845 // and ordered comparison fails.
2846 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2847 switch (ISD::getUnorderedFlavor(Cond)) {
2848 default:
2849 llvm_unreachable("Unknown flavor!");
2850 case 0: // Known false.
2851 return getBoolConstant(false, dl, VT, OpVT);
2852 case 1: // Known true.
2853 return getBoolConstant(true, dl, VT, OpVT);
2854 case 2: // Undefined.
2855 return GetUndefBooleanConstant();
2856 }
2857 }
2858
2859 // Could not fold it.
2860 return SDValue();
2861}
2862
2863/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2864/// use this predicate to simplify operations downstream.
2866 unsigned BitWidth = Op.getScalarValueSizeInBits();
2868}
2869
2870// TODO: Should have argument to specify if sign bit of nan is ignorable.
2872 if (Depth >= MaxRecursionDepth)
2873 return false; // Limit search depth.
2874
2875 unsigned Opc = Op.getOpcode();
2876 switch (Opc) {
2877 case ISD::FABS:
2878 return true;
2879 case ISD::AssertNoFPClass: {
2880 FPClassTest NoFPClass =
2881 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
2882
2883 const FPClassTest TestMask = fcNan | fcNegative;
2884 return (NoFPClass & TestMask) == TestMask;
2885 }
2886 case ISD::ARITH_FENCE:
2887 return SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2888 case ISD::FEXP:
2889 case ISD::FEXP2:
2890 case ISD::FEXP10:
2891 return Op->getFlags().hasNoNaNs();
2892 case ISD::FMINNUM:
2893 case ISD::FMINNUM_IEEE:
2894 case ISD::FMINIMUM:
2895 case ISD::FMINIMUMNUM:
2896 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2897 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2898 case ISD::FMAXNUM:
2899 case ISD::FMAXNUM_IEEE:
2900 case ISD::FMAXIMUM:
2901 case ISD::FMAXIMUMNUM:
2902 // TODO: If we can ignore the sign bit of nans, only one side being known 0
2903 // is sufficient.
2904 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2905 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2906 default:
2907 return false;
2908 }
2909
2910 llvm_unreachable("covered opcode switch");
2911}
2912
2913/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2914/// this predicate to simplify operations downstream. Mask is known to be zero
2915/// for bits that V cannot have.
2917 unsigned Depth) const {
2918 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2919}
2920
2921/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2922/// DemandedElts. We use this predicate to simplify operations downstream.
2923/// Mask is known to be zero for bits that V cannot have.
2925 const APInt &DemandedElts,
2926 unsigned Depth) const {
2927 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2928}
2929
2930/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2931/// DemandedElts. We use this predicate to simplify operations downstream.
2933 unsigned Depth /* = 0 */) const {
2934 return computeKnownBits(V, DemandedElts, Depth).isZero();
2935}
2936
2937/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2939 unsigned Depth) const {
2940 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2941}
2942
2944 const APInt &DemandedElts,
2945 unsigned Depth) const {
2946 EVT VT = Op.getValueType();
2947 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2948
2949 unsigned NumElts = VT.getVectorNumElements();
2950 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2951
2952 APInt KnownZeroElements = APInt::getZero(NumElts);
2953 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2954 if (!DemandedElts[EltIdx])
2955 continue; // Don't query elements that are not demanded.
2956 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2957 if (MaskedVectorIsZero(Op, Mask, Depth))
2958 KnownZeroElements.setBit(EltIdx);
2959 }
2960 return KnownZeroElements;
2961}
2962
2963/// isSplatValue - Return true if the vector V has the same value
2964/// across all DemandedElts. For scalable vectors, we don't know the
2965/// number of lanes at compile time. Instead, we use a 1 bit APInt
2966/// to represent a conservative value for all lanes; that is, that
2967/// one bit value is implicitly splatted across all lanes.
2968bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2969 APInt &UndefElts, unsigned Depth) const {
2970 unsigned Opcode = V.getOpcode();
2971 EVT VT = V.getValueType();
2972 assert(VT.isVector() && "Vector type expected");
2973 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2974 "scalable demanded bits are ignored");
2975
2976 if (!DemandedElts)
2977 return false; // No demanded elts, better to assume we don't know anything.
2978
2979 if (Depth >= MaxRecursionDepth)
2980 return false; // Limit search depth.
2981
2982 // Deal with some common cases here that work for both fixed and scalable
2983 // vector types.
2984 switch (Opcode) {
2985 case ISD::SPLAT_VECTOR:
2986 UndefElts = V.getOperand(0).isUndef()
2987 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2988 : APInt(DemandedElts.getBitWidth(), 0);
2989 return true;
2990 case ISD::ADD:
2991 case ISD::SUB:
2992 case ISD::AND:
2993 case ISD::XOR:
2994 case ISD::OR: {
2995 APInt UndefLHS, UndefRHS;
2996 SDValue LHS = V.getOperand(0);
2997 SDValue RHS = V.getOperand(1);
2998 // Only recognize splats with the same demanded undef elements for both
2999 // operands, otherwise we might fail to handle binop-specific undef
3000 // handling.
3001 // e.g. (and undef, 0) -> 0 etc.
3002 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
3003 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1) &&
3004 (DemandedElts & UndefLHS) == (DemandedElts & UndefRHS)) {
3005 UndefElts = UndefLHS | UndefRHS;
3006 return true;
3007 }
3008 return false;
3009 }
3010 case ISD::ABS:
3012 case ISD::TRUNCATE:
3013 case ISD::SIGN_EXTEND:
3014 case ISD::ZERO_EXTEND:
3015 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
3016 default:
3017 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
3018 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
3019 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
3020 Depth);
3021 break;
3022 }
3023
3024 // We don't support other cases than those above for scalable vectors at
3025 // the moment.
3026 if (VT.isScalableVector())
3027 return false;
3028
3029 unsigned NumElts = VT.getVectorNumElements();
3030 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
3031 UndefElts = APInt::getZero(NumElts);
3032
3033 switch (Opcode) {
3034 case ISD::BUILD_VECTOR: {
3035 SDValue Scl;
3036 for (unsigned i = 0; i != NumElts; ++i) {
3037 SDValue Op = V.getOperand(i);
3038 if (Op.isUndef()) {
3039 UndefElts.setBit(i);
3040 continue;
3041 }
3042 if (!DemandedElts[i])
3043 continue;
3044 if (Scl && Scl != Op)
3045 return false;
3046 Scl = Op;
3047 }
3048 return true;
3049 }
3050 case ISD::VECTOR_SHUFFLE: {
3051 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
3052 APInt DemandedLHS = APInt::getZero(NumElts);
3053 APInt DemandedRHS = APInt::getZero(NumElts);
3054 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
3055 for (int i = 0; i != (int)NumElts; ++i) {
3056 int M = Mask[i];
3057 if (M < 0) {
3058 UndefElts.setBit(i);
3059 continue;
3060 }
3061 if (!DemandedElts[i])
3062 continue;
3063 if (M < (int)NumElts)
3064 DemandedLHS.setBit(M);
3065 else
3066 DemandedRHS.setBit(M - NumElts);
3067 }
3068
3069 // If we aren't demanding either op, assume there's no splat.
3070 // If we are demanding both ops, assume there's no splat.
3071 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
3072 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
3073 return false;
3074
3075 // See if the demanded elts of the source op is a splat or we only demand
3076 // one element, which should always be a splat.
3077 // TODO: Handle source ops splats with undefs.
3078 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
3079 APInt SrcUndefs;
3080 return (SrcElts.popcount() == 1) ||
3081 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
3082 (SrcElts & SrcUndefs).isZero());
3083 };
3084 if (!DemandedLHS.isZero())
3085 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
3086 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
3087 }
3089 // Offset the demanded elts by the subvector index.
3090 SDValue Src = V.getOperand(0);
3091 // We don't support scalable vectors at the moment.
3092 if (Src.getValueType().isScalableVector())
3093 return false;
3094 uint64_t Idx = V.getConstantOperandVal(1);
3095 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3096 APInt UndefSrcElts;
3097 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3098 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3099 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3100 return true;
3101 }
3102 break;
3103 }
3107 // Widen the demanded elts by the src element count.
3108 SDValue Src = V.getOperand(0);
3109 // We don't support scalable vectors at the moment.
3110 if (Src.getValueType().isScalableVector())
3111 return false;
3112 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3113 APInt UndefSrcElts;
3114 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3115 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3116 UndefElts = UndefSrcElts.trunc(NumElts);
3117 return true;
3118 }
3119 break;
3120 }
3121 case ISD::BITCAST: {
3122 SDValue Src = V.getOperand(0);
3123 EVT SrcVT = Src.getValueType();
3124 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3125 unsigned BitWidth = VT.getScalarSizeInBits();
3126
3127 // Ignore bitcasts from unsupported types.
3128 // TODO: Add fp support?
3129 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3130 break;
3131
3132 // Bitcast 'small element' vector to 'large element' vector.
3133 if ((BitWidth % SrcBitWidth) == 0) {
3134 // See if each sub element is a splat.
3135 unsigned Scale = BitWidth / SrcBitWidth;
3136 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3137 APInt ScaledDemandedElts =
3138 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3139 for (unsigned I = 0; I != Scale; ++I) {
3140 APInt SubUndefElts;
3141 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3142 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3143 SubDemandedElts &= ScaledDemandedElts;
3144 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3145 return false;
3146 // TODO: Add support for merging sub undef elements.
3147 if (!SubUndefElts.isZero())
3148 return false;
3149 }
3150 return true;
3151 }
3152 break;
3153 }
3154 }
3155
3156 return false;
3157}
3158
3159/// Helper wrapper to main isSplatValue function.
3160bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3161 EVT VT = V.getValueType();
3162 assert(VT.isVector() && "Vector type expected");
3163
3164 APInt UndefElts;
3165 // Since the number of lanes in a scalable vector is unknown at compile time,
3166 // we track one bit which is implicitly broadcast to all lanes. This means
3167 // that all lanes in a scalable vector are considered demanded.
3168 APInt DemandedElts
3170 return isSplatValue(V, DemandedElts, UndefElts) &&
3171 (AllowUndefs || !UndefElts);
3172}
3173
3176
3177 EVT VT = V.getValueType();
3178 unsigned Opcode = V.getOpcode();
3179 switch (Opcode) {
3180 default: {
3181 APInt UndefElts;
3182 // Since the number of lanes in a scalable vector is unknown at compile time,
3183 // we track one bit which is implicitly broadcast to all lanes. This means
3184 // that all lanes in a scalable vector are considered demanded.
3185 APInt DemandedElts
3187
3188 if (isSplatValue(V, DemandedElts, UndefElts)) {
3189 if (VT.isScalableVector()) {
3190 // DemandedElts and UndefElts are ignored for scalable vectors, since
3191 // the only supported cases are SPLAT_VECTOR nodes.
3192 SplatIdx = 0;
3193 } else {
3194 // Handle case where all demanded elements are UNDEF.
3195 if (DemandedElts.isSubsetOf(UndefElts)) {
3196 SplatIdx = 0;
3197 return getUNDEF(VT);
3198 }
3199 SplatIdx = (UndefElts & DemandedElts).countr_one();
3200 }
3201 return V;
3202 }
3203 break;
3204 }
3205 case ISD::SPLAT_VECTOR:
3206 SplatIdx = 0;
3207 return V;
3208 case ISD::VECTOR_SHUFFLE: {
3209 assert(!VT.isScalableVector());
3210 // Check if this is a shuffle node doing a splat.
3211 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3212 // getTargetVShiftNode currently struggles without the splat source.
3213 auto *SVN = cast<ShuffleVectorSDNode>(V);
3214 if (!SVN->isSplat())
3215 break;
3216 int Idx = SVN->getSplatIndex();
3217 int NumElts = V.getValueType().getVectorNumElements();
3218 SplatIdx = Idx % NumElts;
3219 return V.getOperand(Idx / NumElts);
3220 }
3221 }
3222
3223 return SDValue();
3224}
3225
3227 int SplatIdx;
3228 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3229 EVT SVT = SrcVector.getValueType().getScalarType();
3230 EVT LegalSVT = SVT;
3231 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3232 if (!SVT.isInteger())
3233 return SDValue();
3234 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3235 if (LegalSVT.bitsLT(SVT))
3236 return SDValue();
3237 }
3238 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3239 }
3240 return SDValue();
3241}
3242
3243std::optional<ConstantRange>
3245 unsigned Depth) const {
3246 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3247 V.getOpcode() == ISD::SRA) &&
3248 "Unknown shift node");
3249 // Shifting more than the bitwidth is not valid.
3250 unsigned BitWidth = V.getScalarValueSizeInBits();
3251
3252 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3253 const APInt &ShAmt = Cst->getAPIntValue();
3254 if (ShAmt.uge(BitWidth))
3255 return std::nullopt;
3256 return ConstantRange(ShAmt);
3257 }
3258
3259 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3260 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3261 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3262 if (!DemandedElts[i])
3263 continue;
3264 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3265 if (!SA) {
3266 MinAmt = MaxAmt = nullptr;
3267 break;
3268 }
3269 const APInt &ShAmt = SA->getAPIntValue();
3270 if (ShAmt.uge(BitWidth))
3271 return std::nullopt;
3272 if (!MinAmt || MinAmt->ugt(ShAmt))
3273 MinAmt = &ShAmt;
3274 if (!MaxAmt || MaxAmt->ult(ShAmt))
3275 MaxAmt = &ShAmt;
3276 }
3277 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3278 "Failed to find matching min/max shift amounts");
3279 if (MinAmt && MaxAmt)
3280 return ConstantRange(*MinAmt, *MaxAmt + 1);
3281 }
3282
3283 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3284 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3285 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3286 if (KnownAmt.getMaxValue().ult(BitWidth))
3287 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3288
3289 return std::nullopt;
3290}
3291
3292std::optional<unsigned>
3294 unsigned Depth) const {
3295 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3296 V.getOpcode() == ISD::SRA) &&
3297 "Unknown shift node");
3298 if (std::optional<ConstantRange> AmtRange =
3299 getValidShiftAmountRange(V, DemandedElts, Depth))
3300 if (const APInt *ShAmt = AmtRange->getSingleElement())
3301 return ShAmt->getZExtValue();
3302 return std::nullopt;
3303}
3304
3305std::optional<unsigned>
3307 APInt DemandedElts = getDemandAllEltsMask(V);
3308 return getValidShiftAmount(V, DemandedElts, Depth);
3309}
3310
3311std::optional<unsigned>
3313 unsigned Depth) const {
3314 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3315 V.getOpcode() == ISD::SRA) &&
3316 "Unknown shift node");
3317 if (std::optional<ConstantRange> AmtRange =
3318 getValidShiftAmountRange(V, DemandedElts, Depth))
3319 return AmtRange->getUnsignedMin().getZExtValue();
3320 return std::nullopt;
3321}
3322
3323std::optional<unsigned>
3325 APInt DemandedElts = getDemandAllEltsMask(V);
3326 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3327}
3328
3329std::optional<unsigned>
3331 unsigned Depth) const {
3332 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3333 V.getOpcode() == ISD::SRA) &&
3334 "Unknown shift node");
3335 if (std::optional<ConstantRange> AmtRange =
3336 getValidShiftAmountRange(V, DemandedElts, Depth))
3337 return AmtRange->getUnsignedMax().getZExtValue();
3338 return std::nullopt;
3339}
3340
3341std::optional<unsigned>
3343 APInt DemandedElts = getDemandAllEltsMask(V);
3344 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3345}
3346
3347/// Determine which bits of Op are known to be either zero or one and return
3348/// them in Known. For vectors, the known bits are those that are shared by
3349/// every vector element.
3351 APInt DemandedElts = getDemandAllEltsMask(Op);
3352 return computeKnownBits(Op, DemandedElts, Depth);
3353}
3354
3355/// Determine which bits of Op are known to be either zero or one and return
3356/// them in Known. The DemandedElts argument allows us to only collect the known
3357/// bits that are shared by the requested vector elements.
3359 unsigned Depth) const {
3360 unsigned BitWidth = Op.getScalarValueSizeInBits();
3361
3362 KnownBits Known(BitWidth); // Don't know anything.
3363
3364 if (auto OptAPInt = Op->bitcastToAPInt()) {
3365 // We know all of the bits for a constant!
3366 return KnownBits::makeConstant(*std::move(OptAPInt));
3367 }
3368
3369 if (Depth >= MaxRecursionDepth)
3370 return Known; // Limit search depth.
3371
3372 KnownBits Known2;
3373 unsigned NumElts = DemandedElts.getBitWidth();
3374 assert((!Op.getValueType().isScalableVector() || NumElts == 1) &&
3375 "DemandedElts for scalable vectors must be 1 to represent all lanes");
3376 assert((!Op.getValueType().isFixedLengthVector() ||
3377 NumElts == Op.getValueType().getVectorNumElements()) &&
3378 "Unexpected vector size");
3379
3380 if (!DemandedElts)
3381 return Known; // No demanded elts, better to assume we don't know anything.
3382
3383 unsigned Opcode = Op.getOpcode();
3384 switch (Opcode) {
3385 case ISD::MERGE_VALUES:
3386 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3387 Depth + 1);
3388 case ISD::SPLAT_VECTOR: {
3389 SDValue SrcOp = Op.getOperand(0);
3390 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3391 "Expected SPLAT_VECTOR implicit truncation");
3392 // Implicitly truncate the bits to match the official semantics of
3393 // SPLAT_VECTOR.
3394 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3395 break;
3396 }
3398 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3399 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3400 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3401 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3402 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3403 }
3404 break;
3405 }
3406 case ISD::STEP_VECTOR: {
3407 const APInt &Step = Op.getConstantOperandAPInt(0);
3408
3409 if (Step.isPowerOf2())
3410 Known.Zero.setLowBits(Step.logBase2());
3411
3413
3414 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3415 break;
3416 const APInt MinNumElts =
3417 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3418
3419 bool Overflow;
3420 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3422 .umul_ov(MinNumElts, Overflow);
3423 if (Overflow)
3424 break;
3425
3426 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3427 if (Overflow)
3428 break;
3429
3430 Known.Zero.setHighBits(MaxValue.countl_zero());
3431 break;
3432 }
3433 case ISD::BUILD_VECTOR:
3434 assert(!Op.getValueType().isScalableVector());
3435 // Collect the known bits that are shared by every demanded vector element.
3436 Known.setAllConflict();
3437 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3438 if (!DemandedElts[i])
3439 continue;
3440
3441 SDValue SrcOp = Op.getOperand(i);
3442 Known2 = computeKnownBits(SrcOp, Depth + 1);
3443
3444 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3445 if (SrcOp.getValueSizeInBits() != BitWidth) {
3446 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3447 "Expected BUILD_VECTOR implicit truncation");
3448 Known2 = Known2.trunc(BitWidth);
3449 }
3450
3451 // Known bits are the values that are shared by every demanded element.
3452 Known = Known.intersectWith(Known2);
3453
3454 // If we don't know any bits, early out.
3455 if (Known.isUnknown())
3456 break;
3457 }
3458 break;
3459 case ISD::VECTOR_COMPRESS: {
3460 SDValue Vec = Op.getOperand(0);
3461 SDValue PassThru = Op.getOperand(2);
3462 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3463 // If we don't know any bits, early out.
3464 if (Known.isUnknown())
3465 break;
3466 Known2 = computeKnownBits(Vec, Depth + 1);
3467 Known = Known.intersectWith(Known2);
3468 break;
3469 }
3470 case ISD::VECTOR_SHUFFLE: {
3471 assert(!Op.getValueType().isScalableVector());
3472 // Collect the known bits that are shared by every vector element referenced
3473 // by the shuffle.
3474 APInt DemandedLHS, DemandedRHS;
3476 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3477 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3478 DemandedLHS, DemandedRHS))
3479 break;
3480
3481 // Known bits are the values that are shared by every demanded element.
3482 Known.setAllConflict();
3483 if (!!DemandedLHS) {
3484 SDValue LHS = Op.getOperand(0);
3485 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3486 Known = Known.intersectWith(Known2);
3487 }
3488 // If we don't know any bits, early out.
3489 if (Known.isUnknown())
3490 break;
3491 if (!!DemandedRHS) {
3492 SDValue RHS = Op.getOperand(1);
3493 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3494 Known = Known.intersectWith(Known2);
3495 }
3496 break;
3497 }
3498 case ISD::VSCALE: {
3500 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3501 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3502 break;
3503 }
3504 case ISD::CONCAT_VECTORS: {
3505 if (Op.getValueType().isScalableVector())
3506 break;
3507 // Split DemandedElts and test each of the demanded subvectors.
3508 Known.setAllConflict();
3509 EVT SubVectorVT = Op.getOperand(0).getValueType();
3510 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3511 unsigned NumSubVectors = Op.getNumOperands();
3512 for (unsigned i = 0; i != NumSubVectors; ++i) {
3513 APInt DemandedSub =
3514 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3515 if (!!DemandedSub) {
3516 SDValue Sub = Op.getOperand(i);
3517 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3518 Known = Known.intersectWith(Known2);
3519 }
3520 // If we don't know any bits, early out.
3521 if (Known.isUnknown())
3522 break;
3523 }
3524 break;
3525 }
3526 case ISD::INSERT_SUBVECTOR: {
3527 if (Op.getValueType().isScalableVector())
3528 break;
3529 // Demand any elements from the subvector and the remainder from the src its
3530 // inserted into.
3531 SDValue Src = Op.getOperand(0);
3532 SDValue Sub = Op.getOperand(1);
3533 uint64_t Idx = Op.getConstantOperandVal(2);
3534 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3535 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3536 APInt DemandedSrcElts = DemandedElts;
3537 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3538
3539 Known.setAllConflict();
3540 if (!!DemandedSubElts) {
3541 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3542 if (Known.isUnknown())
3543 break; // early-out.
3544 }
3545 if (!!DemandedSrcElts) {
3546 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3547 Known = Known.intersectWith(Known2);
3548 }
3549 break;
3550 }
3552 // Offset the demanded elts by the subvector index.
3553 SDValue Src = Op.getOperand(0);
3554
3555 APInt DemandedSrcElts;
3556 if (Src.getValueType().isScalableVector())
3557 DemandedSrcElts = APInt(1, 1); // <=> 'demand all elements'
3558 else {
3559 uint64_t Idx = Op.getConstantOperandVal(1);
3560 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3561 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3562 }
3563 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3564 break;
3565 }
3566 case ISD::SCALAR_TO_VECTOR: {
3567 if (Op.getValueType().isScalableVector())
3568 break;
3569 // We know about scalar_to_vector as much as we know about it source,
3570 // which becomes the first element of otherwise unknown vector.
3571 if (DemandedElts != 1)
3572 break;
3573
3574 SDValue N0 = Op.getOperand(0);
3575 Known = computeKnownBits(N0, Depth + 1);
3576 if (N0.getValueSizeInBits() != BitWidth)
3577 Known = Known.trunc(BitWidth);
3578
3579 break;
3580 }
3581 case ISD::BITCAST: {
3582 if (Op.getValueType().isScalableVector())
3583 break;
3584
3585 SDValue N0 = Op.getOperand(0);
3586 EVT SubVT = N0.getValueType();
3587 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3588
3589 // Ignore bitcasts from unsupported types.
3590 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3591 break;
3592
3593 // Fast handling of 'identity' bitcasts.
3594 if (BitWidth == SubBitWidth) {
3595 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3596 break;
3597 }
3598
3599 bool IsLE = getDataLayout().isLittleEndian();
3600
3601 // Bitcast 'small element' vector to 'large element' scalar/vector.
3602 if ((BitWidth % SubBitWidth) == 0) {
3603 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3604
3605 // Collect known bits for the (larger) output by collecting the known
3606 // bits from each set of sub elements and shift these into place.
3607 // We need to separately call computeKnownBits for each set of
3608 // sub elements as the knownbits for each is likely to be different.
3609 unsigned SubScale = BitWidth / SubBitWidth;
3610 APInt SubDemandedElts(NumElts * SubScale, 0);
3611 for (unsigned i = 0; i != NumElts; ++i)
3612 if (DemandedElts[i])
3613 SubDemandedElts.setBit(i * SubScale);
3614
3615 for (unsigned i = 0; i != SubScale; ++i) {
3616 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3617 Depth + 1);
3618 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3619 Known.insertBits(Known2, SubBitWidth * Shifts);
3620 }
3621 }
3622
3623 // Bitcast 'large element' scalar/vector to 'small element' vector.
3624 if ((SubBitWidth % BitWidth) == 0) {
3625 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3626
3627 // Collect known bits for the (smaller) output by collecting the known
3628 // bits from the overlapping larger input elements and extracting the
3629 // sub sections we actually care about.
3630 unsigned SubScale = SubBitWidth / BitWidth;
3631 APInt SubDemandedElts =
3632 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3633 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3634
3635 Known.setAllConflict();
3636 for (unsigned i = 0; i != NumElts; ++i)
3637 if (DemandedElts[i]) {
3638 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3639 unsigned Offset = (Shifts % SubScale) * BitWidth;
3640 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3641 // If we don't know any bits, early out.
3642 if (Known.isUnknown())
3643 break;
3644 }
3645 }
3646 break;
3647 }
3648 case ISD::AND:
3649 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3650 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3651
3652 Known &= Known2;
3653 break;
3654 case ISD::OR:
3655 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3656 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3657
3658 Known |= Known2;
3659 break;
3660 case ISD::XOR:
3661 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3662 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3663
3664 Known ^= Known2;
3665 break;
3666 case ISD::MUL: {
3667 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3668 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3669 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3670 // TODO: SelfMultiply can be poison, but not undef.
3671 if (SelfMultiply)
3672 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3673 Op.getOperand(0), DemandedElts, UndefPoisonKind::UndefOrPoison,
3674 Depth + 1);
3675 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3676
3677 // If the multiplication is known not to overflow, the product of a number
3678 // with itself is non-negative. Only do this if we didn't already computed
3679 // the opposite value for the sign bit.
3680 if (Op->getFlags().hasNoSignedWrap() &&
3681 Op.getOperand(0) == Op.getOperand(1) &&
3682 !Known.isNegative())
3683 Known.makeNonNegative();
3684 break;
3685 }
3686 case ISD::MULHU: {
3687 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3688 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3689 Known = KnownBits::mulhu(Known, Known2);
3690 break;
3691 }
3692 case ISD::MULHS: {
3693 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3694 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3695 Known = KnownBits::mulhs(Known, Known2);
3696 break;
3697 }
3698 case ISD::ABDU: {
3699 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3700 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3701 Known = KnownBits::abdu(Known, Known2);
3702 break;
3703 }
3704 case ISD::ABDS: {
3705 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3706 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3707 Known = KnownBits::abds(Known, Known2);
3708 unsigned SignBits1 =
3709 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3710 if (SignBits1 == 1)
3711 break;
3712 unsigned SignBits0 =
3713 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3714 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3715 break;
3716 }
3717 case ISD::UMUL_LOHI: {
3718 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3719 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3720 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3721 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3722 if (Op.getResNo() == 0)
3723 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3724 else
3725 Known = KnownBits::mulhu(Known, Known2);
3726 break;
3727 }
3728 case ISD::SMUL_LOHI: {
3729 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3730 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3731 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3732 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3733 if (Op.getResNo() == 0)
3734 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3735 else
3736 Known = KnownBits::mulhs(Known, Known2);
3737 break;
3738 }
3739 case ISD::AVGFLOORU: {
3740 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3741 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3742 Known = KnownBits::avgFloorU(Known, Known2);
3743 break;
3744 }
3745 case ISD::AVGCEILU: {
3746 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3747 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3748 Known = KnownBits::avgCeilU(Known, Known2);
3749 break;
3750 }
3751 case ISD::AVGFLOORS: {
3752 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3753 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3754 Known = KnownBits::avgFloorS(Known, Known2);
3755 break;
3756 }
3757 case ISD::AVGCEILS: {
3758 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3759 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3760 Known = KnownBits::avgCeilS(Known, Known2);
3761 break;
3762 }
3763 case ISD::SELECT:
3764 case ISD::VSELECT:
3765 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3766 // If we don't know any bits, early out.
3767 if (Known.isUnknown())
3768 break;
3769 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3770
3771 // Only known if known in both the LHS and RHS.
3772 Known = Known.intersectWith(Known2);
3773 break;
3774 case ISD::SELECT_CC:
3775 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3776 // If we don't know any bits, early out.
3777 if (Known.isUnknown())
3778 break;
3779 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3780
3781 // Only known if known in both the LHS and RHS.
3782 Known = Known.intersectWith(Known2);
3783 break;
3784 case ISD::SMULO:
3785 case ISD::UMULO:
3786 if (Op.getResNo() != 1)
3787 break;
3788 // The boolean result conforms to getBooleanContents.
3789 // If we know the result of a setcc has the top bits zero, use this info.
3790 // We know that we have an integer-based boolean since these operations
3791 // are only available for integer.
3792 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3794 BitWidth > 1)
3795 Known.Zero.setBitsFrom(1);
3796 break;
3797 case ISD::SETCC:
3798 case ISD::SETCCCARRY:
3799 case ISD::STRICT_FSETCC:
3800 case ISD::STRICT_FSETCCS: {
3801 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3802 // If we know the result of a setcc has the top bits zero, use this info.
3803 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3805 BitWidth > 1)
3806 Known.Zero.setBitsFrom(1);
3807 break;
3808 }
3809 case ISD::SHL: {
3810 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3811 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3812
3813 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3814 bool NSW = Op->getFlags().hasNoSignedWrap();
3815
3816 bool ShAmtNonZero = Known2.isNonZero();
3817
3818 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3819
3820 // Minimum shift low bits are known zero.
3821 if (std::optional<unsigned> ShMinAmt =
3822 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3823 Known.Zero.setLowBits(*ShMinAmt);
3824 break;
3825 }
3826 case ISD::SRL:
3827 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3828 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3829 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3830 Op->getFlags().hasExact());
3831
3832 // Minimum shift high bits are known zero.
3833 if (std::optional<unsigned> ShMinAmt =
3834 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3835 Known.Zero.setHighBits(*ShMinAmt);
3836 break;
3837 case ISD::SRA:
3838 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3839 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3840 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3841 Op->getFlags().hasExact());
3842 break;
3843 case ISD::ROTL:
3844 case ISD::ROTR:
3845 if (ConstantSDNode *C =
3846 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3847 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3848
3849 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3850
3851 // Canonicalize to ROTR.
3852 if (Opcode == ISD::ROTL && Amt != 0)
3853 Amt = BitWidth - Amt;
3854
3855 Known.Zero = Known.Zero.rotr(Amt);
3856 Known.One = Known.One.rotr(Amt);
3857 }
3858 break;
3859 case ISD::FSHL:
3860 case ISD::FSHR:
3861 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3862 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3863
3864 // For fshl, 0-shift returns the 1st arg.
3865 // For fshr, 0-shift returns the 2nd arg.
3866 if (Amt == 0) {
3867 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3868 DemandedElts, Depth + 1);
3869 break;
3870 }
3871
3872 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3873 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3874 const APInt ShAmt(BitWidth, Amt);
3875 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3876 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3877 Known = Opcode == ISD::FSHL ? KnownBits::fshl(Known, Known2, ShAmt)
3878 : KnownBits::fshr(Known, Known2, ShAmt);
3879 }
3880 break;
3881 case ISD::SHL_PARTS:
3882 case ISD::SRA_PARTS:
3883 case ISD::SRL_PARTS: {
3884 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3885
3886 // Collect lo/hi source values and concatenate.
3887 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3888 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3889 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3890 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3891 Known = Known2.concat(Known);
3892
3893 // Collect shift amount.
3894 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3895
3896 if (Opcode == ISD::SHL_PARTS)
3897 Known = KnownBits::shl(Known, Known2);
3898 else if (Opcode == ISD::SRA_PARTS)
3899 Known = KnownBits::ashr(Known, Known2);
3900 else // if (Opcode == ISD::SRL_PARTS)
3901 Known = KnownBits::lshr(Known, Known2);
3902
3903 // TODO: Minimum shift low/high bits are known zero.
3904
3905 if (Op.getResNo() == 0)
3906 Known = Known.extractBits(LoBits, 0);
3907 else
3908 Known = Known.extractBits(HiBits, LoBits);
3909 break;
3910 }
3912 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3913 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3914 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3915 break;
3916 }
3917 case ISD::CTTZ:
3918 case ISD::CTTZ_ZERO_POISON: {
3919 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3920 // If we have a known 1, its position is our upper bound.
3921 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3922 unsigned LowBits = llvm::bit_width(PossibleTZ);
3923 Known.Zero.setBitsFrom(LowBits);
3924 break;
3925 }
3926 case ISD::CTLZ:
3927 case ISD::CTLZ_ZERO_POISON: {
3928 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3929 // If we have a known 1, its position is our upper bound.
3930 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3931 unsigned LowBits = llvm::bit_width(PossibleLZ);
3932 Known.Zero.setBitsFrom(LowBits);
3933 break;
3934 }
3935 case ISD::CTLS: {
3936 unsigned MinRedundantSignBits =
3937 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1;
3938 ConstantRange Range(APInt(BitWidth, MinRedundantSignBits),
3940 Known = Range.toKnownBits();
3941 break;
3942 }
3943 case ISD::CTPOP: {
3944 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3945 // If we know some of the bits are zero, they can't be one.
3946 unsigned PossibleOnes = Known2.countMaxPopulation();
3947 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3948 break;
3949 }
3950 case ISD::PARITY: {
3951 // Parity returns 0 everywhere but the LSB.
3952 Known.Zero.setBitsFrom(1);
3953 break;
3954 }
3955 case ISD::PDEP: {
3956 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3957 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3958 Known = KnownBits::pdep(Known2, Known);
3959 break;
3960 }
3961 case ISD::PEXT: {
3962 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3963 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3964 Known = KnownBits::pext(Known2, Known);
3965 break;
3966 }
3967 case ISD::CLMUL: {
3968 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3969 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3970 Known = KnownBits::clmul(Known, Known2);
3971 break;
3972 }
3973 case ISD::MGATHER:
3974 case ISD::MLOAD: {
3975 ISD::LoadExtType ETy =
3976 (Opcode == ISD::MGATHER)
3977 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3978 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3979 if (ETy == ISD::ZEXTLOAD) {
3980 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3981 KnownBits Known0(MemVT.getScalarSizeInBits());
3982 return Known0.zext(BitWidth);
3983 }
3984 break;
3985 }
3986 case ISD::LOAD: {
3988 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3989 if (ISD::isNON_EXTLoad(LD) && Cst) {
3990 // Determine any common known bits from the loaded constant pool value.
3991 Type *CstTy = Cst->getType();
3992 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3993 !Op.getValueType().isScalableVector()) {
3994 // If its a vector splat, then we can (quickly) reuse the scalar path.
3995 // NOTE: We assume all elements match and none are UNDEF.
3996 if (CstTy->isVectorTy()) {
3997 if (const Constant *Splat = Cst->getSplatValue()) {
3998 Cst = Splat;
3999 CstTy = Cst->getType();
4000 }
4001 }
4002 // TODO - do we need to handle different bitwidths?
4003 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
4004 // Iterate across all vector elements finding common known bits.
4005 Known.setAllConflict();
4006 for (unsigned i = 0; i != NumElts; ++i) {
4007 if (!DemandedElts[i])
4008 continue;
4009 if (Constant *Elt = Cst->getAggregateElement(i)) {
4010 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
4011 const APInt &Value = CInt->getValue();
4012 Known.One &= Value;
4013 Known.Zero &= ~Value;
4014 continue;
4015 }
4016 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
4017 APInt Value = CFP->getValueAPF().bitcastToAPInt();
4018 Known.One &= Value;
4019 Known.Zero &= ~Value;
4020 continue;
4021 }
4022 }
4023 Known.One.clearAllBits();
4024 Known.Zero.clearAllBits();
4025 break;
4026 }
4027 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
4028 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
4029 Known = KnownBits::makeConstant(CInt->getValue());
4030 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
4031 Known =
4032 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
4033 }
4034 }
4035 }
4036 } else if (Op.getResNo() == 0) {
4037 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
4038 KnownBits KnownScalarMemory(ScalarMemorySize);
4039 if (const MDNode *MD = LD->getRanges())
4040 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4041
4042 // Extend the Known bits from memory to the size of the scalar result.
4043 if (ISD::isZEXTLoad(Op.getNode()))
4044 Known = KnownScalarMemory.zext(BitWidth);
4045 else if (ISD::isSEXTLoad(Op.getNode()))
4046 Known = KnownScalarMemory.sext(BitWidth);
4047 else if (ISD::isEXTLoad(Op.getNode()))
4048 Known = KnownScalarMemory.anyext(BitWidth);
4049 else
4050 Known = KnownScalarMemory;
4051 assert(Known.getBitWidth() == BitWidth);
4052 return Known;
4053 }
4054 break;
4055 }
4057 if (Op.getValueType().isScalableVector())
4058 break;
4059 EVT InVT = Op.getOperand(0).getValueType();
4060 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4061 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4062 Known = Known.zext(BitWidth);
4063 break;
4064 }
4065 case ISD::ZERO_EXTEND: {
4066 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4067 Known = Known.zext(BitWidth);
4068 break;
4069 }
4071 if (Op.getValueType().isScalableVector())
4072 break;
4073 EVT InVT = Op.getOperand(0).getValueType();
4074 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4075 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4076 // If the sign bit is known to be zero or one, then sext will extend
4077 // it to the top bits, else it will just zext.
4078 Known = Known.sext(BitWidth);
4079 break;
4080 }
4081 case ISD::SIGN_EXTEND: {
4082 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4083 // If the sign bit is known to be zero or one, then sext will extend
4084 // it to the top bits, else it will just zext.
4085 Known = Known.sext(BitWidth);
4086 break;
4087 }
4089 if (Op.getValueType().isScalableVector())
4090 break;
4091 EVT InVT = Op.getOperand(0).getValueType();
4092 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4093 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4094 Known = Known.anyext(BitWidth);
4095 break;
4096 }
4097 case ISD::ANY_EXTEND: {
4098 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4099 Known = Known.anyext(BitWidth);
4100 break;
4101 }
4102 case ISD::TRUNCATE: {
4103 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4104 Known = Known.trunc(BitWidth);
4105 break;
4106 }
4107 case ISD::TRUNCATE_SSAT_S: {
4108 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4109 Known = Known.truncSSat(BitWidth);
4110 break;
4111 }
4112 case ISD::TRUNCATE_SSAT_U: {
4113 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4114 Known = Known.truncSSatU(BitWidth);
4115 break;
4116 }
4117 case ISD::TRUNCATE_USAT_U: {
4118 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4119 Known = Known.truncUSat(BitWidth);
4120 break;
4121 }
4122 case ISD::AssertZext: {
4123 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4125 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4126 Known.Zero |= (~InMask);
4127 Known.One &= (~Known.Zero);
4128 break;
4129 }
4130 case ISD::AssertAlign: {
4131 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4132 assert(LogOfAlign != 0);
4133
4134 // TODO: Should use maximum with source
4135 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4136 // well as clearing one bits.
4137 Known.Zero.setLowBits(LogOfAlign);
4138 Known.One.clearLowBits(LogOfAlign);
4139 break;
4140 }
4141 case ISD::AssertNoFPClass: {
4142 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4143
4144 FPClassTest NoFPClass =
4145 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
4146 const FPClassTest NegativeTestMask = fcNan | fcNegative;
4147 if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
4148 // Cannot be negative.
4149 Known.makeNonNegative();
4150 }
4151
4152 const FPClassTest PositiveTestMask = fcNan | fcPositive;
4153 if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
4154 // Cannot be positive.
4155 Known.makeNegative();
4156 }
4157
4158 break;
4159 }
4160 case ISD::FABS:
4161 // fabs clears the sign bit
4162 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4163 Known.makeNonNegative();
4164 break;
4165 case ISD::FGETSIGN:
4166 // All bits are zero except the low bit.
4167 Known.Zero.setBitsFrom(1);
4168 break;
4169 case ISD::ADD: {
4170 SDNodeFlags Flags = Op.getNode()->getFlags();
4171 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4172 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4173 bool SelfAdd = Op.getOperand(0) == Op.getOperand(1) &&
4175 Op.getOperand(0), DemandedElts,
4177 Known = KnownBits::add(Known, Known2, Flags.hasNoSignedWrap(),
4178 Flags.hasNoUnsignedWrap(), SelfAdd);
4179 break;
4180 }
4181 case ISD::SUB: {
4182 SDNodeFlags Flags = Op.getNode()->getFlags();
4183 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4184 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4185 Known = KnownBits::sub(Known, Known2, Flags.hasNoSignedWrap(),
4186 Flags.hasNoUnsignedWrap());
4187 break;
4188 }
4189 case ISD::USUBO:
4190 case ISD::SSUBO:
4191 case ISD::USUBO_CARRY:
4192 case ISD::SSUBO_CARRY:
4193 if (Op.getResNo() == 1) {
4194 // If we know the result of a setcc has the top bits zero, use this info.
4195 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4197 BitWidth > 1)
4198 Known.Zero.setBitsFrom(1);
4199 break;
4200 }
4201 [[fallthrough]];
4202 case ISD::SUBC: {
4203 assert(Op.getResNo() == 0 &&
4204 "We only compute knownbits for the difference here.");
4205
4206 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4207 KnownBits Borrow(1);
4208 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4209 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4210 // Borrow has bit width 1
4211 Borrow = Borrow.trunc(1);
4212 } else {
4213 Borrow.setAllZero();
4214 }
4215
4216 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4217 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4218 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4219 break;
4220 }
4221 case ISD::UADDO:
4222 case ISD::SADDO:
4223 case ISD::UADDO_CARRY:
4224 case ISD::SADDO_CARRY:
4225 if (Op.getResNo() == 1) {
4226 // If we know the result of a setcc has the top bits zero, use this info.
4227 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4229 BitWidth > 1)
4230 Known.Zero.setBitsFrom(1);
4231 break;
4232 }
4233 [[fallthrough]];
4234 case ISD::ADDC:
4235 case ISD::ADDE: {
4236 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4237
4238 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4239 KnownBits Carry(1);
4240 if (Opcode == ISD::ADDE)
4241 // Can't track carry from glue, set carry to unknown.
4242 Carry.resetAll();
4243 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4244 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4245 // Carry has bit width 1
4246 Carry = Carry.trunc(1);
4247 } else {
4248 Carry.setAllZero();
4249 }
4250
4251 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4252 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4253 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4254 break;
4255 }
4256 case ISD::UDIV: {
4257 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4258 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4259 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4260 break;
4261 }
4262 case ISD::SDIV: {
4263 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4264 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4265 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4266 break;
4267 }
4268 case ISD::SREM: {
4269 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4270 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4271 Known = KnownBits::srem(Known, Known2);
4272 break;
4273 }
4274 case ISD::UREM: {
4275 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4276 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4277 Known = KnownBits::urem(Known, Known2);
4278 break;
4279 }
4280 case ISD::EXTRACT_ELEMENT: {
4281 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4282 const unsigned Index = Op.getConstantOperandVal(1);
4283 const unsigned EltBitWidth = Op.getValueSizeInBits();
4284
4285 // Remove low part of known bits mask
4286 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4287 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4288
4289 // Remove high part of known bit mask
4290 Known = Known.trunc(EltBitWidth);
4291 break;
4292 }
4294 SDValue InVec = Op.getOperand(0);
4295 SDValue EltNo = Op.getOperand(1);
4296 EVT VecVT = InVec.getValueType();
4297 // computeKnownBits not yet implemented for scalable vectors.
4298 if (VecVT.isScalableVector())
4299 break;
4300 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4301 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4302
4303 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4304 // anything about the extended bits.
4305 if (BitWidth > EltBitWidth)
4306 Known = Known.trunc(EltBitWidth);
4307
4308 // If we know the element index, just demand that vector element, else for
4309 // an unknown element index, ignore DemandedElts and demand them all.
4310 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4311 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4312 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4313 DemandedSrcElts =
4314 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4315
4316 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4317 if (BitWidth > EltBitWidth)
4318 Known = Known.anyext(BitWidth);
4319 break;
4320 }
4322 if (Op.getValueType().isScalableVector())
4323 break;
4324
4325 // If we know the element index, split the demand between the
4326 // source vector and the inserted element, otherwise assume we need
4327 // the original demanded vector elements and the value.
4328 SDValue InVec = Op.getOperand(0);
4329 SDValue InVal = Op.getOperand(1);
4330 SDValue EltNo = Op.getOperand(2);
4331 bool DemandedVal = true;
4332 APInt DemandedVecElts = DemandedElts;
4333 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4334 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4335 unsigned EltIdx = CEltNo->getZExtValue();
4336 DemandedVal = !!DemandedElts[EltIdx];
4337 DemandedVecElts.clearBit(EltIdx);
4338 }
4339 Known.setAllConflict();
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 case ISD::ABS_MIN_POISON: {
4362 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4363 Known = Known2.abs();
4364 Known.Zero.setHighBits(
4365 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4366 break;
4367 }
4368 case ISD::USUBSAT: {
4369 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4370 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4371 Known = KnownBits::usub_sat(Known, Known2);
4372 break;
4373 }
4374 case ISD::UMIN: {
4375 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4376 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4377 Known = KnownBits::umin(Known, Known2);
4378 break;
4379 }
4380 case ISD::UMAX: {
4381 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4382 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4383 Known = KnownBits::umax(Known, Known2);
4384 break;
4385 }
4386 case ISD::SMIN:
4387 case ISD::SMAX: {
4388 // If we have a clamp pattern, we know that the number of sign bits will be
4389 // the minimum of the clamp min/max range.
4390 bool IsMax = (Opcode == ISD::SMAX);
4391 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4392 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4393 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4394 CstHigh =
4395 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4396 if (CstLow && CstHigh) {
4397 if (!IsMax)
4398 std::swap(CstLow, CstHigh);
4399
4400 const APInt &ValueLow = CstLow->getAPIntValue();
4401 const APInt &ValueHigh = CstHigh->getAPIntValue();
4402 if (ValueLow.sle(ValueHigh)) {
4403 unsigned LowSignBits = ValueLow.getNumSignBits();
4404 unsigned HighSignBits = ValueHigh.getNumSignBits();
4405 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4406 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4407 Known.One.setHighBits(MinSignBits);
4408 break;
4409 }
4410 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4411 Known.Zero.setHighBits(MinSignBits);
4412 break;
4413 }
4414 }
4415 }
4416
4417 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4418 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4419 if (IsMax)
4420 Known = KnownBits::smax(Known, Known2);
4421 else
4422 Known = KnownBits::smin(Known, Known2);
4423
4424 // For SMAX, if CstLow is non-negative we know the result will be
4425 // non-negative and thus all sign bits are 0.
4426 // TODO: There's an equivalent of this for smin with negative constant for
4427 // known ones.
4428 if (IsMax && CstLow) {
4429 const APInt &ValueLow = CstLow->getAPIntValue();
4430 if (ValueLow.isNonNegative()) {
4431 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4432 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4433 }
4434 }
4435
4436 break;
4437 }
4438 case ISD::UINT_TO_FP: {
4439 Known.makeNonNegative();
4440 break;
4441 }
4442 case ISD::SINT_TO_FP: {
4443 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4444 if (Known2.isNonNegative())
4445 Known.makeNonNegative();
4446 else if (Known2.isNegative())
4447 Known.makeNegative();
4448 break;
4449 }
4450 case ISD::FP_TO_UINT_SAT: {
4451 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4452 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4454 break;
4455 }
4456 case ISD::ATOMIC_LOAD: {
4457 // If we are looking at the loaded value.
4458 if (Op.getResNo() == 0) {
4459 auto *AT = cast<AtomicSDNode>(Op);
4460 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4461 KnownBits KnownScalarMemory(ScalarMemorySize);
4462 if (const MDNode *MD = AT->getRanges())
4463 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4464
4465 switch (AT->getExtensionType()) {
4466 case ISD::ZEXTLOAD:
4467 Known = KnownScalarMemory.zext(BitWidth);
4468 break;
4469 case ISD::SEXTLOAD:
4470 Known = KnownScalarMemory.sext(BitWidth);
4471 break;
4472 case ISD::EXTLOAD:
4473 switch (TLI->getExtendForAtomicOps()) {
4474 case ISD::ZERO_EXTEND:
4475 Known = KnownScalarMemory.zext(BitWidth);
4476 break;
4477 case ISD::SIGN_EXTEND:
4478 Known = KnownScalarMemory.sext(BitWidth);
4479 break;
4480 default:
4481 Known = KnownScalarMemory.anyext(BitWidth);
4482 break;
4483 }
4484 break;
4485 case ISD::NON_EXTLOAD:
4486 Known = KnownScalarMemory;
4487 break;
4488 }
4489 assert(Known.getBitWidth() == BitWidth);
4490 }
4491 break;
4492 }
4494 if (Op.getResNo() == 1) {
4495 // The boolean result conforms to getBooleanContents.
4496 // If we know the result of a setcc has the top bits zero, use this info.
4497 // We know that we have an integer-based boolean since these operations
4498 // are only available for integer.
4499 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4501 BitWidth > 1)
4502 Known.Zero.setBitsFrom(1);
4503 break;
4504 }
4505 [[fallthrough]];
4507 case ISD::ATOMIC_SWAP:
4518 case ISD::ATOMIC_LOAD_UMAX: {
4519 // If we are looking at the loaded value.
4520 if (Op.getResNo() == 0) {
4521 auto *AT = cast<AtomicSDNode>(Op);
4522 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4523
4524 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4525 Known.Zero.setBitsFrom(MemBits);
4526 }
4527 break;
4528 }
4529 case ISD::FrameIndex:
4531 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4532 Known, getMachineFunction());
4533 break;
4534
4535 default:
4536 if (Opcode < ISD::BUILTIN_OP_END)
4537 break;
4538 [[fallthrough]];
4542 // Allow the target to implement this method for its nodes.
4543 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4544 break;
4545 }
4546
4547 return Known;
4548}
4549
4550/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4563
4566 // X + 0 never overflow
4567 if (isNullConstant(N1))
4568 return OFK_Never;
4569
4570 // If both operands each have at least two sign bits, the addition
4571 // cannot overflow.
4572 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4573 return OFK_Never;
4574
4575 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4576 return OFK_Sometime;
4577}
4578
4581 // X + 0 never overflow
4582 if (isNullConstant(N1))
4583 return OFK_Never;
4584
4585 // mulhi + 1 never overflow
4586 KnownBits N1Known = computeKnownBits(N1);
4587 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4588 N1Known.getMaxValue().ult(2))
4589 return OFK_Never;
4590
4591 KnownBits N0Known = computeKnownBits(N0);
4592 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4593 N0Known.getMaxValue().ult(2))
4594 return OFK_Never;
4595
4596 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4597 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4598 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4599 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4600}
4601
4604 // X - 0 never overflow
4605 if (isNullConstant(N1))
4606 return OFK_Never;
4607
4608 // If both operands each have at least two sign bits, the subtraction
4609 // cannot overflow.
4610 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4611 return OFK_Never;
4612
4613 KnownBits N0Known = computeKnownBits(N0);
4614 KnownBits N1Known = computeKnownBits(N1);
4615 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4616 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4617 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4618}
4619
4622 // X - 0 never overflow
4623 if (isNullConstant(N1))
4624 return OFK_Never;
4625
4626 ConstantRange N0Range =
4627 computeConstantRangeIncludingKnownBits(N0, /*ForSigned=*/false);
4628 ConstantRange N1Range =
4629 computeConstantRangeIncludingKnownBits(N1, /*ForSigned=*/false);
4630 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4631}
4632
4635 // X * 0 and X * 1 never overflow.
4636 if (isNullConstant(N1) || isOneConstant(N1))
4637 return OFK_Never;
4638
4641 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4642}
4643
4646 // X * 0 and X * 1 never overflow.
4647 if (isNullConstant(N1) || isOneConstant(N1))
4648 return OFK_Never;
4649
4650 // Get the size of the result.
4651 unsigned BitWidth = N0.getScalarValueSizeInBits();
4652
4653 // Sum of the sign bits.
4654 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4655
4656 // If we have enough sign bits, then there's no overflow.
4657 if (SignBits > BitWidth + 1)
4658 return OFK_Never;
4659
4660 if (SignBits == BitWidth + 1) {
4661 // The overflow occurs when the true multiplication of the
4662 // the operands is the minimum negative number.
4663 KnownBits N0Known = computeKnownBits(N0);
4664 KnownBits N1Known = computeKnownBits(N1);
4665 // If one of the operands is non-negative, then there's no
4666 // overflow.
4667 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4668 return OFK_Never;
4669 }
4670
4671 return OFK_Sometime;
4672}
4673
4675 unsigned Depth) const {
4676 APInt DemandedElts = getDemandAllEltsMask(Op);
4677 return computeConstantRange(Op, DemandedElts, ForSigned, Depth);
4678}
4679
4681 const APInt &DemandedElts,
4682 bool ForSigned,
4683 unsigned Depth) const {
4684 EVT VT = Op.getValueType();
4685 unsigned BitWidth = VT.getScalarSizeInBits();
4686
4687 if (Depth >= MaxRecursionDepth)
4688 return ConstantRange::getFull(BitWidth);
4689
4690 if (ConstantSDNode *C = isConstOrConstSplat(Op, DemandedElts))
4691 return ConstantRange(C->getAPIntValue());
4692
4693 unsigned Opcode = Op.getOpcode();
4694 switch (Opcode) {
4695 case ISD::VSCALE: {
4697 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
4698 return getVScaleRange(&F, BitWidth).multiply(Multiplier);
4699 }
4700 default:
4701 break;
4702 }
4703
4704 return ConstantRange::getFull(BitWidth);
4705}
4706
4709 unsigned Depth) const {
4710 APInt DemandedElts = getDemandAllEltsMask(Op);
4711 return computeConstantRangeIncludingKnownBits(Op, DemandedElts, ForSigned,
4712 Depth);
4713}
4714
4716 SDValue Op, const APInt &DemandedElts, bool ForSigned,
4717 unsigned Depth) const {
4718 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
4719 ConstantRange CR1 = ConstantRange::fromKnownBits(Known, ForSigned);
4720 ConstantRange CR2 = computeConstantRange(Op, DemandedElts, ForSigned, Depth);
4723 return CR1.intersectWith(CR2, RangeType);
4724}
4725
4727 unsigned Depth) const {
4728 APInt DemandedElts = getDemandAllEltsMask(Val);
4729 return isKnownToBeAPowerOfTwo(Val, DemandedElts, OrZero, Depth);
4730}
4731
4733 const APInt &DemandedElts,
4734 bool OrZero, unsigned Depth) const {
4735 if (Depth >= MaxRecursionDepth)
4736 return false; // Limit search depth.
4737
4738 EVT OpVT = Val.getValueType();
4739 unsigned BitWidth = OpVT.getScalarSizeInBits();
4740 [[maybe_unused]] unsigned NumElts = DemandedElts.getBitWidth();
4741 assert((!OpVT.isScalableVector() || NumElts == 1) &&
4742 "DemandedElts for scalable vectors must be 1 to represent all lanes");
4743 assert(
4744 (!OpVT.isFixedLengthVector() || NumElts == OpVT.getVectorNumElements()) &&
4745 "Unexpected vector size");
4746
4747 auto IsPowerOfTwoOrZero = [BitWidth, OrZero](const ConstantSDNode *C) {
4748 APInt V = C->getAPIntValue().zextOrTrunc(BitWidth);
4749 return (OrZero && V.isZero()) || V.isPowerOf2();
4750 };
4751
4752 // Is the constant a known power of 2 or zero?
4753 if (ISD::matchUnaryPredicate(Val, IsPowerOfTwoOrZero))
4754 return true;
4755
4756 switch (Val.getOpcode()) {
4757 case ISD::BUILD_VECTOR:
4758 // Are all operands of a build vector constant powers of two or zero?
4759 if (all_of(enumerate(Val->ops()), [&](auto P) {
4760 auto *C = dyn_cast<ConstantSDNode>(P.value());
4761 return !DemandedElts[P.index()] || (C && IsPowerOfTwoOrZero(C));
4762 }))
4763 return true;
4764 break;
4765
4766 case ISD::SPLAT_VECTOR:
4767 // Is the operand of a splat vector a constant power of two?
4768 if (auto *C = dyn_cast<ConstantSDNode>(Val->getOperand(0)))
4769 if (IsPowerOfTwoOrZero(C))
4770 return true;
4771 break;
4772
4774 SDValue InVec = Val.getOperand(0);
4775 SDValue EltNo = Val.getOperand(1);
4776 EVT VecVT = InVec.getValueType();
4777
4778 // Skip scalable vectors or implicit extensions.
4779 if (VecVT.isScalableVector() ||
4780 OpVT.getScalarSizeInBits() != VecVT.getScalarSizeInBits())
4781 break;
4782
4783 // If we know the element index, just demand that vector element, else for
4784 // an unknown element index, ignore DemandedElts and demand them all.
4785 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4786 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4787 APInt DemandedSrcElts =
4788 ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts)
4789 ? APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue())
4790 : APInt::getAllOnes(NumSrcElts);
4791 return isKnownToBeAPowerOfTwo(InVec, DemandedSrcElts, OrZero, Depth + 1);
4792 }
4793
4794 case ISD::AND: {
4795 // Looking for `x & -x` pattern:
4796 // If x == 0:
4797 // x & -x -> 0
4798 // If x != 0:
4799 // x & -x -> non-zero pow2
4800 // so if we find the pattern return whether we know `x` is non-zero.
4801 SDValue X, Z;
4802 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))) ||
4803 (sd_match(Val, m_And(m_Value(X), m_Sub(m_Value(Z), m_Deferred(X)))) &&
4804 MaskedVectorIsZero(Z, DemandedElts, Depth + 1)))
4805 return OrZero || isKnownNeverZero(X, DemandedElts, Depth);
4806 break;
4807 }
4808
4809 case ISD::SHL: {
4810 // A left-shift of a constant one will have exactly one bit set because
4811 // shifting the bit off the end is undefined.
4812 auto *C = isConstOrConstSplat(Val.getOperand(0), DemandedElts);
4813 if (C && C->getAPIntValue() == 1)
4814 return true;
4815 return (OrZero || isKnownNeverZero(Val, DemandedElts, Depth)) &&
4816 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4817 Depth + 1);
4818 }
4819
4820 case ISD::SRL: {
4821 // A logical right-shift of a constant sign-bit will have exactly
4822 // one bit set.
4823 auto *C = isConstOrConstSplat(Val.getOperand(0), DemandedElts);
4824 if (C && C->getAPIntValue().isSignMask())
4825 return true;
4826 return (OrZero || isKnownNeverZero(Val, DemandedElts, Depth)) &&
4827 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4828 Depth + 1);
4829 }
4830
4831 case ISD::TRUNCATE:
4832 return (OrZero || isKnownNeverZero(Val, DemandedElts, Depth)) &&
4833 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4834 Depth + 1);
4835
4836 case ISD::ROTL:
4837 case ISD::ROTR:
4838 return isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4839 Depth + 1);
4840 case ISD::BSWAP:
4841 case ISD::BITREVERSE:
4842 return isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4843 Depth + 1);
4844
4845 case ISD::SMIN:
4846 case ISD::SMAX:
4847 case ISD::UMIN:
4848 case ISD::UMAX:
4849 return isKnownToBeAPowerOfTwo(Val.getOperand(1), DemandedElts, OrZero,
4850 Depth + 1) &&
4851 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4852 Depth + 1);
4853
4854 case ISD::SELECT:
4855 case ISD::VSELECT:
4856 return isKnownToBeAPowerOfTwo(Val.getOperand(2), DemandedElts, OrZero,
4857 Depth + 1) &&
4858 isKnownToBeAPowerOfTwo(Val.getOperand(1), DemandedElts, OrZero,
4859 Depth + 1);
4860
4861 case ISD::ZERO_EXTEND:
4862 return isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
4863 Depth + 1);
4864
4865 case ISD::VSCALE:
4866 // vscale(power-of-two) is a power-of-two
4867 return isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
4868 Depth + 1);
4869
4870 case ISD::VECTOR_SHUFFLE: {
4872 // Demanded elements with undef shuffle mask elements are unknown
4873 // - we cannot guarantee they are a power of two, so return false.
4874 APInt DemandedLHS, DemandedRHS;
4876 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4877 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4878 DemandedLHS, DemandedRHS))
4879 return false;
4880
4881 // All demanded elements from LHS must be known power of two.
4882 if (!!DemandedLHS && !isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedLHS,
4883 OrZero, Depth + 1))
4884 return false;
4885
4886 // All demanded elements from RHS must be known power of two.
4887 if (!!DemandedRHS && !isKnownToBeAPowerOfTwo(Val.getOperand(1), DemandedRHS,
4888 OrZero, Depth + 1))
4889 return false;
4890
4891 return true;
4892 }
4893 }
4894
4895 // More could be done here, though the above checks are enough
4896 // to handle some common cases.
4897 return false;
4898}
4899
4901 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4902 return C1->getValueAPF().getExactLog2Abs() >= 0;
4903
4904 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4905 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4906
4907 return false;
4908}
4909
4911 APInt DemandedElts = getDemandAllEltsMask(Op);
4912 return ComputeNumSignBits(Op, DemandedElts, Depth);
4913}
4914
4915unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4916 unsigned Depth) const {
4917 EVT VT = Op.getValueType();
4918 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4919 unsigned VTBits = VT.getScalarSizeInBits();
4920 unsigned NumElts = DemandedElts.getBitWidth();
4921 unsigned Tmp, Tmp2;
4922 unsigned FirstAnswer = 1;
4923
4924 assert((!VT.isScalableVector() || NumElts == 1) &&
4925 "DemandedElts for scalable vectors must be 1 to represent all lanes");
4926
4927 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4928 const APInt &Val = C->getAPIntValue();
4929 return Val.getNumSignBits();
4930 }
4931
4932 if (Depth >= MaxRecursionDepth)
4933 return 1; // Limit search depth.
4934
4935 if (!DemandedElts)
4936 return 1; // No demanded elts, better to assume we don't know anything.
4937
4938 unsigned Opcode = Op.getOpcode();
4939 switch (Opcode) {
4940 default: break;
4941 case ISD::AssertSext:
4942 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4943 return VTBits-Tmp+1;
4944 case ISD::AssertZext:
4945 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4946 return VTBits-Tmp;
4947 case ISD::FREEZE:
4948 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4950 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4951 break;
4952 case ISD::MERGE_VALUES:
4953 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4954 Depth + 1);
4955 case ISD::SPLAT_VECTOR: {
4956 // Check if the sign bits of source go down as far as the truncated value.
4957 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4958 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4959 if (NumSrcSignBits > (NumSrcBits - VTBits))
4960 return NumSrcSignBits - (NumSrcBits - VTBits);
4961 break;
4962 }
4963 case ISD::BUILD_VECTOR:
4964 assert(!VT.isScalableVector());
4965 Tmp = VTBits;
4966 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4967 if (!DemandedElts[i])
4968 continue;
4969
4970 SDValue SrcOp = Op.getOperand(i);
4971 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4972 // for constant nodes to ensure we only look at the sign bits.
4974 APInt T = C->getAPIntValue().trunc(VTBits);
4975 Tmp2 = T.getNumSignBits();
4976 } else {
4977 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4978
4979 if (SrcOp.getValueSizeInBits() != VTBits) {
4980 assert(SrcOp.getValueSizeInBits() > VTBits &&
4981 "Expected BUILD_VECTOR implicit truncation");
4982 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4983 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4984 }
4985 }
4986 Tmp = std::min(Tmp, Tmp2);
4987 }
4988 return Tmp;
4989
4990 case ISD::VECTOR_COMPRESS: {
4991 SDValue Vec = Op.getOperand(0);
4992 SDValue PassThru = Op.getOperand(2);
4993 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4994 if (Tmp == 1)
4995 return 1;
4996 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
4997 Tmp = std::min(Tmp, Tmp2);
4998 return Tmp;
4999 }
5000
5001 case ISD::VECTOR_SHUFFLE: {
5002 // Collect the minimum number of sign bits that are shared by every vector
5003 // element referenced by the shuffle.
5004 APInt DemandedLHS, DemandedRHS;
5006 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
5007 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
5008 DemandedLHS, DemandedRHS))
5009 return 1;
5010
5011 Tmp = std::numeric_limits<unsigned>::max();
5012 if (!!DemandedLHS)
5013 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
5014 if (!!DemandedRHS) {
5015 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
5016 Tmp = std::min(Tmp, Tmp2);
5017 }
5018 // If we don't know anything, early out and try computeKnownBits fall-back.
5019 if (Tmp == 1)
5020 break;
5021 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5022 return Tmp;
5023 }
5024
5025 case ISD::BITCAST: {
5026 if (VT.isScalableVector())
5027 break;
5028 SDValue N0 = Op.getOperand(0);
5029 EVT SrcVT = N0.getValueType();
5030 unsigned SrcBits = SrcVT.getScalarSizeInBits();
5031
5032 // Ignore bitcasts from unsupported types..
5033 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
5034 break;
5035
5036 // Fast handling of 'identity' bitcasts.
5037 if (VTBits == SrcBits)
5038 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
5039
5040 bool IsLE = getDataLayout().isLittleEndian();
5041
5042 // Bitcast 'large element' scalar/vector to 'small element' vector.
5043 if ((SrcBits % VTBits) == 0) {
5044 assert(VT.isVector() && "Expected bitcast to vector");
5045
5046 unsigned Scale = SrcBits / VTBits;
5047 APInt SrcDemandedElts =
5048 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
5049
5050 // Fast case - sign splat can be simply split across the small elements.
5051 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
5052 if (Tmp == SrcBits)
5053 return VTBits;
5054
5055 // Slow case - determine how far the sign extends into each sub-element.
5056 Tmp2 = VTBits;
5057 for (unsigned i = 0; i != NumElts; ++i)
5058 if (DemandedElts[i]) {
5059 unsigned SubOffset = i % Scale;
5060 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
5061 SubOffset = SubOffset * VTBits;
5062 if (Tmp <= SubOffset)
5063 return 1;
5064 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
5065 }
5066 return Tmp2;
5067 }
5068 break;
5069 }
5070
5072 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
5073 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
5074 return VTBits - Tmp + 1;
5075 case ISD::SIGN_EXTEND:
5076 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
5077 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
5079 // Max of the input and what this extends.
5080 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
5081 Tmp = VTBits-Tmp+1;
5082 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
5083 return std::max(Tmp, Tmp2);
5085 if (VT.isScalableVector())
5086 break;
5087 SDValue Src = Op.getOperand(0);
5088 EVT SrcVT = Src.getValueType();
5089 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
5090 Tmp = VTBits - SrcVT.getScalarSizeInBits();
5091 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
5092 }
5093 case ISD::SRA:
5094 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5095 // SRA X, C -> adds C sign bits.
5096 if (std::optional<unsigned> ShAmt =
5097 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
5098 Tmp = std::min(Tmp + *ShAmt, VTBits);
5099 return Tmp;
5100 case ISD::SHL:
5101 if (std::optional<ConstantRange> ShAmtRange =
5102 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
5103 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
5104 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
5105 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
5106 // shifted out, then we can compute the number of sign bits for the
5107 // operand being extended. A future improvement could be to pass along the
5108 // "shifted left by" information in the recursive calls to
5109 // ComputeKnownSignBits. Allowing us to handle this more generically.
5110 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
5111 SDValue Ext = Op.getOperand(0);
5112 EVT ExtVT = Ext.getValueType();
5113 SDValue Extendee = Ext.getOperand(0);
5114 EVT ExtendeeVT = Extendee.getValueType();
5115 unsigned SizeDifference =
5116 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
5117 if (SizeDifference <= MinShAmt) {
5118 Tmp = SizeDifference +
5119 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
5120 if (MaxShAmt < Tmp)
5121 return Tmp - MaxShAmt;
5122 }
5123 }
5124 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
5125 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5126 if (MaxShAmt < Tmp)
5127 return Tmp - MaxShAmt;
5128 }
5129 break;
5130 case ISD::AND:
5131 case ISD::OR:
5132 case ISD::XOR: // NOT is handled here.
5133 // Logical binary ops preserve the number of sign bits at the worst.
5134 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
5135 if (Tmp != 1) {
5136 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
5137 FirstAnswer = std::min(Tmp, Tmp2);
5138 // We computed what we know about the sign bits as our first
5139 // answer. Now proceed to the generic code that uses
5140 // computeKnownBits, and pick whichever answer is better.
5141 }
5142 break;
5143
5144 case ISD::SELECT:
5145 case ISD::VSELECT:
5146 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
5147 if (Tmp == 1) return 1; // Early out.
5148 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
5149 return std::min(Tmp, Tmp2);
5150 case ISD::SELECT_CC:
5151 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
5152 if (Tmp == 1) return 1; // Early out.
5153 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
5154 return std::min(Tmp, Tmp2);
5155
5156 case ISD::SMIN:
5157 case ISD::SMAX: {
5158 // If we have a clamp pattern, we know that the number of sign bits will be
5159 // the minimum of the clamp min/max range.
5160 bool IsMax = (Opcode == ISD::SMAX);
5161 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
5162 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
5163 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
5164 CstHigh =
5165 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
5166 if (CstLow && CstHigh) {
5167 if (!IsMax)
5168 std::swap(CstLow, CstHigh);
5169 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
5170 Tmp = CstLow->getAPIntValue().getNumSignBits();
5171 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
5172 return std::min(Tmp, Tmp2);
5173 }
5174 }
5175
5176 // Fallback - just get the minimum number of sign bits of the operands.
5177 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5178 if (Tmp == 1)
5179 return 1; // Early out.
5180 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5181 return std::min(Tmp, Tmp2);
5182 }
5183 case ISD::UMIN:
5184 case ISD::UMAX:
5185 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5186 if (Tmp == 1)
5187 return 1; // Early out.
5188 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5189 return std::min(Tmp, Tmp2);
5190 case ISD::SSUBO_CARRY:
5191 case ISD::USUBO_CARRY:
5192 // sub_carry(x,x,c) -> 0/-1 (sext carry)
5193 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
5194 return VTBits;
5195 [[fallthrough]];
5196 case ISD::SADDO:
5197 case ISD::UADDO:
5198 case ISD::SADDO_CARRY:
5199 case ISD::UADDO_CARRY:
5200 case ISD::SSUBO:
5201 case ISD::USUBO:
5202 case ISD::SMULO:
5203 case ISD::UMULO:
5204 if (Op.getResNo() != 1)
5205 break;
5206 // The boolean result conforms to getBooleanContents. Fall through.
5207 // If setcc returns 0/-1, all bits are sign bits.
5208 // We know that we have an integer-based boolean since these operations
5209 // are only available for integer.
5210 if (TLI->getBooleanContents(VT.isVector(), false) ==
5212 return VTBits;
5213 break;
5214 case ISD::SETCC:
5215 case ISD::SETCCCARRY:
5216 case ISD::STRICT_FSETCC:
5217 case ISD::STRICT_FSETCCS: {
5218 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
5219 // If setcc returns 0/-1, all bits are sign bits.
5220 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
5222 return VTBits;
5223 break;
5224 }
5225 case ISD::ROTL:
5226 case ISD::ROTR:
5227 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5228
5229 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
5230 if (Tmp == VTBits)
5231 return VTBits;
5232
5233 if (ConstantSDNode *C =
5234 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
5235 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
5236
5237 // Handle rotate right by N like a rotate left by 32-N.
5238 if (Opcode == ISD::ROTR)
5239 RotAmt = (VTBits - RotAmt) % VTBits;
5240
5241 // If we aren't rotating out all of the known-in sign bits, return the
5242 // number that are left. This handles rotl(sext(x), 1) for example.
5243 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
5244 }
5245 break;
5246 case ISD::ADD:
5247 case ISD::ADDC:
5248 // TODO: Move Operand 1 check before Operand 0 check
5249 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5250 if (Tmp == 1) return 1; // Early out.
5251
5252 // Special case decrementing a value (ADD X, -1):
5253 if (ConstantSDNode *CRHS =
5254 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5255 if (CRHS->isAllOnes()) {
5256 KnownBits Known =
5257 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5258
5259 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5260 // sign bits set.
5261 if ((Known.Zero | 1).isAllOnes())
5262 return VTBits;
5263
5264 // If we are subtracting one from a positive number, there is no carry
5265 // out of the result.
5266 if (Known.isNonNegative())
5267 return Tmp;
5268 }
5269
5270 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5271 if (Tmp2 == 1) return 1; // Early out.
5272
5273 // Add can have at most one carry bit. Thus we know that the output
5274 // is, at worst, one more bit than the inputs.
5275 return std::min(Tmp, Tmp2) - 1;
5276 case ISD::SUB:
5277 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5278 if (Tmp2 == 1) return 1; // Early out.
5279
5280 // Handle NEG.
5281 if (ConstantSDNode *CLHS =
5282 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5283 if (CLHS->isZero()) {
5284 KnownBits Known =
5285 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5286 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5287 // sign bits set.
5288 if ((Known.Zero | 1).isAllOnes())
5289 return VTBits;
5290
5291 // If the input is known to be positive (the sign bit is known clear),
5292 // the output of the NEG has the same number of sign bits as the input.
5293 if (Known.isNonNegative())
5294 return Tmp2;
5295
5296 // Otherwise, we treat this like a SUB.
5297 }
5298
5299 // Sub can have at most one carry bit. Thus we know that the output
5300 // is, at worst, one more bit than the inputs.
5301 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5302 if (Tmp == 1) return 1; // Early out.
5303 return std::min(Tmp, Tmp2) - 1;
5304 case ISD::MUL: {
5305 // The output of the Mul can be at most twice the valid bits in the inputs.
5306 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5307 if (SignBitsOp0 == 1)
5308 break;
5309 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5310 if (SignBitsOp1 == 1)
5311 break;
5312 unsigned OutValidBits =
5313 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5314 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5315 }
5316 case ISD::AVGCEILS:
5317 case ISD::AVGFLOORS:
5318 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5319 if (Tmp == 1)
5320 return 1; // Early out.
5321 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5322 return std::min(Tmp, Tmp2);
5323 case ISD::SREM:
5324 // The sign bit is the LHS's sign bit, except when the result of the
5325 // remainder is zero. The magnitude of the result should be less than or
5326 // equal to the magnitude of the LHS. Therefore, the result should have
5327 // at least as many sign bits as the left hand side.
5328 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5329 case ISD::TRUNCATE: {
5330 // Check if the sign bits of source go down as far as the truncated value.
5331 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5332 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5333 if (NumSrcSignBits > (NumSrcBits - VTBits))
5334 return NumSrcSignBits - (NumSrcBits - VTBits);
5335 break;
5336 }
5337 case ISD::EXTRACT_ELEMENT: {
5338 if (VT.isScalableVector())
5339 break;
5340 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5341 const int BitWidth = Op.getValueSizeInBits();
5342 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5343
5344 // Get reverse index (starting from 1), Op1 value indexes elements from
5345 // little end. Sign starts at big end.
5346 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5347
5348 // If the sign portion ends in our element the subtraction gives correct
5349 // result. Otherwise it gives either negative or > bitwidth result
5350 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5351 }
5353 if (VT.isScalableVector())
5354 break;
5355 // If we know the element index, split the demand between the
5356 // source vector and the inserted element, otherwise assume we need
5357 // the original demanded vector elements and the value.
5358 SDValue InVec = Op.getOperand(0);
5359 SDValue InVal = Op.getOperand(1);
5360 SDValue EltNo = Op.getOperand(2);
5361 bool DemandedVal = true;
5362 APInt DemandedVecElts = DemandedElts;
5363 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5364 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5365 unsigned EltIdx = CEltNo->getZExtValue();
5366 DemandedVal = !!DemandedElts[EltIdx];
5367 DemandedVecElts.clearBit(EltIdx);
5368 }
5369 Tmp = std::numeric_limits<unsigned>::max();
5370 if (DemandedVal) {
5371 // TODO - handle implicit truncation of inserted elements.
5372 if (InVal.getScalarValueSizeInBits() != VTBits)
5373 break;
5374 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5375 Tmp = std::min(Tmp, Tmp2);
5376 }
5377 if (!!DemandedVecElts) {
5378 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5379 Tmp = std::min(Tmp, Tmp2);
5380 }
5381 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5382 return Tmp;
5383 }
5385 SDValue InVec = Op.getOperand(0);
5386 SDValue EltNo = Op.getOperand(1);
5387 EVT VecVT = InVec.getValueType();
5388 // ComputeNumSignBits not yet implemented for scalable vectors.
5389 if (VecVT.isScalableVector())
5390 break;
5391 const unsigned BitWidth = Op.getValueSizeInBits();
5392 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5393 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5394
5395 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5396 // anything about sign bits. But if the sizes match we can derive knowledge
5397 // about sign bits from the vector operand.
5398 if (BitWidth != EltBitWidth)
5399 break;
5400
5401 // If we know the element index, just demand that vector element, else for
5402 // an unknown element index, ignore DemandedElts and demand them all.
5403 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5404 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5405 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5406 DemandedSrcElts =
5407 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5408
5409 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5410 }
5412 // Offset the demanded elts by the subvector index.
5413 SDValue Src = Op.getOperand(0);
5414
5415 APInt DemandedSrcElts;
5416 if (Src.getValueType().isScalableVector())
5417 DemandedSrcElts = APInt(1, 1);
5418 else {
5419 uint64_t Idx = Op.getConstantOperandVal(1);
5420 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5421 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5422 }
5423 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5424 }
5425 case ISD::CONCAT_VECTORS: {
5426 if (VT.isScalableVector())
5427 break;
5428 // Determine the minimum number of sign bits across all demanded
5429 // elts of the input vectors. Early out if the result is already 1.
5430 Tmp = std::numeric_limits<unsigned>::max();
5431 EVT SubVectorVT = Op.getOperand(0).getValueType();
5432 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5433 unsigned NumSubVectors = Op.getNumOperands();
5434 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5435 APInt DemandedSub =
5436 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5437 if (!DemandedSub)
5438 continue;
5439 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5440 Tmp = std::min(Tmp, Tmp2);
5441 }
5442 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5443 return Tmp;
5444 }
5445 case ISD::INSERT_SUBVECTOR: {
5446 if (VT.isScalableVector())
5447 break;
5448 // Demand any elements from the subvector and the remainder from the src its
5449 // inserted into.
5450 SDValue Src = Op.getOperand(0);
5451 SDValue Sub = Op.getOperand(1);
5452 uint64_t Idx = Op.getConstantOperandVal(2);
5453 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5454 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5455 APInt DemandedSrcElts = DemandedElts;
5456 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5457
5458 Tmp = std::numeric_limits<unsigned>::max();
5459 if (!!DemandedSubElts) {
5460 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5461 if (Tmp == 1)
5462 return 1; // early-out
5463 }
5464 if (!!DemandedSrcElts) {
5465 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5466 Tmp = std::min(Tmp, Tmp2);
5467 }
5468 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5469 return Tmp;
5470 }
5471 case ISD::LOAD: {
5472 // If we are looking at the loaded value of the SDNode.
5473 if (Op.getResNo() != 0)
5474 break;
5475
5477 if (const MDNode *Ranges = LD->getRanges()) {
5478 if (DemandedElts != 1)
5479 break;
5480
5482 if (VTBits > CR.getBitWidth()) {
5483 switch (LD->getExtensionType()) {
5484 case ISD::SEXTLOAD:
5485 CR = CR.signExtend(VTBits);
5486 break;
5487 case ISD::ZEXTLOAD:
5488 CR = CR.zeroExtend(VTBits);
5489 break;
5490 default:
5491 break;
5492 }
5493 }
5494
5495 if (VTBits != CR.getBitWidth())
5496 break;
5497 return std::min(CR.getSignedMin().getNumSignBits(),
5499 }
5500
5501 unsigned ExtType = LD->getExtensionType();
5502 switch (ExtType) {
5503 default:
5504 break;
5505 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5506 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5507 return VTBits - Tmp + 1;
5508 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5509 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5510 return VTBits - Tmp;
5511 case ISD::NON_EXTLOAD:
5512 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5513 // We only need to handle vectors - computeKnownBits should handle
5514 // scalar cases.
5515 Type *CstTy = Cst->getType();
5516 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5517 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5518 VTBits == CstTy->getScalarSizeInBits()) {
5519 Tmp = VTBits;
5520 for (unsigned i = 0; i != NumElts; ++i) {
5521 if (!DemandedElts[i])
5522 continue;
5523 if (Constant *Elt = Cst->getAggregateElement(i)) {
5524 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5525 const APInt &Value = CInt->getValue();
5526 Tmp = std::min(Tmp, Value.getNumSignBits());
5527 continue;
5528 }
5529 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5530 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5531 Tmp = std::min(Tmp, Value.getNumSignBits());
5532 continue;
5533 }
5534 }
5535 // Unknown type. Conservatively assume no bits match sign bit.
5536 return 1;
5537 }
5538 return Tmp;
5539 }
5540 }
5541 break;
5542 }
5543
5544 break;
5545 }
5548 case ISD::ATOMIC_SWAP:
5560 case ISD::ATOMIC_LOAD: {
5561 auto *AT = cast<AtomicSDNode>(Op);
5562 // If we are looking at the loaded value.
5563 if (Op.getResNo() == 0) {
5564 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5565 if (Tmp == VTBits)
5566 return 1; // early-out
5567
5568 // For atomic_load, prefer to use the extension type.
5569 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5570 switch (AT->getExtensionType()) {
5571 default:
5572 break;
5573 case ISD::SEXTLOAD:
5574 return VTBits - Tmp + 1;
5575 case ISD::ZEXTLOAD:
5576 return VTBits - Tmp;
5577 }
5578 }
5579
5580 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5581 return VTBits - Tmp + 1;
5582 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5583 return VTBits - Tmp;
5584 }
5585 break;
5586 }
5587 }
5588
5589 // Allow the target to implement this method for its nodes.
5590 if (Opcode >= ISD::BUILTIN_OP_END ||
5591 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5592 Opcode == ISD::INTRINSIC_W_CHAIN ||
5593 Opcode == ISD::INTRINSIC_VOID) {
5594 // TODO: This can probably be removed once target code is audited. This
5595 // is here purely to reduce patch size and review complexity.
5596 if (!VT.isScalableVector()) {
5597 unsigned NumBits =
5598 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5599 if (NumBits > 1)
5600 FirstAnswer = std::max(FirstAnswer, NumBits);
5601 }
5602 }
5603
5604 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5605 // use this information.
5606 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5607 return std::max(FirstAnswer, Known.countMinSignBits());
5608}
5609
5611 unsigned Depth) const {
5612 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5613 return Op.getScalarValueSizeInBits() - SignBits + 1;
5614}
5615
5617 const APInt &DemandedElts,
5618 unsigned Depth) const {
5619 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5620 return Op.getScalarValueSizeInBits() - SignBits + 1;
5621}
5622
5624 UndefPoisonKind Kind,
5625 unsigned Depth) const {
5626 // Early out for FREEZE.
5627 if (Op.getOpcode() == ISD::FREEZE)
5628 return true;
5629
5630 APInt DemandedElts = getDemandAllEltsMask(Op);
5631 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, Kind, Depth);
5632}
5633
5635 const APInt &DemandedElts,
5636 UndefPoisonKind Kind,
5637 unsigned Depth) const {
5638 unsigned Opcode = Op.getOpcode();
5639
5640 // Early out for FREEZE.
5641 if (Opcode == ISD::FREEZE)
5642 return true;
5643
5644 if (Depth >= MaxRecursionDepth)
5645 return false; // Limit search depth.
5646
5647 if (isIntOrFPConstant(Op))
5648 return true;
5649
5650 switch (Opcode) {
5651 case ISD::CONDCODE:
5652 case ISD::VALUETYPE:
5653 case ISD::FrameIndex:
5655 case ISD::CopyFromReg:
5656 return true;
5657
5658 case ISD::POISON:
5659 return !includesPoison(Kind);
5660
5661 case ISD::UNDEF:
5662 return !includesUndef(Kind);
5663
5664 case ISD::BITCAST: {
5665 SDValue Src = Op.getOperand(0);
5666 EVT SrcVT = Src.getValueType();
5667 EVT DstVT = Op.getValueType();
5668
5669 if (!SrcVT.isVector() || !DstVT.isVector())
5670 return isGuaranteedNotToBeUndefOrPoison(Src, Kind, Depth + 1);
5671
5672 unsigned SrcEltBits = SrcVT.getScalarSizeInBits();
5673 unsigned DstEltBits = DstVT.getScalarSizeInBits();
5674 ElementCount NumSrcElts = SrcVT.getVectorElementCount();
5675 [[maybe_unused]] ElementCount NumDstElts = DstVT.getVectorElementCount();
5676
5677 if (SrcEltBits == DstEltBits)
5678 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedElts, Kind,
5679 Depth + 1);
5680
5681 if (SrcEltBits < DstEltBits) {
5682 if (DstEltBits % SrcEltBits != 0)
5683 return isGuaranteedNotToBeUndefOrPoison(Src, Kind, Depth + 1);
5684
5685 assert(NumSrcElts == NumDstElts * (DstEltBits / SrcEltBits) &&
5686 "Unexpected vector bitcast");
5687 APInt DemandedSrcElts =
5688 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts.getKnownMinValue());
5689 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, Kind,
5690 Depth + 1);
5691 }
5692
5693 if (SrcEltBits % DstEltBits != 0)
5694 return isGuaranteedNotToBeUndefOrPoison(Src, Kind, Depth + 1);
5695
5696 assert(NumDstElts == NumSrcElts * (SrcEltBits / DstEltBits) &&
5697 "Unexpected vector bitcast");
5698 APInt DemandedSrcElts =
5699 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts.getKnownMinValue());
5700 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, Kind,
5701 Depth + 1);
5702 }
5703
5704 case ISD::BUILD_VECTOR:
5705 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5706 // this shouldn't affect the result.
5707 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5708 if (!DemandedElts[i])
5709 continue;
5710 if (!isGuaranteedNotToBeUndefOrPoison(Op.getOperand(i), Kind, Depth + 1))
5711 return false;
5712 }
5713 return true;
5714
5715 case ISD::CONCAT_VECTORS: {
5716 EVT VT = Op.getValueType();
5717 if (!VT.isFixedLengthVector())
5718 break;
5719
5720 EVT SubVT = Op.getOperand(0).getValueType();
5721 unsigned NumSubElts = SubVT.getVectorNumElements();
5722 for (unsigned I = 0, E = Op.getNumOperands(); I != E; ++I) {
5723 APInt DemandedSubElts =
5724 DemandedElts.extractBits(NumSubElts, I * NumSubElts);
5725 if (!!DemandedSubElts &&
5726 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(I), DemandedSubElts,
5727 Kind, Depth + 1))
5728 return false;
5729 }
5730 return true;
5731 }
5732
5734 SDValue Src = Op.getOperand(0);
5735 if (Src.getValueType().isScalableVector())
5736 break;
5737 uint64_t Idx = Op.getConstantOperandVal(1);
5738 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5739 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5740 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, Kind,
5741 Depth + 1);
5742 }
5743
5744 case ISD::INSERT_SUBVECTOR: {
5745 if (Op.getValueType().isScalableVector())
5746 break;
5747 SDValue Src = Op.getOperand(0);
5748 SDValue Sub = Op.getOperand(1);
5749 uint64_t Idx = Op.getConstantOperandVal(2);
5750 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5751 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5752 APInt DemandedSrcElts = DemandedElts;
5753 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5754
5755 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5756 Sub, DemandedSubElts, Kind, Depth + 1))
5757 return false;
5758 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5759 Src, DemandedSrcElts, Kind, Depth + 1))
5760 return false;
5761 return true;
5762 }
5763
5765 SDValue Src = Op.getOperand(0);
5766 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5767 EVT SrcVT = Src.getValueType();
5768 if (SrcVT.isFixedLengthVector() && IndexC &&
5769 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5770 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5771 IndexC->getZExtValue());
5772 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, Kind,
5773 Depth + 1);
5774 }
5775 break;
5776 }
5777
5779 SDValue InVec = Op.getOperand(0);
5780 SDValue InVal = Op.getOperand(1);
5781 SDValue EltNo = Op.getOperand(2);
5782 EVT VT = InVec.getValueType();
5783 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5784 if (IndexC && VT.isFixedLengthVector() &&
5785 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5786 if (DemandedElts[IndexC->getZExtValue()] &&
5787 !isGuaranteedNotToBeUndefOrPoison(InVal, Kind, Depth + 1))
5788 return false;
5789 APInt InVecDemandedElts = DemandedElts;
5790 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5791 if (!!InVecDemandedElts &&
5793 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5794 InVecDemandedElts, Kind, Depth + 1))
5795 return false;
5796 return true;
5797 }
5798 break;
5799 }
5800
5802 // Check upper (known undef) elements.
5803 if (DemandedElts.ugt(1) && includesUndef(Kind))
5804 return false;
5805 // Check element zero.
5806 if (DemandedElts[0] &&
5807 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), Kind, Depth + 1))
5808 return false;
5809 return true;
5810
5811 case ISD::SPLAT_VECTOR:
5812 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), Kind, Depth + 1);
5813
5814 case ISD::SELECT: {
5815 return !canCreateUndefOrPoison(Op, DemandedElts, Kind,
5816 /*ConsiderFlags*/ true, Depth) &&
5817 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), Kind,
5818 Depth + 1) &&
5819 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedElts,
5820 Kind, Depth + 1) &&
5821 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(2), DemandedElts,
5822 Kind, Depth + 1);
5823 }
5824
5825 case ISD::VECTOR_SHUFFLE: {
5826 APInt DemandedLHS, DemandedRHS;
5827 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5828 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5829 DemandedElts, DemandedLHS, DemandedRHS,
5830 /*AllowUndefElts=*/false))
5831 return false;
5832 if (!DemandedLHS.isZero() &&
5833 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS, Kind,
5834 Depth + 1))
5835 return false;
5836 if (!DemandedRHS.isZero() &&
5837 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS, Kind,
5838 Depth + 1))
5839 return false;
5840 return true;
5841 }
5842
5843 case ISD::SHL:
5844 case ISD::SRL:
5845 case ISD::SRA:
5846 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5847 // enough to check operand 0 if Op can't create undef/poison.
5848 return !canCreateUndefOrPoison(Op, DemandedElts, Kind,
5849 /*ConsiderFlags*/ true, Depth) &&
5850 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5851 Kind, Depth + 1);
5852
5853 case ISD::BSWAP:
5854 case ISD::CTPOP:
5855 case ISD::BITREVERSE:
5856 case ISD::AND:
5857 case ISD::OR:
5858 case ISD::XOR:
5859 case ISD::ADD:
5860 case ISD::SUB:
5861 case ISD::MUL:
5862 case ISD::SADDSAT:
5863 case ISD::UADDSAT:
5864 case ISD::SSUBSAT:
5865 case ISD::USUBSAT:
5866 case ISD::SSHLSAT:
5867 case ISD::USHLSAT:
5868 case ISD::SMIN:
5869 case ISD::SMAX:
5870 case ISD::UMIN:
5871 case ISD::UMAX:
5872 case ISD::ZERO_EXTEND:
5873 case ISD::SIGN_EXTEND:
5874 case ISD::ANY_EXTEND:
5875 case ISD::TRUNCATE:
5876 case ISD::VSELECT: {
5877 // If Op can't create undef/poison and none of its operands are undef/poison
5878 // then Op is never undef/poison. A difference from the more common check
5879 // below, outside the switch, is that we handle elementwise operations for
5880 // which the DemandedElts mask is valid for all operands here.
5881 return !canCreateUndefOrPoison(Op, DemandedElts, Kind,
5882 /*ConsiderFlags*/ true, Depth) &&
5883 all_of(Op->ops(), [&](SDValue V) {
5884 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts, Kind,
5885 Depth + 1);
5886 });
5887 }
5888
5889 // TODO: Search for noundef attributes from library functions.
5890
5891 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5892
5893 default:
5894 // Allow the target to implement this method for its nodes.
5895 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5896 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5897 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5898 Op, DemandedElts, *this, Kind, Depth);
5899 break;
5900 }
5901
5902 // If Op can't create undef/poison and none of its operands are undef/poison
5903 // then Op is never undef/poison.
5904 // NOTE: TargetNodes can handle this in themselves in
5905 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5906 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5907 return !canCreateUndefOrPoison(Op, Kind, /*ConsiderFlags*/ true, Depth) &&
5908 all_of(Op->ops(), [&](SDValue V) {
5909 return isGuaranteedNotToBeUndefOrPoison(V, Kind, Depth + 1);
5910 });
5911}
5912
5914 bool ConsiderFlags,
5915 unsigned Depth) const {
5916 APInt DemandedElts = getDemandAllEltsMask(Op);
5917 return canCreateUndefOrPoison(Op, DemandedElts, Kind, ConsiderFlags, Depth);
5918}
5919
5921 UndefPoisonKind Kind,
5922 bool ConsiderFlags,
5923 unsigned Depth) const {
5924 if (ConsiderFlags && includesPoison(Kind) && Op->hasPoisonGeneratingFlags())
5925 return true;
5926
5927 unsigned Opcode = Op.getOpcode();
5928 switch (Opcode) {
5929 case ISD::AssertSext:
5930 case ISD::AssertZext:
5931 case ISD::AssertAlign:
5933 // Assertion nodes can create poison if the assertion fails.
5934 return includesPoison(Kind);
5935
5936 case ISD::FREEZE:
5940 case ISD::SADDSAT:
5941 case ISD::UADDSAT:
5942 case ISD::SSUBSAT:
5943 case ISD::USUBSAT:
5944 case ISD::MULHU:
5945 case ISD::MULHS:
5946 case ISD::AVGFLOORS:
5947 case ISD::AVGFLOORU:
5948 case ISD::AVGCEILS:
5949 case ISD::AVGCEILU:
5950 case ISD::ABDU:
5951 case ISD::ABDS:
5952 case ISD::SMIN:
5953 case ISD::SMAX:
5954 case ISD::SCMP:
5955 case ISD::UMIN:
5956 case ISD::UMAX:
5957 case ISD::UCMP:
5958 case ISD::AND:
5959 case ISD::XOR:
5960 case ISD::ROTL:
5961 case ISD::ROTR:
5962 case ISD::FSHL:
5963 case ISD::FSHR:
5964 case ISD::BSWAP:
5965 case ISD::CTTZ:
5966 case ISD::CTLZ:
5967 case ISD::CTLS:
5968 case ISD::CTPOP:
5969 case ISD::BITREVERSE:
5970 case ISD::PARITY:
5971 case ISD::SIGN_EXTEND:
5972 case ISD::TRUNCATE:
5976 case ISD::BITCAST:
5977 case ISD::BUILD_VECTOR:
5978 case ISD::BUILD_PAIR:
5979 case ISD::SPLAT_VECTOR:
5980 case ISD::FABS:
5981 case ISD::FCEIL:
5982 case ISD::FFLOOR:
5983 case ISD::FTRUNC:
5984 case ISD::FRINT:
5985 case ISD::FNEARBYINT:
5986 case ISD::FROUND:
5987 case ISD::FROUNDEVEN:
5988 return false;
5989
5990 case ISD::ABS:
5991 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5992 // Different to Intrinsic::abs.
5993 return false;
5995 // ABS_MIN_POISON may produce poison if the input is INT_MIN.
5996 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) <= 1;
5997
5998 case ISD::ADDC:
5999 case ISD::SUBC:
6000 case ISD::ADDE:
6001 case ISD::SUBE:
6002 case ISD::SADDO:
6003 case ISD::SSUBO:
6004 case ISD::SMULO:
6005 case ISD::SADDO_CARRY:
6006 case ISD::SSUBO_CARRY:
6007 case ISD::UADDO:
6008 case ISD::USUBO:
6009 case ISD::UMULO:
6010 case ISD::UADDO_CARRY:
6011 case ISD::USUBO_CARRY:
6012 // No poison on result or overflow flags.
6013 return false;
6014
6015 case ISD::SELECT_CC:
6016 case ISD::SETCC: {
6017 // Integer setcc cannot create undef or poison.
6018 if (Op.getOperand(0).getValueType().isInteger())
6019 return false;
6020
6021 // FP compares are more complicated. They can create poison for nan/infinity
6022 // based on options and flags. The options and flags also cause special
6023 // nonan condition codes to be used. Those condition codes may be preserved
6024 // even if the nonan flag is dropped somewhere.
6025 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
6026 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
6027 return (unsigned)CCCode & 0x10U;
6028 }
6029
6030 case ISD::OR:
6031 case ISD::ZERO_EXTEND:
6032 case ISD::SELECT:
6033 case ISD::VSELECT:
6034 case ISD::ADD:
6035 case ISD::SUB:
6036 case ISD::MUL:
6037 case ISD::FNEG:
6038 case ISD::FADD:
6039 case ISD::FSUB:
6040 case ISD::FMUL:
6041 case ISD::FDIV:
6042 case ISD::FREM:
6043 case ISD::FCOPYSIGN:
6044 case ISD::FMA:
6045 case ISD::FMAD:
6046 case ISD::FMULADD:
6047 case ISD::FP_EXTEND:
6048 case ISD::FMINNUM:
6049 case ISD::FMAXNUM:
6050 case ISD::FMINNUM_IEEE:
6051 case ISD::FMAXNUM_IEEE:
6052 case ISD::FMINIMUM:
6053 case ISD::FMAXIMUM:
6054 case ISD::FMINIMUMNUM:
6055 case ISD::FMAXIMUMNUM:
6061 // No poison except from flags (which is handled above)
6062 return false;
6063
6064 case ISD::SHL:
6065 case ISD::SRL:
6066 case ISD::SRA:
6067 // If the max shift amount isn't in range, then the shift can
6068 // create poison.
6069 return includesPoison(Kind) &&
6070 !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
6071
6074 // If the amount is zero then the result will be poison.
6075 // TODO: Add isKnownNeverZero DemandedElts handling.
6076 return includesPoison(Kind) &&
6077 !isKnownNeverZero(Op.getOperand(0), Depth + 1);
6078
6080 // Check if we demand any upper (undef) elements.
6081 return includesUndef(Kind) && DemandedElts.ugt(1);
6082
6085 // Ensure that the element index is in bounds.
6086 if (includesPoison(Kind)) {
6087 EVT VecVT = Op.getOperand(0).getValueType();
6088 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
6089 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
6090 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
6091 }
6092 return false;
6093 }
6094
6095 case ISD::VECTOR_SHUFFLE: {
6096 // Check for any demanded shuffle element that is undef.
6097 auto *SVN = cast<ShuffleVectorSDNode>(Op);
6098 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
6099 if (Elt < 0 && DemandedElts[Idx])
6100 return true;
6101 return false;
6102 }
6103
6105 return false;
6106
6107 default:
6108 // Allow the target to implement this method for its nodes.
6109 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6110 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
6111 return TLI->canCreateUndefOrPoisonForTargetNode(
6112 Op, DemandedElts, *this, Kind, ConsiderFlags, Depth);
6113 break;
6114 }
6115
6116 // Be conservative and return true.
6117 return true;
6118}
6119
6120bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
6121 unsigned Opcode = Op.getOpcode();
6122 if (Opcode == ISD::OR)
6123 return Op->getFlags().hasDisjoint() ||
6124 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
6125 if (Opcode == ISD::XOR)
6126 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
6127 return false;
6128}
6129
6131 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
6132 (Op.isAnyAdd() || isADDLike(Op));
6133}
6134
6136 FPClassTest InterestedClasses,
6137 unsigned Depth) const {
6138 APInt DemandedElts = getDemandAllEltsMask(Op);
6139 return computeKnownFPClass(Op, DemandedElts, InterestedClasses, Depth);
6140}
6141
6143 const APInt &DemandedElts,
6144 FPClassTest InterestedClasses,
6145 unsigned Depth) const {
6146 KnownFPClass Known;
6147
6148 if (const auto *CFP = dyn_cast<ConstantFPSDNode>(Op))
6149 return KnownFPClass(CFP->getValueAPF());
6150
6151 if (Depth >= MaxRecursionDepth)
6152 return Known;
6153
6154 if (Op.getOpcode() == ISD::UNDEF)
6155 return Known;
6156
6157 EVT VT = Op.getValueType();
6158 assert(VT.isFloatingPoint() && "Computing KnownFPClass on non-FP op!");
6159 assert((!VT.isFixedLengthVector() ||
6160 DemandedElts.getBitWidth() == VT.getVectorNumElements()) &&
6161 "Unexpected vector size");
6162
6163 if (!DemandedElts)
6164 return Known;
6165
6166 unsigned Opcode = Op.getOpcode();
6167 switch (Opcode) {
6168 case ISD::POISON: {
6169 Known.KnownFPClasses = fcNone;
6170 Known.SignBit = false;
6171 break;
6172 }
6173 case ISD::FNEG: {
6174 Known = computeKnownFPClass(Op.getOperand(0), DemandedElts,
6175 InterestedClasses, Depth + 1);
6176 Known.fneg();
6177 break;
6178 }
6179 case ISD::BUILD_VECTOR: {
6180 assert(!VT.isScalableVector());
6181 bool First = true;
6182 for (unsigned I = 0, E = Op.getNumOperands(); I != E; ++I) {
6183 if (!DemandedElts[I])
6184 continue;
6185
6186 if (First) {
6187 Known =
6188 computeKnownFPClass(Op.getOperand(I), InterestedClasses, Depth + 1);
6189 First = false;
6190 } else {
6191 Known |=
6192 computeKnownFPClass(Op.getOperand(I), InterestedClasses, Depth + 1);
6193 }
6194
6195 if (Known.isUnknown())
6196 break;
6197 }
6198 break;
6199 }
6201 SDValue Src = Op.getOperand(0);
6202 auto *CIdx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6203 EVT SrcVT = Src.getValueType();
6204 if (SrcVT.isFixedLengthVector() && CIdx) {
6205 if (CIdx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
6206 APInt DemandedSrcElts = APInt::getOneBitSet(
6207 SrcVT.getVectorNumElements(), CIdx->getZExtValue());
6208 Known = computeKnownFPClass(Src, DemandedSrcElts, InterestedClasses,
6209 Depth + 1);
6210 } else {
6211 // Out of bounds index is poison.
6212 Known.KnownFPClasses = fcNone;
6213 }
6214 } else {
6215 Known = computeKnownFPClass(Src, InterestedClasses, Depth + 1);
6216 }
6217 break;
6218 }
6219 case ISD::SPLAT_VECTOR: {
6220 Known = computeKnownFPClass(Op.getOperand(0), InterestedClasses, Depth + 1);
6221 break;
6222 }
6223 case ISD::BITCAST: {
6224 // FIXME: It should not be necessary to check for an elementwise bitcast.
6225 // If a bitcast is not elementwise between vector / scalar types,
6226 // computeKnownBits already splices the known bits of the source elements
6227 // appropriately so as to line up with the bits of the result's demanded
6228 // elements.
6229 EVT SrcVT = Op.getOperand(0).getValueType();
6230 if (VT.isScalableVector() || SrcVT.isScalableVector())
6231 break;
6232 unsigned VTNumElts = VT.isVector() ? VT.getVectorNumElements() : 1;
6233 unsigned SrcVTNumElts = SrcVT.isVector() ? SrcVT.getVectorNumElements() : 1;
6234 if (VTNumElts != SrcVTNumElts)
6235 break;
6236
6237 KnownBits Bits = computeKnownBits(Op, DemandedElts, Depth + 1);
6238 Known = KnownFPClass::bitcast(VT.getFltSemantics(), Bits);
6239 break;
6240 }
6241 case ISD::FABS: {
6242 Known = computeKnownFPClass(Op.getOperand(0), DemandedElts,
6243 InterestedClasses, Depth + 1);
6244 Known.fabs();
6245 break;
6246 }
6247 case ISD::FCOPYSIGN: {
6248 Known = computeKnownFPClass(Op.getOperand(0), DemandedElts,
6249 InterestedClasses, Depth + 1);
6250 KnownFPClass KnownSign = computeKnownFPClass(Op.getOperand(1), DemandedElts,
6251 InterestedClasses, Depth + 1);
6252 Known.copysign(KnownSign);
6253 break;
6254 }
6255 case ISD::AssertNoFPClass: {
6256 Known = computeKnownFPClass(Op.getOperand(0), DemandedElts,
6257 InterestedClasses, Depth + 1);
6258 FPClassTest AssertedClasses =
6259 static_cast<FPClassTest>(Op->getConstantOperandVal(1));
6260 Known.KnownFPClasses &= ~AssertedClasses;
6261 break;
6262 }
6264 SDValue Src = Op.getOperand(0);
6265 EVT SrcVT = Src.getValueType();
6266 if (SrcVT.isFixedLengthVector()) {
6267 unsigned Idx = Op.getConstantOperandVal(1);
6268 unsigned NumSrcElts = SrcVT.getVectorNumElements();
6269
6270 APInt DemandedSrcElts = DemandedElts.zextOrTrunc(NumSrcElts).shl(Idx);
6271 Known = computeKnownFPClass(Src, DemandedSrcElts, InterestedClasses,
6272 Depth + 1);
6273 } else {
6274 Known = computeKnownFPClass(Src, InterestedClasses, Depth + 1);
6275 }
6276 break;
6277 }
6278 case ISD::INSERT_SUBVECTOR: {
6279 SDValue BaseVector = Op.getOperand(0);
6280 SDValue SubVector = Op.getOperand(1);
6281 EVT BaseVT = BaseVector.getValueType();
6282 if (BaseVT.isFixedLengthVector()) {
6283 unsigned Idx = Op.getConstantOperandVal(2);
6284 unsigned NumBaseElts = BaseVT.getVectorNumElements();
6285 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
6286
6287 APInt DemandedMask =
6288 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
6289 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
6290 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
6291
6292 if (!DemandedSrcElts.isZero())
6293 Known = computeKnownFPClass(BaseVector, DemandedSrcElts,
6294 InterestedClasses, Depth + 1);
6295 if (!DemandedSubElts.isZero()) {
6297 SubVector, DemandedSubElts, InterestedClasses, Depth + 1);
6298 Known = DemandedSrcElts.isZero() ? SubKnown : (Known | SubKnown);
6299 }
6300 } else {
6301 Known = computeKnownFPClass(SubVector, InterestedClasses, Depth + 1);
6302 if (!Known.isUnknown())
6303 Known |= computeKnownFPClass(BaseVector, InterestedClasses, Depth + 1);
6304 }
6305 break;
6306 }
6307 case ISD::SELECT:
6308 case ISD::VSELECT: {
6309 // TODO: Add adjustKnownFPClassForSelectArm clamp recognition as in
6310 // IR-level ValueTracking.
6311 KnownFPClass KnownFalseClass = computeKnownFPClass(
6312 Op.getOperand(2), DemandedElts, InterestedClasses, Depth + 1);
6313 if (KnownFalseClass.isUnknown())
6314 break;
6315 KnownFPClass KnownTrueClass = computeKnownFPClass(
6316 Op.getOperand(1), DemandedElts, InterestedClasses, Depth + 1);
6317 Known = KnownTrueClass.intersectWith(KnownFalseClass);
6318 break;
6319 }
6320 default:
6321 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6322 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6323 TLI->computeKnownFPClassForTargetNode(Op, Known, DemandedElts, *this,
6324 Depth);
6325 }
6326 break;
6327 }
6328
6329 return Known;
6330}
6331
6333 unsigned Depth) const {
6334 APInt DemandedElts = getDemandAllEltsMask(Op);
6335 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
6336}
6337
6339 bool SNaN, unsigned Depth) const {
6340 assert(!DemandedElts.isZero() && "No demanded elements");
6341
6342 // If we're told that NaNs won't happen, assume they won't.
6343 if (Op->getFlags().hasNoNaNs())
6344 return true;
6345
6346 if (Depth >= MaxRecursionDepth)
6347 return false; // Limit search depth.
6348
6349 unsigned Opcode = Op.getOpcode();
6350 switch (Opcode) {
6351 case ISD::FADD:
6352 case ISD::FSUB:
6353 case ISD::FMUL:
6354 case ISD::FDIV:
6355 case ISD::FREM:
6356 case ISD::FSIN:
6357 case ISD::FCOS:
6358 case ISD::FTAN:
6359 case ISD::FASIN:
6360 case ISD::FACOS:
6361 case ISD::FATAN:
6362 case ISD::FATAN2:
6363 case ISD::FSINH:
6364 case ISD::FCOSH:
6365 case ISD::FTANH:
6366 case ISD::FMA:
6367 case ISD::FMULADD:
6368 case ISD::FMAD: {
6369 if (SNaN)
6370 return true;
6371 // TODO: Need isKnownNeverInfinity
6372 return false;
6373 }
6374 case ISD::FCANONICALIZE:
6375 case ISD::FEXP:
6376 case ISD::FEXP2:
6377 case ISD::FEXP10:
6378 case ISD::FTRUNC:
6379 case ISD::FFLOOR:
6380 case ISD::FCEIL:
6381 case ISD::FROUND:
6382 case ISD::FROUNDEVEN:
6383 case ISD::LROUND:
6384 case ISD::LLROUND:
6385 case ISD::FRINT:
6386 case ISD::LRINT:
6387 case ISD::LLRINT:
6388 case ISD::FNEARBYINT:
6389 case ISD::FLDEXP: {
6390 if (SNaN)
6391 return true;
6392 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6393 }
6394 case ISD::FABS:
6395 case ISD::FNEG:
6396 case ISD::FCOPYSIGN: {
6397 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6398 }
6399 case ISD::SELECT:
6400 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
6401 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
6402 case ISD::FP_EXTEND:
6403 case ISD::FP_ROUND: {
6404 if (SNaN)
6405 return true;
6406 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6407 }
6408 case ISD::SINT_TO_FP:
6409 case ISD::UINT_TO_FP:
6410 return true;
6411 case ISD::FSQRT: // Need is known positive
6412 case ISD::FLOG:
6413 case ISD::FLOG2:
6414 case ISD::FLOG10:
6415 case ISD::FPOWI:
6416 case ISD::FPOW: {
6417 if (SNaN)
6418 return true;
6419 // TODO: Refine on operand
6420 return false;
6421 }
6422 case ISD::FMINNUM:
6423 case ISD::FMAXNUM:
6424 case ISD::FMINIMUMNUM:
6425 case ISD::FMAXIMUMNUM: {
6426 // Only one needs to be known not-nan, since it will be returned if the
6427 // other ends up being one.
6428 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
6429 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
6430 }
6431 case ISD::FMINNUM_IEEE:
6432 case ISD::FMAXNUM_IEEE: {
6433 if (SNaN)
6434 return true;
6435 // This can return a NaN if either operand is an sNaN, or if both operands
6436 // are NaN.
6437 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
6438 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
6439 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
6440 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
6441 }
6442 case ISD::FMINIMUM:
6443 case ISD::FMAXIMUM: {
6444 // TODO: Does this quiet or return the origina NaN as-is?
6445 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
6446 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
6447 }
6449 SDValue Src = Op.getOperand(0);
6450 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6451 EVT SrcVT = Src.getValueType();
6452 if (SrcVT.isFixedLengthVector() && Idx &&
6453 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
6454 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
6455 Idx->getZExtValue());
6456 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
6457 }
6458 return isKnownNeverNaN(Src, SNaN, Depth + 1);
6459 }
6461 SDValue Src = Op.getOperand(0);
6462 if (Src.getValueType().isFixedLengthVector()) {
6463 unsigned Idx = Op.getConstantOperandVal(1);
6464 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
6465 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
6466 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
6467 }
6468 return isKnownNeverNaN(Src, SNaN, Depth + 1);
6469 }
6470 case ISD::INSERT_SUBVECTOR: {
6471 SDValue BaseVector = Op.getOperand(0);
6472 SDValue SubVector = Op.getOperand(1);
6473 EVT BaseVectorVT = BaseVector.getValueType();
6474 if (BaseVectorVT.isFixedLengthVector()) {
6475 unsigned Idx = Op.getConstantOperandVal(2);
6476 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
6477 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
6478
6479 // Clear/Extract the bits at the position where the subvector will be
6480 // inserted.
6481 APInt DemandedMask =
6482 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
6483 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
6484 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
6485
6486 bool NeverNaN = true;
6487 if (!DemandedSrcElts.isZero())
6488 NeverNaN &=
6489 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
6490 if (NeverNaN && !DemandedSubElts.isZero())
6491 NeverNaN &=
6492 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
6493 return NeverNaN;
6494 }
6495 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
6496 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
6497 }
6498 case ISD::BUILD_VECTOR: {
6499 unsigned NumElts = Op.getNumOperands();
6500 for (unsigned I = 0; I != NumElts; ++I)
6501 if (DemandedElts[I] &&
6502 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
6503 return false;
6504 return true;
6505 }
6506 case ISD::SPLAT_VECTOR:
6507 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
6508 case ISD::AssertNoFPClass: {
6509 FPClassTest NoFPClass =
6510 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
6511 if ((NoFPClass & fcNan) == fcNan)
6512 return true;
6513 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
6514 return true;
6515 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6516 }
6517 default:
6518 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6519 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6520 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
6521 Depth);
6522 }
6523 break;
6524 }
6525
6526 FPClassTest NanMask = SNaN ? fcSNan : fcNan;
6527 KnownFPClass Known = computeKnownFPClass(Op, DemandedElts, NanMask, Depth);
6528 return Known.isKnownNever(NanMask);
6529}
6530
6532 APInt DemandedElts = getDemandAllEltsMask(Op);
6533 return isKnownNeverLogicalZero(Op, DemandedElts, Depth);
6534}
6535
6537 const APInt &DemandedElts,
6538 unsigned Depth) const {
6539 assert(!DemandedElts.isZero() && "No demanded elements");
6540 EVT VT = Op.getValueType();
6541 KnownFPClass Known =
6542 computeKnownFPClass(Op, DemandedElts, fcZero | fcSubnormal, Depth);
6543 return Known.isKnownNeverLogicalZero(getDenormalMode(VT));
6544}
6545
6547 APInt DemandedElts = getDemandAllEltsMask(Op);
6548 return isKnownNeverZero(Op, DemandedElts, Depth);
6549}
6550
6552 unsigned Depth) const {
6553 if (Depth >= MaxRecursionDepth)
6554 return false; // Limit search depth.
6555
6556 EVT OpVT = Op.getValueType();
6557 unsigned BitWidth = OpVT.getScalarSizeInBits();
6558
6559 assert(!Op.getValueType().isFloatingPoint() &&
6560 "Floating point types unsupported - use isKnownNeverLogicalZero");
6561
6562 // If the value is a constant, we can obviously see if it is a zero or not.
6563 auto IsNeverZero = [BitWidth](const ConstantSDNode *C) {
6564 APInt V = C->getAPIntValue().zextOrTrunc(BitWidth);
6565 return !V.isZero();
6566 };
6567
6568 if (ISD::matchUnaryPredicate(Op, IsNeverZero))
6569 return true;
6570
6571 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6572 // some degree.
6573 switch (Op.getOpcode()) {
6574 default:
6575 break;
6576
6577 case ISD::BUILD_VECTOR:
6578 // Are all operands of a build vector constant non-zero?
6579 if (all_of(enumerate(Op->ops()), [&](auto P) {
6580 auto *C = dyn_cast<ConstantSDNode>(P.value());
6581 return !DemandedElts[P.index()] || (C && IsNeverZero(C));
6582 }))
6583 return true;
6584 break;
6585
6586 case ISD::SPLAT_VECTOR:
6587 // Is the operand of a splat vector a constant non-zero?
6588 if (auto *C = dyn_cast<ConstantSDNode>(Op->getOperand(0)))
6589 if (IsNeverZero(C))
6590 return true;
6591 break;
6592
6594 SDValue InVec = Op.getOperand(0);
6595 SDValue EltNo = Op.getOperand(1);
6596 EVT VecVT = InVec.getValueType();
6597
6598 // Skip scalable vectors or implicit extensions.
6599 if (VecVT.isScalableVector() ||
6600 OpVT.getScalarSizeInBits() != VecVT.getScalarSizeInBits())
6601 break;
6602
6603 // If we know the element index, just demand that vector element, else for
6604 // an unknown element index, ignore DemandedElts and demand them all.
6605 const unsigned NumSrcElts = VecVT.getVectorNumElements();
6606 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
6607 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
6608 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
6609 DemandedSrcElts =
6610 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
6611
6612 return isKnownNeverZero(InVec, DemandedSrcElts, Depth + 1);
6613 }
6614
6615 case ISD::OR:
6616 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6617 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6618
6619 case ISD::VSELECT:
6620 case ISD::SELECT:
6621 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6622 isKnownNeverZero(Op.getOperand(2), DemandedElts, Depth + 1);
6623
6624 case ISD::SHL: {
6625 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6626 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6627 KnownBits ValKnown =
6628 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6629 // 1 << X is never zero.
6630 if (ValKnown.One[0])
6631 return true;
6632 // If max shift cnt of known ones is non-zero, result is non-zero.
6633 APInt MaxCnt = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1)
6634 .getMaxValue();
6635 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6636 !ValKnown.One.shl(MaxCnt).isZero())
6637 return true;
6638 break;
6639 }
6640
6641 case ISD::VECTOR_SHUFFLE: {
6642 if (Op.getValueType().isScalableVector())
6643 return false;
6644
6645 unsigned NumElts = DemandedElts.getBitWidth();
6646
6647 // All demanded elements from LHS and RHS must be known non-zero.
6648 // Demanded elements with undef shuffle mask elements are unknown.
6649
6650 APInt DemandedLHS, DemandedRHS;
6651 auto *SVN = cast<ShuffleVectorSDNode>(Op);
6652 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
6653 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
6654 DemandedLHS, DemandedRHS))
6655 return false;
6656
6657 return (!DemandedLHS ||
6658 isKnownNeverZero(Op.getOperand(0), DemandedLHS, Depth + 1)) &&
6659 (!DemandedRHS ||
6660 isKnownNeverZero(Op.getOperand(1), DemandedRHS, Depth + 1));
6661 }
6662
6663 case ISD::UADDSAT:
6664 case ISD::UMAX:
6665 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6666 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6667
6668 case ISD::UMIN:
6669 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6670 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6671
6672 // For smin/smax: If either operand is known negative/positive
6673 // respectively we don't need the other to be known at all.
6674 case ISD::SMAX: {
6675 KnownBits Op1 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
6676 if (Op1.isStrictlyPositive())
6677 return true;
6678
6679 KnownBits Op0 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6680 if (Op0.isStrictlyPositive())
6681 return true;
6682
6683 if (Op1.isNonZero() && Op0.isNonZero())
6684 return true;
6685
6686 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6687 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6688 }
6689 case ISD::SMIN: {
6690 KnownBits Op1 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
6691 if (Op1.isNegative())
6692 return true;
6693
6694 KnownBits Op0 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6695 if (Op0.isNegative())
6696 return true;
6697
6698 if (Op1.isNonZero() && Op0.isNonZero())
6699 return true;
6700
6701 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6702 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6703 }
6704
6705 case ISD::ROTL:
6706 case ISD::ROTR:
6707 case ISD::BITREVERSE:
6708 case ISD::BSWAP:
6709 case ISD::CTPOP:
6710 case ISD::ABS:
6712 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6713
6714 case ISD::SRA:
6715 case ISD::SRL: {
6716 if (Op->getFlags().hasExact())
6717 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6718 KnownBits ValKnown =
6719 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6720 if (ValKnown.isNegative())
6721 return true;
6722 // If max shift cnt of known ones is non-zero, result is non-zero.
6723 APInt MaxCnt = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1)
6724 .getMaxValue();
6725 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6726 !ValKnown.One.lshr(MaxCnt).isZero())
6727 return true;
6728 break;
6729 }
6730 case ISD::UDIV:
6731 case ISD::SDIV:
6732 // div exact can only produce a zero if the dividend is zero.
6733 // TODO: For udiv this is also true if Op1 u<= Op0
6734 if (Op->getFlags().hasExact())
6735 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6736 break;
6737
6738 case ISD::ADD:
6739 if (Op->getFlags().hasNoUnsignedWrap())
6740 if (isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6741 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1))
6742 return true;
6743 // TODO: There are a lot more cases we can prove for add.
6744 break;
6745
6746 case ISD::SUB: {
6747 if (isNullConstant(Op.getOperand(0)))
6748 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1);
6749
6750 std::optional<bool> ne = KnownBits::ne(
6751 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1),
6752 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1));
6753 return ne && *ne;
6754 }
6755
6756 case ISD::MUL:
6757 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6758 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6759 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6760 return true;
6761 break;
6762
6763 case ISD::ZERO_EXTEND:
6764 case ISD::SIGN_EXTEND:
6765 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6766 case ISD::VSCALE: {
6768 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6769 ConstantRange CR =
6770 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6771 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6772 return true;
6773 break;
6774 }
6775 }
6776
6777 return computeKnownBits(Op, DemandedElts, Depth).isNonZero();
6778}
6779
6781 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6782 return !C1->isNegative();
6783
6784 switch (Op.getOpcode()) {
6785 case ISD::FABS:
6786 case ISD::FEXP:
6787 case ISD::FEXP2:
6788 case ISD::FEXP10:
6789 return true;
6790 default:
6791 return false;
6792 }
6793
6794 llvm_unreachable("covered opcode switch");
6795}
6796
6798 assert(Use.getValueType().isFloatingPoint());
6799 const SDNode *User = Use.getUser();
6800 if (User->getFlags().hasNoSignedZeros())
6801 return true;
6802
6803 unsigned OperandNo = Use.getOperandNo();
6804 // Check if this use is insensitive to the sign of zero
6805 switch (User->getOpcode()) {
6806 case ISD::SETCC:
6807 // Comparisons: IEEE-754 specifies +0.0 == -0.0.
6808 case ISD::FABS:
6809 // fabs always produces +0.0.
6810 return true;
6811 case ISD::FCOPYSIGN:
6812 // copysign overwrites the sign bit of the first operand.
6813 return OperandNo == 0;
6814 case ISD::FADD:
6815 case ISD::FSUB: {
6816 // Arithmetic with non-zero constants fixes the uncertainty around the
6817 // sign bit.
6818 SDValue Other = User->getOperand(1 - OperandNo);
6820 }
6821 case ISD::FP_TO_SINT:
6822 case ISD::FP_TO_UINT:
6823 // fp-to-int conversions normalize signed zeros.
6824 return true;
6825 default:
6826 return false;
6827 }
6828}
6829
6831 if (Op->getFlags().hasNoSignedZeros())
6832 return true;
6833 // FIXME: Limit the amount of checked uses to not introduce a compile-time
6834 // regression. Ideally, this should be implemented as a demanded-bits
6835 // optimization that stems from the users.
6836 if (Op->use_size() > 2)
6837 return false;
6838 return all_of(Op->uses(),
6839 [&](const SDUse &Use) { return canIgnoreSignBitOfZero(Use); });
6840}
6841
6843 // Check the obvious case.
6844 if (A == B) return true;
6845
6846 // For negative and positive zero.
6849 if (CA->isZero() && CB->isZero()) return true;
6850
6851 // Otherwise they may not be equal.
6852 return false;
6853}
6854
6855// Only bits set in Mask must be negated, other bits may be arbitrary.
6857 if (isBitwiseNot(V, AllowUndefs))
6858 return V.getOperand(0);
6859
6860 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6861 // bits in the non-extended part.
6862 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6863 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6864 return SDValue();
6865 SDValue ExtArg = V.getOperand(0);
6866 if (ExtArg.getScalarValueSizeInBits() >=
6867 MaskC->getAPIntValue().getActiveBits() &&
6868 isBitwiseNot(ExtArg, AllowUndefs) &&
6869 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6870 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6871 return ExtArg.getOperand(0).getOperand(0);
6872 return SDValue();
6873}
6874
6876 // Match masked merge pattern (X & ~M) op (Y & M)
6877 // Including degenerate case (X & ~M) op M
6878 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6879 SDValue Other) {
6880 if (SDValue NotOperand =
6881 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6882 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6883 NotOperand->getOpcode() == ISD::TRUNCATE)
6884 NotOperand = NotOperand->getOperand(0);
6885
6886 if (Other == NotOperand)
6887 return true;
6888 if (Other->getOpcode() == ISD::AND)
6889 return NotOperand == Other->getOperand(0) ||
6890 NotOperand == Other->getOperand(1);
6891 }
6892 return false;
6893 };
6894
6895 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6896 A = A->getOperand(0);
6897
6898 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6899 B = B->getOperand(0);
6900
6901 if (A->getOpcode() == ISD::AND)
6902 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6903 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6904 return false;
6905}
6906
6907// FIXME: unify with llvm::haveNoCommonBitsSet.
6909 assert(A.getValueType() == B.getValueType() &&
6910 "Values must have the same type");
6913 return true;
6916}
6917
6918static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6919 SelectionDAG &DAG) {
6920 if (cast<ConstantSDNode>(Step)->isZero())
6921 return DAG.getConstant(0, DL, VT);
6922
6923 return SDValue();
6924}
6925
6928 SelectionDAG &DAG) {
6929 int NumOps = Ops.size();
6930 assert(NumOps != 0 && "Can't build an empty vector!");
6931 assert(!VT.isScalableVector() &&
6932 "BUILD_VECTOR cannot be used with scalable types");
6933 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6934 "Incorrect element count in BUILD_VECTOR!");
6935
6936 // BUILD_VECTOR of UNDEFs is UNDEF.
6937 bool AllPoison = true;
6938 if (llvm::all_of(Ops, [&AllPoison](SDValue Op) {
6939 AllPoison &= Op.getOpcode() == ISD::POISON;
6940 return Op.isUndef();
6941 }))
6942 return AllPoison ? DAG.getPOISON(VT) : DAG.getUNDEF(VT);
6943
6944 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6945 SDValue IdentitySrc;
6946 bool IsIdentity = true;
6947 for (int i = 0; i != NumOps; ++i) {
6948 if (Ops[i].getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
6949 Ops[i].getOperand(0).getValueType() != VT ||
6950 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6951 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6952 Ops[i].getConstantOperandAPInt(1) != i) {
6953 IsIdentity = false;
6954 break;
6955 }
6956 IdentitySrc = Ops[i].getOperand(0);
6957 }
6958 if (IsIdentity)
6959 return IdentitySrc;
6960
6961 return SDValue();
6962}
6963
6964/// Try to simplify vector concatenation to an input value, undef, or build
6965/// vector.
6968 SelectionDAG &DAG) {
6969 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6971 [Ops](SDValue Op) {
6972 return Ops[0].getValueType() == Op.getValueType();
6973 }) &&
6974 "Concatenation of vectors with inconsistent value types!");
6975 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6976 VT.getVectorElementCount() &&
6977 "Incorrect element count in vector concatenation!");
6978
6979 if (Ops.size() == 1)
6980 return Ops[0];
6981
6982 // Concat of UNDEFs is UNDEF.
6983 bool AllPoison = true;
6984 if (llvm::all_of(Ops, [&AllPoison](SDValue Op) {
6985 AllPoison &= Op.getOpcode() == ISD::POISON;
6986 return Op.isUndef();
6987 }))
6988 return AllPoison ? DAG.getPOISON(VT) : DAG.getUNDEF(VT);
6989
6990 // Scan the operands and look for extract operations from a single source
6991 // that correspond to insertion at the same location via this concatenation:
6992 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6993 SDValue IdentitySrc;
6994 bool IsIdentity = true;
6995 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6996 SDValue Op = Ops[i];
6997 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6998 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6999 Op.getOperand(0).getValueType() != VT ||
7000 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
7001 Op.getConstantOperandVal(1) != IdentityIndex) {
7002 IsIdentity = false;
7003 break;
7004 }
7005 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
7006 "Unexpected identity source vector for concat of extracts");
7007 IdentitySrc = Op.getOperand(0);
7008 }
7009 if (IsIdentity) {
7010 assert(IdentitySrc && "Failed to set source vector of extracts");
7011 return IdentitySrc;
7012 }
7013
7014 // The code below this point is only designed to work for fixed width
7015 // vectors, so we bail out for now.
7016 if (VT.isScalableVector())
7017 return SDValue();
7018
7019 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
7020 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
7021 // BUILD_VECTOR.
7022 // FIXME: Add support for SCALAR_TO_VECTOR as well.
7023 EVT SVT = VT.getScalarType();
7025 for (SDValue Op : Ops) {
7026 EVT OpVT = Op.getValueType();
7027 if (Op.getOpcode() == ISD::POISON)
7028 Elts.append(OpVT.getVectorNumElements(), DAG.getPOISON(SVT));
7029 else if (Op.getOpcode() == ISD::UNDEF)
7030 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
7031 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
7032 Elts.append(Op->op_begin(), Op->op_end());
7033 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
7034 OpVT.getVectorNumElements() == 1 &&
7035 isNullConstant(Op.getOperand(2)))
7036 Elts.push_back(Op.getOperand(1));
7037 else
7038 return SDValue();
7039 }
7040
7041 // BUILD_VECTOR requires all inputs to be of the same type, find the
7042 // maximum type and extend them all.
7043 for (SDValue Op : Elts)
7044 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
7045
7046 if (SVT.bitsGT(VT.getScalarType())) {
7047 for (SDValue &Op : Elts) {
7048 if (Op.getOpcode() == ISD::POISON)
7049 Op = DAG.getPOISON(SVT);
7050 else if (Op.getOpcode() == ISD::UNDEF)
7051 Op = DAG.getUNDEF(SVT);
7052 else
7053 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
7054 ? DAG.getZExtOrTrunc(Op, DL, SVT)
7055 : DAG.getSExtOrTrunc(Op, DL, SVT);
7056 }
7057 }
7058
7059 SDValue V = DAG.getBuildVector(VT, DL, Elts);
7060 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
7061 return V;
7062}
7063
7064/// Gets or creates the specified node.
7065SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
7066 SDVTList VTs = getVTList(VT);
7068 AddNodeIDNode(ID, Opcode, VTs, {});
7069 void *IP = nullptr;
7070 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7071 return SDValue(E, 0);
7072
7073 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7074 CSEMap.InsertNode(N, IP);
7075
7076 InsertNode(N);
7077 SDValue V = SDValue(N, 0);
7078 NewSDValueDbgMsg(V, "Creating new node: ", this);
7079 return V;
7080}
7081
7082SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7083 SDValue N1) {
7084 SDNodeFlags Flags;
7085 if (Inserter)
7086 Flags = Inserter->getFlags();
7087 return getNode(Opcode, DL, VT, N1, Flags);
7088}
7089
7090SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7091 SDValue N1, const SDNodeFlags Flags) {
7092 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
7093
7094 // Constant fold unary operations with a vector integer or float operand.
7095 switch (Opcode) {
7096 default:
7097 // FIXME: Entirely reasonable to perform folding of other unary
7098 // operations here as the need arises.
7099 break;
7100 case ISD::FNEG:
7101 case ISD::FABS:
7102 case ISD::FCEIL:
7103 case ISD::FTRUNC:
7104 case ISD::FFLOOR:
7105 case ISD::FP_EXTEND:
7106 case ISD::FP_TO_SINT:
7107 case ISD::FP_TO_UINT:
7108 case ISD::FP_TO_FP16:
7109 case ISD::FP_TO_BF16:
7110 case ISD::TRUNCATE:
7111 case ISD::ANY_EXTEND:
7112 case ISD::ZERO_EXTEND:
7113 case ISD::SIGN_EXTEND:
7114 case ISD::UINT_TO_FP:
7115 case ISD::SINT_TO_FP:
7116 case ISD::FP16_TO_FP:
7117 case ISD::BF16_TO_FP:
7118 case ISD::BITCAST:
7119 case ISD::ABS:
7121 case ISD::BITREVERSE:
7122 case ISD::BSWAP:
7123 case ISD::CTLZ:
7125 case ISD::CTTZ:
7127 case ISD::CTPOP:
7128 case ISD::CTLS:
7129 case ISD::STEP_VECTOR: {
7130 SDValue Ops = {N1};
7131 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
7132 return Fold;
7133 }
7134 }
7135
7136 unsigned OpOpcode = N1.getNode()->getOpcode();
7137 switch (Opcode) {
7138 case ISD::STEP_VECTOR:
7139 assert(VT.isScalableVector() &&
7140 "STEP_VECTOR can only be used with scalable types");
7141 assert(OpOpcode == ISD::TargetConstant &&
7142 VT.getVectorElementType() == N1.getValueType() &&
7143 "Unexpected step operand");
7144 break;
7145 case ISD::FREEZE:
7146 assert(VT == N1.getValueType() && "Unexpected VT!");
7148 return N1;
7149 break;
7150 case ISD::TokenFactor:
7151 case ISD::MERGE_VALUES:
7153 return N1; // Factor, merge or concat of one node? No need.
7154 case ISD::BUILD_VECTOR: {
7155 // Attempt to simplify BUILD_VECTOR.
7156 SDValue Ops[] = {N1};
7157 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7158 return V;
7159 break;
7160 }
7161 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
7162 case ISD::FP_EXTEND:
7164 "Invalid FP cast!");
7165 if (N1.getValueType() == VT) return N1; // noop conversion.
7166 assert((!VT.isVector() || VT.getVectorElementCount() ==
7168 "Vector element count mismatch!");
7169 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
7170 if (N1.isUndef())
7171 return getUNDEF(VT);
7172 break;
7173 case ISD::FP_TO_SINT:
7174 case ISD::FP_TO_UINT:
7175 if (N1.isUndef())
7176 return getUNDEF(VT);
7177 break;
7178 case ISD::SINT_TO_FP:
7179 case ISD::UINT_TO_FP:
7180 // [us]itofp(undef) = 0, because the result value is bounded.
7181 if (N1.isUndef())
7182 return getConstantFP(0.0, DL, VT);
7183 break;
7184 case ISD::SIGN_EXTEND:
7185 assert(VT.isInteger() && N1.getValueType().isInteger() &&
7186 "Invalid SIGN_EXTEND!");
7187 assert(VT.isVector() == N1.getValueType().isVector() &&
7188 "SIGN_EXTEND result type type should be vector iff the operand "
7189 "type is vector!");
7190 if (N1.getValueType() == VT) return N1; // noop extension
7191 assert((!VT.isVector() || VT.getVectorElementCount() ==
7193 "Vector element count mismatch!");
7194 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
7195 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
7196 SDNodeFlags Flags;
7197 if (OpOpcode == ISD::ZERO_EXTEND)
7198 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7199 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
7200 transferDbgValues(N1, NewVal);
7201 return NewVal;
7202 }
7203
7204 if (OpOpcode == ISD::POISON)
7205 return getPOISON(VT);
7206
7207 if (N1.isUndef())
7208 // sext(undef) = 0, because the top bits will all be the same.
7209 return getConstant(0, DL, VT);
7210
7211 // Skip unnecessary sext_inreg pattern:
7212 // (sext (trunc x)) -> x iff the upper bits are all signbits.
7213 if (OpOpcode == ISD::TRUNCATE) {
7214 SDValue OpOp = N1.getOperand(0);
7215 if (OpOp.getValueType() == VT) {
7216 unsigned NumSignExtBits =
7218 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
7219 transferDbgValues(N1, OpOp);
7220 return OpOp;
7221 }
7222 }
7223 }
7224 break;
7225 case ISD::ZERO_EXTEND:
7226 assert(VT.isInteger() && N1.getValueType().isInteger() &&
7227 "Invalid ZERO_EXTEND!");
7228 assert(VT.isVector() == N1.getValueType().isVector() &&
7229 "ZERO_EXTEND result type type should be vector iff the operand "
7230 "type is vector!");
7231 if (N1.getValueType() == VT) return N1; // noop extension
7232 assert((!VT.isVector() || VT.getVectorElementCount() ==
7234 "Vector element count mismatch!");
7235 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
7236 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
7237 SDNodeFlags Flags;
7238 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7239 SDValue NewVal =
7240 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
7241 transferDbgValues(N1, NewVal);
7242 return NewVal;
7243 }
7244
7245 if (OpOpcode == ISD::POISON)
7246 return getPOISON(VT);
7247
7248 if (N1.isUndef())
7249 // zext(undef) = 0, because the top bits will be zero.
7250 return getConstant(0, DL, VT);
7251
7252 // Skip unnecessary zext_inreg pattern:
7253 // (zext (trunc x)) -> x iff the upper bits are known zero.
7254 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
7255 // use to recognise zext_inreg patterns.
7256 if (OpOpcode == ISD::TRUNCATE) {
7257 SDValue OpOp = N1.getOperand(0);
7258 if (OpOp.getValueType() == VT) {
7259 if (OpOp.getOpcode() != ISD::AND) {
7262 if (MaskedValueIsZero(OpOp, HiBits)) {
7263 transferDbgValues(N1, OpOp);
7264 return OpOp;
7265 }
7266 }
7267 }
7268 }
7269 break;
7270 case ISD::ANY_EXTEND:
7271 assert(VT.isInteger() && N1.getValueType().isInteger() &&
7272 "Invalid ANY_EXTEND!");
7273 assert(VT.isVector() == N1.getValueType().isVector() &&
7274 "ANY_EXTEND result type type should be vector iff the operand "
7275 "type is vector!");
7276 if (N1.getValueType() == VT) return N1; // noop extension
7277 assert((!VT.isVector() || VT.getVectorElementCount() ==
7279 "Vector element count mismatch!");
7280 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
7281
7282 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
7283 OpOpcode == ISD::ANY_EXTEND) {
7284 SDNodeFlags Flags;
7285 if (OpOpcode == ISD::ZERO_EXTEND)
7286 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7287 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
7288 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
7289 }
7290 if (N1.isUndef())
7291 return getUNDEF(VT);
7292
7293 // (ext (trunc x)) -> x
7294 if (OpOpcode == ISD::TRUNCATE) {
7295 SDValue OpOp = N1.getOperand(0);
7296 if (OpOp.getValueType() == VT) {
7297 transferDbgValues(N1, OpOp);
7298 return OpOp;
7299 }
7300 }
7301 break;
7302 case ISD::TRUNCATE:
7303 assert(VT.isInteger() && N1.getValueType().isInteger() &&
7304 "Invalid TRUNCATE!");
7305 assert(VT.isVector() == N1.getValueType().isVector() &&
7306 "TRUNCATE result type type should be vector iff the operand "
7307 "type is vector!");
7308 if (N1.getValueType() == VT) return N1; // noop truncate
7309 assert((!VT.isVector() || VT.getVectorElementCount() ==
7311 "Vector element count mismatch!");
7312 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
7313 if (OpOpcode == ISD::TRUNCATE)
7314 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
7315 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
7316 OpOpcode == ISD::ANY_EXTEND) {
7317 // If the source is smaller than the dest, we still need an extend.
7319 VT.getScalarType())) {
7320 SDNodeFlags Flags;
7321 if (OpOpcode == ISD::ZERO_EXTEND)
7322 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7323 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
7324 }
7325 if (N1.getOperand(0).getValueType().bitsGT(VT))
7326 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
7327 return N1.getOperand(0);
7328 }
7329 if (N1.isUndef())
7330 return getUNDEF(VT);
7331 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
7332 return getVScale(DL, VT,
7334 break;
7338 assert(VT.isVector() && "This DAG node is restricted to vector types.");
7339 assert(N1.getValueType().bitsLE(VT) &&
7340 "The input must be the same size or smaller than the result.");
7343 "The destination vector type must have fewer lanes than the input.");
7344 break;
7345 case ISD::ABS:
7346 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
7347 if (N1.isUndef())
7348 return getConstant(0, DL, VT);
7349 break;
7351 assert(VT.isInteger() && VT == N1.getValueType() &&
7352 "Invalid ABS_MIN_POISON!");
7353 if (N1.isUndef())
7354 return getConstant(0, DL, VT);
7355 break;
7356 case ISD::BSWAP:
7357 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
7358 assert((VT.getScalarSizeInBits() % 16 == 0) &&
7359 "BSWAP types must be a multiple of 16 bits!");
7360 if (N1.isUndef())
7361 return getUNDEF(VT);
7362 // bswap(bswap(X)) -> X.
7363 if (OpOpcode == ISD::BSWAP)
7364 return N1.getOperand(0);
7365 break;
7366 case ISD::BITREVERSE:
7367 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
7368 if (N1.isUndef())
7369 return getUNDEF(VT);
7370 break;
7371 case ISD::BITCAST:
7373 "Cannot BITCAST between types of different sizes!");
7374 if (VT == N1.getValueType()) return N1; // noop conversion.
7375 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
7376 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
7377 if (N1.isUndef())
7378 return getUNDEF(VT);
7379 break;
7381 assert(VT.isVector() && !N1.getValueType().isVector() &&
7382 (VT.getVectorElementType() == N1.getValueType() ||
7384 N1.getValueType().isInteger() &&
7386 "Illegal SCALAR_TO_VECTOR node!");
7387 if (N1.isUndef())
7388 return getUNDEF(VT);
7389 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
7390 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
7392 N1.getConstantOperandVal(1) == 0 &&
7393 N1.getOperand(0).getValueType() == VT)
7394 return N1.getOperand(0);
7395 break;
7396 case ISD::FNEG:
7397 // Negation of an unknown bag of bits is still completely undefined.
7398 if (N1.isUndef())
7399 return getUNDEF(VT);
7400
7401 if (OpOpcode == ISD::FNEG) // --X -> X
7402 return N1.getOperand(0);
7403 break;
7404 case ISD::FABS:
7405 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
7406 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
7407 break;
7408 case ISD::VSCALE:
7409 assert(VT == N1.getValueType() && "Unexpected VT!");
7410 break;
7411 case ISD::CTPOP:
7412 if (N1.getValueType().getScalarType() == MVT::i1)
7413 return N1;
7414 break;
7415 case ISD::CTLZ:
7416 case ISD::CTTZ:
7417 if (N1.getValueType().getScalarType() == MVT::i1)
7418 return getNOT(DL, N1, N1.getValueType());
7419 break;
7420 case ISD::CTLS:
7421 if (N1.getValueType().getScalarType() == MVT::i1)
7422 return getConstant(0, DL, VT);
7423 break;
7424 case ISD::VECREDUCE_ADD:
7425 if (N1.getValueType().getScalarType() == MVT::i1)
7426 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
7427 break;
7430 if (N1.getValueType().getScalarType() == MVT::i1)
7431 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
7432 break;
7435 if (N1.getValueType().getScalarType() == MVT::i1)
7436 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
7437 break;
7438 case ISD::SPLAT_VECTOR:
7439 assert(VT.isVector() && "Wrong return type!");
7440 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
7441 // that for now.
7443 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
7445 N1.getValueType().isInteger() &&
7447 "Wrong operand type!");
7448 break;
7449 }
7450
7451 SDNode *N;
7452 SDVTList VTs = getVTList(VT);
7453 SDValue Ops[] = {N1};
7454 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
7456 AddNodeIDNode(ID, Opcode, VTs, Ops);
7457 void *IP = nullptr;
7458 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7459 E->intersectFlagsWith(Flags);
7460 return SDValue(E, 0);
7461 }
7462
7463 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7464 N->setFlags(Flags);
7465 createOperands(N, Ops);
7466 CSEMap.InsertNode(N, IP);
7467 } else {
7468 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7469 createOperands(N, Ops);
7470 }
7471
7472 InsertNode(N);
7473 SDValue V = SDValue(N, 0);
7474 NewSDValueDbgMsg(V, "Creating new node: ", this);
7475 return V;
7476}
7477
7478static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
7479 const APInt &C2) {
7480 switch (Opcode) {
7481 case ISD::ADD: return C1 + C2;
7482 case ISD::SUB: return C1 - C2;
7483 case ISD::MUL: return C1 * C2;
7484 case ISD::AND: return C1 & C2;
7485 case ISD::OR: return C1 | C2;
7486 case ISD::XOR: return C1 ^ C2;
7487 case ISD::SHL: return C1 << C2;
7488 case ISD::SRL: return C1.lshr(C2);
7489 case ISD::SRA: return C1.ashr(C2);
7490 case ISD::ROTL: return C1.rotl(C2);
7491 case ISD::ROTR: return C1.rotr(C2);
7492 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
7493 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
7494 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
7495 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
7496 case ISD::SADDSAT: return C1.sadd_sat(C2);
7497 case ISD::UADDSAT: return C1.uadd_sat(C2);
7498 case ISD::SSUBSAT: return C1.ssub_sat(C2);
7499 case ISD::USUBSAT: return C1.usub_sat(C2);
7500 case ISD::SSHLSAT: return C1.sshl_sat(C2);
7501 case ISD::USHLSAT: return C1.ushl_sat(C2);
7502 case ISD::UDIV:
7503 if (!C2.getBoolValue())
7504 break;
7505 return C1.udiv(C2);
7506 case ISD::UREM:
7507 if (!C2.getBoolValue())
7508 break;
7509 return C1.urem(C2);
7510 case ISD::SDIV:
7511 if (!C2.getBoolValue())
7512 break;
7513 return C1.sdiv(C2);
7514 case ISD::SREM:
7515 if (!C2.getBoolValue())
7516 break;
7517 return C1.srem(C2);
7518 case ISD::AVGFLOORS:
7519 return APIntOps::avgFloorS(C1, C2);
7520 case ISD::AVGFLOORU:
7521 return APIntOps::avgFloorU(C1, C2);
7522 case ISD::AVGCEILS:
7523 return APIntOps::avgCeilS(C1, C2);
7524 case ISD::AVGCEILU:
7525 return APIntOps::avgCeilU(C1, C2);
7526 case ISD::ABDS:
7527 return APIntOps::abds(C1, C2);
7528 case ISD::ABDU:
7529 return APIntOps::abdu(C1, C2);
7530 case ISD::MULHS:
7531 return APIntOps::mulhs(C1, C2);
7532 case ISD::MULHU:
7533 return APIntOps::mulhu(C1, C2);
7534 case ISD::CLMUL:
7535 return APIntOps::clmul(C1, C2);
7536 case ISD::CLMULR:
7537 return APIntOps::clmulr(C1, C2);
7538 case ISD::CLMULH:
7539 return APIntOps::clmulh(C1, C2);
7540 case ISD::PEXT:
7541 return APIntOps::pext(C1, C2);
7542 case ISD::PDEP:
7543 return APIntOps::pdep(C1, C2);
7544 }
7545 return std::nullopt;
7546}
7547// Handle constant folding with UNDEF.
7548// TODO: Handle more cases.
7549static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
7550 bool IsUndef1, const APInt &C2,
7551 bool IsUndef2) {
7552 if (!(IsUndef1 || IsUndef2))
7553 return FoldValue(Opcode, C1, C2);
7554
7555 // Fold and(x, undef) -> 0
7556 // Fold mul(x, undef) -> 0
7557 if (Opcode == ISD::AND || Opcode == ISD::MUL)
7558 return APInt::getZero(C1.getBitWidth());
7559
7560 return std::nullopt;
7561}
7562
7564 const GlobalAddressSDNode *GA,
7565 const SDNode *N2) {
7566 if (GA->getOpcode() != ISD::GlobalAddress)
7567 return SDValue();
7568 if (!TLI->isOffsetFoldingLegal(GA))
7569 return SDValue();
7570 auto *C2 = dyn_cast<ConstantSDNode>(N2);
7571 if (!C2)
7572 return SDValue();
7573 int64_t Offset = C2->getSExtValue();
7574 switch (Opcode) {
7575 case ISD::ADD:
7576 case ISD::PTRADD:
7577 break;
7578 case ISD::SUB: Offset = -uint64_t(Offset); break;
7579 default: return SDValue();
7580 }
7581 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
7582 GA->getOffset() + uint64_t(Offset));
7583}
7584
7586 switch (Opcode) {
7587 case ISD::SDIV:
7588 case ISD::UDIV:
7589 case ISD::SREM:
7590 case ISD::UREM: {
7591 // If a divisor is zero/undef or any element of a divisor vector is
7592 // zero/undef, the whole op is undef.
7593 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
7594 SDValue Divisor = Ops[1];
7595 if (Divisor.isUndef() || isNullConstant(Divisor))
7596 return true;
7597
7598 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
7599 llvm::any_of(Divisor->op_values(),
7600 [](SDValue V) { return V.isUndef() ||
7601 isNullConstant(V); });
7602 // TODO: Handle signed overflow.
7603 }
7604 // TODO: Handle oversized shifts.
7605 default:
7606 return false;
7607 }
7608}
7609
7612 SDNodeFlags Flags) {
7613 // If the opcode is a target-specific ISD node, there's nothing we can
7614 // do here and the operand rules may not line up with the below, so
7615 // bail early.
7616 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
7617 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
7618 // foldCONCAT_VECTORS in getNode before this is called.
7619 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
7620 return SDValue();
7621
7622 unsigned NumOps = Ops.size();
7623 if (NumOps == 0)
7624 return SDValue();
7625
7626 if (isUndef(Opcode, Ops))
7627 return getUNDEF(VT);
7628
7629 // Handle unary special cases.
7630 if (NumOps == 1) {
7631 SDValue N1 = Ops[0];
7632
7633 // Constant fold unary operations with an integer constant operand. Even
7634 // opaque constant will be folded, because the folding of unary operations
7635 // doesn't create new constants with different values. Nevertheless, the
7636 // opaque flag is preserved during folding to prevent future folding with
7637 // other constants.
7638 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
7639 const APInt &Val = C->getAPIntValue();
7640 switch (Opcode) {
7641 case ISD::SIGN_EXTEND:
7642 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7643 C->isTargetOpcode(), C->isOpaque());
7644 case ISD::TRUNCATE:
7645 if (C->isOpaque())
7646 break;
7647 [[fallthrough]];
7648 case ISD::ZERO_EXTEND:
7649 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7650 C->isTargetOpcode(), C->isOpaque());
7651 case ISD::ANY_EXTEND:
7652 // Some targets like RISCV prefer to sign extend some types.
7653 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
7654 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7655 C->isTargetOpcode(), C->isOpaque());
7656 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7657 C->isTargetOpcode(), C->isOpaque());
7658 case ISD::ABS:
7659 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
7660 C->isOpaque());
7662 if (Val.isMinSignedValue())
7663 return getPOISON(VT);
7664 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
7665 C->isOpaque());
7666 case ISD::BITREVERSE:
7667 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
7668 C->isOpaque());
7669 case ISD::BSWAP:
7670 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
7671 C->isOpaque());
7672 case ISD::CTPOP:
7673 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
7674 C->isOpaque());
7675 case ISD::CTLZ:
7677 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7678 C->isOpaque());
7679 case ISD::CTTZ:
7681 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7682 C->isOpaque());
7683 case ISD::CTLS:
7684 // CTLS returns the number of extra sign bits so subtract one.
7685 return getConstant(Val.getNumSignBits() - 1, DL, VT,
7686 C->isTargetOpcode(), C->isOpaque());
7687 case ISD::UINT_TO_FP:
7688 case ISD::SINT_TO_FP: {
7690 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7692 return getConstantFP(FPV, DL, VT);
7693 }
7694 case ISD::FP16_TO_FP:
7695 case ISD::BF16_TO_FP: {
7696 bool Ignored;
7697 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7698 : APFloat::BFloat(),
7699 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7700
7701 // This can return overflow, underflow, or inexact; we don't care.
7702 // FIXME need to be more flexible about rounding mode.
7704 &Ignored);
7705 return getConstantFP(FPV, DL, VT);
7706 }
7707 case ISD::STEP_VECTOR:
7708 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7709 return V;
7710 break;
7711 case ISD::BITCAST:
7712 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7713 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7714 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7715 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7716 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7717 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7718 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7719 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7720 break;
7721 }
7722 }
7723
7724 // Constant fold unary operations with a floating point constant operand.
7725 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7726 APFloat V = C->getValueAPF(); // make copy
7727 switch (Opcode) {
7728 case ISD::FNEG:
7729 V.changeSign();
7730 return getConstantFP(V, DL, VT);
7731 case ISD::FABS:
7732 V.clearSign();
7733 return getConstantFP(V, DL, VT);
7734 case ISD::FCEIL: {
7735 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7737 return getConstantFP(V, DL, VT);
7738 return SDValue();
7739 }
7740 case ISD::FTRUNC: {
7741 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7743 return getConstantFP(V, DL, VT);
7744 return SDValue();
7745 }
7746 case ISD::FFLOOR: {
7747 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7749 return getConstantFP(V, DL, VT);
7750 return SDValue();
7751 }
7752 case ISD::FP_EXTEND: {
7753 bool ignored;
7754 // This can return overflow, underflow, or inexact; we don't care.
7755 // FIXME need to be more flexible about rounding mode.
7756 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7757 &ignored);
7758 return getConstantFP(V, DL, VT);
7759 }
7760 case ISD::FP_TO_SINT:
7761 case ISD::FP_TO_UINT: {
7762 bool ignored;
7763 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7764 // FIXME need to be more flexible about rounding mode.
7766 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7767 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7768 break;
7769 return getConstant(IntVal, DL, VT);
7770 }
7771 case ISD::FP_TO_FP16:
7772 case ISD::FP_TO_BF16: {
7773 bool Ignored;
7774 // This can return overflow, underflow, or inexact; we don't care.
7775 // FIXME need to be more flexible about rounding mode.
7776 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7777 : APFloat::BFloat(),
7779 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7780 }
7781 case ISD::BITCAST:
7782 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7783 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7784 VT);
7785 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7786 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7787 VT);
7788 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7789 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7790 VT);
7791 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7792 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7793 break;
7794 }
7795 }
7796
7797 // Early-out if we failed to constant fold a bitcast.
7798 if (Opcode == ISD::BITCAST)
7799 return SDValue();
7800 }
7801
7802 // Handle binops special cases.
7803 if (NumOps == 2) {
7804 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7805 return CFP;
7806
7807 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7808 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7809 if (C1->isOpaque() || C2->isOpaque())
7810 return SDValue();
7811
7812 std::optional<APInt> FoldAttempt =
7813 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7814 if (!FoldAttempt)
7815 return SDValue();
7816
7817 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7818 assert((!Folded || !VT.isVector()) &&
7819 "Can't fold vectors ops with scalar operands");
7820 return Folded;
7821 }
7822 }
7823
7824 // fold (add Sym, c) -> Sym+c
7826 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7827 if (TLI->isCommutativeBinOp(Opcode))
7829 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7830
7831 // fold (sext_in_reg c1) -> c2
7832 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7833 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7834
7835 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7836 unsigned FromBits = EVT.getScalarSizeInBits();
7837 Val <<= Val.getBitWidth() - FromBits;
7838 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7839 return getConstant(Val, DL, ConstantVT);
7840 };
7841
7842 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7843 const APInt &Val = C1->getAPIntValue();
7844 return SignExtendInReg(Val, VT);
7845 }
7846
7848 SmallVector<SDValue, 8> ScalarOps;
7849 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7850 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7851 SDValue Op = Ops[0].getOperand(I);
7852 if (Op.isUndef()) {
7853 ScalarOps.push_back(getUNDEF(OpVT));
7854 continue;
7855 }
7856 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7857 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7858 }
7859 return getBuildVector(VT, DL, ScalarOps);
7860 }
7861
7862 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7863 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7864 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7865 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7866 Ops[0].getOperand(0).getValueType()));
7867 }
7868 }
7869
7870 // Handle fshl/fshr special cases.
7871 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7872 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7873 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7874 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7875
7876 if (C1 && C2 && C3) {
7877 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7878 return SDValue();
7879 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7880 &V3 = C3->getAPIntValue();
7881
7882 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7883 : APIntOps::fshr(V1, V2, V3);
7884 return getConstant(FoldedVal, DL, VT);
7885 }
7886 }
7887
7888 // Handle fma/fmad special cases.
7889 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7890 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7891 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7892 Ops[2].getValueType() == VT && "FMA types must match!");
7896 if (C1 && C2 && C3) {
7897 APFloat V1 = C1->getValueAPF();
7898 const APFloat &V2 = C2->getValueAPF();
7899 const APFloat &V3 = C3->getValueAPF();
7900 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7901 V1.multiply(V2, APFloat::rmNearestTiesToEven);
7903 } else
7904 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
7905 return getConstantFP(V1, DL, VT);
7906 }
7907 }
7908
7909 // This is for vector folding only from here on.
7910 if (!VT.isVector())
7911 return SDValue();
7912
7913 ElementCount NumElts = VT.getVectorElementCount();
7914
7915 // See if we can fold through any bitcasted integer ops.
7916 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7917 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7918 (Ops[0].getOpcode() == ISD::BITCAST ||
7919 Ops[1].getOpcode() == ISD::BITCAST)) {
7922 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7923 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7924 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7925 N2.getValueType().isInteger()) {
7926 bool IsLE = getDataLayout().isLittleEndian();
7927 unsigned EltBits = VT.getScalarSizeInBits();
7928 SmallVector<APInt> RawBits1, RawBits2;
7929 BitVector UndefElts1, UndefElts2;
7930 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7931 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7932 SmallVector<APInt> RawBits;
7933 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7934 std::optional<APInt> Fold = FoldValueWithUndef(
7935 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7936 if (!Fold)
7937 break;
7938 RawBits.push_back(*Fold);
7939 }
7940 if (RawBits.size() == NumElts.getFixedValue()) {
7941 // We have constant folded, but we might need to cast this again back
7942 // to the original (possibly legalized) type.
7943 EVT BVVT, BVEltVT;
7944 if (N1.getValueType() == VT) {
7945 BVVT = N1.getValueType();
7946 BVEltVT = BV1->getOperand(0).getValueType();
7947 } else {
7948 BVVT = N2.getValueType();
7949 BVEltVT = BV2->getOperand(0).getValueType();
7950 }
7951 unsigned BVEltBits = BVEltVT.getSizeInBits();
7952 SmallVector<APInt> DstBits;
7953 BitVector DstUndefs;
7955 DstBits, RawBits, DstUndefs,
7956 BitVector(RawBits.size(), false));
7957 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7958 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7959 if (DstUndefs[I])
7960 continue;
7961 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7962 }
7963 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7964 }
7965 }
7966 }
7967 // Logic ops can be folded from raw integer bits - mainly for AVX512 masks.
7968 if (ISD::isBitwiseLogicOp(Opcode) && isa<ConstantSDNode>(N1) &&
7969 isa<ConstantSDNode>(N2)) {
7970 if (SDValue Res = FoldConstantArithmetic(Opcode, DL, N1.getValueType(),
7971 {N1, N2}, Flags))
7972 return getBitcast(VT, Res);
7973 }
7974 }
7975
7976 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7977 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7978 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7979 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7980 APInt RHSVal;
7981 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7982 APInt NewStep = Opcode == ISD::MUL
7983 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7984 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7985 return getStepVector(DL, VT, NewStep);
7986 }
7987 }
7988
7989 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7990 return !Op.getValueType().isVector() ||
7991 Op.getValueType().getVectorElementCount() == NumElts;
7992 };
7993
7994 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7995 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7996 Op.getOpcode() == ISD::BUILD_VECTOR ||
7997 Op.getOpcode() == ISD::SPLAT_VECTOR;
7998 };
7999
8000 // All operands must be vector types with the same number of elements as
8001 // the result type and must be either UNDEF or a build/splat vector
8002 // or UNDEF scalars.
8003 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
8004 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
8005 return SDValue();
8006
8007 // If we are comparing vectors, then the result needs to be a i1 boolean that
8008 // is then extended back to the legal result type depending on how booleans
8009 // are represented.
8010 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
8011 ISD::NodeType ExtendCode =
8012 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
8013 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
8015
8016 // Find legal integer scalar type for constant promotion and
8017 // ensure that its scalar size is at least as large as source.
8018 EVT LegalSVT = VT.getScalarType();
8019 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
8020 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
8021 if (LegalSVT.bitsLT(VT.getScalarType()))
8022 return SDValue();
8023 }
8024
8025 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
8026 // only have one operand to check. For fixed-length vector types we may have
8027 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
8028 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
8029
8030 // Constant fold each scalar lane separately.
8031 SmallVector<SDValue, 4> ScalarResults;
8032 for (unsigned I = 0; I != NumVectorElts; I++) {
8033 SmallVector<SDValue, 4> ScalarOps;
8034 for (SDValue Op : Ops) {
8035 EVT InSVT = Op.getValueType().getScalarType();
8036 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
8037 Op.getOpcode() != ISD::SPLAT_VECTOR) {
8038 if (Op.isUndef())
8039 ScalarOps.push_back(getUNDEF(InSVT));
8040 else
8041 ScalarOps.push_back(Op);
8042 continue;
8043 }
8044
8045 SDValue ScalarOp =
8046 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
8047 EVT ScalarVT = ScalarOp.getValueType();
8048
8049 // Build vector (integer) scalar operands may need implicit
8050 // truncation - do this before constant folding.
8051 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
8052 // Don't create illegally-typed nodes unless they're constants or undef
8053 // - if we fail to constant fold we can't guarantee the (dead) nodes
8054 // we're creating will be cleaned up before being visited for
8055 // legalization.
8056 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
8057 !isa<ConstantSDNode>(ScalarOp) &&
8058 TLI->getTypeAction(*getContext(), InSVT) !=
8060 return SDValue();
8061 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
8062 }
8063
8064 ScalarOps.push_back(ScalarOp);
8065 }
8066
8067 // Constant fold the scalar operands.
8068 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
8069
8070 // Scalar folding only succeeded if the result is a constant or UNDEF.
8071 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
8072 ScalarResult.getOpcode() != ISD::ConstantFP)
8073 return SDValue();
8074
8075 // Legalize the (integer) scalar constant if necessary. We only do
8076 // this once we know the folding succeeded, since otherwise we would
8077 // get a node with illegal type which has a user.
8078 if (LegalSVT != SVT)
8079 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
8080
8081 ScalarResults.push_back(ScalarResult);
8082 }
8083
8084 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
8085 : getBuildVector(VT, DL, ScalarResults);
8086 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
8087 return V;
8088}
8089
8092 // TODO: Add support for unary/ternary fp opcodes.
8093 if (Ops.size() != 2)
8094 return SDValue();
8095
8096 // TODO: We don't do any constant folding for strict FP opcodes here, but we
8097 // should. That will require dealing with a potentially non-default
8098 // rounding mode, checking the "opStatus" return value from the APFloat
8099 // math calculations, and possibly other variations.
8100 SDValue N1 = Ops[0];
8101 SDValue N2 = Ops[1];
8102 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
8103 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
8104 if (N1CFP && N2CFP) {
8105 APFloat C1 = N1CFP->getValueAPF(); // make copy
8106 const APFloat &C2 = N2CFP->getValueAPF();
8107 switch (Opcode) {
8108 case ISD::FADD:
8110 return getConstantFP(C1, DL, VT);
8111 case ISD::FSUB:
8113 return getConstantFP(C1, DL, VT);
8114 case ISD::FMUL:
8116 return getConstantFP(C1, DL, VT);
8117 case ISD::FDIV:
8119 return getConstantFP(C1, DL, VT);
8120 case ISD::FREM:
8121 C1.mod(C2);
8122 return getConstantFP(C1, DL, VT);
8123 case ISD::FCOPYSIGN:
8124 C1.copySign(C2);
8125 return getConstantFP(C1, DL, VT);
8126 case ISD::FMINNUM:
8127 return getConstantFP(minnum(C1, C2), DL, VT);
8128 case ISD::FMAXNUM:
8129 return getConstantFP(maxnum(C1, C2), DL, VT);
8130 case ISD::FMINIMUM:
8131 return getConstantFP(minimum(C1, C2), DL, VT);
8132 case ISD::FMAXIMUM:
8133 return getConstantFP(maximum(C1, C2), DL, VT);
8134 case ISD::FMINIMUMNUM:
8135 return getConstantFP(minimumnum(C1, C2), DL, VT);
8136 case ISD::FMAXIMUMNUM:
8137 return getConstantFP(maximumnum(C1, C2), DL, VT);
8138 default: break;
8139 }
8140 }
8141 if (N1CFP && Opcode == ISD::FP_ROUND) {
8142 APFloat C1 = N1CFP->getValueAPF(); // make copy
8143 bool Unused;
8144 // This can return overflow, underflow, or inexact; we don't care.
8145 // FIXME need to be more flexible about rounding mode.
8147 &Unused);
8148 return getConstantFP(C1, DL, VT);
8149 }
8150
8151 switch (Opcode) {
8152 case ISD::FSUB:
8153 // -0.0 - undef --> undef (consistent with "fneg undef")
8154 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
8155 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
8156 return getUNDEF(VT);
8157 [[fallthrough]];
8158
8159 case ISD::FADD:
8160 case ISD::FMUL:
8161 case ISD::FDIV:
8162 case ISD::FREM:
8163 // If both operands are undef, the result is undef. If 1 operand is undef,
8164 // the result is NaN. This should match the behavior of the IR optimizer.
8165 if (N1.isUndef() && N2.isUndef())
8166 return getUNDEF(VT);
8167 if (N1.isUndef() || N2.isUndef())
8169 }
8170 return SDValue();
8171}
8172
8174 const SDLoc &DL, EVT DstEltVT) {
8175 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
8176
8177 // If this is already the right type, we're done.
8178 if (SrcEltVT == DstEltVT)
8179 return SDValue(BV, 0);
8180
8181 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
8182 unsigned DstBitSize = DstEltVT.getSizeInBits();
8183
8184 // If this is a conversion of N elements of one type to N elements of another
8185 // type, convert each element. This handles FP<->INT cases.
8186 if (SrcBitSize == DstBitSize) {
8188 for (SDValue Op : BV->op_values()) {
8189 // If the vector element type is not legal, the BUILD_VECTOR operands
8190 // are promoted and implicitly truncated. Make that explicit here.
8191 if (Op.getValueType() != SrcEltVT)
8192 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
8193 Ops.push_back(getBitcast(DstEltVT, Op));
8194 }
8195 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
8197 return getBuildVector(VT, DL, Ops);
8198 }
8199
8200 // Otherwise, we're growing or shrinking the elements. To avoid having to
8201 // handle annoying details of growing/shrinking FP values, we convert them to
8202 // int first.
8203 if (SrcEltVT.isFloatingPoint()) {
8204 // Convert the input float vector to a int vector where the elements are the
8205 // same sizes.
8206 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
8207 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
8209 DstEltVT);
8210 return SDValue();
8211 }
8212
8213 // Now we know the input is an integer vector. If the output is a FP type,
8214 // convert to integer first, then to FP of the right size.
8215 if (DstEltVT.isFloatingPoint()) {
8216 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
8217 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
8219 DstEltVT);
8220 return SDValue();
8221 }
8222
8223 // Okay, we know the src/dst types are both integers of differing types.
8224 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
8225
8226 // Extract the constant raw bit data.
8227 BitVector UndefElements;
8228 SmallVector<APInt> RawBits;
8229 bool IsLE = getDataLayout().isLittleEndian();
8230 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
8231 return SDValue();
8232
8234 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
8235 if (UndefElements[I])
8236 Ops.push_back(getUNDEF(DstEltVT));
8237 else
8238 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
8239 }
8240
8241 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
8242 return getBuildVector(VT, DL, Ops);
8243}
8244
8246 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
8247
8248 // There's no need to assert on a byte-aligned pointer. All pointers are at
8249 // least byte aligned.
8250 if (A == Align(1))
8251 return Val;
8252
8253 SDVTList VTs = getVTList(Val.getValueType());
8255 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
8256 ID.AddInteger(A.value());
8257
8258 void *IP = nullptr;
8259 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
8260 return SDValue(E, 0);
8261
8262 auto *N =
8263 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
8264 createOperands(N, {Val});
8265
8266 CSEMap.InsertNode(N, IP);
8267 InsertNode(N);
8268
8269 SDValue V(N, 0);
8270 NewSDValueDbgMsg(V, "Creating new node: ", this);
8271 return V;
8272}
8273
8274SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8275 SDValue N1, SDValue N2) {
8276 SDNodeFlags Flags;
8277 if (Inserter)
8278 Flags = Inserter->getFlags();
8279 return getNode(Opcode, DL, VT, N1, N2, Flags);
8280}
8281
8283 SDValue &N2) const {
8284 if (!TLI->isCommutativeBinOp(Opcode))
8285 return;
8286
8287 // Canonicalize:
8288 // binop(const, nonconst) -> binop(nonconst, const)
8291 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
8292 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
8293 if ((N1C && !N2C) || (N1CFP && !N2CFP))
8294 std::swap(N1, N2);
8295
8296 // Canonicalize:
8297 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
8298 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
8300 std::swap(N1, N2);
8301}
8302
8303SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8304 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
8306 N2.getOpcode() != ISD::DELETED_NODE &&
8307 "Operand is DELETED_NODE!");
8308
8309 canonicalizeCommutativeBinop(Opcode, N1, N2);
8310
8311 auto *N1C = dyn_cast<ConstantSDNode>(N1);
8312 auto *N2C = dyn_cast<ConstantSDNode>(N2);
8313
8314 // Don't allow undefs in vector splats - we might be returning N2 when folding
8315 // to zero etc.
8316 ConstantSDNode *N2CV =
8317 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
8318
8319 switch (Opcode) {
8320 default: break;
8321 case ISD::TokenFactor:
8322 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
8323 N2.getValueType() == MVT::Other && "Invalid token factor!");
8324 // Fold trivial token factors.
8325 if (N1.getOpcode() == ISD::EntryToken) return N2;
8326 if (N2.getOpcode() == ISD::EntryToken) return N1;
8327 if (N1 == N2) return N1;
8328 break;
8329 case ISD::BUILD_VECTOR: {
8330 // Attempt to simplify BUILD_VECTOR.
8331 SDValue Ops[] = {N1, N2};
8332 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8333 return V;
8334 break;
8335 }
8336 case ISD::CONCAT_VECTORS: {
8337 SDValue Ops[] = {N1, N2};
8338 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8339 return V;
8340 break;
8341 }
8342 case ISD::AND:
8343 assert(VT.isInteger() && "This operator does not apply to FP types!");
8344 assert(N1.getValueType() == N2.getValueType() &&
8345 N1.getValueType() == VT && "Binary operator types must match!");
8346 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
8347 // worth handling here.
8348 if (N2CV && N2CV->isZero())
8349 return N2;
8350 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
8351 return N1;
8352 break;
8353 case ISD::OR:
8354 case ISD::XOR:
8355 case ISD::ADD:
8356 case ISD::PTRADD:
8357 case ISD::SUB:
8358 assert(VT.isInteger() && "This operator does not apply to FP types!");
8359 assert(N1.getValueType() == N2.getValueType() &&
8360 N1.getValueType() == VT && "Binary operator types must match!");
8361 // The equal operand types requirement is unnecessarily strong for PTRADD.
8362 // However, the SelectionDAGBuilder does not generate PTRADDs with different
8363 // operand types, and we'd need to re-implement GEP's non-standard wrapping
8364 // logic everywhere where PTRADDs may be folded or combined to properly
8365 // support them. If/when we introduce pointer types to the SDAG, we will
8366 // need to relax this constraint.
8367
8368 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
8369 // it's worth handling here.
8370 if (N2CV && N2CV->isZero())
8371 return N1;
8372 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
8373 VT.getScalarType() == MVT::i1)
8374 return getNode(ISD::XOR, DL, VT, N1, N2);
8375 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
8376 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
8377 N2.getOpcode() == ISD::VSCALE) {
8378 const APInt &C1 = N1->getConstantOperandAPInt(0);
8379 const APInt &C2 = N2->getConstantOperandAPInt(0);
8380 return getVScale(DL, VT, C1 + C2);
8381 }
8382 break;
8383 case ISD::MUL:
8384 assert(VT.isInteger() && "This operator does not apply to FP types!");
8385 assert(N1.getValueType() == N2.getValueType() &&
8386 N1.getValueType() == VT && "Binary operator types must match!");
8387 if (VT.getScalarType() == MVT::i1)
8388 return getNode(ISD::AND, DL, VT, N1, N2);
8389 if (N2CV && N2CV->isZero())
8390 return N2;
8391 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
8392 const APInt &MulImm = N1->getConstantOperandAPInt(0);
8393 const APInt &N2CImm = N2C->getAPIntValue();
8394 return getVScale(DL, VT, MulImm * N2CImm);
8395 }
8396 break;
8397 case ISD::UDIV:
8398 case ISD::UREM:
8399 case ISD::MULHU:
8400 case ISD::MULHS:
8401 case ISD::SDIV:
8402 case ISD::SREM:
8403 case ISD::SADDSAT:
8404 case ISD::SSUBSAT:
8405 case ISD::UADDSAT:
8406 case ISD::USUBSAT:
8407 assert(VT.isInteger() && "This operator does not apply to FP types!");
8408 assert(N1.getValueType() == N2.getValueType() &&
8409 N1.getValueType() == VT && "Binary operator types must match!");
8410 if (VT.getScalarType() == MVT::i1) {
8411 // fold (add_sat x, y) -> (or x, y) for bool types.
8412 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
8413 return getNode(ISD::OR, DL, VT, N1, N2);
8414 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
8415 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
8416 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
8417 }
8418 break;
8419 case ISD::SCMP:
8420 case ISD::UCMP:
8421 assert(N1.getValueType() == N2.getValueType() &&
8422 "Types of operands of UCMP/SCMP must match");
8423 assert(N1.getValueType().isVector() == VT.isVector() &&
8424 "Operands and return type of must both be scalars or vectors");
8425 if (VT.isVector())
8428 "Result and operands must have the same number of elements");
8429 break;
8430 case ISD::AVGFLOORS:
8431 case ISD::AVGFLOORU:
8432 case ISD::AVGCEILS:
8433 case ISD::AVGCEILU:
8434 assert(VT.isInteger() && "This operator does not apply to FP types!");
8435 assert(N1.getValueType() == N2.getValueType() &&
8436 N1.getValueType() == VT && "Binary operator types must match!");
8437 break;
8438 case ISD::ABDS:
8439 case ISD::ABDU:
8440 assert(VT.isInteger() && "This operator does not apply to FP types!");
8441 assert(N1.getValueType() == N2.getValueType() &&
8442 N1.getValueType() == VT && "Binary operator types must match!");
8443 if (VT.getScalarType() == MVT::i1)
8444 return getNode(ISD::XOR, DL, VT, N1, N2);
8445 break;
8446 case ISD::SMIN:
8447 case ISD::UMAX:
8448 assert(VT.isInteger() && "This operator does not apply to FP types!");
8449 assert(N1.getValueType() == N2.getValueType() &&
8450 N1.getValueType() == VT && "Binary operator types must match!");
8451 if (VT.getScalarType() == MVT::i1)
8452 return getNode(ISD::OR, DL, VT, N1, N2);
8453 break;
8454 case ISD::SMAX:
8455 case ISD::UMIN:
8456 assert(VT.isInteger() && "This operator does not apply to FP types!");
8457 assert(N1.getValueType() == N2.getValueType() &&
8458 N1.getValueType() == VT && "Binary operator types must match!");
8459 if (VT.getScalarType() == MVT::i1)
8460 return getNode(ISD::AND, DL, VT, N1, N2);
8461 break;
8462 case ISD::FADD:
8463 case ISD::FSUB:
8464 case ISD::FMUL:
8465 case ISD::FDIV:
8466 case ISD::FREM:
8467 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
8468 assert(N1.getValueType() == N2.getValueType() &&
8469 N1.getValueType() == VT && "Binary operator types must match!");
8470 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
8471 return V;
8472 break;
8473 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
8474 assert(N1.getValueType() == VT &&
8477 "Invalid FCOPYSIGN!");
8478 break;
8479 case ISD::SHL:
8480 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
8481 const APInt &MulImm = N1->getConstantOperandAPInt(0);
8482 const APInt &ShiftImm = N2C->getAPIntValue();
8483 return getVScale(DL, VT, MulImm << ShiftImm);
8484 }
8485 [[fallthrough]];
8486 case ISD::SRA:
8487 case ISD::SRL:
8488 if (SDValue V = simplifyShift(N1, N2))
8489 return V;
8490 [[fallthrough]];
8491 case ISD::ROTL:
8492 case ISD::ROTR:
8493 case ISD::SSHLSAT:
8494 case ISD::USHLSAT:
8495 assert(VT == N1.getValueType() &&
8496 "Shift operators return type must be the same as their first arg");
8497 assert(VT.isInteger() && N2.getValueType().isInteger() &&
8498 "Shifts only work on integers");
8499 assert((!VT.isVector() || VT == N2.getValueType()) &&
8500 "Vector shift amounts must be in the same as their first arg");
8501 // Verify that the shift amount VT is big enough to hold valid shift
8502 // amounts. This catches things like trying to shift an i1024 value by an
8503 // i8, which is easy to fall into in generic code that uses
8504 // TLI.getShiftAmount().
8507 "Invalid use of small shift amount with oversized value!");
8508
8509 // Always fold shifts of i1 values so the code generator doesn't need to
8510 // handle them. Since we know the size of the shift has to be less than the
8511 // size of the value, the shift/rotate count is guaranteed to be zero.
8512 if (VT == MVT::i1)
8513 return N1;
8514 if (N2CV && N2CV->isZero())
8515 return N1;
8516 break;
8517 case ISD::FP_ROUND:
8519 VT.bitsLE(N1.getValueType()) && N2C &&
8520 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
8521 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
8522 if (N1.getValueType() == VT) return N1; // noop conversion.
8523 break;
8524 case ISD::IS_FPCLASS: {
8526 "IS_FPCLASS is used for a non-floating type");
8527 assert(isa<ConstantSDNode>(N2) && "FPClassTest is not Constant");
8528 // is.fpclass(poison, mask) -> poison
8529 if (N1.getOpcode() == ISD::POISON)
8530 return getPOISON(VT);
8531 FPClassTest Mask = static_cast<FPClassTest>(N2->getAsZExtVal());
8532 // If all tests are made, it doesn't matter what the value is.
8533 if ((Mask & fcAllFlags) == fcAllFlags)
8534 return getBoolConstant(true, DL, VT, N1.getValueType());
8535 if ((Mask & fcAllFlags) == 0)
8536 return getBoolConstant(false, DL, VT, N1.getValueType());
8537 break;
8538 }
8539 case ISD::AssertNoFPClass: {
8541 "AssertNoFPClass is used for a non-floating type");
8542 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
8543 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
8544 assert(llvm::to_underlying(NoFPClass) <=
8546 "FPClassTest value too large");
8547 (void)NoFPClass;
8548 break;
8549 }
8550 case ISD::AssertSext:
8551 case ISD::AssertZext: {
8552 EVT EVT = cast<VTSDNode>(N2)->getVT();
8553 assert(VT == N1.getValueType() && "Not an inreg extend!");
8554 assert(VT.isInteger() && EVT.isInteger() &&
8555 "Cannot *_EXTEND_INREG FP types");
8556 assert(!EVT.isVector() &&
8557 "AssertSExt/AssertZExt type should be the vector element type "
8558 "rather than the vector type!");
8559 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
8560 if (VT.getScalarType() == EVT) return N1; // noop assertion.
8561 break;
8562 }
8564 EVT EVT = cast<VTSDNode>(N2)->getVT();
8565 assert(VT == N1.getValueType() && "Not an inreg extend!");
8566 assert(VT.isInteger() && EVT.isInteger() &&
8567 "Cannot *_EXTEND_INREG FP types");
8568 assert(EVT.isVector() == VT.isVector() &&
8569 "SIGN_EXTEND_INREG type should be vector iff the operand "
8570 "type is vector!");
8571 assert((!EVT.isVector() ||
8573 "Vector element counts must match in SIGN_EXTEND_INREG");
8574 assert(EVT.getScalarType().bitsLE(VT.getScalarType()) && "Not extending!");
8575 if (EVT == VT) return N1; // Not actually extending
8576 break;
8577 }
8579 case ISD::FP_TO_UINT_SAT: {
8580 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
8581 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
8582 assert(N1.getValueType().isVector() == VT.isVector() &&
8583 "FP_TO_*INT_SAT type should be vector iff the operand type is "
8584 "vector!");
8585 assert((!VT.isVector() || VT.getVectorElementCount() ==
8587 "Vector element counts must match in FP_TO_*INT_SAT");
8588 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
8589 "Type to saturate to must be a scalar.");
8590 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
8591 "Not extending!");
8592 break;
8593 }
8596 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
8597 element type of the vector.");
8598
8599 // Extract from an undefined value or using an undefined index is undefined.
8600 if (N1.isUndef() || N2.isUndef())
8601 return getUNDEF(VT);
8602
8603 // EXTRACT_VECTOR_ELT of out-of-bounds element is POISON for fixed length
8604 // vectors. For scalable vectors we will provide appropriate support for
8605 // dealing with arbitrary indices.
8606 if (N2C && N1.getValueType().isFixedLengthVector() &&
8607 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
8608 return getPOISON(VT);
8609
8610 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
8611 // expanding copies of large vectors from registers. This only works for
8612 // fixed length vectors, since we need to know the exact number of
8613 // elements.
8614 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
8616 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
8617 return getExtractVectorElt(DL, VT,
8618 N1.getOperand(N2C->getZExtValue() / Factor),
8619 N2C->getZExtValue() % Factor);
8620 }
8621
8622 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
8623 // lowering is expanding large vector constants.
8624 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
8625 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
8628 "BUILD_VECTOR used for scalable vectors");
8629 unsigned Index =
8630 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
8631 SDValue Elt = N1.getOperand(Index);
8632
8633 if (VT != Elt.getValueType())
8634 // If the vector element type is not legal, the BUILD_VECTOR operands
8635 // are promoted and implicitly truncated, and the result implicitly
8636 // extended. Make that explicit here.
8637 Elt = getAnyExtOrTrunc(Elt, DL, VT);
8638
8639 return Elt;
8640 }
8641
8642 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
8643 // operations are lowered to scalars.
8644 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
8645 // If the indices are the same, return the inserted element else
8646 // if the indices are known different, extract the element from
8647 // the original vector.
8648 SDValue N1Op2 = N1.getOperand(2);
8650
8651 if (N1Op2C && N2C) {
8652 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
8653 if (VT == N1.getOperand(1).getValueType())
8654 return N1.getOperand(1);
8655 if (VT.isFloatingPoint()) {
8657 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
8658 }
8659 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
8660 }
8661 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
8662 }
8663 }
8664
8665 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
8666 // when vector types are scalarized and v1iX is legal.
8667 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
8668 // Here we are completely ignoring the extract element index (N2),
8669 // which is fine for fixed width vectors, since any index other than 0
8670 // is undefined anyway. However, this cannot be ignored for scalable
8671 // vectors - in theory we could support this, but we don't want to do this
8672 // without a profitability check.
8673 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8675 N1.getValueType().getVectorNumElements() == 1) {
8676 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
8677 N1.getOperand(1));
8678 }
8679 break;
8681 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
8682 assert(!N1.getValueType().isVector() && !VT.isVector() &&
8683 (N1.getValueType().isInteger() == VT.isInteger()) &&
8684 N1.getValueType() != VT &&
8685 "Wrong types for EXTRACT_ELEMENT!");
8686
8687 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
8688 // 64-bit integers into 32-bit parts. Instead of building the extract of
8689 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
8690 if (N1.getOpcode() == ISD::BUILD_PAIR)
8691 return N1.getOperand(N2C->getZExtValue());
8692
8693 // EXTRACT_ELEMENT of a constant int is also very common.
8694 if (N1C) {
8695 unsigned ElementSize = VT.getSizeInBits();
8696 unsigned Shift = ElementSize * N2C->getZExtValue();
8697 const APInt &Val = N1C->getAPIntValue();
8698 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
8699 }
8700 break;
8702 EVT N1VT = N1.getValueType();
8703 assert(VT.isVector() && N1VT.isVector() &&
8704 "Extract subvector VTs must be vectors!");
8706 "Extract subvector VTs must have the same element type!");
8707 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8708 "Cannot extract a scalable vector from a fixed length vector!");
8709 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8711 "Extract subvector must be from larger vector to smaller vector!");
8712 assert(N2C && "Extract subvector index must be a constant");
8713 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8714 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8715 N1VT.getVectorMinNumElements()) &&
8716 "Extract subvector overflow!");
8717 assert(N2C->getAPIntValue().getBitWidth() ==
8718 TLI->getVectorIdxWidth(getDataLayout()) &&
8719 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8720 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8721 "Extract index is not a multiple of the output vector length");
8722
8723 // Trivial extraction.
8724 if (VT == N1VT)
8725 return N1;
8726
8727 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8728 if (N1.isUndef())
8729 return getUNDEF(VT);
8730
8731 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8732 // the concat have the same type as the extract.
8733 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8734 VT == N1.getOperand(0).getValueType()) {
8735 unsigned Factor = VT.getVectorMinNumElements();
8736 return N1.getOperand(N2C->getZExtValue() / Factor);
8737 }
8738
8739 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8740 // during shuffle legalization.
8741 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8742 VT == N1.getOperand(1).getValueType())
8743 return N1.getOperand(1);
8744 break;
8745 }
8746 }
8747
8748 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8749 switch (Opcode) {
8750 case ISD::XOR:
8751 case ISD::ADD:
8752 case ISD::PTRADD:
8753 case ISD::SUB:
8755 case ISD::UDIV:
8756 case ISD::SDIV:
8757 case ISD::UREM:
8758 case ISD::SREM:
8759 case ISD::MUL:
8760 case ISD::AND:
8761 case ISD::SSUBSAT:
8762 case ISD::USUBSAT:
8763 case ISD::UMIN:
8764 case ISD::OR:
8765 case ISD::SADDSAT:
8766 case ISD::UADDSAT:
8767 case ISD::UMAX:
8768 case ISD::SMAX:
8769 case ISD::SMIN:
8770 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8771 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8772 }
8773 }
8774
8775 // Canonicalize an UNDEF to the RHS, even over a constant.
8776 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8777 if (TLI->isCommutativeBinOp(Opcode)) {
8778 std::swap(N1, N2);
8779 } else {
8780 switch (Opcode) {
8781 case ISD::PTRADD:
8782 case ISD::SUB:
8783 // fold op(undef, non_undef_arg2) -> undef.
8784 return N1;
8786 case ISD::UDIV:
8787 case ISD::SDIV:
8788 case ISD::UREM:
8789 case ISD::SREM:
8790 case ISD::SSUBSAT:
8791 case ISD::USUBSAT:
8792 // fold op(undef, non_undef_arg2) -> 0.
8793 return getConstant(0, DL, VT);
8794 }
8795 }
8796 }
8797
8798 // Fold a bunch of operators when the RHS is undef.
8799 if (N2.getOpcode() == ISD::UNDEF) {
8800 switch (Opcode) {
8801 case ISD::XOR:
8802 if (N1.getOpcode() == ISD::UNDEF)
8803 // Handle undef ^ undef -> 0 special case. This is a common
8804 // idiom (misuse).
8805 return getConstant(0, DL, VT);
8806 [[fallthrough]];
8807 case ISD::ADD:
8808 case ISD::PTRADD:
8809 case ISD::SUB:
8810 // fold op(arg1, undef) -> undef.
8811 return N2;
8812 case ISD::UDIV:
8813 case ISD::SDIV:
8814 case ISD::UREM:
8815 case ISD::SREM:
8816 // fold op(arg1, undef) -> poison.
8817 return getPOISON(VT);
8818 case ISD::MUL:
8819 case ISD::AND:
8820 case ISD::SSUBSAT:
8821 case ISD::USUBSAT:
8822 case ISD::UMIN:
8823 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8824 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8825 case ISD::OR:
8826 case ISD::SADDSAT:
8827 case ISD::UADDSAT:
8828 case ISD::UMAX:
8829 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8830 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8831 case ISD::SMAX:
8832 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8833 return N1.getOpcode() == ISD::UNDEF
8834 ? N2
8835 : getConstant(
8837 VT);
8838 case ISD::SMIN:
8839 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8840 return N1.getOpcode() == ISD::UNDEF
8841 ? N2
8842 : getConstant(
8844 VT);
8845 }
8846 }
8847
8848 // Perform trivial constant folding.
8849 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8850 return SV;
8851
8852 // Memoize this node if possible.
8853 SDNode *N;
8854 SDVTList VTs = getVTList(VT);
8855 SDValue Ops[] = {N1, N2};
8856 if (VT != MVT::Glue) {
8858 AddNodeIDNode(ID, Opcode, VTs, Ops);
8859 void *IP = nullptr;
8860 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8861 E->intersectFlagsWith(Flags);
8862 return SDValue(E, 0);
8863 }
8864
8865 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8866 N->setFlags(Flags);
8867 createOperands(N, Ops);
8868 CSEMap.InsertNode(N, IP);
8869 } else {
8870 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8871 createOperands(N, Ops);
8872 }
8873
8874 InsertNode(N);
8875 SDValue V = SDValue(N, 0);
8876 NewSDValueDbgMsg(V, "Creating new node: ", this);
8877 return V;
8878}
8879
8880SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8881 SDValue N1, SDValue N2, SDValue N3) {
8882 SDNodeFlags Flags;
8883 if (Inserter)
8884 Flags = Inserter->getFlags();
8885 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8886}
8887
8888SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8889 SDValue N1, SDValue N2, SDValue N3,
8890 const SDNodeFlags Flags) {
8892 N2.getOpcode() != ISD::DELETED_NODE &&
8893 N3.getOpcode() != ISD::DELETED_NODE &&
8894 "Operand is DELETED_NODE!");
8895 // Perform various simplifications.
8896 switch (Opcode) {
8897 case ISD::BUILD_VECTOR: {
8898 // Attempt to simplify BUILD_VECTOR.
8899 SDValue Ops[] = {N1, N2, N3};
8900 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8901 return V;
8902 break;
8903 }
8904 case ISD::CONCAT_VECTORS: {
8905 SDValue Ops[] = {N1, N2, N3};
8906 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8907 return V;
8908 break;
8909 }
8910 case ISD::SETCC: {
8911 assert(VT.isInteger() && "SETCC result type must be an integer!");
8912 assert(N1.getValueType() == N2.getValueType() &&
8913 "SETCC operands must have the same type!");
8914 assert(VT.isVector() == N1.getValueType().isVector() &&
8915 "SETCC type should be vector iff the operand type is vector!");
8916 assert((!VT.isVector() || VT.getVectorElementCount() ==
8918 "SETCC vector element counts must match!");
8919 // Use FoldSetCC to simplify SETCC's.
8920 if (SDValue V =
8921 FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL, Flags))
8922 return V;
8923 break;
8924 }
8925 case ISD::SELECT:
8926 case ISD::VSELECT:
8927 if (SDValue V = simplifySelect(N1, N2, N3))
8928 return V;
8929 break;
8931 llvm_unreachable("should use getVectorShuffle constructor!");
8933 if (isNullConstant(N3))
8934 return N1;
8935 break;
8937 if (isNullConstant(N3))
8938 return N2;
8939 break;
8941 assert(VT.isVector() && VT == N1.getValueType() &&
8942 "INSERT_VECTOR_ELT vector type mismatch");
8944 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8945 assert((!VT.isFloatingPoint() ||
8946 VT.getVectorElementType() == N2.getValueType()) &&
8947 "INSERT_VECTOR_ELT fp scalar type mismatch");
8948 assert((!VT.isInteger() ||
8950 "INSERT_VECTOR_ELT int scalar size mismatch");
8951
8952 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8953 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8954 // for scalable vectors where we will generate appropriate code to
8955 // deal with out-of-bounds cases correctly.
8956 if (N3C && VT.isFixedLengthVector() &&
8957 N3C->getZExtValue() >= VT.getVectorNumElements())
8958 return getUNDEF(VT);
8959
8960 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8961 if (N3.isUndef())
8962 return getUNDEF(VT);
8963
8964 // If inserting poison, just use the input vector.
8965 if (N2.getOpcode() == ISD::POISON)
8966 return N1;
8967
8968 // Inserting undef into undef/poison is still undef.
8969 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8970 return getUNDEF(VT);
8971
8972 // If the inserted element is an UNDEF, just use the input vector.
8973 // But not if skipping the insert could make the result more poisonous.
8974 if (N2.isUndef()) {
8975 if (N3C && VT.isFixedLengthVector()) {
8976 APInt EltMask =
8977 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8978 if (isGuaranteedNotToBePoison(N1, EltMask))
8979 return N1;
8980 } else if (isGuaranteedNotToBePoison(N1))
8981 return N1;
8982 }
8983 break;
8984 }
8985 case ISD::INSERT_SUBVECTOR: {
8986 // If inserting poison, just use the input vector,
8987 if (N2.getOpcode() == ISD::POISON)
8988 return N1;
8989
8990 // Inserting undef into undef/poison is still undef.
8991 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8992 return getUNDEF(VT);
8993
8994 EVT N2VT = N2.getValueType();
8995 assert(VT == N1.getValueType() &&
8996 "Dest and insert subvector source types must match!");
8997 assert(VT.isVector() && N2VT.isVector() &&
8998 "Insert subvector VTs must be vectors!");
9000 "Insert subvector VTs must have the same element type!");
9001 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
9002 "Cannot insert a scalable vector into a fixed length vector!");
9003 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
9005 "Insert subvector must be from smaller vector to larger vector!");
9007 "Insert subvector index must be constant");
9008 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
9009 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
9011 "Insert subvector overflow!");
9013 TLI->getVectorIdxWidth(getDataLayout()) &&
9014 "Constant index for INSERT_SUBVECTOR has an invalid size");
9015
9016 // Trivial insertion.
9017 if (VT == N2VT)
9018 return N2;
9019
9020 // If this is an insert of an extracted vector into an undef/poison vector,
9021 // we can just use the input to the extract. But not if skipping the
9022 // extract+insert could make the result more poisonous.
9023 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
9024 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
9025 if (N1.getOpcode() == ISD::POISON)
9026 return N2.getOperand(0);
9027 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
9028 unsigned LoBit = N3->getAsZExtVal();
9029 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
9030 APInt EltMask =
9031 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
9032 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
9033 return N2.getOperand(0);
9034 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
9035 return N2.getOperand(0);
9036 }
9037
9038 // If the inserted subvector is UNDEF, just use the input vector.
9039 // But not if skipping the insert could make the result more poisonous.
9040 if (N2.isUndef()) {
9041 if (VT.isFixedLengthVector()) {
9042 unsigned LoBit = N3->getAsZExtVal();
9043 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
9044 APInt EltMask =
9045 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
9046 if (isGuaranteedNotToBePoison(N1, EltMask))
9047 return N1;
9048 } else if (isGuaranteedNotToBePoison(N1))
9049 return N1;
9050 }
9051 break;
9052 }
9053 case ISD::BITCAST:
9054 // Fold bit_convert nodes from a type to themselves.
9055 if (N1.getValueType() == VT)
9056 return N1;
9057 break;
9058 case ISD::VP_TRUNCATE:
9059 case ISD::VP_SIGN_EXTEND:
9060 case ISD::VP_ZERO_EXTEND:
9061 // Don't create noop casts.
9062 if (N1.getValueType() == VT)
9063 return N1;
9064 break;
9065 case ISD::VECTOR_COMPRESS: {
9066 [[maybe_unused]] EVT VecVT = N1.getValueType();
9067 [[maybe_unused]] EVT MaskVT = N2.getValueType();
9068 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
9069 assert(VT == VecVT && "Vector and result type don't match.");
9070 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
9071 "All inputs must be vectors.");
9072 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
9074 "Vector and mask must have same number of elements.");
9075
9076 if (N1.isUndef() || N2.isUndef())
9077 return N3;
9078
9079 break;
9080 }
9085 [[maybe_unused]] EVT AccVT = N1.getValueType();
9086 [[maybe_unused]] EVT Input1VT = N2.getValueType();
9087 [[maybe_unused]] EVT Input2VT = N3.getValueType();
9088 assert(Input1VT.isVector() && Input1VT == Input2VT &&
9089 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
9090 "node to have the same type!");
9091 assert(VT.isVector() && VT == AccVT &&
9092 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
9093 "the same type as its result!");
9095 AccVT.getVectorElementCount()) &&
9096 "Expected the element count of the second and third operands of the "
9097 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
9098 "element count of the first operand and the result!");
9100 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
9101 "node to have an element type which is the same as or smaller than "
9102 "the element type of the first operand and result!");
9103 break;
9104 }
9105 }
9106
9107 // Perform trivial constant folding for arithmetic operators.
9108 switch (Opcode) {
9109 case ISD::FMA:
9110 case ISD::FMAD:
9111 case ISD::SETCC:
9112 case ISD::FSHL:
9113 case ISD::FSHR:
9114 if (SDValue SV =
9115 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
9116 return SV;
9117 break;
9118 }
9119
9120 // Memoize node if it doesn't produce a glue result.
9121 SDNode *N;
9122 SDVTList VTs = getVTList(VT);
9123 SDValue Ops[] = {N1, N2, N3};
9124 if (VT != MVT::Glue) {
9126 AddNodeIDNode(ID, Opcode, VTs, Ops);
9127 void *IP = nullptr;
9128 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9129 E->intersectFlagsWith(Flags);
9130 return SDValue(E, 0);
9131 }
9132
9133 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
9134 N->setFlags(Flags);
9135 createOperands(N, Ops);
9136 CSEMap.InsertNode(N, IP);
9137 } else {
9138 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
9139 createOperands(N, Ops);
9140 }
9141
9142 InsertNode(N);
9143 SDValue V = SDValue(N, 0);
9144 NewSDValueDbgMsg(V, "Creating new node: ", this);
9145 return V;
9146}
9147
9148SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9149 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
9150 const SDNodeFlags Flags) {
9151 SDValue Ops[] = { N1, N2, N3, N4 };
9152 return getNode(Opcode, DL, VT, Ops, Flags);
9153}
9154
9155SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9156 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
9157 SDNodeFlags Flags;
9158 if (Inserter)
9159 Flags = Inserter->getFlags();
9160 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
9161}
9162
9163SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9164 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
9165 SDValue N5, const SDNodeFlags Flags) {
9166 SDValue Ops[] = { N1, N2, N3, N4, N5 };
9167 return getNode(Opcode, DL, VT, Ops, Flags);
9168}
9169
9170SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9171 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
9172 SDValue N5) {
9173 SDNodeFlags Flags;
9174 if (Inserter)
9175 Flags = Inserter->getFlags();
9176 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
9177}
9178
9179/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
9180/// the incoming stack arguments to be loaded from the stack.
9182 SmallVector<SDValue, 8> ArgChains;
9183
9184 // Include the original chain at the beginning of the list. When this is
9185 // used by target LowerCall hooks, this helps legalize find the
9186 // CALLSEQ_BEGIN node.
9187 ArgChains.push_back(Chain);
9188
9189 // Add a chain value for each stack argument.
9190 for (SDNode *U : getEntryNode().getNode()->users())
9191 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
9192 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
9193 if (FI->getIndex() < 0)
9194 ArgChains.push_back(SDValue(L, 1));
9195
9196 // Build a tokenfactor for all the chains.
9197 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
9198}
9199
9200/// getMemsetValue - Vectorized representation of the memset value
9201/// operand.
9203 const SDLoc &dl) {
9204 assert(!Value.isUndef());
9205
9206 unsigned NumBits = VT.getScalarSizeInBits();
9208 assert(C->getAPIntValue().getBitWidth() == 8);
9209 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
9210 if (VT.isInteger()) {
9211 bool IsOpaque = VT.getSizeInBits() > 64 ||
9212 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
9213 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
9214 }
9215 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
9216 }
9217
9218 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
9219 EVT IntVT = VT.getScalarType();
9220 if (!IntVT.isInteger())
9221 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
9222
9223 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
9224 if (NumBits > 8) {
9225 // Use a multiplication with 0x010101... to extend the input to the
9226 // required length.
9227 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
9228 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
9229 DAG.getConstant(Magic, dl, IntVT));
9230 }
9231
9232 if (VT != Value.getValueType() && !VT.isInteger())
9233 Value = DAG.getBitcast(VT.getScalarType(), Value);
9234 if (VT != Value.getValueType())
9235 Value = DAG.getSplatBuildVector(VT, dl, Value);
9236
9237 return Value;
9238}
9239
9240/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
9241/// used when a memcpy is turned into a memset when the source is a constant
9242/// string ptr.
9244 const TargetLowering &TLI,
9245 const ConstantDataArraySlice &Slice) {
9246 // Handle vector with all elements zero.
9247 if (Slice.Array == nullptr) {
9248 if (VT.isInteger())
9249 return DAG.getConstant(0, dl, VT);
9250 return DAG.getNode(ISD::BITCAST, dl, VT,
9251 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
9252 }
9253
9254 assert(!VT.isVector() && "Can't handle vector type here!");
9255 unsigned NumVTBits = VT.getSizeInBits();
9256 unsigned NumVTBytes = NumVTBits / 8;
9257 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
9258
9259 APInt Val(NumVTBits, 0);
9260 if (DAG.getDataLayout().isLittleEndian()) {
9261 for (unsigned i = 0; i != NumBytes; ++i)
9262 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
9263 } else {
9264 for (unsigned i = 0; i != NumBytes; ++i)
9265 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
9266 }
9267
9268 // If the "cost" of materializing the integer immediate is less than the cost
9269 // of a load, then it is cost effective to turn the load into the immediate.
9270 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
9271 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
9272 return DAG.getConstant(Val, dl, VT);
9273 return SDValue();
9274}
9275
9277 const SDLoc &DL,
9278 const SDNodeFlags Flags) {
9279 SDValue Index = getTypeSize(DL, Base.getValueType(), Offset);
9280 return getMemBasePlusOffset(Base, Index, DL, Flags);
9281}
9282
9284 const SDLoc &DL,
9285 const SDNodeFlags Flags) {
9286 assert(Offset.getValueType().isInteger());
9287 EVT BasePtrVT = Ptr.getValueType();
9288 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
9289 BasePtrVT))
9290 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
9291 // InBounds only applies to PTRADD, don't set it if we generate ADD.
9292 SDNodeFlags AddFlags = Flags;
9293 AddFlags.setInBounds(false);
9294 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
9295}
9296
9297/// Returns true if memcpy source is constant data.
9299 uint64_t SrcDelta = 0;
9300 GlobalAddressSDNode *G = nullptr;
9301 if (Src.getOpcode() == ISD::GlobalAddress)
9303 else if (Src->isAnyAdd() &&
9304 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
9305 Src.getOperand(1).getOpcode() == ISD::Constant) {
9306 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
9307 SrcDelta = Src.getConstantOperandVal(1);
9308 }
9309 if (!G)
9310 return false;
9311
9312 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
9313 SrcDelta + G->getOffset());
9314}
9315
9317 SelectionDAG &DAG) {
9318 // On Darwin, -Os means optimize for size without hurting performance, so
9319 // only really optimize for size when -Oz (MinSize) is used.
9321 return MF.getFunction().hasMinSize();
9322 return DAG.shouldOptForSize();
9323}
9324
9326 SmallVector<SDValue, 32> &OutChains, unsigned From,
9327 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
9328 SmallVector<SDValue, 16> &OutStoreChains) {
9329 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
9330 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
9331 SmallVector<SDValue, 16> GluedLoadChains;
9332 for (unsigned i = From; i < To; ++i) {
9333 OutChains.push_back(OutLoadChains[i]);
9334 GluedLoadChains.push_back(OutLoadChains[i]);
9335 }
9336
9337 // Chain for all loads.
9338 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
9339 GluedLoadChains);
9340
9341 for (unsigned i = From; i < To; ++i) {
9342 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
9343 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
9344 ST->getBasePtr(), ST->getMemoryVT(),
9345 ST->getMemOperand());
9346 OutChains.push_back(NewStore);
9347 }
9348}
9349
9350static SDValue
9352 SDValue Dst, SDValue Src, uint64_t Size, Align DstAlign,
9353 Align SrcAlign, bool isVol, bool AlwaysInline,
9354 MachinePointerInfo DstPtrInfo,
9355 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9356 BatchAAResults *BatchAA) {
9357 // Turn a memcpy of undef to nop.
9358 // FIXME: We need to honor volatile even is Src is undef.
9359 if (Src.isUndef())
9360 return Chain;
9361
9362 // Expand memcpy to a series of load and store ops if the size operand falls
9363 // below a certain threshold.
9364 // TODO: In the AlwaysInline case, if the size is big then generate a loop
9365 // rather than maybe a humongous number of loads and stores.
9366 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9367 const DataLayout &DL = DAG.getDataLayout();
9368 LLVMContext &C = *DAG.getContext();
9369 std::vector<EVT> MemOps;
9370 bool DstAlignCanChange = false;
9372 MachineFrameInfo &MFI = MF.getFrameInfo();
9373 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9375 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9376 DstAlignCanChange = true;
9377 SrcAlign = std::max(SrcAlign, DAG.InferPtrAlign(Src).valueOrOne());
9379 // If marked as volatile, perform a copy even when marked as constant.
9380 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
9381 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
9382 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
9383 const MemOp Op = isZeroConstant
9384 ? MemOp::Set(Size, DstAlignCanChange, DstAlign,
9385 /*IsZeroMemset*/ true, isVol)
9386 : MemOp::Copy(Size, DstAlignCanChange, DstAlign,
9387 SrcAlign, isVol, CopyFromConstant);
9388 if (!TLI.findOptimalMemOpLowering(
9389 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
9390 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes(), nullptr))
9391 return SDValue();
9392
9393 if (DstAlignCanChange) {
9394 Type *Ty = MemOps[0].getTypeForEVT(C);
9395 Align NewDstAlign = DL.getABITypeAlign(Ty);
9396
9397 // Don't promote to an alignment that would require dynamic stack
9398 // realignment which may conflict with optimizations such as tail call
9399 // optimization.
9401 if (!TRI->hasStackRealignment(MF))
9402 if (MaybeAlign StackAlign = DL.getStackAlignment())
9403 NewDstAlign = std::min(NewDstAlign, *StackAlign);
9404
9405 if (NewDstAlign > DstAlign) {
9406 // Give the stack frame object a larger alignment if needed.
9407 if (MFI.getObjectAlign(FI->getIndex()) < NewDstAlign)
9408 MFI.setObjectAlignment(FI->getIndex(), NewDstAlign);
9409 DstAlign = NewDstAlign;
9410 }
9411 }
9412
9413 // Prepare AAInfo for loads/stores after lowering this memcpy.
9414 AAMDNodes NewAAInfo = AAInfo;
9415 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9416
9417 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
9418 bool isConstant =
9419 BatchAA && SrcVal &&
9420 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
9421
9422 MachineMemOperand::Flags MMOFlags =
9424 SmallVector<SDValue, 16> OutLoadChains;
9425 SmallVector<SDValue, 16> OutStoreChains;
9426 SmallVector<SDValue, 32> OutChains;
9427 unsigned NumMemOps = MemOps.size();
9428 uint64_t SrcOff = 0, DstOff = 0;
9429 for (unsigned i = 0; i != NumMemOps; ++i) {
9430 EVT VT = MemOps[i];
9431 unsigned VTSize = VT.getSizeInBits() / 8;
9432 SDValue Value, Store;
9433
9434 if (VTSize > Size) {
9435 // Issuing an unaligned load / store pair that overlaps with the previous
9436 // pair. Adjust the offset accordingly.
9437 assert(i == NumMemOps-1 && i != 0);
9438 SrcOff -= VTSize - Size;
9439 DstOff -= VTSize - Size;
9440 }
9441
9442 if (CopyFromConstant &&
9443 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
9444 // It's unlikely a store of a vector immediate can be done in a single
9445 // instruction. It would require a load from a constantpool first.
9446 // We only handle zero vectors here.
9447 // FIXME: Handle other cases where store of vector immediate is done in
9448 // a single instruction.
9449 ConstantDataArraySlice SubSlice;
9450 if (SrcOff < Slice.Length) {
9451 SubSlice = Slice;
9452 SubSlice.move(SrcOff);
9453 } else {
9454 // This is an out-of-bounds access and hence UB. Pretend we read zero.
9455 SubSlice.Array = nullptr;
9456 SubSlice.Offset = 0;
9457 SubSlice.Length = VTSize;
9458 }
9459 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
9460 if (Value.getNode()) {
9461 Store = DAG.getStore(
9462 Chain, dl, Value,
9463 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9464 DstPtrInfo.getWithOffset(DstOff), DstAlign, MMOFlags, NewAAInfo);
9465 OutChains.push_back(Store);
9466 }
9467 }
9468
9469 if (!Store.getNode()) {
9470 // The type might not be legal for the target. This should only happen
9471 // if the type is smaller than a legal type, as on PPC, so the right
9472 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
9473 // to Load/Store if NVT==VT.
9474 // FIXME does the case above also need this?
9475 EVT NVT = TLI.getTypeToTransformTo(C, VT);
9476 assert(NVT.bitsGE(VT));
9477
9478 bool isDereferenceable =
9479 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
9480 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
9481 if (isDereferenceable)
9483 if (isConstant)
9484 SrcMMOFlags |= MachineMemOperand::MOInvariant;
9485
9486 Value = DAG.getExtLoad(
9487 ISD::EXTLOAD, dl, NVT, Chain,
9488 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
9489 SrcPtrInfo.getWithOffset(SrcOff), VT,
9490 commonAlignment(SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
9491 OutLoadChains.push_back(Value.getValue(1));
9492
9493 Store = DAG.getTruncStore(
9494 Chain, dl, Value,
9495 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9496 DstPtrInfo.getWithOffset(DstOff), VT, DstAlign, MMOFlags, NewAAInfo);
9497 OutStoreChains.push_back(Store);
9498 }
9499 SrcOff += VTSize;
9500 DstOff += VTSize;
9501 Size -= VTSize;
9502 }
9503
9504 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
9506 unsigned NumLdStInMemcpy = OutStoreChains.size();
9507
9508 if (NumLdStInMemcpy) {
9509 // It may be that memcpy might be converted to memset if it's memcpy
9510 // of constants. In such a case, we won't have loads and stores, but
9511 // just stores. In the absence of loads, there is nothing to gang up.
9512 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
9513 // If target does not care, just leave as it.
9514 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
9515 OutChains.push_back(OutLoadChains[i]);
9516 OutChains.push_back(OutStoreChains[i]);
9517 }
9518 } else {
9519 // Ld/St less than/equal limit set by target.
9520 if (NumLdStInMemcpy <= GluedLdStLimit) {
9521 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
9522 NumLdStInMemcpy, OutLoadChains,
9523 OutStoreChains);
9524 } else {
9525 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
9526 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
9527 unsigned GlueIter = 0;
9528
9529 // Residual ld/st.
9530 if (RemainingLdStInMemcpy) {
9532 DAG, dl, OutChains, NumLdStInMemcpy - RemainingLdStInMemcpy,
9533 NumLdStInMemcpy, OutLoadChains, OutStoreChains);
9534 }
9535
9536 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
9537 unsigned IndexFrom = NumLdStInMemcpy - RemainingLdStInMemcpy -
9538 GlueIter - GluedLdStLimit;
9539 unsigned IndexTo = NumLdStInMemcpy - RemainingLdStInMemcpy - GlueIter;
9540 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
9541 OutLoadChains, OutStoreChains);
9542 GlueIter += GluedLdStLimit;
9543 }
9544 }
9545 }
9546 }
9547 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9548}
9549
9551 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
9552 uint64_t Size, Align DstAlign, Align SrcAlign, bool isVol,
9553 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
9554 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo) {
9555 // Turn a memmove of undef to nop.
9556 // FIXME: We need to honor volatile even is Src is undef.
9557 if (Src.isUndef())
9558 return Chain;
9559
9560 // Expand memmove to a series of load and store ops if the size operand falls
9561 // below a certain threshold.
9562 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9563 const DataLayout &DL = DAG.getDataLayout();
9564 LLVMContext &C = *DAG.getContext();
9565 std::vector<EVT> MemOps;
9566 bool DstAlignCanChange = false;
9568 MachineFrameInfo &MFI = MF.getFrameInfo();
9569 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9571 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9572 DstAlignCanChange = true;
9573 SrcAlign = std::max(SrcAlign, DAG.InferPtrAlign(Src).valueOrOne());
9574 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
9575 if (!TLI.findOptimalMemOpLowering(
9576 C, MemOps, Limit,
9577 MemOp::Copy(Size, DstAlignCanChange, DstAlign, SrcAlign, isVol),
9578 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
9579 MF.getFunction().getAttributes(), nullptr))
9580 return SDValue();
9581
9582 if (DstAlignCanChange) {
9583 Type *Ty = MemOps[0].getTypeForEVT(C);
9584 Align NewDstAlign = DL.getABITypeAlign(Ty);
9585
9586 // Don't promote to an alignment that would require dynamic stack
9587 // realignment which may conflict with optimizations such as tail call
9588 // optimization.
9590 if (!TRI->hasStackRealignment(MF))
9591 if (MaybeAlign StackAlign = DL.getStackAlignment())
9592 NewDstAlign = std::min(NewDstAlign, *StackAlign);
9593
9594 if (NewDstAlign > DstAlign) {
9595 // Give the stack frame object a larger alignment if needed.
9596 if (MFI.getObjectAlign(FI->getIndex()) < NewDstAlign)
9597 MFI.setObjectAlignment(FI->getIndex(), NewDstAlign);
9598 DstAlign = NewDstAlign;
9599 }
9600 }
9601
9602 // Prepare AAInfo for loads/stores after lowering this memmove.
9603 AAMDNodes NewAAInfo = AAInfo;
9604 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9605
9606 MachineMemOperand::Flags MMOFlags =
9608 uint64_t SrcOff = 0;
9609 SmallVector<SDValue, 8> LoadValues;
9610 SmallVector<SDValue, 8> LoadChains;
9611 SmallVector<SDValue, 8> OutChains;
9612 unsigned NumMemOps = MemOps.size();
9613 for (unsigned i = 0; i < NumMemOps; i++) {
9614 EVT VT = MemOps[i];
9615 unsigned VTSize = VT.getSizeInBits() / 8;
9616 SDValue Value;
9617 bool IsOverlapping = false;
9618
9619 if (i == NumMemOps - 1 && i != 0 && VTSize > Size - SrcOff) {
9620 // Issuing an unaligned load / store pair that overlaps with the previous
9621 // pair. Adjust the offset accordingly.
9622 SrcOff = Size - VTSize;
9623 IsOverlapping = true;
9624 }
9625
9626 // Calculate the actual alignment at the current offset. The alignment at
9627 // SrcOff may be lower than the base alignment, especially when using
9628 // overlapping loads.
9629 Align SrcAlignAtOffset = commonAlignment(SrcAlign, SrcOff);
9630 if (IsOverlapping) {
9631 // Verify that the target allows misaligned memory accesses at the
9632 // adjusted offset when using overlapping loads.
9633 unsigned Fast;
9634 if (!TLI.allowsMisalignedMemoryAccesses(VT, SrcPtrInfo.getAddrSpace(),
9635 SrcAlignAtOffset, MMOFlags,
9636 &Fast) ||
9637 !Fast) {
9638 // This should have been caught by findOptimalMemOpLowering, but verify
9639 // here for safety.
9640 return SDValue();
9641 }
9642 }
9643
9644 bool isDereferenceable =
9645 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
9646 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
9647 if (isDereferenceable)
9649 Value =
9650 DAG.getLoad(VT, dl, Chain,
9651 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
9652 SrcPtrInfo.getWithOffset(SrcOff), SrcAlignAtOffset,
9653 SrcMMOFlags, NewAAInfo);
9654 LoadValues.push_back(Value);
9655 LoadChains.push_back(Value.getValue(1));
9656 SrcOff += VTSize;
9657 }
9658 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
9659 OutChains.clear();
9660 uint64_t DstOff = 0;
9661 for (unsigned i = 0; i < NumMemOps; i++) {
9662 EVT VT = MemOps[i];
9663 unsigned VTSize = VT.getSizeInBits() / 8;
9664 SDValue Store;
9665 bool IsOverlapping = false;
9666
9667 if (i == NumMemOps - 1 && i != 0 && VTSize > Size - DstOff) {
9668 // Issuing an unaligned load / store pair that overlaps with the previous
9669 // pair. Adjust the offset accordingly.
9670 DstOff = Size - VTSize;
9671 IsOverlapping = true;
9672 }
9673
9674 // Calculate the actual alignment at the current offset. The alignment at
9675 // DstOff may be lower than the base alignment, especially when using
9676 // overlapping stores.
9677 Align DstAlignAtOffset = commonAlignment(DstAlign, DstOff);
9678 if (IsOverlapping) {
9679 // Verify that the target allows misaligned memory accesses at the
9680 // adjusted offset when using overlapping stores.
9681 unsigned Fast;
9682 if (!TLI.allowsMisalignedMemoryAccesses(VT, DstPtrInfo.getAddrSpace(),
9683 DstAlignAtOffset, MMOFlags,
9684 &Fast) ||
9685 !Fast) {
9686 // This should have been caught by findOptimalMemOpLowering, but verify
9687 // here for safety.
9688 return SDValue();
9689 }
9690 }
9691 Store = DAG.getStore(
9692 Chain, dl, LoadValues[i],
9693 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9694 DstPtrInfo.getWithOffset(DstOff), DstAlignAtOffset, MMOFlags,
9695 NewAAInfo);
9696 OutChains.push_back(Store);
9697 DstOff += VTSize;
9698 }
9699
9700 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9701}
9702
9703/// Lower the call to 'memset' intrinsic function into a series of store
9704/// operations.
9705///
9706/// \param DAG Selection DAG where lowered code is placed.
9707/// \param dl Link to corresponding IR location.
9708/// \param Chain Control flow dependency.
9709/// \param Dst Pointer to destination memory location.
9710/// \param Src Value of byte to write into the memory.
9711/// \param Size Number of bytes to write.
9712/// \param Alignment Alignment of the destination in bytes.
9713/// \param isVol True if destination is volatile.
9714/// \param AlwaysInline Makes sure no function call is generated.
9715/// \param DstPtrInfo IR information on the memory pointer.
9716/// \returns New head in the control flow, if lowering was successful, empty
9717/// SDValue otherwise.
9718///
9719/// The function tries to replace 'llvm.memset' intrinsic with several store
9720/// operations and value calculation code. This is usually profitable for small
9721/// memory size or when the semantic requires inlining.
9723 SDValue Chain, SDValue Dst, SDValue Src,
9724 uint64_t Size, Align Alignment, bool isVol,
9725 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
9726 const AAMDNodes &AAInfo) {
9727 // Turn a memset of undef to nop.
9728 // FIXME: We need to honor volatile even is Src is undef.
9729 if (Src.isUndef())
9730 return Chain;
9731
9732 // Expand memset to a series of load/store ops if the size operand
9733 // falls below a certain threshold.
9734 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9735 std::vector<EVT> MemOps;
9736 bool DstAlignCanChange = false;
9737 LLVMContext &C = *DAG.getContext();
9739 MachineFrameInfo &MFI = MF.getFrameInfo();
9740 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9742 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9743 DstAlignCanChange = true;
9744 bool IsZeroVal = isNullConstant(Src);
9745 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
9746
9747 EVT LargestVT;
9748 if (!TLI.findOptimalMemOpLowering(
9749 C, MemOps, Limit,
9750 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
9751 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes(),
9752 &LargestVT))
9753 return SDValue();
9754
9755 if (DstAlignCanChange) {
9756 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9757 const DataLayout &DL = DAG.getDataLayout();
9758 Align NewAlign = DL.getABITypeAlign(Ty);
9759
9760 // Don't promote to an alignment that would require dynamic stack
9761 // realignment which may conflict with optimizations such as tail call
9762 // optimization.
9764 if (!TRI->hasStackRealignment(MF))
9765 if (MaybeAlign StackAlign = DL.getStackAlignment())
9766 NewAlign = std::min(NewAlign, *StackAlign);
9767
9768 if (NewAlign > Alignment) {
9769 // Give the stack frame object a larger alignment if needed.
9770 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9771 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9772 Alignment = NewAlign;
9773 }
9774 }
9775
9776 SmallVector<SDValue, 8> OutChains;
9777 uint64_t DstOff = 0;
9778 unsigned NumMemOps = MemOps.size();
9779
9780 // Find the largest store and generate the bit pattern for it.
9781 // If target didn't set LargestVT, compute it from MemOps.
9782 if (!LargestVT.isSimple()) {
9783 LargestVT = MemOps[0];
9784 for (unsigned i = 1; i < NumMemOps; i++)
9785 if (MemOps[i].bitsGT(LargestVT))
9786 LargestVT = MemOps[i];
9787 }
9788 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9789
9790 // Prepare AAInfo for loads/stores after lowering this memset.
9791 AAMDNodes NewAAInfo = AAInfo;
9792 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9793
9794 for (unsigned i = 0; i < NumMemOps; i++) {
9795 EVT VT = MemOps[i];
9796 unsigned VTSize = VT.getSizeInBits() / 8;
9797 // The target should specify store types that exactly cover the memset size
9798 // (with the last store potentially being oversized for overlapping stores).
9799 assert(Size > 0 && "Target specified more stores than needed in "
9800 "findOptimalMemOpLowering");
9801 if (VTSize > Size) {
9802 // Issuing an unaligned load / store pair that overlaps with the previous
9803 // pair. Adjust the offset accordingly.
9804 assert(i == NumMemOps-1 && i != 0);
9805 DstOff -= VTSize - Size;
9806 }
9807
9808 // If this store is smaller than the largest store see whether we can get
9809 // the smaller value for free with a truncate or extract vector element and
9810 // then store.
9811 SDValue Value = MemSetValue;
9812 if (VT.bitsLT(LargestVT)) {
9813 unsigned Index;
9814 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9815 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9816 if (!LargestVT.isVector() && !VT.isVector() &&
9817 TLI.isTruncateFree(LargestVT, VT))
9818 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9819 else if (LargestVT.isVector() && !VT.isVector() &&
9821 LargestVT.getTypeForEVT(*DAG.getContext()),
9822 VT.getSizeInBits(), Index) &&
9823 TLI.isTypeLegal(SVT) &&
9824 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9825 // Target which can combine store(extractelement VectorTy, Idx) can get
9826 // the smaller value for free.
9827 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9828 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9829 } else
9830 Value = getMemsetValue(Src, VT, DAG, dl);
9831 }
9832 assert(Value.getValueType() == VT && "Value with wrong type.");
9833 SDValue Store = DAG.getStore(
9834 Chain, dl, Value,
9835 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9836 DstPtrInfo.getWithOffset(DstOff), Alignment,
9838 NewAAInfo);
9839 OutChains.push_back(Store);
9840 DstOff += VT.getSizeInBits() / 8;
9841 // For oversized overlapping stores, only subtract the remaining bytes.
9842 // For normal stores, subtract the full store size.
9843 if (VTSize > Size) {
9844 Size = 0;
9845 } else {
9846 Size -= VTSize;
9847 }
9848 }
9849
9850 // After processing all stores, Size should be exactly 0. Any remaining bytes
9851 // indicate a bug in the target's findOptimalMemOpLowering implementation.
9852 assert(Size == 0 && "Target's findOptimalMemOpLowering did not specify "
9853 "stores that exactly cover the memset size");
9854
9855 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9856}
9857
9859 unsigned AS) {
9860 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9861 // pointer operands can be losslessly bitcasted to pointers of address space 0
9862 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9863 report_fatal_error("cannot lower memory intrinsic in address space " +
9864 Twine(AS));
9865 }
9866}
9867
9869 const SelectionDAG *SelDAG,
9870 bool AllowReturnsFirstArg) {
9871 if (!CI || !CI->isTailCall())
9872 return false;
9873 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9874 // helper symbol we lower to.
9875 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9876 AllowReturnsFirstArg &&
9878}
9879
9880static std::pair<SDValue, SDValue>
9883 const CallInst *CI, RTLIB::Libcall Call,
9884 SelectionDAG *DAG, const TargetLowering *TLI) {
9885 RTLIB::LibcallImpl LCImpl = DAG->getLibcalls().getLibcallImpl(Call);
9886
9887 if (LCImpl == RTLIB::Unsupported)
9888 return {};
9889
9891 bool IsTailCall =
9892 isInTailCallPositionWrapper(CI, DAG, /*AllowReturnsFirstArg=*/true);
9893 SDValue Callee =
9894 DAG->getExternalSymbol(LCImpl, TLI->getPointerTy(DAG->getDataLayout()));
9895
9896 CLI.setDebugLoc(dl)
9897 .setChain(Chain)
9899 CI->getType(), Callee, std::move(Args))
9900 .setTailCall(IsTailCall);
9901
9902 return TLI->LowerCallTo(CLI);
9903}
9904
9905std::pair<SDValue, SDValue> SelectionDAG::getStrcmp(SDValue Chain,
9906 const SDLoc &dl, SDValue S1,
9907 SDValue S2,
9908 const CallInst *CI) {
9910 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9911 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9912 RTLIB::STRCMP, this, TLI);
9913}
9914
9915std::pair<SDValue, SDValue> SelectionDAG::getStrstr(SDValue Chain,
9916 const SDLoc &dl, SDValue S1,
9917 SDValue S2,
9918 const CallInst *CI) {
9920 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9921 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9922 RTLIB::STRSTR, this, TLI);
9923}
9924
9925std::pair<SDValue, SDValue> SelectionDAG::getMemccpy(SDValue Chain,
9926 const SDLoc &dl,
9927 SDValue Dst, SDValue Src,
9929 const CallInst *CI) {
9931
9933 {Dst, PT},
9934 {Src, PT},
9937 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9938 RTLIB::MEMCCPY, this, TLI);
9939}
9940
9941std::pair<SDValue, SDValue>
9943 SDValue Mem1, SDValue Size, const CallInst *CI) {
9946 {Mem0, PT},
9947 {Mem1, PT},
9949 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9950 RTLIB::MEMCMP, this, TLI);
9951}
9952
9953std::pair<SDValue, SDValue> SelectionDAG::getStrcpy(SDValue Chain,
9954 const SDLoc &dl,
9955 SDValue Dst, SDValue Src,
9956 const CallInst *CI) {
9958 TargetLowering::ArgListTy Args = {{Dst, PT}, {Src, PT}};
9959 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9960 RTLIB::STRCPY, this, TLI);
9961}
9962
9963std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9964 const SDLoc &dl,
9965 SDValue Src,
9966 const CallInst *CI) {
9967 // Emit a library call.
9970 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9971 RTLIB::STRLEN, this, TLI);
9972}
9973
9975 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9976 Align DstAlign, Align SrcAlign, bool isVol, bool AlwaysInline,
9977 const CallInst *CI, std::optional<bool> OverrideTailCall,
9978 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
9979 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
9980 // Check to see if we should lower the memcpy to loads and stores first.
9981 // For cases within the target-specified limits, this is the best choice.
9983 if (ConstantSize) {
9984 // Memcpy with size zero? Just return the original chain.
9985 if (ConstantSize->isZero())
9986 return Chain;
9987
9989 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), DstAlign,
9990 SrcAlign, isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9991 if (Result.getNode())
9992 return Result;
9993 }
9994
9995 // Then check to see if we should lower the memcpy with target-specific
9996 // code. If the target chooses to do this, this is the next best.
9997 if (TSI) {
9998 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9999 *this, dl, Chain, Dst, Src, Size, DstAlign, SrcAlign, isVol,
10000 AlwaysInline, DstPtrInfo, SrcPtrInfo);
10001 if (Result.getNode())
10002 return Result;
10003 }
10004
10005 // If we really need inline code and the target declined to provide it,
10006 // use a (potentially long) sequence of loads and stores.
10007 if (AlwaysInline) {
10008 assert(ConstantSize && "AlwaysInline requires a constant size!");
10010 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), DstAlign,
10011 SrcAlign, isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
10012 }
10013
10016
10017 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
10018 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
10019 // respect volatile, so they may do things like read or write memory
10020 // beyond the given memory regions. But fixing this isn't easy, and most
10021 // people don't care.
10022
10023 // Emit a library call.
10026 Args.emplace_back(Dst, PtrTy);
10027 Args.emplace_back(Src, PtrTy);
10028 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
10029 // FIXME: pass in SDLoc
10031 bool IsTailCall = false;
10032 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
10033
10034 if (OverrideTailCall.has_value()) {
10035 IsTailCall = *OverrideTailCall;
10036 } else {
10037 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
10038 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
10039 }
10040
10041 CLI.setDebugLoc(dl)
10042 .setChain(Chain)
10043 .setLibCallee(
10044 Libcalls->getLibcallImplCallingConv(MemCpyImpl),
10045 Dst.getValueType().getTypeForEVT(*getContext()),
10046 getExternalSymbol(MemCpyImpl, TLI->getPointerTy(getDataLayout())),
10047 std::move(Args))
10049 .setTailCall(IsTailCall);
10050
10051 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
10052 return CallResult.second;
10053}
10054
10056 SDValue Dst, SDValue Src, SDValue Size,
10057 Type *SizeTy, unsigned ElemSz,
10058 bool isTailCall,
10059 MachinePointerInfo DstPtrInfo,
10060 MachinePointerInfo SrcPtrInfo) {
10061 // Emit a library call.
10064 Args.emplace_back(Dst, ArgTy);
10065 Args.emplace_back(Src, ArgTy);
10066 Args.emplace_back(Size, SizeTy);
10067
10068 RTLIB::Libcall LibraryCall =
10070 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
10071 if (LibcallImpl == RTLIB::Unsupported)
10072 report_fatal_error("Unsupported element size");
10073
10075 CLI.setDebugLoc(dl)
10076 .setChain(Chain)
10077 .setLibCallee(
10078 Libcalls->getLibcallImplCallingConv(LibcallImpl),
10080 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
10081 std::move(Args))
10083 .setTailCall(isTailCall);
10084
10085 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
10086 return CallResult.second;
10087}
10088
10090 SDValue Src, SDValue Size, Align DstAlign,
10091 Align SrcAlign, bool isVol, const CallInst *CI,
10092 std::optional<bool> OverrideTailCall,
10093 MachinePointerInfo DstPtrInfo,
10094 MachinePointerInfo SrcPtrInfo,
10095 const AAMDNodes &AAInfo,
10096 BatchAAResults *BatchAA) {
10097 // Check to see if we should lower the memmove to loads and stores first.
10098 // For cases within the target-specified limits, this is the best choice.
10100 if (ConstantSize) {
10101 // Memmove with size zero? Just return the original chain.
10102 if (ConstantSize->isZero())
10103 return Chain;
10104
10106 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), DstAlign,
10107 SrcAlign, isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
10108 if (Result.getNode())
10109 return Result;
10110 }
10111
10112 // Then check to see if we should lower the memmove with target-specific
10113 // code. If the target chooses to do this, this is the next best.
10114 if (TSI) {
10115 SDValue Result = TSI->EmitTargetCodeForMemmove(
10116 *this, dl, Chain, Dst, Src, Size, DstAlign, SrcAlign, isVol, DstPtrInfo,
10117 SrcPtrInfo);
10118 if (Result.getNode())
10119 return Result;
10120 }
10121
10124
10125 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
10126 // not be safe. See memcpy above for more details.
10127
10128 // Emit a library call.
10131 Args.emplace_back(Dst, PtrTy);
10132 Args.emplace_back(Src, PtrTy);
10133 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
10134 // FIXME: pass in SDLoc
10136
10137 RTLIB::LibcallImpl MemmoveImpl = Libcalls->getLibcallImpl(RTLIB::MEMMOVE);
10138
10139 bool IsTailCall = false;
10140 if (OverrideTailCall.has_value()) {
10141 IsTailCall = *OverrideTailCall;
10142 } else {
10143 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
10144 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
10145 }
10146
10147 CLI.setDebugLoc(dl)
10148 .setChain(Chain)
10149 .setLibCallee(
10150 Libcalls->getLibcallImplCallingConv(MemmoveImpl),
10151 Dst.getValueType().getTypeForEVT(*getContext()),
10152 getExternalSymbol(MemmoveImpl, TLI->getPointerTy(getDataLayout())),
10153 std::move(Args))
10155 .setTailCall(IsTailCall);
10156
10157 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
10158 return CallResult.second;
10159}
10160
10162 SDValue Dst, SDValue Src, SDValue Size,
10163 Type *SizeTy, unsigned ElemSz,
10164 bool isTailCall,
10165 MachinePointerInfo DstPtrInfo,
10166 MachinePointerInfo SrcPtrInfo) {
10167 // Emit a library call.
10170 Args.emplace_back(Dst, IntPtrTy);
10171 Args.emplace_back(Src, IntPtrTy);
10172 Args.emplace_back(Size, SizeTy);
10173
10174 RTLIB::Libcall LibraryCall =
10176 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
10177 if (LibcallImpl == RTLIB::Unsupported)
10178 report_fatal_error("Unsupported element size");
10179
10181 CLI.setDebugLoc(dl)
10182 .setChain(Chain)
10183 .setLibCallee(
10184 Libcalls->getLibcallImplCallingConv(LibcallImpl),
10186 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
10187 std::move(Args))
10189 .setTailCall(isTailCall);
10190
10191 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
10192 return CallResult.second;
10193}
10194
10196 SDValue Src, SDValue Size, Align Alignment,
10197 bool isVol, bool AlwaysInline,
10198 const CallInst *CI,
10199 MachinePointerInfo DstPtrInfo,
10200 const AAMDNodes &AAInfo) {
10201 // Check to see if we should lower the memset to stores first.
10202 // For cases within the target-specified limits, this is the best choice.
10204 if (ConstantSize) {
10205 // Memset with size zero? Just return the original chain.
10206 if (ConstantSize->isZero())
10207 return Chain;
10208
10209 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
10210 ConstantSize->getZExtValue(), Alignment,
10211 isVol, false, DstPtrInfo, AAInfo);
10212
10213 if (Result.getNode())
10214 return Result;
10215 }
10216
10217 // Then check to see if we should lower the memset with target-specific
10218 // code. If the target chooses to do this, this is the next best.
10219 if (TSI) {
10220 SDValue Result = TSI->EmitTargetCodeForMemset(
10221 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
10222 if (Result.getNode())
10223 return Result;
10224 }
10225
10226 // If we really need inline code and the target declined to provide it,
10227 // use a (potentially long) sequence of loads and stores.
10228 if (AlwaysInline) {
10229 assert(ConstantSize && "AlwaysInline requires a constant size!");
10230 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
10231 ConstantSize->getZExtValue(), Alignment,
10232 isVol, true, DstPtrInfo, AAInfo);
10233 assert(Result &&
10234 "getMemsetStores must return a valid sequence when AlwaysInline");
10235 return Result;
10236 }
10237
10239
10240 // Emit a library call.
10241 auto &Ctx = *getContext();
10242 const auto& DL = getDataLayout();
10243
10245 // FIXME: pass in SDLoc
10246 CLI.setDebugLoc(dl).setChain(Chain);
10247
10248 RTLIB::LibcallImpl BzeroImpl = Libcalls->getLibcallImpl(RTLIB::BZERO);
10249 bool UseBZero = BzeroImpl != RTLIB::Unsupported && isNullConstant(Src);
10250
10251 // If zeroing out and bzero is present, use it.
10252 if (UseBZero) {
10254 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
10255 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
10256 CLI.setLibCallee(
10257 Libcalls->getLibcallImplCallingConv(BzeroImpl), Type::getVoidTy(Ctx),
10258 getExternalSymbol(BzeroImpl, TLI->getPointerTy(DL)), std::move(Args));
10259 } else {
10260 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
10261
10263 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
10264 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
10265 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
10266 CLI.setLibCallee(Libcalls->getLibcallImplCallingConv(MemsetImpl),
10267 Dst.getValueType().getTypeForEVT(Ctx),
10268 getExternalSymbol(MemsetImpl, TLI->getPointerTy(DL)),
10269 std::move(Args));
10270 }
10271
10272 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
10273 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
10274
10275 // If we're going to use bzero, make sure not to tail call unless the
10276 // subsequent return doesn't need a value, as bzero doesn't return the first
10277 // arg unlike memset.
10278 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
10279 bool IsTailCall =
10280 CI && CI->isTailCall() &&
10281 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
10282 CLI.setDiscardResult().setTailCall(IsTailCall);
10283
10284 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
10285 return CallResult.second;
10286}
10287
10290 Type *SizeTy, unsigned ElemSz,
10291 bool isTailCall,
10292 MachinePointerInfo DstPtrInfo) {
10293 // Emit a library call.
10295 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
10296 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
10297 Args.emplace_back(Size, SizeTy);
10298
10299 RTLIB::Libcall LibraryCall =
10301 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
10302 if (LibcallImpl == RTLIB::Unsupported)
10303 report_fatal_error("Unsupported element size");
10304
10306 CLI.setDebugLoc(dl)
10307 .setChain(Chain)
10308 .setLibCallee(
10309 Libcalls->getLibcallImplCallingConv(LibcallImpl),
10311 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
10312 std::move(Args))
10314 .setTailCall(isTailCall);
10315
10316 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
10317 return CallResult.second;
10318}
10319
10320SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
10322 MachineMemOperand *MMO,
10323 ISD::LoadExtType ExtType) {
10325 AddNodeIDNode(ID, Opcode, VTList, Ops);
10326 ID.AddInteger(MemVT.getRawBits());
10327 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
10328 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
10329 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10330 ID.AddInteger(MMO->getFlags());
10331 void* IP = nullptr;
10332 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10333 E->refineAlignment(MMO);
10334 E->refineRanges(MMO);
10335 return SDValue(E, 0);
10336 }
10337
10338 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
10339 VTList, MemVT, MMO, ExtType);
10340 createOperands(N, Ops);
10341
10342 CSEMap.InsertNode(N, IP);
10343 InsertNode(N);
10344 SDValue V(N, 0);
10345 NewSDValueDbgMsg(V, "Creating new node: ", this);
10346 return V;
10347}
10348
10350 EVT MemVT, SDVTList VTs, SDValue Chain,
10351 SDValue Ptr, SDValue Cmp, SDValue Swp,
10352 MachineMemOperand *MMO) {
10353 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
10355 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
10356
10357 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
10358 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
10359}
10360
10361SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
10362 SDValue Chain, SDValue Ptr, SDValue Val,
10363 MachineMemOperand *MMO) {
10364 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
10365 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
10366 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
10367 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
10368 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
10369 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
10370 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
10371 Opcode == ISD::ATOMIC_LOAD_FMIN ||
10372 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
10373 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
10374 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
10375 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
10376 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
10377 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
10378 Opcode == ISD::ATOMIC_STORE) &&
10379 "Invalid Atomic Op");
10380
10381 EVT VT = Val.getValueType();
10382
10383 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
10384 getVTList(VT, MVT::Other);
10385 SDValue Ops[] = {Chain, Ptr, Val};
10386 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
10387}
10388
10390 EVT MemVT, EVT VT, SDValue Chain,
10391 SDValue Ptr, MachineMemOperand *MMO) {
10392 SDVTList VTs = getVTList(VT, MVT::Other);
10393 SDValue Ops[] = {Chain, Ptr};
10394 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
10395}
10396
10397/// getMergeValues - Create a MERGE_VALUES node from the given operands.
10399 if (Ops.size() == 1)
10400 return Ops[0];
10401
10403 VTs.reserve(Ops.size());
10404 for (const SDValue &Op : Ops)
10405 VTs.push_back(Op.getValueType());
10406 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
10407}
10408
10410 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
10411 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
10413 const AAMDNodes &AAInfo) {
10414 if (Size.hasValue() && !Size.getValue())
10416
10418 MachineMemOperand *MMO =
10419 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
10420
10421 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
10422}
10423
10425 SDVTList VTList,
10426 ArrayRef<SDValue> Ops, EVT MemVT,
10427 MachineMemOperand *MMO) {
10428 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, ArrayRef(MMO));
10429}
10430
10432 SDVTList VTList,
10433 ArrayRef<SDValue> Ops, EVT MemVT,
10435 assert(!MMOs.empty() && "Must have at least one MMO");
10436 assert(
10437 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
10438 Opcode == ISD::PREFETCH ||
10439 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
10440 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
10441 "Opcode is not a memory-accessing opcode!");
10442
10444 if (MMOs.size() == 1) {
10445 MemRefs = MMOs[0];
10446 } else {
10447 // Allocate: [size_t count][MMO*][MMO*]...
10448 size_t AllocSize =
10449 sizeof(size_t) + MMOs.size() * sizeof(MachineMemOperand *);
10450 void *Buffer = Allocator.Allocate(AllocSize, alignof(size_t));
10451 size_t *CountPtr = static_cast<size_t *>(Buffer);
10452 *CountPtr = MMOs.size();
10453 MachineMemOperand **Array =
10454 reinterpret_cast<MachineMemOperand **>(CountPtr + 1);
10455 llvm::copy(MMOs, Array);
10456 MemRefs = Array;
10457 }
10458
10459 // Memoize the node unless it returns a glue result.
10461 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
10463 AddNodeIDNode(ID, Opcode, VTList, Ops);
10464 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
10465 Opcode, dl.getIROrder(), VTList, MemVT, MemRefs));
10466 ID.AddInteger(MemVT.getRawBits());
10467 for (const MachineMemOperand *MMO : MMOs) {
10468 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10469 ID.AddInteger(MMO->getFlags());
10470 }
10471 void *IP = nullptr;
10472 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10473 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMOs);
10474 return SDValue(E, 0);
10475 }
10476
10477 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
10478 VTList, MemVT, MemRefs);
10479 createOperands(N, Ops);
10480 CSEMap.InsertNode(N, IP);
10481 } else {
10482 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
10483 VTList, MemVT, MemRefs);
10484 createOperands(N, Ops);
10485 }
10486 InsertNode(N);
10487 SDValue V(N, 0);
10488 NewSDValueDbgMsg(V, "Creating new node: ", this);
10489 return V;
10490}
10491
10493 SDValue Chain, int FrameIndex) {
10494 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
10495 const auto VTs = getVTList(MVT::Other);
10496 SDValue Ops[2] = {
10497 Chain,
10498 getFrameIndex(FrameIndex,
10499 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
10500 true)};
10501
10503 AddNodeIDNode(ID, Opcode, VTs, Ops);
10504 ID.AddInteger(FrameIndex);
10505 void *IP = nullptr;
10506 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10507 return SDValue(E, 0);
10508
10509 LifetimeSDNode *N =
10510 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
10511 createOperands(N, Ops);
10512 CSEMap.InsertNode(N, IP);
10513 InsertNode(N);
10514 SDValue V(N, 0);
10515 NewSDValueDbgMsg(V, "Creating new node: ", this);
10516 return V;
10517}
10518
10520 uint64_t Guid, uint64_t Index,
10521 uint32_t Attr) {
10522 const unsigned Opcode = ISD::PSEUDO_PROBE;
10523 const auto VTs = getVTList(MVT::Other);
10524 SDValue Ops[] = {Chain};
10526 AddNodeIDNode(ID, Opcode, VTs, Ops);
10527 ID.AddInteger(Guid);
10528 ID.AddInteger(Index);
10529 void *IP = nullptr;
10530 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
10531 return SDValue(E, 0);
10532
10533 auto *N = newSDNode<PseudoProbeSDNode>(
10534 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
10535 createOperands(N, Ops);
10536 CSEMap.InsertNode(N, IP);
10537 InsertNode(N);
10538 SDValue V(N, 0);
10539 NewSDValueDbgMsg(V, "Creating new node: ", this);
10540 return V;
10541}
10542
10543/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
10544/// MachinePointerInfo record from it. This is particularly useful because the
10545/// code generator has many cases where it doesn't bother passing in a
10546/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
10548 SelectionDAG &DAG, SDValue Ptr,
10549 int64_t Offset = 0) {
10550 // If this is FI+Offset, we can model it.
10551 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
10553 FI->getIndex(), Offset);
10554
10555 // If this is (FI+Offset1)+Offset2, we can model it.
10556 if (Ptr.getOpcode() != ISD::ADD ||
10559 return Info;
10560
10561 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
10563 DAG.getMachineFunction(), FI,
10564 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
10565}
10566
10567/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
10568/// MachinePointerInfo record from it. This is particularly useful because the
10569/// code generator has many cases where it doesn't bother passing in a
10570/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
10572 SelectionDAG &DAG, SDValue Ptr,
10573 SDValue OffsetOp) {
10574 // If the 'Offset' value isn't a constant, we can't handle this.
10576 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
10577 if (OffsetOp.isUndef())
10578 return InferPointerInfo(Info, DAG, Ptr);
10579 return Info;
10580}
10581
10583 EVT VT, const SDLoc &dl, SDValue Chain,
10584 SDValue Ptr, SDValue Offset,
10585 MachinePointerInfo PtrInfo, EVT MemVT,
10586 Align Alignment,
10587 MachineMemOperand::Flags MMOFlags,
10588 const AAMDNodes &AAInfo, const MDNode *Ranges) {
10589 assert(Chain.getValueType() == MVT::Other &&
10590 "Invalid chain type");
10591
10592 MMOFlags |= MachineMemOperand::MOLoad;
10593 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10594 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10595 // clients.
10596 if (PtrInfo.V.isNull())
10597 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10598
10599 TypeSize Size = MemVT.getStoreSize();
10601 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10602 Alignment, AAInfo, Ranges);
10603 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
10604}
10605
10607 EVT VT, const SDLoc &dl, SDValue Chain,
10608 SDValue Ptr, SDValue Offset, EVT MemVT,
10609 MachineMemOperand *MMO) {
10610 if (VT == MemVT) {
10611 ExtType = ISD::NON_EXTLOAD;
10612 } else if (ExtType == ISD::NON_EXTLOAD) {
10613 assert(VT == MemVT && "Non-extending load from different memory type!");
10614 } else {
10615 // Extending load.
10616 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
10617 "Should only be an extending load, not truncating!");
10618 assert(VT.isInteger() == MemVT.isInteger() &&
10619 "Cannot convert from FP to Int or Int -> FP!");
10620 assert(VT.isVector() == MemVT.isVector() &&
10621 "Cannot use an ext load to convert to or from a vector!");
10622 assert((!VT.isVector() ||
10624 "Cannot use an ext load to change the number of vector elements!");
10625 }
10626
10627 assert((!MMO->getRanges() ||
10629 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
10630 MemVT.isInteger())) &&
10631 "Range metadata and load type must match!");
10632
10633 bool Indexed = AM != ISD::UNINDEXED;
10634 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10635
10636 SDVTList VTs = Indexed ?
10637 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
10638 SDValue Ops[] = { Chain, Ptr, Offset };
10640 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
10641 ID.AddInteger(MemVT.getRawBits());
10642 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
10643 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
10644 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10645 ID.AddInteger(MMO->getFlags());
10646 void *IP = nullptr;
10647 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10648 E->refineAlignment(MMO);
10649 E->refineRanges(MMO);
10650 return SDValue(E, 0);
10651 }
10652 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10653 ExtType, MemVT, MMO);
10654 createOperands(N, Ops);
10655
10656 CSEMap.InsertNode(N, IP);
10657 InsertNode(N);
10658 SDValue V(N, 0);
10659 NewSDValueDbgMsg(V, "Creating new node: ", this);
10660 return V;
10661}
10662
10664 SDValue Ptr, MachinePointerInfo PtrInfo,
10665 MaybeAlign Alignment,
10666 MachineMemOperand::Flags MMOFlags,
10667 const AAMDNodes &AAInfo, const MDNode *Ranges) {
10669 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10670 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
10671}
10672
10674 SDValue Ptr, MachineMemOperand *MMO) {
10676 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10677 VT, MMO);
10678}
10679
10681 EVT VT, SDValue Chain, SDValue Ptr,
10682 MachinePointerInfo PtrInfo, EVT MemVT,
10683 MaybeAlign Alignment,
10684 MachineMemOperand::Flags MMOFlags,
10685 const AAMDNodes &AAInfo) {
10687 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
10688 MemVT, Alignment, MMOFlags, AAInfo);
10689}
10690
10692 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
10693 MachineMemOperand *MMO) {
10695 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
10696 MemVT, MMO);
10697}
10698
10702 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
10703 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10704 // Don't propagate the invariant or dereferenceable flags.
10705 auto MMOFlags =
10706 LD->getMemOperand()->getFlags() &
10708 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10709 LD->getChain(), Base, Offset, LD->getPointerInfo(),
10710 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
10711}
10712
10714 SDValue Ptr, MachinePointerInfo PtrInfo,
10715 Align Alignment,
10716 MachineMemOperand::Flags MMOFlags,
10717 const AAMDNodes &AAInfo) {
10718 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10719
10720 MMOFlags |= MachineMemOperand::MOStore;
10721 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10722
10723 if (PtrInfo.V.isNull())
10724 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10725
10728 MachineMemOperand *MMO =
10729 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
10730 return getStore(Chain, dl, Val, Ptr, MMO);
10731}
10732
10734 SDValue Ptr, MachineMemOperand *MMO) {
10736 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
10738}
10739
10741 SDValue Ptr, SDValue Offset, EVT SVT,
10743 bool IsTruncating) {
10744 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10745 EVT VT = Val.getValueType();
10746 if (VT == SVT) {
10747 IsTruncating = false;
10748 } else if (!IsTruncating) {
10749 assert(VT == SVT && "No-truncating store from different memory type!");
10750 } else {
10752 "Should only be a truncating store, not extending!");
10753 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10754 assert(VT.isVector() == SVT.isVector() &&
10755 "Cannot use trunc store to convert to or from a vector!");
10756 assert((!VT.isVector() ||
10758 "Cannot use trunc store to change the number of vector elements!");
10759 }
10760
10761 bool Indexed = AM != ISD::UNINDEXED;
10762 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
10763 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10764 : getVTList(MVT::Other);
10765 SDValue Ops[] = {Chain, Val, Ptr, Offset};
10768 ID.AddInteger(SVT.getRawBits());
10769 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
10770 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
10771 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10772 ID.AddInteger(MMO->getFlags());
10773 void *IP = nullptr;
10774 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10775 cast<StoreSDNode>(E)->refineAlignment(MMO);
10776 return SDValue(E, 0);
10777 }
10778 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10779 IsTruncating, SVT, MMO);
10780 createOperands(N, Ops);
10781
10782 CSEMap.InsertNode(N, IP);
10783 InsertNode(N);
10784 SDValue V(N, 0);
10785 NewSDValueDbgMsg(V, "Creating new node: ", this);
10786 return V;
10787}
10788
10790 SDValue Ptr, MachinePointerInfo PtrInfo,
10791 EVT SVT, Align Alignment,
10792 MachineMemOperand::Flags MMOFlags,
10793 const AAMDNodes &AAInfo) {
10794 assert(Chain.getValueType() == MVT::Other &&
10795 "Invalid chain type");
10796
10797 MMOFlags |= MachineMemOperand::MOStore;
10798 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10799
10800 if (PtrInfo.V.isNull())
10801 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10802
10804 MachineMemOperand *MMO = MF.getMachineMemOperand(
10805 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10806 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
10807}
10808
10810 SDValue Ptr, EVT SVT,
10811 MachineMemOperand *MMO) {
10813 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
10814}
10815
10819 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
10820 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
10821 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10822 ST->getMemoryVT(), ST->getMemOperand(), AM,
10823 ST->isTruncatingStore());
10824}
10825
10827 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
10828 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
10829 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
10830 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
10831 const MDNode *Ranges, bool IsExpanding) {
10832 MMOFlags |= MachineMemOperand::MOLoad;
10833 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10834 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10835 // clients.
10836 if (PtrInfo.V.isNull())
10837 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10838
10839 TypeSize Size = MemVT.getStoreSize();
10841 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10842 Alignment, AAInfo, Ranges);
10843 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10844 MMO, IsExpanding);
10845}
10846
10848 ISD::LoadExtType ExtType, EVT VT,
10849 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10850 SDValue Offset, SDValue Mask, SDValue EVL,
10851 EVT MemVT, MachineMemOperand *MMO,
10852 bool IsExpanding) {
10853 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10854 assert(Mask.getValueType().getVectorElementCount() ==
10855 VT.getVectorElementCount() &&
10856 "Vector width mismatch between mask and data");
10857
10858 bool Indexed = AM != ISD::UNINDEXED;
10859 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10860
10861 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10862 : getVTList(VT, MVT::Other);
10863 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10865 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10866 ID.AddInteger(MemVT.getRawBits());
10867 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10868 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10869 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10870 ID.AddInteger(MMO->getFlags());
10871 void *IP = nullptr;
10872 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10873 E->refineAlignment(MMO);
10874 E->refineRanges(MMO);
10875 return SDValue(E, 0);
10876 }
10877 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10878 ExtType, IsExpanding, MemVT, MMO);
10879 createOperands(N, Ops);
10880
10881 CSEMap.InsertNode(N, IP);
10882 InsertNode(N);
10883 SDValue V(N, 0);
10884 NewSDValueDbgMsg(V, "Creating new node: ", this);
10885 return V;
10886}
10887
10889 SDValue Ptr, SDValue Mask, SDValue EVL,
10890 MachinePointerInfo PtrInfo,
10891 MaybeAlign Alignment,
10892 MachineMemOperand::Flags MMOFlags,
10893 const AAMDNodes &AAInfo, const MDNode *Ranges,
10894 bool IsExpanding) {
10896 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10897 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10898 IsExpanding);
10899}
10900
10902 SDValue Ptr, SDValue Mask, SDValue EVL,
10903 MachineMemOperand *MMO, bool IsExpanding) {
10905 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10906 Mask, EVL, VT, MMO, IsExpanding);
10907}
10908
10910 EVT VT, SDValue Chain, SDValue Ptr,
10911 SDValue Mask, SDValue EVL,
10912 MachinePointerInfo PtrInfo, EVT MemVT,
10913 MaybeAlign Alignment,
10914 MachineMemOperand::Flags MMOFlags,
10915 const AAMDNodes &AAInfo, bool IsExpanding) {
10917 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10918 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10919 IsExpanding);
10920}
10921
10923 EVT VT, SDValue Chain, SDValue Ptr,
10924 SDValue Mask, SDValue EVL, EVT MemVT,
10925 MachineMemOperand *MMO, bool IsExpanding) {
10927 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10928 EVL, MemVT, MMO, IsExpanding);
10929}
10930
10934 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10935 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10936 // Don't propagate the invariant or dereferenceable flags.
10937 auto MMOFlags =
10938 LD->getMemOperand()->getFlags() &
10940 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10941 LD->getChain(), Base, Offset, LD->getMask(),
10942 LD->getVectorLength(), LD->getPointerInfo(),
10943 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10944 nullptr, LD->isExpandingLoad());
10945}
10946
10948 SDValue Ptr, SDValue Offset, SDValue Mask,
10949 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10950 ISD::MemIndexedMode AM, bool IsTruncating,
10951 bool IsCompressing) {
10952 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10953 assert(Mask.getValueType().getVectorElementCount() ==
10955 "Vector width mismatch between mask and data");
10956
10957 bool Indexed = AM != ISD::UNINDEXED;
10958 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10959 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10960 : getVTList(MVT::Other);
10961 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10963 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10964 ID.AddInteger(MemVT.getRawBits());
10965 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10966 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10967 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10968 ID.AddInteger(MMO->getFlags());
10969 void *IP = nullptr;
10970 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10971 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10972 return SDValue(E, 0);
10973 }
10974 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10975 IsTruncating, IsCompressing, MemVT, MMO);
10976 createOperands(N, Ops);
10977
10978 CSEMap.InsertNode(N, IP);
10979 InsertNode(N);
10980 SDValue V(N, 0);
10981 NewSDValueDbgMsg(V, "Creating new node: ", this);
10982 return V;
10983}
10984
10986 SDValue Val, SDValue Ptr, SDValue Mask,
10987 SDValue EVL, MachinePointerInfo PtrInfo,
10988 EVT SVT, Align Alignment,
10989 MachineMemOperand::Flags MMOFlags,
10990 const AAMDNodes &AAInfo,
10991 bool IsCompressing) {
10992 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10993
10994 MMOFlags |= MachineMemOperand::MOStore;
10995 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10996
10997 if (PtrInfo.V.isNull())
10998 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10999
11001 MachineMemOperand *MMO = MF.getMachineMemOperand(
11002 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
11003 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
11004 IsCompressing);
11005}
11006
11008 SDValue Val, SDValue Ptr, SDValue Mask,
11009 SDValue EVL, EVT SVT,
11010 MachineMemOperand *MMO,
11011 bool IsCompressing) {
11012 EVT VT = Val.getValueType();
11013
11014 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
11015 if (VT == SVT)
11016 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
11017 EVL, VT, MMO, ISD::UNINDEXED,
11018 /*IsTruncating*/ false, IsCompressing);
11019
11021 "Should only be a truncating store, not extending!");
11022 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
11023 assert(VT.isVector() == SVT.isVector() &&
11024 "Cannot use trunc store to convert to or from a vector!");
11025 assert((!VT.isVector() ||
11027 "Cannot use trunc store to change the number of vector elements!");
11028
11029 SDVTList VTs = getVTList(MVT::Other);
11031 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
11033 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
11034 ID.AddInteger(SVT.getRawBits());
11035 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
11036 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
11037 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11038 ID.AddInteger(MMO->getFlags());
11039 void *IP = nullptr;
11040 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11041 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
11042 return SDValue(E, 0);
11043 }
11044 auto *N =
11045 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
11046 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
11047 createOperands(N, Ops);
11048
11049 CSEMap.InsertNode(N, IP);
11050 InsertNode(N);
11051 SDValue V(N, 0);
11052 NewSDValueDbgMsg(V, "Creating new node: ", this);
11053 return V;
11054}
11055
11059 auto *ST = cast<VPStoreSDNode>(OrigStore);
11060 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
11061 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
11062 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
11063 Offset, ST->getMask(), ST->getVectorLength()};
11065 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
11066 ID.AddInteger(ST->getMemoryVT().getRawBits());
11067 ID.AddInteger(ST->getRawSubclassData());
11068 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
11069 ID.AddInteger(ST->getMemOperand()->getFlags());
11070 void *IP = nullptr;
11071 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
11072 return SDValue(E, 0);
11073
11074 auto *N = newSDNode<VPStoreSDNode>(
11075 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
11076 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
11077 createOperands(N, Ops);
11078
11079 CSEMap.InsertNode(N, IP);
11080 InsertNode(N);
11081 SDValue V(N, 0);
11082 NewSDValueDbgMsg(V, "Creating new node: ", this);
11083 return V;
11084}
11085
11087 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
11088 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
11089 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
11090 bool Indexed = AM != ISD::UNINDEXED;
11091 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
11092
11093 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
11094 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
11095 : getVTList(VT, MVT::Other);
11097 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
11098 ID.AddInteger(VT.getRawBits());
11099 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
11100 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
11101 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11102
11103 void *IP = nullptr;
11104 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11105 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
11106 return SDValue(E, 0);
11107 }
11108
11109 auto *N =
11110 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
11111 ExtType, IsExpanding, MemVT, MMO);
11112 createOperands(N, Ops);
11113 CSEMap.InsertNode(N, IP);
11114 InsertNode(N);
11115 SDValue V(N, 0);
11116 NewSDValueDbgMsg(V, "Creating new node: ", this);
11117 return V;
11118}
11119
11121 SDValue Ptr, SDValue Stride,
11122 SDValue Mask, SDValue EVL,
11123 MachineMemOperand *MMO,
11124 bool IsExpanding) {
11126 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
11127 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
11128}
11129
11131 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
11132 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
11133 MachineMemOperand *MMO, bool IsExpanding) {
11135 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
11136 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
11137}
11138
11140 SDValue Val, SDValue Ptr,
11141 SDValue Offset, SDValue Stride,
11142 SDValue Mask, SDValue EVL, EVT MemVT,
11143 MachineMemOperand *MMO,
11145 bool IsTruncating, bool IsCompressing) {
11146 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
11147 bool Indexed = AM != ISD::UNINDEXED;
11148 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
11149 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
11150 : getVTList(MVT::Other);
11151 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
11153 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
11154 ID.AddInteger(MemVT.getRawBits());
11155 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
11156 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
11157 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11158 void *IP = nullptr;
11159 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11160 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
11161 return SDValue(E, 0);
11162 }
11163 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
11164 VTs, AM, IsTruncating,
11165 IsCompressing, MemVT, MMO);
11166 createOperands(N, Ops);
11167
11168 CSEMap.InsertNode(N, IP);
11169 InsertNode(N);
11170 SDValue V(N, 0);
11171 NewSDValueDbgMsg(V, "Creating new node: ", this);
11172 return V;
11173}
11174
11176 SDValue Val, SDValue Ptr,
11177 SDValue Stride, SDValue Mask,
11178 SDValue EVL, EVT SVT,
11179 MachineMemOperand *MMO,
11180 bool IsCompressing) {
11181 EVT VT = Val.getValueType();
11182
11183 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
11184 if (VT == SVT)
11185 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
11186 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
11187 /*IsTruncating*/ false, IsCompressing);
11188
11190 "Should only be a truncating store, not extending!");
11191 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
11192 assert(VT.isVector() == SVT.isVector() &&
11193 "Cannot use trunc store to convert to or from a vector!");
11194 assert((!VT.isVector() ||
11196 "Cannot use trunc store to change the number of vector elements!");
11197
11198 SDVTList VTs = getVTList(MVT::Other);
11200 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
11202 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
11203 ID.AddInteger(SVT.getRawBits());
11204 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
11205 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
11206 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11207 void *IP = nullptr;
11208 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11209 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
11210 return SDValue(E, 0);
11211 }
11212 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
11213 VTs, ISD::UNINDEXED, true,
11214 IsCompressing, SVT, MMO);
11215 createOperands(N, Ops);
11216
11217 CSEMap.InsertNode(N, IP);
11218 InsertNode(N);
11219 SDValue V(N, 0);
11220 NewSDValueDbgMsg(V, "Creating new node: ", this);
11221 return V;
11222}
11223
11226 ISD::MemIndexType IndexType) {
11227 assert(Ops.size() == 6 && "Incompatible number of operands");
11228
11230 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
11231 ID.AddInteger(VT.getRawBits());
11232 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
11233 dl.getIROrder(), VTs, VT, MMO, IndexType));
11234 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11235 ID.AddInteger(MMO->getFlags());
11236 void *IP = nullptr;
11237 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11238 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
11239 return SDValue(E, 0);
11240 }
11241
11242 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
11243 VT, MMO, IndexType);
11244 createOperands(N, Ops);
11245
11246 assert(N->getMask().getValueType().getVectorElementCount() ==
11247 N->getValueType(0).getVectorElementCount() &&
11248 "Vector width mismatch between mask and data");
11249 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11250 N->getValueType(0).getVectorElementCount().isScalable() &&
11251 "Scalable flags of index and data do not match");
11253 N->getIndex().getValueType().getVectorElementCount(),
11254 N->getValueType(0).getVectorElementCount()) &&
11255 "Vector width mismatch between index and data");
11256 assert(isa<ConstantSDNode>(N->getScale()) &&
11257 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11258 "Scale should be a constant power of 2");
11259
11260 CSEMap.InsertNode(N, IP);
11261 InsertNode(N);
11262 SDValue V(N, 0);
11263 NewSDValueDbgMsg(V, "Creating new node: ", this);
11264 return V;
11265}
11266
11269 MachineMemOperand *MMO,
11270 ISD::MemIndexType IndexType) {
11271 assert(Ops.size() == 7 && "Incompatible number of operands");
11272
11274 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
11275 ID.AddInteger(VT.getRawBits());
11276 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
11277 dl.getIROrder(), VTs, VT, MMO, IndexType));
11278 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11279 ID.AddInteger(MMO->getFlags());
11280 void *IP = nullptr;
11281 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11282 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
11283 return SDValue(E, 0);
11284 }
11285 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
11286 VT, MMO, IndexType);
11287 createOperands(N, Ops);
11288
11289 assert(N->getMask().getValueType().getVectorElementCount() ==
11290 N->getValue().getValueType().getVectorElementCount() &&
11291 "Vector width mismatch between mask and data");
11292 assert(
11293 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11294 N->getValue().getValueType().getVectorElementCount().isScalable() &&
11295 "Scalable flags of index and data do not match");
11297 N->getIndex().getValueType().getVectorElementCount(),
11298 N->getValue().getValueType().getVectorElementCount()) &&
11299 "Vector width mismatch between index and data");
11300 assert(isa<ConstantSDNode>(N->getScale()) &&
11301 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11302 "Scale should be a constant power of 2");
11303
11304 CSEMap.InsertNode(N, IP);
11305 InsertNode(N);
11306 SDValue V(N, 0);
11307 NewSDValueDbgMsg(V, "Creating new node: ", this);
11308 return V;
11309}
11310
11313 SDValue PassThru, EVT MemVT,
11314 MachineMemOperand *MMO,
11316 ISD::LoadExtType ExtTy, bool isExpanding) {
11317 bool Indexed = AM != ISD::UNINDEXED;
11318 assert((Indexed || Offset.isUndef()) &&
11319 "Unindexed masked load with an offset!");
11320 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
11321 : getVTList(VT, MVT::Other);
11322 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
11325 ID.AddInteger(MemVT.getRawBits());
11326 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
11327 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
11328 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11329 ID.AddInteger(MMO->getFlags());
11330 void *IP = nullptr;
11331 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11332 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
11333 return SDValue(E, 0);
11334 }
11335 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
11336 AM, ExtTy, isExpanding, MemVT, MMO);
11337 createOperands(N, Ops);
11338
11339 CSEMap.InsertNode(N, IP);
11340 InsertNode(N);
11341 SDValue V(N, 0);
11342 NewSDValueDbgMsg(V, "Creating new node: ", this);
11343 return V;
11344}
11345
11350 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
11351 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
11352 Offset, LD->getMask(), LD->getPassThru(),
11353 LD->getMemoryVT(), LD->getMemOperand(), AM,
11354 LD->getExtensionType(), LD->isExpandingLoad());
11355}
11356
11359 SDValue Mask, EVT MemVT,
11360 MachineMemOperand *MMO,
11361 ISD::MemIndexedMode AM, bool IsTruncating,
11362 bool IsCompressing) {
11363 assert(Chain.getValueType() == MVT::Other &&
11364 "Invalid chain type");
11365 bool Indexed = AM != ISD::UNINDEXED;
11366 assert((Indexed || Offset.isUndef()) &&
11367 "Unindexed masked store with an offset!");
11368 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
11369 : getVTList(MVT::Other);
11370 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
11373 ID.AddInteger(MemVT.getRawBits());
11374 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
11375 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
11376 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11377 ID.AddInteger(MMO->getFlags());
11378 void *IP = nullptr;
11379 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11380 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
11381 return SDValue(E, 0);
11382 }
11383 auto *N =
11384 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
11385 IsTruncating, IsCompressing, MemVT, MMO);
11386 createOperands(N, Ops);
11387
11388 CSEMap.InsertNode(N, IP);
11389 InsertNode(N);
11390 SDValue V(N, 0);
11391 NewSDValueDbgMsg(V, "Creating new node: ", this);
11392 return V;
11393}
11394
11399 assert(ST->getOffset().isUndef() &&
11400 "Masked store is already a indexed store!");
11401 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
11402 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
11403 AM, ST->isTruncatingStore(), ST->isCompressingStore());
11404}
11405
11408 MachineMemOperand *MMO,
11409 ISD::MemIndexType IndexType,
11410 ISD::LoadExtType ExtTy) {
11411 assert(Ops.size() == 6 && "Incompatible number of operands");
11412
11415 ID.AddInteger(MemVT.getRawBits());
11416 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
11417 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
11418 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11419 ID.AddInteger(MMO->getFlags());
11420 void *IP = nullptr;
11421 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11422 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
11423 return SDValue(E, 0);
11424 }
11425
11426 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
11427 VTs, MemVT, MMO, IndexType, ExtTy);
11428 createOperands(N, Ops);
11429
11430 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
11431 "Incompatible type of the PassThru value in MaskedGatherSDNode");
11432 assert(N->getMask().getValueType().getVectorElementCount() ==
11433 N->getValueType(0).getVectorElementCount() &&
11434 "Vector width mismatch between mask and data");
11435 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11436 N->getValueType(0).getVectorElementCount().isScalable() &&
11437 "Scalable flags of index and data do not match");
11439 N->getIndex().getValueType().getVectorElementCount(),
11440 N->getValueType(0).getVectorElementCount()) &&
11441 "Vector width mismatch between index and data");
11442 assert(isa<ConstantSDNode>(N->getScale()) &&
11443 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11444 "Scale should be a constant power of 2");
11445
11446 CSEMap.InsertNode(N, IP);
11447 InsertNode(N);
11448 SDValue V(N, 0);
11449 NewSDValueDbgMsg(V, "Creating new node: ", this);
11450 return V;
11451}
11452
11455 MachineMemOperand *MMO,
11456 ISD::MemIndexType IndexType,
11457 bool IsTrunc) {
11458 assert(Ops.size() == 6 && "Incompatible number of operands");
11459
11462 ID.AddInteger(MemVT.getRawBits());
11463 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
11464 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
11465 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11466 ID.AddInteger(MMO->getFlags());
11467 void *IP = nullptr;
11468 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11469 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
11470 return SDValue(E, 0);
11471 }
11472
11473 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
11474 VTs, MemVT, MMO, IndexType, IsTrunc);
11475 createOperands(N, Ops);
11476
11477 assert(N->getMask().getValueType().getVectorElementCount() ==
11478 N->getValue().getValueType().getVectorElementCount() &&
11479 "Vector width mismatch between mask and data");
11480 assert(
11481 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11482 N->getValue().getValueType().getVectorElementCount().isScalable() &&
11483 "Scalable flags of index and data do not match");
11485 N->getIndex().getValueType().getVectorElementCount(),
11486 N->getValue().getValueType().getVectorElementCount()) &&
11487 "Vector width mismatch between index and data");
11488 assert(isa<ConstantSDNode>(N->getScale()) &&
11489 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11490 "Scale should be a constant power of 2");
11491
11492 CSEMap.InsertNode(N, IP);
11493 InsertNode(N);
11494 SDValue V(N, 0);
11495 NewSDValueDbgMsg(V, "Creating new node: ", this);
11496 return V;
11497}
11498
11500 const SDLoc &dl, ArrayRef<SDValue> Ops,
11501 MachineMemOperand *MMO,
11502 ISD::MemIndexType IndexType) {
11503 assert(Ops.size() == 7 && "Incompatible number of operands");
11504
11507 ID.AddInteger(MemVT.getRawBits());
11508 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
11509 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
11510 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11511 ID.AddInteger(MMO->getFlags());
11512 void *IP = nullptr;
11513 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11514 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
11515 return SDValue(E, 0);
11516 }
11517
11518 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
11519 VTs, MemVT, MMO, IndexType);
11520 createOperands(N, Ops);
11521
11522 assert(N->getMask().getValueType().getVectorElementCount() ==
11523 N->getIndex().getValueType().getVectorElementCount() &&
11524 "Vector width mismatch between mask and data");
11525 assert(isa<ConstantSDNode>(N->getScale()) &&
11526 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11527 "Scale should be a constant power of 2");
11528 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
11529
11530 CSEMap.InsertNode(N, IP);
11531 InsertNode(N);
11532 SDValue V(N, 0);
11533 NewSDValueDbgMsg(V, "Creating new node: ", this);
11534 return V;
11535}
11536
11538 SDValue Ptr, SDValue Mask, SDValue EVL,
11539 MachineMemOperand *MMO) {
11540 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
11541 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
11543 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
11544 ID.AddInteger(VT.getRawBits());
11545 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
11546 VTs, VT, MMO));
11547 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11548 ID.AddInteger(MMO->getFlags());
11549 void *IP = nullptr;
11550 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11551 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
11552 return SDValue(E, 0);
11553 }
11554 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
11555 VT, MMO);
11556 createOperands(N, Ops);
11557
11558 CSEMap.InsertNode(N, IP);
11559 InsertNode(N);
11560 SDValue V(N, 0);
11561 NewSDValueDbgMsg(V, "Creating new node: ", this);
11562 return V;
11563}
11564
11566 EVT MemVT, MachineMemOperand *MMO) {
11567 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
11568 SDVTList VTs = getVTList(MVT::Other);
11569 SDValue Ops[] = {Chain, Ptr};
11572 ID.AddInteger(MemVT.getRawBits());
11573 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
11574 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
11575 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11576 ID.AddInteger(MMO->getFlags());
11577 void *IP = nullptr;
11578 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
11579 return SDValue(E, 0);
11580
11581 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
11582 dl.getDebugLoc(), VTs, MemVT, MMO);
11583 createOperands(N, Ops);
11584
11585 CSEMap.InsertNode(N, IP);
11586 InsertNode(N);
11587 SDValue V(N, 0);
11588 NewSDValueDbgMsg(V, "Creating new node: ", this);
11589 return V;
11590}
11591
11593 EVT MemVT, MachineMemOperand *MMO) {
11594 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
11595 SDVTList VTs = getVTList(MVT::Other);
11596 SDValue Ops[] = {Chain, Ptr};
11599 ID.AddInteger(MemVT.getRawBits());
11600 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
11601 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
11602 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11603 ID.AddInteger(MMO->getFlags());
11604 void *IP = nullptr;
11605 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
11606 return SDValue(E, 0);
11607
11608 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
11609 dl.getDebugLoc(), VTs, MemVT, MMO);
11610 createOperands(N, Ops);
11611
11612 CSEMap.InsertNode(N, IP);
11613 InsertNode(N);
11614 SDValue V(N, 0);
11615 NewSDValueDbgMsg(V, "Creating new node: ", this);
11616 return V;
11617}
11618
11620 // select undef, T, F --> T (if T is a constant), otherwise F
11621 // select, ?, undef, F --> F
11622 // select, ?, T, undef --> T
11623 if (Cond.isUndef())
11624 return isConstantValueOfAnyType(T) ? T : F;
11625 if (T.isUndef())
11627 if (F.isUndef())
11629
11630 // select true, T, F --> T
11631 // select false, T, F --> F
11632 if (auto C = isBoolConstant(Cond))
11633 return *C ? T : F;
11634
11635 // select ?, T, T --> T
11636 if (T == F)
11637 return T;
11638
11639 return SDValue();
11640}
11641
11643 // shift undef, Y --> 0 (can always assume that the undef value is 0)
11644 if (X.isUndef())
11645 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
11646 // shift X, undef --> undef (because it may shift by the bitwidth)
11647 if (Y.isUndef())
11648 return getUNDEF(X.getValueType());
11649
11650 // shift 0, Y --> 0
11651 // shift X, 0 --> X
11653 return X;
11654
11655 // shift X, C >= bitwidth(X) --> undef
11656 // All vector elements must be too big (or undef) to avoid partial undefs.
11657 auto isShiftTooBig = [X](ConstantSDNode *Val) {
11658 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
11659 };
11660 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
11661 return getUNDEF(X.getValueType());
11662
11663 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
11664 if (X.getValueType().getScalarType() == MVT::i1)
11665 return X;
11666
11667 return SDValue();
11668}
11669
11671 SDNodeFlags Flags) {
11672 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
11673 // (an undef operand can be chosen to be Nan/Inf), then the result of this
11674 // operation is poison. That result can be relaxed to undef.
11675 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
11676 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
11677 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
11678 (YC && YC->getValueAPF().isNaN());
11679 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
11680 (YC && YC->getValueAPF().isInfinity());
11681
11682 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
11683 return getUNDEF(X.getValueType());
11684
11685 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
11686 return getUNDEF(X.getValueType());
11687
11688 if (!YC)
11689 return SDValue();
11690
11691 // X + -0.0 --> X
11692 if (Opcode == ISD::FADD)
11693 if (YC->getValueAPF().isNegZero())
11694 return X;
11695
11696 // X - +0.0 --> X
11697 if (Opcode == ISD::FSUB)
11698 if (YC->getValueAPF().isPosZero())
11699 return X;
11700
11701 // X * 1.0 --> X
11702 // X / 1.0 --> X
11703 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
11704 if (YC->getValueAPF().isOne())
11705 return X;
11706
11707 // X * 0.0 --> 0.0
11708 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
11709 if (YC->getValueAPF().isZero())
11710 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
11711
11712 return SDValue();
11713}
11714
11716 SDValue Ptr, SDValue SV, unsigned Align) {
11717 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
11718 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
11719}
11720
11721SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11723 switch (Ops.size()) {
11724 case 0: return getNode(Opcode, DL, VT);
11725 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
11726 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
11727 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
11728 default: break;
11729 }
11730
11731 // Copy from an SDUse array into an SDValue array for use with
11732 // the regular getNode logic.
11734 return getNode(Opcode, DL, VT, NewOps);
11735}
11736
11737SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11739 SDNodeFlags Flags;
11740 if (Inserter)
11741 Flags = Inserter->getFlags();
11742 return getNode(Opcode, DL, VT, Ops, Flags);
11743}
11744
11745SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11746 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11747 unsigned NumOps = Ops.size();
11748 switch (NumOps) {
11749 case 0: return getNode(Opcode, DL, VT);
11750 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
11751 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
11752 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
11753 default: break;
11754 }
11755
11756#ifndef NDEBUG
11757 for (const auto &Op : Ops)
11758 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11759 "Operand is DELETED_NODE!");
11760#endif
11761
11762 switch (Opcode) {
11763 default: break;
11764 case ISD::BUILD_VECTOR:
11765 // Attempt to simplify BUILD_VECTOR.
11766 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
11767 return V;
11768 break;
11770 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
11771 return V;
11772 break;
11773 case ISD::SELECT_CC:
11774 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
11775 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
11776 "LHS and RHS of condition must have same type!");
11777 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11778 "True and False arms of SelectCC must have same type!");
11779 assert(Ops[2].getValueType() == VT &&
11780 "select_cc node must be of same type as true and false value!");
11781 assert((!Ops[0].getValueType().isVector() ||
11782 Ops[0].getValueType().getVectorElementCount() ==
11783 VT.getVectorElementCount()) &&
11784 "Expected select_cc with vector result to have the same sized "
11785 "comparison type!");
11786 break;
11787 case ISD::BR_CC:
11788 assert(NumOps == 5 && "BR_CC takes 5 operands!");
11789 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11790 "LHS/RHS of comparison should match types!");
11791 break;
11792 case ISD::VP_ADD:
11793 case ISD::VP_SUB:
11794 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
11795 if (VT.getScalarType() == MVT::i1)
11796 Opcode = ISD::VP_XOR;
11797 break;
11798 case ISD::VP_MUL:
11799 // If it is VP_MUL mask operation then turn it to VP_AND
11800 if (VT.getScalarType() == MVT::i1)
11801 Opcode = ISD::VP_AND;
11802 break;
11803 case ISD::VP_REDUCE_MUL:
11804 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
11805 if (VT == MVT::i1)
11806 Opcode = ISD::VP_REDUCE_AND;
11807 break;
11808 case ISD::VP_REDUCE_ADD:
11809 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
11810 if (VT == MVT::i1)
11811 Opcode = ISD::VP_REDUCE_XOR;
11812 break;
11813 case ISD::VP_REDUCE_SMAX:
11814 case ISD::VP_REDUCE_UMIN:
11815 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
11816 // VP_REDUCE_AND.
11817 if (VT == MVT::i1)
11818 Opcode = ISD::VP_REDUCE_AND;
11819 break;
11820 case ISD::VP_REDUCE_SMIN:
11821 case ISD::VP_REDUCE_UMAX:
11822 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
11823 // VP_REDUCE_OR.
11824 if (VT == MVT::i1)
11825 Opcode = ISD::VP_REDUCE_OR;
11826 break;
11827 }
11828
11829 // Memoize nodes.
11830 SDNode *N;
11831 SDVTList VTs = getVTList(VT);
11832
11833 if (VT != MVT::Glue) {
11835 AddNodeIDNode(ID, Opcode, VTs, Ops);
11836 void *IP = nullptr;
11837
11838 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11839 E->intersectFlagsWith(Flags);
11840 return SDValue(E, 0);
11841 }
11842
11843 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11844 createOperands(N, Ops);
11845
11846 CSEMap.InsertNode(N, IP);
11847 } else {
11848 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11849 createOperands(N, Ops);
11850 }
11851
11852 N->setFlags(Flags);
11853 InsertNode(N);
11854 SDValue V(N, 0);
11855 NewSDValueDbgMsg(V, "Creating new node: ", this);
11856 return V;
11857}
11858
11859SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11860 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11861 SDNodeFlags Flags;
11862 if (Inserter)
11863 Flags = Inserter->getFlags();
11864 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11865}
11866
11867SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11869 const SDNodeFlags Flags) {
11870 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11871}
11872
11873SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11875 SDNodeFlags Flags;
11876 if (Inserter)
11877 Flags = Inserter->getFlags();
11878 return getNode(Opcode, DL, VTList, Ops, Flags);
11879}
11880
11881SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11882 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11883 if (VTList.NumVTs == 1)
11884 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11885
11886#ifndef NDEBUG
11887 for (const auto &Op : Ops)
11888 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11889 "Operand is DELETED_NODE!");
11890#endif
11891
11892 switch (Opcode) {
11893 case ISD::SADDO:
11894 case ISD::UADDO:
11895 case ISD::SSUBO:
11896 case ISD::USUBO: {
11897 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11898 "Invalid add/sub overflow op!");
11899 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11900 Ops[0].getValueType() == Ops[1].getValueType() &&
11901 Ops[0].getValueType() == VTList.VTs[0] &&
11902 "Binary operator types must match!");
11903 SDValue N1 = Ops[0], N2 = Ops[1];
11904 canonicalizeCommutativeBinop(Opcode, N1, N2);
11905
11906 // (X +- 0) -> X with zero-overflow.
11907 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11908 /*AllowTruncation*/ true);
11909 if (N2CV && N2CV->isZero()) {
11910 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11911 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11912 }
11913
11914 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11915 VTList.VTs[1].getScalarType() == MVT::i1) {
11916 SDValue F1 = getFreeze(N1);
11917 SDValue F2 = getFreeze(N2);
11918 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11919 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11920 return getNode(ISD::MERGE_VALUES, DL, VTList,
11921 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11922 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11923 Flags);
11924 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11925 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11926 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11927 return getNode(ISD::MERGE_VALUES, DL, VTList,
11928 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11929 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11930 Flags);
11931 }
11932 }
11933 break;
11934 }
11935 case ISD::SADDO_CARRY:
11936 case ISD::UADDO_CARRY:
11937 case ISD::SSUBO_CARRY:
11938 case ISD::USUBO_CARRY:
11939 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11940 "Invalid add/sub overflow op!");
11941 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11942 Ops[0].getValueType() == Ops[1].getValueType() &&
11943 Ops[0].getValueType() == VTList.VTs[0] &&
11944 Ops[2].getValueType() == VTList.VTs[1] &&
11945 "Binary operator types must match!");
11946 break;
11947 case ISD::SMUL_LOHI:
11948 case ISD::UMUL_LOHI: {
11949 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11950 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11951 VTList.VTs[0] == Ops[0].getValueType() &&
11952 VTList.VTs[0] == Ops[1].getValueType() &&
11953 "Binary operator types must match!");
11954 // Constant fold.
11957 if (LHS && RHS) {
11958 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11959 unsigned OutWidth = Width * 2;
11960 APInt Val = LHS->getAPIntValue();
11961 APInt Mul = RHS->getAPIntValue();
11962 if (Opcode == ISD::SMUL_LOHI) {
11963 Val = Val.sext(OutWidth);
11964 Mul = Mul.sext(OutWidth);
11965 } else {
11966 Val = Val.zext(OutWidth);
11967 Mul = Mul.zext(OutWidth);
11968 }
11969 Val *= Mul;
11970
11971 SDValue Hi =
11972 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11973 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11974 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11975 }
11976 break;
11977 }
11978 case ISD::FFREXP: {
11979 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11980 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11981 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11982
11984 int FrexpExp;
11985 APFloat FrexpMant =
11986 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11987 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11988 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11989 DL, VTList.VTs[1]);
11990 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11991 }
11992
11993 break;
11994 }
11996 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11997 "Invalid STRICT_FP_EXTEND!");
11998 assert(VTList.VTs[0].isFloatingPoint() &&
11999 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
12000 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
12001 "STRICT_FP_EXTEND result type should be vector iff the operand "
12002 "type is vector!");
12003 assert((!VTList.VTs[0].isVector() ||
12004 VTList.VTs[0].getVectorElementCount() ==
12005 Ops[1].getValueType().getVectorElementCount()) &&
12006 "Vector element count mismatch!");
12007 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
12008 "Invalid fpext node, dst <= src!");
12009 break;
12011 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
12012 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
12013 "STRICT_FP_ROUND result type should be vector iff the operand "
12014 "type is vector!");
12015 assert((!VTList.VTs[0].isVector() ||
12016 VTList.VTs[0].getVectorElementCount() ==
12017 Ops[1].getValueType().getVectorElementCount()) &&
12018 "Vector element count mismatch!");
12019 assert(VTList.VTs[0].isFloatingPoint() &&
12020 Ops[1].getValueType().isFloatingPoint() &&
12021 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
12022 Ops[2].getOpcode() == ISD::TargetConstant &&
12023 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
12024 "Invalid STRICT_FP_ROUND!");
12025 break;
12026 }
12027
12028 // Memoize the node unless it returns a glue result.
12029 SDNode *N;
12030 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
12032 AddNodeIDNode(ID, Opcode, VTList, Ops);
12033 void *IP = nullptr;
12034 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
12035 E->intersectFlagsWith(Flags);
12036 return SDValue(E, 0);
12037 }
12038
12039 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
12040 createOperands(N, Ops);
12041 CSEMap.InsertNode(N, IP);
12042 } else {
12043 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
12044 createOperands(N, Ops);
12045 }
12046
12047 N->setFlags(Flags);
12048 InsertNode(N);
12049 SDValue V(N, 0);
12050 NewSDValueDbgMsg(V, "Creating new node: ", this);
12051 return V;
12052}
12053
12054SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
12055 SDVTList VTList) {
12056 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
12057}
12058
12059SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
12060 SDValue N1) {
12061 SDValue Ops[] = { N1 };
12062 return getNode(Opcode, DL, VTList, Ops);
12063}
12064
12065SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
12066 SDValue N1, SDValue N2) {
12067 SDValue Ops[] = { N1, N2 };
12068 return getNode(Opcode, DL, VTList, Ops);
12069}
12070
12071SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
12072 SDValue N1, SDValue N2, SDValue N3) {
12073 SDValue Ops[] = { N1, N2, N3 };
12074 return getNode(Opcode, DL, VTList, Ops);
12075}
12076
12077SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
12078 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
12079 SDValue Ops[] = { N1, N2, N3, N4 };
12080 return getNode(Opcode, DL, VTList, Ops);
12081}
12082
12083SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
12084 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
12085 SDValue N5) {
12086 SDValue Ops[] = { N1, N2, N3, N4, N5 };
12087 return getNode(Opcode, DL, VTList, Ops);
12088}
12089
12091 if (!VT.isExtended())
12092 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
12093
12094 return makeVTList(&(*EVTs.insert(VT).first), 1);
12095}
12096
12099 ID.AddInteger(2U);
12100 ID.AddInteger(VT1.getRawBits());
12101 ID.AddInteger(VT2.getRawBits());
12102
12103 void *IP = nullptr;
12104 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
12105 if (!Result) {
12106 EVT *Array = Allocator.Allocate<EVT>(2);
12107 Array[0] = VT1;
12108 Array[1] = VT2;
12109 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
12110 VTListMap.InsertNode(Result, IP);
12111 }
12112 return Result->getSDVTList();
12113}
12114
12117 ID.AddInteger(3U);
12118 ID.AddInteger(VT1.getRawBits());
12119 ID.AddInteger(VT2.getRawBits());
12120 ID.AddInteger(VT3.getRawBits());
12121
12122 void *IP = nullptr;
12123 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
12124 if (!Result) {
12125 EVT *Array = Allocator.Allocate<EVT>(3);
12126 Array[0] = VT1;
12127 Array[1] = VT2;
12128 Array[2] = VT3;
12129 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
12130 VTListMap.InsertNode(Result, IP);
12131 }
12132 return Result->getSDVTList();
12133}
12134
12137 ID.AddInteger(4U);
12138 ID.AddInteger(VT1.getRawBits());
12139 ID.AddInteger(VT2.getRawBits());
12140 ID.AddInteger(VT3.getRawBits());
12141 ID.AddInteger(VT4.getRawBits());
12142
12143 void *IP = nullptr;
12144 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
12145 if (!Result) {
12146 EVT *Array = Allocator.Allocate<EVT>(4);
12147 Array[0] = VT1;
12148 Array[1] = VT2;
12149 Array[2] = VT3;
12150 Array[3] = VT4;
12151 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
12152 VTListMap.InsertNode(Result, IP);
12153 }
12154 return Result->getSDVTList();
12155}
12156
12158 unsigned NumVTs = VTs.size();
12160 ID.AddInteger(NumVTs);
12161 for (unsigned index = 0; index < NumVTs; index++) {
12162 ID.AddInteger(VTs[index].getRawBits());
12163 }
12164
12165 void *IP = nullptr;
12166 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
12167 if (!Result) {
12168 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
12169 llvm::copy(VTs, Array);
12170 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
12171 VTListMap.InsertNode(Result, IP);
12172 }
12173 return Result->getSDVTList();
12174}
12175
12176
12177/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
12178/// specified operands. If the resultant node already exists in the DAG,
12179/// this does not modify the specified node, instead it returns the node that
12180/// already exists. If the resultant node does not exist in the DAG, the
12181/// input node is returned. As a degenerate case, if you specify the same
12182/// input operands as the node already has, the input node is returned.
12184 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
12185
12186 // Check to see if there is no change.
12187 if (Op == N->getOperand(0)) return N;
12188
12189 // See if the modified node already exists.
12190 void *InsertPos = nullptr;
12191 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
12192 return Existing;
12193
12194 // Nope it doesn't. Remove the node from its current place in the maps.
12195 if (InsertPos)
12196 if (!RemoveNodeFromCSEMaps(N))
12197 InsertPos = nullptr;
12198
12199 // Now we update the operands.
12200 N->OperandList[0].set(Op);
12201
12203 // If this gets put into a CSE map, add it.
12204 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
12205 return N;
12206}
12207
12209 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
12210
12211 // Check to see if there is no change.
12212 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
12213 return N; // No operands changed, just return the input node.
12214
12215 // See if the modified node already exists.
12216 void *InsertPos = nullptr;
12217 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
12218 return Existing;
12219
12220 // Nope it doesn't. Remove the node from its current place in the maps.
12221 if (InsertPos)
12222 if (!RemoveNodeFromCSEMaps(N))
12223 InsertPos = nullptr;
12224
12225 // Now we update the operands.
12226 if (N->OperandList[0] != Op1)
12227 N->OperandList[0].set(Op1);
12228 if (N->OperandList[1] != Op2)
12229 N->OperandList[1].set(Op2);
12230
12232 // If this gets put into a CSE map, add it.
12233 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
12234 return N;
12235}
12236
12239 SDValue Ops[] = { Op1, Op2, Op3 };
12240 return UpdateNodeOperands(N, Ops);
12241}
12242
12245 SDValue Op3, SDValue Op4) {
12246 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
12247 return UpdateNodeOperands(N, Ops);
12248}
12249
12252 SDValue Op3, SDValue Op4, SDValue Op5) {
12253 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
12254 return UpdateNodeOperands(N, Ops);
12255}
12256
12259 unsigned NumOps = Ops.size();
12260 assert(N->getNumOperands() == NumOps &&
12261 "Update with wrong number of operands");
12262
12263 // If no operands changed just return the input node.
12264 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
12265 return N;
12266
12267 // See if the modified node already exists.
12268 void *InsertPos = nullptr;
12269 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
12270 return Existing;
12271
12272 // Nope it doesn't. Remove the node from its current place in the maps.
12273 if (InsertPos)
12274 if (!RemoveNodeFromCSEMaps(N))
12275 InsertPos = nullptr;
12276
12277 // Now we update the operands.
12278 for (unsigned i = 0; i != NumOps; ++i)
12279 if (N->OperandList[i] != Ops[i])
12280 N->OperandList[i].set(Ops[i]);
12281
12283 // If this gets put into a CSE map, add it.
12284 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
12285 return N;
12286}
12287
12288/// DropOperands - Release the operands and set this node to have
12289/// zero operands.
12291 // Unlike the code in MorphNodeTo that does this, we don't need to
12292 // watch for dead nodes here.
12293 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
12294 SDUse &Use = *I++;
12295 Use.set(SDValue());
12296 }
12297}
12298
12300 ArrayRef<MachineMemOperand *> NewMemRefs) {
12301 if (NewMemRefs.empty()) {
12302 N->clearMemRefs();
12303 return;
12304 }
12305
12306 // Check if we can avoid allocating by storing a single reference directly.
12307 if (NewMemRefs.size() == 1) {
12308 N->MemRefs = NewMemRefs[0];
12309 N->NumMemRefs = 1;
12310 return;
12311 }
12312
12313 MachineMemOperand **MemRefsBuffer =
12314 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
12315 llvm::copy(NewMemRefs, MemRefsBuffer);
12316 N->MemRefs = MemRefsBuffer;
12317 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
12318}
12319
12320/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
12321/// machine opcode.
12322///
12324 EVT VT) {
12325 SDVTList VTs = getVTList(VT);
12326 return SelectNodeTo(N, MachineOpc, VTs, {});
12327}
12328
12330 EVT VT, SDValue Op1) {
12331 SDVTList VTs = getVTList(VT);
12332 SDValue Ops[] = { Op1 };
12333 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12334}
12335
12337 EVT VT, SDValue Op1,
12338 SDValue Op2) {
12339 SDVTList VTs = getVTList(VT);
12340 SDValue Ops[] = { Op1, Op2 };
12341 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12342}
12343
12345 EVT VT, SDValue Op1,
12346 SDValue Op2, SDValue Op3) {
12347 SDVTList VTs = getVTList(VT);
12348 SDValue Ops[] = { Op1, Op2, Op3 };
12349 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12350}
12351
12354 SDVTList VTs = getVTList(VT);
12355 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12356}
12357
12359 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
12360 SDVTList VTs = getVTList(VT1, VT2);
12361 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12362}
12363
12365 EVT VT1, EVT VT2) {
12366 SDVTList VTs = getVTList(VT1, VT2);
12367 return SelectNodeTo(N, MachineOpc, VTs, {});
12368}
12369
12371 EVT VT1, EVT VT2, EVT VT3,
12373 SDVTList VTs = getVTList(VT1, VT2, VT3);
12374 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12375}
12376
12378 EVT VT1, EVT VT2,
12379 SDValue Op1, SDValue Op2) {
12380 SDVTList VTs = getVTList(VT1, VT2);
12381 SDValue Ops[] = { Op1, Op2 };
12382 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12383}
12384
12387 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
12388 // Reset the NodeID to -1.
12389 New->setNodeId(-1);
12390 if (New != N) {
12391 ReplaceAllUsesWith(N, New);
12393 }
12394 return New;
12395}
12396
12397/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
12398/// the line number information on the merged node since it is not possible to
12399/// preserve the information that operation is associated with multiple lines.
12400/// This will make the debugger working better at -O0, were there is a higher
12401/// probability having other instructions associated with that line.
12402///
12403/// For IROrder, we keep the smaller of the two
12404SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
12405 DebugLoc NLoc = N->getDebugLoc();
12406 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
12407 N->setDebugLoc(DebugLoc());
12408 }
12409 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
12410 N->setIROrder(Order);
12411 return N;
12412}
12413
12414/// MorphNodeTo - This *mutates* the specified node to have the specified
12415/// return type, opcode, and operands.
12416///
12417/// Note that MorphNodeTo returns the resultant node. If there is already a
12418/// node of the specified opcode and operands, it returns that node instead of
12419/// the current one. Note that the SDLoc need not be the same.
12420///
12421/// Using MorphNodeTo is faster than creating a new node and swapping it in
12422/// with ReplaceAllUsesWith both because it often avoids allocating a new
12423/// node, and because it doesn't require CSE recalculation for any of
12424/// the node's users.
12425///
12426/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
12427/// As a consequence it isn't appropriate to use from within the DAG combiner or
12428/// the legalizer which maintain worklists that would need to be updated when
12429/// deleting things.
12432 // If an identical node already exists, use it.
12433 void *IP = nullptr;
12434 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
12436 AddNodeIDNode(ID, Opc, VTs, Ops);
12437 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
12438 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
12439 }
12440
12441 if (!RemoveNodeFromCSEMaps(N))
12442 IP = nullptr;
12443
12444 // Start the morphing.
12445 N->NodeType = Opc;
12446 N->ValueList = VTs.VTs;
12447 N->NumValues = VTs.NumVTs;
12448
12449 // Clear the operands list, updating used nodes to remove this from their
12450 // use list. Keep track of any operands that become dead as a result.
12451 SmallPtrSet<SDNode*, 16> DeadNodeSet;
12452 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
12453 SDUse &Use = *I++;
12454 SDNode *Used = Use.getNode();
12455 Use.set(SDValue());
12456 if (Used->use_empty())
12457 DeadNodeSet.insert(Used);
12458 }
12459
12460 // For MachineNode, initialize the memory references information.
12462 MN->clearMemRefs();
12463
12464 // Swap for an appropriately sized array from the recycler.
12465 removeOperands(N);
12466 createOperands(N, Ops);
12467
12468 // Delete any nodes that are still dead after adding the uses for the
12469 // new operands.
12470 if (!DeadNodeSet.empty()) {
12471 SmallVector<SDNode *, 16> DeadNodes;
12472 for (SDNode *N : DeadNodeSet)
12473 if (N->use_empty())
12474 DeadNodes.push_back(N);
12475 RemoveDeadNodes(DeadNodes);
12476 }
12477
12478 if (IP)
12479 CSEMap.InsertNode(N, IP); // Memoize the new node.
12480 return N;
12481}
12482
12484 unsigned OrigOpc = Node->getOpcode();
12485 unsigned NewOpc;
12486 switch (OrigOpc) {
12487 default:
12488 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
12489#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
12490 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
12491#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
12492 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
12493#include "llvm/IR/ConstrainedOps.def"
12494 }
12495
12496 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
12497
12498 // We're taking this node out of the chain, so we need to re-link things.
12499 SDValue InputChain = Node->getOperand(0);
12500 SDValue OutputChain = SDValue(Node, 1);
12501 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
12502
12504 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
12505 Ops.push_back(Node->getOperand(i));
12506
12507 SDVTList VTs = getVTList(Node->getValueType(0));
12508 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
12509
12510 // MorphNodeTo can operate in two ways: if an existing node with the
12511 // specified operands exists, it can just return it. Otherwise, it
12512 // updates the node in place to have the requested operands.
12513 if (Res == Node) {
12514 // If we updated the node in place, reset the node ID. To the isel,
12515 // this should be just like a newly allocated machine node.
12516 Res->setNodeId(-1);
12517 } else {
12520 }
12521
12522 return Res;
12523}
12524
12525/// getMachineNode - These are used for target selectors to create a new node
12526/// with specified return type(s), MachineInstr opcode, and operands.
12527///
12528/// Note that getMachineNode returns the resultant node. If there is already a
12529/// node of the specified opcode and operands, it returns that node instead of
12530/// the current one.
12532 EVT VT) {
12533 SDVTList VTs = getVTList(VT);
12534 return getMachineNode(Opcode, dl, VTs, {});
12535}
12536
12538 EVT VT, SDValue Op1) {
12539 SDVTList VTs = getVTList(VT);
12540 SDValue Ops[] = { Op1 };
12541 return getMachineNode(Opcode, dl, VTs, Ops);
12542}
12543
12545 EVT VT, SDValue Op1, SDValue Op2) {
12546 SDVTList VTs = getVTList(VT);
12547 SDValue Ops[] = { Op1, Op2 };
12548 return getMachineNode(Opcode, dl, VTs, Ops);
12549}
12550
12552 EVT VT, SDValue Op1, SDValue Op2,
12553 SDValue Op3) {
12554 SDVTList VTs = getVTList(VT);
12555 SDValue Ops[] = { Op1, Op2, Op3 };
12556 return getMachineNode(Opcode, dl, VTs, Ops);
12557}
12558
12561 SDVTList VTs = getVTList(VT);
12562 return getMachineNode(Opcode, dl, VTs, Ops);
12563}
12564
12566 EVT VT1, EVT VT2, SDValue Op1,
12567 SDValue Op2) {
12568 SDVTList VTs = getVTList(VT1, VT2);
12569 SDValue Ops[] = { Op1, Op2 };
12570 return getMachineNode(Opcode, dl, VTs, Ops);
12571}
12572
12574 EVT VT1, EVT VT2, SDValue Op1,
12575 SDValue Op2, SDValue Op3) {
12576 SDVTList VTs = getVTList(VT1, VT2);
12577 SDValue Ops[] = { Op1, Op2, Op3 };
12578 return getMachineNode(Opcode, dl, VTs, Ops);
12579}
12580
12582 EVT VT1, EVT VT2,
12584 SDVTList VTs = getVTList(VT1, VT2);
12585 return getMachineNode(Opcode, dl, VTs, Ops);
12586}
12587
12589 EVT VT1, EVT VT2, EVT VT3,
12590 SDValue Op1, SDValue Op2) {
12591 SDVTList VTs = getVTList(VT1, VT2, VT3);
12592 SDValue Ops[] = { Op1, Op2 };
12593 return getMachineNode(Opcode, dl, VTs, Ops);
12594}
12595
12597 EVT VT1, EVT VT2, EVT VT3,
12598 SDValue Op1, SDValue Op2,
12599 SDValue Op3) {
12600 SDVTList VTs = getVTList(VT1, VT2, VT3);
12601 SDValue Ops[] = { Op1, Op2, Op3 };
12602 return getMachineNode(Opcode, dl, VTs, Ops);
12603}
12604
12606 EVT VT1, EVT VT2, EVT VT3,
12608 SDVTList VTs = getVTList(VT1, VT2, VT3);
12609 return getMachineNode(Opcode, dl, VTs, Ops);
12610}
12611
12613 ArrayRef<EVT> ResultTys,
12615 SDVTList VTs = getVTList(ResultTys);
12616 return getMachineNode(Opcode, dl, VTs, Ops);
12617}
12618
12620 SDVTList VTs,
12622 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
12624 void *IP = nullptr;
12625
12626 if (DoCSE) {
12628 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
12629 IP = nullptr;
12630 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
12631 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
12632 }
12633 }
12634
12635 // Allocate a new MachineSDNode.
12636 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
12637 createOperands(N, Ops);
12638
12639 if (DoCSE)
12640 CSEMap.InsertNode(N, IP);
12641
12642 InsertNode(N);
12643 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
12644 return N;
12645}
12646
12647/// getTargetExtractSubreg - A convenience function for creating
12648/// TargetOpcode::EXTRACT_SUBREG nodes.
12650 SDValue Operand) {
12651 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
12652 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
12653 VT, Operand, SRIdxVal);
12654 return SDValue(Subreg, 0);
12655}
12656
12657/// getTargetInsertSubreg - A convenience function for creating
12658/// TargetOpcode::INSERT_SUBREG nodes.
12660 SDValue Operand, SDValue Subreg) {
12661 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
12662 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
12663 VT, Operand, Subreg, SRIdxVal);
12664 return SDValue(Result, 0);
12665}
12666
12667/// getNodeIfExists - Get the specified node if it's already available, or
12668/// else return NULL.
12671 bool AllowCommute) {
12672 SDNodeFlags Flags;
12673 if (Inserter)
12674 Flags = Inserter->getFlags();
12675 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
12676}
12677
12680 const SDNodeFlags Flags,
12681 bool AllowCommute) {
12682 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
12683 return nullptr;
12684
12685 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
12687 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
12688 void *IP = nullptr;
12689 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
12690 E->intersectFlagsWith(Flags);
12691 return E;
12692 }
12693 return nullptr;
12694 };
12695
12696 if (SDNode *Existing = Lookup(Ops))
12697 return Existing;
12698
12699 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
12700 return Lookup({Ops[1], Ops[0]});
12701
12702 return nullptr;
12703}
12704
12705/// doesNodeExist - Check if a node exists without modifying its flags.
12706bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
12708 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
12710 AddNodeIDNode(ID, Opcode, VTList, Ops);
12711 void *IP = nullptr;
12712 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
12713 return true;
12714 }
12715 return false;
12716}
12717
12718/// getDbgValue - Creates a SDDbgValue node.
12719///
12720/// SDNode
12722 SDNode *N, unsigned R, bool IsIndirect,
12723 const DebugLoc &DL, unsigned O) {
12724 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12725 "Expected inlined-at fields to agree");
12726 return new (DbgInfo->getAlloc())
12727 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
12728 {}, IsIndirect, DL, O,
12729 /*IsVariadic=*/false);
12730}
12731
12732/// Constant
12734 DIExpression *Expr,
12735 const Value *C,
12736 const DebugLoc &DL, unsigned O) {
12737 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12738 "Expected inlined-at fields to agree");
12739 return new (DbgInfo->getAlloc())
12740 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
12741 /*IsIndirect=*/false, DL, O,
12742 /*IsVariadic=*/false);
12743}
12744
12745/// FrameIndex
12747 DIExpression *Expr, unsigned FI,
12748 bool IsIndirect,
12749 const DebugLoc &DL,
12750 unsigned O) {
12751 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12752 "Expected inlined-at fields to agree");
12753 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
12754}
12755
12756/// FrameIndex with dependencies
12758 DIExpression *Expr, unsigned FI,
12759 ArrayRef<SDNode *> Dependencies,
12760 bool IsIndirect,
12761 const DebugLoc &DL,
12762 unsigned O) {
12763 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12764 "Expected inlined-at fields to agree");
12765 return new (DbgInfo->getAlloc())
12766 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
12767 Dependencies, IsIndirect, DL, O,
12768 /*IsVariadic=*/false);
12769}
12770
12771/// VReg
12773 Register VReg, bool IsIndirect,
12774 const DebugLoc &DL, unsigned O) {
12775 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12776 "Expected inlined-at fields to agree");
12777 return new (DbgInfo->getAlloc())
12778 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
12779 {}, IsIndirect, DL, O,
12780 /*IsVariadic=*/false);
12781}
12782
12785 ArrayRef<SDNode *> Dependencies,
12786 bool IsIndirect, const DebugLoc &DL,
12787 unsigned O, bool IsVariadic) {
12788 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12789 "Expected inlined-at fields to agree");
12790 return new (DbgInfo->getAlloc())
12791 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
12792 DL, O, IsVariadic);
12793}
12794
12796 unsigned OffsetInBits, unsigned SizeInBits,
12797 bool InvalidateDbg) {
12798 SDNode *FromNode = From.getNode();
12799 SDNode *ToNode = To.getNode();
12800 assert(FromNode && ToNode && "Can't modify dbg values");
12801
12802 // PR35338
12803 // TODO: assert(From != To && "Redundant dbg value transfer");
12804 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
12805 if (From == To || FromNode == ToNode)
12806 return;
12807
12808 if (!FromNode->getHasDebugValue())
12809 return;
12810
12811 SDDbgOperand FromLocOp =
12812 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
12814
12816 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
12817 if (Dbg->isInvalidated())
12818 continue;
12819
12820 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
12821
12822 // Create a new location ops vector that is equal to the old vector, but
12823 // with each instance of FromLocOp replaced with ToLocOp.
12824 bool Changed = false;
12825 auto NewLocOps = Dbg->copyLocationOps();
12826 std::replace_if(
12827 NewLocOps.begin(), NewLocOps.end(),
12828 [&Changed, FromLocOp](const SDDbgOperand &Op) {
12829 bool Match = Op == FromLocOp;
12830 Changed |= Match;
12831 return Match;
12832 },
12833 ToLocOp);
12834 // Ignore this SDDbgValue if we didn't find a matching location.
12835 if (!Changed)
12836 continue;
12837
12838 DIVariable *Var = Dbg->getVariable();
12839 auto *Expr = Dbg->getExpression();
12840 // If a fragment is requested, update the expression.
12841 if (SizeInBits) {
12842 // When splitting a larger (e.g., sign-extended) value whose
12843 // lower bits are described with an SDDbgValue, do not attempt
12844 // to transfer the SDDbgValue to the upper bits.
12845 if (auto FI = Expr->getFragmentInfo())
12846 if (OffsetInBits + SizeInBits > FI->SizeInBits)
12847 continue;
12848 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
12849 SizeInBits);
12850 if (!Fragment)
12851 continue;
12852 Expr = *Fragment;
12853 }
12854
12855 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
12856 // Clone the SDDbgValue and move it to To.
12857 SDDbgValue *Clone = getDbgValueList(
12858 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12859 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12860 Dbg->isVariadic());
12861 ClonedDVs.push_back(Clone);
12862
12863 if (InvalidateDbg) {
12864 // Invalidate value and indicate the SDDbgValue should not be emitted.
12865 Dbg->setIsInvalidated();
12866 Dbg->setIsEmitted();
12867 }
12868 }
12869
12870 for (SDDbgValue *Dbg : ClonedDVs) {
12871 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12872 "Transferred DbgValues should depend on the new SDNode");
12873 AddDbgValue(Dbg, false);
12874 }
12875}
12876
12878 if (!N.getHasDebugValue())
12879 return;
12880
12881 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12882 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12883 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12884 return SDDbgOperand::fromNode(Node, ResNo);
12885 };
12886
12888 for (auto *DV : GetDbgValues(&N)) {
12889 if (DV->isInvalidated())
12890 continue;
12891 switch (N.getOpcode()) {
12892 default:
12893 break;
12894 case ISD::ADD: {
12895 SDValue N0 = N.getOperand(0);
12896 SDValue N1 = N.getOperand(1);
12897 if (!isa<ConstantSDNode>(N0)) {
12898 bool RHSConstant = isa<ConstantSDNode>(N1);
12900 if (RHSConstant)
12901 Offset = N.getConstantOperandVal(1);
12902 // We are not allowed to turn indirect debug values variadic, so
12903 // don't salvage those.
12904 if (!RHSConstant && DV->isIndirect())
12905 continue;
12906
12907 // Rewrite an ADD constant node into a DIExpression. Since we are
12908 // performing arithmetic to compute the variable's *value* in the
12909 // DIExpression, we need to mark the expression with a
12910 // DW_OP_stack_value.
12911 auto *DIExpr = DV->getExpression();
12912 auto NewLocOps = DV->copyLocationOps();
12913 bool Changed = false;
12914 size_t OrigLocOpsSize = NewLocOps.size();
12915 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12916 // We're not given a ResNo to compare against because the whole
12917 // node is going away. We know that any ISD::ADD only has one
12918 // result, so we can assume any node match is using the result.
12919 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12920 NewLocOps[i].getSDNode() != &N)
12921 continue;
12922 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12923 if (RHSConstant) {
12926 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12927 } else {
12928 // Convert to a variadic expression (if not already).
12929 // convertToVariadicExpression() returns a const pointer, so we use
12930 // a temporary const variable here.
12931 const auto *TmpDIExpr =
12935 ExprOps.push_back(NewLocOps.size());
12936 ExprOps.push_back(dwarf::DW_OP_plus);
12937 SDDbgOperand RHS =
12939 NewLocOps.push_back(RHS);
12940 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12941 }
12942 Changed = true;
12943 }
12944 (void)Changed;
12945 assert(Changed && "Salvage target doesn't use N");
12946
12947 bool IsVariadic =
12948 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12949
12950 auto AdditionalDependencies = DV->getAdditionalDependencies();
12951 SDDbgValue *Clone = getDbgValueList(
12952 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12953 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12954 ClonedDVs.push_back(Clone);
12955 DV->setIsInvalidated();
12956 DV->setIsEmitted();
12957 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12958 N0.getNode()->dumprFull(this);
12959 dbgs() << " into " << *DIExpr << '\n');
12960 }
12961 break;
12962 }
12963 case ISD::TRUNCATE: {
12964 SDValue N0 = N.getOperand(0);
12965 TypeSize FromSize = N0.getValueSizeInBits();
12966 TypeSize ToSize = N.getValueSizeInBits(0);
12967
12968 DIExpression *DbgExpression = DV->getExpression();
12969 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12970 auto NewLocOps = DV->copyLocationOps();
12971 bool Changed = false;
12972 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12973 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12974 NewLocOps[i].getSDNode() != &N)
12975 continue;
12976
12977 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12978 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12979 Changed = true;
12980 }
12981 assert(Changed && "Salvage target doesn't use N");
12982 (void)Changed;
12983
12984 SDDbgValue *Clone =
12985 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12986 DV->getAdditionalDependencies(), DV->isIndirect(),
12987 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12988
12989 ClonedDVs.push_back(Clone);
12990 DV->setIsInvalidated();
12991 DV->setIsEmitted();
12992 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12993 dbgs() << " into " << *DbgExpression << '\n');
12994 break;
12995 }
12996 }
12997 }
12998
12999 for (SDDbgValue *Dbg : ClonedDVs) {
13000 assert((!Dbg->getSDNodes().empty() ||
13001 llvm::any_of(Dbg->getLocationOps(),
13002 [&](const SDDbgOperand &Op) {
13003 return Op.getKind() == SDDbgOperand::FRAMEIX;
13004 })) &&
13005 "Salvaged DbgValue should depend on a new SDNode");
13006 AddDbgValue(Dbg, false);
13007 }
13008}
13009
13010/// Creates a SDDbgLabel node.
13012 const DebugLoc &DL, unsigned O) {
13013 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
13014 "Expected inlined-at fields to agree");
13015 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
13016}
13017
13018namespace {
13019
13020/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
13021/// pointed to by a use iterator is deleted, increment the use iterator
13022/// so that it doesn't dangle.
13023///
13024class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
13027
13028 void NodeDeleted(SDNode *N, SDNode *E) override {
13029 // Increment the iterator as needed.
13030 while (UI != UE && N == UI->getUser())
13031 ++UI;
13032 }
13033
13034public:
13035 RAUWUpdateListener(SelectionDAG &d,
13038 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
13039};
13040
13041} // end anonymous namespace
13042
13043/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
13044/// This can cause recursive merging of nodes in the DAG.
13045///
13046/// This version assumes From has a single result value.
13047///
13049 SDNode *From = FromN.getNode();
13050 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
13051 "Cannot replace with this method!");
13052 assert(From != To.getNode() && "Cannot replace uses of with self");
13053
13054 // Preserve Debug Values
13055 transferDbgValues(FromN, To);
13056 // Preserve extra info.
13057 copyExtraInfo(From, To.getNode());
13058
13059 // Iterate over all the existing uses of From. New uses will be added
13060 // to the beginning of the use list, which we avoid visiting.
13061 // This specifically avoids visiting uses of From that arise while the
13062 // replacement is happening, because any such uses would be the result
13063 // of CSE: If an existing node looks like From after one of its operands
13064 // is replaced by To, we don't want to replace of all its users with To
13065 // too. See PR3018 for more info.
13066 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
13067 RAUWUpdateListener Listener(*this, UI, UE);
13068 while (UI != UE) {
13069 SDNode *User = UI->getUser();
13070
13071 // This node is about to morph, remove its old self from the CSE maps.
13072 RemoveNodeFromCSEMaps(User);
13073
13074 // A user can appear in a use list multiple times, and when this
13075 // happens the uses are usually next to each other in the list.
13076 // To help reduce the number of CSE recomputations, process all
13077 // the uses of this user that we can find this way.
13078 do {
13079 SDUse &Use = *UI;
13080 ++UI;
13081 Use.set(To);
13082 if (To->isDivergent() != From->isDivergent())
13084 } while (UI != UE && UI->getUser() == User);
13085 // Now that we have modified User, add it back to the CSE maps. If it
13086 // already exists there, recursively merge the results together.
13087 AddModifiedNodeToCSEMaps(User);
13088 }
13089
13090 // If we just RAUW'd the root, take note.
13091 if (FromN == getRoot())
13092 setRoot(To);
13093}
13094
13095/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
13096/// This can cause recursive merging of nodes in the DAG.
13097///
13098/// This version assumes that for each value of From, there is a
13099/// corresponding value in To in the same position with the same type.
13100///
13102#ifndef NDEBUG
13103 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
13104 assert((!From->hasAnyUseOfValue(i) ||
13105 From->getValueType(i) == To->getValueType(i)) &&
13106 "Cannot use this version of ReplaceAllUsesWith!");
13107#endif
13108
13109 // Handle the trivial case.
13110 if (From == To)
13111 return;
13112
13113 // Preserve Debug Info. Only do this if there's a use.
13114 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
13115 if (From->hasAnyUseOfValue(i)) {
13116 assert((i < To->getNumValues()) && "Invalid To location");
13117 transferDbgValues(SDValue(From, i), SDValue(To, i));
13118 }
13119 // Preserve extra info.
13120 copyExtraInfo(From, To);
13121
13122 // Iterate over just the existing users of From. See the comments in
13123 // the ReplaceAllUsesWith above.
13124 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
13125 RAUWUpdateListener Listener(*this, UI, UE);
13126 while (UI != UE) {
13127 SDNode *User = UI->getUser();
13128
13129 // This node is about to morph, remove its old self from the CSE maps.
13130 RemoveNodeFromCSEMaps(User);
13131
13132 // A user can appear in a use list multiple times, and when this
13133 // happens the uses are usually next to each other in the list.
13134 // To help reduce the number of CSE recomputations, process all
13135 // the uses of this user that we can find this way.
13136 do {
13137 SDUse &Use = *UI;
13138 ++UI;
13139 Use.setNode(To);
13140 if (To->isDivergent() != From->isDivergent())
13142 } while (UI != UE && UI->getUser() == User);
13143
13144 // Now that we have modified User, add it back to the CSE maps. If it
13145 // already exists there, recursively merge the results together.
13146 AddModifiedNodeToCSEMaps(User);
13147 }
13148
13149 // If we just RAUW'd the root, take note.
13150 if (From == getRoot().getNode())
13151 setRoot(SDValue(To, getRoot().getResNo()));
13152}
13153
13154/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
13155/// This can cause recursive merging of nodes in the DAG.
13156///
13157/// This version can replace From with any result values. To must match the
13158/// number and types of values returned by From.
13160 if (From->getNumValues() == 1) // Handle the simple case efficiently.
13161 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
13162
13163 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
13164 // Preserve Debug Info.
13165 transferDbgValues(SDValue(From, i), To[i]);
13166 // Preserve extra info.
13167 copyExtraInfo(From, To[i].getNode());
13168 }
13169
13170 // Iterate over just the existing users of From. See the comments in
13171 // the ReplaceAllUsesWith above.
13172 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
13173 RAUWUpdateListener Listener(*this, UI, UE);
13174 while (UI != UE) {
13175 SDNode *User = UI->getUser();
13176
13177 // This node is about to morph, remove its old self from the CSE maps.
13178 RemoveNodeFromCSEMaps(User);
13179
13180 // A user can appear in a use list multiple times, and when this happens the
13181 // uses are usually next to each other in the list. To help reduce the
13182 // number of CSE and divergence recomputations, process all the uses of this
13183 // user that we can find this way.
13184 bool To_IsDivergent = false;
13185 do {
13186 SDUse &Use = *UI;
13187 const SDValue &ToOp = To[Use.getResNo()];
13188 ++UI;
13189 Use.set(ToOp);
13190 if (ToOp.getValueType() != MVT::Other)
13191 To_IsDivergent |= ToOp->isDivergent();
13192 } while (UI != UE && UI->getUser() == User);
13193
13194 if (To_IsDivergent != From->isDivergent())
13196
13197 // Now that we have modified User, add it back to the CSE maps. If it
13198 // already exists there, recursively merge the results together.
13199 AddModifiedNodeToCSEMaps(User);
13200 }
13201
13202 // If we just RAUW'd the root, take note.
13203 if (From == getRoot().getNode())
13204 setRoot(SDValue(To[getRoot().getResNo()]));
13205}
13206
13207/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
13208/// uses of other values produced by From.getNode() alone. The Deleted
13209/// vector is handled the same way as for ReplaceAllUsesWith.
13211 // Handle the really simple, really trivial case efficiently.
13212 if (From == To) return;
13213
13214 // Handle the simple, trivial, case efficiently.
13215 if (From.getNode()->getNumValues() == 1) {
13216 ReplaceAllUsesWith(From, To);
13217 return;
13218 }
13219
13220 // Preserve Debug Info.
13221 transferDbgValues(From, To);
13222 copyExtraInfo(From.getNode(), To.getNode());
13223
13224 // Iterate over just the existing users of From. See the comments in
13225 // the ReplaceAllUsesWith above.
13226 SDNode::use_iterator UI = From.getNode()->use_begin(),
13227 UE = From.getNode()->use_end();
13228 RAUWUpdateListener Listener(*this, UI, UE);
13229 while (UI != UE) {
13230 SDNode *User = UI->getUser();
13231 bool UserRemovedFromCSEMaps = false;
13232
13233 // A user can appear in a use list multiple times, and when this
13234 // happens the uses are usually next to each other in the list.
13235 // To help reduce the number of CSE recomputations, process all
13236 // the uses of this user that we can find this way.
13237 do {
13238 SDUse &Use = *UI;
13239
13240 // Skip uses of different values from the same node.
13241 if (Use.getResNo() != From.getResNo()) {
13242 ++UI;
13243 continue;
13244 }
13245
13246 // If this node hasn't been modified yet, it's still in the CSE maps,
13247 // so remove its old self from the CSE maps.
13248 if (!UserRemovedFromCSEMaps) {
13249 RemoveNodeFromCSEMaps(User);
13250 UserRemovedFromCSEMaps = true;
13251 }
13252
13253 ++UI;
13254 Use.set(To);
13255 if (To->isDivergent() != From->isDivergent())
13257 } while (UI != UE && UI->getUser() == User);
13258 // We are iterating over all uses of the From node, so if a use
13259 // doesn't use the specific value, no changes are made.
13260 if (!UserRemovedFromCSEMaps)
13261 continue;
13262
13263 // Now that we have modified User, add it back to the CSE maps. If it
13264 // already exists there, recursively merge the results together.
13265 AddModifiedNodeToCSEMaps(User);
13266 }
13267
13268 // If we just RAUW'd the root, take note.
13269 if (From == getRoot())
13270 setRoot(To);
13271}
13272
13273namespace {
13274
13275/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
13276/// to record information about a use.
13277struct UseMemo {
13278 SDNode *User;
13279 unsigned Index;
13280 SDUse *Use;
13281};
13282
13283/// operator< - Sort Memos by User.
13284bool operator<(const UseMemo &L, const UseMemo &R) {
13285 return (intptr_t)L.User < (intptr_t)R.User;
13286}
13287
13288/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
13289/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
13290/// the node already has been taken care of recursively.
13291class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
13292 SmallVectorImpl<UseMemo> &Uses;
13293
13294 void NodeDeleted(SDNode *N, SDNode *E) override {
13295 for (UseMemo &Memo : Uses)
13296 if (Memo.User == N)
13297 Memo.User = nullptr;
13298 }
13299
13300public:
13301 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
13302 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
13303};
13304
13305} // end anonymous namespace
13306
13307/// Return true if a glue output should propagate divergence information.
13309 switch (Node->getOpcode()) {
13310 case ISD::CopyFromReg:
13311 case ISD::CopyToReg:
13312 return false;
13313 default:
13314 return true;
13315 }
13316
13317 llvm_unreachable("covered opcode switch");
13318}
13319
13321 if (TLI->isSDNodeAlwaysUniform(N)) {
13322 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
13323 "Conflicting divergence information!");
13324 return false;
13325 }
13326 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
13327 return true;
13328 for (const auto &Op : N->ops()) {
13329 EVT VT = Op.getValueType();
13330
13331 // Skip Chain. It does not carry divergence.
13332 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
13333 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
13334 return true;
13335 }
13336 return false;
13337}
13338
13340 SmallVector<SDNode *, 16> Worklist(1, N);
13341 do {
13342 N = Worklist.pop_back_val();
13343 bool IsDivergent = calculateDivergence(N);
13344 if (N->SDNodeBits.IsDivergent != IsDivergent) {
13345 N->SDNodeBits.IsDivergent = IsDivergent;
13346 llvm::append_range(Worklist, N->users());
13347 }
13348 } while (!Worklist.empty());
13349}
13350
13351void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
13353 Order.reserve(AllNodes.size());
13354 for (auto &N : allnodes()) {
13355 unsigned NOps = N.getNumOperands();
13356 Degree[&N] = NOps;
13357 if (0 == NOps)
13358 Order.push_back(&N);
13359 }
13360 for (size_t I = 0; I != Order.size(); ++I) {
13361 SDNode *N = Order[I];
13362 for (auto *U : N->users()) {
13363 unsigned &UnsortedOps = Degree[U];
13364 if (0 == --UnsortedOps)
13365 Order.push_back(U);
13366 }
13367 }
13368}
13369
13370#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
13371void SelectionDAG::VerifyDAGDivergence() {
13372 std::vector<SDNode *> TopoOrder;
13373 CreateTopologicalOrder(TopoOrder);
13374 for (auto *N : TopoOrder) {
13375 assert(calculateDivergence(N) == N->isDivergent() &&
13376 "Divergence bit inconsistency detected");
13377 }
13378}
13379#endif
13380
13381/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
13382/// uses of other values produced by From.getNode() alone. The same value
13383/// may appear in both the From and To list. The Deleted vector is
13384/// handled the same way as for ReplaceAllUsesWith.
13386 const SDValue *To,
13387 unsigned Num){
13388 // Handle the simple, trivial case efficiently.
13389 if (Num == 1)
13390 return ReplaceAllUsesOfValueWith(*From, *To);
13391
13392 transferDbgValues(*From, *To);
13393 copyExtraInfo(From->getNode(), To->getNode());
13394
13395 // Read up all the uses and make records of them. This helps
13396 // processing new uses that are introduced during the
13397 // replacement process.
13399 for (unsigned i = 0; i != Num; ++i) {
13400 unsigned FromResNo = From[i].getResNo();
13401 SDNode *FromNode = From[i].getNode();
13402 for (SDUse &Use : FromNode->uses()) {
13403 if (Use.getResNo() == FromResNo) {
13404 UseMemo Memo = {Use.getUser(), i, &Use};
13405 Uses.push_back(Memo);
13406 }
13407 }
13408 }
13409
13410 // Sort the uses, so that all the uses from a given User are together.
13412 RAUOVWUpdateListener Listener(*this, Uses);
13413
13414 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
13415 UseIndex != UseIndexEnd; ) {
13416 // We know that this user uses some value of From. If it is the right
13417 // value, update it.
13418 SDNode *User = Uses[UseIndex].User;
13419 // If the node has been deleted by recursive CSE updates when updating
13420 // another node, then just skip this entry.
13421 if (User == nullptr) {
13422 ++UseIndex;
13423 continue;
13424 }
13425
13426 // This node is about to morph, remove its old self from the CSE maps.
13427 RemoveNodeFromCSEMaps(User);
13428
13429 // The Uses array is sorted, so all the uses for a given User
13430 // are next to each other in the list.
13431 // To help reduce the number of CSE recomputations, process all
13432 // the uses of this user that we can find this way.
13433 do {
13434 unsigned i = Uses[UseIndex].Index;
13435 SDUse &Use = *Uses[UseIndex].Use;
13436 ++UseIndex;
13437
13438 Use.set(To[i]);
13439 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
13440
13441 // Now that we have modified User, add it back to the CSE maps. If it
13442 // already exists there, recursively merge the results together.
13443 AddModifiedNodeToCSEMaps(User);
13444 }
13445}
13446
13447/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
13448/// based on their topological order. It returns the maximum id and a vector
13449/// of the SDNodes* in assigned order by reference.
13451 unsigned DAGSize = 0;
13452
13453 // SortedPos tracks the progress of the algorithm. Nodes before it are
13454 // sorted, nodes after it are unsorted. When the algorithm completes
13455 // it is at the end of the list.
13456 allnodes_iterator SortedPos = allnodes_begin();
13457
13458 // Visit all the nodes. Move nodes with no operands to the front of
13459 // the list immediately. Annotate nodes that do have operands with their
13460 // operand count. Before we do this, the Node Id fields of the nodes
13461 // may contain arbitrary values. After, the Node Id fields for nodes
13462 // before SortedPos will contain the topological sort index, and the
13463 // Node Id fields for nodes At SortedPos and after will contain the
13464 // count of outstanding operands.
13466 checkForCycles(&N, this);
13467 unsigned Degree = N.getNumOperands();
13468 if (Degree == 0) {
13469 // A node with no uses, add it to the result array immediately.
13470 N.setNodeId(DAGSize++);
13471 allnodes_iterator Q(&N);
13472 if (Q != SortedPos)
13473 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
13474 assert(SortedPos != AllNodes.end() && "Overran node list");
13475 ++SortedPos;
13476 } else {
13477 // Temporarily use the Node Id as scratch space for the degree count.
13478 N.setNodeId(Degree);
13479 }
13480 }
13481
13482 // Visit all the nodes. As we iterate, move nodes into sorted order,
13483 // such that by the time the end is reached all nodes will be sorted.
13484 for (SDNode &Node : allnodes()) {
13485 SDNode *N = &Node;
13486 checkForCycles(N, this);
13487 // N is in sorted position, so all its uses have one less operand
13488 // that needs to be sorted.
13489 for (SDNode *P : N->users()) {
13490 unsigned Degree = P->getNodeId();
13491 assert(Degree != 0 && "Invalid node degree");
13492 --Degree;
13493 if (Degree == 0) {
13494 // All of P's operands are sorted, so P may sorted now.
13495 P->setNodeId(DAGSize++);
13496 if (P->getIterator() != SortedPos)
13497 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
13498 assert(SortedPos != AllNodes.end() && "Overran node list");
13499 ++SortedPos;
13500 } else {
13501 // Update P's outstanding operand count.
13502 P->setNodeId(Degree);
13503 }
13504 }
13505 if (Node.getIterator() == SortedPos) {
13506#ifndef NDEBUG
13508 SDNode *S = &*++I;
13509 dbgs() << "Overran sorted position:\n";
13510 S->dumprFull(this); dbgs() << "\n";
13511 dbgs() << "Checking if this is due to cycles\n";
13512 checkForCycles(this, true);
13513#endif
13514 llvm_unreachable(nullptr);
13515 }
13516 }
13517
13518 assert(SortedPos == AllNodes.end() &&
13519 "Topological sort incomplete!");
13520 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
13521 "First node in topological sort is not the entry token!");
13522 assert(AllNodes.front().getNodeId() == 0 &&
13523 "First node in topological sort has non-zero id!");
13524 assert(AllNodes.front().getNumOperands() == 0 &&
13525 "First node in topological sort has operands!");
13526 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
13527 "Last node in topologic sort has unexpected id!");
13528 assert(AllNodes.back().use_empty() &&
13529 "Last node in topologic sort has users!");
13530 assert(DAGSize == allnodes_size() && "Node count mismatch!");
13531 return DAGSize;
13532}
13533
13535 SmallVectorImpl<const SDNode *> &SortedNodes) const {
13536 SortedNodes.clear();
13537 // Node -> remaining number of outstanding operands.
13538 DenseMap<const SDNode *, unsigned> RemainingOperands;
13539
13540 // Put nodes without any operands into SortedNodes first.
13541 for (const SDNode &N : allnodes()) {
13542 checkForCycles(&N, this);
13543 unsigned NumOperands = N.getNumOperands();
13544 if (NumOperands == 0)
13545 SortedNodes.push_back(&N);
13546 else
13547 // Record their total number of outstanding operands.
13548 RemainingOperands[&N] = NumOperands;
13549 }
13550
13551 // A node is pushed into SortedNodes when all of its operands (predecessors in
13552 // the graph) are also in SortedNodes.
13553 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
13554 const SDNode *N = SortedNodes[i];
13555 for (const SDNode *U : N->users()) {
13556 // HandleSDNode is never part of a DAG and therefore has no entry in
13557 // RemainingOperands.
13558 if (U->getOpcode() == ISD::HANDLENODE)
13559 continue;
13560 unsigned &NumRemOperands = RemainingOperands[U];
13561 assert(NumRemOperands && "Invalid number of remaining operands");
13562 --NumRemOperands;
13563 if (!NumRemOperands)
13564 SortedNodes.push_back(U);
13565 }
13566 }
13567
13568 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
13569 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
13570 "First node in topological sort is not the entry token");
13571 assert(SortedNodes.front()->getNumOperands() == 0 &&
13572 "First node in topological sort has operands");
13573}
13574
13575/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
13576/// value is produced by SD.
13577void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
13578 for (SDNode *SD : DB->getSDNodes()) {
13579 if (!SD)
13580 continue;
13581 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
13582 SD->setHasDebugValue(true);
13583 }
13584 DbgInfo->add(DB, isParameter);
13585}
13586
13587void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
13588
13590 SDValue NewMemOpChain) {
13591 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
13592 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
13593 // The new memory operation must have the same position as the old load in
13594 // terms of memory dependency. Create a TokenFactor for the old load and new
13595 // memory operation and update uses of the old load's output chain to use that
13596 // TokenFactor.
13597 if (OldChain == NewMemOpChain || OldChain.use_empty())
13598 return NewMemOpChain;
13599
13600 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
13601 OldChain, NewMemOpChain);
13602 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
13603 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
13604 return TokenFactor;
13605}
13606
13608 SDValue NewMemOp) {
13609 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
13610 SDValue OldChain = SDValue(OldLoad, 1);
13611 SDValue NewMemOpChain = NewMemOp.getValue(1);
13612 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
13613}
13614
13616 Function **OutFunction) {
13617 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
13618
13619 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
13620 auto *Module = MF->getFunction().getParent();
13621 auto *Function = Module->getFunction(Symbol);
13622
13623 if (OutFunction != nullptr)
13624 *OutFunction = Function;
13625
13626 if (Function != nullptr) {
13627 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
13628 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
13629 }
13630
13631 std::string ErrorStr;
13632 raw_string_ostream ErrorFormatter(ErrorStr);
13633 ErrorFormatter << "Undefined external symbol ";
13634 ErrorFormatter << '"' << Symbol << '"';
13635 report_fatal_error(Twine(ErrorStr));
13636}
13637
13638//===----------------------------------------------------------------------===//
13639// SDNode Class
13640//===----------------------------------------------------------------------===//
13641
13644 return Const != nullptr && Const->isZero();
13645}
13646
13648 return V.isUndef() || isNullConstant(V);
13649}
13650
13653 return Const != nullptr && Const->isZero() && !Const->isNegative();
13654}
13655
13658 return Const != nullptr && Const->isAllOnes();
13659}
13660
13663 return Const != nullptr && Const->isOne();
13664}
13665
13668 return Const != nullptr && Const->isMinSignedValue();
13669}
13670
13672 SDValue V, unsigned OperandNo,
13673 unsigned Depth) const {
13674 APInt DemandedElts = getDemandAllEltsMask(V);
13675 return isIdentityElement(Opcode, Flags, V, DemandedElts, OperandNo, Depth);
13676}
13677
13679 SDValue V, const APInt &DemandedElts,
13680 unsigned OperandNo, unsigned Depth) const {
13681 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
13682 // TODO: Target-specific opcodes could be added.
13683 if (V.getValueType().isInteger()) {
13684 KnownBits Known = computeKnownBits(V, DemandedElts, Depth);
13685 if (Known.isConstant()) {
13686 const APInt &Const = Known.getConstant();
13687 switch (Opcode) {
13688 case ISD::ADD:
13689 case ISD::OR:
13690 case ISD::XOR:
13691 case ISD::UMAX:
13692 return Const.isZero();
13693 case ISD::MUL:
13694 return Const.isOne();
13695 case ISD::AND:
13696 case ISD::UMIN:
13697 return Const.isAllOnes();
13698 case ISD::SMAX:
13699 return Const.isMinSignedValue();
13700 case ISD::SMIN:
13701 return Const.isMaxSignedValue();
13702 case ISD::SUB:
13703 case ISD::SHL:
13704 case ISD::SRA:
13705 case ISD::SRL:
13706 return OperandNo == 1 && Const.isZero();
13707 case ISD::UDIV:
13708 case ISD::SDIV:
13709 return OperandNo == 1 && Const.isOne();
13710 }
13711 }
13712 } else if (auto *ConstFP = isConstOrConstSplatFP(V, DemandedElts)) {
13713 switch (Opcode) {
13714 case ISD::FADD:
13715 return ConstFP->isZero() &&
13716 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
13717 case ISD::FSUB:
13718 return OperandNo == 1 && ConstFP->isZero() &&
13719 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
13720 case ISD::FMUL:
13721 return ConstFP->isOne();
13722 case ISD::FDIV:
13723 return OperandNo == 1 && ConstFP->isOne();
13724 case ISD::FMINNUM:
13725 case ISD::FMAXNUM: {
13726 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13727 EVT VT = V.getValueType();
13728 const fltSemantics &Semantics = VT.getFltSemantics();
13729 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics)
13730 : !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
13731 : APFloat::getLargest(Semantics);
13732 if (Opcode == ISD::FMAXNUM)
13733 NeutralAF.changeSign();
13734
13735 return ConstFP->isExactlyValue(NeutralAF);
13736 }
13737 }
13738 }
13739 return false;
13740}
13741
13743 while (V.getOpcode() == ISD::BITCAST)
13744 V = V.getOperand(0);
13745 return V;
13746}
13747
13749 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
13750 V = V.getOperand(0);
13751 return V;
13752}
13753
13755 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
13756 V = V.getOperand(0);
13757 return V;
13758}
13759
13761 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13762 SDValue InVec = V.getOperand(0);
13763 SDValue EltNo = V.getOperand(2);
13764 EVT VT = InVec.getValueType();
13765 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
13766 if (IndexC && VT.isFixedLengthVector() &&
13767 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
13768 !DemandedElts[IndexC->getZExtValue()]) {
13769 V = InVec;
13770 continue;
13771 }
13772 break;
13773 }
13774 return V;
13775}
13776
13778 while (V.getOpcode() == ISD::TRUNCATE)
13779 V = V.getOperand(0);
13780 return V;
13781}
13782
13783bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
13784 if (V.getOpcode() != ISD::XOR)
13785 return false;
13786 V = peekThroughBitcasts(V.getOperand(1));
13787 unsigned NumBits = V.getScalarValueSizeInBits();
13788 ConstantSDNode *C =
13789 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
13790 return C && (C->getAPIntValue().countr_one() >= NumBits);
13791}
13792
13794 bool AllowTruncation) {
13795 APInt DemandedElts = getDemandAllEltsMask(N);
13796 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
13797}
13798
13800 bool AllowUndefs,
13801 bool AllowTruncation) {
13803 return CN;
13804
13805 // SplatVectors can truncate their operands. Ignore that case here unless
13806 // AllowTruncation is set.
13807 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
13808 EVT VecEltVT = N->getValueType(0).getVectorElementType();
13809 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
13810 EVT CVT = CN->getValueType(0);
13811 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
13812 if (AllowTruncation || CVT == VecEltVT)
13813 return CN;
13814 }
13815 }
13816
13818 BitVector UndefElements;
13819 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
13820
13821 // BuildVectors can truncate their operands. Ignore that case here unless
13822 // AllowTruncation is set.
13823 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13824 if (CN && (UndefElements.none() || AllowUndefs)) {
13825 EVT CVT = CN->getValueType(0);
13826 EVT NSVT = N.getValueType().getScalarType();
13827 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
13828 if (AllowTruncation || (CVT == NSVT))
13829 return CN;
13830 }
13831 }
13832
13833 return nullptr;
13834}
13835
13837 APInt DemandedElts = getDemandAllEltsMask(N);
13838 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
13839}
13840
13842 const APInt &DemandedElts,
13843 bool AllowUndefs) {
13845 return CN;
13846
13848 BitVector UndefElements;
13849 ConstantFPSDNode *CN =
13850 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
13851 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13852 if (CN && (UndefElements.none() || AllowUndefs))
13853 return CN;
13854 }
13855
13856 if (N.getOpcode() == ISD::SPLAT_VECTOR)
13857 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
13858 return CN;
13859
13860 return nullptr;
13861}
13862
13863bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
13864 // TODO: may want to use peekThroughBitcast() here.
13865 ConstantSDNode *C =
13866 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13867 return C && C->isZero();
13868}
13869
13870bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
13871 ConstantSDNode *C =
13872 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
13873 return C && C->isOne();
13874}
13875
13876bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
13877 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13878 return C && C->isOne();
13879}
13880
13881bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
13883 unsigned BitWidth = N.getScalarValueSizeInBits();
13884 ConstantSDNode *C =
13885 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13886 return C && C->getAPIntValue().countTrailingOnes() >= BitWidth;
13887}
13888
13889bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
13890 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13891 return C && APInt::isSameValue(C->getAPIntValue(),
13892 APInt(C->getAPIntValue().getBitWidth(), 1));
13893}
13894
13895bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
13897 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
13898 return C && C->isZero();
13899}
13900
13901bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
13902 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13903 return C && C->isZero();
13904}
13905
13909
13911 unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt,
13913 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MemRefs(memrefs) {
13914 bool IsVolatile = false;
13915 bool IsNonTemporal = false;
13916 bool IsDereferenceable = true;
13917 bool IsInvariant = true;
13918 for (const MachineMemOperand *MMO : memoperands()) {
13919 IsVolatile |= MMO->isVolatile();
13920 IsNonTemporal |= MMO->isNonTemporal();
13921 IsDereferenceable &= MMO->isDereferenceable();
13922 IsInvariant &= MMO->isInvariant();
13923 }
13924 MemSDNodeBits.IsVolatile = IsVolatile;
13925 MemSDNodeBits.IsNonTemporal = IsNonTemporal;
13926 MemSDNodeBits.IsDereferenceable = IsDereferenceable;
13927 MemSDNodeBits.IsInvariant = IsInvariant;
13928
13929 // For the single-MMO case, we check here that the size of the memory operand
13930 // fits within the size of the MMO. This is because the MMO might indicate
13931 // only a possible address range instead of specifying the affected memory
13932 // addresses precisely.
13935 getMemOperand()->getSize().getValue())) &&
13936 "Size mismatch!");
13937}
13938
13939/// Profile - Gather unique data for the node.
13940///
13942 AddNodeIDNode(ID, this);
13943}
13944
13945namespace {
13946
13947 struct EVTArray {
13948 std::vector<EVT> VTs;
13949
13950 EVTArray() {
13951 VTs.reserve(MVT::VALUETYPE_SIZE);
13952 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13953 VTs.push_back(MVT((MVT::SimpleValueType)i));
13954 }
13955 };
13956
13957} // end anonymous namespace
13958
13959/// getValueTypeList - Return a pointer to the specified value type.
13960///
13961const EVT *SDNode::getValueTypeList(MVT VT) {
13962 static EVTArray SimpleVTArray;
13963
13964 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13965 return &SimpleVTArray.VTs[VT.SimpleTy];
13966}
13967
13968/// hasAnyUseOfValue - Return true if there are any use of the indicated
13969/// value. This method ignores uses of other values defined by this operation.
13970bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13971 assert(Value < getNumValues() && "Bad value!");
13972
13973 for (SDUse &U : uses())
13974 if (U.getResNo() == Value)
13975 return true;
13976
13977 return false;
13978}
13979
13980/// isOnlyUserOf - Return true if this node is the only use of N.
13981bool SDNode::isOnlyUserOf(const SDNode *N) const {
13982 bool Seen = false;
13983 for (const SDNode *User : N->users()) {
13984 if (User == this)
13985 Seen = true;
13986 else
13987 return false;
13988 }
13989
13990 return Seen;
13991}
13992
13993/// Return true if the only users of N are contained in Nodes.
13995 bool Seen = false;
13996 for (const SDNode *User : N->users()) {
13997 if (llvm::is_contained(Nodes, User))
13998 Seen = true;
13999 else
14000 return false;
14001 }
14002
14003 return Seen;
14004}
14005
14006/// Return true if the referenced return value is an operand of N.
14007bool SDValue::isOperandOf(const SDNode *N) const {
14008 return is_contained(N->op_values(), *this);
14009}
14010
14011bool SDNode::isOperandOf(const SDNode *N) const {
14012 return any_of(N->op_values(),
14013 [this](SDValue Op) { return this == Op.getNode(); });
14014}
14015
14016/// reachesChainWithoutSideEffects - Return true if this operand (which must
14017/// be a chain) reaches the specified operand without crossing any
14018/// side-effecting instructions on any chain path. In practice, this looks
14019/// through token factors and non-volatile loads. In order to remain efficient,
14020/// this only looks a couple of nodes in, it does not do an exhaustive search.
14021///
14022/// Note that we only need to examine chains when we're searching for
14023/// side-effects; SelectionDAG requires that all side-effects are represented
14024/// by chains, even if another operand would force a specific ordering. This
14025/// constraint is necessary to allow transformations like splitting loads.
14027 unsigned Depth) const {
14028 if (*this == Dest) return true;
14029
14030 // Don't search too deeply, we just want to be able to see through
14031 // TokenFactor's etc.
14032 if (Depth == 0) return false;
14033
14034 // If this is a token factor, all inputs to the TF happen in parallel.
14035 if (getOpcode() == ISD::TokenFactor) {
14036 // First, try a shallow search.
14037 if (is_contained((*this)->ops(), Dest)) {
14038 // We found the chain we want as an operand of this TokenFactor.
14039 // Essentially, we reach the chain without side-effects if we could
14040 // serialize the TokenFactor into a simple chain of operations with
14041 // Dest as the last operation. This is automatically true if the
14042 // chain has one use: there are no other ordering constraints.
14043 // If the chain has more than one use, we give up: some other
14044 // use of Dest might force a side-effect between Dest and the current
14045 // node.
14046 if (Dest.hasOneUse())
14047 return true;
14048 }
14049 // Next, try a deep search: check whether every operand of the TokenFactor
14050 // reaches Dest.
14051 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
14052 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
14053 });
14054 }
14055
14056 // Loads don't have side effects, look through them.
14057 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
14058 if (Ld->isUnordered())
14059 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
14060 }
14061 return false;
14062}
14063
14064bool SDNode::hasPredecessor(const SDNode *N) const {
14067 Worklist.push_back(this);
14068 return hasPredecessorHelper(N, Visited, Worklist);
14069}
14070
14072 this->Flags &= Flags;
14073}
14074
14075SDValue
14077 ArrayRef<ISD::NodeType> CandidateBinOps,
14078 bool AllowPartials) {
14079 // The pattern must end in an extract from index 0.
14080 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
14081 !isNullConstant(Extract->getOperand(1)))
14082 return SDValue();
14083
14084 // Match against one of the candidate binary ops.
14085 SDValue Op = Extract->getOperand(0);
14086 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
14087 return Op.getOpcode() == unsigned(BinOp);
14088 }))
14089 return SDValue();
14090
14091 // Floating-point reductions may require relaxed constraints on the final step
14092 // of the reduction because they may reorder intermediate operations.
14093 unsigned CandidateBinOp = Op.getOpcode();
14094 if (Op.getValueType().isFloatingPoint()) {
14095 SDNodeFlags Flags = Op->getFlags();
14096 switch (CandidateBinOp) {
14097 case ISD::FADD:
14098 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
14099 return SDValue();
14100 break;
14101 default:
14102 llvm_unreachable("Unhandled FP opcode for binop reduction");
14103 }
14104 }
14105
14106 // Matching failed - attempt to see if we did enough stages that a partial
14107 // reduction from a subvector is possible.
14108 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
14109 if (!AllowPartials || !Op)
14110 return SDValue();
14111 EVT OpVT = Op.getValueType();
14112 EVT OpSVT = OpVT.getScalarType();
14113 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
14114 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
14115 return SDValue();
14116 BinOp = (ISD::NodeType)CandidateBinOp;
14117 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
14118 };
14119
14120 // At each stage, we're looking for something that looks like:
14121 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
14122 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
14123 // i32 undef, i32 undef, i32 undef, i32 undef>
14124 // %a = binop <8 x i32> %op, %s
14125 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
14126 // we expect something like:
14127 // <4,5,6,7,u,u,u,u>
14128 // <2,3,u,u,u,u,u,u>
14129 // <1,u,u,u,u,u,u,u>
14130 // While a partial reduction match would be:
14131 // <2,3,u,u,u,u,u,u>
14132 // <1,u,u,u,u,u,u,u>
14133 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
14134 SDValue PrevOp;
14135 for (unsigned i = 0; i < Stages; ++i) {
14136 unsigned MaskEnd = (1 << i);
14137
14138 if (Op.getOpcode() != CandidateBinOp)
14139 return PartialReduction(PrevOp, MaskEnd);
14140
14141 SDValue Op0 = Op.getOperand(0);
14142 SDValue Op1 = Op.getOperand(1);
14143
14145 if (Shuffle) {
14146 Op = Op1;
14147 } else {
14148 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
14149 Op = Op0;
14150 }
14151
14152 // The first operand of the shuffle should be the same as the other operand
14153 // of the binop.
14154 if (!Shuffle || Shuffle->getOperand(0) != Op)
14155 return PartialReduction(PrevOp, MaskEnd);
14156
14157 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
14158 for (int Index = 0; Index < (int)MaskEnd; ++Index)
14159 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
14160 return PartialReduction(PrevOp, MaskEnd);
14161
14162 PrevOp = Op;
14163 }
14164
14165 // Handle subvector reductions, which tend to appear after the shuffle
14166 // reduction stages.
14167 while (Op.getOpcode() == CandidateBinOp) {
14168 unsigned NumElts = Op.getValueType().getVectorNumElements();
14169 SDValue Op0 = Op.getOperand(0);
14170 SDValue Op1 = Op.getOperand(1);
14171 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
14173 Op0.getOperand(0) != Op1.getOperand(0))
14174 break;
14175 SDValue Src = Op0.getOperand(0);
14176 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
14177 if (NumSrcElts != (2 * NumElts))
14178 break;
14179 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
14180 Op1.getConstantOperandAPInt(1) == NumElts) &&
14181 !(Op1.getConstantOperandAPInt(1) == 0 &&
14182 Op0.getConstantOperandAPInt(1) == NumElts))
14183 break;
14184 Op = Src;
14185 }
14186
14187 BinOp = (ISD::NodeType)CandidateBinOp;
14188 return Op;
14189}
14190
14192 EVT VT = N->getValueType(0);
14193 EVT EltVT = VT.getVectorElementType();
14194 unsigned NE = VT.getVectorNumElements();
14195
14196 SDLoc dl(N);
14197
14198 // If ResNE is 0, fully unroll the vector op.
14199 if (ResNE == 0)
14200 ResNE = NE;
14201 else if (NE > ResNE)
14202 NE = ResNE;
14203
14204 if (N->getNumValues() == 2) {
14205 SmallVector<SDValue, 8> Scalars0, Scalars1;
14206 SmallVector<SDValue, 4> Operands(N->getNumOperands());
14207 EVT VT1 = N->getValueType(1);
14208 EVT EltVT1 = VT1.getVectorElementType();
14209
14210 unsigned i;
14211 for (i = 0; i != NE; ++i) {
14212 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
14213 SDValue Operand = N->getOperand(j);
14214 EVT OperandVT = Operand.getValueType();
14215
14216 // A vector operand; extract a single element.
14217 EVT OperandEltVT = OperandVT.getVectorElementType();
14218 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
14219 }
14220
14221 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
14222 Scalars0.push_back(EltOp);
14223 Scalars1.push_back(EltOp.getValue(1));
14224 }
14225
14226 for (; i < ResNE; ++i) {
14227 Scalars0.push_back(getUNDEF(EltVT));
14228 Scalars1.push_back(getUNDEF(EltVT1));
14229 }
14230
14231 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
14232 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
14233 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
14234 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
14235 return getMergeValues({Vec0, Vec1}, dl);
14236 }
14237
14238 assert(N->getNumValues() == 1 &&
14239 "Can't unroll a vector with multiple results!");
14240
14242 SmallVector<SDValue, 4> Operands(N->getNumOperands());
14243
14244 unsigned i;
14245 for (i= 0; i != NE; ++i) {
14246 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
14247 SDValue Operand = N->getOperand(j);
14248 EVT OperandVT = Operand.getValueType();
14249 if (OperandVT.isVector()) {
14250 // A vector operand; extract a single element.
14251 EVT OperandEltVT = OperandVT.getVectorElementType();
14252 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
14253 } else {
14254 // A scalar operand; just use it as is.
14255 Operands[j] = Operand;
14256 }
14257 }
14258
14259 switch (N->getOpcode()) {
14260 default: {
14261 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
14262 N->getFlags()));
14263 break;
14264 }
14265 case ISD::VSELECT:
14266 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
14267 break;
14268 case ISD::SHL:
14269 case ISD::SRA:
14270 case ISD::SRL:
14271 case ISD::ROTL:
14272 case ISD::ROTR:
14273 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
14274 getShiftAmountOperand(Operands[0].getValueType(),
14275 Operands[1])));
14276 break;
14278 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
14279 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
14280 Operands[0],
14281 getValueType(ExtVT)));
14282 break;
14283 }
14284 case ISD::ADDRSPACECAST: {
14285 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
14286 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
14287 ASC->getSrcAddressSpace(),
14288 ASC->getDestAddressSpace()));
14289 break;
14290 }
14291 }
14292 }
14293
14294 for (; i < ResNE; ++i)
14295 Scalars.push_back(getUNDEF(EltVT));
14296
14297 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
14298 return getBuildVector(VecVT, dl, Scalars);
14299}
14300
14301std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
14302 SDNode *N, unsigned ResNE) {
14303 unsigned Opcode = N->getOpcode();
14304 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
14305 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
14306 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
14307 "Expected an overflow opcode");
14308
14309 EVT ResVT = N->getValueType(0);
14310 EVT OvVT = N->getValueType(1);
14311 EVT ResEltVT = ResVT.getVectorElementType();
14312 EVT OvEltVT = OvVT.getVectorElementType();
14313 SDLoc dl(N);
14314
14315 // If ResNE is 0, fully unroll the vector op.
14316 unsigned NE = ResVT.getVectorNumElements();
14317 if (ResNE == 0)
14318 ResNE = NE;
14319 else if (NE > ResNE)
14320 NE = ResNE;
14321
14322 SmallVector<SDValue, 8> LHSScalars;
14323 SmallVector<SDValue, 8> RHSScalars;
14324 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
14325 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
14326
14327 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
14328 SDVTList VTs = getVTList(ResEltVT, SVT);
14329 SmallVector<SDValue, 8> ResScalars;
14330 SmallVector<SDValue, 8> OvScalars;
14331 for (unsigned i = 0; i < NE; ++i) {
14332 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
14333 SDValue Ov =
14334 getSelect(dl, OvEltVT, Res.getValue(1),
14335 getBoolConstant(true, dl, OvEltVT, ResVT),
14336 getConstant(0, dl, OvEltVT));
14337
14338 ResScalars.push_back(Res);
14339 OvScalars.push_back(Ov);
14340 }
14341
14342 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
14343 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
14344
14345 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
14346 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
14347 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
14348 getBuildVector(NewOvVT, dl, OvScalars));
14349}
14350
14353 unsigned Bytes,
14354 int Dist) const {
14355 if (LD->isVolatile() || Base->isVolatile())
14356 return false;
14357 // TODO: probably too restrictive for atomics, revisit
14358 if (!LD->isSimple())
14359 return false;
14360 if (LD->isIndexed() || Base->isIndexed())
14361 return false;
14362 if (LD->getChain() != Base->getChain())
14363 return false;
14364 EVT VT = LD->getMemoryVT();
14365 if (VT.getSizeInBits() / 8 != Bytes)
14366 return false;
14367
14368 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
14369 auto LocDecomp = BaseIndexOffset::match(LD, *this);
14370
14371 int64_t Offset = 0;
14372 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
14373 return (Dist * (int64_t)Bytes == Offset);
14374 return false;
14375}
14376
14377/// InferPtrAlignment - Infer alignment of a load / store address. Return
14378/// std::nullopt if it cannot be inferred.
14380 // If this is a GlobalAddress + cst, return the alignment.
14381 const GlobalValue *GV = nullptr;
14382 int64_t GVOffset = 0;
14383 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
14384 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
14385 KnownBits Known(PtrWidth);
14387 unsigned AlignBits = Known.countMinTrailingZeros();
14388 if (AlignBits)
14389 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
14390 }
14391
14392 // If this is a direct reference to a stack slot, use information about the
14393 // stack slot's alignment.
14394 int FrameIdx = INT_MIN;
14395 int64_t FrameOffset = 0;
14397 FrameIdx = FI->getIndex();
14398 } else if (isBaseWithConstantOffset(Ptr) &&
14400 // Handle FI+Cst
14401 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
14402 FrameOffset = Ptr.getConstantOperandVal(1);
14403 }
14404
14405 if (FrameIdx != INT_MIN) {
14407 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
14408 }
14409
14410 return std::nullopt;
14411}
14412
14413/// Split the scalar node with EXTRACT_ELEMENT using the provided
14414/// VTs and return the low/high part.
14415std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
14416 const SDLoc &DL,
14417 const EVT &LoVT,
14418 const EVT &HiVT) {
14419 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
14420 "Split node must be a scalar type");
14421 SDValue Lo =
14423 SDValue Hi =
14425 return std::make_pair(Lo, Hi);
14426}
14427
14428/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
14429/// which is split (or expanded) into two not necessarily identical pieces.
14430std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
14431 // Currently all types are split in half.
14432 EVT LoVT, HiVT;
14433 if (!VT.isVector())
14434 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
14435 else
14436 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
14437
14438 return std::make_pair(LoVT, HiVT);
14439}
14440
14441/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
14442/// type, dependent on an enveloping VT that has been split into two identical
14443/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
14444std::pair<EVT, EVT>
14446 bool *HiIsEmpty) const {
14447 EVT EltTp = VT.getVectorElementType();
14448 // Examples:
14449 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
14450 // custom VL=9 with enveloping VL=8/8 yields 8/1
14451 // custom VL=10 with enveloping VL=8/8 yields 8/2
14452 // etc.
14453 ElementCount VTNumElts = VT.getVectorElementCount();
14454 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
14455 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
14456 "Mixing fixed width and scalable vectors when enveloping a type");
14457 EVT LoVT, HiVT;
14458 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
14459 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
14460 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
14461 *HiIsEmpty = false;
14462 } else {
14463 // Flag that hi type has zero storage size, but return split envelop type
14464 // (this would be easier if vector types with zero elements were allowed).
14465 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
14466 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
14467 *HiIsEmpty = true;
14468 }
14469 return std::make_pair(LoVT, HiVT);
14470}
14471
14472/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
14473/// low/high part.
14474std::pair<SDValue, SDValue>
14475SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
14476 const EVT &HiVT) {
14477 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
14478 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
14479 "Splitting vector with an invalid mixture of fixed and scalable "
14480 "vector types");
14482 N.getValueType().getVectorMinNumElements() &&
14483 "More vector elements requested than available!");
14484 SDValue Lo, Hi;
14485 Lo = getExtractSubvector(DL, LoVT, N, 0);
14486 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
14487 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
14488 // IDX with the runtime scaling factor of the result vector type. For
14489 // fixed-width result vectors, that runtime scaling factor is 1.
14491 return std::make_pair(Lo, Hi);
14492}
14493
14494std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
14495 const SDLoc &DL) {
14496 // Split the vector length parameter.
14497 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
14498 EVT VT = N.getValueType();
14500 "Expecting the mask to be an evenly-sized vector");
14501 SDValue HalfNumElts = getElementCount(
14503 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
14504 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
14505 return std::make_pair(Lo, Hi);
14506}
14507
14508/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
14510 EVT VT = N.getValueType();
14513 return getInsertSubvector(DL, getPOISON(WideVT), N, 0);
14514}
14515
14518 unsigned Start, unsigned Count,
14519 EVT EltVT) {
14520 EVT VT = Op.getValueType();
14521 if (Count == 0)
14523 if (EltVT == EVT())
14524 EltVT = VT.getVectorElementType();
14525 SDLoc SL(Op);
14526 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
14527 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
14528 }
14529}
14530
14531// getAddressSpace - Return the address space this GlobalAddress belongs to.
14533 return getGlobal()->getType()->getAddressSpace();
14534}
14535
14538 return Val.MachineCPVal->getType();
14539 return Val.ConstVal->getType();
14540}
14541
14542bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
14543 unsigned &SplatBitSize,
14544 bool &HasAnyUndefs,
14545 unsigned MinSplatBits,
14546 bool IsBigEndian) const {
14547 EVT VT = getValueType(0);
14548 assert(VT.isVector() && "Expected a vector type");
14549 unsigned VecWidth = VT.getSizeInBits();
14550 if (MinSplatBits > VecWidth)
14551 return false;
14552
14553 // FIXME: The widths are based on this node's type, but build vectors can
14554 // truncate their operands.
14555 SplatValue = APInt(VecWidth, 0);
14556 SplatUndef = APInt(VecWidth, 0);
14557
14558 // Get the bits. Bits with undefined values (when the corresponding element
14559 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
14560 // in SplatValue. If any of the values are not constant, give up and return
14561 // false.
14562 unsigned int NumOps = getNumOperands();
14563 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
14564 unsigned EltWidth = VT.getScalarSizeInBits();
14565
14566 for (unsigned j = 0; j < NumOps; ++j) {
14567 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
14568 SDValue OpVal = getOperand(i);
14569 unsigned BitPos = j * EltWidth;
14570
14571 if (OpVal.isUndef())
14572 SplatUndef.setBits(BitPos, BitPos + EltWidth);
14573 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
14574 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
14575 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
14576 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
14577 else
14578 return false;
14579 }
14580
14581 // The build_vector is all constants or undefs. Find the smallest element
14582 // size that splats the vector.
14583 HasAnyUndefs = (SplatUndef != 0);
14584
14585 // FIXME: This does not work for vectors with elements less than 8 bits.
14586 while (VecWidth > 8) {
14587 // If we can't split in half, stop here.
14588 if (VecWidth & 1)
14589 break;
14590
14591 unsigned HalfSize = VecWidth / 2;
14592 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
14593 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
14594 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
14595 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
14596
14597 // If the two halves do not match (ignoring undef bits), stop here.
14598 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
14599 MinSplatBits > HalfSize)
14600 break;
14601
14602 SplatValue = HighValue | LowValue;
14603 SplatUndef = HighUndef & LowUndef;
14604
14605 VecWidth = HalfSize;
14606 }
14607
14608 // FIXME: The loop above only tries to split in halves. But if the input
14609 // vector for example is <3 x i16> it wouldn't be able to detect a
14610 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
14611 // optimizations. I guess that back in the days when this helper was created
14612 // vectors normally was power-of-2 sized.
14613
14614 SplatBitSize = VecWidth;
14615 return true;
14616}
14617
14619 BitVector *UndefElements) const {
14620 unsigned NumOps = getNumOperands();
14621 if (UndefElements) {
14622 UndefElements->clear();
14623 UndefElements->resize(NumOps);
14624 }
14625 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
14626 if (!DemandedElts)
14627 return SDValue();
14628 SDValue Splatted;
14629 for (unsigned i = 0; i != NumOps; ++i) {
14630 if (!DemandedElts[i])
14631 continue;
14632 SDValue Op = getOperand(i);
14633 if (Op.isUndef()) {
14634 if (UndefElements)
14635 (*UndefElements)[i] = true;
14636 } else if (!Splatted) {
14637 Splatted = Op;
14638 } else if (Splatted != Op) {
14639 return SDValue();
14640 }
14641 }
14642
14643 if (!Splatted) {
14644 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
14645 assert(getOperand(FirstDemandedIdx).isUndef() &&
14646 "Can only have a splat without a constant for all undefs.");
14647 return getOperand(FirstDemandedIdx);
14648 }
14649
14650 return Splatted;
14651}
14652
14654 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
14655 return getSplatValue(DemandedElts, UndefElements);
14656}
14657
14659 SmallVectorImpl<SDValue> &Sequence,
14660 BitVector *UndefElements) const {
14661 unsigned NumOps = getNumOperands();
14662 Sequence.clear();
14663 if (UndefElements) {
14664 UndefElements->clear();
14665 UndefElements->resize(NumOps);
14666 }
14667 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
14668 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
14669 return false;
14670
14671 // Set the undefs even if we don't find a sequence (like getSplatValue).
14672 if (UndefElements)
14673 for (unsigned I = 0; I != NumOps; ++I)
14674 if (DemandedElts[I] && getOperand(I).isUndef())
14675 (*UndefElements)[I] = true;
14676
14677 // Iteratively widen the sequence length looking for repetitions.
14678 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
14679 Sequence.append(SeqLen, SDValue());
14680 for (unsigned I = 0; I != NumOps; ++I) {
14681 if (!DemandedElts[I])
14682 continue;
14683 SDValue &SeqOp = Sequence[I % SeqLen];
14685 if (Op.isUndef()) {
14686 if (!SeqOp)
14687 SeqOp = Op;
14688 continue;
14689 }
14690 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
14691 Sequence.clear();
14692 break;
14693 }
14694 SeqOp = Op;
14695 }
14696 if (!Sequence.empty())
14697 return true;
14698 }
14699
14700 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
14701 return false;
14702}
14703
14705 BitVector *UndefElements) const {
14706 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
14707 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
14708}
14709
14712 BitVector *UndefElements) const {
14714 getSplatValue(DemandedElts, UndefElements));
14715}
14716
14719 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
14720}
14721
14724 BitVector *UndefElements) const {
14726 getSplatValue(DemandedElts, UndefElements));
14727}
14728
14733
14734int32_t
14736 uint32_t BitWidth) const {
14737 if (ConstantFPSDNode *CN =
14739 bool IsExact;
14740 APSInt IntVal(BitWidth);
14741 const APFloat &APF = CN->getValueAPF();
14742 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
14743 APFloat::opOK ||
14744 !IsExact)
14745 return -1;
14746
14747 return IntVal.exactLogBase2();
14748 }
14749 return -1;
14750}
14751
14753 bool IsLittleEndian, unsigned DstEltSizeInBits,
14754 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
14755 // Early-out if this contains anything but Undef/Constant/ConstantFP.
14756 if (!isConstant())
14757 return false;
14758
14759 unsigned NumSrcOps = getNumOperands();
14760 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
14761 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14762 "Invalid bitcast scale");
14763
14764 // Extract raw src bits.
14765 SmallVector<APInt> SrcBitElements(NumSrcOps,
14766 APInt::getZero(SrcEltSizeInBits));
14767 BitVector SrcUndeElements(NumSrcOps, false);
14768
14769 for (unsigned I = 0; I != NumSrcOps; ++I) {
14771 if (Op.isUndef()) {
14772 SrcUndeElements.set(I);
14773 continue;
14774 }
14775 auto *CInt = dyn_cast<ConstantSDNode>(Op);
14776 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
14777 assert((CInt || CFP) && "Unknown constant");
14778 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
14779 : CFP->getValueAPF().bitcastToAPInt();
14780 }
14781
14782 // Recast to dst width.
14783 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
14784 SrcBitElements, UndefElements, SrcUndeElements);
14785 return true;
14786}
14787
14788void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
14789 unsigned DstEltSizeInBits,
14790 SmallVectorImpl<APInt> &DstBitElements,
14791 ArrayRef<APInt> SrcBitElements,
14792 BitVector &DstUndefElements,
14793 const BitVector &SrcUndefElements) {
14794 unsigned NumSrcOps = SrcBitElements.size();
14795 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
14796 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14797 "Invalid bitcast scale");
14798 assert(NumSrcOps == SrcUndefElements.size() &&
14799 "Vector size mismatch");
14800
14801 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
14802 DstUndefElements.clear();
14803 DstUndefElements.resize(NumDstOps, false);
14804 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
14805
14806 // Concatenate src elements constant bits together into dst element.
14807 if (SrcEltSizeInBits <= DstEltSizeInBits) {
14808 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
14809 for (unsigned I = 0; I != NumDstOps; ++I) {
14810 DstUndefElements.set(I);
14811 APInt &DstBits = DstBitElements[I];
14812 for (unsigned J = 0; J != Scale; ++J) {
14813 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14814 if (SrcUndefElements[Idx])
14815 continue;
14816 DstUndefElements.reset(I);
14817 const APInt &SrcBits = SrcBitElements[Idx];
14818 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
14819 "Illegal constant bitwidths");
14820 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
14821 }
14822 }
14823 return;
14824 }
14825
14826 // Split src element constant bits into dst elements.
14827 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
14828 for (unsigned I = 0; I != NumSrcOps; ++I) {
14829 if (SrcUndefElements[I]) {
14830 DstUndefElements.set(I * Scale, (I + 1) * Scale);
14831 continue;
14832 }
14833 const APInt &SrcBits = SrcBitElements[I];
14834 for (unsigned J = 0; J != Scale; ++J) {
14835 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14836 APInt &DstBits = DstBitElements[Idx];
14837 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
14838 }
14839 }
14840}
14841
14843 for (const SDValue &Op : op_values()) {
14844 unsigned Opc = Op.getOpcode();
14845 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
14846 return false;
14847 }
14848 return true;
14849}
14850
14851std::optional<std::pair<APInt, APInt>>
14853 unsigned NumOps = getNumOperands();
14854 if (NumOps < 2)
14855 return std::nullopt;
14856
14857 unsigned EltSize = getValueType(0).getScalarSizeInBits();
14858 APInt Start, Stride;
14859 int FirstIdx = -1, SecondIdx = -1;
14860
14861 // Find the first two non-undef constant elements to determine Start and
14862 // Stride, then verify all remaining elements match the sequence.
14863 for (unsigned I = 0; I < NumOps; ++I) {
14865 if (Op->isUndef())
14866 continue;
14867 if (!isa<ConstantSDNode>(Op))
14868 return std::nullopt;
14869
14870 APInt Val = getConstantOperandAPInt(I).trunc(EltSize);
14871 if (FirstIdx < 0) {
14872 FirstIdx = I;
14873 Start = Val;
14874 } else if (SecondIdx < 0) {
14875 SecondIdx = I;
14876 // Compute stride using modular arithmetic. Simple division would handle
14877 // common strides (1, 2, -1, etc.), but modular inverse maximizes matches.
14878 // Example: <0, poison, poison, 0xFF> has stride 0x55 since 3*0x55 = 0xFF
14879 // Note that modular arithmetic is agnostic to signed/unsigned.
14880 unsigned IdxDiff = I - FirstIdx;
14881 APInt ValDiff = Val - Start;
14882
14883 // Step 1: Factor out common powers of 2 from IdxDiff and ValDiff.
14884 unsigned CommonPow2Bits = llvm::countr_zero(IdxDiff);
14885 if (ValDiff.countr_zero() < CommonPow2Bits)
14886 return std::nullopt; // ValDiff not divisible by 2^CommonPow2Bits
14887 IdxDiff >>= CommonPow2Bits;
14888 ValDiff.lshrInPlace(CommonPow2Bits);
14889
14890 // Step 2: IdxDiff is now odd, so its inverse mod 2^EltSize exists.
14891 // TODO: There are 2^CommonPow2Bits valid strides; currently we only try
14892 // one, but we could try all candidates to handle more cases.
14893 Stride = ValDiff * APInt(EltSize, IdxDiff).multiplicativeInverse();
14894 if (Stride.isZero())
14895 return std::nullopt;
14896
14897 // Step 3: Adjust Start based on the first defined element's index.
14898 Start -= Stride * FirstIdx;
14899 } else {
14900 // Verify this element matches the sequence.
14901 if (Val != Start + Stride * I)
14902 return std::nullopt;
14903 }
14904 }
14905
14906 // Need at least two defined elements.
14907 if (SecondIdx < 0)
14908 return std::nullopt;
14909
14910 return std::make_pair(Start, Stride);
14911}
14912
14914 // Find the first non-undef value in the shuffle mask.
14915 unsigned i, e;
14916 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
14917 /* search */;
14918
14919 // If all elements are undefined, this shuffle can be considered a splat
14920 // (although it should eventually get simplified away completely).
14921 if (i == e)
14922 return true;
14923
14924 // Make sure all remaining elements are either undef or the same as the first
14925 // non-undef value.
14926 for (int Idx = Mask[i]; i != e; ++i)
14927 if (Mask[i] >= 0 && Mask[i] != Idx)
14928 return false;
14929 return true;
14930}
14931
14932// Returns true if it is a constant integer BuildVector or constant integer,
14933// possibly hidden by a bitcast.
14935 SDValue N, bool AllowOpaques) const {
14937
14938 if (auto *C = dyn_cast<ConstantSDNode>(N))
14939 return AllowOpaques || !C->isOpaque();
14940
14942 return true;
14943
14944 // Treat a GlobalAddress supporting constant offset folding as a
14945 // constant integer.
14946 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14947 if (GA->getOpcode() == ISD::GlobalAddress &&
14948 TLI->isOffsetFoldingLegal(GA))
14949 return true;
14950
14951 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14952 isa<ConstantSDNode>(N.getOperand(0)))
14953 return true;
14954 return false;
14955}
14956
14957// Returns true if it is a constant float BuildVector or constant float.
14960 return true;
14961
14963 return true;
14964
14965 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14966 isa<ConstantFPSDNode>(N.getOperand(0)))
14967 return true;
14968
14969 return false;
14970}
14971
14972std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14973 ConstantSDNode *Const =
14974 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14975 if (!Const)
14976 return std::nullopt;
14977
14978 EVT VT = N->getValueType(0);
14979 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14980 switch (TLI->getBooleanContents(N.getValueType())) {
14982 if (CVal.isOne())
14983 return true;
14984 if (CVal.isZero())
14985 return false;
14986 return std::nullopt;
14988 if (CVal.isAllOnes())
14989 return true;
14990 if (CVal.isZero())
14991 return false;
14992 return std::nullopt;
14994 return CVal[0];
14995 }
14996 llvm_unreachable("Unknown BooleanContent enum");
14997}
14998
14999void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
15000 assert(!Node->OperandList && "Node already has operands");
15002 "too many operands to fit into SDNode");
15003 SDUse *Ops = OperandRecycler.allocate(
15004 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
15005
15006 bool IsDivergent = false;
15007 for (unsigned I = 0; I != Vals.size(); ++I) {
15008 Ops[I].setUser(Node);
15009 Ops[I].setInitial(Vals[I]);
15010 EVT VT = Ops[I].getValueType();
15011
15012 // Skip Chain. It does not carry divergence.
15013 if (VT != MVT::Other &&
15014 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
15015 Ops[I].getNode()->isDivergent()) {
15016 IsDivergent = true;
15017 }
15018 }
15019 Node->NumOperands = Vals.size();
15020 Node->OperandList = Ops;
15021 if (!TLI->isSDNodeAlwaysUniform(Node)) {
15022 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
15023 Node->SDNodeBits.IsDivergent = IsDivergent;
15024 }
15025 checkForCycles(Node);
15026}
15027
15030 size_t Limit = SDNode::getMaxNumOperands();
15031 while (Vals.size() > Limit) {
15032 unsigned SliceIdx = Vals.size() - Limit;
15033 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
15034 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
15035 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
15036 Vals.emplace_back(NewTF);
15037 }
15038 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
15039}
15040
15042 EVT VT, SDNodeFlags Flags) {
15043 switch (Opcode) {
15044 default:
15045 return SDValue();
15046 case ISD::ADD:
15047 case ISD::OR:
15048 case ISD::XOR:
15049 case ISD::UMAX:
15050 return getConstant(0, DL, VT);
15051 case ISD::MUL:
15052 return getConstant(1, DL, VT);
15053 case ISD::AND:
15054 case ISD::UMIN:
15055 return getAllOnesConstant(DL, VT);
15056 case ISD::SMAX:
15058 case ISD::SMIN:
15060 case ISD::FADD:
15061 // If flags allow, prefer positive zero since it's generally cheaper
15062 // to materialize on most targets.
15063 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
15064 case ISD::FMUL:
15065 return getConstantFP(1.0, DL, VT);
15066 case ISD::FMINNUM:
15067 case ISD::FMAXNUM: {
15068 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
15069 const fltSemantics &Semantics = VT.getFltSemantics();
15070 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
15071 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
15072 APFloat::getLargest(Semantics);
15073 if (Opcode == ISD::FMAXNUM)
15074 NeutralAF.changeSign();
15075
15076 return getConstantFP(NeutralAF, DL, VT);
15077 }
15078 case ISD::FMINIMUM:
15079 case ISD::FMAXIMUM: {
15080 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
15081 const fltSemantics &Semantics = VT.getFltSemantics();
15082 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
15083 : APFloat::getLargest(Semantics);
15084 if (Opcode == ISD::FMAXIMUM)
15085 NeutralAF.changeSign();
15086
15087 return getConstantFP(NeutralAF, DL, VT);
15088 }
15089
15090 }
15091}
15092
15094 SDValue Acc, SDValue LHS,
15095 SDValue RHS) {
15096 EVT AccVT = Acc.getValueType();
15097 if (AccVT.isFloatingPoint()) {
15098 assert(Opc == ISD::PARTIAL_REDUCE_FMLA && "Unexpected opcode");
15099 SDValue NegRHS = getNode(ISD::FNEG, DL, RHS.getValueType(), RHS);
15100 return getNode(Opc, DL, AccVT, Acc, LHS, NegRHS);
15101 }
15103 "Unexpected opcode");
15104 SDValue NegAcc = getNegative(Acc, DL, AccVT);
15105 SDValue MLA = getNode(Opc, DL, AccVT, NegAcc, LHS, RHS);
15106 return getNegative(MLA, DL, AccVT);
15107}
15108
15109/// Helper used to make a call to a library function that has one argument of
15110/// pointer type.
15111///
15112/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
15113/// used to get or set floating-point state. They have one argument of pointer
15114/// type, which points to the memory region containing bits of the
15115/// floating-point state. The value returned by such function is ignored in the
15116/// created call.
15117///
15118/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
15119/// \param Ptr Pointer used to save/load state.
15120/// \param InChain Ingoing token chain.
15121/// \returns Outgoing chain token.
15123 SDValue InChain,
15124 const SDLoc &DLoc) {
15125 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
15127 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
15128 RTLIB::LibcallImpl LibcallImpl =
15129 Libcalls->getLibcallImpl(static_cast<RTLIB::Libcall>(LibFunc));
15130 if (LibcallImpl == RTLIB::Unsupported)
15131 reportFatalUsageError("emitting call to unsupported libcall");
15132
15133 SDValue Callee =
15134 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout()));
15136 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
15137 Libcalls->getLibcallImplCallingConv(LibcallImpl),
15138 Type::getVoidTy(*getContext()), Callee, std::move(Args));
15139 return TLI->LowerCallTo(CLI).second;
15140}
15141
15143 assert(From && To && "Invalid SDNode; empty source SDValue?");
15144 auto I = SDEI.find(From);
15145 if (I == SDEI.end())
15146 return;
15147
15148 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
15149 // the iterator, hence the need to make a copy to prevent a use-after-free.
15150 NodeExtraInfo NEI = I->second;
15151 if (LLVM_LIKELY(!NEI.PCSections)) {
15152 // No deep copy required for the types of extra info set.
15153 //
15154 // FIXME: Investigate if other types of extra info also need deep copy. This
15155 // depends on the types of nodes they can be attached to: if some extra info
15156 // is only ever attached to nodes where a replacement To node is always the
15157 // node where later use and propagation of the extra info has the intended
15158 // semantics, no deep copy is required.
15159 SDEI[To] = std::move(NEI);
15160 return;
15161 }
15162
15163 const SDNode *EntrySDN = getEntryNode().getNode();
15164
15165 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
15166 // through the replacement of From with To. Otherwise, replacements of a node
15167 // (From) with more complex nodes (To and its operands) may result in lost
15168 // extra info where the root node (To) is insignificant in further propagating
15169 // and using extra info when further lowering to MIR.
15170 //
15171 // In the first step pre-populate the visited set with the nodes reachable
15172 // from the old From node. This avoids copying NodeExtraInfo to parts of the
15173 // DAG that is not new and should be left untouched.
15174 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
15175 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
15176 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
15177 if (MaxDepth == 0) {
15178 // Remember this node in case we need to increase MaxDepth and continue
15179 // populating FromReach from this node.
15180 Leafs.emplace_back(N);
15181 return;
15182 }
15183 if (!FromReach.insert(N).second)
15184 return;
15185 for (const SDValue &Op : N->op_values())
15186 Self(Self, Op.getNode(), MaxDepth - 1);
15187 };
15188
15189 // Copy extra info to To and all its transitive operands (that are new).
15191 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
15192 if (FromReach.contains(N))
15193 return true;
15194 if (!Visited.insert(N).second)
15195 return true;
15196 if (EntrySDN == N)
15197 return false;
15198 for (const SDValue &Op : N->op_values()) {
15199 if (N == To && Op.getNode() == EntrySDN) {
15200 // Special case: New node's operand is the entry node; just need to
15201 // copy extra info to new node.
15202 break;
15203 }
15204 if (!Self(Self, Op.getNode()))
15205 return false;
15206 }
15207 // Copy only if entry node was not reached.
15208 SDEI[N] = std::move(NEI);
15209 return true;
15210 };
15211
15212 // We first try with a lower MaxDepth, assuming that the path to common
15213 // operands between From and To is relatively short. This significantly
15214 // improves performance in the common case. The initial MaxDepth is big
15215 // enough to avoid retry in the common case; the last MaxDepth is large
15216 // enough to avoid having to use the fallback below (and protects from
15217 // potential stack exhaustion from recursion).
15218 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
15219 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
15220 // StartFrom is the previous (or initial) set of leafs reachable at the
15221 // previous maximum depth.
15223 std::swap(StartFrom, Leafs);
15224 for (const SDNode *N : StartFrom)
15225 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
15226 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
15227 return;
15228 // This should happen very rarely (reached the entry node).
15229 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
15230 assert(!Leafs.empty());
15231 }
15232
15233 // This should not happen - but if it did, that means the subgraph reachable
15234 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
15235 // could not visit all reachable common operands. Consequently, we were able
15236 // to reach the entry node.
15237 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
15238 assert(false && "From subgraph too complex - increase max. MaxDepth?");
15239 // Best-effort fallback if assertions disabled.
15240 SDEI[To] = std::move(NEI);
15241}
15242
15243#ifndef NDEBUG
15244static void checkForCyclesHelper(const SDNode *N,
15247 const llvm::SelectionDAG *DAG) {
15248 // If this node has already been checked, don't check it again.
15249 if (Checked.count(N))
15250 return;
15251
15252 // If a node has already been visited on this depth-first walk, reject it as
15253 // a cycle.
15254 if (!Visited.insert(N).second) {
15255 errs() << "Detected cycle in SelectionDAG\n";
15256 dbgs() << "Offending node:\n";
15257 N->dumprFull(DAG); dbgs() << "\n";
15258 abort();
15259 }
15260
15261 for (const SDValue &Op : N->op_values())
15262 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
15263
15264 Checked.insert(N);
15265 Visited.erase(N);
15266}
15267#endif
15268
15270 const llvm::SelectionDAG *DAG,
15271 bool force) {
15272#ifndef NDEBUG
15273 bool check = force;
15274#ifdef EXPENSIVE_CHECKS
15275 check = true;
15276#endif // EXPENSIVE_CHECKS
15277 if (check) {
15278 assert(N && "Checking nonexistent SDNode");
15281 checkForCyclesHelper(N, visited, checked, DAG);
15282 }
15283#endif // !NDEBUG
15284}
15285
15286void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
15287 checkForCycles(DAG->getRoot().getNode(), DAG, force);
15288}
return SDValue()
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isConstant(const MachineInstr &MI)
constexpr LLT S1
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
#define X(NUM, ENUM, NAME)
Definition ELF.h:856
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
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:594
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:337
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
This file defines a hash set that can be used to remove duplication of nodes in a graph.
static MaybeAlign getAlign(Value *Ptr)
iv users
Definition IVUsers.cpp:48
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
static constexpr Value * getValue(Ty &ValueOrUse)
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static Register getMemsetValue(Register Val, LLT Ty, MachineIRBuilder &MIB)
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:539
static Align getPrefTypeAlign(EVT VT, SelectionDAG &DAG)
static bool isConstantSplatVector(SDValue N, APInt &SplatValue, unsigned MinSizeInBits)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register const TargetRegisterInfo * TRI
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
#define T
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define P(N)
PowerPC Reduce CR logical Operation
const SmallVectorImpl< MachineOperand > & Cond
Remove Loads Into Fake Uses
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
Contains matchers for matching SelectionDAG nodes and values.
static Type * getValueType(Value *V, bool LookThroughCmp=false)
Returns the "element 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 bool shouldLowerMemFuncForSize(const MachineFunction &MF, SelectionDAG &DAG)
static SDValue getFixedOrScalableQuantity(SelectionDAG &DAG, const SDLoc &DL, EVT VT, Ty Quantity)
static std::pair< SDValue, SDValue > getRuntimeCallSDValueHelper(SDValue Chain, const SDLoc &dl, TargetLowering::ArgListTy &&Args, const CallInst *CI, RTLIB::Libcall Call, SelectionDAG *DAG, const TargetLowering *TLI)
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 getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align DstAlign, Align SrcAlign, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo, BatchAAResults *BatchAA)
static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC)
AddNodeIDOpcode - Add the node opcode to the NodeID data.
static ISD::CondCode getSetCCInverseImpl(ISD::CondCode Op, bool isIntegerLike)
static bool doNotCSE(SDNode *N)
doNotCSE - Return true if CSE should not be performed for this node.
static cl::opt< int > MaxLdStGlue("ldstmemcpy-glue-max", cl::desc("Number limit for gluing ld/st of memcpy."), cl::Hidden, cl::init(0))
static void AddNodeIDOperands(FoldingSetNodeID &ID, ArrayRef< SDValue > Ops)
AddNodeIDOperands - Various routines for adding operands to the NodeID data.
static SDValue foldCONCAT_VECTORS(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
Try to simplify vector concatenation to an input value, undef, or build vector.
static MachinePointerInfo InferPointerInfo(const MachinePointerInfo &Info, SelectionDAG &DAG, SDValue Ptr, int64_t Offset=0)
InferPointerInfo - If the specified ptr/offset is a frame index, infer a MachinePointerInfo record fr...
static bool isInTailCallPositionWrapper(const CallInst *CI, const SelectionDAG *SelDAG, bool AllowReturnsFirstArg)
static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N)
If this is an SDNode with special info, add this info to the NodeID data.
static bool gluePropagatesDivergence(const SDNode *Node)
Return true if a glue output should propagate divergence information.
static void NewSDValueDbgMsg(SDValue V, StringRef Msg, SelectionDAG *G)
static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs)
makeVTList - Return an instance of the SDVTList struct initialized with the specified members.
static void checkForCyclesHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallPtrSetImpl< const SDNode * > &Checked, const llvm::SelectionDAG *DAG)
static void chainLoadsAndStoresForMemcpy(SelectionDAG &DAG, const SDLoc &dl, SmallVector< SDValue, 32 > &OutChains, unsigned From, unsigned To, SmallVector< SDValue, 16 > &OutLoadChains, SmallVector< SDValue, 16 > &OutStoreChains)
static int isSignedOp(ISD::CondCode Opcode)
For an integer comparison, return 1 if the comparison is a signed operation and 2 if the result is an...
static std::optional< APInt > FoldValue(unsigned Opcode, const APInt &C1, const APInt &C2)
static SDValue FoldBUILD_VECTOR(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
static void checkAddrSpaceIsValidForLibcall(const TargetLowering *TLI, unsigned AS)
static cl::opt< unsigned > MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192), cl::desc("DAG combiner limit number of steps when searching DAG " "for predecessor nodes"))
static APInt getDemandAllEltsMask(SDValue V)
Construct a DemandedElts mask which demands all elements of V.
static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align DstAlign, Align SrcAlign, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo)
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
This file describes how to lower LLVM code to machine code.
static void removeOperands(MachineInstr &MI, unsigned i)
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
static unsigned getSize(unsigned Kind)
static const fltSemantics & IEEEsingle()
Definition APFloat.h:297
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition APFloat.h:335
static constexpr roundingMode rmTowardZero
Definition APFloat.h:349
static const fltSemantics & BFloat()
Definition APFloat.h:296
static const fltSemantics & IEEEquad()
Definition APFloat.h:299
static const fltSemantics & IEEEdouble()
Definition APFloat.h:298
static constexpr roundingMode rmTowardNegative
Definition APFloat.h:348
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:345
static constexpr roundingMode rmTowardPositive
Definition APFloat.h:347
static const fltSemantics & IEEEhalf()
Definition APFloat.h:295
opStatus
IEEE-754R 7: Default exception handling.
Definition APFloat.h:361
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition APFloat.h:1185
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1273
void copySign(const APFloat &RHS)
Definition APFloat.h:1367
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:5901
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1255
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1246
bool isFinite() const
Definition APFloat.h:1549
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition APFloat.h:1412
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1264
bool isZero() const
Definition APFloat.h:1540
LLVM_READONLY bool isOne() const
Definition APFloat.h:1622
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1203
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.h:1397
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1163
opStatus mod(const APFloat &RHS)
Definition APFloat.h:1291
bool isPosZero() const
Definition APFloat.h:1555
bool isNegZero() const
Definition APFloat.h:1556
void changeSign()
Definition APFloat.h:1362
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition APFloat.h:1174
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:2006
LLVM_ABI APInt usub_sat(const APInt &RHS) const
Definition APInt.cpp:2090
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1599
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1429
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1055
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:230
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:424
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1563
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1414
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1693
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1408
LLVM_ABI APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition APInt.cpp:640
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1076
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1535
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:968
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1353
APInt abs() const
Get the absolute value.
Definition APInt.h:1818
LLVM_ABI APInt sadd_sat(const APInt &RHS) const
Definition APInt.cpp:2061
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1189
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition APInt.h:259
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1692
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1511
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1118
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1670
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1419
LLVM_ABI APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition APInt.cpp:1197
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:790
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition APInt.h:841
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1173
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1662
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1651
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1621
static LLVM_ABI APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition APInt.cpp:652
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt sshl_sat(const APInt &RHS) const
Definition APInt.cpp:2121
LLVM_ABI APInt ushl_sat(const APInt &RHS) const
Definition APInt.cpp:2135
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1084
static bool isSameValue(const APInt &I1, const APInt &I2, bool SignedCompare=false)
Determine if two APInts have the same value, after zero-extending or sign-extending (if SignedCompare...
Definition APInt.h:555
LLVM_ABI APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition APInt.cpp:1184
LLVM_ABI void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition APInt.cpp:398
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition APInt.h:1458
unsigned logBase2() const
Definition APInt.h:1784
LLVM_ABI APInt uadd_sat(const APInt &RHS) const
Definition APInt.cpp:2071
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:834
LLVM_ABI APInt multiplicativeInverse() const
Definition APInt.cpp:1300
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1771
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1157
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition APInt.cpp:1028
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition APInt.h:1390
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:880
LLVM_ABI APInt byteSwap() const
Definition APInt.cpp:768
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1264
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:307
void clearBits(unsigned LoBit, unsigned HiBit)
Clear the bits from LoBit (inclusive) to HiBit (exclusive) to 0.
Definition APInt.h:1440
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1411
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:483
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1244
bool isOne() const
Determine if this is a value of 1.
Definition APInt.h:390
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition APInt.h:865
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:858
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1228
LLVM_ABI APInt ssub_sat(const APInt &RHS) const
Definition APInt.cpp:2080
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
unsigned getSrcAddressSpace() const
unsigned getDestAddressSpace() const
static Capacity get(size_t N)
Get the capacity of an array that can hold at least N elements.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
This is an SDNode representing atomic operations.
static LLVM_ABI BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, offset addresses.
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
BitVector & reset()
Reset all bits in the bitvector.
Definition BitVector.h:409
void resize(unsigned N, bool t=false)
Grow or shrink the bitvector.
Definition BitVector.h:355
void clear()
Removes all bits from the bitvector.
Definition BitVector.h:349
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:366
bool none() const
Returns true if none of the bits are set.
Definition BitVector.h:207
size_type size() const
Returns the number of bits in this bitvector.
Definition BitVector.h:178
const BlockAddress * getBlockAddress() const
The address of a basic block.
Definition Constants.h:1088
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A "pseudo-class" with methods for operating on BUILD_VECTORs.
LLVM_ABI bool getConstantRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &RawBitElements, BitVector &UndefElements) const
Extract the raw bit data from a build vector of Undef, Constant or ConstantFP node elements.
static LLVM_ABI void recastRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &DstBitElements, ArrayRef< APInt > SrcBitElements, BitVector &DstUndefElements, const BitVector &SrcUndefElements)
Recast bit data SrcBitElements to DstEltSizeInBits wide elements.
LLVM_ABI bool getRepeatedSequence(const APInt &DemandedElts, SmallVectorImpl< SDValue > &Sequence, BitVector *UndefElements=nullptr) const
Find the shortest repeating sequence of values in the build vector.
LLVM_ABI ConstantFPSDNode * getConstantFPSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant FP or null if this is not a constant FP splat.
LLVM_ABI SDValue getSplatValue(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted value or a null value if this is not a splat.
LLVM_ABI bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef, unsigned &SplatBitSize, bool &HasAnyUndefs, unsigned MinSplatBits=0, bool isBigEndian=false) const
Check if this is a constant splat, and if so, find the smallest element size that splats the vector.
LLVM_ABI ConstantSDNode * getConstantSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant or null if this is not a constant splat.
LLVM_ABI int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements, uint32_t BitWidth) const
If this is a constant FP splat and the splatted constant FP is an exact power or 2,...
LLVM_ABI std::optional< std::pair< APInt, APInt > > isArithmeticSequence() const
If this BuildVector is constant and represents an arithmetic sequence "<a, a+n, a+2n,...
LLVM_ABI bool isConstant() const
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
static LLVM_ABI bool isValueValidForType(EVT VT, const APFloat &Val)
const APFloat & getValueAPF() const
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
const APFloat & getValue() const
Definition Constants.h:464
This is the shared class of boolean and integer constants.
Definition Constants.h:87
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition Constants.h:162
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
MachineConstantPoolValue * getMachineCPVal() const
const Constant * getConstVal() const
LLVM_ABI Type * getType() const
This class represents a range of values.
PreferredRangeType
If represented precisely, the result of some range operations may consist of multiple disjoint ranges...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI ConstantRange multiply(const ConstantRange &Other, unsigned NoWrapKind=0) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
LLVM_ABI APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
uint64_t getZExtValue() const
const APInt & getAPIntValue() const
This is an important base class in LLVM.
Definition Constant.h:43
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
DWARF expression.
static LLVM_ABI ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed)
Returns the ops for a zero- or sign-extension in a DIExpression.
static LLVM_ABI void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static LLVM_ABI const DIExpression * convertToVariadicExpression(const DIExpression *Expr)
If Expr is a non-variadic expression (i.e.
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
Base class for variables.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:217
LLVM_ABI IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
LLVM_ABI Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
LLVM_ABI Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
A debug info location.
Definition DebugLoc.h:126
Implements a dense probed hash-table based set.
Definition DenseSet.h:289
const char * getSymbol() const
This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:208
Data structure describing the variable locations in a function.
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:685
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:328
LLVM_ABI unsigned getAddressSpace() const
const GlobalValue * getGlobal() const
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
unsigned getAddressSpace() const
Module * getParent()
Get the module that this global value is contained inside of...
PointerType * getType() const
Global values are always pointers.
This class is used to form a handle around another node that is persistent and is updated across invo...
const SDValue & getValue() const
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Tracks which library functions to use for a particular subtarget.
CallingConv::ID getLibcallImplCallingConv(RTLIB::LibcallImpl Call) const
Get the CallingConv that should be used for the specified libcall.
RTLIB::LibcallImpl getLibcallImpl(RTLIB::Libcall Call) const
Return the lowering's selection of implementation call for Call.
This SDNode is used for LIFETIME_START/LIFETIME_END values.
This class is used to represent ISD::LOAD nodes.
static LocationSize precise(uint64_t Value)
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Metadata node.
Definition Metadata.h:1069
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1426
Machine Value Type.
SimpleValueType SimpleTy
static MVT getIntegerVT(unsigned BitWidth)
Abstract base class for all machine specific constantpool value subclasses.
virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID)=0
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
void setObjectAlignment(int ObjectIdx, Align Alignment)
setObjectAlignment - Change the alignment of the specified stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
const MDNode * getRanges() const
Return the range tag for the memory reference.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
const MachinePointerInfo & getPointerInfo() const
Flags getFlags() const
Return the raw flags of the source value,.
This class contains meta information specific to a module.
An SDNode that represents everything that will be needed to construct a MachineInstr.
This class is used to represent an MGATHER node.
This class is used to represent an MLOAD node.
This class is used to represent an MSCATTER node.
This class is used to represent an MSTORE node.
This SDNode is used for target intrinsics that touch memory and need an associated MachineMemOperand.
size_t getNumMemOperands() const
Return the number of memory operands.
LLVM_ABI MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt, PointerUnion< MachineMemOperand *, MachineMemOperand ** > memrefs)
Constructor that supports single or multiple MMOs.
PointerUnion< MachineMemOperand *, MachineMemOperand ** > MemRefs
Memory reference information.
MachineMemOperand * getMemOperand() const
Return the unique MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
ArrayRef< MachineMemOperand * > memoperands() const
Return the memory operands for this node.
unsigned getRawSubclassData() const
Return the SubclassData value, without HasDebugValue.
EVT getMemoryVT() const
Return the type of the in-memory value.
Representation for a specific memory location.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition Module.cpp:235
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
The optimization diagnostic interface.
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
Class to represent pointers.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
unsigned getAddressSpace() const
Return the address space of the Pointer type.
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
A discriminated union of two or more pointer types, with the discriminator in the low bits of the poi...
bool isNull() const
Test if the pointer held in the union is null, regardless of which type it is.
Analysis providing profile information.
void Deallocate(SubClass *E)
Deallocate - Release storage for the pointed-to object.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Keeps track of dbg_value information through SDISel.
LLVM_ABI void add(SDDbgValue *V, bool isParameter)
LLVM_ABI void erase(const SDNode *Node)
Invalidate all DbgValues attached to the node and remove it from the Node-to-DbgValues map.
Holds the information from a dbg_label node through SDISel.
Holds the information for a single machine location through SDISel; either an SDNode,...
static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo)
static SDDbgOperand fromFrameIdx(unsigned FrameIdx)
static SDDbgOperand fromVReg(Register VReg)
static SDDbgOperand fromConst(const Value *Const)
@ SDNODE
Value is the result of an expression.
Holds the information from a dbg_value node through SDISel.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
const DebugLoc & getDebugLoc() const
unsigned getIROrder() const
This class provides iterator support for SDUse operands that use a specific SDNode.
Represents one node in the SelectionDAG.
ArrayRef< SDUse > ops() const
const APInt & getAsAPIntVal() const
Helper method returns the APInt value of a ConstantSDNode.
LLVM_ABI void dumprFull(const SelectionDAG *G=nullptr) const
printrFull to dbgs().
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool isDivergent() const
LLVM_ABI bool isOnlyUserOf(const SDNode *N) const
Return true if this node is the only use of N.
iterator_range< value_op_iterator > op_values() const
unsigned getIROrder() const
Return the node ordering.
static constexpr size_t getMaxNumOperands()
Return the maximum number of operands that a SDNode can hold.
iterator_range< use_iterator > uses()
MemSDNodeBitfields MemSDNodeBits
LLVM_ABI void Profile(FoldingSetNodeID &ID) const
Gather unique data for the node.
bool getHasDebugValue() const
SDNodeFlags getFlags() const
void setNodeId(int Id)
Set unique node id.
LLVM_ABI void intersectFlagsWith(const SDNodeFlags Flags)
Clear any flags in this node that aren't also set in Flags.
static bool hasPredecessorHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallVectorImpl< const SDNode * > &Worklist, unsigned int MaxSteps=0, bool TopologicalPrune=false)
Returns true if N is a predecessor of any node in Worklist.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
bool use_empty() const
Return true if there are no uses of this node.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
unsigned getNumOperands() const
Return the number of values used by this operation.
const SDValue & getOperand(unsigned Num) const
static LLVM_ABI bool areOnlyUsersOf(ArrayRef< const SDNode * > Nodes, const SDNode *N)
Return true if all the users of N are contained in Nodes.
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
LLVM_ABI bool isOperandOf(const SDNode *N) const
Return true if this node is an operand of N.
const APInt & getConstantOperandAPInt(unsigned Num) const
Helper method returns the APInt of a ConstantSDNode operand.
std::optional< APInt > bitcastToAPInt() const
LLVM_ABI bool hasPredecessor(const SDNode *N) const
Return true if N is a predecessor of this node.
LLVM_ABI bool hasAnyUseOfValue(unsigned Value) const
Return true if there are any use of the indicated value.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
bool isUndef() const
Returns true if the node type is UNDEF or POISON.
op_iterator op_end() const
op_iterator op_begin() const
static use_iterator use_end()
LLVM_ABI void DropOperands()
Release the operands and set this node to have zero operands.
SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
Create an SDNode.
Represents a use of a SDNode.
SDNode * getUser()
This returns the SDNode that contains this Use.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
LLVM_ABI bool isOperandOf(const SDNode *N) const
Return true if the referenced return value is an operand of N.
SDValue()=default
LLVM_ABI bool reachesChainWithoutSideEffects(SDValue Dest, unsigned Depth=2) const
Return true if this operand (which must be a chain) reaches the specified operand without crossing an...
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
bool use_empty() const
Return true if there are no nodes using value ResNo of Node.
const APInt & getConstantOperandAPInt(unsigned i) const
uint64_t getScalarValueSizeInBits() const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
uint64_t getConstantOperandVal(unsigned i) const
unsigned getOpcode() const
virtual void verifyTargetNode(const SelectionDAG &DAG, const SDNode *N) const
Checks that the given target-specific node is valid. Aborts if it is not.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
LLVM_ABI SDValue getElementCount(const SDLoc &DL, EVT VT, ElementCount EC)
LLVM_ABI Align getReducedAlign(EVT VT, bool UseABI)
In most cases this function returns the ABI alignment for a given type, except for illegal vector typ...
LLVM_ABI SDValue getVPZeroExtendInReg(SDValue Op, SDValue Mask, SDValue EVL, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
LLVM_ABI SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op)
Return the specified value casted to the target's desired shift amount type.
LLVM_ABI SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI std::pair< SDValue, SDValue > getMemccpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue C, SDValue Size, const CallInst *CI)
Lower a memccpy operation into a target library call and return the resulting chain and call result a...
LLVM_ABI bool isKnownNeverLogicalZero(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
Test whether the given floating point SDValue (or all elements of it, if it is a vector) is known to ...
LLVM_ABI SDValue getExtLoadVP(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsExpanding=false)
SDValue getExtractVectorElt(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Extract element at Idx from Vec.
LLVM_ABI SDValue getSplatSourceVector(SDValue V, int &SplatIndex)
If V is a splatted value, return the source vector and its splat index.
LLVM_ABI SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI OverflowKind computeOverflowForUnsignedSub(SDValue N0, SDValue N1) const
Determine if the result of the unsigned sub of 2 nodes can overflow.
LLVM_ABI unsigned ComputeMaxSignificantBits(SDValue Op, unsigned Depth=0) const
Get the upper bound on bit size for this Value Op as a signed integer.
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
LLVM_ABI std::pair< SDValue, SDValue > getStrlen(SDValue Chain, const SDLoc &dl, SDValue Src, const CallInst *CI)
Lower a strlen operation into a target library call and return the resulting chain and call result as...
LLVM_ABI SDValue getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy)
LLVM_ABI SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS)
Return an AddrSpaceCastSDNode.
LLVM_ABI SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond, const SDLoc &dl, SDNodeFlags Flags={})
Constant fold a setcc to true or false.
bool isKnownNeverSNaN(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
LLVM_ABI std::optional< bool > isBoolConstant(SDValue N) const
Check if a value \op N is a constant using the target's BooleanContent for its type.
LLVM_ABI SDValue getStackArgumentTokenFactor(SDValue Chain)
Compute a TokenFactor to force all the incoming stack arguments to be loaded from the stack.
const TargetSubtargetInfo & getSubtarget() const
LLVM_ABI ConstantRange computeConstantRange(SDValue Op, bool ForSigned, unsigned Depth=0) const
Determine the possible constant range of an integer or vector of integers.
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
LLVM_ABI void updateDivergence(SDNode *N)
LLVM_ABI SDValue getSplatValue(SDValue V, bool LegalTypes=false)
If V is a splat vector, return its scalar source operand by extracting that element from the source v...
LLVM_ABI SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
LLVM_ABI SDValue getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Value, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo)
LLVM_ABI SDValue getAtomicLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT MemVT, EVT VT, SDValue Chain, SDValue Ptr, MachineMemOperand *MMO)
LLVM_ABI SDNode * getNodeIfExists(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops, const SDNodeFlags Flags, bool AllowCommute=false)
Get the specified node if it's already available, or else return NULL.
LLVM_ABI SDValue getPseudoProbeNode(const SDLoc &Dl, SDValue Chain, uint64_t Guid, uint64_t Index, uint32_t Attr)
Creates a PseudoProbeSDNode with function GUID Guid and the index of the block Index it is probing,...
LLVM_ABI SDValue getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
LLVM_ABI SDNode * SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT)
These are used for target selectors to mutate the specified node to have the specified return type,...
LLVM_ABI void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE, Pass *PassPtr, const TargetLibraryInfo *LibraryInfo, const LibcallLoweringInfo *LibcallsInfo, UniformityInfo *UA, ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin, MachineModuleInfo &MMI, FunctionVarLocs const *FnVarLocs)
Prepare this SelectionDAG to process code in the given MachineFunction.
LLVM_ABI SelectionDAG(const TargetMachine &TM, CodeGenOptLevel)
LLVM_ABI SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getBitcastedSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getStridedLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDVTList VTs, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp, MachineMemOperand *MMO)
Gets a node for an atomic cmpxchg op.
LLVM_ABI SDValue makeEquivalentMemoryOrdering(SDValue OldChain, SDValue NewMemOpChain)
If an existing load has uses of its chain, create a token factor node with that chain and the new mem...
LLVM_ABI bool isConstantIntBuildVectorOrConstantInt(SDValue N, bool AllowOpaques=true) const
Test whether the given value is a constant int or similar node.
LLVM_ABI void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To, unsigned Num)
Like ReplaceAllUsesOfValueWith, but for multiple values at once.
LLVM_ABI SDValue getJumpTableDebugInfo(int JTI, SDValue Chain, const SDLoc &DL)
LLVM_ABI SDValue getSymbolFunctionGlobalAddress(SDValue Op, Function **TargetFunction=nullptr)
Return a GlobalAddress of the function from the current module with name matching the given ExternalS...
LLVM_ABI std::optional< unsigned > getValidMaximumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
LLVM_ABI SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm)
Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
OverflowKind
Used to represent the possible overflow behavior of an operation.
static LLVM_ABI unsigned getHasPredecessorMaxSteps()
LLVM_ABI bool haveNoCommonBitsSet(SDValue A, SDValue B) const
Return true if A and B have no common bits set.
SDValue getExtractSubvector(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Return the VT typed sub-vector of Vec at Idx.
LLVM_ABI bool cannotBeOrderedNegativeFP(SDValue Op) const
Test whether the given float value is known to be positive.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI bool calculateDivergence(SDNode *N)
LLVM_ABI std::pair< SDValue, SDValue > getStrcmp(SDValue Chain, const SDLoc &dl, SDValue S0, SDValue S1, const CallInst *CI)
Lower a strcmp operation into a target library call and return the resulting chain and call result as...
LLVM_ABI SDValue getGetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A)
Return an AssertAlignSDNode.
LLVM_ABI SDNode * mutateStrictFPToFP(SDNode *Node)
Mutate the specified strict FP node to its non-strict equivalent, unlinking the node from its chain a...
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
LLVM_ABI bool canIgnoreSignBitOfZero(const SDUse &Use) const
Check if a use of a float value is insensitive to signed zeros.
LLVM_ABI bool SignBitIsZeroFP(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero, for a floating-point value.
LLVM_ABI SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef< SDValue > Ops, EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags Flags=MachineMemOperand::MOLoad|MachineMemOperand::MOStore, LocationSize Size=LocationSize::precise(0), const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
SDValue getInsertSubvector(const SDLoc &DL, SDValue Vec, SDValue SubVec, unsigned Idx)
Insert SubVec at the Idx element of Vec.
LLVM_ABI SDValue getBitcastedZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI SDValue getStepVector(const SDLoc &DL, EVT ResVT, const APInt &StepVal)
Returns a vector of type ResVT whose elements contain the linear sequence <0, Step,...
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false, SDNodeFlags Flags={})
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
LLVM_ABI SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Val, MachineMemOperand *MMO)
Gets a node for an atomic op, produces result (if relevant) and chain and takes 2 operands.
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
LLVM_ABI bool shouldOptForSize() const
LLVM_ABI SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
LLVM_ABI SDValue getVPZExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be an integer vector, to the vector-type VT,...
LLVM_ABI SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align DstAlign, Align SrcAlign, bool isVol, bool AlwaysInline, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
const TargetLowering & getTargetLoweringInfo() const
LLVM_ABI bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
LLVM_ABI SDValue getStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
bool isGuaranteedNotToBePoison(SDValue Op, unsigned Depth=0) const
Return true if this function can prove that Op is never poison.
LLVM_ABI SDValue getIdentityElement(unsigned Opcode, const SDLoc &DL, EVT VT, SDNodeFlags Flags)
Get the (commutative) identity element for the given opcode, if it exists.
LLVM_ABI SDValue expandVACopy(SDNode *Node)
Expand the specified ISD::VACOPY node as the Legalize pass would.
LLVM_ABI SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI APInt computeVectorKnownZeroElements(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
For each demanded element of a vector, see if it is known to be zero.
LLVM_ABI void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
bool NewNodesMustHaveLegalTypes
When true, additional steps are taken to ensure that getConstant() and similar functions return DAG n...
LLVM_ABI std::pair< EVT, EVT > GetSplitDestVTs(const EVT &VT) const
Compute the VTs needed for the low/hi parts of a type which is split (or expanded) into two not neces...
LLVM_ABI void salvageDebugInfo(SDNode &N)
To be invoked on an SDNode that is slated to be erased.
LLVM_ABI SDNode * MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs, ArrayRef< SDValue > Ops)
This mutates the specified node to have the specified return type, opcode, and operands.
LLVM_ABI std::pair< SDValue, SDValue > UnrollVectorOverflowOp(SDNode *N, unsigned ResNE=0)
Like UnrollVectorOp(), but for the [US](ADD|SUB|MUL)O family of opcodes.
allnodes_const_iterator allnodes_begin() const
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
LLVM_ABI SDValue getGatherVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI SDValue getBitcastedAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts, unsigned Depth=0) const
Test whether V has a splatted value for all the demanded elements.
LLVM_ABI void DeleteNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
LLVM_ABI SDDbgValue * getDbgValueList(DIVariable *Var, DIExpression *Expr, ArrayRef< SDDbgOperand > Locs, ArrayRef< SDNode * > Dependencies, bool IsIndirect, const DebugLoc &DL, unsigned O, bool IsVariadic)
Creates a SDDbgValue node from a list of locations.
LLVM_ABI std::pair< SDValue, SDValue > getStrcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, const CallInst *CI)
Lower a strcpy operation into a target library call and return the resulting chain and call result as...
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build Select's if you just have operands and don't want to check...
LLVM_ABI SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT)
Create negative operation as (SUB 0, Val).
LLVM_ABI std::optional< unsigned > getValidShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has a uniform shift amount that is less than the element bit-width of the shi...
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
LLVM_ABI SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal)
Try to simplify a select/vselect into 1 of its operands or a constant.
LLVM_ABI SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
LLVM_ABI bool isConstantFPBuildVectorOrConstantFP(SDValue N) const
Test whether the given value is a constant FP or similar node.
const DataLayout & getDataLayout() const
LLVM_ABI SDValue getPartialReduceMLS(unsigned Opc, const SDLoc &DL, SDValue Acc, SDValue LHS, SDValue RHS)
Get an expression that implements a partial multiply-subtract reduction.
LLVM_ABI SDValue expandVAArg(SDNode *Node)
Expand the specified ISD::VAARG node as the Legalize pass would.
LLVM_ABI SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
LLVM_ABI bool doesNodeExist(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops)
Check if a node exists without modifying its flags.
LLVM_ABI ConstantRange computeConstantRangeIncludingKnownBits(SDValue Op, bool ForSigned, unsigned Depth=0) const
Combine constant ranges from computeConstantRange() and computeKnownBits().
const SelectionDAGTargetInfo & getSelectionDAGInfo() const
LLVM_ABI bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base, unsigned Bytes, int Dist) const
Return true if loads are next to each other and can be merged.
LLVM_ABI SDValue getMaskedHistogram(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDDbgLabel * getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O)
Creates a SDDbgLabel node.
LLVM_ABI SDValue getStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI OverflowKind computeOverflowForUnsignedMul(SDValue N0, SDValue N1) const
Determine if the result of the unsigned mul of 2 nodes can overflow.
LLVM_ABI void copyExtraInfo(SDNode *From, SDNode *To)
Copy extra info associated with one node to another.
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
LLVM_ABI SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue SV, unsigned Align)
VAArg produces a result and token chain, and takes a pointer and a source value as input.
LLVM_ABI SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getLoadFFVP(EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachineMemOperand *MMO)
LLVM_ABI SDValue getTypeSize(const SDLoc &DL, EVT VT, TypeSize TS)
LLVM_ABI SDValue getMDNode(const MDNode *MD)
Return an MDNodeSDNode which holds an MDNode.
LLVM_ABI void clear()
Clear state and free memory necessary to make this SelectionDAG ready to process a new block.
LLVM_ABI std::pair< SDValue, SDValue > getMemcmp(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, const CallInst *CI)
Lower a memcmp operation into a target library call and return the resulting chain and call result as...
LLVM_ABI void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
LLVM_ABI SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV)
Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to the shuffle node in input but with swa...
LLVM_ABI std::pair< SDValue, SDValue > SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the vector with EXTRACT_SUBVECTOR using the provided VTs and return the low/high part.
LLVM_ABI SDValue makeStateFunctionCall(unsigned LibFunc, SDValue Ptr, SDValue InChain, const SDLoc &DLoc)
Helper used to make a call to a library function that has one argument of pointer type.
LLVM_ABI SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
LLVM_ABI SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
LLVM_ABI SDValue getIndexedLoadVP(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getSrcValue(const Value *v)
Construct a node to track a Value* through the backend.
SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op)
LLVM_ABI SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI OverflowKind computeOverflowForSignedMul(SDValue N0, SDValue N1) const
Determine if the result of the signed mul of 2 nodes can overflow.
LLVM_ABI MaybeAlign InferPtrAlign(SDValue Ptr) const
Infer alignment of a load / store address.
LLVM_ABI void dump() const
Dump the textual format of this DAG.
LLVM_ABI bool MaskedValueIsAllOnes(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if '(Op & Mask) == Mask'.
LLVM_ABI bool SignBitIsZero(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero.
LLVM_ABI void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
LLVM_ABI void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI void AddDbgLabel(SDDbgLabel *DB)
Add a dbg_label SDNode.
bool isConstantValueOfAnyType(SDValue N) const
LLVM_ABI bool canCreateUndefOrPoison(SDValue Op, const APInt &DemandedElts, UndefPoisonKind Kind=UndefPoisonKind::UndefOrPoison, bool ConsiderFlags=true, unsigned Depth=0) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
LLVM_ABI SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
LLVM_ABI SDDbgValue * getVRegDbgValue(DIVariable *Var, DIExpression *Expr, Register VReg, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a VReg SDDbgValue node.
LLVM_ABI KnownFPClass computeKnownFPClass(SDValue Op, FPClassTest InterestedClasses, unsigned Depth=0) const
Determine floating-point class information about Op.
LLVM_ABI bool isIdentityElement(unsigned Opc, SDNodeFlags Flags, SDValue V, unsigned OperandNo, unsigned Depth=0) const
Returns true if V is an identity element of Opc with Flags.
LLVM_ABI SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI SDValue getIndexedStoreVP(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(SDValue Op, UndefPoisonKind Kind=UndefPoisonKind::UndefOrPoison, unsigned Depth=0) const
Return true if this function can prove that Op is never poison and, Kind can be used to track poison ...
LLVM_ABI bool isKnownNeverZero(SDValue Op, unsigned Depth=0) const
Test whether the given SDValue is known to contain non-zero value(s).
LLVM_ABI SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SDNodeFlags Flags=SDNodeFlags())
LLVM_ABI std::optional< unsigned > getValidMinimumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue getSetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT)
Convert Op, which must be of integer type, to the integer type VT, by using an extension appropriate ...
LLVM_ABI SDValue getMaskedStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Base, SDValue Offset, SDValue Mask, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
LLVM_ABI std::pair< SDValue, SDValue > getStrictFPExtendOrRound(SDValue Op, SDValue Chain, const SDLoc &DL, EVT VT)
Convert Op, which must be a STRICT operation of float type, to the float type VT, by either extending...
LLVM_ABI std::pair< SDValue, SDValue > SplitEVL(SDValue N, EVT VecVT, const SDLoc &DL)
Split the explicit vector length parameter of a VP operation.
LLVM_ABI SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either truncating it or perform...
LLVM_ABI SDValue getVPLogicalNOT(const SDLoc &DL, SDValue Val, SDValue Mask, SDValue EVL, EVT VT)
Create a vector-predicated logical NOT operation as (VP_XOR Val, BooleanOne, Mask,...
LLVM_ABI SDValue getMaskFromElementCount(const SDLoc &DL, EVT VT, ElementCount Len)
Return a vector with the first 'Len' lanes set to true and remaining lanes set to false.
LLVM_ABI SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
iterator_range< allnodes_iterator > allnodes()
LLVM_ABI SDValue getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI SDValue WidenVector(const SDValue &N, const SDLoc &DL)
Widen the vector up to the next power of two using INSERT_SUBVECTOR.
const LibcallLoweringInfo & getLibcalls() const
LLVM_ABI SDValue getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, const MDNode *Ranges=nullptr, bool IsExpanding=false)
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDDbgValue * getConstantDbgValue(DIVariable *Var, DIExpression *Expr, const Value *C, const DebugLoc &DL, unsigned O)
Creates a constant SDDbgValue node.
LLVM_ABI SDValue getScatterVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain, int FrameIndex)
Creates a LifetimeSDNode that starts (IsStart==true) or ends (IsStart==false) the lifetime of the Fra...
ArrayRef< SDDbgValue * > GetDbgValues(const SDNode *SD) const
Get the debug values which reference the given SDNode.
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI OverflowKind computeOverflowForSignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the signed addition of 2 nodes can overflow.
LLVM_ABI SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
LLVM_ABI unsigned AssignTopologicalOrder()
Topological-sort the AllNodes list and a assign a unique node id for each node in the DAG based on th...
ilist< SDNode >::size_type allnodes_size() const
LLVM_ABI bool isKnownNeverNaN(SDValue Op, const APInt &DemandedElts, bool SNaN=false, unsigned Depth=0) const
Test whether the given SDValue (or all elements of it, if it is a vector) is known to never be NaN in...
LLVM_ABI SDValue FoldConstantBuildVector(BuildVectorSDNode *BV, const SDLoc &DL, EVT DstEltVT)
Fold BUILD_VECTOR of constants/undefs to the destination type BUILD_VECTOR of constants/undefs elemen...
LLVM_ABI SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI SDValue getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getTruncStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsCompressing=false)
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI unsigned ComputeNumSignBits(SDValue Op, unsigned Depth=0) const
Return the number of times the sign bit of the register is replicated into the other bits.
LLVM_ABI bool MaskedVectorIsZero(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
Return true if 'Op' is known to be zero in DemandedElts.
LLVM_ABI SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT)
Create a true or false constant of type VT using the target's BooleanContent for type OpVT.
LLVM_ABI SDDbgValue * getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr, unsigned FI, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a FrameIndex SDDbgValue node.
LLVM_ABI SDValue getExtStridedLoadVP(ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align DstAlign, Align SrcAlign, bool isVol, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI SDValue getJumpTable(int JTI, EVT VT, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side,...
LLVM_ABI SDValue getVPPtrExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be of integer type, to the vector-type integer type VT,...
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI void getTopologicallyOrderedNodes(SmallVectorImpl< const SDNode * > &SortedNodes) const
Get all the nodes in their topological order without modifying any states.
LLVM_ABI void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.getNode() alone.
MachineFunction & getMachineFunction() const
LLVM_ABI std::pair< SDValue, SDValue > getStrstr(SDValue Chain, const SDLoc &dl, SDValue S0, SDValue S1, const CallInst *CI)
Lower a strstr operation into a target library call and return the resulting chain and call result as...
LLVM_ABI SDValue getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to extend the Op as a pointer value assuming it was the smaller SrcTy ...
LLVM_ABI OverflowKind computeOverflowForUnsignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the unsigned addition of 2 nodes can overflow.
SDValue getPOISON(EVT VT)
Return a POISON node. POISON does not have a useful SDLoc.
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI SDValue getTruncStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT SVT, MachineMemOperand *MMO, bool IsCompressing=false)
LLVM_ABI void canonicalizeCommutativeBinop(unsigned Opcode, SDValue &N1, SDValue &N2) const
Swap N1 and N2 if Opcode is a commutative binary opcode and the canonical form expects the opposite o...
LLVM_ABI KnownBits computeKnownBits(SDValue Op, unsigned Depth=0) const
Determine which bits of Op are known to be either zero or one and return them in Known.
LLVM_ABI SDValue getRegisterMask(const uint32_t *RegMask)
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVM_ABI SDValue getCondCode(ISD::CondCode Cond)
LLVM_ABI bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
LLVM_ABI bool isKnownToBeAPowerOfTwoFP(SDValue Val, unsigned Depth=0) const
Test if the given fp value is known to be an integer power-of-2, either positive or negative.
LLVM_ABI OverflowKind computeOverflowForSignedSub(SDValue N0, SDValue N1) const
Determine if the result of the signed sub of 2 nodes can overflow.
SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, TypeSize Offset)
Create an add instruction with appropriate flags when used for addressing some offset of an object.
LLVMContext * getContext() const
LLVM_ABI SDValue simplifyFPBinop(unsigned Opcode, SDValue X, SDValue Y, SDNodeFlags Flags)
Try to simplify a floating-point binary operation into 1 of its operands or a constant.
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
LLVM_ABI bool isKnownToBeAPowerOfTwo(SDValue Val, bool OrZero=false, unsigned Depth=0) const
Test if the given value is known to have exactly one bit set.
LLVM_ABI SDValue getDeactivationSymbol(const GlobalValue *GV)
LLVM_ABI SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
LLVM_ABI SDValue getMCSymbol(MCSymbol *Sym, EVT VT)
LLVM_ABI bool isUndef(unsigned Opcode, ArrayRef< SDValue > Ops)
Return true if the result of this operation is always undefined.
LLVM_ABI SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
LLVM_ABI SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
LLVM_ABI std::pair< EVT, EVT > GetDependentSplitDestVTs(const EVT &VT, const EVT &EnvVT, bool *HiIsEmpty) const
Compute the VTs needed for the low/hi parts of a type, dependent on an enveloping VT that has been sp...
LLVM_ABI SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
Fold floating-point operations when all operands are constants and/or undefined.
LLVM_ABI std::optional< ConstantRange > getValidShiftAmountRange(SDValue V, const APInt &DemandedElts, unsigned Depth) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue FoldSymbolOffset(unsigned Opcode, EVT VT, const GlobalAddressSDNode *GA, const SDNode *N2)
LLVM_ABI SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand, SDValue Subreg)
A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
LLVM_ABI SDDbgValue * getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N, unsigned R, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a SDDbgValue node.
LLVM_ABI SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Base, SDValue Offset, SDValue Mask, SDValue Src0, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, ISD::LoadExtType, bool IsExpanding=false)
DenormalMode getDenormalMode(EVT VT) const
Return the current function's default denormal handling kind for the given floating point type.
SDValue getSplat(EVT VT, const SDLoc &DL, SDValue Op)
Returns a node representing a splat of one value into all lanes of the provided vector type.
LLVM_ABI std::pair< SDValue, SDValue > SplitScalar(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the scalar node with EXTRACT_ELEMENT using the provided VTs and return the low/high part.
LLVM_ABI SDValue matchBinOpReduction(SDNode *Extract, ISD::NodeType &BinOp, ArrayRef< ISD::NodeType > CandidateBinOps, bool AllowPartials=false)
Match a binop + shuffle pyramid that represents a horizontal reduction over the elements of a vector ...
LLVM_ABI bool isADDLike(SDValue Op, bool NoWrap=false) const
Return true if the specified operand is an ISD::OR or ISD::XOR node that can be treated as an ISD::AD...
LLVM_ABI SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
LLVM_ABI SDValue simplifyShift(SDValue X, SDValue Y)
Try to simplify a shift into 1 of its operands or a constant.
LLVM_ABI void transferDbgValues(SDValue From, SDValue To, unsigned OffsetInBits=0, unsigned SizeInBits=0, bool InvalidateDbg=true)
Transfer debug values from one node to another, while optionally generating fragment expressions for ...
LLVM_ABI SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a logical NOT operation as (XOR Val, BooleanOne).
LLVM_ABI SDValue getMaskedScatter(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, bool IsTruncating=false)
ilist< SDNode >::iterator allnodes_iterator
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
int getMaskElt(unsigned Idx) const
ArrayRef< int > getMask() const
static void commuteMask(MutableArrayRef< int > Mask)
Change values in a shuffle permute mask assuming the two vector operands have swapped position.
static LLVM_ABI bool isSplatMask(ArrayRef< int > Mask)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class is used to represent ISD::STORE nodes.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:138
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
Completely target-dependent object reference.
unsigned getTargetFlags() const
Provides information about what library functions are available for the current target.
virtual bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const
Return true if it is beneficial to convert a load of a constant to just the constant itself.
const TargetMachine & getTargetMachine() const
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
unsigned getMaxStoresPerMemcpy(bool OptSize) const
Get maximum # of store operations permitted for llvm.memcpy.
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
virtual bool allowsMisalignedMemoryAccesses(EVT, unsigned AddrSpace=0, Align Alignment=Align(1), MachineMemOperand::Flags Flags=MachineMemOperand::MONone, unsigned *=nullptr) const
Determine if the target supports unaligned memory accesses.
virtual bool shallExtractConstSplatVectorElementToStore(Type *VectorTy, unsigned ElemSizeInBits, unsigned &Index) const
Return true if the target shall perform extract vector element and store given that the vector is kno...
virtual bool isTruncateFree(Type *FromTy, Type *ToTy) const
Return true if it's free to truncate a value of type FromTy to type ToTy.
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
BooleanContent
Enum that describes how the target represents true/false values.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
std::vector< ArgListEntry > ArgListTy
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
virtual bool isLegalStoreImmediate(int64_t Value) const
Return true if the specified immediate is legal for the value input of a store instruction.
static ISD::NodeType getExtendForContent(BooleanContent Content)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual bool findOptimalMemOpLowering(LLVMContext &Context, std::vector< EVT > &MemOps, unsigned Limit, const MemOp &Op, unsigned DstAS, unsigned SrcAS, const AttributeList &FuncAttributes, EVT *LargestVT=nullptr) 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.
Primary interface to the complete machine description for the target machine.
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const SelectionDAGTargetInfo * getSelectionDAGInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
virtual const TargetLowering * getTargetLowering() const
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, DriverKit, XROS, or bridgeOS).
Definition Triple.h:634
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:288
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:309
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:282
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:307
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:232
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:36
LLVM_ABI void set(Value *Val)
Definition Value.h:874
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
Value * getOperand(unsigned i) const
Definition User.h:207
This class is used to represent an VP_GATHER node.
This class is used to represent a VP_LOAD node.
This class is used to represent an VP_SCATTER node.
This class is used to represent a VP_STORE node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_LOAD node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_STORE node.
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:212
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:185
constexpr bool hasKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns true if there exists a value X where RHS.multiplyCoefficientBy(X) will result in a value whos...
Definition TypeSize.h:269
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:230
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr bool isKnownEven() const
A return value of true indicates we know at compile time that the number of elements (vscale * Min) i...
Definition TypeSize.h:176
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
constexpr LeafTy divideCoefficientBy(ScalarTy RHS) const
We do not provide the '/' operator here because division for polynomial types does not work in the sa...
Definition TypeSize.h:252
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:237
A raw_ostream that writes to an std::string.
CallInst * Call
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt clmulr(const APInt &LHS, const APInt &RHS)
Perform a reversed carry-less multiply.
Definition APInt.cpp:3232
LLVM_ABI APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition APInt.cpp:3162
LLVM_ABI APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition APInt.cpp:3149
LLVM_ABI APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition APInt.cpp:3139
LLVM_ABI APInt pext(const APInt &Val, const APInt &Mask)
Perform a "compress" operation, also known as pext or bext.
Definition APInt.cpp:3242
LLVM_ABI APInt fshr(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift right.
Definition APInt.cpp:3213
LLVM_ABI APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition APInt.cpp:3154
LLVM_ABI APInt clmul(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, also known as XOR multiplication, and return low-bits.
Definition APInt.cpp:3222
LLVM_ABI APInt pdep(const APInt &Val, const APInt &Mask)
Perform an "expand" operation, also known as pdep or bdep.
Definition APInt.cpp:3252
APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition APInt.h:2297
LLVM_ABI APInt fshl(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift left.
Definition APInt.cpp:3204
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3040
LLVM_ABI APInt clmulh(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, and return high-bits.
Definition APInt.cpp:3237
APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition APInt.h:2302
LLVM_ABI APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition APInt.cpp:3134
LLVM_ABI APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition APInt.cpp:3144
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ Fast
Attempts to make calls as fast as possible (e.g.
Definition CallingConv.h:41
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, bool isIntegerLike)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
ISD namespace - This namespace contains an enum which represents all of the SelectionDAG node types a...
Definition ISDOpcodes.h:24
LLVM_ABI CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical AND between different comparisons of identical values: ((X op1 Y) & (X...
LLVM_ABI bool isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are ~0 ...
bool isNON_EXTLoad(const SDNode *N)
Returns true if the specified node is a non-extending load.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:827
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:261
@ TargetConstantPool
Definition ISDOpcodes.h:189
@ MDNODE_SDNODE
MDNODE_SDNODE - This is a node that holdes an MDNode*, which is used to reference metadata in the IR.
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:511
@ PTRADD
PTRADD represents pointer arithmetic semantics, for targets that opt in using shouldPreservePtrArith(...
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition ISDOpcodes.h:45
@ POISON
POISON - A poison node.
Definition ISDOpcodes.h:236
@ PARTIAL_REDUCE_SMLA
PARTIAL_REDUCE_[U|S]MLA(Accumulator, Input1, Input2) The partial reduction nodes sign or zero extend ...
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
@ MLOAD
Masked load and store - consecutive vector load and store operations with additional mask operand tha...
@ FGETSIGN
INT = FGETSIGN(FP) - Return the sign bit of the specified floating point value as an integer 0/1 valu...
Definition ISDOpcodes.h:538
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:275
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:600
@ JUMP_TABLE_DEBUG_INFO
JUMP_TABLE_DEBUG_INFO - Jumptable debug info.
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:787
@ TargetBlockAddress
Definition ISDOpcodes.h:191
@ DEACTIVATION_SYMBOL
Untyped node storing deactivation symbol reference (DeactivationSymbolSDNode).
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:294
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition ISDOpcodes.h:522
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:264
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:861
@ ATOMIC_LOAD_USUB_COND
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:518
@ FATAN2
FATAN2 - atan2, inspired by libm.
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:220
@ GlobalAddress
Definition ISDOpcodes.h:88
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:888
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:584
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:417
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:747
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition ISDOpcodes.h:918
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
@ FMULADD
FMULADD - Performs a * b + c, with, or without, intermediate rounding.
Definition ISDOpcodes.h:528
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:254
@ CLMUL
Carry-less multiplication operations.
Definition ISDOpcodes.h:778
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
@ GlobalTLSAddress
Definition ISDOpcodes.h:89
@ SRCVALUE
SRCVALUE - This is a node type that holds a Value* that is used to make reference to a value in the L...
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
@ ATOMIC_LOAD_USUB_SAT
@ CTLZ_ZERO_POISON
Definition ISDOpcodes.h:796
@ PARTIAL_REDUCE_UMLA
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:852
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition ISDOpcodes.h:715
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:665
@ TargetExternalSymbol
Definition ISDOpcodes.h:190
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
@ TargetJumpTable
Definition ISDOpcodes.h:188
@ TargetIndex
TargetIndex - Like a constant pool entry, but with completely target-dependent semantics.
Definition ISDOpcodes.h:198
@ PARTIAL_REDUCE_FMLA
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
@ TRUNCATE_SSAT_U
Definition ISDOpcodes.h:881
@ 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:835
@ FNEG
Perform various unary floating-point operations inspired by libm.
@ BR_CC
BR_CC - Conditional branch.
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:352
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition ISDOpcodes.h:691
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:541
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:548
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:374
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:804
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:233
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:247
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:672
@ AssertAlign
AssertAlign - These nodes record if a register contains a value that has a known alignment and the tr...
Definition ISDOpcodes.h:69
@ GET_ACTIVE_LANE_MASK
GET_ACTIVE_LANE_MASK - this corrosponds to the llvm.get.active.lane.mask intrinsic.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:230
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:348
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition ISDOpcodes.h:185
@ ARITH_FENCE
ARITH_FENCE - This corresponds to a arithmetic fence intrinsic.
@ CTLS
Count leading redundant sign bits.
Definition ISDOpcodes.h:800
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:704
@ ATOMIC_LOAD_FMAXIMUM
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:769
@ AssertNoFPClass
AssertNoFPClass - These nodes record if a register contains a float value that is known to be not som...
Definition ISDOpcodes.h:78
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:649
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:614
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition ISDOpcodes.h:48
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:576
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition ISDOpcodes.h:224
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:858
@ TargetConstantFP
Definition ISDOpcodes.h:180
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:819
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum maximum on two values, following IEEE-754 definition...
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:386
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:356
@ ATOMIC_LOAD_FMINIMUM
@ TargetFrameIndex
Definition ISDOpcodes.h:187
@ VECTOR_SPLICE_LEFT
VECTOR_SPLICE_LEFT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1, VEC2) left by OFFSET elements an...
Definition ISDOpcodes.h:653
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition ISDOpcodes.h:907
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:896
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:727
@ MASKED_UDIV
Masked vector arithmetic that returns poison on disabled lanes.
@ LIFETIME_START
This corresponds to the llvm.lifetime.
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:986
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:813
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:328
@ MGATHER
Masked gather and scatter - load and store operations for a vector of random addresses with additiona...
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
@ ATOMIC_LOAD_UDEC_WRAP
@ PEXT
Parallel bit extract (compress) and parallel bit deposit (expand).
Definition ISDOpcodes.h:783
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:500
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:934
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition ISDOpcodes.h:179
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:505
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:739
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:205
@ GET_FPENV_MEM
Gets the current floating-point environment.
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:735
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:710
@ VECTOR_SPLICE_RIGHT
VECTOR_SPLICE_RIGHT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1,VEC2) right by OFFSET elements a...
Definition ISDOpcodes.h:657
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:304
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition ISDOpcodes.h:681
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:241
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:565
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
@ CTTZ_ZERO_POISON
Bit counting operators with a poisoned result for zero inputs.
Definition ISDOpcodes.h:795
@ ExternalSymbol
Definition ISDOpcodes.h:93
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:967
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:699
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:929
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
@ EXPERIMENTAL_VECTOR_HISTOGRAM
Experimental vector histogram intrinsic Operands: Input Chain, Inc, Mask, Base, Index,...
@ 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:953
@ VECREDUCE_FMINIMUM
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:864
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
@ VECREDUCE_SEQ_FMUL
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:841
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ ATOMIC_LOAD_UINC_WRAP
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:534
@ PARTIAL_REDUCE_SUMLA
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:365
@ SET_FPENV_MEM
Sets the current floating point environment.
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
@ TRUNCATE_SSAT_S
TRUNCATE_[SU]SAT_[SU] - Truncate for saturated operand [SU] located in middle, prefix for SAT means i...
Definition ISDOpcodes.h:879
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:722
@ TRUNCATE_USAT_U
Definition ISDOpcodes.h:883
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:338
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:213
@ TargetGlobalTLSAddress
Definition ISDOpcodes.h:186
@ ABS_MIN_POISON
ABS with a poison result for INT_MIN.
Definition ISDOpcodes.h:751
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:556
LLVM_ABI NodeType getOppositeSignednessMinMaxOpcode(unsigned MinMaxOpc)
Given a MinMaxOpc of ISD::(U|S)MIN or ISD::(U|S)MAX, returns the corresponding opcode with the opposi...
LLVM_ABI bool isBuildVectorOfConstantSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantSDNode or undef.
LLVM_ABI NodeType getExtForLoadExtType(bool IsFP, LoadExtType)
bool isZEXTLoad(const SDNode *N)
Returns true if the specified node is a ZEXTLOAD.
bool isExtOpcode(unsigned Opcode)
LLVM_ABI bool isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are 0 o...
LLVM_ABI NodeType getUnmaskedBinOpOpcode(unsigned MaskedOpc)
Given a MaskedOpc of ISD::MASKED_(U|S)(DIV|REM), returns the unmasked ISD::(U|S)(DIV|REM).
LLVM_ABI bool isVectorShrinkable(const SDNode *N, unsigned NewEltSize, bool Signed)
Returns true if the specified node is a vector where all elements can be truncated to the specified e...
LLVM_ABI bool isVPBinaryOp(unsigned Opcode)
Whether this is a vector-predicated binary operation opcode.
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, EVT Type)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
LLVM_ABI std::optional< unsigned > getBaseOpcodeForVP(unsigned Opcode, bool hasFPExcept)
Translate this VP Opcode to its corresponding non-VP Opcode.
bool isBitwiseLogicOp(unsigned Opcode)
Whether this is bitwise logic opcode.
bool isTrueWhenEqual(CondCode Cond)
Return true if the specified condition returns true if the two operands to the condition are equal.
LLVM_ABI std::optional< unsigned > getVPMaskIdx(unsigned Opcode)
The operand position of the vector mask.
unsigned getUnorderedFlavor(CondCode Cond)
This function returns 0 if the condition is always false if an operand is a NaN, 1 if the condition i...
LLVM_ABI std::optional< unsigned > getVPExplicitVectorLengthIdx(unsigned Opcode)
The operand position of the explicit vector length parameter.
bool isEXTLoad(const SDNode *N)
Returns true if the specified node is a EXTLOAD.
LLVM_ABI bool allOperandsUndef(const SDNode *N)
Return true if the node has at least one operand and all operands of the specified node are ISD::UNDE...
LLVM_ABI bool isFreezeUndef(const SDNode *N)
Return true if the specified node is FREEZE(UNDEF).
LLVM_ABI CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y).
LLVM_ABI std::optional< unsigned > getVPForBaseOpcode(unsigned Opcode)
Translate this non-VP Opcode to its corresponding VP Opcode.
MemIndexType
MemIndexType enum - This enum defines how to interpret MGATHER/SCATTER's index parameter when calcula...
LLVM_ABI bool isBuildVectorAllZeros(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are 0 or undef.
bool matchUnaryPredicateImpl(SDValue Op, std::function< bool(ConstNodeType *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant BUI...
LLVM_ABI bool isConstantSplatVector(const SDNode *N, APInt &SplatValue)
Node predicates.
LLVM_ABI NodeType getInverseMinMaxOpcode(unsigned MinMaxOpc)
Given a MinMaxOpc of ISD::(U|S)MIN or ISD::(U|S)MAX, returns ISD::(U|S)MAX and ISD::(U|S)MIN,...
LLVM_ABI bool matchBinaryPredicate(SDValue LHS, SDValue RHS, std::function< bool(ConstantSDNode *, ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTypeMismatch=false)
Attempt to match a binary predicate against a pair of scalar/splat constants or every element of a pa...
LLVM_ABI bool isVPReduction(unsigned Opcode)
Whether this is a vector-predicated reduction opcode.
bool matchUnaryPredicate(SDValue Op, std::function< bool(ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Hook for matching ConstantSDNode predicate.
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
LLVM_ABI bool isBuildVectorOfConstantFPSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantFPSDNode or undef.
bool isSEXTLoad(const SDNode *N)
Returns true if the specified node is a SEXTLOAD.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
LLVM_ABI bool isBuildVectorAllOnes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are ~0 or undef.
LLVM_ABI NodeType getVecReduceBaseOpcode(unsigned VecReduceOpcode)
Get underlying scalar opcode for VECREDUCE opcode.
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
LLVM_ABI bool isVPOpcode(unsigned Opcode)
Whether this is a vector-predicated Opcode.
LLVM_ABI CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical OR between different comparisons of identical values: ((X op1 Y) | (X ...
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
match_deferred< 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()...
auto m_Value()
Match an arbitrary value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
LLVM_ABI Libcall getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMCPY_ELEMENT_UNORDERED_ATOMIC - Return MEMCPY_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI Libcall getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMSET_ELEMENT_UNORDERED_ATOMIC - Return MEMSET_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI Libcall getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMMOVE_ELEMENT_UNORDERED_ATOMIC - Return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_* value for the given e...
bool sd_match(SDNode *N, const SelectionDAG *DAG, Pattern &&P)
initializer< Ty > init(const Ty &Val)
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition Dwarf.h:149
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:668
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
GenericUniformityInfo< SSAContext > UniformityInfo
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:344
@ Offset
Definition DWP.cpp:573
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:360
LLVM_ABI ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp:237
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1759
LLVM_ABI SDValue peekThroughExtractSubvectors(SDValue V)
Return the non-extracted vector source operand of V if it exists.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
LLVM_ABI bool isAllOnesOrAllOnesSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant -1 integer or a splatted vector of a constant -1 integer (with...
Definition Utils.cpp:1572
LLVM_ABI SDValue getBitwiseNotOperand(SDValue V, SDValue Mask, bool AllowUndefs)
If V is a bitwise not, returns the inverted operand.
@ Undef
Value of the register doesn't matter.
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:315
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
LLVM_ABI bool isOneOrOneSplatFP(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant floating-point value, or a splatted vector of a constant float...
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:325
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition APFloat.h:1762
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:633
auto cast_or_null(const Y &Val)
Definition Casting.h:714
LLVM_ABI bool isNullOrNullSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
Definition Utils.cpp:1554
LLVM_ABI bool isMinSignedConstant(SDValue V)
Returns true if V is a constant min signed integer value.
LLVM_ABI ConstantFPSDNode * isConstOrConstSplatFP(SDValue N, bool AllowUndefs=false)
Returns the SDNode if it is a constant splat BuildVector or constant float.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition APFloat.h:1674
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 maxNum semantics.
Definition APFloat.h:1717
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
LLVM_ABI bool isBitwiseNot(SDValue V, bool AllowUndefs=false)
Returns true if V is a bitwise not operation.
LLVM_ABI SDValue peekThroughInsertVectorElt(SDValue V, const APInt &DemandedElts)
Recursively peek through INSERT_VECTOR_ELT nodes, returning the source vector operand of V,...
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI void checkForCycles(const SelectionDAG *DAG, bool force=false)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition APFloat.h:1748
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI SDValue peekThroughTruncates(SDValue V)
Return the non-truncated source operand of V if it exists.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1753
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
LLVM_ABI SDValue peekThroughOneUseBitcasts(SDValue V)
Return the non-bitcasted and one-use source operand of V if it exists.
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:82
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
bool includesPoison(UndefPoisonKind Kind)
Returns true if Kind includes the Poison bit.
Definition UndefPoison.h:27
LLVM_ABI bool isOneOrOneSplat(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
bool includesUndef(UndefPoisonKind Kind)
Returns true if Kind includes the Undef bit.
Definition UndefPoison.h:33
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 minNum semantics.
Definition APFloat.h:1698
@ Mul
Product of integers.
@ Sub
Subtraction of integers.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
LLVM_ABI bool isNullConstantOrUndef(SDValue V)
Returns true if V is a constant integer zero or an UNDEF node.
IntPtrTy
Definition InstrProf.h:82
LLVM_ABI 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:539
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI ConstantSDNode * isConstOrConstSplat(SDValue N, bool AllowUndefs=false, bool AllowTruncation=false)
Returns the SDNode if it is a constant splat BuildVector or constant int.
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1885
constexpr unsigned BitWidth
LLVM_ABI bool funcReturnsFirstArgOfCall(const CallInst &CI)
Returns true if the parent of CI returns CI's first argument after calling CI.
Definition Analysis.cpp:719
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isZeroOrZeroSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
LLVM_ABI bool isOneConstant(SDValue V)
Returns true if V is a constant integer one.
UndefPoisonKind
Enumeration to track whether we are interested in Undef, Poison, or both.
Definition UndefPoison.h:20
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
LLVM_ABI bool isNullFPConstant(SDValue V)
Returns true if V is an FP constant with a value of positive zero.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant (+/-)0.0 floating-point value or a splatted vector thereof (wi...
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition APFloat.h:1735
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1775
LLVM_ABI bool isOnesOrOnesSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
LLVM_ABI bool isAllOnesConstant(SDValue V)
Returns true if V is an integer constant with all bits set.
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition MathExtras.h:373
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:862
#define N
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition Metadata.h:763
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition Metadata.h:783
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:780
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
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:418
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:145
intptr_t getRawBits() const
Definition ValueTypes.h:543
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:70
EVT changeTypeToInteger() const
Return the type converted to an equivalently sized integer or vector with integer element type.
Definition ValueTypes.h:129
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:307
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:323
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:155
ElementCount getVectorElementCount() const
Definition ValueTypes.h:373
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:396
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:382
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:408
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:339
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:61
bool isFixedLengthVector() const
Definition ValueTypes.h:199
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:176
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:346
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:315
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition ValueTypes.h:279
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:187
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:351
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition ValueTypes.h:150
LLVM_ABI const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:359
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition ValueTypes.h:331
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition ValueTypes.h:484
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:160
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:315
LLVM_ABI KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:269
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:106
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:78
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:125
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:256
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:64
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:288
static LLVM_ABI std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
void makeNegative()
Make this value negative.
Definition KnownBits.h:120
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:97
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:165
KnownBits byteSwap() const
Definition KnownBits.h:559
static LLVM_ABI KnownBits fshl(const KnownBits &LHS, const KnownBits &RHS, const APInt &Amt)
Compute known bits for fshl(LHS, RHS, Amt).
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:303
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:84
KnownBits reverseBits() const
Definition KnownBits.h:563
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition KnownBits.h:247
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:176
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:72
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false, bool SelfAdd=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:361
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:109
static LLVM_ABI KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:239
static LLVM_ABI KnownBits pdep(const KnownBits &Val, const KnownBits &Mask)
Compute known bits for pdep(Val, Mask).
static LLVM_ABI KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorU.
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:325
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:184
static LLVM_ABI KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, const KnownBits &Borrow)
Compute known bits results from subtracting RHS from LHS with 1-bit Borrow.
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:200
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:146
static LLVM_ABI KnownBits fshr(const KnownBits &LHS, const KnownBits &RHS, const APInt &Amt)
Compute known bits for fshr(LHS, RHS, Amt).
static LLVM_ABI KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition KnownBits.h:112
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static LLVM_ABI KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:340
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:103
LLVM_ABI KnownBits truncSSat(unsigned BitWidth) const
Truncate with signed saturation (signed input -> signed output)
static LLVM_ABI KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry)
Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
Definition KnownBits.cpp:54
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:376
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:294
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:233
static LLVM_ABI KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:171
static LLVM_ABI KnownBits clmul(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for clmul(LHS, RHS).
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
LLVM_ABI KnownBits truncUSat(unsigned BitWidth) const
Truncate with unsigned saturation (unsigned input -> unsigned output)
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
LLVM_ABI KnownBits truncSSatU(unsigned BitWidth) const
Truncate with signed saturation to unsigned (signed input -> unsigned output)
static LLVM_ABI KnownBits pext(const KnownBits &Val, const KnownBits &Mask)
Compute known bits for pext(Val, Mask).
static LLVM_ABI KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
const APInt & getConstant() const
Returns the value when all bits have a known value.
Definition KnownBits.h:58
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
void copysign(const KnownFPClass &Sign)
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a zero.
bool isUnknown() const
KnownFPClass intersectWith(const KnownFPClass &RHS) const
std::optional< bool > SignBit
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
static LLVM_ABI KnownFPClass bitcast(const fltSemantics &FltSemantics, const KnownBits &Bits)
Report known values for a bitcast into a float with provided semantics.
This class contains a discriminated union of information about pointers in memory operands,...
LLVM_ABI bool isDereferenceable(unsigned Size, LLVMContext &C, const DataLayout &DL) const
Return true if memory region [V, V+Offset+Size) is known to be dereferenceable.
LLVM_ABI unsigned getAddrSpace() const
Return the LLVM IR address space number that this pointer points into.
PointerUnion< const Value *, const PseudoSourceValue * > V
This is the IR pointer value for the access, or it is null if unknown.
MachinePointerInfo getWithOffset(int64_t O) const
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition Alignment.h:130
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)
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl)
Get the libcall routine name for the specified libcall implementation.
These are IR-level optimization flags that may be propagated to SDNodes.
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
unsigned int NumVTs
Clients of various APIs that cause global effects on the DAG can optionally implement this interface.
virtual void NodeDeleted(SDNode *N, SDNode *E)
The node N that was deleted and, if E is not null, an equivalent node E that replaced it.
virtual void NodeInserted(SDNode *N)
The node N that was inserted.
virtual void NodeUpdated(SDNode *N)
The node N that was updated.
This structure contains all information that is necessary for lowering calls.
CallLoweringInfo & setLibCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList)
CallLoweringInfo & setDiscardResult(bool Value=true)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
CallLoweringInfo & setTailCall(bool Value=true)
CallLoweringInfo & setChain(SDValue InChain)