Missing files for the BlockFrequency analysis added.
[oota-llvm.git] / include / llvm / InlineAsm.h
1 //===-- llvm/InlineAsm.h - Class to represent inline asm strings-*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class represents the inline asm strings, which are Value*'s that are
11 // used as the callee operand of call instructions.  InlineAsm's are uniqued
12 // like constants, and created via InlineAsm::get(...).
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_INLINEASM_H
17 #define LLVM_INLINEASM_H
18
19 #include "llvm/Value.h"
20 #include <vector>
21
22 namespace llvm {
23
24 class PointerType;
25 class FunctionType;
26 class Module;
27 struct InlineAsmKeyType;
28 template<class ValType, class ValRefType, class TypeClass, class ConstantClass,
29          bool HasLargeKey>
30 class ConstantUniqueMap;
31 template<class ConstantClass, class TypeClass, class ValType>
32 struct ConstantCreator;
33
34 class InlineAsm : public Value {
35   friend struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType>;
36   friend class ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&,
37                                  PointerType, InlineAsm, false>;
38
39   InlineAsm(const InlineAsm &);             // do not implement
40   void operator=(const InlineAsm&);         // do not implement
41
42   std::string AsmString, Constraints;
43   bool HasSideEffects;
44   bool IsAlignStack;
45   
46   InlineAsm(const PointerType *Ty, const std::string &AsmString,
47             const std::string &Constraints, bool hasSideEffects,
48             bool isAlignStack);
49   virtual ~InlineAsm();
50
51   /// When the ConstantUniqueMap merges two types and makes two InlineAsms
52   /// identical, it destroys one of them with this method.
53   void destroyConstant();
54 public:
55
56   /// InlineAsm::get - Return the specified uniqued inline asm string.
57   ///
58   static InlineAsm *get(const FunctionType *Ty, StringRef AsmString,
59                         StringRef Constraints, bool hasSideEffects,
60                         bool isAlignStack = false);
61   
62   bool hasSideEffects() const { return HasSideEffects; }
63   bool isAlignStack() const { return IsAlignStack; }
64   
65   /// getType - InlineAsm's are always pointers.
66   ///
67   const PointerType *getType() const {
68     return reinterpret_cast<const PointerType*>(Value::getType());
69   }
70   
71   /// getFunctionType - InlineAsm's are always pointers to functions.
72   ///
73   const FunctionType *getFunctionType() const;
74   
75   const std::string &getAsmString() const { return AsmString; }
76   const std::string &getConstraintString() const { return Constraints; }
77
78   /// Verify - This static method can be used by the parser to check to see if
79   /// the specified constraint string is legal for the type.  This returns true
80   /// if legal, false if not.
81   ///
82   static bool Verify(const FunctionType *Ty, StringRef Constraints);
83
84   // Constraint String Parsing 
85   enum ConstraintPrefix {
86     isInput,            // 'x'
87     isOutput,           // '=x'
88     isClobber           // '~x'
89   };
90   
91   typedef std::vector<std::string> ConstraintCodeVector;
92   
93   struct SubConstraintInfo {
94     /// MatchingInput - If this is not -1, this is an output constraint where an
95     /// input constraint is required to match it (e.g. "0").  The value is the
96     /// constraint number that matches this one (for example, if this is
97     /// constraint #0 and constraint #4 has the value "0", this will be 4).
98     signed char MatchingInput;
99     /// Code - The constraint code, either the register name (in braces) or the
100     /// constraint letter/number.
101     ConstraintCodeVector Codes;
102     /// Default constructor.
103     SubConstraintInfo() : MatchingInput(-1) {}
104   };
105
106   typedef std::vector<SubConstraintInfo> SubConstraintInfoVector;
107   struct ConstraintInfo;
108   typedef std::vector<ConstraintInfo> ConstraintInfoVector;
109   
110   struct ConstraintInfo {
111     /// Type - The basic type of the constraint: input/output/clobber
112     ///
113     ConstraintPrefix Type;
114     
115     /// isEarlyClobber - "&": output operand writes result before inputs are all
116     /// read.  This is only ever set for an output operand.
117     bool isEarlyClobber; 
118     
119     /// MatchingInput - If this is not -1, this is an output constraint where an
120     /// input constraint is required to match it (e.g. "0").  The value is the
121     /// constraint number that matches this one (for example, if this is
122     /// constraint #0 and constraint #4 has the value "0", this will be 4).
123     signed char MatchingInput;
124     
125     /// hasMatchingInput - Return true if this is an output constraint that has
126     /// a matching input constraint.
127     bool hasMatchingInput() const { return MatchingInput != -1; }
128     
129     /// isCommutative - This is set to true for a constraint that is commutative
130     /// with the next operand.
131     bool isCommutative;
132     
133     /// isIndirect - True if this operand is an indirect operand.  This means
134     /// that the address of the source or destination is present in the call
135     /// instruction, instead of it being returned or passed in explicitly.  This
136     /// is represented with a '*' in the asm string.
137     bool isIndirect;
138     
139     /// Code - The constraint code, either the register name (in braces) or the
140     /// constraint letter/number.
141     ConstraintCodeVector Codes;
142     
143     /// isMultipleAlternative - '|': has multiple-alternative constraints.
144     bool isMultipleAlternative;
145     
146     /// multipleAlternatives - If there are multiple alternative constraints,
147     /// this array will contain them.  Otherwise it will be empty.
148     SubConstraintInfoVector multipleAlternatives;
149     
150     /// The currently selected alternative constraint index.
151     unsigned currentAlternativeIndex;
152     
153     ///Default constructor.
154     ConstraintInfo();
155     
156     /// Copy constructor.
157     ConstraintInfo(const ConstraintInfo &other);
158     
159     /// Parse - Analyze the specified string (e.g. "=*&{eax}") and fill in the
160     /// fields in this structure.  If the constraint string is not understood,
161     /// return true, otherwise return false.
162     bool Parse(StringRef Str, ConstraintInfoVector &ConstraintsSoFar);
163                
164     /// selectAlternative - Point this constraint to the alternative constraint
165     /// indicated by the index.
166     void selectAlternative(unsigned index);
167   };
168   
169   /// ParseConstraints - Split up the constraint string into the specific
170   /// constraints and their prefixes.  If this returns an empty vector, and if
171   /// the constraint string itself isn't empty, there was an error parsing.
172   static ConstraintInfoVector ParseConstraints(StringRef ConstraintString);
173   
174   /// ParseConstraints - Parse the constraints of this inlineasm object, 
175   /// returning them the same way that ParseConstraints(str) does.
176   ConstraintInfoVector ParseConstraints() const {
177     return ParseConstraints(Constraints);
178   }
179   
180   // Methods for support type inquiry through isa, cast, and dyn_cast:
181   static inline bool classof(const InlineAsm *) { return true; }
182   static inline bool classof(const Value *V) {
183     return V->getValueID() == Value::InlineAsmVal;
184   }
185
186   
187   // These are helper methods for dealing with flags in the INLINEASM SDNode
188   // in the backend.
189   
190   enum {
191     Op_InputChain = 0,
192     Op_AsmString = 1,
193     Op_MDNode = 2,
194     Op_ExtraInfo = 3,    // HasSideEffects, IsAlignStack
195     Op_FirstOperand = 4,
196
197     MIOp_AsmString = 0,
198     MIOp_ExtraInfo = 1,    // HasSideEffects, IsAlignStack
199     MIOp_FirstOperand = 2,
200
201     Extra_HasSideEffects = 1,
202     Extra_IsAlignStack = 2,
203     
204     Kind_RegUse = 1,
205     Kind_RegDef = 2,
206     Kind_Imm = 3,
207     Kind_Mem = 4,
208     Kind_RegDefEarlyClobber = 6,
209     
210     Flag_MatchingOperand = 0x80000000
211   };
212   
213   static unsigned getFlagWord(unsigned Kind, unsigned NumOps) {
214     assert(((NumOps << 3) & ~0xffff) == 0 && "Too many inline asm operands!");
215     return Kind | (NumOps << 3);
216   }
217   
218   /// getFlagWordForMatchingOp - Augment an existing flag word returned by
219   /// getFlagWord with information indicating that this input operand is tied 
220   /// to a previous output operand.
221   static unsigned getFlagWordForMatchingOp(unsigned InputFlag,
222                                            unsigned MatchedOperandNo) {
223     return InputFlag | Flag_MatchingOperand | (MatchedOperandNo << 16);
224   }
225
226   static unsigned getKind(unsigned Flags) {
227     return Flags & 7;
228   }
229
230   static bool isRegDefKind(unsigned Flag){ return getKind(Flag) == Kind_RegDef;}
231   static bool isImmKind(unsigned Flag) { return getKind(Flag) == Kind_Imm; }
232   static bool isMemKind(unsigned Flag) { return getKind(Flag) == Kind_Mem; }
233   static bool isRegDefEarlyClobberKind(unsigned Flag) {
234     return getKind(Flag) == Kind_RegDefEarlyClobber;
235   }
236   
237   /// getNumOperandRegisters - Extract the number of registers field from the
238   /// inline asm operand flag.
239   static unsigned getNumOperandRegisters(unsigned Flag) {
240     return (Flag & 0xffff) >> 3;
241   }
242
243   /// isUseOperandTiedToDef - Return true if the flag of the inline asm
244   /// operand indicates it is an use operand that's matched to a def operand.
245   static bool isUseOperandTiedToDef(unsigned Flag, unsigned &Idx) {
246     if ((Flag & Flag_MatchingOperand) == 0)
247       return false;
248     Idx = (Flag & ~Flag_MatchingOperand) >> 16;
249     return true;
250   }
251
252
253 };
254
255 } // End llvm namespace
256
257 #endif