1 //===-- llvm/CodeGen/SelectionDAG.h - InstSelection DAG ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the SelectionDAG class, and transitively defines the
11 // SDNode class and subclasses.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_SELECTIONDAG_H
16 #define LLVM_CODEGEN_SELECTIONDAG_H
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/CodeGen/SelectionDAGNodes.h"
31 class MachineModuleInfo;
32 class MachineFunction;
33 class MachineConstantPoolValue;
34 class FunctionLoweringInfo;
36 /// SelectionDAG class - This is used to represent a portion of an LLVM function
37 /// in a low-level Data Dependence DAG representation suitable for instruction
38 /// selection. This DAG is constructed as the first step of instruction
39 /// selection in order to allow implementation of machine specific optimizations
40 /// and code simplifications.
42 /// The representation used by the SelectionDAG is a target-independent
43 /// representation, which has some similarities to the GCC RTL representation,
44 /// but is significantly more simple, powerful, and is a graph form instead of a
50 FunctionLoweringInfo &FLI;
51 MachineModuleInfo *MMI;
53 /// Root - The root of the entire DAG. EntryNode - The starting token.
54 SDOperand Root, EntryNode;
56 /// AllNodes - A linked list of nodes in the current DAG.
57 alist<SDNode, LargestSDNode> &AllNodes;
59 /// CSEMap - This structure is used to memoize nodes, automatically performing
60 /// CSE with existing nodes with a duplicate is requested.
61 FoldingSet<SDNode> CSEMap;
63 /// Allocator - Pool allocation for misc. objects that are created once per
65 BumpPtrAllocator Allocator;
68 SelectionDAG(TargetLowering &tli, MachineFunction &mf,
69 FunctionLoweringInfo &fli, MachineModuleInfo *mmi,
70 alist<SDNode, LargestSDNode> &NodePool)
71 : TLI(tli), MF(mf), FLI(fli), MMI(mmi), AllNodes(NodePool) {
72 EntryNode = Root = getNode(ISD::EntryToken, MVT::Other);
76 MachineFunction &getMachineFunction() const { return MF; }
77 const TargetMachine &getTarget() const;
78 TargetLowering &getTargetLoweringInfo() const { return TLI; }
79 FunctionLoweringInfo &getFunctionLoweringInfo() const { return FLI; }
80 MachineModuleInfo *getMachineModuleInfo() const { return MMI; }
82 /// viewGraph - Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
87 std::map<const SDNode *, std::string> NodeGraphAttrs;
90 /// clearGraphAttrs - Clear all previously defined node graph attributes.
91 /// Intended to be used from a debugging tool (eg. gdb).
92 void clearGraphAttrs();
94 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
96 void setGraphAttrs(const SDNode *N, const char *Attrs);
98 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
99 /// Used from getNodeAttributes.
100 const std::string getGraphAttrs(const SDNode *N) const;
102 /// setGraphColor - Convenience for setting node color attribute.
104 void setGraphColor(const SDNode *N, const char *Color);
106 typedef alist<SDNode, LargestSDNode>::const_iterator allnodes_const_iterator;
107 allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
108 allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
109 typedef alist<SDNode, LargestSDNode>::iterator allnodes_iterator;
110 allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
111 allnodes_iterator allnodes_end() { return AllNodes.end(); }
112 alist<SDNode, LargestSDNode>::size_type allnodes_size() const {
113 return AllNodes.size();
116 /// getRoot - Return the root tag of the SelectionDAG.
118 const SDOperand &getRoot() const { return Root; }
120 /// getEntryNode - Return the token chain corresponding to the entry of the
122 const SDOperand &getEntryNode() const { return EntryNode; }
124 /// setRoot - Set the current root tag of the SelectionDAG.
126 const SDOperand &setRoot(SDOperand N) { return Root = N; }
128 /// Combine - This iterates over the nodes in the SelectionDAG, folding
129 /// certain types of nodes together, or eliminating superfluous nodes. When
130 /// the AfterLegalize argument is set to 'true', Combine takes care not to
131 /// generate any nodes that will be illegal on the target.
132 void Combine(bool AfterLegalize, AliasAnalysis &AA);
134 /// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that
135 /// only uses types natively supported by the target.
137 /// Note that this is an involved process that may invalidate pointers into
139 void LegalizeTypes();
141 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
142 /// compatible with the target instruction selector, as indicated by the
143 /// TargetLowering object.
145 /// Note that this is an involved process that may invalidate pointers into
149 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
151 void RemoveDeadNodes();
153 /// DeleteNode - Remove the specified node from the system. This node must
154 /// have no referrers.
155 void DeleteNode(SDNode *N);
157 /// getVTList - Return an SDVTList that represents the list of values
159 SDVTList getVTList(MVT VT);
160 SDVTList getVTList(MVT VT1, MVT VT2);
161 SDVTList getVTList(MVT VT1, MVT VT2, MVT VT3);
162 SDVTList getVTList(const MVT *VTs, unsigned NumVTs);
164 /// getNodeValueTypes - These are obsolete, use getVTList instead.
165 const MVT *getNodeValueTypes(MVT VT) {
166 return getVTList(VT).VTs;
168 const MVT *getNodeValueTypes(MVT VT1, MVT VT2) {
169 return getVTList(VT1, VT2).VTs;
171 const MVT *getNodeValueTypes(MVT VT1, MVT VT2, MVT VT3) {
172 return getVTList(VT1, VT2, VT3).VTs;
174 const MVT *getNodeValueTypes(const std::vector<MVT> &vtList) {
175 return getVTList(&vtList[0], (unsigned)vtList.size()).VTs;
179 //===--------------------------------------------------------------------===//
180 // Node creation methods.
182 SDOperand getConstant(uint64_t Val, MVT VT, bool isTarget = false);
183 SDOperand getConstant(const APInt &Val, MVT VT, bool isTarget = false);
184 SDOperand getIntPtrConstant(uint64_t Val, bool isTarget = false);
185 SDOperand getTargetConstant(uint64_t Val, MVT VT) {
186 return getConstant(Val, VT, true);
188 SDOperand getTargetConstant(const APInt &Val, MVT VT) {
189 return getConstant(Val, VT, true);
191 SDOperand getConstantFP(double Val, MVT VT, bool isTarget = false);
192 SDOperand getConstantFP(const APFloat& Val, MVT VT, bool isTarget = false);
193 SDOperand getTargetConstantFP(double Val, MVT VT) {
194 return getConstantFP(Val, VT, true);
196 SDOperand getTargetConstantFP(const APFloat& Val, MVT VT) {
197 return getConstantFP(Val, VT, true);
199 SDOperand getGlobalAddress(const GlobalValue *GV, MVT VT,
200 int offset = 0, bool isTargetGA = false);
201 SDOperand getTargetGlobalAddress(const GlobalValue *GV, MVT VT,
203 return getGlobalAddress(GV, VT, offset, true);
205 SDOperand getFrameIndex(int FI, MVT VT, bool isTarget = false);
206 SDOperand getTargetFrameIndex(int FI, MVT VT) {
207 return getFrameIndex(FI, VT, true);
209 SDOperand getJumpTable(int JTI, MVT VT, bool isTarget = false);
210 SDOperand getTargetJumpTable(int JTI, MVT VT) {
211 return getJumpTable(JTI, VT, true);
213 SDOperand getConstantPool(Constant *C, MVT VT,
214 unsigned Align = 0, int Offs = 0, bool isT=false);
215 SDOperand getTargetConstantPool(Constant *C, MVT VT,
216 unsigned Align = 0, int Offset = 0) {
217 return getConstantPool(C, VT, Align, Offset, true);
219 SDOperand getConstantPool(MachineConstantPoolValue *C, MVT VT,
220 unsigned Align = 0, int Offs = 0, bool isT=false);
221 SDOperand getTargetConstantPool(MachineConstantPoolValue *C,
222 MVT VT, unsigned Align = 0,
224 return getConstantPool(C, VT, Align, Offset, true);
226 SDOperand getBasicBlock(MachineBasicBlock *MBB);
227 SDOperand getExternalSymbol(const char *Sym, MVT VT);
228 SDOperand getTargetExternalSymbol(const char *Sym, MVT VT);
229 SDOperand getArgFlags(ISD::ArgFlagsTy Flags);
230 SDOperand getValueType(MVT);
231 SDOperand getRegister(unsigned Reg, MVT VT);
232 SDOperand getDbgStopPoint(SDOperand Root, unsigned Line, unsigned Col,
233 const CompileUnitDesc *CU);
234 SDOperand getLabel(unsigned Opcode, SDOperand Root, unsigned LabelID);
236 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N) {
237 return getNode(ISD::CopyToReg, MVT::Other, Chain,
238 getRegister(Reg, N.getValueType()), N);
241 // This version of the getCopyToReg method takes an extra operand, which
242 // indicates that there is potentially an incoming flag value (if Flag is not
243 // null) and that there should be a flag result.
244 SDOperand getCopyToReg(SDOperand Chain, unsigned Reg, SDOperand N,
246 const MVT *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
247 SDOperand Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Flag };
248 return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
251 // Similar to last getCopyToReg() except parameter Reg is a SDOperand
252 SDOperand getCopyToReg(SDOperand Chain, SDOperand Reg, SDOperand N,
254 const MVT *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
255 SDOperand Ops[] = { Chain, Reg, N, Flag };
256 return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.Val ? 4 : 3);
259 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT VT) {
260 const MVT *VTs = getNodeValueTypes(VT, MVT::Other);
261 SDOperand Ops[] = { Chain, getRegister(Reg, VT) };
262 return getNode(ISD::CopyFromReg, VTs, 2, Ops, 2);
265 // This version of the getCopyFromReg method takes an extra operand, which
266 // indicates that there is potentially an incoming flag value (if Flag is not
267 // null) and that there should be a flag result.
268 SDOperand getCopyFromReg(SDOperand Chain, unsigned Reg, MVT VT,
270 const MVT *VTs = getNodeValueTypes(VT, MVT::Other, MVT::Flag);
271 SDOperand Ops[] = { Chain, getRegister(Reg, VT), Flag };
272 return getNode(ISD::CopyFromReg, VTs, 3, Ops, Flag.Val ? 3 : 2);
275 SDOperand getCondCode(ISD::CondCode Cond);
277 /// getZeroExtendInReg - Return the expression required to zero extend the Op
278 /// value assuming it was the smaller SrcTy value.
279 SDOperand getZeroExtendInReg(SDOperand Op, MVT SrcTy);
281 /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
282 /// a flag result (to ensure it's not CSE'd).
283 SDOperand getCALLSEQ_START(SDOperand Chain, SDOperand Op) {
284 const MVT *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
285 SDOperand Ops[] = { Chain, Op };
286 return getNode(ISD::CALLSEQ_START, VTs, 2, Ops, 2);
289 /// getCALLSEQ_END - Return a new CALLSEQ_END node, which always must have a
290 /// flag result (to ensure it's not CSE'd).
291 SDOperand getCALLSEQ_END(SDOperand Chain, SDOperand Op1, SDOperand Op2,
293 SDVTList NodeTys = getVTList(MVT::Other, MVT::Flag);
294 SmallVector<SDOperand, 4> Ops;
295 Ops.push_back(Chain);
298 Ops.push_back(InFlag);
299 return getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0],
300 (unsigned)Ops.size() - (InFlag.Val == 0 ? 1 : 0));
303 /// getNode - Gets or creates the specified node.
305 SDOperand getNode(unsigned Opcode, MVT VT);
306 SDOperand getNode(unsigned Opcode, MVT VT, SDOperand N);
307 SDOperand getNode(unsigned Opcode, MVT VT, SDOperand N1, SDOperand N2);
308 SDOperand getNode(unsigned Opcode, MVT VT,
309 SDOperand N1, SDOperand N2, SDOperand N3);
310 SDOperand getNode(unsigned Opcode, MVT VT,
311 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
312 SDOperand getNode(unsigned Opcode, MVT VT,
313 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
315 SDOperand getNode(unsigned Opcode, MVT VT,
316 const SDOperand *Ops, unsigned NumOps);
317 SDOperand getNode(unsigned Opcode, MVT VT,
318 const SDUse *Ops, unsigned NumOps);
319 SDOperand getNode(unsigned Opcode, const std::vector<MVT> &ResultTys,
320 const SDOperand *Ops, unsigned NumOps);
321 SDOperand getNode(unsigned Opcode, const MVT *VTs, unsigned NumVTs,
322 const SDOperand *Ops, unsigned NumOps);
323 SDOperand getNode(unsigned Opcode, SDVTList VTs);
324 SDOperand getNode(unsigned Opcode, SDVTList VTs, SDOperand N);
325 SDOperand getNode(unsigned Opcode, SDVTList VTs, SDOperand N1, SDOperand N2);
326 SDOperand getNode(unsigned Opcode, SDVTList VTs,
327 SDOperand N1, SDOperand N2, SDOperand N3);
328 SDOperand getNode(unsigned Opcode, SDVTList VTs,
329 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4);
330 SDOperand getNode(unsigned Opcode, SDVTList VTs,
331 SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4,
333 SDOperand getNode(unsigned Opcode, SDVTList VTs,
334 const SDOperand *Ops, unsigned NumOps);
336 SDOperand getMemcpy(SDOperand Chain, SDOperand Dst, SDOperand Src,
337 SDOperand Size, unsigned Align,
339 const Value *DstSV, uint64_t DstSVOff,
340 const Value *SrcSV, uint64_t SrcSVOff);
342 SDOperand getMemmove(SDOperand Chain, SDOperand Dst, SDOperand Src,
343 SDOperand Size, unsigned Align,
344 const Value *DstSV, uint64_t DstOSVff,
345 const Value *SrcSV, uint64_t SrcSVOff);
347 SDOperand getMemset(SDOperand Chain, SDOperand Dst, SDOperand Src,
348 SDOperand Size, unsigned Align,
349 const Value *DstSV, uint64_t DstSVOff);
351 /// getSetCC - Helper function to make it easier to build SetCC's if you just
352 /// have an ISD::CondCode instead of an SDOperand.
354 SDOperand getSetCC(MVT VT, SDOperand LHS, SDOperand RHS,
355 ISD::CondCode Cond) {
356 return getNode(ISD::SETCC, VT, LHS, RHS, getCondCode(Cond));
359 /// getVSetCC - Helper function to make it easier to build VSetCC's nodes
360 /// if you just have an ISD::CondCode instead of an SDOperand.
362 SDOperand getVSetCC(MVT VT, SDOperand LHS, SDOperand RHS,
363 ISD::CondCode Cond) {
364 return getNode(ISD::VSETCC, VT, LHS, RHS, getCondCode(Cond));
367 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
368 /// just have an ISD::CondCode instead of an SDOperand.
370 SDOperand getSelectCC(SDOperand LHS, SDOperand RHS,
371 SDOperand True, SDOperand False, ISD::CondCode Cond) {
372 return getNode(ISD::SELECT_CC, True.getValueType(), LHS, RHS, True, False,
376 /// getVAArg - VAArg produces a result and token chain, and takes a pointer
377 /// and a source value as input.
378 SDOperand getVAArg(MVT VT, SDOperand Chain, SDOperand Ptr,
381 /// getAtomic - Gets a node for an atomic op, produces result and chain, takes
383 SDOperand getAtomic(unsigned Opcode, SDOperand Chain, SDOperand Ptr,
384 SDOperand Cmp, SDOperand Swp, const Value* PtrVal,
385 unsigned Alignment=0);
387 /// getAtomic - Gets a node for an atomic op, produces result and chain, takes
389 SDOperand getAtomic(unsigned Opcode, SDOperand Chain, SDOperand Ptr,
390 SDOperand Val, const Value* PtrVal,
391 unsigned Alignment = 0);
393 /// getMergeValues - Create a MERGE_VALUES node from the given operands.
394 /// Allowed to return something different (and simpler) if Simplify is true.
395 SDOperand getMergeValues(const SDOperand *Ops, unsigned NumOps,
396 bool Simplify = true);
398 /// getMergeValues - Create a MERGE_VALUES node from the given types and ops.
399 /// Allowed to return something different (and simpler) if Simplify is true.
400 /// May be faster than the above version if VTs is known and NumOps is large.
401 SDOperand getMergeValues(SDVTList VTs, const SDOperand *Ops, unsigned NumOps,
402 bool Simplify = true) {
403 if (Simplify && NumOps == 1)
405 return getNode(ISD::MERGE_VALUES, VTs, Ops, NumOps);
408 /// getLoad - Loads are not normal binary operators: their result type is not
409 /// determined by their operands, and they produce a value AND a token chain.
411 SDOperand getLoad(MVT VT, SDOperand Chain, SDOperand Ptr,
412 const Value *SV, int SVOffset, bool isVolatile=false,
413 unsigned Alignment=0);
414 SDOperand getExtLoad(ISD::LoadExtType ExtType, MVT VT,
415 SDOperand Chain, SDOperand Ptr, const Value *SV,
416 int SVOffset, MVT EVT, bool isVolatile=false,
417 unsigned Alignment=0);
418 SDOperand getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
419 SDOperand Offset, ISD::MemIndexedMode AM);
420 SDOperand getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
421 MVT VT, SDOperand Chain,
422 SDOperand Ptr, SDOperand Offset,
423 const Value *SV, int SVOffset, MVT EVT,
424 bool isVolatile=false, unsigned Alignment=0);
426 /// getStore - Helper function to build ISD::STORE nodes.
428 SDOperand getStore(SDOperand Chain, SDOperand Val, SDOperand Ptr,
429 const Value *SV, int SVOffset, bool isVolatile=false,
430 unsigned Alignment=0);
431 SDOperand getTruncStore(SDOperand Chain, SDOperand Val, SDOperand Ptr,
432 const Value *SV, int SVOffset, MVT TVT,
433 bool isVolatile=false, unsigned Alignment=0);
434 SDOperand getIndexedStore(SDOperand OrigStoe, SDOperand Base,
435 SDOperand Offset, ISD::MemIndexedMode AM);
437 // getSrcValue - Construct a node to track a Value* through the backend.
438 SDOperand getSrcValue(const Value *v);
440 // getMemOperand - Construct a node to track a memory reference
441 // through the backend.
442 SDOperand getMemOperand(const MachineMemOperand &MO);
444 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
445 /// specified operands. If the resultant node already exists in the DAG,
446 /// this does not modify the specified node, instead it returns the node that
447 /// already exists. If the resultant node does not exist in the DAG, the
448 /// input node is returned. As a degenerate case, if you specify the same
449 /// input operands as the node already has, the input node is returned.
450 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op);
451 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2);
452 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
454 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
455 SDOperand Op3, SDOperand Op4);
456 SDOperand UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
457 SDOperand Op3, SDOperand Op4, SDOperand Op5);
458 SDOperand UpdateNodeOperands(SDOperand N,
459 const SDOperand *Ops, unsigned NumOps);
461 /// SelectNodeTo - These are used for target selectors to *mutate* the
462 /// specified node to have the specified return type, Target opcode, and
463 /// operands. Note that target opcodes are stored as
464 /// ~TargetOpcode in the node opcode field. The resultant node is returned.
465 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT);
466 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT, SDOperand Op1);
467 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT,
468 SDOperand Op1, SDOperand Op2);
469 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT,
470 SDOperand Op1, SDOperand Op2, SDOperand Op3);
471 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT,
472 const SDOperand *Ops, unsigned NumOps);
473 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1, MVT VT2);
474 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
475 MVT VT2, const SDOperand *Ops, unsigned NumOps);
476 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
477 MVT VT2, MVT VT3, const SDOperand *Ops, unsigned NumOps);
478 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
479 MVT VT2, SDOperand Op1);
480 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
481 MVT VT2, SDOperand Op1, SDOperand Op2);
482 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
483 MVT VT2, SDOperand Op1, SDOperand Op2, SDOperand Op3);
484 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, SDVTList VTs,
485 const SDOperand *Ops, unsigned NumOps);
487 /// MorphNodeTo - These *mutate* the specified node to have the specified
488 /// return type, opcode, and operands.
489 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT);
490 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT, SDOperand Op1);
491 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT,
492 SDOperand Op1, SDOperand Op2);
493 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT,
494 SDOperand Op1, SDOperand Op2, SDOperand Op3);
495 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT,
496 const SDOperand *Ops, unsigned NumOps);
497 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1, MVT VT2);
498 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1,
499 MVT VT2, const SDOperand *Ops, unsigned NumOps);
500 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1,
501 MVT VT2, MVT VT3, const SDOperand *Ops, unsigned NumOps);
502 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1,
503 MVT VT2, SDOperand Op1);
504 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1,
505 MVT VT2, SDOperand Op1, SDOperand Op2);
506 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1,
507 MVT VT2, SDOperand Op1, SDOperand Op2, SDOperand Op3);
508 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs,
509 const SDOperand *Ops, unsigned NumOps);
511 /// getTargetNode - These are used for target selectors to create a new node
512 /// with specified return type(s), target opcode, and operands.
514 /// Note that getTargetNode returns the resultant node. If there is already a
515 /// node of the specified opcode and operands, it returns that node instead of
517 SDNode *getTargetNode(unsigned Opcode, MVT VT);
518 SDNode *getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1);
519 SDNode *getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1, SDOperand Op2);
520 SDNode *getTargetNode(unsigned Opcode, MVT VT,
521 SDOperand Op1, SDOperand Op2, SDOperand Op3);
522 SDNode *getTargetNode(unsigned Opcode, MVT VT,
523 const SDOperand *Ops, unsigned NumOps);
524 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2);
525 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, SDOperand Op1);
526 SDNode *getTargetNode(unsigned Opcode, MVT VT1,
527 MVT VT2, SDOperand Op1, SDOperand Op2);
528 SDNode *getTargetNode(unsigned Opcode, MVT VT1,
529 MVT VT2, SDOperand Op1, SDOperand Op2, SDOperand Op3);
530 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
531 const SDOperand *Ops, unsigned NumOps);
532 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
533 SDOperand Op1, SDOperand Op2);
534 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
535 SDOperand Op1, SDOperand Op2, SDOperand Op3);
536 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
537 const SDOperand *Ops, unsigned NumOps);
538 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3, MVT VT4,
539 const SDOperand *Ops, unsigned NumOps);
540 SDNode *getTargetNode(unsigned Opcode, const std::vector<MVT> &ResultTys,
541 const SDOperand *Ops, unsigned NumOps);
543 /// getNodeIfExists - Get the specified node if it's already available, or
544 /// else return NULL.
545 SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTs,
546 const SDOperand *Ops, unsigned NumOps);
548 /// DAGUpdateListener - Clients of various APIs that cause global effects on
549 /// the DAG can optionally implement this interface. This allows the clients
550 /// to handle the various sorts of updates that happen.
551 class DAGUpdateListener {
553 virtual ~DAGUpdateListener();
555 /// NodeDeleted - The node N that was deleted and, if E is not null, an
556 /// equivalent node E that replaced it.
557 virtual void NodeDeleted(SDNode *N, SDNode *E) = 0;
559 /// NodeUpdated - The node N that was updated.
560 virtual void NodeUpdated(SDNode *N) = 0;
563 /// RemoveDeadNode - Remove the specified node from the system. If any of its
564 /// operands then becomes dead, remove them as well. Inform UpdateListener
565 /// for each node deleted.
566 void RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener = 0);
568 /// RemoveDeadNodes - This method deletes the unreachable nodes in the
569 /// given list, and any nodes that become unreachable as a result.
570 void RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
571 DAGUpdateListener *UpdateListener = 0);
573 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
574 /// This can cause recursive merging of nodes in the DAG. Use the first
575 /// version if 'From' is known to have a single result, use the second
576 /// if you have two nodes with identical results, use the third otherwise.
578 /// These methods all take an optional UpdateListener, which (if not null) is
579 /// informed about nodes that are deleted and modified due to recursive
580 /// changes in the dag.
582 void ReplaceAllUsesWith(SDOperand From, SDOperand Op,
583 DAGUpdateListener *UpdateListener = 0);
584 void ReplaceAllUsesWith(SDNode *From, SDNode *To,
585 DAGUpdateListener *UpdateListener = 0);
586 void ReplaceAllUsesWith(SDNode *From, const SDOperand *To,
587 DAGUpdateListener *UpdateListener = 0);
589 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
590 /// uses of other values produced by From.Val alone.
591 void ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
592 DAGUpdateListener *UpdateListener = 0);
594 /// ReplaceAllUsesOfValuesWith - Like ReplaceAllUsesOfValueWith, but
595 /// for multiple values at once. This correctly handles the case where
596 /// there is an overlap between the From values and the To values.
597 void ReplaceAllUsesOfValuesWith(const SDOperand *From, const SDOperand *To,
599 DAGUpdateListener *UpdateListener = 0);
601 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
602 /// based on their topological order. It returns the maximum id and a vector
603 /// of the SDNodes* in assigned order by reference.
604 unsigned AssignTopologicalOrder(std::vector<SDNode*> &TopOrder);
606 /// isCommutativeBinOp - Returns true if the opcode is a commutative binary
608 static bool isCommutativeBinOp(unsigned Opcode) {
609 // FIXME: This should get its info from the td file, so that we can include
624 case ISD::ADDE: return true;
625 default: return false;
631 /// CreateStackTemporary - Create a stack temporary, suitable for holding the
632 /// specified value type. If minAlign is specified, the slot size will have
633 /// at least that alignment.
634 SDOperand CreateStackTemporary(MVT VT, unsigned minAlign = 1);
636 /// FoldSetCC - Constant fold a setcc to true or false.
637 SDOperand FoldSetCC(MVT VT, SDOperand N1,
638 SDOperand N2, ISD::CondCode Cond);
640 /// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
641 /// use this predicate to simplify operations downstream.
642 bool SignBitIsZero(SDOperand Op, unsigned Depth = 0) const;
644 /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We
645 /// use this predicate to simplify operations downstream. Op and Mask are
646 /// known to be the same type.
647 bool MaskedValueIsZero(SDOperand Op, const APInt &Mask, unsigned Depth = 0)
650 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
651 /// known to be either zero or one and return them in the KnownZero/KnownOne
652 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
653 /// processing. Targets can implement the computeMaskedBitsForTargetNode
654 /// method in the TargetLowering class to allow target nodes to be understood.
655 void ComputeMaskedBits(SDOperand Op, const APInt &Mask, APInt &KnownZero,
656 APInt &KnownOne, unsigned Depth = 0) const;
658 /// ComputeNumSignBits - Return the number of times the sign bit of the
659 /// register is replicated into the other bits. We know that at least 1 bit
660 /// is always equal to the sign bit (itself), but other cases can give us
661 /// information. For example, immediately after an "SRA X, 2", we know that
662 /// the top 3 bits are all equal to each other, so we return 3. Targets can
663 /// implement the ComputeNumSignBitsForTarget method in the TargetLowering
664 /// class to allow target nodes to be understood.
665 unsigned ComputeNumSignBits(SDOperand Op, unsigned Depth = 0) const;
667 /// isVerifiedDebugInfoDesc - Returns true if the specified SDOperand has
668 /// been verified as a debug information descriptor.
669 bool isVerifiedDebugInfoDesc(SDOperand Op) const;
671 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
672 /// element of the result of the vector shuffle.
673 SDOperand getShuffleScalarElt(const SDNode *N, unsigned Idx);
676 inline alist_traits<SDNode, LargestSDNode>::AllocatorType &getAllocator();
677 void RemoveNodeFromCSEMaps(SDNode *N);
678 SDNode *AddNonLeafNodeToCSEMaps(SDNode *N);
679 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op, void *&InsertPos);
680 SDNode *FindModifiedNodeSlot(SDNode *N, SDOperand Op1, SDOperand Op2,
682 SDNode *FindModifiedNodeSlot(SDNode *N, const SDOperand *Ops, unsigned NumOps,
685 void DeleteNodeNotInCSEMaps(SDNode *N);
687 unsigned getMVTAlignment(MVT MemoryVT) const;
689 // List of non-single value types.
690 std::vector<SDVTList> VTList;
692 // Maps to auto-CSE operations.
693 std::vector<CondCodeSDNode*> CondCodeNodes;
695 std::vector<SDNode*> ValueTypeNodes;
696 std::map<MVT, SDNode*, MVT::compareRawBits> ExtendedValueTypeNodes;
697 StringMap<SDNode*> ExternalSymbols;
698 StringMap<SDNode*> TargetExternalSymbols;
701 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
702 typedef SelectionDAG::allnodes_iterator nodes_iterator;
703 static nodes_iterator nodes_begin(SelectionDAG *G) {
704 return G->allnodes_begin();
706 static nodes_iterator nodes_end(SelectionDAG *G) {
707 return G->allnodes_end();
711 } // end namespace llvm