1 //===-- llvm/CodeGen/SelectionDAG.h - InstSelection DAG ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file declares the SelectionDAG class, and transitively defines the
11 // SDNode class and subclasses.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_SELECTIONDAG_H
16 #define LLVM_CODEGEN_SELECTIONDAG_H
18 #include "llvm/CodeGen/SelectionDAGNodes.h"
20 #include <string> // FIXME remove eventually, turning map into const char* map.
25 class MachineFunction;
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.
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
42 // Root - The root of the entire DAG. EntryNode - The starting token.
43 SDOperand Root, EntryNode;
45 // AllNodes - All of the nodes in the DAG
46 std::vector<SDNode*> AllNodes;
48 // ValueNodes - track SrcValue nodes
49 std::map<std::pair<const Value*, int>, SDNode*> ValueNodes;
52 SelectionDAG(TargetLowering &tli, MachineFunction &mf) : TLI(tli), MF(mf) {
53 EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
57 MachineFunction &getMachineFunction() const { return MF; }
58 const TargetMachine &getTarget() const;
59 TargetLowering &getTargetLoweringInfo() const { return TLI; }
61 /// viewGraph - Pop up a ghostview window with the DAG rendered using 'dot'.
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 unsigned allnodes_size() const { return AllNodes.size(); }
71 /// getRoot - Return the root tag of the SelectionDAG.
73 const SDOperand &getRoot() const { return Root; }
75 /// getEntryNode - Return the token chain corresponding to the entry of the
77 const SDOperand &getEntryNode() const { return EntryNode; }
79 /// setRoot - Set the current root tag of the SelectionDAG.
81 const SDOperand &setRoot(SDOperand N) { return Root = N; }
83 /// Combine - This iterates over the nodes in the SelectionDAG, folding
84 /// certain types of nodes together, or eliminating superfluous nodes. When
85 /// the AfterLegalize argument is set to 'true', Combine takes care not to
86 /// generate any nodes that will be illegal on the target.
87 void Combine(bool AfterLegalize);
89 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
90 /// compatible with the target instruction selector, as indicated by the
91 /// TargetLowering object.
93 /// Note that this is an involved process that may invalidate pointers into
97 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
98 /// SelectionDAG, including nodes (like loads) that have uses of their token
99 /// chain but no other uses and no side effect. If a node is passed in as an
100 /// argument, it is used as the seed for node deletion.
101 void RemoveDeadNodes(SDNode *N = 0);
103 SDOperand getConstant(uint64_t Val, MVT::ValueType VT);
104 SDOperand getTargetConstant(uint64_t Val, MVT::ValueType VT);
105 SDOperand getConstantFP(double Val, MVT::ValueType VT);
106 SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT);
107 SDOperand getTargetGlobalAddress(const GlobalValue *GV, MVT::ValueType VT);
108 SDOperand getFrameIndex(int FI, MVT::ValueType VT);
109 SDOperand getTargetFrameIndex(int FI, MVT::ValueType VT);
110 SDOperand getConstantPool(Constant *C, MVT::ValueType VT);
111 SDOperand getTargetConstantPool(Constant *C, MVT::ValueType VT);
112 SDOperand getBasicBlock(MachineBasicBlock *MBB);
113 SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
114 SDOperand getValueType(MVT::ValueType);
115 SDOperand getRegister(unsigned Reg, MVT::ValueType VT);
117 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
118 return getNode(ISD::CopyToReg, MVT::Other, Chain,
119 getRegister(Reg, N.getValueType()), N);
122 // This version of the getCopyToReg method takes an extra operand, which
123 // indicates that there is potentially an incoming flag value (if Flag is not
124 // null) and that there should be a flag result.
125 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N,
127 std::vector<MVT::ValueType> VTs;
128 VTs.push_back(MVT::Other);
129 VTs.push_back(MVT::Flag);
130 std::vector<SDOperand> Ops;
131 Ops.push_back(Chain);
132 Ops.push_back(getRegister(Reg, N.getValueType()));
134 if (Flag.Val) Ops.push_back(Flag);
135 return getNode(ISD::CopyToReg, VTs, Ops);
138 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
139 std::vector<MVT::ValueType> ResultTys;
140 ResultTys.push_back(VT);
141 ResultTys.push_back(MVT::Other);
142 std::vector<SDOperand> Ops;
143 Ops.push_back(Chain);
144 Ops.push_back(getRegister(Reg, VT));
145 return getNode(ISD::CopyFromReg, ResultTys, Ops);
148 // This version of the getCopyFromReg method takes an extra operand, which
149 // indicates that there is potentially an incoming flag value (if Flag is not
150 // null) and that there should be a flag result.
151 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT,
153 std::vector<MVT::ValueType> ResultTys;
154 ResultTys.push_back(VT);
155 ResultTys.push_back(MVT::Other);
156 ResultTys.push_back(MVT::Flag);
157 std::vector<SDOperand> Ops;
158 Ops.push_back(Chain);
159 Ops.push_back(getRegister(Reg, VT));
160 if (Flag.Val) Ops.push_back(Flag);
161 return getNode(ISD::CopyFromReg, ResultTys, Ops);
164 SDOperand getImplicitDef(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
165 return getNode(ISD::ImplicitDef, MVT::Other, Chain, getRegister(Reg, VT));
168 /// getCall - Note that this destroys the vector of RetVals passed in.
170 SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
171 SDOperand Callee, bool isTailCall = false) {
172 SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, Chain,
174 NN->setValueTypes(RetVals);
175 AllNodes.push_back(NN);
179 /// getCall - This is identical to the one above, and should be used for calls
180 /// where arguments are passed in physical registers. This destroys the
181 /// RetVals and ArgsInRegs vectors.
182 SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
183 SDOperand Callee, std::vector<SDOperand> &ArgsInRegs,
184 bool isTailCall = false) {
185 ArgsInRegs.insert(ArgsInRegs.begin(), Callee);
186 ArgsInRegs.insert(ArgsInRegs.begin(), Chain);
187 SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, ArgsInRegs);
188 NN->setValueTypes(RetVals);
189 AllNodes.push_back(NN);
193 SDOperand getCondCode(ISD::CondCode Cond);
195 /// getZeroExtendInReg - Return the expression required to zero extend the Op
196 /// value assuming it was the smaller SrcTy value.
197 SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
199 /// getNode - Gets or creates the specified node.
201 SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
202 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
203 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
204 SDOperand N1, SDOperand N2);
205 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
206 SDOperand N1, SDOperand N2, SDOperand N3);
207 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
208 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
209 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
210 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
212 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
213 std::vector<SDOperand> &Children);
214 SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
215 std::vector<SDOperand> &Ops);
217 /// getSetCC - Helper function to make it easier to build SetCC's if you just
218 /// have an ISD::CondCode instead of an SDOperand.
220 SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS,
221 ISD::CondCode Cond) {
222 return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
225 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
226 /// just have an ISD::CondCode instead of an SDOperand.
228 SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
229 SDOperand True, SDOperand False, ISD::CondCode Cond) {
230 MVT::ValueType VT = True.getValueType();
231 return getNode(ISD::SELECT_CC, VT, LHS, RHS, True, False,getCondCode(Cond));
234 /// getBR2Way_CC - Helper function to make it easier to build BRTWOWAY_CC
237 SDOperand getBR2Way_CC(SDOperand Chain, SDOperand CCNode, SDOperand LHS,
238 SDOperand RHS, SDOperand True, SDOperand False) {
239 std::vector<SDOperand> Ops;
240 Ops.push_back(Chain);
241 Ops.push_back(CCNode);
245 Ops.push_back(False);
246 return getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
249 /// getLoad - Loads are not normal binary operators: their result type is not
250 /// determined by their operands, and they produce a value AND a token chain.
252 SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
254 SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain,
255 SDOperand Ptr, SDOperand SV, MVT::ValueType EVT);
257 // getSrcValue - construct a node to track a Value* through the backend
258 SDOperand getSrcValue(const Value* I, int offset = 0);
261 /// SelectNodeTo - These are used for target selectors to *mutate* the
262 /// specified node to have the specified return type, Target opcode, and
263 /// operands. Note that target opcodes are stored as
264 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
265 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT);
266 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
268 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
269 SDOperand Op1, SDOperand Op2);
270 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
271 SDOperand Op1, SDOperand Op2, SDOperand Op3);
272 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
273 SDOperand Op1, SDOperand Op2, SDOperand Op3, SDOperand Op4);
274 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
275 SDOperand Op1, SDOperand Op2, SDOperand Op3, SDOperand Op4,
277 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
278 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
279 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
280 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
283 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
285 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1);
287 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
288 SDOperand Op1, SDOperand Op2) {
289 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2);
291 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
292 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
293 std::vector<MVT::ValueType> ResultTys;
294 ResultTys.push_back(VT1);
295 ResultTys.push_back(VT2);
296 std::vector<SDOperand> Ops;
299 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
301 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
302 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
303 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3);
305 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
306 SDOperand Op1, SDOperand Op2, SDOperand Op3,
308 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4);
310 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
311 SDOperand Op1, SDOperand Op2, SDOperand Op3,
312 SDOperand Op4, SDOperand Op5) {
313 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5);
315 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
316 std::vector<SDOperand> &Ops) {
317 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops);
319 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
320 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
321 std::vector<MVT::ValueType> ResultTys;
322 ResultTys.push_back(VT1);
323 ResultTys.push_back(VT2);
324 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
327 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
328 /// This can cause recursive merging of nodes in the DAG. Use the first
329 /// version if 'From' is known to have a single result, use the second
330 /// if you have two nodes with identical results, use the third otherwise.
332 /// These methods all take an optional vector, which (if not null) is
333 /// populated with any nodes that are deleted from the SelectionDAG, due to
334 /// new equivalences that are discovered.
336 void ReplaceAllUsesWith(SDOperand From, SDOperand Op,
337 std::vector<SDNode*> *Deleted = 0);
338 void ReplaceAllUsesWith(SDNode *From, SDNode *To,
339 std::vector<SDNode*> *Deleted = 0);
340 void ReplaceAllUsesWith(SDNode *From, const std::vector<SDOperand> &To,
341 std::vector<SDNode*> *Deleted = 0);
344 /// DeleteNode - Remove the specified node from the system. This node must
345 /// have no referrers.
346 void DeleteNode(SDNode *N);
351 void RemoveNodeFromCSEMaps(SDNode *N);
352 SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
353 void DeleteNodeIfDead(SDNode *N, void *NodeSet);
354 void DeleteNodeNotInCSEMaps(SDNode *N);
356 /// SimplifySetCC - Try to simplify a setcc built with the specified operands
357 /// and cc. If unable to simplify it, return a null SDOperand.
358 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1,
359 SDOperand N2, ISD::CondCode Cond);
361 /// SimplifySelectCC - Try to simplify a select_cc built with the specified
362 /// operands and cc. This can be used to simplify both the select_cc node,
363 /// and a select node whose first operand is a setcc.
364 SDOperand SimplifySelectCC(SDOperand N1, SDOperand N2, SDOperand N3,
365 SDOperand N4, ISD::CondCode CC);
367 // Maps to auto-CSE operations.
368 std::map<std::pair<unsigned, MVT::ValueType>, SDNode *> NullaryOps;
369 std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >,
371 std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >,
374 std::map<std::pair<unsigned, MVT::ValueType>, RegisterSDNode*> RegNodes;
375 std::vector<CondCodeSDNode*> CondCodeNodes;
377 std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >,
380 std::map<const GlobalValue*, SDNode*> GlobalValues;
381 std::map<const GlobalValue*, SDNode*> TargetGlobalValues;
382 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants;
383 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> TargetConstants;
384 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs;
385 std::map<int, SDNode*> FrameIndices, TargetFrameIndices;
386 std::map<Constant *, SDNode*> ConstantPoolIndices;
387 std::map<Constant *, SDNode*> TargetConstantPoolIndices;
388 std::map<MachineBasicBlock *, SDNode*> BBNodes;
389 std::vector<SDNode*> ValueTypeNodes;
390 std::map<std::string, SDNode*> ExternalSymbols;
391 std::map<std::pair<unsigned,
392 std::pair<MVT::ValueType, std::vector<SDOperand> > >,
393 SDNode*> OneResultNodes;
394 std::map<std::pair<unsigned,
395 std::pair<std::vector<MVT::ValueType>,
396 std::vector<SDOperand> > >,
397 SDNode*> ArbitraryNodes;
400 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
401 typedef SelectionDAG::allnodes_iterator nodes_iterator;
402 static nodes_iterator nodes_begin(SelectionDAG *G) {
403 return G->allnodes_begin();
405 static nodes_iterator nodes_end(SelectionDAG *G) {
406 return G->allnodes_end();