blob: 06a7b20ff9cb5b1e9df6ca2bea7825ca98eb9d17 [file] [log] [blame]
Chris Lattner63b570d2005-01-07 07:45:27 +00001//===-- llvm/CodeGen/SelectionDAGNodes.h - SelectionDAG Nodes ---*- C++ -*-===//
Misha Brukmanea61c352005-04-21 20:39:54 +00002//
Chris Lattner63b570d2005-01-07 07:45:27 +00003// 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.
Misha Brukmanea61c352005-04-21 20:39:54 +00007//
Chris Lattner63b570d2005-01-07 07:45:27 +00008//===----------------------------------------------------------------------===//
Misha Brukmanea61c352005-04-21 20:39:54 +00009//
Chris Lattner63b570d2005-01-07 07:45:27 +000010// 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"
Andrew Lenharth2d86ea22005-04-27 20:10:01 +000023#include "llvm/Value.h"
Chris Lattner1080b9e2005-01-10 23:05:53 +000024#include "llvm/ADT/GraphTraits.h"
25#include "llvm/ADT/GraphTraits.h"
26#include "llvm/ADT/iterator"
Jeff Cohen39931a32005-01-07 19:21:49 +000027#include "llvm/Support/DataTypes.h"
Chris Lattner63b570d2005-01-07 07:45:27 +000028#include <cassert>
29#include <vector>
30
31namespace llvm {
32
33class SelectionDAG;
34class GlobalValue;
35class MachineBasicBlock;
36class SDNode;
37template <typename T> struct simplify_type;
38
39/// ISD namespace - This namespace contains an enum which represents all of the
40/// SelectionDAG node types and value types.
41///
42namespace ISD {
43 //===--------------------------------------------------------------------===//
44 /// ISD::NodeType enum - This enum defines all of the operators valid in a
45 /// SelectionDAG.
46 ///
47 enum NodeType {
Chris Lattner8a496fc2005-01-13 17:58:35 +000048 // EntryToken - This is the marker used to indicate the start of the region.
49 EntryToken,
50
51 // Token factor - This node is takes multiple tokens as input and produces a
52 // single token result. This is used to represent the fact that the operand
53 // operators are independent of each other.
54 TokenFactor,
Misha Brukmanea61c352005-04-21 20:39:54 +000055
Chris Lattner8a496fc2005-01-13 17:58:35 +000056 // Various leaf nodes.
57 Constant, ConstantFP, GlobalAddress, FrameIndex, ConstantPool,
Chris Lattner63b570d2005-01-07 07:45:27 +000058 BasicBlock, ExternalSymbol,
59
60 // CopyToReg - This node has chain and child nodes, and an associated
61 // register number. The instruction selector must guarantee that the value
Chris Lattner18c2f132005-01-13 20:50:02 +000062 // of the value node is available in the register stored in the RegSDNode
63 // object.
Chris Lattner63b570d2005-01-07 07:45:27 +000064 CopyToReg,
65
66 // CopyFromReg - This node indicates that the input value is a virtual or
67 // physical register that is defined outside of the scope of this
Chris Lattner18c2f132005-01-13 20:50:02 +000068 // SelectionDAG. The register is available from the RegSDNode object.
Chris Lattner63b570d2005-01-07 07:45:27 +000069 CopyFromReg,
70
Chris Lattner18c2f132005-01-13 20:50:02 +000071 // ImplicitDef - This node indicates that the specified register is
72 // implicitly defined by some operation (e.g. its a live-in argument). This
73 // register is indicated in the RegSDNode object. The only operand to this
74 // is the token chain coming in, the only result is the token chain going
75 // out.
76 ImplicitDef,
77
Nate Begemanfc1b1da2005-04-01 22:34:39 +000078 // UNDEF - An undefined node
79 UNDEF,
80
Chris Lattner63b570d2005-01-07 07:45:27 +000081 // EXTRACT_ELEMENT - This is used to get the first or second (determined by
82 // a Constant, which is required to be operand #1), element of the aggregate
83 // value specified as operand #0. This is only for use before legalization,
84 // for values that will be broken into multiple registers.
85 EXTRACT_ELEMENT,
86
87 // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways. Given
88 // two values of the same integer value type, this produces a value twice as
89 // big. Like EXTRACT_ELEMENT, this can only be used before legalization.
90 BUILD_PAIR,
91
92
93 // Simple binary arithmetic operators.
Chris Lattnerbede0b72005-04-06 04:21:29 +000094 ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
95
96 // MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing
97 // an unsigned/signed value of type i[2*n], then return the top part.
98 MULHU, MULHS,
Chris Lattner63b570d2005-01-07 07:45:27 +000099
100 // Bitwise operators.
101 AND, OR, XOR, SHL, SRA, SRL,
102
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000103 // Counting operators
104 CTTZ, CTLZ, CTPOP,
105
Chris Lattner63b570d2005-01-07 07:45:27 +0000106 // Select operator.
107 SELECT,
108
109 // SetCC operator - This evaluates to a boolean (i1) true value if the
110 // condition is true. These nodes are instances of the
111 // SetCCSDNode class, which contains the condition code as extra
112 // state.
113 SETCC,
114
Chris Lattner5880b9f2005-01-20 18:50:39 +0000115 // ADD_PARTS/SUB_PARTS - These operators take two logical operands which are
116 // broken into a multiple pieces each, and return the resulting pieces of
117 // doing an atomic add/sub operation. This is used to handle add/sub of
118 // expanded types. The operation ordering is:
119 // [Lo,Hi] = op [LoLHS,HiLHS], [LoRHS,HiRHS]
120 ADD_PARTS, SUB_PARTS,
Chris Lattner63b570d2005-01-07 07:45:27 +0000121
Chris Lattner14c5b532005-04-02 03:30:33 +0000122 // SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded
123 // integer shift operations, just like ADD/SUB_PARTS. The operation
124 // ordering is:
Chris Lattner6b8f2d62005-04-02 03:59:45 +0000125 // [Lo,Hi] = op [LoLHS,HiLHS], Amt
Chris Lattner14c5b532005-04-02 03:30:33 +0000126 SHL_PARTS, SRA_PARTS, SRL_PARTS,
127
Chris Lattner63b570d2005-01-07 07:45:27 +0000128 // Conversion operators. These are all single input single output
129 // operations. For all of these, the result type must be strictly
130 // wider or narrower (depending on the operation) than the source
131 // type.
132
133 // SIGN_EXTEND - Used for integer types, replicating the sign bit
134 // into new bits.
135 SIGN_EXTEND,
136
137 // ZERO_EXTEND - Used for integer types, zeroing the new bits.
138 ZERO_EXTEND,
139
140 // TRUNCATE - Completely drop the high bits.
141 TRUNCATE,
142
Chris Lattner1645ed02005-01-08 08:08:49 +0000143 // [SU]INT_TO_FP - These operators convert integers (whose interpreted sign
144 // depends on the first letter) to floating point.
145 SINT_TO_FP,
146 UINT_TO_FP,
147
Chris Lattnerea576102005-04-13 02:36:41 +0000148 // SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to
149 // sign extend a small value in a large integer register (e.g. sign
150 // extending the low 8 bits of a 32-bit register to fill the top 24 bits
151 // with the 7th bit). The size of the smaller type is indicated by the
152 // ExtraValueType in the MVTSDNode for the operator.
Chris Lattner859157d2005-01-15 06:17:04 +0000153 SIGN_EXTEND_INREG,
Chris Lattner859157d2005-01-15 06:17:04 +0000154
Chris Lattner1645ed02005-01-08 08:08:49 +0000155 // FP_TO_[US]INT - Convert a floating point value to a signed or unsigned
156 // integer.
157 FP_TO_SINT,
158 FP_TO_UINT,
159
Chris Lattner63b570d2005-01-07 07:45:27 +0000160 // FP_ROUND - Perform a rounding operation from the current
Chris Lattner859157d2005-01-15 06:17:04 +0000161 // precision down to the specified precision (currently always 64->32).
Chris Lattner63b570d2005-01-07 07:45:27 +0000162 FP_ROUND,
163
Chris Lattner859157d2005-01-15 06:17:04 +0000164 // FP_ROUND_INREG - This operator takes a floating point register, and
165 // rounds it to a floating point value. It then promotes it and returns it
166 // in a register of the same size. This operation effectively just discards
167 // excess precision. The type to round down to is specified by the
168 // ExtraValueType in the MVTSDNode (currently always 64->32->64).
169 FP_ROUND_INREG,
170
Chris Lattner63b570d2005-01-07 07:45:27 +0000171 // FP_EXTEND - Extend a smaller FP type into a larger FP type.
172 FP_EXTEND,
173
Chris Lattner7f644642005-04-28 21:44:03 +0000174 // FNEG, FABS, FSQRT, FSIN, FCOS - Perform unary floating point negation,
175 // absolute value, square root, sine and cosine operations.
176 FNEG, FABS, FSQRT, FSIN, FCOS,
Chris Lattner7366fd32005-04-02 04:58:28 +0000177
Chris Lattner1cff05c2005-01-14 22:07:46 +0000178 // Other operators. LOAD and STORE have token chains as their first
179 // operand, then the same operands as an LLVM load/store instruction.
Chris Lattner63b570d2005-01-07 07:45:27 +0000180 LOAD, STORE,
181
Chris Lattner1cff05c2005-01-14 22:07:46 +0000182 // EXTLOAD, SEXTLOAD, ZEXTLOAD - These three operators are instances of the
183 // MVTSDNode. All of these load a value from memory and extend them to a
184 // larger value (e.g. load a byte into a word register). All three of these
185 // have two operands, a chain and a pointer to load from. The extra value
186 // type is the source type being loaded.
187 //
188 // SEXTLOAD loads the integer operand and sign extends it to a larger
189 // integer result type.
190 // ZEXTLOAD loads the integer operand and zero extends it to a larger
191 // integer result type.
Misha Brukmanea61c352005-04-21 20:39:54 +0000192 // EXTLOAD is used for two things: floating point extending loads, and
Chris Lattner1cff05c2005-01-14 22:07:46 +0000193 // integer extending loads where it doesn't matter what the high
194 // bits are set to. The code generator is allowed to codegen this
195 // into whichever operation is more efficient.
196 EXTLOAD, SEXTLOAD, ZEXTLOAD,
197
198 // TRUNCSTORE - This operators truncates (for integer) or rounds (for FP) a
199 // value and stores it to memory in one operation. This can be used for
200 // either integer or floating point operands, and the stored type
201 // represented as the 'extra' value type in the MVTSDNode representing the
202 // operator. This node has the same three operands as a standard store.
203 TRUNCSTORE,
204
Chris Lattner63b570d2005-01-07 07:45:27 +0000205 // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
206 // to a specified boundary. The first operand is the token chain, the
207 // second is the number of bytes to allocate, and the third is the alignment
208 // boundary.
209 DYNAMIC_STACKALLOC,
210
211 // Control flow instructions. These all have token chains.
Misha Brukmanea61c352005-04-21 20:39:54 +0000212
Chris Lattner63b570d2005-01-07 07:45:27 +0000213 // BR - Unconditional branch. The first operand is the chain
214 // operand, the second is the MBB to branch to.
215 BR,
216
217 // BRCOND - Conditional branch. The first operand is the chain,
218 // the second is the condition, the third is the block to branch
219 // to if the condition is true.
220 BRCOND,
221
Chris Lattner1df63382005-04-09 03:21:50 +0000222 // BRCONDTWOWAY - Two-way conditional branch. The first operand is the
223 // chain, the second is the condition, the third is the block to branch to
224 // if true, and the forth is the block to branch to if false. Targets
225 // usually do not implement this, preferring to have legalize demote the
226 // operation to BRCOND/BR pairs when necessary.
227 BRCONDTWOWAY,
228
Chris Lattner63b570d2005-01-07 07:45:27 +0000229 // RET - Return from function. The first operand is the chain,
230 // and any subsequent operands are the return values for the
231 // function. This operation can have variable number of operands.
232 RET,
233
234 // CALL - Call to a function pointer. The first operand is the chain, the
235 // second is the destination function pointer (a GlobalAddress for a direct
236 // call). Arguments have already been lowered to explicit DAGs according to
Chris Lattner4c1eae92005-05-13 18:40:17 +0000237 // the calling convention in effect here. TAILCALL is the same as CALL, but
238 // the callee is known not to access the stack of the caller.
Chris Lattner63b570d2005-01-07 07:45:27 +0000239 CALL,
Chris Lattner4c1eae92005-05-13 18:40:17 +0000240 TAILCALL,
Chris Lattneref36aa72005-01-11 05:56:17 +0000241
242 // MEMSET/MEMCPY/MEMMOVE - The first operand is the chain, and the rest
243 // correspond to the operands of the LLVM intrinsic functions. The only
244 // result is a token chain. The alignment argument is guaranteed to be a
245 // Constant node.
246 MEMSET,
247 MEMMOVE,
248 MEMCPY,
Misha Brukmanea61c352005-04-21 20:39:54 +0000249
Chris Lattner16cd04d2005-05-12 23:24:06 +0000250 // CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of
251 // a call sequence, and carry arbitrary information that target might want
252 // to know. The first operand is a chain, the rest are specified by the
253 // target and not touched by the DAG optimizers.
254 CALLSEQ_START, // Beginning of a call sequence
255 CALLSEQ_END, // End of a call sequence
Chris Lattner63b570d2005-01-07 07:45:27 +0000256
Chris Lattner21074f42005-05-09 20:21:27 +0000257 // SRCVALUE - This corresponds to a Value*, and is used to associate memory
258 // locations with their value. This allows one use alias analysis
259 // information in the backend.
260 SRCVALUE,
261
Misha Brukmane3f570c2005-03-31 21:30:35 +0000262 // PCMARKER - This corresponds to the pcmarker intrinsic.
Andrew Lenharth95762122005-03-31 21:24:06 +0000263 PCMARKER,
Chris Lattner63b570d2005-01-07 07:45:27 +0000264
Chris Lattner21074f42005-05-09 20:21:27 +0000265 // READPORT, WRITEPORT, READIO, WRITEIO - These correspond to the LLVM
266 // intrinsics of the same name. The first operand is a token chain, the
267 // other operands match the intrinsic. These produce a token chain in
268 // addition to a value (if any).
269 READPORT, WRITEPORT, READIO, WRITEIO,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000270
Chris Lattner63b570d2005-01-07 07:45:27 +0000271 // BUILTIN_OP_END - This must be the last enum value in this list.
272 BUILTIN_OP_END,
273 };
274
275 //===--------------------------------------------------------------------===//
276 /// ISD::CondCode enum - These are ordered carefully to make the bitfields
277 /// below work out, when considering SETFALSE (something that never exists
278 /// dynamically) as 0. "U" -> Unsigned (for integer operands) or Unordered
279 /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
280 /// to. If the "N" column is 1, the result of the comparison is undefined if
281 /// the input is a NAN.
282 ///
283 /// All of these (except for the 'always folded ops') should be handled for
284 /// floating point. For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
285 /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
286 ///
287 /// Note that these are laid out in a specific order to allow bit-twiddling
288 /// to transform conditions.
289 enum CondCode {
290 // Opcode N U L G E Intuitive operation
291 SETFALSE, // 0 0 0 0 Always false (always folded)
292 SETOEQ, // 0 0 0 1 True if ordered and equal
293 SETOGT, // 0 0 1 0 True if ordered and greater than
294 SETOGE, // 0 0 1 1 True if ordered and greater than or equal
295 SETOLT, // 0 1 0 0 True if ordered and less than
296 SETOLE, // 0 1 0 1 True if ordered and less than or equal
297 SETONE, // 0 1 1 0 True if ordered and operands are unequal
298 SETO, // 0 1 1 1 True if ordered (no nans)
299 SETUO, // 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
300 SETUEQ, // 1 0 0 1 True if unordered or equal
301 SETUGT, // 1 0 1 0 True if unordered or greater than
302 SETUGE, // 1 0 1 1 True if unordered, greater than, or equal
303 SETULT, // 1 1 0 0 True if unordered or less than
Misha Brukmanea61c352005-04-21 20:39:54 +0000304 SETULE, // 1 1 0 1 True if unordered, less than, or equal
Chris Lattner63b570d2005-01-07 07:45:27 +0000305 SETUNE, // 1 1 1 0 True if unordered or not equal
306 SETTRUE, // 1 1 1 1 Always true (always folded)
307 // Don't care operations: undefined if the input is a nan.
308 SETFALSE2, // 1 X 0 0 0 Always false (always folded)
309 SETEQ, // 1 X 0 0 1 True if equal
310 SETGT, // 1 X 0 1 0 True if greater than
311 SETGE, // 1 X 0 1 1 True if greater than or equal
312 SETLT, // 1 X 1 0 0 True if less than
Misha Brukmanea61c352005-04-21 20:39:54 +0000313 SETLE, // 1 X 1 0 1 True if less than or equal
Chris Lattner63b570d2005-01-07 07:45:27 +0000314 SETNE, // 1 X 1 1 0 True if not equal
315 SETTRUE2, // 1 X 1 1 1 Always true (always folded)
316
317 SETCC_INVALID, // Marker value.
318 };
319
320 /// isSignedIntSetCC - Return true if this is a setcc instruction that
321 /// performs a signed comparison when used with integer operands.
322 inline bool isSignedIntSetCC(CondCode Code) {
323 return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
324 }
325
326 /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
327 /// performs an unsigned comparison when used with integer operands.
328 inline bool isUnsignedIntSetCC(CondCode Code) {
329 return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
330 }
331
332 /// isTrueWhenEqual - Return true if the specified condition returns true if
333 /// the two operands to the condition are equal. Note that if one of the two
334 /// operands is a NaN, this value is meaningless.
335 inline bool isTrueWhenEqual(CondCode Cond) {
336 return ((int)Cond & 1) != 0;
337 }
338
339 /// getUnorderedFlavor - This function returns 0 if the condition is always
340 /// false if an operand is a NaN, 1 if the condition is always true if the
341 /// operand is a NaN, and 2 if the condition is undefined if the operand is a
342 /// NaN.
343 inline unsigned getUnorderedFlavor(CondCode Cond) {
344 return ((int)Cond >> 3) & 3;
345 }
346
347 /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
348 /// 'op' is a valid SetCC operation.
349 CondCode getSetCCInverse(CondCode Operation, bool isInteger);
350
351 /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
352 /// when given the operation for (X op Y).
353 CondCode getSetCCSwappedOperands(CondCode Operation);
354
355 /// getSetCCOrOperation - Return the result of a logical OR between different
356 /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This
357 /// function returns SETCC_INVALID if it is not possible to represent the
358 /// resultant comparison.
359 CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
360
361 /// getSetCCAndOperation - Return the result of a logical AND between
362 /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
363 /// function returns SETCC_INVALID if it is not possible to represent the
364 /// resultant comparison.
365 CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
366} // end llvm::ISD namespace
367
368
369//===----------------------------------------------------------------------===//
370/// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
371/// values as the result of a computation. Many nodes return multiple values,
372/// from loads (which define a token and a return value) to ADDC (which returns
373/// a result and a carry value), to calls (which may return an arbitrary number
374/// of values).
375///
376/// As such, each use of a SelectionDAG computation must indicate the node that
377/// computes it as well as which return value to use from that node. This pair
378/// of information is represented with the SDOperand value type.
379///
Chris Lattnerf26bc8e2005-01-08 19:52:31 +0000380class SDOperand {
381public:
Chris Lattner63b570d2005-01-07 07:45:27 +0000382 SDNode *Val; // The node defining the value we are using.
383 unsigned ResNo; // Which return value of the node we are using.
384
385 SDOperand() : Val(0) {}
386 SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
387
388 bool operator==(const SDOperand &O) const {
389 return Val == O.Val && ResNo == O.ResNo;
390 }
391 bool operator!=(const SDOperand &O) const {
392 return !operator==(O);
393 }
394 bool operator<(const SDOperand &O) const {
395 return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
396 }
397
398 SDOperand getValue(unsigned R) const {
399 return SDOperand(Val, R);
400 }
401
402 /// getValueType - Return the ValueType of the referenced return value.
403 ///
404 inline MVT::ValueType getValueType() const;
Misha Brukmanea61c352005-04-21 20:39:54 +0000405
Chris Lattner63b570d2005-01-07 07:45:27 +0000406 // Forwarding methods - These forward to the corresponding methods in SDNode.
407 inline unsigned getOpcode() const;
Chris Lattner0442fbf2005-01-21 21:39:38 +0000408 inline unsigned getNodeDepth() const;
Chris Lattner63b570d2005-01-07 07:45:27 +0000409 inline unsigned getNumOperands() const;
410 inline const SDOperand &getOperand(unsigned i) const;
Chris Lattnera44f4ae2005-01-13 22:58:50 +0000411
412 /// hasOneUse - Return true if there is exactly one operation using this
413 /// result value of the defining operator.
414 inline bool hasOneUse() const;
Chris Lattner63b570d2005-01-07 07:45:27 +0000415};
416
417
418/// simplify_type specializations - Allow casting operators to work directly on
419/// SDOperands as if they were SDNode*'s.
420template<> struct simplify_type<SDOperand> {
421 typedef SDNode* SimpleType;
422 static SimpleType getSimplifiedValue(const SDOperand &Val) {
423 return static_cast<SimpleType>(Val.Val);
424 }
425};
426template<> struct simplify_type<const SDOperand> {
427 typedef SDNode* SimpleType;
428 static SimpleType getSimplifiedValue(const SDOperand &Val) {
429 return static_cast<SimpleType>(Val.Val);
430 }
431};
432
433
434/// SDNode - Represents one node in the SelectionDAG.
435///
436class SDNode {
Chris Lattner0442fbf2005-01-21 21:39:38 +0000437 /// NodeType - The operation that this node performs.
438 ///
439 unsigned short NodeType;
440
441 /// NodeDepth - Node depth is defined as MAX(Node depth of children)+1. This
442 /// means that leaves have a depth of 1, things that use only leaves have a
443 /// depth of 2, etc.
444 unsigned short NodeDepth;
445
446 /// Operands - The values that are used by this operation.
447 ///
Chris Lattner63b570d2005-01-07 07:45:27 +0000448 std::vector<SDOperand> Operands;
449
450 /// Values - The types of the values this node defines. SDNode's may define
451 /// multiple values simultaneously.
452 std::vector<MVT::ValueType> Values;
453
454 /// Uses - These are all of the SDNode's that use a value produced by this
455 /// node.
456 std::vector<SDNode*> Uses;
457public:
458
459 //===--------------------------------------------------------------------===//
460 // Accessors
461 //
462 unsigned getOpcode() const { return NodeType; }
463
464 size_t use_size() const { return Uses.size(); }
465 bool use_empty() const { return Uses.empty(); }
466 bool hasOneUse() const { return Uses.size() == 1; }
467
Chris Lattner0442fbf2005-01-21 21:39:38 +0000468 /// getNodeDepth - Return the distance from this node to the leaves in the
469 /// graph. The leaves have a depth of 1.
470 unsigned getNodeDepth() const { return NodeDepth; }
471
Chris Lattner7ece3802005-01-17 02:24:59 +0000472 typedef std::vector<SDNode*>::const_iterator use_iterator;
473 use_iterator use_begin() const { return Uses.begin(); }
474 use_iterator use_end() const { return Uses.end(); }
475
Chris Lattnerb18a2f82005-01-12 18:37:33 +0000476 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
477 /// indicated value. This method ignores uses of other values defined by this
478 /// operation.
479 bool hasNUsesOfValue(unsigned NUses, unsigned Value);
480
Chris Lattner63b570d2005-01-07 07:45:27 +0000481 /// getNumOperands - Return the number of values used by this operation.
482 ///
483 unsigned getNumOperands() const { return Operands.size(); }
484
485 const SDOperand &getOperand(unsigned Num) {
486 assert(Num < Operands.size() && "Invalid child # of SDNode!");
487 return Operands[Num];
488 }
489
490 const SDOperand &getOperand(unsigned Num) const {
491 assert(Num < Operands.size() && "Invalid child # of SDNode!");
492 return Operands[Num];
493 }
494
495 /// getNumValues - Return the number of values defined/returned by this
496 /// operator.
497 ///
498 unsigned getNumValues() const { return Values.size(); }
499
500 /// getValueType - Return the type of a specified result.
501 ///
502 MVT::ValueType getValueType(unsigned ResNo) const {
503 assert(ResNo < Values.size() && "Illegal result number!");
504 return Values[ResNo];
505 }
506
Chris Lattner6e6e3ce2005-01-10 23:25:04 +0000507 /// getOperationName - Return the opcode of this operation for printing.
508 ///
509 const char* getOperationName() const;
Chris Lattner63b570d2005-01-07 07:45:27 +0000510 void dump() const;
511
512 static bool classof(const SDNode *) { return true; }
513
Chris Lattner73b35372005-05-11 18:56:45 +0000514
515 /// setAdjCallChain - This method should only be used by the legalizer.
516 void setAdjCallChain(SDOperand N);
517
Chris Lattner63b570d2005-01-07 07:45:27 +0000518protected:
519 friend class SelectionDAG;
520
Chris Lattner0442fbf2005-01-21 21:39:38 +0000521 SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT), NodeDepth(1) {
Chris Lattner63b570d2005-01-07 07:45:27 +0000522 Values.reserve(1);
523 Values.push_back(VT);
524 }
Chris Lattner63b570d2005-01-07 07:45:27 +0000525 SDNode(unsigned NT, SDOperand Op)
Chris Lattner0442fbf2005-01-21 21:39:38 +0000526 : NodeType(NT), NodeDepth(Op.Val->getNodeDepth()+1) {
Chris Lattner63b570d2005-01-07 07:45:27 +0000527 Operands.reserve(1); Operands.push_back(Op);
528 Op.Val->Uses.push_back(this);
529 }
530 SDNode(unsigned NT, SDOperand N1, SDOperand N2)
531 : NodeType(NT) {
Chris Lattner0442fbf2005-01-21 21:39:38 +0000532 if (N1.Val->getNodeDepth() > N2.Val->getNodeDepth())
533 NodeDepth = N1.Val->getNodeDepth()+1;
534 else
535 NodeDepth = N2.Val->getNodeDepth()+1;
Chris Lattner63b570d2005-01-07 07:45:27 +0000536 Operands.reserve(2); Operands.push_back(N1); Operands.push_back(N2);
537 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
538 }
539 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
540 : NodeType(NT) {
Chris Lattner0442fbf2005-01-21 21:39:38 +0000541 unsigned ND = N1.Val->getNodeDepth();
542 if (ND < N2.Val->getNodeDepth())
543 ND = N2.Val->getNodeDepth();
544 if (ND < N3.Val->getNodeDepth())
545 ND = N3.Val->getNodeDepth();
546 NodeDepth = ND+1;
547
Chris Lattner63b570d2005-01-07 07:45:27 +0000548 Operands.reserve(3); Operands.push_back(N1); Operands.push_back(N2);
549 Operands.push_back(N3);
550 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
551 N3.Val->Uses.push_back(this);
552 }
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000553 SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3, SDOperand N4)
554 : NodeType(NT) {
555 unsigned ND = N1.Val->getNodeDepth();
556 if (ND < N2.Val->getNodeDepth())
557 ND = N2.Val->getNodeDepth();
558 if (ND < N3.Val->getNodeDepth())
559 ND = N3.Val->getNodeDepth();
560 if (ND < N4.Val->getNodeDepth())
561 ND = N4.Val->getNodeDepth();
562 NodeDepth = ND+1;
563
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000564 Operands.reserve(4); Operands.push_back(N1); Operands.push_back(N2);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000565 Operands.push_back(N3); Operands.push_back(N4);
566 N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
567 N3.Val->Uses.push_back(this); N4.Val->Uses.push_back(this);
568 }
Chris Lattner63b570d2005-01-07 07:45:27 +0000569 SDNode(unsigned NT, std::vector<SDOperand> &Nodes) : NodeType(NT) {
570 Operands.swap(Nodes);
Chris Lattner0442fbf2005-01-21 21:39:38 +0000571 unsigned ND = 0;
572 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattneref36aa72005-01-11 05:56:17 +0000573 Operands[i].Val->Uses.push_back(this);
Chris Lattner0442fbf2005-01-21 21:39:38 +0000574 if (ND < Operands[i].Val->getNodeDepth())
575 ND = Operands[i].Val->getNodeDepth();
576 }
577 NodeDepth = ND+1;
Chris Lattner63b570d2005-01-07 07:45:27 +0000578 }
579
580 virtual ~SDNode() {
581 // FIXME: Drop uses.
582 }
583
584 void setValueTypes(MVT::ValueType VT) {
585 Values.reserve(1);
586 Values.push_back(VT);
587 }
588 void setValueTypes(MVT::ValueType VT1, MVT::ValueType VT2) {
589 Values.reserve(2);
590 Values.push_back(VT1);
591 Values.push_back(VT2);
592 }
593 /// Note: this method destroys the vector passed in.
594 void setValueTypes(std::vector<MVT::ValueType> &VTs) {
595 std::swap(Values, VTs);
596 }
Chris Lattnerd1fc9642005-01-07 21:08:55 +0000597
598 void removeUser(SDNode *User) {
599 // Remove this user from the operand's use list.
600 for (unsigned i = Uses.size(); ; --i) {
601 assert(i != 0 && "Didn't find user!");
602 if (Uses[i-1] == User) {
603 Uses.erase(Uses.begin()+i-1);
604 break;
605 }
606 }
607 }
Chris Lattner63b570d2005-01-07 07:45:27 +0000608};
609
610
611// Define inline functions from the SDOperand class.
612
613inline unsigned SDOperand::getOpcode() const {
614 return Val->getOpcode();
615}
Chris Lattner0442fbf2005-01-21 21:39:38 +0000616inline unsigned SDOperand::getNodeDepth() const {
617 return Val->getNodeDepth();
618}
Chris Lattner63b570d2005-01-07 07:45:27 +0000619inline MVT::ValueType SDOperand::getValueType() const {
620 return Val->getValueType(ResNo);
621}
622inline unsigned SDOperand::getNumOperands() const {
623 return Val->getNumOperands();
624}
625inline const SDOperand &SDOperand::getOperand(unsigned i) const {
626 return Val->getOperand(i);
627}
Chris Lattnera44f4ae2005-01-13 22:58:50 +0000628inline bool SDOperand::hasOneUse() const {
629 return Val->hasNUsesOfValue(1, ResNo);
630}
Chris Lattner63b570d2005-01-07 07:45:27 +0000631
632
633class ConstantSDNode : public SDNode {
634 uint64_t Value;
635protected:
636 friend class SelectionDAG;
637 ConstantSDNode(uint64_t val, MVT::ValueType VT)
638 : SDNode(ISD::Constant, VT), Value(val) {
639 }
640public:
641
642 uint64_t getValue() const { return Value; }
643
644 int64_t getSignExtended() const {
645 unsigned Bits = MVT::getSizeInBits(getValueType(0));
Chris Lattnerf26bc8e2005-01-08 19:52:31 +0000646 return ((int64_t)Value << (64-Bits)) >> (64-Bits);
Chris Lattner63b570d2005-01-07 07:45:27 +0000647 }
648
649 bool isNullValue() const { return Value == 0; }
650 bool isAllOnesValue() const {
Chris Lattner2bffad32005-04-08 21:31:29 +0000651 int NumBits = MVT::getSizeInBits(getValueType(0));
652 if (NumBits == 64) return Value+1 == 0;
653 return Value == (1ULL << NumBits)-1;
Chris Lattner63b570d2005-01-07 07:45:27 +0000654 }
655
656 static bool classof(const ConstantSDNode *) { return true; }
657 static bool classof(const SDNode *N) {
658 return N->getOpcode() == ISD::Constant;
659 }
660};
661
662class ConstantFPSDNode : public SDNode {
663 double Value;
664protected:
665 friend class SelectionDAG;
666 ConstantFPSDNode(double val, MVT::ValueType VT)
667 : SDNode(ISD::ConstantFP, VT), Value(val) {
668 }
669public:
670
671 double getValue() const { return Value; }
672
673 /// isExactlyValue - We don't rely on operator== working on double values, as
674 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
675 /// As such, this method can be used to do an exact bit-for-bit comparison of
676 /// two floating point values.
677 bool isExactlyValue(double V) const {
678 union {
679 double V;
680 uint64_t I;
681 } T1;
682 T1.V = Value;
683 union {
684 double V;
685 uint64_t I;
686 } T2;
687 T2.V = V;
688 return T1.I == T2.I;
689 }
690
691 static bool classof(const ConstantFPSDNode *) { return true; }
692 static bool classof(const SDNode *N) {
693 return N->getOpcode() == ISD::ConstantFP;
694 }
695};
696
697class GlobalAddressSDNode : public SDNode {
698 GlobalValue *TheGlobal;
699protected:
700 friend class SelectionDAG;
701 GlobalAddressSDNode(const GlobalValue *GA, MVT::ValueType VT)
702 : SDNode(ISD::GlobalAddress, VT) {
703 TheGlobal = const_cast<GlobalValue*>(GA);
Chris Lattner63b570d2005-01-07 07:45:27 +0000704 }
705public:
706
707 GlobalValue *getGlobal() const { return TheGlobal; }
708
709 static bool classof(const GlobalAddressSDNode *) { return true; }
710 static bool classof(const SDNode *N) {
711 return N->getOpcode() == ISD::GlobalAddress;
712 }
713};
714
715
716class FrameIndexSDNode : public SDNode {
717 int FI;
718protected:
719 friend class SelectionDAG;
720 FrameIndexSDNode(int fi, MVT::ValueType VT)
721 : SDNode(ISD::FrameIndex, VT), FI(fi) {}
722public:
723
724 int getIndex() const { return FI; }
725
726 static bool classof(const FrameIndexSDNode *) { return true; }
727 static bool classof(const SDNode *N) {
728 return N->getOpcode() == ISD::FrameIndex;
729 }
730};
731
732class ConstantPoolSDNode : public SDNode {
733 unsigned CPI;
734protected:
735 friend class SelectionDAG;
736 ConstantPoolSDNode(unsigned cpi, MVT::ValueType VT)
737 : SDNode(ISD::ConstantPool, VT), CPI(cpi) {}
738public:
739
740 unsigned getIndex() const { return CPI; }
741
742 static bool classof(const ConstantPoolSDNode *) { return true; }
743 static bool classof(const SDNode *N) {
744 return N->getOpcode() == ISD::ConstantPool;
745 }
746};
747
748class BasicBlockSDNode : public SDNode {
749 MachineBasicBlock *MBB;
750protected:
751 friend class SelectionDAG;
752 BasicBlockSDNode(MachineBasicBlock *mbb)
753 : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
754public:
755
756 MachineBasicBlock *getBasicBlock() const { return MBB; }
757
758 static bool classof(const BasicBlockSDNode *) { return true; }
759 static bool classof(const SDNode *N) {
760 return N->getOpcode() == ISD::BasicBlock;
761 }
762};
763
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000764class SrcValueSDNode : public SDNode {
765 const Value *V;
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000766 int offset;
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000767protected:
768 friend class SelectionDAG;
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000769 SrcValueSDNode(const Value* v, int o)
770 : SDNode(ISD::SRCVALUE, MVT::Other), V(v), offset(o) {}
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000771
772public:
773 const Value *getValue() const { return V; }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000774 int getOffset() const { return offset; }
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000775
776 static bool classof(const SrcValueSDNode *) { return true; }
777 static bool classof(const SDNode *N) {
778 return N->getOpcode() == ISD::SRCVALUE;
779 }
780};
781
Chris Lattner63b570d2005-01-07 07:45:27 +0000782
Chris Lattner18c2f132005-01-13 20:50:02 +0000783class RegSDNode : public SDNode {
Chris Lattner63b570d2005-01-07 07:45:27 +0000784 unsigned Reg;
785protected:
786 friend class SelectionDAG;
Chris Lattner60e48782005-01-14 22:37:20 +0000787 RegSDNode(unsigned Opc, SDOperand Chain, SDOperand Src, unsigned reg)
Chris Lattner1cff05c2005-01-14 22:07:46 +0000788 : SDNode(Opc, Chain, Src), Reg(reg) {
Chris Lattner63b570d2005-01-07 07:45:27 +0000789 }
Chris Lattner60e48782005-01-14 22:37:20 +0000790 RegSDNode(unsigned Opc, SDOperand Chain, unsigned reg)
791 : SDNode(Opc, Chain), Reg(reg) {}
Chris Lattner63b570d2005-01-07 07:45:27 +0000792public:
793
794 unsigned getReg() const { return Reg; }
795
Chris Lattner18c2f132005-01-13 20:50:02 +0000796 static bool classof(const RegSDNode *) { return true; }
Chris Lattner63b570d2005-01-07 07:45:27 +0000797 static bool classof(const SDNode *N) {
798 return N->getOpcode() == ISD::CopyToReg ||
Chris Lattner18c2f132005-01-13 20:50:02 +0000799 N->getOpcode() == ISD::CopyFromReg ||
800 N->getOpcode() == ISD::ImplicitDef;
Chris Lattner63b570d2005-01-07 07:45:27 +0000801 }
802};
803
804class ExternalSymbolSDNode : public SDNode {
805 const char *Symbol;
806protected:
807 friend class SelectionDAG;
808 ExternalSymbolSDNode(const char *Sym, MVT::ValueType VT)
809 : SDNode(ISD::ExternalSymbol, VT), Symbol(Sym) {
810 }
811public:
812
813 const char *getSymbol() const { return Symbol; }
814
815 static bool classof(const ExternalSymbolSDNode *) { return true; }
816 static bool classof(const SDNode *N) {
817 return N->getOpcode() == ISD::ExternalSymbol;
818 }
819};
820
821class SetCCSDNode : public SDNode {
822 ISD::CondCode Condition;
823protected:
824 friend class SelectionDAG;
825 SetCCSDNode(ISD::CondCode Cond, SDOperand LHS, SDOperand RHS)
826 : SDNode(ISD::SETCC, LHS, RHS), Condition(Cond) {
Chris Lattner63b570d2005-01-07 07:45:27 +0000827 }
828public:
829
830 ISD::CondCode getCondition() const { return Condition; }
831
832 static bool classof(const SetCCSDNode *) { return true; }
833 static bool classof(const SDNode *N) {
834 return N->getOpcode() == ISD::SETCC;
835 }
836};
837
Chris Lattner1cff05c2005-01-14 22:07:46 +0000838/// MVTSDNode - This class is used for operators that require an extra
839/// value-type to be kept with the node.
840class MVTSDNode : public SDNode {
841 MVT::ValueType ExtraValueType;
842protected:
843 friend class SelectionDAG;
Chris Lattner859157d2005-01-15 06:17:04 +0000844 MVTSDNode(unsigned Opc, MVT::ValueType VT1, SDOperand Op0, MVT::ValueType EVT)
845 : SDNode(Opc, Op0), ExtraValueType(EVT) {
846 setValueTypes(VT1);
847 }
Chris Lattner60e48782005-01-14 22:37:20 +0000848 MVTSDNode(unsigned Opc, MVT::ValueType VT1, MVT::ValueType VT2,
Chris Lattner1cff05c2005-01-14 22:07:46 +0000849 SDOperand Op0, SDOperand Op1, SDOperand Op2, MVT::ValueType EVT)
850 : SDNode(Opc, Op0, Op1, Op2), ExtraValueType(EVT) {
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000851 setValueTypes(VT1, VT2);
852 }
853
854 MVTSDNode(unsigned Opc, MVT::ValueType VT,
855 SDOperand Op0, SDOperand Op1, SDOperand Op2, SDOperand Op3, MVT::ValueType EVT)
856 : SDNode(Opc, Op0, Op1, Op2, Op3), ExtraValueType(EVT) {
Chris Lattner1cff05c2005-01-14 22:07:46 +0000857 setValueTypes(VT);
858 }
859public:
860
861 MVT::ValueType getExtraValueType() const { return ExtraValueType; }
862
863 static bool classof(const MVTSDNode *) { return true; }
864 static bool classof(const SDNode *N) {
Misha Brukmanea61c352005-04-21 20:39:54 +0000865 return
Chris Lattner859157d2005-01-15 06:17:04 +0000866 N->getOpcode() == ISD::SIGN_EXTEND_INREG ||
Chris Lattner859157d2005-01-15 06:17:04 +0000867 N->getOpcode() == ISD::FP_ROUND_INREG ||
Chris Lattner1cff05c2005-01-14 22:07:46 +0000868 N->getOpcode() == ISD::EXTLOAD ||
Misha Brukmanea61c352005-04-21 20:39:54 +0000869 N->getOpcode() == ISD::SEXTLOAD ||
Chris Lattner1cff05c2005-01-14 22:07:46 +0000870 N->getOpcode() == ISD::ZEXTLOAD ||
871 N->getOpcode() == ISD::TRUNCSTORE;
872 }
873};
Chris Lattner1080b9e2005-01-10 23:05:53 +0000874
875class SDNodeIterator : public forward_iterator<SDNode, ptrdiff_t> {
876 SDNode *Node;
877 unsigned Operand;
Misha Brukmanea61c352005-04-21 20:39:54 +0000878
Chris Lattner1080b9e2005-01-10 23:05:53 +0000879 SDNodeIterator(SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
880public:
881 bool operator==(const SDNodeIterator& x) const {
882 return Operand == x.Operand;
883 }
884 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
885
886 const SDNodeIterator &operator=(const SDNodeIterator &I) {
887 assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
888 Operand = I.Operand;
889 return *this;
890 }
Misha Brukmanea61c352005-04-21 20:39:54 +0000891
Chris Lattner1080b9e2005-01-10 23:05:53 +0000892 pointer operator*() const {
893 return Node->getOperand(Operand).Val;
894 }
895 pointer operator->() const { return operator*(); }
Misha Brukmanea61c352005-04-21 20:39:54 +0000896
Chris Lattner1080b9e2005-01-10 23:05:53 +0000897 SDNodeIterator& operator++() { // Preincrement
898 ++Operand;
899 return *this;
900 }
901 SDNodeIterator operator++(int) { // Postincrement
Misha Brukmanea61c352005-04-21 20:39:54 +0000902 SDNodeIterator tmp = *this; ++*this; return tmp;
Chris Lattner1080b9e2005-01-10 23:05:53 +0000903 }
904
905 static SDNodeIterator begin(SDNode *N) { return SDNodeIterator(N, 0); }
906 static SDNodeIterator end (SDNode *N) {
907 return SDNodeIterator(N, N->getNumOperands());
908 }
909
910 unsigned getOperand() const { return Operand; }
911 const SDNode *getNode() const { return Node; }
912};
913
914template <> struct GraphTraits<SDNode*> {
915 typedef SDNode NodeType;
916 typedef SDNodeIterator ChildIteratorType;
917 static inline NodeType *getEntryNode(SDNode *N) { return N; }
Misha Brukmanea61c352005-04-21 20:39:54 +0000918 static inline ChildIteratorType child_begin(NodeType *N) {
Chris Lattner1080b9e2005-01-10 23:05:53 +0000919 return SDNodeIterator::begin(N);
920 }
Misha Brukmanea61c352005-04-21 20:39:54 +0000921 static inline ChildIteratorType child_end(NodeType *N) {
Chris Lattner1080b9e2005-01-10 23:05:53 +0000922 return SDNodeIterator::end(N);
923 }
924};
925
926
927
928
Chris Lattner63b570d2005-01-07 07:45:27 +0000929} // end llvm namespace
930
931#endif