Add new SIGN_EXTEND_INREG, ZERO_EXTEND_INREG, and FP_ROUND_INREG operators.
[oota-llvm.git] / include / llvm / CodeGen / SelectionDAG.h
1 //===-- llvm/CodeGen/SelectionDAG.h - InstSelection DAG ---------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 // 
10 // This file declares the SelectionDAG class, and transitively defines the
11 // SDNode class and subclasses.
12 //   
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CODEGEN_SELECTIONDAG_H
16 #define LLVM_CODEGEN_SELECTIONDAG_H
17
18 #include "llvm/CodeGen/SelectionDAGNodes.h"
19 #include <map>
20 #include <string> // FIXME remove eventually, turning map into const char* map.
21
22 namespace llvm {
23   class TargetLowering;
24   class TargetMachine;
25   class MachineFunction;
26
27 /// SelectionDAG class - This is used to represent a portion of an LLVM function
28 /// in a low-level Data Dependence DAG representation suitable for instruction
29 /// selection.  This DAG is constructed as the first step of instruction
30 /// selection in order to allow implementation of machine specific optimizations
31 /// and code simplifications.
32 ///
33 /// The representation used by the SelectionDAG is a target-independent
34 /// representation, which has some similarities to the GCC RTL representation,
35 /// but is significantly more simple, powerful, and is a graph form instead of a
36 /// linear form.
37 ///
38 class SelectionDAG {
39   const TargetMachine &TM;
40   MachineFunction &MF;
41
42   // Root - The root of the entire DAG.  EntryNode - The starting token.
43   SDOperand Root, EntryNode;
44
45   // AllNodes - All of the nodes in the DAG
46   std::vector<SDNode*> AllNodes;
47 public:
48   SelectionDAG(const TargetMachine &tm, MachineFunction &mf) : TM(tm), MF(mf) {
49     EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
50   }
51   ~SelectionDAG();
52
53   MachineFunction &getMachineFunction() const { return MF; }
54   const TargetMachine &getTarget() { return TM; }
55
56   /// viewGraph - Pop up a ghostview window with the DAG rendered using 'dot'.
57   ///
58   void viewGraph();
59
60
61   typedef std::vector<SDNode*>::const_iterator allnodes_iterator;
62   allnodes_iterator allnodes_begin() const { return AllNodes.begin(); }
63   allnodes_iterator allnodes_end() const { return AllNodes.end(); }
64   
65   /// getRoot - Return the root tag of the SelectionDAG.
66   ///
67   const SDOperand &getRoot() const { return Root; }
68
69   /// getEntryNode - Return the token chain corresponding to the entry of the
70   /// function.
71   const SDOperand &getEntryNode() const { return EntryNode; }
72
73   /// setRoot - Set the current root tag of the SelectionDAG.
74   ///
75   const SDOperand &setRoot(SDOperand N) { return Root = N; }
76
77   /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
78   /// compatible with the target instruction selector, as indicated by the
79   /// TargetLowering object.
80   ///
81   /// Note that this is an involved process that may invalidate pointers into
82   /// the graph.
83   void Legalize(TargetLowering &TLI);
84
85   /// RemoveDeadNodes - This method deletes all unreachable nodes in the
86   /// SelectionDAG, including nodes (like loads) that have uses of their token
87   /// chain but no other uses and no side effect.  If a node is passed in as an
88   /// argument, it is used as the seed for node deletion.
89   void RemoveDeadNodes(SDNode *N = 0);
90
91   SDOperand getConstant(uint64_t Val, MVT::ValueType VT);
92   SDOperand getConstantFP(double Val, MVT::ValueType VT);
93   SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT);
94   SDOperand getFrameIndex(int FI, MVT::ValueType VT);
95   SDOperand getConstantPool(unsigned CPIdx, MVT::ValueType VT);
96   SDOperand getBasicBlock(MachineBasicBlock *MBB);
97   SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
98
99   SDOperand getCopyToReg(SDOperand Chain, SDOperand N, unsigned Reg) {
100     // Note: these are auto-CSE'd because the caller doesn't make requests that
101     // could cause duplicates to occur.
102     SDNode *NN = new RegSDNode(ISD::CopyToReg, Chain, N, Reg);
103     NN->setValueTypes(MVT::Other);
104     AllNodes.push_back(NN);
105     return SDOperand(NN, 0);
106   }
107
108   SDOperand getCopyFromReg(unsigned Reg, MVT::ValueType VT, SDOperand Chain) {
109     // Note: These nodes are auto-CSE'd by the caller of this method.
110     SDNode *NN = new RegSDNode(ISD::CopyFromReg, Chain, Reg);
111     NN->setValueTypes(VT, MVT::Other);
112     AllNodes.push_back(NN);
113     return SDOperand(NN, 0);
114   }
115
116   SDOperand getImplicitDef(SDOperand Chain, unsigned Reg) {
117     // Note: These nodes are auto-CSE'd by the caller of this method.
118     SDNode *NN = new RegSDNode(ISD::ImplicitDef, Chain, Reg);
119     NN->setValueTypes(MVT::Other);
120     AllNodes.push_back(NN);
121     return SDOperand(NN, 0);
122   }
123
124   /// getCall - Note that this destroys the vector of RetVals passed in.
125   ///
126   SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
127                   SDOperand Callee) {
128     SDNode *NN = new SDNode(ISD::CALL, Chain, Callee);
129     NN->setValueTypes(RetVals);
130     AllNodes.push_back(NN);
131     return NN;
132   }
133
134   SDOperand getSetCC(ISD::CondCode, SDOperand LHS, SDOperand RHS);
135
136   /// getNode - Gets or creates the specified node.
137   ///
138   SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
139   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
140   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
141                     SDOperand N1, SDOperand N2);
142   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
143                     SDOperand N1, SDOperand N2, SDOperand N3);
144   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
145                     std::vector<SDOperand> &Children);
146
147   // getNode - These versions take an extra value type for extending and
148   // truncating loads, stores, rounds, extends etc.
149   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N1,
150                     SDOperand N2, MVT::ValueType EVT);
151   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
152                     SDOperand N, MVT::ValueType EVT);
153   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N1,
154                     SDOperand N2, SDOperand N3, MVT::ValueType EVT);
155
156   /// getLoad - Loads are not normal binary operators: their result type is not
157   /// determined by their operands, and they produce a value AND a token chain.
158   ///
159   SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr);
160
161   void replaceAllUsesWith(SDOperand Old, SDOperand New) {
162     assert(Old != New && "RAUW self!");
163     assert(0 && "Unimplemented!");
164   }
165
166   void dump() const;
167
168 private:
169   void DeleteNodeIfDead(SDNode *N, void *NodeSet);
170
171   // Maps to auto-CSE operations.
172   std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >,
173            SDNode *> UnaryOps;
174   std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >,
175            SDNode *> BinaryOps;
176
177   std::map<std::pair<std::pair<SDOperand, SDOperand>, ISD::CondCode>,
178            SetCCSDNode*> SetCCs;
179
180   std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >,
181            SDNode *> Loads;
182
183   std::map<const GlobalValue*, SDNode*> GlobalValues;
184   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants;
185   std::map<std::pair<double, MVT::ValueType>, SDNode*> ConstantFPs;
186   std::map<int, SDNode*> FrameIndices;
187   std::map<unsigned, SDNode*> ConstantPoolIndices;
188   std::map<MachineBasicBlock *, SDNode*> BBNodes;
189   std::map<std::string, SDNode*> ExternalSymbols;
190   struct EVTStruct {
191     unsigned Opcode;
192     MVT::ValueType VT, EVT;
193     std::vector<SDOperand> Ops;
194     bool operator<(const EVTStruct &RHS) const {
195       if (Opcode < RHS.Opcode) return true;
196       if (Opcode > RHS.Opcode) return false;
197       if (VT < RHS.VT) return true;
198       if (VT > RHS.VT) return false;
199       if (EVT < RHS.EVT) return true;
200       if (EVT > RHS.EVT) return false;
201       return Ops < RHS.Ops;
202     }
203   };
204   std::map<EVTStruct, SDNode*> MVTSDNodes;
205 };
206
207 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
208   typedef SelectionDAG::allnodes_iterator nodes_iterator;
209   static nodes_iterator nodes_begin(SelectionDAG *G) {
210     return G->allnodes_begin();
211   }
212   static nodes_iterator nodes_end(SelectionDAG *G) {
213     return G->allnodes_end();
214   }
215 };
216
217 }
218
219 #endif