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/StringMap.h"
21 #include "llvm/CodeGen/SelectionDAGNodes.h"
22 #include "llvm/Support/RecyclingAllocator.h"
23 #include "llvm/Target/TargetMachine.h"
33 class FunctionLoweringInfo;
34 class MachineConstantPoolValue;
35 class MachineFunction;
36 class MachineModuleInfo;
41 template<> struct ilist_traits<SDNode> : public ilist_default_traits<SDNode> {
43 mutable ilist_half_node<SDNode> Sentinel;
45 SDNode *createSentinel() const {
46 return static_cast<SDNode*>(&Sentinel);
48 static void destroySentinel(SDNode *) {}
50 SDNode *provideInitialHead() const { return createSentinel(); }
51 SDNode *ensureHead(SDNode*) const { return createSentinel(); }
52 static void noteHead(SDNode*, SDNode*) {}
54 static void deleteNode(SDNode *) {
55 assert(0 && "ilist_traits<SDNode> shouldn't see a deleteNode call!");
58 static void createNode(const SDNode &);
61 /// SDDbgInfo - Keeps track of dbg_value information through SDISel. We do
62 /// not build SDNodes for these so as not to perturb the generated code;
63 /// instead the info is kept off to the side in this structure. Each SDNode may
64 /// have one or more associated dbg_value entries. This information is kept in
67 SmallVector<SDDbgValue*, 32> DbgValues;
68 DenseMap<const SDNode*, SmallVector<SDDbgValue*, 2> > DbgValMap;
70 void operator=(const SDDbgInfo&); // Do not implement.
71 SDDbgInfo(const SDDbgInfo&); // Do not implement.
75 void add(SDDbgValue *V, const SDNode *Node = 0) {
77 DbgValMap[Node].push_back(V);
78 DbgValues.push_back(V);
87 return DbgValues.empty();
90 SmallVector<SDDbgValue*,2> &getSDDbgValues(const SDNode *Node) {
91 return DbgValMap[Node];
94 typedef SmallVector<SDDbgValue*,32>::iterator DbgIterator;
95 DbgIterator DbgBegin() { return DbgValues.begin(); }
96 DbgIterator DbgEnd() { return DbgValues.end(); }
100 Unrestricted, // Combine may create illegal operations and illegal types.
101 NoIllegalTypes, // Combine may create illegal operations but no illegal types.
102 NoIllegalOperations // Combine may only create legal operations and types.
106 void checkForCycles(const SDNode *N);
107 void checkForCycles(const SelectionDAG *DAG);
109 /// SelectionDAG class - This is used to represent a portion of an LLVM function
110 /// in a low-level Data Dependence DAG representation suitable for instruction
111 /// selection. This DAG is constructed as the first step of instruction
112 /// selection in order to allow implementation of machine specific optimizations
113 /// and code simplifications.
115 /// The representation used by the SelectionDAG is a target-independent
116 /// representation, which has some similarities to the GCC RTL representation,
117 /// but is significantly more simple, powerful, and is a graph form instead of a
123 FunctionLoweringInfo &FLI;
124 MachineModuleInfo *MMI;
126 LLVMContext* Context;
128 /// EntryNode - The starting token.
131 /// Root - The root of the entire DAG.
134 /// AllNodes - A linked list of nodes in the current DAG.
135 ilist<SDNode> AllNodes;
137 /// NodeAllocatorType - The AllocatorType for allocating SDNodes. We use
138 /// pool allocation with recycling.
139 typedef RecyclingAllocator<BumpPtrAllocator, SDNode, sizeof(LargestSDNode),
140 AlignOf<MostAlignedSDNode>::Alignment>
143 /// NodeAllocator - Pool allocation for nodes.
144 NodeAllocatorType NodeAllocator;
146 /// CSEMap - This structure is used to memoize nodes, automatically performing
147 /// CSE with existing nodes when a duplicate is requested.
148 FoldingSet<SDNode> CSEMap;
150 /// OperandAllocator - Pool allocation for machine-opcode SDNode operands.
151 BumpPtrAllocator OperandAllocator;
153 /// Allocator - Pool allocation for misc. objects that are created once per
155 BumpPtrAllocator Allocator;
157 /// SDNodeOrdering - The ordering of the SDNodes. It roughly corresponds to
158 /// the ordering of the original LLVM instructions.
159 SDNodeOrdering *Ordering;
161 /// DbgInfo - Tracks dbg_value information through SDISel.
164 /// VerifyNode - Sanity check the given node. Aborts if it is invalid.
165 void VerifyNode(SDNode *N);
167 /// setGraphColorHelper - Implementation of setSubgraphColor.
168 /// Return whether we had to truncate the search.
170 bool setSubgraphColorHelper(SDNode *N, const char *Color,
171 DenseSet<SDNode *> &visited,
172 int level, bool &printed);
174 void operator=(const SelectionDAG&); // Do not implement.
175 SelectionDAG(const SelectionDAG&); // Do not implement.
178 SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli);
181 /// init - Prepare this SelectionDAG to process code in the given
184 void init(MachineFunction &mf, MachineModuleInfo *mmi, DwarfWriter *dw);
186 /// clear - Clear state and free memory necessary to make this
187 /// SelectionDAG ready to process a new block.
191 MachineFunction &getMachineFunction() const { return *MF; }
192 const TargetMachine &getTarget() const;
193 TargetLowering &getTargetLoweringInfo() const { return TLI; }
194 FunctionLoweringInfo &getFunctionLoweringInfo() const { return FLI; }
195 MachineModuleInfo *getMachineModuleInfo() const { return MMI; }
196 DwarfWriter *getDwarfWriter() const { return DW; }
197 LLVMContext *getContext() const {return Context; }
199 /// viewGraph - Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
201 void viewGraph(const std::string &Title);
205 std::map<const SDNode *, std::string> NodeGraphAttrs;
208 /// clearGraphAttrs - Clear all previously defined node graph attributes.
209 /// Intended to be used from a debugging tool (eg. gdb).
210 void clearGraphAttrs();
212 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
214 void setGraphAttrs(const SDNode *N, const char *Attrs);
216 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
217 /// Used from getNodeAttributes.
218 const std::string getGraphAttrs(const SDNode *N) const;
220 /// setGraphColor - Convenience for setting node color attribute.
222 void setGraphColor(const SDNode *N, const char *Color);
224 /// setGraphColor - Convenience for setting subgraph color attribute.
226 void setSubgraphColor(SDNode *N, const char *Color);
228 typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
229 allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
230 allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
231 typedef ilist<SDNode>::iterator allnodes_iterator;
232 allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
233 allnodes_iterator allnodes_end() { return AllNodes.end(); }
234 ilist<SDNode>::size_type allnodes_size() const {
235 return AllNodes.size();
238 /// getRoot - Return the root tag of the SelectionDAG.
240 const SDValue &getRoot() const { return Root; }
242 /// getEntryNode - Return the token chain corresponding to the entry of the
244 SDValue getEntryNode() const {
245 return SDValue(const_cast<SDNode *>(&EntryNode), 0);
248 /// setRoot - Set the current root tag of the SelectionDAG.
250 const SDValue &setRoot(SDValue N) {
251 assert((!N.getNode() || N.getValueType() == MVT::Other) &&
252 "DAG root value is not a chain!");
254 checkForCycles(N.getNode());
257 checkForCycles(this);
261 /// Combine - This iterates over the nodes in the SelectionDAG, folding
262 /// certain types of nodes together, or eliminating superfluous nodes. The
263 /// Level argument controls whether Combine is allowed to produce nodes and
264 /// types that are illegal on the target.
265 void Combine(CombineLevel Level, AliasAnalysis &AA,
266 CodeGenOpt::Level OptLevel);
268 /// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that
269 /// only uses types natively supported by the target. Returns "true" if it
270 /// made any changes.
272 /// Note that this is an involved process that may invalidate pointers into
274 bool LegalizeTypes();
276 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
277 /// compatible with the target instruction selector, as indicated by the
278 /// TargetLowering object.
280 /// Note that this is an involved process that may invalidate pointers into
282 void Legalize(CodeGenOpt::Level OptLevel);
284 /// LegalizeVectors - This transforms the SelectionDAG into a SelectionDAG
285 /// that only uses vector math operations supported by the target. This is
286 /// necessary as a separate step from Legalize because unrolling a vector
287 /// operation can introduce illegal types, which requires running
288 /// LegalizeTypes again.
290 /// This returns true if it made any changes; in that case, LegalizeTypes
291 /// is called again before Legalize.
293 /// Note that this is an involved process that may invalidate pointers into
295 bool LegalizeVectors();
297 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
299 void RemoveDeadNodes();
301 /// DeleteNode - Remove the specified node from the system. This node must
302 /// have no referrers.
303 void DeleteNode(SDNode *N);
305 /// getVTList - Return an SDVTList that represents the list of values
307 SDVTList getVTList(EVT VT);
308 SDVTList getVTList(EVT VT1, EVT VT2);
309 SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3);
310 SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4);
311 SDVTList getVTList(const EVT *VTs, unsigned NumVTs);
313 //===--------------------------------------------------------------------===//
314 // Node creation methods.
316 SDValue getConstant(uint64_t Val, EVT VT, bool isTarget = false);
317 SDValue getConstant(const APInt &Val, EVT VT, bool isTarget = false);
318 SDValue getConstant(const ConstantInt &Val, EVT VT, bool isTarget = false);
319 SDValue getIntPtrConstant(uint64_t Val, bool isTarget = false);
320 SDValue getTargetConstant(uint64_t Val, EVT VT) {
321 return getConstant(Val, VT, true);
323 SDValue getTargetConstant(const APInt &Val, EVT VT) {
324 return getConstant(Val, VT, true);
326 SDValue getTargetConstant(const ConstantInt &Val, EVT VT) {
327 return getConstant(Val, VT, true);
329 SDValue getConstantFP(double Val, EVT VT, bool isTarget = false);
330 SDValue getConstantFP(const APFloat& Val, EVT VT, bool isTarget = false);
331 SDValue getConstantFP(const ConstantFP &CF, EVT VT, bool isTarget = false);
332 SDValue getTargetConstantFP(double Val, EVT VT) {
333 return getConstantFP(Val, VT, true);
335 SDValue getTargetConstantFP(const APFloat& Val, EVT VT) {
336 return getConstantFP(Val, VT, true);
338 SDValue getTargetConstantFP(const ConstantFP &Val, EVT VT) {
339 return getConstantFP(Val, VT, true);
341 SDValue getGlobalAddress(const GlobalValue *GV, EVT VT,
342 int64_t offset = 0, bool isTargetGA = false,
343 unsigned char TargetFlags = 0);
344 SDValue getTargetGlobalAddress(const GlobalValue *GV, EVT VT,
346 unsigned char TargetFlags = 0) {
347 return getGlobalAddress(GV, VT, offset, true, TargetFlags);
349 SDValue getFrameIndex(int FI, EVT VT, bool isTarget = false);
350 SDValue getTargetFrameIndex(int FI, EVT VT) {
351 return getFrameIndex(FI, VT, true);
353 SDValue getJumpTable(int JTI, EVT VT, bool isTarget = false,
354 unsigned char TargetFlags = 0);
355 SDValue getTargetJumpTable(int JTI, EVT VT, unsigned char TargetFlags = 0) {
356 return getJumpTable(JTI, VT, true, TargetFlags);
358 SDValue getConstantPool(Constant *C, EVT VT,
359 unsigned Align = 0, int Offs = 0, bool isT=false,
360 unsigned char TargetFlags = 0);
361 SDValue getTargetConstantPool(Constant *C, EVT VT,
362 unsigned Align = 0, int Offset = 0,
363 unsigned char TargetFlags = 0) {
364 return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
366 SDValue getConstantPool(MachineConstantPoolValue *C, EVT VT,
367 unsigned Align = 0, int Offs = 0, bool isT=false,
368 unsigned char TargetFlags = 0);
369 SDValue getTargetConstantPool(MachineConstantPoolValue *C,
370 EVT VT, unsigned Align = 0,
371 int Offset = 0, unsigned char TargetFlags=0) {
372 return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
374 // When generating a branch to a BB, we don't in general know enough
375 // to provide debug info for the BB at that time, so keep this one around.
376 SDValue getBasicBlock(MachineBasicBlock *MBB);
377 SDValue getBasicBlock(MachineBasicBlock *MBB, DebugLoc dl);
378 SDValue getExternalSymbol(const char *Sym, EVT VT);
379 SDValue getExternalSymbol(const char *Sym, DebugLoc dl, EVT VT);
380 SDValue getTargetExternalSymbol(const char *Sym, EVT VT,
381 unsigned char TargetFlags = 0);
382 SDValue getValueType(EVT);
383 SDValue getRegister(unsigned Reg, EVT VT);
384 SDValue getEHLabel(DebugLoc dl, SDValue Root, MCSymbol *Label);
385 SDValue getBlockAddress(BlockAddress *BA, EVT VT,
386 bool isTarget = false, unsigned char TargetFlags = 0);
388 SDValue getCopyToReg(SDValue Chain, DebugLoc dl, unsigned Reg, SDValue N) {
389 return getNode(ISD::CopyToReg, dl, MVT::Other, Chain,
390 getRegister(Reg, N.getValueType()), N);
393 // This version of the getCopyToReg method takes an extra operand, which
394 // indicates that there is potentially an incoming flag value (if Flag is not
395 // null) and that there should be a flag result.
396 SDValue getCopyToReg(SDValue Chain, DebugLoc dl, unsigned Reg, SDValue N,
398 SDVTList VTs = getVTList(MVT::Other, MVT::Flag);
399 SDValue Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Flag };
400 return getNode(ISD::CopyToReg, dl, VTs, Ops, Flag.getNode() ? 4 : 3);
403 // Similar to last getCopyToReg() except parameter Reg is a SDValue
404 SDValue getCopyToReg(SDValue Chain, DebugLoc dl, SDValue Reg, SDValue N,
406 SDVTList VTs = getVTList(MVT::Other, MVT::Flag);
407 SDValue Ops[] = { Chain, Reg, N, Flag };
408 return getNode(ISD::CopyToReg, dl, VTs, Ops, Flag.getNode() ? 4 : 3);
411 SDValue getCopyFromReg(SDValue Chain, DebugLoc dl, unsigned Reg, EVT VT) {
412 SDVTList VTs = getVTList(VT, MVT::Other);
413 SDValue Ops[] = { Chain, getRegister(Reg, VT) };
414 return getNode(ISD::CopyFromReg, dl, VTs, Ops, 2);
417 // This version of the getCopyFromReg method takes an extra operand, which
418 // indicates that there is potentially an incoming flag value (if Flag is not
419 // null) and that there should be a flag result.
420 SDValue getCopyFromReg(SDValue Chain, DebugLoc dl, unsigned Reg, EVT VT,
422 SDVTList VTs = getVTList(VT, MVT::Other, MVT::Flag);
423 SDValue Ops[] = { Chain, getRegister(Reg, VT), Flag };
424 return getNode(ISD::CopyFromReg, dl, VTs, Ops, Flag.getNode() ? 3 : 2);
427 SDValue getCondCode(ISD::CondCode Cond);
429 /// Returns the ConvertRndSat Note: Avoid using this node because it may
430 /// disappear in the future and most targets don't support it.
431 SDValue getConvertRndSat(EVT VT, DebugLoc dl, SDValue Val, SDValue DTy,
433 SDValue Rnd, SDValue Sat, ISD::CvtCode Code);
435 /// getVectorShuffle - Return an ISD::VECTOR_SHUFFLE node. The number of
436 /// elements in VT, which must be a vector type, must match the number of
437 /// mask elements NumElts. A integer mask element equal to -1 is treated as
439 SDValue getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1, SDValue N2,
440 const int *MaskElts);
442 /// getSExtOrTrunc - Convert Op, which must be of integer type, to the
443 /// integer type VT, by either sign-extending or truncating it.
444 SDValue getSExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT);
446 /// getZExtOrTrunc - Convert Op, which must be of integer type, to the
447 /// integer type VT, by either zero-extending or truncating it.
448 SDValue getZExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT);
450 /// getZeroExtendInReg - Return the expression required to zero extend the Op
451 /// value assuming it was the smaller SrcTy value.
452 SDValue getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT SrcTy);
454 /// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
455 SDValue getNOT(DebugLoc DL, SDValue Val, EVT VT);
457 /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
458 /// a flag result (to ensure it's not CSE'd). CALLSEQ_START does not have a
460 SDValue getCALLSEQ_START(SDValue Chain, SDValue Op) {
461 SDVTList VTs = getVTList(MVT::Other, MVT::Flag);
462 SDValue Ops[] = { Chain, Op };
463 return getNode(ISD::CALLSEQ_START, DebugLoc::getUnknownLoc(),
467 /// getCALLSEQ_END - Return a new CALLSEQ_END node, which always must have a
468 /// flag result (to ensure it's not CSE'd). CALLSEQ_END does not have
469 /// a useful DebugLoc.
470 SDValue getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2,
472 SDVTList NodeTys = getVTList(MVT::Other, MVT::Flag);
473 SmallVector<SDValue, 4> Ops;
474 Ops.push_back(Chain);
477 Ops.push_back(InFlag);
478 return getNode(ISD::CALLSEQ_END, DebugLoc::getUnknownLoc(), NodeTys,
480 (unsigned)Ops.size() - (InFlag.getNode() == 0 ? 1 : 0));
483 /// getUNDEF - Return an UNDEF node. UNDEF does not have a useful DebugLoc.
484 SDValue getUNDEF(EVT VT) {
485 return getNode(ISD::UNDEF, DebugLoc::getUnknownLoc(), VT);
488 /// getGLOBAL_OFFSET_TABLE - Return a GLOBAL_OFFSET_TABLE node. This does
489 /// not have a useful DebugLoc.
490 SDValue getGLOBAL_OFFSET_TABLE(EVT VT) {
491 return getNode(ISD::GLOBAL_OFFSET_TABLE, DebugLoc::getUnknownLoc(), VT);
494 /// getNode - Gets or creates the specified node.
496 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT);
497 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT, SDValue N);
498 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT, SDValue N1, SDValue N2);
499 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
500 SDValue N1, SDValue N2, SDValue N3);
501 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
502 SDValue N1, SDValue N2, SDValue N3, SDValue N4);
503 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
504 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
506 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
507 const SDUse *Ops, unsigned NumOps);
508 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
509 const SDValue *Ops, unsigned NumOps);
510 SDValue getNode(unsigned Opcode, DebugLoc DL,
511 const std::vector<EVT> &ResultTys,
512 const SDValue *Ops, unsigned NumOps);
513 SDValue getNode(unsigned Opcode, DebugLoc DL, const EVT *VTs, unsigned NumVTs,
514 const SDValue *Ops, unsigned NumOps);
515 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
516 const SDValue *Ops, unsigned NumOps);
517 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs);
518 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs, SDValue N);
519 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
520 SDValue N1, SDValue N2);
521 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
522 SDValue N1, SDValue N2, SDValue N3);
523 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
524 SDValue N1, SDValue N2, SDValue N3, SDValue N4);
525 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
526 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
529 /// getStackArgumentTokenFactor - Compute a TokenFactor to force all
530 /// the incoming stack arguments to be loaded from the stack. This is
531 /// used in tail call lowering to protect stack arguments from being
533 SDValue getStackArgumentTokenFactor(SDValue Chain);
535 SDValue getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst, SDValue Src,
536 SDValue Size, unsigned Align, bool AlwaysInline,
537 const Value *DstSV, uint64_t DstSVOff,
538 const Value *SrcSV, uint64_t SrcSVOff);
540 SDValue getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst, SDValue Src,
541 SDValue Size, unsigned Align,
542 const Value *DstSV, uint64_t DstOSVff,
543 const Value *SrcSV, uint64_t SrcSVOff);
545 SDValue getMemset(SDValue Chain, DebugLoc dl, SDValue Dst, SDValue Src,
546 SDValue Size, unsigned Align,
547 const Value *DstSV, uint64_t DstSVOff);
549 /// getSetCC - Helper function to make it easier to build SetCC's if you just
550 /// have an ISD::CondCode instead of an SDValue.
552 SDValue getSetCC(DebugLoc DL, EVT VT, SDValue LHS, SDValue RHS,
553 ISD::CondCode Cond) {
554 return getNode(ISD::SETCC, DL, VT, LHS, RHS, getCondCode(Cond));
557 /// getVSetCC - Helper function to make it easier to build VSetCC's nodes
558 /// if you just have an ISD::CondCode instead of an SDValue.
560 SDValue getVSetCC(DebugLoc DL, EVT VT, SDValue LHS, SDValue RHS,
561 ISD::CondCode Cond) {
562 return getNode(ISD::VSETCC, DL, VT, LHS, RHS, getCondCode(Cond));
565 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
566 /// just have an ISD::CondCode instead of an SDValue.
568 SDValue getSelectCC(DebugLoc DL, SDValue LHS, SDValue RHS,
569 SDValue True, SDValue False, ISD::CondCode Cond) {
570 return getNode(ISD::SELECT_CC, DL, True.getValueType(),
571 LHS, RHS, True, False, getCondCode(Cond));
574 /// getVAArg - VAArg produces a result and token chain, and takes a pointer
575 /// and a source value as input.
576 SDValue getVAArg(EVT VT, DebugLoc dl, SDValue Chain, SDValue Ptr,
579 /// getAtomic - Gets a node for an atomic op, produces result and chain and
581 SDValue getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, SDValue Chain,
582 SDValue Ptr, SDValue Cmp, SDValue Swp, const Value* PtrVal,
583 unsigned Alignment=0);
584 SDValue getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, SDValue Chain,
585 SDValue Ptr, SDValue Cmp, SDValue Swp,
586 MachineMemOperand *MMO);
588 /// getAtomic - Gets a node for an atomic op, produces result and chain and
589 /// takes 2 operands.
590 SDValue getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, SDValue Chain,
591 SDValue Ptr, SDValue Val, const Value* PtrVal,
592 unsigned Alignment = 0);
593 SDValue getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, SDValue Chain,
594 SDValue Ptr, SDValue Val,
595 MachineMemOperand *MMO);
597 /// getMemIntrinsicNode - Creates a MemIntrinsicNode that may produce a
598 /// result and takes a list of operands. Opcode may be INTRINSIC_VOID,
599 /// INTRINSIC_W_CHAIN, or a target-specific opcode with a value not
600 /// less than FIRST_TARGET_MEMORY_OPCODE.
601 SDValue getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
602 const EVT *VTs, unsigned NumVTs,
603 const SDValue *Ops, unsigned NumOps,
604 EVT MemVT, const Value *srcValue, int SVOff,
605 unsigned Align = 0, bool Vol = false,
606 bool ReadMem = true, bool WriteMem = true);
608 SDValue getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
609 const SDValue *Ops, unsigned NumOps,
610 EVT MemVT, const Value *srcValue, int SVOff,
611 unsigned Align = 0, bool Vol = false,
612 bool ReadMem = true, bool WriteMem = true);
614 SDValue getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
615 const SDValue *Ops, unsigned NumOps,
616 EVT MemVT, MachineMemOperand *MMO);
618 /// getMergeValues - Create a MERGE_VALUES node from the given operands.
619 SDValue getMergeValues(const SDValue *Ops, unsigned NumOps, DebugLoc dl);
621 /// getLoad - Loads are not normal binary operators: their result type is not
622 /// determined by their operands, and they produce a value AND a token chain.
624 SDValue getLoad(EVT VT, DebugLoc dl, SDValue Chain, SDValue Ptr,
625 const Value *SV, int SVOffset, bool isVolatile,
626 bool isNonTemporal, unsigned Alignment);
627 SDValue getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT,
628 SDValue Chain, SDValue Ptr, const Value *SV,
629 int SVOffset, EVT MemVT, bool isVolatile,
630 bool isNonTemporal, unsigned Alignment);
631 SDValue getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
632 SDValue Offset, ISD::MemIndexedMode AM);
633 SDValue getLoad(ISD::MemIndexedMode AM, DebugLoc dl, ISD::LoadExtType ExtType,
634 EVT VT, SDValue Chain, SDValue Ptr, SDValue Offset,
635 const Value *SV, int SVOffset, EVT MemVT,
636 bool isVolatile, bool isNonTemporal, unsigned Alignment);
637 SDValue getLoad(ISD::MemIndexedMode AM, DebugLoc dl, ISD::LoadExtType ExtType,
638 EVT VT, SDValue Chain, SDValue Ptr, SDValue Offset,
639 EVT MemVT, MachineMemOperand *MMO);
641 /// getStore - Helper function to build ISD::STORE nodes.
643 SDValue getStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
644 const Value *SV, int SVOffset, bool isVolatile,
645 bool isNonTemporal, unsigned Alignment);
646 SDValue getStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
647 MachineMemOperand *MMO);
648 SDValue getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
649 const Value *SV, int SVOffset, EVT TVT,
650 bool isNonTemporal, bool isVolatile,
652 SDValue getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
653 EVT TVT, MachineMemOperand *MMO);
654 SDValue getIndexedStore(SDValue OrigStoe, DebugLoc dl, SDValue Base,
655 SDValue Offset, ISD::MemIndexedMode AM);
657 /// getSrcValue - Construct a node to track a Value* through the backend.
658 SDValue getSrcValue(const Value *v);
660 /// getShiftAmountOperand - Return the specified value casted to
661 /// the target's desired shift amount type.
662 SDValue getShiftAmountOperand(SDValue Op);
664 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
665 /// specified operands. If the resultant node already exists in the DAG,
666 /// this does not modify the specified node, instead it returns the node that
667 /// already exists. If the resultant node does not exist in the DAG, the
668 /// input node is returned. As a degenerate case, if you specify the same
669 /// input operands as the node already has, the input node is returned.
670 SDValue UpdateNodeOperands(SDValue N, SDValue Op);
671 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2);
672 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
674 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
675 SDValue Op3, SDValue Op4);
676 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
677 SDValue Op3, SDValue Op4, SDValue Op5);
678 SDValue UpdateNodeOperands(SDValue N,
679 const SDValue *Ops, unsigned NumOps);
681 /// SelectNodeTo - These are used for target selectors to *mutate* the
682 /// specified node to have the specified return type, Target opcode, and
683 /// operands. Note that target opcodes are stored as
684 /// ~TargetOpcode in the node opcode field. The resultant node is returned.
685 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT);
686 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT, SDValue Op1);
687 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
688 SDValue Op1, SDValue Op2);
689 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
690 SDValue Op1, SDValue Op2, SDValue Op3);
691 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
692 const SDValue *Ops, unsigned NumOps);
693 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1, EVT VT2);
694 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
695 EVT VT2, const SDValue *Ops, unsigned NumOps);
696 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
697 EVT VT2, EVT VT3, const SDValue *Ops, unsigned NumOps);
698 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1,
699 EVT VT2, EVT VT3, EVT VT4, const SDValue *Ops,
701 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
702 EVT VT2, SDValue Op1);
703 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
704 EVT VT2, SDValue Op1, SDValue Op2);
705 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
706 EVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
707 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
708 EVT VT2, EVT VT3, SDValue Op1, SDValue Op2, SDValue Op3);
709 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, SDVTList VTs,
710 const SDValue *Ops, unsigned NumOps);
712 /// MorphNodeTo - This *mutates* the specified node to have the specified
713 /// return type, opcode, and operands.
714 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs,
715 const SDValue *Ops, unsigned NumOps);
717 /// getMachineNode - These are used for target selectors to create a new node
718 /// with specified return type(s), MachineInstr opcode, and operands.
720 /// Note that getMachineNode returns the resultant node. If there is already
721 /// a node of the specified opcode and operands, it returns that node instead
722 /// of the current one.
723 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT);
724 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
726 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
727 SDValue Op1, SDValue Op2);
728 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
729 SDValue Op1, SDValue Op2, SDValue Op3);
730 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
731 const SDValue *Ops, unsigned NumOps);
732 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2);
733 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
735 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
736 EVT VT2, SDValue Op1, SDValue Op2);
737 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
738 EVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
739 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
740 const SDValue *Ops, unsigned NumOps);
741 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
742 EVT VT3, SDValue Op1, SDValue Op2);
743 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
744 EVT VT3, SDValue Op1, SDValue Op2, SDValue Op3);
745 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
746 EVT VT3, const SDValue *Ops, unsigned NumOps);
747 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
748 EVT VT3, EVT VT4, const SDValue *Ops, unsigned NumOps);
749 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl,
750 const std::vector<EVT> &ResultTys, const SDValue *Ops,
752 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, SDVTList VTs,
753 const SDValue *Ops, unsigned NumOps);
755 /// getTargetExtractSubreg - A convenience function for creating
756 /// TargetInstrInfo::EXTRACT_SUBREG nodes.
757 SDValue getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
760 /// getTargetInsertSubreg - A convenience function for creating
761 /// TargetInstrInfo::INSERT_SUBREG nodes.
762 SDValue getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT,
763 SDValue Operand, SDValue Subreg);
765 /// getNodeIfExists - Get the specified node if it's already available, or
766 /// else return NULL.
767 SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTs,
768 const SDValue *Ops, unsigned NumOps);
770 /// DAGUpdateListener - Clients of various APIs that cause global effects on
771 /// the DAG can optionally implement this interface. This allows the clients
772 /// to handle the various sorts of updates that happen.
773 class DAGUpdateListener {
775 virtual ~DAGUpdateListener();
777 /// NodeDeleted - The node N that was deleted and, if E is not null, an
778 /// equivalent node E that replaced it.
779 virtual void NodeDeleted(SDNode *N, SDNode *E) = 0;
781 /// NodeUpdated - The node N that was updated.
782 virtual void NodeUpdated(SDNode *N) = 0;
785 /// RemoveDeadNode - Remove the specified node from the system. If any of its
786 /// operands then becomes dead, remove them as well. Inform UpdateListener
787 /// for each node deleted.
788 void RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener = 0);
790 /// RemoveDeadNodes - This method deletes the unreachable nodes in the
791 /// given list, and any nodes that become unreachable as a result.
792 void RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
793 DAGUpdateListener *UpdateListener = 0);
795 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
796 /// This can cause recursive merging of nodes in the DAG. Use the first
797 /// version if 'From' is known to have a single result, use the second
798 /// if you have two nodes with identical results (or if 'To' has a superset
799 /// of the results of 'From'), use the third otherwise.
801 /// These methods all take an optional UpdateListener, which (if not null) is
802 /// informed about nodes that are deleted and modified due to recursive
803 /// changes in the dag.
805 /// These functions only replace all existing uses. It's possible that as
806 /// these replacements are being performed, CSE may cause the From node
807 /// to be given new uses. These new uses of From are left in place, and
808 /// not automatically transfered to To.
810 void ReplaceAllUsesWith(SDValue From, SDValue Op,
811 DAGUpdateListener *UpdateListener = 0);
812 void ReplaceAllUsesWith(SDNode *From, SDNode *To,
813 DAGUpdateListener *UpdateListener = 0);
814 void ReplaceAllUsesWith(SDNode *From, const SDValue *To,
815 DAGUpdateListener *UpdateListener = 0);
817 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
818 /// uses of other values produced by From.Val alone.
819 void ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
820 DAGUpdateListener *UpdateListener = 0);
822 /// ReplaceAllUsesOfValuesWith - Like ReplaceAllUsesOfValueWith, but
823 /// for multiple values at once. This correctly handles the case where
824 /// there is an overlap between the From values and the To values.
825 void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To,
827 DAGUpdateListener *UpdateListener = 0);
829 /// AssignTopologicalOrder - Topological-sort the AllNodes list and a
830 /// assign a unique node id for each node in the DAG based on their
831 /// topological order. Returns the number of nodes.
832 unsigned AssignTopologicalOrder();
834 /// RepositionNode - Move node N in the AllNodes list to be immediately
835 /// before the given iterator Position. This may be used to update the
836 /// topological ordering when the list of nodes is modified.
837 void RepositionNode(allnodes_iterator Position, SDNode *N) {
838 AllNodes.insert(Position, AllNodes.remove(N));
841 /// isCommutativeBinOp - Returns true if the opcode is a commutative binary
843 static bool isCommutativeBinOp(unsigned Opcode) {
844 // FIXME: This should get its info from the td file, so that we can include
861 case ISD::ADDE: return true;
862 default: return false;
866 /// AssignOrdering - Assign an order to the SDNode.
867 void AssignOrdering(const SDNode *SD, unsigned Order);
869 /// GetOrdering - Get the order for the SDNode.
870 unsigned GetOrdering(const SDNode *SD) const;
872 /// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
873 /// value is produced by SD.
874 void AddDbgValue(SDDbgValue *DB, SDNode *SD = 0);
876 /// GetDbgValues - Get the debug values which reference the given SDNode.
877 SmallVector<SDDbgValue*,2> &GetDbgValues(const SDNode* SD) {
878 return DbgInfo->getSDDbgValues(SD);
881 /// hasDebugValues - Return true if there are any SDDbgValue nodes associated
882 /// with this SelectionDAG.
883 bool hasDebugValues() const { return !DbgInfo->empty(); }
885 SDDbgInfo::DbgIterator DbgBegin() { return DbgInfo->DbgBegin(); }
886 SDDbgInfo::DbgIterator DbgEnd() { return DbgInfo->DbgEnd(); }
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(EVT VT, unsigned minAlign = 1);
895 /// CreateStackTemporary - Create a stack temporary suitable for holding
896 /// either of the specified value types.
897 SDValue CreateStackTemporary(EVT VT1, EVT 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(EVT 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 /// isKnownNeverNan - Test whether the given SDValue is known to never be NaN.
937 bool isKnownNeverNaN(SDValue Op) const;
939 /// isKnownNeverZero - Test whether the given SDValue is known to never be
940 /// positive or negative Zero.
941 bool isKnownNeverZero(SDValue Op) const;
943 /// isEqualTo - Test whether two SDValues are known to compare equal. This
944 /// is true if they are the same value, or if one is negative zero and the
945 /// other positive zero.
946 bool isEqualTo(SDValue A, SDValue B) const;
948 /// isVerifiedDebugInfoDesc - Returns true if the specified SDValue has
949 /// been verified as a debug information descriptor.
950 bool isVerifiedDebugInfoDesc(SDValue Op) const;
952 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
953 /// element of the result of the vector shuffle.
954 SDValue getShuffleScalarElt(const ShuffleVectorSDNode *N, unsigned Idx);
956 /// UnrollVectorOp - Utility function used by legalize and lowering to
957 /// "unroll" a vector operation by splitting out the scalars and operating
958 /// on each element individually. If the ResNE is 0, fully unroll the vector
959 /// op. If ResNE is less than the width of the vector op, unroll up to ResNE.
960 /// If the ResNE is greater than the width of the vector op, unroll the
961 /// vector op and fill the end of the resulting vector with UNDEFS.
962 SDValue UnrollVectorOp(SDNode *N, unsigned ResNE = 0);
964 /// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
965 /// location that is 'Dist' units away from the location that the 'Base' load
967 bool isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
968 unsigned Bytes, int Dist) const;
970 /// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
971 /// it cannot be inferred.
972 unsigned InferPtrAlignment(SDValue Ptr) const;
975 bool RemoveNodeFromCSEMaps(SDNode *N);
976 void AddModifiedNodeToCSEMaps(SDNode *N, DAGUpdateListener *UpdateListener);
977 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op, void *&InsertPos);
978 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op1, SDValue Op2,
980 SDNode *FindModifiedNodeSlot(SDNode *N, const SDValue *Ops, unsigned NumOps,
983 void DeleteNodeNotInCSEMaps(SDNode *N);
984 void DeallocateNode(SDNode *N);
986 unsigned getEVTAlignment(EVT MemoryVT) const;
988 void allnodes_clear();
990 /// VTList - List of non-single value types.
991 std::vector<SDVTList> VTList;
993 /// CondCodeNodes - Maps to auto-CSE operations.
994 std::vector<CondCodeSDNode*> CondCodeNodes;
996 std::vector<SDNode*> ValueTypeNodes;
997 std::map<EVT, SDNode*, EVT::compareRawBits> ExtendedValueTypeNodes;
998 StringMap<SDNode*> ExternalSymbols;
1000 std::map<std::pair<std::string, unsigned char>,SDNode*> TargetExternalSymbols;
1003 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
1004 typedef SelectionDAG::allnodes_iterator nodes_iterator;
1005 static nodes_iterator nodes_begin(SelectionDAG *G) {
1006 return G->allnodes_begin();
1008 static nodes_iterator nodes_end(SelectionDAG *G) {
1009 return G->allnodes_end();
1013 } // end namespace llvm