SelectionDAG.h revision f0e80cf909b69b5f31b8d027de6ee4b963205f66
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 getTargetExternalSymbol(const char *Sym, MVT::ValueType VT); 115 SDOperand getValueType(MVT::ValueType); 116 SDOperand getRegister(unsigned Reg, MVT::ValueType VT); 117 118 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) { 119 return getNode(ISD::CopyToReg, MVT::Other, Chain, 120 getRegister(Reg, N.getValueType()), N); 121 } 122 123 // This version of the getCopyToReg method takes an extra operand, which 124 // indicates that there is potentially an incoming flag value (if Flag is not 125 // null) and that there should be a flag result. 126 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N, 127 SDOperand Flag) { 128 std::vector<MVT::ValueType> VTs; 129 VTs.push_back(MVT::Other); 130 VTs.push_back(MVT::Flag); 131 std::vector<SDOperand> Ops; 132 Ops.push_back(Chain); 133 Ops.push_back(getRegister(Reg, N.getValueType())); 134 Ops.push_back(N); 135 if (Flag.Val) Ops.push_back(Flag); 136 return getNode(ISD::CopyToReg, VTs, Ops); 137 } 138 139 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT) { 140 std::vector<MVT::ValueType> ResultTys; 141 ResultTys.push_back(VT); 142 ResultTys.push_back(MVT::Other); 143 std::vector<SDOperand> Ops; 144 Ops.push_back(Chain); 145 Ops.push_back(getRegister(Reg, VT)); 146 return getNode(ISD::CopyFromReg, ResultTys, Ops); 147 } 148 149 // This version of the getCopyFromReg method takes an extra operand, which 150 // indicates that there is potentially an incoming flag value (if Flag is not 151 // null) and that there should be a flag result. 152 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT::ValueType VT, 153 SDOperand Flag) { 154 std::vector<MVT::ValueType> ResultTys; 155 ResultTys.push_back(VT); 156 ResultTys.push_back(MVT::Other); 157 ResultTys.push_back(MVT::Flag); 158 std::vector<SDOperand> Ops; 159 Ops.push_back(Chain); 160 Ops.push_back(getRegister(Reg, VT)); 161 if (Flag.Val) Ops.push_back(Flag); 162 return getNode(ISD::CopyFromReg, ResultTys, Ops); 163 } 164 165 SDOperand getImplicitDef(SDOperand Chain, unsigned Reg, MVT::ValueType VT) { 166 return getNode(ISD::ImplicitDef, MVT::Other, Chain, getRegister(Reg, VT)); 167 } 168 169 /// getCall - Note that this destroys the vector of RetVals passed in. 170 /// 171 SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain, 172 SDOperand Callee, bool isTailCall = false) { 173 SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, Chain, 174 Callee); 175 NN->setValueTypes(RetVals); 176 AllNodes.push_back(NN); 177 return NN; 178 } 179 180 /// getCall - This is identical to the one above, and should be used for calls 181 /// where arguments are passed in physical registers. This destroys the 182 /// RetVals and ArgsInRegs vectors. 183 SDNode *getCall(std::vector<MVT::ValueType> &RetVals, SDOperand Chain, 184 SDOperand Callee, std::vector<SDOperand> &ArgsInRegs, 185 bool isTailCall = false) { 186 ArgsInRegs.insert(ArgsInRegs.begin(), Callee); 187 ArgsInRegs.insert(ArgsInRegs.begin(), Chain); 188 SDNode *NN = new SDNode(isTailCall ? ISD::TAILCALL : ISD::CALL, ArgsInRegs); 189 NN->setValueTypes(RetVals); 190 AllNodes.push_back(NN); 191 return NN; 192 } 193 194 SDOperand getCondCode(ISD::CondCode Cond); 195 196 /// getZeroExtendInReg - Return the expression required to zero extend the Op 197 /// value assuming it was the smaller SrcTy value. 198 SDOperand getZeroExtendInReg(SDOperand Op, MVT::ValueType SrcTy); 199 200 /// getNode - Gets or creates the specified node. 201 /// 202 SDOperand getNode(unsigned Opcode, MVT::ValueType VT); 203 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, SDOperand N); 204 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, 205 SDOperand N1, SDOperand N2); 206 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, 207 SDOperand N1, SDOperand N2, SDOperand N3); 208 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, 209 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4); 210 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, 211 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4, 212 SDOperand N5); 213 SDOperand getNode(unsigned Opcode, MVT::ValueType VT, 214 std::vector<SDOperand> &Children); 215 SDOperand getNode(unsigned Opcode, std::vector<MVT::ValueType> &ResultTys, 216 std::vector<SDOperand> &Ops); 217 218 /// getSetCC - Helper function to make it easier to build SetCC's if you just 219 /// have an ISD::CondCode instead of an SDOperand. 220 /// 221 SDOperand getSetCC(MVT::ValueType VT, SDOperand LHS, SDOperand RHS, 222 ISD::CondCode Cond) { 223 return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond)); 224 } 225 226 /// getSelectCC - Helper function to make it easier to build SelectCC's if you 227 /// just have an ISD::CondCode instead of an SDOperand. 228 /// 229 SDOperand getSelectCC(SDOperand LHS, SDOperand RHS, 230 SDOperand True, SDOperand False, ISD::CondCode Cond) { 231 MVT::ValueType VT = True.getValueType(); 232 return getNode(ISD::SELECT_CC, VT, LHS, RHS, True, False,getCondCode(Cond)); 233 } 234 235 /// getBR2Way_CC - Helper function to make it easier to build BRTWOWAY_CC 236 /// nodes. 237 /// 238 SDOperand getBR2Way_CC(SDOperand Chain, SDOperand CCNode, SDOperand LHS, 239 SDOperand RHS, SDOperand True, SDOperand False) { 240 std::vector<SDOperand> Ops; 241 Ops.push_back(Chain); 242 Ops.push_back(CCNode); 243 Ops.push_back(LHS); 244 Ops.push_back(RHS); 245 Ops.push_back(True); 246 Ops.push_back(False); 247 return getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops); 248 } 249 250 /// getLoad - Loads are not normal binary operators: their result type is not 251 /// determined by their operands, and they produce a value AND a token chain. 252 /// 253 SDOperand getLoad(MVT::ValueType VT, SDOperand Chain, SDOperand Ptr, 254 SDOperand SV); 255 SDOperand getExtLoad(unsigned Opcode, MVT::ValueType VT, SDOperand Chain, 256 SDOperand Ptr, SDOperand SV, MVT::ValueType EVT); 257 258 // getSrcValue - construct a node to track a Value* through the backend 259 SDOperand getSrcValue(const Value* I, int offset = 0); 260 261 262 /// SelectNodeTo - These are used for target selectors to *mutate* the 263 /// specified node to have the specified return type, Target opcode, and 264 /// operands. Note that target opcodes are stored as 265 /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field. 266 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT); 267 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 268 SDOperand Op1); 269 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 270 SDOperand Op1, SDOperand Op2); 271 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 272 SDOperand Op1, SDOperand Op2, SDOperand Op3); 273 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 274 SDOperand Op1, SDOperand Op2, SDOperand Op3, SDOperand Op4); 275 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT, 276 SDOperand Op1, SDOperand Op2, SDOperand Op3, SDOperand Op4, 277 SDOperand Op5); 278 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1, 279 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2); 280 void SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT::ValueType VT1, 281 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2, 282 SDOperand Op3); 283 284 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT) { 285 return getNode(ISD::BUILTIN_OP_END+Opcode, VT); 286 } 287 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 288 SDOperand Op1) { 289 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1); 290 } 291 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 292 SDOperand Op1, SDOperand Op2) { 293 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2); 294 } 295 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1, 296 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) { 297 std::vector<MVT::ValueType> ResultTys; 298 ResultTys.push_back(VT1); 299 ResultTys.push_back(VT2); 300 std::vector<SDOperand> Ops; 301 Ops.push_back(Op1); 302 Ops.push_back(Op2); 303 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops); 304 } 305 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 306 SDOperand Op1, SDOperand Op2, SDOperand Op3) { 307 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3); 308 } 309 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 310 SDOperand Op1, SDOperand Op2, SDOperand Op3, 311 SDOperand Op4) { 312 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4); 313 } 314 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 315 SDOperand Op1, SDOperand Op2, SDOperand Op3, 316 SDOperand Op4, SDOperand Op5) { 317 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5); 318 } 319 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT, 320 std::vector<SDOperand> &Ops) { 321 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops); 322 } 323 SDOperand getTargetNode(unsigned Opcode, MVT::ValueType VT1, 324 MVT::ValueType VT2, std::vector<SDOperand> &Ops) { 325 std::vector<MVT::ValueType> ResultTys; 326 ResultTys.push_back(VT1); 327 ResultTys.push_back(VT2); 328 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops); 329 } 330 331 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. 332 /// This can cause recursive merging of nodes in the DAG. Use the first 333 /// version if 'From' is known to have a single result, use the second 334 /// if you have two nodes with identical results, use the third otherwise. 335 /// 336 /// These methods all take an optional vector, which (if not null) is 337 /// populated with any nodes that are deleted from the SelectionDAG, due to 338 /// new equivalences that are discovered. 339 /// 340 void ReplaceAllUsesWith(SDOperand From, SDOperand Op, 341 std::vector<SDNode*> *Deleted = 0); 342 void ReplaceAllUsesWith(SDNode *From, SDNode *To, 343 std::vector<SDNode*> *Deleted = 0); 344 void ReplaceAllUsesWith(SDNode *From, const std::vector<SDOperand> &To, 345 std::vector<SDNode*> *Deleted = 0); 346 347 348 /// DeleteNode - Remove the specified node from the system. This node must 349 /// have no referrers. 350 void DeleteNode(SDNode *N); 351 352 void dump() const; 353 354private: 355 void RemoveNodeFromCSEMaps(SDNode *N); 356 SDNode *AddNonLeafNodeToCSEMaps(SDNode *N); 357 void DestroyDeadNode(SDNode *N); 358 void DeleteNodeNotInCSEMaps(SDNode *N); 359 360 /// SimplifySetCC - Try to simplify a setcc built with the specified operands 361 /// and cc. If unable to simplify it, return a null SDOperand. 362 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N1, 363 SDOperand N2, ISD::CondCode Cond); 364 365 // Maps to auto-CSE operations. 366 std::map<std::pair<unsigned, MVT::ValueType>, SDNode *> NullaryOps; 367 std::map<std::pair<unsigned, std::pair<SDOperand, MVT::ValueType> >, 368 SDNode *> UnaryOps; 369 std::map<std::pair<unsigned, std::pair<SDOperand, SDOperand> >, 370 SDNode *> BinaryOps; 371 372 std::map<std::pair<unsigned, MVT::ValueType>, RegisterSDNode*> RegNodes; 373 std::vector<CondCodeSDNode*> CondCodeNodes; 374 375 std::map<std::pair<SDOperand, std::pair<SDOperand, MVT::ValueType> >, 376 SDNode *> Loads; 377 378 std::map<const GlobalValue*, SDNode*> GlobalValues; 379 std::map<const GlobalValue*, SDNode*> TargetGlobalValues; 380 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> Constants; 381 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> TargetConstants; 382 std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs; 383 std::map<int, SDNode*> FrameIndices, TargetFrameIndices; 384 std::map<Constant *, SDNode*> ConstantPoolIndices; 385 std::map<Constant *, SDNode*> TargetConstantPoolIndices; 386 std::map<MachineBasicBlock *, SDNode*> BBNodes; 387 std::vector<SDNode*> ValueTypeNodes; 388 std::map<std::string, SDNode*> ExternalSymbols; 389 std::map<std::string, SDNode*> TargetExternalSymbols; 390 std::map<std::pair<unsigned, 391 std::pair<MVT::ValueType, std::vector<SDOperand> > >, 392 SDNode*> OneResultNodes; 393 std::map<std::pair<unsigned, 394 std::pair<std::vector<MVT::ValueType>, 395 std::vector<SDOperand> > >, 396 SDNode*> ArbitraryNodes; 397}; 398 399template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> { 400 typedef SelectionDAG::allnodes_iterator nodes_iterator; 401 static nodes_iterator nodes_begin(SelectionDAG *G) { 402 return G->allnodes_begin(); 403 } 404 static nodes_iterator nodes_end(SelectionDAG *G) { 405 return G->allnodes_end(); 406 } 407}; 408 409} 410 411#endif 412