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 instructions to the code generator.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TARGET_TARGETINSTRINFO_H
15 #define LLVM_TARGET_TARGETINSTRINFO_H
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/Support/DataTypes.h"
27 class TargetRegisterClass;
30 template<class T> class SmallVectorImpl;
32 //---------------------------------------------------------------------------
33 // Data types used to define information about a single machine instruction
34 //---------------------------------------------------------------------------
36 typedef short MachineOpCode;
37 typedef unsigned InstrSchedClass;
39 //---------------------------------------------------------------------------
40 // struct TargetInstrDescriptor:
41 // Predefined information about each machine instruction.
42 // Designed to initialized statically.
45 const unsigned M_BRANCH_FLAG = 1 << 0;
46 const unsigned M_CALL_FLAG = 1 << 1;
47 const unsigned M_RET_FLAG = 1 << 2;
48 const unsigned M_BARRIER_FLAG = 1 << 3;
49 const unsigned M_DELAY_SLOT_FLAG = 1 << 4;
50 const unsigned M_LOAD_FLAG = 1 << 5;
51 const unsigned M_STORE_FLAG = 1 << 6;
52 const unsigned M_INDIRECT_FLAG = 1 << 7;
53 const unsigned M_IMPLICIT_DEF_FLAG = 1 << 8;
55 // M_CONVERTIBLE_TO_3_ADDR - This is a 2-address instruction which can be
56 // changed into a 3-address instruction if the first two operands cannot be
57 // assigned to the same register. The target must implement the
58 // TargetInstrInfo::convertToThreeAddress method for this instruction.
59 const unsigned M_CONVERTIBLE_TO_3_ADDR = 1 << 9;
61 // This M_COMMUTABLE - is a 2- or 3-address instruction (of the form X = op Y,
62 // Z), which produces the same result if Y and Z are exchanged.
63 const unsigned M_COMMUTABLE = 1 << 10;
65 // M_TERMINATOR_FLAG - Is this instruction part of the terminator for a basic
66 // block? Typically this is things like return and branch instructions.
67 // Various passes use this to insert code into the bottom of a basic block, but
68 // before control flow occurs.
69 const unsigned M_TERMINATOR_FLAG = 1 << 11;
71 // M_USES_CUSTOM_DAG_SCHED_INSERTION - Set if this instruction requires custom
72 // insertion support when the DAG scheduler is inserting it into a machine basic
74 const unsigned M_USES_CUSTOM_DAG_SCHED_INSERTION = 1 << 12;
76 // M_VARIABLE_OPS - Set if this instruction can have a variable number of extra
77 // operands in addition to the minimum number operands specified.
78 const unsigned M_VARIABLE_OPS = 1 << 13;
80 // M_PREDICABLE - Set if this instruction has a predicate operand that
81 // controls execution. It may be set to 'always'.
82 const unsigned M_PREDICABLE = 1 << 14;
84 // M_REMATERIALIZIBLE - Set if this instruction can be trivally re-materialized
85 // at any time, e.g. constant generation, load from constant pool.
86 const unsigned M_REMATERIALIZIBLE = 1 << 15;
88 // M_NOT_DUPLICABLE - Set if this instruction cannot be safely duplicated.
89 // (e.g. instructions with unique labels attached).
90 const unsigned M_NOT_DUPLICABLE = 1 << 16;
92 // M_HAS_OPTIONAL_DEF - Set if this instruction has an optional definition, e.g.
93 // ARM instructions which can set condition code if 's' bit is set.
94 const unsigned M_HAS_OPTIONAL_DEF = 1 << 17;
96 // M_NEVER_HAS_SIDE_EFFECTS - Set if this instruction has no side effects that
97 // are not captured by any operands of the instruction or other flags, and when
98 // *all* instances of the instruction of that opcode have no side effects.
100 // Note: This and M_MAY_HAVE_SIDE_EFFECTS are mutually exclusive. You can't set
101 // both! If neither flag is set, then the instruction *always* has side effects.
102 const unsigned M_NEVER_HAS_SIDE_EFFECTS = 1 << 18;
104 // M_MAY_HAVE_SIDE_EFFECTS - Set if some instances of this instruction can have
105 // side effects. The virtual method "isReallySideEffectFree" is called to
106 // determine this. Load instructions are an example of where this is useful. In
107 // general, loads always have side effects. However, loads from constant pools
108 // don't. We let the specific back end make this determination.
110 // Note: This and M_NEVER_HAS_SIDE_EFFECTS are mutually exclusive. You can't set
111 // both! If neither flag is set, then the instruction *always* has side effects.
112 const unsigned M_MAY_HAVE_SIDE_EFFECTS = 1 << 19;
114 // Machine operand flags
115 // M_LOOK_UP_PTR_REG_CLASS - Set if this operand is a pointer value and it
116 // requires a callback to look up its register class.
117 const unsigned M_LOOK_UP_PTR_REG_CLASS = 1 << 0;
119 /// M_PREDICATE_OPERAND - Set if this is one of the operands that made up of the
120 /// predicate operand that controls an M_PREDICATED instruction.
121 const unsigned M_PREDICATE_OPERAND = 1 << 1;
123 /// M_OPTIONAL_DEF_OPERAND - Set if this operand is a optional def.
125 const unsigned M_OPTIONAL_DEF_OPERAND = 1 << 2;
128 // Operand constraints: only "tied_to" for now.
129 enum OperandConstraint {
130 TIED_TO = 0 // Must be allocated the same register as.
134 /// TargetOperandInfo - This holds information about one operand of a machine
135 /// instruction, indicating the register class for register operands, etc.
137 class TargetOperandInfo {
139 /// RegClass - This specifies the register class enumeration of the operand
140 /// if the operand is a register. If not, this contains 0.
141 unsigned short RegClass;
142 unsigned short Flags;
143 /// Lower 16 bits are used to specify which constraints are set. The higher 16
144 /// bits are used to specify the value of constraints (4 bits each).
145 unsigned int Constraints;
146 /// Currently no other information.
150 class TargetInstrDescriptor {
152 MachineOpCode Opcode; // The opcode.
153 unsigned short numOperands; // Num of args (may be more if variable_ops).
154 unsigned short numDefs; // Num of args that are definitions.
155 const char * Name; // Assembly language mnemonic for the opcode.
156 InstrSchedClass schedClass; // enum identifying instr sched class
157 unsigned Flags; // flags identifying machine instr class
158 unsigned TSFlags; // Target Specific Flag values
159 const unsigned *ImplicitUses; // Registers implicitly read by this instr
160 const unsigned *ImplicitDefs; // Registers implicitly defined by this instr
161 const TargetOperandInfo *OpInfo; // 'numOperands' entries about operands.
163 /// getOperandConstraint - Returns the value of the specific constraint if
164 /// it is set. Returns -1 if it is not set.
165 int getOperandConstraint(unsigned OpNum,
166 TOI::OperandConstraint Constraint) const {
167 assert((OpNum < numOperands || (Flags & M_VARIABLE_OPS)) &&
168 "Invalid operand # of TargetInstrInfo");
169 if (OpNum < numOperands &&
170 (OpInfo[OpNum].Constraints & (1 << Constraint))) {
171 unsigned Pos = 16 + Constraint * 4;
172 return (int)(OpInfo[OpNum].Constraints >> Pos) & 0xf;
177 /// findTiedToSrcOperand - Returns the operand that is tied to the specified
178 /// dest operand. Returns -1 if there isn't one.
179 int findTiedToSrcOperand(unsigned OpNum) const;
183 //---------------------------------------------------------------------------
185 /// TargetInstrInfo - Interface to description of machine instructions
187 class TargetInstrInfo {
188 const TargetInstrDescriptor* desc; // raw array to allow static init'n
189 unsigned NumOpcodes; // number of entries in the desc array
190 unsigned numRealOpCodes; // number of non-dummy op codes
192 TargetInstrInfo(const TargetInstrInfo &); // DO NOT IMPLEMENT
193 void operator=(const TargetInstrInfo &); // DO NOT IMPLEMENT
195 TargetInstrInfo(const TargetInstrDescriptor *desc, unsigned NumOpcodes);
196 virtual ~TargetInstrInfo();
198 // Invariant opcodes: All instruction sets have these as their low opcodes.
207 unsigned getNumOpcodes() const { return NumOpcodes; }
209 /// get - Return the machine instruction descriptor that corresponds to the
210 /// specified instruction opcode.
212 const TargetInstrDescriptor& get(MachineOpCode Opcode) const {
213 assert((unsigned)Opcode < NumOpcodes);
217 const char *getName(MachineOpCode Opcode) const {
218 return get(Opcode).Name;
221 int getNumOperands(MachineOpCode Opcode) const {
222 return get(Opcode).numOperands;
225 int getNumDefs(MachineOpCode Opcode) const {
226 return get(Opcode).numDefs;
229 InstrSchedClass getSchedClass(MachineOpCode Opcode) const {
230 return get(Opcode).schedClass;
233 const unsigned *getImplicitUses(MachineOpCode Opcode) const {
234 return get(Opcode).ImplicitUses;
237 const unsigned *getImplicitDefs(MachineOpCode Opcode) const {
238 return get(Opcode).ImplicitDefs;
243 // Query instruction class flags according to the machine-independent
244 // flags listed above.
246 bool isReturn(MachineOpCode Opcode) const {
247 return get(Opcode).Flags & M_RET_FLAG;
250 bool isCommutableInstr(MachineOpCode Opcode) const {
251 return get(Opcode).Flags & M_COMMUTABLE;
253 bool isTerminatorInstr(MachineOpCode Opcode) const {
254 return get(Opcode).Flags & M_TERMINATOR_FLAG;
257 bool isBranch(MachineOpCode Opcode) const {
258 return get(Opcode).Flags & M_BRANCH_FLAG;
261 bool isIndirectBranch(MachineOpCode Opcode) const {
262 return get(Opcode).Flags & M_INDIRECT_FLAG;
265 /// isBarrier - Returns true if the specified instruction stops control flow
266 /// from executing the instruction immediately following it. Examples include
267 /// unconditional branches and return instructions.
268 bool isBarrier(MachineOpCode Opcode) const {
269 return get(Opcode).Flags & M_BARRIER_FLAG;
272 bool isCall(MachineOpCode Opcode) const {
273 return get(Opcode).Flags & M_CALL_FLAG;
275 bool isLoad(MachineOpCode Opcode) const {
276 return get(Opcode).Flags & M_LOAD_FLAG;
278 bool isStore(MachineOpCode Opcode) const {
279 return get(Opcode).Flags & M_STORE_FLAG;
282 /// hasDelaySlot - Returns true if the specified instruction has a delay slot
283 /// which must be filled by the code generator.
284 bool hasDelaySlot(MachineOpCode Opcode) const {
285 return get(Opcode).Flags & M_DELAY_SLOT_FLAG;
288 /// usesCustomDAGSchedInsertionHook - Return true if this instruction requires
289 /// custom insertion support when the DAG scheduler is inserting it into a
290 /// machine basic block.
291 bool usesCustomDAGSchedInsertionHook(MachineOpCode Opcode) const {
292 return get(Opcode).Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION;
295 bool hasVariableOperands(MachineOpCode Opcode) const {
296 return get(Opcode).Flags & M_VARIABLE_OPS;
299 bool isPredicable(MachineOpCode Opcode) const {
300 return get(Opcode).Flags & M_PREDICABLE;
303 bool isNotDuplicable(MachineOpCode Opcode) const {
304 return get(Opcode).Flags & M_NOT_DUPLICABLE;
307 bool hasOptionalDef(MachineOpCode Opcode) const {
308 return get(Opcode).Flags & M_HAS_OPTIONAL_DEF;
311 /// isTriviallyReMaterializable - Return true if the instruction is trivially
312 /// rematerializable, meaning it has no side effects and requires no operands
313 /// that aren't always available.
314 bool isTriviallyReMaterializable(MachineInstr *MI) const {
315 return (MI->getInstrDescriptor()->Flags & M_REMATERIALIZIBLE) &&
316 isReallyTriviallyReMaterializable(MI);
319 /// hasUnmodelledSideEffects - Returns true if the instruction has side
320 /// effects that are not captured by any operands of the instruction or other
322 bool hasUnmodelledSideEffects(MachineInstr *MI) const {
323 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
324 if (!(TID->Flags & M_NEVER_HAS_SIDE_EFFECTS ||
325 TID->Flags & M_MAY_HAVE_SIDE_EFFECTS)) return true;
326 if (TID->Flags & M_NEVER_HAS_SIDE_EFFECTS) return false;
327 return !isReallySideEffectFree(MI); // May have side effects
330 /// isReallyTriviallyReMaterializable - For instructions with opcodes for
331 /// which the M_REMATERIALIZABLE flag is set, this function tests whether the
332 /// instruction itself is actually trivially rematerializable, considering
333 /// its operands. This is used for targets that have instructions that are
334 /// only trivially rematerializable for specific uses. This predicate must
335 /// return false if the instruction has any side effects other than
336 /// producing a value, or if it requres any address registers that are not
337 /// always available.
338 virtual bool isReallyTriviallyReMaterializable(MachineInstr *MI) const {
342 /// isReallySideEffectFree - If the M_MAY_HAVE_SIDE_EFFECTS flag is set, this
343 /// method is called to determine if the specific instance of this
344 /// instruction has side effects. This is useful in cases of instructions,
345 /// like loads, which generally always have side effects. A load from a
346 /// constant pool doesn't have side effects, though. So we need to
347 /// differentiate it from the general case.
348 virtual bool isReallySideEffectFree(MachineInstr *MI) const {
352 /// getOperandConstraint - Returns the value of the specific constraint if
353 /// it is set. Returns -1 if it is not set.
354 int getOperandConstraint(MachineOpCode Opcode, unsigned OpNum,
355 TOI::OperandConstraint Constraint) const {
356 return get(Opcode).getOperandConstraint(OpNum, Constraint);
359 /// Return true if the instruction is a register to register move
360 /// and leave the source and dest operands in the passed parameters.
361 virtual bool isMoveInstr(const MachineInstr& MI,
363 unsigned& destReg) const {
367 /// isLoadFromStackSlot - If the specified machine instruction is a direct
368 /// load from a stack slot, return the virtual or physical register number of
369 /// the destination along with the FrameIndex of the loaded stack slot. If
370 /// not, return 0. This predicate must return 0 if the instruction has
371 /// any side effects other than loading from the stack slot.
372 virtual unsigned isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const{
376 /// isStoreToStackSlot - If the specified machine instruction is a direct
377 /// store to a stack slot, return the virtual or physical register number of
378 /// the source reg along with the FrameIndex of the loaded stack slot. If
379 /// not, return 0. This predicate must return 0 if the instruction has
380 /// any side effects other than storing to the stack slot.
381 virtual unsigned isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const {
385 /// convertToThreeAddress - This method must be implemented by targets that
386 /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
387 /// may be able to convert a two-address instruction into one or more true
388 /// three-address instructions on demand. This allows the X86 target (for
389 /// example) to convert ADD and SHL instructions into LEA instructions if they
390 /// would require register copies due to two-addressness.
392 /// This method returns a null pointer if the transformation cannot be
393 /// performed, otherwise it returns the last new instruction.
395 virtual MachineInstr *
396 convertToThreeAddress(MachineFunction::iterator &MFI,
397 MachineBasicBlock::iterator &MBBI, LiveVariables &LV) const {
401 /// commuteInstruction - If a target has any instructions that are commutable,
402 /// but require converting to a different instruction or making non-trivial
403 /// changes to commute them, this method can overloaded to do this. The
404 /// default implementation of this method simply swaps the first two operands
405 /// of MI and returns it.
407 /// If a target wants to make more aggressive changes, they can construct and
408 /// return a new machine instruction. If an instruction cannot commute, it
409 /// can also return null.
411 virtual MachineInstr *commuteInstruction(MachineInstr *MI) const = 0;
413 /// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
414 /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
415 /// implemented for a target). Upon success, this returns false and returns
416 /// with the following information in various cases:
418 /// 1. If this block ends with no branches (it just falls through to its succ)
419 /// just return false, leaving TBB/FBB null.
420 /// 2. If this block ends with only an unconditional branch, it sets TBB to be
421 /// the destination block.
422 /// 3. If this block ends with an conditional branch and it falls through to
423 /// an successor block, it sets TBB to be the branch destination block and a
424 /// list of operands that evaluate the condition. These
425 /// operands can be passed to other TargetInstrInfo methods to create new
427 /// 4. If this block ends with an conditional branch and an unconditional
428 /// block, it returns the 'true' destination in TBB, the 'false' destination
429 /// in FBB, and a list of operands that evaluate the condition. These
430 /// operands can be passed to other TargetInstrInfo methods to create new
433 /// Note that RemoveBranch and InsertBranch must be implemented to support
434 /// cases where this method returns success.
436 virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
437 MachineBasicBlock *&FBB,
438 std::vector<MachineOperand> &Cond) const {
442 /// RemoveBranch - Remove the branching code at the end of the specific MBB.
443 /// this is only invoked in cases where AnalyzeBranch returns success. It
444 /// returns the number of instructions that were removed.
445 virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const {
446 assert(0 && "Target didn't implement TargetInstrInfo::RemoveBranch!");
450 /// InsertBranch - Insert a branch into the end of the specified
451 /// MachineBasicBlock. This operands to this method are the same as those
452 /// returned by AnalyzeBranch. This is invoked in cases where AnalyzeBranch
453 /// returns success and when an unconditional branch (TBB is non-null, FBB is
454 /// null, Cond is empty) needs to be inserted. It returns the number of
455 /// instructions inserted.
456 virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
457 MachineBasicBlock *FBB,
458 const std::vector<MachineOperand> &Cond) const {
459 assert(0 && "Target didn't implement TargetInstrInfo::InsertBranch!");
463 /// copyRegToReg - Add a copy between a pair of registers
464 virtual void copyRegToReg(MachineBasicBlock &MBB,
465 MachineBasicBlock::iterator MI,
466 unsigned DestReg, unsigned SrcReg,
467 const TargetRegisterClass *DestRC,
468 const TargetRegisterClass *SrcRC) const {
469 assert(0 && "Target didn't implement TargetInstrInfo::copyRegToReg!");
472 virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
473 MachineBasicBlock::iterator MI,
474 unsigned SrcReg, bool isKill, int FrameIndex,
475 const TargetRegisterClass *RC) const {
476 assert(0 && "Target didn't implement TargetInstrInfo::storeRegToStackSlot!");
479 virtual void storeRegToAddr(MachineFunction &MF, unsigned SrcReg, bool isKill,
480 SmallVectorImpl<MachineOperand> &Addr,
481 const TargetRegisterClass *RC,
482 SmallVectorImpl<MachineInstr*> &NewMIs) const {
483 assert(0 && "Target didn't implement TargetInstrInfo::storeRegToAddr!");
486 virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
487 MachineBasicBlock::iterator MI,
488 unsigned DestReg, int FrameIndex,
489 const TargetRegisterClass *RC) const {
490 assert(0 && "Target didn't implement TargetInstrInfo::loadRegFromStackSlot!");
493 virtual void loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
494 SmallVectorImpl<MachineOperand> &Addr,
495 const TargetRegisterClass *RC,
496 SmallVectorImpl<MachineInstr*> &NewMIs) const {
497 assert(0 && "Target didn't implement TargetInstrInfo::loadRegFromAddr!");
500 /// BlockHasNoFallThrough - Return true if the specified block does not
501 /// fall-through into its successor block. This is primarily used when a
502 /// branch is unanalyzable. It is useful for things like unconditional
503 /// indirect branches (jump tables).
504 virtual bool BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
508 /// ReverseBranchCondition - Reverses the branch condition of the specified
509 /// condition list, returning false on success and true if it cannot be
511 virtual bool ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
515 /// insertNoop - Insert a noop into the instruction stream at the specified
517 virtual void insertNoop(MachineBasicBlock &MBB,
518 MachineBasicBlock::iterator MI) const {
519 assert(0 && "Target didn't implement insertNoop!");
523 /// isPredicated - Returns true if the instruction is already predicated.
525 virtual bool isPredicated(const MachineInstr *MI) const {
529 /// isUnpredicatedTerminator - Returns true if the instruction is a
530 /// terminator instruction that has not been predicated.
531 virtual bool isUnpredicatedTerminator(const MachineInstr *MI) const;
533 /// PredicateInstruction - Convert the instruction into a predicated
534 /// instruction. It returns true if the operation was successful.
536 bool PredicateInstruction(MachineInstr *MI,
537 const std::vector<MachineOperand> &Pred) const = 0;
539 /// SubsumesPredicate - Returns true if the first specified predicate
540 /// subsumes the second, e.g. GE subsumes GT.
542 bool SubsumesPredicate(const std::vector<MachineOperand> &Pred1,
543 const std::vector<MachineOperand> &Pred2) const {
547 /// DefinesPredicate - If the specified instruction defines any predicate
548 /// or condition code register(s) used for predication, returns true as well
549 /// as the definition predicate(s) by reference.
550 virtual bool DefinesPredicate(MachineInstr *MI,
551 std::vector<MachineOperand> &Pred) const {
555 /// getPointerRegClass - Returns a TargetRegisterClass used for pointer
557 virtual const TargetRegisterClass *getPointerRegClass() const {
558 assert(0 && "Target didn't implement getPointerRegClass!");
560 return 0; // Must return a value in order to compile with VS 2005
564 /// TargetInstrInfoImpl - This is the default implementation of
565 /// TargetInstrInfo, which just provides a couple of default implementations
566 /// for various methods. This separated out because it is implemented in
567 /// libcodegen, not in libtarget.
568 class TargetInstrInfoImpl : public TargetInstrInfo {
570 TargetInstrInfoImpl(const TargetInstrDescriptor *desc, unsigned NumOpcodes)
571 : TargetInstrInfo(desc, NumOpcodes) {}
573 virtual MachineInstr *commuteInstruction(MachineInstr *MI) const;
574 virtual bool PredicateInstruction(MachineInstr *MI,
575 const std::vector<MachineOperand> &Pred) const;
579 } // End llvm namespace