SelectionDAGNodes.h 103 KB

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