f3d41cea15f7e85f8de9acdbc6b6aff9dfe76e1d
[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   /// getZeroExtendInReg - Return the expression required to zero extend the Op
153   /// value assuming it was the smaller SrcTy value.
154   SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
155
156   /// getNode - Gets or creates the specified node.
157   ///
158   SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
159   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
160   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
161                     SDOperand N1, SDOperand N2);
162   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
163                     SDOperand N1, SDOperand N2, SDOperand N3);
164   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
165                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
166   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
167                     std::vector<SDOperand> &Children);
168
169   // getNode - These versions take an extra value type for extending and
170   // truncating loads, stores, rounds, extends etc.
171   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N1,
172                     SDOperand N2, MVT::ValueType EVT);
173   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
174                     SDOperand N, MVT::ValueType EVT);
175   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N1,
176                     SDOperand N2, SDOperand N3, MVT::ValueType EVT);
177   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N1,
178                     SDOperand N2, SDOperand N3, SDOperand N4, MVT::ValueType EVT);
179
180   /// getLoad - Loads are not normal binary operators: their result type is not
181   /// determined by their operands, and they produce a value AND a token chain.
182   ///
183   SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr, SDOperand SV);
184
185   // getSrcValue - construct a node to track a Value* through the backend
186   SDOperand getSrcValue(const Value* I);
187
188   void replaceAllUsesWith(SDOperand Old, SDOperand New) {
189     assert(Old != New && "RAUW self!");
190     assert(0 && "Unimplemented!");
191   }
192
193   void dump() const;
194
195 private:
196   void DeleteNodeIfDead(SDNode *N, void *NodeSet);
197
198   // Maps to auto-CSE operations.
199   std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >,
200            SDNode *> UnaryOps;
201   std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >,
202            SDNode *> BinaryOps;
203
204   std::map<std::pair<std::pair<SDOperand, SDOperand>,
205                      std::pair<ISD::CondCode, MVT::ValueType> >,
206            SetCCSDNode*> SetCCs;
207
208   std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >,
209            SDNode *> Loads;
210
211   std::map<const GlobalValue*, SDNode*> GlobalValues;
212   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants;
213   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs;
214   std::map<int, SDNode*> FrameIndices;
215   std::map<unsigned, SDNode*> ConstantPoolIndices;
216   std::map<MachineBasicBlock *, SDNode*> BBNodes;
217   std::map<std::string, SDNode*> ExternalSymbols;
218   struct EVTStruct {
219     unsigned Opcode;
220     MVT::ValueType VT, EVT;
221     std::vector<SDOperand> Ops;
222     bool operator<(const EVTStruct &RHS) const {
223       if (Opcode < RHS.Opcode) return true;
224       if (Opcode > RHS.Opcode) return false;
225       if (VT < RHS.VT) return true;
226       if (VT > RHS.VT) return false;
227       if (EVT < RHS.EVT) return true;
228       if (EVT > RHS.EVT) return false;
229       return Ops < RHS.Ops;
230     }
231   };
232   std::map<EVTStruct, SDNode*> MVTSDNodes;
233 };
234
235 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
236   typedef SelectionDAG::allnodes_iterator nodes_iterator;
237   static nodes_iterator nodes_begin(SelectionDAG *G) {
238     return G->allnodes_begin();
239   }
240   static nodes_iterator nodes_end(SelectionDAG *G) {
241     return G->allnodes_end();
242   }
243 };
244
245 }
246
247 #endif