1*fe013be4SDimitry Andric //===- CodeGenInstAlias.cpp - CodeGen InstAlias Class Wrapper -------------===//
2*fe013be4SDimitry Andric //
3*fe013be4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*fe013be4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*fe013be4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*fe013be4SDimitry Andric //
7*fe013be4SDimitry Andric //===----------------------------------------------------------------------===//
8*fe013be4SDimitry Andric //
9*fe013be4SDimitry Andric // This file implements the CodeGenInstAlias class.
10*fe013be4SDimitry Andric //
11*fe013be4SDimitry Andric //===----------------------------------------------------------------------===//
12*fe013be4SDimitry Andric 
13*fe013be4SDimitry Andric #include "CodeGenInstAlias.h"
14*fe013be4SDimitry Andric #include "CodeGenInstruction.h"
15*fe013be4SDimitry Andric #include "CodeGenRegisters.h"
16*fe013be4SDimitry Andric #include "CodeGenTarget.h"
17*fe013be4SDimitry Andric #include "llvm/ADT/StringMap.h"
18*fe013be4SDimitry Andric #include "llvm/TableGen/Error.h"
19*fe013be4SDimitry Andric #include "llvm/TableGen/Record.h"
20*fe013be4SDimitry Andric 
21*fe013be4SDimitry Andric using namespace llvm;
22*fe013be4SDimitry Andric 
23*fe013be4SDimitry Andric /// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
24*fe013be4SDimitry Andric /// constructor.  It checks if an argument in an InstAlias pattern matches
25*fe013be4SDimitry Andric /// the corresponding operand of the instruction.  It returns true on a
26*fe013be4SDimitry Andric /// successful match, with ResOp set to the result operand to be used.
tryAliasOpMatch(DagInit * Result,unsigned AliasOpNo,Record * InstOpRec,bool hasSubOps,ArrayRef<SMLoc> Loc,CodeGenTarget & T,ResultOperand & ResOp)27*fe013be4SDimitry Andric bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
28*fe013be4SDimitry Andric                                        Record *InstOpRec, bool hasSubOps,
29*fe013be4SDimitry Andric                                        ArrayRef<SMLoc> Loc, CodeGenTarget &T,
30*fe013be4SDimitry Andric                                        ResultOperand &ResOp) {
31*fe013be4SDimitry Andric   Init *Arg = Result->getArg(AliasOpNo);
32*fe013be4SDimitry Andric   DefInit *ADI = dyn_cast<DefInit>(Arg);
33*fe013be4SDimitry Andric   Record *ResultRecord = ADI ? ADI->getDef() : nullptr;
34*fe013be4SDimitry Andric 
35*fe013be4SDimitry Andric   if (ADI && ADI->getDef() == InstOpRec) {
36*fe013be4SDimitry Andric     // If the operand is a record, it must have a name, and the record type
37*fe013be4SDimitry Andric     // must match up with the instruction's argument type.
38*fe013be4SDimitry Andric     if (!Result->getArgName(AliasOpNo))
39*fe013be4SDimitry Andric       PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
40*fe013be4SDimitry Andric                                " must have a name!");
41*fe013be4SDimitry Andric     ResOp = ResultOperand(std::string(Result->getArgNameStr(AliasOpNo)),
42*fe013be4SDimitry Andric                           ResultRecord);
43*fe013be4SDimitry Andric     return true;
44*fe013be4SDimitry Andric   }
45*fe013be4SDimitry Andric 
46*fe013be4SDimitry Andric   // For register operands, the source register class can be a subclass
47*fe013be4SDimitry Andric   // of the instruction register class, not just an exact match.
48*fe013be4SDimitry Andric   if (InstOpRec->isSubClassOf("RegisterOperand"))
49*fe013be4SDimitry Andric     InstOpRec = InstOpRec->getValueAsDef("RegClass");
50*fe013be4SDimitry Andric 
51*fe013be4SDimitry Andric   if (ADI && ADI->getDef()->isSubClassOf("RegisterOperand"))
52*fe013be4SDimitry Andric     ADI = ADI->getDef()->getValueAsDef("RegClass")->getDefInit();
53*fe013be4SDimitry Andric 
54*fe013be4SDimitry Andric   if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) {
55*fe013be4SDimitry Andric     if (!InstOpRec->isSubClassOf("RegisterClass"))
56*fe013be4SDimitry Andric       return false;
57*fe013be4SDimitry Andric     if (!T.getRegisterClass(InstOpRec).hasSubClass(
58*fe013be4SDimitry Andric             &T.getRegisterClass(ADI->getDef())))
59*fe013be4SDimitry Andric       return false;
60*fe013be4SDimitry Andric     ResOp = ResultOperand(std::string(Result->getArgNameStr(AliasOpNo)),
61*fe013be4SDimitry Andric                           ResultRecord);
62*fe013be4SDimitry Andric     return true;
63*fe013be4SDimitry Andric   }
64*fe013be4SDimitry Andric 
65*fe013be4SDimitry Andric   // Handle explicit registers.
66*fe013be4SDimitry Andric   if (ADI && ADI->getDef()->isSubClassOf("Register")) {
67*fe013be4SDimitry Andric     if (InstOpRec->isSubClassOf("OptionalDefOperand")) {
68*fe013be4SDimitry Andric       DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo");
69*fe013be4SDimitry Andric       // The operand info should only have a single (register) entry. We
70*fe013be4SDimitry Andric       // want the register class of it.
71*fe013be4SDimitry Andric       InstOpRec = cast<DefInit>(DI->getArg(0))->getDef();
72*fe013be4SDimitry Andric     }
73*fe013be4SDimitry Andric 
74*fe013be4SDimitry Andric     if (!InstOpRec->isSubClassOf("RegisterClass"))
75*fe013be4SDimitry Andric       return false;
76*fe013be4SDimitry Andric 
77*fe013be4SDimitry Andric     if (!T.getRegisterClass(InstOpRec).contains(
78*fe013be4SDimitry Andric             T.getRegBank().getReg(ADI->getDef())))
79*fe013be4SDimitry Andric       PrintFatalError(Loc, "fixed register " + ADI->getDef()->getName() +
80*fe013be4SDimitry Andric                                " is not a member of the " +
81*fe013be4SDimitry Andric                                InstOpRec->getName() + " register class!");
82*fe013be4SDimitry Andric 
83*fe013be4SDimitry Andric     if (Result->getArgName(AliasOpNo))
84*fe013be4SDimitry Andric       PrintFatalError(Loc, "result fixed register argument must "
85*fe013be4SDimitry Andric                            "not have a name!");
86*fe013be4SDimitry Andric 
87*fe013be4SDimitry Andric     ResOp = ResultOperand(ResultRecord);
88*fe013be4SDimitry Andric     return true;
89*fe013be4SDimitry Andric   }
90*fe013be4SDimitry Andric 
91*fe013be4SDimitry Andric   // Handle "zero_reg" for optional def operands.
92*fe013be4SDimitry Andric   if (ADI && ADI->getDef()->getName() == "zero_reg") {
93*fe013be4SDimitry Andric 
94*fe013be4SDimitry Andric     // Check if this is an optional def.
95*fe013be4SDimitry Andric     // Tied operands where the source is a sub-operand of a complex operand
96*fe013be4SDimitry Andric     // need to represent both operands in the alias destination instruction.
97*fe013be4SDimitry Andric     // Allow zero_reg for the tied portion. This can and should go away once
98*fe013be4SDimitry Andric     // the MC representation of things doesn't use tied operands at all.
99*fe013be4SDimitry Andric     // if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
100*fe013be4SDimitry Andric     //  throw TGError(Loc, "reg0 used for result that is not an "
101*fe013be4SDimitry Andric     //                "OptionalDefOperand!");
102*fe013be4SDimitry Andric 
103*fe013be4SDimitry Andric     ResOp = ResultOperand(static_cast<Record *>(nullptr));
104*fe013be4SDimitry Andric     return true;
105*fe013be4SDimitry Andric   }
106*fe013be4SDimitry Andric 
107*fe013be4SDimitry Andric   // Literal integers.
108*fe013be4SDimitry Andric   if (IntInit *II = dyn_cast<IntInit>(Arg)) {
109*fe013be4SDimitry Andric     if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
110*fe013be4SDimitry Andric       return false;
111*fe013be4SDimitry Andric     // Integer arguments can't have names.
112*fe013be4SDimitry Andric     if (Result->getArgName(AliasOpNo))
113*fe013be4SDimitry Andric       PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
114*fe013be4SDimitry Andric                                " must not have a name!");
115*fe013be4SDimitry Andric     ResOp = ResultOperand(II->getValue());
116*fe013be4SDimitry Andric     return true;
117*fe013be4SDimitry Andric   }
118*fe013be4SDimitry Andric 
119*fe013be4SDimitry Andric   // Bits<n> (also used for 0bxx literals)
120*fe013be4SDimitry Andric   if (BitsInit *BI = dyn_cast<BitsInit>(Arg)) {
121*fe013be4SDimitry Andric     if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
122*fe013be4SDimitry Andric       return false;
123*fe013be4SDimitry Andric     if (!BI->isComplete())
124*fe013be4SDimitry Andric       return false;
125*fe013be4SDimitry Andric     // Convert the bits init to an integer and use that for the result.
126*fe013be4SDimitry Andric     IntInit *II = dyn_cast_or_null<IntInit>(
127*fe013be4SDimitry Andric         BI->convertInitializerTo(IntRecTy::get(BI->getRecordKeeper())));
128*fe013be4SDimitry Andric     if (!II)
129*fe013be4SDimitry Andric       return false;
130*fe013be4SDimitry Andric     ResOp = ResultOperand(II->getValue());
131*fe013be4SDimitry Andric     return true;
132*fe013be4SDimitry Andric   }
133*fe013be4SDimitry Andric 
134*fe013be4SDimitry Andric   // If both are Operands with the same MVT, allow the conversion. It's
135*fe013be4SDimitry Andric   // up to the user to make sure the values are appropriate, just like
136*fe013be4SDimitry Andric   // for isel Pat's.
137*fe013be4SDimitry Andric   if (InstOpRec->isSubClassOf("Operand") && ADI &&
138*fe013be4SDimitry Andric       ADI->getDef()->isSubClassOf("Operand")) {
139*fe013be4SDimitry Andric     // FIXME: What other attributes should we check here? Identical
140*fe013be4SDimitry Andric     // MIOperandInfo perhaps?
141*fe013be4SDimitry Andric     if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type"))
142*fe013be4SDimitry Andric       return false;
143*fe013be4SDimitry Andric     ResOp = ResultOperand(std::string(Result->getArgNameStr(AliasOpNo)),
144*fe013be4SDimitry Andric                           ADI->getDef());
145*fe013be4SDimitry Andric     return true;
146*fe013be4SDimitry Andric   }
147*fe013be4SDimitry Andric 
148*fe013be4SDimitry Andric   return false;
149*fe013be4SDimitry Andric }
150*fe013be4SDimitry Andric 
getMINumOperands() const151*fe013be4SDimitry Andric unsigned CodeGenInstAlias::ResultOperand::getMINumOperands() const {
152*fe013be4SDimitry Andric   if (!isRecord())
153*fe013be4SDimitry Andric     return 1;
154*fe013be4SDimitry Andric 
155*fe013be4SDimitry Andric   Record *Rec = getRecord();
156*fe013be4SDimitry Andric   if (!Rec->isSubClassOf("Operand"))
157*fe013be4SDimitry Andric     return 1;
158*fe013be4SDimitry Andric 
159*fe013be4SDimitry Andric   DagInit *MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
160*fe013be4SDimitry Andric   if (MIOpInfo->getNumArgs() == 0) {
161*fe013be4SDimitry Andric     // Unspecified, so it defaults to 1
162*fe013be4SDimitry Andric     return 1;
163*fe013be4SDimitry Andric   }
164*fe013be4SDimitry Andric 
165*fe013be4SDimitry Andric   return MIOpInfo->getNumArgs();
166*fe013be4SDimitry Andric }
167*fe013be4SDimitry Andric 
CodeGenInstAlias(Record * R,CodeGenTarget & T)168*fe013be4SDimitry Andric CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T) : TheDef(R) {
169*fe013be4SDimitry Andric   Result = R->getValueAsDag("ResultInst");
170*fe013be4SDimitry Andric   AsmString = std::string(R->getValueAsString("AsmString"));
171*fe013be4SDimitry Andric 
172*fe013be4SDimitry Andric   // Verify that the root of the result is an instruction.
173*fe013be4SDimitry Andric   DefInit *DI = dyn_cast<DefInit>(Result->getOperator());
174*fe013be4SDimitry Andric   if (!DI || !DI->getDef()->isSubClassOf("Instruction"))
175*fe013be4SDimitry Andric     PrintFatalError(R->getLoc(),
176*fe013be4SDimitry Andric                     "result of inst alias should be an instruction");
177*fe013be4SDimitry Andric 
178*fe013be4SDimitry Andric   ResultInst = &T.getInstruction(DI->getDef());
179*fe013be4SDimitry Andric 
180*fe013be4SDimitry Andric   // NameClass - If argument names are repeated, we need to verify they have
181*fe013be4SDimitry Andric   // the same class.
182*fe013be4SDimitry Andric   StringMap<Record *> NameClass;
183*fe013be4SDimitry Andric   for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
184*fe013be4SDimitry Andric     DefInit *ADI = dyn_cast<DefInit>(Result->getArg(i));
185*fe013be4SDimitry Andric     if (!ADI || !Result->getArgName(i))
186*fe013be4SDimitry Andric       continue;
187*fe013be4SDimitry Andric     // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
188*fe013be4SDimitry Andric     // $foo can exist multiple times in the result list, but it must have the
189*fe013be4SDimitry Andric     // same type.
190*fe013be4SDimitry Andric     Record *&Entry = NameClass[Result->getArgNameStr(i)];
191*fe013be4SDimitry Andric     if (Entry && Entry != ADI->getDef())
192*fe013be4SDimitry Andric       PrintFatalError(R->getLoc(), "result value $" + Result->getArgNameStr(i) +
193*fe013be4SDimitry Andric                                        " is both " + Entry->getName() +
194*fe013be4SDimitry Andric                                        " and " + ADI->getDef()->getName() +
195*fe013be4SDimitry Andric                                        "!");
196*fe013be4SDimitry Andric     Entry = ADI->getDef();
197*fe013be4SDimitry Andric   }
198*fe013be4SDimitry Andric 
199*fe013be4SDimitry Andric   // Decode and validate the arguments of the result.
200*fe013be4SDimitry Andric   unsigned AliasOpNo = 0;
201*fe013be4SDimitry Andric   for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
202*fe013be4SDimitry Andric 
203*fe013be4SDimitry Andric     // Tied registers don't have an entry in the result dag unless they're part
204*fe013be4SDimitry Andric     // of a complex operand, in which case we include them anyways, as we
205*fe013be4SDimitry Andric     // don't have any other way to specify the whole operand.
206*fe013be4SDimitry Andric     if (ResultInst->Operands[i].MINumOperands == 1 &&
207*fe013be4SDimitry Andric         ResultInst->Operands[i].getTiedRegister() != -1) {
208*fe013be4SDimitry Andric       // Tied operands of different RegisterClass should be explicit within an
209*fe013be4SDimitry Andric       // instruction's syntax and so cannot be skipped.
210*fe013be4SDimitry Andric       int TiedOpNum = ResultInst->Operands[i].getTiedRegister();
211*fe013be4SDimitry Andric       if (ResultInst->Operands[i].Rec->getName() ==
212*fe013be4SDimitry Andric           ResultInst->Operands[TiedOpNum].Rec->getName())
213*fe013be4SDimitry Andric         continue;
214*fe013be4SDimitry Andric     }
215*fe013be4SDimitry Andric 
216*fe013be4SDimitry Andric     if (AliasOpNo >= Result->getNumArgs())
217*fe013be4SDimitry Andric       PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
218*fe013be4SDimitry Andric 
219*fe013be4SDimitry Andric     Record *InstOpRec = ResultInst->Operands[i].Rec;
220*fe013be4SDimitry Andric     unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
221*fe013be4SDimitry Andric     ResultOperand ResOp(static_cast<int64_t>(0));
222*fe013be4SDimitry Andric     if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
223*fe013be4SDimitry Andric                         R->getLoc(), T, ResOp)) {
224*fe013be4SDimitry Andric       // If this is a simple operand, or a complex operand with a custom match
225*fe013be4SDimitry Andric       // class, then we can match is verbatim.
226*fe013be4SDimitry Andric       if (NumSubOps == 1 || (InstOpRec->getValue("ParserMatchClass") &&
227*fe013be4SDimitry Andric                              InstOpRec->getValueAsDef("ParserMatchClass")
228*fe013be4SDimitry Andric                                      ->getValueAsString("Name") != "Imm")) {
229*fe013be4SDimitry Andric         ResultOperands.push_back(ResOp);
230*fe013be4SDimitry Andric         ResultInstOperandIndex.push_back(std::make_pair(i, -1));
231*fe013be4SDimitry Andric         ++AliasOpNo;
232*fe013be4SDimitry Andric 
233*fe013be4SDimitry Andric         // Otherwise, we need to match each of the suboperands individually.
234*fe013be4SDimitry Andric       } else {
235*fe013be4SDimitry Andric         DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
236*fe013be4SDimitry Andric         for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
237*fe013be4SDimitry Andric           Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
238*fe013be4SDimitry Andric 
239*fe013be4SDimitry Andric           // Take care to instantiate each of the suboperands with the correct
240*fe013be4SDimitry Andric           // nomenclature: $foo.bar
241*fe013be4SDimitry Andric           ResultOperands.emplace_back(
242*fe013be4SDimitry Andric               Result->getArgName(AliasOpNo)->getAsUnquotedString() + "." +
243*fe013be4SDimitry Andric                   MIOI->getArgName(SubOp)->getAsUnquotedString(),
244*fe013be4SDimitry Andric               SubRec);
245*fe013be4SDimitry Andric           ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
246*fe013be4SDimitry Andric         }
247*fe013be4SDimitry Andric         ++AliasOpNo;
248*fe013be4SDimitry Andric       }
249*fe013be4SDimitry Andric       continue;
250*fe013be4SDimitry Andric     }
251*fe013be4SDimitry Andric 
252*fe013be4SDimitry Andric     // If the argument did not match the instruction operand, and the operand
253*fe013be4SDimitry Andric     // is composed of multiple suboperands, try matching the suboperands.
254*fe013be4SDimitry Andric     if (NumSubOps > 1) {
255*fe013be4SDimitry Andric       DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
256*fe013be4SDimitry Andric       for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
257*fe013be4SDimitry Andric         if (AliasOpNo >= Result->getNumArgs())
258*fe013be4SDimitry Andric           PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
259*fe013be4SDimitry Andric         Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
260*fe013be4SDimitry Andric         if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false, R->getLoc(), T,
261*fe013be4SDimitry Andric                             ResOp)) {
262*fe013be4SDimitry Andric           ResultOperands.push_back(ResOp);
263*fe013be4SDimitry Andric           ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
264*fe013be4SDimitry Andric           ++AliasOpNo;
265*fe013be4SDimitry Andric         } else {
266*fe013be4SDimitry Andric           PrintFatalError(
267*fe013be4SDimitry Andric               R->getLoc(),
268*fe013be4SDimitry Andric               "result argument #" + Twine(AliasOpNo) +
269*fe013be4SDimitry Andric                   " does not match instruction operand class " +
270*fe013be4SDimitry Andric                   (SubOp == 0 ? InstOpRec->getName() : SubRec->getName()));
271*fe013be4SDimitry Andric         }
272*fe013be4SDimitry Andric       }
273*fe013be4SDimitry Andric       continue;
274*fe013be4SDimitry Andric     }
275*fe013be4SDimitry Andric     PrintFatalError(R->getLoc(),
276*fe013be4SDimitry Andric                     "result argument #" + Twine(AliasOpNo) +
277*fe013be4SDimitry Andric                         " does not match instruction operand class " +
278*fe013be4SDimitry Andric                         InstOpRec->getName());
279*fe013be4SDimitry Andric   }
280*fe013be4SDimitry Andric 
281*fe013be4SDimitry Andric   if (AliasOpNo != Result->getNumArgs())
282*fe013be4SDimitry Andric     PrintFatalError(R->getLoc(), "too many operands for instruction!");
283*fe013be4SDimitry Andric }
284