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/ilist.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/CodeGen/SelectionDAGNodes.h"
34 class MachineModuleInfo;
36 class MachineFunction;
37 class MachineConstantPoolValue;
38 class FunctionLoweringInfo;
40 template<> struct ilist_traits<SDNode> : public ilist_default_traits<SDNode> {
42 mutable SDNode Sentinel;
44 ilist_traits() : Sentinel(ISD::DELETED_NODE, SDVTList()) {}
46 SDNode *createSentinel() const {
49 static void destroySentinel(SDNode *) {}
51 static void deleteNode(SDNode *) {
52 assert(0 && "ilist_traits<SDNode> shouldn't see a deleteNode call!");
55 static void createNode(const SDNode &);
59 Unrestricted, // Combine may create illegal operations and illegal types.
60 NoIllegalTypes, // Combine may create illegal operations but no illegal types.
61 NoIllegalOperations // Combine may only create legal operations and types.
64 /// SelectionDAG class - This is used to represent a portion of an LLVM function
65 /// in a low-level Data Dependence DAG representation suitable for instruction
66 /// selection. This DAG is constructed as the first step of instruction
67 /// selection in order to allow implementation of machine specific optimizations
68 /// and code simplifications.
70 /// The representation used by the SelectionDAG is a target-independent
71 /// representation, which has some similarities to the GCC RTL representation,
72 /// but is significantly more simple, powerful, and is a graph form instead of a
78 FunctionLoweringInfo &FLI;
79 MachineModuleInfo *MMI;
82 /// CurDebugLoc - current file + line number. Changes as we build the DAG.
85 /// EntryNode - The starting token.
88 /// Root - The root of the entire DAG.
91 /// AllNodes - A linked list of nodes in the current DAG.
92 ilist<SDNode> AllNodes;
94 /// NodeAllocatorType - The AllocatorType for allocating SDNodes. We use
95 /// pool allocation with recycling.
96 typedef RecyclingAllocator<BumpPtrAllocator, SDNode, sizeof(LargestSDNode),
97 AlignOf<MostAlignedSDNode>::Alignment>
100 /// NodeAllocator - Pool allocation for nodes.
101 NodeAllocatorType NodeAllocator;
103 /// CSEMap - This structure is used to memoize nodes, automatically performing
104 /// CSE with existing nodes with a duplicate is requested.
105 FoldingSet<SDNode> CSEMap;
107 /// OperandAllocator - Pool allocation for machine-opcode SDNode operands.
108 BumpPtrAllocator OperandAllocator;
110 /// Allocator - Pool allocation for misc. objects that are created once per
112 BumpPtrAllocator Allocator;
114 /// VerifyNode - Sanity check the given node. Aborts if it is invalid.
115 void VerifyNode(SDNode *N);
117 /// setGraphColorHelper - Implementation of setSubgraphColor.
118 /// Return whether we had to truncate the search.
120 bool setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
121 int level, bool &printed);
124 SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli);
127 /// init - Prepare this SelectionDAG to process code in the given
130 void init(MachineFunction &mf, MachineModuleInfo *mmi, DwarfWriter *dw);
132 /// clear - Clear state and free memory necessary to make this
133 /// SelectionDAG ready to process a new block.
137 MachineFunction &getMachineFunction() const { return *MF; }
138 const TargetMachine &getTarget() const;
139 TargetLowering &getTargetLoweringInfo() const { return TLI; }
140 FunctionLoweringInfo &getFunctionLoweringInfo() const { return FLI; }
141 MachineModuleInfo *getMachineModuleInfo() const { return MMI; }
142 DwarfWriter *getDwarfWriter() const { return DW; }
143 DebugLoc getCurDebugLoc() const { return CurDebugLoc; }
145 /// viewGraph - Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
147 void viewGraph(const std::string &Title);
151 std::map<const SDNode *, std::string> NodeGraphAttrs;
154 /// clearGraphAttrs - Clear all previously defined node graph attributes.
155 /// Intended to be used from a debugging tool (eg. gdb).
156 void clearGraphAttrs();
158 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
160 void setGraphAttrs(const SDNode *N, const char *Attrs);
162 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
163 /// Used from getNodeAttributes.
164 const std::string getGraphAttrs(const SDNode *N) const;
166 /// setGraphColor - Convenience for setting node color attribute.
168 void setGraphColor(const SDNode *N, const char *Color);
170 /// setGraphColor - Convenience for setting subgraph color attribute.
172 void setSubgraphColor(SDNode *N, const char *Color);
174 typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
175 allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
176 allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
177 typedef ilist<SDNode>::iterator allnodes_iterator;
178 allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
179 allnodes_iterator allnodes_end() { return AllNodes.end(); }
180 ilist<SDNode>::size_type allnodes_size() const {
181 return AllNodes.size();
184 /// getRoot - Return the root tag of the SelectionDAG.
186 const SDValue &getRoot() const { return Root; }
188 /// getEntryNode - Return the token chain corresponding to the entry of the
190 SDValue getEntryNode() const {
191 return SDValue(const_cast<SDNode *>(&EntryNode), 0);
194 /// setRoot - Set the current root tag of the SelectionDAG.
196 const SDValue &setRoot(SDValue N) {
197 assert((!N.getNode() || N.getValueType() == MVT::Other) &&
198 "DAG root value is not a chain!");
202 void setCurDebugLoc(DebugLoc dl) { CurDebugLoc = dl; }
204 /// Combine - This iterates over the nodes in the SelectionDAG, folding
205 /// certain types of nodes together, or eliminating superfluous nodes. The
206 /// Level argument controls whether Combine is allowed to produce nodes and
207 /// types that are illegal on the target.
208 void Combine(CombineLevel Level, AliasAnalysis &AA, bool Fast);
210 /// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that
211 /// only uses types natively supported by the target. Returns "true" if it
212 /// made any changes.
214 /// Note that this is an involved process that may invalidate pointers into
216 bool LegalizeTypes();
218 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
219 /// compatible with the target instruction selector, as indicated by the
220 /// TargetLowering object.
222 /// Note that this is an involved process that may invalidate pointers into
224 void Legalize(bool TypesNeedLegalizing);
226 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
228 void RemoveDeadNodes();
230 /// DeleteNode - Remove the specified node from the system. This node must
231 /// have no referrers.
232 void DeleteNode(SDNode *N);
234 /// getVTList - Return an SDVTList that represents the list of values
236 SDVTList getVTList(MVT VT);
237 SDVTList getVTList(MVT VT1, MVT VT2);
238 SDVTList getVTList(MVT VT1, MVT VT2, MVT VT3);
239 SDVTList getVTList(MVT VT1, MVT VT2, MVT VT3, MVT VT4);
240 SDVTList getVTList(const MVT *VTs, unsigned NumVTs);
242 /// getNodeValueTypes - These are obsolete, use getVTList instead.
243 const MVT *getNodeValueTypes(MVT VT) {
244 return getVTList(VT).VTs;
246 const MVT *getNodeValueTypes(MVT VT1, MVT VT2) {
247 return getVTList(VT1, VT2).VTs;
249 const MVT *getNodeValueTypes(MVT VT1, MVT VT2, MVT VT3) {
250 return getVTList(VT1, VT2, VT3).VTs;
252 const MVT *getNodeValueTypes(MVT VT1, MVT VT2, MVT VT3, MVT VT4) {
253 return getVTList(VT1, VT2, VT3, VT4).VTs;
255 const MVT *getNodeValueTypes(const std::vector<MVT> &vtList) {
256 return getVTList(&vtList[0], (unsigned)vtList.size()).VTs;
260 //===--------------------------------------------------------------------===//
261 // Node creation methods.
263 SDValue getConstant(uint64_t Val, MVT VT, bool isTarget = false);
264 SDValue getConstant(const APInt &Val, MVT VT, bool isTarget = false);
265 SDValue getConstant(const ConstantInt &Val, MVT VT, bool isTarget = false);
266 SDValue getIntPtrConstant(uint64_t Val, bool isTarget = false);
267 SDValue getTargetConstant(uint64_t Val, MVT VT) {
268 return getConstant(Val, VT, true);
270 SDValue getTargetConstant(const APInt &Val, MVT VT) {
271 return getConstant(Val, VT, true);
273 SDValue getTargetConstant(const ConstantInt &Val, MVT VT) {
274 return getConstant(Val, VT, true);
276 SDValue getConstantFP(double Val, MVT VT, bool isTarget = false);
277 SDValue getConstantFP(const APFloat& Val, MVT VT, bool isTarget = false);
278 SDValue getConstantFP(const ConstantFP &CF, MVT VT, bool isTarget = false);
279 SDValue getTargetConstantFP(double Val, MVT VT) {
280 return getConstantFP(Val, VT, true);
282 SDValue getTargetConstantFP(const APFloat& Val, MVT VT) {
283 return getConstantFP(Val, VT, true);
285 SDValue getTargetConstantFP(const ConstantFP &Val, MVT VT) {
286 return getConstantFP(Val, VT, true);
288 SDValue getGlobalAddress(const GlobalValue *GV, MVT VT,
289 int64_t offset = 0, bool isTargetGA = false);
290 SDValue getTargetGlobalAddress(const GlobalValue *GV, MVT VT,
291 int64_t offset = 0) {
292 return getGlobalAddress(GV, VT, offset, true);
294 SDValue getFrameIndex(int FI, MVT VT, bool isTarget = false);
295 SDValue getTargetFrameIndex(int FI, MVT VT) {
296 return getFrameIndex(FI, VT, true);
298 SDValue getJumpTable(int JTI, MVT VT, bool isTarget = false);
299 SDValue getTargetJumpTable(int JTI, MVT VT) {
300 return getJumpTable(JTI, VT, true);
302 SDValue getConstantPool(Constant *C, MVT VT,
303 unsigned Align = 0, int Offs = 0, bool isT=false);
304 SDValue getTargetConstantPool(Constant *C, MVT VT,
305 unsigned Align = 0, int Offset = 0) {
306 return getConstantPool(C, VT, Align, Offset, true);
308 SDValue getConstantPool(MachineConstantPoolValue *C, MVT VT,
309 unsigned Align = 0, int Offs = 0, bool isT=false);
310 SDValue getTargetConstantPool(MachineConstantPoolValue *C,
311 MVT VT, unsigned Align = 0,
313 return getConstantPool(C, VT, Align, Offset, true);
315 SDValue getBasicBlock(MachineBasicBlock *MBB);
316 SDValue getBasicBlock(MachineBasicBlock *MBB, DebugLoc dl);
317 SDValue getExternalSymbol(const char *Sym, MVT VT);
318 SDValue getExternalSymbol(const char *Sym, DebugLoc dl, MVT VT);
319 SDValue getTargetExternalSymbol(const char *Sym, MVT VT);
320 SDValue getTargetExternalSymbol(const char *Sym, DebugLoc dl, MVT VT);
321 SDValue getArgFlags(ISD::ArgFlagsTy Flags);
322 SDValue getValueType(MVT);
323 SDValue getRegister(unsigned Reg, MVT VT);
324 SDValue getDbgStopPoint(SDValue Root, unsigned Line, unsigned Col,
326 SDValue getLabel(unsigned Opcode, SDValue Root, unsigned LabelID);
327 SDValue getLabel(unsigned Opcode, DebugLoc dl, SDValue Root,
330 SDValue getCopyToReg(SDValue Chain, unsigned Reg, SDValue N) {
331 return getNode(ISD::CopyToReg, MVT::Other, Chain,
332 getRegister(Reg, N.getValueType()), N);
335 // This version of the getCopyToReg method takes an extra operand, which
336 // indicates that there is potentially an incoming flag value (if Flag is not
337 // null) and that there should be a flag result.
338 SDValue getCopyToReg(SDValue Chain, unsigned Reg, SDValue N,
340 const MVT *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
341 SDValue Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Flag };
342 return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.getNode() ? 4 : 3);
345 // Similar to last getCopyToReg() except parameter Reg is a SDValue
346 SDValue getCopyToReg(SDValue Chain, SDValue Reg, SDValue N,
348 const MVT *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
349 SDValue Ops[] = { Chain, Reg, N, Flag };
350 return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.getNode() ? 4 : 3);
353 SDValue getCopyFromReg(SDValue Chain, unsigned Reg, MVT VT) {
354 const MVT *VTs = getNodeValueTypes(VT, MVT::Other);
355 SDValue Ops[] = { Chain, getRegister(Reg, VT) };
356 return getNode(ISD::CopyFromReg, VTs, 2, Ops, 2);
359 // This version of the getCopyFromReg method takes an extra operand, which
360 // indicates that there is potentially an incoming flag value (if Flag is not
361 // null) and that there should be a flag result.
362 SDValue getCopyFromReg(SDValue Chain, unsigned Reg, MVT VT,
364 const MVT *VTs = getNodeValueTypes(VT, MVT::Other, MVT::Flag);
365 SDValue Ops[] = { Chain, getRegister(Reg, VT), Flag };
366 return getNode(ISD::CopyFromReg, VTs, 3, Ops, Flag.getNode() ? 3 : 2);
369 SDValue getCondCode(ISD::CondCode Cond);
371 /// Returns the ConvertRndSat Note: Avoid using this node because it may
372 /// disappear in the future and most targets don't support it.
373 SDValue getConvertRndSat(MVT VT, SDValue Val, SDValue DTy, SDValue STy,
374 SDValue Rnd, SDValue Sat, ISD::CvtCode Code);
376 /// getZeroExtendInReg - Return the expression required to zero extend the Op
377 /// value assuming it was the smaller SrcTy value.
378 SDValue getZeroExtendInReg(SDValue Op, MVT SrcTy);
379 SDValue getZeroExtendInReg(SDValue Op, DebugLoc DL, MVT SrcTy);
381 /// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
382 SDValue getNOT(SDValue Val, MVT VT);
383 SDValue getNOT(DebugLoc DL, SDValue Val, MVT VT);
385 /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
386 /// a flag result (to ensure it's not CSE'd).
387 SDValue getCALLSEQ_START(SDValue Chain, SDValue Op) {
388 const MVT *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
389 SDValue Ops[] = { Chain, Op };
390 return getNode(ISD::CALLSEQ_START, VTs, 2, Ops, 2);
393 /// getCALLSEQ_END - Return a new CALLSEQ_END node, which always must have a
394 /// flag result (to ensure it's not CSE'd).
395 SDValue getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2,
397 SDVTList NodeTys = getVTList(MVT::Other, MVT::Flag);
398 SmallVector<SDValue, 4> Ops;
399 Ops.push_back(Chain);
402 Ops.push_back(InFlag);
403 return getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0],
404 (unsigned)Ops.size() - (InFlag.getNode() == 0 ? 1 : 0));
407 /// getNode - Gets or creates the specified node.
409 SDValue getNode(unsigned Opcode, MVT VT);
410 SDValue getNode(unsigned Opcode, DebugLoc DL, MVT VT);
411 SDValue getNode(unsigned Opcode, MVT VT, SDValue N);
412 SDValue getNode(unsigned Opcode, DebugLoc DL, MVT VT, SDValue N);
413 SDValue getNode(unsigned Opcode, MVT VT, SDValue N1, SDValue N2);
414 SDValue getNode(unsigned Opcode, DebugLoc DL, MVT VT, SDValue N1, SDValue N2);
415 SDValue getNode(unsigned Opcode, MVT VT,
416 SDValue N1, SDValue N2, SDValue N3);
417 SDValue getNode(unsigned Opcode, DebugLoc DL, MVT VT,
418 SDValue N1, SDValue N2, SDValue N3);
419 SDValue getNode(unsigned Opcode, MVT VT,
420 SDValue N1, SDValue N2, SDValue N3, SDValue N4);
421 SDValue getNode(unsigned Opcode, DebugLoc DL, MVT VT,
422 SDValue N1, SDValue N2, SDValue N3, SDValue N4);
423 SDValue getNode(unsigned Opcode, MVT VT,
424 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
426 SDValue getNode(unsigned Opcode, DebugLoc DL, MVT VT,
427 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
429 SDValue getNode(unsigned Opcode, MVT VT,
430 const SDUse *Ops, unsigned NumOps);
431 SDValue getNode(unsigned Opcode, DebugLoc DL, MVT VT,
432 const SDUse *Ops, unsigned NumOps);
433 SDValue getNode(unsigned Opcode, MVT VT,
434 const SDValue *Ops, unsigned NumOps);
435 SDValue getNode(unsigned Opcode, DebugLoc DL, MVT VT,
436 const SDValue *Ops, unsigned NumOps);
437 SDValue getNode(unsigned Opcode, const std::vector<MVT> &ResultTys,
438 const SDValue *Ops, unsigned NumOps);
439 SDValue getNode(unsigned Opcode, DebugLoc DL,
440 const std::vector<MVT> &ResultTys,
441 const SDValue *Ops, unsigned NumOps);
442 SDValue getNode(unsigned Opcode, const MVT *VTs, unsigned NumVTs,
443 const SDValue *Ops, unsigned NumOps);
444 SDValue getNode(unsigned Opcode, DebugLoc DL, const MVT *VTs, unsigned NumVTs,
445 const SDValue *Ops, unsigned NumOps);
446 SDValue getNode(unsigned Opcode, SDVTList VTs,
447 const SDValue *Ops, unsigned NumOps);
448 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
449 const SDValue *Ops, unsigned NumOps);
451 SDValue getNode(unsigned Opcode, SDVTList VTs);
452 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs);
453 SDValue getNode(unsigned Opcode, SDVTList VTs, SDValue N);
454 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs, SDValue N);
455 SDValue getNode(unsigned Opcode, SDVTList VTs, SDValue N1, SDValue N2);
456 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
457 SDValue N1, SDValue N2);
458 SDValue getNode(unsigned Opcode, SDVTList VTs,
459 SDValue N1, SDValue N2, SDValue N3);
460 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
461 SDValue N1, SDValue N2, SDValue N3);
462 SDValue getNode(unsigned Opcode, SDVTList VTs,
463 SDValue N1, SDValue N2, SDValue N3, SDValue N4);
464 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
465 SDValue N1, SDValue N2, SDValue N3, SDValue N4);
466 SDValue getNode(unsigned Opcode, SDVTList VTs,
467 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
469 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
470 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
473 SDValue getMemcpy(SDValue Chain, SDValue Dst, SDValue Src,
474 SDValue Size, unsigned Align, bool AlwaysInline,
475 const Value *DstSV, uint64_t DstSVOff,
476 const Value *SrcSV, uint64_t SrcSVOff);
478 SDValue getMemmove(SDValue Chain, SDValue Dst, SDValue Src,
479 SDValue Size, unsigned Align,
480 const Value *DstSV, uint64_t DstOSVff,
481 const Value *SrcSV, uint64_t SrcSVOff);
483 SDValue getMemset(SDValue Chain, SDValue Dst, SDValue Src,
484 SDValue Size, unsigned Align,
485 const Value *DstSV, uint64_t DstSVOff);
487 /// getSetCC - Helper function to make it easier to build SetCC's if you just
488 /// have an ISD::CondCode instead of an SDValue.
490 SDValue getSetCC(MVT VT, SDValue LHS, SDValue RHS,
491 ISD::CondCode Cond) {
492 return getNode(ISD::SETCC, DebugLoc::getUnknownLoc(), VT,
493 LHS, RHS, getCondCode(Cond));
495 SDValue getSetCC(DebugLoc DL, MVT VT, SDValue LHS, SDValue RHS,
496 ISD::CondCode Cond) {
497 return getNode(ISD::SETCC, DL, VT, LHS, RHS, getCondCode(Cond));
500 /// getVSetCC - Helper function to make it easier to build VSetCC's nodes
501 /// if you just have an ISD::CondCode instead of an SDValue.
503 SDValue getVSetCC(MVT VT, SDValue LHS, SDValue RHS,
504 ISD::CondCode Cond) {
505 return getNode(ISD::VSETCC, DebugLoc::getUnknownLoc(), VT,
506 LHS, RHS, getCondCode(Cond));
508 SDValue getVSetCC(DebugLoc DL, MVT VT, SDValue LHS, SDValue RHS,
509 ISD::CondCode Cond) {
510 return getNode(ISD::VSETCC, DL, VT, LHS, RHS, getCondCode(Cond));
513 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
514 /// just have an ISD::CondCode instead of an SDValue.
516 SDValue getSelectCC(SDValue LHS, SDValue RHS,
517 SDValue True, SDValue False, ISD::CondCode Cond) {
518 return getNode(ISD::SELECT_CC, DebugLoc::getUnknownLoc(), True.getValueType(),
519 LHS, RHS, True, False, getCondCode(Cond));
521 SDValue getSelectCC(DebugLoc DL, SDValue LHS, SDValue RHS,
522 SDValue True, SDValue False, ISD::CondCode Cond) {
523 return getNode(ISD::SELECT_CC, DL, True.getValueType(),
524 LHS, RHS, True, False, getCondCode(Cond));
527 /// getVAArg - VAArg produces a result and token chain, and takes a pointer
528 /// and a source value as input.
529 SDValue getVAArg(MVT VT, SDValue Chain, SDValue Ptr,
532 /// getAtomic - Gets a node for an atomic op, produces result and chain and
534 SDValue getAtomic(unsigned Opcode, MVT MemVT, SDValue Chain, SDValue Ptr,
535 SDValue Cmp, SDValue Swp, const Value* PtrVal,
536 unsigned Alignment=0);
537 SDValue getAtomic(unsigned Opcode, DebugLoc dl, MVT MemVT, SDValue Chain,
538 SDValue Ptr, SDValue Cmp, SDValue Swp, const Value* PtrVal,
539 unsigned Alignment=0);
541 /// getAtomic - Gets a node for an atomic op, produces result and chain and
542 /// takes 2 operands.
543 SDValue getAtomic(unsigned Opcode, MVT MemVT, SDValue Chain, SDValue Ptr,
544 SDValue Val, const Value* PtrVal,
545 unsigned Alignment = 0);
546 SDValue getAtomic(unsigned Opcode, DebugLoc dl, MVT MemVT, SDValue Chain,
547 SDValue Ptr, SDValue Val, const Value* PtrVal,
548 unsigned Alignment = 0);
550 /// getMemIntrinsicNode - Creates a MemIntrinsicNode that may produce a
551 /// result and takes a list of operands.
552 SDValue getMemIntrinsicNode(unsigned Opcode,
553 const MVT *VTs, unsigned NumVTs,
554 const SDValue *Ops, unsigned NumOps,
555 MVT MemVT, const Value *srcValue, int SVOff,
556 unsigned Align = 0, bool Vol = false,
557 bool ReadMem = true, bool WriteMem = true);
558 SDValue getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
559 const MVT *VTs, unsigned NumVTs,
560 const SDValue *Ops, unsigned NumOps,
561 MVT MemVT, const Value *srcValue, int SVOff,
562 unsigned Align = 0, bool Vol = false,
563 bool ReadMem = true, bool WriteMem = true);
565 SDValue getMemIntrinsicNode(unsigned Opcode, SDVTList VTList,
566 const SDValue *Ops, unsigned NumOps,
567 MVT MemVT, const Value *srcValue, int SVOff,
568 unsigned Align = 0, bool Vol = false,
569 bool ReadMem = true, bool WriteMem = true);
570 SDValue getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
571 const SDValue *Ops, unsigned NumOps,
572 MVT MemVT, const Value *srcValue, int SVOff,
573 unsigned Align = 0, bool Vol = false,
574 bool ReadMem = true, bool WriteMem = true);
576 /// getMergeValues - Create a MERGE_VALUES node from the given operands.
577 SDValue getMergeValues(const SDValue *Ops, unsigned NumOps);
579 /// getCall - Create a CALL node from the given information.
581 SDValue getCall(unsigned CallingConv, bool IsVarArgs, bool IsTailCall,
582 bool isInreg, SDVTList VTs, const SDValue *Operands,
583 unsigned NumOperands);
584 SDValue getCall(unsigned CallingConv, DebugLoc dl, bool IsVarArgs,
585 bool IsTailCall, bool isInreg, SDVTList VTs,
586 const SDValue *Operands, unsigned NumOperands);
588 /// getLoad - Loads are not normal binary operators: their result type is not
589 /// determined by their operands, and they produce a value AND a token chain.
591 SDValue getLoad(MVT VT, SDValue Chain, SDValue Ptr,
592 const Value *SV, int SVOffset, bool isVolatile=false,
593 unsigned Alignment=0);
594 SDValue getLoad(MVT VT, DebugLoc dl, SDValue Chain, SDValue Ptr,
595 const Value *SV, int SVOffset, bool isVolatile=false,
596 unsigned Alignment=0);
597 SDValue getExtLoad(ISD::LoadExtType ExtType, MVT VT,
598 SDValue Chain, SDValue Ptr, const Value *SV,
599 int SVOffset, MVT EVT, bool isVolatile=false,
600 unsigned Alignment=0);
601 SDValue getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, MVT VT,
602 SDValue Chain, SDValue Ptr, const Value *SV,
603 int SVOffset, MVT EVT, bool isVolatile=false,
604 unsigned Alignment=0);
605 SDValue getIndexedLoad(SDValue OrigLoad, SDValue Base,
606 SDValue Offset, ISD::MemIndexedMode AM);
607 SDValue getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
608 SDValue Offset, ISD::MemIndexedMode AM);
609 SDValue getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
610 MVT VT, SDValue Chain,
611 SDValue Ptr, SDValue Offset,
612 const Value *SV, int SVOffset, MVT EVT,
613 bool isVolatile=false, unsigned Alignment=0);
614 SDValue getLoad(ISD::MemIndexedMode AM, DebugLoc dl, ISD::LoadExtType ExtType,
615 MVT VT, SDValue Chain,
616 SDValue Ptr, SDValue Offset,
617 const Value *SV, int SVOffset, MVT EVT,
618 bool isVolatile=false, unsigned Alignment=0);
620 /// getStore - Helper function to build ISD::STORE nodes.
622 SDValue getStore(SDValue Chain, SDValue Val, SDValue Ptr,
623 const Value *SV, int SVOffset, bool isVolatile=false,
624 unsigned Alignment=0);
625 SDValue getStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
626 const Value *SV, int SVOffset, bool isVolatile=false,
627 unsigned Alignment=0);
628 SDValue getTruncStore(SDValue Chain, SDValue Val, SDValue Ptr,
629 const Value *SV, int SVOffset, MVT TVT,
630 bool isVolatile=false, unsigned Alignment=0);
631 SDValue getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
632 const Value *SV, int SVOffset, MVT TVT,
633 bool isVolatile=false, unsigned Alignment=0);
634 SDValue getIndexedStore(SDValue OrigStoe, SDValue Base,
635 SDValue Offset, ISD::MemIndexedMode AM);
636 SDValue getIndexedStore(SDValue OrigStoe, DebugLoc dl, SDValue Base,
637 SDValue Offset, ISD::MemIndexedMode AM);
639 /// getSrcValue - Construct a node to track a Value* through the backend.
640 SDValue getSrcValue(const Value *v);
642 /// getMemOperand - Construct a node to track a memory reference
643 /// through the backend.
644 SDValue getMemOperand(const MachineMemOperand &MO);
646 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
647 /// specified operands. If the resultant node already exists in the DAG,
648 /// this does not modify the specified node, instead it returns the node that
649 /// already exists. If the resultant node does not exist in the DAG, the
650 /// input node is returned. As a degenerate case, if you specify the same
651 /// input operands as the node already has, the input node is returned.
652 SDValue UpdateNodeOperands(SDValue N, SDValue Op);
653 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2);
654 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
656 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
657 SDValue Op3, SDValue Op4);
658 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
659 SDValue Op3, SDValue Op4, SDValue Op5);
660 SDValue UpdateNodeOperands(SDValue N,
661 const SDValue *Ops, unsigned NumOps);
663 /// SelectNodeTo - These are used for target selectors to *mutate* the
664 /// specified node to have the specified return type, Target opcode, and
665 /// operands. Note that target opcodes are stored as
666 /// ~TargetOpcode in the node opcode field. The resultant node is returned.
667 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT);
668 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT, SDValue Op1);
669 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT,
670 SDValue Op1, SDValue Op2);
671 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT,
672 SDValue Op1, SDValue Op2, SDValue Op3);
673 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT,
674 const SDValue *Ops, unsigned NumOps);
675 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1, MVT VT2);
676 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
677 MVT VT2, const SDValue *Ops, unsigned NumOps);
678 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
679 MVT VT2, MVT VT3, const SDValue *Ops, unsigned NumOps);
680 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, MVT VT1,
681 MVT VT2, MVT VT3, MVT VT4, const SDValue *Ops,
683 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
684 MVT VT2, SDValue Op1);
685 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
686 MVT VT2, SDValue Op1, SDValue Op2);
687 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
688 MVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
689 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
690 MVT VT2, MVT VT3, SDValue Op1, SDValue Op2, SDValue Op3);
691 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, SDVTList VTs,
692 const SDValue *Ops, unsigned NumOps);
694 /// MorphNodeTo - These *mutate* the specified node to have the specified
695 /// return type, opcode, and operands.
696 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT);
697 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT, SDValue Op1);
698 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT,
699 SDValue Op1, SDValue Op2);
700 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT,
701 SDValue Op1, SDValue Op2, SDValue Op3);
702 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT,
703 const SDValue *Ops, unsigned NumOps);
704 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1, MVT VT2);
705 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1,
706 MVT VT2, const SDValue *Ops, unsigned NumOps);
707 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1,
708 MVT VT2, MVT VT3, const SDValue *Ops, unsigned NumOps);
709 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1,
710 MVT VT2, SDValue Op1);
711 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1,
712 MVT VT2, SDValue Op1, SDValue Op2);
713 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1,
714 MVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
715 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs,
716 const SDValue *Ops, unsigned NumOps);
718 /// getTargetNode - These are used for target selectors to create a new node
719 /// with specified return type(s), target opcode, and operands.
721 /// Note that getTargetNode returns the resultant node. If there is already a
722 /// node of the specified opcode and operands, it returns that node instead of
724 SDNode *getTargetNode(unsigned Opcode, MVT VT);
725 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT);
727 SDNode *getTargetNode(unsigned Opcode, MVT VT, SDValue Op1);
728 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT, SDValue Op1);
730 SDNode *getTargetNode(unsigned Opcode, MVT VT, SDValue Op1, SDValue Op2);
731 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT, SDValue Op1,
734 SDNode *getTargetNode(unsigned Opcode, MVT VT,
735 SDValue Op1, SDValue Op2, SDValue Op3);
736 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
737 SDValue Op1, SDValue Op2, SDValue Op3);
739 SDNode *getTargetNode(unsigned Opcode, MVT VT,
740 const SDValue *Ops, unsigned NumOps);
741 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
742 const SDValue *Ops, unsigned NumOps);
744 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2);
745 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1, MVT VT2);
747 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, SDValue Op1);
748 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1, MVT VT2,
751 SDNode *getTargetNode(unsigned Opcode, MVT VT1,
752 MVT VT2, SDValue Op1, SDValue Op2);
753 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
754 MVT VT2, SDValue Op1, SDValue Op2);
756 SDNode *getTargetNode(unsigned Opcode, MVT VT1,
757 MVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
758 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
759 MVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
761 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
762 const SDValue *Ops, unsigned NumOps);
763 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1, MVT VT2,
764 const SDValue *Ops, unsigned NumOps);
766 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
767 SDValue Op1, SDValue Op2);
768 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1, MVT VT2, MVT VT3,
769 SDValue Op1, SDValue Op2);
771 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
772 SDValue Op1, SDValue Op2, SDValue Op3);
773 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1, MVT VT2, MVT VT3,
774 SDValue Op1, SDValue Op2, SDValue Op3);
776 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
777 const SDValue *Ops, unsigned NumOps);
778 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1, MVT VT2, MVT VT3,
779 const SDValue *Ops, unsigned NumOps);
781 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3, MVT VT4,
782 const SDValue *Ops, unsigned NumOps);
783 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1, MVT VT2, MVT VT3,
784 MVT VT4, const SDValue *Ops, unsigned NumOps);
786 SDNode *getTargetNode(unsigned Opcode, const std::vector<MVT> &ResultTys,
787 const SDValue *Ops, unsigned NumOps);
788 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl,
789 const std::vector<MVT> &ResultTys, const SDValue *Ops,
792 /// getNodeIfExists - Get the specified node if it's already available, or
793 /// else return NULL.
794 SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTs,
795 const SDValue *Ops, unsigned NumOps);
797 /// DAGUpdateListener - Clients of various APIs that cause global effects on
798 /// the DAG can optionally implement this interface. This allows the clients
799 /// to handle the various sorts of updates that happen.
800 class DAGUpdateListener {
802 virtual ~DAGUpdateListener();
804 /// NodeDeleted - The node N that was deleted and, if E is not null, an
805 /// equivalent node E that replaced it.
806 virtual void NodeDeleted(SDNode *N, SDNode *E) = 0;
808 /// NodeUpdated - The node N that was updated.
809 virtual void NodeUpdated(SDNode *N) = 0;
812 /// RemoveDeadNode - Remove the specified node from the system. If any of its
813 /// operands then becomes dead, remove them as well. Inform UpdateListener
814 /// for each node deleted.
815 void RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener = 0);
817 /// RemoveDeadNodes - This method deletes the unreachable nodes in the
818 /// given list, and any nodes that become unreachable as a result.
819 void RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
820 DAGUpdateListener *UpdateListener = 0);
822 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
823 /// This can cause recursive merging of nodes in the DAG. Use the first
824 /// version if 'From' is known to have a single result, use the second
825 /// if you have two nodes with identical results, use the third otherwise.
827 /// These methods all take an optional UpdateListener, which (if not null) is
828 /// informed about nodes that are deleted and modified due to recursive
829 /// changes in the dag.
831 /// These functions only replace all existing uses. It's possible that as
832 /// these replacements are being performed, CSE may cause the From node
833 /// to be given new uses. These new uses of From are left in place, and
834 /// not automatically transfered to To.
836 void ReplaceAllUsesWith(SDValue From, SDValue Op,
837 DAGUpdateListener *UpdateListener = 0);
838 void ReplaceAllUsesWith(SDNode *From, SDNode *To,
839 DAGUpdateListener *UpdateListener = 0);
840 void ReplaceAllUsesWith(SDNode *From, const SDValue *To,
841 DAGUpdateListener *UpdateListener = 0);
843 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
844 /// uses of other values produced by From.Val alone.
845 void ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
846 DAGUpdateListener *UpdateListener = 0);
848 /// ReplaceAllUsesOfValuesWith - Like ReplaceAllUsesOfValueWith, but
849 /// for multiple values at once. This correctly handles the case where
850 /// there is an overlap between the From values and the To values.
851 void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To,
853 DAGUpdateListener *UpdateListener = 0);
855 /// AssignTopologicalOrder - Topological-sort the AllNodes list and a
856 /// assign a unique node id for each node in the DAG based on their
857 /// topological order. Returns the number of nodes.
858 unsigned AssignTopologicalOrder();
860 /// RepositionNode - Move node N in the AllNodes list to be immediately
861 /// before the given iterator Position. This may be used to update the
862 /// topological ordering when the list of nodes is modified.
863 void RepositionNode(allnodes_iterator Position, SDNode *N) {
864 AllNodes.insert(Position, AllNodes.remove(N));
867 /// isCommutativeBinOp - Returns true if the opcode is a commutative binary
869 static bool isCommutativeBinOp(unsigned Opcode) {
870 // FIXME: This should get its info from the td file, so that we can include
885 case ISD::ADDE: return true;
886 default: return false;
892 /// CreateStackTemporary - Create a stack temporary, suitable for holding the
893 /// specified value type. If minAlign is specified, the slot size will have
894 /// at least that alignment.
895 SDValue CreateStackTemporary(MVT VT, unsigned minAlign = 1);
897 /// CreateStackTemporary - Create a stack temporary suitable for holding
898 /// either of the specified value types.
899 SDValue CreateStackTemporary(MVT VT1, MVT VT2);
901 /// FoldConstantArithmetic -
902 SDValue FoldConstantArithmetic(unsigned Opcode,
904 ConstantSDNode *Cst1,
905 ConstantSDNode *Cst2);
907 /// FoldSetCC - Constant fold a setcc to true or false.
908 SDValue FoldSetCC(MVT VT, SDValue N1,
909 SDValue N2, ISD::CondCode Cond);
911 /// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
912 /// use this predicate to simplify operations downstream.
913 bool SignBitIsZero(SDValue Op, unsigned Depth = 0) const;
915 /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We
916 /// use this predicate to simplify operations downstream. Op and Mask are
917 /// known to be the same type.
918 bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth = 0)
921 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
922 /// known to be either zero or one and return them in the KnownZero/KnownOne
923 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
924 /// processing. Targets can implement the computeMaskedBitsForTargetNode
925 /// method in the TargetLowering class to allow target nodes to be understood.
926 void ComputeMaskedBits(SDValue Op, const APInt &Mask, APInt &KnownZero,
927 APInt &KnownOne, unsigned Depth = 0) const;
929 /// ComputeNumSignBits - Return the number of times the sign bit of the
930 /// register is replicated into the other bits. We know that at least 1 bit
931 /// is always equal to the sign bit (itself), but other cases can give us
932 /// information. For example, immediately after an "SRA X, 2", we know that
933 /// the top 3 bits are all equal to each other, so we return 3. Targets can
934 /// implement the ComputeNumSignBitsForTarget method in the TargetLowering
935 /// class to allow target nodes to be understood.
936 unsigned ComputeNumSignBits(SDValue Op, unsigned Depth = 0) const;
938 /// isVerifiedDebugInfoDesc - Returns true if the specified SDValue has
939 /// been verified as a debug information descriptor.
940 bool isVerifiedDebugInfoDesc(SDValue Op) const;
942 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
943 /// element of the result of the vector shuffle.
944 SDValue getShuffleScalarElt(const SDNode *N, unsigned Idx);
947 bool RemoveNodeFromCSEMaps(SDNode *N);
948 void AddModifiedNodeToCSEMaps(SDNode *N, DAGUpdateListener *UpdateListener);
949 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op, void *&InsertPos);
950 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op1, SDValue Op2,
952 SDNode *FindModifiedNodeSlot(SDNode *N, const SDValue *Ops, unsigned NumOps,
955 void DeleteNodeNotInCSEMaps(SDNode *N);
956 void DeallocateNode(SDNode *N);
958 unsigned getMVTAlignment(MVT MemoryVT) const;
960 void allnodes_clear();
962 /// VTList - List of non-single value types.
963 std::vector<SDVTList> VTList;
965 /// CondCodeNodes - Maps to auto-CSE operations.
966 std::vector<CondCodeSDNode*> CondCodeNodes;
968 std::vector<SDNode*> ValueTypeNodes;
969 std::map<MVT, SDNode*, MVT::compareRawBits> ExtendedValueTypeNodes;
970 StringMap<SDNode*> ExternalSymbols;
971 StringMap<SDNode*> TargetExternalSymbols;
974 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
975 typedef SelectionDAG::allnodes_iterator nodes_iterator;
976 static nodes_iterator nodes_begin(SelectionDAG *G) {
977 return G->allnodes_begin();
979 static nodes_iterator nodes_end(SelectionDAG *G) {
980 return G->allnodes_end();
984 } // end namespace llvm