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"
32 class FunctionLoweringInfo;
33 class MachineConstantPoolValue;
34 class MachineFunction;
40 template<> struct ilist_traits<SDNode> : public ilist_default_traits<SDNode> {
42 mutable ilist_half_node<SDNode> Sentinel;
44 SDNode *createSentinel() const {
45 return static_cast<SDNode*>(&Sentinel);
47 static void destroySentinel(SDNode *) {}
49 SDNode *provideInitialHead() const { return createSentinel(); }
50 SDNode *ensureHead(SDNode*) const { return createSentinel(); }
51 static void noteHead(SDNode*, SDNode*) {}
53 static void deleteNode(SDNode *) {
54 assert(0 && "ilist_traits<SDNode> shouldn't see a deleteNode call!");
57 static void createNode(const SDNode &);
60 /// SDDbgInfo - Keeps track of dbg_value information through SDISel. We do
61 /// not build SDNodes for these so as not to perturb the generated code;
62 /// instead the info is kept off to the side in this structure. Each SDNode may
63 /// have one or more associated dbg_value entries. This information is kept in
66 SmallVector<SDDbgValue*, 32> DbgValues;
67 DenseMap<const SDNode*, SmallVector<SDDbgValue*, 2> > DbgValMap;
69 void operator=(const SDDbgInfo&); // Do not implement.
70 SDDbgInfo(const SDDbgInfo&); // Do not implement.
74 void add(SDDbgValue *V, const SDNode *Node = 0) {
76 DbgValMap[Node].push_back(V);
77 DbgValues.push_back(V);
86 return DbgValues.empty();
89 SmallVector<SDDbgValue*,2> &getSDDbgValues(const SDNode *Node) {
90 return DbgValMap[Node];
93 typedef SmallVector<SDDbgValue*,32>::iterator DbgIterator;
94 DbgIterator DbgBegin() { return DbgValues.begin(); }
95 DbgIterator DbgEnd() { return DbgValues.end(); }
99 Unrestricted, // Combine may create illegal operations and illegal types.
100 NoIllegalTypes, // Combine may create illegal operations but no illegal types.
101 NoIllegalOperations // Combine may only create legal operations and types.
105 void checkForCycles(const SDNode *N);
106 void checkForCycles(const SelectionDAG *DAG);
108 /// SelectionDAG class - This is used to represent a portion of an LLVM function
109 /// in a low-level Data Dependence DAG representation suitable for instruction
110 /// selection. This DAG is constructed as the first step of instruction
111 /// selection in order to allow implementation of machine specific optimizations
112 /// and code simplifications.
114 /// The representation used by the SelectionDAG is a target-independent
115 /// representation, which has some similarities to the GCC RTL representation,
116 /// but is significantly more simple, powerful, and is a graph form instead of a
122 FunctionLoweringInfo &FLI;
123 LLVMContext *Context;
125 /// EntryNode - The starting token.
128 /// Root - The root of the entire DAG.
131 /// AllNodes - A linked list of nodes in the current DAG.
132 ilist<SDNode> AllNodes;
134 /// NodeAllocatorType - The AllocatorType for allocating SDNodes. We use
135 /// pool allocation with recycling.
136 typedef RecyclingAllocator<BumpPtrAllocator, SDNode, sizeof(LargestSDNode),
137 AlignOf<MostAlignedSDNode>::Alignment>
140 /// NodeAllocator - Pool allocation for nodes.
141 NodeAllocatorType NodeAllocator;
143 /// CSEMap - This structure is used to memoize nodes, automatically performing
144 /// CSE with existing nodes when a duplicate is requested.
145 FoldingSet<SDNode> CSEMap;
147 /// OperandAllocator - Pool allocation for machine-opcode SDNode operands.
148 BumpPtrAllocator OperandAllocator;
150 /// Allocator - Pool allocation for misc. objects that are created once per
152 BumpPtrAllocator Allocator;
154 /// SDNodeOrdering - The ordering of the SDNodes. It roughly corresponds to
155 /// the ordering of the original LLVM instructions.
156 SDNodeOrdering *Ordering;
158 /// DbgInfo - Tracks dbg_value information through SDISel.
161 /// VerifyNode - Sanity check the given node. Aborts if it is invalid.
162 void VerifyNode(SDNode *N);
164 /// setGraphColorHelper - Implementation of setSubgraphColor.
165 /// Return whether we had to truncate the search.
167 bool setSubgraphColorHelper(SDNode *N, const char *Color,
168 DenseSet<SDNode *> &visited,
169 int level, bool &printed);
171 void operator=(const SelectionDAG&); // Do not implement.
172 SelectionDAG(const SelectionDAG&); // Do not implement.
175 SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli);
178 /// init - Prepare this SelectionDAG to process code in the given
181 void init(MachineFunction &mf);
183 /// clear - Clear state and free memory necessary to make this
184 /// SelectionDAG ready to process a new block.
188 MachineFunction &getMachineFunction() const { return *MF; }
189 const TargetMachine &getTarget() const;
190 TargetLowering &getTargetLoweringInfo() const { return TLI; }
191 FunctionLoweringInfo &getFunctionLoweringInfo() const { return FLI; }
192 LLVMContext *getContext() const {return Context; }
194 /// viewGraph - Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
196 void viewGraph(const std::string &Title);
200 std::map<const SDNode *, std::string> NodeGraphAttrs;
203 /// clearGraphAttrs - Clear all previously defined node graph attributes.
204 /// Intended to be used from a debugging tool (eg. gdb).
205 void clearGraphAttrs();
207 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
209 void setGraphAttrs(const SDNode *N, const char *Attrs);
211 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
212 /// Used from getNodeAttributes.
213 const std::string getGraphAttrs(const SDNode *N) const;
215 /// setGraphColor - Convenience for setting node color attribute.
217 void setGraphColor(const SDNode *N, const char *Color);
219 /// setGraphColor - Convenience for setting subgraph color attribute.
221 void setSubgraphColor(SDNode *N, const char *Color);
223 typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
224 allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
225 allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
226 typedef ilist<SDNode>::iterator allnodes_iterator;
227 allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
228 allnodes_iterator allnodes_end() { return AllNodes.end(); }
229 ilist<SDNode>::size_type allnodes_size() const {
230 return AllNodes.size();
233 /// getRoot - Return the root tag of the SelectionDAG.
235 const SDValue &getRoot() const { return Root; }
237 /// getEntryNode - Return the token chain corresponding to the entry of the
239 SDValue getEntryNode() const {
240 return SDValue(const_cast<SDNode *>(&EntryNode), 0);
243 /// setRoot - Set the current root tag of the SelectionDAG.
245 const SDValue &setRoot(SDValue N) {
246 assert((!N.getNode() || N.getValueType() == MVT::Other) &&
247 "DAG root value is not a chain!");
249 checkForCycles(N.getNode());
252 checkForCycles(this);
256 /// Combine - This iterates over the nodes in the SelectionDAG, folding
257 /// certain types of nodes together, or eliminating superfluous nodes. The
258 /// Level argument controls whether Combine is allowed to produce nodes and
259 /// types that are illegal on the target.
260 void Combine(CombineLevel Level, AliasAnalysis &AA,
261 CodeGenOpt::Level OptLevel);
263 /// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that
264 /// only uses types natively supported by the target. Returns "true" if it
265 /// made any changes.
267 /// Note that this is an involved process that may invalidate pointers into
269 bool LegalizeTypes();
271 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
272 /// compatible with the target instruction selector, as indicated by the
273 /// TargetLowering object.
275 /// Note that this is an involved process that may invalidate pointers into
277 void Legalize(CodeGenOpt::Level OptLevel);
279 /// LegalizeVectors - This transforms the SelectionDAG into a SelectionDAG
280 /// that only uses vector math operations supported by the target. This is
281 /// necessary as a separate step from Legalize because unrolling a vector
282 /// operation can introduce illegal types, which requires running
283 /// LegalizeTypes again.
285 /// This returns true if it made any changes; in that case, LegalizeTypes
286 /// is called again before Legalize.
288 /// Note that this is an involved process that may invalidate pointers into
290 bool LegalizeVectors();
292 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
294 void RemoveDeadNodes();
296 /// DeleteNode - Remove the specified node from the system. This node must
297 /// have no referrers.
298 void DeleteNode(SDNode *N);
300 /// getVTList - Return an SDVTList that represents the list of values
302 SDVTList getVTList(EVT VT);
303 SDVTList getVTList(EVT VT1, EVT VT2);
304 SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3);
305 SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4);
306 SDVTList getVTList(const EVT *VTs, unsigned NumVTs);
308 //===--------------------------------------------------------------------===//
309 // Node creation methods.
311 SDValue getConstant(uint64_t Val, EVT VT, bool isTarget = false);
312 SDValue getConstant(const APInt &Val, EVT VT, bool isTarget = false);
313 SDValue getConstant(const ConstantInt &Val, EVT VT, bool isTarget = false);
314 SDValue getIntPtrConstant(uint64_t Val, bool isTarget = false);
315 SDValue getTargetConstant(uint64_t Val, EVT VT) {
316 return getConstant(Val, VT, true);
318 SDValue getTargetConstant(const APInt &Val, EVT VT) {
319 return getConstant(Val, VT, true);
321 SDValue getTargetConstant(const ConstantInt &Val, EVT VT) {
322 return getConstant(Val, VT, true);
324 SDValue getConstantFP(double Val, EVT VT, bool isTarget = false);
325 SDValue getConstantFP(const APFloat& Val, EVT VT, bool isTarget = false);
326 SDValue getConstantFP(const ConstantFP &CF, EVT VT, bool isTarget = false);
327 SDValue getTargetConstantFP(double Val, EVT VT) {
328 return getConstantFP(Val, VT, true);
330 SDValue getTargetConstantFP(const APFloat& Val, EVT VT) {
331 return getConstantFP(Val, VT, true);
333 SDValue getTargetConstantFP(const ConstantFP &Val, EVT VT) {
334 return getConstantFP(Val, VT, true);
336 SDValue getGlobalAddress(const GlobalValue *GV, EVT VT,
337 int64_t offset = 0, bool isTargetGA = false,
338 unsigned char TargetFlags = 0);
339 SDValue getTargetGlobalAddress(const GlobalValue *GV, EVT VT,
341 unsigned char TargetFlags = 0) {
342 return getGlobalAddress(GV, VT, offset, true, TargetFlags);
344 SDValue getFrameIndex(int FI, EVT VT, bool isTarget = false);
345 SDValue getTargetFrameIndex(int FI, EVT VT) {
346 return getFrameIndex(FI, VT, true);
348 SDValue getJumpTable(int JTI, EVT VT, bool isTarget = false,
349 unsigned char TargetFlags = 0);
350 SDValue getTargetJumpTable(int JTI, EVT VT, unsigned char TargetFlags = 0) {
351 return getJumpTable(JTI, VT, true, TargetFlags);
353 SDValue getConstantPool(Constant *C, EVT VT,
354 unsigned Align = 0, int Offs = 0, bool isT=false,
355 unsigned char TargetFlags = 0);
356 SDValue getTargetConstantPool(Constant *C, EVT VT,
357 unsigned Align = 0, int Offset = 0,
358 unsigned char TargetFlags = 0) {
359 return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
361 SDValue getConstantPool(MachineConstantPoolValue *C, EVT VT,
362 unsigned Align = 0, int Offs = 0, bool isT=false,
363 unsigned char TargetFlags = 0);
364 SDValue getTargetConstantPool(MachineConstantPoolValue *C,
365 EVT VT, unsigned Align = 0,
366 int Offset = 0, unsigned char TargetFlags=0) {
367 return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
369 // When generating a branch to a BB, we don't in general know enough
370 // to provide debug info for the BB at that time, so keep this one around.
371 SDValue getBasicBlock(MachineBasicBlock *MBB);
372 SDValue getBasicBlock(MachineBasicBlock *MBB, DebugLoc dl);
373 SDValue getExternalSymbol(const char *Sym, EVT VT);
374 SDValue getExternalSymbol(const char *Sym, DebugLoc dl, EVT VT);
375 SDValue getTargetExternalSymbol(const char *Sym, EVT VT,
376 unsigned char TargetFlags = 0);
377 SDValue getValueType(EVT);
378 SDValue getRegister(unsigned Reg, EVT VT);
379 SDValue getEHLabel(DebugLoc dl, SDValue Root, MCSymbol *Label);
380 SDValue getBlockAddress(BlockAddress *BA, EVT VT,
381 bool isTarget = false, unsigned char TargetFlags = 0);
383 SDValue getCopyToReg(SDValue Chain, DebugLoc dl, unsigned Reg, SDValue N) {
384 return getNode(ISD::CopyToReg, dl, MVT::Other, Chain,
385 getRegister(Reg, N.getValueType()), N);
388 // This version of the getCopyToReg method takes an extra operand, which
389 // indicates that there is potentially an incoming flag value (if Flag is not
390 // null) and that there should be a flag result.
391 SDValue getCopyToReg(SDValue Chain, DebugLoc dl, unsigned Reg, SDValue N,
393 SDVTList VTs = getVTList(MVT::Other, MVT::Flag);
394 SDValue Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Flag };
395 return getNode(ISD::CopyToReg, dl, VTs, Ops, Flag.getNode() ? 4 : 3);
398 // Similar to last getCopyToReg() except parameter Reg is a SDValue
399 SDValue getCopyToReg(SDValue Chain, DebugLoc dl, SDValue Reg, SDValue N,
401 SDVTList VTs = getVTList(MVT::Other, MVT::Flag);
402 SDValue Ops[] = { Chain, Reg, N, Flag };
403 return getNode(ISD::CopyToReg, dl, VTs, Ops, Flag.getNode() ? 4 : 3);
406 SDValue getCopyFromReg(SDValue Chain, DebugLoc dl, unsigned Reg, EVT VT) {
407 SDVTList VTs = getVTList(VT, MVT::Other);
408 SDValue Ops[] = { Chain, getRegister(Reg, VT) };
409 return getNode(ISD::CopyFromReg, dl, VTs, Ops, 2);
412 // This version of the getCopyFromReg method takes an extra operand, which
413 // indicates that there is potentially an incoming flag value (if Flag is not
414 // null) and that there should be a flag result.
415 SDValue getCopyFromReg(SDValue Chain, DebugLoc dl, unsigned Reg, EVT VT,
417 SDVTList VTs = getVTList(VT, MVT::Other, MVT::Flag);
418 SDValue Ops[] = { Chain, getRegister(Reg, VT), Flag };
419 return getNode(ISD::CopyFromReg, dl, VTs, Ops, Flag.getNode() ? 3 : 2);
422 SDValue getCondCode(ISD::CondCode Cond);
424 /// Returns the ConvertRndSat Note: Avoid using this node because it may
425 /// disappear in the future and most targets don't support it.
426 SDValue getConvertRndSat(EVT VT, DebugLoc dl, SDValue Val, SDValue DTy,
428 SDValue Rnd, SDValue Sat, ISD::CvtCode Code);
430 /// getVectorShuffle - Return an ISD::VECTOR_SHUFFLE node. The number of
431 /// elements in VT, which must be a vector type, must match the number of
432 /// mask elements NumElts. A integer mask element equal to -1 is treated as
434 SDValue getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1, SDValue N2,
435 const int *MaskElts);
437 /// getSExtOrTrunc - Convert Op, which must be of integer type, to the
438 /// integer type VT, by either sign-extending or truncating it.
439 SDValue getSExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT);
441 /// getZExtOrTrunc - Convert Op, which must be of integer type, to the
442 /// integer type VT, by either zero-extending or truncating it.
443 SDValue getZExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT);
445 /// getZeroExtendInReg - Return the expression required to zero extend the Op
446 /// value assuming it was the smaller SrcTy value.
447 SDValue getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT SrcTy);
449 /// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
450 SDValue getNOT(DebugLoc DL, SDValue Val, EVT VT);
452 /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
453 /// a flag result (to ensure it's not CSE'd). CALLSEQ_START does not have a
455 SDValue getCALLSEQ_START(SDValue Chain, SDValue Op) {
456 SDVTList VTs = getVTList(MVT::Other, MVT::Flag);
457 SDValue Ops[] = { Chain, Op };
458 return getNode(ISD::CALLSEQ_START, DebugLoc(), VTs, Ops, 2);
461 /// getCALLSEQ_END - Return a new CALLSEQ_END node, which always must have a
462 /// flag result (to ensure it's not CSE'd). CALLSEQ_END does not have
463 /// a useful DebugLoc.
464 SDValue getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2,
466 SDVTList NodeTys = getVTList(MVT::Other, MVT::Flag);
467 SmallVector<SDValue, 4> Ops;
468 Ops.push_back(Chain);
471 Ops.push_back(InFlag);
472 return getNode(ISD::CALLSEQ_END, DebugLoc(), NodeTys, &Ops[0],
473 (unsigned)Ops.size() - (InFlag.getNode() == 0 ? 1 : 0));
476 /// getUNDEF - Return an UNDEF node. UNDEF does not have a useful DebugLoc.
477 SDValue getUNDEF(EVT VT) {
478 return getNode(ISD::UNDEF, DebugLoc(), VT);
481 /// getGLOBAL_OFFSET_TABLE - Return a GLOBAL_OFFSET_TABLE node. This does
482 /// not have a useful DebugLoc.
483 SDValue getGLOBAL_OFFSET_TABLE(EVT VT) {
484 return getNode(ISD::GLOBAL_OFFSET_TABLE, DebugLoc(), VT);
487 /// getNode - Gets or creates the specified node.
489 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT);
490 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT, SDValue N);
491 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT, SDValue N1, SDValue N2);
492 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
493 SDValue N1, SDValue N2, SDValue N3);
494 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
495 SDValue N1, SDValue N2, SDValue N3, SDValue N4);
496 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
497 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
499 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
500 const SDUse *Ops, unsigned NumOps);
501 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
502 const SDValue *Ops, unsigned NumOps);
503 SDValue getNode(unsigned Opcode, DebugLoc DL,
504 const std::vector<EVT> &ResultTys,
505 const SDValue *Ops, unsigned NumOps);
506 SDValue getNode(unsigned Opcode, DebugLoc DL, const EVT *VTs, unsigned NumVTs,
507 const SDValue *Ops, unsigned NumOps);
508 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
509 const SDValue *Ops, unsigned NumOps);
510 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs);
511 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs, SDValue N);
512 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
513 SDValue N1, SDValue N2);
514 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
515 SDValue N1, SDValue N2, SDValue N3);
516 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
517 SDValue N1, SDValue N2, SDValue N3, SDValue N4);
518 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
519 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
522 /// getStackArgumentTokenFactor - Compute a TokenFactor to force all
523 /// the incoming stack arguments to be loaded from the stack. This is
524 /// used in tail call lowering to protect stack arguments from being
526 SDValue getStackArgumentTokenFactor(SDValue Chain);
528 SDValue getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst, SDValue Src,
529 SDValue Size, unsigned Align, bool isVol, bool AlwaysInline,
530 const Value *DstSV, uint64_t DstSVOff,
531 const Value *SrcSV, uint64_t SrcSVOff);
533 SDValue getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst, SDValue Src,
534 SDValue Size, unsigned Align, bool isVol,
535 const Value *DstSV, uint64_t DstOSVff,
536 const Value *SrcSV, uint64_t SrcSVOff);
538 SDValue getMemset(SDValue Chain, DebugLoc dl, SDValue Dst, SDValue Src,
539 SDValue Size, unsigned Align, bool isVol,
540 const Value *DstSV, uint64_t DstSVOff);
542 /// getSetCC - Helper function to make it easier to build SetCC's if you just
543 /// have an ISD::CondCode instead of an SDValue.
545 SDValue getSetCC(DebugLoc DL, EVT VT, SDValue LHS, SDValue RHS,
546 ISD::CondCode Cond) {
547 return getNode(ISD::SETCC, DL, VT, LHS, RHS, getCondCode(Cond));
550 /// getVSetCC - Helper function to make it easier to build VSetCC's nodes
551 /// if you just have an ISD::CondCode instead of an SDValue.
553 SDValue getVSetCC(DebugLoc DL, EVT VT, SDValue LHS, SDValue RHS,
554 ISD::CondCode Cond) {
555 return getNode(ISD::VSETCC, DL, VT, LHS, RHS, getCondCode(Cond));
558 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
559 /// just have an ISD::CondCode instead of an SDValue.
561 SDValue getSelectCC(DebugLoc DL, SDValue LHS, SDValue RHS,
562 SDValue True, SDValue False, ISD::CondCode Cond) {
563 return getNode(ISD::SELECT_CC, DL, True.getValueType(),
564 LHS, RHS, True, False, getCondCode(Cond));
567 /// getVAArg - VAArg produces a result and token chain, and takes a pointer
568 /// and a source value as input.
569 SDValue getVAArg(EVT VT, DebugLoc dl, SDValue Chain, SDValue Ptr,
572 /// getAtomic - Gets a node for an atomic op, produces result and chain and
574 SDValue getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, SDValue Chain,
575 SDValue Ptr, SDValue Cmp, SDValue Swp, const Value* PtrVal,
576 unsigned Alignment=0);
577 SDValue getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, SDValue Chain,
578 SDValue Ptr, SDValue Cmp, SDValue Swp,
579 MachineMemOperand *MMO);
581 /// getAtomic - Gets a node for an atomic op, produces result and chain and
582 /// takes 2 operands.
583 SDValue getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, SDValue Chain,
584 SDValue Ptr, SDValue Val, const Value* PtrVal,
585 unsigned Alignment = 0);
586 SDValue getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, SDValue Chain,
587 SDValue Ptr, SDValue Val,
588 MachineMemOperand *MMO);
590 /// getMemIntrinsicNode - Creates a MemIntrinsicNode that may produce a
591 /// result and takes a list of operands. Opcode may be INTRINSIC_VOID,
592 /// INTRINSIC_W_CHAIN, or a target-specific opcode with a value not
593 /// less than FIRST_TARGET_MEMORY_OPCODE.
594 SDValue getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
595 const EVT *VTs, unsigned NumVTs,
596 const SDValue *Ops, unsigned NumOps,
597 EVT MemVT, const Value *srcValue, int SVOff,
598 unsigned Align = 0, bool Vol = false,
599 bool ReadMem = true, bool WriteMem = true);
601 SDValue getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
602 const SDValue *Ops, unsigned NumOps,
603 EVT MemVT, const Value *srcValue, int SVOff,
604 unsigned Align = 0, bool Vol = false,
605 bool ReadMem = true, bool WriteMem = true);
607 SDValue getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
608 const SDValue *Ops, unsigned NumOps,
609 EVT MemVT, MachineMemOperand *MMO);
611 /// getMergeValues - Create a MERGE_VALUES node from the given operands.
612 SDValue getMergeValues(const SDValue *Ops, unsigned NumOps, DebugLoc dl);
614 /// getLoad - Loads are not normal binary operators: their result type is not
615 /// determined by their operands, and they produce a value AND a token chain.
617 SDValue getLoad(EVT VT, DebugLoc dl, SDValue Chain, SDValue Ptr,
618 const Value *SV, int SVOffset, bool isVolatile,
619 bool isNonTemporal, unsigned Alignment);
620 SDValue getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT,
621 SDValue Chain, SDValue Ptr, const Value *SV,
622 int SVOffset, EVT MemVT, bool isVolatile,
623 bool isNonTemporal, unsigned Alignment);
624 SDValue getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
625 SDValue Offset, ISD::MemIndexedMode AM);
626 SDValue getLoad(ISD::MemIndexedMode AM, DebugLoc dl, ISD::LoadExtType ExtType,
627 EVT VT, SDValue Chain, SDValue Ptr, SDValue Offset,
628 const Value *SV, int SVOffset, EVT MemVT,
629 bool isVolatile, bool isNonTemporal, unsigned Alignment);
630 SDValue getLoad(ISD::MemIndexedMode AM, DebugLoc dl, ISD::LoadExtType ExtType,
631 EVT VT, SDValue Chain, SDValue Ptr, SDValue Offset,
632 EVT MemVT, MachineMemOperand *MMO);
634 /// getStore - Helper function to build ISD::STORE nodes.
636 SDValue getStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
637 const Value *SV, int SVOffset, bool isVolatile,
638 bool isNonTemporal, unsigned Alignment);
639 SDValue getStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
640 MachineMemOperand *MMO);
641 SDValue getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
642 const Value *SV, int SVOffset, EVT TVT,
643 bool isNonTemporal, bool isVolatile,
645 SDValue getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
646 EVT TVT, MachineMemOperand *MMO);
647 SDValue getIndexedStore(SDValue OrigStoe, DebugLoc dl, SDValue Base,
648 SDValue Offset, ISD::MemIndexedMode AM);
650 /// getSrcValue - Construct a node to track a Value* through the backend.
651 SDValue getSrcValue(const Value *v);
653 /// getShiftAmountOperand - Return the specified value casted to
654 /// the target's desired shift amount type.
655 SDValue getShiftAmountOperand(SDValue Op);
657 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
658 /// specified operands. If the resultant node already exists in the DAG,
659 /// this does not modify the specified node, instead it returns the node that
660 /// already exists. If the resultant node does not exist in the DAG, the
661 /// input node is returned. As a degenerate case, if you specify the same
662 /// input operands as the node already has, the input node is returned.
663 SDValue UpdateNodeOperands(SDValue N, SDValue Op);
664 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2);
665 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
667 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
668 SDValue Op3, SDValue Op4);
669 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
670 SDValue Op3, SDValue Op4, SDValue Op5);
671 SDValue UpdateNodeOperands(SDValue N,
672 const SDValue *Ops, unsigned NumOps);
674 /// SelectNodeTo - These are used for target selectors to *mutate* the
675 /// specified node to have the specified return type, Target opcode, and
676 /// operands. Note that target opcodes are stored as
677 /// ~TargetOpcode in the node opcode field. The resultant node is returned.
678 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT);
679 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT, SDValue Op1);
680 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
681 SDValue Op1, SDValue Op2);
682 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
683 SDValue Op1, SDValue Op2, SDValue Op3);
684 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
685 const SDValue *Ops, unsigned NumOps);
686 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1, EVT VT2);
687 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
688 EVT VT2, const SDValue *Ops, unsigned NumOps);
689 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
690 EVT VT2, EVT VT3, const SDValue *Ops, unsigned NumOps);
691 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1,
692 EVT VT2, EVT VT3, EVT VT4, const SDValue *Ops,
694 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
695 EVT VT2, SDValue Op1);
696 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
697 EVT VT2, SDValue Op1, SDValue Op2);
698 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
699 EVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
700 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
701 EVT VT2, EVT VT3, SDValue Op1, SDValue Op2, SDValue Op3);
702 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, SDVTList VTs,
703 const SDValue *Ops, unsigned NumOps);
705 /// MorphNodeTo - This *mutates* the specified node to have the specified
706 /// return type, opcode, and operands.
707 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs,
708 const SDValue *Ops, unsigned NumOps);
710 /// getMachineNode - These are used for target selectors to create a new node
711 /// with specified return type(s), MachineInstr opcode, and operands.
713 /// Note that getMachineNode returns the resultant node. If there is already
714 /// a node of the specified opcode and operands, it returns that node instead
715 /// of the current one.
716 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT);
717 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
719 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
720 SDValue Op1, SDValue Op2);
721 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
722 SDValue Op1, SDValue Op2, SDValue Op3);
723 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
724 const SDValue *Ops, unsigned NumOps);
725 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2);
726 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
728 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
729 EVT VT2, SDValue Op1, SDValue Op2);
730 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
731 EVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
732 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
733 const SDValue *Ops, unsigned NumOps);
734 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
735 EVT VT3, SDValue Op1, SDValue Op2);
736 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
737 EVT VT3, SDValue Op1, SDValue Op2, SDValue Op3);
738 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
739 EVT VT3, const SDValue *Ops, unsigned NumOps);
740 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
741 EVT VT3, EVT VT4, const SDValue *Ops, unsigned NumOps);
742 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl,
743 const std::vector<EVT> &ResultTys, const SDValue *Ops,
745 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, SDVTList VTs,
746 const SDValue *Ops, unsigned NumOps);
748 /// getTargetExtractSubreg - A convenience function for creating
749 /// TargetInstrInfo::EXTRACT_SUBREG nodes.
750 SDValue getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
753 /// getTargetInsertSubreg - A convenience function for creating
754 /// TargetInstrInfo::INSERT_SUBREG nodes.
755 SDValue getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT,
756 SDValue Operand, SDValue Subreg);
758 /// getNodeIfExists - Get the specified node if it's already available, or
759 /// else return NULL.
760 SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTs,
761 const SDValue *Ops, unsigned NumOps);
763 /// getDbgValue - Creates a SDDbgValue node.
765 SDDbgValue *getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
766 DebugLoc DL, unsigned O);
767 SDDbgValue *getDbgValue(MDNode *MDPtr, Value *C, uint64_t Off,
768 DebugLoc DL, unsigned O);
769 SDDbgValue *getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
770 DebugLoc DL, unsigned O);
772 /// DAGUpdateListener - Clients of various APIs that cause global effects on
773 /// the DAG can optionally implement this interface. This allows the clients
774 /// to handle the various sorts of updates that happen.
775 class DAGUpdateListener {
777 virtual ~DAGUpdateListener();
779 /// NodeDeleted - The node N that was deleted and, if E is not null, an
780 /// equivalent node E that replaced it.
781 virtual void NodeDeleted(SDNode *N, SDNode *E) = 0;
783 /// NodeUpdated - The node N that was updated.
784 virtual void NodeUpdated(SDNode *N) = 0;
787 /// RemoveDeadNode - Remove the specified node from the system. If any of its
788 /// operands then becomes dead, remove them as well. Inform UpdateListener
789 /// for each node deleted.
790 void RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener = 0);
792 /// RemoveDeadNodes - This method deletes the unreachable nodes in the
793 /// given list, and any nodes that become unreachable as a result.
794 void RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
795 DAGUpdateListener *UpdateListener = 0);
797 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
798 /// This can cause recursive merging of nodes in the DAG. Use the first
799 /// version if 'From' is known to have a single result, use the second
800 /// if you have two nodes with identical results (or if 'To' has a superset
801 /// of the results of 'From'), use the third otherwise.
803 /// These methods all take an optional UpdateListener, which (if not null) is
804 /// informed about nodes that are deleted and modified due to recursive
805 /// changes in the dag.
807 /// These functions only replace all existing uses. It's possible that as
808 /// these replacements are being performed, CSE may cause the From node
809 /// to be given new uses. These new uses of From are left in place, and
810 /// not automatically transfered to To.
812 void ReplaceAllUsesWith(SDValue From, SDValue Op,
813 DAGUpdateListener *UpdateListener = 0);
814 void ReplaceAllUsesWith(SDNode *From, SDNode *To,
815 DAGUpdateListener *UpdateListener = 0);
816 void ReplaceAllUsesWith(SDNode *From, const SDValue *To,
817 DAGUpdateListener *UpdateListener = 0);
819 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
820 /// uses of other values produced by From.Val alone.
821 void ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
822 DAGUpdateListener *UpdateListener = 0);
824 /// ReplaceAllUsesOfValuesWith - Like ReplaceAllUsesOfValueWith, but
825 /// for multiple values at once. This correctly handles the case where
826 /// there is an overlap between the From values and the To values.
827 void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To,
829 DAGUpdateListener *UpdateListener = 0);
831 /// AssignTopologicalOrder - Topological-sort the AllNodes list and a
832 /// assign a unique node id for each node in the DAG based on their
833 /// topological order. Returns the number of nodes.
834 unsigned AssignTopologicalOrder();
836 /// RepositionNode - Move node N in the AllNodes list to be immediately
837 /// before the given iterator Position. This may be used to update the
838 /// topological ordering when the list of nodes is modified.
839 void RepositionNode(allnodes_iterator Position, SDNode *N) {
840 AllNodes.insert(Position, AllNodes.remove(N));
843 /// isCommutativeBinOp - Returns true if the opcode is a commutative binary
845 static bool isCommutativeBinOp(unsigned Opcode) {
846 // FIXME: This should get its info from the td file, so that we can include
863 case ISD::ADDE: return true;
864 default: return false;
868 /// AssignOrdering - Assign an order to the SDNode.
869 void AssignOrdering(const SDNode *SD, unsigned Order);
871 /// GetOrdering - Get the order for the SDNode.
872 unsigned GetOrdering(const SDNode *SD) const;
874 /// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
875 /// value is produced by SD.
876 void AddDbgValue(SDDbgValue *DB, SDNode *SD = 0);
878 /// GetDbgValues - Get the debug values which reference the given SDNode.
879 SmallVector<SDDbgValue*,2> &GetDbgValues(const SDNode* SD) {
880 return DbgInfo->getSDDbgValues(SD);
883 /// hasDebugValues - Return true if there are any SDDbgValue nodes associated
884 /// with this SelectionDAG.
885 bool hasDebugValues() const { return !DbgInfo->empty(); }
887 SDDbgInfo::DbgIterator DbgBegin() { return DbgInfo->DbgBegin(); }
888 SDDbgInfo::DbgIterator DbgEnd() { return DbgInfo->DbgEnd(); }
892 /// CreateStackTemporary - Create a stack temporary, suitable for holding the
893 /// specified value type. If minAlign is specified, the slot size will have
894 /// at least that alignment.
895 SDValue CreateStackTemporary(EVT VT, unsigned minAlign = 1);
897 /// CreateStackTemporary - Create a stack temporary suitable for holding
898 /// either of the specified value types.
899 SDValue CreateStackTemporary(EVT VT1, EVT VT2);
901 /// FoldConstantArithmetic -
902 SDValue FoldConstantArithmetic(unsigned Opcode,
904 ConstantSDNode *Cst1,
905 ConstantSDNode *Cst2);
907 /// FoldSetCC - Constant fold a setcc to true or false.
908 SDValue FoldSetCC(EVT VT, SDValue N1,
909 SDValue N2, ISD::CondCode Cond, DebugLoc dl);
911 /// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
912 /// use this predicate to simplify operations downstream.
913 bool SignBitIsZero(SDValue Op, unsigned Depth = 0) const;
915 /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We
916 /// use this predicate to simplify operations downstream. Op and Mask are
917 /// known to be the same type.
918 bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth = 0)
921 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
922 /// known to be either zero or one and return them in the KnownZero/KnownOne
923 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
924 /// processing. Targets can implement the computeMaskedBitsForTargetNode
925 /// method in the TargetLowering class to allow target nodes to be understood.
926 void ComputeMaskedBits(SDValue Op, const APInt &Mask, APInt &KnownZero,
927 APInt &KnownOne, unsigned Depth = 0) const;
929 /// ComputeNumSignBits - Return the number of times the sign bit of the
930 /// register is replicated into the other bits. We know that at least 1 bit
931 /// is always equal to the sign bit (itself), but other cases can give us
932 /// information. For example, immediately after an "SRA X, 2", we know that
933 /// the top 3 bits are all equal to each other, so we return 3. Targets can
934 /// implement the ComputeNumSignBitsForTarget method in the TargetLowering
935 /// class to allow target nodes to be understood.
936 unsigned ComputeNumSignBits(SDValue Op, unsigned Depth = 0) const;
938 /// isKnownNeverNan - Test whether the given SDValue is known to never be NaN.
939 bool isKnownNeverNaN(SDValue Op) const;
941 /// isKnownNeverZero - Test whether the given SDValue is known to never be
942 /// positive or negative Zero.
943 bool isKnownNeverZero(SDValue Op) const;
945 /// isEqualTo - Test whether two SDValues are known to compare equal. This
946 /// is true if they are the same value, or if one is negative zero and the
947 /// other positive zero.
948 bool isEqualTo(SDValue A, SDValue B) const;
950 /// isVerifiedDebugInfoDesc - Returns true if the specified SDValue has
951 /// been verified as a debug information descriptor.
952 bool isVerifiedDebugInfoDesc(SDValue Op) const;
954 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
955 /// element of the result of the vector shuffle.
956 SDValue getShuffleScalarElt(const ShuffleVectorSDNode *N, unsigned Idx);
958 /// UnrollVectorOp - Utility function used by legalize and lowering to
959 /// "unroll" a vector operation by splitting out the scalars and operating
960 /// on each element individually. If the ResNE is 0, fully unroll the vector
961 /// op. If ResNE is less than the width of the vector op, unroll up to ResNE.
962 /// If the ResNE is greater than the width of the vector op, unroll the
963 /// vector op and fill the end of the resulting vector with UNDEFS.
964 SDValue UnrollVectorOp(SDNode *N, unsigned ResNE = 0);
966 /// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
967 /// location that is 'Dist' units away from the location that the 'Base' load
969 bool isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
970 unsigned Bytes, int Dist) const;
972 /// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
973 /// it cannot be inferred.
974 unsigned InferPtrAlignment(SDValue Ptr) const;
977 bool RemoveNodeFromCSEMaps(SDNode *N);
978 void AddModifiedNodeToCSEMaps(SDNode *N, DAGUpdateListener *UpdateListener);
979 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op, void *&InsertPos);
980 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op1, SDValue Op2,
982 SDNode *FindModifiedNodeSlot(SDNode *N, const SDValue *Ops, unsigned NumOps,
985 void DeleteNodeNotInCSEMaps(SDNode *N);
986 void DeallocateNode(SDNode *N);
988 unsigned getEVTAlignment(EVT MemoryVT) const;
990 void allnodes_clear();
992 /// VTList - List of non-single value types.
993 std::vector<SDVTList> VTList;
995 /// CondCodeNodes - Maps to auto-CSE operations.
996 std::vector<CondCodeSDNode*> CondCodeNodes;
998 std::vector<SDNode*> ValueTypeNodes;
999 std::map<EVT, SDNode*, EVT::compareRawBits> ExtendedValueTypeNodes;
1000 StringMap<SDNode*> ExternalSymbols;
1002 std::map<std::pair<std::string, unsigned char>,SDNode*> TargetExternalSymbols;
1005 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
1006 typedef SelectionDAG::allnodes_iterator nodes_iterator;
1007 static nodes_iterator nodes_begin(SelectionDAG *G) {
1008 return G->allnodes_begin();
1010 static nodes_iterator nodes_end(SelectionDAG *G) {
1011 return G->allnodes_end();
1015 } // end namespace llvm