blob: 2011eb87710917ecffb984e7330242d5d89e71e8 [file] [log] [blame]
Chris Lattner63b570d2005-01-07 07:45:27 +00001//===-- llvm/CodeGen/SelectionDAGNodes.h - SelectionDAG Nodes ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the SDNode class and derived classes, which are used to
11// represent the nodes and operations present in a SelectionDAG. These nodes
12// and operations are machine code level operations, with some similarities to
13// the GCC RTL representation.
14//
15// Clients should include the SelectionDAG.h file instead of this file directly.
16//
17//===----------------------------------------------------------------------===//
18
19#ifndef LLVM_CODEGEN_SELECTIONDAGNODES_H
20#define LLVM_CODEGEN_SELECTIONDAGNODES_H
21
22#include "llvm/CodeGen/ValueTypes.h"
Chris Lattner1080b9e2005-01-10 23:05:53 +000023#include "llvm/ADT/GraphTraits.h"
24#include "llvm/ADT/GraphTraits.h"
25#include "llvm/ADT/iterator"
Jeff Cohen39931a32005-01-07 19:21:49 +000026#include "llvm/Support/DataTypes.h"
Chris Lattner63b570d2005-01-07 07:45:27 +000027#include <cassert>
28#include <vector>
29
30namespace llvm {
31
32class SelectionDAG;
33class GlobalValue;
34class MachineBasicBlock;
35class SDNode;
36template <typename T> struct simplify_type;
37
38/// ISD namespace - This namespace contains an enum which represents all of the
39/// SelectionDAG node types and value types.
40///
41namespace ISD {
42 //===--------------------------------------------------------------------===//
43 /// ISD::NodeType enum - This enum defines all of the operators valid in a
44 /// SelectionDAG.
45 ///
46 enum NodeType {
Chris Lattner8a496fc2005-01-13 17:58:35 +000047 // EntryToken - This is the marker used to indicate the start of the region.
48 EntryToken,
49
50 // Token factor - This node is takes multiple tokens as input and produces a
51 // single token result. This is used to represent the fact that the operand
52 // operators are independent of each other.
53 TokenFactor,
54
55 // Various leaf nodes.
56 Constant, ConstantFP, GlobalAddress, FrameIndex, ConstantPool,
Chris Lattner63b570d2005-01-07 07:45:27 +000057 BasicBlock, ExternalSymbol,
58
59 // CopyToReg - This node has chain and child nodes, and an associated
60 // register number. The instruction selector must guarantee that the value
Chris Lattner18c2f132005-01-13 20:50:02 +000061 // of the value node is available in the register stored in the RegSDNode
62 // object.
Chris Lattner63b570d2005-01-07 07:45:27 +000063 CopyToReg,
64
65 // CopyFromReg - This node indicates that the input value is a virtual or
66 // physical register that is defined outside of the scope of this
Chris Lattner18c2f132005-01-13 20:50:02 +000067 // SelectionDAG. The register is available from the RegSDNode object.
Chris Lattner63b570d2005-01-07 07:45:27 +000068 CopyFromReg,
69
Chris Lattner18c2f132005-01-13 20:50:02 +000070 // ImplicitDef - This node indicates that the specified register is
71 // implicitly defined by some operation (e.g. its a live-in argument). This
72 // register is indicated in the RegSDNode object. The only operand to this
73 // is the token chain coming in, the only result is the token chain going
74 // out.
75 ImplicitDef,
76
Nate Begemanfc1b1da2005-04-01 22:34:39 +000077 // UNDEF - An undefined node
78 UNDEF,
79
Chris Lattner63b570d2005-01-07 07:45:27 +000080 // EXTRACT_ELEMENT - This is used to get the first or second (determined by
81 // a Constant, which is required to be operand #1), element of the aggregate
82 // value specified as operand #0. This is only for use before legalization,
83 // for values that will be broken into multiple registers.
84 EXTRACT_ELEMENT,
85
86 // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways. Given
87 // two values of the same integer value type, this produces a value twice as
88 // big. Like EXTRACT_ELEMENT, this can only be used before legalization.
89 BUILD_PAIR,
90
91
92 // Simple binary arithmetic operators.
Chris Lattnerbede0b72005-04-06 04:21:29 +000093 ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
94
95 // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing
96 // an unsigned/signed value of type i[2*n], then return the top part.
97 MULHU, MULHS,
Chris Lattner63b570d2005-01-07 07:45:27 +000098
99 // Bitwise operators.
100 AND, OR, XOR, SHL, SRA, SRL,
101
102 // Select operator.
103 SELECT,
104
105 // SetCC operator - This evaluates to a boolean (i1) true value if the
106 // condition is true. These nodes are instances of the
107 // SetCCSDNode class, which contains the condition code as extra
108 // state.
109 SETCC,
110
Chris Lattner5880b9f2005-01-20 18:50:39 +0000111 // ADD_PARTS/SUB_PARTS - These operators take two logical operands which are
112 // broken into a multiple pieces each, and return the resulting pieces of
113 // doing an atomic add/sub operation. This is used to handle add/sub of
114 // expanded types. The operation ordering is:
115 // [Lo,Hi] = op [LoLHS,HiLHS], [LoRHS,HiRHS]
116 ADD_PARTS, SUB_PARTS,
Chris Lattner63b570d2005-01-07 07:45:27 +0000117
Chris Lattner14c5b532005-04-02 03:30:33 +0000118 // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded
119 // integer shift operations, just like ADD/SUB_PARTS. The operation
120 // ordering is:
Chris Lattner6b8f2d62005-04-02 03:59:45 +0000121 // [Lo,Hi] = op [LoLHS,HiLHS], Amt
Chris Lattner14c5b532005-04-02 03:30:33 +0000122 SHL_PARTS, SRA_PARTS, SRL_PARTS,
123
Chris Lattner63b570d2005-01-07 07:45:27 +0000124 // Conversion operators. These are all single input single output
125 // operations. For all of these, the result type must be strictly
126 // wider or narrower (depending on the operation) than the source
127 // type.
128
129 // SIGN_EXTEND - Used for integer types, replicating the sign bit
130 // into new bits.
131 SIGN_EXTEND,
132
133 // ZERO_EXTEND - Used for integer types, zeroing the new bits.
134 ZERO_EXTEND,
135
136 // TRUNCATE - Completely drop the high bits.
137 TRUNCATE,
138
Chris Lattner1645ed02005-01-08 08:08:49 +0000139 // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
140 // depends on the first letter) to floating point.
141 SINT_TO_FP,
142 UINT_TO_FP,
143
Chris Lattner859157d2005-01-15 06:17:04 +0000144 // SIGN_EXTEND_INREG/ZERO_EXTEND_INREG - These operators atomically performs
145 // a SHL/(SRA|SHL) pair to (sign|zero) extend a small value in a large
146 // integer register (e.g. sign extending the low 8 bits of a 32-bit register
147 // to fill the top 24 bits with the 7th bit). The size of the smaller type
148 // is indicated by the ExtraValueType in the MVTSDNode for the operator.
149 SIGN_EXTEND_INREG,
150 ZERO_EXTEND_INREG,
151
Chris Lattner1645ed02005-01-08 08:08:49 +0000152 // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
153 // integer.
154 FP_TO_SINT,
155 FP_TO_UINT,
156
Chris Lattner63b570d2005-01-07 07:45:27 +0000157 // FP_ROUND - Perform a rounding operation from the current
Chris Lattner859157d2005-01-15 06:17:04 +0000158 // precision down to the specified precision (currently always 64->32).
Chris Lattner63b570d2005-01-07 07:45:27 +0000159 FP_ROUND,
160
Chris Lattner859157d2005-01-15 06:17:04 +0000161 // FP_ROUND_INREG - This operator takes a floating point register, and
162 // rounds it to a floating point value. It then promotes it and returns it
163 // in a register of the same size. This operation effectively just discards
164 // excess precision. The type to round down to is specified by the
165 // ExtraValueType in the MVTSDNode (currently always 64->32->64).
166 FP_ROUND_INREG,
167
Chris Lattner63b570d2005-01-07 07:45:27 +0000168 // FP_EXTEND - Extend a smaller FP type into a larger FP type.
169 FP_EXTEND,
170
Chris Lattner7366fd32005-04-02 04:58:28 +0000171 // FNEG, FABS - Perform unary floating point negation and absolute value
172 // operations.
173 FNEG, FABS,
174
Chris Lattner1cff05c2005-01-14 22:07:46 +0000175 // Other operators. LOAD and STORE have token chains as their first
176 // operand, then the same operands as an LLVM load/store instruction.
Chris Lattner63b570d2005-01-07 07:45:27 +0000177 LOAD, STORE,
178
Chris Lattner1cff05c2005-01-14 22:07:46 +0000179 // EXTLOAD, SEXTLOAD, ZEXTLOAD - These three operators are instances of the
180 // MVTSDNode. All of these load a value from memory and extend them to a
181 // larger value (e.g. load a byte into a word register). All three of these
182 // have two operands, a chain and a pointer to load from. The extra value
183 // type is the source type being loaded.
184 //
185 // SEXTLOAD loads the integer operand and sign extends it to a larger
186 // integer result type.
187 // ZEXTLOAD loads the integer operand and zero extends it to a larger
188 // integer result type.
189 // EXTLOAD is used for two things: floating point extending loads, and
190 // integer extending loads where it doesn't matter what the high
191 // bits are set to. The code generator is allowed to codegen this
192 // into whichever operation is more efficient.
193 EXTLOAD, SEXTLOAD, ZEXTLOAD,
194
195 // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a
196 // value and stores it to memory in one operation. This can be used for
197 // either integer or floating point operands, and the stored type
198 // represented as the 'extra' value type in the MVTSDNode representing the
199 // operator. This node has the same three operands as a standard store.
200 TRUNCSTORE,
201
Chris Lattner63b570d2005-01-07 07:45:27 +0000202 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
203 // to a specified boundary. The first operand is the token chain, the
204 // second is the number of bytes to allocate, and the third is the alignment
205 // boundary.
206 DYNAMIC_STACKALLOC,
207
208 // Control flow instructions. These all have token chains.
209
210 // BR - Unconditional branch. The first operand is the chain
211 // operand, the second is the MBB to branch to.
212 BR,
213
214 // BRCOND - Conditional branch. The first operand is the chain,
215 // the second is the condition, the third is the block to branch
216 // to if the condition is true.
217 BRCOND,
218
Chris Lattner1df63382005-04-09 03:21:50 +0000219 // BRCONDTWOWAY - Two-way conditional branch. The first operand is the
220 // chain, the second is the condition, the third is the block to branch to
221 // if true, and the forth is the block to branch to if false. Targets
222 // usually do not implement this, preferring to have legalize demote the
223 // operation to BRCOND/BR pairs when necessary.
224 BRCONDTWOWAY,
225
Chris Lattner63b570d2005-01-07 07:45:27 +0000226 // RET - Return from function. The first operand is the chain,
227 // and any subsequent operands are the return values for the
228 // function. This operation can have variable number of operands.
229 RET,
230
231 // CALL - Call to a function pointer. The first operand is the chain, the
232 // second is the destination function pointer (a GlobalAddress for a direct
233 // call). Arguments have already been lowered to explicit DAGs according to
234 // the calling convention in effect here.
235 CALL,
Chris Lattneref36aa72005-01-11 05:56:17 +0000236
237 // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest
238 // correspond to the operands of the LLVM intrinsic functions. The only
239 // result is a token chain. The alignment argument is guaranteed to be a
240 // Constant node.
241 MEMSET,
242 MEMMOVE,
243 MEMCPY,
Chris Lattner63b570d2005-01-07 07:45:27 +0000244
245 // ADJCALLSTACKDOWN/ADJCALLSTACKUP - These operators mark the beginning and
246 // end of a call sequence and indicate how much the stack pointer needs to
247 // be adjusted for that particular call. The first operand is a chain, the
248 // second is a ConstantSDNode of intptr type.
249 ADJCALLSTACKDOWN, // Beginning of a call sequence
250 ADJCALLSTACKUP, // End of a call sequence
251
Misha Brukmane3f570c2005-03-31 21:30:35 +0000252 // PCMARKER - This corresponds to the pcmarker intrinsic.
Andrew Lenharth95762122005-03-31 21:24:06 +0000253 PCMARKER,
Chris Lattner63b570d2005-01-07 07:45:27 +0000254
255 // BUILTIN_OP_END - This must be the last enum value in this list.
256 BUILTIN_OP_END,
257 };
258
259 //===--------------------------------------------------------------------===//
260 /// ISD::CondCode enum - These are ordered carefully to make the bitfields
261 /// below work out, when considering SETFALSE (something that never exists
262 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered
263 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
264 /// to. If the "N" column is 1, the result of the comparison is undefined if
265 /// the input is a NAN.
266 ///
267 /// All of these (except for the 'always folded ops') should be handled for
268 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
269 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
270 ///
271 /// Note that these are laid out in a specific order to allow bit-twiddling
272 /// to transform conditions.
273 enum CondCode {
274 // Opcode N U L G E Intuitive operation
275 SETFALSE, // 0 0 0 0 Always false (always folded)
276 SETOEQ, // 0 0 0 1 True if ordered and equal
277 SETOGT, // 0 0 1 0 True if ordered and greater than
278 SETOGE, // 0 0 1 1 True if ordered and greater than or equal
279 SETOLT, // 0 1 0 0 True if ordered and less than
280 SETOLE, // 0 1 0 1 True if ordered and less than or equal
281 SETONE, // 0 1 1 0 True if ordered and operands are unequal
282 SETO, // 0 1 1 1 True if ordered (no nans)
283 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
284 SETUEQ, // 1 0 0 1 True if unordered or equal
285 SETUGT, // 1 0 1 0 True if unordered or greater than
286 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal
287 SETULT, // 1 1 0 0 True if unordered or less than
288 SETULE, // 1 1 0 1 True if unordered, less than, or equal
289 SETUNE, // 1 1 1 0 True if unordered or not equal
290 SETTRUE, // 1 1 1 1 Always true (always folded)
291 // Don't care operations: undefined if the input is a nan.
292 SETFALSE2, // 1 X 0 0 0 Always false (always folded)
293 SETEQ, // 1 X 0 0 1 True if equal
294 SETGT, // 1 X 0 1 0 True if greater than
295 SETGE, // 1 X 0 1 1 True if greater than or equal
296 SETLT, // 1 X 1 0 0 True if less than
297 SETLE, // 1 X 1 0 1 True if less than or equal
298 SETNE, // 1 X 1 1 0 True if not equal
299 SETTRUE2, // 1 X 1 1 1 Always true (always folded)
300
301 SETCC_INVALID, // Marker value.
302 };
303
304 /// isSignedIntSetCC - Return true if this is a setcc instruction that
305 /// performs a signed comparison when used with integer operands.
306 inline bool isSignedIntSetCC(CondCode Code) {
307 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
308 }
309
310 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
311 /// performs an unsigned comparison when used with integer operands.
312 inline bool isUnsignedIntSetCC(CondCode Code) {
313 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
314 }
315
316 /// isTrueWhenEqual - Return true if the specified condition returns true if
317 /// the two operands to the condition are equal. Note that if one of the two
318 /// operands is a NaN, this value is meaningless.
319 inline bool isTrueWhenEqual(CondCode Cond) {
320 return ((int)Cond & 1) != 0;
321 }
322
323 /// getUnorderedFlavor - This function returns 0 if the condition is always
324 /// false if an operand is a NaN, 1 if the condition is always true if the
325 /// operand is a NaN, and 2 if the condition is undefined if the operand is a
326 /// NaN.
327 inline unsigned getUnorderedFlavor(CondCode Cond) {
328 return ((int)Cond >> 3) & 3;
329 }
330
331 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
332 /// 'op' is a valid SetCC operation.
333 CondCode getSetCCInverse(CondCode Operation, bool isInteger);
334
335 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
336 /// when given the operation for (X op Y).
337 CondCode getSetCCSwappedOperands(CondCode Operation);
338
339 /// getSetCCOrOperation - Return the result of a logical OR between different
340 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This
341 /// function returns SETCC_INVALID if it is not possible to represent the
342 /// resultant comparison.
343 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
344
345 /// getSetCCAndOperation - Return the result of a logical AND between
346 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
347 /// function returns SETCC_INVALID if it is not possible to represent the
348 /// resultant comparison.
349 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
350} // end llvm::ISD namespace
351
352
353//===----------------------------------------------------------------------===//
354/// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
355/// values as the result of a computation. Many nodes return multiple values,
356/// from loads (which define a token and a return value) to ADDC (which returns
357/// a result and a carry value), to calls (which may return an arbitrary number
358/// of values).
359///
360/// As such, each use of a SelectionDAG computation must indicate the node that
361/// computes it as well as which return value to use from that node. This pair
362/// of information is represented with the SDOperand value type.
363///
Chris Lattnerf26bc8e2005-01-08 19:52:31 +0000364class SDOperand {
365public:
Chris Lattner63b570d2005-01-07 07:45:27 +0000366 SDNode *Val; // The node defining the value we are using.
367 unsigned ResNo; // Which return value of the node we are using.
368
369 SDOperand() : Val(0) {}
370 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
371
372 bool operator==(const SDOperand &O) const {
373 return Val == O.Val && ResNo == O.ResNo;
374 }
375 bool operator!=(const SDOperand &O) const {
376 return !operator==(O);
377 }
378 bool operator<(const SDOperand &O) const {
379 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
380 }
381
382 SDOperand getValue(unsigned R) const {
383 return SDOperand(Val, R);
384 }
385
386 /// getValueType - Return the ValueType of the referenced return value.
387 ///
388 inline MVT::ValueType getValueType() const;
Chris Lattner0442fbf2005-01-21 21:39:38 +0000389
Chris Lattner63b570d2005-01-07 07:45:27 +0000390 // Forwarding methods - These forward to the corresponding methods in SDNode.
391 inline unsigned getOpcode() const;
Chris Lattner0442fbf2005-01-21 21:39:38 +0000392 inline unsigned getNodeDepth() const;
Chris Lattner63b570d2005-01-07 07:45:27 +0000393 inline unsigned getNumOperands() const;
394 inline const SDOperand &getOperand(unsigned i) const;
Chris Lattnera44f4ae2005-01-13 22:58:50 +0000395
396 /// hasOneUse - Return true if there is exactly one operation using this
397 /// result value of the defining operator.
398 inline bool hasOneUse() const;
Chris Lattner63b570d2005-01-07 07:45:27 +0000399};
400
401
402/// simplify_type specializations - Allow casting operators to work directly on
403/// SDOperands as if they were SDNode*'s.
404template<> struct simplify_type<SDOperand> {
405 typedef SDNode* SimpleType;
406 static SimpleType getSimplifiedValue(const SDOperand &Val) {
407 return static_cast<SimpleType>(Val.Val);
408 }
409};
410template<> struct simplify_type<const SDOperand> {
411 typedef SDNode* SimpleType;
412 static SimpleType getSimplifiedValue(const SDOperand &Val) {
413 return static_cast<SimpleType>(Val.Val);
414 }
415};
416
417
418/// SDNode - Represents one node in the SelectionDAG.
419///
420class SDNode {
Chris Lattner0442fbf2005-01-21 21:39:38 +0000421 /// NodeType - The operation that this node performs.
422 ///
423 unsigned short NodeType;
424
425 /// NodeDepth - Node depth is defined as MAX(Node depth of children)+1. This
426 /// means that leaves have a depth of 1, things that use only leaves have a
427 /// depth of 2, etc.
428 unsigned short NodeDepth;
429
430 /// Operands - The values that are used by this operation.
431 ///
Chris Lattner63b570d2005-01-07 07:45:27 +0000432 std::vector<SDOperand> Operands;
433
434 /// Values - The types of the values this node defines. SDNode's may define
435 /// multiple values simultaneously.
436 std::vector<MVT::ValueType> Values;
437
438 /// Uses - These are all of the SDNode's that use a value produced by this
439 /// node.
440 std::vector<SDNode*> Uses;
441public:
442
443 //===--------------------------------------------------------------------===//
444 // Accessors
445 //
446 unsigned getOpcode() const { return NodeType; }
447
448 size_t use_size() const { return Uses.size(); }
449 bool use_empty() const { return Uses.empty(); }
450 bool hasOneUse() const { return Uses.size() == 1; }
451
Chris Lattner0442fbf2005-01-21 21:39:38 +0000452 /// getNodeDepth - Return the distance from this node to the leaves in the
453 /// graph. The leaves have a depth of 1.
454 unsigned getNodeDepth() const { return NodeDepth; }
455
Chris Lattner7ece3802005-01-17 02:24:59 +0000456 typedef std::vector<SDNode*>::const_iterator use_iterator;
457 use_iterator use_begin() const { return Uses.begin(); }
458 use_iterator use_end() const { return Uses.end(); }
459
Chris Lattnerb18a2f82005-01-12 18:37:33 +0000460 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
461 /// indicated value. This method ignores uses of other values defined by this
462 /// operation.
463 bool hasNUsesOfValue(unsigned NUses, unsigned Value);
464
Chris Lattner63b570d2005-01-07 07:45:27 +0000465 /// getNumOperands - Return the number of values used by this operation.
466 ///
467 unsigned getNumOperands() const { return Operands.size(); }
468
469 const SDOperand &getOperand(unsigned Num) {
470 assert(Num < Operands.size() && "Invalid child # of SDNode!");
471 return Operands[Num];
472 }
473
474 const SDOperand &getOperand(unsigned Num) const {
475 assert(Num < Operands.size() && "Invalid child # of SDNode!");
476 return Operands[Num];
477 }
478
479 /// getNumValues - Return the number of values defined/returned by this
480 /// operator.
481 ///
482 unsigned getNumValues() const { return Values.size(); }
483
484 /// getValueType - Return the type of a specified result.
485 ///
486 MVT::ValueType getValueType(unsigned ResNo) const {
487 assert(ResNo < Values.size() && "Illegal result number!");
488 return Values[ResNo];
489 }
490
Chris Lattner6e6e3ce2005-01-10 23:25:04 +0000491 /// getOperationName - Return the opcode of this operation for printing.
492 ///
493 const char* getOperationName() const;
Chris Lattner63b570d2005-01-07 07:45:27 +0000494 void dump() const;
495
496 static bool classof(const SDNode *) { return true; }
497
498protected:
499 friend class SelectionDAG;
500
Chris Lattner0442fbf2005-01-21 21:39:38 +0000501 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT), NodeDepth(1) {
Chris Lattner63b570d2005-01-07 07:45:27 +0000502 Values.reserve(1);
503 Values.push_back(VT);
504 }
Chris Lattner63b570d2005-01-07 07:45:27 +0000505 SDNode(unsigned NT, SDOperand Op)
Chris Lattner0442fbf2005-01-21 21:39:38 +0000506 : NodeType(NT), NodeDepth(Op.Val->getNodeDepth()+1) {
Chris Lattner63b570d2005-01-07 07:45:27 +0000507 Operands.reserve(1); Operands.push_back(Op);
508 Op.Val->Uses.push_back(this);
509 }
510 SDNode(unsigned NT, SDOperand N1, SDOperand N2)
511 : NodeType(NT) {
Chris Lattner0442fbf2005-01-21 21:39:38 +0000512 if (N1.Val->getNodeDepth() > N2.Val->getNodeDepth())
513 NodeDepth = N1.Val->getNodeDepth()+1;
514 else
515 NodeDepth = N2.Val->getNodeDepth()+1;
Chris Lattner63b570d2005-01-07 07:45:27 +0000516 Operands.reserve(2); Operands.push_back(N1); Operands.push_back(N2);
517 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
518 }
519 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
520 : NodeType(NT) {
Chris Lattner0442fbf2005-01-21 21:39:38 +0000521 unsigned ND = N1.Val->getNodeDepth();
522 if (ND < N2.Val->getNodeDepth())
523 ND = N2.Val->getNodeDepth();
524 if (ND < N3.Val->getNodeDepth())
525 ND = N3.Val->getNodeDepth();
526 NodeDepth = ND+1;
527
Chris Lattner63b570d2005-01-07 07:45:27 +0000528 Operands.reserve(3); Operands.push_back(N1); Operands.push_back(N2);
529 Operands.push_back(N3);
530 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
531 N3.Val->Uses.push_back(this);
532 }
533 SDNode(unsigned NT, std::vector<SDOperand> &Nodes) : NodeType(NT) {
534 Operands.swap(Nodes);
Chris Lattner0442fbf2005-01-21 21:39:38 +0000535 unsigned ND = 0;
536 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattneref36aa72005-01-11 05:56:17 +0000537 Operands[i].Val->Uses.push_back(this);
Chris Lattner0442fbf2005-01-21 21:39:38 +0000538 if (ND < Operands[i].Val->getNodeDepth())
539 ND = Operands[i].Val->getNodeDepth();
540 }
541 NodeDepth = ND+1;
Chris Lattner63b570d2005-01-07 07:45:27 +0000542 }
543
544 virtual ~SDNode() {
545 // FIXME: Drop uses.
546 }
547
548 void setValueTypes(MVT::ValueType VT) {
549 Values.reserve(1);
550 Values.push_back(VT);
551 }
552 void setValueTypes(MVT::ValueType VT1, MVT::ValueType VT2) {
553 Values.reserve(2);
554 Values.push_back(VT1);
555 Values.push_back(VT2);
556 }
557 /// Note: this method destroys the vector passed in.
558 void setValueTypes(std::vector<MVT::ValueType> &VTs) {
559 std::swap(Values, VTs);
560 }
Chris Lattnerd1fc9642005-01-07 21:08:55 +0000561
562 void removeUser(SDNode *User) {
563 // Remove this user from the operand's use list.
564 for (unsigned i = Uses.size(); ; --i) {
565 assert(i != 0 && "Didn't find user!");
566 if (Uses[i-1] == User) {
567 Uses.erase(Uses.begin()+i-1);
568 break;
569 }
570 }
571 }
Chris Lattner63b570d2005-01-07 07:45:27 +0000572};
573
574
575// Define inline functions from the SDOperand class.
576
577inline unsigned SDOperand::getOpcode() const {
578 return Val->getOpcode();
579}
Chris Lattner0442fbf2005-01-21 21:39:38 +0000580inline unsigned SDOperand::getNodeDepth() const {
581 return Val->getNodeDepth();
582}
Chris Lattner63b570d2005-01-07 07:45:27 +0000583inline MVT::ValueType SDOperand::getValueType() const {
584 return Val->getValueType(ResNo);
585}
586inline unsigned SDOperand::getNumOperands() const {
587 return Val->getNumOperands();
588}
589inline const SDOperand &SDOperand::getOperand(unsigned i) const {
590 return Val->getOperand(i);
591}
Chris Lattnera44f4ae2005-01-13 22:58:50 +0000592inline bool SDOperand::hasOneUse() const {
593 return Val->hasNUsesOfValue(1, ResNo);
594}
Chris Lattner63b570d2005-01-07 07:45:27 +0000595
596
597class ConstantSDNode : public SDNode {
598 uint64_t Value;
599protected:
600 friend class SelectionDAG;
601 ConstantSDNode(uint64_t val, MVT::ValueType VT)
602 : SDNode(ISD::Constant, VT), Value(val) {
603 }
604public:
605
606 uint64_t getValue() const { return Value; }
607
608 int64_t getSignExtended() const {
609 unsigned Bits = MVT::getSizeInBits(getValueType(0));
Chris Lattnerf26bc8e2005-01-08 19:52:31 +0000610 return ((int64_t)Value << (64-Bits)) >> (64-Bits);
Chris Lattner63b570d2005-01-07 07:45:27 +0000611 }
612
613 bool isNullValue() const { return Value == 0; }
614 bool isAllOnesValue() const {
Chris Lattner2bffad32005-04-08 21:31:29 +0000615 int NumBits = MVT::getSizeInBits(getValueType(0));
616 if (NumBits == 64) return Value+1 == 0;
617 return Value == (1ULL << NumBits)-1;
Chris Lattner63b570d2005-01-07 07:45:27 +0000618 }
619
620 static bool classof(const ConstantSDNode *) { return true; }
621 static bool classof(const SDNode *N) {
622 return N->getOpcode() == ISD::Constant;
623 }
624};
625
626class ConstantFPSDNode : public SDNode {
627 double Value;
628protected:
629 friend class SelectionDAG;
630 ConstantFPSDNode(double val, MVT::ValueType VT)
631 : SDNode(ISD::ConstantFP, VT), Value(val) {
632 }
633public:
634
635 double getValue() const { return Value; }
636
637 /// isExactlyValue - We don't rely on operator== working on double values, as
638 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
639 /// As such, this method can be used to do an exact bit-for-bit comparison of
640 /// two floating point values.
641 bool isExactlyValue(double V) const {
642 union {
643 double V;
644 uint64_t I;
645 } T1;
646 T1.V = Value;
647 union {
648 double V;
649 uint64_t I;
650 } T2;
651 T2.V = V;
652 return T1.I == T2.I;
653 }
654
655 static bool classof(const ConstantFPSDNode *) { return true; }
656 static bool classof(const SDNode *N) {
657 return N->getOpcode() == ISD::ConstantFP;
658 }
659};
660
661class GlobalAddressSDNode : public SDNode {
662 GlobalValue *TheGlobal;
663protected:
664 friend class SelectionDAG;
665 GlobalAddressSDNode(const GlobalValue *GA, MVT::ValueType VT)
666 : SDNode(ISD::GlobalAddress, VT) {
667 TheGlobal = const_cast<GlobalValue*>(GA);
Chris Lattner63b570d2005-01-07 07:45:27 +0000668 }
669public:
670
671 GlobalValue *getGlobal() const { return TheGlobal; }
672
673 static bool classof(const GlobalAddressSDNode *) { return true; }
674 static bool classof(const SDNode *N) {
675 return N->getOpcode() == ISD::GlobalAddress;
676 }
677};
678
679
680class FrameIndexSDNode : public SDNode {
681 int FI;
682protected:
683 friend class SelectionDAG;
684 FrameIndexSDNode(int fi, MVT::ValueType VT)
685 : SDNode(ISD::FrameIndex, VT), FI(fi) {}
686public:
687
688 int getIndex() const { return FI; }
689
690 static bool classof(const FrameIndexSDNode *) { return true; }
691 static bool classof(const SDNode *N) {
692 return N->getOpcode() == ISD::FrameIndex;
693 }
694};
695
696class ConstantPoolSDNode : public SDNode {
697 unsigned CPI;
698protected:
699 friend class SelectionDAG;
700 ConstantPoolSDNode(unsigned cpi, MVT::ValueType VT)
701 : SDNode(ISD::ConstantPool, VT), CPI(cpi) {}
702public:
703
704 unsigned getIndex() const { return CPI; }
705
706 static bool classof(const ConstantPoolSDNode *) { return true; }
707 static bool classof(const SDNode *N) {
708 return N->getOpcode() == ISD::ConstantPool;
709 }
710};
711
712class BasicBlockSDNode : public SDNode {
713 MachineBasicBlock *MBB;
714protected:
715 friend class SelectionDAG;
716 BasicBlockSDNode(MachineBasicBlock *mbb)
717 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
718public:
719
720 MachineBasicBlock *getBasicBlock() const { return MBB; }
721
722 static bool classof(const BasicBlockSDNode *) { return true; }
723 static bool classof(const SDNode *N) {
724 return N->getOpcode() == ISD::BasicBlock;
725 }
726};
727
728
Chris Lattner18c2f132005-01-13 20:50:02 +0000729class RegSDNode : public SDNode {
Chris Lattner63b570d2005-01-07 07:45:27 +0000730 unsigned Reg;
731protected:
732 friend class SelectionDAG;
Chris Lattner60e48782005-01-14 22:37:20 +0000733 RegSDNode(unsigned Opc, SDOperand Chain, SDOperand Src, unsigned reg)
Chris Lattner1cff05c2005-01-14 22:07:46 +0000734 : SDNode(Opc, Chain, Src), Reg(reg) {
Chris Lattner63b570d2005-01-07 07:45:27 +0000735 }
Chris Lattner60e48782005-01-14 22:37:20 +0000736 RegSDNode(unsigned Opc, SDOperand Chain, unsigned reg)
737 : SDNode(Opc, Chain), Reg(reg) {}
Chris Lattner63b570d2005-01-07 07:45:27 +0000738public:
739
740 unsigned getReg() const { return Reg; }
741
Chris Lattner18c2f132005-01-13 20:50:02 +0000742 static bool classof(const RegSDNode *) { return true; }
Chris Lattner63b570d2005-01-07 07:45:27 +0000743 static bool classof(const SDNode *N) {
744 return N->getOpcode() == ISD::CopyToReg ||
Chris Lattner18c2f132005-01-13 20:50:02 +0000745 N->getOpcode() == ISD::CopyFromReg ||
746 N->getOpcode() == ISD::ImplicitDef;
Chris Lattner63b570d2005-01-07 07:45:27 +0000747 }
748};
749
750class ExternalSymbolSDNode : public SDNode {
751 const char *Symbol;
752protected:
753 friend class SelectionDAG;
754 ExternalSymbolSDNode(const char *Sym, MVT::ValueType VT)
755 : SDNode(ISD::ExternalSymbol, VT), Symbol(Sym) {
756 }
757public:
758
759 const char *getSymbol() const { return Symbol; }
760
761 static bool classof(const ExternalSymbolSDNode *) { return true; }
762 static bool classof(const SDNode *N) {
763 return N->getOpcode() == ISD::ExternalSymbol;
764 }
765};
766
767class SetCCSDNode : public SDNode {
768 ISD::CondCode Condition;
769protected:
770 friend class SelectionDAG;
771 SetCCSDNode(ISD::CondCode Cond, SDOperand LHS, SDOperand RHS)
772 : SDNode(ISD::SETCC, LHS, RHS), Condition(Cond) {
Chris Lattner63b570d2005-01-07 07:45:27 +0000773 }
774public:
775
776 ISD::CondCode getCondition() const { return Condition; }
777
778 static bool classof(const SetCCSDNode *) { return true; }
779 static bool classof(const SDNode *N) {
780 return N->getOpcode() == ISD::SETCC;
781 }
782};
783
Chris Lattner1cff05c2005-01-14 22:07:46 +0000784/// MVTSDNode - This class is used for operators that require an extra
785/// value-type to be kept with the node.
786class MVTSDNode : public SDNode {
787 MVT::ValueType ExtraValueType;
788protected:
789 friend class SelectionDAG;
Chris Lattner859157d2005-01-15 06:17:04 +0000790 MVTSDNode(unsigned Opc, MVT::ValueType VT1, SDOperand Op0, MVT::ValueType EVT)
791 : SDNode(Opc, Op0), ExtraValueType(EVT) {
792 setValueTypes(VT1);
793 }
Chris Lattner60e48782005-01-14 22:37:20 +0000794 MVTSDNode(unsigned Opc, MVT::ValueType VT1, MVT::ValueType VT2,
Chris Lattner1cff05c2005-01-14 22:07:46 +0000795 SDOperand Op0, SDOperand Op1, MVT::ValueType EVT)
796 : SDNode(Opc, Op0, Op1), ExtraValueType(EVT) {
Chris Lattner60e48782005-01-14 22:37:20 +0000797 setValueTypes(VT1, VT2);
Chris Lattner1cff05c2005-01-14 22:07:46 +0000798 }
799 MVTSDNode(unsigned Opc, MVT::ValueType VT,
800 SDOperand Op0, SDOperand Op1, SDOperand Op2, MVT::ValueType EVT)
801 : SDNode(Opc, Op0, Op1, Op2), ExtraValueType(EVT) {
802 setValueTypes(VT);
803 }
804public:
805
806 MVT::ValueType getExtraValueType() const { return ExtraValueType; }
807
808 static bool classof(const MVTSDNode *) { return true; }
809 static bool classof(const SDNode *N) {
810 return
Chris Lattner859157d2005-01-15 06:17:04 +0000811 N->getOpcode() == ISD::SIGN_EXTEND_INREG ||
812 N->getOpcode() == ISD::ZERO_EXTEND_INREG ||
813 N->getOpcode() == ISD::FP_ROUND_INREG ||
Chris Lattner1cff05c2005-01-14 22:07:46 +0000814 N->getOpcode() == ISD::EXTLOAD ||
815 N->getOpcode() == ISD::SEXTLOAD ||
816 N->getOpcode() == ISD::ZEXTLOAD ||
817 N->getOpcode() == ISD::TRUNCSTORE;
818 }
819};
Chris Lattner1080b9e2005-01-10 23:05:53 +0000820
821class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
822 SDNode *Node;
823 unsigned Operand;
824
825 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
826public:
827 bool operator==(const SDNodeIterator& x) const {
828 return Operand == x.Operand;
829 }
830 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
831
832 const SDNodeIterator &operator=(const SDNodeIterator &I) {
833 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
834 Operand = I.Operand;
835 return *this;
836 }
837
838 pointer operator*() const {
839 return Node->getOperand(Operand).Val;
840 }
841 pointer operator->() const { return operator*(); }
842
843 SDNodeIterator& operator++() { // Preincrement
844 ++Operand;
845 return *this;
846 }
847 SDNodeIterator operator++(int) { // Postincrement
848 SDNodeIterator tmp = *this; ++*this; return tmp;
849 }
850
851 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
852 static SDNodeIterator end (SDNode *N) {
853 return SDNodeIterator(N, N->getNumOperands());
854 }
855
856 unsigned getOperand() const { return Operand; }
857 const SDNode *getNode() const { return Node; }
858};
859
860template <> struct GraphTraits<SDNode*> {
861 typedef SDNode NodeType;
862 typedef SDNodeIterator ChildIteratorType;
863 static inline NodeType *getEntryNode(SDNode *N) { return N; }
864 static inline ChildIteratorType child_begin(NodeType *N) {
865 return SDNodeIterator::begin(N);
866 }
867 static inline ChildIteratorType child_end(NodeType *N) {
868 return SDNodeIterator::end(N);
869 }
870};
871
872
873
874
Chris Lattner63b570d2005-01-07 07:45:27 +0000875} // end llvm namespace
876
877#endif