1 //===-- llvm/Target/TargetInstrInfo.h - Instruction Info --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file describes the target machine instruction set to the code generator.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TARGET_TARGETINSTRINFO_H
15 #define LLVM_TARGET_TARGETINSTRINFO_H
17 #include "llvm/Target/TargetInstrDesc.h"
18 #include "llvm/CodeGen/MachineFunction.h"
23 class TargetRegisterClass;
24 class TargetRegisterInfo;
26 class CalleeSavedInfo;
29 class MachineMemOperand;
31 template<class T> class SmallVectorImpl;
34 //---------------------------------------------------------------------------
36 /// TargetInstrInfo - Interface to description of machine instruction set
38 class TargetInstrInfo {
39 const TargetInstrDesc *Descriptors; // Raw array to allow static init'n
40 unsigned NumOpcodes; // Number of entries in the desc array
42 TargetInstrInfo(const TargetInstrInfo &); // DO NOT IMPLEMENT
43 void operator=(const TargetInstrInfo &); // DO NOT IMPLEMENT
45 TargetInstrInfo(const TargetInstrDesc *desc, unsigned NumOpcodes);
46 virtual ~TargetInstrInfo();
48 unsigned getNumOpcodes() const { return NumOpcodes; }
50 /// get - Return the machine instruction descriptor that corresponds to the
51 /// specified instruction opcode.
53 const TargetInstrDesc &get(unsigned Opcode) const {
54 assert(Opcode < NumOpcodes && "Invalid opcode!");
55 return Descriptors[Opcode];
58 /// isTriviallyReMaterializable - Return true if the instruction is trivially
59 /// rematerializable, meaning it has no side effects and requires no operands
60 /// that aren't always available.
61 bool isTriviallyReMaterializable(const MachineInstr *MI,
62 AliasAnalysis *AA = 0) const {
63 return MI->getOpcode() == TargetOpcode::IMPLICIT_DEF ||
64 (MI->getDesc().isRematerializable() &&
65 (isReallyTriviallyReMaterializable(MI, AA) ||
66 isReallyTriviallyReMaterializableGeneric(MI, AA)));
70 /// isReallyTriviallyReMaterializable - For instructions with opcodes for
71 /// which the M_REMATERIALIZABLE flag is set, this hook lets the target
72 /// specify whether the instruction is actually trivially rematerializable,
73 /// taking into consideration its operands. This predicate must return false
74 /// if the instruction has any side effects other than producing a value, or
75 /// if it requres any address registers that are not always available.
76 virtual bool isReallyTriviallyReMaterializable(const MachineInstr *MI,
77 AliasAnalysis *AA) const {
82 /// isReallyTriviallyReMaterializableGeneric - For instructions with opcodes
83 /// for which the M_REMATERIALIZABLE flag is set and the target hook
84 /// isReallyTriviallyReMaterializable returns false, this function does
85 /// target-independent tests to determine if the instruction is really
86 /// trivially rematerializable.
87 bool isReallyTriviallyReMaterializableGeneric(const MachineInstr *MI,
88 AliasAnalysis *AA) const;
91 /// isMoveInstr - Return true if the instruction is a register to register
92 /// move and return the source and dest operands and their sub-register
93 /// indices by reference.
94 virtual bool isMoveInstr(const MachineInstr& MI,
95 unsigned& SrcReg, unsigned& DstReg,
96 unsigned& SrcSubIdx, unsigned& DstSubIdx) const {
100 /// isCoalescableExtInstr - Return true if the instruction is a "coalescable"
101 /// extension instruction. That is, it's like a copy where it's legal for the
102 /// source to overlap the destination. e.g. X86::MOVSX64rr32. If this returns
103 /// true, then it's expected the pre-extension value is available as a subreg
104 /// of the result register. This also returns the sub-register index in
106 virtual bool isCoalescableExtInstr(const MachineInstr &MI,
107 unsigned &SrcReg, unsigned &DstReg,
108 unsigned &SubIdx) const {
112 /// isIdentityCopy - Return true if the instruction is a copy (or
113 /// extract_subreg, insert_subreg, subreg_to_reg) where the source and
114 /// destination registers are the same.
115 bool isIdentityCopy(const MachineInstr &MI) const {
116 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
117 if (isMoveInstr(MI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) &&
121 if (MI.getOpcode() == TargetOpcode::EXTRACT_SUBREG &&
122 MI.getOperand(0).getReg() == MI.getOperand(1).getReg())
125 if ((MI.getOpcode() == TargetOpcode::INSERT_SUBREG ||
126 MI.getOpcode() == TargetOpcode::SUBREG_TO_REG) &&
127 MI.getOperand(0).getReg() == MI.getOperand(2).getReg())
132 /// isLoadFromStackSlot - If the specified machine instruction is a direct
133 /// load from a stack slot, return the virtual or physical register number of
134 /// the destination along with the FrameIndex of the loaded stack slot. If
135 /// not, return 0. This predicate must return 0 if the instruction has
136 /// any side effects other than loading from the stack slot.
137 virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
138 int &FrameIndex) const {
142 /// isLoadFromStackSlotPostFE - Check for post-frame ptr elimination
143 /// stack locations as well. This uses a heuristic so it isn't
144 /// reliable for correctness.
145 virtual unsigned isLoadFromStackSlotPostFE(const MachineInstr *MI,
146 int &FrameIndex) const {
150 /// hasLoadFromStackSlot - If the specified machine instruction has
151 /// a load from a stack slot, return true along with the FrameIndex
152 /// of the loaded stack slot and the machine mem operand containing
153 /// the reference. If not, return false. Unlike
154 /// isLoadFromStackSlot, this returns true for any instructions that
155 /// loads from the stack. This is just a hint, as some cases may be
157 virtual bool hasLoadFromStackSlot(const MachineInstr *MI,
158 const MachineMemOperand *&MMO,
159 int &FrameIndex) const {
163 /// isStoreToStackSlot - If the specified machine instruction is a direct
164 /// store to a stack slot, return the virtual or physical register number of
165 /// the source reg along with the FrameIndex of the loaded stack slot. If
166 /// not, return 0. This predicate must return 0 if the instruction has
167 /// any side effects other than storing to the stack slot.
168 virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
169 int &FrameIndex) const {
173 /// isStoreToStackSlotPostFE - Check for post-frame ptr elimination
174 /// stack locations as well. This uses a heuristic so it isn't
175 /// reliable for correctness.
176 virtual unsigned isStoreToStackSlotPostFE(const MachineInstr *MI,
177 int &FrameIndex) const {
181 /// hasStoreToStackSlot - If the specified machine instruction has a
182 /// store to a stack slot, return true along with the FrameIndex of
183 /// the loaded stack slot and the machine mem operand containing the
184 /// reference. If not, return false. Unlike isStoreToStackSlot,
185 /// this returns true for any instructions that loads from the
186 /// stack. This is just a hint, as some cases may be missed.
187 virtual bool hasStoreToStackSlot(const MachineInstr *MI,
188 const MachineMemOperand *&MMO,
189 int &FrameIndex) const {
193 /// reMaterialize - Re-issue the specified 'original' instruction at the
194 /// specific location targeting a new destination register.
195 virtual void reMaterialize(MachineBasicBlock &MBB,
196 MachineBasicBlock::iterator MI,
197 unsigned DestReg, unsigned SubIdx,
198 const MachineInstr *Orig,
199 const TargetRegisterInfo *TRI) const = 0;
201 /// duplicate - Create a duplicate of the Orig instruction in MF. This is like
202 /// MachineFunction::CloneMachineInstr(), but the target may update operands
203 /// that are required to be unique.
205 /// The instruction must be duplicable as indicated by isNotDuplicable().
206 virtual MachineInstr *duplicate(MachineInstr *Orig,
207 MachineFunction &MF) const = 0;
209 /// convertToThreeAddress - This method must be implemented by targets that
210 /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
211 /// may be able to convert a two-address instruction into one or more true
212 /// three-address instructions on demand. This allows the X86 target (for
213 /// example) to convert ADD and SHL instructions into LEA instructions if they
214 /// would require register copies due to two-addressness.
216 /// This method returns a null pointer if the transformation cannot be
217 /// performed, otherwise it returns the last new instruction.
219 virtual MachineInstr *
220 convertToThreeAddress(MachineFunction::iterator &MFI,
221 MachineBasicBlock::iterator &MBBI, LiveVariables *LV) const {
225 /// commuteInstruction - If a target has any instructions that are commutable,
226 /// but require converting to a different instruction or making non-trivial
227 /// changes to commute them, this method can overloaded to do this. The
228 /// default implementation of this method simply swaps the first two operands
229 /// of MI and returns it.
231 /// If a target wants to make more aggressive changes, they can construct and
232 /// return a new machine instruction. If an instruction cannot commute, it
233 /// can also return null.
235 /// If NewMI is true, then a new machine instruction must be created.
237 virtual MachineInstr *commuteInstruction(MachineInstr *MI,
238 bool NewMI = false) const = 0;
240 /// findCommutedOpIndices - If specified MI is commutable, return the two
241 /// operand indices that would swap value. Return true if the instruction
242 /// is not in a form which this routine understands.
243 virtual bool findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
244 unsigned &SrcOpIdx2) const = 0;
246 /// isIdentical - Return true if two instructions are identical. This differs
247 /// from MachineInstr::isIdenticalTo() in that it does not require the
248 /// virtual destination registers to be the same. This is used by MachineLICM
249 /// and other MI passes to perform CSE.
250 virtual bool isIdentical(const MachineInstr *MI,
251 const MachineInstr *Other,
252 const MachineRegisterInfo *MRI) const = 0;
254 /// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
255 /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
256 /// implemented for a target). Upon success, this returns false and returns
257 /// with the following information in various cases:
259 /// 1. If this block ends with no branches (it just falls through to its succ)
260 /// just return false, leaving TBB/FBB null.
261 /// 2. If this block ends with only an unconditional branch, it sets TBB to be
262 /// the destination block.
263 /// 3. If this block ends with a conditional branch and it falls through to a
264 /// successor block, it sets TBB to be the branch destination block and a
265 /// list of operands that evaluate the condition. These operands can be
266 /// passed to other TargetInstrInfo methods to create new branches.
267 /// 4. If this block ends with a conditional branch followed by an
268 /// unconditional branch, it returns the 'true' destination in TBB, the
269 /// 'false' destination in FBB, and a list of operands that evaluate the
270 /// condition. These operands can be passed to other TargetInstrInfo
271 /// methods to create new branches.
273 /// Note that RemoveBranch and InsertBranch must be implemented to support
274 /// cases where this method returns success.
276 /// If AllowModify is true, then this routine is allowed to modify the basic
277 /// block (e.g. delete instructions after the unconditional branch).
279 virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
280 MachineBasicBlock *&FBB,
281 SmallVectorImpl<MachineOperand> &Cond,
282 bool AllowModify = false) const {
286 /// RemoveBranch - Remove the branching code at the end of the specific MBB.
287 /// This is only invoked in cases where AnalyzeBranch returns success. It
288 /// returns the number of instructions that were removed.
289 virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const {
290 assert(0 && "Target didn't implement TargetInstrInfo::RemoveBranch!");
294 /// InsertBranch - Insert branch code into the end of the specified
295 /// MachineBasicBlock. The operands to this method are the same as those
296 /// returned by AnalyzeBranch. This is only invoked in cases where
297 /// AnalyzeBranch returns success. It returns the number of instructions
300 /// It is also invoked by tail merging to add unconditional branches in
301 /// cases where AnalyzeBranch doesn't apply because there was no original
302 /// branch to analyze. At least this much must be implemented, else tail
303 /// merging needs to be disabled.
304 virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
305 MachineBasicBlock *FBB,
306 const SmallVectorImpl<MachineOperand> &Cond) const {
307 assert(0 && "Target didn't implement TargetInstrInfo::InsertBranch!");
311 /// copyRegToReg - Emit instructions to copy between a pair of registers. It
312 /// returns false if the target does not how to copy between the specified
314 virtual bool copyRegToReg(MachineBasicBlock &MBB,
315 MachineBasicBlock::iterator MI,
316 unsigned DestReg, unsigned SrcReg,
317 const TargetRegisterClass *DestRC,
318 const TargetRegisterClass *SrcRC) const {
319 assert(0 && "Target didn't implement TargetInstrInfo::copyRegToReg!");
323 /// storeRegToStackSlot - Store the specified register of the given register
324 /// class to the specified stack frame index. The store instruction is to be
325 /// added to the given machine basic block before the specified machine
326 /// instruction. If isKill is true, the register operand is the last use and
327 /// must be marked kill.
328 virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
329 MachineBasicBlock::iterator MI,
330 unsigned SrcReg, bool isKill, int FrameIndex,
331 const TargetRegisterClass *RC) const {
332 assert(0 && "Target didn't implement TargetInstrInfo::storeRegToStackSlot!");
335 /// loadRegFromStackSlot - Load the specified register of the given register
336 /// class from the specified stack frame index. The load instruction is to be
337 /// added to the given machine basic block before the specified machine
339 virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
340 MachineBasicBlock::iterator MI,
341 unsigned DestReg, int FrameIndex,
342 const TargetRegisterClass *RC) const {
343 assert(0 && "Target didn't implement TargetInstrInfo::loadRegFromStackSlot!");
346 /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
347 /// saved registers and returns true if it isn't possible / profitable to do
348 /// so by issuing a series of store instructions via
349 /// storeRegToStackSlot(). Returns false otherwise.
350 virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
351 MachineBasicBlock::iterator MI,
352 const std::vector<CalleeSavedInfo> &CSI) const {
356 /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
357 /// saved registers and returns true if it isn't possible / profitable to do
358 /// so by issuing a series of load instructions via loadRegToStackSlot().
359 /// Returns false otherwise.
360 virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
361 MachineBasicBlock::iterator MI,
362 const std::vector<CalleeSavedInfo> &CSI) const {
366 /// foldMemoryOperand - Attempt to fold a load or store of the specified stack
367 /// slot into the specified machine instruction for the specified operand(s).
368 /// If this is possible, a new instruction is returned with the specified
369 /// operand folded, otherwise NULL is returned. The client is responsible for
370 /// removing the old instruction and adding the new one in the instruction
372 MachineInstr* foldMemoryOperand(MachineFunction &MF,
374 const SmallVectorImpl<unsigned> &Ops,
375 int FrameIndex) const;
377 /// foldMemoryOperand - Same as the previous version except it allows folding
378 /// of any load and store from / to any address, not just from a specific
380 MachineInstr* foldMemoryOperand(MachineFunction &MF,
382 const SmallVectorImpl<unsigned> &Ops,
383 MachineInstr* LoadMI) const;
386 /// foldMemoryOperandImpl - Target-dependent implementation for
387 /// foldMemoryOperand. Target-independent code in foldMemoryOperand will
388 /// take care of adding a MachineMemOperand to the newly created instruction.
389 virtual MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
391 const SmallVectorImpl<unsigned> &Ops,
392 int FrameIndex) const {
396 /// foldMemoryOperandImpl - Target-dependent implementation for
397 /// foldMemoryOperand. Target-independent code in foldMemoryOperand will
398 /// take care of adding a MachineMemOperand to the newly created instruction.
399 virtual MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
401 const SmallVectorImpl<unsigned> &Ops,
402 MachineInstr* LoadMI) const {
407 /// canFoldMemoryOperand - Returns true for the specified load / store if
408 /// folding is possible.
410 bool canFoldMemoryOperand(const MachineInstr *MI,
411 const SmallVectorImpl<unsigned> &Ops) const {
415 /// unfoldMemoryOperand - Separate a single instruction which folded a load or
416 /// a store or a load and a store into two or more instruction. If this is
417 /// possible, returns true as well as the new instructions by reference.
418 virtual bool unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
419 unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
420 SmallVectorImpl<MachineInstr*> &NewMIs) const{
424 virtual bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
425 SmallVectorImpl<SDNode*> &NewNodes) const {
429 /// getOpcodeAfterMemoryUnfold - Returns the opcode of the would be new
430 /// instruction after load / store are unfolded from an instruction of the
431 /// specified opcode. It returns zero if the specified unfolding is not
432 /// possible. If LoadRegIndex is non-null, it is filled in with the operand
433 /// index of the operand which will hold the register holding the loaded
435 virtual unsigned getOpcodeAfterMemoryUnfold(unsigned Opc,
436 bool UnfoldLoad, bool UnfoldStore,
437 unsigned *LoadRegIndex = 0) const {
441 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler
442 /// to determine if two loads are loading from the same base address. It
443 /// should only return true if the base pointers are the same and the
444 /// only differences between the two addresses are the offset. It also returns
445 /// the offsets by reference.
446 virtual bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
447 int64_t &Offset1, int64_t &Offset2) const {
451 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
452 /// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
453 /// be scheduled togther. On some targets if two loads are loading from
454 /// addresses in the same cache line, it's better if they are scheduled
455 /// together. This function takes two integers that represent the load offsets
456 /// from the common base address. It returns true if it decides it's desirable
457 /// to schedule the two loads together. "NumLoads" is the number of loads that
458 /// have already been scheduled after Load1.
459 virtual bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
460 int64_t Offset1, int64_t Offset2,
461 unsigned NumLoads) const {
465 /// ReverseBranchCondition - Reverses the branch condition of the specified
466 /// condition list, returning false on success and true if it cannot be
469 bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
473 /// insertNoop - Insert a noop into the instruction stream at the specified
475 virtual void insertNoop(MachineBasicBlock &MBB,
476 MachineBasicBlock::iterator MI) const;
478 /// isPredicated - Returns true if the instruction is already predicated.
480 virtual bool isPredicated(const MachineInstr *MI) const {
484 /// isUnpredicatedTerminator - Returns true if the instruction is a
485 /// terminator instruction that has not been predicated.
486 virtual bool isUnpredicatedTerminator(const MachineInstr *MI) const;
488 /// PredicateInstruction - Convert the instruction into a predicated
489 /// instruction. It returns true if the operation was successful.
491 bool PredicateInstruction(MachineInstr *MI,
492 const SmallVectorImpl<MachineOperand> &Pred) const = 0;
494 /// SubsumesPredicate - Returns true if the first specified predicate
495 /// subsumes the second, e.g. GE subsumes GT.
497 bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
498 const SmallVectorImpl<MachineOperand> &Pred2) const {
502 /// DefinesPredicate - If the specified instruction defines any predicate
503 /// or condition code register(s) used for predication, returns true as well
504 /// as the definition predicate(s) by reference.
505 virtual bool DefinesPredicate(MachineInstr *MI,
506 std::vector<MachineOperand> &Pred) const {
510 /// isPredicable - Return true if the specified instruction can be predicated.
511 /// By default, this returns true for every instruction with a
512 /// PredicateOperand.
513 virtual bool isPredicable(MachineInstr *MI) const {
514 return MI->getDesc().isPredicable();
517 /// isSafeToMoveRegClassDefs - Return true if it's safe to move a machine
518 /// instruction that defines the specified register class.
519 virtual bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
523 /// GetInstSize - Returns the size of the specified Instruction.
525 virtual unsigned GetInstSizeInBytes(const MachineInstr *MI) const {
526 assert(0 && "Target didn't implement TargetInstrInfo::GetInstSize!");
530 /// GetFunctionSizeInBytes - Returns the size of the specified
533 virtual unsigned GetFunctionSizeInBytes(const MachineFunction &MF) const = 0;
535 /// Measure the specified inline asm to determine an approximation of its
537 virtual unsigned getInlineAsmLength(const char *Str,
538 const MCAsmInfo &MAI) const;
541 /// TargetInstrInfoImpl - This is the default implementation of
542 /// TargetInstrInfo, which just provides a couple of default implementations
543 /// for various methods. This separated out because it is implemented in
544 /// libcodegen, not in libtarget.
545 class TargetInstrInfoImpl : public TargetInstrInfo {
547 TargetInstrInfoImpl(const TargetInstrDesc *desc, unsigned NumOpcodes)
548 : TargetInstrInfo(desc, NumOpcodes) {}
550 virtual MachineInstr *commuteInstruction(MachineInstr *MI,
551 bool NewMI = false) const;
552 virtual bool findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
553 unsigned &SrcOpIdx2) const;
554 virtual bool PredicateInstruction(MachineInstr *MI,
555 const SmallVectorImpl<MachineOperand> &Pred) const;
556 virtual void reMaterialize(MachineBasicBlock &MBB,
557 MachineBasicBlock::iterator MI,
558 unsigned DestReg, unsigned SubReg,
559 const MachineInstr *Orig,
560 const TargetRegisterInfo *TRI) const;
561 virtual MachineInstr *duplicate(MachineInstr *Orig,
562 MachineFunction &MF) const;
563 virtual bool isIdentical(const MachineInstr *MI,
564 const MachineInstr *Other,
565 const MachineRegisterInfo *MRI) const;
567 virtual unsigned GetFunctionSizeInBytes(const MachineFunction &MF) const;
570 } // End llvm namespace