SelectionDAG.h 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/CodeGen/SelectionDAG.h - InstSelection DAG ----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares the SelectionDAG class, and transitively defines the
  15. // SDNode class and subclasses.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CODEGEN_SELECTIONDAG_H
  19. #define LLVM_CODEGEN_SELECTIONDAG_H
  20. #include "llvm/ADT/APFloat.h"
  21. #include "llvm/ADT/APInt.h"
  22. #include "llvm/ADT/ArrayRef.h"
  23. #include "llvm/ADT/DenseMap.h"
  24. #include "llvm/ADT/DenseSet.h"
  25. #include "llvm/ADT/FoldingSet.h"
  26. #include "llvm/ADT/SmallVector.h"
  27. #include "llvm/ADT/StringMap.h"
  28. #include "llvm/ADT/ilist.h"
  29. #include "llvm/ADT/iterator.h"
  30. #include "llvm/ADT/iterator_range.h"
  31. #include "llvm/CodeGen/DAGCombine.h"
  32. #include "llvm/CodeGen/ISDOpcodes.h"
  33. #include "llvm/CodeGen/MachineFunction.h"
  34. #include "llvm/CodeGen/MachineMemOperand.h"
  35. #include "llvm/CodeGen/SelectionDAGNodes.h"
  36. #include "llvm/CodeGen/ValueTypes.h"
  37. #include "llvm/IR/DebugLoc.h"
  38. #include "llvm/IR/Metadata.h"
  39. #include "llvm/Support/Allocator.h"
  40. #include "llvm/Support/ArrayRecycler.h"
  41. #include "llvm/Support/CodeGen.h"
  42. #include "llvm/Support/ErrorHandling.h"
  43. #include "llvm/Support/MachineValueType.h"
  44. #include "llvm/Support/RecyclingAllocator.h"
  45. #include <cassert>
  46. #include <cstdint>
  47. #include <functional>
  48. #include <map>
  49. #include <string>
  50. #include <tuple>
  51. #include <utility>
  52. #include <vector>
  53. namespace llvm {
  54. class DIExpression;
  55. class DILabel;
  56. class DIVariable;
  57. class Function;
  58. class Pass;
  59. class Type;
  60. template <class GraphType> struct GraphTraits;
  61. template <typename T, unsigned int N> class SmallSetVector;
  62. template <typename T, typename Enable> struct FoldingSetTrait;
  63. class AAResults;
  64. class BlockAddress;
  65. class BlockFrequencyInfo;
  66. class Constant;
  67. class ConstantFP;
  68. class ConstantInt;
  69. class DataLayout;
  70. struct fltSemantics;
  71. class FunctionLoweringInfo;
  72. class FunctionVarLocs;
  73. class GlobalValue;
  74. struct KnownBits;
  75. class LegacyDivergenceAnalysis;
  76. class LLVMContext;
  77. class MachineBasicBlock;
  78. class MachineConstantPoolValue;
  79. class MCSymbol;
  80. class OptimizationRemarkEmitter;
  81. class ProfileSummaryInfo;
  82. class SDDbgValue;
  83. class SDDbgOperand;
  84. class SDDbgLabel;
  85. class SelectionDAG;
  86. class SelectionDAGTargetInfo;
  87. class TargetLibraryInfo;
  88. class TargetLowering;
  89. class TargetMachine;
  90. class TargetSubtargetInfo;
  91. class Value;
  92. class SDVTListNode : public FoldingSetNode {
  93. friend struct FoldingSetTrait<SDVTListNode>;
  94. /// A reference to an Interned FoldingSetNodeID for this node.
  95. /// The Allocator in SelectionDAG holds the data.
  96. /// SDVTList contains all types which are frequently accessed in SelectionDAG.
  97. /// The size of this list is not expected to be big so it won't introduce
  98. /// a memory penalty.
  99. FoldingSetNodeIDRef FastID;
  100. const EVT *VTs;
  101. unsigned int NumVTs;
  102. /// The hash value for SDVTList is fixed, so cache it to avoid
  103. /// hash calculation.
  104. unsigned HashValue;
  105. public:
  106. SDVTListNode(const FoldingSetNodeIDRef ID, const EVT *VT, unsigned int Num) :
  107. FastID(ID), VTs(VT), NumVTs(Num) {
  108. HashValue = ID.ComputeHash();
  109. }
  110. SDVTList getSDVTList() {
  111. SDVTList result = {VTs, NumVTs};
  112. return result;
  113. }
  114. };
  115. /// Specialize FoldingSetTrait for SDVTListNode
  116. /// to avoid computing temp FoldingSetNodeID and hash value.
  117. template<> struct FoldingSetTrait<SDVTListNode> : DefaultFoldingSetTrait<SDVTListNode> {
  118. static void Profile(const SDVTListNode &X, FoldingSetNodeID& ID) {
  119. ID = X.FastID;
  120. }
  121. static bool Equals(const SDVTListNode &X, const FoldingSetNodeID &ID,
  122. unsigned IDHash, FoldingSetNodeID &TempID) {
  123. if (X.HashValue != IDHash)
  124. return false;
  125. return ID == X.FastID;
  126. }
  127. static unsigned ComputeHash(const SDVTListNode &X, FoldingSetNodeID &TempID) {
  128. return X.HashValue;
  129. }
  130. };
  131. template <> struct ilist_alloc_traits<SDNode> {
  132. static void deleteNode(SDNode *) {
  133. llvm_unreachable("ilist_traits<SDNode> shouldn't see a deleteNode call!");
  134. }
  135. };
  136. /// Keeps track of dbg_value information through SDISel. We do
  137. /// not build SDNodes for these so as not to perturb the generated code;
  138. /// instead the info is kept off to the side in this structure. Each SDNode may
  139. /// have one or more associated dbg_value entries. This information is kept in
  140. /// DbgValMap.
  141. /// Byval parameters are handled separately because they don't use alloca's,
  142. /// which busts the normal mechanism. There is good reason for handling all
  143. /// parameters separately: they may not have code generated for them, they
  144. /// should always go at the beginning of the function regardless of other code
  145. /// motion, and debug info for them is potentially useful even if the parameter
  146. /// is unused. Right now only byval parameters are handled separately.
  147. class SDDbgInfo {
  148. BumpPtrAllocator Alloc;
  149. SmallVector<SDDbgValue*, 32> DbgValues;
  150. SmallVector<SDDbgValue*, 32> ByvalParmDbgValues;
  151. SmallVector<SDDbgLabel*, 4> DbgLabels;
  152. using DbgValMapType = DenseMap<const SDNode *, SmallVector<SDDbgValue *, 2>>;
  153. DbgValMapType DbgValMap;
  154. public:
  155. SDDbgInfo() = default;
  156. SDDbgInfo(const SDDbgInfo &) = delete;
  157. SDDbgInfo &operator=(const SDDbgInfo &) = delete;
  158. void add(SDDbgValue *V, bool isParameter);
  159. void add(SDDbgLabel *L) { DbgLabels.push_back(L); }
  160. /// Invalidate all DbgValues attached to the node and remove
  161. /// it from the Node-to-DbgValues map.
  162. void erase(const SDNode *Node);
  163. void clear() {
  164. DbgValMap.clear();
  165. DbgValues.clear();
  166. ByvalParmDbgValues.clear();
  167. DbgLabels.clear();
  168. Alloc.Reset();
  169. }
  170. BumpPtrAllocator &getAlloc() { return Alloc; }
  171. bool empty() const {
  172. return DbgValues.empty() && ByvalParmDbgValues.empty() && DbgLabels.empty();
  173. }
  174. ArrayRef<SDDbgValue*> getSDDbgValues(const SDNode *Node) const {
  175. auto I = DbgValMap.find(Node);
  176. if (I != DbgValMap.end())
  177. return I->second;
  178. return ArrayRef<SDDbgValue*>();
  179. }
  180. using DbgIterator = SmallVectorImpl<SDDbgValue*>::iterator;
  181. using DbgLabelIterator = SmallVectorImpl<SDDbgLabel*>::iterator;
  182. DbgIterator DbgBegin() { return DbgValues.begin(); }
  183. DbgIterator DbgEnd() { return DbgValues.end(); }
  184. DbgIterator ByvalParmDbgBegin() { return ByvalParmDbgValues.begin(); }
  185. DbgIterator ByvalParmDbgEnd() { return ByvalParmDbgValues.end(); }
  186. DbgLabelIterator DbgLabelBegin() { return DbgLabels.begin(); }
  187. DbgLabelIterator DbgLabelEnd() { return DbgLabels.end(); }
  188. };
  189. void checkForCycles(const SelectionDAG *DAG, bool force = false);
  190. /// This is used to represent a portion of an LLVM function in a low-level
  191. /// Data Dependence DAG representation suitable for instruction selection.
  192. /// This DAG is constructed as the first step of instruction selection in order
  193. /// to allow implementation of machine specific optimizations
  194. /// and code simplifications.
  195. ///
  196. /// The representation used by the SelectionDAG is a target-independent
  197. /// representation, which has some similarities to the GCC RTL representation,
  198. /// but is significantly more simple, powerful, and is a graph form instead of a
  199. /// linear form.
  200. ///
  201. class SelectionDAG {
  202. const TargetMachine &TM;
  203. const SelectionDAGTargetInfo *TSI = nullptr;
  204. const TargetLowering *TLI = nullptr;
  205. const TargetLibraryInfo *LibInfo = nullptr;
  206. const FunctionVarLocs *FnVarLocs = nullptr;
  207. MachineFunction *MF;
  208. Pass *SDAGISelPass = nullptr;
  209. LLVMContext *Context;
  210. CodeGenOpt::Level OptLevel;
  211. LegacyDivergenceAnalysis * DA = nullptr;
  212. FunctionLoweringInfo * FLI = nullptr;
  213. /// The function-level optimization remark emitter. Used to emit remarks
  214. /// whenever manipulating the DAG.
  215. OptimizationRemarkEmitter *ORE;
  216. ProfileSummaryInfo *PSI = nullptr;
  217. BlockFrequencyInfo *BFI = nullptr;
  218. /// List of non-single value types.
  219. FoldingSet<SDVTListNode> VTListMap;
  220. /// Pool allocation for misc. objects that are created once per SelectionDAG.
  221. BumpPtrAllocator Allocator;
  222. /// The starting token.
  223. SDNode EntryNode;
  224. /// The root of the entire DAG.
  225. SDValue Root;
  226. /// A linked list of nodes in the current DAG.
  227. ilist<SDNode> AllNodes;
  228. /// The AllocatorType for allocating SDNodes. We use
  229. /// pool allocation with recycling.
  230. using NodeAllocatorType = RecyclingAllocator<BumpPtrAllocator, SDNode,
  231. sizeof(LargestSDNode),
  232. alignof(MostAlignedSDNode)>;
  233. /// Pool allocation for nodes.
  234. NodeAllocatorType NodeAllocator;
  235. /// This structure is used to memoize nodes, automatically performing
  236. /// CSE with existing nodes when a duplicate is requested.
  237. FoldingSet<SDNode> CSEMap;
  238. /// Pool allocation for machine-opcode SDNode operands.
  239. BumpPtrAllocator OperandAllocator;
  240. ArrayRecycler<SDUse> OperandRecycler;
  241. /// Tracks dbg_value and dbg_label information through SDISel.
  242. SDDbgInfo *DbgInfo;
  243. using CallSiteInfo = MachineFunction::CallSiteInfo;
  244. using CallSiteInfoImpl = MachineFunction::CallSiteInfoImpl;
  245. struct NodeExtraInfo {
  246. CallSiteInfo CSInfo;
  247. MDNode *HeapAllocSite = nullptr;
  248. MDNode *PCSections = nullptr;
  249. bool NoMerge = false;
  250. };
  251. /// Out-of-line extra information for SDNodes.
  252. DenseMap<const SDNode *, NodeExtraInfo> SDEI;
  253. /// PersistentId counter to be used when inserting the next
  254. /// SDNode to this SelectionDAG. We do not place that under
  255. /// `#if LLVM_ENABLE_ABI_BREAKING_CHECKS` intentionally because
  256. /// it adds unneeded complexity without noticeable
  257. /// benefits (see discussion with @thakis in D120714).
  258. uint16_t NextPersistentId = 0;
  259. public:
  260. /// Clients of various APIs that cause global effects on
  261. /// the DAG can optionally implement this interface. This allows the clients
  262. /// to handle the various sorts of updates that happen.
  263. ///
  264. /// A DAGUpdateListener automatically registers itself with DAG when it is
  265. /// constructed, and removes itself when destroyed in RAII fashion.
  266. struct DAGUpdateListener {
  267. DAGUpdateListener *const Next;
  268. SelectionDAG &DAG;
  269. explicit DAGUpdateListener(SelectionDAG &D)
  270. : Next(D.UpdateListeners), DAG(D) {
  271. DAG.UpdateListeners = this;
  272. }
  273. virtual ~DAGUpdateListener() {
  274. assert(DAG.UpdateListeners == this &&
  275. "DAGUpdateListeners must be destroyed in LIFO order");
  276. DAG.UpdateListeners = Next;
  277. }
  278. /// The node N that was deleted and, if E is not null, an
  279. /// equivalent node E that replaced it.
  280. virtual void NodeDeleted(SDNode *N, SDNode *E);
  281. /// The node N that was updated.
  282. virtual void NodeUpdated(SDNode *N);
  283. /// The node N that was inserted.
  284. virtual void NodeInserted(SDNode *N);
  285. };
  286. struct DAGNodeDeletedListener : public DAGUpdateListener {
  287. std::function<void(SDNode *, SDNode *)> Callback;
  288. DAGNodeDeletedListener(SelectionDAG &DAG,
  289. std::function<void(SDNode *, SDNode *)> Callback)
  290. : DAGUpdateListener(DAG), Callback(std::move(Callback)) {}
  291. void NodeDeleted(SDNode *N, SDNode *E) override { Callback(N, E); }
  292. private:
  293. virtual void anchor();
  294. };
  295. struct DAGNodeInsertedListener : public DAGUpdateListener {
  296. std::function<void(SDNode *)> Callback;
  297. DAGNodeInsertedListener(SelectionDAG &DAG,
  298. std::function<void(SDNode *)> Callback)
  299. : DAGUpdateListener(DAG), Callback(std::move(Callback)) {}
  300. void NodeInserted(SDNode *N) override { Callback(N); }
  301. private:
  302. virtual void anchor();
  303. };
  304. /// Help to insert SDNodeFlags automatically in transforming. Use
  305. /// RAII to save and resume flags in current scope.
  306. class FlagInserter {
  307. SelectionDAG &DAG;
  308. SDNodeFlags Flags;
  309. FlagInserter *LastInserter;
  310. public:
  311. FlagInserter(SelectionDAG &SDAG, SDNodeFlags Flags)
  312. : DAG(SDAG), Flags(Flags),
  313. LastInserter(SDAG.getFlagInserter()) {
  314. SDAG.setFlagInserter(this);
  315. }
  316. FlagInserter(SelectionDAG &SDAG, SDNode *N)
  317. : FlagInserter(SDAG, N->getFlags()) {}
  318. FlagInserter(const FlagInserter &) = delete;
  319. FlagInserter &operator=(const FlagInserter &) = delete;
  320. ~FlagInserter() { DAG.setFlagInserter(LastInserter); }
  321. SDNodeFlags getFlags() const { return Flags; }
  322. };
  323. /// When true, additional steps are taken to
  324. /// ensure that getConstant() and similar functions return DAG nodes that
  325. /// have legal types. This is important after type legalization since
  326. /// any illegally typed nodes generated after this point will not experience
  327. /// type legalization.
  328. bool NewNodesMustHaveLegalTypes = false;
  329. private:
  330. /// DAGUpdateListener is a friend so it can manipulate the listener stack.
  331. friend struct DAGUpdateListener;
  332. /// Linked list of registered DAGUpdateListener instances.
  333. /// This stack is maintained by DAGUpdateListener RAII.
  334. DAGUpdateListener *UpdateListeners = nullptr;
  335. /// Implementation of setSubgraphColor.
  336. /// Return whether we had to truncate the search.
  337. bool setSubgraphColorHelper(SDNode *N, const char *Color,
  338. DenseSet<SDNode *> &visited,
  339. int level, bool &printed);
  340. template <typename SDNodeT, typename... ArgTypes>
  341. SDNodeT *newSDNode(ArgTypes &&... Args) {
  342. return new (NodeAllocator.template Allocate<SDNodeT>())
  343. SDNodeT(std::forward<ArgTypes>(Args)...);
  344. }
  345. /// Build a synthetic SDNodeT with the given args and extract its subclass
  346. /// data as an integer (e.g. for use in a folding set).
  347. ///
  348. /// The args to this function are the same as the args to SDNodeT's
  349. /// constructor, except the second arg (assumed to be a const DebugLoc&) is
  350. /// omitted.
  351. template <typename SDNodeT, typename... ArgTypes>
  352. static uint16_t getSyntheticNodeSubclassData(unsigned IROrder,
  353. ArgTypes &&... Args) {
  354. // The compiler can reduce this expression to a constant iff we pass an
  355. // empty DebugLoc. Thankfully, the debug location doesn't have any bearing
  356. // on the subclass data.
  357. return SDNodeT(IROrder, DebugLoc(), std::forward<ArgTypes>(Args)...)
  358. .getRawSubclassData();
  359. }
  360. template <typename SDNodeTy>
  361. static uint16_t getSyntheticNodeSubclassData(unsigned Opc, unsigned Order,
  362. SDVTList VTs, EVT MemoryVT,
  363. MachineMemOperand *MMO) {
  364. return SDNodeTy(Opc, Order, DebugLoc(), VTs, MemoryVT, MMO)
  365. .getRawSubclassData();
  366. }
  367. void createOperands(SDNode *Node, ArrayRef<SDValue> Vals);
  368. void removeOperands(SDNode *Node) {
  369. if (!Node->OperandList)
  370. return;
  371. OperandRecycler.deallocate(
  372. ArrayRecycler<SDUse>::Capacity::get(Node->NumOperands),
  373. Node->OperandList);
  374. Node->NumOperands = 0;
  375. Node->OperandList = nullptr;
  376. }
  377. void CreateTopologicalOrder(std::vector<SDNode*>& Order);
  378. public:
  379. // Maximum depth for recursive analysis such as computeKnownBits, etc.
  380. static constexpr unsigned MaxRecursionDepth = 6;
  381. explicit SelectionDAG(const TargetMachine &TM, CodeGenOpt::Level);
  382. SelectionDAG(const SelectionDAG &) = delete;
  383. SelectionDAG &operator=(const SelectionDAG &) = delete;
  384. ~SelectionDAG();
  385. /// Prepare this SelectionDAG to process code in the given MachineFunction.
  386. void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE,
  387. Pass *PassPtr, const TargetLibraryInfo *LibraryInfo,
  388. LegacyDivergenceAnalysis *Divergence, ProfileSummaryInfo *PSIin,
  389. BlockFrequencyInfo *BFIin, FunctionVarLocs const *FnVarLocs);
  390. void setFunctionLoweringInfo(FunctionLoweringInfo * FuncInfo) {
  391. FLI = FuncInfo;
  392. }
  393. /// Clear state and free memory necessary to make this
  394. /// SelectionDAG ready to process a new block.
  395. void clear();
  396. MachineFunction &getMachineFunction() const { return *MF; }
  397. const Pass *getPass() const { return SDAGISelPass; }
  398. const DataLayout &getDataLayout() const { return MF->getDataLayout(); }
  399. const TargetMachine &getTarget() const { return TM; }
  400. const TargetSubtargetInfo &getSubtarget() const { return MF->getSubtarget(); }
  401. template <typename STC> const STC &getSubtarget() const {
  402. return MF->getSubtarget<STC>();
  403. }
  404. const TargetLowering &getTargetLoweringInfo() const { return *TLI; }
  405. const TargetLibraryInfo &getLibInfo() const { return *LibInfo; }
  406. const SelectionDAGTargetInfo &getSelectionDAGInfo() const { return *TSI; }
  407. const LegacyDivergenceAnalysis *getDivergenceAnalysis() const { return DA; }
  408. /// Returns the result of the AssignmentTrackingAnalysis pass if it's
  409. /// available, otherwise return nullptr.
  410. const FunctionVarLocs *getFunctionVarLocs() const { return FnVarLocs; }
  411. LLVMContext *getContext() const { return Context; }
  412. OptimizationRemarkEmitter &getORE() const { return *ORE; }
  413. ProfileSummaryInfo *getPSI() const { return PSI; }
  414. BlockFrequencyInfo *getBFI() const { return BFI; }
  415. FlagInserter *getFlagInserter() { return Inserter; }
  416. void setFlagInserter(FlagInserter *FI) { Inserter = FI; }
  417. /// Just dump dot graph to a user-provided path and title.
  418. /// This doesn't open the dot viewer program and
  419. /// helps visualization when outside debugging session.
  420. /// FileName expects absolute path. If provided
  421. /// without any path separators then the file
  422. /// will be created in the current directory.
  423. /// Error will be emitted if the path is insane.
  424. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  425. LLVM_DUMP_METHOD void dumpDotGraph(const Twine &FileName, const Twine &Title);
  426. #endif
  427. /// Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
  428. void viewGraph(const std::string &Title);
  429. void viewGraph();
  430. #if LLVM_ENABLE_ABI_BREAKING_CHECKS
  431. std::map<const SDNode *, std::string> NodeGraphAttrs;
  432. #endif
  433. /// Clear all previously defined node graph attributes.
  434. /// Intended to be used from a debugging tool (eg. gdb).
  435. void clearGraphAttrs();
  436. /// Set graph attributes for a node. (eg. "color=red".)
  437. void setGraphAttrs(const SDNode *N, const char *Attrs);
  438. /// Get graph attributes for a node. (eg. "color=red".)
  439. /// Used from getNodeAttributes.
  440. std::string getGraphAttrs(const SDNode *N) const;
  441. /// Convenience for setting node color attribute.
  442. void setGraphColor(const SDNode *N, const char *Color);
  443. /// Convenience for setting subgraph color attribute.
  444. void setSubgraphColor(SDNode *N, const char *Color);
  445. using allnodes_const_iterator = ilist<SDNode>::const_iterator;
  446. allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
  447. allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
  448. using allnodes_iterator = ilist<SDNode>::iterator;
  449. allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
  450. allnodes_iterator allnodes_end() { return AllNodes.end(); }
  451. ilist<SDNode>::size_type allnodes_size() const {
  452. return AllNodes.size();
  453. }
  454. iterator_range<allnodes_iterator> allnodes() {
  455. return make_range(allnodes_begin(), allnodes_end());
  456. }
  457. iterator_range<allnodes_const_iterator> allnodes() const {
  458. return make_range(allnodes_begin(), allnodes_end());
  459. }
  460. /// Return the root tag of the SelectionDAG.
  461. const SDValue &getRoot() const { return Root; }
  462. /// Return the token chain corresponding to the entry of the function.
  463. SDValue getEntryNode() const {
  464. return SDValue(const_cast<SDNode *>(&EntryNode), 0);
  465. }
  466. /// Set the current root tag of the SelectionDAG.
  467. ///
  468. const SDValue &setRoot(SDValue N) {
  469. assert((!N.getNode() || N.getValueType() == MVT::Other) &&
  470. "DAG root value is not a chain!");
  471. if (N.getNode())
  472. checkForCycles(N.getNode(), this);
  473. Root = N;
  474. if (N.getNode())
  475. checkForCycles(this);
  476. return Root;
  477. }
  478. #ifndef NDEBUG
  479. void VerifyDAGDivergence();
  480. #endif
  481. /// This iterates over the nodes in the SelectionDAG, folding
  482. /// certain types of nodes together, or eliminating superfluous nodes. The
  483. /// Level argument controls whether Combine is allowed to produce nodes and
  484. /// types that are illegal on the target.
  485. void Combine(CombineLevel Level, AAResults *AA,
  486. CodeGenOpt::Level OptLevel);
  487. /// This transforms the SelectionDAG into a SelectionDAG that
  488. /// only uses types natively supported by the target.
  489. /// Returns "true" if it made any changes.
  490. ///
  491. /// Note that this is an involved process that may invalidate pointers into
  492. /// the graph.
  493. bool LegalizeTypes();
  494. /// This transforms the SelectionDAG into a SelectionDAG that is
  495. /// compatible with the target instruction selector, as indicated by the
  496. /// TargetLowering object.
  497. ///
  498. /// Note that this is an involved process that may invalidate pointers into
  499. /// the graph.
  500. void Legalize();
  501. /// Transforms a SelectionDAG node and any operands to it into a node
  502. /// that is compatible with the target instruction selector, as indicated by
  503. /// the TargetLowering object.
  504. ///
  505. /// \returns true if \c N is a valid, legal node after calling this.
  506. ///
  507. /// This essentially runs a single recursive walk of the \c Legalize process
  508. /// over the given node (and its operands). This can be used to incrementally
  509. /// legalize the DAG. All of the nodes which are directly replaced,
  510. /// potentially including N, are added to the output parameter \c
  511. /// UpdatedNodes so that the delta to the DAG can be understood by the
  512. /// caller.
  513. ///
  514. /// When this returns false, N has been legalized in a way that make the
  515. /// pointer passed in no longer valid. It may have even been deleted from the
  516. /// DAG, and so it shouldn't be used further. When this returns true, the
  517. /// N passed in is a legal node, and can be immediately processed as such.
  518. /// This may still have done some work on the DAG, and will still populate
  519. /// UpdatedNodes with any new nodes replacing those originally in the DAG.
  520. bool LegalizeOp(SDNode *N, SmallSetVector<SDNode *, 16> &UpdatedNodes);
  521. /// This transforms the SelectionDAG into a SelectionDAG
  522. /// that only uses vector math operations supported by the target. This is
  523. /// necessary as a separate step from Legalize because unrolling a vector
  524. /// operation can introduce illegal types, which requires running
  525. /// LegalizeTypes again.
  526. ///
  527. /// This returns true if it made any changes; in that case, LegalizeTypes
  528. /// is called again before Legalize.
  529. ///
  530. /// Note that this is an involved process that may invalidate pointers into
  531. /// the graph.
  532. bool LegalizeVectors();
  533. /// This method deletes all unreachable nodes in the SelectionDAG.
  534. void RemoveDeadNodes();
  535. /// Remove the specified node from the system. This node must
  536. /// have no referrers.
  537. void DeleteNode(SDNode *N);
  538. /// Return an SDVTList that represents the list of values specified.
  539. SDVTList getVTList(EVT VT);
  540. SDVTList getVTList(EVT VT1, EVT VT2);
  541. SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3);
  542. SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4);
  543. SDVTList getVTList(ArrayRef<EVT> VTs);
  544. //===--------------------------------------------------------------------===//
  545. // Node creation methods.
  546. /// Create a ConstantSDNode wrapping a constant value.
  547. /// If VT is a vector type, the constant is splatted into a BUILD_VECTOR.
  548. ///
  549. /// If only legal types can be produced, this does the necessary
  550. /// transformations (e.g., if the vector element type is illegal).
  551. /// @{
  552. SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT,
  553. bool isTarget = false, bool isOpaque = false);
  554. SDValue getConstant(const APInt &Val, const SDLoc &DL, EVT VT,
  555. bool isTarget = false, bool isOpaque = false);
  556. SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget = false,
  557. bool IsOpaque = false) {
  558. return getConstant(APInt::getAllOnes(VT.getScalarSizeInBits()), DL, VT,
  559. IsTarget, IsOpaque);
  560. }
  561. SDValue getConstant(const ConstantInt &Val, const SDLoc &DL, EVT VT,
  562. bool isTarget = false, bool isOpaque = false);
  563. SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL,
  564. bool isTarget = false);
  565. SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL,
  566. bool LegalTypes = true);
  567. SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL,
  568. bool isTarget = false);
  569. SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT,
  570. bool isOpaque = false) {
  571. return getConstant(Val, DL, VT, true, isOpaque);
  572. }
  573. SDValue getTargetConstant(const APInt &Val, const SDLoc &DL, EVT VT,
  574. bool isOpaque = false) {
  575. return getConstant(Val, DL, VT, true, isOpaque);
  576. }
  577. SDValue getTargetConstant(const ConstantInt &Val, const SDLoc &DL, EVT VT,
  578. bool isOpaque = false) {
  579. return getConstant(Val, DL, VT, true, isOpaque);
  580. }
  581. /// Create a true or false constant of type \p VT using the target's
  582. /// BooleanContent for type \p OpVT.
  583. SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT);
  584. /// @}
  585. /// Create a ConstantFPSDNode wrapping a constant value.
  586. /// If VT is a vector type, the constant is splatted into a BUILD_VECTOR.
  587. ///
  588. /// If only legal types can be produced, this does the necessary
  589. /// transformations (e.g., if the vector element type is illegal).
  590. /// The forms that take a double should only be used for simple constants
  591. /// that can be exactly represented in VT. No checks are made.
  592. /// @{
  593. SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT,
  594. bool isTarget = false);
  595. SDValue getConstantFP(const APFloat &Val, const SDLoc &DL, EVT VT,
  596. bool isTarget = false);
  597. SDValue getConstantFP(const ConstantFP &V, const SDLoc &DL, EVT VT,
  598. bool isTarget = false);
  599. SDValue getTargetConstantFP(double Val, const SDLoc &DL, EVT VT) {
  600. return getConstantFP(Val, DL, VT, true);
  601. }
  602. SDValue getTargetConstantFP(const APFloat &Val, const SDLoc &DL, EVT VT) {
  603. return getConstantFP(Val, DL, VT, true);
  604. }
  605. SDValue getTargetConstantFP(const ConstantFP &Val, const SDLoc &DL, EVT VT) {
  606. return getConstantFP(Val, DL, VT, true);
  607. }
  608. /// @}
  609. SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT,
  610. int64_t offset = 0, bool isTargetGA = false,
  611. unsigned TargetFlags = 0);
  612. SDValue getTargetGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT,
  613. int64_t offset = 0, unsigned TargetFlags = 0) {
  614. return getGlobalAddress(GV, DL, VT, offset, true, TargetFlags);
  615. }
  616. SDValue getFrameIndex(int FI, EVT VT, bool isTarget = false);
  617. SDValue getTargetFrameIndex(int FI, EVT VT) {
  618. return getFrameIndex(FI, VT, true);
  619. }
  620. SDValue getJumpTable(int JTI, EVT VT, bool isTarget = false,
  621. unsigned TargetFlags = 0);
  622. SDValue getTargetJumpTable(int JTI, EVT VT, unsigned TargetFlags = 0) {
  623. return getJumpTable(JTI, VT, true, TargetFlags);
  624. }
  625. SDValue getConstantPool(const Constant *C, EVT VT,
  626. MaybeAlign Align = std::nullopt, int Offs = 0,
  627. bool isT = false, unsigned TargetFlags = 0);
  628. SDValue getTargetConstantPool(const Constant *C, EVT VT,
  629. MaybeAlign Align = std::nullopt, int Offset = 0,
  630. unsigned TargetFlags = 0) {
  631. return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
  632. }
  633. SDValue getConstantPool(MachineConstantPoolValue *C, EVT VT,
  634. MaybeAlign Align = std::nullopt, int Offs = 0,
  635. bool isT = false, unsigned TargetFlags = 0);
  636. SDValue getTargetConstantPool(MachineConstantPoolValue *C, EVT VT,
  637. MaybeAlign Align = std::nullopt, int Offset = 0,
  638. unsigned TargetFlags = 0) {
  639. return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
  640. }
  641. SDValue getTargetIndex(int Index, EVT VT, int64_t Offset = 0,
  642. unsigned TargetFlags = 0);
  643. // When generating a branch to a BB, we don't in general know enough
  644. // to provide debug info for the BB at that time, so keep this one around.
  645. SDValue getBasicBlock(MachineBasicBlock *MBB);
  646. SDValue getExternalSymbol(const char *Sym, EVT VT);
  647. SDValue getTargetExternalSymbol(const char *Sym, EVT VT,
  648. unsigned TargetFlags = 0);
  649. SDValue getMCSymbol(MCSymbol *Sym, EVT VT);
  650. SDValue getValueType(EVT);
  651. SDValue getRegister(unsigned Reg, EVT VT);
  652. SDValue getRegisterMask(const uint32_t *RegMask);
  653. SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label);
  654. SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root,
  655. MCSymbol *Label);
  656. SDValue getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset = 0,
  657. bool isTarget = false, unsigned TargetFlags = 0);
  658. SDValue getTargetBlockAddress(const BlockAddress *BA, EVT VT,
  659. int64_t Offset = 0, unsigned TargetFlags = 0) {
  660. return getBlockAddress(BA, VT, Offset, true, TargetFlags);
  661. }
  662. SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, unsigned Reg,
  663. SDValue N) {
  664. return getNode(ISD::CopyToReg, dl, MVT::Other, Chain,
  665. getRegister(Reg, N.getValueType()), N);
  666. }
  667. // This version of the getCopyToReg method takes an extra operand, which
  668. // indicates that there is potentially an incoming glue value (if Glue is not
  669. // null) and that there should be a glue result.
  670. SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, unsigned Reg, SDValue N,
  671. SDValue Glue) {
  672. SDVTList VTs = getVTList(MVT::Other, MVT::Glue);
  673. SDValue Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Glue };
  674. return getNode(ISD::CopyToReg, dl, VTs,
  675. ArrayRef(Ops, Glue.getNode() ? 4 : 3));
  676. }
  677. // Similar to last getCopyToReg() except parameter Reg is a SDValue
  678. SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, SDValue Reg, SDValue N,
  679. SDValue Glue) {
  680. SDVTList VTs = getVTList(MVT::Other, MVT::Glue);
  681. SDValue Ops[] = { Chain, Reg, N, Glue };
  682. return getNode(ISD::CopyToReg, dl, VTs,
  683. ArrayRef(Ops, Glue.getNode() ? 4 : 3));
  684. }
  685. SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, unsigned Reg, EVT VT) {
  686. SDVTList VTs = getVTList(VT, MVT::Other);
  687. SDValue Ops[] = { Chain, getRegister(Reg, VT) };
  688. return getNode(ISD::CopyFromReg, dl, VTs, Ops);
  689. }
  690. // This version of the getCopyFromReg method takes an extra operand, which
  691. // indicates that there is potentially an incoming glue value (if Glue is not
  692. // null) and that there should be a glue result.
  693. SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, unsigned Reg, EVT VT,
  694. SDValue Glue) {
  695. SDVTList VTs = getVTList(VT, MVT::Other, MVT::Glue);
  696. SDValue Ops[] = { Chain, getRegister(Reg, VT), Glue };
  697. return getNode(ISD::CopyFromReg, dl, VTs,
  698. ArrayRef(Ops, Glue.getNode() ? 3 : 2));
  699. }
  700. SDValue getCondCode(ISD::CondCode Cond);
  701. /// Return an ISD::VECTOR_SHUFFLE node. The number of elements in VT,
  702. /// which must be a vector type, must match the number of mask elements
  703. /// NumElts. An integer mask element equal to -1 is treated as undefined.
  704. SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2,
  705. ArrayRef<int> Mask);
  706. /// Return an ISD::BUILD_VECTOR node. The number of elements in VT,
  707. /// which must be a vector type, must match the number of operands in Ops.
  708. /// The operands must have the same type as (or, for integers, a type wider
  709. /// than) VT's element type.
  710. SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef<SDValue> Ops) {
  711. // VerifySDNode (via InsertNode) checks BUILD_VECTOR later.
  712. return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
  713. }
  714. /// Return an ISD::BUILD_VECTOR node. The number of elements in VT,
  715. /// which must be a vector type, must match the number of operands in Ops.
  716. /// The operands must have the same type as (or, for integers, a type wider
  717. /// than) VT's element type.
  718. SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef<SDUse> Ops) {
  719. // VerifySDNode (via InsertNode) checks BUILD_VECTOR later.
  720. return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
  721. }
  722. /// Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all
  723. /// elements. VT must be a vector type. Op's type must be the same as (or,
  724. /// for integers, a type wider than) VT's element type.
  725. SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op) {
  726. // VerifySDNode (via InsertNode) checks BUILD_VECTOR later.
  727. if (Op.getOpcode() == ISD::UNDEF) {
  728. assert((VT.getVectorElementType() == Op.getValueType() ||
  729. (VT.isInteger() &&
  730. VT.getVectorElementType().bitsLE(Op.getValueType()))) &&
  731. "A splatted value must have a width equal or (for integers) "
  732. "greater than the vector element type!");
  733. return getNode(ISD::UNDEF, SDLoc(), VT);
  734. }
  735. SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Op);
  736. return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
  737. }
  738. // Return a splat ISD::SPLAT_VECTOR node, consisting of Op splatted to all
  739. // elements.
  740. SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op) {
  741. if (Op.getOpcode() == ISD::UNDEF) {
  742. assert((VT.getVectorElementType() == Op.getValueType() ||
  743. (VT.isInteger() &&
  744. VT.getVectorElementType().bitsLE(Op.getValueType()))) &&
  745. "A splatted value must have a width equal or (for integers) "
  746. "greater than the vector element type!");
  747. return getNode(ISD::UNDEF, SDLoc(), VT);
  748. }
  749. return getNode(ISD::SPLAT_VECTOR, DL, VT, Op);
  750. }
  751. /// Returns a node representing a splat of one value into all lanes
  752. /// of the provided vector type. This is a utility which returns
  753. /// either a BUILD_VECTOR or SPLAT_VECTOR depending on the
  754. /// scalability of the desired vector type.
  755. SDValue getSplat(EVT VT, const SDLoc &DL, SDValue Op) {
  756. assert(VT.isVector() && "Can't splat to non-vector type");
  757. return VT.isScalableVector() ?
  758. getSplatVector(VT, DL, Op) : getSplatBuildVector(VT, DL, Op);
  759. }
  760. /// Returns a vector of type ResVT whose elements contain the linear sequence
  761. /// <0, Step, Step * 2, Step * 3, ...>
  762. SDValue getStepVector(const SDLoc &DL, EVT ResVT, APInt StepVal);
  763. /// Returns a vector of type ResVT whose elements contain the linear sequence
  764. /// <0, 1, 2, 3, ...>
  765. SDValue getStepVector(const SDLoc &DL, EVT ResVT);
  766. /// Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to
  767. /// the shuffle node in input but with swapped operands.
  768. ///
  769. /// Example: shuffle A, B, <0,5,2,7> -> shuffle B, A, <4,1,6,3>
  770. SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV);
  771. /// Convert Op, which must be of float type, to the
  772. /// float type VT, by either extending or rounding (by truncation).
  773. SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT);
  774. /// Convert Op, which must be a STRICT operation of float type, to the
  775. /// float type VT, by either extending or rounding (by truncation).
  776. std::pair<SDValue, SDValue>
  777. getStrictFPExtendOrRound(SDValue Op, SDValue Chain, const SDLoc &DL, EVT VT);
  778. /// Convert *_EXTEND_VECTOR_INREG to *_EXTEND opcode.
  779. static unsigned getOpcode_EXTEND(unsigned Opcode) {
  780. switch (Opcode) {
  781. case ISD::ANY_EXTEND:
  782. case ISD::ANY_EXTEND_VECTOR_INREG:
  783. return ISD::ANY_EXTEND;
  784. case ISD::ZERO_EXTEND:
  785. case ISD::ZERO_EXTEND_VECTOR_INREG:
  786. return ISD::ZERO_EXTEND;
  787. case ISD::SIGN_EXTEND:
  788. case ISD::SIGN_EXTEND_VECTOR_INREG:
  789. return ISD::SIGN_EXTEND;
  790. }
  791. llvm_unreachable("Unknown opcode");
  792. }
  793. /// Convert *_EXTEND to *_EXTEND_VECTOR_INREG opcode.
  794. static unsigned getOpcode_EXTEND_VECTOR_INREG(unsigned Opcode) {
  795. switch (Opcode) {
  796. case ISD::ANY_EXTEND:
  797. case ISD::ANY_EXTEND_VECTOR_INREG:
  798. return ISD::ANY_EXTEND_VECTOR_INREG;
  799. case ISD::ZERO_EXTEND:
  800. case ISD::ZERO_EXTEND_VECTOR_INREG:
  801. return ISD::ZERO_EXTEND_VECTOR_INREG;
  802. case ISD::SIGN_EXTEND:
  803. case ISD::SIGN_EXTEND_VECTOR_INREG:
  804. return ISD::SIGN_EXTEND_VECTOR_INREG;
  805. }
  806. llvm_unreachable("Unknown opcode");
  807. }
  808. /// Convert Op, which must be of integer type, to the
  809. /// integer type VT, by either any-extending or truncating it.
  810. SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT);
  811. /// Convert Op, which must be of integer type, to the
  812. /// integer type VT, by either sign-extending or truncating it.
  813. SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT);
  814. /// Convert Op, which must be of integer type, to the
  815. /// integer type VT, by either zero-extending or truncating it.
  816. SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT);
  817. /// Return the expression required to zero extend the Op
  818. /// value assuming it was the smaller SrcTy value.
  819. SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT);
  820. /// Convert Op, which must be of integer type, to the integer type VT, by
  821. /// either truncating it or performing either zero or sign extension as
  822. /// appropriate extension for the pointer's semantics.
  823. SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT);
  824. /// Return the expression required to extend the Op as a pointer value
  825. /// assuming it was the smaller SrcTy value. This may be either a zero extend
  826. /// or a sign extend.
  827. SDValue getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT);
  828. /// Convert Op, which must be of integer type, to the integer type VT,
  829. /// by using an extension appropriate for the target's
  830. /// BooleanContent for type OpVT or truncating it.
  831. SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT);
  832. /// Create negative operation as (SUB 0, Val).
  833. SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT);
  834. /// Create a bitwise NOT operation as (XOR Val, -1).
  835. SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT);
  836. /// Create a logical NOT operation as (XOR Val, BooleanOne).
  837. SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT);
  838. /// Create a vector-predicated logical NOT operation as (VP_XOR Val,
  839. /// BooleanOne, Mask, EVL).
  840. SDValue getVPLogicalNOT(const SDLoc &DL, SDValue Val, SDValue Mask,
  841. SDValue EVL, EVT VT);
  842. /// Convert a vector-predicated Op, which must be an integer vector, to the
  843. /// vector-type VT, by performing either vector-predicated zext or truncating
  844. /// it. The Op will be returned as-is if Op and VT are vectors containing
  845. /// integer with same width.
  846. SDValue getVPZExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask,
  847. SDValue EVL);
  848. /// Convert a vector-predicated Op, which must be of integer type, to the
  849. /// vector-type integer type VT, by either truncating it or performing either
  850. /// vector-predicated zero or sign extension as appropriate extension for the
  851. /// pointer's semantics. This function just redirects to getVPZExtOrTrunc
  852. /// right now.
  853. SDValue getVPPtrExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask,
  854. SDValue EVL);
  855. /// Returns sum of the base pointer and offset.
  856. /// Unlike getObjectPtrOffset this does not set NoUnsignedWrap by default.
  857. SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL,
  858. const SDNodeFlags Flags = SDNodeFlags());
  859. SDValue getMemBasePlusOffset(SDValue Base, SDValue Offset, const SDLoc &DL,
  860. const SDNodeFlags Flags = SDNodeFlags());
  861. /// Create an add instruction with appropriate flags when used for
  862. /// addressing some offset of an object. i.e. if a load is split into multiple
  863. /// components, create an add nuw from the base pointer to the offset.
  864. SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, TypeSize Offset) {
  865. SDNodeFlags Flags;
  866. Flags.setNoUnsignedWrap(true);
  867. return getMemBasePlusOffset(Ptr, Offset, SL, Flags);
  868. }
  869. SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, SDValue Offset) {
  870. // The object itself can't wrap around the address space, so it shouldn't be
  871. // possible for the adds of the offsets to the split parts to overflow.
  872. SDNodeFlags Flags;
  873. Flags.setNoUnsignedWrap(true);
  874. return getMemBasePlusOffset(Ptr, Offset, SL, Flags);
  875. }
  876. /// Return a new CALLSEQ_START node, that starts new call frame, in which
  877. /// InSize bytes are set up inside CALLSEQ_START..CALLSEQ_END sequence and
  878. /// OutSize specifies part of the frame set up prior to the sequence.
  879. SDValue getCALLSEQ_START(SDValue Chain, uint64_t InSize, uint64_t OutSize,
  880. const SDLoc &DL) {
  881. SDVTList VTs = getVTList(MVT::Other, MVT::Glue);
  882. SDValue Ops[] = { Chain,
  883. getIntPtrConstant(InSize, DL, true),
  884. getIntPtrConstant(OutSize, DL, true) };
  885. return getNode(ISD::CALLSEQ_START, DL, VTs, Ops);
  886. }
  887. /// Return a new CALLSEQ_END node, which always must have a
  888. /// glue result (to ensure it's not CSE'd).
  889. /// CALLSEQ_END does not have a useful SDLoc.
  890. SDValue getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2,
  891. SDValue InGlue, const SDLoc &DL) {
  892. SDVTList NodeTys = getVTList(MVT::Other, MVT::Glue);
  893. SmallVector<SDValue, 4> Ops;
  894. Ops.push_back(Chain);
  895. Ops.push_back(Op1);
  896. Ops.push_back(Op2);
  897. if (InGlue.getNode())
  898. Ops.push_back(InGlue);
  899. return getNode(ISD::CALLSEQ_END, DL, NodeTys, Ops);
  900. }
  901. SDValue getCALLSEQ_END(SDValue Chain, uint64_t Size1, uint64_t Size2,
  902. SDValue Glue, const SDLoc &DL) {
  903. return getCALLSEQ_END(
  904. Chain, getIntPtrConstant(Size1, DL, /*isTarget=*/true),
  905. getIntPtrConstant(Size2, DL, /*isTarget=*/true), Glue, DL);
  906. }
  907. /// Return true if the result of this operation is always undefined.
  908. bool isUndef(unsigned Opcode, ArrayRef<SDValue> Ops);
  909. /// Return an UNDEF node. UNDEF does not have a useful SDLoc.
  910. SDValue getUNDEF(EVT VT) {
  911. return getNode(ISD::UNDEF, SDLoc(), VT);
  912. }
  913. /// Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
  914. SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm) {
  915. assert(MulImm.getMinSignedBits() <= VT.getSizeInBits() &&
  916. "Immediate does not fit VT");
  917. return getNode(ISD::VSCALE, DL, VT,
  918. getConstant(MulImm.sextOrTrunc(VT.getSizeInBits()), DL, VT));
  919. }
  920. /// Return a GLOBAL_OFFSET_TABLE node. This does not have a useful SDLoc.
  921. SDValue getGLOBAL_OFFSET_TABLE(EVT VT) {
  922. return getNode(ISD::GLOBAL_OFFSET_TABLE, SDLoc(), VT);
  923. }
  924. /// Gets or creates the specified node.
  925. ///
  926. SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
  927. ArrayRef<SDUse> Ops);
  928. SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
  929. ArrayRef<SDValue> Ops, const SDNodeFlags Flags);
  930. SDValue getNode(unsigned Opcode, const SDLoc &DL, ArrayRef<EVT> ResultTys,
  931. ArrayRef<SDValue> Ops);
  932. SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
  933. ArrayRef<SDValue> Ops, const SDNodeFlags Flags);
  934. // Use flags from current flag inserter.
  935. SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
  936. ArrayRef<SDValue> Ops);
  937. SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
  938. ArrayRef<SDValue> Ops);
  939. SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue Operand);
  940. SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1,
  941. SDValue N2);
  942. SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1,
  943. SDValue N2, SDValue N3);
  944. // Specialize based on number of operands.
  945. SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT);
  946. SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue Operand,
  947. const SDNodeFlags Flags);
  948. SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1,
  949. SDValue N2, const SDNodeFlags Flags);
  950. SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1,
  951. SDValue N2, SDValue N3, const SDNodeFlags Flags);
  952. SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1,
  953. SDValue N2, SDValue N3, SDValue N4);
  954. SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, SDValue N1,
  955. SDValue N2, SDValue N3, SDValue N4, SDValue N5);
  956. // Specialize again based on number of operands for nodes with a VTList
  957. // rather than a single VT.
  958. SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList);
  959. SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, SDValue N);
  960. SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, SDValue N1,
  961. SDValue N2);
  962. SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, SDValue N1,
  963. SDValue N2, SDValue N3);
  964. SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, SDValue N1,
  965. SDValue N2, SDValue N3, SDValue N4);
  966. SDValue getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, SDValue N1,
  967. SDValue N2, SDValue N3, SDValue N4, SDValue N5);
  968. /// Compute a TokenFactor to force all the incoming stack arguments to be
  969. /// loaded from the stack. This is used in tail call lowering to protect
  970. /// stack arguments from being clobbered.
  971. SDValue getStackArgumentTokenFactor(SDValue Chain);
  972. SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src,
  973. SDValue Size, Align Alignment, bool isVol,
  974. bool AlwaysInline, bool isTailCall,
  975. MachinePointerInfo DstPtrInfo,
  976. MachinePointerInfo SrcPtrInfo,
  977. const AAMDNodes &AAInfo = AAMDNodes(),
  978. AAResults *AA = nullptr);
  979. SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src,
  980. SDValue Size, Align Alignment, bool isVol, bool isTailCall,
  981. MachinePointerInfo DstPtrInfo,
  982. MachinePointerInfo SrcPtrInfo,
  983. const AAMDNodes &AAInfo = AAMDNodes(),
  984. AAResults *AA = nullptr);
  985. SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src,
  986. SDValue Size, Align Alignment, bool isVol,
  987. bool AlwaysInline, bool isTailCall,
  988. MachinePointerInfo DstPtrInfo,
  989. const AAMDNodes &AAInfo = AAMDNodes());
  990. SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst,
  991. SDValue Src, SDValue Size, Type *SizeTy,
  992. unsigned ElemSz, bool isTailCall,
  993. MachinePointerInfo DstPtrInfo,
  994. MachinePointerInfo SrcPtrInfo);
  995. SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst,
  996. SDValue Src, SDValue Size, Type *SizeTy,
  997. unsigned ElemSz, bool isTailCall,
  998. MachinePointerInfo DstPtrInfo,
  999. MachinePointerInfo SrcPtrInfo);
  1000. SDValue getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst,
  1001. SDValue Value, SDValue Size, Type *SizeTy,
  1002. unsigned ElemSz, bool isTailCall,
  1003. MachinePointerInfo DstPtrInfo);
  1004. /// Helper function to make it easier to build SetCC's if you just have an
  1005. /// ISD::CondCode instead of an SDValue.
  1006. SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS,
  1007. ISD::CondCode Cond, SDValue Chain = SDValue(),
  1008. bool IsSignaling = false) {
  1009. assert(LHS.getValueType().isVector() == RHS.getValueType().isVector() &&
  1010. "Vector/scalar operand type mismatch for setcc");
  1011. assert(LHS.getValueType().isVector() == VT.isVector() &&
  1012. "Vector/scalar result type mismatch for setcc");
  1013. assert(Cond != ISD::SETCC_INVALID &&
  1014. "Cannot create a setCC of an invalid node.");
  1015. if (Chain)
  1016. return getNode(IsSignaling ? ISD::STRICT_FSETCCS : ISD::STRICT_FSETCC, DL,
  1017. {VT, MVT::Other}, {Chain, LHS, RHS, getCondCode(Cond)});
  1018. return getNode(ISD::SETCC, DL, VT, LHS, RHS, getCondCode(Cond));
  1019. }
  1020. /// Helper function to make it easier to build VP_SETCCs if you just have an
  1021. /// ISD::CondCode instead of an SDValue.
  1022. SDValue getSetCCVP(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS,
  1023. ISD::CondCode Cond, SDValue Mask, SDValue EVL) {
  1024. assert(LHS.getValueType().isVector() && RHS.getValueType().isVector() &&
  1025. "Cannot compare scalars");
  1026. assert(Cond != ISD::SETCC_INVALID &&
  1027. "Cannot create a setCC of an invalid node.");
  1028. return getNode(ISD::VP_SETCC, DL, VT, LHS, RHS, getCondCode(Cond), Mask,
  1029. EVL);
  1030. }
  1031. /// Helper function to make it easier to build Select's if you just have
  1032. /// operands and don't want to check for vector.
  1033. SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS,
  1034. SDValue RHS) {
  1035. assert(LHS.getValueType() == VT && RHS.getValueType() == VT &&
  1036. "Cannot use select on differing types");
  1037. auto Opcode = Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT;
  1038. return getNode(Opcode, DL, VT, Cond, LHS, RHS);
  1039. }
  1040. /// Helper function to make it easier to build SelectCC's if you just have an
  1041. /// ISD::CondCode instead of an SDValue.
  1042. SDValue getSelectCC(const SDLoc &DL, SDValue LHS, SDValue RHS, SDValue True,
  1043. SDValue False, ISD::CondCode Cond) {
  1044. return getNode(ISD::SELECT_CC, DL, True.getValueType(), LHS, RHS, True,
  1045. False, getCondCode(Cond));
  1046. }
  1047. /// Try to simplify a select/vselect into 1 of its operands or a constant.
  1048. SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal);
  1049. /// Try to simplify a shift into 1 of its operands or a constant.
  1050. SDValue simplifyShift(SDValue X, SDValue Y);
  1051. /// Try to simplify a floating-point binary operation into 1 of its operands
  1052. /// or a constant.
  1053. SDValue simplifyFPBinop(unsigned Opcode, SDValue X, SDValue Y,
  1054. SDNodeFlags Flags);
  1055. /// VAArg produces a result and token chain, and takes a pointer
  1056. /// and a source value as input.
  1057. SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr,
  1058. SDValue SV, unsigned Align);
  1059. /// Gets a node for an atomic cmpxchg op. There are two
  1060. /// valid Opcodes. ISD::ATOMIC_CMO_SWAP produces the value loaded and a
  1061. /// chain result. ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS produces the value loaded,
  1062. /// a success flag (initially i1), and a chain.
  1063. SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT,
  1064. SDVTList VTs, SDValue Chain, SDValue Ptr,
  1065. SDValue Cmp, SDValue Swp, MachineMemOperand *MMO);
  1066. /// Gets a node for an atomic op, produces result (if relevant)
  1067. /// and chain and takes 2 operands.
  1068. SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain,
  1069. SDValue Ptr, SDValue Val, MachineMemOperand *MMO);
  1070. /// Gets a node for an atomic op, produces result and chain and
  1071. /// takes 1 operand.
  1072. SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, EVT VT,
  1073. SDValue Chain, SDValue Ptr, MachineMemOperand *MMO);
  1074. /// Gets a node for an atomic op, produces result and chain and takes N
  1075. /// operands.
  1076. SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
  1077. SDVTList VTList, ArrayRef<SDValue> Ops,
  1078. MachineMemOperand *MMO);
  1079. /// Creates a MemIntrinsicNode that may produce a
  1080. /// result and takes a list of operands. Opcode may be INTRINSIC_VOID,
  1081. /// INTRINSIC_W_CHAIN, or a target-specific opcode with a value not
  1082. /// less than FIRST_TARGET_MEMORY_OPCODE.
  1083. SDValue getMemIntrinsicNode(
  1084. unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
  1085. EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
  1086. MachineMemOperand::Flags Flags = MachineMemOperand::MOLoad |
  1087. MachineMemOperand::MOStore,
  1088. uint64_t Size = 0, const AAMDNodes &AAInfo = AAMDNodes());
  1089. inline SDValue getMemIntrinsicNode(
  1090. unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
  1091. EVT MemVT, MachinePointerInfo PtrInfo,
  1092. MaybeAlign Alignment = std::nullopt,
  1093. MachineMemOperand::Flags Flags = MachineMemOperand::MOLoad |
  1094. MachineMemOperand::MOStore,
  1095. uint64_t Size = 0, const AAMDNodes &AAInfo = AAMDNodes()) {
  1096. // Ensure that codegen never sees alignment 0
  1097. return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, PtrInfo,
  1098. Alignment.value_or(getEVTAlign(MemVT)), Flags,
  1099. Size, AAInfo);
  1100. }
  1101. SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList,
  1102. ArrayRef<SDValue> Ops, EVT MemVT,
  1103. MachineMemOperand *MMO);
  1104. /// Creates a LifetimeSDNode that starts (`IsStart==true`) or ends
  1105. /// (`IsStart==false`) the lifetime of the portion of `FrameIndex` between
  1106. /// offsets `Offset` and `Offset + Size`.
  1107. SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain,
  1108. int FrameIndex, int64_t Size, int64_t Offset = -1);
  1109. /// Creates a PseudoProbeSDNode with function GUID `Guid` and
  1110. /// the index of the block `Index` it is probing, as well as the attributes
  1111. /// `attr` of the probe.
  1112. SDValue getPseudoProbeNode(const SDLoc &Dl, SDValue Chain, uint64_t Guid,
  1113. uint64_t Index, uint32_t Attr);
  1114. /// Create a MERGE_VALUES node from the given operands.
  1115. SDValue getMergeValues(ArrayRef<SDValue> Ops, const SDLoc &dl);
  1116. /// Loads are not normal binary operators: their result type is not
  1117. /// determined by their operands, and they produce a value AND a token chain.
  1118. ///
  1119. /// This function will set the MOLoad flag on MMOFlags, but you can set it if
  1120. /// you want. The MOStore flag must not be set.
  1121. SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr,
  1122. MachinePointerInfo PtrInfo,
  1123. MaybeAlign Alignment = MaybeAlign(),
  1124. MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
  1125. const AAMDNodes &AAInfo = AAMDNodes(),
  1126. const MDNode *Ranges = nullptr);
  1127. /// FIXME: Remove once transition to Align is over.
  1128. LLVM_DEPRECATED("Use the getLoad function that takes a MaybeAlign instead",
  1129. "")
  1130. inline SDValue
  1131. getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr,
  1132. MachinePointerInfo PtrInfo, unsigned Alignment,
  1133. MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
  1134. const AAMDNodes &AAInfo = AAMDNodes(),
  1135. const MDNode *Ranges = nullptr) {
  1136. return getLoad(VT, dl, Chain, Ptr, PtrInfo, MaybeAlign(Alignment), MMOFlags,
  1137. AAInfo, Ranges);
  1138. }
  1139. SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr,
  1140. MachineMemOperand *MMO);
  1141. SDValue
  1142. getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain,
  1143. SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT,
  1144. MaybeAlign Alignment = MaybeAlign(),
  1145. MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
  1146. const AAMDNodes &AAInfo = AAMDNodes());
  1147. SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT,
  1148. SDValue Chain, SDValue Ptr, EVT MemVT,
  1149. MachineMemOperand *MMO);
  1150. SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base,
  1151. SDValue Offset, ISD::MemIndexedMode AM);
  1152. SDValue getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT,
  1153. const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset,
  1154. MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
  1155. MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
  1156. const AAMDNodes &AAInfo = AAMDNodes(),
  1157. const MDNode *Ranges = nullptr);
  1158. inline SDValue getLoad(
  1159. ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
  1160. SDValue Chain, SDValue Ptr, SDValue Offset, MachinePointerInfo PtrInfo,
  1161. EVT MemVT, MaybeAlign Alignment = MaybeAlign(),
  1162. MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
  1163. const AAMDNodes &AAInfo = AAMDNodes(), const MDNode *Ranges = nullptr) {
  1164. // Ensures that codegen never sees a None Alignment.
  1165. return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, PtrInfo, MemVT,
  1166. Alignment.value_or(getEVTAlign(MemVT)), MMOFlags, AAInfo,
  1167. Ranges);
  1168. }
  1169. /// FIXME: Remove once transition to Align is over.
  1170. LLVM_DEPRECATED("Use the getLoad function that takes a MaybeAlign instead",
  1171. "")
  1172. inline SDValue
  1173. getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT,
  1174. const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset,
  1175. MachinePointerInfo PtrInfo, EVT MemVT, unsigned Alignment,
  1176. MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
  1177. const AAMDNodes &AAInfo = AAMDNodes(),
  1178. const MDNode *Ranges = nullptr) {
  1179. return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, PtrInfo, MemVT,
  1180. MaybeAlign(Alignment), MMOFlags, AAInfo, Ranges);
  1181. }
  1182. SDValue getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT,
  1183. const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset,
  1184. EVT MemVT, MachineMemOperand *MMO);
  1185. /// Helper function to build ISD::STORE nodes.
  1186. ///
  1187. /// This function will set the MOStore flag on MMOFlags, but you can set it if
  1188. /// you want. The MOLoad and MOInvariant flags must not be set.
  1189. SDValue
  1190. getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
  1191. MachinePointerInfo PtrInfo, Align Alignment,
  1192. MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
  1193. const AAMDNodes &AAInfo = AAMDNodes());
  1194. inline SDValue
  1195. getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
  1196. MachinePointerInfo PtrInfo, MaybeAlign Alignment = MaybeAlign(),
  1197. MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
  1198. const AAMDNodes &AAInfo = AAMDNodes()) {
  1199. return getStore(Chain, dl, Val, Ptr, PtrInfo,
  1200. Alignment.value_or(getEVTAlign(Val.getValueType())),
  1201. MMOFlags, AAInfo);
  1202. }
  1203. /// FIXME: Remove once transition to Align is over.
  1204. LLVM_DEPRECATED("Use the version that takes a MaybeAlign instead", "")
  1205. inline SDValue
  1206. getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
  1207. MachinePointerInfo PtrInfo, unsigned Alignment,
  1208. MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
  1209. const AAMDNodes &AAInfo = AAMDNodes()) {
  1210. return getStore(Chain, dl, Val, Ptr, PtrInfo, MaybeAlign(Alignment),
  1211. MMOFlags, AAInfo);
  1212. }
  1213. SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
  1214. MachineMemOperand *MMO);
  1215. SDValue
  1216. getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
  1217. MachinePointerInfo PtrInfo, EVT SVT, Align Alignment,
  1218. MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
  1219. const AAMDNodes &AAInfo = AAMDNodes());
  1220. inline SDValue
  1221. getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
  1222. MachinePointerInfo PtrInfo, EVT SVT,
  1223. MaybeAlign Alignment = MaybeAlign(),
  1224. MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
  1225. const AAMDNodes &AAInfo = AAMDNodes()) {
  1226. return getTruncStore(Chain, dl, Val, Ptr, PtrInfo, SVT,
  1227. Alignment.value_or(getEVTAlign(SVT)), MMOFlags,
  1228. AAInfo);
  1229. }
  1230. /// FIXME: Remove once transition to Align is over.
  1231. LLVM_DEPRECATED("Use the version that takes a MaybeAlign instead", "")
  1232. inline SDValue
  1233. getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
  1234. MachinePointerInfo PtrInfo, EVT SVT, unsigned Alignment,
  1235. MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
  1236. const AAMDNodes &AAInfo = AAMDNodes()) {
  1237. return getTruncStore(Chain, dl, Val, Ptr, PtrInfo, SVT,
  1238. MaybeAlign(Alignment), MMOFlags, AAInfo);
  1239. }
  1240. SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val,
  1241. SDValue Ptr, EVT SVT, MachineMemOperand *MMO);
  1242. SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base,
  1243. SDValue Offset, ISD::MemIndexedMode AM);
  1244. SDValue getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT,
  1245. const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset,
  1246. SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo,
  1247. EVT MemVT, Align Alignment,
  1248. MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
  1249. const MDNode *Ranges = nullptr, bool IsExpanding = false);
  1250. inline SDValue
  1251. getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT,
  1252. const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset,
  1253. SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT,
  1254. MaybeAlign Alignment = MaybeAlign(),
  1255. MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
  1256. const AAMDNodes &AAInfo = AAMDNodes(),
  1257. const MDNode *Ranges = nullptr, bool IsExpanding = false) {
  1258. // Ensures that codegen never sees a None Alignment.
  1259. return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL,
  1260. PtrInfo, MemVT, Alignment.value_or(getEVTAlign(MemVT)),
  1261. MMOFlags, AAInfo, Ranges, IsExpanding);
  1262. }
  1263. SDValue getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT,
  1264. const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset,
  1265. SDValue Mask, SDValue EVL, EVT MemVT,
  1266. MachineMemOperand *MMO, bool IsExpanding = false);
  1267. SDValue getLoadVP(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr,
  1268. SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo,
  1269. MaybeAlign Alignment, MachineMemOperand::Flags MMOFlags,
  1270. const AAMDNodes &AAInfo, const MDNode *Ranges = nullptr,
  1271. bool IsExpanding = false);
  1272. SDValue getLoadVP(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr,
  1273. SDValue Mask, SDValue EVL, MachineMemOperand *MMO,
  1274. bool IsExpanding = false);
  1275. SDValue getExtLoadVP(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT,
  1276. SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL,
  1277. MachinePointerInfo PtrInfo, EVT MemVT,
  1278. MaybeAlign Alignment, MachineMemOperand::Flags MMOFlags,
  1279. const AAMDNodes &AAInfo, bool IsExpanding = false);
  1280. SDValue getExtLoadVP(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT,
  1281. SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL,
  1282. EVT MemVT, MachineMemOperand *MMO,
  1283. bool IsExpanding = false);
  1284. SDValue getIndexedLoadVP(SDValue OrigLoad, const SDLoc &dl, SDValue Base,
  1285. SDValue Offset, ISD::MemIndexedMode AM);
  1286. SDValue getStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
  1287. SDValue Offset, SDValue Mask, SDValue EVL, EVT MemVT,
  1288. MachineMemOperand *MMO, ISD::MemIndexedMode AM,
  1289. bool IsTruncating = false, bool IsCompressing = false);
  1290. SDValue getTruncStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val,
  1291. SDValue Ptr, SDValue Mask, SDValue EVL,
  1292. MachinePointerInfo PtrInfo, EVT SVT, Align Alignment,
  1293. MachineMemOperand::Flags MMOFlags,
  1294. const AAMDNodes &AAInfo, bool IsCompressing = false);
  1295. SDValue getTruncStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val,
  1296. SDValue Ptr, SDValue Mask, SDValue EVL, EVT SVT,
  1297. MachineMemOperand *MMO, bool IsCompressing = false);
  1298. SDValue getIndexedStoreVP(SDValue OrigStore, const SDLoc &dl, SDValue Base,
  1299. SDValue Offset, ISD::MemIndexedMode AM);
  1300. SDValue getStridedLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
  1301. EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr,
  1302. SDValue Offset, SDValue Stride, SDValue Mask,
  1303. SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT,
  1304. Align Alignment, MachineMemOperand::Flags MMOFlags,
  1305. const AAMDNodes &AAInfo,
  1306. const MDNode *Ranges = nullptr,
  1307. bool IsExpanding = false);
  1308. inline SDValue getStridedLoadVP(
  1309. ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
  1310. SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
  1311. SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT,
  1312. MaybeAlign Alignment = MaybeAlign(),
  1313. MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
  1314. const AAMDNodes &AAInfo = AAMDNodes(), const MDNode *Ranges = nullptr,
  1315. bool IsExpanding = false) {
  1316. // Ensures that codegen never sees a None Alignment.
  1317. return getStridedLoadVP(AM, ExtType, VT, DL, Chain, Ptr, Offset, Stride,
  1318. Mask, EVL, PtrInfo, MemVT,
  1319. Alignment.value_or(getEVTAlign(MemVT)), MMOFlags,
  1320. AAInfo, Ranges, IsExpanding);
  1321. }
  1322. SDValue getStridedLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
  1323. EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr,
  1324. SDValue Offset, SDValue Stride, SDValue Mask,
  1325. SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
  1326. bool IsExpanding = false);
  1327. SDValue getStridedLoadVP(EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr,
  1328. SDValue Stride, SDValue Mask, SDValue EVL,
  1329. MachinePointerInfo PtrInfo, MaybeAlign Alignment,
  1330. MachineMemOperand::Flags MMOFlags,
  1331. const AAMDNodes &AAInfo,
  1332. const MDNode *Ranges = nullptr,
  1333. bool IsExpanding = false);
  1334. SDValue getStridedLoadVP(EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr,
  1335. SDValue Stride, SDValue Mask, SDValue EVL,
  1336. MachineMemOperand *MMO, bool IsExpanding = false);
  1337. SDValue
  1338. getExtStridedLoadVP(ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT,
  1339. SDValue Chain, SDValue Ptr, SDValue Stride, SDValue Mask,
  1340. SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT,
  1341. MaybeAlign Alignment, MachineMemOperand::Flags MMOFlags,
  1342. const AAMDNodes &AAInfo, bool IsExpanding = false);
  1343. SDValue getExtStridedLoadVP(ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT,
  1344. SDValue Chain, SDValue Ptr, SDValue Stride,
  1345. SDValue Mask, SDValue EVL, EVT MemVT,
  1346. MachineMemOperand *MMO, bool IsExpanding = false);
  1347. SDValue getIndexedStridedLoadVP(SDValue OrigLoad, const SDLoc &DL,
  1348. SDValue Base, SDValue Offset,
  1349. ISD::MemIndexedMode AM);
  1350. SDValue getStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val,
  1351. SDValue Ptr, SDValue Offset, SDValue Stride,
  1352. SDValue Mask, SDValue EVL, EVT MemVT,
  1353. MachineMemOperand *MMO, ISD::MemIndexedMode AM,
  1354. bool IsTruncating = false,
  1355. bool IsCompressing = false);
  1356. SDValue getTruncStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val,
  1357. SDValue Ptr, SDValue Stride, SDValue Mask,
  1358. SDValue EVL, MachinePointerInfo PtrInfo,
  1359. EVT SVT, Align Alignment,
  1360. MachineMemOperand::Flags MMOFlags,
  1361. const AAMDNodes &AAInfo,
  1362. bool IsCompressing = false);
  1363. SDValue getTruncStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val,
  1364. SDValue Ptr, SDValue Stride, SDValue Mask,
  1365. SDValue EVL, EVT SVT, MachineMemOperand *MMO,
  1366. bool IsCompressing = false);
  1367. SDValue getIndexedStridedStoreVP(SDValue OrigStore, const SDLoc &DL,
  1368. SDValue Base, SDValue Offset,
  1369. ISD::MemIndexedMode AM);
  1370. SDValue getGatherVP(SDVTList VTs, EVT VT, const SDLoc &dl,
  1371. ArrayRef<SDValue> Ops, MachineMemOperand *MMO,
  1372. ISD::MemIndexType IndexType);
  1373. SDValue getScatterVP(SDVTList VTs, EVT VT, const SDLoc &dl,
  1374. ArrayRef<SDValue> Ops, MachineMemOperand *MMO,
  1375. ISD::MemIndexType IndexType);
  1376. SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Base,
  1377. SDValue Offset, SDValue Mask, SDValue Src0, EVT MemVT,
  1378. MachineMemOperand *MMO, ISD::MemIndexedMode AM,
  1379. ISD::LoadExtType, bool IsExpanding = false);
  1380. SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base,
  1381. SDValue Offset, ISD::MemIndexedMode AM);
  1382. SDValue getMaskedStore(SDValue Chain, const SDLoc &dl, SDValue Val,
  1383. SDValue Base, SDValue Offset, SDValue Mask, EVT MemVT,
  1384. MachineMemOperand *MMO, ISD::MemIndexedMode AM,
  1385. bool IsTruncating = false, bool IsCompressing = false);
  1386. SDValue getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl,
  1387. SDValue Base, SDValue Offset,
  1388. ISD::MemIndexedMode AM);
  1389. SDValue getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl,
  1390. ArrayRef<SDValue> Ops, MachineMemOperand *MMO,
  1391. ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy);
  1392. SDValue getMaskedScatter(SDVTList VTs, EVT MemVT, const SDLoc &dl,
  1393. ArrayRef<SDValue> Ops, MachineMemOperand *MMO,
  1394. ISD::MemIndexType IndexType,
  1395. bool IsTruncating = false);
  1396. /// Construct a node to track a Value* through the backend.
  1397. SDValue getSrcValue(const Value *v);
  1398. /// Return an MDNodeSDNode which holds an MDNode.
  1399. SDValue getMDNode(const MDNode *MD);
  1400. /// Return a bitcast using the SDLoc of the value operand, and casting to the
  1401. /// provided type. Use getNode to set a custom SDLoc.
  1402. SDValue getBitcast(EVT VT, SDValue V);
  1403. /// Return an AddrSpaceCastSDNode.
  1404. SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS,
  1405. unsigned DestAS);
  1406. /// Return a freeze using the SDLoc of the value operand.
  1407. SDValue getFreeze(SDValue V);
  1408. /// Return an AssertAlignSDNode.
  1409. SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A);
  1410. /// Swap N1 and N2 if Opcode is a commutative binary opcode
  1411. /// and the canonical form expects the opposite order.
  1412. void canonicalizeCommutativeBinop(unsigned Opcode, SDValue &N1,
  1413. SDValue &N2) const;
  1414. /// Return the specified value casted to
  1415. /// the target's desired shift amount type.
  1416. SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op);
  1417. /// Expand the specified \c ISD::VAARG node as the Legalize pass would.
  1418. SDValue expandVAArg(SDNode *Node);
  1419. /// Expand the specified \c ISD::VACOPY node as the Legalize pass would.
  1420. SDValue expandVACopy(SDNode *Node);
  1421. /// Returs an GlobalAddress of the function from the current module with
  1422. /// name matching the given ExternalSymbol. Additionally can provide the
  1423. /// matched function.
  1424. /// Panics the function doesn't exists.
  1425. SDValue getSymbolFunctionGlobalAddress(SDValue Op,
  1426. Function **TargetFunction = nullptr);
  1427. /// *Mutate* the specified node in-place to have the
  1428. /// specified operands. If the resultant node already exists in the DAG,
  1429. /// this does not modify the specified node, instead it returns the node that
  1430. /// already exists. If the resultant node does not exist in the DAG, the
  1431. /// input node is returned. As a degenerate case, if you specify the same
  1432. /// input operands as the node already has, the input node is returned.
  1433. SDNode *UpdateNodeOperands(SDNode *N, SDValue Op);
  1434. SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2);
  1435. SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
  1436. SDValue Op3);
  1437. SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
  1438. SDValue Op3, SDValue Op4);
  1439. SDNode *UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
  1440. SDValue Op3, SDValue Op4, SDValue Op5);
  1441. SDNode *UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops);
  1442. /// Creates a new TokenFactor containing \p Vals. If \p Vals contains 64k
  1443. /// values or more, move values into new TokenFactors in 64k-1 blocks, until
  1444. /// the final TokenFactor has less than 64k operands.
  1445. SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl<SDValue> &Vals);
  1446. /// *Mutate* the specified machine node's memory references to the provided
  1447. /// list.
  1448. void setNodeMemRefs(MachineSDNode *N,
  1449. ArrayRef<MachineMemOperand *> NewMemRefs);
  1450. // Calculate divergence of node \p N based on its operands.
  1451. bool calculateDivergence(SDNode *N);
  1452. // Propagates the change in divergence to users
  1453. void updateDivergence(SDNode * N);
  1454. /// These are used for target selectors to *mutate* the
  1455. /// specified node to have the specified return type, Target opcode, and
  1456. /// operands. Note that target opcodes are stored as
  1457. /// ~TargetOpcode in the node opcode field. The resultant node is returned.
  1458. SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT);
  1459. SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT, SDValue Op1);
  1460. SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT,
  1461. SDValue Op1, SDValue Op2);
  1462. SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT,
  1463. SDValue Op1, SDValue Op2, SDValue Op3);
  1464. SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT,
  1465. ArrayRef<SDValue> Ops);
  1466. SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1, EVT VT2);
  1467. SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1,
  1468. EVT VT2, ArrayRef<SDValue> Ops);
  1469. SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1,
  1470. EVT VT2, EVT VT3, ArrayRef<SDValue> Ops);
  1471. SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1,
  1472. EVT VT2, SDValue Op1, SDValue Op2);
  1473. SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, SDVTList VTs,
  1474. ArrayRef<SDValue> Ops);
  1475. /// This *mutates* the specified node to have the specified
  1476. /// return type, opcode, and operands.
  1477. SDNode *MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs,
  1478. ArrayRef<SDValue> Ops);
  1479. /// Mutate the specified strict FP node to its non-strict equivalent,
  1480. /// unlinking the node from its chain and dropping the metadata arguments.
  1481. /// The node must be a strict FP node.
  1482. SDNode *mutateStrictFPToFP(SDNode *Node);
  1483. /// These are used for target selectors to create a new node
  1484. /// with specified return type(s), MachineInstr opcode, and operands.
  1485. ///
  1486. /// Note that getMachineNode returns the resultant node. If there is already
  1487. /// a node of the specified opcode and operands, it returns that node instead
  1488. /// of the current one.
  1489. MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT);
  1490. MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT,
  1491. SDValue Op1);
  1492. MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT,
  1493. SDValue Op1, SDValue Op2);
  1494. MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT,
  1495. SDValue Op1, SDValue Op2, SDValue Op3);
  1496. MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT,
  1497. ArrayRef<SDValue> Ops);
  1498. MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
  1499. EVT VT2, SDValue Op1, SDValue Op2);
  1500. MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
  1501. EVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
  1502. MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
  1503. EVT VT2, ArrayRef<SDValue> Ops);
  1504. MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
  1505. EVT VT2, EVT VT3, SDValue Op1, SDValue Op2);
  1506. MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
  1507. EVT VT2, EVT VT3, SDValue Op1, SDValue Op2,
  1508. SDValue Op3);
  1509. MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT1,
  1510. EVT VT2, EVT VT3, ArrayRef<SDValue> Ops);
  1511. MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl,
  1512. ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops);
  1513. MachineSDNode *getMachineNode(unsigned Opcode, const SDLoc &dl, SDVTList VTs,
  1514. ArrayRef<SDValue> Ops);
  1515. /// A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
  1516. SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT,
  1517. SDValue Operand);
  1518. /// A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
  1519. SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT,
  1520. SDValue Operand, SDValue Subreg);
  1521. /// Get the specified node if it's already available, or else return NULL.
  1522. SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTList,
  1523. ArrayRef<SDValue> Ops, const SDNodeFlags Flags);
  1524. SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTList,
  1525. ArrayRef<SDValue> Ops);
  1526. /// Check if a node exists without modifying its flags.
  1527. bool doesNodeExist(unsigned Opcode, SDVTList VTList, ArrayRef<SDValue> Ops);
  1528. /// Creates a SDDbgValue node.
  1529. SDDbgValue *getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N,
  1530. unsigned R, bool IsIndirect, const DebugLoc &DL,
  1531. unsigned O);
  1532. /// Creates a constant SDDbgValue node.
  1533. SDDbgValue *getConstantDbgValue(DIVariable *Var, DIExpression *Expr,
  1534. const Value *C, const DebugLoc &DL,
  1535. unsigned O);
  1536. /// Creates a FrameIndex SDDbgValue node.
  1537. SDDbgValue *getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr,
  1538. unsigned FI, bool IsIndirect,
  1539. const DebugLoc &DL, unsigned O);
  1540. /// Creates a FrameIndex SDDbgValue node.
  1541. SDDbgValue *getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr,
  1542. unsigned FI,
  1543. ArrayRef<SDNode *> Dependencies,
  1544. bool IsIndirect, const DebugLoc &DL,
  1545. unsigned O);
  1546. /// Creates a VReg SDDbgValue node.
  1547. SDDbgValue *getVRegDbgValue(DIVariable *Var, DIExpression *Expr,
  1548. unsigned VReg, bool IsIndirect,
  1549. const DebugLoc &DL, unsigned O);
  1550. /// Creates a SDDbgValue node from a list of locations.
  1551. SDDbgValue *getDbgValueList(DIVariable *Var, DIExpression *Expr,
  1552. ArrayRef<SDDbgOperand> Locs,
  1553. ArrayRef<SDNode *> Dependencies, bool IsIndirect,
  1554. const DebugLoc &DL, unsigned O, bool IsVariadic);
  1555. /// Creates a SDDbgLabel node.
  1556. SDDbgLabel *getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O);
  1557. /// Transfer debug values from one node to another, while optionally
  1558. /// generating fragment expressions for split-up values. If \p InvalidateDbg
  1559. /// is set, debug values are invalidated after they are transferred.
  1560. void transferDbgValues(SDValue From, SDValue To, unsigned OffsetInBits = 0,
  1561. unsigned SizeInBits = 0, bool InvalidateDbg = true);
  1562. /// Remove the specified node from the system. If any of its
  1563. /// operands then becomes dead, remove them as well. Inform UpdateListener
  1564. /// for each node deleted.
  1565. void RemoveDeadNode(SDNode *N);
  1566. /// This method deletes the unreachable nodes in the
  1567. /// given list, and any nodes that become unreachable as a result.
  1568. void RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes);
  1569. /// Modify anything using 'From' to use 'To' instead.
  1570. /// This can cause recursive merging of nodes in the DAG. Use the first
  1571. /// version if 'From' is known to have a single result, use the second
  1572. /// if you have two nodes with identical results (or if 'To' has a superset
  1573. /// of the results of 'From'), use the third otherwise.
  1574. ///
  1575. /// These methods all take an optional UpdateListener, which (if not null) is
  1576. /// informed about nodes that are deleted and modified due to recursive
  1577. /// changes in the dag.
  1578. ///
  1579. /// These functions only replace all existing uses. It's possible that as
  1580. /// these replacements are being performed, CSE may cause the From node
  1581. /// to be given new uses. These new uses of From are left in place, and
  1582. /// not automatically transferred to To.
  1583. ///
  1584. void ReplaceAllUsesWith(SDValue From, SDValue To);
  1585. void ReplaceAllUsesWith(SDNode *From, SDNode *To);
  1586. void ReplaceAllUsesWith(SDNode *From, const SDValue *To);
  1587. /// Replace any uses of From with To, leaving
  1588. /// uses of other values produced by From.getNode() alone.
  1589. void ReplaceAllUsesOfValueWith(SDValue From, SDValue To);
  1590. /// Like ReplaceAllUsesOfValueWith, but for multiple values at once.
  1591. /// This correctly handles the case where
  1592. /// there is an overlap between the From values and the To values.
  1593. void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To,
  1594. unsigned Num);
  1595. /// If an existing load has uses of its chain, create a token factor node with
  1596. /// that chain and the new memory node's chain and update users of the old
  1597. /// chain to the token factor. This ensures that the new memory node will have
  1598. /// the same relative memory dependency position as the old load. Returns the
  1599. /// new merged load chain.
  1600. SDValue makeEquivalentMemoryOrdering(SDValue OldChain, SDValue NewMemOpChain);
  1601. /// If an existing load has uses of its chain, create a token factor node with
  1602. /// that chain and the new memory node's chain and update users of the old
  1603. /// chain to the token factor. This ensures that the new memory node will have
  1604. /// the same relative memory dependency position as the old load. Returns the
  1605. /// new merged load chain.
  1606. SDValue makeEquivalentMemoryOrdering(LoadSDNode *OldLoad, SDValue NewMemOp);
  1607. /// Topological-sort the AllNodes list and a
  1608. /// assign a unique node id for each node in the DAG based on their
  1609. /// topological order. Returns the number of nodes.
  1610. unsigned AssignTopologicalOrder();
  1611. /// Move node N in the AllNodes list to be immediately
  1612. /// before the given iterator Position. This may be used to update the
  1613. /// topological ordering when the list of nodes is modified.
  1614. void RepositionNode(allnodes_iterator Position, SDNode *N) {
  1615. AllNodes.insert(Position, AllNodes.remove(N));
  1616. }
  1617. /// Returns an APFloat semantics tag appropriate for the given type. If VT is
  1618. /// a vector type, the element semantics are returned.
  1619. static const fltSemantics &EVTToAPFloatSemantics(EVT VT) {
  1620. switch (VT.getScalarType().getSimpleVT().SimpleTy) {
  1621. default: llvm_unreachable("Unknown FP format");
  1622. case MVT::f16: return APFloat::IEEEhalf();
  1623. case MVT::bf16: return APFloat::BFloat();
  1624. case MVT::f32: return APFloat::IEEEsingle();
  1625. case MVT::f64: return APFloat::IEEEdouble();
  1626. case MVT::f80: return APFloat::x87DoubleExtended();
  1627. case MVT::f128: return APFloat::IEEEquad();
  1628. case MVT::ppcf128: return APFloat::PPCDoubleDouble();
  1629. }
  1630. }
  1631. /// Add a dbg_value SDNode. If SD is non-null that means the
  1632. /// value is produced by SD.
  1633. void AddDbgValue(SDDbgValue *DB, bool isParameter);
  1634. /// Add a dbg_label SDNode.
  1635. void AddDbgLabel(SDDbgLabel *DB);
  1636. /// Get the debug values which reference the given SDNode.
  1637. ArrayRef<SDDbgValue*> GetDbgValues(const SDNode* SD) const {
  1638. return DbgInfo->getSDDbgValues(SD);
  1639. }
  1640. public:
  1641. /// Return true if there are any SDDbgValue nodes associated
  1642. /// with this SelectionDAG.
  1643. bool hasDebugValues() const { return !DbgInfo->empty(); }
  1644. SDDbgInfo::DbgIterator DbgBegin() const { return DbgInfo->DbgBegin(); }
  1645. SDDbgInfo::DbgIterator DbgEnd() const { return DbgInfo->DbgEnd(); }
  1646. SDDbgInfo::DbgIterator ByvalParmDbgBegin() const {
  1647. return DbgInfo->ByvalParmDbgBegin();
  1648. }
  1649. SDDbgInfo::DbgIterator ByvalParmDbgEnd() const {
  1650. return DbgInfo->ByvalParmDbgEnd();
  1651. }
  1652. SDDbgInfo::DbgLabelIterator DbgLabelBegin() const {
  1653. return DbgInfo->DbgLabelBegin();
  1654. }
  1655. SDDbgInfo::DbgLabelIterator DbgLabelEnd() const {
  1656. return DbgInfo->DbgLabelEnd();
  1657. }
  1658. /// To be invoked on an SDNode that is slated to be erased. This
  1659. /// function mirrors \c llvm::salvageDebugInfo.
  1660. void salvageDebugInfo(SDNode &N);
  1661. void dump() const;
  1662. /// In most cases this function returns the ABI alignment for a given type,
  1663. /// except for illegal vector types where the alignment exceeds that of the
  1664. /// stack. In such cases we attempt to break the vector down to a legal type
  1665. /// and return the ABI alignment for that instead.
  1666. Align getReducedAlign(EVT VT, bool UseABI);
  1667. /// Create a stack temporary based on the size in bytes and the alignment
  1668. SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment);
  1669. /// Create a stack temporary, suitable for holding the specified value type.
  1670. /// If minAlign is specified, the slot size will have at least that alignment.
  1671. SDValue CreateStackTemporary(EVT VT, unsigned minAlign = 1);
  1672. /// Create a stack temporary suitable for holding either of the specified
  1673. /// value types.
  1674. SDValue CreateStackTemporary(EVT VT1, EVT VT2);
  1675. SDValue FoldSymbolOffset(unsigned Opcode, EVT VT,
  1676. const GlobalAddressSDNode *GA,
  1677. const SDNode *N2);
  1678. SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT,
  1679. ArrayRef<SDValue> Ops);
  1680. /// Fold floating-point operations with 2 operands when both operands are
  1681. /// constants and/or undefined.
  1682. SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT,
  1683. SDValue N1, SDValue N2);
  1684. /// Constant fold a setcc to true or false.
  1685. SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond,
  1686. const SDLoc &dl);
  1687. /// Return true if the sign bit of Op is known to be zero.
  1688. /// We use this predicate to simplify operations downstream.
  1689. bool SignBitIsZero(SDValue Op, unsigned Depth = 0) const;
  1690. /// Return true if 'Op & Mask' is known to be zero. We
  1691. /// use this predicate to simplify operations downstream. Op and Mask are
  1692. /// known to be the same type.
  1693. bool MaskedValueIsZero(SDValue Op, const APInt &Mask,
  1694. unsigned Depth = 0) const;
  1695. /// Return true if 'Op & Mask' is known to be zero in DemandedElts. We
  1696. /// use this predicate to simplify operations downstream. Op and Mask are
  1697. /// known to be the same type.
  1698. bool MaskedValueIsZero(SDValue Op, const APInt &Mask,
  1699. const APInt &DemandedElts, unsigned Depth = 0) const;
  1700. /// Return true if 'Op' is known to be zero in DemandedElts. We
  1701. /// use this predicate to simplify operations downstream.
  1702. bool MaskedVectorIsZero(SDValue Op, const APInt &DemandedElts,
  1703. unsigned Depth = 0) const;
  1704. /// Return true if '(Op & Mask) == Mask'.
  1705. /// Op and Mask are known to be the same type.
  1706. bool MaskedValueIsAllOnes(SDValue Op, const APInt &Mask,
  1707. unsigned Depth = 0) const;
  1708. /// For each demanded element of a vector, see if it is known to be zero.
  1709. APInt computeVectorKnownZeroElements(SDValue Op, const APInt &DemandedElts,
  1710. unsigned Depth = 0) const;
  1711. /// Determine which bits of Op are known to be either zero or one and return
  1712. /// them in Known. For vectors, the known bits are those that are shared by
  1713. /// every vector element.
  1714. /// Targets can implement the computeKnownBitsForTargetNode method in the
  1715. /// TargetLowering class to allow target nodes to be understood.
  1716. KnownBits computeKnownBits(SDValue Op, unsigned Depth = 0) const;
  1717. /// Determine which bits of Op are known to be either zero or one and return
  1718. /// them in Known. The DemandedElts argument allows us to only collect the
  1719. /// known bits that are shared by the requested vector elements.
  1720. /// Targets can implement the computeKnownBitsForTargetNode method in the
  1721. /// TargetLowering class to allow target nodes to be understood.
  1722. KnownBits computeKnownBits(SDValue Op, const APInt &DemandedElts,
  1723. unsigned Depth = 0) const;
  1724. /// Used to represent the possible overflow behavior of an operation.
  1725. /// Never: the operation cannot overflow.
  1726. /// Always: the operation will always overflow.
  1727. /// Sometime: the operation may or may not overflow.
  1728. enum OverflowKind {
  1729. OFK_Never,
  1730. OFK_Sometime,
  1731. OFK_Always,
  1732. };
  1733. /// Determine if the result of the addition of 2 node can overflow.
  1734. OverflowKind computeOverflowKind(SDValue N0, SDValue N1) const;
  1735. /// Test if the given value is known to have exactly one bit set. This differs
  1736. /// from computeKnownBits in that it doesn't necessarily determine which bit
  1737. /// is set.
  1738. bool isKnownToBeAPowerOfTwo(SDValue Val) const;
  1739. /// Return the number of times the sign bit of the register is replicated into
  1740. /// the other bits. We know that at least 1 bit is always equal to the sign
  1741. /// bit (itself), but other cases can give us information. For example,
  1742. /// immediately after an "SRA X, 2", we know that the top 3 bits are all equal
  1743. /// to each other, so we return 3. Targets can implement the
  1744. /// ComputeNumSignBitsForTarget method in the TargetLowering class to allow
  1745. /// target nodes to be understood.
  1746. unsigned ComputeNumSignBits(SDValue Op, unsigned Depth = 0) const;
  1747. /// Return the number of times the sign bit of the register is replicated into
  1748. /// the other bits. We know that at least 1 bit is always equal to the sign
  1749. /// bit (itself), but other cases can give us information. For example,
  1750. /// immediately after an "SRA X, 2", we know that the top 3 bits are all equal
  1751. /// to each other, so we return 3. The DemandedElts argument allows
  1752. /// us to only collect the minimum sign bits of the requested vector elements.
  1753. /// Targets can implement the ComputeNumSignBitsForTarget method in the
  1754. /// TargetLowering class to allow target nodes to be understood.
  1755. unsigned ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
  1756. unsigned Depth = 0) const;
  1757. /// Get the upper bound on bit size for this Value \p Op as a signed integer.
  1758. /// i.e. x == sext(trunc(x to MaxSignedBits) to bitwidth(x)).
  1759. /// Similar to the APInt::getSignificantBits function.
  1760. /// Helper wrapper to ComputeNumSignBits.
  1761. unsigned ComputeMaxSignificantBits(SDValue Op, unsigned Depth = 0) const;
  1762. /// Get the upper bound on bit size for this Value \p Op as a signed integer.
  1763. /// i.e. x == sext(trunc(x to MaxSignedBits) to bitwidth(x)).
  1764. /// Similar to the APInt::getSignificantBits function.
  1765. /// Helper wrapper to ComputeNumSignBits.
  1766. unsigned ComputeMaxSignificantBits(SDValue Op, const APInt &DemandedElts,
  1767. unsigned Depth = 0) const;
  1768. /// Return true if this function can prove that \p Op is never poison
  1769. /// and, if \p PoisonOnly is false, does not have undef bits.
  1770. bool isGuaranteedNotToBeUndefOrPoison(SDValue Op, bool PoisonOnly = false,
  1771. unsigned Depth = 0) const;
  1772. /// Return true if this function can prove that \p Op is never poison
  1773. /// and, if \p PoisonOnly is false, does not have undef bits. The DemandedElts
  1774. /// argument limits the check to the requested vector elements.
  1775. bool isGuaranteedNotToBeUndefOrPoison(SDValue Op, const APInt &DemandedElts,
  1776. bool PoisonOnly = false,
  1777. unsigned Depth = 0) const;
  1778. /// Return true if this function can prove that \p Op is never poison.
  1779. bool isGuaranteedNotToBePoison(SDValue Op, unsigned Depth = 0) const {
  1780. return isGuaranteedNotToBeUndefOrPoison(Op, /*PoisonOnly*/ true, Depth);
  1781. }
  1782. /// Return true if this function can prove that \p Op is never poison. The
  1783. /// DemandedElts argument limits the check to the requested vector elements.
  1784. bool isGuaranteedNotToBePoison(SDValue Op, const APInt &DemandedElts,
  1785. unsigned Depth = 0) const {
  1786. return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts,
  1787. /*PoisonOnly*/ true, Depth);
  1788. }
  1789. /// Return true if Op can create undef or poison from non-undef & non-poison
  1790. /// operands. The DemandedElts argument limits the check to the requested
  1791. /// vector elements.
  1792. ///
  1793. /// \p ConsiderFlags controls whether poison producing flags on the
  1794. /// instruction are considered. This can be used to see if the instruction
  1795. /// could still introduce undef or poison even without poison generating flags
  1796. /// which might be on the instruction. (i.e. could the result of
  1797. /// Op->dropPoisonGeneratingFlags() still create poison or undef)
  1798. bool canCreateUndefOrPoison(SDValue Op, const APInt &DemandedElts,
  1799. bool PoisonOnly = false,
  1800. bool ConsiderFlags = true,
  1801. unsigned Depth = 0) const;
  1802. /// Return true if Op can create undef or poison from non-undef & non-poison
  1803. /// operands.
  1804. ///
  1805. /// \p ConsiderFlags controls whether poison producing flags on the
  1806. /// instruction are considered. This can be used to see if the instruction
  1807. /// could still introduce undef or poison even without poison generating flags
  1808. /// which might be on the instruction. (i.e. could the result of
  1809. /// Op->dropPoisonGeneratingFlags() still create poison or undef)
  1810. bool canCreateUndefOrPoison(SDValue Op, bool PoisonOnly = false,
  1811. bool ConsiderFlags = true,
  1812. unsigned Depth = 0) const;
  1813. /// Return true if the specified operand is an ISD::ADD with a ConstantSDNode
  1814. /// on the right-hand side, or if it is an ISD::OR with a ConstantSDNode that
  1815. /// is guaranteed to have the same semantics as an ADD. This handles the
  1816. /// equivalence:
  1817. /// X|Cst == X+Cst iff X&Cst = 0.
  1818. bool isBaseWithConstantOffset(SDValue Op) const;
  1819. /// Test whether the given SDValue (or all elements of it, if it is a
  1820. /// vector) is known to never be NaN. If \p SNaN is true, returns if \p Op is
  1821. /// known to never be a signaling NaN (it may still be a qNaN).
  1822. bool isKnownNeverNaN(SDValue Op, bool SNaN = false, unsigned Depth = 0) const;
  1823. /// \returns true if \p Op is known to never be a signaling NaN.
  1824. bool isKnownNeverSNaN(SDValue Op, unsigned Depth = 0) const {
  1825. return isKnownNeverNaN(Op, true, Depth);
  1826. }
  1827. /// Test whether the given floating point SDValue is known to never be
  1828. /// positive or negative zero.
  1829. bool isKnownNeverZeroFloat(SDValue Op) const;
  1830. /// Test whether the given SDValue is known to contain non-zero value(s).
  1831. bool isKnownNeverZero(SDValue Op) const;
  1832. /// Test whether two SDValues are known to compare equal. This
  1833. /// is true if they are the same value, or if one is negative zero and the
  1834. /// other positive zero.
  1835. bool isEqualTo(SDValue A, SDValue B) const;
  1836. /// Return true if A and B have no common bits set. As an example, this can
  1837. /// allow an 'add' to be transformed into an 'or'.
  1838. bool haveNoCommonBitsSet(SDValue A, SDValue B) const;
  1839. /// Test whether \p V has a splatted value for all the demanded elements.
  1840. ///
  1841. /// On success \p UndefElts will indicate the elements that have UNDEF
  1842. /// values instead of the splat value, this is only guaranteed to be correct
  1843. /// for \p DemandedElts.
  1844. ///
  1845. /// NOTE: The function will return true for a demanded splat of UNDEF values.
  1846. bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts,
  1847. unsigned Depth = 0) const;
  1848. /// Test whether \p V has a splatted value.
  1849. bool isSplatValue(SDValue V, bool AllowUndefs = false) const;
  1850. /// If V is a splatted value, return the source vector and its splat index.
  1851. SDValue getSplatSourceVector(SDValue V, int &SplatIndex);
  1852. /// If V is a splat vector, return its scalar source operand by extracting
  1853. /// that element from the source vector. If LegalTypes is true, this method
  1854. /// may only return a legally-typed splat value. If it cannot legalize the
  1855. /// splatted value it will return SDValue().
  1856. SDValue getSplatValue(SDValue V, bool LegalTypes = false);
  1857. /// If a SHL/SRA/SRL node \p V has a constant or splat constant shift amount
  1858. /// that is less than the element bit-width of the shift node, return it.
  1859. const APInt *getValidShiftAmountConstant(SDValue V,
  1860. const APInt &DemandedElts) const;
  1861. /// If a SHL/SRA/SRL node \p V has constant shift amounts that are all less
  1862. /// than the element bit-width of the shift node, return the minimum value.
  1863. const APInt *
  1864. getValidMinimumShiftAmountConstant(SDValue V,
  1865. const APInt &DemandedElts) const;
  1866. /// If a SHL/SRA/SRL node \p V has constant shift amounts that are all less
  1867. /// than the element bit-width of the shift node, return the maximum value.
  1868. const APInt *
  1869. getValidMaximumShiftAmountConstant(SDValue V,
  1870. const APInt &DemandedElts) const;
  1871. /// Match a binop + shuffle pyramid that represents a horizontal reduction
  1872. /// over the elements of a vector starting from the EXTRACT_VECTOR_ELT node /p
  1873. /// Extract. The reduction must use one of the opcodes listed in /p
  1874. /// CandidateBinOps and on success /p BinOp will contain the matching opcode.
  1875. /// Returns the vector that is being reduced on, or SDValue() if a reduction
  1876. /// was not matched. If \p AllowPartials is set then in the case of a
  1877. /// reduction pattern that only matches the first few stages, the extracted
  1878. /// subvector of the start of the reduction is returned.
  1879. SDValue matchBinOpReduction(SDNode *Extract, ISD::NodeType &BinOp,
  1880. ArrayRef<ISD::NodeType> CandidateBinOps,
  1881. bool AllowPartials = false);
  1882. /// Utility function used by legalize and lowering to
  1883. /// "unroll" a vector operation by splitting out the scalars and operating
  1884. /// on each element individually. If the ResNE is 0, fully unroll the vector
  1885. /// op. If ResNE is less than the width of the vector op, unroll up to ResNE.
  1886. /// If the ResNE is greater than the width of the vector op, unroll the
  1887. /// vector op and fill the end of the resulting vector with UNDEFS.
  1888. SDValue UnrollVectorOp(SDNode *N, unsigned ResNE = 0);
  1889. /// Like UnrollVectorOp(), but for the [US](ADD|SUB|MUL)O family of opcodes.
  1890. /// This is a separate function because those opcodes have two results.
  1891. std::pair<SDValue, SDValue> UnrollVectorOverflowOp(SDNode *N,
  1892. unsigned ResNE = 0);
  1893. /// Return true if loads are next to each other and can be
  1894. /// merged. Check that both are nonvolatile and if LD is loading
  1895. /// 'Bytes' bytes from a location that is 'Dist' units away from the
  1896. /// location that the 'Base' load is loading from.
  1897. bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base,
  1898. unsigned Bytes, int Dist) const;
  1899. /// Infer alignment of a load / store address. Return std::nullopt if it
  1900. /// cannot be inferred.
  1901. MaybeAlign InferPtrAlign(SDValue Ptr) const;
  1902. /// Compute the VTs needed for the low/hi parts of a type
  1903. /// which is split (or expanded) into two not necessarily identical pieces.
  1904. std::pair<EVT, EVT> GetSplitDestVTs(const EVT &VT) const;
  1905. /// Compute the VTs needed for the low/hi parts of a type, dependent on an
  1906. /// enveloping VT that has been split into two identical pieces. Sets the
  1907. /// HisIsEmpty flag when hi type has zero storage size.
  1908. std::pair<EVT, EVT> GetDependentSplitDestVTs(const EVT &VT, const EVT &EnvVT,
  1909. bool *HiIsEmpty) const;
  1910. /// Split the vector with EXTRACT_SUBVECTOR using the provides
  1911. /// VTs and return the low/high part.
  1912. std::pair<SDValue, SDValue> SplitVector(const SDValue &N, const SDLoc &DL,
  1913. const EVT &LoVT, const EVT &HiVT);
  1914. /// Split the vector with EXTRACT_SUBVECTOR and return the low/high part.
  1915. std::pair<SDValue, SDValue> SplitVector(const SDValue &N, const SDLoc &DL) {
  1916. EVT LoVT, HiVT;
  1917. std::tie(LoVT, HiVT) = GetSplitDestVTs(N.getValueType());
  1918. return SplitVector(N, DL, LoVT, HiVT);
  1919. }
  1920. /// Split the explicit vector length parameter of a VP operation.
  1921. std::pair<SDValue, SDValue> SplitEVL(SDValue N, EVT VecVT, const SDLoc &DL);
  1922. /// Split the node's operand with EXTRACT_SUBVECTOR and
  1923. /// return the low/high part.
  1924. std::pair<SDValue, SDValue> SplitVectorOperand(const SDNode *N, unsigned OpNo)
  1925. {
  1926. return SplitVector(N->getOperand(OpNo), SDLoc(N));
  1927. }
  1928. /// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
  1929. SDValue WidenVector(const SDValue &N, const SDLoc &DL);
  1930. /// Append the extracted elements from Start to Count out of the vector Op in
  1931. /// Args. If Count is 0, all of the elements will be extracted. The extracted
  1932. /// elements will have type EVT if it is provided, and otherwise their type
  1933. /// will be Op's element type.
  1934. void ExtractVectorElements(SDValue Op, SmallVectorImpl<SDValue> &Args,
  1935. unsigned Start = 0, unsigned Count = 0,
  1936. EVT EltVT = EVT());
  1937. /// Compute the default alignment value for the given type.
  1938. Align getEVTAlign(EVT MemoryVT) const;
  1939. /// Test whether the given value is a constant int or similar node.
  1940. SDNode *isConstantIntBuildVectorOrConstantInt(SDValue N) const;
  1941. /// Test whether the given value is a constant FP or similar node.
  1942. SDNode *isConstantFPBuildVectorOrConstantFP(SDValue N) const ;
  1943. /// \returns true if \p N is any kind of constant or build_vector of
  1944. /// constants, int or float. If a vector, it may not necessarily be a splat.
  1945. inline bool isConstantValueOfAnyType(SDValue N) const {
  1946. return isConstantIntBuildVectorOrConstantInt(N) ||
  1947. isConstantFPBuildVectorOrConstantFP(N);
  1948. }
  1949. /// Set CallSiteInfo to be associated with Node.
  1950. void addCallSiteInfo(const SDNode *Node, CallSiteInfoImpl &&CallInfo) {
  1951. SDEI[Node].CSInfo = std::move(CallInfo);
  1952. }
  1953. /// Return CallSiteInfo associated with Node, or a default if none exists.
  1954. CallSiteInfo getCallSiteInfo(const SDNode *Node) {
  1955. auto I = SDEI.find(Node);
  1956. return I != SDEI.end() ? std::move(I->second).CSInfo : CallSiteInfo();
  1957. }
  1958. /// Set HeapAllocSite to be associated with Node.
  1959. void addHeapAllocSite(const SDNode *Node, MDNode *MD) {
  1960. SDEI[Node].HeapAllocSite = MD;
  1961. }
  1962. /// Return HeapAllocSite associated with Node, or nullptr if none exists.
  1963. MDNode *getHeapAllocSite(const SDNode *Node) const {
  1964. auto I = SDEI.find(Node);
  1965. return I != SDEI.end() ? I->second.HeapAllocSite : nullptr;
  1966. }
  1967. /// Set PCSections to be associated with Node.
  1968. void addPCSections(const SDNode *Node, MDNode *MD) {
  1969. SDEI[Node].PCSections = MD;
  1970. }
  1971. /// Return PCSections associated with Node, or nullptr if none exists.
  1972. MDNode *getPCSections(const SDNode *Node) const {
  1973. auto It = SDEI.find(Node);
  1974. return It != SDEI.end() ? It->second.PCSections : nullptr;
  1975. }
  1976. /// Set NoMergeSiteInfo to be associated with Node if NoMerge is true.
  1977. void addNoMergeSiteInfo(const SDNode *Node, bool NoMerge) {
  1978. if (NoMerge)
  1979. SDEI[Node].NoMerge = NoMerge;
  1980. }
  1981. /// Return NoMerge info associated with Node.
  1982. bool getNoMergeSiteInfo(const SDNode *Node) const {
  1983. auto I = SDEI.find(Node);
  1984. return I != SDEI.end() ? I->second.NoMerge : false;
  1985. }
  1986. /// Copy extra info associated with one node to another.
  1987. void copyExtraInfo(SDNode *From, SDNode *To);
  1988. /// Return the current function's default denormal handling kind for the given
  1989. /// floating point type.
  1990. DenormalMode getDenormalMode(EVT VT) const {
  1991. return MF->getDenormalMode(EVTToAPFloatSemantics(VT));
  1992. }
  1993. bool shouldOptForSize() const;
  1994. /// Get the (commutative) neutral element for the given opcode, if it exists.
  1995. SDValue getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT,
  1996. SDNodeFlags Flags);
  1997. /// Some opcodes may create immediate undefined behavior when used with some
  1998. /// values (integer division-by-zero for example). Therefore, these operations
  1999. /// are not generally safe to move around or change.
  2000. bool isSafeToSpeculativelyExecute(unsigned Opcode) const {
  2001. switch (Opcode) {
  2002. case ISD::SDIV:
  2003. case ISD::SREM:
  2004. case ISD::SDIVREM:
  2005. case ISD::UDIV:
  2006. case ISD::UREM:
  2007. case ISD::UDIVREM:
  2008. return false;
  2009. default:
  2010. return true;
  2011. }
  2012. }
  2013. private:
  2014. void InsertNode(SDNode *N);
  2015. bool RemoveNodeFromCSEMaps(SDNode *N);
  2016. void AddModifiedNodeToCSEMaps(SDNode *N);
  2017. SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op, void *&InsertPos);
  2018. SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op1, SDValue Op2,
  2019. void *&InsertPos);
  2020. SDNode *FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
  2021. void *&InsertPos);
  2022. SDNode *UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &loc);
  2023. void DeleteNodeNotInCSEMaps(SDNode *N);
  2024. void DeallocateNode(SDNode *N);
  2025. void allnodes_clear();
  2026. /// Look up the node specified by ID in CSEMap. If it exists, return it. If
  2027. /// not, return the insertion token that will make insertion faster. This
  2028. /// overload is for nodes other than Constant or ConstantFP, use the other one
  2029. /// for those.
  2030. SDNode *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos);
  2031. /// Look up the node specified by ID in CSEMap. If it exists, return it. If
  2032. /// not, return the insertion token that will make insertion faster. Performs
  2033. /// additional processing for constant nodes.
  2034. SDNode *FindNodeOrInsertPos(const FoldingSetNodeID &ID, const SDLoc &DL,
  2035. void *&InsertPos);
  2036. /// Maps to auto-CSE operations.
  2037. std::vector<CondCodeSDNode*> CondCodeNodes;
  2038. std::vector<SDNode*> ValueTypeNodes;
  2039. std::map<EVT, SDNode*, EVT::compareRawBits> ExtendedValueTypeNodes;
  2040. StringMap<SDNode*> ExternalSymbols;
  2041. std::map<std::pair<std::string, unsigned>, SDNode *> TargetExternalSymbols;
  2042. DenseMap<MCSymbol *, SDNode *> MCSymbols;
  2043. FlagInserter *Inserter = nullptr;
  2044. };
  2045. template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
  2046. using nodes_iterator = pointer_iterator<SelectionDAG::allnodes_iterator>;
  2047. static nodes_iterator nodes_begin(SelectionDAG *G) {
  2048. return nodes_iterator(G->allnodes_begin());
  2049. }
  2050. static nodes_iterator nodes_end(SelectionDAG *G) {
  2051. return nodes_iterator(G->allnodes_end());
  2052. }
  2053. };
  2054. } // end namespace llvm
  2055. #endif // LLVM_CODEGEN_SELECTIONDAG_H
  2056. #ifdef __GNUC__
  2057. #pragma GCC diagnostic pop
  2058. #endif