SelectionDAG.h revision 453d21e66c6140857c444e85c112d007acee985b
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; 47 48 // ValueNodes - track SrcValue nodes 49 std::map<std::pair<const Value*, int>, SDNode*> ValueNodes; 50 51public: 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 unsigned allnodes_size() const { return AllNodes.size(); } 70 71 /// getRoot - Return the root tag of the SelectionDAG. 72 /// 73 const SDOperand &getRoot() const { return Root; } 74 75 /// getEntryNode - Return the token chain corresponding to the entry of the 76 /// function. 77 const SDOperand &getEntryNode() const { return EntryNode; } 78 79 /// setRoot - Set the current root tag of the SelectionDAG. 80 /// 81 const SDOperand &setRoot(SDOperand N) { return Root = N; } 82 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); 88 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. 92 /// 93 /// Note that this is an involved process that may invalidate pointers into 94 /// the graph. 95 void Legalize(); 96 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); 102 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); 116 117 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) { 118 return getNode(ISD::CopyToReg, MVT::Other, Chain, 119 getRegister(Reg, N.getValueType()), N); 120 } 121 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, 126 SDOperand Flag) { 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())); 133 Ops.push_back(N); 134 if (Flag.Val) Ops.push_back(Flag); 135 return getNode(ISD::CopyToReg, VTs, Ops); 136 } 137 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); 146 } 147 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, 152 SDOperand Flag) { 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); 162 } 163 164 SDOperand getImplicitDef(SDOperand Chain, unsigned Reg, MVT::ValueType VT) { 165 return getNode(ISD::ImplicitDef, MVT::Other, Chain, getRegister(Reg, VT)); 166 } 167 168 /// getCall - Note that this destroys the vector of RetVals passed in. 169 /// 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, 173 Callee); 174 NN->setValueTypes(RetVals); 175 AllNodes.push_back(NN); 176 return NN; 177 } 178 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); 190 return NN; 191 } 192 193 SDOperand getCondCode(ISD::CondCode Cond); 194 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); 198 199 /// getNode - Gets or creates the specified node. 200 /// 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, 211 SDOperand N5); 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); 216 217 /// getSetCC - Helper function to make it easier to build SetCC's if you just 218 /// have an ISD::CondCode instead of an SDOperand. 219 /// 220 SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS, 221 ISD::CondCode Cond) { 222 return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond)); 223 } 224 225 /// getSelectCC - Helper function to make it easier to build SelectCC's if you 226 /// just have an ISD::CondCode instead of an SDOperand. 227 /// 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)); 232 } 233 234 /// getBR2Way_CC - Helper function to make it easier to build BRTWOWAY_CC 235 /// nodes. 236 /// 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); 242 Ops.push_back(LHS); 243 Ops.push_back(RHS); 244 Ops.push_back(True); 245 Ops.push_back(False); 246 return getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops); 247 } 248 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. 251 /// 252 SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr, 253 SDOperand SV); 254 SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain, 255 SDOperand Ptr, SDOperand SV, MVT::ValueType EVT); 256 257 // getSrcValue - construct a node to track a Value* through the backend 258 SDOperand getSrcValue(const Value* I, int offset = 0); 259 260 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, 267 SDOperand Op1); 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, 276 SDOperand Op5); 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, 281 SDOperand Op3); 282 283 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 284 SDOperand Op1) { 285 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1); 286 } 287 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 288 SDOperand Op1, SDOperand Op2) { 289 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2); 290 } 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; 297 Ops.push_back(Op1); 298 Ops.push_back(Op2); 299 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops); 300 } 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); 304 } 305 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 306 SDOperand Op1, SDOperand Op2, SDOperand Op3, 307 SDOperand Op4) { 308 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4); 309 } 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); 314 } 315 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 316 std::vector<SDOperand> &Ops) { 317 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops); 318 } 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); 325 } 326 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. 331 /// 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. 335 /// 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); 342 343 344 /// DeleteNode - Remove the specified node from the system. This node must 345 /// have no referrers. 346 void DeleteNode(SDNode *N); 347 348 void dump() const; 349 350private: 351 void RemoveNodeFromCSEMaps(SDNode *N); 352 SDNode *AddNonLeafNodeToCSEMaps(SDNode *N); 353 void DeleteNodeIfDead(SDNode *N, void *NodeSet); 354 void DeleteNodeNotInCSEMaps(SDNode *N); 355 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); 360 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); 366 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> >, 370 SDNode *> UnaryOps; 371 std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >, 372 SDNode *> BinaryOps; 373 374 std::map<std::pair<unsigned, MVT::ValueType>, RegisterSDNode*> RegNodes; 375 std::vector<CondCodeSDNode*> CondCodeNodes; 376 377 std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >, 378 SDNode *> Loads; 379 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; 398}; 399 400template <> 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(); 404 } 405 static nodes_iterator nodes_end(SelectionDAG *G) { 406 return G->allnodes_end(); 407 } 408}; 409 410} 411 412#endif 413