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"
19 #include "llvm/ADT/ilist"
23 #include <string> // FIXME remove eventually, turning map into const char* map.
28 class MachineFunction;
30 /// SelectionDAG class - This is used to represent a portion of an LLVM function
31 /// in a low-level Data Dependence DAG representation suitable for instruction
32 /// selection. This DAG is constructed as the first step of instruction
33 /// selection in order to allow implementation of machine specific optimizations
34 /// and code simplifications.
36 /// The representation used by the SelectionDAG is a target-independent
37 /// representation, which has some similarities to the GCC RTL representation,
38 /// but is significantly more simple, powerful, and is a graph form instead of a
45 // Root - The root of the entire DAG. EntryNode - The starting token.
46 SDOperand Root, EntryNode;
48 // AllNodes - A linked list of nodes in the current DAG.
49 ilist<SDNode> AllNodes;
51 // ValueNodes - track SrcValue nodes
52 std::map<std::pair<const Value*, int>, SDNode*> ValueNodes;
55 SelectionDAG(TargetLowering &tli, MachineFunction &mf) : TLI(tli), MF(mf) {
56 EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
60 MachineFunction &getMachineFunction() const { return MF; }
61 const TargetMachine &getTarget() const;
62 TargetLowering &getTargetLoweringInfo() const { return TLI; }
64 /// viewGraph - Pop up a ghostview window with the DAG rendered using 'dot'.
69 typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
70 allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
71 allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
72 typedef ilist<SDNode>::iterator allnodes_iterator;
73 allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
74 allnodes_iterator allnodes_end() { return AllNodes.end(); }
76 /// getRoot - Return the root tag of the SelectionDAG.
78 const SDOperand &getRoot() const { return Root; }
80 /// getEntryNode - Return the token chain corresponding to the entry of the
82 const SDOperand &getEntryNode() const { return EntryNode; }
84 /// setRoot - Set the current root tag of the SelectionDAG.
86 const SDOperand &setRoot(SDOperand N) { return Root = N; }
88 /// Combine - This iterates over the nodes in the SelectionDAG, folding
89 /// certain types of nodes together, or eliminating superfluous nodes. When
90 /// the AfterLegalize argument is set to 'true', Combine takes care not to
91 /// generate any nodes that will be illegal on the target.
92 void Combine(bool AfterLegalize);
94 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
95 /// compatible with the target instruction selector, as indicated by the
96 /// TargetLowering object.
98 /// Note that this is an involved process that may invalidate pointers into
102 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
103 /// SelectionDAG, including nodes (like loads) that have uses of their token
104 /// chain but no other uses and no side effect. If a node is passed in as an
105 /// argument, it is used as the seed for node deletion.
106 void RemoveDeadNodes(SDNode *N = 0);
108 SDOperand getConstant(uint64_t Val, MVT::ValueType VT);
109 SDOperand getTargetConstant(uint64_t Val, MVT::ValueType VT);
110 SDOperand getConstantFP(double Val, MVT::ValueType VT);
111 SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT);
112 SDOperand getTargetGlobalAddress(const GlobalValue *GV, MVT::ValueType VT);
113 SDOperand getFrameIndex(int FI, MVT::ValueType VT);
114 SDOperand getTargetFrameIndex(int FI, MVT::ValueType VT);
115 SDOperand getConstantPool(Constant *C, MVT::ValueType VT);
116 SDOperand getTargetConstantPool(Constant *C, MVT::ValueType VT);
117 SDOperand getBasicBlock(MachineBasicBlock *MBB);
118 SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
119 SDOperand getTargetExternalSymbol(const char *Sym, MVT::ValueType VT);
120 SDOperand getValueType(MVT::ValueType);
121 SDOperand getRegister(unsigned Reg, MVT::ValueType VT);
123 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
124 return getNode(ISD::CopyToReg, MVT::Other, Chain,
125 getRegister(Reg, N.getValueType()), N);
128 // This version of the getCopyToReg method takes an extra operand, which
129 // indicates that there is potentially an incoming flag value (if Flag is not
130 // null) and that there should be a flag result.
131 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N,
133 std::vector<MVT::ValueType> VTs;
134 VTs.push_back(MVT::Other);
135 VTs.push_back(MVT::Flag);
136 std::vector<SDOperand> Ops;
137 Ops.push_back(Chain);
138 Ops.push_back(getRegister(Reg, N.getValueType()));
140 if (Flag.Val) Ops.push_back(Flag);
141 return getNode(ISD::CopyToReg, VTs, Ops);
144 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
145 std::vector<MVT::ValueType> ResultTys;
146 ResultTys.push_back(VT);
147 ResultTys.push_back(MVT::Other);
148 std::vector<SDOperand> Ops;
149 Ops.push_back(Chain);
150 Ops.push_back(getRegister(Reg, VT));
151 return getNode(ISD::CopyFromReg, ResultTys, Ops);
154 // This version of the getCopyFromReg method takes an extra operand, which
155 // indicates that there is potentially an incoming flag value (if Flag is not
156 // null) and that there should be a flag result.
157 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT,
159 std::vector<MVT::ValueType> ResultTys;
160 ResultTys.push_back(VT);
161 ResultTys.push_back(MVT::Other);
162 ResultTys.push_back(MVT::Flag);
163 std::vector<SDOperand> Ops;
164 Ops.push_back(Chain);
165 Ops.push_back(getRegister(Reg, VT));
166 if (Flag.Val) Ops.push_back(Flag);
167 return getNode(ISD::CopyFromReg, ResultTys, Ops);
170 SDOperand getImplicitDef(SDOperand Chain, unsigned Reg, MVT::ValueType VT) {
171 return getNode(ISD::ImplicitDef, MVT::Other, Chain, getRegister(Reg, VT));
174 /// getCall - Note that this destroys the vector of RetVals passed in.
176 SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
177 SDOperand Callee, bool isTailCall = false) {
178 SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, Chain,
180 setNodeValueTypes(NN, RetVals);
181 AllNodes.push_back(NN);
185 /// getCall - This is identical to the one above, and should be used for calls
186 /// where arguments are passed in physical registers. This destroys the
187 /// RetVals and ArgsInRegs vectors.
188 SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain,
189 SDOperand Callee, std::vector<SDOperand> &ArgsInRegs,
190 bool isTailCall = false) {
191 ArgsInRegs.insert(ArgsInRegs.begin(), Callee);
192 ArgsInRegs.insert(ArgsInRegs.begin(), Chain);
193 SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, ArgsInRegs);
194 setNodeValueTypes(NN, RetVals);
195 AllNodes.push_back(NN);
199 SDOperand getCondCode(ISD::CondCode Cond);
201 /// getZeroExtendInReg - Return the expression required to zero extend the Op
202 /// value assuming it was the smaller SrcTy value.
203 SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy);
205 /// getNode - Gets or creates the specified node.
207 SDOperand getNode(unsigned Opcode, MVT::ValueType VT);
208 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N);
209 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
210 SDOperand N1, SDOperand N2);
211 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
212 SDOperand N1, SDOperand N2, SDOperand N3);
213 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
214 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
215 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
216 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
218 SDOperand getNode(unsigned Opcode, MVT::ValueType VT,
219 std::vector<SDOperand> &Children);
220 SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys,
221 std::vector<SDOperand> &Ops);
223 /// getSetCC - Helper function to make it easier to build SetCC's if you just
224 /// have an ISD::CondCode instead of an SDOperand.
226 SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS,
227 ISD::CondCode Cond) {
228 return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
231 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
232 /// just have an ISD::CondCode instead of an SDOperand.
234 SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
235 SDOperand True, SDOperand False, ISD::CondCode Cond) {
236 MVT::ValueType VT = True.getValueType();
237 return getNode(ISD::SELECT_CC, VT, LHS, RHS, True, False,getCondCode(Cond));
240 /// getBR2Way_CC - Helper function to make it easier to build BRTWOWAY_CC
243 SDOperand getBR2Way_CC(SDOperand Chain, SDOperand CCNode, SDOperand LHS,
244 SDOperand RHS, SDOperand True, SDOperand False) {
245 std::vector<SDOperand> Ops;
246 Ops.push_back(Chain);
247 Ops.push_back(CCNode);
251 Ops.push_back(False);
252 return getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
255 /// getLoad - Loads are not normal binary operators: their result type is not
256 /// determined by their operands, and they produce a value AND a token chain.
258 SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr,
260 SDOperand getVecLoad(unsigned Count, MVT::ValueType VT, SDOperand Chain,
261 SDOperand Ptr, SDOperand SV);
262 SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain,
263 SDOperand Ptr, SDOperand SV, MVT::ValueType EVT);
265 // getSrcValue - construct a node to track a Value* through the backend
266 SDOperand getSrcValue(const Value* I, int offset = 0);
269 /// SelectNodeTo - These are used for target selectors to *mutate* the
270 /// specified node to have the specified return type, Target opcode, and
271 /// operands. Note that target opcodes are stored as
272 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
273 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT);
274 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
276 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
277 SDOperand Op1, SDOperand Op2);
278 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
279 SDOperand Op1, SDOperand Op2, SDOperand Op3);
280 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
281 SDOperand Op1, SDOperand Op2, SDOperand Op3, SDOperand Op4);
282 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT,
283 SDOperand Op1, SDOperand Op2, SDOperand Op3, SDOperand Op4,
285 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
286 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2);
287 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1,
288 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
291 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT) {
292 return getNode(ISD::BUILTIN_OP_END+Opcode, VT);
294 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
296 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1);
298 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
299 SDOperand Op1, SDOperand Op2) {
300 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2);
302 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
303 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
304 std::vector<MVT::ValueType> ResultTys;
305 ResultTys.push_back(VT1);
306 ResultTys.push_back(VT2);
307 std::vector<SDOperand> Ops;
310 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
312 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
313 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
314 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3);
316 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
317 SDOperand Op1, SDOperand Op2, SDOperand Op3,
319 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4);
321 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
322 SDOperand Op1, SDOperand Op2, SDOperand Op3,
323 SDOperand Op4, SDOperand Op5) {
324 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5);
326 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT,
327 std::vector<SDOperand> &Ops) {
328 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops);
330 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1,
331 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
332 std::vector<MVT::ValueType> ResultTys;
333 ResultTys.push_back(VT1);
334 ResultTys.push_back(VT2);
335 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops);
338 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
339 /// This can cause recursive merging of nodes in the DAG. Use the first
340 /// version if 'From' is known to have a single result, use the second
341 /// if you have two nodes with identical results, use the third otherwise.
343 /// These methods all take an optional vector, which (if not null) is
344 /// populated with any nodes that are deleted from the SelectionDAG, due to
345 /// new equivalences that are discovered.
347 void ReplaceAllUsesWith(SDOperand From, SDOperand Op,
348 std::vector<SDNode*> *Deleted = 0);
349 void ReplaceAllUsesWith(SDNode *From, SDNode *To,
350 std::vector<SDNode*> *Deleted = 0);
351 void ReplaceAllUsesWith(SDNode *From, const std::vector<SDOperand> &To,
352 std::vector<SDNode*> *Deleted = 0);
355 /// DeleteNode - Remove the specified node from the system. This node must
356 /// have no referrers.
357 void DeleteNode(SDNode *N);
362 void RemoveNodeFromCSEMaps(SDNode *N);
363 SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
364 void DestroyDeadNode(SDNode *N);
365 void DeleteNodeNotInCSEMaps(SDNode *N);
366 void setNodeValueTypes(SDNode *N, std::vector<MVT::ValueType> &RetVals);
367 void setNodeValueTypes(SDNode *N, MVT::ValueType VT1, MVT::ValueType VT2);
370 /// SimplifySetCC - Try to simplify a setcc built with the specified operands
371 /// and cc. If unable to simplify it, return a null SDOperand.
372 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1,
373 SDOperand N2, ISD::CondCode Cond);
375 // List of non-single value types.
376 std::list<std::vector<MVT::ValueType> > VTList;
378 // Maps to auto-CSE operations.
379 std::map<std::pair<unsigned, MVT::ValueType>, SDNode *> NullaryOps;
380 std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >,
382 std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >,
385 std::map<std::pair<unsigned, MVT::ValueType>, RegisterSDNode*> RegNodes;
386 std::vector<CondCodeSDNode*> CondCodeNodes;
388 std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >,
391 std::map<const GlobalValue*, SDNode*> GlobalValues;
392 std::map<const GlobalValue*, SDNode*> TargetGlobalValues;
393 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants;
394 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> TargetConstants;
395 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs;
396 std::map<int, SDNode*> FrameIndices, TargetFrameIndices;
397 std::map<Constant *, SDNode*> ConstantPoolIndices;
398 std::map<Constant *, SDNode*> TargetConstantPoolIndices;
399 std::map<MachineBasicBlock *, SDNode*> BBNodes;
400 std::vector<SDNode*> ValueTypeNodes;
401 std::map<std::string, SDNode*> ExternalSymbols;
402 std::map<std::string, SDNode*> TargetExternalSymbols;
403 std::map<std::pair<unsigned,
404 std::pair<MVT::ValueType, std::vector<SDOperand> > >,
405 SDNode*> OneResultNodes;
406 std::map<std::pair<unsigned,
407 std::pair<std::vector<MVT::ValueType>,
408 std::vector<SDOperand> > >,
409 SDNode*> ArbitraryNodes;
412 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
413 typedef SelectionDAG::allnodes_iterator nodes_iterator;
414 static nodes_iterator nodes_begin(SelectionDAG *G) {
415 return G->allnodes_begin();
417 static nodes_iterator nodes_end(SelectionDAG *G) {
418 return G->allnodes_end();
422 } // end namespace llvm