195e8e435b0c2a9c6ab309a6a80b816f87813d3e
[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 "llvm/ADT/ilist"
20
21 #include <map>
22 #include <list>
23 #include <string>
24
25 namespace llvm {
26   class TargetLowering;
27   class TargetMachine;
28   class MachineDebugInfo;
29   class MachineFunction;
30
31 /// SelectionDAG class - This is used to represent a portion of an LLVM function
32 /// in a low-level Data Dependence DAG representation suitable for instruction
33 /// selection.  This DAG is constructed as the first step of instruction
34 /// selection in order to allow implementation of machine specific optimizations
35 /// and code simplifications.
36 ///
37 /// The representation used by the SelectionDAG is a target-independent
38 /// representation, which has some similarities to the GCC RTL representation,
39 /// but is significantly more simple, powerful, and is a graph form instead of a
40 /// linear form.
41 ///
42 class SelectionDAG {
43   TargetLowering &TLI;
44   MachineFunction &MF;
45   MachineDebugInfo *DI;
46
47   // Root - The root of the entire DAG.  EntryNode - The starting token.
48   SDOperand Root, EntryNode;
49
50   // AllNodes - A linked list of nodes in the current DAG.
51   ilist<SDNode> AllNodes;
52
53   // ValueNodes - track SrcValue nodes
54   std::map<std::pair<const Value*, int>, SDNode*> ValueNodes;
55
56 public:
57   SelectionDAG(TargetLowering &tli, MachineFunction &mf, MachineDebugInfo *di)
58   : TLI(tli), MF(mf), DI(di) {
59     EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
60   }
61   ~SelectionDAG();
62
63   MachineFunction &getMachineFunction() const { return MF; }
64   const TargetMachine &getTarget() const;
65   TargetLowering &getTargetLoweringInfo() const { return TLI; }
66   MachineDebugInfo *getMachineDebugInfo() const { return DI; }
67
68   /// viewGraph - Pop up a ghostview window with the DAG rendered using 'dot'.
69   ///
70   void viewGraph();
71
72
73   typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
74   allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
75   allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
76   typedef ilist<SDNode>::iterator allnodes_iterator;
77   allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
78   allnodes_iterator allnodes_end() { return AllNodes.end(); }
79   
80   /// getRoot - Return the root tag of the SelectionDAG.
81   ///
82   const SDOperand &getRoot() const { return Root; }
83
84   /// getEntryNode - Return the token chain corresponding to the entry of the
85   /// function.
86   const SDOperand &getEntryNode() const { return EntryNode; }
87
88   /// setRoot - Set the current root tag of the SelectionDAG.
89   ///
90   const SDOperand &setRoot(SDOperand N) { return Root = N; }
91
92   /// Combine - This iterates over the nodes in the SelectionDAG, folding
93   /// certain types of nodes together, or eliminating superfluous nodes.  When
94   /// the AfterLegalize argument is set to 'true', Combine takes care not to
95   /// generate any nodes that will be illegal on the target.
96   void Combine(bool AfterLegalize);
97   
98   /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
99   /// compatible with the target instruction selector, as indicated by the
100   /// TargetLowering object.
101   ///
102   /// Note that this is an involved process that may invalidate pointers into
103   /// the graph.
104   void Legalize();
105
106   /// RemoveDeadNodes - This method deletes all unreachable nodes in the
107   /// SelectionDAG, including nodes (like loads) that have uses of their token
108   /// chain but no other uses and no side effect.  If a node is passed in as an
109   /// argument, it is used as the seed for node deletion.
110   void RemoveDeadNodes(SDNode *N = 0);
111
112   SDOperand getString(const std::string &Val);
113   SDOperand getConstant(uint64_t Val, MVT::ValueType VT);
114   SDOperand getTargetConstant(uint64_t Val, MVT::ValueType VT);
115   SDOperand getConstantFP(double Val, MVT::ValueType VT);
116   SDOperand getTargetConstantFP(double Val, MVT::ValueType VT);
117   SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT,
118                              int offset = 0);
119   SDOperand getTargetGlobalAddress(const GlobalValue *GV, MVT::ValueType VT,
120                                    int offset = 0);
121   SDOperand getFrameIndex(int FI, MVT::ValueType VT);
122   SDOperand getTargetFrameIndex(int FI, MVT::ValueType VT);
123   SDOperand getConstantPool(Constant *C, MVT::ValueType VT);
124   SDOperand getTargetConstantPool(Constant *C, MVT::ValueType VT);
125   SDOperand getBasicBlock(MachineBasicBlock *MBB);
126   SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
127   SDOperand getTargetExternalSymbol(const char *Sym, MVT::ValueType VT);
128   SDOperand getValueType(MVT::ValueType);
129   SDOperand getRegister(unsigned Reg, MVT::ValueType VT);
130
131   SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
132     return getNode(ISD::CopyToReg, MVT::Other, Chain,
133                    getRegister(Reg, N.getValueType()), N);
134   }
135
136   // This version of the getCopyToReg method takes an extra operand, which
137   // indicates that there is potentially an incoming flag value (if Flag is not
138   // null) and that there should be a flag result.
139   SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N,
140                          SDOperand Flag) {
141     std::vector<MVT::ValueType> VTs;
142     VTs.push_back(MVT::Other);
143     VTs.push_back(MVT::Flag);
144     std::vector<SDOperand> Ops;
145     Ops.push_back(Chain);
146     Ops.push_back(getRegister(Reg, N.getValueType()));
147     Ops.push_back(N);
148     if (Flag.Val) Ops.push_back(Flag);
149     return getNode(ISD::CopyToReg, VTs, Ops);
150   }
151
152   // Similar to last getCopyToReg() except parameter Reg is a SDOperand
153   SDOperand getCopyToReg(SDOperand Chain, SDOperand Reg, SDOperand N,
154                          SDOperand Flag) {
155     std::vector<MVT::ValueType> VTs;
156     VTs.push_back(MVT::Other);
157     VTs.push_back(MVT::Flag);
158     std::vector<SDOperand> Ops;
159     Ops.push_back(Chain);
160     Ops.push_back(Reg);
161     Ops.push_back(N);
162     if (Flag.Val) Ops.push_back(Flag);
163     return getNode(ISD::CopyToReg, VTs, Ops);
164   }
165   
166   SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
167     std::vector<MVT::ValueType> ResultTys;
168     ResultTys.push_back(VT);
169     ResultTys.push_back(MVT::Other);
170     std::vector<SDOperand> Ops;
171     Ops.push_back(Chain);
172     Ops.push_back(getRegister(Reg, VT));
173     return getNode(ISD::CopyFromReg, ResultTys, Ops);
174   }
175   
176   // This version of the getCopyFromReg method takes an extra operand, which
177   // indicates that there is potentially an incoming flag value (if Flag is not
178   // null) and that there should be a flag result.
179   SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT,
180                            SDOperand Flag) {
181     std::vector<MVT::ValueType> ResultTys;
182     ResultTys.push_back(VT);
183     ResultTys.push_back(MVT::Other);
184     ResultTys.push_back(MVT::Flag);
185     std::vector<SDOperand> Ops;
186     Ops.push_back(Chain);
187     Ops.push_back(getRegister(Reg, VT));
188     if (Flag.Val) Ops.push_back(Flag);
189     return getNode(ISD::CopyFromReg, ResultTys, Ops);
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   /// getVAArg - VAArg produces a result and token chain, and takes a pointer
234   /// and a source value as input.
235   SDOperand getVAArg(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
236                      SDOperand SV);
237
238   /// getLoad - Loads are not normal binary operators: their result type is not
239   /// determined by their operands, and they produce a value AND a token chain.
240   ///
241   SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
242                     SDOperand SV);
243   SDOperand getVecLoad(unsigned Count, MVT::ValueType VT, SDOperand Chain, 
244                        SDOperand Ptr, SDOperand SV);
245   SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain,
246                        SDOperand Ptr, SDOperand SV, MVT::ValueType EVT);
247
248   // getSrcValue - construct a node to track a Value* through the backend
249   SDOperand getSrcValue(const Value* I, int offset = 0);
250
251   /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
252   /// specified operands.  If the resultant node already exists in the DAG,
253   /// this does not modify the specified node, instead it returns the node that
254   /// already exists.  If the resultant node does not exist in the DAG, the
255   /// input node is returned.  As a degenerate case, if you specify the same
256   /// input operands as the node already has, the input node is returned.
257   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op);
258   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2);
259   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
260                                SDOperand Op3);
261   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
262                                SDOperand Op3, SDOperand Op4);
263   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
264                                SDOperand Op3, SDOperand Op4, SDOperand Op5);
265   SDOperand UpdateNodeOperands(SDOperand N, const std::vector<SDOperand> &Op);
266   
267   /// SelectNodeTo - These are used for target selectors to *mutate* the
268   /// specified node to have the specified return type, Target opcode, and
269   /// operands.  Note that target opcodes are stored as
270   /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.  The 0th value
271   /// of the resultant node is returned.
272   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT);
273   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
274                          SDOperand Op1);
275   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
276                          SDOperand Op1, SDOperand Op2);
277   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
278                          SDOperand Op1, SDOperand Op2, SDOperand Op3);
279   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
280                          SDOperand Op1, SDOperand Op2, SDOperand Op3, 
281                          SDOperand Op4);
282   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
283                          SDOperand Op1, SDOperand Op2, SDOperand Op3,
284                          SDOperand Op4, SDOperand Op5);
285   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
286                          SDOperand Op1, SDOperand Op2, SDOperand Op3, 
287                          SDOperand Op4, SDOperand Op5, SDOperand Op6);
288   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
289                          SDOperand Op1, SDOperand Op2, SDOperand Op3,
290                          SDOperand Op4, SDOperand Op5, SDOperand Op6,
291                          SDOperand Op7);
292   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
293                          SDOperand Op1, SDOperand Op2, SDOperand Op3,
294                          SDOperand Op4, SDOperand Op5, SDOperand Op6,
295                          SDOperand Op7, SDOperand Op8);
296   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1, 
297                          MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
298   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
299                          MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
300                          SDOperand Op3);
301   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
302                          MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
303                          SDOperand Op3, SDOperand Op4);
304   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
305                          MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
306                          SDOperand Op3, SDOperand Op4, SDOperand Op5);
307
308   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT) {
309     return getNode(ISD::BUILTIN_OP_END+Opcode, VT);
310   }
311   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
312                           SDOperand Op1) {
313     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1);
314   }
315   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
316                           SDOperand Op1, SDOperand Op2) {
317     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2);
318   }
319   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
320                           SDOperand Op1, SDOperand Op2, SDOperand Op3) {
321     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3);
322   }
323   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
324                           SDOperand Op1, SDOperand Op2, SDOperand Op3,
325                           SDOperand Op4) {
326     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4);
327   }
328   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
329                           SDOperand Op1, SDOperand Op2, SDOperand Op3,
330                           SDOperand Op4, SDOperand Op5) {
331     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5);
332   }
333   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
334                           SDOperand Op1, SDOperand Op2, SDOperand Op3,
335                           SDOperand Op4, SDOperand Op5, SDOperand Op6) {
336     std::vector<SDOperand> Ops;
337     Ops.reserve(6);
338     Ops.push_back(Op1);
339     Ops.push_back(Op2);
340     Ops.push_back(Op3);
341     Ops.push_back(Op4);
342     Ops.push_back(Op5);
343     Ops.push_back(Op6);
344     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops);
345   }
346   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
347                           SDOperand Op1, SDOperand Op2, SDOperand Op3,
348                           SDOperand Op4, SDOperand Op5, SDOperand Op6,
349                           SDOperand Op7) {
350     std::vector<SDOperand> Ops;
351     Ops.reserve(7);
352     Ops.push_back(Op1);
353     Ops.push_back(Op2);
354     Ops.push_back(Op3);
355     Ops.push_back(Op4);
356     Ops.push_back(Op5);
357     Ops.push_back(Op6);
358     Ops.push_back(Op7);
359     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops);
360   }
361   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
362                           SDOperand Op1, SDOperand Op2, SDOperand Op3,
363                           SDOperand Op4, SDOperand Op5, SDOperand Op6,
364                           SDOperand Op7, SDOperand Op8) {
365     std::vector<SDOperand> Ops;
366     Ops.reserve(8);
367     Ops.push_back(Op1);
368     Ops.push_back(Op2);
369     Ops.push_back(Op3);
370     Ops.push_back(Op4);
371     Ops.push_back(Op5);
372     Ops.push_back(Op6);
373     Ops.push_back(Op7);
374     Ops.push_back(Op8);
375     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops);
376   }
377   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
378                           std::vector<SDOperand> &Ops) {
379     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops);
380   }
381   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
382                           MVT::ValueType VT2, SDOperand Op1) {
383     std::vector<MVT::ValueType> ResultTys;
384     ResultTys.push_back(VT1);
385     ResultTys.push_back(VT2);
386     std::vector<SDOperand> Ops;
387     Ops.push_back(Op1);
388     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
389   }
390   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
391                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
392     std::vector<MVT::ValueType> ResultTys;
393     ResultTys.push_back(VT1);
394     ResultTys.push_back(VT2);
395     std::vector<SDOperand> Ops;
396     Ops.push_back(Op1);
397     Ops.push_back(Op2);
398     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
399   }
400   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
401                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
402                           SDOperand Op3) {
403     std::vector<MVT::ValueType> ResultTys;
404     ResultTys.push_back(VT1);
405     ResultTys.push_back(VT2);
406     std::vector<SDOperand> Ops;
407     Ops.push_back(Op1);
408     Ops.push_back(Op2);
409     Ops.push_back(Op3);
410     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
411   }
412   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
413                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
414                           SDOperand Op3, SDOperand Op4) {
415     std::vector<MVT::ValueType> ResultTys;
416     ResultTys.push_back(VT1);
417     ResultTys.push_back(VT2);
418     std::vector<SDOperand> Ops;
419     Ops.push_back(Op1);
420     Ops.push_back(Op2);
421     Ops.push_back(Op3);
422     Ops.push_back(Op4);
423     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
424   }
425   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
426                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
427                           SDOperand Op3, SDOperand Op4, SDOperand Op5) {
428     std::vector<MVT::ValueType> ResultTys;
429     ResultTys.push_back(VT1);
430     ResultTys.push_back(VT2);
431     std::vector<SDOperand> Ops;
432     Ops.push_back(Op1);
433     Ops.push_back(Op2);
434     Ops.push_back(Op3);
435     Ops.push_back(Op4);
436     Ops.push_back(Op5);
437     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
438   }
439   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
440                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
441                           SDOperand Op3, SDOperand Op4, SDOperand Op5,
442                           SDOperand Op6) {
443     std::vector<MVT::ValueType> ResultTys;
444     ResultTys.push_back(VT1);
445     ResultTys.push_back(VT2);
446     std::vector<SDOperand> Ops;
447     Ops.push_back(Op1);
448     Ops.push_back(Op2);
449     Ops.push_back(Op3);
450     Ops.push_back(Op4);
451     Ops.push_back(Op5);
452     Ops.push_back(Op6);
453     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
454   }
455   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
456                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
457                           SDOperand Op3, SDOperand Op4, SDOperand Op5,
458                           SDOperand Op6, SDOperand Op7) {
459     std::vector<MVT::ValueType> ResultTys;
460     ResultTys.push_back(VT1);
461     ResultTys.push_back(VT2);
462     std::vector<SDOperand> Ops;
463     Ops.push_back(Op1);
464     Ops.push_back(Op2);
465     Ops.push_back(Op3);
466     Ops.push_back(Op4);
467     Ops.push_back(Op5);
468     Ops.push_back(Op6); 
469     Ops.push_back(Op7);
470    return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
471   }
472   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
473                           MVT::ValueType VT2, MVT::ValueType VT3,
474                           SDOperand Op1, SDOperand Op2) {
475     std::vector<MVT::ValueType> ResultTys;
476     ResultTys.push_back(VT1);
477     ResultTys.push_back(VT2);
478     ResultTys.push_back(VT3);
479     std::vector<SDOperand> Ops;
480     Ops.push_back(Op1);
481     Ops.push_back(Op2);
482     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
483   }
484   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
485                           MVT::ValueType VT2, MVT::ValueType VT3,
486                           SDOperand Op1, SDOperand Op2,
487                           SDOperand Op3, SDOperand Op4, SDOperand Op5) {
488     std::vector<MVT::ValueType> ResultTys;
489     ResultTys.push_back(VT1);
490     ResultTys.push_back(VT2);
491     ResultTys.push_back(VT3);
492     std::vector<SDOperand> Ops;
493     Ops.push_back(Op1);
494     Ops.push_back(Op2);
495     Ops.push_back(Op3);
496     Ops.push_back(Op4);
497     Ops.push_back(Op5);
498     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
499   }
500   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
501                           MVT::ValueType VT2, MVT::ValueType VT3,
502                           SDOperand Op1, SDOperand Op2,
503                           SDOperand Op3, SDOperand Op4, SDOperand Op5,
504                           SDOperand Op6) {
505     std::vector<MVT::ValueType> ResultTys;
506     ResultTys.push_back(VT1);
507     ResultTys.push_back(VT2);
508     ResultTys.push_back(VT3);
509     std::vector<SDOperand> Ops;
510     Ops.push_back(Op1);
511     Ops.push_back(Op2);
512     Ops.push_back(Op3);
513     Ops.push_back(Op4);
514     Ops.push_back(Op5);
515     Ops.push_back(Op6);
516     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
517   }
518   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
519                           MVT::ValueType VT2, MVT::ValueType VT3,
520                           SDOperand Op1, SDOperand Op2,
521                           SDOperand Op3, SDOperand Op4, SDOperand Op5,
522                           SDOperand Op6, SDOperand Op7) {
523     std::vector<MVT::ValueType> ResultTys;
524     ResultTys.push_back(VT1);
525     ResultTys.push_back(VT2);
526     ResultTys.push_back(VT3);
527     std::vector<SDOperand> Ops;
528     Ops.push_back(Op1);
529     Ops.push_back(Op2);
530     Ops.push_back(Op3);
531     Ops.push_back(Op4);
532     Ops.push_back(Op5);
533     Ops.push_back(Op6);
534     Ops.push_back(Op7);
535     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
536   }
537   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1, 
538                           MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
539     std::vector<MVT::ValueType> ResultTys;
540     ResultTys.push_back(VT1);
541     ResultTys.push_back(VT2);
542     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
543   }
544   
545   /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
546   /// This can cause recursive merging of nodes in the DAG.  Use the first
547   /// version if 'From' is known to have a single result, use the second
548   /// if you have two nodes with identical results, use the third otherwise.
549   ///
550   /// These methods all take an optional vector, which (if not null) is 
551   /// populated with any nodes that are deleted from the SelectionDAG, due to
552   /// new equivalences that are discovered.
553   ///
554   void ReplaceAllUsesWith(SDOperand From, SDOperand Op,
555                           std::vector<SDNode*> *Deleted = 0);
556   void ReplaceAllUsesWith(SDNode *From, SDNode *To,
557                           std::vector<SDNode*> *Deleted = 0);
558   void ReplaceAllUsesWith(SDNode *From, const std::vector<SDOperand> &To,
559                           std::vector<SDNode*> *Deleted = 0);
560   
561   
562   /// DeleteNode - Remove the specified node from the system.  This node must
563   /// have no referrers.
564   void DeleteNode(SDNode *N);
565   
566   void dump() const;
567
568 private:
569   void RemoveNodeFromCSEMaps(SDNode *N);
570   SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
571   SDNode **FindModifiedNodeSlot(SDNode *N, SDOperand Op);
572   SDNode **FindModifiedNodeSlot(SDNode *N, SDOperand Op1, SDOperand Op2);
573   SDNode **FindModifiedNodeSlot(SDNode *N, const std::vector<SDOperand> &Ops);
574
575   void DestroyDeadNode(SDNode *N);
576   void DeleteNodeNotInCSEMaps(SDNode *N);
577   void setNodeValueTypes(SDNode *N, std::vector<MVT::ValueType> &RetVals);
578   void setNodeValueTypes(SDNode *N, MVT::ValueType VT1, MVT::ValueType VT2);
579   
580   
581   /// SimplifySetCC - Try to simplify a setcc built with the specified operands 
582   /// and cc.  If unable to simplify it, return a null SDOperand.
583   SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1,
584                           SDOperand N2, ISD::CondCode Cond);
585   
586   // List of non-single value types.
587   std::list<std::vector<MVT::ValueType> > VTList;
588   
589   // Maps to auto-CSE operations.
590   std::map<std::pair<unsigned, MVT::ValueType>, SDNode *> NullaryOps;
591   std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >,
592            SDNode *> UnaryOps;
593   std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >,
594            SDNode *> BinaryOps;
595
596   std::map<std::pair<unsigned, MVT::ValueType>, RegisterSDNode*> RegNodes;
597   std::vector<CondCodeSDNode*> CondCodeNodes;
598
599   std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >,
600            SDNode *> Loads;
601
602   std::map<std::pair<const GlobalValue*, int>, SDNode*> GlobalValues;
603   std::map<std::pair<const GlobalValue*, int>, SDNode*> TargetGlobalValues;
604   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants;
605   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> TargetConstants;
606   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs;
607   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> TargetConstantFPs;
608   std::map<int, SDNode*> FrameIndices, TargetFrameIndices;
609   std::map<Constant *, SDNode*> ConstantPoolIndices;
610   std::map<Constant *, SDNode*> TargetConstantPoolIndices;
611   std::map<MachineBasicBlock *, SDNode*> BBNodes;
612   std::vector<SDNode*> ValueTypeNodes;
613   std::map<std::string, SDNode*> ExternalSymbols;
614   std::map<std::string, SDNode*> TargetExternalSymbols;
615   std::map<std::string, StringSDNode*> StringNodes;
616   std::map<std::pair<unsigned,
617                      std::pair<MVT::ValueType, std::vector<SDOperand> > >,
618            SDNode*> OneResultNodes;
619   std::map<std::pair<unsigned,
620                      std::pair<std::vector<MVT::ValueType>,
621                                std::vector<SDOperand> > >,
622            SDNode*> ArbitraryNodes;
623 };
624
625 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
626   typedef SelectionDAG::allnodes_iterator nodes_iterator;
627   static nodes_iterator nodes_begin(SelectionDAG *G) {
628     return G->allnodes_begin();
629   }
630   static nodes_iterator nodes_end(SelectionDAG *G) {
631     return G->allnodes_end();
632   }
633 };
634
635 }  // end namespace llvm
636
637 #endif