add ReplaceAllUsesWith, and a helper to implemented it
[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
48   // ValueNodes - track SrcValue nodes
49   std::map<std::pair<const Value*, int>, SDNode*> ValueNodes;
50
51 public:
52   SelectionDAG(TargetLowering &tli, MachineFunction &mf) : TLI(tli), MF(mf) {
53     EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
54   }
55   ~SelectionDAG();
56
57   MachineFunction &getMachineFunction() const { return MF; }
58   const TargetMachine &getTarget() const;
59   TargetLowering &getTargetLoweringInfo() const { return TLI; }
60
61   /// viewGraph - Pop up a ghostview window with the DAG rendered using 'dot'.
62   ///
63   void viewGraph();
64
65
66   typedef std::vector<SDNode*>::const_iterator allnodes_iterator;
67   allnodes_iterator allnodes_begin() const { return AllNodes.begin(); }
68   allnodes_iterator allnodes_end() const { return AllNodes.end(); }
69
70   /// getRoot - Return the root tag of the SelectionDAG.
71   ///
72   const SDOperand &getRoot() const { return Root; }
73
74   /// getEntryNode - Return the token chain corresponding to the entry of the
75   /// function.
76   const SDOperand &getEntryNode() const { return EntryNode; }
77
78   /// setRoot - Set the current root tag of the SelectionDAG.
79   ///
80   const SDOperand &setRoot(SDOperand N) { return Root = N; }
81
82   /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
83   /// compatible with the target instruction selector, as indicated by the
84   /// TargetLowering object.
85   ///
86   /// Note that this is an involved process that may invalidate pointers into
87   /// the graph.
88   void Legalize();
89
90   /// RemoveDeadNodes - This method deletes all unreachable nodes in the
91   /// SelectionDAG, including nodes (like loads) that have uses of their token
92   /// chain but no other uses and no side effect.  If a node is passed in as an
93   /// argument, it is used as the seed for node deletion.
94   void RemoveDeadNodes(SDNode *N = 0);
95
96   SDOperand getConstant(uint64_t Val, MVT::ValueType VT);
97   SDOperand getTargetConstant(uint64_t Val, MVT::ValueType VT);
98   SDOperand getConstantFP(double Val, MVT::ValueType VT);
99   SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT);
100   SDOperand getFrameIndex(int FI, MVT::ValueType VT);
101   SDOperand getConstantPool(unsigned CPIdx, MVT::ValueType VT);
102   SDOperand getBasicBlock(MachineBasicBlock *MBB);
103   SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
104   SDOperand getValueType(MVT::ValueType);
105   SDOperand getRegister(unsigned Reg, MVT::ValueType VT);
106
107   SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
108     return getNode(ISD::CopyToReg, MVT::Other, Chain,
109                    getRegister(Reg, N.getValueType()), N);
110   }
111
112   SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
113     std::vector<MVT::ValueType> ResultTys;
114     ResultTys.push_back(VT);
115     ResultTys.push_back(MVT::Other);
116     std::vector<SDOperand> Ops;
117     Ops.push_back(Chain);
118     Ops.push_back(getRegister(Reg, VT));
119     return getNode(ISD::CopyFromReg, ResultTys, Ops);
120   }
121
122   SDOperand getImplicitDef(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
123     return getNode(ISD::ImplicitDef, MVT::Other, Chain, getRegister(Reg, VT));
124   }
125
126   /// getCall - Note that this destroys the vector of RetVals passed in.
127   ///
128   SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
129                   SDOperand Callee, bool isTailCall = false) {
130     SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, Chain,
131                             Callee);
132     NN->setValueTypes(RetVals);
133     AllNodes.push_back(NN);
134     return NN;
135   }
136
137   /// getCall - This is identical to the one above, and should be used for calls
138   /// where arguments are passed in physical registers.  This destroys the
139   /// RetVals and ArgsInRegs vectors.
140   SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
141                   SDOperand Callee, std::vector<SDOperand> &ArgsInRegs,
142                   bool isTailCall = false) {
143     ArgsInRegs.insert(ArgsInRegs.begin(), Callee);
144     ArgsInRegs.insert(ArgsInRegs.begin(), Chain);
145     SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, ArgsInRegs);
146     NN->setValueTypes(RetVals);
147     AllNodes.push_back(NN);
148     return NN;
149   }
150
151   SDOperand getCondCode(ISD::CondCode Cond);
152
153   /// getZeroExtendInReg - Return the expression required to zero extend the Op
154   /// value assuming it was the smaller SrcTy value.
155   SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
156
157   /// getNode - Gets or creates the specified node.
158   ///
159   SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
160   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
161   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
162                     SDOperand N1, SDOperand N2);
163   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
164                     SDOperand N1, SDOperand N2, SDOperand N3);
165   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
166                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
167   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
168                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
169                     SDOperand N5);
170   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
171                     std::vector<SDOperand> &Children);
172   SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
173                     std::vector<SDOperand> &Ops);
174
175   /// getSetCC - Helper function to make it easier to build SetCC's if you just
176   /// have an ISD::CondCode instead of an SDOperand.
177   ///
178   SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS,
179                      ISD::CondCode Cond) {
180     return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
181   }
182
183   /// getSelectCC - Helper function to make it easier to build SelectCC's if you
184   /// just have an ISD::CondCode instead of an SDOperand.
185   ///
186   SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
187                         SDOperand True, SDOperand False, ISD::CondCode Cond) {
188     MVT::ValueType VT = True.getValueType();
189     return getNode(ISD::SELECT_CC, VT, LHS, RHS, True, False,getCondCode(Cond));
190   }
191   
192   /// getBR2Way_CC - Helper function to make it easier to build BRTWOWAY_CC
193   /// nodes.
194   ///
195   SDOperand getBR2Way_CC(SDOperand Chain, SDOperand CCNode, SDOperand LHS, 
196                          SDOperand RHS, SDOperand True, SDOperand False) {
197     std::vector<SDOperand> Ops;
198     Ops.push_back(Chain);
199     Ops.push_back(CCNode);
200     Ops.push_back(LHS);
201     Ops.push_back(RHS);
202     Ops.push_back(True);
203     Ops.push_back(False);
204     return getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
205   }
206
207   /// getLoad - Loads are not normal binary operators: their result type is not
208   /// determined by their operands, and they produce a value AND a token chain.
209   ///
210   SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
211                     SDOperand SV);
212   SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain,
213                        SDOperand Ptr, SDOperand SV, MVT::ValueType EVT);
214
215   // getSrcValue - construct a node to track a Value* through the backend
216   SDOperand getSrcValue(const Value* I, int offset = 0);
217
218   
219   /// SelectNodeTo - These are used for target selectors to *mutate* the
220   /// specified node to have the specified return type, Target opcode, and
221   /// operands.  Note that target opcodes are stored as
222   /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
223   void SelectNodeTo(SDNode *N, MVT::ValueType VT, unsigned TargetOpc,
224                     SDOperand Op1);
225   void SelectNodeTo(SDNode *N, MVT::ValueType VT, unsigned TargetOpc,
226                     SDOperand Op1, SDOperand Op2);
227   void SelectNodeTo(SDNode *N, MVT::ValueType VT, unsigned TargetOpc,
228                     SDOperand Op1, SDOperand Op2, SDOperand Op3);
229   
230   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
231                           SDOperand Op1) {
232     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1);
233   }
234   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
235                           SDOperand Op1, SDOperand Op2) {
236     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2);
237   }
238   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
239                           SDOperand Op1, SDOperand Op2, SDOperand Op3) {
240     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3);
241   }
242   
243   /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
244   /// This can cause recursive merging of nodes in the DAG.
245   ///
246   void ReplaceAllUsesWith(SDNode *From, SDNode *To);
247   
248   void dump() const;
249
250 private:
251   void RemoveNodeFromCSEMaps(SDNode *N);
252   SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
253   void DeleteNodeIfDead(SDNode *N, void *NodeSet);
254   
255   /// SimplifySetCC - Try to simplify a setcc built with the specified operands 
256   /// and cc.  If unable to simplify it, return a null SDOperand.
257   SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1,
258                           SDOperand N2, ISD::CondCode Cond);
259
260   /// SimplifySelectCC - Try to simplify a select_cc built with the specified
261   /// operands and cc.  This can be used to simplify both the select_cc node,
262   /// and a select node whose first operand is a setcc.
263   SDOperand SimplifySelectCC(SDOperand N1, SDOperand N2, SDOperand N3,
264                              SDOperand N4, ISD::CondCode CC);
265     
266   // Maps to auto-CSE operations.
267   std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >,
268            SDNode *> UnaryOps;
269   std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >,
270            SDNode *> BinaryOps;
271
272   std::vector<RegisterSDNode*> RegNodes;
273   std::vector<CondCodeSDNode*> CondCodeNodes;
274
275   std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >,
276            SDNode *> Loads;
277
278   std::map<const GlobalValue*, SDNode*> GlobalValues;
279   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants;
280   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> TargetConstants;
281   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs;
282   std::map<int, SDNode*> FrameIndices;
283   std::map<unsigned, SDNode*> ConstantPoolIndices;
284   std::map<MachineBasicBlock *, SDNode*> BBNodes;
285   std::vector<SDNode*> ValueTypeNodes;
286   std::map<std::string, SDNode*> ExternalSymbols;
287   std::map<std::pair<unsigned,
288                      std::pair<MVT::ValueType, std::vector<SDOperand> > >,
289            SDNode*> OneResultNodes;
290   std::map<std::pair<unsigned,
291                      std::pair<std::vector<MVT::ValueType>,
292                                std::vector<SDOperand> > >,
293            SDNode*> ArbitraryNodes;
294 };
295
296 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
297   typedef SelectionDAG::allnodes_iterator nodes_iterator;
298   static nodes_iterator nodes_begin(SelectionDAG *G) {
299     return G->allnodes_begin();
300   }
301   static nodes_iterator nodes_end(SelectionDAG *G) {
302     return G->allnodes_end();
303   }
304 };
305
306 }
307
308 #endif