blob: 285da140087fd093a408615900d30a514385aa67 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- CodeGenInstruction.h - Instruction Class Wrapper ---------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerfd6c2f02007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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
17#include "llvm/CodeGen/ValueTypes.h"
18#include <string>
19#include <vector>
20#include <utility>
21
22namespace llvm {
23 class Record;
24 class DagInit;
25
26 class CodeGenInstruction {
27 public:
28 Record *TheDef; // The actual record defining this instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029 std::string Namespace; // The namespace the instruction is in.
30
31 /// AsmString - The format string used to emit a .s file for the
32 /// instruction.
33 std::string AsmString;
Chris Lattner4a7bcdc2008-01-06 01:35:39 +000034
Chris Lattnerfd38cb12010-02-10 01:45:28 +000035 class ConstraintInfo {
36 enum { None, EarlyClobber, Tied } Kind;
37 unsigned OtherTiedOperand;
38 public:
39 ConstraintInfo() : Kind(None) {}
40
41 static ConstraintInfo getEarlyClobber() {
42 ConstraintInfo I;
43 I.Kind = EarlyClobber;
Chris Lattner62446172010-02-10 21:22:51 +000044 I.OtherTiedOperand = 0;
Chris Lattnerfd38cb12010-02-10 01:45:28 +000045 return I;
46 }
47
48 static ConstraintInfo getTied(unsigned Op) {
49 ConstraintInfo I;
50 I.Kind = Tied;
51 I.OtherTiedOperand = Op;
52 return I;
53 }
54
55 bool isNone() const { return Kind == None; }
56 bool isEarlyClobber() const { return Kind == EarlyClobber; }
57 bool isTied() const { return Kind == Tied; }
58
59 unsigned getTiedOperand() const {
60 assert(isTied());
61 return OtherTiedOperand;
62 }
63 };
64
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 /// OperandInfo - The information we keep track of for each operand in the
66 /// operand list for a tablegen instruction.
67 struct OperandInfo {
68 /// Rec - The definition this operand is declared as.
69 ///
70 Record *Rec;
71
72 /// Name - If this operand was assigned a symbolic name, this is it,
73 /// otherwise, it's empty.
74 std::string Name;
75
76 /// PrinterMethodName - The method used to print operands of this type in
77 /// the asmprinter.
78 std::string PrinterMethodName;
79
80 /// MIOperandNo - Currently (this is meant to be phased out), some logical
81 /// operands correspond to multiple MachineInstr operands. In the X86
82 /// target for example, one address operand is represented as 4
83 /// MachineOperands. Because of this, the operand number in the
84 /// OperandList may not match the MachineInstr operand num. Until it
85 /// does, this contains the MI operand index of this operand.
86 unsigned MIOperandNo;
87 unsigned MINumOperands; // The number of operands.
88
89 /// DoNotEncode - Bools are set to true in this vector for each operand in
90 /// the DisableEncoding list. These should not be emitted by the code
91 /// emitter.
92 std::vector<bool> DoNotEncode;
93
94 /// MIOperandInfo - Default MI operand type. Note an operand may be made
95 /// up of multiple MI operands.
96 DagInit *MIOperandInfo;
97
98 /// Constraint info for this operand. This operand can have pieces, so we
99 /// track constraint info for each.
Chris Lattnerfd38cb12010-02-10 01:45:28 +0000100 std::vector<ConstraintInfo> Constraints;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101
102 OperandInfo(Record *R, const std::string &N, const std::string &PMN,
103 unsigned MION, unsigned MINO, DagInit *MIOI)
104 : Rec(R), Name(N), PrinterMethodName(PMN), MIOperandNo(MION),
105 MINumOperands(MINO), MIOperandInfo(MIOI) {}
106 };
107
Evan Chengb783fa32007-07-19 01:14:50 +0000108 /// NumDefs - Number of def operands declared.
109 ///
110 unsigned NumDefs;
111
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 /// OperandList - The list of declared operands, along with their declared
113 /// type (which is a record).
114 std::vector<OperandInfo> OperandList;
115
116 // Various boolean values we track for the instruction.
117 bool isReturn;
118 bool isBranch;
Owen Andersonf8053082007-11-12 07:39:39 +0000119 bool isIndirectBranch;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 bool isBarrier;
121 bool isCall;
Dan Gohman5574cc72008-12-03 18:15:48 +0000122 bool canFoldAsLoad;
Chris Lattner76a64e22008-01-08 18:05:21 +0000123 bool mayLoad, mayStore;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 bool isPredicable;
125 bool isConvertibleToThreeAddress;
126 bool isCommutable;
127 bool isTerminator;
128 bool isReMaterializable;
129 bool hasDelaySlot;
Dan Gohman30afe012009-10-29 18:10:34 +0000130 bool usesCustomInserter;
Chris Lattner2fb37c02008-01-07 05:19:29 +0000131 bool isVariadic;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 bool hasCtrlDep;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 bool isNotDuplicable;
134 bool hasOptionalDef;
Bill Wendlingaa25bb12008-05-28 22:54:52 +0000135 bool hasSideEffects;
136 bool mayHaveSideEffects;
137 bool neverHasSideEffects;
138 bool isAsCheapAsAMove;
Evan Chengf6ea3032009-10-01 08:21:18 +0000139 bool hasExtraSrcRegAllocReq;
140 bool hasExtraDefRegAllocReq;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141
142 /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
143 /// where $foo is a whole operand and $foo.bar refers to a suboperand.
144 /// This throws an exception if the name is invalid. If AllowWholeOp is
145 /// true, references to operands with suboperands are allowed, otherwise
146 /// not.
147 std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
148 bool AllowWholeOp = true);
149
150 /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
151 /// flat machineinstr operand #.
152 unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
153 return OperandList[Op.first].MIOperandNo + Op.second;
154 }
155
156 /// getSubOperandNumber - Unflatten a operand number into an
157 /// operand/suboperand pair.
158 std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
159 for (unsigned i = 0; ; ++i) {
160 assert(i < OperandList.size() && "Invalid flat operand #");
161 if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
162 return std::make_pair(i, Op-OperandList[i].MIOperandNo);
163 }
164 }
165
166
167 /// isFlatOperandNotEmitted - Return true if the specified flat operand #
168 /// should not be emitted with the code emitter.
169 bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
170 std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
171 if (OperandList[Op.first].DoNotEncode.size() > Op.second)
172 return OperandList[Op.first].DoNotEncode[Op.second];
173 return false;
174 }
175
176 CodeGenInstruction(Record *R, const std::string &AsmStr);
177
178 /// getOperandNamed - Return the index of the operand with the specified
179 /// non-empty name. If the instruction does not have an operand with the
180 /// specified name, throw an exception.
181 unsigned getOperandNamed(const std::string &Name) const;
182 };
183}
184
185#endif