f9d7172672e9ee169168c13b098e02866501cd3a
[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   /// getCall - Note that this destroys the vector of RetVals passed in.
192   ///
193   SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
194                   SDOperand Callee, bool isTailCall = false) {
195     SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, Chain,
196                             Callee);
197     setNodeValueTypes(NN, RetVals);
198     AllNodes.push_back(NN);
199     return NN;
200   }
201   /// getCall - Note that this destroys the vector of RetVals passed in.
202   ///
203   SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
204                   SDOperand Callee, SDOperand Flag, bool isTailCall = false) {
205     SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, Chain,
206                             Callee, Flag);
207     setNodeValueTypes(NN, RetVals);
208     AllNodes.push_back(NN);
209     return NN;
210   }
211   
212   /// getCall - This is identical to the one above, and should be used for calls
213   /// where arguments are passed in physical registers.  This destroys the
214   /// RetVals and ArgsInRegs vectors.
215   SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
216                   SDOperand Callee, std::vector<SDOperand> &ArgsInRegs,
217                   bool isTailCall = false) {
218     ArgsInRegs.insert(ArgsInRegs.begin(), Callee);
219     ArgsInRegs.insert(ArgsInRegs.begin(), Chain);
220     SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, ArgsInRegs);
221     setNodeValueTypes(NN, RetVals);
222     AllNodes.push_back(NN);
223     return NN;
224   }
225
226   SDOperand getCondCode(ISD::CondCode Cond);
227
228   /// getZeroExtendInReg - Return the expression required to zero extend the Op
229   /// value assuming it was the smaller SrcTy value.
230   SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
231
232   /// getNode - Gets or creates the specified node.
233   ///
234   SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
235   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
236   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
237                     SDOperand N1, SDOperand N2);
238   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
239                     SDOperand N1, SDOperand N2, SDOperand N3);
240   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
241                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
242   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
243                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
244                     SDOperand N5);
245   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
246                     std::vector<SDOperand> &Children);
247   SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
248                     std::vector<SDOperand> &Ops);
249
250   /// getSetCC - Helper function to make it easier to build SetCC's if you just
251   /// have an ISD::CondCode instead of an SDOperand.
252   ///
253   SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS,
254                      ISD::CondCode Cond) {
255     return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
256   }
257
258   /// getSelectCC - Helper function to make it easier to build SelectCC's if you
259   /// just have an ISD::CondCode instead of an SDOperand.
260   ///
261   SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
262                         SDOperand True, SDOperand False, ISD::CondCode Cond) {
263     MVT::ValueType VT = True.getValueType();
264     return getNode(ISD::SELECT_CC, VT, LHS, RHS, True, False,getCondCode(Cond));
265   }
266   
267   /// getBR2Way_CC - Helper function to make it easier to build BRTWOWAY_CC
268   /// nodes.
269   ///
270   SDOperand getBR2Way_CC(SDOperand Chain, SDOperand CCNode, SDOperand LHS, 
271                          SDOperand RHS, SDOperand True, SDOperand False) {
272     std::vector<SDOperand> Ops;
273     Ops.push_back(Chain);
274     Ops.push_back(CCNode);
275     Ops.push_back(LHS);
276     Ops.push_back(RHS);
277     Ops.push_back(True);
278     Ops.push_back(False);
279     return getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
280   }
281
282   /// getLoad - Loads are not normal binary operators: their result type is not
283   /// determined by their operands, and they produce a value AND a token chain.
284   ///
285   SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
286                     SDOperand SV);
287   SDOperand getVecLoad(unsigned Count, MVT::ValueType VT, SDOperand Chain, 
288                        SDOperand Ptr, SDOperand SV);
289   SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain,
290                        SDOperand Ptr, SDOperand SV, MVT::ValueType EVT);
291
292   // getSrcValue - construct a node to track a Value* through the backend
293   SDOperand getSrcValue(const Value* I, int offset = 0);
294
295   
296   /// SelectNodeTo - These are used for target selectors to *mutate* the
297   /// specified node to have the specified return type, Target opcode, and
298   /// operands.  Note that target opcodes are stored as
299   /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.  The 0th value
300   /// of the resultant node is returned.
301   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT);
302   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
303                          SDOperand Op1);
304   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
305                          SDOperand Op1, SDOperand Op2);
306   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
307                          SDOperand Op1, SDOperand Op2, SDOperand Op3);
308   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
309                          SDOperand Op1, SDOperand Op2, SDOperand Op3, 
310                          SDOperand Op4);
311   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
312                          SDOperand Op1, SDOperand Op2, SDOperand Op3,
313                          SDOperand Op4, SDOperand Op5);
314   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
315                          SDOperand Op1, SDOperand Op2, SDOperand Op3, 
316                          SDOperand Op4, SDOperand Op5, SDOperand Op6);
317   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
318                          SDOperand Op1, SDOperand Op2, SDOperand Op3,
319                          SDOperand Op4, SDOperand Op5, SDOperand Op6,
320                          SDOperand Op7);
321   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 
322                          SDOperand Op1, SDOperand Op2, SDOperand Op3,
323                          SDOperand Op4, SDOperand Op5, SDOperand Op6,
324                          SDOperand Op7, SDOperand Op8);
325   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1, 
326                          MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
327   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
328                          MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
329                          SDOperand Op3);
330   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
331                          MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
332                          SDOperand Op3, SDOperand Op4);
333   SDOperand SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
334                          MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
335                          SDOperand Op3, SDOperand Op4, SDOperand Op5);
336
337   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT) {
338     return getNode(ISD::BUILTIN_OP_END+Opcode, VT);
339   }
340   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
341                           SDOperand Op1) {
342     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1);
343   }
344   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
345                           SDOperand Op1, SDOperand Op2) {
346     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2);
347   }
348   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
349                           SDOperand Op1, SDOperand Op2, SDOperand Op3) {
350     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3);
351   }
352   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
353                           SDOperand Op1, SDOperand Op2, SDOperand Op3,
354                           SDOperand Op4) {
355     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4);
356   }
357   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
358                           SDOperand Op1, SDOperand Op2, SDOperand Op3,
359                           SDOperand Op4, SDOperand Op5) {
360     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5);
361   }
362   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
363                           SDOperand Op1, SDOperand Op2, SDOperand Op3,
364                           SDOperand Op4, SDOperand Op5, SDOperand Op6) {
365     std::vector<SDOperand> Ops;
366     Ops.reserve(6);
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     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) {
379     std::vector<SDOperand> Ops;
380     Ops.reserve(7);
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     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops);
389   }
390   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
391                           SDOperand Op1, SDOperand Op2, SDOperand Op3,
392                           SDOperand Op4, SDOperand Op5, SDOperand Op6,
393                           SDOperand Op7, SDOperand Op8) {
394     std::vector<SDOperand> Ops;
395     Ops.reserve(8);
396     Ops.push_back(Op1);
397     Ops.push_back(Op2);
398     Ops.push_back(Op3);
399     Ops.push_back(Op4);
400     Ops.push_back(Op5);
401     Ops.push_back(Op6);
402     Ops.push_back(Op7);
403     Ops.push_back(Op8);
404     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops);
405   }
406   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
407                           std::vector<SDOperand> &Ops) {
408     return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops);
409   }
410   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
411                           MVT::ValueType VT2, SDOperand Op1) {
412     std::vector<MVT::ValueType> ResultTys;
413     ResultTys.push_back(VT1);
414     ResultTys.push_back(VT2);
415     std::vector<SDOperand> Ops;
416     Ops.push_back(Op1);
417     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
418   }
419   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
420                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
421     std::vector<MVT::ValueType> ResultTys;
422     ResultTys.push_back(VT1);
423     ResultTys.push_back(VT2);
424     std::vector<SDOperand> Ops;
425     Ops.push_back(Op1);
426     Ops.push_back(Op2);
427     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
428   }
429   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
430                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
431                           SDOperand Op3) {
432     std::vector<MVT::ValueType> ResultTys;
433     ResultTys.push_back(VT1);
434     ResultTys.push_back(VT2);
435     std::vector<SDOperand> Ops;
436     Ops.push_back(Op1);
437     Ops.push_back(Op2);
438     Ops.push_back(Op3);
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) {
444     std::vector<MVT::ValueType> ResultTys;
445     ResultTys.push_back(VT1);
446     ResultTys.push_back(VT2);
447     std::vector<SDOperand> Ops;
448     Ops.push_back(Op1);
449     Ops.push_back(Op2);
450     Ops.push_back(Op3);
451     Ops.push_back(Op4);
452     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
453   }
454   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
455                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
456                           SDOperand Op3, SDOperand Op4, SDOperand Op5) {
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     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
467   }
468   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
469                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
470                           SDOperand Op3, SDOperand Op4, SDOperand Op5,
471                           SDOperand Op6) {
472     std::vector<MVT::ValueType> ResultTys;
473     ResultTys.push_back(VT1);
474     ResultTys.push_back(VT2);
475     std::vector<SDOperand> Ops;
476     Ops.push_back(Op1);
477     Ops.push_back(Op2);
478     Ops.push_back(Op3);
479     Ops.push_back(Op4);
480     Ops.push_back(Op5);
481     Ops.push_back(Op6);
482     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
483   }
484   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
485                           MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
486                           SDOperand Op3, SDOperand Op4, SDOperand Op5,
487                           SDOperand Op6, SDOperand Op7) {
488     std::vector<MVT::ValueType> ResultTys;
489     ResultTys.push_back(VT1);
490     ResultTys.push_back(VT2);
491     std::vector<SDOperand> Ops;
492     Ops.push_back(Op1);
493     Ops.push_back(Op2);
494     Ops.push_back(Op3);
495     Ops.push_back(Op4);
496     Ops.push_back(Op5);
497     Ops.push_back(Op6); 
498     Ops.push_back(Op7);
499    return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
500   }
501   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
502                           MVT::ValueType VT2, MVT::ValueType VT3,
503                           SDOperand Op1, SDOperand Op2) {
504     std::vector<MVT::ValueType> ResultTys;
505     ResultTys.push_back(VT1);
506     ResultTys.push_back(VT2);
507     ResultTys.push_back(VT3);
508     std::vector<SDOperand> Ops;
509     Ops.push_back(Op1);
510     Ops.push_back(Op2);
511     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
512   }
513   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
514                           MVT::ValueType VT2, MVT::ValueType VT3,
515                           SDOperand Op1, SDOperand Op2,
516                           SDOperand Op3, SDOperand Op4, SDOperand Op5,
517                           SDOperand Op6) {
518     std::vector<MVT::ValueType> ResultTys;
519     ResultTys.push_back(VT1);
520     ResultTys.push_back(VT2);
521     ResultTys.push_back(VT3);
522     std::vector<SDOperand> Ops;
523     Ops.push_back(Op1);
524     Ops.push_back(Op2);
525     Ops.push_back(Op3);
526     Ops.push_back(Op4);
527     Ops.push_back(Op5);
528     Ops.push_back(Op6);
529     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
530   }
531   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
532                           MVT::ValueType VT2, MVT::ValueType VT3,
533                           SDOperand Op1, SDOperand Op2,
534                           SDOperand Op3, SDOperand Op4, SDOperand Op5,
535                           SDOperand Op6, SDOperand Op7) {
536     std::vector<MVT::ValueType> ResultTys;
537     ResultTys.push_back(VT1);
538     ResultTys.push_back(VT2);
539     ResultTys.push_back(VT3);
540     std::vector<SDOperand> Ops;
541     Ops.push_back(Op1);
542     Ops.push_back(Op2);
543     Ops.push_back(Op3);
544     Ops.push_back(Op4);
545     Ops.push_back(Op5);
546     Ops.push_back(Op6);
547     Ops.push_back(Op7);
548     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
549   }
550   SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1, 
551                           MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
552     std::vector<MVT::ValueType> ResultTys;
553     ResultTys.push_back(VT1);
554     ResultTys.push_back(VT2);
555     return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
556   }
557   
558   /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
559   /// This can cause recursive merging of nodes in the DAG.  Use the first
560   /// version if 'From' is known to have a single result, use the second
561   /// if you have two nodes with identical results, use the third otherwise.
562   ///
563   /// These methods all take an optional vector, which (if not null) is 
564   /// populated with any nodes that are deleted from the SelectionDAG, due to
565   /// new equivalences that are discovered.
566   ///
567   void ReplaceAllUsesWith(SDOperand From, SDOperand Op,
568                           std::vector<SDNode*> *Deleted = 0);
569   void ReplaceAllUsesWith(SDNode *From, SDNode *To,
570                           std::vector<SDNode*> *Deleted = 0);
571   void ReplaceAllUsesWith(SDNode *From, const std::vector<SDOperand> &To,
572                           std::vector<SDNode*> *Deleted = 0);
573   
574   
575   /// DeleteNode - Remove the specified node from the system.  This node must
576   /// have no referrers.
577   void DeleteNode(SDNode *N);
578   
579   void dump() const;
580
581 private:
582   void RemoveNodeFromCSEMaps(SDNode *N);
583   SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
584   void DestroyDeadNode(SDNode *N);
585   void DeleteNodeNotInCSEMaps(SDNode *N);
586   void setNodeValueTypes(SDNode *N, std::vector<MVT::ValueType> &RetVals);
587   void setNodeValueTypes(SDNode *N, MVT::ValueType VT1, MVT::ValueType VT2);
588   
589   
590   /// SimplifySetCC - Try to simplify a setcc built with the specified operands 
591   /// and cc.  If unable to simplify it, return a null SDOperand.
592   SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1,
593                           SDOperand N2, ISD::CondCode Cond);
594   
595   // List of non-single value types.
596   std::list<std::vector<MVT::ValueType> > VTList;
597   
598   // Maps to auto-CSE operations.
599   std::map<std::pair<unsigned, MVT::ValueType>, SDNode *> NullaryOps;
600   std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >,
601            SDNode *> UnaryOps;
602   std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >,
603            SDNode *> BinaryOps;
604
605   std::map<std::pair<unsigned, MVT::ValueType>, RegisterSDNode*> RegNodes;
606   std::vector<CondCodeSDNode*> CondCodeNodes;
607
608   std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >,
609            SDNode *> Loads;
610
611   std::map<std::pair<const GlobalValue*, int>, SDNode*> GlobalValues;
612   std::map<std::pair<const GlobalValue*, int>, SDNode*> TargetGlobalValues;
613   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants;
614   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> TargetConstants;
615   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs;
616   std::map<int, SDNode*> FrameIndices, TargetFrameIndices;
617   std::map<Constant *, SDNode*> ConstantPoolIndices;
618   std::map<Constant *, SDNode*> TargetConstantPoolIndices;
619   std::map<MachineBasicBlock *, SDNode*> BBNodes;
620   std::vector<SDNode*> ValueTypeNodes;
621   std::map<std::string, SDNode*> ExternalSymbols;
622   std::map<std::string, SDNode*> TargetExternalSymbols;
623   std::map<std::string, StringSDNode*> StringNodes;
624   std::map<std::pair<unsigned,
625                      std::pair<MVT::ValueType, std::vector<SDOperand> > >,
626            SDNode*> OneResultNodes;
627   std::map<std::pair<unsigned,
628                      std::pair<std::vector<MVT::ValueType>,
629                                std::vector<SDOperand> > >,
630            SDNode*> ArbitraryNodes;
631 };
632
633 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
634   typedef SelectionDAG::allnodes_iterator nodes_iterator;
635   static nodes_iterator nodes_begin(SelectionDAG *G) {
636     return G->allnodes_begin();
637   }
638   static nodes_iterator nodes_end(SelectionDAG *G) {
639     return G->allnodes_end();
640   }
641 };
642
643 }  // end namespace llvm
644
645 #endif