Added support for machine specific constantpool values. These are useful for
[oota-llvm.git] / include / llvm / CodeGen / SelectionDAG.h
1 //===-- llvm/CodeGen/SelectionDAG.h - InstSelection DAG ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the SelectionDAG class, and transitively defines the
11 // SDNode class and subclasses.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CODEGEN_SELECTIONDAG_H
16 #define LLVM_CODEGEN_SELECTIONDAG_H
17
18 #include "llvm/CodeGen/SelectionDAGCSEMap.h"
19 #include "llvm/ADT/ilist"
20
21 #include <list>
22 #include <vector>
23 #include <map>
24 #include <set>
25 #include <string>
26
27 namespace llvm {
28   class TargetLowering;
29   class TargetMachine;
30   class MachineDebugInfo;
31   class MachineFunction;
32   class MachineConstantPoolValue;
33
34 /// SelectionDAG class - This is used to represent a portion of an LLVM function
35 /// in a low-level Data Dependence DAG representation suitable for instruction
36 /// selection.  This DAG is constructed as the first step of instruction
37 /// selection in order to allow implementation of machine specific optimizations
38 /// and code simplifications.
39 ///
40 /// The representation used by the SelectionDAG is a target-independent
41 /// representation, which has some similarities to the GCC RTL representation,
42 /// but is significantly more simple, powerful, and is a graph form instead of a
43 /// linear form.
44 ///
45 class SelectionDAG {
46   TargetLowering &TLI;
47   MachineFunction &MF;
48   MachineDebugInfo *DI;
49
50   /// Root - The root of the entire DAG.  EntryNode - The starting token.
51   SDOperand Root, EntryNode;
52
53   /// AllNodes - A linked list of nodes in the current DAG.
54   ilist<SDNode> AllNodes;
55
56   /// CSEMap - This structure is used to memoize nodes, automatically performing
57   /// CSE with existing nodes with a duplicate is requested.
58   SelectionDAGCSEMap CSEMap;
59
60 public:
61   SelectionDAG(TargetLowering &tli, MachineFunction &mf, MachineDebugInfo *di)
62   : TLI(tli), MF(mf), DI(di) {
63     EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
64   }
65   ~SelectionDAG();
66
67   MachineFunction &getMachineFunction() const { return MF; }
68   const TargetMachine &getTarget() const;
69   TargetLowering &getTargetLoweringInfo() const { return TLI; }
70   MachineDebugInfo *getMachineDebugInfo() const { return DI; }
71
72   /// viewGraph - Pop up a ghostview window with the DAG rendered using 'dot'.
73   ///
74   void viewGraph();
75
76
77   typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
78   allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
79   allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
80   typedef ilist<SDNode>::iterator allnodes_iterator;
81   allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
82   allnodes_iterator allnodes_end() { return AllNodes.end(); }
83   
84   /// getRoot - Return the root tag of the SelectionDAG.
85   ///
86   const SDOperand &getRoot() const { return Root; }
87
88   /// getEntryNode - Return the token chain corresponding to the entry of the
89   /// function.
90   const SDOperand &getEntryNode() const { return EntryNode; }
91
92   /// setRoot - Set the current root tag of the SelectionDAG.
93   ///
94   const SDOperand &setRoot(SDOperand N) { return Root = N; }
95
96   /// Combine - This iterates over the nodes in the SelectionDAG, folding
97   /// certain types of nodes together, or eliminating superfluous nodes.  When
98   /// the AfterLegalize argument is set to 'true', Combine takes care not to
99   /// generate any nodes that will be illegal on the target.
100   void Combine(bool AfterLegalize);
101   
102   /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
103   /// compatible with the target instruction selector, as indicated by the
104   /// TargetLowering object.
105   ///
106   /// Note that this is an involved process that may invalidate pointers into
107   /// the graph.
108   void Legalize();
109
110   /// RemoveDeadNodes - This method deletes all unreachable nodes in the
111   /// SelectionDAG.
112   void RemoveDeadNodes();
113   
114   /// getVTList - Return an SDVTList that represents the list of values
115   /// specified.
116   SDVTList getVTList(MVT::ValueType VT);
117   SDVTList getVTList(MVT::ValueType VT1, MVT::ValueType VT2);
118   SDVTList getVTList(MVT::ValueType VT1, MVT::ValueType VT2,MVT::ValueType VT3);
119   SDVTList getVTList(const MVT::ValueType *VTs, unsigned NumVTs);
120   
121   /// getNodeValueTypes - These are obsolete, use getVTList instead.
122   const MVT::ValueType *getNodeValueTypes(MVT::ValueType VT) {
123     return getVTList(VT).VTs;
124   }
125   const MVT::ValueType *getNodeValueTypes(MVT::ValueType VT1, 
126                                           MVT::ValueType VT2) {
127     return getVTList(VT1, VT2).VTs;
128   }
129   const MVT::ValueType *getNodeValueTypes(MVT::ValueType VT1,MVT::ValueType VT2,
130                                           MVT::ValueType VT3) {
131     return getVTList(VT1, VT2, VT3).VTs;
132   }
133   const MVT::ValueType *getNodeValueTypes(std::vector<MVT::ValueType> &VTList) {
134     return getVTList(&VTList[0], VTList.size()).VTs;
135   }
136   
137   
138   //===--------------------------------------------------------------------===//
139   // Node creation methods.
140   //
141   SDOperand getString(const std::string &Val);
142   SDOperand getConstant(uint64_t Val, MVT::ValueType VT, bool isTarget = false);
143   SDOperand getTargetConstant(uint64_t Val, MVT::ValueType VT) {
144     return getConstant(Val, VT, true);
145   }
146   SDOperand getConstantFP(double Val, MVT::ValueType VT, bool isTarget = false);
147   SDOperand getTargetConstantFP(double Val, MVT::ValueType VT) {
148     return getConstantFP(Val, VT, true);
149   }
150   SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT,
151                              int offset = 0, bool isTargetGA = false);
152   SDOperand getTargetGlobalAddress(const GlobalValue *GV, MVT::ValueType VT,
153                                    int offset = 0) {
154     return getGlobalAddress(GV, VT, offset, true);
155   }
156   SDOperand getFrameIndex(int FI, MVT::ValueType VT, bool isTarget = false);
157   SDOperand getTargetFrameIndex(int FI, MVT::ValueType VT) {
158     return getFrameIndex(FI, VT, true);
159   }
160   SDOperand getJumpTable(int JTI, MVT::ValueType VT, bool isTarget = false);
161   SDOperand getTargetJumpTable(int JTI, MVT::ValueType VT) {
162     return getJumpTable(JTI, VT, true);
163   }
164   SDOperand getConstantPool(Constant *C, MVT::ValueType VT,
165                             unsigned Align = 0, int Offs = 0, bool isT=false);
166   SDOperand getTargetConstantPool(Constant *C, MVT::ValueType VT,
167                                   unsigned Align = 0, int Offset = 0) {
168     return getConstantPool(C, VT, Align, Offset, true);
169   }
170   SDOperand getConstantPool(MachineConstantPoolValue *C, MVT::ValueType VT,
171                             unsigned Align = 0, int Offs = 0, bool isT=false);
172   SDOperand getTargetConstantPool(MachineConstantPoolValue *C,
173                                   MVT::ValueType VT, unsigned Align = 0,
174                                   int Offset = 0) {
175     return getConstantPool(C, VT, Align, Offset, true);
176   }
177   SDOperand getBasicBlock(MachineBasicBlock *MBB);
178   SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
179   SDOperand getTargetExternalSymbol(const char *Sym, MVT::ValueType VT);
180   SDOperand getValueType(MVT::ValueType);
181   SDOperand getRegister(unsigned Reg, MVT::ValueType VT);
182
183   SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
184     return getNode(ISD::CopyToReg, MVT::Other, Chain,
185                    getRegister(Reg, N.getValueType()), N);
186   }
187
188   // This version of the getCopyToReg method takes an extra operand, which
189   // indicates that there is potentially an incoming flag value (if Flag is not
190   // null) and that there should be a flag result.
191   SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N,
192                          SDOperand Flag) {
193     const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
194     SDOperand Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Flag };
195     return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
196   }
197
198   // Similar to last getCopyToReg() except parameter Reg is a SDOperand
199   SDOperand getCopyToReg(SDOperand Chain, SDOperand Reg, SDOperand N,
200                          SDOperand Flag) {
201     const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
202     SDOperand Ops[] = { Chain, Reg, N, Flag };
203     return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
204   }
205   
206   SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
207     const MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
208     SDOperand Ops[] = { Chain, getRegister(Reg, VT) };
209     return getNode(ISD::CopyFromReg, VTs, 2, Ops, 2);
210   }
211   
212   // This version of the getCopyFromReg method takes an extra operand, which
213   // indicates that there is potentially an incoming flag value (if Flag is not
214   // null) and that there should be a flag result.
215   SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT,
216                            SDOperand Flag) {
217     const MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other, MVT::Flag);
218     SDOperand Ops[] = { Chain, getRegister(Reg, VT), Flag };
219     return getNode(ISD::CopyFromReg, VTs, 3, Ops, Flag.Val ? 3 : 2);
220   }
221
222   SDOperand getCondCode(ISD::CondCode Cond);
223
224   /// getZeroExtendInReg - Return the expression required to zero extend the Op
225   /// value assuming it was the smaller SrcTy value.
226   SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
227   
228   /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
229   /// a flag result (to ensure it's not CSE'd).
230   SDOperand getCALLSEQ_START(SDOperand Chain, SDOperand Op) {
231     const MVT::ValueType *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
232     SDOperand Ops[] = { Chain,  Op };
233     return getNode(ISD::CALLSEQ_START, VTs, 2, Ops, 2);
234   }
235
236   /// getNode - Gets or creates the specified node.
237   ///
238   SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
239   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
240   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
241                     SDOperand N1, SDOperand N2);
242   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
243                     SDOperand N1, SDOperand N2, SDOperand N3);
244   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
245                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
246   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
247                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
248                     SDOperand N5);
249   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
250                     const SDOperand *Ops, unsigned NumOps);
251   SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
252                     const SDOperand *Ops, unsigned NumOps);
253   SDOperand getNode(unsigned Opcode, const MVT::ValueType *VTs, unsigned NumVTs,
254                     const SDOperand *Ops, unsigned NumOps);
255   SDOperand getNode(unsigned Opcode, SDVTList VTs,
256                     const SDOperand *Ops, unsigned NumOps);
257   
258   /// getSetCC - Helper function to make it easier to build SetCC's if you just
259   /// have an ISD::CondCode instead of an SDOperand.
260   ///
261   SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS,
262                      ISD::CondCode Cond) {
263     return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
264   }
265
266   /// getSelectCC - Helper function to make it easier to build SelectCC's if you
267   /// just have an ISD::CondCode instead of an SDOperand.
268   ///
269   SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
270                         SDOperand True, SDOperand False, ISD::CondCode Cond) {
271     return getNode(ISD::SELECT_CC, True.getValueType(), LHS, RHS, True, False,
272                    getCondCode(Cond));
273   }
274   
275   /// getVAArg - VAArg produces a result and token chain, and takes a pointer
276   /// and a source value as input.
277   SDOperand getVAArg(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
278                      SDOperand SV);
279
280   /// getLoad - Loads are not normal binary operators: their result type is not
281   /// determined by their operands, and they produce a value AND a token chain.
282   ///
283   SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
284                     SDOperand SV);
285   SDOperand getVecLoad(unsigned Count, MVT::ValueType VT, SDOperand Chain, 
286                        SDOperand Ptr, SDOperand SV);
287   SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain,
288                        SDOperand Ptr, SDOperand SV, MVT::ValueType EVT);
289
290   // getSrcValue - construct a node to track a Value* through the backend
291   SDOperand getSrcValue(const Value* I, int offset = 0);
292
293   /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
294   /// specified operands.  If the resultant node already exists in the DAG,
295   /// this does not modify the specified node, instead it returns the node that
296   /// already exists.  If the resultant node does not exist in the DAG, the
297   /// input node is returned.  As a degenerate case, if you specify the same
298   /// input operands as the node already has, the input node is returned.
299   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op);
300   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2);
301   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
302                                SDOperand Op3);
303   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
304                                SDOperand Op3, SDOperand Op4);
305   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
306                                SDOperand Op3, SDOperand Op4, SDOperand Op5);
307   SDOperand UpdateNodeOperands(SDOperand N, SDOperand *Ops, unsigned NumOps);
308   
309   /// SelectNodeTo - These are used for target selectors to *mutate* the
310   /// specified node to have the specified return type, Target opcode, and
311   /// operands.  Note that target opcodes are stored as
312   /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.  The 0th value
313   /// of the resultant node is returned.
314   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT);
315   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
316                        SDOperand Op1);
317   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
318                        SDOperand Op1, SDOperand Op2);
319   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
320                        SDOperand Op1, SDOperand Op2, SDOperand Op3);
321   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
322                         const SDOperand *Ops, unsigned NumOps);
323   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1, 
324                        MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
325   SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
326                        MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
327                        SDOperand Op3);
328
329
330   /// getTargetNode - These are used for target selectors to create a new node
331   /// with specified return type(s), target opcode, and operands.
332   ///
333   /// Note that getTargetNode returns the resultant node.  If there is already a
334   /// node of the specified opcode and operands, it returns that node instead of
335   /// the current one.
336   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT);
337   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
338                         SDOperand Op1);
339   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
340                         SDOperand Op1, SDOperand Op2);
341   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT,
342                         SDOperand Op1, SDOperand Op2, SDOperand Op3);
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,
351                         SDOperand Op3);
352   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1, 
353                         MVT::ValueType VT2,
354                         const SDOperand *Ops, unsigned NumOps);
355   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1,
356                         MVT::ValueType VT2, MVT::ValueType VT3,
357                         SDOperand Op1, SDOperand Op2);
358   SDNode *getTargetNode(unsigned Opcode, MVT::ValueType VT1, 
359                         MVT::ValueType VT2, MVT::ValueType VT3,
360                         const SDOperand *Ops, unsigned NumOps);
361   
362   /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
363   /// This can cause recursive merging of nodes in the DAG.  Use the first
364   /// version if 'From' is known to have a single result, use the second
365   /// if you have two nodes with identical results, use the third otherwise.
366   ///
367   /// These methods all take an optional vector, which (if not null) is 
368   /// populated with any nodes that are deleted from the SelectionDAG, due to
369   /// new equivalences that are discovered.
370   ///
371   void ReplaceAllUsesWith(SDOperand From, SDOperand Op,
372                           std::vector<SDNode*> *Deleted = 0);
373   void ReplaceAllUsesWith(SDNode *From, SDNode *To,
374                           std::vector<SDNode*> *Deleted = 0);
375   void ReplaceAllUsesWith(SDNode *From, const SDOperand *To,
376                           std::vector<SDNode*> *Deleted = 0);
377
378   /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
379   /// uses of other values produced by From.Val alone.  The Deleted vector is
380   /// handled the same was as for ReplaceAllUsesWith, but it is required for
381   /// this method.
382   void ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
383                                  std::vector<SDNode*> &Deleted);
384
385   /// DeleteNode - Remove the specified node from the system.  This node must
386   /// have no referrers.
387   void DeleteNode(SDNode *N);
388
389   /// AssignNodeIds - Assign a unique node id for each node in the DAG based on
390   /// their allnodes order. It returns the maximum id.
391   unsigned AssignNodeIds();
392
393   /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
394   /// based on their topological order. It returns the maximum id and a vector
395   /// of the SDNodes* in assigned order by reference.
396   unsigned AssignTopologicalOrder(std::vector<SDNode*> &TopOrder);
397
398   /// isCommutativeBinOp - Returns true if the opcode is a commutative binary
399   /// operation.
400   static bool isCommutativeBinOp(unsigned Opcode) {
401     switch (Opcode) {
402     case ISD::ADD:
403     case ISD::MUL:
404     case ISD::MULHU:
405     case ISD::MULHS:
406     case ISD::FADD:
407     case ISD::FMUL:
408     case ISD::AND:
409     case ISD::OR:
410     case ISD::XOR:
411     case ISD::ADDC: 
412     case ISD::ADDE: return true;
413     default: return false;
414     }
415   }
416
417   void dump() const;
418
419 private:
420   void RemoveNodeFromCSEMaps(SDNode *N);
421   SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
422   SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op, void *&InsertPos);
423   SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op1, SDOperand Op2,
424                                void *&InsertPos);
425   SDNode *FindModifiedNodeSlot(SDNode *N, const SDOperand *Ops, unsigned NumOps,
426                                void *&InsertPos);
427
428   void DeleteNodeNotInCSEMaps(SDNode *N);
429   
430   /// SimplifySetCC - Try to simplify a setcc built with the specified operands 
431   /// and cc.  If unable to simplify it, return a null SDOperand.
432   SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1,
433                           SDOperand N2, ISD::CondCode Cond);
434   
435   // List of non-single value types.
436   std::list<std::vector<MVT::ValueType> > VTList;
437   
438   // Maps to auto-CSE operations.
439   std::vector<CondCodeSDNode*> CondCodeNodes;
440
441   std::vector<SDNode*> ValueTypeNodes;
442   std::map<std::string, SDNode*> ExternalSymbols;
443   std::map<std::string, SDNode*> TargetExternalSymbols;
444   std::map<std::string, StringSDNode*> StringNodes;
445 };
446
447 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
448   typedef SelectionDAG::allnodes_iterator nodes_iterator;
449   static nodes_iterator nodes_begin(SelectionDAG *G) {
450     return G->allnodes_begin();
451   }
452   static nodes_iterator nodes_end(SelectionDAG *G) {
453     return G->allnodes_end();
454   }
455 };
456
457 }  // end namespace llvm
458
459 #endif