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 CSEMap.InsertNode(N, IP);
1843 InsertNode(N);
1844 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1845 }
1846
1847 SDValue Result(N, 0);
1848 if (VT.isVector())
1849 Result = getSplat(VT, DL, Result);
1850 return Result;
1851}
1852
1854 bool isT, bool isO) {
1855 unsigned Size = VT.getScalarSizeInBits();
1856 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1857}
1858
1860 bool IsOpaque) {
1862 IsTarget, IsOpaque);
1863}
1864
1866 bool isTarget) {
1867 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1868}
1869
1871 const SDLoc &DL) {
1872 assert(VT.isInteger() && "Shift amount is not an integer type!");
1873 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1874 return getConstant(Val, DL, ShiftVT);
1875}
1876
1878 const SDLoc &DL) {
1879 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1880 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1881}
1882
1884 bool isTarget) {
1885 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1886}
1887
1889 bool isTarget) {
1890 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1891}
1892
1894 EVT VT, bool isTarget) {
1895 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1896
1897 EVT EltVT = VT.getScalarType();
1898 const ConstantFP *Elt = &V;
1899
1900 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1901 // the to-be-splatted scalar ConstantFP.
1902 if (isa<VectorType>(Elt->getType()))
1903 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1904
1905 // Do the map lookup using the actual bit pattern for the floating point
1906 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1907 // we don't have issues with SNANs.
1908 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1909 SDVTList VTs = getVTList(EltVT);
1911 AddNodeIDNode(ID, Opc, VTs, {});
1912 ID.AddPointer(Elt);
1913 void *IP = nullptr;
1914 SDNode *N = nullptr;
1915 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1916 if (!VT.isVector())
1917 return SDValue(N, 0);
1918
1919 if (!N) {
1920 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1921 CSEMap.InsertNode(N, IP);
1922 InsertNode(N);
1923 }
1924
1925 SDValue Result(N, 0);
1926 if (VT.isVector())
1927 Result = getSplat(VT, DL, Result);
1928 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1929 return Result;
1930}
1931
1933 bool isTarget) {
1934 EVT EltVT = VT.getScalarType();
1935 if (EltVT == MVT::f32)
1936 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1937 if (EltVT == MVT::f64)
1938 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1939 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1940 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1941 bool Ignored;
1942 APFloat APF = APFloat(Val);
1944 &Ignored);
1945 return getConstantFP(APF, DL, VT, isTarget);
1946 }
1947 llvm_unreachable("Unsupported type in getConstantFP");
1948}
1949
1951 EVT VT, int64_t Offset, bool isTargetGA,
1952 unsigned TargetFlags) {
1953 assert((TargetFlags == 0 || isTargetGA) &&
1954 "Cannot set target flags on target-independent globals");
1955
1956 // Truncate (with sign-extension) the offset value to the pointer size.
1958 if (BitWidth < 64)
1960
1961 unsigned Opc;
1962 if (GV->isThreadLocal())
1964 else
1966
1967 SDVTList VTs = getVTList(VT);
1969 AddNodeIDNode(ID, Opc, VTs, {});
1970 ID.AddPointer(GV);
1971 ID.AddInteger(Offset);
1972 ID.AddInteger(TargetFlags);
1973 void *IP = nullptr;
1974 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1975 return SDValue(E, 0);
1976
1977 auto *N = newSDNode<GlobalAddressSDNode>(
1978 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1979 CSEMap.InsertNode(N, IP);
1980 InsertNode(N);
1981 return SDValue(N, 0);
1982}
1983
1985 SDVTList VTs = getVTList(MVT::Untyped);
1988 ID.AddPointer(GV);
1989 void *IP = nullptr;
1990 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP))
1991 return SDValue(E, 0);
1992
1993 auto *N = newSDNode<DeactivationSymbolSDNode>(GV, VTs);
1994 CSEMap.InsertNode(N, IP);
1995 InsertNode(N);
1996 return SDValue(N, 0);
1997}
1998
1999SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
2000 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
2001 SDVTList VTs = getVTList(VT);
2003 AddNodeIDNode(ID, Opc, VTs, {});
2004 ID.AddInteger(FI);
2005 void *IP = nullptr;
2006 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2007 return SDValue(E, 0);
2008
2009 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
2010 CSEMap.InsertNode(N, IP);
2011 InsertNode(N);
2012 return SDValue(N, 0);
2013}
2014
2015SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
2016 unsigned TargetFlags) {
2017 assert((TargetFlags == 0 || isTarget) &&
2018 "Cannot set target flags on target-independent jump tables");
2019 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
2020 SDVTList VTs = getVTList(VT);
2022 AddNodeIDNode(ID, Opc, VTs, {});
2023 ID.AddInteger(JTI);
2024 ID.AddInteger(TargetFlags);
2025 void *IP = nullptr;
2026 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2027 return SDValue(E, 0);
2028
2029 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
2030 CSEMap.InsertNode(N, IP);
2031 InsertNode(N);
2032 return SDValue(N, 0);
2033}
2034
2036 const SDLoc &DL) {
2038 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Other, Chain,
2039 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
2040}
2041
2043 MaybeAlign Alignment, int Offset,
2044 bool isTarget, unsigned TargetFlags) {
2045 assert((TargetFlags == 0 || isTarget) &&
2046 "Cannot set target flags on target-independent globals");
2047 if (!Alignment)
2048 Alignment = shouldOptForSize()
2049 ? getDataLayout().getABITypeAlign(C->getType())
2050 : getDataLayout().getPrefTypeAlign(C->getType());
2051 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2052 SDVTList VTs = getVTList(VT);
2054 AddNodeIDNode(ID, Opc, VTs, {});
2055 ID.AddInteger(Alignment->value());
2056 ID.AddInteger(Offset);
2057 ID.AddPointer(C);
2058 ID.AddInteger(TargetFlags);
2059 void *IP = nullptr;
2060 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2061 return SDValue(E, 0);
2062
2063 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2064 TargetFlags);
2065 CSEMap.InsertNode(N, IP);
2066 InsertNode(N);
2067 SDValue V = SDValue(N, 0);
2068 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
2069 return V;
2070}
2071
2073 MaybeAlign Alignment, int Offset,
2074 bool isTarget, unsigned TargetFlags) {
2075 assert((TargetFlags == 0 || isTarget) &&
2076 "Cannot set target flags on target-independent globals");
2077 if (!Alignment)
2078 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
2079 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2080 SDVTList VTs = getVTList(VT);
2082 AddNodeIDNode(ID, Opc, VTs, {});
2083 ID.AddInteger(Alignment->value());
2084 ID.AddInteger(Offset);
2085 C->addSelectionDAGCSEId(ID);
2086 ID.AddInteger(TargetFlags);
2087 void *IP = nullptr;
2088 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2089 return SDValue(E, 0);
2090
2091 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2092 TargetFlags);
2093 CSEMap.InsertNode(N, IP);
2094 InsertNode(N);
2095 return SDValue(N, 0);
2096}
2097
2100 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2101 ID.AddPointer(MBB);
2102 void *IP = nullptr;
2103 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2104 return SDValue(E, 0);
2105
2106 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2107 CSEMap.InsertNode(N, IP);
2108 InsertNode(N);
2109 return SDValue(N, 0);
2110}
2111
2113 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2114 ValueTypeNodes.size())
2115 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2116
2117 SDNode *&N = VT.isExtended() ?
2118 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2119
2120 if (N) return SDValue(N, 0);
2121 N = newSDNode<VTSDNode>(VT);
2122 InsertNode(N);
2123 return SDValue(N, 0);
2124}
2125
2127 SDNode *&N = ExternalSymbols[Sym];
2128 if (N) return SDValue(N, 0);
2129 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2130 InsertNode(N);
2131 return SDValue(N, 0);
2132}
2133
2134SDValue SelectionDAG::getExternalSymbol(RTLIB::LibcallImpl Libcall, EVT VT) {
2136 return getExternalSymbol(SymName.data(), VT);
2137}
2138
2140 SDNode *&N = MCSymbols[Sym];
2141 if (N)
2142 return SDValue(N, 0);
2143 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2144 InsertNode(N);
2145 return SDValue(N, 0);
2146}
2147
2149 unsigned TargetFlags) {
2150 SDNode *&N =
2151 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2152 if (N) return SDValue(N, 0);
2153 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2154 InsertNode(N);
2155 return SDValue(N, 0);
2156}
2157
2159 EVT VT, unsigned TargetFlags) {
2161 return getTargetExternalSymbol(SymName.data(), VT, TargetFlags);
2162}
2163
2165 if ((unsigned)Cond >= CondCodeNodes.size())
2166 CondCodeNodes.resize(Cond+1);
2167
2168 if (!CondCodeNodes[Cond]) {
2169 auto *N = newSDNode<CondCodeSDNode>(Cond);
2170 CondCodeNodes[Cond] = N;
2171 InsertNode(N);
2172 }
2173
2174 return SDValue(CondCodeNodes[Cond], 0);
2175}
2176
2178 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2179 "APInt size does not match type size!");
2180
2181 if (MulImm == 0)
2182 return getConstant(0, DL, VT);
2183
2184 const MachineFunction &MF = getMachineFunction();
2185 const Function &F = MF.getFunction();
2186 ConstantRange CR = getVScaleRange(&F, 64);
2187 if (const APInt *C = CR.getSingleElement())
2188 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2189
2190 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2191}
2192
2193/// \returns a value of type \p VT that represents the runtime value of \p
2194/// Quantity, i.e. scaled by vscale if it's scalable, or a fixed constant
2195/// otherwise. Quantity should be a FixedOrScalableQuantity, i.e. ElementCount
2196/// or TypeSize.
2197template <typename Ty>
2199 EVT VT, Ty Quantity) {
2200 if (Quantity.isScalable())
2201 return DAG.getVScale(
2202 DL, VT, APInt(VT.getSizeInBits(), Quantity.getKnownMinValue()));
2203
2204 return DAG.getConstant(Quantity.getKnownMinValue(), DL, VT);
2205}
2206
2208 ElementCount EC) {
2209 return getFixedOrScalableQuantity(*this, DL, VT, EC);
2210}
2211
2213 return getFixedOrScalableQuantity(*this, DL, VT, TS);
2214}
2215
2217 ElementCount EC) {
2218 EVT IdxVT = TLI->getVectorIdxTy(getDataLayout());
2219 EVT MaskVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), DataVT);
2220 return getNode(ISD::GET_ACTIVE_LANE_MASK, DL, MaskVT,
2221 getConstant(0, DL, IdxVT), getElementCount(DL, IdxVT, EC));
2222}
2223
2225 APInt One(ResVT.getScalarSizeInBits(), 1);
2226 return getStepVector(DL, ResVT, One);
2227}
2228
2230 const APInt &StepVal) {
2231 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2232 if (ResVT.isScalableVector())
2233 return getNode(
2234 ISD::STEP_VECTOR, DL, ResVT,
2235 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2236
2237 SmallVector<SDValue, 16> OpsStepConstants;
2238 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2239 OpsStepConstants.push_back(
2240 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2241 return getBuildVector(ResVT, DL, OpsStepConstants);
2242}
2243
2244/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2245/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2250
2252 SDValue N2, ArrayRef<int> Mask) {
2253 assert(VT.getVectorNumElements() == Mask.size() &&
2254 "Must have the same number of vector elements as mask elements!");
2255 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2256 "Invalid VECTOR_SHUFFLE");
2257
2258 // Canonicalize shuffle undef, undef -> undef
2259 if (N1.isUndef() && N2.isUndef())
2260 return getUNDEF(VT);
2261
2262 // Validate that all indices in Mask are within the range of the elements
2263 // input to the shuffle.
2264 int NElts = Mask.size();
2265 assert(llvm::all_of(Mask,
2266 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2267 "Index out of range");
2268
2269 // Copy the mask so we can do any needed cleanup.
2270 SmallVector<int, 8> MaskVec(Mask);
2271
2272 // Canonicalize shuffle v, v -> v, undef
2273 if (N1 == N2) {
2274 N2 = getUNDEF(VT);
2275 for (int i = 0; i != NElts; ++i)
2276 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2277 }
2278
2279 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2280 if (N1.isUndef())
2281 commuteShuffle(N1, N2, MaskVec);
2282
2283 if (TLI->hasVectorBlend()) {
2284 // If shuffling a splat, try to blend the splat instead. We do this here so
2285 // that even when this arises during lowering we don't have to re-handle it.
2286 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2287 BitVector UndefElements;
2288 SDValue Splat = BV->getSplatValue(&UndefElements);
2289 if (!Splat)
2290 return;
2291
2292 for (int i = 0; i < NElts; ++i) {
2293 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2294 continue;
2295
2296 // If this input comes from undef, mark it as such.
2297 if (UndefElements[MaskVec[i] - Offset]) {
2298 MaskVec[i] = -1;
2299 continue;
2300 }
2301
2302 // If we can blend a non-undef lane, use that instead.
2303 if (!UndefElements[i])
2304 MaskVec[i] = i + Offset;
2305 }
2306 };
2307 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2308 BlendSplat(N1BV, 0);
2309 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2310 BlendSplat(N2BV, NElts);
2311 }
2312
2313 // Canonicalize all index into lhs, -> shuffle lhs, undef
2314 // Canonicalize all index into rhs, -> shuffle rhs, undef
2315 bool AllLHS = true, AllRHS = true;
2316 bool N2Undef = N2.isUndef();
2317 for (int i = 0; i != NElts; ++i) {
2318 if (MaskVec[i] >= NElts) {
2319 if (N2Undef)
2320 MaskVec[i] = -1;
2321 else
2322 AllLHS = false;
2323 } else if (MaskVec[i] >= 0) {
2324 AllRHS = false;
2325 }
2326 }
2327 if (AllLHS && AllRHS)
2328 return getUNDEF(VT);
2329 if (AllLHS && !N2Undef)
2330 N2 = getUNDEF(VT);
2331 if (AllRHS) {
2332 N1 = getUNDEF(VT);
2333 commuteShuffle(N1, N2, MaskVec);
2334 }
2335 // Reset our undef status after accounting for the mask.
2336 N2Undef = N2.isUndef();
2337 // Re-check whether both sides ended up undef.
2338 if (N1.isUndef() && N2Undef)
2339 return getUNDEF(VT);
2340
2341 // If Identity shuffle return that node.
2342 bool Identity = true, AllSame = true;
2343 for (int i = 0; i != NElts; ++i) {
2344 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2345 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2346 }
2347 if (Identity && NElts)
2348 return N1;
2349
2350 // Shuffling a constant splat doesn't change the result.
2351 if (N2Undef) {
2352 SDValue V = N1;
2353
2354 // Look through any bitcasts. We check that these don't change the number
2355 // (and size) of elements and just changes their types.
2356 while (V.getOpcode() == ISD::BITCAST)
2357 V = V->getOperand(0);
2358
2359 // A splat should always show up as a build vector node.
2360 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2361 BitVector UndefElements;
2362 SDValue Splat = BV->getSplatValue(&UndefElements);
2363 // If this is a splat of an undef, shuffling it is also undef.
2364 if (Splat && Splat.isUndef())
2365 return getUNDEF(VT);
2366
2367 bool SameNumElts =
2368 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2369
2370 // We only have a splat which can skip shuffles if there is a splatted
2371 // value and no undef lanes rearranged by the shuffle.
2372 if (Splat && UndefElements.none()) {
2373 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2374 // number of elements match or the value splatted is a zero constant.
2375 if (SameNumElts || isNullConstant(Splat))
2376 return N1;
2377 }
2378
2379 // If the shuffle itself creates a splat, build the vector directly.
2380 if (AllSame && SameNumElts) {
2381 EVT BuildVT = BV->getValueType(0);
2382 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2383 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2384
2385 // We may have jumped through bitcasts, so the type of the
2386 // BUILD_VECTOR may not match the type of the shuffle.
2387 if (BuildVT != VT)
2388 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2389 return NewBV;
2390 }
2391 }
2392 }
2393
2394 SDVTList VTs = getVTList(VT);
2396 SDValue Ops[2] = { N1, N2 };
2398 for (int i = 0; i != NElts; ++i)
2399 ID.AddInteger(MaskVec[i]);
2400
2401 void* IP = nullptr;
2402 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2403 return SDValue(E, 0);
2404
2405 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2406 // SDNode doesn't have access to it. This memory will be "leaked" when
2407 // the node is deallocated, but recovered when the NodeAllocator is released.
2408 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2409 llvm::copy(MaskVec, MaskAlloc);
2410
2411 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2412 dl.getDebugLoc(), MaskAlloc);
2413 createOperands(N, Ops);
2414
2415 CSEMap.InsertNode(N, IP);
2416 InsertNode(N);
2417 SDValue V = SDValue(N, 0);
2418 NewSDValueDbgMsg(V, "Creating new node: ", this);
2419 return V;
2420}
2421
2423 EVT VT = SV.getValueType(0);
2424 SmallVector<int, 8> MaskVec(SV.getMask());
2426
2427 SDValue Op0 = SV.getOperand(0);
2428 SDValue Op1 = SV.getOperand(1);
2429 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2430}
2431
2433 SDVTList VTs = getVTList(VT);
2435 AddNodeIDNode(ID, ISD::Register, VTs, {});
2436 ID.AddInteger(Reg.id());
2437 void *IP = nullptr;
2438 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2439 return SDValue(E, 0);
2440
2441 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2442 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2443 CSEMap.InsertNode(N, IP);
2444 InsertNode(N);
2445 return SDValue(N, 0);
2446}
2447
2450 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2451 ID.AddPointer(RegMask);
2452 void *IP = nullptr;
2453 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2454 return SDValue(E, 0);
2455
2456 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2457 CSEMap.InsertNode(N, IP);
2458 InsertNode(N);
2459 return SDValue(N, 0);
2460}
2461
2463 MCSymbol *Label) {
2464 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2465}
2466
2467SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2468 SDValue Root, MCSymbol *Label) {
2470 SDValue Ops[] = { Root };
2471 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2472 ID.AddPointer(Label);
2473 void *IP = nullptr;
2474 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2475 return SDValue(E, 0);
2476
2477 auto *N =
2478 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2479 createOperands(N, Ops);
2480
2481 CSEMap.InsertNode(N, IP);
2482 InsertNode(N);
2483 return SDValue(N, 0);
2484}
2485
2487 int64_t Offset, bool isTarget,
2488 unsigned TargetFlags) {
2489 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2490 SDVTList VTs = getVTList(VT);
2491
2493 AddNodeIDNode(ID, Opc, VTs, {});
2494 ID.AddPointer(BA);
2495 ID.AddInteger(Offset);
2496 ID.AddInteger(TargetFlags);
2497 void *IP = nullptr;
2498 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2499 return SDValue(E, 0);
2500
2501 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2502 CSEMap.InsertNode(N, IP);
2503 InsertNode(N);
2504 return SDValue(N, 0);
2505}
2506
2509 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2510 ID.AddPointer(V);
2511
2512 void *IP = nullptr;
2513 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2514 return SDValue(E, 0);
2515
2516 auto *N = newSDNode<SrcValueSDNode>(V);
2517 CSEMap.InsertNode(N, IP);
2518 InsertNode(N);
2519 return SDValue(N, 0);
2520}
2521
2524 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2525 ID.AddPointer(MD);
2526
2527 void *IP = nullptr;
2528 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2529 return SDValue(E, 0);
2530
2531 auto *N = newSDNode<MDNodeSDNode>(MD);
2532 CSEMap.InsertNode(N, IP);
2533 InsertNode(N);
2534 return SDValue(N, 0);
2535}
2536
2538 if (VT == V.getValueType())
2539 return V;
2540
2541 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2542}
2543
2545 unsigned SrcAS, unsigned DestAS) {
2546 SDVTList VTs = getVTList(VT);
2547 SDValue Ops[] = {Ptr};
2550 ID.AddInteger(SrcAS);
2551 ID.AddInteger(DestAS);
2552
2553 void *IP = nullptr;
2554 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2555 return SDValue(E, 0);
2556
2557 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2558 VTs, SrcAS, DestAS);
2559 createOperands(N, Ops);
2560
2561 CSEMap.InsertNode(N, IP);
2562 InsertNode(N);
2563 return SDValue(N, 0);
2564}
2565
2567 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2568}
2569
2571 bool PoisonOnly) {
2572 if (isGuaranteedNotToBeUndefOrPoison(V, DemandedElts, PoisonOnly))
2573 return V;
2574 return getFreeze(V);
2575}
2576
2577/// getShiftAmountOperand - Return the specified value casted to
2578/// the target's desired shift amount type.
2580 EVT OpTy = Op.getValueType();
2581 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2582 if (OpTy == ShTy || OpTy.isVector()) return Op;
2583
2584 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2585}
2586
2588 SDLoc dl(Node);
2590 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2591 EVT VT = Node->getValueType(0);
2592 SDValue Tmp1 = Node->getOperand(0);
2593 SDValue Tmp2 = Node->getOperand(1);
2594 const MaybeAlign MA(Node->getConstantOperandVal(3));
2595
2596 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2597 Tmp2, MachinePointerInfo(V));
2598 SDValue VAList = VAListLoad;
2599
2600 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2601 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2602 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2603
2604 VAList = getNode(
2605 ISD::AND, dl, VAList.getValueType(), VAList,
2606 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2607 }
2608
2609 // Increment the pointer, VAList, to the next vaarg
2610 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2611 getConstant(getDataLayout().getTypeAllocSize(
2612 VT.getTypeForEVT(*getContext())),
2613 dl, VAList.getValueType()));
2614 // Store the incremented VAList to the legalized pointer
2615 Tmp1 =
2616 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2617 // Load the actual argument out of the pointer VAList
2618 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2619}
2620
2622 SDLoc dl(Node);
2624 // This defaults to loading a pointer from the input and storing it to the
2625 // output, returning the chain.
2626 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2627 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2628 SDValue Tmp1 =
2629 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2630 Node->getOperand(2), MachinePointerInfo(VS));
2631 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2632 MachinePointerInfo(VD));
2633}
2634
2636 const DataLayout &DL = getDataLayout();
2637 Type *Ty = VT.getTypeForEVT(*getContext());
2638 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2639
2640 if (TLI->isTypeLegal(VT) || !VT.isVector())
2641 return RedAlign;
2642
2643 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2644 const Align StackAlign = TFI->getStackAlign();
2645
2646 // See if we can choose a smaller ABI alignment in cases where it's an
2647 // illegal vector type that will get broken down.
2648 if (RedAlign > StackAlign) {
2649 EVT IntermediateVT;
2650 MVT RegisterVT;
2651 unsigned NumIntermediates;
2652 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2653 NumIntermediates, RegisterVT);
2654 Ty = IntermediateVT.getTypeForEVT(*getContext());
2655 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2656 if (RedAlign2 < RedAlign)
2657 RedAlign = RedAlign2;
2658
2659 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2660 // If the stack is not realignable, the alignment should be limited to the
2661 // StackAlignment
2662 RedAlign = std::min(RedAlign, StackAlign);
2663 }
2664
2665 return RedAlign;
2666}
2667
2669 MachineFrameInfo &MFI = MF->getFrameInfo();
2670 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2671 int StackID = 0;
2672 if (Bytes.isScalable())
2673 StackID = TFI->getStackIDForScalableVectors();
2674 // The stack id gives an indication of whether the object is scalable or
2675 // not, so it's safe to pass in the minimum size here.
2676 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2677 false, nullptr, StackID);
2678 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2679}
2680
2682 Type *Ty = VT.getTypeForEVT(*getContext());
2683 Align StackAlign =
2684 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2685 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2686}
2687
2689 TypeSize VT1Size = VT1.getStoreSize();
2690 TypeSize VT2Size = VT2.getStoreSize();
2691 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2692 "Don't know how to choose the maximum size when creating a stack "
2693 "temporary");
2694 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2695 ? VT1Size
2696 : VT2Size;
2697
2698 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2699 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2700 const DataLayout &DL = getDataLayout();
2701 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2702 return CreateStackTemporary(Bytes, Align);
2703}
2704
2706 ISD::CondCode Cond, const SDLoc &dl,
2707 SDNodeFlags Flags) {
2708 EVT OpVT = N1.getValueType();
2709
2710 auto GetUndefBooleanConstant = [&]() {
2711 if (VT.getScalarType() == MVT::i1 ||
2712 TLI->getBooleanContents(OpVT) ==
2714 return getUNDEF(VT);
2715 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2716 // so we cannot use getUNDEF(). Return zero instead.
2717 return getConstant(0, dl, VT);
2718 };
2719
2720 // These setcc operations always fold.
2721 switch (Cond) {
2722 default: break;
2723 case ISD::SETFALSE:
2724 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2725 case ISD::SETTRUE:
2726 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2727
2728 case ISD::SETOEQ:
2729 case ISD::SETOGT:
2730 case ISD::SETOGE:
2731 case ISD::SETOLT:
2732 case ISD::SETOLE:
2733 case ISD::SETONE:
2734 case ISD::SETO:
2735 case ISD::SETUO:
2736 case ISD::SETUEQ:
2737 case ISD::SETUNE:
2738 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2739 break;
2740 }
2741
2742 if (OpVT.isInteger()) {
2743 // For EQ and NE, we can always pick a value for the undef to make the
2744 // predicate pass or fail, so we can return undef.
2745 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2746 // icmp eq/ne X, undef -> undef.
2747 if ((N1.isUndef() || N2.isUndef()) &&
2748 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2749 return GetUndefBooleanConstant();
2750
2751 // If both operands are undef, we can return undef for int comparison.
2752 // icmp undef, undef -> undef.
2753 if (N1.isUndef() && N2.isUndef())
2754 return GetUndefBooleanConstant();
2755
2756 // icmp X, X -> true/false
2757 // icmp X, undef -> true/false because undef could be X.
2758 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2759 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2760 }
2761
2763 const APInt &C2 = N2C->getAPIntValue();
2765 const APInt &C1 = N1C->getAPIntValue();
2766
2768 dl, VT, OpVT);
2769 }
2770 }
2771
2772 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2773 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2774
2775 if (N1CFP && N2CFP) {
2776 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2777 switch (Cond) {
2778 default: break;
2779 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2780 return GetUndefBooleanConstant();
2781 [[fallthrough]];
2782 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2783 OpVT);
2784 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2785 return GetUndefBooleanConstant();
2786 [[fallthrough]];
2788 R==APFloat::cmpLessThan, dl, VT,
2789 OpVT);
2790 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2791 return GetUndefBooleanConstant();
2792 [[fallthrough]];
2793 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2794 OpVT);
2795 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2796 return GetUndefBooleanConstant();
2797 [[fallthrough]];
2799 VT, OpVT);
2800 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2801 return GetUndefBooleanConstant();
2802 [[fallthrough]];
2804 R==APFloat::cmpEqual, dl, VT,
2805 OpVT);
2806 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2807 return GetUndefBooleanConstant();
2808 [[fallthrough]];
2810 R==APFloat::cmpEqual, dl, VT, OpVT);
2811 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2812 OpVT);
2813 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2814 OpVT);
2816 R==APFloat::cmpEqual, dl, VT,
2817 OpVT);
2818 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2819 OpVT);
2821 R==APFloat::cmpLessThan, dl, VT,
2822 OpVT);
2824 R==APFloat::cmpUnordered, dl, VT,
2825 OpVT);
2827 VT, OpVT);
2828 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2829 OpVT);
2830 }
2831 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2832 // Ensure that the constant occurs on the RHS.
2834 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2835 return SDValue();
2836 return getSetCC(dl, VT, N2, N1, SwappedCond, /*Chain=*/{},
2837 /*IsSignaling=*/false, Flags);
2838 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2839 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2840 // If an operand is known to be a nan (or undef that could be a nan), we can
2841 // fold it.
2842 // Choosing NaN for the undef will always make unordered comparison succeed
2843 // and ordered comparison fails.
2844 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2845 switch (ISD::getUnorderedFlavor(Cond)) {
2846 default:
2847 llvm_unreachable("Unknown flavor!");
2848 case 0: // Known false.
2849 return getBoolConstant(false, dl, VT, OpVT);
2850 case 1: // Known true.
2851 return getBoolConstant(true, dl, VT, OpVT);
2852 case 2: // Undefined.
2853 return GetUndefBooleanConstant();
2854 }
2855 }
2856
2857 // Could not fold it.
2858 return SDValue();
2859}
2860
2861/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2862/// use this predicate to simplify operations downstream.
2864 unsigned BitWidth = Op.getScalarValueSizeInBits();
2866}
2867
2868// TODO: Should have argument to specify if sign bit of nan is ignorable.
2870 if (Depth >= MaxRecursionDepth)
2871 return false; // Limit search depth.
2872
2873 unsigned Opc = Op.getOpcode();
2874 switch (Opc) {
2875 case ISD::FABS:
2876 return true;
2877 case ISD::AssertNoFPClass: {
2878 FPClassTest NoFPClass =
2879 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
2880
2881 const FPClassTest TestMask = fcNan | fcNegative;
2882 return (NoFPClass & TestMask) == TestMask;
2883 }
2884 case ISD::ARITH_FENCE:
2885 return SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2886 case ISD::FEXP:
2887 case ISD::FEXP2:
2888 case ISD::FEXP10:
2889 return Op->getFlags().hasNoNaNs();
2890 case ISD::FMINNUM:
2891 case ISD::FMINNUM_IEEE:
2892 case ISD::FMINIMUM:
2893 case ISD::FMINIMUMNUM:
2894 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2895 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2896 case ISD::FMAXNUM:
2897 case ISD::FMAXNUM_IEEE:
2898 case ISD::FMAXIMUM:
2899 case ISD::FMAXIMUMNUM:
2900 // TODO: If we can ignore the sign bit of nans, only one side being known 0
2901 // is sufficient.
2902 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2903 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2904 default:
2905 return false;
2906 }
2907
2908 llvm_unreachable("covered opcode switch");
2909}
2910
2911/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2912/// this predicate to simplify operations downstream. Mask is known to be zero
2913/// for bits that V cannot have.
2915 unsigned Depth) const {
2916 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2917}
2918
2919/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2920/// DemandedElts. We use this predicate to simplify operations downstream.
2921/// Mask is known to be zero for bits that V cannot have.
2923 const APInt &DemandedElts,
2924 unsigned Depth) const {
2925 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2926}
2927
2928/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2929/// DemandedElts. We use this predicate to simplify operations downstream.
2931 unsigned Depth /* = 0 */) const {
2932 return computeKnownBits(V, DemandedElts, Depth).isZero();
2933}
2934
2935/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2937 unsigned Depth) const {
2938 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2939}
2940
2942 const APInt &DemandedElts,
2943 unsigned Depth) const {
2944 EVT VT = Op.getValueType();
2945 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2946
2947 unsigned NumElts = VT.getVectorNumElements();
2948 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2949
2950 APInt KnownZeroElements = APInt::getZero(NumElts);
2951 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2952 if (!DemandedElts[EltIdx])
2953 continue; // Don't query elements that are not demanded.
2954 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2955 if (MaskedVectorIsZero(Op, Mask, Depth))
2956 KnownZeroElements.setBit(EltIdx);
2957 }
2958 return KnownZeroElements;
2959}
2960
2961/// isSplatValue - Return true if the vector V has the same value
2962/// across all DemandedElts. For scalable vectors, we don't know the
2963/// number of lanes at compile time. Instead, we use a 1 bit APInt
2964/// to represent a conservative value for all lanes; that is, that
2965/// one bit value is implicitly splatted across all lanes.
2966bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2967 APInt &UndefElts, unsigned Depth) const {
2968 unsigned Opcode = V.getOpcode();
2969 EVT VT = V.getValueType();
2970 assert(VT.isVector() && "Vector type expected");
2971 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2972 "scalable demanded bits are ignored");
2973
2974 if (!DemandedElts)
2975 return false; // No demanded elts, better to assume we don't know anything.
2976
2977 if (Depth >= MaxRecursionDepth)
2978 return false; // Limit search depth.
2979
2980 // Deal with some common cases here that work for both fixed and scalable
2981 // vector types.
2982 switch (Opcode) {
2983 case ISD::SPLAT_VECTOR:
2984 UndefElts = V.getOperand(0).isUndef()
2985 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2986 : APInt(DemandedElts.getBitWidth(), 0);
2987 return true;
2988 case ISD::ADD:
2989 case ISD::SUB:
2990 case ISD::AND:
2991 case ISD::XOR:
2992 case ISD::OR: {
2993 APInt UndefLHS, UndefRHS;
2994 SDValue LHS = V.getOperand(0);
2995 SDValue RHS = V.getOperand(1);
2996 // Only recognize splats with the same demanded undef elements for both
2997 // operands, otherwise we might fail to handle binop-specific undef
2998 // handling.
2999 // e.g. (and undef, 0) -> 0 etc.
3000 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
3001 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1) &&
3002 (DemandedElts & UndefLHS) == (DemandedElts & UndefRHS)) {
3003 UndefElts = UndefLHS | UndefRHS;
3004 return true;
3005 }
3006 return false;
3007 }
3008 case ISD::ABS:
3009 case ISD::TRUNCATE:
3010 case ISD::SIGN_EXTEND:
3011 case ISD::ZERO_EXTEND:
3012 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
3013 default:
3014 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
3015 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
3016 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
3017 Depth);
3018 break;
3019 }
3020
3021 // We don't support other cases than those above for scalable vectors at
3022 // the moment.
3023 if (VT.isScalableVector())
3024 return false;
3025
3026 unsigned NumElts = VT.getVectorNumElements();
3027 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
3028 UndefElts = APInt::getZero(NumElts);
3029
3030 switch (Opcode) {
3031 case ISD::BUILD_VECTOR: {
3032 SDValue Scl;
3033 for (unsigned i = 0; i != NumElts; ++i) {
3034 SDValue Op = V.getOperand(i);
3035 if (Op.isUndef()) {
3036 UndefElts.setBit(i);
3037 continue;
3038 }
3039 if (!DemandedElts[i])
3040 continue;
3041 if (Scl && Scl != Op)
3042 return false;
3043 Scl = Op;
3044 }
3045 return true;
3046 }
3047 case ISD::VECTOR_SHUFFLE: {
3048 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
3049 APInt DemandedLHS = APInt::getZero(NumElts);
3050 APInt DemandedRHS = APInt::getZero(NumElts);
3051 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
3052 for (int i = 0; i != (int)NumElts; ++i) {
3053 int M = Mask[i];
3054 if (M < 0) {
3055 UndefElts.setBit(i);
3056 continue;
3057 }
3058 if (!DemandedElts[i])
3059 continue;
3060 if (M < (int)NumElts)
3061 DemandedLHS.setBit(M);
3062 else
3063 DemandedRHS.setBit(M - NumElts);
3064 }
3065
3066 // If we aren't demanding either op, assume there's no splat.
3067 // If we are demanding both ops, assume there's no splat.
3068 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
3069 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
3070 return false;
3071
3072 // See if the demanded elts of the source op is a splat or we only demand
3073 // one element, which should always be a splat.
3074 // TODO: Handle source ops splats with undefs.
3075 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
3076 APInt SrcUndefs;
3077 return (SrcElts.popcount() == 1) ||
3078 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
3079 (SrcElts & SrcUndefs).isZero());
3080 };
3081 if (!DemandedLHS.isZero())
3082 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
3083 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
3084 }
3086 // Offset the demanded elts by the subvector index.
3087 SDValue Src = V.getOperand(0);
3088 // We don't support scalable vectors at the moment.
3089 if (Src.getValueType().isScalableVector())
3090 return false;
3091 uint64_t Idx = V.getConstantOperandVal(1);
3092 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3093 APInt UndefSrcElts;
3094 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3095 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3096 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3097 return true;
3098 }
3099 break;
3100 }
3104 // Widen the demanded elts by the src element count.
3105 SDValue Src = V.getOperand(0);
3106 // We don't support scalable vectors at the moment.
3107 if (Src.getValueType().isScalableVector())
3108 return false;
3109 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3110 APInt UndefSrcElts;
3111 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3112 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3113 UndefElts = UndefSrcElts.trunc(NumElts);
3114 return true;
3115 }
3116 break;
3117 }
3118 case ISD::BITCAST: {
3119 SDValue Src = V.getOperand(0);
3120 EVT SrcVT = Src.getValueType();
3121 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3122 unsigned BitWidth = VT.getScalarSizeInBits();
3123
3124 // Ignore bitcasts from unsupported types.
3125 // TODO: Add fp support?
3126 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3127 break;
3128
3129 // Bitcast 'small element' vector to 'large element' vector.
3130 if ((BitWidth % SrcBitWidth) == 0) {
3131 // See if each sub element is a splat.
3132 unsigned Scale = BitWidth / SrcBitWidth;
3133 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3134 APInt ScaledDemandedElts =
3135 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3136 for (unsigned I = 0; I != Scale; ++I) {
3137 APInt SubUndefElts;
3138 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3139 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3140 SubDemandedElts &= ScaledDemandedElts;
3141 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3142 return false;
3143 // TODO: Add support for merging sub undef elements.
3144 if (!SubUndefElts.isZero())
3145 return false;
3146 }
3147 return true;
3148 }
3149 break;
3150 }
3151 }
3152
3153 return false;
3154}
3155
3156/// Helper wrapper to main isSplatValue function.
3157bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3158 EVT VT = V.getValueType();
3159 assert(VT.isVector() && "Vector type expected");
3160
3161 APInt UndefElts;
3162 // Since the number of lanes in a scalable vector is unknown at compile time,
3163 // we track one bit which is implicitly broadcast to all lanes. This means
3164 // that all lanes in a scalable vector are considered demanded.
3165 APInt DemandedElts
3167 return isSplatValue(V, DemandedElts, UndefElts) &&
3168 (AllowUndefs || !UndefElts);
3169}
3170
3173
3174 EVT VT = V.getValueType();
3175 unsigned Opcode = V.getOpcode();
3176 switch (Opcode) {
3177 default: {
3178 APInt UndefElts;
3179 // Since the number of lanes in a scalable vector is unknown at compile time,
3180 // we track one bit which is implicitly broadcast to all lanes. This means
3181 // that all lanes in a scalable vector are considered demanded.
3182 APInt DemandedElts
3184
3185 if (isSplatValue(V, DemandedElts, UndefElts)) {
3186 if (VT.isScalableVector()) {
3187 // DemandedElts and UndefElts are ignored for scalable vectors, since
3188 // the only supported cases are SPLAT_VECTOR nodes.
3189 SplatIdx = 0;
3190 } else {
3191 // Handle case where all demanded elements are UNDEF.
3192 if (DemandedElts.isSubsetOf(UndefElts)) {
3193 SplatIdx = 0;
3194 return getUNDEF(VT);
3195 }
3196 SplatIdx = (UndefElts & DemandedElts).countr_one();
3197 }
3198 return V;
3199 }
3200 break;
3201 }
3202 case ISD::SPLAT_VECTOR:
3203 SplatIdx = 0;
3204 return V;
3205 case ISD::VECTOR_SHUFFLE: {
3206 assert(!VT.isScalableVector());
3207 // Check if this is a shuffle node doing a splat.
3208 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3209 // getTargetVShiftNode currently struggles without the splat source.
3210 auto *SVN = cast<ShuffleVectorSDNode>(V);
3211 if (!SVN->isSplat())
3212 break;
3213 int Idx = SVN->getSplatIndex();
3214 int NumElts = V.getValueType().getVectorNumElements();
3215 SplatIdx = Idx % NumElts;
3216 return V.getOperand(Idx / NumElts);
3217 }
3218 }
3219
3220 return SDValue();
3221}
3222
3224 int SplatIdx;
3225 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3226 EVT SVT = SrcVector.getValueType().getScalarType();
3227 EVT LegalSVT = SVT;
3228 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3229 if (!SVT.isInteger())
3230 return SDValue();
3231 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3232 if (LegalSVT.bitsLT(SVT))
3233 return SDValue();
3234 }
3235 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3236 }
3237 return SDValue();
3238}
3239
3240std::optional<ConstantRange>
3242 unsigned Depth) const {
3243 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3244 V.getOpcode() == ISD::SRA) &&
3245 "Unknown shift node");
3246 // Shifting more than the bitwidth is not valid.
3247 unsigned BitWidth = V.getScalarValueSizeInBits();
3248
3249 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3250 const APInt &ShAmt = Cst->getAPIntValue();
3251 if (ShAmt.uge(BitWidth))
3252 return std::nullopt;
3253 return ConstantRange(ShAmt);
3254 }
3255
3256 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3257 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3258 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3259 if (!DemandedElts[i])
3260 continue;
3261 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3262 if (!SA) {
3263 MinAmt = MaxAmt = nullptr;
3264 break;
3265 }
3266 const APInt &ShAmt = SA->getAPIntValue();
3267 if (ShAmt.uge(BitWidth))
3268 return std::nullopt;
3269 if (!MinAmt || MinAmt->ugt(ShAmt))
3270 MinAmt = &ShAmt;
3271 if (!MaxAmt || MaxAmt->ult(ShAmt))
3272 MaxAmt = &ShAmt;
3273 }
3274 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3275 "Failed to find matching min/max shift amounts");
3276 if (MinAmt && MaxAmt)
3277 return ConstantRange(*MinAmt, *MaxAmt + 1);
3278 }
3279
3280 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3281 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3282 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3283 if (KnownAmt.getMaxValue().ult(BitWidth))
3284 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3285
3286 return std::nullopt;
3287}
3288
3289std::optional<unsigned>
3291 unsigned Depth) const {
3292 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3293 V.getOpcode() == ISD::SRA) &&
3294 "Unknown shift node");
3295 if (std::optional<ConstantRange> AmtRange =
3296 getValidShiftAmountRange(V, DemandedElts, Depth))
3297 if (const APInt *ShAmt = AmtRange->getSingleElement())
3298 return ShAmt->getZExtValue();
3299 return std::nullopt;
3300}
3301
3302std::optional<unsigned>
3304 APInt DemandedElts = getDemandAllEltsMask(V);
3305 return getValidShiftAmount(V, DemandedElts, Depth);
3306}
3307
3308std::optional<unsigned>
3310 unsigned Depth) const {
3311 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3312 V.getOpcode() == ISD::SRA) &&
3313 "Unknown shift node");
3314 if (std::optional<ConstantRange> AmtRange =
3315 getValidShiftAmountRange(V, DemandedElts, Depth))
3316 return AmtRange->getUnsignedMin().getZExtValue();
3317 return std::nullopt;
3318}
3319
3320std::optional<unsigned>
3322 APInt DemandedElts = getDemandAllEltsMask(V);
3323 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3324}
3325
3326std::optional<unsigned>
3328 unsigned Depth) const {
3329 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3330 V.getOpcode() == ISD::SRA) &&
3331 "Unknown shift node");
3332 if (std::optional<ConstantRange> AmtRange =
3333 getValidShiftAmountRange(V, DemandedElts, Depth))
3334 return AmtRange->getUnsignedMax().getZExtValue();
3335 return std::nullopt;
3336}
3337
3338std::optional<unsigned>
3340 APInt DemandedElts = getDemandAllEltsMask(V);
3341 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3342}
3343
3344/// Determine which bits of Op are known to be either zero or one and return
3345/// them in Known. For vectors, the known bits are those that are shared by
3346/// every vector element.
3348 APInt DemandedElts = getDemandAllEltsMask(Op);
3349 return computeKnownBits(Op, DemandedElts, Depth);
3350}
3351
3352/// Determine which bits of Op are known to be either zero or one and return
3353/// them in Known. The DemandedElts argument allows us to only collect the known
3354/// bits that are shared by the requested vector elements.
3356 unsigned Depth) const {
3357 unsigned BitWidth = Op.getScalarValueSizeInBits();
3358
3359 KnownBits Known(BitWidth); // Don't know anything.
3360
3361 if (auto OptAPInt = Op->bitcastToAPInt()) {
3362 // We know all of the bits for a constant!
3363 return KnownBits::makeConstant(*std::move(OptAPInt));
3364 }
3365
3366 if (Depth >= MaxRecursionDepth)
3367 return Known; // Limit search depth.
3368
3369 KnownBits Known2;
3370 unsigned NumElts = DemandedElts.getBitWidth();
3371 assert((!Op.getValueType().isScalableVector() || NumElts == 1) &&
3372 "DemandedElts for scalable vectors must be 1 to represent all lanes");
3373 assert((!Op.getValueType().isFixedLengthVector() ||
3374 NumElts == Op.getValueType().getVectorNumElements()) &&
3375 "Unexpected vector size");
3376
3377 if (!DemandedElts)
3378 return Known; // No demanded elts, better to assume we don't know anything.
3379
3380 unsigned Opcode = Op.getOpcode();
3381 switch (Opcode) {
3382 case ISD::MERGE_VALUES:
3383 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3384 Depth + 1);
3385 case ISD::SPLAT_VECTOR: {
3386 SDValue SrcOp = Op.getOperand(0);
3387 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3388 "Expected SPLAT_VECTOR implicit truncation");
3389 // Implicitly truncate the bits to match the official semantics of
3390 // SPLAT_VECTOR.
3391 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3392 break;
3393 }
3395 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3396 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3397 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3398 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3399 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3400 }
3401 break;
3402 }
3403 case ISD::STEP_VECTOR: {
3404 const APInt &Step = Op.getConstantOperandAPInt(0);
3405
3406 if (Step.isPowerOf2())
3407 Known.Zero.setLowBits(Step.logBase2());
3408
3410
3411 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3412 break;
3413 const APInt MinNumElts =
3414 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3415
3416 bool Overflow;
3417 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3419 .umul_ov(MinNumElts, Overflow);
3420 if (Overflow)
3421 break;
3422
3423 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3424 if (Overflow)
3425 break;
3426
3427 Known.Zero.setHighBits(MaxValue.countl_zero());
3428 break;
3429 }
3430 case ISD::BUILD_VECTOR:
3431 assert(!Op.getValueType().isScalableVector());
3432 // Collect the known bits that are shared by every demanded vector element.
3433 Known.setAllConflict();
3434 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3435 if (!DemandedElts[i])
3436 continue;
3437
3438 SDValue SrcOp = Op.getOperand(i);
3439 Known2 = computeKnownBits(SrcOp, Depth + 1);
3440
3441 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3442 if (SrcOp.getValueSizeInBits() != BitWidth) {
3443 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3444 "Expected BUILD_VECTOR implicit truncation");
3445 Known2 = Known2.trunc(BitWidth);
3446 }
3447
3448 // Known bits are the values that are shared by every demanded element.
3449 Known = Known.intersectWith(Known2);
3450
3451 // If we don't know any bits, early out.
3452 if (Known.isUnknown())
3453 break;
3454 }
3455 break;
3456 case ISD::VECTOR_COMPRESS: {
3457 SDValue Vec = Op.getOperand(0);
3458 SDValue PassThru = Op.getOperand(2);
3459 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3460 // If we don't know any bits, early out.
3461 if (Known.isUnknown())
3462 break;
3463 Known2 = computeKnownBits(Vec, Depth + 1);
3464 Known = Known.intersectWith(Known2);
3465 break;
3466 }
3467 case ISD::VECTOR_SHUFFLE: {
3468 assert(!Op.getValueType().isScalableVector());
3469 // Collect the known bits that are shared by every vector element referenced
3470 // by the shuffle.
3471 APInt DemandedLHS, DemandedRHS;
3473 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3474 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3475 DemandedLHS, DemandedRHS))
3476 break;
3477
3478 // Known bits are the values that are shared by every demanded element.
3479 Known.setAllConflict();
3480 if (!!DemandedLHS) {
3481 SDValue LHS = Op.getOperand(0);
3482 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3483 Known = Known.intersectWith(Known2);
3484 }
3485 // If we don't know any bits, early out.
3486 if (Known.isUnknown())
3487 break;
3488 if (!!DemandedRHS) {
3489 SDValue RHS = Op.getOperand(1);
3490 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3491 Known = Known.intersectWith(Known2);
3492 }
3493 break;
3494 }
3495 case ISD::VSCALE: {
3497 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3498 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3499 break;
3500 }
3501 case ISD::CONCAT_VECTORS: {
3502 if (Op.getValueType().isScalableVector())
3503 break;
3504 // Split DemandedElts and test each of the demanded subvectors.
3505 Known.setAllConflict();
3506 EVT SubVectorVT = Op.getOperand(0).getValueType();
3507 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3508 unsigned NumSubVectors = Op.getNumOperands();
3509 for (unsigned i = 0; i != NumSubVectors; ++i) {
3510 APInt DemandedSub =
3511 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3512 if (!!DemandedSub) {
3513 SDValue Sub = Op.getOperand(i);
3514 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3515 Known = Known.intersectWith(Known2);
3516 }
3517 // If we don't know any bits, early out.
3518 if (Known.isUnknown())
3519 break;
3520 }
3521 break;
3522 }
3523 case ISD::INSERT_SUBVECTOR: {
3524 if (Op.getValueType().isScalableVector())
3525 break;
3526 // Demand any elements from the subvector and the remainder from the src its
3527 // inserted into.
3528 SDValue Src = Op.getOperand(0);
3529 SDValue Sub = Op.getOperand(1);
3530 uint64_t Idx = Op.getConstantOperandVal(2);
3531 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3532 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3533 APInt DemandedSrcElts = DemandedElts;
3534 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3535
3536 Known.setAllConflict();
3537 if (!!DemandedSubElts) {
3538 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3539 if (Known.isUnknown())
3540 break; // early-out.
3541 }
3542 if (!!DemandedSrcElts) {
3543 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3544 Known = Known.intersectWith(Known2);
3545 }
3546 break;
3547 }
3549 // Offset the demanded elts by the subvector index.
3550 SDValue Src = Op.getOperand(0);
3551
3552 APInt DemandedSrcElts;
3553 if (Src.getValueType().isScalableVector())
3554 DemandedSrcElts = APInt(1, 1); // <=> 'demand all elements'
3555 else {
3556 uint64_t Idx = Op.getConstantOperandVal(1);
3557 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3558 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3559 }
3560 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3561 break;
3562 }
3563 case ISD::SCALAR_TO_VECTOR: {
3564 if (Op.getValueType().isScalableVector())
3565 break;
3566 // We know about scalar_to_vector as much as we know about it source,
3567 // which becomes the first element of otherwise unknown vector.
3568 if (DemandedElts != 1)
3569 break;
3570
3571 SDValue N0 = Op.getOperand(0);
3572 Known = computeKnownBits(N0, Depth + 1);
3573 if (N0.getValueSizeInBits() != BitWidth)
3574 Known = Known.trunc(BitWidth);
3575
3576 break;
3577 }
3578 case ISD::BITCAST: {
3579 if (Op.getValueType().isScalableVector())
3580 break;
3581
3582 SDValue N0 = Op.getOperand(0);
3583 EVT SubVT = N0.getValueType();
3584 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3585
3586 // Ignore bitcasts from unsupported types.
3587 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3588 break;
3589
3590 // Fast handling of 'identity' bitcasts.
3591 if (BitWidth == SubBitWidth) {
3592 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3593 break;
3594 }
3595
3596 bool IsLE = getDataLayout().isLittleEndian();
3597
3598 // Bitcast 'small element' vector to 'large element' scalar/vector.
3599 if ((BitWidth % SubBitWidth) == 0) {
3600 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3601
3602 // Collect known bits for the (larger) output by collecting the known
3603 // bits from each set of sub elements and shift these into place.
3604 // We need to separately call computeKnownBits for each set of
3605 // sub elements as the knownbits for each is likely to be different.
3606 unsigned SubScale = BitWidth / SubBitWidth;
3607 APInt SubDemandedElts(NumElts * SubScale, 0);
3608 for (unsigned i = 0; i != NumElts; ++i)
3609 if (DemandedElts[i])
3610 SubDemandedElts.setBit(i * SubScale);
3611
3612 for (unsigned i = 0; i != SubScale; ++i) {
3613 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3614 Depth + 1);
3615 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3616 Known.insertBits(Known2, SubBitWidth * Shifts);
3617 }
3618 }
3619
3620 // Bitcast 'large element' scalar/vector to 'small element' vector.
3621 if ((SubBitWidth % BitWidth) == 0) {
3622 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3623
3624 // Collect known bits for the (smaller) output by collecting the known
3625 // bits from the overlapping larger input elements and extracting the
3626 // sub sections we actually care about.
3627 unsigned SubScale = SubBitWidth / BitWidth;
3628 APInt SubDemandedElts =
3629 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3630 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3631
3632 Known.setAllConflict();
3633 for (unsigned i = 0; i != NumElts; ++i)
3634 if (DemandedElts[i]) {
3635 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3636 unsigned Offset = (Shifts % SubScale) * BitWidth;
3637 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3638 // If we don't know any bits, early out.
3639 if (Known.isUnknown())
3640 break;
3641 }
3642 }
3643 break;
3644 }
3645 case ISD::AND:
3646 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3647 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3648
3649 Known &= Known2;
3650 break;
3651 case ISD::OR:
3652 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3653 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3654
3655 Known |= Known2;
3656 break;
3657 case ISD::XOR:
3658 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3659 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3660
3661 Known ^= Known2;
3662 break;
3663 case ISD::MUL: {
3664 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3665 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3666 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3667 // TODO: SelfMultiply can be poison, but not undef.
3668 if (SelfMultiply)
3669 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3670 Op.getOperand(0), DemandedElts, false, Depth + 1);
3671 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3672
3673 // If the multiplication is known not to overflow, the product of a number
3674 // with itself is non-negative. Only do this if we didn't already computed
3675 // the opposite value for the sign bit.
3676 if (Op->getFlags().hasNoSignedWrap() &&
3677 Op.getOperand(0) == Op.getOperand(1) &&
3678 !Known.isNegative())
3679 Known.makeNonNegative();
3680 break;
3681 }
3682 case ISD::MULHU: {
3683 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3684 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3685 Known = KnownBits::mulhu(Known, Known2);
3686 break;
3687 }
3688 case ISD::MULHS: {
3689 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3690 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3691 Known = KnownBits::mulhs(Known, Known2);
3692 break;
3693 }
3694 case ISD::ABDU: {
3695 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3696 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3697 Known = KnownBits::abdu(Known, Known2);
3698 break;
3699 }
3700 case ISD::ABDS: {
3701 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3702 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3703 Known = KnownBits::abds(Known, Known2);
3704 unsigned SignBits1 =
3705 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3706 if (SignBits1 == 1)
3707 break;
3708 unsigned SignBits0 =
3709 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3710 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3711 break;
3712 }
3713 case ISD::UMUL_LOHI: {
3714 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3715 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3716 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3717 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3718 if (Op.getResNo() == 0)
3719 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3720 else
3721 Known = KnownBits::mulhu(Known, Known2);
3722 break;
3723 }
3724 case ISD::SMUL_LOHI: {
3725 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3726 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3727 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3728 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3729 if (Op.getResNo() == 0)
3730 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3731 else
3732 Known = KnownBits::mulhs(Known, Known2);
3733 break;
3734 }
3735 case ISD::AVGFLOORU: {
3736 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3737 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3738 Known = KnownBits::avgFloorU(Known, Known2);
3739 break;
3740 }
3741 case ISD::AVGCEILU: {
3742 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3743 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3744 Known = KnownBits::avgCeilU(Known, Known2);
3745 break;
3746 }
3747 case ISD::AVGFLOORS: {
3748 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3749 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3750 Known = KnownBits::avgFloorS(Known, Known2);
3751 break;
3752 }
3753 case ISD::AVGCEILS: {
3754 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3755 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3756 Known = KnownBits::avgCeilS(Known, Known2);
3757 break;
3758 }
3759 case ISD::SELECT:
3760 case ISD::VSELECT:
3761 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3762 // If we don't know any bits, early out.
3763 if (Known.isUnknown())
3764 break;
3765 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3766
3767 // Only known if known in both the LHS and RHS.
3768 Known = Known.intersectWith(Known2);
3769 break;
3770 case ISD::SELECT_CC:
3771 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3772 // If we don't know any bits, early out.
3773 if (Known.isUnknown())
3774 break;
3775 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3776
3777 // Only known if known in both the LHS and RHS.
3778 Known = Known.intersectWith(Known2);
3779 break;
3780 case ISD::SMULO:
3781 case ISD::UMULO:
3782 if (Op.getResNo() != 1)
3783 break;
3784 // The boolean result conforms to getBooleanContents.
3785 // If we know the result of a setcc has the top bits zero, use this info.
3786 // We know that we have an integer-based boolean since these operations
3787 // are only available for integer.
3788 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3790 BitWidth > 1)
3791 Known.Zero.setBitsFrom(1);
3792 break;
3793 case ISD::SETCC:
3794 case ISD::SETCCCARRY:
3795 case ISD::STRICT_FSETCC:
3796 case ISD::STRICT_FSETCCS: {
3797 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3798 // If we know the result of a setcc has the top bits zero, use this info.
3799 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3801 BitWidth > 1)
3802 Known.Zero.setBitsFrom(1);
3803 break;
3804 }
3805 case ISD::SHL: {
3806 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3807 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3808
3809 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3810 bool NSW = Op->getFlags().hasNoSignedWrap();
3811
3812 bool ShAmtNonZero = Known2.isNonZero();
3813
3814 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3815
3816 // Minimum shift low bits are known zero.
3817 if (std::optional<unsigned> ShMinAmt =
3818 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3819 Known.Zero.setLowBits(*ShMinAmt);
3820 break;
3821 }
3822 case ISD::SRL:
3823 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3824 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3825 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3826 Op->getFlags().hasExact());
3827
3828 // Minimum shift high bits are known zero.
3829 if (std::optional<unsigned> ShMinAmt =
3830 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3831 Known.Zero.setHighBits(*ShMinAmt);
3832 break;
3833 case ISD::SRA:
3834 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3835 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3836 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3837 Op->getFlags().hasExact());
3838 break;
3839 case ISD::ROTL:
3840 case ISD::ROTR:
3841 if (ConstantSDNode *C =
3842 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3843 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3844
3845 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3846
3847 // Canonicalize to ROTR.
3848 if (Opcode == ISD::ROTL && Amt != 0)
3849 Amt = BitWidth - Amt;
3850
3851 Known.Zero = Known.Zero.rotr(Amt);
3852 Known.One = Known.One.rotr(Amt);
3853 }
3854 break;
3855 case ISD::FSHL:
3856 case ISD::FSHR:
3857 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3858 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3859
3860 // For fshl, 0-shift returns the 1st arg.
3861 // For fshr, 0-shift returns the 2nd arg.
3862 if (Amt == 0) {
3863 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3864 DemandedElts, Depth + 1);
3865 break;
3866 }
3867
3868 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3869 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3870 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3871 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3872 if (Opcode == ISD::FSHL) {
3873 Known <<= Amt;
3874 Known2 >>= BitWidth - Amt;
3875 } else {
3876 Known <<= BitWidth - Amt;
3877 Known2 >>= Amt;
3878 }
3879 Known = Known.unionWith(Known2);
3880 }
3881 break;
3882 case ISD::SHL_PARTS:
3883 case ISD::SRA_PARTS:
3884 case ISD::SRL_PARTS: {
3885 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3886
3887 // Collect lo/hi source values and concatenate.
3888 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3889 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3890 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3891 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3892 Known = Known2.concat(Known);
3893
3894 // Collect shift amount.
3895 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3896
3897 if (Opcode == ISD::SHL_PARTS)
3898 Known = KnownBits::shl(Known, Known2);
3899 else if (Opcode == ISD::SRA_PARTS)
3900 Known = KnownBits::ashr(Known, Known2);
3901 else // if (Opcode == ISD::SRL_PARTS)
3902 Known = KnownBits::lshr(Known, Known2);
3903
3904 // TODO: Minimum shift low/high bits are known zero.
3905
3906 if (Op.getResNo() == 0)
3907 Known = Known.extractBits(LoBits, 0);
3908 else
3909 Known = Known.extractBits(HiBits, LoBits);
3910 break;
3911 }
3913 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3914 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3915 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3916 break;
3917 }
3918 case ISD::CTTZ:
3919 case ISD::CTTZ_ZERO_UNDEF: {
3920 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3921 // If we have a known 1, its position is our upper bound.
3922 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3923 unsigned LowBits = llvm::bit_width(PossibleTZ);
3924 Known.Zero.setBitsFrom(LowBits);
3925 break;
3926 }
3927 case ISD::CTLZ:
3928 case ISD::CTLZ_ZERO_UNDEF: {
3929 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3930 // If we have a known 1, its position is our upper bound.
3931 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3932 unsigned LowBits = llvm::bit_width(PossibleLZ);
3933 Known.Zero.setBitsFrom(LowBits);
3934 break;
3935 }
3936 case ISD::CTLS: {
3937 unsigned MinRedundantSignBits =
3938 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1;
3939 ConstantRange Range(APInt(BitWidth, MinRedundantSignBits),
3941 Known = Range.toKnownBits();
3942 break;
3943 }
3944 case ISD::CTPOP: {
3945 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3946 // If we know some of the bits are zero, they can't be one.
3947 unsigned PossibleOnes = Known2.countMaxPopulation();
3948 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3949 break;
3950 }
3951 case ISD::PARITY: {
3952 // Parity returns 0 everywhere but the LSB.
3953 Known.Zero.setBitsFrom(1);
3954 break;
3955 }
3956 case ISD::CLMUL: {
3957 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3958 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3959 Known = KnownBits::clmul(Known, Known2);
3960 break;
3961 }
3962 case ISD::MGATHER:
3963 case ISD::MLOAD: {
3964 ISD::LoadExtType ETy =
3965 (Opcode == ISD::MGATHER)
3966 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3967 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3968 if (ETy == ISD::ZEXTLOAD) {
3969 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3970 KnownBits Known0(MemVT.getScalarSizeInBits());
3971 return Known0.zext(BitWidth);
3972 }
3973 break;
3974 }
3975 case ISD::LOAD: {
3977 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3978 if (ISD::isNON_EXTLoad(LD) && Cst) {
3979 // Determine any common known bits from the loaded constant pool value.
3980 Type *CstTy = Cst->getType();
3981 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3982 !Op.getValueType().isScalableVector()) {
3983 // If its a vector splat, then we can (quickly) reuse the scalar path.
3984 // NOTE: We assume all elements match and none are UNDEF.
3985 if (CstTy->isVectorTy()) {
3986 if (const Constant *Splat = Cst->getSplatValue()) {
3987 Cst = Splat;
3988 CstTy = Cst->getType();
3989 }
3990 }
3991 // TODO - do we need to handle different bitwidths?
3992 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3993 // Iterate across all vector elements finding common known bits.
3994 Known.setAllConflict();
3995 for (unsigned i = 0; i != NumElts; ++i) {
3996 if (!DemandedElts[i])
3997 continue;
3998 if (Constant *Elt = Cst->getAggregateElement(i)) {
3999 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
4000 const APInt &Value = CInt->getValue();
4001 Known.One &= Value;
4002 Known.Zero &= ~Value;
4003 continue;
4004 }
4005 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
4006 APInt Value = CFP->getValueAPF().bitcastToAPInt();
4007 Known.One &= Value;
4008 Known.Zero &= ~Value;
4009 continue;
4010 }
4011 }
4012 Known.One.clearAllBits();
4013 Known.Zero.clearAllBits();
4014 break;
4015 }
4016 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
4017 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
4018 Known = KnownBits::makeConstant(CInt->getValue());
4019 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
4020 Known =
4021 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
4022 }
4023 }
4024 }
4025 } else if (Op.getResNo() == 0) {
4026 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
4027 KnownBits KnownScalarMemory(ScalarMemorySize);
4028 if (const MDNode *MD = LD->getRanges())
4029 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4030
4031 // Extend the Known bits from memory to the size of the scalar result.
4032 if (ISD::isZEXTLoad(Op.getNode()))
4033 Known = KnownScalarMemory.zext(BitWidth);
4034 else if (ISD::isSEXTLoad(Op.getNode()))
4035 Known = KnownScalarMemory.sext(BitWidth);
4036 else if (ISD::isEXTLoad(Op.getNode()))
4037 Known = KnownScalarMemory.anyext(BitWidth);
4038 else
4039 Known = KnownScalarMemory;
4040 assert(Known.getBitWidth() == BitWidth);
4041 return Known;
4042 }
4043 break;
4044 }
4046 if (Op.getValueType().isScalableVector())
4047 break;
4048 EVT InVT = Op.getOperand(0).getValueType();
4049 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4050 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4051 Known = Known.zext(BitWidth);
4052 break;
4053 }
4054 case ISD::ZERO_EXTEND: {
4055 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4056 Known = Known.zext(BitWidth);
4057 break;
4058 }
4060 if (Op.getValueType().isScalableVector())
4061 break;
4062 EVT InVT = Op.getOperand(0).getValueType();
4063 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4064 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4065 // If the sign bit is known to be zero or one, then sext will extend
4066 // it to the top bits, else it will just zext.
4067 Known = Known.sext(BitWidth);
4068 break;
4069 }
4070 case ISD::SIGN_EXTEND: {
4071 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4072 // If the sign bit is known to be zero or one, then sext will extend
4073 // it to the top bits, else it will just zext.
4074 Known = Known.sext(BitWidth);
4075 break;
4076 }
4078 if (Op.getValueType().isScalableVector())
4079 break;
4080 EVT InVT = Op.getOperand(0).getValueType();
4081 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4082 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4083 Known = Known.anyext(BitWidth);
4084 break;
4085 }
4086 case ISD::ANY_EXTEND: {
4087 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4088 Known = Known.anyext(BitWidth);
4089 break;
4090 }
4091 case ISD::TRUNCATE: {
4092 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4093 Known = Known.trunc(BitWidth);
4094 break;
4095 }
4096 case ISD::TRUNCATE_SSAT_S: {
4097 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4098 Known = Known.truncSSat(BitWidth);
4099 break;
4100 }
4101 case ISD::TRUNCATE_SSAT_U: {
4102 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4103 Known = Known.truncSSatU(BitWidth);
4104 break;
4105 }
4106 case ISD::TRUNCATE_USAT_U: {
4107 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4108 Known = Known.truncUSat(BitWidth);
4109 break;
4110 }
4111 case ISD::AssertZext: {
4112 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4114 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4115 Known.Zero |= (~InMask);
4116 Known.One &= (~Known.Zero);
4117 break;
4118 }
4119 case ISD::AssertAlign: {
4120 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4121 assert(LogOfAlign != 0);
4122
4123 // TODO: Should use maximum with source
4124 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4125 // well as clearing one bits.
4126 Known.Zero.setLowBits(LogOfAlign);
4127 Known.One.clearLowBits(LogOfAlign);
4128 break;
4129 }
4130 case ISD::AssertNoFPClass: {
4131 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4132
4133 FPClassTest NoFPClass =
4134 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
4135 const FPClassTest NegativeTestMask = fcNan | fcNegative;
4136 if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
4137 // Cannot be negative.
4138 Known.makeNonNegative();
4139 }
4140
4141 const FPClassTest PositiveTestMask = fcNan | fcPositive;
4142 if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
4143 // Cannot be positive.
4144 Known.makeNegative();
4145 }
4146
4147 break;
4148 }
4149 case ISD::FABS:
4150 // fabs clears the sign bit
4151 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4152 Known.makeNonNegative();
4153 break;
4154 case ISD::FGETSIGN:
4155 // All bits are zero except the low bit.
4156 Known.Zero.setBitsFrom(1);
4157 break;
4158 case ISD::ADD: {
4159 SDNodeFlags Flags = Op.getNode()->getFlags();
4160 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4161 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4162 bool SelfAdd = Op.getOperand(0) == Op.getOperand(1) &&
4164 Op.getOperand(0), DemandedElts, false, Depth + 1);
4165 Known = KnownBits::add(Known, Known2, Flags.hasNoSignedWrap(),
4166 Flags.hasNoUnsignedWrap(), SelfAdd);
4167 break;
4168 }
4169 case ISD::SUB: {
4170 SDNodeFlags Flags = Op.getNode()->getFlags();
4171 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4172 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4173 Known = KnownBits::sub(Known, Known2, Flags.hasNoSignedWrap(),
4174 Flags.hasNoUnsignedWrap());
4175 break;
4176 }
4177 case ISD::USUBO:
4178 case ISD::SSUBO:
4179 case ISD::USUBO_CARRY:
4180 case ISD::SSUBO_CARRY:
4181 if (Op.getResNo() == 1) {
4182 // If we know the result of a setcc has the top bits zero, use this info.
4183 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4185 BitWidth > 1)
4186 Known.Zero.setBitsFrom(1);
4187 break;
4188 }
4189 [[fallthrough]];
4190 case ISD::SUBC: {
4191 assert(Op.getResNo() == 0 &&
4192 "We only compute knownbits for the difference here.");
4193
4194 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4195 KnownBits Borrow(1);
4196 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4197 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4198 // Borrow has bit width 1
4199 Borrow = Borrow.trunc(1);
4200 } else {
4201 Borrow.setAllZero();
4202 }
4203
4204 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4205 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4206 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4207 break;
4208 }
4209 case ISD::UADDO:
4210 case ISD::SADDO:
4211 case ISD::UADDO_CARRY:
4212 case ISD::SADDO_CARRY:
4213 if (Op.getResNo() == 1) {
4214 // If we know the result of a setcc has the top bits zero, use this info.
4215 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4217 BitWidth > 1)
4218 Known.Zero.setBitsFrom(1);
4219 break;
4220 }
4221 [[fallthrough]];
4222 case ISD::ADDC:
4223 case ISD::ADDE: {
4224 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4225
4226 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4227 KnownBits Carry(1);
4228 if (Opcode == ISD::ADDE)
4229 // Can't track carry from glue, set carry to unknown.
4230 Carry.resetAll();
4231 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4232 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4233 // Carry has bit width 1
4234 Carry = Carry.trunc(1);
4235 } else {
4236 Carry.setAllZero();
4237 }
4238
4239 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4240 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4241 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4242 break;
4243 }
4244 case ISD::UDIV: {
4245 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4246 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4247 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4248 break;
4249 }
4250 case ISD::SDIV: {
4251 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4252 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4253 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4254 break;
4255 }
4256 case ISD::SREM: {
4257 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4258 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4259 Known = KnownBits::srem(Known, Known2);
4260 break;
4261 }
4262 case ISD::UREM: {
4263 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4264 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4265 Known = KnownBits::urem(Known, Known2);
4266 break;
4267 }
4268 case ISD::EXTRACT_ELEMENT: {
4269 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4270 const unsigned Index = Op.getConstantOperandVal(1);
4271 const unsigned EltBitWidth = Op.getValueSizeInBits();
4272
4273 // Remove low part of known bits mask
4274 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4275 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4276
4277 // Remove high part of known bit mask
4278 Known = Known.trunc(EltBitWidth);
4279 break;
4280 }
4282 SDValue InVec = Op.getOperand(0);
4283 SDValue EltNo = Op.getOperand(1);
4284 EVT VecVT = InVec.getValueType();
4285 // computeKnownBits not yet implemented for scalable vectors.
4286 if (VecVT.isScalableVector())
4287 break;
4288 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4289 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4290
4291 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4292 // anything about the extended bits.
4293 if (BitWidth > EltBitWidth)
4294 Known = Known.trunc(EltBitWidth);
4295
4296 // If we know the element index, just demand that vector element, else for
4297 // an unknown element index, ignore DemandedElts and demand them all.
4298 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4299 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4300 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4301 DemandedSrcElts =
4302 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4303
4304 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4305 if (BitWidth > EltBitWidth)
4306 Known = Known.anyext(BitWidth);
4307 break;
4308 }
4310 if (Op.getValueType().isScalableVector())
4311 break;
4312
4313 // If we know the element index, split the demand between the
4314 // source vector and the inserted element, otherwise assume we need
4315 // the original demanded vector elements and the value.
4316 SDValue InVec = Op.getOperand(0);
4317 SDValue InVal = Op.getOperand(1);
4318 SDValue EltNo = Op.getOperand(2);
4319 bool DemandedVal = true;
4320 APInt DemandedVecElts = DemandedElts;
4321 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4322 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4323 unsigned EltIdx = CEltNo->getZExtValue();
4324 DemandedVal = !!DemandedElts[EltIdx];
4325 DemandedVecElts.clearBit(EltIdx);
4326 }
4327 Known.setAllConflict();
4328 if (DemandedVal) {
4329 Known2 = computeKnownBits(InVal, Depth + 1);
4330 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4331 }
4332 if (!!DemandedVecElts) {
4333 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4334 Known = Known.intersectWith(Known2);
4335 }
4336 break;
4337 }
4338 case ISD::BITREVERSE: {
4339 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4340 Known = Known2.reverseBits();
4341 break;
4342 }
4343 case ISD::BSWAP: {
4344 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4345 Known = Known2.byteSwap();
4346 break;
4347 }
4348 case ISD::ABS: {
4349 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4350 Known = Known2.abs();
4351 Known.Zero.setHighBits(
4352 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4353 break;
4354 }
4355 case ISD::USUBSAT: {
4356 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4357 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4358 Known = KnownBits::usub_sat(Known, Known2);
4359 break;
4360 }
4361 case ISD::UMIN: {
4362 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4363 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4364 Known = KnownBits::umin(Known, Known2);
4365 break;
4366 }
4367 case ISD::UMAX: {
4368 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4369 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4370 Known = KnownBits::umax(Known, Known2);
4371 break;
4372 }
4373 case ISD::SMIN:
4374 case ISD::SMAX: {
4375 // If we have a clamp pattern, we know that the number of sign bits will be
4376 // the minimum of the clamp min/max range.
4377 bool IsMax = (Opcode == ISD::SMAX);
4378 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4379 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4380 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4381 CstHigh =
4382 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4383 if (CstLow && CstHigh) {
4384 if (!IsMax)
4385 std::swap(CstLow, CstHigh);
4386
4387 const APInt &ValueLow = CstLow->getAPIntValue();
4388 const APInt &ValueHigh = CstHigh->getAPIntValue();
4389 if (ValueLow.sle(ValueHigh)) {
4390 unsigned LowSignBits = ValueLow.getNumSignBits();
4391 unsigned HighSignBits = ValueHigh.getNumSignBits();
4392 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4393 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4394 Known.One.setHighBits(MinSignBits);
4395 break;
4396 }
4397 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4398 Known.Zero.setHighBits(MinSignBits);
4399 break;
4400 }
4401 }
4402 }
4403
4404 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4405 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4406 if (IsMax)
4407 Known = KnownBits::smax(Known, Known2);
4408 else
4409 Known = KnownBits::smin(Known, Known2);
4410
4411 // For SMAX, if CstLow is non-negative we know the result will be
4412 // non-negative and thus all sign bits are 0.
4413 // TODO: There's an equivalent of this for smin with negative constant for
4414 // known ones.
4415 if (IsMax && CstLow) {
4416 const APInt &ValueLow = CstLow->getAPIntValue();
4417 if (ValueLow.isNonNegative()) {
4418 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4419 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4420 }
4421 }
4422
4423 break;
4424 }
4425 case ISD::UINT_TO_FP: {
4426 Known.makeNonNegative();
4427 break;
4428 }
4429 case ISD::SINT_TO_FP: {
4430 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4431 if (Known2.isNonNegative())
4432 Known.makeNonNegative();
4433 else if (Known2.isNegative())
4434 Known.makeNegative();
4435 break;
4436 }
4437 case ISD::FP_TO_UINT_SAT: {
4438 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4439 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4441 break;
4442 }
4443 case ISD::ATOMIC_LOAD: {
4444 // If we are looking at the loaded value.
4445 if (Op.getResNo() == 0) {
4446 auto *AT = cast<AtomicSDNode>(Op);
4447 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4448 KnownBits KnownScalarMemory(ScalarMemorySize);
4449 if (const MDNode *MD = AT->getRanges())
4450 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4451
4452 switch (AT->getExtensionType()) {
4453 case ISD::ZEXTLOAD:
4454 Known = KnownScalarMemory.zext(BitWidth);
4455 break;
4456 case ISD::SEXTLOAD:
4457 Known = KnownScalarMemory.sext(BitWidth);
4458 break;
4459 case ISD::EXTLOAD:
4460 switch (TLI->getExtendForAtomicOps()) {
4461 case ISD::ZERO_EXTEND:
4462 Known = KnownScalarMemory.zext(BitWidth);
4463 break;
4464 case ISD::SIGN_EXTEND:
4465 Known = KnownScalarMemory.sext(BitWidth);
4466 break;
4467 default:
4468 Known = KnownScalarMemory.anyext(BitWidth);
4469 break;
4470 }
4471 break;
4472 case ISD::NON_EXTLOAD:
4473 Known = KnownScalarMemory;
4474 break;
4475 }
4476 assert(Known.getBitWidth() == BitWidth);
4477 }
4478 break;
4479 }
4481 if (Op.getResNo() == 1) {
4482 // The boolean result conforms to getBooleanContents.
4483 // If we know the result of a setcc has the top bits zero, use this info.
4484 // We know that we have an integer-based boolean since these operations
4485 // are only available for integer.
4486 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4488 BitWidth > 1)
4489 Known.Zero.setBitsFrom(1);
4490 break;
4491 }
4492 [[fallthrough]];
4494 case ISD::ATOMIC_SWAP:
4505 case ISD::ATOMIC_LOAD_UMAX: {
4506 // If we are looking at the loaded value.
4507 if (Op.getResNo() == 0) {
4508 auto *AT = cast<AtomicSDNode>(Op);
4509 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4510
4511 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4512 Known.Zero.setBitsFrom(MemBits);
4513 }
4514 break;
4515 }
4516 case ISD::FrameIndex:
4518 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4519 Known, getMachineFunction());
4520 break;
4521
4522 default:
4523 if (Opcode < ISD::BUILTIN_OP_END)
4524 break;
4525 [[fallthrough]];
4529 // TODO: Probably okay to remove after audit; here to reduce change size
4530 // in initial enablement patch for scalable vectors
4531 if (Op.getValueType().isScalableVector())
4532 break;
4533
4534 // Allow the target to implement this method for its nodes.
4535 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4536 break;
4537 }
4538
4539 return Known;
4540}
4541
4542/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4555
4558 // X + 0 never overflow
4559 if (isNullConstant(N1))
4560 return OFK_Never;
4561
4562 // If both operands each have at least two sign bits, the addition
4563 // cannot overflow.
4564 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4565 return OFK_Never;
4566
4567 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4568 return OFK_Sometime;
4569}
4570
4573 // X + 0 never overflow
4574 if (isNullConstant(N1))
4575 return OFK_Never;
4576
4577 // mulhi + 1 never overflow
4578 KnownBits N1Known = computeKnownBits(N1);
4579 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4580 N1Known.getMaxValue().ult(2))
4581 return OFK_Never;
4582
4583 KnownBits N0Known = computeKnownBits(N0);
4584 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4585 N0Known.getMaxValue().ult(2))
4586 return OFK_Never;
4587
4588 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4589 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4590 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4591 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4592}
4593
4596 // X - 0 never overflow
4597 if (isNullConstant(N1))
4598 return OFK_Never;
4599
4600 // If both operands each have at least two sign bits, the subtraction
4601 // cannot overflow.
4602 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4603 return OFK_Never;
4604
4605 KnownBits N0Known = computeKnownBits(N0);
4606 KnownBits N1Known = computeKnownBits(N1);
4607 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4608 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4609 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4610}
4611
4614 // X - 0 never overflow
4615 if (isNullConstant(N1))
4616 return OFK_Never;
4617
4618 ConstantRange N0Range =
4619 computeConstantRangeIncludingKnownBits(N0, /*ForSigned=*/false);
4620 ConstantRange N1Range =
4621 computeConstantRangeIncludingKnownBits(N1, /*ForSigned=*/false);
4622 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4623}
4624
4627 // X * 0 and X * 1 never overflow.
4628 if (isNullConstant(N1) || isOneConstant(N1))
4629 return OFK_Never;
4630
4633 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4634}
4635
4638 // X * 0 and X * 1 never overflow.
4639 if (isNullConstant(N1) || isOneConstant(N1))
4640 return OFK_Never;
4641
4642 // Get the size of the result.
4643 unsigned BitWidth = N0.getScalarValueSizeInBits();
4644
4645 // Sum of the sign bits.
4646 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4647
4648 // If we have enough sign bits, then there's no overflow.
4649 if (SignBits > BitWidth + 1)
4650 return OFK_Never;
4651
4652 if (SignBits == BitWidth + 1) {
4653 // The overflow occurs when the true multiplication of the
4654 // the operands is the minimum negative number.
4655 KnownBits N0Known = computeKnownBits(N0);
4656 KnownBits N1Known = computeKnownBits(N1);
4657 // If one of the operands is non-negative, then there's no
4658 // overflow.
4659 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4660 return OFK_Never;
4661 }
4662
4663 return OFK_Sometime;
4664}
4665
4667 unsigned Depth) const {
4668 APInt DemandedElts = getDemandAllEltsMask(Op);
4669 return computeConstantRange(Op, DemandedElts, ForSigned, Depth);
4670}
4671
4673 const APInt &DemandedElts,
4674 bool ForSigned,
4675 unsigned Depth) const {
4676 EVT VT = Op.getValueType();
4677 unsigned BitWidth = VT.getScalarSizeInBits();
4678
4679 if (Depth >= MaxRecursionDepth)
4680 return ConstantRange::getFull(BitWidth);
4681
4682 if (ConstantSDNode *C = isConstOrConstSplat(Op, DemandedElts))
4683 return ConstantRange(C->getAPIntValue());
4684
4685 unsigned Opcode = Op.getOpcode();
4686 switch (Opcode) {
4687 case ISD::VSCALE: {
4689 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
4690 return getVScaleRange(&F, BitWidth).multiply(Multiplier);
4691 }
4692 default:
4693 break;
4694 }
4695
4696 return ConstantRange::getFull(BitWidth);
4697}
4698
4701 unsigned Depth) const {
4702 APInt DemandedElts = getDemandAllEltsMask(Op);
4703 return computeConstantRangeIncludingKnownBits(Op, DemandedElts, ForSigned,
4704 Depth);
4705}
4706
4708 SDValue Op, const APInt &DemandedElts, bool ForSigned,
4709 unsigned Depth) const {
4710 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
4711 ConstantRange CR1 = ConstantRange::fromKnownBits(Known, ForSigned);
4712 ConstantRange CR2 = computeConstantRange(Op, DemandedElts, ForSigned, Depth);
4715 return CR1.intersectWith(CR2, RangeType);
4716}
4717
4719 unsigned Depth) const {
4720 APInt DemandedElts = getDemandAllEltsMask(Val);
4721 return isKnownToBeAPowerOfTwo(Val, DemandedElts, OrZero, Depth);
4722}
4723
4725 const APInt &DemandedElts,
4726 bool OrZero, unsigned Depth) const {
4727 if (Depth >= MaxRecursionDepth)
4728 return false; // Limit search depth.
4729
4730 EVT OpVT = Val.getValueType();
4731 unsigned BitWidth = OpVT.getScalarSizeInBits();
4732 [[maybe_unused]] unsigned NumElts = DemandedElts.getBitWidth();
4733 assert((!OpVT.isScalableVector() || NumElts == 1) &&
4734 "DemandedElts for scalable vectors must be 1 to represent all lanes");
4735 assert(
4736 (!OpVT.isFixedLengthVector() || NumElts == OpVT.getVectorNumElements()) &&
4737 "Unexpected vector size");
4738
4739 auto IsPowerOfTwoOrZero = [BitWidth, OrZero](const ConstantSDNode *C) {
4740 APInt V = C->getAPIntValue().zextOrTrunc(BitWidth);
4741 return (OrZero && V.isZero()) || V.isPowerOf2();
4742 };
4743
4744 // Is the constant a known power of 2 or zero?
4745 if (ISD::matchUnaryPredicate(Val, IsPowerOfTwoOrZero))
4746 return true;
4747
4748 switch (Val.getOpcode()) {
4749 case ISD::BUILD_VECTOR:
4750 // Are all operands of a build vector constant powers of two or zero?
4751 if (all_of(enumerate(Val->ops()), [&](auto P) {
4752 auto *C = dyn_cast<ConstantSDNode>(P.value());
4753 return !DemandedElts[P.index()] || (C && IsPowerOfTwoOrZero(C));
4754 }))
4755 return true;
4756 break;
4757
4758 case ISD::SPLAT_VECTOR:
4759 // Is the operand of a splat vector a constant power of two?
4760 if (auto *C = dyn_cast<ConstantSDNode>(Val->getOperand(0)))
4761 if (IsPowerOfTwoOrZero(C))
4762 return true;
4763 break;
4764
4766 SDValue InVec = Val.getOperand(0);
4767 SDValue EltNo = Val.getOperand(1);
4768 EVT VecVT = InVec.getValueType();
4769
4770 // Skip scalable vectors or implicit extensions.
4771 if (VecVT.isScalableVector() ||
4772 OpVT.getScalarSizeInBits() != VecVT.getScalarSizeInBits())
4773 break;
4774
4775 // If we know the element index, just demand that vector element, else for
4776 // an unknown element index, ignore DemandedElts and demand them all.
4777 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4778 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4779 APInt DemandedSrcElts =
4780 ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts)
4781 ? APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue())
4782 : APInt::getAllOnes(NumSrcElts);
4783 return isKnownToBeAPowerOfTwo(InVec, DemandedSrcElts, OrZero, Depth + 1);
4784 }
4785
4786 case ISD::AND: {
4787 // Looking for `x & -x` pattern:
4788 // If x == 0:
4789 // x & -x -> 0
4790 // If x != 0:
4791 // x & -x -> non-zero pow2
4792 // so if we find the pattern return whether we know `x` is non-zero.
4793 SDValue X, Z;
4794 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))) ||
4795 (sd_match(Val, m_And(m_Value(X), m_Sub(m_Value(Z), m_Deferred(X)))) &&
4796 MaskedVectorIsZero(Z, DemandedElts, Depth + 1)))
4797 return OrZero || isKnownNeverZero(X, DemandedElts, Depth);
4798 break;
4799 }
4800
4801 case ISD::SHL: {
4802 // A left-shift of a constant one will have exactly one bit set because
4803 // shifting the bit off the end is undefined.
4804 auto *C = isConstOrConstSplat(Val.getOperand(0), DemandedElts);
4805 if (C && C->getAPIntValue() == 1)
4806 return true;
4807 return (OrZero || isKnownNeverZero(Val, DemandedElts, Depth)) &&
4808 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4809 Depth + 1);
4810 }
4811
4812 case ISD::SRL: {
4813 // A logical right-shift of a constant sign-bit will have exactly
4814 // one bit set.
4815 auto *C = isConstOrConstSplat(Val.getOperand(0), DemandedElts);
4816 if (C && C->getAPIntValue().isSignMask())
4817 return true;
4818 return (OrZero || isKnownNeverZero(Val, DemandedElts, Depth)) &&
4819 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4820 Depth + 1);
4821 }
4822
4823 case ISD::TRUNCATE:
4824 return (OrZero || isKnownNeverZero(Val, DemandedElts, Depth)) &&
4825 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4826 Depth + 1);
4827
4828 case ISD::ROTL:
4829 case ISD::ROTR:
4830 return isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4831 Depth + 1);
4832 case ISD::BSWAP:
4833 case ISD::BITREVERSE:
4834 return isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4835 Depth + 1);
4836
4837 case ISD::SMIN:
4838 case ISD::SMAX:
4839 case ISD::UMIN:
4840 case ISD::UMAX:
4841 return isKnownToBeAPowerOfTwo(Val.getOperand(1), DemandedElts, OrZero,
4842 Depth + 1) &&
4843 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4844 Depth + 1);
4845
4846 case ISD::SELECT:
4847 case ISD::VSELECT:
4848 return isKnownToBeAPowerOfTwo(Val.getOperand(2), DemandedElts, OrZero,
4849 Depth + 1) &&
4850 isKnownToBeAPowerOfTwo(Val.getOperand(1), DemandedElts, OrZero,
4851 Depth + 1);
4852
4853 case ISD::ZERO_EXTEND:
4854 return isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
4855 Depth + 1);
4856
4857 case ISD::VSCALE:
4858 // vscale(power-of-two) is a power-of-two
4859 return isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
4860 Depth + 1);
4861
4862 case ISD::VECTOR_SHUFFLE: {
4864 // Demanded elements with undef shuffle mask elements are unknown
4865 // - we cannot guarantee they are a power of two, so return false.
4866 APInt DemandedLHS, DemandedRHS;
4868 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4869 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4870 DemandedLHS, DemandedRHS))
4871 return false;
4872
4873 // All demanded elements from LHS must be known power of two.
4874 if (!!DemandedLHS && !isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedLHS,
4875 OrZero, Depth + 1))
4876 return false;
4877
4878 // All demanded elements from RHS must be known power of two.
4879 if (!!DemandedRHS && !isKnownToBeAPowerOfTwo(Val.getOperand(1), DemandedRHS,
4880 OrZero, Depth + 1))
4881 return false;
4882
4883 return true;
4884 }
4885 }
4886
4887 // More could be done here, though the above checks are enough
4888 // to handle some common cases.
4889 return false;
4890}
4891
4893 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4894 return C1->getValueAPF().getExactLog2Abs() >= 0;
4895
4896 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4897 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4898
4899 return false;
4900}
4901
4903 APInt DemandedElts = getDemandAllEltsMask(Op);
4904 return ComputeNumSignBits(Op, DemandedElts, Depth);
4905}
4906
4907unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4908 unsigned Depth) const {
4909 EVT VT = Op.getValueType();
4910 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4911 unsigned VTBits = VT.getScalarSizeInBits();
4912 unsigned NumElts = DemandedElts.getBitWidth();
4913 unsigned Tmp, Tmp2;
4914 unsigned FirstAnswer = 1;
4915
4916 assert((!VT.isScalableVector() || NumElts == 1) &&
4917 "DemandedElts for scalable vectors must be 1 to represent all lanes");
4918
4919 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4920 const APInt &Val = C->getAPIntValue();
4921 return Val.getNumSignBits();
4922 }
4923
4924 if (Depth >= MaxRecursionDepth)
4925 return 1; // Limit search depth.
4926
4927 if (!DemandedElts)
4928 return 1; // No demanded elts, better to assume we don't know anything.
4929
4930 unsigned Opcode = Op.getOpcode();
4931 switch (Opcode) {
4932 default: break;
4933 case ISD::AssertSext:
4934 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4935 return VTBits-Tmp+1;
4936 case ISD::AssertZext:
4937 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4938 return VTBits-Tmp;
4939 case ISD::FREEZE:
4940 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4941 /*PoisonOnly=*/false))
4942 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4943 break;
4944 case ISD::MERGE_VALUES:
4945 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4946 Depth + 1);
4947 case ISD::SPLAT_VECTOR: {
4948 // Check if the sign bits of source go down as far as the truncated value.
4949 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4950 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4951 if (NumSrcSignBits > (NumSrcBits - VTBits))
4952 return NumSrcSignBits - (NumSrcBits - VTBits);
4953 break;
4954 }
4955 case ISD::BUILD_VECTOR:
4956 assert(!VT.isScalableVector());
4957 Tmp = VTBits;
4958 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4959 if (!DemandedElts[i])
4960 continue;
4961
4962 SDValue SrcOp = Op.getOperand(i);
4963 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4964 // for constant nodes to ensure we only look at the sign bits.
4966 APInt T = C->getAPIntValue().trunc(VTBits);
4967 Tmp2 = T.getNumSignBits();
4968 } else {
4969 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4970
4971 if (SrcOp.getValueSizeInBits() != VTBits) {
4972 assert(SrcOp.getValueSizeInBits() > VTBits &&
4973 "Expected BUILD_VECTOR implicit truncation");
4974 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4975 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4976 }
4977 }
4978 Tmp = std::min(Tmp, Tmp2);
4979 }
4980 return Tmp;
4981
4982 case ISD::VECTOR_COMPRESS: {
4983 SDValue Vec = Op.getOperand(0);
4984 SDValue PassThru = Op.getOperand(2);
4985 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4986 if (Tmp == 1)
4987 return 1;
4988 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
4989 Tmp = std::min(Tmp, Tmp2);
4990 return Tmp;
4991 }
4992
4993 case ISD::VECTOR_SHUFFLE: {
4994 // Collect the minimum number of sign bits that are shared by every vector
4995 // element referenced by the shuffle.
4996 APInt DemandedLHS, DemandedRHS;
4998 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4999 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
5000 DemandedLHS, DemandedRHS))
5001 return 1;
5002
5003 Tmp = std::numeric_limits<unsigned>::max();
5004 if (!!DemandedLHS)
5005 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
5006 if (!!DemandedRHS) {
5007 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
5008 Tmp = std::min(Tmp, Tmp2);
5009 }
5010 // If we don't know anything, early out and try computeKnownBits fall-back.
5011 if (Tmp == 1)
5012 break;
5013 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5014 return Tmp;
5015 }
5016
5017 case ISD::BITCAST: {
5018 if (VT.isScalableVector())
5019 break;
5020 SDValue N0 = Op.getOperand(0);
5021 EVT SrcVT = N0.getValueType();
5022 unsigned SrcBits = SrcVT.getScalarSizeInBits();
5023
5024 // Ignore bitcasts from unsupported types..
5025 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
5026 break;
5027
5028 // Fast handling of 'identity' bitcasts.
5029 if (VTBits == SrcBits)
5030 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
5031
5032 bool IsLE = getDataLayout().isLittleEndian();
5033
5034 // Bitcast 'large element' scalar/vector to 'small element' vector.
5035 if ((SrcBits % VTBits) == 0) {
5036 assert(VT.isVector() && "Expected bitcast to vector");
5037
5038 unsigned Scale = SrcBits / VTBits;
5039 APInt SrcDemandedElts =
5040 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
5041
5042 // Fast case - sign splat can be simply split across the small elements.
5043 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
5044 if (Tmp == SrcBits)
5045 return VTBits;
5046
5047 // Slow case - determine how far the sign extends into each sub-element.
5048 Tmp2 = VTBits;
5049 for (unsigned i = 0; i != NumElts; ++i)
5050 if (DemandedElts[i]) {
5051 unsigned SubOffset = i % Scale;
5052 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
5053 SubOffset = SubOffset * VTBits;
5054 if (Tmp <= SubOffset)
5055 return 1;
5056 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
5057 }
5058 return Tmp2;
5059 }
5060 break;
5061 }
5062
5064 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
5065 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
5066 return VTBits - Tmp + 1;
5067 case ISD::SIGN_EXTEND:
5068 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
5069 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
5071 // Max of the input and what this extends.
5072 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
5073 Tmp = VTBits-Tmp+1;
5074 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
5075 return std::max(Tmp, Tmp2);
5077 if (VT.isScalableVector())
5078 break;
5079 SDValue Src = Op.getOperand(0);
5080 EVT SrcVT = Src.getValueType();
5081 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
5082 Tmp = VTBits - SrcVT.getScalarSizeInBits();
5083 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
5084 }
5085 case ISD::SRA:
5086 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5087 // SRA X, C -> adds C sign bits.
5088 if (std::optional<unsigned> ShAmt =
5089 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
5090 Tmp = std::min(Tmp + *ShAmt, VTBits);
5091 return Tmp;
5092 case ISD::SHL:
5093 if (std::optional<ConstantRange> ShAmtRange =
5094 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
5095 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
5096 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
5097 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
5098 // shifted out, then we can compute the number of sign bits for the
5099 // operand being extended. A future improvement could be to pass along the
5100 // "shifted left by" information in the recursive calls to
5101 // ComputeKnownSignBits. Allowing us to handle this more generically.
5102 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
5103 SDValue Ext = Op.getOperand(0);
5104 EVT ExtVT = Ext.getValueType();
5105 SDValue Extendee = Ext.getOperand(0);
5106 EVT ExtendeeVT = Extendee.getValueType();
5107 unsigned SizeDifference =
5108 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
5109 if (SizeDifference <= MinShAmt) {
5110 Tmp = SizeDifference +
5111 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
5112 if (MaxShAmt < Tmp)
5113 return Tmp - MaxShAmt;
5114 }
5115 }
5116 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
5117 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5118 if (MaxShAmt < Tmp)
5119 return Tmp - MaxShAmt;
5120 }
5121 break;
5122 case ISD::AND:
5123 case ISD::OR:
5124 case ISD::XOR: // NOT is handled here.
5125 // Logical binary ops preserve the number of sign bits at the worst.
5126 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
5127 if (Tmp != 1) {
5128 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
5129 FirstAnswer = std::min(Tmp, Tmp2);
5130 // We computed what we know about the sign bits as our first
5131 // answer. Now proceed to the generic code that uses
5132 // computeKnownBits, and pick whichever answer is better.
5133 }
5134 break;
5135
5136 case ISD::SELECT:
5137 case ISD::VSELECT:
5138 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
5139 if (Tmp == 1) return 1; // Early out.
5140 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
5141 return std::min(Tmp, Tmp2);
5142 case ISD::SELECT_CC:
5143 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
5144 if (Tmp == 1) return 1; // Early out.
5145 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
5146 return std::min(Tmp, Tmp2);
5147
5148 case ISD::SMIN:
5149 case ISD::SMAX: {
5150 // If we have a clamp pattern, we know that the number of sign bits will be
5151 // the minimum of the clamp min/max range.
5152 bool IsMax = (Opcode == ISD::SMAX);
5153 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
5154 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
5155 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
5156 CstHigh =
5157 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
5158 if (CstLow && CstHigh) {
5159 if (!IsMax)
5160 std::swap(CstLow, CstHigh);
5161 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
5162 Tmp = CstLow->getAPIntValue().getNumSignBits();
5163 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
5164 return std::min(Tmp, Tmp2);
5165 }
5166 }
5167
5168 // Fallback - just get the minimum number of sign bits of the operands.
5169 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5170 if (Tmp == 1)
5171 return 1; // Early out.
5172 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5173 return std::min(Tmp, Tmp2);
5174 }
5175 case ISD::UMIN:
5176 case ISD::UMAX:
5177 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5178 if (Tmp == 1)
5179 return 1; // Early out.
5180 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5181 return std::min(Tmp, Tmp2);
5182 case ISD::SSUBO_CARRY:
5183 case ISD::USUBO_CARRY:
5184 // sub_carry(x,x,c) -> 0/-1 (sext carry)
5185 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
5186 return VTBits;
5187 [[fallthrough]];
5188 case ISD::SADDO:
5189 case ISD::UADDO:
5190 case ISD::SADDO_CARRY:
5191 case ISD::UADDO_CARRY:
5192 case ISD::SSUBO:
5193 case ISD::USUBO:
5194 case ISD::SMULO:
5195 case ISD::UMULO:
5196 if (Op.getResNo() != 1)
5197 break;
5198 // The boolean result conforms to getBooleanContents. Fall through.
5199 // If setcc returns 0/-1, all bits are sign bits.
5200 // We know that we have an integer-based boolean since these operations
5201 // are only available for integer.
5202 if (TLI->getBooleanContents(VT.isVector(), false) ==
5204 return VTBits;
5205 break;
5206 case ISD::SETCC:
5207 case ISD::SETCCCARRY:
5208 case ISD::STRICT_FSETCC:
5209 case ISD::STRICT_FSETCCS: {
5210 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
5211 // If setcc returns 0/-1, all bits are sign bits.
5212 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
5214 return VTBits;
5215 break;
5216 }
5217 case ISD::ROTL:
5218 case ISD::ROTR:
5219 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5220
5221 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
5222 if (Tmp == VTBits)
5223 return VTBits;
5224
5225 if (ConstantSDNode *C =
5226 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
5227 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
5228
5229 // Handle rotate right by N like a rotate left by 32-N.
5230 if (Opcode == ISD::ROTR)
5231 RotAmt = (VTBits - RotAmt) % VTBits;
5232
5233 // If we aren't rotating out all of the known-in sign bits, return the
5234 // number that are left. This handles rotl(sext(x), 1) for example.
5235 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
5236 }
5237 break;
5238 case ISD::ADD:
5239 case ISD::ADDC:
5240 // TODO: Move Operand 1 check before Operand 0 check
5241 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5242 if (Tmp == 1) return 1; // Early out.
5243
5244 // Special case decrementing a value (ADD X, -1):
5245 if (ConstantSDNode *CRHS =
5246 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5247 if (CRHS->isAllOnes()) {
5248 KnownBits Known =
5249 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5250
5251 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5252 // sign bits set.
5253 if ((Known.Zero | 1).isAllOnes())
5254 return VTBits;
5255
5256 // If we are subtracting one from a positive number, there is no carry
5257 // out of the result.
5258 if (Known.isNonNegative())
5259 return Tmp;
5260 }
5261
5262 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5263 if (Tmp2 == 1) return 1; // Early out.
5264
5265 // Add can have at most one carry bit. Thus we know that the output
5266 // is, at worst, one more bit than the inputs.
5267 return std::min(Tmp, Tmp2) - 1;
5268 case ISD::SUB:
5269 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5270 if (Tmp2 == 1) return 1; // Early out.
5271
5272 // Handle NEG.
5273 if (ConstantSDNode *CLHS =
5274 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5275 if (CLHS->isZero()) {
5276 KnownBits Known =
5277 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5278 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5279 // sign bits set.
5280 if ((Known.Zero | 1).isAllOnes())
5281 return VTBits;
5282
5283 // If the input is known to be positive (the sign bit is known clear),
5284 // the output of the NEG has the same number of sign bits as the input.
5285 if (Known.isNonNegative())
5286 return Tmp2;
5287
5288 // Otherwise, we treat this like a SUB.
5289 }
5290
5291 // Sub can have at most one carry bit. Thus we know that the output
5292 // is, at worst, one more bit than the inputs.
5293 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5294 if (Tmp == 1) return 1; // Early out.
5295 return std::min(Tmp, Tmp2) - 1;
5296 case ISD::MUL: {
5297 // The output of the Mul can be at most twice the valid bits in the inputs.
5298 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5299 if (SignBitsOp0 == 1)
5300 break;
5301 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5302 if (SignBitsOp1 == 1)
5303 break;
5304 unsigned OutValidBits =
5305 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5306 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5307 }
5308 case ISD::AVGCEILS:
5309 case ISD::AVGFLOORS:
5310 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5311 if (Tmp == 1)
5312 return 1; // Early out.
5313 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5314 return std::min(Tmp, Tmp2);
5315 case ISD::SREM:
5316 // The sign bit is the LHS's sign bit, except when the result of the
5317 // remainder is zero. The magnitude of the result should be less than or
5318 // equal to the magnitude of the LHS. Therefore, the result should have
5319 // at least as many sign bits as the left hand side.
5320 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5321 case ISD::TRUNCATE: {
5322 // Check if the sign bits of source go down as far as the truncated value.
5323 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5324 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5325 if (NumSrcSignBits > (NumSrcBits - VTBits))
5326 return NumSrcSignBits - (NumSrcBits - VTBits);
5327 break;
5328 }
5329 case ISD::EXTRACT_ELEMENT: {
5330 if (VT.isScalableVector())
5331 break;
5332 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5333 const int BitWidth = Op.getValueSizeInBits();
5334 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5335
5336 // Get reverse index (starting from 1), Op1 value indexes elements from
5337 // little end. Sign starts at big end.
5338 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5339
5340 // If the sign portion ends in our element the subtraction gives correct
5341 // result. Otherwise it gives either negative or > bitwidth result
5342 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5343 }
5345 if (VT.isScalableVector())
5346 break;
5347 // If we know the element index, split the demand between the
5348 // source vector and the inserted element, otherwise assume we need
5349 // the original demanded vector elements and the value.
5350 SDValue InVec = Op.getOperand(0);
5351 SDValue InVal = Op.getOperand(1);
5352 SDValue EltNo = Op.getOperand(2);
5353 bool DemandedVal = true;
5354 APInt DemandedVecElts = DemandedElts;
5355 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5356 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5357 unsigned EltIdx = CEltNo->getZExtValue();
5358 DemandedVal = !!DemandedElts[EltIdx];
5359 DemandedVecElts.clearBit(EltIdx);
5360 }
5361 Tmp = std::numeric_limits<unsigned>::max();
5362 if (DemandedVal) {
5363 // TODO - handle implicit truncation of inserted elements.
5364 if (InVal.getScalarValueSizeInBits() != VTBits)
5365 break;
5366 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5367 Tmp = std::min(Tmp, Tmp2);
5368 }
5369 if (!!DemandedVecElts) {
5370 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5371 Tmp = std::min(Tmp, Tmp2);
5372 }
5373 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5374 return Tmp;
5375 }
5377 SDValue InVec = Op.getOperand(0);
5378 SDValue EltNo = Op.getOperand(1);
5379 EVT VecVT = InVec.getValueType();
5380 // ComputeNumSignBits not yet implemented for scalable vectors.
5381 if (VecVT.isScalableVector())
5382 break;
5383 const unsigned BitWidth = Op.getValueSizeInBits();
5384 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5385 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5386
5387 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5388 // anything about sign bits. But if the sizes match we can derive knowledge
5389 // about sign bits from the vector operand.
5390 if (BitWidth != EltBitWidth)
5391 break;
5392
5393 // If we know the element index, just demand that vector element, else for
5394 // an unknown element index, ignore DemandedElts and demand them all.
5395 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5396 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5397 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5398 DemandedSrcElts =
5399 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5400
5401 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5402 }
5404 // Offset the demanded elts by the subvector index.
5405 SDValue Src = Op.getOperand(0);
5406
5407 APInt DemandedSrcElts;
5408 if (Src.getValueType().isScalableVector())
5409 DemandedSrcElts = APInt(1, 1);
5410 else {
5411 uint64_t Idx = Op.getConstantOperandVal(1);
5412 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5413 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5414 }
5415 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5416 }
5417 case ISD::CONCAT_VECTORS: {
5418 if (VT.isScalableVector())
5419 break;
5420 // Determine the minimum number of sign bits across all demanded
5421 // elts of the input vectors. Early out if the result is already 1.
5422 Tmp = std::numeric_limits<unsigned>::max();
5423 EVT SubVectorVT = Op.getOperand(0).getValueType();
5424 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5425 unsigned NumSubVectors = Op.getNumOperands();
5426 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5427 APInt DemandedSub =
5428 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5429 if (!DemandedSub)
5430 continue;
5431 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5432 Tmp = std::min(Tmp, Tmp2);
5433 }
5434 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5435 return Tmp;
5436 }
5437 case ISD::INSERT_SUBVECTOR: {
5438 if (VT.isScalableVector())
5439 break;
5440 // Demand any elements from the subvector and the remainder from the src its
5441 // inserted into.
5442 SDValue Src = Op.getOperand(0);
5443 SDValue Sub = Op.getOperand(1);
5444 uint64_t Idx = Op.getConstantOperandVal(2);
5445 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5446 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5447 APInt DemandedSrcElts = DemandedElts;
5448 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5449
5450 Tmp = std::numeric_limits<unsigned>::max();
5451 if (!!DemandedSubElts) {
5452 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5453 if (Tmp == 1)
5454 return 1; // early-out
5455 }
5456 if (!!DemandedSrcElts) {
5457 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5458 Tmp = std::min(Tmp, Tmp2);
5459 }
5460 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5461 return Tmp;
5462 }
5463 case ISD::LOAD: {
5464 // If we are looking at the loaded value of the SDNode.
5465 if (Op.getResNo() != 0)
5466 break;
5467
5469 if (const MDNode *Ranges = LD->getRanges()) {
5470 if (DemandedElts != 1)
5471 break;
5472
5474 if (VTBits > CR.getBitWidth()) {
5475 switch (LD->getExtensionType()) {
5476 case ISD::SEXTLOAD:
5477 CR = CR.signExtend(VTBits);
5478 break;
5479 case ISD::ZEXTLOAD:
5480 CR = CR.zeroExtend(VTBits);
5481 break;
5482 default:
5483 break;
5484 }
5485 }
5486
5487 if (VTBits != CR.getBitWidth())
5488 break;
5489 return std::min(CR.getSignedMin().getNumSignBits(),
5491 }
5492
5493 unsigned ExtType = LD->getExtensionType();
5494 switch (ExtType) {
5495 default:
5496 break;
5497 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5498 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5499 return VTBits - Tmp + 1;
5500 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5501 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5502 return VTBits - Tmp;
5503 case ISD::NON_EXTLOAD:
5504 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5505 // We only need to handle vectors - computeKnownBits should handle
5506 // scalar cases.
5507 Type *CstTy = Cst->getType();
5508 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5509 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5510 VTBits == CstTy->getScalarSizeInBits()) {
5511 Tmp = VTBits;
5512 for (unsigned i = 0; i != NumElts; ++i) {
5513 if (!DemandedElts[i])
5514 continue;
5515 if (Constant *Elt = Cst->getAggregateElement(i)) {
5516 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5517 const APInt &Value = CInt->getValue();
5518 Tmp = std::min(Tmp, Value.getNumSignBits());
5519 continue;
5520 }
5521 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5522 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5523 Tmp = std::min(Tmp, Value.getNumSignBits());
5524 continue;
5525 }
5526 }
5527 // Unknown type. Conservatively assume no bits match sign bit.
5528 return 1;
5529 }
5530 return Tmp;
5531 }
5532 }
5533 break;
5534 }
5535
5536 break;
5537 }
5540 case ISD::ATOMIC_SWAP:
5552 case ISD::ATOMIC_LOAD: {
5553 auto *AT = cast<AtomicSDNode>(Op);
5554 // If we are looking at the loaded value.
5555 if (Op.getResNo() == 0) {
5556 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5557 if (Tmp == VTBits)
5558 return 1; // early-out
5559
5560 // For atomic_load, prefer to use the extension type.
5561 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5562 switch (AT->getExtensionType()) {
5563 default:
5564 break;
5565 case ISD::SEXTLOAD:
5566 return VTBits - Tmp + 1;
5567 case ISD::ZEXTLOAD:
5568 return VTBits - Tmp;
5569 }
5570 }
5571
5572 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5573 return VTBits - Tmp + 1;
5574 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5575 return VTBits - Tmp;
5576 }
5577 break;
5578 }
5579 }
5580
5581 // Allow the target to implement this method for its nodes.
5582 if (Opcode >= ISD::BUILTIN_OP_END ||
5583 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5584 Opcode == ISD::INTRINSIC_W_CHAIN ||
5585 Opcode == ISD::INTRINSIC_VOID) {
5586 // TODO: This can probably be removed once target code is audited. This
5587 // is here purely to reduce patch size and review complexity.
5588 if (!VT.isScalableVector()) {
5589 unsigned NumBits =
5590 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5591 if (NumBits > 1)
5592 FirstAnswer = std::max(FirstAnswer, NumBits);
5593 }
5594 }
5595
5596 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5597 // use this information.
5598 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5599 return std::max(FirstAnswer, Known.countMinSignBits());
5600}
5601
5603 unsigned Depth) const {
5604 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5605 return Op.getScalarValueSizeInBits() - SignBits + 1;
5606}
5607
5609 const APInt &DemandedElts,
5610 unsigned Depth) const {
5611 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5612 return Op.getScalarValueSizeInBits() - SignBits + 1;
5613}
5614
5616 unsigned Depth) const {
5617 // Early out for FREEZE.
5618 if (Op.getOpcode() == ISD::FREEZE)
5619 return true;
5620
5621 APInt DemandedElts = getDemandAllEltsMask(Op);
5622 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5623}
5624
5626 const APInt &DemandedElts,
5627 bool PoisonOnly,
5628 unsigned Depth) const {
5629 unsigned Opcode = Op.getOpcode();
5630
5631 // Early out for FREEZE.
5632 if (Opcode == ISD::FREEZE)
5633 return true;
5634
5635 if (Depth >= MaxRecursionDepth)
5636 return false; // Limit search depth.
5637
5638 if (isIntOrFPConstant(Op))
5639 return true;
5640
5641 switch (Opcode) {
5642 case ISD::CONDCODE:
5643 case ISD::VALUETYPE:
5644 case ISD::FrameIndex:
5646 case ISD::CopyFromReg:
5647 return true;
5648
5649 case ISD::POISON:
5650 return false;
5651
5652 case ISD::UNDEF:
5653 return PoisonOnly;
5654
5655 case ISD::BUILD_VECTOR:
5656 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5657 // this shouldn't affect the result.
5658 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5659 if (!DemandedElts[i])
5660 continue;
5662 Depth + 1))
5663 return false;
5664 }
5665 return true;
5666
5668 SDValue Src = Op.getOperand(0);
5669 if (Src.getValueType().isScalableVector())
5670 break;
5671 uint64_t Idx = Op.getConstantOperandVal(1);
5672 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5673 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5674 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5675 Depth + 1);
5676 }
5677
5678 case ISD::INSERT_SUBVECTOR: {
5679 if (Op.getValueType().isScalableVector())
5680 break;
5681 SDValue Src = Op.getOperand(0);
5682 SDValue Sub = Op.getOperand(1);
5683 uint64_t Idx = Op.getConstantOperandVal(2);
5684 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5685 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5686 APInt DemandedSrcElts = DemandedElts;
5687 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5688
5689 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5690 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5691 return false;
5692 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5693 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5694 return false;
5695 return true;
5696 }
5697
5699 SDValue Src = Op.getOperand(0);
5700 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5701 EVT SrcVT = Src.getValueType();
5702 if (SrcVT.isFixedLengthVector() && IndexC &&
5703 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5704 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5705 IndexC->getZExtValue());
5706 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5707 Depth + 1);
5708 }
5709 break;
5710 }
5711
5713 SDValue InVec = Op.getOperand(0);
5714 SDValue InVal = Op.getOperand(1);
5715 SDValue EltNo = Op.getOperand(2);
5716 EVT VT = InVec.getValueType();
5717 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5718 if (IndexC && VT.isFixedLengthVector() &&
5719 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5720 if (DemandedElts[IndexC->getZExtValue()] &&
5722 return false;
5723 APInt InVecDemandedElts = DemandedElts;
5724 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5725 if (!!InVecDemandedElts &&
5727 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5728 InVecDemandedElts, PoisonOnly, Depth + 1))
5729 return false;
5730 return true;
5731 }
5732 break;
5733 }
5734
5736 // Check upper (known undef) elements.
5737 if (DemandedElts.ugt(1) && !PoisonOnly)
5738 return false;
5739 // Check element zero.
5740 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5741 Op.getOperand(0), PoisonOnly, Depth + 1))
5742 return false;
5743 return true;
5744
5745 case ISD::SPLAT_VECTOR:
5746 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5747 Depth + 1);
5748
5749 case ISD::VECTOR_SHUFFLE: {
5750 APInt DemandedLHS, DemandedRHS;
5751 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5752 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5753 DemandedElts, DemandedLHS, DemandedRHS,
5754 /*AllowUndefElts=*/false))
5755 return false;
5756 if (!DemandedLHS.isZero() &&
5757 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5758 PoisonOnly, Depth + 1))
5759 return false;
5760 if (!DemandedRHS.isZero() &&
5761 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5762 PoisonOnly, Depth + 1))
5763 return false;
5764 return true;
5765 }
5766
5767 case ISD::SHL:
5768 case ISD::SRL:
5769 case ISD::SRA:
5770 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5771 // enough to check operand 0 if Op can't create undef/poison.
5772 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5773 /*ConsiderFlags*/ true, Depth) &&
5774 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5775 PoisonOnly, Depth + 1);
5776
5777 case ISD::BSWAP:
5778 case ISD::CTPOP:
5779 case ISD::BITREVERSE:
5780 case ISD::AND:
5781 case ISD::OR:
5782 case ISD::XOR:
5783 case ISD::ADD:
5784 case ISD::SUB:
5785 case ISD::MUL:
5786 case ISD::SADDSAT:
5787 case ISD::UADDSAT:
5788 case ISD::SSUBSAT:
5789 case ISD::USUBSAT:
5790 case ISD::SSHLSAT:
5791 case ISD::USHLSAT:
5792 case ISD::SMIN:
5793 case ISD::SMAX:
5794 case ISD::UMIN:
5795 case ISD::UMAX:
5796 case ISD::ZERO_EXTEND:
5797 case ISD::SIGN_EXTEND:
5798 case ISD::ANY_EXTEND:
5799 case ISD::TRUNCATE:
5800 case ISD::VSELECT: {
5801 // If Op can't create undef/poison and none of its operands are undef/poison
5802 // then Op is never undef/poison. A difference from the more common check
5803 // below, outside the switch, is that we handle elementwise operations for
5804 // which the DemandedElts mask is valid for all operands here.
5805 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5806 /*ConsiderFlags*/ true, Depth) &&
5807 all_of(Op->ops(), [&](SDValue V) {
5808 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5809 PoisonOnly, Depth + 1);
5810 });
5811 }
5812
5813 // TODO: Search for noundef attributes from library functions.
5814
5815 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5816
5817 default:
5818 // Allow the target to implement this method for its nodes.
5819 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5820 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5821 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5822 Op, DemandedElts, *this, PoisonOnly, Depth);
5823 break;
5824 }
5825
5826 // If Op can't create undef/poison and none of its operands are undef/poison
5827 // then Op is never undef/poison.
5828 // NOTE: TargetNodes can handle this in themselves in
5829 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5830 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5831 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5832 Depth) &&
5833 all_of(Op->ops(), [&](SDValue V) {
5834 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5835 });
5836}
5837
5839 bool ConsiderFlags,
5840 unsigned Depth) const {
5841 APInt DemandedElts = getDemandAllEltsMask(Op);
5842 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5843 Depth);
5844}
5845
5847 bool PoisonOnly, bool ConsiderFlags,
5848 unsigned Depth) const {
5849 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5850 return true;
5851
5852 unsigned Opcode = Op.getOpcode();
5853 switch (Opcode) {
5854 case ISD::AssertSext:
5855 case ISD::AssertZext:
5856 case ISD::AssertAlign:
5858 // Assertion nodes can create poison if the assertion fails.
5859 return true;
5860
5861 case ISD::FREEZE:
5865 case ISD::SADDSAT:
5866 case ISD::UADDSAT:
5867 case ISD::SSUBSAT:
5868 case ISD::USUBSAT:
5869 case ISD::MULHU:
5870 case ISD::MULHS:
5871 case ISD::AVGFLOORS:
5872 case ISD::AVGFLOORU:
5873 case ISD::AVGCEILS:
5874 case ISD::AVGCEILU:
5875 case ISD::ABDU:
5876 case ISD::ABDS:
5877 case ISD::SMIN:
5878 case ISD::SMAX:
5879 case ISD::SCMP:
5880 case ISD::UMIN:
5881 case ISD::UMAX:
5882 case ISD::UCMP:
5883 case ISD::AND:
5884 case ISD::XOR:
5885 case ISD::ROTL:
5886 case ISD::ROTR:
5887 case ISD::FSHL:
5888 case ISD::FSHR:
5889 case ISD::BSWAP:
5890 case ISD::CTTZ:
5891 case ISD::CTLZ:
5892 case ISD::CTLS:
5893 case ISD::CTPOP:
5894 case ISD::BITREVERSE:
5895 case ISD::PARITY:
5896 case ISD::SIGN_EXTEND:
5897 case ISD::TRUNCATE:
5901 case ISD::BITCAST:
5902 case ISD::BUILD_VECTOR:
5903 case ISD::BUILD_PAIR:
5904 case ISD::SPLAT_VECTOR:
5905 case ISD::FABS:
5906 return false;
5907
5908 case ISD::ABS:
5909 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5910 // Different to Intrinsic::abs.
5911 return false;
5912
5913 case ISD::ADDC:
5914 case ISD::SUBC:
5915 case ISD::ADDE:
5916 case ISD::SUBE:
5917 case ISD::SADDO:
5918 case ISD::SSUBO:
5919 case ISD::SMULO:
5920 case ISD::SADDO_CARRY:
5921 case ISD::SSUBO_CARRY:
5922 case ISD::UADDO:
5923 case ISD::USUBO:
5924 case ISD::UMULO:
5925 case ISD::UADDO_CARRY:
5926 case ISD::USUBO_CARRY:
5927 // No poison on result or overflow flags.
5928 return false;
5929
5930 case ISD::SELECT_CC:
5931 case ISD::SETCC: {
5932 // Integer setcc cannot create undef or poison.
5933 if (Op.getOperand(0).getValueType().isInteger())
5934 return false;
5935
5936 // FP compares are more complicated. They can create poison for nan/infinity
5937 // based on options and flags. The options and flags also cause special
5938 // nonan condition codes to be used. Those condition codes may be preserved
5939 // even if the nonan flag is dropped somewhere.
5940 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5941 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5942 return (unsigned)CCCode & 0x10U;
5943 }
5944
5945 case ISD::OR:
5946 case ISD::ZERO_EXTEND:
5947 case ISD::SELECT:
5948 case ISD::VSELECT:
5949 case ISD::ADD:
5950 case ISD::SUB:
5951 case ISD::MUL:
5952 case ISD::FNEG:
5953 case ISD::FADD:
5954 case ISD::FSUB:
5955 case ISD::FMUL:
5956 case ISD::FDIV:
5957 case ISD::FREM:
5958 case ISD::FCOPYSIGN:
5959 case ISD::FMA:
5960 case ISD::FMAD:
5961 case ISD::FMULADD:
5962 case ISD::FP_EXTEND:
5968 // No poison except from flags (which is handled above)
5969 return false;
5970
5971 case ISD::SHL:
5972 case ISD::SRL:
5973 case ISD::SRA:
5974 // If the max shift amount isn't in range, then the shift can
5975 // create poison.
5976 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5977
5980 // If the amount is zero then the result will be poison.
5981 // TODO: Add isKnownNeverZero DemandedElts handling.
5982 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5983
5985 // Check if we demand any upper (undef) elements.
5986 return !PoisonOnly && DemandedElts.ugt(1);
5987
5990 // Ensure that the element index is in bounds.
5991 EVT VecVT = Op.getOperand(0).getValueType();
5992 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5993 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5994 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5995 }
5996
5997 case ISD::VECTOR_SHUFFLE: {
5998 // Check for any demanded shuffle element that is undef.
5999 auto *SVN = cast<ShuffleVectorSDNode>(Op);
6000 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
6001 if (Elt < 0 && DemandedElts[Idx])
6002 return true;
6003 return false;
6004 }
6005
6007 return false;
6008
6009 default:
6010 // Allow the target to implement this method for its nodes.
6011 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6012 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
6013 return TLI->canCreateUndefOrPoisonForTargetNode(
6014 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
6015 break;
6016 }
6017
6018 // Be conservative and return true.
6019 return true;
6020}
6021
6022bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
6023 unsigned Opcode = Op.getOpcode();
6024 if (Opcode == ISD::OR)
6025 return Op->getFlags().hasDisjoint() ||
6026 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
6027 if (Opcode == ISD::XOR)
6028 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
6029 return false;
6030}
6031
6033 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
6034 (Op.isAnyAdd() || isADDLike(Op));
6035}
6036
6038 FPClassTest InterestedClasses,
6039 unsigned Depth) const {
6040 APInt DemandedElts = getDemandAllEltsMask(Op);
6041 return computeKnownFPClass(Op, DemandedElts, InterestedClasses, Depth);
6042}
6043
6045 const APInt &DemandedElts,
6046 FPClassTest InterestedClasses,
6047 unsigned Depth) const {
6048 KnownFPClass Known;
6049
6050 if (const auto *CFP = dyn_cast<ConstantFPSDNode>(Op))
6051 return KnownFPClass(CFP->getValueAPF());
6052
6053 if (Depth >= MaxRecursionDepth)
6054 return Known;
6055
6056 if (Op.getOpcode() == ISD::UNDEF)
6057 return Known;
6058
6059 EVT VT = Op.getValueType();
6060 assert(VT.isFloatingPoint() && "Computing KnownFPClass on non-FP op!");
6061 assert((!VT.isFixedLengthVector() ||
6062 DemandedElts.getBitWidth() == VT.getVectorNumElements()) &&
6063 "Unexpected vector size");
6064
6065 if (!DemandedElts)
6066 return Known;
6067
6068 unsigned Opcode = Op.getOpcode();
6069 switch (Opcode) {
6070 case ISD::POISON: {
6071 Known.KnownFPClasses = fcNone;
6072 Known.SignBit = false;
6073 break;
6074 }
6075 case ISD::FNEG: {
6076 Known = computeKnownFPClass(Op.getOperand(0), DemandedElts,
6077 InterestedClasses, Depth + 1);
6078 Known.fneg();
6079 break;
6080 }
6081 case ISD::BUILD_VECTOR: {
6082 assert(!VT.isScalableVector());
6083 bool First = true;
6084 for (unsigned I = 0, E = Op.getNumOperands(); I != E; ++I) {
6085 if (!DemandedElts[I])
6086 continue;
6087
6088 if (First) {
6089 Known =
6090 computeKnownFPClass(Op.getOperand(I), InterestedClasses, Depth + 1);
6091 First = false;
6092 } else {
6093 Known |=
6094 computeKnownFPClass(Op.getOperand(I), InterestedClasses, Depth + 1);
6095 }
6096
6097 if (Known.isUnknown())
6098 break;
6099 }
6100 break;
6101 }
6102 case ISD::SPLAT_VECTOR: {
6103 Known = computeKnownFPClass(Op.getOperand(0), InterestedClasses, Depth + 1);
6104 break;
6105 }
6106 case ISD::BITCAST: {
6107 // FIXME: It should not be necessary to check for an elementwise bitcast.
6108 // If a bitcast is not elementwise between vector / scalar types,
6109 // computeKnownBits already splices the known bits of the source elements
6110 // appropriately so as to line up with the bits of the result's demanded
6111 // elements.
6112 EVT SrcVT = Op.getOperand(0).getValueType();
6113 if (VT.isScalableVector() || SrcVT.isScalableVector())
6114 break;
6115 unsigned VTNumElts = VT.isVector() ? VT.getVectorNumElements() : 1;
6116 unsigned SrcVTNumElts = SrcVT.isVector() ? SrcVT.getVectorNumElements() : 1;
6117 if (VTNumElts != SrcVTNumElts)
6118 break;
6119
6120 KnownBits Bits = computeKnownBits(Op, DemandedElts, Depth + 1);
6121 Known = KnownFPClass::bitcast(VT.getFltSemantics(), Bits);
6122 break;
6123 }
6124 case ISD::FABS: {
6125 Known = computeKnownFPClass(Op.getOperand(0), DemandedElts,
6126 InterestedClasses, Depth + 1);
6127 Known.fabs();
6128 break;
6129 }
6130 case ISD::AssertNoFPClass: {
6131 Known = computeKnownFPClass(Op.getOperand(0), DemandedElts,
6132 InterestedClasses, Depth + 1);
6133 FPClassTest AssertedClasses =
6134 static_cast<FPClassTest>(Op->getConstantOperandVal(1));
6135 Known.KnownFPClasses &= ~AssertedClasses;
6136 break;
6137 }
6138 default:
6139 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6140 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6141 TLI->computeKnownFPClassForTargetNode(Op, Known, DemandedElts, *this,
6142 Depth);
6143 }
6144 break;
6145 }
6146
6147 return Known;
6148}
6149
6151 unsigned Depth) const {
6152 APInt DemandedElts = getDemandAllEltsMask(Op);
6153 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
6154}
6155
6157 bool SNaN, unsigned Depth) const {
6158 assert(!DemandedElts.isZero() && "No demanded elements");
6159
6160 // If we're told that NaNs won't happen, assume they won't.
6161 if (Op->getFlags().hasNoNaNs())
6162 return true;
6163
6164 if (Depth >= MaxRecursionDepth)
6165 return false; // Limit search depth.
6166
6167 unsigned Opcode = Op.getOpcode();
6168 switch (Opcode) {
6169 case ISD::FADD:
6170 case ISD::FSUB:
6171 case ISD::FMUL:
6172 case ISD::FDIV:
6173 case ISD::FREM:
6174 case ISD::FSIN:
6175 case ISD::FCOS:
6176 case ISD::FTAN:
6177 case ISD::FASIN:
6178 case ISD::FACOS:
6179 case ISD::FATAN:
6180 case ISD::FATAN2:
6181 case ISD::FSINH:
6182 case ISD::FCOSH:
6183 case ISD::FTANH:
6184 case ISD::FMA:
6185 case ISD::FMULADD:
6186 case ISD::FMAD: {
6187 if (SNaN)
6188 return true;
6189 // TODO: Need isKnownNeverInfinity
6190 return false;
6191 }
6192 case ISD::FCANONICALIZE:
6193 case ISD::FEXP:
6194 case ISD::FEXP2:
6195 case ISD::FEXP10:
6196 case ISD::FTRUNC:
6197 case ISD::FFLOOR:
6198 case ISD::FCEIL:
6199 case ISD::FROUND:
6200 case ISD::FROUNDEVEN:
6201 case ISD::LROUND:
6202 case ISD::LLROUND:
6203 case ISD::FRINT:
6204 case ISD::LRINT:
6205 case ISD::LLRINT:
6206 case ISD::FNEARBYINT:
6207 case ISD::FLDEXP: {
6208 if (SNaN)
6209 return true;
6210 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6211 }
6212 case ISD::FABS:
6213 case ISD::FNEG:
6214 case ISD::FCOPYSIGN: {
6215 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6216 }
6217 case ISD::SELECT:
6218 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
6219 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
6220 case ISD::FP_EXTEND:
6221 case ISD::FP_ROUND: {
6222 if (SNaN)
6223 return true;
6224 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6225 }
6226 case ISD::SINT_TO_FP:
6227 case ISD::UINT_TO_FP:
6228 return true;
6229 case ISD::FSQRT: // Need is known positive
6230 case ISD::FLOG:
6231 case ISD::FLOG2:
6232 case ISD::FLOG10:
6233 case ISD::FPOWI:
6234 case ISD::FPOW: {
6235 if (SNaN)
6236 return true;
6237 // TODO: Refine on operand
6238 return false;
6239 }
6240 case ISD::FMINNUM:
6241 case ISD::FMAXNUM:
6242 case ISD::FMINIMUMNUM:
6243 case ISD::FMAXIMUMNUM: {
6244 // Only one needs to be known not-nan, since it will be returned if the
6245 // other ends up being one.
6246 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
6247 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
6248 }
6249 case ISD::FMINNUM_IEEE:
6250 case ISD::FMAXNUM_IEEE: {
6251 if (SNaN)
6252 return true;
6253 // This can return a NaN if either operand is an sNaN, or if both operands
6254 // are NaN.
6255 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
6256 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
6257 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
6258 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
6259 }
6260 case ISD::FMINIMUM:
6261 case ISD::FMAXIMUM: {
6262 // TODO: Does this quiet or return the origina NaN as-is?
6263 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
6264 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
6265 }
6267 SDValue Src = Op.getOperand(0);
6268 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6269 EVT SrcVT = Src.getValueType();
6270 if (SrcVT.isFixedLengthVector() && Idx &&
6271 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
6272 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
6273 Idx->getZExtValue());
6274 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
6275 }
6276 return isKnownNeverNaN(Src, SNaN, Depth + 1);
6277 }
6279 SDValue Src = Op.getOperand(0);
6280 if (Src.getValueType().isFixedLengthVector()) {
6281 unsigned Idx = Op.getConstantOperandVal(1);
6282 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
6283 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
6284 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
6285 }
6286 return isKnownNeverNaN(Src, SNaN, Depth + 1);
6287 }
6288 case ISD::INSERT_SUBVECTOR: {
6289 SDValue BaseVector = Op.getOperand(0);
6290 SDValue SubVector = Op.getOperand(1);
6291 EVT BaseVectorVT = BaseVector.getValueType();
6292 if (BaseVectorVT.isFixedLengthVector()) {
6293 unsigned Idx = Op.getConstantOperandVal(2);
6294 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
6295 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
6296
6297 // Clear/Extract the bits at the position where the subvector will be
6298 // inserted.
6299 APInt DemandedMask =
6300 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
6301 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
6302 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
6303
6304 bool NeverNaN = true;
6305 if (!DemandedSrcElts.isZero())
6306 NeverNaN &=
6307 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
6308 if (NeverNaN && !DemandedSubElts.isZero())
6309 NeverNaN &=
6310 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
6311 return NeverNaN;
6312 }
6313 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
6314 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
6315 }
6316 case ISD::BUILD_VECTOR: {
6317 unsigned NumElts = Op.getNumOperands();
6318 for (unsigned I = 0; I != NumElts; ++I)
6319 if (DemandedElts[I] &&
6320 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
6321 return false;
6322 return true;
6323 }
6324 case ISD::SPLAT_VECTOR:
6325 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
6326 case ISD::AssertNoFPClass: {
6327 FPClassTest NoFPClass =
6328 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
6329 if ((NoFPClass & fcNan) == fcNan)
6330 return true;
6331 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
6332 return true;
6333 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6334 }
6335 default:
6336 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6337 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6338 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
6339 Depth);
6340 }
6341 break;
6342 }
6343
6344 FPClassTest NanMask = SNaN ? fcSNan : fcNan;
6345 KnownFPClass Known = computeKnownFPClass(Op, DemandedElts, NanMask, Depth);
6346 return Known.isKnownNever(NanMask);
6347}
6348
6350 APInt DemandedElts = getDemandAllEltsMask(Op);
6351 return isKnownNeverLogicalZero(Op, DemandedElts, Depth);
6352}
6353
6355 const APInt &DemandedElts,
6356 unsigned Depth) const {
6357 assert(!DemandedElts.isZero() && "No demanded elements");
6358 EVT VT = Op.getValueType();
6359 KnownFPClass Known =
6360 computeKnownFPClass(Op, DemandedElts, fcZero | fcSubnormal, Depth);
6361 return Known.isKnownNeverLogicalZero(getDenormalMode(VT));
6362}
6363
6365 APInt DemandedElts = getDemandAllEltsMask(Op);
6366 return isKnownNeverZero(Op, DemandedElts, Depth);
6367}
6368
6370 unsigned Depth) const {
6371 if (Depth >= MaxRecursionDepth)
6372 return false; // Limit search depth.
6373
6374 EVT OpVT = Op.getValueType();
6375 unsigned BitWidth = OpVT.getScalarSizeInBits();
6376
6377 assert(!Op.getValueType().isFloatingPoint() &&
6378 "Floating point types unsupported - use isKnownNeverLogicalZero");
6379
6380 // If the value is a constant, we can obviously see if it is a zero or not.
6381 auto IsNeverZero = [BitWidth](const ConstantSDNode *C) {
6382 APInt V = C->getAPIntValue().zextOrTrunc(BitWidth);
6383 return !V.isZero();
6384 };
6385
6386 if (ISD::matchUnaryPredicate(Op, IsNeverZero))
6387 return true;
6388
6389 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6390 // some degree.
6391 switch (Op.getOpcode()) {
6392 default:
6393 break;
6394
6395 case ISD::BUILD_VECTOR:
6396 // Are all operands of a build vector constant non-zero?
6397 if (all_of(enumerate(Op->ops()), [&](auto P) {
6398 auto *C = dyn_cast<ConstantSDNode>(P.value());
6399 return !DemandedElts[P.index()] || (C && IsNeverZero(C));
6400 }))
6401 return true;
6402 break;
6403
6404 case ISD::SPLAT_VECTOR:
6405 // Is the operand of a splat vector a constant non-zero?
6406 if (auto *C = dyn_cast<ConstantSDNode>(Op->getOperand(0)))
6407 if (IsNeverZero(C))
6408 return true;
6409 break;
6410
6412 SDValue InVec = Op.getOperand(0);
6413 SDValue EltNo = Op.getOperand(1);
6414 EVT VecVT = InVec.getValueType();
6415
6416 // Skip scalable vectors or implicit extensions.
6417 if (VecVT.isScalableVector() ||
6418 OpVT.getScalarSizeInBits() != VecVT.getScalarSizeInBits())
6419 break;
6420
6421 // If we know the element index, just demand that vector element, else for
6422 // an unknown element index, ignore DemandedElts and demand them all.
6423 const unsigned NumSrcElts = VecVT.getVectorNumElements();
6424 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
6425 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
6426 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
6427 DemandedSrcElts =
6428 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
6429
6430 return isKnownNeverZero(InVec, DemandedSrcElts, Depth + 1);
6431 }
6432
6433 case ISD::OR:
6434 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6435 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6436
6437 case ISD::VSELECT:
6438 case ISD::SELECT:
6439 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6440 isKnownNeverZero(Op.getOperand(2), DemandedElts, Depth + 1);
6441
6442 case ISD::SHL: {
6443 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6444 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6445 KnownBits ValKnown =
6446 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6447 // 1 << X is never zero.
6448 if (ValKnown.One[0])
6449 return true;
6450 // If max shift cnt of known ones is non-zero, result is non-zero.
6451 APInt MaxCnt = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1)
6452 .getMaxValue();
6453 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6454 !ValKnown.One.shl(MaxCnt).isZero())
6455 return true;
6456 break;
6457 }
6458
6459 case ISD::VECTOR_SHUFFLE: {
6460 if (Op.getValueType().isScalableVector())
6461 return false;
6462
6463 unsigned NumElts = DemandedElts.getBitWidth();
6464
6465 // All demanded elements from LHS and RHS must be known non-zero.
6466 // Demanded elements with undef shuffle mask elements are unknown.
6467
6468 APInt DemandedLHS, DemandedRHS;
6469 auto *SVN = cast<ShuffleVectorSDNode>(Op);
6470 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
6471 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
6472 DemandedLHS, DemandedRHS))
6473 return false;
6474
6475 return (!DemandedLHS ||
6476 isKnownNeverZero(Op.getOperand(0), DemandedLHS, Depth + 1)) &&
6477 (!DemandedRHS ||
6478 isKnownNeverZero(Op.getOperand(1), DemandedRHS, Depth + 1));
6479 }
6480
6481 case ISD::UADDSAT:
6482 case ISD::UMAX:
6483 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6484 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6485
6486 case ISD::UMIN:
6487 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6488 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6489
6490 // For smin/smax: If either operand is known negative/positive
6491 // respectively we don't need the other to be known at all.
6492 case ISD::SMAX: {
6493 KnownBits Op1 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
6494 if (Op1.isStrictlyPositive())
6495 return true;
6496
6497 KnownBits Op0 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6498 if (Op0.isStrictlyPositive())
6499 return true;
6500
6501 if (Op1.isNonZero() && Op0.isNonZero())
6502 return true;
6503
6504 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6505 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6506 }
6507 case ISD::SMIN: {
6508 KnownBits Op1 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
6509 if (Op1.isNegative())
6510 return true;
6511
6512 KnownBits Op0 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6513 if (Op0.isNegative())
6514 return true;
6515
6516 if (Op1.isNonZero() && Op0.isNonZero())
6517 return true;
6518
6519 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6520 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6521 }
6522
6523 case ISD::ROTL:
6524 case ISD::ROTR:
6525 case ISD::BITREVERSE:
6526 case ISD::BSWAP:
6527 case ISD::CTPOP:
6528 case ISD::ABS:
6529 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6530
6531 case ISD::SRA:
6532 case ISD::SRL: {
6533 if (Op->getFlags().hasExact())
6534 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6535 KnownBits ValKnown =
6536 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6537 if (ValKnown.isNegative())
6538 return true;
6539 // If max shift cnt of known ones is non-zero, result is non-zero.
6540 APInt MaxCnt = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1)
6541 .getMaxValue();
6542 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6543 !ValKnown.One.lshr(MaxCnt).isZero())
6544 return true;
6545 break;
6546 }
6547 case ISD::UDIV:
6548 case ISD::SDIV:
6549 // div exact can only produce a zero if the dividend is zero.
6550 // TODO: For udiv this is also true if Op1 u<= Op0
6551 if (Op->getFlags().hasExact())
6552 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6553 break;
6554
6555 case ISD::ADD:
6556 if (Op->getFlags().hasNoUnsignedWrap())
6557 if (isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6558 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1))
6559 return true;
6560 // TODO: There are a lot more cases we can prove for add.
6561 break;
6562
6563 case ISD::SUB: {
6564 if (isNullConstant(Op.getOperand(0)))
6565 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1);
6566
6567 std::optional<bool> ne = KnownBits::ne(
6568 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1),
6569 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1));
6570 return ne && *ne;
6571 }
6572
6573 case ISD::MUL:
6574 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6575 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6576 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6577 return true;
6578 break;
6579
6580 case ISD::ZERO_EXTEND:
6581 case ISD::SIGN_EXTEND:
6582 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6583 case ISD::VSCALE: {
6585 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6586 ConstantRange CR =
6587 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6588 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6589 return true;
6590 break;
6591 }
6592 }
6593
6595}
6596
6598 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6599 return !C1->isNegative();
6600
6601 switch (Op.getOpcode()) {
6602 case ISD::FABS:
6603 case ISD::FEXP:
6604 case ISD::FEXP2:
6605 case ISD::FEXP10:
6606 return true;
6607 default:
6608 return false;
6609 }
6610
6611 llvm_unreachable("covered opcode switch");
6612}
6613
6615 assert(Use.getValueType().isFloatingPoint());
6616 const SDNode *User = Use.getUser();
6617 if (User->getFlags().hasNoSignedZeros())
6618 return true;
6619
6620 unsigned OperandNo = Use.getOperandNo();
6621 // Check if this use is insensitive to the sign of zero
6622 switch (User->getOpcode()) {
6623 case ISD::SETCC:
6624 // Comparisons: IEEE-754 specifies +0.0 == -0.0.
6625 case ISD::FABS:
6626 // fabs always produces +0.0.
6627 return true;
6628 case ISD::FCOPYSIGN:
6629 // copysign overwrites the sign bit of the first operand.
6630 return OperandNo == 0;
6631 case ISD::FADD:
6632 case ISD::FSUB: {
6633 // Arithmetic with non-zero constants fixes the uncertainty around the
6634 // sign bit.
6635 SDValue Other = User->getOperand(1 - OperandNo);
6637 }
6638 case ISD::FP_TO_SINT:
6639 case ISD::FP_TO_UINT:
6640 // fp-to-int conversions normalize signed zeros.
6641 return true;
6642 default:
6643 return false;
6644 }
6645}
6646
6648 if (Op->getFlags().hasNoSignedZeros())
6649 return true;
6650 // FIXME: Limit the amount of checked uses to not introduce a compile-time
6651 // regression. Ideally, this should be implemented as a demanded-bits
6652 // optimization that stems from the users.
6653 if (Op->use_size() > 2)
6654 return false;
6655 return all_of(Op->uses(),
6656 [&](const SDUse &Use) { return canIgnoreSignBitOfZero(Use); });
6657}
6658
6660 // Check the obvious case.
6661 if (A == B) return true;
6662
6663 // For negative and positive zero.
6666 if (CA->isZero() && CB->isZero()) return true;
6667
6668 // Otherwise they may not be equal.
6669 return false;
6670}
6671
6672// Only bits set in Mask must be negated, other bits may be arbitrary.
6674 if (isBitwiseNot(V, AllowUndefs))
6675 return V.getOperand(0);
6676
6677 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6678 // bits in the non-extended part.
6679 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6680 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6681 return SDValue();
6682 SDValue ExtArg = V.getOperand(0);
6683 if (ExtArg.getScalarValueSizeInBits() >=
6684 MaskC->getAPIntValue().getActiveBits() &&
6685 isBitwiseNot(ExtArg, AllowUndefs) &&
6686 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6687 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6688 return ExtArg.getOperand(0).getOperand(0);
6689 return SDValue();
6690}
6691
6693 // Match masked merge pattern (X & ~M) op (Y & M)
6694 // Including degenerate case (X & ~M) op M
6695 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6696 SDValue Other) {
6697 if (SDValue NotOperand =
6698 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6699 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6700 NotOperand->getOpcode() == ISD::TRUNCATE)
6701 NotOperand = NotOperand->getOperand(0);
6702
6703 if (Other == NotOperand)
6704 return true;
6705 if (Other->getOpcode() == ISD::AND)
6706 return NotOperand == Other->getOperand(0) ||
6707 NotOperand == Other->getOperand(1);
6708 }
6709 return false;
6710 };
6711
6712 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6713 A = A->getOperand(0);
6714
6715 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6716 B = B->getOperand(0);
6717
6718 if (A->getOpcode() == ISD::AND)
6719 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6720 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6721 return false;
6722}
6723
6724// FIXME: unify with llvm::haveNoCommonBitsSet.
6726 assert(A.getValueType() == B.getValueType() &&
6727 "Values must have the same type");
6730 return true;
6733}
6734
6735static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6736 SelectionDAG &DAG) {
6737 if (cast<ConstantSDNode>(Step)->isZero())
6738 return DAG.getConstant(0, DL, VT);
6739
6740 return SDValue();
6741}
6742
6745 SelectionDAG &DAG) {
6746 int NumOps = Ops.size();
6747 assert(NumOps != 0 && "Can't build an empty vector!");
6748 assert(!VT.isScalableVector() &&
6749 "BUILD_VECTOR cannot be used with scalable types");
6750 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6751 "Incorrect element count in BUILD_VECTOR!");
6752
6753 // BUILD_VECTOR of UNDEFs is UNDEF.
6754 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6755 return DAG.getUNDEF(VT);
6756
6757 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6758 SDValue IdentitySrc;
6759 bool IsIdentity = true;
6760 for (int i = 0; i != NumOps; ++i) {
6762 Ops[i].getOperand(0).getValueType() != VT ||
6763 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6764 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6765 Ops[i].getConstantOperandAPInt(1) != i) {
6766 IsIdentity = false;
6767 break;
6768 }
6769 IdentitySrc = Ops[i].getOperand(0);
6770 }
6771 if (IsIdentity)
6772 return IdentitySrc;
6773
6774 return SDValue();
6775}
6776
6777/// Try to simplify vector concatenation to an input value, undef, or build
6778/// vector.
6781 SelectionDAG &DAG) {
6782 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6784 [Ops](SDValue Op) {
6785 return Ops[0].getValueType() == Op.getValueType();
6786 }) &&
6787 "Concatenation of vectors with inconsistent value types!");
6788 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6789 VT.getVectorElementCount() &&
6790 "Incorrect element count in vector concatenation!");
6791
6792 if (Ops.size() == 1)
6793 return Ops[0];
6794
6795 // Concat of UNDEFs is UNDEF.
6796 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6797 return DAG.getUNDEF(VT);
6798
6799 // Scan the operands and look for extract operations from a single source
6800 // that correspond to insertion at the same location via this concatenation:
6801 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6802 SDValue IdentitySrc;
6803 bool IsIdentity = true;
6804 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6805 SDValue Op = Ops[i];
6806 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6807 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6808 Op.getOperand(0).getValueType() != VT ||
6809 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6810 Op.getConstantOperandVal(1) != IdentityIndex) {
6811 IsIdentity = false;
6812 break;
6813 }
6814 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6815 "Unexpected identity source vector for concat of extracts");
6816 IdentitySrc = Op.getOperand(0);
6817 }
6818 if (IsIdentity) {
6819 assert(IdentitySrc && "Failed to set source vector of extracts");
6820 return IdentitySrc;
6821 }
6822
6823 // The code below this point is only designed to work for fixed width
6824 // vectors, so we bail out for now.
6825 if (VT.isScalableVector())
6826 return SDValue();
6827
6828 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6829 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6830 // BUILD_VECTOR.
6831 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6832 EVT SVT = VT.getScalarType();
6834 for (SDValue Op : Ops) {
6835 EVT OpVT = Op.getValueType();
6836 if (Op.isUndef())
6837 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6838 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6839 Elts.append(Op->op_begin(), Op->op_end());
6840 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6841 OpVT.getVectorNumElements() == 1 &&
6842 isNullConstant(Op.getOperand(2)))
6843 Elts.push_back(Op.getOperand(1));
6844 else
6845 return SDValue();
6846 }
6847
6848 // BUILD_VECTOR requires all inputs to be of the same type, find the
6849 // maximum type and extend them all.
6850 for (SDValue Op : Elts)
6851 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6852
6853 if (SVT.bitsGT(VT.getScalarType())) {
6854 for (SDValue &Op : Elts) {
6855 if (Op.isUndef())
6856 Op = DAG.getUNDEF(SVT);
6857 else
6858 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6859 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6860 : DAG.getSExtOrTrunc(Op, DL, SVT);
6861 }
6862 }
6863
6864 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6865 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6866 return V;
6867}
6868
6869/// Gets or creates the specified node.
6870SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6871 SDVTList VTs = getVTList(VT);
6873 AddNodeIDNode(ID, Opcode, VTs, {});
6874 void *IP = nullptr;
6875 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6876 return SDValue(E, 0);
6877
6878 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6879 CSEMap.InsertNode(N, IP);
6880
6881 InsertNode(N);
6882 SDValue V = SDValue(N, 0);
6883 NewSDValueDbgMsg(V, "Creating new node: ", this);
6884 return V;
6885}
6886
6887SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6888 SDValue N1) {
6889 SDNodeFlags Flags;
6890 if (Inserter)
6891 Flags = Inserter->getFlags();
6892 return getNode(Opcode, DL, VT, N1, Flags);
6893}
6894
6895SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6896 SDValue N1, const SDNodeFlags Flags) {
6897 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6898
6899 // Constant fold unary operations with a vector integer or float operand.
6900 switch (Opcode) {
6901 default:
6902 // FIXME: Entirely reasonable to perform folding of other unary
6903 // operations here as the need arises.
6904 break;
6905 case ISD::FNEG:
6906 case ISD::FABS:
6907 case ISD::FCEIL:
6908 case ISD::FTRUNC:
6909 case ISD::FFLOOR:
6910 case ISD::FP_EXTEND:
6911 case ISD::FP_TO_SINT:
6912 case ISD::FP_TO_UINT:
6913 case ISD::FP_TO_FP16:
6914 case ISD::FP_TO_BF16:
6915 case ISD::TRUNCATE:
6916 case ISD::ANY_EXTEND:
6917 case ISD::ZERO_EXTEND:
6918 case ISD::SIGN_EXTEND:
6919 case ISD::UINT_TO_FP:
6920 case ISD::SINT_TO_FP:
6921 case ISD::FP16_TO_FP:
6922 case ISD::BF16_TO_FP:
6923 case ISD::BITCAST:
6924 case ISD::ABS:
6925 case ISD::BITREVERSE:
6926 case ISD::BSWAP:
6927 case ISD::CTLZ:
6929 case ISD::CTTZ:
6931 case ISD::CTPOP:
6932 case ISD::CTLS:
6933 case ISD::STEP_VECTOR: {
6934 SDValue Ops = {N1};
6935 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6936 return Fold;
6937 }
6938 }
6939
6940 unsigned OpOpcode = N1.getNode()->getOpcode();
6941 switch (Opcode) {
6942 case ISD::STEP_VECTOR:
6943 assert(VT.isScalableVector() &&
6944 "STEP_VECTOR can only be used with scalable types");
6945 assert(OpOpcode == ISD::TargetConstant &&
6946 VT.getVectorElementType() == N1.getValueType() &&
6947 "Unexpected step operand");
6948 break;
6949 case ISD::FREEZE:
6950 assert(VT == N1.getValueType() && "Unexpected VT!");
6951 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6952 return N1;
6953 break;
6954 case ISD::TokenFactor:
6955 case ISD::MERGE_VALUES:
6957 return N1; // Factor, merge or concat of one node? No need.
6958 case ISD::BUILD_VECTOR: {
6959 // Attempt to simplify BUILD_VECTOR.
6960 SDValue Ops[] = {N1};
6961 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6962 return V;
6963 break;
6964 }
6965 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6966 case ISD::FP_EXTEND:
6968 "Invalid FP cast!");
6969 if (N1.getValueType() == VT) return N1; // noop conversion.
6970 assert((!VT.isVector() || VT.getVectorElementCount() ==
6972 "Vector element count mismatch!");
6973 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6974 if (N1.isUndef())
6975 return getUNDEF(VT);
6976 break;
6977 case ISD::FP_TO_SINT:
6978 case ISD::FP_TO_UINT:
6979 if (N1.isUndef())
6980 return getUNDEF(VT);
6981 break;
6982 case ISD::SINT_TO_FP:
6983 case ISD::UINT_TO_FP:
6984 // [us]itofp(undef) = 0, because the result value is bounded.
6985 if (N1.isUndef())
6986 return getConstantFP(0.0, DL, VT);
6987 break;
6988 case ISD::SIGN_EXTEND:
6989 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6990 "Invalid SIGN_EXTEND!");
6991 assert(VT.isVector() == N1.getValueType().isVector() &&
6992 "SIGN_EXTEND result type type should be vector iff the operand "
6993 "type is vector!");
6994 if (N1.getValueType() == VT) return N1; // noop extension
6995 assert((!VT.isVector() || VT.getVectorElementCount() ==
6997 "Vector element count mismatch!");
6998 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6999 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
7000 SDNodeFlags Flags;
7001 if (OpOpcode == ISD::ZERO_EXTEND)
7002 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7003 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
7004 transferDbgValues(N1, NewVal);
7005 return NewVal;
7006 }
7007
7008 if (OpOpcode == ISD::POISON)
7009 return getPOISON(VT);
7010
7011 if (N1.isUndef())
7012 // sext(undef) = 0, because the top bits will all be the same.
7013 return getConstant(0, DL, VT);
7014
7015 // Skip unnecessary sext_inreg pattern:
7016 // (sext (trunc x)) -> x iff the upper bits are all signbits.
7017 if (OpOpcode == ISD::TRUNCATE) {
7018 SDValue OpOp = N1.getOperand(0);
7019 if (OpOp.getValueType() == VT) {
7020 unsigned NumSignExtBits =
7022 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
7023 transferDbgValues(N1, OpOp);
7024 return OpOp;
7025 }
7026 }
7027 }
7028 break;
7029 case ISD::ZERO_EXTEND:
7030 assert(VT.isInteger() && N1.getValueType().isInteger() &&
7031 "Invalid ZERO_EXTEND!");
7032 assert(VT.isVector() == N1.getValueType().isVector() &&
7033 "ZERO_EXTEND result type type should be vector iff the operand "
7034 "type is vector!");
7035 if (N1.getValueType() == VT) return N1; // noop extension
7036 assert((!VT.isVector() || VT.getVectorElementCount() ==
7038 "Vector element count mismatch!");
7039 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
7040 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
7041 SDNodeFlags Flags;
7042 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7043 SDValue NewVal =
7044 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
7045 transferDbgValues(N1, NewVal);
7046 return NewVal;
7047 }
7048
7049 if (OpOpcode == ISD::POISON)
7050 return getPOISON(VT);
7051
7052 if (N1.isUndef())
7053 // zext(undef) = 0, because the top bits will be zero.
7054 return getConstant(0, DL, VT);
7055
7056 // Skip unnecessary zext_inreg pattern:
7057 // (zext (trunc x)) -> x iff the upper bits are known zero.
7058 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
7059 // use to recognise zext_inreg patterns.
7060 if (OpOpcode == ISD::TRUNCATE) {
7061 SDValue OpOp = N1.getOperand(0);
7062 if (OpOp.getValueType() == VT) {
7063 if (OpOp.getOpcode() != ISD::AND) {
7066 if (MaskedValueIsZero(OpOp, HiBits)) {
7067 transferDbgValues(N1, OpOp);
7068 return OpOp;
7069 }
7070 }
7071 }
7072 }
7073 break;
7074 case ISD::ANY_EXTEND:
7075 assert(VT.isInteger() && N1.getValueType().isInteger() &&
7076 "Invalid ANY_EXTEND!");
7077 assert(VT.isVector() == N1.getValueType().isVector() &&
7078 "ANY_EXTEND result type type should be vector iff the operand "
7079 "type is vector!");
7080 if (N1.getValueType() == VT) return N1; // noop extension
7081 assert((!VT.isVector() || VT.getVectorElementCount() ==
7083 "Vector element count mismatch!");
7084 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
7085
7086 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
7087 OpOpcode == ISD::ANY_EXTEND) {
7088 SDNodeFlags Flags;
7089 if (OpOpcode == ISD::ZERO_EXTEND)
7090 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7091 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
7092 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
7093 }
7094 if (N1.isUndef())
7095 return getUNDEF(VT);
7096
7097 // (ext (trunc x)) -> x
7098 if (OpOpcode == ISD::TRUNCATE) {
7099 SDValue OpOp = N1.getOperand(0);
7100 if (OpOp.getValueType() == VT) {
7101 transferDbgValues(N1, OpOp);
7102 return OpOp;
7103 }
7104 }
7105 break;
7106 case ISD::TRUNCATE:
7107 assert(VT.isInteger() && N1.getValueType().isInteger() &&
7108 "Invalid TRUNCATE!");
7109 assert(VT.isVector() == N1.getValueType().isVector() &&
7110 "TRUNCATE result type type should be vector iff the operand "
7111 "type is vector!");
7112 if (N1.getValueType() == VT) return N1; // noop truncate
7113 assert((!VT.isVector() || VT.getVectorElementCount() ==
7115 "Vector element count mismatch!");
7116 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
7117 if (OpOpcode == ISD::TRUNCATE)
7118 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
7119 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
7120 OpOpcode == ISD::ANY_EXTEND) {
7121 // If the source is smaller than the dest, we still need an extend.
7123 VT.getScalarType())) {
7124 SDNodeFlags Flags;
7125 if (OpOpcode == ISD::ZERO_EXTEND)
7126 Flags.setNonNeg(N1->getFlags().hasNonNeg());
7127 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
7128 }
7129 if (N1.getOperand(0).getValueType().bitsGT(VT))
7130 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
7131 return N1.getOperand(0);
7132 }
7133 if (N1.isUndef())
7134 return getUNDEF(VT);
7135 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
7136 return getVScale(DL, VT,
7138 break;
7142 assert(VT.isVector() && "This DAG node is restricted to vector types.");
7143 assert(N1.getValueType().bitsLE(VT) &&
7144 "The input must be the same size or smaller than the result.");
7147 "The destination vector type must have fewer lanes than the input.");
7148 break;
7149 case ISD::ABS:
7150 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
7151 if (N1.isUndef())
7152 return getConstant(0, DL, VT);
7153 break;
7154 case ISD::BSWAP:
7155 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
7156 assert((VT.getScalarSizeInBits() % 16 == 0) &&
7157 "BSWAP types must be a multiple of 16 bits!");
7158 if (N1.isUndef())
7159 return getUNDEF(VT);
7160 // bswap(bswap(X)) -> X.
7161 if (OpOpcode == ISD::BSWAP)
7162 return N1.getOperand(0);
7163 break;
7164 case ISD::BITREVERSE:
7165 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
7166 if (N1.isUndef())
7167 return getUNDEF(VT);
7168 break;
7169 case ISD::BITCAST:
7171 "Cannot BITCAST between types of different sizes!");
7172 if (VT == N1.getValueType()) return N1; // noop conversion.
7173 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
7174 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
7175 if (N1.isUndef())
7176 return getUNDEF(VT);
7177 break;
7179 assert(VT.isVector() && !N1.getValueType().isVector() &&
7180 (VT.getVectorElementType() == N1.getValueType() ||
7182 N1.getValueType().isInteger() &&
7184 "Illegal SCALAR_TO_VECTOR node!");
7185 if (N1.isUndef())
7186 return getUNDEF(VT);
7187 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
7188 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
7190 N1.getConstantOperandVal(1) == 0 &&
7191 N1.getOperand(0).getValueType() == VT)
7192 return N1.getOperand(0);
7193 break;
7194 case ISD::FNEG:
7195 // Negation of an unknown bag of bits is still completely undefined.
7196 if (N1.isUndef())
7197 return getUNDEF(VT);
7198
7199 if (OpOpcode == ISD::FNEG) // --X -> X
7200 return N1.getOperand(0);
7201 break;
7202 case ISD::FABS:
7203 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
7204 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
7205 break;
7206 case ISD::VSCALE:
7207 assert(VT == N1.getValueType() && "Unexpected VT!");
7208 break;
7209 case ISD::CTPOP:
7210 if (N1.getValueType().getScalarType() == MVT::i1)
7211 return N1;
7212 break;
7213 case ISD::CTLZ:
7214 case ISD::CTTZ:
7215 if (N1.getValueType().getScalarType() == MVT::i1)
7216 return getNOT(DL, N1, N1.getValueType());
7217 break;
7218 case ISD::CTLS:
7219 if (N1.getValueType().getScalarType() == MVT::i1)
7220 return getConstant(0, DL, VT);
7221 break;
7222 case ISD::VECREDUCE_ADD:
7223 if (N1.getValueType().getScalarType() == MVT::i1)
7224 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
7225 break;
7228 if (N1.getValueType().getScalarType() == MVT::i1)
7229 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
7230 break;
7233 if (N1.getValueType().getScalarType() == MVT::i1)
7234 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
7235 break;
7236 case ISD::SPLAT_VECTOR:
7237 assert(VT.isVector() && "Wrong return type!");
7238 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
7239 // that for now.
7241 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
7243 N1.getValueType().isInteger() &&
7245 "Wrong operand type!");
7246 break;
7247 }
7248
7249 SDNode *N;
7250 SDVTList VTs = getVTList(VT);
7251 SDValue Ops[] = {N1};
7252 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
7254 AddNodeIDNode(ID, Opcode, VTs, Ops);
7255 void *IP = nullptr;
7256 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7257 E->intersectFlagsWith(Flags);
7258 return SDValue(E, 0);
7259 }
7260
7261 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7262 N->setFlags(Flags);
7263 createOperands(N, Ops);
7264 CSEMap.InsertNode(N, IP);
7265 } else {
7266 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7267 createOperands(N, Ops);
7268 }
7269
7270 InsertNode(N);
7271 SDValue V = SDValue(N, 0);
7272 NewSDValueDbgMsg(V, "Creating new node: ", this);
7273 return V;
7274}
7275
7276static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
7277 const APInt &C2) {
7278 switch (Opcode) {
7279 case ISD::ADD: return C1 + C2;
7280 case ISD::SUB: return C1 - C2;
7281 case ISD::MUL: return C1 * C2;
7282 case ISD::AND: return C1 & C2;
7283 case ISD::OR: return C1 | C2;
7284 case ISD::XOR: return C1 ^ C2;
7285 case ISD::SHL: return C1 << C2;
7286 case ISD::SRL: return C1.lshr(C2);
7287 case ISD::SRA: return C1.ashr(C2);
7288 case ISD::ROTL: return C1.rotl(C2);
7289 case ISD::ROTR: return C1.rotr(C2);
7290 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
7291 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
7292 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
7293 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
7294 case ISD::SADDSAT: return C1.sadd_sat(C2);
7295 case ISD::UADDSAT: return C1.uadd_sat(C2);
7296 case ISD::SSUBSAT: return C1.ssub_sat(C2);
7297 case ISD::USUBSAT: return C1.usub_sat(C2);
7298 case ISD::SSHLSAT: return C1.sshl_sat(C2);
7299 case ISD::USHLSAT: return C1.ushl_sat(C2);
7300 case ISD::UDIV:
7301 if (!C2.getBoolValue())
7302 break;
7303 return C1.udiv(C2);
7304 case ISD::UREM:
7305 if (!C2.getBoolValue())
7306 break;
7307 return C1.urem(C2);
7308 case ISD::SDIV:
7309 if (!C2.getBoolValue())
7310 break;
7311 return C1.sdiv(C2);
7312 case ISD::SREM:
7313 if (!C2.getBoolValue())
7314 break;
7315 return C1.srem(C2);
7316 case ISD::AVGFLOORS:
7317 return APIntOps::avgFloorS(C1, C2);
7318 case ISD::AVGFLOORU:
7319 return APIntOps::avgFloorU(C1, C2);
7320 case ISD::AVGCEILS:
7321 return APIntOps::avgCeilS(C1, C2);
7322 case ISD::AVGCEILU:
7323 return APIntOps::avgCeilU(C1, C2);
7324 case ISD::ABDS:
7325 return APIntOps::abds(C1, C2);
7326 case ISD::ABDU:
7327 return APIntOps::abdu(C1, C2);
7328 case ISD::MULHS:
7329 return APIntOps::mulhs(C1, C2);
7330 case ISD::MULHU:
7331 return APIntOps::mulhu(C1, C2);
7332 case ISD::CLMUL:
7333 return APIntOps::clmul(C1, C2);
7334 case ISD::CLMULR:
7335 return APIntOps::clmulr(C1, C2);
7336 case ISD::CLMULH:
7337 return APIntOps::clmulh(C1, C2);
7338 }
7339 return std::nullopt;
7340}
7341// Handle constant folding with UNDEF.
7342// TODO: Handle more cases.
7343static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
7344 bool IsUndef1, const APInt &C2,
7345 bool IsUndef2) {
7346 if (!(IsUndef1 || IsUndef2))
7347 return FoldValue(Opcode, C1, C2);
7348
7349 // Fold and(x, undef) -> 0
7350 // Fold mul(x, undef) -> 0
7351 if (Opcode == ISD::AND || Opcode == ISD::MUL)
7352 return APInt::getZero(C1.getBitWidth());
7353
7354 return std::nullopt;
7355}
7356
7358 const GlobalAddressSDNode *GA,
7359 const SDNode *N2) {
7360 if (GA->getOpcode() != ISD::GlobalAddress)
7361 return SDValue();
7362 if (!TLI->isOffsetFoldingLegal(GA))
7363 return SDValue();
7364 auto *C2 = dyn_cast<ConstantSDNode>(N2);
7365 if (!C2)
7366 return SDValue();
7367 int64_t Offset = C2->getSExtValue();
7368 switch (Opcode) {
7369 case ISD::ADD:
7370 case ISD::PTRADD:
7371 break;
7372 case ISD::SUB: Offset = -uint64_t(Offset); break;
7373 default: return SDValue();
7374 }
7375 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
7376 GA->getOffset() + uint64_t(Offset));
7377}
7378
7380 switch (Opcode) {
7381 case ISD::SDIV:
7382 case ISD::UDIV:
7383 case ISD::SREM:
7384 case ISD::UREM: {
7385 // If a divisor is zero/undef or any element of a divisor vector is
7386 // zero/undef, the whole op is undef.
7387 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
7388 SDValue Divisor = Ops[1];
7389 if (Divisor.isUndef() || isNullConstant(Divisor))
7390 return true;
7391
7392 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
7393 llvm::any_of(Divisor->op_values(),
7394 [](SDValue V) { return V.isUndef() ||
7395 isNullConstant(V); });
7396 // TODO: Handle signed overflow.
7397 }
7398 // TODO: Handle oversized shifts.
7399 default:
7400 return false;
7401 }
7402}
7403
7406 SDNodeFlags Flags) {
7407 // If the opcode is a target-specific ISD node, there's nothing we can
7408 // do here and the operand rules may not line up with the below, so
7409 // bail early.
7410 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
7411 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
7412 // foldCONCAT_VECTORS in getNode before this is called.
7413 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
7414 return SDValue();
7415
7416 unsigned NumOps = Ops.size();
7417 if (NumOps == 0)
7418 return SDValue();
7419
7420 if (isUndef(Opcode, Ops))
7421 return getUNDEF(VT);
7422
7423 // Handle unary special cases.
7424 if (NumOps == 1) {
7425 SDValue N1 = Ops[0];
7426
7427 // Constant fold unary operations with an integer constant operand. Even
7428 // opaque constant will be folded, because the folding of unary operations
7429 // doesn't create new constants with different values. Nevertheless, the
7430 // opaque flag is preserved during folding to prevent future folding with
7431 // other constants.
7432 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
7433 const APInt &Val = C->getAPIntValue();
7434 switch (Opcode) {
7435 case ISD::SIGN_EXTEND:
7436 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7437 C->isTargetOpcode(), C->isOpaque());
7438 case ISD::TRUNCATE:
7439 if (C->isOpaque())
7440 break;
7441 [[fallthrough]];
7442 case ISD::ZERO_EXTEND:
7443 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7444 C->isTargetOpcode(), C->isOpaque());
7445 case ISD::ANY_EXTEND:
7446 // Some targets like RISCV prefer to sign extend some types.
7447 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
7448 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7449 C->isTargetOpcode(), C->isOpaque());
7450 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7451 C->isTargetOpcode(), C->isOpaque());
7452 case ISD::ABS:
7453 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
7454 C->isOpaque());
7455 case ISD::BITREVERSE:
7456 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
7457 C->isOpaque());
7458 case ISD::BSWAP:
7459 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
7460 C->isOpaque());
7461 case ISD::CTPOP:
7462 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
7463 C->isOpaque());
7464 case ISD::CTLZ:
7466 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7467 C->isOpaque());
7468 case ISD::CTTZ:
7470 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7471 C->isOpaque());
7472 case ISD::CTLS:
7473 // CTLS returns the number of extra sign bits so subtract one.
7474 return getConstant(Val.getNumSignBits() - 1, DL, VT,
7475 C->isTargetOpcode(), C->isOpaque());
7476 case ISD::UINT_TO_FP:
7477 case ISD::SINT_TO_FP: {
7479 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7481 return getConstantFP(FPV, DL, VT);
7482 }
7483 case ISD::FP16_TO_FP:
7484 case ISD::BF16_TO_FP: {
7485 bool Ignored;
7486 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7487 : APFloat::BFloat(),
7488 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7489
7490 // This can return overflow, underflow, or inexact; we don't care.
7491 // FIXME need to be more flexible about rounding mode.
7493 &Ignored);
7494 return getConstantFP(FPV, DL, VT);
7495 }
7496 case ISD::STEP_VECTOR:
7497 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7498 return V;
7499 break;
7500 case ISD::BITCAST:
7501 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7502 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7503 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7504 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7505 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7506 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7507 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7508 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7509 break;
7510 }
7511 }
7512
7513 // Constant fold unary operations with a floating point constant operand.
7514 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7515 APFloat V = C->getValueAPF(); // make copy
7516 switch (Opcode) {
7517 case ISD::FNEG:
7518 V.changeSign();
7519 return getConstantFP(V, DL, VT);
7520 case ISD::FABS:
7521 V.clearSign();
7522 return getConstantFP(V, DL, VT);
7523 case ISD::FCEIL: {
7524 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7526 return getConstantFP(V, DL, VT);
7527 return SDValue();
7528 }
7529 case ISD::FTRUNC: {
7530 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7532 return getConstantFP(V, DL, VT);
7533 return SDValue();
7534 }
7535 case ISD::FFLOOR: {
7536 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7538 return getConstantFP(V, DL, VT);
7539 return SDValue();
7540 }
7541 case ISD::FP_EXTEND: {
7542 bool ignored;
7543 // This can return overflow, underflow, or inexact; we don't care.
7544 // FIXME need to be more flexible about rounding mode.
7545 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7546 &ignored);
7547 return getConstantFP(V, DL, VT);
7548 }
7549 case ISD::FP_TO_SINT:
7550 case ISD::FP_TO_UINT: {
7551 bool ignored;
7552 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7553 // FIXME need to be more flexible about rounding mode.
7555 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7556 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7557 break;
7558 return getConstant(IntVal, DL, VT);
7559 }
7560 case ISD::FP_TO_FP16:
7561 case ISD::FP_TO_BF16: {
7562 bool Ignored;
7563 // This can return overflow, underflow, or inexact; we don't care.
7564 // FIXME need to be more flexible about rounding mode.
7565 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7566 : APFloat::BFloat(),
7568 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7569 }
7570 case ISD::BITCAST:
7571 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7572 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7573 VT);
7574 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7575 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7576 VT);
7577 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7578 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7579 VT);
7580 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7581 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7582 break;
7583 }
7584 }
7585
7586 // Early-out if we failed to constant fold a bitcast.
7587 if (Opcode == ISD::BITCAST)
7588 return SDValue();
7589 }
7590
7591 // Handle binops special cases.
7592 if (NumOps == 2) {
7593 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7594 return CFP;
7595
7596 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7597 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7598 if (C1->isOpaque() || C2->isOpaque())
7599 return SDValue();
7600
7601 std::optional<APInt> FoldAttempt =
7602 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7603 if (!FoldAttempt)
7604 return SDValue();
7605
7606 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7607 assert((!Folded || !VT.isVector()) &&
7608 "Can't fold vectors ops with scalar operands");
7609 return Folded;
7610 }
7611 }
7612
7613 // fold (add Sym, c) -> Sym+c
7615 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7616 if (TLI->isCommutativeBinOp(Opcode))
7618 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7619
7620 // fold (sext_in_reg c1) -> c2
7621 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7622 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7623
7624 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7625 unsigned FromBits = EVT.getScalarSizeInBits();
7626 Val <<= Val.getBitWidth() - FromBits;
7627 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7628 return getConstant(Val, DL, ConstantVT);
7629 };
7630
7631 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7632 const APInt &Val = C1->getAPIntValue();
7633 return SignExtendInReg(Val, VT);
7634 }
7635
7637 SmallVector<SDValue, 8> ScalarOps;
7638 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7639 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7640 SDValue Op = Ops[0].getOperand(I);
7641 if (Op.isUndef()) {
7642 ScalarOps.push_back(getUNDEF(OpVT));
7643 continue;
7644 }
7645 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7646 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7647 }
7648 return getBuildVector(VT, DL, ScalarOps);
7649 }
7650
7651 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7652 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7653 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7654 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7655 Ops[0].getOperand(0).getValueType()));
7656 }
7657 }
7658
7659 // Handle fshl/fshr special cases.
7660 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7661 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7662 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7663 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7664
7665 if (C1 && C2 && C3) {
7666 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7667 return SDValue();
7668 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7669 &V3 = C3->getAPIntValue();
7670
7671 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7672 : APIntOps::fshr(V1, V2, V3);
7673 return getConstant(FoldedVal, DL, VT);
7674 }
7675 }
7676
7677 // Handle fma/fmad special cases.
7678 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7679 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7680 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7681 Ops[2].getValueType() == VT && "FMA types must match!");
7685 if (C1 && C2 && C3) {
7686 APFloat V1 = C1->getValueAPF();
7687 const APFloat &V2 = C2->getValueAPF();
7688 const APFloat &V3 = C3->getValueAPF();
7689 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7692 } else
7694 return getConstantFP(V1, DL, VT);
7695 }
7696 }
7697
7698 // This is for vector folding only from here on.
7699 if (!VT.isVector())
7700 return SDValue();
7701
7702 ElementCount NumElts = VT.getVectorElementCount();
7703
7704 // See if we can fold through any bitcasted integer ops.
7705 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7706 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7707 (Ops[0].getOpcode() == ISD::BITCAST ||
7708 Ops[1].getOpcode() == ISD::BITCAST)) {
7711 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7712 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7713 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7714 N2.getValueType().isInteger()) {
7715 bool IsLE = getDataLayout().isLittleEndian();
7716 unsigned EltBits = VT.getScalarSizeInBits();
7717 SmallVector<APInt> RawBits1, RawBits2;
7718 BitVector UndefElts1, UndefElts2;
7719 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7720 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7721 SmallVector<APInt> RawBits;
7722 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7723 std::optional<APInt> Fold = FoldValueWithUndef(
7724 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7725 if (!Fold)
7726 break;
7727 RawBits.push_back(*Fold);
7728 }
7729 if (RawBits.size() == NumElts.getFixedValue()) {
7730 // We have constant folded, but we might need to cast this again back
7731 // to the original (possibly legalized) type.
7732 EVT BVVT, BVEltVT;
7733 if (N1.getValueType() == VT) {
7734 BVVT = N1.getValueType();
7735 BVEltVT = BV1->getOperand(0).getValueType();
7736 } else {
7737 BVVT = N2.getValueType();
7738 BVEltVT = BV2->getOperand(0).getValueType();
7739 }
7740 unsigned BVEltBits = BVEltVT.getSizeInBits();
7741 SmallVector<APInt> DstBits;
7742 BitVector DstUndefs;
7744 DstBits, RawBits, DstUndefs,
7745 BitVector(RawBits.size(), false));
7746 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7747 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7748 if (DstUndefs[I])
7749 continue;
7750 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7751 }
7752 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7753 }
7754 }
7755 }
7756 }
7757
7758 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7759 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7760 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7761 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7762 APInt RHSVal;
7763 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7764 APInt NewStep = Opcode == ISD::MUL
7765 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7766 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7767 return getStepVector(DL, VT, NewStep);
7768 }
7769 }
7770
7771 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7772 return !Op.getValueType().isVector() ||
7773 Op.getValueType().getVectorElementCount() == NumElts;
7774 };
7775
7776 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7777 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7778 Op.getOpcode() == ISD::BUILD_VECTOR ||
7779 Op.getOpcode() == ISD::SPLAT_VECTOR;
7780 };
7781
7782 // All operands must be vector types with the same number of elements as
7783 // the result type and must be either UNDEF or a build/splat vector
7784 // or UNDEF scalars.
7785 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7786 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7787 return SDValue();
7788
7789 // If we are comparing vectors, then the result needs to be a i1 boolean that
7790 // is then extended back to the legal result type depending on how booleans
7791 // are represented.
7792 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7793 ISD::NodeType ExtendCode =
7794 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7795 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7797
7798 // Find legal integer scalar type for constant promotion and
7799 // ensure that its scalar size is at least as large as source.
7800 EVT LegalSVT = VT.getScalarType();
7801 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7802 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7803 if (LegalSVT.bitsLT(VT.getScalarType()))
7804 return SDValue();
7805 }
7806
7807 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7808 // only have one operand to check. For fixed-length vector types we may have
7809 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7810 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7811
7812 // Constant fold each scalar lane separately.
7813 SmallVector<SDValue, 4> ScalarResults;
7814 for (unsigned I = 0; I != NumVectorElts; I++) {
7815 SmallVector<SDValue, 4> ScalarOps;
7816 for (SDValue Op : Ops) {
7817 EVT InSVT = Op.getValueType().getScalarType();
7818 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7819 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7820 if (Op.isUndef())
7821 ScalarOps.push_back(getUNDEF(InSVT));
7822 else
7823 ScalarOps.push_back(Op);
7824 continue;
7825 }
7826
7827 SDValue ScalarOp =
7828 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7829 EVT ScalarVT = ScalarOp.getValueType();
7830
7831 // Build vector (integer) scalar operands may need implicit
7832 // truncation - do this before constant folding.
7833 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7834 // Don't create illegally-typed nodes unless they're constants or undef
7835 // - if we fail to constant fold we can't guarantee the (dead) nodes
7836 // we're creating will be cleaned up before being visited for
7837 // legalization.
7838 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7839 !isa<ConstantSDNode>(ScalarOp) &&
7840 TLI->getTypeAction(*getContext(), InSVT) !=
7842 return SDValue();
7843 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7844 }
7845
7846 ScalarOps.push_back(ScalarOp);
7847 }
7848
7849 // Constant fold the scalar operands.
7850 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7851
7852 // Scalar folding only succeeded if the result is a constant or UNDEF.
7853 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7854 ScalarResult.getOpcode() != ISD::ConstantFP)
7855 return SDValue();
7856
7857 // Legalize the (integer) scalar constant if necessary. We only do
7858 // this once we know the folding succeeded, since otherwise we would
7859 // get a node with illegal type which has a user.
7860 if (LegalSVT != SVT)
7861 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7862
7863 ScalarResults.push_back(ScalarResult);
7864 }
7865
7866 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7867 : getBuildVector(VT, DL, ScalarResults);
7868 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7869 return V;
7870}
7871
7874 // TODO: Add support for unary/ternary fp opcodes.
7875 if (Ops.size() != 2)
7876 return SDValue();
7877
7878 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7879 // should. That will require dealing with a potentially non-default
7880 // rounding mode, checking the "opStatus" return value from the APFloat
7881 // math calculations, and possibly other variations.
7882 SDValue N1 = Ops[0];
7883 SDValue N2 = Ops[1];
7884 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7885 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7886 if (N1CFP && N2CFP) {
7887 APFloat C1 = N1CFP->getValueAPF(); // make copy
7888 const APFloat &C2 = N2CFP->getValueAPF();
7889 switch (Opcode) {
7890 case ISD::FADD:
7892 return getConstantFP(C1, DL, VT);
7893 case ISD::FSUB:
7895 return getConstantFP(C1, DL, VT);
7896 case ISD::FMUL:
7898 return getConstantFP(C1, DL, VT);
7899 case ISD::FDIV:
7901 return getConstantFP(C1, DL, VT);
7902 case ISD::FREM:
7903 C1.mod(C2);
7904 return getConstantFP(C1, DL, VT);
7905 case ISD::FCOPYSIGN:
7906 C1.copySign(C2);
7907 return getConstantFP(C1, DL, VT);
7908 case ISD::FMINNUM:
7909 return getConstantFP(minnum(C1, C2), DL, VT);
7910 case ISD::FMAXNUM:
7911 return getConstantFP(maxnum(C1, C2), DL, VT);
7912 case ISD::FMINIMUM:
7913 return getConstantFP(minimum(C1, C2), DL, VT);
7914 case ISD::FMAXIMUM:
7915 return getConstantFP(maximum(C1, C2), DL, VT);
7916 case ISD::FMINIMUMNUM:
7917 return getConstantFP(minimumnum(C1, C2), DL, VT);
7918 case ISD::FMAXIMUMNUM:
7919 return getConstantFP(maximumnum(C1, C2), DL, VT);
7920 default: break;
7921 }
7922 }
7923 if (N1CFP && Opcode == ISD::FP_ROUND) {
7924 APFloat C1 = N1CFP->getValueAPF(); // make copy
7925 bool Unused;
7926 // This can return overflow, underflow, or inexact; we don't care.
7927 // FIXME need to be more flexible about rounding mode.
7929 &Unused);
7930 return getConstantFP(C1, DL, VT);
7931 }
7932
7933 switch (Opcode) {
7934 case ISD::FSUB:
7935 // -0.0 - undef --> undef (consistent with "fneg undef")
7936 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7937 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7938 return getUNDEF(VT);
7939 [[fallthrough]];
7940
7941 case ISD::FADD:
7942 case ISD::FMUL:
7943 case ISD::FDIV:
7944 case ISD::FREM:
7945 // If both operands are undef, the result is undef. If 1 operand is undef,
7946 // the result is NaN. This should match the behavior of the IR optimizer.
7947 if (N1.isUndef() && N2.isUndef())
7948 return getUNDEF(VT);
7949 if (N1.isUndef() || N2.isUndef())
7951 }
7952 return SDValue();
7953}
7954
7956 const SDLoc &DL, EVT DstEltVT) {
7957 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7958
7959 // If this is already the right type, we're done.
7960 if (SrcEltVT == DstEltVT)
7961 return SDValue(BV, 0);
7962
7963 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7964 unsigned DstBitSize = DstEltVT.getSizeInBits();
7965
7966 // If this is a conversion of N elements of one type to N elements of another
7967 // type, convert each element. This handles FP<->INT cases.
7968 if (SrcBitSize == DstBitSize) {
7970 for (SDValue Op : BV->op_values()) {
7971 // If the vector element type is not legal, the BUILD_VECTOR operands
7972 // are promoted and implicitly truncated. Make that explicit here.
7973 if (Op.getValueType() != SrcEltVT)
7974 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7975 Ops.push_back(getBitcast(DstEltVT, Op));
7976 }
7977 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7979 return getBuildVector(VT, DL, Ops);
7980 }
7981
7982 // Otherwise, we're growing or shrinking the elements. To avoid having to
7983 // handle annoying details of growing/shrinking FP values, we convert them to
7984 // int first.
7985 if (SrcEltVT.isFloatingPoint()) {
7986 // Convert the input float vector to a int vector where the elements are the
7987 // same sizes.
7988 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7989 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7991 DstEltVT);
7992 return SDValue();
7993 }
7994
7995 // Now we know the input is an integer vector. If the output is a FP type,
7996 // convert to integer first, then to FP of the right size.
7997 if (DstEltVT.isFloatingPoint()) {
7998 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7999 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
8001 DstEltVT);
8002 return SDValue();
8003 }
8004
8005 // Okay, we know the src/dst types are both integers of differing types.
8006 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
8007
8008 // Extract the constant raw bit data.
8009 BitVector UndefElements;
8010 SmallVector<APInt> RawBits;
8011 bool IsLE = getDataLayout().isLittleEndian();
8012 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
8013 return SDValue();
8014
8016 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
8017 if (UndefElements[I])
8018 Ops.push_back(getUNDEF(DstEltVT));
8019 else
8020 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
8021 }
8022
8023 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
8024 return getBuildVector(VT, DL, Ops);
8025}
8026
8028 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
8029
8030 // There's no need to assert on a byte-aligned pointer. All pointers are at
8031 // least byte aligned.
8032 if (A == Align(1))
8033 return Val;
8034
8035 SDVTList VTs = getVTList(Val.getValueType());
8037 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
8038 ID.AddInteger(A.value());
8039
8040 void *IP = nullptr;
8041 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
8042 return SDValue(E, 0);
8043
8044 auto *N =
8045 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
8046 createOperands(N, {Val});
8047
8048 CSEMap.InsertNode(N, IP);
8049 InsertNode(N);
8050
8051 SDValue V(N, 0);
8052 NewSDValueDbgMsg(V, "Creating new node: ", this);
8053 return V;
8054}
8055
8056SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8057 SDValue N1, SDValue N2) {
8058 SDNodeFlags Flags;
8059 if (Inserter)
8060 Flags = Inserter->getFlags();
8061 return getNode(Opcode, DL, VT, N1, N2, Flags);
8062}
8063
8065 SDValue &N2) const {
8066 if (!TLI->isCommutativeBinOp(Opcode))
8067 return;
8068
8069 // Canonicalize:
8070 // binop(const, nonconst) -> binop(nonconst, const)
8073 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
8074 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
8075 if ((N1C && !N2C) || (N1CFP && !N2CFP))
8076 std::swap(N1, N2);
8077
8078 // Canonicalize:
8079 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
8080 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
8082 std::swap(N1, N2);
8083}
8084
8085SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8086 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
8088 N2.getOpcode() != ISD::DELETED_NODE &&
8089 "Operand is DELETED_NODE!");
8090
8091 canonicalizeCommutativeBinop(Opcode, N1, N2);
8092
8093 auto *N1C = dyn_cast<ConstantSDNode>(N1);
8094 auto *N2C = dyn_cast<ConstantSDNode>(N2);
8095
8096 // Don't allow undefs in vector splats - we might be returning N2 when folding
8097 // to zero etc.
8098 ConstantSDNode *N2CV =
8099 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
8100
8101 switch (Opcode) {
8102 default: break;
8103 case ISD::TokenFactor:
8104 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
8105 N2.getValueType() == MVT::Other && "Invalid token factor!");
8106 // Fold trivial token factors.
8107 if (N1.getOpcode() == ISD::EntryToken) return N2;
8108 if (N2.getOpcode() == ISD::EntryToken) return N1;
8109 if (N1 == N2) return N1;
8110 break;
8111 case ISD::BUILD_VECTOR: {
8112 // Attempt to simplify BUILD_VECTOR.
8113 SDValue Ops[] = {N1, N2};
8114 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8115 return V;
8116 break;
8117 }
8118 case ISD::CONCAT_VECTORS: {
8119 SDValue Ops[] = {N1, N2};
8120 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8121 return V;
8122 break;
8123 }
8124 case ISD::AND:
8125 assert(VT.isInteger() && "This operator does not apply to FP types!");
8126 assert(N1.getValueType() == N2.getValueType() &&
8127 N1.getValueType() == VT && "Binary operator types must match!");
8128 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
8129 // worth handling here.
8130 if (N2CV && N2CV->isZero())
8131 return N2;
8132 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
8133 return N1;
8134 break;
8135 case ISD::OR:
8136 case ISD::XOR:
8137 case ISD::ADD:
8138 case ISD::PTRADD:
8139 case ISD::SUB:
8140 assert(VT.isInteger() && "This operator does not apply to FP types!");
8141 assert(N1.getValueType() == N2.getValueType() &&
8142 N1.getValueType() == VT && "Binary operator types must match!");
8143 // The equal operand types requirement is unnecessarily strong for PTRADD.
8144 // However, the SelectionDAGBuilder does not generate PTRADDs with different
8145 // operand types, and we'd need to re-implement GEP's non-standard wrapping
8146 // logic everywhere where PTRADDs may be folded or combined to properly
8147 // support them. If/when we introduce pointer types to the SDAG, we will
8148 // need to relax this constraint.
8149
8150 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
8151 // it's worth handling here.
8152 if (N2CV && N2CV->isZero())
8153 return N1;
8154 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
8155 VT.getScalarType() == MVT::i1)
8156 return getNode(ISD::XOR, DL, VT, N1, N2);
8157 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
8158 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
8159 N2.getOpcode() == ISD::VSCALE) {
8160 const APInt &C1 = N1->getConstantOperandAPInt(0);
8161 const APInt &C2 = N2->getConstantOperandAPInt(0);
8162 return getVScale(DL, VT, C1 + C2);
8163 }
8164 break;
8165 case ISD::MUL:
8166 assert(VT.isInteger() && "This operator does not apply to FP types!");
8167 assert(N1.getValueType() == N2.getValueType() &&
8168 N1.getValueType() == VT && "Binary operator types must match!");
8169 if (VT.getScalarType() == MVT::i1)
8170 return getNode(ISD::AND, DL, VT, N1, N2);
8171 if (N2CV && N2CV->isZero())
8172 return N2;
8173 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
8174 const APInt &MulImm = N1->getConstantOperandAPInt(0);
8175 const APInt &N2CImm = N2C->getAPIntValue();
8176 return getVScale(DL, VT, MulImm * N2CImm);
8177 }
8178 break;
8179 case ISD::UDIV:
8180 case ISD::UREM:
8181 case ISD::MULHU:
8182 case ISD::MULHS:
8183 case ISD::SDIV:
8184 case ISD::SREM:
8185 case ISD::SADDSAT:
8186 case ISD::SSUBSAT:
8187 case ISD::UADDSAT:
8188 case ISD::USUBSAT:
8189 assert(VT.isInteger() && "This operator does not apply to FP types!");
8190 assert(N1.getValueType() == N2.getValueType() &&
8191 N1.getValueType() == VT && "Binary operator types must match!");
8192 if (VT.getScalarType() == MVT::i1) {
8193 // fold (add_sat x, y) -> (or x, y) for bool types.
8194 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
8195 return getNode(ISD::OR, DL, VT, N1, N2);
8196 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
8197 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
8198 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
8199 }
8200 break;
8201 case ISD::SCMP:
8202 case ISD::UCMP:
8203 assert(N1.getValueType() == N2.getValueType() &&
8204 "Types of operands of UCMP/SCMP must match");
8205 assert(N1.getValueType().isVector() == VT.isVector() &&
8206 "Operands and return type of must both be scalars or vectors");
8207 if (VT.isVector())
8210 "Result and operands must have the same number of elements");
8211 break;
8212 case ISD::AVGFLOORS:
8213 case ISD::AVGFLOORU:
8214 case ISD::AVGCEILS:
8215 case ISD::AVGCEILU:
8216 assert(VT.isInteger() && "This operator does not apply to FP types!");
8217 assert(N1.getValueType() == N2.getValueType() &&
8218 N1.getValueType() == VT && "Binary operator types must match!");
8219 break;
8220 case ISD::ABDS:
8221 case ISD::ABDU:
8222 assert(VT.isInteger() && "This operator does not apply to FP types!");
8223 assert(N1.getValueType() == N2.getValueType() &&
8224 N1.getValueType() == VT && "Binary operator types must match!");
8225 if (VT.getScalarType() == MVT::i1)
8226 return getNode(ISD::XOR, DL, VT, N1, N2);
8227 break;
8228 case ISD::SMIN:
8229 case ISD::UMAX:
8230 assert(VT.isInteger() && "This operator does not apply to FP types!");
8231 assert(N1.getValueType() == N2.getValueType() &&
8232 N1.getValueType() == VT && "Binary operator types must match!");
8233 if (VT.getScalarType() == MVT::i1)
8234 return getNode(ISD::OR, DL, VT, N1, N2);
8235 break;
8236 case ISD::SMAX:
8237 case ISD::UMIN:
8238 assert(VT.isInteger() && "This operator does not apply to FP types!");
8239 assert(N1.getValueType() == N2.getValueType() &&
8240 N1.getValueType() == VT && "Binary operator types must match!");
8241 if (VT.getScalarType() == MVT::i1)
8242 return getNode(ISD::AND, DL, VT, N1, N2);
8243 break;
8244 case ISD::FADD:
8245 case ISD::FSUB:
8246 case ISD::FMUL:
8247 case ISD::FDIV:
8248 case ISD::FREM:
8249 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
8250 assert(N1.getValueType() == N2.getValueType() &&
8251 N1.getValueType() == VT && "Binary operator types must match!");
8252 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
8253 return V;
8254 break;
8255 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
8256 assert(N1.getValueType() == VT &&
8259 "Invalid FCOPYSIGN!");
8260 break;
8261 case ISD::SHL:
8262 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
8263 const APInt &MulImm = N1->getConstantOperandAPInt(0);
8264 const APInt &ShiftImm = N2C->getAPIntValue();
8265 return getVScale(DL, VT, MulImm << ShiftImm);
8266 }
8267 [[fallthrough]];
8268 case ISD::SRA:
8269 case ISD::SRL:
8270 if (SDValue V = simplifyShift(N1, N2))
8271 return V;
8272 [[fallthrough]];
8273 case ISD::ROTL:
8274 case ISD::ROTR:
8275 case ISD::SSHLSAT:
8276 case ISD::USHLSAT:
8277 assert(VT == N1.getValueType() &&
8278 "Shift operators return type must be the same as their first arg");
8279 assert(VT.isInteger() && N2.getValueType().isInteger() &&
8280 "Shifts only work on integers");
8281 assert((!VT.isVector() || VT == N2.getValueType()) &&
8282 "Vector shift amounts must be in the same as their first arg");
8283 // Verify that the shift amount VT is big enough to hold valid shift
8284 // amounts. This catches things like trying to shift an i1024 value by an
8285 // i8, which is easy to fall into in generic code that uses
8286 // TLI.getShiftAmount().
8289 "Invalid use of small shift amount with oversized value!");
8290
8291 // Always fold shifts of i1 values so the code generator doesn't need to
8292 // handle them. Since we know the size of the shift has to be less than the
8293 // size of the value, the shift/rotate count is guaranteed to be zero.
8294 if (VT == MVT::i1)
8295 return N1;
8296 if (N2CV && N2CV->isZero())
8297 return N1;
8298 break;
8299 case ISD::FP_ROUND:
8301 VT.bitsLE(N1.getValueType()) && N2C &&
8302 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
8303 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
8304 if (N1.getValueType() == VT) return N1; // noop conversion.
8305 break;
8306 case ISD::IS_FPCLASS: {
8308 "IS_FPCLASS is used for a non-floating type");
8309 assert(isa<ConstantSDNode>(N2) && "FPClassTest is not Constant");
8310 FPClassTest Mask = static_cast<FPClassTest>(N2->getAsZExtVal());
8311 // If all tests are made, it doesn't matter what the value is.
8312 if ((Mask & fcAllFlags) == fcAllFlags)
8313 return getBoolConstant(true, DL, VT, N1.getValueType());
8314 if ((Mask & fcAllFlags) == 0)
8315 return getBoolConstant(false, DL, VT, N1.getValueType());
8316 break;
8317 }
8318 case ISD::AssertNoFPClass: {
8320 "AssertNoFPClass is used for a non-floating type");
8321 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
8322 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
8323 assert(llvm::to_underlying(NoFPClass) <=
8325 "FPClassTest value too large");
8326 (void)NoFPClass;
8327 break;
8328 }
8329 case ISD::AssertSext:
8330 case ISD::AssertZext: {
8331 EVT EVT = cast<VTSDNode>(N2)->getVT();
8332 assert(VT == N1.getValueType() && "Not an inreg extend!");
8333 assert(VT.isInteger() && EVT.isInteger() &&
8334 "Cannot *_EXTEND_INREG FP types");
8335 assert(!EVT.isVector() &&
8336 "AssertSExt/AssertZExt type should be the vector element type "
8337 "rather than the vector type!");
8338 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
8339 if (VT.getScalarType() == EVT) return N1; // noop assertion.
8340 break;
8341 }
8343 EVT EVT = cast<VTSDNode>(N2)->getVT();
8344 assert(VT == N1.getValueType() && "Not an inreg extend!");
8345 assert(VT.isInteger() && EVT.isInteger() &&
8346 "Cannot *_EXTEND_INREG FP types");
8347 assert(EVT.isVector() == VT.isVector() &&
8348 "SIGN_EXTEND_INREG type should be vector iff the operand "
8349 "type is vector!");
8350 assert((!EVT.isVector() ||
8352 "Vector element counts must match in SIGN_EXTEND_INREG");
8353 assert(EVT.getScalarType().bitsLE(VT.getScalarType()) && "Not extending!");
8354 if (EVT == VT) return N1; // Not actually extending
8355 break;
8356 }
8358 case ISD::FP_TO_UINT_SAT: {
8359 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
8360 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
8361 assert(N1.getValueType().isVector() == VT.isVector() &&
8362 "FP_TO_*INT_SAT type should be vector iff the operand type is "
8363 "vector!");
8364 assert((!VT.isVector() || VT.getVectorElementCount() ==
8366 "Vector element counts must match in FP_TO_*INT_SAT");
8367 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
8368 "Type to saturate to must be a scalar.");
8369 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
8370 "Not extending!");
8371 break;
8372 }
8375 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
8376 element type of the vector.");
8377
8378 // Extract from an undefined value or using an undefined index is undefined.
8379 if (N1.isUndef() || N2.isUndef())
8380 return getUNDEF(VT);
8381
8382 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
8383 // vectors. For scalable vectors we will provide appropriate support for
8384 // dealing with arbitrary indices.
8385 if (N2C && N1.getValueType().isFixedLengthVector() &&
8386 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
8387 return getUNDEF(VT);
8388
8389 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
8390 // expanding copies of large vectors from registers. This only works for
8391 // fixed length vectors, since we need to know the exact number of
8392 // elements.
8393 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
8395 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
8396 return getExtractVectorElt(DL, VT,
8397 N1.getOperand(N2C->getZExtValue() / Factor),
8398 N2C->getZExtValue() % Factor);
8399 }
8400
8401 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
8402 // lowering is expanding large vector constants.
8403 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
8404 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
8407 "BUILD_VECTOR used for scalable vectors");
8408 unsigned Index =
8409 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
8410 SDValue Elt = N1.getOperand(Index);
8411
8412 if (VT != Elt.getValueType())
8413 // If the vector element type is not legal, the BUILD_VECTOR operands
8414 // are promoted and implicitly truncated, and the result implicitly
8415 // extended. Make that explicit here.
8416 Elt = getAnyExtOrTrunc(Elt, DL, VT);
8417
8418 return Elt;
8419 }
8420
8421 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
8422 // operations are lowered to scalars.
8423 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
8424 // If the indices are the same, return the inserted element else
8425 // if the indices are known different, extract the element from
8426 // the original vector.
8427 SDValue N1Op2 = N1.getOperand(2);
8429
8430 if (N1Op2C && N2C) {
8431 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
8432 if (VT == N1.getOperand(1).getValueType())
8433 return N1.getOperand(1);
8434 if (VT.isFloatingPoint()) {
8436 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
8437 }
8438 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
8439 }
8440 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
8441 }
8442 }
8443
8444 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
8445 // when vector types are scalarized and v1iX is legal.
8446 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
8447 // Here we are completely ignoring the extract element index (N2),
8448 // which is fine for fixed width vectors, since any index other than 0
8449 // is undefined anyway. However, this cannot be ignored for scalable
8450 // vectors - in theory we could support this, but we don't want to do this
8451 // without a profitability check.
8452 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8454 N1.getValueType().getVectorNumElements() == 1) {
8455 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
8456 N1.getOperand(1));
8457 }
8458 break;
8460 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
8461 assert(!N1.getValueType().isVector() && !VT.isVector() &&
8462 (N1.getValueType().isInteger() == VT.isInteger()) &&
8463 N1.getValueType() != VT &&
8464 "Wrong types for EXTRACT_ELEMENT!");
8465
8466 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
8467 // 64-bit integers into 32-bit parts. Instead of building the extract of
8468 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
8469 if (N1.getOpcode() == ISD::BUILD_PAIR)
8470 return N1.getOperand(N2C->getZExtValue());
8471
8472 // EXTRACT_ELEMENT of a constant int is also very common.
8473 if (N1C) {
8474 unsigned ElementSize = VT.getSizeInBits();
8475 unsigned Shift = ElementSize * N2C->getZExtValue();
8476 const APInt &Val = N1C->getAPIntValue();
8477 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
8478 }
8479 break;
8481 EVT N1VT = N1.getValueType();
8482 assert(VT.isVector() && N1VT.isVector() &&
8483 "Extract subvector VTs must be vectors!");
8485 "Extract subvector VTs must have the same element type!");
8486 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8487 "Cannot extract a scalable vector from a fixed length vector!");
8488 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8490 "Extract subvector must be from larger vector to smaller vector!");
8491 assert(N2C && "Extract subvector index must be a constant");
8492 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8493 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8494 N1VT.getVectorMinNumElements()) &&
8495 "Extract subvector overflow!");
8496 assert(N2C->getAPIntValue().getBitWidth() ==
8497 TLI->getVectorIdxWidth(getDataLayout()) &&
8498 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8499 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8500 "Extract index is not a multiple of the output vector length");
8501
8502 // Trivial extraction.
8503 if (VT == N1VT)
8504 return N1;
8505
8506 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8507 if (N1.isUndef())
8508 return getUNDEF(VT);
8509
8510 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8511 // the concat have the same type as the extract.
8512 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8513 VT == N1.getOperand(0).getValueType()) {
8514 unsigned Factor = VT.getVectorMinNumElements();
8515 return N1.getOperand(N2C->getZExtValue() / Factor);
8516 }
8517
8518 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8519 // during shuffle legalization.
8520 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8521 VT == N1.getOperand(1).getValueType())
8522 return N1.getOperand(1);
8523 break;
8524 }
8525 }
8526
8527 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8528 switch (Opcode) {
8529 case ISD::XOR:
8530 case ISD::ADD:
8531 case ISD::PTRADD:
8532 case ISD::SUB:
8534 case ISD::UDIV:
8535 case ISD::SDIV:
8536 case ISD::UREM:
8537 case ISD::SREM:
8538 case ISD::MUL:
8539 case ISD::AND:
8540 case ISD::SSUBSAT:
8541 case ISD::USUBSAT:
8542 case ISD::UMIN:
8543 case ISD::OR:
8544 case ISD::SADDSAT:
8545 case ISD::UADDSAT:
8546 case ISD::UMAX:
8547 case ISD::SMAX:
8548 case ISD::SMIN:
8549 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8550 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8551 }
8552 }
8553
8554 // Canonicalize an UNDEF to the RHS, even over a constant.
8555 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8556 if (TLI->isCommutativeBinOp(Opcode)) {
8557 std::swap(N1, N2);
8558 } else {
8559 switch (Opcode) {
8560 case ISD::PTRADD:
8561 case ISD::SUB:
8562 // fold op(undef, non_undef_arg2) -> undef.
8563 return N1;
8565 case ISD::UDIV:
8566 case ISD::SDIV:
8567 case ISD::UREM:
8568 case ISD::SREM:
8569 case ISD::SSUBSAT:
8570 case ISD::USUBSAT:
8571 // fold op(undef, non_undef_arg2) -> 0.
8572 return getConstant(0, DL, VT);
8573 }
8574 }
8575 }
8576
8577 // Fold a bunch of operators when the RHS is undef.
8578 if (N2.getOpcode() == ISD::UNDEF) {
8579 switch (Opcode) {
8580 case ISD::XOR:
8581 if (N1.getOpcode() == ISD::UNDEF)
8582 // Handle undef ^ undef -> 0 special case. This is a common
8583 // idiom (misuse).
8584 return getConstant(0, DL, VT);
8585 [[fallthrough]];
8586 case ISD::ADD:
8587 case ISD::PTRADD:
8588 case ISD::SUB:
8589 // fold op(arg1, undef) -> undef.
8590 return N2;
8591 case ISD::UDIV:
8592 case ISD::SDIV:
8593 case ISD::UREM:
8594 case ISD::SREM:
8595 // fold op(arg1, undef) -> poison.
8596 return getPOISON(VT);
8597 case ISD::MUL:
8598 case ISD::AND:
8599 case ISD::SSUBSAT:
8600 case ISD::USUBSAT:
8601 case ISD::UMIN:
8602 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8603 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8604 case ISD::OR:
8605 case ISD::SADDSAT:
8606 case ISD::UADDSAT:
8607 case ISD::UMAX:
8608 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8609 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8610 case ISD::SMAX:
8611 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8612 return N1.getOpcode() == ISD::UNDEF
8613 ? N2
8614 : getConstant(
8616 VT);
8617 case ISD::SMIN:
8618 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8619 return N1.getOpcode() == ISD::UNDEF
8620 ? N2
8621 : getConstant(
8623 VT);
8624 }
8625 }
8626
8627 // Perform trivial constant folding.
8628 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8629 return SV;
8630
8631 // Memoize this node if possible.
8632 SDNode *N;
8633 SDVTList VTs = getVTList(VT);
8634 SDValue Ops[] = {N1, N2};
8635 if (VT != MVT::Glue) {
8637 AddNodeIDNode(ID, Opcode, VTs, Ops);
8638 void *IP = nullptr;
8639 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8640 E->intersectFlagsWith(Flags);
8641 return SDValue(E, 0);
8642 }
8643
8644 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8645 N->setFlags(Flags);
8646 createOperands(N, Ops);
8647 CSEMap.InsertNode(N, IP);
8648 } else {
8649 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8650 createOperands(N, Ops);
8651 }
8652
8653 InsertNode(N);
8654 SDValue V = SDValue(N, 0);
8655 NewSDValueDbgMsg(V, "Creating new node: ", this);
8656 return V;
8657}
8658
8659SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8660 SDValue N1, SDValue N2, SDValue N3) {
8661 SDNodeFlags Flags;
8662 if (Inserter)
8663 Flags = Inserter->getFlags();
8664 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8665}
8666
8667SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8668 SDValue N1, SDValue N2, SDValue N3,
8669 const SDNodeFlags Flags) {
8671 N2.getOpcode() != ISD::DELETED_NODE &&
8672 N3.getOpcode() != ISD::DELETED_NODE &&
8673 "Operand is DELETED_NODE!");
8674 // Perform various simplifications.
8675 switch (Opcode) {
8676 case ISD::BUILD_VECTOR: {
8677 // Attempt to simplify BUILD_VECTOR.
8678 SDValue Ops[] = {N1, N2, N3};
8679 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8680 return V;
8681 break;
8682 }
8683 case ISD::CONCAT_VECTORS: {
8684 SDValue Ops[] = {N1, N2, N3};
8685 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8686 return V;
8687 break;
8688 }
8689 case ISD::SETCC: {
8690 assert(VT.isInteger() && "SETCC result type must be an integer!");
8691 assert(N1.getValueType() == N2.getValueType() &&
8692 "SETCC operands must have the same type!");
8693 assert(VT.isVector() == N1.getValueType().isVector() &&
8694 "SETCC type should be vector iff the operand type is vector!");
8695 assert((!VT.isVector() || VT.getVectorElementCount() ==
8697 "SETCC vector element counts must match!");
8698 // Use FoldSetCC to simplify SETCC's.
8699 if (SDValue V =
8700 FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL, Flags))
8701 return V;
8702 break;
8703 }
8704 case ISD::SELECT:
8705 case ISD::VSELECT:
8706 if (SDValue V = simplifySelect(N1, N2, N3))
8707 return V;
8708 break;
8710 llvm_unreachable("should use getVectorShuffle constructor!");
8712 if (isNullConstant(N3))
8713 return N1;
8714 break;
8716 if (isNullConstant(N3))
8717 return N2;
8718 break;
8720 assert(VT.isVector() && VT == N1.getValueType() &&
8721 "INSERT_VECTOR_ELT vector type mismatch");
8723 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8724 assert((!VT.isFloatingPoint() ||
8725 VT.getVectorElementType() == N2.getValueType()) &&
8726 "INSERT_VECTOR_ELT fp scalar type mismatch");
8727 assert((!VT.isInteger() ||
8729 "INSERT_VECTOR_ELT int scalar size mismatch");
8730
8731 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8732 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8733 // for scalable vectors where we will generate appropriate code to
8734 // deal with out-of-bounds cases correctly.
8735 if (N3C && VT.isFixedLengthVector() &&
8736 N3C->getZExtValue() >= VT.getVectorNumElements())
8737 return getUNDEF(VT);
8738
8739 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8740 if (N3.isUndef())
8741 return getUNDEF(VT);
8742
8743 // If inserting poison, just use the input vector.
8744 if (N2.getOpcode() == ISD::POISON)
8745 return N1;
8746
8747 // Inserting undef into undef/poison is still undef.
8748 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8749 return getUNDEF(VT);
8750
8751 // If the inserted element is an UNDEF, just use the input vector.
8752 // But not if skipping the insert could make the result more poisonous.
8753 if (N2.isUndef()) {
8754 if (N3C && VT.isFixedLengthVector()) {
8755 APInt EltMask =
8756 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8757 if (isGuaranteedNotToBePoison(N1, EltMask))
8758 return N1;
8759 } else if (isGuaranteedNotToBePoison(N1))
8760 return N1;
8761 }
8762 break;
8763 }
8764 case ISD::INSERT_SUBVECTOR: {
8765 // If inserting poison, just use the input vector,
8766 if (N2.getOpcode() == ISD::POISON)
8767 return N1;
8768
8769 // Inserting undef into undef/poison is still undef.
8770 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8771 return getUNDEF(VT);
8772
8773 EVT N2VT = N2.getValueType();
8774 assert(VT == N1.getValueType() &&
8775 "Dest and insert subvector source types must match!");
8776 assert(VT.isVector() && N2VT.isVector() &&
8777 "Insert subvector VTs must be vectors!");
8779 "Insert subvector VTs must have the same element type!");
8780 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8781 "Cannot insert a scalable vector into a fixed length vector!");
8782 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8784 "Insert subvector must be from smaller vector to larger vector!");
8786 "Insert subvector index must be constant");
8787 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8788 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8790 "Insert subvector overflow!");
8792 TLI->getVectorIdxWidth(getDataLayout()) &&
8793 "Constant index for INSERT_SUBVECTOR has an invalid size");
8794
8795 // Trivial insertion.
8796 if (VT == N2VT)
8797 return N2;
8798
8799 // If this is an insert of an extracted vector into an undef/poison vector,
8800 // we can just use the input to the extract. But not if skipping the
8801 // extract+insert could make the result more poisonous.
8802 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8803 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8804 if (N1.getOpcode() == ISD::POISON)
8805 return N2.getOperand(0);
8806 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8807 unsigned LoBit = N3->getAsZExtVal();
8808 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8809 APInt EltMask =
8810 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8811 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8812 return N2.getOperand(0);
8813 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8814 return N2.getOperand(0);
8815 }
8816
8817 // If the inserted subvector is UNDEF, just use the input vector.
8818 // But not if skipping the insert could make the result more poisonous.
8819 if (N2.isUndef()) {
8820 if (VT.isFixedLengthVector()) {
8821 unsigned LoBit = N3->getAsZExtVal();
8822 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8823 APInt EltMask =
8824 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8825 if (isGuaranteedNotToBePoison(N1, EltMask))
8826 return N1;
8827 } else if (isGuaranteedNotToBePoison(N1))
8828 return N1;
8829 }
8830 break;
8831 }
8832 case ISD::BITCAST:
8833 // Fold bit_convert nodes from a type to themselves.
8834 if (N1.getValueType() == VT)
8835 return N1;
8836 break;
8837 case ISD::VP_TRUNCATE:
8838 case ISD::VP_SIGN_EXTEND:
8839 case ISD::VP_ZERO_EXTEND:
8840 // Don't create noop casts.
8841 if (N1.getValueType() == VT)
8842 return N1;
8843 break;
8844 case ISD::VECTOR_COMPRESS: {
8845 [[maybe_unused]] EVT VecVT = N1.getValueType();
8846 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8847 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8848 assert(VT == VecVT && "Vector and result type don't match.");
8849 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8850 "All inputs must be vectors.");
8851 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8853 "Vector and mask must have same number of elements.");
8854
8855 if (N1.isUndef() || N2.isUndef())
8856 return N3;
8857
8858 break;
8859 }
8864 [[maybe_unused]] EVT AccVT = N1.getValueType();
8865 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8866 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8867 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8868 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8869 "node to have the same type!");
8870 assert(VT.isVector() && VT == AccVT &&
8871 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8872 "the same type as its result!");
8874 AccVT.getVectorElementCount()) &&
8875 "Expected the element count of the second and third operands of the "
8876 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8877 "element count of the first operand and the result!");
8879 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8880 "node to have an element type which is the same as or smaller than "
8881 "the element type of the first operand and result!");
8882 break;
8883 }
8884 }
8885
8886 // Perform trivial constant folding for arithmetic operators.
8887 switch (Opcode) {
8888 case ISD::FMA:
8889 case ISD::FMAD:
8890 case ISD::SETCC:
8891 case ISD::FSHL:
8892 case ISD::FSHR:
8893 if (SDValue SV =
8894 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8895 return SV;
8896 break;
8897 }
8898
8899 // Memoize node if it doesn't produce a glue result.
8900 SDNode *N;
8901 SDVTList VTs = getVTList(VT);
8902 SDValue Ops[] = {N1, N2, N3};
8903 if (VT != MVT::Glue) {
8905 AddNodeIDNode(ID, Opcode, VTs, Ops);
8906 void *IP = nullptr;
8907 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8908 E->intersectFlagsWith(Flags);
8909 return SDValue(E, 0);
8910 }
8911
8912 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8913 N->setFlags(Flags);
8914 createOperands(N, Ops);
8915 CSEMap.InsertNode(N, IP);
8916 } else {
8917 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8918 createOperands(N, Ops);
8919 }
8920
8921 InsertNode(N);
8922 SDValue V = SDValue(N, 0);
8923 NewSDValueDbgMsg(V, "Creating new node: ", this);
8924 return V;
8925}
8926
8927SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8928 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8929 const SDNodeFlags Flags) {
8930 SDValue Ops[] = { N1, N2, N3, N4 };
8931 return getNode(Opcode, DL, VT, Ops, Flags);
8932}
8933
8934SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8935 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8936 SDNodeFlags Flags;
8937 if (Inserter)
8938 Flags = Inserter->getFlags();
8939 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8940}
8941
8942SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8943 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8944 SDValue N5, const SDNodeFlags Flags) {
8945 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8946 return getNode(Opcode, DL, VT, Ops, Flags);
8947}
8948
8949SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8950 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8951 SDValue N5) {
8952 SDNodeFlags Flags;
8953 if (Inserter)
8954 Flags = Inserter->getFlags();
8955 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8956}
8957
8958/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8959/// the incoming stack arguments to be loaded from the stack.
8961 SmallVector<SDValue, 8> ArgChains;
8962
8963 // Include the original chain at the beginning of the list. When this is
8964 // used by target LowerCall hooks, this helps legalize find the
8965 // CALLSEQ_BEGIN node.
8966 ArgChains.push_back(Chain);
8967
8968 // Add a chain value for each stack argument.
8969 for (SDNode *U : getEntryNode().getNode()->users())
8970 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8971 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8972 if (FI->getIndex() < 0)
8973 ArgChains.push_back(SDValue(L, 1));
8974
8975 // Build a tokenfactor for all the chains.
8976 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8977}
8978
8979/// getMemsetValue - Vectorized representation of the memset value
8980/// operand.
8982 const SDLoc &dl) {
8983 assert(!Value.isUndef());
8984
8985 unsigned NumBits = VT.getScalarSizeInBits();
8987 assert(C->getAPIntValue().getBitWidth() == 8);
8988 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8989 if (VT.isInteger()) {
8990 bool IsOpaque = VT.getSizeInBits() > 64 ||
8991 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8992 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8993 }
8994 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8995 }
8996
8997 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8998 EVT IntVT = VT.getScalarType();
8999 if (!IntVT.isInteger())
9000 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
9001
9002 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
9003 if (NumBits > 8) {
9004 // Use a multiplication with 0x010101... to extend the input to the
9005 // required length.
9006 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
9007 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
9008 DAG.getConstant(Magic, dl, IntVT));
9009 }
9010
9011 if (VT != Value.getValueType() && !VT.isInteger())
9012 Value = DAG.getBitcast(VT.getScalarType(), Value);
9013 if (VT != Value.getValueType())
9014 Value = DAG.getSplatBuildVector(VT, dl, Value);
9015
9016 return Value;
9017}
9018
9019/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
9020/// used when a memcpy is turned into a memset when the source is a constant
9021/// string ptr.
9023 const TargetLowering &TLI,
9024 const ConstantDataArraySlice &Slice) {
9025 // Handle vector with all elements zero.
9026 if (Slice.Array == nullptr) {
9027 if (VT.isInteger())
9028 return DAG.getConstant(0, dl, VT);
9029 return DAG.getNode(ISD::BITCAST, dl, VT,
9030 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
9031 }
9032
9033 assert(!VT.isVector() && "Can't handle vector type here!");
9034 unsigned NumVTBits = VT.getSizeInBits();
9035 unsigned NumVTBytes = NumVTBits / 8;
9036 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
9037
9038 APInt Val(NumVTBits, 0);
9039 if (DAG.getDataLayout().isLittleEndian()) {
9040 for (unsigned i = 0; i != NumBytes; ++i)
9041 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
9042 } else {
9043 for (unsigned i = 0; i != NumBytes; ++i)
9044 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
9045 }
9046
9047 // If the "cost" of materializing the integer immediate is less than the cost
9048 // of a load, then it is cost effective to turn the load into the immediate.
9049 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
9050 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
9051 return DAG.getConstant(Val, dl, VT);
9052 return SDValue();
9053}
9054
9056 const SDLoc &DL,
9057 const SDNodeFlags Flags) {
9058 SDValue Index = getTypeSize(DL, Base.getValueType(), Offset);
9059 return getMemBasePlusOffset(Base, Index, DL, Flags);
9060}
9061
9063 const SDLoc &DL,
9064 const SDNodeFlags Flags) {
9065 assert(Offset.getValueType().isInteger());
9066 EVT BasePtrVT = Ptr.getValueType();
9067 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
9068 BasePtrVT))
9069 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
9070 // InBounds only applies to PTRADD, don't set it if we generate ADD.
9071 SDNodeFlags AddFlags = Flags;
9072 AddFlags.setInBounds(false);
9073 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
9074}
9075
9076/// Returns true if memcpy source is constant data.
9078 uint64_t SrcDelta = 0;
9079 GlobalAddressSDNode *G = nullptr;
9080 if (Src.getOpcode() == ISD::GlobalAddress)
9082 else if (Src->isAnyAdd() &&
9083 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
9084 Src.getOperand(1).getOpcode() == ISD::Constant) {
9085 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
9086 SrcDelta = Src.getConstantOperandVal(1);
9087 }
9088 if (!G)
9089 return false;
9090
9091 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
9092 SrcDelta + G->getOffset());
9093}
9094
9096 SelectionDAG &DAG) {
9097 // On Darwin, -Os means optimize for size without hurting performance, so
9098 // only really optimize for size when -Oz (MinSize) is used.
9100 return MF.getFunction().hasMinSize();
9101 return DAG.shouldOptForSize();
9102}
9103
9105 SmallVector<SDValue, 32> &OutChains, unsigned From,
9106 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
9107 SmallVector<SDValue, 16> &OutStoreChains) {
9108 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
9109 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
9110 SmallVector<SDValue, 16> GluedLoadChains;
9111 for (unsigned i = From; i < To; ++i) {
9112 OutChains.push_back(OutLoadChains[i]);
9113 GluedLoadChains.push_back(OutLoadChains[i]);
9114 }
9115
9116 // Chain for all loads.
9117 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
9118 GluedLoadChains);
9119
9120 for (unsigned i = From; i < To; ++i) {
9121 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
9122 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
9123 ST->getBasePtr(), ST->getMemoryVT(),
9124 ST->getMemOperand());
9125 OutChains.push_back(NewStore);
9126 }
9127}
9128
9130 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
9131 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
9132 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
9133 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
9134 // Turn a memcpy of undef to nop.
9135 // FIXME: We need to honor volatile even is Src is undef.
9136 if (Src.isUndef())
9137 return Chain;
9138
9139 // Expand memcpy to a series of load and store ops if the size operand falls
9140 // below a certain threshold.
9141 // TODO: In the AlwaysInline case, if the size is big then generate a loop
9142 // rather than maybe a humongous number of loads and stores.
9143 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9144 const DataLayout &DL = DAG.getDataLayout();
9145 LLVMContext &C = *DAG.getContext();
9146 std::vector<EVT> MemOps;
9147 bool DstAlignCanChange = false;
9149 MachineFrameInfo &MFI = MF.getFrameInfo();
9150 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9152 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9153 DstAlignCanChange = true;
9154 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
9155 if (!SrcAlign || Alignment > *SrcAlign)
9156 SrcAlign = Alignment;
9157 assert(SrcAlign && "SrcAlign must be set");
9159 // If marked as volatile, perform a copy even when marked as constant.
9160 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
9161 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
9162 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
9163 const MemOp Op = isZeroConstant
9164 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
9165 /*IsZeroMemset*/ true, isVol)
9166 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
9167 *SrcAlign, isVol, CopyFromConstant);
9168 if (!TLI.findOptimalMemOpLowering(
9169 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
9170 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes(), nullptr))
9171 return SDValue();
9172
9173 if (DstAlignCanChange) {
9174 Type *Ty = MemOps[0].getTypeForEVT(C);
9175 Align NewAlign = DL.getABITypeAlign(Ty);
9176
9177 // Don't promote to an alignment that would require dynamic stack
9178 // realignment which may conflict with optimizations such as tail call
9179 // optimization.
9181 if (!TRI->hasStackRealignment(MF))
9182 if (MaybeAlign StackAlign = DL.getStackAlignment())
9183 NewAlign = std::min(NewAlign, *StackAlign);
9184
9185 if (NewAlign > Alignment) {
9186 // Give the stack frame object a larger alignment if needed.
9187 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9188 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9189 Alignment = NewAlign;
9190 }
9191 }
9192
9193 // Prepare AAInfo for loads/stores after lowering this memcpy.
9194 AAMDNodes NewAAInfo = AAInfo;
9195 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9196
9197 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
9198 bool isConstant =
9199 BatchAA && SrcVal &&
9200 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
9201
9202 MachineMemOperand::Flags MMOFlags =
9204 SmallVector<SDValue, 16> OutLoadChains;
9205 SmallVector<SDValue, 16> OutStoreChains;
9206 SmallVector<SDValue, 32> OutChains;
9207 unsigned NumMemOps = MemOps.size();
9208 uint64_t SrcOff = 0, DstOff = 0;
9209 for (unsigned i = 0; i != NumMemOps; ++i) {
9210 EVT VT = MemOps[i];
9211 unsigned VTSize = VT.getSizeInBits() / 8;
9212 SDValue Value, Store;
9213
9214 if (VTSize > Size) {
9215 // Issuing an unaligned load / store pair that overlaps with the previous
9216 // pair. Adjust the offset accordingly.
9217 assert(i == NumMemOps-1 && i != 0);
9218 SrcOff -= VTSize - Size;
9219 DstOff -= VTSize - Size;
9220 }
9221
9222 if (CopyFromConstant &&
9223 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
9224 // It's unlikely a store of a vector immediate can be done in a single
9225 // instruction. It would require a load from a constantpool first.
9226 // We only handle zero vectors here.
9227 // FIXME: Handle other cases where store of vector immediate is done in
9228 // a single instruction.
9229 ConstantDataArraySlice SubSlice;
9230 if (SrcOff < Slice.Length) {
9231 SubSlice = Slice;
9232 SubSlice.move(SrcOff);
9233 } else {
9234 // This is an out-of-bounds access and hence UB. Pretend we read zero.
9235 SubSlice.Array = nullptr;
9236 SubSlice.Offset = 0;
9237 SubSlice.Length = VTSize;
9238 }
9239 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
9240 if (Value.getNode()) {
9241 Store = DAG.getStore(
9242 Chain, dl, Value,
9243 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9244 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
9245 OutChains.push_back(Store);
9246 }
9247 }
9248
9249 if (!Store.getNode()) {
9250 // The type might not be legal for the target. This should only happen
9251 // if the type is smaller than a legal type, as on PPC, so the right
9252 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
9253 // to Load/Store if NVT==VT.
9254 // FIXME does the case above also need this?
9255 EVT NVT = TLI.getTypeToTransformTo(C, VT);
9256 assert(NVT.bitsGE(VT));
9257
9258 bool isDereferenceable =
9259 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
9260 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
9261 if (isDereferenceable)
9263 if (isConstant)
9264 SrcMMOFlags |= MachineMemOperand::MOInvariant;
9265
9266 Value = DAG.getExtLoad(
9267 ISD::EXTLOAD, dl, NVT, Chain,
9268 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
9269 SrcPtrInfo.getWithOffset(SrcOff), VT,
9270 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
9271 OutLoadChains.push_back(Value.getValue(1));
9272
9273 Store = DAG.getTruncStore(
9274 Chain, dl, Value,
9275 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9276 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
9277 OutStoreChains.push_back(Store);
9278 }
9279 SrcOff += VTSize;
9280 DstOff += VTSize;
9281 Size -= VTSize;
9282 }
9283
9284 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
9286 unsigned NumLdStInMemcpy = OutStoreChains.size();
9287
9288 if (NumLdStInMemcpy) {
9289 // It may be that memcpy might be converted to memset if it's memcpy
9290 // of constants. In such a case, we won't have loads and stores, but
9291 // just stores. In the absence of loads, there is nothing to gang up.
9292 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
9293 // If target does not care, just leave as it.
9294 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
9295 OutChains.push_back(OutLoadChains[i]);
9296 OutChains.push_back(OutStoreChains[i]);
9297 }
9298 } else {
9299 // Ld/St less than/equal limit set by target.
9300 if (NumLdStInMemcpy <= GluedLdStLimit) {
9301 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
9302 NumLdStInMemcpy, OutLoadChains,
9303 OutStoreChains);
9304 } else {
9305 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
9306 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
9307 unsigned GlueIter = 0;
9308
9309 // Residual ld/st.
9310 if (RemainingLdStInMemcpy) {
9312 DAG, dl, OutChains, NumLdStInMemcpy - RemainingLdStInMemcpy,
9313 NumLdStInMemcpy, OutLoadChains, OutStoreChains);
9314 }
9315
9316 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
9317 unsigned IndexFrom = NumLdStInMemcpy - RemainingLdStInMemcpy -
9318 GlueIter - GluedLdStLimit;
9319 unsigned IndexTo = NumLdStInMemcpy - RemainingLdStInMemcpy - GlueIter;
9320 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
9321 OutLoadChains, OutStoreChains);
9322 GlueIter += GluedLdStLimit;
9323 }
9324 }
9325 }
9326 }
9327 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9328}
9329
9331 SDValue Chain, SDValue Dst, SDValue Src,
9332 uint64_t Size, Align Alignment,
9333 bool isVol, bool AlwaysInline,
9334 MachinePointerInfo DstPtrInfo,
9335 MachinePointerInfo SrcPtrInfo,
9336 const AAMDNodes &AAInfo) {
9337 // Turn a memmove of undef to nop.
9338 // FIXME: We need to honor volatile even is Src is undef.
9339 if (Src.isUndef())
9340 return Chain;
9341
9342 // Expand memmove to a series of load and store ops if the size operand falls
9343 // below a certain threshold.
9344 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9345 const DataLayout &DL = DAG.getDataLayout();
9346 LLVMContext &C = *DAG.getContext();
9347 std::vector<EVT> MemOps;
9348 bool DstAlignCanChange = false;
9350 MachineFrameInfo &MFI = MF.getFrameInfo();
9351 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9353 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9354 DstAlignCanChange = true;
9355 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
9356 if (!SrcAlign || Alignment > *SrcAlign)
9357 SrcAlign = Alignment;
9358 assert(SrcAlign && "SrcAlign must be set");
9359 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
9360 if (!TLI.findOptimalMemOpLowering(
9361 C, MemOps, Limit,
9362 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign, isVol),
9363 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
9364 MF.getFunction().getAttributes(), nullptr))
9365 return SDValue();
9366
9367 if (DstAlignCanChange) {
9368 Type *Ty = MemOps[0].getTypeForEVT(C);
9369 Align NewAlign = DL.getABITypeAlign(Ty);
9370
9371 // Don't promote to an alignment that would require dynamic stack
9372 // realignment which may conflict with optimizations such as tail call
9373 // optimization.
9375 if (!TRI->hasStackRealignment(MF))
9376 if (MaybeAlign StackAlign = DL.getStackAlignment())
9377 NewAlign = std::min(NewAlign, *StackAlign);
9378
9379 if (NewAlign > Alignment) {
9380 // Give the stack frame object a larger alignment if needed.
9381 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9382 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9383 Alignment = NewAlign;
9384 }
9385 }
9386
9387 // Prepare AAInfo for loads/stores after lowering this memmove.
9388 AAMDNodes NewAAInfo = AAInfo;
9389 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9390
9391 MachineMemOperand::Flags MMOFlags =
9393 uint64_t SrcOff = 0;
9394 SmallVector<SDValue, 8> LoadValues;
9395 SmallVector<SDValue, 8> LoadChains;
9396 SmallVector<SDValue, 8> OutChains;
9397 unsigned NumMemOps = MemOps.size();
9398 for (unsigned i = 0; i < NumMemOps; i++) {
9399 EVT VT = MemOps[i];
9400 unsigned VTSize = VT.getSizeInBits() / 8;
9401 SDValue Value;
9402 bool IsOverlapping = false;
9403
9404 if (i == NumMemOps - 1 && i != 0 && VTSize > Size - SrcOff) {
9405 // Issuing an unaligned load / store pair that overlaps with the previous
9406 // pair. Adjust the offset accordingly.
9407 SrcOff = Size - VTSize;
9408 IsOverlapping = true;
9409 }
9410
9411 // Calculate the actual alignment at the current offset. The alignment at
9412 // SrcOff may be lower than the base alignment, especially when using
9413 // overlapping loads.
9414 Align SrcAlignAtOffset = commonAlignment(*SrcAlign, SrcOff);
9415 if (IsOverlapping) {
9416 // Verify that the target allows misaligned memory accesses at the
9417 // adjusted offset when using overlapping loads.
9418 unsigned Fast;
9419 if (!TLI.allowsMisalignedMemoryAccesses(VT, SrcPtrInfo.getAddrSpace(),
9420 SrcAlignAtOffset, MMOFlags,
9421 &Fast) ||
9422 !Fast) {
9423 // This should have been caught by findOptimalMemOpLowering, but verify
9424 // here for safety.
9425 return SDValue();
9426 }
9427 }
9428
9429 bool isDereferenceable =
9430 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
9431 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
9432 if (isDereferenceable)
9434 Value =
9435 DAG.getLoad(VT, dl, Chain,
9436 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
9437 SrcPtrInfo.getWithOffset(SrcOff), SrcAlignAtOffset,
9438 SrcMMOFlags, NewAAInfo);
9439 LoadValues.push_back(Value);
9440 LoadChains.push_back(Value.getValue(1));
9441 SrcOff += VTSize;
9442 }
9443 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
9444 OutChains.clear();
9445 uint64_t DstOff = 0;
9446 for (unsigned i = 0; i < NumMemOps; i++) {
9447 EVT VT = MemOps[i];
9448 unsigned VTSize = VT.getSizeInBits() / 8;
9449 SDValue Store;
9450 bool IsOverlapping = false;
9451
9452 if (i == NumMemOps - 1 && i != 0 && VTSize > Size - DstOff) {
9453 // Issuing an unaligned load / store pair that overlaps with the previous
9454 // pair. Adjust the offset accordingly.
9455 DstOff = Size - VTSize;
9456 IsOverlapping = true;
9457 }
9458
9459 // Calculate the actual alignment at the current offset. The alignment at
9460 // DstOff may be lower than the base alignment, especially when using
9461 // overlapping stores.
9462 Align DstAlignAtOffset = commonAlignment(Alignment, DstOff);
9463 if (IsOverlapping) {
9464 // Verify that the target allows misaligned memory accesses at the
9465 // adjusted offset when using overlapping stores.
9466 unsigned Fast;
9467 if (!TLI.allowsMisalignedMemoryAccesses(VT, DstPtrInfo.getAddrSpace(),
9468 DstAlignAtOffset, MMOFlags,
9469 &Fast) ||
9470 !Fast) {
9471 // This should have been caught by findOptimalMemOpLowering, but verify
9472 // here for safety.
9473 return SDValue();
9474 }
9475 }
9476 Store = DAG.getStore(
9477 Chain, dl, LoadValues[i],
9478 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9479 DstPtrInfo.getWithOffset(DstOff), DstAlignAtOffset, MMOFlags,
9480 NewAAInfo);
9481 OutChains.push_back(Store);
9482 DstOff += VTSize;
9483 }
9484
9485 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9486}
9487
9488/// Lower the call to 'memset' intrinsic function into a series of store
9489/// operations.
9490///
9491/// \param DAG Selection DAG where lowered code is placed.
9492/// \param dl Link to corresponding IR location.
9493/// \param Chain Control flow dependency.
9494/// \param Dst Pointer to destination memory location.
9495/// \param Src Value of byte to write into the memory.
9496/// \param Size Number of bytes to write.
9497/// \param Alignment Alignment of the destination in bytes.
9498/// \param isVol True if destination is volatile.
9499/// \param AlwaysInline Makes sure no function call is generated.
9500/// \param DstPtrInfo IR information on the memory pointer.
9501/// \returns New head in the control flow, if lowering was successful, empty
9502/// SDValue otherwise.
9503///
9504/// The function tries to replace 'llvm.memset' intrinsic with several store
9505/// operations and value calculation code. This is usually profitable for small
9506/// memory size or when the semantic requires inlining.
9508 SDValue Chain, SDValue Dst, SDValue Src,
9509 uint64_t Size, Align Alignment, bool isVol,
9510 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
9511 const AAMDNodes &AAInfo) {
9512 // Turn a memset of undef to nop.
9513 // FIXME: We need to honor volatile even is Src is undef.
9514 if (Src.isUndef())
9515 return Chain;
9516
9517 // Expand memset to a series of load/store ops if the size operand
9518 // falls below a certain threshold.
9519 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9520 std::vector<EVT> MemOps;
9521 bool DstAlignCanChange = false;
9522 LLVMContext &C = *DAG.getContext();
9524 MachineFrameInfo &MFI = MF.getFrameInfo();
9525 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9527 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9528 DstAlignCanChange = true;
9529 bool IsZeroVal = isNullConstant(Src);
9530 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
9531
9532 EVT LargestVT;
9533 if (!TLI.findOptimalMemOpLowering(
9534 C, MemOps, Limit,
9535 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
9536 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes(),
9537 &LargestVT))
9538 return SDValue();
9539
9540 if (DstAlignCanChange) {
9541 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9542 const DataLayout &DL = DAG.getDataLayout();
9543 Align NewAlign = DL.getABITypeAlign(Ty);
9544
9545 // Don't promote to an alignment that would require dynamic stack
9546 // realignment which may conflict with optimizations such as tail call
9547 // optimization.
9549 if (!TRI->hasStackRealignment(MF))
9550 if (MaybeAlign StackAlign = DL.getStackAlignment())
9551 NewAlign = std::min(NewAlign, *StackAlign);
9552
9553 if (NewAlign > Alignment) {
9554 // Give the stack frame object a larger alignment if needed.
9555 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9556 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9557 Alignment = NewAlign;
9558 }
9559 }
9560
9561 SmallVector<SDValue, 8> OutChains;
9562 uint64_t DstOff = 0;
9563 unsigned NumMemOps = MemOps.size();
9564
9565 // Find the largest store and generate the bit pattern for it.
9566 // If target didn't set LargestVT, compute it from MemOps.
9567 if (!LargestVT.isSimple()) {
9568 LargestVT = MemOps[0];
9569 for (unsigned i = 1; i < NumMemOps; i++)
9570 if (MemOps[i].bitsGT(LargestVT))
9571 LargestVT = MemOps[i];
9572 }
9573 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9574
9575 // Prepare AAInfo for loads/stores after lowering this memset.
9576 AAMDNodes NewAAInfo = AAInfo;
9577 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9578
9579 for (unsigned i = 0; i < NumMemOps; i++) {
9580 EVT VT = MemOps[i];
9581 unsigned VTSize = VT.getSizeInBits() / 8;
9582 // The target should specify store types that exactly cover the memset size
9583 // (with the last store potentially being oversized for overlapping stores).
9584 assert(Size > 0 && "Target specified more stores than needed in "
9585 "findOptimalMemOpLowering");
9586 if (VTSize > Size) {
9587 // Issuing an unaligned load / store pair that overlaps with the previous
9588 // pair. Adjust the offset accordingly.
9589 assert(i == NumMemOps-1 && i != 0);
9590 DstOff -= VTSize - Size;
9591 }
9592
9593 // If this store is smaller than the largest store see whether we can get
9594 // the smaller value for free with a truncate or extract vector element and
9595 // then store.
9596 SDValue Value = MemSetValue;
9597 if (VT.bitsLT(LargestVT)) {
9598 unsigned Index;
9599 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9600 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9601 if (!LargestVT.isVector() && !VT.isVector() &&
9602 TLI.isTruncateFree(LargestVT, VT))
9603 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9604 else if (LargestVT.isVector() && !VT.isVector() &&
9606 LargestVT.getTypeForEVT(*DAG.getContext()),
9607 VT.getSizeInBits(), Index) &&
9608 TLI.isTypeLegal(SVT) &&
9609 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9610 // Target which can combine store(extractelement VectorTy, Idx) can get
9611 // the smaller value for free.
9612 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9613 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9614 } else
9615 Value = getMemsetValue(Src, VT, DAG, dl);
9616 }
9617 assert(Value.getValueType() == VT && "Value with wrong type.");
9618 SDValue Store = DAG.getStore(
9619 Chain, dl, Value,
9620 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9621 DstPtrInfo.getWithOffset(DstOff), Alignment,
9623 NewAAInfo);
9624 OutChains.push_back(Store);
9625 DstOff += VT.getSizeInBits() / 8;
9626 // For oversized overlapping stores, only subtract the remaining bytes.
9627 // For normal stores, subtract the full store size.
9628 if (VTSize > Size) {
9629 Size = 0;
9630 } else {
9631 Size -= VTSize;
9632 }
9633 }
9634
9635 // After processing all stores, Size should be exactly 0. Any remaining bytes
9636 // indicate a bug in the target's findOptimalMemOpLowering implementation.
9637 assert(Size == 0 && "Target's findOptimalMemOpLowering did not specify "
9638 "stores that exactly cover the memset size");
9639
9640 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9641}
9642
9644 unsigned AS) {
9645 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9646 // pointer operands can be losslessly bitcasted to pointers of address space 0
9647 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9648 report_fatal_error("cannot lower memory intrinsic in address space " +
9649 Twine(AS));
9650 }
9651}
9652
9654 const SelectionDAG *SelDAG,
9655 bool AllowReturnsFirstArg) {
9656 if (!CI || !CI->isTailCall())
9657 return false;
9658 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9659 // helper symbol we lower to.
9660 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9661 AllowReturnsFirstArg &&
9663}
9664
9665static std::pair<SDValue, SDValue>
9668 const CallInst *CI, RTLIB::Libcall Call,
9669 SelectionDAG *DAG, const TargetLowering *TLI) {
9670 RTLIB::LibcallImpl LCImpl = DAG->getLibcalls().getLibcallImpl(Call);
9671
9672 if (LCImpl == RTLIB::Unsupported)
9673 return {};
9674
9676 bool IsTailCall =
9677 isInTailCallPositionWrapper(CI, DAG, /*AllowReturnsFirstArg=*/true);
9678 SDValue Callee =
9679 DAG->getExternalSymbol(LCImpl, TLI->getPointerTy(DAG->getDataLayout()));
9680
9681 CLI.setDebugLoc(dl)
9682 .setChain(Chain)
9684 CI->getType(), Callee, std::move(Args))
9685 .setTailCall(IsTailCall);
9686
9687 return TLI->LowerCallTo(CLI);
9688}
9689
9690std::pair<SDValue, SDValue> SelectionDAG::getStrcmp(SDValue Chain,
9691 const SDLoc &dl, SDValue S1,
9692 SDValue S2,
9693 const CallInst *CI) {
9695 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9696 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9697 RTLIB::STRCMP, this, TLI);
9698}
9699
9700std::pair<SDValue, SDValue> SelectionDAG::getStrstr(SDValue Chain,
9701 const SDLoc &dl, SDValue S1,
9702 SDValue S2,
9703 const CallInst *CI) {
9705 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9706 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9707 RTLIB::STRSTR, this, TLI);
9708}
9709
9710std::pair<SDValue, SDValue> SelectionDAG::getMemccpy(SDValue Chain,
9711 const SDLoc &dl,
9712 SDValue Dst, SDValue Src,
9714 const CallInst *CI) {
9716
9718 {Dst, PT},
9719 {Src, PT},
9722 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9723 RTLIB::MEMCCPY, this, TLI);
9724}
9725
9726std::pair<SDValue, SDValue>
9728 SDValue Mem1, SDValue Size, const CallInst *CI) {
9731 {Mem0, PT},
9732 {Mem1, PT},
9734 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9735 RTLIB::MEMCMP, this, TLI);
9736}
9737
9738std::pair<SDValue, SDValue> SelectionDAG::getStrcpy(SDValue Chain,
9739 const SDLoc &dl,
9740 SDValue Dst, SDValue Src,
9741 const CallInst *CI) {
9743 TargetLowering::ArgListTy Args = {{Dst, PT}, {Src, PT}};
9744 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9745 RTLIB::STRCPY, this, TLI);
9746}
9747
9748std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9749 const SDLoc &dl,
9750 SDValue Src,
9751 const CallInst *CI) {
9752 // Emit a library call.
9755 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9756 RTLIB::STRLEN, this, TLI);
9757}
9758
9760 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9761 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9762 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9763 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9764 BatchAAResults *BatchAA) {
9765 // Check to see if we should lower the memcpy to loads and stores first.
9766 // For cases within the target-specified limits, this is the best choice.
9768 if (ConstantSize) {
9769 // Memcpy with size zero? Just return the original chain.
9770 if (ConstantSize->isZero())
9771 return Chain;
9772
9774 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9775 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9776 if (Result.getNode())
9777 return Result;
9778 }
9779
9780 // Then check to see if we should lower the memcpy with target-specific
9781 // code. If the target chooses to do this, this is the next best.
9782 if (TSI) {
9783 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9784 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9785 DstPtrInfo, SrcPtrInfo);
9786 if (Result.getNode())
9787 return Result;
9788 }
9789
9790 // If we really need inline code and the target declined to provide it,
9791 // use a (potentially long) sequence of loads and stores.
9792 if (AlwaysInline) {
9793 assert(ConstantSize && "AlwaysInline requires a constant size!");
9795 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9796 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9797 }
9798
9801
9802 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9803 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9804 // respect volatile, so they may do things like read or write memory
9805 // beyond the given memory regions. But fixing this isn't easy, and most
9806 // people don't care.
9807
9808 // Emit a library call.
9811 Args.emplace_back(Dst, PtrTy);
9812 Args.emplace_back(Src, PtrTy);
9813 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9814 // FIXME: pass in SDLoc
9816 bool IsTailCall = false;
9817 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9818
9819 if (OverrideTailCall.has_value()) {
9820 IsTailCall = *OverrideTailCall;
9821 } else {
9822 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9823 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9824 }
9825
9826 CLI.setDebugLoc(dl)
9827 .setChain(Chain)
9828 .setLibCallee(
9829 Libcalls->getLibcallImplCallingConv(MemCpyImpl),
9830 Dst.getValueType().getTypeForEVT(*getContext()),
9831 getExternalSymbol(MemCpyImpl, TLI->getPointerTy(getDataLayout())),
9832 std::move(Args))
9834 .setTailCall(IsTailCall);
9835
9836 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9837 return CallResult.second;
9838}
9839
9841 SDValue Dst, SDValue Src, SDValue Size,
9842 Type *SizeTy, unsigned ElemSz,
9843 bool isTailCall,
9844 MachinePointerInfo DstPtrInfo,
9845 MachinePointerInfo SrcPtrInfo) {
9846 // Emit a library call.
9849 Args.emplace_back(Dst, ArgTy);
9850 Args.emplace_back(Src, ArgTy);
9851 Args.emplace_back(Size, SizeTy);
9852
9853 RTLIB::Libcall LibraryCall =
9855 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9856 if (LibcallImpl == RTLIB::Unsupported)
9857 report_fatal_error("Unsupported element size");
9858
9860 CLI.setDebugLoc(dl)
9861 .setChain(Chain)
9862 .setLibCallee(
9863 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9865 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9866 std::move(Args))
9868 .setTailCall(isTailCall);
9869
9870 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9871 return CallResult.second;
9872}
9873
9875 SDValue Src, SDValue Size, Align Alignment,
9876 bool isVol, const CallInst *CI,
9877 std::optional<bool> OverrideTailCall,
9878 MachinePointerInfo DstPtrInfo,
9879 MachinePointerInfo SrcPtrInfo,
9880 const AAMDNodes &AAInfo,
9881 BatchAAResults *BatchAA) {
9882 // Check to see if we should lower the memmove to loads and stores first.
9883 // For cases within the target-specified limits, this is the best choice.
9885 if (ConstantSize) {
9886 // Memmove with size zero? Just return the original chain.
9887 if (ConstantSize->isZero())
9888 return Chain;
9889
9891 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9892 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9893 if (Result.getNode())
9894 return Result;
9895 }
9896
9897 // Then check to see if we should lower the memmove with target-specific
9898 // code. If the target chooses to do this, this is the next best.
9899 if (TSI) {
9900 SDValue Result =
9901 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9902 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9903 if (Result.getNode())
9904 return Result;
9905 }
9906
9909
9910 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9911 // not be safe. See memcpy above for more details.
9912
9913 // Emit a library call.
9916 Args.emplace_back(Dst, PtrTy);
9917 Args.emplace_back(Src, PtrTy);
9918 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9919 // FIXME: pass in SDLoc
9921
9922 RTLIB::LibcallImpl MemmoveImpl = Libcalls->getLibcallImpl(RTLIB::MEMMOVE);
9923
9924 bool IsTailCall = false;
9925 if (OverrideTailCall.has_value()) {
9926 IsTailCall = *OverrideTailCall;
9927 } else {
9928 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9929 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9930 }
9931
9932 CLI.setDebugLoc(dl)
9933 .setChain(Chain)
9934 .setLibCallee(
9935 Libcalls->getLibcallImplCallingConv(MemmoveImpl),
9936 Dst.getValueType().getTypeForEVT(*getContext()),
9937 getExternalSymbol(MemmoveImpl, TLI->getPointerTy(getDataLayout())),
9938 std::move(Args))
9940 .setTailCall(IsTailCall);
9941
9942 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9943 return CallResult.second;
9944}
9945
9947 SDValue Dst, SDValue Src, SDValue Size,
9948 Type *SizeTy, unsigned ElemSz,
9949 bool isTailCall,
9950 MachinePointerInfo DstPtrInfo,
9951 MachinePointerInfo SrcPtrInfo) {
9952 // Emit a library call.
9954 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9955 Args.emplace_back(Dst, IntPtrTy);
9956 Args.emplace_back(Src, IntPtrTy);
9957 Args.emplace_back(Size, SizeTy);
9958
9959 RTLIB::Libcall LibraryCall =
9961 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9962 if (LibcallImpl == RTLIB::Unsupported)
9963 report_fatal_error("Unsupported element size");
9964
9966 CLI.setDebugLoc(dl)
9967 .setChain(Chain)
9968 .setLibCallee(
9969 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9971 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9972 std::move(Args))
9974 .setTailCall(isTailCall);
9975
9976 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9977 return CallResult.second;
9978}
9979
9981 SDValue Src, SDValue Size, Align Alignment,
9982 bool isVol, bool AlwaysInline,
9983 const CallInst *CI,
9984 MachinePointerInfo DstPtrInfo,
9985 const AAMDNodes &AAInfo) {
9986 // Check to see if we should lower the memset to stores first.
9987 // For cases within the target-specified limits, this is the best choice.
9989 if (ConstantSize) {
9990 // Memset with size zero? Just return the original chain.
9991 if (ConstantSize->isZero())
9992 return Chain;
9993
9994 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9995 ConstantSize->getZExtValue(), Alignment,
9996 isVol, false, DstPtrInfo, AAInfo);
9997
9998 if (Result.getNode())
9999 return Result;
10000 }
10001
10002 // Then check to see if we should lower the memset with target-specific
10003 // code. If the target chooses to do this, this is the next best.
10004 if (TSI) {
10005 SDValue Result = TSI->EmitTargetCodeForMemset(
10006 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
10007 if (Result.getNode())
10008 return Result;
10009 }
10010
10011 // If we really need inline code and the target declined to provide it,
10012 // use a (potentially long) sequence of loads and stores.
10013 if (AlwaysInline) {
10014 assert(ConstantSize && "AlwaysInline requires a constant size!");
10015 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
10016 ConstantSize->getZExtValue(), Alignment,
10017 isVol, true, DstPtrInfo, AAInfo);
10018 assert(Result &&
10019 "getMemsetStores must return a valid sequence when AlwaysInline");
10020 return Result;
10021 }
10022
10024
10025 // Emit a library call.
10026 auto &Ctx = *getContext();
10027 const auto& DL = getDataLayout();
10028
10030 // FIXME: pass in SDLoc
10031 CLI.setDebugLoc(dl).setChain(Chain);
10032
10033 RTLIB::LibcallImpl BzeroImpl = Libcalls->getLibcallImpl(RTLIB::BZERO);
10034 bool UseBZero = BzeroImpl != RTLIB::Unsupported && isNullConstant(Src);
10035
10036 // If zeroing out and bzero is present, use it.
10037 if (UseBZero) {
10039 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
10040 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
10041 CLI.setLibCallee(
10042 Libcalls->getLibcallImplCallingConv(BzeroImpl), Type::getVoidTy(Ctx),
10043 getExternalSymbol(BzeroImpl, TLI->getPointerTy(DL)), std::move(Args));
10044 } else {
10045 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
10046
10048 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
10049 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
10050 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
10051 CLI.setLibCallee(Libcalls->getLibcallImplCallingConv(MemsetImpl),
10052 Dst.getValueType().getTypeForEVT(Ctx),
10053 getExternalSymbol(MemsetImpl, TLI->getPointerTy(DL)),
10054 std::move(Args));
10055 }
10056
10057 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
10058 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
10059
10060 // If we're going to use bzero, make sure not to tail call unless the
10061 // subsequent return doesn't need a value, as bzero doesn't return the first
10062 // arg unlike memset.
10063 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
10064 bool IsTailCall =
10065 CI && CI->isTailCall() &&
10066 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
10067 CLI.setDiscardResult().setTailCall(IsTailCall);
10068
10069 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
10070 return CallResult.second;
10071}
10072
10075 Type *SizeTy, unsigned ElemSz,
10076 bool isTailCall,
10077 MachinePointerInfo DstPtrInfo) {
10078 // Emit a library call.
10080 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
10081 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
10082 Args.emplace_back(Size, SizeTy);
10083
10084 RTLIB::Libcall LibraryCall =
10086 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
10087 if (LibcallImpl == RTLIB::Unsupported)
10088 report_fatal_error("Unsupported element size");
10089
10091 CLI.setDebugLoc(dl)
10092 .setChain(Chain)
10093 .setLibCallee(
10094 Libcalls->getLibcallImplCallingConv(LibcallImpl),
10096 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
10097 std::move(Args))
10099 .setTailCall(isTailCall);
10100
10101 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
10102 return CallResult.second;
10103}
10104
10105SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
10107 MachineMemOperand *MMO,
10108 ISD::LoadExtType ExtType) {
10110 AddNodeIDNode(ID, Opcode, VTList, Ops);
10111 ID.AddInteger(MemVT.getRawBits());
10112 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
10113 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
10114 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10115 ID.AddInteger(MMO->getFlags());
10116 void* IP = nullptr;
10117 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10118 E->refineAlignment(MMO);
10119 E->refineRanges(MMO);
10120 return SDValue(E, 0);
10121 }
10122
10123 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
10124 VTList, MemVT, MMO, ExtType);
10125 createOperands(N, Ops);
10126
10127 CSEMap.InsertNode(N, IP);
10128 InsertNode(N);
10129 SDValue V(N, 0);
10130 NewSDValueDbgMsg(V, "Creating new node: ", this);
10131 return V;
10132}
10133
10135 EVT MemVT, SDVTList VTs, SDValue Chain,
10136 SDValue Ptr, SDValue Cmp, SDValue Swp,
10137 MachineMemOperand *MMO) {
10138 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
10140 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
10141
10142 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
10143 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
10144}
10145
10146SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
10147 SDValue Chain, SDValue Ptr, SDValue Val,
10148 MachineMemOperand *MMO) {
10149 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
10150 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
10151 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
10152 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
10153 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
10154 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
10155 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
10156 Opcode == ISD::ATOMIC_LOAD_FMIN ||
10157 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
10158 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
10159 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
10160 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
10161 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
10162 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
10163 Opcode == ISD::ATOMIC_STORE) &&
10164 "Invalid Atomic Op");
10165
10166 EVT VT = Val.getValueType();
10167
10168 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
10169 getVTList(VT, MVT::Other);
10170 SDValue Ops[] = {Chain, Ptr, Val};
10171 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
10172}
10173
10175 EVT MemVT, EVT VT, SDValue Chain,
10176 SDValue Ptr, MachineMemOperand *MMO) {
10177 SDVTList VTs = getVTList(VT, MVT::Other);
10178 SDValue Ops[] = {Chain, Ptr};
10179 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
10180}
10181
10182/// getMergeValues - Create a MERGE_VALUES node from the given operands.
10184 if (Ops.size() == 1)
10185 return Ops[0];
10186
10188 VTs.reserve(Ops.size());
10189 for (const SDValue &Op : Ops)
10190 VTs.push_back(Op.getValueType());
10191 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
10192}
10193
10195 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
10196 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
10198 const AAMDNodes &AAInfo) {
10199 if (Size.hasValue() && !Size.getValue())
10201
10203 MachineMemOperand *MMO =
10204 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
10205
10206 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
10207}
10208
10210 SDVTList VTList,
10211 ArrayRef<SDValue> Ops, EVT MemVT,
10212 MachineMemOperand *MMO) {
10213 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, ArrayRef(MMO));
10214}
10215
10217 SDVTList VTList,
10218 ArrayRef<SDValue> Ops, EVT MemVT,
10220 assert(!MMOs.empty() && "Must have at least one MMO");
10221 assert(
10222 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
10223 Opcode == ISD::PREFETCH ||
10224 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
10225 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
10226 "Opcode is not a memory-accessing opcode!");
10227
10229 if (MMOs.size() == 1) {
10230 MemRefs = MMOs[0];
10231 } else {
10232 // Allocate: [size_t count][MMO*][MMO*]...
10233 size_t AllocSize =
10234 sizeof(size_t) + MMOs.size() * sizeof(MachineMemOperand *);
10235 void *Buffer = Allocator.Allocate(AllocSize, alignof(size_t));
10236 size_t *CountPtr = static_cast<size_t *>(Buffer);
10237 *CountPtr = MMOs.size();
10238 MachineMemOperand **Array =
10239 reinterpret_cast<MachineMemOperand **>(CountPtr + 1);
10240 llvm::copy(MMOs, Array);
10241 MemRefs = Array;
10242 }
10243
10244 // Memoize the node unless it returns a glue result.
10246 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
10248 AddNodeIDNode(ID, Opcode, VTList, Ops);
10249 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
10250 Opcode, dl.getIROrder(), VTList, MemVT, MemRefs));
10251 ID.AddInteger(MemVT.getRawBits());
10252 for (const MachineMemOperand *MMO : MMOs) {
10253 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10254 ID.AddInteger(MMO->getFlags());
10255 }
10256 void *IP = nullptr;
10257 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10258 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMOs);
10259 return SDValue(E, 0);
10260 }
10261
10262 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
10263 VTList, MemVT, MemRefs);
10264 createOperands(N, Ops);
10265 CSEMap.InsertNode(N, IP);
10266 } else {
10267 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
10268 VTList, MemVT, MemRefs);
10269 createOperands(N, Ops);
10270 }
10271 InsertNode(N);
10272 SDValue V(N, 0);
10273 NewSDValueDbgMsg(V, "Creating new node: ", this);
10274 return V;
10275}
10276
10278 SDValue Chain, int FrameIndex) {
10279 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
10280 const auto VTs = getVTList(MVT::Other);
10281 SDValue Ops[2] = {
10282 Chain,
10283 getFrameIndex(FrameIndex,
10284 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
10285 true)};
10286
10288 AddNodeIDNode(ID, Opcode, VTs, Ops);
10289 ID.AddInteger(FrameIndex);
10290 void *IP = nullptr;
10291 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10292 return SDValue(E, 0);
10293
10294 LifetimeSDNode *N =
10295 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
10296 createOperands(N, Ops);
10297 CSEMap.InsertNode(N, IP);
10298 InsertNode(N);
10299 SDValue V(N, 0);
10300 NewSDValueDbgMsg(V, "Creating new node: ", this);
10301 return V;
10302}
10303
10305 uint64_t Guid, uint64_t Index,
10306 uint32_t Attr) {
10307 const unsigned Opcode = ISD::PSEUDO_PROBE;
10308 const auto VTs = getVTList(MVT::Other);
10309 SDValue Ops[] = {Chain};
10311 AddNodeIDNode(ID, Opcode, VTs, Ops);
10312 ID.AddInteger(Guid);
10313 ID.AddInteger(Index);
10314 void *IP = nullptr;
10315 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
10316 return SDValue(E, 0);
10317
10318 auto *N = newSDNode<PseudoProbeSDNode>(
10319 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
10320 createOperands(N, Ops);
10321 CSEMap.InsertNode(N, IP);
10322 InsertNode(N);
10323 SDValue V(N, 0);
10324 NewSDValueDbgMsg(V, "Creating new node: ", this);
10325 return V;
10326}
10327
10328/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
10329/// MachinePointerInfo record from it. This is particularly useful because the
10330/// code generator has many cases where it doesn't bother passing in a
10331/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
10333 SelectionDAG &DAG, SDValue Ptr,
10334 int64_t Offset = 0) {
10335 // If this is FI+Offset, we can model it.
10336 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
10338 FI->getIndex(), Offset);
10339
10340 // If this is (FI+Offset1)+Offset2, we can model it.
10341 if (Ptr.getOpcode() != ISD::ADD ||
10344 return Info;
10345
10346 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
10348 DAG.getMachineFunction(), FI,
10349 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
10350}
10351
10352/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
10353/// MachinePointerInfo record from it. This is particularly useful because the
10354/// code generator has many cases where it doesn't bother passing in a
10355/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
10357 SelectionDAG &DAG, SDValue Ptr,
10358 SDValue OffsetOp) {
10359 // If the 'Offset' value isn't a constant, we can't handle this.
10361 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
10362 if (OffsetOp.isUndef())
10363 return InferPointerInfo(Info, DAG, Ptr);
10364 return Info;
10365}
10366
10368 EVT VT, const SDLoc &dl, SDValue Chain,
10369 SDValue Ptr, SDValue Offset,
10370 MachinePointerInfo PtrInfo, EVT MemVT,
10371 Align Alignment,
10372 MachineMemOperand::Flags MMOFlags,
10373 const AAMDNodes &AAInfo, const MDNode *Ranges) {
10374 assert(Chain.getValueType() == MVT::Other &&
10375 "Invalid chain type");
10376
10377 MMOFlags |= MachineMemOperand::MOLoad;
10378 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10379 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10380 // clients.
10381 if (PtrInfo.V.isNull())
10382 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10383
10384 TypeSize Size = MemVT.getStoreSize();
10386 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10387 Alignment, AAInfo, Ranges);
10388 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
10389}
10390
10392 EVT VT, const SDLoc &dl, SDValue Chain,
10393 SDValue Ptr, SDValue Offset, EVT MemVT,
10394 MachineMemOperand *MMO) {
10395 if (VT == MemVT) {
10396 ExtType = ISD::NON_EXTLOAD;
10397 } else if (ExtType == ISD::NON_EXTLOAD) {
10398 assert(VT == MemVT && "Non-extending load from different memory type!");
10399 } else {
10400 // Extending load.
10401 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
10402 "Should only be an extending load, not truncating!");
10403 assert(VT.isInteger() == MemVT.isInteger() &&
10404 "Cannot convert from FP to Int or Int -> FP!");
10405 assert(VT.isVector() == MemVT.isVector() &&
10406 "Cannot use an ext load to convert to or from a vector!");
10407 assert((!VT.isVector() ||
10409 "Cannot use an ext load to change the number of vector elements!");
10410 }
10411
10412 assert((!MMO->getRanges() ||
10414 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
10415 MemVT.isInteger())) &&
10416 "Range metadata and load type must match!");
10417
10418 bool Indexed = AM != ISD::UNINDEXED;
10419 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10420
10421 SDVTList VTs = Indexed ?
10422 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
10423 SDValue Ops[] = { Chain, Ptr, Offset };
10425 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
10426 ID.AddInteger(MemVT.getRawBits());
10427 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
10428 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
10429 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10430 ID.AddInteger(MMO->getFlags());
10431 void *IP = nullptr;
10432 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10433 E->refineAlignment(MMO);
10434 E->refineRanges(MMO);
10435 return SDValue(E, 0);
10436 }
10437 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10438 ExtType, MemVT, MMO);
10439 createOperands(N, Ops);
10440
10441 CSEMap.InsertNode(N, IP);
10442 InsertNode(N);
10443 SDValue V(N, 0);
10444 NewSDValueDbgMsg(V, "Creating new node: ", this);
10445 return V;
10446}
10447
10449 SDValue Ptr, MachinePointerInfo PtrInfo,
10450 MaybeAlign Alignment,
10451 MachineMemOperand::Flags MMOFlags,
10452 const AAMDNodes &AAInfo, const MDNode *Ranges) {
10454 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10455 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
10456}
10457
10459 SDValue Ptr, MachineMemOperand *MMO) {
10461 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10462 VT, MMO);
10463}
10464
10466 EVT VT, SDValue Chain, SDValue Ptr,
10467 MachinePointerInfo PtrInfo, EVT MemVT,
10468 MaybeAlign Alignment,
10469 MachineMemOperand::Flags MMOFlags,
10470 const AAMDNodes &AAInfo) {
10472 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
10473 MemVT, Alignment, MMOFlags, AAInfo);
10474}
10475
10477 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
10478 MachineMemOperand *MMO) {
10480 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
10481 MemVT, MMO);
10482}
10483
10487 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
10488 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10489 // Don't propagate the invariant or dereferenceable flags.
10490 auto MMOFlags =
10491 LD->getMemOperand()->getFlags() &
10493 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10494 LD->getChain(), Base, Offset, LD->getPointerInfo(),
10495 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
10496}
10497
10499 SDValue Ptr, MachinePointerInfo PtrInfo,
10500 Align Alignment,
10501 MachineMemOperand::Flags MMOFlags,
10502 const AAMDNodes &AAInfo) {
10503 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10504
10505 MMOFlags |= MachineMemOperand::MOStore;
10506 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10507
10508 if (PtrInfo.V.isNull())
10509 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10510
10513 MachineMemOperand *MMO =
10514 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
10515 return getStore(Chain, dl, Val, Ptr, MMO);
10516}
10517
10519 SDValue Ptr, MachineMemOperand *MMO) {
10521 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
10523}
10524
10526 SDValue Ptr, SDValue Offset, EVT SVT,
10528 bool IsTruncating) {
10529 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10530 EVT VT = Val.getValueType();
10531 if (VT == SVT) {
10532 IsTruncating = false;
10533 } else if (!IsTruncating) {
10534 assert(VT == SVT && "No-truncating store from different memory type!");
10535 } else {
10537 "Should only be a truncating store, not extending!");
10538 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10539 assert(VT.isVector() == SVT.isVector() &&
10540 "Cannot use trunc store to convert to or from a vector!");
10541 assert((!VT.isVector() ||
10543 "Cannot use trunc store to change the number of vector elements!");
10544 }
10545
10546 bool Indexed = AM != ISD::UNINDEXED;
10547 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
10548 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10549 : getVTList(MVT::Other);
10550 SDValue Ops[] = {Chain, Val, Ptr, Offset};
10553 ID.AddInteger(SVT.getRawBits());
10554 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
10555 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
10556 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10557 ID.AddInteger(MMO->getFlags());
10558 void *IP = nullptr;
10559 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10560 cast<StoreSDNode>(E)->refineAlignment(MMO);
10561 return SDValue(E, 0);
10562 }
10563 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10564 IsTruncating, SVT, MMO);
10565 createOperands(N, Ops);
10566
10567 CSEMap.InsertNode(N, IP);
10568 InsertNode(N);
10569 SDValue V(N, 0);
10570 NewSDValueDbgMsg(V, "Creating new node: ", this);
10571 return V;
10572}
10573
10575 SDValue Ptr, MachinePointerInfo PtrInfo,
10576 EVT SVT, Align Alignment,
10577 MachineMemOperand::Flags MMOFlags,
10578 const AAMDNodes &AAInfo) {
10579 assert(Chain.getValueType() == MVT::Other &&
10580 "Invalid chain type");
10581
10582 MMOFlags |= MachineMemOperand::MOStore;
10583 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10584
10585 if (PtrInfo.V.isNull())
10586 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10587
10589 MachineMemOperand *MMO = MF.getMachineMemOperand(
10590 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10591 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
10592}
10593
10595 SDValue Ptr, EVT SVT,
10596 MachineMemOperand *MMO) {
10598 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
10599}
10600
10604 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
10605 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
10606 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10607 ST->getMemoryVT(), ST->getMemOperand(), AM,
10608 ST->isTruncatingStore());
10609}
10610
10612 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
10613 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
10614 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
10615 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
10616 const MDNode *Ranges, bool IsExpanding) {
10617 MMOFlags |= MachineMemOperand::MOLoad;
10618 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10619 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10620 // clients.
10621 if (PtrInfo.V.isNull())
10622 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10623
10624 TypeSize Size = MemVT.getStoreSize();
10626 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10627 Alignment, AAInfo, Ranges);
10628 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10629 MMO, IsExpanding);
10630}
10631
10633 ISD::LoadExtType ExtType, EVT VT,
10634 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10635 SDValue Offset, SDValue Mask, SDValue EVL,
10636 EVT MemVT, MachineMemOperand *MMO,
10637 bool IsExpanding) {
10638 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10639 assert(Mask.getValueType().getVectorElementCount() ==
10640 VT.getVectorElementCount() &&
10641 "Vector width mismatch between mask and data");
10642
10643 bool Indexed = AM != ISD::UNINDEXED;
10644 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10645
10646 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10647 : getVTList(VT, MVT::Other);
10648 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10650 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10651 ID.AddInteger(MemVT.getRawBits());
10652 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10653 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10654 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10655 ID.AddInteger(MMO->getFlags());
10656 void *IP = nullptr;
10657 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10658 E->refineAlignment(MMO);
10659 E->refineRanges(MMO);
10660 return SDValue(E, 0);
10661 }
10662 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10663 ExtType, IsExpanding, MemVT, MMO);
10664 createOperands(N, Ops);
10665
10666 CSEMap.InsertNode(N, IP);
10667 InsertNode(N);
10668 SDValue V(N, 0);
10669 NewSDValueDbgMsg(V, "Creating new node: ", this);
10670 return V;
10671}
10672
10674 SDValue Ptr, SDValue Mask, SDValue EVL,
10675 MachinePointerInfo PtrInfo,
10676 MaybeAlign Alignment,
10677 MachineMemOperand::Flags MMOFlags,
10678 const AAMDNodes &AAInfo, const MDNode *Ranges,
10679 bool IsExpanding) {
10681 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10682 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10683 IsExpanding);
10684}
10685
10687 SDValue Ptr, SDValue Mask, SDValue EVL,
10688 MachineMemOperand *MMO, bool IsExpanding) {
10690 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10691 Mask, EVL, VT, MMO, IsExpanding);
10692}
10693
10695 EVT VT, SDValue Chain, SDValue Ptr,
10696 SDValue Mask, SDValue EVL,
10697 MachinePointerInfo PtrInfo, EVT MemVT,
10698 MaybeAlign Alignment,
10699 MachineMemOperand::Flags MMOFlags,
10700 const AAMDNodes &AAInfo, bool IsExpanding) {
10702 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10703 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10704 IsExpanding);
10705}
10706
10708 EVT VT, SDValue Chain, SDValue Ptr,
10709 SDValue Mask, SDValue EVL, EVT MemVT,
10710 MachineMemOperand *MMO, bool IsExpanding) {
10712 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10713 EVL, MemVT, MMO, IsExpanding);
10714}
10715
10719 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10720 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10721 // Don't propagate the invariant or dereferenceable flags.
10722 auto MMOFlags =
10723 LD->getMemOperand()->getFlags() &
10725 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10726 LD->getChain(), Base, Offset, LD->getMask(),
10727 LD->getVectorLength(), LD->getPointerInfo(),
10728 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10729 nullptr, LD->isExpandingLoad());
10730}
10731
10733 SDValue Ptr, SDValue Offset, SDValue Mask,
10734 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10735 ISD::MemIndexedMode AM, bool IsTruncating,
10736 bool IsCompressing) {
10737 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10738 assert(Mask.getValueType().getVectorElementCount() ==
10740 "Vector width mismatch between mask and data");
10741
10742 bool Indexed = AM != ISD::UNINDEXED;
10743 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10744 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10745 : getVTList(MVT::Other);
10746 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10748 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10749 ID.AddInteger(MemVT.getRawBits());
10750 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10751 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10752 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10753 ID.AddInteger(MMO->getFlags());
10754 void *IP = nullptr;
10755 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10756 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10757 return SDValue(E, 0);
10758 }
10759 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10760 IsTruncating, IsCompressing, MemVT, MMO);
10761 createOperands(N, Ops);
10762
10763 CSEMap.InsertNode(N, IP);
10764 InsertNode(N);
10765 SDValue V(N, 0);
10766 NewSDValueDbgMsg(V, "Creating new node: ", this);
10767 return V;
10768}
10769
10771 SDValue Val, SDValue Ptr, SDValue Mask,
10772 SDValue EVL, MachinePointerInfo PtrInfo,
10773 EVT SVT, Align Alignment,
10774 MachineMemOperand::Flags MMOFlags,
10775 const AAMDNodes &AAInfo,
10776 bool IsCompressing) {
10777 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10778
10779 MMOFlags |= MachineMemOperand::MOStore;
10780 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10781
10782 if (PtrInfo.V.isNull())
10783 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10784
10786 MachineMemOperand *MMO = MF.getMachineMemOperand(
10787 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10788 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10789 IsCompressing);
10790}
10791
10793 SDValue Val, SDValue Ptr, SDValue Mask,
10794 SDValue EVL, EVT SVT,
10795 MachineMemOperand *MMO,
10796 bool IsCompressing) {
10797 EVT VT = Val.getValueType();
10798
10799 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10800 if (VT == SVT)
10801 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10802 EVL, VT, MMO, ISD::UNINDEXED,
10803 /*IsTruncating*/ false, IsCompressing);
10804
10806 "Should only be a truncating store, not extending!");
10807 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10808 assert(VT.isVector() == SVT.isVector() &&
10809 "Cannot use trunc store to convert to or from a vector!");
10810 assert((!VT.isVector() ||
10812 "Cannot use trunc store to change the number of vector elements!");
10813
10814 SDVTList VTs = getVTList(MVT::Other);
10816 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10818 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10819 ID.AddInteger(SVT.getRawBits());
10820 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10821 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10822 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10823 ID.AddInteger(MMO->getFlags());
10824 void *IP = nullptr;
10825 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10826 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10827 return SDValue(E, 0);
10828 }
10829 auto *N =
10830 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10831 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10832 createOperands(N, Ops);
10833
10834 CSEMap.InsertNode(N, IP);
10835 InsertNode(N);
10836 SDValue V(N, 0);
10837 NewSDValueDbgMsg(V, "Creating new node: ", this);
10838 return V;
10839}
10840
10844 auto *ST = cast<VPStoreSDNode>(OrigStore);
10845 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10846 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10847 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10848 Offset, ST->getMask(), ST->getVectorLength()};
10850 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10851 ID.AddInteger(ST->getMemoryVT().getRawBits());
10852 ID.AddInteger(ST->getRawSubclassData());
10853 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10854 ID.AddInteger(ST->getMemOperand()->getFlags());
10855 void *IP = nullptr;
10856 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10857 return SDValue(E, 0);
10858
10859 auto *N = newSDNode<VPStoreSDNode>(
10860 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10861 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10862 createOperands(N, Ops);
10863
10864 CSEMap.InsertNode(N, IP);
10865 InsertNode(N);
10866 SDValue V(N, 0);
10867 NewSDValueDbgMsg(V, "Creating new node: ", this);
10868 return V;
10869}
10870
10872 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10873 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10874 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10875 bool Indexed = AM != ISD::UNINDEXED;
10876 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10877
10878 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10879 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10880 : getVTList(VT, MVT::Other);
10882 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10883 ID.AddInteger(VT.getRawBits());
10884 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10885 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10886 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10887
10888 void *IP = nullptr;
10889 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10890 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10891 return SDValue(E, 0);
10892 }
10893
10894 auto *N =
10895 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10896 ExtType, IsExpanding, MemVT, MMO);
10897 createOperands(N, Ops);
10898 CSEMap.InsertNode(N, IP);
10899 InsertNode(N);
10900 SDValue V(N, 0);
10901 NewSDValueDbgMsg(V, "Creating new node: ", this);
10902 return V;
10903}
10904
10906 SDValue Ptr, SDValue Stride,
10907 SDValue Mask, SDValue EVL,
10908 MachineMemOperand *MMO,
10909 bool IsExpanding) {
10911 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
10912 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10913}
10914
10916 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10917 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10918 MachineMemOperand *MMO, bool IsExpanding) {
10920 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10921 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10922}
10923
10925 SDValue Val, SDValue Ptr,
10926 SDValue Offset, SDValue Stride,
10927 SDValue Mask, SDValue EVL, EVT MemVT,
10928 MachineMemOperand *MMO,
10930 bool IsTruncating, bool IsCompressing) {
10931 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10932 bool Indexed = AM != ISD::UNINDEXED;
10933 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10934 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10935 : getVTList(MVT::Other);
10936 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10938 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10939 ID.AddInteger(MemVT.getRawBits());
10940 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10941 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10942 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10943 void *IP = nullptr;
10944 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10945 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10946 return SDValue(E, 0);
10947 }
10948 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10949 VTs, AM, IsTruncating,
10950 IsCompressing, MemVT, MMO);
10951 createOperands(N, Ops);
10952
10953 CSEMap.InsertNode(N, IP);
10954 InsertNode(N);
10955 SDValue V(N, 0);
10956 NewSDValueDbgMsg(V, "Creating new node: ", this);
10957 return V;
10958}
10959
10961 SDValue Val, SDValue Ptr,
10962 SDValue Stride, SDValue Mask,
10963 SDValue EVL, EVT SVT,
10964 MachineMemOperand *MMO,
10965 bool IsCompressing) {
10966 EVT VT = Val.getValueType();
10967
10968 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10969 if (VT == SVT)
10970 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10971 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10972 /*IsTruncating*/ false, IsCompressing);
10973
10975 "Should only be a truncating store, not extending!");
10976 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10977 assert(VT.isVector() == SVT.isVector() &&
10978 "Cannot use trunc store to convert to or from a vector!");
10979 assert((!VT.isVector() ||
10981 "Cannot use trunc store to change the number of vector elements!");
10982
10983 SDVTList VTs = getVTList(MVT::Other);
10985 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10987 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10988 ID.AddInteger(SVT.getRawBits());
10989 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10990 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10991 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10992 void *IP = nullptr;
10993 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10994 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10995 return SDValue(E, 0);
10996 }
10997 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10998 VTs, ISD::UNINDEXED, true,
10999 IsCompressing, SVT, MMO);
11000 createOperands(N, Ops);
11001
11002 CSEMap.InsertNode(N, IP);
11003 InsertNode(N);
11004 SDValue V(N, 0);
11005 NewSDValueDbgMsg(V, "Creating new node: ", this);
11006 return V;
11007}
11008
11011 ISD::MemIndexType IndexType) {
11012 assert(Ops.size() == 6 && "Incompatible number of operands");
11013
11015 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
11016 ID.AddInteger(VT.getRawBits());
11017 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
11018 dl.getIROrder(), VTs, VT, MMO, IndexType));
11019 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11020 ID.AddInteger(MMO->getFlags());
11021 void *IP = nullptr;
11022 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11023 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
11024 return SDValue(E, 0);
11025 }
11026
11027 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
11028 VT, MMO, IndexType);
11029 createOperands(N, Ops);
11030
11031 assert(N->getMask().getValueType().getVectorElementCount() ==
11032 N->getValueType(0).getVectorElementCount() &&
11033 "Vector width mismatch between mask and data");
11034 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11035 N->getValueType(0).getVectorElementCount().isScalable() &&
11036 "Scalable flags of index and data do not match");
11038 N->getIndex().getValueType().getVectorElementCount(),
11039 N->getValueType(0).getVectorElementCount()) &&
11040 "Vector width mismatch between index and data");
11041 assert(isa<ConstantSDNode>(N->getScale()) &&
11042 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11043 "Scale should be a constant power of 2");
11044
11045 CSEMap.InsertNode(N, IP);
11046 InsertNode(N);
11047 SDValue V(N, 0);
11048 NewSDValueDbgMsg(V, "Creating new node: ", this);
11049 return V;
11050}
11051
11054 MachineMemOperand *MMO,
11055 ISD::MemIndexType IndexType) {
11056 assert(Ops.size() == 7 && "Incompatible number of operands");
11057
11059 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
11060 ID.AddInteger(VT.getRawBits());
11061 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
11062 dl.getIROrder(), VTs, VT, MMO, IndexType));
11063 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11064 ID.AddInteger(MMO->getFlags());
11065 void *IP = nullptr;
11066 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11067 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
11068 return SDValue(E, 0);
11069 }
11070 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
11071 VT, MMO, IndexType);
11072 createOperands(N, Ops);
11073
11074 assert(N->getMask().getValueType().getVectorElementCount() ==
11075 N->getValue().getValueType().getVectorElementCount() &&
11076 "Vector width mismatch between mask and data");
11077 assert(
11078 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11079 N->getValue().getValueType().getVectorElementCount().isScalable() &&
11080 "Scalable flags of index and data do not match");
11082 N->getIndex().getValueType().getVectorElementCount(),
11083 N->getValue().getValueType().getVectorElementCount()) &&
11084 "Vector width mismatch between index and data");
11085 assert(isa<ConstantSDNode>(N->getScale()) &&
11086 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11087 "Scale should be a constant power of 2");
11088
11089 CSEMap.InsertNode(N, IP);
11090 InsertNode(N);
11091 SDValue V(N, 0);
11092 NewSDValueDbgMsg(V, "Creating new node: ", this);
11093 return V;
11094}
11095
11098 SDValue PassThru, EVT MemVT,
11099 MachineMemOperand *MMO,
11101 ISD::LoadExtType ExtTy, bool isExpanding) {
11102 bool Indexed = AM != ISD::UNINDEXED;
11103 assert((Indexed || Offset.isUndef()) &&
11104 "Unindexed masked load with an offset!");
11105 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
11106 : getVTList(VT, MVT::Other);
11107 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
11110 ID.AddInteger(MemVT.getRawBits());
11111 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
11112 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
11113 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11114 ID.AddInteger(MMO->getFlags());
11115 void *IP = nullptr;
11116 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11117 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
11118 return SDValue(E, 0);
11119 }
11120 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
11121 AM, ExtTy, isExpanding, MemVT, MMO);
11122 createOperands(N, Ops);
11123
11124 CSEMap.InsertNode(N, IP);
11125 InsertNode(N);
11126 SDValue V(N, 0);
11127 NewSDValueDbgMsg(V, "Creating new node: ", this);
11128 return V;
11129}
11130
11135 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
11136 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
11137 Offset, LD->getMask(), LD->getPassThru(),
11138 LD->getMemoryVT(), LD->getMemOperand(), AM,
11139 LD->getExtensionType(), LD->isExpandingLoad());
11140}
11141
11144 SDValue Mask, EVT MemVT,
11145 MachineMemOperand *MMO,
11146 ISD::MemIndexedMode AM, bool IsTruncating,
11147 bool IsCompressing) {
11148 assert(Chain.getValueType() == MVT::Other &&
11149 "Invalid chain type");
11150 bool Indexed = AM != ISD::UNINDEXED;
11151 assert((Indexed || Offset.isUndef()) &&
11152 "Unindexed masked store with an offset!");
11153 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
11154 : getVTList(MVT::Other);
11155 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
11158 ID.AddInteger(MemVT.getRawBits());
11159 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
11160 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
11161 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11162 ID.AddInteger(MMO->getFlags());
11163 void *IP = nullptr;
11164 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11165 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
11166 return SDValue(E, 0);
11167 }
11168 auto *N =
11169 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
11170 IsTruncating, IsCompressing, MemVT, MMO);
11171 createOperands(N, Ops);
11172
11173 CSEMap.InsertNode(N, IP);
11174 InsertNode(N);
11175 SDValue V(N, 0);
11176 NewSDValueDbgMsg(V, "Creating new node: ", this);
11177 return V;
11178}
11179
11184 assert(ST->getOffset().isUndef() &&
11185 "Masked store is already a indexed store!");
11186 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
11187 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
11188 AM, ST->isTruncatingStore(), ST->isCompressingStore());
11189}
11190
11193 MachineMemOperand *MMO,
11194 ISD::MemIndexType IndexType,
11195 ISD::LoadExtType ExtTy) {
11196 assert(Ops.size() == 6 && "Incompatible number of operands");
11197
11200 ID.AddInteger(MemVT.getRawBits());
11201 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
11202 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
11203 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11204 ID.AddInteger(MMO->getFlags());
11205 void *IP = nullptr;
11206 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11207 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
11208 return SDValue(E, 0);
11209 }
11210
11211 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
11212 VTs, MemVT, MMO, IndexType, ExtTy);
11213 createOperands(N, Ops);
11214
11215 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
11216 "Incompatible type of the PassThru value in MaskedGatherSDNode");
11217 assert(N->getMask().getValueType().getVectorElementCount() ==
11218 N->getValueType(0).getVectorElementCount() &&
11219 "Vector width mismatch between mask and data");
11220 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11221 N->getValueType(0).getVectorElementCount().isScalable() &&
11222 "Scalable flags of index and data do not match");
11224 N->getIndex().getValueType().getVectorElementCount(),
11225 N->getValueType(0).getVectorElementCount()) &&
11226 "Vector width mismatch between index and data");
11227 assert(isa<ConstantSDNode>(N->getScale()) &&
11228 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11229 "Scale should be a constant power of 2");
11230
11231 CSEMap.InsertNode(N, IP);
11232 InsertNode(N);
11233 SDValue V(N, 0);
11234 NewSDValueDbgMsg(V, "Creating new node: ", this);
11235 return V;
11236}
11237
11240 MachineMemOperand *MMO,
11241 ISD::MemIndexType IndexType,
11242 bool IsTrunc) {
11243 assert(Ops.size() == 6 && "Incompatible number of operands");
11244
11247 ID.AddInteger(MemVT.getRawBits());
11248 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
11249 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
11250 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11251 ID.AddInteger(MMO->getFlags());
11252 void *IP = nullptr;
11253 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11254 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
11255 return SDValue(E, 0);
11256 }
11257
11258 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
11259 VTs, MemVT, MMO, IndexType, IsTrunc);
11260 createOperands(N, Ops);
11261
11262 assert(N->getMask().getValueType().getVectorElementCount() ==
11263 N->getValue().getValueType().getVectorElementCount() &&
11264 "Vector width mismatch between mask and data");
11265 assert(
11266 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11267 N->getValue().getValueType().getVectorElementCount().isScalable() &&
11268 "Scalable flags of index and data do not match");
11270 N->getIndex().getValueType().getVectorElementCount(),
11271 N->getValue().getValueType().getVectorElementCount()) &&
11272 "Vector width mismatch between index and data");
11273 assert(isa<ConstantSDNode>(N->getScale()) &&
11274 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11275 "Scale should be a constant power of 2");
11276
11277 CSEMap.InsertNode(N, IP);
11278 InsertNode(N);
11279 SDValue V(N, 0);
11280 NewSDValueDbgMsg(V, "Creating new node: ", this);
11281 return V;
11282}
11283
11285 const SDLoc &dl, ArrayRef<SDValue> Ops,
11286 MachineMemOperand *MMO,
11287 ISD::MemIndexType IndexType) {
11288 assert(Ops.size() == 7 && "Incompatible number of operands");
11289
11292 ID.AddInteger(MemVT.getRawBits());
11293 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
11294 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
11295 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11296 ID.AddInteger(MMO->getFlags());
11297 void *IP = nullptr;
11298 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11299 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
11300 return SDValue(E, 0);
11301 }
11302
11303 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
11304 VTs, MemVT, MMO, IndexType);
11305 createOperands(N, Ops);
11306
11307 assert(N->getMask().getValueType().getVectorElementCount() ==
11308 N->getIndex().getValueType().getVectorElementCount() &&
11309 "Vector width mismatch between mask and data");
11310 assert(isa<ConstantSDNode>(N->getScale()) &&
11311 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11312 "Scale should be a constant power of 2");
11313 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
11314
11315 CSEMap.InsertNode(N, IP);
11316 InsertNode(N);
11317 SDValue V(N, 0);
11318 NewSDValueDbgMsg(V, "Creating new node: ", this);
11319 return V;
11320}
11321
11323 SDValue Ptr, SDValue Mask, SDValue EVL,
11324 MachineMemOperand *MMO) {
11325 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
11326 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
11328 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
11329 ID.AddInteger(VT.getRawBits());
11330 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
11331 VTs, VT, MMO));
11332 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11333 ID.AddInteger(MMO->getFlags());
11334 void *IP = nullptr;
11335 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11336 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
11337 return SDValue(E, 0);
11338 }
11339 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
11340 VT, MMO);
11341 createOperands(N, Ops);
11342
11343 CSEMap.InsertNode(N, IP);
11344 InsertNode(N);
11345 SDValue V(N, 0);
11346 NewSDValueDbgMsg(V, "Creating new node: ", this);
11347 return V;
11348}
11349
11351 EVT MemVT, MachineMemOperand *MMO) {
11352 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
11353 SDVTList VTs = getVTList(MVT::Other);
11354 SDValue Ops[] = {Chain, Ptr};
11357 ID.AddInteger(MemVT.getRawBits());
11358 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
11359 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
11360 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11361 ID.AddInteger(MMO->getFlags());
11362 void *IP = nullptr;
11363 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
11364 return SDValue(E, 0);
11365
11366 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
11367 dl.getDebugLoc(), VTs, MemVT, MMO);
11368 createOperands(N, Ops);
11369
11370 CSEMap.InsertNode(N, IP);
11371 InsertNode(N);
11372 SDValue V(N, 0);
11373 NewSDValueDbgMsg(V, "Creating new node: ", this);
11374 return V;
11375}
11376
11378 EVT MemVT, MachineMemOperand *MMO) {
11379 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
11380 SDVTList VTs = getVTList(MVT::Other);
11381 SDValue Ops[] = {Chain, Ptr};
11384 ID.AddInteger(MemVT.getRawBits());
11385 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
11386 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
11387 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11388 ID.AddInteger(MMO->getFlags());
11389 void *IP = nullptr;
11390 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
11391 return SDValue(E, 0);
11392
11393 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
11394 dl.getDebugLoc(), VTs, MemVT, MMO);
11395 createOperands(N, Ops);
11396
11397 CSEMap.InsertNode(N, IP);
11398 InsertNode(N);
11399 SDValue V(N, 0);
11400 NewSDValueDbgMsg(V, "Creating new node: ", this);
11401 return V;
11402}
11403
11405 // select undef, T, F --> T (if T is a constant), otherwise F
11406 // select, ?, undef, F --> F
11407 // select, ?, T, undef --> T
11408 if (Cond.isUndef())
11409 return isConstantValueOfAnyType(T) ? T : F;
11410 if (T.isUndef())
11412 if (F.isUndef())
11414
11415 // select true, T, F --> T
11416 // select false, T, F --> F
11417 if (auto C = isBoolConstant(Cond))
11418 return *C ? T : F;
11419
11420 // select ?, T, T --> T
11421 if (T == F)
11422 return T;
11423
11424 return SDValue();
11425}
11426
11428 // shift undef, Y --> 0 (can always assume that the undef value is 0)
11429 if (X.isUndef())
11430 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
11431 // shift X, undef --> undef (because it may shift by the bitwidth)
11432 if (Y.isUndef())
11433 return getUNDEF(X.getValueType());
11434
11435 // shift 0, Y --> 0
11436 // shift X, 0 --> X
11438 return X;
11439
11440 // shift X, C >= bitwidth(X) --> undef
11441 // All vector elements must be too big (or undef) to avoid partial undefs.
11442 auto isShiftTooBig = [X](ConstantSDNode *Val) {
11443 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
11444 };
11445 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
11446 return getUNDEF(X.getValueType());
11447
11448 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
11449 if (X.getValueType().getScalarType() == MVT::i1)
11450 return X;
11451
11452 return SDValue();
11453}
11454
11456 SDNodeFlags Flags) {
11457 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
11458 // (an undef operand can be chosen to be Nan/Inf), then the result of this
11459 // operation is poison. That result can be relaxed to undef.
11460 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
11461 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
11462 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
11463 (YC && YC->getValueAPF().isNaN());
11464 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
11465 (YC && YC->getValueAPF().isInfinity());
11466
11467 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
11468 return getUNDEF(X.getValueType());
11469
11470 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
11471 return getUNDEF(X.getValueType());
11472
11473 if (!YC)
11474 return SDValue();
11475
11476 // X + -0.0 --> X
11477 if (Opcode == ISD::FADD)
11478 if (YC->getValueAPF().isNegZero())
11479 return X;
11480
11481 // X - +0.0 --> X
11482 if (Opcode == ISD::FSUB)
11483 if (YC->getValueAPF().isPosZero())
11484 return X;
11485
11486 // X * 1.0 --> X
11487 // X / 1.0 --> X
11488 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
11489 if (YC->getValueAPF().isExactlyValue(1.0))
11490 return X;
11491
11492 // X * 0.0 --> 0.0
11493 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
11494 if (YC->getValueAPF().isZero())
11495 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
11496
11497 return SDValue();
11498}
11499
11501 SDValue Ptr, SDValue SV, unsigned Align) {
11502 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
11503 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
11504}
11505
11506SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11508 switch (Ops.size()) {
11509 case 0: return getNode(Opcode, DL, VT);
11510 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
11511 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
11512 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
11513 default: break;
11514 }
11515
11516 // Copy from an SDUse array into an SDValue array for use with
11517 // the regular getNode logic.
11519 return getNode(Opcode, DL, VT, NewOps);
11520}
11521
11522SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11524 SDNodeFlags Flags;
11525 if (Inserter)
11526 Flags = Inserter->getFlags();
11527 return getNode(Opcode, DL, VT, Ops, Flags);
11528}
11529
11530SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11531 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11532 unsigned NumOps = Ops.size();
11533 switch (NumOps) {
11534 case 0: return getNode(Opcode, DL, VT);
11535 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
11536 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
11537 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
11538 default: break;
11539 }
11540
11541#ifndef NDEBUG
11542 for (const auto &Op : Ops)
11543 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11544 "Operand is DELETED_NODE!");
11545#endif
11546
11547 switch (Opcode) {
11548 default: break;
11549 case ISD::BUILD_VECTOR:
11550 // Attempt to simplify BUILD_VECTOR.
11551 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
11552 return V;
11553 break;
11555 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
11556 return V;
11557 break;
11558 case ISD::SELECT_CC:
11559 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
11560 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
11561 "LHS and RHS of condition must have same type!");
11562 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11563 "True and False arms of SelectCC must have same type!");
11564 assert(Ops[2].getValueType() == VT &&
11565 "select_cc node must be of same type as true and false value!");
11566 assert((!Ops[0].getValueType().isVector() ||
11567 Ops[0].getValueType().getVectorElementCount() ==
11568 VT.getVectorElementCount()) &&
11569 "Expected select_cc with vector result to have the same sized "
11570 "comparison type!");
11571 break;
11572 case ISD::BR_CC:
11573 assert(NumOps == 5 && "BR_CC takes 5 operands!");
11574 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11575 "LHS/RHS of comparison should match types!");
11576 break;
11577 case ISD::VP_ADD:
11578 case ISD::VP_SUB:
11579 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
11580 if (VT.getScalarType() == MVT::i1)
11581 Opcode = ISD::VP_XOR;
11582 break;
11583 case ISD::VP_MUL:
11584 // If it is VP_MUL mask operation then turn it to VP_AND
11585 if (VT.getScalarType() == MVT::i1)
11586 Opcode = ISD::VP_AND;
11587 break;
11588 case ISD::VP_REDUCE_MUL:
11589 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
11590 if (VT == MVT::i1)
11591 Opcode = ISD::VP_REDUCE_AND;
11592 break;
11593 case ISD::VP_REDUCE_ADD:
11594 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
11595 if (VT == MVT::i1)
11596 Opcode = ISD::VP_REDUCE_XOR;
11597 break;
11598 case ISD::VP_REDUCE_SMAX:
11599 case ISD::VP_REDUCE_UMIN:
11600 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
11601 // VP_REDUCE_AND.
11602 if (VT == MVT::i1)
11603 Opcode = ISD::VP_REDUCE_AND;
11604 break;
11605 case ISD::VP_REDUCE_SMIN:
11606 case ISD::VP_REDUCE_UMAX:
11607 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
11608 // VP_REDUCE_OR.
11609 if (VT == MVT::i1)
11610 Opcode = ISD::VP_REDUCE_OR;
11611 break;
11612 }
11613
11614 // Memoize nodes.
11615 SDNode *N;
11616 SDVTList VTs = getVTList(VT);
11617
11618 if (VT != MVT::Glue) {
11620 AddNodeIDNode(ID, Opcode, VTs, Ops);
11621 void *IP = nullptr;
11622
11623 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11624 E->intersectFlagsWith(Flags);
11625 return SDValue(E, 0);
11626 }
11627
11628 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11629 createOperands(N, Ops);
11630
11631 CSEMap.InsertNode(N, IP);
11632 } else {
11633 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11634 createOperands(N, Ops);
11635 }
11636
11637 N->setFlags(Flags);
11638 InsertNode(N);
11639 SDValue V(N, 0);
11640 NewSDValueDbgMsg(V, "Creating new node: ", this);
11641 return V;
11642}
11643
11644SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11645 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11646 SDNodeFlags Flags;
11647 if (Inserter)
11648 Flags = Inserter->getFlags();
11649 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11650}
11651
11652SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11654 const SDNodeFlags Flags) {
11655 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11656}
11657
11658SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11660 SDNodeFlags Flags;
11661 if (Inserter)
11662 Flags = Inserter->getFlags();
11663 return getNode(Opcode, DL, VTList, Ops, Flags);
11664}
11665
11666SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11667 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11668 if (VTList.NumVTs == 1)
11669 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11670
11671#ifndef NDEBUG
11672 for (const auto &Op : Ops)
11673 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11674 "Operand is DELETED_NODE!");
11675#endif
11676
11677 switch (Opcode) {
11678 case ISD::SADDO:
11679 case ISD::UADDO:
11680 case ISD::SSUBO:
11681 case ISD::USUBO: {
11682 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11683 "Invalid add/sub overflow op!");
11684 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11685 Ops[0].getValueType() == Ops[1].getValueType() &&
11686 Ops[0].getValueType() == VTList.VTs[0] &&
11687 "Binary operator types must match!");
11688 SDValue N1 = Ops[0], N2 = Ops[1];
11689 canonicalizeCommutativeBinop(Opcode, N1, N2);
11690
11691 // (X +- 0) -> X with zero-overflow.
11692 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11693 /*AllowTruncation*/ true);
11694 if (N2CV && N2CV->isZero()) {
11695 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11696 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11697 }
11698
11699 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11700 VTList.VTs[1].getScalarType() == MVT::i1) {
11701 SDValue F1 = getFreeze(N1);
11702 SDValue F2 = getFreeze(N2);
11703 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11704 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11705 return getNode(ISD::MERGE_VALUES, DL, VTList,
11706 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11707 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11708 Flags);
11709 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11710 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11711 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11712 return getNode(ISD::MERGE_VALUES, DL, VTList,
11713 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11714 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11715 Flags);
11716 }
11717 }
11718 break;
11719 }
11720 case ISD::SADDO_CARRY:
11721 case ISD::UADDO_CARRY:
11722 case ISD::SSUBO_CARRY:
11723 case ISD::USUBO_CARRY:
11724 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11725 "Invalid add/sub overflow op!");
11726 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11727 Ops[0].getValueType() == Ops[1].getValueType() &&
11728 Ops[0].getValueType() == VTList.VTs[0] &&
11729 Ops[2].getValueType() == VTList.VTs[1] &&
11730 "Binary operator types must match!");
11731 break;
11732 case ISD::SMUL_LOHI:
11733 case ISD::UMUL_LOHI: {
11734 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11735 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11736 VTList.VTs[0] == Ops[0].getValueType() &&
11737 VTList.VTs[0] == Ops[1].getValueType() &&
11738 "Binary operator types must match!");
11739 // Constant fold.
11742 if (LHS && RHS) {
11743 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11744 unsigned OutWidth = Width * 2;
11745 APInt Val = LHS->getAPIntValue();
11746 APInt Mul = RHS->getAPIntValue();
11747 if (Opcode == ISD::SMUL_LOHI) {
11748 Val = Val.sext(OutWidth);
11749 Mul = Mul.sext(OutWidth);
11750 } else {
11751 Val = Val.zext(OutWidth);
11752 Mul = Mul.zext(OutWidth);
11753 }
11754 Val *= Mul;
11755
11756 SDValue Hi =
11757 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11758 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11759 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11760 }
11761 break;
11762 }
11763 case ISD::FFREXP: {
11764 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11765 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11766 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11767
11769 int FrexpExp;
11770 APFloat FrexpMant =
11771 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11772 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11773 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11774 DL, VTList.VTs[1]);
11775 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11776 }
11777
11778 break;
11779 }
11781 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11782 "Invalid STRICT_FP_EXTEND!");
11783 assert(VTList.VTs[0].isFloatingPoint() &&
11784 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11785 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11786 "STRICT_FP_EXTEND result type should be vector iff the operand "
11787 "type is vector!");
11788 assert((!VTList.VTs[0].isVector() ||
11789 VTList.VTs[0].getVectorElementCount() ==
11790 Ops[1].getValueType().getVectorElementCount()) &&
11791 "Vector element count mismatch!");
11792 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11793 "Invalid fpext node, dst <= src!");
11794 break;
11796 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11797 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11798 "STRICT_FP_ROUND result type should be vector iff the operand "
11799 "type is vector!");
11800 assert((!VTList.VTs[0].isVector() ||
11801 VTList.VTs[0].getVectorElementCount() ==
11802 Ops[1].getValueType().getVectorElementCount()) &&
11803 "Vector element count mismatch!");
11804 assert(VTList.VTs[0].isFloatingPoint() &&
11805 Ops[1].getValueType().isFloatingPoint() &&
11806 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11807 Ops[2].getOpcode() == ISD::TargetConstant &&
11808 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11809 "Invalid STRICT_FP_ROUND!");
11810 break;
11811 }
11812
11813 // Memoize the node unless it returns a glue result.
11814 SDNode *N;
11815 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11817 AddNodeIDNode(ID, Opcode, VTList, Ops);
11818 void *IP = nullptr;
11819 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11820 E->intersectFlagsWith(Flags);
11821 return SDValue(E, 0);
11822 }
11823
11824 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11825 createOperands(N, Ops);
11826 CSEMap.InsertNode(N, IP);
11827 } else {
11828 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11829 createOperands(N, Ops);
11830 }
11831
11832 N->setFlags(Flags);
11833 InsertNode(N);
11834 SDValue V(N, 0);
11835 NewSDValueDbgMsg(V, "Creating new node: ", this);
11836 return V;
11837}
11838
11839SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11840 SDVTList VTList) {
11841 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11842}
11843
11844SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11845 SDValue N1) {
11846 SDValue Ops[] = { N1 };
11847 return getNode(Opcode, DL, VTList, Ops);
11848}
11849
11850SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11851 SDValue N1, SDValue N2) {
11852 SDValue Ops[] = { N1, N2 };
11853 return getNode(Opcode, DL, VTList, Ops);
11854}
11855
11856SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11857 SDValue N1, SDValue N2, SDValue N3) {
11858 SDValue Ops[] = { N1, N2, N3 };
11859 return getNode(Opcode, DL, VTList, Ops);
11860}
11861
11862SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11863 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11864 SDValue Ops[] = { N1, N2, N3, N4 };
11865 return getNode(Opcode, DL, VTList, Ops);
11866}
11867
11868SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11869 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11870 SDValue N5) {
11871 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11872 return getNode(Opcode, DL, VTList, Ops);
11873}
11874
11876 if (!VT.isExtended())
11877 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11878
11879 return makeVTList(&(*EVTs.insert(VT).first), 1);
11880}
11881
11884 ID.AddInteger(2U);
11885 ID.AddInteger(VT1.getRawBits());
11886 ID.AddInteger(VT2.getRawBits());
11887
11888 void *IP = nullptr;
11889 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11890 if (!Result) {
11891 EVT *Array = Allocator.Allocate<EVT>(2);
11892 Array[0] = VT1;
11893 Array[1] = VT2;
11894 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11895 VTListMap.InsertNode(Result, IP);
11896 }
11897 return Result->getSDVTList();
11898}
11899
11902 ID.AddInteger(3U);
11903 ID.AddInteger(VT1.getRawBits());
11904 ID.AddInteger(VT2.getRawBits());
11905 ID.AddInteger(VT3.getRawBits());
11906
11907 void *IP = nullptr;
11908 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11909 if (!Result) {
11910 EVT *Array = Allocator.Allocate<EVT>(3);
11911 Array[0] = VT1;
11912 Array[1] = VT2;
11913 Array[2] = VT3;
11914 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11915 VTListMap.InsertNode(Result, IP);
11916 }
11917 return Result->getSDVTList();
11918}
11919
11922 ID.AddInteger(4U);
11923 ID.AddInteger(VT1.getRawBits());
11924 ID.AddInteger(VT2.getRawBits());
11925 ID.AddInteger(VT3.getRawBits());
11926 ID.AddInteger(VT4.getRawBits());
11927
11928 void *IP = nullptr;
11929 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11930 if (!Result) {
11931 EVT *Array = Allocator.Allocate<EVT>(4);
11932 Array[0] = VT1;
11933 Array[1] = VT2;
11934 Array[2] = VT3;
11935 Array[3] = VT4;
11936 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11937 VTListMap.InsertNode(Result, IP);
11938 }
11939 return Result->getSDVTList();
11940}
11941
11943 unsigned NumVTs = VTs.size();
11945 ID.AddInteger(NumVTs);
11946 for (unsigned index = 0; index < NumVTs; index++) {
11947 ID.AddInteger(VTs[index].getRawBits());
11948 }
11949
11950 void *IP = nullptr;
11951 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11952 if (!Result) {
11953 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11954 llvm::copy(VTs, Array);
11955 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11956 VTListMap.InsertNode(Result, IP);
11957 }
11958 return Result->getSDVTList();
11959}
11960
11961
11962/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11963/// specified operands. If the resultant node already exists in the DAG,
11964/// this does not modify the specified node, instead it returns the node that
11965/// already exists. If the resultant node does not exist in the DAG, the
11966/// input node is returned. As a degenerate case, if you specify the same
11967/// input operands as the node already has, the input node is returned.
11969 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11970
11971 // Check to see if there is no change.
11972 if (Op == N->getOperand(0)) return N;
11973
11974 // See if the modified node already exists.
11975 void *InsertPos = nullptr;
11976 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11977 return Existing;
11978
11979 // Nope it doesn't. Remove the node from its current place in the maps.
11980 if (InsertPos)
11981 if (!RemoveNodeFromCSEMaps(N))
11982 InsertPos = nullptr;
11983
11984 // Now we update the operands.
11985 N->OperandList[0].set(Op);
11986
11988 // If this gets put into a CSE map, add it.
11989 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11990 return N;
11991}
11992
11994 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11995
11996 // Check to see if there is no change.
11997 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11998 return N; // No operands changed, just return the input node.
11999
12000 // See if the modified node already exists.
12001 void *InsertPos = nullptr;
12002 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
12003 return Existing;
12004
12005 // Nope it doesn't. Remove the node from its current place in the maps.
12006 if (InsertPos)
12007 if (!RemoveNodeFromCSEMaps(N))
12008 InsertPos = nullptr;
12009
12010 // Now we update the operands.
12011 if (N->OperandList[0] != Op1)
12012 N->OperandList[0].set(Op1);
12013 if (N->OperandList[1] != Op2)
12014 N->OperandList[1].set(Op2);
12015
12017 // If this gets put into a CSE map, add it.
12018 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
12019 return N;
12020}
12021
12024 SDValue Ops[] = { Op1, Op2, Op3 };
12025 return UpdateNodeOperands(N, Ops);
12026}
12027
12030 SDValue Op3, SDValue Op4) {
12031 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
12032 return UpdateNodeOperands(N, Ops);
12033}
12034
12037 SDValue Op3, SDValue Op4, SDValue Op5) {
12038 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
12039 return UpdateNodeOperands(N, Ops);
12040}
12041
12044 unsigned NumOps = Ops.size();
12045 assert(N->getNumOperands() == NumOps &&
12046 "Update with wrong number of operands");
12047
12048 // If no operands changed just return the input node.
12049 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
12050 return N;
12051
12052 // See if the modified node already exists.
12053 void *InsertPos = nullptr;
12054 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
12055 return Existing;
12056
12057 // Nope it doesn't. Remove the node from its current place in the maps.
12058 if (InsertPos)
12059 if (!RemoveNodeFromCSEMaps(N))
12060 InsertPos = nullptr;
12061
12062 // Now we update the operands.
12063 for (unsigned i = 0; i != NumOps; ++i)
12064 if (N->OperandList[i] != Ops[i])
12065 N->OperandList[i].set(Ops[i]);
12066
12068 // If this gets put into a CSE map, add it.
12069 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
12070 return N;
12071}
12072
12073/// DropOperands - Release the operands and set this node to have
12074/// zero operands.
12076 // Unlike the code in MorphNodeTo that does this, we don't need to
12077 // watch for dead nodes here.
12078 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
12079 SDUse &Use = *I++;
12080 Use.set(SDValue());
12081 }
12082}
12083
12085 ArrayRef<MachineMemOperand *> NewMemRefs) {
12086 if (NewMemRefs.empty()) {
12087 N->clearMemRefs();
12088 return;
12089 }
12090
12091 // Check if we can avoid allocating by storing a single reference directly.
12092 if (NewMemRefs.size() == 1) {
12093 N->MemRefs = NewMemRefs[0];
12094 N->NumMemRefs = 1;
12095 return;
12096 }
12097
12098 MachineMemOperand **MemRefsBuffer =
12099 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
12100 llvm::copy(NewMemRefs, MemRefsBuffer);
12101 N->MemRefs = MemRefsBuffer;
12102 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
12103}
12104
12105/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
12106/// machine opcode.
12107///
12109 EVT VT) {
12110 SDVTList VTs = getVTList(VT);
12111 return SelectNodeTo(N, MachineOpc, VTs, {});
12112}
12113
12115 EVT VT, SDValue Op1) {
12116 SDVTList VTs = getVTList(VT);
12117 SDValue Ops[] = { Op1 };
12118 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12119}
12120
12122 EVT VT, SDValue Op1,
12123 SDValue Op2) {
12124 SDVTList VTs = getVTList(VT);
12125 SDValue Ops[] = { Op1, Op2 };
12126 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12127}
12128
12130 EVT VT, SDValue Op1,
12131 SDValue Op2, SDValue Op3) {
12132 SDVTList VTs = getVTList(VT);
12133 SDValue Ops[] = { Op1, Op2, Op3 };
12134 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12135}
12136
12139 SDVTList VTs = getVTList(VT);
12140 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12141}
12142
12144 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
12145 SDVTList VTs = getVTList(VT1, VT2);
12146 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12147}
12148
12150 EVT VT1, EVT VT2) {
12151 SDVTList VTs = getVTList(VT1, VT2);
12152 return SelectNodeTo(N, MachineOpc, VTs, {});
12153}
12154
12156 EVT VT1, EVT VT2, EVT VT3,
12158 SDVTList VTs = getVTList(VT1, VT2, VT3);
12159 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12160}
12161
12163 EVT VT1, EVT VT2,
12164 SDValue Op1, SDValue Op2) {
12165 SDVTList VTs = getVTList(VT1, VT2);
12166 SDValue Ops[] = { Op1, Op2 };
12167 return SelectNodeTo(N, MachineOpc, VTs, Ops);
12168}
12169
12172 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
12173 // Reset the NodeID to -1.
12174 New->setNodeId(-1);
12175 if (New != N) {
12176 ReplaceAllUsesWith(N, New);
12178 }
12179 return New;
12180}
12181
12182/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
12183/// the line number information on the merged node since it is not possible to
12184/// preserve the information that operation is associated with multiple lines.
12185/// This will make the debugger working better at -O0, were there is a higher
12186/// probability having other instructions associated with that line.
12187///
12188/// For IROrder, we keep the smaller of the two
12189SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
12190 DebugLoc NLoc = N->getDebugLoc();
12191 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
12192 N->setDebugLoc(DebugLoc());
12193 }
12194 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
12195 N->setIROrder(Order);
12196 return N;
12197}
12198
12199/// MorphNodeTo - This *mutates* the specified node to have the specified
12200/// return type, opcode, and operands.
12201///
12202/// Note that MorphNodeTo returns the resultant node. If there is already a
12203/// node of the specified opcode and operands, it returns that node instead of
12204/// the current one. Note that the SDLoc need not be the same.
12205///
12206/// Using MorphNodeTo is faster than creating a new node and swapping it in
12207/// with ReplaceAllUsesWith both because it often avoids allocating a new
12208/// node, and because it doesn't require CSE recalculation for any of
12209/// the node's users.
12210///
12211/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
12212/// As a consequence it isn't appropriate to use from within the DAG combiner or
12213/// the legalizer which maintain worklists that would need to be updated when
12214/// deleting things.
12217 // If an identical node already exists, use it.
12218 void *IP = nullptr;
12219 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
12221 AddNodeIDNode(ID, Opc, VTs, Ops);
12222 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
12223 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
12224 }
12225
12226 if (!RemoveNodeFromCSEMaps(N))
12227 IP = nullptr;
12228
12229 // Start the morphing.
12230 N->NodeType = Opc;
12231 N->ValueList = VTs.VTs;
12232 N->NumValues = VTs.NumVTs;
12233
12234 // Clear the operands list, updating used nodes to remove this from their
12235 // use list. Keep track of any operands that become dead as a result.
12236 SmallPtrSet<SDNode*, 16> DeadNodeSet;
12237 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
12238 SDUse &Use = *I++;
12239 SDNode *Used = Use.getNode();
12240 Use.set(SDValue());
12241 if (Used->use_empty())
12242 DeadNodeSet.insert(Used);
12243 }
12244
12245 // For MachineNode, initialize the memory references information.
12247 MN->clearMemRefs();
12248
12249 // Swap for an appropriately sized array from the recycler.
12250 removeOperands(N);
12251 createOperands(N, Ops);
12252
12253 // Delete any nodes that are still dead after adding the uses for the
12254 // new operands.
12255 if (!DeadNodeSet.empty()) {
12256 SmallVector<SDNode *, 16> DeadNodes;
12257 for (SDNode *N : DeadNodeSet)
12258 if (N->use_empty())
12259 DeadNodes.push_back(N);
12260 RemoveDeadNodes(DeadNodes);
12261 }
12262
12263 if (IP)
12264 CSEMap.InsertNode(N, IP); // Memoize the new node.
12265 return N;
12266}
12267
12269 unsigned OrigOpc = Node->getOpcode();
12270 unsigned NewOpc;
12271 switch (OrigOpc) {
12272 default:
12273 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
12274#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
12275 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
12276#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
12277 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
12278#include "llvm/IR/ConstrainedOps.def"
12279 }
12280
12281 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
12282
12283 // We're taking this node out of the chain, so we need to re-link things.
12284 SDValue InputChain = Node->getOperand(0);
12285 SDValue OutputChain = SDValue(Node, 1);
12286 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
12287
12289 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
12290 Ops.push_back(Node->getOperand(i));
12291
12292 SDVTList VTs = getVTList(Node->getValueType(0));
12293 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
12294
12295 // MorphNodeTo can operate in two ways: if an existing node with the
12296 // specified operands exists, it can just return it. Otherwise, it
12297 // updates the node in place to have the requested operands.
12298 if (Res == Node) {
12299 // If we updated the node in place, reset the node ID. To the isel,
12300 // this should be just like a newly allocated machine node.
12301 Res->setNodeId(-1);
12302 } else {
12305 }
12306
12307 return Res;
12308}
12309
12310/// getMachineNode - These are used for target selectors to create a new node
12311/// with specified return type(s), MachineInstr opcode, and operands.
12312///
12313/// Note that getMachineNode returns the resultant node. If there is already a
12314/// node of the specified opcode and operands, it returns that node instead of
12315/// the current one.
12317 EVT VT) {
12318 SDVTList VTs = getVTList(VT);
12319 return getMachineNode(Opcode, dl, VTs, {});
12320}
12321
12323 EVT VT, SDValue Op1) {
12324 SDVTList VTs = getVTList(VT);
12325 SDValue Ops[] = { Op1 };
12326 return getMachineNode(Opcode, dl, VTs, Ops);
12327}
12328
12330 EVT VT, SDValue Op1, SDValue Op2) {
12331 SDVTList VTs = getVTList(VT);
12332 SDValue Ops[] = { Op1, Op2 };
12333 return getMachineNode(Opcode, dl, VTs, Ops);
12334}
12335
12337 EVT VT, SDValue Op1, SDValue Op2,
12338 SDValue Op3) {
12339 SDVTList VTs = getVTList(VT);
12340 SDValue Ops[] = { Op1, Op2, Op3 };
12341 return getMachineNode(Opcode, dl, VTs, Ops);
12342}
12343
12346 SDVTList VTs = getVTList(VT);
12347 return getMachineNode(Opcode, dl, VTs, Ops);
12348}
12349
12351 EVT VT1, EVT VT2, SDValue Op1,
12352 SDValue Op2) {
12353 SDVTList VTs = getVTList(VT1, VT2);
12354 SDValue Ops[] = { Op1, Op2 };
12355 return getMachineNode(Opcode, dl, VTs, Ops);
12356}
12357
12359 EVT VT1, EVT VT2, SDValue Op1,
12360 SDValue Op2, SDValue Op3) {
12361 SDVTList VTs = getVTList(VT1, VT2);
12362 SDValue Ops[] = { Op1, Op2, Op3 };
12363 return getMachineNode(Opcode, dl, VTs, Ops);
12364}
12365
12367 EVT VT1, EVT VT2,
12369 SDVTList VTs = getVTList(VT1, VT2);
12370 return getMachineNode(Opcode, dl, VTs, Ops);
12371}
12372
12374 EVT VT1, EVT VT2, EVT VT3,
12375 SDValue Op1, SDValue Op2) {
12376 SDVTList VTs = getVTList(VT1, VT2, VT3);
12377 SDValue Ops[] = { Op1, Op2 };
12378 return getMachineNode(Opcode, dl, VTs, Ops);
12379}
12380
12382 EVT VT1, EVT VT2, EVT VT3,
12383 SDValue Op1, SDValue Op2,
12384 SDValue Op3) {
12385 SDVTList VTs = getVTList(VT1, VT2, VT3);
12386 SDValue Ops[] = { Op1, Op2, Op3 };
12387 return getMachineNode(Opcode, dl, VTs, Ops);
12388}
12389
12391 EVT VT1, EVT VT2, EVT VT3,
12393 SDVTList VTs = getVTList(VT1, VT2, VT3);
12394 return getMachineNode(Opcode, dl, VTs, Ops);
12395}
12396
12398 ArrayRef<EVT> ResultTys,
12400 SDVTList VTs = getVTList(ResultTys);
12401 return getMachineNode(Opcode, dl, VTs, Ops);
12402}
12403
12405 SDVTList VTs,
12407 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
12409 void *IP = nullptr;
12410
12411 if (DoCSE) {
12413 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
12414 IP = nullptr;
12415 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
12416 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
12417 }
12418 }
12419
12420 // Allocate a new MachineSDNode.
12421 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
12422 createOperands(N, Ops);
12423
12424 if (DoCSE)
12425 CSEMap.InsertNode(N, IP);
12426
12427 InsertNode(N);
12428 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
12429 return N;
12430}
12431
12432/// getTargetExtractSubreg - A convenience function for creating
12433/// TargetOpcode::EXTRACT_SUBREG nodes.
12435 SDValue Operand) {
12436 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
12437 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
12438 VT, Operand, SRIdxVal);
12439 return SDValue(Subreg, 0);
12440}
12441
12442/// getTargetInsertSubreg - A convenience function for creating
12443/// TargetOpcode::INSERT_SUBREG nodes.
12445 SDValue Operand, SDValue Subreg) {
12446 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
12447 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
12448 VT, Operand, Subreg, SRIdxVal);
12449 return SDValue(Result, 0);
12450}
12451
12452/// getNodeIfExists - Get the specified node if it's already available, or
12453/// else return NULL.
12456 bool AllowCommute) {
12457 SDNodeFlags Flags;
12458 if (Inserter)
12459 Flags = Inserter->getFlags();
12460 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
12461}
12462
12465 const SDNodeFlags Flags,
12466 bool AllowCommute) {
12467 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
12468 return nullptr;
12469
12470 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
12472 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
12473 void *IP = nullptr;
12474 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
12475 E->intersectFlagsWith(Flags);
12476 return E;
12477 }
12478 return nullptr;
12479 };
12480
12481 if (SDNode *Existing = Lookup(Ops))
12482 return Existing;
12483
12484 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
12485 return Lookup({Ops[1], Ops[0]});
12486
12487 return nullptr;
12488}
12489
12490/// doesNodeExist - Check if a node exists without modifying its flags.
12491bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
12493 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
12495 AddNodeIDNode(ID, Opcode, VTList, Ops);
12496 void *IP = nullptr;
12497 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
12498 return true;
12499 }
12500 return false;
12501}
12502
12503/// getDbgValue - Creates a SDDbgValue node.
12504///
12505/// SDNode
12507 SDNode *N, unsigned R, bool IsIndirect,
12508 const DebugLoc &DL, unsigned O) {
12509 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12510 "Expected inlined-at fields to agree");
12511 return new (DbgInfo->getAlloc())
12512 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
12513 {}, IsIndirect, DL, O,
12514 /*IsVariadic=*/false);
12515}
12516
12517/// Constant
12519 DIExpression *Expr,
12520 const Value *C,
12521 const DebugLoc &DL, unsigned O) {
12522 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12523 "Expected inlined-at fields to agree");
12524 return new (DbgInfo->getAlloc())
12525 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
12526 /*IsIndirect=*/false, DL, O,
12527 /*IsVariadic=*/false);
12528}
12529
12530/// FrameIndex
12532 DIExpression *Expr, unsigned FI,
12533 bool IsIndirect,
12534 const DebugLoc &DL,
12535 unsigned O) {
12536 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12537 "Expected inlined-at fields to agree");
12538 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
12539}
12540
12541/// FrameIndex with dependencies
12543 DIExpression *Expr, unsigned FI,
12544 ArrayRef<SDNode *> Dependencies,
12545 bool IsIndirect,
12546 const DebugLoc &DL,
12547 unsigned O) {
12548 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12549 "Expected inlined-at fields to agree");
12550 return new (DbgInfo->getAlloc())
12551 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
12552 Dependencies, IsIndirect, DL, O,
12553 /*IsVariadic=*/false);
12554}
12555
12556/// VReg
12558 Register VReg, bool IsIndirect,
12559 const DebugLoc &DL, unsigned O) {
12560 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12561 "Expected inlined-at fields to agree");
12562 return new (DbgInfo->getAlloc())
12563 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
12564 {}, IsIndirect, DL, O,
12565 /*IsVariadic=*/false);
12566}
12567
12570 ArrayRef<SDNode *> Dependencies,
12571 bool IsIndirect, const DebugLoc &DL,
12572 unsigned O, bool IsVariadic) {
12573 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12574 "Expected inlined-at fields to agree");
12575 return new (DbgInfo->getAlloc())
12576 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
12577 DL, O, IsVariadic);
12578}
12579
12581 unsigned OffsetInBits, unsigned SizeInBits,
12582 bool InvalidateDbg) {
12583 SDNode *FromNode = From.getNode();
12584 SDNode *ToNode = To.getNode();
12585 assert(FromNode && ToNode && "Can't modify dbg values");
12586
12587 // PR35338
12588 // TODO: assert(From != To && "Redundant dbg value transfer");
12589 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
12590 if (From == To || FromNode == ToNode)
12591 return;
12592
12593 if (!FromNode->getHasDebugValue())
12594 return;
12595
12596 SDDbgOperand FromLocOp =
12597 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
12599
12601 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
12602 if (Dbg->isInvalidated())
12603 continue;
12604
12605 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
12606
12607 // Create a new location ops vector that is equal to the old vector, but
12608 // with each instance of FromLocOp replaced with ToLocOp.
12609 bool Changed = false;
12610 auto NewLocOps = Dbg->copyLocationOps();
12611 std::replace_if(
12612 NewLocOps.begin(), NewLocOps.end(),
12613 [&Changed, FromLocOp](const SDDbgOperand &Op) {
12614 bool Match = Op == FromLocOp;
12615 Changed |= Match;
12616 return Match;
12617 },
12618 ToLocOp);
12619 // Ignore this SDDbgValue if we didn't find a matching location.
12620 if (!Changed)
12621 continue;
12622
12623 DIVariable *Var = Dbg->getVariable();
12624 auto *Expr = Dbg->getExpression();
12625 // If a fragment is requested, update the expression.
12626 if (SizeInBits) {
12627 // When splitting a larger (e.g., sign-extended) value whose
12628 // lower bits are described with an SDDbgValue, do not attempt
12629 // to transfer the SDDbgValue to the upper bits.
12630 if (auto FI = Expr->getFragmentInfo())
12631 if (OffsetInBits + SizeInBits > FI->SizeInBits)
12632 continue;
12633 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
12634 SizeInBits);
12635 if (!Fragment)
12636 continue;
12637 Expr = *Fragment;
12638 }
12639
12640 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
12641 // Clone the SDDbgValue and move it to To.
12642 SDDbgValue *Clone = getDbgValueList(
12643 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12644 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12645 Dbg->isVariadic());
12646 ClonedDVs.push_back(Clone);
12647
12648 if (InvalidateDbg) {
12649 // Invalidate value and indicate the SDDbgValue should not be emitted.
12650 Dbg->setIsInvalidated();
12651 Dbg->setIsEmitted();
12652 }
12653 }
12654
12655 for (SDDbgValue *Dbg : ClonedDVs) {
12656 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12657 "Transferred DbgValues should depend on the new SDNode");
12658 AddDbgValue(Dbg, false);
12659 }
12660}
12661
12663 if (!N.getHasDebugValue())
12664 return;
12665
12666 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12667 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12668 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12669 return SDDbgOperand::fromNode(Node, ResNo);
12670 };
12671
12673 for (auto *DV : GetDbgValues(&N)) {
12674 if (DV->isInvalidated())
12675 continue;
12676 switch (N.getOpcode()) {
12677 default:
12678 break;
12679 case ISD::ADD: {
12680 SDValue N0 = N.getOperand(0);
12681 SDValue N1 = N.getOperand(1);
12682 if (!isa<ConstantSDNode>(N0)) {
12683 bool RHSConstant = isa<ConstantSDNode>(N1);
12685 if (RHSConstant)
12686 Offset = N.getConstantOperandVal(1);
12687 // We are not allowed to turn indirect debug values variadic, so
12688 // don't salvage those.
12689 if (!RHSConstant && DV->isIndirect())
12690 continue;
12691
12692 // Rewrite an ADD constant node into a DIExpression. Since we are
12693 // performing arithmetic to compute the variable's *value* in the
12694 // DIExpression, we need to mark the expression with a
12695 // DW_OP_stack_value.
12696 auto *DIExpr = DV->getExpression();
12697 auto NewLocOps = DV->copyLocationOps();
12698 bool Changed = false;
12699 size_t OrigLocOpsSize = NewLocOps.size();
12700 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12701 // We're not given a ResNo to compare against because the whole
12702 // node is going away. We know that any ISD::ADD only has one
12703 // result, so we can assume any node match is using the result.
12704 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12705 NewLocOps[i].getSDNode() != &N)
12706 continue;
12707 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12708 if (RHSConstant) {
12711 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12712 } else {
12713 // Convert to a variadic expression (if not already).
12714 // convertToVariadicExpression() returns a const pointer, so we use
12715 // a temporary const variable here.
12716 const auto *TmpDIExpr =
12720 ExprOps.push_back(NewLocOps.size());
12721 ExprOps.push_back(dwarf::DW_OP_plus);
12722 SDDbgOperand RHS =
12724 NewLocOps.push_back(RHS);
12725 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12726 }
12727 Changed = true;
12728 }
12729 (void)Changed;
12730 assert(Changed && "Salvage target doesn't use N");
12731
12732 bool IsVariadic =
12733 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12734
12735 auto AdditionalDependencies = DV->getAdditionalDependencies();
12736 SDDbgValue *Clone = getDbgValueList(
12737 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12738 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12739 ClonedDVs.push_back(Clone);
12740 DV->setIsInvalidated();
12741 DV->setIsEmitted();
12742 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12743 N0.getNode()->dumprFull(this);
12744 dbgs() << " into " << *DIExpr << '\n');
12745 }
12746 break;
12747 }
12748 case ISD::TRUNCATE: {
12749 SDValue N0 = N.getOperand(0);
12750 TypeSize FromSize = N0.getValueSizeInBits();
12751 TypeSize ToSize = N.getValueSizeInBits(0);
12752
12753 DIExpression *DbgExpression = DV->getExpression();
12754 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12755 auto NewLocOps = DV->copyLocationOps();
12756 bool Changed = false;
12757 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12758 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12759 NewLocOps[i].getSDNode() != &N)
12760 continue;
12761
12762 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12763 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12764 Changed = true;
12765 }
12766 assert(Changed && "Salvage target doesn't use N");
12767 (void)Changed;
12768
12769 SDDbgValue *Clone =
12770 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12771 DV->getAdditionalDependencies(), DV->isIndirect(),
12772 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12773
12774 ClonedDVs.push_back(Clone);
12775 DV->setIsInvalidated();
12776 DV->setIsEmitted();
12777 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12778 dbgs() << " into " << *DbgExpression << '\n');
12779 break;
12780 }
12781 }
12782 }
12783
12784 for (SDDbgValue *Dbg : ClonedDVs) {
12785 assert((!Dbg->getSDNodes().empty() ||
12786 llvm::any_of(Dbg->getLocationOps(),
12787 [&](const SDDbgOperand &Op) {
12788 return Op.getKind() == SDDbgOperand::FRAMEIX;
12789 })) &&
12790 "Salvaged DbgValue should depend on a new SDNode");
12791 AddDbgValue(Dbg, false);
12792 }
12793}
12794
12795/// Creates a SDDbgLabel node.
12797 const DebugLoc &DL, unsigned O) {
12798 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12799 "Expected inlined-at fields to agree");
12800 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12801}
12802
12803namespace {
12804
12805/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12806/// pointed to by a use iterator is deleted, increment the use iterator
12807/// so that it doesn't dangle.
12808///
12809class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12812
12813 void NodeDeleted(SDNode *N, SDNode *E) override {
12814 // Increment the iterator as needed.
12815 while (UI != UE && N == UI->getUser())
12816 ++UI;
12817 }
12818
12819public:
12820 RAUWUpdateListener(SelectionDAG &d,
12823 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12824};
12825
12826} // end anonymous namespace
12827
12828/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12829/// This can cause recursive merging of nodes in the DAG.
12830///
12831/// This version assumes From has a single result value.
12832///
12834 SDNode *From = FromN.getNode();
12835 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12836 "Cannot replace with this method!");
12837 assert(From != To.getNode() && "Cannot replace uses of with self");
12838
12839 // Preserve Debug Values
12840 transferDbgValues(FromN, To);
12841 // Preserve extra info.
12842 copyExtraInfo(From, To.getNode());
12843
12844 // Iterate over all the existing uses of From. New uses will be added
12845 // to the beginning of the use list, which we avoid visiting.
12846 // This specifically avoids visiting uses of From that arise while the
12847 // replacement is happening, because any such uses would be the result
12848 // of CSE: If an existing node looks like From after one of its operands
12849 // is replaced by To, we don't want to replace of all its users with To
12850 // too. See PR3018 for more info.
12851 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12852 RAUWUpdateListener Listener(*this, UI, UE);
12853 while (UI != UE) {
12854 SDNode *User = UI->getUser();
12855
12856 // This node is about to morph, remove its old self from the CSE maps.
12857 RemoveNodeFromCSEMaps(User);
12858
12859 // A user can appear in a use list multiple times, and when this
12860 // happens the uses are usually next to each other in the list.
12861 // To help reduce the number of CSE recomputations, process all
12862 // the uses of this user that we can find this way.
12863 do {
12864 SDUse &Use = *UI;
12865 ++UI;
12866 Use.set(To);
12867 if (To->isDivergent() != From->isDivergent())
12869 } while (UI != UE && UI->getUser() == User);
12870 // Now that we have modified User, add it back to the CSE maps. If it
12871 // already exists there, recursively merge the results together.
12872 AddModifiedNodeToCSEMaps(User);
12873 }
12874
12875 // If we just RAUW'd the root, take note.
12876 if (FromN == getRoot())
12877 setRoot(To);
12878}
12879
12880/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12881/// This can cause recursive merging of nodes in the DAG.
12882///
12883/// This version assumes that for each value of From, there is a
12884/// corresponding value in To in the same position with the same type.
12885///
12887#ifndef NDEBUG
12888 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12889 assert((!From->hasAnyUseOfValue(i) ||
12890 From->getValueType(i) == To->getValueType(i)) &&
12891 "Cannot use this version of ReplaceAllUsesWith!");
12892#endif
12893
12894 // Handle the trivial case.
12895 if (From == To)
12896 return;
12897
12898 // Preserve Debug Info. Only do this if there's a use.
12899 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12900 if (From->hasAnyUseOfValue(i)) {
12901 assert((i < To->getNumValues()) && "Invalid To location");
12902 transferDbgValues(SDValue(From, i), SDValue(To, i));
12903 }
12904 // Preserve extra info.
12905 copyExtraInfo(From, To);
12906
12907 // Iterate over just the existing users of From. See the comments in
12908 // the ReplaceAllUsesWith above.
12909 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12910 RAUWUpdateListener Listener(*this, UI, UE);
12911 while (UI != UE) {
12912 SDNode *User = UI->getUser();
12913
12914 // This node is about to morph, remove its old self from the CSE maps.
12915 RemoveNodeFromCSEMaps(User);
12916
12917 // A user can appear in a use list multiple times, and when this
12918 // happens the uses are usually next to each other in the list.
12919 // To help reduce the number of CSE recomputations, process all
12920 // the uses of this user that we can find this way.
12921 do {
12922 SDUse &Use = *UI;
12923 ++UI;
12924 Use.setNode(To);
12925 if (To->isDivergent() != From->isDivergent())
12927 } while (UI != UE && UI->getUser() == User);
12928
12929 // Now that we have modified User, add it back to the CSE maps. If it
12930 // already exists there, recursively merge the results together.
12931 AddModifiedNodeToCSEMaps(User);
12932 }
12933
12934 // If we just RAUW'd the root, take note.
12935 if (From == getRoot().getNode())
12936 setRoot(SDValue(To, getRoot().getResNo()));
12937}
12938
12939/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12940/// This can cause recursive merging of nodes in the DAG.
12941///
12942/// This version can replace From with any result values. To must match the
12943/// number and types of values returned by From.
12945 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12946 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12947
12948 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12949 // Preserve Debug Info.
12950 transferDbgValues(SDValue(From, i), To[i]);
12951 // Preserve extra info.
12952 copyExtraInfo(From, To[i].getNode());
12953 }
12954
12955 // Iterate over just the existing users of From. See the comments in
12956 // the ReplaceAllUsesWith above.
12957 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12958 RAUWUpdateListener Listener(*this, UI, UE);
12959 while (UI != UE) {
12960 SDNode *User = UI->getUser();
12961
12962 // This node is about to morph, remove its old self from the CSE maps.
12963 RemoveNodeFromCSEMaps(User);
12964
12965 // A user can appear in a use list multiple times, and when this happens the
12966 // uses are usually next to each other in the list. To help reduce the
12967 // number of CSE and divergence recomputations, process all the uses of this
12968 // user that we can find this way.
12969 bool To_IsDivergent = false;
12970 do {
12971 SDUse &Use = *UI;
12972 const SDValue &ToOp = To[Use.getResNo()];
12973 ++UI;
12974 Use.set(ToOp);
12975 if (ToOp.getValueType() != MVT::Other)
12976 To_IsDivergent |= ToOp->isDivergent();
12977 } while (UI != UE && UI->getUser() == User);
12978
12979 if (To_IsDivergent != From->isDivergent())
12981
12982 // Now that we have modified User, add it back to the CSE maps. If it
12983 // already exists there, recursively merge the results together.
12984 AddModifiedNodeToCSEMaps(User);
12985 }
12986
12987 // If we just RAUW'd the root, take note.
12988 if (From == getRoot().getNode())
12989 setRoot(SDValue(To[getRoot().getResNo()]));
12990}
12991
12992/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12993/// uses of other values produced by From.getNode() alone. The Deleted
12994/// vector is handled the same way as for ReplaceAllUsesWith.
12996 // Handle the really simple, really trivial case efficiently.
12997 if (From == To) return;
12998
12999 // Handle the simple, trivial, case efficiently.
13000 if (From.getNode()->getNumValues() == 1) {
13001 ReplaceAllUsesWith(From, To);
13002 return;
13003 }
13004
13005 // Preserve Debug Info.
13006 transferDbgValues(From, To);
13007 copyExtraInfo(From.getNode(), To.getNode());
13008
13009 // Iterate over just the existing users of From. See the comments in
13010 // the ReplaceAllUsesWith above.
13011 SDNode::use_iterator UI = From.getNode()->use_begin(),
13012 UE = From.getNode()->use_end();
13013 RAUWUpdateListener Listener(*this, UI, UE);
13014 while (UI != UE) {
13015 SDNode *User = UI->getUser();
13016 bool UserRemovedFromCSEMaps = false;
13017
13018 // A user can appear in a use list multiple times, and when this
13019 // happens the uses are usually next to each other in the list.
13020 // To help reduce the number of CSE recomputations, process all
13021 // the uses of this user that we can find this way.
13022 do {
13023 SDUse &Use = *UI;
13024
13025 // Skip uses of different values from the same node.
13026 if (Use.getResNo() != From.getResNo()) {
13027 ++UI;
13028 continue;
13029 }
13030
13031 // If this node hasn't been modified yet, it's still in the CSE maps,
13032 // so remove its old self from the CSE maps.
13033 if (!UserRemovedFromCSEMaps) {
13034 RemoveNodeFromCSEMaps(User);
13035 UserRemovedFromCSEMaps = true;
13036 }
13037
13038 ++UI;
13039 Use.set(To);
13040 if (To->isDivergent() != From->isDivergent())
13042 } while (UI != UE && UI->getUser() == User);
13043 // We are iterating over all uses of the From node, so if a use
13044 // doesn't use the specific value, no changes are made.
13045 if (!UserRemovedFromCSEMaps)
13046 continue;
13047
13048 // Now that we have modified User, add it back to the CSE maps. If it
13049 // already exists there, recursively merge the results together.
13050 AddModifiedNodeToCSEMaps(User);
13051 }
13052
13053 // If we just RAUW'd the root, take note.
13054 if (From == getRoot())
13055 setRoot(To);
13056}
13057
13058namespace {
13059
13060/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
13061/// to record information about a use.
13062struct UseMemo {
13063 SDNode *User;
13064 unsigned Index;
13065 SDUse *Use;
13066};
13067
13068/// operator< - Sort Memos by User.
13069bool operator<(const UseMemo &L, const UseMemo &R) {
13070 return (intptr_t)L.User < (intptr_t)R.User;
13071}
13072
13073/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
13074/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
13075/// the node already has been taken care of recursively.
13076class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
13077 SmallVectorImpl<UseMemo> &Uses;
13078
13079 void NodeDeleted(SDNode *N, SDNode *E) override {
13080 for (UseMemo &Memo : Uses)
13081 if (Memo.User == N)
13082 Memo.User = nullptr;
13083 }
13084
13085public:
13086 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
13087 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
13088};
13089
13090} // end anonymous namespace
13091
13092/// Return true if a glue output should propagate divergence information.
13094 switch (Node->getOpcode()) {
13095 case ISD::CopyFromReg:
13096 case ISD::CopyToReg:
13097 return false;
13098 default:
13099 return true;
13100 }
13101
13102 llvm_unreachable("covered opcode switch");
13103}
13104
13106 if (TLI->isSDNodeAlwaysUniform(N)) {
13107 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
13108 "Conflicting divergence information!");
13109 return false;
13110 }
13111 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
13112 return true;
13113 for (const auto &Op : N->ops()) {
13114 EVT VT = Op.getValueType();
13115
13116 // Skip Chain. It does not carry divergence.
13117 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
13118 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
13119 return true;
13120 }
13121 return false;
13122}
13123
13125 SmallVector<SDNode *, 16> Worklist(1, N);
13126 do {
13127 N = Worklist.pop_back_val();
13128 bool IsDivergent = calculateDivergence(N);
13129 if (N->SDNodeBits.IsDivergent != IsDivergent) {
13130 N->SDNodeBits.IsDivergent = IsDivergent;
13131 llvm::append_range(Worklist, N->users());
13132 }
13133 } while (!Worklist.empty());
13134}
13135
13136void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
13138 Order.reserve(AllNodes.size());
13139 for (auto &N : allnodes()) {
13140 unsigned NOps = N.getNumOperands();
13141 Degree[&N] = NOps;
13142 if (0 == NOps)
13143 Order.push_back(&N);
13144 }
13145 for (size_t I = 0; I != Order.size(); ++I) {
13146 SDNode *N = Order[I];
13147 for (auto *U : N->users()) {
13148 unsigned &UnsortedOps = Degree[U];
13149 if (0 == --UnsortedOps)
13150 Order.push_back(U);
13151 }
13152 }
13153}
13154
13155#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
13156void SelectionDAG::VerifyDAGDivergence() {
13157 std::vector<SDNode *> TopoOrder;
13158 CreateTopologicalOrder(TopoOrder);
13159 for (auto *N : TopoOrder) {
13160 assert(calculateDivergence(N) == N->isDivergent() &&
13161 "Divergence bit inconsistency detected");
13162 }
13163}
13164#endif
13165
13166/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
13167/// uses of other values produced by From.getNode() alone. The same value
13168/// may appear in both the From and To list. The Deleted vector is
13169/// handled the same way as for ReplaceAllUsesWith.
13171 const SDValue *To,
13172 unsigned Num){
13173 // Handle the simple, trivial case efficiently.
13174 if (Num == 1)
13175 return ReplaceAllUsesOfValueWith(*From, *To);
13176
13177 transferDbgValues(*From, *To);
13178 copyExtraInfo(From->getNode(), To->getNode());
13179
13180 // Read up all the uses and make records of them. This helps
13181 // processing new uses that are introduced during the
13182 // replacement process.
13184 for (unsigned i = 0; i != Num; ++i) {
13185 unsigned FromResNo = From[i].getResNo();
13186 SDNode *FromNode = From[i].getNode();
13187 for (SDUse &Use : FromNode->uses()) {
13188 if (Use.getResNo() == FromResNo) {
13189 UseMemo Memo = {Use.getUser(), i, &Use};
13190 Uses.push_back(Memo);
13191 }
13192 }
13193 }
13194
13195 // Sort the uses, so that all the uses from a given User are together.
13197 RAUOVWUpdateListener Listener(*this, Uses);
13198
13199 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
13200 UseIndex != UseIndexEnd; ) {
13201 // We know that this user uses some value of From. If it is the right
13202 // value, update it.
13203 SDNode *User = Uses[UseIndex].User;
13204 // If the node has been deleted by recursive CSE updates when updating
13205 // another node, then just skip this entry.
13206 if (User == nullptr) {
13207 ++UseIndex;
13208 continue;
13209 }
13210
13211 // This node is about to morph, remove its old self from the CSE maps.
13212 RemoveNodeFromCSEMaps(User);
13213
13214 // The Uses array is sorted, so all the uses for a given User
13215 // are next to each other in the list.
13216 // To help reduce the number of CSE recomputations, process all
13217 // the uses of this user that we can find this way.
13218 do {
13219 unsigned i = Uses[UseIndex].Index;
13220 SDUse &Use = *Uses[UseIndex].Use;
13221 ++UseIndex;
13222
13223 Use.set(To[i]);
13224 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
13225
13226 // Now that we have modified User, add it back to the CSE maps. If it
13227 // already exists there, recursively merge the results together.
13228 AddModifiedNodeToCSEMaps(User);
13229 }
13230}
13231
13232/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
13233/// based on their topological order. It returns the maximum id and a vector
13234/// of the SDNodes* in assigned order by reference.
13236 unsigned DAGSize = 0;
13237
13238 // SortedPos tracks the progress of the algorithm. Nodes before it are
13239 // sorted, nodes after it are unsorted. When the algorithm completes
13240 // it is at the end of the list.
13241 allnodes_iterator SortedPos = allnodes_begin();
13242
13243 // Visit all the nodes. Move nodes with no operands to the front of
13244 // the list immediately. Annotate nodes that do have operands with their
13245 // operand count. Before we do this, the Node Id fields of the nodes
13246 // may contain arbitrary values. After, the Node Id fields for nodes
13247 // before SortedPos will contain the topological sort index, and the
13248 // Node Id fields for nodes At SortedPos and after will contain the
13249 // count of outstanding operands.
13251 checkForCycles(&N, this);
13252 unsigned Degree = N.getNumOperands();
13253 if (Degree == 0) {
13254 // A node with no uses, add it to the result array immediately.
13255 N.setNodeId(DAGSize++);
13256 allnodes_iterator Q(&N);
13257 if (Q != SortedPos)
13258 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
13259 assert(SortedPos != AllNodes.end() && "Overran node list");
13260 ++SortedPos;
13261 } else {
13262 // Temporarily use the Node Id as scratch space for the degree count.
13263 N.setNodeId(Degree);
13264 }
13265 }
13266
13267 // Visit all the nodes. As we iterate, move nodes into sorted order,
13268 // such that by the time the end is reached all nodes will be sorted.
13269 for (SDNode &Node : allnodes()) {
13270 SDNode *N = &Node;
13271 checkForCycles(N, this);
13272 // N is in sorted position, so all its uses have one less operand
13273 // that needs to be sorted.
13274 for (SDNode *P : N->users()) {
13275 unsigned Degree = P->getNodeId();
13276 assert(Degree != 0 && "Invalid node degree");
13277 --Degree;
13278 if (Degree == 0) {
13279 // All of P's operands are sorted, so P may sorted now.
13280 P->setNodeId(DAGSize++);
13281 if (P->getIterator() != SortedPos)
13282 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
13283 assert(SortedPos != AllNodes.end() && "Overran node list");
13284 ++SortedPos;
13285 } else {
13286 // Update P's outstanding operand count.
13287 P->setNodeId(Degree);
13288 }
13289 }
13290 if (Node.getIterator() == SortedPos) {
13291#ifndef NDEBUG
13293 SDNode *S = &*++I;
13294 dbgs() << "Overran sorted position:\n";
13295 S->dumprFull(this); dbgs() << "\n";
13296 dbgs() << "Checking if this is due to cycles\n";
13297 checkForCycles(this, true);
13298#endif
13299 llvm_unreachable(nullptr);
13300 }
13301 }
13302
13303 assert(SortedPos == AllNodes.end() &&
13304 "Topological sort incomplete!");
13305 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
13306 "First node in topological sort is not the entry token!");
13307 assert(AllNodes.front().getNodeId() == 0 &&
13308 "First node in topological sort has non-zero id!");
13309 assert(AllNodes.front().getNumOperands() == 0 &&
13310 "First node in topological sort has operands!");
13311 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
13312 "Last node in topologic sort has unexpected id!");
13313 assert(AllNodes.back().use_empty() &&
13314 "Last node in topologic sort has users!");
13315 assert(DAGSize == allnodes_size() && "Node count mismatch!");
13316 return DAGSize;
13317}
13318
13320 SmallVectorImpl<const SDNode *> &SortedNodes) const {
13321 SortedNodes.clear();
13322 // Node -> remaining number of outstanding operands.
13323 DenseMap<const SDNode *, unsigned> RemainingOperands;
13324
13325 // Put nodes without any operands into SortedNodes first.
13326 for (const SDNode &N : allnodes()) {
13327 checkForCycles(&N, this);
13328 unsigned NumOperands = N.getNumOperands();
13329 if (NumOperands == 0)
13330 SortedNodes.push_back(&N);
13331 else
13332 // Record their total number of outstanding operands.
13333 RemainingOperands[&N] = NumOperands;
13334 }
13335
13336 // A node is pushed into SortedNodes when all of its operands (predecessors in
13337 // the graph) are also in SortedNodes.
13338 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
13339 const SDNode *N = SortedNodes[i];
13340 for (const SDNode *U : N->users()) {
13341 // HandleSDNode is never part of a DAG and therefore has no entry in
13342 // RemainingOperands.
13343 if (U->getOpcode() == ISD::HANDLENODE)
13344 continue;
13345 unsigned &NumRemOperands = RemainingOperands[U];
13346 assert(NumRemOperands && "Invalid number of remaining operands");
13347 --NumRemOperands;
13348 if (!NumRemOperands)
13349 SortedNodes.push_back(U);
13350 }
13351 }
13352
13353 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
13354 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
13355 "First node in topological sort is not the entry token");
13356 assert(SortedNodes.front()->getNumOperands() == 0 &&
13357 "First node in topological sort has operands");
13358}
13359
13360/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
13361/// value is produced by SD.
13362void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
13363 for (SDNode *SD : DB->getSDNodes()) {
13364 if (!SD)
13365 continue;
13366 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
13367 SD->setHasDebugValue(true);
13368 }
13369 DbgInfo->add(DB, isParameter);
13370}
13371
13372void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
13373
13375 SDValue NewMemOpChain) {
13376 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
13377 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
13378 // The new memory operation must have the same position as the old load in
13379 // terms of memory dependency. Create a TokenFactor for the old load and new
13380 // memory operation and update uses of the old load's output chain to use that
13381 // TokenFactor.
13382 if (OldChain == NewMemOpChain || OldChain.use_empty())
13383 return NewMemOpChain;
13384
13385 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
13386 OldChain, NewMemOpChain);
13387 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
13388 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
13389 return TokenFactor;
13390}
13391
13393 SDValue NewMemOp) {
13394 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
13395 SDValue OldChain = SDValue(OldLoad, 1);
13396 SDValue NewMemOpChain = NewMemOp.getValue(1);
13397 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
13398}
13399
13401 Function **OutFunction) {
13402 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
13403
13404 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
13405 auto *Module = MF->getFunction().getParent();
13406 auto *Function = Module->getFunction(Symbol);
13407
13408 if (OutFunction != nullptr)
13409 *OutFunction = Function;
13410
13411 if (Function != nullptr) {
13412 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
13413 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
13414 }
13415
13416 std::string ErrorStr;
13417 raw_string_ostream ErrorFormatter(ErrorStr);
13418 ErrorFormatter << "Undefined external symbol ";
13419 ErrorFormatter << '"' << Symbol << '"';
13420 report_fatal_error(Twine(ErrorStr));
13421}
13422
13423//===----------------------------------------------------------------------===//
13424// SDNode Class
13425//===----------------------------------------------------------------------===//
13426
13429 return Const != nullptr && Const->isZero();
13430}
13431
13433 return V.isUndef() || isNullConstant(V);
13434}
13435
13438 return Const != nullptr && Const->isZero() && !Const->isNegative();
13439}
13440
13443 return Const != nullptr && Const->isAllOnes();
13444}
13445
13448 return Const != nullptr && Const->isOne();
13449}
13450
13453 return Const != nullptr && Const->isMinSignedValue();
13454}
13455
13456bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
13457 unsigned OperandNo) {
13458 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
13459 // TODO: Target-specific opcodes could be added.
13460 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
13461 /*AllowTruncation*/ true)) {
13462 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
13463 switch (Opcode) {
13464 case ISD::ADD:
13465 case ISD::OR:
13466 case ISD::XOR:
13467 case ISD::UMAX:
13468 return Const.isZero();
13469 case ISD::MUL:
13470 return Const.isOne();
13471 case ISD::AND:
13472 case ISD::UMIN:
13473 return Const.isAllOnes();
13474 case ISD::SMAX:
13475 return Const.isMinSignedValue();
13476 case ISD::SMIN:
13477 return Const.isMaxSignedValue();
13478 case ISD::SUB:
13479 case ISD::SHL:
13480 case ISD::SRA:
13481 case ISD::SRL:
13482 return OperandNo == 1 && Const.isZero();
13483 case ISD::UDIV:
13484 case ISD::SDIV:
13485 return OperandNo == 1 && Const.isOne();
13486 }
13487 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
13488 switch (Opcode) {
13489 case ISD::FADD:
13490 return ConstFP->isZero() &&
13491 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
13492 case ISD::FSUB:
13493 return OperandNo == 1 && ConstFP->isZero() &&
13494 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
13495 case ISD::FMUL:
13496 return ConstFP->isExactlyValue(1.0);
13497 case ISD::FDIV:
13498 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
13499 case ISD::FMINNUM:
13500 case ISD::FMAXNUM: {
13501 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13502 EVT VT = V.getValueType();
13503 const fltSemantics &Semantics = VT.getFltSemantics();
13504 APFloat NeutralAF = !Flags.hasNoNaNs()
13505 ? APFloat::getQNaN(Semantics)
13506 : !Flags.hasNoInfs()
13507 ? APFloat::getInf(Semantics)
13508 : APFloat::getLargest(Semantics);
13509 if (Opcode == ISD::FMAXNUM)
13510 NeutralAF.changeSign();
13511
13512 return ConstFP->isExactlyValue(NeutralAF);
13513 }
13514 }
13515 }
13516 return false;
13517}
13518
13520 while (V.getOpcode() == ISD::BITCAST)
13521 V = V.getOperand(0);
13522 return V;
13523}
13524
13526 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
13527 V = V.getOperand(0);
13528 return V;
13529}
13530
13532 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
13533 V = V.getOperand(0);
13534 return V;
13535}
13536
13538 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13539 SDValue InVec = V.getOperand(0);
13540 SDValue EltNo = V.getOperand(2);
13541 EVT VT = InVec.getValueType();
13542 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
13543 if (IndexC && VT.isFixedLengthVector() &&
13544 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
13545 !DemandedElts[IndexC->getZExtValue()]) {
13546 V = InVec;
13547 continue;
13548 }
13549 break;
13550 }
13551 return V;
13552}
13553
13555 while (V.getOpcode() == ISD::TRUNCATE)
13556 V = V.getOperand(0);
13557 return V;
13558}
13559
13560bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
13561 if (V.getOpcode() != ISD::XOR)
13562 return false;
13563 V = peekThroughBitcasts(V.getOperand(1));
13564 unsigned NumBits = V.getScalarValueSizeInBits();
13565 ConstantSDNode *C =
13566 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
13567 return C && (C->getAPIntValue().countr_one() >= NumBits);
13568}
13569
13571 bool AllowTruncation) {
13572 APInt DemandedElts = getDemandAllEltsMask(N);
13573 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
13574}
13575
13577 bool AllowUndefs,
13578 bool AllowTruncation) {
13580 return CN;
13581
13582 // SplatVectors can truncate their operands. Ignore that case here unless
13583 // AllowTruncation is set.
13584 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
13585 EVT VecEltVT = N->getValueType(0).getVectorElementType();
13586 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
13587 EVT CVT = CN->getValueType(0);
13588 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
13589 if (AllowTruncation || CVT == VecEltVT)
13590 return CN;
13591 }
13592 }
13593
13595 BitVector UndefElements;
13596 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
13597
13598 // BuildVectors can truncate their operands. Ignore that case here unless
13599 // AllowTruncation is set.
13600 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13601 if (CN && (UndefElements.none() || AllowUndefs)) {
13602 EVT CVT = CN->getValueType(0);
13603 EVT NSVT = N.getValueType().getScalarType();
13604 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
13605 if (AllowTruncation || (CVT == NSVT))
13606 return CN;
13607 }
13608 }
13609
13610 return nullptr;
13611}
13612
13614 APInt DemandedElts = getDemandAllEltsMask(N);
13615 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
13616}
13617
13619 const APInt &DemandedElts,
13620 bool AllowUndefs) {
13622 return CN;
13623
13625 BitVector UndefElements;
13626 ConstantFPSDNode *CN =
13627 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
13628 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13629 if (CN && (UndefElements.none() || AllowUndefs))
13630 return CN;
13631 }
13632
13633 if (N.getOpcode() == ISD::SPLAT_VECTOR)
13634 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
13635 return CN;
13636
13637 return nullptr;
13638}
13639
13640bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
13641 // TODO: may want to use peekThroughBitcast() here.
13642 ConstantSDNode *C =
13643 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13644 return C && C->isZero();
13645}
13646
13647bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
13648 ConstantSDNode *C =
13649 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
13650 return C && C->isOne();
13651}
13652
13653bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
13654 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13655 return C && C->isExactlyValue(1.0);
13656}
13657
13658bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
13660 unsigned BitWidth = N.getScalarValueSizeInBits();
13661 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13662 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
13663}
13664
13665bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
13666 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13667 return C && APInt::isSameValue(C->getAPIntValue(),
13668 APInt(C->getAPIntValue().getBitWidth(), 1));
13669}
13670
13671bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
13673 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
13674 return C && C->isZero();
13675}
13676
13677bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
13678 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13679 return C && C->isZero();
13680}
13681
13685
13687 unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt,
13689 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MemRefs(memrefs) {
13690 bool IsVolatile = false;
13691 bool IsNonTemporal = false;
13692 bool IsDereferenceable = true;
13693 bool IsInvariant = true;
13694 for (const MachineMemOperand *MMO : memoperands()) {
13695 IsVolatile |= MMO->isVolatile();
13696 IsNonTemporal |= MMO->isNonTemporal();
13697 IsDereferenceable &= MMO->isDereferenceable();
13698 IsInvariant &= MMO->isInvariant();
13699 }
13700 MemSDNodeBits.IsVolatile = IsVolatile;
13701 MemSDNodeBits.IsNonTemporal = IsNonTemporal;
13702 MemSDNodeBits.IsDereferenceable = IsDereferenceable;
13703 MemSDNodeBits.IsInvariant = IsInvariant;
13704
13705 // For the single-MMO case, we check here that the size of the memory operand
13706 // fits within the size of the MMO. This is because the MMO might indicate
13707 // only a possible address range instead of specifying the affected memory
13708 // addresses precisely.
13711 getMemOperand()->getSize().getValue())) &&
13712 "Size mismatch!");
13713}
13714
13715/// Profile - Gather unique data for the node.
13716///
13718 AddNodeIDNode(ID, this);
13719}
13720
13721namespace {
13722
13723 struct EVTArray {
13724 std::vector<EVT> VTs;
13725
13726 EVTArray() {
13727 VTs.reserve(MVT::VALUETYPE_SIZE);
13728 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13729 VTs.push_back(MVT((MVT::SimpleValueType)i));
13730 }
13731 };
13732
13733} // end anonymous namespace
13734
13735/// getValueTypeList - Return a pointer to the specified value type.
13736///
13737const EVT *SDNode::getValueTypeList(MVT VT) {
13738 static EVTArray SimpleVTArray;
13739
13740 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13741 return &SimpleVTArray.VTs[VT.SimpleTy];
13742}
13743
13744/// hasAnyUseOfValue - Return true if there are any use of the indicated
13745/// value. This method ignores uses of other values defined by this operation.
13746bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13747 assert(Value < getNumValues() && "Bad value!");
13748
13749 for (SDUse &U : uses())
13750 if (U.getResNo() == Value)
13751 return true;
13752
13753 return false;
13754}
13755
13756/// isOnlyUserOf - Return true if this node is the only use of N.
13757bool SDNode::isOnlyUserOf(const SDNode *N) const {
13758 bool Seen = false;
13759 for (const SDNode *User : N->users()) {
13760 if (User == this)
13761 Seen = true;
13762 else
13763 return false;
13764 }
13765
13766 return Seen;
13767}
13768
13769/// Return true if the only users of N are contained in Nodes.
13771 bool Seen = false;
13772 for (const SDNode *User : N->users()) {
13773 if (llvm::is_contained(Nodes, User))
13774 Seen = true;
13775 else
13776 return false;
13777 }
13778
13779 return Seen;
13780}
13781
13782/// Return true if the referenced return value is an operand of N.
13783bool SDValue::isOperandOf(const SDNode *N) const {
13784 return is_contained(N->op_values(), *this);
13785}
13786
13787bool SDNode::isOperandOf(const SDNode *N) const {
13788 return any_of(N->op_values(),
13789 [this](SDValue Op) { return this == Op.getNode(); });
13790}
13791
13792/// reachesChainWithoutSideEffects - Return true if this operand (which must
13793/// be a chain) reaches the specified operand without crossing any
13794/// side-effecting instructions on any chain path. In practice, this looks
13795/// through token factors and non-volatile loads. In order to remain efficient,
13796/// this only looks a couple of nodes in, it does not do an exhaustive search.
13797///
13798/// Note that we only need to examine chains when we're searching for
13799/// side-effects; SelectionDAG requires that all side-effects are represented
13800/// by chains, even if another operand would force a specific ordering. This
13801/// constraint is necessary to allow transformations like splitting loads.
13803 unsigned Depth) const {
13804 if (*this == Dest) return true;
13805
13806 // Don't search too deeply, we just want to be able to see through
13807 // TokenFactor's etc.
13808 if (Depth == 0) return false;
13809
13810 // If this is a token factor, all inputs to the TF happen in parallel.
13811 if (getOpcode() == ISD::TokenFactor) {
13812 // First, try a shallow search.
13813 if (is_contained((*this)->ops(), Dest)) {
13814 // We found the chain we want as an operand of this TokenFactor.
13815 // Essentially, we reach the chain without side-effects if we could
13816 // serialize the TokenFactor into a simple chain of operations with
13817 // Dest as the last operation. This is automatically true if the
13818 // chain has one use: there are no other ordering constraints.
13819 // If the chain has more than one use, we give up: some other
13820 // use of Dest might force a side-effect between Dest and the current
13821 // node.
13822 if (Dest.hasOneUse())
13823 return true;
13824 }
13825 // Next, try a deep search: check whether every operand of the TokenFactor
13826 // reaches Dest.
13827 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13828 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13829 });
13830 }
13831
13832 // Loads don't have side effects, look through them.
13833 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13834 if (Ld->isUnordered())
13835 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13836 }
13837 return false;
13838}
13839
13840bool SDNode::hasPredecessor(const SDNode *N) const {
13843 Worklist.push_back(this);
13844 return hasPredecessorHelper(N, Visited, Worklist);
13845}
13846
13848 this->Flags &= Flags;
13849}
13850
13851SDValue
13853 ArrayRef<ISD::NodeType> CandidateBinOps,
13854 bool AllowPartials) {
13855 // The pattern must end in an extract from index 0.
13856 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13857 !isNullConstant(Extract->getOperand(1)))
13858 return SDValue();
13859
13860 // Match against one of the candidate binary ops.
13861 SDValue Op = Extract->getOperand(0);
13862 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13863 return Op.getOpcode() == unsigned(BinOp);
13864 }))
13865 return SDValue();
13866
13867 // Floating-point reductions may require relaxed constraints on the final step
13868 // of the reduction because they may reorder intermediate operations.
13869 unsigned CandidateBinOp = Op.getOpcode();
13870 if (Op.getValueType().isFloatingPoint()) {
13871 SDNodeFlags Flags = Op->getFlags();
13872 switch (CandidateBinOp) {
13873 case ISD::FADD:
13874 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13875 return SDValue();
13876 break;
13877 default:
13878 llvm_unreachable("Unhandled FP opcode for binop reduction");
13879 }
13880 }
13881
13882 // Matching failed - attempt to see if we did enough stages that a partial
13883 // reduction from a subvector is possible.
13884 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13885 if (!AllowPartials || !Op)
13886 return SDValue();
13887 EVT OpVT = Op.getValueType();
13888 EVT OpSVT = OpVT.getScalarType();
13889 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13890 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13891 return SDValue();
13892 BinOp = (ISD::NodeType)CandidateBinOp;
13893 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13894 };
13895
13896 // At each stage, we're looking for something that looks like:
13897 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13898 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13899 // i32 undef, i32 undef, i32 undef, i32 undef>
13900 // %a = binop <8 x i32> %op, %s
13901 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13902 // we expect something like:
13903 // <4,5,6,7,u,u,u,u>
13904 // <2,3,u,u,u,u,u,u>
13905 // <1,u,u,u,u,u,u,u>
13906 // While a partial reduction match would be:
13907 // <2,3,u,u,u,u,u,u>
13908 // <1,u,u,u,u,u,u,u>
13909 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13910 SDValue PrevOp;
13911 for (unsigned i = 0; i < Stages; ++i) {
13912 unsigned MaskEnd = (1 << i);
13913
13914 if (Op.getOpcode() != CandidateBinOp)
13915 return PartialReduction(PrevOp, MaskEnd);
13916
13917 SDValue Op0 = Op.getOperand(0);
13918 SDValue Op1 = Op.getOperand(1);
13919
13921 if (Shuffle) {
13922 Op = Op1;
13923 } else {
13924 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13925 Op = Op0;
13926 }
13927
13928 // The first operand of the shuffle should be the same as the other operand
13929 // of the binop.
13930 if (!Shuffle || Shuffle->getOperand(0) != Op)
13931 return PartialReduction(PrevOp, MaskEnd);
13932
13933 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13934 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13935 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13936 return PartialReduction(PrevOp, MaskEnd);
13937
13938 PrevOp = Op;
13939 }
13940
13941 // Handle subvector reductions, which tend to appear after the shuffle
13942 // reduction stages.
13943 while (Op.getOpcode() == CandidateBinOp) {
13944 unsigned NumElts = Op.getValueType().getVectorNumElements();
13945 SDValue Op0 = Op.getOperand(0);
13946 SDValue Op1 = Op.getOperand(1);
13947 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13949 Op0.getOperand(0) != Op1.getOperand(0))
13950 break;
13951 SDValue Src = Op0.getOperand(0);
13952 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13953 if (NumSrcElts != (2 * NumElts))
13954 break;
13955 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13956 Op1.getConstantOperandAPInt(1) == NumElts) &&
13957 !(Op1.getConstantOperandAPInt(1) == 0 &&
13958 Op0.getConstantOperandAPInt(1) == NumElts))
13959 break;
13960 Op = Src;
13961 }
13962
13963 BinOp = (ISD::NodeType)CandidateBinOp;
13964 return Op;
13965}
13966
13968 EVT VT = N->getValueType(0);
13969 EVT EltVT = VT.getVectorElementType();
13970 unsigned NE = VT.getVectorNumElements();
13971
13972 SDLoc dl(N);
13973
13974 // If ResNE is 0, fully unroll the vector op.
13975 if (ResNE == 0)
13976 ResNE = NE;
13977 else if (NE > ResNE)
13978 NE = ResNE;
13979
13980 if (N->getNumValues() == 2) {
13981 SmallVector<SDValue, 8> Scalars0, Scalars1;
13982 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13983 EVT VT1 = N->getValueType(1);
13984 EVT EltVT1 = VT1.getVectorElementType();
13985
13986 unsigned i;
13987 for (i = 0; i != NE; ++i) {
13988 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13989 SDValue Operand = N->getOperand(j);
13990 EVT OperandVT = Operand.getValueType();
13991
13992 // A vector operand; extract a single element.
13993 EVT OperandEltVT = OperandVT.getVectorElementType();
13994 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13995 }
13996
13997 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13998 Scalars0.push_back(EltOp);
13999 Scalars1.push_back(EltOp.getValue(1));
14000 }
14001
14002 for (; i < ResNE; ++i) {
14003 Scalars0.push_back(getUNDEF(EltVT));
14004 Scalars1.push_back(getUNDEF(EltVT1));
14005 }
14006
14007 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
14008 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
14009 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
14010 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
14011 return getMergeValues({Vec0, Vec1}, dl);
14012 }
14013
14014 assert(N->getNumValues() == 1 &&
14015 "Can't unroll a vector with multiple results!");
14016
14018 SmallVector<SDValue, 4> Operands(N->getNumOperands());
14019
14020 unsigned i;
14021 for (i= 0; i != NE; ++i) {
14022 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
14023 SDValue Operand = N->getOperand(j);
14024 EVT OperandVT = Operand.getValueType();
14025 if (OperandVT.isVector()) {
14026 // A vector operand; extract a single element.
14027 EVT OperandEltVT = OperandVT.getVectorElementType();
14028 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
14029 } else {
14030 // A scalar operand; just use it as is.
14031 Operands[j] = Operand;
14032 }
14033 }
14034
14035 switch (N->getOpcode()) {
14036 default: {
14037 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
14038 N->getFlags()));
14039 break;
14040 }
14041 case ISD::VSELECT:
14042 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
14043 break;
14044 case ISD::SHL:
14045 case ISD::SRA:
14046 case ISD::SRL:
14047 case ISD::ROTL:
14048 case ISD::ROTR:
14049 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
14050 getShiftAmountOperand(Operands[0].getValueType(),
14051 Operands[1])));
14052 break;
14054 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
14055 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
14056 Operands[0],
14057 getValueType(ExtVT)));
14058 break;
14059 }
14060 case ISD::ADDRSPACECAST: {
14061 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
14062 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
14063 ASC->getSrcAddressSpace(),
14064 ASC->getDestAddressSpace()));
14065 break;
14066 }
14067 }
14068 }
14069
14070 for (; i < ResNE; ++i)
14071 Scalars.push_back(getUNDEF(EltVT));
14072
14073 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
14074 return getBuildVector(VecVT, dl, Scalars);
14075}
14076
14077std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
14078 SDNode *N, unsigned ResNE) {
14079 unsigned Opcode = N->getOpcode();
14080 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
14081 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
14082 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
14083 "Expected an overflow opcode");
14084
14085 EVT ResVT = N->getValueType(0);
14086 EVT OvVT = N->getValueType(1);
14087 EVT ResEltVT = ResVT.getVectorElementType();
14088 EVT OvEltVT = OvVT.getVectorElementType();
14089 SDLoc dl(N);
14090
14091 // If ResNE is 0, fully unroll the vector op.
14092 unsigned NE = ResVT.getVectorNumElements();
14093 if (ResNE == 0)
14094 ResNE = NE;
14095 else if (NE > ResNE)
14096 NE = ResNE;
14097
14098 SmallVector<SDValue, 8> LHSScalars;
14099 SmallVector<SDValue, 8> RHSScalars;
14100 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
14101 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
14102
14103 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
14104 SDVTList VTs = getVTList(ResEltVT, SVT);
14105 SmallVector<SDValue, 8> ResScalars;
14106 SmallVector<SDValue, 8> OvScalars;
14107 for (unsigned i = 0; i < NE; ++i) {
14108 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
14109 SDValue Ov =
14110 getSelect(dl, OvEltVT, Res.getValue(1),
14111 getBoolConstant(true, dl, OvEltVT, ResVT),
14112 getConstant(0, dl, OvEltVT));
14113
14114 ResScalars.push_back(Res);
14115 OvScalars.push_back(Ov);
14116 }
14117
14118 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
14119 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
14120
14121 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
14122 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
14123 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
14124 getBuildVector(NewOvVT, dl, OvScalars));
14125}
14126
14129 unsigned Bytes,
14130 int Dist) const {
14131 if (LD->isVolatile() || Base->isVolatile())
14132 return false;
14133 // TODO: probably too restrictive for atomics, revisit
14134 if (!LD->isSimple())
14135 return false;
14136 if (LD->isIndexed() || Base->isIndexed())
14137 return false;
14138 if (LD->getChain() != Base->getChain())
14139 return false;
14140 EVT VT = LD->getMemoryVT();
14141 if (VT.getSizeInBits() / 8 != Bytes)
14142 return false;
14143
14144 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
14145 auto LocDecomp = BaseIndexOffset::match(LD, *this);
14146
14147 int64_t Offset = 0;
14148 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
14149 return (Dist * (int64_t)Bytes == Offset);
14150 return false;
14151}
14152
14153/// InferPtrAlignment - Infer alignment of a load / store address. Return
14154/// std::nullopt if it cannot be inferred.
14156 // If this is a GlobalAddress + cst, return the alignment.
14157 const GlobalValue *GV = nullptr;
14158 int64_t GVOffset = 0;
14159 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
14160 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
14161 KnownBits Known(PtrWidth);
14163 unsigned AlignBits = Known.countMinTrailingZeros();
14164 if (AlignBits)
14165 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
14166 }
14167
14168 // If this is a direct reference to a stack slot, use information about the
14169 // stack slot's alignment.
14170 int FrameIdx = INT_MIN;
14171 int64_t FrameOffset = 0;
14173 FrameIdx = FI->getIndex();
14174 } else if (isBaseWithConstantOffset(Ptr) &&
14176 // Handle FI+Cst
14177 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
14178 FrameOffset = Ptr.getConstantOperandVal(1);
14179 }
14180
14181 if (FrameIdx != INT_MIN) {
14183 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
14184 }
14185
14186 return std::nullopt;
14187}
14188
14189/// Split the scalar node with EXTRACT_ELEMENT using the provided
14190/// VTs and return the low/high part.
14191std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
14192 const SDLoc &DL,
14193 const EVT &LoVT,
14194 const EVT &HiVT) {
14195 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
14196 "Split node must be a scalar type");
14197 SDValue Lo =
14199 SDValue Hi =
14201 return std::make_pair(Lo, Hi);
14202}
14203
14204/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
14205/// which is split (or expanded) into two not necessarily identical pieces.
14206std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
14207 // Currently all types are split in half.
14208 EVT LoVT, HiVT;
14209 if (!VT.isVector())
14210 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
14211 else
14212 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
14213
14214 return std::make_pair(LoVT, HiVT);
14215}
14216
14217/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
14218/// type, dependent on an enveloping VT that has been split into two identical
14219/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
14220std::pair<EVT, EVT>
14222 bool *HiIsEmpty) const {
14223 EVT EltTp = VT.getVectorElementType();
14224 // Examples:
14225 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
14226 // custom VL=9 with enveloping VL=8/8 yields 8/1
14227 // custom VL=10 with enveloping VL=8/8 yields 8/2
14228 // etc.
14229 ElementCount VTNumElts = VT.getVectorElementCount();
14230 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
14231 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
14232 "Mixing fixed width and scalable vectors when enveloping a type");
14233 EVT LoVT, HiVT;
14234 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
14235 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
14236 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
14237 *HiIsEmpty = false;
14238 } else {
14239 // Flag that hi type has zero storage size, but return split envelop type
14240 // (this would be easier if vector types with zero elements were allowed).
14241 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
14242 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
14243 *HiIsEmpty = true;
14244 }
14245 return std::make_pair(LoVT, HiVT);
14246}
14247
14248/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
14249/// low/high part.
14250std::pair<SDValue, SDValue>
14251SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
14252 const EVT &HiVT) {
14253 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
14254 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
14255 "Splitting vector with an invalid mixture of fixed and scalable "
14256 "vector types");
14258 N.getValueType().getVectorMinNumElements() &&
14259 "More vector elements requested than available!");
14260 SDValue Lo, Hi;
14261 Lo = getExtractSubvector(DL, LoVT, N, 0);
14262 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
14263 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
14264 // IDX with the runtime scaling factor of the result vector type. For
14265 // fixed-width result vectors, that runtime scaling factor is 1.
14268 return std::make_pair(Lo, Hi);
14269}
14270
14271std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
14272 const SDLoc &DL) {
14273 // Split the vector length parameter.
14274 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
14275 EVT VT = N.getValueType();
14277 "Expecting the mask to be an evenly-sized vector");
14278 SDValue HalfNumElts = getElementCount(
14280 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
14281 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
14282 return std::make_pair(Lo, Hi);
14283}
14284
14285/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
14287 EVT VT = N.getValueType();
14290 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
14291}
14292
14295 unsigned Start, unsigned Count,
14296 EVT EltVT) {
14297 EVT VT = Op.getValueType();
14298 if (Count == 0)
14300 if (EltVT == EVT())
14301 EltVT = VT.getVectorElementType();
14302 SDLoc SL(Op);
14303 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
14304 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
14305 }
14306}
14307
14308// getAddressSpace - Return the address space this GlobalAddress belongs to.
14310 return getGlobal()->getType()->getAddressSpace();
14311}
14312
14315 return Val.MachineCPVal->getType();
14316 return Val.ConstVal->getType();
14317}
14318
14319bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
14320 unsigned &SplatBitSize,
14321 bool &HasAnyUndefs,
14322 unsigned MinSplatBits,
14323 bool IsBigEndian) const {
14324 EVT VT = getValueType(0);
14325 assert(VT.isVector() && "Expected a vector type");
14326 unsigned VecWidth = VT.getSizeInBits();
14327 if (MinSplatBits > VecWidth)
14328 return false;
14329
14330 // FIXME: The widths are based on this node's type, but build vectors can
14331 // truncate their operands.
14332 SplatValue = APInt(VecWidth, 0);
14333 SplatUndef = APInt(VecWidth, 0);
14334
14335 // Get the bits. Bits with undefined values (when the corresponding element
14336 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
14337 // in SplatValue. If any of the values are not constant, give up and return
14338 // false.
14339 unsigned int NumOps = getNumOperands();
14340 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
14341 unsigned EltWidth = VT.getScalarSizeInBits();
14342
14343 for (unsigned j = 0; j < NumOps; ++j) {
14344 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
14345 SDValue OpVal = getOperand(i);
14346 unsigned BitPos = j * EltWidth;
14347
14348 if (OpVal.isUndef())
14349 SplatUndef.setBits(BitPos, BitPos + EltWidth);
14350 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
14351 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
14352 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
14353 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
14354 else
14355 return false;
14356 }
14357
14358 // The build_vector is all constants or undefs. Find the smallest element
14359 // size that splats the vector.
14360 HasAnyUndefs = (SplatUndef != 0);
14361
14362 // FIXME: This does not work for vectors with elements less than 8 bits.
14363 while (VecWidth > 8) {
14364 // If we can't split in half, stop here.
14365 if (VecWidth & 1)
14366 break;
14367
14368 unsigned HalfSize = VecWidth / 2;
14369 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
14370 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
14371 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
14372 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
14373
14374 // If the two halves do not match (ignoring undef bits), stop here.
14375 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
14376 MinSplatBits > HalfSize)
14377 break;
14378
14379 SplatValue = HighValue | LowValue;
14380 SplatUndef = HighUndef & LowUndef;
14381
14382 VecWidth = HalfSize;
14383 }
14384
14385 // FIXME: The loop above only tries to split in halves. But if the input
14386 // vector for example is <3 x i16> it wouldn't be able to detect a
14387 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
14388 // optimizations. I guess that back in the days when this helper was created
14389 // vectors normally was power-of-2 sized.
14390
14391 SplatBitSize = VecWidth;
14392 return true;
14393}
14394
14396 BitVector *UndefElements) const {
14397 unsigned NumOps = getNumOperands();
14398 if (UndefElements) {
14399 UndefElements->clear();
14400 UndefElements->resize(NumOps);
14401 }
14402 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
14403 if (!DemandedElts)
14404 return SDValue();
14405 SDValue Splatted;
14406 for (unsigned i = 0; i != NumOps; ++i) {
14407 if (!DemandedElts[i])
14408 continue;
14409 SDValue Op = getOperand(i);
14410 if (Op.isUndef()) {
14411 if (UndefElements)
14412 (*UndefElements)[i] = true;
14413 } else if (!Splatted) {
14414 Splatted = Op;
14415 } else if (Splatted != Op) {
14416 return SDValue();
14417 }
14418 }
14419
14420 if (!Splatted) {
14421 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
14422 assert(getOperand(FirstDemandedIdx).isUndef() &&
14423 "Can only have a splat without a constant for all undefs.");
14424 return getOperand(FirstDemandedIdx);
14425 }
14426
14427 return Splatted;
14428}
14429
14431 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
14432 return getSplatValue(DemandedElts, UndefElements);
14433}
14434
14436 SmallVectorImpl<SDValue> &Sequence,
14437 BitVector *UndefElements) const {
14438 unsigned NumOps = getNumOperands();
14439 Sequence.clear();
14440 if (UndefElements) {
14441 UndefElements->clear();
14442 UndefElements->resize(NumOps);
14443 }
14444 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
14445 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
14446 return false;
14447
14448 // Set the undefs even if we don't find a sequence (like getSplatValue).
14449 if (UndefElements)
14450 for (unsigned I = 0; I != NumOps; ++I)
14451 if (DemandedElts[I] && getOperand(I).isUndef())
14452 (*UndefElements)[I] = true;
14453
14454 // Iteratively widen the sequence length looking for repetitions.
14455 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
14456 Sequence.append(SeqLen, SDValue());
14457 for (unsigned I = 0; I != NumOps; ++I) {
14458 if (!DemandedElts[I])
14459 continue;
14460 SDValue &SeqOp = Sequence[I % SeqLen];
14462 if (Op.isUndef()) {
14463 if (!SeqOp)
14464 SeqOp = Op;
14465 continue;
14466 }
14467 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
14468 Sequence.clear();
14469 break;
14470 }
14471 SeqOp = Op;
14472 }
14473 if (!Sequence.empty())
14474 return true;
14475 }
14476
14477 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
14478 return false;
14479}
14480
14482 BitVector *UndefElements) const {
14483 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
14484 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
14485}
14486
14489 BitVector *UndefElements) const {
14491 getSplatValue(DemandedElts, UndefElements));
14492}
14493
14496 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
14497}
14498
14501 BitVector *UndefElements) const {
14503 getSplatValue(DemandedElts, UndefElements));
14504}
14505
14510
14511int32_t
14513 uint32_t BitWidth) const {
14514 if (ConstantFPSDNode *CN =
14516 bool IsExact;
14517 APSInt IntVal(BitWidth);
14518 const APFloat &APF = CN->getValueAPF();
14519 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
14520 APFloat::opOK ||
14521 !IsExact)
14522 return -1;
14523
14524 return IntVal.exactLogBase2();
14525 }
14526 return -1;
14527}
14528
14530 bool IsLittleEndian, unsigned DstEltSizeInBits,
14531 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
14532 // Early-out if this contains anything but Undef/Constant/ConstantFP.
14533 if (!isConstant())
14534 return false;
14535
14536 unsigned NumSrcOps = getNumOperands();
14537 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
14538 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14539 "Invalid bitcast scale");
14540
14541 // Extract raw src bits.
14542 SmallVector<APInt> SrcBitElements(NumSrcOps,
14543 APInt::getZero(SrcEltSizeInBits));
14544 BitVector SrcUndeElements(NumSrcOps, false);
14545
14546 for (unsigned I = 0; I != NumSrcOps; ++I) {
14548 if (Op.isUndef()) {
14549 SrcUndeElements.set(I);
14550 continue;
14551 }
14552 auto *CInt = dyn_cast<ConstantSDNode>(Op);
14553 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
14554 assert((CInt || CFP) && "Unknown constant");
14555 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
14556 : CFP->getValueAPF().bitcastToAPInt();
14557 }
14558
14559 // Recast to dst width.
14560 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
14561 SrcBitElements, UndefElements, SrcUndeElements);
14562 return true;
14563}
14564
14565void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
14566 unsigned DstEltSizeInBits,
14567 SmallVectorImpl<APInt> &DstBitElements,
14568 ArrayRef<APInt> SrcBitElements,
14569 BitVector &DstUndefElements,
14570 const BitVector &SrcUndefElements) {
14571 unsigned NumSrcOps = SrcBitElements.size();
14572 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
14573 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14574 "Invalid bitcast scale");
14575 assert(NumSrcOps == SrcUndefElements.size() &&
14576 "Vector size mismatch");
14577
14578 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
14579 DstUndefElements.clear();
14580 DstUndefElements.resize(NumDstOps, false);
14581 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
14582
14583 // Concatenate src elements constant bits together into dst element.
14584 if (SrcEltSizeInBits <= DstEltSizeInBits) {
14585 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
14586 for (unsigned I = 0; I != NumDstOps; ++I) {
14587 DstUndefElements.set(I);
14588 APInt &DstBits = DstBitElements[I];
14589 for (unsigned J = 0; J != Scale; ++J) {
14590 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14591 if (SrcUndefElements[Idx])
14592 continue;
14593 DstUndefElements.reset(I);
14594 const APInt &SrcBits = SrcBitElements[Idx];
14595 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
14596 "Illegal constant bitwidths");
14597 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
14598 }
14599 }
14600 return;
14601 }
14602
14603 // Split src element constant bits into dst elements.
14604 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
14605 for (unsigned I = 0; I != NumSrcOps; ++I) {
14606 if (SrcUndefElements[I]) {
14607 DstUndefElements.set(I * Scale, (I + 1) * Scale);
14608 continue;
14609 }
14610 const APInt &SrcBits = SrcBitElements[I];
14611 for (unsigned J = 0; J != Scale; ++J) {
14612 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14613 APInt &DstBits = DstBitElements[Idx];
14614 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
14615 }
14616 }
14617}
14618
14620 for (const SDValue &Op : op_values()) {
14621 unsigned Opc = Op.getOpcode();
14622 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
14623 return false;
14624 }
14625 return true;
14626}
14627
14628std::optional<std::pair<APInt, APInt>>
14630 unsigned NumOps = getNumOperands();
14631 if (NumOps < 2)
14632 return std::nullopt;
14633
14634 unsigned EltSize = getValueType(0).getScalarSizeInBits();
14635 APInt Start, Stride;
14636 int FirstIdx = -1, SecondIdx = -1;
14637
14638 // Find the first two non-undef constant elements to determine Start and
14639 // Stride, then verify all remaining elements match the sequence.
14640 for (unsigned I = 0; I < NumOps; ++I) {
14642 if (Op->isUndef())
14643 continue;
14644 if (!isa<ConstantSDNode>(Op))
14645 return std::nullopt;
14646
14647 APInt Val = getConstantOperandAPInt(I).trunc(EltSize);
14648 if (FirstIdx < 0) {
14649 FirstIdx = I;
14650 Start = Val;
14651 } else if (SecondIdx < 0) {
14652 SecondIdx = I;
14653 // Compute stride using modular arithmetic. Simple division would handle
14654 // common strides (1, 2, -1, etc.), but modular inverse maximizes matches.
14655 // Example: <0, poison, poison, 0xFF> has stride 0x55 since 3*0x55 = 0xFF
14656 // Note that modular arithmetic is agnostic to signed/unsigned.
14657 unsigned IdxDiff = I - FirstIdx;
14658 APInt ValDiff = Val - Start;
14659
14660 // Step 1: Factor out common powers of 2 from IdxDiff and ValDiff.
14661 unsigned CommonPow2Bits = llvm::countr_zero(IdxDiff);
14662 if (ValDiff.countr_zero() < CommonPow2Bits)
14663 return std::nullopt; // ValDiff not divisible by 2^CommonPow2Bits
14664 IdxDiff >>= CommonPow2Bits;
14665 ValDiff.lshrInPlace(CommonPow2Bits);
14666
14667 // Step 2: IdxDiff is now odd, so its inverse mod 2^EltSize exists.
14668 // TODO: There are 2^CommonPow2Bits valid strides; currently we only try
14669 // one, but we could try all candidates to handle more cases.
14670 Stride = ValDiff * APInt(EltSize, IdxDiff).multiplicativeInverse();
14671 if (Stride.isZero())
14672 return std::nullopt;
14673
14674 // Step 3: Adjust Start based on the first defined element's index.
14675 Start -= Stride * FirstIdx;
14676 } else {
14677 // Verify this element matches the sequence.
14678 if (Val != Start + Stride * I)
14679 return std::nullopt;
14680 }
14681 }
14682
14683 // Need at least two defined elements.
14684 if (SecondIdx < 0)
14685 return std::nullopt;
14686
14687 return std::make_pair(Start, Stride);
14688}
14689
14691 // Find the first non-undef value in the shuffle mask.
14692 unsigned i, e;
14693 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
14694 /* search */;
14695
14696 // If all elements are undefined, this shuffle can be considered a splat
14697 // (although it should eventually get simplified away completely).
14698 if (i == e)
14699 return true;
14700
14701 // Make sure all remaining elements are either undef or the same as the first
14702 // non-undef value.
14703 for (int Idx = Mask[i]; i != e; ++i)
14704 if (Mask[i] >= 0 && Mask[i] != Idx)
14705 return false;
14706 return true;
14707}
14708
14709// Returns true if it is a constant integer BuildVector or constant integer,
14710// possibly hidden by a bitcast.
14712 SDValue N, bool AllowOpaques) const {
14714
14715 if (auto *C = dyn_cast<ConstantSDNode>(N))
14716 return AllowOpaques || !C->isOpaque();
14717
14719 return true;
14720
14721 // Treat a GlobalAddress supporting constant offset folding as a
14722 // constant integer.
14723 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14724 if (GA->getOpcode() == ISD::GlobalAddress &&
14725 TLI->isOffsetFoldingLegal(GA))
14726 return true;
14727
14728 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14729 isa<ConstantSDNode>(N.getOperand(0)))
14730 return true;
14731 return false;
14732}
14733
14734// Returns true if it is a constant float BuildVector or constant float.
14737 return true;
14738
14740 return true;
14741
14742 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14743 isa<ConstantFPSDNode>(N.getOperand(0)))
14744 return true;
14745
14746 return false;
14747}
14748
14749std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14750 ConstantSDNode *Const =
14751 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14752 if (!Const)
14753 return std::nullopt;
14754
14755 EVT VT = N->getValueType(0);
14756 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14757 switch (TLI->getBooleanContents(N.getValueType())) {
14759 if (CVal.isOne())
14760 return true;
14761 if (CVal.isZero())
14762 return false;
14763 return std::nullopt;
14765 if (CVal.isAllOnes())
14766 return true;
14767 if (CVal.isZero())
14768 return false;
14769 return std::nullopt;
14771 return CVal[0];
14772 }
14773 llvm_unreachable("Unknown BooleanContent enum");
14774}
14775
14776void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14777 assert(!Node->OperandList && "Node already has operands");
14779 "too many operands to fit into SDNode");
14780 SDUse *Ops = OperandRecycler.allocate(
14781 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14782
14783 bool IsDivergent = false;
14784 for (unsigned I = 0; I != Vals.size(); ++I) {
14785 Ops[I].setUser(Node);
14786 Ops[I].setInitial(Vals[I]);
14787 EVT VT = Ops[I].getValueType();
14788
14789 // Skip Chain. It does not carry divergence.
14790 if (VT != MVT::Other &&
14791 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14792 Ops[I].getNode()->isDivergent()) {
14793 IsDivergent = true;
14794 }
14795 }
14796 Node->NumOperands = Vals.size();
14797 Node->OperandList = Ops;
14798 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14799 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14800 Node->SDNodeBits.IsDivergent = IsDivergent;
14801 }
14802 checkForCycles(Node);
14803}
14804
14807 size_t Limit = SDNode::getMaxNumOperands();
14808 while (Vals.size() > Limit) {
14809 unsigned SliceIdx = Vals.size() - Limit;
14810 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14811 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14812 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14813 Vals.emplace_back(NewTF);
14814 }
14815 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14816}
14817
14819 EVT VT, SDNodeFlags Flags) {
14820 switch (Opcode) {
14821 default:
14822 return SDValue();
14823 case ISD::ADD:
14824 case ISD::OR:
14825 case ISD::XOR:
14826 case ISD::UMAX:
14827 return getConstant(0, DL, VT);
14828 case ISD::MUL:
14829 return getConstant(1, DL, VT);
14830 case ISD::AND:
14831 case ISD::UMIN:
14832 return getAllOnesConstant(DL, VT);
14833 case ISD::SMAX:
14835 case ISD::SMIN:
14837 case ISD::FADD:
14838 // If flags allow, prefer positive zero since it's generally cheaper
14839 // to materialize on most targets.
14840 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14841 case ISD::FMUL:
14842 return getConstantFP(1.0, DL, VT);
14843 case ISD::FMINNUM:
14844 case ISD::FMAXNUM: {
14845 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14846 const fltSemantics &Semantics = VT.getFltSemantics();
14847 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14848 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14849 APFloat::getLargest(Semantics);
14850 if (Opcode == ISD::FMAXNUM)
14851 NeutralAF.changeSign();
14852
14853 return getConstantFP(NeutralAF, DL, VT);
14854 }
14855 case ISD::FMINIMUM:
14856 case ISD::FMAXIMUM: {
14857 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14858 const fltSemantics &Semantics = VT.getFltSemantics();
14859 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14860 : APFloat::getLargest(Semantics);
14861 if (Opcode == ISD::FMAXIMUM)
14862 NeutralAF.changeSign();
14863
14864 return getConstantFP(NeutralAF, DL, VT);
14865 }
14866
14867 }
14868}
14869
14870/// Helper used to make a call to a library function that has one argument of
14871/// pointer type.
14872///
14873/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14874/// used to get or set floating-point state. They have one argument of pointer
14875/// type, which points to the memory region containing bits of the
14876/// floating-point state. The value returned by such function is ignored in the
14877/// created call.
14878///
14879/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14880/// \param Ptr Pointer used to save/load state.
14881/// \param InChain Ingoing token chain.
14882/// \returns Outgoing chain token.
14884 SDValue InChain,
14885 const SDLoc &DLoc) {
14886 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14888 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14889 RTLIB::LibcallImpl LibcallImpl =
14890 Libcalls->getLibcallImpl(static_cast<RTLIB::Libcall>(LibFunc));
14891 if (LibcallImpl == RTLIB::Unsupported)
14892 reportFatalUsageError("emitting call to unsupported libcall");
14893
14894 SDValue Callee =
14895 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout()));
14897 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14898 Libcalls->getLibcallImplCallingConv(LibcallImpl),
14899 Type::getVoidTy(*getContext()), Callee, std::move(Args));
14900 return TLI->LowerCallTo(CLI).second;
14901}
14902
14904 assert(From && To && "Invalid SDNode; empty source SDValue?");
14905 auto I = SDEI.find(From);
14906 if (I == SDEI.end())
14907 return;
14908
14909 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14910 // the iterator, hence the need to make a copy to prevent a use-after-free.
14911 NodeExtraInfo NEI = I->second;
14912 if (LLVM_LIKELY(!NEI.PCSections)) {
14913 // No deep copy required for the types of extra info set.
14914 //
14915 // FIXME: Investigate if other types of extra info also need deep copy. This
14916 // depends on the types of nodes they can be attached to: if some extra info
14917 // is only ever attached to nodes where a replacement To node is always the
14918 // node where later use and propagation of the extra info has the intended
14919 // semantics, no deep copy is required.
14920 SDEI[To] = std::move(NEI);
14921 return;
14922 }
14923
14924 const SDNode *EntrySDN = getEntryNode().getNode();
14925
14926 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14927 // through the replacement of From with To. Otherwise, replacements of a node
14928 // (From) with more complex nodes (To and its operands) may result in lost
14929 // extra info where the root node (To) is insignificant in further propagating
14930 // and using extra info when further lowering to MIR.
14931 //
14932 // In the first step pre-populate the visited set with the nodes reachable
14933 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14934 // DAG that is not new and should be left untouched.
14935 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14936 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14937 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14938 if (MaxDepth == 0) {
14939 // Remember this node in case we need to increase MaxDepth and continue
14940 // populating FromReach from this node.
14941 Leafs.emplace_back(N);
14942 return;
14943 }
14944 if (!FromReach.insert(N).second)
14945 return;
14946 for (const SDValue &Op : N->op_values())
14947 Self(Self, Op.getNode(), MaxDepth - 1);
14948 };
14949
14950 // Copy extra info to To and all its transitive operands (that are new).
14952 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14953 if (FromReach.contains(N))
14954 return true;
14955 if (!Visited.insert(N).second)
14956 return true;
14957 if (EntrySDN == N)
14958 return false;
14959 for (const SDValue &Op : N->op_values()) {
14960 if (N == To && Op.getNode() == EntrySDN) {
14961 // Special case: New node's operand is the entry node; just need to
14962 // copy extra info to new node.
14963 break;
14964 }
14965 if (!Self(Self, Op.getNode()))
14966 return false;
14967 }
14968 // Copy only if entry node was not reached.
14969 SDEI[N] = std::move(NEI);
14970 return true;
14971 };
14972
14973 // We first try with a lower MaxDepth, assuming that the path to common
14974 // operands between From and To is relatively short. This significantly
14975 // improves performance in the common case. The initial MaxDepth is big
14976 // enough to avoid retry in the common case; the last MaxDepth is large
14977 // enough to avoid having to use the fallback below (and protects from
14978 // potential stack exhaustion from recursion).
14979 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14980 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14981 // StartFrom is the previous (or initial) set of leafs reachable at the
14982 // previous maximum depth.
14984 std::swap(StartFrom, Leafs);
14985 for (const SDNode *N : StartFrom)
14986 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14987 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14988 return;
14989 // This should happen very rarely (reached the entry node).
14990 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14991 assert(!Leafs.empty());
14992 }
14993
14994 // This should not happen - but if it did, that means the subgraph reachable
14995 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14996 // could not visit all reachable common operands. Consequently, we were able
14997 // to reach the entry node.
14998 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14999 assert(false && "From subgraph too complex - increase max. MaxDepth?");
15000 // Best-effort fallback if assertions disabled.
15001 SDEI[To] = std::move(NEI);
15002}
15003
15004#ifndef NDEBUG
15005static void checkForCyclesHelper(const SDNode *N,
15008 const llvm::SelectionDAG *DAG) {
15009 // If this node has already been checked, don't check it again.
15010 if (Checked.count(N))
15011 return;
15012
15013 // If a node has already been visited on this depth-first walk, reject it as
15014 // a cycle.
15015 if (!Visited.insert(N).second) {
15016 errs() << "Detected cycle in SelectionDAG\n";
15017 dbgs() << "Offending node:\n";
15018 N->dumprFull(DAG); dbgs() << "\n";
15019 abort();
15020 }
15021
15022 for (const SDValue &Op : N->op_values())
15023 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
15024
15025 Checked.insert(N);
15026 Visited.erase(N);
15027}
15028#endif
15029
15031 const llvm::SelectionDAG *DAG,
15032 bool force) {
15033#ifndef NDEBUG
15034 bool check = force;
15035#ifdef EXPENSIVE_CHECKS
15036 check = true;
15037#endif // EXPENSIVE_CHECKS
15038 if (check) {
15039 assert(N && "Checking nonexistent SDNode");
15042 checkForCyclesHelper(N, visited, checked, DAG);
15043 }
15044#endif // !NDEBUG
15045}
15046
15047void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
15048 checkForCycles(DAG->getRoot().getNode(), DAG, force);
15049}
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)
#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 std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
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:1499
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1236
bool isFinite() const
Definition APFloat.h:1521
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:1512
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:1527
bool isNegZero() const
Definition APFloat.h:1528
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:2011
LLVM_ABI APInt usub_sat(const APInt &RHS) const
Definition APInt.cpp:2095
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1604
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:1421
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1043
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:1555
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1406
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1685
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1400
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:1064
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1527
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:956
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1345
APInt abs() const
Get the absolute value.
Definition APInt.h:1810
LLVM_ABI APInt sadd_sat(const APInt &RHS) const
Definition APInt.cpp:2066
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:1697
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1503
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:1675
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1411
LLVM_ABI APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition APInt.cpp:1185
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:778
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:1654
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1643
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1613
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:2126
LLVM_ABI APInt ushl_sat(const APInt &RHS) const
Definition APInt.cpp:2140
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1072
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:1172
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:1450
unsigned logBase2() const
Definition APInt.h:1776
LLVM_ABI APInt uadd_sat(const APInt &RHS) const
Definition APInt.cpp:2076
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:834
LLVM_ABI APInt multiplicativeInverse() const
Definition APInt.cpp:1305
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1776
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:1016
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition APInt.h:1382
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:880
LLVM_ABI APInt byteSwap() const
Definition APInt.cpp:756
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:1432
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:1403
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:2085
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.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
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()
Definition BitVector.h:411
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition BitVector.h:360
void clear()
clear - Removes all bits from the bitvector.
Definition BitVector.h:354
BitVector & set()
Definition BitVector.h:370
bool none() const
none - Returns true if none of the bits are set.
Definition BitVector.h:207
size_type size() const
size - 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:1065
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:215
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
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:209
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
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:298
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 getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT, SDNodeFlags Flags)
Get the (commutative) neutral element for the given opcode, if it exists.
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 expandVACopy(SDNode *Node)
Expand the specified ISD::VACOPY node as the Legalize pass would.
LLVM_ABI SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI APInt computeVectorKnownZeroElements(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
For each demanded element of a vector, see if it is known to be zero.
LLVM_ABI void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
bool NewNodesMustHaveLegalTypes
When true, additional steps are taken to ensure that getConstant() and similar functions return DAG n...
LLVM_ABI std::pair< EVT, EVT > GetSplitDestVTs(const EVT &VT) const
Compute the VTs needed for the low/hi parts of a type which is split (or expanded) into two not neces...
LLVM_ABI void salvageDebugInfo(SDNode &N)
To be invoked on an SDNode that is slated to be erased.
LLVM_ABI SDNode * MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs, ArrayRef< SDValue > Ops)
This mutates the specified node to have the specified return type, opcode, and operands.
LLVM_ABI std::pair< SDValue, SDValue > UnrollVectorOverflowOp(SDNode *N, unsigned ResNE=0)
Like UnrollVectorOp(), but for the [US](ADD|SUB|MUL)O family of opcodes.
allnodes_const_iterator allnodes_begin() const
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
LLVM_ABI SDValue getGatherVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI SDValue getBitcastedAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts, unsigned Depth=0) const
Test whether V has a splatted value for all the demanded elements.
LLVM_ABI void DeleteNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
LLVM_ABI SDDbgValue * getDbgValueList(DIVariable *Var, DIExpression *Expr, ArrayRef< SDDbgOperand > Locs, ArrayRef< SDNode * > Dependencies, bool IsIndirect, const DebugLoc &DL, unsigned O, bool IsVariadic)
Creates a SDDbgValue node from a list of locations.
LLVM_ABI std::pair< SDValue, SDValue > getStrcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, const CallInst *CI)
Lower a strcpy operation into a target library call and return the resulting chain and call result as...
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build Select's if you just have operands and don't want to check...
LLVM_ABI SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT)
Create negative operation as (SUB 0, Val).
LLVM_ABI std::optional< unsigned > getValidShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has a uniform shift amount that is less than the element bit-width of the shi...
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
LLVM_ABI SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal)
Try to simplify a select/vselect into 1 of its operands or a constant.
LLVM_ABI SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
LLVM_ABI bool isConstantFPBuildVectorOrConstantFP(SDValue N) const
Test whether the given value is a constant FP or similar node.
const DataLayout & getDataLayout() const
LLVM_ABI SDValue 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 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.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr const char * data() const
data - 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:646
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:3241
LLVM_ABI APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition APInt.cpp:3171
LLVM_ABI APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition APInt.cpp:3158
LLVM_ABI APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition APInt.cpp:3148
LLVM_ABI APInt fshr(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift right.
Definition APInt.cpp:3222
LLVM_ABI APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition APInt.cpp:3163
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:3231
APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition APInt.h:2286
LLVM_ABI APInt fshl(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift left.
Definition APInt.cpp:3213
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:3049
LLVM_ABI APInt clmulh(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, and return high-bits.
Definition APInt.cpp:3246
APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition APInt.h:2291
LLVM_ABI APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition APInt.cpp:3143
LLVM_ABI APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition APInt.cpp:3153
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 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)
auto m_Value()
Match an arbitrary value and ignore it.
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
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:532
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:1759
LLVM_ABI SDValue peekThroughExtractSubvectors(SDValue V)
Return the non-extracted vector source operand of V if it exists.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
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:1631
LLVM_ABI SDValue getBitwiseNotOperand(SDValue V, SDValue Mask, bool AllowUndefs)
If V is a bitwise not, returns the inverted operand.
@ Undef
Value of the register doesn't matter.
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:313
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:323
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition APFloat.h:1710
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:634
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:1613
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:1622
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:202
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 maxNum semantics.
Definition APFloat.h:1665
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
LLVM_ABI bool isBitwiseNot(SDValue V, bool AllowUndefs=false)
Returns true if V is a bitwise not operation.
LLVM_ABI SDValue peekThroughInsertVectorElt(SDValue V, const APInt &DemandedElts)
Recursively peek through INSERT_VECTOR_ELT nodes, returning the source vector operand of V,...
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI void checkForCycles(const SelectionDAG *DAG, bool force=false)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition APFloat.h:1696
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:1753
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
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:1646
@ 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:1885
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:1947
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
LLVM_ABI bool isNullFPConstant(SDValue V)
Returns true if V is an FP constant with a value of positive zero.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant (+/-)0.0 floating-point value or a splatted vector thereof (wi...
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition APFloat.h:1683
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1723
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 isNeutralConstant(unsigned Opc, SDNodeFlags Flags, SDValue V, unsigned OperandNo)
Returns true if V is a neutral element of Opc with Flags.
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:872
#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:317
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:271
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:108
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:80
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:127
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:258
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:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:290
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:122
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:99
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:167
KnownBits byteSwap() const
Definition KnownBits.h:547
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:305
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:551
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition KnownBits.h:249
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:178
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
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:363
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:337
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:111
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:241
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:327
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:186
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:202
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:148
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:114
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:342
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
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:378
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:296
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:235
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:173
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.
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a zero.
bool isUnknown() 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)