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 const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
159 SDOperand Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Flag };
160 return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
163 // Similar to last getCopyToReg() except parameter Reg is a SDOperand
164 SDOperand getCopyToReg(SDOperand Chain, SDOperand Reg, SDOperand N,
166 const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
167 SDOperand Ops[] = { Chain, Reg, N, Flag };
168 return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
171 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
172 const MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
173 SDOperand Ops[] = { Chain, getRegister(Reg, VT) };
174 return getNode(ISD::CopyFromReg, VTs, 2, Ops, 2);
177 // This version of the getCopyFromReg method takes an extra operand, which
178 // indicates that there is potentially an incoming flag value (if Flag is not
179 // null) and that there should be a flag result.
180 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT,
182 const MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other, MVT::Flag);
183 SDOperand Ops[] = { Chain, getRegister(Reg, VT), Flag };
184 return getNode(ISD::CopyFromReg, VTs, 3, Ops, Flag.Val ? 3 : 2);
187 SDOperand getCondCode(ISD::CondCode Cond);
189 /// getZeroExtendInReg - Return the expression required to zero extend the Op
190 /// value assuming it was the smaller SrcTy value.
191 SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
193 /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
194 /// a flag result (to ensure it's not CSE'd).
195 SDOperand getCALLSEQ_START(SDOperand Chain, SDOperand Op) {
196 const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
197 SDOperand Ops[] = { Chain, Op };
198 return getNode(ISD::CALLSEQ_START, VTs, 2, Ops, 2);
201 /// getNode - Gets or creates the specified node.
203 SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
204 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
205 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
206 SDOperand N1, SDOperand N2);
207 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
208 SDOperand N1, SDOperand N2, SDOperand N3);
209 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
210 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
211 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
212 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
214 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
215 const SDOperand *Ops, unsigned NumOps);
216 SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
217 const SDOperand *Ops, unsigned NumOps);
218 SDOperand getNode(unsigned Opcode, const MVT::ValueType *VTs, unsigned NumVTs,
219 const SDOperand *Ops, unsigned NumOps);
221 /// getSetCC - Helper function to make it easier to build SetCC's if you just
222 /// have an ISD::CondCode instead of an SDOperand.
224 SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS,
225 ISD::CondCode Cond) {
226 return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
229 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
230 /// just have an ISD::CondCode instead of an SDOperand.
232 SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
233 SDOperand True, SDOperand False, ISD::CondCode Cond) {
234 return getNode(ISD::SELECT_CC, True.getValueType(), LHS, RHS, True, False,
238 /// getVAArg - VAArg produces a result and token chain, and takes a pointer
239 /// and a source value as input.
240 SDOperand getVAArg(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
243 /// getLoad - Loads are not normal binary operators: their result type is not
244 /// determined by their operands, and they produce a value AND a token chain.
246 SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
248 SDOperand getVecLoad(unsigned Count, MVT::ValueType VT, SDOperand Chain,
249 SDOperand Ptr, SDOperand SV);
250 SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain,
251 SDOperand Ptr, SDOperand SV, MVT::ValueType EVT);
253 // getSrcValue - construct a node to track a Value* through the backend
254 SDOperand getSrcValue(const Value* I, int offset = 0);
256 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
257 /// specified operands. If the resultant node already exists in the DAG,
258 /// this does not modify the specified node, instead it returns the node that
259 /// already exists. If the resultant node does not exist in the DAG, the
260 /// input node is returned. As a degenerate case, if you specify the same
261 /// input operands as the node already has, the input node is returned.
262 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op);
263 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2);
264 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
266 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
267 SDOperand Op3, SDOperand Op4);
268 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
269 SDOperand Op3, SDOperand Op4, SDOperand Op5);
270 SDOperand UpdateNodeOperands(SDOperand N, SDOperand *Ops, unsigned NumOps);
272 /// SelectNodeTo - These are used for target selectors to *mutate* the
273 /// specified node to have the specified return type, Target opcode, and
274 /// operands. Note that target opcodes are stored as
275 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field. The 0th value
276 /// of the resultant node is returned.
277 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT);
278 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
280 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
281 SDOperand Op1, SDOperand Op2);
282 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
283 SDOperand Op1, SDOperand Op2, SDOperand Op3);
284 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
285 SDOperand Op1, SDOperand Op2, SDOperand Op3,
287 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
288 SDOperand Op1, SDOperand Op2, SDOperand Op3,
289 SDOperand Op4, SDOperand Op5);
290 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
291 SDOperand Op1, SDOperand Op2, SDOperand Op3,
292 SDOperand Op4, SDOperand Op5, SDOperand Op6);
293 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
294 SDOperand Op1, SDOperand Op2, SDOperand Op3,
295 SDOperand Op4, SDOperand Op5, SDOperand Op6,
297 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
298 SDOperand Op1, SDOperand Op2, SDOperand Op3,
299 SDOperand Op4, SDOperand Op5, SDOperand Op6,
300 SDOperand Op7, SDOperand Op8);
301 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
302 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
303 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
304 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
306 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
307 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
308 SDOperand Op3, SDOperand Op4);
309 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
310 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
311 SDOperand Op3, SDOperand Op4, SDOperand Op5);
313 /// getTargetNode - These are used for target selectors to create a new node
314 /// with specified return type(s), target opcode, and operands.
316 /// Note that getTargetNode returns the resultant node. If there is already a
317 /// node of the specified opcode and operands, it returns that node instead of
319 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT);
320 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
322 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
323 SDOperand Op1, SDOperand Op2);
324 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
325 SDOperand Op1, SDOperand Op2, SDOperand Op3);
326 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
327 SDOperand Op1, SDOperand Op2, SDOperand Op3,
329 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
330 SDOperand Op1, SDOperand Op2, SDOperand Op3,
331 SDOperand Op4, SDOperand Op5);
332 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
333 SDOperand Op1, SDOperand Op2, SDOperand Op3,
334 SDOperand Op4, SDOperand Op5, SDOperand Op6);
335 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
336 SDOperand Op1, SDOperand Op2, SDOperand Op3,
337 SDOperand Op4, SDOperand Op5, SDOperand Op6,
339 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
340 SDOperand Op1, SDOperand Op2, SDOperand Op3,
341 SDOperand Op4, SDOperand Op5, SDOperand Op6,
342 SDOperand Op7, SDOperand Op8);
343 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
344 const SDOperand *Ops, unsigned NumOps);
345 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
346 MVT::ValueType VT2, SDOperand Op1);
347 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
348 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
349 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
350 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
352 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
353 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
354 SDOperand Op3, SDOperand Op4);
355 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
356 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
357 SDOperand Op3, SDOperand Op4, SDOperand Op5);
358 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
359 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
360 SDOperand Op3, SDOperand Op4, SDOperand Op5,
362 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
363 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
364 SDOperand Op3, SDOperand Op4, SDOperand Op5,
365 SDOperand Op6, SDOperand Op7);
366 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
367 MVT::ValueType VT2, MVT::ValueType VT3,
368 SDOperand Op1, SDOperand Op2);
369 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
370 MVT::ValueType VT2, MVT::ValueType VT3,
371 SDOperand Op1, SDOperand Op2,
372 SDOperand Op3, SDOperand Op4, SDOperand Op5);
373 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
374 MVT::ValueType VT2, MVT::ValueType VT3,
375 SDOperand Op1, SDOperand Op2,
376 SDOperand Op3, SDOperand Op4, SDOperand Op5,
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 SDOperand Op6, SDOperand Op7);
383 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
385 const SDOperand *Ops, unsigned NumOps);
387 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
388 /// This can cause recursive merging of nodes in the DAG. Use the first
389 /// version if 'From' is known to have a single result, use the second
390 /// if you have two nodes with identical results, use the third otherwise.
392 /// These methods all take an optional vector, which (if not null) is
393 /// populated with any nodes that are deleted from the SelectionDAG, due to
394 /// new equivalences that are discovered.
396 void ReplaceAllUsesWith(SDOperand From, SDOperand Op,
397 std::vector<SDNode*> *Deleted = 0);
398 void ReplaceAllUsesWith(SDNode *From, SDNode *To,
399 std::vector<SDNode*> *Deleted = 0);
400 void ReplaceAllUsesWith(SDNode *From, const SDOperand *To,
401 std::vector<SDNode*> *Deleted = 0);
403 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
404 /// uses of other values produced by From.Val alone. The Deleted vector is
405 /// handled the same was as for ReplaceAllUsesWith, but it is required for
407 void ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
408 std::vector<SDNode*> &Deleted);
410 /// DeleteNode - Remove the specified node from the system. This node must
411 /// have no referrers.
412 void DeleteNode(SDNode *N);
414 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
415 /// their allnodes order. It returns the maximum id.
416 unsigned AssignNodeIds();
418 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
419 /// based on their topological order. It returns the maximum id and a vector
420 /// of the SDNodes* in assigned order by reference.
421 unsigned AssignTopologicalOrder(std::vector<SDNode*> &TopOrder);
425 /// getNodeValueTypes - Return a pointer to an intern'd representation of the
426 /// specified valuetype list.
427 MVT::ValueType *getNodeValueTypes(MVT::ValueType VT1);
428 MVT::ValueType *getNodeValueTypes(MVT::ValueType VT1, MVT::ValueType VT2);
429 MVT::ValueType *getNodeValueTypes(MVT::ValueType VT1, MVT::ValueType VT2,
431 MVT::ValueType *getNodeValueTypes(std::vector<MVT::ValueType> &RetVals);
434 void RemoveNodeFromCSEMaps(SDNode *N);
435 SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
436 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op, void *&InsertPos);
437 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op1, SDOperand Op2,
439 SDNode *FindModifiedNodeSlot(SDNode *N, const SDOperand *Ops, unsigned NumOps,
442 void DeleteNodeNotInCSEMaps(SDNode *N);
444 /// SimplifySetCC - Try to simplify a setcc built with the specified operands
445 /// and cc. If unable to simplify it, return a null SDOperand.
446 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1,
447 SDOperand N2, ISD::CondCode Cond);
449 // List of non-single value types.
450 std::list<std::vector<MVT::ValueType> > VTList;
452 // Maps to auto-CSE operations.
453 std::vector<CondCodeSDNode*> CondCodeNodes;
455 std::vector<SDNode*> ValueTypeNodes;
456 std::map<std::string, SDNode*> ExternalSymbols;
457 std::map<std::string, SDNode*> TargetExternalSymbols;
458 std::map<std::string, StringSDNode*> StringNodes;
461 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
462 typedef SelectionDAG::allnodes_iterator nodes_iterator;
463 static nodes_iterator nodes_begin(SelectionDAG *G) {
464 return G->allnodes_begin();
466 static nodes_iterator nodes_end(SelectionDAG *G) {
467 return G->allnodes_end();
471 } // end namespace llvm