SelectionDAGNodes.h 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307
  1. //===-- llvm/CodeGen/SelectionDAGNodes.h - SelectionDAG Nodes ---*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file declares the SDNode class and derived classes, which are used to
  11. // represent the nodes and operations present in a SelectionDAG. These nodes
  12. // and operations are machine code level operations, with some similarities to
  13. // the GCC RTL representation.
  14. //
  15. // Clients should include the SelectionDAG.h file instead of this file directly.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CODEGEN_SELECTIONDAGNODES_H
  19. #define LLVM_CODEGEN_SELECTIONDAGNODES_H
  20. #include "llvm/ADT/BitVector.h"
  21. #include "llvm/ADT/FoldingSet.h"
  22. #include "llvm/ADT/GraphTraits.h"
  23. #include "llvm/ADT/STLExtras.h"
  24. #include "llvm/ADT/SmallPtrSet.h"
  25. #include "llvm/ADT/SmallVector.h"
  26. #include "llvm/ADT/ilist_node.h"
  27. #include "llvm/ADT/iterator_range.h"
  28. #include "llvm/CodeGen/ISDOpcodes.h"
  29. #include "llvm/CodeGen/MachineMemOperand.h"
  30. #include "llvm/CodeGen/ValueTypes.h"
  31. #include "llvm/IR/Constants.h"
  32. #include "llvm/IR/DebugLoc.h"
  33. #include "llvm/IR/Instructions.h"
  34. #include "llvm/Support/DataTypes.h"
  35. #include "llvm/Support/MathExtras.h"
  36. #include <cassert>
  37. namespace llvm {
  38. class SelectionDAG;
  39. class GlobalValue;
  40. class MachineBasicBlock;
  41. class MachineConstantPoolValue;
  42. class SDNode;
  43. class Value;
  44. class MCSymbol;
  45. template <typename T> struct DenseMapInfo;
  46. template <typename T> struct simplify_type;
  47. template <typename T> struct ilist_traits;
  48. void checkForCycles(const SDNode *N, const SelectionDAG *DAG = nullptr,
  49. bool force = false);
  50. /// This represents a list of ValueType's that has been intern'd by
  51. /// a SelectionDAG. Instances of this simple value class are returned by
  52. /// SelectionDAG::getVTList(...).
  53. ///
  54. struct SDVTList {
  55. const EVT *VTs;
  56. unsigned int NumVTs;
  57. };
  58. namespace ISD {
  59. /// Node predicates
  60. /// Return true if the specified node is a
  61. /// BUILD_VECTOR where all of the elements are ~0 or undef.
  62. bool isBuildVectorAllOnes(const SDNode *N);
  63. /// Return true if the specified node is a
  64. /// BUILD_VECTOR where all of the elements are 0 or undef.
  65. bool isBuildVectorAllZeros(const SDNode *N);
  66. /// \brief Return true if the specified node is a BUILD_VECTOR node of
  67. /// all ConstantSDNode or undef.
  68. bool isBuildVectorOfConstantSDNodes(const SDNode *N);
  69. /// \brief Return true if the specified node is a BUILD_VECTOR node of
  70. /// all ConstantFPSDNode or undef.
  71. bool isBuildVectorOfConstantFPSDNodes(const SDNode *N);
  72. /// Return true if the specified node is a
  73. /// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
  74. /// element is not an undef.
  75. bool isScalarToVector(const SDNode *N);
  76. /// Return true if the node has at least one operand
  77. /// and all operands of the specified node are ISD::UNDEF.
  78. bool allOperandsUndef(const SDNode *N);
  79. } // end llvm:ISD namespace
  80. // //
  81. ///////////////////////////////////////////////////////////////////////////////
  82. /// Unlike LLVM values, Selection DAG nodes may return multiple
  83. /// values as the result of a computation. Many nodes return multiple values,
  84. /// from loads (which define a token and a return value) to ADDC (which returns
  85. /// a result and a carry value), to calls (which may return an arbitrary number
  86. /// of values).
  87. ///
  88. /// As such, each use of a SelectionDAG computation must indicate the node that
  89. /// computes it as well as which return value to use from that node. This pair
  90. /// of information is represented with the SDValue value type.
  91. ///
  92. class SDValue {
  93. friend struct DenseMapInfo<SDValue>;
  94. SDNode *Node; // The node defining the value we are using.
  95. unsigned ResNo; // Which return value of the node we are using.
  96. public:
  97. SDValue() : Node(nullptr), ResNo(0) {}
  98. SDValue(SDNode *node, unsigned resno);
  99. /// get the index which selects a specific result in the SDNode
  100. unsigned getResNo() const { return ResNo; }
  101. /// get the SDNode which holds the desired result
  102. SDNode *getNode() const { return Node; }
  103. /// set the SDNode
  104. void setNode(SDNode *N) { Node = N; }
  105. inline SDNode *operator->() const { return Node; }
  106. bool operator==(const SDValue &O) const {
  107. return Node == O.Node && ResNo == O.ResNo;
  108. }
  109. bool operator!=(const SDValue &O) const {
  110. return !operator==(O);
  111. }
  112. bool operator<(const SDValue &O) const {
  113. return std::tie(Node, ResNo) < std::tie(O.Node, O.ResNo);
  114. }
  115. explicit operator bool() const {
  116. return Node != nullptr;
  117. }
  118. SDValue getValue(unsigned R) const {
  119. return SDValue(Node, R);
  120. }
  121. // Return true if this node is an operand of N.
  122. bool isOperandOf(const SDNode *N) const;
  123. /// Return the ValueType of the referenced return value.
  124. inline EVT getValueType() const;
  125. /// Return the simple ValueType of the referenced return value.
  126. MVT getSimpleValueType() const {
  127. return getValueType().getSimpleVT();
  128. }
  129. /// Returns the size of the value in bits.
  130. unsigned getValueSizeInBits() const {
  131. return getValueType().getSizeInBits();
  132. }
  133. unsigned getScalarValueSizeInBits() const {
  134. return getValueType().getScalarType().getSizeInBits();
  135. }
  136. // Forwarding methods - These forward to the corresponding methods in SDNode.
  137. inline unsigned getOpcode() const;
  138. inline unsigned getNumOperands() const;
  139. inline const SDValue &getOperand(unsigned i) const;
  140. inline uint64_t getConstantOperandVal(unsigned i) const;
  141. inline bool isTargetMemoryOpcode() const;
  142. inline bool isTargetOpcode() const;
  143. inline bool isMachineOpcode() const;
  144. inline unsigned getMachineOpcode() const;
  145. inline const DebugLoc &getDebugLoc() const;
  146. inline void dump() const;
  147. inline void dumpr() const;
  148. /// Return true if this operand (which must be a chain) reaches the
  149. /// specified operand without crossing any side-effecting instructions.
  150. /// In practice, this looks through token factors and non-volatile loads.
  151. /// In order to remain efficient, this only
  152. /// looks a couple of nodes in, it does not do an exhaustive search.
  153. bool reachesChainWithoutSideEffects(SDValue Dest,
  154. unsigned Depth = 2) const;
  155. /// Return true if there are no nodes using value ResNo of Node.
  156. inline bool use_empty() const;
  157. /// Return true if there is exactly one node using value ResNo of Node.
  158. inline bool hasOneUse() const;
  159. };
  160. template<> struct DenseMapInfo<SDValue> {
  161. static inline SDValue getEmptyKey() {
  162. SDValue V;
  163. V.ResNo = -1U;
  164. return V;
  165. }
  166. static inline SDValue getTombstoneKey() {
  167. SDValue V;
  168. V.ResNo = -2U;
  169. return V;
  170. }
  171. static unsigned getHashValue(const SDValue &Val) {
  172. return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
  173. (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
  174. }
  175. static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
  176. return LHS == RHS;
  177. }
  178. };
  179. template <> struct isPodLike<SDValue> { static const bool value = true; };
  180. /// Allow casting operators to work directly on
  181. /// SDValues as if they were SDNode*'s.
  182. template<> struct simplify_type<SDValue> {
  183. typedef SDNode* SimpleType;
  184. static SimpleType getSimplifiedValue(SDValue &Val) {
  185. return Val.getNode();
  186. }
  187. };
  188. template<> struct simplify_type<const SDValue> {
  189. typedef /*const*/ SDNode* SimpleType;
  190. static SimpleType getSimplifiedValue(const SDValue &Val) {
  191. return Val.getNode();
  192. }
  193. };
  194. /// Represents a use of a SDNode. This class holds an SDValue,
  195. /// which records the SDNode being used and the result number, a
  196. /// pointer to the SDNode using the value, and Next and Prev pointers,
  197. /// which link together all the uses of an SDNode.
  198. ///
  199. class SDUse {
  200. /// Val - The value being used.
  201. SDValue Val;
  202. /// User - The user of this value.
  203. SDNode *User;
  204. /// Prev, Next - Pointers to the uses list of the SDNode referred by
  205. /// this operand.
  206. SDUse **Prev, *Next;
  207. SDUse(const SDUse &U) = delete;
  208. void operator=(const SDUse &U) = delete;
  209. public:
  210. SDUse() : Val(), User(nullptr), Prev(nullptr), Next(nullptr) {}
  211. /// Normally SDUse will just implicitly convert to an SDValue that it holds.
  212. operator const SDValue&() const { return Val; }
  213. /// If implicit conversion to SDValue doesn't work, the get() method returns
  214. /// the SDValue.
  215. const SDValue &get() const { return Val; }
  216. /// This returns the SDNode that contains this Use.
  217. SDNode *getUser() { return User; }
  218. /// Get the next SDUse in the use list.
  219. SDUse *getNext() const { return Next; }
  220. /// Convenience function for get().getNode().
  221. SDNode *getNode() const { return Val.getNode(); }
  222. /// Convenience function for get().getResNo().
  223. unsigned getResNo() const { return Val.getResNo(); }
  224. /// Convenience function for get().getValueType().
  225. EVT getValueType() const { return Val.getValueType(); }
  226. /// Convenience function for get().operator==
  227. bool operator==(const SDValue &V) const {
  228. return Val == V;
  229. }
  230. /// Convenience function for get().operator!=
  231. bool operator!=(const SDValue &V) const {
  232. return Val != V;
  233. }
  234. /// Convenience function for get().operator<
  235. bool operator<(const SDValue &V) const {
  236. return Val < V;
  237. }
  238. private:
  239. friend class SelectionDAG;
  240. friend class SDNode;
  241. void setUser(SDNode *p) { User = p; }
  242. /// Remove this use from its existing use list, assign it the
  243. /// given value, and add it to the new value's node's use list.
  244. inline void set(const SDValue &V);
  245. /// Like set, but only supports initializing a newly-allocated
  246. /// SDUse with a non-null value.
  247. inline void setInitial(const SDValue &V);
  248. /// Like set, but only sets the Node portion of the value,
  249. /// leaving the ResNo portion unmodified.
  250. inline void setNode(SDNode *N);
  251. void addToList(SDUse **List) {
  252. Next = *List;
  253. if (Next) Next->Prev = &Next;
  254. Prev = List;
  255. *List = this;
  256. }
  257. void removeFromList() {
  258. *Prev = Next;
  259. if (Next) Next->Prev = Prev;
  260. }
  261. };
  262. /// simplify_type specializations - Allow casting operators to work directly on
  263. /// SDValues as if they were SDNode*'s.
  264. template<> struct simplify_type<SDUse> {
  265. typedef SDNode* SimpleType;
  266. static SimpleType getSimplifiedValue(SDUse &Val) {
  267. return Val.getNode();
  268. }
  269. };
  270. /// Represents one node in the SelectionDAG.
  271. ///
  272. class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
  273. private:
  274. /// The operation that this node performs.
  275. int16_t NodeType;
  276. /// This is true if OperandList was new[]'d. If true,
  277. /// then they will be delete[]'d when the node is destroyed.
  278. uint16_t OperandsNeedDelete : 1;
  279. /// This tracks whether this node has one or more dbg_value
  280. /// nodes corresponding to it.
  281. uint16_t HasDebugValue : 1;
  282. protected:
  283. /// This member is defined by this class, but is not used for
  284. /// anything. Subclasses can use it to hold whatever state they find useful.
  285. /// This field is initialized to zero by the ctor.
  286. uint16_t SubclassData : 14;
  287. private:
  288. /// Unique id per SDNode in the DAG.
  289. int NodeId;
  290. /// The values that are used by this operation.
  291. SDUse *OperandList;
  292. /// The types of the values this node defines. SDNode's may
  293. /// define multiple values simultaneously.
  294. const EVT *ValueList;
  295. /// List of uses for this SDNode.
  296. SDUse *UseList;
  297. /// The number of entries in the Operand/Value list.
  298. unsigned short NumOperands, NumValues;
  299. // The ordering of the SDNodes. It roughly corresponds to the ordering of the
  300. // original LLVM instructions.
  301. // This is used for turning off scheduling, because we'll forgo
  302. // the normal scheduling algorithms and output the instructions according to
  303. // this ordering.
  304. unsigned IROrder;
  305. /// Source line information.
  306. DebugLoc debugLoc;
  307. /// Return a pointer to the specified value type.
  308. static const EVT *getValueTypeList(EVT VT);
  309. friend class SelectionDAG;
  310. friend struct ilist_traits<SDNode>;
  311. public:
  312. //===--------------------------------------------------------------------===//
  313. // Accessors
  314. //
  315. /// Return the SelectionDAG opcode value for this node. For
  316. /// pre-isel nodes (those for which isMachineOpcode returns false), these
  317. /// are the opcode values in the ISD and <target>ISD namespaces. For
  318. /// post-isel opcodes, see getMachineOpcode.
  319. unsigned getOpcode() const { return (unsigned short)NodeType; }
  320. /// Test if this node has a target-specific opcode (in the
  321. /// \<target\>ISD namespace).
  322. bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
  323. /// Test if this node has a target-specific
  324. /// memory-referencing opcode (in the \<target\>ISD namespace and
  325. /// greater than FIRST_TARGET_MEMORY_OPCODE).
  326. bool isTargetMemoryOpcode() const {
  327. return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
  328. }
  329. /// Test if this node is a memory intrinsic (with valid pointer information).
  330. /// INTRINSIC_W_CHAIN and INTRINSIC_VOID nodes are sometimes created for
  331. /// non-memory intrinsics (with chains) that are not really instances of
  332. /// MemSDNode. For such nodes, we need some extra state to determine the
  333. /// proper classof relationship.
  334. bool isMemIntrinsic() const {
  335. return (NodeType == ISD::INTRINSIC_W_CHAIN ||
  336. NodeType == ISD::INTRINSIC_VOID) && ((SubclassData >> 13) & 1);
  337. }
  338. /// Test if this node has a post-isel opcode, directly
  339. /// corresponding to a MachineInstr opcode.
  340. bool isMachineOpcode() const { return NodeType < 0; }
  341. /// This may only be called if isMachineOpcode returns
  342. /// true. It returns the MachineInstr opcode value that the node's opcode
  343. /// corresponds to.
  344. unsigned getMachineOpcode() const {
  345. assert(isMachineOpcode() && "Not a MachineInstr opcode!");
  346. return ~NodeType;
  347. }
  348. /// Get this bit.
  349. bool getHasDebugValue() const { return HasDebugValue; }
  350. /// Set this bit.
  351. void setHasDebugValue(bool b) { HasDebugValue = b; }
  352. /// Return true if there are no uses of this node.
  353. bool use_empty() const { return UseList == nullptr; }
  354. /// Return true if there is exactly one use of this node.
  355. bool hasOneUse() const {
  356. return !use_empty() && std::next(use_begin()) == use_end();
  357. }
  358. /// Return the number of uses of this node. This method takes
  359. /// time proportional to the number of uses.
  360. size_t use_size() const { return std::distance(use_begin(), use_end()); }
  361. /// Return the unique node id.
  362. int getNodeId() const { return NodeId; }
  363. /// Set unique node id.
  364. void setNodeId(int Id) { NodeId = Id; }
  365. /// Return the node ordering.
  366. unsigned getIROrder() const { return IROrder; }
  367. /// Set the node ordering.
  368. void setIROrder(unsigned Order) { IROrder = Order; }
  369. /// Return the source location info.
  370. const DebugLoc &getDebugLoc() const { return debugLoc; }
  371. /// Set source location info. Try to avoid this, putting
  372. /// it in the constructor is preferable.
  373. void setDebugLoc(DebugLoc dl) { debugLoc = std::move(dl); }
  374. /// This class provides iterator support for SDUse
  375. /// operands that use a specific SDNode.
  376. class use_iterator
  377. : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
  378. SDUse *Op;
  379. explicit use_iterator(SDUse *op) : Op(op) {
  380. }
  381. friend class SDNode;
  382. public:
  383. typedef std::iterator<std::forward_iterator_tag,
  384. SDUse, ptrdiff_t>::reference reference;
  385. typedef std::iterator<std::forward_iterator_tag,
  386. SDUse, ptrdiff_t>::pointer pointer;
  387. use_iterator(const use_iterator &I) : Op(I.Op) {}
  388. use_iterator() : Op(nullptr) {}
  389. bool operator==(const use_iterator &x) const {
  390. return Op == x.Op;
  391. }
  392. bool operator!=(const use_iterator &x) const {
  393. return !operator==(x);
  394. }
  395. /// Return true if this iterator is at the end of uses list.
  396. bool atEnd() const { return Op == nullptr; }
  397. // Iterator traversal: forward iteration only.
  398. use_iterator &operator++() { // Preincrement
  399. assert(Op && "Cannot increment end iterator!");
  400. Op = Op->getNext();
  401. return *this;
  402. }
  403. use_iterator operator++(int) { // Postincrement
  404. use_iterator tmp = *this; ++*this; return tmp;
  405. }
  406. /// Retrieve a pointer to the current user node.
  407. SDNode *operator*() const {
  408. assert(Op && "Cannot dereference end iterator!");
  409. return Op->getUser();
  410. }
  411. SDNode *operator->() const { return operator*(); }
  412. SDUse &getUse() const { return *Op; }
  413. /// Retrieve the operand # of this use in its user.
  414. unsigned getOperandNo() const {
  415. assert(Op && "Cannot dereference end iterator!");
  416. return (unsigned)(Op - Op->getUser()->OperandList);
  417. }
  418. };
  419. /// Provide iteration support to walk over all uses of an SDNode.
  420. use_iterator use_begin() const {
  421. return use_iterator(UseList);
  422. }
  423. static use_iterator use_end() { return use_iterator(nullptr); }
  424. inline iterator_range<use_iterator> uses() {
  425. return iterator_range<use_iterator>(use_begin(), use_end());
  426. }
  427. inline iterator_range<use_iterator> uses() const {
  428. return iterator_range<use_iterator>(use_begin(), use_end());
  429. }
  430. /// Return true if there are exactly NUSES uses of the indicated value.
  431. /// This method ignores uses of other values defined by this operation.
  432. bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
  433. /// Return true if there are any use of the indicated value.
  434. /// This method ignores uses of other values defined by this operation.
  435. bool hasAnyUseOfValue(unsigned Value) const;
  436. /// Return true if this node is the only use of N.
  437. bool isOnlyUserOf(const SDNode *N) const;
  438. /// Return true if this node is an operand of N.
  439. bool isOperandOf(const SDNode *N) const;
  440. /// Return true if this node is a predecessor of N.
  441. /// NOTE: Implemented on top of hasPredecessor and every bit as
  442. /// expensive. Use carefully.
  443. bool isPredecessorOf(const SDNode *N) const {
  444. return N->hasPredecessor(this);
  445. }
  446. /// Return true if N is a predecessor of this node.
  447. /// N is either an operand of this node, or can be reached by recursively
  448. /// traversing up the operands.
  449. /// NOTE: This is an expensive method. Use it carefully.
  450. bool hasPredecessor(const SDNode *N) const;
  451. /// Return true if N is a predecessor of this node.
  452. /// N is either an operand of this node, or can be reached by recursively
  453. /// traversing up the operands.
  454. /// In this helper the Visited and worklist sets are held externally to
  455. /// cache predecessors over multiple invocations. If you want to test for
  456. /// multiple predecessors this method is preferable to multiple calls to
  457. /// hasPredecessor. Be sure to clear Visited and Worklist if the DAG
  458. /// changes.
  459. /// NOTE: This is still very expensive. Use carefully.
  460. bool hasPredecessorHelper(const SDNode *N,
  461. SmallPtrSetImpl<const SDNode *> &Visited,
  462. SmallVectorImpl<const SDNode *> &Worklist) const;
  463. /// Return the number of values used by this operation.
  464. unsigned getNumOperands() const { return NumOperands; }
  465. /// Helper method returns the integer value of a ConstantSDNode operand.
  466. uint64_t getConstantOperandVal(unsigned Num) const;
  467. const SDValue &getOperand(unsigned Num) const {
  468. assert(Num < NumOperands && "Invalid child # of SDNode!");
  469. return OperandList[Num];
  470. }
  471. typedef SDUse* op_iterator;
  472. op_iterator op_begin() const { return OperandList; }
  473. op_iterator op_end() const { return OperandList+NumOperands; }
  474. ArrayRef<SDUse> ops() const { return makeArrayRef(op_begin(), op_end()); }
  475. /// Iterator for directly iterating over the operand SDValue's.
  476. struct value_op_iterator
  477. : iterator_adaptor_base<value_op_iterator, op_iterator,
  478. std::random_access_iterator_tag, SDValue,
  479. ptrdiff_t, value_op_iterator *,
  480. value_op_iterator *> {
  481. explicit value_op_iterator(SDUse *U = nullptr)
  482. : iterator_adaptor_base(U) {}
  483. const SDValue &operator*() const { return I->get(); }
  484. };
  485. iterator_range<value_op_iterator> op_values() const {
  486. return iterator_range<value_op_iterator>(value_op_iterator(op_begin()),
  487. value_op_iterator(op_end()));
  488. }
  489. SDVTList getVTList() const {
  490. SDVTList X = { ValueList, NumValues };
  491. return X;
  492. }
  493. /// If this node has a glue operand, return the node
  494. /// to which the glue operand points. Otherwise return NULL.
  495. SDNode *getGluedNode() const {
  496. if (getNumOperands() != 0 &&
  497. getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
  498. return getOperand(getNumOperands()-1).getNode();
  499. return nullptr;
  500. }
  501. // If this is a pseudo op, like copyfromreg, look to see if there is a
  502. // real target node glued to it. If so, return the target node.
  503. const SDNode *getGluedMachineNode() const {
  504. const SDNode *FoundNode = this;
  505. // Climb up glue edges until a machine-opcode node is found, or the
  506. // end of the chain is reached.
  507. while (!FoundNode->isMachineOpcode()) {
  508. const SDNode *N = FoundNode->getGluedNode();
  509. if (!N) break;
  510. FoundNode = N;
  511. }
  512. return FoundNode;
  513. }
  514. /// If this node has a glue value with a user, return
  515. /// the user (there is at most one). Otherwise return NULL.
  516. SDNode *getGluedUser() const {
  517. for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
  518. if (UI.getUse().get().getValueType() == MVT::Glue)
  519. return *UI;
  520. return nullptr;
  521. }
  522. /// Return the number of values defined/returned by this operator.
  523. unsigned getNumValues() const { return NumValues; }
  524. /// Return the type of a specified result.
  525. EVT getValueType(unsigned ResNo) const {
  526. assert(ResNo < NumValues && "Illegal result number!");
  527. return ValueList[ResNo];
  528. }
  529. /// Return the type of a specified result as a simple type.
  530. MVT getSimpleValueType(unsigned ResNo) const {
  531. return getValueType(ResNo).getSimpleVT();
  532. }
  533. /// Returns MVT::getSizeInBits(getValueType(ResNo)).
  534. unsigned getValueSizeInBits(unsigned ResNo) const {
  535. return getValueType(ResNo).getSizeInBits();
  536. }
  537. typedef const EVT* value_iterator;
  538. value_iterator value_begin() const { return ValueList; }
  539. value_iterator value_end() const { return ValueList+NumValues; }
  540. /// Return the opcode of this operation for printing.
  541. std::string getOperationName(const SelectionDAG *G = nullptr) const;
  542. static const char* getIndexedModeName(ISD::MemIndexedMode AM);
  543. void print_types(raw_ostream &OS, const SelectionDAG *G) const;
  544. void print_details(raw_ostream &OS, const SelectionDAG *G) const;
  545. void print(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
  546. void printr(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
  547. /// Print a SelectionDAG node and all children down to
  548. /// the leaves. The given SelectionDAG allows target-specific nodes
  549. /// to be printed in human-readable form. Unlike printr, this will
  550. /// print the whole DAG, including children that appear multiple
  551. /// times.
  552. ///
  553. void printrFull(raw_ostream &O, const SelectionDAG *G = nullptr) const;
  554. /// Print a SelectionDAG node and children up to
  555. /// depth "depth." The given SelectionDAG allows target-specific
  556. /// nodes to be printed in human-readable form. Unlike printr, this
  557. /// will print children that appear multiple times wherever they are
  558. /// used.
  559. ///
  560. void printrWithDepth(raw_ostream &O, const SelectionDAG *G = nullptr,
  561. unsigned depth = 100) const;
  562. /// Dump this node, for debugging.
  563. void dump() const;
  564. /// Dump (recursively) this node and its use-def subgraph.
  565. void dumpr() const;
  566. /// Dump this node, for debugging.
  567. /// The given SelectionDAG allows target-specific nodes to be printed
  568. /// in human-readable form.
  569. void dump(const SelectionDAG *G) const;
  570. /// Dump (recursively) this node and its use-def subgraph.
  571. /// The given SelectionDAG allows target-specific nodes to be printed
  572. /// in human-readable form.
  573. void dumpr(const SelectionDAG *G) const;
  574. /// printrFull to dbgs(). The given SelectionDAG allows
  575. /// target-specific nodes to be printed in human-readable form.
  576. /// Unlike dumpr, this will print the whole DAG, including children
  577. /// that appear multiple times.
  578. void dumprFull(const SelectionDAG *G = nullptr) const;
  579. /// printrWithDepth to dbgs(). The given
  580. /// SelectionDAG allows target-specific nodes to be printed in
  581. /// human-readable form. Unlike dumpr, this will print children
  582. /// that appear multiple times wherever they are used.
  583. ///
  584. void dumprWithDepth(const SelectionDAG *G = nullptr,
  585. unsigned depth = 100) const;
  586. /// Gather unique data for the node.
  587. void Profile(FoldingSetNodeID &ID) const;
  588. /// This method should only be used by the SDUse class.
  589. void addUse(SDUse &U) { U.addToList(&UseList); }
  590. protected:
  591. static SDVTList getSDVTList(EVT VT) {
  592. SDVTList Ret = { getValueTypeList(VT), 1 };
  593. return Ret;
  594. }
  595. SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
  596. ArrayRef<SDValue> Ops)
  597. : NodeType(Opc), OperandsNeedDelete(true), HasDebugValue(false),
  598. SubclassData(0), NodeId(-1),
  599. OperandList(Ops.size() ? new SDUse[Ops.size()] : nullptr),
  600. ValueList(VTs.VTs), UseList(nullptr), NumOperands(Ops.size()),
  601. NumValues(VTs.NumVTs), IROrder(Order), debugLoc(std::move(dl)) {
  602. assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
  603. assert(NumOperands == Ops.size() &&
  604. "NumOperands wasn't wide enough for its operands!");
  605. assert(NumValues == VTs.NumVTs &&
  606. "NumValues wasn't wide enough for its operands!");
  607. for (unsigned i = 0; i != Ops.size(); ++i) {
  608. assert(OperandList && "no operands available");
  609. OperandList[i].setUser(this);
  610. OperandList[i].setInitial(Ops[i]);
  611. }
  612. checkForCycles(this);
  613. }
  614. /// This constructor adds no operands itself; operands can be
  615. /// set later with InitOperands.
  616. SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
  617. : NodeType(Opc), OperandsNeedDelete(false), HasDebugValue(false),
  618. SubclassData(0), NodeId(-1), OperandList(nullptr), ValueList(VTs.VTs),
  619. UseList(nullptr), NumOperands(0), NumValues(VTs.NumVTs),
  620. IROrder(Order), debugLoc(std::move(dl)) {
  621. assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
  622. assert(NumValues == VTs.NumVTs &&
  623. "NumValues wasn't wide enough for its operands!");
  624. }
  625. /// Initialize the operands list of this with 1 operand.
  626. void InitOperands(SDUse *Ops, const SDValue &Op0) {
  627. Ops[0].setUser(this);
  628. Ops[0].setInitial(Op0);
  629. NumOperands = 1;
  630. OperandList = Ops;
  631. checkForCycles(this);
  632. }
  633. /// Initialize the operands list of this with 2 operands.
  634. void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1) {
  635. Ops[0].setUser(this);
  636. Ops[0].setInitial(Op0);
  637. Ops[1].setUser(this);
  638. Ops[1].setInitial(Op1);
  639. NumOperands = 2;
  640. OperandList = Ops;
  641. checkForCycles(this);
  642. }
  643. /// Initialize the operands list of this with 3 operands.
  644. void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
  645. const SDValue &Op2) {
  646. Ops[0].setUser(this);
  647. Ops[0].setInitial(Op0);
  648. Ops[1].setUser(this);
  649. Ops[1].setInitial(Op1);
  650. Ops[2].setUser(this);
  651. Ops[2].setInitial(Op2);
  652. NumOperands = 3;
  653. OperandList = Ops;
  654. checkForCycles(this);
  655. }
  656. /// Initialize the operands list of this with 4 operands.
  657. void InitOperands(SDUse *Ops, const SDValue &Op0, const SDValue &Op1,
  658. const SDValue &Op2, const SDValue &Op3) {
  659. Ops[0].setUser(this);
  660. Ops[0].setInitial(Op0);
  661. Ops[1].setUser(this);
  662. Ops[1].setInitial(Op1);
  663. Ops[2].setUser(this);
  664. Ops[2].setInitial(Op2);
  665. Ops[3].setUser(this);
  666. Ops[3].setInitial(Op3);
  667. NumOperands = 4;
  668. OperandList = Ops;
  669. checkForCycles(this);
  670. }
  671. /// Initialize the operands list of this with N operands.
  672. void InitOperands(SDUse *Ops, const SDValue *Vals, unsigned N) {
  673. for (unsigned i = 0; i != N; ++i) {
  674. Ops[i].setUser(this);
  675. Ops[i].setInitial(Vals[i]);
  676. }
  677. NumOperands = N;
  678. assert(NumOperands == N &&
  679. "NumOperands wasn't wide enough for its operands!");
  680. OperandList = Ops;
  681. checkForCycles(this);
  682. }
  683. /// Release the operands and set this node to have zero operands.
  684. void DropOperands();
  685. };
  686. /// Wrapper class for IR location info (IR ordering and DebugLoc) to be passed
  687. /// into SDNode creation functions.
  688. /// When an SDNode is created from the DAGBuilder, the DebugLoc is extracted
  689. /// from the original Instruction, and IROrder is the ordinal position of
  690. /// the instruction.
  691. /// When an SDNode is created after the DAG is being built, both DebugLoc and
  692. /// the IROrder are propagated from the original SDNode.
  693. /// So SDLoc class provides two constructors besides the default one, one to
  694. /// be used by the DAGBuilder, the other to be used by others.
  695. class SDLoc {
  696. private:
  697. // Ptr could be used for either Instruction* or SDNode*. It is used for
  698. // Instruction* if IROrder is not -1.
  699. const void *Ptr;
  700. int IROrder;
  701. public:
  702. SDLoc() : Ptr(nullptr), IROrder(0) {}
  703. SDLoc(const SDNode *N) : Ptr(N), IROrder(-1) {
  704. assert(N && "null SDNode");
  705. }
  706. SDLoc(const SDValue V) : Ptr(V.getNode()), IROrder(-1) {
  707. assert(Ptr && "null SDNode");
  708. }
  709. SDLoc(const Instruction *I, int Order) : Ptr(I), IROrder(Order) {
  710. assert(Order >= 0 && "bad IROrder");
  711. }
  712. unsigned getIROrder() {
  713. if (IROrder >= 0 || Ptr == nullptr) {
  714. return (unsigned)IROrder;
  715. }
  716. const SDNode *N = (const SDNode*)(Ptr);
  717. return N->getIROrder();
  718. }
  719. DebugLoc getDebugLoc() {
  720. if (!Ptr) {
  721. return DebugLoc();
  722. }
  723. if (IROrder >= 0) {
  724. const Instruction *I = (const Instruction*)(Ptr);
  725. return I->getDebugLoc();
  726. }
  727. const SDNode *N = (const SDNode*)(Ptr);
  728. return N->getDebugLoc();
  729. }
  730. };
  731. // Define inline functions from the SDValue class.
  732. inline SDValue::SDValue(SDNode *node, unsigned resno)
  733. : Node(node), ResNo(resno) {
  734. assert((!Node || ResNo < Node->getNumValues()) &&
  735. "Invalid result number for the given node!");
  736. assert(ResNo < -2U && "Cannot use result numbers reserved for DenseMaps.");
  737. }
  738. inline unsigned SDValue::getOpcode() const {
  739. return Node->getOpcode();
  740. }
  741. inline EVT SDValue::getValueType() const {
  742. return Node->getValueType(ResNo);
  743. }
  744. inline unsigned SDValue::getNumOperands() const {
  745. return Node->getNumOperands();
  746. }
  747. inline const SDValue &SDValue::getOperand(unsigned i) const {
  748. return Node->getOperand(i);
  749. }
  750. inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
  751. return Node->getConstantOperandVal(i);
  752. }
  753. inline bool SDValue::isTargetOpcode() const {
  754. return Node->isTargetOpcode();
  755. }
  756. inline bool SDValue::isTargetMemoryOpcode() const {
  757. return Node->isTargetMemoryOpcode();
  758. }
  759. inline bool SDValue::isMachineOpcode() const {
  760. return Node->isMachineOpcode();
  761. }
  762. inline unsigned SDValue::getMachineOpcode() const {
  763. return Node->getMachineOpcode();
  764. }
  765. inline bool SDValue::use_empty() const {
  766. return !Node->hasAnyUseOfValue(ResNo);
  767. }
  768. inline bool SDValue::hasOneUse() const {
  769. return Node->hasNUsesOfValue(1, ResNo);
  770. }
  771. inline const DebugLoc &SDValue::getDebugLoc() const {
  772. return Node->getDebugLoc();
  773. }
  774. inline void SDValue::dump() const {
  775. return Node->dump();
  776. }
  777. inline void SDValue::dumpr() const {
  778. return Node->dumpr();
  779. }
  780. // Define inline functions from the SDUse class.
  781. inline void SDUse::set(const SDValue &V) {
  782. if (Val.getNode()) removeFromList();
  783. Val = V;
  784. if (V.getNode()) V.getNode()->addUse(*this);
  785. }
  786. inline void SDUse::setInitial(const SDValue &V) {
  787. Val = V;
  788. V.getNode()->addUse(*this);
  789. }
  790. inline void SDUse::setNode(SDNode *N) {
  791. if (Val.getNode()) removeFromList();
  792. Val.setNode(N);
  793. if (N) N->addUse(*this);
  794. }
  795. /// These are IR-level optimization flags that may be propagated to SDNodes.
  796. /// TODO: This data structure should be shared by the IR optimizer and the
  797. /// the backend.
  798. struct SDNodeFlags {
  799. private:
  800. bool NoUnsignedWrap : 1;
  801. bool NoSignedWrap : 1;
  802. bool Exact : 1;
  803. bool UnsafeAlgebra : 1;
  804. bool NoNaNs : 1;
  805. bool NoInfs : 1;
  806. bool NoSignedZeros : 1;
  807. bool AllowReciprocal : 1;
  808. public:
  809. /// Default constructor turns off all optimization flags.
  810. SDNodeFlags() {
  811. NoUnsignedWrap = false;
  812. NoSignedWrap = false;
  813. Exact = false;
  814. UnsafeAlgebra = false;
  815. NoNaNs = false;
  816. NoInfs = false;
  817. NoSignedZeros = false;
  818. AllowReciprocal = false;
  819. }
  820. // These are mutators for each flag.
  821. void setNoUnsignedWrap(bool b) { NoUnsignedWrap = b; }
  822. void setNoSignedWrap(bool b) { NoSignedWrap = b; }
  823. void setExact(bool b) { Exact = b; }
  824. void setUnsafeAlgebra(bool b) { UnsafeAlgebra = b; }
  825. void setNoNaNs(bool b) { NoNaNs = b; }
  826. void setNoInfs(bool b) { NoInfs = b; }
  827. void setNoSignedZeros(bool b) { NoSignedZeros = b; }
  828. void setAllowReciprocal(bool b) { AllowReciprocal = b; }
  829. // These are accessors for each flag.
  830. bool hasNoUnsignedWrap() const { return NoUnsignedWrap; }
  831. bool hasNoSignedWrap() const { return NoSignedWrap; }
  832. bool hasExact() const { return Exact; }
  833. bool hasUnsafeAlgebra() const { return UnsafeAlgebra; }
  834. bool hasNoNaNs() const { return NoNaNs; }
  835. bool hasNoInfs() const { return NoInfs; }
  836. bool hasNoSignedZeros() const { return NoSignedZeros; }
  837. bool hasAllowReciprocal() const { return AllowReciprocal; }
  838. /// Return a raw encoding of the flags.
  839. /// This function should only be used to add data to the NodeID value.
  840. unsigned getRawFlags() const {
  841. return (NoUnsignedWrap << 0) | (NoSignedWrap << 1) | (Exact << 2) |
  842. (UnsafeAlgebra << 3) | (NoNaNs << 4) | (NoInfs << 5) |
  843. (NoSignedZeros << 6) | (AllowReciprocal << 7);
  844. }
  845. };
  846. /// This class is used for single-operand SDNodes. This is solely
  847. /// to allow co-allocation of node operands with the node itself.
  848. class UnarySDNode : public SDNode {
  849. SDUse Op;
  850. public:
  851. UnarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
  852. SDValue X)
  853. : SDNode(Opc, Order, dl, VTs) {
  854. InitOperands(&Op, X);
  855. }
  856. };
  857. /// This class is used for two-operand SDNodes. This is solely
  858. /// to allow co-allocation of node operands with the node itself.
  859. class BinarySDNode : public SDNode {
  860. SDUse Ops[2];
  861. public:
  862. BinarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
  863. SDValue X, SDValue Y)
  864. : SDNode(Opc, Order, dl, VTs) {
  865. InitOperands(Ops, X, Y);
  866. }
  867. };
  868. /// Returns true if the opcode is a binary operation with flags.
  869. static bool isBinOpWithFlags(unsigned Opcode) {
  870. switch (Opcode) {
  871. case ISD::SDIV:
  872. case ISD::UDIV:
  873. case ISD::SRA:
  874. case ISD::SRL:
  875. case ISD::MUL:
  876. case ISD::ADD:
  877. case ISD::SUB:
  878. case ISD::SHL:
  879. case ISD::FADD:
  880. case ISD::FDIV:
  881. case ISD::FMUL:
  882. case ISD::FREM:
  883. case ISD::FSUB:
  884. return true;
  885. default:
  886. return false;
  887. }
  888. }
  889. /// This class is an extension of BinarySDNode
  890. /// used from those opcodes that have associated extra flags.
  891. class BinaryWithFlagsSDNode : public BinarySDNode {
  892. public:
  893. SDNodeFlags Flags;
  894. BinaryWithFlagsSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
  895. SDValue X, SDValue Y, const SDNodeFlags &NodeFlags)
  896. : BinarySDNode(Opc, Order, dl, VTs, X, Y), Flags(NodeFlags) {}
  897. static bool classof(const SDNode *N) {
  898. return isBinOpWithFlags(N->getOpcode());
  899. }
  900. };
  901. /// This class is used for three-operand SDNodes. This is solely
  902. /// to allow co-allocation of node operands with the node itself.
  903. class TernarySDNode : public SDNode {
  904. SDUse Ops[3];
  905. public:
  906. TernarySDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
  907. SDValue X, SDValue Y, SDValue Z)
  908. : SDNode(Opc, Order, dl, VTs) {
  909. InitOperands(Ops, X, Y, Z);
  910. }
  911. };
  912. /// This class is used to form a handle around another node that
  913. /// is persistent and is updated across invocations of replaceAllUsesWith on its
  914. /// operand. This node should be directly created by end-users and not added to
  915. /// the AllNodes list.
  916. class HandleSDNode : public SDNode {
  917. SDUse Op;
  918. public:
  919. explicit HandleSDNode(SDValue X)
  920. : SDNode(ISD::HANDLENODE, 0, DebugLoc(), getSDVTList(MVT::Other)) {
  921. InitOperands(&Op, X);
  922. }
  923. ~HandleSDNode();
  924. const SDValue &getValue() const { return Op; }
  925. };
  926. class AddrSpaceCastSDNode : public UnarySDNode {
  927. private:
  928. unsigned SrcAddrSpace;
  929. unsigned DestAddrSpace;
  930. public:
  931. AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT, SDValue X,
  932. unsigned SrcAS, unsigned DestAS);
  933. unsigned getSrcAddressSpace() const { return SrcAddrSpace; }
  934. unsigned getDestAddressSpace() const { return DestAddrSpace; }
  935. static bool classof(const SDNode *N) {
  936. return N->getOpcode() == ISD::ADDRSPACECAST;
  937. }
  938. };
  939. /// This is an abstract virtual class for memory operations.
  940. class MemSDNode : public SDNode {
  941. private:
  942. // VT of in-memory value.
  943. EVT MemoryVT;
  944. protected:
  945. /// Memory reference information.
  946. MachineMemOperand *MMO;
  947. public:
  948. MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
  949. EVT MemoryVT, MachineMemOperand *MMO);
  950. MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
  951. ArrayRef<SDValue> Ops, EVT MemoryVT, MachineMemOperand *MMO);
  952. bool readMem() const { return MMO->isLoad(); }
  953. bool writeMem() const { return MMO->isStore(); }
  954. /// Returns alignment and volatility of the memory access
  955. unsigned getOriginalAlignment() const {
  956. return MMO->getBaseAlignment();
  957. }
  958. unsigned getAlignment() const {
  959. return MMO->getAlignment();
  960. }
  961. /// Return the SubclassData value, which contains an
  962. /// encoding of the volatile flag, as well as bits used by subclasses. This
  963. /// function should only be used to compute a FoldingSetNodeID value.
  964. unsigned getRawSubclassData() const {
  965. return SubclassData;
  966. }
  967. // We access subclass data here so that we can check consistency
  968. // with MachineMemOperand information.
  969. bool isVolatile() const { return (SubclassData >> 5) & 1; }
  970. bool isNonTemporal() const { return (SubclassData >> 6) & 1; }
  971. bool isInvariant() const { return (SubclassData >> 7) & 1; }
  972. AtomicOrdering getOrdering() const {
  973. return AtomicOrdering((SubclassData >> 8) & 15);
  974. }
  975. SynchronizationScope getSynchScope() const {
  976. return SynchronizationScope((SubclassData >> 12) & 1);
  977. }
  978. // Returns the offset from the location of the access.
  979. int64_t getSrcValueOffset() const { return MMO->getOffset(); }
  980. /// Returns the AA info that describes the dereference.
  981. AAMDNodes getAAInfo() const { return MMO->getAAInfo(); }
  982. /// Returns the Ranges that describes the dereference.
  983. const MDNode *getRanges() const { return MMO->getRanges(); }
  984. /// Return the type of the in-memory value.
  985. EVT getMemoryVT() const { return MemoryVT; }
  986. /// Return a MachineMemOperand object describing the memory
  987. /// reference performed by operation.
  988. MachineMemOperand *getMemOperand() const { return MMO; }
  989. const MachinePointerInfo &getPointerInfo() const {
  990. return MMO->getPointerInfo();
  991. }
  992. /// Return the address space for the associated pointer
  993. unsigned getAddressSpace() const {
  994. return getPointerInfo().getAddrSpace();
  995. }
  996. /// Update this MemSDNode's MachineMemOperand information
  997. /// to reflect the alignment of NewMMO, if it has a greater alignment.
  998. /// This must only be used when the new alignment applies to all users of
  999. /// this MachineMemOperand.
  1000. void refineAlignment(const MachineMemOperand *NewMMO) {
  1001. MMO->refineAlignment(NewMMO);
  1002. }
  1003. const SDValue &getChain() const { return getOperand(0); }
  1004. const SDValue &getBasePtr() const {
  1005. return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
  1006. }
  1007. // Methods to support isa and dyn_cast
  1008. static bool classof(const SDNode *N) {
  1009. // For some targets, we lower some target intrinsics to a MemIntrinsicNode
  1010. // with either an intrinsic or a target opcode.
  1011. return N->getOpcode() == ISD::LOAD ||
  1012. N->getOpcode() == ISD::STORE ||
  1013. N->getOpcode() == ISD::PREFETCH ||
  1014. N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
  1015. N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS ||
  1016. N->getOpcode() == ISD::ATOMIC_SWAP ||
  1017. N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
  1018. N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
  1019. N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
  1020. N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
  1021. N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
  1022. N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
  1023. N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
  1024. N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
  1025. N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
  1026. N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
  1027. N->getOpcode() == ISD::ATOMIC_LOAD ||
  1028. N->getOpcode() == ISD::ATOMIC_STORE ||
  1029. N->getOpcode() == ISD::MLOAD ||
  1030. N->getOpcode() == ISD::MSTORE ||
  1031. N->getOpcode() == ISD::MGATHER ||
  1032. N->getOpcode() == ISD::MSCATTER ||
  1033. N->isMemIntrinsic() ||
  1034. N->isTargetMemoryOpcode();
  1035. }
  1036. };
  1037. /// This is an SDNode representing atomic operations.
  1038. class AtomicSDNode : public MemSDNode {
  1039. SDUse Ops[4];
  1040. /// For cmpxchg instructions, the ordering requirements when a store does not
  1041. /// occur.
  1042. AtomicOrdering FailureOrdering;
  1043. void InitAtomic(AtomicOrdering SuccessOrdering,
  1044. AtomicOrdering FailureOrdering,
  1045. SynchronizationScope SynchScope) {
  1046. // This must match encodeMemSDNodeFlags() in SelectionDAG.cpp.
  1047. assert((SuccessOrdering & 15) == SuccessOrdering &&
  1048. "Ordering may not require more than 4 bits!");
  1049. assert((FailureOrdering & 15) == FailureOrdering &&
  1050. "Ordering may not require more than 4 bits!");
  1051. assert((SynchScope & 1) == SynchScope &&
  1052. "SynchScope may not require more than 1 bit!");
  1053. SubclassData |= SuccessOrdering << 8;
  1054. SubclassData |= SynchScope << 12;
  1055. this->FailureOrdering = FailureOrdering;
  1056. assert(getSuccessOrdering() == SuccessOrdering &&
  1057. "Ordering encoding error!");
  1058. assert(getFailureOrdering() == FailureOrdering &&
  1059. "Ordering encoding error!");
  1060. assert(getSynchScope() == SynchScope && "Synch-scope encoding error!");
  1061. }
  1062. public:
  1063. // Opc: opcode for atomic
  1064. // VTL: value type list
  1065. // Chain: memory chain for operaand
  1066. // Ptr: address to update as a SDValue
  1067. // Cmp: compare value
  1068. // Swp: swap value
  1069. // SrcVal: address to update as a Value (used for MemOperand)
  1070. // Align: alignment of memory
  1071. AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
  1072. EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp,
  1073. MachineMemOperand *MMO, AtomicOrdering Ordering,
  1074. SynchronizationScope SynchScope)
  1075. : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
  1076. InitAtomic(Ordering, Ordering, SynchScope);
  1077. InitOperands(Ops, Chain, Ptr, Cmp, Swp);
  1078. }
  1079. AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
  1080. EVT MemVT,
  1081. SDValue Chain, SDValue Ptr,
  1082. SDValue Val, MachineMemOperand *MMO,
  1083. AtomicOrdering Ordering, SynchronizationScope SynchScope)
  1084. : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
  1085. InitAtomic(Ordering, Ordering, SynchScope);
  1086. InitOperands(Ops, Chain, Ptr, Val);
  1087. }
  1088. AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL,
  1089. EVT MemVT,
  1090. SDValue Chain, SDValue Ptr,
  1091. MachineMemOperand *MMO,
  1092. AtomicOrdering Ordering, SynchronizationScope SynchScope)
  1093. : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
  1094. InitAtomic(Ordering, Ordering, SynchScope);
  1095. InitOperands(Ops, Chain, Ptr);
  1096. }
  1097. AtomicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTL, EVT MemVT,
  1098. const SDValue* AllOps, SDUse *DynOps, unsigned NumOps,
  1099. MachineMemOperand *MMO,
  1100. AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
  1101. SynchronizationScope SynchScope)
  1102. : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
  1103. InitAtomic(SuccessOrdering, FailureOrdering, SynchScope);
  1104. assert((DynOps || NumOps <= array_lengthof(Ops)) &&
  1105. "Too many ops for internal storage!");
  1106. InitOperands(DynOps ? DynOps : Ops, AllOps, NumOps);
  1107. }
  1108. const SDValue &getBasePtr() const { return getOperand(1); }
  1109. const SDValue &getVal() const { return getOperand(2); }
  1110. AtomicOrdering getSuccessOrdering() const {
  1111. return getOrdering();
  1112. }
  1113. // Not quite enough room in SubclassData for everything, so failure gets its
  1114. // own field.
  1115. AtomicOrdering getFailureOrdering() const {
  1116. return FailureOrdering;
  1117. }
  1118. bool isCompareAndSwap() const {
  1119. unsigned Op = getOpcode();
  1120. return Op == ISD::ATOMIC_CMP_SWAP || Op == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS;
  1121. }
  1122. // Methods to support isa and dyn_cast
  1123. static bool classof(const SDNode *N) {
  1124. return N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
  1125. N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS ||
  1126. N->getOpcode() == ISD::ATOMIC_SWAP ||
  1127. N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
  1128. N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
  1129. N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
  1130. N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
  1131. N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
  1132. N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
  1133. N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
  1134. N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
  1135. N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
  1136. N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
  1137. N->getOpcode() == ISD::ATOMIC_LOAD ||
  1138. N->getOpcode() == ISD::ATOMIC_STORE;
  1139. }
  1140. };
  1141. /// This SDNode is used for target intrinsics that touch
  1142. /// memory and need an associated MachineMemOperand. Its opcode may be
  1143. /// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
  1144. /// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
  1145. class MemIntrinsicSDNode : public MemSDNode {
  1146. public:
  1147. MemIntrinsicSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
  1148. ArrayRef<SDValue> Ops, EVT MemoryVT,
  1149. MachineMemOperand *MMO)
  1150. : MemSDNode(Opc, Order, dl, VTs, Ops, MemoryVT, MMO) {
  1151. SubclassData |= 1u << 13;
  1152. }
  1153. // Methods to support isa and dyn_cast
  1154. static bool classof(const SDNode *N) {
  1155. // We lower some target intrinsics to their target opcode
  1156. // early a node with a target opcode can be of this class
  1157. return N->isMemIntrinsic() ||
  1158. N->getOpcode() == ISD::PREFETCH ||
  1159. N->isTargetMemoryOpcode();
  1160. }
  1161. };
  1162. /// This SDNode is used to implement the code generator
  1163. /// support for the llvm IR shufflevector instruction. It combines elements
  1164. /// from two input vectors into a new input vector, with the selection and
  1165. /// ordering of elements determined by an array of integers, referred to as
  1166. /// the shuffle mask. For input vectors of width N, mask indices of 0..N-1
  1167. /// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
  1168. /// An index of -1 is treated as undef, such that the code generator may put
  1169. /// any value in the corresponding element of the result.
  1170. class ShuffleVectorSDNode : public SDNode {
  1171. SDUse Ops[2];
  1172. // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
  1173. // is freed when the SelectionDAG object is destroyed.
  1174. const int *Mask;
  1175. protected:
  1176. friend class SelectionDAG;
  1177. ShuffleVectorSDNode(EVT VT, unsigned Order, DebugLoc dl, SDValue N1,
  1178. SDValue N2, const int *M)
  1179. : SDNode(ISD::VECTOR_SHUFFLE, Order, dl, getSDVTList(VT)), Mask(M) {
  1180. InitOperands(Ops, N1, N2);
  1181. }
  1182. public:
  1183. ArrayRef<int> getMask() const {
  1184. EVT VT = getValueType(0);
  1185. return makeArrayRef(Mask, VT.getVectorNumElements());
  1186. }
  1187. int getMaskElt(unsigned Idx) const {
  1188. assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!");
  1189. return Mask[Idx];
  1190. }
  1191. bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
  1192. int getSplatIndex() const {
  1193. assert(isSplat() && "Cannot get splat index for non-splat!");
  1194. EVT VT = getValueType(0);
  1195. for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
  1196. if (Mask[i] >= 0)
  1197. return Mask[i];
  1198. }
  1199. llvm_unreachable("Splat with all undef indices?");
  1200. }
  1201. static bool isSplatMask(const int *Mask, EVT VT);
  1202. /// Change values in a shuffle permute mask assuming
  1203. /// the two vector operands have swapped position.
  1204. static void commuteMask(SmallVectorImpl<int> &Mask) {
  1205. unsigned NumElems = Mask.size();
  1206. for (unsigned i = 0; i != NumElems; ++i) {
  1207. int idx = Mask[i];
  1208. if (idx < 0)
  1209. continue;
  1210. else if (idx < (int)NumElems)
  1211. Mask[i] = idx + NumElems;
  1212. else
  1213. Mask[i] = idx - NumElems;
  1214. }
  1215. }
  1216. static bool classof(const SDNode *N) {
  1217. return N->getOpcode() == ISD::VECTOR_SHUFFLE;
  1218. }
  1219. };
  1220. class ConstantSDNode : public SDNode {
  1221. const ConstantInt *Value;
  1222. friend class SelectionDAG;
  1223. ConstantSDNode(bool isTarget, bool isOpaque, const ConstantInt *val,
  1224. DebugLoc DL, EVT VT)
  1225. : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant,
  1226. 0, DL, getSDVTList(VT)), Value(val) {
  1227. SubclassData |= (uint16_t)isOpaque;
  1228. }
  1229. public:
  1230. const ConstantInt *getConstantIntValue() const { return Value; }
  1231. const APInt &getAPIntValue() const { return Value->getValue(); }
  1232. uint64_t getZExtValue() const { return Value->getZExtValue(); }
  1233. int64_t getSExtValue() const { return Value->getSExtValue(); }
  1234. bool isOne() const { return Value->isOne(); }
  1235. bool isNullValue() const { return Value->isNullValue(); }
  1236. bool isAllOnesValue() const { return Value->isAllOnesValue(); }
  1237. bool isOpaque() const { return SubclassData & 1; }
  1238. static bool classof(const SDNode *N) {
  1239. return N->getOpcode() == ISD::Constant ||
  1240. N->getOpcode() == ISD::TargetConstant;
  1241. }
  1242. };
  1243. class ConstantFPSDNode : public SDNode {
  1244. const ConstantFP *Value;
  1245. friend class SelectionDAG;
  1246. ConstantFPSDNode(bool isTarget, const ConstantFP *val, DebugLoc DL, EVT VT)
  1247. : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP,
  1248. 0, DL, getSDVTList(VT)), Value(val) {
  1249. }
  1250. public:
  1251. const APFloat& getValueAPF() const { return Value->getValueAPF(); }
  1252. const ConstantFP *getConstantFPValue() const { return Value; }
  1253. /// Return true if the value is positive or negative zero.
  1254. bool isZero() const { return Value->isZero(); }
  1255. /// Return true if the value is a NaN.
  1256. bool isNaN() const { return Value->isNaN(); }
  1257. /// Return true if the value is an infinity
  1258. bool isInfinity() const { return Value->isInfinity(); }
  1259. /// Return true if the value is negative.
  1260. bool isNegative() const { return Value->isNegative(); }
  1261. /// We don't rely on operator== working on double values, as
  1262. /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
  1263. /// As such, this method can be used to do an exact bit-for-bit comparison of
  1264. /// two floating point values.
  1265. /// We leave the version with the double argument here because it's just so
  1266. /// convenient to write "2.0" and the like. Without this function we'd
  1267. /// have to duplicate its logic everywhere it's called.
  1268. bool isExactlyValue(double V) const {
  1269. bool ignored;
  1270. APFloat Tmp(V);
  1271. Tmp.convert(Value->getValueAPF().getSemantics(),
  1272. APFloat::rmNearestTiesToEven, &ignored);
  1273. return isExactlyValue(Tmp);
  1274. }
  1275. bool isExactlyValue(const APFloat& V) const;
  1276. static bool isValueValidForType(EVT VT, const APFloat& Val);
  1277. static bool classof(const SDNode *N) {
  1278. return N->getOpcode() == ISD::ConstantFP ||
  1279. N->getOpcode() == ISD::TargetConstantFP;
  1280. }
  1281. };
  1282. class GlobalAddressSDNode : public SDNode {
  1283. const GlobalValue *TheGlobal;
  1284. int64_t Offset;
  1285. unsigned char TargetFlags;
  1286. friend class SelectionDAG;
  1287. GlobalAddressSDNode(unsigned Opc, unsigned Order, DebugLoc DL,
  1288. const GlobalValue *GA, EVT VT, int64_t o,
  1289. unsigned char TargetFlags);
  1290. public:
  1291. const GlobalValue *getGlobal() const { return TheGlobal; }
  1292. int64_t getOffset() const { return Offset; }
  1293. unsigned char getTargetFlags() const { return TargetFlags; }
  1294. // Return the address space this GlobalAddress belongs to.
  1295. unsigned getAddressSpace() const;
  1296. static bool classof(const SDNode *N) {
  1297. return N->getOpcode() == ISD::GlobalAddress ||
  1298. N->getOpcode() == ISD::TargetGlobalAddress ||
  1299. N->getOpcode() == ISD::GlobalTLSAddress ||
  1300. N->getOpcode() == ISD::TargetGlobalTLSAddress;
  1301. }
  1302. };
  1303. class FrameIndexSDNode : public SDNode {
  1304. int FI;
  1305. friend class SelectionDAG;
  1306. FrameIndexSDNode(int fi, EVT VT, bool isTarg)
  1307. : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
  1308. 0, DebugLoc(), getSDVTList(VT)), FI(fi) {
  1309. }
  1310. public:
  1311. int getIndex() const { return FI; }
  1312. static bool classof(const SDNode *N) {
  1313. return N->getOpcode() == ISD::FrameIndex ||
  1314. N->getOpcode() == ISD::TargetFrameIndex;
  1315. }
  1316. };
  1317. class JumpTableSDNode : public SDNode {
  1318. int JTI;
  1319. unsigned char TargetFlags;
  1320. friend class SelectionDAG;
  1321. JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned char TF)
  1322. : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
  1323. 0, DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
  1324. }
  1325. public:
  1326. int getIndex() const { return JTI; }
  1327. unsigned char getTargetFlags() const { return TargetFlags; }
  1328. static bool classof(const SDNode *N) {
  1329. return N->getOpcode() == ISD::JumpTable ||
  1330. N->getOpcode() == ISD::TargetJumpTable;
  1331. }
  1332. };
  1333. class ConstantPoolSDNode : public SDNode {
  1334. union {
  1335. const Constant *ConstVal;
  1336. MachineConstantPoolValue *MachineCPVal;
  1337. } Val;
  1338. int Offset; // It's a MachineConstantPoolValue if top bit is set.
  1339. unsigned Alignment; // Minimum alignment requirement of CP (not log2 value).
  1340. unsigned char TargetFlags;
  1341. friend class SelectionDAG;
  1342. ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
  1343. unsigned Align, unsigned char TF)
  1344. : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
  1345. DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
  1346. TargetFlags(TF) {
  1347. assert(Offset >= 0 && "Offset is too large");
  1348. Val.ConstVal = c;
  1349. }
  1350. ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v,
  1351. EVT VT, int o, unsigned Align, unsigned char TF)
  1352. : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
  1353. DebugLoc(), getSDVTList(VT)), Offset(o), Alignment(Align),
  1354. TargetFlags(TF) {
  1355. assert(Offset >= 0 && "Offset is too large");
  1356. Val.MachineCPVal = v;
  1357. Offset |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
  1358. }
  1359. public:
  1360. bool isMachineConstantPoolEntry() const {
  1361. return Offset < 0;
  1362. }
  1363. const Constant *getConstVal() const {
  1364. assert(!isMachineConstantPoolEntry() && "Wrong constantpool type");
  1365. return Val.ConstVal;
  1366. }
  1367. MachineConstantPoolValue *getMachineCPVal() const {
  1368. assert(isMachineConstantPoolEntry() && "Wrong constantpool type");
  1369. return Val.MachineCPVal;
  1370. }
  1371. int getOffset() const {
  1372. return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
  1373. }
  1374. // Return the alignment of this constant pool object, which is either 0 (for
  1375. // default alignment) or the desired value.
  1376. unsigned getAlignment() const { return Alignment; }
  1377. unsigned char getTargetFlags() const { return TargetFlags; }
  1378. Type *getType() const;
  1379. static bool classof(const SDNode *N) {
  1380. return N->getOpcode() == ISD::ConstantPool ||
  1381. N->getOpcode() == ISD::TargetConstantPool;
  1382. }
  1383. };
  1384. /// Completely target-dependent object reference.
  1385. class TargetIndexSDNode : public SDNode {
  1386. unsigned char TargetFlags;
  1387. int Index;
  1388. int64_t Offset;
  1389. friend class SelectionDAG;
  1390. public:
  1391. TargetIndexSDNode(int Idx, EVT VT, int64_t Ofs, unsigned char TF)
  1392. : SDNode(ISD::TargetIndex, 0, DebugLoc(), getSDVTList(VT)),
  1393. TargetFlags(TF), Index(Idx), Offset(Ofs) {}
  1394. public:
  1395. unsigned char getTargetFlags() const { return TargetFlags; }
  1396. int getIndex() const { return Index; }
  1397. int64_t getOffset() const { return Offset; }
  1398. static bool classof(const SDNode *N) {
  1399. return N->getOpcode() == ISD::TargetIndex;
  1400. }
  1401. };
  1402. class BasicBlockSDNode : public SDNode {
  1403. MachineBasicBlock *MBB;
  1404. friend class SelectionDAG;
  1405. /// Debug info is meaningful and potentially useful here, but we create
  1406. /// blocks out of order when they're jumped to, which makes it a bit
  1407. /// harder. Let's see if we need it first.
  1408. explicit BasicBlockSDNode(MachineBasicBlock *mbb)
  1409. : SDNode(ISD::BasicBlock, 0, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb)
  1410. {}
  1411. public:
  1412. MachineBasicBlock *getBasicBlock() const { return MBB; }
  1413. static bool classof(const SDNode *N) {
  1414. return N->getOpcode() == ISD::BasicBlock;
  1415. }
  1416. };
  1417. /// A "pseudo-class" with methods for operating on BUILD_VECTORs.
  1418. class BuildVectorSDNode : public SDNode {
  1419. // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
  1420. explicit BuildVectorSDNode() = delete;
  1421. public:
  1422. /// Check if this is a constant splat, and if so, find the
  1423. /// smallest element size that splats the vector. If MinSplatBits is
  1424. /// nonzero, the element size must be at least that large. Note that the
  1425. /// splat element may be the entire vector (i.e., a one element vector).
  1426. /// Returns the splat element value in SplatValue. Any undefined bits in
  1427. /// that value are zero, and the corresponding bits in the SplatUndef mask
  1428. /// are set. The SplatBitSize value is set to the splat element size in
  1429. /// bits. HasAnyUndefs is set to true if any bits in the vector are
  1430. /// undefined. isBigEndian describes the endianness of the target.
  1431. bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
  1432. unsigned &SplatBitSize, bool &HasAnyUndefs,
  1433. unsigned MinSplatBits = 0,
  1434. bool isBigEndian = false) const;
  1435. /// \brief Returns the splatted value or a null value if this is not a splat.
  1436. ///
  1437. /// If passed a non-null UndefElements bitvector, it will resize it to match
  1438. /// the vector width and set the bits where elements are undef.
  1439. SDValue getSplatValue(BitVector *UndefElements = nullptr) const;
  1440. /// \brief Returns the splatted constant or null if this is not a constant
  1441. /// splat.
  1442. ///
  1443. /// If passed a non-null UndefElements bitvector, it will resize it to match
  1444. /// the vector width and set the bits where elements are undef.
  1445. ConstantSDNode *
  1446. getConstantSplatNode(BitVector *UndefElements = nullptr) const;
  1447. /// \brief Returns the splatted constant FP or null if this is not a constant
  1448. /// FP splat.
  1449. ///
  1450. /// If passed a non-null UndefElements bitvector, it will resize it to match
  1451. /// the vector width and set the bits where elements are undef.
  1452. ConstantFPSDNode *
  1453. getConstantFPSplatNode(BitVector *UndefElements = nullptr) const;
  1454. bool isConstant() const;
  1455. static inline bool classof(const SDNode *N) {
  1456. return N->getOpcode() == ISD::BUILD_VECTOR;
  1457. }
  1458. };
  1459. /// An SDNode that holds an arbitrary LLVM IR Value. This is
  1460. /// used when the SelectionDAG needs to make a simple reference to something
  1461. /// in the LLVM IR representation.
  1462. ///
  1463. class SrcValueSDNode : public SDNode {
  1464. const Value *V;
  1465. friend class SelectionDAG;
  1466. /// Create a SrcValue for a general value.
  1467. explicit SrcValueSDNode(const Value *v)
  1468. : SDNode(ISD::SRCVALUE, 0, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
  1469. public:
  1470. /// Return the contained Value.
  1471. const Value *getValue() const { return V; }
  1472. static bool classof(const SDNode *N) {
  1473. return N->getOpcode() == ISD::SRCVALUE;
  1474. }
  1475. };
  1476. class MDNodeSDNode : public SDNode {
  1477. const MDNode *MD;
  1478. friend class SelectionDAG;
  1479. explicit MDNodeSDNode(const MDNode *md)
  1480. : SDNode(ISD::MDNODE_SDNODE, 0, DebugLoc(), getSDVTList(MVT::Other)), MD(md)
  1481. {}
  1482. public:
  1483. const MDNode *getMD() const { return MD; }
  1484. static bool classof(const SDNode *N) {
  1485. return N->getOpcode() == ISD::MDNODE_SDNODE;
  1486. }
  1487. };
  1488. class RegisterSDNode : public SDNode {
  1489. unsigned Reg;
  1490. friend class SelectionDAG;
  1491. RegisterSDNode(unsigned reg, EVT VT)
  1492. : SDNode(ISD::Register, 0, DebugLoc(), getSDVTList(VT)), Reg(reg) {
  1493. }
  1494. public:
  1495. unsigned getReg() const { return Reg; }
  1496. static bool classof(const SDNode *N) {
  1497. return N->getOpcode() == ISD::Register;
  1498. }
  1499. };
  1500. class RegisterMaskSDNode : public SDNode {
  1501. // The memory for RegMask is not owned by the node.
  1502. const uint32_t *RegMask;
  1503. friend class SelectionDAG;
  1504. RegisterMaskSDNode(const uint32_t *mask)
  1505. : SDNode(ISD::RegisterMask, 0, DebugLoc(), getSDVTList(MVT::Untyped)),
  1506. RegMask(mask) {}
  1507. public:
  1508. const uint32_t *getRegMask() const { return RegMask; }
  1509. static bool classof(const SDNode *N) {
  1510. return N->getOpcode() == ISD::RegisterMask;
  1511. }
  1512. };
  1513. class BlockAddressSDNode : public SDNode {
  1514. const BlockAddress *BA;
  1515. int64_t Offset;
  1516. unsigned char TargetFlags;
  1517. friend class SelectionDAG;
  1518. BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
  1519. int64_t o, unsigned char Flags)
  1520. : SDNode(NodeTy, 0, DebugLoc(), getSDVTList(VT)),
  1521. BA(ba), Offset(o), TargetFlags(Flags) {
  1522. }
  1523. public:
  1524. const BlockAddress *getBlockAddress() const { return BA; }
  1525. int64_t getOffset() const { return Offset; }
  1526. unsigned char getTargetFlags() const { return TargetFlags; }
  1527. static bool classof(const SDNode *N) {
  1528. return N->getOpcode() == ISD::BlockAddress ||
  1529. N->getOpcode() == ISD::TargetBlockAddress;
  1530. }
  1531. };
  1532. class EHLabelSDNode : public SDNode {
  1533. SDUse Chain;
  1534. MCSymbol *Label;
  1535. friend class SelectionDAG;
  1536. EHLabelSDNode(unsigned Order, DebugLoc dl, SDValue ch, MCSymbol *L)
  1537. : SDNode(ISD::EH_LABEL, Order, dl, getSDVTList(MVT::Other)), Label(L) {
  1538. InitOperands(&Chain, ch);
  1539. }
  1540. public:
  1541. MCSymbol *getLabel() const { return Label; }
  1542. static bool classof(const SDNode *N) {
  1543. return N->getOpcode() == ISD::EH_LABEL;
  1544. }
  1545. };
  1546. class ExternalSymbolSDNode : public SDNode {
  1547. const char *Symbol;
  1548. unsigned char TargetFlags;
  1549. friend class SelectionDAG;
  1550. ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned char TF, EVT VT)
  1551. : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol,
  1552. 0, DebugLoc(), getSDVTList(VT)), Symbol(Sym), TargetFlags(TF) {
  1553. }
  1554. public:
  1555. const char *getSymbol() const { return Symbol; }
  1556. unsigned char getTargetFlags() const { return TargetFlags; }
  1557. static bool classof(const SDNode *N) {
  1558. return N->getOpcode() == ISD::ExternalSymbol ||
  1559. N->getOpcode() == ISD::TargetExternalSymbol;
  1560. }
  1561. };
  1562. class MCSymbolSDNode : public SDNode {
  1563. MCSymbol *Symbol;
  1564. friend class SelectionDAG;
  1565. MCSymbolSDNode(MCSymbol *Symbol, EVT VT)
  1566. : SDNode(ISD::MCSymbol, 0, DebugLoc(), getSDVTList(VT)), Symbol(Symbol) {}
  1567. public:
  1568. MCSymbol *getMCSymbol() const { return Symbol; }
  1569. static bool classof(const SDNode *N) {
  1570. return N->getOpcode() == ISD::MCSymbol;
  1571. }
  1572. };
  1573. class CondCodeSDNode : public SDNode {
  1574. ISD::CondCode Condition;
  1575. friend class SelectionDAG;
  1576. explicit CondCodeSDNode(ISD::CondCode Cond)
  1577. : SDNode(ISD::CONDCODE, 0, DebugLoc(), getSDVTList(MVT::Other)),
  1578. Condition(Cond) {
  1579. }
  1580. public:
  1581. ISD::CondCode get() const { return Condition; }
  1582. static bool classof(const SDNode *N) {
  1583. return N->getOpcode() == ISD::CONDCODE;
  1584. }
  1585. };
  1586. /// NOTE: avoid using this node as this may disappear in the
  1587. /// future and most targets don't support it.
  1588. class CvtRndSatSDNode : public SDNode {
  1589. ISD::CvtCode CvtCode;
  1590. friend class SelectionDAG;
  1591. explicit CvtRndSatSDNode(EVT VT, unsigned Order, DebugLoc dl,
  1592. ArrayRef<SDValue> Ops, ISD::CvtCode Code)
  1593. : SDNode(ISD::CONVERT_RNDSAT, Order, dl, getSDVTList(VT), Ops),
  1594. CvtCode(Code) {
  1595. assert(Ops.size() == 5 && "wrong number of operations");
  1596. }
  1597. public:
  1598. ISD::CvtCode getCvtCode() const { return CvtCode; }
  1599. static bool classof(const SDNode *N) {
  1600. return N->getOpcode() == ISD::CONVERT_RNDSAT;
  1601. }
  1602. };
  1603. /// This class is used to represent EVT's, which are used
  1604. /// to parameterize some operations.
  1605. class VTSDNode : public SDNode {
  1606. EVT ValueType;
  1607. friend class SelectionDAG;
  1608. explicit VTSDNode(EVT VT)
  1609. : SDNode(ISD::VALUETYPE, 0, DebugLoc(), getSDVTList(MVT::Other)),
  1610. ValueType(VT) {
  1611. }
  1612. public:
  1613. EVT getVT() const { return ValueType; }
  1614. static bool classof(const SDNode *N) {
  1615. return N->getOpcode() == ISD::VALUETYPE;
  1616. }
  1617. };
  1618. /// Base class for LoadSDNode and StoreSDNode
  1619. class LSBaseSDNode : public MemSDNode {
  1620. //! Operand array for load and store
  1621. /*!
  1622. \note Moving this array to the base class captures more
  1623. common functionality shared between LoadSDNode and
  1624. StoreSDNode
  1625. */
  1626. SDUse Ops[4];
  1627. public:
  1628. LSBaseSDNode(ISD::NodeType NodeTy, unsigned Order, DebugLoc dl,
  1629. SDValue *Operands, unsigned numOperands,
  1630. SDVTList VTs, ISD::MemIndexedMode AM, EVT MemVT,
  1631. MachineMemOperand *MMO)
  1632. : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
  1633. SubclassData |= AM << 2;
  1634. assert(getAddressingMode() == AM && "MemIndexedMode encoding error!");
  1635. InitOperands(Ops, Operands, numOperands);
  1636. assert((getOffset().getOpcode() == ISD::UNDEF || isIndexed()) &&
  1637. "Only indexed loads and stores have a non-undef offset operand");
  1638. }
  1639. const SDValue &getOffset() const {
  1640. return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
  1641. }
  1642. /// Return the addressing mode for this load or store:
  1643. /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
  1644. ISD::MemIndexedMode getAddressingMode() const {
  1645. return ISD::MemIndexedMode((SubclassData >> 2) & 7);
  1646. }
  1647. /// Return true if this is a pre/post inc/dec load/store.
  1648. bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
  1649. /// Return true if this is NOT a pre/post inc/dec load/store.
  1650. bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
  1651. static bool classof(const SDNode *N) {
  1652. return N->getOpcode() == ISD::LOAD ||
  1653. N->getOpcode() == ISD::STORE;
  1654. }
  1655. };
  1656. /// This class is used to represent ISD::LOAD nodes.
  1657. class LoadSDNode : public LSBaseSDNode {
  1658. friend class SelectionDAG;
  1659. LoadSDNode(SDValue *ChainPtrOff, unsigned Order, DebugLoc dl, SDVTList VTs,
  1660. ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
  1661. MachineMemOperand *MMO)
  1662. : LSBaseSDNode(ISD::LOAD, Order, dl, ChainPtrOff, 3, VTs, AM, MemVT, MMO) {
  1663. SubclassData |= (unsigned short)ETy;
  1664. assert(getExtensionType() == ETy && "LoadExtType encoding error!");
  1665. assert(readMem() && "Load MachineMemOperand is not a load!");
  1666. assert(!writeMem() && "Load MachineMemOperand is a store!");
  1667. }
  1668. public:
  1669. /// Return whether this is a plain node,
  1670. /// or one of the varieties of value-extending loads.
  1671. ISD::LoadExtType getExtensionType() const {
  1672. return ISD::LoadExtType(SubclassData & 3);
  1673. }
  1674. const SDValue &getBasePtr() const { return getOperand(1); }
  1675. const SDValue &getOffset() const { return getOperand(2); }
  1676. static bool classof(const SDNode *N) {
  1677. return N->getOpcode() == ISD::LOAD;
  1678. }
  1679. };
  1680. /// This class is used to represent ISD::STORE nodes.
  1681. class StoreSDNode : public LSBaseSDNode {
  1682. friend class SelectionDAG;
  1683. StoreSDNode(SDValue *ChainValuePtrOff, unsigned Order, DebugLoc dl,
  1684. SDVTList VTs, ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
  1685. MachineMemOperand *MMO)
  1686. : LSBaseSDNode(ISD::STORE, Order, dl, ChainValuePtrOff, 4,
  1687. VTs, AM, MemVT, MMO) {
  1688. SubclassData |= (unsigned short)isTrunc;
  1689. assert(isTruncatingStore() == isTrunc && "isTrunc encoding error!");
  1690. assert(!readMem() && "Store MachineMemOperand is a load!");
  1691. assert(writeMem() && "Store MachineMemOperand is not a store!");
  1692. }
  1693. public:
  1694. /// Return true if the op does a truncation before store.
  1695. /// For integers this is the same as doing a TRUNCATE and storing the result.
  1696. /// For floats, it is the same as doing an FP_ROUND and storing the result.
  1697. bool isTruncatingStore() const { return SubclassData & 1; }
  1698. const SDValue &getValue() const { return getOperand(1); }
  1699. const SDValue &getBasePtr() const { return getOperand(2); }
  1700. const SDValue &getOffset() const { return getOperand(3); }
  1701. static bool classof(const SDNode *N) {
  1702. return N->getOpcode() == ISD::STORE;
  1703. }
  1704. };
  1705. /// This base class is used to represent MLOAD and MSTORE nodes
  1706. class MaskedLoadStoreSDNode : public MemSDNode {
  1707. // Operands
  1708. SDUse Ops[4];
  1709. public:
  1710. friend class SelectionDAG;
  1711. MaskedLoadStoreSDNode(ISD::NodeType NodeTy, unsigned Order, DebugLoc dl,
  1712. SDValue *Operands, unsigned numOperands,
  1713. SDVTList VTs, EVT MemVT, MachineMemOperand *MMO)
  1714. : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
  1715. InitOperands(Ops, Operands, numOperands);
  1716. }
  1717. // In the both nodes address is Op1, mask is Op2:
  1718. // MaskedLoadSDNode (Chain, ptr, mask, src0), src0 is a passthru value
  1719. // MaskedStoreSDNode (Chain, ptr, mask, data)
  1720. // Mask is a vector of i1 elements
  1721. const SDValue &getBasePtr() const { return getOperand(1); }
  1722. const SDValue &getMask() const { return getOperand(2); }
  1723. static bool classof(const SDNode *N) {
  1724. return N->getOpcode() == ISD::MLOAD ||
  1725. N->getOpcode() == ISD::MSTORE;
  1726. }
  1727. };
  1728. /// This class is used to represent an MLOAD node
  1729. class MaskedLoadSDNode : public MaskedLoadStoreSDNode {
  1730. public:
  1731. friend class SelectionDAG;
  1732. MaskedLoadSDNode(unsigned Order, DebugLoc dl, SDValue *Operands,
  1733. unsigned numOperands, SDVTList VTs, ISD::LoadExtType ETy,
  1734. EVT MemVT, MachineMemOperand *MMO)
  1735. : MaskedLoadStoreSDNode(ISD::MLOAD, Order, dl, Operands, numOperands,
  1736. VTs, MemVT, MMO) {
  1737. SubclassData |= (unsigned short)ETy;
  1738. }
  1739. ISD::LoadExtType getExtensionType() const {
  1740. return ISD::LoadExtType(SubclassData & 3);
  1741. }
  1742. const SDValue &getSrc0() const { return getOperand(3); }
  1743. static bool classof(const SDNode *N) {
  1744. return N->getOpcode() == ISD::MLOAD;
  1745. }
  1746. };
  1747. /// This class is used to represent an MSTORE node
  1748. class MaskedStoreSDNode : public MaskedLoadStoreSDNode {
  1749. public:
  1750. friend class SelectionDAG;
  1751. MaskedStoreSDNode(unsigned Order, DebugLoc dl, SDValue *Operands,
  1752. unsigned numOperands, SDVTList VTs, bool isTrunc, EVT MemVT,
  1753. MachineMemOperand *MMO)
  1754. : MaskedLoadStoreSDNode(ISD::MSTORE, Order, dl, Operands, numOperands,
  1755. VTs, MemVT, MMO) {
  1756. SubclassData |= (unsigned short)isTrunc;
  1757. }
  1758. /// Return true if the op does a truncation before store.
  1759. /// For integers this is the same as doing a TRUNCATE and storing the result.
  1760. /// For floats, it is the same as doing an FP_ROUND and storing the result.
  1761. bool isTruncatingStore() const { return SubclassData & 1; }
  1762. const SDValue &getValue() const { return getOperand(3); }
  1763. static bool classof(const SDNode *N) {
  1764. return N->getOpcode() == ISD::MSTORE;
  1765. }
  1766. };
  1767. /// This is a base class used to represent
  1768. /// MGATHER and MSCATTER nodes
  1769. ///
  1770. class MaskedGatherScatterSDNode : public MemSDNode {
  1771. // Operands
  1772. SDUse Ops[5];
  1773. public:
  1774. friend class SelectionDAG;
  1775. MaskedGatherScatterSDNode(ISD::NodeType NodeTy, unsigned Order, DebugLoc dl,
  1776. ArrayRef<SDValue> Operands, SDVTList VTs, EVT MemVT,
  1777. MachineMemOperand *MMO)
  1778. : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
  1779. assert(Operands.size() == 5 && "Incompatible number of operands");
  1780. InitOperands(Ops, Operands.data(), Operands.size());
  1781. }
  1782. // In the both nodes address is Op1, mask is Op2:
  1783. // MaskedGatherSDNode (Chain, src0, mask, base, index), src0 is a passthru value
  1784. // MaskedScatterSDNode (Chain, value, mask, base, index)
  1785. // Mask is a vector of i1 elements
  1786. const SDValue &getBasePtr() const { return getOperand(3); }
  1787. const SDValue &getIndex() const { return getOperand(4); }
  1788. const SDValue &getMask() const { return getOperand(2); }
  1789. const SDValue &getValue() const { return getOperand(1); }
  1790. static bool classof(const SDNode *N) {
  1791. return N->getOpcode() == ISD::MGATHER ||
  1792. N->getOpcode() == ISD::MSCATTER;
  1793. }
  1794. };
  1795. /// This class is used to represent an MGATHER node
  1796. ///
  1797. class MaskedGatherSDNode : public MaskedGatherScatterSDNode {
  1798. public:
  1799. friend class SelectionDAG;
  1800. MaskedGatherSDNode(unsigned Order, DebugLoc dl, ArrayRef<SDValue> Operands,
  1801. SDVTList VTs, EVT MemVT, MachineMemOperand *MMO)
  1802. : MaskedGatherScatterSDNode(ISD::MGATHER, Order, dl, Operands, VTs, MemVT,
  1803. MMO) {
  1804. assert(getValue().getValueType() == getValueType(0) &&
  1805. "Incompatible type of the PathThru value in MaskedGatherSDNode");
  1806. assert(getMask().getValueType().getVectorNumElements() ==
  1807. getValueType(0).getVectorNumElements() &&
  1808. "Vector width mismatch between mask and data");
  1809. assert(getMask().getValueType().getScalarType() == MVT::i1 &&
  1810. "Vector width mismatch between mask and data");
  1811. }
  1812. static bool classof(const SDNode *N) {
  1813. return N->getOpcode() == ISD::MGATHER;
  1814. }
  1815. };
  1816. /// This class is used to represent an MSCATTER node
  1817. ///
  1818. class MaskedScatterSDNode : public MaskedGatherScatterSDNode {
  1819. public:
  1820. friend class SelectionDAG;
  1821. MaskedScatterSDNode(unsigned Order, DebugLoc dl,ArrayRef<SDValue> Operands,
  1822. SDVTList VTs, EVT MemVT, MachineMemOperand *MMO)
  1823. : MaskedGatherScatterSDNode(ISD::MSCATTER, Order, dl, Operands, VTs, MemVT,
  1824. MMO) {
  1825. assert(getMask().getValueType().getVectorNumElements() ==
  1826. getValue().getValueType().getVectorNumElements() &&
  1827. "Vector width mismatch between mask and data");
  1828. assert(getMask().getValueType().getScalarType() == MVT::i1 &&
  1829. "Vector width mismatch between mask and data");
  1830. }
  1831. static bool classof(const SDNode *N) {
  1832. return N->getOpcode() == ISD::MSCATTER;
  1833. }
  1834. };
  1835. /// An SDNode that represents everything that will be needed
  1836. /// to construct a MachineInstr. These nodes are created during the
  1837. /// instruction selection proper phase.
  1838. class MachineSDNode : public SDNode {
  1839. public:
  1840. typedef MachineMemOperand **mmo_iterator;
  1841. private:
  1842. friend class SelectionDAG;
  1843. MachineSDNode(unsigned Opc, unsigned Order, const DebugLoc DL, SDVTList VTs)
  1844. : SDNode(Opc, Order, DL, VTs), MemRefs(nullptr), MemRefsEnd(nullptr) {}
  1845. /// Operands for this instruction, if they fit here. If
  1846. /// they don't, this field is unused.
  1847. SDUse LocalOperands[4];
  1848. /// Memory reference descriptions for this instruction.
  1849. mmo_iterator MemRefs;
  1850. mmo_iterator MemRefsEnd;
  1851. public:
  1852. mmo_iterator memoperands_begin() const { return MemRefs; }
  1853. mmo_iterator memoperands_end() const { return MemRefsEnd; }
  1854. bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
  1855. /// Assign this MachineSDNodes's memory reference descriptor
  1856. /// list. This does not transfer ownership.
  1857. void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
  1858. for (mmo_iterator MMI = NewMemRefs, MME = NewMemRefsEnd; MMI != MME; ++MMI)
  1859. assert(*MMI && "Null mem ref detected!");
  1860. MemRefs = NewMemRefs;
  1861. MemRefsEnd = NewMemRefsEnd;
  1862. }
  1863. static bool classof(const SDNode *N) {
  1864. return N->isMachineOpcode();
  1865. }
  1866. };
  1867. class SDNodeIterator : public std::iterator<std::forward_iterator_tag,
  1868. SDNode, ptrdiff_t> {
  1869. const SDNode *Node;
  1870. unsigned Operand;
  1871. SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
  1872. public:
  1873. bool operator==(const SDNodeIterator& x) const {
  1874. return Operand == x.Operand;
  1875. }
  1876. bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
  1877. pointer operator*() const {
  1878. return Node->getOperand(Operand).getNode();
  1879. }
  1880. pointer operator->() const { return operator*(); }
  1881. SDNodeIterator& operator++() { // Preincrement
  1882. ++Operand;
  1883. return *this;
  1884. }
  1885. SDNodeIterator operator++(int) { // Postincrement
  1886. SDNodeIterator tmp = *this; ++*this; return tmp;
  1887. }
  1888. size_t operator-(SDNodeIterator Other) const {
  1889. assert(Node == Other.Node &&
  1890. "Cannot compare iterators of two different nodes!");
  1891. return Operand - Other.Operand;
  1892. }
  1893. static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); }
  1894. static SDNodeIterator end (const SDNode *N) {
  1895. return SDNodeIterator(N, N->getNumOperands());
  1896. }
  1897. unsigned getOperand() const { return Operand; }
  1898. const SDNode *getNode() const { return Node; }
  1899. };
  1900. template <> struct GraphTraits<SDNode*> {
  1901. typedef SDNode NodeType;
  1902. typedef SDNodeIterator ChildIteratorType;
  1903. static inline NodeType *getEntryNode(SDNode *N) { return N; }
  1904. static inline ChildIteratorType child_begin(NodeType *N) {
  1905. return SDNodeIterator::begin(N);
  1906. }
  1907. static inline ChildIteratorType child_end(NodeType *N) {
  1908. return SDNodeIterator::end(N);
  1909. }
  1910. };
  1911. /// The largest SDNode class.
  1912. typedef MaskedGatherScatterSDNode LargestSDNode;
  1913. /// The SDNode class with the greatest alignment requirement.
  1914. typedef GlobalAddressSDNode MostAlignedSDNode;
  1915. namespace ISD {
  1916. /// Returns true if the specified node is a non-extending and unindexed load.
  1917. inline bool isNormalLoad(const SDNode *N) {
  1918. const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
  1919. return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
  1920. Ld->getAddressingMode() == ISD::UNINDEXED;
  1921. }
  1922. /// Returns true if the specified node is a non-extending load.
  1923. inline bool isNON_EXTLoad(const SDNode *N) {
  1924. return isa<LoadSDNode>(N) &&
  1925. cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
  1926. }
  1927. /// Returns true if the specified node is a EXTLOAD.
  1928. inline bool isEXTLoad(const SDNode *N) {
  1929. return isa<LoadSDNode>(N) &&
  1930. cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
  1931. }
  1932. /// Returns true if the specified node is a SEXTLOAD.
  1933. inline bool isSEXTLoad(const SDNode *N) {
  1934. return isa<LoadSDNode>(N) &&
  1935. cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
  1936. }
  1937. /// Returns true if the specified node is a ZEXTLOAD.
  1938. inline bool isZEXTLoad(const SDNode *N) {
  1939. return isa<LoadSDNode>(N) &&
  1940. cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
  1941. }
  1942. /// Returns true if the specified node is an unindexed load.
  1943. inline bool isUNINDEXEDLoad(const SDNode *N) {
  1944. return isa<LoadSDNode>(N) &&
  1945. cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
  1946. }
  1947. /// Returns true if the specified node is a non-truncating
  1948. /// and unindexed store.
  1949. inline bool isNormalStore(const SDNode *N) {
  1950. const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
  1951. return St && !St->isTruncatingStore() &&
  1952. St->getAddressingMode() == ISD::UNINDEXED;
  1953. }
  1954. /// Returns true if the specified node is a non-truncating store.
  1955. inline bool isNON_TRUNCStore(const SDNode *N) {
  1956. return isa<StoreSDNode>(N) && !cast<StoreSDNode>(N)->isTruncatingStore();
  1957. }
  1958. /// Returns true if the specified node is a truncating store.
  1959. inline bool isTRUNCStore(const SDNode *N) {
  1960. return isa<StoreSDNode>(N) && cast<StoreSDNode>(N)->isTruncatingStore();
  1961. }
  1962. /// Returns true if the specified node is an unindexed store.
  1963. inline bool isUNINDEXEDStore(const SDNode *N) {
  1964. return isa<StoreSDNode>(N) &&
  1965. cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
  1966. }
  1967. }
  1968. } // end llvm namespace
  1969. #endif