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