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 /// EntryNode - The starting token.
85 /// Root - The root of the entire DAG.
88 /// AllNodes - A linked list of nodes in the current DAG.
89 ilist<SDNode> AllNodes;
91 /// NodeAllocatorType - The AllocatorType for allocating SDNodes. We use
92 /// pool allocation with recycling.
93 typedef RecyclingAllocator<BumpPtrAllocator, SDNode, sizeof(LargestSDNode),
94 AlignOf<MostAlignedSDNode>::Alignment>
97 /// NodeAllocator - Pool allocation for nodes.
98 NodeAllocatorType NodeAllocator;
100 /// CSEMap - This structure is used to memoize nodes, automatically performing
101 /// CSE with existing nodes with a duplicate is requested.
102 FoldingSet<SDNode> CSEMap;
104 /// OperandAllocator - Pool allocation for machine-opcode SDNode operands.
105 BumpPtrAllocator OperandAllocator;
107 /// Allocator - Pool allocation for misc. objects that are created once per
109 BumpPtrAllocator Allocator;
111 /// VerifyNode - Sanity check the given node. Aborts if it is invalid.
112 void VerifyNode(SDNode *N);
114 /// setGraphColorHelper - Implementation of setSubgraphColor.
115 /// Return whether we had to truncate the search.
117 bool setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
118 int level, bool &printed);
121 SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli);
124 /// init - Prepare this SelectionDAG to process code in the given
127 void init(MachineFunction &mf, MachineModuleInfo *mmi, DwarfWriter *dw);
129 /// clear - Clear state and free memory necessary to make this
130 /// SelectionDAG ready to process a new block.
134 MachineFunction &getMachineFunction() const { return *MF; }
135 const TargetMachine &getTarget() const;
136 TargetLowering &getTargetLoweringInfo() const { return TLI; }
137 FunctionLoweringInfo &getFunctionLoweringInfo() const { return FLI; }
138 MachineModuleInfo *getMachineModuleInfo() const { return MMI; }
139 DwarfWriter *getDwarfWriter() const { return DW; }
141 /// viewGraph - Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
143 void viewGraph(const std::string &Title);
147 std::map<const SDNode *, std::string> NodeGraphAttrs;
150 /// clearGraphAttrs - Clear all previously defined node graph attributes.
151 /// Intended to be used from a debugging tool (eg. gdb).
152 void clearGraphAttrs();
154 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
156 void setGraphAttrs(const SDNode *N, const char *Attrs);
158 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
159 /// Used from getNodeAttributes.
160 const std::string getGraphAttrs(const SDNode *N) const;
162 /// setGraphColor - Convenience for setting node color attribute.
164 void setGraphColor(const SDNode *N, const char *Color);
166 /// setGraphColor - Convenience for setting subgraph color attribute.
168 void setSubgraphColor(SDNode *N, const char *Color);
170 typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
171 allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
172 allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
173 typedef ilist<SDNode>::iterator allnodes_iterator;
174 allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
175 allnodes_iterator allnodes_end() { return AllNodes.end(); }
176 ilist<SDNode>::size_type allnodes_size() const {
177 return AllNodes.size();
180 /// getRoot - Return the root tag of the SelectionDAG.
182 const SDValue &getRoot() const { return Root; }
184 /// getEntryNode - Return the token chain corresponding to the entry of the
186 SDValue getEntryNode() const {
187 return SDValue(const_cast<SDNode *>(&EntryNode), 0);
190 /// setRoot - Set the current root tag of the SelectionDAG.
192 const SDValue &setRoot(SDValue N) {
193 assert((!N.getNode() || N.getValueType() == MVT::Other) &&
194 "DAG root value is not a chain!");
198 /// Combine - This iterates over the nodes in the SelectionDAG, folding
199 /// certain types of nodes together, or eliminating superfluous nodes. The
200 /// Level argument controls whether Combine is allowed to produce nodes and
201 /// types that are illegal on the target.
202 void Combine(CombineLevel Level, AliasAnalysis &AA, bool Fast);
204 /// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that
205 /// only uses types natively supported by the target. Returns "true" if it
206 /// made any changes.
208 /// Note that this is an involved process that may invalidate pointers into
210 bool LegalizeTypes();
212 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
213 /// compatible with the target instruction selector, as indicated by the
214 /// TargetLowering object.
216 /// Note that this is an involved process that may invalidate pointers into
218 void Legalize(bool TypesNeedLegalizing);
220 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
222 void RemoveDeadNodes();
224 /// DeleteNode - Remove the specified node from the system. This node must
225 /// have no referrers.
226 void DeleteNode(SDNode *N);
228 /// getVTList - Return an SDVTList that represents the list of values
230 SDVTList getVTList(MVT VT);
231 SDVTList getVTList(MVT VT1, MVT VT2);
232 SDVTList getVTList(MVT VT1, MVT VT2, MVT VT3);
233 SDVTList getVTList(MVT VT1, MVT VT2, MVT VT3, MVT VT4);
234 SDVTList getVTList(const MVT *VTs, unsigned NumVTs);
236 /// getNodeValueTypes - These are obsolete, use getVTList instead.
237 const MVT *getNodeValueTypes(MVT VT) {
238 return getVTList(VT).VTs;
240 const MVT *getNodeValueTypes(MVT VT1, MVT VT2) {
241 return getVTList(VT1, VT2).VTs;
243 const MVT *getNodeValueTypes(MVT VT1, MVT VT2, MVT VT3) {
244 return getVTList(VT1, VT2, VT3).VTs;
246 const MVT *getNodeValueTypes(MVT VT1, MVT VT2, MVT VT3, MVT VT4) {
247 return getVTList(VT1, VT2, VT3, VT4).VTs;
249 const MVT *getNodeValueTypes(const std::vector<MVT> &vtList) {
250 return getVTList(&vtList[0], (unsigned)vtList.size()).VTs;
254 //===--------------------------------------------------------------------===//
255 // Node creation methods.
257 SDValue getConstant(uint64_t Val, MVT VT, bool isTarget = false);
258 SDValue getConstant(const APInt &Val, MVT VT, bool isTarget = false);
259 SDValue getConstant(const ConstantInt &Val, MVT VT, bool isTarget = false);
260 SDValue getIntPtrConstant(uint64_t Val, bool isTarget = false);
261 SDValue getTargetConstant(uint64_t Val, MVT VT) {
262 return getConstant(Val, VT, true);
264 SDValue getTargetConstant(const APInt &Val, MVT VT) {
265 return getConstant(Val, VT, true);
267 SDValue getTargetConstant(const ConstantInt &Val, MVT VT) {
268 return getConstant(Val, VT, true);
270 SDValue getConstantFP(double Val, MVT VT, bool isTarget = false);
271 SDValue getConstantFP(const APFloat& Val, MVT VT, bool isTarget = false);
272 SDValue getConstantFP(const ConstantFP &CF, MVT VT, bool isTarget = false);
273 SDValue getTargetConstantFP(double Val, MVT VT) {
274 return getConstantFP(Val, VT, true);
276 SDValue getTargetConstantFP(const APFloat& Val, MVT VT) {
277 return getConstantFP(Val, VT, true);
279 SDValue getTargetConstantFP(const ConstantFP &Val, MVT VT) {
280 return getConstantFP(Val, VT, true);
282 SDValue getGlobalAddress(const GlobalValue *GV, MVT VT,
283 int64_t offset = 0, bool isTargetGA = false);
284 SDValue getTargetGlobalAddress(const GlobalValue *GV, MVT VT,
285 int64_t offset = 0) {
286 return getGlobalAddress(GV, VT, offset, true);
288 SDValue getFrameIndex(int FI, MVT VT, bool isTarget = false);
289 SDValue getTargetFrameIndex(int FI, MVT VT) {
290 return getFrameIndex(FI, VT, true);
292 SDValue getJumpTable(int JTI, MVT VT, bool isTarget = false);
293 SDValue getTargetJumpTable(int JTI, MVT VT) {
294 return getJumpTable(JTI, VT, true);
296 SDValue getConstantPool(Constant *C, MVT VT,
297 unsigned Align = 0, int Offs = 0, bool isT=false);
298 SDValue getTargetConstantPool(Constant *C, MVT VT,
299 unsigned Align = 0, int Offset = 0) {
300 return getConstantPool(C, VT, Align, Offset, true);
302 SDValue getConstantPool(MachineConstantPoolValue *C, MVT VT,
303 unsigned Align = 0, int Offs = 0, bool isT=false);
304 SDValue getTargetConstantPool(MachineConstantPoolValue *C,
305 MVT VT, unsigned Align = 0,
307 return getConstantPool(C, VT, Align, Offset, true);
309 SDValue getBasicBlock(MachineBasicBlock *MBB);
310 SDValue getBasicBlock(MachineBasicBlock *MBB, DebugLoc dl);
311 SDValue getExternalSymbol(const char *Sym, MVT VT);
312 SDValue getExternalSymbol(const char *Sym, DebugLoc dl, MVT VT);
313 SDValue getTargetExternalSymbol(const char *Sym, MVT VT);
314 SDValue getTargetExternalSymbol(const char *Sym, DebugLoc dl, MVT VT);
315 SDValue getArgFlags(ISD::ArgFlagsTy Flags);
316 SDValue getValueType(MVT);
317 SDValue getRegister(unsigned Reg, MVT VT);
318 SDValue getDbgStopPoint(SDValue Root, unsigned Line, unsigned Col,
320 SDValue getLabel(unsigned Opcode, SDValue Root, unsigned LabelID);
321 SDValue getLabel(unsigned Opcode, DebugLoc dl, SDValue Root,
324 SDValue getCopyToReg(SDValue Chain, unsigned Reg, SDValue N) {
325 return getNode(ISD::CopyToReg, MVT::Other, Chain,
326 getRegister(Reg, N.getValueType()), N);
329 // This version of the getCopyToReg method takes an extra operand, which
330 // indicates that there is potentially an incoming flag value (if Flag is not
331 // null) and that there should be a flag result.
332 SDValue getCopyToReg(SDValue Chain, unsigned Reg, SDValue N,
334 const MVT *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
335 SDValue Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Flag };
336 return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.getNode() ? 4 : 3);
339 // Similar to last getCopyToReg() except parameter Reg is a SDValue
340 SDValue getCopyToReg(SDValue Chain, SDValue Reg, SDValue N,
342 const MVT *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
343 SDValue Ops[] = { Chain, Reg, N, Flag };
344 return getNode(ISD::CopyToReg, VTs, 2, Ops, Flag.getNode() ? 4 : 3);
347 SDValue getCopyFromReg(SDValue Chain, unsigned Reg, MVT VT) {
348 const MVT *VTs = getNodeValueTypes(VT, MVT::Other);
349 SDValue Ops[] = { Chain, getRegister(Reg, VT) };
350 return getNode(ISD::CopyFromReg, VTs, 2, Ops, 2);
353 // This version of the getCopyFromReg method takes an extra operand, which
354 // indicates that there is potentially an incoming flag value (if Flag is not
355 // null) and that there should be a flag result.
356 SDValue getCopyFromReg(SDValue Chain, unsigned Reg, MVT VT,
358 const MVT *VTs = getNodeValueTypes(VT, MVT::Other, MVT::Flag);
359 SDValue Ops[] = { Chain, getRegister(Reg, VT), Flag };
360 return getNode(ISD::CopyFromReg, VTs, 3, Ops, Flag.getNode() ? 3 : 2);
363 SDValue getCondCode(ISD::CondCode Cond);
365 /// Returns the ConvertRndSat Note: Avoid using this node because it may
366 /// disappear in the future and most targets don't support it.
367 SDValue getConvertRndSat(MVT VT, SDValue Val, SDValue DTy, SDValue STy,
368 SDValue Rnd, SDValue Sat, ISD::CvtCode Code);
370 /// getZeroExtendInReg - Return the expression required to zero extend the Op
371 /// value assuming it was the smaller SrcTy value.
372 SDValue getZeroExtendInReg(SDValue Op, MVT SrcTy);
373 SDValue getZeroExtendInReg(SDValue Op, DebugLoc DL, MVT SrcTy);
375 /// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
376 SDValue getNOT(DebugLoc DL, SDValue Val, MVT VT);
378 /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
379 /// a flag result (to ensure it's not CSE'd).
380 SDValue getCALLSEQ_START(SDValue Chain, SDValue Op) {
381 const MVT *VTs = getNodeValueTypes(MVT::Other, MVT::Flag);
382 SDValue Ops[] = { Chain, Op };
383 return getNode(ISD::CALLSEQ_START, VTs, 2, Ops, 2);
386 /// getCALLSEQ_END - Return a new CALLSEQ_END node, which always must have a
387 /// flag result (to ensure it's not CSE'd).
388 SDValue getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2,
390 SDVTList NodeTys = getVTList(MVT::Other, MVT::Flag);
391 SmallVector<SDValue, 4> Ops;
392 Ops.push_back(Chain);
395 Ops.push_back(InFlag);
396 return getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0],
397 (unsigned)Ops.size() - (InFlag.getNode() == 0 ? 1 : 0));
400 /// getNode - Gets or creates the specified node.
402 SDValue getNode(unsigned Opcode, MVT VT);
403 SDValue getNode(unsigned Opcode, DebugLoc DL, MVT VT);
404 SDValue getNode(unsigned Opcode, MVT VT, SDValue N);
405 SDValue getNode(unsigned Opcode, DebugLoc DL, MVT VT, SDValue N);
406 SDValue getNode(unsigned Opcode, MVT VT, SDValue N1, SDValue N2);
407 SDValue getNode(unsigned Opcode, DebugLoc DL, MVT VT, SDValue N1, SDValue N2);
408 SDValue getNode(unsigned Opcode, MVT VT,
409 SDValue N1, SDValue N2, SDValue N3);
410 SDValue getNode(unsigned Opcode, DebugLoc DL, MVT VT,
411 SDValue N1, SDValue N2, SDValue N3);
412 SDValue getNode(unsigned Opcode, MVT VT,
413 SDValue N1, SDValue N2, SDValue N3, SDValue N4);
414 SDValue getNode(unsigned Opcode, DebugLoc DL, MVT VT,
415 SDValue N1, SDValue N2, SDValue N3, SDValue N4);
416 SDValue getNode(unsigned Opcode, MVT VT,
417 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
419 SDValue getNode(unsigned Opcode, DebugLoc DL, MVT VT,
420 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
422 SDValue getNode(unsigned Opcode, MVT VT,
423 const SDUse *Ops, unsigned NumOps);
424 SDValue getNode(unsigned Opcode, DebugLoc DL, MVT VT,
425 const SDUse *Ops, unsigned NumOps);
426 SDValue getNode(unsigned Opcode, MVT VT,
427 const SDValue *Ops, unsigned NumOps);
428 SDValue getNode(unsigned Opcode, DebugLoc DL, MVT VT,
429 const SDValue *Ops, unsigned NumOps);
430 SDValue getNode(unsigned Opcode, const std::vector<MVT> &ResultTys,
431 const SDValue *Ops, unsigned NumOps);
432 SDValue getNode(unsigned Opcode, DebugLoc DL,
433 const std::vector<MVT> &ResultTys,
434 const SDValue *Ops, unsigned NumOps);
435 SDValue getNode(unsigned Opcode, const MVT *VTs, unsigned NumVTs,
436 const SDValue *Ops, unsigned NumOps);
437 SDValue getNode(unsigned Opcode, DebugLoc DL, const MVT *VTs, unsigned NumVTs,
438 const SDValue *Ops, unsigned NumOps);
439 SDValue getNode(unsigned Opcode, SDVTList VTs,
440 const SDValue *Ops, unsigned NumOps);
441 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
442 const SDValue *Ops, unsigned NumOps);
444 SDValue getNode(unsigned Opcode, SDVTList VTs);
445 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs);
446 SDValue getNode(unsigned Opcode, SDVTList VTs, SDValue N);
447 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs, SDValue N);
448 SDValue getNode(unsigned Opcode, SDVTList VTs, SDValue N1, SDValue N2);
449 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
450 SDValue N1, SDValue N2);
451 SDValue getNode(unsigned Opcode, SDVTList VTs,
452 SDValue N1, SDValue N2, SDValue N3);
453 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
454 SDValue N1, SDValue N2, SDValue N3);
455 SDValue getNode(unsigned Opcode, SDVTList VTs,
456 SDValue N1, SDValue N2, SDValue N3, SDValue N4);
457 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
458 SDValue N1, SDValue N2, SDValue N3, SDValue N4);
459 SDValue getNode(unsigned Opcode, SDVTList VTs,
460 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
462 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
463 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
466 SDValue getMemcpy(SDValue Chain, SDValue Dst, SDValue Src,
467 SDValue Size, unsigned Align, bool AlwaysInline,
468 const Value *DstSV, uint64_t DstSVOff,
469 const Value *SrcSV, uint64_t SrcSVOff);
471 SDValue getMemmove(SDValue Chain, SDValue Dst, SDValue Src,
472 SDValue Size, unsigned Align,
473 const Value *DstSV, uint64_t DstOSVff,
474 const Value *SrcSV, uint64_t SrcSVOff);
476 SDValue getMemset(SDValue Chain, SDValue Dst, SDValue Src,
477 SDValue Size, unsigned Align,
478 const Value *DstSV, uint64_t DstSVOff);
480 /// getSetCC - Helper function to make it easier to build SetCC's if you just
481 /// have an ISD::CondCode instead of an SDValue.
483 SDValue getSetCC(MVT VT, SDValue LHS, SDValue RHS,
484 ISD::CondCode Cond) {
485 return getNode(ISD::SETCC, DebugLoc::getUnknownLoc(), VT,
486 LHS, RHS, getCondCode(Cond));
488 SDValue getSetCC(DebugLoc DL, MVT VT, SDValue LHS, SDValue RHS,
489 ISD::CondCode Cond) {
490 return getNode(ISD::SETCC, DL, VT, LHS, RHS, getCondCode(Cond));
493 /// getVSetCC - Helper function to make it easier to build VSetCC's nodes
494 /// if you just have an ISD::CondCode instead of an SDValue.
496 SDValue getVSetCC(MVT VT, SDValue LHS, SDValue RHS,
497 ISD::CondCode Cond) {
498 return getNode(ISD::VSETCC, DebugLoc::getUnknownLoc(), VT,
499 LHS, RHS, getCondCode(Cond));
501 SDValue getVSetCC(DebugLoc DL, MVT VT, SDValue LHS, SDValue RHS,
502 ISD::CondCode Cond) {
503 return getNode(ISD::VSETCC, DL, VT, LHS, RHS, getCondCode(Cond));
506 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
507 /// just have an ISD::CondCode instead of an SDValue.
509 SDValue getSelectCC(SDValue LHS, SDValue RHS,
510 SDValue True, SDValue False, ISD::CondCode Cond) {
511 return getNode(ISD::SELECT_CC, DebugLoc::getUnknownLoc(), True.getValueType(),
512 LHS, RHS, True, False, getCondCode(Cond));
514 SDValue getSelectCC(DebugLoc DL, SDValue LHS, SDValue RHS,
515 SDValue True, SDValue False, ISD::CondCode Cond) {
516 return getNode(ISD::SELECT_CC, DL, True.getValueType(),
517 LHS, RHS, True, False, getCondCode(Cond));
520 /// getVAArg - VAArg produces a result and token chain, and takes a pointer
521 /// and a source value as input.
522 SDValue getVAArg(MVT VT, SDValue Chain, SDValue Ptr,
525 /// getAtomic - Gets a node for an atomic op, produces result and chain and
527 SDValue getAtomic(unsigned Opcode, MVT MemVT, SDValue Chain, SDValue Ptr,
528 SDValue Cmp, SDValue Swp, const Value* PtrVal,
529 unsigned Alignment=0);
530 SDValue getAtomic(unsigned Opcode, DebugLoc dl, MVT MemVT, SDValue Chain,
531 SDValue Ptr, SDValue Cmp, SDValue Swp, const Value* PtrVal,
532 unsigned Alignment=0);
534 /// getAtomic - Gets a node for an atomic op, produces result and chain and
535 /// takes 2 operands.
536 SDValue getAtomic(unsigned Opcode, MVT MemVT, SDValue Chain, SDValue Ptr,
537 SDValue Val, const Value* PtrVal,
538 unsigned Alignment = 0);
539 SDValue getAtomic(unsigned Opcode, DebugLoc dl, MVT MemVT, SDValue Chain,
540 SDValue Ptr, SDValue Val, const Value* PtrVal,
541 unsigned Alignment = 0);
543 /// getMemIntrinsicNode - Creates a MemIntrinsicNode that may produce a
544 /// result and takes a list of operands.
545 SDValue getMemIntrinsicNode(unsigned Opcode,
546 const MVT *VTs, unsigned NumVTs,
547 const SDValue *Ops, unsigned NumOps,
548 MVT MemVT, const Value *srcValue, int SVOff,
549 unsigned Align = 0, bool Vol = false,
550 bool ReadMem = true, bool WriteMem = true);
551 SDValue getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
552 const MVT *VTs, unsigned NumVTs,
553 const SDValue *Ops, unsigned NumOps,
554 MVT MemVT, const Value *srcValue, int SVOff,
555 unsigned Align = 0, bool Vol = false,
556 bool ReadMem = true, bool WriteMem = true);
558 SDValue getMemIntrinsicNode(unsigned Opcode, SDVTList VTList,
559 const SDValue *Ops, unsigned NumOps,
560 MVT MemVT, const Value *srcValue, int SVOff,
561 unsigned Align = 0, bool Vol = false,
562 bool ReadMem = true, bool WriteMem = true);
563 SDValue getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
564 const SDValue *Ops, unsigned NumOps,
565 MVT MemVT, const Value *srcValue, int SVOff,
566 unsigned Align = 0, bool Vol = false,
567 bool ReadMem = true, bool WriteMem = true);
569 /// getMergeValues - Create a MERGE_VALUES node from the given operands.
570 SDValue getMergeValues(const SDValue *Ops, unsigned NumOps);
571 SDValue getMergeValues(const SDValue *Ops, unsigned NumOps, DebugLoc dl);
573 /// getCall - Create a CALL node from the given information.
575 SDValue getCall(unsigned CallingConv, bool IsVarArgs, bool IsTailCall,
576 bool isInreg, SDVTList VTs, const SDValue *Operands,
577 unsigned NumOperands);
578 SDValue getCall(unsigned CallingConv, DebugLoc dl, bool IsVarArgs,
579 bool IsTailCall, bool isInreg, SDVTList VTs,
580 const SDValue *Operands, unsigned NumOperands);
582 /// getLoad - Loads are not normal binary operators: their result type is not
583 /// determined by their operands, and they produce a value AND a token chain.
585 SDValue getLoad(MVT VT, SDValue Chain, SDValue Ptr,
586 const Value *SV, int SVOffset, bool isVolatile=false,
587 unsigned Alignment=0);
588 SDValue getLoad(MVT VT, DebugLoc dl, SDValue Chain, SDValue Ptr,
589 const Value *SV, int SVOffset, bool isVolatile=false,
590 unsigned Alignment=0);
591 SDValue getExtLoad(ISD::LoadExtType ExtType, MVT VT,
592 SDValue Chain, SDValue Ptr, const Value *SV,
593 int SVOffset, MVT EVT, bool isVolatile=false,
594 unsigned Alignment=0);
595 SDValue getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, MVT VT,
596 SDValue Chain, SDValue Ptr, const Value *SV,
597 int SVOffset, MVT EVT, bool isVolatile=false,
598 unsigned Alignment=0);
599 SDValue getIndexedLoad(SDValue OrigLoad, SDValue Base,
600 SDValue Offset, ISD::MemIndexedMode AM);
601 SDValue getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
602 SDValue Offset, ISD::MemIndexedMode AM);
603 SDValue getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
604 MVT VT, SDValue Chain,
605 SDValue Ptr, SDValue Offset,
606 const Value *SV, int SVOffset, MVT EVT,
607 bool isVolatile=false, unsigned Alignment=0);
608 SDValue getLoad(ISD::MemIndexedMode AM, DebugLoc dl, ISD::LoadExtType ExtType,
609 MVT VT, SDValue Chain,
610 SDValue Ptr, SDValue Offset,
611 const Value *SV, int SVOffset, MVT EVT,
612 bool isVolatile=false, unsigned Alignment=0);
614 /// getStore - Helper function to build ISD::STORE nodes.
616 SDValue getStore(SDValue Chain, SDValue Val, SDValue Ptr,
617 const Value *SV, int SVOffset, bool isVolatile=false,
618 unsigned Alignment=0);
619 SDValue getStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
620 const Value *SV, int SVOffset, bool isVolatile=false,
621 unsigned Alignment=0);
622 SDValue getTruncStore(SDValue Chain, SDValue Val, SDValue Ptr,
623 const Value *SV, int SVOffset, MVT TVT,
624 bool isVolatile=false, unsigned Alignment=0);
625 SDValue getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
626 const Value *SV, int SVOffset, MVT TVT,
627 bool isVolatile=false, unsigned Alignment=0);
628 SDValue getIndexedStore(SDValue OrigStoe, SDValue Base,
629 SDValue Offset, ISD::MemIndexedMode AM);
630 SDValue getIndexedStore(SDValue OrigStoe, DebugLoc dl, SDValue Base,
631 SDValue Offset, ISD::MemIndexedMode AM);
633 /// getSrcValue - Construct a node to track a Value* through the backend.
634 SDValue getSrcValue(const Value *v);
636 /// getMemOperand - Construct a node to track a memory reference
637 /// through the backend.
638 SDValue getMemOperand(const MachineMemOperand &MO);
640 /// getShiftAmountOperand - Return the specified value casted to
641 /// the target's desired shift amount type.
642 SDValue getShiftAmountOperand(SDValue Op);
644 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
645 /// specified operands. If the resultant node already exists in the DAG,
646 /// this does not modify the specified node, instead it returns the node that
647 /// already exists. If the resultant node does not exist in the DAG, the
648 /// input node is returned. As a degenerate case, if you specify the same
649 /// input operands as the node already has, the input node is returned.
650 SDValue UpdateNodeOperands(SDValue N, SDValue Op);
651 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2);
652 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
654 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
655 SDValue Op3, SDValue Op4);
656 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
657 SDValue Op3, SDValue Op4, SDValue Op5);
658 SDValue UpdateNodeOperands(SDValue N,
659 const SDValue *Ops, unsigned NumOps);
661 /// SelectNodeTo - These are used for target selectors to *mutate* the
662 /// specified node to have the specified return type, Target opcode, and
663 /// operands. Note that target opcodes are stored as
664 /// ~TargetOpcode in the node opcode field. The resultant node is returned.
665 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT);
666 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT, SDValue Op1);
667 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT,
668 SDValue Op1, SDValue Op2);
669 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT,
670 SDValue Op1, SDValue Op2, SDValue Op3);
671 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT,
672 const SDValue *Ops, unsigned NumOps);
673 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1, MVT VT2);
674 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
675 MVT VT2, const SDValue *Ops, unsigned NumOps);
676 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
677 MVT VT2, MVT VT3, const SDValue *Ops, unsigned NumOps);
678 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, MVT VT1,
679 MVT VT2, MVT VT3, MVT VT4, const SDValue *Ops,
681 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
682 MVT VT2, SDValue Op1);
683 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
684 MVT VT2, SDValue Op1, SDValue Op2);
685 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
686 MVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
687 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, MVT VT1,
688 MVT VT2, MVT VT3, SDValue Op1, SDValue Op2, SDValue Op3);
689 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, SDVTList VTs,
690 const SDValue *Ops, unsigned NumOps);
692 /// MorphNodeTo - These *mutate* the specified node to have the specified
693 /// return type, opcode, and operands.
694 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT);
695 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT, SDValue Op1);
696 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT,
697 SDValue Op1, SDValue Op2);
698 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT,
699 SDValue Op1, SDValue Op2, SDValue Op3);
700 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT,
701 const SDValue *Ops, unsigned NumOps);
702 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1, MVT VT2);
703 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1,
704 MVT VT2, const SDValue *Ops, unsigned NumOps);
705 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1,
706 MVT VT2, MVT VT3, const SDValue *Ops, unsigned NumOps);
707 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1,
708 MVT VT2, SDValue Op1);
709 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1,
710 MVT VT2, SDValue Op1, SDValue Op2);
711 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, MVT VT1,
712 MVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
713 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs,
714 const SDValue *Ops, unsigned NumOps);
716 /// getTargetNode - These are used for target selectors to create a new node
717 /// with specified return type(s), target opcode, and operands.
719 /// Note that getTargetNode returns the resultant node. If there is already a
720 /// node of the specified opcode and operands, it returns that node instead of
722 SDNode *getTargetNode(unsigned Opcode, MVT VT);
723 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT);
725 SDNode *getTargetNode(unsigned Opcode, MVT VT, SDValue Op1);
726 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT, SDValue Op1);
728 SDNode *getTargetNode(unsigned Opcode, MVT VT, SDValue Op1, SDValue Op2);
729 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT, SDValue Op1,
732 SDNode *getTargetNode(unsigned Opcode, MVT VT,
733 SDValue Op1, SDValue Op2, SDValue Op3);
734 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
735 SDValue Op1, SDValue Op2, SDValue Op3);
737 SDNode *getTargetNode(unsigned Opcode, MVT VT,
738 const SDValue *Ops, unsigned NumOps);
739 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
740 const SDValue *Ops, unsigned NumOps);
742 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2);
743 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1, MVT VT2);
745 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, SDValue Op1);
746 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1, MVT VT2,
749 SDNode *getTargetNode(unsigned Opcode, MVT VT1,
750 MVT VT2, SDValue Op1, SDValue Op2);
751 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
752 MVT VT2, SDValue Op1, SDValue Op2);
754 SDNode *getTargetNode(unsigned Opcode, MVT VT1,
755 MVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
756 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
757 MVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
759 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
760 const SDValue *Ops, unsigned NumOps);
761 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1, MVT VT2,
762 const SDValue *Ops, unsigned NumOps);
764 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
765 SDValue Op1, SDValue Op2);
766 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1, MVT VT2, MVT VT3,
767 SDValue Op1, SDValue Op2);
769 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
770 SDValue Op1, SDValue Op2, SDValue Op3);
771 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1, MVT VT2, MVT VT3,
772 SDValue Op1, SDValue Op2, SDValue Op3);
774 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
775 const SDValue *Ops, unsigned NumOps);
776 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1, MVT VT2, MVT VT3,
777 const SDValue *Ops, unsigned NumOps);
779 SDNode *getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3, MVT VT4,
780 const SDValue *Ops, unsigned NumOps);
781 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1, MVT VT2, MVT VT3,
782 MVT VT4, const SDValue *Ops, unsigned NumOps);
784 SDNode *getTargetNode(unsigned Opcode, const std::vector<MVT> &ResultTys,
785 const SDValue *Ops, unsigned NumOps);
786 SDNode *getTargetNode(unsigned Opcode, DebugLoc dl,
787 const std::vector<MVT> &ResultTys, const SDValue *Ops,
790 /// getNodeIfExists - Get the specified node if it's already available, or
791 /// else return NULL.
792 SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTs,
793 const SDValue *Ops, unsigned NumOps);
795 /// DAGUpdateListener - Clients of various APIs that cause global effects on
796 /// the DAG can optionally implement this interface. This allows the clients
797 /// to handle the various sorts of updates that happen.
798 class DAGUpdateListener {
800 virtual ~DAGUpdateListener();
802 /// NodeDeleted - The node N that was deleted and, if E is not null, an
803 /// equivalent node E that replaced it.
804 virtual void NodeDeleted(SDNode *N, SDNode *E) = 0;
806 /// NodeUpdated - The node N that was updated.
807 virtual void NodeUpdated(SDNode *N) = 0;
810 /// RemoveDeadNode - Remove the specified node from the system. If any of its
811 /// operands then becomes dead, remove them as well. Inform UpdateListener
812 /// for each node deleted.
813 void RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener = 0);
815 /// RemoveDeadNodes - This method deletes the unreachable nodes in the
816 /// given list, and any nodes that become unreachable as a result.
817 void RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
818 DAGUpdateListener *UpdateListener = 0);
820 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
821 /// This can cause recursive merging of nodes in the DAG. Use the first
822 /// version if 'From' is known to have a single result, use the second
823 /// if you have two nodes with identical results, use the third otherwise.
825 /// These methods all take an optional UpdateListener, which (if not null) is
826 /// informed about nodes that are deleted and modified due to recursive
827 /// changes in the dag.
829 /// These functions only replace all existing uses. It's possible that as
830 /// these replacements are being performed, CSE may cause the From node
831 /// to be given new uses. These new uses of From are left in place, and
832 /// not automatically transfered to To.
834 void ReplaceAllUsesWith(SDValue From, SDValue Op,
835 DAGUpdateListener *UpdateListener = 0);
836 void ReplaceAllUsesWith(SDNode *From, SDNode *To,
837 DAGUpdateListener *UpdateListener = 0);
838 void ReplaceAllUsesWith(SDNode *From, const SDValue *To,
839 DAGUpdateListener *UpdateListener = 0);
841 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
842 /// uses of other values produced by From.Val alone.
843 void ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
844 DAGUpdateListener *UpdateListener = 0);
846 /// ReplaceAllUsesOfValuesWith - Like ReplaceAllUsesOfValueWith, but
847 /// for multiple values at once. This correctly handles the case where
848 /// there is an overlap between the From values and the To values.
849 void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To,
851 DAGUpdateListener *UpdateListener = 0);
853 /// AssignTopologicalOrder - Topological-sort the AllNodes list and a
854 /// assign a unique node id for each node in the DAG based on their
855 /// topological order. Returns the number of nodes.
856 unsigned AssignTopologicalOrder();
858 /// RepositionNode - Move node N in the AllNodes list to be immediately
859 /// before the given iterator Position. This may be used to update the
860 /// topological ordering when the list of nodes is modified.
861 void RepositionNode(allnodes_iterator Position, SDNode *N) {
862 AllNodes.insert(Position, AllNodes.remove(N));
865 /// isCommutativeBinOp - Returns true if the opcode is a commutative binary
867 static bool isCommutativeBinOp(unsigned Opcode) {
868 // FIXME: This should get its info from the td file, so that we can include
883 case ISD::ADDE: return true;
884 default: return false;
890 /// CreateStackTemporary - Create a stack temporary, suitable for holding the
891 /// specified value type. If minAlign is specified, the slot size will have
892 /// at least that alignment.
893 SDValue CreateStackTemporary(MVT VT, unsigned minAlign = 1);
895 /// CreateStackTemporary - Create a stack temporary suitable for holding
896 /// either of the specified value types.
897 SDValue CreateStackTemporary(MVT VT1, MVT VT2);
899 /// FoldConstantArithmetic -
900 SDValue FoldConstantArithmetic(unsigned Opcode,
902 ConstantSDNode *Cst1,
903 ConstantSDNode *Cst2);
905 /// FoldSetCC - Constant fold a setcc to true or false.
906 SDValue FoldSetCC(MVT VT, SDValue N1,
907 SDValue N2, ISD::CondCode Cond, DebugLoc dl);
909 /// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
910 /// use this predicate to simplify operations downstream.
911 bool SignBitIsZero(SDValue Op, unsigned Depth = 0) const;
913 /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We
914 /// use this predicate to simplify operations downstream. Op and Mask are
915 /// known to be the same type.
916 bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth = 0)
919 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
920 /// known to be either zero or one and return them in the KnownZero/KnownOne
921 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
922 /// processing. Targets can implement the computeMaskedBitsForTargetNode
923 /// method in the TargetLowering class to allow target nodes to be understood.
924 void ComputeMaskedBits(SDValue Op, const APInt &Mask, APInt &KnownZero,
925 APInt &KnownOne, unsigned Depth = 0) const;
927 /// ComputeNumSignBits - Return the number of times the sign bit of the
928 /// register is replicated into the other bits. We know that at least 1 bit
929 /// is always equal to the sign bit (itself), but other cases can give us
930 /// information. For example, immediately after an "SRA X, 2", we know that
931 /// the top 3 bits are all equal to each other, so we return 3. Targets can
932 /// implement the ComputeNumSignBitsForTarget method in the TargetLowering
933 /// class to allow target nodes to be understood.
934 unsigned ComputeNumSignBits(SDValue Op, unsigned Depth = 0) const;
936 /// isVerifiedDebugInfoDesc - Returns true if the specified SDValue has
937 /// been verified as a debug information descriptor.
938 bool isVerifiedDebugInfoDesc(SDValue Op) const;
940 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
941 /// element of the result of the vector shuffle.
942 SDValue getShuffleScalarElt(const SDNode *N, unsigned Idx);
945 bool RemoveNodeFromCSEMaps(SDNode *N);
946 void AddModifiedNodeToCSEMaps(SDNode *N, DAGUpdateListener *UpdateListener);
947 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op, void *&InsertPos);
948 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op1, SDValue Op2,
950 SDNode *FindModifiedNodeSlot(SDNode *N, const SDValue *Ops, unsigned NumOps,
953 void DeleteNodeNotInCSEMaps(SDNode *N);
954 void DeallocateNode(SDNode *N);
956 unsigned getMVTAlignment(MVT MemoryVT) const;
958 void allnodes_clear();
960 /// VTList - List of non-single value types.
961 std::vector<SDVTList> VTList;
963 /// CondCodeNodes - Maps to auto-CSE operations.
964 std::vector<CondCodeSDNode*> CondCodeNodes;
966 std::vector<SDNode*> ValueTypeNodes;
967 std::map<MVT, SDNode*, MVT::compareRawBits> ExtendedValueTypeNodes;
968 StringMap<SDNode*> ExternalSymbols;
969 StringMap<SDNode*> TargetExternalSymbols;
972 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
973 typedef SelectionDAG::allnodes_iterator nodes_iterator;
974 static nodes_iterator nodes_begin(SelectionDAG *G) {
975 return G->allnodes_begin();
977 static nodes_iterator nodes_end(SelectionDAG *G) {
978 return G->allnodes_end();
982 } // end namespace llvm