Allow physregs to occur in the dag with multiple types. Though I don't like
[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 getTargetGlobalAddress(const GlobalValue *GV, MVT::ValueType VT);
101   SDOperand getFrameIndex(int FI, MVT::ValueType VT);
102   SDOperand getTargetFrameIndex(int FI, MVT::ValueType VT);
103   SDOperand getConstantPool(Constant *C, MVT::ValueType VT);
104   SDOperand getTargetConstantPool(Constant *C, MVT::ValueType VT);
105   SDOperand getBasicBlock(MachineBasicBlock *MBB);
106   SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
107   SDOperand getValueType(MVT::ValueType);
108   SDOperand getRegister(unsigned Reg, MVT::ValueType VT);
109
110   SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
111     return getNode(ISD::CopyToReg, MVT::Other, Chain,
112                    getRegister(Reg, N.getValueType()), N);
113   }
114
115   // This version of the getCopyToReg method takes an extra operand, which
116   // indicates that there is potentially an incoming flag value (if Flag is not
117   // null) and that there should be a flag result.
118   SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N,
119                          SDOperand Flag) {
120     std::vector<MVT::ValueType> VTs;
121     VTs.push_back(MVT::Other);
122     VTs.push_back(MVT::Flag);
123     std::vector<SDOperand> Ops;
124     Ops.push_back(Chain);
125     Ops.push_back(getRegister(Reg, N.getValueType()));
126     Ops.push_back(N);
127     if (Flag.Val) Ops.push_back(Flag);
128     return getNode(ISD::CopyToReg, VTs, Ops);
129   }
130   
131   SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
132     std::vector<MVT::ValueType> ResultTys;
133     ResultTys.push_back(VT);
134     ResultTys.push_back(MVT::Other);
135     std::vector<SDOperand> Ops;
136     Ops.push_back(Chain);
137     Ops.push_back(getRegister(Reg, VT));
138     return getNode(ISD::CopyFromReg, ResultTys, Ops);
139   }
140   
141   // This version of the getCopyFromReg method takes an extra operand, which
142   // indicates that there is potentially an incoming flag value (if Flag is not
143   // null) and that there should be a flag result.
144   SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT,
145                            SDOperand Flag) {
146     std::vector<MVT::ValueType> ResultTys;
147     ResultTys.push_back(VT);
148     ResultTys.push_back(MVT::Other);
149     ResultTys.push_back(MVT::Flag);
150     std::vector<SDOperand> Ops;
151     Ops.push_back(Chain);
152     Ops.push_back(getRegister(Reg, VT));
153     if (Flag.Val) Ops.push_back(Flag);
154     return getNode(ISD::CopyFromReg, ResultTys, Ops);
155   }
156
157   SDOperand getImplicitDef(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
158     return getNode(ISD::ImplicitDef, MVT::Other, Chain, getRegister(Reg, VT));
159   }
160
161   /// getCall - Note that this destroys the vector of RetVals passed in.
162   ///
163   SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
164                   SDOperand Callee, bool isTailCall = false) {
165     SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, Chain,
166                             Callee);
167     NN->setValueTypes(RetVals);
168     AllNodes.push_back(NN);
169     return NN;
170   }
171
172   /// getCall - This is identical to the one above, and should be used for calls
173   /// where arguments are passed in physical registers.  This destroys the
174   /// RetVals and ArgsInRegs vectors.
175   SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
176                   SDOperand Callee, std::vector<SDOperand> &ArgsInRegs,
177                   bool isTailCall = false) {
178     ArgsInRegs.insert(ArgsInRegs.begin(), Callee);
179     ArgsInRegs.insert(ArgsInRegs.begin(), Chain);
180     SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, ArgsInRegs);
181     NN->setValueTypes(RetVals);
182     AllNodes.push_back(NN);
183     return NN;
184   }
185
186   SDOperand getCondCode(ISD::CondCode Cond);
187
188   /// getZeroExtendInReg - Return the expression required to zero extend the Op
189   /// value assuming it was the smaller SrcTy value.
190   SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
191
192   /// getNode - Gets or creates the specified node.
193   ///
194   SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
195   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
196   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
197                     SDOperand N1, SDOperand N2);
198   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
199                     SDOperand N1, SDOperand N2, SDOperand N3);
200   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
201                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
202   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
203                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
204                     SDOperand N5);
205   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
206                     std::vector<SDOperand> &Children);
207   SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
208                     std::vector<SDOperand> &Ops);
209
210   /// getSetCC - Helper function to make it easier to build SetCC's if you just
211   /// have an ISD::CondCode instead of an SDOperand.
212   ///
213   SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS,
214                      ISD::CondCode Cond) {
215     return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
216   }
217
218   /// getSelectCC - Helper function to make it easier to build SelectCC's if you
219   /// just have an ISD::CondCode instead of an SDOperand.
220   ///
221   SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
222                         SDOperand True, SDOperand False, ISD::CondCode Cond) {
223     MVT::ValueType VT = True.getValueType();
224     return getNode(ISD::SELECT_CC, VT, LHS, RHS, True, False,getCondCode(Cond));
225   }
226   
227   /// getBR2Way_CC - Helper function to make it easier to build BRTWOWAY_CC
228   /// nodes.
229   ///
230   SDOperand getBR2Way_CC(SDOperand Chain, SDOperand CCNode, SDOperand LHS, 
231                          SDOperand RHS, SDOperand True, SDOperand False) {
232     std::vector<SDOperand> Ops;
233     Ops.push_back(Chain);
234     Ops.push_back(CCNode);
235     Ops.push_back(LHS);
236     Ops.push_back(RHS);
237     Ops.push_back(True);
238     Ops.push_back(False);
239     return getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
240   }
241
242   /// getLoad - Loads are not normal binary operators: their result type is not
243   /// determined by their operands, and they produce a value AND a token chain.
244   ///
245   SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
246                     SDOperand SV);
247   SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain,
248                        SDOperand Ptr, SDOperand SV, MVT::ValueType EVT);
249
250   // getSrcValue - construct a node to track a Value* through the backend
251   SDOperand getSrcValue(const Value* I, int offset = 0);
252
253   
254   /// SelectNodeTo - These are used for target selectors to *mutate* the
255   /// specified node to have the specified return type, Target opcode, and
256   /// operands.  Note that target opcodes are stored as
257   /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
258   void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT);
259   void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
260                     SDOperand Op1);
261   void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
262                     SDOperand Op1, SDOperand Op2);
263   void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
264                     SDOperand Op1, SDOperand Op2, SDOperand Op3);
265   void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
266                     SDOperand Op1, SDOperand Op2, SDOperand Op3, SDOperand Op4);
267   void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
268                     SDOperand Op1, SDOperand Op2, SDOperand Op3, SDOperand Op4,
269                     SDOperand Op5);
270   void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1, 
271                     MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
272   void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
273                     MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
274                     SDOperand Op3);
275   
276   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
277                           SDOperand Op1) {
278     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1);
279   }
280   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
281                           SDOperand Op1, SDOperand Op2) {
282     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2);
283   }
284   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
285                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
286     std::vector<MVT::ValueType> ResultTys;
287     ResultTys.push_back(VT1);
288     ResultTys.push_back(VT2);
289     std::vector<SDOperand> Ops;
290     Ops.push_back(Op1);
291     Ops.push_back(Op2);
292     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
293   }
294   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
295                           SDOperand Op1, SDOperand Op2, SDOperand Op3) {
296     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3);
297   }
298   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
299                           SDOperand Op1, SDOperand Op2, SDOperand Op3,
300                           SDOperand Op4) {
301     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4);
302   }
303   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
304                           SDOperand Op1, SDOperand Op2, SDOperand Op3,
305                           SDOperand Op4, SDOperand Op5) {
306     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5);
307   }
308   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
309                           std::vector<SDOperand> &Ops) {
310     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops);
311   }
312   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1, 
313                           MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
314     std::vector<MVT::ValueType> ResultTys;
315     ResultTys.push_back(VT1);
316     ResultTys.push_back(VT2);
317     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
318   }
319   
320   /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
321   /// This can cause recursive merging of nodes in the DAG.  Use the first
322   /// version if 'From' is known to have a single result, use the second
323   /// if you have two nodes with identical results, use the third otherwise.
324   ///
325   void ReplaceAllUsesWith(SDOperand From, SDOperand Op);
326   void ReplaceAllUsesWith(SDNode *From, SDNode *To);
327   void ReplaceAllUsesWith(SDNode *From, const std::vector<SDOperand> &To);
328   
329   
330   /// DeleteNode - Remove the specified node from the system.  This node must
331   /// have no referrers.
332   void DeleteNode(SDNode *N);
333   
334   void dump() const;
335
336 private:
337   void RemoveNodeFromCSEMaps(SDNode *N);
338   SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
339   void DeleteNodeIfDead(SDNode *N, void *NodeSet);
340   
341   /// SimplifySetCC - Try to simplify a setcc built with the specified operands 
342   /// and cc.  If unable to simplify it, return a null SDOperand.
343   SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1,
344                           SDOperand N2, ISD::CondCode Cond);
345
346   /// SimplifySelectCC - Try to simplify a select_cc built with the specified
347   /// operands and cc.  This can be used to simplify both the select_cc node,
348   /// and a select node whose first operand is a setcc.
349   SDOperand SimplifySelectCC(SDOperand N1, SDOperand N2, SDOperand N3,
350                              SDOperand N4, ISD::CondCode CC);
351     
352   // Maps to auto-CSE operations.
353   std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >,
354            SDNode *> UnaryOps;
355   std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >,
356            SDNode *> BinaryOps;
357
358   std::map<std::pair<unsigned, MVT::ValueType>, RegisterSDNode*> RegNodes;
359   std::vector<CondCodeSDNode*> CondCodeNodes;
360
361   std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >,
362            SDNode *> Loads;
363
364   std::map<const GlobalValue*, SDNode*> GlobalValues;
365   std::map<const GlobalValue*, SDNode*> TargetGlobalValues;
366   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants;
367   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> TargetConstants;
368   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs;
369   std::map<int, SDNode*> FrameIndices, TargetFrameIndices;
370   std::map<Constant *, SDNode*> ConstantPoolIndices;
371   std::map<Constant *, SDNode*> TargetConstantPoolIndices;
372   std::map<MachineBasicBlock *, SDNode*> BBNodes;
373   std::vector<SDNode*> ValueTypeNodes;
374   std::map<std::string, SDNode*> ExternalSymbols;
375   std::map<std::pair<unsigned,
376                      std::pair<MVT::ValueType, std::vector<SDOperand> > >,
377            SDNode*> OneResultNodes;
378   std::map<std::pair<unsigned,
379                      std::pair<std::vector<MVT::ValueType>,
380                                std::vector<SDOperand> > >,
381            SDNode*> ArbitraryNodes;
382 };
383
384 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
385   typedef SelectionDAG::allnodes_iterator nodes_iterator;
386   static nodes_iterator nodes_begin(SelectionDAG *G) {
387     return G->allnodes_begin();
388   }
389   static nodes_iterator nodes_end(SelectionDAG *G) {
390     return G->allnodes_end();
391   }
392 };
393
394 }
395
396 #endif