1 //===- CodeGenInstruction.cpp - CodeGen Instruction Class Wrapper ---------===//
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 implements the CodeGenInstruction class.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenInstruction.h"
15 #include "CodeGenTarget.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/STLExtras.h"
22 //===----------------------------------------------------------------------===//
23 // CGIOperandList Implementation
24 //===----------------------------------------------------------------------===//
26 CGIOperandList::CGIOperandList(Record
*R
) : TheDef(R
) {
28 hasOptionalDef
= false;
31 DagInit
*OutDI
= R
->getValueAsDag("OutOperandList");
33 if (DefInit
*Init
= dynamic_cast<DefInit
*>(OutDI
->getOperator())) {
34 if (Init
->getDef()->getName() != "outs")
35 throw R
->getName() + ": invalid def name for output list: use 'outs'";
37 throw R
->getName() + ": invalid output list: use 'outs'";
39 NumDefs
= OutDI
->getNumArgs();
41 DagInit
*InDI
= R
->getValueAsDag("InOperandList");
42 if (DefInit
*Init
= dynamic_cast<DefInit
*>(InDI
->getOperator())) {
43 if (Init
->getDef()->getName() != "ins")
44 throw R
->getName() + ": invalid def name for input list: use 'ins'";
46 throw R
->getName() + ": invalid input list: use 'ins'";
48 unsigned MIOperandNo
= 0;
49 std::set
<std::string
> OperandNames
;
50 for (unsigned i
= 0, e
= InDI
->getNumArgs()+OutDI
->getNumArgs(); i
!= e
; ++i
){
54 ArgInit
= OutDI
->getArg(i
);
55 ArgName
= OutDI
->getArgName(i
);
57 ArgInit
= InDI
->getArg(i
-NumDefs
);
58 ArgName
= InDI
->getArgName(i
-NumDefs
);
61 DefInit
*Arg
= dynamic_cast<DefInit
*>(ArgInit
);
63 throw "Illegal operand for the '" + R
->getName() + "' instruction!";
65 Record
*Rec
= Arg
->getDef();
66 std::string PrintMethod
= "printOperand";
67 std::string EncoderMethod
;
69 DagInit
*MIOpInfo
= 0;
70 if (Rec
->isSubClassOf("Operand")) {
71 PrintMethod
= Rec
->getValueAsString("PrintMethod");
72 // If there is an explicit encoder method, use it.
73 if (Rec
->getValue("EncoderMethod"))
74 EncoderMethod
= Rec
->getValueAsString("EncoderMethod");
75 MIOpInfo
= Rec
->getValueAsDag("MIOperandInfo");
77 // Verify that MIOpInfo has an 'ops' root value.
78 if (!dynamic_cast<DefInit
*>(MIOpInfo
->getOperator()) ||
79 dynamic_cast<DefInit
*>(MIOpInfo
->getOperator())
80 ->getDef()->getName() != "ops")
81 throw "Bad value for MIOperandInfo in operand '" + Rec
->getName() +
84 // If we have MIOpInfo, then we have #operands equal to number of entries
86 if (unsigned NumArgs
= MIOpInfo
->getNumArgs())
89 if (Rec
->isSubClassOf("PredicateOperand"))
91 else if (Rec
->isSubClassOf("OptionalDefOperand"))
92 hasOptionalDef
= true;
93 } else if (Rec
->getName() == "variable_ops") {
96 } else if (!Rec
->isSubClassOf("RegisterClass") &&
97 Rec
->getName() != "ptr_rc" && Rec
->getName() != "unknown")
98 throw "Unknown operand class '" + Rec
->getName() +
99 "' in '" + R
->getName() + "' instruction!";
101 // Check that the operand has a name and that it's unique.
103 throw "In instruction '" + R
->getName() + "', operand #" + utostr(i
) +
105 if (!OperandNames
.insert(ArgName
).second
)
106 throw "In instruction '" + R
->getName() + "', operand #" + utostr(i
) +
107 " has the same name as a previous operand!";
109 OperandList
.push_back(OperandInfo(Rec
, ArgName
, PrintMethod
, EncoderMethod
,
110 MIOperandNo
, NumOps
, MIOpInfo
));
111 MIOperandNo
+= NumOps
;
115 // Make sure the constraints list for each operand is large enough to hold
116 // constraint info, even if none is present.
117 for (unsigned i
= 0, e
= OperandList
.size(); i
!= e
; ++i
)
118 OperandList
[i
].Constraints
.resize(OperandList
[i
].MINumOperands
);
122 /// getOperandNamed - Return the index of the operand with the specified
123 /// non-empty name. If the instruction does not have an operand with the
124 /// specified name, throw an exception.
126 unsigned CGIOperandList::getOperandNamed(StringRef Name
) const {
128 if (hasOperandNamed(Name
, OpIdx
)) return OpIdx
;
129 throw "'" + TheDef
->getName() + "' does not have an operand named '$" +
133 /// hasOperandNamed - Query whether the instruction has an operand of the
134 /// given name. If so, return true and set OpIdx to the index of the
135 /// operand. Otherwise, return false.
136 bool CGIOperandList::hasOperandNamed(StringRef Name
, unsigned &OpIdx
) const {
137 assert(!Name
.empty() && "Cannot search for operand with no name!");
138 for (unsigned i
= 0, e
= OperandList
.size(); i
!= e
; ++i
)
139 if (OperandList
[i
].Name
== Name
) {
146 std::pair
<unsigned,unsigned>
147 CGIOperandList::ParseOperandName(const std::string
&Op
, bool AllowWholeOp
) {
148 if (Op
.empty() || Op
[0] != '$')
149 throw TheDef
->getName() + ": Illegal operand name: '" + Op
+ "'";
151 std::string OpName
= Op
.substr(1);
152 std::string SubOpName
;
154 // Check to see if this is $foo.bar.
155 std::string::size_type DotIdx
= OpName
.find_first_of(".");
156 if (DotIdx
!= std::string::npos
) {
157 SubOpName
= OpName
.substr(DotIdx
+1);
158 if (SubOpName
.empty())
159 throw TheDef
->getName() + ": illegal empty suboperand name in '" +Op
+"'";
160 OpName
= OpName
.substr(0, DotIdx
);
163 unsigned OpIdx
= getOperandNamed(OpName
);
165 if (SubOpName
.empty()) { // If no suboperand name was specified:
166 // If one was needed, throw.
167 if (OperandList
[OpIdx
].MINumOperands
> 1 && !AllowWholeOp
&&
169 throw TheDef
->getName() + ": Illegal to refer to"
170 " whole operand part of complex operand '" + Op
+ "'";
172 // Otherwise, return the operand.
173 return std::make_pair(OpIdx
, 0U);
176 // Find the suboperand number involved.
177 DagInit
*MIOpInfo
= OperandList
[OpIdx
].MIOperandInfo
;
179 throw TheDef
->getName() + ": unknown suboperand name in '" + Op
+ "'";
181 // Find the operand with the right name.
182 for (unsigned i
= 0, e
= MIOpInfo
->getNumArgs(); i
!= e
; ++i
)
183 if (MIOpInfo
->getArgName(i
) == SubOpName
)
184 return std::make_pair(OpIdx
, i
);
186 // Otherwise, didn't find it!
187 throw TheDef
->getName() + ": unknown suboperand name in '" + Op
+ "'";
190 static void ParseConstraint(const std::string
&CStr
, CGIOperandList
&Ops
) {
191 // EARLY_CLOBBER: @early $reg
192 std::string::size_type wpos
= CStr
.find_first_of(" \t");
193 std::string::size_type start
= CStr
.find_first_not_of(" \t");
194 std::string Tok
= CStr
.substr(start
, wpos
- start
);
195 if (Tok
== "@earlyclobber") {
196 std::string Name
= CStr
.substr(wpos
+1);
197 wpos
= Name
.find_first_not_of(" \t");
198 if (wpos
== std::string::npos
)
199 throw "Illegal format for @earlyclobber constraint: '" + CStr
+ "'";
200 Name
= Name
.substr(wpos
);
201 std::pair
<unsigned,unsigned> Op
= Ops
.ParseOperandName(Name
, false);
203 // Build the string for the operand
204 if (!Ops
[Op
.first
].Constraints
[Op
.second
].isNone())
205 throw "Operand '" + Name
+ "' cannot have multiple constraints!";
206 Ops
[Op
.first
].Constraints
[Op
.second
] =
207 CGIOperandList::ConstraintInfo::getEarlyClobber();
211 // Only other constraint is "TIED_TO" for now.
212 std::string::size_type pos
= CStr
.find_first_of('=');
213 assert(pos
!= std::string::npos
&& "Unrecognized constraint");
214 start
= CStr
.find_first_not_of(" \t");
215 std::string Name
= CStr
.substr(start
, pos
- start
);
217 // TIED_TO: $src1 = $dst
218 wpos
= Name
.find_first_of(" \t");
219 if (wpos
== std::string::npos
)
220 throw "Illegal format for tied-to constraint: '" + CStr
+ "'";
221 std::string DestOpName
= Name
.substr(0, wpos
);
222 std::pair
<unsigned,unsigned> DestOp
= Ops
.ParseOperandName(DestOpName
, false);
224 Name
= CStr
.substr(pos
+1);
225 wpos
= Name
.find_first_not_of(" \t");
226 if (wpos
== std::string::npos
)
227 throw "Illegal format for tied-to constraint: '" + CStr
+ "'";
229 std::pair
<unsigned,unsigned> SrcOp
=
230 Ops
.ParseOperandName(Name
.substr(wpos
), false);
232 throw "Illegal tied-to operand constraint '" + CStr
+ "'";
235 unsigned FlatOpNo
= Ops
.getFlattenedOperandNumber(SrcOp
);
237 if (!Ops
[DestOp
.first
].Constraints
[DestOp
.second
].isNone())
238 throw "Operand '" + DestOpName
+ "' cannot have multiple constraints!";
239 Ops
[DestOp
.first
].Constraints
[DestOp
.second
] =
240 CGIOperandList::ConstraintInfo::getTied(FlatOpNo
);
243 static void ParseConstraints(const std::string
&CStr
, CGIOperandList
&Ops
) {
244 if (CStr
.empty()) return;
246 const std::string
delims(",");
247 std::string::size_type bidx
, eidx
;
249 bidx
= CStr
.find_first_not_of(delims
);
250 while (bidx
!= std::string::npos
) {
251 eidx
= CStr
.find_first_of(delims
, bidx
);
252 if (eidx
== std::string::npos
)
253 eidx
= CStr
.length();
255 ParseConstraint(CStr
.substr(bidx
, eidx
- bidx
), Ops
);
256 bidx
= CStr
.find_first_not_of(delims
, eidx
);
260 void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding
) {
263 tie(OpName
, DisableEncoding
) = getToken(DisableEncoding
, " ,\t");
264 if (OpName
.empty()) break;
266 // Figure out which operand this is.
267 std::pair
<unsigned,unsigned> Op
= ParseOperandName(OpName
, false);
269 // Mark the operand as not-to-be encoded.
270 if (Op
.second
>= OperandList
[Op
.first
].DoNotEncode
.size())
271 OperandList
[Op
.first
].DoNotEncode
.resize(Op
.second
+1);
272 OperandList
[Op
.first
].DoNotEncode
[Op
.second
] = true;
277 //===----------------------------------------------------------------------===//
278 // CodeGenInstruction Implementation
279 //===----------------------------------------------------------------------===//
281 CodeGenInstruction::CodeGenInstruction(Record
*R
) : TheDef(R
), Operands(R
) {
282 Namespace
= R
->getValueAsString("Namespace");
283 AsmString
= R
->getValueAsString("AsmString");
285 isReturn
= R
->getValueAsBit("isReturn");
286 isBranch
= R
->getValueAsBit("isBranch");
287 isIndirectBranch
= R
->getValueAsBit("isIndirectBranch");
288 isCompare
= R
->getValueAsBit("isCompare");
289 isBarrier
= R
->getValueAsBit("isBarrier");
290 isCall
= R
->getValueAsBit("isCall");
291 canFoldAsLoad
= R
->getValueAsBit("canFoldAsLoad");
292 mayLoad
= R
->getValueAsBit("mayLoad");
293 mayStore
= R
->getValueAsBit("mayStore");
294 isPredicable
= Operands
.isPredicable
|| R
->getValueAsBit("isPredicable");
295 isConvertibleToThreeAddress
= R
->getValueAsBit("isConvertibleToThreeAddress");
296 isCommutable
= R
->getValueAsBit("isCommutable");
297 isTerminator
= R
->getValueAsBit("isTerminator");
298 isReMaterializable
= R
->getValueAsBit("isReMaterializable");
299 hasDelaySlot
= R
->getValueAsBit("hasDelaySlot");
300 usesCustomInserter
= R
->getValueAsBit("usesCustomInserter");
301 hasCtrlDep
= R
->getValueAsBit("hasCtrlDep");
302 isNotDuplicable
= R
->getValueAsBit("isNotDuplicable");
303 hasSideEffects
= R
->getValueAsBit("hasSideEffects");
304 neverHasSideEffects
= R
->getValueAsBit("neverHasSideEffects");
305 isAsCheapAsAMove
= R
->getValueAsBit("isAsCheapAsAMove");
306 hasExtraSrcRegAllocReq
= R
->getValueAsBit("hasExtraSrcRegAllocReq");
307 hasExtraDefRegAllocReq
= R
->getValueAsBit("hasExtraDefRegAllocReq");
308 ImplicitDefs
= R
->getValueAsListOfDefs("Defs");
309 ImplicitUses
= R
->getValueAsListOfDefs("Uses");
311 if (neverHasSideEffects
+ hasSideEffects
> 1)
312 throw R
->getName() + ": multiple conflicting side-effect flags set!";
314 // Parse Constraints.
315 ParseConstraints(R
->getValueAsString("Constraints"), Operands
);
317 // Parse the DisableEncoding field.
318 Operands
.ProcessDisableEncoding(R
->getValueAsString("DisableEncoding"));
321 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
322 /// implicit def and it has a known VT, return the VT, otherwise return
324 MVT::SimpleValueType
CodeGenInstruction::
325 HasOneImplicitDefWithKnownVT(const CodeGenTarget
&TargetInfo
) const {
326 if (ImplicitDefs
.empty()) return MVT::Other
;
328 // Check to see if the first implicit def has a resolvable type.
329 Record
*FirstImplicitDef
= ImplicitDefs
[0];
330 assert(FirstImplicitDef
->isSubClassOf("Register"));
331 const std::vector
<MVT::SimpleValueType
> &RegVTs
=
332 TargetInfo
.getRegisterVTs(FirstImplicitDef
);
333 if (RegVTs
.size() == 1)
339 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
340 /// include text from the specified variant, returning the new string.
341 std::string
CodeGenInstruction::
342 FlattenAsmStringVariants(StringRef Cur
, unsigned Variant
) {
343 std::string Res
= "";
346 // Find the start of the next variant string.
347 size_t VariantsStart
= 0;
348 for (size_t e
= Cur
.size(); VariantsStart
!= e
; ++VariantsStart
)
349 if (Cur
[VariantsStart
] == '{' &&
350 (VariantsStart
== 0 || (Cur
[VariantsStart
-1] != '$' &&
351 Cur
[VariantsStart
-1] != '\\')))
354 // Add the prefix to the result.
355 Res
+= Cur
.slice(0, VariantsStart
);
356 if (VariantsStart
== Cur
.size())
359 ++VariantsStart
; // Skip the '{'.
361 // Scan to the end of the variants string.
362 size_t VariantsEnd
= VariantsStart
;
363 unsigned NestedBraces
= 1;
364 for (size_t e
= Cur
.size(); VariantsEnd
!= e
; ++VariantsEnd
) {
365 if (Cur
[VariantsEnd
] == '}' && Cur
[VariantsEnd
-1] != '\\') {
366 if (--NestedBraces
== 0)
368 } else if (Cur
[VariantsEnd
] == '{')
372 // Select the Nth variant (or empty).
373 StringRef Selection
= Cur
.slice(VariantsStart
, VariantsEnd
);
374 for (unsigned i
= 0; i
!= Variant
; ++i
)
375 Selection
= Selection
.split('|').second
;
376 Res
+= Selection
.split('|').first
;
378 assert(VariantsEnd
!= Cur
.size() &&
379 "Unterminated variants in assembly string!");
380 Cur
= Cur
.substr(VariantsEnd
+ 1);
387 //===----------------------------------------------------------------------===//
388 /// CodeGenInstAlias Implementation
389 //===----------------------------------------------------------------------===//
391 CodeGenInstAlias::CodeGenInstAlias(Record
*R
) : TheDef(R
), Operands(R
) {
392 AsmString
= R
->getValueAsString("AsmString");
394 Result
= R
->getValueAsDag("ResultInst");