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