SelectionDAG.h revision b80e2be8894db9f843f32ebaffb9b7fd6b57d206
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 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); 122 123 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) { 124 return getNode(ISD::CopyToReg, MVT::Other, Chain, 125 getRegister(Reg, N.getValueType()), N); 126 } 127 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, 132 SDOperand Flag) { 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())); 139 Ops.push_back(N); 140 if (Flag.Val) Ops.push_back(Flag); 141 return getNode(ISD::CopyToReg, VTs, Ops); 142 } 143 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); 152 } 153 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, 158 SDOperand Flag) { 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); 168 } 169 170 SDOperand getImplicitDef(SDOperand Chain, unsigned Reg, MVT::ValueType VT) { 171 return getNode(ISD::ImplicitDef, MVT::Other, Chain, getRegister(Reg, VT)); 172 } 173 174 /// getCall - Note that this destroys the vector of RetVals passed in. 175 /// 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, 179 Callee); 180 setNodeValueTypes(NN, RetVals); 181 AllNodes.push_back(NN); 182 return NN; 183 } 184 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); 196 return NN; 197 } 198 199 SDOperand getCondCode(ISD::CondCode Cond); 200 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); 204 205 /// getNode - Gets or creates the specified node. 206 /// 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, 217 SDOperand N5); 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); 222 223 /// getSetCC - Helper function to make it easier to build SetCC's if you just 224 /// have an ISD::CondCode instead of an SDOperand. 225 /// 226 SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS, 227 ISD::CondCode Cond) { 228 return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond)); 229 } 230 231 /// getSelectCC - Helper function to make it easier to build SelectCC's if you 232 /// just have an ISD::CondCode instead of an SDOperand. 233 /// 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)); 238 } 239 240 /// getBR2Way_CC - Helper function to make it easier to build BRTWOWAY_CC 241 /// nodes. 242 /// 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); 248 Ops.push_back(LHS); 249 Ops.push_back(RHS); 250 Ops.push_back(True); 251 Ops.push_back(False); 252 return getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops); 253 } 254 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. 257 /// 258 SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr, 259 SDOperand SV); 260 SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain, 261 SDOperand Ptr, SDOperand SV, MVT::ValueType EVT); 262 263 // getSrcValue - construct a node to track a Value* through the backend 264 SDOperand getSrcValue(const Value* I, int offset = 0); 265 266 267 /// SelectNodeTo - These are used for target selectors to *mutate* the 268 /// specified node to have the specified return type, Target opcode, and 269 /// operands. Note that target opcodes are stored as 270 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field. 271 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT); 272 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 273 SDOperand Op1); 274 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 275 SDOperand Op1, SDOperand Op2); 276 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 277 SDOperand Op1, SDOperand Op2, SDOperand Op3); 278 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 279 SDOperand Op1, SDOperand Op2, SDOperand Op3, SDOperand Op4); 280 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 281 SDOperand Op1, SDOperand Op2, SDOperand Op3, SDOperand Op4, 282 SDOperand Op5); 283 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1, 284 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2); 285 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1, 286 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2, 287 SDOperand Op3); 288 289 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT) { 290 return getNode(ISD::BUILTIN_OP_END+Opcode, VT); 291 } 292 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 293 SDOperand Op1) { 294 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1); 295 } 296 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 297 SDOperand Op1, SDOperand Op2) { 298 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2); 299 } 300 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1, 301 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) { 302 std::vector<MVT::ValueType> ResultTys; 303 ResultTys.push_back(VT1); 304 ResultTys.push_back(VT2); 305 std::vector<SDOperand> Ops; 306 Ops.push_back(Op1); 307 Ops.push_back(Op2); 308 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops); 309 } 310 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 311 SDOperand Op1, SDOperand Op2, SDOperand Op3) { 312 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3); 313 } 314 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 315 SDOperand Op1, SDOperand Op2, SDOperand Op3, 316 SDOperand Op4) { 317 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4); 318 } 319 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 320 SDOperand Op1, SDOperand Op2, SDOperand Op3, 321 SDOperand Op4, SDOperand Op5) { 322 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5); 323 } 324 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 325 std::vector<SDOperand> &Ops) { 326 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops); 327 } 328 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1, 329 MVT::ValueType VT2, std::vector<SDOperand> &Ops) { 330 std::vector<MVT::ValueType> ResultTys; 331 ResultTys.push_back(VT1); 332 ResultTys.push_back(VT2); 333 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops); 334 } 335 336 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 337 /// This can cause recursive merging of nodes in the DAG. Use the first 338 /// version if 'From' is known to have a single result, use the second 339 /// if you have two nodes with identical results, use the third otherwise. 340 /// 341 /// These methods all take an optional vector, which (if not null) is 342 /// populated with any nodes that are deleted from the SelectionDAG, due to 343 /// new equivalences that are discovered. 344 /// 345 void ReplaceAllUsesWith(SDOperand From, SDOperand Op, 346 std::vector<SDNode*> *Deleted = 0); 347 void ReplaceAllUsesWith(SDNode *From, SDNode *To, 348 std::vector<SDNode*> *Deleted = 0); 349 void ReplaceAllUsesWith(SDNode *From, const std::vector<SDOperand> &To, 350 std::vector<SDNode*> *Deleted = 0); 351 352 353 /// DeleteNode - Remove the specified node from the system. This node must 354 /// have no referrers. 355 void DeleteNode(SDNode *N); 356 357 void dump() const; 358 359private: 360 void RemoveNodeFromCSEMaps(SDNode *N); 361 SDNode *AddNonLeafNodeToCSEMaps(SDNode *N); 362 void DestroyDeadNode(SDNode *N); 363 void DeleteNodeNotInCSEMaps(SDNode *N); 364 void setNodeValueTypes(SDNode *N, std::vector<MVT::ValueType> &RetVals); 365 void setNodeValueTypes(SDNode *N, MVT::ValueType VT1, MVT::ValueType VT2); 366 367 368 /// SimplifySetCC - Try to simplify a setcc built with the specified operands 369 /// and cc. If unable to simplify it, return a null SDOperand. 370 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1, 371 SDOperand N2, ISD::CondCode Cond); 372 373 // List of non-single value types. 374 std::list<std::vector<MVT::ValueType> > VTList; 375 376 // Maps to auto-CSE operations. 377 std::map<std::pair<unsigned, MVT::ValueType>, SDNode *> NullaryOps; 378 std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >, 379 SDNode *> UnaryOps; 380 std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >, 381 SDNode *> BinaryOps; 382 383 std::map<std::pair<unsigned, MVT::ValueType>, RegisterSDNode*> RegNodes; 384 std::vector<CondCodeSDNode*> CondCodeNodes; 385 386 std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >, 387 SDNode *> Loads; 388 389 std::map<const GlobalValue*, SDNode*> GlobalValues; 390 std::map<const GlobalValue*, SDNode*> TargetGlobalValues; 391 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants; 392 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> TargetConstants; 393 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs; 394 std::map<int, SDNode*> FrameIndices, TargetFrameIndices; 395 std::map<Constant *, SDNode*> ConstantPoolIndices; 396 std::map<Constant *, SDNode*> TargetConstantPoolIndices; 397 std::map<MachineBasicBlock *, SDNode*> BBNodes; 398 std::vector<SDNode*> ValueTypeNodes; 399 std::map<std::string, SDNode*> ExternalSymbols; 400 std::map<std::string, SDNode*> TargetExternalSymbols; 401 std::map<std::pair<unsigned, 402 std::pair<MVT::ValueType, std::vector<SDOperand> > >, 403 SDNode*> OneResultNodes; 404 std::map<std::pair<unsigned, 405 std::pair<std::vector<MVT::ValueType>, 406 std::vector<SDOperand> > >, 407 SDNode*> ArbitraryNodes; 408}; 409 410template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> { 411 typedef SelectionDAG::allnodes_iterator nodes_iterator; 412 static nodes_iterator nodes_begin(SelectionDAG *G) { 413 return G->allnodes_begin(); 414 } 415 static nodes_iterator nodes_end(SelectionDAG *G) { 416 return G->allnodes_end(); 417 } 418}; 419 420} // end namespace llvm 421 422#endif 423