LLVM 19.0.0git
SelectionDAG.cpp
Go to the documentation of this file.
1//===- SelectionDAG.cpp - Implement the SelectionDAG data structures ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This implements the SelectionDAG class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "SDNodeDbgValue.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/APSInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/Twine.h"
50#include "llvm/IR/Constant.h"
52#include "llvm/IR/Constants.h"
53#include "llvm/IR/DataLayout.h"
55#include "llvm/IR/DebugLoc.h"
57#include "llvm/IR/Function.h"
58#include "llvm/IR/GlobalValue.h"
59#include "llvm/IR/Metadata.h"
60#include "llvm/IR/Type.h"
64#include "llvm/Support/Debug.h"
68#include "llvm/Support/Mutex.h"
74#include <algorithm>
75#include <cassert>
76#include <cstdint>
77#include <cstdlib>
78#include <limits>
79#include <set>
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 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
114}
115
116//===----------------------------------------------------------------------===//
117// ConstantFPSDNode Class
118//===----------------------------------------------------------------------===//
119
120/// isExactlyValue - We don't rely on operator== working on double values, as
121/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
122/// As such, this method can be used to do an exact bit-for-bit comparison of
123/// two floating point values.
125 return getValueAPF().bitwiseIsEqual(V);
126}
127
129 const APFloat& Val) {
130 assert(VT.isFloatingPoint() && "Can only convert between FP types");
131
132 // convert modifies in place, so make a copy.
133 APFloat Val2 = APFloat(Val);
134 bool losesInfo;
137 &losesInfo);
138 return !losesInfo;
139}
140
141//===----------------------------------------------------------------------===//
142// ISD Namespace
143//===----------------------------------------------------------------------===//
144
145bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
146 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
147 unsigned EltSize =
148 N->getValueType(0).getVectorElementType().getSizeInBits();
149 if (auto *Op0 = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
150 SplatVal = Op0->getAPIntValue().trunc(EltSize);
151 return true;
152 }
153 if (auto *Op0 = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) {
154 SplatVal = Op0->getValueAPF().bitcastToAPInt().trunc(EltSize);
155 return true;
156 }
157 }
158
159 auto *BV = dyn_cast<BuildVectorSDNode>(N);
160 if (!BV)
161 return false;
162
163 APInt SplatUndef;
164 unsigned SplatBitSize;
165 bool HasUndefs;
166 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
167 // Endianness does not matter here. We are checking for a splat given the
168 // element size of the vector, and if we find such a splat for little endian
169 // layout, then that should be valid also for big endian (as the full vector
170 // size is known to be a multiple of the element size).
171 const bool IsBigEndian = false;
172 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
173 EltSize, IsBigEndian) &&
174 EltSize == SplatBitSize;
175}
176
177// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
178// specializations of the more general isConstantSplatVector()?
179
180bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
181 // Look through a bit convert.
182 while (N->getOpcode() == ISD::BITCAST)
183 N = N->getOperand(0).getNode();
184
185 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
186 APInt SplatVal;
187 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
188 }
189
190 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
191
192 unsigned i = 0, e = N->getNumOperands();
193
194 // Skip over all of the undef values.
195 while (i != e && N->getOperand(i).isUndef())
196 ++i;
197
198 // Do not accept an all-undef vector.
199 if (i == e) return false;
200
201 // Do not accept build_vectors that aren't all constants or which have non-~0
202 // elements. We have to be a bit careful here, as the type of the constant
203 // may not be the same as the type of the vector elements due to type
204 // legalization (the elements are promoted to a legal type for the target and
205 // a vector of a type may be legal when the base element type is not).
206 // We only want to check enough bits to cover the vector elements, because
207 // we care if the resultant vector is all ones, not whether the individual
208 // constants are.
209 SDValue NotZero = N->getOperand(i);
210 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
211 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
212 if (CN->getAPIntValue().countr_one() < EltSize)
213 return false;
214 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
215 if (CFPN->getValueAPF().bitcastToAPInt().countr_one() < EltSize)
216 return false;
217 } else
218 return false;
219
220 // Okay, we have at least one ~0 value, check to see if the rest match or are
221 // undefs. Even with the above element type twiddling, this should be OK, as
222 // the same type legalization should have applied to all the elements.
223 for (++i; i != e; ++i)
224 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
225 return false;
226 return true;
227}
228
229bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
230 // Look through a bit convert.
231 while (N->getOpcode() == ISD::BITCAST)
232 N = N->getOperand(0).getNode();
233
234 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
235 APInt SplatVal;
236 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
237 }
238
239 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
240
241 bool IsAllUndef = true;
242 for (const SDValue &Op : N->op_values()) {
243 if (Op.isUndef())
244 continue;
245 IsAllUndef = false;
246 // Do not accept build_vectors that aren't all constants or which have non-0
247 // elements. We have to be a bit careful here, as the type of the constant
248 // may not be the same as the type of the vector elements due to type
249 // legalization (the elements are promoted to a legal type for the target
250 // and a vector of a type may be legal when the base element type is not).
251 // We only want to check enough bits to cover the vector elements, because
252 // we care if the resultant vector is all zeros, not whether the individual
253 // constants are.
254 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
255 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
256 if (CN->getAPIntValue().countr_zero() < EltSize)
257 return false;
258 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
259 if (CFPN->getValueAPF().bitcastToAPInt().countr_zero() < EltSize)
260 return false;
261 } else
262 return false;
263 }
264
265 // Do not accept an all-undef vector.
266 if (IsAllUndef)
267 return false;
268 return true;
269}
270
272 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
273}
274
276 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
277}
278
280 if (N->getOpcode() != ISD::BUILD_VECTOR)
281 return false;
282
283 for (const SDValue &Op : N->op_values()) {
284 if (Op.isUndef())
285 continue;
286 if (!isa<ConstantSDNode>(Op))
287 return false;
288 }
289 return true;
290}
291
293 if (N->getOpcode() != ISD::BUILD_VECTOR)
294 return false;
295
296 for (const SDValue &Op : N->op_values()) {
297 if (Op.isUndef())
298 continue;
299 if (!isa<ConstantFPSDNode>(Op))
300 return false;
301 }
302 return true;
303}
304
305bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
306 bool Signed) {
307 assert(N->getValueType(0).isVector() && "Expected a vector!");
308
309 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
310 if (EltSize <= NewEltSize)
311 return false;
312
313 if (N->getOpcode() == ISD::ZERO_EXTEND) {
314 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
315 NewEltSize) &&
316 !Signed;
317 }
318 if (N->getOpcode() == ISD::SIGN_EXTEND) {
319 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
320 NewEltSize) &&
321 Signed;
322 }
323 if (N->getOpcode() != ISD::BUILD_VECTOR)
324 return false;
325
326 for (const SDValue &Op : N->op_values()) {
327 if (Op.isUndef())
328 continue;
329 if (!isa<ConstantSDNode>(Op))
330 return false;
331
332 APInt C = Op->getAsAPIntVal().trunc(EltSize);
333 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
334 return false;
335 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
336 return false;
337 }
338
339 return true;
340}
341
343 // Return false if the node has no operands.
344 // This is "logically inconsistent" with the definition of "all" but
345 // is probably the desired behavior.
346 if (N->getNumOperands() == 0)
347 return false;
348 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
349}
350
352 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
353}
354
355template <typename ConstNodeType>
357 std::function<bool(ConstNodeType *)> Match,
358 bool AllowUndefs) {
359 // FIXME: Add support for scalar UNDEF cases?
360 if (auto *C = dyn_cast<ConstNodeType>(Op))
361 return Match(C);
362
363 // FIXME: Add support for vector UNDEF cases?
364 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
365 ISD::SPLAT_VECTOR != Op.getOpcode())
366 return false;
367
368 EVT SVT = Op.getValueType().getScalarType();
369 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
370 if (AllowUndefs && Op.getOperand(i).isUndef()) {
371 if (!Match(nullptr))
372 return false;
373 continue;
374 }
375
376 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
377 if (!Cst || Cst->getValueType(0) != SVT || !Match(Cst))
378 return false;
379 }
380 return true;
381}
382// Build used template types.
383template bool ISD::matchUnaryPredicateImpl<ConstantSDNode>(
384 SDValue, std::function<bool(ConstantSDNode *)>, bool);
385template bool ISD::matchUnaryPredicateImpl<ConstantFPSDNode>(
386 SDValue, std::function<bool(ConstantFPSDNode *)>, bool);
387
389 SDValue LHS, SDValue RHS,
390 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
391 bool AllowUndefs, bool AllowTypeMismatch) {
392 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
393 return false;
394
395 // TODO: Add support for scalar UNDEF cases?
396 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
397 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
398 return Match(LHSCst, RHSCst);
399
400 // TODO: Add support for vector UNDEF cases?
401 if (LHS.getOpcode() != RHS.getOpcode() ||
402 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
403 LHS.getOpcode() != ISD::SPLAT_VECTOR))
404 return false;
405
406 EVT SVT = LHS.getValueType().getScalarType();
407 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
408 SDValue LHSOp = LHS.getOperand(i);
409 SDValue RHSOp = RHS.getOperand(i);
410 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
411 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
412 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
413 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
414 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
415 return false;
416 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
417 LHSOp.getValueType() != RHSOp.getValueType()))
418 return false;
419 if (!Match(LHSCst, RHSCst))
420 return false;
421 }
422 return true;
423}
424
426 switch (VecReduceOpcode) {
427 default:
428 llvm_unreachable("Expected VECREDUCE opcode");
431 case ISD::VP_REDUCE_FADD:
432 case ISD::VP_REDUCE_SEQ_FADD:
433 return ISD::FADD;
436 case ISD::VP_REDUCE_FMUL:
437 case ISD::VP_REDUCE_SEQ_FMUL:
438 return ISD::FMUL;
440 case ISD::VP_REDUCE_ADD:
441 return ISD::ADD;
443 case ISD::VP_REDUCE_MUL:
444 return ISD::MUL;
446 case ISD::VP_REDUCE_AND:
447 return ISD::AND;
449 case ISD::VP_REDUCE_OR:
450 return ISD::OR;
452 case ISD::VP_REDUCE_XOR:
453 return ISD::XOR;
455 case ISD::VP_REDUCE_SMAX:
456 return ISD::SMAX;
458 case ISD::VP_REDUCE_SMIN:
459 return ISD::SMIN;
461 case ISD::VP_REDUCE_UMAX:
462 return ISD::UMAX;
464 case ISD::VP_REDUCE_UMIN:
465 return ISD::UMIN;
467 case ISD::VP_REDUCE_FMAX:
468 return ISD::FMAXNUM;
470 case ISD::VP_REDUCE_FMIN:
471 return ISD::FMINNUM;
473 return ISD::FMAXIMUM;
475 return ISD::FMINIMUM;
476 }
477}
478
479bool ISD::isVPOpcode(unsigned Opcode) {
480 switch (Opcode) {
481 default:
482 return false;
483#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
484 case ISD::VPSD: \
485 return true;
486#include "llvm/IR/VPIntrinsics.def"
487 }
488}
489
490bool ISD::isVPBinaryOp(unsigned Opcode) {
491 switch (Opcode) {
492 default:
493 break;
494#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
495#define VP_PROPERTY_BINARYOP return true;
496#define END_REGISTER_VP_SDNODE(VPSD) break;
497#include "llvm/IR/VPIntrinsics.def"
498 }
499 return false;
500}
501
502bool ISD::isVPReduction(unsigned Opcode) {
503 switch (Opcode) {
504 default:
505 break;
506#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
507#define VP_PROPERTY_REDUCTION(STARTPOS, ...) return true;
508#define END_REGISTER_VP_SDNODE(VPSD) break;
509#include "llvm/IR/VPIntrinsics.def"
510 }
511 return false;
512}
513
514/// The operand position of the vector mask.
515std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
516 switch (Opcode) {
517 default:
518 return std::nullopt;
519#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
520 case ISD::VPSD: \
521 return MASKPOS;
522#include "llvm/IR/VPIntrinsics.def"
523 }
524}
525
526/// The operand position of the explicit vector length parameter.
527std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
528 switch (Opcode) {
529 default:
530 return std::nullopt;
531#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
532 case ISD::VPSD: \
533 return EVLPOS;
534#include "llvm/IR/VPIntrinsics.def"
535 }
536}
537
538std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
539 bool hasFPExcept) {
540 // FIXME: Return strict opcodes in case of fp exceptions.
541 switch (VPOpcode) {
542 default:
543 return std::nullopt;
544#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
545#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
546#define END_REGISTER_VP_SDNODE(VPOPC) break;
547#include "llvm/IR/VPIntrinsics.def"
548 }
549 return std::nullopt;
550}
551
552unsigned ISD::getVPForBaseOpcode(unsigned Opcode) {
553 switch (Opcode) {
554 default:
555 llvm_unreachable("can not translate this Opcode to VP.");
556#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
557#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
558#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
559#include "llvm/IR/VPIntrinsics.def"
560 }
561}
562
564 switch (ExtType) {
565 case ISD::EXTLOAD:
566 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
567 case ISD::SEXTLOAD:
568 return ISD::SIGN_EXTEND;
569 case ISD::ZEXTLOAD:
570 return ISD::ZERO_EXTEND;
571 default:
572 break;
573 }
574
575 llvm_unreachable("Invalid LoadExtType");
576}
577
579 // To perform this operation, we just need to swap the L and G bits of the
580 // operation.
581 unsigned OldL = (Operation >> 2) & 1;
582 unsigned OldG = (Operation >> 1) & 1;
583 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
584 (OldL << 1) | // New G bit
585 (OldG << 2)); // New L bit.
586}
587
589 unsigned Operation = Op;
590 if (isIntegerLike)
591 Operation ^= 7; // Flip L, G, E bits, but not U.
592 else
593 Operation ^= 15; // Flip all of the condition bits.
594
596 Operation &= ~8; // Don't let N and U bits get set.
597
598 return ISD::CondCode(Operation);
599}
600
602 return getSetCCInverseImpl(Op, Type.isInteger());
603}
604
606 bool isIntegerLike) {
607 return getSetCCInverseImpl(Op, isIntegerLike);
608}
609
610/// For an integer comparison, return 1 if the comparison is a signed operation
611/// and 2 if the result is an unsigned comparison. Return zero if the operation
612/// does not depend on the sign of the input (setne and seteq).
613static int isSignedOp(ISD::CondCode Opcode) {
614 switch (Opcode) {
615 default: llvm_unreachable("Illegal integer setcc operation!");
616 case ISD::SETEQ:
617 case ISD::SETNE: return 0;
618 case ISD::SETLT:
619 case ISD::SETLE:
620 case ISD::SETGT:
621 case ISD::SETGE: return 1;
622 case ISD::SETULT:
623 case ISD::SETULE:
624 case ISD::SETUGT:
625 case ISD::SETUGE: return 2;
626 }
627}
628
630 EVT Type) {
631 bool IsInteger = Type.isInteger();
632 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
633 // Cannot fold a signed integer setcc with an unsigned integer setcc.
634 return ISD::SETCC_INVALID;
635
636 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
637
638 // If the N and U bits get set, then the resultant comparison DOES suddenly
639 // care about orderedness, and it is true when ordered.
640 if (Op > ISD::SETTRUE2)
641 Op &= ~16; // Clear the U bit if the N bit is set.
642
643 // Canonicalize illegal integer setcc's.
644 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
645 Op = ISD::SETNE;
646
647 return ISD::CondCode(Op);
648}
649
651 EVT Type) {
652 bool IsInteger = Type.isInteger();
653 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
654 // Cannot fold a signed setcc with an unsigned setcc.
655 return ISD::SETCC_INVALID;
656
657 // Combine all of the condition bits.
658 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
659
660 // Canonicalize illegal integer setcc's.
661 if (IsInteger) {
662 switch (Result) {
663 default: break;
664 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
665 case ISD::SETOEQ: // SETEQ & SETU[LG]E
666 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
667 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
668 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
669 }
670 }
671
672 return Result;
673}
674
675//===----------------------------------------------------------------------===//
676// SDNode Profile Support
677//===----------------------------------------------------------------------===//
678
679/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
680static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
681 ID.AddInteger(OpC);
682}
683
684/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
685/// solely with their pointer.
687 ID.AddPointer(VTList.VTs);
688}
689
690/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
692 ArrayRef<SDValue> Ops) {
693 for (const auto &Op : Ops) {
694 ID.AddPointer(Op.getNode());
695 ID.AddInteger(Op.getResNo());
696 }
697}
698
699/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
701 ArrayRef<SDUse> Ops) {
702 for (const auto &Op : Ops) {
703 ID.AddPointer(Op.getNode());
704 ID.AddInteger(Op.getResNo());
705 }
706}
707
708static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
709 SDVTList VTList, ArrayRef<SDValue> OpList) {
710 AddNodeIDOpcode(ID, OpC);
711 AddNodeIDValueTypes(ID, VTList);
712 AddNodeIDOperands(ID, OpList);
713}
714
715/// If this is an SDNode with special info, add this info to the NodeID data.
717 switch (N->getOpcode()) {
720 case ISD::MCSymbol:
721 llvm_unreachable("Should only be used on nodes with operands");
722 default: break; // Normal nodes don't need extra info.
724 case ISD::Constant: {
725 const ConstantSDNode *C = cast<ConstantSDNode>(N);
726 ID.AddPointer(C->getConstantIntValue());
727 ID.AddBoolean(C->isOpaque());
728 break;
729 }
731 case ISD::ConstantFP:
732 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
733 break;
738 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
739 ID.AddPointer(GA->getGlobal());
740 ID.AddInteger(GA->getOffset());
741 ID.AddInteger(GA->getTargetFlags());
742 break;
743 }
744 case ISD::BasicBlock:
745 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
746 break;
747 case ISD::Register:
748 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
749 break;
751 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
752 break;
753 case ISD::SRCVALUE:
754 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
755 break;
756 case ISD::FrameIndex:
758 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
759 break;
762 if (cast<LifetimeSDNode>(N)->hasOffset()) {
763 ID.AddInteger(cast<LifetimeSDNode>(N)->getSize());
764 ID.AddInteger(cast<LifetimeSDNode>(N)->getOffset());
765 }
766 break;
768 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
769 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
770 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
771 break;
772 case ISD::JumpTable:
774 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
775 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
776 break;
779 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
780 ID.AddInteger(CP->getAlign().value());
781 ID.AddInteger(CP->getOffset());
782 if (CP->isMachineConstantPoolEntry())
783 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
784 else
785 ID.AddPointer(CP->getConstVal());
786 ID.AddInteger(CP->getTargetFlags());
787 break;
788 }
789 case ISD::TargetIndex: {
790 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
791 ID.AddInteger(TI->getIndex());
792 ID.AddInteger(TI->getOffset());
793 ID.AddInteger(TI->getTargetFlags());
794 break;
795 }
796 case ISD::LOAD: {
797 const LoadSDNode *LD = cast<LoadSDNode>(N);
798 ID.AddInteger(LD->getMemoryVT().getRawBits());
799 ID.AddInteger(LD->getRawSubclassData());
800 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
801 ID.AddInteger(LD->getMemOperand()->getFlags());
802 break;
803 }
804 case ISD::STORE: {
805 const StoreSDNode *ST = cast<StoreSDNode>(N);
806 ID.AddInteger(ST->getMemoryVT().getRawBits());
807 ID.AddInteger(ST->getRawSubclassData());
808 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
809 ID.AddInteger(ST->getMemOperand()->getFlags());
810 break;
811 }
812 case ISD::VP_LOAD: {
813 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
814 ID.AddInteger(ELD->getMemoryVT().getRawBits());
815 ID.AddInteger(ELD->getRawSubclassData());
816 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
817 ID.AddInteger(ELD->getMemOperand()->getFlags());
818 break;
819 }
820 case ISD::VP_STORE: {
821 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
822 ID.AddInteger(EST->getMemoryVT().getRawBits());
823 ID.AddInteger(EST->getRawSubclassData());
824 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
825 ID.AddInteger(EST->getMemOperand()->getFlags());
826 break;
827 }
828 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
829 const VPStridedLoadSDNode *SLD = cast<VPStridedLoadSDNode>(N);
830 ID.AddInteger(SLD->getMemoryVT().getRawBits());
831 ID.AddInteger(SLD->getRawSubclassData());
832 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
833 break;
834 }
835 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
836 const VPStridedStoreSDNode *SST = cast<VPStridedStoreSDNode>(N);
837 ID.AddInteger(SST->getMemoryVT().getRawBits());
838 ID.AddInteger(SST->getRawSubclassData());
839 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
840 break;
841 }
842 case ISD::VP_GATHER: {
843 const VPGatherSDNode *EG = cast<VPGatherSDNode>(N);
844 ID.AddInteger(EG->getMemoryVT().getRawBits());
845 ID.AddInteger(EG->getRawSubclassData());
846 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
847 ID.AddInteger(EG->getMemOperand()->getFlags());
848 break;
849 }
850 case ISD::VP_SCATTER: {
851 const VPScatterSDNode *ES = cast<VPScatterSDNode>(N);
852 ID.AddInteger(ES->getMemoryVT().getRawBits());
853 ID.AddInteger(ES->getRawSubclassData());
854 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
855 ID.AddInteger(ES->getMemOperand()->getFlags());
856 break;
857 }
858 case ISD::MLOAD: {
859 const MaskedLoadSDNode *MLD = cast<MaskedLoadSDNode>(N);
860 ID.AddInteger(MLD->getMemoryVT().getRawBits());
861 ID.AddInteger(MLD->getRawSubclassData());
862 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
863 ID.AddInteger(MLD->getMemOperand()->getFlags());
864 break;
865 }
866 case ISD::MSTORE: {
867 const MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
868 ID.AddInteger(MST->getMemoryVT().getRawBits());
869 ID.AddInteger(MST->getRawSubclassData());
870 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
871 ID.AddInteger(MST->getMemOperand()->getFlags());
872 break;
873 }
874 case ISD::MGATHER: {
875 const MaskedGatherSDNode *MG = cast<MaskedGatherSDNode>(N);
876 ID.AddInteger(MG->getMemoryVT().getRawBits());
877 ID.AddInteger(MG->getRawSubclassData());
878 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
879 ID.AddInteger(MG->getMemOperand()->getFlags());
880 break;
881 }
882 case ISD::MSCATTER: {
883 const MaskedScatterSDNode *MS = cast<MaskedScatterSDNode>(N);
884 ID.AddInteger(MS->getMemoryVT().getRawBits());
885 ID.AddInteger(MS->getRawSubclassData());
886 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
887 ID.AddInteger(MS->getMemOperand()->getFlags());
888 break;
889 }
892 case ISD::ATOMIC_SWAP:
904 case ISD::ATOMIC_LOAD:
905 case ISD::ATOMIC_STORE: {
906 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
907 ID.AddInteger(AT->getMemoryVT().getRawBits());
908 ID.AddInteger(AT->getRawSubclassData());
909 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
910 ID.AddInteger(AT->getMemOperand()->getFlags());
911 break;
912 }
913 case ISD::VECTOR_SHUFFLE: {
914 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
915 for (int M : Mask)
916 ID.AddInteger(M);
917 break;
918 }
920 case ISD::BlockAddress: {
921 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
922 ID.AddPointer(BA->getBlockAddress());
923 ID.AddInteger(BA->getOffset());
924 ID.AddInteger(BA->getTargetFlags());
925 break;
926 }
927 case ISD::AssertAlign:
928 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
929 break;
930 case ISD::PREFETCH:
933 // Handled by MemIntrinsicSDNode check after the switch.
934 break;
935 } // end switch (N->getOpcode())
936
937 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
938 // to check.
939 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
940 ID.AddInteger(MN->getRawSubclassData());
941 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
942 ID.AddInteger(MN->getMemOperand()->getFlags());
943 ID.AddInteger(MN->getMemoryVT().getRawBits());
944 }
945}
946
947/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
948/// data.
949static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
950 AddNodeIDOpcode(ID, N->getOpcode());
951 // Add the return value info.
952 AddNodeIDValueTypes(ID, N->getVTList());
953 // Add the operand info.
954 AddNodeIDOperands(ID, N->ops());
955
956 // Handle SDNode leafs with special info.
958}
959
960//===----------------------------------------------------------------------===//
961// SelectionDAG Class
962//===----------------------------------------------------------------------===//
963
964/// doNotCSE - Return true if CSE should not be performed for this node.
965static bool doNotCSE(SDNode *N) {
966 if (N->getValueType(0) == MVT::Glue)
967 return true; // Never CSE anything that produces a glue result.
968
969 switch (N->getOpcode()) {
970 default: break;
971 case ISD::HANDLENODE:
972 case ISD::EH_LABEL:
973 return true; // Never CSE these nodes.
974 }
975
976 // Check that remaining values produced are not flags.
977 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
978 if (N->getValueType(i) == MVT::Glue)
979 return true; // Never CSE anything that produces a glue result.
980
981 return false;
982}
983
984/// RemoveDeadNodes - This method deletes all unreachable nodes in the
985/// SelectionDAG.
987 // Create a dummy node (which is not added to allnodes), that adds a reference
988 // to the root node, preventing it from being deleted.
989 HandleSDNode Dummy(getRoot());
990
992
993 // Add all obviously-dead nodes to the DeadNodes worklist.
994 for (SDNode &Node : allnodes())
995 if (Node.use_empty())
996 DeadNodes.push_back(&Node);
997
998 RemoveDeadNodes(DeadNodes);
999
1000 // If the root changed (e.g. it was a dead load, update the root).
1001 setRoot(Dummy.getValue());
1002}
1003
1004/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1005/// given list, and any nodes that become unreachable as a result.
1007
1008 // Process the worklist, deleting the nodes and adding their uses to the
1009 // worklist.
1010 while (!DeadNodes.empty()) {
1011 SDNode *N = DeadNodes.pop_back_val();
1012 // Skip to next node if we've already managed to delete the node. This could
1013 // happen if replacing a node causes a node previously added to the node to
1014 // be deleted.
1015 if (N->getOpcode() == ISD::DELETED_NODE)
1016 continue;
1017
1018 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1019 DUL->NodeDeleted(N, nullptr);
1020
1021 // Take the node out of the appropriate CSE map.
1022 RemoveNodeFromCSEMaps(N);
1023
1024 // Next, brutally remove the operand list. This is safe to do, as there are
1025 // no cycles in the graph.
1026 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1027 SDUse &Use = *I++;
1028 SDNode *Operand = Use.getNode();
1029 Use.set(SDValue());
1030
1031 // Now that we removed this operand, see if there are no uses of it left.
1032 if (Operand->use_empty())
1033 DeadNodes.push_back(Operand);
1034 }
1035
1036 DeallocateNode(N);
1037 }
1038}
1039
1041 SmallVector<SDNode*, 16> DeadNodes(1, N);
1042
1043 // Create a dummy node that adds a reference to the root node, preventing
1044 // it from being deleted. (This matters if the root is an operand of the
1045 // dead node.)
1046 HandleSDNode Dummy(getRoot());
1047
1048 RemoveDeadNodes(DeadNodes);
1049}
1050
1052 // First take this out of the appropriate CSE map.
1053 RemoveNodeFromCSEMaps(N);
1054
1055 // Finally, remove uses due to operands of this node, remove from the
1056 // AllNodes list, and delete the node.
1057 DeleteNodeNotInCSEMaps(N);
1058}
1059
1060void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1061 assert(N->getIterator() != AllNodes.begin() &&
1062 "Cannot delete the entry node!");
1063 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1064
1065 // Drop all of the operands and decrement used node's use counts.
1066 N->DropOperands();
1067
1068 DeallocateNode(N);
1069}
1070
1071void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1072 assert(!(V->isVariadic() && isParameter));
1073 if (isParameter)
1074 ByvalParmDbgValues.push_back(V);
1075 else
1076 DbgValues.push_back(V);
1077 for (const SDNode *Node : V->getSDNodes())
1078 if (Node)
1079 DbgValMap[Node].push_back(V);
1080}
1081
1082void SDDbgInfo::erase(const SDNode *Node) {
1083 DbgValMapType::iterator I = DbgValMap.find(Node);
1084 if (I == DbgValMap.end())
1085 return;
1086 for (auto &Val: I->second)
1087 Val->setIsInvalidated();
1088 DbgValMap.erase(I);
1089}
1090
1091void SelectionDAG::DeallocateNode(SDNode *N) {
1092 // If we have operands, deallocate them.
1093 removeOperands(N);
1094
1095 NodeAllocator.Deallocate(AllNodes.remove(N));
1096
1097 // Set the opcode to DELETED_NODE to help catch bugs when node
1098 // memory is reallocated.
1099 // FIXME: There are places in SDag that have grown a dependency on the opcode
1100 // value in the released node.
1101 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1102 N->NodeType = ISD::DELETED_NODE;
1103
1104 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1105 // them and forget about that node.
1106 DbgInfo->erase(N);
1107
1108 // Invalidate extra info.
1109 SDEI.erase(N);
1110}
1111
1112#ifndef NDEBUG
1113/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1114static void VerifySDNode(SDNode *N, const TargetLowering *TLI) {
1115 switch (N->getOpcode()) {
1116 default:
1117 if (N->getOpcode() > ISD::BUILTIN_OP_END)
1118 TLI->verifyTargetSDNode(N);
1119 break;
1120 case ISD::BUILD_PAIR: {
1121 EVT VT = N->getValueType(0);
1122 assert(N->getNumValues() == 1 && "Too many results!");
1123 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1124 "Wrong return type!");
1125 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1126 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1127 "Mismatched operand types!");
1128 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1129 "Wrong operand type!");
1130 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1131 "Wrong return type size");
1132 break;
1133 }
1134 case ISD::BUILD_VECTOR: {
1135 assert(N->getNumValues() == 1 && "Too many results!");
1136 assert(N->getValueType(0).isVector() && "Wrong return type!");
1137 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1138 "Wrong number of operands!");
1139 EVT EltVT = N->getValueType(0).getVectorElementType();
1140 for (const SDUse &Op : N->ops()) {
1141 assert((Op.getValueType() == EltVT ||
1142 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1143 EltVT.bitsLE(Op.getValueType()))) &&
1144 "Wrong operand type!");
1145 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1146 "Operands must all have the same type");
1147 }
1148 break;
1149 }
1150 }
1151}
1152#endif // NDEBUG
1153
1154/// Insert a newly allocated node into the DAG.
1155///
1156/// Handles insertion into the all nodes list and CSE map, as well as
1157/// verification and other common operations when a new node is allocated.
1158void SelectionDAG::InsertNode(SDNode *N) {
1159 AllNodes.push_back(N);
1160#ifndef NDEBUG
1161 N->PersistentId = NextPersistentId++;
1162 VerifySDNode(N, TLI);
1163#endif
1164 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1165 DUL->NodeInserted(N);
1166}
1167
1168/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1169/// correspond to it. This is useful when we're about to delete or repurpose
1170/// the node. We don't want future request for structurally identical nodes
1171/// to return N anymore.
1172bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1173 bool Erased = false;
1174 switch (N->getOpcode()) {
1175 case ISD::HANDLENODE: return false; // noop.
1176 case ISD::CONDCODE:
1177 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1178 "Cond code doesn't exist!");
1179 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1180 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1181 break;
1183 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1184 break;
1186 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1187 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1188 ESN->getSymbol(), ESN->getTargetFlags()));
1189 break;
1190 }
1191 case ISD::MCSymbol: {
1192 auto *MCSN = cast<MCSymbolSDNode>(N);
1193 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1194 break;
1195 }
1196 case ISD::VALUETYPE: {
1197 EVT VT = cast<VTSDNode>(N)->getVT();
1198 if (VT.isExtended()) {
1199 Erased = ExtendedValueTypeNodes.erase(VT);
1200 } else {
1201 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1202 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1203 }
1204 break;
1205 }
1206 default:
1207 // Remove it from the CSE Map.
1208 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1209 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1210 Erased = CSEMap.RemoveNode(N);
1211 break;
1212 }
1213#ifndef NDEBUG
1214 // Verify that the node was actually in one of the CSE maps, unless it has a
1215 // glue result (which cannot be CSE'd) or is one of the special cases that are
1216 // not subject to CSE.
1217 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1218 !N->isMachineOpcode() && !doNotCSE(N)) {
1219 N->dump(this);
1220 dbgs() << "\n";
1221 llvm_unreachable("Node is not in map!");
1222 }
1223#endif
1224 return Erased;
1225}
1226
1227/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1228/// maps and modified in place. Add it back to the CSE maps, unless an identical
1229/// node already exists, in which case transfer all its users to the existing
1230/// node. This transfer can potentially trigger recursive merging.
1231void
1232SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1233 // For node types that aren't CSE'd, just act as if no identical node
1234 // already exists.
1235 if (!doNotCSE(N)) {
1236 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1237 if (Existing != N) {
1238 // If there was already an existing matching node, use ReplaceAllUsesWith
1239 // to replace the dead one with the existing one. This can cause
1240 // recursive merging of other unrelated nodes down the line.
1241 ReplaceAllUsesWith(N, Existing);
1242
1243 // N is now dead. Inform the listeners and delete it.
1244 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1245 DUL->NodeDeleted(N, Existing);
1246 DeleteNodeNotInCSEMaps(N);
1247 return;
1248 }
1249 }
1250
1251 // If the node doesn't already exist, we updated it. Inform listeners.
1252 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1253 DUL->NodeUpdated(N);
1254}
1255
1256/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1257/// were replaced with those specified. If this node is never memoized,
1258/// return null, otherwise return a pointer to the slot it would take. If a
1259/// node already exists with these operands, the slot will be non-null.
1260SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1261 void *&InsertPos) {
1262 if (doNotCSE(N))
1263 return nullptr;
1264
1265 SDValue Ops[] = { Op };
1267 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1269 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1270 if (Node)
1271 Node->intersectFlagsWith(N->getFlags());
1272 return Node;
1273}
1274
1275/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1276/// were replaced with those specified. If this node is never memoized,
1277/// return null, otherwise return a pointer to the slot it would take. If a
1278/// node already exists with these operands, the slot will be non-null.
1279SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1280 SDValue Op1, SDValue Op2,
1281 void *&InsertPos) {
1282 if (doNotCSE(N))
1283 return nullptr;
1284
1285 SDValue Ops[] = { Op1, Op2 };
1287 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1289 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1290 if (Node)
1291 Node->intersectFlagsWith(N->getFlags());
1292 return Node;
1293}
1294
1295/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1296/// were replaced with those specified. If this node is never memoized,
1297/// return null, otherwise return a pointer to the slot it would take. If a
1298/// node already exists with these operands, the slot will be non-null.
1299SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1300 void *&InsertPos) {
1301 if (doNotCSE(N))
1302 return nullptr;
1303
1305 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1307 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1308 if (Node)
1309 Node->intersectFlagsWith(N->getFlags());
1310 return Node;
1311}
1312
1314 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1315 : VT.getTypeForEVT(*getContext());
1316
1317 return getDataLayout().getABITypeAlign(Ty);
1318}
1319
1320// EntryNode could meaningfully have debug info if we can find it...
1322 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1323 getVTList(MVT::Other, MVT::Glue)),
1324 Root(getEntryNode()) {
1325 InsertNode(&EntryNode);
1326 DbgInfo = new SDDbgInfo();
1327}
1328
1330 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1331 const TargetLibraryInfo *LibraryInfo,
1332 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1333 BlockFrequencyInfo *BFIin,
1334 FunctionVarLocs const *VarLocs) {
1335 MF = &NewMF;
1336 SDAGISelPass = PassPtr;
1337 ORE = &NewORE;
1340 LibInfo = LibraryInfo;
1341 Context = &MF->getFunction().getContext();
1342 UA = NewUA;
1343 PSI = PSIin;
1344 BFI = BFIin;
1345 FnVarLocs = VarLocs;
1346}
1347
1349 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1350 allnodes_clear();
1351 OperandRecycler.clear(OperandAllocator);
1352 delete DbgInfo;
1353}
1354
1356 return MF->getFunction().hasOptSize() ||
1358}
1359
1360void SelectionDAG::allnodes_clear() {
1361 assert(&*AllNodes.begin() == &EntryNode);
1362 AllNodes.remove(AllNodes.begin());
1363 while (!AllNodes.empty())
1364 DeallocateNode(&AllNodes.front());
1365#ifndef NDEBUG
1366 NextPersistentId = 0;
1367#endif
1368}
1369
1370SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1371 void *&InsertPos) {
1372 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1373 if (N) {
1374 switch (N->getOpcode()) {
1375 default: break;
1376 case ISD::Constant:
1377 case ISD::ConstantFP:
1378 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1379 "debug location. Use another overload.");
1380 }
1381 }
1382 return N;
1383}
1384
1385SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1386 const SDLoc &DL, void *&InsertPos) {
1387 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1388 if (N) {
1389 switch (N->getOpcode()) {
1390 case ISD::Constant:
1391 case ISD::ConstantFP:
1392 // Erase debug location from the node if the node is used at several
1393 // different places. Do not propagate one location to all uses as it
1394 // will cause a worse single stepping debugging experience.
1395 if (N->getDebugLoc() != DL.getDebugLoc())
1396 N->setDebugLoc(DebugLoc());
1397 break;
1398 default:
1399 // When the node's point of use is located earlier in the instruction
1400 // sequence than its prior point of use, update its debug info to the
1401 // earlier location.
1402 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1403 N->setDebugLoc(DL.getDebugLoc());
1404 break;
1405 }
1406 }
1407 return N;
1408}
1409
1411 allnodes_clear();
1412 OperandRecycler.clear(OperandAllocator);
1413 OperandAllocator.Reset();
1414 CSEMap.clear();
1415
1416 ExtendedValueTypeNodes.clear();
1417 ExternalSymbols.clear();
1418 TargetExternalSymbols.clear();
1419 MCSymbols.clear();
1420 SDEI.clear();
1421 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
1422 static_cast<CondCodeSDNode*>(nullptr));
1423 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
1424 static_cast<SDNode*>(nullptr));
1425
1426 EntryNode.UseList = nullptr;
1427 InsertNode(&EntryNode);
1428 Root = getEntryNode();
1429 DbgInfo->clear();
1430}
1431
1433 return VT.bitsGT(Op.getValueType())
1434 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1435 : getNode(ISD::FP_ROUND, DL, VT, Op,
1436 getIntPtrConstant(0, DL, /*isTarget=*/true));
1437}
1438
1439std::pair<SDValue, SDValue>
1441 const SDLoc &DL, EVT VT) {
1442 assert(!VT.bitsEq(Op.getValueType()) &&
1443 "Strict no-op FP extend/round not allowed.");
1444 SDValue Res =
1445 VT.bitsGT(Op.getValueType())
1446 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1447 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1448 {Chain, Op, getIntPtrConstant(0, DL)});
1449
1450 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1451}
1452
1454 return VT.bitsGT(Op.getValueType()) ?
1455 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1456 getNode(ISD::TRUNCATE, DL, VT, Op);
1457}
1458
1460 return VT.bitsGT(Op.getValueType()) ?
1461 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1462 getNode(ISD::TRUNCATE, DL, VT, Op);
1463}
1464
1466 return VT.bitsGT(Op.getValueType()) ?
1467 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1468 getNode(ISD::TRUNCATE, DL, VT, Op);
1469}
1470
1472 EVT VT) {
1473 assert(!VT.isVector());
1474 auto Type = Op.getValueType();
1475 SDValue DestOp;
1476 if (Type == VT)
1477 return Op;
1478 auto Size = Op.getValueSizeInBits();
1479 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1480 if (DestOp.getValueType() == VT)
1481 return DestOp;
1482
1483 return getAnyExtOrTrunc(DestOp, DL, VT);
1484}
1485
1487 EVT VT) {
1488 assert(!VT.isVector());
1489 auto Type = Op.getValueType();
1490 SDValue DestOp;
1491 if (Type == VT)
1492 return Op;
1493 auto Size = Op.getValueSizeInBits();
1494 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1495 if (DestOp.getValueType() == VT)
1496 return DestOp;
1497
1498 return getSExtOrTrunc(DestOp, DL, VT);
1499}
1500
1502 EVT VT) {
1503 assert(!VT.isVector());
1504 auto Type = Op.getValueType();
1505 SDValue DestOp;
1506 if (Type == VT)
1507 return Op;
1508 auto Size = Op.getValueSizeInBits();
1509 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1510 if (DestOp.getValueType() == VT)
1511 return DestOp;
1512
1513 return getZExtOrTrunc(DestOp, DL, VT);
1514}
1515
1517 EVT OpVT) {
1518 if (VT.bitsLE(Op.getValueType()))
1519 return getNode(ISD::TRUNCATE, SL, VT, Op);
1520
1522 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1523}
1524
1526 EVT OpVT = Op.getValueType();
1527 assert(VT.isInteger() && OpVT.isInteger() &&
1528 "Cannot getZeroExtendInReg FP types");
1529 assert(VT.isVector() == OpVT.isVector() &&
1530 "getZeroExtendInReg type should be vector iff the operand "
1531 "type is vector!");
1532 assert((!VT.isVector() ||
1534 "Vector element counts must match in getZeroExtendInReg");
1535 assert(VT.bitsLE(OpVT) && "Not extending!");
1536 if (OpVT == VT)
1537 return Op;
1539 VT.getScalarSizeInBits());
1540 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1541}
1542
1544 // Only unsigned pointer semantics are supported right now. In the future this
1545 // might delegate to TLI to check pointer signedness.
1546 return getZExtOrTrunc(Op, DL, VT);
1547}
1548
1550 // Only unsigned pointer semantics are supported right now. In the future this
1551 // might delegate to TLI to check pointer signedness.
1552 return getZeroExtendInReg(Op, DL, VT);
1553}
1554
1556 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1557}
1558
1559/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1561 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1562}
1563
1565 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1566 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1567}
1568
1570 SDValue Mask, SDValue EVL, EVT VT) {
1571 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1572 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1573}
1574
1576 SDValue Mask, SDValue EVL) {
1577 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1578}
1579
1581 SDValue Mask, SDValue EVL) {
1582 if (VT.bitsGT(Op.getValueType()))
1583 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1584 if (VT.bitsLT(Op.getValueType()))
1585 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1586 return Op;
1587}
1588
1590 EVT OpVT) {
1591 if (!V)
1592 return getConstant(0, DL, VT);
1593
1594 switch (TLI->getBooleanContents(OpVT)) {
1597 return getConstant(1, DL, VT);
1599 return getAllOnesConstant(DL, VT);
1600 }
1601 llvm_unreachable("Unexpected boolean content enum!");
1602}
1603
1605 bool isT, bool isO) {
1606 EVT EltVT = VT.getScalarType();
1607 assert((EltVT.getSizeInBits() >= 64 ||
1608 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1609 "getConstant with a uint64_t value that doesn't fit in the type!");
1610 return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
1611}
1612
1614 bool isT, bool isO) {
1615 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1616}
1617
1619 EVT VT, bool isT, bool isO) {
1620 assert(VT.isInteger() && "Cannot create FP integer constant!");
1621
1622 EVT EltVT = VT.getScalarType();
1623 const ConstantInt *Elt = &Val;
1624
1625 // In some cases the vector type is legal but the element type is illegal and
1626 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1627 // inserted value (the type does not need to match the vector element type).
1628 // Any extra bits introduced will be truncated away.
1629 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1631 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1632 APInt NewVal;
1633 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1634 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1635 else
1636 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1637 Elt = ConstantInt::get(*getContext(), NewVal);
1638 }
1639 // In other cases the element type is illegal and needs to be expanded, for
1640 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1641 // the value into n parts and use a vector type with n-times the elements.
1642 // Then bitcast to the type requested.
1643 // Legalizing constants too early makes the DAGCombiner's job harder so we
1644 // only legalize if the DAG tells us we must produce legal types.
1645 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1646 TLI->getTypeAction(*getContext(), EltVT) ==
1648 const APInt &NewVal = Elt->getValue();
1649 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1650 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1651
1652 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1653 if (VT.isScalableVector() ||
1655 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1656 "Can only handle an even split!");
1657 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1658
1659 SmallVector<SDValue, 2> ScalarParts;
1660 for (unsigned i = 0; i != Parts; ++i)
1661 ScalarParts.push_back(getConstant(
1662 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1663 ViaEltVT, isT, isO));
1664
1665 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1666 }
1667
1668 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1669 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1670
1671 // Check the temporary vector is the correct size. If this fails then
1672 // getTypeToTransformTo() probably returned a type whose size (in bits)
1673 // isn't a power-of-2 factor of the requested type size.
1674 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1675
1676 SmallVector<SDValue, 2> EltParts;
1677 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1678 EltParts.push_back(getConstant(
1679 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1680 ViaEltVT, isT, isO));
1681
1682 // EltParts is currently in little endian order. If we actually want
1683 // big-endian order then reverse it now.
1684 if (getDataLayout().isBigEndian())
1685 std::reverse(EltParts.begin(), EltParts.end());
1686
1687 // The elements must be reversed when the element order is different
1688 // to the endianness of the elements (because the BITCAST is itself a
1689 // vector shuffle in this situation). However, we do not need any code to
1690 // perform this reversal because getConstant() is producing a vector
1691 // splat.
1692 // This situation occurs in MIPS MSA.
1693
1695 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1696 llvm::append_range(Ops, EltParts);
1697
1698 SDValue V =
1699 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1700 return V;
1701 }
1702
1703 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1704 "APInt size does not match type size!");
1705 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1707 AddNodeIDNode(ID, Opc, getVTList(EltVT), std::nullopt);
1708 ID.AddPointer(Elt);
1709 ID.AddBoolean(isO);
1710 void *IP = nullptr;
1711 SDNode *N = nullptr;
1712 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1713 if (!VT.isVector())
1714 return SDValue(N, 0);
1715
1716 if (!N) {
1717 N = newSDNode<ConstantSDNode>(isT, isO, Elt, EltVT);
1718 CSEMap.InsertNode(N, IP);
1719 InsertNode(N);
1720 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1721 }
1722
1723 SDValue Result(N, 0);
1724 if (VT.isVector())
1725 Result = getSplat(VT, DL, Result);
1726 return Result;
1727}
1728
1730 bool isTarget) {
1731 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1732}
1733
1735 const SDLoc &DL, bool LegalTypes) {
1736 assert(VT.isInteger() && "Shift amount is not an integer type!");
1737 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout(), LegalTypes);
1738 return getConstant(Val, DL, ShiftVT);
1739}
1740
1742 const SDLoc &DL, bool LegalTypes) {
1743 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1744 return getShiftAmountConstant(Val.getZExtValue(), VT, DL, LegalTypes);
1745}
1746
1748 bool isTarget) {
1749 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1750}
1751
1753 bool isTarget) {
1754 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1755}
1756
1758 EVT VT, bool isTarget) {
1759 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1760
1761 EVT EltVT = VT.getScalarType();
1762
1763 // Do the map lookup using the actual bit pattern for the floating point
1764 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1765 // we don't have issues with SNANs.
1766 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1768 AddNodeIDNode(ID, Opc, getVTList(EltVT), std::nullopt);
1769 ID.AddPointer(&V);
1770 void *IP = nullptr;
1771 SDNode *N = nullptr;
1772 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1773 if (!VT.isVector())
1774 return SDValue(N, 0);
1775
1776 if (!N) {
1777 N = newSDNode<ConstantFPSDNode>(isTarget, &V, EltVT);
1778 CSEMap.InsertNode(N, IP);
1779 InsertNode(N);
1780 }
1781
1782 SDValue Result(N, 0);
1783 if (VT.isVector())
1784 Result = getSplat(VT, DL, Result);
1785 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1786 return Result;
1787}
1788
1790 bool isTarget) {
1791 EVT EltVT = VT.getScalarType();
1792 if (EltVT == MVT::f32)
1793 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1794 if (EltVT == MVT::f64)
1795 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1796 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1797 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1798 bool Ignored;
1799 APFloat APF = APFloat(Val);
1801 &Ignored);
1802 return getConstantFP(APF, DL, VT, isTarget);
1803 }
1804 llvm_unreachable("Unsupported type in getConstantFP");
1805}
1806
1808 EVT VT, int64_t Offset, bool isTargetGA,
1809 unsigned TargetFlags) {
1810 assert((TargetFlags == 0 || isTargetGA) &&
1811 "Cannot set target flags on target-independent globals");
1812
1813 // Truncate (with sign-extension) the offset value to the pointer size.
1815 if (BitWidth < 64)
1817
1818 unsigned Opc;
1819 if (GV->isThreadLocal())
1821 else
1822 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
1823
1825 AddNodeIDNode(ID, Opc, getVTList(VT), std::nullopt);
1826 ID.AddPointer(GV);
1827 ID.AddInteger(Offset);
1828 ID.AddInteger(TargetFlags);
1829 void *IP = nullptr;
1830 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1831 return SDValue(E, 0);
1832
1833 auto *N = newSDNode<GlobalAddressSDNode>(
1834 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VT, Offset, TargetFlags);
1835 CSEMap.InsertNode(N, IP);
1836 InsertNode(N);
1837 return SDValue(N, 0);
1838}
1839
1840SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1841 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1843 AddNodeIDNode(ID, Opc, getVTList(VT), std::nullopt);
1844 ID.AddInteger(FI);
1845 void *IP = nullptr;
1846 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1847 return SDValue(E, 0);
1848
1849 auto *N = newSDNode<FrameIndexSDNode>(FI, VT, isTarget);
1850 CSEMap.InsertNode(N, IP);
1851 InsertNode(N);
1852 return SDValue(N, 0);
1853}
1854
1855SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1856 unsigned TargetFlags) {
1857 assert((TargetFlags == 0 || isTarget) &&
1858 "Cannot set target flags on target-independent jump tables");
1859 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1861 AddNodeIDNode(ID, Opc, getVTList(VT), std::nullopt);
1862 ID.AddInteger(JTI);
1863 ID.AddInteger(TargetFlags);
1864 void *IP = nullptr;
1865 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1866 return SDValue(E, 0);
1867
1868 auto *N = newSDNode<JumpTableSDNode>(JTI, VT, isTarget, TargetFlags);
1869 CSEMap.InsertNode(N, IP);
1870 InsertNode(N);
1871 return SDValue(N, 0);
1872}
1873
1875 const SDLoc &DL) {
1877 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Glue, Chain,
1878 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1879}
1880
1882 MaybeAlign Alignment, int Offset,
1883 bool isTarget, unsigned TargetFlags) {
1884 assert((TargetFlags == 0 || isTarget) &&
1885 "Cannot set target flags on target-independent globals");
1886 if (!Alignment)
1887 Alignment = shouldOptForSize()
1888 ? getDataLayout().getABITypeAlign(C->getType())
1889 : getDataLayout().getPrefTypeAlign(C->getType());
1890 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1892 AddNodeIDNode(ID, Opc, getVTList(VT), std::nullopt);
1893 ID.AddInteger(Alignment->value());
1894 ID.AddInteger(Offset);
1895 ID.AddPointer(C);
1896 ID.AddInteger(TargetFlags);
1897 void *IP = nullptr;
1898 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1899 return SDValue(E, 0);
1900
1901 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VT, Offset, *Alignment,
1902 TargetFlags);
1903 CSEMap.InsertNode(N, IP);
1904 InsertNode(N);
1905 SDValue V = SDValue(N, 0);
1906 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
1907 return V;
1908}
1909
1911 MaybeAlign Alignment, int Offset,
1912 bool isTarget, unsigned TargetFlags) {
1913 assert((TargetFlags == 0 || isTarget) &&
1914 "Cannot set target flags on target-independent globals");
1915 if (!Alignment)
1916 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
1917 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1919 AddNodeIDNode(ID, Opc, getVTList(VT), std::nullopt);
1920 ID.AddInteger(Alignment->value());
1921 ID.AddInteger(Offset);
1922 C->addSelectionDAGCSEId(ID);
1923 ID.AddInteger(TargetFlags);
1924 void *IP = nullptr;
1925 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1926 return SDValue(E, 0);
1927
1928 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VT, Offset, *Alignment,
1929 TargetFlags);
1930 CSEMap.InsertNode(N, IP);
1931 InsertNode(N);
1932 return SDValue(N, 0);
1933}
1934
1937 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), std::nullopt);
1938 ID.AddPointer(MBB);
1939 void *IP = nullptr;
1940 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1941 return SDValue(E, 0);
1942
1943 auto *N = newSDNode<BasicBlockSDNode>(MBB);
1944 CSEMap.InsertNode(N, IP);
1945 InsertNode(N);
1946 return SDValue(N, 0);
1947}
1948
1950 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1951 ValueTypeNodes.size())
1952 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
1953
1954 SDNode *&N = VT.isExtended() ?
1955 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
1956
1957 if (N) return SDValue(N, 0);
1958 N = newSDNode<VTSDNode>(VT);
1959 InsertNode(N);
1960 return SDValue(N, 0);
1961}
1962
1964 SDNode *&N = ExternalSymbols[Sym];
1965 if (N) return SDValue(N, 0);
1966 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, VT);
1967 InsertNode(N);
1968 return SDValue(N, 0);
1969}
1970
1972 SDNode *&N = MCSymbols[Sym];
1973 if (N)
1974 return SDValue(N, 0);
1975 N = newSDNode<MCSymbolSDNode>(Sym, VT);
1976 InsertNode(N);
1977 return SDValue(N, 0);
1978}
1979
1981 unsigned TargetFlags) {
1982 SDNode *&N =
1983 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
1984 if (N) return SDValue(N, 0);
1985 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, VT);
1986 InsertNode(N);
1987 return SDValue(N, 0);
1988}
1989
1991 if ((unsigned)Cond >= CondCodeNodes.size())
1992 CondCodeNodes.resize(Cond+1);
1993
1994 if (!CondCodeNodes[Cond]) {
1995 auto *N = newSDNode<CondCodeSDNode>(Cond);
1996 CondCodeNodes[Cond] = N;
1997 InsertNode(N);
1998 }
1999
2000 return SDValue(CondCodeNodes[Cond], 0);
2001}
2002
2004 bool ConstantFold) {
2005 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2006 "APInt size does not match type size!");
2007
2008 if (MulImm == 0)
2009 return getConstant(0, DL, VT);
2010
2011 if (ConstantFold) {
2012 const MachineFunction &MF = getMachineFunction();
2013 const Function &F = MF.getFunction();
2014 ConstantRange CR = getVScaleRange(&F, 64);
2015 if (const APInt *C = CR.getSingleElement())
2016 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2017 }
2018
2019 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2020}
2021
2023 bool ConstantFold) {
2024 if (EC.isScalable())
2025 return getVScale(DL, VT,
2026 APInt(VT.getSizeInBits(), EC.getKnownMinValue()));
2027
2028 return getConstant(EC.getKnownMinValue(), DL, VT);
2029}
2030
2032 APInt One(ResVT.getScalarSizeInBits(), 1);
2033 return getStepVector(DL, ResVT, One);
2034}
2035
2037 const APInt &StepVal) {
2038 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2039 if (ResVT.isScalableVector())
2040 return getNode(
2041 ISD::STEP_VECTOR, DL, ResVT,
2042 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2043
2044 SmallVector<SDValue, 16> OpsStepConstants;
2045 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2046 OpsStepConstants.push_back(
2047 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2048 return getBuildVector(ResVT, DL, OpsStepConstants);
2049}
2050
2051/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2052/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2054 std::swap(N1, N2);
2056}
2057
2059 SDValue N2, ArrayRef<int> Mask) {
2060 assert(VT.getVectorNumElements() == Mask.size() &&
2061 "Must have the same number of vector elements as mask elements!");
2062 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2063 "Invalid VECTOR_SHUFFLE");
2064
2065 // Canonicalize shuffle undef, undef -> undef
2066 if (N1.isUndef() && N2.isUndef())
2067 return getUNDEF(VT);
2068
2069 // Validate that all indices in Mask are within the range of the elements
2070 // input to the shuffle.
2071 int NElts = Mask.size();
2072 assert(llvm::all_of(Mask,
2073 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2074 "Index out of range");
2075
2076 // Copy the mask so we can do any needed cleanup.
2077 SmallVector<int, 8> MaskVec(Mask);
2078
2079 // Canonicalize shuffle v, v -> v, undef
2080 if (N1 == N2) {
2081 N2 = getUNDEF(VT);
2082 for (int i = 0; i != NElts; ++i)
2083 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2084 }
2085
2086 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2087 if (N1.isUndef())
2088 commuteShuffle(N1, N2, MaskVec);
2089
2090 if (TLI->hasVectorBlend()) {
2091 // If shuffling a splat, try to blend the splat instead. We do this here so
2092 // that even when this arises during lowering we don't have to re-handle it.
2093 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2094 BitVector UndefElements;
2095 SDValue Splat = BV->getSplatValue(&UndefElements);
2096 if (!Splat)
2097 return;
2098
2099 for (int i = 0; i < NElts; ++i) {
2100 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2101 continue;
2102
2103 // If this input comes from undef, mark it as such.
2104 if (UndefElements[MaskVec[i] - Offset]) {
2105 MaskVec[i] = -1;
2106 continue;
2107 }
2108
2109 // If we can blend a non-undef lane, use that instead.
2110 if (!UndefElements[i])
2111 MaskVec[i] = i + Offset;
2112 }
2113 };
2114 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2115 BlendSplat(N1BV, 0);
2116 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2117 BlendSplat(N2BV, NElts);
2118 }
2119
2120 // Canonicalize all index into lhs, -> shuffle lhs, undef
2121 // Canonicalize all index into rhs, -> shuffle rhs, undef
2122 bool AllLHS = true, AllRHS = true;
2123 bool N2Undef = N2.isUndef();
2124 for (int i = 0; i != NElts; ++i) {
2125 if (MaskVec[i] >= NElts) {
2126 if (N2Undef)
2127 MaskVec[i] = -1;
2128 else
2129 AllLHS = false;
2130 } else if (MaskVec[i] >= 0) {
2131 AllRHS = false;
2132 }
2133 }
2134 if (AllLHS && AllRHS)
2135 return getUNDEF(VT);
2136 if (AllLHS && !N2Undef)
2137 N2 = getUNDEF(VT);
2138 if (AllRHS) {
2139 N1 = getUNDEF(VT);
2140 commuteShuffle(N1, N2, MaskVec);
2141 }
2142 // Reset our undef status after accounting for the mask.
2143 N2Undef = N2.isUndef();
2144 // Re-check whether both sides ended up undef.
2145 if (N1.isUndef() && N2Undef)
2146 return getUNDEF(VT);
2147
2148 // If Identity shuffle return that node.
2149 bool Identity = true, AllSame = true;
2150 for (int i = 0; i != NElts; ++i) {
2151 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2152 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2153 }
2154 if (Identity && NElts)
2155 return N1;
2156
2157 // Shuffling a constant splat doesn't change the result.
2158 if (N2Undef) {
2159 SDValue V = N1;
2160
2161 // Look through any bitcasts. We check that these don't change the number
2162 // (and size) of elements and just changes their types.
2163 while (V.getOpcode() == ISD::BITCAST)
2164 V = V->getOperand(0);
2165
2166 // A splat should always show up as a build vector node.
2167 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2168 BitVector UndefElements;
2169 SDValue Splat = BV->getSplatValue(&UndefElements);
2170 // If this is a splat of an undef, shuffling it is also undef.
2171 if (Splat && Splat.isUndef())
2172 return getUNDEF(VT);
2173
2174 bool SameNumElts =
2175 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2176
2177 // We only have a splat which can skip shuffles if there is a splatted
2178 // value and no undef lanes rearranged by the shuffle.
2179 if (Splat && UndefElements.none()) {
2180 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2181 // number of elements match or the value splatted is a zero constant.
2182 if (SameNumElts || isNullConstant(Splat))
2183 return N1;
2184 }
2185
2186 // If the shuffle itself creates a splat, build the vector directly.
2187 if (AllSame && SameNumElts) {
2188 EVT BuildVT = BV->getValueType(0);
2189 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2190 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2191
2192 // We may have jumped through bitcasts, so the type of the
2193 // BUILD_VECTOR may not match the type of the shuffle.
2194 if (BuildVT != VT)
2195 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2196 return NewBV;
2197 }
2198 }
2199 }
2200
2202 SDValue Ops[2] = { N1, N2 };
2204 for (int i = 0; i != NElts; ++i)
2205 ID.AddInteger(MaskVec[i]);
2206
2207 void* IP = nullptr;
2208 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2209 return SDValue(E, 0);
2210
2211 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2212 // SDNode doesn't have access to it. This memory will be "leaked" when
2213 // the node is deallocated, but recovered when the NodeAllocator is released.
2214 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2215 llvm::copy(MaskVec, MaskAlloc);
2216
2217 auto *N = newSDNode<ShuffleVectorSDNode>(VT, dl.getIROrder(),
2218 dl.getDebugLoc(), MaskAlloc);
2219 createOperands(N, Ops);
2220
2221 CSEMap.InsertNode(N, IP);
2222 InsertNode(N);
2223 SDValue V = SDValue(N, 0);
2224 NewSDValueDbgMsg(V, "Creating new node: ", this);
2225 return V;
2226}
2227
2229 EVT VT = SV.getValueType(0);
2230 SmallVector<int, 8> MaskVec(SV.getMask());
2232
2233 SDValue Op0 = SV.getOperand(0);
2234 SDValue Op1 = SV.getOperand(1);
2235 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2236}
2237
2240 AddNodeIDNode(ID, ISD::Register, getVTList(VT), std::nullopt);
2241 ID.AddInteger(RegNo);
2242 void *IP = nullptr;
2243 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2244 return SDValue(E, 0);
2245
2246 auto *N = newSDNode<RegisterSDNode>(RegNo, VT);
2247 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2248 CSEMap.InsertNode(N, IP);
2249 InsertNode(N);
2250 return SDValue(N, 0);
2251}
2252
2255 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), std::nullopt);
2256 ID.AddPointer(RegMask);
2257 void *IP = nullptr;
2258 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2259 return SDValue(E, 0);
2260
2261 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2262 CSEMap.InsertNode(N, IP);
2263 InsertNode(N);
2264 return SDValue(N, 0);
2265}
2266
2268 MCSymbol *Label) {
2269 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2270}
2271
2272SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2273 SDValue Root, MCSymbol *Label) {
2275 SDValue Ops[] = { Root };
2276 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2277 ID.AddPointer(Label);
2278 void *IP = nullptr;
2279 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2280 return SDValue(E, 0);
2281
2282 auto *N =
2283 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2284 createOperands(N, Ops);
2285
2286 CSEMap.InsertNode(N, IP);
2287 InsertNode(N);
2288 return SDValue(N, 0);
2289}
2290
2292 int64_t Offset, bool isTarget,
2293 unsigned TargetFlags) {
2294 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2295
2297 AddNodeIDNode(ID, Opc, getVTList(VT), std::nullopt);
2298 ID.AddPointer(BA);
2299 ID.AddInteger(Offset);
2300 ID.AddInteger(TargetFlags);
2301 void *IP = nullptr;
2302 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2303 return SDValue(E, 0);
2304
2305 auto *N = newSDNode<BlockAddressSDNode>(Opc, VT, BA, Offset, TargetFlags);
2306 CSEMap.InsertNode(N, IP);
2307 InsertNode(N);
2308 return SDValue(N, 0);
2309}
2310
2313 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), std::nullopt);
2314 ID.AddPointer(V);
2315
2316 void *IP = nullptr;
2317 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2318 return SDValue(E, 0);
2319
2320 auto *N = newSDNode<SrcValueSDNode>(V);
2321 CSEMap.InsertNode(N, IP);
2322 InsertNode(N);
2323 return SDValue(N, 0);
2324}
2325
2328 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), std::nullopt);
2329 ID.AddPointer(MD);
2330
2331 void *IP = nullptr;
2332 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2333 return SDValue(E, 0);
2334
2335 auto *N = newSDNode<MDNodeSDNode>(MD);
2336 CSEMap.InsertNode(N, IP);
2337 InsertNode(N);
2338 return SDValue(N, 0);
2339}
2340
2342 if (VT == V.getValueType())
2343 return V;
2344
2345 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2346}
2347
2349 unsigned SrcAS, unsigned DestAS) {
2350 SDValue Ops[] = {Ptr};
2353 ID.AddInteger(SrcAS);
2354 ID.AddInteger(DestAS);
2355
2356 void *IP = nullptr;
2357 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2358 return SDValue(E, 0);
2359
2360 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2361 VT, SrcAS, DestAS);
2362 createOperands(N, Ops);
2363
2364 CSEMap.InsertNode(N, IP);
2365 InsertNode(N);
2366 return SDValue(N, 0);
2367}
2368
2370 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2371}
2372
2373/// getShiftAmountOperand - Return the specified value casted to
2374/// the target's desired shift amount type.
2376 EVT OpTy = Op.getValueType();
2377 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2378 if (OpTy == ShTy || OpTy.isVector()) return Op;
2379
2380 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2381}
2382
2384 SDLoc dl(Node);
2386 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2387 EVT VT = Node->getValueType(0);
2388 SDValue Tmp1 = Node->getOperand(0);
2389 SDValue Tmp2 = Node->getOperand(1);
2390 const MaybeAlign MA(Node->getConstantOperandVal(3));
2391
2392 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2393 Tmp2, MachinePointerInfo(V));
2394 SDValue VAList = VAListLoad;
2395
2396 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2397 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2398 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2399
2400 VAList =
2401 getNode(ISD::AND, dl, VAList.getValueType(), VAList,
2402 getConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2403 }
2404
2405 // Increment the pointer, VAList, to the next vaarg
2406 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2407 getConstant(getDataLayout().getTypeAllocSize(
2408 VT.getTypeForEVT(*getContext())),
2409 dl, VAList.getValueType()));
2410 // Store the incremented VAList to the legalized pointer
2411 Tmp1 =
2412 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2413 // Load the actual argument out of the pointer VAList
2414 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2415}
2416
2418 SDLoc dl(Node);
2420 // This defaults to loading a pointer from the input and storing it to the
2421 // output, returning the chain.
2422 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2423 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2424 SDValue Tmp1 =
2425 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2426 Node->getOperand(2), MachinePointerInfo(VS));
2427 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2428 MachinePointerInfo(VD));
2429}
2430
2432 const DataLayout &DL = getDataLayout();
2433 Type *Ty = VT.getTypeForEVT(*getContext());
2434 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2435
2436 if (TLI->isTypeLegal(VT) || !VT.isVector())
2437 return RedAlign;
2438
2440 const Align StackAlign = TFI->getStackAlign();
2441
2442 // See if we can choose a smaller ABI alignment in cases where it's an
2443 // illegal vector type that will get broken down.
2444 if (RedAlign > StackAlign) {
2445 EVT IntermediateVT;
2446 MVT RegisterVT;
2447 unsigned NumIntermediates;
2448 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2449 NumIntermediates, RegisterVT);
2450 Ty = IntermediateVT.getTypeForEVT(*getContext());
2451 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2452 if (RedAlign2 < RedAlign)
2453 RedAlign = RedAlign2;
2454 }
2455
2456 return RedAlign;
2457}
2458
2460 MachineFrameInfo &MFI = MF->getFrameInfo();
2462 int StackID = 0;
2463 if (Bytes.isScalable())
2464 StackID = TFI->getStackIDForScalableVectors();
2465 // The stack id gives an indication of whether the object is scalable or
2466 // not, so it's safe to pass in the minimum size here.
2467 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2468 false, nullptr, StackID);
2469 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2470}
2471
2473 Type *Ty = VT.getTypeForEVT(*getContext());
2474 Align StackAlign =
2475 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2476 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2477}
2478
2480 TypeSize VT1Size = VT1.getStoreSize();
2481 TypeSize VT2Size = VT2.getStoreSize();
2482 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2483 "Don't know how to choose the maximum size when creating a stack "
2484 "temporary");
2485 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2486 ? VT1Size
2487 : VT2Size;
2488
2489 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2490 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2491 const DataLayout &DL = getDataLayout();
2492 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2493 return CreateStackTemporary(Bytes, Align);
2494}
2495
2497 ISD::CondCode Cond, const SDLoc &dl) {
2498 EVT OpVT = N1.getValueType();
2499
2500 auto GetUndefBooleanConstant = [&]() {
2501 if (VT.getScalarType() == MVT::i1 ||
2502 TLI->getBooleanContents(OpVT) ==
2504 return getUNDEF(VT);
2505 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2506 // so we cannot use getUNDEF(). Return zero instead.
2507 return getConstant(0, dl, VT);
2508 };
2509
2510 // These setcc operations always fold.
2511 switch (Cond) {
2512 default: break;
2513 case ISD::SETFALSE:
2514 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2515 case ISD::SETTRUE:
2516 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2517
2518 case ISD::SETOEQ:
2519 case ISD::SETOGT:
2520 case ISD::SETOGE:
2521 case ISD::SETOLT:
2522 case ISD::SETOLE:
2523 case ISD::SETONE:
2524 case ISD::SETO:
2525 case ISD::SETUO:
2526 case ISD::SETUEQ:
2527 case ISD::SETUNE:
2528 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2529 break;
2530 }
2531
2532 if (OpVT.isInteger()) {
2533 // For EQ and NE, we can always pick a value for the undef to make the
2534 // predicate pass or fail, so we can return undef.
2535 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2536 // icmp eq/ne X, undef -> undef.
2537 if ((N1.isUndef() || N2.isUndef()) &&
2538 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2539 return GetUndefBooleanConstant();
2540
2541 // If both operands are undef, we can return undef for int comparison.
2542 // icmp undef, undef -> undef.
2543 if (N1.isUndef() && N2.isUndef())
2544 return GetUndefBooleanConstant();
2545
2546 // icmp X, X -> true/false
2547 // icmp X, undef -> true/false because undef could be X.
2548 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2549 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2550 }
2551
2552 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2)) {
2553 const APInt &C2 = N2C->getAPIntValue();
2554 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
2555 const APInt &C1 = N1C->getAPIntValue();
2556
2558 dl, VT, OpVT);
2559 }
2560 }
2561
2562 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2563 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2564
2565 if (N1CFP && N2CFP) {
2566 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2567 switch (Cond) {
2568 default: break;
2569 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2570 return GetUndefBooleanConstant();
2571 [[fallthrough]];
2572 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2573 OpVT);
2574 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2575 return GetUndefBooleanConstant();
2576 [[fallthrough]];
2578 R==APFloat::cmpLessThan, dl, VT,
2579 OpVT);
2580 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2581 return GetUndefBooleanConstant();
2582 [[fallthrough]];
2583 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2584 OpVT);
2585 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2586 return GetUndefBooleanConstant();
2587 [[fallthrough]];
2589 VT, OpVT);
2590 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2591 return GetUndefBooleanConstant();
2592 [[fallthrough]];
2594 R==APFloat::cmpEqual, dl, VT,
2595 OpVT);
2596 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2597 return GetUndefBooleanConstant();
2598 [[fallthrough]];
2600 R==APFloat::cmpEqual, dl, VT, OpVT);
2601 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2602 OpVT);
2603 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2604 OpVT);
2606 R==APFloat::cmpEqual, dl, VT,
2607 OpVT);
2608 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2609 OpVT);
2611 R==APFloat::cmpLessThan, dl, VT,
2612 OpVT);
2614 R==APFloat::cmpUnordered, dl, VT,
2615 OpVT);
2617 VT, OpVT);
2618 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2619 OpVT);
2620 }
2621 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2622 // Ensure that the constant occurs on the RHS.
2624 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2625 return SDValue();
2626 return getSetCC(dl, VT, N2, N1, SwappedCond);
2627 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2628 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2629 // If an operand is known to be a nan (or undef that could be a nan), we can
2630 // fold it.
2631 // Choosing NaN for the undef will always make unordered comparison succeed
2632 // and ordered comparison fails.
2633 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2634 switch (ISD::getUnorderedFlavor(Cond)) {
2635 default:
2636 llvm_unreachable("Unknown flavor!");
2637 case 0: // Known false.
2638 return getBoolConstant(false, dl, VT, OpVT);
2639 case 1: // Known true.
2640 return getBoolConstant(true, dl, VT, OpVT);
2641 case 2: // Undefined.
2642 return GetUndefBooleanConstant();
2643 }
2644 }
2645
2646 // Could not fold it.
2647 return SDValue();
2648}
2649
2650/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2651/// use this predicate to simplify operations downstream.
2653 unsigned BitWidth = Op.getScalarValueSizeInBits();
2655}
2656
2657/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2658/// this predicate to simplify operations downstream. Mask is known to be zero
2659/// for bits that V cannot have.
2661 unsigned Depth) const {
2662 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2663}
2664
2665/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2666/// DemandedElts. We use this predicate to simplify operations downstream.
2667/// Mask is known to be zero for bits that V cannot have.
2669 const APInt &DemandedElts,
2670 unsigned Depth) const {
2671 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2672}
2673
2674/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2675/// DemandedElts. We use this predicate to simplify operations downstream.
2677 unsigned Depth /* = 0 */) const {
2678 return computeKnownBits(V, DemandedElts, Depth).isZero();
2679}
2680
2681/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2683 unsigned Depth) const {
2684 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2685}
2686
2688 const APInt &DemandedElts,
2689 unsigned Depth) const {
2690 EVT VT = Op.getValueType();
2691 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2692
2693 unsigned NumElts = VT.getVectorNumElements();
2694 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2695
2696 APInt KnownZeroElements = APInt::getZero(NumElts);
2697 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2698 if (!DemandedElts[EltIdx])
2699 continue; // Don't query elements that are not demanded.
2700 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2701 if (MaskedVectorIsZero(Op, Mask, Depth))
2702 KnownZeroElements.setBit(EltIdx);
2703 }
2704 return KnownZeroElements;
2705}
2706
2707/// isSplatValue - Return true if the vector V has the same value
2708/// across all DemandedElts. For scalable vectors, we don't know the
2709/// number of lanes at compile time. Instead, we use a 1 bit APInt
2710/// to represent a conservative value for all lanes; that is, that
2711/// one bit value is implicitly splatted across all lanes.
2712bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2713 APInt &UndefElts, unsigned Depth) const {
2714 unsigned Opcode = V.getOpcode();
2715 EVT VT = V.getValueType();
2716 assert(VT.isVector() && "Vector type expected");
2717 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2718 "scalable demanded bits are ignored");
2719
2720 if (!DemandedElts)
2721 return false; // No demanded elts, better to assume we don't know anything.
2722
2723 if (Depth >= MaxRecursionDepth)
2724 return false; // Limit search depth.
2725
2726 // Deal with some common cases here that work for both fixed and scalable
2727 // vector types.
2728 switch (Opcode) {
2729 case ISD::SPLAT_VECTOR:
2730 UndefElts = V.getOperand(0).isUndef()
2731 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2732 : APInt(DemandedElts.getBitWidth(), 0);
2733 return true;
2734 case ISD::ADD:
2735 case ISD::SUB:
2736 case ISD::AND:
2737 case ISD::XOR:
2738 case ISD::OR: {
2739 APInt UndefLHS, UndefRHS;
2740 SDValue LHS = V.getOperand(0);
2741 SDValue RHS = V.getOperand(1);
2742 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2743 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1)) {
2744 UndefElts = UndefLHS | UndefRHS;
2745 return true;
2746 }
2747 return false;
2748 }
2749 case ISD::ABS:
2750 case ISD::TRUNCATE:
2751 case ISD::SIGN_EXTEND:
2752 case ISD::ZERO_EXTEND:
2753 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2754 default:
2755 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2756 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2757 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2758 Depth);
2759 break;
2760}
2761
2762 // We don't support other cases than those above for scalable vectors at
2763 // the moment.
2764 if (VT.isScalableVector())
2765 return false;
2766
2767 unsigned NumElts = VT.getVectorNumElements();
2768 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2769 UndefElts = APInt::getZero(NumElts);
2770
2771 switch (Opcode) {
2772 case ISD::BUILD_VECTOR: {
2773 SDValue Scl;
2774 for (unsigned i = 0; i != NumElts; ++i) {
2775 SDValue Op = V.getOperand(i);
2776 if (Op.isUndef()) {
2777 UndefElts.setBit(i);
2778 continue;
2779 }
2780 if (!DemandedElts[i])
2781 continue;
2782 if (Scl && Scl != Op)
2783 return false;
2784 Scl = Op;
2785 }
2786 return true;
2787 }
2788 case ISD::VECTOR_SHUFFLE: {
2789 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2790 APInt DemandedLHS = APInt::getZero(NumElts);
2791 APInt DemandedRHS = APInt::getZero(NumElts);
2792 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
2793 for (int i = 0; i != (int)NumElts; ++i) {
2794 int M = Mask[i];
2795 if (M < 0) {
2796 UndefElts.setBit(i);
2797 continue;
2798 }
2799 if (!DemandedElts[i])
2800 continue;
2801 if (M < (int)NumElts)
2802 DemandedLHS.setBit(M);
2803 else
2804 DemandedRHS.setBit(M - NumElts);
2805 }
2806
2807 // If we aren't demanding either op, assume there's no splat.
2808 // If we are demanding both ops, assume there's no splat.
2809 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
2810 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
2811 return false;
2812
2813 // See if the demanded elts of the source op is a splat or we only demand
2814 // one element, which should always be a splat.
2815 // TODO: Handle source ops splats with undefs.
2816 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
2817 APInt SrcUndefs;
2818 return (SrcElts.popcount() == 1) ||
2819 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
2820 (SrcElts & SrcUndefs).isZero());
2821 };
2822 if (!DemandedLHS.isZero())
2823 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
2824 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
2825 }
2827 // Offset the demanded elts by the subvector index.
2828 SDValue Src = V.getOperand(0);
2829 // We don't support scalable vectors at the moment.
2830 if (Src.getValueType().isScalableVector())
2831 return false;
2832 uint64_t Idx = V.getConstantOperandVal(1);
2833 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2834 APInt UndefSrcElts;
2835 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
2836 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2837 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
2838 return true;
2839 }
2840 break;
2841 }
2845 // Widen the demanded elts by the src element count.
2846 SDValue Src = V.getOperand(0);
2847 // We don't support scalable vectors at the moment.
2848 if (Src.getValueType().isScalableVector())
2849 return false;
2850 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2851 APInt UndefSrcElts;
2852 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
2853 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2854 UndefElts = UndefSrcElts.trunc(NumElts);
2855 return true;
2856 }
2857 break;
2858 }
2859 case ISD::BITCAST: {
2860 SDValue Src = V.getOperand(0);
2861 EVT SrcVT = Src.getValueType();
2862 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
2863 unsigned BitWidth = VT.getScalarSizeInBits();
2864
2865 // Ignore bitcasts from unsupported types.
2866 // TODO: Add fp support?
2867 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
2868 break;
2869
2870 // Bitcast 'small element' vector to 'large element' vector.
2871 if ((BitWidth % SrcBitWidth) == 0) {
2872 // See if each sub element is a splat.
2873 unsigned Scale = BitWidth / SrcBitWidth;
2874 unsigned NumSrcElts = SrcVT.getVectorNumElements();
2875 APInt ScaledDemandedElts =
2876 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
2877 for (unsigned I = 0; I != Scale; ++I) {
2878 APInt SubUndefElts;
2879 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
2880 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
2881 SubDemandedElts &= ScaledDemandedElts;
2882 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
2883 return false;
2884 // TODO: Add support for merging sub undef elements.
2885 if (!SubUndefElts.isZero())
2886 return false;
2887 }
2888 return true;
2889 }
2890 break;
2891 }
2892 }
2893
2894 return false;
2895}
2896
2897/// Helper wrapper to main isSplatValue function.
2898bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
2899 EVT VT = V.getValueType();
2900 assert(VT.isVector() && "Vector type expected");
2901
2902 APInt UndefElts;
2903 // Since the number of lanes in a scalable vector is unknown at compile time,
2904 // we track one bit which is implicitly broadcast to all lanes. This means
2905 // that all lanes in a scalable vector are considered demanded.
2906 APInt DemandedElts
2908 return isSplatValue(V, DemandedElts, UndefElts) &&
2909 (AllowUndefs || !UndefElts);
2910}
2911
2914
2915 EVT VT = V.getValueType();
2916 unsigned Opcode = V.getOpcode();
2917 switch (Opcode) {
2918 default: {
2919 APInt UndefElts;
2920 // Since the number of lanes in a scalable vector is unknown at compile time,
2921 // we track one bit which is implicitly broadcast to all lanes. This means
2922 // that all lanes in a scalable vector are considered demanded.
2923 APInt DemandedElts
2925
2926 if (isSplatValue(V, DemandedElts, UndefElts)) {
2927 if (VT.isScalableVector()) {
2928 // DemandedElts and UndefElts are ignored for scalable vectors, since
2929 // the only supported cases are SPLAT_VECTOR nodes.
2930 SplatIdx = 0;
2931 } else {
2932 // Handle case where all demanded elements are UNDEF.
2933 if (DemandedElts.isSubsetOf(UndefElts)) {
2934 SplatIdx = 0;
2935 return getUNDEF(VT);
2936 }
2937 SplatIdx = (UndefElts & DemandedElts).countr_one();
2938 }
2939 return V;
2940 }
2941 break;
2942 }
2943 case ISD::SPLAT_VECTOR:
2944 SplatIdx = 0;
2945 return V;
2946 case ISD::VECTOR_SHUFFLE: {
2947 assert(!VT.isScalableVector());
2948 // Check if this is a shuffle node doing a splat.
2949 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
2950 // getTargetVShiftNode currently struggles without the splat source.
2951 auto *SVN = cast<ShuffleVectorSDNode>(V);
2952 if (!SVN->isSplat())
2953 break;
2954 int Idx = SVN->getSplatIndex();
2955 int NumElts = V.getValueType().getVectorNumElements();
2956 SplatIdx = Idx % NumElts;
2957 return V.getOperand(Idx / NumElts);
2958 }
2959 }
2960
2961 return SDValue();
2962}
2963
2965 int SplatIdx;
2966 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
2967 EVT SVT = SrcVector.getValueType().getScalarType();
2968 EVT LegalSVT = SVT;
2969 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
2970 if (!SVT.isInteger())
2971 return SDValue();
2972 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
2973 if (LegalSVT.bitsLT(SVT))
2974 return SDValue();
2975 }
2976 return getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(V), LegalSVT, SrcVector,
2977 getVectorIdxConstant(SplatIdx, SDLoc(V)));
2978 }
2979 return SDValue();
2980}
2981
2982const APInt *
2984 const APInt &DemandedElts) const {
2985 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
2986 V.getOpcode() == ISD::SRA) &&
2987 "Unknown shift node");
2988 unsigned BitWidth = V.getScalarValueSizeInBits();
2989 if (ConstantSDNode *SA = isConstOrConstSplat(V.getOperand(1), DemandedElts)) {
2990 // Shifting more than the bitwidth is not valid.
2991 const APInt &ShAmt = SA->getAPIntValue();
2992 if (ShAmt.ult(BitWidth))
2993 return &ShAmt;
2994 }
2995 return nullptr;
2996}
2997
2999 SDValue V, const APInt &DemandedElts) const {
3000 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3001 V.getOpcode() == ISD::SRA) &&
3002 "Unknown shift node");
3003 if (const APInt *ValidAmt = getValidShiftAmountConstant(V, DemandedElts))
3004 return ValidAmt;
3005 unsigned BitWidth = V.getScalarValueSizeInBits();
3006 auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1));
3007 if (!BV)
3008 return nullptr;
3009 const APInt *MinShAmt = nullptr;
3010 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3011 if (!DemandedElts[i])
3012 continue;
3013 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3014 if (!SA)
3015 return nullptr;
3016 // Shifting more than the bitwidth is not valid.
3017 const APInt &ShAmt = SA->getAPIntValue();
3018 if (ShAmt.uge(BitWidth))
3019 return nullptr;
3020 if (MinShAmt && MinShAmt->ule(ShAmt))
3021 continue;
3022 MinShAmt = &ShAmt;
3023 }
3024 return MinShAmt;
3025}
3026
3028 SDValue V, const APInt &DemandedElts) const {
3029 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3030 V.getOpcode() == ISD::SRA) &&
3031 "Unknown shift node");
3032 if (const APInt *ValidAmt = getValidShiftAmountConstant(V, DemandedElts))
3033 return ValidAmt;
3034 unsigned BitWidth = V.getScalarValueSizeInBits();
3035 auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1));
3036 if (!BV)
3037 return nullptr;
3038 const APInt *MaxShAmt = nullptr;
3039 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3040 if (!DemandedElts[i])
3041 continue;
3042 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3043 if (!SA)
3044 return nullptr;
3045 // Shifting more than the bitwidth is not valid.
3046 const APInt &ShAmt = SA->getAPIntValue();
3047 if (ShAmt.uge(BitWidth))
3048 return nullptr;
3049 if (MaxShAmt && MaxShAmt->uge(ShAmt))
3050 continue;
3051 MaxShAmt = &ShAmt;
3052 }
3053 return MaxShAmt;
3054}
3055
3056/// Determine which bits of Op are known to be either zero or one and return
3057/// them in Known. For vectors, the known bits are those that are shared by
3058/// every vector element.
3060 EVT VT = Op.getValueType();
3061
3062 // Since the number of lanes in a scalable vector is unknown at compile time,
3063 // we track one bit which is implicitly broadcast to all lanes. This means
3064 // that all lanes in a scalable vector are considered demanded.
3065 APInt DemandedElts = VT.isFixedLengthVector()
3067 : APInt(1, 1);
3068 return computeKnownBits(Op, DemandedElts, Depth);
3069}
3070
3071/// Determine which bits of Op are known to be either zero or one and return
3072/// them in Known. The DemandedElts argument allows us to only collect the known
3073/// bits that are shared by the requested vector elements.
3075 unsigned Depth) const {
3076 unsigned BitWidth = Op.getScalarValueSizeInBits();
3077
3078 KnownBits Known(BitWidth); // Don't know anything.
3079
3080 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
3081 // We know all of the bits for a constant!
3082 return KnownBits::makeConstant(C->getAPIntValue());
3083 }
3084 if (auto *C = dyn_cast<ConstantFPSDNode>(Op)) {
3085 // We know all of the bits for a constant fp!
3086 return KnownBits::makeConstant(C->getValueAPF().bitcastToAPInt());
3087 }
3088
3089 if (Depth >= MaxRecursionDepth)
3090 return Known; // Limit search depth.
3091
3092 KnownBits Known2;
3093 unsigned NumElts = DemandedElts.getBitWidth();
3094 assert((!Op.getValueType().isFixedLengthVector() ||
3095 NumElts == Op.getValueType().getVectorNumElements()) &&
3096 "Unexpected vector size");
3097
3098 if (!DemandedElts)
3099 return Known; // No demanded elts, better to assume we don't know anything.
3100
3101 unsigned Opcode = Op.getOpcode();
3102 switch (Opcode) {
3103 case ISD::MERGE_VALUES:
3104 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3105 Depth + 1);
3106 case ISD::SPLAT_VECTOR: {
3107 SDValue SrcOp = Op.getOperand(0);
3108 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3109 "Expected SPLAT_VECTOR implicit truncation");
3110 // Implicitly truncate the bits to match the official semantics of
3111 // SPLAT_VECTOR.
3112 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3113 break;
3114 }
3116 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3117 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3118 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3119 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3120 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3121 }
3122 break;
3123 }
3124 case ISD::STEP_VECTOR: {
3125 const APInt &Step = Op.getConstantOperandAPInt(0);
3126
3127 if (Step.isPowerOf2())
3128 Known.Zero.setLowBits(Step.logBase2());
3129
3131
3132 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3133 break;
3134 const APInt MinNumElts =
3135 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3136
3137 bool Overflow;
3138 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3140 .umul_ov(MinNumElts, Overflow);
3141 if (Overflow)
3142 break;
3143
3144 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3145 if (Overflow)
3146 break;
3147
3148 Known.Zero.setHighBits(MaxValue.countl_zero());
3149 break;
3150 }
3151 case ISD::BUILD_VECTOR:
3152 assert(!Op.getValueType().isScalableVector());
3153 // Collect the known bits that are shared by every demanded vector element.
3154 Known.Zero.setAllBits(); Known.One.setAllBits();
3155 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3156 if (!DemandedElts[i])
3157 continue;
3158
3159 SDValue SrcOp = Op.getOperand(i);
3160 Known2 = computeKnownBits(SrcOp, Depth + 1);
3161
3162 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3163 if (SrcOp.getValueSizeInBits() != BitWidth) {
3164 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3165 "Expected BUILD_VECTOR implicit truncation");
3166 Known2 = Known2.trunc(BitWidth);
3167 }
3168
3169 // Known bits are the values that are shared by every demanded element.
3170 Known = Known.intersectWith(Known2);
3171
3172 // If we don't know any bits, early out.
3173 if (Known.isUnknown())
3174 break;
3175 }
3176 break;
3177 case ISD::VECTOR_SHUFFLE: {
3178 assert(!Op.getValueType().isScalableVector());
3179 // Collect the known bits that are shared by every vector element referenced
3180 // by the shuffle.
3181 APInt DemandedLHS, DemandedRHS;
3182 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
3183 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3184 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3185 DemandedLHS, DemandedRHS))
3186 break;
3187
3188 // Known bits are the values that are shared by every demanded element.
3189 Known.Zero.setAllBits(); Known.One.setAllBits();
3190 if (!!DemandedLHS) {
3191 SDValue LHS = Op.getOperand(0);
3192 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3193 Known = Known.intersectWith(Known2);
3194 }
3195 // If we don't know any bits, early out.
3196 if (Known.isUnknown())
3197 break;
3198 if (!!DemandedRHS) {
3199 SDValue RHS = Op.getOperand(1);
3200 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3201 Known = Known.intersectWith(Known2);
3202 }
3203 break;
3204 }
3205 case ISD::VSCALE: {
3207 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3208 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3209 break;
3210 }
3211 case ISD::CONCAT_VECTORS: {
3212 if (Op.getValueType().isScalableVector())
3213 break;
3214 // Split DemandedElts and test each of the demanded subvectors.
3215 Known.Zero.setAllBits(); Known.One.setAllBits();
3216 EVT SubVectorVT = Op.getOperand(0).getValueType();
3217 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3218 unsigned NumSubVectors = Op.getNumOperands();
3219 for (unsigned i = 0; i != NumSubVectors; ++i) {
3220 APInt DemandedSub =
3221 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3222 if (!!DemandedSub) {
3223 SDValue Sub = Op.getOperand(i);
3224 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3225 Known = Known.intersectWith(Known2);
3226 }
3227 // If we don't know any bits, early out.
3228 if (Known.isUnknown())
3229 break;
3230 }
3231 break;
3232 }
3233 case ISD::INSERT_SUBVECTOR: {
3234 if (Op.getValueType().isScalableVector())
3235 break;
3236 // Demand any elements from the subvector and the remainder from the src its
3237 // inserted into.
3238 SDValue Src = Op.getOperand(0);
3239 SDValue Sub = Op.getOperand(1);
3240 uint64_t Idx = Op.getConstantOperandVal(2);
3241 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3242 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3243 APInt DemandedSrcElts = DemandedElts;
3244 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
3245
3246 Known.One.setAllBits();
3247 Known.Zero.setAllBits();
3248 if (!!DemandedSubElts) {
3249 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3250 if (Known.isUnknown())
3251 break; // early-out.
3252 }
3253 if (!!DemandedSrcElts) {
3254 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3255 Known = Known.intersectWith(Known2);
3256 }
3257 break;
3258 }
3260 // Offset the demanded elts by the subvector index.
3261 SDValue Src = Op.getOperand(0);
3262 // Bail until we can represent demanded elements for scalable vectors.
3263 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3264 break;
3265 uint64_t Idx = Op.getConstantOperandVal(1);
3266 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3267 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3268 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3269 break;
3270 }
3271 case ISD::SCALAR_TO_VECTOR: {
3272 if (Op.getValueType().isScalableVector())
3273 break;
3274 // We know about scalar_to_vector as much as we know about it source,
3275 // which becomes the first element of otherwise unknown vector.
3276 if (DemandedElts != 1)
3277 break;
3278
3279 SDValue N0 = Op.getOperand(0);
3280 Known = computeKnownBits(N0, Depth + 1);
3281 if (N0.getValueSizeInBits() != BitWidth)
3282 Known = Known.trunc(BitWidth);
3283
3284 break;
3285 }
3286 case ISD::BITCAST: {
3287 if (Op.getValueType().isScalableVector())
3288 break;
3289
3290 SDValue N0 = Op.getOperand(0);
3291 EVT SubVT = N0.getValueType();
3292 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3293
3294 // Ignore bitcasts from unsupported types.
3295 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3296 break;
3297
3298 // Fast handling of 'identity' bitcasts.
3299 if (BitWidth == SubBitWidth) {
3300 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3301 break;
3302 }
3303
3304 bool IsLE = getDataLayout().isLittleEndian();
3305
3306 // Bitcast 'small element' vector to 'large element' scalar/vector.
3307 if ((BitWidth % SubBitWidth) == 0) {
3308 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3309
3310 // Collect known bits for the (larger) output by collecting the known
3311 // bits from each set of sub elements and shift these into place.
3312 // We need to separately call computeKnownBits for each set of
3313 // sub elements as the knownbits for each is likely to be different.
3314 unsigned SubScale = BitWidth / SubBitWidth;
3315 APInt SubDemandedElts(NumElts * SubScale, 0);
3316 for (unsigned i = 0; i != NumElts; ++i)
3317 if (DemandedElts[i])
3318 SubDemandedElts.setBit(i * SubScale);
3319
3320 for (unsigned i = 0; i != SubScale; ++i) {
3321 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3322 Depth + 1);
3323 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3324 Known.insertBits(Known2, SubBitWidth * Shifts);
3325 }
3326 }
3327
3328 // Bitcast 'large element' scalar/vector to 'small element' vector.
3329 if ((SubBitWidth % BitWidth) == 0) {
3330 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3331
3332 // Collect known bits for the (smaller) output by collecting the known
3333 // bits from the overlapping larger input elements and extracting the
3334 // sub sections we actually care about.
3335 unsigned SubScale = SubBitWidth / BitWidth;
3336 APInt SubDemandedElts =
3337 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3338 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3339
3340 Known.Zero.setAllBits(); Known.One.setAllBits();
3341 for (unsigned i = 0; i != NumElts; ++i)
3342 if (DemandedElts[i]) {
3343 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3344 unsigned Offset = (Shifts % SubScale) * BitWidth;
3345 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3346 // If we don't know any bits, early out.
3347 if (Known.isUnknown())
3348 break;
3349 }
3350 }
3351 break;
3352 }
3353 case ISD::AND:
3354 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3355 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3356
3357 Known &= Known2;
3358 break;
3359 case ISD::OR:
3360 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3361 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3362
3363 Known |= Known2;
3364 break;
3365 case ISD::XOR:
3366 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3367 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3368
3369 Known ^= Known2;
3370 break;
3371 case ISD::MUL: {
3372 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3373 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3374 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3375 // TODO: SelfMultiply can be poison, but not undef.
3376 if (SelfMultiply)
3377 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3378 Op.getOperand(0), DemandedElts, false, Depth + 1);
3379 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3380
3381 // If the multiplication is known not to overflow, the product of a number
3382 // with itself is non-negative. Only do this if we didn't already computed
3383 // the opposite value for the sign bit.
3384 if (Op->getFlags().hasNoSignedWrap() &&
3385 Op.getOperand(0) == Op.getOperand(1) &&
3386 !Known.isNegative())
3387 Known.makeNonNegative();
3388 break;
3389 }
3390 case ISD::MULHU: {
3391 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3392 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3393 Known = KnownBits::mulhu(Known, Known2);
3394 break;
3395 }
3396 case ISD::MULHS: {
3397 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3398 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3399 Known = KnownBits::mulhs(Known, Known2);
3400 break;
3401 }
3402 case ISD::ABDU: {
3403 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3404 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3405 Known = KnownBits::abdu(Known, Known2);
3406 break;
3407 }
3408 case ISD::ABDS: {
3409 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3410 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3411 Known = KnownBits::abds(Known, Known2);
3412 break;
3413 }
3414 case ISD::UMUL_LOHI: {
3415 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3416 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3417 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3418 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3419 if (Op.getResNo() == 0)
3420 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3421 else
3422 Known = KnownBits::mulhu(Known, Known2);
3423 break;
3424 }
3425 case ISD::SMUL_LOHI: {
3426 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3427 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3428 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3429 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3430 if (Op.getResNo() == 0)
3431 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3432 else
3433 Known = KnownBits::mulhs(Known, Known2);
3434 break;
3435 }
3436 case ISD::AVGFLOORU:
3437 case ISD::AVGCEILU:
3438 case ISD::AVGFLOORS:
3439 case ISD::AVGCEILS: {
3440 bool IsCeil = Opcode == ISD::AVGCEILU || Opcode == ISD::AVGCEILS;
3441 bool IsSigned = Opcode == ISD::AVGFLOORS || Opcode == ISD::AVGCEILS;
3442 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3443 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3444 Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
3445 Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
3446 KnownBits Carry = KnownBits::makeConstant(APInt(1, IsCeil ? 1 : 0));
3447 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
3448 Known = Known.extractBits(BitWidth, 1);
3449 break;
3450 }
3451 case ISD::SELECT:
3452 case ISD::VSELECT:
3453 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3454 // If we don't know any bits, early out.
3455 if (Known.isUnknown())
3456 break;
3457 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3458
3459 // Only known if known in both the LHS and RHS.
3460 Known = Known.intersectWith(Known2);
3461 break;
3462 case ISD::SELECT_CC:
3463 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3464 // If we don't know any bits, early out.
3465 if (Known.isUnknown())
3466 break;
3467 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3468
3469 // Only known if known in both the LHS and RHS.
3470 Known = Known.intersectWith(Known2);
3471 break;
3472 case ISD::SMULO:
3473 case ISD::UMULO:
3474 if (Op.getResNo() != 1)
3475 break;
3476 // The boolean result conforms to getBooleanContents.
3477 // If we know the result of a setcc has the top bits zero, use this info.
3478 // We know that we have an integer-based boolean since these operations
3479 // are only available for integer.
3480 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3482 BitWidth > 1)
3483 Known.Zero.setBitsFrom(1);
3484 break;
3485 case ISD::SETCC:
3486 case ISD::SETCCCARRY:
3487 case ISD::STRICT_FSETCC:
3488 case ISD::STRICT_FSETCCS: {
3489 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3490 // If we know the result of a setcc has the top bits zero, use this info.
3491 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3493 BitWidth > 1)
3494 Known.Zero.setBitsFrom(1);
3495 break;
3496 }
3497 case ISD::SHL:
3498 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3499 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3500 Known = KnownBits::shl(Known, Known2);
3501
3502 // Minimum shift low bits are known zero.
3503 if (const APInt *ShMinAmt =
3505 Known.Zero.setLowBits(ShMinAmt->getZExtValue());
3506 break;
3507 case ISD::SRL:
3508 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3509 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3510 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3511 Op->getFlags().hasExact());
3512
3513 // Minimum shift high bits are known zero.
3514 if (const APInt *ShMinAmt =
3516 Known.Zero.setHighBits(ShMinAmt->getZExtValue());
3517 break;
3518 case ISD::SRA:
3519 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3520 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3521 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3522 Op->getFlags().hasExact());
3523 break;
3524 case ISD::FSHL:
3525 case ISD::FSHR:
3526 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3527 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3528
3529 // For fshl, 0-shift returns the 1st arg.
3530 // For fshr, 0-shift returns the 2nd arg.
3531 if (Amt == 0) {
3532 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3533 DemandedElts, Depth + 1);
3534 break;
3535 }
3536
3537 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3538 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3539 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3540 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3541 if (Opcode == ISD::FSHL) {
3542 Known.One <<= Amt;
3543 Known.Zero <<= Amt;
3544 Known2.One.lshrInPlace(BitWidth - Amt);
3545 Known2.Zero.lshrInPlace(BitWidth - Amt);
3546 } else {
3547 Known.One <<= BitWidth - Amt;
3548 Known.Zero <<= BitWidth - Amt;
3549 Known2.One.lshrInPlace(Amt);
3550 Known2.Zero.lshrInPlace(Amt);
3551 }
3552 Known = Known.unionWith(Known2);
3553 }
3554 break;
3555 case ISD::SHL_PARTS:
3556 case ISD::SRA_PARTS:
3557 case ISD::SRL_PARTS: {
3558 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3559
3560 // Collect lo/hi source values and concatenate.
3561 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3562 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3563 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3564 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3565 Known = Known2.concat(Known);
3566
3567 // Collect shift amount.
3568 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3569
3570 if (Opcode == ISD::SHL_PARTS)
3571 Known = KnownBits::shl(Known, Known2);
3572 else if (Opcode == ISD::SRA_PARTS)
3573 Known = KnownBits::ashr(Known, Known2);
3574 else // if (Opcode == ISD::SRL_PARTS)
3575 Known = KnownBits::lshr(Known, Known2);
3576
3577 // TODO: Minimum shift low/high bits are known zero.
3578
3579 if (Op.getResNo() == 0)
3580 Known = Known.extractBits(LoBits, 0);
3581 else
3582 Known = Known.extractBits(HiBits, LoBits);
3583 break;
3584 }
3586 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3587 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3588 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3589 break;
3590 }
3591 case ISD::CTTZ:
3592 case ISD::CTTZ_ZERO_UNDEF: {
3593 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3594 // If we have a known 1, its position is our upper bound.
3595 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3596 unsigned LowBits = llvm::bit_width(PossibleTZ);
3597 Known.Zero.setBitsFrom(LowBits);
3598 break;
3599 }
3600 case ISD::CTLZ:
3601 case ISD::CTLZ_ZERO_UNDEF: {
3602 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3603 // If we have a known 1, its position is our upper bound.
3604 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3605 unsigned LowBits = llvm::bit_width(PossibleLZ);
3606 Known.Zero.setBitsFrom(LowBits);
3607 break;
3608 }
3609 case ISD::CTPOP: {
3610 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3611 // If we know some of the bits are zero, they can't be one.
3612 unsigned PossibleOnes = Known2.countMaxPopulation();
3613 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3614 break;
3615 }
3616 case ISD::PARITY: {
3617 // Parity returns 0 everywhere but the LSB.
3618 Known.Zero.setBitsFrom(1);
3619 break;
3620 }
3621 case ISD::LOAD: {
3622 LoadSDNode *LD = cast<LoadSDNode>(Op);
3623 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3624 if (ISD::isNON_EXTLoad(LD) && Cst) {
3625 // Determine any common known bits from the loaded constant pool value.
3626 Type *CstTy = Cst->getType();
3627 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3628 !Op.getValueType().isScalableVector()) {
3629 // If its a vector splat, then we can (quickly) reuse the scalar path.
3630 // NOTE: We assume all elements match and none are UNDEF.
3631 if (CstTy->isVectorTy()) {
3632 if (const Constant *Splat = Cst->getSplatValue()) {
3633 Cst = Splat;
3634 CstTy = Cst->getType();
3635 }
3636 }
3637 // TODO - do we need to handle different bitwidths?
3638 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3639 // Iterate across all vector elements finding common known bits.
3640 Known.One.setAllBits();
3641 Known.Zero.setAllBits();
3642 for (unsigned i = 0; i != NumElts; ++i) {
3643 if (!DemandedElts[i])
3644 continue;
3645 if (Constant *Elt = Cst->getAggregateElement(i)) {
3646 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3647 const APInt &Value = CInt->getValue();
3648 Known.One &= Value;
3649 Known.Zero &= ~Value;
3650 continue;
3651 }
3652 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3653 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3654 Known.One &= Value;
3655 Known.Zero &= ~Value;
3656 continue;
3657 }
3658 }
3659 Known.One.clearAllBits();
3660 Known.Zero.clearAllBits();
3661 break;
3662 }
3663 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3664 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3665 Known = KnownBits::makeConstant(CInt->getValue());
3666 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3667 Known =
3668 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3669 }
3670 }
3671 }
3672 } else if (Op.getResNo() == 0) {
3673 KnownBits Known0(!LD->getMemoryVT().isScalableVT()
3674 ? LD->getMemoryVT().getFixedSizeInBits()
3675 : BitWidth);
3676 EVT VT = Op.getValueType();
3677 // Fill in any known bits from range information. There are 3 types being
3678 // used. The results VT (same vector elt size as BitWidth), the loaded
3679 // MemoryVT (which may or may not be vector) and the range VTs original
3680 // type. The range matadata needs the full range (i.e
3681 // MemoryVT().getSizeInBits()), which is truncated to the correct elt size
3682 // if it is know. These are then extended to the original VT sizes below.
3683 if (const MDNode *MD = LD->getRanges()) {
3685 if (VT.isVector()) {
3686 // Handle truncation to the first demanded element.
3687 // TODO: Figure out which demanded elements are covered
3688 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
3689 break;
3690 Known0 = Known0.trunc(BitWidth);
3691 }
3692 }
3693
3694 if (LD->getMemoryVT().isVector())
3695 Known0 = Known0.trunc(LD->getMemoryVT().getScalarSizeInBits());
3696
3697 // Extend the Known bits from memory to the size of the result.
3698 if (ISD::isZEXTLoad(Op.getNode()))
3699 Known = Known0.zext(BitWidth);
3700 else if (ISD::isSEXTLoad(Op.getNode()))
3701 Known = Known0.sext(BitWidth);
3702 else if (ISD::isEXTLoad(Op.getNode()))
3703 Known = Known0.anyext(BitWidth);
3704 else
3705 Known = Known0;
3706 assert(Known.getBitWidth() == BitWidth);
3707 return Known;
3708 }
3709 break;
3710 }
3712 if (Op.getValueType().isScalableVector())
3713 break;
3714 EVT InVT = Op.getOperand(0).getValueType();
3715 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3716 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3717 Known = Known.zext(BitWidth);
3718 break;
3719 }
3720 case ISD::ZERO_EXTEND: {
3721 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3722 Known = Known.zext(BitWidth);
3723 break;
3724 }
3726 if (Op.getValueType().isScalableVector())
3727 break;
3728 EVT InVT = Op.getOperand(0).getValueType();
3729 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3730 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3731 // If the sign bit is known to be zero or one, then sext will extend
3732 // it to the top bits, else it will just zext.
3733 Known = Known.sext(BitWidth);
3734 break;
3735 }
3736 case ISD::SIGN_EXTEND: {
3737 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3738 // If the sign bit is known to be zero or one, then sext will extend
3739 // it to the top bits, else it will just zext.
3740 Known = Known.sext(BitWidth);
3741 break;
3742 }
3744 if (Op.getValueType().isScalableVector())
3745 break;
3746 EVT InVT = Op.getOperand(0).getValueType();
3747 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3748 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3749 Known = Known.anyext(BitWidth);
3750 break;
3751 }
3752 case ISD::ANY_EXTEND: {
3753 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3754 Known = Known.anyext(BitWidth);
3755 break;
3756 }
3757 case ISD::TRUNCATE: {
3758 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3759 Known = Known.trunc(BitWidth);
3760 break;
3761 }
3762 case ISD::AssertZext: {
3763 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3765 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3766 Known.Zero |= (~InMask);
3767 Known.One &= (~Known.Zero);
3768 break;
3769 }
3770 case ISD::AssertAlign: {
3771 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
3772 assert(LogOfAlign != 0);
3773
3774 // TODO: Should use maximum with source
3775 // If a node is guaranteed to be aligned, set low zero bits accordingly as
3776 // well as clearing one bits.
3777 Known.Zero.setLowBits(LogOfAlign);
3778 Known.One.clearLowBits(LogOfAlign);
3779 break;
3780 }
3781 case ISD::FGETSIGN:
3782 // All bits are zero except the low bit.
3783 Known.Zero.setBitsFrom(1);
3784 break;
3785 case ISD::ADD:
3786 case ISD::SUB: {
3787 SDNodeFlags Flags = Op.getNode()->getFlags();
3788 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3789 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3791 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
3792 Flags.hasNoUnsignedWrap(), Known, Known2);
3793 break;
3794 }
3795 case ISD::USUBO:
3796 case ISD::SSUBO:
3797 case ISD::USUBO_CARRY:
3798 case ISD::SSUBO_CARRY:
3799 if (Op.getResNo() == 1) {
3800 // If we know the result of a setcc has the top bits zero, use this info.
3801 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3803 BitWidth > 1)
3804 Known.Zero.setBitsFrom(1);
3805 break;
3806 }
3807 [[fallthrough]];
3808 case ISD::SUBC: {
3809 assert(Op.getResNo() == 0 &&
3810 "We only compute knownbits for the difference here.");
3811
3812 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
3813 KnownBits Borrow(1);
3814 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
3815 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3816 // Borrow has bit width 1
3817 Borrow = Borrow.trunc(1);
3818 } else {
3819 Borrow.setAllZero();
3820 }
3821
3822 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3823 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3824 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
3825 break;
3826 }
3827 case ISD::UADDO:
3828 case ISD::SADDO:
3829 case ISD::UADDO_CARRY:
3830 case ISD::SADDO_CARRY:
3831 if (Op.getResNo() == 1) {
3832 // If we know the result of a setcc has the top bits zero, use this info.
3833 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3835 BitWidth > 1)
3836 Known.Zero.setBitsFrom(1);
3837 break;
3838 }
3839 [[fallthrough]];
3840 case ISD::ADDC:
3841 case ISD::ADDE: {
3842 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
3843
3844 // With ADDE and UADDO_CARRY, a carry bit may be added in.
3845 KnownBits Carry(1);
3846 if (Opcode == ISD::ADDE)
3847 // Can't track carry from glue, set carry to unknown.
3848 Carry.resetAll();
3849 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
3850 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3851 // Carry has bit width 1
3852 Carry = Carry.trunc(1);
3853 } else {
3854 Carry.setAllZero();
3855 }
3856
3857 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3858 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3859 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
3860 break;
3861 }
3862 case ISD::UDIV: {
3863 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3864 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3865 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
3866 break;
3867 }
3868 case ISD::SDIV: {
3869 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3870 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3871 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
3872 break;
3873 }
3874 case ISD::SREM: {
3875 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3876 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3877 Known = KnownBits::srem(Known, Known2);
3878 break;
3879 }
3880 case ISD::UREM: {
3881 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3882 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3883 Known = KnownBits::urem(Known, Known2);
3884 break;
3885 }
3886 case ISD::EXTRACT_ELEMENT: {
3887 Known = computeKnownBits(Op.getOperand(0), Depth+1);
3888 const unsigned Index = Op.getConstantOperandVal(1);
3889 const unsigned EltBitWidth = Op.getValueSizeInBits();
3890
3891 // Remove low part of known bits mask
3892 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
3893 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
3894
3895 // Remove high part of known bit mask
3896 Known = Known.trunc(EltBitWidth);
3897 break;
3898 }
3900 SDValue InVec = Op.getOperand(0);
3901 SDValue EltNo = Op.getOperand(1);
3902 EVT VecVT = InVec.getValueType();
3903 // computeKnownBits not yet implemented for scalable vectors.
3904 if (VecVT.isScalableVector())
3905 break;
3906 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
3907 const unsigned NumSrcElts = VecVT.getVectorNumElements();
3908
3909 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
3910 // anything about the extended bits.
3911 if (BitWidth > EltBitWidth)
3912 Known = Known.trunc(EltBitWidth);
3913
3914 // If we know the element index, just demand that vector element, else for
3915 // an unknown element index, ignore DemandedElts and demand them all.
3916 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
3917 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
3918 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
3919 DemandedSrcElts =
3920 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
3921
3922 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
3923 if (BitWidth > EltBitWidth)
3924 Known = Known.anyext(BitWidth);
3925 break;
3926 }
3928 if (Op.getValueType().isScalableVector())
3929 break;
3930
3931 // If we know the element index, split the demand between the
3932 // source vector and the inserted element, otherwise assume we need
3933 // the original demanded vector elements and the value.
3934 SDValue InVec = Op.getOperand(0);
3935 SDValue InVal = Op.getOperand(1);
3936 SDValue EltNo = Op.getOperand(2);
3937 bool DemandedVal = true;
3938 APInt DemandedVecElts = DemandedElts;
3939 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
3940 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
3941 unsigned EltIdx = CEltNo->getZExtValue();
3942 DemandedVal = !!DemandedElts[EltIdx];
3943 DemandedVecElts.clearBit(EltIdx);
3944 }
3945 Known.One.setAllBits();
3946 Known.Zero.setAllBits();
3947 if (DemandedVal) {
3948 Known2 = computeKnownBits(InVal, Depth + 1);
3949 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
3950 }
3951 if (!!DemandedVecElts) {
3952 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
3953 Known = Known.intersectWith(Known2);
3954 }
3955 break;
3956 }
3957 case ISD::BITREVERSE: {
3958 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3959 Known = Known2.reverseBits();
3960 break;
3961 }
3962 case ISD::BSWAP: {
3963 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3964 Known = Known2.byteSwap();
3965 break;
3966 }
3967 case ISD::ABS: {
3968 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3969 Known = Known2.abs();
3970 break;
3971 }
3972 case ISD::USUBSAT: {
3973 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3974 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3975 Known = KnownBits::usub_sat(Known, Known2);
3976 break;
3977 }
3978 case ISD::UMIN: {
3979 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3980 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3981 Known = KnownBits::umin(Known, Known2);
3982 break;
3983 }
3984 case ISD::UMAX: {
3985 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3986 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3987 Known = KnownBits::umax(Known, Known2);
3988 break;
3989 }
3990 case ISD::SMIN:
3991 case ISD::SMAX: {
3992 // If we have a clamp pattern, we know that the number of sign bits will be
3993 // the minimum of the clamp min/max range.
3994 bool IsMax = (Opcode == ISD::SMAX);
3995 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
3996 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
3997 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
3998 CstHigh =
3999 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4000 if (CstLow && CstHigh) {
4001 if (!IsMax)
4002 std::swap(CstLow, CstHigh);
4003
4004 const APInt &ValueLow = CstLow->getAPIntValue();
4005 const APInt &ValueHigh = CstHigh->getAPIntValue();
4006 if (ValueLow.sle(ValueHigh)) {
4007 unsigned LowSignBits = ValueLow.getNumSignBits();
4008 unsigned HighSignBits = ValueHigh.getNumSignBits();
4009 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4010 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4011 Known.One.setHighBits(MinSignBits);
4012 break;
4013 }
4014 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4015 Known.Zero.setHighBits(MinSignBits);
4016 break;
4017 }
4018 }
4019 }
4020
4021 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4022 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4023 if (IsMax)
4024 Known = KnownBits::smax(Known, Known2);
4025 else
4026 Known = KnownBits::smin(Known, Known2);
4027
4028 // For SMAX, if CstLow is non-negative we know the result will be
4029 // non-negative and thus all sign bits are 0.
4030 // TODO: There's an equivalent of this for smin with negative constant for
4031 // known ones.
4032 if (IsMax && CstLow) {
4033 const APInt &ValueLow = CstLow->getAPIntValue();
4034 if (ValueLow.isNonNegative()) {
4035 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4036 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4037 }
4038 }
4039
4040 break;
4041 }
4042 case ISD::UINT_TO_FP: {
4043 Known.makeNonNegative();
4044 break;
4045 }
4046 case ISD::SINT_TO_FP: {
4047 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4048 if (Known2.isNonNegative())
4049 Known.makeNonNegative();
4050 else if (Known2.isNegative())
4051 Known.makeNegative();
4052 break;
4053 }
4054 case ISD::FP_TO_UINT_SAT: {
4055 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4056 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4058 break;
4059 }
4061 if (Op.getResNo() == 1) {
4062 // The boolean result conforms to getBooleanContents.
4063 // If we know the result of a setcc has the top bits zero, use this info.
4064 // We know that we have an integer-based boolean since these operations
4065 // are only available for integer.
4066 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4068 BitWidth > 1)
4069 Known.Zero.setBitsFrom(1);
4070 break;
4071 }
4072 [[fallthrough]];
4074 case ISD::ATOMIC_SWAP:
4086 case ISD::ATOMIC_LOAD: {
4087 unsigned MemBits =
4088 cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
4089 // If we are looking at the loaded value.
4090 if (Op.getResNo() == 0) {
4092 Known.Zero.setBitsFrom(MemBits);
4093 else if (Op->getOpcode() == ISD::ATOMIC_LOAD &&
4094 cast<AtomicSDNode>(Op)->getExtensionType() == ISD::ZEXTLOAD)
4095 Known.Zero.setBitsFrom(MemBits);
4096 }
4097 break;
4098 }
4099 case ISD::FrameIndex:
4101 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4102 Known, getMachineFunction());
4103 break;
4104
4105 default:
4106 if (Opcode < ISD::BUILTIN_OP_END)
4107 break;
4108 [[fallthrough]];
4112 // TODO: Probably okay to remove after audit; here to reduce change size
4113 // in initial enablement patch for scalable vectors
4114 if (Op.getValueType().isScalableVector())
4115 break;
4116
4117 // Allow the target to implement this method for its nodes.
4118 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4119 break;
4120 }
4121
4122 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
4123 return Known;
4124}
4125
4126/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4128 switch (OR) {
4136 }
4137 llvm_unreachable("Unknown OverflowResult");
4138}
4139
4142 // X + 0 never overflow
4143 if (isNullConstant(N1))
4144 return OFK_Never;
4145
4146 // If both operands each have at least two sign bits, the addition
4147 // cannot overflow.
4148 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4149 return OFK_Never;
4150
4151 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4152 return OFK_Sometime;
4153}
4154
4157 // X + 0 never overflow
4158 if (isNullConstant(N1))
4159 return OFK_Never;
4160
4161 // mulhi + 1 never overflow
4162 KnownBits N1Known = computeKnownBits(N1);
4163 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4164 N1Known.getMaxValue().ult(2))
4165 return OFK_Never;
4166
4167 KnownBits N0Known = computeKnownBits(N0);
4168 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4169 N0Known.getMaxValue().ult(2))
4170 return OFK_Never;
4171
4172 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4173 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4174 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4175 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4176}
4177
4180 // X - 0 never overflow
4181 if (isNullConstant(N1))
4182 return OFK_Never;
4183
4184 // If both operands each have at least two sign bits, the subtraction
4185 // cannot overflow.
4186 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4187 return OFK_Never;
4188
4189 KnownBits N0Known = computeKnownBits(N0);
4190 KnownBits N1Known = computeKnownBits(N1);
4191 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4192 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4193 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4194}
4195
4198 // X - 0 never overflow
4199 if (isNullConstant(N1))
4200 return OFK_Never;
4201
4202 KnownBits N0Known = computeKnownBits(N0);
4203 KnownBits N1Known = computeKnownBits(N1);
4204 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4205 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4206 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4207}
4208
4211 // X * 0 and X * 1 never overflow.
4212 if (isNullConstant(N1) || isOneConstant(N1))
4213 return OFK_Never;
4214
4215 KnownBits N0Known = computeKnownBits(N0);
4216 KnownBits N1Known = computeKnownBits(N1);
4217 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4218 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4219 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4220}
4221
4224 // X * 0 and X * 1 never overflow.
4225 if (isNullConstant(N1) || isOneConstant(N1))
4226 return OFK_Never;
4227
4228 // Get the size of the result.
4229 unsigned BitWidth = N0.getScalarValueSizeInBits();
4230
4231 // Sum of the sign bits.
4232 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4233
4234 // If we have enough sign bits, then there's no overflow.
4235 if (SignBits > BitWidth + 1)
4236 return OFK_Never;
4237
4238 if (SignBits == BitWidth + 1) {
4239 // The overflow occurs when the true multiplication of the
4240 // the operands is the minimum negative number.
4241 KnownBits N0Known = computeKnownBits(N0);
4242 KnownBits N1Known = computeKnownBits(N1);
4243 // If one of the operands is non-negative, then there's no
4244 // overflow.
4245 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4246 return OFK_Never;
4247 }
4248
4249 return OFK_Sometime;
4250}
4251
4253 if (Depth >= MaxRecursionDepth)
4254 return false; // Limit search depth.
4255
4256 EVT OpVT = Val.getValueType();
4257 unsigned BitWidth = OpVT.getScalarSizeInBits();
4258
4259 // Is the constant a known power of 2?
4261 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4262 }))
4263 return true;
4264
4265 // A left-shift of a constant one will have exactly one bit set because
4266 // shifting the bit off the end is undefined.
4267 if (Val.getOpcode() == ISD::SHL) {
4268 auto *C = isConstOrConstSplat(Val.getOperand(0));
4269 if (C && C->getAPIntValue() == 1)
4270 return true;
4271 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4272 isKnownNeverZero(Val, Depth);
4273 }
4274
4275 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4276 // one bit set.
4277 if (Val.getOpcode() == ISD::SRL) {
4278 auto *C = isConstOrConstSplat(Val.getOperand(0));
4279 if (C && C->getAPIntValue().isSignMask())
4280 return true;
4281 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4282 isKnownNeverZero(Val, Depth);
4283 }
4284
4285 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4286 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4287
4288 // Are all operands of a build vector constant powers of two?
4289 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4290 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4291 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4292 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4293 return false;
4294 }))
4295 return true;
4296
4297 // Is the operand of a splat vector a constant power of two?
4298 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4299 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val->getOperand(0)))
4300 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4301 return true;
4302
4303 // vscale(power-of-two) is a power-of-two for some targets
4304 if (Val.getOpcode() == ISD::VSCALE &&
4305 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4307 return true;
4308
4309 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4310 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4311 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4313
4314 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4315 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4317
4318 // Looking for `x & -x` pattern:
4319 // If x == 0:
4320 // x & -x -> 0
4321 // If x != 0:
4322 // x & -x -> non-zero pow2
4323 // so if we find the pattern return whether we know `x` is non-zero.
4324 SDValue X;
4325 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4326 return isKnownNeverZero(X, Depth);
4327
4328 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4329 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4330
4331 // More could be done here, though the above checks are enough
4332 // to handle some common cases.
4333 return false;
4334}
4335
4337 EVT VT = Op.getValueType();
4338
4339 // Since the number of lanes in a scalable vector is unknown at compile time,
4340 // we track one bit which is implicitly broadcast to all lanes. This means
4341 // that all lanes in a scalable vector are considered demanded.
4342 APInt DemandedElts = VT.isFixedLengthVector()
4344 : APInt(1, 1);
4345 return ComputeNumSignBits(Op, DemandedElts, Depth);
4346}
4347
4348unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4349 unsigned Depth) const {
4350 EVT VT = Op.getValueType();
4351 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4352 unsigned VTBits = VT.getScalarSizeInBits();
4353 unsigned NumElts = DemandedElts.getBitWidth();
4354 unsigned Tmp, Tmp2;
4355 unsigned FirstAnswer = 1;
4356
4357 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4358 const APInt &Val = C->getAPIntValue();
4359 return Val.getNumSignBits();
4360 }
4361
4362 if (Depth >= MaxRecursionDepth)
4363 return 1; // Limit search depth.
4364
4365 if (!DemandedElts)
4366 return 1; // No demanded elts, better to assume we don't know anything.
4367
4368 unsigned Opcode = Op.getOpcode();
4369 switch (Opcode) {
4370 default: break;
4371 case ISD::AssertSext:
4372 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4373 return VTBits-Tmp+1;
4374 case ISD::AssertZext:
4375 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4376 return VTBits-Tmp;
4377 case ISD::MERGE_VALUES:
4378 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4379 Depth + 1);
4380 case ISD::SPLAT_VECTOR: {
4381 // Check if the sign bits of source go down as far as the truncated value.
4382 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4383 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4384 if (NumSrcSignBits > (NumSrcBits - VTBits))
4385 return NumSrcSignBits - (NumSrcBits - VTBits);
4386 break;
4387 }
4388 case ISD::BUILD_VECTOR:
4389 assert(!VT.isScalableVector());
4390 Tmp = VTBits;
4391 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4392 if (!DemandedElts[i])
4393 continue;
4394
4395 SDValue SrcOp = Op.getOperand(i);
4396 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4397 // for constant nodes to ensure we only look at the sign bits.
4398 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SrcOp)) {
4399 APInt T = C->getAPIntValue().trunc(VTBits);
4400 Tmp2 = T.getNumSignBits();
4401 } else {
4402 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4403
4404 if (SrcOp.getValueSizeInBits() != VTBits) {
4405 assert(SrcOp.getValueSizeInBits() > VTBits &&
4406 "Expected BUILD_VECTOR implicit truncation");
4407 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4408 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4409 }
4410 }
4411 Tmp = std::min(Tmp, Tmp2);
4412 }
4413 return Tmp;
4414
4415 case ISD::VECTOR_SHUFFLE: {
4416 // Collect the minimum number of sign bits that are shared by every vector
4417 // element referenced by the shuffle.
4418 APInt DemandedLHS, DemandedRHS;
4419 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
4420 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4421 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4422 DemandedLHS, DemandedRHS))
4423 return 1;
4424
4425 Tmp = std::numeric_limits<unsigned>::max();
4426 if (!!DemandedLHS)
4427 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4428 if (!!DemandedRHS) {
4429 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4430 Tmp = std::min(Tmp, Tmp2);
4431 }
4432 // If we don't know anything, early out and try computeKnownBits fall-back.
4433 if (Tmp == 1)
4434 break;
4435 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4436 return Tmp;
4437 }
4438
4439 case ISD::BITCAST: {
4440 if (VT.isScalableVector())
4441 break;
4442 SDValue N0 = Op.getOperand(0);
4443 EVT SrcVT = N0.getValueType();
4444 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4445
4446 // Ignore bitcasts from unsupported types..
4447 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4448 break;
4449
4450 // Fast handling of 'identity' bitcasts.
4451 if (VTBits == SrcBits)
4452 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4453
4454 bool IsLE = getDataLayout().isLittleEndian();
4455
4456 // Bitcast 'large element' scalar/vector to 'small element' vector.
4457 if ((SrcBits % VTBits) == 0) {
4458 assert(VT.isVector() && "Expected bitcast to vector");
4459
4460 unsigned Scale = SrcBits / VTBits;
4461 APInt SrcDemandedElts =
4462 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4463
4464 // Fast case - sign splat can be simply split across the small elements.
4465 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4466 if (Tmp == SrcBits)
4467 return VTBits;
4468
4469 // Slow case - determine how far the sign extends into each sub-element.
4470 Tmp2 = VTBits;
4471 for (unsigned i = 0; i != NumElts; ++i)
4472 if (DemandedElts[i]) {
4473 unsigned SubOffset = i % Scale;
4474 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4475 SubOffset = SubOffset * VTBits;
4476 if (Tmp <= SubOffset)
4477 return 1;
4478 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4479 }
4480 return Tmp2;
4481 }
4482 break;
4483 }
4484
4486 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4487 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4488 return VTBits - Tmp + 1;
4489 case ISD::SIGN_EXTEND:
4490 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4491 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4493 // Max of the input and what this extends.
4494 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4495 Tmp = VTBits-Tmp+1;
4496 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4497 return std::max(Tmp, Tmp2);
4499 if (VT.isScalableVector())
4500 break;
4501 SDValue Src = Op.getOperand(0);
4502 EVT SrcVT = Src.getValueType();
4503 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4504 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4505 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4506 }
4507 case ISD::SRA:
4508 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4509 // SRA X, C -> adds C sign bits.
4510 if (const APInt *ShAmt =
4512 Tmp = std::min<uint64_t>(Tmp + ShAmt->getZExtValue(), VTBits);
4513 return Tmp;
4514 case ISD::SHL:
4515 if (const APInt *ShAmt =
4516 getValidMaximumShiftAmountConstant(Op, DemandedElts)) {
4517 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4518 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4519 if (ShAmt->ult(Tmp))
4520 return Tmp - ShAmt->getZExtValue();
4521 }
4522 break;
4523 case ISD::AND:
4524 case ISD::OR:
4525 case ISD::XOR: // NOT is handled here.
4526 // Logical binary ops preserve the number of sign bits at the worst.
4527 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4528 if (Tmp != 1) {
4529 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4530 FirstAnswer = std::min(Tmp, Tmp2);
4531 // We computed what we know about the sign bits as our first
4532 // answer. Now proceed to the generic code that uses
4533 // computeKnownBits, and pick whichever answer is better.
4534 }
4535 break;
4536
4537 case ISD::SELECT:
4538 case ISD::VSELECT:
4539 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4540 if (Tmp == 1) return 1; // Early out.
4541 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4542 return std::min(Tmp, Tmp2);
4543 case ISD::SELECT_CC:
4544 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4545 if (Tmp == 1) return 1; // Early out.
4546 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4547 return std::min(Tmp, Tmp2);
4548
4549 case ISD::SMIN:
4550 case ISD::SMAX: {
4551 // If we have a clamp pattern, we know that the number of sign bits will be
4552 // the minimum of the clamp min/max range.
4553 bool IsMax = (Opcode == ISD::SMAX);
4554 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4555 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4556 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4557 CstHigh =
4558 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4559 if (CstLow && CstHigh) {
4560 if (!IsMax)
4561 std::swap(CstLow, CstHigh);
4562 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4563 Tmp = CstLow->getAPIntValue().getNumSignBits();
4564 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4565 return std::min(Tmp, Tmp2);
4566 }
4567 }
4568
4569 // Fallback - just get the minimum number of sign bits of the operands.
4570 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4571 if (Tmp == 1)
4572 return 1; // Early out.
4573 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4574 return std::min(Tmp, Tmp2);
4575 }
4576 case ISD::UMIN:
4577 case ISD::UMAX:
4578 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4579 if (Tmp == 1)
4580 return 1; // Early out.
4581 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4582 return std::min(Tmp, Tmp2);
4583 case ISD::SADDO:
4584 case ISD::UADDO:
4585 case ISD::SADDO_CARRY:
4586 case ISD::UADDO_CARRY:
4587 case ISD::SSUBO:
4588 case ISD::USUBO:
4589 case ISD::SSUBO_CARRY:
4590 case ISD::USUBO_CARRY:
4591 case ISD::SMULO:
4592 case ISD::UMULO:
4593 if (Op.getResNo() != 1)
4594 break;
4595 // The boolean result conforms to getBooleanContents. Fall through.
4596 // If setcc returns 0/-1, all bits are sign bits.
4597 // We know that we have an integer-based boolean since these operations
4598 // are only available for integer.
4599 if (TLI->getBooleanContents(VT.isVector(), false) ==
4601 return VTBits;
4602 break;
4603 case ISD::SETCC:
4604 case ISD::SETCCCARRY:
4605 case ISD::STRICT_FSETCC:
4606 case ISD::STRICT_FSETCCS: {
4607 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
4608 // If setcc returns 0/-1, all bits are sign bits.
4609 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
4611 return VTBits;
4612 break;
4613 }
4614 case ISD::ROTL:
4615 case ISD::ROTR:
4616 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4617
4618 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
4619 if (Tmp == VTBits)
4620 return VTBits;
4621
4622 if (ConstantSDNode *C =
4623 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
4624 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
4625
4626 // Handle rotate right by N like a rotate left by 32-N.
4627 if (Opcode == ISD::ROTR)
4628 RotAmt = (VTBits - RotAmt) % VTBits;
4629
4630 // If we aren't rotating out all of the known-in sign bits, return the
4631 // number that are left. This handles rotl(sext(x), 1) for example.
4632 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
4633 }
4634 break;
4635 case ISD::ADD:
4636 case ISD::ADDC:
4637 // Add can have at most one carry bit. Thus we know that the output
4638 // is, at worst, one more bit than the inputs.
4639 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4640 if (Tmp == 1) return 1; // Early out.
4641
4642 // Special case decrementing a value (ADD X, -1):
4643 if (ConstantSDNode *CRHS =
4644 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
4645 if (CRHS->isAllOnes()) {
4646 KnownBits Known =
4647 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4648
4649 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4650 // sign bits set.
4651 if ((Known.Zero | 1).isAllOnes())
4652 return VTBits;
4653
4654 // If we are subtracting one from a positive number, there is no carry
4655 // out of the result.
4656 if (Known.isNonNegative())
4657 return Tmp;
4658 }
4659
4660 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4661 if (Tmp2 == 1) return 1; // Early out.
4662 return std::min(Tmp, Tmp2) - 1;
4663 case ISD::SUB:
4664 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4665 if (Tmp2 == 1) return 1; // Early out.
4666
4667 // Handle NEG.
4668 if (ConstantSDNode *CLHS =
4669 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
4670 if (CLHS->isZero()) {
4671 KnownBits Known =
4672 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4673 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4674 // sign bits set.
4675 if ((Known.Zero | 1).isAllOnes())
4676 return VTBits;
4677
4678 // If the input is known to be positive (the sign bit is known clear),
4679 // the output of the NEG has the same number of sign bits as the input.
4680 if (Known.isNonNegative())
4681 return Tmp2;
4682
4683 // Otherwise, we treat this like a SUB.
4684 }
4685
4686 // Sub can have at most one carry bit. Thus we know that the output
4687 // is, at worst, one more bit than the inputs.
4688 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4689 if (Tmp == 1) return 1; // Early out.
4690 return std::min(Tmp, Tmp2) - 1;
4691 case ISD::MUL: {
4692 // The output of the Mul can be at most twice the valid bits in the inputs.
4693 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4694 if (SignBitsOp0 == 1)
4695 break;
4696 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
4697 if (SignBitsOp1 == 1)
4698 break;
4699 unsigned OutValidBits =
4700 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
4701 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
4702 }
4703 case ISD::SREM:
4704 // The sign bit is the LHS's sign bit, except when the result of the
4705 // remainder is zero. The magnitude of the result should be less than or
4706 // equal to the magnitude of the LHS. Therefore, the result should have
4707 // at least as many sign bits as the left hand side.
4708 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4709 case ISD::TRUNCATE: {
4710 // Check if the sign bits of source go down as far as the truncated value.
4711 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
4712 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4713 if (NumSrcSignBits > (NumSrcBits - VTBits))
4714 return NumSrcSignBits - (NumSrcBits - VTBits);
4715 break;
4716 }
4717 case ISD::EXTRACT_ELEMENT: {
4718 if (VT.isScalableVector())
4719 break;
4720 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
4721 const int BitWidth = Op.getValueSizeInBits();
4722 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
4723
4724 // Get reverse index (starting from 1), Op1 value indexes elements from
4725 // little end. Sign starts at big end.
4726 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
4727
4728 // If the sign portion ends in our element the subtraction gives correct
4729 // result. Otherwise it gives either negative or > bitwidth result
4730 return std::clamp(KnownSign - rIndex * BitWidth, 0, BitWidth);
4731 }
4733 if (VT.isScalableVector())
4734 break;
4735 // If we know the element index, split the demand between the
4736 // source vector and the inserted element, otherwise assume we need
4737 // the original demanded vector elements and the value.
4738 SDValue InVec = Op.getOperand(0);
4739 SDValue InVal = Op.getOperand(1);
4740 SDValue EltNo = Op.getOperand(2);
4741 bool DemandedVal = true;
4742 APInt DemandedVecElts = DemandedElts;
4743 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4744 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4745 unsigned EltIdx = CEltNo->getZExtValue();
4746 DemandedVal = !!DemandedElts[EltIdx];
4747 DemandedVecElts.clearBit(EltIdx);
4748 }
4749 Tmp = std::numeric_limits<unsigned>::max();
4750 if (DemandedVal) {
4751 // TODO - handle implicit truncation of inserted elements.
4752 if (InVal.getScalarValueSizeInBits() != VTBits)
4753 break;
4754 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
4755 Tmp = std::min(Tmp, Tmp2);
4756 }
4757 if (!!DemandedVecElts) {
4758 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
4759 Tmp = std::min(Tmp, Tmp2);
4760 }
4761 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4762 return Tmp;
4763 }
4765 assert(!VT.isScalableVector());
4766 SDValue InVec = Op.getOperand(0);
4767 SDValue EltNo = Op.getOperand(1);
4768 EVT VecVT = InVec.getValueType();
4769 // ComputeNumSignBits not yet implemented for scalable vectors.
4770 if (VecVT.isScalableVector())
4771 break;
4772 const unsigned BitWidth = Op.getValueSizeInBits();
4773 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
4774 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4775
4776 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
4777 // anything about sign bits. But if the sizes match we can derive knowledge
4778 // about sign bits from the vector operand.
4779 if (BitWidth != EltBitWidth)
4780 break;
4781
4782 // If we know the element index, just demand that vector element, else for
4783 // an unknown element index, ignore DemandedElts and demand them all.
4784 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4785 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4786 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4787 DemandedSrcElts =
4788 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4789
4790 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
4791 }
4793 // Offset the demanded elts by the subvector index.
4794 SDValue Src = Op.getOperand(0);
4795 // Bail until we can represent demanded elements for scalable vectors.
4796 if (Src.getValueType().isScalableVector())
4797 break;
4798 uint64_t Idx = Op.getConstantOperandVal(1);
4799 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
4800 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
4801 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
4802 }
4803 case ISD::CONCAT_VECTORS: {
4804 if (VT.isScalableVector())
4805 break;
4806 // Determine the minimum number of sign bits across all demanded
4807 // elts of the input vectors. Early out if the result is already 1.
4808 Tmp = std::numeric_limits<unsigned>::max();
4809 EVT SubVectorVT = Op.getOperand(0).getValueType();
4810 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
4811 unsigned NumSubVectors = Op.getNumOperands();
4812 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
4813 APInt DemandedSub =
4814 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
4815 if (!DemandedSub)
4816 continue;
4817 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
4818 Tmp = std::min(Tmp, Tmp2);
4819 }
4820 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4821 return Tmp;
4822 }
4823 case ISD::INSERT_SUBVECTOR: {
4824 if (VT.isScalableVector())
4825 break;
4826 // Demand any elements from the subvector and the remainder from the src its
4827 // inserted into.
4828 SDValue Src = Op.getOperand(0);
4829 SDValue Sub = Op.getOperand(1);
4830 uint64_t Idx = Op.getConstantOperandVal(2);
4831 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
4832 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
4833 APInt DemandedSrcElts = DemandedElts;
4834 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
4835
4836 Tmp = std::numeric_limits<unsigned>::max();
4837 if (!!DemandedSubElts) {
4838 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
4839 if (Tmp == 1)
4840 return 1; // early-out
4841 }
4842 if (!!DemandedSrcElts) {
4843 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
4844 Tmp = std::min(Tmp, Tmp2);
4845 }
4846 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4847 return Tmp;
4848 }
4849 case ISD::LOAD: {
4850 LoadSDNode *LD = cast<LoadSDNode>(Op);
4851 if (const MDNode *Ranges = LD->getRanges()) {
4852 if (DemandedElts != 1)
4853 break;
4854
4856 if (VTBits > CR.getBitWidth()) {
4857 switch (LD->getExtensionType()) {
4858 case ISD::SEXTLOAD:
4859 CR = CR.signExtend(VTBits);
4860 break;
4861 case ISD::ZEXTLOAD:
4862 CR = CR.zeroExtend(VTBits);
4863 break;
4864 default:
4865 break;
4866 }
4867 }
4868
4869 if (VTBits != CR.getBitWidth())
4870 break;
4871 return std::min(CR.getSignedMin().getNumSignBits(),
4873 }
4874
4875 break;
4876 }
4879 case ISD::ATOMIC_SWAP:
4891 case ISD::ATOMIC_LOAD: {
4892 Tmp = cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
4893 // If we are looking at the loaded value.
4894 if (Op.getResNo() == 0) {
4895 if (Tmp == VTBits)
4896 return 1; // early-out
4898 return VTBits - Tmp + 1;
4900 return VTBits - Tmp;
4901 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
4902 ISD::LoadExtType ETy = cast<AtomicSDNode>(Op)->getExtensionType();
4903 if (ETy == ISD::SEXTLOAD)
4904 return VTBits - Tmp + 1;
4905 if (ETy == ISD::ZEXTLOAD)
4906 return VTBits - Tmp;
4907 }
4908 }
4909 break;
4910 }
4911 }
4912
4913 // If we are looking at the loaded value of the SDNode.
4914 if (Op.getResNo() == 0) {
4915 // Handle LOADX separately here. EXTLOAD case will fallthrough.
4916 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
4917 unsigned ExtType = LD->getExtensionType();
4918 switch (ExtType) {
4919 default: break;
4920 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
4921 Tmp = LD->getMemoryVT().getScalarSizeInBits();
4922 return VTBits - Tmp + 1;
4923 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
4924 Tmp = LD->getMemoryVT().getScalarSizeInBits();
4925 return VTBits - Tmp;
4926 case ISD::NON_EXTLOAD:
4927 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
4928 // We only need to handle vectors - computeKnownBits should handle
4929 // scalar cases.
4930 Type *CstTy = Cst->getType();
4931 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
4932 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
4933 VTBits == CstTy->getScalarSizeInBits()) {
4934 Tmp = VTBits;
4935 for (unsigned i = 0; i != NumElts; ++i) {
4936 if (!DemandedElts[i])
4937 continue;
4938 if (Constant *Elt = Cst->getAggregateElement(i)) {
4939 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
4940 const APInt &Value = CInt->getValue();
4941 Tmp = std::min(Tmp, Value.getNumSignBits());
4942 continue;
4943 }
4944 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
4945 APInt Value = CFP->getValueAPF().bitcastToAPInt();
4946 Tmp = std::min(Tmp, Value.getNumSignBits());
4947 continue;
4948 }
4949 }
4950 // Unknown type. Conservatively assume no bits match sign bit.
4951 return 1;
4952 }
4953 return Tmp;
4954 }
4955 }
4956 break;
4957 }
4958 }
4959 }
4960
4961 // Allow the target to implement this method for its nodes.
4962 if (Opcode >= ISD::BUILTIN_OP_END ||
4963 Opcode == ISD::INTRINSIC_WO_CHAIN ||
4964 Opcode == ISD::INTRINSIC_W_CHAIN ||
4965 Opcode == ISD::INTRINSIC_VOID) {
4966 // TODO: This can probably be removed once target code is audited. This
4967 // is here purely to reduce patch size and review complexity.
4968 if (!VT.isScalableVector()) {
4969 unsigned NumBits =
4970 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
4971 if (NumBits > 1)
4972 FirstAnswer = std::max(FirstAnswer, NumBits);
4973 }
4974 }
4975
4976 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4977 // use this information.
4978 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
4979 return std::max(FirstAnswer, Known.countMinSignBits());
4980}
4981
4983 unsigned Depth) const {
4984 unsigned SignBits = ComputeNumSignBits(Op, Depth);
4985 return Op.getScalarValueSizeInBits() - SignBits + 1;
4986}
4987
4989 const APInt &DemandedElts,
4990 unsigned Depth) const {
4991 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
4992 return Op.getScalarValueSizeInBits() - SignBits + 1;
4993}
4994
4996 unsigned Depth) const {
4997 // Early out for FREEZE.
4998 if (Op.getOpcode() == ISD::FREEZE)
4999 return true;
5000
5001 // TODO: Assume we don't know anything for now.
5002 EVT VT = Op.getValueType();
5003 if (VT.isScalableVector())
5004 return false;
5005
5006 APInt DemandedElts = VT.isVector()
5008 : APInt(1, 1);
5009 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5010}
5011
5013 const APInt &DemandedElts,
5014 bool PoisonOnly,
5015 unsigned Depth) const {
5016 unsigned Opcode = Op.getOpcode();
5017
5018 // Early out for FREEZE.
5019 if (Opcode == ISD::FREEZE)
5020 return true;
5021
5022 if (Depth >= MaxRecursionDepth)
5023 return false; // Limit search depth.
5024
5025 if (isIntOrFPConstant(Op))
5026 return true;
5027
5028 switch (Opcode) {
5029 case ISD::CONDCODE:
5030 case ISD::VALUETYPE:
5031 case ISD::FrameIndex:
5033 return true;
5034
5035 case ISD::UNDEF:
5036 return PoisonOnly;
5037
5038 case ISD::BUILD_VECTOR:
5039 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5040 // this shouldn't affect the result.
5041 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5042 if (!DemandedElts[i])
5043 continue;
5045 Depth + 1))
5046 return false;
5047 }
5048 return true;
5049
5050 // TODO: Search for noundef attributes from library functions.
5051
5052 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5053
5054 default:
5055 // Allow the target to implement this method for its nodes.
5056 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5057 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5059 Op, DemandedElts, *this, PoisonOnly, Depth);
5060 break;
5061 }
5062
5063 // If Op can't create undef/poison and none of its operands are undef/poison
5064 // then Op is never undef/poison.
5065 // NOTE: TargetNodes can handle this in themselves in
5066 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5067 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5068 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5069 Depth) &&
5070 all_of(Op->ops(), [&](SDValue V) {
5071 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5072 });
5073}
5074
5076 bool ConsiderFlags,
5077 unsigned Depth) const {
5078 // TODO: Assume we don't know anything for now.
5079 EVT VT = Op.getValueType();
5080 if (VT.isScalableVector())
5081 return true;
5082
5083 APInt DemandedElts = VT.isVector()
5085 : APInt(1, 1);
5086 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5087 Depth);
5088}
5089
5091 bool PoisonOnly, bool ConsiderFlags,
5092 unsigned Depth) const {
5093 // TODO: Assume we don't know anything for now.
5094 EVT VT = Op.getValueType();
5095 if (VT.isScalableVector())
5096 return true;
5097
5098 unsigned Opcode = Op.getOpcode();
5099 switch (Opcode) {
5100 case ISD::FREEZE:
5103 case ISD::AND:
5104 case ISD::XOR:
5105 case ISD::ROTL:
5106 case ISD::ROTR:
5107 case ISD::FSHL:
5108 case ISD::FSHR:
5109 case ISD::BSWAP:
5110 case ISD::CTPOP:
5111 case ISD::BITREVERSE:
5112 case ISD::PARITY:
5113 case ISD::SIGN_EXTEND:
5114 case ISD::TRUNCATE:
5118 case ISD::BITCAST:
5119 case ISD::BUILD_VECTOR:
5120 case ISD::BUILD_PAIR:
5121 return false;
5122
5123 case ISD::SETCC: {
5124 // Integer setcc cannot create undef or poison.
5125 if (Op.getOperand(0).getValueType().isInteger())
5126 return false;
5127
5128 // FP compares are more complicated. They can create poison for nan/infinity
5129 // based on options and flags. The options and flags also cause special
5130 // nonan condition codes to be used. Those condition codes may be preserved
5131 // even if the nonan flag is dropped somewhere.
5132 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5133 if (((unsigned)CCCode & 0x10U))
5134 return true;
5135
5137 return Options.NoNaNsFPMath || Options.NoInfsFPMath ||
5138 (ConsiderFlags &&
5139 (Op->getFlags().hasNoNaNs() || Op->getFlags().hasNoInfs()));
5140 }
5141
5142 // Matches hasPoisonGeneratingFlags().
5143 case ISD::ZERO_EXTEND:
5144 return ConsiderFlags && Op->getFlags().hasNonNeg();
5145
5146 case ISD::ADD:
5147 case ISD::SUB:
5148 case ISD::MUL:
5149 // Matches hasPoisonGeneratingFlags().
5150 return ConsiderFlags && (Op->getFlags().hasNoSignedWrap() ||
5151 Op->getFlags().hasNoUnsignedWrap());
5152
5153 case ISD::SHL:
5154 // If the max shift amount isn't in range, then the shift can create poison.
5155 if (!getValidMaximumShiftAmountConstant(Op, DemandedElts))
5156 return true;
5157
5158 // Matches hasPoisonGeneratingFlags().
5159 return ConsiderFlags && (Op->getFlags().hasNoSignedWrap() ||
5160 Op->getFlags().hasNoUnsignedWrap());
5161
5162 // Matches hasPoisonGeneratingFlags().
5163 case ISD::OR:
5164 return ConsiderFlags && Op->getFlags().hasDisjoint();
5165
5167 // Check if we demand any upper (undef) elements.
5168 return !PoisonOnly && DemandedElts.ugt(1);
5169
5171 // Ensure that the element index is in bounds.
5172 EVT VecVT = Op.getOperand(0).getValueType();
5173 KnownBits KnownIdx = computeKnownBits(Op.getOperand(1), Depth + 1);
5174 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5175 }
5176
5178 // Ensure that the element index is in bounds.
5179 EVT VecVT = Op.getOperand(0).getValueType();
5180 KnownBits KnownIdx = computeKnownBits(Op.getOperand(2), Depth + 1);
5181 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5182 }
5183
5184 default:
5185 // Allow the target to implement this method for its nodes.
5186 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5187 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5189 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5190 break;
5191 }
5192
5193 // Be conservative and return true.
5194 return true;
5195}
5196
5198 unsigned Opcode = Op.getOpcode();
5199 if (Opcode == ISD::OR)
5200 return Op->getFlags().hasDisjoint() ||
5201 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5202 if (Opcode == ISD::XOR)
5203 return isMinSignedConstant(Op.getOperand(1));
5204 return false;
5205}
5206
5208 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5209 (Op.getOpcode() == ISD::ADD || isADDLike(Op));
5210}
5211
5212bool SelectionDAG::isKnownNeverNaN(SDValue Op, bool SNaN, unsigned Depth) const {
5213 // If we're told that NaNs won't happen, assume they won't.
5214 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5215 return true;
5216
5217 if (Depth >= MaxRecursionDepth)
5218 return false; // Limit search depth.
5219
5220 // If the value is a constant, we can obviously see if it is a NaN or not.
5221 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) {
5222 return !C->getValueAPF().isNaN() ||
5223 (SNaN && !C->getValueAPF().isSignaling());
5224 }
5225
5226 unsigned Opcode = Op.getOpcode();
5227 switch (Opcode) {
5228 case ISD::FADD:
5229 case ISD::FSUB:
5230 case ISD::FMUL:
5231 case ISD::FDIV:
5232 case ISD::FREM:
5233 case ISD::FSIN:
5234 case ISD::FCOS:
5235 case ISD::FMA:
5236 case ISD::FMAD: {
5237 if (SNaN)
5238 return true;
5239 // TODO: Need isKnownNeverInfinity
5240 return false;
5241 }
5242 case ISD::FCANONICALIZE:
5243 case ISD::FEXP:
5244 case ISD::FEXP2:
5245 case ISD::FEXP10:
5246 case ISD::FTRUNC:
5247 case ISD::FFLOOR:
5248 case ISD::FCEIL:
5249 case ISD::FROUND:
5250 case ISD::FROUNDEVEN:
5251 case ISD::FRINT:
5252 case ISD::LRINT:
5253 case ISD::LLRINT:
5254 case ISD::FNEARBYINT:
5255 case ISD::FLDEXP: {
5256 if (SNaN)
5257 return true;
5258 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5259 }
5260 case ISD::FABS:
5261 case ISD::FNEG:
5262 case ISD::FCOPYSIGN: {
5263 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5264 }
5265 case ISD::SELECT:
5266 return isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) &&
5267 isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1);
5268 case ISD::FP_EXTEND:
5269 case ISD::FP_ROUND: {
5270 if (SNaN)
5271 return true;
5272 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5273 }
5274 case ISD::SINT_TO_FP:
5275 case ISD::UINT_TO_FP:
5276 return true;
5277 case ISD::FSQRT: // Need is known positive
5278 case ISD::FLOG:
5279 case ISD::FLOG2:
5280 case ISD::FLOG10:
5281 case ISD::FPOWI:
5282 case ISD::FPOW: {
5283 if (SNaN)
5284 return true;
5285 // TODO: Refine on operand
5286 return false;
5287 }
5288 case ISD::FMINNUM:
5289 case ISD::FMAXNUM: {
5290 // Only one needs to be known not-nan, since it will be returned if the
5291 // other ends up being one.
5292 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) ||
5293 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5294 }
5295 case ISD::FMINNUM_IEEE:
5296 case ISD::FMAXNUM_IEEE: {
5297 if (SNaN)
5298 return true;
5299 // This can return a NaN if either operand is an sNaN, or if both operands
5300 // are NaN.
5301 return (isKnownNeverNaN(Op.getOperand(0), false, Depth + 1) &&
5302 isKnownNeverSNaN(Op.getOperand(1), Depth + 1)) ||
5303 (isKnownNeverNaN(Op.getOperand(1), false, Depth + 1) &&
5304 isKnownNeverSNaN(Op.getOperand(0), Depth + 1));
5305 }
5306 case ISD::FMINIMUM:
5307 case ISD::FMAXIMUM: {
5308 // TODO: Does this quiet or return the origina NaN as-is?
5309 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) &&
5310 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5311 }
5313 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5314 }
5315 case ISD::BUILD_VECTOR: {
5316 for (const SDValue &Opnd : Op->ops())
5317 if (!isKnownNeverNaN(Opnd, SNaN, Depth + 1))
5318 return false;
5319 return true;
5320 }
5321 default:
5322 if (Opcode >= ISD::BUILTIN_OP_END ||
5323 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5324 Opcode == ISD::INTRINSIC_W_CHAIN ||
5325 Opcode == ISD::INTRINSIC_VOID) {
5326 return TLI->isKnownNeverNaNForTargetNode(Op, *this, SNaN, Depth);
5327 }
5328
5329 return false;
5330 }
5331}
5332
5334 assert(Op.getValueType().isFloatingPoint() &&
5335 "Floating point type expected");
5336
5337 // If the value is a constant, we can obviously see if it is a zero or not.
5339 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
5340}
5341
5343 if (Depth >= MaxRecursionDepth)
5344 return false; // Limit search depth.
5345
5346 assert(!Op.getValueType().isFloatingPoint() &&
5347 "Floating point types unsupported - use isKnownNeverZeroFloat");
5348
5349 // If the value is a constant, we can obviously see if it is a zero or not.
5351 [](ConstantSDNode *C) { return !C->isZero(); }))
5352 return true;
5353
5354 // TODO: Recognize more cases here. Most of the cases are also incomplete to
5355 // some degree.
5356 switch (Op.getOpcode()) {
5357 default:
5358 break;
5359
5360 case ISD::OR:
5361 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5362 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5363
5364 case ISD::VSELECT:
5365 case ISD::SELECT:
5366 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5367 isKnownNeverZero(Op.getOperand(2), Depth + 1);
5368
5369 case ISD::SHL: {
5370 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5371 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5372 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5373 // 1 << X is never zero.
5374 if (ValKnown.One[0])
5375 return true;
5376 // If max shift cnt of known ones is non-zero, result is non-zero.
5377 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5378 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5379 !ValKnown.One.shl(MaxCnt).isZero())
5380 return true;
5381 break;
5382 }
5383 case ISD::UADDSAT:
5384 case ISD::UMAX:
5385 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5386 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5387
5388 // For smin/smax: If either operand is known negative/positive
5389 // respectively we don't need the other to be known at all.
5390 case ISD::SMAX: {
5391 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5392 if (Op1.isStrictlyPositive())
5393 return true;
5394
5395 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5396 if (Op0.isStrictlyPositive())
5397 return true;
5398
5399 if (Op1.isNonZero() && Op0.isNonZero())
5400 return true;
5401
5402 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5403 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5404 }
5405 case ISD::SMIN: {
5406 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5407 if (Op1.isNegative())
5408 return true;
5409
5410 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5411 if (Op0.isNegative())
5412 return true;
5413
5414 if (Op1.isNonZero() && Op0.isNonZero())
5415 return true;
5416
5417 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5418 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5419 }
5420 case ISD::UMIN:
5421 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5422 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5423
5424 case ISD::ROTL:
5425 case ISD::ROTR:
5426 case ISD::BITREVERSE:
5427 case ISD::BSWAP:
5428 case ISD::CTPOP:
5429 case ISD::ABS:
5430 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5431
5432 case ISD::SRA:
5433 case ISD::SRL: {
5434 if (Op->getFlags().hasExact())
5435 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5436 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5437 if (ValKnown.isNegative())
5438 return true;
5439 // If max shift cnt of known ones is non-zero, result is non-zero.
5440 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5441 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5442 !ValKnown.One.lshr(MaxCnt).isZero())
5443 return true;
5444 break;
5445 }
5446 case ISD::UDIV:
5447 case ISD::SDIV:
5448 // div exact can only produce a zero if the dividend is zero.
5449 // TODO: For udiv this is also true if Op1 u<= Op0
5450 if (Op->getFlags().hasExact())
5451 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5452 break;
5453
5454 case ISD::ADD:
5455 if (Op->getFlags().hasNoUnsignedWrap())
5456 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5457 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5458 return true;
5459 // TODO: There are a lot more cases we can prove for add.
5460 break;
5461
5462 case ISD::SUB: {
5463 if (isNullConstant(Op.getOperand(0)))
5464 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
5465
5466 std::optional<bool> ne =
5467 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
5468 computeKnownBits(Op.getOperand(1), Depth + 1));
5469 return ne && *ne;
5470 }
5471
5472 case ISD::MUL:
5473 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5474 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5475 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5476 return true;
5477 break;
5478
5479 case ISD::ZERO_EXTEND:
5480 case ISD::SIGN_EXTEND:
5481 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5482 }
5483
5485}
5486
5488 // Check the obvious case.
5489 if (A == B) return true;
5490
5491 // For negative and positive zero.
5492 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
5493 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
5494 if (CA->isZero() && CB->isZero()) return true;
5495
5496 // Otherwise they may not be equal.
5497 return false;
5498}
5499
5500// Only bits set in Mask must be negated, other bits may be arbitrary.
5502 if (isBitwiseNot(V, AllowUndefs))
5503 return V.getOperand(0);
5504
5505 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
5506 // bits in the non-extended part.
5507 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
5508 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
5509 return SDValue();
5510 SDValue ExtArg = V.getOperand(0);
5511 if (ExtArg.getScalarValueSizeInBits() >=
5512 MaskC->getAPIntValue().getActiveBits() &&
5513 isBitwiseNot(ExtArg, AllowUndefs) &&
5514 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
5515 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
5516 return ExtArg.getOperand(0).getOperand(0);
5517 return SDValue();
5518}
5519
5521 // Match masked merge pattern (X & ~M) op (Y & M)
5522 // Including degenerate case (X & ~M) op M
5523 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
5524 SDValue Other) {
5525 if (SDValue NotOperand =
5526 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
5527 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
5528 NotOperand->getOpcode() == ISD::TRUNCATE)
5529 NotOperand = NotOperand->getOperand(0);
5530
5531 if (Other == NotOperand)
5532 return true;
5533 if (Other->getOpcode() == ISD::AND)
5534 return NotOperand == Other->getOperand(0) ||
5535 NotOperand == Other->getOperand(1);
5536 }
5537 return false;
5538 };
5539
5540 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
5541 A = A->getOperand(0);
5542
5543 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
5544 B = B->getOperand(0);
5545
5546 if (A->getOpcode() == ISD::AND)
5547 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
5548 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
5549 return false;
5550}
5551
5552// FIXME: unify with llvm::haveNoCommonBitsSet.
5554 assert(A.getValueType() == B.getValueType() &&
5555 "Values must have the same type");
5558 return true;
5561}
5562
5563static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
5564 SelectionDAG &DAG) {
5565 if (cast<ConstantSDNode>(Step)->isZero())
5566 return DAG.getConstant(0, DL, VT);
5567
5568 return SDValue();
5569}
5570
5573 SelectionDAG &DAG) {
5574 int NumOps = Ops.size();
5575 assert(NumOps != 0 && "Can't build an empty vector!");
5576 assert(!VT.isScalableVector() &&
5577 "BUILD_VECTOR cannot be used with scalable types");
5578 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
5579 "Incorrect element count in BUILD_VECTOR!");
5580
5581 // BUILD_VECTOR of UNDEFs is UNDEF.
5582 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
5583 return DAG.getUNDEF(VT);
5584
5585 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
5586 SDValue IdentitySrc;
5587 bool IsIdentity = true;
5588 for (int i = 0; i != NumOps; ++i) {
5589 if (Ops[i].getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
5590 Ops[i].getOperand(0).getValueType() != VT ||
5591 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
5592 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
5593 Ops[i].getConstantOperandAPInt(1) != i) {
5594 IsIdentity = false;
5595 break;
5596 }
5597 IdentitySrc = Ops[i].getOperand(0);
5598 }
5599 if (IsIdentity)
5600 return IdentitySrc;
5601
5602 return SDValue();
5603}
5604
5605/// Try to simplify vector concatenation to an input value, undef, or build
5606/// vector.
5609 SelectionDAG &DAG) {
5610 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
5611 assert(llvm::all_of(Ops,
5612 [Ops](SDValue Op) {
5613 return Ops[0].getValueType() == Op.getValueType();
5614 }) &&
5615 "Concatenation of vectors with inconsistent value types!");
5616 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
5617 VT.getVectorElementCount() &&
5618 "Incorrect element count in vector concatenation!");
5619
5620 if (Ops.size() == 1)
5621 return Ops[0];
5622
5623 // Concat of UNDEFs is UNDEF.
5624 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
5625 return DAG.getUNDEF(VT);
5626
5627 // Scan the operands and look for extract operations from a single source
5628 // that correspond to insertion at the same location via this concatenation:
5629 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
5630 SDValue IdentitySrc;
5631 bool IsIdentity = true;
5632 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
5633 SDValue Op = Ops[i];
5634 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
5635 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
5636 Op.getOperand(0).getValueType() != VT ||
5637 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
5638 Op.getConstantOperandVal(1) != IdentityIndex) {
5639 IsIdentity = false;
5640 break;
5641 }
5642 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
5643 "Unexpected identity source vector for concat of extracts");
5644 IdentitySrc = Op.getOperand(0);
5645 }
5646 if (IsIdentity) {
5647 assert(IdentitySrc && "Failed to set source vector of extracts");
5648 return IdentitySrc;
5649 }
5650
5651 // The code below this point is only designed to work for fixed width
5652 // vectors, so we bail out for now.
5653 if (VT.isScalableVector())
5654 return SDValue();
5655
5656 // A CONCAT_VECTOR with all UNDEF/BUILD_VECTOR operands can be
5657 // simplified to one big BUILD_VECTOR.
5658 // FIXME: Add support for SCALAR_TO_VECTOR as well.
5659 EVT SVT = VT.getScalarType();
5661 for (SDValue Op : Ops) {
5662 EVT OpVT = Op.getValueType();
5663 if (Op.isUndef())
5664 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
5665 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
5666 Elts.append(Op->op_begin(), Op->op_end());
5667 else
5668 return SDValue();
5669 }
5670
5671 // BUILD_VECTOR requires all inputs to be of the same type, find the
5672 // maximum type and extend them all.
5673 for (SDValue Op : Elts)
5674 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
5675
5676 if (SVT.bitsGT(VT.getScalarType())) {
5677 for (SDValue &Op : Elts) {
5678 if (Op.isUndef())
5679 Op = DAG.getUNDEF(SVT);
5680 else
5681 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
5682 ? DAG.getZExtOrTrunc(Op, DL, SVT)
5683 : DAG.getSExtOrTrunc(Op, DL, SVT);
5684 }
5685 }
5686
5687 SDValue V = DAG.getBuildVector(VT, DL, Elts);
5688 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
5689 return V;
5690}
5691
5692/// Gets or creates the specified node.
5693SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
5695 AddNodeIDNode(ID, Opcode, getVTList(VT), std::nullopt);
5696 void *IP = nullptr;
5697 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
5698 return SDValue(E, 0);
5699
5700 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(),
5701 getVTList(VT));
5702 CSEMap.InsertNode(N, IP);
5703
5704 InsertNode(N);
5705 SDValue V = SDValue(N, 0);
5706 NewSDValueDbgMsg(V, "Creating new node: ", this);
5707 return V;
5708}
5709
5710SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5711 SDValue N1) {
5712 SDNodeFlags Flags;
5713 if (Inserter)
5714 Flags = Inserter->getFlags();
5715 return getNode(Opcode, DL, VT, N1, Flags);
5716}
5717
5718SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5719 SDValue N1, const SDNodeFlags Flags) {
5720 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
5721
5722 // Constant fold unary operations with a vector integer or float operand.
5723 switch (Opcode) {
5724 default:
5725 // FIXME: Entirely reasonable to perform folding of other unary
5726 // operations here as the need arises.
5727 break;
5728 case ISD::FNEG:
5729 case ISD::FABS:
5730 case ISD::FCEIL:
5731 case ISD::FTRUNC:
5732 case ISD::FFLOOR:
5733 case ISD::FP_EXTEND:
5734 case ISD::FP_TO_SINT:
5735 case ISD::FP_TO_UINT:
5736 case ISD::FP_TO_FP16:
5737 case ISD::FP_TO_BF16:
5738 case ISD::TRUNCATE:
5739 case ISD::ANY_EXTEND:
5740 case ISD::ZERO_EXTEND:
5741 case ISD::SIGN_EXTEND:
5742 case ISD::UINT_TO_FP:
5743 case ISD::SINT_TO_FP:
5744 case ISD::FP16_TO_FP:
5745 case ISD::BF16_TO_FP:
5746 case ISD::BITCAST:
5747 case ISD::ABS:
5748 case ISD::BITREVERSE:
5749 case ISD::BSWAP:
5750 case ISD::CTLZ:
5752 case ISD::CTTZ:
5754 case ISD::CTPOP:
5755 case ISD::STEP_VECTOR: {
5756 SDValue Ops = {N1};
5757 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
5758 return Fold;
5759 }
5760 }
5761
5762 unsigned OpOpcode = N1.getNode()->getOpcode();
5763 switch (Opcode) {
5764 case ISD::STEP_VECTOR:
5765 assert(VT.isScalableVector() &&
5766 "STEP_VECTOR can only be used with scalable types");
5767 assert(OpOpcode == ISD::TargetConstant &&
5768 VT.getVectorElementType() == N1.getValueType() &&
5769 "Unexpected step operand");
5770 break;
5771 case ISD::FREEZE:
5772 assert(VT == N1.getValueType() && "Unexpected VT!");
5773 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly*/ false,
5774 /*Depth*/ 1))
5775 return N1;
5776 break;
5777 case ISD::TokenFactor:
5778 case ISD::MERGE_VALUES:
5780 return N1; // Factor, merge or concat of one node? No need.
5781 case ISD::BUILD_VECTOR: {
5782 // Attempt to simplify BUILD_VECTOR.
5783 SDValue Ops[] = {N1};
5784 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
5785 return V;
5786 break;
5787 }
5788 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
5789 case ISD::FP_EXTEND:
5791 "Invalid FP cast!");
5792 if (N1.getValueType() == VT) return N1; // noop conversion.
5793 assert((!VT.isVector() || VT.getVectorElementCount() ==
5795 "Vector element count mismatch!");
5796 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
5797 if (N1.isUndef())
5798 return getUNDEF(VT);
5799 break;
5800 case ISD::FP_TO_SINT:
5801 case ISD::FP_TO_UINT:
5802 if (N1.isUndef())
5803 return getUNDEF(VT);
5804 break;
5805 case ISD::SINT_TO_FP:
5806 case ISD::UINT_TO_FP:
5807 // [us]itofp(undef) = 0, because the result value is bounded.
5808 if (N1.isUndef())
5809 return getConstantFP(0.0, DL, VT);
5810 break;
5811 case ISD::SIGN_EXTEND:
5812 assert(VT.isInteger() && N1.getValueType().isInteger() &&
5813 "Invalid SIGN_EXTEND!");
5814 assert(VT.isVector() == N1.getValueType().isVector() &&
5815 "SIGN_EXTEND result type type should be vector iff the operand "
5816 "type is vector!");
5817 if (N1.getValueType() == VT) return N1; // noop extension
5818 assert((!VT.isVector() || VT.getVectorElementCount() ==
5820 "Vector element count mismatch!");
5821 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
5822 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
5823 SDNodeFlags Flags;
5824 if (OpOpcode == ISD::ZERO_EXTEND)
5825 Flags.setNonNeg(N1->getFlags().hasNonNeg());
5826 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
5827 }
5828 if (OpOpcode == ISD::UNDEF)
5829 // sext(undef) = 0, because the top bits will all be the same.
5830 return getConstant(0, DL, VT);
5831 break;
5832 case ISD::ZERO_EXTEND:
5833 assert(VT.isInteger() && N1.getValueType().isInteger() &&
5834 "Invalid ZERO_EXTEND!");
5835 assert(VT.isVector() == N1.getValueType().isVector() &&
5836 "ZERO_EXTEND result type type should be vector iff the operand "
5837 "type is vector!");
5838 if (N1.getValueType() == VT) return N1; // noop extension
5839 assert((!VT.isVector() || VT.getVectorElementCount() ==
5841 "Vector element count mismatch!");
5842 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
5843 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
5844 SDNodeFlags Flags;
5845 Flags.setNonNeg(N1->getFlags().hasNonNeg());
5846 return getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
5847 }
5848 if (OpOpcode == ISD::UNDEF)
5849 // zext(undef) = 0, because the top bits will be zero.
5850 return getConstant(0, DL, VT);
5851
5852 // Skip unnecessary zext_inreg pattern:
5853 // (zext (trunc x)) -> x iff the upper bits are known zero.
5854 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
5855 // use to recognise zext_inreg patterns.
5856 if (OpOpcode == ISD::TRUNCATE) {
5857 SDValue OpOp = N1.getOperand(0);
5858 if (OpOp.getValueType() == VT) {
5859 if (OpOp.getOpcode() != ISD::AND) {
5862 if (MaskedValueIsZero(OpOp, HiBits)) {
5863 transferDbgValues(N1, OpOp);
5864 return OpOp;
5865 }
5866 }
5867 }
5868 }
5869 break;
5870 case ISD::ANY_EXTEND:
5871 assert(VT.isInteger() && N1.getValueType().isInteger() &&
5872 "Invalid ANY_EXTEND!");
5873 assert(VT.isVector() == N1.getValueType().isVector() &&
5874 "ANY_EXTEND result type type should be vector iff the operand "
5875 "type is vector!");
5876 if (N1.getValueType() == VT) return N1; // noop extension
5877 assert((!VT.isVector() || VT.getVectorElementCount() ==
5879 "Vector element count mismatch!");
5880 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
5881
5882 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
5883 OpOpcode == ISD::ANY_EXTEND) {
5884 SDNodeFlags Flags;
5885 if (OpOpcode == ISD::ZERO_EXTEND)
5886 Flags.setNonNeg(N1->getFlags().hasNonNeg());
5887 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
5888 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
5889 }
5890 if (OpOpcode == ISD::UNDEF)
5891 return getUNDEF(VT);
5892
5893 // (ext (trunc x)) -> x
5894 if (OpOpcode == ISD::TRUNCATE) {
5895 SDValue OpOp = N1.getOperand(0);
5896 if (OpOp.getValueType() == VT) {
5897 transferDbgValues(N1, OpOp);
5898 return OpOp;
5899 }
5900 }
5901 break;
5902 case ISD::TRUNCATE:
5903 assert(VT.isInteger() && N1.getValueType().isInteger() &&
5904 "Invalid TRUNCATE!");
5905 assert(VT.isVector() == N1.getValueType().isVector() &&
5906 "TRUNCATE result type type should be vector iff the operand "
5907 "type is vector!");
5908 if (N1.getValueType() == VT) return N1; // noop truncate
5909 assert((!VT.isVector() || VT.getVectorElementCount() ==
5911 "Vector element count mismatch!");
5912 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
5913 if (OpOpcode == ISD::TRUNCATE)
5914 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
5915 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
5916 OpOpcode == ISD::ANY_EXTEND) {
5917 // If the source is smaller than the dest, we still need an extend.
5919 VT.getScalarType()))
5920 return getNode(OpOpcode, DL, VT, N1.getOperand(0));
5921 if (N1.getOperand(0).getValueType().bitsGT(VT))
5922 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
5923 return N1.getOperand(0);
5924 }
5925 if (OpOpcode == ISD::UNDEF)
5926 return getUNDEF(VT);
5927 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
5928 return getVScale(DL, VT,
5930 break;
5934 assert(VT.isVector() && "This DAG node is restricted to vector types.");
5935 assert(N1.getValueType().bitsLE(VT) &&
5936 "The input must be the same size or smaller than the result.");
5939 "The destination vector type must have fewer lanes than the input.");
5940 break;
5941 case ISD::ABS:
5942 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
5943 if (OpOpcode == ISD::UNDEF)
5944 return getConstant(0, DL, VT);
5945 break;
5946 case ISD::BSWAP:
5947 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
5948 assert((VT.getScalarSizeInBits() % 16 == 0) &&
5949 "BSWAP types must be a multiple of 16 bits!");
5950 if (OpOpcode == ISD::UNDEF)
5951 return getUNDEF(VT);
5952 // bswap(bswap(X)) -> X.
5953 if (OpOpcode == ISD::BSWAP)
5954 return N1.getOperand(0);
5955 break;
5956 case ISD::BITREVERSE:
5957 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
5958 if (OpOpcode == ISD::UNDEF)
5959 return getUNDEF(VT);
5960 break;
5961 case ISD::BITCAST:
5963 "Cannot BITCAST between types of different sizes!");
5964 if (VT == N1.getValueType()) return N1; // noop conversion.
5965 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
5966 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
5967 if (OpOpcode == ISD::UNDEF)
5968 return getUNDEF(VT);
5969 break;
5971 assert(VT.isVector() && !N1.getValueType().isVector() &&
5972 (VT.getVectorElementType() == N1.getValueType() ||
5974 N1.getValueType().isInteger() &&
5976 "Illegal SCALAR_TO_VECTOR node!");
5977 if (OpOpcode == ISD::UNDEF)
5978 return getUNDEF(VT);
5979 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
5980 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
5981 isa<ConstantSDNode>(N1.getOperand(1)) &&
5982 N1.getConstantOperandVal(1) == 0 &&
5983 N1.getOperand(0).getValueType() == VT)
5984 return N1.getOperand(0);
5985 break;
5986 case ISD::FNEG:
5987 // Negation of an unknown bag of bits is still completely undefined.
5988 if (OpOpcode == ISD::UNDEF)
5989 return getUNDEF(VT);
5990
5991 if (OpOpcode == ISD::FNEG) // --X -> X
5992 return N1.getOperand(0);
5993 break;
5994 case ISD::FABS:
5995 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
5996 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
5997 break;
5998 case ISD::VSCALE:
5999 assert(VT == N1.getValueType() && "Unexpected VT!");
6000 break;
6001 case ISD::CTPOP:
6002 if (N1.getValueType().getScalarType() == MVT::i1)
6003 return N1;
6004 break;
6005 case ISD::CTLZ:
6006 case ISD::CTTZ:
6007 if (N1.getValueType().getScalarType() == MVT::i1)
6008 return getNOT(DL, N1, N1.getValueType());
6009 break;
6010 case ISD::VECREDUCE_ADD:
6011 if (N1.getValueType().getScalarType() == MVT::i1)
6012 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6013 break;
6016 if (N1.getValueType().getScalarType() == MVT::i1)
6017 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6018 break;
6021 if (N1.getValueType().getScalarType() == MVT::i1)
6022 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6023 break;
6024 case ISD::SPLAT_VECTOR:
6025 assert(VT.isVector() && "Wrong return type!");
6026 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6027 // that for now.
6029 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6031 N1.getValueType().isInteger() &&
6033 "Wrong operand type!");
6034 break;
6035 }
6036
6037 SDNode *N;
6038 SDVTList VTs = getVTList(VT);
6039 SDValue Ops[] = {N1};
6040 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6042 AddNodeIDNode(ID, Opcode, VTs, Ops);
6043 void *IP = nullptr;
6044 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6045 E->intersectFlagsWith(Flags);
6046 return SDValue(E, 0);
6047 }
6048
6049 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6050 N->setFlags(Flags);
6051 createOperands(N, Ops);
6052 CSEMap.InsertNode(N, IP);
6053 } else {
6054 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6055 createOperands(N, Ops);
6056 }
6057
6058 InsertNode(N);
6059 SDValue V = SDValue(N, 0);
6060 NewSDValueDbgMsg(V, "Creating new node: ", this);
6061 return V;
6062}
6063
6064static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6065 const APInt &C2) {
6066 switch (Opcode) {
6067 case ISD::ADD: return C1 + C2;
6068 case ISD::SUB: return C1 - C2;
6069 case ISD::MUL: return C1 * C2;
6070 case ISD::AND: return C1 & C2;
6071 case ISD::OR: return C1 | C2;
6072 case ISD::XOR: return C1 ^ C2;
6073 case ISD::SHL: return C1 << C2;
6074 case ISD::SRL: return C1.lshr(C2);
6075 case ISD::SRA: return C1.ashr(C2);
6076 case ISD::ROTL: return C1.rotl(C2);
6077 case ISD::ROTR: return C1.rotr(C2);
6078 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6079 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6080 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6081 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6082 case ISD::SADDSAT: return C1.sadd_sat(C2);
6083 case ISD::UADDSAT: return C1.uadd_sat(C2);
6084 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6085 case ISD::USUBSAT: return C1.usub_sat(C2);
6086 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6087 case ISD::USHLSAT: return C1.ushl_sat(C2);
6088 case ISD::UDIV:
6089 if (!C2.getBoolValue())
6090 break;
6091 return C1.udiv(C2);
6092 case ISD::UREM:
6093 if (!C2.getBoolValue())
6094 break;
6095 return C1.urem(C2);
6096 case ISD::SDIV:
6097 if (!C2.getBoolValue())
6098 break;
6099 return C1.sdiv(C2);
6100 case ISD::SREM:
6101 if (!C2.getBoolValue())
6102 break;
6103 return C1.srem(C2);
6104 case ISD::AVGFLOORS:
6105 return APIntOps::avgFloorS(C1, C2);
6106 case ISD::AVGFLOORU:
6107 return APIntOps::avgFloorU(C1, C2);
6108 case ISD::AVGCEILS:
6109 return APIntOps::avgCeilS(C1, C2);
6110 case ISD::AVGCEILU:
6111 return APIntOps::avgCeilU(C1, C2);
6112 case ISD::ABDS:
6113 return APIntOps::abds(C1, C2);
6114 case ISD::ABDU:
6115 return APIntOps::abdu(C1, C2);
6116 case ISD::MULHS:
6117 return APIntOps::mulhs(C1, C2);
6118 case ISD::MULHU:
6119 return APIntOps::mulhu(C1, C2);
6120 }
6121 return std::nullopt;
6122}
6123// Handle constant folding with UNDEF.
6124// TODO: Handle more cases.
6125static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6126 bool IsUndef1, const APInt &C2,
6127 bool IsUndef2) {
6128 if (!(IsUndef1 || IsUndef2))
6129 return FoldValue(Opcode, C1, C2);
6130
6131 // Fold and(x, undef) -> 0
6132 // Fold mul(x, undef) -> 0
6133 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6134 return APInt::getZero(C1.getBitWidth());
6135
6136 return std::nullopt;
6137}
6138
6140 const GlobalAddressSDNode *GA,
6141 const SDNode *N2) {
6142 if (GA->getOpcode() != ISD::GlobalAddress)
6143 return SDValue();
6144 if (!TLI->isOffsetFoldingLegal(GA))
6145 return SDValue();
6146 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6147 if (!C2)
6148 return SDValue();
6149 int64_t Offset = C2->getSExtValue();
6150 switch (Opcode) {
6151 case ISD::ADD: break;
6152 case ISD::SUB: Offset = -uint64_t(Offset); break;
6153 default: return SDValue();
6154 }
6155 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6156 GA->getOffset() + uint64_t(Offset));
6157}
6158
6159bool SelectionDAG::isUndef(unsigned Opcode, ArrayRef<SDValue> Ops) {
6160 switch (Opcode) {
6161 case ISD::SDIV:
6162 case ISD::UDIV:
6163 case ISD::SREM:
6164 case ISD::UREM: {
6165 // If a divisor is zero/undef or any element of a divisor vector is
6166 // zero/undef, the whole op is undef.
6167 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6168 SDValue Divisor = Ops[1];
6169 if (Divisor.isUndef() || isNullConstant(Divisor))
6170 return true;
6171
6172 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6173 llvm::any_of(Divisor->op_values(),
6174 [](SDValue V) { return V.isUndef() ||
6175 isNullConstant(V); });
6176 // TODO: Handle signed overflow.
6177 }
6178 // TODO: Handle oversized shifts.
6179 default:
6180 return false;
6181 }
6182}
6183
6185 EVT VT, ArrayRef<SDValue> Ops) {
6186 // If the opcode is a target-specific ISD node, there's nothing we can
6187 // do here and the operand rules may not line up with the below, so
6188 // bail early.
6189 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6190 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6191 // foldCONCAT_VECTORS in getNode before this is called.
6192 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6193 return SDValue();
6194
6195 unsigned NumOps = Ops.size();
6196 if (NumOps == 0)
6197 return SDValue();
6198
6199 if (isUndef(Opcode, Ops))
6200 return getUNDEF(VT);
6201
6202 // Handle unary special cases.
6203 if (NumOps == 1) {
6204 SDValue N1 = Ops[0];
6205
6206 // Constant fold unary operations with an integer constant operand. Even
6207 // opaque constant will be folded, because the folding of unary operations
6208 // doesn't create new constants with different values. Nevertheless, the
6209 // opaque flag is preserved during folding to prevent future folding with
6210 // other constants.
6211 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6212 const APInt &Val = C->getAPIntValue();
6213 switch (Opcode) {
6214 case ISD::SIGN_EXTEND:
6215 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6216 C->isTargetOpcode(), C->isOpaque());
6217 case ISD::TRUNCATE:
6218 if (C->isOpaque())
6219 break;
6220 [[fallthrough]];
6221 case ISD::ZERO_EXTEND:
6222 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6223 C->isTargetOpcode(), C->isOpaque());
6224 case ISD::ANY_EXTEND:
6225 // Some targets like RISCV prefer to sign extend some types.
6226 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
6227 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6228 C->isTargetOpcode(), C->isOpaque());
6229 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6230 C->isTargetOpcode(), C->isOpaque());
6231 case ISD::ABS:
6232 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
6233 C->isOpaque());
6234 case ISD::BITREVERSE:
6235 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
6236 C->isOpaque());
6237 case ISD::BSWAP:
6238 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
6239 C->isOpaque());
6240 case ISD::CTPOP:
6241 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
6242 C->isOpaque());
6243 case ISD::CTLZ:
6245 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
6246 C->isOpaque());
6247 case ISD::CTTZ:
6249 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
6250 C->isOpaque());
6251 case ISD::UINT_TO_FP:
6252 case ISD::SINT_TO_FP: {
6255 (void)apf.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
6257 return getConstantFP(apf, DL, VT);
6258 }
6259 case ISD::FP16_TO_FP:
6260 case ISD::BF16_TO_FP: {
6261 bool Ignored;
6262 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
6263 : APFloat::BFloat(),
6264 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
6265
6266 // This can return overflow, underflow, or inexact; we don't care.
6267 // FIXME need to be more flexible about rounding mode.
6268 (void)FPV.convert(EVTToAPFloatSemantics(VT),
6270 return getConstantFP(FPV, DL, VT);
6271 }
6272 case ISD::STEP_VECTOR:
6273 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
6274 return V;
6275 break;
6276 case ISD::BITCAST:
6277 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
6278 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
6279 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
6280 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
6281 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
6282 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
6283 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
6284 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
6285 break;
6286 }
6287 }
6288
6289 // Constant fold unary operations with a floating point constant operand.
6290 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
6291 APFloat V = C->getValueAPF(); // make copy
6292 switch (Opcode) {
6293 case ISD::FNEG:
6294 V.changeSign();
6295 return getConstantFP(V, DL, VT);
6296 case ISD::FABS:
6297 V.clearSign();
6298 return getConstantFP(V, DL, VT);
6299 case ISD::FCEIL: {
6300 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
6301 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6302 return getConstantFP(V, DL, VT);
6303 return SDValue();
6304 }
6305 case ISD::FTRUNC: {
6306 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
6307 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6308 return getConstantFP(V, DL, VT);
6309 return SDValue();
6310 }
6311 case ISD::FFLOOR: {
6312 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
6313 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6314 return getConstantFP(V, DL, VT);
6315 return SDValue();
6316 }
6317 case ISD::FP_EXTEND: {
6318 bool ignored;
6319 // This can return overflow, underflow, or inexact; we don't care.
6320 // FIXME need to be more flexible about rounding mode.
6322 &ignored);
6323 return getConstantFP(V, DL, VT);
6324 }
6325 case ISD::FP_TO_SINT:
6326 case ISD::FP_TO_UINT: {
6327 bool ignored;
6328 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
6329 // FIXME need to be more flexible about rounding mode.
6331 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
6332 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
6333 break;
6334 return getConstant(IntVal, DL, VT);
6335 }
6336 case ISD::FP_TO_FP16:
6337 case ISD::FP_TO_BF16: {
6338 bool Ignored;
6339 // This can return overflow, underflow, or inexact; we don't care.
6340 // FIXME need to be more flexible about rounding mode.
6341 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
6342 : APFloat::BFloat(),
6344 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6345 }
6346 case ISD::BITCAST:
6347 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
6348 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6349 VT);
6350 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
6351 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6352 VT);
6353 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
6354 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
6355 VT);
6356 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
6357 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6358 break;
6359 }
6360 }
6361
6362 // Early-out if we failed to constant fold a bitcast.
6363 if (Opcode == ISD::BITCAST)
6364 return SDValue();
6365 }
6366
6367 // Handle binops special cases.
6368 if (NumOps == 2) {
6369 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
6370 return CFP;
6371
6372 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
6373 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
6374 if (C1->isOpaque() || C2->isOpaque())
6375 return SDValue();
6376
6377 std::optional<APInt> FoldAttempt =
6378 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
6379 if (!FoldAttempt)
6380 return SDValue();
6381
6382 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
6383 assert((!Folded || !VT.isVector()) &&
6384 "Can't fold vectors ops with scalar operands");
6385 return Folded;
6386 }
6387 }
6388
6389 // fold (add Sym, c) -> Sym+c
6390 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[0]))
6391 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
6392 if (TLI->isCommutativeBinOp(Opcode))
6393 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[1]))
6394 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
6395 }
6396
6397 // This is for vector folding only from here on.
6398 if (!VT.isVector())
6399 return SDValue();
6400
6401 ElementCount NumElts = VT.getVectorElementCount();
6402
6403 // See if we can fold through bitcasted integer ops.
6404 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
6405 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
6406 Ops[0].getOpcode() == ISD::BITCAST &&
6407 Ops[1].getOpcode() == ISD::BITCAST) {
6408 SDValue N1 = peekThroughBitcasts(Ops[0]);
6409 SDValue N2 = peekThroughBitcasts(Ops[1]);
6410 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
6411 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
6412 EVT BVVT = N1.getValueType();
6413 if (BV1 && BV2 && BVVT.isInteger() && BVVT == N2.getValueType()) {
6414 bool IsLE = getDataLayout().isLittleEndian();
6415 unsigned EltBits = VT.getScalarSizeInBits();
6416 SmallVector<APInt> RawBits1, RawBits2;
6417 BitVector UndefElts1, UndefElts2;
6418 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
6419 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
6420 SmallVector<APInt> RawBits;
6421 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
6422 std::optional<APInt> Fold = FoldValueWithUndef(
6423 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
6424 if (!Fold)
6425 break;
6426 RawBits.push_back(*Fold);
6427 }
6428 if (RawBits.size() == NumElts.getFixedValue()) {
6429 // We have constant folded, but we need to cast this again back to
6430 // the original (possibly legalized) type.
6431 SmallVector<APInt> DstBits;
6432 BitVector DstUndefs;
6434 DstBits, RawBits, DstUndefs,
6435 BitVector(RawBits.size(), false));
6436 EVT BVEltVT = BV1->getOperand(0).getValueType();
6437 unsigned BVEltBits = BVEltVT.getSizeInBits();
6438 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
6439 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
6440 if (DstUndefs[I])
6441 continue;
6442 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
6443 }
6444 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
6445 }
6446 }
6447 }
6448 }
6449
6450 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
6451 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
6452 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
6453 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
6454 APInt RHSVal;
6455 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
6456 APInt NewStep = Opcode == ISD::MUL
6457 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
6458 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
6459 return getStepVector(DL, VT, NewStep);
6460 }
6461 }
6462
6463 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
6464 return !Op.getValueType().isVector() ||
6465 Op.getValueType().getVectorElementCount() == NumElts;
6466 };
6467
6468 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
6469 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
6470 Op.getOpcode() == ISD::BUILD_VECTOR ||
6471 Op.getOpcode() == ISD::SPLAT_VECTOR;
6472 };
6473
6474 // All operands must be vector types with the same number of elements as
6475 // the result type and must be either UNDEF or a build/splat vector
6476 // or UNDEF scalars.
6477 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
6478 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
6479 return SDValue();
6480
6481 // If we are comparing vectors, then the result needs to be a i1 boolean that
6482 // is then extended back to the legal result type depending on how booleans
6483 // are represented.
6484 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
6485 ISD::NodeType ExtendCode =
6486 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
6489
6490 // Find legal integer scalar type for constant promotion and
6491 // ensure that its scalar size is at least as large as source.
6492 EVT LegalSVT = VT.getScalarType();
6493 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
6494 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
6495 if (LegalSVT.bitsLT(VT.getScalarType()))
6496 return SDValue();
6497 }
6498
6499 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
6500 // only have one operand to check. For fixed-length vector types we may have
6501 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
6502 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
6503
6504 // Constant fold each scalar lane separately.
6505 SmallVector<SDValue, 4> ScalarResults;
6506 for (unsigned I = 0; I != NumVectorElts; I++) {
6507 SmallVector<SDValue, 4> ScalarOps;
6508 for (SDValue Op : Ops) {
6509 EVT InSVT = Op.getValueType().getScalarType();
6510 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
6511 Op.getOpcode() != ISD::SPLAT_VECTOR) {
6512 if (Op.isUndef())
6513 ScalarOps.push_back(getUNDEF(InSVT));
6514 else
6515 ScalarOps.push_back(Op);
6516 continue;
6517 }
6518
6519 SDValue ScalarOp =
6520 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
6521 EVT ScalarVT = ScalarOp.getValueType();
6522
6523 // Build vector (integer) scalar operands may need implicit
6524 // truncation - do this before constant folding.
6525 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
6526 // Don't create illegally-typed nodes unless they're constants or undef
6527 // - if we fail to constant fold we can't guarantee the (dead) nodes
6528 // we're creating will be cleaned up before being visited for
6529 // legalization.
6530 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
6531 !isa<ConstantSDNode>(ScalarOp) &&
6532 TLI->getTypeAction(*getContext(), InSVT) !=
6534 return SDValue();
6535 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
6536 }
6537
6538 ScalarOps.push_back(ScalarOp);
6539 }
6540
6541 // Constant fold the scalar operands.
6542 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps);
6543
6544 // Legalize the (integer) scalar constant if necessary.
6545 if (LegalSVT != SVT)
6546 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
6547
6548 // Scalar folding only succeeded if the result is a constant or UNDEF.
6549 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
6550 ScalarResult.getOpcode() != ISD::ConstantFP)
6551 return SDValue();
6552 ScalarResults.push_back(ScalarResult);
6553 }
6554
6555 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
6556 : getBuildVector(VT, DL, ScalarResults);
6557 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
6558 return V;
6559}
6560
6562 EVT VT, ArrayRef<SDValue> Ops) {
6563 // TODO: Add support for unary/ternary fp opcodes.
6564 if (Ops.size() != 2)
6565 return SDValue();
6566
6567 // TODO: We don't do any constant folding for strict FP opcodes here, but we
6568 // should. That will require dealing with a potentially non-default
6569 // rounding mode, checking the "opStatus" return value from the APFloat
6570 // math calculations, and possibly other variations.
6571 SDValue N1 = Ops[0];
6572 SDValue N2 = Ops[1];
6573 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
6574 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
6575 if (N1CFP && N2CFP) {
6576 APFloat C1 = N1CFP->getValueAPF(); // make copy
6577 const APFloat &C2 = N2CFP->getValueAPF();
6578 switch (Opcode) {
6579 case ISD::FADD:
6581 return getConstantFP(C1, DL, VT);
6582 case ISD::FSUB:
6584 return getConstantFP(C1, DL, VT);
6585 case ISD::FMUL:
6587 return getConstantFP(C1, DL, VT);
6588 case ISD::FDIV:
6590 return getConstantFP(C1, DL, VT);
6591 case ISD::FREM:
6592 C1.mod(C2);
6593 return getConstantFP(C1, DL, VT);
6594 case ISD::FCOPYSIGN:
6595 C1.copySign(C2);
6596 return getConstantFP(C1, DL, VT);
6597 case ISD::FMINNUM:
6598 return getConstantFP(minnum(C1, C2), DL, VT);
6599 case ISD::FMAXNUM:
6600 return getConstantFP(maxnum(C1, C2), DL, VT);
6601 case ISD::FMINIMUM:
6602 return getConstantFP(minimum(C1, C2), DL, VT);
6603 case ISD::FMAXIMUM:
6604 return getConstantFP(maximum(C1, C2), DL, VT);
6605 default: break;
6606 }
6607 }
6608 if (N1CFP && Opcode == ISD::FP_ROUND) {
6609 APFloat C1 = N1CFP->getValueAPF(); // make copy
6610 bool Unused;
6611 // This can return overflow, underflow, or inexact; we don't care.
6612 // FIXME need to be more flexible about rounding mode.
6614 &Unused);
6615 return getConstantFP(C1, DL, VT);
6616 }
6617
6618 switch (Opcode) {
6619 case ISD::FSUB:
6620 // -0.0 - undef --> undef (consistent with "fneg undef")
6621 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
6622 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
6623 return getUNDEF(VT);
6624 [[fallthrough]];
6625
6626 case ISD::FADD:
6627 case ISD::FMUL:
6628 case ISD::FDIV:
6629 case ISD::FREM:
6630 // If both operands are undef, the result is undef. If 1 operand is undef,
6631 // the result is NaN. This should match the behavior of the IR optimizer.
6632 if (N1.isUndef() && N2.isUndef())
6633 return getUNDEF(VT);
6634 if (N1.isUndef() || N2.isUndef())
6636 }
6637 return SDValue();
6638}
6639
6641 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
6642
6643 // There's no need to assert on a byte-aligned pointer. All pointers are at
6644 // least byte aligned.
6645 if (A == Align(1))
6646 return Val;
6647
6650 ID.AddInteger(A.value());
6651
6652 void *IP = nullptr;
6653 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6654 return SDValue(E, 0);
6655
6656 auto *N = newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(),
6657 Val.getValueType(), A);
6658 createOperands(N, {Val});
6659
6660 CSEMap.InsertNode(N, IP);
6661 InsertNode(N);
6662
6663 SDValue V(N, 0);
6664 NewSDValueDbgMsg(V, "Creating new node: ", this);
6665 return V;
6666}
6667
6668SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6669 SDValue N1, SDValue N2) {
6670 SDNodeFlags Flags;
6671 if (Inserter)
6672 Flags = Inserter->getFlags();
6673 return getNode(Opcode, DL, VT, N1, N2, Flags);
6674}
6675
6677 SDValue &N2) const {
6678 if (!TLI->isCommutativeBinOp(Opcode))
6679 return;
6680
6681 // Canonicalize:
6682 // binop(const, nonconst) -> binop(nonconst, const)
6687 if ((N1C && !N2C) || (N1CFP && !N2CFP))
6688 std::swap(N1, N2);
6689
6690 // Canonicalize:
6691 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
6692 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
6694 std::swap(N1, N2);
6695}
6696
6697SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6698 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
6700 N2.getOpcode() != ISD::DELETED_NODE &&
6701 "Operand is DELETED_NODE!");
6702
6703 canonicalizeCommutativeBinop(Opcode, N1, N2);
6704
6705 auto *N1C = dyn_cast<ConstantSDNode>(N1);
6706 auto *N2C = dyn_cast<ConstantSDNode>(N2);
6707
6708 // Don't allow undefs in vector splats - we might be returning N2 when folding
6709 // to zero etc.
6710 ConstantSDNode *N2CV =
6711 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
6712
6713 switch (Opcode) {
6714 default: break;
6715 case ISD::TokenFactor:
6716 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
6717 N2.getValueType() == MVT::Other && "Invalid token factor!");
6718 // Fold trivial token factors.
6719 if (N1.getOpcode() == ISD::EntryToken) return N2;
6720 if (N2.getOpcode() == ISD::EntryToken) return N1;
6721 if (N1 == N2) return N1;
6722 break;
6723 case ISD::BUILD_VECTOR: {
6724 // Attempt to simplify BUILD_VECTOR.
6725 SDValue Ops[] = {N1, N2};
6726 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6727 return V;
6728 break;
6729 }
6730 case ISD::CONCAT_VECTORS: {
6731 SDValue Ops[] = {N1, N2};
6732 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
6733 return V;
6734 break;
6735 }
6736 case ISD::AND:
6737 assert(VT.isInteger() && "This operator does not apply to FP types!");
6738 assert(N1.getValueType() == N2.getValueType() &&
6739 N1.getValueType() == VT && "Binary operator types must match!");
6740 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
6741 // worth handling here.
6742 if (N2CV && N2CV->isZero())
6743 return N2;
6744 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
6745 return N1;
6746 break;
6747 case ISD::OR:
6748 case ISD::XOR:
6749 case ISD::ADD:
6750 case ISD::SUB:
6751 assert(VT.isInteger() && "This operator does not apply to FP types!");
6752 assert(N1.getValueType() == N2.getValueType() &&
6753 N1.getValueType() == VT && "Binary operator types must match!");
6754 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
6755 // it's worth handling here.
6756 if (N2CV && N2CV->isZero())
6757 return N1;
6758 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) && VT.isVector() &&
6759 VT.getVectorElementType() == MVT::i1)
6760 return getNode(ISD::XOR, DL, VT, N1, N2);
6761 break;
6762 case ISD::MUL:
6763 assert(VT.isInteger() && "This operator does not apply to FP types!");
6764 assert(N1.getValueType() == N2.getValueType() &&
6765 N1.getValueType() == VT && "Binary operator types must match!");
6766 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
6767 return getNode(ISD::AND, DL, VT, N1, N2);
6768 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
6769 const APInt &MulImm = N1->getConstantOperandAPInt(0);
6770 const APInt &N2CImm = N2C->getAPIntValue();
6771 return getVScale(DL, VT, MulImm * N2CImm);
6772 }
6773 break;
6774 case ISD::UDIV:
6775 case ISD::UREM:
6776 case ISD::MULHU:
6777 case ISD::MULHS:
6778 case ISD::SDIV:
6779 case ISD::SREM:
6780 case ISD::SADDSAT:
6781 case ISD::SSUBSAT:
6782 case ISD::UADDSAT:
6783 case ISD::USUBSAT:
6784 assert(VT.isInteger() && "This operator does not apply to FP types!");
6785 assert(N1.getValueType() == N2.getValueType() &&
6786 N1.getValueType() == VT && "Binary operator types must match!");
6787 if (VT.isVector() && VT.getVectorElementType() == MVT::i1) {
6788 // fold (add_sat x, y) -> (or x, y) for bool types.
6789 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
6790 return getNode(ISD::OR, DL, VT, N1, N2);
6791 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
6792 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
6793 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
6794 }
6795 break;
6796 case ISD::ABDS:
6797 case ISD::ABDU:
6798 assert(VT.isInteger() && "This operator does not apply to FP types!");
6799 assert(N1.getValueType() == N2.getValueType() &&
6800 N1.getValueType() == VT && "Binary operator types must match!");
6801 break;
6802 case ISD::SMIN:
6803 case ISD::UMAX:
6804 assert(VT.isInteger() && "This operator does not apply to FP types!");
6805 assert(N1.getValueType() == N2.getValueType() &&
6806 N1.getValueType() == VT && "Binary operator types must match!");
6807 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
6808 return getNode(ISD::OR, DL, VT, N1, N2);
6809 break;
6810 case ISD::SMAX:
6811 case ISD::UMIN:
6812 assert(VT.isInteger() && "This operator does not apply to FP types!");
6813 assert(N1.getValueType() == N2.getValueType() &&
6814 N1.getValueType() == VT && "Binary operator types must match!");
6815 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
6816 return getNode(ISD::AND, DL, VT, N1, N2);
6817 break;
6818 case ISD::FADD:
6819 case ISD::FSUB:
6820 case ISD::FMUL:
6821 case ISD::FDIV:
6822 case ISD::FREM:
6823 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
6824 assert(N1.getValueType() == N2.getValueType() &&
6825 N1.getValueType() == VT && "Binary operator types must match!");
6826 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
6827 return V;
6828 break;
6829 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
6830 assert(N1.getValueType() == VT &&
6833 "Invalid FCOPYSIGN!");
6834 break;
6835 case ISD::SHL:
6836 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
6837 const APInt &MulImm = N1->getConstantOperandAPInt(0);
6838 const APInt &ShiftImm = N2C->getAPIntValue();
6839 return getVScale(DL, VT, MulImm << ShiftImm);
6840 }
6841 [[fallthrough]];
6842 case ISD::SRA:
6843 case ISD::SRL:
6844 if (SDValue V = simplifyShift(N1, N2))
6845 return V;
6846 [[fallthrough]];
6847 case ISD::ROTL:
6848 case ISD::ROTR:
6849 assert(VT == N1.getValueType() &&
6850 "Shift operators return type must be the same as their first arg");
6851 assert(VT.isInteger() && N2.getValueType().isInteger() &&
6852 "Shifts only work on integers");
6853 assert((!VT.isVector() || VT == N2.getValueType()) &&
6854 "Vector shift amounts must be in the same as their first arg");
6855 // Verify that the shift amount VT is big enough to hold valid shift
6856 // amounts. This catches things like trying to shift an i1024 value by an
6857 // i8, which is easy to fall into in generic code that uses
6858 // TLI.getShiftAmount().
6861 "Invalid use of small shift amount with oversized value!");
6862
6863 // Always fold shifts of i1 values so the code generator doesn't need to
6864 // handle them. Since we know the size of the shift has to be less than the
6865 // size of the value, the shift/rotate count is guaranteed to be zero.
6866 if (VT == MVT::i1)
6867 return N1;
6868 if (N2CV && N2CV->isZero())
6869 return N1;
6870 break;
6871 case ISD::FP_ROUND:
6872 assert(VT.isFloatingPoint() &&
6874 VT.bitsLE(N1.getValueType()) &&
6875 N2C && (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
6876 "Invalid FP_ROUND!");
6877 if (N1.getValueType() == VT) return N1; // noop conversion.
6878 break;
6879 case ISD::AssertSext:
6880 case ISD::AssertZext: {
6881 EVT EVT = cast<VTSDNode>(N2)->getVT();
6882 assert(VT == N1.getValueType() && "Not an inreg extend!");
6883 assert(VT.isInteger() && EVT.isInteger() &&
6884 "Cannot *_EXTEND_INREG FP types");
6885 assert(!EVT.isVector() &&
6886 "AssertSExt/AssertZExt type should be the vector element type "
6887 "rather than the vector type!");
6888 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
6889 if (VT.getScalarType() == EVT) return N1; // noop assertion.
6890 break;
6891 }
6893 EVT EVT = cast<VTSDNode>(N2)->getVT();
6894 assert(VT == N1.getValueType() && "Not an inreg extend!");
6895 assert(VT.isInteger() && EVT.isInteger() &&
6896 "Cannot *_EXTEND_INREG FP types");
6897 assert(EVT.isVector() == VT.isVector() &&
6898 "SIGN_EXTEND_INREG type should be vector iff the operand "
6899 "type is vector!");
6900 assert((!EVT.isVector() ||
6902 "Vector element counts must match in SIGN_EXTEND_INREG");
6903 assert(EVT.bitsLE(VT) && "Not extending!");
6904 if (EVT == VT) return N1; // Not actually extending
6905
6906 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
6907 unsigned FromBits = EVT.getScalarSizeInBits();
6908 Val <<= Val.getBitWidth() - FromBits;
6909 Val.ashrInPlace(Val.getBitWidth() - FromBits);
6910 return getConstant(Val, DL, ConstantVT);
6911 };
6912
6913 if (N1C) {
6914 const APInt &Val = N1C->getAPIntValue();
6915 return SignExtendInReg(Val, VT);
6916 }
6917
6920 llvm::EVT OpVT = N1.getOperand(0).getValueType();
6921 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
6922 SDValue Op = N1.getOperand(i);
6923 if (Op.isUndef()) {
6924 Ops.push_back(getUNDEF(OpVT));
6925 continue;
6926 }
6927 ConstantSDNode *C = cast<ConstantSDNode>(Op);
6928 APInt Val = C->getAPIntValue();
6929 Ops.push_back(SignExtendInReg(Val, OpVT));
6930 }
6931 return getBuildVector(VT, DL, Ops);
6932 }
6933
6934 if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
6935 isa<ConstantSDNode>(N1.getOperand(0)))
6936 return getNode(
6937 ISD::SPLAT_VECTOR, DL, VT,
6938 SignExtendInReg(N1.getConstantOperandAPInt(0),
6939 N1.getOperand(0).getValueType()));
6940 break;
6941 }
6943 case ISD::FP_TO_UINT_SAT: {
6944 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
6945 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
6946 assert(N1.getValueType().isVector() == VT.isVector() &&
6947 "FP_TO_*INT_SAT type should be vector iff the operand type is "
6948 "vector!");
6949 assert((!VT.isVector() || VT.getVectorElementCount() ==
6951 "Vector element counts must match in FP_TO_*INT_SAT");
6952 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
6953 "Type to saturate to must be a scalar.");
6954 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
6955 "Not extending!");
6956 break;
6957 }
6960 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
6961 element type of the vector.");
6962
6963 // Extract from an undefined value or using an undefined index is undefined.
6964 if (N1.isUndef() || N2.isUndef())
6965 return getUNDEF(VT);
6966
6967 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
6968 // vectors. For scalable vectors we will provide appropriate support for
6969 // dealing with arbitrary indices.
6970 if (N2C && N1.getValueType().isFixedLengthVector() &&
6971 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
6972 return getUNDEF(VT);
6973
6974 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
6975 // expanding copies of large vectors from registers. This only works for
6976 // fixed length vectors, since we need to know the exact number of
6977 // elements.
6978 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
6980 unsigned Factor =
6983 N1.getOperand(N2C->getZExtValue() / Factor),
6984 getVectorIdxConstant(N2C->getZExtValue() % Factor, DL));
6985 }
6986
6987 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
6988 // lowering is expanding large vector constants.
6989 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
6990 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
6993 "BUILD_VECTOR used for scalable vectors");
6994 unsigned Index =
6995 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
6996 SDValue Elt = N1.getOperand(Index);
6997
6998 if (VT != Elt.getValueType())
6999 // If the vector element type is not legal, the BUILD_VECTOR operands
7000 // are promoted and implicitly truncated, and the result implicitly
7001 // extended. Make that explicit here.
7002 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7003
7004 return Elt;
7005 }
7006
7007 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7008 // operations are lowered to scalars.
7009 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7010 // If the indices are the same, return the inserted element else
7011 // if the indices are known different, extract the element from
7012 // the original vector.
7013 SDValue N1Op2 = N1.getOperand(2);
7014 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2);
7015
7016 if (N1Op2C && N2C) {
7017 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7018 if (VT == N1.getOperand(1).getValueType())
7019 return N1.getOperand(1);
7020 if (VT.isFloatingPoint()) {
7022 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7023 }
7024 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7025 }
7026 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7027 }
7028 }
7029
7030 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7031 // when vector types are scalarized and v1iX is legal.
7032 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7033 // Here we are completely ignoring the extract element index (N2),
7034 // which is fine for fixed width vectors, since any index other than 0
7035 // is undefined anyway. However, this cannot be ignored for scalable
7036 // vectors - in theory we could support this, but we don't want to do this
7037 // without a profitability check.
7038 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7040 N1.getValueType().getVectorNumElements() == 1) {
7041 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
7042 N1.getOperand(1));
7043 }
7044 break;
7046 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
7047 assert(!N1.getValueType().isVector() && !VT.isVector() &&
7048 (N1.getValueType().isInteger() == VT.isInteger()) &&
7049 N1.getValueType() != VT &&
7050 "Wrong types for EXTRACT_ELEMENT!");
7051
7052 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
7053 // 64-bit integers into 32-bit parts. Instead of building the extract of
7054 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
7055 if (N1.getOpcode() == ISD::BUILD_PAIR)
7056 return N1.getOperand(N2C->getZExtValue());
7057
7058 // EXTRACT_ELEMENT of a constant int is also very common.
7059 if (N1C) {
7060 unsigned ElementSize = VT.getSizeInBits();
7061 unsigned Shift = ElementSize * N2C->getZExtValue();
7062 const APInt &Val = N1C->getAPIntValue();
7063 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
7064 }
7065 break;
7067 EVT N1VT = N1.getValueType();
7068 assert(VT.isVector() && N1VT.isVector() &&
7069 "Extract subvector VTs must be vectors!");
7071 "Extract subvector VTs must have the same element type!");
7072 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
7073 "Cannot extract a scalable vector from a fixed length vector!");
7074 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7076 "Extract subvector must be from larger vector to smaller vector!");
7077 assert(N2C && "Extract subvector index must be a constant");
7078 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7079 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
7080 N1VT.getVectorMinNumElements()) &&
7081 "Extract subvector overflow!");
7082 assert(N2C->getAPIntValue().getBitWidth() ==
7083 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7084 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
7085
7086 // Trivial extraction.
7087 if (VT == N1VT)
7088 return N1;
7089
7090 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
7091 if (N1.isUndef())
7092 return getUNDEF(VT);
7093
7094 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
7095 // the concat have the same type as the extract.
7096 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
7097 VT == N1.getOperand(0).getValueType()) {
7098 unsigned Factor = VT.getVectorMinNumElements();
7099 return N1.getOperand(N2C->getZExtValue() / Factor);
7100 }
7101
7102 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
7103 // during shuffle legalization.
7104 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
7105 VT == N1.getOperand(1).getValueType())
7106 return N1.getOperand(1);
7107 break;
7108 }
7109 }
7110
7111 // Perform trivial constant folding.
7112 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}))
7113 return SV;
7114
7115 // Canonicalize an UNDEF to the RHS, even over a constant.
7116 if (N1.isUndef()) {
7117 if (TLI->isCommutativeBinOp(Opcode)) {
7118 std::swap(N1, N2);
7119 } else {
7120 switch (Opcode) {
7121 case ISD::SUB:
7122 return getUNDEF(VT); // fold op(undef, arg2) -> undef
7124 case ISD::UDIV:
7125 case ISD::SDIV:
7126 case ISD::UREM:
7127 case ISD::SREM:
7128 case ISD::SSUBSAT:
7129 case ISD::USUBSAT:
7130 return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0
7131 }
7132 }
7133 }
7134
7135 // Fold a bunch of operators when the RHS is undef.
7136 if (N2.isUndef()) {
7137 switch (Opcode) {
7138 case ISD::XOR:
7139 if (N1.isUndef())
7140 // Handle undef ^ undef -> 0 special case. This is a common
7141 // idiom (misuse).
7142 return getConstant(0, DL, VT);
7143 [[fallthrough]];
7144 case ISD::ADD:
7145 case ISD::SUB:
7146 case ISD::UDIV:
7147 case ISD::SDIV:
7148 case ISD::UREM:
7149 case ISD::SREM:
7150 return getUNDEF(VT); // fold op(arg1, undef) -> undef
7151 case ISD::MUL:
7152 case ISD::AND:
7153 case ISD::SSUBSAT:
7154 case ISD::USUBSAT:
7155 return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0
7156 case ISD::OR:
7157 case ISD::SADDSAT:
7158 case ISD::UADDSAT:
7159 return getAllOnesConstant(DL, VT);
7160 }
7161 }
7162
7163 // Memoize this node if possible.
7164 SDNode *N;
7165 SDVTList VTs = getVTList(VT);
7166 SDValue Ops[] = {N1, N2};
7167 if (VT != MVT::Glue) {
7169 AddNodeIDNode(ID, Opcode, VTs, Ops);
7170 void *IP = nullptr;
7171 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7172 E->intersectFlagsWith(Flags);
7173 return SDValue(E, 0);
7174 }
7175
7176 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7177 N->setFlags(Flags);
7178 createOperands(N, Ops);
7179 CSEMap.InsertNode(N, IP);
7180 } else {
7181 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7182 createOperands(N, Ops);
7183 }
7184
7185 InsertNode(N);
7186 SDValue V = SDValue(N, 0);
7187 NewSDValueDbgMsg(V, "Creating new node: ", this);
7188 return V;
7189}
7190
7191SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7192 SDValue N1, SDValue N2, SDValue N3) {
7193 SDNodeFlags Flags;
7194 if (Inserter)
7195 Flags = Inserter->getFlags();
7196 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
7197}
7198
7199SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7200 SDValue N1, SDValue N2, SDValue N3,
7201 const SDNodeFlags Flags) {
7203 N2.getOpcode() != ISD::DELETED_NODE &&
7204 N3.getOpcode() != ISD::DELETED_NODE &&
7205 "Operand is DELETED_NODE!");
7206 // Perform various simplifications.
7207 switch (Opcode) {
7208 case ISD::FMA:
7209 case ISD::FMAD: {
7210 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7211 assert(N1.getValueType() == VT && N2.getValueType() == VT &&
7212 N3.getValueType() == VT && "FMA types must match!");
7213 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
7214 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
7215 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
7216 if (N1CFP && N2CFP && N3CFP) {
7217 APFloat V1 = N1CFP->getValueAPF();
7218 const APFloat &V2 = N2CFP->getValueAPF();
7219 const APFloat &V3 = N3CFP->getValueAPF();
7220 if (Opcode == ISD::FMAD) {
7223 } else
7225 return getConstantFP(V1, DL, VT);
7226 }
7227 break;
7228 }
7229 case ISD::BUILD_VECTOR: {
7230 // Attempt to simplify BUILD_VECTOR.
7231 SDValue Ops[] = {N1, N2, N3};
7232 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7233 return V;
7234 break;
7235 }
7236 case ISD::CONCAT_VECTORS: {
7237 SDValue Ops[] = {N1, N2, N3};
7238 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7239 return V;
7240 break;
7241 }
7242 case ISD::SETCC: {
7243 assert(VT.isInteger() && "SETCC result type must be an integer!");
7244 assert(N1.getValueType() == N2.getValueType() &&
7245 "SETCC operands must have the same type!");
7246 assert(VT.isVector() == N1.getValueType().isVector() &&
7247 "SETCC type should be vector iff the operand type is vector!");
7248 assert((!VT.isVector() || VT.getVectorElementCount() ==
7250 "SETCC vector element counts must match!");
7251 // Use FoldSetCC to simplify SETCC's.
7252 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
7253 return V;
7254 // Vector constant folding.
7255 SDValue Ops[] = {N1, N2, N3};
7256 if (SDValue V = FoldConstantArithmetic(Opcode, DL, VT, Ops)) {
7257 NewSDValueDbgMsg(V, "New node vector constant folding: ", this);
7258 return V;
7259 }
7260 break;
7261 }
7262 case ISD::SELECT:
7263 case ISD::VSELECT:
7264 if (SDValue V = simplifySelect(N1, N2, N3))
7265 return V;
7266 break;
7268 llvm_unreachable("should use getVectorShuffle constructor!");
7269 case ISD::VECTOR_SPLICE: {
7270 if (cast<ConstantSDNode>(N3)->isZero())
7271 return N1;
7272 break;
7273 }
7275 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3);
7276 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
7277 // for scalable vectors where we will generate appropriate code to
7278 // deal with out-of-bounds cases correctly.
7279 if (N3C && N1.getValueType().isFixedLengthVector() &&
7281 return getUNDEF(VT);
7282
7283 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
7284 if (N3.isUndef())
7285 return getUNDEF(VT);
7286
7287 // If the inserted element is an UNDEF, just use the input vector.
7288 if (N2.isUndef())
7289 return N1;
7290
7291 break;
7292 }
7293 case ISD::INSERT_SUBVECTOR: {
7294 // Inserting undef into undef is still undef.
7295 if (N1.isUndef() && N2.isUndef())
7296 return getUNDEF(VT);
7297
7298 EVT N2VT = N2.getValueType();
7299 assert(VT == N1.getValueType() &&
7300 "Dest and insert subvector source types must match!");
7301 assert(VT.isVector() && N2VT.isVector() &&
7302 "Insert subvector VTs must be vectors!");
7304 "Insert subvector VTs must have the same element type!");
7305 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
7306 "Cannot insert a scalable vector into a fixed length vector!");
7307 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7309 "Insert subvector must be from smaller vector to larger vector!");
7310 assert(isa<ConstantSDNode>(N3) &&
7311 "Insert subvector index must be constant");
7312 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7313 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
7315 "Insert subvector overflow!");
7317 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7318 "Constant index for INSERT_SUBVECTOR has an invalid size");
7319
7320 // Trivial insertion.
7321 if (VT == N2VT)
7322 return N2;
7323
7324 // If this is an insert of an extracted vector into an undef vector, we
7325 // can just use the input to the extract.
7326 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7327 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT)
7328 return N2.getOperand(0);
7329 break;
7330 }
7331 case ISD::BITCAST:
7332 // Fold bit_convert nodes from a type to themselves.
7333 if (N1.getValueType() == VT)
7334 return N1;
7335 break;
7336 case ISD::VP_TRUNCATE:
7337 case ISD::VP_SIGN_EXTEND:
7338 case ISD::VP_ZERO_EXTEND:
7339 // Don't create noop casts.
7340 if (N1.getValueType() == VT)
7341 return N1;
7342 break;
7343 }
7344
7345 // Memoize node if it doesn't produce a glue result.
7346 SDNode *N;
7347 SDVTList VTs = getVTList(VT);
7348 SDValue Ops[] = {N1, N2, N3};
7349 if (VT != MVT::Glue) {
7351 AddNodeIDNode(ID, Opcode, VTs, Ops);
7352 void *IP = nullptr;
7353 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7354 E->intersectFlagsWith(Flags);
7355 return SDValue(E, 0);
7356 }
7357
7358 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7359 N->setFlags(Flags);
7360 createOperands(N, Ops);
7361 CSEMap.InsertNode(N, IP);
7362 } else {
7363 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7364 createOperands(N, Ops);
7365 }
7366
7367 InsertNode(N);
7368 SDValue V = SDValue(N, 0);
7369 NewSDValueDbgMsg(V, "Creating new node: ", this);
7370 return V;
7371}
7372
7373SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7374 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
7375 SDValue Ops[] = { N1, N2, N3, N4 };
7376 return getNode(Opcode, DL, VT, Ops);
7377}
7378
7379SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7380 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
7381 SDValue N5) {
7382 SDValue Ops[] = { N1, N2, N3, N4, N5 };
7383 return getNode(Opcode, DL, VT, Ops);
7384}
7385
7386/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
7387/// the incoming stack arguments to be loaded from the stack.
7389 SmallVector<SDValue, 8> ArgChains;
7390
7391 // Include the original chain at the beginning of the list. When this is
7392 // used by target LowerCall hooks, this helps legalize find the
7393 // CALLSEQ_BEGIN node.
7394 ArgChains.push_back(Chain);
7395
7396 // Add a chain value for each stack argument.
7397 for (SDNode *U : getEntryNode().getNode()->uses())
7398 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
7399 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
7400 if (FI->getIndex() < 0)
7401 ArgChains.push_back(SDValue(L, 1));
7402
7403 // Build a tokenfactor for all the chains.
7404 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
7405}
7406
7407/// getMemsetValue - Vectorized representation of the memset value
7408/// operand.
7410 const SDLoc &dl) {
7411 assert(!Value.isUndef());
7412
7413 unsigned NumBits = VT.getScalarSizeInBits();
7414 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
7415 assert(C->getAPIntValue().getBitWidth() == 8);
7416 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
7417 if (VT.isInteger()) {
7418 bool IsOpaque = VT.getSizeInBits() > 64 ||
7419 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
7420 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
7421 }
7422 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), dl,
7423 VT);
7424 }
7425
7426 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
7427 EVT IntVT = VT.getScalarType();
7428 if (!IntVT.isInteger())
7429 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
7430
7431 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
7432 if (NumBits > 8) {
7433 // Use a multiplication with 0x010101... to extend the input to the
7434 // required length.
7435 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
7436 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
7437 DAG.getConstant(Magic, dl, IntVT));
7438 }
7439
7440 if (VT != Value.getValueType() && !VT.isInteger())
7441 Value = DAG.getBitcast(VT.getScalarType(), Value);
7442 if (VT != Value.getValueType())
7443 Value = DAG.getSplatBuildVector(VT, dl, Value);
7444
7445 return Value;
7446}
7447
7448/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
7449/// used when a memcpy is turned into a memset when the source is a constant
7450/// string ptr.
7452 const TargetLowering &TLI,
7453 const ConstantDataArraySlice &Slice) {
7454 // Handle vector with all elements zero.
7455 if (Slice.Array == nullptr) {
7456 if (VT.isInteger())
7457 return DAG.getConstant(0, dl, VT);
7458 if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
7459 return DAG.getConstantFP(0.0, dl, VT);
7460 if (VT.isVector()) {
7461 unsigned NumElts = VT.getVectorNumElements();
7462 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
7463 return DAG.getNode(ISD::BITCAST, dl, VT,
7464 DAG.getConstant(0, dl,
7466 EltVT, NumElts)));
7467 }
7468 llvm_unreachable("Expected type!");
7469 }
7470
7471 assert(!VT.isVector() && "Can't handle vector type here!");
7472 unsigned NumVTBits = VT.getSizeInBits();
7473 unsigned NumVTBytes = NumVTBits / 8;
7474 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
7475
7476 APInt Val(NumVTBits, 0);
7477 if (DAG.getDataLayout().isLittleEndian()) {
7478 for (unsigned i = 0; i != NumBytes; ++i)
7479 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
7480 } else {
7481 for (unsigned i = 0; i != NumBytes; ++i)
7482 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
7483 }
7484
7485 // If the "cost" of materializing the integer immediate is less than the cost
7486 // of a load, then it is cost effective to turn the load into the immediate.
7487 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
7488 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
7489 return DAG.getConstant(Val, dl, VT);
7490 return SDValue();
7491}
7492
7494 const SDLoc &DL,
7495 const SDNodeFlags Flags) {
7496 EVT VT = Base.getValueType();
7497 SDValue Index;
7498
7499 if (Offset.isScalable())
7500 Index = getVScale(DL, Base.getValueType(),
7501 APInt(Base.getValueSizeInBits().getFixedValue(),
7502 Offset.getKnownMinValue()));
7503 else
7504 Index = getConstant(Offset.getFixedValue(), DL, VT);
7505
7506 return getMemBasePlusOffset(Base, Index, DL, Flags);
7507}
7508
7510 const SDLoc &DL,
7511 const SDNodeFlags Flags) {
7512 assert(Offset.getValueType().isInteger());
7513 EVT BasePtrVT = Ptr.getValueType();
7514 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, Flags);
7515}
7516
7517/// Returns true if memcpy source is constant data.
7519 uint64_t SrcDelta = 0;
7520 GlobalAddressSDNode *G = nullptr;
7521 if (Src.getOpcode() == ISD::GlobalAddress)
7522 G = cast<GlobalAddressSDNode>(Src);
7523 else if (Src.getOpcode() == ISD::ADD &&
7524 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
7525 Src.getOperand(1).getOpcode() == ISD::Constant) {
7526 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
7527 SrcDelta = Src.getConstantOperandVal(1);
7528 }
7529 if (!G)
7530 return false;
7531
7532 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
7533 SrcDelta + G->getOffset());
7534}
7535
7537 SelectionDAG &DAG) {
7538 // On Darwin, -Os means optimize for size without hurting performance, so
7539 // only really optimize for size when -Oz (MinSize) is used.
7541 return MF.getFunction().hasMinSize();
7542 return DAG.shouldOptForSize();
7543}
7544
7546 SmallVector<SDValue, 32> &OutChains, unsigned From,
7547 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
7548 SmallVector<SDValue, 16> &OutStoreChains) {
7549 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
7550 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
7551 SmallVector<SDValue, 16> GluedLoadChains;
7552 for (unsigned i = From; i < To; ++i) {
7553 OutChains.push_back(OutLoadChains[i]);
7554 GluedLoadChains.push_back(OutLoadChains[i]);
7555 }
7556
7557 // Chain for all loads.
7558 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
7559 GluedLoadChains);
7560
7561 for (unsigned i = From; i < To; ++i) {
7562 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
7563 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
7564 ST->getBasePtr(), ST->getMemoryVT(),
7565 ST->getMemOperand());
7566 OutChains.push_back(NewStore);
7567 }
7568}
7569
7571 SDValue Chain, SDValue Dst, SDValue Src,
7572 uint64_t Size, Align Alignment,
7573 bool isVol, bool AlwaysInline,
7574 MachinePointerInfo DstPtrInfo,
7575 MachinePointerInfo SrcPtrInfo,
7576 const AAMDNodes &AAInfo, AAResults *AA) {
7577 // Turn a memcpy of undef to nop.
7578 // FIXME: We need to honor volatile even is Src is undef.
7579 if (Src.isUndef())
7580 return Chain;
7581
7582 // Expand memcpy to a series of load and store ops if the size operand falls
7583 // below a certain threshold.
7584 // TODO: In the AlwaysInline case, if the size is big then generate a loop
7585 // rather than maybe a humongous number of loads and stores.
7586 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7587 const DataLayout &DL = DAG.getDataLayout();
7588 LLVMContext &C = *DAG.getContext();
7589 std::vector<EVT> MemOps;
7590 bool DstAlignCanChange = false;
7592 MachineFrameInfo &MFI = MF.getFrameInfo();
7593 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
7594 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
7595 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
7596 DstAlignCanChange = true;
7597 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
7598 if (!SrcAlign || Alignment > *SrcAlign)
7599 SrcAlign = Alignment;
7600 assert(SrcAlign && "SrcAlign must be set");
7602 // If marked as volatile, perform a copy even when marked as constant.
7603 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
7604 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
7605 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
7606 const MemOp Op = isZeroConstant
7607 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
7608 /*IsZeroMemset*/ true, isVol)
7609 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
7610 *SrcAlign, isVol, CopyFromConstant);
7611 if (!TLI.findOptimalMemOpLowering(
7612 MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
7613 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
7614 return SDValue();
7615
7616 if (DstAlignCanChange) {
7617 Type *Ty = MemOps[0].getTypeForEVT(C);
7618 Align NewAlign = DL.getABITypeAlign(Ty);
7619
7620 // Don't promote to an alignment that would require dynamic stack
7621 // realignment which may conflict with optimizations such as tail call
7622 // optimization.
7624 if (!TRI->hasStackRealignment(MF))
7625 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
7626 NewAlign = NewAlign.previous();
7627
7628 if (NewAlign > Alignment) {
7629 // Give the stack frame object a larger alignment if needed.
7630 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
7631 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
7632 Alignment = NewAlign;
7633 }
7634 }
7635
7636 // Prepare AAInfo for loads/stores after lowering this memcpy.
7637 AAMDNodes NewAAInfo = AAInfo;
7638 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
7639
7640 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
7641 bool isConstant =
7642 AA && SrcVal &&
7643 AA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
7644
7645 MachineMemOperand::Flags MMOFlags =
7647 SmallVector<SDValue, 16> OutLoadChains;
7648 SmallVector<SDValue, 16> OutStoreChains;
7649 SmallVector<SDValue, 32> OutChains;
7650 unsigned NumMemOps = MemOps.size();
7651 uint64_t SrcOff = 0, DstOff = 0;
7652 for (unsigned i = 0; i != NumMemOps; ++i) {
7653 EVT VT = MemOps[i];
7654 unsigned VTSize = VT.getSizeInBits() / 8;
7655 SDValue Value, Store;
7656
7657 if (VTSize > Size) {
7658 // Issuing an unaligned load / store pair that overlaps with the previous
7659 // pair. Adjust the offset accordingly.
7660 assert(i == NumMemOps-1 && i != 0);
7661 SrcOff -= VTSize - Size;
7662 DstOff -= VTSize - Size;
7663 }
7664
7665 if (CopyFromConstant &&
7666 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
7667 // It's unlikely a store of a vector immediate can be done in a single
7668 // instruction. It would require a load from a constantpool first.
7669 // We only handle zero vectors here.
7670 // FIXME: Handle other cases where store of vector immediate is done in
7671 // a single instruction.
7672 ConstantDataArraySlice SubSlice;
7673 if (SrcOff < Slice.Length) {
7674 SubSlice = Slice;
7675 SubSlice.move(SrcOff);
7676 } else {
7677 // This is an out-of-bounds access and hence UB. Pretend we read zero.
7678 SubSlice.Array = nullptr;
7679 SubSlice.Offset = 0;
7680 SubSlice.Length = VTSize;
7681 }
7682 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
7683 if (Value.getNode()) {
7684 Store = DAG.getStore(
7685 Chain, dl, Value,
7686 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
7687 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
7688 OutChains.push_back(Store);
7689 }
7690 }
7691
7692 if (!Store.getNode()) {
7693 // The type might not be legal for the target. This should only happen
7694 // if the type is smaller than a legal type, as on PPC, so the right
7695 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
7696 // to Load/Store if NVT==VT.
7697 // FIXME does the case above also need this?
7698 EVT NVT = TLI.getTypeToTransformTo(C, VT);
7699 assert(NVT.bitsGE(VT));
7700
7701 bool isDereferenceable =
7702 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
7703 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
7704 if (isDereferenceable)
7706 if (isConstant)
7707 SrcMMOFlags |= MachineMemOperand::MOInvariant;
7708
7709 Value = DAG.getExtLoad(
7710 ISD::EXTLOAD, dl, NVT, Chain,
7711 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
7712 SrcPtrInfo.getWithOffset(SrcOff), VT,
7713 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
7714 OutLoadChains.push_back(Value.getValue(1));
7715
7716 Store = DAG.getTruncStore(
7717 Chain, dl, Value,
7718 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
7719 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
7720 OutStoreChains.push_back(Store);
7721 }
7722 SrcOff += VTSize;
7723 DstOff += VTSize;
7724 Size -= VTSize;
7725 }
7726
7727 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
7729 unsigned NumLdStInMemcpy = OutStoreChains.size();
7730
7731 if (NumLdStInMemcpy) {
7732 // It may be that memcpy might be converted to memset if it's memcpy
7733 // of constants. In such a case, we won't have loads and stores, but
7734 // just stores. In the absence of loads, there is nothing to gang up.
7735 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
7736 // If target does not care, just leave as it.
7737 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
7738 OutChains.push_back(OutLoadChains[i]);
7739 OutChains.push_back(OutStoreChains[i]);
7740 }
7741 } else {
7742 // Ld/St less than/equal limit set by target.
7743 if (NumLdStInMemcpy <= GluedLdStLimit) {
7744 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
7745 NumLdStInMemcpy, OutLoadChains,
7746 OutStoreChains);
7747 } else {
7748 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
7749 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
7750 unsigned GlueIter = 0;
7751
7752 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
7753 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
7754 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
7755
7756 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
7757 OutLoadChains, OutStoreChains);
7758 GlueIter += GluedLdStLimit;
7759 }
7760
7761 // Residual ld/st.
7762 if (RemainingLdStInMemcpy) {
7763 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
7764 RemainingLdStInMemcpy, OutLoadChains,
7765 OutStoreChains);
7766 }
7767 }
7768 }
7769 }
7770 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
7771}
7772
7774 SDValue Chain, SDValue Dst, SDValue Src,
7775 uint64_t Size, Align Alignment,
7776 bool isVol, bool AlwaysInline,
7777 MachinePointerInfo DstPtrInfo,
7778 MachinePointerInfo SrcPtrInfo,
7779 const AAMDNodes &AAInfo) {
7780 // Turn a memmove of undef to nop.
7781 // FIXME: We need to honor volatile even is Src is undef.
7782 if (Src.isUndef())
7783 return Chain;
7784
7785 // Expand memmove to a series of load and store ops if the size operand falls
7786 // below a certain threshold.
7787 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7788 const DataLayout &DL = DAG.getDataLayout();
7789 LLVMContext &C = *DAG.getContext();
7790 std::vector<EVT> MemOps;
7791 bool DstAlignCanChange = false;
7793 MachineFrameInfo &MFI = MF.getFrameInfo();
7794 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
7795 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
7796 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
7797 DstAlignCanChange = true;
7798 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
7799 if (!SrcAlign || Alignment > *SrcAlign)
7800 SrcAlign = Alignment;
7801 assert(SrcAlign && "SrcAlign must be set");
7802 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
7803 if (!TLI.findOptimalMemOpLowering(
7804 MemOps, Limit,
7805 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
7806 /*IsVolatile*/ true),
7807 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
7808 MF.getFunction().getAttributes()))
7809 return SDValue();
7810
7811 if (DstAlignCanChange) {
7812 Type *Ty = MemOps[0].getTypeForEVT(C);
7813 Align NewAlign = DL.getABITypeAlign(Ty);
7814
7815 // Don't promote to an alignment that would require dynamic stack
7816 // realignment which may conflict with optimizations such as tail call
7817 // optimization.
7819 if (!TRI->hasStackRealignment(MF))
7820 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
7821 NewAlign = NewAlign.previous();
7822
7823 if (NewAlign > Alignment) {
7824 // Give the stack frame object a larger alignment if needed.
7825 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
7826 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
7827 Alignment = NewAlign;
7828 }
7829 }
7830
7831 // Prepare AAInfo for loads/stores after lowering this memmove.
7832 AAMDNodes NewAAInfo = AAInfo;
7833 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
7834
7835 MachineMemOperand::Flags MMOFlags =
7837 uint64_t SrcOff = 0, DstOff = 0;
7838 SmallVector<SDValue, 8> LoadValues;
7839 SmallVector<SDValue, 8> LoadChains;
7840 SmallVector<SDValue, 8> OutChains;
7841 unsigned NumMemOps = MemOps.size();
7842 for (unsigned i = 0; i < NumMemOps; i++) {
7843 EVT VT = MemOps[i];
7844 unsigned VTSize = VT.getSizeInBits() / 8;
7845 SDValue Value;
7846
7847 bool isDereferenceable =
7848 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
7849 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
7850 if (isDereferenceable)
7852
7853 Value = DAG.getLoad(
7854 VT, dl, Chain,
7855 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
7856 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
7857 LoadValues.push_back(Value);
7858 LoadChains.push_back(Value.getValue(1));
7859 SrcOff += VTSize;
7860 }
7861 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
7862 OutChains.clear();
7863 for (unsigned i = 0; i < NumMemOps; i++) {
7864 EVT VT = MemOps[i];
7865 unsigned VTSize = VT.getSizeInBits() / 8;
7866 SDValue Store;
7867
7868 Store = DAG.getStore(
7869 Chain, dl, LoadValues[i],
7870 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
7871 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
7872 OutChains.push_back(Store);
7873 DstOff += VTSize;
7874 }
7875
7876 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
7877}
7878
7879/// Lower the call to 'memset' intrinsic function into a series of store
7880/// operations.
7881///
7882/// \param DAG Selection DAG where lowered code is placed.
7883/// \param dl Link to corresponding IR location.
7884/// \param Chain Control flow dependency.
7885/// \param Dst Pointer to destination memory location.
7886/// \param Src Value of byte to write into the memory.
7887/// \param Size Number of bytes to write.
7888/// \param Alignment Alignment of the destination in bytes.
7889/// \param isVol True if destination is volatile.
7890/// \param AlwaysInline Makes sure no function call is generated.
7891/// \param DstPtrInfo IR information on the memory pointer.
7892/// \returns New head in the control flow, if lowering was successful, empty
7893/// SDValue otherwise.
7894///
7895/// The function tries to replace 'llvm.memset' intrinsic with several store
7896/// operations and value calculation code. This is usually profitable for small
7897/// memory size or when the semantic requires inlining.
7899 SDValue Chain, SDValue Dst, SDValue Src,
7900 uint64_t Size, Align Alignment, bool isVol,
7901 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
7902 const AAMDNodes &AAInfo) {
7903 // Turn a memset of undef to nop.
7904 // FIXME: We need to honor volatile even is Src is undef.
7905 if (Src.isUndef())
7906 return Chain;
7907
7908 // Expand memset to a series of load/store ops if the size operand
7909 // falls below a certain threshold.
7910 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7911 std::vector<EVT> MemOps;
7912 bool DstAlignCanChange = false;
7914 MachineFrameInfo &MFI = MF.getFrameInfo();
7915 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
7916 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
7917 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
7918 DstAlignCanChange = true;
7919 bool IsZeroVal = isNullConstant(Src);
7920 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
7921
7922 if (!TLI.findOptimalMemOpLowering(
7923 MemOps, Limit,
7924 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
7925 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
7926 return SDValue();
7927
7928 if (DstAlignCanChange) {
7929 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
7930 const DataLayout &DL = DAG.getDataLayout();
7931 Align NewAlign = DL.getABITypeAlign(Ty);
7932
7933 // Don't promote to an alignment that would require dynamic stack
7934 // realignment which may conflict with optimizations such as tail call
7935 // optimization.
7937 if (!TRI->hasStackRealignment(MF))
7938 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
7939 NewAlign = NewAlign.previous();
7940
7941 if (NewAlign > Alignment) {
7942 // Give the stack frame object a larger alignment if needed.
7943 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
7944 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
7945 Alignment = NewAlign;
7946 }
7947 }
7948
7949 SmallVector<SDValue, 8> OutChains;
7950 uint64_t DstOff = 0;
7951 unsigned NumMemOps = MemOps.size();
7952
7953 // Find the largest store and generate the bit pattern for it.
7954 EVT LargestVT = MemOps[0];
7955 for (unsigned i = 1; i < NumMemOps; i++)
7956 if (MemOps[i].bitsGT(LargestVT))
7957 LargestVT = MemOps[i];
7958 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
7959
7960 // Prepare AAInfo for loads/stores after lowering this memset.
7961 AAMDNodes NewAAInfo = AAInfo;
7962 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
7963
7964 for (unsigned i = 0; i < NumMemOps; i++) {
7965 EVT VT = MemOps[i];
7966 unsigned VTSize = VT.getSizeInBits() / 8;
7967 if (VTSize > Size) {
7968 // Issuing an unaligned load / store pair that overlaps with the previous
7969 // pair. Adjust the offset accordingly.
7970 assert(i == NumMemOps-1 && i != 0);
7971 DstOff -= VTSize - Size;
7972 }
7973
7974 // If this store is smaller than the largest store see whether we can get
7975 // the smaller value for free with a truncate or extract vector element and
7976 // then store.
7977 SDValue Value = MemSetValue;
7978 if (VT.bitsLT(LargestVT)) {
7979 unsigned Index;
7980 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
7981 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
7982 if (!LargestVT.isVector() && !VT.isVector() &&
7983 TLI.isTruncateFree(LargestVT, VT))
7984 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
7985 else if (LargestVT.isVector() && !VT.isVector() &&
7987 LargestVT.getTypeForEVT(*DAG.getContext()),
7988 VT.getSizeInBits(), Index) &&
7989 TLI.isTypeLegal(SVT) &&
7990 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
7991 // Target which can combine store(extractelement VectorTy, Idx) can get
7992 // the smaller value for free.
7993 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
7994 Value = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, TailValue,
7995 DAG.getVectorIdxConstant(Index, dl));
7996 } else
7997 Value = getMemsetValue(Src, VT, DAG, dl);
7998 }
7999 assert(Value.getValueType() == VT && "Value with wrong type.");
8000 SDValue Store = DAG.getStore(
8001 Chain, dl, Value,
8002 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8003 DstPtrInfo.getWithOffset(DstOff), Alignment,
8005 NewAAInfo);
8006 OutChains.push_back(Store);
8007 DstOff += VT.getSizeInBits() / 8;
8008 Size -= VTSize;
8009 }
8010
8011 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8012}
8013
8015 unsigned AS) {
8016 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
8017 // pointer operands can be losslessly bitcasted to pointers of address space 0
8018 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
8019 report_fatal_error("cannot lower memory intrinsic in address space " +
8020 Twine(AS));
8021 }
8022}
8023
8025 SDValue Src, SDValue Size, Align Alignment,
8026 bool isVol, bool AlwaysInline, bool isTailCall,
8027 MachinePointerInfo DstPtrInfo,
8028 MachinePointerInfo SrcPtrInfo,
8029 const AAMDNodes &AAInfo, AAResults *AA) {
8030 // Check to see if we should lower the memcpy to loads and stores first.
8031 // For cases within the target-specified limits, this is the best choice.
8032 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8033 if (ConstantSize) {
8034 // Memcpy with size zero? Just return the original chain.
8035 if (ConstantSize->isZero())
8036 return Chain;
8037
8039 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8040 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, AA);
8041 if (Result.getNode())
8042 return Result;
8043 }
8044
8045 // Then check to see if we should lower the memcpy with target-specific
8046 // code. If the target chooses to do this, this is the next best.
8047 if (TSI) {
8048 SDValue Result = TSI->EmitTargetCodeForMemcpy(
8049 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
8050 DstPtrInfo, SrcPtrInfo);
8051 if (Result.getNode())
8052 return Result;
8053 }
8054
8055 // If we really need inline code and the target declined to provide it,
8056 // use a (potentially long) sequence of loads and stores.
8057 if (AlwaysInline) {
8058 assert(ConstantSize && "AlwaysInline requires a constant size!");
8060 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8061 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, AA);
8062 }
8063
8066
8067 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
8068 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
8069 // respect volatile, so they may do things like read or write memory
8070 // beyond the given memory regions. But fixing this isn't easy, and most
8071 // people don't care.
8072
8073 // Emit a library call.
8076 Entry.Ty = PointerType::getUnqual(*getContext());
8077 Entry.Node = Dst; Args.push_back(Entry);
8078 Entry.Node = Src; Args.push_back(Entry);
8079
8080 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8081 Entry.Node = Size; Args.push_back(Entry);
8082 // FIXME: pass in SDLoc
8084 CLI.setDebugLoc(dl)
8085 .setChain(Chain)
8086 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
8087 Dst.getValueType().getTypeForEVT(*getContext()),
8088 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
8089 TLI->getPointerTy(getDataLayout())),
8090 std::move(Args))
8092 .setTailCall(isTailCall);
8093
8094 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8095 return CallResult.second;
8096}
8097
8099 SDValue Dst, SDValue Src, SDValue Size,
8100 Type *SizeTy, unsigned ElemSz,
8101 bool isTailCall,
8102 MachinePointerInfo DstPtrInfo,
8103 MachinePointerInfo SrcPtrInfo) {
8104 // Emit a library call.
8107 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8108 Entry.Node = Dst;
8109 Args.push_back(Entry);
8110
8111 Entry.Node = Src;
8112 Args.push_back(Entry);
8113
8114 Entry.Ty = SizeTy;
8115 Entry.Node = Size;
8116 Args.push_back(Entry);
8117
8118 RTLIB::Libcall LibraryCall =
8120 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8121 report_fatal_error("Unsupported element size");
8122
8124 CLI.setDebugLoc(dl)
8125 .setChain(Chain)
8126 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8128 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8129 TLI->getPointerTy(getDataLayout())),
8130 std::move(Args))
8132 .setTailCall(isTailCall);
8133
8134 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8135 return CallResult.second;
8136}
8137
8139 SDValue Src, SDValue Size, Align Alignment,
8140 bool isVol, bool isTailCall,
8141 MachinePointerInfo DstPtrInfo,
8142 MachinePointerInfo SrcPtrInfo,
8143 const AAMDNodes &AAInfo, AAResults *AA) {
8144 // Check to see if we should lower the memmove to loads and stores first.
8145 // For cases within the target-specified limits, this is the best choice.
8146 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8147 if (ConstantSize) {
8148 // Memmove with size zero? Just return the original chain.
8149 if (ConstantSize->isZero())
8150 return Chain;
8151
8153 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8154 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
8155 if (Result.getNode())
8156 return Result;
8157 }
8158
8159 // Then check to see if we should lower the memmove with target-specific
8160 // code. If the target chooses to do this, this is the next best.
8161 if (TSI) {
8162 SDValue Result =
8163 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
8164 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
8165 if (Result.getNode())
8166 return Result;
8167 }
8168
8171
8172 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
8173 // not be safe. See memcpy above for more details.
8174
8175 // Emit a library call.
8178 Entry.Ty = PointerType::getUnqual(*getContext());
8179 Entry.Node = Dst; Args.push_back(Entry);
8180 Entry.Node = Src; Args.push_back(Entry);
8181
8182 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8183 Entry.Node = Size; Args.push_back(Entry);
8184 // FIXME: pass in SDLoc
8186 CLI.setDebugLoc(dl)
8187 .setChain(Chain)
8188 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
8189 Dst.getValueType().getTypeForEVT(*getContext()),
8190 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
8191 TLI->getPointerTy(getDataLayout())),
8192 std::move(Args))
8194 .setTailCall(isTailCall);
8195
8196 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8197 return CallResult.second;
8198}
8199
8201 SDValue Dst, SDValue Src, SDValue Size,
8202 Type *SizeTy, unsigned ElemSz,
8203 bool isTailCall,
8204 MachinePointerInfo DstPtrInfo,
8205 MachinePointerInfo SrcPtrInfo) {
8206 // Emit a library call.
8209 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8210 Entry.Node = Dst;
8211 Args.push_back(Entry);
8212
8213 Entry.Node = Src;
8214 Args.push_back(Entry);
8215
8216 Entry.Ty = SizeTy;
8217 Entry.Node = Size;
8218 Args.push_back(Entry);
8219
8220 RTLIB::Libcall LibraryCall =
8222 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8223 report_fatal_error("Unsupported element size");
8224
8226 CLI.setDebugLoc(dl)
8227 .setChain(Chain)
8228 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8230 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8231 TLI->getPointerTy(getDataLayout())),
8232 std::move(Args))
8234 .setTailCall(isTailCall);
8235
8236 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8237 return CallResult.second;
8238}
8239
8241 SDValue Src, SDValue Size, Align Alignment,
8242 bool isVol, bool AlwaysInline, bool isTailCall,
8243 MachinePointerInfo DstPtrInfo,
8244 const AAMDNodes &AAInfo) {
8245 // Check to see if we should lower the memset to stores first.
8246 // For cases within the target-specified limits, this is the best choice.
8247 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8248 if (ConstantSize) {
8249 // Memset with size zero? Just return the original chain.
8250 if (ConstantSize->isZero())
8251 return Chain;
8252
8253 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8254 ConstantSize->getZExtValue(), Alignment,
8255 isVol, false, DstPtrInfo, AAInfo);
8256
8257 if (Result.getNode())
8258 return Result;
8259 }
8260
8261 // Then check to see if we should lower the memset with target-specific
8262 // code. If the target chooses to do this, this is the next best.
8263 if (TSI) {
8264 SDValue Result = TSI->EmitTargetCodeForMemset(
8265 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
8266 if (Result.getNode())
8267 return Result;
8268 }
8269
8270 // If we really need inline code and the target declined to provide it,
8271 // use a (potentially long) sequence of loads and stores.
8272 if (AlwaysInline) {
8273 assert(ConstantSize && "AlwaysInline requires a constant size!");
8274 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8275 ConstantSize->getZExtValue(), Alignment,
8276 isVol, true, DstPtrInfo, AAInfo);
8277 assert(Result &&
8278 "getMemsetStores must return a valid sequence when AlwaysInline");
8279 return Result;
8280 }
8281
8283
8284 // Emit a library call.
8285 auto &Ctx = *getContext();
8286 const auto& DL = getDataLayout();
8287
8289 // FIXME: pass in SDLoc
8290 CLI.setDebugLoc(dl).setChain(Chain);
8291
8292 const char *BzeroName = getTargetLoweringInfo().getLibcallName(RTLIB::BZERO);
8293
8294 // Helper function to create an Entry from Node and Type.
8295 const auto CreateEntry = [](SDValue Node, Type *Ty) {
8297 Entry.Node = Node;
8298 Entry.Ty = Ty;
8299 return Entry;
8300 };
8301
8302 // If zeroing out and bzero is present, use it.
8303 if (isNullConstant(Src) && BzeroName) {
8305 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8306 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8307 CLI.setLibCallee(
8308 TLI->getLibcallCallingConv(RTLIB::BZERO), Type::getVoidTy(Ctx),
8309 getExternalSymbol(BzeroName, TLI->getPointerTy(DL)), std::move(Args));
8310 } else {
8312 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8313 Args.push_back(CreateEntry(Src, Src.getValueType().getTypeForEVT(Ctx)));
8314 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8315 CLI.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
8316 Dst.getValueType().getTypeForEVT(Ctx),
8317 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
8318 TLI->getPointerTy(DL)),
8319 std::move(Args));
8320 }
8321
8322 CLI.setDiscardResult().setTailCall(isTailCall);
8323
8324 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8325 return CallResult.second;
8326}
8327
8330 Type *SizeTy, unsigned ElemSz,
8331 bool isTailCall,
8332 MachinePointerInfo DstPtrInfo) {
8333 // Emit a library call.
8336 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8337 Entry.Node = Dst;
8338 Args.push_back(Entry);
8339
8340 Entry.Ty = Type::getInt8Ty(*getContext());
8341 Entry.Node = Value;
8342 Args.push_back(Entry);
8343
8344 Entry.Ty = SizeTy;
8345 Entry.Node = Size;
8346 Args.push_back(Entry);
8347
8348 RTLIB::Libcall LibraryCall =
8350 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8351 report_fatal_error("Unsupported element size");
8352
8354 CLI.setDebugLoc(dl)
8355 .setChain(Chain)
8356 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8358 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8359 TLI->getPointerTy(getDataLayout())),
8360 std::move(Args))
8362 .setTailCall(isTailCall);
8363
8364 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8365 return CallResult.second;
8366}
8367
8368SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8369 SDVTList VTList, ArrayRef<SDValue> Ops,
8370 MachineMemOperand *MMO) {
8372 ID.AddInteger(MemVT.getRawBits());
8373 AddNodeIDNode(ID, Opcode, VTList, Ops);
8374 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8375 ID.AddInteger(MMO->getFlags());
8376 void* IP = nullptr;
8377 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8378 cast<AtomicSDNode>(E)->refineAlignment(MMO);
8379 return SDValue(E, 0);
8380 }
8381
8382 auto *N = newSDNode<AtomicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8383 VTList, MemVT, MMO);
8384 createOperands(N, Ops);
8385
8386 CSEMap.InsertNode(N, IP);
8387 InsertNode(N);
8388 return SDValue(N, 0);
8389}
8390
8392 EVT MemVT, SDVTList VTs, SDValue Chain,
8393 SDValue Ptr, SDValue Cmp, SDValue Swp,
8394 MachineMemOperand *MMO) {
8395 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
8397 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
8398
8399 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
8400 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8401}
8402
8403SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8404 SDValue Chain, SDValue Ptr, SDValue Val,
8405 MachineMemOperand *MMO) {
8406 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
8407 Opcode == ISD::ATOMIC_LOAD_SUB ||
8408 Opcode == ISD::ATOMIC_LOAD_AND ||
8409 Opcode == ISD::ATOMIC_LOAD_CLR ||
8410 Opcode == ISD::ATOMIC_LOAD_OR ||
8411 Opcode == ISD::ATOMIC_LOAD_XOR ||
8412 Opcode == ISD::ATOMIC_LOAD_NAND ||
8413 Opcode == ISD::ATOMIC_LOAD_MIN ||
8414 Opcode == ISD::ATOMIC_LOAD_MAX ||
8415 Opcode == ISD::ATOMIC_LOAD_UMIN ||
8416 Opcode == ISD::ATOMIC_LOAD_UMAX ||
8417 Opcode == ISD::ATOMIC_LOAD_FADD ||
8418 Opcode == ISD::ATOMIC_LOAD_FSUB ||
8419 Opcode == ISD::ATOMIC_LOAD_FMAX ||
8420 Opcode == ISD::ATOMIC_LOAD_FMIN ||
8421 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
8422 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
8423 Opcode == ISD::ATOMIC_SWAP ||
8424 Opcode == ISD::ATOMIC_STORE) &&
8425 "Invalid Atomic Op");
8426
8427 EVT VT = Val.getValueType();
8428
8429 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
8430 getVTList(VT, MVT::Other);
8431 SDValue Ops[] = {Chain, Ptr, Val};
8432 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8433}
8434
8435SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8436 EVT VT, SDValue Chain, SDValue Ptr,
8437 MachineMemOperand *MMO) {
8438 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
8439
8440 SDVTList VTs = getVTList(VT, MVT::Other);
8441 SDValue Ops[] = {Chain, Ptr};
8442 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8443}
8444
8445/// getMergeValues - Create a MERGE_VALUES node from the given operands.
8447 if (Ops.size() == 1)
8448 return Ops[0];
8449
8451 VTs.reserve(Ops.size());
8452 for (const SDValue &Op : Ops)
8453 VTs.push_back(Op.getValueType());
8454 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
8455}
8456
8458 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
8459 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
8461 const AAMDNodes &AAInfo) {
8462 if (Size.hasValue() && !Size.getValue())
8464
8466 MachineMemOperand *MMO =
8467 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
8468
8469 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
8470}
8471
8473 SDVTList VTList,
8474 ArrayRef<SDValue> Ops, EVT MemVT,
8475 MachineMemOperand *MMO) {
8476 assert((Opcode == ISD::INTRINSIC_VOID ||
8477 Opcode == ISD::INTRINSIC_W_CHAIN ||
8478 Opcode == ISD::PREFETCH ||
8479 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
8480 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
8481 "Opcode is not a memory-accessing opcode!");
8482
8483 // Memoize the node unless it returns a glue result.
8485 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
8487 AddNodeIDNode(ID, Opcode, VTList, Ops);
8488 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
8489 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
8490 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8491 ID.AddInteger(MMO->getFlags());
8492 ID.AddInteger(MemVT.getRawBits());
8493 void *IP = nullptr;
8494 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8495 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
8496 return SDValue(E, 0);
8497 }
8498
8499 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8500 VTList, MemVT, MMO);
8501 createOperands(N, Ops);
8502
8503 CSEMap.InsertNode(N, IP);
8504 } else {
8505 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8506 VTList, MemVT, MMO);
8507 createOperands(N, Ops);
8508 }
8509 InsertNode(N);
8510 SDValue V(N, 0);
8511 NewSDValueDbgMsg(V, "Creating new node: ", this);
8512 return V;
8513}
8514
8516 SDValue Chain, int FrameIndex,
8517 int64_t Size, int64_t Offset) {
8518 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
8519 const auto VTs = getVTList(MVT::Other);
8520 SDValue Ops[2] = {
8521 Chain,
8522 getFrameIndex(FrameIndex,
8523 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
8524 true)};
8525
8527 AddNodeIDNode(ID, Opcode, VTs, Ops);
8528 ID.AddInteger(FrameIndex);
8529 ID.AddInteger(Size);
8530 ID.AddInteger(Offset);
8531 void *IP = nullptr;
8532 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
8533 return SDValue(E, 0);
8534
8535 LifetimeSDNode *N = newSDNode<LifetimeSDNode>(
8536 Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs, Size, Offset);
8537 createOperands(N, Ops);
8538 CSEMap.InsertNode(N, IP);
8539 InsertNode(N);
8540 SDValue V(N, 0);
8541 NewSDValueDbgMsg(V, "Creating new node: ", this);
8542 return V;
8543}
8544
8546 uint64_t Guid, uint64_t Index,
8547 uint32_t Attr) {
8548 const unsigned Opcode = ISD::PSEUDO_PROBE;
8549 const auto VTs = getVTList(MVT::Other);
8550 SDValue Ops[] = {Chain};
8552 AddNodeIDNode(ID, Opcode, VTs, Ops);
8553 ID.AddInteger(Guid);
8554 ID.AddInteger(Index);
8555 void *IP = nullptr;
8556 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
8557 return SDValue(E, 0);
8558
8559 auto *N = newSDNode<PseudoProbeSDNode>(
8560 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
8561 createOperands(N, Ops);
8562 CSEMap.InsertNode(N, IP);
8563 InsertNode(N);
8564 SDValue V(N, 0);
8565 NewSDValueDbgMsg(V, "Creating new node: ", this);
8566 return V;
8567}
8568
8569/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
8570/// MachinePointerInfo record from it. This is particularly useful because the
8571/// code generator has many cases where it doesn't bother passing in a
8572/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
8574 SelectionDAG &DAG, SDValue Ptr,
8575 int64_t Offset = 0) {
8576 // If this is FI+Offset, we can model it.
8577 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
8579 FI->getIndex(), Offset);
8580
8581 // If this is (FI+Offset1)+Offset2, we can model it.
8582 if (Ptr.getOpcode() != ISD::ADD ||
8583 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
8584 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
8585 return Info;
8586
8587 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
8589 DAG.getMachineFunction(), FI,
8590 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
8591}
8592
8593/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
8594/// MachinePointerInfo record from it. This is particularly useful because the
8595/// code generator has many cases where it doesn't bother passing in a
8596/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
8598 SelectionDAG &DAG, SDValue Ptr,
8599 SDValue OffsetOp) {
8600 // If the 'Offset' value isn't a constant, we can't handle this.
8601 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
8602 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
8603 if (OffsetOp.isUndef())
8604 return InferPointerInfo(Info, DAG, Ptr);
8605 return Info;
8606}
8607
8609 EVT VT, const SDLoc &dl, SDValue Chain,
8611 MachinePointerInfo PtrInfo, EVT MemVT,
8612 Align Alignment,
8613 MachineMemOperand::Flags MMOFlags,
8614 const AAMDNodes &AAInfo, const MDNode *Ranges) {
8615 assert(Chain.getValueType() == MVT::Other &&
8616 "Invalid chain type");
8617
8618 MMOFlags |= MachineMemOperand::MOLoad;
8619 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
8620 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
8621 // clients.
8622 if (PtrInfo.V.isNull())
8623 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
8624
8627 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
8628 Alignment, AAInfo, Ranges);
8629 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
8630}
8631
8633 EVT VT, const SDLoc &dl, SDValue Chain,
8634 SDValue Ptr, SDValue Offset, EVT MemVT,
8635 MachineMemOperand *MMO) {
8636 if (VT == MemVT) {
8637 ExtType = ISD::NON_EXTLOAD;
8638 } else if (ExtType == ISD::NON_EXTLOAD) {
8639 assert(VT == MemVT && "Non-extending load from different memory type!");
8640 } else {
8641 // Extending load.
8642 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
8643 "Should only be an extending load, not truncating!");
8644 assert(VT.isInteger() == MemVT.isInteger() &&
8645 "Cannot convert from FP to Int or Int -> FP!");
8646 assert(VT.isVector() == MemVT.isVector() &&
8647 "Cannot use an ext load to convert to or from a vector!");
8648 assert((!VT.isVector() ||
8650 "Cannot use an ext load to change the number of vector elements!");
8651 }
8652
8653 bool Indexed = AM != ISD::UNINDEXED;
8654 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
8655
8656 SDVTList VTs = Indexed ?
8657 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
8658 SDValue Ops[] = { Chain, Ptr, Offset };
8660 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
8661 ID.AddInteger(MemVT.getRawBits());
8662 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
8663 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
8664 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8665 ID.AddInteger(MMO->getFlags());
8666 void *IP = nullptr;
8667 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8668 cast<LoadSDNode>(E)->refineAlignment(MMO);
8669 return SDValue(E, 0);
8670 }
8671 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
8672 ExtType, MemVT, MMO);
8673 createOperands(N, Ops);
8674
8675 CSEMap.InsertNode(N, IP);
8676 InsertNode(N);
8677 SDValue V(N, 0);
8678 NewSDValueDbgMsg(V, "Creating new node: ", this);
8679 return V;
8680}
8681
8684 MaybeAlign Alignment,
8685 MachineMemOperand::Flags MMOFlags,
8686 const AAMDNodes &AAInfo, const MDNode *Ranges) {
8687 SDValue Undef = getUNDEF(Ptr.getValueType());
8688 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
8689 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
8690}
8691
8694 SDValue Undef = getUNDEF(Ptr.getValueType());
8695 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
8696 VT, MMO);
8697}
8698
8700 EVT VT, SDValue Chain, SDValue Ptr,
8701 MachinePointerInfo PtrInfo, EVT MemVT,
8702 MaybeAlign Alignment,
8703 MachineMemOperand::Flags MMOFlags,
8704 const AAMDNodes &AAInfo) {
8705 SDValue Undef = getUNDEF(Ptr.getValueType());
8706 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
8707 MemVT, Alignment, MMOFlags, AAInfo);
8708}
8709
8711 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
8712 MachineMemOperand *MMO) {
8713 SDValue Undef = getUNDEF(Ptr.getValueType());
8714 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
8715 MemVT, MMO);
8716}
8717
8721 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
8722 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
8723 // Don't propagate the invariant or dereferenceable flags.
8724 auto MMOFlags =
8725 LD->getMemOperand()->getFlags() &
8727 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
8728 LD->getChain(), Base, Offset, LD->getPointerInfo(),
8729 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
8730}
8731
8734 Align Alignment,
8735 MachineMemOperand::Flags MMOFlags,
8736 const AAMDNodes &AAInfo) {
8737 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
8738
8739 MMOFlags |= MachineMemOperand::MOStore;
8740 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
8741
8742 if (PtrInfo.V.isNull())
8743 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
8744
8747 MachineMemOperand *MMO =
8748 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
8749 return getStore(Chain, dl, Val, Ptr, MMO);
8750}
8751
8754 assert(Chain.getValueType() == MVT::Other &&
8755 "Invalid chain type");
8756 EVT VT = Val.getValueType();
8757 SDVTList VTs = getVTList(MVT::Other);
8758 SDValue Undef = getUNDEF(Ptr.getValueType());
8759 SDValue Ops[] = { Chain, Val, Ptr, Undef };
8761 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
8762 ID.AddInteger(VT.getRawBits());
8763 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
8764 dl.getIROrder(), VTs, ISD::UNINDEXED, false, VT, MMO));
8765 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8766 ID.AddInteger(MMO->getFlags());
8767 void *IP = nullptr;
8768 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8769 cast<StoreSDNode>(E)->refineAlignment(MMO);
8770 return SDValue(E, 0);
8771 }
8772 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
8773 ISD::UNINDEXED, false, VT, MMO);
8774 createOperands(N, Ops);
8775
8776 CSEMap.InsertNode(N, IP);
8777 InsertNode(N);
8778 SDValue V(N, 0);
8779 NewSDValueDbgMsg(V, "Creating new node: ", this);
8780 return V;
8781}
8782
8785 EVT SVT, Align Alignment,
8786 MachineMemOperand::Flags MMOFlags,
8787 const AAMDNodes &AAInfo) {
8788 assert(Chain.getValueType() == MVT::Other &&
8789 "Invalid chain type");
8790
8791 MMOFlags |= MachineMemOperand::MOStore;
8792 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
8793
8794 if (PtrInfo.V.isNull())
8795 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
8796
8799 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
8800 AAInfo);
8801 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
8802}
8803
8805 SDValue Ptr, EVT SVT,
8806 MachineMemOperand *MMO) {
8807 EVT VT = Val.getValueType();
8808
8809 assert(Chain.getValueType() == MVT::Other &&
8810 "Invalid chain type");
8811 if (VT == SVT)
8812 return getStore(Chain, dl, Val, Ptr, MMO);
8813
8815 "Should only be a truncating store, not extending!");
8816 assert(VT.isInteger() == SVT.isInteger() &&
8817 "Can't do FP-INT conversion!");
8818 assert(VT.isVector() == SVT.isVector() &&
8819 "Cannot use trunc store to convert to or from a vector!");
8820 assert((!VT.isVector() ||
8822 "Cannot use trunc store to change the number of vector elements!");
8823
8824 SDVTList VTs = getVTList(MVT::Other);
8825 SDValue Undef = getUNDEF(Ptr.getValueType());
8826 SDValue Ops[] = { Chain, Val, Ptr, Undef };
8828 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
8829 ID.AddInteger(SVT.getRawBits());
8830 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
8831 dl.getIROrder(), VTs, ISD::UNINDEXED, true, SVT, MMO));
8832 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8833 ID.AddInteger(MMO->getFlags());
8834 void *IP = nullptr;
8835 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8836 cast<StoreSDNode>(E)->refineAlignment(MMO);
8837 return SDValue(E, 0);
8838 }
8839 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
8840 ISD::UNINDEXED, true, SVT, MMO);
8841 createOperands(N, Ops);
8842
8843 CSEMap.InsertNode(N, IP);
8844 InsertNode(N);
8845 SDValue V(N, 0);
8846 NewSDValueDbgMsg(V, "Creating new node: ", this);
8847 return V;
8848}
8849
8853 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
8854 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
8855 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
8856 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
8858 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
8859 ID.AddInteger(ST->getMemoryVT().getRawBits());
8860 ID.AddInteger(ST->getRawSubclassData());
8861 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
8862 ID.AddInteger(ST->getMemOperand()->getFlags());
8863 void *IP = nullptr;
8864 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
8865 return SDValue(E, 0);
8866
8867 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
8868 ST->isTruncatingStore(), ST->getMemoryVT(),
8869 ST->getMemOperand());
8870 createOperands(N, Ops);
8871
8872 CSEMap.InsertNode(N, IP);
8873 InsertNode(N);
8874 SDValue V(N, 0);
8875 NewSDValueDbgMsg(V, "Creating new node: ", this);
8876 return V;
8877}
8878
8880 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
8881 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
8882 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
8883 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
8884 const MDNode *Ranges, bool IsExpanding) {
8885 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
8886
8887 MMOFlags |= MachineMemOperand::MOLoad;
8888 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
8889 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
8890 // clients.
8891 if (PtrInfo.V.isNull())
8892 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
8893
8896 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
8897 Alignment, AAInfo, Ranges);
8898 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
8899 MMO, IsExpanding);
8900}
8901
8903 ISD::LoadExtType ExtType, EVT VT,
8904 const SDLoc &dl, SDValue Chain, SDValue Ptr,
8905 SDValue Offset, SDValue Mask, SDValue EVL,
8906 EVT MemVT, MachineMemOperand *MMO,
8907 bool IsExpanding) {
8908 bool Indexed = AM != ISD::UNINDEXED;
8909 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
8910
8911 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
8912 : getVTList(VT, MVT::Other);
8913 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
8915 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
8916 ID.AddInteger(MemVT.getRawBits());
8917 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
8918 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
8919 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8920 ID.AddInteger(MMO->getFlags());
8921 void *IP = nullptr;
8922 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8923 cast<VPLoadSDNode>(E)->refineAlignment(MMO);
8924 return SDValue(E, 0);
8925 }
8926 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
8927 ExtType, IsExpanding, MemVT, MMO);
8928 createOperands(N, Ops);
8929
8930 CSEMap.InsertNode(N, IP);
8931 InsertNode(N);
8932 SDValue V(N, 0);
8933 NewSDValueDbgMsg(V, "Creating new node: ", this);
8934 return V;
8935}
8936
8938 SDValue Ptr, SDValue Mask, SDValue EVL,
8939 MachinePointerInfo PtrInfo,
8940 MaybeAlign Alignment,
8941 MachineMemOperand::Flags MMOFlags,
8942 const AAMDNodes &AAInfo, const MDNode *Ranges,
8943 bool IsExpanding) {
8944 SDValue Undef = getUNDEF(Ptr.getValueType());
8945 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
8946 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
8947 IsExpanding);
8948}
8949
8951 SDValue Ptr, SDValue Mask, SDValue EVL,
8952 MachineMemOperand *MMO, bool IsExpanding) {
8953 SDValue Undef = getUNDEF(Ptr.getValueType());
8954 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
8955 Mask, EVL, VT, MMO, IsExpanding);
8956}
8957
8959 EVT VT, SDValue Chain, SDValue Ptr,
8960 SDValue Mask, SDValue EVL,
8961 MachinePointerInfo PtrInfo, EVT MemVT,
8962 MaybeAlign Alignment,
8963 MachineMemOperand::Flags MMOFlags,
8964 const AAMDNodes &AAInfo, bool IsExpanding) {
8965 SDValue Undef = getUNDEF(Ptr.getValueType());
8966 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
8967 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
8968 IsExpanding);
8969}
8970
8972 EVT VT, SDValue Chain, SDValue Ptr,
8973 SDValue Mask, SDValue EVL, EVT MemVT,
8974 MachineMemOperand *MMO, bool IsExpanding) {
8975 SDValue Undef = getUNDEF(Ptr.getValueType());
8976 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
8977 EVL, MemVT, MMO, IsExpanding);
8978}
8979
8983 auto *LD = cast<VPLoadSDNode>(OrigLoad);
8984 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
8985 // Don't propagate the invariant or dereferenceable flags.
8986 auto MMOFlags =
8987 LD->getMemOperand()->getFlags() &
8989 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
8990 LD->getChain(), Base, Offset, LD->getMask(),
8991 LD->getVectorLength(), LD->getPointerInfo(),
8992 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
8993 nullptr, LD->isExpandingLoad());
8994}
8995
8998 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
8999 ISD::MemIndexedMode AM, bool IsTruncating,
9000 bool IsCompressing) {
9001 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9002 bool Indexed = AM != ISD::UNINDEXED;
9003 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9004 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9005 : getVTList(MVT::Other);
9006 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
9008 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9009 ID.AddInteger(MemVT.getRawBits());
9010 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9011 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9012 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9013 ID.AddInteger(MMO->getFlags());
9014 void *IP = nullptr;
9015 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9016 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9017 return SDValue(E, 0);
9018 }
9019 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9020 IsTruncating, IsCompressing, MemVT, MMO);
9021 createOperands(N, Ops);
9022
9023 CSEMap.InsertNode(N, IP);
9024 InsertNode(N);
9025 SDValue V(N, 0);
9026 NewSDValueDbgMsg(V, "Creating new node: ", this);
9027 return V;
9028}
9029
9031 SDValue Val, SDValue Ptr, SDValue Mask,
9032 SDValue EVL, MachinePointerInfo PtrInfo,
9033 EVT SVT, Align Alignment,
9034 MachineMemOperand::Flags MMOFlags,
9035 const AAMDNodes &AAInfo,
9036 bool IsCompressing) {
9037 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9038
9039 MMOFlags |= MachineMemOperand::MOStore;
9040 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9041
9042 if (PtrInfo.V.isNull())
9043 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9044
9047 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
9048 AAInfo);
9049 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
9050 IsCompressing);
9051}
9052
9054 SDValue Val, SDValue Ptr, SDValue Mask,
9055 SDValue EVL, EVT SVT,
9056 MachineMemOperand *MMO,
9057 bool IsCompressing) {
9058 EVT VT = Val.getValueType();
9059
9060 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9061 if (VT == SVT)
9062 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
9063 EVL, VT, MMO, ISD::UNINDEXED,
9064 /*IsTruncating*/ false, IsCompressing);
9065
9067 "Should only be a truncating store, not extending!");
9068 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9069 assert(VT.isVector() == SVT.isVector() &&
9070 "Cannot use trunc store to convert to or from a vector!");
9071 assert((!VT.isVector() ||
9073 "Cannot use trunc store to change the number of vector elements!");
9074
9075 SDVTList VTs = getVTList(MVT::Other);
9076 SDValue Undef = getUNDEF(Ptr.getValueType());
9077 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
9079 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9080 ID.AddInteger(SVT.getRawBits());
9081 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9082 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9083 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9084 ID.AddInteger(MMO->getFlags());
9085 void *IP = nullptr;
9086 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9087 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9088 return SDValue(E, 0);
9089 }
9090 auto *N =
9091 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9092 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
9093 createOperands(N, Ops);
9094
9095 CSEMap.InsertNode(N, IP);
9096 InsertNode(N);
9097 SDValue V(N, 0);
9098 NewSDValueDbgMsg(V, "Creating new node: ", this);
9099 return V;
9100}
9101
9105 auto *ST = cast<VPStoreSDNode>(OrigStore);
9106 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
9107 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
9108 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
9109 Offset, ST->getMask(), ST->getVectorLength()};
9111 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9112 ID.AddInteger(ST->getMemoryVT().getRawBits());
9113 ID.AddInteger(ST->getRawSubclassData());
9114 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
9115 ID.AddInteger(ST->getMemOperand()->getFlags());
9116 void *IP = nullptr;
9117 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9118 return SDValue(E, 0);
9119
9120 auto *N = newSDNode<VPStoreSDNode>(
9121 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
9122 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
9123 createOperands(N, Ops);
9124
9125 CSEMap.InsertNode(N, IP);
9126 InsertNode(N);
9127 SDValue V(N, 0);
9128 NewSDValueDbgMsg(V, "Creating new node: ", this);
9129 return V;
9130}
9131
9133 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
9134 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
9135 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
9136 bool Indexed = AM != ISD::UNINDEXED;
9137 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9138
9139 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
9140 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9141 : getVTList(VT, MVT::Other);
9143 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
9144 ID.AddInteger(VT.getRawBits());
9145 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
9146 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9147 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9148
9149 void *IP = nullptr;
9150 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9151 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
9152 return SDValue(E, 0);
9153 }
9154
9155 auto *N =
9156 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
9157 ExtType, IsExpanding, MemVT, MMO);
9158 createOperands(N, Ops);
9159 CSEMap.InsertNode(N, IP);
9160 InsertNode(N);
9161 SDValue V(N, 0);
9162 NewSDValueDbgMsg(V, "Creating new node: ", this);
9163 return V;
9164}
9165
9167 SDValue Ptr, SDValue Stride,
9168 SDValue Mask, SDValue EVL,
9169 MachineMemOperand *MMO,
9170 bool IsExpanding) {
9171 SDValue Undef = getUNDEF(Ptr.getValueType());
9173 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
9174}
9175
9177 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
9178 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
9179 MachineMemOperand *MMO, bool IsExpanding) {
9180 SDValue Undef = getUNDEF(Ptr.getValueType());
9181 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
9182 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
9183}
9184
9186 SDValue Val, SDValue Ptr,
9187 SDValue Offset, SDValue Stride,
9188 SDValue Mask, SDValue EVL, EVT MemVT,
9189 MachineMemOperand *MMO,
9191 bool IsTruncating, bool IsCompressing) {
9192 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9193 bool Indexed = AM != ISD::UNINDEXED;
9194 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9195 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9196 : getVTList(MVT::Other);
9197 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
9199 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9200 ID.AddInteger(MemVT.getRawBits());
9201 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9202 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9203 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9204 void *IP = nullptr;
9205 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9206 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9207 return SDValue(E, 0);
9208 }
9209 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9210 VTs, AM, IsTruncating,
9211 IsCompressing, MemVT, MMO);
9212 createOperands(N, Ops);
9213
9214 CSEMap.InsertNode(N, IP);
9215 InsertNode(N);
9216 SDValue V(N, 0);
9217 NewSDValueDbgMsg(V, "Creating new node: ", this);
9218 return V;
9219}
9220
9222 SDValue Val, SDValue Ptr,
9223 SDValue Stride, SDValue Mask,
9224 SDValue EVL, EVT SVT,
9225 MachineMemOperand *MMO,
9226 bool IsCompressing) {
9227 EVT VT = Val.getValueType();
9228
9229 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9230 if (VT == SVT)
9231 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
9232 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
9233 /*IsTruncating*/ false, IsCompressing);
9234
9236 "Should only be a truncating store, not extending!");
9237 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9238 assert(VT.isVector() == SVT.isVector() &&
9239 "Cannot use trunc store to convert to or from a vector!");
9240 assert((!VT.isVector() ||
9242 "Cannot use trunc store to change the number of vector elements!");
9243
9244 SDVTList VTs = getVTList(MVT::Other);
9245 SDValue Undef = getUNDEF(Ptr.getValueType());
9246 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
9248 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9249 ID.AddInteger(SVT.getRawBits());
9250 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9251 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9252 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9253 void *IP = nullptr;
9254 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9255 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9256 return SDValue(E, 0);
9257 }
9258 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9259 VTs, ISD::UNINDEXED, true,
9260 IsCompressing, SVT, MMO);
9261 createOperands(N, Ops);
9262
9263 CSEMap.InsertNode(N, IP);
9264 InsertNode(N);
9265 SDValue V(N, 0);
9266 NewSDValueDbgMsg(V, "Creating new node: ", this);
9267 return V;
9268}
9269
9272 ISD::MemIndexType IndexType) {
9273 assert(Ops.size() == 6 && "Incompatible number of operands");
9274
9276 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
9277 ID.AddInteger(VT.getRawBits());
9278 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
9279 dl.getIROrder(), VTs, VT, MMO, IndexType));
9280 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9281 ID.AddInteger(MMO->getFlags());
9282 void *IP = nullptr;
9283 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9284 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
9285 return SDValue(E, 0);
9286 }
9287
9288 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9289 VT, MMO, IndexType);
9290 createOperands(N, Ops);
9291
9292 assert(N->getMask().getValueType().getVectorElementCount() ==
9293 N->getValueType(0).getVectorElementCount() &&
9294 "Vector width mismatch between mask and data");
9295 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9296 N->getValueType(0).getVectorElementCount().isScalable() &&
9297 "Scalable flags of index and data do not match");
9299 N->getIndex().getValueType().getVectorElementCount(),
9300 N->getValueType(0).getVectorElementCount()) &&
9301 "Vector width mismatch between index and data");
9302 assert(isa<ConstantSDNode>(N->getScale()) &&
9303 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9304 "Scale should be a constant power of 2");
9305
9306 CSEMap.InsertNode(N, IP);
9307 InsertNode(N);
9308 SDValue V(N, 0);
9309 NewSDValueDbgMsg(V, "Creating new node: ", this);
9310 return V;
9311}
9312
9315 MachineMemOperand *MMO,
9316 ISD::MemIndexType IndexType) {
9317 assert(Ops.size() == 7 && "Incompatible number of operands");
9318
9320 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
9321 ID.AddInteger(VT.getRawBits());
9322 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
9323 dl.getIROrder(), VTs, VT, MMO, IndexType));
9324 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9325 ID.AddInteger(MMO->getFlags());
9326 void *IP = nullptr;
9327 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9328 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
9329 return SDValue(E, 0);
9330 }
9331 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9332 VT, MMO, IndexType);
9333 createOperands(N, Ops);
9334
9335 assert(N->getMask().getValueType().getVectorElementCount() ==
9336 N->getValue().getValueType().getVectorElementCount() &&
9337 "Vector width mismatch between mask and data");
9338 assert(
9339 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9340 N->getValue().getValueType().getVectorElementCount().isScalable() &&
9341 "Scalable flags of index and data do not match");
9343 N->getIndex().getValueType().getVectorElementCount(),
9344 N->getValue().getValueType().getVectorElementCount()) &&
9345 "Vector width mismatch between index and data");
9346 assert(isa<ConstantSDNode>(N->getScale()) &&
9347 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9348 "Scale should be a constant power of 2");
9349
9350 CSEMap.InsertNode(N, IP);
9351 InsertNode(N);
9352 SDValue V(N, 0);
9353 NewSDValueDbgMsg(V, "Creating new node: ", this);
9354 return V;
9355}
9356
9359 SDValue PassThru, EVT MemVT,
9360 MachineMemOperand *MMO,
9362 ISD::LoadExtType ExtTy, bool isExpanding) {
9363 bool Indexed = AM != ISD::UNINDEXED;
9364 assert((Indexed || Offset.isUndef()) &&
9365 "Unindexed masked load with an offset!");
9366 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
9367 : getVTList(VT, MVT::Other);
9368 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
9370 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
9371 ID.AddInteger(MemVT.getRawBits());
9372 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
9373 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
9374 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9375 ID.AddInteger(MMO->getFlags());
9376 void *IP = nullptr;
9377 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9378 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
9379 return SDValue(E, 0);
9380 }
9381 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9382 AM, ExtTy, isExpanding, MemVT, MMO);
9383 createOperands(N, Ops);
9384
9385 CSEMap.InsertNode(N, IP);
9386 InsertNode(N);
9387 SDValue V(N, 0);
9388 NewSDValueDbgMsg(V, "Creating new node: ", this);
9389 return V;
9390}
9391
9395 MaskedLoadSDNode *LD = cast<MaskedLoadSDNode>(OrigLoad);
9396 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
9397 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
9398 Offset, LD->getMask(), LD->getPassThru(),
9399 LD->getMemoryVT(), LD->getMemOperand(), AM,
9400 LD->getExtensionType(), LD->isExpandingLoad());
9401}
9402
9405 SDValue Mask, EVT MemVT,
9406 MachineMemOperand *MMO,
9407 ISD::MemIndexedMode AM, bool IsTruncating,
9408 bool IsCompressing) {
9409 assert(Chain.getValueType() == MVT::Other &&
9410 "Invalid chain type");
9411 bool Indexed = AM != ISD::UNINDEXED;
9412 assert((Indexed || Offset.isUndef()) &&
9413 "Unindexed masked store with an offset!");
9414 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
9415 : getVTList(MVT::Other);
9416 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
9418 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
9419 ID.AddInteger(MemVT.getRawBits());
9420 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
9421 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9422 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9423 ID.AddInteger(MMO->getFlags());
9424 void *IP = nullptr;
9425 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9426 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
9427 return SDValue(E, 0);
9428 }
9429 auto *N =
9430 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9431 IsTruncating, IsCompressing, MemVT, MMO);
9432 createOperands(N, Ops);
9433
9434 CSEMap.InsertNode(N, IP);
9435 InsertNode(N);
9436 SDValue V(N, 0);
9437 NewSDValueDbgMsg(V, "Creating new node: ", this);
9438 return V;
9439}
9440
9444 MaskedStoreSDNode *ST = cast<MaskedStoreSDNode>(OrigStore);
9445 assert(ST->getOffset().isUndef() &&
9446 "Masked store is already a indexed store!");
9447 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
9448 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
9449 AM, ST->isTruncatingStore(), ST->isCompressingStore());
9450}
9451
9454 MachineMemOperand *MMO,
9455 ISD::MemIndexType IndexType,
9456 ISD::LoadExtType ExtTy) {
9457 assert(Ops.size() == 6 && "Incompatible number of operands");
9458
9460 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
9461 ID.AddInteger(MemVT.getRawBits());
9462 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
9463 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
9464 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9465 ID.AddInteger(MMO->getFlags());
9466 void *IP = nullptr;
9467 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9468 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
9469 return SDValue(E, 0);
9470 }
9471
9472 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9473 VTs, MemVT, MMO, IndexType, ExtTy);
9474 createOperands(N, Ops);
9475
9476 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
9477 "Incompatible type of the PassThru value in MaskedGatherSDNode");
9478 assert(N->getMask().getValueType().getVectorElementCount() ==
9479 N->getValueType(0).getVectorElementCount() &&
9480 "Vector width mismatch between mask and data");
9481 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9482 N->getValueType(0).getVectorElementCount().isScalable() &&
9483 "Scalable flags of index and data do not match");
9485 N->getIndex().getValueType().getVectorElementCount(),
9486 N->getValueType(0).getVectorElementCount()) &&
9487 "Vector width mismatch between index and data");
9488 assert(isa<ConstantSDNode>(N->getScale()) &&
9489 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9490 "Scale should be a constant power of 2");
9491
9492 CSEMap.InsertNode(N, IP);
9493 InsertNode(N);
9494 SDValue V(N, 0);
9495 NewSDValueDbgMsg(V, "Creating new node: ", this);
9496 return V;
9497}
9498
9501 MachineMemOperand *MMO,
9502 ISD::MemIndexType IndexType,
9503 bool IsTrunc) {
9504 assert(Ops.size() == 6 && "Incompatible number of operands");
9505
9507 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
9508 ID.AddInteger(MemVT.getRawBits());
9509 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
9510 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
9511 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9512 ID.AddInteger(MMO->getFlags());
9513 void *IP = nullptr;
9514 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9515 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
9516 return SDValue(E, 0);
9517 }
9518
9519 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9520 VTs, MemVT, MMO, IndexType, IsTrunc);
9521 createOperands(N, Ops);
9522
9523 assert(N->getMask().getValueType().getVectorElementCount() ==
9524 N->getValue().getValueType().getVectorElementCount() &&
9525 "Vector width mismatch between mask and data");
9526 assert(
9527 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9528 N->getValue().getValueType().getVectorElementCount().isScalable() &&
9529 "Scalable flags of index and data do not match");
9531 N->getIndex().getValueType().getVectorElementCount(),
9532 N->getValue().getValueType().getVectorElementCount()) &&
9533 "Vector width mismatch between index and data");
9534 assert(isa<ConstantSDNode>(N->getScale()) &&
9535 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9536 "Scale should be a constant power of 2");
9537
9538 CSEMap.InsertNode(N, IP);
9539 InsertNode(N);
9540 SDValue V(N, 0);
9541 NewSDValueDbgMsg(V, "Creating new node: ", this);
9542 return V;
9543}
9544
9546 EVT MemVT, MachineMemOperand *MMO) {
9547 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9548 SDVTList VTs = getVTList(MVT::Other);
9549 SDValue Ops[] = {Chain, Ptr};
9552 ID.AddInteger(MemVT.getRawBits());
9553 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
9554 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
9555 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9556 ID.AddInteger(MMO->getFlags());
9557 void *IP = nullptr;
9558 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9559 return SDValue(E, 0);
9560
9561 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
9562 dl.getDebugLoc(), VTs, MemVT, MMO);
9563 createOperands(N, Ops);
9564
9565 CSEMap.InsertNode(N, IP);
9566 InsertNode(N);
9567 SDValue V(N, 0);
9568 NewSDValueDbgMsg(V, "Creating new node: ", this);
9569 return V;
9570}
9571
9573 EVT MemVT, MachineMemOperand *MMO) {
9574 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9575 SDVTList VTs = getVTList(MVT::Other);
9576 SDValue Ops[] = {Chain, Ptr};
9579 ID.AddInteger(MemVT.getRawBits());
9580 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
9581 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
9582 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9583 ID.AddInteger(MMO->getFlags());
9584 void *IP = nullptr;
9585 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9586 return SDValue(E, 0);
9587
9588 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
9589 dl.getDebugLoc(), VTs, MemVT, MMO);
9590 createOperands(N, Ops);
9591
9592 CSEMap.InsertNode(N, IP);
9593 InsertNode(N);
9594 SDValue V(N, 0);
9595 NewSDValueDbgMsg(V, "Creating new node: ", this);
9596 return V;
9597}
9598
9600 // select undef, T, F --> T (if T is a constant), otherwise F
9601 // select, ?, undef, F --> F
9602 // select, ?, T, undef --> T
9603 if (Cond.isUndef())
9604 return isConstantValueOfAnyType(T) ? T : F;
9605 if (T.isUndef())
9606 return F;
9607 if (F.isUndef())
9608 return T;
9609
9610 // select true, T, F --> T
9611 // select false, T, F --> F
9612 if (auto *CondC = dyn_cast<ConstantSDNode>(Cond))
9613 return CondC->isZero() ? F : T;
9614
9615 // TODO: This should simplify VSELECT with non-zero constant condition using
9616 // something like this (but check boolean contents to be complete?):
9617 if (ConstantSDNode *CondC = isConstOrConstSplat(Cond, /*AllowUndefs*/ false,
9618 /*AllowTruncation*/ true))
9619 if (CondC->isZero())
9620 return F;
9621
9622 // select ?, T, T --> T
9623 if (T == F)
9624 return T;
9625
9626 return SDValue();
9627}
9628
9630 // shift undef, Y --> 0 (can always assume that the undef value is 0)
9631 if (X.isUndef())
9632 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
9633 // shift X, undef --> undef (because it may shift by the bitwidth)
9634 if (Y.isUndef())
9635 return getUNDEF(X.getValueType());
9636
9637 // shift 0, Y --> 0
9638 // shift X, 0 --> X
9640 return X;
9641
9642 // shift X, C >= bitwidth(X) --> undef
9643 // All vector elements must be too big (or undef) to avoid partial undefs.
9644 auto isShiftTooBig = [X](ConstantSDNode *Val) {
9645 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
9646 };
9647 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
9648 return getUNDEF(X.getValueType());
9649
9650 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
9651 if (X.getValueType().getScalarType() == MVT::i1)
9652 return X;
9653
9654 return SDValue();
9655}
9656
9658 SDNodeFlags Flags) {
9659 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
9660 // (an undef operand can be chosen to be Nan/Inf), then the result of this
9661 // operation is poison. That result can be relaxed to undef.
9662 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
9663 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
9664 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
9665 (YC && YC->getValueAPF().isNaN());
9666 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
9667 (YC && YC->getValueAPF().isInfinity());
9668
9669 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
9670 return getUNDEF(X.getValueType());
9671
9672 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
9673 return getUNDEF(X.getValueType());
9674
9675 if (!YC)
9676 return SDValue();
9677
9678 // X + -0.0 --> X
9679 if (Opcode == ISD::FADD)
9680 if (YC->getValueAPF().isNegZero())
9681 return X;
9682
9683 // X - +0.0 --> X
9684 if (Opcode == ISD::FSUB)
9685 if (YC->getValueAPF().isPosZero())
9686 return X;
9687
9688 // X * 1.0 --> X
9689 // X / 1.0 --> X
9690 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
9691 if (YC->getValueAPF().isExactlyValue(1.0))
9692 return X;
9693
9694 // X * 0.0 --> 0.0
9695 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
9696 if (YC->getValueAPF().isZero())
9697 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
9698
9699 return SDValue();
9700}
9701
9703 SDValue Ptr, SDValue SV, unsigned Align) {
9704 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
9705 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
9706}
9707
9708SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9709 ArrayRef<SDUse> Ops) {
9710 switch (Ops.size()) {
9711 case 0: return getNode(Opcode, DL, VT);
9712 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
9713 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
9714 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
9715 default: break;
9716 }
9717
9718 // Copy from an SDUse array into an SDValue array for use with
9719 // the regular getNode logic.
9720 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
9721 return getNode(Opcode, DL, VT, NewOps);
9722}
9723
9724SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9725 ArrayRef<SDValue> Ops) {
9726 SDNodeFlags Flags;
9727 if (Inserter)
9728 Flags = Inserter->getFlags();
9729 return getNode(Opcode, DL, VT, Ops, Flags);
9730}
9731
9732SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9733 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
9734 unsigned NumOps = Ops.size();
9735 switch (NumOps) {
9736 case 0: return getNode(Opcode, DL, VT);
9737 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
9738 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
9739 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
9740 default: break;
9741 }
9742
9743#ifndef NDEBUG
9744 for (const auto &Op : Ops)
9745 assert(Op.getOpcode() != ISD::DELETED_NODE &&
9746 "Operand is DELETED_NODE!");
9747#endif
9748
9749 switch (Opcode) {
9750 default: break;
9751 case ISD::BUILD_VECTOR:
9752 // Attempt to simplify BUILD_VECTOR.
9753 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
9754 return V;
9755 break;
9757 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
9758 return V;
9759 break;
9760 case ISD::SELECT_CC:
9761 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
9762 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
9763 "LHS and RHS of condition must have same type!");
9764 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
9765 "True and False arms of SelectCC must have same type!");
9766 assert(Ops[2].getValueType() == VT &&
9767 "select_cc node must be of same type as true and false value!");
9768 assert((!Ops[0].getValueType().isVector() ||
9769 Ops[0].getValueType().getVectorElementCount() ==
9770 VT.getVectorElementCount()) &&
9771 "Expected select_cc with vector result to have the same sized "
9772 "comparison type!");
9773 break;
9774 case ISD::BR_CC:
9775 assert(NumOps == 5 && "BR_CC takes 5 operands!");
9776 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
9777 "LHS/RHS of comparison should match types!");
9778 break;
9779 case ISD::VP_ADD:
9780 case ISD::VP_SUB:
9781 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
9782 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
9783 Opcode = ISD::VP_XOR;
9784 break;
9785 case ISD::VP_MUL:
9786 // If it is VP_MUL mask operation then turn it to VP_AND
9787 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
9788 Opcode = ISD::VP_AND;
9789 break;
9790 case ISD::VP_REDUCE_MUL:
9791 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
9792 if (VT == MVT::i1)
9793 Opcode = ISD::VP_REDUCE_AND;
9794 break;
9795 case ISD::VP_REDUCE_ADD:
9796 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
9797 if (VT == MVT::i1)
9798 Opcode = ISD::VP_REDUCE_XOR;
9799 break;
9800 case ISD::VP_REDUCE_SMAX:
9801 case ISD::VP_REDUCE_UMIN:
9802 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
9803 // VP_REDUCE_AND.
9804 if (VT == MVT::i1)
9805 Opcode = ISD::VP_REDUCE_AND;
9806 break;
9807 case ISD::VP_REDUCE_SMIN:
9808 case ISD::VP_REDUCE_UMAX:
9809 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
9810 // VP_REDUCE_OR.
9811 if (VT == MVT::i1)
9812 Opcode = ISD::VP_REDUCE_OR;
9813 break;
9814 }
9815
9816 // Memoize nodes.
9817 SDNode *N;
9818 SDVTList VTs = getVTList(VT);
9819
9820 if (VT != MVT::Glue) {
9822 AddNodeIDNode(ID, Opcode, VTs, Ops);
9823 void *IP = nullptr;
9824
9825 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
9826 return SDValue(E, 0);
9827
9828 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
9829 createOperands(N, Ops);
9830
9831 CSEMap.InsertNode(N, IP);
9832 } else {
9833 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
9834 createOperands(N, Ops);
9835 }
9836
9837 N->setFlags(Flags);
9838 InsertNode(N);
9839 SDValue V(N, 0);
9840 NewSDValueDbgMsg(V, "Creating new node: ", this);
9841 return V;
9842}
9843
9844SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
9845 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
9846 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
9847}
9848
9849SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
9850 ArrayRef<SDValue> Ops) {
9851 SDNodeFlags Flags;
9852 if (Inserter)
9853 Flags = Inserter->getFlags();
9854 return getNode(Opcode, DL, VTList, Ops, Flags);
9855}
9856
9857SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
9858 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
9859 if (VTList.NumVTs == 1)
9860 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
9861
9862#ifndef NDEBUG
9863 for (const auto &Op : Ops)
9864 assert(Op.getOpcode() != ISD::DELETED_NODE &&
9865 "Operand is DELETED_NODE!");
9866#endif
9867
9868 switch (Opcode) {
9869 case ISD::SADDO:
9870 case ISD::UADDO:
9871 case ISD::SSUBO:
9872 case ISD::USUBO: {
9873 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
9874 "Invalid add/sub overflow op!");
9875 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
9876 Ops[0].getValueType() == Ops[1].getValueType() &&
9877 Ops[0].getValueType() == VTList.VTs[0] &&
9878 "Binary operator types must match!");
9879 SDValue N1 = Ops[0], N2 = Ops[1];
9880 canonicalizeCommutativeBinop(Opcode, N1, N2);
9881
9882 // (X +- 0) -> X with zero-overflow.
9883 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
9884 /*AllowTruncation*/ true);
9885 if (N2CV && N2CV->isZero()) {
9886 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
9887 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
9888 }
9889
9890 if (VTList.VTs[0].isVector() &&
9891 VTList.VTs[0].getVectorElementType() == MVT::i1 &&
9892 VTList.VTs[1].getVectorElementType() == MVT::i1) {
9893 SDValue F1 = getFreeze(N1);
9894 SDValue F2 = getFreeze(N2);
9895 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
9896 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
9897 return getNode(ISD::MERGE_VALUES, DL, VTList,
9898 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
9899 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
9900 Flags);
9901 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
9902 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
9903 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
9904 return getNode(ISD::MERGE_VALUES, DL, VTList,
9905 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
9906 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
9907 Flags);
9908 }
9909 }
9910 break;
9911 }
9912 case ISD::SADDO_CARRY:
9913 case ISD::UADDO_CARRY:
9914 case ISD::SSUBO_CARRY:
9915 case ISD::USUBO_CARRY:
9916 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
9917 "Invalid add/sub overflow op!");
9918 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
9919 Ops[0].getValueType() == Ops[1].getValueType() &&
9920 Ops[0].getValueType() == VTList.VTs[0] &&
9921 Ops[2].getValueType() == VTList.VTs[1] &&
9922 "Binary operator types must match!");
9923 break;
9924 case ISD::SMUL_LOHI:
9925 case ISD::UMUL_LOHI: {
9926 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
9927 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
9928 VTList.VTs[0] == Ops[0].getValueType() &&
9929 VTList.VTs[0] == Ops[1].getValueType() &&
9930 "Binary operator types must match!");
9931 // Constant fold.
9932 ConstantSDNode *LHS = dyn_cast<ConstantSDNode>(Ops[0]);
9933 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ops[1]);
9934 if (LHS && RHS) {
9935 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
9936 unsigned OutWidth = Width * 2;
9937 APInt Val = LHS->getAPIntValue();
9938 APInt Mul = RHS->getAPIntValue();
9939 if (Opcode == ISD::SMUL_LOHI) {
9940 Val = Val.sext(OutWidth);
9941 Mul = Mul.sext(OutWidth);
9942 } else {
9943 Val = Val.zext(OutWidth);
9944 Mul = Mul.zext(OutWidth);
9945 }
9946 Val *= Mul;
9947
9948 SDValue Hi =
9949 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
9950 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
9951 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
9952 }
9953 break;
9954 }
9955 case ISD::FFREXP: {
9956 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
9957 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
9958 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
9959
9960 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Ops[0])) {
9961 int FrexpExp;
9962 APFloat FrexpMant =
9963 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
9964 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
9965 SDValue Result1 =
9966 getConstant(FrexpMant.isFinite() ? FrexpExp : 0, DL, VTList.VTs[1]);
9967 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
9968 }
9969
9970 break;
9971 }
9973 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
9974 "Invalid STRICT_FP_EXTEND!");
9975 assert(VTList.VTs[0].isFloatingPoint() &&
9976 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
9977 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
9978 "STRICT_FP_EXTEND result type should be vector iff the operand "
9979 "type is vector!");
9980 assert((!VTList.VTs[0].isVector() ||
9981 VTList.VTs[0].getVectorElementCount() ==
9982 Ops[1].getValueType().getVectorElementCount()) &&
9983 "Vector element count mismatch!");
9984 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
9985 "Invalid fpext node, dst <= src!");
9986 break;
9988 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
9989 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
9990 "STRICT_FP_ROUND result type should be vector iff the operand "
9991 "type is vector!");
9992 assert((!VTList.VTs[0].isVector() ||
9993 VTList.VTs[0].getVectorElementCount() ==
9994 Ops[1].getValueType().getVectorElementCount()) &&
9995 "Vector element count mismatch!");
9996 assert(VTList.VTs[0].isFloatingPoint() &&
9997 Ops[1].getValueType().isFloatingPoint() &&
9998 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
9999 isa<ConstantSDNode>(Ops[2]) &&
10000 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
10001 "Invalid STRICT_FP_ROUND!");
10002 break;
10003#if 0
10004 // FIXME: figure out how to safely handle things like
10005 // int foo(int x) { return 1 << (x & 255); }
10006 // int bar() { return foo(256); }
10007 case ISD::SRA_PARTS:
10008 case ISD::SRL_PARTS:
10009 case ISD::SHL_PARTS:
10010 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
10011 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
10012 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10013 else if (N3.getOpcode() == ISD::AND)
10014 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
10015 // If the and is only masking out bits that cannot effect the shift,
10016 // eliminate the and.
10017 unsigned NumBits = VT.getScalarSizeInBits()*2;
10018 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
10019 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10020 }
10021 break;
10022#endif
10023 }
10024
10025 // Memoize the node unless it returns a glue result.
10026 SDNode *N;
10027 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
10029 AddNodeIDNode(ID, Opcode, VTList, Ops);
10030 void *IP = nullptr;
10031 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
10032 return SDValue(E, 0);
10033
10034 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10035 createOperands(N, Ops);
10036 CSEMap.InsertNode(N, IP);
10037 } else {
10038 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10039 createOperands(N, Ops);
10040 }
10041
10042 N->setFlags(Flags);
10043 InsertNode(N);
10044 SDValue V(N, 0);
10045 NewSDValueDbgMsg(V, "Creating new node: ", this);
10046 return V;
10047}
10048
10049SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10050 SDVTList VTList) {
10051 return getNode(Opcode, DL, VTList, std::nullopt);
10052}
10053
10054SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10055 SDValue N1) {
10056 SDValue Ops[] = { N1 };
10057 return getNode(Opcode, DL, VTList, Ops);
10058}
10059
10060SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10061 SDValue N1, SDValue N2) {
10062 SDValue Ops[] = { N1, N2 };
10063 return getNode(Opcode, DL, VTList, Ops);
10064}
10065
10066SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10067 SDValue N1, SDValue N2, SDValue N3) {
10068 SDValue Ops[] = { N1, N2, N3 };
10069 return getNode(Opcode, DL, VTList, Ops);
10070}
10071
10072SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10073 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
10074 SDValue Ops[] = { N1, N2, N3, N4 };
10075 return getNode(Opcode, DL, VTList, Ops);
10076}
10077
10078SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10079 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
10080 SDValue N5) {
10081 SDValue Ops[] = { N1, N2, N3, N4, N5 };
10082 return getNode(Opcode, DL, VTList, Ops);
10083}
10084
10086 return makeVTList(SDNode::getValueTypeList(VT), 1);
10087}
10088
10091 ID.AddInteger(2U);
10092 ID.AddInteger(VT1.getRawBits());
10093 ID.AddInteger(VT2.getRawBits());
10094
10095 void *IP = nullptr;
10096 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10097 if (!Result) {
10098 EVT *Array = Allocator.Allocate<EVT>(2);
10099 Array[0] = VT1;
10100 Array[1] = VT2;
10101 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
10102 VTListMap.InsertNode(Result, IP);
10103 }
10104 return Result->getSDVTList();
10105}
10106
10109 ID.AddInteger(3U);
10110 ID.AddInteger(VT1.getRawBits());
10111 ID.AddInteger(VT2.getRawBits());
10112 ID.AddInteger(VT3.getRawBits());
10113
10114 void *IP = nullptr;
10115 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10116 if (!Result) {
10117 EVT *Array = Allocator.Allocate<EVT>(3);
10118 Array[0] = VT1;
10119 Array[1] = VT2;
10120 Array[2] = VT3;
10121 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
10122 VTListMap.InsertNode(Result, IP);
10123 }
10124 return Result->getSDVTList();
10125}
10126
10129 ID.AddInteger(4U);
10130 ID.AddInteger(VT1.getRawBits());
10131 ID.AddInteger(VT2.getRawBits());
10132 ID.AddInteger(VT3.getRawBits());
10133 ID.AddInteger(VT4.getRawBits());
10134
10135 void *IP = nullptr;
10136 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10137 if (!Result) {
10138 EVT *Array = Allocator.Allocate<EVT>(4);
10139 Array[0] = VT1;
10140 Array[1] = VT2;
10141 Array[2] = VT3;
10142 Array[3] = VT4;
10143 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
10144 VTListMap.InsertNode(Result, IP);
10145 }
10146 return Result->getSDVTList();
10147}
10148
10150 unsigned NumVTs = VTs.size();
10152 ID.AddInteger(NumVTs);
10153 for (unsigned index = 0; index < NumVTs; index++) {
10154 ID.AddInteger(VTs[index].getRawBits());
10155 }
10156
10157 void *IP = nullptr;
10158 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10159 if (!Result) {
10160 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
10161 llvm::copy(VTs, Array);
10162 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
10163 VTListMap.InsertNode(Result, IP);
10164 }
10165 return Result->getSDVTList();
10166}
10167
10168
10169/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
10170/// specified operands. If the resultant node already exists in the DAG,
10171/// this does not modify the specified node, instead it returns the node that
10172/// already exists. If the resultant node does not exist in the DAG, the
10173/// input node is returned. As a degenerate case, if you specify the same
10174/// input operands as the node already has, the input node is returned.
10176 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
10177
10178 // Check to see if there is no change.
10179 if (Op == N->getOperand(0)) return N;
10180
10181 // See if the modified node already exists.
10182 void *InsertPos = nullptr;
10183 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
10184 return Existing;
10185
10186 // Nope it doesn't. Remove the node from its current place in the maps.
10187 if (InsertPos)
10188 if (!RemoveNodeFromCSEMaps(N))
10189 InsertPos = nullptr;
10190
10191 // Now we update the operands.
10192 N->OperandList[0].set(Op);
10193
10195 // If this gets put into a CSE map, add it.
10196 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10197 return N;
10198}
10199
10201 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
10202
10203 // Check to see if there is no change.
10204 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
10205 return N; // No operands changed, just return the input node.
10206
10207 // See if the modified node already exists.
10208 void *InsertPos = nullptr;
10209 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
10210 return Existing;
10211
10212 // Nope it doesn't. Remove the node from its current place in the maps.
10213 if (InsertPos)
10214 if (!RemoveNodeFromCSEMaps(N))
10215 InsertPos = nullptr;
10216
10217 // Now we update the operands.
10218 if (N->OperandList[0] != Op1)
10219 N->OperandList[0].set(Op1);
10220 if (N->OperandList[1] != Op2)
10221 N->OperandList[1].set(Op2);
10222
10224 // If this gets put into a CSE map, add it.
10225 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10226 return N;
10227}
10228
10231 SDValue Ops[] = { Op1, Op2, Op3 };
10232 return UpdateNodeOperands(N, Ops);
10233}
10234
10237 SDValue Op3, SDValue Op4) {
10238 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
10239 return UpdateNodeOperands(N, Ops);
10240}
10241
10244 SDValue Op3, SDValue Op4, SDValue Op5) {
10245 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
10246 return UpdateNodeOperands(N, Ops);
10247}
10248
10251 unsigned NumOps = Ops.size();
10252 assert(N->getNumOperands() == NumOps &&
10253 "Update with wrong number of operands");
10254
10255 // If no operands changed just return the input node.
10256 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
10257 return N;
10258
10259 // See if the modified node already exists.
10260 void *InsertPos = nullptr;
10261 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
10262 return Existing;
10263
10264 // Nope it doesn't. Remove the node from its current place in the maps.
10265 if (InsertPos)
10266 if (!RemoveNodeFromCSEMaps(N))
10267 InsertPos = nullptr;
10268
10269 // Now we update the operands.
10270 for (unsigned i = 0; i != NumOps; ++i)
10271 if (N->OperandList[i] != Ops[i])
10272 N->OperandList[i].set(Ops[i]);
10273
10275 // If this gets put into a CSE map, add it.
10276 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10277 return N;
10278}
10279
10280/// DropOperands - Release the operands and set this node to have
10281/// zero operands.
10283 // Unlike the code in MorphNodeTo that does this, we don't need to
10284 // watch for dead nodes here.
10285 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
10286 SDUse &Use = *I++;
10287 Use.set(SDValue());
10288 }
10289}
10290
10292 ArrayRef<MachineMemOperand *> NewMemRefs) {
10293 if (NewMemRefs.empty()) {
10294 N->clearMemRefs();
10295 return;
10296 }
10297
10298 // Check if we can avoid allocating by storing a single reference directly.
10299 if (NewMemRefs.size() == 1) {
10300 N->MemRefs = NewMemRefs[0];
10301 N->NumMemRefs = 1;
10302 return;
10303 }
10304
10305 MachineMemOperand **MemRefsBuffer =
10306 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
10307 llvm::copy(NewMemRefs, MemRefsBuffer);
10308 N->MemRefs = MemRefsBuffer;
10309 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
10310}
10311
10312/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
10313/// machine opcode.
10314///
10316 EVT VT) {
10317 SDVTList VTs = getVTList(VT);
10318 return SelectNodeTo(N, MachineOpc, VTs, std::nullopt);
10319}
10320
10322 EVT VT, SDValue Op1) {
10323 SDVTList VTs = getVTList(VT);
10324 SDValue Ops[] = { Op1 };
10325 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10326}
10327
10329 EVT VT, SDValue Op1,
10330 SDValue Op2) {
10331 SDVTList VTs = getVTList(VT);
10332 SDValue Ops[] = { Op1, Op2 };
10333 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10334}
10335
10337 EVT VT, SDValue Op1,
10338 SDValue Op2, SDValue Op3) {
10339 SDVTList VTs = getVTList(VT);
10340 SDValue Ops[] = { Op1, Op2, Op3 };
10341 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10342}
10343
10345 EVT VT, ArrayRef<SDValue> Ops) {
10346 SDVTList VTs = getVTList(VT);
10347 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10348}
10349
10351 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
10352 SDVTList VTs = getVTList(VT1, VT2);
10353 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10354}
10355
10357 EVT VT1, EVT VT2) {
10358 SDVTList VTs = getVTList(VT1, VT2);
10359 return SelectNodeTo(N, MachineOpc, VTs, std::nullopt);
10360}
10361
10363 EVT VT1, EVT VT2, EVT VT3,
10364 ArrayRef<SDValue> Ops) {
10365 SDVTList VTs = getVTList(VT1, VT2, VT3);
10366 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10367}
10368
10370 EVT VT1, EVT VT2,
10371 SDValue Op1, SDValue Op2) {
10372 SDVTList VTs = getVTList(VT1, VT2);
10373 SDValue Ops[] = { Op1, Op2 };
10374 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10375}
10376
10378 SDVTList VTs,ArrayRef<SDValue> Ops) {
10379 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
10380 // Reset the NodeID to -1.
10381 New->setNodeId(-1);
10382 if (New != N) {
10383 ReplaceAllUsesWith(N, New);
10385 }
10386 return New;
10387}
10388
10389/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
10390/// the line number information on the merged node since it is not possible to
10391/// preserve the information that operation is associated with multiple lines.
10392/// This will make the debugger working better at -O0, were there is a higher
10393/// probability having other instructions associated with that line.
10394///
10395/// For IROrder, we keep the smaller of the two
10396SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
10397 DebugLoc NLoc = N->getDebugLoc();
10398 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
10399 N->setDebugLoc(DebugLoc());
10400 }
10401 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
10402 N->setIROrder(Order);
10403 return N;
10404}
10405
10406/// MorphNodeTo - This *mutates* the specified node to have the specified
10407/// return type, opcode, and operands.
10408///
10409/// Note that MorphNodeTo returns the resultant node. If there is already a
10410/// node of the specified opcode and operands, it returns that node instead of
10411/// the current one. Note that the SDLoc need not be the same.
10412///
10413/// Using MorphNodeTo is faster than creating a new node and swapping it in
10414/// with ReplaceAllUsesWith both because it often avoids allocating a new
10415/// node, and because it doesn't require CSE recalculation for any of
10416/// the node's users.
10417///
10418/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
10419/// As a consequence it isn't appropriate to use from within the DAG combiner or
10420/// the legalizer which maintain worklists that would need to be updated when
10421/// deleting things.
10423 SDVTList VTs, ArrayRef<SDValue> Ops) {
10424 // If an identical node already exists, use it.
10425 void *IP = nullptr;
10426 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
10428 AddNodeIDNode(ID, Opc, VTs, Ops);
10429 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
10430 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
10431 }
10432
10433 if (!RemoveNodeFromCSEMaps(N))
10434 IP = nullptr;
10435
10436 // Start the morphing.
10437 N->NodeType = Opc;
10438 N->ValueList = VTs.VTs;
10439 N->NumValues = VTs.NumVTs;
10440
10441 // Clear the operands list, updating used nodes to remove this from their
10442 // use list. Keep track of any operands that become dead as a result.
10443 SmallPtrSet<SDNode*, 16> DeadNodeSet;
10444 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
10445 SDUse &Use = *I++;
10446 SDNode *Used = Use.getNode();
10447 Use.set(SDValue());
10448 if (Used->use_empty())
10449 DeadNodeSet.insert(Used);
10450 }
10451
10452 // For MachineNode, initialize the memory references information.
10453 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N))
10454 MN->clearMemRefs();
10455
10456 // Swap for an appropriately sized array from the recycler.
10457 removeOperands(N);
10458 createOperands(N, Ops);
10459
10460 // Delete any nodes that are still dead after adding the uses for the
10461 // new operands.
10462 if (!DeadNodeSet.empty()) {
10463 SmallVector<SDNode *, 16> DeadNodes;
10464 for (SDNode *N : DeadNodeSet)
10465 if (N->use_empty())
10466 DeadNodes.push_back(N);
10467 RemoveDeadNodes(DeadNodes);
10468 }
10469
10470 if (IP)
10471 CSEMap.InsertNode(N, IP); // Memoize the new node.
10472 return N;
10473}
10474
10476 unsigned OrigOpc = Node->getOpcode();
10477 unsigned NewOpc;
10478 switch (OrigOpc) {
10479 default:
10480 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
10481#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
10482 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
10483#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
10484 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
10485#include "llvm/IR/ConstrainedOps.def"
10486 }
10487
10488 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
10489
10490 // We're taking this node out of the chain, so we need to re-link things.
10491 SDValue InputChain = Node->getOperand(0);
10492 SDValue OutputChain = SDValue(Node, 1);
10493 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
10494
10496 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
10497 Ops.push_back(Node->getOperand(i));
10498
10499 SDVTList VTs = getVTList(Node->getValueType(0));
10500 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
10501
10502 // MorphNodeTo can operate in two ways: if an existing node with the
10503 // specified operands exists, it can just return it. Otherwise, it
10504 // updates the node in place to have the requested operands.
10505 if (Res == Node) {
10506 // If we updated the node in place, reset the node ID. To the isel,
10507 // this should be just like a newly allocated machine node.
10508 Res->setNodeId(-1);
10509 } else {
10510 ReplaceAllUsesWith(Node, Res);
10511 RemoveDeadNode(Node);
10512 }
10513
10514 return Res;
10515}
10516
10517/// getMachineNode - These are used for target selectors to create a new node
10518/// with specified return type(s), MachineInstr opcode, and operands.
10519///
10520/// Note that getMachineNode returns the resultant node. If there is already a
10521/// node of the specified opcode and operands, it returns that node instead of
10522/// the current one.
10524 EVT VT) {
10525 SDVTList VTs = getVTList(VT);
10526 return getMachineNode(Opcode, dl, VTs, std::nullopt);
10527}
10528
10530 EVT VT, SDValue Op1) {
10531 SDVTList VTs = getVTList(VT);
10532 SDValue Ops[] = { Op1 };
10533 return getMachineNode(Opcode, dl, VTs, Ops);
10534}
10535
10537 EVT VT, SDValue Op1, SDValue Op2) {
10538 SDVTList VTs = getVTList(VT);
10539 SDValue Ops[] = { Op1, Op2 };
10540 return getMachineNode(Opcode, dl, VTs, Ops);
10541}
10542
10544 EVT VT, SDValue Op1, SDValue Op2,
10545 SDValue Op3) {
10546 SDVTList VTs = getVTList(VT);
10547 SDValue Ops[] = { Op1, Op2, Op3 };
10548 return getMachineNode(Opcode, dl, VTs, Ops);
10549}
10550
10552 EVT VT, ArrayRef<SDValue> Ops) {
10553 SDVTList VTs = getVTList(VT);
10554 return getMachineNode(Opcode, dl, VTs, Ops);
10555}
10556
10558 EVT VT1, EVT VT2, SDValue Op1,
10559 SDValue Op2) {
10560 SDVTList VTs = getVTList(VT1, VT2);
10561 SDValue Ops[] = { Op1, Op2 };
10562 return getMachineNode(Opcode, dl, VTs, Ops);
10563}
10564
10566 EVT VT1, EVT VT2, SDValue Op1,
10567 SDValue Op2, SDValue Op3) {
10568 SDVTList VTs = getVTList(VT1, VT2);
10569 SDValue Ops[] = { Op1, Op2, Op3 };
10570 return getMachineNode(Opcode, dl, VTs, Ops);
10571}
10572
10574 EVT VT1, EVT VT2,
10575 ArrayRef<SDValue> Ops) {
10576 SDVTList VTs = getVTList(VT1, VT2);
10577 return getMachineNode(Opcode, dl, VTs, Ops);
10578}
10579
10581 EVT VT1, EVT VT2, EVT VT3,
10582 SDValue Op1, SDValue Op2) {
10583 SDVTList VTs = getVTList(VT1, VT2, VT3);
10584 SDValue Ops[] = { Op1, Op2 };
10585 return getMachineNode(Opcode, dl, VTs, Ops);
10586}
10587
10589 EVT VT1, EVT VT2, EVT VT3,
10590 SDValue Op1, SDValue Op2,
10591 SDValue Op3) {
10592 SDVTList VTs = getVTList(VT1, VT2, VT3);
10593 SDValue Ops[] = { Op1, Op2, Op3 };
10594 return getMachineNode(Opcode, dl, VTs, Ops);
10595}
10596
10598 EVT VT1, EVT VT2, EVT VT3,
10599 ArrayRef<SDValue> Ops) {
10600 SDVTList VTs = getVTList(VT1, VT2, VT3);
10601 return getMachineNode(Opcode, dl, VTs, Ops);
10602}
10603
10605 ArrayRef<EVT> ResultTys,
10606 ArrayRef<SDValue> Ops) {
10607 SDVTList VTs = getVTList(ResultTys);
10608 return getMachineNode(Opcode, dl, VTs, Ops);
10609}
10610
10612 SDVTList VTs,
10613 ArrayRef<SDValue> Ops) {
10614 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
10616 void *IP = nullptr;
10617
10618 if (DoCSE) {
10620 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
10621 IP = nullptr;
10622 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10623 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
10624 }
10625 }
10626
10627 // Allocate a new MachineSDNode.
10628 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10629 createOperands(N, Ops);
10630
10631 if (DoCSE)
10632 CSEMap.InsertNode(N, IP);
10633
10634 InsertNode(N);
10635 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
10636 return N;
10637}
10638
10639/// getTargetExtractSubreg - A convenience function for creating
10640/// TargetOpcode::EXTRACT_SUBREG nodes.
10642 SDValue Operand) {
10643 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
10644 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
10645 VT, Operand, SRIdxVal);
10646 return SDValue(Subreg, 0);
10647}
10648
10649/// getTargetInsertSubreg - A convenience function for creating
10650/// TargetOpcode::INSERT_SUBREG nodes.
10652 SDValue Operand, SDValue Subreg) {
10653 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
10654 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
10655 VT, Operand, Subreg, SRIdxVal);
10656 return SDValue(Result, 0);
10657}
10658
10659/// getNodeIfExists - Get the specified node if it's already available, or
10660/// else return NULL.
10662 ArrayRef<SDValue> Ops) {
10663 SDNodeFlags Flags;
10664 if (Inserter)
10665 Flags = Inserter->getFlags();
10666 return getNodeIfExists(Opcode, VTList, Ops, Flags);
10667}
10668
10671 const SDNodeFlags Flags) {
10672 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
10674 AddNodeIDNode(ID, Opcode, VTList, Ops);
10675 void *IP = nullptr;
10676 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP)) {
10677 E->intersectFlagsWith(Flags);
10678 return E;
10679 }
10680 }
10681 return nullptr;
10682}
10683
10684/// doesNodeExist - Check if a node exists without modifying its flags.
10685bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
10686 ArrayRef<SDValue> Ops) {
10687 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
10689 AddNodeIDNode(ID, Opcode, VTList, Ops);
10690 void *IP = nullptr;
10691 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
10692 return true;
10693 }
10694 return false;
10695}
10696
10697/// getDbgValue - Creates a SDDbgValue node.
10698///
10699/// SDNode
10701 SDNode *N, unsigned R, bool IsIndirect,
10702 const DebugLoc &DL, unsigned O) {
10703 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10704 "Expected inlined-at fields to agree");
10705 return new (DbgInfo->getAlloc())
10706 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
10707 {}, IsIndirect, DL, O,
10708 /*IsVariadic=*/false);
10709}
10710
10711/// Constant
10713 DIExpression *Expr,
10714 const Value *C,
10715 const DebugLoc &DL, unsigned O) {
10716 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10717 "Expected inlined-at fields to agree");
10718 return new (DbgInfo->getAlloc())
10719 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
10720 /*IsIndirect=*/false, DL, O,
10721 /*IsVariadic=*/false);
10722}
10723
10724/// FrameIndex
10726 DIExpression *Expr, unsigned FI,
10727 bool IsIndirect,
10728 const DebugLoc &DL,
10729 unsigned O) {
10730 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10731 "Expected inlined-at fields to agree");
10732 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
10733}
10734
10735/// FrameIndex with dependencies
10737 DIExpression *Expr, unsigned FI,
10738 ArrayRef<SDNode *> Dependencies,
10739 bool IsIndirect,
10740 const DebugLoc &DL,
10741 unsigned O) {
10742 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10743 "Expected inlined-at fields to agree");
10744 return new (DbgInfo->getAlloc())
10745 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
10746 Dependencies, IsIndirect, DL, O,
10747 /*IsVariadic=*/false);
10748}
10749
10750/// VReg
10752 unsigned VReg, bool IsIndirect,
10753 const DebugLoc &DL, unsigned O) {
10754 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10755 "Expected inlined-at fields to agree");
10756 return new (DbgInfo->getAlloc())
10757 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
10758 {}, IsIndirect, DL, O,
10759 /*IsVariadic=*/false);
10760}
10761
10764 ArrayRef<SDNode *> Dependencies,
10765 bool IsIndirect, const DebugLoc &DL,
10766 unsigned O, bool IsVariadic) {
10767 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10768 "Expected inlined-at fields to agree");
10769 return new (DbgInfo->getAlloc())
10770 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
10771 DL, O, IsVariadic);
10772}
10773
10775 unsigned OffsetInBits, unsigned SizeInBits,
10776 bool InvalidateDbg) {
10777 SDNode *FromNode = From.getNode();
10778 SDNode *ToNode = To.getNode();
10779 assert(FromNode && ToNode && "Can't modify dbg values");
10780
10781 // PR35338
10782 // TODO: assert(From != To && "Redundant dbg value transfer");
10783 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
10784 if (From == To || FromNode == ToNode)
10785 return;
10786
10787 if (!FromNode->getHasDebugValue())
10788 return;
10789
10790 SDDbgOperand FromLocOp =
10791 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
10793
10795 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
10796 if (Dbg->isInvalidated())
10797 continue;
10798
10799 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
10800
10801 // Create a new location ops vector that is equal to the old vector, but
10802 // with each instance of FromLocOp replaced with ToLocOp.
10803 bool Changed = false;
10804 auto NewLocOps = Dbg->copyLocationOps();
10805 std::replace_if(
10806 NewLocOps.begin(), NewLocOps.end(),
10807 [&Changed, FromLocOp](const SDDbgOperand &Op) {
10808 bool Match = Op == FromLocOp;
10809 Changed |= Match;
10810 return Match;
10811 },
10812 ToLocOp);
10813 // Ignore this SDDbgValue if we didn't find a matching location.
10814 if (!Changed)
10815 continue;
10816
10817 DIVariable *Var = Dbg->getVariable();
10818 auto *Expr = Dbg->getExpression();
10819 // If a fragment is requested, update the expression.
10820 if (SizeInBits) {
10821 // When splitting a larger (e.g., sign-extended) value whose
10822 // lower bits are described with an SDDbgValue, do not attempt
10823 // to transfer the SDDbgValue to the upper bits.
10824 if (auto FI = Expr->getFragmentInfo())
10825 if (OffsetInBits + SizeInBits > FI->SizeInBits)
10826 continue;
10827 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
10828 SizeInBits);
10829 if (!Fragment)
10830 continue;
10831 Expr = *Fragment;
10832 }
10833
10834 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
10835 // Clone the SDDbgValue and move it to To.
10836 SDDbgValue *Clone = getDbgValueList(
10837 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
10838 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
10839 Dbg->isVariadic());
10840 ClonedDVs.push_back(Clone);
10841
10842 if (InvalidateDbg) {
10843 // Invalidate value and indicate the SDDbgValue should not be emitted.
10844 Dbg->setIsInvalidated();
10845 Dbg->setIsEmitted();
10846 }
10847 }
10848
10849 for (SDDbgValue *Dbg : ClonedDVs) {
10850 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
10851 "Transferred DbgValues should depend on the new SDNode");
10852 AddDbgValue(Dbg, false);
10853 }
10854}
10855
10857 if (!N.getHasDebugValue())
10858 return;
10859
10861 for (auto *DV : GetDbgValues(&N)) {
10862 if (DV->isInvalidated())
10863 continue;
10864 switch (N.getOpcode()) {
10865 default:
10866 break;
10867 case ISD::ADD: {
10868 SDValue N0 = N.getOperand(0);
10869 SDValue N1 = N.getOperand(1);
10870 if (!isa<ConstantSDNode>(N0)) {
10871 bool RHSConstant = isa<ConstantSDNode>(N1);
10873 if (RHSConstant)
10874 Offset = N.getConstantOperandVal(1);
10875 // We are not allowed to turn indirect debug values variadic, so
10876 // don't salvage those.
10877 if (!RHSConstant && DV->isIndirect())
10878 continue;
10879
10880 // Rewrite an ADD constant node into a DIExpression. Since we are
10881 // performing arithmetic to compute the variable's *value* in the
10882 // DIExpression, we need to mark the expression with a
10883 // DW_OP_stack_value.
10884 auto *DIExpr = DV->getExpression();
10885 auto NewLocOps = DV->copyLocationOps();
10886 bool Changed = false;
10887 size_t OrigLocOpsSize = NewLocOps.size();
10888 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
10889 // We're not given a ResNo to compare against because the whole
10890 // node is going away. We know that any ISD::ADD only has one
10891 // result, so we can assume any node match is using the result.
10892 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
10893 NewLocOps[i].getSDNode() != &N)
10894 continue;
10895 NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
10896 if (RHSConstant) {
10899 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
10900 } else {
10901 // Convert to a variadic expression (if not already).
10902 // convertToVariadicExpression() returns a const pointer, so we use
10903 // a temporary const variable here.
10904 const auto *TmpDIExpr =
10908 ExprOps.push_back(NewLocOps.size());
10909 ExprOps.push_back(dwarf::DW_OP_plus);
10912 NewLocOps.push_back(RHS);
10913 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
10914 }
10915 Changed = true;
10916 }
10917 (void)Changed;
10918 assert(Changed && "Salvage target doesn't use N");
10919
10920 bool IsVariadic =
10921 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
10922
10923 auto AdditionalDependencies = DV->getAdditionalDependencies();
10924 SDDbgValue *Clone = getDbgValueList(
10925 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
10926 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
10927 ClonedDVs.push_back(Clone);
10928 DV->setIsInvalidated();
10929 DV->setIsEmitted();
10930 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
10931 N0.getNode()->dumprFull(this);
10932 dbgs() << " into " << *DIExpr << '\n');
10933 }
10934 break;
10935 }
10936 case ISD::TRUNCATE: {
10937 SDValue N0 = N.getOperand(0);
10938 TypeSize FromSize = N0.getValueSizeInBits();
10939 TypeSize ToSize = N.getValueSizeInBits(0);
10940
10941 DIExpression *DbgExpression = DV->getExpression();
10942 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
10943 auto NewLocOps = DV->copyLocationOps();
10944 bool Changed = false;
10945 for (size_t i = 0; i < NewLocOps.size(); ++i) {
10946 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
10947 NewLocOps[i].getSDNode() != &N)
10948 continue;
10949
10950 NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
10951 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
10952 Changed = true;
10953 }
10954 assert(Changed && "Salvage target doesn't use N");
10955 (void)Changed;
10956
10957 SDDbgValue *Clone =
10958 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
10959 DV->getAdditionalDependencies(), DV->isIndirect(),
10960 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
10961
10962 ClonedDVs.push_back(Clone);
10963 DV->setIsInvalidated();
10964 DV->setIsEmitted();
10965 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
10966 dbgs() << " into " << *DbgExpression << '\n');
10967 break;
10968 }
10969 }
10970 }
10971
10972 for (SDDbgValue *Dbg : ClonedDVs) {
10973 assert(!Dbg->getSDNodes().empty() &&
10974 "Salvaged DbgValue should depend on a new SDNode");
10975 AddDbgValue(Dbg, false);
10976 }
10977}
10978
10979/// Creates a SDDbgLabel node.
10981 const DebugLoc &DL, unsigned O) {
10982 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
10983 "Expected inlined-at fields to agree");
10984 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
10985}
10986
10987namespace {
10988
10989/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
10990/// pointed to by a use iterator is deleted, increment the use iterator
10991/// so that it doesn't dangle.
10992///
10993class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
10996
10997 void NodeDeleted(SDNode *N, SDNode *E) override {
10998 // Increment the iterator as needed.
10999 while (UI != UE && N == *UI)
11000 ++UI;
11001 }
11002
11003public:
11004 RAUWUpdateListener(SelectionDAG &d,
11007 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
11008};
11009
11010} // end anonymous namespace
11011
11012/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11013/// This can cause recursive merging of nodes in the DAG.
11014///
11015/// This version assumes From has a single result value.
11016///
11018 SDNode *From = FromN.getNode();
11019 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
11020 "Cannot replace with this method!");
11021 assert(From != To.getNode() && "Cannot replace uses of with self");
11022
11023 // Preserve Debug Values
11024 transferDbgValues(FromN, To);
11025 // Preserve extra info.
11026 copyExtraInfo(From, To.getNode());
11027
11028 // Iterate over all the existing uses of From. New uses will be added
11029 // to the beginning of the use list, which we avoid visiting.
11030 // This specifically avoids visiting uses of From that arise while the
11031 // replacement is happening, because any such uses would be the result
11032 // of CSE: If an existing node looks like From after one of its operands
11033 // is replaced by To, we don't want to replace of all its users with To
11034 // too. See PR3018 for more info.
11035 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11036 RAUWUpdateListener Listener(*this, UI, UE);
11037 while (UI != UE) {
11038 SDNode *User = *UI;
11039
11040 // This node is about to morph, remove its old self from the CSE maps.
11041 RemoveNodeFromCSEMaps(User);
11042
11043 // A user can appear in a use list multiple times, and when this
11044 // happens the uses are usually next to each other in the list.
11045 // To help reduce the number of CSE recomputations, process all
11046 // the uses of this user that we can find this way.
11047 do {
11048 SDUse &Use = UI.getUse();
11049 ++UI;
11050 Use.set(To);
11051 if (To->isDivergent() != From->isDivergent())
11053 } while (UI != UE && *UI == User);
11054 // Now that we have modified User, add it back to the CSE maps. If it
11055 // already exists there, recursively merge the results together.
11056 AddModifiedNodeToCSEMaps(User);
11057 }
11058
11059 // If we just RAUW'd the root, take note.
11060 if (FromN == getRoot())
11061 setRoot(To);
11062}
11063
11064/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11065/// This can cause recursive merging of nodes in the DAG.
11066///
11067/// This version assumes that for each value of From, there is a
11068/// corresponding value in To in the same position with the same type.
11069///
11071#ifndef NDEBUG
11072 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11073 assert((!From->hasAnyUseOfValue(i) ||
11074 From->getValueType(i) == To->getValueType(i)) &&
11075 "Cannot use this version of ReplaceAllUsesWith!");
11076#endif
11077
11078 // Handle the trivial case.
11079 if (From == To)
11080 return;
11081
11082 // Preserve Debug Info. Only do this if there's a use.
11083 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11084 if (From->hasAnyUseOfValue(i)) {
11085 assert((i < To->getNumValues()) && "Invalid To location");
11087 }
11088 // Preserve extra info.
11089 copyExtraInfo(From, To);
11090
11091 // Iterate over just the existing users of From. See the comments in
11092 // the ReplaceAllUsesWith above.
11093 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11094 RAUWUpdateListener Listener(*this, UI, UE);
11095 while (UI != UE) {
11096 SDNode *User = *UI;
11097
11098 // This node is about to morph, remove its old self from the CSE maps.
11099 RemoveNodeFromCSEMaps(User);
11100
11101 // A user can appear in a use list multiple times, and when this
11102 // happens the uses are usually next to each other in the list.
11103 // To help reduce the number of CSE recomputations, process all
11104 // the uses of this user that we can find this way.
11105 do {
11106 SDUse &Use = UI.getUse();
11107 ++UI;
11108 Use.setNode(To);
11109 if (To->isDivergent() != From->isDivergent())
11111 } while (UI != UE && *UI == User);
11112
11113 // Now that we have modified User, add it back to the CSE maps. If it
11114 // already exists there, recursively merge the results together.
11115 AddModifiedNodeToCSEMaps(User);
11116 }
11117
11118 // If we just RAUW'd the root, take note.
11119 if (From == getRoot().getNode())
11120 setRoot(SDValue(To, getRoot().getResNo()));
11121}
11122
11123/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11124/// This can cause recursive merging of nodes in the DAG.
11125///
11126/// This version can replace From with any result values. To must match the
11127/// number and types of values returned by From.
11129 if (From->getNumValues() == 1) // Handle the simple case efficiently.
11130 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
11131
11132 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
11133 // Preserve Debug Info.
11134 transferDbgValues(SDValue(From, i), To[i]);
11135 // Preserve extra info.
11136 copyExtraInfo(From, To[i].getNode());
11137 }
11138
11139 // Iterate over just the existing users of From. See the comments in
11140 // the ReplaceAllUsesWith above.
11141 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11142 RAUWUpdateListener Listener(*this, UI, UE);
11143 while (UI != UE) {
11144 SDNode *User = *UI;
11145
11146 // This node is about to morph, remove its old self from the CSE maps.
11147 RemoveNodeFromCSEMaps(User);
11148
11149 // A user can appear in a use list multiple times, and when this happens the
11150 // uses are usually next to each other in the list. To help reduce the
11151 // number of CSE and divergence recomputations, process all the uses of this
11152 // user that we can find this way.
11153 bool To_IsDivergent = false;
11154 do {
11155 SDUse &Use = UI.getUse();
11156 const SDValue &ToOp = To[Use.getResNo()];
11157 ++UI;
11158 Use.set(ToOp);
11159 To_IsDivergent |= ToOp->isDivergent();
11160 } while (UI != UE && *UI == User);
11161
11162 if (To_IsDivergent != From->isDivergent())
11164
11165 // Now that we have modified User, add it back to the CSE maps. If it
11166 // already exists there, recursively merge the results together.
11167 AddModifiedNodeToCSEMaps(User);
11168 }
11169
11170 // If we just RAUW'd the root, take note.
11171 if (From == getRoot().getNode())
11172 setRoot(SDValue(To[getRoot().getResNo()]));
11173}
11174
11175/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
11176/// uses of other values produced by From.getNode() alone. The Deleted
11177/// vector is handled the same way as for ReplaceAllUsesWith.
11179 // Handle the really simple, really trivial case efficiently.
11180 if (From == To) return;
11181
11182 // Handle the simple, trivial, case efficiently.
11183 if (From.getNode()->getNumValues() == 1) {
11185 return;
11186 }
11187
11188 // Preserve Debug Info.
11190 copyExtraInfo(From.getNode(), To.getNode());
11191
11192 // Iterate over just the existing users of From. See the comments in
11193 // the ReplaceAllUsesWith above.
11194 SDNode::use_iterator UI = From.getNode()->use_begin(),
11195 UE = From.getNode()->use_end();
11196 RAUWUpdateListener Listener(*this, UI, UE);
11197 while (UI != UE) {
11198 SDNode *User = *UI;
11199 bool UserRemovedFromCSEMaps = false;
11200
11201 // A user can appear in a use list multiple times, and when this
11202 // happens the uses are usually next to each other in the list.
11203 // To help reduce the number of CSE recomputations, process all
11204 // the uses of this user that we can find this way.
11205 do {
11206 SDUse &Use = UI.getUse();
11207
11208 // Skip uses of different values from the same node.
11209 if (Use.getResNo() != From.getResNo()) {
11210 ++UI;
11211 continue;
11212 }
11213
11214 // If this node hasn't been modified yet, it's still in the CSE maps,
11215 // so remove its old self from the CSE maps.
11216 if (!UserRemovedFromCSEMaps) {
11217 RemoveNodeFromCSEMaps(User);
11218 UserRemovedFromCSEMaps = true;
11219 }
11220
11221 ++UI;
11222 Use.set(To);
11223 if (To->isDivergent() != From->isDivergent())
11225 } while (UI != UE && *UI == User);
11226 // We are iterating over all uses of the From node, so if a use
11227 // doesn't use the specific value, no changes are made.
11228 if (!UserRemovedFromCSEMaps)
11229 continue;
11230
11231 // Now that we have modified User, add it back to the CSE maps. If it
11232 // already exists there, recursively merge the results together.
11233 AddModifiedNodeToCSEMaps(User);
11234 }
11235
11236 // If we just RAUW'd the root, take note.
11237 if (From == getRoot())
11238 setRoot(To);
11239}
11240
11241namespace {
11242
11243/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
11244/// to record information about a use.
11245struct UseMemo {
11246 SDNode *User;
11247 unsigned Index;
11248 SDUse *Use;
11249};
11250
11251/// operator< - Sort Memos by User.
11252bool operator<(const UseMemo &L, const UseMemo &R) {
11253 return (intptr_t)L.User < (intptr_t)R.User;
11254}
11255
11256/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
11257/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
11258/// the node already has been taken care of recursively.
11259class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
11261
11262 void NodeDeleted(SDNode *N, SDNode *E) override {
11263 for (UseMemo &Memo : Uses)
11264 if (Memo.User == N)
11265 Memo.User = nullptr;
11266 }
11267
11268public:
11269 RAUOVWUpdateListener(SelectionDAG &d, SmallVector<UseMemo, 4> &uses)
11270 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
11271};
11272
11273} // end anonymous namespace
11274
11276 if (TLI->isSDNodeAlwaysUniform(N)) {
11277 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
11278 "Conflicting divergence information!");
11279 return false;
11280 }
11281 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
11282 return true;
11283 for (const auto &Op : N->ops()) {
11284 if (Op.Val.getValueType() != MVT::Other && Op.getNode()->isDivergent())
11285 return true;
11286 }
11287 return false;
11288}
11289
11291 SmallVector<SDNode *, 16> Worklist(1, N);
11292 do {
11293 N = Worklist.pop_back_val();
11294 bool IsDivergent = calculateDivergence(N);
11295 if (N->SDNodeBits.IsDivergent != IsDivergent) {
11296 N->SDNodeBits.IsDivergent = IsDivergent;
11297 llvm::append_range(Worklist, N->uses());
11298 }
11299 } while (!Worklist.empty());
11300}
11301
11302void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
11304 Order.reserve(AllNodes.size());
11305 for (auto &N : allnodes()) {
11306 unsigned NOps = N.getNumOperands();
11307 Degree[&N] = NOps;
11308 if (0 == NOps)
11309 Order.push_back(&N);
11310 }
11311 for (size_t I = 0; I != Order.size(); ++I) {
11312 SDNode *N = Order[I];
11313 for (auto *U : N->uses()) {
11314 unsigned &UnsortedOps = Degree[U];
11315 if (0 == --UnsortedOps)
11316 Order.push_back(U);
11317 }
11318 }
11319}
11320
11321#ifndef NDEBUG
11323 std::vector<SDNode *> TopoOrder;
11324 CreateTopologicalOrder(TopoOrder);
11325 for (auto *N : TopoOrder) {
11326 assert(calculateDivergence(N) == N->isDivergent() &&
11327 "Divergence bit inconsistency detected");
11328 }
11329}
11330#endif
11331
11332/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
11333/// uses of other values produced by From.getNode() alone. The same value
11334/// may appear in both the From and To list. The Deleted vector is
11335/// handled the same way as for ReplaceAllUsesWith.
11337 const SDValue *To,
11338 unsigned Num){
11339 // Handle the simple, trivial case efficiently.
11340 if (Num == 1)
11341 return ReplaceAllUsesOfValueWith(*From, *To);
11342
11343 transferDbgValues(*From, *To);
11344 copyExtraInfo(From->getNode(), To->getNode());
11345
11346 // Read up all the uses and make records of them. This helps
11347 // processing new uses that are introduced during the
11348 // replacement process.
11350 for (unsigned i = 0; i != Num; ++i) {
11351 unsigned FromResNo = From[i].getResNo();
11352 SDNode *FromNode = From[i].getNode();
11353 for (SDNode::use_iterator UI = FromNode->use_begin(),
11354 E = FromNode->use_end(); UI != E; ++UI) {
11355 SDUse &Use = UI.getUse();
11356 if (Use.getResNo() == FromResNo) {
11357 UseMemo Memo = { *UI, i, &Use };
11358 Uses.push_back(Memo);
11359 }
11360 }
11361 }
11362
11363 // Sort the uses, so that all the uses from a given User are together.
11365 RAUOVWUpdateListener Listener(*this, Uses);
11366
11367 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
11368 UseIndex != UseIndexEnd; ) {
11369 // We know that this user uses some value of From. If it is the right
11370 // value, update it.
11371 SDNode *User = Uses[UseIndex].User;
11372 // If the node has been deleted by recursive CSE updates when updating
11373 // another node, then just skip this entry.
11374 if (User == nullptr) {
11375 ++UseIndex;
11376 continue;
11377 }
11378
11379 // This node is about to morph, remove its old self from the CSE maps.
11380 RemoveNodeFromCSEMaps(User);
11381
11382 // The Uses array is sorted, so all the uses for a given User
11383 // are next to each other in the list.
11384 // To help reduce the number of CSE recomputations, process all
11385 // the uses of this user that we can find this way.
11386 do {
11387 unsigned i = Uses[UseIndex].Index;
11388 SDUse &Use = *Uses[UseIndex].Use;
11389 ++UseIndex;
11390
11391 Use.set(To[i]);
11392 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
11393
11394 // Now that we have modified User, add it back to the CSE maps. If it
11395 // already exists there, recursively merge the results together.
11396 AddModifiedNodeToCSEMaps(User);
11397 }
11398}
11399
11400/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
11401/// based on their topological order. It returns the maximum id and a vector
11402/// of the SDNodes* in assigned order by reference.
11404 unsigned DAGSize = 0;
11405
11406 // SortedPos tracks the progress of the algorithm. Nodes before it are
11407 // sorted, nodes after it are unsorted. When the algorithm completes
11408 // it is at the end of the list.
11409 allnodes_iterator SortedPos = allnodes_begin();
11410
11411 // Visit all the nodes. Move nodes with no operands to the front of
11412 // the list immediately. Annotate nodes that do have operands with their
11413 // operand count. Before we do this, the Node Id fields of the nodes
11414 // may contain arbitrary values. After, the Node Id fields for nodes
11415 // before SortedPos will contain the topological sort index, and the
11416 // Node Id fields for nodes At SortedPos and after will contain the
11417 // count of outstanding operands.
11419 checkForCycles(&N, this);
11420 unsigned Degree = N.getNumOperands();
11421 if (Degree == 0) {
11422 // A node with no uses, add it to the result array immediately.
11423 N.setNodeId(DAGSize++);
11424 allnodes_iterator Q(&N);
11425 if (Q != SortedPos)
11426 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
11427 assert(SortedPos != AllNodes.end() && "Overran node list");
11428 ++SortedPos;
11429 } else {
11430 // Temporarily use the Node Id as scratch space for the degree count.
11431 N.setNodeId(Degree);
11432 }
11433 }
11434
11435 // Visit all the nodes. As we iterate, move nodes into sorted order,
11436 // such that by the time the end is reached all nodes will be sorted.
11437 for (SDNode &Node : allnodes()) {
11438 SDNode *N = &Node;
11439 checkForCycles(N, this);
11440 // N is in sorted position, so all its uses have one less operand
11441 // that needs to be sorted.
11442 for (SDNode *P : N->uses()) {
11443 unsigned Degree = P->getNodeId();
11444 assert(Degree != 0 && "Invalid node degree");
11445 --Degree;
11446 if (Degree == 0) {
11447 // All of P's operands are sorted, so P may sorted now.
11448 P->setNodeId(DAGSize++);
11449 if (P->getIterator() != SortedPos)
11450 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
11451 assert(SortedPos != AllNodes.end() && "Overran node list");
11452 ++SortedPos;
11453 } else {
11454 // Update P's outstanding operand count.
11455 P->setNodeId(Degree);
11456 }
11457 }
11458 if (Node.getIterator() == SortedPos) {
11459#ifndef NDEBUG
11461 SDNode *S = &*++I;
11462 dbgs() << "Overran sorted position:\n";
11463 S->dumprFull(this); dbgs() << "\n";
11464 dbgs() << "Checking if this is due to cycles\n";
11465 checkForCycles(this, true);
11466#endif
11467 llvm_unreachable(nullptr);
11468 }
11469 }
11470
11471 assert(SortedPos == AllNodes.end() &&
11472 "Topological sort incomplete!");
11473 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
11474 "First node in topological sort is not the entry token!");
11475 assert(AllNodes.front().getNodeId() == 0 &&
11476 "First node in topological sort has non-zero id!");
11477 assert(AllNodes.front().getNumOperands() == 0 &&
11478 "First node in topological sort has operands!");
11479 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
11480 "Last node in topologic sort has unexpected id!");
11481 assert(AllNodes.back().use_empty() &&
11482 "Last node in topologic sort has users!");
11483 assert(DAGSize == allnodes_size() && "Node count mismatch!");
11484 return DAGSize;
11485}
11486
11487/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
11488/// value is produced by SD.
11489void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
11490 for (SDNode *SD : DB->getSDNodes()) {
11491 if (!SD)
11492 continue;
11493 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
11494 SD->setHasDebugValue(true);
11495 }
11496 DbgInfo->add(DB, isParameter);
11497}
11498
11499void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
11500
11502 SDValue NewMemOpChain) {
11503 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
11504 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
11505 // The new memory operation must have the same position as the old load in
11506 // terms of memory dependency. Create a TokenFactor for the old load and new
11507 // memory operation and update uses of the old load's output chain to use that
11508 // TokenFactor.
11509 if (OldChain == NewMemOpChain || OldChain.use_empty())
11510 return NewMemOpChain;
11511
11512 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
11513 OldChain, NewMemOpChain);
11514 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
11515 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
11516 return TokenFactor;
11517}
11518
11520 SDValue NewMemOp) {
11521 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
11522 SDValue OldChain = SDValue(OldLoad, 1);
11523 SDValue NewMemOpChain = NewMemOp.getValue(1);
11524 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
11525}
11526
11528 Function **OutFunction) {
11529 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
11530
11531 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
11532 auto *Module = MF->getFunction().getParent();
11533 auto *Function = Module->getFunction(Symbol);
11534
11535 if (OutFunction != nullptr)
11536 *OutFunction = Function;
11537
11538 if (Function != nullptr) {
11539 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
11540 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
11541 }
11542
11543 std::string ErrorStr;
11544 raw_string_ostream ErrorFormatter(ErrorStr);
11545 ErrorFormatter << "Undefined external symbol ";
11546 ErrorFormatter << '"' << Symbol << '"';
11547 report_fatal_error(Twine(ErrorFormatter.str()));
11548}
11549
11550//===----------------------------------------------------------------------===//
11551// SDNode Class
11552//===----------------------------------------------------------------------===//
11553
11555 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11556 return Const != nullptr && Const->isZero();
11557}
11558
11560 ConstantFPSDNode *Const = dyn_cast<ConstantFPSDNode>(V);
11561 return Const != nullptr && Const->isZero() && !Const->isNegative();
11562}
11563
11565 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11566 return Const != nullptr && Const->isAllOnes();
11567}
11568
11570 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11571 return Const != nullptr && Const->isOne();
11572}
11573
11575 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11576 return Const != nullptr && Const->isMinSignedValue();
11577}
11578
11579bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
11580 unsigned OperandNo) {
11581 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
11582 // TODO: Target-specific opcodes could be added.
11583 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
11584 /*AllowTruncation*/ true)) {
11585 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
11586 switch (Opcode) {
11587 case ISD::ADD:
11588 case ISD::OR:
11589 case ISD::XOR:
11590 case ISD::UMAX:
11591 return Const.isZero();
11592 case ISD::MUL:
11593 return Const.isOne();
11594 case ISD::AND:
11595 case ISD::UMIN:
11596 return Const.isAllOnes();
11597 case ISD::SMAX:
11598 return Const.isMinSignedValue();
11599 case ISD::SMIN:
11600 return Const.isMaxSignedValue();
11601 case ISD::SUB:
11602 case ISD::SHL:
11603 case ISD::SRA:
11604 case ISD::SRL:
11605 return OperandNo == 1 && Const.isZero();
11606 case ISD::UDIV:
11607 case ISD::SDIV:
11608 return OperandNo == 1 && Const.isOne();
11609 }
11610 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
11611 switch (Opcode) {
11612 case ISD::FADD:
11613 return ConstFP->isZero() &&
11614 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
11615 case ISD::FSUB:
11616 return OperandNo == 1 && ConstFP->isZero() &&
11617 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
11618 case ISD::FMUL:
11619 return ConstFP->isExactlyValue(1.0);
11620 case ISD::FDIV:
11621 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
11622 case ISD::FMINNUM:
11623 case ISD::FMAXNUM: {
11624 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
11625 EVT VT = V.getValueType();
11627 APFloat NeutralAF = !Flags.hasNoNaNs()
11628 ? APFloat::getQNaN(Semantics)
11629 : !Flags.hasNoInfs()
11630 ? APFloat::getInf(Semantics)
11631 : APFloat::getLargest(Semantics);
11632 if (Opcode == ISD::FMAXNUM)
11633 NeutralAF.changeSign();
11634
11635 return ConstFP->isExactlyValue(NeutralAF);
11636 }
11637 }
11638 }
11639 return false;
11640}
11641
11643 while (V.getOpcode() == ISD::BITCAST)
11644 V = V.getOperand(0);
11645 return V;
11646}
11647
11649 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
11650 V = V.getOperand(0);
11651 return V;
11652}
11653
11655 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
11656 V = V.getOperand(0);
11657 return V;
11658}
11659
11661 while (V.getOpcode() == ISD::TRUNCATE)
11662 V = V.getOperand(0);
11663 return V;
11664}
11665
11666bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
11667 if (V.getOpcode() != ISD::XOR)
11668 return false;
11669 V = peekThroughBitcasts(V.getOperand(1));
11670 unsigned NumBits = V.getScalarValueSizeInBits();
11671 ConstantSDNode *C =
11672 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
11673 return C && (C->getAPIntValue().countr_one() >= NumBits);
11674}
11675
11677 bool AllowTruncation) {
11678 EVT VT = N.getValueType();
11679 APInt DemandedElts = VT.isFixedLengthVector()
11681 : APInt(1, 1);
11682 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
11683}
11684
11686 bool AllowUndefs,
11687 bool AllowTruncation) {
11688 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
11689 return CN;
11690
11691 // SplatVectors can truncate their operands. Ignore that case here unless
11692 // AllowTruncation is set.
11693 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
11694 EVT VecEltVT = N->getValueType(0).getVectorElementType();
11695 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11696 EVT CVT = CN->getValueType(0);
11697 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
11698 if (AllowTruncation || CVT == VecEltVT)
11699 return CN;
11700 }
11701 }
11702
11703 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
11704 BitVector UndefElements;
11705 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
11706
11707 // BuildVectors can truncate their operands. Ignore that case here unless
11708 // AllowTruncation is set.
11709 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
11710 if (CN && (UndefElements.none() || AllowUndefs)) {
11711 EVT CVT = CN->getValueType(0);
11712 EVT NSVT = N.getValueType().getScalarType();
11713 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
11714 if (AllowTruncation || (CVT == NSVT))
11715 return CN;
11716 }
11717 }
11718
11719 return nullptr;
11720}
11721
11723 EVT VT = N.getValueType();
11724 APInt DemandedElts = VT.isFixedLengthVector()
11726 : APInt(1, 1);
11727 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
11728}
11729
11731 const APInt &DemandedElts,
11732 bool AllowUndefs) {
11733 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
11734 return CN;
11735
11736 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
11737 BitVector UndefElements;
11738 ConstantFPSDNode *CN =
11739 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
11740 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
11741 if (CN && (UndefElements.none() || AllowUndefs))
11742 return CN;
11743 }
11744
11745 if (N.getOpcode() == ISD::SPLAT_VECTOR)
11746 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
11747 return CN;
11748
11749 return nullptr;
11750}
11751
11752bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
11753 // TODO: may want to use peekThroughBitcast() here.
11754 ConstantSDNode *C =
11755 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
11756 return C && C->isZero();
11757}
11758
11759bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
11760 ConstantSDNode *C =
11761 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
11762 return C && C->isOne();
11763}
11764
11765bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
11767 unsigned BitWidth = N.getScalarValueSizeInBits();
11768 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
11769 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
11770}
11771
11773 DropOperands();
11774}
11775
11776GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
11777 const DebugLoc &DL,
11778 const GlobalValue *GA, EVT VT,
11779 int64_t o, unsigned TF)
11780 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
11781 TheGlobal = GA;
11782}
11783
11785 EVT VT, unsigned SrcAS,
11786 unsigned DestAS)
11787 : SDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT)),
11788 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
11789
11790MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
11791 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
11792 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
11793 MemSDNodeBits.IsVolatile = MMO->isVolatile();
11794 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
11795 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
11796 MemSDNodeBits.IsInvariant = MMO->isInvariant();
11797
11798 // We check here that the size of the memory operand fits within the size of
11799 // the MMO. This is because the MMO might indicate only a possible address
11800 // range instead of specifying the affected memory addresses precisely.
11801 assert(
11802 (!MMO->getType().isValid() ||
11804 "Size mismatch!");
11805}
11806
11807/// Profile - Gather unique data for the node.
11808///
11810 AddNodeIDNode(ID, this);
11811}
11812
11813namespace {
11814
11815 struct EVTArray {
11816 std::vector<EVT> VTs;
11817
11818 EVTArray() {
11819 VTs.reserve(MVT::VALUETYPE_SIZE);
11820 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
11821 VTs.push_back(MVT((MVT::SimpleValueType)i));
11822 }
11823 };
11824
11825} // end anonymous namespace
11826
11827/// getValueTypeList - Return a pointer to the specified value type.
11828///
11829const EVT *SDNode::getValueTypeList(EVT VT) {
11830 static std::set<EVT, EVT::compareRawBits> EVTs;
11831 static EVTArray SimpleVTArray;
11832 static sys::SmartMutex<true> VTMutex;
11833
11834 if (VT.isExtended()) {
11835 sys::SmartScopedLock<true> Lock(VTMutex);
11836 return &(*EVTs.insert(VT).first);
11837 }
11838 assert(VT.getSimpleVT() < MVT::VALUETYPE_SIZE && "Value type out of range!");
11839 return &SimpleVTArray.VTs[VT.getSimpleVT().SimpleTy];
11840}
11841
11842/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
11843/// indicated value. This method ignores uses of other values defined by this
11844/// operation.
11845bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
11846 assert(Value < getNumValues() && "Bad value!");
11847
11848 // TODO: Only iterate over uses of a given value of the node
11849 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
11850 if (UI.getUse().getResNo() == Value) {
11851 if (NUses == 0)
11852 return false;
11853 --NUses;
11854 }
11855 }
11856
11857 // Found exactly the right number of uses?
11858 return NUses == 0;
11859}
11860
11861/// hasAnyUseOfValue - Return true if there are any use of the indicated
11862/// value. This method ignores uses of other values defined by this operation.
11863bool SDNode::hasAnyUseOfValue(unsigned Value) const {
11864 assert(Value < getNumValues() && "Bad value!");
11865
11866 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
11867 if (UI.getUse().getResNo() == Value)
11868 return true;
11869
11870 return false;
11871}
11872
11873/// isOnlyUserOf - Return true if this node is the only use of N.
11874bool SDNode::isOnlyUserOf(const SDNode *N) const {
11875 bool Seen = false;
11876 for (const SDNode *User : N->uses()) {
11877 if (User == this)
11878 Seen = true;
11879 else
11880 return false;
11881 }
11882
11883 return Seen;
11884}
11885
11886/// Return true if the only users of N are contained in Nodes.
11888 bool Seen = false;
11889 for (const SDNode *User : N->uses()) {
11890 if (llvm::is_contained(Nodes, User))
11891 Seen = true;
11892 else
11893 return false;
11894 }
11895
11896 return Seen;
11897}
11898
11899/// isOperand - Return true if this node is an operand of N.
11900bool SDValue::isOperandOf(const SDNode *N) const {
11901 return is_contained(N->op_values(), *this);
11902}
11903
11904bool SDNode::isOperandOf(const SDNode *N) const {
11905 return any_of(N->op_values(),
11906 [this](SDValue Op) { return this == Op.getNode(); });
11907}
11908
11909/// reachesChainWithoutSideEffects - Return true if this operand (which must
11910/// be a chain) reaches the specified operand without crossing any
11911/// side-effecting instructions on any chain path. In practice, this looks
11912/// through token factors and non-volatile loads. In order to remain efficient,
11913/// this only looks a couple of nodes in, it does not do an exhaustive search.
11914///
11915/// Note that we only need to examine chains when we're searching for
11916/// side-effects; SelectionDAG requires that all side-effects are represented
11917/// by chains, even if another operand would force a specific ordering. This
11918/// constraint is necessary to allow transformations like splitting loads.
11920 unsigned Depth) const {
11921 if (*this == Dest) return true;
11922
11923 // Don't search too deeply, we just want to be able to see through
11924 // TokenFactor's etc.
11925 if (Depth == 0) return false;
11926
11927 // If this is a token factor, all inputs to the TF happen in parallel.
11928 if (getOpcode() == ISD::TokenFactor) {
11929 // First, try a shallow search.
11930 if (is_contained((*this)->ops(), Dest)) {
11931 // We found the chain we want as an operand of this TokenFactor.
11932 // Essentially, we reach the chain without side-effects if we could
11933 // serialize the TokenFactor into a simple chain of operations with
11934 // Dest as the last operation. This is automatically true if the
11935 // chain has one use: there are no other ordering constraints.
11936 // If the chain has more than one use, we give up: some other
11937 // use of Dest might force a side-effect between Dest and the current
11938 // node.
11939 if (Dest.hasOneUse())
11940 return true;
11941 }
11942 // Next, try a deep search: check whether every operand of the TokenFactor
11943 // reaches Dest.
11944 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
11945 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
11946 });
11947 }
11948
11949 // Loads don't have side effects, look through them.
11950 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
11951 if (Ld->isUnordered())
11952 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
11953 }
11954 return false;
11955}
11956
11957bool SDNode::hasPredecessor(const SDNode *N) const {
11960 Worklist.push_back(this);
11961 return hasPredecessorHelper(N, Visited, Worklist);
11962}
11963
11965 this->Flags.intersectWith(Flags);
11966}
11967
11968SDValue
11970 ArrayRef<ISD::NodeType> CandidateBinOps,
11971 bool AllowPartials) {
11972 // The pattern must end in an extract from index 0.
11973 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
11974 !isNullConstant(Extract->getOperand(1)))
11975 return SDValue();
11976
11977 // Match against one of the candidate binary ops.
11978 SDValue Op = Extract->getOperand(0);
11979 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
11980 return Op.getOpcode() == unsigned(BinOp);
11981 }))
11982 return SDValue();
11983
11984 // Floating-point reductions may require relaxed constraints on the final step
11985 // of the reduction because they may reorder intermediate operations.
11986 unsigned CandidateBinOp = Op.getOpcode();
11987 if (Op.getValueType().isFloatingPoint()) {
11988 SDNodeFlags Flags = Op->getFlags();
11989 switch (CandidateBinOp) {
11990 case ISD::FADD:
11991 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
11992 return SDValue();
11993 break;
11994 default:
11995 llvm_unreachable("Unhandled FP opcode for binop reduction");
11996 }
11997 }
11998
11999 // Matching failed - attempt to see if we did enough stages that a partial
12000 // reduction from a subvector is possible.
12001 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
12002 if (!AllowPartials || !Op)
12003 return SDValue();
12004 EVT OpVT = Op.getValueType();
12005 EVT OpSVT = OpVT.getScalarType();
12006 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
12007 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
12008 return SDValue();
12009 BinOp = (ISD::NodeType)CandidateBinOp;
12010 return getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(Op), SubVT, Op,
12012 };
12013
12014 // At each stage, we're looking for something that looks like:
12015 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
12016 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
12017 // i32 undef, i32 undef, i32 undef, i32 undef>
12018 // %a = binop <8 x i32> %op, %s
12019 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
12020 // we expect something like:
12021 // <4,5,6,7,u,u,u,u>
12022 // <2,3,u,u,u,u,u,u>
12023 // <1,u,u,u,u,u,u,u>
12024 // While a partial reduction match would be:
12025 // <2,3,u,u,u,u,u,u>
12026 // <1,u,u,u,u,u,u,u>
12027 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
12028 SDValue PrevOp;
12029 for (unsigned i = 0; i < Stages; ++i) {
12030 unsigned MaskEnd = (1 << i);
12031
12032 if (Op.getOpcode() != CandidateBinOp)
12033 return PartialReduction(PrevOp, MaskEnd);
12034
12035 SDValue Op0 = Op.getOperand(0);
12036 SDValue Op1 = Op.getOperand(1);
12037
12038 ShuffleVectorSDNode *Shuffle = dyn_cast<ShuffleVectorSDNode>(Op0);
12039 if (Shuffle) {
12040 Op = Op1;
12041 } else {
12042 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
12043 Op = Op0;
12044 }
12045
12046 // The first operand of the shuffle should be the same as the other operand
12047 // of the binop.
12048 if (!Shuffle || Shuffle->getOperand(0) != Op)
12049 return PartialReduction(PrevOp, MaskEnd);
12050
12051 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
12052 for (int Index = 0; Index < (int)MaskEnd; ++Index)
12053 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
12054 return PartialReduction(PrevOp, MaskEnd);
12055
12056 PrevOp = Op;
12057 }
12058
12059 // Handle subvector reductions, which tend to appear after the shuffle
12060 // reduction stages.
12061 while (Op.getOpcode() == CandidateBinOp) {
12062 unsigned NumElts = Op.getValueType().getVectorNumElements();
12063 SDValue Op0 = Op.getOperand(0);
12064 SDValue Op1 = Op.getOperand(1);
12065 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
12067 Op0.getOperand(0) != Op1.getOperand(0))
12068 break;
12069 SDValue Src = Op0.getOperand(0);
12070 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
12071 if (NumSrcElts != (2 * NumElts))
12072 break;
12073 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
12074 Op1.getConstantOperandAPInt(1) == NumElts) &&
12075 !(Op1.getConstantOperandAPInt(1) == 0 &&
12076 Op0.getConstantOperandAPInt(1) == NumElts))
12077 break;
12078 Op = Src;
12079 }
12080
12081 BinOp = (ISD::NodeType)CandidateBinOp;
12082 return Op;
12083}
12084
12086 EVT VT = N->getValueType(0);
12087 EVT EltVT = VT.getVectorElementType();
12088 unsigned NE = VT.getVectorNumElements();
12089
12090 SDLoc dl(N);
12091
12092 // If ResNE is 0, fully unroll the vector op.
12093 if (ResNE == 0)
12094 ResNE = NE;
12095 else if (NE > ResNE)
12096 NE = ResNE;
12097
12098 if (N->getNumValues() == 2) {
12099 SmallVector<SDValue, 8> Scalars0, Scalars1;
12100 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12101 EVT VT1 = N->getValueType(1);
12102 EVT EltVT1 = VT1.getVectorElementType();
12103
12104 unsigned i;
12105 for (i = 0; i != NE; ++i) {
12106 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12107 SDValue Operand = N->getOperand(j);
12108 EVT OperandVT = Operand.getValueType();
12109
12110 // A vector operand; extract a single element.
12111 EVT OperandEltVT = OperandVT.getVectorElementType();
12112 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12113 Operand, getVectorIdxConstant(i, dl));
12114 }
12115
12116 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
12117 Scalars0.push_back(EltOp);
12118 Scalars1.push_back(EltOp.getValue(1));
12119 }
12120
12121 SDValue Vec0 = getBuildVector(VT, dl, Scalars0);
12122 SDValue Vec1 = getBuildVector(VT1, dl, Scalars1);
12123 return getMergeValues({Vec0, Vec1}, dl);
12124 }
12125
12126 assert(N->getNumValues() == 1 &&
12127 "Can't unroll a vector with multiple results!");
12128
12130 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12131
12132 unsigned i;
12133 for (i= 0; i != NE; ++i) {
12134 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12135 SDValue Operand = N->getOperand(j);
12136 EVT OperandVT = Operand.getValueType();
12137 if (OperandVT.isVector()) {
12138 // A vector operand; extract a single element.
12139 EVT OperandEltVT = OperandVT.getVectorElementType();
12140 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12141 Operand, getVectorIdxConstant(i, dl));
12142 } else {
12143 // A scalar operand; just use it as is.
12144 Operands[j] = Operand;
12145 }
12146 }
12147
12148 switch (N->getOpcode()) {
12149 default: {
12150 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
12151 N->getFlags()));
12152 break;
12153 }
12154 case ISD::VSELECT:
12155 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
12156 break;
12157 case ISD::SHL:
12158 case ISD::SRA:
12159 case ISD::SRL:
12160 case ISD::ROTL:
12161 case ISD::ROTR:
12162 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
12164 Operands[1])));
12165 break;
12167 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
12168 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
12169 Operands[0],
12170 getValueType(ExtVT)));
12171 }
12172 }
12173 }
12174
12175 for (; i < ResNE; ++i)
12176 Scalars.push_back(getUNDEF(EltVT));
12177
12178 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
12179 return getBuildVector(VecVT, dl, Scalars);
12180}
12181
12182std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
12183 SDNode *N, unsigned ResNE) {
12184 unsigned Opcode = N->getOpcode();
12185 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
12186 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
12187 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
12188 "Expected an overflow opcode");
12189
12190 EVT ResVT = N->getValueType(0);
12191 EVT OvVT = N->getValueType(1);
12192 EVT ResEltVT = ResVT.getVectorElementType();
12193 EVT OvEltVT = OvVT.getVectorElementType();
12194 SDLoc dl(N);
12195
12196 // If ResNE is 0, fully unroll the vector op.
12197 unsigned NE = ResVT.getVectorNumElements();
12198 if (ResNE == 0)
12199 ResNE = NE;
12200 else if (NE > ResNE)
12201 NE = ResNE;
12202
12203 SmallVector<SDValue, 8> LHSScalars;
12204 SmallVector<SDValue, 8> RHSScalars;
12205 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
12206 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
12207
12208 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
12209 SDVTList VTs = getVTList(ResEltVT, SVT);
12210 SmallVector<SDValue, 8> ResScalars;
12211 SmallVector<SDValue, 8> OvScalars;
12212 for (unsigned i = 0; i < NE; ++i) {
12213 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
12214 SDValue Ov =
12215 getSelect(dl, OvEltVT, Res.getValue(1),
12216 getBoolConstant(true, dl, OvEltVT, ResVT),
12217 getConstant(0, dl, OvEltVT));
12218
12219 ResScalars.push_back(Res);
12220 OvScalars.push_back(Ov);
12221 }
12222
12223 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
12224 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
12225
12226 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
12227 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
12228 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
12229 getBuildVector(NewOvVT, dl, OvScalars));
12230}
12231
12234 unsigned Bytes,
12235 int Dist) const {
12236 if (LD->isVolatile() || Base->isVolatile())
12237 return false;
12238 // TODO: probably too restrictive for atomics, revisit
12239 if (!LD->isSimple())
12240 return false;
12241 if (LD->isIndexed() || Base->isIndexed())
12242 return false;
12243 if (LD->getChain() != Base->getChain())
12244 return false;
12245 EVT VT = LD->getMemoryVT();
12246 if (VT.getSizeInBits() / 8 != Bytes)
12247 return false;
12248
12249 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
12250 auto LocDecomp = BaseIndexOffset::match(LD, *this);
12251
12252 int64_t Offset = 0;
12253 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
12254 return (Dist * (int64_t)Bytes == Offset);
12255 return false;
12256}
12257
12258/// InferPtrAlignment - Infer alignment of a load / store address. Return
12259/// std::nullopt if it cannot be inferred.
12261 // If this is a GlobalAddress + cst, return the alignment.
12262 const GlobalValue *GV = nullptr;
12263 int64_t GVOffset = 0;
12264 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
12265 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
12266 KnownBits Known(PtrWidth);
12268 unsigned AlignBits = Known.countMinTrailingZeros();
12269 if (AlignBits)
12270 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
12271 }
12272
12273 // If this is a direct reference to a stack slot, use information about the
12274 // stack slot's alignment.
12275 int FrameIdx = INT_MIN;
12276 int64_t FrameOffset = 0;
12277 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
12278 FrameIdx = FI->getIndex();
12279 } else if (isBaseWithConstantOffset(Ptr) &&
12280 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
12281 // Handle FI+Cst
12282 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
12283 FrameOffset = Ptr.getConstantOperandVal(1);
12284 }
12285
12286 if (FrameIdx != INT_MIN) {
12288 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
12289 }
12290
12291 return std::nullopt;
12292}
12293
12294/// Split the scalar node with EXTRACT_ELEMENT using the provided
12295/// VTs and return the low/high part.
12296std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
12297 const SDLoc &DL,
12298 const EVT &LoVT,
12299 const EVT &HiVT) {
12300 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
12301 "Split node must be a scalar type");
12302 SDValue Lo =
12304 SDValue Hi =
12306 return std::make_pair(Lo, Hi);
12307}
12308
12309/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
12310/// which is split (or expanded) into two not necessarily identical pieces.
12311std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
12312 // Currently all types are split in half.
12313 EVT LoVT, HiVT;
12314 if (!VT.isVector())
12315 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
12316 else
12317 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
12318
12319 return std::make_pair(LoVT, HiVT);
12320}
12321
12322/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
12323/// type, dependent on an enveloping VT that has been split into two identical
12324/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
12325std::pair<EVT, EVT>
12327 bool *HiIsEmpty) const {
12328 EVT EltTp = VT.getVectorElementType();
12329 // Examples:
12330 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
12331 // custom VL=9 with enveloping VL=8/8 yields 8/1
12332 // custom VL=10 with enveloping VL=8/8 yields 8/2
12333 // etc.
12334 ElementCount VTNumElts = VT.getVectorElementCount();
12335 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
12336 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
12337 "Mixing fixed width and scalable vectors when enveloping a type");
12338 EVT LoVT, HiVT;
12339 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
12340 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12341 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
12342 *HiIsEmpty = false;
12343 } else {
12344 // Flag that hi type has zero storage size, but return split envelop type
12345 // (this would be easier if vector types with zero elements were allowed).
12346 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
12347 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12348 *HiIsEmpty = true;
12349 }
12350 return std::make_pair(LoVT, HiVT);
12351}
12352
12353/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
12354/// low/high part.
12355std::pair<SDValue, SDValue>
12356SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
12357 const EVT &HiVT) {
12358 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
12359 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
12360 "Splitting vector with an invalid mixture of fixed and scalable "
12361 "vector types");
12363 N.getValueType().getVectorMinNumElements() &&
12364 "More vector elements requested than available!");
12365 SDValue Lo, Hi;
12366 Lo =
12368 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
12369 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
12370 // IDX with the runtime scaling factor of the result vector type. For
12371 // fixed-width result vectors, that runtime scaling factor is 1.
12374 return std::make_pair(Lo, Hi);
12375}
12376
12377std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
12378 const SDLoc &DL) {
12379 // Split the vector length parameter.
12380 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
12381 EVT VT = N.getValueType();
12383 "Expecting the mask to be an evenly-sized vector");
12384 unsigned HalfMinNumElts = VecVT.getVectorMinNumElements() / 2;
12385 SDValue HalfNumElts =
12386 VecVT.isFixedLengthVector()
12387 ? getConstant(HalfMinNumElts, DL, VT)
12388 : getVScale(DL, VT, APInt(VT.getScalarSizeInBits(), HalfMinNumElts));
12389 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
12390 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
12391 return std::make_pair(Lo, Hi);
12392}
12393
12394/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
12396 EVT VT = N.getValueType();
12399 return getNode(ISD::INSERT_SUBVECTOR, DL, WideVT, getUNDEF(WideVT), N,
12401}
12402
12405 unsigned Start, unsigned Count,
12406 EVT EltVT) {
12407 EVT VT = Op.getValueType();
12408 if (Count == 0)
12409 Count = VT.getVectorNumElements();
12410 if (EltVT == EVT())
12411 EltVT = VT.getVectorElementType();
12412 SDLoc SL(Op);
12413 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
12414 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Op,
12415 getVectorIdxConstant(i, SL)));
12416 }
12417}
12418
12419// getAddressSpace - Return the address space this GlobalAddress belongs to.
12421 return getGlobal()->getType()->getAddressSpace();
12422}
12423
12426 return Val.MachineCPVal->getType();
12427 return Val.ConstVal->getType();
12428}
12429
12430bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
12431 unsigned &SplatBitSize,
12432 bool &HasAnyUndefs,
12433 unsigned MinSplatBits,
12434 bool IsBigEndian) const {
12435 EVT VT = getValueType(0);
12436 assert(VT.isVector() && "Expected a vector type");
12437 unsigned VecWidth = VT.getSizeInBits();
12438 if (MinSplatBits > VecWidth)
12439 return false;
12440
12441 // FIXME: The widths are based on this node's type, but build vectors can
12442 // truncate their operands.
12443 SplatValue = APInt(VecWidth, 0);
12444 SplatUndef = APInt(VecWidth, 0);
12445
12446 // Get the bits. Bits with undefined values (when the corresponding element
12447 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
12448 // in SplatValue. If any of the values are not constant, give up and return
12449 // false.
12450 unsigned int NumOps = getNumOperands();
12451 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
12452 unsigned EltWidth = VT.getScalarSizeInBits();
12453
12454 for (unsigned j = 0; j < NumOps; ++j) {
12455 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
12456 SDValue OpVal = getOperand(i);
12457 unsigned BitPos = j * EltWidth;
12458
12459 if (OpVal.isUndef())
12460 SplatUndef.setBits(BitPos, BitPos + EltWidth);
12461 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
12462 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
12463 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
12464 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
12465 else
12466 return false;
12467 }
12468
12469 // The build_vector is all constants or undefs. Find the smallest element
12470 // size that splats the vector.
12471 HasAnyUndefs = (SplatUndef != 0);
12472
12473 // FIXME: This does not work for vectors with elements less than 8 bits.
12474 while (VecWidth > 8) {
12475 // If we can't split in half, stop here.
12476 if (VecWidth & 1)
12477 break;
12478
12479 unsigned HalfSize = VecWidth / 2;
12480 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
12481 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
12482 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
12483 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
12484
12485 // If the two halves do not match (ignoring undef bits), stop here.
12486 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
12487 MinSplatBits > HalfSize)
12488 break;
12489
12490 SplatValue = HighValue | LowValue;
12491 SplatUndef = HighUndef & LowUndef;
12492
12493 VecWidth = HalfSize;
12494 }
12495
12496 // FIXME: The loop above only tries to split in halves. But if the input
12497 // vector for example is <3 x i16> it wouldn't be able to detect a
12498 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
12499 // optimizations. I guess that back in the days when this helper was created
12500 // vectors normally was power-of-2 sized.
12501
12502 SplatBitSize = VecWidth;
12503 return true;
12504}
12505
12507 BitVector *UndefElements) const {
12508 unsigned NumOps = getNumOperands();
12509 if (UndefElements) {
12510 UndefElements->clear();
12511 UndefElements->resize(NumOps);
12512 }
12513 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
12514 if (!DemandedElts)
12515 return SDValue();
12516 SDValue Splatted;
12517 for (unsigned i = 0; i != NumOps; ++i) {
12518 if (!DemandedElts[i])
12519 continue;
12520 SDValue Op = getOperand(i);
12521 if (Op.isUndef()) {
12522 if (UndefElements)
12523 (*UndefElements)[i] = true;
12524 } else if (!Splatted) {
12525 Splatted = Op;
12526 } else if (Splatted != Op) {
12527 return SDValue();
12528 }
12529 }
12530
12531 if (!Splatted) {
12532 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
12533 assert(getOperand(FirstDemandedIdx).isUndef() &&
12534 "Can only have a splat without a constant for all undefs.");
12535 return getOperand(FirstDemandedIdx);
12536 }
12537
12538 return Splatted;
12539}
12540
12542 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
12543 return getSplatValue(DemandedElts, UndefElements);
12544}
12545
12547 SmallVectorImpl<SDValue> &Sequence,
12548 BitVector *UndefElements) const {
12549 unsigned NumOps = getNumOperands();
12550 Sequence.clear();
12551 if (UndefElements) {
12552 UndefElements->clear();
12553 UndefElements->resize(NumOps);
12554 }
12555 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
12556 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
12557 return false;
12558
12559 // Set the undefs even if we don't find a sequence (like getSplatValue).
12560 if (UndefElements)
12561 for (unsigned I = 0; I != NumOps; ++I)
12562 if (DemandedElts[I] && getOperand(I).isUndef())
12563 (*UndefElements)[I] = true;
12564
12565 // Iteratively widen the sequence length looking for repetitions.
12566 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
12567 Sequence.append(SeqLen, SDValue());
12568 for (unsigned I = 0; I != NumOps; ++I) {
12569 if (!DemandedElts[I])
12570 continue;
12571 SDValue &SeqOp = Sequence[I % SeqLen];
12573 if (Op.isUndef()) {
12574 if (!SeqOp)
12575 SeqOp = Op;
12576 continue;
12577 }
12578 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
12579 Sequence.clear();
12580 break;
12581 }
12582 SeqOp = Op;
12583 }
12584 if (!Sequence.empty())
12585 return true;
12586 }
12587
12588 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
12589 return false;
12590}
12591
12593 BitVector *UndefElements) const {
12594 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
12595 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
12596}
12597
12600 BitVector *UndefElements) const {
12601 return dyn_cast_or_null<ConstantSDNode>(
12602 getSplatValue(DemandedElts, UndefElements));
12603}
12604
12607 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
12608}
12609
12612 BitVector *UndefElements) const {
12613 return dyn_cast_or_null<ConstantFPSDNode>(
12614 getSplatValue(DemandedElts, UndefElements));
12615}
12616
12619 return dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements));
12620}
12621
12622int32_t
12624 uint32_t BitWidth) const {
12625 if (ConstantFPSDNode *CN =
12626 dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements))) {
12627 bool IsExact;
12628 APSInt IntVal(BitWidth);
12629 const APFloat &APF = CN->getValueAPF();
12630 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
12631 APFloat::opOK ||
12632 !IsExact)
12633 return -1;
12634
12635 return IntVal.exactLogBase2();
12636 }
12637 return -1;
12638}
12639
12641 bool IsLittleEndian, unsigned DstEltSizeInBits,
12642 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
12643 // Early-out if this contains anything but Undef/Constant/ConstantFP.
12644 if (!isConstant())
12645 return false;
12646
12647 unsigned NumSrcOps = getNumOperands();
12648 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
12649 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
12650 "Invalid bitcast scale");
12651
12652 // Extract raw src bits.
12653 SmallVector<APInt> SrcBitElements(NumSrcOps,
12654 APInt::getZero(SrcEltSizeInBits));
12655 BitVector SrcUndeElements(NumSrcOps, false);
12656
12657 for (unsigned I = 0; I != NumSrcOps; ++I) {
12659 if (Op.isUndef()) {
12660 SrcUndeElements.set(I);
12661 continue;
12662 }
12663 auto *CInt = dyn_cast<ConstantSDNode>(Op);
12664 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
12665 assert((CInt || CFP) && "Unknown constant");
12666 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
12667 : CFP->getValueAPF().bitcastToAPInt();
12668 }
12669
12670 // Recast to dst width.
12671 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
12672 SrcBitElements, UndefElements, SrcUndeElements);
12673 return true;
12674}
12675
12676void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
12677 unsigned DstEltSizeInBits,
12678 SmallVectorImpl<APInt> &DstBitElements,
12679 ArrayRef<APInt> SrcBitElements,
12680 BitVector &DstUndefElements,
12681 const BitVector &SrcUndefElements) {
12682 unsigned NumSrcOps = SrcBitElements.size();
12683 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
12684 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
12685 "Invalid bitcast scale");
12686 assert(NumSrcOps == SrcUndefElements.size() &&
12687 "Vector size mismatch");
12688
12689 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
12690 DstUndefElements.clear();
12691 DstUndefElements.resize(NumDstOps, false);
12692 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
12693
12694 // Concatenate src elements constant bits together into dst element.
12695 if (SrcEltSizeInBits <= DstEltSizeInBits) {
12696 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
12697 for (unsigned I = 0; I != NumDstOps; ++I) {
12698 DstUndefElements.set(I);
12699 APInt &DstBits = DstBitElements[I];
12700 for (unsigned J = 0; J != Scale; ++J) {
12701 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
12702 if (SrcUndefElements[Idx])
12703 continue;
12704 DstUndefElements.reset(I);
12705 const APInt &SrcBits = SrcBitElements[Idx];
12706 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
12707 "Illegal constant bitwidths");
12708 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
12709 }
12710 }
12711 return;
12712 }
12713
12714 // Split src element constant bits into dst elements.
12715 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
12716 for (unsigned I = 0; I != NumSrcOps; ++I) {
12717 if (SrcUndefElements[I]) {
12718 DstUndefElements.set(I * Scale, (I + 1) * Scale);
12719 continue;
12720 }
12721 const APInt &SrcBits = SrcBitElements[I];
12722 for (unsigned J = 0; J != Scale; ++J) {
12723 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
12724 APInt &DstBits = DstBitElements[Idx];
12725 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
12726 }
12727 }
12728}
12729
12731 for (const SDValue &Op : op_values()) {
12732 unsigned Opc = Op.getOpcode();
12733 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
12734 return false;
12735 }
12736 return true;
12737}
12738
12739std::optional<std::pair<APInt, APInt>>
12741 unsigned NumOps = getNumOperands();
12742 if (NumOps < 2)
12743 return std::nullopt;
12744
12745 if (!isa<ConstantSDNode>(getOperand(0)) ||
12746 !isa<ConstantSDNode>(getOperand(1)))
12747 return std::nullopt;
12748
12749 unsigned EltSize = getValueType(0).getScalarSizeInBits();
12750 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
12751 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
12752
12753 if (Stride.isZero())
12754 return std::nullopt;
12755
12756 for (unsigned i = 2; i < NumOps; ++i) {
12757 if (!isa<ConstantSDNode>(getOperand(i)))
12758 return std::nullopt;
12759
12760 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
12761 if (Val != (Start + (Stride * i)))
12762 return std::nullopt;
12763 }
12764
12765 return std::make_pair(Start, Stride);
12766}
12767
12768bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
12769 // Find the first non-undef value in the shuffle mask.
12770 unsigned i, e;
12771 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
12772 /* search */;
12773
12774 // If all elements are undefined, this shuffle can be considered a splat
12775 // (although it should eventually get simplified away completely).
12776 if (i == e)
12777 return true;
12778
12779 // Make sure all remaining elements are either undef or the same as the first
12780 // non-undef value.
12781 for (int Idx = Mask[i]; i != e; ++i)
12782 if (Mask[i] >= 0 && Mask[i] != Idx)
12783 return false;
12784 return true;
12785}
12786
12787// Returns the SDNode if it is a constant integer BuildVector
12788// or constant integer.
12790 if (isa<ConstantSDNode>(N))
12791 return N.getNode();
12793 return N.getNode();
12794 // Treat a GlobalAddress supporting constant offset folding as a
12795 // constant integer.
12796 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N))
12797 if (GA->getOpcode() == ISD::GlobalAddress &&
12798 TLI->isOffsetFoldingLegal(GA))
12799 return GA;
12800 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
12801 isa<ConstantSDNode>(N.getOperand(0)))
12802 return N.getNode();
12803 return nullptr;
12804}
12805
12806// Returns the SDNode if it is a constant float BuildVector
12807// or constant float.
12809 if (isa<ConstantFPSDNode>(N))
12810 return N.getNode();
12811
12813 return N.getNode();
12814
12815 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
12816 isa<ConstantFPSDNode>(N.getOperand(0)))
12817 return N.getNode();
12818
12819 return nullptr;
12820}
12821
12822void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
12823 assert(!Node->OperandList && "Node already has operands");
12825 "too many operands to fit into SDNode");
12826 SDUse *Ops = OperandRecycler.allocate(
12827 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
12828
12829 bool IsDivergent = false;
12830 for (unsigned I = 0; I != Vals.size(); ++I) {
12831 Ops[I].setUser(Node);
12832 Ops[I].setInitial(Vals[I]);
12833 if (Ops[I].Val.getValueType() != MVT::Other) // Skip Chain. It does not carry divergence.
12834 IsDivergent |= Ops[I].getNode()->isDivergent();
12835 }
12836 Node->NumOperands = Vals.size();
12837 Node->OperandList = Ops;
12838 if (!TLI->isSDNodeAlwaysUniform(Node)) {
12839 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
12840 Node->SDNodeBits.IsDivergent = IsDivergent;
12841 }
12843}
12844
12847 size_t Limit = SDNode::getMaxNumOperands();
12848 while (Vals.size() > Limit) {
12849 unsigned SliceIdx = Vals.size() - Limit;
12850 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
12851 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
12852 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
12853 Vals.emplace_back(NewTF);
12854 }
12855 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
12856}
12857
12859 EVT VT, SDNodeFlags Flags) {
12860 switch (Opcode) {
12861 default:
12862 return SDValue();
12863 case ISD::ADD:
12864 case ISD::OR:
12865 case ISD::XOR:
12866 case ISD::UMAX:
12867 return getConstant(0, DL, VT);
12868 case ISD::MUL:
12869 return getConstant(1, DL, VT);
12870 case ISD::AND:
12871 case ISD::UMIN:
12872 return getAllOnesConstant(DL, VT);
12873 case ISD::SMAX:
12875 case ISD::SMIN:
12877 case ISD::FADD:
12878 return getConstantFP(-0.0, DL, VT);
12879 case ISD::FMUL:
12880 return getConstantFP(1.0, DL, VT);
12881 case ISD::FMINNUM:
12882 case ISD::FMAXNUM: {
12883 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
12884 const fltSemantics &Semantics = EVTToAPFloatSemantics(VT);
12885 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
12886 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
12887 APFloat::getLargest(Semantics);
12888 if (Opcode == ISD::FMAXNUM)
12889 NeutralAF.changeSign();
12890
12891 return getConstantFP(NeutralAF, DL, VT);
12892 }
12893 case ISD::FMINIMUM:
12894 case ISD::FMAXIMUM: {
12895 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
12896 const fltSemantics &Semantics = EVTToAPFloatSemantics(VT);
12897 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
12898 : APFloat::getLargest(Semantics);
12899 if (Opcode == ISD::FMAXIMUM)
12900 NeutralAF.changeSign();
12901
12902 return getConstantFP(NeutralAF, DL, VT);
12903 }
12904
12905 }
12906}
12907
12908/// Helper used to make a call to a library function that has one argument of
12909/// pointer type.
12910///
12911/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
12912/// used to get or set floating-point state. They have one argument of pointer
12913/// type, which points to the memory region containing bits of the
12914/// floating-point state. The value returned by such function is ignored in the
12915/// created call.
12916///
12917/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
12918/// \param Ptr Pointer used to save/load state.
12919/// \param InChain Ingoing token chain.
12920/// \returns Outgoing chain token.
12922 SDValue InChain,
12923 const SDLoc &DLoc) {
12924 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
12927 Entry.Node = Ptr;
12928 Entry.Ty = Ptr.getValueType().getTypeForEVT(*getContext());
12929 Args.push_back(Entry);
12930 RTLIB::Libcall LC = static_cast<RTLIB::Libcall>(LibFunc);
12931 SDValue Callee = getExternalSymbol(TLI->getLibcallName(LC),
12932 TLI->getPointerTy(getDataLayout()));
12934 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
12935 TLI->getLibcallCallingConv(LC), Type::getVoidTy(*getContext()), Callee,
12936 std::move(Args));
12937 return TLI->LowerCallTo(CLI).second;
12938}
12939
12941 assert(From && To && "Invalid SDNode; empty source SDValue?");
12942 auto I = SDEI.find(From);
12943 if (I == SDEI.end())
12944 return;
12945
12946 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
12947 // the iterator, hence the need to make a copy to prevent a use-after-free.
12948 NodeExtraInfo NEI = I->second;
12949 if (LLVM_LIKELY(!NEI.PCSections)) {
12950 // No deep copy required for the types of extra info set.
12951 //
12952 // FIXME: Investigate if other types of extra info also need deep copy. This
12953 // depends on the types of nodes they can be attached to: if some extra info
12954 // is only ever attached to nodes where a replacement To node is always the
12955 // node where later use and propagation of the extra info has the intended
12956 // semantics, no deep copy is required.
12957 SDEI[To] = std::move(NEI);
12958 return;
12959 }
12960
12961 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
12962 // through the replacement of From with To. Otherwise, replacements of a node
12963 // (From) with more complex nodes (To and its operands) may result in lost
12964 // extra info where the root node (To) is insignificant in further propagating
12965 // and using extra info when further lowering to MIR.
12966 //
12967 // In the first step pre-populate the visited set with the nodes reachable
12968 // from the old From node. This avoids copying NodeExtraInfo to parts of the
12969 // DAG that is not new and should be left untouched.
12970 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
12971 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
12972 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
12973 if (MaxDepth == 0) {
12974 // Remember this node in case we need to increase MaxDepth and continue
12975 // populating FromReach from this node.
12976 Leafs.emplace_back(N);
12977 return;
12978 }
12979 if (!FromReach.insert(N).second)
12980 return;
12981 for (const SDValue &Op : N->op_values())
12982 Self(Self, Op.getNode(), MaxDepth - 1);
12983 };
12984
12985 // Copy extra info to To and all its transitive operands (that are new).
12987 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
12988 if (FromReach.contains(N))
12989 return true;
12990 if (!Visited.insert(N).second)
12991 return true;
12992 if (getEntryNode().getNode() == N)
12993 return false;
12994 for (const SDValue &Op : N->op_values()) {
12995 if (!Self(Self, Op.getNode()))
12996 return false;
12997 }
12998 // Copy only if entry node was not reached.
12999 SDEI[N] = NEI;
13000 return true;
13001 };
13002
13003 // We first try with a lower MaxDepth, assuming that the path to common
13004 // operands between From and To is relatively short. This significantly
13005 // improves performance in the common case. The initial MaxDepth is big
13006 // enough to avoid retry in the common case; the last MaxDepth is large
13007 // enough to avoid having to use the fallback below (and protects from
13008 // potential stack exhaustion from recursion).
13009 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
13010 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
13011 // StartFrom is the previous (or initial) set of leafs reachable at the
13012 // previous maximum depth.
13014 std::swap(StartFrom, Leafs);
13015 for (const SDNode *N : StartFrom)
13016 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
13017 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
13018 return;
13019 // This should happen very rarely (reached the entry node).
13020 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
13021 assert(!Leafs.empty());
13022 }
13023
13024 // This should not happen - but if it did, that means the subgraph reachable
13025 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
13026 // could not visit all reachable common operands. Consequently, we were able
13027 // to reach the entry node.
13028 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
13029 assert(false && "From subgraph too complex - increase max. MaxDepth?");
13030 // Best-effort fallback if assertions disabled.
13031 SDEI[To] = std::move(NEI);
13032}
13033
13034#ifndef NDEBUG
13035static void checkForCyclesHelper(const SDNode *N,
13038 const llvm::SelectionDAG *DAG) {
13039 // If this node has already been checked, don't check it again.
13040 if (Checked.count(N))
13041 return;
13042
13043 // If a node has already been visited on this depth-first walk, reject it as
13044 // a cycle.
13045 if (!Visited.insert(N).second) {
13046 errs() << "Detected cycle in SelectionDAG\n";
13047 dbgs() << "Offending node:\n";
13048 N->dumprFull(DAG); dbgs() << "\n";
13049 abort();
13050 }
13051
13052 for (const SDValue &Op : N->op_values())
13053 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
13054
13055 Checked.insert(N);
13056 Visited.erase(N);
13057}
13058#endif
13059
13061 const llvm::SelectionDAG *DAG,
13062 bool force) {
13063#ifndef NDEBUG
13064 bool check = force;
13065#ifdef EXPENSIVE_CHECKS
13066 check = true;
13067#endif // EXPENSIVE_CHECKS
13068 if (check) {
13069 assert(N && "Checking nonexistent SDNode");
13072 checkForCyclesHelper(N, visited, checked, DAG);
13073 }
13074#endif // !NDEBUG
13075}
13076
13077void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
13078 checkForCycles(DAG->getRoot().getNode(), DAG, force);
13079}
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static bool isConstant(const MachineInstr &MI)
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
This file implements the BitVector class.
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
static std::optional< bool > isBigEndian(const SmallDenseMap< int64_t, int64_t, 8 > &MemOffset2Idx, int64_t LowestIdx)
Given a map from byte offsets in memory to indices in a load/store, determine if that map corresponds...
#define __asan_unpoison_memory_region(p, size)
Definition: Compiler.h:460
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:240
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Looks at all the uses of the given value Returns the Liveness deduced from the uses of this value Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses If the result is MaybeLiveUses might be modified but its content should be ignored(since it might not be complete). DeadArgumentEliminationPass
Given that RA is a live propagate it s liveness to any other values it uses(according to Uses). void DeadArgumentEliminationPass
Given that RA is a live value
#define LLVM_DEBUG(X)
Definition: Debug.h:101
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
This file defines a hash set that can be used to remove duplication of nodes in a graph.
Rewrite Partial Register Uses
#define check(cond)
static const unsigned MaxDepth
static LVOptions Options
Definition: LVOptions.cpp:25
static Register getMemsetValue(Register Val, LLT Ty, MachineIRBuilder &MIB)
static bool shouldLowerMemFuncForSize(const MachineFunction &MF)
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:528
static Align getPrefTypeAlign(EVT VT, SelectionDAG &DAG)
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
mir Rename Register Operands
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
unsigned const TargetRegisterInfo * TRI
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
static unsigned getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
PowerPC Reduce CR logical Operation
const SmallVectorImpl< MachineOperand > & Cond
Contains matchers for matching SelectionDAG nodes and values.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow)
static SDValue getMemsetStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo)
Lower the call to 'memset' intrinsic function into a series of store operations.
static std::optional< APInt > FoldValueWithUndef(unsigned Opcode, const APInt &C1, bool IsUndef1, const APInt &C2, bool IsUndef2)
static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step, SelectionDAG &DAG)
static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC, SDVTList VTList, ArrayRef< SDValue > OpList)
static SDValue getMemsetStringVal(EVT VT, const SDLoc &dl, SelectionDAG &DAG, const TargetLowering &TLI, const ConstantDataArraySlice &Slice)
getMemsetStringVal - Similar to getMemsetValue.
static cl::opt< bool > EnableMemCpyDAGOpt("enable-memcpy-dag-opt", cl::Hidden, cl::init(true), cl::desc("Gang up loads and stores generated by inlining of memcpy"))
static bool haveNoCommonBitsSetCommutative(SDValue A, SDValue B)
static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList)
AddNodeIDValueTypes - Value type lists are intern'd so we can represent them solely with their pointe...
static void commuteShuffle(SDValue &N1, SDValue &N2, MutableArrayRef< int > M)
Swaps the values of N1 and N2.
static bool isMemSrcFromConstant(SDValue Src, ConstantDataArraySlice &Slice)
Returns true if memcpy source is constant data.
static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo)
static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo, AAResults *AA)
static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC)
AddNodeIDOpcode - Add the node opcode to the NodeID data.
static ISD::CondCode getSetCCInverseImpl(ISD::CondCode Op, bool isIntegerLike)
static bool doNotCSE(SDNode *N)
doNotCSE - Return true if CSE should not be performed for this node.
static cl::opt< int > MaxLdStGlue("ldstmemcpy-glue-max", cl::desc("Number limit for gluing ld/st of memcpy."), cl::Hidden, cl::init(0))
static void AddNodeIDOperands(FoldingSetNodeID &ID, ArrayRef< SDValue > Ops)
AddNodeIDOperands - Various routines for adding operands to the NodeID data.
static SDValue foldCONCAT_VECTORS(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
Try to simplify vector concatenation to an input value, undef, or build vector.
static MachinePointerInfo InferPointerInfo(const MachinePointerInfo &Info, SelectionDAG &DAG, SDValue Ptr, int64_t Offset=0)
InferPointerInfo - If the specified ptr/offset is a frame index, infer a MachinePointerInfo record fr...
static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N)
If this is an SDNode with special info, add this info to the NodeID data.
static void NewSDValueDbgMsg(SDValue V, StringRef Msg, SelectionDAG *G)
static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs)
makeVTList - Return an instance of the SDVTList struct initialized with the specified members.
static void VerifySDNode(SDNode *N, const TargetLowering *TLI)
VerifySDNode - Check the given SDNode. Aborts if it is invalid.
static void checkForCyclesHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallPtrSetImpl< const SDNode * > &Checked, const llvm::SelectionDAG *DAG)
static void chainLoadsAndStoresForMemcpy(SelectionDAG &DAG, const SDLoc &dl, SmallVector< SDValue, 32 > &OutChains, unsigned From, unsigned To, SmallVector< SDValue, 16 > &OutLoadChains, SmallVector< SDValue, 16 > &OutStoreChains)
static int isSignedOp(ISD::CondCode Opcode)
For an integer comparison, return 1 if the comparison is a signed operation and 2 if the result is an...
static std::optional< APInt > FoldValue(unsigned Opcode, const APInt &C1, const APInt &C2)
static SDValue FoldBUILD_VECTOR(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
static void checkAddrSpaceIsValidForLibcall(const TargetLowering *TLI, unsigned AS)
static Constant * ConstantFold(Instruction *I, const DataLayout &DL, const SmallDenseMap< Value *, Constant * > &ConstantPool)
Try to fold instruction I into a constant.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file describes how to lower LLVM code to machine code.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:191
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
Value * RHS
Value * LHS
static unsigned getSize(unsigned Kind)
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
Checks whether the given location points to constant memory, or if OrLocal is true whether it points ...
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition: APFloat.h:988
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1069
void copySign(const APFloat &RHS)
Definition: APFloat.h:1163
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5196
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1051
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:1278
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1042
bool isFinite() const
Definition: APFloat.h:1300
bool isNaN() const
Definition: APFloat.h:1293
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1193
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1060
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition: APFloat.h:1096
bool isZero() const
Definition: APFloat.h:1291
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1006
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:1185
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:966
opStatus mod(const APFloat &RHS)
Definition: APFloat.h:1087
bool isPosZero() const
Definition: APFloat.h:1306
bool isNegZero() const
Definition: APFloat.h:1307
void changeSign()
Definition: APFloat.h:1158
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition: APFloat.h:977
bool isInfinity() const
Definition: APFloat.h:1292
Class for arbitrary precision integers.
Definition: APInt.h:76
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1941
APInt usub_sat(const APInt &RHS) const
Definition: APInt.cpp:2025
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1543
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:212
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1385
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:981
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:207
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1491
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition: APInt.h:1370
unsigned popcount() const
Count the number of bits set.
Definition: APInt.h:1620
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition: APInt.h:1364
APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition: APInt.cpp:608
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:1002
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1463
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:906
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition: APInt.h:1308
APInt abs() const
Get the absolute value.
Definition: APInt.h:1737
APInt sadd_sat(const APInt &RHS) const
Definition: APInt.cpp:1996
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:349
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1160
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:358
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1636
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1439
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1089
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:187
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:307
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1614
void clearAllBits()
Set every bit to 0.
Definition: APInt.h:1375
APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition: APInt.cpp:1124
APInt reverseBits() const
Definition: APInt.cpp:737
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:812
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1144
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1589
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition: APInt.h:1578
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition: APInt.h:1548
static APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition: APInt.cpp:620
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:197
APInt sshl_sat(const APInt &RHS) const
Definition: APInt.cpp:2056
APInt ushl_sat(const APInt &RHS) const
Definition: APInt.cpp:2070
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1010
APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition: APInt.cpp:1111
void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition: APInt.cpp:368
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition: APInt.h:1395
unsigned logBase2() const
Definition: APInt.h:1703
APInt uadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2006
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:805
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1297
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition: APInt.cpp:1706
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:312
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1128
APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:954
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition: APInt.h:1345
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:851
APInt byteSwap() const
Definition: APInt.cpp:715
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition: APInt.h:1235
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:418
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:284
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:178
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1367
APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition: APInt.cpp:453
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition: APInt.h:1215
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:264
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:217
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:836
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:829
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1199
APInt ssub_sat(const APInt &RHS) const
Definition: APInt.cpp:2015
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
AddrSpaceCastSDNode(unsigned Order, const DebugLoc &dl, EVT VT, unsigned SrcAS, unsigned DestAS)
Recycle small arrays allocated from a BumpPtrAllocator.
Definition: ArrayRecycler.h:28
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:154
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
iterator begin() const
Definition: ArrayRef.h:153
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition: ArrayRef.h:195
This is an SDNode representing atomic operations.
static BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, offset addresses.
BitVector & reset()
Definition: BitVector.h:392
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition: BitVector.h:341
void clear()
clear - Removes all bits from the bitvector.
Definition: BitVector.h:335
BitVector & set()
Definition: BitVector.h:351
bool none() const
none - Returns true if none of the bits are set.
Definition: BitVector.h:188
size_type size() const
size - Returns the number of bits in this bitvector.
Definition: BitVector.h:159
unsigned getTargetFlags() const
const BlockAddress * getBlockAddress() const
The address of a basic block.
Definition: Constants.h:889
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A "pseudo-class" with methods for operating on BUILD_VECTORs.
bool getConstantRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &RawBitElements, BitVector &UndefElements) const
Extract the raw bit data from a build vector of Undef, Constant or ConstantFP node elements.
static void recastRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &DstBitElements, ArrayRef< APInt > SrcBitElements, BitVector &DstUndefElements, const BitVector &SrcUndefElements)
Recast bit data SrcBitElements to DstEltSizeInBits wide elements.
bool getRepeatedSequence(const APInt &DemandedElts, SmallVectorImpl< SDValue > &Sequence, BitVector *UndefElements=nullptr) const
Find the shortest repeating sequence of values in the build vector.
ConstantFPSDNode * getConstantFPSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant FP or null if this is not a constant FP splat.
std::optional< std::pair< APInt, APInt > > isConstantSequence() const
If this BuildVector is constant and represents the numerical series "<a, a+n, a+2n,...
SDValue getSplatValue(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted value or a null value if this is not a splat.
bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef, unsigned &SplatBitSize, bool &HasAnyUndefs, unsigned MinSplatBits=0, bool isBigEndian=false) const
Check if this is a constant splat, and if so, find the smallest element size that splats the vector.
ConstantSDNode * getConstantSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant or null if this is not a constant splat.
int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements, uint32_t BitWidth) const
If this is a constant FP splat and the splatted constant FP is an exact power or 2,...
LLVM_ATTRIBUTE_RETURNS_NONNULL void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:148
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it,...
Definition: Allocator.h:123
static bool isValueValidForType(EVT VT, const APFloat &Val)
const APFloat & getValueAPF() const
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:268
This is the shared class of boolean and integer constants.
Definition: Constants.h:80
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition: Constants.h:148
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:145
bool isMachineConstantPoolEntry() const
This class represents a range of values.
Definition: ConstantRange.h:47
ConstantRange multiply(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
static ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
KnownBits toKnownBits() const
Return known bits for values in this range.
ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
uint64_t getZExtValue() const
const APInt & getAPIntValue() const
This is an important base class in LLVM.
Definition: Constant.h:41
Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
Definition: Constants.cpp:1699
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:432
DWARF expression.
static ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed)
Returns the ops for a zero- or sign-extension in a DIExpression.
static void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static const DIExpression * convertToVariadicExpression(const DIExpression *Expr)
If Expr is a non-variadic expression (i.e.
static std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
Base class for variables.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
bool isLittleEndian() const
Layout endianness...
Definition: DataLayout.h:238
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
Definition: DataLayout.cpp:878
Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:865
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:763
Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Definition: DataLayout.cpp:874
A debug info location.
Definition: DebugLoc.h:33
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:103
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
const char * getSymbol() const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:320
MachineBasicBlock * MBB
MBB - The current block.
Data structure describing the variable locations in a function.
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition: Function.h:681
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition: Function.h:678
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:339
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:356
unsigned getAddressSpace() const
const GlobalValue * getGlobal() const
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
Definition: GlobalValue.h:263
unsigned getAddressSpace() const
Definition: GlobalValue.h:205
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:294
This class is used to form a handle around another node that is persistent and is updated across invo...
static bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
constexpr bool isValid() const
Definition: LowLevelType.h:145
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
This SDNode is used for LIFETIME_START/LIFETIME_END values, which indicate the offet and size that ar...
This class is used to represent ISD::LOAD nodes.
static LocationSize precise(uint64_t Value)
TypeSize getValue() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
Metadata node.
Definition: Metadata.h:1067
Machine Value Type.
SimpleValueType SimpleTy
static MVT getIntegerVT(unsigned BitWidth)
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
Abstract base class for all machine specific constantpool value subclasses.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
void setObjectAlignment(int ObjectIdx, Align Alignment)
setObjectAlignment - Change the alignment of the specified stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
LocationSize getSize() const
Return the size in bytes of the memory reference.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
const MachinePointerInfo & getPointerInfo() const
Flags getFlags() const
Return the raw flags of the source value,.
An SDNode that represents everything that will be needed to construct a MachineInstr.
This class is used to represent an MGATHER node.
This class is used to represent an MLOAD node.
This class is used to represent an MSCATTER node.
This class is used to represent an MSTORE node.
This SDNode is used for target intrinsics that touch memory and need an associated MachineMemOperand.
MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt, MachineMemOperand *MMO)
MachineMemOperand * MMO
Memory reference information.
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
unsigned getRawSubclassData() const
Return the SubclassData value, without HasDebugValue.
EVT getMemoryVT() const
Return the type of the in-memory value.
Representation for a specific memory location.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:191
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
The optimization diagnostic interface.
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Definition: DerivedTypes.h:662
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:679
bool isNull() const
Test if the pointer held in the union is null, regardless of which type it is.
Definition: PointerUnion.h:142
Analysis providing profile information.
void Deallocate(SubClass *E)
Deallocate - Release storage for the pointed-to object.
Keeps track of dbg_value information through SDISel.
Definition: SelectionDAG.h:159
BumpPtrAllocator & getAlloc()
Definition: SelectionDAG.h:188
void add(SDDbgValue *V, bool isParameter)
void erase(const SDNode *Node)
Invalidate all DbgValues attached to the node and remove it from the Node-to-DbgValues map.
ArrayRef< SDDbgValue * > getSDDbgValues(const SDNode *Node) const
Definition: SelectionDAG.h:194
Holds the information from a dbg_label node through SDISel.
Holds the information for a single machine location through SDISel; either an SDNode,...
static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo)
static SDDbgOperand fromFrameIdx(unsigned FrameIdx)
static SDDbgOperand fromVReg(unsigned VReg)
static SDDbgOperand fromConst(const Value *Const)
@ SDNODE
Value is the result of an expression.
Holds the information from a dbg_value node through SDISel.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
const DebugLoc & getDebugLoc() const
unsigned getIROrder() const
This class provides iterator support for SDUse operands that use a specific SDNode.
Represents one node in the SelectionDAG.
ArrayRef< SDUse > ops() const
const APInt & getAsAPIntVal() const
Helper method returns the APInt value of a ConstantSDNode.
void dumprFull(const SelectionDAG *G=nullptr) const
printrFull to dbgs().
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool isDivergent() const
bool isOnlyUserOf(const SDNode *N) const
Return true if this node is the only use of N.
iterator_range< value_op_iterator > op_values() const
unsigned getIROrder() const
Return the node ordering.
static constexpr size_t getMaxNumOperands()
Return the maximum number of operands that a SDNode can hold.
MemSDNodeBitfields MemSDNodeBits
void Profile(FoldingSetNodeID &ID) const
Gather unique data for the node.
bool getHasDebugValue() const
SDNodeFlags getFlags() const
void setNodeId(int Id)
Set unique node id.
void intersectFlagsWith(const SDNodeFlags Flags)
Clear any flags in this node that aren't also set in Flags.
static bool hasPredecessorHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallVectorImpl< const SDNode * > &Worklist, unsigned int MaxSteps=0, bool TopologicalPrune=false)
Returns true if N is a predecessor of any node in Worklist.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
bool use_empty() const
Return true if there are no uses of this node.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
unsigned getNumOperands() const
Return the number of values used by this operation.
const SDValue & getOperand(unsigned Num) const
static bool areOnlyUsersOf(ArrayRef< const SDNode * > Nodes, const SDNode *N)
Return true if all the users of N are contained in Nodes.
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
bool isOperandOf(const SDNode *N) const
Return true if this node is an operand of N.
const APInt & getConstantOperandAPInt(unsigned Num) const
Helper method returns the APInt of a ConstantSDNode operand.
bool hasPredecessor(const SDNode *N) const
Return true if N is a predecessor of this node.
bool hasAnyUseOfValue(unsigned Value) const
Return true if there are any use of the indicated value.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
bool isUndef() const
Return true if the type of the node type undefined.
bool hasNUsesOfValue(unsigned NUses, unsigned Value) const
Return true if there are exactly NUSES uses of the indicated value.
op_iterator op_end() const
op_iterator op_begin() const
static use_iterator use_end()
void DropOperands()
Release the operands and set this node to have zero operands.
Represents a use of a SDNode.
SDNode * getNode() const
Convenience function for get().getNode().
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
bool isOperandOf(const SDNode *N) const
Return true if this node is an operand of N.
bool reachesChainWithoutSideEffects(SDValue Dest, unsigned Depth=2) const
Return true if this operand (which must be a chain) reaches the specified operand without crossing an...
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
bool use_empty() const
Return true if there are no nodes using value ResNo of Node.
const APInt & getConstantOperandAPInt(unsigned i) const
uint64_t getScalarValueSizeInBits() const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
uint64_t getConstantOperandVal(unsigned i) const
unsigned getOpcode() const
virtual SDValue EmitTargetCodeForMemset(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, bool AlwaysInline, MachinePointerInfo DstPtrInfo) const
Emit target-specific code that performs a memset.
virtual SDValue EmitTargetCodeForMemmove(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const
Emit target-specific code that performs a memmove.
virtual SDValue EmitTargetCodeForMemcpy(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const
Emit target-specific code that performs a memcpy.
SDNodeFlags getFlags() const
Definition: SelectionDAG.h:378
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:225
Align getReducedAlign(EVT VT, bool UseABI)
In most cases this function returns the ABI alignment for a given type, except for illegal vector typ...
SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op)
Return the specified value casted to the target's desired shift amount type.
SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
SDValue getExtLoadVP(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsExpanding=false)
SDValue getSplatSourceVector(SDValue V, int &SplatIndex)
If V is a splatted value, return the source vector and its splat index.
SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root, MCSymbol *Label)
OverflowKind computeOverflowForUnsignedSub(SDValue N0, SDValue N1) const
Determine if the result of the unsigned sub of 2 nodes can overflow.
unsigned ComputeMaxSignificantBits(SDValue Op, unsigned Depth=0) const
Get the upper bound on bit size for this Value Op as a signed integer.
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
Definition: SelectionDAG.h:550
SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), AAResults *AA=nullptr)
SDValue getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy)
bool isKnownNeverSNaN(SDValue Op, unsigned Depth=0) const
SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS)
Return an AddrSpaceCastSDNode.
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS)
Helper function to make it easier to build Select's if you just have operands and don't want to check...
SDValue getStackArgumentTokenFactor(SDValue Chain)
Compute a TokenFactor to force all the incoming stack arguments to be loaded from the stack.
const TargetSubtargetInfo & getSubtarget() const
Definition: SelectionDAG.h:473
const APInt * getValidMaximumShiftAmountConstant(SDValue V, const APInt &DemandedElts) const
If a SHL/SRA/SRL node V has constant shift amounts that are all less than the element bit-width of th...
SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
void updateDivergence(SDNode *N)
SDValue getSplatValue(SDValue V, bool LegalTypes=false)
If V is a splat vector, return its scalar source operand by extracting that element from the source v...
SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond, const SDLoc &dl)
Constant fold a setcc to true or false.
MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
SDValue getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT, SDNodeFlags Flags)
Get the (commutative) neutral element for the given opcode, if it exists.
SDValue getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Value, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo)
SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm, bool ConstantFold=true)
Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
SDValue getPseudoProbeNode(const SDLoc &Dl, SDValue Chain, uint64_t Guid, uint64_t Index, uint32_t Attr)
Creates a PseudoProbeSDNode with function GUID Guid and the index of the block Index it is probing,...
SDValue getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
SDNode * SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT)
These are used for target selectors to mutate the specified node to have the specified return type,...
SelectionDAG(const TargetMachine &TM, CodeGenOptLevel)
SDValue getBitcastedSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
SDValue getStridedLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
SDNode * isConstantIntBuildVectorOrConstantInt(SDValue N) const
Test whether the given value is a constant int or similar node.
SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDVTList VTs, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp, MachineMemOperand *MMO)
Gets a node for an atomic cmpxchg op.
SDValue makeEquivalentMemoryOrdering(SDValue OldChain, SDValue NewMemOpChain)
If an existing load has uses of its chain, create a token factor node with that chain and the new mem...
SDDbgValue * getVRegDbgValue(DIVariable *Var, DIExpression *Expr, unsigned VReg, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a VReg SDDbgValue node.
void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To, unsigned Num)
Like ReplaceAllUsesOfValueWith, but for multiple values at once.
SDValue getJumpTableDebugInfo(int JTI, SDValue Chain, const SDLoc &DL)
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false)
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
SDValue getSymbolFunctionGlobalAddress(SDValue Op, Function **TargetFunction=nullptr)
Return a GlobalAddress of the function from the current module with name matching the given ExternalS...
SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
OverflowKind
Used to represent the possible overflow behavior of an operation.
bool isADDLike(SDValue Op) const
Return true if the specified operand is an ISD::OR or ISD::XOR node that can be treated as an ISD::AD...
bool haveNoCommonBitsSet(SDValue A, SDValue B) const
Return true if A and B have no common bits set.
bool calculateDivergence(SDNode *N)
SDValue getElementCount(const SDLoc &DL, EVT VT, ElementCount EC, bool ConstantFold=true)
SDValue getGetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A)
Return an AssertAlignSDNode.
SDNode * mutateStrictFPToFP(SDNode *Node)
Mutate the specified strict FP node to its non-strict equivalent, unlinking the node from its chain a...
SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
SDValue getBitcastedZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
SDValue getStepVector(const SDLoc &DL, EVT ResVT, const APInt &StepVal)
Returns a vector of type ResVT whose elements contain the linear sequence <0, Step,...
SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Val, MachineMemOperand *MMO)
Gets a node for an atomic op, produces result (if relevant) and chain and takes 2 operands.
Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
bool shouldOptForSize() const
SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
SDValue getVPZExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be an integer vector, to the vector-type VT,...
const TargetLowering & getTargetLoweringInfo() const
Definition: SelectionDAG.h:477
bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
Definition: SelectionDAG.h:447
SDValue getStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
SDValue expandVACopy(SDNode *Node)
Expand the specified ISD::VACOPY node as the Legalize pass would.
SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
APInt computeVectorKnownZeroElements(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
For each demanded element of a vector, see if it is known to be zero.
void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
bool NewNodesMustHaveLegalTypes
When true, additional steps are taken to ensure that getConstant() and similar functions return DAG n...
Definition: SelectionDAG.h:386
std::pair< EVT, EVT > GetSplitDestVTs(const EVT &VT) const
Compute the VTs needed for the low/hi parts of a type which is split (or expanded) into two not neces...
void salvageDebugInfo(SDNode &N)
To be invoked on an SDNode that is slated to be erased.
SDNode * MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs, ArrayRef< SDValue > Ops)
This mutates the specified node to have the specified return type, opcode, and operands.
std::pair< SDValue, SDValue > UnrollVectorOverflowOp(SDNode *N, unsigned ResNE=0)
Like UnrollVectorOp(), but for the [US](ADD|SUB|MUL)O family of opcodes.
allnodes_const_iterator allnodes_begin() const
Definition: SelectionDAG.h:530
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
SDValue getGatherVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
Definition: SelectionDAG.h:827
SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), AAResults *AA=nullptr)
SDValue getBitcastedAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts, unsigned Depth=0) const
Test whether V has a splatted value for all the demanded elements.
void DeleteNode(SDNode *N)
Remove the specified node from the system.
SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
SDDbgValue * getDbgValueList(DIVariable *Var, DIExpression *Expr, ArrayRef< SDDbgOperand > Locs, ArrayRef< SDNode * > Dependencies, bool IsIndirect, const DebugLoc &DL, unsigned O, bool IsVariadic)
Creates a SDDbgValue node from a list of locations.
SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT)
Create negative operation as (SUB 0, Val).
void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal)
Try to simplify a select/vselect into 1 of its operands or a constant.
SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
const DataLayout & getDataLayout() const
Definition: SelectionDAG.h:471
SDNode * isConstantFPBuildVectorOrConstantFP(SDValue N) const
Test whether the given value is a constant FP or similar node.
SDValue expandVAArg(SDNode *Node)
Expand the specified ISD::VAARG node as the Legalize pass would.
SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
bool doesNodeExist(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops)
Check if a node exists without modifying its flags.
bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base, unsigned Bytes, int Dist) const
Return true if loads are next to each other and can be merged.
SDDbgLabel * getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O)
Creates a SDDbgLabel node.
SDValue getStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
OverflowKind computeOverflowForUnsignedMul(SDValue N0, SDValue N1) const
Determine if the result of the unsigned mul of 2 nodes can overflow.
void copyExtraInfo(SDNode *From, SDNode *To)
Copy extra info associated with one node to another.
SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned TargetFlags=0)
SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, bool isTailCall, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo=AAMDNodes())
SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue SV, unsigned Align)
VAArg produces a result and token chain, and takes a pointer and a source value as input.
SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
Definition: SelectionDAG.h:658
SDValue getMDNode(const MDNode *MD)
Return an MDNodeSDNode which holds an MDNode.
void clear()
Clear state and free memory necessary to make this SelectionDAG ready to process a new block.
void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV)
Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to the shuffle node in input but with swa...
std::pair< SDValue, SDValue > SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the vector with EXTRACT_SUBVECTOR using the provided VTs and return the low/high part.
const APInt * getValidShiftAmountConstant(SDValue V, const APInt &DemandedElts) const
If a SHL/SRA/SRL node V has a constant or splat constant shift amount that is less than the element b...
SDValue makeStateFunctionCall(unsigned LibFunc, SDValue Ptr, SDValue InChain, const SDLoc &DLoc)
Helper used to make a call to a library function that has one argument of pointer type.
bool isGuaranteedNotToBeUndefOrPoison(SDValue Op, bool PoisonOnly=false, unsigned Depth=0) const
Return true if this function can prove that Op is never poison and, if PoisonOnly is false,...
SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
SDValue getIndexedLoadVP(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
SDValue getSrcValue(const Value *v)
Construct a node to track a Value* through the backend.
SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op)
Definition: SelectionDAG.h:861
SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
OverflowKind computeOverflowForSignedMul(SDValue N0, SDValue N1) const
Determine if the result of the signed mul of 2 nodes can overflow.
MaybeAlign InferPtrAlign(SDValue Ptr) const
Infer alignment of a load / store address.
bool MaskedValueIsAllOnes(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if '(Op & Mask) == Mask'.
bool SignBitIsZero(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero.
SDValue getRegister(unsigned Reg, EVT VT)
void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE, Pass *PassPtr, const TargetLibraryInfo *LibraryInfo, UniformityInfo *UA, ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin, FunctionVarLocs const *FnVarLocs)
Prepare this SelectionDAG to process code in the given MachineFunction.
void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
void AddDbgLabel(SDDbgLabel *DB)
Add a dbg_label SDNode.
bool isConstantValueOfAnyType(SDValue N) const
SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
SDValue getBasicBlock(MachineBasicBlock *MBB)
SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
bool isKnownToBeAPowerOfTwo(SDValue Val, unsigned Depth=0) const
Test if the given value is known to have exactly one bit set.
SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label)
SDValue getIndexedStoreVP(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
bool isKnownNeverZero(SDValue Op, unsigned Depth=0) const
Test whether the given SDValue is known to contain non-zero value(s).
SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
SDValue getSetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT)
Convert Op, which must be of integer type, to the integer type VT, by using an extension appropriate ...
SDValue getMaskedStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Base, SDValue Offset, SDValue Mask, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
static const fltSemantics & EVTToAPFloatSemantics(EVT VT)
Returns an APFloat semantics tag appropriate for the given type.
SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
Definition: SelectionDAG.h:472
std::pair< SDValue, SDValue > getStrictFPExtendOrRound(SDValue Op, SDValue Chain, const SDLoc &DL, EVT VT)
Convert Op, which must be a STRICT operation of float type, to the float type VT, by either extending...
std::pair< SDValue, SDValue > SplitEVL(SDValue N, EVT VecVT, const SDLoc &DL)
Split the explicit vector length parameter of a VP operation.
SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either truncating it or perform...
SDValue getVPLogicalNOT(const SDLoc &DL, SDValue Val, SDValue Mask, SDValue EVL, EVT VT)
Create a vector-predicated logical NOT operation as (VP_XOR Val, BooleanOne, Mask,...
SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
iterator_range< allnodes_iterator > allnodes()
Definition: SelectionDAG.h:542
SDValue getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, bool isTarget=false, unsigned TargetFlags=0)
SDValue WidenVector(const SDValue &N, const SDLoc &DL)
Widen the vector up to the next power of two using INSERT_SUBVECTOR.
bool isKnownNeverZeroFloat(SDValue Op) const
Test whether the given floating point SDValue is known to never be positive or negative zero.
SDValue getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, const MDNode *Ranges=nullptr, bool IsExpanding=false)
SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
SDDbgValue * getConstantDbgValue(DIVariable *Var, DIExpression *Expr, const Value *C, const DebugLoc &DL, unsigned O)
Creates a constant SDDbgValue node.
SDValue getScatterVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getValueType(EVT)
ArrayRef< SDDbgValue * > GetDbgValues(const SDNode *SD) const
Get the debug values which reference the given SDNode.
SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
OverflowKind computeOverflowForSignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the signed addition of 2 nodes can overflow.
SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
unsigned AssignTopologicalOrder()
Topological-sort the AllNodes list and a assign a unique node id for each node in the DAG based on th...
ilist< SDNode >::size_type allnodes_size() const
Definition: SelectionDAG.h:538
SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
bool isKnownNeverNaN(SDValue Op, bool SNaN=false, unsigned Depth=0) const
Test whether the given SDValue (or all elements of it, if it is a vector) is known to never be NaN.
SDValue getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
SDValue getTruncStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsCompressing=false)
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
Definition: SelectionDAG.h:675
unsigned ComputeNumSignBits(SDValue Op, unsigned Depth=0) const
Return the number of times the sign bit of the register is replicated into the other bits.
bool MaskedVectorIsZero(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
Return true if 'Op' is known to be zero in DemandedElts.
SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT)
Create a true or false constant of type VT using the target's BooleanContent for type OpVT.
SDDbgValue * getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr, unsigned FI, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a FrameIndex SDDbgValue node.
SDValue getExtStridedLoadVP(ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
SDValue getJumpTable(int JTI, EVT VT, bool isTarget=false, unsigned TargetFlags=0)
bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side,...
SDValue getVPPtrExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be of integer type, to the vector-type integer type VT,...
SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.getNode() alone.
MachineFunction & getMachineFunction() const
Definition: SelectionDAG.h:468
SDValue getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to extend the Op as a pointer value assuming it was the smaller SrcTy ...
bool canCreateUndefOrPoison(SDValue Op, const APInt &DemandedElts, bool PoisonOnly=false, bool ConsiderFlags=true, unsigned Depth=0) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
OverflowKind computeOverflowForUnsignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the unsigned addition of 2 nodes can overflow.
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
Definition: SelectionDAG.h:844
SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
SDValue getTruncStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT SVT, MachineMemOperand *MMO, bool IsCompressing=false)
void canonicalizeCommutativeBinop(unsigned Opcode, SDValue &N1, SDValue &N2) const
Swap N1 and N2 if Opcode is a commutative binary opcode and the canonical form expects the opposite o...
KnownBits computeKnownBits(SDValue Op, unsigned Depth=0) const
Determine which bits of Op are known to be either zero or one and return them in Known.
SDValue getRegisterMask(const uint32_t *RegMask)
SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
SDValue getCondCode(ISD::CondCode Cond)
SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain, int FrameIndex, int64_t Size, int64_t Offset=-1)
Creates a LifetimeSDNode that starts (IsStart==true) or ends (IsStart==false) the lifetime of the por...
bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
OverflowKind computeOverflowForSignedSub(SDValue N0, SDValue N1) const
Determine if the result of the signed sub of 2 nodes can overflow.
LLVMContext * getContext() const
Definition: SelectionDAG.h:484
SDValue simplifyFPBinop(unsigned Opcode, SDValue X, SDValue Y, SDNodeFlags Flags)
Try to simplify a floating-point binary operation into 1 of its operands or a constant.
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
Definition: SelectionDAG.h:559
const APInt * getValidMinimumShiftAmountConstant(SDValue V, const APInt &DemandedElts) const
If a SHL/SRA/SRL node V has constant shift amounts that are all less than the element bit-width of th...
SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL, bool LegalTypes=true)
SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef< SDValue > Ops, EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags Flags=MachineMemOperand::MOLoad|MachineMemOperand::MOStore, LocationSize Size=0, const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
SDValue getMCSymbol(MCSymbol *Sym, EVT VT)
bool isUndef(unsigned Opcode, ArrayRef< SDValue > Ops)
Return true if the result of this operation is always undefined.
SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
std::pair< EVT, EVT > GetDependentSplitDestVTs(const EVT &VT, const EVT &EnvVT, bool *HiIsEmpty) const
Compute the VTs needed for the low/hi parts of a type, dependent on an enveloping VT that has been sp...
SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
Fold floating-point operations when all operands are constants and/or undefined.
SDNode * getNodeIfExists(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops, const SDNodeFlags Flags)
Get the specified node if it's already available, or else return NULL.
SDValue FoldSymbolOffset(unsigned Opcode, EVT VT, const GlobalAddressSDNode *GA, const SDNode *N2)
SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand, SDValue Subreg)
A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
Definition: SelectionDAG.h:553
SDDbgValue * getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N, unsigned R, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a SDDbgValue node.
SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Base, SDValue Offset, SDValue Mask, SDValue Src0, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, ISD::LoadExtType, bool IsExpanding=false)
SDValue getSplat(EVT VT, const SDLoc &DL, SDValue Op)
Returns a node representing a splat of one value into all lanes of the provided vector type.
Definition: SelectionDAG.h:877
std::pair< SDValue, SDValue > SplitScalar(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the scalar node with EXTRACT_ELEMENT using the provided VTs and return the low/high part.
SDValue matchBinOpReduction(SDNode *Extract, ISD::NodeType &BinOp, ArrayRef< ISD::NodeType > CandidateBinOps, bool AllowPartials=false)
Match a binop + shuffle pyramid that represents a horizontal reduction over the elements of a vector ...
SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
SDValue simplifyShift(SDValue X, SDValue Y)
Try to simplify a shift into 1 of its operands or a constant.
void transferDbgValues(SDValue From, SDValue To, unsigned OffsetInBits=0, unsigned SizeInBits=0, bool InvalidateDbg=true)
Transfer debug values from one node to another, while optionally generating fragment expressions for ...
SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a logical NOT operation as (XOR Val, BooleanOne).
SDValue getMaskedScatter(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, bool IsTruncating=false)
ilist< SDNode >::iterator allnodes_iterator
Definition: SelectionDAG.h:533
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
static bool isSplatMask(const int *Mask, EVT VT)
int getMaskElt(unsigned Idx) const
ArrayRef< int > getMask() const
static void commuteMask(MutableArrayRef< int > Mask)
Change values in a shuffle permute mask assuming the two vector operands have swapped position.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:321
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false.
Definition: SmallPtrSet.h:356
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:360
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:342
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:717
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
void reserve(size_type N)
Definition: SmallVector.h:676
iterator erase(const_iterator CI)
Definition: SmallVector.h:750
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
This class is used to represent ISD::STORE nodes.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
Completely target-dependent object reference.
unsigned getTargetFlags() const
Provides information about what library functions are available for the current target.
CallingConv::ID getLibcallCallingConv(RTLIB::Libcall Call) const
Get the CallingConv that should be used for the specified libcall.
virtual bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const
Return true if it is beneficial to convert a load of a constant to just the constant itself.
virtual bool isSExtCheaperThanZExt(EVT FromTy, EVT ToTy) const
Return true if sign-extension from FromTy to ToTy is cheaper than zero-extension.
virtual MVT getVectorIdxTy(const DataLayout &DL) const
Returns the type to be used for the index operand of: ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT...
const TargetMachine & getTargetMachine() const
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
unsigned getMaxStoresPerMemcpy(bool OptSize) const
Get maximum # of store operations permitted for llvm.memcpy.
virtual bool isCommutativeBinOp(unsigned Opcode) const
Returns true if the opcode is a commutative binary operation.
virtual ISD::NodeType getExtendForAtomicOps() const
Returns how the platform's atomic operations are extended (ZERO_EXTEND, SIGN_EXTEND,...
virtual bool isExtractSubvectorCheap(EVT ResVT, EVT SrcVT, unsigned Index) const
Return true if EXTRACT_SUBVECTOR is cheap for extracting this result type from this source type with ...
virtual bool shallExtractConstSplatVectorElementToStore(Type *VectorTy, unsigned ElemSizeInBits, unsigned &Index) const
Return true if the target shall perform extract vector element and store given that the vector is kno...
virtual bool isTruncateFree(Type *FromTy, Type *ToTy) const
Return true if it's free to truncate a value of type FromTy to type ToTy.
virtual EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const
Return the ValueType of the result of SETCC operations.
EVT getShiftAmountTy(EVT LHSTy, const DataLayout &DL, bool LegalTypes=true) const
Returns the type for the shift amount of a shift opcode.
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
BooleanContent getBooleanContents(bool isVec, bool isFloat) const
For targets without i1 registers, this gives the nature of the high-bits of boolean values held in ty...
bool isCondCodeLegal(ISD::CondCode CC, MVT VT) const
Return true if the specified condition code is legal on this target.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
BooleanContent
Enum that describes how the target represents true/false values.
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
Align getMinStackArgumentAlignment() const
Return the minimum stack alignment of an argument.
LegalizeTypeAction getTypeAction(LLVMContext &Context, EVT VT) const
Return how we should legalize values of this type, either it is already legal (return 'Legal') or we ...
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
std::vector< ArgListEntry > ArgListTy
virtual bool hasVectorBlend() const
Return true if the target has a vector blend instruction.
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
MVT getFrameIndexTy(const DataLayout &DL) const
Return the type for frame index, which is determined by the alloca address space specified through th...
virtual bool isLegalStoreImmediate(int64_t Value) const
Return true if the specified immediate is legal for the value input of a store instruction.
unsigned getVectorTypeBreakdown(LLVMContext &Context, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Vector types are broken down into some number of legal first class types.
static ISD::NodeType getExtendForContent(BooleanContent Content)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual void computeKnownBitsForFrameIndex(int FIOp, KnownBits &Known, const MachineFunction &MF) const
Determine which of the bits of FrameIndex FIOp are known to be 0.
virtual unsigned ComputeNumSignBitsForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const
This method can be implemented by targets that want to expose additional information about sign bits ...
virtual void verifyTargetSDNode(const SDNode *N) const
Check the given SDNode. Aborts if it is invalid.
virtual bool findOptimalMemOpLowering(std::vector< EVT > &MemOps, unsigned Limit, const MemOp &Op, unsigned DstAS, unsigned SrcAS, const AttributeList &FuncAttributes) const
Determines the optimal series of memory ops to replace the memset / memcpy.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
virtual bool isKnownNeverNaNForTargetNode(SDValue Op, const SelectionDAG &DAG, bool SNaN=false, unsigned Depth=0) const
If SNaN is false,.
virtual void computeKnownBitsForTargetNode(const SDValue Op, KnownBits &Known, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const
Determine which of the bits specified in Mask are known to be either zero or one and return them in t...
virtual bool isSDNodeSourceOfDivergence(const SDNode *N, FunctionLoweringInfo *FLI, UniformityInfo *UA) const
virtual bool isSDNodeAlwaysUniform(const SDNode *N) const
virtual bool isSplatValueForTargetNode(SDValue Op, const APInt &DemandedElts, APInt &UndefElts, const SelectionDAG &DAG, unsigned Depth=0) const
Return true if vector Op has the same value across all DemandedElts, indicating any elements which ma...
virtual bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const
Return true if folding a constant offset with the given GlobalAddress is legal.
virtual const Constant * getTargetConstantFromLoad(LoadSDNode *LD) const
This method returns the constant pool value that will be loaded by LD.
virtual bool isGAPlusOffset(SDNode *N, const GlobalValue *&GA, int64_t &Offset) const
Returns true (and the GlobalValue and the offset) if the node is a GlobalAddress + offset.
virtual bool isGuaranteedNotToBeUndefOrPoisonForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, bool PoisonOnly, unsigned Depth) const
Return true if this function can prove that Op is never poison and, if PoisonOnly is false,...
virtual bool canCreateUndefOrPoisonForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, bool PoisonOnly, bool ConsiderFlags, unsigned Depth) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:76
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
TargetOptions Options
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const SelectionDAGTargetInfo * getSelectionDAGInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetLowering * getTargetLowering() const
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, XROS, or DriverKit).
Definition: Triple.h:553
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition: TypeSize.h:330
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:265
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
static Type * getVoidTy(LLVMContext &C)
static IntegerType * getInt8Ty(LLVMContext &C)
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
void set(Value *Val)
Definition: Value.h:882
This class is used to represent an VP_GATHER node.
This class is used to represent a VP_LOAD node.
This class is used to represent an VP_SCATTER node.
This class is used to represent a VP_STORE node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_LOAD node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_STORE node.
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition: DenseSet.h:185
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:187
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:217
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:171
constexpr bool isKnownEven() const
A return value of true indicates we know at compile time that the number of elements (vscale * Min) i...
Definition: TypeSize.h:176
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition: TypeSize.h:168
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:224
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:660
std::string & str()
Returns the string's reference.
Definition: raw_ostream.h:678
SmartMutex - A mutex with a compile time constant parameter that indicates whether this mutex should ...
Definition: Mutex.h:28
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition: APInt.cpp:3100
const APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition: APInt.h:2203
APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition: APInt.cpp:3087
APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition: APInt.cpp:3077
const APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition: APInt.h:2198
APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition: APInt.cpp:3092
APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition: APInt.cpp:2978
APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition: APInt.cpp:3072
APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition: APInt.cpp:3082
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
CondCode getSetCCInverse(CondCode Operation, bool isIntegerLike)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical AND between different comparisons of identical values: ((X op1 Y) & (X...
bool isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are ~0 ...
bool isNON_EXTLoad(const SDNode *N)
Returns true if the specified node is a non-extending load.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition: ISDOpcodes.h:40
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition: ISDOpcodes.h:750
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition: ISDOpcodes.h:236
@ CTLZ_ZERO_UNDEF
Definition: ISDOpcodes.h:723
@ TargetConstantPool
Definition: ISDOpcodes.h:168
@ MDNODE_SDNODE
MDNODE_SDNODE - This is a node that holdes an MDNode*, which is used to reference metadata in the IR.
Definition: ISDOpcodes.h:1177
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition: ISDOpcodes.h:476
@ ATOMIC_LOAD_FMAX
Definition: ISDOpcodes.h:1282
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition: ISDOpcodes.h:44
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
Definition: ISDOpcodes.h:1345
@ VECREDUCE_SMIN
Definition: ISDOpcodes.h:1376
@ FGETSIGN
INT = FGETSIGN(FP) - Return the sign bit of the specified floating point value as an integer 0/1 valu...
Definition: ISDOpcodes.h:497
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition: ISDOpcodes.h:250
@ ATOMIC_LOAD_NAND
Definition: ISDOpcodes.h:1275
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition: ISDOpcodes.h:559
@ JUMP_TABLE_DEBUG_INFO
JUMP_TABLE_DEBUG_INFO - Jumptable debug info.
Definition: ISDOpcodes.h:1066
@ BSWAP
Byte Swap and Counting operators.
Definition: ISDOpcodes.h:714
@ TargetBlockAddress
Definition: ISDOpcodes.h:170
@ ConstantFP
Definition: ISDOpcodes.h:77
@ ATOMIC_LOAD_MAX
Definition: ISDOpcodes.h:1277
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, ptr, val) This corresponds to "store atomic" instruction.
Definition: ISDOpcodes.h:1247
@ ATOMIC_LOAD_UMIN
Definition: ISDOpcodes.h:1278
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:269
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition: ISDOpcodes.h:487
@ FMAXNUM_IEEE
Definition: ISDOpcodes.h:985
@ ADD
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:239
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:1037
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:783
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition: ISDOpcodes.h:483
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition: ISDOpcodes.h:199
@ GlobalAddress
Definition: ISDOpcodes.h:78
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
Definition: ISDOpcodes.h:1260
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition: ISDOpcodes.h:790
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition: ISDOpcodes.h:543
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
Definition: ISDOpcodes.h:1361
@ FADD
Simple binary floating point operators.
Definition: ISDOpcodes.h:390
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
Definition: ISDOpcodes.h:1365
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition: ISDOpcodes.h:688
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition: ISDOpcodes.h:820
@ VECREDUCE_SMAX
Definition: ISDOpcodes.h:1375
@ STRICT_FSETCCS
Definition: ISDOpcodes.h:477
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
Definition: ISDOpcodes.h:913
@ ATOMIC_LOAD_OR
Definition: ISDOpcodes.h:1273
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:903
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition: ISDOpcodes.h:229
@ ATOMIC_LOAD_XOR
Definition: ISDOpcodes.h:1274
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
Definition: ISDOpcodes.h:939
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
Definition: ISDOpcodes.h:1406
@ ATOMIC_LOAD_FADD
Definition: ISDOpcodes.h:1280
@ GlobalTLSAddress
Definition: ISDOpcodes.h:79
@ SRCVALUE
SRCVALUE - This is a node type that holds a Value* that is used to make reference to a value in the L...
Definition: ISDOpcodes.h:1173
@ FrameIndex
Definition: ISDOpcodes.h:80
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
Definition: ISDOpcodes.h:1108
@ SIGN_EXTEND
Conversion operators.
Definition: ISDOpcodes.h:774
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition: ISDOpcodes.h:662
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition: ISDOpcodes.h:620
@ TargetExternalSymbol
Definition: ISDOpcodes.h:169
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
Definition: ISDOpcodes.h:1358
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition: ISDOpcodes.h:722
@ TargetJumpTable
Definition: ISDOpcodes.h:167
@ TargetIndex
TargetIndex - Like a constant pool entry, but with completely target-dependent semantics.
Definition: ISDOpcodes.h:177
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
Definition: ISDOpcodes.h:1227
@ VECREDUCE_FMIN
Definition: ISDOpcodes.h:1362
@ 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:758
@ FNEG
Perform various unary floating-point operations inspired by libm.
Definition: ISDOpcodes.h:930
@ ATOMIC_LOAD_FSUB
Definition: ISDOpcodes.h:1281
@ BR_CC
BR_CC - Conditional branch.
Definition: ISDOpcodes.h:1083
@ SSUBO
Same for subtraction.
Definition: ISDOpcodes.h:327
@ ATOMIC_LOAD_MIN
Definition: ISDOpcodes.h:1276
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition: ISDOpcodes.h:646
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition: ISDOpcodes.h:500
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition: ISDOpcodes.h:349
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition: ISDOpcodes.h:727
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
Definition: ISDOpcodes.h:1243
@ UNDEF
UNDEF - An undefined node.
Definition: ISDOpcodes.h:211
@ VECREDUCE_UMAX
Definition: ISDOpcodes.h:1377
@ RegisterMask
Definition: ISDOpcodes.h:75
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition: ISDOpcodes.h:222
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition: ISDOpcodes.h:627
@ AssertAlign
AssertAlign - These nodes record if a register contains a value that has a known alignment and the tr...
Definition: ISDOpcodes.h:68
@ ATOMIC_LOAD_FMIN
Definition: ISDOpcodes.h:1283
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition: ISDOpcodes.h:323
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition: ISDOpcodes.h:164
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
Definition: ISDOpcodes.h:1370
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition: ISDOpcodes.h:651
@ SHL
Shift and rotation operations.
Definition: ISDOpcodes.h:705
@ ATOMIC_LOAD_CLR
Definition: ISDOpcodes.h:1272
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition: ISDOpcodes.h:600
@ ATOMIC_LOAD_AND
Definition: ISDOpcodes.h:1271
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition: ISDOpcodes.h:573
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
Definition: ISDOpcodes.h:984
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition: ISDOpcodes.h:47
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition: ISDOpcodes.h:535
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:780
@ TargetConstantFP
Definition: ISDOpcodes.h:159
@ FP_TO_UINT_SAT
Definition: ISDOpcodes.h:856
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition: ISDOpcodes.h:742
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
Definition: ISDOpcodes.h:1335
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
Definition: ISDOpcodes.h:1254
@ ATOMIC_LOAD_UMAX
Definition: ISDOpcodes.h:1279
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
Definition: ISDOpcodes.h:971
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition: ISDOpcodes.h:359
@ SMULO
Same for multiplication.
Definition: ISDOpcodes.h:331
@ TargetFrameIndex
Definition: ISDOpcodes.h:166
@ ConstantPool
Definition: ISDOpcodes.h:82
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition: ISDOpcodes.h:809
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition: ISDOpcodes.h:798
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition: ISDOpcodes.h:674
@ LIFETIME_START
This corresponds to the llvm.lifetime.
Definition: ISDOpcodes.h:1310
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:888
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition: ISDOpcodes.h:736
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:303
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
Definition: ISDOpcodes.h:1197
@ VECREDUCE_UMIN
Definition: ISDOpcodes.h:1378
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
Definition: ISDOpcodes.h:922
@ ATOMIC_LOAD_UDEC_WRAP
Definition: ISDOpcodes.h:1285
@ ATOMIC_LOAD_ADD
Definition: ISDOpcodes.h:1269
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition: ISDOpcodes.h:465
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
Definition: ISDOpcodes.h:990
@ ATOMIC_LOAD_SUB
Definition: ISDOpcodes.h:1270
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:836
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition: ISDOpcodes.h:158
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:470
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:680
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition: ISDOpcodes.h:184
@ GET_FPENV_MEM
Gets the current floating-point environment.
Definition: ISDOpcodes.h:1013
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
Definition: ISDOpcodes.h:1330
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition: ISDOpcodes.h:657
@ VECREDUCE_FMUL
Definition: ISDOpcodes.h:1359
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:279
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition: ISDOpcodes.h:636
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition: ISDOpcodes.h:524
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition: ISDOpcodes.h:52
@ VECTOR_SPLICE
VECTOR_SPLICE(VEC1, VEC2, IMM) - Returns a subvector of the same type as VEC1/VEC2 from CONCAT_VECTOR...
Definition: ISDOpcodes.h:612
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
Definition: ISDOpcodes.h:1268
@ ExternalSymbol
Definition: ISDOpcodes.h:83
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
Definition: ISDOpcodes.h:944
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition: ISDOpcodes.h:869
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition: ISDOpcodes.h:831
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition: ISDOpcodes.h:907
@ 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:855
@ VECREDUCE_FMINIMUM
Definition: ISDOpcodes.h:1366
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:786
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
Definition: ISDOpcodes.h:1152
@ BlockAddress
Definition: ISDOpcodes.h:84
@ VECREDUCE_SEQ_FMUL
Definition: ISDOpcodes.h:1346
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition: ISDOpcodes.h:763
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition: ISDOpcodes.h:61
@ ATOMIC_LOAD_UINC_WRAP
Definition: ISDOpcodes.h:1284
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition: ISDOpcodes.h:493
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition: ISDOpcodes.h:340
@ AssertZext
Definition: ISDOpcodes.h:62
@ SET_FPENV_MEM
Sets the current floating point environment.
Definition: ISDOpcodes.h:1018
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:313
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition: ISDOpcodes.h:192
@ TargetGlobalTLSAddress
Definition: ISDOpcodes.h:165
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition: ISDOpcodes.h:515
unsigned getVPForBaseOpcode(unsigned Opcode)
Translate this non-VP Opcode to its corresponding VP Opcode.
bool isBuildVectorOfConstantSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantSDNode or undef.
NodeType getExtForLoadExtType(bool IsFP, LoadExtType)
bool matchUnaryPredicate(SDValue Op, std::function< bool(ConstantSDNode *)> Match, bool AllowUndefs=false)
Hook for matching ConstantSDNode predicate.
bool isZEXTLoad(const SDNode *N)
Returns true if the specified node is a ZEXTLOAD.
bool matchUnaryFpPredicate(SDValue Op, std::function< bool(ConstantFPSDNode *)> Match, bool AllowUndefs=false)
Hook for matching ConstantFPSDNode predicate.
static const int FIRST_TARGET_MEMORY_OPCODE
FIRST_TARGET_MEMORY_OPCODE - Target-specific pre-isel operations which do not reference a specific me...
Definition: ISDOpcodes.h:1418
bool isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are 0 o...
bool isVectorShrinkable(const SDNode *N, unsigned NewEltSize, bool Signed)
Returns true if the specified node is a vector where all elements can be truncated to the specified e...
bool matchUnaryPredicateImpl(SDValue Op, std::function< bool(ConstNodeType *)> Match, bool AllowUndefs=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant BUI...
bool isVPBinaryOp(unsigned Opcode)
Whether this is a vector-predicated binary operation opcode.
CondCode getSetCCInverse(CondCode Operation, EVT Type)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
std::optional< unsigned > getBaseOpcodeForVP(unsigned Opcode, bool hasFPExcept)
Translate this VP Opcode to its corresponding non-VP Opcode.
bool isTrueWhenEqual(CondCode Cond)
Return true if the specified condition returns true if the two operands to the condition are equal.
Definition: ISDOpcodes.h:1587
std::optional< unsigned > getVPMaskIdx(unsigned Opcode)
The operand position of the vector mask.
unsigned getUnorderedFlavor(CondCode Cond)
This function returns 0 if the condition is always false if an operand is a NaN, 1 if the condition i...
Definition: ISDOpcodes.h:1592
std::optional< unsigned > getVPExplicitVectorLengthIdx(unsigned Opcode)
The operand position of the explicit vector length parameter.
bool isEXTLoad(const SDNode *N)
Returns true if the specified node is a EXTLOAD.
bool allOperandsUndef(const SDNode *N)
Return true if the node has at least one operand and all operands of the specified node are ISD::UNDE...
bool isFreezeUndef(const SDNode *N)
Return true if the specified node is FREEZE(UNDEF).
CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y).
MemIndexType
MemIndexType enum - This enum defines how to interpret MGATHER/SCATTER's index parameter when calcula...
Definition: ISDOpcodes.h:1491
bool isBuildVectorAllZeros(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are 0 or undef.
bool isConstantSplatVector(const SDNode *N, APInt &SplatValue)
Node predicates.
bool matchBinaryPredicate(SDValue LHS, SDValue RHS, std::function< bool(ConstantSDNode *, ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTypeMismatch=false)
Attempt to match a binary predicate against a pair of scalar/splat constants or every element of a pa...
bool isVPReduction(unsigned Opcode)
Whether this is a vector-predicated reduction opcode.
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
Definition: ISDOpcodes.h:1478
bool isBuildVectorOfConstantFPSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantFPSDNode or undef.
bool isSEXTLoad(const SDNode *N)
Returns true if the specified node is a SEXTLOAD.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
Definition: ISDOpcodes.h:1529
bool isBuildVectorAllOnes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are ~0 or undef.
NodeType getVecReduceBaseOpcode(unsigned VecReduceOpcode)
Get underlying scalar opcode for VECREDUCE opcode.
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
Definition: ISDOpcodes.h:1509
bool isVPOpcode(unsigned Opcode)
Whether this is a vector-predicated Opcode.
CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical OR between different comparisons of identical values: ((X op1 Y) | (X ...
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
Definition: PatternMatch.h:848
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
Libcall
RTLIB::Libcall enum - This enum defines all of the runtime library calls the backend can emit.
Libcall getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMCPY_ELEMENT_UNORDERED_ATOMIC - Return MEMCPY_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
Libcall getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMSET_ELEMENT_UNORDERED_ATOMIC - Return MEMSET_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
Libcall getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMMOVE_ELEMENT_UNORDERED_ATOMIC - Return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_* value for the given e...
bool sd_match(SDNode *N, const SelectionDAG *DAG, Pattern &&P)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:450
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition: Dwarf.h:146
std::lock_guard< SmartMutex< mt_only > > SmartScopedLock
Definition: Mutex.h:69
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition: MathExtras.h:337
@ Offset
Definition: DWP.cpp:456
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:361
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition: Analysis.cpp:233
SDValue peekThroughExtractSubvectors(SDValue V)
Return the non-extracted vector source operand of V if it exists.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1722
bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:239
bool isAllOnesOrAllOnesSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant -1 integer or a splatted vector of a constant -1 integer (with...
Definition: Utils.cpp:1525
SDValue getBitwiseNotOperand(SDValue V, SDValue Mask, bool AllowUndefs)
If V is a bitwise not, returns the inverted operand.
SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are are tuples (A,...
Definition: STLExtras.h:2406
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition: bit.h:307
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
bool getAlign(const Function &F, unsigned index, unsigned &align)
bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition: bit.h:317
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition: APFloat.h:1436
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2073
bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:656
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:1507
bool isMinSignedConstant(SDValue V)
Returns true if V is a constant min signed integer value.
ConstantFPSDNode * isConstOrConstSplatFP(SDValue N, bool AllowUndefs=false)
Returns the SDNode if it is a constant splat BuildVector or constant float.
ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition: APFloat.h:1373
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1729
bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2019 maximumNumber semantics.
Definition: APFloat.h:1410
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:324
bool isBitwiseNot(SDValue V, bool AllowUndefs=false)
Returns true if V is a bitwise not operation.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:275
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
void checkForCycles(const SelectionDAG *DAG, bool force=false)
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
SDValue peekThroughTruncates(SDValue V)
Return the non-truncated source operand of V if it exists.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1736
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
SDValue peekThroughOneUseBitcasts(SDValue V)
Return the non-bitcasted and one-use source operand of V if it exists.
CodeGenOptLevel
Code generation optimization level.
Definition: CodeGen.h:54
bool isOneOrOneSplat(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Other
Any other memory.
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2019 minimumNumber semantics.
Definition: APFloat.h:1396
@ Mul
Product of integers.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
DWARFExpression::Operation Op
ConstantSDNode * isConstOrConstSplat(SDValue N, bool AllowUndefs=false, bool AllowTruncation=false)
Returns the SDNode if it is a constant splat BuildVector or constant int.
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1824
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
bool isOneConstant(SDValue V)
Returns true if V is a constant integer one.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1879
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition: Alignment.h:212
bool isNullFPConstant(SDValue V)
Returns true if V is an FP constant with a value of positive zero.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition: MathExtras.h:465
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208
void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition: APFloat.h:1423
bool isNeutralConstant(unsigned Opc, SDNodeFlags Flags, SDValue V, unsigned OperandNo)
Returns true if V is a neutral element of Opc with Flags.
bool isAllOnesConstant(SDValue V)
Returns true if V is an integer constant with all bits set.
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition: MathExtras.h:360
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
#define N
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:760
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition: Metadata.h:780
MDNode * TBAA
The tag for type-based alias analysis.
Definition: Metadata.h:777
static const fltSemantics & IEEEsingle() LLVM_READNONE
Definition: APFloat.cpp:249
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition: APFloat.h:220
static constexpr roundingMode rmTowardNegative
Definition: APFloat.h:233
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:230
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:234
static const fltSemantics & IEEEquad() LLVM_READNONE
Definition: APFloat.cpp:251
static const fltSemantics & IEEEdouble() LLVM_READNONE
Definition: APFloat.cpp:250
static const fltSemantics & IEEEhalf() LLVM_READNONE
Definition: APFloat.cpp:247
static constexpr roundingMode rmTowardPositive
Definition: APFloat.h:232
static const fltSemantics & BFloat() LLVM_READNONE
Definition: APFloat.cpp:248
opStatus
IEEE-754R 7: Default exception handling.
Definition: APFloat.h:246
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Align previous() const
Definition: Alignment.h:88
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
void move(uint64_t Delta)
Moves the Offset and adjusts Length accordingly.
const ConstantDataArray * Array
ConstantDataArray pointer.
Extended Value Type.
Definition: ValueTypes.h:34
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition: ValueTypes.h:380
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition: ValueTypes.h:136
intptr_t getRawBits() const
Definition: ValueTypes.h:495
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition: ValueTypes.h:73
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition: ValueTypes.h:274
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition: ValueTypes.h:290
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition: ValueTypes.h:146
ElementCount getVectorElementCount() const
Definition: ValueTypes.h:340
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:358
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition: ValueTypes.h:349
uint64_t getScalarSizeInBits() const
Definition: ValueTypes.h:370
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:306
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition: ValueTypes.h:64
bool isFixedLengthVector() const
Definition: ValueTypes.h:177
bool isVector() const
Return true if this is a vector value type.
Definition: ValueTypes.h:167
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition: ValueTypes.h:313
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition: ValueTypes.h:282
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition: ValueTypes.h:246
Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:202
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition: ValueTypes.h:173
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition: ValueTypes.h:318
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition: ValueTypes.h:141
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition: ValueTypes.h:326
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition: ValueTypes.h:298
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition: ValueTypes.h:438
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition: ValueTypes.h:151
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition: KnownBits.h:297
KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
Definition: KnownBits.cpp:155
static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
Definition: KnownBits.cpp:884
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition: KnownBits.h:251
static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
Definition: KnownBits.cpp:208
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:104
bool isZero() const
Returns true if value is all zero.
Definition: KnownBits.h:77
void makeNonNegative()
Make this value non-negative.
Definition: KnownBits.h:120
static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
Definition: KnownBits.cpp:773
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:238
static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
Definition: KnownBits.cpp:434
static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
Definition: KnownBits.cpp:1030
bool isUnknown() const
Returns true if we don't know any bits.
Definition: KnownBits.h:63
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:270
static std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
Definition: KnownBits.cpp:502
void makeNegative()
Make this value negative.
Definition: KnownBits.h:115
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition: KnownBits.h:157
KnownBits byteSwap() const
Definition: KnownBits.h:457
bool hasConflict() const
Returns true if there is conflicting information.
Definition: KnownBits.h:47
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:285
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition: KnownBits.h:89
KnownBits reverseBits() const
Definition: KnownBits.h:461
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition: KnownBits.h:229
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:40
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
Definition: KnownBits.cpp:184
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition: KnownBits.h:168
void resetAll()
Resets the known state of all bits.
Definition: KnownBits.h:71
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:317
static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
Definition: KnownBits.cpp:376
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition: KnownBits.h:107
static KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
Definition: KnownBits.cpp:234
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition: KnownBits.h:221
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition: KnownBits.h:307
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition: KnownBits.h:176
static KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, const KnownBits &Borrow)
Compute known bits results from subtracting RHS from LHS with 1-bit Borrow.
Definition: KnownBits.cpp:143
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition: KnownBits.h:192
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:141
static KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
Definition: KnownBits.cpp:253
static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
Definition: KnownBits.cpp:221
static KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
Definition: KnownBits.cpp:875
static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition: KnownBits.cpp:1049
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition: KnownBits.cpp:988
static KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition: KnownBits.cpp:57
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition: KnownBits.h:110
static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
Definition: KnownBits.cpp:929
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition: KnownBits.h:328
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:101
static KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry)
Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
Definition: KnownBits.cpp:50
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition: KnownBits.h:276
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition: KnownBits.h:215
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:777
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:163
KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
Definition: KnownBits.cpp:556
static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
Definition: KnownBits.cpp:291
static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
Definition: KnownBits.cpp:202
This class contains a discriminated union of information about pointers in memory operands,...
bool isDereferenceable(unsigned Size, LLVMContext &C, const DataLayout &DL) const
Return true if memory region [V, V+Offset+Size) is known to be dereferenceable.
unsigned getAddrSpace() const
Return the LLVM IR address space number that this pointer points into.
PointerUnion< const Value *, const PseudoSourceValue * > V
This is the IR pointer value for the access, or it is null if unknown.
MachinePointerInfo getWithOffset(int64_t O) const
static MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
static MemOp Set(uint64_t Size, bool DstAlignCanChange, Align DstAlign, bool IsZeroMemset, bool IsVolatile)
static MemOp Copy(uint64_t Size, bool DstAlignCanChange, Align DstAlign, Align SrcAlign, bool IsVolatile, bool MemcpyStrSrc=false)
These are IR-level optimization flags that may be propagated to SDNodes.
void intersectWith(const SDNodeFlags Flags)
Clear any flags in this flag set that aren't also set in Flags.
bool hasNonNeg() const
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
unsigned int NumVTs
Clients of various APIs that cause global effects on the DAG can optionally implement this interface.
Definition: SelectionDAG.h:306
DAGUpdateListener *const Next
Definition: SelectionDAG.h:307
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)