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 bool PoisonOnly) {
2574 if (isGuaranteedNotToBeUndefOrPoison(V, DemandedElts, PoisonOnly))
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:
3011 case ISD::TRUNCATE:
3012 case ISD::SIGN_EXTEND:
3013 case ISD::ZERO_EXTEND:
3014 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
3015 default:
3016 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
3017 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
3018 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
3019 Depth);
3020 break;
3021 }
3022
3023 // We don't support other cases than those above for scalable vectors at
3024 // the moment.
3025 if (VT.isScalableVector())
3026 return false;
3027
3028 unsigned NumElts = VT.getVectorNumElements();
3029 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
3030 UndefElts = APInt::getZero(NumElts);
3031
3032 switch (Opcode) {
3033 case ISD::BUILD_VECTOR: {
3034 SDValue Scl;
3035 for (unsigned i = 0; i != NumElts; ++i) {
3036 SDValue Op = V.getOperand(i);
3037 if (Op.isUndef()) {
3038 UndefElts.setBit(i);
3039 continue;
3040 }
3041 if (!DemandedElts[i])
3042 continue;
3043 if (Scl && Scl != Op)
3044 return false;
3045 Scl = Op;
3046 }
3047 return true;
3048 }
3049 case ISD::VECTOR_SHUFFLE: {
3050 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
3051 APInt DemandedLHS = APInt::getZero(NumElts);
3052 APInt DemandedRHS = APInt::getZero(NumElts);
3053 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
3054 for (int i = 0; i != (int)NumElts; ++i) {
3055 int M = Mask[i];
3056 if (M < 0) {
3057 UndefElts.setBit(i);
3058 continue;
3059 }
3060 if (!DemandedElts[i])
3061 continue;
3062 if (M < (int)NumElts)
3063 DemandedLHS.setBit(M);
3064 else
3065 DemandedRHS.setBit(M - NumElts);
3066 }
3067
3068 // If we aren't demanding either op, assume there's no splat.
3069 // If we are demanding both ops, assume there's no splat.
3070 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
3071 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
3072 return false;
3073
3074 // See if the demanded elts of the source op is a splat or we only demand
3075 // one element, which should always be a splat.
3076 // TODO: Handle source ops splats with undefs.
3077 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
3078 APInt SrcUndefs;
3079 return (SrcElts.popcount() == 1) ||
3080 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
3081 (SrcElts & SrcUndefs).isZero());
3082 };
3083 if (!DemandedLHS.isZero())
3084 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
3085 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
3086 }
3088 // Offset the demanded elts by the subvector index.
3089 SDValue Src = V.getOperand(0);
3090 // We don't support scalable vectors at the moment.
3091 if (Src.getValueType().isScalableVector())
3092 return false;
3093 uint64_t Idx = V.getConstantOperandVal(1);
3094 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3095 APInt UndefSrcElts;
3096 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3097 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3098 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3099 return true;
3100 }
3101 break;
3102 }
3106 // Widen the demanded elts by the src element count.
3107 SDValue Src = V.getOperand(0);
3108 // We don't support scalable vectors at the moment.
3109 if (Src.getValueType().isScalableVector())
3110 return false;
3111 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3112 APInt UndefSrcElts;
3113 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3114 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3115 UndefElts = UndefSrcElts.trunc(NumElts);
3116 return true;
3117 }
3118 break;
3119 }
3120 case ISD::BITCAST: {
3121 SDValue Src = V.getOperand(0);
3122 EVT SrcVT = Src.getValueType();
3123 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3124 unsigned BitWidth = VT.getScalarSizeInBits();
3125
3126 // Ignore bitcasts from unsupported types.
3127 // TODO: Add fp support?
3128 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3129 break;
3130
3131 // Bitcast 'small element' vector to 'large element' vector.
3132 if ((BitWidth % SrcBitWidth) == 0) {
3133 // See if each sub element is a splat.
3134 unsigned Scale = BitWidth / SrcBitWidth;
3135 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3136 APInt ScaledDemandedElts =
3137 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3138 for (unsigned I = 0; I != Scale; ++I) {
3139 APInt SubUndefElts;
3140 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3141 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3142 SubDemandedElts &= ScaledDemandedElts;
3143 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3144 return false;
3145 // TODO: Add support for merging sub undef elements.
3146 if (!SubUndefElts.isZero())
3147 return false;
3148 }
3149 return true;
3150 }
3151 break;
3152 }
3153 }
3154
3155 return false;
3156}
3157
3158/// Helper wrapper to main isSplatValue function.
3159bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3160 EVT VT = V.getValueType();
3161 assert(VT.isVector() && "Vector type expected");
3162
3163 APInt UndefElts;
3164 // Since the number of lanes in a scalable vector is unknown at compile time,
3165 // we track one bit which is implicitly broadcast to all lanes. This means
3166 // that all lanes in a scalable vector are considered demanded.
3167 APInt DemandedElts
3169 return isSplatValue(V, DemandedElts, UndefElts) &&
3170 (AllowUndefs || !UndefElts);
3171}
3172
3175
3176 EVT VT = V.getValueType();
3177 unsigned Opcode = V.getOpcode();
3178 switch (Opcode) {
3179 default: {
3180 APInt UndefElts;
3181 // Since the number of lanes in a scalable vector is unknown at compile time,
3182 // we track one bit which is implicitly broadcast to all lanes. This means
3183 // that all lanes in a scalable vector are considered demanded.
3184 APInt DemandedElts
3186
3187 if (isSplatValue(V, DemandedElts, UndefElts)) {
3188 if (VT.isScalableVector()) {
3189 // DemandedElts and UndefElts are ignored for scalable vectors, since
3190 // the only supported cases are SPLAT_VECTOR nodes.
3191 SplatIdx = 0;
3192 } else {
3193 // Handle case where all demanded elements are UNDEF.
3194 if (DemandedElts.isSubsetOf(UndefElts)) {
3195 SplatIdx = 0;
3196 return getUNDEF(VT);
3197 }
3198 SplatIdx = (UndefElts & DemandedElts).countr_one();
3199 }
3200 return V;
3201 }
3202 break;
3203 }
3204 case ISD::SPLAT_VECTOR:
3205 SplatIdx = 0;
3206 return V;
3207 case ISD::VECTOR_SHUFFLE: {
3208 assert(!VT.isScalableVector());
3209 // Check if this is a shuffle node doing a splat.
3210 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3211 // getTargetVShiftNode currently struggles without the splat source.
3212 auto *SVN = cast<ShuffleVectorSDNode>(V);
3213 if (!SVN->isSplat())
3214 break;
3215 int Idx = SVN->getSplatIndex();
3216 int NumElts = V.getValueType().getVectorNumElements();
3217 SplatIdx = Idx % NumElts;
3218 return V.getOperand(Idx / NumElts);
3219 }
3220 }
3221
3222 return SDValue();
3223}
3224
3226 int SplatIdx;
3227 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3228 EVT SVT = SrcVector.getValueType().getScalarType();
3229 EVT LegalSVT = SVT;
3230 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3231 if (!SVT.isInteger())
3232 return SDValue();
3233 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3234 if (LegalSVT.bitsLT(SVT))
3235 return SDValue();
3236 }
3237 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3238 }
3239 return SDValue();
3240}
3241
3242std::optional<ConstantRange>
3244 unsigned Depth) const {
3245 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3246 V.getOpcode() == ISD::SRA) &&
3247 "Unknown shift node");
3248 // Shifting more than the bitwidth is not valid.
3249 unsigned BitWidth = V.getScalarValueSizeInBits();
3250
3251 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3252 const APInt &ShAmt = Cst->getAPIntValue();
3253 if (ShAmt.uge(BitWidth))
3254 return std::nullopt;
3255 return ConstantRange(ShAmt);
3256 }
3257
3258 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3259 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3260 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3261 if (!DemandedElts[i])
3262 continue;
3263 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3264 if (!SA) {
3265 MinAmt = MaxAmt = nullptr;
3266 break;
3267 }
3268 const APInt &ShAmt = SA->getAPIntValue();
3269 if (ShAmt.uge(BitWidth))
3270 return std::nullopt;
3271 if (!MinAmt || MinAmt->ugt(ShAmt))
3272 MinAmt = &ShAmt;
3273 if (!MaxAmt || MaxAmt->ult(ShAmt))
3274 MaxAmt = &ShAmt;
3275 }
3276 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3277 "Failed to find matching min/max shift amounts");
3278 if (MinAmt && MaxAmt)
3279 return ConstantRange(*MinAmt, *MaxAmt + 1);
3280 }
3281
3282 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3283 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3284 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3285 if (KnownAmt.getMaxValue().ult(BitWidth))
3286 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3287
3288 return std::nullopt;
3289}
3290
3291std::optional<unsigned>
3293 unsigned Depth) const {
3294 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3295 V.getOpcode() == ISD::SRA) &&
3296 "Unknown shift node");
3297 if (std::optional<ConstantRange> AmtRange =
3298 getValidShiftAmountRange(V, DemandedElts, Depth))
3299 if (const APInt *ShAmt = AmtRange->getSingleElement())
3300 return ShAmt->getZExtValue();
3301 return std::nullopt;
3302}
3303
3304std::optional<unsigned>
3306 APInt DemandedElts = getDemandAllEltsMask(V);
3307 return getValidShiftAmount(V, DemandedElts, Depth);
3308}
3309
3310std::optional<unsigned>
3312 unsigned Depth) const {
3313 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3314 V.getOpcode() == ISD::SRA) &&
3315 "Unknown shift node");
3316 if (std::optional<ConstantRange> AmtRange =
3317 getValidShiftAmountRange(V, DemandedElts, Depth))
3318 return AmtRange->getUnsignedMin().getZExtValue();
3319 return std::nullopt;
3320}
3321
3322std::optional<unsigned>
3324 APInt DemandedElts = getDemandAllEltsMask(V);
3325 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3326}
3327
3328std::optional<unsigned>
3330 unsigned Depth) const {
3331 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3332 V.getOpcode() == ISD::SRA) &&
3333 "Unknown shift node");
3334 if (std::optional<ConstantRange> AmtRange =
3335 getValidShiftAmountRange(V, DemandedElts, Depth))
3336 return AmtRange->getUnsignedMax().getZExtValue();
3337 return std::nullopt;
3338}
3339
3340std::optional<unsigned>
3342 APInt DemandedElts = getDemandAllEltsMask(V);
3343 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3344}
3345
3346/// Determine which bits of Op are known to be either zero or one and return
3347/// them in Known. For vectors, the known bits are those that are shared by
3348/// every vector element.
3350 APInt DemandedElts = getDemandAllEltsMask(Op);
3351 return computeKnownBits(Op, DemandedElts, Depth);
3352}
3353
3354/// Determine which bits of Op are known to be either zero or one and return
3355/// them in Known. The DemandedElts argument allows us to only collect the known
3356/// bits that are shared by the requested vector elements.
3358 unsigned Depth) const {
3359 unsigned BitWidth = Op.getScalarValueSizeInBits();
3360
3361 KnownBits Known(BitWidth); // Don't know anything.
3362
3363 if (auto OptAPInt = Op->bitcastToAPInt()) {
3364 // We know all of the bits for a constant!
3365 return KnownBits::makeConstant(*std::move(OptAPInt));
3366 }
3367
3368 if (Depth >= MaxRecursionDepth)
3369 return Known; // Limit search depth.
3370
3371 KnownBits Known2;
3372 unsigned NumElts = DemandedElts.getBitWidth();
3373 assert((!Op.getValueType().isScalableVector() || NumElts == 1) &&
3374 "DemandedElts for scalable vectors must be 1 to represent all lanes");
3375 assert((!Op.getValueType().isFixedLengthVector() ||
3376 NumElts == Op.getValueType().getVectorNumElements()) &&
3377 "Unexpected vector size");
3378
3379 if (!DemandedElts)
3380 return Known; // No demanded elts, better to assume we don't know anything.
3381
3382 unsigned Opcode = Op.getOpcode();
3383 switch (Opcode) {
3384 case ISD::MERGE_VALUES:
3385 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3386 Depth + 1);
3387 case ISD::SPLAT_VECTOR: {
3388 SDValue SrcOp = Op.getOperand(0);
3389 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3390 "Expected SPLAT_VECTOR implicit truncation");
3391 // Implicitly truncate the bits to match the official semantics of
3392 // SPLAT_VECTOR.
3393 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3394 break;
3395 }
3397 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3398 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3399 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3400 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3401 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3402 }
3403 break;
3404 }
3405 case ISD::STEP_VECTOR: {
3406 const APInt &Step = Op.getConstantOperandAPInt(0);
3407
3408 if (Step.isPowerOf2())
3409 Known.Zero.setLowBits(Step.logBase2());
3410
3412
3413 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3414 break;
3415 const APInt MinNumElts =
3416 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3417
3418 bool Overflow;
3419 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3421 .umul_ov(MinNumElts, Overflow);
3422 if (Overflow)
3423 break;
3424
3425 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3426 if (Overflow)
3427 break;
3428
3429 Known.Zero.setHighBits(MaxValue.countl_zero());
3430 break;
3431 }
3432 case ISD::BUILD_VECTOR:
3433 assert(!Op.getValueType().isScalableVector());
3434 // Collect the known bits that are shared by every demanded vector element.
3435 Known.setAllConflict();
3436 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3437 if (!DemandedElts[i])
3438 continue;
3439
3440 SDValue SrcOp = Op.getOperand(i);
3441 Known2 = computeKnownBits(SrcOp, Depth + 1);
3442
3443 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3444 if (SrcOp.getValueSizeInBits() != BitWidth) {
3445 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3446 "Expected BUILD_VECTOR implicit truncation");
3447 Known2 = Known2.trunc(BitWidth);
3448 }
3449
3450 // Known bits are the values that are shared by every demanded element.
3451 Known = Known.intersectWith(Known2);
3452
3453 // If we don't know any bits, early out.
3454 if (Known.isUnknown())
3455 break;
3456 }
3457 break;
3458 case ISD::VECTOR_COMPRESS: {
3459 SDValue Vec = Op.getOperand(0);
3460 SDValue PassThru = Op.getOperand(2);
3461 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3462 // If we don't know any bits, early out.
3463 if (Known.isUnknown())
3464 break;
3465 Known2 = computeKnownBits(Vec, Depth + 1);
3466 Known = Known.intersectWith(Known2);
3467 break;
3468 }
3469 case ISD::VECTOR_SHUFFLE: {
3470 assert(!Op.getValueType().isScalableVector());
3471 // Collect the known bits that are shared by every vector element referenced
3472 // by the shuffle.
3473 APInt DemandedLHS, DemandedRHS;
3475 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3476 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3477 DemandedLHS, DemandedRHS))
3478 break;
3479
3480 // Known bits are the values that are shared by every demanded element.
3481 Known.setAllConflict();
3482 if (!!DemandedLHS) {
3483 SDValue LHS = Op.getOperand(0);
3484 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3485 Known = Known.intersectWith(Known2);
3486 }
3487 // If we don't know any bits, early out.
3488 if (Known.isUnknown())
3489 break;
3490 if (!!DemandedRHS) {
3491 SDValue RHS = Op.getOperand(1);
3492 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3493 Known = Known.intersectWith(Known2);
3494 }
3495 break;
3496 }
3497 case ISD::VSCALE: {
3499 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3500 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3501 break;
3502 }
3503 case ISD::CONCAT_VECTORS: {
3504 if (Op.getValueType().isScalableVector())
3505 break;
3506 // Split DemandedElts and test each of the demanded subvectors.
3507 Known.setAllConflict();
3508 EVT SubVectorVT = Op.getOperand(0).getValueType();
3509 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3510 unsigned NumSubVectors = Op.getNumOperands();
3511 for (unsigned i = 0; i != NumSubVectors; ++i) {
3512 APInt DemandedSub =
3513 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3514 if (!!DemandedSub) {
3515 SDValue Sub = Op.getOperand(i);
3516 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3517 Known = Known.intersectWith(Known2);
3518 }
3519 // If we don't know any bits, early out.
3520 if (Known.isUnknown())
3521 break;
3522 }
3523 break;
3524 }
3525 case ISD::INSERT_SUBVECTOR: {
3526 if (Op.getValueType().isScalableVector())
3527 break;
3528 // Demand any elements from the subvector and the remainder from the src its
3529 // inserted into.
3530 SDValue Src = Op.getOperand(0);
3531 SDValue Sub = Op.getOperand(1);
3532 uint64_t Idx = Op.getConstantOperandVal(2);
3533 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3534 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3535 APInt DemandedSrcElts = DemandedElts;
3536 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3537
3538 Known.setAllConflict();
3539 if (!!DemandedSubElts) {
3540 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3541 if (Known.isUnknown())
3542 break; // early-out.
3543 }
3544 if (!!DemandedSrcElts) {
3545 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3546 Known = Known.intersectWith(Known2);
3547 }
3548 break;
3549 }
3551 // Offset the demanded elts by the subvector index.
3552 SDValue Src = Op.getOperand(0);
3553
3554 APInt DemandedSrcElts;
3555 if (Src.getValueType().isScalableVector())
3556 DemandedSrcElts = APInt(1, 1); // <=> 'demand all elements'
3557 else {
3558 uint64_t Idx = Op.getConstantOperandVal(1);
3559 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3560 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3561 }
3562 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3563 break;
3564 }
3565 case ISD::SCALAR_TO_VECTOR: {
3566 if (Op.getValueType().isScalableVector())
3567 break;
3568 // We know about scalar_to_vector as much as we know about it source,
3569 // which becomes the first element of otherwise unknown vector.
3570 if (DemandedElts != 1)
3571 break;
3572
3573 SDValue N0 = Op.getOperand(0);
3574 Known = computeKnownBits(N0, Depth + 1);
3575 if (N0.getValueSizeInBits() != BitWidth)
3576 Known = Known.trunc(BitWidth);
3577
3578 break;
3579 }
3580 case ISD::BITCAST: {
3581 if (Op.getValueType().isScalableVector())
3582 break;
3583
3584 SDValue N0 = Op.getOperand(0);
3585 EVT SubVT = N0.getValueType();
3586 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3587
3588 // Ignore bitcasts from unsupported types.
3589 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3590 break;
3591
3592 // Fast handling of 'identity' bitcasts.
3593 if (BitWidth == SubBitWidth) {
3594 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3595 break;
3596 }
3597
3598 bool IsLE = getDataLayout().isLittleEndian();
3599
3600 // Bitcast 'small element' vector to 'large element' scalar/vector.
3601 if ((BitWidth % SubBitWidth) == 0) {
3602 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3603
3604 // Collect known bits for the (larger) output by collecting the known
3605 // bits from each set of sub elements and shift these into place.
3606 // We need to separately call computeKnownBits for each set of
3607 // sub elements as the knownbits for each is likely to be different.
3608 unsigned SubScale = BitWidth / SubBitWidth;
3609 APInt SubDemandedElts(NumElts * SubScale, 0);
3610 for (unsigned i = 0; i != NumElts; ++i)
3611 if (DemandedElts[i])
3612 SubDemandedElts.setBit(i * SubScale);
3613
3614 for (unsigned i = 0; i != SubScale; ++i) {
3615 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3616 Depth + 1);
3617 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3618 Known.insertBits(Known2, SubBitWidth * Shifts);
3619 }
3620 }
3621
3622 // Bitcast 'large element' scalar/vector to 'small element' vector.
3623 if ((SubBitWidth % BitWidth) == 0) {
3624 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3625
3626 // Collect known bits for the (smaller) output by collecting the known
3627 // bits from the overlapping larger input elements and extracting the
3628 // sub sections we actually care about.
3629 unsigned SubScale = SubBitWidth / BitWidth;
3630 APInt SubDemandedElts =
3631 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3632 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3633
3634 Known.setAllConflict();
3635 for (unsigned i = 0; i != NumElts; ++i)
3636 if (DemandedElts[i]) {
3637 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3638 unsigned Offset = (Shifts % SubScale) * BitWidth;
3639 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3640 // If we don't know any bits, early out.
3641 if (Known.isUnknown())
3642 break;
3643 }
3644 }
3645 break;
3646 }
3647 case ISD::AND:
3648 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3649 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3650
3651 Known &= Known2;
3652 break;
3653 case ISD::OR:
3654 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3655 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3656
3657 Known |= Known2;
3658 break;
3659 case ISD::XOR:
3660 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3661 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3662
3663 Known ^= Known2;
3664 break;
3665 case ISD::MUL: {
3666 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3667 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3668 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3669 // TODO: SelfMultiply can be poison, but not undef.
3670 if (SelfMultiply)
3671 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3672 Op.getOperand(0), DemandedElts, false, Depth + 1);
3673 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3674
3675 // If the multiplication is known not to overflow, the product of a number
3676 // with itself is non-negative. Only do this if we didn't already computed
3677 // the opposite value for the sign bit.
3678 if (Op->getFlags().hasNoSignedWrap() &&
3679 Op.getOperand(0) == Op.getOperand(1) &&
3680 !Known.isNegative())
3681 Known.makeNonNegative();
3682 break;
3683 }
3684 case ISD::MULHU: {
3685 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3686 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3687 Known = KnownBits::mulhu(Known, Known2);
3688 break;
3689 }
3690 case ISD::MULHS: {
3691 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3692 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3693 Known = KnownBits::mulhs(Known, Known2);
3694 break;
3695 }
3696 case ISD::ABDU: {
3697 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3698 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3699 Known = KnownBits::abdu(Known, Known2);
3700 break;
3701 }
3702 case ISD::ABDS: {
3703 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3704 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3705 Known = KnownBits::abds(Known, Known2);
3706 unsigned SignBits1 =
3707 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3708 if (SignBits1 == 1)
3709 break;
3710 unsigned SignBits0 =
3711 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3712 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3713 break;
3714 }
3715 case ISD::UMUL_LOHI: {
3716 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3717 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3718 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3719 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3720 if (Op.getResNo() == 0)
3721 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3722 else
3723 Known = KnownBits::mulhu(Known, Known2);
3724 break;
3725 }
3726 case ISD::SMUL_LOHI: {
3727 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3728 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3729 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3730 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3731 if (Op.getResNo() == 0)
3732 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3733 else
3734 Known = KnownBits::mulhs(Known, Known2);
3735 break;
3736 }
3737 case ISD::AVGFLOORU: {
3738 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3739 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3740 Known = KnownBits::avgFloorU(Known, Known2);
3741 break;
3742 }
3743 case ISD::AVGCEILU: {
3744 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3745 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3746 Known = KnownBits::avgCeilU(Known, Known2);
3747 break;
3748 }
3749 case ISD::AVGFLOORS: {
3750 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3751 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3752 Known = KnownBits::avgFloorS(Known, Known2);
3753 break;
3754 }
3755 case ISD::AVGCEILS: {
3756 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3757 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3758 Known = KnownBits::avgCeilS(Known, Known2);
3759 break;
3760 }
3761 case ISD::SELECT:
3762 case ISD::VSELECT:
3763 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3764 // If we don't know any bits, early out.
3765 if (Known.isUnknown())
3766 break;
3767 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3768
3769 // Only known if known in both the LHS and RHS.
3770 Known = Known.intersectWith(Known2);
3771 break;
3772 case ISD::SELECT_CC:
3773 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3774 // If we don't know any bits, early out.
3775 if (Known.isUnknown())
3776 break;
3777 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3778
3779 // Only known if known in both the LHS and RHS.
3780 Known = Known.intersectWith(Known2);
3781 break;
3782 case ISD::SMULO:
3783 case ISD::UMULO:
3784 if (Op.getResNo() != 1)
3785 break;
3786 // The boolean result conforms to getBooleanContents.
3787 // If we know the result of a setcc has the top bits zero, use this info.
3788 // We know that we have an integer-based boolean since these operations
3789 // are only available for integer.
3790 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3792 BitWidth > 1)
3793 Known.Zero.setBitsFrom(1);
3794 break;
3795 case ISD::SETCC:
3796 case ISD::SETCCCARRY:
3797 case ISD::STRICT_FSETCC:
3798 case ISD::STRICT_FSETCCS: {
3799 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3800 // If we know the result of a setcc has the top bits zero, use this info.
3801 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3803 BitWidth > 1)
3804 Known.Zero.setBitsFrom(1);
3805 break;
3806 }
3807 case ISD::SHL: {
3808 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3809 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3810
3811 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3812 bool NSW = Op->getFlags().hasNoSignedWrap();
3813
3814 bool ShAmtNonZero = Known2.isNonZero();
3815
3816 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3817
3818 // Minimum shift low bits are known zero.
3819 if (std::optional<unsigned> ShMinAmt =
3820 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3821 Known.Zero.setLowBits(*ShMinAmt);
3822 break;
3823 }
3824 case ISD::SRL:
3825 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3826 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3827 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3828 Op->getFlags().hasExact());
3829
3830 // Minimum shift high bits are known zero.
3831 if (std::optional<unsigned> ShMinAmt =
3832 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3833 Known.Zero.setHighBits(*ShMinAmt);
3834 break;
3835 case ISD::SRA:
3836 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3837 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3838 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3839 Op->getFlags().hasExact());
3840 break;
3841 case ISD::ROTL:
3842 case ISD::ROTR:
3843 if (ConstantSDNode *C =
3844 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3845 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3846
3847 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3848
3849 // Canonicalize to ROTR.
3850 if (Opcode == ISD::ROTL && Amt != 0)
3851 Amt = BitWidth - Amt;
3852
3853 Known.Zero = Known.Zero.rotr(Amt);
3854 Known.One = Known.One.rotr(Amt);
3855 }
3856 break;
3857 case ISD::FSHL:
3858 case ISD::FSHR:
3859 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3860 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3861
3862 // For fshl, 0-shift returns the 1st arg.
3863 // For fshr, 0-shift returns the 2nd arg.
3864 if (Amt == 0) {
3865 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3866 DemandedElts, Depth + 1);
3867 break;
3868 }
3869
3870 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3871 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3872 const APInt ShAmt(BitWidth, Amt);
3873 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3874 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3875 Known = Opcode == ISD::FSHL ? KnownBits::fshl(Known, Known2, ShAmt)
3876 : KnownBits::fshr(Known, Known2, ShAmt);
3877 }
3878 break;
3879 case ISD::SHL_PARTS:
3880 case ISD::SRA_PARTS:
3881 case ISD::SRL_PARTS: {
3882 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3883
3884 // Collect lo/hi source values and concatenate.
3885 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3886 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3887 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3888 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3889 Known = Known2.concat(Known);
3890
3891 // Collect shift amount.
3892 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3893
3894 if (Opcode == ISD::SHL_PARTS)
3895 Known = KnownBits::shl(Known, Known2);
3896 else if (Opcode == ISD::SRA_PARTS)
3897 Known = KnownBits::ashr(Known, Known2);
3898 else // if (Opcode == ISD::SRL_PARTS)
3899 Known = KnownBits::lshr(Known, Known2);
3900
3901 // TODO: Minimum shift low/high bits are known zero.
3902
3903 if (Op.getResNo() == 0)
3904 Known = Known.extractBits(LoBits, 0);
3905 else
3906 Known = Known.extractBits(HiBits, LoBits);
3907 break;
3908 }
3910 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3911 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3912 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3913 break;
3914 }
3915 case ISD::CTTZ:
3916 case ISD::CTTZ_ZERO_UNDEF: {
3917 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3918 // If we have a known 1, its position is our upper bound.
3919 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3920 unsigned LowBits = llvm::bit_width(PossibleTZ);
3921 Known.Zero.setBitsFrom(LowBits);
3922 break;
3923 }
3924 case ISD::CTLZ:
3925 case ISD::CTLZ_ZERO_UNDEF: {
3926 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3927 // If we have a known 1, its position is our upper bound.
3928 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3929 unsigned LowBits = llvm::bit_width(PossibleLZ);
3930 Known.Zero.setBitsFrom(LowBits);
3931 break;
3932 }
3933 case ISD::CTLS: {
3934 unsigned MinRedundantSignBits =
3935 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1;
3936 ConstantRange Range(APInt(BitWidth, MinRedundantSignBits),
3938 Known = Range.toKnownBits();
3939 break;
3940 }
3941 case ISD::CTPOP: {
3942 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3943 // If we know some of the bits are zero, they can't be one.
3944 unsigned PossibleOnes = Known2.countMaxPopulation();
3945 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3946 break;
3947 }
3948 case ISD::PARITY: {
3949 // Parity returns 0 everywhere but the LSB.
3950 Known.Zero.setBitsFrom(1);
3951 break;
3952 }
3953 case ISD::CLMUL: {
3954 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3955 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3956 Known = KnownBits::clmul(Known, Known2);
3957 break;
3958 }
3959 case ISD::MGATHER:
3960 case ISD::MLOAD: {
3961 ISD::LoadExtType ETy =
3962 (Opcode == ISD::MGATHER)
3963 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3964 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3965 if (ETy == ISD::ZEXTLOAD) {
3966 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3967 KnownBits Known0(MemVT.getScalarSizeInBits());
3968 return Known0.zext(BitWidth);
3969 }
3970 break;
3971 }
3972 case ISD::LOAD: {
3974 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3975 if (ISD::isNON_EXTLoad(LD) && Cst) {
3976 // Determine any common known bits from the loaded constant pool value.
3977 Type *CstTy = Cst->getType();
3978 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3979 !Op.getValueType().isScalableVector()) {
3980 // If its a vector splat, then we can (quickly) reuse the scalar path.
3981 // NOTE: We assume all elements match and none are UNDEF.
3982 if (CstTy->isVectorTy()) {
3983 if (const Constant *Splat = Cst->getSplatValue()) {
3984 Cst = Splat;
3985 CstTy = Cst->getType();
3986 }
3987 }
3988 // TODO - do we need to handle different bitwidths?
3989 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3990 // Iterate across all vector elements finding common known bits.
3991 Known.setAllConflict();
3992 for (unsigned i = 0; i != NumElts; ++i) {
3993 if (!DemandedElts[i])
3994 continue;
3995 if (Constant *Elt = Cst->getAggregateElement(i)) {
3996 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3997 const APInt &Value = CInt->getValue();
3998 Known.One &= Value;
3999 Known.Zero &= ~Value;
4000 continue;
4001 }
4002 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
4003 APInt Value = CFP->getValueAPF().bitcastToAPInt();
4004 Known.One &= Value;
4005 Known.Zero &= ~Value;
4006 continue;
4007 }
4008 }
4009 Known.One.clearAllBits();
4010 Known.Zero.clearAllBits();
4011 break;
4012 }
4013 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
4014 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
4015 Known = KnownBits::makeConstant(CInt->getValue());
4016 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
4017 Known =
4018 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
4019 }
4020 }
4021 }
4022 } else if (Op.getResNo() == 0) {
4023 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
4024 KnownBits KnownScalarMemory(ScalarMemorySize);
4025 if (const MDNode *MD = LD->getRanges())
4026 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4027
4028 // Extend the Known bits from memory to the size of the scalar result.
4029 if (ISD::isZEXTLoad(Op.getNode()))
4030 Known = KnownScalarMemory.zext(BitWidth);
4031 else if (ISD::isSEXTLoad(Op.getNode()))
4032 Known = KnownScalarMemory.sext(BitWidth);
4033 else if (ISD::isEXTLoad(Op.getNode()))
4034 Known = KnownScalarMemory.anyext(BitWidth);
4035 else
4036 Known = KnownScalarMemory;
4037 assert(Known.getBitWidth() == BitWidth);
4038 return Known;
4039 }
4040 break;
4041 }
4043 if (Op.getValueType().isScalableVector())
4044 break;
4045 EVT InVT = Op.getOperand(0).getValueType();
4046 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4047 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4048 Known = Known.zext(BitWidth);
4049 break;
4050 }
4051 case ISD::ZERO_EXTEND: {
4052 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4053 Known = Known.zext(BitWidth);
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 // If the sign bit is known to be zero or one, then sext will extend
4063 // it to the top bits, else it will just zext.
4064 Known = Known.sext(BitWidth);
4065 break;
4066 }
4067 case ISD::SIGN_EXTEND: {
4068 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4069 // If the sign bit is known to be zero or one, then sext will extend
4070 // it to the top bits, else it will just zext.
4071 Known = Known.sext(BitWidth);
4072 break;
4073 }
4075 if (Op.getValueType().isScalableVector())
4076 break;
4077 EVT InVT = Op.getOperand(0).getValueType();
4078 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4079 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4080 Known = Known.anyext(BitWidth);
4081 break;
4082 }
4083 case ISD::ANY_EXTEND: {
4084 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4085 Known = Known.anyext(BitWidth);
4086 break;
4087 }
4088 case ISD::TRUNCATE: {
4089 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4090 Known = Known.trunc(BitWidth);
4091 break;
4092 }
4093 case ISD::TRUNCATE_SSAT_S: {
4094 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4095 Known = Known.truncSSat(BitWidth);
4096 break;
4097 }
4098 case ISD::TRUNCATE_SSAT_U: {
4099 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4100 Known = Known.truncSSatU(BitWidth);
4101 break;
4102 }
4103 case ISD::TRUNCATE_USAT_U: {
4104 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4105 Known = Known.truncUSat(BitWidth);
4106 break;
4107 }
4108 case ISD::AssertZext: {
4109 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4111 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4112 Known.Zero |= (~InMask);
4113 Known.One &= (~Known.Zero);
4114 break;
4115 }
4116 case ISD::AssertAlign: {
4117 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4118 assert(LogOfAlign != 0);
4119
4120 // TODO: Should use maximum with source
4121 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4122 // well as clearing one bits.
4123 Known.Zero.setLowBits(LogOfAlign);
4124 Known.One.clearLowBits(LogOfAlign);
4125 break;
4126 }
4127 case ISD::AssertNoFPClass: {
4128 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4129
4130 FPClassTest NoFPClass =
4131 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
4132 const FPClassTest NegativeTestMask = fcNan | fcNegative;
4133 if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
4134 // Cannot be negative.
4135 Known.makeNonNegative();
4136 }
4137
4138 const FPClassTest PositiveTestMask = fcNan | fcPositive;
4139 if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
4140 // Cannot be positive.
4141 Known.makeNegative();
4142 }
4143
4144 break;
4145 }
4146 case ISD::FABS:
4147 // fabs clears the sign bit
4148 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4149 Known.makeNonNegative();
4150 break;
4151 case ISD::FGETSIGN:
4152 // All bits are zero except the low bit.
4153 Known.Zero.setBitsFrom(1);
4154 break;
4155 case ISD::ADD: {
4156 SDNodeFlags Flags = Op.getNode()->getFlags();
4157 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4158 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4159 bool SelfAdd = Op.getOperand(0) == Op.getOperand(1) &&
4161 Op.getOperand(0), DemandedElts, false, Depth + 1);
4162 Known = KnownBits::add(Known, Known2, Flags.hasNoSignedWrap(),
4163 Flags.hasNoUnsignedWrap(), SelfAdd);
4164 break;
4165 }
4166 case ISD::SUB: {
4167 SDNodeFlags Flags = Op.getNode()->getFlags();
4168 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4169 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4170 Known = KnownBits::sub(Known, Known2, Flags.hasNoSignedWrap(),
4171 Flags.hasNoUnsignedWrap());
4172 break;
4173 }
4174 case ISD::USUBO:
4175 case ISD::SSUBO:
4176 case ISD::USUBO_CARRY:
4177 case ISD::SSUBO_CARRY:
4178 if (Op.getResNo() == 1) {
4179 // If we know the result of a setcc has the top bits zero, use this info.
4180 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4182 BitWidth > 1)
4183 Known.Zero.setBitsFrom(1);
4184 break;
4185 }
4186 [[fallthrough]];
4187 case ISD::SUBC: {
4188 assert(Op.getResNo() == 0 &&
4189 "We only compute knownbits for the difference here.");
4190
4191 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4192 KnownBits Borrow(1);
4193 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4194 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4195 // Borrow has bit width 1
4196 Borrow = Borrow.trunc(1);
4197 } else {
4198 Borrow.setAllZero();
4199 }
4200
4201 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4202 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4203 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4204 break;
4205 }
4206 case ISD::UADDO:
4207 case ISD::SADDO:
4208 case ISD::UADDO_CARRY:
4209 case ISD::SADDO_CARRY:
4210 if (Op.getResNo() == 1) {
4211 // If we know the result of a setcc has the top bits zero, use this info.
4212 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4214 BitWidth > 1)
4215 Known.Zero.setBitsFrom(1);
4216 break;
4217 }
4218 [[fallthrough]];
4219 case ISD::ADDC:
4220 case ISD::ADDE: {
4221 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4222
4223 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4224 KnownBits Carry(1);
4225 if (Opcode == ISD::ADDE)
4226 // Can't track carry from glue, set carry to unknown.
4227 Carry.resetAll();
4228 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4229 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4230 // Carry has bit width 1
4231 Carry = Carry.trunc(1);
4232 } else {
4233 Carry.setAllZero();
4234 }
4235
4236 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4237 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4238 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4239 break;
4240 }
4241 case ISD::UDIV: {
4242 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4243 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4244 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4245 break;
4246 }
4247 case ISD::SDIV: {
4248 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4249 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4250 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4251 break;
4252 }
4253 case ISD::SREM: {
4254 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4255 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4256 Known = KnownBits::srem(Known, Known2);
4257 break;
4258 }
4259 case ISD::UREM: {
4260 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4261 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4262 Known = KnownBits::urem(Known, Known2);
4263 break;
4264 }
4265 case ISD::EXTRACT_ELEMENT: {
4266 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4267 const unsigned Index = Op.getConstantOperandVal(1);
4268 const unsigned EltBitWidth = Op.getValueSizeInBits();
4269
4270 // Remove low part of known bits mask
4271 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4272 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4273
4274 // Remove high part of known bit mask
4275 Known = Known.trunc(EltBitWidth);
4276 break;
4277 }
4279 SDValue InVec = Op.getOperand(0);
4280 SDValue EltNo = Op.getOperand(1);
4281 EVT VecVT = InVec.getValueType();
4282 // computeKnownBits not yet implemented for scalable vectors.
4283 if (VecVT.isScalableVector())
4284 break;
4285 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4286 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4287
4288 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4289 // anything about the extended bits.
4290 if (BitWidth > EltBitWidth)
4291 Known = Known.trunc(EltBitWidth);
4292
4293 // If we know the element index, just demand that vector element, else for
4294 // an unknown element index, ignore DemandedElts and demand them all.
4295 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4296 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4297 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4298 DemandedSrcElts =
4299 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4300
4301 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4302 if (BitWidth > EltBitWidth)
4303 Known = Known.anyext(BitWidth);
4304 break;
4305 }
4307 if (Op.getValueType().isScalableVector())
4308 break;
4309
4310 // If we know the element index, split the demand between the
4311 // source vector and the inserted element, otherwise assume we need
4312 // the original demanded vector elements and the value.
4313 SDValue InVec = Op.getOperand(0);
4314 SDValue InVal = Op.getOperand(1);
4315 SDValue EltNo = Op.getOperand(2);
4316 bool DemandedVal = true;
4317 APInt DemandedVecElts = DemandedElts;
4318 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4319 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4320 unsigned EltIdx = CEltNo->getZExtValue();
4321 DemandedVal = !!DemandedElts[EltIdx];
4322 DemandedVecElts.clearBit(EltIdx);
4323 }
4324 Known.setAllConflict();
4325 if (DemandedVal) {
4326 Known2 = computeKnownBits(InVal, Depth + 1);
4327 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4328 }
4329 if (!!DemandedVecElts) {
4330 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4331 Known = Known.intersectWith(Known2);
4332 }
4333 break;
4334 }
4335 case ISD::BITREVERSE: {
4336 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4337 Known = Known2.reverseBits();
4338 break;
4339 }
4340 case ISD::BSWAP: {
4341 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4342 Known = Known2.byteSwap();
4343 break;
4344 }
4345 case ISD::ABS: {
4346 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4347 Known = Known2.abs();
4348 Known.Zero.setHighBits(
4349 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4350 break;
4351 }
4352 case ISD::USUBSAT: {
4353 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4354 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4355 Known = KnownBits::usub_sat(Known, Known2);
4356 break;
4357 }
4358 case ISD::UMIN: {
4359 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4360 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4361 Known = KnownBits::umin(Known, Known2);
4362 break;
4363 }
4364 case ISD::UMAX: {
4365 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4366 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4367 Known = KnownBits::umax(Known, Known2);
4368 break;
4369 }
4370 case ISD::SMIN:
4371 case ISD::SMAX: {
4372 // If we have a clamp pattern, we know that the number of sign bits will be
4373 // the minimum of the clamp min/max range.
4374 bool IsMax = (Opcode == ISD::SMAX);
4375 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4376 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4377 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4378 CstHigh =
4379 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4380 if (CstLow && CstHigh) {
4381 if (!IsMax)
4382 std::swap(CstLow, CstHigh);
4383
4384 const APInt &ValueLow = CstLow->getAPIntValue();
4385 const APInt &ValueHigh = CstHigh->getAPIntValue();
4386 if (ValueLow.sle(ValueHigh)) {
4387 unsigned LowSignBits = ValueLow.getNumSignBits();
4388 unsigned HighSignBits = ValueHigh.getNumSignBits();
4389 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4390 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4391 Known.One.setHighBits(MinSignBits);
4392 break;
4393 }
4394 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4395 Known.Zero.setHighBits(MinSignBits);
4396 break;
4397 }
4398 }
4399 }
4400
4401 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4402 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4403 if (IsMax)
4404 Known = KnownBits::smax(Known, Known2);
4405 else
4406 Known = KnownBits::smin(Known, Known2);
4407
4408 // For SMAX, if CstLow is non-negative we know the result will be
4409 // non-negative and thus all sign bits are 0.
4410 // TODO: There's an equivalent of this for smin with negative constant for
4411 // known ones.
4412 if (IsMax && CstLow) {
4413 const APInt &ValueLow = CstLow->getAPIntValue();
4414 if (ValueLow.isNonNegative()) {
4415 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4416 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4417 }
4418 }
4419
4420 break;
4421 }
4422 case ISD::UINT_TO_FP: {
4423 Known.makeNonNegative();
4424 break;
4425 }
4426 case ISD::SINT_TO_FP: {
4427 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4428 if (Known2.isNonNegative())
4429 Known.makeNonNegative();
4430 else if (Known2.isNegative())
4431 Known.makeNegative();
4432 break;
4433 }
4434 case ISD::FP_TO_UINT_SAT: {
4435 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4436 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4438 break;
4439 }
4440 case ISD::ATOMIC_LOAD: {
4441 // If we are looking at the loaded value.
4442 if (Op.getResNo() == 0) {
4443 auto *AT = cast<AtomicSDNode>(Op);
4444 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4445 KnownBits KnownScalarMemory(ScalarMemorySize);
4446 if (const MDNode *MD = AT->getRanges())
4447 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4448
4449 switch (AT->getExtensionType()) {
4450 case ISD::ZEXTLOAD:
4451 Known = KnownScalarMemory.zext(BitWidth);
4452 break;
4453 case ISD::SEXTLOAD:
4454 Known = KnownScalarMemory.sext(BitWidth);
4455 break;
4456 case ISD::EXTLOAD:
4457 switch (TLI->getExtendForAtomicOps()) {
4458 case ISD::ZERO_EXTEND:
4459 Known = KnownScalarMemory.zext(BitWidth);
4460 break;
4461 case ISD::SIGN_EXTEND:
4462 Known = KnownScalarMemory.sext(BitWidth);
4463 break;
4464 default:
4465 Known = KnownScalarMemory.anyext(BitWidth);
4466 break;
4467 }
4468 break;
4469 case ISD::NON_EXTLOAD:
4470 Known = KnownScalarMemory;
4471 break;
4472 }
4473 assert(Known.getBitWidth() == BitWidth);
4474 }
4475 break;
4476 }
4478 if (Op.getResNo() == 1) {
4479 // The boolean result conforms to getBooleanContents.
4480 // If we know the result of a setcc has the top bits zero, use this info.
4481 // We know that we have an integer-based boolean since these operations
4482 // are only available for integer.
4483 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4485 BitWidth > 1)
4486 Known.Zero.setBitsFrom(1);
4487 break;
4488 }
4489 [[fallthrough]];
4491 case ISD::ATOMIC_SWAP:
4502 case ISD::ATOMIC_LOAD_UMAX: {
4503 // If we are looking at the loaded value.
4504 if (Op.getResNo() == 0) {
4505 auto *AT = cast<AtomicSDNode>(Op);
4506 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4507
4508 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4509 Known.Zero.setBitsFrom(MemBits);
4510 }
4511 break;
4512 }
4513 case ISD::FrameIndex:
4515 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4516 Known, getMachineFunction());
4517 break;
4518
4519 default:
4520 if (Opcode < ISD::BUILTIN_OP_END)
4521 break;
4522 [[fallthrough]];
4526 // TODO: Probably okay to remove after audit; here to reduce change size
4527 // in initial enablement patch for scalable vectors
4528 if (Op.getValueType().isScalableVector())
4529 break;
4530
4531 // Allow the target to implement this method for its nodes.
4532 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4533 break;
4534 }
4535
4536 return Known;
4537}
4538
4539/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4552
4555 // X + 0 never overflow
4556 if (isNullConstant(N1))
4557 return OFK_Never;
4558
4559 // If both operands each have at least two sign bits, the addition
4560 // cannot overflow.
4561 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4562 return OFK_Never;
4563
4564 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4565 return OFK_Sometime;
4566}
4567
4570 // X + 0 never overflow
4571 if (isNullConstant(N1))
4572 return OFK_Never;
4573
4574 // mulhi + 1 never overflow
4575 KnownBits N1Known = computeKnownBits(N1);
4576 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4577 N1Known.getMaxValue().ult(2))
4578 return OFK_Never;
4579
4580 KnownBits N0Known = computeKnownBits(N0);
4581 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4582 N0Known.getMaxValue().ult(2))
4583 return OFK_Never;
4584
4585 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4586 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4587 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4588 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4589}
4590
4593 // X - 0 never overflow
4594 if (isNullConstant(N1))
4595 return OFK_Never;
4596
4597 // If both operands each have at least two sign bits, the subtraction
4598 // cannot overflow.
4599 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4600 return OFK_Never;
4601
4602 KnownBits N0Known = computeKnownBits(N0);
4603 KnownBits N1Known = computeKnownBits(N1);
4604 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4605 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4606 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4607}
4608
4611 // X - 0 never overflow
4612 if (isNullConstant(N1))
4613 return OFK_Never;
4614
4615 ConstantRange N0Range =
4616 computeConstantRangeIncludingKnownBits(N0, /*ForSigned=*/false);
4617 ConstantRange N1Range =
4618 computeConstantRangeIncludingKnownBits(N1, /*ForSigned=*/false);
4619 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4620}
4621
4624 // X * 0 and X * 1 never overflow.
4625 if (isNullConstant(N1) || isOneConstant(N1))
4626 return OFK_Never;
4627
4630 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4631}
4632
4635 // X * 0 and X * 1 never overflow.
4636 if (isNullConstant(N1) || isOneConstant(N1))
4637 return OFK_Never;
4638
4639 // Get the size of the result.
4640 unsigned BitWidth = N0.getScalarValueSizeInBits();
4641
4642 // Sum of the sign bits.
4643 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4644
4645 // If we have enough sign bits, then there's no overflow.
4646 if (SignBits > BitWidth + 1)
4647 return OFK_Never;
4648
4649 if (SignBits == BitWidth + 1) {
4650 // The overflow occurs when the true multiplication of the
4651 // the operands is the minimum negative number.
4652 KnownBits N0Known = computeKnownBits(N0);
4653 KnownBits N1Known = computeKnownBits(N1);
4654 // If one of the operands is non-negative, then there's no
4655 // overflow.
4656 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4657 return OFK_Never;
4658 }
4659
4660 return OFK_Sometime;
4661}
4662
4664 unsigned Depth) const {
4665 APInt DemandedElts = getDemandAllEltsMask(Op);
4666 return computeConstantRange(Op, DemandedElts, ForSigned, Depth);
4667}
4668
4670 const APInt &DemandedElts,
4671 bool ForSigned,
4672 unsigned Depth) const {
4673 EVT VT = Op.getValueType();
4674 unsigned BitWidth = VT.getScalarSizeInBits();
4675
4676 if (Depth >= MaxRecursionDepth)
4677 return ConstantRange::getFull(BitWidth);
4678
4679 if (ConstantSDNode *C = isConstOrConstSplat(Op, DemandedElts))
4680 return ConstantRange(C->getAPIntValue());
4681
4682 unsigned Opcode = Op.getOpcode();
4683 switch (Opcode) {
4684 case ISD::VSCALE: {
4686 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
4687 return getVScaleRange(&F, BitWidth).multiply(Multiplier);
4688 }
4689 default:
4690 break;
4691 }
4692
4693 return ConstantRange::getFull(BitWidth);
4694}
4695
4698 unsigned Depth) const {
4699 APInt DemandedElts = getDemandAllEltsMask(Op);
4700 return computeConstantRangeIncludingKnownBits(Op, DemandedElts, ForSigned,
4701 Depth);
4702}
4703
4705 SDValue Op, const APInt &DemandedElts, bool ForSigned,
4706 unsigned Depth) const {
4707 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
4708 ConstantRange CR1 = ConstantRange::fromKnownBits(Known, ForSigned);
4709 ConstantRange CR2 = computeConstantRange(Op, DemandedElts, ForSigned, Depth);
4712 return CR1.intersectWith(CR2, RangeType);
4713}
4714
4716 unsigned Depth) const {
4717 APInt DemandedElts = getDemandAllEltsMask(Val);
4718 return isKnownToBeAPowerOfTwo(Val, DemandedElts, OrZero, Depth);
4719}
4720
4722 const APInt &DemandedElts,
4723 bool OrZero, unsigned Depth) const {
4724 if (Depth >= MaxRecursionDepth)
4725 return false; // Limit search depth.
4726
4727 EVT OpVT = Val.getValueType();
4728 unsigned BitWidth = OpVT.getScalarSizeInBits();
4729 [[maybe_unused]] unsigned NumElts = DemandedElts.getBitWidth();
4730 assert((!OpVT.isScalableVector() || NumElts == 1) &&
4731 "DemandedElts for scalable vectors must be 1 to represent all lanes");
4732 assert(
4733 (!OpVT.isFixedLengthVector() || NumElts == OpVT.getVectorNumElements()) &&
4734 "Unexpected vector size");
4735
4736 auto IsPowerOfTwoOrZero = [BitWidth, OrZero](const ConstantSDNode *C) {
4737 APInt V = C->getAPIntValue().zextOrTrunc(BitWidth);
4738 return (OrZero && V.isZero()) || V.isPowerOf2();
4739 };
4740
4741 // Is the constant a known power of 2 or zero?
4742 if (ISD::matchUnaryPredicate(Val, IsPowerOfTwoOrZero))
4743 return true;
4744
4745 switch (Val.getOpcode()) {
4746 case ISD::BUILD_VECTOR:
4747 // Are all operands of a build vector constant powers of two or zero?
4748 if (all_of(enumerate(Val->ops()), [&](auto P) {
4749 auto *C = dyn_cast<ConstantSDNode>(P.value());
4750 return !DemandedElts[P.index()] || (C && IsPowerOfTwoOrZero(C));
4751 }))
4752 return true;
4753 break;
4754
4755 case ISD::SPLAT_VECTOR:
4756 // Is the operand of a splat vector a constant power of two?
4757 if (auto *C = dyn_cast<ConstantSDNode>(Val->getOperand(0)))
4758 if (IsPowerOfTwoOrZero(C))
4759 return true;
4760 break;
4761
4763 SDValue InVec = Val.getOperand(0);
4764 SDValue EltNo = Val.getOperand(1);
4765 EVT VecVT = InVec.getValueType();
4766
4767 // Skip scalable vectors or implicit extensions.
4768 if (VecVT.isScalableVector() ||
4769 OpVT.getScalarSizeInBits() != VecVT.getScalarSizeInBits())
4770 break;
4771
4772 // If we know the element index, just demand that vector element, else for
4773 // an unknown element index, ignore DemandedElts and demand them all.
4774 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4775 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4776 APInt DemandedSrcElts =
4777 ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts)
4778 ? APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue())
4779 : APInt::getAllOnes(NumSrcElts);
4780 return isKnownToBeAPowerOfTwo(InVec, DemandedSrcElts, OrZero, Depth + 1);
4781 }
4782
4783 case ISD::AND: {
4784 // Looking for `x & -x` pattern:
4785 // If x == 0:
4786 // x & -x -> 0
4787 // If x != 0:
4788 // x & -x -> non-zero pow2
4789 // so if we find the pattern return whether we know `x` is non-zero.
4790 SDValue X, Z;
4791 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))) ||
4792 (sd_match(Val, m_And(m_Value(X), m_Sub(m_Value(Z), m_Deferred(X)))) &&
4793 MaskedVectorIsZero(Z, DemandedElts, Depth + 1)))
4794 return OrZero || isKnownNeverZero(X, DemandedElts, Depth);
4795 break;
4796 }
4797
4798 case ISD::SHL: {
4799 // A left-shift of a constant one will have exactly one bit set because
4800 // shifting the bit off the end is undefined.
4801 auto *C = isConstOrConstSplat(Val.getOperand(0), DemandedElts);
4802 if (C && C->getAPIntValue() == 1)
4803 return true;
4804 return (OrZero || isKnownNeverZero(Val, DemandedElts, Depth)) &&
4805 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4806 Depth + 1);
4807 }
4808
4809 case ISD::SRL: {
4810 // A logical right-shift of a constant sign-bit will have exactly
4811 // one bit set.
4812 auto *C = isConstOrConstSplat(Val.getOperand(0), DemandedElts);
4813 if (C && C->getAPIntValue().isSignMask())
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::TRUNCATE:
4821 return (OrZero || isKnownNeverZero(Val, DemandedElts, Depth)) &&
4822 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4823 Depth + 1);
4824
4825 case ISD::ROTL:
4826 case ISD::ROTR:
4827 return isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4828 Depth + 1);
4829 case ISD::BSWAP:
4830 case ISD::BITREVERSE:
4831 return isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4832 Depth + 1);
4833
4834 case ISD::SMIN:
4835 case ISD::SMAX:
4836 case ISD::UMIN:
4837 case ISD::UMAX:
4838 return isKnownToBeAPowerOfTwo(Val.getOperand(1), DemandedElts, OrZero,
4839 Depth + 1) &&
4840 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4841 Depth + 1);
4842
4843 case ISD::SELECT:
4844 case ISD::VSELECT:
4845 return isKnownToBeAPowerOfTwo(Val.getOperand(2), DemandedElts, OrZero,
4846 Depth + 1) &&
4847 isKnownToBeAPowerOfTwo(Val.getOperand(1), DemandedElts, OrZero,
4848 Depth + 1);
4849
4850 case ISD::ZERO_EXTEND:
4851 return isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
4852 Depth + 1);
4853
4854 case ISD::VSCALE:
4855 // vscale(power-of-two) is a power-of-two
4856 return isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
4857 Depth + 1);
4858
4859 case ISD::VECTOR_SHUFFLE: {
4861 // Demanded elements with undef shuffle mask elements are unknown
4862 // - we cannot guarantee they are a power of two, so return false.
4863 APInt DemandedLHS, DemandedRHS;
4865 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4866 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4867 DemandedLHS, DemandedRHS))
4868 return false;
4869
4870 // All demanded elements from LHS must be known power of two.
4871 if (!!DemandedLHS && !isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedLHS,
4872 OrZero, Depth + 1))
4873 return false;
4874
4875 // All demanded elements from RHS must be known power of two.
4876 if (!!DemandedRHS && !isKnownToBeAPowerOfTwo(Val.getOperand(1), DemandedRHS,
4877 OrZero, Depth + 1))
4878 return false;
4879
4880 return true;
4881 }
4882 }
4883
4884 // More could be done here, though the above checks are enough
4885 // to handle some common cases.
4886 return false;
4887}
4888
4890 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4891 return C1->getValueAPF().getExactLog2Abs() >= 0;
4892
4893 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4894 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4895
4896 return false;
4897}
4898
4900 APInt DemandedElts = getDemandAllEltsMask(Op);
4901 return ComputeNumSignBits(Op, DemandedElts, Depth);
4902}
4903
4904unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4905 unsigned Depth) const {
4906 EVT VT = Op.getValueType();
4907 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4908 unsigned VTBits = VT.getScalarSizeInBits();
4909 unsigned NumElts = DemandedElts.getBitWidth();
4910 unsigned Tmp, Tmp2;
4911 unsigned FirstAnswer = 1;
4912
4913 assert((!VT.isScalableVector() || NumElts == 1) &&
4914 "DemandedElts for scalable vectors must be 1 to represent all lanes");
4915
4916 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4917 const APInt &Val = C->getAPIntValue();
4918 return Val.getNumSignBits();
4919 }
4920
4921 if (Depth >= MaxRecursionDepth)
4922 return 1; // Limit search depth.
4923
4924 if (!DemandedElts)
4925 return 1; // No demanded elts, better to assume we don't know anything.
4926
4927 unsigned Opcode = Op.getOpcode();
4928 switch (Opcode) {
4929 default: break;
4930 case ISD::AssertSext:
4931 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4932 return VTBits-Tmp+1;
4933 case ISD::AssertZext:
4934 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4935 return VTBits-Tmp;
4936 case ISD::FREEZE:
4937 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4938 /*PoisonOnly=*/false))
4939 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4940 break;
4941 case ISD::MERGE_VALUES:
4942 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4943 Depth + 1);
4944 case ISD::SPLAT_VECTOR: {
4945 // Check if the sign bits of source go down as far as the truncated value.
4946 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4947 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4948 if (NumSrcSignBits > (NumSrcBits - VTBits))
4949 return NumSrcSignBits - (NumSrcBits - VTBits);
4950 break;
4951 }
4952 case ISD::BUILD_VECTOR:
4953 assert(!VT.isScalableVector());
4954 Tmp = VTBits;
4955 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4956 if (!DemandedElts[i])
4957 continue;
4958
4959 SDValue SrcOp = Op.getOperand(i);
4960 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4961 // for constant nodes to ensure we only look at the sign bits.
4963 APInt T = C->getAPIntValue().trunc(VTBits);
4964 Tmp2 = T.getNumSignBits();
4965 } else {
4966 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4967
4968 if (SrcOp.getValueSizeInBits() != VTBits) {
4969 assert(SrcOp.getValueSizeInBits() > VTBits &&
4970 "Expected BUILD_VECTOR implicit truncation");
4971 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4972 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4973 }
4974 }
4975 Tmp = std::min(Tmp, Tmp2);
4976 }
4977 return Tmp;
4978
4979 case ISD::VECTOR_COMPRESS: {
4980 SDValue Vec = Op.getOperand(0);
4981 SDValue PassThru = Op.getOperand(2);
4982 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4983 if (Tmp == 1)
4984 return 1;
4985 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
4986 Tmp = std::min(Tmp, Tmp2);
4987 return Tmp;
4988 }
4989
4990 case ISD::VECTOR_SHUFFLE: {
4991 // Collect the minimum number of sign bits that are shared by every vector
4992 // element referenced by the shuffle.
4993 APInt DemandedLHS, DemandedRHS;
4995 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4996 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4997 DemandedLHS, DemandedRHS))
4998 return 1;
4999
5000 Tmp = std::numeric_limits<unsigned>::max();
5001 if (!!DemandedLHS)
5002 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
5003 if (!!DemandedRHS) {
5004 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
5005 Tmp = std::min(Tmp, Tmp2);
5006 }
5007 // If we don't know anything, early out and try computeKnownBits fall-back.
5008 if (Tmp == 1)
5009 break;
5010 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5011 return Tmp;
5012 }
5013
5014 case ISD::BITCAST: {
5015 if (VT.isScalableVector())
5016 break;
5017 SDValue N0 = Op.getOperand(0);
5018 EVT SrcVT = N0.getValueType();
5019 unsigned SrcBits = SrcVT.getScalarSizeInBits();
5020
5021 // Ignore bitcasts from unsupported types..
5022 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
5023 break;
5024
5025 // Fast handling of 'identity' bitcasts.
5026 if (VTBits == SrcBits)
5027 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
5028
5029 bool IsLE = getDataLayout().isLittleEndian();
5030
5031 // Bitcast 'large element' scalar/vector to 'small element' vector.
5032 if ((SrcBits % VTBits) == 0) {
5033 assert(VT.isVector() && "Expected bitcast to vector");
5034
5035 unsigned Scale = SrcBits / VTBits;
5036 APInt SrcDemandedElts =
5037 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
5038
5039 // Fast case - sign splat can be simply split across the small elements.
5040 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
5041 if (Tmp == SrcBits)
5042 return VTBits;
5043
5044 // Slow case - determine how far the sign extends into each sub-element.
5045 Tmp2 = VTBits;
5046 for (unsigned i = 0; i != NumElts; ++i)
5047 if (DemandedElts[i]) {
5048 unsigned SubOffset = i % Scale;
5049 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
5050 SubOffset = SubOffset * VTBits;
5051 if (Tmp <= SubOffset)
5052 return 1;
5053 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
5054 }
5055 return Tmp2;
5056 }
5057 break;
5058 }
5059
5061 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
5062 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
5063 return VTBits - Tmp + 1;
5064 case ISD::SIGN_EXTEND:
5065 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
5066 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
5068 // Max of the input and what this extends.
5069 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
5070 Tmp = VTBits-Tmp+1;
5071 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
5072 return std::max(Tmp, Tmp2);
5074 if (VT.isScalableVector())
5075 break;
5076 SDValue Src = Op.getOperand(0);
5077 EVT SrcVT = Src.getValueType();
5078 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
5079 Tmp = VTBits - SrcVT.getScalarSizeInBits();
5080 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
5081 }
5082 case ISD::SRA:
5083 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5084 // SRA X, C -> adds C sign bits.
5085 if (std::optional<unsigned> ShAmt =
5086 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
5087 Tmp = std::min(Tmp + *ShAmt, VTBits);
5088 return Tmp;
5089 case ISD::SHL:
5090 if (std::optional<ConstantRange> ShAmtRange =
5091 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
5092 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
5093 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
5094 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
5095 // shifted out, then we can compute the number of sign bits for the
5096 // operand being extended. A future improvement could be to pass along the
5097 // "shifted left by" information in the recursive calls to
5098 // ComputeKnownSignBits. Allowing us to handle this more generically.
5099 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
5100 SDValue Ext = Op.getOperand(0);
5101 EVT ExtVT = Ext.getValueType();
5102 SDValue Extendee = Ext.getOperand(0);
5103 EVT ExtendeeVT = Extendee.getValueType();
5104 unsigned SizeDifference =
5105 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
5106 if (SizeDifference <= MinShAmt) {
5107 Tmp = SizeDifference +
5108 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
5109 if (MaxShAmt < Tmp)
5110 return Tmp - MaxShAmt;
5111 }
5112 }
5113 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
5114 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5115 if (MaxShAmt < Tmp)
5116 return Tmp - MaxShAmt;
5117 }
5118 break;
5119 case ISD::AND:
5120 case ISD::OR:
5121 case ISD::XOR: // NOT is handled here.
5122 // Logical binary ops preserve the number of sign bits at the worst.
5123 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
5124 if (Tmp != 1) {
5125 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
5126 FirstAnswer = std::min(Tmp, Tmp2);
5127 // We computed what we know about the sign bits as our first
5128 // answer. Now proceed to the generic code that uses
5129 // computeKnownBits, and pick whichever answer is better.
5130 }
5131 break;
5132
5133 case ISD::SELECT:
5134 case ISD::VSELECT:
5135 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
5136 if (Tmp == 1) return 1; // Early out.
5137 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
5138 return std::min(Tmp, Tmp2);
5139 case ISD::SELECT_CC:
5140 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
5141 if (Tmp == 1) return 1; // Early out.
5142 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
5143 return std::min(Tmp, Tmp2);
5144
5145 case ISD::SMIN:
5146 case ISD::SMAX: {
5147 // If we have a clamp pattern, we know that the number of sign bits will be
5148 // the minimum of the clamp min/max range.
5149 bool IsMax = (Opcode == ISD::SMAX);
5150 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
5151 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
5152 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
5153 CstHigh =
5154 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
5155 if (CstLow && CstHigh) {
5156 if (!IsMax)
5157 std::swap(CstLow, CstHigh);
5158 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
5159 Tmp = CstLow->getAPIntValue().getNumSignBits();
5160 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
5161 return std::min(Tmp, Tmp2);
5162 }
5163 }
5164
5165 // Fallback - just get the minimum number of sign bits of the operands.
5166 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5167 if (Tmp == 1)
5168 return 1; // Early out.
5169 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5170 return std::min(Tmp, Tmp2);
5171 }
5172 case ISD::UMIN:
5173 case ISD::UMAX:
5174 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5175 if (Tmp == 1)
5176 return 1; // Early out.
5177 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5178 return std::min(Tmp, Tmp2);
5179 case ISD::SSUBO_CARRY:
5180 case ISD::USUBO_CARRY:
5181 // sub_carry(x,x,c) -> 0/-1 (sext carry)
5182 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
5183 return VTBits;
5184 [[fallthrough]];
5185 case ISD::SADDO:
5186 case ISD::UADDO:
5187 case ISD::SADDO_CARRY:
5188 case ISD::UADDO_CARRY:
5189 case ISD::SSUBO:
5190 case ISD::USUBO:
5191 case ISD::SMULO:
5192 case ISD::UMULO:
5193 if (Op.getResNo() != 1)
5194 break;
5195 // The boolean result conforms to getBooleanContents. Fall through.
5196 // If setcc returns 0/-1, all bits are sign bits.
5197 // We know that we have an integer-based boolean since these operations
5198 // are only available for integer.
5199 if (TLI->getBooleanContents(VT.isVector(), false) ==
5201 return VTBits;
5202 break;
5203 case ISD::SETCC:
5204 case ISD::SETCCCARRY:
5205 case ISD::STRICT_FSETCC:
5206 case ISD::STRICT_FSETCCS: {
5207 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
5208 // If setcc returns 0/-1, all bits are sign bits.
5209 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
5211 return VTBits;
5212 break;
5213 }
5214 case ISD::ROTL:
5215 case ISD::ROTR:
5216 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5217
5218 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
5219 if (Tmp == VTBits)
5220 return VTBits;
5221
5222 if (ConstantSDNode *C =
5223 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
5224 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
5225
5226 // Handle rotate right by N like a rotate left by 32-N.
5227 if (Opcode == ISD::ROTR)
5228 RotAmt = (VTBits - RotAmt) % VTBits;
5229
5230 // If we aren't rotating out all of the known-in sign bits, return the
5231 // number that are left. This handles rotl(sext(x), 1) for example.
5232 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
5233 }
5234 break;
5235 case ISD::ADD:
5236 case ISD::ADDC:
5237 // TODO: Move Operand 1 check before Operand 0 check
5238 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5239 if (Tmp == 1) return 1; // Early out.
5240
5241 // Special case decrementing a value (ADD X, -1):
5242 if (ConstantSDNode *CRHS =
5243 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5244 if (CRHS->isAllOnes()) {
5245 KnownBits Known =
5246 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5247
5248 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5249 // sign bits set.
5250 if ((Known.Zero | 1).isAllOnes())
5251 return VTBits;
5252
5253 // If we are subtracting one from a positive number, there is no carry
5254 // out of the result.
5255 if (Known.isNonNegative())
5256 return Tmp;
5257 }
5258
5259 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5260 if (Tmp2 == 1) return 1; // Early out.
5261
5262 // Add can have at most one carry bit. Thus we know that the output
5263 // is, at worst, one more bit than the inputs.
5264 return std::min(Tmp, Tmp2) - 1;
5265 case ISD::SUB:
5266 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5267 if (Tmp2 == 1) return 1; // Early out.
5268
5269 // Handle NEG.
5270 if (ConstantSDNode *CLHS =
5271 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5272 if (CLHS->isZero()) {
5273 KnownBits Known =
5274 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5275 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5276 // sign bits set.
5277 if ((Known.Zero | 1).isAllOnes())
5278 return VTBits;
5279
5280 // If the input is known to be positive (the sign bit is known clear),
5281 // the output of the NEG has the same number of sign bits as the input.
5282 if (Known.isNonNegative())
5283 return Tmp2;
5284
5285 // Otherwise, we treat this like a SUB.
5286 }
5287
5288 // Sub can have at most one carry bit. Thus we know that the output
5289 // is, at worst, one more bit than the inputs.
5290 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5291 if (Tmp == 1) return 1; // Early out.
5292 return std::min(Tmp, Tmp2) - 1;
5293 case ISD::MUL: {
5294 // The output of the Mul can be at most twice the valid bits in the inputs.
5295 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5296 if (SignBitsOp0 == 1)
5297 break;
5298 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5299 if (SignBitsOp1 == 1)
5300 break;
5301 unsigned OutValidBits =
5302 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5303 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5304 }
5305 case ISD::AVGCEILS:
5306 case ISD::AVGFLOORS:
5307 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5308 if (Tmp == 1)
5309 return 1; // Early out.
5310 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5311 return std::min(Tmp, Tmp2);
5312 case ISD::SREM:
5313 // The sign bit is the LHS's sign bit, except when the result of the
5314 // remainder is zero. The magnitude of the result should be less than or
5315 // equal to the magnitude of the LHS. Therefore, the result should have
5316 // at least as many sign bits as the left hand side.
5317 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5318 case ISD::TRUNCATE: {
5319 // Check if the sign bits of source go down as far as the truncated value.
5320 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5321 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5322 if (NumSrcSignBits > (NumSrcBits - VTBits))
5323 return NumSrcSignBits - (NumSrcBits - VTBits);
5324 break;
5325 }
5326 case ISD::EXTRACT_ELEMENT: {
5327 if (VT.isScalableVector())
5328 break;
5329 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5330 const int BitWidth = Op.getValueSizeInBits();
5331 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5332
5333 // Get reverse index (starting from 1), Op1 value indexes elements from
5334 // little end. Sign starts at big end.
5335 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5336
5337 // If the sign portion ends in our element the subtraction gives correct
5338 // result. Otherwise it gives either negative or > bitwidth result
5339 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5340 }
5342 if (VT.isScalableVector())
5343 break;
5344 // If we know the element index, split the demand between the
5345 // source vector and the inserted element, otherwise assume we need
5346 // the original demanded vector elements and the value.
5347 SDValue InVec = Op.getOperand(0);
5348 SDValue InVal = Op.getOperand(1);
5349 SDValue EltNo = Op.getOperand(2);
5350 bool DemandedVal = true;
5351 APInt DemandedVecElts = DemandedElts;
5352 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5353 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5354 unsigned EltIdx = CEltNo->getZExtValue();
5355 DemandedVal = !!DemandedElts[EltIdx];
5356 DemandedVecElts.clearBit(EltIdx);
5357 }
5358 Tmp = std::numeric_limits<unsigned>::max();
5359 if (DemandedVal) {
5360 // TODO - handle implicit truncation of inserted elements.
5361 if (InVal.getScalarValueSizeInBits() != VTBits)
5362 break;
5363 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5364 Tmp = std::min(Tmp, Tmp2);
5365 }
5366 if (!!DemandedVecElts) {
5367 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5368 Tmp = std::min(Tmp, Tmp2);
5369 }
5370 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5371 return Tmp;
5372 }
5374 SDValue InVec = Op.getOperand(0);
5375 SDValue EltNo = Op.getOperand(1);
5376 EVT VecVT = InVec.getValueType();
5377 // ComputeNumSignBits not yet implemented for scalable vectors.
5378 if (VecVT.isScalableVector())
5379 break;
5380 const unsigned BitWidth = Op.getValueSizeInBits();
5381 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5382 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5383
5384 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5385 // anything about sign bits. But if the sizes match we can derive knowledge
5386 // about sign bits from the vector operand.
5387 if (BitWidth != EltBitWidth)
5388 break;
5389
5390 // If we know the element index, just demand that vector element, else for
5391 // an unknown element index, ignore DemandedElts and demand them all.
5392 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5393 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5394 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5395 DemandedSrcElts =
5396 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5397
5398 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5399 }
5401 // Offset the demanded elts by the subvector index.
5402 SDValue Src = Op.getOperand(0);
5403
5404 APInt DemandedSrcElts;
5405 if (Src.getValueType().isScalableVector())
5406 DemandedSrcElts = APInt(1, 1);
5407 else {
5408 uint64_t Idx = Op.getConstantOperandVal(1);
5409 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5410 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5411 }
5412 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5413 }
5414 case ISD::CONCAT_VECTORS: {
5415 if (VT.isScalableVector())
5416 break;
5417 // Determine the minimum number of sign bits across all demanded
5418 // elts of the input vectors. Early out if the result is already 1.
5419 Tmp = std::numeric_limits<unsigned>::max();
5420 EVT SubVectorVT = Op.getOperand(0).getValueType();
5421 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5422 unsigned NumSubVectors = Op.getNumOperands();
5423 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5424 APInt DemandedSub =
5425 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5426 if (!DemandedSub)
5427 continue;
5428 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5429 Tmp = std::min(Tmp, Tmp2);
5430 }
5431 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5432 return Tmp;
5433 }
5434 case ISD::INSERT_SUBVECTOR: {
5435 if (VT.isScalableVector())
5436 break;
5437 // Demand any elements from the subvector and the remainder from the src its
5438 // inserted into.
5439 SDValue Src = Op.getOperand(0);
5440 SDValue Sub = Op.getOperand(1);
5441 uint64_t Idx = Op.getConstantOperandVal(2);
5442 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5443 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5444 APInt DemandedSrcElts = DemandedElts;
5445 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5446
5447 Tmp = std::numeric_limits<unsigned>::max();
5448 if (!!DemandedSubElts) {
5449 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5450 if (Tmp == 1)
5451 return 1; // early-out
5452 }
5453 if (!!DemandedSrcElts) {
5454 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5455 Tmp = std::min(Tmp, Tmp2);
5456 }
5457 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5458 return Tmp;
5459 }
5460 case ISD::LOAD: {
5461 // If we are looking at the loaded value of the SDNode.
5462 if (Op.getResNo() != 0)
5463 break;
5464
5466 if (const MDNode *Ranges = LD->getRanges()) {
5467 if (DemandedElts != 1)
5468 break;
5469
5471 if (VTBits > CR.getBitWidth()) {
5472 switch (LD->getExtensionType()) {
5473 case ISD::SEXTLOAD:
5474 CR = CR.signExtend(VTBits);
5475 break;
5476 case ISD::ZEXTLOAD:
5477 CR = CR.zeroExtend(VTBits);
5478 break;
5479 default:
5480 break;
5481 }
5482 }
5483
5484 if (VTBits != CR.getBitWidth())
5485 break;
5486 return std::min(CR.getSignedMin().getNumSignBits(),
5488 }
5489
5490 unsigned ExtType = LD->getExtensionType();
5491 switch (ExtType) {
5492 default:
5493 break;
5494 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5495 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5496 return VTBits - Tmp + 1;
5497 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5498 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5499 return VTBits - Tmp;
5500 case ISD::NON_EXTLOAD:
5501 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5502 // We only need to handle vectors - computeKnownBits should handle
5503 // scalar cases.
5504 Type *CstTy = Cst->getType();
5505 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5506 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5507 VTBits == CstTy->getScalarSizeInBits()) {
5508 Tmp = VTBits;
5509 for (unsigned i = 0; i != NumElts; ++i) {
5510 if (!DemandedElts[i])
5511 continue;
5512 if (Constant *Elt = Cst->getAggregateElement(i)) {
5513 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5514 const APInt &Value = CInt->getValue();
5515 Tmp = std::min(Tmp, Value.getNumSignBits());
5516 continue;
5517 }
5518 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5519 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5520 Tmp = std::min(Tmp, Value.getNumSignBits());
5521 continue;
5522 }
5523 }
5524 // Unknown type. Conservatively assume no bits match sign bit.
5525 return 1;
5526 }
5527 return Tmp;
5528 }
5529 }
5530 break;
5531 }
5532
5533 break;
5534 }
5537 case ISD::ATOMIC_SWAP:
5549 case ISD::ATOMIC_LOAD: {
5550 auto *AT = cast<AtomicSDNode>(Op);
5551 // If we are looking at the loaded value.
5552 if (Op.getResNo() == 0) {
5553 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5554 if (Tmp == VTBits)
5555 return 1; // early-out
5556
5557 // For atomic_load, prefer to use the extension type.
5558 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5559 switch (AT->getExtensionType()) {
5560 default:
5561 break;
5562 case ISD::SEXTLOAD:
5563 return VTBits - Tmp + 1;
5564 case ISD::ZEXTLOAD:
5565 return VTBits - Tmp;
5566 }
5567 }
5568
5569 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5570 return VTBits - Tmp + 1;
5571 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5572 return VTBits - Tmp;
5573 }
5574 break;
5575 }
5576 }
5577
5578 // Allow the target to implement this method for its nodes.
5579 if (Opcode >= ISD::BUILTIN_OP_END ||
5580 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5581 Opcode == ISD::INTRINSIC_W_CHAIN ||
5582 Opcode == ISD::INTRINSIC_VOID) {
5583 // TODO: This can probably be removed once target code is audited. This
5584 // is here purely to reduce patch size and review complexity.
5585 if (!VT.isScalableVector()) {
5586 unsigned NumBits =
5587 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5588 if (NumBits > 1)
5589 FirstAnswer = std::max(FirstAnswer, NumBits);
5590 }
5591 }
5592
5593 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5594 // use this information.
5595 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5596 return std::max(FirstAnswer, Known.countMinSignBits());
5597}
5598
5600 unsigned Depth) const {
5601 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5602 return Op.getScalarValueSizeInBits() - SignBits + 1;
5603}
5604
5606 const APInt &DemandedElts,
5607 unsigned Depth) const {
5608 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5609 return Op.getScalarValueSizeInBits() - SignBits + 1;
5610}
5611
5613 unsigned Depth) const {
5614 // Early out for FREEZE.
5615 if (Op.getOpcode() == ISD::FREEZE)
5616 return true;
5617
5618 APInt DemandedElts = getDemandAllEltsMask(Op);
5619 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5620}
5621
5623 const APInt &DemandedElts,
5624 bool PoisonOnly,
5625 unsigned Depth) const {
5626 unsigned Opcode = Op.getOpcode();
5627
5628 // Early out for FREEZE.
5629 if (Opcode == ISD::FREEZE)
5630 return true;
5631
5632 if (Depth >= MaxRecursionDepth)
5633 return false; // Limit search depth.
5634
5635 if (isIntOrFPConstant(Op))
5636 return true;
5637
5638 switch (Opcode) {
5639 case ISD::CONDCODE:
5640 case ISD::VALUETYPE:
5641 case ISD::FrameIndex:
5643 case ISD::CopyFromReg:
5644 return true;
5645
5646 case ISD::POISON:
5647 return false;
5648
5649 case ISD::UNDEF:
5650 return PoisonOnly;
5651
5652 case ISD::BUILD_VECTOR:
5653 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5654 // this shouldn't affect the result.
5655 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5656 if (!DemandedElts[i])
5657 continue;
5659 Depth + 1))
5660 return false;
5661 }
5662 return true;
5663
5665 SDValue Src = Op.getOperand(0);
5666 if (Src.getValueType().isScalableVector())
5667 break;
5668 uint64_t Idx = Op.getConstantOperandVal(1);
5669 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5670 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5671 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5672 Depth + 1);
5673 }
5674
5675 case ISD::INSERT_SUBVECTOR: {
5676 if (Op.getValueType().isScalableVector())
5677 break;
5678 SDValue Src = Op.getOperand(0);
5679 SDValue Sub = Op.getOperand(1);
5680 uint64_t Idx = Op.getConstantOperandVal(2);
5681 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5682 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5683 APInt DemandedSrcElts = DemandedElts;
5684 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5685
5686 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5687 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5688 return false;
5689 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5690 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5691 return false;
5692 return true;
5693 }
5694
5696 SDValue Src = Op.getOperand(0);
5697 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5698 EVT SrcVT = Src.getValueType();
5699 if (SrcVT.isFixedLengthVector() && IndexC &&
5700 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5701 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5702 IndexC->getZExtValue());
5703 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5704 Depth + 1);
5705 }
5706 break;
5707 }
5708
5710 SDValue InVec = Op.getOperand(0);
5711 SDValue InVal = Op.getOperand(1);
5712 SDValue EltNo = Op.getOperand(2);
5713 EVT VT = InVec.getValueType();
5714 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5715 if (IndexC && VT.isFixedLengthVector() &&
5716 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5717 if (DemandedElts[IndexC->getZExtValue()] &&
5719 return false;
5720 APInt InVecDemandedElts = DemandedElts;
5721 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5722 if (!!InVecDemandedElts &&
5724 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5725 InVecDemandedElts, PoisonOnly, Depth + 1))
5726 return false;
5727 return true;
5728 }
5729 break;
5730 }
5731
5733 // Check upper (known undef) elements.
5734 if (DemandedElts.ugt(1) && !PoisonOnly)
5735 return false;
5736 // Check element zero.
5737 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5738 Op.getOperand(0), PoisonOnly, Depth + 1))
5739 return false;
5740 return true;
5741
5742 case ISD::SPLAT_VECTOR:
5743 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5744 Depth + 1);
5745
5746 case ISD::VECTOR_SHUFFLE: {
5747 APInt DemandedLHS, DemandedRHS;
5748 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5749 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5750 DemandedElts, DemandedLHS, DemandedRHS,
5751 /*AllowUndefElts=*/false))
5752 return false;
5753 if (!DemandedLHS.isZero() &&
5754 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5755 PoisonOnly, Depth + 1))
5756 return false;
5757 if (!DemandedRHS.isZero() &&
5758 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5759 PoisonOnly, Depth + 1))
5760 return false;
5761 return true;
5762 }
5763
5764 case ISD::SHL:
5765 case ISD::SRL:
5766 case ISD::SRA:
5767 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5768 // enough to check operand 0 if Op can't create undef/poison.
5769 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5770 /*ConsiderFlags*/ true, Depth) &&
5771 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5772 PoisonOnly, Depth + 1);
5773
5774 case ISD::BSWAP:
5775 case ISD::CTPOP:
5776 case ISD::BITREVERSE:
5777 case ISD::AND:
5778 case ISD::OR:
5779 case ISD::XOR:
5780 case ISD::ADD:
5781 case ISD::SUB:
5782 case ISD::MUL:
5783 case ISD::SADDSAT:
5784 case ISD::UADDSAT:
5785 case ISD::SSUBSAT:
5786 case ISD::USUBSAT:
5787 case ISD::SSHLSAT:
5788 case ISD::USHLSAT:
5789 case ISD::SMIN:
5790 case ISD::SMAX:
5791 case ISD::UMIN:
5792 case ISD::UMAX:
5793 case ISD::ZERO_EXTEND:
5794 case ISD::SIGN_EXTEND:
5795 case ISD::ANY_EXTEND:
5796 case ISD::TRUNCATE:
5797 case ISD::VSELECT: {
5798 // If Op can't create undef/poison and none of its operands are undef/poison
5799 // then Op is never undef/poison. A difference from the more common check
5800 // below, outside the switch, is that we handle elementwise operations for
5801 // which the DemandedElts mask is valid for all operands here.
5802 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5803 /*ConsiderFlags*/ true, Depth) &&
5804 all_of(Op->ops(), [&](SDValue V) {
5805 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5806 PoisonOnly, Depth + 1);
5807 });
5808 }
5809
5810 // TODO: Search for noundef attributes from library functions.
5811
5812 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5813
5814 default:
5815 // Allow the target to implement this method for its nodes.
5816 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5817 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5818 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5819 Op, DemandedElts, *this, PoisonOnly, Depth);
5820 break;
5821 }
5822
5823 // If Op can't create undef/poison and none of its operands are undef/poison
5824 // then Op is never undef/poison.
5825 // NOTE: TargetNodes can handle this in themselves in
5826 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5827 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5828 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5829 Depth) &&
5830 all_of(Op->ops(), [&](SDValue V) {
5831 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5832 });
5833}
5834
5836 bool ConsiderFlags,
5837 unsigned Depth) const {
5838 APInt DemandedElts = getDemandAllEltsMask(Op);
5839 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5840 Depth);
5841}
5842
5844 bool PoisonOnly, bool ConsiderFlags,
5845 unsigned Depth) const {
5846 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5847 return true;
5848
5849 unsigned Opcode = Op.getOpcode();
5850 switch (Opcode) {
5851 case ISD::AssertSext:
5852 case ISD::AssertZext:
5853 case ISD::AssertAlign:
5855 // Assertion nodes can create poison if the assertion fails.
5856 return true;
5857
5858 case ISD::FREEZE:
5862 case ISD::SADDSAT:
5863 case ISD::UADDSAT:
5864 case ISD::SSUBSAT:
5865 case ISD::USUBSAT:
5866 case ISD::MULHU:
5867 case ISD::MULHS:
5868 case ISD::AVGFLOORS:
5869 case ISD::AVGFLOORU:
5870 case ISD::AVGCEILS:
5871 case ISD::AVGCEILU:
5872 case ISD::ABDU:
5873 case ISD::ABDS:
5874 case ISD::SMIN:
5875 case ISD::SMAX:
5876 case ISD::SCMP:
5877 case ISD::UMIN:
5878 case ISD::UMAX:
5879 case ISD::UCMP:
5880 case ISD::AND:
5881 case ISD::XOR:
5882 case ISD::ROTL:
5883 case ISD::ROTR:
5884 case ISD::FSHL:
5885 case ISD::FSHR:
5886 case ISD::BSWAP:
5887 case ISD::CTTZ:
5888 case ISD::CTLZ:
5889 case ISD::CTLS:
5890 case ISD::CTPOP:
5891 case ISD::BITREVERSE:
5892 case ISD::PARITY:
5893 case ISD::SIGN_EXTEND:
5894 case ISD::TRUNCATE:
5898 case ISD::BITCAST:
5899 case ISD::BUILD_VECTOR:
5900 case ISD::BUILD_PAIR:
5901 case ISD::SPLAT_VECTOR:
5902 case ISD::FABS:
5903 return false;
5904
5905 case ISD::ABS:
5906 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5907 // Different to Intrinsic::abs.
5908 return false;
5909
5910 case ISD::ADDC:
5911 case ISD::SUBC:
5912 case ISD::ADDE:
5913 case ISD::SUBE:
5914 case ISD::SADDO:
5915 case ISD::SSUBO:
5916 case ISD::SMULO:
5917 case ISD::SADDO_CARRY:
5918 case ISD::SSUBO_CARRY:
5919 case ISD::UADDO:
5920 case ISD::USUBO:
5921 case ISD::UMULO:
5922 case ISD::UADDO_CARRY:
5923 case ISD::USUBO_CARRY:
5924 // No poison on result or overflow flags.
5925 return false;
5926
5927 case ISD::SELECT_CC:
5928 case ISD::SETCC: {
5929 // Integer setcc cannot create undef or poison.
5930 if (Op.getOperand(0).getValueType().isInteger())
5931 return false;
5932
5933 // FP compares are more complicated. They can create poison for nan/infinity
5934 // based on options and flags. The options and flags also cause special
5935 // nonan condition codes to be used. Those condition codes may be preserved
5936 // even if the nonan flag is dropped somewhere.
5937 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5938 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5939 return (unsigned)CCCode & 0x10U;
5940 }
5941
5942 case ISD::OR:
5943 case ISD::ZERO_EXTEND:
5944 case ISD::SELECT:
5945 case ISD::VSELECT:
5946 case ISD::ADD:
5947 case ISD::SUB:
5948 case ISD::MUL:
5949 case ISD::FNEG:
5950 case ISD::FADD:
5951 case ISD::FSUB:
5952 case ISD::FMUL:
5953 case ISD::FDIV:
5954 case ISD::FREM:
5955 case ISD::FCOPYSIGN:
5956 case ISD::FMA:
5957 case ISD::FMAD:
5958 case ISD::FMULADD:
5959 case ISD::FP_EXTEND:
5965 // No poison except from flags (which is handled above)
5966 return false;
5967
5968 case ISD::SHL:
5969 case ISD::SRL:
5970 case ISD::SRA:
5971 // If the max shift amount isn't in range, then the shift can
5972 // create poison.
5973 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5974
5977 // If the amount is zero then the result will be poison.
5978 // TODO: Add isKnownNeverZero DemandedElts handling.
5979 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5980
5982 // Check if we demand any upper (undef) elements.
5983 return !PoisonOnly && DemandedElts.ugt(1);
5984
5987 // Ensure that the element index is in bounds.
5988 EVT VecVT = Op.getOperand(0).getValueType();
5989 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5990 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5991 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5992 }
5993
5994 case ISD::VECTOR_SHUFFLE: {
5995 // Check for any demanded shuffle element that is undef.
5996 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5997 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5998 if (Elt < 0 && DemandedElts[Idx])
5999 return true;
6000 return false;
6001 }
6002
6004 return false;
6005
6006 default:
6007 // Allow the target to implement this method for its nodes.
6008 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6009 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
6010 return TLI->canCreateUndefOrPoisonForTargetNode(
6011 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
6012 break;
6013 }
6014
6015 // Be conservative and return true.
6016 return true;
6017}
6018
6019bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
6020 unsigned Opcode = Op.getOpcode();
6021 if (Opcode == ISD::OR)
6022 return Op->getFlags().hasDisjoint() ||
6023 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
6024 if (Opcode == ISD::XOR)
6025 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
6026 return false;
6027}
6028
6030 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
6031 (Op.isAnyAdd() || isADDLike(Op));
6032}
6033
6035 FPClassTest InterestedClasses,
6036 unsigned Depth) const {
6037 APInt DemandedElts = getDemandAllEltsMask(Op);
6038 return computeKnownFPClass(Op, DemandedElts, InterestedClasses, Depth);
6039}
6040
6042 const APInt &DemandedElts,
6043 FPClassTest InterestedClasses,
6044 unsigned Depth) const {
6045 KnownFPClass Known;
6046
6047 if (const auto *CFP = dyn_cast<ConstantFPSDNode>(Op))
6048 return KnownFPClass(CFP->getValueAPF());
6049
6050 if (Depth >= MaxRecursionDepth)
6051 return Known;
6052
6053 if (Op.getOpcode() == ISD::UNDEF)
6054 return Known;
6055
6056 EVT VT = Op.getValueType();
6057 assert(VT.isFloatingPoint() && "Computing KnownFPClass on non-FP op!");
6058 assert((!VT.isFixedLengthVector() ||
6059 DemandedElts.getBitWidth() == VT.getVectorNumElements()) &&
6060 "Unexpected vector size");
6061
6062 if (!DemandedElts)
6063 return Known;
6064
6065 unsigned Opcode = Op.getOpcode();
6066 switch (Opcode) {
6067 case ISD::POISON: {
6068 Known.KnownFPClasses = fcNone;
6069 Known.SignBit = false;
6070 break;
6071 }
6072 case ISD::FNEG: {
6073 Known = computeKnownFPClass(Op.getOperand(0), DemandedElts,
6074 InterestedClasses, Depth + 1);
6075 Known.fneg();
6076 break;
6077 }
6078 case ISD::BUILD_VECTOR: {
6079 assert(!VT.isScalableVector());
6080 bool First = true;
6081 for (unsigned I = 0, E = Op.getNumOperands(); I != E; ++I) {
6082 if (!DemandedElts[I])
6083 continue;
6084
6085 if (First) {
6086 Known =
6087 computeKnownFPClass(Op.getOperand(I), InterestedClasses, Depth + 1);
6088 First = false;
6089 } else {
6090 Known |=
6091 computeKnownFPClass(Op.getOperand(I), InterestedClasses, Depth + 1);
6092 }
6093
6094 if (Known.isUnknown())
6095 break;
6096 }
6097 break;
6098 }
6100 SDValue Src = Op.getOperand(0);
6101 auto *CIdx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6102 EVT SrcVT = Src.getValueType();
6103 if (SrcVT.isFixedLengthVector() && CIdx) {
6104 if (CIdx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
6105 APInt DemandedSrcElts = APInt::getOneBitSet(
6106 SrcVT.getVectorNumElements(), CIdx->getZExtValue());
6107 Known = computeKnownFPClass(Src, DemandedSrcElts, InterestedClasses,
6108 Depth + 1);
6109 } else {
6110 // Out of bounds index is poison.
6111 Known.KnownFPClasses = fcNone;
6112 }
6113 } else {
6114 Known = computeKnownFPClass(Src, InterestedClasses, Depth + 1);
6115 }
6116 break;
6117 }
6118 case ISD::SPLAT_VECTOR: {
6119 Known = computeKnownFPClass(Op.getOperand(0), InterestedClasses, Depth + 1);
6120 break;
6121 }
6122 case ISD::BITCAST: {
6123 // FIXME: It should not be necessary to check for an elementwise bitcast.
6124 // If a bitcast is not elementwise between vector / scalar types,
6125 // computeKnownBits already splices the known bits of the source elements
6126 // appropriately so as to line up with the bits of the result's demanded
6127 // elements.
6128 EVT SrcVT = Op.getOperand(0).getValueType();
6129 if (VT.isScalableVector() || SrcVT.isScalableVector())
6130 break;
6131 unsigned VTNumElts = VT.isVector() ? VT.getVectorNumElements() : 1;
6132 unsigned SrcVTNumElts = SrcVT.isVector() ? SrcVT.getVectorNumElements() : 1;
6133 if (VTNumElts != SrcVTNumElts)
6134 break;
6135
6136 KnownBits Bits = computeKnownBits(Op, DemandedElts, Depth + 1);
6137 Known = KnownFPClass::bitcast(VT.getFltSemantics(), Bits);
6138 break;
6139 }
6140 case ISD::FABS: {
6141 Known = computeKnownFPClass(Op.getOperand(0), DemandedElts,
6142 InterestedClasses, Depth + 1);
6143 Known.fabs();
6144 break;
6145 }
6146 case ISD::FCOPYSIGN: {
6147 Known = computeKnownFPClass(Op.getOperand(0), DemandedElts,
6148 InterestedClasses, Depth + 1);
6149 KnownFPClass KnownSign = computeKnownFPClass(Op.getOperand(1), DemandedElts,
6150 InterestedClasses, Depth + 1);
6151 Known.copysign(KnownSign);
6152 break;
6153 }
6154 case ISD::AssertNoFPClass: {
6155 Known = computeKnownFPClass(Op.getOperand(0), DemandedElts,
6156 InterestedClasses, Depth + 1);
6157 FPClassTest AssertedClasses =
6158 static_cast<FPClassTest>(Op->getConstantOperandVal(1));
6159 Known.KnownFPClasses &= ~AssertedClasses;
6160 break;
6161 }
6163 SDValue Src = Op.getOperand(0);
6164 EVT SrcVT = Src.getValueType();
6165 if (SrcVT.isFixedLengthVector()) {
6166 unsigned Idx = Op.getConstantOperandVal(1);
6167 unsigned NumSrcElts = SrcVT.getVectorNumElements();
6168
6169 APInt DemandedSrcElts = DemandedElts.zextOrTrunc(NumSrcElts).shl(Idx);
6170 Known = computeKnownFPClass(Src, DemandedSrcElts, InterestedClasses,
6171 Depth + 1);
6172 } else {
6173 Known = computeKnownFPClass(Src, InterestedClasses, Depth + 1);
6174 }
6175 break;
6176 }
6177 case ISD::INSERT_SUBVECTOR: {
6178 SDValue BaseVector = Op.getOperand(0);
6179 SDValue SubVector = Op.getOperand(1);
6180 EVT BaseVT = BaseVector.getValueType();
6181 if (BaseVT.isFixedLengthVector()) {
6182 unsigned Idx = Op.getConstantOperandVal(2);
6183 unsigned NumBaseElts = BaseVT.getVectorNumElements();
6184 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
6185
6186 APInt DemandedMask =
6187 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
6188 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
6189 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
6190
6191 if (!DemandedSrcElts.isZero())
6192 Known = computeKnownFPClass(BaseVector, DemandedSrcElts,
6193 InterestedClasses, Depth + 1);
6194 if (!DemandedSubElts.isZero()) {
6196 SubVector, DemandedSubElts, InterestedClasses, Depth + 1);
6197 Known = DemandedSrcElts.isZero() ? SubKnown : (Known | SubKnown);
6198 }
6199 } else {
6200 Known = computeKnownFPClass(SubVector, InterestedClasses, Depth + 1);
6201 if (!Known.isUnknown())
6202 Known |= computeKnownFPClass(BaseVector, InterestedClasses, Depth + 1);
6203 }
6204 break;
6205 }
6206 case ISD::SELECT:
6207 case ISD::VSELECT: {
6208 // TODO: Add adjustKnownFPClassForSelectArm clamp recognition as in
6209 // IR-level ValueTracking.
6210 KnownFPClass KnownFalseClass = computeKnownFPClass(
6211 Op.getOperand(2), DemandedElts, InterestedClasses, Depth + 1);
6212 if (KnownFalseClass.isUnknown())
6213 break;
6214 KnownFPClass KnownTrueClass = computeKnownFPClass(
6215 Op.getOperand(1), DemandedElts, InterestedClasses, Depth + 1);
6216 Known = KnownTrueClass.intersectWith(KnownFalseClass);
6217 break;
6218 }
6219 default:
6220 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6221 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6222 TLI->computeKnownFPClassForTargetNode(Op, Known, DemandedElts, *this,
6223 Depth);
6224 }
6225 break;
6226 }
6227
6228 return Known;
6229}
6230
6232 unsigned Depth) const {
6233 APInt DemandedElts = getDemandAllEltsMask(Op);
6234 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
6235}
6236
6238 bool SNaN, unsigned Depth) const {
6239 assert(!DemandedElts.isZero() && "No demanded elements");
6240
6241 // If we're told that NaNs won't happen, assume they won't.
6242 if (Op->getFlags().hasNoNaNs())
6243 return true;
6244
6245 if (Depth >= MaxRecursionDepth)
6246 return false; // Limit search depth.
6247
6248 unsigned Opcode = Op.getOpcode();
6249 switch (Opcode) {
6250 case ISD::FADD:
6251 case ISD::FSUB:
6252 case ISD::FMUL:
6253 case ISD::FDIV:
6254 case ISD::FREM:
6255 case ISD::FSIN:
6256 case ISD::FCOS:
6257 case ISD::FTAN:
6258 case ISD::FASIN:
6259 case ISD::FACOS:
6260 case ISD::FATAN:
6261 case ISD::FATAN2:
6262 case ISD::FSINH:
6263 case ISD::FCOSH:
6264 case ISD::FTANH:
6265 case ISD::FMA:
6266 case ISD::FMULADD:
6267 case ISD::FMAD: {
6268 if (SNaN)
6269 return true;
6270 // TODO: Need isKnownNeverInfinity
6271 return false;
6272 }
6273 case ISD::FCANONICALIZE:
6274 case ISD::FEXP:
6275 case ISD::FEXP2:
6276 case ISD::FEXP10:
6277 case ISD::FTRUNC:
6278 case ISD::FFLOOR:
6279 case ISD::FCEIL:
6280 case ISD::FROUND:
6281 case ISD::FROUNDEVEN:
6282 case ISD::LROUND:
6283 case ISD::LLROUND:
6284 case ISD::FRINT:
6285 case ISD::LRINT:
6286 case ISD::LLRINT:
6287 case ISD::FNEARBYINT:
6288 case ISD::FLDEXP: {
6289 if (SNaN)
6290 return true;
6291 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6292 }
6293 case ISD::FABS:
6294 case ISD::FNEG:
6295 case ISD::FCOPYSIGN: {
6296 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6297 }
6298 case ISD::SELECT:
6299 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
6300 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
6301 case ISD::FP_EXTEND:
6302 case ISD::FP_ROUND: {
6303 if (SNaN)
6304 return true;
6305 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6306 }
6307 case ISD::SINT_TO_FP:
6308 case ISD::UINT_TO_FP:
6309 return true;
6310 case ISD::FSQRT: // Need is known positive
6311 case ISD::FLOG:
6312 case ISD::FLOG2:
6313 case ISD::FLOG10:
6314 case ISD::FPOWI:
6315 case ISD::FPOW: {
6316 if (SNaN)
6317 return true;
6318 // TODO: Refine on operand
6319 return false;
6320 }
6321 case ISD::FMINNUM:
6322 case ISD::FMAXNUM:
6323 case ISD::FMINIMUMNUM:
6324 case ISD::FMAXIMUMNUM: {
6325 // Only one needs to be known not-nan, since it will be returned if the
6326 // other ends up being one.
6327 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
6328 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
6329 }
6330 case ISD::FMINNUM_IEEE:
6331 case ISD::FMAXNUM_IEEE: {
6332 if (SNaN)
6333 return true;
6334 // This can return a NaN if either operand is an sNaN, or if both operands
6335 // are NaN.
6336 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
6337 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
6338 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
6339 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
6340 }
6341 case ISD::FMINIMUM:
6342 case ISD::FMAXIMUM: {
6343 // TODO: Does this quiet or return the origina NaN as-is?
6344 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
6345 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
6346 }
6348 SDValue Src = Op.getOperand(0);
6349 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6350 EVT SrcVT = Src.getValueType();
6351 if (SrcVT.isFixedLengthVector() && Idx &&
6352 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
6353 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
6354 Idx->getZExtValue());
6355 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
6356 }
6357 return isKnownNeverNaN(Src, SNaN, Depth + 1);
6358 }
6360 SDValue Src = Op.getOperand(0);
6361 if (Src.getValueType().isFixedLengthVector()) {
6362 unsigned Idx = Op.getConstantOperandVal(1);
6363 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
6364 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
6365 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
6366 }
6367 return isKnownNeverNaN(Src, SNaN, Depth + 1);
6368 }
6369 case ISD::INSERT_SUBVECTOR: {
6370 SDValue BaseVector = Op.getOperand(0);
6371 SDValue SubVector = Op.getOperand(1);
6372 EVT BaseVectorVT = BaseVector.getValueType();
6373 if (BaseVectorVT.isFixedLengthVector()) {
6374 unsigned Idx = Op.getConstantOperandVal(2);
6375 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
6376 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
6377
6378 // Clear/Extract the bits at the position where the subvector will be
6379 // inserted.
6380 APInt DemandedMask =
6381 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
6382 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
6383 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
6384
6385 bool NeverNaN = true;
6386 if (!DemandedSrcElts.isZero())
6387 NeverNaN &=
6388 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
6389 if (NeverNaN && !DemandedSubElts.isZero())
6390 NeverNaN &=
6391 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
6392 return NeverNaN;
6393 }
6394 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
6395 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
6396 }
6397 case ISD::BUILD_VECTOR: {
6398 unsigned NumElts = Op.getNumOperands();
6399 for (unsigned I = 0; I != NumElts; ++I)
6400 if (DemandedElts[I] &&
6401 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
6402 return false;
6403 return true;
6404 }
6405 case ISD::SPLAT_VECTOR:
6406 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
6407 case ISD::AssertNoFPClass: {
6408 FPClassTest NoFPClass =
6409 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
6410 if ((NoFPClass & fcNan) == fcNan)
6411 return true;
6412 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
6413 return true;
6414 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6415 }
6416 default:
6417 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6418 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6419 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
6420 Depth);
6421 }
6422 break;
6423 }
6424
6425 FPClassTest NanMask = SNaN ? fcSNan : fcNan;
6426 KnownFPClass Known = computeKnownFPClass(Op, DemandedElts, NanMask, Depth);
6427 return Known.isKnownNever(NanMask);
6428}
6429
6431 APInt DemandedElts = getDemandAllEltsMask(Op);
6432 return isKnownNeverLogicalZero(Op, DemandedElts, Depth);
6433}
6434
6436 const APInt &DemandedElts,
6437 unsigned Depth) const {
6438 assert(!DemandedElts.isZero() && "No demanded elements");
6439 EVT VT = Op.getValueType();
6440 KnownFPClass Known =
6441 computeKnownFPClass(Op, DemandedElts, fcZero | fcSubnormal, Depth);
6442 return Known.isKnownNeverLogicalZero(getDenormalMode(VT));
6443}
6444
6446 APInt DemandedElts = getDemandAllEltsMask(Op);
6447 return isKnownNeverZero(Op, DemandedElts, Depth);
6448}
6449
6451 unsigned Depth) const {
6452 if (Depth >= MaxRecursionDepth)
6453 return false; // Limit search depth.
6454
6455 EVT OpVT = Op.getValueType();
6456 unsigned BitWidth = OpVT.getScalarSizeInBits();
6457
6458 assert(!Op.getValueType().isFloatingPoint() &&
6459 "Floating point types unsupported - use isKnownNeverLogicalZero");
6460
6461 // If the value is a constant, we can obviously see if it is a zero or not.
6462 auto IsNeverZero = [BitWidth](const ConstantSDNode *C) {
6463 APInt V = C->getAPIntValue().zextOrTrunc(BitWidth);
6464 return !V.isZero();
6465 };
6466
6467 if (ISD::matchUnaryPredicate(Op, IsNeverZero))
6468 return true;
6469
6470 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6471 // some degree.
6472 switch (Op.getOpcode()) {
6473 default:
6474 break;
6475
6476 case ISD::BUILD_VECTOR:
6477 // Are all operands of a build vector constant non-zero?
6478 if (all_of(enumerate(Op->ops()), [&](auto P) {
6479 auto *C = dyn_cast<ConstantSDNode>(P.value());
6480 return !DemandedElts[P.index()] || (C && IsNeverZero(C));
6481 }))
6482 return true;
6483 break;
6484
6485 case ISD::SPLAT_VECTOR:
6486 // Is the operand of a splat vector a constant non-zero?
6487 if (auto *C = dyn_cast<ConstantSDNode>(Op->getOperand(0)))
6488 if (IsNeverZero(C))
6489 return true;
6490 break;
6491
6493 SDValue InVec = Op.getOperand(0);
6494 SDValue EltNo = Op.getOperand(1);
6495 EVT VecVT = InVec.getValueType();
6496
6497 // Skip scalable vectors or implicit extensions.
6498 if (VecVT.isScalableVector() ||
6499 OpVT.getScalarSizeInBits() != VecVT.getScalarSizeInBits())
6500 break;
6501
6502 // If we know the element index, just demand that vector element, else for
6503 // an unknown element index, ignore DemandedElts and demand them all.
6504 const unsigned NumSrcElts = VecVT.getVectorNumElements();
6505 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
6506 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
6507 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
6508 DemandedSrcElts =
6509 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
6510
6511 return isKnownNeverZero(InVec, DemandedSrcElts, Depth + 1);
6512 }
6513
6514 case ISD::OR:
6515 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6516 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6517
6518 case ISD::VSELECT:
6519 case ISD::SELECT:
6520 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6521 isKnownNeverZero(Op.getOperand(2), DemandedElts, Depth + 1);
6522
6523 case ISD::SHL: {
6524 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6525 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6526 KnownBits ValKnown =
6527 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6528 // 1 << X is never zero.
6529 if (ValKnown.One[0])
6530 return true;
6531 // If max shift cnt of known ones is non-zero, result is non-zero.
6532 APInt MaxCnt = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1)
6533 .getMaxValue();
6534 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6535 !ValKnown.One.shl(MaxCnt).isZero())
6536 return true;
6537 break;
6538 }
6539
6540 case ISD::VECTOR_SHUFFLE: {
6541 if (Op.getValueType().isScalableVector())
6542 return false;
6543
6544 unsigned NumElts = DemandedElts.getBitWidth();
6545
6546 // All demanded elements from LHS and RHS must be known non-zero.
6547 // Demanded elements with undef shuffle mask elements are unknown.
6548
6549 APInt DemandedLHS, DemandedRHS;
6550 auto *SVN = cast<ShuffleVectorSDNode>(Op);
6551 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
6552 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
6553 DemandedLHS, DemandedRHS))
6554 return false;
6555
6556 return (!DemandedLHS ||
6557 isKnownNeverZero(Op.getOperand(0), DemandedLHS, Depth + 1)) &&
6558 (!DemandedRHS ||
6559 isKnownNeverZero(Op.getOperand(1), DemandedRHS, Depth + 1));
6560 }
6561
6562 case ISD::UADDSAT:
6563 case ISD::UMAX:
6564 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6565 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6566
6567 case ISD::UMIN:
6568 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6569 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6570
6571 // For smin/smax: If either operand is known negative/positive
6572 // respectively we don't need the other to be known at all.
6573 case ISD::SMAX: {
6574 KnownBits Op1 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
6575 if (Op1.isStrictlyPositive())
6576 return true;
6577
6578 KnownBits Op0 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6579 if (Op0.isStrictlyPositive())
6580 return true;
6581
6582 if (Op1.isNonZero() && Op0.isNonZero())
6583 return true;
6584
6585 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6586 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6587 }
6588 case ISD::SMIN: {
6589 KnownBits Op1 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
6590 if (Op1.isNegative())
6591 return true;
6592
6593 KnownBits Op0 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6594 if (Op0.isNegative())
6595 return true;
6596
6597 if (Op1.isNonZero() && Op0.isNonZero())
6598 return true;
6599
6600 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6601 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6602 }
6603
6604 case ISD::ROTL:
6605 case ISD::ROTR:
6606 case ISD::BITREVERSE:
6607 case ISD::BSWAP:
6608 case ISD::CTPOP:
6609 case ISD::ABS:
6610 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6611
6612 case ISD::SRA:
6613 case ISD::SRL: {
6614 if (Op->getFlags().hasExact())
6615 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6616 KnownBits ValKnown =
6617 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6618 if (ValKnown.isNegative())
6619 return true;
6620 // If max shift cnt of known ones is non-zero, result is non-zero.
6621 APInt MaxCnt = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1)
6622 .getMaxValue();
6623 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6624 !ValKnown.One.lshr(MaxCnt).isZero())
6625 return true;
6626 break;
6627 }
6628 case ISD::UDIV:
6629 case ISD::SDIV:
6630 // div exact can only produce a zero if the dividend is zero.
6631 // TODO: For udiv this is also true if Op1 u<= Op0
6632 if (Op->getFlags().hasExact())
6633 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6634 break;
6635
6636 case ISD::ADD:
6637 if (Op->getFlags().hasNoUnsignedWrap())
6638 if (isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6639 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1))
6640 return true;
6641 // TODO: There are a lot more cases we can prove for add.
6642 break;
6643
6644 case ISD::SUB: {
6645 if (isNullConstant(Op.getOperand(0)))
6646 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1);
6647
6648 std::optional<bool> ne = KnownBits::ne(
6649 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1),
6650 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1));
6651 return ne && *ne;
6652 }
6653
6654 case ISD::MUL:
6655 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6656 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6657 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6658 return true;
6659 break;
6660
6661 case ISD::ZERO_EXTEND:
6662 case ISD::SIGN_EXTEND:
6663 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6664 case ISD::VSCALE: {
6666 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6667 ConstantRange CR =
6668 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6669 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6670 return true;
6671 break;
6672 }
6673 }
6674
6676}
6677
6679 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6680 return !C1->isNegative();
6681
6682 switch (Op.getOpcode()) {
6683 case ISD::FABS:
6684 case ISD::FEXP:
6685 case ISD::FEXP2:
6686 case ISD::FEXP10:
6687 return true;
6688 default:
6689 return false;
6690 }
6691
6692 llvm_unreachable("covered opcode switch");
6693}
6694
6696 assert(Use.getValueType().isFloatingPoint());
6697 const SDNode *User = Use.getUser();
6698 if (User->getFlags().hasNoSignedZeros())
6699 return true;
6700
6701 unsigned OperandNo = Use.getOperandNo();
6702 // Check if this use is insensitive to the sign of zero
6703 switch (User->getOpcode()) {
6704 case ISD::SETCC:
6705 // Comparisons: IEEE-754 specifies +0.0 == -0.0.
6706 case ISD::FABS:
6707 // fabs always produces +0.0.
6708 return true;
6709 case ISD::FCOPYSIGN:
6710 // copysign overwrites the sign bit of the first operand.
6711 return OperandNo == 0;
6712 case ISD::FADD:
6713 case ISD::FSUB: {
6714 // Arithmetic with non-zero constants fixes the uncertainty around the
6715 // sign bit.
6716 SDValue Other = User->getOperand(1 - OperandNo);
6718 }
6719 case ISD::FP_TO_SINT:
6720 case ISD::FP_TO_UINT:
6721 // fp-to-int conversions normalize signed zeros.
6722 return true;
6723 default:
6724 return false;
6725 }
6726}
6727
6729 if (Op->getFlags().hasNoSignedZeros())
6730 return true;
6731 // FIXME: Limit the amount of checked uses to not introduce a compile-time
6732 // regression. Ideally, this should be implemented as a demanded-bits
6733 // optimization that stems from the users.
6734 if (Op->use_size() > 2)
6735 return false;
6736 return all_of(Op->uses(),
6737 [&](const SDUse &Use) { return canIgnoreSignBitOfZero(Use); });
6738}
6739
6741 // Check the obvious case.
6742 if (A == B) return true;
6743
6744 // For negative and positive zero.
6747 if (CA->isZero() && CB->isZero()) return true;
6748
6749 // Otherwise they may not be equal.
6750 return false;
6751}
6752
6753// Only bits set in Mask must be negated, other bits may be arbitrary.
6755 if (isBitwiseNot(V, AllowUndefs))
6756 return V.getOperand(0);
6757
6758 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6759 // bits in the non-extended part.
6760 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6761 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6762 return SDValue();
6763 SDValue ExtArg = V.getOperand(0);
6764 if (ExtArg.getScalarValueSizeInBits() >=
6765 MaskC->getAPIntValue().getActiveBits() &&
6766 isBitwiseNot(ExtArg, AllowUndefs) &&
6767 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6768 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6769 return ExtArg.getOperand(0).getOperand(0);
6770 return SDValue();
6771}
6772
6774 // Match masked merge pattern (X & ~M) op (Y & M)
6775 // Including degenerate case (X & ~M) op M
6776 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6777 SDValue Other) {
6778 if (SDValue NotOperand =
6779 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6780 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6781 NotOperand->getOpcode() == ISD::TRUNCATE)
6782 NotOperand = NotOperand->getOperand(0);
6783
6784 if (Other == NotOperand)
6785 return true;
6786 if (Other->getOpcode() == ISD::AND)
6787 return NotOperand == Other->getOperand(0) ||
6788 NotOperand == Other->getOperand(1);
6789 }
6790 return false;
6791 };
6792
6793 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6794 A = A->getOperand(0);
6795
6796 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6797 B = B->getOperand(0);
6798
6799 if (A->getOpcode() == ISD::AND)
6800 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6801 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6802 return false;
6803}
6804
6805// FIXME: unify with llvm::haveNoCommonBitsSet.
6807 assert(A.getValueType() == B.getValueType() &&
6808 "Values must have the same type");
6811 return true;
6814}
6815
6816static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6817 SelectionDAG &DAG) {
6818 if (cast<ConstantSDNode>(Step)->isZero())
6819 return DAG.getConstant(0, DL, VT);
6820
6821 return SDValue();
6822}
6823
6826 SelectionDAG &DAG) {
6827 int NumOps = Ops.size();
6828 assert(NumOps != 0 && "Can't build an empty vector!");
6829 assert(!VT.isScalableVector() &&
6830 "BUILD_VECTOR cannot be used with scalable types");
6831 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6832 "Incorrect element count in BUILD_VECTOR!");
6833
6834 // BUILD_VECTOR of UNDEFs is UNDEF.
6835 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6836 return DAG.getUNDEF(VT);
6837
6838 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6839 SDValue IdentitySrc;
6840 bool IsIdentity = true;
6841 for (int i = 0; i != NumOps; ++i) {
6842 if (Ops[i].getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
6843 Ops[i].getOperand(0).getValueType() != VT ||
6844 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6845 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6846 Ops[i].getConstantOperandAPInt(1) != i) {
6847 IsIdentity = false;
6848 break;
6849 }
6850 IdentitySrc = Ops[i].getOperand(0);
6851 }
6852 if (IsIdentity)
6853 return IdentitySrc;
6854
6855 return SDValue();
6856}
6857
6858/// Try to simplify vector concatenation to an input value, undef, or build
6859/// vector.
6862 SelectionDAG &DAG) {
6863 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6865 [Ops](SDValue Op) {
6866 return Ops[0].getValueType() == Op.getValueType();
6867 }) &&
6868 "Concatenation of vectors with inconsistent value types!");
6869 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6870 VT.getVectorElementCount() &&
6871 "Incorrect element count in vector concatenation!");
6872
6873 if (Ops.size() == 1)
6874 return Ops[0];
6875
6876 // Concat of UNDEFs is UNDEF.
6877 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6878 return DAG.getUNDEF(VT);
6879
6880 // Scan the operands and look for extract operations from a single source
6881 // that correspond to insertion at the same location via this concatenation:
6882 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6883 SDValue IdentitySrc;
6884 bool IsIdentity = true;
6885 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6886 SDValue Op = Ops[i];
6887 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6888 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6889 Op.getOperand(0).getValueType() != VT ||
6890 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6891 Op.getConstantOperandVal(1) != IdentityIndex) {
6892 IsIdentity = false;
6893 break;
6894 }
6895 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6896 "Unexpected identity source vector for concat of extracts");
6897 IdentitySrc = Op.getOperand(0);
6898 }
6899 if (IsIdentity) {
6900 assert(IdentitySrc && "Failed to set source vector of extracts");
6901 return IdentitySrc;
6902 }
6903
6904 // The code below this point is only designed to work for fixed width
6905 // vectors, so we bail out for now.
6906 if (VT.isScalableVector())
6907 return SDValue();
6908
6909 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6910 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6911 // BUILD_VECTOR.
6912 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6913 EVT SVT = VT.getScalarType();
6915 for (SDValue Op : Ops) {
6916 EVT OpVT = Op.getValueType();
6917 if (Op.isUndef())
6918 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6919 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6920 Elts.append(Op->op_begin(), Op->op_end());
6921 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6922 OpVT.getVectorNumElements() == 1 &&
6923 isNullConstant(Op.getOperand(2)))
6924 Elts.push_back(Op.getOperand(1));
6925 else
6926 return SDValue();
6927 }
6928
6929 // BUILD_VECTOR requires all inputs to be of the same type, find the
6930 // maximum type and extend them all.
6931 for (SDValue Op : Elts)
6932 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6933
6934 if (SVT.bitsGT(VT.getScalarType())) {
6935 for (SDValue &Op : Elts) {
6936 if (Op.isUndef())
6937 Op = DAG.getUNDEF(SVT);
6938 else
6939 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6940 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6941 : DAG.getSExtOrTrunc(Op, DL, SVT);
6942 }
6943 }
6944
6945 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6946 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6947 return V;
6948}
6949
6950/// Gets or creates the specified node.
6951SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6952 SDVTList VTs = getVTList(VT);
6954 AddNodeIDNode(ID, Opcode, VTs, {});
6955 void *IP = nullptr;
6956 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6957 return SDValue(E, 0);
6958
6959 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6960 CSEMap.InsertNode(N, IP);
6961
6962 InsertNode(N);
6963 SDValue V = SDValue(N, 0);
6964 NewSDValueDbgMsg(V, "Creating new node: ", this);
6965 return V;
6966}
6967
6968SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6969 SDValue N1) {
6970 SDNodeFlags Flags;
6971 if (Inserter)
6972 Flags = Inserter->getFlags();
6973 return getNode(Opcode, DL, VT, N1, Flags);
6974}
6975
6976SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6977 SDValue N1, const SDNodeFlags Flags) {
6978 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6979
6980 // Constant fold unary operations with a vector integer or float operand.
6981 switch (Opcode) {
6982 default:
6983 // FIXME: Entirely reasonable to perform folding of other unary
6984 // operations here as the need arises.
6985 break;
6986 case ISD::FNEG:
6987 case ISD::FABS:
6988 case ISD::FCEIL:
6989 case ISD::FTRUNC:
6990 case ISD::FFLOOR:
6991 case ISD::FP_EXTEND:
6992 case ISD::FP_TO_SINT:
6993 case ISD::FP_TO_UINT:
6994 case ISD::FP_TO_FP16:
6995 case ISD::FP_TO_BF16:
6996 case ISD::TRUNCATE:
6997 case ISD::ANY_EXTEND:
6998 case ISD::ZERO_EXTEND:
6999 case ISD::SIGN_EXTEND:
7000 case ISD::UINT_TO_FP:
7001 case ISD::SINT_TO_FP:
7002 case ISD::FP16_TO_FP:
7003 case ISD::BF16_TO_FP:
7004 case ISD::BITCAST:
7005 case ISD::ABS:
7006 case ISD::BITREVERSE:
7007 case ISD::BSWAP:
7008 case ISD::CTLZ:
7010 case ISD::CTTZ:
7012 case ISD::CTPOP:
7013 case ISD::CTLS:
7014 case ISD::STEP_VECTOR: {
7015 SDValue Ops = {N1};
7016 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
7017 return Fold;
7018 }
7019 }
7020
7021 unsigned OpOpcode = N1.getNode()->getOpcode();
7022 switch (Opcode) {
7023 case ISD::STEP_VECTOR:
7024 assert(VT.isScalableVector() &&
7025 "STEP_VECTOR can only be used with scalable types");
7026 assert(OpOpcode == ISD::TargetConstant &&
7027 VT.getVectorElementType() == N1.getValueType() &&
7028 "Unexpected step operand");
7029 break;
7030 case ISD::FREEZE:
7031 assert(VT == N1.getValueType() && "Unexpected VT!");
7032 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
7033 return N1;
7034 break;
7035 case ISD::TokenFactor:
7036 case ISD::MERGE_VALUES:
7038 return N1; // Factor, merge or concat of one node? No need.
7039 case ISD::BUILD_VECTOR: {
7040 // Attempt to simplify BUILD_VECTOR.
7041 SDValue Ops[] = {N1};
7042 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7043 return V;
7044 break;
7045 }
7046 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
7047 case ISD::FP_EXTEND:
7049 "Invalid FP cast!");
7050 if (N1.getValueType() == VT) return N1; // noop conversion.
7051 assert((!VT.isVector() || VT.getVectorElementCount() ==
7053 "Vector element count mismatch!");
7054 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
7055 if (N1.isUndef())
7056 return getUNDEF(VT);
7057 break;
7058 case ISD::FP_TO_SINT:
7059 case ISD::FP_TO_UINT:
7060 if (N1.isUndef())
7061 return getUNDEF(VT);
7062 break;
7063 case ISD::SINT_TO_FP:
7064 case ISD::UINT_TO_FP:
7065 // [us]itofp(undef) = 0, because the result value is bounded.
7066 if (N1.isUndef())
7067 return getConstantFP(0.0, DL, VT);
7068 break;
7069 case ISD::SIGN_EXTEND:
7070 assert(VT.isInteger() && N1.getValueType().isInteger() &&
7071 "Invalid SIGN_EXTEND!");
7072 assert(VT.isVector() == N1.getValueType().isVector() &&
7073 "SIGN_EXTEND result type type should be vector iff the operand "
7074 "type is vector!");
7075 if (N1.getValueType() == VT) return N1; // noop extension
7076 assert((!VT.isVector() || VT.getVectorElementCount() ==
7078 "Vector element count mismatch!");
7079 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
7080 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
7081 SDNodeFlags Flags;
7082 if (OpOpcode == ISD::ZERO_EXTEND)
7083 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7084 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
7085 transferDbgValues(N1, NewVal);
7086 return NewVal;
7087 }
7088
7089 if (OpOpcode == ISD::POISON)
7090 return getPOISON(VT);
7091
7092 if (N1.isUndef())
7093 // sext(undef) = 0, because the top bits will all be the same.
7094 return getConstant(0, DL, VT);
7095
7096 // Skip unnecessary sext_inreg pattern:
7097 // (sext (trunc x)) -> x iff the upper bits are all signbits.
7098 if (OpOpcode == ISD::TRUNCATE) {
7099 SDValue OpOp = N1.getOperand(0);
7100 if (OpOp.getValueType() == VT) {
7101 unsigned NumSignExtBits =
7103 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
7104 transferDbgValues(N1, OpOp);
7105 return OpOp;
7106 }
7107 }
7108 }
7109 break;
7110 case ISD::ZERO_EXTEND:
7111 assert(VT.isInteger() && N1.getValueType().isInteger() &&
7112 "Invalid ZERO_EXTEND!");
7113 assert(VT.isVector() == N1.getValueType().isVector() &&
7114 "ZERO_EXTEND result type type should be vector iff the operand "
7115 "type is vector!");
7116 if (N1.getValueType() == VT) return N1; // noop extension
7117 assert((!VT.isVector() || VT.getVectorElementCount() ==
7119 "Vector element count mismatch!");
7120 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
7121 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
7122 SDNodeFlags Flags;
7123 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7124 SDValue NewVal =
7125 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
7126 transferDbgValues(N1, NewVal);
7127 return NewVal;
7128 }
7129
7130 if (OpOpcode == ISD::POISON)
7131 return getPOISON(VT);
7132
7133 if (N1.isUndef())
7134 // zext(undef) = 0, because the top bits will be zero.
7135 return getConstant(0, DL, VT);
7136
7137 // Skip unnecessary zext_inreg pattern:
7138 // (zext (trunc x)) -> x iff the upper bits are known zero.
7139 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
7140 // use to recognise zext_inreg patterns.
7141 if (OpOpcode == ISD::TRUNCATE) {
7142 SDValue OpOp = N1.getOperand(0);
7143 if (OpOp.getValueType() == VT) {
7144 if (OpOp.getOpcode() != ISD::AND) {
7147 if (MaskedValueIsZero(OpOp, HiBits)) {
7148 transferDbgValues(N1, OpOp);
7149 return OpOp;
7150 }
7151 }
7152 }
7153 }
7154 break;
7155 case ISD::ANY_EXTEND:
7156 assert(VT.isInteger() && N1.getValueType().isInteger() &&
7157 "Invalid ANY_EXTEND!");
7158 assert(VT.isVector() == N1.getValueType().isVector() &&
7159 "ANY_EXTEND result type type should be vector iff the operand "
7160 "type is vector!");
7161 if (N1.getValueType() == VT) return N1; // noop extension
7162 assert((!VT.isVector() || VT.getVectorElementCount() ==
7164 "Vector element count mismatch!");
7165 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
7166
7167 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
7168 OpOpcode == ISD::ANY_EXTEND) {
7169 SDNodeFlags Flags;
7170 if (OpOpcode == ISD::ZERO_EXTEND)
7171 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7172 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
7173 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
7174 }
7175 if (N1.isUndef())
7176 return getUNDEF(VT);
7177
7178 // (ext (trunc x)) -> x
7179 if (OpOpcode == ISD::TRUNCATE) {
7180 SDValue OpOp = N1.getOperand(0);
7181 if (OpOp.getValueType() == VT) {
7182 transferDbgValues(N1, OpOp);
7183 return OpOp;
7184 }
7185 }
7186 break;
7187 case ISD::TRUNCATE:
7188 assert(VT.isInteger() && N1.getValueType().isInteger() &&
7189 "Invalid TRUNCATE!");
7190 assert(VT.isVector() == N1.getValueType().isVector() &&
7191 "TRUNCATE result type type should be vector iff the operand "
7192 "type is vector!");
7193 if (N1.getValueType() == VT) return N1; // noop truncate
7194 assert((!VT.isVector() || VT.getVectorElementCount() ==
7196 "Vector element count mismatch!");
7197 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
7198 if (OpOpcode == ISD::TRUNCATE)
7199 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
7200 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
7201 OpOpcode == ISD::ANY_EXTEND) {
7202 // If the source is smaller than the dest, we still need an extend.
7204 VT.getScalarType())) {
7205 SDNodeFlags Flags;
7206 if (OpOpcode == ISD::ZERO_EXTEND)
7207 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7208 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
7209 }
7210 if (N1.getOperand(0).getValueType().bitsGT(VT))
7211 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
7212 return N1.getOperand(0);
7213 }
7214 if (N1.isUndef())
7215 return getUNDEF(VT);
7216 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
7217 return getVScale(DL, VT,
7219 break;
7223 assert(VT.isVector() && "This DAG node is restricted to vector types.");
7224 assert(N1.getValueType().bitsLE(VT) &&
7225 "The input must be the same size or smaller than the result.");
7228 "The destination vector type must have fewer lanes than the input.");
7229 break;
7230 case ISD::ABS:
7231 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
7232 if (N1.isUndef())
7233 return getConstant(0, DL, VT);
7234 break;
7235 case ISD::BSWAP:
7236 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
7237 assert((VT.getScalarSizeInBits() % 16 == 0) &&
7238 "BSWAP types must be a multiple of 16 bits!");
7239 if (N1.isUndef())
7240 return getUNDEF(VT);
7241 // bswap(bswap(X)) -> X.
7242 if (OpOpcode == ISD::BSWAP)
7243 return N1.getOperand(0);
7244 break;
7245 case ISD::BITREVERSE:
7246 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
7247 if (N1.isUndef())
7248 return getUNDEF(VT);
7249 break;
7250 case ISD::BITCAST:
7252 "Cannot BITCAST between types of different sizes!");
7253 if (VT == N1.getValueType()) return N1; // noop conversion.
7254 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
7255 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
7256 if (N1.isUndef())
7257 return getUNDEF(VT);
7258 break;
7260 assert(VT.isVector() && !N1.getValueType().isVector() &&
7261 (VT.getVectorElementType() == N1.getValueType() ||
7263 N1.getValueType().isInteger() &&
7265 "Illegal SCALAR_TO_VECTOR node!");
7266 if (N1.isUndef())
7267 return getUNDEF(VT);
7268 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
7269 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
7271 N1.getConstantOperandVal(1) == 0 &&
7272 N1.getOperand(0).getValueType() == VT)
7273 return N1.getOperand(0);
7274 break;
7275 case ISD::FNEG:
7276 // Negation of an unknown bag of bits is still completely undefined.
7277 if (N1.isUndef())
7278 return getUNDEF(VT);
7279
7280 if (OpOpcode == ISD::FNEG) // --X -> X
7281 return N1.getOperand(0);
7282 break;
7283 case ISD::FABS:
7284 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
7285 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
7286 break;
7287 case ISD::VSCALE:
7288 assert(VT == N1.getValueType() && "Unexpected VT!");
7289 break;
7290 case ISD::CTPOP:
7291 if (N1.getValueType().getScalarType() == MVT::i1)
7292 return N1;
7293 break;
7294 case ISD::CTLZ:
7295 case ISD::CTTZ:
7296 if (N1.getValueType().getScalarType() == MVT::i1)
7297 return getNOT(DL, N1, N1.getValueType());
7298 break;
7299 case ISD::CTLS:
7300 if (N1.getValueType().getScalarType() == MVT::i1)
7301 return getConstant(0, DL, VT);
7302 break;
7303 case ISD::VECREDUCE_ADD:
7304 if (N1.getValueType().getScalarType() == MVT::i1)
7305 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
7306 break;
7309 if (N1.getValueType().getScalarType() == MVT::i1)
7310 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
7311 break;
7314 if (N1.getValueType().getScalarType() == MVT::i1)
7315 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
7316 break;
7317 case ISD::SPLAT_VECTOR:
7318 assert(VT.isVector() && "Wrong return type!");
7319 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
7320 // that for now.
7322 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
7324 N1.getValueType().isInteger() &&
7326 "Wrong operand type!");
7327 break;
7328 }
7329
7330 SDNode *N;
7331 SDVTList VTs = getVTList(VT);
7332 SDValue Ops[] = {N1};
7333 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
7335 AddNodeIDNode(ID, Opcode, VTs, Ops);
7336 void *IP = nullptr;
7337 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7338 E->intersectFlagsWith(Flags);
7339 return SDValue(E, 0);
7340 }
7341
7342 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7343 N->setFlags(Flags);
7344 createOperands(N, Ops);
7345 CSEMap.InsertNode(N, IP);
7346 } else {
7347 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7348 createOperands(N, Ops);
7349 }
7350
7351 InsertNode(N);
7352 SDValue V = SDValue(N, 0);
7353 NewSDValueDbgMsg(V, "Creating new node: ", this);
7354 return V;
7355}
7356
7357static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
7358 const APInt &C2) {
7359 switch (Opcode) {
7360 case ISD::ADD: return C1 + C2;
7361 case ISD::SUB: return C1 - C2;
7362 case ISD::MUL: return C1 * C2;
7363 case ISD::AND: return C1 & C2;
7364 case ISD::OR: return C1 | C2;
7365 case ISD::XOR: return C1 ^ C2;
7366 case ISD::SHL: return C1 << C2;
7367 case ISD::SRL: return C1.lshr(C2);
7368 case ISD::SRA: return C1.ashr(C2);
7369 case ISD::ROTL: return C1.rotl(C2);
7370 case ISD::ROTR: return C1.rotr(C2);
7371 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
7372 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
7373 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
7374 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
7375 case ISD::SADDSAT: return C1.sadd_sat(C2);
7376 case ISD::UADDSAT: return C1.uadd_sat(C2);
7377 case ISD::SSUBSAT: return C1.ssub_sat(C2);
7378 case ISD::USUBSAT: return C1.usub_sat(C2);
7379 case ISD::SSHLSAT: return C1.sshl_sat(C2);
7380 case ISD::USHLSAT: return C1.ushl_sat(C2);
7381 case ISD::UDIV:
7382 if (!C2.getBoolValue())
7383 break;
7384 return C1.udiv(C2);
7385 case ISD::UREM:
7386 if (!C2.getBoolValue())
7387 break;
7388 return C1.urem(C2);
7389 case ISD::SDIV:
7390 if (!C2.getBoolValue())
7391 break;
7392 return C1.sdiv(C2);
7393 case ISD::SREM:
7394 if (!C2.getBoolValue())
7395 break;
7396 return C1.srem(C2);
7397 case ISD::AVGFLOORS:
7398 return APIntOps::avgFloorS(C1, C2);
7399 case ISD::AVGFLOORU:
7400 return APIntOps::avgFloorU(C1, C2);
7401 case ISD::AVGCEILS:
7402 return APIntOps::avgCeilS(C1, C2);
7403 case ISD::AVGCEILU:
7404 return APIntOps::avgCeilU(C1, C2);
7405 case ISD::ABDS:
7406 return APIntOps::abds(C1, C2);
7407 case ISD::ABDU:
7408 return APIntOps::abdu(C1, C2);
7409 case ISD::MULHS:
7410 return APIntOps::mulhs(C1, C2);
7411 case ISD::MULHU:
7412 return APIntOps::mulhu(C1, C2);
7413 case ISD::CLMUL:
7414 return APIntOps::clmul(C1, C2);
7415 case ISD::CLMULR:
7416 return APIntOps::clmulr(C1, C2);
7417 case ISD::CLMULH:
7418 return APIntOps::clmulh(C1, C2);
7419 }
7420 return std::nullopt;
7421}
7422// Handle constant folding with UNDEF.
7423// TODO: Handle more cases.
7424static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
7425 bool IsUndef1, const APInt &C2,
7426 bool IsUndef2) {
7427 if (!(IsUndef1 || IsUndef2))
7428 return FoldValue(Opcode, C1, C2);
7429
7430 // Fold and(x, undef) -> 0
7431 // Fold mul(x, undef) -> 0
7432 if (Opcode == ISD::AND || Opcode == ISD::MUL)
7433 return APInt::getZero(C1.getBitWidth());
7434
7435 return std::nullopt;
7436}
7437
7439 const GlobalAddressSDNode *GA,
7440 const SDNode *N2) {
7441 if (GA->getOpcode() != ISD::GlobalAddress)
7442 return SDValue();
7443 if (!TLI->isOffsetFoldingLegal(GA))
7444 return SDValue();
7445 auto *C2 = dyn_cast<ConstantSDNode>(N2);
7446 if (!C2)
7447 return SDValue();
7448 int64_t Offset = C2->getSExtValue();
7449 switch (Opcode) {
7450 case ISD::ADD:
7451 case ISD::PTRADD:
7452 break;
7453 case ISD::SUB: Offset = -uint64_t(Offset); break;
7454 default: return SDValue();
7455 }
7456 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
7457 GA->getOffset() + uint64_t(Offset));
7458}
7459
7461 switch (Opcode) {
7462 case ISD::SDIV:
7463 case ISD::UDIV:
7464 case ISD::SREM:
7465 case ISD::UREM: {
7466 // If a divisor is zero/undef or any element of a divisor vector is
7467 // zero/undef, the whole op is undef.
7468 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
7469 SDValue Divisor = Ops[1];
7470 if (Divisor.isUndef() || isNullConstant(Divisor))
7471 return true;
7472
7473 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
7474 llvm::any_of(Divisor->op_values(),
7475 [](SDValue V) { return V.isUndef() ||
7476 isNullConstant(V); });
7477 // TODO: Handle signed overflow.
7478 }
7479 // TODO: Handle oversized shifts.
7480 default:
7481 return false;
7482 }
7483}
7484
7487 SDNodeFlags Flags) {
7488 // If the opcode is a target-specific ISD node, there's nothing we can
7489 // do here and the operand rules may not line up with the below, so
7490 // bail early.
7491 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
7492 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
7493 // foldCONCAT_VECTORS in getNode before this is called.
7494 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
7495 return SDValue();
7496
7497 unsigned NumOps = Ops.size();
7498 if (NumOps == 0)
7499 return SDValue();
7500
7501 if (isUndef(Opcode, Ops))
7502 return getUNDEF(VT);
7503
7504 // Handle unary special cases.
7505 if (NumOps == 1) {
7506 SDValue N1 = Ops[0];
7507
7508 // Constant fold unary operations with an integer constant operand. Even
7509 // opaque constant will be folded, because the folding of unary operations
7510 // doesn't create new constants with different values. Nevertheless, the
7511 // opaque flag is preserved during folding to prevent future folding with
7512 // other constants.
7513 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
7514 const APInt &Val = C->getAPIntValue();
7515 switch (Opcode) {
7516 case ISD::SIGN_EXTEND:
7517 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7518 C->isTargetOpcode(), C->isOpaque());
7519 case ISD::TRUNCATE:
7520 if (C->isOpaque())
7521 break;
7522 [[fallthrough]];
7523 case ISD::ZERO_EXTEND:
7524 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7525 C->isTargetOpcode(), C->isOpaque());
7526 case ISD::ANY_EXTEND:
7527 // Some targets like RISCV prefer to sign extend some types.
7528 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
7529 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7530 C->isTargetOpcode(), C->isOpaque());
7531 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7532 C->isTargetOpcode(), C->isOpaque());
7533 case ISD::ABS:
7534 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
7535 C->isOpaque());
7536 case ISD::BITREVERSE:
7537 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
7538 C->isOpaque());
7539 case ISD::BSWAP:
7540 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
7541 C->isOpaque());
7542 case ISD::CTPOP:
7543 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
7544 C->isOpaque());
7545 case ISD::CTLZ:
7547 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7548 C->isOpaque());
7549 case ISD::CTTZ:
7551 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7552 C->isOpaque());
7553 case ISD::CTLS:
7554 // CTLS returns the number of extra sign bits so subtract one.
7555 return getConstant(Val.getNumSignBits() - 1, DL, VT,
7556 C->isTargetOpcode(), C->isOpaque());
7557 case ISD::UINT_TO_FP:
7558 case ISD::SINT_TO_FP: {
7560 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7562 return getConstantFP(FPV, DL, VT);
7563 }
7564 case ISD::FP16_TO_FP:
7565 case ISD::BF16_TO_FP: {
7566 bool Ignored;
7567 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7568 : APFloat::BFloat(),
7569 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7570
7571 // This can return overflow, underflow, or inexact; we don't care.
7572 // FIXME need to be more flexible about rounding mode.
7574 &Ignored);
7575 return getConstantFP(FPV, DL, VT);
7576 }
7577 case ISD::STEP_VECTOR:
7578 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7579 return V;
7580 break;
7581 case ISD::BITCAST:
7582 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7583 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7584 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7585 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7586 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7587 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7588 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7589 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7590 break;
7591 }
7592 }
7593
7594 // Constant fold unary operations with a floating point constant operand.
7595 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7596 APFloat V = C->getValueAPF(); // make copy
7597 switch (Opcode) {
7598 case ISD::FNEG:
7599 V.changeSign();
7600 return getConstantFP(V, DL, VT);
7601 case ISD::FABS:
7602 V.clearSign();
7603 return getConstantFP(V, DL, VT);
7604 case ISD::FCEIL: {
7605 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7607 return getConstantFP(V, DL, VT);
7608 return SDValue();
7609 }
7610 case ISD::FTRUNC: {
7611 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7613 return getConstantFP(V, DL, VT);
7614 return SDValue();
7615 }
7616 case ISD::FFLOOR: {
7617 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7619 return getConstantFP(V, DL, VT);
7620 return SDValue();
7621 }
7622 case ISD::FP_EXTEND: {
7623 bool ignored;
7624 // This can return overflow, underflow, or inexact; we don't care.
7625 // FIXME need to be more flexible about rounding mode.
7626 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7627 &ignored);
7628 return getConstantFP(V, DL, VT);
7629 }
7630 case ISD::FP_TO_SINT:
7631 case ISD::FP_TO_UINT: {
7632 bool ignored;
7633 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7634 // FIXME need to be more flexible about rounding mode.
7636 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7637 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7638 break;
7639 return getConstant(IntVal, DL, VT);
7640 }
7641 case ISD::FP_TO_FP16:
7642 case ISD::FP_TO_BF16: {
7643 bool Ignored;
7644 // This can return overflow, underflow, or inexact; we don't care.
7645 // FIXME need to be more flexible about rounding mode.
7646 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7647 : APFloat::BFloat(),
7649 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7650 }
7651 case ISD::BITCAST:
7652 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7653 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7654 VT);
7655 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7656 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7657 VT);
7658 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7659 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7660 VT);
7661 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7662 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7663 break;
7664 }
7665 }
7666
7667 // Early-out if we failed to constant fold a bitcast.
7668 if (Opcode == ISD::BITCAST)
7669 return SDValue();
7670 }
7671
7672 // Handle binops special cases.
7673 if (NumOps == 2) {
7674 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7675 return CFP;
7676
7677 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7678 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7679 if (C1->isOpaque() || C2->isOpaque())
7680 return SDValue();
7681
7682 std::optional<APInt> FoldAttempt =
7683 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7684 if (!FoldAttempt)
7685 return SDValue();
7686
7687 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7688 assert((!Folded || !VT.isVector()) &&
7689 "Can't fold vectors ops with scalar operands");
7690 return Folded;
7691 }
7692 }
7693
7694 // fold (add Sym, c) -> Sym+c
7696 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7697 if (TLI->isCommutativeBinOp(Opcode))
7699 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7700
7701 // fold (sext_in_reg c1) -> c2
7702 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7703 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7704
7705 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7706 unsigned FromBits = EVT.getScalarSizeInBits();
7707 Val <<= Val.getBitWidth() - FromBits;
7708 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7709 return getConstant(Val, DL, ConstantVT);
7710 };
7711
7712 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7713 const APInt &Val = C1->getAPIntValue();
7714 return SignExtendInReg(Val, VT);
7715 }
7716
7718 SmallVector<SDValue, 8> ScalarOps;
7719 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7720 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7721 SDValue Op = Ops[0].getOperand(I);
7722 if (Op.isUndef()) {
7723 ScalarOps.push_back(getUNDEF(OpVT));
7724 continue;
7725 }
7726 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7727 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7728 }
7729 return getBuildVector(VT, DL, ScalarOps);
7730 }
7731
7732 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7733 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7734 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7735 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7736 Ops[0].getOperand(0).getValueType()));
7737 }
7738 }
7739
7740 // Handle fshl/fshr special cases.
7741 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7742 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7743 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7744 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7745
7746 if (C1 && C2 && C3) {
7747 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7748 return SDValue();
7749 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7750 &V3 = C3->getAPIntValue();
7751
7752 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7753 : APIntOps::fshr(V1, V2, V3);
7754 return getConstant(FoldedVal, DL, VT);
7755 }
7756 }
7757
7758 // Handle fma/fmad special cases.
7759 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7760 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7761 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7762 Ops[2].getValueType() == VT && "FMA types must match!");
7766 if (C1 && C2 && C3) {
7767 APFloat V1 = C1->getValueAPF();
7768 const APFloat &V2 = C2->getValueAPF();
7769 const APFloat &V3 = C3->getValueAPF();
7770 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7773 } else
7775 return getConstantFP(V1, DL, VT);
7776 }
7777 }
7778
7779 // This is for vector folding only from here on.
7780 if (!VT.isVector())
7781 return SDValue();
7782
7783 ElementCount NumElts = VT.getVectorElementCount();
7784
7785 // See if we can fold through any bitcasted integer ops.
7786 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7787 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7788 (Ops[0].getOpcode() == ISD::BITCAST ||
7789 Ops[1].getOpcode() == ISD::BITCAST)) {
7792 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7793 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7794 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7795 N2.getValueType().isInteger()) {
7796 bool IsLE = getDataLayout().isLittleEndian();
7797 unsigned EltBits = VT.getScalarSizeInBits();
7798 SmallVector<APInt> RawBits1, RawBits2;
7799 BitVector UndefElts1, UndefElts2;
7800 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7801 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7802 SmallVector<APInt> RawBits;
7803 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7804 std::optional<APInt> Fold = FoldValueWithUndef(
7805 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7806 if (!Fold)
7807 break;
7808 RawBits.push_back(*Fold);
7809 }
7810 if (RawBits.size() == NumElts.getFixedValue()) {
7811 // We have constant folded, but we might need to cast this again back
7812 // to the original (possibly legalized) type.
7813 EVT BVVT, BVEltVT;
7814 if (N1.getValueType() == VT) {
7815 BVVT = N1.getValueType();
7816 BVEltVT = BV1->getOperand(0).getValueType();
7817 } else {
7818 BVVT = N2.getValueType();
7819 BVEltVT = BV2->getOperand(0).getValueType();
7820 }
7821 unsigned BVEltBits = BVEltVT.getSizeInBits();
7822 SmallVector<APInt> DstBits;
7823 BitVector DstUndefs;
7825 DstBits, RawBits, DstUndefs,
7826 BitVector(RawBits.size(), false));
7827 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7828 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7829 if (DstUndefs[I])
7830 continue;
7831 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7832 }
7833 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7834 }
7835 }
7836 }
7837 // Logic ops can be folded from raw integer bits - mainly for AVX512 masks.
7838 if (ISD::isBitwiseLogicOp(Opcode) && isa<ConstantSDNode>(N1) &&
7839 isa<ConstantSDNode>(N2)) {
7840 if (SDValue Res = FoldConstantArithmetic(Opcode, DL, N1.getValueType(),
7841 {N1, N2}, Flags))
7842 return getBitcast(VT, Res);
7843 }
7844 }
7845
7846 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7847 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7848 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7849 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7850 APInt RHSVal;
7851 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7852 APInt NewStep = Opcode == ISD::MUL
7853 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7854 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7855 return getStepVector(DL, VT, NewStep);
7856 }
7857 }
7858
7859 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7860 return !Op.getValueType().isVector() ||
7861 Op.getValueType().getVectorElementCount() == NumElts;
7862 };
7863
7864 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7865 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7866 Op.getOpcode() == ISD::BUILD_VECTOR ||
7867 Op.getOpcode() == ISD::SPLAT_VECTOR;
7868 };
7869
7870 // All operands must be vector types with the same number of elements as
7871 // the result type and must be either UNDEF or a build/splat vector
7872 // or UNDEF scalars.
7873 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7874 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7875 return SDValue();
7876
7877 // If we are comparing vectors, then the result needs to be a i1 boolean that
7878 // is then extended back to the legal result type depending on how booleans
7879 // are represented.
7880 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7881 ISD::NodeType ExtendCode =
7882 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7883 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7885
7886 // Find legal integer scalar type for constant promotion and
7887 // ensure that its scalar size is at least as large as source.
7888 EVT LegalSVT = VT.getScalarType();
7889 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7890 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7891 if (LegalSVT.bitsLT(VT.getScalarType()))
7892 return SDValue();
7893 }
7894
7895 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7896 // only have one operand to check. For fixed-length vector types we may have
7897 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7898 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7899
7900 // Constant fold each scalar lane separately.
7901 SmallVector<SDValue, 4> ScalarResults;
7902 for (unsigned I = 0; I != NumVectorElts; I++) {
7903 SmallVector<SDValue, 4> ScalarOps;
7904 for (SDValue Op : Ops) {
7905 EVT InSVT = Op.getValueType().getScalarType();
7906 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7907 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7908 if (Op.isUndef())
7909 ScalarOps.push_back(getUNDEF(InSVT));
7910 else
7911 ScalarOps.push_back(Op);
7912 continue;
7913 }
7914
7915 SDValue ScalarOp =
7916 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7917 EVT ScalarVT = ScalarOp.getValueType();
7918
7919 // Build vector (integer) scalar operands may need implicit
7920 // truncation - do this before constant folding.
7921 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7922 // Don't create illegally-typed nodes unless they're constants or undef
7923 // - if we fail to constant fold we can't guarantee the (dead) nodes
7924 // we're creating will be cleaned up before being visited for
7925 // legalization.
7926 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7927 !isa<ConstantSDNode>(ScalarOp) &&
7928 TLI->getTypeAction(*getContext(), InSVT) !=
7930 return SDValue();
7931 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7932 }
7933
7934 ScalarOps.push_back(ScalarOp);
7935 }
7936
7937 // Constant fold the scalar operands.
7938 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7939
7940 // Scalar folding only succeeded if the result is a constant or UNDEF.
7941 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7942 ScalarResult.getOpcode() != ISD::ConstantFP)
7943 return SDValue();
7944
7945 // Legalize the (integer) scalar constant if necessary. We only do
7946 // this once we know the folding succeeded, since otherwise we would
7947 // get a node with illegal type which has a user.
7948 if (LegalSVT != SVT)
7949 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7950
7951 ScalarResults.push_back(ScalarResult);
7952 }
7953
7954 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7955 : getBuildVector(VT, DL, ScalarResults);
7956 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7957 return V;
7958}
7959
7962 // TODO: Add support for unary/ternary fp opcodes.
7963 if (Ops.size() != 2)
7964 return SDValue();
7965
7966 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7967 // should. That will require dealing with a potentially non-default
7968 // rounding mode, checking the "opStatus" return value from the APFloat
7969 // math calculations, and possibly other variations.
7970 SDValue N1 = Ops[0];
7971 SDValue N2 = Ops[1];
7972 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7973 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7974 if (N1CFP && N2CFP) {
7975 APFloat C1 = N1CFP->getValueAPF(); // make copy
7976 const APFloat &C2 = N2CFP->getValueAPF();
7977 switch (Opcode) {
7978 case ISD::FADD:
7980 return getConstantFP(C1, DL, VT);
7981 case ISD::FSUB:
7983 return getConstantFP(C1, DL, VT);
7984 case ISD::FMUL:
7986 return getConstantFP(C1, DL, VT);
7987 case ISD::FDIV:
7989 return getConstantFP(C1, DL, VT);
7990 case ISD::FREM:
7991 C1.mod(C2);
7992 return getConstantFP(C1, DL, VT);
7993 case ISD::FCOPYSIGN:
7994 C1.copySign(C2);
7995 return getConstantFP(C1, DL, VT);
7996 case ISD::FMINNUM:
7997 return getConstantFP(minnum(C1, C2), DL, VT);
7998 case ISD::FMAXNUM:
7999 return getConstantFP(maxnum(C1, C2), DL, VT);
8000 case ISD::FMINIMUM:
8001 return getConstantFP(minimum(C1, C2), DL, VT);
8002 case ISD::FMAXIMUM:
8003 return getConstantFP(maximum(C1, C2), DL, VT);
8004 case ISD::FMINIMUMNUM:
8005 return getConstantFP(minimumnum(C1, C2), DL, VT);
8006 case ISD::FMAXIMUMNUM:
8007 return getConstantFP(maximumnum(C1, C2), DL, VT);
8008 default: break;
8009 }
8010 }
8011 if (N1CFP && Opcode == ISD::FP_ROUND) {
8012 APFloat C1 = N1CFP->getValueAPF(); // make copy
8013 bool Unused;
8014 // This can return overflow, underflow, or inexact; we don't care.
8015 // FIXME need to be more flexible about rounding mode.
8017 &Unused);
8018 return getConstantFP(C1, DL, VT);
8019 }
8020
8021 switch (Opcode) {
8022 case ISD::FSUB:
8023 // -0.0 - undef --> undef (consistent with "fneg undef")
8024 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
8025 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
8026 return getUNDEF(VT);
8027 [[fallthrough]];
8028
8029 case ISD::FADD:
8030 case ISD::FMUL:
8031 case ISD::FDIV:
8032 case ISD::FREM:
8033 // If both operands are undef, the result is undef. If 1 operand is undef,
8034 // the result is NaN. This should match the behavior of the IR optimizer.
8035 if (N1.isUndef() && N2.isUndef())
8036 return getUNDEF(VT);
8037 if (N1.isUndef() || N2.isUndef())
8039 }
8040 return SDValue();
8041}
8042
8044 const SDLoc &DL, EVT DstEltVT) {
8045 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
8046
8047 // If this is already the right type, we're done.
8048 if (SrcEltVT == DstEltVT)
8049 return SDValue(BV, 0);
8050
8051 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
8052 unsigned DstBitSize = DstEltVT.getSizeInBits();
8053
8054 // If this is a conversion of N elements of one type to N elements of another
8055 // type, convert each element. This handles FP<->INT cases.
8056 if (SrcBitSize == DstBitSize) {
8058 for (SDValue Op : BV->op_values()) {
8059 // If the vector element type is not legal, the BUILD_VECTOR operands
8060 // are promoted and implicitly truncated. Make that explicit here.
8061 if (Op.getValueType() != SrcEltVT)
8062 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
8063 Ops.push_back(getBitcast(DstEltVT, Op));
8064 }
8065 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
8067 return getBuildVector(VT, DL, Ops);
8068 }
8069
8070 // Otherwise, we're growing or shrinking the elements. To avoid having to
8071 // handle annoying details of growing/shrinking FP values, we convert them to
8072 // int first.
8073 if (SrcEltVT.isFloatingPoint()) {
8074 // Convert the input float vector to a int vector where the elements are the
8075 // same sizes.
8076 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
8077 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
8079 DstEltVT);
8080 return SDValue();
8081 }
8082
8083 // Now we know the input is an integer vector. If the output is a FP type,
8084 // convert to integer first, then to FP of the right size.
8085 if (DstEltVT.isFloatingPoint()) {
8086 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
8087 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
8089 DstEltVT);
8090 return SDValue();
8091 }
8092
8093 // Okay, we know the src/dst types are both integers of differing types.
8094 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
8095
8096 // Extract the constant raw bit data.
8097 BitVector UndefElements;
8098 SmallVector<APInt> RawBits;
8099 bool IsLE = getDataLayout().isLittleEndian();
8100 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
8101 return SDValue();
8102
8104 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
8105 if (UndefElements[I])
8106 Ops.push_back(getUNDEF(DstEltVT));
8107 else
8108 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
8109 }
8110
8111 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
8112 return getBuildVector(VT, DL, Ops);
8113}
8114
8116 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
8117
8118 // There's no need to assert on a byte-aligned pointer. All pointers are at
8119 // least byte aligned.
8120 if (A == Align(1))
8121 return Val;
8122
8123 SDVTList VTs = getVTList(Val.getValueType());
8125 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
8126 ID.AddInteger(A.value());
8127
8128 void *IP = nullptr;
8129 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
8130 return SDValue(E, 0);
8131
8132 auto *N =
8133 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
8134 createOperands(N, {Val});
8135
8136 CSEMap.InsertNode(N, IP);
8137 InsertNode(N);
8138
8139 SDValue V(N, 0);
8140 NewSDValueDbgMsg(V, "Creating new node: ", this);
8141 return V;
8142}
8143
8144SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8145 SDValue N1, SDValue N2) {
8146 SDNodeFlags Flags;
8147 if (Inserter)
8148 Flags = Inserter->getFlags();
8149 return getNode(Opcode, DL, VT, N1, N2, Flags);
8150}
8151
8153 SDValue &N2) const {
8154 if (!TLI->isCommutativeBinOp(Opcode))
8155 return;
8156
8157 // Canonicalize:
8158 // binop(const, nonconst) -> binop(nonconst, const)
8161 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
8162 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
8163 if ((N1C && !N2C) || (N1CFP && !N2CFP))
8164 std::swap(N1, N2);
8165
8166 // Canonicalize:
8167 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
8168 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
8170 std::swap(N1, N2);
8171}
8172
8173SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8174 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
8176 N2.getOpcode() != ISD::DELETED_NODE &&
8177 "Operand is DELETED_NODE!");
8178
8179 canonicalizeCommutativeBinop(Opcode, N1, N2);
8180
8181 auto *N1C = dyn_cast<ConstantSDNode>(N1);
8182 auto *N2C = dyn_cast<ConstantSDNode>(N2);
8183
8184 // Don't allow undefs in vector splats - we might be returning N2 when folding
8185 // to zero etc.
8186 ConstantSDNode *N2CV =
8187 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
8188
8189 switch (Opcode) {
8190 default: break;
8191 case ISD::TokenFactor:
8192 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
8193 N2.getValueType() == MVT::Other && "Invalid token factor!");
8194 // Fold trivial token factors.
8195 if (N1.getOpcode() == ISD::EntryToken) return N2;
8196 if (N2.getOpcode() == ISD::EntryToken) return N1;
8197 if (N1 == N2) return N1;
8198 break;
8199 case ISD::BUILD_VECTOR: {
8200 // Attempt to simplify BUILD_VECTOR.
8201 SDValue Ops[] = {N1, N2};
8202 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8203 return V;
8204 break;
8205 }
8206 case ISD::CONCAT_VECTORS: {
8207 SDValue Ops[] = {N1, N2};
8208 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8209 return V;
8210 break;
8211 }
8212 case ISD::AND:
8213 assert(VT.isInteger() && "This operator does not apply to FP types!");
8214 assert(N1.getValueType() == N2.getValueType() &&
8215 N1.getValueType() == VT && "Binary operator types must match!");
8216 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
8217 // worth handling here.
8218 if (N2CV && N2CV->isZero())
8219 return N2;
8220 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
8221 return N1;
8222 break;
8223 case ISD::OR:
8224 case ISD::XOR:
8225 case ISD::ADD:
8226 case ISD::PTRADD:
8227 case ISD::SUB:
8228 assert(VT.isInteger() && "This operator does not apply to FP types!");
8229 assert(N1.getValueType() == N2.getValueType() &&
8230 N1.getValueType() == VT && "Binary operator types must match!");
8231 // The equal operand types requirement is unnecessarily strong for PTRADD.
8232 // However, the SelectionDAGBuilder does not generate PTRADDs with different
8233 // operand types, and we'd need to re-implement GEP's non-standard wrapping
8234 // logic everywhere where PTRADDs may be folded or combined to properly
8235 // support them. If/when we introduce pointer types to the SDAG, we will
8236 // need to relax this constraint.
8237
8238 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
8239 // it's worth handling here.
8240 if (N2CV && N2CV->isZero())
8241 return N1;
8242 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
8243 VT.getScalarType() == MVT::i1)
8244 return getNode(ISD::XOR, DL, VT, N1, N2);
8245 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
8246 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
8247 N2.getOpcode() == ISD::VSCALE) {
8248 const APInt &C1 = N1->getConstantOperandAPInt(0);
8249 const APInt &C2 = N2->getConstantOperandAPInt(0);
8250 return getVScale(DL, VT, C1 + C2);
8251 }
8252 break;
8253 case ISD::MUL:
8254 assert(VT.isInteger() && "This operator does not apply to FP types!");
8255 assert(N1.getValueType() == N2.getValueType() &&
8256 N1.getValueType() == VT && "Binary operator types must match!");
8257 if (VT.getScalarType() == MVT::i1)
8258 return getNode(ISD::AND, DL, VT, N1, N2);
8259 if (N2CV && N2CV->isZero())
8260 return N2;
8261 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
8262 const APInt &MulImm = N1->getConstantOperandAPInt(0);
8263 const APInt &N2CImm = N2C->getAPIntValue();
8264 return getVScale(DL, VT, MulImm * N2CImm);
8265 }
8266 break;
8267 case ISD::UDIV:
8268 case ISD::UREM:
8269 case ISD::MULHU:
8270 case ISD::MULHS:
8271 case ISD::SDIV:
8272 case ISD::SREM:
8273 case ISD::SADDSAT:
8274 case ISD::SSUBSAT:
8275 case ISD::UADDSAT:
8276 case ISD::USUBSAT:
8277 assert(VT.isInteger() && "This operator does not apply to FP types!");
8278 assert(N1.getValueType() == N2.getValueType() &&
8279 N1.getValueType() == VT && "Binary operator types must match!");
8280 if (VT.getScalarType() == MVT::i1) {
8281 // fold (add_sat x, y) -> (or x, y) for bool types.
8282 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
8283 return getNode(ISD::OR, DL, VT, N1, N2);
8284 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
8285 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
8286 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
8287 }
8288 break;
8289 case ISD::SCMP:
8290 case ISD::UCMP:
8291 assert(N1.getValueType() == N2.getValueType() &&
8292 "Types of operands of UCMP/SCMP must match");
8293 assert(N1.getValueType().isVector() == VT.isVector() &&
8294 "Operands and return type of must both be scalars or vectors");
8295 if (VT.isVector())
8298 "Result and operands must have the same number of elements");
8299 break;
8300 case ISD::AVGFLOORS:
8301 case ISD::AVGFLOORU:
8302 case ISD::AVGCEILS:
8303 case ISD::AVGCEILU:
8304 assert(VT.isInteger() && "This operator does not apply to FP types!");
8305 assert(N1.getValueType() == N2.getValueType() &&
8306 N1.getValueType() == VT && "Binary operator types must match!");
8307 break;
8308 case ISD::ABDS:
8309 case ISD::ABDU:
8310 assert(VT.isInteger() && "This operator does not apply to FP types!");
8311 assert(N1.getValueType() == N2.getValueType() &&
8312 N1.getValueType() == VT && "Binary operator types must match!");
8313 if (VT.getScalarType() == MVT::i1)
8314 return getNode(ISD::XOR, DL, VT, N1, N2);
8315 break;
8316 case ISD::SMIN:
8317 case ISD::UMAX:
8318 assert(VT.isInteger() && "This operator does not apply to FP types!");
8319 assert(N1.getValueType() == N2.getValueType() &&
8320 N1.getValueType() == VT && "Binary operator types must match!");
8321 if (VT.getScalarType() == MVT::i1)
8322 return getNode(ISD::OR, DL, VT, N1, N2);
8323 break;
8324 case ISD::SMAX:
8325 case ISD::UMIN:
8326 assert(VT.isInteger() && "This operator does not apply to FP types!");
8327 assert(N1.getValueType() == N2.getValueType() &&
8328 N1.getValueType() == VT && "Binary operator types must match!");
8329 if (VT.getScalarType() == MVT::i1)
8330 return getNode(ISD::AND, DL, VT, N1, N2);
8331 break;
8332 case ISD::FADD:
8333 case ISD::FSUB:
8334 case ISD::FMUL:
8335 case ISD::FDIV:
8336 case ISD::FREM:
8337 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
8338 assert(N1.getValueType() == N2.getValueType() &&
8339 N1.getValueType() == VT && "Binary operator types must match!");
8340 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
8341 return V;
8342 break;
8343 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
8344 assert(N1.getValueType() == VT &&
8347 "Invalid FCOPYSIGN!");
8348 break;
8349 case ISD::SHL:
8350 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
8351 const APInt &MulImm = N1->getConstantOperandAPInt(0);
8352 const APInt &ShiftImm = N2C->getAPIntValue();
8353 return getVScale(DL, VT, MulImm << ShiftImm);
8354 }
8355 [[fallthrough]];
8356 case ISD::SRA:
8357 case ISD::SRL:
8358 if (SDValue V = simplifyShift(N1, N2))
8359 return V;
8360 [[fallthrough]];
8361 case ISD::ROTL:
8362 case ISD::ROTR:
8363 case ISD::SSHLSAT:
8364 case ISD::USHLSAT:
8365 assert(VT == N1.getValueType() &&
8366 "Shift operators return type must be the same as their first arg");
8367 assert(VT.isInteger() && N2.getValueType().isInteger() &&
8368 "Shifts only work on integers");
8369 assert((!VT.isVector() || VT == N2.getValueType()) &&
8370 "Vector shift amounts must be in the same as their first arg");
8371 // Verify that the shift amount VT is big enough to hold valid shift
8372 // amounts. This catches things like trying to shift an i1024 value by an
8373 // i8, which is easy to fall into in generic code that uses
8374 // TLI.getShiftAmount().
8377 "Invalid use of small shift amount with oversized value!");
8378
8379 // Always fold shifts of i1 values so the code generator doesn't need to
8380 // handle them. Since we know the size of the shift has to be less than the
8381 // size of the value, the shift/rotate count is guaranteed to be zero.
8382 if (VT == MVT::i1)
8383 return N1;
8384 if (N2CV && N2CV->isZero())
8385 return N1;
8386 break;
8387 case ISD::FP_ROUND:
8389 VT.bitsLE(N1.getValueType()) && N2C &&
8390 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
8391 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
8392 if (N1.getValueType() == VT) return N1; // noop conversion.
8393 break;
8394 case ISD::IS_FPCLASS: {
8396 "IS_FPCLASS is used for a non-floating type");
8397 assert(isa<ConstantSDNode>(N2) && "FPClassTest is not Constant");
8398 // is.fpclass(poison, mask) -> poison
8399 if (N1.getOpcode() == ISD::POISON)
8400 return getPOISON(VT);
8401 FPClassTest Mask = static_cast<FPClassTest>(N2->getAsZExtVal());
8402 // If all tests are made, it doesn't matter what the value is.
8403 if ((Mask & fcAllFlags) == fcAllFlags)
8404 return getBoolConstant(true, DL, VT, N1.getValueType());
8405 if ((Mask & fcAllFlags) == 0)
8406 return getBoolConstant(false, DL, VT, N1.getValueType());
8407 break;
8408 }
8409 case ISD::AssertNoFPClass: {
8411 "AssertNoFPClass is used for a non-floating type");
8412 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
8413 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
8414 assert(llvm::to_underlying(NoFPClass) <=
8416 "FPClassTest value too large");
8417 (void)NoFPClass;
8418 break;
8419 }
8420 case ISD::AssertSext:
8421 case ISD::AssertZext: {
8422 EVT EVT = cast<VTSDNode>(N2)->getVT();
8423 assert(VT == N1.getValueType() && "Not an inreg extend!");
8424 assert(VT.isInteger() && EVT.isInteger() &&
8425 "Cannot *_EXTEND_INREG FP types");
8426 assert(!EVT.isVector() &&
8427 "AssertSExt/AssertZExt type should be the vector element type "
8428 "rather than the vector type!");
8429 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
8430 if (VT.getScalarType() == EVT) return N1; // noop assertion.
8431 break;
8432 }
8434 EVT EVT = cast<VTSDNode>(N2)->getVT();
8435 assert(VT == N1.getValueType() && "Not an inreg extend!");
8436 assert(VT.isInteger() && EVT.isInteger() &&
8437 "Cannot *_EXTEND_INREG FP types");
8438 assert(EVT.isVector() == VT.isVector() &&
8439 "SIGN_EXTEND_INREG type should be vector iff the operand "
8440 "type is vector!");
8441 assert((!EVT.isVector() ||
8443 "Vector element counts must match in SIGN_EXTEND_INREG");
8444 assert(EVT.getScalarType().bitsLE(VT.getScalarType()) && "Not extending!");
8445 if (EVT == VT) return N1; // Not actually extending
8446 break;
8447 }
8449 case ISD::FP_TO_UINT_SAT: {
8450 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
8451 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
8452 assert(N1.getValueType().isVector() == VT.isVector() &&
8453 "FP_TO_*INT_SAT type should be vector iff the operand type is "
8454 "vector!");
8455 assert((!VT.isVector() || VT.getVectorElementCount() ==
8457 "Vector element counts must match in FP_TO_*INT_SAT");
8458 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
8459 "Type to saturate to must be a scalar.");
8460 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
8461 "Not extending!");
8462 break;
8463 }
8466 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
8467 element type of the vector.");
8468
8469 // Extract from an undefined value or using an undefined index is undefined.
8470 if (N1.isUndef() || N2.isUndef())
8471 return getUNDEF(VT);
8472
8473 // EXTRACT_VECTOR_ELT of out-of-bounds element is POISON for fixed length
8474 // vectors. For scalable vectors we will provide appropriate support for
8475 // dealing with arbitrary indices.
8476 if (N2C && N1.getValueType().isFixedLengthVector() &&
8477 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
8478 return getPOISON(VT);
8479
8480 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
8481 // expanding copies of large vectors from registers. This only works for
8482 // fixed length vectors, since we need to know the exact number of
8483 // elements.
8484 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
8486 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
8487 return getExtractVectorElt(DL, VT,
8488 N1.getOperand(N2C->getZExtValue() / Factor),
8489 N2C->getZExtValue() % Factor);
8490 }
8491
8492 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
8493 // lowering is expanding large vector constants.
8494 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
8495 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
8498 "BUILD_VECTOR used for scalable vectors");
8499 unsigned Index =
8500 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
8501 SDValue Elt = N1.getOperand(Index);
8502
8503 if (VT != Elt.getValueType())
8504 // If the vector element type is not legal, the BUILD_VECTOR operands
8505 // are promoted and implicitly truncated, and the result implicitly
8506 // extended. Make that explicit here.
8507 Elt = getAnyExtOrTrunc(Elt, DL, VT);
8508
8509 return Elt;
8510 }
8511
8512 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
8513 // operations are lowered to scalars.
8514 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
8515 // If the indices are the same, return the inserted element else
8516 // if the indices are known different, extract the element from
8517 // the original vector.
8518 SDValue N1Op2 = N1.getOperand(2);
8520
8521 if (N1Op2C && N2C) {
8522 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
8523 if (VT == N1.getOperand(1).getValueType())
8524 return N1.getOperand(1);
8525 if (VT.isFloatingPoint()) {
8527 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
8528 }
8529 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
8530 }
8531 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
8532 }
8533 }
8534
8535 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
8536 // when vector types are scalarized and v1iX is legal.
8537 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
8538 // Here we are completely ignoring the extract element index (N2),
8539 // which is fine for fixed width vectors, since any index other than 0
8540 // is undefined anyway. However, this cannot be ignored for scalable
8541 // vectors - in theory we could support this, but we don't want to do this
8542 // without a profitability check.
8543 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8545 N1.getValueType().getVectorNumElements() == 1) {
8546 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
8547 N1.getOperand(1));
8548 }
8549 break;
8551 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
8552 assert(!N1.getValueType().isVector() && !VT.isVector() &&
8553 (N1.getValueType().isInteger() == VT.isInteger()) &&
8554 N1.getValueType() != VT &&
8555 "Wrong types for EXTRACT_ELEMENT!");
8556
8557 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
8558 // 64-bit integers into 32-bit parts. Instead of building the extract of
8559 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
8560 if (N1.getOpcode() == ISD::BUILD_PAIR)
8561 return N1.getOperand(N2C->getZExtValue());
8562
8563 // EXTRACT_ELEMENT of a constant int is also very common.
8564 if (N1C) {
8565 unsigned ElementSize = VT.getSizeInBits();
8566 unsigned Shift = ElementSize * N2C->getZExtValue();
8567 const APInt &Val = N1C->getAPIntValue();
8568 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
8569 }
8570 break;
8572 EVT N1VT = N1.getValueType();
8573 assert(VT.isVector() && N1VT.isVector() &&
8574 "Extract subvector VTs must be vectors!");
8576 "Extract subvector VTs must have the same element type!");
8577 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8578 "Cannot extract a scalable vector from a fixed length vector!");
8579 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8581 "Extract subvector must be from larger vector to smaller vector!");
8582 assert(N2C && "Extract subvector index must be a constant");
8583 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8584 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8585 N1VT.getVectorMinNumElements()) &&
8586 "Extract subvector overflow!");
8587 assert(N2C->getAPIntValue().getBitWidth() ==
8588 TLI->getVectorIdxWidth(getDataLayout()) &&
8589 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8590 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8591 "Extract index is not a multiple of the output vector length");
8592
8593 // Trivial extraction.
8594 if (VT == N1VT)
8595 return N1;
8596
8597 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8598 if (N1.isUndef())
8599 return getUNDEF(VT);
8600
8601 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8602 // the concat have the same type as the extract.
8603 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8604 VT == N1.getOperand(0).getValueType()) {
8605 unsigned Factor = VT.getVectorMinNumElements();
8606 return N1.getOperand(N2C->getZExtValue() / Factor);
8607 }
8608
8609 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8610 // during shuffle legalization.
8611 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8612 VT == N1.getOperand(1).getValueType())
8613 return N1.getOperand(1);
8614 break;
8615 }
8616 }
8617
8618 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8619 switch (Opcode) {
8620 case ISD::XOR:
8621 case ISD::ADD:
8622 case ISD::PTRADD:
8623 case ISD::SUB:
8625 case ISD::UDIV:
8626 case ISD::SDIV:
8627 case ISD::UREM:
8628 case ISD::SREM:
8629 case ISD::MUL:
8630 case ISD::AND:
8631 case ISD::SSUBSAT:
8632 case ISD::USUBSAT:
8633 case ISD::UMIN:
8634 case ISD::OR:
8635 case ISD::SADDSAT:
8636 case ISD::UADDSAT:
8637 case ISD::UMAX:
8638 case ISD::SMAX:
8639 case ISD::SMIN:
8640 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8641 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8642 }
8643 }
8644
8645 // Canonicalize an UNDEF to the RHS, even over a constant.
8646 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8647 if (TLI->isCommutativeBinOp(Opcode)) {
8648 std::swap(N1, N2);
8649 } else {
8650 switch (Opcode) {
8651 case ISD::PTRADD:
8652 case ISD::SUB:
8653 // fold op(undef, non_undef_arg2) -> undef.
8654 return N1;
8656 case ISD::UDIV:
8657 case ISD::SDIV:
8658 case ISD::UREM:
8659 case ISD::SREM:
8660 case ISD::SSUBSAT:
8661 case ISD::USUBSAT:
8662 // fold op(undef, non_undef_arg2) -> 0.
8663 return getConstant(0, DL, VT);
8664 }
8665 }
8666 }
8667
8668 // Fold a bunch of operators when the RHS is undef.
8669 if (N2.getOpcode() == ISD::UNDEF) {
8670 switch (Opcode) {
8671 case ISD::XOR:
8672 if (N1.getOpcode() == ISD::UNDEF)
8673 // Handle undef ^ undef -> 0 special case. This is a common
8674 // idiom (misuse).
8675 return getConstant(0, DL, VT);
8676 [[fallthrough]];
8677 case ISD::ADD:
8678 case ISD::PTRADD:
8679 case ISD::SUB:
8680 // fold op(arg1, undef) -> undef.
8681 return N2;
8682 case ISD::UDIV:
8683 case ISD::SDIV:
8684 case ISD::UREM:
8685 case ISD::SREM:
8686 // fold op(arg1, undef) -> poison.
8687 return getPOISON(VT);
8688 case ISD::MUL:
8689 case ISD::AND:
8690 case ISD::SSUBSAT:
8691 case ISD::USUBSAT:
8692 case ISD::UMIN:
8693 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8694 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8695 case ISD::OR:
8696 case ISD::SADDSAT:
8697 case ISD::UADDSAT:
8698 case ISD::UMAX:
8699 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8700 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8701 case ISD::SMAX:
8702 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8703 return N1.getOpcode() == ISD::UNDEF
8704 ? N2
8705 : getConstant(
8707 VT);
8708 case ISD::SMIN:
8709 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8710 return N1.getOpcode() == ISD::UNDEF
8711 ? N2
8712 : getConstant(
8714 VT);
8715 }
8716 }
8717
8718 // Perform trivial constant folding.
8719 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8720 return SV;
8721
8722 // Memoize this node if possible.
8723 SDNode *N;
8724 SDVTList VTs = getVTList(VT);
8725 SDValue Ops[] = {N1, N2};
8726 if (VT != MVT::Glue) {
8728 AddNodeIDNode(ID, Opcode, VTs, Ops);
8729 void *IP = nullptr;
8730 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8731 E->intersectFlagsWith(Flags);
8732 return SDValue(E, 0);
8733 }
8734
8735 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8736 N->setFlags(Flags);
8737 createOperands(N, Ops);
8738 CSEMap.InsertNode(N, IP);
8739 } else {
8740 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8741 createOperands(N, Ops);
8742 }
8743
8744 InsertNode(N);
8745 SDValue V = SDValue(N, 0);
8746 NewSDValueDbgMsg(V, "Creating new node: ", this);
8747 return V;
8748}
8749
8750SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8751 SDValue N1, SDValue N2, SDValue N3) {
8752 SDNodeFlags Flags;
8753 if (Inserter)
8754 Flags = Inserter->getFlags();
8755 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8756}
8757
8758SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8759 SDValue N1, SDValue N2, SDValue N3,
8760 const SDNodeFlags Flags) {
8762 N2.getOpcode() != ISD::DELETED_NODE &&
8763 N3.getOpcode() != ISD::DELETED_NODE &&
8764 "Operand is DELETED_NODE!");
8765 // Perform various simplifications.
8766 switch (Opcode) {
8767 case ISD::BUILD_VECTOR: {
8768 // Attempt to simplify BUILD_VECTOR.
8769 SDValue Ops[] = {N1, N2, N3};
8770 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8771 return V;
8772 break;
8773 }
8774 case ISD::CONCAT_VECTORS: {
8775 SDValue Ops[] = {N1, N2, N3};
8776 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8777 return V;
8778 break;
8779 }
8780 case ISD::SETCC: {
8781 assert(VT.isInteger() && "SETCC result type must be an integer!");
8782 assert(N1.getValueType() == N2.getValueType() &&
8783 "SETCC operands must have the same type!");
8784 assert(VT.isVector() == N1.getValueType().isVector() &&
8785 "SETCC type should be vector iff the operand type is vector!");
8786 assert((!VT.isVector() || VT.getVectorElementCount() ==
8788 "SETCC vector element counts must match!");
8789 // Use FoldSetCC to simplify SETCC's.
8790 if (SDValue V =
8791 FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL, Flags))
8792 return V;
8793 break;
8794 }
8795 case ISD::SELECT:
8796 case ISD::VSELECT:
8797 if (SDValue V = simplifySelect(N1, N2, N3))
8798 return V;
8799 break;
8801 llvm_unreachable("should use getVectorShuffle constructor!");
8803 if (isNullConstant(N3))
8804 return N1;
8805 break;
8807 if (isNullConstant(N3))
8808 return N2;
8809 break;
8811 assert(VT.isVector() && VT == N1.getValueType() &&
8812 "INSERT_VECTOR_ELT vector type mismatch");
8814 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8815 assert((!VT.isFloatingPoint() ||
8816 VT.getVectorElementType() == N2.getValueType()) &&
8817 "INSERT_VECTOR_ELT fp scalar type mismatch");
8818 assert((!VT.isInteger() ||
8820 "INSERT_VECTOR_ELT int scalar size mismatch");
8821
8822 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8823 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8824 // for scalable vectors where we will generate appropriate code to
8825 // deal with out-of-bounds cases correctly.
8826 if (N3C && VT.isFixedLengthVector() &&
8827 N3C->getZExtValue() >= VT.getVectorNumElements())
8828 return getUNDEF(VT);
8829
8830 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8831 if (N3.isUndef())
8832 return getUNDEF(VT);
8833
8834 // If inserting poison, just use the input vector.
8835 if (N2.getOpcode() == ISD::POISON)
8836 return N1;
8837
8838 // Inserting undef into undef/poison is still undef.
8839 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8840 return getUNDEF(VT);
8841
8842 // If the inserted element is an UNDEF, just use the input vector.
8843 // But not if skipping the insert could make the result more poisonous.
8844 if (N2.isUndef()) {
8845 if (N3C && VT.isFixedLengthVector()) {
8846 APInt EltMask =
8847 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8848 if (isGuaranteedNotToBePoison(N1, EltMask))
8849 return N1;
8850 } else if (isGuaranteedNotToBePoison(N1))
8851 return N1;
8852 }
8853 break;
8854 }
8855 case ISD::INSERT_SUBVECTOR: {
8856 // If inserting poison, just use the input vector,
8857 if (N2.getOpcode() == ISD::POISON)
8858 return N1;
8859
8860 // Inserting undef into undef/poison is still undef.
8861 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8862 return getUNDEF(VT);
8863
8864 EVT N2VT = N2.getValueType();
8865 assert(VT == N1.getValueType() &&
8866 "Dest and insert subvector source types must match!");
8867 assert(VT.isVector() && N2VT.isVector() &&
8868 "Insert subvector VTs must be vectors!");
8870 "Insert subvector VTs must have the same element type!");
8871 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8872 "Cannot insert a scalable vector into a fixed length vector!");
8873 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8875 "Insert subvector must be from smaller vector to larger vector!");
8877 "Insert subvector index must be constant");
8878 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8879 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8881 "Insert subvector overflow!");
8883 TLI->getVectorIdxWidth(getDataLayout()) &&
8884 "Constant index for INSERT_SUBVECTOR has an invalid size");
8885
8886 // Trivial insertion.
8887 if (VT == N2VT)
8888 return N2;
8889
8890 // If this is an insert of an extracted vector into an undef/poison vector,
8891 // we can just use the input to the extract. But not if skipping the
8892 // extract+insert could make the result more poisonous.
8893 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8894 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8895 if (N1.getOpcode() == ISD::POISON)
8896 return N2.getOperand(0);
8897 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8898 unsigned LoBit = N3->getAsZExtVal();
8899 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8900 APInt EltMask =
8901 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8902 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8903 return N2.getOperand(0);
8904 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8905 return N2.getOperand(0);
8906 }
8907
8908 // If the inserted subvector is UNDEF, just use the input vector.
8909 // But not if skipping the insert could make the result more poisonous.
8910 if (N2.isUndef()) {
8911 if (VT.isFixedLengthVector()) {
8912 unsigned LoBit = N3->getAsZExtVal();
8913 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8914 APInt EltMask =
8915 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8916 if (isGuaranteedNotToBePoison(N1, EltMask))
8917 return N1;
8918 } else if (isGuaranteedNotToBePoison(N1))
8919 return N1;
8920 }
8921 break;
8922 }
8923 case ISD::BITCAST:
8924 // Fold bit_convert nodes from a type to themselves.
8925 if (N1.getValueType() == VT)
8926 return N1;
8927 break;
8928 case ISD::VP_TRUNCATE:
8929 case ISD::VP_SIGN_EXTEND:
8930 case ISD::VP_ZERO_EXTEND:
8931 // Don't create noop casts.
8932 if (N1.getValueType() == VT)
8933 return N1;
8934 break;
8935 case ISD::VECTOR_COMPRESS: {
8936 [[maybe_unused]] EVT VecVT = N1.getValueType();
8937 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8938 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8939 assert(VT == VecVT && "Vector and result type don't match.");
8940 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8941 "All inputs must be vectors.");
8942 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8944 "Vector and mask must have same number of elements.");
8945
8946 if (N1.isUndef() || N2.isUndef())
8947 return N3;
8948
8949 break;
8950 }
8955 [[maybe_unused]] EVT AccVT = N1.getValueType();
8956 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8957 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8958 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8959 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8960 "node to have the same type!");
8961 assert(VT.isVector() && VT == AccVT &&
8962 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8963 "the same type as its result!");
8965 AccVT.getVectorElementCount()) &&
8966 "Expected the element count of the second and third operands of the "
8967 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8968 "element count of the first operand and the result!");
8970 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8971 "node to have an element type which is the same as or smaller than "
8972 "the element type of the first operand and result!");
8973 break;
8974 }
8975 }
8976
8977 // Perform trivial constant folding for arithmetic operators.
8978 switch (Opcode) {
8979 case ISD::FMA:
8980 case ISD::FMAD:
8981 case ISD::SETCC:
8982 case ISD::FSHL:
8983 case ISD::FSHR:
8984 if (SDValue SV =
8985 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8986 return SV;
8987 break;
8988 }
8989
8990 // Memoize node if it doesn't produce a glue result.
8991 SDNode *N;
8992 SDVTList VTs = getVTList(VT);
8993 SDValue Ops[] = {N1, N2, N3};
8994 if (VT != MVT::Glue) {
8996 AddNodeIDNode(ID, Opcode, VTs, Ops);
8997 void *IP = nullptr;
8998 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8999 E->intersectFlagsWith(Flags);
9000 return SDValue(E, 0);
9001 }
9002
9003 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
9004 N->setFlags(Flags);
9005 createOperands(N, Ops);
9006 CSEMap.InsertNode(N, IP);
9007 } else {
9008 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
9009 createOperands(N, Ops);
9010 }
9011
9012 InsertNode(N);
9013 SDValue V = SDValue(N, 0);
9014 NewSDValueDbgMsg(V, "Creating new node: ", this);
9015 return V;
9016}
9017
9018SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9019 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
9020 const SDNodeFlags Flags) {
9021 SDValue Ops[] = { N1, N2, N3, N4 };
9022 return getNode(Opcode, DL, VT, Ops, Flags);
9023}
9024
9025SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9026 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
9027 SDNodeFlags Flags;
9028 if (Inserter)
9029 Flags = Inserter->getFlags();
9030 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
9031}
9032
9033SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9034 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
9035 SDValue N5, const SDNodeFlags Flags) {
9036 SDValue Ops[] = { N1, N2, N3, N4, N5 };
9037 return getNode(Opcode, DL, VT, Ops, Flags);
9038}
9039
9040SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9041 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
9042 SDValue N5) {
9043 SDNodeFlags Flags;
9044 if (Inserter)
9045 Flags = Inserter->getFlags();
9046 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
9047}
9048
9049/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
9050/// the incoming stack arguments to be loaded from the stack.
9052 SmallVector<SDValue, 8> ArgChains;
9053
9054 // Include the original chain at the beginning of the list. When this is
9055 // used by target LowerCall hooks, this helps legalize find the
9056 // CALLSEQ_BEGIN node.
9057 ArgChains.push_back(Chain);
9058
9059 // Add a chain value for each stack argument.
9060 for (SDNode *U : getEntryNode().getNode()->users())
9061 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
9062 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
9063 if (FI->getIndex() < 0)
9064 ArgChains.push_back(SDValue(L, 1));
9065
9066 // Build a tokenfactor for all the chains.
9067 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
9068}
9069
9070/// getMemsetValue - Vectorized representation of the memset value
9071/// operand.
9073 const SDLoc &dl) {
9074 assert(!Value.isUndef());
9075
9076 unsigned NumBits = VT.getScalarSizeInBits();
9078 assert(C->getAPIntValue().getBitWidth() == 8);
9079 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
9080 if (VT.isInteger()) {
9081 bool IsOpaque = VT.getSizeInBits() > 64 ||
9082 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
9083 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
9084 }
9085 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
9086 }
9087
9088 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
9089 EVT IntVT = VT.getScalarType();
9090 if (!IntVT.isInteger())
9091 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
9092
9093 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
9094 if (NumBits > 8) {
9095 // Use a multiplication with 0x010101... to extend the input to the
9096 // required length.
9097 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
9098 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
9099 DAG.getConstant(Magic, dl, IntVT));
9100 }
9101
9102 if (VT != Value.getValueType() && !VT.isInteger())
9103 Value = DAG.getBitcast(VT.getScalarType(), Value);
9104 if (VT != Value.getValueType())
9105 Value = DAG.getSplatBuildVector(VT, dl, Value);
9106
9107 return Value;
9108}
9109
9110/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
9111/// used when a memcpy is turned into a memset when the source is a constant
9112/// string ptr.
9114 const TargetLowering &TLI,
9115 const ConstantDataArraySlice &Slice) {
9116 // Handle vector with all elements zero.
9117 if (Slice.Array == nullptr) {
9118 if (VT.isInteger())
9119 return DAG.getConstant(0, dl, VT);
9120 return DAG.getNode(ISD::BITCAST, dl, VT,
9121 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
9122 }
9123
9124 assert(!VT.isVector() && "Can't handle vector type here!");
9125 unsigned NumVTBits = VT.getSizeInBits();
9126 unsigned NumVTBytes = NumVTBits / 8;
9127 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
9128
9129 APInt Val(NumVTBits, 0);
9130 if (DAG.getDataLayout().isLittleEndian()) {
9131 for (unsigned i = 0; i != NumBytes; ++i)
9132 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
9133 } else {
9134 for (unsigned i = 0; i != NumBytes; ++i)
9135 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
9136 }
9137
9138 // If the "cost" of materializing the integer immediate is less than the cost
9139 // of a load, then it is cost effective to turn the load into the immediate.
9140 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
9141 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
9142 return DAG.getConstant(Val, dl, VT);
9143 return SDValue();
9144}
9145
9147 const SDLoc &DL,
9148 const SDNodeFlags Flags) {
9149 SDValue Index = getTypeSize(DL, Base.getValueType(), Offset);
9150 return getMemBasePlusOffset(Base, Index, DL, Flags);
9151}
9152
9154 const SDLoc &DL,
9155 const SDNodeFlags Flags) {
9156 assert(Offset.getValueType().isInteger());
9157 EVT BasePtrVT = Ptr.getValueType();
9158 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
9159 BasePtrVT))
9160 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
9161 // InBounds only applies to PTRADD, don't set it if we generate ADD.
9162 SDNodeFlags AddFlags = Flags;
9163 AddFlags.setInBounds(false);
9164 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
9165}
9166
9167/// Returns true if memcpy source is constant data.
9169 uint64_t SrcDelta = 0;
9170 GlobalAddressSDNode *G = nullptr;
9171 if (Src.getOpcode() == ISD::GlobalAddress)
9173 else if (Src->isAnyAdd() &&
9174 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
9175 Src.getOperand(1).getOpcode() == ISD::Constant) {
9176 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
9177 SrcDelta = Src.getConstantOperandVal(1);
9178 }
9179 if (!G)
9180 return false;
9181
9182 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
9183 SrcDelta + G->getOffset());
9184}
9185
9187 SelectionDAG &DAG) {
9188 // On Darwin, -Os means optimize for size without hurting performance, so
9189 // only really optimize for size when -Oz (MinSize) is used.
9191 return MF.getFunction().hasMinSize();
9192 return DAG.shouldOptForSize();
9193}
9194
9196 SmallVector<SDValue, 32> &OutChains, unsigned From,
9197 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
9198 SmallVector<SDValue, 16> &OutStoreChains) {
9199 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
9200 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
9201 SmallVector<SDValue, 16> GluedLoadChains;
9202 for (unsigned i = From; i < To; ++i) {
9203 OutChains.push_back(OutLoadChains[i]);
9204 GluedLoadChains.push_back(OutLoadChains[i]);
9205 }
9206
9207 // Chain for all loads.
9208 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
9209 GluedLoadChains);
9210
9211 for (unsigned i = From; i < To; ++i) {
9212 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
9213 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
9214 ST->getBasePtr(), ST->getMemoryVT(),
9215 ST->getMemOperand());
9216 OutChains.push_back(NewStore);
9217 }
9218}
9219
9221 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
9222 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
9223 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
9224 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
9225 // Turn a memcpy of undef to nop.
9226 // FIXME: We need to honor volatile even is Src is undef.
9227 if (Src.isUndef())
9228 return Chain;
9229
9230 // Expand memcpy to a series of load and store ops if the size operand falls
9231 // below a certain threshold.
9232 // TODO: In the AlwaysInline case, if the size is big then generate a loop
9233 // rather than maybe a humongous number of loads and stores.
9234 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9235 const DataLayout &DL = DAG.getDataLayout();
9236 LLVMContext &C = *DAG.getContext();
9237 std::vector<EVT> MemOps;
9238 bool DstAlignCanChange = false;
9240 MachineFrameInfo &MFI = MF.getFrameInfo();
9241 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9243 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9244 DstAlignCanChange = true;
9245 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
9246 if (!SrcAlign || Alignment > *SrcAlign)
9247 SrcAlign = Alignment;
9248 assert(SrcAlign && "SrcAlign must be set");
9250 // If marked as volatile, perform a copy even when marked as constant.
9251 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
9252 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
9253 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
9254 const MemOp Op = isZeroConstant
9255 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
9256 /*IsZeroMemset*/ true, isVol)
9257 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
9258 *SrcAlign, isVol, CopyFromConstant);
9259 if (!TLI.findOptimalMemOpLowering(
9260 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
9261 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes(), nullptr))
9262 return SDValue();
9263
9264 if (DstAlignCanChange) {
9265 Type *Ty = MemOps[0].getTypeForEVT(C);
9266 Align NewAlign = DL.getABITypeAlign(Ty);
9267
9268 // Don't promote to an alignment that would require dynamic stack
9269 // realignment which may conflict with optimizations such as tail call
9270 // optimization.
9272 if (!TRI->hasStackRealignment(MF))
9273 if (MaybeAlign StackAlign = DL.getStackAlignment())
9274 NewAlign = std::min(NewAlign, *StackAlign);
9275
9276 if (NewAlign > Alignment) {
9277 // Give the stack frame object a larger alignment if needed.
9278 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9279 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9280 Alignment = NewAlign;
9281 }
9282 }
9283
9284 // Prepare AAInfo for loads/stores after lowering this memcpy.
9285 AAMDNodes NewAAInfo = AAInfo;
9286 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9287
9288 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
9289 bool isConstant =
9290 BatchAA && SrcVal &&
9291 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
9292
9293 MachineMemOperand::Flags MMOFlags =
9295 SmallVector<SDValue, 16> OutLoadChains;
9296 SmallVector<SDValue, 16> OutStoreChains;
9297 SmallVector<SDValue, 32> OutChains;
9298 unsigned NumMemOps = MemOps.size();
9299 uint64_t SrcOff = 0, DstOff = 0;
9300 for (unsigned i = 0; i != NumMemOps; ++i) {
9301 EVT VT = MemOps[i];
9302 unsigned VTSize = VT.getSizeInBits() / 8;
9303 SDValue Value, Store;
9304
9305 if (VTSize > Size) {
9306 // Issuing an unaligned load / store pair that overlaps with the previous
9307 // pair. Adjust the offset accordingly.
9308 assert(i == NumMemOps-1 && i != 0);
9309 SrcOff -= VTSize - Size;
9310 DstOff -= VTSize - Size;
9311 }
9312
9313 if (CopyFromConstant &&
9314 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
9315 // It's unlikely a store of a vector immediate can be done in a single
9316 // instruction. It would require a load from a constantpool first.
9317 // We only handle zero vectors here.
9318 // FIXME: Handle other cases where store of vector immediate is done in
9319 // a single instruction.
9320 ConstantDataArraySlice SubSlice;
9321 if (SrcOff < Slice.Length) {
9322 SubSlice = Slice;
9323 SubSlice.move(SrcOff);
9324 } else {
9325 // This is an out-of-bounds access and hence UB. Pretend we read zero.
9326 SubSlice.Array = nullptr;
9327 SubSlice.Offset = 0;
9328 SubSlice.Length = VTSize;
9329 }
9330 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
9331 if (Value.getNode()) {
9332 Store = DAG.getStore(
9333 Chain, dl, Value,
9334 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9335 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
9336 OutChains.push_back(Store);
9337 }
9338 }
9339
9340 if (!Store.getNode()) {
9341 // The type might not be legal for the target. This should only happen
9342 // if the type is smaller than a legal type, as on PPC, so the right
9343 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
9344 // to Load/Store if NVT==VT.
9345 // FIXME does the case above also need this?
9346 EVT NVT = TLI.getTypeToTransformTo(C, VT);
9347 assert(NVT.bitsGE(VT));
9348
9349 bool isDereferenceable =
9350 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
9351 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
9352 if (isDereferenceable)
9354 if (isConstant)
9355 SrcMMOFlags |= MachineMemOperand::MOInvariant;
9356
9357 Value = DAG.getExtLoad(
9358 ISD::EXTLOAD, dl, NVT, Chain,
9359 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
9360 SrcPtrInfo.getWithOffset(SrcOff), VT,
9361 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
9362 OutLoadChains.push_back(Value.getValue(1));
9363
9364 Store = DAG.getTruncStore(
9365 Chain, dl, Value,
9366 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9367 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
9368 OutStoreChains.push_back(Store);
9369 }
9370 SrcOff += VTSize;
9371 DstOff += VTSize;
9372 Size -= VTSize;
9373 }
9374
9375 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
9377 unsigned NumLdStInMemcpy = OutStoreChains.size();
9378
9379 if (NumLdStInMemcpy) {
9380 // It may be that memcpy might be converted to memset if it's memcpy
9381 // of constants. In such a case, we won't have loads and stores, but
9382 // just stores. In the absence of loads, there is nothing to gang up.
9383 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
9384 // If target does not care, just leave as it.
9385 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
9386 OutChains.push_back(OutLoadChains[i]);
9387 OutChains.push_back(OutStoreChains[i]);
9388 }
9389 } else {
9390 // Ld/St less than/equal limit set by target.
9391 if (NumLdStInMemcpy <= GluedLdStLimit) {
9392 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
9393 NumLdStInMemcpy, OutLoadChains,
9394 OutStoreChains);
9395 } else {
9396 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
9397 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
9398 unsigned GlueIter = 0;
9399
9400 // Residual ld/st.
9401 if (RemainingLdStInMemcpy) {
9403 DAG, dl, OutChains, NumLdStInMemcpy - RemainingLdStInMemcpy,
9404 NumLdStInMemcpy, OutLoadChains, OutStoreChains);
9405 }
9406
9407 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
9408 unsigned IndexFrom = NumLdStInMemcpy - RemainingLdStInMemcpy -
9409 GlueIter - GluedLdStLimit;
9410 unsigned IndexTo = NumLdStInMemcpy - RemainingLdStInMemcpy - GlueIter;
9411 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
9412 OutLoadChains, OutStoreChains);
9413 GlueIter += GluedLdStLimit;
9414 }
9415 }
9416 }
9417 }
9418 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9419}
9420
9422 SDValue Chain, SDValue Dst, SDValue Src,
9423 uint64_t Size, Align Alignment,
9424 bool isVol, bool AlwaysInline,
9425 MachinePointerInfo DstPtrInfo,
9426 MachinePointerInfo SrcPtrInfo,
9427 const AAMDNodes &AAInfo) {
9428 // Turn a memmove of undef to nop.
9429 // FIXME: We need to honor volatile even is Src is undef.
9430 if (Src.isUndef())
9431 return Chain;
9432
9433 // Expand memmove to a series of load and store ops if the size operand falls
9434 // below a certain threshold.
9435 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9436 const DataLayout &DL = DAG.getDataLayout();
9437 LLVMContext &C = *DAG.getContext();
9438 std::vector<EVT> MemOps;
9439 bool DstAlignCanChange = false;
9441 MachineFrameInfo &MFI = MF.getFrameInfo();
9442 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9444 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9445 DstAlignCanChange = true;
9446 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
9447 if (!SrcAlign || Alignment > *SrcAlign)
9448 SrcAlign = Alignment;
9449 assert(SrcAlign && "SrcAlign must be set");
9450 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
9451 if (!TLI.findOptimalMemOpLowering(
9452 C, MemOps, Limit,
9453 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign, isVol),
9454 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
9455 MF.getFunction().getAttributes(), nullptr))
9456 return SDValue();
9457
9458 if (DstAlignCanChange) {
9459 Type *Ty = MemOps[0].getTypeForEVT(C);
9460 Align NewAlign = DL.getABITypeAlign(Ty);
9461
9462 // Don't promote to an alignment that would require dynamic stack
9463 // realignment which may conflict with optimizations such as tail call
9464 // optimization.
9466 if (!TRI->hasStackRealignment(MF))
9467 if (MaybeAlign StackAlign = DL.getStackAlignment())
9468 NewAlign = std::min(NewAlign, *StackAlign);
9469
9470 if (NewAlign > Alignment) {
9471 // Give the stack frame object a larger alignment if needed.
9472 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9473 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9474 Alignment = NewAlign;
9475 }
9476 }
9477
9478 // Prepare AAInfo for loads/stores after lowering this memmove.
9479 AAMDNodes NewAAInfo = AAInfo;
9480 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9481
9482 MachineMemOperand::Flags MMOFlags =
9484 uint64_t SrcOff = 0;
9485 SmallVector<SDValue, 8> LoadValues;
9486 SmallVector<SDValue, 8> LoadChains;
9487 SmallVector<SDValue, 8> OutChains;
9488 unsigned NumMemOps = MemOps.size();
9489 for (unsigned i = 0; i < NumMemOps; i++) {
9490 EVT VT = MemOps[i];
9491 unsigned VTSize = VT.getSizeInBits() / 8;
9492 SDValue Value;
9493 bool IsOverlapping = false;
9494
9495 if (i == NumMemOps - 1 && i != 0 && VTSize > Size - SrcOff) {
9496 // Issuing an unaligned load / store pair that overlaps with the previous
9497 // pair. Adjust the offset accordingly.
9498 SrcOff = Size - VTSize;
9499 IsOverlapping = true;
9500 }
9501
9502 // Calculate the actual alignment at the current offset. The alignment at
9503 // SrcOff may be lower than the base alignment, especially when using
9504 // overlapping loads.
9505 Align SrcAlignAtOffset = commonAlignment(*SrcAlign, SrcOff);
9506 if (IsOverlapping) {
9507 // Verify that the target allows misaligned memory accesses at the
9508 // adjusted offset when using overlapping loads.
9509 unsigned Fast;
9510 if (!TLI.allowsMisalignedMemoryAccesses(VT, SrcPtrInfo.getAddrSpace(),
9511 SrcAlignAtOffset, MMOFlags,
9512 &Fast) ||
9513 !Fast) {
9514 // This should have been caught by findOptimalMemOpLowering, but verify
9515 // here for safety.
9516 return SDValue();
9517 }
9518 }
9519
9520 bool isDereferenceable =
9521 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
9522 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
9523 if (isDereferenceable)
9525 Value =
9526 DAG.getLoad(VT, dl, Chain,
9527 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
9528 SrcPtrInfo.getWithOffset(SrcOff), SrcAlignAtOffset,
9529 SrcMMOFlags, NewAAInfo);
9530 LoadValues.push_back(Value);
9531 LoadChains.push_back(Value.getValue(1));
9532 SrcOff += VTSize;
9533 }
9534 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
9535 OutChains.clear();
9536 uint64_t DstOff = 0;
9537 for (unsigned i = 0; i < NumMemOps; i++) {
9538 EVT VT = MemOps[i];
9539 unsigned VTSize = VT.getSizeInBits() / 8;
9540 SDValue Store;
9541 bool IsOverlapping = false;
9542
9543 if (i == NumMemOps - 1 && i != 0 && VTSize > Size - DstOff) {
9544 // Issuing an unaligned load / store pair that overlaps with the previous
9545 // pair. Adjust the offset accordingly.
9546 DstOff = Size - VTSize;
9547 IsOverlapping = true;
9548 }
9549
9550 // Calculate the actual alignment at the current offset. The alignment at
9551 // DstOff may be lower than the base alignment, especially when using
9552 // overlapping stores.
9553 Align DstAlignAtOffset = commonAlignment(Alignment, DstOff);
9554 if (IsOverlapping) {
9555 // Verify that the target allows misaligned memory accesses at the
9556 // adjusted offset when using overlapping stores.
9557 unsigned Fast;
9558 if (!TLI.allowsMisalignedMemoryAccesses(VT, DstPtrInfo.getAddrSpace(),
9559 DstAlignAtOffset, MMOFlags,
9560 &Fast) ||
9561 !Fast) {
9562 // This should have been caught by findOptimalMemOpLowering, but verify
9563 // here for safety.
9564 return SDValue();
9565 }
9566 }
9567 Store = DAG.getStore(
9568 Chain, dl, LoadValues[i],
9569 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9570 DstPtrInfo.getWithOffset(DstOff), DstAlignAtOffset, MMOFlags,
9571 NewAAInfo);
9572 OutChains.push_back(Store);
9573 DstOff += VTSize;
9574 }
9575
9576 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9577}
9578
9579/// Lower the call to 'memset' intrinsic function into a series of store
9580/// operations.
9581///
9582/// \param DAG Selection DAG where lowered code is placed.
9583/// \param dl Link to corresponding IR location.
9584/// \param Chain Control flow dependency.
9585/// \param Dst Pointer to destination memory location.
9586/// \param Src Value of byte to write into the memory.
9587/// \param Size Number of bytes to write.
9588/// \param Alignment Alignment of the destination in bytes.
9589/// \param isVol True if destination is volatile.
9590/// \param AlwaysInline Makes sure no function call is generated.
9591/// \param DstPtrInfo IR information on the memory pointer.
9592/// \returns New head in the control flow, if lowering was successful, empty
9593/// SDValue otherwise.
9594///
9595/// The function tries to replace 'llvm.memset' intrinsic with several store
9596/// operations and value calculation code. This is usually profitable for small
9597/// memory size or when the semantic requires inlining.
9599 SDValue Chain, SDValue Dst, SDValue Src,
9600 uint64_t Size, Align Alignment, bool isVol,
9601 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
9602 const AAMDNodes &AAInfo) {
9603 // Turn a memset of undef to nop.
9604 // FIXME: We need to honor volatile even is Src is undef.
9605 if (Src.isUndef())
9606 return Chain;
9607
9608 // Expand memset to a series of load/store ops if the size operand
9609 // falls below a certain threshold.
9610 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9611 std::vector<EVT> MemOps;
9612 bool DstAlignCanChange = false;
9613 LLVMContext &C = *DAG.getContext();
9615 MachineFrameInfo &MFI = MF.getFrameInfo();
9616 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9618 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9619 DstAlignCanChange = true;
9620 bool IsZeroVal = isNullConstant(Src);
9621 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
9622
9623 EVT LargestVT;
9624 if (!TLI.findOptimalMemOpLowering(
9625 C, MemOps, Limit,
9626 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
9627 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes(),
9628 &LargestVT))
9629 return SDValue();
9630
9631 if (DstAlignCanChange) {
9632 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9633 const DataLayout &DL = DAG.getDataLayout();
9634 Align NewAlign = DL.getABITypeAlign(Ty);
9635
9636 // Don't promote to an alignment that would require dynamic stack
9637 // realignment which may conflict with optimizations such as tail call
9638 // optimization.
9640 if (!TRI->hasStackRealignment(MF))
9641 if (MaybeAlign StackAlign = DL.getStackAlignment())
9642 NewAlign = std::min(NewAlign, *StackAlign);
9643
9644 if (NewAlign > Alignment) {
9645 // Give the stack frame object a larger alignment if needed.
9646 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9647 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9648 Alignment = NewAlign;
9649 }
9650 }
9651
9652 SmallVector<SDValue, 8> OutChains;
9653 uint64_t DstOff = 0;
9654 unsigned NumMemOps = MemOps.size();
9655
9656 // Find the largest store and generate the bit pattern for it.
9657 // If target didn't set LargestVT, compute it from MemOps.
9658 if (!LargestVT.isSimple()) {
9659 LargestVT = MemOps[0];
9660 for (unsigned i = 1; i < NumMemOps; i++)
9661 if (MemOps[i].bitsGT(LargestVT))
9662 LargestVT = MemOps[i];
9663 }
9664 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9665
9666 // Prepare AAInfo for loads/stores after lowering this memset.
9667 AAMDNodes NewAAInfo = AAInfo;
9668 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9669
9670 for (unsigned i = 0; i < NumMemOps; i++) {
9671 EVT VT = MemOps[i];
9672 unsigned VTSize = VT.getSizeInBits() / 8;
9673 // The target should specify store types that exactly cover the memset size
9674 // (with the last store potentially being oversized for overlapping stores).
9675 assert(Size > 0 && "Target specified more stores than needed in "
9676 "findOptimalMemOpLowering");
9677 if (VTSize > Size) {
9678 // Issuing an unaligned load / store pair that overlaps with the previous
9679 // pair. Adjust the offset accordingly.
9680 assert(i == NumMemOps-1 && i != 0);
9681 DstOff -= VTSize - Size;
9682 }
9683
9684 // If this store is smaller than the largest store see whether we can get
9685 // the smaller value for free with a truncate or extract vector element and
9686 // then store.
9687 SDValue Value = MemSetValue;
9688 if (VT.bitsLT(LargestVT)) {
9689 unsigned Index;
9690 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9691 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9692 if (!LargestVT.isVector() && !VT.isVector() &&
9693 TLI.isTruncateFree(LargestVT, VT))
9694 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9695 else if (LargestVT.isVector() && !VT.isVector() &&
9697 LargestVT.getTypeForEVT(*DAG.getContext()),
9698 VT.getSizeInBits(), Index) &&
9699 TLI.isTypeLegal(SVT) &&
9700 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9701 // Target which can combine store(extractelement VectorTy, Idx) can get
9702 // the smaller value for free.
9703 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9704 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9705 } else
9706 Value = getMemsetValue(Src, VT, DAG, dl);
9707 }
9708 assert(Value.getValueType() == VT && "Value with wrong type.");
9709 SDValue Store = DAG.getStore(
9710 Chain, dl, Value,
9711 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9712 DstPtrInfo.getWithOffset(DstOff), Alignment,
9714 NewAAInfo);
9715 OutChains.push_back(Store);
9716 DstOff += VT.getSizeInBits() / 8;
9717 // For oversized overlapping stores, only subtract the remaining bytes.
9718 // For normal stores, subtract the full store size.
9719 if (VTSize > Size) {
9720 Size = 0;
9721 } else {
9722 Size -= VTSize;
9723 }
9724 }
9725
9726 // After processing all stores, Size should be exactly 0. Any remaining bytes
9727 // indicate a bug in the target's findOptimalMemOpLowering implementation.
9728 assert(Size == 0 && "Target's findOptimalMemOpLowering did not specify "
9729 "stores that exactly cover the memset size");
9730
9731 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9732}
9733
9735 unsigned AS) {
9736 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9737 // pointer operands can be losslessly bitcasted to pointers of address space 0
9738 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9739 report_fatal_error("cannot lower memory intrinsic in address space " +
9740 Twine(AS));
9741 }
9742}
9743
9745 const SelectionDAG *SelDAG,
9746 bool AllowReturnsFirstArg) {
9747 if (!CI || !CI->isTailCall())
9748 return false;
9749 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9750 // helper symbol we lower to.
9751 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9752 AllowReturnsFirstArg &&
9754}
9755
9756static std::pair<SDValue, SDValue>
9759 const CallInst *CI, RTLIB::Libcall Call,
9760 SelectionDAG *DAG, const TargetLowering *TLI) {
9761 RTLIB::LibcallImpl LCImpl = DAG->getLibcalls().getLibcallImpl(Call);
9762
9763 if (LCImpl == RTLIB::Unsupported)
9764 return {};
9765
9767 bool IsTailCall =
9768 isInTailCallPositionWrapper(CI, DAG, /*AllowReturnsFirstArg=*/true);
9769 SDValue Callee =
9770 DAG->getExternalSymbol(LCImpl, TLI->getPointerTy(DAG->getDataLayout()));
9771
9772 CLI.setDebugLoc(dl)
9773 .setChain(Chain)
9775 CI->getType(), Callee, std::move(Args))
9776 .setTailCall(IsTailCall);
9777
9778 return TLI->LowerCallTo(CLI);
9779}
9780
9781std::pair<SDValue, SDValue> SelectionDAG::getStrcmp(SDValue Chain,
9782 const SDLoc &dl, SDValue S1,
9783 SDValue S2,
9784 const CallInst *CI) {
9786 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9787 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9788 RTLIB::STRCMP, this, TLI);
9789}
9790
9791std::pair<SDValue, SDValue> SelectionDAG::getStrstr(SDValue Chain,
9792 const SDLoc &dl, SDValue S1,
9793 SDValue S2,
9794 const CallInst *CI) {
9796 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9797 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9798 RTLIB::STRSTR, this, TLI);
9799}
9800
9801std::pair<SDValue, SDValue> SelectionDAG::getMemccpy(SDValue Chain,
9802 const SDLoc &dl,
9803 SDValue Dst, SDValue Src,
9805 const CallInst *CI) {
9807
9809 {Dst, PT},
9810 {Src, PT},
9813 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9814 RTLIB::MEMCCPY, this, TLI);
9815}
9816
9817std::pair<SDValue, SDValue>
9819 SDValue Mem1, SDValue Size, const CallInst *CI) {
9822 {Mem0, PT},
9823 {Mem1, PT},
9825 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9826 RTLIB::MEMCMP, this, TLI);
9827}
9828
9829std::pair<SDValue, SDValue> SelectionDAG::getStrcpy(SDValue Chain,
9830 const SDLoc &dl,
9831 SDValue Dst, SDValue Src,
9832 const CallInst *CI) {
9834 TargetLowering::ArgListTy Args = {{Dst, PT}, {Src, PT}};
9835 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9836 RTLIB::STRCPY, this, TLI);
9837}
9838
9839std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9840 const SDLoc &dl,
9841 SDValue Src,
9842 const CallInst *CI) {
9843 // Emit a library call.
9846 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9847 RTLIB::STRLEN, this, TLI);
9848}
9849
9851 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9852 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9853 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9854 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9855 BatchAAResults *BatchAA) {
9856 // Check to see if we should lower the memcpy to loads and stores first.
9857 // For cases within the target-specified limits, this is the best choice.
9859 if (ConstantSize) {
9860 // Memcpy with size zero? Just return the original chain.
9861 if (ConstantSize->isZero())
9862 return Chain;
9863
9865 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9866 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9867 if (Result.getNode())
9868 return Result;
9869 }
9870
9871 // Then check to see if we should lower the memcpy with target-specific
9872 // code. If the target chooses to do this, this is the next best.
9873 if (TSI) {
9874 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9875 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9876 DstPtrInfo, SrcPtrInfo);
9877 if (Result.getNode())
9878 return Result;
9879 }
9880
9881 // If we really need inline code and the target declined to provide it,
9882 // use a (potentially long) sequence of loads and stores.
9883 if (AlwaysInline) {
9884 assert(ConstantSize && "AlwaysInline requires a constant size!");
9886 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9887 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9888 }
9889
9892
9893 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9894 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9895 // respect volatile, so they may do things like read or write memory
9896 // beyond the given memory regions. But fixing this isn't easy, and most
9897 // people don't care.
9898
9899 // Emit a library call.
9902 Args.emplace_back(Dst, PtrTy);
9903 Args.emplace_back(Src, PtrTy);
9904 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9905 // FIXME: pass in SDLoc
9907 bool IsTailCall = false;
9908 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9909
9910 if (OverrideTailCall.has_value()) {
9911 IsTailCall = *OverrideTailCall;
9912 } else {
9913 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9914 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9915 }
9916
9917 CLI.setDebugLoc(dl)
9918 .setChain(Chain)
9919 .setLibCallee(
9920 Libcalls->getLibcallImplCallingConv(MemCpyImpl),
9921 Dst.getValueType().getTypeForEVT(*getContext()),
9922 getExternalSymbol(MemCpyImpl, TLI->getPointerTy(getDataLayout())),
9923 std::move(Args))
9925 .setTailCall(IsTailCall);
9926
9927 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9928 return CallResult.second;
9929}
9930
9932 SDValue Dst, SDValue Src, SDValue Size,
9933 Type *SizeTy, unsigned ElemSz,
9934 bool isTailCall,
9935 MachinePointerInfo DstPtrInfo,
9936 MachinePointerInfo SrcPtrInfo) {
9937 // Emit a library call.
9940 Args.emplace_back(Dst, ArgTy);
9941 Args.emplace_back(Src, ArgTy);
9942 Args.emplace_back(Size, SizeTy);
9943
9944 RTLIB::Libcall LibraryCall =
9946 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9947 if (LibcallImpl == RTLIB::Unsupported)
9948 report_fatal_error("Unsupported element size");
9949
9951 CLI.setDebugLoc(dl)
9952 .setChain(Chain)
9953 .setLibCallee(
9954 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9956 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9957 std::move(Args))
9959 .setTailCall(isTailCall);
9960
9961 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9962 return CallResult.second;
9963}
9964
9966 SDValue Src, SDValue Size, Align Alignment,
9967 bool isVol, const CallInst *CI,
9968 std::optional<bool> OverrideTailCall,
9969 MachinePointerInfo DstPtrInfo,
9970 MachinePointerInfo SrcPtrInfo,
9971 const AAMDNodes &AAInfo,
9972 BatchAAResults *BatchAA) {
9973 // Check to see if we should lower the memmove to loads and stores first.
9974 // For cases within the target-specified limits, this is the best choice.
9976 if (ConstantSize) {
9977 // Memmove with size zero? Just return the original chain.
9978 if (ConstantSize->isZero())
9979 return Chain;
9980
9982 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9983 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9984 if (Result.getNode())
9985 return Result;
9986 }
9987
9988 // Then check to see if we should lower the memmove with target-specific
9989 // code. If the target chooses to do this, this is the next best.
9990 if (TSI) {
9991 SDValue Result =
9992 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9993 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9994 if (Result.getNode())
9995 return Result;
9996 }
9997
10000
10001 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
10002 // not be safe. See memcpy above for more details.
10003
10004 // Emit a library call.
10007 Args.emplace_back(Dst, PtrTy);
10008 Args.emplace_back(Src, PtrTy);
10009 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
10010 // FIXME: pass in SDLoc
10012
10013 RTLIB::LibcallImpl MemmoveImpl = Libcalls->getLibcallImpl(RTLIB::MEMMOVE);
10014
10015 bool IsTailCall = false;
10016 if (OverrideTailCall.has_value()) {
10017 IsTailCall = *OverrideTailCall;
10018 } else {
10019 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
10020 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
10021 }
10022
10023 CLI.setDebugLoc(dl)
10024 .setChain(Chain)
10025 .setLibCallee(
10026 Libcalls->getLibcallImplCallingConv(MemmoveImpl),
10027 Dst.getValueType().getTypeForEVT(*getContext()),
10028 getExternalSymbol(MemmoveImpl, TLI->getPointerTy(getDataLayout())),
10029 std::move(Args))
10031 .setTailCall(IsTailCall);
10032
10033 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
10034 return CallResult.second;
10035}
10036
10038 SDValue Dst, SDValue Src, SDValue Size,
10039 Type *SizeTy, unsigned ElemSz,
10040 bool isTailCall,
10041 MachinePointerInfo DstPtrInfo,
10042 MachinePointerInfo SrcPtrInfo) {
10043 // Emit a library call.
10045 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
10046 Args.emplace_back(Dst, IntPtrTy);
10047 Args.emplace_back(Src, IntPtrTy);
10048 Args.emplace_back(Size, SizeTy);
10049
10050 RTLIB::Libcall LibraryCall =
10052 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
10053 if (LibcallImpl == RTLIB::Unsupported)
10054 report_fatal_error("Unsupported element size");
10055
10057 CLI.setDebugLoc(dl)
10058 .setChain(Chain)
10059 .setLibCallee(
10060 Libcalls->getLibcallImplCallingConv(LibcallImpl),
10062 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
10063 std::move(Args))
10065 .setTailCall(isTailCall);
10066
10067 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
10068 return CallResult.second;
10069}
10070
10072 SDValue Src, SDValue Size, Align Alignment,
10073 bool isVol, bool AlwaysInline,
10074 const CallInst *CI,
10075 MachinePointerInfo DstPtrInfo,
10076 const AAMDNodes &AAInfo) {
10077 // Check to see if we should lower the memset to stores first.
10078 // For cases within the target-specified limits, this is the best choice.
10080 if (ConstantSize) {
10081 // Memset with size zero? Just return the original chain.
10082 if (ConstantSize->isZero())
10083 return Chain;
10084
10085 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
10086 ConstantSize->getZExtValue(), Alignment,
10087 isVol, false, DstPtrInfo, AAInfo);
10088
10089 if (Result.getNode())
10090 return Result;
10091 }
10092
10093 // Then check to see if we should lower the memset with target-specific
10094 // code. If the target chooses to do this, this is the next best.
10095 if (TSI) {
10096 SDValue Result = TSI->EmitTargetCodeForMemset(
10097 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
10098 if (Result.getNode())
10099 return Result;
10100 }
10101
10102 // If we really need inline code and the target declined to provide it,
10103 // use a (potentially long) sequence of loads and stores.
10104 if (AlwaysInline) {
10105 assert(ConstantSize && "AlwaysInline requires a constant size!");
10106 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
10107 ConstantSize->getZExtValue(), Alignment,
10108 isVol, true, DstPtrInfo, AAInfo);
10109 assert(Result &&
10110 "getMemsetStores must return a valid sequence when AlwaysInline");
10111 return Result;
10112 }
10113
10115
10116 // Emit a library call.
10117 auto &Ctx = *getContext();
10118 const auto& DL = getDataLayout();
10119
10121 // FIXME: pass in SDLoc
10122 CLI.setDebugLoc(dl).setChain(Chain);
10123
10124 RTLIB::LibcallImpl BzeroImpl = Libcalls->getLibcallImpl(RTLIB::BZERO);
10125 bool UseBZero = BzeroImpl != RTLIB::Unsupported && isNullConstant(Src);
10126
10127 // If zeroing out and bzero is present, use it.
10128 if (UseBZero) {
10130 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
10131 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
10132 CLI.setLibCallee(
10133 Libcalls->getLibcallImplCallingConv(BzeroImpl), Type::getVoidTy(Ctx),
10134 getExternalSymbol(BzeroImpl, TLI->getPointerTy(DL)), std::move(Args));
10135 } else {
10136 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
10137
10139 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
10140 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
10141 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
10142 CLI.setLibCallee(Libcalls->getLibcallImplCallingConv(MemsetImpl),
10143 Dst.getValueType().getTypeForEVT(Ctx),
10144 getExternalSymbol(MemsetImpl, TLI->getPointerTy(DL)),
10145 std::move(Args));
10146 }
10147
10148 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
10149 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
10150
10151 // If we're going to use bzero, make sure not to tail call unless the
10152 // subsequent return doesn't need a value, as bzero doesn't return the first
10153 // arg unlike memset.
10154 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
10155 bool IsTailCall =
10156 CI && CI->isTailCall() &&
10157 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
10158 CLI.setDiscardResult().setTailCall(IsTailCall);
10159
10160 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
10161 return CallResult.second;
10162}
10163
10166 Type *SizeTy, unsigned ElemSz,
10167 bool isTailCall,
10168 MachinePointerInfo DstPtrInfo) {
10169 // Emit a library call.
10171 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
10172 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
10173 Args.emplace_back(Size, SizeTy);
10174
10175 RTLIB::Libcall LibraryCall =
10177 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
10178 if (LibcallImpl == RTLIB::Unsupported)
10179 report_fatal_error("Unsupported element size");
10180
10182 CLI.setDebugLoc(dl)
10183 .setChain(Chain)
10184 .setLibCallee(
10185 Libcalls->getLibcallImplCallingConv(LibcallImpl),
10187 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
10188 std::move(Args))
10190 .setTailCall(isTailCall);
10191
10192 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
10193 return CallResult.second;
10194}
10195
10196SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
10198 MachineMemOperand *MMO,
10199 ISD::LoadExtType ExtType) {
10201 AddNodeIDNode(ID, Opcode, VTList, Ops);
10202 ID.AddInteger(MemVT.getRawBits());
10203 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
10204 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
10205 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10206 ID.AddInteger(MMO->getFlags());
10207 void* IP = nullptr;
10208 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10209 E->refineAlignment(MMO);
10210 E->refineRanges(MMO);
10211 return SDValue(E, 0);
10212 }
10213
10214 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
10215 VTList, MemVT, MMO, ExtType);
10216 createOperands(N, Ops);
10217
10218 CSEMap.InsertNode(N, IP);
10219 InsertNode(N);
10220 SDValue V(N, 0);
10221 NewSDValueDbgMsg(V, "Creating new node: ", this);
10222 return V;
10223}
10224
10226 EVT MemVT, SDVTList VTs, SDValue Chain,
10227 SDValue Ptr, SDValue Cmp, SDValue Swp,
10228 MachineMemOperand *MMO) {
10229 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
10231 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
10232
10233 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
10234 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
10235}
10236
10237SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
10238 SDValue Chain, SDValue Ptr, SDValue Val,
10239 MachineMemOperand *MMO) {
10240 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
10241 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
10242 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
10243 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
10244 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
10245 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
10246 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
10247 Opcode == ISD::ATOMIC_LOAD_FMIN ||
10248 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
10249 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
10250 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
10251 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
10252 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
10253 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
10254 Opcode == ISD::ATOMIC_STORE) &&
10255 "Invalid Atomic Op");
10256
10257 EVT VT = Val.getValueType();
10258
10259 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
10260 getVTList(VT, MVT::Other);
10261 SDValue Ops[] = {Chain, Ptr, Val};
10262 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
10263}
10264
10266 EVT MemVT, EVT VT, SDValue Chain,
10267 SDValue Ptr, MachineMemOperand *MMO) {
10268 SDVTList VTs = getVTList(VT, MVT::Other);
10269 SDValue Ops[] = {Chain, Ptr};
10270 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
10271}
10272
10273/// getMergeValues - Create a MERGE_VALUES node from the given operands.
10275 if (Ops.size() == 1)
10276 return Ops[0];
10277
10279 VTs.reserve(Ops.size());
10280 for (const SDValue &Op : Ops)
10281 VTs.push_back(Op.getValueType());
10282 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
10283}
10284
10286 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
10287 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
10289 const AAMDNodes &AAInfo) {
10290 if (Size.hasValue() && !Size.getValue())
10292
10294 MachineMemOperand *MMO =
10295 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
10296
10297 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
10298}
10299
10301 SDVTList VTList,
10302 ArrayRef<SDValue> Ops, EVT MemVT,
10303 MachineMemOperand *MMO) {
10304 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, ArrayRef(MMO));
10305}
10306
10308 SDVTList VTList,
10309 ArrayRef<SDValue> Ops, EVT MemVT,
10311 assert(!MMOs.empty() && "Must have at least one MMO");
10312 assert(
10313 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
10314 Opcode == ISD::PREFETCH ||
10315 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
10316 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
10317 "Opcode is not a memory-accessing opcode!");
10318
10320 if (MMOs.size() == 1) {
10321 MemRefs = MMOs[0];
10322 } else {
10323 // Allocate: [size_t count][MMO*][MMO*]...
10324 size_t AllocSize =
10325 sizeof(size_t) + MMOs.size() * sizeof(MachineMemOperand *);
10326 void *Buffer = Allocator.Allocate(AllocSize, alignof(size_t));
10327 size_t *CountPtr = static_cast<size_t *>(Buffer);
10328 *CountPtr = MMOs.size();
10329 MachineMemOperand **Array =
10330 reinterpret_cast<MachineMemOperand **>(CountPtr + 1);
10331 llvm::copy(MMOs, Array);
10332 MemRefs = Array;
10333 }
10334
10335 // Memoize the node unless it returns a glue result.
10337 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
10339 AddNodeIDNode(ID, Opcode, VTList, Ops);
10340 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
10341 Opcode, dl.getIROrder(), VTList, MemVT, MemRefs));
10342 ID.AddInteger(MemVT.getRawBits());
10343 for (const MachineMemOperand *MMO : MMOs) {
10344 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10345 ID.AddInteger(MMO->getFlags());
10346 }
10347 void *IP = nullptr;
10348 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10349 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMOs);
10350 return SDValue(E, 0);
10351 }
10352
10353 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
10354 VTList, MemVT, MemRefs);
10355 createOperands(N, Ops);
10356 CSEMap.InsertNode(N, IP);
10357 } else {
10358 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
10359 VTList, MemVT, MemRefs);
10360 createOperands(N, Ops);
10361 }
10362 InsertNode(N);
10363 SDValue V(N, 0);
10364 NewSDValueDbgMsg(V, "Creating new node: ", this);
10365 return V;
10366}
10367
10369 SDValue Chain, int FrameIndex) {
10370 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
10371 const auto VTs = getVTList(MVT::Other);
10372 SDValue Ops[2] = {
10373 Chain,
10374 getFrameIndex(FrameIndex,
10375 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
10376 true)};
10377
10379 AddNodeIDNode(ID, Opcode, VTs, Ops);
10380 ID.AddInteger(FrameIndex);
10381 void *IP = nullptr;
10382 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10383 return SDValue(E, 0);
10384
10385 LifetimeSDNode *N =
10386 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
10387 createOperands(N, Ops);
10388 CSEMap.InsertNode(N, IP);
10389 InsertNode(N);
10390 SDValue V(N, 0);
10391 NewSDValueDbgMsg(V, "Creating new node: ", this);
10392 return V;
10393}
10394
10396 uint64_t Guid, uint64_t Index,
10397 uint32_t Attr) {
10398 const unsigned Opcode = ISD::PSEUDO_PROBE;
10399 const auto VTs = getVTList(MVT::Other);
10400 SDValue Ops[] = {Chain};
10402 AddNodeIDNode(ID, Opcode, VTs, Ops);
10403 ID.AddInteger(Guid);
10404 ID.AddInteger(Index);
10405 void *IP = nullptr;
10406 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
10407 return SDValue(E, 0);
10408
10409 auto *N = newSDNode<PseudoProbeSDNode>(
10410 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
10411 createOperands(N, Ops);
10412 CSEMap.InsertNode(N, IP);
10413 InsertNode(N);
10414 SDValue V(N, 0);
10415 NewSDValueDbgMsg(V, "Creating new node: ", this);
10416 return V;
10417}
10418
10419/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
10420/// MachinePointerInfo record from it. This is particularly useful because the
10421/// code generator has many cases where it doesn't bother passing in a
10422/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
10424 SelectionDAG &DAG, SDValue Ptr,
10425 int64_t Offset = 0) {
10426 // If this is FI+Offset, we can model it.
10427 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
10429 FI->getIndex(), Offset);
10430
10431 // If this is (FI+Offset1)+Offset2, we can model it.
10432 if (Ptr.getOpcode() != ISD::ADD ||
10435 return Info;
10436
10437 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
10439 DAG.getMachineFunction(), FI,
10440 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
10441}
10442
10443/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
10444/// MachinePointerInfo record from it. This is particularly useful because the
10445/// code generator has many cases where it doesn't bother passing in a
10446/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
10448 SelectionDAG &DAG, SDValue Ptr,
10449 SDValue OffsetOp) {
10450 // If the 'Offset' value isn't a constant, we can't handle this.
10452 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
10453 if (OffsetOp.isUndef())
10454 return InferPointerInfo(Info, DAG, Ptr);
10455 return Info;
10456}
10457
10459 EVT VT, const SDLoc &dl, SDValue Chain,
10460 SDValue Ptr, SDValue Offset,
10461 MachinePointerInfo PtrInfo, EVT MemVT,
10462 Align Alignment,
10463 MachineMemOperand::Flags MMOFlags,
10464 const AAMDNodes &AAInfo, const MDNode *Ranges) {
10465 assert(Chain.getValueType() == MVT::Other &&
10466 "Invalid chain type");
10467
10468 MMOFlags |= MachineMemOperand::MOLoad;
10469 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10470 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10471 // clients.
10472 if (PtrInfo.V.isNull())
10473 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10474
10475 TypeSize Size = MemVT.getStoreSize();
10477 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10478 Alignment, AAInfo, Ranges);
10479 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
10480}
10481
10483 EVT VT, const SDLoc &dl, SDValue Chain,
10484 SDValue Ptr, SDValue Offset, EVT MemVT,
10485 MachineMemOperand *MMO) {
10486 if (VT == MemVT) {
10487 ExtType = ISD::NON_EXTLOAD;
10488 } else if (ExtType == ISD::NON_EXTLOAD) {
10489 assert(VT == MemVT && "Non-extending load from different memory type!");
10490 } else {
10491 // Extending load.
10492 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
10493 "Should only be an extending load, not truncating!");
10494 assert(VT.isInteger() == MemVT.isInteger() &&
10495 "Cannot convert from FP to Int or Int -> FP!");
10496 assert(VT.isVector() == MemVT.isVector() &&
10497 "Cannot use an ext load to convert to or from a vector!");
10498 assert((!VT.isVector() ||
10500 "Cannot use an ext load to change the number of vector elements!");
10501 }
10502
10503 assert((!MMO->getRanges() ||
10505 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
10506 MemVT.isInteger())) &&
10507 "Range metadata and load type must match!");
10508
10509 bool Indexed = AM != ISD::UNINDEXED;
10510 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10511
10512 SDVTList VTs = Indexed ?
10513 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
10514 SDValue Ops[] = { Chain, Ptr, Offset };
10516 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
10517 ID.AddInteger(MemVT.getRawBits());
10518 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
10519 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
10520 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10521 ID.AddInteger(MMO->getFlags());
10522 void *IP = nullptr;
10523 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10524 E->refineAlignment(MMO);
10525 E->refineRanges(MMO);
10526 return SDValue(E, 0);
10527 }
10528 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10529 ExtType, MemVT, MMO);
10530 createOperands(N, Ops);
10531
10532 CSEMap.InsertNode(N, IP);
10533 InsertNode(N);
10534 SDValue V(N, 0);
10535 NewSDValueDbgMsg(V, "Creating new node: ", this);
10536 return V;
10537}
10538
10540 SDValue Ptr, MachinePointerInfo PtrInfo,
10541 MaybeAlign Alignment,
10542 MachineMemOperand::Flags MMOFlags,
10543 const AAMDNodes &AAInfo, const MDNode *Ranges) {
10545 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10546 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
10547}
10548
10550 SDValue Ptr, MachineMemOperand *MMO) {
10552 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10553 VT, MMO);
10554}
10555
10557 EVT VT, SDValue Chain, SDValue Ptr,
10558 MachinePointerInfo PtrInfo, EVT MemVT,
10559 MaybeAlign Alignment,
10560 MachineMemOperand::Flags MMOFlags,
10561 const AAMDNodes &AAInfo) {
10563 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
10564 MemVT, Alignment, MMOFlags, AAInfo);
10565}
10566
10568 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
10569 MachineMemOperand *MMO) {
10571 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
10572 MemVT, MMO);
10573}
10574
10578 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
10579 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10580 // Don't propagate the invariant or dereferenceable flags.
10581 auto MMOFlags =
10582 LD->getMemOperand()->getFlags() &
10584 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10585 LD->getChain(), Base, Offset, LD->getPointerInfo(),
10586 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
10587}
10588
10590 SDValue Ptr, MachinePointerInfo PtrInfo,
10591 Align Alignment,
10592 MachineMemOperand::Flags MMOFlags,
10593 const AAMDNodes &AAInfo) {
10594 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10595
10596 MMOFlags |= MachineMemOperand::MOStore;
10597 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10598
10599 if (PtrInfo.V.isNull())
10600 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10601
10604 MachineMemOperand *MMO =
10605 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
10606 return getStore(Chain, dl, Val, Ptr, MMO);
10607}
10608
10610 SDValue Ptr, MachineMemOperand *MMO) {
10612 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
10614}
10615
10617 SDValue Ptr, SDValue Offset, EVT SVT,
10619 bool IsTruncating) {
10620 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10621 EVT VT = Val.getValueType();
10622 if (VT == SVT) {
10623 IsTruncating = false;
10624 } else if (!IsTruncating) {
10625 assert(VT == SVT && "No-truncating store from different memory type!");
10626 } else {
10628 "Should only be a truncating store, not extending!");
10629 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10630 assert(VT.isVector() == SVT.isVector() &&
10631 "Cannot use trunc store to convert to or from a vector!");
10632 assert((!VT.isVector() ||
10634 "Cannot use trunc store to change the number of vector elements!");
10635 }
10636
10637 bool Indexed = AM != ISD::UNINDEXED;
10638 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
10639 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10640 : getVTList(MVT::Other);
10641 SDValue Ops[] = {Chain, Val, Ptr, Offset};
10644 ID.AddInteger(SVT.getRawBits());
10645 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
10646 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
10647 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10648 ID.AddInteger(MMO->getFlags());
10649 void *IP = nullptr;
10650 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10651 cast<StoreSDNode>(E)->refineAlignment(MMO);
10652 return SDValue(E, 0);
10653 }
10654 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10655 IsTruncating, SVT, MMO);
10656 createOperands(N, Ops);
10657
10658 CSEMap.InsertNode(N, IP);
10659 InsertNode(N);
10660 SDValue V(N, 0);
10661 NewSDValueDbgMsg(V, "Creating new node: ", this);
10662 return V;
10663}
10664
10666 SDValue Ptr, MachinePointerInfo PtrInfo,
10667 EVT SVT, Align Alignment,
10668 MachineMemOperand::Flags MMOFlags,
10669 const AAMDNodes &AAInfo) {
10670 assert(Chain.getValueType() == MVT::Other &&
10671 "Invalid chain type");
10672
10673 MMOFlags |= MachineMemOperand::MOStore;
10674 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10675
10676 if (PtrInfo.V.isNull())
10677 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10678
10680 MachineMemOperand *MMO = MF.getMachineMemOperand(
10681 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10682 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
10683}
10684
10686 SDValue Ptr, EVT SVT,
10687 MachineMemOperand *MMO) {
10689 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
10690}
10691
10695 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
10696 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
10697 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10698 ST->getMemoryVT(), ST->getMemOperand(), AM,
10699 ST->isTruncatingStore());
10700}
10701
10703 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
10704 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
10705 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
10706 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
10707 const MDNode *Ranges, bool IsExpanding) {
10708 MMOFlags |= MachineMemOperand::MOLoad;
10709 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10710 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10711 // clients.
10712 if (PtrInfo.V.isNull())
10713 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10714
10715 TypeSize Size = MemVT.getStoreSize();
10717 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10718 Alignment, AAInfo, Ranges);
10719 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10720 MMO, IsExpanding);
10721}
10722
10724 ISD::LoadExtType ExtType, EVT VT,
10725 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10726 SDValue Offset, SDValue Mask, SDValue EVL,
10727 EVT MemVT, MachineMemOperand *MMO,
10728 bool IsExpanding) {
10729 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10730 assert(Mask.getValueType().getVectorElementCount() ==
10731 VT.getVectorElementCount() &&
10732 "Vector width mismatch between mask and data");
10733
10734 bool Indexed = AM != ISD::UNINDEXED;
10735 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10736
10737 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10738 : getVTList(VT, MVT::Other);
10739 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10741 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10742 ID.AddInteger(MemVT.getRawBits());
10743 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10744 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10745 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10746 ID.AddInteger(MMO->getFlags());
10747 void *IP = nullptr;
10748 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10749 E->refineAlignment(MMO);
10750 E->refineRanges(MMO);
10751 return SDValue(E, 0);
10752 }
10753 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10754 ExtType, IsExpanding, MemVT, MMO);
10755 createOperands(N, Ops);
10756
10757 CSEMap.InsertNode(N, IP);
10758 InsertNode(N);
10759 SDValue V(N, 0);
10760 NewSDValueDbgMsg(V, "Creating new node: ", this);
10761 return V;
10762}
10763
10765 SDValue Ptr, SDValue Mask, SDValue EVL,
10766 MachinePointerInfo PtrInfo,
10767 MaybeAlign Alignment,
10768 MachineMemOperand::Flags MMOFlags,
10769 const AAMDNodes &AAInfo, const MDNode *Ranges,
10770 bool IsExpanding) {
10772 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10773 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10774 IsExpanding);
10775}
10776
10778 SDValue Ptr, SDValue Mask, SDValue EVL,
10779 MachineMemOperand *MMO, bool IsExpanding) {
10781 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10782 Mask, EVL, VT, MMO, IsExpanding);
10783}
10784
10786 EVT VT, SDValue Chain, SDValue Ptr,
10787 SDValue Mask, SDValue EVL,
10788 MachinePointerInfo PtrInfo, EVT MemVT,
10789 MaybeAlign Alignment,
10790 MachineMemOperand::Flags MMOFlags,
10791 const AAMDNodes &AAInfo, bool IsExpanding) {
10793 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10794 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10795 IsExpanding);
10796}
10797
10799 EVT VT, SDValue Chain, SDValue Ptr,
10800 SDValue Mask, SDValue EVL, EVT MemVT,
10801 MachineMemOperand *MMO, bool IsExpanding) {
10803 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10804 EVL, MemVT, MMO, IsExpanding);
10805}
10806
10810 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10811 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10812 // Don't propagate the invariant or dereferenceable flags.
10813 auto MMOFlags =
10814 LD->getMemOperand()->getFlags() &
10816 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10817 LD->getChain(), Base, Offset, LD->getMask(),
10818 LD->getVectorLength(), LD->getPointerInfo(),
10819 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10820 nullptr, LD->isExpandingLoad());
10821}
10822
10824 SDValue Ptr, SDValue Offset, SDValue Mask,
10825 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10826 ISD::MemIndexedMode AM, bool IsTruncating,
10827 bool IsCompressing) {
10828 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10829 assert(Mask.getValueType().getVectorElementCount() ==
10831 "Vector width mismatch between mask and data");
10832
10833 bool Indexed = AM != ISD::UNINDEXED;
10834 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10835 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10836 : getVTList(MVT::Other);
10837 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10839 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10840 ID.AddInteger(MemVT.getRawBits());
10841 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10842 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10843 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10844 ID.AddInteger(MMO->getFlags());
10845 void *IP = nullptr;
10846 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10847 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10848 return SDValue(E, 0);
10849 }
10850 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10851 IsTruncating, IsCompressing, MemVT, MMO);
10852 createOperands(N, Ops);
10853
10854 CSEMap.InsertNode(N, IP);
10855 InsertNode(N);
10856 SDValue V(N, 0);
10857 NewSDValueDbgMsg(V, "Creating new node: ", this);
10858 return V;
10859}
10860
10862 SDValue Val, SDValue Ptr, SDValue Mask,
10863 SDValue EVL, MachinePointerInfo PtrInfo,
10864 EVT SVT, Align Alignment,
10865 MachineMemOperand::Flags MMOFlags,
10866 const AAMDNodes &AAInfo,
10867 bool IsCompressing) {
10868 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10869
10870 MMOFlags |= MachineMemOperand::MOStore;
10871 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10872
10873 if (PtrInfo.V.isNull())
10874 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10875
10877 MachineMemOperand *MMO = MF.getMachineMemOperand(
10878 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10879 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10880 IsCompressing);
10881}
10882
10884 SDValue Val, SDValue Ptr, SDValue Mask,
10885 SDValue EVL, EVT SVT,
10886 MachineMemOperand *MMO,
10887 bool IsCompressing) {
10888 EVT VT = Val.getValueType();
10889
10890 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10891 if (VT == SVT)
10892 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10893 EVL, VT, MMO, ISD::UNINDEXED,
10894 /*IsTruncating*/ false, IsCompressing);
10895
10897 "Should only be a truncating store, not extending!");
10898 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10899 assert(VT.isVector() == SVT.isVector() &&
10900 "Cannot use trunc store to convert to or from a vector!");
10901 assert((!VT.isVector() ||
10903 "Cannot use trunc store to change the number of vector elements!");
10904
10905 SDVTList VTs = getVTList(MVT::Other);
10907 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10909 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10910 ID.AddInteger(SVT.getRawBits());
10911 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10912 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10913 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10914 ID.AddInteger(MMO->getFlags());
10915 void *IP = nullptr;
10916 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10917 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10918 return SDValue(E, 0);
10919 }
10920 auto *N =
10921 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10922 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10923 createOperands(N, Ops);
10924
10925 CSEMap.InsertNode(N, IP);
10926 InsertNode(N);
10927 SDValue V(N, 0);
10928 NewSDValueDbgMsg(V, "Creating new node: ", this);
10929 return V;
10930}
10931
10935 auto *ST = cast<VPStoreSDNode>(OrigStore);
10936 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10937 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10938 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10939 Offset, ST->getMask(), ST->getVectorLength()};
10941 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10942 ID.AddInteger(ST->getMemoryVT().getRawBits());
10943 ID.AddInteger(ST->getRawSubclassData());
10944 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10945 ID.AddInteger(ST->getMemOperand()->getFlags());
10946 void *IP = nullptr;
10947 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10948 return SDValue(E, 0);
10949
10950 auto *N = newSDNode<VPStoreSDNode>(
10951 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10952 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10953 createOperands(N, Ops);
10954
10955 CSEMap.InsertNode(N, IP);
10956 InsertNode(N);
10957 SDValue V(N, 0);
10958 NewSDValueDbgMsg(V, "Creating new node: ", this);
10959 return V;
10960}
10961
10963 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10964 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10965 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10966 bool Indexed = AM != ISD::UNINDEXED;
10967 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10968
10969 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10970 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10971 : getVTList(VT, MVT::Other);
10973 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10974 ID.AddInteger(VT.getRawBits());
10975 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10976 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10977 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10978
10979 void *IP = nullptr;
10980 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10981 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10982 return SDValue(E, 0);
10983 }
10984
10985 auto *N =
10986 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10987 ExtType, IsExpanding, MemVT, MMO);
10988 createOperands(N, Ops);
10989 CSEMap.InsertNode(N, IP);
10990 InsertNode(N);
10991 SDValue V(N, 0);
10992 NewSDValueDbgMsg(V, "Creating new node: ", this);
10993 return V;
10994}
10995
10997 SDValue Ptr, SDValue Stride,
10998 SDValue Mask, SDValue EVL,
10999 MachineMemOperand *MMO,
11000 bool IsExpanding) {
11002 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
11003 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
11004}
11005
11007 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
11008 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
11009 MachineMemOperand *MMO, bool IsExpanding) {
11011 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
11012 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
11013}
11014
11016 SDValue Val, SDValue Ptr,
11017 SDValue Offset, SDValue Stride,
11018 SDValue Mask, SDValue EVL, EVT MemVT,
11019 MachineMemOperand *MMO,
11021 bool IsTruncating, bool IsCompressing) {
11022 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
11023 bool Indexed = AM != ISD::UNINDEXED;
11024 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
11025 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
11026 : getVTList(MVT::Other);
11027 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
11029 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
11030 ID.AddInteger(MemVT.getRawBits());
11031 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
11032 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
11033 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11034 void *IP = nullptr;
11035 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11036 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
11037 return SDValue(E, 0);
11038 }
11039 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
11040 VTs, AM, IsTruncating,
11041 IsCompressing, MemVT, MMO);
11042 createOperands(N, Ops);
11043
11044 CSEMap.InsertNode(N, IP);
11045 InsertNode(N);
11046 SDValue V(N, 0);
11047 NewSDValueDbgMsg(V, "Creating new node: ", this);
11048 return V;
11049}
11050
11052 SDValue Val, SDValue Ptr,
11053 SDValue Stride, SDValue Mask,
11054 SDValue EVL, EVT SVT,
11055 MachineMemOperand *MMO,
11056 bool IsCompressing) {
11057 EVT VT = Val.getValueType();
11058
11059 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
11060 if (VT == SVT)
11061 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
11062 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
11063 /*IsTruncating*/ false, IsCompressing);
11064
11066 "Should only be a truncating store, not extending!");
11067 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
11068 assert(VT.isVector() == SVT.isVector() &&
11069 "Cannot use trunc store to convert to or from a vector!");
11070 assert((!VT.isVector() ||
11072 "Cannot use trunc store to change the number of vector elements!");
11073
11074 SDVTList VTs = getVTList(MVT::Other);
11076 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
11078 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
11079 ID.AddInteger(SVT.getRawBits());
11080 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
11081 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
11082 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11083 void *IP = nullptr;
11084 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11085 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
11086 return SDValue(E, 0);
11087 }
11088 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
11089 VTs, ISD::UNINDEXED, true,
11090 IsCompressing, SVT, MMO);
11091 createOperands(N, Ops);
11092
11093 CSEMap.InsertNode(N, IP);
11094 InsertNode(N);
11095 SDValue V(N, 0);
11096 NewSDValueDbgMsg(V, "Creating new node: ", this);
11097 return V;
11098}
11099
11102 ISD::MemIndexType IndexType) {
11103 assert(Ops.size() == 6 && "Incompatible number of operands");
11104
11106 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
11107 ID.AddInteger(VT.getRawBits());
11108 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
11109 dl.getIROrder(), VTs, VT, MMO, IndexType));
11110 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11111 ID.AddInteger(MMO->getFlags());
11112 void *IP = nullptr;
11113 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11114 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
11115 return SDValue(E, 0);
11116 }
11117
11118 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
11119 VT, MMO, IndexType);
11120 createOperands(N, Ops);
11121
11122 assert(N->getMask().getValueType().getVectorElementCount() ==
11123 N->getValueType(0).getVectorElementCount() &&
11124 "Vector width mismatch between mask and data");
11125 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11126 N->getValueType(0).getVectorElementCount().isScalable() &&
11127 "Scalable flags of index and data do not match");
11129 N->getIndex().getValueType().getVectorElementCount(),
11130 N->getValueType(0).getVectorElementCount()) &&
11131 "Vector width mismatch between index and data");
11132 assert(isa<ConstantSDNode>(N->getScale()) &&
11133 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11134 "Scale should be a constant power of 2");
11135
11136 CSEMap.InsertNode(N, IP);
11137 InsertNode(N);
11138 SDValue V(N, 0);
11139 NewSDValueDbgMsg(V, "Creating new node: ", this);
11140 return V;
11141}
11142
11145 MachineMemOperand *MMO,
11146 ISD::MemIndexType IndexType) {
11147 assert(Ops.size() == 7 && "Incompatible number of operands");
11148
11150 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
11151 ID.AddInteger(VT.getRawBits());
11152 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
11153 dl.getIROrder(), VTs, VT, MMO, IndexType));
11154 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11155 ID.AddInteger(MMO->getFlags());
11156 void *IP = nullptr;
11157 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11158 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
11159 return SDValue(E, 0);
11160 }
11161 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
11162 VT, MMO, IndexType);
11163 createOperands(N, Ops);
11164
11165 assert(N->getMask().getValueType().getVectorElementCount() ==
11166 N->getValue().getValueType().getVectorElementCount() &&
11167 "Vector width mismatch between mask and data");
11168 assert(
11169 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11170 N->getValue().getValueType().getVectorElementCount().isScalable() &&
11171 "Scalable flags of index and data do not match");
11173 N->getIndex().getValueType().getVectorElementCount(),
11174 N->getValue().getValueType().getVectorElementCount()) &&
11175 "Vector width mismatch between index and data");
11176 assert(isa<ConstantSDNode>(N->getScale()) &&
11177 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11178 "Scale should be a constant power of 2");
11179
11180 CSEMap.InsertNode(N, IP);
11181 InsertNode(N);
11182 SDValue V(N, 0);
11183 NewSDValueDbgMsg(V, "Creating new node: ", this);
11184 return V;
11185}
11186
11189 SDValue PassThru, EVT MemVT,
11190 MachineMemOperand *MMO,
11192 ISD::LoadExtType ExtTy, bool isExpanding) {
11193 bool Indexed = AM != ISD::UNINDEXED;
11194 assert((Indexed || Offset.isUndef()) &&
11195 "Unindexed masked load with an offset!");
11196 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
11197 : getVTList(VT, MVT::Other);
11198 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
11201 ID.AddInteger(MemVT.getRawBits());
11202 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
11203 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
11204 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11205 ID.AddInteger(MMO->getFlags());
11206 void *IP = nullptr;
11207 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11208 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
11209 return SDValue(E, 0);
11210 }
11211 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
11212 AM, ExtTy, isExpanding, MemVT, MMO);
11213 createOperands(N, Ops);
11214
11215 CSEMap.InsertNode(N, IP);
11216 InsertNode(N);
11217 SDValue V(N, 0);
11218 NewSDValueDbgMsg(V, "Creating new node: ", this);
11219 return V;
11220}
11221
11226 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
11227 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
11228 Offset, LD->getMask(), LD->getPassThru(),
11229 LD->getMemoryVT(), LD->getMemOperand(), AM,
11230 LD->getExtensionType(), LD->isExpandingLoad());
11231}
11232
11235 SDValue Mask, EVT MemVT,
11236 MachineMemOperand *MMO,
11237 ISD::MemIndexedMode AM, bool IsTruncating,
11238 bool IsCompressing) {
11239 assert(Chain.getValueType() == MVT::Other &&
11240 "Invalid chain type");
11241 bool Indexed = AM != ISD::UNINDEXED;
11242 assert((Indexed || Offset.isUndef()) &&
11243 "Unindexed masked store with an offset!");
11244 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
11245 : getVTList(MVT::Other);
11246 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
11249 ID.AddInteger(MemVT.getRawBits());
11250 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
11251 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
11252 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11253 ID.AddInteger(MMO->getFlags());
11254 void *IP = nullptr;
11255 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11256 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
11257 return SDValue(E, 0);
11258 }
11259 auto *N =
11260 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
11261 IsTruncating, IsCompressing, MemVT, MMO);
11262 createOperands(N, Ops);
11263
11264 CSEMap.InsertNode(N, IP);
11265 InsertNode(N);
11266 SDValue V(N, 0);
11267 NewSDValueDbgMsg(V, "Creating new node: ", this);
11268 return V;
11269}
11270
11275 assert(ST->getOffset().isUndef() &&
11276 "Masked store is already a indexed store!");
11277 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
11278 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
11279 AM, ST->isTruncatingStore(), ST->isCompressingStore());
11280}
11281
11284 MachineMemOperand *MMO,
11285 ISD::MemIndexType IndexType,
11286 ISD::LoadExtType ExtTy) {
11287 assert(Ops.size() == 6 && "Incompatible number of operands");
11288
11291 ID.AddInteger(MemVT.getRawBits());
11292 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
11293 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
11294 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11295 ID.AddInteger(MMO->getFlags());
11296 void *IP = nullptr;
11297 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11298 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
11299 return SDValue(E, 0);
11300 }
11301
11302 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
11303 VTs, MemVT, MMO, IndexType, ExtTy);
11304 createOperands(N, Ops);
11305
11306 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
11307 "Incompatible type of the PassThru value in MaskedGatherSDNode");
11308 assert(N->getMask().getValueType().getVectorElementCount() ==
11309 N->getValueType(0).getVectorElementCount() &&
11310 "Vector width mismatch between mask and data");
11311 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11312 N->getValueType(0).getVectorElementCount().isScalable() &&
11313 "Scalable flags of index and data do not match");
11315 N->getIndex().getValueType().getVectorElementCount(),
11316 N->getValueType(0).getVectorElementCount()) &&
11317 "Vector width mismatch between index and data");
11318 assert(isa<ConstantSDNode>(N->getScale()) &&
11319 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11320 "Scale should be a constant power of 2");
11321
11322 CSEMap.InsertNode(N, IP);
11323 InsertNode(N);
11324 SDValue V(N, 0);
11325 NewSDValueDbgMsg(V, "Creating new node: ", this);
11326 return V;
11327}
11328
11331 MachineMemOperand *MMO,
11332 ISD::MemIndexType IndexType,
11333 bool IsTrunc) {
11334 assert(Ops.size() == 6 && "Incompatible number of operands");
11335
11338 ID.AddInteger(MemVT.getRawBits());
11339 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
11340 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
11341 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11342 ID.AddInteger(MMO->getFlags());
11343 void *IP = nullptr;
11344 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11345 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
11346 return SDValue(E, 0);
11347 }
11348
11349 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
11350 VTs, MemVT, MMO, IndexType, IsTrunc);
11351 createOperands(N, Ops);
11352
11353 assert(N->getMask().getValueType().getVectorElementCount() ==
11354 N->getValue().getValueType().getVectorElementCount() &&
11355 "Vector width mismatch between mask and data");
11356 assert(
11357 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11358 N->getValue().getValueType().getVectorElementCount().isScalable() &&
11359 "Scalable flags of index and data do not match");
11361 N->getIndex().getValueType().getVectorElementCount(),
11362 N->getValue().getValueType().getVectorElementCount()) &&
11363 "Vector width mismatch between index and data");
11364 assert(isa<ConstantSDNode>(N->getScale()) &&
11365 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11366 "Scale should be a constant power of 2");
11367
11368 CSEMap.InsertNode(N, IP);
11369 InsertNode(N);
11370 SDValue V(N, 0);
11371 NewSDValueDbgMsg(V, "Creating new node: ", this);
11372 return V;
11373}
11374
11376 const SDLoc &dl, ArrayRef<SDValue> Ops,
11377 MachineMemOperand *MMO,
11378 ISD::MemIndexType IndexType) {
11379 assert(Ops.size() == 7 && "Incompatible number of operands");
11380
11383 ID.AddInteger(MemVT.getRawBits());
11384 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
11385 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
11386 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11387 ID.AddInteger(MMO->getFlags());
11388 void *IP = nullptr;
11389 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11390 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
11391 return SDValue(E, 0);
11392 }
11393
11394 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
11395 VTs, MemVT, MMO, IndexType);
11396 createOperands(N, Ops);
11397
11398 assert(N->getMask().getValueType().getVectorElementCount() ==
11399 N->getIndex().getValueType().getVectorElementCount() &&
11400 "Vector width mismatch between mask and data");
11401 assert(isa<ConstantSDNode>(N->getScale()) &&
11402 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11403 "Scale should be a constant power of 2");
11404 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
11405
11406 CSEMap.InsertNode(N, IP);
11407 InsertNode(N);
11408 SDValue V(N, 0);
11409 NewSDValueDbgMsg(V, "Creating new node: ", this);
11410 return V;
11411}
11412
11414 SDValue Ptr, SDValue Mask, SDValue EVL,
11415 MachineMemOperand *MMO) {
11416 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
11417 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
11419 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
11420 ID.AddInteger(VT.getRawBits());
11421 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
11422 VTs, VT, MMO));
11423 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11424 ID.AddInteger(MMO->getFlags());
11425 void *IP = nullptr;
11426 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11427 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
11428 return SDValue(E, 0);
11429 }
11430 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
11431 VT, MMO);
11432 createOperands(N, Ops);
11433
11434 CSEMap.InsertNode(N, IP);
11435 InsertNode(N);
11436 SDValue V(N, 0);
11437 NewSDValueDbgMsg(V, "Creating new node: ", this);
11438 return V;
11439}
11440
11442 EVT MemVT, MachineMemOperand *MMO) {
11443 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
11444 SDVTList VTs = getVTList(MVT::Other);
11445 SDValue Ops[] = {Chain, Ptr};
11448 ID.AddInteger(MemVT.getRawBits());
11449 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
11450 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
11451 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11452 ID.AddInteger(MMO->getFlags());
11453 void *IP = nullptr;
11454 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
11455 return SDValue(E, 0);
11456
11457 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
11458 dl.getDebugLoc(), VTs, MemVT, MMO);
11459 createOperands(N, Ops);
11460
11461 CSEMap.InsertNode(N, IP);
11462 InsertNode(N);
11463 SDValue V(N, 0);
11464 NewSDValueDbgMsg(V, "Creating new node: ", this);
11465 return V;
11466}
11467
11469 EVT MemVT, MachineMemOperand *MMO) {
11470 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
11471 SDVTList VTs = getVTList(MVT::Other);
11472 SDValue Ops[] = {Chain, Ptr};
11475 ID.AddInteger(MemVT.getRawBits());
11476 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
11477 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
11478 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11479 ID.AddInteger(MMO->getFlags());
11480 void *IP = nullptr;
11481 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
11482 return SDValue(E, 0);
11483
11484 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
11485 dl.getDebugLoc(), VTs, MemVT, MMO);
11486 createOperands(N, Ops);
11487
11488 CSEMap.InsertNode(N, IP);
11489 InsertNode(N);
11490 SDValue V(N, 0);
11491 NewSDValueDbgMsg(V, "Creating new node: ", this);
11492 return V;
11493}
11494
11496 // select undef, T, F --> T (if T is a constant), otherwise F
11497 // select, ?, undef, F --> F
11498 // select, ?, T, undef --> T
11499 if (Cond.isUndef())
11500 return isConstantValueOfAnyType(T) ? T : F;
11501 if (T.isUndef())
11503 if (F.isUndef())
11505
11506 // select true, T, F --> T
11507 // select false, T, F --> F
11508 if (auto C = isBoolConstant(Cond))
11509 return *C ? T : F;
11510
11511 // select ?, T, T --> T
11512 if (T == F)
11513 return T;
11514
11515 return SDValue();
11516}
11517
11519 // shift undef, Y --> 0 (can always assume that the undef value is 0)
11520 if (X.isUndef())
11521 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
11522 // shift X, undef --> undef (because it may shift by the bitwidth)
11523 if (Y.isUndef())
11524 return getUNDEF(X.getValueType());
11525
11526 // shift 0, Y --> 0
11527 // shift X, 0 --> X
11529 return X;
11530
11531 // shift X, C >= bitwidth(X) --> undef
11532 // All vector elements must be too big (or undef) to avoid partial undefs.
11533 auto isShiftTooBig = [X](ConstantSDNode *Val) {
11534 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
11535 };
11536 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
11537 return getUNDEF(X.getValueType());
11538
11539 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
11540 if (X.getValueType().getScalarType() == MVT::i1)
11541 return X;
11542
11543 return SDValue();
11544}
11545
11547 SDNodeFlags Flags) {
11548 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
11549 // (an undef operand can be chosen to be Nan/Inf), then the result of this
11550 // operation is poison. That result can be relaxed to undef.
11551 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
11552 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
11553 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
11554 (YC && YC->getValueAPF().isNaN());
11555 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
11556 (YC && YC->getValueAPF().isInfinity());
11557
11558 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
11559 return getUNDEF(X.getValueType());
11560
11561 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
11562 return getUNDEF(X.getValueType());
11563
11564 if (!YC)
11565 return SDValue();
11566
11567 // X + -0.0 --> X
11568 if (Opcode == ISD::FADD)
11569 if (YC->getValueAPF().isNegZero())
11570 return X;
11571
11572 // X - +0.0 --> X
11573 if (Opcode == ISD::FSUB)
11574 if (YC->getValueAPF().isPosZero())
11575 return X;
11576
11577 // X * 1.0 --> X
11578 // X / 1.0 --> X
11579 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
11580 if (YC->getValueAPF().isExactlyValue(1.0))
11581 return X;
11582
11583 // X * 0.0 --> 0.0
11584 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
11585 if (YC->getValueAPF().isZero())
11586 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
11587
11588 return SDValue();
11589}
11590
11592 SDValue Ptr, SDValue SV, unsigned Align) {
11593 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
11594 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
11595}
11596
11597SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11599 switch (Ops.size()) {
11600 case 0: return getNode(Opcode, DL, VT);
11601 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
11602 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
11603 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
11604 default: break;
11605 }
11606
11607 // Copy from an SDUse array into an SDValue array for use with
11608 // the regular getNode logic.
11610 return getNode(Opcode, DL, VT, NewOps);
11611}
11612
11613SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11615 SDNodeFlags Flags;
11616 if (Inserter)
11617 Flags = Inserter->getFlags();
11618 return getNode(Opcode, DL, VT, Ops, Flags);
11619}
11620
11621SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11622 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11623 unsigned NumOps = Ops.size();
11624 switch (NumOps) {
11625 case 0: return getNode(Opcode, DL, VT);
11626 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
11627 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
11628 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
11629 default: break;
11630 }
11631
11632#ifndef NDEBUG
11633 for (const auto &Op : Ops)
11634 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11635 "Operand is DELETED_NODE!");
11636#endif
11637
11638 switch (Opcode) {
11639 default: break;
11640 case ISD::BUILD_VECTOR:
11641 // Attempt to simplify BUILD_VECTOR.
11642 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
11643 return V;
11644 break;
11646 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
11647 return V;
11648 break;
11649 case ISD::SELECT_CC:
11650 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
11651 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
11652 "LHS and RHS of condition must have same type!");
11653 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11654 "True and False arms of SelectCC must have same type!");
11655 assert(Ops[2].getValueType() == VT &&
11656 "select_cc node must be of same type as true and false value!");
11657 assert((!Ops[0].getValueType().isVector() ||
11658 Ops[0].getValueType().getVectorElementCount() ==
11659 VT.getVectorElementCount()) &&
11660 "Expected select_cc with vector result to have the same sized "
11661 "comparison type!");
11662 break;
11663 case ISD::BR_CC:
11664 assert(NumOps == 5 && "BR_CC takes 5 operands!");
11665 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11666 "LHS/RHS of comparison should match types!");
11667 break;
11668 case ISD::VP_ADD:
11669 case ISD::VP_SUB:
11670 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
11671 if (VT.getScalarType() == MVT::i1)
11672 Opcode = ISD::VP_XOR;
11673 break;
11674 case ISD::VP_MUL:
11675 // If it is VP_MUL mask operation then turn it to VP_AND
11676 if (VT.getScalarType() == MVT::i1)
11677 Opcode = ISD::VP_AND;
11678 break;
11679 case ISD::VP_REDUCE_MUL:
11680 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
11681 if (VT == MVT::i1)
11682 Opcode = ISD::VP_REDUCE_AND;
11683 break;
11684 case ISD::VP_REDUCE_ADD:
11685 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
11686 if (VT == MVT::i1)
11687 Opcode = ISD::VP_REDUCE_XOR;
11688 break;
11689 case ISD::VP_REDUCE_SMAX:
11690 case ISD::VP_REDUCE_UMIN:
11691 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
11692 // VP_REDUCE_AND.
11693 if (VT == MVT::i1)
11694 Opcode = ISD::VP_REDUCE_AND;
11695 break;
11696 case ISD::VP_REDUCE_SMIN:
11697 case ISD::VP_REDUCE_UMAX:
11698 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
11699 // VP_REDUCE_OR.
11700 if (VT == MVT::i1)
11701 Opcode = ISD::VP_REDUCE_OR;
11702 break;
11703 }
11704
11705 // Memoize nodes.
11706 SDNode *N;
11707 SDVTList VTs = getVTList(VT);
11708
11709 if (VT != MVT::Glue) {
11711 AddNodeIDNode(ID, Opcode, VTs, Ops);
11712 void *IP = nullptr;
11713
11714 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11715 E->intersectFlagsWith(Flags);
11716 return SDValue(E, 0);
11717 }
11718
11719 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11720 createOperands(N, Ops);
11721
11722 CSEMap.InsertNode(N, IP);
11723 } else {
11724 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11725 createOperands(N, Ops);
11726 }
11727
11728 N->setFlags(Flags);
11729 InsertNode(N);
11730 SDValue V(N, 0);
11731 NewSDValueDbgMsg(V, "Creating new node: ", this);
11732 return V;
11733}
11734
11735SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11736 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11737 SDNodeFlags Flags;
11738 if (Inserter)
11739 Flags = Inserter->getFlags();
11740 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11741}
11742
11743SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11745 const SDNodeFlags Flags) {
11746 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11747}
11748
11749SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11751 SDNodeFlags Flags;
11752 if (Inserter)
11753 Flags = Inserter->getFlags();
11754 return getNode(Opcode, DL, VTList, Ops, Flags);
11755}
11756
11757SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11758 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11759 if (VTList.NumVTs == 1)
11760 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11761
11762#ifndef NDEBUG
11763 for (const auto &Op : Ops)
11764 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11765 "Operand is DELETED_NODE!");
11766#endif
11767
11768 switch (Opcode) {
11769 case ISD::SADDO:
11770 case ISD::UADDO:
11771 case ISD::SSUBO:
11772 case ISD::USUBO: {
11773 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11774 "Invalid add/sub overflow op!");
11775 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11776 Ops[0].getValueType() == Ops[1].getValueType() &&
11777 Ops[0].getValueType() == VTList.VTs[0] &&
11778 "Binary operator types must match!");
11779 SDValue N1 = Ops[0], N2 = Ops[1];
11780 canonicalizeCommutativeBinop(Opcode, N1, N2);
11781
11782 // (X +- 0) -> X with zero-overflow.
11783 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11784 /*AllowTruncation*/ true);
11785 if (N2CV && N2CV->isZero()) {
11786 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11787 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11788 }
11789
11790 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11791 VTList.VTs[1].getScalarType() == MVT::i1) {
11792 SDValue F1 = getFreeze(N1);
11793 SDValue F2 = getFreeze(N2);
11794 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11795 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11796 return getNode(ISD::MERGE_VALUES, DL, VTList,
11797 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11798 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11799 Flags);
11800 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11801 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11802 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11803 return getNode(ISD::MERGE_VALUES, DL, VTList,
11804 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11805 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11806 Flags);
11807 }
11808 }
11809 break;
11810 }
11811 case ISD::SADDO_CARRY:
11812 case ISD::UADDO_CARRY:
11813 case ISD::SSUBO_CARRY:
11814 case ISD::USUBO_CARRY:
11815 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11816 "Invalid add/sub overflow op!");
11817 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11818 Ops[0].getValueType() == Ops[1].getValueType() &&
11819 Ops[0].getValueType() == VTList.VTs[0] &&
11820 Ops[2].getValueType() == VTList.VTs[1] &&
11821 "Binary operator types must match!");
11822 break;
11823 case ISD::SMUL_LOHI:
11824 case ISD::UMUL_LOHI: {
11825 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11826 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11827 VTList.VTs[0] == Ops[0].getValueType() &&
11828 VTList.VTs[0] == Ops[1].getValueType() &&
11829 "Binary operator types must match!");
11830 // Constant fold.
11833 if (LHS && RHS) {
11834 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11835 unsigned OutWidth = Width * 2;
11836 APInt Val = LHS->getAPIntValue();
11837 APInt Mul = RHS->getAPIntValue();
11838 if (Opcode == ISD::SMUL_LOHI) {
11839 Val = Val.sext(OutWidth);
11840 Mul = Mul.sext(OutWidth);
11841 } else {
11842 Val = Val.zext(OutWidth);
11843 Mul = Mul.zext(OutWidth);
11844 }
11845 Val *= Mul;
11846
11847 SDValue Hi =
11848 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11849 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11850 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11851 }
11852 break;
11853 }
11854 case ISD::FFREXP: {
11855 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11856 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11857 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11858
11860 int FrexpExp;
11861 APFloat FrexpMant =
11862 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11863 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11864 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11865 DL, VTList.VTs[1]);
11866 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11867 }
11868
11869 break;
11870 }
11872 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11873 "Invalid STRICT_FP_EXTEND!");
11874 assert(VTList.VTs[0].isFloatingPoint() &&
11875 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11876 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11877 "STRICT_FP_EXTEND result type should be vector iff the operand "
11878 "type is vector!");
11879 assert((!VTList.VTs[0].isVector() ||
11880 VTList.VTs[0].getVectorElementCount() ==
11881 Ops[1].getValueType().getVectorElementCount()) &&
11882 "Vector element count mismatch!");
11883 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11884 "Invalid fpext node, dst <= src!");
11885 break;
11887 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11888 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11889 "STRICT_FP_ROUND result type should be vector iff the operand "
11890 "type is vector!");
11891 assert((!VTList.VTs[0].isVector() ||
11892 VTList.VTs[0].getVectorElementCount() ==
11893 Ops[1].getValueType().getVectorElementCount()) &&
11894 "Vector element count mismatch!");
11895 assert(VTList.VTs[0].isFloatingPoint() &&
11896 Ops[1].getValueType().isFloatingPoint() &&
11897 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11898 Ops[2].getOpcode() == ISD::TargetConstant &&
11899 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11900 "Invalid STRICT_FP_ROUND!");
11901 break;
11902 }
11903
11904 // Memoize the node unless it returns a glue result.
11905 SDNode *N;
11906 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11908 AddNodeIDNode(ID, Opcode, VTList, Ops);
11909 void *IP = nullptr;
11910 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11911 E->intersectFlagsWith(Flags);
11912 return SDValue(E, 0);
11913 }
11914
11915 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11916 createOperands(N, Ops);
11917 CSEMap.InsertNode(N, IP);
11918 } else {
11919 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11920 createOperands(N, Ops);
11921 }
11922
11923 N->setFlags(Flags);
11924 InsertNode(N);
11925 SDValue V(N, 0);
11926 NewSDValueDbgMsg(V, "Creating new node: ", this);
11927 return V;
11928}
11929
11930SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11931 SDVTList VTList) {
11932 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11933}
11934
11935SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11936 SDValue N1) {
11937 SDValue Ops[] = { N1 };
11938 return getNode(Opcode, DL, VTList, Ops);
11939}
11940
11941SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11942 SDValue N1, SDValue N2) {
11943 SDValue Ops[] = { N1, N2 };
11944 return getNode(Opcode, DL, VTList, Ops);
11945}
11946
11947SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11948 SDValue N1, SDValue N2, SDValue N3) {
11949 SDValue Ops[] = { N1, N2, N3 };
11950 return getNode(Opcode, DL, VTList, Ops);
11951}
11952
11953SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11954 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11955 SDValue Ops[] = { N1, N2, N3, N4 };
11956 return getNode(Opcode, DL, VTList, Ops);
11957}
11958
11959SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11960 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11961 SDValue N5) {
11962 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11963 return getNode(Opcode, DL, VTList, Ops);
11964}
11965
11967 if (!VT.isExtended())
11968 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11969
11970 return makeVTList(&(*EVTs.insert(VT).first), 1);
11971}
11972
11975 ID.AddInteger(2U);
11976 ID.AddInteger(VT1.getRawBits());
11977 ID.AddInteger(VT2.getRawBits());
11978
11979 void *IP = nullptr;
11980 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11981 if (!Result) {
11982 EVT *Array = Allocator.Allocate<EVT>(2);
11983 Array[0] = VT1;
11984 Array[1] = VT2;
11985 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11986 VTListMap.InsertNode(Result, IP);
11987 }
11988 return Result->getSDVTList();
11989}
11990
11993 ID.AddInteger(3U);
11994 ID.AddInteger(VT1.getRawBits());
11995 ID.AddInteger(VT2.getRawBits());
11996 ID.AddInteger(VT3.getRawBits());
11997
11998 void *IP = nullptr;
11999 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
12000 if (!Result) {
12001 EVT *Array = Allocator.Allocate<EVT>(3);
12002 Array[0] = VT1;
12003 Array[1] = VT2;
12004 Array[2] = VT3;
12005 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
12006 VTListMap.InsertNode(Result, IP);
12007 }
12008 return Result->getSDVTList();
12009}
12010
12013 ID.AddInteger(4U);
12014 ID.AddInteger(VT1.getRawBits());
12015 ID.AddInteger(VT2.getRawBits());
12016 ID.AddInteger(VT3.getRawBits());
12017 ID.AddInteger(VT4.getRawBits());
12018
12019 void *IP = nullptr;
12020 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
12021 if (!Result) {
12022 EVT *Array = Allocator.Allocate<EVT>(4);
12023 Array[0] = VT1;
12024 Array[1] = VT2;
12025 Array[2] = VT3;
12026 Array[3] = VT4;
12027 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
12028 VTListMap.InsertNode(Result, IP);
12029 }
12030 return Result->getSDVTList();
12031}
12032
12034 unsigned NumVTs = VTs.size();
12036 ID.AddInteger(NumVTs);
12037 for (unsigned index = 0; index < NumVTs; index++) {
12038 ID.AddInteger(VTs[index].getRawBits());
12039 }
12040
12041 void *IP = nullptr;
12042 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
12043 if (!Result) {
12044 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
12045 llvm::copy(VTs, Array);
12046 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
12047 VTListMap.InsertNode(Result, IP);
12048 }
12049 return Result->getSDVTList();
12050}
12051
12052
12053/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
12054/// specified operands. If the resultant node already exists in the DAG,
12055/// this does not modify the specified node, instead it returns the node that
12056/// already exists. If the resultant node does not exist in the DAG, the
12057/// input node is returned. As a degenerate case, if you specify the same
12058/// input operands as the node already has, the input node is returned.
12060 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
12061
12062 // Check to see if there is no change.
12063 if (Op == N->getOperand(0)) return N;
12064
12065 // See if the modified node already exists.
12066 void *InsertPos = nullptr;
12067 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
12068 return Existing;
12069
12070 // Nope it doesn't. Remove the node from its current place in the maps.
12071 if (InsertPos)
12072 if (!RemoveNodeFromCSEMaps(N))
12073 InsertPos = nullptr;
12074
12075 // Now we update the operands.
12076 N->OperandList[0].set(Op);
12077
12079 // If this gets put into a CSE map, add it.
12080 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
12081 return N;
12082}
12083
12085 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
12086
12087 // Check to see if there is no change.
12088 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
12089 return N; // No operands changed, just return the input node.
12090
12091 // See if the modified node already exists.
12092 void *InsertPos = nullptr;
12093 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
12094 return Existing;
12095
12096 // Nope it doesn't. Remove the node from its current place in the maps.
12097 if (InsertPos)
12098 if (!RemoveNodeFromCSEMaps(N))
12099 InsertPos = nullptr;
12100
12101 // Now we update the operands.
12102 if (N->OperandList[0] != Op1)
12103 N->OperandList[0].set(Op1);
12104 if (N->OperandList[1] != Op2)
12105 N->OperandList[1].set(Op2);
12106
12108 // If this gets put into a CSE map, add it.
12109 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
12110 return N;
12111}
12112
12115 SDValue Ops[] = { Op1, Op2, Op3 };
12116 return UpdateNodeOperands(N, Ops);
12117}
12118
12121 SDValue Op3, SDValue Op4) {
12122 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
12123 return UpdateNodeOperands(N, Ops);
12124}
12125
12128 SDValue Op3, SDValue Op4, SDValue Op5) {
12129 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
12130 return UpdateNodeOperands(N, Ops);
12131}
12132
12135 unsigned NumOps = Ops.size();
12136 assert(N->getNumOperands() == NumOps &&
12137 "Update with wrong number of operands");
12138
12139 // If no operands changed just return the input node.
12140 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
12141 return N;
12142
12143 // See if the modified node already exists.
12144 void *InsertPos = nullptr;
12145 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
12146 return Existing;
12147
12148 // Nope it doesn't. Remove the node from its current place in the maps.
12149 if (InsertPos)
12150 if (!RemoveNodeFromCSEMaps(N))
12151 InsertPos = nullptr;
12152
12153 // Now we update the operands.
12154 for (unsigned i = 0; i != NumOps; ++i)
12155 if (N->OperandList[i] != Ops[i])
12156 N->OperandList[i].set(Ops[i]);
12157
12159 // If this gets put into a CSE map, add it.
12160 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
12161 return N;
12162}
12163
12164/// DropOperands - Release the operands and set this node to have
12165/// zero operands.
12167 // Unlike the code in MorphNodeTo that does this, we don't need to
12168 // watch for dead nodes here.
12169 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
12170 SDUse &Use = *I++;
12171 Use.set(SDValue());
12172 }
12173}
12174
12176 ArrayRef<MachineMemOperand *> NewMemRefs) {
12177 if (NewMemRefs.empty()) {
12178 N->clearMemRefs();
12179 return;
12180 }
12181
12182 // Check if we can avoid allocating by storing a single reference directly.
12183 if (NewMemRefs.size() == 1) {
12184 N->MemRefs = NewMemRefs[0];
12185 N->NumMemRefs = 1;
12186 return;
12187 }
12188
12189 MachineMemOperand **MemRefsBuffer =
12190 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
12191 llvm::copy(NewMemRefs, MemRefsBuffer);
12192 N->MemRefs = MemRefsBuffer;
12193 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
12194}
12195
12196/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
12197/// machine opcode.
12198///
12200 EVT VT) {
12201 SDVTList VTs = getVTList(VT);
12202 return SelectNodeTo(N, MachineOpc, VTs, {});
12203}
12204
12206 EVT VT, SDValue Op1) {
12207 SDVTList VTs = getVTList(VT);
12208 SDValue Ops[] = { Op1 };
12209 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12210}
12211
12213 EVT VT, SDValue Op1,
12214 SDValue Op2) {
12215 SDVTList VTs = getVTList(VT);
12216 SDValue Ops[] = { Op1, Op2 };
12217 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12218}
12219
12221 EVT VT, SDValue Op1,
12222 SDValue Op2, SDValue Op3) {
12223 SDVTList VTs = getVTList(VT);
12224 SDValue Ops[] = { Op1, Op2, Op3 };
12225 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12226}
12227
12230 SDVTList VTs = getVTList(VT);
12231 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12232}
12233
12235 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
12236 SDVTList VTs = getVTList(VT1, VT2);
12237 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12238}
12239
12241 EVT VT1, EVT VT2) {
12242 SDVTList VTs = getVTList(VT1, VT2);
12243 return SelectNodeTo(N, MachineOpc, VTs, {});
12244}
12245
12247 EVT VT1, EVT VT2, EVT VT3,
12249 SDVTList VTs = getVTList(VT1, VT2, VT3);
12250 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12251}
12252
12254 EVT VT1, EVT VT2,
12255 SDValue Op1, SDValue Op2) {
12256 SDVTList VTs = getVTList(VT1, VT2);
12257 SDValue Ops[] = { Op1, Op2 };
12258 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12259}
12260
12263 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
12264 // Reset the NodeID to -1.
12265 New->setNodeId(-1);
12266 if (New != N) {
12267 ReplaceAllUsesWith(N, New);
12269 }
12270 return New;
12271}
12272
12273/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
12274/// the line number information on the merged node since it is not possible to
12275/// preserve the information that operation is associated with multiple lines.
12276/// This will make the debugger working better at -O0, were there is a higher
12277/// probability having other instructions associated with that line.
12278///
12279/// For IROrder, we keep the smaller of the two
12280SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
12281 DebugLoc NLoc = N->getDebugLoc();
12282 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
12283 N->setDebugLoc(DebugLoc());
12284 }
12285 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
12286 N->setIROrder(Order);
12287 return N;
12288}
12289
12290/// MorphNodeTo - This *mutates* the specified node to have the specified
12291/// return type, opcode, and operands.
12292///
12293/// Note that MorphNodeTo returns the resultant node. If there is already a
12294/// node of the specified opcode and operands, it returns that node instead of
12295/// the current one. Note that the SDLoc need not be the same.
12296///
12297/// Using MorphNodeTo is faster than creating a new node and swapping it in
12298/// with ReplaceAllUsesWith both because it often avoids allocating a new
12299/// node, and because it doesn't require CSE recalculation for any of
12300/// the node's users.
12301///
12302/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
12303/// As a consequence it isn't appropriate to use from within the DAG combiner or
12304/// the legalizer which maintain worklists that would need to be updated when
12305/// deleting things.
12308 // If an identical node already exists, use it.
12309 void *IP = nullptr;
12310 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
12312 AddNodeIDNode(ID, Opc, VTs, Ops);
12313 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
12314 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
12315 }
12316
12317 if (!RemoveNodeFromCSEMaps(N))
12318 IP = nullptr;
12319
12320 // Start the morphing.
12321 N->NodeType = Opc;
12322 N->ValueList = VTs.VTs;
12323 N->NumValues = VTs.NumVTs;
12324
12325 // Clear the operands list, updating used nodes to remove this from their
12326 // use list. Keep track of any operands that become dead as a result.
12327 SmallPtrSet<SDNode*, 16> DeadNodeSet;
12328 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
12329 SDUse &Use = *I++;
12330 SDNode *Used = Use.getNode();
12331 Use.set(SDValue());
12332 if (Used->use_empty())
12333 DeadNodeSet.insert(Used);
12334 }
12335
12336 // For MachineNode, initialize the memory references information.
12338 MN->clearMemRefs();
12339
12340 // Swap for an appropriately sized array from the recycler.
12341 removeOperands(N);
12342 createOperands(N, Ops);
12343
12344 // Delete any nodes that are still dead after adding the uses for the
12345 // new operands.
12346 if (!DeadNodeSet.empty()) {
12347 SmallVector<SDNode *, 16> DeadNodes;
12348 for (SDNode *N : DeadNodeSet)
12349 if (N->use_empty())
12350 DeadNodes.push_back(N);
12351 RemoveDeadNodes(DeadNodes);
12352 }
12353
12354 if (IP)
12355 CSEMap.InsertNode(N, IP); // Memoize the new node.
12356 return N;
12357}
12358
12360 unsigned OrigOpc = Node->getOpcode();
12361 unsigned NewOpc;
12362 switch (OrigOpc) {
12363 default:
12364 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
12365#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
12366 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
12367#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
12368 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
12369#include "llvm/IR/ConstrainedOps.def"
12370 }
12371
12372 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
12373
12374 // We're taking this node out of the chain, so we need to re-link things.
12375 SDValue InputChain = Node->getOperand(0);
12376 SDValue OutputChain = SDValue(Node, 1);
12377 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
12378
12380 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
12381 Ops.push_back(Node->getOperand(i));
12382
12383 SDVTList VTs = getVTList(Node->getValueType(0));
12384 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
12385
12386 // MorphNodeTo can operate in two ways: if an existing node with the
12387 // specified operands exists, it can just return it. Otherwise, it
12388 // updates the node in place to have the requested operands.
12389 if (Res == Node) {
12390 // If we updated the node in place, reset the node ID. To the isel,
12391 // this should be just like a newly allocated machine node.
12392 Res->setNodeId(-1);
12393 } else {
12396 }
12397
12398 return Res;
12399}
12400
12401/// getMachineNode - These are used for target selectors to create a new node
12402/// with specified return type(s), MachineInstr opcode, and operands.
12403///
12404/// Note that getMachineNode returns the resultant node. If there is already a
12405/// node of the specified opcode and operands, it returns that node instead of
12406/// the current one.
12408 EVT VT) {
12409 SDVTList VTs = getVTList(VT);
12410 return getMachineNode(Opcode, dl, VTs, {});
12411}
12412
12414 EVT VT, SDValue Op1) {
12415 SDVTList VTs = getVTList(VT);
12416 SDValue Ops[] = { Op1 };
12417 return getMachineNode(Opcode, dl, VTs, Ops);
12418}
12419
12421 EVT VT, SDValue Op1, SDValue Op2) {
12422 SDVTList VTs = getVTList(VT);
12423 SDValue Ops[] = { Op1, Op2 };
12424 return getMachineNode(Opcode, dl, VTs, Ops);
12425}
12426
12428 EVT VT, SDValue Op1, SDValue Op2,
12429 SDValue Op3) {
12430 SDVTList VTs = getVTList(VT);
12431 SDValue Ops[] = { Op1, Op2, Op3 };
12432 return getMachineNode(Opcode, dl, VTs, Ops);
12433}
12434
12437 SDVTList VTs = getVTList(VT);
12438 return getMachineNode(Opcode, dl, VTs, Ops);
12439}
12440
12442 EVT VT1, EVT VT2, SDValue Op1,
12443 SDValue Op2) {
12444 SDVTList VTs = getVTList(VT1, VT2);
12445 SDValue Ops[] = { Op1, Op2 };
12446 return getMachineNode(Opcode, dl, VTs, Ops);
12447}
12448
12450 EVT VT1, EVT VT2, SDValue Op1,
12451 SDValue Op2, SDValue Op3) {
12452 SDVTList VTs = getVTList(VT1, VT2);
12453 SDValue Ops[] = { Op1, Op2, Op3 };
12454 return getMachineNode(Opcode, dl, VTs, Ops);
12455}
12456
12458 EVT VT1, EVT VT2,
12460 SDVTList VTs = getVTList(VT1, VT2);
12461 return getMachineNode(Opcode, dl, VTs, Ops);
12462}
12463
12465 EVT VT1, EVT VT2, EVT VT3,
12466 SDValue Op1, SDValue Op2) {
12467 SDVTList VTs = getVTList(VT1, VT2, VT3);
12468 SDValue Ops[] = { Op1, Op2 };
12469 return getMachineNode(Opcode, dl, VTs, Ops);
12470}
12471
12473 EVT VT1, EVT VT2, EVT VT3,
12474 SDValue Op1, SDValue Op2,
12475 SDValue Op3) {
12476 SDVTList VTs = getVTList(VT1, VT2, VT3);
12477 SDValue Ops[] = { Op1, Op2, Op3 };
12478 return getMachineNode(Opcode, dl, VTs, Ops);
12479}
12480
12482 EVT VT1, EVT VT2, EVT VT3,
12484 SDVTList VTs = getVTList(VT1, VT2, VT3);
12485 return getMachineNode(Opcode, dl, VTs, Ops);
12486}
12487
12489 ArrayRef<EVT> ResultTys,
12491 SDVTList VTs = getVTList(ResultTys);
12492 return getMachineNode(Opcode, dl, VTs, Ops);
12493}
12494
12496 SDVTList VTs,
12498 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
12500 void *IP = nullptr;
12501
12502 if (DoCSE) {
12504 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
12505 IP = nullptr;
12506 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
12507 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
12508 }
12509 }
12510
12511 // Allocate a new MachineSDNode.
12512 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
12513 createOperands(N, Ops);
12514
12515 if (DoCSE)
12516 CSEMap.InsertNode(N, IP);
12517
12518 InsertNode(N);
12519 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
12520 return N;
12521}
12522
12523/// getTargetExtractSubreg - A convenience function for creating
12524/// TargetOpcode::EXTRACT_SUBREG nodes.
12526 SDValue Operand) {
12527 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
12528 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
12529 VT, Operand, SRIdxVal);
12530 return SDValue(Subreg, 0);
12531}
12532
12533/// getTargetInsertSubreg - A convenience function for creating
12534/// TargetOpcode::INSERT_SUBREG nodes.
12536 SDValue Operand, SDValue Subreg) {
12537 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
12538 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
12539 VT, Operand, Subreg, SRIdxVal);
12540 return SDValue(Result, 0);
12541}
12542
12543/// getNodeIfExists - Get the specified node if it's already available, or
12544/// else return NULL.
12547 bool AllowCommute) {
12548 SDNodeFlags Flags;
12549 if (Inserter)
12550 Flags = Inserter->getFlags();
12551 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
12552}
12553
12556 const SDNodeFlags Flags,
12557 bool AllowCommute) {
12558 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
12559 return nullptr;
12560
12561 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
12563 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
12564 void *IP = nullptr;
12565 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
12566 E->intersectFlagsWith(Flags);
12567 return E;
12568 }
12569 return nullptr;
12570 };
12571
12572 if (SDNode *Existing = Lookup(Ops))
12573 return Existing;
12574
12575 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
12576 return Lookup({Ops[1], Ops[0]});
12577
12578 return nullptr;
12579}
12580
12581/// doesNodeExist - Check if a node exists without modifying its flags.
12582bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
12584 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
12586 AddNodeIDNode(ID, Opcode, VTList, Ops);
12587 void *IP = nullptr;
12588 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
12589 return true;
12590 }
12591 return false;
12592}
12593
12594/// getDbgValue - Creates a SDDbgValue node.
12595///
12596/// SDNode
12598 SDNode *N, unsigned R, bool IsIndirect,
12599 const DebugLoc &DL, unsigned O) {
12600 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12601 "Expected inlined-at fields to agree");
12602 return new (DbgInfo->getAlloc())
12603 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
12604 {}, IsIndirect, DL, O,
12605 /*IsVariadic=*/false);
12606}
12607
12608/// Constant
12610 DIExpression *Expr,
12611 const Value *C,
12612 const DebugLoc &DL, unsigned O) {
12613 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12614 "Expected inlined-at fields to agree");
12615 return new (DbgInfo->getAlloc())
12616 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
12617 /*IsIndirect=*/false, DL, O,
12618 /*IsVariadic=*/false);
12619}
12620
12621/// FrameIndex
12623 DIExpression *Expr, unsigned FI,
12624 bool IsIndirect,
12625 const DebugLoc &DL,
12626 unsigned O) {
12627 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12628 "Expected inlined-at fields to agree");
12629 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
12630}
12631
12632/// FrameIndex with dependencies
12634 DIExpression *Expr, unsigned FI,
12635 ArrayRef<SDNode *> Dependencies,
12636 bool IsIndirect,
12637 const DebugLoc &DL,
12638 unsigned O) {
12639 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12640 "Expected inlined-at fields to agree");
12641 return new (DbgInfo->getAlloc())
12642 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
12643 Dependencies, IsIndirect, DL, O,
12644 /*IsVariadic=*/false);
12645}
12646
12647/// VReg
12649 Register VReg, bool IsIndirect,
12650 const DebugLoc &DL, unsigned O) {
12651 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12652 "Expected inlined-at fields to agree");
12653 return new (DbgInfo->getAlloc())
12654 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
12655 {}, IsIndirect, DL, O,
12656 /*IsVariadic=*/false);
12657}
12658
12661 ArrayRef<SDNode *> Dependencies,
12662 bool IsIndirect, const DebugLoc &DL,
12663 unsigned O, bool IsVariadic) {
12664 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12665 "Expected inlined-at fields to agree");
12666 return new (DbgInfo->getAlloc())
12667 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
12668 DL, O, IsVariadic);
12669}
12670
12672 unsigned OffsetInBits, unsigned SizeInBits,
12673 bool InvalidateDbg) {
12674 SDNode *FromNode = From.getNode();
12675 SDNode *ToNode = To.getNode();
12676 assert(FromNode && ToNode && "Can't modify dbg values");
12677
12678 // PR35338
12679 // TODO: assert(From != To && "Redundant dbg value transfer");
12680 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
12681 if (From == To || FromNode == ToNode)
12682 return;
12683
12684 if (!FromNode->getHasDebugValue())
12685 return;
12686
12687 SDDbgOperand FromLocOp =
12688 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
12690
12692 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
12693 if (Dbg->isInvalidated())
12694 continue;
12695
12696 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
12697
12698 // Create a new location ops vector that is equal to the old vector, but
12699 // with each instance of FromLocOp replaced with ToLocOp.
12700 bool Changed = false;
12701 auto NewLocOps = Dbg->copyLocationOps();
12702 std::replace_if(
12703 NewLocOps.begin(), NewLocOps.end(),
12704 [&Changed, FromLocOp](const SDDbgOperand &Op) {
12705 bool Match = Op == FromLocOp;
12706 Changed |= Match;
12707 return Match;
12708 },
12709 ToLocOp);
12710 // Ignore this SDDbgValue if we didn't find a matching location.
12711 if (!Changed)
12712 continue;
12713
12714 DIVariable *Var = Dbg->getVariable();
12715 auto *Expr = Dbg->getExpression();
12716 // If a fragment is requested, update the expression.
12717 if (SizeInBits) {
12718 // When splitting a larger (e.g., sign-extended) value whose
12719 // lower bits are described with an SDDbgValue, do not attempt
12720 // to transfer the SDDbgValue to the upper bits.
12721 if (auto FI = Expr->getFragmentInfo())
12722 if (OffsetInBits + SizeInBits > FI->SizeInBits)
12723 continue;
12724 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
12725 SizeInBits);
12726 if (!Fragment)
12727 continue;
12728 Expr = *Fragment;
12729 }
12730
12731 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
12732 // Clone the SDDbgValue and move it to To.
12733 SDDbgValue *Clone = getDbgValueList(
12734 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12735 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12736 Dbg->isVariadic());
12737 ClonedDVs.push_back(Clone);
12738
12739 if (InvalidateDbg) {
12740 // Invalidate value and indicate the SDDbgValue should not be emitted.
12741 Dbg->setIsInvalidated();
12742 Dbg->setIsEmitted();
12743 }
12744 }
12745
12746 for (SDDbgValue *Dbg : ClonedDVs) {
12747 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12748 "Transferred DbgValues should depend on the new SDNode");
12749 AddDbgValue(Dbg, false);
12750 }
12751}
12752
12754 if (!N.getHasDebugValue())
12755 return;
12756
12757 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12758 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12759 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12760 return SDDbgOperand::fromNode(Node, ResNo);
12761 };
12762
12764 for (auto *DV : GetDbgValues(&N)) {
12765 if (DV->isInvalidated())
12766 continue;
12767 switch (N.getOpcode()) {
12768 default:
12769 break;
12770 case ISD::ADD: {
12771 SDValue N0 = N.getOperand(0);
12772 SDValue N1 = N.getOperand(1);
12773 if (!isa<ConstantSDNode>(N0)) {
12774 bool RHSConstant = isa<ConstantSDNode>(N1);
12776 if (RHSConstant)
12777 Offset = N.getConstantOperandVal(1);
12778 // We are not allowed to turn indirect debug values variadic, so
12779 // don't salvage those.
12780 if (!RHSConstant && DV->isIndirect())
12781 continue;
12782
12783 // Rewrite an ADD constant node into a DIExpression. Since we are
12784 // performing arithmetic to compute the variable's *value* in the
12785 // DIExpression, we need to mark the expression with a
12786 // DW_OP_stack_value.
12787 auto *DIExpr = DV->getExpression();
12788 auto NewLocOps = DV->copyLocationOps();
12789 bool Changed = false;
12790 size_t OrigLocOpsSize = NewLocOps.size();
12791 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12792 // We're not given a ResNo to compare against because the whole
12793 // node is going away. We know that any ISD::ADD only has one
12794 // result, so we can assume any node match is using the result.
12795 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12796 NewLocOps[i].getSDNode() != &N)
12797 continue;
12798 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12799 if (RHSConstant) {
12802 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12803 } else {
12804 // Convert to a variadic expression (if not already).
12805 // convertToVariadicExpression() returns a const pointer, so we use
12806 // a temporary const variable here.
12807 const auto *TmpDIExpr =
12811 ExprOps.push_back(NewLocOps.size());
12812 ExprOps.push_back(dwarf::DW_OP_plus);
12813 SDDbgOperand RHS =
12815 NewLocOps.push_back(RHS);
12816 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12817 }
12818 Changed = true;
12819 }
12820 (void)Changed;
12821 assert(Changed && "Salvage target doesn't use N");
12822
12823 bool IsVariadic =
12824 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12825
12826 auto AdditionalDependencies = DV->getAdditionalDependencies();
12827 SDDbgValue *Clone = getDbgValueList(
12828 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12829 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12830 ClonedDVs.push_back(Clone);
12831 DV->setIsInvalidated();
12832 DV->setIsEmitted();
12833 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12834 N0.getNode()->dumprFull(this);
12835 dbgs() << " into " << *DIExpr << '\n');
12836 }
12837 break;
12838 }
12839 case ISD::TRUNCATE: {
12840 SDValue N0 = N.getOperand(0);
12841 TypeSize FromSize = N0.getValueSizeInBits();
12842 TypeSize ToSize = N.getValueSizeInBits(0);
12843
12844 DIExpression *DbgExpression = DV->getExpression();
12845 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12846 auto NewLocOps = DV->copyLocationOps();
12847 bool Changed = false;
12848 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12849 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12850 NewLocOps[i].getSDNode() != &N)
12851 continue;
12852
12853 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12854 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12855 Changed = true;
12856 }
12857 assert(Changed && "Salvage target doesn't use N");
12858 (void)Changed;
12859
12860 SDDbgValue *Clone =
12861 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12862 DV->getAdditionalDependencies(), DV->isIndirect(),
12863 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12864
12865 ClonedDVs.push_back(Clone);
12866 DV->setIsInvalidated();
12867 DV->setIsEmitted();
12868 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12869 dbgs() << " into " << *DbgExpression << '\n');
12870 break;
12871 }
12872 }
12873 }
12874
12875 for (SDDbgValue *Dbg : ClonedDVs) {
12876 assert((!Dbg->getSDNodes().empty() ||
12877 llvm::any_of(Dbg->getLocationOps(),
12878 [&](const SDDbgOperand &Op) {
12879 return Op.getKind() == SDDbgOperand::FRAMEIX;
12880 })) &&
12881 "Salvaged DbgValue should depend on a new SDNode");
12882 AddDbgValue(Dbg, false);
12883 }
12884}
12885
12886/// Creates a SDDbgLabel node.
12888 const DebugLoc &DL, unsigned O) {
12889 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12890 "Expected inlined-at fields to agree");
12891 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12892}
12893
12894namespace {
12895
12896/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12897/// pointed to by a use iterator is deleted, increment the use iterator
12898/// so that it doesn't dangle.
12899///
12900class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12903
12904 void NodeDeleted(SDNode *N, SDNode *E) override {
12905 // Increment the iterator as needed.
12906 while (UI != UE && N == UI->getUser())
12907 ++UI;
12908 }
12909
12910public:
12911 RAUWUpdateListener(SelectionDAG &d,
12914 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12915};
12916
12917} // end anonymous namespace
12918
12919/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12920/// This can cause recursive merging of nodes in the DAG.
12921///
12922/// This version assumes From has a single result value.
12923///
12925 SDNode *From = FromN.getNode();
12926 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12927 "Cannot replace with this method!");
12928 assert(From != To.getNode() && "Cannot replace uses of with self");
12929
12930 // Preserve Debug Values
12931 transferDbgValues(FromN, To);
12932 // Preserve extra info.
12933 copyExtraInfo(From, To.getNode());
12934
12935 // Iterate over all the existing uses of From. New uses will be added
12936 // to the beginning of the use list, which we avoid visiting.
12937 // This specifically avoids visiting uses of From that arise while the
12938 // replacement is happening, because any such uses would be the result
12939 // of CSE: If an existing node looks like From after one of its operands
12940 // is replaced by To, we don't want to replace of all its users with To
12941 // too. See PR3018 for more info.
12942 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12943 RAUWUpdateListener Listener(*this, UI, UE);
12944 while (UI != UE) {
12945 SDNode *User = UI->getUser();
12946
12947 // This node is about to morph, remove its old self from the CSE maps.
12948 RemoveNodeFromCSEMaps(User);
12949
12950 // A user can appear in a use list multiple times, and when this
12951 // happens the uses are usually next to each other in the list.
12952 // To help reduce the number of CSE recomputations, process all
12953 // the uses of this user that we can find this way.
12954 do {
12955 SDUse &Use = *UI;
12956 ++UI;
12957 Use.set(To);
12958 if (To->isDivergent() != From->isDivergent())
12960 } while (UI != UE && UI->getUser() == User);
12961 // Now that we have modified User, add it back to the CSE maps. If it
12962 // already exists there, recursively merge the results together.
12963 AddModifiedNodeToCSEMaps(User);
12964 }
12965
12966 // If we just RAUW'd the root, take note.
12967 if (FromN == getRoot())
12968 setRoot(To);
12969}
12970
12971/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12972/// This can cause recursive merging of nodes in the DAG.
12973///
12974/// This version assumes that for each value of From, there is a
12975/// corresponding value in To in the same position with the same type.
12976///
12978#ifndef NDEBUG
12979 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12980 assert((!From->hasAnyUseOfValue(i) ||
12981 From->getValueType(i) == To->getValueType(i)) &&
12982 "Cannot use this version of ReplaceAllUsesWith!");
12983#endif
12984
12985 // Handle the trivial case.
12986 if (From == To)
12987 return;
12988
12989 // Preserve Debug Info. Only do this if there's a use.
12990 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12991 if (From->hasAnyUseOfValue(i)) {
12992 assert((i < To->getNumValues()) && "Invalid To location");
12993 transferDbgValues(SDValue(From, i), SDValue(To, i));
12994 }
12995 // Preserve extra info.
12996 copyExtraInfo(From, To);
12997
12998 // Iterate over just the existing users of From. See the comments in
12999 // the ReplaceAllUsesWith above.
13000 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
13001 RAUWUpdateListener Listener(*this, UI, UE);
13002 while (UI != UE) {
13003 SDNode *User = UI->getUser();
13004
13005 // This node is about to morph, remove its old self from the CSE maps.
13006 RemoveNodeFromCSEMaps(User);
13007
13008 // A user can appear in a use list multiple times, and when this
13009 // happens the uses are usually next to each other in the list.
13010 // To help reduce the number of CSE recomputations, process all
13011 // the uses of this user that we can find this way.
13012 do {
13013 SDUse &Use = *UI;
13014 ++UI;
13015 Use.setNode(To);
13016 if (To->isDivergent() != From->isDivergent())
13018 } while (UI != UE && UI->getUser() == User);
13019
13020 // Now that we have modified User, add it back to the CSE maps. If it
13021 // already exists there, recursively merge the results together.
13022 AddModifiedNodeToCSEMaps(User);
13023 }
13024
13025 // If we just RAUW'd the root, take note.
13026 if (From == getRoot().getNode())
13027 setRoot(SDValue(To, getRoot().getResNo()));
13028}
13029
13030/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
13031/// This can cause recursive merging of nodes in the DAG.
13032///
13033/// This version can replace From with any result values. To must match the
13034/// number and types of values returned by From.
13036 if (From->getNumValues() == 1) // Handle the simple case efficiently.
13037 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
13038
13039 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
13040 // Preserve Debug Info.
13041 transferDbgValues(SDValue(From, i), To[i]);
13042 // Preserve extra info.
13043 copyExtraInfo(From, To[i].getNode());
13044 }
13045
13046 // Iterate over just the existing users of From. See the comments in
13047 // the ReplaceAllUsesWith above.
13048 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
13049 RAUWUpdateListener Listener(*this, UI, UE);
13050 while (UI != UE) {
13051 SDNode *User = UI->getUser();
13052
13053 // This node is about to morph, remove its old self from the CSE maps.
13054 RemoveNodeFromCSEMaps(User);
13055
13056 // A user can appear in a use list multiple times, and when this happens the
13057 // uses are usually next to each other in the list. To help reduce the
13058 // number of CSE and divergence recomputations, process all the uses of this
13059 // user that we can find this way.
13060 bool To_IsDivergent = false;
13061 do {
13062 SDUse &Use = *UI;
13063 const SDValue &ToOp = To[Use.getResNo()];
13064 ++UI;
13065 Use.set(ToOp);
13066 if (ToOp.getValueType() != MVT::Other)
13067 To_IsDivergent |= ToOp->isDivergent();
13068 } while (UI != UE && UI->getUser() == User);
13069
13070 if (To_IsDivergent != From->isDivergent())
13072
13073 // Now that we have modified User, add it back to the CSE maps. If it
13074 // already exists there, recursively merge the results together.
13075 AddModifiedNodeToCSEMaps(User);
13076 }
13077
13078 // If we just RAUW'd the root, take note.
13079 if (From == getRoot().getNode())
13080 setRoot(SDValue(To[getRoot().getResNo()]));
13081}
13082
13083/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
13084/// uses of other values produced by From.getNode() alone. The Deleted
13085/// vector is handled the same way as for ReplaceAllUsesWith.
13087 // Handle the really simple, really trivial case efficiently.
13088 if (From == To) return;
13089
13090 // Handle the simple, trivial, case efficiently.
13091 if (From.getNode()->getNumValues() == 1) {
13092 ReplaceAllUsesWith(From, To);
13093 return;
13094 }
13095
13096 // Preserve Debug Info.
13097 transferDbgValues(From, To);
13098 copyExtraInfo(From.getNode(), To.getNode());
13099
13100 // Iterate over just the existing users of From. See the comments in
13101 // the ReplaceAllUsesWith above.
13102 SDNode::use_iterator UI = From.getNode()->use_begin(),
13103 UE = From.getNode()->use_end();
13104 RAUWUpdateListener Listener(*this, UI, UE);
13105 while (UI != UE) {
13106 SDNode *User = UI->getUser();
13107 bool UserRemovedFromCSEMaps = false;
13108
13109 // A user can appear in a use list multiple times, and when this
13110 // happens the uses are usually next to each other in the list.
13111 // To help reduce the number of CSE recomputations, process all
13112 // the uses of this user that we can find this way.
13113 do {
13114 SDUse &Use = *UI;
13115
13116 // Skip uses of different values from the same node.
13117 if (Use.getResNo() != From.getResNo()) {
13118 ++UI;
13119 continue;
13120 }
13121
13122 // If this node hasn't been modified yet, it's still in the CSE maps,
13123 // so remove its old self from the CSE maps.
13124 if (!UserRemovedFromCSEMaps) {
13125 RemoveNodeFromCSEMaps(User);
13126 UserRemovedFromCSEMaps = true;
13127 }
13128
13129 ++UI;
13130 Use.set(To);
13131 if (To->isDivergent() != From->isDivergent())
13133 } while (UI != UE && UI->getUser() == User);
13134 // We are iterating over all uses of the From node, so if a use
13135 // doesn't use the specific value, no changes are made.
13136 if (!UserRemovedFromCSEMaps)
13137 continue;
13138
13139 // Now that we have modified User, add it back to the CSE maps. If it
13140 // already exists there, recursively merge the results together.
13141 AddModifiedNodeToCSEMaps(User);
13142 }
13143
13144 // If we just RAUW'd the root, take note.
13145 if (From == getRoot())
13146 setRoot(To);
13147}
13148
13149namespace {
13150
13151/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
13152/// to record information about a use.
13153struct UseMemo {
13154 SDNode *User;
13155 unsigned Index;
13156 SDUse *Use;
13157};
13158
13159/// operator< - Sort Memos by User.
13160bool operator<(const UseMemo &L, const UseMemo &R) {
13161 return (intptr_t)L.User < (intptr_t)R.User;
13162}
13163
13164/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
13165/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
13166/// the node already has been taken care of recursively.
13167class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
13168 SmallVectorImpl<UseMemo> &Uses;
13169
13170 void NodeDeleted(SDNode *N, SDNode *E) override {
13171 for (UseMemo &Memo : Uses)
13172 if (Memo.User == N)
13173 Memo.User = nullptr;
13174 }
13175
13176public:
13177 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
13178 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
13179};
13180
13181} // end anonymous namespace
13182
13183/// Return true if a glue output should propagate divergence information.
13185 switch (Node->getOpcode()) {
13186 case ISD::CopyFromReg:
13187 case ISD::CopyToReg:
13188 return false;
13189 default:
13190 return true;
13191 }
13192
13193 llvm_unreachable("covered opcode switch");
13194}
13195
13197 if (TLI->isSDNodeAlwaysUniform(N)) {
13198 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
13199 "Conflicting divergence information!");
13200 return false;
13201 }
13202 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
13203 return true;
13204 for (const auto &Op : N->ops()) {
13205 EVT VT = Op.getValueType();
13206
13207 // Skip Chain. It does not carry divergence.
13208 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
13209 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
13210 return true;
13211 }
13212 return false;
13213}
13214
13216 SmallVector<SDNode *, 16> Worklist(1, N);
13217 do {
13218 N = Worklist.pop_back_val();
13219 bool IsDivergent = calculateDivergence(N);
13220 if (N->SDNodeBits.IsDivergent != IsDivergent) {
13221 N->SDNodeBits.IsDivergent = IsDivergent;
13222 llvm::append_range(Worklist, N->users());
13223 }
13224 } while (!Worklist.empty());
13225}
13226
13227void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
13229 Order.reserve(AllNodes.size());
13230 for (auto &N : allnodes()) {
13231 unsigned NOps = N.getNumOperands();
13232 Degree[&N] = NOps;
13233 if (0 == NOps)
13234 Order.push_back(&N);
13235 }
13236 for (size_t I = 0; I != Order.size(); ++I) {
13237 SDNode *N = Order[I];
13238 for (auto *U : N->users()) {
13239 unsigned &UnsortedOps = Degree[U];
13240 if (0 == --UnsortedOps)
13241 Order.push_back(U);
13242 }
13243 }
13244}
13245
13246#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
13247void SelectionDAG::VerifyDAGDivergence() {
13248 std::vector<SDNode *> TopoOrder;
13249 CreateTopologicalOrder(TopoOrder);
13250 for (auto *N : TopoOrder) {
13251 assert(calculateDivergence(N) == N->isDivergent() &&
13252 "Divergence bit inconsistency detected");
13253 }
13254}
13255#endif
13256
13257/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
13258/// uses of other values produced by From.getNode() alone. The same value
13259/// may appear in both the From and To list. The Deleted vector is
13260/// handled the same way as for ReplaceAllUsesWith.
13262 const SDValue *To,
13263 unsigned Num){
13264 // Handle the simple, trivial case efficiently.
13265 if (Num == 1)
13266 return ReplaceAllUsesOfValueWith(*From, *To);
13267
13268 transferDbgValues(*From, *To);
13269 copyExtraInfo(From->getNode(), To->getNode());
13270
13271 // Read up all the uses and make records of them. This helps
13272 // processing new uses that are introduced during the
13273 // replacement process.
13275 for (unsigned i = 0; i != Num; ++i) {
13276 unsigned FromResNo = From[i].getResNo();
13277 SDNode *FromNode = From[i].getNode();
13278 for (SDUse &Use : FromNode->uses()) {
13279 if (Use.getResNo() == FromResNo) {
13280 UseMemo Memo = {Use.getUser(), i, &Use};
13281 Uses.push_back(Memo);
13282 }
13283 }
13284 }
13285
13286 // Sort the uses, so that all the uses from a given User are together.
13288 RAUOVWUpdateListener Listener(*this, Uses);
13289
13290 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
13291 UseIndex != UseIndexEnd; ) {
13292 // We know that this user uses some value of From. If it is the right
13293 // value, update it.
13294 SDNode *User = Uses[UseIndex].User;
13295 // If the node has been deleted by recursive CSE updates when updating
13296 // another node, then just skip this entry.
13297 if (User == nullptr) {
13298 ++UseIndex;
13299 continue;
13300 }
13301
13302 // This node is about to morph, remove its old self from the CSE maps.
13303 RemoveNodeFromCSEMaps(User);
13304
13305 // The Uses array is sorted, so all the uses for a given User
13306 // are next to each other in the list.
13307 // To help reduce the number of CSE recomputations, process all
13308 // the uses of this user that we can find this way.
13309 do {
13310 unsigned i = Uses[UseIndex].Index;
13311 SDUse &Use = *Uses[UseIndex].Use;
13312 ++UseIndex;
13313
13314 Use.set(To[i]);
13315 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
13316
13317 // Now that we have modified User, add it back to the CSE maps. If it
13318 // already exists there, recursively merge the results together.
13319 AddModifiedNodeToCSEMaps(User);
13320 }
13321}
13322
13323/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
13324/// based on their topological order. It returns the maximum id and a vector
13325/// of the SDNodes* in assigned order by reference.
13327 unsigned DAGSize = 0;
13328
13329 // SortedPos tracks the progress of the algorithm. Nodes before it are
13330 // sorted, nodes after it are unsorted. When the algorithm completes
13331 // it is at the end of the list.
13332 allnodes_iterator SortedPos = allnodes_begin();
13333
13334 // Visit all the nodes. Move nodes with no operands to the front of
13335 // the list immediately. Annotate nodes that do have operands with their
13336 // operand count. Before we do this, the Node Id fields of the nodes
13337 // may contain arbitrary values. After, the Node Id fields for nodes
13338 // before SortedPos will contain the topological sort index, and the
13339 // Node Id fields for nodes At SortedPos and after will contain the
13340 // count of outstanding operands.
13342 checkForCycles(&N, this);
13343 unsigned Degree = N.getNumOperands();
13344 if (Degree == 0) {
13345 // A node with no uses, add it to the result array immediately.
13346 N.setNodeId(DAGSize++);
13347 allnodes_iterator Q(&N);
13348 if (Q != SortedPos)
13349 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
13350 assert(SortedPos != AllNodes.end() && "Overran node list");
13351 ++SortedPos;
13352 } else {
13353 // Temporarily use the Node Id as scratch space for the degree count.
13354 N.setNodeId(Degree);
13355 }
13356 }
13357
13358 // Visit all the nodes. As we iterate, move nodes into sorted order,
13359 // such that by the time the end is reached all nodes will be sorted.
13360 for (SDNode &Node : allnodes()) {
13361 SDNode *N = &Node;
13362 checkForCycles(N, this);
13363 // N is in sorted position, so all its uses have one less operand
13364 // that needs to be sorted.
13365 for (SDNode *P : N->users()) {
13366 unsigned Degree = P->getNodeId();
13367 assert(Degree != 0 && "Invalid node degree");
13368 --Degree;
13369 if (Degree == 0) {
13370 // All of P's operands are sorted, so P may sorted now.
13371 P->setNodeId(DAGSize++);
13372 if (P->getIterator() != SortedPos)
13373 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
13374 assert(SortedPos != AllNodes.end() && "Overran node list");
13375 ++SortedPos;
13376 } else {
13377 // Update P's outstanding operand count.
13378 P->setNodeId(Degree);
13379 }
13380 }
13381 if (Node.getIterator() == SortedPos) {
13382#ifndef NDEBUG
13384 SDNode *S = &*++I;
13385 dbgs() << "Overran sorted position:\n";
13386 S->dumprFull(this); dbgs() << "\n";
13387 dbgs() << "Checking if this is due to cycles\n";
13388 checkForCycles(this, true);
13389#endif
13390 llvm_unreachable(nullptr);
13391 }
13392 }
13393
13394 assert(SortedPos == AllNodes.end() &&
13395 "Topological sort incomplete!");
13396 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
13397 "First node in topological sort is not the entry token!");
13398 assert(AllNodes.front().getNodeId() == 0 &&
13399 "First node in topological sort has non-zero id!");
13400 assert(AllNodes.front().getNumOperands() == 0 &&
13401 "First node in topological sort has operands!");
13402 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
13403 "Last node in topologic sort has unexpected id!");
13404 assert(AllNodes.back().use_empty() &&
13405 "Last node in topologic sort has users!");
13406 assert(DAGSize == allnodes_size() && "Node count mismatch!");
13407 return DAGSize;
13408}
13409
13411 SmallVectorImpl<const SDNode *> &SortedNodes) const {
13412 SortedNodes.clear();
13413 // Node -> remaining number of outstanding operands.
13414 DenseMap<const SDNode *, unsigned> RemainingOperands;
13415
13416 // Put nodes without any operands into SortedNodes first.
13417 for (const SDNode &N : allnodes()) {
13418 checkForCycles(&N, this);
13419 unsigned NumOperands = N.getNumOperands();
13420 if (NumOperands == 0)
13421 SortedNodes.push_back(&N);
13422 else
13423 // Record their total number of outstanding operands.
13424 RemainingOperands[&N] = NumOperands;
13425 }
13426
13427 // A node is pushed into SortedNodes when all of its operands (predecessors in
13428 // the graph) are also in SortedNodes.
13429 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
13430 const SDNode *N = SortedNodes[i];
13431 for (const SDNode *U : N->users()) {
13432 // HandleSDNode is never part of a DAG and therefore has no entry in
13433 // RemainingOperands.
13434 if (U->getOpcode() == ISD::HANDLENODE)
13435 continue;
13436 unsigned &NumRemOperands = RemainingOperands[U];
13437 assert(NumRemOperands && "Invalid number of remaining operands");
13438 --NumRemOperands;
13439 if (!NumRemOperands)
13440 SortedNodes.push_back(U);
13441 }
13442 }
13443
13444 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
13445 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
13446 "First node in topological sort is not the entry token");
13447 assert(SortedNodes.front()->getNumOperands() == 0 &&
13448 "First node in topological sort has operands");
13449}
13450
13451/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
13452/// value is produced by SD.
13453void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
13454 for (SDNode *SD : DB->getSDNodes()) {
13455 if (!SD)
13456 continue;
13457 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
13458 SD->setHasDebugValue(true);
13459 }
13460 DbgInfo->add(DB, isParameter);
13461}
13462
13463void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
13464
13466 SDValue NewMemOpChain) {
13467 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
13468 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
13469 // The new memory operation must have the same position as the old load in
13470 // terms of memory dependency. Create a TokenFactor for the old load and new
13471 // memory operation and update uses of the old load's output chain to use that
13472 // TokenFactor.
13473 if (OldChain == NewMemOpChain || OldChain.use_empty())
13474 return NewMemOpChain;
13475
13476 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
13477 OldChain, NewMemOpChain);
13478 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
13479 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
13480 return TokenFactor;
13481}
13482
13484 SDValue NewMemOp) {
13485 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
13486 SDValue OldChain = SDValue(OldLoad, 1);
13487 SDValue NewMemOpChain = NewMemOp.getValue(1);
13488 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
13489}
13490
13492 Function **OutFunction) {
13493 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
13494
13495 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
13496 auto *Module = MF->getFunction().getParent();
13497 auto *Function = Module->getFunction(Symbol);
13498
13499 if (OutFunction != nullptr)
13500 *OutFunction = Function;
13501
13502 if (Function != nullptr) {
13503 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
13504 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
13505 }
13506
13507 std::string ErrorStr;
13508 raw_string_ostream ErrorFormatter(ErrorStr);
13509 ErrorFormatter << "Undefined external symbol ";
13510 ErrorFormatter << '"' << Symbol << '"';
13511 report_fatal_error(Twine(ErrorStr));
13512}
13513
13514//===----------------------------------------------------------------------===//
13515// SDNode Class
13516//===----------------------------------------------------------------------===//
13517
13520 return Const != nullptr && Const->isZero();
13521}
13522
13524 return V.isUndef() || isNullConstant(V);
13525}
13526
13529 return Const != nullptr && Const->isZero() && !Const->isNegative();
13530}
13531
13534 return Const != nullptr && Const->isAllOnes();
13535}
13536
13539 return Const != nullptr && Const->isOne();
13540}
13541
13544 return Const != nullptr && Const->isMinSignedValue();
13545}
13546
13548 SDValue V, unsigned OperandNo,
13549 unsigned Depth) const {
13550 APInt DemandedElts = getDemandAllEltsMask(V);
13551 return isIdentityElement(Opcode, Flags, V, DemandedElts, OperandNo, Depth);
13552}
13553
13555 SDValue V, const APInt &DemandedElts,
13556 unsigned OperandNo, unsigned Depth) const {
13557 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
13558 // TODO: Target-specific opcodes could be added.
13559 if (auto *ConstV = isConstOrConstSplat(V, DemandedElts, /*AllowUndefs*/ false,
13560 /*AllowTruncation*/ true)) {
13561 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
13562 switch (Opcode) {
13563 case ISD::ADD:
13564 case ISD::OR:
13565 case ISD::XOR:
13566 case ISD::UMAX:
13567 return Const.isZero();
13568 case ISD::MUL:
13569 return Const.isOne();
13570 case ISD::AND:
13571 case ISD::UMIN:
13572 return Const.isAllOnes();
13573 case ISD::SMAX:
13574 return Const.isMinSignedValue();
13575 case ISD::SMIN:
13576 return Const.isMaxSignedValue();
13577 case ISD::SUB:
13578 case ISD::SHL:
13579 case ISD::SRA:
13580 case ISD::SRL:
13581 return OperandNo == 1 && Const.isZero();
13582 case ISD::UDIV:
13583 case ISD::SDIV:
13584 return OperandNo == 1 && Const.isOne();
13585 }
13586 } else if (auto *ConstFP = isConstOrConstSplatFP(V, DemandedElts)) {
13587 switch (Opcode) {
13588 case ISD::FADD:
13589 return ConstFP->isZero() &&
13590 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
13591 case ISD::FSUB:
13592 return OperandNo == 1 && ConstFP->isZero() &&
13593 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
13594 case ISD::FMUL:
13595 return ConstFP->isExactlyValue(1.0);
13596 case ISD::FDIV:
13597 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
13598 case ISD::FMINNUM:
13599 case ISD::FMAXNUM: {
13600 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13601 EVT VT = V.getValueType();
13602 const fltSemantics &Semantics = VT.getFltSemantics();
13603 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics)
13604 : !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
13605 : APFloat::getLargest(Semantics);
13606 if (Opcode == ISD::FMAXNUM)
13607 NeutralAF.changeSign();
13608
13609 return ConstFP->isExactlyValue(NeutralAF);
13610 }
13611 }
13612 }
13613 return false;
13614}
13615
13617 while (V.getOpcode() == ISD::BITCAST)
13618 V = V.getOperand(0);
13619 return V;
13620}
13621
13623 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
13624 V = V.getOperand(0);
13625 return V;
13626}
13627
13629 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
13630 V = V.getOperand(0);
13631 return V;
13632}
13633
13635 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13636 SDValue InVec = V.getOperand(0);
13637 SDValue EltNo = V.getOperand(2);
13638 EVT VT = InVec.getValueType();
13639 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
13640 if (IndexC && VT.isFixedLengthVector() &&
13641 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
13642 !DemandedElts[IndexC->getZExtValue()]) {
13643 V = InVec;
13644 continue;
13645 }
13646 break;
13647 }
13648 return V;
13649}
13650
13652 while (V.getOpcode() == ISD::TRUNCATE)
13653 V = V.getOperand(0);
13654 return V;
13655}
13656
13657bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
13658 if (V.getOpcode() != ISD::XOR)
13659 return false;
13660 V = peekThroughBitcasts(V.getOperand(1));
13661 unsigned NumBits = V.getScalarValueSizeInBits();
13662 ConstantSDNode *C =
13663 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
13664 return C && (C->getAPIntValue().countr_one() >= NumBits);
13665}
13666
13668 bool AllowTruncation) {
13669 APInt DemandedElts = getDemandAllEltsMask(N);
13670 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
13671}
13672
13674 bool AllowUndefs,
13675 bool AllowTruncation) {
13677 return CN;
13678
13679 // SplatVectors can truncate their operands. Ignore that case here unless
13680 // AllowTruncation is set.
13681 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
13682 EVT VecEltVT = N->getValueType(0).getVectorElementType();
13683 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
13684 EVT CVT = CN->getValueType(0);
13685 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
13686 if (AllowTruncation || CVT == VecEltVT)
13687 return CN;
13688 }
13689 }
13690
13692 BitVector UndefElements;
13693 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
13694
13695 // BuildVectors can truncate their operands. Ignore that case here unless
13696 // AllowTruncation is set.
13697 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13698 if (CN && (UndefElements.none() || AllowUndefs)) {
13699 EVT CVT = CN->getValueType(0);
13700 EVT NSVT = N.getValueType().getScalarType();
13701 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
13702 if (AllowTruncation || (CVT == NSVT))
13703 return CN;
13704 }
13705 }
13706
13707 return nullptr;
13708}
13709
13711 APInt DemandedElts = getDemandAllEltsMask(N);
13712 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
13713}
13714
13716 const APInt &DemandedElts,
13717 bool AllowUndefs) {
13719 return CN;
13720
13722 BitVector UndefElements;
13723 ConstantFPSDNode *CN =
13724 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
13725 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13726 if (CN && (UndefElements.none() || AllowUndefs))
13727 return CN;
13728 }
13729
13730 if (N.getOpcode() == ISD::SPLAT_VECTOR)
13731 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
13732 return CN;
13733
13734 return nullptr;
13735}
13736
13737bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
13738 // TODO: may want to use peekThroughBitcast() here.
13739 ConstantSDNode *C =
13740 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13741 return C && C->isZero();
13742}
13743
13744bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
13745 ConstantSDNode *C =
13746 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
13747 return C && C->isOne();
13748}
13749
13750bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
13751 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13752 return C && C->isExactlyValue(1.0);
13753}
13754
13755bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
13757 unsigned BitWidth = N.getScalarValueSizeInBits();
13758 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13759 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
13760}
13761
13762bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
13763 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13764 return C && APInt::isSameValue(C->getAPIntValue(),
13765 APInt(C->getAPIntValue().getBitWidth(), 1));
13766}
13767
13768bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
13770 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
13771 return C && C->isZero();
13772}
13773
13774bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
13775 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13776 return C && C->isZero();
13777}
13778
13782
13784 unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt,
13786 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MemRefs(memrefs) {
13787 bool IsVolatile = false;
13788 bool IsNonTemporal = false;
13789 bool IsDereferenceable = true;
13790 bool IsInvariant = true;
13791 for (const MachineMemOperand *MMO : memoperands()) {
13792 IsVolatile |= MMO->isVolatile();
13793 IsNonTemporal |= MMO->isNonTemporal();
13794 IsDereferenceable &= MMO->isDereferenceable();
13795 IsInvariant &= MMO->isInvariant();
13796 }
13797 MemSDNodeBits.IsVolatile = IsVolatile;
13798 MemSDNodeBits.IsNonTemporal = IsNonTemporal;
13799 MemSDNodeBits.IsDereferenceable = IsDereferenceable;
13800 MemSDNodeBits.IsInvariant = IsInvariant;
13801
13802 // For the single-MMO case, we check here that the size of the memory operand
13803 // fits within the size of the MMO. This is because the MMO might indicate
13804 // only a possible address range instead of specifying the affected memory
13805 // addresses precisely.
13808 getMemOperand()->getSize().getValue())) &&
13809 "Size mismatch!");
13810}
13811
13812/// Profile - Gather unique data for the node.
13813///
13815 AddNodeIDNode(ID, this);
13816}
13817
13818namespace {
13819
13820 struct EVTArray {
13821 std::vector<EVT> VTs;
13822
13823 EVTArray() {
13824 VTs.reserve(MVT::VALUETYPE_SIZE);
13825 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13826 VTs.push_back(MVT((MVT::SimpleValueType)i));
13827 }
13828 };
13829
13830} // end anonymous namespace
13831
13832/// getValueTypeList - Return a pointer to the specified value type.
13833///
13834const EVT *SDNode::getValueTypeList(MVT VT) {
13835 static EVTArray SimpleVTArray;
13836
13837 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13838 return &SimpleVTArray.VTs[VT.SimpleTy];
13839}
13840
13841/// hasAnyUseOfValue - Return true if there are any use of the indicated
13842/// value. This method ignores uses of other values defined by this operation.
13843bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13844 assert(Value < getNumValues() && "Bad value!");
13845
13846 for (SDUse &U : uses())
13847 if (U.getResNo() == Value)
13848 return true;
13849
13850 return false;
13851}
13852
13853/// isOnlyUserOf - Return true if this node is the only use of N.
13854bool SDNode::isOnlyUserOf(const SDNode *N) const {
13855 bool Seen = false;
13856 for (const SDNode *User : N->users()) {
13857 if (User == this)
13858 Seen = true;
13859 else
13860 return false;
13861 }
13862
13863 return Seen;
13864}
13865
13866/// Return true if the only users of N are contained in Nodes.
13868 bool Seen = false;
13869 for (const SDNode *User : N->users()) {
13870 if (llvm::is_contained(Nodes, User))
13871 Seen = true;
13872 else
13873 return false;
13874 }
13875
13876 return Seen;
13877}
13878
13879/// Return true if the referenced return value is an operand of N.
13880bool SDValue::isOperandOf(const SDNode *N) const {
13881 return is_contained(N->op_values(), *this);
13882}
13883
13884bool SDNode::isOperandOf(const SDNode *N) const {
13885 return any_of(N->op_values(),
13886 [this](SDValue Op) { return this == Op.getNode(); });
13887}
13888
13889/// reachesChainWithoutSideEffects - Return true if this operand (which must
13890/// be a chain) reaches the specified operand without crossing any
13891/// side-effecting instructions on any chain path. In practice, this looks
13892/// through token factors and non-volatile loads. In order to remain efficient,
13893/// this only looks a couple of nodes in, it does not do an exhaustive search.
13894///
13895/// Note that we only need to examine chains when we're searching for
13896/// side-effects; SelectionDAG requires that all side-effects are represented
13897/// by chains, even if another operand would force a specific ordering. This
13898/// constraint is necessary to allow transformations like splitting loads.
13900 unsigned Depth) const {
13901 if (*this == Dest) return true;
13902
13903 // Don't search too deeply, we just want to be able to see through
13904 // TokenFactor's etc.
13905 if (Depth == 0) return false;
13906
13907 // If this is a token factor, all inputs to the TF happen in parallel.
13908 if (getOpcode() == ISD::TokenFactor) {
13909 // First, try a shallow search.
13910 if (is_contained((*this)->ops(), Dest)) {
13911 // We found the chain we want as an operand of this TokenFactor.
13912 // Essentially, we reach the chain without side-effects if we could
13913 // serialize the TokenFactor into a simple chain of operations with
13914 // Dest as the last operation. This is automatically true if the
13915 // chain has one use: there are no other ordering constraints.
13916 // If the chain has more than one use, we give up: some other
13917 // use of Dest might force a side-effect between Dest and the current
13918 // node.
13919 if (Dest.hasOneUse())
13920 return true;
13921 }
13922 // Next, try a deep search: check whether every operand of the TokenFactor
13923 // reaches Dest.
13924 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13925 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13926 });
13927 }
13928
13929 // Loads don't have side effects, look through them.
13930 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13931 if (Ld->isUnordered())
13932 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13933 }
13934 return false;
13935}
13936
13937bool SDNode::hasPredecessor(const SDNode *N) const {
13940 Worklist.push_back(this);
13941 return hasPredecessorHelper(N, Visited, Worklist);
13942}
13943
13945 this->Flags &= Flags;
13946}
13947
13948SDValue
13950 ArrayRef<ISD::NodeType> CandidateBinOps,
13951 bool AllowPartials) {
13952 // The pattern must end in an extract from index 0.
13953 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13954 !isNullConstant(Extract->getOperand(1)))
13955 return SDValue();
13956
13957 // Match against one of the candidate binary ops.
13958 SDValue Op = Extract->getOperand(0);
13959 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13960 return Op.getOpcode() == unsigned(BinOp);
13961 }))
13962 return SDValue();
13963
13964 // Floating-point reductions may require relaxed constraints on the final step
13965 // of the reduction because they may reorder intermediate operations.
13966 unsigned CandidateBinOp = Op.getOpcode();
13967 if (Op.getValueType().isFloatingPoint()) {
13968 SDNodeFlags Flags = Op->getFlags();
13969 switch (CandidateBinOp) {
13970 case ISD::FADD:
13971 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13972 return SDValue();
13973 break;
13974 default:
13975 llvm_unreachable("Unhandled FP opcode for binop reduction");
13976 }
13977 }
13978
13979 // Matching failed - attempt to see if we did enough stages that a partial
13980 // reduction from a subvector is possible.
13981 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13982 if (!AllowPartials || !Op)
13983 return SDValue();
13984 EVT OpVT = Op.getValueType();
13985 EVT OpSVT = OpVT.getScalarType();
13986 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13987 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13988 return SDValue();
13989 BinOp = (ISD::NodeType)CandidateBinOp;
13990 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13991 };
13992
13993 // At each stage, we're looking for something that looks like:
13994 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13995 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13996 // i32 undef, i32 undef, i32 undef, i32 undef>
13997 // %a = binop <8 x i32> %op, %s
13998 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13999 // we expect something like:
14000 // <4,5,6,7,u,u,u,u>
14001 // <2,3,u,u,u,u,u,u>
14002 // <1,u,u,u,u,u,u,u>
14003 // While a partial reduction match would be:
14004 // <2,3,u,u,u,u,u,u>
14005 // <1,u,u,u,u,u,u,u>
14006 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
14007 SDValue PrevOp;
14008 for (unsigned i = 0; i < Stages; ++i) {
14009 unsigned MaskEnd = (1 << i);
14010
14011 if (Op.getOpcode() != CandidateBinOp)
14012 return PartialReduction(PrevOp, MaskEnd);
14013
14014 SDValue Op0 = Op.getOperand(0);
14015 SDValue Op1 = Op.getOperand(1);
14016
14018 if (Shuffle) {
14019 Op = Op1;
14020 } else {
14021 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
14022 Op = Op0;
14023 }
14024
14025 // The first operand of the shuffle should be the same as the other operand
14026 // of the binop.
14027 if (!Shuffle || Shuffle->getOperand(0) != Op)
14028 return PartialReduction(PrevOp, MaskEnd);
14029
14030 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
14031 for (int Index = 0; Index < (int)MaskEnd; ++Index)
14032 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
14033 return PartialReduction(PrevOp, MaskEnd);
14034
14035 PrevOp = Op;
14036 }
14037
14038 // Handle subvector reductions, which tend to appear after the shuffle
14039 // reduction stages.
14040 while (Op.getOpcode() == CandidateBinOp) {
14041 unsigned NumElts = Op.getValueType().getVectorNumElements();
14042 SDValue Op0 = Op.getOperand(0);
14043 SDValue Op1 = Op.getOperand(1);
14044 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
14046 Op0.getOperand(0) != Op1.getOperand(0))
14047 break;
14048 SDValue Src = Op0.getOperand(0);
14049 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
14050 if (NumSrcElts != (2 * NumElts))
14051 break;
14052 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
14053 Op1.getConstantOperandAPInt(1) == NumElts) &&
14054 !(Op1.getConstantOperandAPInt(1) == 0 &&
14055 Op0.getConstantOperandAPInt(1) == NumElts))
14056 break;
14057 Op = Src;
14058 }
14059
14060 BinOp = (ISD::NodeType)CandidateBinOp;
14061 return Op;
14062}
14063
14065 EVT VT = N->getValueType(0);
14066 EVT EltVT = VT.getVectorElementType();
14067 unsigned NE = VT.getVectorNumElements();
14068
14069 SDLoc dl(N);
14070
14071 // If ResNE is 0, fully unroll the vector op.
14072 if (ResNE == 0)
14073 ResNE = NE;
14074 else if (NE > ResNE)
14075 NE = ResNE;
14076
14077 if (N->getNumValues() == 2) {
14078 SmallVector<SDValue, 8> Scalars0, Scalars1;
14079 SmallVector<SDValue, 4> Operands(N->getNumOperands());
14080 EVT VT1 = N->getValueType(1);
14081 EVT EltVT1 = VT1.getVectorElementType();
14082
14083 unsigned i;
14084 for (i = 0; i != NE; ++i) {
14085 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
14086 SDValue Operand = N->getOperand(j);
14087 EVT OperandVT = Operand.getValueType();
14088
14089 // A vector operand; extract a single element.
14090 EVT OperandEltVT = OperandVT.getVectorElementType();
14091 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
14092 }
14093
14094 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
14095 Scalars0.push_back(EltOp);
14096 Scalars1.push_back(EltOp.getValue(1));
14097 }
14098
14099 for (; i < ResNE; ++i) {
14100 Scalars0.push_back(getUNDEF(EltVT));
14101 Scalars1.push_back(getUNDEF(EltVT1));
14102 }
14103
14104 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
14105 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
14106 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
14107 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
14108 return getMergeValues({Vec0, Vec1}, dl);
14109 }
14110
14111 assert(N->getNumValues() == 1 &&
14112 "Can't unroll a vector with multiple results!");
14113
14115 SmallVector<SDValue, 4> Operands(N->getNumOperands());
14116
14117 unsigned i;
14118 for (i= 0; i != NE; ++i) {
14119 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
14120 SDValue Operand = N->getOperand(j);
14121 EVT OperandVT = Operand.getValueType();
14122 if (OperandVT.isVector()) {
14123 // A vector operand; extract a single element.
14124 EVT OperandEltVT = OperandVT.getVectorElementType();
14125 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
14126 } else {
14127 // A scalar operand; just use it as is.
14128 Operands[j] = Operand;
14129 }
14130 }
14131
14132 switch (N->getOpcode()) {
14133 default: {
14134 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
14135 N->getFlags()));
14136 break;
14137 }
14138 case ISD::VSELECT:
14139 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
14140 break;
14141 case ISD::SHL:
14142 case ISD::SRA:
14143 case ISD::SRL:
14144 case ISD::ROTL:
14145 case ISD::ROTR:
14146 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
14147 getShiftAmountOperand(Operands[0].getValueType(),
14148 Operands[1])));
14149 break;
14151 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
14152 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
14153 Operands[0],
14154 getValueType(ExtVT)));
14155 break;
14156 }
14157 case ISD::ADDRSPACECAST: {
14158 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
14159 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
14160 ASC->getSrcAddressSpace(),
14161 ASC->getDestAddressSpace()));
14162 break;
14163 }
14164 }
14165 }
14166
14167 for (; i < ResNE; ++i)
14168 Scalars.push_back(getUNDEF(EltVT));
14169
14170 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
14171 return getBuildVector(VecVT, dl, Scalars);
14172}
14173
14174std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
14175 SDNode *N, unsigned ResNE) {
14176 unsigned Opcode = N->getOpcode();
14177 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
14178 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
14179 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
14180 "Expected an overflow opcode");
14181
14182 EVT ResVT = N->getValueType(0);
14183 EVT OvVT = N->getValueType(1);
14184 EVT ResEltVT = ResVT.getVectorElementType();
14185 EVT OvEltVT = OvVT.getVectorElementType();
14186 SDLoc dl(N);
14187
14188 // If ResNE is 0, fully unroll the vector op.
14189 unsigned NE = ResVT.getVectorNumElements();
14190 if (ResNE == 0)
14191 ResNE = NE;
14192 else if (NE > ResNE)
14193 NE = ResNE;
14194
14195 SmallVector<SDValue, 8> LHSScalars;
14196 SmallVector<SDValue, 8> RHSScalars;
14197 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
14198 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
14199
14200 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
14201 SDVTList VTs = getVTList(ResEltVT, SVT);
14202 SmallVector<SDValue, 8> ResScalars;
14203 SmallVector<SDValue, 8> OvScalars;
14204 for (unsigned i = 0; i < NE; ++i) {
14205 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
14206 SDValue Ov =
14207 getSelect(dl, OvEltVT, Res.getValue(1),
14208 getBoolConstant(true, dl, OvEltVT, ResVT),
14209 getConstant(0, dl, OvEltVT));
14210
14211 ResScalars.push_back(Res);
14212 OvScalars.push_back(Ov);
14213 }
14214
14215 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
14216 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
14217
14218 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
14219 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
14220 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
14221 getBuildVector(NewOvVT, dl, OvScalars));
14222}
14223
14226 unsigned Bytes,
14227 int Dist) const {
14228 if (LD->isVolatile() || Base->isVolatile())
14229 return false;
14230 // TODO: probably too restrictive for atomics, revisit
14231 if (!LD->isSimple())
14232 return false;
14233 if (LD->isIndexed() || Base->isIndexed())
14234 return false;
14235 if (LD->getChain() != Base->getChain())
14236 return false;
14237 EVT VT = LD->getMemoryVT();
14238 if (VT.getSizeInBits() / 8 != Bytes)
14239 return false;
14240
14241 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
14242 auto LocDecomp = BaseIndexOffset::match(LD, *this);
14243
14244 int64_t Offset = 0;
14245 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
14246 return (Dist * (int64_t)Bytes == Offset);
14247 return false;
14248}
14249
14250/// InferPtrAlignment - Infer alignment of a load / store address. Return
14251/// std::nullopt if it cannot be inferred.
14253 // If this is a GlobalAddress + cst, return the alignment.
14254 const GlobalValue *GV = nullptr;
14255 int64_t GVOffset = 0;
14256 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
14257 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
14258 KnownBits Known(PtrWidth);
14260 unsigned AlignBits = Known.countMinTrailingZeros();
14261 if (AlignBits)
14262 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
14263 }
14264
14265 // If this is a direct reference to a stack slot, use information about the
14266 // stack slot's alignment.
14267 int FrameIdx = INT_MIN;
14268 int64_t FrameOffset = 0;
14270 FrameIdx = FI->getIndex();
14271 } else if (isBaseWithConstantOffset(Ptr) &&
14273 // Handle FI+Cst
14274 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
14275 FrameOffset = Ptr.getConstantOperandVal(1);
14276 }
14277
14278 if (FrameIdx != INT_MIN) {
14280 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
14281 }
14282
14283 return std::nullopt;
14284}
14285
14286/// Split the scalar node with EXTRACT_ELEMENT using the provided
14287/// VTs and return the low/high part.
14288std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
14289 const SDLoc &DL,
14290 const EVT &LoVT,
14291 const EVT &HiVT) {
14292 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
14293 "Split node must be a scalar type");
14294 SDValue Lo =
14296 SDValue Hi =
14298 return std::make_pair(Lo, Hi);
14299}
14300
14301/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
14302/// which is split (or expanded) into two not necessarily identical pieces.
14303std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
14304 // Currently all types are split in half.
14305 EVT LoVT, HiVT;
14306 if (!VT.isVector())
14307 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
14308 else
14309 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
14310
14311 return std::make_pair(LoVT, HiVT);
14312}
14313
14314/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
14315/// type, dependent on an enveloping VT that has been split into two identical
14316/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
14317std::pair<EVT, EVT>
14319 bool *HiIsEmpty) const {
14320 EVT EltTp = VT.getVectorElementType();
14321 // Examples:
14322 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
14323 // custom VL=9 with enveloping VL=8/8 yields 8/1
14324 // custom VL=10 with enveloping VL=8/8 yields 8/2
14325 // etc.
14326 ElementCount VTNumElts = VT.getVectorElementCount();
14327 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
14328 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
14329 "Mixing fixed width and scalable vectors when enveloping a type");
14330 EVT LoVT, HiVT;
14331 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
14332 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
14333 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
14334 *HiIsEmpty = false;
14335 } else {
14336 // Flag that hi type has zero storage size, but return split envelop type
14337 // (this would be easier if vector types with zero elements were allowed).
14338 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
14339 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
14340 *HiIsEmpty = true;
14341 }
14342 return std::make_pair(LoVT, HiVT);
14343}
14344
14345/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
14346/// low/high part.
14347std::pair<SDValue, SDValue>
14348SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
14349 const EVT &HiVT) {
14350 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
14351 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
14352 "Splitting vector with an invalid mixture of fixed and scalable "
14353 "vector types");
14355 N.getValueType().getVectorMinNumElements() &&
14356 "More vector elements requested than available!");
14357 SDValue Lo, Hi;
14358 Lo = getExtractSubvector(DL, LoVT, N, 0);
14359 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
14360 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
14361 // IDX with the runtime scaling factor of the result vector type. For
14362 // fixed-width result vectors, that runtime scaling factor is 1.
14365 return std::make_pair(Lo, Hi);
14366}
14367
14368std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
14369 const SDLoc &DL) {
14370 // Split the vector length parameter.
14371 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
14372 EVT VT = N.getValueType();
14374 "Expecting the mask to be an evenly-sized vector");
14375 SDValue HalfNumElts = getElementCount(
14377 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
14378 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
14379 return std::make_pair(Lo, Hi);
14380}
14381
14382/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
14384 EVT VT = N.getValueType();
14387 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
14388}
14389
14392 unsigned Start, unsigned Count,
14393 EVT EltVT) {
14394 EVT VT = Op.getValueType();
14395 if (Count == 0)
14397 if (EltVT == EVT())
14398 EltVT = VT.getVectorElementType();
14399 SDLoc SL(Op);
14400 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
14401 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
14402 }
14403}
14404
14405// getAddressSpace - Return the address space this GlobalAddress belongs to.
14407 return getGlobal()->getType()->getAddressSpace();
14408}
14409
14412 return Val.MachineCPVal->getType();
14413 return Val.ConstVal->getType();
14414}
14415
14416bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
14417 unsigned &SplatBitSize,
14418 bool &HasAnyUndefs,
14419 unsigned MinSplatBits,
14420 bool IsBigEndian) const {
14421 EVT VT = getValueType(0);
14422 assert(VT.isVector() && "Expected a vector type");
14423 unsigned VecWidth = VT.getSizeInBits();
14424 if (MinSplatBits > VecWidth)
14425 return false;
14426
14427 // FIXME: The widths are based on this node's type, but build vectors can
14428 // truncate their operands.
14429 SplatValue = APInt(VecWidth, 0);
14430 SplatUndef = APInt(VecWidth, 0);
14431
14432 // Get the bits. Bits with undefined values (when the corresponding element
14433 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
14434 // in SplatValue. If any of the values are not constant, give up and return
14435 // false.
14436 unsigned int NumOps = getNumOperands();
14437 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
14438 unsigned EltWidth = VT.getScalarSizeInBits();
14439
14440 for (unsigned j = 0; j < NumOps; ++j) {
14441 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
14442 SDValue OpVal = getOperand(i);
14443 unsigned BitPos = j * EltWidth;
14444
14445 if (OpVal.isUndef())
14446 SplatUndef.setBits(BitPos, BitPos + EltWidth);
14447 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
14448 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
14449 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
14450 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
14451 else
14452 return false;
14453 }
14454
14455 // The build_vector is all constants or undefs. Find the smallest element
14456 // size that splats the vector.
14457 HasAnyUndefs = (SplatUndef != 0);
14458
14459 // FIXME: This does not work for vectors with elements less than 8 bits.
14460 while (VecWidth > 8) {
14461 // If we can't split in half, stop here.
14462 if (VecWidth & 1)
14463 break;
14464
14465 unsigned HalfSize = VecWidth / 2;
14466 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
14467 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
14468 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
14469 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
14470
14471 // If the two halves do not match (ignoring undef bits), stop here.
14472 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
14473 MinSplatBits > HalfSize)
14474 break;
14475
14476 SplatValue = HighValue | LowValue;
14477 SplatUndef = HighUndef & LowUndef;
14478
14479 VecWidth = HalfSize;
14480 }
14481
14482 // FIXME: The loop above only tries to split in halves. But if the input
14483 // vector for example is <3 x i16> it wouldn't be able to detect a
14484 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
14485 // optimizations. I guess that back in the days when this helper was created
14486 // vectors normally was power-of-2 sized.
14487
14488 SplatBitSize = VecWidth;
14489 return true;
14490}
14491
14493 BitVector *UndefElements) const {
14494 unsigned NumOps = getNumOperands();
14495 if (UndefElements) {
14496 UndefElements->clear();
14497 UndefElements->resize(NumOps);
14498 }
14499 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
14500 if (!DemandedElts)
14501 return SDValue();
14502 SDValue Splatted;
14503 for (unsigned i = 0; i != NumOps; ++i) {
14504 if (!DemandedElts[i])
14505 continue;
14506 SDValue Op = getOperand(i);
14507 if (Op.isUndef()) {
14508 if (UndefElements)
14509 (*UndefElements)[i] = true;
14510 } else if (!Splatted) {
14511 Splatted = Op;
14512 } else if (Splatted != Op) {
14513 return SDValue();
14514 }
14515 }
14516
14517 if (!Splatted) {
14518 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
14519 assert(getOperand(FirstDemandedIdx).isUndef() &&
14520 "Can only have a splat without a constant for all undefs.");
14521 return getOperand(FirstDemandedIdx);
14522 }
14523
14524 return Splatted;
14525}
14526
14528 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
14529 return getSplatValue(DemandedElts, UndefElements);
14530}
14531
14533 SmallVectorImpl<SDValue> &Sequence,
14534 BitVector *UndefElements) const {
14535 unsigned NumOps = getNumOperands();
14536 Sequence.clear();
14537 if (UndefElements) {
14538 UndefElements->clear();
14539 UndefElements->resize(NumOps);
14540 }
14541 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
14542 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
14543 return false;
14544
14545 // Set the undefs even if we don't find a sequence (like getSplatValue).
14546 if (UndefElements)
14547 for (unsigned I = 0; I != NumOps; ++I)
14548 if (DemandedElts[I] && getOperand(I).isUndef())
14549 (*UndefElements)[I] = true;
14550
14551 // Iteratively widen the sequence length looking for repetitions.
14552 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
14553 Sequence.append(SeqLen, SDValue());
14554 for (unsigned I = 0; I != NumOps; ++I) {
14555 if (!DemandedElts[I])
14556 continue;
14557 SDValue &SeqOp = Sequence[I % SeqLen];
14559 if (Op.isUndef()) {
14560 if (!SeqOp)
14561 SeqOp = Op;
14562 continue;
14563 }
14564 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
14565 Sequence.clear();
14566 break;
14567 }
14568 SeqOp = Op;
14569 }
14570 if (!Sequence.empty())
14571 return true;
14572 }
14573
14574 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
14575 return false;
14576}
14577
14579 BitVector *UndefElements) const {
14580 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
14581 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
14582}
14583
14586 BitVector *UndefElements) const {
14588 getSplatValue(DemandedElts, UndefElements));
14589}
14590
14593 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
14594}
14595
14598 BitVector *UndefElements) const {
14600 getSplatValue(DemandedElts, UndefElements));
14601}
14602
14607
14608int32_t
14610 uint32_t BitWidth) const {
14611 if (ConstantFPSDNode *CN =
14613 bool IsExact;
14614 APSInt IntVal(BitWidth);
14615 const APFloat &APF = CN->getValueAPF();
14616 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
14617 APFloat::opOK ||
14618 !IsExact)
14619 return -1;
14620
14621 return IntVal.exactLogBase2();
14622 }
14623 return -1;
14624}
14625
14627 bool IsLittleEndian, unsigned DstEltSizeInBits,
14628 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
14629 // Early-out if this contains anything but Undef/Constant/ConstantFP.
14630 if (!isConstant())
14631 return false;
14632
14633 unsigned NumSrcOps = getNumOperands();
14634 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
14635 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14636 "Invalid bitcast scale");
14637
14638 // Extract raw src bits.
14639 SmallVector<APInt> SrcBitElements(NumSrcOps,
14640 APInt::getZero(SrcEltSizeInBits));
14641 BitVector SrcUndeElements(NumSrcOps, false);
14642
14643 for (unsigned I = 0; I != NumSrcOps; ++I) {
14645 if (Op.isUndef()) {
14646 SrcUndeElements.set(I);
14647 continue;
14648 }
14649 auto *CInt = dyn_cast<ConstantSDNode>(Op);
14650 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
14651 assert((CInt || CFP) && "Unknown constant");
14652 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
14653 : CFP->getValueAPF().bitcastToAPInt();
14654 }
14655
14656 // Recast to dst width.
14657 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
14658 SrcBitElements, UndefElements, SrcUndeElements);
14659 return true;
14660}
14661
14662void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
14663 unsigned DstEltSizeInBits,
14664 SmallVectorImpl<APInt> &DstBitElements,
14665 ArrayRef<APInt> SrcBitElements,
14666 BitVector &DstUndefElements,
14667 const BitVector &SrcUndefElements) {
14668 unsigned NumSrcOps = SrcBitElements.size();
14669 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
14670 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14671 "Invalid bitcast scale");
14672 assert(NumSrcOps == SrcUndefElements.size() &&
14673 "Vector size mismatch");
14674
14675 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
14676 DstUndefElements.clear();
14677 DstUndefElements.resize(NumDstOps, false);
14678 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
14679
14680 // Concatenate src elements constant bits together into dst element.
14681 if (SrcEltSizeInBits <= DstEltSizeInBits) {
14682 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
14683 for (unsigned I = 0; I != NumDstOps; ++I) {
14684 DstUndefElements.set(I);
14685 APInt &DstBits = DstBitElements[I];
14686 for (unsigned J = 0; J != Scale; ++J) {
14687 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14688 if (SrcUndefElements[Idx])
14689 continue;
14690 DstUndefElements.reset(I);
14691 const APInt &SrcBits = SrcBitElements[Idx];
14692 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
14693 "Illegal constant bitwidths");
14694 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
14695 }
14696 }
14697 return;
14698 }
14699
14700 // Split src element constant bits into dst elements.
14701 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
14702 for (unsigned I = 0; I != NumSrcOps; ++I) {
14703 if (SrcUndefElements[I]) {
14704 DstUndefElements.set(I * Scale, (I + 1) * Scale);
14705 continue;
14706 }
14707 const APInt &SrcBits = SrcBitElements[I];
14708 for (unsigned J = 0; J != Scale; ++J) {
14709 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14710 APInt &DstBits = DstBitElements[Idx];
14711 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
14712 }
14713 }
14714}
14715
14717 for (const SDValue &Op : op_values()) {
14718 unsigned Opc = Op.getOpcode();
14719 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
14720 return false;
14721 }
14722 return true;
14723}
14724
14725std::optional<std::pair<APInt, APInt>>
14727 unsigned NumOps = getNumOperands();
14728 if (NumOps < 2)
14729 return std::nullopt;
14730
14731 unsigned EltSize = getValueType(0).getScalarSizeInBits();
14732 APInt Start, Stride;
14733 int FirstIdx = -1, SecondIdx = -1;
14734
14735 // Find the first two non-undef constant elements to determine Start and
14736 // Stride, then verify all remaining elements match the sequence.
14737 for (unsigned I = 0; I < NumOps; ++I) {
14739 if (Op->isUndef())
14740 continue;
14741 if (!isa<ConstantSDNode>(Op))
14742 return std::nullopt;
14743
14744 APInt Val = getConstantOperandAPInt(I).trunc(EltSize);
14745 if (FirstIdx < 0) {
14746 FirstIdx = I;
14747 Start = Val;
14748 } else if (SecondIdx < 0) {
14749 SecondIdx = I;
14750 // Compute stride using modular arithmetic. Simple division would handle
14751 // common strides (1, 2, -1, etc.), but modular inverse maximizes matches.
14752 // Example: <0, poison, poison, 0xFF> has stride 0x55 since 3*0x55 = 0xFF
14753 // Note that modular arithmetic is agnostic to signed/unsigned.
14754 unsigned IdxDiff = I - FirstIdx;
14755 APInt ValDiff = Val - Start;
14756
14757 // Step 1: Factor out common powers of 2 from IdxDiff and ValDiff.
14758 unsigned CommonPow2Bits = llvm::countr_zero(IdxDiff);
14759 if (ValDiff.countr_zero() < CommonPow2Bits)
14760 return std::nullopt; // ValDiff not divisible by 2^CommonPow2Bits
14761 IdxDiff >>= CommonPow2Bits;
14762 ValDiff.lshrInPlace(CommonPow2Bits);
14763
14764 // Step 2: IdxDiff is now odd, so its inverse mod 2^EltSize exists.
14765 // TODO: There are 2^CommonPow2Bits valid strides; currently we only try
14766 // one, but we could try all candidates to handle more cases.
14767 Stride = ValDiff * APInt(EltSize, IdxDiff).multiplicativeInverse();
14768 if (Stride.isZero())
14769 return std::nullopt;
14770
14771 // Step 3: Adjust Start based on the first defined element's index.
14772 Start -= Stride * FirstIdx;
14773 } else {
14774 // Verify this element matches the sequence.
14775 if (Val != Start + Stride * I)
14776 return std::nullopt;
14777 }
14778 }
14779
14780 // Need at least two defined elements.
14781 if (SecondIdx < 0)
14782 return std::nullopt;
14783
14784 return std::make_pair(Start, Stride);
14785}
14786
14788 // Find the first non-undef value in the shuffle mask.
14789 unsigned i, e;
14790 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
14791 /* search */;
14792
14793 // If all elements are undefined, this shuffle can be considered a splat
14794 // (although it should eventually get simplified away completely).
14795 if (i == e)
14796 return true;
14797
14798 // Make sure all remaining elements are either undef or the same as the first
14799 // non-undef value.
14800 for (int Idx = Mask[i]; i != e; ++i)
14801 if (Mask[i] >= 0 && Mask[i] != Idx)
14802 return false;
14803 return true;
14804}
14805
14806// Returns true if it is a constant integer BuildVector or constant integer,
14807// possibly hidden by a bitcast.
14809 SDValue N, bool AllowOpaques) const {
14811
14812 if (auto *C = dyn_cast<ConstantSDNode>(N))
14813 return AllowOpaques || !C->isOpaque();
14814
14816 return true;
14817
14818 // Treat a GlobalAddress supporting constant offset folding as a
14819 // constant integer.
14820 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14821 if (GA->getOpcode() == ISD::GlobalAddress &&
14822 TLI->isOffsetFoldingLegal(GA))
14823 return true;
14824
14825 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14826 isa<ConstantSDNode>(N.getOperand(0)))
14827 return true;
14828 return false;
14829}
14830
14831// Returns true if it is a constant float BuildVector or constant float.
14834 return true;
14835
14837 return true;
14838
14839 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14840 isa<ConstantFPSDNode>(N.getOperand(0)))
14841 return true;
14842
14843 return false;
14844}
14845
14846std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14847 ConstantSDNode *Const =
14848 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14849 if (!Const)
14850 return std::nullopt;
14851
14852 EVT VT = N->getValueType(0);
14853 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14854 switch (TLI->getBooleanContents(N.getValueType())) {
14856 if (CVal.isOne())
14857 return true;
14858 if (CVal.isZero())
14859 return false;
14860 return std::nullopt;
14862 if (CVal.isAllOnes())
14863 return true;
14864 if (CVal.isZero())
14865 return false;
14866 return std::nullopt;
14868 return CVal[0];
14869 }
14870 llvm_unreachable("Unknown BooleanContent enum");
14871}
14872
14873void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14874 assert(!Node->OperandList && "Node already has operands");
14876 "too many operands to fit into SDNode");
14877 SDUse *Ops = OperandRecycler.allocate(
14878 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14879
14880 bool IsDivergent = false;
14881 for (unsigned I = 0; I != Vals.size(); ++I) {
14882 Ops[I].setUser(Node);
14883 Ops[I].setInitial(Vals[I]);
14884 EVT VT = Ops[I].getValueType();
14885
14886 // Skip Chain. It does not carry divergence.
14887 if (VT != MVT::Other &&
14888 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14889 Ops[I].getNode()->isDivergent()) {
14890 IsDivergent = true;
14891 }
14892 }
14893 Node->NumOperands = Vals.size();
14894 Node->OperandList = Ops;
14895 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14896 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14897 Node->SDNodeBits.IsDivergent = IsDivergent;
14898 }
14899 checkForCycles(Node);
14900}
14901
14904 size_t Limit = SDNode::getMaxNumOperands();
14905 while (Vals.size() > Limit) {
14906 unsigned SliceIdx = Vals.size() - Limit;
14907 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14908 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14909 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14910 Vals.emplace_back(NewTF);
14911 }
14912 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14913}
14914
14916 EVT VT, SDNodeFlags Flags) {
14917 switch (Opcode) {
14918 default:
14919 return SDValue();
14920 case ISD::ADD:
14921 case ISD::OR:
14922 case ISD::XOR:
14923 case ISD::UMAX:
14924 return getConstant(0, DL, VT);
14925 case ISD::MUL:
14926 return getConstant(1, DL, VT);
14927 case ISD::AND:
14928 case ISD::UMIN:
14929 return getAllOnesConstant(DL, VT);
14930 case ISD::SMAX:
14932 case ISD::SMIN:
14934 case ISD::FADD:
14935 // If flags allow, prefer positive zero since it's generally cheaper
14936 // to materialize on most targets.
14937 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14938 case ISD::FMUL:
14939 return getConstantFP(1.0, DL, VT);
14940 case ISD::FMINNUM:
14941 case ISD::FMAXNUM: {
14942 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14943 const fltSemantics &Semantics = VT.getFltSemantics();
14944 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14945 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14946 APFloat::getLargest(Semantics);
14947 if (Opcode == ISD::FMAXNUM)
14948 NeutralAF.changeSign();
14949
14950 return getConstantFP(NeutralAF, DL, VT);
14951 }
14952 case ISD::FMINIMUM:
14953 case ISD::FMAXIMUM: {
14954 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14955 const fltSemantics &Semantics = VT.getFltSemantics();
14956 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14957 : APFloat::getLargest(Semantics);
14958 if (Opcode == ISD::FMAXIMUM)
14959 NeutralAF.changeSign();
14960
14961 return getConstantFP(NeutralAF, DL, VT);
14962 }
14963
14964 }
14965}
14966
14968 SDValue Acc, SDValue LHS,
14969 SDValue RHS) {
14970 EVT AccVT = Acc.getValueType();
14971 if (AccVT.isFloatingPoint()) {
14972 assert(Opc == ISD::PARTIAL_REDUCE_FMLA && "Unexpected opcode");
14973 SDValue NegRHS = getNode(ISD::FNEG, DL, RHS.getValueType(), RHS);
14974 return getNode(Opc, DL, AccVT, Acc, LHS, NegRHS);
14975 }
14977 "Unexpected opcode");
14978 SDValue NegAcc = getNegative(Acc, DL, AccVT);
14979 SDValue MLA = getNode(Opc, DL, AccVT, NegAcc, LHS, RHS);
14980 return getNegative(MLA, DL, AccVT);
14981}
14982
14983/// Helper used to make a call to a library function that has one argument of
14984/// pointer type.
14985///
14986/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14987/// used to get or set floating-point state. They have one argument of pointer
14988/// type, which points to the memory region containing bits of the
14989/// floating-point state. The value returned by such function is ignored in the
14990/// created call.
14991///
14992/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14993/// \param Ptr Pointer used to save/load state.
14994/// \param InChain Ingoing token chain.
14995/// \returns Outgoing chain token.
14997 SDValue InChain,
14998 const SDLoc &DLoc) {
14999 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
15001 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
15002 RTLIB::LibcallImpl LibcallImpl =
15003 Libcalls->getLibcallImpl(static_cast<RTLIB::Libcall>(LibFunc));
15004 if (LibcallImpl == RTLIB::Unsupported)
15005 reportFatalUsageError("emitting call to unsupported libcall");
15006
15007 SDValue Callee =
15008 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout()));
15010 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
15011 Libcalls->getLibcallImplCallingConv(LibcallImpl),
15012 Type::getVoidTy(*getContext()), Callee, std::move(Args));
15013 return TLI->LowerCallTo(CLI).second;
15014}
15015
15017 assert(From && To && "Invalid SDNode; empty source SDValue?");
15018 auto I = SDEI.find(From);
15019 if (I == SDEI.end())
15020 return;
15021
15022 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
15023 // the iterator, hence the need to make a copy to prevent a use-after-free.
15024 NodeExtraInfo NEI = I->second;
15025 if (LLVM_LIKELY(!NEI.PCSections)) {
15026 // No deep copy required for the types of extra info set.
15027 //
15028 // FIXME: Investigate if other types of extra info also need deep copy. This
15029 // depends on the types of nodes they can be attached to: if some extra info
15030 // is only ever attached to nodes where a replacement To node is always the
15031 // node where later use and propagation of the extra info has the intended
15032 // semantics, no deep copy is required.
15033 SDEI[To] = std::move(NEI);
15034 return;
15035 }
15036
15037 const SDNode *EntrySDN = getEntryNode().getNode();
15038
15039 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
15040 // through the replacement of From with To. Otherwise, replacements of a node
15041 // (From) with more complex nodes (To and its operands) may result in lost
15042 // extra info where the root node (To) is insignificant in further propagating
15043 // and using extra info when further lowering to MIR.
15044 //
15045 // In the first step pre-populate the visited set with the nodes reachable
15046 // from the old From node. This avoids copying NodeExtraInfo to parts of the
15047 // DAG that is not new and should be left untouched.
15048 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
15049 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
15050 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
15051 if (MaxDepth == 0) {
15052 // Remember this node in case we need to increase MaxDepth and continue
15053 // populating FromReach from this node.
15054 Leafs.emplace_back(N);
15055 return;
15056 }
15057 if (!FromReach.insert(N).second)
15058 return;
15059 for (const SDValue &Op : N->op_values())
15060 Self(Self, Op.getNode(), MaxDepth - 1);
15061 };
15062
15063 // Copy extra info to To and all its transitive operands (that are new).
15065 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
15066 if (FromReach.contains(N))
15067 return true;
15068 if (!Visited.insert(N).second)
15069 return true;
15070 if (EntrySDN == N)
15071 return false;
15072 for (const SDValue &Op : N->op_values()) {
15073 if (N == To && Op.getNode() == EntrySDN) {
15074 // Special case: New node's operand is the entry node; just need to
15075 // copy extra info to new node.
15076 break;
15077 }
15078 if (!Self(Self, Op.getNode()))
15079 return false;
15080 }
15081 // Copy only if entry node was not reached.
15082 SDEI[N] = std::move(NEI);
15083 return true;
15084 };
15085
15086 // We first try with a lower MaxDepth, assuming that the path to common
15087 // operands between From and To is relatively short. This significantly
15088 // improves performance in the common case. The initial MaxDepth is big
15089 // enough to avoid retry in the common case; the last MaxDepth is large
15090 // enough to avoid having to use the fallback below (and protects from
15091 // potential stack exhaustion from recursion).
15092 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
15093 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
15094 // StartFrom is the previous (or initial) set of leafs reachable at the
15095 // previous maximum depth.
15097 std::swap(StartFrom, Leafs);
15098 for (const SDNode *N : StartFrom)
15099 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
15100 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
15101 return;
15102 // This should happen very rarely (reached the entry node).
15103 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
15104 assert(!Leafs.empty());
15105 }
15106
15107 // This should not happen - but if it did, that means the subgraph reachable
15108 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
15109 // could not visit all reachable common operands. Consequently, we were able
15110 // to reach the entry node.
15111 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
15112 assert(false && "From subgraph too complex - increase max. MaxDepth?");
15113 // Best-effort fallback if assertions disabled.
15114 SDEI[To] = std::move(NEI);
15115}
15116
15117#ifndef NDEBUG
15118static void checkForCyclesHelper(const SDNode *N,
15121 const llvm::SelectionDAG *DAG) {
15122 // If this node has already been checked, don't check it again.
15123 if (Checked.count(N))
15124 return;
15125
15126 // If a node has already been visited on this depth-first walk, reject it as
15127 // a cycle.
15128 if (!Visited.insert(N).second) {
15129 errs() << "Detected cycle in SelectionDAG\n";
15130 dbgs() << "Offending node:\n";
15131 N->dumprFull(DAG); dbgs() << "\n";
15132 abort();
15133 }
15134
15135 for (const SDValue &Op : N->op_values())
15136 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
15137
15138 Checked.insert(N);
15139 Visited.erase(N);
15140}
15141#endif
15142
15144 const llvm::SelectionDAG *DAG,
15145 bool force) {
15146#ifndef NDEBUG
15147 bool check = force;
15148#ifdef EXPENSIVE_CHECKS
15149 check = true;
15150#endif // EXPENSIVE_CHECKS
15151 if (check) {
15152 assert(N && "Checking nonexistent SDNode");
15155 checkForCyclesHelper(N, visited, checked, DAG);
15156 }
15157#endif // !NDEBUG
15158}
15159
15160void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
15161 checkForCycles(DAG->getRoot().getNode(), DAG, force);
15162}
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:851
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:592
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:335
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.
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.
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 shouldLowerMemFuncForSize(const MachineFunction &MF)
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 SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo, BatchAAResults *BatchAA)
static SDValue 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 getMemmoveLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo)
static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC)
AddNodeIDOpcode - Add the node opcode to the NodeID data.
static ISD::CondCode getSetCCInverseImpl(ISD::CondCode Op, bool isIntegerLike)
static bool doNotCSE(SDNode *N)
doNotCSE - Return true if CSE should not be performed for this node.
static cl::opt< int > MaxLdStGlue("ldstmemcpy-glue-max", cl::desc("Number limit for gluing ld/st of memcpy."), cl::Hidden, cl::init(0))
static void AddNodeIDOperands(FoldingSetNodeID &ID, ArrayRef< SDValue > Ops)
AddNodeIDOperands - Various routines for adding operands to the NodeID data.
static 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.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
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:296
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition APFloat.h:334
static constexpr roundingMode rmTowardZero
Definition APFloat.h:348
static const fltSemantics & BFloat()
Definition APFloat.h:295
static const fltSemantics & IEEEquad()
Definition APFloat.h:298
static const fltSemantics & IEEEdouble()
Definition APFloat.h:297
static constexpr roundingMode rmTowardNegative
Definition APFloat.h:347
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:344
static constexpr roundingMode rmTowardPositive
Definition APFloat.h:346
static const fltSemantics & IEEEhalf()
Definition APFloat.h:294
opStatus
IEEE-754R 7: Default exception handling.
Definition APFloat.h:360
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition APFloat.h:1175
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1263
void copySign(const APFloat &RHS)
Definition APFloat.h:1357
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:5890
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1245
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
Definition APFloat.h:1517
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1236
bool isFinite() const
Definition APFloat.h:1539
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition APFloat.h:1402
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1254
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition APFloat.h:1290
bool isZero() const
Definition APFloat.h:1530
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1193
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.h:1387
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1153
opStatus mod(const APFloat &RHS)
Definition APFloat.h:1281
bool isPosZero() const
Definition APFloat.h:1545
bool isNegZero() const
Definition APFloat.h:1546
void changeSign()
Definition APFloat.h:1352
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition APFloat.h:1164
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:2022
LLVM_ABI APInt usub_sat(const APInt &RHS) const
Definition APInt.cpp:2106
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1615
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:1054
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:230
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:639
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1075
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:967
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:2077
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:1708
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:1686
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:1196
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:789
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:651
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:2137
LLVM_ABI APInt ushl_sat(const APInt &RHS) const
Definition APInt.cpp:2151
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1083
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:1183
LLVM_ABI void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition APInt.cpp:397
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition APInt.h:1458
unsigned logBase2() const
Definition APInt.h:1784
LLVM_ABI APInt uadd_sat(const APInt &RHS) const
Definition APInt.cpp:2087
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:834
LLVM_ABI APInt multiplicativeInverse() const
Definition APInt.cpp:1316
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1787
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:1027
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:767
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:482
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:2096
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:39
size_t size() const
Get the array size.
Definition ArrayRef.h:140
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:135
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:1071
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.
LLVM_ABI ConstantRange multiply(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
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 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:123
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
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:711
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:354
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.
LLVM_ABI CallingConv::ID getLibcallImplCallingConv(RTLIB::LibcallImpl Call) const
Get the CallingConv that should be used for the specified libcall.
LLVM_ABI 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:1080
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1444
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:293
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 SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
LLVM_ABI bool shouldOptForSize() const
LLVM_ABI SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
LLVM_ABI SDValue getVPZExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be an integer vector, to the vector-type VT,...
const TargetLowering & getTargetLoweringInfo() const
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
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 bool isGuaranteedNotToBeUndefOrPoison(SDValue Op, bool PoisonOnly=false, unsigned Depth=0) const
Return true if this function can prove that Op is never poison and, if PoisonOnly is false,...
LLVM_ABI SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
LLVM_ABI SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
LLVM_ABI SDValue getIndexedLoadVP(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getSrcValue(const Value *v)
Construct a node to track a Value* through the backend.
SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op)
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 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 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 Alignment, bool isVol, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI SDValue getJumpTable(int JTI, EVT VT, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side,...
LLVM_ABI SDValue getVPPtrExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be of integer type, to the vector-type integer type VT,...
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI void 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 bool canCreateUndefOrPoison(SDValue Op, const APInt &DemandedElts, bool PoisonOnly=false, bool ConsiderFlags=true, unsigned Depth=0) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
LLVM_ABI OverflowKind computeOverflowForUnsignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the unsigned addition of 2 nodes can overflow.
SDValue getPOISON(EVT VT)
Return a POISON node. POISON does not have a useful SDLoc.
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
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:55
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:137
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:644
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:290
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:313
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:286
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:311
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:201
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:236
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:35
LLVM_ABI void set(Value *Val)
Definition Value.h:883
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:202
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:175
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:3252
LLVM_ABI APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition APInt.cpp:3182
LLVM_ABI APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition APInt.cpp:3169
LLVM_ABI APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition APInt.cpp:3159
LLVM_ABI APInt fshr(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift right.
Definition APInt.cpp:3233
LLVM_ABI APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition APInt.cpp:3174
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:3242
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:3224
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:3060
LLVM_ABI APInt clmulh(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, and return high-bits.
Definition APInt.cpp:3257
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:3154
LLVM_ABI APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition APInt.cpp:3164
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:819
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:261
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:788
@ 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:779
@ 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:853
@ 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:880
@ 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:910
@ 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...
Definition ISDOpcodes.h:993
@ 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:774
@ 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
@ PARTIAL_REDUCE_UMLA
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:844
@ 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.
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:787
@ 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:873
@ 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:827
@ 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:796
@ 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:792
@ 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:765
@ 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:850
@ 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:811
@ 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:899
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:888
@ 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:978
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:805
@ 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
@ 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:926
@ 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,...
@ 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:959
@ 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:921
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition ISDOpcodes.h:997
@ 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:945
@ VECREDUCE_FMINIMUM
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:856
@ 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:833
@ 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:871
@ 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:875
@ 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
@ 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:557
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:360
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:1758
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:1738
MaybeAlign getAlign(const CallInst &I, unsigned Index)
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
LLVM_ABI bool isAllOnesOrAllOnesSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant -1 integer or a splatted vector of a constant -1 integer (with...
Definition Utils.cpp:1565
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:2553
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:1728
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2207
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:1547
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:1640
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:1745
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:1683
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:1635
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition APFloat.h:1714
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI SDValue peekThroughTruncates(SDValue V)
Return the non-truncated source operand of V if it exists.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1752
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.
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
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
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
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 minNum semantics.
Definition APFloat.h:1664
@ Mul
Product of integers.
@ Sub
Subtraction of integers.
LLVM_ABI bool isNullConstantOrUndef(SDValue V)
Returns true if V is a constant integer zero or an UNDEF node.
bool isInTailCallPosition(const CallBase &Call, const TargetMachine &TM, bool ReturnsFirstArg=false)
Test if the given instruction is in a position to be optimized with a tail-call.
Definition Analysis.cpp: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:1884
constexpr unsigned BitWidth
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.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1946
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:1701
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1741
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:876
#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:403
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:528
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:292
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:308
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:358
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:381
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:367
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:393
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:324
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:189
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:331
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:300
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition ValueTypes.h:264
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:182
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:336
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:344
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition ValueTypes.h:316
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition ValueTypes.h:469
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:553
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:557
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
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 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 avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
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
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)