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