Use enumeration for preffered EH dwarf encoding reason
[oota-llvm.git] / include / llvm / Target / TargetInstrDesc.h
1 //===-- llvm/Target/TargetInstrDesc.h - Instruction Descriptors -*- 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 file defines the TargetOperandInfo and TargetInstrDesc classes, which
11 // are used to describe target instructions and their operands. 
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TARGET_TARGETINSTRDESC_H
16 #define LLVM_TARGET_TARGETINSTRDESC_H
17
18 #include <cassert>
19
20 namespace llvm {
21
22 //===----------------------------------------------------------------------===//
23 // Machine Operand Flags and Description
24 //===----------------------------------------------------------------------===//
25   
26 namespace TOI {
27   // Operand constraints: only "tied_to" for now.
28   enum OperandConstraint {
29     TIED_TO = 0  // Must be allocated the same register as.
30   };
31   
32   /// OperandFlags - These are flags set on operands, but should be considered
33   /// private, all access should go through the TargetOperandInfo accessors.
34   /// See the accessors for a description of what these are.
35   enum OperandFlags {
36     LookupPtrRegClass = 0,
37     Predicate,
38     OptionalDef
39   };
40 }
41
42 /// TargetOperandInfo - This holds information about one operand of a machine
43 /// instruction, indicating the register class for register operands, etc.
44 ///
45 class TargetOperandInfo {
46 public:
47   /// RegClass - This specifies the register class enumeration of the operand 
48   /// if the operand is a register.  If not, this contains 0.
49   unsigned short RegClass;
50   unsigned short Flags;
51   /// Lower 16 bits are used to specify which constraints are set. The higher 16
52   /// bits are used to specify the value of constraints (4 bits each).
53   unsigned int Constraints;
54   /// Currently no other information.
55   
56   /// isLookupPtrRegClass - Set if this operand is a pointer value and it
57   /// requires a callback to look up its register class.
58   bool isLookupPtrRegClass() const { return Flags&(1 <<TOI::LookupPtrRegClass);}
59   
60   /// isPredicate - Set if this is one of the operands that made up of
61   /// the predicate operand that controls an isPredicable() instruction.
62   bool isPredicate() const { return Flags & (1 << TOI::Predicate); }
63   
64   /// isOptionalDef - Set if this operand is a optional def.
65   ///
66   bool isOptionalDef() const { return Flags & (1 << TOI::OptionalDef); }
67 };
68
69   
70 //===----------------------------------------------------------------------===//
71 // Machine Instruction Flags and Description
72 //===----------------------------------------------------------------------===//
73
74 /// TargetInstrDesc flags - These should be considered private to the
75 /// implementation of the TargetInstrDesc class.  Clients should use the
76 /// predicate methods on TargetInstrDesc, not use these directly.  These
77 /// all correspond to bitfields in the TargetInstrDesc::Flags field.
78 namespace TID {
79   enum {
80     Variadic = 0,
81     HasOptionalDef,
82     Return,
83     Call,
84     ImplicitDef,
85     Barrier,
86     Terminator,
87     Branch,
88     IndirectBranch,
89     Predicable,
90     NotDuplicable,
91     DelaySlot,
92     SimpleLoad,
93     MayLoad,
94     MayStore,
95     UnmodeledSideEffects,
96     Commutable,
97     ConvertibleTo3Addr,
98     UsesCustomDAGSchedInserter,
99     Rematerializable
100   };
101 }
102
103 /// TargetInstrDesc - Describe properties that are true of each
104 /// instruction in the target description file.  This captures information about
105 /// side effects, register use and many other things.  There is one instance of
106 /// this struct for each target instruction class, and the MachineInstr class
107 /// points to this struct directly to describe itself.
108 class TargetInstrDesc {
109 public:
110   unsigned short  Opcode;        // The opcode number.
111   unsigned short  NumOperands;   // Num of args (may be more if variable_ops)
112   unsigned short  NumDefs;       // Num of args that are definitions.
113   unsigned short  SchedClass;    // enum identifying instr sched class
114   const char *    Name;          // Name of the instruction record in td file.
115   unsigned        Flags;         // flags identifying machine instr class
116   unsigned        TSFlags;       // Target Specific Flag values
117   const unsigned *ImplicitUses;  // Registers implicitly read by this instr
118   const unsigned *ImplicitDefs;  // Registers implicitly defined by this instr
119   const TargetOperandInfo *OpInfo; // 'NumOperands' entries about operands.
120
121   /// getOperandConstraint - Returns the value of the specific constraint if
122   /// it is set. Returns -1 if it is not set.
123   int getOperandConstraint(unsigned OpNum,
124                            TOI::OperandConstraint Constraint) const {
125     if (OpNum < NumOperands &&
126         (OpInfo[OpNum].Constraints & (1 << Constraint))) {
127       unsigned Pos = 16 + Constraint * 4;
128       return (int)(OpInfo[OpNum].Constraints >> Pos) & 0xf;
129     }
130     return -1;
131   }
132
133   /// findTiedToSrcOperand - Returns the operand that is tied to the specified
134   /// dest operand. Returns -1 if there isn't one.
135   int findTiedToSrcOperand(unsigned OpNum) const;
136   
137   /// getOpcode - Return the opcode number for this descriptor.
138   unsigned getOpcode() const {
139     return Opcode;
140   }
141   
142   /// getName - Return the name of the record in the .td file for this
143   /// instruction, for example "ADD8ri".
144   const char *getName() const {
145     return Name;
146   }
147   
148   /// getNumOperands - Return the number of declared MachineOperands for this
149   /// MachineInstruction.  Note that variadic (isVariadic() returns true)
150   /// instructions may have additional operands at the end of the list, and note
151   /// that the machine instruction may include implicit register def/uses as
152   /// well.
153   unsigned getNumOperands() const {
154     return NumOperands;
155   }
156   
157   /// getNumDefs - Return the number of MachineOperands that are register
158   /// definitions.  Register definitions always occur at the start of the 
159   /// machine operand list.  This is the number of "outs" in the .td file.
160   unsigned getNumDefs() const {
161     return NumDefs;
162   }
163   
164   /// isVariadic - Return true if this instruction can have a variable number of
165   /// operands.  In this case, the variable operands will be after the normal
166   /// operands but before the implicit definitions and uses (if any are
167   /// present).
168   bool isVariadic() const {
169     return Flags & (1 << TID::Variadic);
170   }
171   
172   /// hasOptionalDef - Set if this instruction has an optional definition, e.g.
173   /// ARM instructions which can set condition code if 's' bit is set.
174   bool hasOptionalDef() const {
175     return Flags & (1 << TID::HasOptionalDef);
176   }
177   
178   /// getImplicitUses - Return a list of machine operands that are potentially
179   /// read by any instance of this machine instruction.  For example, on X86,
180   /// the "adc" instruction adds two register operands and adds the carry bit in
181   /// from the flags register.  In this case, the instruction is marked as
182   /// implicitly reading the flags.  Likewise, the variable shift instruction on
183   /// X86 is marked as implicitly reading the 'CL' register, which it always
184   /// does.
185   ///
186   /// This method returns null if the instruction has no implicit uses.
187   const unsigned *getImplicitUses() const {
188     return ImplicitUses;
189   }
190   
191   /// getImplicitDefs - Return a list of machine operands that are potentially
192   /// written by any instance of this machine instruction.  For example, on X86,
193   /// many instructions implicitly set the flags register.  In this case, they
194   /// are marked as setting the FLAGS.  Likewise, many instructions always
195   /// deposit their result in a physical register.  For example, the X86 divide
196   /// instruction always deposits the quotient and remainder in the EAX/EDX
197   /// registers.  For that instruction, this will return a list containing the
198   /// EAX/EDX/EFLAGS registers.
199   ///
200   /// This method returns null if the instruction has no implicit uses.
201   const unsigned *getImplicitDefs() const {
202     return ImplicitDefs;
203   }
204
205   /// getSchedClass - Return the scheduling class for this instruction.  The
206   /// scheduling class is an index into the InstrItineraryData table.  This
207   /// returns zero if there is no known scheduling information for the
208   /// instruction.
209   ///
210   unsigned getSchedClass() const {
211     return SchedClass;
212   }
213   
214   bool isReturn() const {
215     return Flags & (1 << TID::Return);
216   }
217   
218   bool isCall() const {
219     return Flags & (1 << TID::Call);
220   }
221   
222   /// isImplicitDef - Return true if this is an "IMPLICIT_DEF" instruction,
223   /// which defines a register to an unspecified value.  These basically
224   /// correspond to x = undef.
225   bool isImplicitDef() const {
226     return Flags & (1 << TID::ImplicitDef);
227   }
228   
229   /// isBarrier - Returns true if the specified instruction stops control flow
230   /// from executing the instruction immediately following it.  Examples include
231   /// unconditional branches and return instructions.
232   bool isBarrier() const {
233     return Flags & (1 << TID::Barrier);
234   }
235   
236   /// isTerminator - Returns true if this instruction part of the terminator for
237   /// a basic block.  Typically this is things like return and branch
238   /// instructions.
239   ///
240   /// Various passes use this to insert code into the bottom of a basic block,
241   /// but before control flow occurs.
242   bool isTerminator() const {
243     return Flags & (1 << TID::Terminator);
244   }
245   
246   /// isBranch - Returns true if this is a conditional, unconditional, or
247   /// indirect branch.  Predicates below can be used to discriminate between
248   /// these cases, and the TargetInstrInfo::AnalyzeBranch method can be used to
249   /// get more information.
250   bool isBranch() const {
251     return Flags & (1 << TID::Branch);
252   }
253
254   /// isIndirectBranch - Return true if this is an indirect branch, such as a
255   /// branch through a register.
256   bool isIndirectBranch() const {
257     return Flags & (1 << TID::IndirectBranch);
258   }
259   
260   /// isConditionalBranch - Return true if this is a branch which may fall
261   /// through to the next instruction or may transfer control flow to some other
262   /// block.  The TargetInstrInfo::AnalyzeBranch method can be used to get more
263   /// information about this branch.
264   bool isConditionalBranch() const {
265     return isBranch() & !isBarrier() & !isIndirectBranch();
266   }
267   
268   /// isUnconditionalBranch - Return true if this is a branch which always
269   /// transfers control flow to some other block.  The
270   /// TargetInstrInfo::AnalyzeBranch method can be used to get more information
271   /// about this branch.
272   bool isUnconditionalBranch() const {
273     return isBranch() & isBarrier() & !isIndirectBranch();
274   }
275   
276   // isPredicable - Return true if this instruction has a predicate operand that
277   // controls execution.  It may be set to 'always', or may be set to other
278   /// values.   There are various methods in TargetInstrInfo that can be used to
279   /// control and modify the predicate in this instruction.
280   bool isPredicable() const {
281     return Flags & (1 << TID::Predicable);
282   }
283   
284   /// isNotDuplicable - Return true if this instruction cannot be safely
285   /// duplicated.  For example, if the instruction has a unique labels attached
286   /// to it, duplicating it would cause multiple definition errors.
287   bool isNotDuplicable() const {
288     return Flags & (1 << TID::NotDuplicable);
289   }
290   
291   /// hasDelaySlot - Returns true if the specified instruction has a delay slot
292   /// which must be filled by the code generator.
293   bool hasDelaySlot() const {
294     return Flags & (1 << TID::DelaySlot);
295   }
296   
297   /// isSimpleLoad - Return true for instructions that are simple loads from
298   /// memory.  This should only be set on instructions that load a value from
299   /// memory and return it in their only virtual register definition.
300   /// Instructions that return a value loaded from memory and then modified in
301   /// some way should not return true for this.
302   bool isSimpleLoad() const {
303     return Flags & (1 << TID::SimpleLoad);
304   }
305   
306   //===--------------------------------------------------------------------===//
307   // Side Effect Analysis
308   //===--------------------------------------------------------------------===//
309
310   /// mayLoad - Return true if this instruction could possibly read memory.
311   /// Instructions with this flag set are not necessarily simple load
312   /// instructions, they may load a value and modify it, for example.
313   bool mayLoad() const {
314     return Flags & (1 << TID::MayLoad);
315   }
316   
317   
318   /// mayStore - Return true if this instruction could possibly modify memory.
319   /// Instructions with this flag set are not necessarily simple store
320   /// instructions, they may store a modified value based on their operands, or
321   /// may not actually modify anything, for example.
322   bool mayStore() const {
323     return Flags & (1 << TID::MayStore);
324   }
325   
326   /// hasUnmodeledSideEffects - Return true if this instruction has side
327   /// effects that are not modeled by other flags.  This does not return true
328   /// for instructions whose effects are captured by:
329   ///
330   ///  1. Their operand list and implicit definition/use list.  Register use/def
331   ///     info is explicit for instructions.
332   ///  2. Memory accesses.  Use mayLoad/mayStore.
333   ///  3. Calling, branching, returning: use isCall/isReturn/isBranch.
334   ///
335   /// Examples of side effects would be modifying 'invisible' machine state like
336   /// a control register, flushing a cache, modifying a register invisible to
337   /// LLVM, etc.
338   ///
339   bool hasUnmodeledSideEffects() const {
340     return Flags & (1 << TID::UnmodeledSideEffects);
341   }
342   
343   //===--------------------------------------------------------------------===//
344   // Flags that indicate whether an instruction can be modified by a method.
345   //===--------------------------------------------------------------------===//
346   
347   /// isCommutable - Return true if this may be a 2- or 3-address
348   /// instruction (of the form "X = op Y, Z, ..."), which produces the same
349   /// result if Y and Z are exchanged.  If this flag is set, then the 
350   /// TargetInstrInfo::commuteInstruction method may be used to hack on the
351   /// instruction.
352   ///
353   /// Note that this flag may be set on instructions that are only commutable
354   /// sometimes.  In these cases, the call to commuteInstruction will fail.
355   /// Also note that some instructions require non-trivial modification to
356   /// commute them.
357   bool isCommutable() const {
358     return Flags & (1 << TID::Commutable);
359   }
360   
361   /// isConvertibleTo3Addr - Return true if this is a 2-address instruction
362   /// which can be changed into a 3-address instruction if needed.  Doing this
363   /// transformation can be profitable in the register allocator, because it
364   /// means that the instruction can use a 2-address form if possible, but
365   /// degrade into a less efficient form if the source and dest register cannot
366   /// be assigned to the same register.  For example, this allows the x86
367   /// backend to turn a "shl reg, 3" instruction into an LEA instruction, which
368   /// is the same speed as the shift but has bigger code size.
369   ///
370   /// If this returns true, then the target must implement the
371   /// TargetInstrInfo::convertToThreeAddress method for this instruction, which
372   /// is allowed to fail if the transformation isn't valid for this specific
373   /// instruction (e.g. shl reg, 4 on x86).
374   ///
375   bool isConvertibleTo3Addr() const {
376     return Flags & (1 << TID::ConvertibleTo3Addr);
377   }
378   
379   /// usesCustomDAGSchedInsertionHook - Return true if this instruction requires
380   /// custom insertion support when the DAG scheduler is inserting it into a
381   /// machine basic block.  If this is true for the instruction, it basically
382   /// means that it is a pseudo instruction used at SelectionDAG time that is 
383   /// expanded out into magic code by the target when MachineInstrs are formed.
384   ///
385   /// If this is true, the TargetLoweringInfo::InsertAtEndOfBasicBlock method
386   /// is used to insert this into the MachineBasicBlock.
387   bool usesCustomDAGSchedInsertionHook() const {
388     return Flags & (1 << TID::UsesCustomDAGSchedInserter);
389   }
390   
391   /// isRematerializable - Returns true if this instruction is a candidate for
392   /// remat.  This flag is deprecated, please don't use it anymore.  If this
393   /// flag is set, the isReallyTriviallyReMaterializable() method is called to
394   /// verify the instruction is really rematable.
395   bool isRematerializable() const {
396     return Flags & (1 << TID::Rematerializable);
397   }
398 };
399
400 } // end namespace llvm
401
402 #endif