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