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 MachineModuleInfo;
35 class MachineFunction;
36 class MachineConstantPoolValue;
37 class FunctionLoweringInfo;
39 template<> struct ilist_traits<SDNode> : public ilist_default_traits<SDNode> {
41 mutable ilist_half_node<SDNode> Sentinel;
43 SDNode *createSentinel() const {
44 return static_cast<SDNode*>(&Sentinel);
46 static void destroySentinel(SDNode *) {}
48 SDNode *provideInitialHead() const { return createSentinel(); }
49 SDNode *ensureHead(SDNode*) const { return createSentinel(); }
50 static void noteHead(SDNode*, SDNode*) {}
52 static void deleteNode(SDNode *) {
53 assert(0 && "ilist_traits<SDNode> shouldn't see a deleteNode call!");
56 static void createNode(const SDNode &);
60 Unrestricted, // Combine may create illegal operations and illegal types.
61 NoIllegalTypes, // Combine may create illegal operations but no illegal types.
62 NoIllegalOperations // Combine may only create legal operations and types.
65 /// SelectionDAG class - This is used to represent a portion of an LLVM function
66 /// in a low-level Data Dependence DAG representation suitable for instruction
67 /// selection. This DAG is constructed as the first step of instruction
68 /// selection in order to allow implementation of machine specific optimizations
69 /// and code simplifications.
71 /// The representation used by the SelectionDAG is a target-independent
72 /// representation, which has some similarities to the GCC RTL representation,
73 /// but is significantly more simple, powerful, and is a graph form instead of a
79 FunctionLoweringInfo &FLI;
80 MachineModuleInfo *MMI;
84 /// EntryNode - The starting token.
87 /// Root - The root of the entire DAG.
90 /// AllNodes - A linked list of nodes in the current DAG.
91 ilist<SDNode> AllNodes;
93 /// NodeAllocatorType - The AllocatorType for allocating SDNodes. We use
94 /// pool allocation with recycling.
95 typedef RecyclingAllocator<BumpPtrAllocator, SDNode, sizeof(LargestSDNode),
96 AlignOf<MostAlignedSDNode>::Alignment>
99 /// NodeAllocator - Pool allocation for nodes.
100 NodeAllocatorType NodeAllocator;
102 /// CSEMap - This structure is used to memoize nodes, automatically performing
103 /// CSE with existing nodes when a duplicate is requested.
104 FoldingSet<SDNode> CSEMap;
106 /// OperandAllocator - Pool allocation for machine-opcode SDNode operands.
107 BumpPtrAllocator OperandAllocator;
109 /// Allocator - Pool allocation for misc. objects that are created once per
111 BumpPtrAllocator Allocator;
113 /// NodeOrdering - Assigns a "line number" value to each SDNode that
114 /// corresponds to the "line number" of the original LLVM instruction. This
115 /// used for turning off scheduling, because we'll forgo the normal scheduling
116 /// algorithm and output the instructions according to this ordering.
118 /// LineNo - The line of the instruction the node corresponds to. A value of
119 /// `0' means it's not assigned.
121 std::map<const SDNode*, unsigned> Order;
123 void operator=(const NodeOrdering&); // Do not implement.
124 NodeOrdering(const NodeOrdering&); // Do not implement.
126 NodeOrdering() : LineNo(0) {}
128 void add(const SDNode *Node) {
129 assert(LineNo && "Invalid line number!");
130 Order[Node] = LineNo;
132 void remove(const SDNode *Node) {
133 std::map<const SDNode*, unsigned>::iterator Itr = Order.find(Node);
134 if (Itr != Order.end())
141 unsigned getLineNo(const SDNode *Node) {
142 unsigned LN = Order[Node];
143 assert(LN && "Node isn't in ordering map!");
153 /// VerifyNode - Sanity check the given node. Aborts if it is invalid.
154 void VerifyNode(SDNode *N);
156 /// setGraphColorHelper - Implementation of setSubgraphColor.
157 /// Return whether we had to truncate the search.
159 bool setSubgraphColorHelper(SDNode *N, const char *Color,
160 DenseSet<SDNode *> &visited,
161 int level, bool &printed);
163 void operator=(const SelectionDAG&); // Do not implement.
164 SelectionDAG(const SelectionDAG&); // Do not implement.
167 SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli);
170 /// init - Prepare this SelectionDAG to process code in the given
173 void init(MachineFunction &mf, MachineModuleInfo *mmi, DwarfWriter *dw);
175 /// clear - Clear state and free memory necessary to make this
176 /// SelectionDAG ready to process a new block.
180 MachineFunction &getMachineFunction() const { return *MF; }
181 const TargetMachine &getTarget() const;
182 TargetLowering &getTargetLoweringInfo() const { return TLI; }
183 FunctionLoweringInfo &getFunctionLoweringInfo() const { return FLI; }
184 MachineModuleInfo *getMachineModuleInfo() const { return MMI; }
185 DwarfWriter *getDwarfWriter() const { return DW; }
186 LLVMContext *getContext() const {return Context; }
188 /// viewGraph - Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
190 void viewGraph(const std::string &Title);
194 std::map<const SDNode *, std::string> NodeGraphAttrs;
197 /// clearGraphAttrs - Clear all previously defined node graph attributes.
198 /// Intended to be used from a debugging tool (eg. gdb).
199 void clearGraphAttrs();
201 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
203 void setGraphAttrs(const SDNode *N, const char *Attrs);
205 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
206 /// Used from getNodeAttributes.
207 const std::string getGraphAttrs(const SDNode *N) const;
209 /// setGraphColor - Convenience for setting node color attribute.
211 void setGraphColor(const SDNode *N, const char *Color);
213 /// setGraphColor - Convenience for setting subgraph color attribute.
215 void setSubgraphColor(SDNode *N, const char *Color);
217 typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
218 allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
219 allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
220 typedef ilist<SDNode>::iterator allnodes_iterator;
221 allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
222 allnodes_iterator allnodes_end() { return AllNodes.end(); }
223 ilist<SDNode>::size_type allnodes_size() const {
224 return AllNodes.size();
227 /// getRoot - Return the root tag of the SelectionDAG.
229 const SDValue &getRoot() const { return Root; }
231 /// getEntryNode - Return the token chain corresponding to the entry of the
233 SDValue getEntryNode() const {
234 return SDValue(const_cast<SDNode *>(&EntryNode), 0);
237 /// setRoot - Set the current root tag of the SelectionDAG.
239 const SDValue &setRoot(SDValue N) {
240 assert((!N.getNode() || N.getValueType() == MVT::Other) &&
241 "DAG root value is not a chain!");
245 /// NewInst - Tell the ordering object that we're processing a new
252 /// Combine - This iterates over the nodes in the SelectionDAG, folding
253 /// certain types of nodes together, or eliminating superfluous nodes. The
254 /// Level argument controls whether Combine is allowed to produce nodes and
255 /// types that are illegal on the target.
256 void Combine(CombineLevel Level, AliasAnalysis &AA,
257 CodeGenOpt::Level OptLevel);
259 /// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that
260 /// only uses types natively supported by the target. Returns "true" if it
261 /// made any changes.
263 /// Note that this is an involved process that may invalidate pointers into
265 bool LegalizeTypes();
267 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
268 /// compatible with the target instruction selector, as indicated by the
269 /// TargetLowering object.
271 /// Note that this is an involved process that may invalidate pointers into
273 void Legalize(CodeGenOpt::Level OptLevel);
275 /// LegalizeVectors - This transforms the SelectionDAG into a SelectionDAG
276 /// that only uses vector math operations supported by the target. This is
277 /// necessary as a separate step from Legalize because unrolling a vector
278 /// operation can introduce illegal types, which requires running
279 /// LegalizeTypes again.
281 /// This returns true if it made any changes; in that case, LegalizeTypes
282 /// is called again before Legalize.
284 /// Note that this is an involved process that may invalidate pointers into
286 bool LegalizeVectors();
288 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
290 void RemoveDeadNodes();
292 /// DeleteNode - Remove the specified node from the system. This node must
293 /// have no referrers.
294 void DeleteNode(SDNode *N);
296 /// getVTList - Return an SDVTList that represents the list of values
298 SDVTList getVTList(EVT VT);
299 SDVTList getVTList(EVT VT1, EVT VT2);
300 SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3);
301 SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4);
302 SDVTList getVTList(const EVT *VTs, unsigned NumVTs);
304 //===--------------------------------------------------------------------===//
305 // Node creation methods.
307 SDValue getConstant(uint64_t Val, EVT VT, bool isTarget = false);
308 SDValue getConstant(const APInt &Val, EVT VT, bool isTarget = false);
309 SDValue getConstant(const ConstantInt &Val, EVT VT, bool isTarget = false);
310 SDValue getIntPtrConstant(uint64_t Val, bool isTarget = false);
311 SDValue getTargetConstant(uint64_t Val, EVT VT) {
312 return getConstant(Val, VT, true);
314 SDValue getTargetConstant(const APInt &Val, EVT VT) {
315 return getConstant(Val, VT, true);
317 SDValue getTargetConstant(const ConstantInt &Val, EVT VT) {
318 return getConstant(Val, VT, true);
320 SDValue getConstantFP(double Val, EVT VT, bool isTarget = false);
321 SDValue getConstantFP(const APFloat& Val, EVT VT, bool isTarget = false);
322 SDValue getConstantFP(const ConstantFP &CF, EVT VT, bool isTarget = false);
323 SDValue getTargetConstantFP(double Val, EVT VT) {
324 return getConstantFP(Val, VT, true);
326 SDValue getTargetConstantFP(const APFloat& Val, EVT VT) {
327 return getConstantFP(Val, VT, true);
329 SDValue getTargetConstantFP(const ConstantFP &Val, EVT VT) {
330 return getConstantFP(Val, VT, true);
332 SDValue getGlobalAddress(const GlobalValue *GV, EVT VT,
333 int64_t offset = 0, bool isTargetGA = false,
334 unsigned char TargetFlags = 0);
335 SDValue getTargetGlobalAddress(const GlobalValue *GV, EVT VT,
337 unsigned char TargetFlags = 0) {
338 return getGlobalAddress(GV, VT, offset, true, TargetFlags);
340 SDValue getFrameIndex(int FI, EVT VT, bool isTarget = false);
341 SDValue getTargetFrameIndex(int FI, EVT VT) {
342 return getFrameIndex(FI, VT, true);
344 SDValue getJumpTable(int JTI, EVT VT, bool isTarget = false,
345 unsigned char TargetFlags = 0);
346 SDValue getTargetJumpTable(int JTI, EVT VT, unsigned char TargetFlags = 0) {
347 return getJumpTable(JTI, VT, true, TargetFlags);
349 SDValue getConstantPool(Constant *C, EVT VT,
350 unsigned Align = 0, int Offs = 0, bool isT=false,
351 unsigned char TargetFlags = 0);
352 SDValue getTargetConstantPool(Constant *C, EVT VT,
353 unsigned Align = 0, int Offset = 0,
354 unsigned char TargetFlags = 0) {
355 return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
357 SDValue getConstantPool(MachineConstantPoolValue *C, EVT VT,
358 unsigned Align = 0, int Offs = 0, bool isT=false,
359 unsigned char TargetFlags = 0);
360 SDValue getTargetConstantPool(MachineConstantPoolValue *C,
361 EVT VT, unsigned Align = 0,
362 int Offset = 0, unsigned char TargetFlags=0) {
363 return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
365 // When generating a branch to a BB, we don't in general know enough
366 // to provide debug info for the BB at that time, so keep this one around.
367 SDValue getBasicBlock(MachineBasicBlock *MBB);
368 SDValue getBasicBlock(MachineBasicBlock *MBB, DebugLoc dl);
369 SDValue getExternalSymbol(const char *Sym, EVT VT);
370 SDValue getExternalSymbol(const char *Sym, DebugLoc dl, EVT VT);
371 SDValue getTargetExternalSymbol(const char *Sym, EVT VT,
372 unsigned char TargetFlags = 0);
373 SDValue getValueType(EVT);
374 SDValue getRegister(unsigned Reg, EVT VT);
375 SDValue getLabel(unsigned Opcode, DebugLoc dl, SDValue Root,
377 SDValue getBlockAddress(BlockAddress *BA, EVT VT,
378 bool isTarget = false, unsigned char TargetFlags = 0);
380 SDValue getCopyToReg(SDValue Chain, DebugLoc dl, unsigned Reg, SDValue N) {
381 return getNode(ISD::CopyToReg, dl, MVT::Other, Chain,
382 getRegister(Reg, N.getValueType()), N);
385 // This version of the getCopyToReg method takes an extra operand, which
386 // indicates that there is potentially an incoming flag value (if Flag is not
387 // null) and that there should be a flag result.
388 SDValue getCopyToReg(SDValue Chain, DebugLoc dl, unsigned Reg, SDValue N,
390 SDVTList VTs = getVTList(MVT::Other, MVT::Flag);
391 SDValue Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Flag };
392 return getNode(ISD::CopyToReg, dl, VTs, Ops, Flag.getNode() ? 4 : 3);
395 // Similar to last getCopyToReg() except parameter Reg is a SDValue
396 SDValue getCopyToReg(SDValue Chain, DebugLoc dl, SDValue Reg, SDValue N,
398 SDVTList VTs = getVTList(MVT::Other, MVT::Flag);
399 SDValue Ops[] = { Chain, Reg, N, Flag };
400 return getNode(ISD::CopyToReg, dl, VTs, Ops, Flag.getNode() ? 4 : 3);
403 SDValue getCopyFromReg(SDValue Chain, DebugLoc dl, unsigned Reg, EVT VT) {
404 SDVTList VTs = getVTList(VT, MVT::Other);
405 SDValue Ops[] = { Chain, getRegister(Reg, VT) };
406 return getNode(ISD::CopyFromReg, dl, VTs, Ops, 2);
409 // This version of the getCopyFromReg method takes an extra operand, which
410 // indicates that there is potentially an incoming flag value (if Flag is not
411 // null) and that there should be a flag result.
412 SDValue getCopyFromReg(SDValue Chain, DebugLoc dl, unsigned Reg, EVT VT,
414 SDVTList VTs = getVTList(VT, MVT::Other, MVT::Flag);
415 SDValue Ops[] = { Chain, getRegister(Reg, VT), Flag };
416 return getNode(ISD::CopyFromReg, dl, VTs, Ops, Flag.getNode() ? 3 : 2);
419 SDValue getCondCode(ISD::CondCode Cond);
421 /// Returns the ConvertRndSat Note: Avoid using this node because it may
422 /// disappear in the future and most targets don't support it.
423 SDValue getConvertRndSat(EVT VT, DebugLoc dl, SDValue Val, SDValue DTy,
425 SDValue Rnd, SDValue Sat, ISD::CvtCode Code);
427 /// getVectorShuffle - Return an ISD::VECTOR_SHUFFLE node. The number of
428 /// elements in VT, which must be a vector type, must match the number of
429 /// mask elements NumElts. A integer mask element equal to -1 is treated as
431 SDValue getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1, SDValue N2,
432 const int *MaskElts);
434 /// getSExtOrTrunc - Convert Op, which must be of integer type, to the
435 /// integer type VT, by either sign-extending or truncating it.
436 SDValue getSExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT);
438 /// getZExtOrTrunc - Convert Op, which must be of integer type, to the
439 /// integer type VT, by either zero-extending or truncating it.
440 SDValue getZExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT);
442 /// getZeroExtendInReg - Return the expression required to zero extend the Op
443 /// value assuming it was the smaller SrcTy value.
444 SDValue getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT SrcTy);
446 /// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
447 SDValue getNOT(DebugLoc DL, SDValue Val, EVT VT);
449 /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
450 /// a flag result (to ensure it's not CSE'd). CALLSEQ_START does not have a
452 SDValue getCALLSEQ_START(SDValue Chain, SDValue Op) {
453 SDVTList VTs = getVTList(MVT::Other, MVT::Flag);
454 SDValue Ops[] = { Chain, Op };
455 return getNode(ISD::CALLSEQ_START, DebugLoc::getUnknownLoc(),
459 /// getCALLSEQ_END - Return a new CALLSEQ_END node, which always must have a
460 /// flag result (to ensure it's not CSE'd). CALLSEQ_END does not have
461 /// a useful DebugLoc.
462 SDValue getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2,
464 SDVTList NodeTys = getVTList(MVT::Other, MVT::Flag);
465 SmallVector<SDValue, 4> Ops;
466 Ops.push_back(Chain);
469 Ops.push_back(InFlag);
470 return getNode(ISD::CALLSEQ_END, DebugLoc::getUnknownLoc(), NodeTys,
472 (unsigned)Ops.size() - (InFlag.getNode() == 0 ? 1 : 0));
475 /// getUNDEF - Return an UNDEF node. UNDEF does not have a useful DebugLoc.
476 SDValue getUNDEF(EVT VT) {
477 return getNode(ISD::UNDEF, DebugLoc::getUnknownLoc(), VT);
480 /// getGLOBAL_OFFSET_TABLE - Return a GLOBAL_OFFSET_TABLE node. This does
481 /// not have a useful DebugLoc.
482 SDValue getGLOBAL_OFFSET_TABLE(EVT VT) {
483 return getNode(ISD::GLOBAL_OFFSET_TABLE, DebugLoc::getUnknownLoc(), VT);
486 /// getNode - Gets or creates the specified node.
488 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT);
489 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT, SDValue N);
490 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT, SDValue N1, SDValue N2);
491 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
492 SDValue N1, SDValue N2, SDValue N3);
493 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
494 SDValue N1, SDValue N2, SDValue N3, SDValue N4);
495 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
496 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
498 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
499 const SDUse *Ops, unsigned NumOps);
500 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
501 const SDValue *Ops, unsigned NumOps);
502 SDValue getNode(unsigned Opcode, DebugLoc DL,
503 const std::vector<EVT> &ResultTys,
504 const SDValue *Ops, unsigned NumOps);
505 SDValue getNode(unsigned Opcode, DebugLoc DL, const EVT *VTs, unsigned NumVTs,
506 const SDValue *Ops, unsigned NumOps);
507 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
508 const SDValue *Ops, unsigned NumOps);
509 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs);
510 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs, SDValue N);
511 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
512 SDValue N1, SDValue N2);
513 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
514 SDValue N1, SDValue N2, SDValue N3);
515 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
516 SDValue N1, SDValue N2, SDValue N3, SDValue N4);
517 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
518 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
521 /// getStackArgumentTokenFactor - Compute a TokenFactor to force all
522 /// the incoming stack arguments to be loaded from the stack. This is
523 /// used in tail call lowering to protect stack arguments from being
525 SDValue getStackArgumentTokenFactor(SDValue Chain);
527 SDValue getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst, SDValue Src,
528 SDValue Size, unsigned Align, bool AlwaysInline,
529 const Value *DstSV, uint64_t DstSVOff,
530 const Value *SrcSV, uint64_t SrcSVOff);
532 SDValue getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst, SDValue Src,
533 SDValue Size, unsigned Align,
534 const Value *DstSV, uint64_t DstOSVff,
535 const Value *SrcSV, uint64_t SrcSVOff);
537 SDValue getMemset(SDValue Chain, DebugLoc dl, SDValue Dst, SDValue Src,
538 SDValue Size, unsigned Align,
539 const Value *DstSV, uint64_t DstSVOff);
541 /// getSetCC - Helper function to make it easier to build SetCC's if you just
542 /// have an ISD::CondCode instead of an SDValue.
544 SDValue getSetCC(DebugLoc DL, EVT VT, SDValue LHS, SDValue RHS,
545 ISD::CondCode Cond) {
546 return getNode(ISD::SETCC, DL, VT, LHS, RHS, getCondCode(Cond));
549 /// getVSetCC - Helper function to make it easier to build VSetCC's nodes
550 /// if you just have an ISD::CondCode instead of an SDValue.
552 SDValue getVSetCC(DebugLoc DL, EVT VT, SDValue LHS, SDValue RHS,
553 ISD::CondCode Cond) {
554 return getNode(ISD::VSETCC, DL, VT, LHS, RHS, getCondCode(Cond));
557 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
558 /// just have an ISD::CondCode instead of an SDValue.
560 SDValue getSelectCC(DebugLoc DL, SDValue LHS, SDValue RHS,
561 SDValue True, SDValue False, ISD::CondCode Cond) {
562 return getNode(ISD::SELECT_CC, DL, True.getValueType(),
563 LHS, RHS, True, False, getCondCode(Cond));
566 /// getVAArg - VAArg produces a result and token chain, and takes a pointer
567 /// and a source value as input.
568 SDValue getVAArg(EVT VT, DebugLoc dl, SDValue Chain, SDValue Ptr,
571 /// getAtomic - Gets a node for an atomic op, produces result and chain and
573 SDValue getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, SDValue Chain,
574 SDValue Ptr, SDValue Cmp, SDValue Swp, const Value* PtrVal,
575 unsigned Alignment=0);
576 SDValue getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, SDValue Chain,
577 SDValue Ptr, SDValue Cmp, SDValue Swp,
578 MachineMemOperand *MMO);
580 /// getAtomic - Gets a node for an atomic op, produces result and chain and
581 /// takes 2 operands.
582 SDValue getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, SDValue Chain,
583 SDValue Ptr, SDValue Val, const Value* PtrVal,
584 unsigned Alignment = 0);
585 SDValue getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, SDValue Chain,
586 SDValue Ptr, SDValue Val,
587 MachineMemOperand *MMO);
589 /// getMemIntrinsicNode - Creates a MemIntrinsicNode that may produce a
590 /// result and takes a list of operands. Opcode may be INTRINSIC_VOID,
591 /// INTRINSIC_W_CHAIN, or a target-specific opcode with a value not
592 /// less than FIRST_TARGET_MEMORY_OPCODE.
593 SDValue getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
594 const EVT *VTs, unsigned NumVTs,
595 const SDValue *Ops, unsigned NumOps,
596 EVT MemVT, const Value *srcValue, int SVOff,
597 unsigned Align = 0, bool Vol = false,
598 bool ReadMem = true, bool WriteMem = true);
600 SDValue getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
601 const SDValue *Ops, unsigned NumOps,
602 EVT MemVT, const Value *srcValue, int SVOff,
603 unsigned Align = 0, bool Vol = false,
604 bool ReadMem = true, bool WriteMem = true);
606 SDValue getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
607 const SDValue *Ops, unsigned NumOps,
608 EVT MemVT, MachineMemOperand *MMO);
610 /// getMergeValues - Create a MERGE_VALUES node from the given operands.
611 SDValue getMergeValues(const SDValue *Ops, unsigned NumOps, DebugLoc dl);
613 /// getLoad - Loads are not normal binary operators: their result type is not
614 /// determined by their operands, and they produce a value AND a token chain.
616 SDValue getLoad(EVT VT, DebugLoc dl, SDValue Chain, SDValue Ptr,
617 const Value *SV, int SVOffset, bool isVolatile=false,
618 unsigned Alignment=0);
619 SDValue getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT,
620 SDValue Chain, SDValue Ptr, const Value *SV,
621 int SVOffset, EVT MemVT, bool isVolatile=false,
622 unsigned Alignment=0);
623 SDValue getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
624 SDValue Offset, ISD::MemIndexedMode AM);
625 SDValue getLoad(ISD::MemIndexedMode AM, DebugLoc dl, ISD::LoadExtType ExtType,
626 EVT VT, SDValue Chain, SDValue Ptr, SDValue Offset,
627 const Value *SV, int SVOffset, EVT MemVT,
628 bool isVolatile=false, unsigned Alignment=0);
629 SDValue getLoad(ISD::MemIndexedMode AM, DebugLoc dl, ISD::LoadExtType ExtType,
630 EVT VT, SDValue Chain, SDValue Ptr, SDValue Offset,
631 EVT MemVT, MachineMemOperand *MMO);
633 /// getStore - Helper function to build ISD::STORE nodes.
635 SDValue getStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
636 const Value *SV, int SVOffset, bool isVolatile=false,
637 unsigned Alignment=0);
638 SDValue getStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
639 MachineMemOperand *MMO);
640 SDValue getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
641 const Value *SV, int SVOffset, EVT TVT,
642 bool isVolatile=false, unsigned Alignment=0);
643 SDValue getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
644 EVT TVT, MachineMemOperand *MMO);
645 SDValue getIndexedStore(SDValue OrigStoe, DebugLoc dl, SDValue Base,
646 SDValue Offset, ISD::MemIndexedMode AM);
648 /// getSrcValue - Construct a node to track a Value* through the backend.
649 SDValue getSrcValue(const Value *v);
651 /// getShiftAmountOperand - Return the specified value casted to
652 /// the target's desired shift amount type.
653 SDValue getShiftAmountOperand(SDValue Op);
655 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
656 /// specified operands. If the resultant node already exists in the DAG,
657 /// this does not modify the specified node, instead it returns the node that
658 /// already exists. If the resultant node does not exist in the DAG, the
659 /// input node is returned. As a degenerate case, if you specify the same
660 /// input operands as the node already has, the input node is returned.
661 SDValue UpdateNodeOperands(SDValue N, SDValue Op);
662 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2);
663 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
665 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
666 SDValue Op3, SDValue Op4);
667 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
668 SDValue Op3, SDValue Op4, SDValue Op5);
669 SDValue UpdateNodeOperands(SDValue N,
670 const SDValue *Ops, unsigned NumOps);
672 /// SelectNodeTo - These are used for target selectors to *mutate* the
673 /// specified node to have the specified return type, Target opcode, and
674 /// operands. Note that target opcodes are stored as
675 /// ~TargetOpcode in the node opcode field. The resultant node is returned.
676 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT);
677 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT, SDValue Op1);
678 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
679 SDValue Op1, SDValue Op2);
680 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
681 SDValue Op1, SDValue Op2, SDValue Op3);
682 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
683 const SDValue *Ops, unsigned NumOps);
684 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1, EVT VT2);
685 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
686 EVT VT2, const SDValue *Ops, unsigned NumOps);
687 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
688 EVT VT2, EVT VT3, const SDValue *Ops, unsigned NumOps);
689 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1,
690 EVT VT2, EVT VT3, EVT VT4, const SDValue *Ops,
692 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
693 EVT VT2, SDValue Op1);
694 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
695 EVT VT2, SDValue Op1, SDValue Op2);
696 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
697 EVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
698 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
699 EVT VT2, EVT VT3, SDValue Op1, SDValue Op2, SDValue Op3);
700 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, SDVTList VTs,
701 const SDValue *Ops, unsigned NumOps);
703 /// MorphNodeTo - These *mutate* the specified node to have the specified
704 /// return type, opcode, and operands.
705 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT);
706 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT, SDValue Op1);
707 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT,
708 SDValue Op1, SDValue Op2);
709 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT,
710 SDValue Op1, SDValue Op2, SDValue Op3);
711 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT,
712 const SDValue *Ops, unsigned NumOps);
713 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT1, EVT VT2);
714 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT1,
715 EVT VT2, const SDValue *Ops, unsigned NumOps);
716 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT1,
717 EVT VT2, EVT VT3, const SDValue *Ops, unsigned NumOps);
718 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT1,
719 EVT VT2, SDValue Op1);
720 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT1,
721 EVT VT2, SDValue Op1, SDValue Op2);
722 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT1,
723 EVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
724 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs,
725 const SDValue *Ops, unsigned NumOps);
727 /// getMachineNode - These are used for target selectors to create a new node
728 /// with specified return type(s), MachineInstr opcode, and operands.
730 /// Note that getMachineNode returns the resultant node. If there is already
731 /// a node of the specified opcode and operands, it returns that node instead
732 /// of the current one.
733 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT);
734 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
736 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
737 SDValue Op1, SDValue Op2);
738 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
739 SDValue Op1, SDValue Op2, SDValue Op3);
740 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
741 const SDValue *Ops, unsigned NumOps);
742 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2);
743 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
745 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
746 EVT VT2, SDValue Op1, SDValue Op2);
747 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
748 EVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
749 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
750 const SDValue *Ops, unsigned NumOps);
751 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
752 EVT VT3, SDValue Op1, SDValue Op2);
753 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
754 EVT VT3, SDValue Op1, SDValue Op2, SDValue Op3);
755 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
756 EVT VT3, const SDValue *Ops, unsigned NumOps);
757 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
758 EVT VT3, EVT VT4, const SDValue *Ops, unsigned NumOps);
759 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl,
760 const std::vector<EVT> &ResultTys, const SDValue *Ops,
762 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, SDVTList VTs,
763 const SDValue *Ops, unsigned NumOps);
765 /// getTargetExtractSubreg - A convenience function for creating
766 /// TargetInstrInfo::EXTRACT_SUBREG nodes.
767 SDValue getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
770 /// getTargetInsertSubreg - A convenience function for creating
771 /// TargetInstrInfo::INSERT_SUBREG nodes.
772 SDValue getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT,
773 SDValue Operand, SDValue Subreg);
775 /// getNodeIfExists - Get the specified node if it's already available, or
776 /// else return NULL.
777 SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTs,
778 const SDValue *Ops, unsigned NumOps);
780 /// DAGUpdateListener - Clients of various APIs that cause global effects on
781 /// the DAG can optionally implement this interface. This allows the clients
782 /// to handle the various sorts of updates that happen.
783 class DAGUpdateListener {
785 virtual ~DAGUpdateListener();
787 /// NodeDeleted - The node N that was deleted and, if E is not null, an
788 /// equivalent node E that replaced it.
789 virtual void NodeDeleted(SDNode *N, SDNode *E) = 0;
791 /// NodeUpdated - The node N that was updated.
792 virtual void NodeUpdated(SDNode *N) = 0;
795 /// RemoveDeadNode - Remove the specified node from the system. If any of its
796 /// operands then becomes dead, remove them as well. Inform UpdateListener
797 /// for each node deleted.
798 void RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener = 0);
800 /// RemoveDeadNodes - This method deletes the unreachable nodes in the
801 /// given list, and any nodes that become unreachable as a result.
802 void RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
803 DAGUpdateListener *UpdateListener = 0);
805 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
806 /// This can cause recursive merging of nodes in the DAG. Use the first
807 /// version if 'From' is known to have a single result, use the second
808 /// if you have two nodes with identical results (or if 'To' has a superset
809 /// of the results of 'From'), use the third otherwise.
811 /// These methods all take an optional UpdateListener, which (if not null) is
812 /// informed about nodes that are deleted and modified due to recursive
813 /// changes in the dag.
815 /// These functions only replace all existing uses. It's possible that as
816 /// these replacements are being performed, CSE may cause the From node
817 /// to be given new uses. These new uses of From are left in place, and
818 /// not automatically transfered to To.
820 void ReplaceAllUsesWith(SDValue From, SDValue Op,
821 DAGUpdateListener *UpdateListener = 0);
822 void ReplaceAllUsesWith(SDNode *From, SDNode *To,
823 DAGUpdateListener *UpdateListener = 0);
824 void ReplaceAllUsesWith(SDNode *From, const SDValue *To,
825 DAGUpdateListener *UpdateListener = 0);
827 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
828 /// uses of other values produced by From.Val alone.
829 void ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
830 DAGUpdateListener *UpdateListener = 0);
832 /// ReplaceAllUsesOfValuesWith - Like ReplaceAllUsesOfValueWith, but
833 /// for multiple values at once. This correctly handles the case where
834 /// there is an overlap between the From values and the To values.
835 void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To,
837 DAGUpdateListener *UpdateListener = 0);
839 /// AssignTopologicalOrder - Topological-sort the AllNodes list and a
840 /// assign a unique node id for each node in the DAG based on their
841 /// topological order. Returns the number of nodes.
842 unsigned AssignTopologicalOrder();
844 /// RepositionNode - Move node N in the AllNodes list to be immediately
845 /// before the given iterator Position. This may be used to update the
846 /// topological ordering when the list of nodes is modified.
847 void RepositionNode(allnodes_iterator Position, SDNode *N) {
848 AllNodes.insert(Position, AllNodes.remove(N));
851 /// isCommutativeBinOp - Returns true if the opcode is a commutative binary
853 static bool isCommutativeBinOp(unsigned Opcode) {
854 // FIXME: This should get its info from the td file, so that we can include
871 case ISD::ADDE: return true;
872 default: return false;
878 /// CreateStackTemporary - Create a stack temporary, suitable for holding the
879 /// specified value type. If minAlign is specified, the slot size will have
880 /// at least that alignment.
881 SDValue CreateStackTemporary(EVT VT, unsigned minAlign = 1);
883 /// CreateStackTemporary - Create a stack temporary suitable for holding
884 /// either of the specified value types.
885 SDValue CreateStackTemporary(EVT VT1, EVT VT2);
887 /// FoldConstantArithmetic -
888 SDValue FoldConstantArithmetic(unsigned Opcode,
890 ConstantSDNode *Cst1,
891 ConstantSDNode *Cst2);
893 /// FoldSetCC - Constant fold a setcc to true or false.
894 SDValue FoldSetCC(EVT VT, SDValue N1,
895 SDValue N2, ISD::CondCode Cond, DebugLoc dl);
897 /// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
898 /// use this predicate to simplify operations downstream.
899 bool SignBitIsZero(SDValue Op, unsigned Depth = 0) const;
901 /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We
902 /// use this predicate to simplify operations downstream. Op and Mask are
903 /// known to be the same type.
904 bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth = 0)
907 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
908 /// known to be either zero or one and return them in the KnownZero/KnownOne
909 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
910 /// processing. Targets can implement the computeMaskedBitsForTargetNode
911 /// method in the TargetLowering class to allow target nodes to be understood.
912 void ComputeMaskedBits(SDValue Op, const APInt &Mask, APInt &KnownZero,
913 APInt &KnownOne, unsigned Depth = 0) const;
915 /// ComputeNumSignBits - Return the number of times the sign bit of the
916 /// register is replicated into the other bits. We know that at least 1 bit
917 /// is always equal to the sign bit (itself), but other cases can give us
918 /// information. For example, immediately after an "SRA X, 2", we know that
919 /// the top 3 bits are all equal to each other, so we return 3. Targets can
920 /// implement the ComputeNumSignBitsForTarget method in the TargetLowering
921 /// class to allow target nodes to be understood.
922 unsigned ComputeNumSignBits(SDValue Op, unsigned Depth = 0) const;
924 /// isKnownNeverNan - Test whether the given SDValue is known to never be NaN.
925 bool isKnownNeverNaN(SDValue Op) const;
927 /// isVerifiedDebugInfoDesc - Returns true if the specified SDValue has
928 /// been verified as a debug information descriptor.
929 bool isVerifiedDebugInfoDesc(SDValue Op) const;
931 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
932 /// element of the result of the vector shuffle.
933 SDValue getShuffleScalarElt(const ShuffleVectorSDNode *N, unsigned Idx);
935 /// UnrollVectorOp - Utility function used by legalize and lowering to
936 /// "unroll" a vector operation by splitting out the scalars and operating
937 /// on each element individually. If the ResNE is 0, fully unroll the vector
938 /// op. If ResNE is less than the width of the vector op, unroll up to ResNE.
939 /// If the ResNE is greater than the width of the vector op, unroll the
940 /// vector op and fill the end of the resulting vector with UNDEFS.
941 SDValue UnrollVectorOp(SDNode *N, unsigned ResNE = 0);
943 /// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
944 /// location that is 'Dist' units away from the location that the 'Base' load
946 bool isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
947 unsigned Bytes, int Dist) const;
949 /// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
950 /// it cannot be inferred.
951 unsigned InferPtrAlignment(SDValue Ptr) const;
954 bool RemoveNodeFromCSEMaps(SDNode *N);
955 void AddModifiedNodeToCSEMaps(SDNode *N, DAGUpdateListener *UpdateListener);
956 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op, void *&InsertPos);
957 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op1, SDValue Op2,
959 SDNode *FindModifiedNodeSlot(SDNode *N, const SDValue *Ops, unsigned NumOps,
962 void DeleteNodeNotInCSEMaps(SDNode *N);
963 void DeallocateNode(SDNode *N);
965 unsigned getEVTAlignment(EVT MemoryVT) const;
967 void allnodes_clear();
969 /// VTList - List of non-single value types.
970 std::vector<SDVTList> VTList;
972 /// CondCodeNodes - Maps to auto-CSE operations.
973 std::vector<CondCodeSDNode*> CondCodeNodes;
975 std::vector<SDNode*> ValueTypeNodes;
976 std::map<EVT, SDNode*, EVT::compareRawBits> ExtendedValueTypeNodes;
977 StringMap<SDNode*> ExternalSymbols;
979 std::map<std::pair<std::string, unsigned char>,SDNode*> TargetExternalSymbols;
982 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
983 typedef SelectionDAG::allnodes_iterator nodes_iterator;
984 static nodes_iterator nodes_begin(SelectionDAG *G) {
985 return G->allnodes_begin();
987 static nodes_iterator nodes_end(SelectionDAG *G) {
988 return G->allnodes_end();
992 } // end namespace llvm