blob: b02d0d38f975dbae0ef2604537e369351eea4ce7 [file] [log] [blame]
Chris Lattnerec352402004-08-01 05:04:00 +00001//===- CodeGenInstruction.h - Instruction Class Wrapper ---------*- C++ -*-===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
Chris Lattnerec352402004-08-01 05:04:00 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
Chris Lattnerec352402004-08-01 05:04:00 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a wrapper class for the 'Instruction' TableGen class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CODEGEN_INSTRUCTION_H
15#define CODEGEN_INSTRUCTION_H
16
Chris Lattner87c59052004-08-01 07:42:39 +000017#include "llvm/CodeGen/ValueTypes.h"
Chris Lattnerec352402004-08-01 05:04:00 +000018#include <string>
19#include <vector>
20#include <utility>
21
22namespace llvm {
23 class Record;
Chris Lattner65303d62005-11-19 07:05:57 +000024 class DagInit;
Chris Lattner9414ae52010-03-27 20:09:24 +000025 class CodeGenTarget;
Chris Lattnerec352402004-08-01 05:04:00 +000026
Jeff Cohend41b30d2006-11-05 19:31:28 +000027 class CodeGenInstruction {
28 public:
Chris Lattnerec352402004-08-01 05:04:00 +000029 Record *TheDef; // The actual record defining this instruction.
Chris Lattnerec352402004-08-01 05:04:00 +000030 std::string Namespace; // The namespace the instruction is in.
31
32 /// AsmString - The format string used to emit a .s file for the
33 /// instruction.
34 std::string AsmString;
Chris Lattner6cc654b2008-01-06 01:35:39 +000035
Chris Lattnera7d479c2010-02-10 01:45:28 +000036 class ConstraintInfo {
37 enum { None, EarlyClobber, Tied } Kind;
38 unsigned OtherTiedOperand;
39 public:
40 ConstraintInfo() : Kind(None) {}
41
42 static ConstraintInfo getEarlyClobber() {
43 ConstraintInfo I;
44 I.Kind = EarlyClobber;
Chris Lattnere555c9f2010-02-10 21:22:51 +000045 I.OtherTiedOperand = 0;
Chris Lattnera7d479c2010-02-10 01:45:28 +000046 return I;
47 }
48
49 static ConstraintInfo getTied(unsigned Op) {
50 ConstraintInfo I;
51 I.Kind = Tied;
52 I.OtherTiedOperand = Op;
53 return I;
54 }
55
56 bool isNone() const { return Kind == None; }
57 bool isEarlyClobber() const { return Kind == EarlyClobber; }
58 bool isTied() const { return Kind == Tied; }
59
60 unsigned getTiedOperand() const {
61 assert(isTied());
62 return OtherTiedOperand;
63 }
64 };
65
Chris Lattnercf03da02004-08-11 02:22:39 +000066 /// OperandInfo - The information we keep track of for each operand in the
67 /// operand list for a tablegen instruction.
Chris Lattner87c59052004-08-01 07:42:39 +000068 struct OperandInfo {
Chris Lattnercf03da02004-08-11 02:22:39 +000069 /// Rec - The definition this operand is declared as.
Chris Lattner0e384b62005-08-19 16:57:28 +000070 ///
Chris Lattner87c59052004-08-01 07:42:39 +000071 Record *Rec;
Chris Lattnercf03da02004-08-11 02:22:39 +000072
Chris Lattnercf03da02004-08-11 02:22:39 +000073 /// Name - If this operand was assigned a symbolic name, this is it,
74 /// otherwise, it's empty.
Chris Lattner87c59052004-08-01 07:42:39 +000075 std::string Name;
Chris Lattnercf03da02004-08-11 02:22:39 +000076
77 /// PrinterMethodName - The method used to print operands of this type in
78 /// the asmprinter.
79 std::string PrinterMethodName;
80
81 /// MIOperandNo - Currently (this is meant to be phased out), some logical
82 /// operands correspond to multiple MachineInstr operands. In the X86
83 /// target for example, one address operand is represented as 4
84 /// MachineOperands. Because of this, the operand number in the
85 /// OperandList may not match the MachineInstr operand num. Until it
86 /// does, this contains the MI operand index of this operand.
87 unsigned MIOperandNo;
Chris Lattnercfbf96a2005-08-18 23:38:41 +000088 unsigned MINumOperands; // The number of operands.
Chris Lattnercf03da02004-08-11 02:22:39 +000089
Chris Lattnerf64f9a42006-11-15 23:23:02 +000090 /// DoNotEncode - Bools are set to true in this vector for each operand in
91 /// the DisableEncoding list. These should not be emitted by the code
92 /// emitter.
93 std::vector<bool> DoNotEncode;
94
Nate Begeman8ef9d162005-11-30 23:58:18 +000095 /// MIOperandInfo - Default MI operand type. Note an operand may be made
96 /// up of multiple MI operands.
Chris Lattner65303d62005-11-19 07:05:57 +000097 DagInit *MIOperandInfo;
Chris Lattnera0cca4a2006-11-06 23:49:51 +000098
Chris Lattner0bb75002006-11-15 02:38:17 +000099 /// Constraint info for this operand. This operand can have pieces, so we
100 /// track constraint info for each.
Chris Lattnera7d479c2010-02-10 01:45:28 +0000101 std::vector<ConstraintInfo> Constraints;
Chris Lattner65303d62005-11-19 07:05:57 +0000102
Nate Begeman86193d12005-12-01 00:12:04 +0000103 OperandInfo(Record *R, const std::string &N, const std::string &PMN,
104 unsigned MION, unsigned MINO, DagInit *MIOI)
105 : Rec(R), Name(N), PrinterMethodName(PMN), MIOperandNo(MION),
Chris Lattner65303d62005-11-19 07:05:57 +0000106 MINumOperands(MINO), MIOperandInfo(MIOI) {}
Chris Lattner87c59052004-08-01 07:42:39 +0000107 };
Misha Brukman3da94ae2005-04-22 00:00:37 +0000108
Chris Lattnercedef1c2010-03-18 20:50:52 +0000109 /// NumDefs - Number of def operands declared, this is the number of
110 /// elements in the instruction's (outs) list.
Evan Cheng64d80e32007-07-19 01:14:50 +0000111 ///
112 unsigned NumDefs;
113
Chris Lattnerec352402004-08-01 05:04:00 +0000114 /// OperandList - The list of declared operands, along with their declared
115 /// type (which is a record).
Chris Lattner87c59052004-08-01 07:42:39 +0000116 std::vector<OperandInfo> OperandList;
Chris Lattnerec352402004-08-01 05:04:00 +0000117
Chris Lattnerf506b6b2010-03-18 21:42:03 +0000118 /// ImplicitDefs/ImplicitUses - These are lists of registers that are
119 /// implicitly defined and used by the instruction.
120 std::vector<Record*> ImplicitDefs, ImplicitUses;
121
Chris Lattnerec352402004-08-01 05:04:00 +0000122 // Various boolean values we track for the instruction.
123 bool isReturn;
124 bool isBranch;
Owen Anderson20ab2902007-11-12 07:39:39 +0000125 bool isIndirectBranch;
Bill Wendling73739d02010-08-08 01:49:35 +0000126 bool isCompare;
Chris Lattnerec352402004-08-01 05:04:00 +0000127 bool isBarrier;
128 bool isCall;
Dan Gohman15511cf2008-12-03 18:15:48 +0000129 bool canFoldAsLoad;
Chris Lattnerdcc8b4f2008-01-08 18:05:21 +0000130 bool mayLoad, mayStore;
Evan Cheng5127ce02007-05-16 20:45:24 +0000131 bool isPredicable;
Chris Lattneraad75aa2005-01-02 02:29:04 +0000132 bool isConvertibleToThreeAddress;
133 bool isCommutable;
Chris Lattnerec352402004-08-01 05:04:00 +0000134 bool isTerminator;
Dan Gohmand45eddd2007-06-26 00:48:07 +0000135 bool isReMaterializable;
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000136 bool hasDelaySlot;
Dan Gohman533297b2009-10-29 18:10:34 +0000137 bool usesCustomInserter;
Chris Lattner8f707e12008-01-07 05:19:29 +0000138 bool isVariadic;
Evan Cheng1c3d19e2005-12-04 08:18:16 +0000139 bool hasCtrlDep;
Evan Chengeaa91b02007-06-19 01:26:51 +0000140 bool isNotDuplicable;
Evan Cheng88cc0922007-07-10 18:05:01 +0000141 bool hasOptionalDef;
Bill Wendling8370d382008-05-28 22:54:52 +0000142 bool hasSideEffects;
Bill Wendling8370d382008-05-28 22:54:52 +0000143 bool neverHasSideEffects;
144 bool isAsCheapAsAMove;
Evan Cheng799d6972009-10-01 08:21:18 +0000145 bool hasExtraSrcRegAllocReq;
146 bool hasExtraDefRegAllocReq;
Chris Lattner0bb75002006-11-15 02:38:17 +0000147
148 /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
149 /// where $foo is a whole operand and $foo.bar refers to a suboperand.
150 /// This throws an exception if the name is invalid. If AllowWholeOp is
151 /// true, references to operands with suboperands are allowed, otherwise
152 /// not.
153 std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
154 bool AllowWholeOp = true);
155
156 /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
157 /// flat machineinstr operand #.
158 unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
159 return OperandList[Op.first].MIOperandNo + Op.second;
160 }
161
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000162 /// getSubOperandNumber - Unflatten a operand number into an
163 /// operand/suboperand pair.
164 std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
165 for (unsigned i = 0; ; ++i) {
166 assert(i < OperandList.size() && "Invalid flat operand #");
167 if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
168 return std::make_pair(i, Op-OperandList[i].MIOperandNo);
169 }
170 }
171
172
173 /// isFlatOperandNotEmitted - Return true if the specified flat operand #
174 /// should not be emitted with the code emitter.
175 bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
176 std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
177 if (OperandList[Op.first].DoNotEncode.size() > Op.second)
178 return OperandList[Op.first].DoNotEncode[Op.second];
179 return false;
180 }
Chris Lattnerec352402004-08-01 05:04:00 +0000181
Chris Lattner175580c2004-08-14 22:50:53 +0000182 CodeGenInstruction(Record *R, const std::string &AsmStr);
Chris Lattner87c59052004-08-01 07:42:39 +0000183
184 /// getOperandNamed - Return the index of the operand with the specified
185 /// non-empty name. If the instruction does not have an operand with the
186 /// specified name, throw an exception.
187 unsigned getOperandNamed(const std::string &Name) const;
Chris Lattner9414ae52010-03-27 20:09:24 +0000188
189 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
190 /// implicit def and it has a known VT, return the VT, otherwise return
191 /// MVT::Other.
192 MVT::SimpleValueType
193 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
Chris Lattnerec352402004-08-01 05:04:00 +0000194 };
195}
196
197#endif