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