1 //===-- llvm/CodeGen/SelectionDAG.h - InstSelection DAG ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the SelectionDAG class, and transitively defines the
11 // SDNode class and subclasses.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_SELECTIONDAG_H
16 #define LLVM_CODEGEN_SELECTIONDAG_H
18 #include "llvm/CodeGen/SelectionDAGNodes.h"
19 #include "llvm/CodeGen/SelectionDAGCSEMap.h"
20 #include "llvm/ADT/ilist"
30 class MachineDebugInfo;
31 class MachineFunction;
33 /// SelectionDAG class - This is used to represent a portion of an LLVM function
34 /// in a low-level Data Dependence DAG representation suitable for instruction
35 /// selection. This DAG is constructed as the first step of instruction
36 /// selection in order to allow implementation of machine specific optimizations
37 /// and code simplifications.
39 /// The representation used by the SelectionDAG is a target-independent
40 /// representation, which has some similarities to the GCC RTL representation,
41 /// but is significantly more simple, powerful, and is a graph form instead of a
49 /// Root - The root of the entire DAG. EntryNode - The starting token.
50 SDOperand Root, EntryNode;
52 /// AllNodes - A linked list of nodes in the current DAG.
53 ilist<SDNode> AllNodes;
55 /// CSEMap - This structure is used to memoize nodes, automatically performing
56 /// CSE with existing nodes with a duplicate is requested.
57 SelectionDAGCSEMap CSEMap;
60 SelectionDAG(TargetLowering &tli, MachineFunction &mf, MachineDebugInfo *di)
61 : TLI(tli), MF(mf), DI(di) {
62 EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
66 MachineFunction &getMachineFunction() const { return MF; }
67 const TargetMachine &getTarget() const;
68 TargetLowering &getTargetLoweringInfo() const { return TLI; }
69 MachineDebugInfo *getMachineDebugInfo() const { return DI; }
71 /// viewGraph - Pop up a ghostview window with the DAG rendered using 'dot'.
76 typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
77 allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
78 allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
79 typedef ilist<SDNode>::iterator allnodes_iterator;
80 allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
81 allnodes_iterator allnodes_end() { return AllNodes.end(); }
83 /// getRoot - Return the root tag of the SelectionDAG.
85 const SDOperand &getRoot() const { return Root; }
87 /// getEntryNode - Return the token chain corresponding to the entry of the
89 const SDOperand &getEntryNode() const { return EntryNode; }
91 /// setRoot - Set the current root tag of the SelectionDAG.
93 const SDOperand &setRoot(SDOperand N) { return Root = N; }
95 /// Combine - This iterates over the nodes in the SelectionDAG, folding
96 /// certain types of nodes together, or eliminating superfluous nodes. When
97 /// the AfterLegalize argument is set to 'true', Combine takes care not to
98 /// generate any nodes that will be illegal on the target.
99 void Combine(bool AfterLegalize);
101 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
102 /// compatible with the target instruction selector, as indicated by the
103 /// TargetLowering object.
105 /// Note that this is an involved process that may invalidate pointers into
109 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
111 void RemoveDeadNodes();
113 SDOperand getString(const std::string &Val);
114 SDOperand getConstant(uint64_t Val, MVT::ValueType VT, bool isTarget = false);
115 SDOperand getTargetConstant(uint64_t Val, MVT::ValueType VT) {
116 return getConstant(Val, VT, true);
118 SDOperand getConstantFP(double Val, MVT::ValueType VT, bool isTarget = false);
119 SDOperand getTargetConstantFP(double Val, MVT::ValueType VT) {
120 return getConstantFP(Val, VT, true);
122 SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT,
123 int offset = 0, bool isTargetGA = false);
124 SDOperand getTargetGlobalAddress(const GlobalValue *GV, MVT::ValueType VT,
126 return getGlobalAddress(GV, VT, offset, true);
128 SDOperand getFrameIndex(int FI, MVT::ValueType VT, bool isTarget = false);
129 SDOperand getTargetFrameIndex(int FI, MVT::ValueType VT) {
130 return getFrameIndex(FI, VT, true);
132 SDOperand getJumpTable(int JTI, MVT::ValueType VT, bool isTarget = false);
133 SDOperand getTargetJumpTable(int JTI, MVT::ValueType VT) {
134 return getJumpTable(JTI, VT, true);
136 SDOperand getConstantPool(Constant *C, MVT::ValueType VT,
137 unsigned Align = 0, int Offs = 0, bool isT=false);
138 SDOperand getTargetConstantPool(Constant *C, MVT::ValueType VT,
139 unsigned Align = 0, int Offset = 0) {
140 return getConstantPool(C, VT, Align, Offset, true);
142 SDOperand getBasicBlock(MachineBasicBlock *MBB);
143 SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
144 SDOperand getTargetExternalSymbol(const char *Sym, MVT::ValueType VT);
145 SDOperand getValueType(MVT::ValueType);
146 SDOperand getRegister(unsigned Reg, MVT::ValueType VT);
148 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
149 return getNode(ISD::CopyToReg, MVT::Other, Chain,
150 getRegister(Reg, N.getValueType()), N);
153 // This version of the getCopyToReg method takes an extra operand, which
154 // indicates that there is potentially an incoming flag value (if Flag is not
155 // null) and that there should be a flag result.
156 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N,
158 std::vector<MVT::ValueType> VTs;
159 VTs.push_back(MVT::Other);
160 VTs.push_back(MVT::Flag);
161 SDOperand Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Flag };
162 return getNode(ISD::CopyToReg, VTs, Ops, Flag.Val ? 4 : 3);
165 // Similar to last getCopyToReg() except parameter Reg is a SDOperand
166 SDOperand getCopyToReg(SDOperand Chain, SDOperand Reg, SDOperand N,
168 std::vector<MVT::ValueType> VTs;
169 VTs.push_back(MVT::Other);
170 VTs.push_back(MVT::Flag);
171 SDOperand Ops[] = { Chain, Reg, N, Flag };
172 return getNode(ISD::CopyToReg, VTs, Ops, Flag.Val ? 4 : 3);
175 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
176 std::vector<MVT::ValueType> ResultTys;
177 ResultTys.push_back(VT);
178 ResultTys.push_back(MVT::Other);
179 SDOperand Ops[] = { Chain, getRegister(Reg, VT) };
180 return getNode(ISD::CopyFromReg, ResultTys, Ops, 2);
183 // This version of the getCopyFromReg method takes an extra operand, which
184 // indicates that there is potentially an incoming flag value (if Flag is not
185 // null) and that there should be a flag result.
186 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT,
188 std::vector<MVT::ValueType> ResultTys;
189 ResultTys.push_back(VT);
190 ResultTys.push_back(MVT::Other);
191 ResultTys.push_back(MVT::Flag);
192 SDOperand Ops[] = { Chain, getRegister(Reg, VT), Flag };
193 return getNode(ISD::CopyFromReg, ResultTys, Ops, Flag.Val ? 3 : 2);
196 SDOperand getCondCode(ISD::CondCode Cond);
198 /// getZeroExtendInReg - Return the expression required to zero extend the Op
199 /// value assuming it was the smaller SrcTy value.
200 SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
202 /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
203 /// a flag result (to ensure it's not CSE'd).
204 SDOperand getCALLSEQ_START(SDOperand Chain, SDOperand Op) {
205 std::vector<MVT::ValueType> ResultTys;
206 ResultTys.push_back(MVT::Other);
207 ResultTys.push_back(MVT::Flag);
208 SDOperand Ops[] = { Chain, Op };
209 return getNode(ISD::CALLSEQ_START, ResultTys, Ops, 2);
212 /// getNode - Gets or creates the specified node.
214 SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
215 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
216 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
217 SDOperand N1, SDOperand N2);
218 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
219 SDOperand N1, SDOperand N2, SDOperand N3);
220 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
221 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
222 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
223 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
225 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
226 const SDOperand *Ops, unsigned NumOps);
227 SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
228 const SDOperand *Ops, unsigned NumOps);
230 /// getSetCC - Helper function to make it easier to build SetCC's if you just
231 /// have an ISD::CondCode instead of an SDOperand.
233 SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS,
234 ISD::CondCode Cond) {
235 return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
238 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
239 /// just have an ISD::CondCode instead of an SDOperand.
241 SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
242 SDOperand True, SDOperand False, ISD::CondCode Cond) {
243 MVT::ValueType VT = True.getValueType();
244 return getNode(ISD::SELECT_CC, VT, LHS, RHS, True, False,getCondCode(Cond));
247 /// getVAArg - VAArg produces a result and token chain, and takes a pointer
248 /// and a source value as input.
249 SDOperand getVAArg(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
252 /// getLoad - Loads are not normal binary operators: their result type is not
253 /// determined by their operands, and they produce a value AND a token chain.
255 SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
257 SDOperand getVecLoad(unsigned Count, MVT::ValueType VT, SDOperand Chain,
258 SDOperand Ptr, SDOperand SV);
259 SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain,
260 SDOperand Ptr, SDOperand SV, MVT::ValueType EVT);
262 // getSrcValue - construct a node to track a Value* through the backend
263 SDOperand getSrcValue(const Value* I, int offset = 0);
265 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
266 /// specified operands. If the resultant node already exists in the DAG,
267 /// this does not modify the specified node, instead it returns the node that
268 /// already exists. If the resultant node does not exist in the DAG, the
269 /// input node is returned. As a degenerate case, if you specify the same
270 /// input operands as the node already has, the input node is returned.
271 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op);
272 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2);
273 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
275 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
276 SDOperand Op3, SDOperand Op4);
277 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
278 SDOperand Op3, SDOperand Op4, SDOperand Op5);
279 SDOperand UpdateNodeOperands(SDOperand N, SDOperand *Ops, unsigned NumOps);
281 /// SelectNodeTo - These are used for target selectors to *mutate* the
282 /// specified node to have the specified return type, Target opcode, and
283 /// operands. Note that target opcodes are stored as
284 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field. The 0th value
285 /// of the resultant node is returned.
286 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT);
287 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
289 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
290 SDOperand Op1, SDOperand Op2);
291 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
292 SDOperand Op1, SDOperand Op2, SDOperand Op3);
293 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
294 SDOperand Op1, SDOperand Op2, SDOperand Op3,
296 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
297 SDOperand Op1, SDOperand Op2, SDOperand Op3,
298 SDOperand Op4, SDOperand Op5);
299 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
300 SDOperand Op1, SDOperand Op2, SDOperand Op3,
301 SDOperand Op4, SDOperand Op5, SDOperand Op6);
302 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
303 SDOperand Op1, SDOperand Op2, SDOperand Op3,
304 SDOperand Op4, SDOperand Op5, SDOperand Op6,
306 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
307 SDOperand Op1, SDOperand Op2, SDOperand Op3,
308 SDOperand Op4, SDOperand Op5, SDOperand Op6,
309 SDOperand Op7, SDOperand Op8);
310 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
311 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
312 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
313 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
315 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
316 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
317 SDOperand Op3, SDOperand Op4);
318 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
319 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
320 SDOperand Op3, SDOperand Op4, SDOperand Op5);
322 /// getTargetNode - These are used for target selectors to create a new node
323 /// with specified return type(s), target opcode, and operands.
325 /// Note that getTargetNode returns the resultant node. If there is already a
326 /// node of the specified opcode and operands, it returns that node instead of
328 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT);
329 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
331 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
332 SDOperand Op1, SDOperand Op2);
333 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
334 SDOperand Op1, SDOperand Op2, SDOperand Op3);
335 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
336 SDOperand Op1, SDOperand Op2, SDOperand Op3,
338 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
339 SDOperand Op1, SDOperand Op2, SDOperand Op3,
340 SDOperand Op4, SDOperand Op5);
341 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
342 SDOperand Op1, SDOperand Op2, SDOperand Op3,
343 SDOperand Op4, SDOperand Op5, SDOperand Op6);
344 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
345 SDOperand Op1, SDOperand Op2, SDOperand Op3,
346 SDOperand Op4, SDOperand Op5, SDOperand Op6,
348 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
349 SDOperand Op1, SDOperand Op2, SDOperand Op3,
350 SDOperand Op4, SDOperand Op5, SDOperand Op6,
351 SDOperand Op7, SDOperand Op8);
352 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
353 const SDOperand *Ops, unsigned NumOps);
354 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
355 MVT::ValueType VT2, SDOperand Op1);
356 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
357 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
358 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
359 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
361 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
362 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
363 SDOperand Op3, SDOperand Op4);
364 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
365 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
366 SDOperand Op3, SDOperand Op4, SDOperand Op5);
367 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
368 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
369 SDOperand Op3, SDOperand Op4, SDOperand Op5,
371 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
372 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
373 SDOperand Op3, SDOperand Op4, SDOperand Op5,
374 SDOperand Op6, SDOperand Op7);
375 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
376 MVT::ValueType VT2, MVT::ValueType VT3,
377 SDOperand Op1, SDOperand Op2);
378 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
379 MVT::ValueType VT2, MVT::ValueType VT3,
380 SDOperand Op1, SDOperand Op2,
381 SDOperand Op3, SDOperand Op4, SDOperand Op5);
382 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
383 MVT::ValueType VT2, MVT::ValueType VT3,
384 SDOperand Op1, SDOperand Op2,
385 SDOperand Op3, SDOperand Op4, SDOperand Op5,
387 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
388 MVT::ValueType VT2, MVT::ValueType VT3,
389 SDOperand Op1, SDOperand Op2,
390 SDOperand Op3, SDOperand Op4, SDOperand Op5,
391 SDOperand Op6, SDOperand Op7);
392 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
394 const SDOperand *Ops, unsigned NumOps);
396 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
397 /// This can cause recursive merging of nodes in the DAG. Use the first
398 /// version if 'From' is known to have a single result, use the second
399 /// if you have two nodes with identical results, use the third otherwise.
401 /// These methods all take an optional vector, which (if not null) is
402 /// populated with any nodes that are deleted from the SelectionDAG, due to
403 /// new equivalences that are discovered.
405 void ReplaceAllUsesWith(SDOperand From, SDOperand Op,
406 std::vector<SDNode*> *Deleted = 0);
407 void ReplaceAllUsesWith(SDNode *From, SDNode *To,
408 std::vector<SDNode*> *Deleted = 0);
409 void ReplaceAllUsesWith(SDNode *From, const SDOperand *To,
410 std::vector<SDNode*> *Deleted = 0);
412 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
413 /// uses of other values produced by From.Val alone. The Deleted vector is
414 /// handled the same was as for ReplaceAllUsesWith, but it is required for
416 void ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
417 std::vector<SDNode*> &Deleted);
419 /// DeleteNode - Remove the specified node from the system. This node must
420 /// have no referrers.
421 void DeleteNode(SDNode *N);
423 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
424 /// their allnodes order. It returns the maximum id.
425 unsigned AssignNodeIds();
427 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
428 /// based on their topological order. It returns the maximum id and a vector
429 /// of the SDNodes* in assigned order by reference.
430 unsigned AssignTopologicalOrder(std::vector<SDNode*> &TopOrder);
434 /// InsertISelMapEntry - A helper function to insert a key / element pair
435 /// into a SDOperand to SDOperand map. This is added to avoid the map
436 /// insertion operator from being inlined.
437 static void InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
438 SDNode *Key, unsigned KeyResNo,
439 SDNode *Element, unsigned ElementResNo);
442 void RemoveNodeFromCSEMaps(SDNode *N);
443 SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
444 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op, void *&InsertPos);
445 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op1, SDOperand Op2,
447 SDNode *FindModifiedNodeSlot(SDNode *N, const SDOperand *Ops, unsigned NumOps,
450 void DeleteNodeNotInCSEMaps(SDNode *N);
451 MVT::ValueType *getNodeValueTypes(MVT::ValueType VT1);
452 MVT::ValueType *getNodeValueTypes(MVT::ValueType VT1, MVT::ValueType VT2);
453 MVT::ValueType *getNodeValueTypes(std::vector<MVT::ValueType> &RetVals);
456 /// SimplifySetCC - Try to simplify a setcc built with the specified operands
457 /// and cc. If unable to simplify it, return a null SDOperand.
458 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1,
459 SDOperand N2, ISD::CondCode Cond);
461 // List of non-single value types.
462 std::list<std::vector<MVT::ValueType> > VTList;
464 // Maps to auto-CSE operations.
465 std::vector<CondCodeSDNode*> CondCodeNodes;
467 std::vector<SDNode*> ValueTypeNodes;
468 std::map<std::string, SDNode*> ExternalSymbols;
469 std::map<std::string, SDNode*> TargetExternalSymbols;
470 std::map<std::string, StringSDNode*> StringNodes;
473 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
474 typedef SelectionDAG::allnodes_iterator nodes_iterator;
475 static nodes_iterator nodes_begin(SelectionDAG *G) {
476 return G->allnodes_begin();
478 static nodes_iterator nodes_end(SelectionDAG *G) {
479 return G->allnodes_end();
483 } // end namespace llvm