SelectionDAGNodes.h 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/CodeGen/SelectionDAGNodes.h - SelectionDAG Nodes ----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares the SDNode class and derived classes, which are used to
  15. // represent the nodes and operations present in a SelectionDAG. These nodes
  16. // and operations are machine code level operations, with some similarities to
  17. // the GCC RTL representation.
  18. //
  19. // Clients should include the SelectionDAG.h file instead of this file directly.
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #ifndef LLVM_CODEGEN_SELECTIONDAGNODES_H
  23. #define LLVM_CODEGEN_SELECTIONDAGNODES_H
  24. #include "llvm/ADT/APFloat.h"
  25. #include "llvm/ADT/ArrayRef.h"
  26. #include "llvm/ADT/BitVector.h"
  27. #include "llvm/ADT/FoldingSet.h"
  28. #include "llvm/ADT/GraphTraits.h"
  29. #include "llvm/ADT/SmallPtrSet.h"
  30. #include "llvm/ADT/SmallVector.h"
  31. #include "llvm/ADT/ilist_node.h"
  32. #include "llvm/ADT/iterator.h"
  33. #include "llvm/ADT/iterator_range.h"
  34. #include "llvm/CodeGen/ISDOpcodes.h"
  35. #include "llvm/CodeGen/MachineMemOperand.h"
  36. #include "llvm/CodeGen/Register.h"
  37. #include "llvm/CodeGen/ValueTypes.h"
  38. #include "llvm/IR/Constants.h"
  39. #include "llvm/IR/DebugLoc.h"
  40. #include "llvm/IR/Instruction.h"
  41. #include "llvm/IR/Instructions.h"
  42. #include "llvm/IR/Metadata.h"
  43. #include "llvm/IR/Operator.h"
  44. #include "llvm/Support/AlignOf.h"
  45. #include "llvm/Support/AtomicOrdering.h"
  46. #include "llvm/Support/Casting.h"
  47. #include "llvm/Support/ErrorHandling.h"
  48. #include "llvm/Support/MachineValueType.h"
  49. #include "llvm/Support/TypeSize.h"
  50. #include <algorithm>
  51. #include <cassert>
  52. #include <climits>
  53. #include <cstddef>
  54. #include <cstdint>
  55. #include <cstring>
  56. #include <iterator>
  57. #include <string>
  58. #include <tuple>
  59. #include <utility>
  60. namespace llvm {
  61. class APInt;
  62. class Constant;
  63. class GlobalValue;
  64. class MachineBasicBlock;
  65. class MachineConstantPoolValue;
  66. class MCSymbol;
  67. class raw_ostream;
  68. class SDNode;
  69. class SelectionDAG;
  70. class Type;
  71. class Value;
  72. void checkForCycles(const SDNode *N, const SelectionDAG *DAG = nullptr,
  73. bool force = false);
  74. /// This represents a list of ValueType's that has been intern'd by
  75. /// a SelectionDAG. Instances of this simple value class are returned by
  76. /// SelectionDAG::getVTList(...).
  77. ///
  78. struct SDVTList {
  79. const EVT *VTs;
  80. unsigned int NumVTs;
  81. };
  82. namespace ISD {
  83. /// Node predicates
  84. /// If N is a BUILD_VECTOR or SPLAT_VECTOR node whose elements are all the
  85. /// same constant or undefined, return true and return the constant value in
  86. /// \p SplatValue.
  87. bool isConstantSplatVector(const SDNode *N, APInt &SplatValue);
  88. /// Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where
  89. /// all of the elements are ~0 or undef. If \p BuildVectorOnly is set to
  90. /// true, it only checks BUILD_VECTOR.
  91. bool isConstantSplatVectorAllOnes(const SDNode *N,
  92. bool BuildVectorOnly = false);
  93. /// Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where
  94. /// all of the elements are 0 or undef. If \p BuildVectorOnly is set to true, it
  95. /// only checks BUILD_VECTOR.
  96. bool isConstantSplatVectorAllZeros(const SDNode *N,
  97. bool BuildVectorOnly = false);
  98. /// Return true if the specified node is a BUILD_VECTOR where all of the
  99. /// elements are ~0 or undef.
  100. bool isBuildVectorAllOnes(const SDNode *N);
  101. /// Return true if the specified node is a BUILD_VECTOR where all of the
  102. /// elements are 0 or undef.
  103. bool isBuildVectorAllZeros(const SDNode *N);
  104. /// Return true if the specified node is a BUILD_VECTOR node of all
  105. /// ConstantSDNode or undef.
  106. bool isBuildVectorOfConstantSDNodes(const SDNode *N);
  107. /// Return true if the specified node is a BUILD_VECTOR node of all
  108. /// ConstantFPSDNode or undef.
  109. bool isBuildVectorOfConstantFPSDNodes(const SDNode *N);
  110. /// Returns true if the specified node is a vector where all elements can
  111. /// be truncated to the specified element size without a loss in meaning.
  112. bool isVectorShrinkable(const SDNode *N, unsigned NewEltSize, bool Signed);
  113. /// Return true if the node has at least one operand and all operands of the
  114. /// specified node are ISD::UNDEF.
  115. bool allOperandsUndef(const SDNode *N);
  116. /// Return true if the specified node is FREEZE(UNDEF).
  117. bool isFreezeUndef(const SDNode *N);
  118. } // end namespace ISD
  119. //===----------------------------------------------------------------------===//
  120. /// Unlike LLVM values, Selection DAG nodes may return multiple
  121. /// values as the result of a computation. Many nodes return multiple values,
  122. /// from loads (which define a token and a return value) to ADDC (which returns
  123. /// a result and a carry value), to calls (which may return an arbitrary number
  124. /// of values).
  125. ///
  126. /// As such, each use of a SelectionDAG computation must indicate the node that
  127. /// computes it as well as which return value to use from that node. This pair
  128. /// of information is represented with the SDValue value type.
  129. ///
  130. class SDValue {
  131. friend struct DenseMapInfo<SDValue>;
  132. SDNode *Node = nullptr; // The node defining the value we are using.
  133. unsigned ResNo = 0; // Which return value of the node we are using.
  134. public:
  135. SDValue() = default;
  136. SDValue(SDNode *node, unsigned resno);
  137. /// get the index which selects a specific result in the SDNode
  138. unsigned getResNo() const { return ResNo; }
  139. /// get the SDNode which holds the desired result
  140. SDNode *getNode() const { return Node; }
  141. /// set the SDNode
  142. void setNode(SDNode *N) { Node = N; }
  143. inline SDNode *operator->() const { return Node; }
  144. bool operator==(const SDValue &O) const {
  145. return Node == O.Node && ResNo == O.ResNo;
  146. }
  147. bool operator!=(const SDValue &O) const {
  148. return !operator==(O);
  149. }
  150. bool operator<(const SDValue &O) const {
  151. return std::tie(Node, ResNo) < std::tie(O.Node, O.ResNo);
  152. }
  153. explicit operator bool() const {
  154. return Node != nullptr;
  155. }
  156. SDValue getValue(unsigned R) const {
  157. return SDValue(Node, R);
  158. }
  159. /// Return true if this node is an operand of N.
  160. bool isOperandOf(const SDNode *N) const;
  161. /// Return the ValueType of the referenced return value.
  162. inline EVT getValueType() const;
  163. /// Return the simple ValueType of the referenced return value.
  164. MVT getSimpleValueType() const {
  165. return getValueType().getSimpleVT();
  166. }
  167. /// Returns the size of the value in bits.
  168. ///
  169. /// If the value type is a scalable vector type, the scalable property will
  170. /// be set and the runtime size will be a positive integer multiple of the
  171. /// base size.
  172. TypeSize getValueSizeInBits() const {
  173. return getValueType().getSizeInBits();
  174. }
  175. uint64_t getScalarValueSizeInBits() const {
  176. return getValueType().getScalarType().getFixedSizeInBits();
  177. }
  178. // Forwarding methods - These forward to the corresponding methods in SDNode.
  179. inline unsigned getOpcode() const;
  180. inline unsigned getNumOperands() const;
  181. inline const SDValue &getOperand(unsigned i) const;
  182. inline uint64_t getConstantOperandVal(unsigned i) const;
  183. inline const APInt &getConstantOperandAPInt(unsigned i) const;
  184. inline bool isTargetMemoryOpcode() const;
  185. inline bool isTargetOpcode() const;
  186. inline bool isMachineOpcode() const;
  187. inline bool isUndef() const;
  188. inline unsigned getMachineOpcode() const;
  189. inline const DebugLoc &getDebugLoc() const;
  190. inline void dump() const;
  191. inline void dump(const SelectionDAG *G) const;
  192. inline void dumpr() const;
  193. inline void dumpr(const SelectionDAG *G) const;
  194. /// Return true if this operand (which must be a chain) reaches the
  195. /// specified operand without crossing any side-effecting instructions.
  196. /// In practice, this looks through token factors and non-volatile loads.
  197. /// In order to remain efficient, this only
  198. /// looks a couple of nodes in, it does not do an exhaustive search.
  199. bool reachesChainWithoutSideEffects(SDValue Dest,
  200. unsigned Depth = 2) const;
  201. /// Return true if there are no nodes using value ResNo of Node.
  202. inline bool use_empty() const;
  203. /// Return true if there is exactly one node using value ResNo of Node.
  204. inline bool hasOneUse() const;
  205. };
  206. template<> struct DenseMapInfo<SDValue> {
  207. static inline SDValue getEmptyKey() {
  208. SDValue V;
  209. V.ResNo = -1U;
  210. return V;
  211. }
  212. static inline SDValue getTombstoneKey() {
  213. SDValue V;
  214. V.ResNo = -2U;
  215. return V;
  216. }
  217. static unsigned getHashValue(const SDValue &Val) {
  218. return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
  219. (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
  220. }
  221. static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
  222. return LHS == RHS;
  223. }
  224. };
  225. /// Allow casting operators to work directly on
  226. /// SDValues as if they were SDNode*'s.
  227. template<> struct simplify_type<SDValue> {
  228. using SimpleType = SDNode *;
  229. static SimpleType getSimplifiedValue(SDValue &Val) {
  230. return Val.getNode();
  231. }
  232. };
  233. template<> struct simplify_type<const SDValue> {
  234. using SimpleType = /*const*/ SDNode *;
  235. static SimpleType getSimplifiedValue(const SDValue &Val) {
  236. return Val.getNode();
  237. }
  238. };
  239. /// Represents a use of a SDNode. This class holds an SDValue,
  240. /// which records the SDNode being used and the result number, a
  241. /// pointer to the SDNode using the value, and Next and Prev pointers,
  242. /// which link together all the uses of an SDNode.
  243. ///
  244. class SDUse {
  245. /// Val - The value being used.
  246. SDValue Val;
  247. /// User - The user of this value.
  248. SDNode *User = nullptr;
  249. /// Prev, Next - Pointers to the uses list of the SDNode referred by
  250. /// this operand.
  251. SDUse **Prev = nullptr;
  252. SDUse *Next = nullptr;
  253. public:
  254. SDUse() = default;
  255. SDUse(const SDUse &U) = delete;
  256. SDUse &operator=(const SDUse &) = delete;
  257. /// Normally SDUse will just implicitly convert to an SDValue that it holds.
  258. operator const SDValue&() const { return Val; }
  259. /// If implicit conversion to SDValue doesn't work, the get() method returns
  260. /// the SDValue.
  261. const SDValue &get() const { return Val; }
  262. /// This returns the SDNode that contains this Use.
  263. SDNode *getUser() { return User; }
  264. const SDNode *getUser() const { return User; }
  265. /// Get the next SDUse in the use list.
  266. SDUse *getNext() const { return Next; }
  267. /// Convenience function for get().getNode().
  268. SDNode *getNode() const { return Val.getNode(); }
  269. /// Convenience function for get().getResNo().
  270. unsigned getResNo() const { return Val.getResNo(); }
  271. /// Convenience function for get().getValueType().
  272. EVT getValueType() const { return Val.getValueType(); }
  273. /// Convenience function for get().operator==
  274. bool operator==(const SDValue &V) const {
  275. return Val == V;
  276. }
  277. /// Convenience function for get().operator!=
  278. bool operator!=(const SDValue &V) const {
  279. return Val != V;
  280. }
  281. /// Convenience function for get().operator<
  282. bool operator<(const SDValue &V) const {
  283. return Val < V;
  284. }
  285. private:
  286. friend class SelectionDAG;
  287. friend class SDNode;
  288. // TODO: unfriend HandleSDNode once we fix its operand handling.
  289. friend class HandleSDNode;
  290. void setUser(SDNode *p) { User = p; }
  291. /// Remove this use from its existing use list, assign it the
  292. /// given value, and add it to the new value's node's use list.
  293. inline void set(const SDValue &V);
  294. /// Like set, but only supports initializing a newly-allocated
  295. /// SDUse with a non-null value.
  296. inline void setInitial(const SDValue &V);
  297. /// Like set, but only sets the Node portion of the value,
  298. /// leaving the ResNo portion unmodified.
  299. inline void setNode(SDNode *N);
  300. void addToList(SDUse **List) {
  301. Next = *List;
  302. if (Next) Next->Prev = &Next;
  303. Prev = List;
  304. *List = this;
  305. }
  306. void removeFromList() {
  307. *Prev = Next;
  308. if (Next) Next->Prev = Prev;
  309. }
  310. };
  311. /// simplify_type specializations - Allow casting operators to work directly on
  312. /// SDValues as if they were SDNode*'s.
  313. template<> struct simplify_type<SDUse> {
  314. using SimpleType = SDNode *;
  315. static SimpleType getSimplifiedValue(SDUse &Val) {
  316. return Val.getNode();
  317. }
  318. };
  319. /// These are IR-level optimization flags that may be propagated to SDNodes.
  320. /// TODO: This data structure should be shared by the IR optimizer and the
  321. /// the backend.
  322. struct SDNodeFlags {
  323. private:
  324. bool NoUnsignedWrap : 1;
  325. bool NoSignedWrap : 1;
  326. bool Exact : 1;
  327. bool NoNaNs : 1;
  328. bool NoInfs : 1;
  329. bool NoSignedZeros : 1;
  330. bool AllowReciprocal : 1;
  331. bool AllowContract : 1;
  332. bool ApproximateFuncs : 1;
  333. bool AllowReassociation : 1;
  334. // We assume instructions do not raise floating-point exceptions by default,
  335. // and only those marked explicitly may do so. We could choose to represent
  336. // this via a positive "FPExcept" flags like on the MI level, but having a
  337. // negative "NoFPExcept" flag here (that defaults to true) makes the flag
  338. // intersection logic more straightforward.
  339. bool NoFPExcept : 1;
  340. public:
  341. /// Default constructor turns off all optimization flags.
  342. SDNodeFlags()
  343. : NoUnsignedWrap(false), NoSignedWrap(false), Exact(false), NoNaNs(false),
  344. NoInfs(false), NoSignedZeros(false), AllowReciprocal(false),
  345. AllowContract(false), ApproximateFuncs(false),
  346. AllowReassociation(false), NoFPExcept(false) {}
  347. /// Propagate the fast-math-flags from an IR FPMathOperator.
  348. void copyFMF(const FPMathOperator &FPMO) {
  349. setNoNaNs(FPMO.hasNoNaNs());
  350. setNoInfs(FPMO.hasNoInfs());
  351. setNoSignedZeros(FPMO.hasNoSignedZeros());
  352. setAllowReciprocal(FPMO.hasAllowReciprocal());
  353. setAllowContract(FPMO.hasAllowContract());
  354. setApproximateFuncs(FPMO.hasApproxFunc());
  355. setAllowReassociation(FPMO.hasAllowReassoc());
  356. }
  357. // These are mutators for each flag.
  358. void setNoUnsignedWrap(bool b) { NoUnsignedWrap = b; }
  359. void setNoSignedWrap(bool b) { NoSignedWrap = b; }
  360. void setExact(bool b) { Exact = b; }
  361. void setNoNaNs(bool b) { NoNaNs = b; }
  362. void setNoInfs(bool b) { NoInfs = b; }
  363. void setNoSignedZeros(bool b) { NoSignedZeros = b; }
  364. void setAllowReciprocal(bool b) { AllowReciprocal = b; }
  365. void setAllowContract(bool b) { AllowContract = b; }
  366. void setApproximateFuncs(bool b) { ApproximateFuncs = b; }
  367. void setAllowReassociation(bool b) { AllowReassociation = b; }
  368. void setNoFPExcept(bool b) { NoFPExcept = b; }
  369. // These are accessors for each flag.
  370. bool hasNoUnsignedWrap() const { return NoUnsignedWrap; }
  371. bool hasNoSignedWrap() const { return NoSignedWrap; }
  372. bool hasExact() const { return Exact; }
  373. bool hasNoNaNs() const { return NoNaNs; }
  374. bool hasNoInfs() const { return NoInfs; }
  375. bool hasNoSignedZeros() const { return NoSignedZeros; }
  376. bool hasAllowReciprocal() const { return AllowReciprocal; }
  377. bool hasAllowContract() const { return AllowContract; }
  378. bool hasApproximateFuncs() const { return ApproximateFuncs; }
  379. bool hasAllowReassociation() const { return AllowReassociation; }
  380. bool hasNoFPExcept() const { return NoFPExcept; }
  381. /// Clear any flags in this flag set that aren't also set in Flags. All
  382. /// flags will be cleared if Flags are undefined.
  383. void intersectWith(const SDNodeFlags Flags) {
  384. NoUnsignedWrap &= Flags.NoUnsignedWrap;
  385. NoSignedWrap &= Flags.NoSignedWrap;
  386. Exact &= Flags.Exact;
  387. NoNaNs &= Flags.NoNaNs;
  388. NoInfs &= Flags.NoInfs;
  389. NoSignedZeros &= Flags.NoSignedZeros;
  390. AllowReciprocal &= Flags.AllowReciprocal;
  391. AllowContract &= Flags.AllowContract;
  392. ApproximateFuncs &= Flags.ApproximateFuncs;
  393. AllowReassociation &= Flags.AllowReassociation;
  394. NoFPExcept &= Flags.NoFPExcept;
  395. }
  396. };
  397. /// Represents one node in the SelectionDAG.
  398. ///
  399. class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
  400. private:
  401. /// The operation that this node performs.
  402. int32_t NodeType;
  403. public:
  404. /// Unique and persistent id per SDNode in the DAG. Used for debug printing.
  405. /// We do not place that under `#if LLVM_ENABLE_ABI_BREAKING_CHECKS`
  406. /// intentionally because it adds unneeded complexity without noticeable
  407. /// benefits (see discussion with @thakis in D120714).
  408. uint16_t PersistentId;
  409. protected:
  410. // We define a set of mini-helper classes to help us interpret the bits in our
  411. // SubclassData. These are designed to fit within a uint16_t so they pack
  412. // with PersistentId.
  413. #if defined(_AIX) && (!defined(__GNUC__) || defined(__clang__))
  414. // Except for GCC; by default, AIX compilers store bit-fields in 4-byte words
  415. // and give the `pack` pragma push semantics.
  416. #define BEGIN_TWO_BYTE_PACK() _Pragma("pack(2)")
  417. #define END_TWO_BYTE_PACK() _Pragma("pack(pop)")
  418. #else
  419. #define BEGIN_TWO_BYTE_PACK()
  420. #define END_TWO_BYTE_PACK()
  421. #endif
  422. BEGIN_TWO_BYTE_PACK()
  423. class SDNodeBitfields {
  424. friend class SDNode;
  425. friend class MemIntrinsicSDNode;
  426. friend class MemSDNode;
  427. friend class SelectionDAG;
  428. uint16_t HasDebugValue : 1;
  429. uint16_t IsMemIntrinsic : 1;
  430. uint16_t IsDivergent : 1;
  431. };
  432. enum { NumSDNodeBits = 3 };
  433. class ConstantSDNodeBitfields {
  434. friend class ConstantSDNode;
  435. uint16_t : NumSDNodeBits;
  436. uint16_t IsOpaque : 1;
  437. };
  438. class MemSDNodeBitfields {
  439. friend class MemSDNode;
  440. friend class MemIntrinsicSDNode;
  441. friend class AtomicSDNode;
  442. uint16_t : NumSDNodeBits;
  443. uint16_t IsVolatile : 1;
  444. uint16_t IsNonTemporal : 1;
  445. uint16_t IsDereferenceable : 1;
  446. uint16_t IsInvariant : 1;
  447. };
  448. enum { NumMemSDNodeBits = NumSDNodeBits + 4 };
  449. class LSBaseSDNodeBitfields {
  450. friend class LSBaseSDNode;
  451. friend class VPBaseLoadStoreSDNode;
  452. friend class MaskedLoadStoreSDNode;
  453. friend class MaskedGatherScatterSDNode;
  454. friend class VPGatherScatterSDNode;
  455. uint16_t : NumMemSDNodeBits;
  456. // This storage is shared between disparate class hierarchies to hold an
  457. // enumeration specific to the class hierarchy in use.
  458. // LSBaseSDNode => enum ISD::MemIndexedMode
  459. // VPLoadStoreBaseSDNode => enum ISD::MemIndexedMode
  460. // MaskedLoadStoreBaseSDNode => enum ISD::MemIndexedMode
  461. // VPGatherScatterSDNode => enum ISD::MemIndexType
  462. // MaskedGatherScatterSDNode => enum ISD::MemIndexType
  463. uint16_t AddressingMode : 3;
  464. };
  465. enum { NumLSBaseSDNodeBits = NumMemSDNodeBits + 3 };
  466. class LoadSDNodeBitfields {
  467. friend class LoadSDNode;
  468. friend class VPLoadSDNode;
  469. friend class VPStridedLoadSDNode;
  470. friend class MaskedLoadSDNode;
  471. friend class MaskedGatherSDNode;
  472. friend class VPGatherSDNode;
  473. uint16_t : NumLSBaseSDNodeBits;
  474. uint16_t ExtTy : 2; // enum ISD::LoadExtType
  475. uint16_t IsExpanding : 1;
  476. };
  477. class StoreSDNodeBitfields {
  478. friend class StoreSDNode;
  479. friend class VPStoreSDNode;
  480. friend class VPStridedStoreSDNode;
  481. friend class MaskedStoreSDNode;
  482. friend class MaskedScatterSDNode;
  483. friend class VPScatterSDNode;
  484. uint16_t : NumLSBaseSDNodeBits;
  485. uint16_t IsTruncating : 1;
  486. uint16_t IsCompressing : 1;
  487. };
  488. union {
  489. char RawSDNodeBits[sizeof(uint16_t)];
  490. SDNodeBitfields SDNodeBits;
  491. ConstantSDNodeBitfields ConstantSDNodeBits;
  492. MemSDNodeBitfields MemSDNodeBits;
  493. LSBaseSDNodeBitfields LSBaseSDNodeBits;
  494. LoadSDNodeBitfields LoadSDNodeBits;
  495. StoreSDNodeBitfields StoreSDNodeBits;
  496. };
  497. END_TWO_BYTE_PACK()
  498. #undef BEGIN_TWO_BYTE_PACK
  499. #undef END_TWO_BYTE_PACK
  500. // RawSDNodeBits must cover the entirety of the union. This means that all of
  501. // the union's members must have size <= RawSDNodeBits. We write the RHS as
  502. // "2" instead of sizeof(RawSDNodeBits) because MSVC can't handle the latter.
  503. static_assert(sizeof(SDNodeBitfields) <= 2, "field too wide");
  504. static_assert(sizeof(ConstantSDNodeBitfields) <= 2, "field too wide");
  505. static_assert(sizeof(MemSDNodeBitfields) <= 2, "field too wide");
  506. static_assert(sizeof(LSBaseSDNodeBitfields) <= 2, "field too wide");
  507. static_assert(sizeof(LoadSDNodeBitfields) <= 2, "field too wide");
  508. static_assert(sizeof(StoreSDNodeBitfields) <= 2, "field too wide");
  509. private:
  510. friend class SelectionDAG;
  511. // TODO: unfriend HandleSDNode once we fix its operand handling.
  512. friend class HandleSDNode;
  513. /// Unique id per SDNode in the DAG.
  514. int NodeId = -1;
  515. /// The values that are used by this operation.
  516. SDUse *OperandList = nullptr;
  517. /// The types of the values this node defines. SDNode's may
  518. /// define multiple values simultaneously.
  519. const EVT *ValueList;
  520. /// List of uses for this SDNode.
  521. SDUse *UseList = nullptr;
  522. /// The number of entries in the Operand/Value list.
  523. unsigned short NumOperands = 0;
  524. unsigned short NumValues;
  525. // The ordering of the SDNodes. It roughly corresponds to the ordering of the
  526. // original LLVM instructions.
  527. // This is used for turning off scheduling, because we'll forgo
  528. // the normal scheduling algorithms and output the instructions according to
  529. // this ordering.
  530. unsigned IROrder;
  531. /// Source line information.
  532. DebugLoc debugLoc;
  533. /// Return a pointer to the specified value type.
  534. static const EVT *getValueTypeList(EVT VT);
  535. SDNodeFlags Flags;
  536. uint32_t CFIType = 0;
  537. public:
  538. //===--------------------------------------------------------------------===//
  539. // Accessors
  540. //
  541. /// Return the SelectionDAG opcode value for this node. For
  542. /// pre-isel nodes (those for which isMachineOpcode returns false), these
  543. /// are the opcode values in the ISD and <target>ISD namespaces. For
  544. /// post-isel opcodes, see getMachineOpcode.
  545. unsigned getOpcode() const { return (unsigned)NodeType; }
  546. /// Test if this node has a target-specific opcode (in the
  547. /// \<target\>ISD namespace).
  548. bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
  549. /// Test if this node has a target-specific opcode that may raise
  550. /// FP exceptions (in the \<target\>ISD namespace and greater than
  551. /// FIRST_TARGET_STRICTFP_OPCODE). Note that all target memory
  552. /// opcode are currently automatically considered to possibly raise
  553. /// FP exceptions as well.
  554. bool isTargetStrictFPOpcode() const {
  555. return NodeType >= ISD::FIRST_TARGET_STRICTFP_OPCODE;
  556. }
  557. /// Test if this node has a target-specific
  558. /// memory-referencing opcode (in the \<target\>ISD namespace and
  559. /// greater than FIRST_TARGET_MEMORY_OPCODE).
  560. bool isTargetMemoryOpcode() const {
  561. return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
  562. }
  563. /// Return true if the type of the node type undefined.
  564. bool isUndef() const { return NodeType == ISD::UNDEF; }
  565. /// Test if this node is a memory intrinsic (with valid pointer information).
  566. /// INTRINSIC_W_CHAIN and INTRINSIC_VOID nodes are sometimes created for
  567. /// non-memory intrinsics (with chains) that are not really instances of
  568. /// MemSDNode. For such nodes, we need some extra state to determine the
  569. /// proper classof relationship.
  570. bool isMemIntrinsic() const {
  571. return (NodeType == ISD::INTRINSIC_W_CHAIN ||
  572. NodeType == ISD::INTRINSIC_VOID) &&
  573. SDNodeBits.IsMemIntrinsic;
  574. }
  575. /// Test if this node is a strict floating point pseudo-op.
  576. bool isStrictFPOpcode() {
  577. switch (NodeType) {
  578. default:
  579. return false;
  580. case ISD::STRICT_FP16_TO_FP:
  581. case ISD::STRICT_FP_TO_FP16:
  582. #define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
  583. case ISD::STRICT_##DAGN:
  584. #include "llvm/IR/ConstrainedOps.def"
  585. return true;
  586. }
  587. }
  588. /// Test if this node is a vector predication operation.
  589. bool isVPOpcode() const { return ISD::isVPOpcode(getOpcode()); }
  590. /// Test if this node has a post-isel opcode, directly
  591. /// corresponding to a MachineInstr opcode.
  592. bool isMachineOpcode() const { return NodeType < 0; }
  593. /// This may only be called if isMachineOpcode returns
  594. /// true. It returns the MachineInstr opcode value that the node's opcode
  595. /// corresponds to.
  596. unsigned getMachineOpcode() const {
  597. assert(isMachineOpcode() && "Not a MachineInstr opcode!");
  598. return ~NodeType;
  599. }
  600. bool getHasDebugValue() const { return SDNodeBits.HasDebugValue; }
  601. void setHasDebugValue(bool b) { SDNodeBits.HasDebugValue = b; }
  602. bool isDivergent() const { return SDNodeBits.IsDivergent; }
  603. /// Return true if there are no uses of this node.
  604. bool use_empty() const { return UseList == nullptr; }
  605. /// Return true if there is exactly one use of this node.
  606. bool hasOneUse() const { return hasSingleElement(uses()); }
  607. /// Return the number of uses of this node. This method takes
  608. /// time proportional to the number of uses.
  609. size_t use_size() const { return std::distance(use_begin(), use_end()); }
  610. /// Return the unique node id.
  611. int getNodeId() const { return NodeId; }
  612. /// Set unique node id.
  613. void setNodeId(int Id) { NodeId = Id; }
  614. /// Return the node ordering.
  615. unsigned getIROrder() const { return IROrder; }
  616. /// Set the node ordering.
  617. void setIROrder(unsigned Order) { IROrder = Order; }
  618. /// Return the source location info.
  619. const DebugLoc &getDebugLoc() const { return debugLoc; }
  620. /// Set source location info. Try to avoid this, putting
  621. /// it in the constructor is preferable.
  622. void setDebugLoc(DebugLoc dl) { debugLoc = std::move(dl); }
  623. /// This class provides iterator support for SDUse
  624. /// operands that use a specific SDNode.
  625. class use_iterator {
  626. friend class SDNode;
  627. SDUse *Op = nullptr;
  628. explicit use_iterator(SDUse *op) : Op(op) {}
  629. public:
  630. using iterator_category = std::forward_iterator_tag;
  631. using value_type = SDUse;
  632. using difference_type = std::ptrdiff_t;
  633. using pointer = value_type *;
  634. using reference = value_type &;
  635. use_iterator() = default;
  636. use_iterator(const use_iterator &I) = default;
  637. use_iterator &operator=(const use_iterator &) = default;
  638. bool operator==(const use_iterator &x) const { return Op == x.Op; }
  639. bool operator!=(const use_iterator &x) const {
  640. return !operator==(x);
  641. }
  642. /// Return true if this iterator is at the end of uses list.
  643. bool atEnd() const { return Op == nullptr; }
  644. // Iterator traversal: forward iteration only.
  645. use_iterator &operator++() { // Preincrement
  646. assert(Op && "Cannot increment end iterator!");
  647. Op = Op->getNext();
  648. return *this;
  649. }
  650. use_iterator operator++(int) { // Postincrement
  651. use_iterator tmp = *this; ++*this; return tmp;
  652. }
  653. /// Retrieve a pointer to the current user node.
  654. SDNode *operator*() const {
  655. assert(Op && "Cannot dereference end iterator!");
  656. return Op->getUser();
  657. }
  658. SDNode *operator->() const { return operator*(); }
  659. SDUse &getUse() const { return *Op; }
  660. /// Retrieve the operand # of this use in its user.
  661. unsigned getOperandNo() const {
  662. assert(Op && "Cannot dereference end iterator!");
  663. return (unsigned)(Op - Op->getUser()->OperandList);
  664. }
  665. };
  666. /// Provide iteration support to walk over all uses of an SDNode.
  667. use_iterator use_begin() const {
  668. return use_iterator(UseList);
  669. }
  670. static use_iterator use_end() { return use_iterator(nullptr); }
  671. inline iterator_range<use_iterator> uses() {
  672. return make_range(use_begin(), use_end());
  673. }
  674. inline iterator_range<use_iterator> uses() const {
  675. return make_range(use_begin(), use_end());
  676. }
  677. /// Return true if there are exactly NUSES uses of the indicated value.
  678. /// This method ignores uses of other values defined by this operation.
  679. bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
  680. /// Return true if there are any use of the indicated value.
  681. /// This method ignores uses of other values defined by this operation.
  682. bool hasAnyUseOfValue(unsigned Value) const;
  683. /// Return true if this node is the only use of N.
  684. bool isOnlyUserOf(const SDNode *N) const;
  685. /// Return true if this node is an operand of N.
  686. bool isOperandOf(const SDNode *N) const;
  687. /// Return true if this node is a predecessor of N.
  688. /// NOTE: Implemented on top of hasPredecessor and every bit as
  689. /// expensive. Use carefully.
  690. bool isPredecessorOf(const SDNode *N) const {
  691. return N->hasPredecessor(this);
  692. }
  693. /// Return true if N is a predecessor of this node.
  694. /// N is either an operand of this node, or can be reached by recursively
  695. /// traversing up the operands.
  696. /// NOTE: This is an expensive method. Use it carefully.
  697. bool hasPredecessor(const SDNode *N) const;
  698. /// Returns true if N is a predecessor of any node in Worklist. This
  699. /// helper keeps Visited and Worklist sets externally to allow unions
  700. /// searches to be performed in parallel, caching of results across
  701. /// queries and incremental addition to Worklist. Stops early if N is
  702. /// found but will resume. Remember to clear Visited and Worklists
  703. /// if DAG changes. MaxSteps gives a maximum number of nodes to visit before
  704. /// giving up. The TopologicalPrune flag signals that positive NodeIds are
  705. /// topologically ordered (Operands have strictly smaller node id) and search
  706. /// can be pruned leveraging this.
  707. static bool hasPredecessorHelper(const SDNode *N,
  708. SmallPtrSetImpl<const SDNode *> &Visited,
  709. SmallVectorImpl<const SDNode *> &Worklist,
  710. unsigned int MaxSteps = 0,
  711. bool TopologicalPrune = false) {
  712. SmallVector<const SDNode *, 8> DeferredNodes;
  713. if (Visited.count(N))
  714. return true;
  715. // Node Id's are assigned in three places: As a topological
  716. // ordering (> 0), during legalization (results in values set to
  717. // 0), new nodes (set to -1). If N has a topolgical id then we
  718. // know that all nodes with ids smaller than it cannot be
  719. // successors and we need not check them. Filter out all node
  720. // that can't be matches. We add them to the worklist before exit
  721. // in case of multiple calls. Note that during selection the topological id
  722. // may be violated if a node's predecessor is selected before it. We mark
  723. // this at selection negating the id of unselected successors and
  724. // restricting topological pruning to positive ids.
  725. int NId = N->getNodeId();
  726. // If we Invalidated the Id, reconstruct original NId.
  727. if (NId < -1)
  728. NId = -(NId + 1);
  729. bool Found = false;
  730. while (!Worklist.empty()) {
  731. const SDNode *M = Worklist.pop_back_val();
  732. int MId = M->getNodeId();
  733. if (TopologicalPrune && M->getOpcode() != ISD::TokenFactor && (NId > 0) &&
  734. (MId > 0) && (MId < NId)) {
  735. DeferredNodes.push_back(M);
  736. continue;
  737. }
  738. for (const SDValue &OpV : M->op_values()) {
  739. SDNode *Op = OpV.getNode();
  740. if (Visited.insert(Op).second)
  741. Worklist.push_back(Op);
  742. if (Op == N)
  743. Found = true;
  744. }
  745. if (Found)
  746. break;
  747. if (MaxSteps != 0 && Visited.size() >= MaxSteps)
  748. break;
  749. }
  750. // Push deferred nodes back on worklist.
  751. Worklist.append(DeferredNodes.begin(), DeferredNodes.end());
  752. // If we bailed early, conservatively return found.
  753. if (MaxSteps != 0 && Visited.size() >= MaxSteps)
  754. return true;
  755. return Found;
  756. }
  757. /// Return true if all the users of N are contained in Nodes.
  758. /// NOTE: Requires at least one match, but doesn't require them all.
  759. static bool areOnlyUsersOf(ArrayRef<const SDNode *> Nodes, const SDNode *N);
  760. /// Return the number of values used by this operation.
  761. unsigned getNumOperands() const { return NumOperands; }
  762. /// Return the maximum number of operands that a SDNode can hold.
  763. static constexpr size_t getMaxNumOperands() {
  764. return std::numeric_limits<decltype(SDNode::NumOperands)>::max();
  765. }
  766. /// Helper method returns the integer value of a ConstantSDNode operand.
  767. inline uint64_t getConstantOperandVal(unsigned Num) const;
  768. /// Helper method returns the APInt of a ConstantSDNode operand.
  769. inline const APInt &getConstantOperandAPInt(unsigned Num) const;
  770. const SDValue &getOperand(unsigned Num) const {
  771. assert(Num < NumOperands && "Invalid child # of SDNode!");
  772. return OperandList[Num];
  773. }
  774. using op_iterator = SDUse *;
  775. op_iterator op_begin() const { return OperandList; }
  776. op_iterator op_end() const { return OperandList+NumOperands; }
  777. ArrayRef<SDUse> ops() const { return ArrayRef(op_begin(), op_end()); }
  778. /// Iterator for directly iterating over the operand SDValue's.
  779. struct value_op_iterator
  780. : iterator_adaptor_base<value_op_iterator, op_iterator,
  781. std::random_access_iterator_tag, SDValue,
  782. ptrdiff_t, value_op_iterator *,
  783. value_op_iterator *> {
  784. explicit value_op_iterator(SDUse *U = nullptr)
  785. : iterator_adaptor_base(U) {}
  786. const SDValue &operator*() const { return I->get(); }
  787. };
  788. iterator_range<value_op_iterator> op_values() const {
  789. return make_range(value_op_iterator(op_begin()),
  790. value_op_iterator(op_end()));
  791. }
  792. SDVTList getVTList() const {
  793. SDVTList X = { ValueList, NumValues };
  794. return X;
  795. }
  796. /// If this node has a glue operand, return the node
  797. /// to which the glue operand points. Otherwise return NULL.
  798. SDNode *getGluedNode() const {
  799. if (getNumOperands() != 0 &&
  800. getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
  801. return getOperand(getNumOperands()-1).getNode();
  802. return nullptr;
  803. }
  804. /// If this node has a glue value with a user, return
  805. /// the user (there is at most one). Otherwise return NULL.
  806. SDNode *getGluedUser() const {
  807. for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
  808. if (UI.getUse().get().getValueType() == MVT::Glue)
  809. return *UI;
  810. return nullptr;
  811. }
  812. SDNodeFlags getFlags() const { return Flags; }
  813. void setFlags(SDNodeFlags NewFlags) { Flags = NewFlags; }
  814. /// Clear any flags in this node that aren't also set in Flags.
  815. /// If Flags is not in a defined state then this has no effect.
  816. void intersectFlagsWith(const SDNodeFlags Flags);
  817. void setCFIType(uint32_t Type) { CFIType = Type; }
  818. uint32_t getCFIType() const { return CFIType; }
  819. /// Return the number of values defined/returned by this operator.
  820. unsigned getNumValues() const { return NumValues; }
  821. /// Return the type of a specified result.
  822. EVT getValueType(unsigned ResNo) const {
  823. assert(ResNo < NumValues && "Illegal result number!");
  824. return ValueList[ResNo];
  825. }
  826. /// Return the type of a specified result as a simple type.
  827. MVT getSimpleValueType(unsigned ResNo) const {
  828. return getValueType(ResNo).getSimpleVT();
  829. }
  830. /// Returns MVT::getSizeInBits(getValueType(ResNo)).
  831. ///
  832. /// If the value type is a scalable vector type, the scalable property will
  833. /// be set and the runtime size will be a positive integer multiple of the
  834. /// base size.
  835. TypeSize getValueSizeInBits(unsigned ResNo) const {
  836. return getValueType(ResNo).getSizeInBits();
  837. }
  838. using value_iterator = const EVT *;
  839. value_iterator value_begin() const { return ValueList; }
  840. value_iterator value_end() const { return ValueList+NumValues; }
  841. iterator_range<value_iterator> values() const {
  842. return llvm::make_range(value_begin(), value_end());
  843. }
  844. /// Return the opcode of this operation for printing.
  845. std::string getOperationName(const SelectionDAG *G = nullptr) const;
  846. static const char* getIndexedModeName(ISD::MemIndexedMode AM);
  847. void print_types(raw_ostream &OS, const SelectionDAG *G) const;
  848. void print_details(raw_ostream &OS, const SelectionDAG *G) const;
  849. void print(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
  850. void printr(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
  851. /// Print a SelectionDAG node and all children down to
  852. /// the leaves. The given SelectionDAG allows target-specific nodes
  853. /// to be printed in human-readable form. Unlike printr, this will
  854. /// print the whole DAG, including children that appear multiple
  855. /// times.
  856. ///
  857. void printrFull(raw_ostream &O, const SelectionDAG *G = nullptr) const;
  858. /// Print a SelectionDAG node and children up to
  859. /// depth "depth." The given SelectionDAG allows target-specific
  860. /// nodes to be printed in human-readable form. Unlike printr, this
  861. /// will print children that appear multiple times wherever they are
  862. /// used.
  863. ///
  864. void printrWithDepth(raw_ostream &O, const SelectionDAG *G = nullptr,
  865. unsigned depth = 100) const;
  866. /// Dump this node, for debugging.
  867. void dump() const;
  868. /// Dump (recursively) this node and its use-def subgraph.
  869. void dumpr() const;
  870. /// Dump this node, for debugging.
  871. /// The given SelectionDAG allows target-specific nodes to be printed
  872. /// in human-readable form.
  873. void dump(const SelectionDAG *G) const;
  874. /// Dump (recursively) this node and its use-def subgraph.
  875. /// The given SelectionDAG allows target-specific nodes to be printed
  876. /// in human-readable form.
  877. void dumpr(const SelectionDAG *G) const;
  878. /// printrFull to dbgs(). The given SelectionDAG allows
  879. /// target-specific nodes to be printed in human-readable form.
  880. /// Unlike dumpr, this will print the whole DAG, including children
  881. /// that appear multiple times.
  882. void dumprFull(const SelectionDAG *G = nullptr) const;
  883. /// printrWithDepth to dbgs(). The given
  884. /// SelectionDAG allows target-specific nodes to be printed in
  885. /// human-readable form. Unlike dumpr, this will print children
  886. /// that appear multiple times wherever they are used.
  887. ///
  888. void dumprWithDepth(const SelectionDAG *G = nullptr,
  889. unsigned depth = 100) const;
  890. /// Gather unique data for the node.
  891. void Profile(FoldingSetNodeID &ID) const;
  892. /// This method should only be used by the SDUse class.
  893. void addUse(SDUse &U) { U.addToList(&UseList); }
  894. protected:
  895. static SDVTList getSDVTList(EVT VT) {
  896. SDVTList Ret = { getValueTypeList(VT), 1 };
  897. return Ret;
  898. }
  899. /// Create an SDNode.
  900. ///
  901. /// SDNodes are created without any operands, and never own the operand
  902. /// storage. To add operands, see SelectionDAG::createOperands.
  903. SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
  904. : NodeType(Opc), ValueList(VTs.VTs), NumValues(VTs.NumVTs),
  905. IROrder(Order), debugLoc(std::move(dl)) {
  906. memset(&RawSDNodeBits, 0, sizeof(RawSDNodeBits));
  907. assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
  908. assert(NumValues == VTs.NumVTs &&
  909. "NumValues wasn't wide enough for its operands!");
  910. }
  911. /// Release the operands and set this node to have zero operands.
  912. void DropOperands();
  913. };
  914. /// Wrapper class for IR location info (IR ordering and DebugLoc) to be passed
  915. /// into SDNode creation functions.
  916. /// When an SDNode is created from the DAGBuilder, the DebugLoc is extracted
  917. /// from the original Instruction, and IROrder is the ordinal position of
  918. /// the instruction.
  919. /// When an SDNode is created after the DAG is being built, both DebugLoc and
  920. /// the IROrder are propagated from the original SDNode.
  921. /// So SDLoc class provides two constructors besides the default one, one to
  922. /// be used by the DAGBuilder, the other to be used by others.
  923. class SDLoc {
  924. private:
  925. DebugLoc DL;
  926. int IROrder = 0;
  927. public:
  928. SDLoc() = default;
  929. SDLoc(const SDNode *N) : DL(N->getDebugLoc()), IROrder(N->getIROrder()) {}
  930. SDLoc(const SDValue V) : SDLoc(V.getNode()) {}
  931. SDLoc(const Instruction *I, int Order) : IROrder(Order) {
  932. assert(Order >= 0 && "bad IROrder");
  933. if (I)
  934. DL = I->getDebugLoc();
  935. }
  936. unsigned getIROrder() const { return IROrder; }
  937. const DebugLoc &getDebugLoc() const { return DL; }
  938. };
  939. // Define inline functions from the SDValue class.
  940. inline SDValue::SDValue(SDNode *node, unsigned resno)
  941. : Node(node), ResNo(resno) {
  942. // Explicitly check for !ResNo to avoid use-after-free, because there are
  943. // callers that use SDValue(N, 0) with a deleted N to indicate successful
  944. // combines.
  945. assert((!Node || !ResNo || ResNo < Node->getNumValues()) &&
  946. "Invalid result number for the given node!");
  947. assert(ResNo < -2U && "Cannot use result numbers reserved for DenseMaps.");
  948. }
  949. inline unsigned SDValue::getOpcode() const {
  950. return Node->getOpcode();
  951. }
  952. inline EVT SDValue::getValueType() const {
  953. return Node->getValueType(ResNo);
  954. }
  955. inline unsigned SDValue::getNumOperands() const {
  956. return Node->getNumOperands();
  957. }
  958. inline const SDValue &SDValue::getOperand(unsigned i) const {
  959. return Node->getOperand(i);
  960. }
  961. inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
  962. return Node->getConstantOperandVal(i);
  963. }
  964. inline const APInt &SDValue::getConstantOperandAPInt(unsigned i) const {
  965. return Node->getConstantOperandAPInt(i);
  966. }
  967. inline bool SDValue::isTargetOpcode() const {
  968. return Node->isTargetOpcode();
  969. }
  970. inline bool SDValue::isTargetMemoryOpcode() const {
  971. return Node->isTargetMemoryOpcode();
  972. }
  973. inline bool SDValue::isMachineOpcode() const {
  974. return Node->isMachineOpcode();
  975. }
  976. inline unsigned SDValue::getMachineOpcode() const {
  977. return Node->getMachineOpcode();
  978. }
  979. inline bool SDValue::isUndef() const {
  980. return Node->isUndef();
  981. }
  982. inline bool SDValue::use_empty() const {
  983. return !Node->hasAnyUseOfValue(ResNo);
  984. }
  985. inline bool SDValue::hasOneUse() const {
  986. return Node->hasNUsesOfValue(1, ResNo);
  987. }
  988. inline const DebugLoc &SDValue::getDebugLoc() const {
  989. return Node->getDebugLoc();
  990. }
  991. inline void SDValue::dump() const {
  992. return Node->dump();
  993. }
  994. inline void SDValue::dump(const SelectionDAG *G) const {
  995. return Node->dump(G);
  996. }
  997. inline void SDValue::dumpr() const {
  998. return Node->dumpr();
  999. }
  1000. inline void SDValue::dumpr(const SelectionDAG *G) const {
  1001. return Node->dumpr(G);
  1002. }
  1003. // Define inline functions from the SDUse class.
  1004. inline void SDUse::set(const SDValue &V) {
  1005. if (Val.getNode()) removeFromList();
  1006. Val = V;
  1007. if (V.getNode())
  1008. V->addUse(*this);
  1009. }
  1010. inline void SDUse::setInitial(const SDValue &V) {
  1011. Val = V;
  1012. V->addUse(*this);
  1013. }
  1014. inline void SDUse::setNode(SDNode *N) {
  1015. if (Val.getNode()) removeFromList();
  1016. Val.setNode(N);
  1017. if (N) N->addUse(*this);
  1018. }
  1019. /// This class is used to form a handle around another node that
  1020. /// is persistent and is updated across invocations of replaceAllUsesWith on its
  1021. /// operand. This node should be directly created by end-users and not added to
  1022. /// the AllNodes list.
  1023. class HandleSDNode : public SDNode {
  1024. SDUse Op;
  1025. public:
  1026. explicit HandleSDNode(SDValue X)
  1027. : SDNode(ISD::HANDLENODE, 0, DebugLoc(), getSDVTList(MVT::Other)) {
  1028. // HandleSDNodes are never inserted into the DAG, so they won't be
  1029. // auto-numbered. Use ID 65535 as a sentinel.
  1030. PersistentId = 0xffff;
  1031. // Manually set up the operand list. This node type is special in that it's
  1032. // always stack allocated and SelectionDAG does not manage its operands.
  1033. // TODO: This should either (a) not be in the SDNode hierarchy, or (b) not
  1034. // be so special.
  1035. Op.setUser(this);
  1036. Op.setInitial(X);
  1037. NumOperands = 1;
  1038. OperandList = &Op;
  1039. }
  1040. ~HandleSDNode();
  1041. const SDValue &getValue() const { return Op; }
  1042. };
  1043. class AddrSpaceCastSDNode : public SDNode {
  1044. private:
  1045. unsigned SrcAddrSpace;
  1046. unsigned DestAddrSpace;
  1047. public:
  1048. AddrSpaceCastSDNode(unsigned Order, const DebugLoc &dl, EVT VT,
  1049. unsigned SrcAS, unsigned DestAS);
  1050. unsigned getSrcAddressSpace() const { return SrcAddrSpace; }
  1051. unsigned getDestAddressSpace() const { return DestAddrSpace; }
  1052. static bool classof(const SDNode *N) {
  1053. return N->getOpcode() == ISD::ADDRSPACECAST;
  1054. }
  1055. };
  1056. /// This is an abstract virtual class for memory operations.
  1057. class MemSDNode : public SDNode {
  1058. private:
  1059. // VT of in-memory value.
  1060. EVT MemoryVT;
  1061. protected:
  1062. /// Memory reference information.
  1063. MachineMemOperand *MMO;
  1064. public:
  1065. MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs,
  1066. EVT memvt, MachineMemOperand *MMO);
  1067. bool readMem() const { return MMO->isLoad(); }
  1068. bool writeMem() const { return MMO->isStore(); }
  1069. /// Returns alignment and volatility of the memory access
  1070. Align getOriginalAlign() const { return MMO->getBaseAlign(); }
  1071. Align getAlign() const { return MMO->getAlign(); }
  1072. /// Return the SubclassData value, without HasDebugValue. This contains an
  1073. /// encoding of the volatile flag, as well as bits used by subclasses. This
  1074. /// function should only be used to compute a FoldingSetNodeID value.
  1075. /// The HasDebugValue bit is masked out because CSE map needs to match
  1076. /// nodes with debug info with nodes without debug info. Same is about
  1077. /// isDivergent bit.
  1078. unsigned getRawSubclassData() const {
  1079. uint16_t Data;
  1080. union {
  1081. char RawSDNodeBits[sizeof(uint16_t)];
  1082. SDNodeBitfields SDNodeBits;
  1083. };
  1084. memcpy(&RawSDNodeBits, &this->RawSDNodeBits, sizeof(this->RawSDNodeBits));
  1085. SDNodeBits.HasDebugValue = 0;
  1086. SDNodeBits.IsDivergent = false;
  1087. memcpy(&Data, &RawSDNodeBits, sizeof(RawSDNodeBits));
  1088. return Data;
  1089. }
  1090. bool isVolatile() const { return MemSDNodeBits.IsVolatile; }
  1091. bool isNonTemporal() const { return MemSDNodeBits.IsNonTemporal; }
  1092. bool isDereferenceable() const { return MemSDNodeBits.IsDereferenceable; }
  1093. bool isInvariant() const { return MemSDNodeBits.IsInvariant; }
  1094. // Returns the offset from the location of the access.
  1095. int64_t getSrcValueOffset() const { return MMO->getOffset(); }
  1096. /// Returns the AA info that describes the dereference.
  1097. AAMDNodes getAAInfo() const { return MMO->getAAInfo(); }
  1098. /// Returns the Ranges that describes the dereference.
  1099. const MDNode *getRanges() const { return MMO->getRanges(); }
  1100. /// Returns the synchronization scope ID for this memory operation.
  1101. SyncScope::ID getSyncScopeID() const { return MMO->getSyncScopeID(); }
  1102. /// Return the atomic ordering requirements for this memory operation. For
  1103. /// cmpxchg atomic operations, return the atomic ordering requirements when
  1104. /// store occurs.
  1105. AtomicOrdering getSuccessOrdering() const {
  1106. return MMO->getSuccessOrdering();
  1107. }
  1108. /// Return a single atomic ordering that is at least as strong as both the
  1109. /// success and failure orderings for an atomic operation. (For operations
  1110. /// other than cmpxchg, this is equivalent to getSuccessOrdering().)
  1111. AtomicOrdering getMergedOrdering() const { return MMO->getMergedOrdering(); }
  1112. /// Return true if the memory operation ordering is Unordered or higher.
  1113. bool isAtomic() const { return MMO->isAtomic(); }
  1114. /// Returns true if the memory operation doesn't imply any ordering
  1115. /// constraints on surrounding memory operations beyond the normal memory
  1116. /// aliasing rules.
  1117. bool isUnordered() const { return MMO->isUnordered(); }
  1118. /// Returns true if the memory operation is neither atomic or volatile.
  1119. bool isSimple() const { return !isAtomic() && !isVolatile(); }
  1120. /// Return the type of the in-memory value.
  1121. EVT getMemoryVT() const { return MemoryVT; }
  1122. /// Return a MachineMemOperand object describing the memory
  1123. /// reference performed by operation.
  1124. MachineMemOperand *getMemOperand() const { return MMO; }
  1125. const MachinePointerInfo &getPointerInfo() const {
  1126. return MMO->getPointerInfo();
  1127. }
  1128. /// Return the address space for the associated pointer
  1129. unsigned getAddressSpace() const {
  1130. return getPointerInfo().getAddrSpace();
  1131. }
  1132. /// Update this MemSDNode's MachineMemOperand information
  1133. /// to reflect the alignment of NewMMO, if it has a greater alignment.
  1134. /// This must only be used when the new alignment applies to all users of
  1135. /// this MachineMemOperand.
  1136. void refineAlignment(const MachineMemOperand *NewMMO) {
  1137. MMO->refineAlignment(NewMMO);
  1138. }
  1139. const SDValue &getChain() const { return getOperand(0); }
  1140. const SDValue &getBasePtr() const {
  1141. switch (getOpcode()) {
  1142. case ISD::STORE:
  1143. case ISD::VP_STORE:
  1144. case ISD::MSTORE:
  1145. case ISD::VP_SCATTER:
  1146. case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
  1147. return getOperand(2);
  1148. case ISD::MGATHER:
  1149. case ISD::MSCATTER:
  1150. return getOperand(3);
  1151. default:
  1152. return getOperand(1);
  1153. }
  1154. }
  1155. // Methods to support isa and dyn_cast
  1156. static bool classof(const SDNode *N) {
  1157. // For some targets, we lower some target intrinsics to a MemIntrinsicNode
  1158. // with either an intrinsic or a target opcode.
  1159. switch (N->getOpcode()) {
  1160. case ISD::LOAD:
  1161. case ISD::STORE:
  1162. case ISD::PREFETCH:
  1163. case ISD::ATOMIC_CMP_SWAP:
  1164. case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
  1165. case ISD::ATOMIC_SWAP:
  1166. case ISD::ATOMIC_LOAD_ADD:
  1167. case ISD::ATOMIC_LOAD_SUB:
  1168. case ISD::ATOMIC_LOAD_AND:
  1169. case ISD::ATOMIC_LOAD_CLR:
  1170. case ISD::ATOMIC_LOAD_OR:
  1171. case ISD::ATOMIC_LOAD_XOR:
  1172. case ISD::ATOMIC_LOAD_NAND:
  1173. case ISD::ATOMIC_LOAD_MIN:
  1174. case ISD::ATOMIC_LOAD_MAX:
  1175. case ISD::ATOMIC_LOAD_UMIN:
  1176. case ISD::ATOMIC_LOAD_UMAX:
  1177. case ISD::ATOMIC_LOAD_FADD:
  1178. case ISD::ATOMIC_LOAD_FSUB:
  1179. case ISD::ATOMIC_LOAD_FMAX:
  1180. case ISD::ATOMIC_LOAD_FMIN:
  1181. case ISD::ATOMIC_LOAD_UINC_WRAP:
  1182. case ISD::ATOMIC_LOAD_UDEC_WRAP:
  1183. case ISD::ATOMIC_LOAD:
  1184. case ISD::ATOMIC_STORE:
  1185. case ISD::MLOAD:
  1186. case ISD::MSTORE:
  1187. case ISD::MGATHER:
  1188. case ISD::MSCATTER:
  1189. case ISD::VP_LOAD:
  1190. case ISD::VP_STORE:
  1191. case ISD::VP_GATHER:
  1192. case ISD::VP_SCATTER:
  1193. case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
  1194. case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
  1195. return true;
  1196. default:
  1197. return N->isMemIntrinsic() || N->isTargetMemoryOpcode();
  1198. }
  1199. }
  1200. };
  1201. /// This is an SDNode representing atomic operations.
  1202. class AtomicSDNode : public MemSDNode {
  1203. public:
  1204. AtomicSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTL,
  1205. EVT MemVT, MachineMemOperand *MMO)
  1206. : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
  1207. assert(((Opc != ISD::ATOMIC_LOAD && Opc != ISD::ATOMIC_STORE) ||
  1208. MMO->isAtomic()) && "then why are we using an AtomicSDNode?");
  1209. }
  1210. const SDValue &getBasePtr() const { return getOperand(1); }
  1211. const SDValue &getVal() const { return getOperand(2); }
  1212. /// Returns true if this SDNode represents cmpxchg atomic operation, false
  1213. /// otherwise.
  1214. bool isCompareAndSwap() const {
  1215. unsigned Op = getOpcode();
  1216. return Op == ISD::ATOMIC_CMP_SWAP ||
  1217. Op == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS;
  1218. }
  1219. /// For cmpxchg atomic operations, return the atomic ordering requirements
  1220. /// when store does not occur.
  1221. AtomicOrdering getFailureOrdering() const {
  1222. assert(isCompareAndSwap() && "Must be cmpxchg operation");
  1223. return MMO->getFailureOrdering();
  1224. }
  1225. // Methods to support isa and dyn_cast
  1226. static bool classof(const SDNode *N) {
  1227. return N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
  1228. N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS ||
  1229. N->getOpcode() == ISD::ATOMIC_SWAP ||
  1230. N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
  1231. N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
  1232. N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
  1233. N->getOpcode() == ISD::ATOMIC_LOAD_CLR ||
  1234. N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
  1235. N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
  1236. N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
  1237. N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
  1238. N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
  1239. N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
  1240. N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
  1241. N->getOpcode() == ISD::ATOMIC_LOAD_FADD ||
  1242. N->getOpcode() == ISD::ATOMIC_LOAD_FSUB ||
  1243. N->getOpcode() == ISD::ATOMIC_LOAD_FMAX ||
  1244. N->getOpcode() == ISD::ATOMIC_LOAD_FMIN ||
  1245. N->getOpcode() == ISD::ATOMIC_LOAD_UINC_WRAP ||
  1246. N->getOpcode() == ISD::ATOMIC_LOAD_UDEC_WRAP ||
  1247. N->getOpcode() == ISD::ATOMIC_LOAD ||
  1248. N->getOpcode() == ISD::ATOMIC_STORE;
  1249. }
  1250. };
  1251. /// This SDNode is used for target intrinsics that touch
  1252. /// memory and need an associated MachineMemOperand. Its opcode may be
  1253. /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
  1254. /// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
  1255. class MemIntrinsicSDNode : public MemSDNode {
  1256. public:
  1257. MemIntrinsicSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
  1258. SDVTList VTs, EVT MemoryVT, MachineMemOperand *MMO)
  1259. : MemSDNode(Opc, Order, dl, VTs, MemoryVT, MMO) {
  1260. SDNodeBits.IsMemIntrinsic = true;
  1261. }
  1262. // Methods to support isa and dyn_cast
  1263. static bool classof(const SDNode *N) {
  1264. // We lower some target intrinsics to their target opcode
  1265. // early a node with a target opcode can be of this class
  1266. return N->isMemIntrinsic() ||
  1267. N->getOpcode() == ISD::PREFETCH ||
  1268. N->isTargetMemoryOpcode();
  1269. }
  1270. };
  1271. /// This SDNode is used to implement the code generator
  1272. /// support for the llvm IR shufflevector instruction. It combines elements
  1273. /// from two input vectors into a new input vector, with the selection and
  1274. /// ordering of elements determined by an array of integers, referred to as
  1275. /// the shuffle mask. For input vectors of width N, mask indices of 0..N-1
  1276. /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
  1277. /// An index of -1 is treated as undef, such that the code generator may put
  1278. /// any value in the corresponding element of the result.
  1279. class ShuffleVectorSDNode : public SDNode {
  1280. // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
  1281. // is freed when the SelectionDAG object is destroyed.
  1282. const int *Mask;
  1283. protected:
  1284. friend class SelectionDAG;
  1285. ShuffleVectorSDNode(EVT VT, unsigned Order, const DebugLoc &dl, const int *M)
  1286. : SDNode(ISD::VECTOR_SHUFFLE, Order, dl, getSDVTList(VT)), Mask(M) {}
  1287. public:
  1288. ArrayRef<int> getMask() const {
  1289. EVT VT = getValueType(0);
  1290. return ArrayRef(Mask, VT.getVectorNumElements());
  1291. }
  1292. int getMaskElt(unsigned Idx) const {
  1293. assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
  1294. return Mask[Idx];
  1295. }
  1296. bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
  1297. int getSplatIndex() const {
  1298. assert(isSplat() && "Cannot get splat index for non-splat!");
  1299. EVT VT = getValueType(0);
  1300. for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
  1301. if (Mask[i] >= 0)
  1302. return Mask[i];
  1303. // We can choose any index value here and be correct because all elements
  1304. // are undefined. Return 0 for better potential for callers to simplify.
  1305. return 0;
  1306. }
  1307. static bool isSplatMask(const int *Mask, EVT VT);
  1308. /// Change values in a shuffle permute mask assuming
  1309. /// the two vector operands have swapped position.
  1310. static void commuteMask(MutableArrayRef<int> Mask) {
  1311. unsigned NumElems = Mask.size();
  1312. for (unsigned i = 0; i != NumElems; ++i) {
  1313. int idx = Mask[i];
  1314. if (idx < 0)
  1315. continue;
  1316. else if (idx < (int)NumElems)
  1317. Mask[i] = idx + NumElems;
  1318. else
  1319. Mask[i] = idx - NumElems;
  1320. }
  1321. }
  1322. static bool classof(const SDNode *N) {
  1323. return N->getOpcode() == ISD::VECTOR_SHUFFLE;
  1324. }
  1325. };
  1326. class ConstantSDNode : public SDNode {
  1327. friend class SelectionDAG;
  1328. const ConstantInt *Value;
  1329. ConstantSDNode(bool isTarget, bool isOpaque, const ConstantInt *val, EVT VT)
  1330. : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, 0, DebugLoc(),
  1331. getSDVTList(VT)),
  1332. Value(val) {
  1333. ConstantSDNodeBits.IsOpaque = isOpaque;
  1334. }
  1335. public:
  1336. const ConstantInt *getConstantIntValue() const { return Value; }
  1337. const APInt &getAPIntValue() const { return Value->getValue(); }
  1338. uint64_t getZExtValue() const { return Value->getZExtValue(); }
  1339. int64_t getSExtValue() const { return Value->getSExtValue(); }
  1340. uint64_t getLimitedValue(uint64_t Limit = UINT64_MAX) {
  1341. return Value->getLimitedValue(Limit);
  1342. }
  1343. MaybeAlign getMaybeAlignValue() const { return Value->getMaybeAlignValue(); }
  1344. Align getAlignValue() const { return Value->getAlignValue(); }
  1345. bool isOne() const { return Value->isOne(); }
  1346. bool isZero() const { return Value->isZero(); }
  1347. // NOTE: This is soft-deprecated. Please use `isZero()` instead.
  1348. bool isNullValue() const { return isZero(); }
  1349. bool isAllOnes() const { return Value->isMinusOne(); }
  1350. // NOTE: This is soft-deprecated. Please use `isAllOnes()` instead.
  1351. bool isAllOnesValue() const { return isAllOnes(); }
  1352. bool isMaxSignedValue() const { return Value->isMaxValue(true); }
  1353. bool isMinSignedValue() const { return Value->isMinValue(true); }
  1354. bool isOpaque() const { return ConstantSDNodeBits.IsOpaque; }
  1355. static bool classof(const SDNode *N) {
  1356. return N->getOpcode() == ISD::Constant ||
  1357. N->getOpcode() == ISD::TargetConstant;
  1358. }
  1359. };
  1360. uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
  1361. return cast<ConstantSDNode>(getOperand(Num))->getZExtValue();
  1362. }
  1363. const APInt &SDNode::getConstantOperandAPInt(unsigned Num) const {
  1364. return cast<ConstantSDNode>(getOperand(Num))->getAPIntValue();
  1365. }
  1366. class ConstantFPSDNode : public SDNode {
  1367. friend class SelectionDAG;
  1368. const ConstantFP *Value;
  1369. ConstantFPSDNode(bool isTarget, const ConstantFP *val, EVT VT)
  1370. : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP, 0,
  1371. DebugLoc(), getSDVTList(VT)),
  1372. Value(val) {}
  1373. public:
  1374. const APFloat& getValueAPF() const { return Value->getValueAPF(); }
  1375. const ConstantFP *getConstantFPValue() const { return Value; }
  1376. /// Return true if the value is positive or negative zero.
  1377. bool isZero() const { return Value->isZero(); }
  1378. /// Return true if the value is a NaN.
  1379. bool isNaN() const { return Value->isNaN(); }
  1380. /// Return true if the value is an infinity
  1381. bool isInfinity() const { return Value->isInfinity(); }
  1382. /// Return true if the value is negative.
  1383. bool isNegative() const { return Value->isNegative(); }
  1384. /// We don't rely on operator== working on double values, as
  1385. /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
  1386. /// As such, this method can be used to do an exact bit-for-bit comparison of
  1387. /// two floating point values.
  1388. /// We leave the version with the double argument here because it's just so
  1389. /// convenient to write "2.0" and the like. Without this function we'd
  1390. /// have to duplicate its logic everywhere it's called.
  1391. bool isExactlyValue(double V) const {
  1392. return Value->getValueAPF().isExactlyValue(V);
  1393. }
  1394. bool isExactlyValue(const APFloat& V) const;
  1395. static bool isValueValidForType(EVT VT, const APFloat& Val);
  1396. static bool classof(const SDNode *N) {
  1397. return N->getOpcode() == ISD::ConstantFP ||
  1398. N->getOpcode() == ISD::TargetConstantFP;
  1399. }
  1400. };
  1401. /// Returns true if \p V is a constant integer zero.
  1402. bool isNullConstant(SDValue V);
  1403. /// Returns true if \p V is an FP constant with a value of positive zero.
  1404. bool isNullFPConstant(SDValue V);
  1405. /// Returns true if \p V is an integer constant with all bits set.
  1406. bool isAllOnesConstant(SDValue V);
  1407. /// Returns true if \p V is a constant integer one.
  1408. bool isOneConstant(SDValue V);
  1409. /// Returns true if \p V is a constant min signed integer value.
  1410. bool isMinSignedConstant(SDValue V);
  1411. /// Returns true if \p V is a neutral element of Opc with Flags.
  1412. /// When OperandNo is 0, it checks that V is a left identity. Otherwise, it
  1413. /// checks that V is a right identity.
  1414. bool isNeutralConstant(unsigned Opc, SDNodeFlags Flags, SDValue V,
  1415. unsigned OperandNo);
  1416. /// Return the non-bitcasted source operand of \p V if it exists.
  1417. /// If \p V is not a bitcasted value, it is returned as-is.
  1418. SDValue peekThroughBitcasts(SDValue V);
  1419. /// Return the non-bitcasted and one-use source operand of \p V if it exists.
  1420. /// If \p V is not a bitcasted one-use value, it is returned as-is.
  1421. SDValue peekThroughOneUseBitcasts(SDValue V);
  1422. /// Return the non-extracted vector source operand of \p V if it exists.
  1423. /// If \p V is not an extracted subvector, it is returned as-is.
  1424. SDValue peekThroughExtractSubvectors(SDValue V);
  1425. /// Returns true if \p V is a bitwise not operation. Assumes that an all ones
  1426. /// constant is canonicalized to be operand 1.
  1427. bool isBitwiseNot(SDValue V, bool AllowUndefs = false);
  1428. /// If \p V is a bitwise not, returns the inverted operand. Otherwise returns
  1429. /// an empty SDValue. Only bits set in \p Mask are required to be inverted,
  1430. /// other bits may be arbitrary.
  1431. SDValue getBitwiseNotOperand(SDValue V, SDValue Mask, bool AllowUndefs);
  1432. /// Returns the SDNode if it is a constant splat BuildVector or constant int.
  1433. ConstantSDNode *isConstOrConstSplat(SDValue N, bool AllowUndefs = false,
  1434. bool AllowTruncation = false);
  1435. /// Returns the SDNode if it is a demanded constant splat BuildVector or
  1436. /// constant int.
  1437. ConstantSDNode *isConstOrConstSplat(SDValue N, const APInt &DemandedElts,
  1438. bool AllowUndefs = false,
  1439. bool AllowTruncation = false);
  1440. /// Returns the SDNode if it is a constant splat BuildVector or constant float.
  1441. ConstantFPSDNode *isConstOrConstSplatFP(SDValue N, bool AllowUndefs = false);
  1442. /// Returns the SDNode if it is a demanded constant splat BuildVector or
  1443. /// constant float.
  1444. ConstantFPSDNode *isConstOrConstSplatFP(SDValue N, const APInt &DemandedElts,
  1445. bool AllowUndefs = false);
  1446. /// Return true if the value is a constant 0 integer or a splatted vector of
  1447. /// a constant 0 integer (with no undefs by default).
  1448. /// Build vector implicit truncation is not an issue for null values.
  1449. bool isNullOrNullSplat(SDValue V, bool AllowUndefs = false);
  1450. /// Return true if the value is a constant 1 integer or a splatted vector of a
  1451. /// constant 1 integer (with no undefs).
  1452. /// Build vector implicit truncation is allowed, but the truncated bits need to
  1453. /// be zero.
  1454. bool isOneOrOneSplat(SDValue V, bool AllowUndefs = false);
  1455. /// Return true if the value is a constant -1 integer or a splatted vector of a
  1456. /// constant -1 integer (with no undefs).
  1457. /// Does not permit build vector implicit truncation.
  1458. bool isAllOnesOrAllOnesSplat(SDValue V, bool AllowUndefs = false);
  1459. /// Return true if \p V is either a integer or FP constant.
  1460. inline bool isIntOrFPConstant(SDValue V) {
  1461. return isa<ConstantSDNode>(V) || isa<ConstantFPSDNode>(V);
  1462. }
  1463. class GlobalAddressSDNode : public SDNode {
  1464. friend class SelectionDAG;
  1465. const GlobalValue *TheGlobal;
  1466. int64_t Offset;
  1467. unsigned TargetFlags;
  1468. GlobalAddressSDNode(unsigned Opc, unsigned Order, const DebugLoc &DL,
  1469. const GlobalValue *GA, EVT VT, int64_t o,
  1470. unsigned TF);
  1471. public:
  1472. const GlobalValue *getGlobal() const { return TheGlobal; }
  1473. int64_t getOffset() const { return Offset; }
  1474. unsigned getTargetFlags() const { return TargetFlags; }
  1475. // Return the address space this GlobalAddress belongs to.
  1476. unsigned getAddressSpace() const;
  1477. static bool classof(const SDNode *N) {
  1478. return N->getOpcode() == ISD::GlobalAddress ||
  1479. N->getOpcode() == ISD::TargetGlobalAddress ||
  1480. N->getOpcode() == ISD::GlobalTLSAddress ||
  1481. N->getOpcode() == ISD::TargetGlobalTLSAddress;
  1482. }
  1483. };
  1484. class FrameIndexSDNode : public SDNode {
  1485. friend class SelectionDAG;
  1486. int FI;
  1487. FrameIndexSDNode(int fi, EVT VT, bool isTarg)
  1488. : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
  1489. 0, DebugLoc(), getSDVTList(VT)), FI(fi) {
  1490. }
  1491. public:
  1492. int getIndex() const { return FI; }
  1493. static bool classof(const SDNode *N) {
  1494. return N->getOpcode() == ISD::FrameIndex ||
  1495. N->getOpcode() == ISD::TargetFrameIndex;
  1496. }
  1497. };
  1498. /// This SDNode is used for LIFETIME_START/LIFETIME_END values, which indicate
  1499. /// the offet and size that are started/ended in the underlying FrameIndex.
  1500. class LifetimeSDNode : public SDNode {
  1501. friend class SelectionDAG;
  1502. int64_t Size;
  1503. int64_t Offset; // -1 if offset is unknown.
  1504. LifetimeSDNode(unsigned Opcode, unsigned Order, const DebugLoc &dl,
  1505. SDVTList VTs, int64_t Size, int64_t Offset)
  1506. : SDNode(Opcode, Order, dl, VTs), Size(Size), Offset(Offset) {}
  1507. public:
  1508. int64_t getFrameIndex() const {
  1509. return cast<FrameIndexSDNode>(getOperand(1))->getIndex();
  1510. }
  1511. bool hasOffset() const { return Offset >= 0; }
  1512. int64_t getOffset() const {
  1513. assert(hasOffset() && "offset is unknown");
  1514. return Offset;
  1515. }
  1516. int64_t getSize() const {
  1517. assert(hasOffset() && "offset is unknown");
  1518. return Size;
  1519. }
  1520. // Methods to support isa and dyn_cast
  1521. static bool classof(const SDNode *N) {
  1522. return N->getOpcode() == ISD::LIFETIME_START ||
  1523. N->getOpcode() == ISD::LIFETIME_END;
  1524. }
  1525. };
  1526. /// This SDNode is used for PSEUDO_PROBE values, which are the function guid and
  1527. /// the index of the basic block being probed. A pseudo probe serves as a place
  1528. /// holder and will be removed at the end of compilation. It does not have any
  1529. /// operand because we do not want the instruction selection to deal with any.
  1530. class PseudoProbeSDNode : public SDNode {
  1531. friend class SelectionDAG;
  1532. uint64_t Guid;
  1533. uint64_t Index;
  1534. uint32_t Attributes;
  1535. PseudoProbeSDNode(unsigned Opcode, unsigned Order, const DebugLoc &Dl,
  1536. SDVTList VTs, uint64_t Guid, uint64_t Index, uint32_t Attr)
  1537. : SDNode(Opcode, Order, Dl, VTs), Guid(Guid), Index(Index),
  1538. Attributes(Attr) {}
  1539. public:
  1540. uint64_t getGuid() const { return Guid; }
  1541. uint64_t getIndex() const { return Index; }
  1542. uint32_t getAttributes() const { return Attributes; }
  1543. // Methods to support isa and dyn_cast
  1544. static bool classof(const SDNode *N) {
  1545. return N->getOpcode() == ISD::PSEUDO_PROBE;
  1546. }
  1547. };
  1548. class JumpTableSDNode : public SDNode {
  1549. friend class SelectionDAG;
  1550. int JTI;
  1551. unsigned TargetFlags;
  1552. JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned TF)
  1553. : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
  1554. 0, DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
  1555. }
  1556. public:
  1557. int getIndex() const { return JTI; }
  1558. unsigned getTargetFlags() const { return TargetFlags; }
  1559. static bool classof(const SDNode *N) {
  1560. return N->getOpcode() == ISD::JumpTable ||
  1561. N->getOpcode() == ISD::TargetJumpTable;
  1562. }
  1563. };
  1564. class ConstantPoolSDNode : public SDNode {
  1565. friend class SelectionDAG;
  1566. union {
  1567. const Constant *ConstVal;
  1568. MachineConstantPoolValue *MachineCPVal;
  1569. } Val;
  1570. int Offset; // It's a MachineConstantPoolValue if top bit is set.
  1571. Align Alignment; // Minimum alignment requirement of CP.
  1572. unsigned TargetFlags;
  1573. ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
  1574. Align Alignment, unsigned TF)
  1575. : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
  1576. DebugLoc(), getSDVTList(VT)),
  1577. Offset(o), Alignment(Alignment), TargetFlags(TF) {
  1578. assert(Offset >= 0 && "Offset is too large");
  1579. Val.ConstVal = c;
  1580. }
  1581. ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v, EVT VT, int o,
  1582. Align Alignment, unsigned TF)
  1583. : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
  1584. DebugLoc(), getSDVTList(VT)),
  1585. Offset(o), Alignment(Alignment), TargetFlags(TF) {
  1586. assert(Offset >= 0 && "Offset is too large");
  1587. Val.MachineCPVal = v;
  1588. Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
  1589. }
  1590. public:
  1591. bool isMachineConstantPoolEntry() const {
  1592. return Offset < 0;
  1593. }
  1594. const Constant *getConstVal() const {
  1595. assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
  1596. return Val.ConstVal;
  1597. }
  1598. MachineConstantPoolValue *getMachineCPVal() const {
  1599. assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
  1600. return Val.MachineCPVal;
  1601. }
  1602. int getOffset() const {
  1603. return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
  1604. }
  1605. // Return the alignment of this constant pool object, which is either 0 (for
  1606. // default alignment) or the desired value.
  1607. Align getAlign() const { return Alignment; }
  1608. unsigned getTargetFlags() const { return TargetFlags; }
  1609. Type *getType() const;
  1610. static bool classof(const SDNode *N) {
  1611. return N->getOpcode() == ISD::ConstantPool ||
  1612. N->getOpcode() == ISD::TargetConstantPool;
  1613. }
  1614. };
  1615. /// Completely target-dependent object reference.
  1616. class TargetIndexSDNode : public SDNode {
  1617. friend class SelectionDAG;
  1618. unsigned TargetFlags;
  1619. int Index;
  1620. int64_t Offset;
  1621. public:
  1622. TargetIndexSDNode(int Idx, EVT VT, int64_t Ofs, unsigned TF)
  1623. : SDNode(ISD::TargetIndex, 0, DebugLoc(), getSDVTList(VT)),
  1624. TargetFlags(TF), Index(Idx), Offset(Ofs) {}
  1625. unsigned getTargetFlags() const { return TargetFlags; }
  1626. int getIndex() const { return Index; }
  1627. int64_t getOffset() const { return Offset; }
  1628. static bool classof(const SDNode *N) {
  1629. return N->getOpcode() == ISD::TargetIndex;
  1630. }
  1631. };
  1632. class BasicBlockSDNode : public SDNode {
  1633. friend class SelectionDAG;
  1634. MachineBasicBlock *MBB;
  1635. /// Debug info is meaningful and potentially useful here, but we create
  1636. /// blocks out of order when they're jumped to, which makes it a bit
  1637. /// harder. Let's see if we need it first.
  1638. explicit BasicBlockSDNode(MachineBasicBlock *mbb)
  1639. : SDNode(ISD::BasicBlock, 0, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb)
  1640. {}
  1641. public:
  1642. MachineBasicBlock *getBasicBlock() const { return MBB; }
  1643. static bool classof(const SDNode *N) {
  1644. return N->getOpcode() == ISD::BasicBlock;
  1645. }
  1646. };
  1647. /// A "pseudo-class" with methods for operating on BUILD_VECTORs.
  1648. class BuildVectorSDNode : public SDNode {
  1649. public:
  1650. // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
  1651. explicit BuildVectorSDNode() = delete;
  1652. /// Check if this is a constant splat, and if so, find the
  1653. /// smallest element size that splats the vector. If MinSplatBits is
  1654. /// nonzero, the element size must be at least that large. Note that the
  1655. /// splat element may be the entire vector (i.e., a one element vector).
  1656. /// Returns the splat element value in SplatValue. Any undefined bits in
  1657. /// that value are zero, and the corresponding bits in the SplatUndef mask
  1658. /// are set. The SplatBitSize value is set to the splat element size in
  1659. /// bits. HasAnyUndefs is set to true if any bits in the vector are
  1660. /// undefined. isBigEndian describes the endianness of the target.
  1661. bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
  1662. unsigned &SplatBitSize, bool &HasAnyUndefs,
  1663. unsigned MinSplatBits = 0,
  1664. bool isBigEndian = false) const;
  1665. /// Returns the demanded splatted value or a null value if this is not a
  1666. /// splat.
  1667. ///
  1668. /// The DemandedElts mask indicates the elements that must be in the splat.
  1669. /// If passed a non-null UndefElements bitvector, it will resize it to match
  1670. /// the vector width and set the bits where elements are undef.
  1671. SDValue getSplatValue(const APInt &DemandedElts,
  1672. BitVector *UndefElements = nullptr) const;
  1673. /// Returns the splatted value or a null value if this is not a splat.
  1674. ///
  1675. /// If passed a non-null UndefElements bitvector, it will resize it to match
  1676. /// the vector width and set the bits where elements are undef.
  1677. SDValue getSplatValue(BitVector *UndefElements = nullptr) const;
  1678. /// Find the shortest repeating sequence of values in the build vector.
  1679. ///
  1680. /// e.g. { u, X, u, X, u, u, X, u } -> { X }
  1681. /// { X, Y, u, Y, u, u, X, u } -> { X, Y }
  1682. ///
  1683. /// Currently this must be a power-of-2 build vector.
  1684. /// The DemandedElts mask indicates the elements that must be present,
  1685. /// undemanded elements in Sequence may be null (SDValue()). If passed a
  1686. /// non-null UndefElements bitvector, it will resize it to match the original
  1687. /// vector width and set the bits where elements are undef. If result is
  1688. /// false, Sequence will be empty.
  1689. bool getRepeatedSequence(const APInt &DemandedElts,
  1690. SmallVectorImpl<SDValue> &Sequence,
  1691. BitVector *UndefElements = nullptr) const;
  1692. /// Find the shortest repeating sequence of values in the build vector.
  1693. ///
  1694. /// e.g. { u, X, u, X, u, u, X, u } -> { X }
  1695. /// { X, Y, u, Y, u, u, X, u } -> { X, Y }
  1696. ///
  1697. /// Currently this must be a power-of-2 build vector.
  1698. /// If passed a non-null UndefElements bitvector, it will resize it to match
  1699. /// the original vector width and set the bits where elements are undef.
  1700. /// If result is false, Sequence will be empty.
  1701. bool getRepeatedSequence(SmallVectorImpl<SDValue> &Sequence,
  1702. BitVector *UndefElements = nullptr) const;
  1703. /// Returns the demanded splatted constant or null if this is not a constant
  1704. /// splat.
  1705. ///
  1706. /// The DemandedElts mask indicates the elements that must be in the splat.
  1707. /// If passed a non-null UndefElements bitvector, it will resize it to match
  1708. /// the vector width and set the bits where elements are undef.
  1709. ConstantSDNode *
  1710. getConstantSplatNode(const APInt &DemandedElts,
  1711. BitVector *UndefElements = nullptr) const;
  1712. /// Returns the splatted constant or null if this is not a constant
  1713. /// splat.
  1714. ///
  1715. /// If passed a non-null UndefElements bitvector, it will resize it to match
  1716. /// the vector width and set the bits where elements are undef.
  1717. ConstantSDNode *
  1718. getConstantSplatNode(BitVector *UndefElements = nullptr) const;
  1719. /// Returns the demanded splatted constant FP or null if this is not a
  1720. /// constant FP splat.
  1721. ///
  1722. /// The DemandedElts mask indicates the elements that must be in the splat.
  1723. /// If passed a non-null UndefElements bitvector, it will resize it to match
  1724. /// the vector width and set the bits where elements are undef.
  1725. ConstantFPSDNode *
  1726. getConstantFPSplatNode(const APInt &DemandedElts,
  1727. BitVector *UndefElements = nullptr) const;
  1728. /// Returns the splatted constant FP or null if this is not a constant
  1729. /// FP splat.
  1730. ///
  1731. /// If passed a non-null UndefElements bitvector, it will resize it to match
  1732. /// the vector width and set the bits where elements are undef.
  1733. ConstantFPSDNode *
  1734. getConstantFPSplatNode(BitVector *UndefElements = nullptr) const;
  1735. /// If this is a constant FP splat and the splatted constant FP is an
  1736. /// exact power or 2, return the log base 2 integer value. Otherwise,
  1737. /// return -1.
  1738. ///
  1739. /// The BitWidth specifies the necessary bit precision.
  1740. int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements,
  1741. uint32_t BitWidth) const;
  1742. /// Extract the raw bit data from a build vector of Undef, Constant or
  1743. /// ConstantFP node elements. Each raw bit element will be \p
  1744. /// DstEltSizeInBits wide, undef elements are treated as zero, and entirely
  1745. /// undefined elements are flagged in \p UndefElements.
  1746. bool getConstantRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits,
  1747. SmallVectorImpl<APInt> &RawBitElements,
  1748. BitVector &UndefElements) const;
  1749. bool isConstant() const;
  1750. /// If this BuildVector is constant and represents the numerical series
  1751. /// "<a, a+n, a+2n, a+3n, ...>" where a is integer and n is a non-zero integer,
  1752. /// the value "<a,n>" is returned.
  1753. std::optional<std::pair<APInt, APInt>> isConstantSequence() const;
  1754. /// Recast bit data \p SrcBitElements to \p DstEltSizeInBits wide elements.
  1755. /// Undef elements are treated as zero, and entirely undefined elements are
  1756. /// flagged in \p DstUndefElements.
  1757. static void recastRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits,
  1758. SmallVectorImpl<APInt> &DstBitElements,
  1759. ArrayRef<APInt> SrcBitElements,
  1760. BitVector &DstUndefElements,
  1761. const BitVector &SrcUndefElements);
  1762. static bool classof(const SDNode *N) {
  1763. return N->getOpcode() == ISD::BUILD_VECTOR;
  1764. }
  1765. };
  1766. /// An SDNode that holds an arbitrary LLVM IR Value. This is
  1767. /// used when the SelectionDAG needs to make a simple reference to something
  1768. /// in the LLVM IR representation.
  1769. ///
  1770. class SrcValueSDNode : public SDNode {
  1771. friend class SelectionDAG;
  1772. const Value *V;
  1773. /// Create a SrcValue for a general value.
  1774. explicit SrcValueSDNode(const Value *v)
  1775. : SDNode(ISD::SRCVALUE, 0, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
  1776. public:
  1777. /// Return the contained Value.
  1778. const Value *getValue() const { return V; }
  1779. static bool classof(const SDNode *N) {
  1780. return N->getOpcode() == ISD::SRCVALUE;
  1781. }
  1782. };
  1783. class MDNodeSDNode : public SDNode {
  1784. friend class SelectionDAG;
  1785. const MDNode *MD;
  1786. explicit MDNodeSDNode(const MDNode *md)
  1787. : SDNode(ISD::MDNODE_SDNODE, 0, DebugLoc(), getSDVTList(MVT::Other)), MD(md)
  1788. {}
  1789. public:
  1790. const MDNode *getMD() const { return MD; }
  1791. static bool classof(const SDNode *N) {
  1792. return N->getOpcode() == ISD::MDNODE_SDNODE;
  1793. }
  1794. };
  1795. class RegisterSDNode : public SDNode {
  1796. friend class SelectionDAG;
  1797. Register Reg;
  1798. RegisterSDNode(Register reg, EVT VT)
  1799. : SDNode(ISD::Register, 0, DebugLoc(), getSDVTList(VT)), Reg(reg) {}
  1800. public:
  1801. Register getReg() const { return Reg; }
  1802. static bool classof(const SDNode *N) {
  1803. return N->getOpcode() == ISD::Register;
  1804. }
  1805. };
  1806. class RegisterMaskSDNode : public SDNode {
  1807. friend class SelectionDAG;
  1808. // The memory for RegMask is not owned by the node.
  1809. const uint32_t *RegMask;
  1810. RegisterMaskSDNode(const uint32_t *mask)
  1811. : SDNode(ISD::RegisterMask, 0, DebugLoc(), getSDVTList(MVT::Untyped)),
  1812. RegMask(mask) {}
  1813. public:
  1814. const uint32_t *getRegMask() const { return RegMask; }
  1815. static bool classof(const SDNode *N) {
  1816. return N->getOpcode() == ISD::RegisterMask;
  1817. }
  1818. };
  1819. class BlockAddressSDNode : public SDNode {
  1820. friend class SelectionDAG;
  1821. const BlockAddress *BA;
  1822. int64_t Offset;
  1823. unsigned TargetFlags;
  1824. BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
  1825. int64_t o, unsigned Flags)
  1826. : SDNode(NodeTy, 0, DebugLoc(), getSDVTList(VT)),
  1827. BA(ba), Offset(o), TargetFlags(Flags) {}
  1828. public:
  1829. const BlockAddress *getBlockAddress() const { return BA; }
  1830. int64_t getOffset() const { return Offset; }
  1831. unsigned getTargetFlags() const { return TargetFlags; }
  1832. static bool classof(const SDNode *N) {
  1833. return N->getOpcode() == ISD::BlockAddress ||
  1834. N->getOpcode() == ISD::TargetBlockAddress;
  1835. }
  1836. };
  1837. class LabelSDNode : public SDNode {
  1838. friend class SelectionDAG;
  1839. MCSymbol *Label;
  1840. LabelSDNode(unsigned Opcode, unsigned Order, const DebugLoc &dl, MCSymbol *L)
  1841. : SDNode(Opcode, Order, dl, getSDVTList(MVT::Other)), Label(L) {
  1842. assert(LabelSDNode::classof(this) && "not a label opcode");
  1843. }
  1844. public:
  1845. MCSymbol *getLabel() const { return Label; }
  1846. static bool classof(const SDNode *N) {
  1847. return N->getOpcode() == ISD::EH_LABEL ||
  1848. N->getOpcode() == ISD::ANNOTATION_LABEL;
  1849. }
  1850. };
  1851. class ExternalSymbolSDNode : public SDNode {
  1852. friend class SelectionDAG;
  1853. const char *Symbol;
  1854. unsigned TargetFlags;
  1855. ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned TF, EVT VT)
  1856. : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol, 0,
  1857. DebugLoc(), getSDVTList(VT)),
  1858. Symbol(Sym), TargetFlags(TF) {}
  1859. public:
  1860. const char *getSymbol() const { return Symbol; }
  1861. unsigned getTargetFlags() const { return TargetFlags; }
  1862. static bool classof(const SDNode *N) {
  1863. return N->getOpcode() == ISD::ExternalSymbol ||
  1864. N->getOpcode() == ISD::TargetExternalSymbol;
  1865. }
  1866. };
  1867. class MCSymbolSDNode : public SDNode {
  1868. friend class SelectionDAG;
  1869. MCSymbol *Symbol;
  1870. MCSymbolSDNode(MCSymbol *Symbol, EVT VT)
  1871. : SDNode(ISD::MCSymbol, 0, DebugLoc(), getSDVTList(VT)), Symbol(Symbol) {}
  1872. public:
  1873. MCSymbol *getMCSymbol() const { return Symbol; }
  1874. static bool classof(const SDNode *N) {
  1875. return N->getOpcode() == ISD::MCSymbol;
  1876. }
  1877. };
  1878. class CondCodeSDNode : public SDNode {
  1879. friend class SelectionDAG;
  1880. ISD::CondCode Condition;
  1881. explicit CondCodeSDNode(ISD::CondCode Cond)
  1882. : SDNode(ISD::CONDCODE, 0, DebugLoc(), getSDVTList(MVT::Other)),
  1883. Condition(Cond) {}
  1884. public:
  1885. ISD::CondCode get() const { return Condition; }
  1886. static bool classof(const SDNode *N) {
  1887. return N->getOpcode() == ISD::CONDCODE;
  1888. }
  1889. };
  1890. /// This class is used to represent EVT's, which are used
  1891. /// to parameterize some operations.
  1892. class VTSDNode : public SDNode {
  1893. friend class SelectionDAG;
  1894. EVT ValueType;
  1895. explicit VTSDNode(EVT VT)
  1896. : SDNode(ISD::VALUETYPE, 0, DebugLoc(), getSDVTList(MVT::Other)),
  1897. ValueType(VT) {}
  1898. public:
  1899. EVT getVT() const { return ValueType; }
  1900. static bool classof(const SDNode *N) {
  1901. return N->getOpcode() == ISD::VALUETYPE;
  1902. }
  1903. };
  1904. /// Base class for LoadSDNode and StoreSDNode
  1905. class LSBaseSDNode : public MemSDNode {
  1906. public:
  1907. LSBaseSDNode(ISD::NodeType NodeTy, unsigned Order, const DebugLoc &dl,
  1908. SDVTList VTs, ISD::MemIndexedMode AM, EVT MemVT,
  1909. MachineMemOperand *MMO)
  1910. : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
  1911. LSBaseSDNodeBits.AddressingMode = AM;
  1912. assert(getAddressingMode() == AM && "Value truncated");
  1913. }
  1914. const SDValue &getOffset() const {
  1915. return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
  1916. }
  1917. /// Return the addressing mode for this load or store:
  1918. /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
  1919. ISD::MemIndexedMode getAddressingMode() const {
  1920. return static_cast<ISD::MemIndexedMode>(LSBaseSDNodeBits.AddressingMode);
  1921. }
  1922. /// Return true if this is a pre/post inc/dec load/store.
  1923. bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
  1924. /// Return true if this is NOT a pre/post inc/dec load/store.
  1925. bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
  1926. static bool classof(const SDNode *N) {
  1927. return N->getOpcode() == ISD::LOAD ||
  1928. N->getOpcode() == ISD::STORE;
  1929. }
  1930. };
  1931. /// This class is used to represent ISD::LOAD nodes.
  1932. class LoadSDNode : public LSBaseSDNode {
  1933. friend class SelectionDAG;
  1934. LoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
  1935. ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
  1936. MachineMemOperand *MMO)
  1937. : LSBaseSDNode(ISD::LOAD, Order, dl, VTs, AM, MemVT, MMO) {
  1938. LoadSDNodeBits.ExtTy = ETy;
  1939. assert(readMem() && "Load MachineMemOperand is not a load!");
  1940. assert(!writeMem() && "Load MachineMemOperand is a store!");
  1941. }
  1942. public:
  1943. /// Return whether this is a plain node,
  1944. /// or one of the varieties of value-extending loads.
  1945. ISD::LoadExtType getExtensionType() const {
  1946. return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy);
  1947. }
  1948. const SDValue &getBasePtr() const { return getOperand(1); }
  1949. const SDValue &getOffset() const { return getOperand(2); }
  1950. static bool classof(const SDNode *N) {
  1951. return N->getOpcode() == ISD::LOAD;
  1952. }
  1953. };
  1954. /// This class is used to represent ISD::STORE nodes.
  1955. class StoreSDNode : public LSBaseSDNode {
  1956. friend class SelectionDAG;
  1957. StoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
  1958. ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
  1959. MachineMemOperand *MMO)
  1960. : LSBaseSDNode(ISD::STORE, Order, dl, VTs, AM, MemVT, MMO) {
  1961. StoreSDNodeBits.IsTruncating = isTrunc;
  1962. assert(!readMem() && "Store MachineMemOperand is a load!");
  1963. assert(writeMem() && "Store MachineMemOperand is not a store!");
  1964. }
  1965. public:
  1966. /// Return true if the op does a truncation before store.
  1967. /// For integers this is the same as doing a TRUNCATE and storing the result.
  1968. /// For floats, it is the same as doing an FP_ROUND and storing the result.
  1969. bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
  1970. void setTruncatingStore(bool Truncating) {
  1971. StoreSDNodeBits.IsTruncating = Truncating;
  1972. }
  1973. const SDValue &getValue() const { return getOperand(1); }
  1974. const SDValue &getBasePtr() const { return getOperand(2); }
  1975. const SDValue &getOffset() const { return getOperand(3); }
  1976. static bool classof(const SDNode *N) {
  1977. return N->getOpcode() == ISD::STORE;
  1978. }
  1979. };
  1980. /// This base class is used to represent VP_LOAD, VP_STORE,
  1981. /// EXPERIMENTAL_VP_STRIDED_LOAD and EXPERIMENTAL_VP_STRIDED_STORE nodes
  1982. class VPBaseLoadStoreSDNode : public MemSDNode {
  1983. public:
  1984. friend class SelectionDAG;
  1985. VPBaseLoadStoreSDNode(ISD::NodeType NodeTy, unsigned Order,
  1986. const DebugLoc &DL, SDVTList VTs,
  1987. ISD::MemIndexedMode AM, EVT MemVT,
  1988. MachineMemOperand *MMO)
  1989. : MemSDNode(NodeTy, Order, DL, VTs, MemVT, MMO) {
  1990. LSBaseSDNodeBits.AddressingMode = AM;
  1991. assert(getAddressingMode() == AM && "Value truncated");
  1992. }
  1993. // VPStridedStoreSDNode (Chain, Data, Ptr, Offset, Stride, Mask, EVL)
  1994. // VPStoreSDNode (Chain, Data, Ptr, Offset, Mask, EVL)
  1995. // VPStridedLoadSDNode (Chain, Ptr, Offset, Stride, Mask, EVL)
  1996. // VPLoadSDNode (Chain, Ptr, Offset, Mask, EVL)
  1997. // Mask is a vector of i1 elements;
  1998. // the type of EVL is TLI.getVPExplicitVectorLengthTy().
  1999. const SDValue &getOffset() const {
  2000. return getOperand((getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD ||
  2001. getOpcode() == ISD::VP_LOAD)
  2002. ? 2
  2003. : 3);
  2004. }
  2005. const SDValue &getBasePtr() const {
  2006. return getOperand((getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD ||
  2007. getOpcode() == ISD::VP_LOAD)
  2008. ? 1
  2009. : 2);
  2010. }
  2011. const SDValue &getMask() const {
  2012. switch (getOpcode()) {
  2013. default:
  2014. llvm_unreachable("Invalid opcode");
  2015. case ISD::VP_LOAD:
  2016. return getOperand(3);
  2017. case ISD::VP_STORE:
  2018. case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
  2019. return getOperand(4);
  2020. case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
  2021. return getOperand(5);
  2022. }
  2023. }
  2024. const SDValue &getVectorLength() const {
  2025. switch (getOpcode()) {
  2026. default:
  2027. llvm_unreachable("Invalid opcode");
  2028. case ISD::VP_LOAD:
  2029. return getOperand(4);
  2030. case ISD::VP_STORE:
  2031. case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
  2032. return getOperand(5);
  2033. case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
  2034. return getOperand(6);
  2035. }
  2036. }
  2037. /// Return the addressing mode for this load or store:
  2038. /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
  2039. ISD::MemIndexedMode getAddressingMode() const {
  2040. return static_cast<ISD::MemIndexedMode>(LSBaseSDNodeBits.AddressingMode);
  2041. }
  2042. /// Return true if this is a pre/post inc/dec load/store.
  2043. bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
  2044. /// Return true if this is NOT a pre/post inc/dec load/store.
  2045. bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
  2046. static bool classof(const SDNode *N) {
  2047. return N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD ||
  2048. N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_STORE ||
  2049. N->getOpcode() == ISD::VP_LOAD || N->getOpcode() == ISD::VP_STORE;
  2050. }
  2051. };
  2052. /// This class is used to represent a VP_LOAD node
  2053. class VPLoadSDNode : public VPBaseLoadStoreSDNode {
  2054. public:
  2055. friend class SelectionDAG;
  2056. VPLoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
  2057. ISD::MemIndexedMode AM, ISD::LoadExtType ETy, bool isExpanding,
  2058. EVT MemVT, MachineMemOperand *MMO)
  2059. : VPBaseLoadStoreSDNode(ISD::VP_LOAD, Order, dl, VTs, AM, MemVT, MMO) {
  2060. LoadSDNodeBits.ExtTy = ETy;
  2061. LoadSDNodeBits.IsExpanding = isExpanding;
  2062. }
  2063. ISD::LoadExtType getExtensionType() const {
  2064. return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy);
  2065. }
  2066. const SDValue &getBasePtr() const { return getOperand(1); }
  2067. const SDValue &getOffset() const { return getOperand(2); }
  2068. const SDValue &getMask() const { return getOperand(3); }
  2069. const SDValue &getVectorLength() const { return getOperand(4); }
  2070. static bool classof(const SDNode *N) {
  2071. return N->getOpcode() == ISD::VP_LOAD;
  2072. }
  2073. bool isExpandingLoad() const { return LoadSDNodeBits.IsExpanding; }
  2074. };
  2075. /// This class is used to represent an EXPERIMENTAL_VP_STRIDED_LOAD node.
  2076. class VPStridedLoadSDNode : public VPBaseLoadStoreSDNode {
  2077. public:
  2078. friend class SelectionDAG;
  2079. VPStridedLoadSDNode(unsigned Order, const DebugLoc &DL, SDVTList VTs,
  2080. ISD::MemIndexedMode AM, ISD::LoadExtType ETy,
  2081. bool IsExpanding, EVT MemVT, MachineMemOperand *MMO)
  2082. : VPBaseLoadStoreSDNode(ISD::EXPERIMENTAL_VP_STRIDED_LOAD, Order, DL, VTs,
  2083. AM, MemVT, MMO) {
  2084. LoadSDNodeBits.ExtTy = ETy;
  2085. LoadSDNodeBits.IsExpanding = IsExpanding;
  2086. }
  2087. ISD::LoadExtType getExtensionType() const {
  2088. return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy);
  2089. }
  2090. const SDValue &getBasePtr() const { return getOperand(1); }
  2091. const SDValue &getOffset() const { return getOperand(2); }
  2092. const SDValue &getStride() const { return getOperand(3); }
  2093. const SDValue &getMask() const { return getOperand(4); }
  2094. const SDValue &getVectorLength() const { return getOperand(5); }
  2095. static bool classof(const SDNode *N) {
  2096. return N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD;
  2097. }
  2098. bool isExpandingLoad() const { return LoadSDNodeBits.IsExpanding; }
  2099. };
  2100. /// This class is used to represent a VP_STORE node
  2101. class VPStoreSDNode : public VPBaseLoadStoreSDNode {
  2102. public:
  2103. friend class SelectionDAG;
  2104. VPStoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
  2105. ISD::MemIndexedMode AM, bool isTrunc, bool isCompressing,
  2106. EVT MemVT, MachineMemOperand *MMO)
  2107. : VPBaseLoadStoreSDNode(ISD::VP_STORE, Order, dl, VTs, AM, MemVT, MMO) {
  2108. StoreSDNodeBits.IsTruncating = isTrunc;
  2109. StoreSDNodeBits.IsCompressing = isCompressing;
  2110. }
  2111. /// Return true if this is a truncating store.
  2112. /// For integers this is the same as doing a TRUNCATE and storing the result.
  2113. /// For floats, it is the same as doing an FP_ROUND and storing the result.
  2114. bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
  2115. /// Returns true if the op does a compression to the vector before storing.
  2116. /// The node contiguously stores the active elements (integers or floats)
  2117. /// in src (those with their respective bit set in writemask k) to unaligned
  2118. /// memory at base_addr.
  2119. bool isCompressingStore() const { return StoreSDNodeBits.IsCompressing; }
  2120. const SDValue &getValue() const { return getOperand(1); }
  2121. const SDValue &getBasePtr() const { return getOperand(2); }
  2122. const SDValue &getOffset() const { return getOperand(3); }
  2123. const SDValue &getMask() const { return getOperand(4); }
  2124. const SDValue &getVectorLength() const { return getOperand(5); }
  2125. static bool classof(const SDNode *N) {
  2126. return N->getOpcode() == ISD::VP_STORE;
  2127. }
  2128. };
  2129. /// This class is used to represent an EXPERIMENTAL_VP_STRIDED_STORE node.
  2130. class VPStridedStoreSDNode : public VPBaseLoadStoreSDNode {
  2131. public:
  2132. friend class SelectionDAG;
  2133. VPStridedStoreSDNode(unsigned Order, const DebugLoc &DL, SDVTList VTs,
  2134. ISD::MemIndexedMode AM, bool IsTrunc, bool IsCompressing,
  2135. EVT MemVT, MachineMemOperand *MMO)
  2136. : VPBaseLoadStoreSDNode(ISD::EXPERIMENTAL_VP_STRIDED_STORE, Order, DL,
  2137. VTs, AM, MemVT, MMO) {
  2138. StoreSDNodeBits.IsTruncating = IsTrunc;
  2139. StoreSDNodeBits.IsCompressing = IsCompressing;
  2140. }
  2141. /// Return true if this is a truncating store.
  2142. /// For integers this is the same as doing a TRUNCATE and storing the result.
  2143. /// For floats, it is the same as doing an FP_ROUND and storing the result.
  2144. bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
  2145. /// Returns true if the op does a compression to the vector before storing.
  2146. /// The node contiguously stores the active elements (integers or floats)
  2147. /// in src (those with their respective bit set in writemask k) to unaligned
  2148. /// memory at base_addr.
  2149. bool isCompressingStore() const { return StoreSDNodeBits.IsCompressing; }
  2150. const SDValue &getValue() const { return getOperand(1); }
  2151. const SDValue &getBasePtr() const { return getOperand(2); }
  2152. const SDValue &getOffset() const { return getOperand(3); }
  2153. const SDValue &getStride() const { return getOperand(4); }
  2154. const SDValue &getMask() const { return getOperand(5); }
  2155. const SDValue &getVectorLength() const { return getOperand(6); }
  2156. static bool classof(const SDNode *N) {
  2157. return N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_STORE;
  2158. }
  2159. };
  2160. /// This base class is used to represent MLOAD and MSTORE nodes
  2161. class MaskedLoadStoreSDNode : public MemSDNode {
  2162. public:
  2163. friend class SelectionDAG;
  2164. MaskedLoadStoreSDNode(ISD::NodeType NodeTy, unsigned Order,
  2165. const DebugLoc &dl, SDVTList VTs,
  2166. ISD::MemIndexedMode AM, EVT MemVT,
  2167. MachineMemOperand *MMO)
  2168. : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
  2169. LSBaseSDNodeBits.AddressingMode = AM;
  2170. assert(getAddressingMode() == AM && "Value truncated");
  2171. }
  2172. // MaskedLoadSDNode (Chain, ptr, offset, mask, passthru)
  2173. // MaskedStoreSDNode (Chain, data, ptr, offset, mask)
  2174. // Mask is a vector of i1 elements
  2175. const SDValue &getOffset() const {
  2176. return getOperand(getOpcode() == ISD::MLOAD ? 2 : 3);
  2177. }
  2178. const SDValue &getMask() const {
  2179. return getOperand(getOpcode() == ISD::MLOAD ? 3 : 4);
  2180. }
  2181. /// Return the addressing mode for this load or store:
  2182. /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
  2183. ISD::MemIndexedMode getAddressingMode() const {
  2184. return static_cast<ISD::MemIndexedMode>(LSBaseSDNodeBits.AddressingMode);
  2185. }
  2186. /// Return true if this is a pre/post inc/dec load/store.
  2187. bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
  2188. /// Return true if this is NOT a pre/post inc/dec load/store.
  2189. bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
  2190. static bool classof(const SDNode *N) {
  2191. return N->getOpcode() == ISD::MLOAD ||
  2192. N->getOpcode() == ISD::MSTORE;
  2193. }
  2194. };
  2195. /// This class is used to represent an MLOAD node
  2196. class MaskedLoadSDNode : public MaskedLoadStoreSDNode {
  2197. public:
  2198. friend class SelectionDAG;
  2199. MaskedLoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
  2200. ISD::MemIndexedMode AM, ISD::LoadExtType ETy,
  2201. bool IsExpanding, EVT MemVT, MachineMemOperand *MMO)
  2202. : MaskedLoadStoreSDNode(ISD::MLOAD, Order, dl, VTs, AM, MemVT, MMO) {
  2203. LoadSDNodeBits.ExtTy = ETy;
  2204. LoadSDNodeBits.IsExpanding = IsExpanding;
  2205. }
  2206. ISD::LoadExtType getExtensionType() const {
  2207. return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy);
  2208. }
  2209. const SDValue &getBasePtr() const { return getOperand(1); }
  2210. const SDValue &getOffset() const { return getOperand(2); }
  2211. const SDValue &getMask() const { return getOperand(3); }
  2212. const SDValue &getPassThru() const { return getOperand(4); }
  2213. static bool classof(const SDNode *N) {
  2214. return N->getOpcode() == ISD::MLOAD;
  2215. }
  2216. bool isExpandingLoad() const { return LoadSDNodeBits.IsExpanding; }
  2217. };
  2218. /// This class is used to represent an MSTORE node
  2219. class MaskedStoreSDNode : public MaskedLoadStoreSDNode {
  2220. public:
  2221. friend class SelectionDAG;
  2222. MaskedStoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
  2223. ISD::MemIndexedMode AM, bool isTrunc, bool isCompressing,
  2224. EVT MemVT, MachineMemOperand *MMO)
  2225. : MaskedLoadStoreSDNode(ISD::MSTORE, Order, dl, VTs, AM, MemVT, MMO) {
  2226. StoreSDNodeBits.IsTruncating = isTrunc;
  2227. StoreSDNodeBits.IsCompressing = isCompressing;
  2228. }
  2229. /// Return true if the op does a truncation before store.
  2230. /// For integers this is the same as doing a TRUNCATE and storing the result.
  2231. /// For floats, it is the same as doing an FP_ROUND and storing the result.
  2232. bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
  2233. /// Returns true if the op does a compression to the vector before storing.
  2234. /// The node contiguously stores the active elements (integers or floats)
  2235. /// in src (those with their respective bit set in writemask k) to unaligned
  2236. /// memory at base_addr.
  2237. bool isCompressingStore() const { return StoreSDNodeBits.IsCompressing; }
  2238. const SDValue &getValue() const { return getOperand(1); }
  2239. const SDValue &getBasePtr() const { return getOperand(2); }
  2240. const SDValue &getOffset() const { return getOperand(3); }
  2241. const SDValue &getMask() const { return getOperand(4); }
  2242. static bool classof(const SDNode *N) {
  2243. return N->getOpcode() == ISD::MSTORE;
  2244. }
  2245. };
  2246. /// This is a base class used to represent
  2247. /// VP_GATHER and VP_SCATTER nodes
  2248. ///
  2249. class VPGatherScatterSDNode : public MemSDNode {
  2250. public:
  2251. friend class SelectionDAG;
  2252. VPGatherScatterSDNode(ISD::NodeType NodeTy, unsigned Order,
  2253. const DebugLoc &dl, SDVTList VTs, EVT MemVT,
  2254. MachineMemOperand *MMO, ISD::MemIndexType IndexType)
  2255. : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
  2256. LSBaseSDNodeBits.AddressingMode = IndexType;
  2257. assert(getIndexType() == IndexType && "Value truncated");
  2258. }
  2259. /// How is Index applied to BasePtr when computing addresses.
  2260. ISD::MemIndexType getIndexType() const {
  2261. return static_cast<ISD::MemIndexType>(LSBaseSDNodeBits.AddressingMode);
  2262. }
  2263. bool isIndexScaled() const {
  2264. return !cast<ConstantSDNode>(getScale())->isOne();
  2265. }
  2266. bool isIndexSigned() const { return isIndexTypeSigned(getIndexType()); }
  2267. // In the both nodes address is Op1, mask is Op2:
  2268. // VPGatherSDNode (Chain, base, index, scale, mask, vlen)
  2269. // VPScatterSDNode (Chain, value, base, index, scale, mask, vlen)
  2270. // Mask is a vector of i1 elements
  2271. const SDValue &getBasePtr() const {
  2272. return getOperand((getOpcode() == ISD::VP_GATHER) ? 1 : 2);
  2273. }
  2274. const SDValue &getIndex() const {
  2275. return getOperand((getOpcode() == ISD::VP_GATHER) ? 2 : 3);
  2276. }
  2277. const SDValue &getScale() const {
  2278. return getOperand((getOpcode() == ISD::VP_GATHER) ? 3 : 4);
  2279. }
  2280. const SDValue &getMask() const {
  2281. return getOperand((getOpcode() == ISD::VP_GATHER) ? 4 : 5);
  2282. }
  2283. const SDValue &getVectorLength() const {
  2284. return getOperand((getOpcode() == ISD::VP_GATHER) ? 5 : 6);
  2285. }
  2286. static bool classof(const SDNode *N) {
  2287. return N->getOpcode() == ISD::VP_GATHER ||
  2288. N->getOpcode() == ISD::VP_SCATTER;
  2289. }
  2290. };
  2291. /// This class is used to represent an VP_GATHER node
  2292. ///
  2293. class VPGatherSDNode : public VPGatherScatterSDNode {
  2294. public:
  2295. friend class SelectionDAG;
  2296. VPGatherSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT MemVT,
  2297. MachineMemOperand *MMO, ISD::MemIndexType IndexType)
  2298. : VPGatherScatterSDNode(ISD::VP_GATHER, Order, dl, VTs, MemVT, MMO,
  2299. IndexType) {}
  2300. static bool classof(const SDNode *N) {
  2301. return N->getOpcode() == ISD::VP_GATHER;
  2302. }
  2303. };
  2304. /// This class is used to represent an VP_SCATTER node
  2305. ///
  2306. class VPScatterSDNode : public VPGatherScatterSDNode {
  2307. public:
  2308. friend class SelectionDAG;
  2309. VPScatterSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT MemVT,
  2310. MachineMemOperand *MMO, ISD::MemIndexType IndexType)
  2311. : VPGatherScatterSDNode(ISD::VP_SCATTER, Order, dl, VTs, MemVT, MMO,
  2312. IndexType) {}
  2313. const SDValue &getValue() const { return getOperand(1); }
  2314. static bool classof(const SDNode *N) {
  2315. return N->getOpcode() == ISD::VP_SCATTER;
  2316. }
  2317. };
  2318. /// This is a base class used to represent
  2319. /// MGATHER and MSCATTER nodes
  2320. ///
  2321. class MaskedGatherScatterSDNode : public MemSDNode {
  2322. public:
  2323. friend class SelectionDAG;
  2324. MaskedGatherScatterSDNode(ISD::NodeType NodeTy, unsigned Order,
  2325. const DebugLoc &dl, SDVTList VTs, EVT MemVT,
  2326. MachineMemOperand *MMO, ISD::MemIndexType IndexType)
  2327. : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
  2328. LSBaseSDNodeBits.AddressingMode = IndexType;
  2329. assert(getIndexType() == IndexType && "Value truncated");
  2330. }
  2331. /// How is Index applied to BasePtr when computing addresses.
  2332. ISD::MemIndexType getIndexType() const {
  2333. return static_cast<ISD::MemIndexType>(LSBaseSDNodeBits.AddressingMode);
  2334. }
  2335. bool isIndexScaled() const {
  2336. return !cast<ConstantSDNode>(getScale())->isOne();
  2337. }
  2338. bool isIndexSigned() const { return isIndexTypeSigned(getIndexType()); }
  2339. // In the both nodes address is Op1, mask is Op2:
  2340. // MaskedGatherSDNode (Chain, passthru, mask, base, index, scale)
  2341. // MaskedScatterSDNode (Chain, value, mask, base, index, scale)
  2342. // Mask is a vector of i1 elements
  2343. const SDValue &getBasePtr() const { return getOperand(3); }
  2344. const SDValue &getIndex() const { return getOperand(4); }
  2345. const SDValue &getMask() const { return getOperand(2); }
  2346. const SDValue &getScale() const { return getOperand(5); }
  2347. static bool classof(const SDNode *N) {
  2348. return N->getOpcode() == ISD::MGATHER ||
  2349. N->getOpcode() == ISD::MSCATTER;
  2350. }
  2351. };
  2352. /// This class is used to represent an MGATHER node
  2353. ///
  2354. class MaskedGatherSDNode : public MaskedGatherScatterSDNode {
  2355. public:
  2356. friend class SelectionDAG;
  2357. MaskedGatherSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
  2358. EVT MemVT, MachineMemOperand *MMO,
  2359. ISD::MemIndexType IndexType, ISD::LoadExtType ETy)
  2360. : MaskedGatherScatterSDNode(ISD::MGATHER, Order, dl, VTs, MemVT, MMO,
  2361. IndexType) {
  2362. LoadSDNodeBits.ExtTy = ETy;
  2363. }
  2364. const SDValue &getPassThru() const { return getOperand(1); }
  2365. ISD::LoadExtType getExtensionType() const {
  2366. return ISD::LoadExtType(LoadSDNodeBits.ExtTy);
  2367. }
  2368. static bool classof(const SDNode *N) {
  2369. return N->getOpcode() == ISD::MGATHER;
  2370. }
  2371. };
  2372. /// This class is used to represent an MSCATTER node
  2373. ///
  2374. class MaskedScatterSDNode : public MaskedGatherScatterSDNode {
  2375. public:
  2376. friend class SelectionDAG;
  2377. MaskedScatterSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
  2378. EVT MemVT, MachineMemOperand *MMO,
  2379. ISD::MemIndexType IndexType, bool IsTrunc)
  2380. : MaskedGatherScatterSDNode(ISD::MSCATTER, Order, dl, VTs, MemVT, MMO,
  2381. IndexType) {
  2382. StoreSDNodeBits.IsTruncating = IsTrunc;
  2383. }
  2384. /// Return true if the op does a truncation before store.
  2385. /// For integers this is the same as doing a TRUNCATE and storing the result.
  2386. /// For floats, it is the same as doing an FP_ROUND and storing the result.
  2387. bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
  2388. const SDValue &getValue() const { return getOperand(1); }
  2389. static bool classof(const SDNode *N) {
  2390. return N->getOpcode() == ISD::MSCATTER;
  2391. }
  2392. };
  2393. /// An SDNode that represents everything that will be needed
  2394. /// to construct a MachineInstr. These nodes are created during the
  2395. /// instruction selection proper phase.
  2396. ///
  2397. /// Note that the only supported way to set the `memoperands` is by calling the
  2398. /// `SelectionDAG::setNodeMemRefs` function as the memory management happens
  2399. /// inside the DAG rather than in the node.
  2400. class MachineSDNode : public SDNode {
  2401. private:
  2402. friend class SelectionDAG;
  2403. MachineSDNode(unsigned Opc, unsigned Order, const DebugLoc &DL, SDVTList VTs)
  2404. : SDNode(Opc, Order, DL, VTs) {}
  2405. // We use a pointer union between a single `MachineMemOperand` pointer and
  2406. // a pointer to an array of `MachineMemOperand` pointers. This is null when
  2407. // the number of these is zero, the single pointer variant used when the
  2408. // number is one, and the array is used for larger numbers.
  2409. //
  2410. // The array is allocated via the `SelectionDAG`'s allocator and so will
  2411. // always live until the DAG is cleaned up and doesn't require ownership here.
  2412. //
  2413. // We can't use something simpler like `TinyPtrVector` here because `SDNode`
  2414. // subclasses aren't managed in a conforming C++ manner. See the comments on
  2415. // `SelectionDAG::MorphNodeTo` which details what all goes on, but the
  2416. // constraint here is that these don't manage memory with their constructor or
  2417. // destructor and can be initialized to a good state even if they start off
  2418. // uninitialized.
  2419. PointerUnion<MachineMemOperand *, MachineMemOperand **> MemRefs = {};
  2420. // Note that this could be folded into the above `MemRefs` member if doing so
  2421. // is advantageous at some point. We don't need to store this in most cases.
  2422. // However, at the moment this doesn't appear to make the allocation any
  2423. // smaller and makes the code somewhat simpler to read.
  2424. int NumMemRefs = 0;
  2425. public:
  2426. using mmo_iterator = ArrayRef<MachineMemOperand *>::const_iterator;
  2427. ArrayRef<MachineMemOperand *> memoperands() const {
  2428. // Special case the common cases.
  2429. if (NumMemRefs == 0)
  2430. return {};
  2431. if (NumMemRefs == 1)
  2432. return ArrayRef(MemRefs.getAddrOfPtr1(), 1);
  2433. // Otherwise we have an actual array.
  2434. return ArrayRef(MemRefs.get<MachineMemOperand **>(), NumMemRefs);
  2435. }
  2436. mmo_iterator memoperands_begin() const { return memoperands().begin(); }
  2437. mmo_iterator memoperands_end() const { return memoperands().end(); }
  2438. bool memoperands_empty() const { return memoperands().empty(); }
  2439. /// Clear out the memory reference descriptor list.
  2440. void clearMemRefs() {
  2441. MemRefs = nullptr;
  2442. NumMemRefs = 0;
  2443. }
  2444. static bool classof(const SDNode *N) {
  2445. return N->isMachineOpcode();
  2446. }
  2447. };
  2448. /// An SDNode that records if a register contains a value that is guaranteed to
  2449. /// be aligned accordingly.
  2450. class AssertAlignSDNode : public SDNode {
  2451. Align Alignment;
  2452. public:
  2453. AssertAlignSDNode(unsigned Order, const DebugLoc &DL, EVT VT, Align A)
  2454. : SDNode(ISD::AssertAlign, Order, DL, getSDVTList(VT)), Alignment(A) {}
  2455. Align getAlign() const { return Alignment; }
  2456. static bool classof(const SDNode *N) {
  2457. return N->getOpcode() == ISD::AssertAlign;
  2458. }
  2459. };
  2460. class SDNodeIterator {
  2461. const SDNode *Node;
  2462. unsigned Operand;
  2463. SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
  2464. public:
  2465. using iterator_category = std::forward_iterator_tag;
  2466. using value_type = SDNode;
  2467. using difference_type = std::ptrdiff_t;
  2468. using pointer = value_type *;
  2469. using reference = value_type &;
  2470. bool operator==(const SDNodeIterator& x) const {
  2471. return Operand == x.Operand;
  2472. }
  2473. bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
  2474. pointer operator*() const {
  2475. return Node->getOperand(Operand).getNode();
  2476. }
  2477. pointer operator->() const { return operator*(); }
  2478. SDNodeIterator& operator++() { // Preincrement
  2479. ++Operand;
  2480. return *this;
  2481. }
  2482. SDNodeIterator operator++(int) { // Postincrement
  2483. SDNodeIterator tmp = *this; ++*this; return tmp;
  2484. }
  2485. size_t operator-(SDNodeIterator Other) const {
  2486. assert(Node == Other.Node &&
  2487. "Cannot compare iterators of two different nodes!");
  2488. return Operand - Other.Operand;
  2489. }
  2490. static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); }
  2491. static SDNodeIterator end (const SDNode *N) {
  2492. return SDNodeIterator(N, N->getNumOperands());
  2493. }
  2494. unsigned getOperand() const { return Operand; }
  2495. const SDNode *getNode() const { return Node; }
  2496. };
  2497. template <> struct GraphTraits<SDNode*> {
  2498. using NodeRef = SDNode *;
  2499. using ChildIteratorType = SDNodeIterator;
  2500. static NodeRef getEntryNode(SDNode *N) { return N; }
  2501. static ChildIteratorType child_begin(NodeRef N) {
  2502. return SDNodeIterator::begin(N);
  2503. }
  2504. static ChildIteratorType child_end(NodeRef N) {
  2505. return SDNodeIterator::end(N);
  2506. }
  2507. };
  2508. /// A representation of the largest SDNode, for use in sizeof().
  2509. ///
  2510. /// This needs to be a union because the largest node differs on 32 bit systems
  2511. /// with 4 and 8 byte pointer alignment, respectively.
  2512. using LargestSDNode = AlignedCharArrayUnion<AtomicSDNode, TargetIndexSDNode,
  2513. BlockAddressSDNode,
  2514. GlobalAddressSDNode,
  2515. PseudoProbeSDNode>;
  2516. /// The SDNode class with the greatest alignment requirement.
  2517. using MostAlignedSDNode = GlobalAddressSDNode;
  2518. namespace ISD {
  2519. /// Returns true if the specified node is a non-extending and unindexed load.
  2520. inline bool isNormalLoad(const SDNode *N) {
  2521. const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
  2522. return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
  2523. Ld->getAddressingMode() == ISD::UNINDEXED;
  2524. }
  2525. /// Returns true if the specified node is a non-extending load.
  2526. inline bool isNON_EXTLoad(const SDNode *N) {
  2527. return isa<LoadSDNode>(N) &&
  2528. cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
  2529. }
  2530. /// Returns true if the specified node is a EXTLOAD.
  2531. inline bool isEXTLoad(const SDNode *N) {
  2532. return isa<LoadSDNode>(N) &&
  2533. cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
  2534. }
  2535. /// Returns true if the specified node is a SEXTLOAD.
  2536. inline bool isSEXTLoad(const SDNode *N) {
  2537. return isa<LoadSDNode>(N) &&
  2538. cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
  2539. }
  2540. /// Returns true if the specified node is a ZEXTLOAD.
  2541. inline bool isZEXTLoad(const SDNode *N) {
  2542. return isa<LoadSDNode>(N) &&
  2543. cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
  2544. }
  2545. /// Returns true if the specified node is an unindexed load.
  2546. inline bool isUNINDEXEDLoad(const SDNode *N) {
  2547. return isa<LoadSDNode>(N) &&
  2548. cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
  2549. }
  2550. /// Returns true if the specified node is a non-truncating
  2551. /// and unindexed store.
  2552. inline bool isNormalStore(const SDNode *N) {
  2553. const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
  2554. return St && !St->isTruncatingStore() &&
  2555. St->getAddressingMode() == ISD::UNINDEXED;
  2556. }
  2557. /// Returns true if the specified node is an unindexed store.
  2558. inline bool isUNINDEXEDStore(const SDNode *N) {
  2559. return isa<StoreSDNode>(N) &&
  2560. cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
  2561. }
  2562. /// Attempt to match a unary predicate against a scalar/splat constant or
  2563. /// every element of a constant BUILD_VECTOR.
  2564. /// If AllowUndef is true, then UNDEF elements will pass nullptr to Match.
  2565. bool matchUnaryPredicate(SDValue Op,
  2566. std::function<bool(ConstantSDNode *)> Match,
  2567. bool AllowUndefs = false);
  2568. /// Attempt to match a binary predicate against a pair of scalar/splat
  2569. /// constants or every element of a pair of constant BUILD_VECTORs.
  2570. /// If AllowUndef is true, then UNDEF elements will pass nullptr to Match.
  2571. /// If AllowTypeMismatch is true then RetType + ArgTypes don't need to match.
  2572. bool matchBinaryPredicate(
  2573. SDValue LHS, SDValue RHS,
  2574. std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
  2575. bool AllowUndefs = false, bool AllowTypeMismatch = false);
  2576. /// Returns true if the specified value is the overflow result from one
  2577. /// of the overflow intrinsic nodes.
  2578. inline bool isOverflowIntrOpRes(SDValue Op) {
  2579. unsigned Opc = Op.getOpcode();
  2580. return (Op.getResNo() == 1 &&
  2581. (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
  2582. Opc == ISD::USUBO || Opc == ISD::SMULO || Opc == ISD::UMULO));
  2583. }
  2584. } // end namespace ISD
  2585. } // end namespace llvm
  2586. #endif // LLVM_CODEGEN_SELECTIONDAGNODES_H
  2587. #ifdef __GNUC__
  2588. #pragma GCC diagnostic pop
  2589. #endif