6d75e47acfb6d67624e839d15d2680f6d5d3ec85
[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                             unsigned Alignment=0);
125   SDOperand getTargetConstantPool(Constant *C, MVT::ValueType VT,
126                                   unsigned Alignment=0);
127   SDOperand getBasicBlock(MachineBasicBlock *MBB);
128   SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
129   SDOperand getTargetExternalSymbol(const char *Sym, MVT::ValueType VT);
130   SDOperand getValueType(MVT::ValueType);
131   SDOperand getRegister(unsigned Reg, MVT::ValueType VT);
132
133   SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
134     return getNode(ISD::CopyToReg, MVT::Other, Chain,
135                    getRegister(Reg, N.getValueType()), N);
136   }
137
138   // This version of the getCopyToReg method takes an extra operand, which
139   // indicates that there is potentially an incoming flag value (if Flag is not
140   // null) and that there should be a flag result.
141   SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N,
142                          SDOperand Flag) {
143     std::vector<MVT::ValueType> VTs;
144     VTs.push_back(MVT::Other);
145     VTs.push_back(MVT::Flag);
146     std::vector<SDOperand> Ops;
147     Ops.push_back(Chain);
148     Ops.push_back(getRegister(Reg, N.getValueType()));
149     Ops.push_back(N);
150     if (Flag.Val) Ops.push_back(Flag);
151     return getNode(ISD::CopyToReg, VTs, Ops);
152   }
153
154   // Similar to last getCopyToReg() except parameter Reg is a SDOperand
155   SDOperand getCopyToReg(SDOperand Chain, SDOperand Reg, SDOperand N,
156                          SDOperand Flag) {
157     std::vector<MVT::ValueType> VTs;
158     VTs.push_back(MVT::Other);
159     VTs.push_back(MVT::Flag);
160     std::vector<SDOperand> Ops;
161     Ops.push_back(Chain);
162     Ops.push_back(Reg);
163     Ops.push_back(N);
164     if (Flag.Val) Ops.push_back(Flag);
165     return getNode(ISD::CopyToReg, VTs, Ops);
166   }
167   
168   SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
169     std::vector<MVT::ValueType> ResultTys;
170     ResultTys.push_back(VT);
171     ResultTys.push_back(MVT::Other);
172     std::vector<SDOperand> Ops;
173     Ops.push_back(Chain);
174     Ops.push_back(getRegister(Reg, VT));
175     return getNode(ISD::CopyFromReg, ResultTys, Ops);
176   }
177   
178   // This version of the getCopyFromReg method takes an extra operand, which
179   // indicates that there is potentially an incoming flag value (if Flag is not
180   // null) and that there should be a flag result.
181   SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT,
182                            SDOperand Flag) {
183     std::vector<MVT::ValueType> ResultTys;
184     ResultTys.push_back(VT);
185     ResultTys.push_back(MVT::Other);
186     ResultTys.push_back(MVT::Flag);
187     std::vector<SDOperand> Ops;
188     Ops.push_back(Chain);
189     Ops.push_back(getRegister(Reg, VT));
190     if (Flag.Val) Ops.push_back(Flag);
191     return getNode(ISD::CopyFromReg, ResultTys, Ops);
192   }
193
194   SDOperand getCondCode(ISD::CondCode Cond);
195
196   /// getZeroExtendInReg - Return the expression required to zero extend the Op
197   /// value assuming it was the smaller SrcTy value.
198   SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
199
200   /// getNode - Gets or creates the specified node.
201   ///
202   SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
203   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
204   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
205                     SDOperand N1, SDOperand N2);
206   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
207                     SDOperand N1, SDOperand N2, SDOperand N3);
208   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
209                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
210   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
211                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
212                     SDOperand N5);
213   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
214                     std::vector<SDOperand> &Children);
215   SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
216                     std::vector<SDOperand> &Ops);
217
218   /// getSetCC - Helper function to make it easier to build SetCC's if you just
219   /// have an ISD::CondCode instead of an SDOperand.
220   ///
221   SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS,
222                      ISD::CondCode Cond) {
223     return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
224   }
225
226   /// getSelectCC - Helper function to make it easier to build SelectCC's if you
227   /// just have an ISD::CondCode instead of an SDOperand.
228   ///
229   SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
230                         SDOperand True, SDOperand False, ISD::CondCode Cond) {
231     MVT::ValueType VT = True.getValueType();
232     return getNode(ISD::SELECT_CC, VT, LHS, RHS, True, False,getCondCode(Cond));
233   }
234   
235   /// getVAArg - VAArg produces a result and token chain, and takes a pointer
236   /// and a source value as input.
237   SDOperand getVAArg(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
238                      SDOperand SV);
239
240   /// getLoad - Loads are not normal binary operators: their result type is not
241   /// determined by their operands, and they produce a value AND a token chain.
242   ///
243   SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
244                     SDOperand SV);
245   SDOperand getVecLoad(unsigned Count, MVT::ValueType VT, SDOperand Chain, 
246                        SDOperand Ptr, 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   /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
254   /// specified operands.  If the resultant node already exists in the DAG,
255   /// this does not modify the specified node, instead it returns the node that
256   /// already exists.  If the resultant node does not exist in the DAG, the
257   /// input node is returned.  As a degenerate case, if you specify the same
258   /// input operands as the node already has, the input node is returned.
259   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op);
260   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2);
261   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
262                                SDOperand Op3);
263   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
264                                SDOperand Op3, SDOperand Op4);
265   SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
266                                SDOperand Op3, SDOperand Op4, SDOperand Op5);
267   SDOperand UpdateNodeOperands(SDOperand N, const std::vector<SDOperand> &Op);
268   
269   /// SelectNodeTo - These are used for target selectors to *mutate* the
270   /// specified node to have the specified return type, Target opcode, and
271   /// operands.  Note that target opcodes are stored as
272   /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.  The 0th value
273   /// of the resultant node is returned.
274   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT);
275   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
276                          SDOperand Op1);
277   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
278                          SDOperand Op1, SDOperand Op2);
279   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
280                          SDOperand Op1, SDOperand Op2, SDOperand Op3);
281   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
282                          SDOperand Op1, SDOperand Op2, SDOperand Op3, 
283                          SDOperand Op4);
284   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
285                          SDOperand Op1, SDOperand Op2, SDOperand Op3,
286                          SDOperand Op4, SDOperand Op5);
287   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
288                          SDOperand Op1, SDOperand Op2, SDOperand Op3, 
289                          SDOperand Op4, SDOperand Op5, SDOperand Op6);
290   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
291                          SDOperand Op1, SDOperand Op2, SDOperand Op3,
292                          SDOperand Op4, SDOperand Op5, SDOperand Op6,
293                          SDOperand Op7);
294   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
295                          SDOperand Op1, SDOperand Op2, SDOperand Op3,
296                          SDOperand Op4, SDOperand Op5, SDOperand Op6,
297                          SDOperand Op7, SDOperand Op8);
298   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1, 
299                          MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
300   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
301                          MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
302                          SDOperand Op3);
303   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
304                          MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
305                          SDOperand Op3, SDOperand Op4);
306   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
307                          MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
308                          SDOperand Op3, SDOperand Op4, SDOperand Op5);
309
310   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT) {
311     return getNode(ISD::BUILTIN_OP_END+Opcode, VT);
312   }
313   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
314                           SDOperand Op1) {
315     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1);
316   }
317   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
318                           SDOperand Op1, SDOperand Op2) {
319     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2);
320   }
321   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
322                           SDOperand Op1, SDOperand Op2, SDOperand Op3) {
323     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3);
324   }
325   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
326                           SDOperand Op1, SDOperand Op2, SDOperand Op3,
327                           SDOperand Op4) {
328     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4);
329   }
330   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
331                           SDOperand Op1, SDOperand Op2, SDOperand Op3,
332                           SDOperand Op4, SDOperand Op5) {
333     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5);
334   }
335   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
336                           SDOperand Op1, SDOperand Op2, SDOperand Op3,
337                           SDOperand Op4, SDOperand Op5, SDOperand Op6) {
338     std::vector<SDOperand> Ops;
339     Ops.reserve(6);
340     Ops.push_back(Op1);
341     Ops.push_back(Op2);
342     Ops.push_back(Op3);
343     Ops.push_back(Op4);
344     Ops.push_back(Op5);
345     Ops.push_back(Op6);
346     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops);
347   }
348   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
349                           SDOperand Op1, SDOperand Op2, SDOperand Op3,
350                           SDOperand Op4, SDOperand Op5, SDOperand Op6,
351                           SDOperand Op7) {
352     std::vector<SDOperand> Ops;
353     Ops.reserve(7);
354     Ops.push_back(Op1);
355     Ops.push_back(Op2);
356     Ops.push_back(Op3);
357     Ops.push_back(Op4);
358     Ops.push_back(Op5);
359     Ops.push_back(Op6);
360     Ops.push_back(Op7);
361     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops);
362   }
363   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
364                           SDOperand Op1, SDOperand Op2, SDOperand Op3,
365                           SDOperand Op4, SDOperand Op5, SDOperand Op6,
366                           SDOperand Op7, SDOperand Op8) {
367     std::vector<SDOperand> Ops;
368     Ops.reserve(8);
369     Ops.push_back(Op1);
370     Ops.push_back(Op2);
371     Ops.push_back(Op3);
372     Ops.push_back(Op4);
373     Ops.push_back(Op5);
374     Ops.push_back(Op6);
375     Ops.push_back(Op7);
376     Ops.push_back(Op8);
377     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops);
378   }
379   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
380                           std::vector<SDOperand> &Ops) {
381     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops);
382   }
383   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
384                           MVT::ValueType VT2, SDOperand Op1) {
385     std::vector<MVT::ValueType> ResultTys;
386     ResultTys.push_back(VT1);
387     ResultTys.push_back(VT2);
388     std::vector<SDOperand> Ops;
389     Ops.push_back(Op1);
390     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
391   }
392   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
393                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
394     std::vector<MVT::ValueType> ResultTys;
395     ResultTys.push_back(VT1);
396     ResultTys.push_back(VT2);
397     std::vector<SDOperand> Ops;
398     Ops.push_back(Op1);
399     Ops.push_back(Op2);
400     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
401   }
402   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
403                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
404                           SDOperand Op3) {
405     std::vector<MVT::ValueType> ResultTys;
406     ResultTys.push_back(VT1);
407     ResultTys.push_back(VT2);
408     std::vector<SDOperand> Ops;
409     Ops.push_back(Op1);
410     Ops.push_back(Op2);
411     Ops.push_back(Op3);
412     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
413   }
414   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
415                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
416                           SDOperand Op3, SDOperand Op4) {
417     std::vector<MVT::ValueType> ResultTys;
418     ResultTys.push_back(VT1);
419     ResultTys.push_back(VT2);
420     std::vector<SDOperand> Ops;
421     Ops.push_back(Op1);
422     Ops.push_back(Op2);
423     Ops.push_back(Op3);
424     Ops.push_back(Op4);
425     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
426   }
427   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
428                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
429                           SDOperand Op3, SDOperand Op4, SDOperand Op5) {
430     std::vector<MVT::ValueType> ResultTys;
431     ResultTys.push_back(VT1);
432     ResultTys.push_back(VT2);
433     std::vector<SDOperand> Ops;
434     Ops.push_back(Op1);
435     Ops.push_back(Op2);
436     Ops.push_back(Op3);
437     Ops.push_back(Op4);
438     Ops.push_back(Op5);
439     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
440   }
441   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
442                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
443                           SDOperand Op3, SDOperand Op4, SDOperand Op5,
444                           SDOperand Op6) {
445     std::vector<MVT::ValueType> ResultTys;
446     ResultTys.push_back(VT1);
447     ResultTys.push_back(VT2);
448     std::vector<SDOperand> Ops;
449     Ops.push_back(Op1);
450     Ops.push_back(Op2);
451     Ops.push_back(Op3);
452     Ops.push_back(Op4);
453     Ops.push_back(Op5);
454     Ops.push_back(Op6);
455     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
456   }
457   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
458                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
459                           SDOperand Op3, SDOperand Op4, SDOperand Op5,
460                           SDOperand Op6, SDOperand Op7) {
461     std::vector<MVT::ValueType> ResultTys;
462     ResultTys.push_back(VT1);
463     ResultTys.push_back(VT2);
464     std::vector<SDOperand> Ops;
465     Ops.push_back(Op1);
466     Ops.push_back(Op2);
467     Ops.push_back(Op3);
468     Ops.push_back(Op4);
469     Ops.push_back(Op5);
470     Ops.push_back(Op6); 
471     Ops.push_back(Op7);
472    return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
473   }
474   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
475                           MVT::ValueType VT2, MVT::ValueType VT3,
476                           SDOperand Op1, SDOperand Op2) {
477     std::vector<MVT::ValueType> ResultTys;
478     ResultTys.push_back(VT1);
479     ResultTys.push_back(VT2);
480     ResultTys.push_back(VT3);
481     std::vector<SDOperand> Ops;
482     Ops.push_back(Op1);
483     Ops.push_back(Op2);
484     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
485   }
486   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
487                           MVT::ValueType VT2, MVT::ValueType VT3,
488                           SDOperand Op1, SDOperand Op2,
489                           SDOperand Op3, SDOperand Op4, SDOperand Op5) {
490     std::vector<MVT::ValueType> ResultTys;
491     ResultTys.push_back(VT1);
492     ResultTys.push_back(VT2);
493     ResultTys.push_back(VT3);
494     std::vector<SDOperand> Ops;
495     Ops.push_back(Op1);
496     Ops.push_back(Op2);
497     Ops.push_back(Op3);
498     Ops.push_back(Op4);
499     Ops.push_back(Op5);
500     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
501   }
502   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
503                           MVT::ValueType VT2, MVT::ValueType VT3,
504                           SDOperand Op1, SDOperand Op2,
505                           SDOperand Op3, SDOperand Op4, SDOperand Op5,
506                           SDOperand Op6) {
507     std::vector<MVT::ValueType> ResultTys;
508     ResultTys.push_back(VT1);
509     ResultTys.push_back(VT2);
510     ResultTys.push_back(VT3);
511     std::vector<SDOperand> Ops;
512     Ops.push_back(Op1);
513     Ops.push_back(Op2);
514     Ops.push_back(Op3);
515     Ops.push_back(Op4);
516     Ops.push_back(Op5);
517     Ops.push_back(Op6);
518     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
519   }
520   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
521                           MVT::ValueType VT2, MVT::ValueType VT3,
522                           SDOperand Op1, SDOperand Op2,
523                           SDOperand Op3, SDOperand Op4, SDOperand Op5,
524                           SDOperand Op6, SDOperand Op7) {
525     std::vector<MVT::ValueType> ResultTys;
526     ResultTys.push_back(VT1);
527     ResultTys.push_back(VT2);
528     ResultTys.push_back(VT3);
529     std::vector<SDOperand> Ops;
530     Ops.push_back(Op1);
531     Ops.push_back(Op2);
532     Ops.push_back(Op3);
533     Ops.push_back(Op4);
534     Ops.push_back(Op5);
535     Ops.push_back(Op6);
536     Ops.push_back(Op7);
537     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
538   }
539   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1, 
540                           MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
541     std::vector<MVT::ValueType> ResultTys;
542     ResultTys.push_back(VT1);
543     ResultTys.push_back(VT2);
544     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
545   }
546   
547   /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
548   /// This can cause recursive merging of nodes in the DAG.  Use the first
549   /// version if 'From' is known to have a single result, use the second
550   /// if you have two nodes with identical results, use the third otherwise.
551   ///
552   /// These methods all take an optional vector, which (if not null) is 
553   /// populated with any nodes that are deleted from the SelectionDAG, due to
554   /// new equivalences that are discovered.
555   ///
556   void ReplaceAllUsesWith(SDOperand From, SDOperand Op,
557                           std::vector<SDNode*> *Deleted = 0);
558   void ReplaceAllUsesWith(SDNode *From, SDNode *To,
559                           std::vector<SDNode*> *Deleted = 0);
560   void ReplaceAllUsesWith(SDNode *From, const std::vector<SDOperand> &To,
561                           std::vector<SDNode*> *Deleted = 0);
562   
563   
564   /// DeleteNode - Remove the specified node from the system.  This node must
565   /// have no referrers.
566   void DeleteNode(SDNode *N);
567   
568   void dump() const;
569
570 private:
571   void RemoveNodeFromCSEMaps(SDNode *N);
572   SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
573   SDNode **FindModifiedNodeSlot(SDNode *N, SDOperand Op);
574   SDNode **FindModifiedNodeSlot(SDNode *N, SDOperand Op1, SDOperand Op2);
575   SDNode **FindModifiedNodeSlot(SDNode *N, const std::vector<SDOperand> &Ops);
576
577   void DestroyDeadNode(SDNode *N);
578   void DeleteNodeNotInCSEMaps(SDNode *N);
579   void setNodeValueTypes(SDNode *N, std::vector<MVT::ValueType> &RetVals);
580   void setNodeValueTypes(SDNode *N, MVT::ValueType VT1, MVT::ValueType VT2);
581   
582   
583   /// SimplifySetCC - Try to simplify a setcc built with the specified operands 
584   /// and cc.  If unable to simplify it, return a null SDOperand.
585   SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1,
586                           SDOperand N2, ISD::CondCode Cond);
587   
588   // List of non-single value types.
589   std::list<std::vector<MVT::ValueType> > VTList;
590   
591   // Maps to auto-CSE operations.
592   std::map<std::pair<unsigned, MVT::ValueType>, SDNode *> NullaryOps;
593   std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >,
594            SDNode *> UnaryOps;
595   std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >,
596            SDNode *> BinaryOps;
597
598   std::map<std::pair<unsigned, MVT::ValueType>, RegisterSDNode*> RegNodes;
599   std::vector<CondCodeSDNode*> CondCodeNodes;
600
601   std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >,
602            SDNode *> Loads;
603
604   std::map<std::pair<const GlobalValue*, int>, SDNode*> GlobalValues;
605   std::map<std::pair<const GlobalValue*, int>, SDNode*> TargetGlobalValues;
606   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants;
607   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> TargetConstants;
608   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs;
609   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> TargetConstantFPs;
610   std::map<int, SDNode*> FrameIndices, TargetFrameIndices;
611   std::map<std::pair<Constant *, unsigned>, SDNode*> ConstantPoolIndices;
612   std::map<std::pair<Constant *, unsigned>, SDNode*> TargetConstantPoolIndices;
613   std::map<MachineBasicBlock *, SDNode*> BBNodes;
614   std::vector<SDNode*> ValueTypeNodes;
615   std::map<std::string, SDNode*> ExternalSymbols;
616   std::map<std::string, SDNode*> TargetExternalSymbols;
617   std::map<std::string, StringSDNode*> StringNodes;
618   std::map<std::pair<unsigned,
619                      std::pair<MVT::ValueType, std::vector<SDOperand> > >,
620            SDNode*> OneResultNodes;
621   std::map<std::pair<unsigned,
622                      std::pair<std::vector<MVT::ValueType>,
623                                std::vector<SDOperand> > >,
624            SDNode*> ArbitraryNodes;
625 };
626
627 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
628   typedef SelectionDAG::allnodes_iterator nodes_iterator;
629   static nodes_iterator nodes_begin(SelectionDAG *G) {
630     return G->allnodes_begin();
631   }
632   static nodes_iterator nodes_end(SelectionDAG *G) {
633     return G->allnodes_end();
634   }
635 };
636
637 }  // end namespace llvm
638
639 #endif