Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 1 | //===- CodeGenInstruction.h - Instruction Class Wrapper ---------*- C++ -*-===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 3060910 | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 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 Lattner | 87c5905 | 2004-08-01 07:42:39 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/ValueTypes.h" |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <vector> |
| 20 | #include <utility> |
| 21 | |
| 22 | namespace llvm { |
| 23 | class Record; |
Chris Lattner | 65303d6 | 2005-11-19 07:05:57 +0000 | [diff] [blame] | 24 | class DagInit; |
Chris Lattner | 9414ae5 | 2010-03-27 20:09:24 +0000 | [diff] [blame] | 25 | class CodeGenTarget; |
Chris Lattner | 4d43d0f | 2010-11-01 01:07:14 +0000 | [diff] [blame] | 26 | class StringRef; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 27 | |
| 28 | class CGIOperandList { |
Jeff Cohen | d41b30d | 2006-11-05 19:31:28 +0000 | [diff] [blame] | 29 | public: |
Chris Lattner | a7d479c | 2010-02-10 01:45:28 +0000 | [diff] [blame] | 30 | class ConstraintInfo { |
| 31 | enum { None, EarlyClobber, Tied } Kind; |
| 32 | unsigned OtherTiedOperand; |
| 33 | public: |
| 34 | ConstraintInfo() : Kind(None) {} |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 35 | |
Chris Lattner | a7d479c | 2010-02-10 01:45:28 +0000 | [diff] [blame] | 36 | static ConstraintInfo getEarlyClobber() { |
| 37 | ConstraintInfo I; |
| 38 | I.Kind = EarlyClobber; |
Chris Lattner | e555c9f | 2010-02-10 21:22:51 +0000 | [diff] [blame] | 39 | I.OtherTiedOperand = 0; |
Chris Lattner | a7d479c | 2010-02-10 01:45:28 +0000 | [diff] [blame] | 40 | return I; |
| 41 | } |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 42 | |
Chris Lattner | a7d479c | 2010-02-10 01:45:28 +0000 | [diff] [blame] | 43 | static ConstraintInfo getTied(unsigned Op) { |
| 44 | ConstraintInfo I; |
| 45 | I.Kind = Tied; |
| 46 | I.OtherTiedOperand = Op; |
| 47 | return I; |
| 48 | } |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 49 | |
Chris Lattner | a7d479c | 2010-02-10 01:45:28 +0000 | [diff] [blame] | 50 | bool isNone() const { return Kind == None; } |
| 51 | bool isEarlyClobber() const { return Kind == EarlyClobber; } |
| 52 | bool isTied() const { return Kind == Tied; } |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 53 | |
Chris Lattner | a7d479c | 2010-02-10 01:45:28 +0000 | [diff] [blame] | 54 | unsigned getTiedOperand() const { |
| 55 | assert(isTied()); |
| 56 | return OtherTiedOperand; |
| 57 | } |
| 58 | }; |
Jim Grosbach | 9ed2cee | 2010-10-08 18:09:59 +0000 | [diff] [blame] | 59 | |
Chris Lattner | cf03da0 | 2004-08-11 02:22:39 +0000 | [diff] [blame] | 60 | /// OperandInfo - The information we keep track of for each operand in the |
| 61 | /// operand list for a tablegen instruction. |
Chris Lattner | 87c5905 | 2004-08-01 07:42:39 +0000 | [diff] [blame] | 62 | struct OperandInfo { |
Chris Lattner | cf03da0 | 2004-08-11 02:22:39 +0000 | [diff] [blame] | 63 | /// Rec - The definition this operand is declared as. |
Chris Lattner | 0e384b6 | 2005-08-19 16:57:28 +0000 | [diff] [blame] | 64 | /// |
Chris Lattner | 87c5905 | 2004-08-01 07:42:39 +0000 | [diff] [blame] | 65 | Record *Rec; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 66 | |
Chris Lattner | cf03da0 | 2004-08-11 02:22:39 +0000 | [diff] [blame] | 67 | /// Name - If this operand was assigned a symbolic name, this is it, |
| 68 | /// otherwise, it's empty. |
Chris Lattner | 87c5905 | 2004-08-01 07:42:39 +0000 | [diff] [blame] | 69 | std::string Name; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 70 | |
Chris Lattner | cf03da0 | 2004-08-11 02:22:39 +0000 | [diff] [blame] | 71 | /// PrinterMethodName - The method used to print operands of this type in |
| 72 | /// the asmprinter. |
| 73 | std::string PrinterMethodName; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 74 | |
Jim Grosbach | 5013f74 | 2010-10-12 22:21:57 +0000 | [diff] [blame] | 75 | /// EncoderMethodName - The method used to get the machine operand value |
| 76 | /// for binary encoding. "getMachineOpValue" by default. |
| 77 | std::string EncoderMethodName; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 78 | |
Chris Lattner | cf03da0 | 2004-08-11 02:22:39 +0000 | [diff] [blame] | 79 | /// MIOperandNo - Currently (this is meant to be phased out), some logical |
| 80 | /// operands correspond to multiple MachineInstr operands. In the X86 |
| 81 | /// target for example, one address operand is represented as 4 |
| 82 | /// MachineOperands. Because of this, the operand number in the |
| 83 | /// OperandList may not match the MachineInstr operand num. Until it |
| 84 | /// does, this contains the MI operand index of this operand. |
| 85 | unsigned MIOperandNo; |
Chris Lattner | cfbf96a | 2005-08-18 23:38:41 +0000 | [diff] [blame] | 86 | unsigned MINumOperands; // The number of operands. |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 87 | |
Chris Lattner | f64f9a4 | 2006-11-15 23:23:02 +0000 | [diff] [blame] | 88 | /// DoNotEncode - Bools are set to true in this vector for each operand in |
| 89 | /// the DisableEncoding list. These should not be emitted by the code |
| 90 | /// emitter. |
| 91 | std::vector<bool> DoNotEncode; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 92 | |
Nate Begeman | 8ef9d16 | 2005-11-30 23:58:18 +0000 | [diff] [blame] | 93 | /// MIOperandInfo - Default MI operand type. Note an operand may be made |
| 94 | /// up of multiple MI operands. |
Chris Lattner | 65303d6 | 2005-11-19 07:05:57 +0000 | [diff] [blame] | 95 | DagInit *MIOperandInfo; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 0bb7500 | 2006-11-15 02:38:17 +0000 | [diff] [blame] | 97 | /// Constraint info for this operand. This operand can have pieces, so we |
| 98 | /// track constraint info for each. |
Chris Lattner | a7d479c | 2010-02-10 01:45:28 +0000 | [diff] [blame] | 99 | std::vector<ConstraintInfo> Constraints; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 100 | |
Jim Grosbach | 9ed2cee | 2010-10-08 18:09:59 +0000 | [diff] [blame] | 101 | OperandInfo(Record *R, const std::string &N, const std::string &PMN, |
Jim Grosbach | 5013f74 | 2010-10-12 22:21:57 +0000 | [diff] [blame] | 102 | const std::string &EMN, unsigned MION, unsigned MINO, |
| 103 | DagInit *MIOI) |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 104 | : Rec(R), Name(N), PrinterMethodName(PMN), EncoderMethodName(EMN), |
| 105 | MIOperandNo(MION), MINumOperands(MINO), MIOperandInfo(MIOI) {} |
Chris Lattner | 87c5905 | 2004-08-01 07:42:39 +0000 | [diff] [blame] | 106 | }; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 107 | |
| 108 | CGIOperandList(Record *D); |
| 109 | |
| 110 | Record *TheDef; // The actual record containing this OperandList. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 111 | |
Chris Lattner | cedef1c | 2010-03-18 20:50:52 +0000 | [diff] [blame] | 112 | /// NumDefs - Number of def operands declared, this is the number of |
| 113 | /// elements in the instruction's (outs) list. |
Evan Cheng | 64d80e3 | 2007-07-19 01:14:50 +0000 | [diff] [blame] | 114 | /// |
| 115 | unsigned NumDefs; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 116 | |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 117 | /// OperandList - The list of declared operands, along with their declared |
| 118 | /// type (which is a record). |
Chris Lattner | 87c5905 | 2004-08-01 07:42:39 +0000 | [diff] [blame] | 119 | std::vector<OperandInfo> OperandList; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 120 | |
| 121 | // Information gleaned from the operand list. |
| 122 | bool isPredicable; |
| 123 | bool hasOptionalDef; |
| 124 | bool isVariadic; |
| 125 | |
| 126 | // Provide transparent accessors to the operand list. |
| 127 | unsigned size() const { return OperandList.size(); } |
| 128 | const OperandInfo &operator[](unsigned i) const { return OperandList[i]; } |
| 129 | OperandInfo &operator[](unsigned i) { return OperandList[i]; } |
| 130 | OperandInfo &back() { return OperandList.back(); } |
| 131 | const OperandInfo &back() const { return OperandList.back(); } |
| 132 | |
| 133 | |
| 134 | /// getOperandNamed - Return the index of the operand with the specified |
| 135 | /// non-empty name. If the instruction does not have an operand with the |
| 136 | /// specified name, throw an exception. |
| 137 | unsigned getOperandNamed(StringRef Name) const; |
| 138 | |
| 139 | /// hasOperandNamed - Query whether the instruction has an operand of the |
| 140 | /// given name. If so, return true and set OpIdx to the index of the |
| 141 | /// operand. Otherwise, return false. |
| 142 | bool hasOperandNamed(StringRef Name, unsigned &OpIdx) const; |
| 143 | |
| 144 | /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar", |
| 145 | /// where $foo is a whole operand and $foo.bar refers to a suboperand. |
| 146 | /// This throws an exception if the name is invalid. If AllowWholeOp is |
| 147 | /// true, references to operands with suboperands are allowed, otherwise |
| 148 | /// not. |
| 149 | std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op, |
| 150 | bool AllowWholeOp = true); |
| 151 | |
| 152 | /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a |
| 153 | /// flat machineinstr operand #. |
| 154 | unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const { |
| 155 | return OperandList[Op.first].MIOperandNo + Op.second; |
| 156 | } |
| 157 | |
| 158 | /// getSubOperandNumber - Unflatten a operand number into an |
| 159 | /// operand/suboperand pair. |
| 160 | std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const { |
| 161 | for (unsigned i = 0; ; ++i) { |
| 162 | assert(i < OperandList.size() && "Invalid flat operand #"); |
| 163 | if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op) |
| 164 | return std::make_pair(i, Op-OperandList[i].MIOperandNo); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /// isFlatOperandNotEmitted - Return true if the specified flat operand # |
| 170 | /// should not be emitted with the code emitter. |
| 171 | bool isFlatOperandNotEmitted(unsigned FlatOpNo) const { |
| 172 | std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo); |
| 173 | if (OperandList[Op.first].DoNotEncode.size() > Op.second) |
| 174 | return OperandList[Op.first].DoNotEncode[Op.second]; |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | void ProcessDisableEncoding(std::string Value); |
| 179 | }; |
| 180 | |
| 181 | |
| 182 | class CodeGenInstruction { |
| 183 | public: |
| 184 | Record *TheDef; // The actual record defining this instruction. |
| 185 | std::string Namespace; // The namespace the instruction is in. |
| 186 | |
| 187 | /// AsmString - The format string used to emit a .s file for the |
| 188 | /// instruction. |
| 189 | std::string AsmString; |
| 190 | |
| 191 | /// Operands - This is information about the (ins) and (outs) list specified |
| 192 | /// to the instruction. |
| 193 | CGIOperandList Operands; |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 194 | |
Chris Lattner | f506b6b | 2010-03-18 21:42:03 +0000 | [diff] [blame] | 195 | /// ImplicitDefs/ImplicitUses - These are lists of registers that are |
| 196 | /// implicitly defined and used by the instruction. |
| 197 | std::vector<Record*> ImplicitDefs, ImplicitUses; |
| 198 | |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 199 | // Various boolean values we track for the instruction. |
| 200 | bool isReturn; |
| 201 | bool isBranch; |
Owen Anderson | 20ab290 | 2007-11-12 07:39:39 +0000 | [diff] [blame] | 202 | bool isIndirectBranch; |
Bill Wendling | 73739d0 | 2010-08-08 01:49:35 +0000 | [diff] [blame] | 203 | bool isCompare; |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 204 | bool isBarrier; |
| 205 | bool isCall; |
Dan Gohman | 15511cf | 2008-12-03 18:15:48 +0000 | [diff] [blame] | 206 | bool canFoldAsLoad; |
Chris Lattner | dcc8b4f | 2008-01-08 18:05:21 +0000 | [diff] [blame] | 207 | bool mayLoad, mayStore; |
Evan Cheng | 5127ce0 | 2007-05-16 20:45:24 +0000 | [diff] [blame] | 208 | bool isPredicable; |
Chris Lattner | aad75aa | 2005-01-02 02:29:04 +0000 | [diff] [blame] | 209 | bool isConvertibleToThreeAddress; |
| 210 | bool isCommutable; |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 211 | bool isTerminator; |
Dan Gohman | d45eddd | 2007-06-26 00:48:07 +0000 | [diff] [blame] | 212 | bool isReMaterializable; |
Chris Lattner | 5b71d3a | 2004-09-28 18:38:01 +0000 | [diff] [blame] | 213 | bool hasDelaySlot; |
Dan Gohman | 533297b | 2009-10-29 18:10:34 +0000 | [diff] [blame] | 214 | bool usesCustomInserter; |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 215 | bool hasCtrlDep; |
Evan Cheng | eaa91b0 | 2007-06-19 01:26:51 +0000 | [diff] [blame] | 216 | bool isNotDuplicable; |
Bill Wendling | 8370d38 | 2008-05-28 22:54:52 +0000 | [diff] [blame] | 217 | bool hasSideEffects; |
Bill Wendling | 8370d38 | 2008-05-28 22:54:52 +0000 | [diff] [blame] | 218 | bool neverHasSideEffects; |
| 219 | bool isAsCheapAsAMove; |
Evan Cheng | 799d697 | 2009-10-01 08:21:18 +0000 | [diff] [blame] | 220 | bool hasExtraSrcRegAllocReq; |
| 221 | bool hasExtraDefRegAllocReq; |
Jim Grosbach | 9ed2cee | 2010-10-08 18:09:59 +0000 | [diff] [blame] | 222 | |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 223 | |
Chris Lattner | f780811 | 2010-11-01 02:15:23 +0000 | [diff] [blame] | 224 | CodeGenInstruction(Record *R); |
Chris Lattner | 87c5905 | 2004-08-01 07:42:39 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 9414ae5 | 2010-03-27 20:09:24 +0000 | [diff] [blame] | 226 | /// HasOneImplicitDefWithKnownVT - If the instruction has at least one |
| 227 | /// implicit def and it has a known VT, return the VT, otherwise return |
| 228 | /// MVT::Other. |
Jim Grosbach | 9ed2cee | 2010-10-08 18:09:59 +0000 | [diff] [blame] | 229 | MVT::SimpleValueType |
Chris Lattner | 9414ae5 | 2010-03-27 20:09:24 +0000 | [diff] [blame] | 230 | HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const; |
Chris Lattner | 4d43d0f | 2010-11-01 01:07:14 +0000 | [diff] [blame] | 231 | |
| 232 | |
| 233 | /// FlattenAsmStringVariants - Flatten the specified AsmString to only |
| 234 | /// include text from the specified variant, returning the new string. |
| 235 | static std::string FlattenAsmStringVariants(StringRef AsmString, |
| 236 | unsigned Variant); |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 237 | }; |
Chris Lattner | c76e80d | 2010-11-01 04:05:41 +0000 | [diff] [blame] | 238 | |
| 239 | |
| 240 | /// CodeGenInstAlias - This represents an InstAlias definition. |
| 241 | class CodeGenInstAlias { |
| 242 | public: |
| 243 | Record *TheDef; // The actual record defining this InstAlias. |
| 244 | |
| 245 | /// AsmString - The format string used to emit a .s file for the |
| 246 | /// instruction. |
| 247 | std::string AsmString; |
| 248 | |
| 249 | /// Operands - This is information about the (ins) and (outs) list specified |
| 250 | /// to the alias. |
| 251 | CGIOperandList Operands; |
| 252 | |
Chris Lattner | b501d4f | 2010-11-01 05:34:34 +0000 | [diff] [blame^] | 253 | /// Result - The result instruction. |
| 254 | DagInit *Result; |
| 255 | |
Chris Lattner | c76e80d | 2010-11-01 04:05:41 +0000 | [diff] [blame] | 256 | CodeGenInstAlias(Record *R); |
| 257 | }; |
| 258 | } |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 259 | |
| 260 | #endif |