SelectionDAG.h 97 KB

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