SelectionDAG.h revision 61ca74bc3a29b2af2be7e4bd612289da8aae85b5
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 "llvm/ADT/ilist" 20 21#include <map> 22#include <list> 23#include <string> // FIXME remove eventually, turning map into const char* map. 24 25namespace llvm { 26 class TargetLowering; 27 class TargetMachine; 28 class MachineFunction; 29 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. 35/// 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 39/// linear form. 40/// 41class SelectionDAG { 42 TargetLowering &TLI; 43 MachineFunction &MF; 44 45 // Root - The root of the entire DAG. EntryNode - The starting token. 46 SDOperand Root, EntryNode; 47 48 // AllNodes - A linked list of nodes in the current DAG. 49 ilist<SDNode> AllNodes; 50 51 // ValueNodes - track SrcValue nodes 52 std::map<std::pair<const Value*, int>, SDNode*> ValueNodes; 53 54public: 55 SelectionDAG(TargetLowering &tli, MachineFunction &mf) : TLI(tli), MF(mf) { 56 EntryNode = Root = getNode(ISD::EntryToken, MVT::Other); 57 } 58 ~SelectionDAG(); 59 60 MachineFunction &getMachineFunction() const { return MF; } 61 const TargetMachine &getTarget() const; 62 TargetLowering &getTargetLoweringInfo() const { return TLI; } 63 64 /// viewGraph - Pop up a ghostview window with the DAG rendered using 'dot'. 65 /// 66 void viewGraph(); 67 68 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(); } 75 76 /// getRoot - Return the root tag of the SelectionDAG. 77 /// 78 const SDOperand &getRoot() const { return Root; } 79 80 /// getEntryNode - Return the token chain corresponding to the entry of the 81 /// function. 82 const SDOperand &getEntryNode() const { return EntryNode; } 83 84 /// setRoot - Set the current root tag of the SelectionDAG. 85 /// 86 const SDOperand &setRoot(SDOperand N) { return Root = N; } 87 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); 93 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. 97 /// 98 /// Note that this is an involved process that may invalidate pointers into 99 /// the graph. 100 void Legalize(); 101 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); 107 108 SDOperand getString(const std::string &Val); 109 SDOperand getConstant(uint64_t Val, MVT::ValueType VT); 110 SDOperand getTargetConstant(uint64_t Val, MVT::ValueType VT); 111 SDOperand getConstantFP(double Val, MVT::ValueType VT); 112 SDOperand getGlobalAddress(const GlobalValue *GV, MVT::ValueType VT); 113 SDOperand getTargetGlobalAddress(const GlobalValue *GV, MVT::ValueType VT, 114 int offset=0); 115 SDOperand getFrameIndex(int FI, MVT::ValueType VT); 116 SDOperand getTargetFrameIndex(int FI, MVT::ValueType VT); 117 SDOperand getConstantPool(Constant *C, MVT::ValueType VT); 118 SDOperand getTargetConstantPool(Constant *C, MVT::ValueType VT); 119 SDOperand getBasicBlock(MachineBasicBlock *MBB); 120 SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT); 121 SDOperand getTargetExternalSymbol(const char *Sym, MVT::ValueType VT); 122 SDOperand getValueType(MVT::ValueType); 123 SDOperand getRegister(unsigned Reg, MVT::ValueType VT); 124 125 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) { 126 return getNode(ISD::CopyToReg, MVT::Other, Chain, 127 getRegister(Reg, N.getValueType()), N); 128 } 129 130 // This version of the getCopyToReg method takes an extra operand, which 131 // indicates that there is potentially an incoming flag value (if Flag is not 132 // null) and that there should be a flag result. 133 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N, 134 SDOperand Flag) { 135 std::vector<MVT::ValueType> VTs; 136 VTs.push_back(MVT::Other); 137 VTs.push_back(MVT::Flag); 138 std::vector<SDOperand> Ops; 139 Ops.push_back(Chain); 140 Ops.push_back(getRegister(Reg, N.getValueType())); 141 Ops.push_back(N); 142 if (Flag.Val) Ops.push_back(Flag); 143 return getNode(ISD::CopyToReg, VTs, Ops); 144 } 145 146 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) { 147 std::vector<MVT::ValueType> ResultTys; 148 ResultTys.push_back(VT); 149 ResultTys.push_back(MVT::Other); 150 std::vector<SDOperand> Ops; 151 Ops.push_back(Chain); 152 Ops.push_back(getRegister(Reg, VT)); 153 return getNode(ISD::CopyFromReg, ResultTys, Ops); 154 } 155 156 // This version of the getCopyFromReg method takes an extra operand, which 157 // indicates that there is potentially an incoming flag value (if Flag is not 158 // null) and that there should be a flag result. 159 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT, 160 SDOperand Flag) { 161 std::vector<MVT::ValueType> ResultTys; 162 ResultTys.push_back(VT); 163 ResultTys.push_back(MVT::Other); 164 ResultTys.push_back(MVT::Flag); 165 std::vector<SDOperand> Ops; 166 Ops.push_back(Chain); 167 Ops.push_back(getRegister(Reg, VT)); 168 if (Flag.Val) Ops.push_back(Flag); 169 return getNode(ISD::CopyFromReg, ResultTys, Ops); 170 } 171 172 SDOperand getImplicitDef(SDOperand Chain, unsigned Reg, MVT::ValueType VT) { 173 return getNode(ISD::ImplicitDef, MVT::Other, Chain, getRegister(Reg, VT)); 174 } 175 176 /// getCall - Note that this destroys the vector of RetVals passed in. 177 /// 178 SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain, 179 SDOperand Callee, bool isTailCall = false) { 180 SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, Chain, 181 Callee); 182 setNodeValueTypes(NN, RetVals); 183 AllNodes.push_back(NN); 184 return NN; 185 } 186 187 /// getCall - This is identical to the one above, and should be used for calls 188 /// where arguments are passed in physical registers. This destroys the 189 /// RetVals and ArgsInRegs vectors. 190 SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain, 191 SDOperand Callee, std::vector<SDOperand> &ArgsInRegs, 192 bool isTailCall = false) { 193 ArgsInRegs.insert(ArgsInRegs.begin(), Callee); 194 ArgsInRegs.insert(ArgsInRegs.begin(), Chain); 195 SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, ArgsInRegs); 196 setNodeValueTypes(NN, RetVals); 197 AllNodes.push_back(NN); 198 return NN; 199 } 200 201 SDOperand getCondCode(ISD::CondCode Cond); 202 203 /// getZeroExtendInReg - Return the expression required to zero extend the Op 204 /// value assuming it was the smaller SrcTy value. 205 SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy); 206 207 /// getNode - Gets or creates the specified node. 208 /// 209 SDOperand getNode(unsigned Opcode, MVT::ValueType VT); 210 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N); 211 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, 212 SDOperand N1, SDOperand N2); 213 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, 214 SDOperand N1, SDOperand N2, SDOperand N3); 215 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, 216 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4); 217 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, 218 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4, 219 SDOperand N5); 220 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, 221 std::vector<SDOperand> &Children); 222 SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys, 223 std::vector<SDOperand> &Ops); 224 225 /// getSetCC - Helper function to make it easier to build SetCC's if you just 226 /// have an ISD::CondCode instead of an SDOperand. 227 /// 228 SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS, 229 ISD::CondCode Cond) { 230 return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond)); 231 } 232 233 /// getSelectCC - Helper function to make it easier to build SelectCC's if you 234 /// just have an ISD::CondCode instead of an SDOperand. 235 /// 236 SDOperand getSelectCC(SDOperand LHS, SDOperand RHS, 237 SDOperand True, SDOperand False, ISD::CondCode Cond) { 238 MVT::ValueType VT = True.getValueType(); 239 return getNode(ISD::SELECT_CC, VT, LHS, RHS, True, False,getCondCode(Cond)); 240 } 241 242 /// getBR2Way_CC - Helper function to make it easier to build BRTWOWAY_CC 243 /// nodes. 244 /// 245 SDOperand getBR2Way_CC(SDOperand Chain, SDOperand CCNode, SDOperand LHS, 246 SDOperand RHS, SDOperand True, SDOperand False) { 247 std::vector<SDOperand> Ops; 248 Ops.push_back(Chain); 249 Ops.push_back(CCNode); 250 Ops.push_back(LHS); 251 Ops.push_back(RHS); 252 Ops.push_back(True); 253 Ops.push_back(False); 254 return getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops); 255 } 256 257 /// getLoad - Loads are not normal binary operators: their result type is not 258 /// determined by their operands, and they produce a value AND a token chain. 259 /// 260 SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr, 261 SDOperand SV); 262 SDOperand getVecLoad(unsigned Count, MVT::ValueType VT, SDOperand Chain, 263 SDOperand Ptr, SDOperand SV); 264 SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain, 265 SDOperand Ptr, SDOperand SV, MVT::ValueType EVT); 266 267 // getSrcValue - construct a node to track a Value* through the backend 268 SDOperand getSrcValue(const Value* I, int offset = 0); 269 270 271 /// SelectNodeTo - These are used for target selectors to *mutate* the 272 /// specified node to have the specified return type, Target opcode, and 273 /// operands. Note that target opcodes are stored as 274 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field. 275 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT); 276 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 277 SDOperand Op1); 278 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 279 SDOperand Op1, SDOperand Op2); 280 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 281 SDOperand Op1, SDOperand Op2, SDOperand Op3); 282 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 283 SDOperand Op1, SDOperand Op2, SDOperand Op3, SDOperand Op4); 284 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 285 SDOperand Op1, SDOperand Op2, SDOperand Op3, SDOperand Op4, 286 SDOperand Op5); 287 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 288 SDOperand Op1, SDOperand Op2, SDOperand Op3, SDOperand Op4, 289 SDOperand Op5, SDOperand Op6); 290 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1, 291 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2); 292 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1, 293 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2, 294 SDOperand Op3); 295 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1, 296 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2, 297 SDOperand Op3, SDOperand Op4); 298 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1, 299 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2, 300 SDOperand Op3, SDOperand Op4, SDOperand Op5); 301 302 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT) { 303 return getNode(ISD::BUILTIN_OP_END+Opcode, VT); 304 } 305 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 306 SDOperand Op1) { 307 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1); 308 } 309 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 310 SDOperand Op1, SDOperand Op2) { 311 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2); 312 } 313 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1, 314 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) { 315 std::vector<MVT::ValueType> ResultTys; 316 ResultTys.push_back(VT1); 317 ResultTys.push_back(VT2); 318 std::vector<SDOperand> Ops; 319 Ops.push_back(Op1); 320 Ops.push_back(Op2); 321 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops); 322 } 323 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 324 SDOperand Op1, SDOperand Op2, SDOperand Op3) { 325 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3); 326 } 327 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 328 SDOperand Op1, SDOperand Op2, SDOperand Op3, 329 SDOperand Op4) { 330 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4); 331 } 332 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 333 SDOperand Op1, SDOperand Op2, SDOperand Op3, 334 SDOperand Op4, SDOperand Op5) { 335 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5); 336 } 337 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 338 std::vector<SDOperand> &Ops) { 339 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops); 340 } 341 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1, 342 MVT::ValueType VT2, std::vector<SDOperand> &Ops) { 343 std::vector<MVT::ValueType> ResultTys; 344 ResultTys.push_back(VT1); 345 ResultTys.push_back(VT2); 346 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops); 347 } 348 349 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 350 /// This can cause recursive merging of nodes in the DAG. Use the first 351 /// version if 'From' is known to have a single result, use the second 352 /// if you have two nodes with identical results, use the third otherwise. 353 /// 354 /// These methods all take an optional vector, which (if not null) is 355 /// populated with any nodes that are deleted from the SelectionDAG, due to 356 /// new equivalences that are discovered. 357 /// 358 void ReplaceAllUsesWith(SDOperand From, SDOperand Op, 359 std::vector<SDNode*> *Deleted = 0); 360 void ReplaceAllUsesWith(SDNode *From, SDNode *To, 361 std::vector<SDNode*> *Deleted = 0); 362 void ReplaceAllUsesWith(SDNode *From, const std::vector<SDOperand> &To, 363 std::vector<SDNode*> *Deleted = 0); 364 365 366 /// DeleteNode - Remove the specified node from the system. This node must 367 /// have no referrers. 368 void DeleteNode(SDNode *N); 369 370 void dump() const; 371 372private: 373 void RemoveNodeFromCSEMaps(SDNode *N); 374 SDNode *AddNonLeafNodeToCSEMaps(SDNode *N); 375 void DestroyDeadNode(SDNode *N); 376 void DeleteNodeNotInCSEMaps(SDNode *N); 377 void setNodeValueTypes(SDNode *N, std::vector<MVT::ValueType> &RetVals); 378 void setNodeValueTypes(SDNode *N, MVT::ValueType VT1, MVT::ValueType VT2); 379 380 381 /// SimplifySetCC - Try to simplify a setcc built with the specified operands 382 /// and cc. If unable to simplify it, return a null SDOperand. 383 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1, 384 SDOperand N2, ISD::CondCode Cond); 385 386 // List of non-single value types. 387 std::list<std::vector<MVT::ValueType> > VTList; 388 389 // Maps to auto-CSE operations. 390 std::map<std::pair<unsigned, MVT::ValueType>, SDNode *> NullaryOps; 391 std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >, 392 SDNode *> UnaryOps; 393 std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >, 394 SDNode *> BinaryOps; 395 396 std::map<std::pair<unsigned, MVT::ValueType>, RegisterSDNode*> RegNodes; 397 std::vector<CondCodeSDNode*> CondCodeNodes; 398 399 std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >, 400 SDNode *> Loads; 401 402 std::map<const GlobalValue*, SDNode*> GlobalValues; 403 std::map<const GlobalValue*, SDNode*> TargetGlobalValues; 404 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants; 405 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> TargetConstants; 406 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs; 407 std::map<int, SDNode*> FrameIndices, TargetFrameIndices; 408 std::map<Constant *, SDNode*> ConstantPoolIndices; 409 std::map<Constant *, SDNode*> TargetConstantPoolIndices; 410 std::map<MachineBasicBlock *, SDNode*> BBNodes; 411 std::vector<SDNode*> ValueTypeNodes; 412 std::map<std::string, SDNode*> ExternalSymbols; 413 std::map<std::string, SDNode*> TargetExternalSymbols; 414 std::map<std::string, StringSDNode*> StringNodes; 415 std::map<std::pair<unsigned, 416 std::pair<MVT::ValueType, std::vector<SDOperand> > >, 417 SDNode*> OneResultNodes; 418 std::map<std::pair<unsigned, 419 std::pair<std::vector<MVT::ValueType>, 420 std::vector<SDOperand> > >, 421 SDNode*> ArbitraryNodes; 422}; 423 424template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> { 425 typedef SelectionDAG::allnodes_iterator nodes_iterator; 426 static nodes_iterator nodes_begin(SelectionDAG *G) { 427 return G->allnodes_begin(); 428 } 429 static nodes_iterator nodes_end(SelectionDAG *G) { 430 return G->allnodes_end(); 431 } 432}; 433 434} // end namespace llvm 435 436#endif 437