TargetInstrInfo.h revision c93457053cfecb24105ee3800c8e53921b950d8f
1//===-- llvm/Target/TargetInstrInfo.h - Instruction Info --------*- 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 describes the target machine instruction set to the code generator.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TARGET_TARGETINSTRINFO_H
15#define LLVM_TARGET_TARGETINSTRINFO_H
16
17#include "llvm/Target/TargetInstrDesc.h"
18#include "llvm/CodeGen/MachineFunction.h"
19
20namespace llvm {
21
22class TargetRegisterClass;
23class LiveVariables;
24class CalleeSavedInfo;
25class SDNode;
26class SelectionDAG;
27
28template<class T> class SmallVectorImpl;
29
30
31//---------------------------------------------------------------------------
32///
33/// TargetInstrInfo - Interface to description of machine instruction set
34///
35class TargetInstrInfo {
36  const TargetInstrDesc *Descriptors; // Raw array to allow static init'n
37  unsigned NumOpcodes;                // Number of entries in the desc array
38
39  TargetInstrInfo(const TargetInstrInfo &);  // DO NOT IMPLEMENT
40  void operator=(const TargetInstrInfo &);   // DO NOT IMPLEMENT
41public:
42  TargetInstrInfo(const TargetInstrDesc *desc, unsigned NumOpcodes);
43  virtual ~TargetInstrInfo();
44
45  // Invariant opcodes: All instruction sets have these as their low opcodes.
46  enum {
47    PHI = 0,
48    INLINEASM = 1,
49    DBG_LABEL = 2,
50    EH_LABEL = 3,
51    GC_LABEL = 4,
52    DECLARE = 5,
53    EXTRACT_SUBREG = 6,
54    INSERT_SUBREG = 7,
55    IMPLICIT_DEF = 8,
56    SUBREG_TO_REG = 9
57  };
58
59  unsigned getNumOpcodes() const { return NumOpcodes; }
60
61  /// get - Return the machine instruction descriptor that corresponds to the
62  /// specified instruction opcode.
63  ///
64  const TargetInstrDesc &get(unsigned Opcode) const {
65    assert(Opcode < NumOpcodes && "Invalid opcode!");
66    return Descriptors[Opcode];
67  }
68
69  /// isTriviallyReMaterializable - Return true if the instruction is trivially
70  /// rematerializable, meaning it has no side effects and requires no operands
71  /// that aren't always available.
72  bool isTriviallyReMaterializable(const MachineInstr *MI) const {
73    return MI->getDesc().isRematerializable() &&
74           isReallyTriviallyReMaterializable(MI);
75  }
76
77protected:
78  /// isReallyTriviallyReMaterializable - For instructions with opcodes for
79  /// which the M_REMATERIALIZABLE flag is set, this function tests whether the
80  /// instruction itself is actually trivially rematerializable, considering
81  /// its operands.  This is used for targets that have instructions that are
82  /// only trivially rematerializable for specific uses.  This predicate must
83  /// return false if the instruction has any side effects other than
84  /// producing a value, or if it requres any address registers that are not
85  /// always available.
86  virtual bool isReallyTriviallyReMaterializable(const MachineInstr *MI) const {
87    return true;
88  }
89
90public:
91  /// Return true if the instruction is a register to register move
92  /// and leave the source and dest operands in the passed parameters.
93  virtual bool isMoveInstr(const MachineInstr& MI,
94                           unsigned& sourceReg,
95                           unsigned& destReg) const {
96    return false;
97  }
98
99  /// isLoadFromStackSlot - If the specified machine instruction is a direct
100  /// load from a stack slot, return the virtual or physical register number of
101  /// the destination along with the FrameIndex of the loaded stack slot.  If
102  /// not, return 0.  This predicate must return 0 if the instruction has
103  /// any side effects other than loading from the stack slot.
104  virtual unsigned isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const{
105    return 0;
106  }
107
108  /// isStoreToStackSlot - If the specified machine instruction is a direct
109  /// store to a stack slot, return the virtual or physical register number of
110  /// the source reg along with the FrameIndex of the loaded stack slot.  If
111  /// not, return 0.  This predicate must return 0 if the instruction has
112  /// any side effects other than storing to the stack slot.
113  virtual unsigned isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const {
114    return 0;
115  }
116
117  /// reMaterialize - Re-issue the specified 'original' instruction at the
118  /// specific location targeting a new destination register.
119  virtual void reMaterialize(MachineBasicBlock &MBB,
120                             MachineBasicBlock::iterator MI,
121                             unsigned DestReg,
122                             const MachineInstr *Orig) const = 0;
123
124  /// isInvariantLoad - Return true if the specified instruction (which is
125  /// marked mayLoad) is loading from a location whose value is invariant across
126  /// the function.  For example, loading a value from the constant pool or from
127  /// from the argument area of a function if it does not change.  This should
128  /// only return true of *all* loads the instruction does are invariant (if it
129  /// does multiple loads).
130  virtual bool isInvariantLoad(MachineInstr *MI) const {
131    return false;
132  }
133
134  /// convertToThreeAddress - This method must be implemented by targets that
135  /// set the M_CONVERTIBLE_TO_3_ADDR flag.  When this flag is set, the target
136  /// may be able to convert a two-address instruction into one or more true
137  /// three-address instructions on demand.  This allows the X86 target (for
138  /// example) to convert ADD and SHL instructions into LEA instructions if they
139  /// would require register copies due to two-addressness.
140  ///
141  /// This method returns a null pointer if the transformation cannot be
142  /// performed, otherwise it returns the last new instruction.
143  ///
144  virtual MachineInstr *
145  convertToThreeAddress(MachineFunction::iterator &MFI,
146                   MachineBasicBlock::iterator &MBBI, LiveVariables *LV) const {
147    return 0;
148  }
149
150  /// commuteInstruction - If a target has any instructions that are commutable,
151  /// but require converting to a different instruction or making non-trivial
152  /// changes to commute them, this method can overloaded to do this.  The
153  /// default implementation of this method simply swaps the first two operands
154  /// of MI and returns it.
155  ///
156  /// If a target wants to make more aggressive changes, they can construct and
157  /// return a new machine instruction.  If an instruction cannot commute, it
158  /// can also return null.
159  ///
160  /// If NewMI is true, then a new machine instruction must be created.
161  ///
162  virtual MachineInstr *commuteInstruction(MachineInstr *MI,
163                                           bool NewMI = false) const = 0;
164
165  /// CommuteChangesDestination - Return true if commuting the specified
166  /// instruction will also changes the destination operand. Also return the
167  /// current operand index of the would be new destination register by
168  /// reference. This can happen when the commutable instruction is also a
169  /// two-address instruction.
170  virtual bool CommuteChangesDestination(MachineInstr *MI,
171                                         unsigned &OpIdx) const = 0;
172
173  /// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
174  /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
175  /// implemented for a target).  Upon success, this returns false and returns
176  /// with the following information in various cases:
177  ///
178  /// 1. If this block ends with no branches (it just falls through to its succ)
179  ///    just return false, leaving TBB/FBB null.
180  /// 2. If this block ends with only an unconditional branch, it sets TBB to be
181  ///    the destination block.
182  /// 3. If this block ends with an conditional branch and it falls through to
183  ///    an successor block, it sets TBB to be the branch destination block and a
184  ///    list of operands that evaluate the condition. These
185  ///    operands can be passed to other TargetInstrInfo methods to create new
186  ///    branches.
187  /// 4. If this block ends with an conditional branch and an unconditional
188  ///    block, it returns the 'true' destination in TBB, the 'false' destination
189  ///    in FBB, and a list of operands that evaluate the condition. These
190  ///    operands can be passed to other TargetInstrInfo methods to create new
191  ///    branches.
192  ///
193  /// Note that RemoveBranch and InsertBranch must be implemented to support
194  /// cases where this method returns success.
195  ///
196  virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
197                             MachineBasicBlock *&FBB,
198                             SmallVectorImpl<MachineOperand> &Cond) const {
199    return true;
200  }
201
202  /// RemoveBranch - Remove the branching code at the end of the specific MBB.
203  /// this is only invoked in cases where AnalyzeBranch returns success. It
204  /// returns the number of instructions that were removed.
205  virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const {
206    assert(0 && "Target didn't implement TargetInstrInfo::RemoveBranch!");
207    return 0;
208  }
209
210  /// InsertBranch - Insert a branch into the end of the specified
211  /// MachineBasicBlock.  This operands to this method are the same as those
212  /// returned by AnalyzeBranch.  This is invoked in cases where AnalyzeBranch
213  /// returns success and when an unconditional branch (TBB is non-null, FBB is
214  /// null, Cond is empty) needs to be inserted. It returns the number of
215  /// instructions inserted.
216  virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
217                            MachineBasicBlock *FBB,
218                            const SmallVectorImpl<MachineOperand> &Cond) const {
219    assert(0 && "Target didn't implement TargetInstrInfo::InsertBranch!");
220    return 0;
221  }
222
223  /// copyRegToReg - Add a copy between a pair of registers
224  virtual bool copyRegToReg(MachineBasicBlock &MBB,
225                            MachineBasicBlock::iterator MI,
226                            unsigned DestReg, unsigned SrcReg,
227                            const TargetRegisterClass *DestRC,
228                            const TargetRegisterClass *SrcRC) const {
229    assert(0 && "Target didn't implement TargetInstrInfo::copyRegToReg!");
230    return false;
231  }
232
233  virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
234                                   MachineBasicBlock::iterator MI,
235                                   unsigned SrcReg, bool isKill, int FrameIndex,
236                                   const TargetRegisterClass *RC) const {
237    assert(0 && "Target didn't implement TargetInstrInfo::storeRegToStackSlot!");
238  }
239
240  virtual void storeRegToAddr(MachineFunction &MF, unsigned SrcReg, bool isKill,
241                              SmallVectorImpl<MachineOperand> &Addr,
242                              const TargetRegisterClass *RC,
243                              SmallVectorImpl<MachineInstr*> &NewMIs) const {
244    assert(0 && "Target didn't implement TargetInstrInfo::storeRegToAddr!");
245  }
246
247  virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
248                                    MachineBasicBlock::iterator MI,
249                                    unsigned DestReg, int FrameIndex,
250                                    const TargetRegisterClass *RC) const {
251    assert(0 && "Target didn't implement TargetInstrInfo::loadRegFromStackSlot!");
252  }
253
254  virtual void loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
255                               SmallVectorImpl<MachineOperand> &Addr,
256                               const TargetRegisterClass *RC,
257                               SmallVectorImpl<MachineInstr*> &NewMIs) const {
258    assert(0 && "Target didn't implement TargetInstrInfo::loadRegFromAddr!");
259  }
260
261  /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
262  /// saved registers and returns true if it isn't possible / profitable to do
263  /// so by issuing a series of store instructions via
264  /// storeRegToStackSlot(). Returns false otherwise.
265  virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
266                                         MachineBasicBlock::iterator MI,
267                                const std::vector<CalleeSavedInfo> &CSI) const {
268    return false;
269  }
270
271  /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
272  /// saved registers and returns true if it isn't possible / profitable to do
273  /// so by issuing a series of load instructions via loadRegToStackSlot().
274  /// Returns false otherwise.
275  virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
276                                           MachineBasicBlock::iterator MI,
277                                const std::vector<CalleeSavedInfo> &CSI) const {
278    return false;
279  }
280
281  /// foldMemoryOperand - Attempt to fold a load or store of the specified stack
282  /// slot into the specified machine instruction for the specified operand(s).
283  /// If this is possible, a new instruction is returned with the specified
284  /// operand folded, otherwise NULL is returned. The client is responsible for
285  /// removing the old instruction and adding the new one in the instruction
286  /// stream.
287  virtual MachineInstr* foldMemoryOperand(MachineFunction &MF,
288                                          MachineInstr* MI,
289                                          SmallVectorImpl<unsigned> &Ops,
290                                          int FrameIndex) const {
291    return 0;
292  }
293
294  /// foldMemoryOperand - Same as the previous version except it allows folding
295  /// of any load and store from / to any address, not just from a specific
296  /// stack slot.
297  virtual MachineInstr* foldMemoryOperand(MachineFunction &MF,
298                                          MachineInstr* MI,
299                                          SmallVectorImpl<unsigned> &Ops,
300                                          MachineInstr* LoadMI) const {
301    return 0;
302  }
303
304  /// canFoldMemoryOperand - Returns true if the specified load / store is
305  /// folding is possible.
306  virtual
307  bool canFoldMemoryOperand(MachineInstr *MI,
308                            SmallVectorImpl<unsigned> &Ops) const{
309    return false;
310  }
311
312  /// unfoldMemoryOperand - Separate a single instruction which folded a load or
313  /// a store or a load and a store into two or more instruction. If this is
314  /// possible, returns true as well as the new instructions by reference.
315  virtual bool unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
316                                unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
317                                  SmallVectorImpl<MachineInstr*> &NewMIs) const{
318    return false;
319  }
320
321  virtual bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
322                                   SmallVectorImpl<SDNode*> &NewNodes) const {
323    return false;
324  }
325
326  /// getOpcodeAfterMemoryUnfold - Returns the opcode of the would be new
327  /// instruction after load / store are unfolded from an instruction of the
328  /// specified opcode. It returns zero if the specified unfolding is not
329  /// possible.
330  virtual unsigned getOpcodeAfterMemoryUnfold(unsigned Opc,
331                                      bool UnfoldLoad, bool UnfoldStore) const {
332    return 0;
333  }
334
335  /// BlockHasNoFallThrough - Return true if the specified block does not
336  /// fall-through into its successor block.  This is primarily used when a
337  /// branch is unanalyzable.  It is useful for things like unconditional
338  /// indirect branches (jump tables).
339  virtual bool BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
340    return false;
341  }
342
343  /// ReverseBranchCondition - Reverses the branch condition of the specified
344  /// condition list, returning false on success and true if it cannot be
345  /// reversed.
346  virtual
347  bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
348    return true;
349  }
350
351  /// insertNoop - Insert a noop into the instruction stream at the specified
352  /// point.
353  virtual void insertNoop(MachineBasicBlock &MBB,
354                          MachineBasicBlock::iterator MI) const {
355    assert(0 && "Target didn't implement insertNoop!");
356    abort();
357  }
358
359  /// isPredicated - Returns true if the instruction is already predicated.
360  ///
361  virtual bool isPredicated(const MachineInstr *MI) const {
362    return false;
363  }
364
365  /// isUnpredicatedTerminator - Returns true if the instruction is a
366  /// terminator instruction that has not been predicated.
367  virtual bool isUnpredicatedTerminator(const MachineInstr *MI) const;
368
369  /// PredicateInstruction - Convert the instruction into a predicated
370  /// instruction. It returns true if the operation was successful.
371  virtual
372  bool PredicateInstruction(MachineInstr *MI,
373                        const SmallVectorImpl<MachineOperand> &Pred) const = 0;
374
375  /// SubsumesPredicate - Returns true if the first specified predicate
376  /// subsumes the second, e.g. GE subsumes GT.
377  virtual
378  bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
379                         const SmallVectorImpl<MachineOperand> &Pred2) const {
380    return false;
381  }
382
383  /// DefinesPredicate - If the specified instruction defines any predicate
384  /// or condition code register(s) used for predication, returns true as well
385  /// as the definition predicate(s) by reference.
386  virtual bool DefinesPredicate(MachineInstr *MI,
387                                std::vector<MachineOperand> &Pred) const {
388    return false;
389  }
390
391  /// getPointerRegClass - Returns a TargetRegisterClass used for pointer
392  /// values.
393  virtual const TargetRegisterClass *getPointerRegClass() const {
394    assert(0 && "Target didn't implement getPointerRegClass!");
395    abort();
396    return 0; // Must return a value in order to compile with VS 2005
397  }
398
399  /// GetInstSize - Returns the size of the specified Instruction.
400  ///
401  virtual unsigned GetInstSizeInBytes(const MachineInstr *MI) const {
402    assert(0 && "Target didn't implement TargetInstrInfo::GetInstSize!");
403    return 0;
404  }
405
406  /// GetFunctionSizeInBytes - Returns the size of the specified MachineFunction.
407  ///
408  virtual unsigned GetFunctionSizeInBytes(const MachineFunction &MF) const = 0;
409
410};
411
412/// TargetInstrInfoImpl - This is the default implementation of
413/// TargetInstrInfo, which just provides a couple of default implementations
414/// for various methods.  This separated out because it is implemented in
415/// libcodegen, not in libtarget.
416class TargetInstrInfoImpl : public TargetInstrInfo {
417protected:
418  TargetInstrInfoImpl(const TargetInstrDesc *desc, unsigned NumOpcodes)
419  : TargetInstrInfo(desc, NumOpcodes) {}
420public:
421  virtual MachineInstr *commuteInstruction(MachineInstr *MI,
422                                           bool NewMI = false) const;
423  virtual bool CommuteChangesDestination(MachineInstr *MI,
424                                         unsigned &OpIdx) const;
425  virtual bool PredicateInstruction(MachineInstr *MI,
426                            const SmallVectorImpl<MachineOperand> &Pred) const;
427  virtual void reMaterialize(MachineBasicBlock &MBB,
428                             MachineBasicBlock::iterator MI,
429                             unsigned DestReg,
430                             const MachineInstr *Orig) const;
431  virtual unsigned GetFunctionSizeInBytes(const MachineFunction &MF) const;
432};
433
434} // End llvm namespace
435
436#endif
437