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