1 //===-- llvm/CodeGen/SelectionDAG.h - InstSelection DAG Rep. ----*- 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, which is used to represent an LLVM
11 // function in a low-level representation suitable for instruction selection.
12 // This DAG is constructed as the first step of instruction selection in order
13 // to allow implementation of machine specific optimizations and code
16 // The representation used by the SelectionDAG is a target-independent
17 // representation, which is loosly modeled after the GCC RTL representation, but
18 // is significantly simpler.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_CODEGEN_SELECTIONDAG_H
23 #define LLVM_CODEGEN_SELECTIONDAG_H
25 #include "llvm/CodeGen/ValueTypes.h"
26 #include "Support/DataTypes.h"
35 class MachineBasicBlock;
36 class MachineFunction;
38 class SelectionDAGNode;
39 class SelectionDAGBlock;
40 class SelectionDAGBuilder;
41 class SelectionDAGTargetBuilder;
43 /// ISD namespace - This namespace contains an enum which represents all of the
44 /// SelectionDAG node types and value types.
48 // ChainNode nodes are used to sequence operations within a basic block
49 // which cannot be reordered (such as loads, stores, calls, etc).
50 // BlockChainNodes are used to connect the DAG's for different basic blocks
52 ChainNode, BlockChainNode,
54 // ProtoNodes are nodes that are only half way constructed.
58 Constant, FrameIndex, BasicBlock,
60 // Simple binary arithmetic operators
61 Plus, Minus, Times, SDiv, UDiv, SRem, URem,
67 SetEQ, SetNE, SetLT, SetLE, SetGT, SetGE,
69 // Control flow instructions
70 Br, BrCond, Switch, Ret, RetVoid,
73 Load, Store, PHI, Call,
75 // Unknown operators, of a specified arity
81 friend class SelectionDAGBuilder;
83 const TargetMachine &TM;
84 MVT::ValueType PointerType; // The ValueType the target uses for pointers
86 // ValueMap - The SelectionDAGNode for each LLVM value in the function.
87 std::map<const Value*, SelectionDAGNode*> ValueMap;
89 // BlockMap - The MachineBasicBlock created for each LLVM BasicBlock
90 std::map<const BasicBlock*, MachineBasicBlock*> BlockMap;
92 // Root - The root of the entire DAG
93 SelectionDAGNode *Root;
95 // AllNodes - All of the nodes in the DAG
96 std::vector<SelectionDAGNode*> AllNodes;
98 /// SelectionDAG constructor - Build a SelectionDAG for the specified
99 /// function. Implemented in DAGBuilder.cpp
101 SelectionDAG(MachineFunction &F, const TargetMachine &TM,
102 SelectionDAGTargetBuilder &SDTB);
105 /// getValueType - Return the ValueType for the specified LLVM type. This
106 /// method works on all scalar LLVM types.
108 MVT::ValueType getValueType(const Type *Ty) const;
110 /// getRoot - Return the root of the current SelectionDAG.
112 SelectionDAGNode *getRoot() const { return Root; }
114 /// getMachineFunction - Return the MachineFunction object that this
115 /// SelectionDAG corresponds to.
117 MachineFunction &getMachineFunction() const { return F; }
119 //===--------------------------------------------------------------------===//
120 // Addition and updating methods
123 /// addNode - Add the specified node to the SelectionDAG so that it will be
124 /// deleted when the DAG is...
126 SelectionDAGNode *addNode(SelectionDAGNode *N) {
127 AllNodes.push_back(N);
131 /// addNodeForValue - Add the specified node to the SelectionDAG so that it
132 /// will be deleted when the DAG is... and update the value map to indicate
133 /// that the specified DAG node computes the value. Note that it is an error
134 /// to specify multiple DAG nodes that compute the same value.
136 SelectionDAGNode *addNodeForValue(SelectionDAGNode *N, const Value *V) {
137 assert(ValueMap.count(V) == 0 && "Value already has a DAG node!");
138 return addNode(ValueMap[V] = N);
143 void addInstructionToDAG(const Instruction &I, const BasicBlock &BB);
147 /// SelectionDAGReducedValue - During the reducer pass we need the ability to
148 /// add an arbitrary (but usually 1 or 0) number of arbitrarily sized values to
149 /// the selection DAG. Because of this, we represent these values as a singly
150 /// linked list of values attached to the DAGNode. We end up putting the
151 /// arbitrary state for the value in subclasses of this node.
153 /// Note that this class does not have a virtual dtor, this is because we know
154 /// that the subclasses will not hold state that needs to be destroyed.
156 class SelectionDAGReducedValue {
158 SelectionDAGReducedValue *Next;
160 SelectionDAGReducedValue(unsigned C) : Code(C), Next(0) {}
162 /// getValueCode - Return the code for this reducer value...
164 unsigned getValueCode() const { return Code; }
166 /// getNext - Return the next value in the list
168 const SelectionDAGReducedValue *getNext() const { return Next; }
169 void setNext(SelectionDAGReducedValue *N) { Next = N; }
171 SelectionDAGReducedValue *getNext() { return Next; }
176 /// SelectionDAGNode - Represents one node in the selection DAG.
178 class SelectionDAGNode {
179 std::vector<SelectionDAGNode*> Uses;
180 ISD::NodeType NodeType;
181 MVT::ValueType ValueType;
182 MachineBasicBlock *BB;
183 SelectionDAGReducedValue *ValList;
185 /// Costs - Each pair of elements of 'Costs' contains the cost of producing
186 /// the value with the target specific slot number and the production number
187 /// to use to produce it. A zero value for the production number indicates
188 /// that the cost has not yet been computed.
191 SelectionDAGNode(ISD::NodeType NT, MVT::ValueType VT,
192 MachineBasicBlock *bb = 0)
193 : NodeType(NT), ValueType(VT), BB(bb), ValList(0), Costs(0) {}
195 SelectionDAGNode(ISD::NodeType NT, MVT::ValueType VT, MachineBasicBlock *bb,
197 : NodeType(NT), ValueType(VT), BB(bb), ValList(0), Costs(0) {
198 assert(NT != ISD::ProtoNode && "Cannot specify uses for a protonode!");
199 Uses.reserve(1); Uses.push_back(N);
201 SelectionDAGNode(ISD::NodeType NT, MVT::ValueType VT, MachineBasicBlock *bb,
202 SelectionDAGNode *N1, SelectionDAGNode *N2)
203 : NodeType(NT), ValueType(VT), BB(bb), ValList(0), Costs(0) {
204 assert(NT != ISD::ProtoNode && "Cannot specify uses for a protonode!");
205 Uses.reserve(2); Uses.push_back(N1); Uses.push_back(N2);
207 SelectionDAGNode(ISD::NodeType NT, MVT::ValueType VT, MachineBasicBlock *bb,
208 SelectionDAGNode *N1, SelectionDAGNode *N2,
209 SelectionDAGNode *N3)
210 : NodeType(NT), ValueType(VT), BB(bb), ValList(0), Costs(0) {
211 assert(NT != ISD::ProtoNode && "Cannot specify uses for a protonode!");
212 Uses.reserve(3); Uses.push_back(N1); Uses.push_back(N2); Uses.push_back(N3);
215 ~SelectionDAGNode() { delete [] Costs; delete ValList; }
217 void setNode(ISD::NodeType NT, MachineBasicBlock *bb) {
218 assert(NodeType == ISD::ProtoNode && NT != ISD::ProtoNode);
219 NodeType = NT; BB = bb;
221 void setNode(ISD::NodeType NT, MachineBasicBlock *bb, SelectionDAGNode *N) {
222 assert(NodeType == ISD::ProtoNode && NT != ISD::ProtoNode);
223 NodeType = NT; BB = bb; Uses.reserve(1); Uses.push_back(N);
225 void setNode(ISD::NodeType NT, MachineBasicBlock *bb,
226 SelectionDAGNode *N1, SelectionDAGNode *N2) {
227 assert(NodeType == ISD::ProtoNode && NT != ISD::ProtoNode);
228 NodeType = NT; BB = bb;
229 Uses.reserve(1); Uses.push_back(N1); Uses.push_back(N2);
232 //===--------------------------------------------------------------------===//
235 ISD::NodeType getNodeType() const { return NodeType; }
236 MVT::ValueType getValueType() const { return ValueType; }
237 MachineBasicBlock *getBB() const { return BB; }
239 SelectionDAGNode *getUse(unsigned Num) {
240 assert(Num < Uses.size() && "Invalid child # of SelectionDAGNode!");
245 Type *getValue(unsigned Code) const {
246 SelectionDAGReducedValue *Vals = ValList;
248 assert(Vals && "Code does not exist in this list!");
249 if (Vals->getValueCode() == Code)
251 Vals = Vals->getNext();
256 Type *hasValue(unsigned Code) const {
257 SelectionDAGReducedValue *Vals = ValList;
259 if (Vals->getValueCode() == Code)
261 Vals = Vals->getNext();
266 void addValue(SelectionDAGReducedValue *New) {
267 assert(New->getNext() == 0);
268 New->setNext(ValList);
272 //===--------------------------------------------------------------------===//
273 // Utility methods used by the pattern matching instruction selector
276 /// getPatternFor - Return the pattern selected to compute the specified slot,
277 /// or zero if there is no pattern yet.
279 unsigned getPatternFor(unsigned Slot) const {
280 return Costs ? Costs[Slot*2] : 0;
283 /// getCostFor - Return the cost to compute the value corresponding to Slot.
285 unsigned getCostFor(unsigned Slot) const {
286 return Costs ? Costs[Slot*2+1] : 0;
289 /// setPatternCostFor - Sets the pattern and the cost for the specified slot
290 /// to the specified values. This allocates the Costs vector if necessary, so
291 /// you must specify the maximum number of slots that may be used.
293 void setPatternCostFor(unsigned Slot, unsigned Pattern, unsigned Cost,
296 Costs = new unsigned[NumSlots*2];
297 for (unsigned i = 0; i != NumSlots*2; ++i) Costs[i] = 0;
299 Costs[Slot*2] = Pattern;
300 Costs[Slot*2+1] = Cost;
305 void printit(unsigned Offset, unsigned &LastID,
306 std::map<const SelectionDAGNode*, unsigned> &NodeIDs) const;
310 /// SelectionDAGTargetBuilder - This class must be implemented by the target, to
311 /// indicate how to perform the extremely target-specific tasks of building DAG
312 /// nodes to represent the calling convention used by the target.
314 struct SelectionDAGTargetBuilder {
315 /// expandArguments - This method is called once by the SelectionDAG
316 /// construction mechanisms to add DAG nodes for each formal argument to the
317 /// current function. If any of the incoming arguments lives on the stack,
318 /// this method should also create the stack slots for the arguments as
320 virtual void expandArguments(SelectionDAG &SD) = 0;
322 /// expandCall - This method is called once per function call by the
323 /// SelectionDAG construction algorithm. It must add DAG nodes to the
324 /// SelectionDAG specified to perform that call.
325 virtual void expandCall(SelectionDAG &SD, CallInst &CI) = 0;
329 enum { // Builtin Slot numbers
346 template<typename ValType, unsigned NodeCode>
347 struct ReducedValue : public SelectionDAGReducedValue {
348 ReducedValue(const ValType &V) : SelectionDAGReducedValue(NodeCode), Val(V) {}
352 typedef ReducedValue<int, ISD::FrameIndex_i32_Slot > ReducedValue_FrameIndex_i32;
353 typedef ReducedValue<int, ISD::FrameIndex_i64_Slot > ReducedValue_FrameIndex_i64;
354 typedef ReducedValue<MachineBasicBlock*, ISD::BasicBlock_i32_Slot > ReducedValue_BasicBlock_i32;
355 typedef ReducedValue<MachineBasicBlock*, ISD::BasicBlock_i64_Slot > ReducedValue_BasicBlock_i64;
357 typedef ReducedValue<bool , ISD::Constant_i1_Slot > ReducedValue_Constant_i1;
358 typedef ReducedValue<unsigned char , ISD::Constant_i8_Slot > ReducedValue_Constant_i8;
359 typedef ReducedValue<unsigned short, ISD::Constant_i16_Slot> ReducedValue_Constant_i16;
360 typedef ReducedValue<unsigned , ISD::Constant_i32_Slot> ReducedValue_Constant_i32;
361 typedef ReducedValue<uint64_t , ISD::Constant_i64_Slot> ReducedValue_Constant_i64;
362 typedef ReducedValue<float , ISD::Constant_f32_Slot> ReducedValue_Constant_f32;
363 typedef ReducedValue<double , ISD::Constant_f64_Slot> ReducedValue_Constant_f64;