Added TargetInstrDescriptor::numDefs - num of results.
[oota-llvm.git] / include / llvm / Target / TargetInstrInfo.h
1 //===-- llvm/Target/TargetInstrInfo.h - Instruction Info --------*- 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 describes the target machine instructions to the code generator.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TARGET_TARGETINSTRINFO_H
15 #define LLVM_TARGET_TARGETINSTRINFO_H
16
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/Support/DataTypes.h"
20 #include <vector>
21 #include <cassert>
22
23 namespace llvm {
24
25 class MachineInstr;
26 class TargetMachine;
27 class MachineCodeForInstruction;
28 class TargetRegisterClass;
29 class LiveVariables;
30
31 //---------------------------------------------------------------------------
32 // Data types used to define information about a single machine instruction
33 //---------------------------------------------------------------------------
34
35 typedef short MachineOpCode;
36 typedef unsigned InstrSchedClass;
37
38 //---------------------------------------------------------------------------
39 // struct TargetInstrDescriptor:
40 //  Predefined information about each machine instruction.
41 //  Designed to initialized statically.
42 //
43
44 const unsigned M_BRANCH_FLAG           = 1 << 0;
45 const unsigned M_CALL_FLAG             = 1 << 1;
46 const unsigned M_RET_FLAG              = 1 << 2;
47 const unsigned M_BARRIER_FLAG          = 1 << 3;
48 const unsigned M_DELAY_SLOT_FLAG       = 1 << 4;
49 const unsigned M_LOAD_FLAG             = 1 << 5;
50 const unsigned M_STORE_FLAG            = 1 << 6;
51
52 // M_CONVERTIBLE_TO_3_ADDR - This is a 2-address instruction which can be
53 // changed into a 3-address instruction if the first two operands cannot be
54 // assigned to the same register.  The target must implement the
55 // TargetInstrInfo::convertToThreeAddress method for this instruction.
56 const unsigned M_CONVERTIBLE_TO_3_ADDR = 1 << 7;
57
58 // This M_COMMUTABLE - is a 2- or 3-address instruction (of the form X = op Y,
59 // Z), which produces the same result if Y and Z are exchanged.
60 const unsigned M_COMMUTABLE            = 1 << 8;
61
62 // M_TERMINATOR_FLAG - Is this instruction part of the terminator for a basic
63 // block?  Typically this is things like return and branch instructions.
64 // Various passes use this to insert code into the bottom of a basic block, but
65 // before control flow occurs.
66 const unsigned M_TERMINATOR_FLAG       = 1 << 9;
67
68 // M_USES_CUSTOM_DAG_SCHED_INSERTION - Set if this instruction requires custom
69 // insertion support when the DAG scheduler is inserting it into a machine basic
70 // block.
71 const unsigned M_USES_CUSTOM_DAG_SCHED_INSERTION = 1 << 10;
72
73 // M_VARIABLE_OPS - Set if this instruction can have a variable number of extra
74 // operands in addition to the minimum number operands specified.
75 const unsigned M_VARIABLE_OPS = 1 << 11;
76
77 // M_PREDICABLE - Set if this instruction has a predicate operand that
78 // controls execution. It may be set to 'always'.
79 const unsigned M_PREDICABLE = 1 << 12;
80
81 // M_REMATERIALIZIBLE - Set if this instruction can be trivally re-materialized
82 // at any time, e.g. constant generation, load from constant pool.
83 const unsigned M_REMATERIALIZIBLE = 1 << 13;
84
85 // M_NOT_DUPLICABLE - Set if this instruction cannot be safely duplicated.
86 // (e.g. instructions with unique labels attached).
87 const unsigned M_NOT_DUPLICABLE = 1 << 14;
88
89 // M_HAS_OPTIONAL_DEF - Set if this instruction has an optional definition, e.g.
90 // ARM instructions which can set condition code if 's' bit is set.
91 const unsigned M_HAS_OPTIONAL_DEF = 1 << 15;
92
93 // Machine operand flags
94 // M_LOOK_UP_PTR_REG_CLASS - Set if this operand is a pointer value and it
95 // requires a callback to look up its register class.
96 const unsigned M_LOOK_UP_PTR_REG_CLASS = 1 << 0;
97
98 /// M_PREDICATE_OPERAND - Set if this is one of the operands that made up of the
99 /// predicate operand that controls an M_PREDICATED instruction.
100 const unsigned M_PREDICATE_OPERAND = 1 << 1;
101
102 /// M_OPTIONAL_DEF_OPERAND - Set if this operand is a optional def.
103 ///
104 const unsigned M_OPTIONAL_DEF_OPERAND = 1 << 2;
105
106 namespace TOI {
107   // Operand constraints: only "tied_to" for now.
108   enum OperandConstraint {
109     TIED_TO = 0  // Must be allocated the same register as.
110   };
111 }
112
113 /// TargetOperandInfo - This holds information about one operand of a machine
114 /// instruction, indicating the register class for register operands, etc.
115 ///
116 class TargetOperandInfo {
117 public:
118   /// RegClass - This specifies the register class enumeration of the operand 
119   /// if the operand is a register.  If not, this contains 0.
120   unsigned short RegClass;
121   unsigned short Flags;
122   /// Lower 16 bits are used to specify which constraints are set. The higher 16
123   /// bits are used to specify the value of constraints (4 bits each).
124   unsigned int Constraints;
125   /// Currently no other information.
126 };
127
128
129 class TargetInstrDescriptor {
130 public:
131   MachineOpCode   Opcode;        // The opcode.
132   unsigned short  numOperands;   // Num of args (may be more if variable_ops).
133   unsigned short  numDefs;       // Num of args that are definitions.
134   const char *    Name;          // Assembly language mnemonic for the opcode.
135   InstrSchedClass schedClass;    // enum  identifying instr sched class
136   unsigned        Flags;         // flags identifying machine instr class
137   unsigned        TSFlags;       // Target Specific Flag values
138   const unsigned *ImplicitUses;  // Registers implicitly read by this instr
139   const unsigned *ImplicitDefs;  // Registers implicitly defined by this instr
140   const TargetOperandInfo *OpInfo; // 'numOperands' entries about operands.
141
142   /// getOperandConstraint - Returns the value of the specific constraint if
143   /// it is set. Returns -1 if it is not set.
144   int getOperandConstraint(unsigned OpNum,
145                            TOI::OperandConstraint Constraint) const {
146     assert((OpNum < numOperands || (Flags & M_VARIABLE_OPS)) &&
147            "Invalid operand # of TargetInstrInfo");
148     if (OpNum < numOperands &&
149         (OpInfo[OpNum].Constraints & (1 << Constraint))) {
150       unsigned Pos = 16 + Constraint * 4;
151       return (int)(OpInfo[OpNum].Constraints >> Pos) & 0xf;
152     }
153     return -1;
154   }
155
156   /// findTiedToSrcOperand - Returns the operand that is tied to the specified
157   /// dest operand. Returns -1 if there isn't one.
158   int findTiedToSrcOperand(unsigned OpNum) const;
159 };
160
161
162 //---------------------------------------------------------------------------
163 ///
164 /// TargetInstrInfo - Interface to description of machine instructions
165 ///
166 class TargetInstrInfo {
167   const TargetInstrDescriptor* desc;    // raw array to allow static init'n
168   unsigned NumOpcodes;                  // number of entries in the desc array
169   unsigned numRealOpCodes;              // number of non-dummy op codes
170
171   TargetInstrInfo(const TargetInstrInfo &);  // DO NOT IMPLEMENT
172   void operator=(const TargetInstrInfo &);   // DO NOT IMPLEMENT
173 public:
174   TargetInstrInfo(const TargetInstrDescriptor *desc, unsigned NumOpcodes);
175   virtual ~TargetInstrInfo();
176
177   // Invariant opcodes: All instruction sets have these as their low opcodes.
178   enum { 
179     PHI = 0,
180     INLINEASM = 1,
181     LABEL = 2,
182     EXTRACT_SUBREG = 3,
183     INSERT_SUBREG = 4
184   };
185
186   unsigned getNumOpcodes() const { return NumOpcodes; }
187
188   /// get - Return the machine instruction descriptor that corresponds to the
189   /// specified instruction opcode.
190   ///
191   const TargetInstrDescriptor& get(MachineOpCode Opcode) const {
192     assert((unsigned)Opcode < NumOpcodes);
193     return desc[Opcode];
194   }
195
196   const char *getName(MachineOpCode Opcode) const {
197     return get(Opcode).Name;
198   }
199
200   int getNumOperands(MachineOpCode Opcode) const {
201     return get(Opcode).numOperands;
202   }
203
204   InstrSchedClass getSchedClass(MachineOpCode Opcode) const {
205     return get(Opcode).schedClass;
206   }
207
208   const unsigned *getImplicitUses(MachineOpCode Opcode) const {
209     return get(Opcode).ImplicitUses;
210   }
211
212   const unsigned *getImplicitDefs(MachineOpCode Opcode) const {
213     return get(Opcode).ImplicitDefs;
214   }
215
216
217   //
218   // Query instruction class flags according to the machine-independent
219   // flags listed above.
220   //
221   bool isReturn(MachineOpCode Opcode) const {
222     return get(Opcode).Flags & M_RET_FLAG;
223   }
224
225   bool isCommutableInstr(MachineOpCode Opcode) const {
226     return get(Opcode).Flags & M_COMMUTABLE;
227   }
228   bool isTerminatorInstr(MachineOpCode Opcode) const {
229     return get(Opcode).Flags & M_TERMINATOR_FLAG;
230   }
231   
232   bool isBranch(MachineOpCode Opcode) const {
233     return get(Opcode).Flags & M_BRANCH_FLAG;
234   }
235   
236   /// isBarrier - Returns true if the specified instruction stops control flow
237   /// from executing the instruction immediately following it.  Examples include
238   /// unconditional branches and return instructions.
239   bool isBarrier(MachineOpCode Opcode) const {
240     return get(Opcode).Flags & M_BARRIER_FLAG;
241   }
242   
243   bool isCall(MachineOpCode Opcode) const {
244     return get(Opcode).Flags & M_CALL_FLAG;
245   }
246   bool isLoad(MachineOpCode Opcode) const {
247     return get(Opcode).Flags & M_LOAD_FLAG;
248   }
249   bool isStore(MachineOpCode Opcode) const {
250     return get(Opcode).Flags & M_STORE_FLAG;
251   }
252   
253   /// hasDelaySlot - Returns true if the specified instruction has a delay slot
254   /// which must be filled by the code generator.
255   bool hasDelaySlot(MachineOpCode Opcode) const {
256     return get(Opcode).Flags & M_DELAY_SLOT_FLAG;
257   }
258   
259   /// usesCustomDAGSchedInsertionHook - Return true if this instruction requires
260   /// custom insertion support when the DAG scheduler is inserting it into a
261   /// machine basic block.
262   bool usesCustomDAGSchedInsertionHook(MachineOpCode Opcode) const {
263     return get(Opcode).Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION;
264   }
265
266   bool hasVariableOperands(MachineOpCode Opcode) const {
267     return get(Opcode).Flags & M_VARIABLE_OPS;
268   }
269
270   bool isPredicable(MachineOpCode Opcode) const {
271     return get(Opcode).Flags & M_PREDICABLE;
272   }
273
274   bool isNotDuplicable(MachineOpCode Opcode) const {
275     return get(Opcode).Flags & M_NOT_DUPLICABLE;
276   }
277
278   bool hasOptionalDef(MachineOpCode Opcode) const {
279     return get(Opcode).Flags & M_HAS_OPTIONAL_DEF;
280   }
281
282   /// isTriviallyReMaterializable - Return true if the instruction is trivially
283   /// rematerializable, meaning it has no side effects and requires no operands
284   /// that aren't always available.
285   bool isTriviallyReMaterializable(MachineInstr *MI) const {
286     return (MI->getInstrDescriptor()->Flags & M_REMATERIALIZIBLE) &&
287            isReallyTriviallyReMaterializable(MI);
288   }
289
290 protected:
291   /// isReallyTriviallyReMaterializable - For instructions with opcodes for
292   /// which the M_REMATERIALIZABLE flag is set, this function tests whether the
293   /// instruction itself is actually trivially rematerializable, considering
294   /// its operands.  This is used for targets that have instructions that are
295   /// only trivially rematerializable for specific uses.  This predicate must
296   /// return false if the instruction has any side effects other than
297   /// producing a value, or if it requres any address registers that are not
298   /// always available.
299   virtual bool isReallyTriviallyReMaterializable(MachineInstr *MI) const {
300     return true;
301   }
302
303 public:
304   /// getOperandConstraint - Returns the value of the specific constraint if
305   /// it is set. Returns -1 if it is not set.
306   int getOperandConstraint(MachineOpCode Opcode, unsigned OpNum,
307                            TOI::OperandConstraint Constraint) const {
308     return get(Opcode).getOperandConstraint(OpNum, Constraint);
309   }
310
311   /// Return true if the instruction is a register to register move
312   /// and leave the source and dest operands in the passed parameters.
313   virtual bool isMoveInstr(const MachineInstr& MI,
314                            unsigned& sourceReg,
315                            unsigned& destReg) const {
316     return false;
317   }
318   
319   /// isLoadFromStackSlot - If the specified machine instruction is a direct
320   /// load from a stack slot, return the virtual or physical register number of
321   /// the destination along with the FrameIndex of the loaded stack slot.  If
322   /// not, return 0.  This predicate must return 0 if the instruction has
323   /// any side effects other than loading from the stack slot.
324   virtual unsigned isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const{
325     return 0;
326   }
327   
328   /// isStoreToStackSlot - If the specified machine instruction is a direct
329   /// store to a stack slot, return the virtual or physical register number of
330   /// the source reg along with the FrameIndex of the loaded stack slot.  If
331   /// not, return 0.  This predicate must return 0 if the instruction has
332   /// any side effects other than storing to the stack slot.
333   virtual unsigned isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const {
334     return 0;
335   }
336
337   /// convertToThreeAddress - This method must be implemented by targets that
338   /// set the M_CONVERTIBLE_TO_3_ADDR flag.  When this flag is set, the target
339   /// may be able to convert a two-address instruction into one or more true
340   /// three-address instructions on demand.  This allows the X86 target (for
341   /// example) to convert ADD and SHL instructions into LEA instructions if they
342   /// would require register copies due to two-addressness.
343   ///
344   /// This method returns a null pointer if the transformation cannot be
345   /// performed, otherwise it returns the last new instruction.
346   ///
347   virtual MachineInstr *
348   convertToThreeAddress(MachineFunction::iterator &MFI,
349                    MachineBasicBlock::iterator &MBBI, LiveVariables &LV) const {
350     return 0;
351   }
352
353   /// commuteInstruction - If a target has any instructions that are commutable,
354   /// but require converting to a different instruction or making non-trivial
355   /// changes to commute them, this method can overloaded to do this.  The
356   /// default implementation of this method simply swaps the first two operands
357   /// of MI and returns it.
358   ///
359   /// If a target wants to make more aggressive changes, they can construct and
360   /// return a new machine instruction.  If an instruction cannot commute, it
361   /// can also return null.
362   ///
363   virtual MachineInstr *commuteInstruction(MachineInstr *MI) const;
364
365   /// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
366   /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
367   /// implemented for a target).  Upon success, this returns false and returns
368   /// with the following information in various cases:
369   ///
370   /// 1. If this block ends with no branches (it just falls through to its succ)
371   ///    just return false, leaving TBB/FBB null.
372   /// 2. If this block ends with only an unconditional branch, it sets TBB to be
373   ///    the destination block.
374   /// 3. If this block ends with an conditional branch and it falls through to
375   ///    an successor block, it sets TBB to be the branch destination block and a
376   ///    list of operands that evaluate the condition. These
377   ///    operands can be passed to other TargetInstrInfo methods to create new
378   ///    branches.
379   /// 4. If this block ends with an conditional branch and an unconditional
380   ///    block, it returns the 'true' destination in TBB, the 'false' destination
381   ///    in FBB, and a list of operands that evaluate the condition. These
382   ///    operands can be passed to other TargetInstrInfo methods to create new
383   ///    branches.
384   ///
385   /// Note that RemoveBranch and InsertBranch must be implemented to support
386   /// cases where this method returns success.
387   ///
388   virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
389                              MachineBasicBlock *&FBB,
390                              std::vector<MachineOperand> &Cond) const {
391     return true;
392   }
393   
394   /// RemoveBranch - Remove the branching code at the end of the specific MBB.
395   /// this is only invoked in cases where AnalyzeBranch returns success. It
396   /// returns the number of instructions that were removed.
397   virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const {
398     assert(0 && "Target didn't implement TargetInstrInfo::RemoveBranch!"); 
399     return 0;
400   }
401   
402   /// InsertBranch - Insert a branch into the end of the specified
403   /// MachineBasicBlock.  This operands to this method are the same as those
404   /// returned by AnalyzeBranch.  This is invoked in cases where AnalyzeBranch
405   /// returns success and when an unconditional branch (TBB is non-null, FBB is
406   /// null, Cond is empty) needs to be inserted. It returns the number of
407   /// instructions inserted.
408   virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
409                             MachineBasicBlock *FBB,
410                             const std::vector<MachineOperand> &Cond) const {
411     assert(0 && "Target didn't implement TargetInstrInfo::InsertBranch!"); 
412     return 0;
413   }
414   
415   /// BlockHasNoFallThrough - Return true if the specified block does not
416   /// fall-through into its successor block.  This is primarily used when a
417   /// branch is unanalyzable.  It is useful for things like unconditional
418   /// indirect branches (jump tables).
419   virtual bool BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
420     return false;
421   }
422   
423   /// ReverseBranchCondition - Reverses the branch condition of the specified
424   /// condition list, returning false on success and true if it cannot be
425   /// reversed.
426   virtual bool ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
427     return true;
428   }
429   
430   /// insertNoop - Insert a noop into the instruction stream at the specified
431   /// point.
432   virtual void insertNoop(MachineBasicBlock &MBB, 
433                           MachineBasicBlock::iterator MI) const {
434     assert(0 && "Target didn't implement insertNoop!");
435     abort();
436   }
437
438   /// isPredicated - Returns true if the instruction is already predicated.
439   ///
440   virtual bool isPredicated(const MachineInstr *MI) const {
441     return false;
442   }
443
444   /// isUnpredicatedTerminator - Returns true if the instruction is a
445   /// terminator instruction that has not been predicated.
446   virtual bool isUnpredicatedTerminator(const MachineInstr *MI) const;
447
448   /// PredicateInstruction - Convert the instruction into a predicated
449   /// instruction. It returns true if the operation was successful.
450   virtual
451   bool PredicateInstruction(MachineInstr *MI,
452                             const std::vector<MachineOperand> &Pred) const;
453
454   /// SubsumesPredicate - Returns true if the first specified predicate
455   /// subsumes the second, e.g. GE subsumes GT.
456   virtual
457   bool SubsumesPredicate(const std::vector<MachineOperand> &Pred1,
458                          const std::vector<MachineOperand> &Pred2) const {
459     return false;
460   }
461
462   /// DefinesPredicate - If the specified instruction defines any predicate
463   /// or condition code register(s) used for predication, returns true as well
464   /// as the definition predicate(s) by reference.
465   virtual bool DefinesPredicate(MachineInstr *MI,
466                                 std::vector<MachineOperand> &Pred) const {
467     return false;
468   }
469
470   /// getPointerRegClass - Returns a TargetRegisterClass used for pointer
471   /// values.
472   virtual const TargetRegisterClass *getPointerRegClass() const {
473     assert(0 && "Target didn't implement getPointerRegClass!");
474     abort();
475     return 0; // Must return a value in order to compile with VS 2005
476   }
477 };
478
479 } // End llvm namespace
480
481 #endif