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;
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 &);
61 Unrestricted, // Combine may create illegal operations and illegal types.
62 NoIllegalTypes, // Combine may create illegal operations but no illegal types.
63 NoIllegalOperations // Combine may only create legal operations and types.
67 void checkForCycles(const SDNode *N);
68 void checkForCycles(const SelectionDAG *DAG);
70 /// SelectionDAG class - This is used to represent a portion of an LLVM function
71 /// in a low-level Data Dependence DAG representation suitable for instruction
72 /// selection. This DAG is constructed as the first step of instruction
73 /// selection in order to allow implementation of machine specific optimizations
74 /// and code simplifications.
76 /// The representation used by the SelectionDAG is a target-independent
77 /// representation, which has some similarities to the GCC RTL representation,
78 /// but is significantly more simple, powerful, and is a graph form instead of a
84 FunctionLoweringInfo &FLI;
85 MachineModuleInfo *MMI;
89 /// EntryNode - The starting token.
92 /// Root - The root of the entire DAG.
95 /// AllNodes - A linked list of nodes in the current DAG.
96 ilist<SDNode> AllNodes;
98 /// NodeAllocatorType - The AllocatorType for allocating SDNodes. We use
99 /// pool allocation with recycling.
100 typedef RecyclingAllocator<BumpPtrAllocator, SDNode, sizeof(LargestSDNode),
101 AlignOf<MostAlignedSDNode>::Alignment>
104 /// NodeAllocator - Pool allocation for nodes.
105 NodeAllocatorType NodeAllocator;
107 /// CSEMap - This structure is used to memoize nodes, automatically performing
108 /// CSE with existing nodes when a duplicate is requested.
109 FoldingSet<SDNode> CSEMap;
111 /// OperandAllocator - Pool allocation for machine-opcode SDNode operands.
112 BumpPtrAllocator OperandAllocator;
114 /// Allocator - Pool allocation for misc. objects that are created once per
116 BumpPtrAllocator Allocator;
118 /// SDNodeOrdering - The ordering of the SDNodes. It roughly corresponds to
119 /// the ordering of the original LLVM instructions.
120 SDNodeOrdering *Ordering;
122 /// VerifyNode - Sanity check the given node. Aborts if it is invalid.
123 void VerifyNode(SDNode *N);
125 /// setGraphColorHelper - Implementation of setSubgraphColor.
126 /// Return whether we had to truncate the search.
128 bool setSubgraphColorHelper(SDNode *N, const char *Color,
129 DenseSet<SDNode *> &visited,
130 int level, bool &printed);
132 void operator=(const SelectionDAG&); // Do not implement.
133 SelectionDAG(const SelectionDAG&); // Do not implement.
136 SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli);
139 /// init - Prepare this SelectionDAG to process code in the given
142 void init(MachineFunction &mf, MachineModuleInfo *mmi, DwarfWriter *dw);
144 /// clear - Clear state and free memory necessary to make this
145 /// SelectionDAG ready to process a new block.
149 MachineFunction &getMachineFunction() const { return *MF; }
150 const TargetMachine &getTarget() const;
151 TargetLowering &getTargetLoweringInfo() const { return TLI; }
152 FunctionLoweringInfo &getFunctionLoweringInfo() const { return FLI; }
153 MachineModuleInfo *getMachineModuleInfo() const { return MMI; }
154 DwarfWriter *getDwarfWriter() const { return DW; }
155 LLVMContext *getContext() const {return Context; }
157 /// viewGraph - Pop up a GraphViz/gv window with the DAG rendered using 'dot'.
159 void viewGraph(const std::string &Title);
163 std::map<const SDNode *, std::string> NodeGraphAttrs;
166 /// clearGraphAttrs - Clear all previously defined node graph attributes.
167 /// Intended to be used from a debugging tool (eg. gdb).
168 void clearGraphAttrs();
170 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
172 void setGraphAttrs(const SDNode *N, const char *Attrs);
174 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
175 /// Used from getNodeAttributes.
176 const std::string getGraphAttrs(const SDNode *N) const;
178 /// setGraphColor - Convenience for setting node color attribute.
180 void setGraphColor(const SDNode *N, const char *Color);
182 /// setGraphColor - Convenience for setting subgraph color attribute.
184 void setSubgraphColor(SDNode *N, const char *Color);
186 typedef ilist<SDNode>::const_iterator allnodes_const_iterator;
187 allnodes_const_iterator allnodes_begin() const { return AllNodes.begin(); }
188 allnodes_const_iterator allnodes_end() const { return AllNodes.end(); }
189 typedef ilist<SDNode>::iterator allnodes_iterator;
190 allnodes_iterator allnodes_begin() { return AllNodes.begin(); }
191 allnodes_iterator allnodes_end() { return AllNodes.end(); }
192 ilist<SDNode>::size_type allnodes_size() const {
193 return AllNodes.size();
196 /// getRoot - Return the root tag of the SelectionDAG.
198 const SDValue &getRoot() const { return Root; }
200 /// getEntryNode - Return the token chain corresponding to the entry of the
202 SDValue getEntryNode() const {
203 return SDValue(const_cast<SDNode *>(&EntryNode), 0);
206 /// setRoot - Set the current root tag of the SelectionDAG.
208 const SDValue &setRoot(SDValue N) {
209 assert((!N.getNode() || N.getValueType() == MVT::Other) &&
210 "DAG root value is not a chain!");
212 checkForCycles(N.getNode());
215 checkForCycles(this);
219 /// Combine - This iterates over the nodes in the SelectionDAG, folding
220 /// certain types of nodes together, or eliminating superfluous nodes. The
221 /// Level argument controls whether Combine is allowed to produce nodes and
222 /// types that are illegal on the target.
223 void Combine(CombineLevel Level, AliasAnalysis &AA,
224 CodeGenOpt::Level OptLevel);
226 /// LegalizeTypes - This transforms the SelectionDAG into a SelectionDAG that
227 /// only uses types natively supported by the target. Returns "true" if it
228 /// made any changes.
230 /// Note that this is an involved process that may invalidate pointers into
232 bool LegalizeTypes();
234 /// Legalize - This transforms the SelectionDAG into a SelectionDAG that is
235 /// compatible with the target instruction selector, as indicated by the
236 /// TargetLowering object.
238 /// Note that this is an involved process that may invalidate pointers into
240 void Legalize(CodeGenOpt::Level OptLevel);
242 /// LegalizeVectors - This transforms the SelectionDAG into a SelectionDAG
243 /// that only uses vector math operations supported by the target. This is
244 /// necessary as a separate step from Legalize because unrolling a vector
245 /// operation can introduce illegal types, which requires running
246 /// LegalizeTypes again.
248 /// This returns true if it made any changes; in that case, LegalizeTypes
249 /// is called again before Legalize.
251 /// Note that this is an involved process that may invalidate pointers into
253 bool LegalizeVectors();
255 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
257 void RemoveDeadNodes();
259 /// DeleteNode - Remove the specified node from the system. This node must
260 /// have no referrers.
261 void DeleteNode(SDNode *N);
263 /// getVTList - Return an SDVTList that represents the list of values
265 SDVTList getVTList(EVT VT);
266 SDVTList getVTList(EVT VT1, EVT VT2);
267 SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3);
268 SDVTList getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4);
269 SDVTList getVTList(const EVT *VTs, unsigned NumVTs);
271 //===--------------------------------------------------------------------===//
272 // Node creation methods.
274 SDValue getConstant(uint64_t Val, EVT VT, bool isTarget = false);
275 SDValue getConstant(const APInt &Val, EVT VT, bool isTarget = false);
276 SDValue getConstant(const ConstantInt &Val, EVT VT, bool isTarget = false);
277 SDValue getIntPtrConstant(uint64_t Val, bool isTarget = false);
278 SDValue getTargetConstant(uint64_t Val, EVT VT) {
279 return getConstant(Val, VT, true);
281 SDValue getTargetConstant(const APInt &Val, EVT VT) {
282 return getConstant(Val, VT, true);
284 SDValue getTargetConstant(const ConstantInt &Val, EVT VT) {
285 return getConstant(Val, VT, true);
287 SDValue getConstantFP(double Val, EVT VT, bool isTarget = false);
288 SDValue getConstantFP(const APFloat& Val, EVT VT, bool isTarget = false);
289 SDValue getConstantFP(const ConstantFP &CF, EVT VT, bool isTarget = false);
290 SDValue getTargetConstantFP(double Val, EVT VT) {
291 return getConstantFP(Val, VT, true);
293 SDValue getTargetConstantFP(const APFloat& Val, EVT VT) {
294 return getConstantFP(Val, VT, true);
296 SDValue getTargetConstantFP(const ConstantFP &Val, EVT VT) {
297 return getConstantFP(Val, VT, true);
299 SDValue getGlobalAddress(const GlobalValue *GV, EVT VT,
300 int64_t offset = 0, bool isTargetGA = false,
301 unsigned char TargetFlags = 0);
302 SDValue getTargetGlobalAddress(const GlobalValue *GV, EVT VT,
304 unsigned char TargetFlags = 0) {
305 return getGlobalAddress(GV, VT, offset, true, TargetFlags);
307 SDValue getFrameIndex(int FI, EVT VT, bool isTarget = false);
308 SDValue getTargetFrameIndex(int FI, EVT VT) {
309 return getFrameIndex(FI, VT, true);
311 SDValue getJumpTable(int JTI, EVT VT, bool isTarget = false,
312 unsigned char TargetFlags = 0);
313 SDValue getTargetJumpTable(int JTI, EVT VT, unsigned char TargetFlags = 0) {
314 return getJumpTable(JTI, VT, true, TargetFlags);
316 SDValue getConstantPool(Constant *C, EVT VT,
317 unsigned Align = 0, int Offs = 0, bool isT=false,
318 unsigned char TargetFlags = 0);
319 SDValue getTargetConstantPool(Constant *C, EVT VT,
320 unsigned Align = 0, int Offset = 0,
321 unsigned char TargetFlags = 0) {
322 return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
324 SDValue getConstantPool(MachineConstantPoolValue *C, EVT VT,
325 unsigned Align = 0, int Offs = 0, bool isT=false,
326 unsigned char TargetFlags = 0);
327 SDValue getTargetConstantPool(MachineConstantPoolValue *C,
328 EVT VT, unsigned Align = 0,
329 int Offset = 0, unsigned char TargetFlags=0) {
330 return getConstantPool(C, VT, Align, Offset, true, TargetFlags);
332 // When generating a branch to a BB, we don't in general know enough
333 // to provide debug info for the BB at that time, so keep this one around.
334 SDValue getBasicBlock(MachineBasicBlock *MBB);
335 SDValue getBasicBlock(MachineBasicBlock *MBB, DebugLoc dl);
336 SDValue getExternalSymbol(const char *Sym, EVT VT);
337 SDValue getExternalSymbol(const char *Sym, DebugLoc dl, EVT VT);
338 SDValue getTargetExternalSymbol(const char *Sym, EVT VT,
339 unsigned char TargetFlags = 0);
340 SDValue getValueType(EVT);
341 SDValue getRegister(unsigned Reg, EVT VT);
342 SDValue getLabel(unsigned Opcode, DebugLoc dl, SDValue Root,
344 SDValue getBlockAddress(BlockAddress *BA, EVT VT,
345 bool isTarget = false, unsigned char TargetFlags = 0);
347 SDValue getCopyToReg(SDValue Chain, DebugLoc dl, unsigned Reg, SDValue N) {
348 return getNode(ISD::CopyToReg, dl, MVT::Other, Chain,
349 getRegister(Reg, N.getValueType()), N);
352 // This version of the getCopyToReg method takes an extra operand, which
353 // indicates that there is potentially an incoming flag value (if Flag is not
354 // null) and that there should be a flag result.
355 SDValue getCopyToReg(SDValue Chain, DebugLoc dl, unsigned Reg, SDValue N,
357 SDVTList VTs = getVTList(MVT::Other, MVT::Flag);
358 SDValue Ops[] = { Chain, getRegister(Reg, N.getValueType()), N, Flag };
359 return getNode(ISD::CopyToReg, dl, VTs, Ops, Flag.getNode() ? 4 : 3);
362 // Similar to last getCopyToReg() except parameter Reg is a SDValue
363 SDValue getCopyToReg(SDValue Chain, DebugLoc dl, SDValue Reg, SDValue N,
365 SDVTList VTs = getVTList(MVT::Other, MVT::Flag);
366 SDValue Ops[] = { Chain, Reg, N, Flag };
367 return getNode(ISD::CopyToReg, dl, VTs, Ops, Flag.getNode() ? 4 : 3);
370 SDValue getCopyFromReg(SDValue Chain, DebugLoc dl, unsigned Reg, EVT VT) {
371 SDVTList VTs = getVTList(VT, MVT::Other);
372 SDValue Ops[] = { Chain, getRegister(Reg, VT) };
373 return getNode(ISD::CopyFromReg, dl, VTs, Ops, 2);
376 // This version of the getCopyFromReg method takes an extra operand, which
377 // indicates that there is potentially an incoming flag value (if Flag is not
378 // null) and that there should be a flag result.
379 SDValue getCopyFromReg(SDValue Chain, DebugLoc dl, unsigned Reg, EVT VT,
381 SDVTList VTs = getVTList(VT, MVT::Other, MVT::Flag);
382 SDValue Ops[] = { Chain, getRegister(Reg, VT), Flag };
383 return getNode(ISD::CopyFromReg, dl, VTs, Ops, Flag.getNode() ? 3 : 2);
386 SDValue getCondCode(ISD::CondCode Cond);
388 /// Returns the ConvertRndSat Note: Avoid using this node because it may
389 /// disappear in the future and most targets don't support it.
390 SDValue getConvertRndSat(EVT VT, DebugLoc dl, SDValue Val, SDValue DTy,
392 SDValue Rnd, SDValue Sat, ISD::CvtCode Code);
394 /// getVectorShuffle - Return an ISD::VECTOR_SHUFFLE node. The number of
395 /// elements in VT, which must be a vector type, must match the number of
396 /// mask elements NumElts. A integer mask element equal to -1 is treated as
398 SDValue getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1, SDValue N2,
399 const int *MaskElts);
401 /// getSExtOrTrunc - Convert Op, which must be of integer type, to the
402 /// integer type VT, by either sign-extending or truncating it.
403 SDValue getSExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT);
405 /// getZExtOrTrunc - Convert Op, which must be of integer type, to the
406 /// integer type VT, by either zero-extending or truncating it.
407 SDValue getZExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT);
409 /// getZeroExtendInReg - Return the expression required to zero extend the Op
410 /// value assuming it was the smaller SrcTy value.
411 SDValue getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT SrcTy);
413 /// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
414 SDValue getNOT(DebugLoc DL, SDValue Val, EVT VT);
416 /// getCALLSEQ_START - Return a new CALLSEQ_START node, which always must have
417 /// a flag result (to ensure it's not CSE'd). CALLSEQ_START does not have a
419 SDValue getCALLSEQ_START(SDValue Chain, SDValue Op) {
420 SDVTList VTs = getVTList(MVT::Other, MVT::Flag);
421 SDValue Ops[] = { Chain, Op };
422 return getNode(ISD::CALLSEQ_START, DebugLoc::getUnknownLoc(),
426 /// getCALLSEQ_END - Return a new CALLSEQ_END node, which always must have a
427 /// flag result (to ensure it's not CSE'd). CALLSEQ_END does not have
428 /// a useful DebugLoc.
429 SDValue getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2,
431 SDVTList NodeTys = getVTList(MVT::Other, MVT::Flag);
432 SmallVector<SDValue, 4> Ops;
433 Ops.push_back(Chain);
436 Ops.push_back(InFlag);
437 return getNode(ISD::CALLSEQ_END, DebugLoc::getUnknownLoc(), NodeTys,
439 (unsigned)Ops.size() - (InFlag.getNode() == 0 ? 1 : 0));
442 /// getUNDEF - Return an UNDEF node. UNDEF does not have a useful DebugLoc.
443 SDValue getUNDEF(EVT VT) {
444 return getNode(ISD::UNDEF, DebugLoc::getUnknownLoc(), VT);
447 /// getGLOBAL_OFFSET_TABLE - Return a GLOBAL_OFFSET_TABLE node. This does
448 /// not have a useful DebugLoc.
449 SDValue getGLOBAL_OFFSET_TABLE(EVT VT) {
450 return getNode(ISD::GLOBAL_OFFSET_TABLE, DebugLoc::getUnknownLoc(), VT);
453 /// getNode - Gets or creates the specified node.
455 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT);
456 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT, SDValue N);
457 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT, SDValue N1, SDValue N2);
458 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
459 SDValue N1, SDValue N2, SDValue N3);
460 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
461 SDValue N1, SDValue N2, SDValue N3, SDValue N4);
462 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
463 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
465 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
466 const SDUse *Ops, unsigned NumOps);
467 SDValue getNode(unsigned Opcode, DebugLoc DL, EVT VT,
468 const SDValue *Ops, unsigned NumOps);
469 SDValue getNode(unsigned Opcode, DebugLoc DL,
470 const std::vector<EVT> &ResultTys,
471 const SDValue *Ops, unsigned NumOps);
472 SDValue getNode(unsigned Opcode, DebugLoc DL, const EVT *VTs, unsigned NumVTs,
473 const SDValue *Ops, unsigned NumOps);
474 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
475 const SDValue *Ops, unsigned NumOps);
476 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs);
477 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs, SDValue N);
478 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
479 SDValue N1, SDValue N2);
480 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
481 SDValue N1, SDValue N2, SDValue N3);
482 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
483 SDValue N1, SDValue N2, SDValue N3, SDValue N4);
484 SDValue getNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
485 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
488 /// getStackArgumentTokenFactor - Compute a TokenFactor to force all
489 /// the incoming stack arguments to be loaded from the stack. This is
490 /// used in tail call lowering to protect stack arguments from being
492 SDValue getStackArgumentTokenFactor(SDValue Chain);
494 SDValue getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst, SDValue Src,
495 SDValue Size, unsigned Align, bool AlwaysInline,
496 const Value *DstSV, uint64_t DstSVOff,
497 const Value *SrcSV, uint64_t SrcSVOff);
499 SDValue getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst, SDValue Src,
500 SDValue Size, unsigned Align,
501 const Value *DstSV, uint64_t DstOSVff,
502 const Value *SrcSV, uint64_t SrcSVOff);
504 SDValue getMemset(SDValue Chain, DebugLoc dl, SDValue Dst, SDValue Src,
505 SDValue Size, unsigned Align,
506 const Value *DstSV, uint64_t DstSVOff);
508 /// getSetCC - Helper function to make it easier to build SetCC's if you just
509 /// have an ISD::CondCode instead of an SDValue.
511 SDValue getSetCC(DebugLoc DL, EVT VT, SDValue LHS, SDValue RHS,
512 ISD::CondCode Cond) {
513 return getNode(ISD::SETCC, DL, VT, LHS, RHS, getCondCode(Cond));
516 /// getVSetCC - Helper function to make it easier to build VSetCC's nodes
517 /// if you just have an ISD::CondCode instead of an SDValue.
519 SDValue getVSetCC(DebugLoc DL, EVT VT, SDValue LHS, SDValue RHS,
520 ISD::CondCode Cond) {
521 return getNode(ISD::VSETCC, DL, VT, LHS, RHS, getCondCode(Cond));
524 /// getSelectCC - Helper function to make it easier to build SelectCC's if you
525 /// just have an ISD::CondCode instead of an SDValue.
527 SDValue getSelectCC(DebugLoc DL, SDValue LHS, SDValue RHS,
528 SDValue True, SDValue False, ISD::CondCode Cond) {
529 return getNode(ISD::SELECT_CC, DL, True.getValueType(),
530 LHS, RHS, True, False, getCondCode(Cond));
533 /// getVAArg - VAArg produces a result and token chain, and takes a pointer
534 /// and a source value as input.
535 SDValue getVAArg(EVT VT, DebugLoc dl, SDValue Chain, SDValue Ptr,
538 /// getAtomic - Gets a node for an atomic op, produces result and chain and
540 SDValue getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, SDValue Chain,
541 SDValue Ptr, SDValue Cmp, SDValue Swp, const Value* PtrVal,
542 unsigned Alignment=0);
543 SDValue getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, SDValue Chain,
544 SDValue Ptr, SDValue Cmp, SDValue Swp,
545 MachineMemOperand *MMO);
547 /// getAtomic - Gets a node for an atomic op, produces result and chain and
548 /// takes 2 operands.
549 SDValue getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, SDValue Chain,
550 SDValue Ptr, SDValue Val, const Value* PtrVal,
551 unsigned Alignment = 0);
552 SDValue getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT, SDValue Chain,
553 SDValue Ptr, SDValue Val,
554 MachineMemOperand *MMO);
556 /// getMemIntrinsicNode - Creates a MemIntrinsicNode that may produce a
557 /// result and takes a list of operands. Opcode may be INTRINSIC_VOID,
558 /// INTRINSIC_W_CHAIN, or a target-specific opcode with a value not
559 /// less than FIRST_TARGET_MEMORY_OPCODE.
560 SDValue getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
561 const EVT *VTs, unsigned NumVTs,
562 const SDValue *Ops, unsigned NumOps,
563 EVT MemVT, const Value *srcValue, int SVOff,
564 unsigned Align = 0, bool Vol = false,
565 bool ReadMem = true, bool WriteMem = true);
567 SDValue getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
568 const SDValue *Ops, unsigned NumOps,
569 EVT MemVT, const Value *srcValue, int SVOff,
570 unsigned Align = 0, bool Vol = false,
571 bool ReadMem = true, bool WriteMem = true);
573 SDValue getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
574 const SDValue *Ops, unsigned NumOps,
575 EVT MemVT, MachineMemOperand *MMO);
577 /// getMergeValues - Create a MERGE_VALUES node from the given operands.
578 SDValue getMergeValues(const SDValue *Ops, unsigned NumOps, DebugLoc dl);
580 /// getLoad - Loads are not normal binary operators: their result type is not
581 /// determined by their operands, and they produce a value AND a token chain.
583 SDValue getLoad(EVT VT, DebugLoc dl, SDValue Chain, SDValue Ptr,
584 const Value *SV, int SVOffset, bool isVolatile=false,
585 unsigned Alignment=0);
586 SDValue getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT,
587 SDValue Chain, SDValue Ptr, const Value *SV,
588 int SVOffset, EVT MemVT, bool isVolatile=false,
589 unsigned Alignment=0);
590 SDValue getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
591 SDValue Offset, ISD::MemIndexedMode AM);
592 SDValue getLoad(ISD::MemIndexedMode AM, DebugLoc dl, ISD::LoadExtType ExtType,
593 EVT VT, SDValue Chain, SDValue Ptr, SDValue Offset,
594 const Value *SV, int SVOffset, EVT MemVT,
595 bool isVolatile=false, unsigned Alignment=0);
596 SDValue getLoad(ISD::MemIndexedMode AM, DebugLoc dl, ISD::LoadExtType ExtType,
597 EVT VT, SDValue Chain, SDValue Ptr, SDValue Offset,
598 EVT MemVT, MachineMemOperand *MMO);
600 /// getStore - Helper function to build ISD::STORE nodes.
602 SDValue getStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
603 const Value *SV, int SVOffset, bool isVolatile=false,
604 unsigned Alignment=0);
605 SDValue getStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
606 MachineMemOperand *MMO);
607 SDValue getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
608 const Value *SV, int SVOffset, EVT TVT,
609 bool isVolatile=false, unsigned Alignment=0);
610 SDValue getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val, SDValue Ptr,
611 EVT TVT, MachineMemOperand *MMO);
612 SDValue getIndexedStore(SDValue OrigStoe, DebugLoc dl, SDValue Base,
613 SDValue Offset, ISD::MemIndexedMode AM);
615 /// getSrcValue - Construct a node to track a Value* through the backend.
616 SDValue getSrcValue(const Value *v);
618 /// getShiftAmountOperand - Return the specified value casted to
619 /// the target's desired shift amount type.
620 SDValue getShiftAmountOperand(SDValue Op);
622 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
623 /// specified operands. If the resultant node already exists in the DAG,
624 /// this does not modify the specified node, instead it returns the node that
625 /// already exists. If the resultant node does not exist in the DAG, the
626 /// input node is returned. As a degenerate case, if you specify the same
627 /// input operands as the node already has, the input node is returned.
628 SDValue UpdateNodeOperands(SDValue N, SDValue Op);
629 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2);
630 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
632 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
633 SDValue Op3, SDValue Op4);
634 SDValue UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
635 SDValue Op3, SDValue Op4, SDValue Op5);
636 SDValue UpdateNodeOperands(SDValue N,
637 const SDValue *Ops, unsigned NumOps);
639 /// SelectNodeTo - These are used for target selectors to *mutate* the
640 /// specified node to have the specified return type, Target opcode, and
641 /// operands. Note that target opcodes are stored as
642 /// ~TargetOpcode in the node opcode field. The resultant node is returned.
643 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT);
644 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT, SDValue Op1);
645 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
646 SDValue Op1, SDValue Op2);
647 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
648 SDValue Op1, SDValue Op2, SDValue Op3);
649 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT,
650 const SDValue *Ops, unsigned NumOps);
651 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1, EVT VT2);
652 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
653 EVT VT2, const SDValue *Ops, unsigned NumOps);
654 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
655 EVT VT2, EVT VT3, const SDValue *Ops, unsigned NumOps);
656 SDNode *SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT1,
657 EVT VT2, EVT VT3, EVT VT4, const SDValue *Ops,
659 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
660 EVT VT2, SDValue Op1);
661 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
662 EVT VT2, SDValue Op1, SDValue Op2);
663 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
664 EVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
665 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, EVT VT1,
666 EVT VT2, EVT VT3, SDValue Op1, SDValue Op2, SDValue Op3);
667 SDNode *SelectNodeTo(SDNode *N, unsigned TargetOpc, SDVTList VTs,
668 const SDValue *Ops, unsigned NumOps);
670 /// MorphNodeTo - These *mutate* the specified node to have the specified
671 /// return type, opcode, and operands.
672 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT);
673 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT, SDValue Op1);
674 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT,
675 SDValue Op1, SDValue Op2);
676 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT,
677 SDValue Op1, SDValue Op2, SDValue Op3);
678 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT,
679 const SDValue *Ops, unsigned NumOps);
680 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT1, EVT VT2);
681 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT1,
682 EVT VT2, const SDValue *Ops, unsigned NumOps);
683 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT1,
684 EVT VT2, EVT VT3, const SDValue *Ops, unsigned NumOps);
685 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT1,
686 EVT VT2, SDValue Op1);
687 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT1,
688 EVT VT2, SDValue Op1, SDValue Op2);
689 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, EVT VT1,
690 EVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
691 SDNode *MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs,
692 const SDValue *Ops, unsigned NumOps);
694 /// getMachineNode - These are used for target selectors to create a new node
695 /// with specified return type(s), MachineInstr opcode, and operands.
697 /// Note that getMachineNode returns the resultant node. If there is already
698 /// a node of the specified opcode and operands, it returns that node instead
699 /// of the current one.
700 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT);
701 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
703 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
704 SDValue Op1, SDValue Op2);
705 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
706 SDValue Op1, SDValue Op2, SDValue Op3);
707 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
708 const SDValue *Ops, unsigned NumOps);
709 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2);
710 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
712 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
713 EVT VT2, SDValue Op1, SDValue Op2);
714 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
715 EVT VT2, SDValue Op1, SDValue Op2, SDValue Op3);
716 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
717 const SDValue *Ops, unsigned NumOps);
718 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
719 EVT VT3, SDValue Op1, SDValue Op2);
720 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
721 EVT VT3, SDValue Op1, SDValue Op2, SDValue Op3);
722 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
723 EVT VT3, const SDValue *Ops, unsigned NumOps);
724 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2,
725 EVT VT3, EVT VT4, const SDValue *Ops, unsigned NumOps);
726 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl,
727 const std::vector<EVT> &ResultTys, const SDValue *Ops,
729 MachineSDNode *getMachineNode(unsigned Opcode, DebugLoc dl, SDVTList VTs,
730 const SDValue *Ops, unsigned NumOps);
732 /// getTargetExtractSubreg - A convenience function for creating
733 /// TargetInstrInfo::EXTRACT_SUBREG nodes.
734 SDValue getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
737 /// getTargetInsertSubreg - A convenience function for creating
738 /// TargetInstrInfo::INSERT_SUBREG nodes.
739 SDValue getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT,
740 SDValue Operand, SDValue Subreg);
742 /// getNodeIfExists - Get the specified node if it's already available, or
743 /// else return NULL.
744 SDNode *getNodeIfExists(unsigned Opcode, SDVTList VTs,
745 const SDValue *Ops, unsigned NumOps);
747 /// DAGUpdateListener - Clients of various APIs that cause global effects on
748 /// the DAG can optionally implement this interface. This allows the clients
749 /// to handle the various sorts of updates that happen.
750 class DAGUpdateListener {
752 virtual ~DAGUpdateListener();
754 /// NodeDeleted - The node N that was deleted and, if E is not null, an
755 /// equivalent node E that replaced it.
756 virtual void NodeDeleted(SDNode *N, SDNode *E) = 0;
758 /// NodeUpdated - The node N that was updated.
759 virtual void NodeUpdated(SDNode *N) = 0;
762 /// RemoveDeadNode - Remove the specified node from the system. If any of its
763 /// operands then becomes dead, remove them as well. Inform UpdateListener
764 /// for each node deleted.
765 void RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener = 0);
767 /// RemoveDeadNodes - This method deletes the unreachable nodes in the
768 /// given list, and any nodes that become unreachable as a result.
769 void RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
770 DAGUpdateListener *UpdateListener = 0);
772 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
773 /// This can cause recursive merging of nodes in the DAG. Use the first
774 /// version if 'From' is known to have a single result, use the second
775 /// if you have two nodes with identical results (or if 'To' has a superset
776 /// of the results of 'From'), use the third otherwise.
778 /// These methods all take an optional UpdateListener, which (if not null) is
779 /// informed about nodes that are deleted and modified due to recursive
780 /// changes in the dag.
782 /// These functions only replace all existing uses. It's possible that as
783 /// these replacements are being performed, CSE may cause the From node
784 /// to be given new uses. These new uses of From are left in place, and
785 /// not automatically transfered to To.
787 void ReplaceAllUsesWith(SDValue From, SDValue Op,
788 DAGUpdateListener *UpdateListener = 0);
789 void ReplaceAllUsesWith(SDNode *From, SDNode *To,
790 DAGUpdateListener *UpdateListener = 0);
791 void ReplaceAllUsesWith(SDNode *From, const SDValue *To,
792 DAGUpdateListener *UpdateListener = 0);
794 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
795 /// uses of other values produced by From.Val alone.
796 void ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
797 DAGUpdateListener *UpdateListener = 0);
799 /// ReplaceAllUsesOfValuesWith - Like ReplaceAllUsesOfValueWith, but
800 /// for multiple values at once. This correctly handles the case where
801 /// there is an overlap between the From values and the To values.
802 void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To,
804 DAGUpdateListener *UpdateListener = 0);
806 /// AssignTopologicalOrder - Topological-sort the AllNodes list and a
807 /// assign a unique node id for each node in the DAG based on their
808 /// topological order. Returns the number of nodes.
809 unsigned AssignTopologicalOrder();
811 /// RepositionNode - Move node N in the AllNodes list to be immediately
812 /// before the given iterator Position. This may be used to update the
813 /// topological ordering when the list of nodes is modified.
814 void RepositionNode(allnodes_iterator Position, SDNode *N) {
815 AllNodes.insert(Position, AllNodes.remove(N));
818 /// isCommutativeBinOp - Returns true if the opcode is a commutative binary
820 static bool isCommutativeBinOp(unsigned Opcode) {
821 // FIXME: This should get its info from the td file, so that we can include
838 case ISD::ADDE: return true;
839 default: return false;
843 /// AssignOrdering - Assign an order to the SDNode.
844 void AssignOrdering(SDNode *SD, unsigned Order);
846 /// GetOrdering - Get the order for the SDNode.
847 unsigned GetOrdering(const SDNode *SD) const;
851 /// CreateStackTemporary - Create a stack temporary, suitable for holding the
852 /// specified value type. If minAlign is specified, the slot size will have
853 /// at least that alignment.
854 SDValue CreateStackTemporary(EVT VT, unsigned minAlign = 1);
856 /// CreateStackTemporary - Create a stack temporary suitable for holding
857 /// either of the specified value types.
858 SDValue CreateStackTemporary(EVT VT1, EVT VT2);
860 /// FoldConstantArithmetic -
861 SDValue FoldConstantArithmetic(unsigned Opcode,
863 ConstantSDNode *Cst1,
864 ConstantSDNode *Cst2);
866 /// FoldSetCC - Constant fold a setcc to true or false.
867 SDValue FoldSetCC(EVT VT, SDValue N1,
868 SDValue N2, ISD::CondCode Cond, DebugLoc dl);
870 /// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
871 /// use this predicate to simplify operations downstream.
872 bool SignBitIsZero(SDValue Op, unsigned Depth = 0) const;
874 /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We
875 /// use this predicate to simplify operations downstream. Op and Mask are
876 /// known to be the same type.
877 bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth = 0)
880 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
881 /// known to be either zero or one and return them in the KnownZero/KnownOne
882 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
883 /// processing. Targets can implement the computeMaskedBitsForTargetNode
884 /// method in the TargetLowering class to allow target nodes to be understood.
885 void ComputeMaskedBits(SDValue Op, const APInt &Mask, APInt &KnownZero,
886 APInt &KnownOne, unsigned Depth = 0) const;
888 /// ComputeNumSignBits - Return the number of times the sign bit of the
889 /// register is replicated into the other bits. We know that at least 1 bit
890 /// is always equal to the sign bit (itself), but other cases can give us
891 /// information. For example, immediately after an "SRA X, 2", we know that
892 /// the top 3 bits are all equal to each other, so we return 3. Targets can
893 /// implement the ComputeNumSignBitsForTarget method in the TargetLowering
894 /// class to allow target nodes to be understood.
895 unsigned ComputeNumSignBits(SDValue Op, unsigned Depth = 0) const;
897 /// isKnownNeverNan - Test whether the given SDValue is known to never be NaN.
898 bool isKnownNeverNaN(SDValue Op) const;
900 /// isVerifiedDebugInfoDesc - Returns true if the specified SDValue has
901 /// been verified as a debug information descriptor.
902 bool isVerifiedDebugInfoDesc(SDValue Op) const;
904 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
905 /// element of the result of the vector shuffle.
906 SDValue getShuffleScalarElt(const ShuffleVectorSDNode *N, unsigned Idx);
908 /// UnrollVectorOp - Utility function used by legalize and lowering to
909 /// "unroll" a vector operation by splitting out the scalars and operating
910 /// on each element individually. If the ResNE is 0, fully unroll the vector
911 /// op. If ResNE is less than the width of the vector op, unroll up to ResNE.
912 /// If the ResNE is greater than the width of the vector op, unroll the
913 /// vector op and fill the end of the resulting vector with UNDEFS.
914 SDValue UnrollVectorOp(SDNode *N, unsigned ResNE = 0);
916 /// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
917 /// location that is 'Dist' units away from the location that the 'Base' load
919 bool isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
920 unsigned Bytes, int Dist) const;
922 /// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
923 /// it cannot be inferred.
924 unsigned InferPtrAlignment(SDValue Ptr) const;
927 bool RemoveNodeFromCSEMaps(SDNode *N);
928 void AddModifiedNodeToCSEMaps(SDNode *N, DAGUpdateListener *UpdateListener);
929 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op, void *&InsertPos);
930 SDNode *FindModifiedNodeSlot(SDNode *N, SDValue Op1, SDValue Op2,
932 SDNode *FindModifiedNodeSlot(SDNode *N, const SDValue *Ops, unsigned NumOps,
935 void DeleteNodeNotInCSEMaps(SDNode *N);
936 void DeallocateNode(SDNode *N);
938 unsigned getEVTAlignment(EVT MemoryVT) const;
940 void allnodes_clear();
942 /// VTList - List of non-single value types.
943 std::vector<SDVTList> VTList;
945 /// CondCodeNodes - Maps to auto-CSE operations.
946 std::vector<CondCodeSDNode*> CondCodeNodes;
948 std::vector<SDNode*> ValueTypeNodes;
949 std::map<EVT, SDNode*, EVT::compareRawBits> ExtendedValueTypeNodes;
950 StringMap<SDNode*> ExternalSymbols;
952 std::map<std::pair<std::string, unsigned char>,SDNode*> TargetExternalSymbols;
955 template <> struct GraphTraits<SelectionDAG*> : public GraphTraits<SDNode*> {
956 typedef SelectionDAG::allnodes_iterator nodes_iterator;
957 static nodes_iterator nodes_begin(SelectionDAG *G) {
958 return G->allnodes_begin();
960 static nodes_iterator nodes_end(SelectionDAG *G) {
961 return G->allnodes_end();
965 } // end namespace llvm