1 //===- CodeGenInstruction.cpp - CodeGen Instruction Class Wrapper ---------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the CodeGenInstruction class.
11 //===----------------------------------------------------------------------===//
13 #include "CodeGenInstruction.h"
14 #include "CodeGenTarget.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/TableGen/Error.h"
19 #include "llvm/TableGen/Record.h"
23 //===----------------------------------------------------------------------===//
24 // CGIOperandList Implementation
25 //===----------------------------------------------------------------------===//
27 CGIOperandList::CGIOperandList(Record
*R
) : TheDef(R
) {
29 hasOptionalDef
= false;
32 DagInit
*OutDI
= R
->getValueAsDag("OutOperandList");
34 if (DefInit
*Init
= dyn_cast
<DefInit
>(OutDI
->getOperator())) {
35 if (Init
->getDef()->getName() != "outs")
36 PrintFatalError(R
->getLoc(),
38 ": invalid def name for output list: use 'outs'");
40 PrintFatalError(R
->getLoc(),
41 R
->getName() + ": invalid output list: use 'outs'");
43 NumDefs
= OutDI
->getNumArgs();
45 DagInit
*InDI
= R
->getValueAsDag("InOperandList");
46 if (DefInit
*Init
= dyn_cast
<DefInit
>(InDI
->getOperator())) {
47 if (Init
->getDef()->getName() != "ins")
48 PrintFatalError(R
->getLoc(),
50 ": invalid def name for input list: use 'ins'");
52 PrintFatalError(R
->getLoc(),
53 R
->getName() + ": invalid input list: use 'ins'");
55 unsigned MIOperandNo
= 0;
56 std::set
<std::string
> OperandNames
;
57 unsigned e
= InDI
->getNumArgs() + OutDI
->getNumArgs();
58 OperandList
.reserve(e
);
59 for (unsigned i
= 0; i
!= e
; ++i
){
63 ArgInit
= OutDI
->getArg(i
);
64 ArgName
= OutDI
->getArgNameStr(i
);
66 ArgInit
= InDI
->getArg(i
-NumDefs
);
67 ArgName
= InDI
->getArgNameStr(i
-NumDefs
);
70 DefInit
*Arg
= dyn_cast
<DefInit
>(ArgInit
);
72 PrintFatalError(R
->getLoc(), "Illegal operand for the '" + R
->getName() +
75 Record
*Rec
= Arg
->getDef();
76 std::string PrintMethod
= "printOperand";
77 std::string EncoderMethod
;
78 std::string OperandType
= "OPERAND_UNKNOWN";
79 std::string OperandNamespace
= "MCOI";
81 DagInit
*MIOpInfo
= nullptr;
82 if (Rec
->isSubClassOf("RegisterOperand")) {
83 PrintMethod
= Rec
->getValueAsString("PrintMethod");
84 OperandType
= Rec
->getValueAsString("OperandType");
85 OperandNamespace
= Rec
->getValueAsString("OperandNamespace");
86 EncoderMethod
= Rec
->getValueAsString("EncoderMethod");
87 } else if (Rec
->isSubClassOf("Operand")) {
88 PrintMethod
= Rec
->getValueAsString("PrintMethod");
89 OperandType
= Rec
->getValueAsString("OperandType");
90 OperandNamespace
= Rec
->getValueAsString("OperandNamespace");
91 // If there is an explicit encoder method, use it.
92 EncoderMethod
= Rec
->getValueAsString("EncoderMethod");
93 MIOpInfo
= Rec
->getValueAsDag("MIOperandInfo");
95 // Verify that MIOpInfo has an 'ops' root value.
96 if (!isa
<DefInit
>(MIOpInfo
->getOperator()) ||
97 cast
<DefInit
>(MIOpInfo
->getOperator())->getDef()->getName() != "ops")
98 PrintFatalError(R
->getLoc(),
99 "Bad value for MIOperandInfo in operand '" +
100 Rec
->getName() + "'\n");
102 // If we have MIOpInfo, then we have #operands equal to number of entries
104 if (unsigned NumArgs
= MIOpInfo
->getNumArgs())
107 if (Rec
->isSubClassOf("PredicateOp"))
109 else if (Rec
->isSubClassOf("OptionalDefOperand"))
110 hasOptionalDef
= true;
111 } else if (Rec
->getName() == "variable_ops") {
114 } else if (Rec
->isSubClassOf("RegisterClass")) {
115 OperandType
= "OPERAND_REGISTER";
116 } else if (!Rec
->isSubClassOf("PointerLikeRegClass") &&
117 !Rec
->isSubClassOf("unknown_class"))
118 PrintFatalError(R
->getLoc(), "Unknown operand class '" + Rec
->getName() +
119 "' in '" + R
->getName() +
122 // Check that the operand has a name and that it's unique.
124 PrintFatalError(R
->getLoc(), "In instruction '" + R
->getName() +
125 "', operand #" + Twine(i
) +
127 if (!OperandNames
.insert(ArgName
).second
)
128 PrintFatalError(R
->getLoc(),
129 "In instruction '" + R
->getName() + "', operand #" +
131 " has the same name as a previous operand!");
133 OperandList
.emplace_back(Rec
, ArgName
, PrintMethod
, EncoderMethod
,
134 OperandNamespace
+ "::" + OperandType
, MIOperandNo
,
136 MIOperandNo
+= NumOps
;
140 // Make sure the constraints list for each operand is large enough to hold
141 // constraint info, even if none is present.
142 for (OperandInfo
&OpInfo
: OperandList
)
143 OpInfo
.Constraints
.resize(OpInfo
.MINumOperands
);
147 /// getOperandNamed - Return the index of the operand with the specified
148 /// non-empty name. If the instruction does not have an operand with the
149 /// specified name, abort.
151 unsigned CGIOperandList::getOperandNamed(StringRef Name
) const {
153 if (hasOperandNamed(Name
, OpIdx
))
155 PrintFatalError(TheDef
->getLoc(), "'" + TheDef
->getName() +
156 "' does not have an operand named '$" +
160 /// hasOperandNamed - Query whether the instruction has an operand of the
161 /// given name. If so, return true and set OpIdx to the index of the
162 /// operand. Otherwise, return false.
163 bool CGIOperandList::hasOperandNamed(StringRef Name
, unsigned &OpIdx
) const {
164 assert(!Name
.empty() && "Cannot search for operand with no name!");
165 for (unsigned i
= 0, e
= OperandList
.size(); i
!= e
; ++i
)
166 if (OperandList
[i
].Name
== Name
) {
173 std::pair
<unsigned,unsigned>
174 CGIOperandList::ParseOperandName(const std::string
&Op
, bool AllowWholeOp
) {
175 if (Op
.empty() || Op
[0] != '$')
176 PrintFatalError(TheDef
->getLoc(),
177 TheDef
->getName() + ": Illegal operand name: '" + Op
+ "'");
179 std::string OpName
= Op
.substr(1);
180 std::string SubOpName
;
182 // Check to see if this is $foo.bar.
183 std::string::size_type DotIdx
= OpName
.find_first_of('.');
184 if (DotIdx
!= std::string::npos
) {
185 SubOpName
= OpName
.substr(DotIdx
+1);
186 if (SubOpName
.empty())
187 PrintFatalError(TheDef
->getLoc(),
189 ": illegal empty suboperand name in '" + Op
+ "'");
190 OpName
= OpName
.substr(0, DotIdx
);
193 unsigned OpIdx
= getOperandNamed(OpName
);
195 if (SubOpName
.empty()) { // If no suboperand name was specified:
196 // If one was needed, throw.
197 if (OperandList
[OpIdx
].MINumOperands
> 1 && !AllowWholeOp
&&
199 PrintFatalError(TheDef
->getLoc(),
201 ": Illegal to refer to"
202 " whole operand part of complex operand '" +
205 // Otherwise, return the operand.
206 return std::make_pair(OpIdx
, 0U);
209 // Find the suboperand number involved.
210 DagInit
*MIOpInfo
= OperandList
[OpIdx
].MIOperandInfo
;
212 PrintFatalError(TheDef
->getLoc(), TheDef
->getName() +
213 ": unknown suboperand name in '" +
216 // Find the operand with the right name.
217 for (unsigned i
= 0, e
= MIOpInfo
->getNumArgs(); i
!= e
; ++i
)
218 if (MIOpInfo
->getArgNameStr(i
) == SubOpName
)
219 return std::make_pair(OpIdx
, i
);
221 // Otherwise, didn't find it!
222 PrintFatalError(TheDef
->getLoc(), TheDef
->getName() +
223 ": unknown suboperand name in '" + Op
+
225 return std::make_pair(0U, 0U);
228 static void ParseConstraint(const std::string
&CStr
, CGIOperandList
&Ops
,
230 // EARLY_CLOBBER: @early $reg
231 std::string::size_type wpos
= CStr
.find_first_of(" \t");
232 std::string::size_type start
= CStr
.find_first_not_of(" \t");
233 std::string Tok
= CStr
.substr(start
, wpos
- start
);
234 if (Tok
== "@earlyclobber") {
235 std::string Name
= CStr
.substr(wpos
+1);
236 wpos
= Name
.find_first_not_of(" \t");
237 if (wpos
== std::string::npos
)
239 Rec
->getLoc(), "Illegal format for @earlyclobber constraint in '" +
240 Rec
->getName() + "': '" + CStr
+ "'");
241 Name
= Name
.substr(wpos
);
242 std::pair
<unsigned,unsigned> Op
= Ops
.ParseOperandName(Name
, false);
244 // Build the string for the operand
245 if (!Ops
[Op
.first
].Constraints
[Op
.second
].isNone())
247 Rec
->getLoc(), "Operand '" + Name
+ "' of '" + Rec
->getName() +
248 "' cannot have multiple constraints!");
249 Ops
[Op
.first
].Constraints
[Op
.second
] =
250 CGIOperandList::ConstraintInfo::getEarlyClobber();
254 // Only other constraint is "TIED_TO" for now.
255 std::string::size_type pos
= CStr
.find_first_of('=');
256 if (pos
== std::string::npos
)
258 Rec
->getLoc(), "Unrecognized constraint '" + CStr
+
259 "' in '" + Rec
->getName() + "'");
260 start
= CStr
.find_first_not_of(" \t");
262 // TIED_TO: $src1 = $dst
263 wpos
= CStr
.find_first_of(" \t", start
);
264 if (wpos
== std::string::npos
|| wpos
> pos
)
266 Rec
->getLoc(), "Illegal format for tied-to constraint in '" +
267 Rec
->getName() + "': '" + CStr
+ "'");
268 std::string LHSOpName
= StringRef(CStr
).substr(start
, wpos
- start
);
269 std::pair
<unsigned,unsigned> LHSOp
= Ops
.ParseOperandName(LHSOpName
, false);
271 wpos
= CStr
.find_first_not_of(" \t", pos
+ 1);
272 if (wpos
== std::string::npos
)
274 Rec
->getLoc(), "Illegal format for tied-to constraint: '" + CStr
+ "'");
276 std::string RHSOpName
= StringRef(CStr
).substr(wpos
);
277 std::pair
<unsigned,unsigned> RHSOp
= Ops
.ParseOperandName(RHSOpName
, false);
279 // Sort the operands into order, which should put the output one
280 // first. But keep the original order, for use in diagnostics.
281 bool FirstIsDest
= (LHSOp
< RHSOp
);
282 std::pair
<unsigned,unsigned> DestOp
= (FirstIsDest
? LHSOp
: RHSOp
);
283 StringRef DestOpName
= (FirstIsDest
? LHSOpName
: RHSOpName
);
284 std::pair
<unsigned,unsigned> SrcOp
= (FirstIsDest
? RHSOp
: LHSOp
);
285 StringRef SrcOpName
= (FirstIsDest
? RHSOpName
: LHSOpName
);
287 // Ensure one operand is a def and the other is a use.
288 if (DestOp
.first
>= Ops
.NumDefs
)
290 Rec
->getLoc(), "Input operands '" + LHSOpName
+ "' and '" + RHSOpName
+
291 "' of '" + Rec
->getName() + "' cannot be tied!");
292 if (SrcOp
.first
< Ops
.NumDefs
)
294 Rec
->getLoc(), "Output operands '" + LHSOpName
+ "' and '" + RHSOpName
+
295 "' of '" + Rec
->getName() + "' cannot be tied!");
297 // The constraint has to go on the operand with higher index, i.e.
298 // the source one. Check there isn't another constraint there
300 if (!Ops
[SrcOp
.first
].Constraints
[SrcOp
.second
].isNone())
302 Rec
->getLoc(), "Operand '" + SrcOpName
+ "' of '" + Rec
->getName() +
303 "' cannot have multiple constraints!");
305 unsigned DestFlatOpNo
= Ops
.getFlattenedOperandNumber(DestOp
);
306 auto NewConstraint
= CGIOperandList::ConstraintInfo::getTied(DestFlatOpNo
);
308 // Check that the earlier operand is not the target of another tie
309 // before making it the target of this one.
310 for (const CGIOperandList::OperandInfo
&Op
: Ops
) {
311 for (unsigned i
= 0; i
< Op
.MINumOperands
; i
++)
312 if (Op
.Constraints
[i
] == NewConstraint
)
314 Rec
->getLoc(), "Operand '" + DestOpName
+ "' of '" + Rec
->getName() +
315 "' cannot have multiple operands tied to it!");
318 Ops
[SrcOp
.first
].Constraints
[SrcOp
.second
] = NewConstraint
;
321 static void ParseConstraints(const std::string
&CStr
, CGIOperandList
&Ops
,
323 if (CStr
.empty()) return;
325 const std::string
delims(",");
326 std::string::size_type bidx
, eidx
;
328 bidx
= CStr
.find_first_not_of(delims
);
329 while (bidx
!= std::string::npos
) {
330 eidx
= CStr
.find_first_of(delims
, bidx
);
331 if (eidx
== std::string::npos
)
332 eidx
= CStr
.length();
334 ParseConstraint(CStr
.substr(bidx
, eidx
- bidx
), Ops
, Rec
);
335 bidx
= CStr
.find_first_not_of(delims
, eidx
);
339 void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding
) {
341 std::pair
<StringRef
, StringRef
> P
= getToken(DisableEncoding
, " ,\t");
342 std::string OpName
= P
.first
;
343 DisableEncoding
= P
.second
;
344 if (OpName
.empty()) break;
346 // Figure out which operand this is.
347 std::pair
<unsigned,unsigned> Op
= ParseOperandName(OpName
, false);
349 // Mark the operand as not-to-be encoded.
350 if (Op
.second
>= OperandList
[Op
.first
].DoNotEncode
.size())
351 OperandList
[Op
.first
].DoNotEncode
.resize(Op
.second
+1);
352 OperandList
[Op
.first
].DoNotEncode
[Op
.second
] = true;
357 //===----------------------------------------------------------------------===//
358 // CodeGenInstruction Implementation
359 //===----------------------------------------------------------------------===//
361 CodeGenInstruction::CodeGenInstruction(Record
*R
)
362 : TheDef(R
), Operands(R
), InferredFrom(nullptr) {
363 Namespace
= R
->getValueAsString("Namespace");
364 AsmString
= R
->getValueAsString("AsmString");
366 isPreISelOpcode
= R
->getValueAsBit("isPreISelOpcode");
367 isReturn
= R
->getValueAsBit("isReturn");
368 isEHScopeReturn
= R
->getValueAsBit("isEHScopeReturn");
369 isBranch
= R
->getValueAsBit("isBranch");
370 isIndirectBranch
= R
->getValueAsBit("isIndirectBranch");
371 isCompare
= R
->getValueAsBit("isCompare");
372 isMoveImm
= R
->getValueAsBit("isMoveImm");
373 isMoveReg
= R
->getValueAsBit("isMoveReg");
374 isBitcast
= R
->getValueAsBit("isBitcast");
375 isSelect
= R
->getValueAsBit("isSelect");
376 isBarrier
= R
->getValueAsBit("isBarrier");
377 isCall
= R
->getValueAsBit("isCall");
378 isAdd
= R
->getValueAsBit("isAdd");
379 isTrap
= R
->getValueAsBit("isTrap");
380 canFoldAsLoad
= R
->getValueAsBit("canFoldAsLoad");
381 isPredicable
= !R
->getValueAsBit("isUnpredicable") && (
382 Operands
.isPredicable
|| R
->getValueAsBit("isPredicable"));
383 isConvertibleToThreeAddress
= R
->getValueAsBit("isConvertibleToThreeAddress");
384 isCommutable
= R
->getValueAsBit("isCommutable");
385 isTerminator
= R
->getValueAsBit("isTerminator");
386 isReMaterializable
= R
->getValueAsBit("isReMaterializable");
387 hasDelaySlot
= R
->getValueAsBit("hasDelaySlot");
388 usesCustomInserter
= R
->getValueAsBit("usesCustomInserter");
389 hasPostISelHook
= R
->getValueAsBit("hasPostISelHook");
390 hasCtrlDep
= R
->getValueAsBit("hasCtrlDep");
391 isNotDuplicable
= R
->getValueAsBit("isNotDuplicable");
392 isRegSequence
= R
->getValueAsBit("isRegSequence");
393 isExtractSubreg
= R
->getValueAsBit("isExtractSubreg");
394 isInsertSubreg
= R
->getValueAsBit("isInsertSubreg");
395 isConvergent
= R
->getValueAsBit("isConvergent");
396 hasNoSchedulingInfo
= R
->getValueAsBit("hasNoSchedulingInfo");
397 FastISelShouldIgnore
= R
->getValueAsBit("FastISelShouldIgnore");
398 variadicOpsAreDefs
= R
->getValueAsBit("variadicOpsAreDefs");
401 mayLoad
= R
->getValueAsBitOrUnset("mayLoad", Unset
);
402 mayLoad_Unset
= Unset
;
403 mayStore
= R
->getValueAsBitOrUnset("mayStore", Unset
);
404 mayStore_Unset
= Unset
;
405 mayRaiseFPException
= R
->getValueAsBit("mayRaiseFPException");
406 hasSideEffects
= R
->getValueAsBitOrUnset("hasSideEffects", Unset
);
407 hasSideEffects_Unset
= Unset
;
409 isAsCheapAsAMove
= R
->getValueAsBit("isAsCheapAsAMove");
410 hasExtraSrcRegAllocReq
= R
->getValueAsBit("hasExtraSrcRegAllocReq");
411 hasExtraDefRegAllocReq
= R
->getValueAsBit("hasExtraDefRegAllocReq");
412 isCodeGenOnly
= R
->getValueAsBit("isCodeGenOnly");
413 isPseudo
= R
->getValueAsBit("isPseudo");
414 ImplicitDefs
= R
->getValueAsListOfDefs("Defs");
415 ImplicitUses
= R
->getValueAsListOfDefs("Uses");
417 // This flag is only inferred from the pattern.
419 hasChain_Inferred
= false;
421 // Parse Constraints.
422 ParseConstraints(R
->getValueAsString("Constraints"), Operands
, R
);
424 // Parse the DisableEncoding field.
425 Operands
.ProcessDisableEncoding(R
->getValueAsString("DisableEncoding"));
427 // First check for a ComplexDeprecationPredicate.
428 if (R
->getValue("ComplexDeprecationPredicate")) {
429 HasComplexDeprecationPredicate
= true;
430 DeprecatedReason
= R
->getValueAsString("ComplexDeprecationPredicate");
431 } else if (RecordVal
*Dep
= R
->getValue("DeprecatedFeatureMask")) {
432 // Check if we have a Subtarget feature mask.
433 HasComplexDeprecationPredicate
= false;
434 DeprecatedReason
= Dep
->getValue()->getAsString();
436 // This instruction isn't deprecated.
437 HasComplexDeprecationPredicate
= false;
438 DeprecatedReason
= "";
442 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
443 /// implicit def and it has a known VT, return the VT, otherwise return
445 MVT::SimpleValueType
CodeGenInstruction::
446 HasOneImplicitDefWithKnownVT(const CodeGenTarget
&TargetInfo
) const {
447 if (ImplicitDefs
.empty()) return MVT::Other
;
449 // Check to see if the first implicit def has a resolvable type.
450 Record
*FirstImplicitDef
= ImplicitDefs
[0];
451 assert(FirstImplicitDef
->isSubClassOf("Register"));
452 const std::vector
<ValueTypeByHwMode
> &RegVTs
=
453 TargetInfo
.getRegisterVTs(FirstImplicitDef
);
454 if (RegVTs
.size() == 1 && RegVTs
[0].isSimple())
455 return RegVTs
[0].getSimple().SimpleTy
;
460 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
461 /// include text from the specified variant, returning the new string.
462 std::string
CodeGenInstruction::
463 FlattenAsmStringVariants(StringRef Cur
, unsigned Variant
) {
464 std::string Res
= "";
467 // Find the start of the next variant string.
468 size_t VariantsStart
= 0;
469 for (size_t e
= Cur
.size(); VariantsStart
!= e
; ++VariantsStart
)
470 if (Cur
[VariantsStart
] == '{' &&
471 (VariantsStart
== 0 || (Cur
[VariantsStart
-1] != '$' &&
472 Cur
[VariantsStart
-1] != '\\')))
475 // Add the prefix to the result.
476 Res
+= Cur
.slice(0, VariantsStart
);
477 if (VariantsStart
== Cur
.size())
480 ++VariantsStart
; // Skip the '{'.
482 // Scan to the end of the variants string.
483 size_t VariantsEnd
= VariantsStart
;
484 unsigned NestedBraces
= 1;
485 for (size_t e
= Cur
.size(); VariantsEnd
!= e
; ++VariantsEnd
) {
486 if (Cur
[VariantsEnd
] == '}' && Cur
[VariantsEnd
-1] != '\\') {
487 if (--NestedBraces
== 0)
489 } else if (Cur
[VariantsEnd
] == '{')
493 // Select the Nth variant (or empty).
494 StringRef Selection
= Cur
.slice(VariantsStart
, VariantsEnd
);
495 for (unsigned i
= 0; i
!= Variant
; ++i
)
496 Selection
= Selection
.split('|').second
;
497 Res
+= Selection
.split('|').first
;
499 assert(VariantsEnd
!= Cur
.size() &&
500 "Unterminated variants in assembly string!");
501 Cur
= Cur
.substr(VariantsEnd
+ 1);
507 bool CodeGenInstruction::isOperandAPointer(unsigned i
) const {
508 if (DagInit
*ConstraintList
= TheDef
->getValueAsDag("InOperandList")) {
509 if (i
< ConstraintList
->getNumArgs()) {
510 if (DefInit
*Constraint
= dyn_cast
<DefInit
>(ConstraintList
->getArg(i
))) {
511 return Constraint
->getDef()->isSubClassOf("TypedOperand") &&
512 Constraint
->getDef()->getValueAsBit("IsPointer");
519 //===----------------------------------------------------------------------===//
520 /// CodeGenInstAlias Implementation
521 //===----------------------------------------------------------------------===//
523 /// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
524 /// constructor. It checks if an argument in an InstAlias pattern matches
525 /// the corresponding operand of the instruction. It returns true on a
526 /// successful match, with ResOp set to the result operand to be used.
527 bool CodeGenInstAlias::tryAliasOpMatch(DagInit
*Result
, unsigned AliasOpNo
,
528 Record
*InstOpRec
, bool hasSubOps
,
529 ArrayRef
<SMLoc
> Loc
, CodeGenTarget
&T
,
530 ResultOperand
&ResOp
) {
531 Init
*Arg
= Result
->getArg(AliasOpNo
);
532 DefInit
*ADI
= dyn_cast
<DefInit
>(Arg
);
533 Record
*ResultRecord
= ADI
? ADI
->getDef() : nullptr;
535 if (ADI
&& ADI
->getDef() == InstOpRec
) {
536 // If the operand is a record, it must have a name, and the record type
537 // must match up with the instruction's argument type.
538 if (!Result
->getArgName(AliasOpNo
))
539 PrintFatalError(Loc
, "result argument #" + Twine(AliasOpNo
) +
540 " must have a name!");
541 ResOp
= ResultOperand(Result
->getArgNameStr(AliasOpNo
), ResultRecord
);
545 // For register operands, the source register class can be a subclass
546 // of the instruction register class, not just an exact match.
547 if (InstOpRec
->isSubClassOf("RegisterOperand"))
548 InstOpRec
= InstOpRec
->getValueAsDef("RegClass");
550 if (ADI
&& ADI
->getDef()->isSubClassOf("RegisterOperand"))
551 ADI
= ADI
->getDef()->getValueAsDef("RegClass")->getDefInit();
553 if (ADI
&& ADI
->getDef()->isSubClassOf("RegisterClass")) {
554 if (!InstOpRec
->isSubClassOf("RegisterClass"))
556 if (!T
.getRegisterClass(InstOpRec
)
557 .hasSubClass(&T
.getRegisterClass(ADI
->getDef())))
559 ResOp
= ResultOperand(Result
->getArgNameStr(AliasOpNo
), ResultRecord
);
563 // Handle explicit registers.
564 if (ADI
&& ADI
->getDef()->isSubClassOf("Register")) {
565 if (InstOpRec
->isSubClassOf("OptionalDefOperand")) {
566 DagInit
*DI
= InstOpRec
->getValueAsDag("MIOperandInfo");
567 // The operand info should only have a single (register) entry. We
568 // want the register class of it.
569 InstOpRec
= cast
<DefInit
>(DI
->getArg(0))->getDef();
572 if (!InstOpRec
->isSubClassOf("RegisterClass"))
575 if (!T
.getRegisterClass(InstOpRec
)
576 .contains(T
.getRegBank().getReg(ADI
->getDef())))
577 PrintFatalError(Loc
, "fixed register " + ADI
->getDef()->getName() +
578 " is not a member of the " + InstOpRec
->getName() +
581 if (Result
->getArgName(AliasOpNo
))
582 PrintFatalError(Loc
, "result fixed register argument must "
585 ResOp
= ResultOperand(ResultRecord
);
589 // Handle "zero_reg" for optional def operands.
590 if (ADI
&& ADI
->getDef()->getName() == "zero_reg") {
592 // Check if this is an optional def.
593 // Tied operands where the source is a sub-operand of a complex operand
594 // need to represent both operands in the alias destination instruction.
595 // Allow zero_reg for the tied portion. This can and should go away once
596 // the MC representation of things doesn't use tied operands at all.
597 //if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
598 // throw TGError(Loc, "reg0 used for result that is not an "
599 // "OptionalDefOperand!");
601 ResOp
= ResultOperand(static_cast<Record
*>(nullptr));
606 if (IntInit
*II
= dyn_cast
<IntInit
>(Arg
)) {
607 if (hasSubOps
|| !InstOpRec
->isSubClassOf("Operand"))
609 // Integer arguments can't have names.
610 if (Result
->getArgName(AliasOpNo
))
611 PrintFatalError(Loc
, "result argument #" + Twine(AliasOpNo
) +
612 " must not have a name!");
613 ResOp
= ResultOperand(II
->getValue());
617 // Bits<n> (also used for 0bxx literals)
618 if (BitsInit
*BI
= dyn_cast
<BitsInit
>(Arg
)) {
619 if (hasSubOps
|| !InstOpRec
->isSubClassOf("Operand"))
621 if (!BI
->isComplete())
623 // Convert the bits init to an integer and use that for the result.
625 dyn_cast_or_null
<IntInit
>(BI
->convertInitializerTo(IntRecTy::get()));
628 ResOp
= ResultOperand(II
->getValue());
632 // If both are Operands with the same MVT, allow the conversion. It's
633 // up to the user to make sure the values are appropriate, just like
635 if (InstOpRec
->isSubClassOf("Operand") && ADI
&&
636 ADI
->getDef()->isSubClassOf("Operand")) {
637 // FIXME: What other attributes should we check here? Identical
638 // MIOperandInfo perhaps?
639 if (InstOpRec
->getValueInit("Type") != ADI
->getDef()->getValueInit("Type"))
641 ResOp
= ResultOperand(Result
->getArgNameStr(AliasOpNo
), ADI
->getDef());
648 unsigned CodeGenInstAlias::ResultOperand::getMINumOperands() const {
652 Record
*Rec
= getRecord();
653 if (!Rec
->isSubClassOf("Operand"))
656 DagInit
*MIOpInfo
= Rec
->getValueAsDag("MIOperandInfo");
657 if (MIOpInfo
->getNumArgs() == 0) {
658 // Unspecified, so it defaults to 1
662 return MIOpInfo
->getNumArgs();
665 CodeGenInstAlias::CodeGenInstAlias(Record
*R
, CodeGenTarget
&T
)
667 Result
= R
->getValueAsDag("ResultInst");
668 AsmString
= R
->getValueAsString("AsmString");
671 // Verify that the root of the result is an instruction.
672 DefInit
*DI
= dyn_cast
<DefInit
>(Result
->getOperator());
673 if (!DI
|| !DI
->getDef()->isSubClassOf("Instruction"))
674 PrintFatalError(R
->getLoc(),
675 "result of inst alias should be an instruction");
677 ResultInst
= &T
.getInstruction(DI
->getDef());
679 // NameClass - If argument names are repeated, we need to verify they have
681 StringMap
<Record
*> NameClass
;
682 for (unsigned i
= 0, e
= Result
->getNumArgs(); i
!= e
; ++i
) {
683 DefInit
*ADI
= dyn_cast
<DefInit
>(Result
->getArg(i
));
684 if (!ADI
|| !Result
->getArgName(i
))
686 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
687 // $foo can exist multiple times in the result list, but it must have the
689 Record
*&Entry
= NameClass
[Result
->getArgNameStr(i
)];
690 if (Entry
&& Entry
!= ADI
->getDef())
691 PrintFatalError(R
->getLoc(), "result value $" + Result
->getArgNameStr(i
) +
692 " is both " + Entry
->getName() + " and " +
693 ADI
->getDef()->getName() + "!");
694 Entry
= ADI
->getDef();
697 // Decode and validate the arguments of the result.
698 unsigned AliasOpNo
= 0;
699 for (unsigned i
= 0, e
= ResultInst
->Operands
.size(); i
!= e
; ++i
) {
701 // Tied registers don't have an entry in the result dag unless they're part
702 // of a complex operand, in which case we include them anyways, as we
703 // don't have any other way to specify the whole operand.
704 if (ResultInst
->Operands
[i
].MINumOperands
== 1 &&
705 ResultInst
->Operands
[i
].getTiedRegister() != -1) {
706 // Tied operands of different RegisterClass should be explicit within an
707 // instruction's syntax and so cannot be skipped.
708 int TiedOpNum
= ResultInst
->Operands
[i
].getTiedRegister();
709 if (ResultInst
->Operands
[i
].Rec
->getName() ==
710 ResultInst
->Operands
[TiedOpNum
].Rec
->getName())
714 if (AliasOpNo
>= Result
->getNumArgs())
715 PrintFatalError(R
->getLoc(), "not enough arguments for instruction!");
717 Record
*InstOpRec
= ResultInst
->Operands
[i
].Rec
;
718 unsigned NumSubOps
= ResultInst
->Operands
[i
].MINumOperands
;
719 ResultOperand
ResOp(static_cast<int64_t>(0));
720 if (tryAliasOpMatch(Result
, AliasOpNo
, InstOpRec
, (NumSubOps
> 1),
721 R
->getLoc(), T
, ResOp
)) {
722 // If this is a simple operand, or a complex operand with a custom match
723 // class, then we can match is verbatim.
724 if (NumSubOps
== 1 ||
725 (InstOpRec
->getValue("ParserMatchClass") &&
726 InstOpRec
->getValueAsDef("ParserMatchClass")
727 ->getValueAsString("Name") != "Imm")) {
728 ResultOperands
.push_back(ResOp
);
729 ResultInstOperandIndex
.push_back(std::make_pair(i
, -1));
732 // Otherwise, we need to match each of the suboperands individually.
734 DagInit
*MIOI
= ResultInst
->Operands
[i
].MIOperandInfo
;
735 for (unsigned SubOp
= 0; SubOp
!= NumSubOps
; ++SubOp
) {
736 Record
*SubRec
= cast
<DefInit
>(MIOI
->getArg(SubOp
))->getDef();
738 // Take care to instantiate each of the suboperands with the correct
739 // nomenclature: $foo.bar
740 ResultOperands
.emplace_back(
741 Result
->getArgName(AliasOpNo
)->getAsUnquotedString() + "." +
742 MIOI
->getArgName(SubOp
)->getAsUnquotedString(), SubRec
);
743 ResultInstOperandIndex
.push_back(std::make_pair(i
, SubOp
));
750 // If the argument did not match the instruction operand, and the operand
751 // is composed of multiple suboperands, try matching the suboperands.
753 DagInit
*MIOI
= ResultInst
->Operands
[i
].MIOperandInfo
;
754 for (unsigned SubOp
= 0; SubOp
!= NumSubOps
; ++SubOp
) {
755 if (AliasOpNo
>= Result
->getNumArgs())
756 PrintFatalError(R
->getLoc(), "not enough arguments for instruction!");
757 Record
*SubRec
= cast
<DefInit
>(MIOI
->getArg(SubOp
))->getDef();
758 if (tryAliasOpMatch(Result
, AliasOpNo
, SubRec
, false,
759 R
->getLoc(), T
, ResOp
)) {
760 ResultOperands
.push_back(ResOp
);
761 ResultInstOperandIndex
.push_back(std::make_pair(i
, SubOp
));
764 PrintFatalError(R
->getLoc(), "result argument #" + Twine(AliasOpNo
) +
765 " does not match instruction operand class " +
766 (SubOp
== 0 ? InstOpRec
->getName() :SubRec
->getName()));
771 PrintFatalError(R
->getLoc(), "result argument #" + Twine(AliasOpNo
) +
772 " does not match instruction operand class " +
773 InstOpRec
->getName());
776 if (AliasOpNo
!= Result
->getNumArgs())
777 PrintFatalError(R
->getLoc(), "too many operands for instruction!");