Initial implementation of the nodes in a SelectionDAG.
[oota-llvm.git] / include / llvm / CodeGen / SelectionDAGNodes.h
1 //===-- 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"
23 #include <cassert>
24 #include <vector>
25
26 namespace llvm {
27
28 class SelectionDAG;
29 class GlobalValue;
30 class MachineBasicBlock;
31 class SDNode;
32 template <typename T> struct simplify_type;
33
34 /// ISD namespace - This namespace contains an enum which represents all of the
35 /// SelectionDAG node types and value types.
36 ///
37 namespace ISD {
38   //===--------------------------------------------------------------------===//
39   /// ISD::NodeType enum - This enum defines all of the operators valid in a
40   /// SelectionDAG.
41   ///
42   enum NodeType {
43     // Leaf nodes
44     EntryToken, Constant, ConstantFP, GlobalAddress, FrameIndex, ConstantPool,
45     BasicBlock, ExternalSymbol,
46
47     // CopyToReg - This node has chain and child nodes, and an associated
48     // register number.  The instruction selector must guarantee that the value
49     // of the value node is available in the virtual register stored in the
50     // CopyRegSDNode object.
51     CopyToReg,
52
53     // CopyFromReg - This node indicates that the input value is a virtual or
54     // physical register that is defined outside of the scope of this
55     // SelectionDAG.  The virtual register is available from the
56     // CopyRegSDNode object.
57     CopyFromReg,
58
59     // EXTRACT_ELEMENT - This is used to get the first or second (determined by
60     // a Constant, which is required to be operand #1), element of the aggregate
61     // value specified as operand #0.  This is only for use before legalization,
62     // for values that will be broken into multiple registers.
63     EXTRACT_ELEMENT,
64
65     // BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.  Given
66     // two values of the same integer value type, this produces a value twice as
67     // big.  Like EXTRACT_ELEMENT, this can only be used before legalization.
68     BUILD_PAIR,
69
70
71     // Simple binary arithmetic operators.
72     ADD, SUB, MUL, SDIV, UDIV, SREM, UREM,
73
74     // Bitwise operators.
75     AND, OR, XOR, SHL, SRA, SRL,
76
77     // Select operator.
78     SELECT,
79
80     // SetCC operator - This evaluates to a boolean (i1) true value if the
81     // condition is true.  These nodes are instances of the
82     // SetCCSDNode class, which contains the condition code as extra
83     // state.
84     SETCC,
85
86     // addc - Three input, two output operator: (X, Y, C) -> (X+Y+C,
87     // Cout).  X,Y are integer inputs of agreeing size, C is a one bit
88     // value, and two values are produced: the sum and a carry out.
89     ADDC, SUBB,
90
91     // Conversion operators.  These are all single input single output
92     // operations.  For all of these, the result type must be strictly
93     // wider or narrower (depending on the operation) than the source
94     // type.
95
96     // SIGN_EXTEND - Used for integer types, replicating the sign bit
97     // into new bits.
98     SIGN_EXTEND,
99
100     // ZERO_EXTEND - Used for integer types, zeroing the new bits.
101     ZERO_EXTEND,
102
103     // TRUNCATE - Completely drop the high bits.
104     TRUNCATE,
105
106     // FP_ROUND - Perform a rounding operation from the current
107     // precision down to the specified precision.
108     FP_ROUND,
109
110     // FP_EXTEND - Extend a smaller FP type into a larger FP type.
111     FP_EXTEND,
112
113     // Other operators.  LOAD and STORE have token chains.
114     LOAD, STORE,
115
116     // DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned
117     // to a specified boundary.  The first operand is the token chain, the
118     // second is the number of bytes to allocate, and the third is the alignment
119     // boundary.
120     DYNAMIC_STACKALLOC,
121
122     // Control flow instructions.  These all have token chains.
123     
124     // BR - Unconditional branch.  The first operand is the chain
125     // operand, the second is the MBB to branch to.
126     BR,
127
128     // BRCOND - Conditional branch.  The first operand is the chain,
129     // the second is the condition, the third is the block to branch
130     // to if the condition is true.
131     BRCOND,
132
133     // RET - Return from function.  The first operand is the chain,
134     // and any subsequent operands are the return values for the
135     // function.  This operation can have variable number of operands.
136     RET,
137
138     // CALL - Call to a function pointer.  The first operand is the chain, the
139     // second is the destination function pointer (a GlobalAddress for a direct
140     // call).  Arguments have already been lowered to explicit DAGs according to
141     // the calling convention in effect here.
142     CALL,
143     
144     // ADJCALLSTACKDOWN/ADJCALLSTACKUP - These operators mark the beginning and
145     // end of a call sequence and indicate how much the stack pointer needs to
146     // be adjusted for that particular call.  The first operand is a chain, the
147     // second is a ConstantSDNode of intptr type.
148     ADJCALLSTACKDOWN,  // Beginning of a call sequence
149     ADJCALLSTACKUP,    // End of a call sequence
150
151
152     // BUILTIN_OP_END - This must be the last enum value in this list.
153     BUILTIN_OP_END,
154   };
155
156   //===--------------------------------------------------------------------===//
157   /// ISD::CondCode enum - These are ordered carefully to make the bitfields
158   /// below work out, when considering SETFALSE (something that never exists
159   /// dynamically) as 0.  "U" -> Unsigned (for integer operands) or Unordered
160   /// (for floating point), "L" -> Less than, "G" -> Greater than, "E" -> Equal
161   /// to.  If the "N" column is 1, the result of the comparison is undefined if
162   /// the input is a NAN.
163   ///
164   /// All of these (except for the 'always folded ops') should be handled for
165   /// floating point.  For integer, only the SETEQ,SETNE,SETLT,SETLE,SETGT,
166   /// SETGE,SETULT,SETULE,SETUGT, and SETUGE opcodes are used.
167   ///
168   /// Note that these are laid out in a specific order to allow bit-twiddling
169   /// to transform conditions.
170   enum CondCode {
171     // Opcode          N U L G E       Intuitive operation
172     SETFALSE,      //    0 0 0 0       Always false (always folded)
173     SETOEQ,        //    0 0 0 1       True if ordered and equal
174     SETOGT,        //    0 0 1 0       True if ordered and greater than
175     SETOGE,        //    0 0 1 1       True if ordered and greater than or equal
176     SETOLT,        //    0 1 0 0       True if ordered and less than
177     SETOLE,        //    0 1 0 1       True if ordered and less than or equal
178     SETONE,        //    0 1 1 0       True if ordered and operands are unequal
179     SETO,          //    0 1 1 1       True if ordered (no nans)
180     SETUO,         //    1 0 0 0       True if unordered: isnan(X) | isnan(Y)
181     SETUEQ,        //    1 0 0 1       True if unordered or equal
182     SETUGT,        //    1 0 1 0       True if unordered or greater than
183     SETUGE,        //    1 0 1 1       True if unordered, greater than, or equal
184     SETULT,        //    1 1 0 0       True if unordered or less than
185     SETULE,        //    1 1 0 1       True if unordered, less than, or equal 
186     SETUNE,        //    1 1 1 0       True if unordered or not equal
187     SETTRUE,       //    1 1 1 1       Always true (always folded)
188     // Don't care operations: undefined if the input is a nan.
189     SETFALSE2,     //  1 X 0 0 0       Always false (always folded)
190     SETEQ,         //  1 X 0 0 1       True if equal
191     SETGT,         //  1 X 0 1 0       True if greater than
192     SETGE,         //  1 X 0 1 1       True if greater than or equal
193     SETLT,         //  1 X 1 0 0       True if less than
194     SETLE,         //  1 X 1 0 1       True if less than or equal 
195     SETNE,         //  1 X 1 1 0       True if not equal
196     SETTRUE2,      //  1 X 1 1 1       Always true (always folded)
197
198     SETCC_INVALID,      // Marker value.
199   };
200
201   /// isSignedIntSetCC - Return true if this is a setcc instruction that
202   /// performs a signed comparison when used with integer operands.
203   inline bool isSignedIntSetCC(CondCode Code) {
204     return Code == SETGT || Code == SETGE || Code == SETLT || Code == SETLE;
205   }
206
207   /// isUnsignedIntSetCC - Return true if this is a setcc instruction that
208   /// performs an unsigned comparison when used with integer operands.
209   inline bool isUnsignedIntSetCC(CondCode Code) {
210     return Code == SETUGT || Code == SETUGE || Code == SETULT || Code == SETULE;
211   }
212
213   /// isTrueWhenEqual - Return true if the specified condition returns true if
214   /// the two operands to the condition are equal.  Note that if one of the two
215   /// operands is a NaN, this value is meaningless.
216   inline bool isTrueWhenEqual(CondCode Cond) {
217     return ((int)Cond & 1) != 0;
218   }
219
220   /// getUnorderedFlavor - This function returns 0 if the condition is always
221   /// false if an operand is a NaN, 1 if the condition is always true if the
222   /// operand is a NaN, and 2 if the condition is undefined if the operand is a
223   /// NaN.
224   inline unsigned getUnorderedFlavor(CondCode Cond) {
225     return ((int)Cond >> 3) & 3;
226   }
227
228   /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
229   /// 'op' is a valid SetCC operation.
230   CondCode getSetCCInverse(CondCode Operation, bool isInteger);
231
232   /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
233   /// when given the operation for (X op Y).
234   CondCode getSetCCSwappedOperands(CondCode Operation);
235
236   /// getSetCCOrOperation - Return the result of a logical OR between different
237   /// comparisons of identical values: ((X op1 Y) | (X op2 Y)).  This
238   /// function returns SETCC_INVALID if it is not possible to represent the
239   /// resultant comparison.
240   CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, bool isInteger);
241
242   /// getSetCCAndOperation - Return the result of a logical AND between
243   /// different comparisons of identical values: ((X op1 Y) & (X op2 Y)).  This
244   /// function returns SETCC_INVALID if it is not possible to represent the
245   /// resultant comparison.
246   CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, bool isInteger);
247 }  // end llvm::ISD namespace
248
249
250 //===----------------------------------------------------------------------===//
251 /// SDOperand - Unlike LLVM values, Selection DAG nodes may return multiple
252 /// values as the result of a computation.  Many nodes return multiple values,
253 /// from loads (which define a token and a return value) to ADDC (which returns
254 /// a result and a carry value), to calls (which may return an arbitrary number
255 /// of values).
256 ///
257 /// As such, each use of a SelectionDAG computation must indicate the node that
258 /// computes it as well as which return value to use from that node.  This pair
259 /// of information is represented with the SDOperand value type.
260 ///
261 struct SDOperand {
262   SDNode *Val;        // The node defining the value we are using.
263   unsigned ResNo;     // Which return value of the node we are using.
264
265   SDOperand() : Val(0) {}
266   SDOperand(SDNode *val, unsigned resno) : Val(val), ResNo(resno) {}
267
268   bool operator==(const SDOperand &O) const {
269     return Val == O.Val && ResNo == O.ResNo;
270   }
271   bool operator!=(const SDOperand &O) const {
272     return !operator==(O);
273   }
274   bool operator<(const SDOperand &O) const {
275     return Val < O.Val || (Val == O.Val && ResNo < O.ResNo);
276   }
277
278   SDOperand getValue(unsigned R) const {
279     return SDOperand(Val, R);
280   }
281
282   /// getValueType - Return the ValueType of the referenced return value.
283   ///
284   inline MVT::ValueType getValueType() const;
285
286   // Forwarding methods - These forward to the corresponding methods in SDNode.
287   inline unsigned getOpcode() const;
288   inline unsigned getNumOperands() const;
289   inline const SDOperand &getOperand(unsigned i) const;
290 };
291
292
293 /// simplify_type specializations - Allow casting operators to work directly on
294 /// SDOperands as if they were SDNode*'s.
295 template<> struct simplify_type<SDOperand> {
296   typedef SDNode* SimpleType;
297   static SimpleType getSimplifiedValue(const SDOperand &Val) {
298     return static_cast<SimpleType>(Val.Val);
299   }
300 };
301 template<> struct simplify_type<const SDOperand> {
302   typedef SDNode* SimpleType;
303   static SimpleType getSimplifiedValue(const SDOperand &Val) {
304     return static_cast<SimpleType>(Val.Val);
305   }
306 };
307
308
309 /// SDNode - Represents one node in the SelectionDAG.
310 ///
311 class SDNode {
312   unsigned NodeType;
313   std::vector<SDOperand> Operands;
314
315   /// Values - The types of the values this node defines.  SDNode's may define
316   /// multiple values simultaneously.
317   std::vector<MVT::ValueType> Values;
318
319   /// Uses - These are all of the SDNode's that use a value produced by this
320   /// node.
321   std::vector<SDNode*> Uses;
322 public:
323
324   //===--------------------------------------------------------------------===//
325   //  Accessors
326   //
327   unsigned getOpcode()  const { return NodeType; }
328
329   size_t use_size() const { return Uses.size(); }
330   bool use_empty() const { return Uses.empty(); }
331   bool hasOneUse() const { return Uses.size() == 1; }
332
333   /// getNumOperands - Return the number of values used by this operation.
334   ///
335   unsigned getNumOperands() const { return Operands.size(); }
336
337   const SDOperand &getOperand(unsigned Num) {
338     assert(Num < Operands.size() && "Invalid child # of SDNode!");
339     return Operands[Num];
340   }
341
342   const SDOperand &getOperand(unsigned Num) const {
343     assert(Num < Operands.size() && "Invalid child # of SDNode!");
344     return Operands[Num];
345   }
346
347   /// getNumValues - Return the number of values defined/returned by this
348   /// operator.
349   ///
350   unsigned getNumValues() const { return Values.size(); }
351
352   /// getValueType - Return the type of a specified result.
353   ///
354   MVT::ValueType getValueType(unsigned ResNo) const {
355     assert(ResNo < Values.size() && "Illegal result number!");
356     return Values[ResNo];
357   }
358
359   void dump() const;
360
361   static bool classof(const SDNode *) { return true; }
362
363 protected:
364   friend class SelectionDAG;
365
366   SDNode(unsigned NT, MVT::ValueType VT) : NodeType(NT) {
367     Values.reserve(1);
368     Values.push_back(VT);
369   }
370
371   SDNode(unsigned NT, SDOperand Op)
372     : NodeType(NT) {
373     Operands.reserve(1); Operands.push_back(Op);
374     Op.Val->Uses.push_back(this);
375   }
376   SDNode(unsigned NT, SDOperand N1, SDOperand N2)
377     : NodeType(NT) {
378     Operands.reserve(2); Operands.push_back(N1); Operands.push_back(N2);
379     N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
380   }
381   SDNode(unsigned NT, SDOperand N1, SDOperand N2, SDOperand N3)
382     : NodeType(NT) {
383     Operands.reserve(3); Operands.push_back(N1); Operands.push_back(N2);
384     Operands.push_back(N3);
385     N1.Val->Uses.push_back(this); N2.Val->Uses.push_back(this);
386     N3.Val->Uses.push_back(this);
387   }
388   SDNode(unsigned NT, std::vector<SDOperand> &Nodes) : NodeType(NT) {
389     Operands.swap(Nodes);
390     for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
391       Nodes[i].Val->Uses.push_back(this);
392   }
393
394   virtual ~SDNode() {
395     // FIXME: Drop uses.
396   }
397
398   void setValueTypes(MVT::ValueType VT) {
399     Values.reserve(1);
400     Values.push_back(VT);
401   }
402   void setValueTypes(MVT::ValueType VT1, MVT::ValueType VT2) {
403     Values.reserve(2);
404     Values.push_back(VT1);
405     Values.push_back(VT2);
406   }
407   /// Note: this method destroys the vector passed in.
408   void setValueTypes(std::vector<MVT::ValueType> &VTs) {
409     std::swap(Values, VTs);
410   }
411 };
412
413
414 // Define inline functions from the SDOperand class.
415
416 inline unsigned SDOperand::getOpcode() const {
417   return Val->getOpcode();
418 }
419 inline MVT::ValueType SDOperand::getValueType() const {
420   return Val->getValueType(ResNo);
421 }
422 inline unsigned SDOperand::getNumOperands() const {
423   return Val->getNumOperands();
424 }
425 inline const SDOperand &SDOperand::getOperand(unsigned i) const {
426   return Val->getOperand(i);
427 }
428
429
430
431 class ConstantSDNode : public SDNode {
432   uint64_t Value;
433 protected:
434   friend class SelectionDAG;
435   ConstantSDNode(uint64_t val, MVT::ValueType VT)
436     : SDNode(ISD::Constant, VT), Value(val) {
437   }
438 public:
439
440   uint64_t getValue() const { return Value; }
441
442   int64_t getSignExtended() const {
443     unsigned Bits = MVT::getSizeInBits(getValueType(0));
444     return ((int64_t)Value << 64-Bits) >> 64-Bits;
445   }
446
447   bool isNullValue() const { return Value == 0; }
448   bool isAllOnesValue() const {
449     return Value == (1ULL << MVT::getSizeInBits(getValueType(0)))-1;
450   }
451
452   static bool classof(const ConstantSDNode *) { return true; }
453   static bool classof(const SDNode *N) {
454     return N->getOpcode() == ISD::Constant;
455   }
456 };
457
458 class ConstantFPSDNode : public SDNode {
459   double Value;
460 protected:
461   friend class SelectionDAG;
462   ConstantFPSDNode(double val, MVT::ValueType VT)
463     : SDNode(ISD::ConstantFP, VT), Value(val) {
464   }
465 public:
466
467   double getValue() const { return Value; }
468
469   /// isExactlyValue - We don't rely on operator== working on double values, as
470   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
471   /// As such, this method can be used to do an exact bit-for-bit comparison of
472   /// two floating point values.
473   bool isExactlyValue(double V) const {
474     union {
475       double V;
476       uint64_t I;
477     } T1;
478     T1.V = Value;
479     union {
480       double V;
481       uint64_t I;
482     } T2;
483     T2.V = V;
484     return T1.I == T2.I;
485   }
486
487   static bool classof(const ConstantFPSDNode *) { return true; }
488   static bool classof(const SDNode *N) {
489     return N->getOpcode() == ISD::ConstantFP;
490   }
491 };
492
493 class GlobalAddressSDNode : public SDNode {
494   GlobalValue *TheGlobal;
495 protected:
496   friend class SelectionDAG;
497   GlobalAddressSDNode(const GlobalValue *GA, MVT::ValueType VT)
498     : SDNode(ISD::GlobalAddress, VT) {
499     TheGlobal = const_cast<GlobalValue*>(GA);
500
501   }
502 public:
503
504   GlobalValue *getGlobal() const { return TheGlobal; }
505
506   static bool classof(const GlobalAddressSDNode *) { return true; }
507   static bool classof(const SDNode *N) {
508     return N->getOpcode() == ISD::GlobalAddress;
509   }
510 };
511
512
513 class FrameIndexSDNode : public SDNode {
514   int FI;
515 protected:
516   friend class SelectionDAG;
517   FrameIndexSDNode(int fi, MVT::ValueType VT)
518     : SDNode(ISD::FrameIndex, VT), FI(fi) {}
519 public:
520
521   int getIndex() const { return FI; }
522
523   static bool classof(const FrameIndexSDNode *) { return true; }
524   static bool classof(const SDNode *N) {
525     return N->getOpcode() == ISD::FrameIndex;
526   }
527 };
528
529 class ConstantPoolSDNode : public SDNode {
530   unsigned CPI;
531 protected:
532   friend class SelectionDAG;
533   ConstantPoolSDNode(unsigned cpi, MVT::ValueType VT)
534     : SDNode(ISD::ConstantPool, VT), CPI(cpi) {}
535 public:
536
537   unsigned getIndex() const { return CPI; }
538
539   static bool classof(const ConstantPoolSDNode *) { return true; }
540   static bool classof(const SDNode *N) {
541     return N->getOpcode() == ISD::ConstantPool;
542   }
543 };
544
545 class BasicBlockSDNode : public SDNode {
546   MachineBasicBlock *MBB;
547 protected:
548   friend class SelectionDAG;
549   BasicBlockSDNode(MachineBasicBlock *mbb)
550     : SDNode(ISD::BasicBlock, MVT::Other), MBB(mbb) {}
551 public:
552
553   MachineBasicBlock *getBasicBlock() const { return MBB; }
554
555   static bool classof(const BasicBlockSDNode *) { return true; }
556   static bool classof(const SDNode *N) {
557     return N->getOpcode() == ISD::BasicBlock;
558   }
559 };
560
561
562 class CopyRegSDNode : public SDNode {
563   unsigned Reg;
564 protected:
565   friend class SelectionDAG;
566   CopyRegSDNode(SDOperand Chain, SDOperand Src, unsigned reg)
567     : SDNode(ISD::CopyToReg, Chain, Src), Reg(reg) {
568     setValueTypes(MVT::Other);  // Just a token chain.
569   }
570   CopyRegSDNode(unsigned reg, MVT::ValueType VT)
571     : SDNode(ISD::CopyFromReg, VT), Reg(reg) {
572   }
573 public:
574
575   unsigned getReg() const { return Reg; }
576
577   static bool classof(const CopyRegSDNode *) { return true; }
578   static bool classof(const SDNode *N) {
579     return N->getOpcode() == ISD::CopyToReg ||
580            N->getOpcode() == ISD::CopyFromReg;
581   }
582 };
583
584 class ExternalSymbolSDNode : public SDNode {
585   const char *Symbol;
586 protected:
587   friend class SelectionDAG;
588   ExternalSymbolSDNode(const char *Sym, MVT::ValueType VT)
589     : SDNode(ISD::ExternalSymbol, VT), Symbol(Sym) {
590     }
591 public:
592
593   const char *getSymbol() const { return Symbol; }
594
595   static bool classof(const ExternalSymbolSDNode *) { return true; }
596   static bool classof(const SDNode *N) {
597     return N->getOpcode() == ISD::ExternalSymbol;
598   }
599 };
600
601 class SetCCSDNode : public SDNode {
602   ISD::CondCode Condition;
603 protected:
604   friend class SelectionDAG;
605   SetCCSDNode(ISD::CondCode Cond, SDOperand LHS, SDOperand RHS)
606     : SDNode(ISD::SETCC, LHS, RHS), Condition(Cond) {
607     setValueTypes(MVT::i1);
608   }
609 public:
610
611   ISD::CondCode getCondition() const { return Condition; }
612
613   static bool classof(const SetCCSDNode *) { return true; }
614   static bool classof(const SDNode *N) {
615     return N->getOpcode() == ISD::SETCC;
616   }
617 };
618
619 } // end llvm namespace
620
621 #endif