4bb1739ec12cc9a194e9ad70f304c9ed572e8955
[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/SelectionDAGNodes.h"
19 #include <map>
20 #include <string> // FIXME remove eventually, turning map into const char* map.
21
22 namespace llvm {
23   class TargetLowering;
24   class TargetMachine;
25   class MachineFunction;
26
27 /// SelectionDAG class - This is used to represent a portion of an LLVM function
28 /// in a low-level Data Dependence DAG representation suitable for instruction
29 /// selection.  This DAG is constructed as the first step of instruction
30 /// selection in order to allow implementation of machine specific optimizations
31 /// and code simplifications.
32 ///
33 /// The representation used by the SelectionDAG is a target-independent
34 /// representation, which has some similarities to the GCC RTL representation,
35 /// but is significantly more simple, powerful, and is a graph form instead of a
36 /// linear form.
37 ///
38 class SelectionDAG {
39   TargetLowering &TLI;
40   MachineFunction &MF;
41
42   // Root - The root of the entire DAG.  EntryNode - The starting token.
43   SDOperand Root, EntryNode;
44
45   // AllNodes - All of the nodes in the DAG
46   std::vector<SDNode*> AllNodes;
47 public:
48   SelectionDAG(TargetLowering &tli, MachineFunction &mf) : TLI(tli), MF(mf) {
49     EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
50   }
51   ~SelectionDAG();
52
53   MachineFunction &getMachineFunction() const { return MF; }
54   const TargetMachine &getTarget() const;
55   TargetLowering &getTargetLoweringInfo() const { return TLI; }
56
57   /// viewGraph - Pop up a ghostview window with the DAG rendered using 'dot'.
58   ///
59   void viewGraph();
60
61
62   typedef std::vector<SDNode*>::const_iterator allnodes_iterator;
63   allnodes_iterator allnodes_begin() const { return AllNodes.begin(); }
64   allnodes_iterator allnodes_end() const { return AllNodes.end(); }
65   
66   /// getRoot - Return the root tag of the SelectionDAG.
67   ///
68   const SDOperand &getRoot() const { return Root; }
69
70   /// getEntryNode - Return the token chain corresponding to the entry of the
71   /// function.
72   const SDOperand &getEntryNode() const { return EntryNode; }
73
74   /// setRoot - Set the current root tag of the SelectionDAG.
75   ///
76   const SDOperand &setRoot(SDOperand N) { return Root = N; }
77
78   /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
79   /// compatible with the target instruction selector, as indicated by the
80   /// TargetLowering object.
81   ///
82   /// Note that this is an involved process that may invalidate pointers into
83   /// the graph.
84   void Legalize();
85
86   /// RemoveDeadNodes - This method deletes all unreachable nodes in the
87   /// SelectionDAG, including nodes (like loads) that have uses of their token
88   /// chain but no other uses and no side effect.  If a node is passed in as an
89   /// argument, it is used as the seed for node deletion.
90   void RemoveDeadNodes(SDNode *N = 0);
91
92   SDOperand getConstant(uint64_t Val, MVT::ValueType VT);
93   SDOperand getConstantFP(double Val, MVT::ValueType VT);
94   SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT);
95   SDOperand getFrameIndex(int FI, MVT::ValueType VT);
96   SDOperand getConstantPool(unsigned CPIdx, MVT::ValueType VT);
97   SDOperand getBasicBlock(MachineBasicBlock *MBB);
98   SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
99
100   SDOperand getCopyToReg(SDOperand Chain, SDOperand N, unsigned Reg) {
101     // Note: these are auto-CSE'd because the caller doesn't make requests that
102     // could cause duplicates to occur.
103     SDNode *NN = new RegSDNode(ISD::CopyToReg, Chain, N, Reg);
104     NN->setValueTypes(MVT::Other);
105     AllNodes.push_back(NN);
106     return SDOperand(NN, 0);
107   }
108
109   SDOperand getCopyFromReg(unsigned Reg, MVT::ValueType VT, SDOperand Chain) {
110     // Note: These nodes are auto-CSE'd by the caller of this method.
111     SDNode *NN = new RegSDNode(ISD::CopyFromReg, Chain, Reg);
112     NN->setValueTypes(VT, MVT::Other);
113     AllNodes.push_back(NN);
114     return SDOperand(NN, 0);
115   }
116
117   SDOperand getImplicitDef(SDOperand Chain, unsigned Reg) {
118     // Note: These nodes are auto-CSE'd by the caller of this method.
119     SDNode *NN = new RegSDNode(ISD::ImplicitDef, Chain, Reg);
120     NN->setValueTypes(MVT::Other);
121     AllNodes.push_back(NN);
122     return SDOperand(NN, 0);
123   }
124
125   /// getCall - Note that this destroys the vector of RetVals passed in.
126   ///
127   SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
128                   SDOperand Callee) {
129     SDNode *NN = new SDNode(ISD::CALL, Chain, Callee);
130     NN->setValueTypes(RetVals);
131     AllNodes.push_back(NN);
132     return NN;
133   }
134
135   /// getCall - This is identical to the one above, and should be used for calls
136   /// where arguments are passed in physical registers.  This destroys the
137   /// RetVals and ArgsInRegs vectors.
138   SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
139                   SDOperand Callee, std::vector<SDOperand> &ArgsInRegs) {
140     ArgsInRegs.insert(ArgsInRegs.begin(), Callee);
141     ArgsInRegs.insert(ArgsInRegs.begin(), Chain);
142     SDNode *NN = new SDNode(ISD::CALL, ArgsInRegs);
143     NN->setValueTypes(RetVals);
144     AllNodes.push_back(NN);
145     return NN;
146   }
147
148
149   SDOperand getSetCC(ISD::CondCode, MVT::ValueType VT,
150                      SDOperand LHS, SDOperand RHS);
151
152   /// getNode - Gets or creates the specified node.
153   ///
154   SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
155   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
156   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
157                     SDOperand N1, SDOperand N2);
158   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
159                     SDOperand N1, SDOperand N2, SDOperand N3);
160   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
161                     std::vector<SDOperand> &Children);
162
163   // getNode - These versions take an extra value type for extending and
164   // truncating loads, stores, rounds, extends etc.
165   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N1,
166                     SDOperand N2, MVT::ValueType EVT);
167   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
168                     SDOperand N, MVT::ValueType EVT);
169   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N1,
170                     SDOperand N2, SDOperand N3, MVT::ValueType EVT);
171
172   /// getLoad - Loads are not normal binary operators: their result type is not
173   /// determined by their operands, and they produce a value AND a token chain.
174   ///
175   SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr);
176
177   void replaceAllUsesWith(SDOperand Old, SDOperand New) {
178     assert(Old != New && "RAUW self!");
179     assert(0 && "Unimplemented!");
180   }
181
182   void dump() const;
183
184 private:
185   void DeleteNodeIfDead(SDNode *N, void *NodeSet);
186
187   // Maps to auto-CSE operations.
188   std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >,
189            SDNode *> UnaryOps;
190   std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >,
191            SDNode *> BinaryOps;
192
193   std::map<std::pair<std::pair<SDOperand, SDOperand>,
194                      std::pair<ISD::CondCode, MVT::ValueType> >,
195            SetCCSDNode*> SetCCs;
196
197   std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >,
198            SDNode *> Loads;
199
200   std::map<const GlobalValue*, SDNode*> GlobalValues;
201   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants;
202   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs;
203   std::map<int, SDNode*> FrameIndices;
204   std::map<unsigned, SDNode*> ConstantPoolIndices;
205   std::map<MachineBasicBlock *, SDNode*> BBNodes;
206   std::map<std::string, SDNode*> ExternalSymbols;
207   struct EVTStruct {
208     unsigned Opcode;
209     MVT::ValueType VT, EVT;
210     std::vector<SDOperand> Ops;
211     bool operator<(const EVTStruct &RHS) const {
212       if (Opcode < RHS.Opcode) return true;
213       if (Opcode > RHS.Opcode) return false;
214       if (VT < RHS.VT) return true;
215       if (VT > RHS.VT) return false;
216       if (EVT < RHS.EVT) return true;
217       if (EVT > RHS.EVT) return false;
218       return Ops < RHS.Ops;
219     }
220   };
221   std::map<EVTStruct, SDNode*> MVTSDNodes;
222 };
223
224 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
225   typedef SelectionDAG::allnodes_iterator nodes_iterator;
226   static nodes_iterator nodes_begin(SelectionDAG *G) {
227     return G->allnodes_begin();
228   }
229   static nodes_iterator nodes_end(SelectionDAG *G) {
230     return G->allnodes_end();
231   }
232 };
233
234 }
235
236 #endif