Add new node, SELECT_CC. This node is for targets that don't natively
[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   TargetLowering &TLI;
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
48   // ValueNodes - track SrcValue nodes
49   std::map<std::pair<const Value*, int>, SDNode*> ValueNodes;
50
51 public:
52   SelectionDAG(TargetLowering &tli, MachineFunction &mf) : TLI(tli), MF(mf) {
53     EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
54   }
55   ~SelectionDAG();
56
57   MachineFunction &getMachineFunction() const { return MF; }
58   const TargetMachine &getTarget() const;
59   TargetLowering &getTargetLoweringInfo() const { return TLI; }
60
61   /// viewGraph - Pop up a ghostview window with the DAG rendered using 'dot'.
62   ///
63   void viewGraph();
64
65
66   typedef std::vector<SDNode*>::const_iterator allnodes_iterator;
67   allnodes_iterator allnodes_begin() const { return AllNodes.begin(); }
68   allnodes_iterator allnodes_end() const { return AllNodes.end(); }
69
70   /// getRoot - Return the root tag of the SelectionDAG.
71   ///
72   const SDOperand &getRoot() const { return Root; }
73
74   /// getEntryNode - Return the token chain corresponding to the entry of the
75   /// function.
76   const SDOperand &getEntryNode() const { return EntryNode; }
77
78   /// setRoot - Set the current root tag of the SelectionDAG.
79   ///
80   const SDOperand &setRoot(SDOperand N) { return Root = N; }
81
82   /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
83   /// compatible with the target instruction selector, as indicated by the
84   /// TargetLowering object.
85   ///
86   /// Note that this is an involved process that may invalidate pointers into
87   /// the graph.
88   void Legalize();
89
90   /// RemoveDeadNodes - This method deletes all unreachable nodes in the
91   /// SelectionDAG, including nodes (like loads) that have uses of their token
92   /// chain but no other uses and no side effect.  If a node is passed in as an
93   /// argument, it is used as the seed for node deletion.
94   void RemoveDeadNodes(SDNode *N = 0);
95
96   SDOperand getConstant(uint64_t Val, MVT::ValueType VT);
97   SDOperand getConstantFP(double Val, MVT::ValueType VT);
98   SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT);
99   SDOperand getFrameIndex(int FI, MVT::ValueType VT);
100   SDOperand getConstantPool(unsigned CPIdx, MVT::ValueType VT);
101   SDOperand getBasicBlock(MachineBasicBlock *MBB);
102   SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
103   SDOperand getValueType(MVT::ValueType);
104
105   SDOperand getCopyToReg(SDOperand Chain, SDOperand N, unsigned Reg) {
106     // Note: these are auto-CSE'd because the caller doesn't make requests that
107     // could cause duplicates to occur.
108     SDNode *NN = new RegSDNode(ISD::CopyToReg, Chain, N, Reg);
109     NN->setValueTypes(MVT::Other);
110     AllNodes.push_back(NN);
111     return SDOperand(NN, 0);
112   }
113
114   SDOperand getCopyFromReg(unsigned Reg, MVT::ValueType VT, SDOperand Chain) {
115     // Note: These nodes are auto-CSE'd by the caller of this method.
116     SDNode *NN = new RegSDNode(ISD::CopyFromReg, Chain, Reg);
117     NN->setValueTypes(VT, MVT::Other);
118     AllNodes.push_back(NN);
119     return SDOperand(NN, 0);
120   }
121
122   SDOperand getImplicitDef(SDOperand Chain, unsigned Reg) {
123     // Note: These nodes are auto-CSE'd by the caller of this method.
124     SDNode *NN = new RegSDNode(ISD::ImplicitDef, Chain, Reg);
125     NN->setValueTypes(MVT::Other);
126     AllNodes.push_back(NN);
127     return SDOperand(NN, 0);
128   }
129
130   /// getCall - Note that this destroys the vector of RetVals passed in.
131   ///
132   SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
133                   SDOperand Callee, bool isTailCall = false) {
134     SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, Chain,
135                             Callee);
136     NN->setValueTypes(RetVals);
137     AllNodes.push_back(NN);
138     return NN;
139   }
140
141   /// getCall - This is identical to the one above, and should be used for calls
142   /// where arguments are passed in physical registers.  This destroys the
143   /// RetVals and ArgsInRegs vectors.
144   SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
145                   SDOperand Callee, std::vector<SDOperand> &ArgsInRegs,
146                   bool isTailCall = false) {
147     ArgsInRegs.insert(ArgsInRegs.begin(), Callee);
148     ArgsInRegs.insert(ArgsInRegs.begin(), Chain);
149     SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, ArgsInRegs);
150     NN->setValueTypes(RetVals);
151     AllNodes.push_back(NN);
152     return NN;
153   }
154
155   SDOperand getCondCode(ISD::CondCode Cond);
156
157   /// getZeroExtendInReg - Return the expression required to zero extend the Op
158   /// value assuming it was the smaller SrcTy value.
159   SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
160
161   /// getNode - Gets or creates the specified node.
162   ///
163   SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
164   SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
165   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
166                     SDOperand N1, SDOperand N2);
167   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
168                     SDOperand N1, SDOperand N2, SDOperand N3);
169   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
170                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
171   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
172                     SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
173                     SDOperand N5);
174   SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
175                     std::vector<SDOperand> &Children);
176   SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
177                     std::vector<SDOperand> &Ops);
178
179   /// getSetCC - Helper function to make it easier to build SetCC's if you just
180   /// have an ISD::CondCode instead of an SDOperand.
181   ///
182   SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS,
183                      ISD::CondCode Cond) {
184     return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
185   }
186
187   /// getSelectCC - Helper function to make it easier to build SelectCC's if you
188   /// just have an ISD::CondCode instead of an SDOperand.
189   ///
190   SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
191                         SDOperand True, SDOperand False, ISD::CondCode Cond) {
192     MVT::ValueType VT = True.getValueType();
193     assert(LHS.getValueType() == RHS.getValueType() &&
194            "LHS and RHS of condition must have same type!");
195     assert(True.getValueType() == False.getValueType() &&
196            "True and False arms of SelectCC must have same type!");
197     return getNode(ISD::SELECT_CC, VT, LHS, RHS, True, False,getCondCode(Cond));
198   }
199   
200   /// getLoad - Loads are not normal binary operators: their result type is not
201   /// determined by their operands, and they produce a value AND a token chain.
202   ///
203   SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
204                     SDOperand SV);
205   SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain,
206                        SDOperand Ptr, SDOperand SV, MVT::ValueType EVT);
207
208   // getSrcValue - construct a node to track a Value* through the backend
209   SDOperand getSrcValue(const Value* I, int offset = 0);
210
211   void replaceAllUsesWith(SDOperand Old, SDOperand New) {
212     assert(Old != New && "RAUW self!");
213     assert(0 && "Unimplemented!");
214   }
215
216   void dump() const;
217
218 private:
219   void DeleteNodeIfDead(SDNode *N, void *NodeSet);
220   
221   // Try to simplify a setcc built with the specified operands and cc.  If
222   // unable to simplify it, return a null SDOperand.
223   SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1,
224                           SDOperand N2, ISD::CondCode Cond);
225
226   
227   // Maps to auto-CSE operations.
228   std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >,
229            SDNode *> UnaryOps;
230   std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >,
231            SDNode *> BinaryOps;
232
233   std::vector<CondCodeSDNode*> CondCodeNodes;
234
235   std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >,
236            SDNode *> Loads;
237
238   std::map<const GlobalValue*, SDNode*> GlobalValues;
239   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants;
240   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs;
241   std::map<int, SDNode*> FrameIndices;
242   std::map<unsigned, SDNode*> ConstantPoolIndices;
243   std::map<MachineBasicBlock *, SDNode*> BBNodes;
244   std::vector<SDNode*> ValueTypeNodes;
245   std::map<std::string, SDNode*> ExternalSymbols;
246   std::map<std::pair<unsigned,
247                      std::pair<MVT::ValueType, std::vector<SDOperand> > >,
248            SDNode*> OneResultNodes;
249   std::map<std::pair<unsigned,
250                      std::pair<std::vector<MVT::ValueType>,
251                                std::vector<SDOperand> > >,
252            SDNode*> ArbitraryNodes;
253 };
254
255 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
256   typedef SelectionDAG::allnodes_iterator nodes_iterator;
257   static nodes_iterator nodes_begin(SelectionDAG *G) {
258     return G->allnodes_begin();
259   }
260   static nodes_iterator nodes_end(SelectionDAG *G) {
261     return G->allnodes_end();
262   }
263 };
264
265 }
266
267 #endif