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;
56 SelectionDAG(TargetLowering &tli, MachineFunction &mf, MachineDebugInfo *di)
57 : TLI(tli), MF(mf), DI(di) {
58 EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
62 MachineFunction &getMachineFunction() const { return MF; }
63 const TargetMachine &getTarget() const;
64 TargetLowering &getTargetLoweringInfo() const { return TLI; }
65 MachineDebugInfo *getMachineDebugInfo() const { return DI; }
67 /// viewGraph - Pop up a ghostview window with the DAG rendered using 'dot'.
72 typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
73 allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
74 allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
75 typedef ilist<SDNode>::iterator allnodes_iterator;
76 allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
77 allnodes_iterator allnodes_end() { return AllNodes.end(); }
79 /// getRoot - Return the root tag of the SelectionDAG.
81 const SDOperand &getRoot() const { return Root; }
83 /// getEntryNode - Return the token chain corresponding to the entry of the
85 const SDOperand &getEntryNode() const { return EntryNode; }
87 /// setRoot - Set the current root tag of the SelectionDAG.
89 const SDOperand &setRoot(SDOperand N) { return Root = N; }
91 /// Combine - This iterates over the nodes in the SelectionDAG, folding
92 /// certain types of nodes together, or eliminating superfluous nodes. When
93 /// the AfterLegalize argument is set to 'true', Combine takes care not to
94 /// generate any nodes that will be illegal on the target.
95 void Combine(bool AfterLegalize);
97 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
98 /// compatible with the target instruction selector, as indicated by the
99 /// TargetLowering object.
101 /// Note that this is an involved process that may invalidate pointers into
105 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
107 void RemoveDeadNodes();
109 SDOperand getString(const std::string &Val);
110 SDOperand getConstant(uint64_t Val, MVT::ValueType VT, bool isTarget = false);
111 SDOperand getTargetConstant(uint64_t Val, MVT::ValueType VT) {
112 return getConstant(Val, VT, true);
114 SDOperand getConstantFP(double Val, MVT::ValueType VT, bool isTarget = false);
115 SDOperand getTargetConstantFP(double Val, MVT::ValueType VT) {
116 return getConstantFP(Val, VT, true);
118 SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT,
119 int offset = 0, bool isTargetGA = false);
120 SDOperand getTargetGlobalAddress(const GlobalValue *GV, MVT::ValueType VT,
122 return getGlobalAddress(GV, VT, offset, true);
124 SDOperand getFrameIndex(int FI, MVT::ValueType VT, bool isTarget = false);
125 SDOperand getTargetFrameIndex(int FI, MVT::ValueType VT) {
126 return getFrameIndex(FI, VT, true);
128 SDOperand getJumpTable(int JTI, MVT::ValueType VT, bool isTarget = false);
129 SDOperand getTargetJumpTable(int JTI, MVT::ValueType VT) {
130 return getJumpTable(JTI, VT, true);
132 SDOperand getConstantPool(Constant *C, MVT::ValueType VT,
133 unsigned Align = 0, int Offs = 0, bool isT=false);
134 SDOperand getTargetConstantPool(Constant *C, MVT::ValueType VT,
135 unsigned Align = 0, int Offset = 0) {
136 return getConstantPool(C, VT, Align, Offset, true);
138 SDOperand getBasicBlock(MachineBasicBlock *MBB);
139 SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
140 SDOperand getTargetExternalSymbol(const char *Sym, MVT::ValueType VT);
141 SDOperand getValueType(MVT::ValueType);
142 SDOperand getRegister(unsigned Reg, MVT::ValueType VT);
144 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
145 return getNode(ISD::CopyToReg, MVT::Other, Chain,
146 getRegister(Reg, N.getValueType()), N);
149 // This version of the getCopyToReg method takes an extra operand, which
150 // indicates that there is potentially an incoming flag value (if Flag is not
151 // null) and that there should be a flag result.
152 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N,
154 std::vector<MVT::ValueType> VTs;
155 VTs.push_back(MVT::Other);
156 VTs.push_back(MVT::Flag);
157 SDOperand Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Flag };
158 return getNode(ISD::CopyToReg, VTs, Ops, Flag.Val ? 4 : 3);
161 // Similar to last getCopyToReg() except parameter Reg is a SDOperand
162 SDOperand getCopyToReg(SDOperand Chain, SDOperand Reg, SDOperand N,
164 std::vector<MVT::ValueType> VTs;
165 VTs.push_back(MVT::Other);
166 VTs.push_back(MVT::Flag);
167 SDOperand Ops[] = { Chain, Reg, N, Flag };
168 return getNode(ISD::CopyToReg, VTs, Ops, Flag.Val ? 4 : 3);
171 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
172 std::vector<MVT::ValueType> ResultTys;
173 ResultTys.push_back(VT);
174 ResultTys.push_back(MVT::Other);
175 SDOperand Ops[] = { Chain, getRegister(Reg, VT) };
176 return getNode(ISD::CopyFromReg, ResultTys, Ops, 2);
179 // This version of the getCopyFromReg method takes an extra operand, which
180 // indicates that there is potentially an incoming flag value (if Flag is not
181 // null) and that there should be a flag result.
182 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT,
184 std::vector<MVT::ValueType> ResultTys;
185 ResultTys.push_back(VT);
186 ResultTys.push_back(MVT::Other);
187 ResultTys.push_back(MVT::Flag);
188 SDOperand Ops[] = { Chain, getRegister(Reg, VT), Flag };
189 return getNode(ISD::CopyFromReg, ResultTys, Ops, Flag.Val ? 3 : 2);
192 SDOperand getCondCode(ISD::CondCode Cond);
194 /// getZeroExtendInReg - Return the expression required to zero extend the Op
195 /// value assuming it was the smaller SrcTy value.
196 SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
198 /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
199 /// a flag result (to ensure it's not CSE'd).
200 SDOperand getCALLSEQ_START(SDOperand Chain, SDOperand Op) {
201 std::vector<MVT::ValueType> ResultTys;
202 ResultTys.push_back(MVT::Other);
203 ResultTys.push_back(MVT::Flag);
204 SDOperand Ops[] = { Chain, Op };
205 return getNode(ISD::CALLSEQ_START, ResultTys, Ops, 2);
208 /// getNode - Gets or creates the specified node.
210 SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
211 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
212 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
213 SDOperand N1, SDOperand N2);
214 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
215 SDOperand N1, SDOperand N2, SDOperand N3);
216 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
217 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
218 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
219 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
221 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
222 const SDOperand *Ops, unsigned NumOps);
223 SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
224 const SDOperand *Ops, unsigned NumOps);
226 /// getSetCC - Helper function to make it easier to build SetCC's if you just
227 /// have an ISD::CondCode instead of an SDOperand.
229 SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS,
230 ISD::CondCode Cond) {
231 return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
234 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
235 /// just have an ISD::CondCode instead of an SDOperand.
237 SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
238 SDOperand True, SDOperand False, ISD::CondCode Cond) {
239 MVT::ValueType VT = True.getValueType();
240 return getNode(ISD::SELECT_CC, VT, LHS, RHS, True, False,getCondCode(Cond));
243 /// getVAArg - VAArg produces a result and token chain, and takes a pointer
244 /// and a source value as input.
245 SDOperand getVAArg(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
248 /// getLoad - Loads are not normal binary operators: their result type is not
249 /// determined by their operands, and they produce a value AND a token chain.
251 SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
253 SDOperand getVecLoad(unsigned Count, MVT::ValueType VT, SDOperand Chain,
254 SDOperand Ptr, SDOperand SV);
255 SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain,
256 SDOperand Ptr, SDOperand SV, MVT::ValueType EVT);
258 // getSrcValue - construct a node to track a Value* through the backend
259 SDOperand getSrcValue(const Value* I, int offset = 0);
261 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
262 /// specified operands. If the resultant node already exists in the DAG,
263 /// this does not modify the specified node, instead it returns the node that
264 /// already exists. If the resultant node does not exist in the DAG, the
265 /// input node is returned. As a degenerate case, if you specify the same
266 /// input operands as the node already has, the input node is returned.
267 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op);
268 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2);
269 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
271 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
272 SDOperand Op3, SDOperand Op4);
273 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
274 SDOperand Op3, SDOperand Op4, SDOperand Op5);
275 SDOperand UpdateNodeOperands(SDOperand N, SDOperand *Ops, unsigned NumOps);
277 /// SelectNodeTo - These are used for target selectors to *mutate* the
278 /// specified node to have the specified return type, Target opcode, and
279 /// operands. Note that target opcodes are stored as
280 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field. The 0th value
281 /// of the resultant node is returned.
282 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT);
283 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
285 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
286 SDOperand Op1, SDOperand Op2);
287 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
288 SDOperand Op1, SDOperand Op2, SDOperand Op3);
289 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
290 SDOperand Op1, SDOperand Op2, SDOperand Op3,
292 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
293 SDOperand Op1, SDOperand Op2, SDOperand Op3,
294 SDOperand Op4, SDOperand Op5);
295 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
296 SDOperand Op1, SDOperand Op2, SDOperand Op3,
297 SDOperand Op4, SDOperand Op5, SDOperand Op6);
298 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
299 SDOperand Op1, SDOperand Op2, SDOperand Op3,
300 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,
305 SDOperand Op7, SDOperand Op8);
306 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
307 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
308 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
309 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
311 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
312 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
313 SDOperand Op3, SDOperand Op4);
314 SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
315 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
316 SDOperand Op3, SDOperand Op4, SDOperand Op5);
318 /// getTargetNode - These are used for target selectors to create a new node
319 /// with specified return type(s), target opcode, and operands.
321 /// Note that getTargetNode returns the resultant node. If there is already a
322 /// node of the specified opcode and operands, it returns that node instead of
324 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT);
325 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
327 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
328 SDOperand Op1, SDOperand Op2);
329 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
330 SDOperand Op1, SDOperand Op2, SDOperand Op3);
331 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
332 SDOperand Op1, SDOperand Op2, SDOperand Op3,
334 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
335 SDOperand Op1, SDOperand Op2, SDOperand Op3,
336 SDOperand Op4, SDOperand Op5);
337 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
338 SDOperand Op1, SDOperand Op2, SDOperand Op3,
339 SDOperand Op4, SDOperand Op5, SDOperand Op6);
340 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
341 SDOperand Op1, SDOperand Op2, SDOperand Op3,
342 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,
347 SDOperand Op7, SDOperand Op8);
348 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
349 const SDOperand *Ops, unsigned NumOps);
350 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
351 MVT::ValueType VT2, SDOperand Op1);
352 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
353 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
354 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
355 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
357 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
358 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
359 SDOperand Op3, SDOperand Op4);
360 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
361 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
362 SDOperand Op3, SDOperand Op4, SDOperand Op5);
363 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
364 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
365 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,
370 SDOperand Op6, SDOperand Op7);
371 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
372 MVT::ValueType VT2, MVT::ValueType VT3,
373 SDOperand Op1, SDOperand Op2);
374 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
375 MVT::ValueType VT2, MVT::ValueType VT3,
376 SDOperand Op1, SDOperand Op2,
377 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,
383 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
384 MVT::ValueType VT2, MVT::ValueType VT3,
385 SDOperand Op1, SDOperand Op2,
386 SDOperand Op3, SDOperand Op4, SDOperand Op5,
387 SDOperand Op6, SDOperand Op7);
388 SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
390 const SDOperand *Ops, unsigned NumOps);
392 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
393 /// This can cause recursive merging of nodes in the DAG. Use the first
394 /// version if 'From' is known to have a single result, use the second
395 /// if you have two nodes with identical results, use the third otherwise.
397 /// These methods all take an optional vector, which (if not null) is
398 /// populated with any nodes that are deleted from the SelectionDAG, due to
399 /// new equivalences that are discovered.
401 void ReplaceAllUsesWith(SDOperand From, SDOperand Op,
402 std::vector<SDNode*> *Deleted = 0);
403 void ReplaceAllUsesWith(SDNode *From, SDNode *To,
404 std::vector<SDNode*> *Deleted = 0);
405 void ReplaceAllUsesWith(SDNode *From, const SDOperand *To,
406 std::vector<SDNode*> *Deleted = 0);
408 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
409 /// uses of other values produced by From.Val alone. The Deleted vector is
410 /// handled the same was as for ReplaceAllUsesWith, but it is required for
412 void ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
413 std::vector<SDNode*> &Deleted);
415 /// DeleteNode - Remove the specified node from the system. This node must
416 /// have no referrers.
417 void DeleteNode(SDNode *N);
419 /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
420 /// their allnodes order. It returns the maximum id.
421 unsigned AssignNodeIds();
423 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
424 /// based on their topological order. It returns the maximum id and a vector
425 /// of the SDNodes* in assigned order by reference.
426 unsigned AssignTopologicalOrder(std::vector<SDNode*> &TopOrder);
430 /// InsertISelMapEntry - A helper function to insert a key / element pair
431 /// into a SDOperand to SDOperand map. This is added to avoid the map
432 /// insertion operator from being inlined.
433 static void InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
434 SDNode *Key, unsigned KeyResNo,
435 SDNode *Element, unsigned ElementResNo);
438 void RemoveNodeFromCSEMaps(SDNode *N);
439 SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
440 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op, void *&InsertPos);
441 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op1, SDOperand Op2,
443 SDNode *FindModifiedNodeSlot(SDNode *N, const SDOperand *Ops, unsigned NumOps,
446 void DeleteNodeNotInCSEMaps(SDNode *N);
447 MVT::ValueType *getNodeValueTypes(MVT::ValueType VT1);
448 MVT::ValueType *getNodeValueTypes(MVT::ValueType VT1, MVT::ValueType VT2);
449 MVT::ValueType *getNodeValueTypes(std::vector<MVT::ValueType> &RetVals);
452 /// SimplifySetCC - Try to simplify a setcc built with the specified operands
453 /// and cc. If unable to simplify it, return a null SDOperand.
454 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1,
455 SDOperand N2, ISD::CondCode Cond);
457 // List of non-single value types.
458 std::list<std::vector<MVT::ValueType> > VTList;
460 // Maps to auto-CSE operations.
461 std::vector<CondCodeSDNode*> CondCodeNodes;
463 std::vector<SDNode*> ValueTypeNodes;
464 std::map<std::string, SDNode*> ExternalSymbols;
465 std::map<std::string, SDNode*> TargetExternalSymbols;
466 std::map<std::string, StringSDNode*> StringNodes;
467 SelectionDAGCSEMap CSEMap;
470 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
471 typedef SelectionDAG::allnodes_iterator nodes_iterator;
472 static nodes_iterator nodes_begin(SelectionDAG *G) {
473 return G->allnodes_begin();
475 static nodes_iterator nodes_end(SelectionDAG *G) {
476 return G->allnodes_end();
480 } // end namespace llvm