blob: 151295fc95ec02024a07a2834135b91b7d87df09 [file] [log] [blame]
Chris Lattner803a5f62004-08-01 04:04:35 +00001//===- CodeGenTarget.cpp - CodeGen Target Class Wrapper ---------*- C++ -*-===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell01d45822003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell01d45822003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattner45872072003-08-07 05:38:11 +00009//
Chris Lattner803a5f62004-08-01 04:04:35 +000010// This class wrap target description classes used by the various code
Chris Lattner45872072003-08-07 05:38:11 +000011// generation TableGen backends. This makes it easier to access the data and
12// provides a single place that needs to check it for validity. All of these
13// classes throw exceptions on error conditions.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattner803a5f62004-08-01 04:04:35 +000017#include "CodeGenTarget.h"
Chris Lattner43fbbc32006-03-24 19:49:31 +000018#include "CodeGenIntrinsics.h"
Chris Lattner45872072003-08-07 05:38:11 +000019#include "Record.h"
Chris Lattner560a79f2004-10-03 19:34:31 +000020#include "llvm/ADT/StringExtras.h"
21#include "llvm/Support/CommandLine.h"
Bill Wendlingf5da1332006-12-07 22:21:48 +000022#include "llvm/Support/Streams.h"
Chris Lattner5d7d3db2005-09-14 21:05:02 +000023#include <set>
Chris Lattner75ee2eb2005-10-14 03:54:49 +000024#include <algorithm>
Chris Lattner2082ebe2004-08-01 03:55:39 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Chris Lattner560a79f2004-10-03 19:34:31 +000027static cl::opt<unsigned>
28AsmWriterNum("asmwriternum", cl::init(0),
29 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
30
Chris Lattner45872072003-08-07 05:38:11 +000031/// getValueType - Return the MCV::ValueType that the specified TableGen record
32/// corresponds to.
Chris Lattner8850a1b2006-03-27 22:48:18 +000033MVT::ValueType llvm::getValueType(Record *Rec, const CodeGenTarget *CGT) {
Evan Cheng2618d072006-05-17 20:37:59 +000034 return (MVT::ValueType)Rec->getValueAsInt("Value");
Chris Lattner45872072003-08-07 05:38:11 +000035}
36
Chris Lattner2082ebe2004-08-01 03:55:39 +000037std::string llvm::getName(MVT::ValueType T) {
Chris Lattner45872072003-08-07 05:38:11 +000038 switch (T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000039 case MVT::Other: return "UNKNOWN";
Evan Chengd7c2c862006-06-15 00:16:37 +000040 case MVT::i1: return "MVT::i1";
41 case MVT::i8: return "MVT::i8";
42 case MVT::i16: return "MVT::i16";
43 case MVT::i32: return "MVT::i32";
44 case MVT::i64: return "MVT::i64";
45 case MVT::i128: return "MVT::i128";
Reid Spencerc4de3de2007-04-01 07:20:02 +000046 case MVT::iAny: return "MVT::iAny";
Evan Chengd7c2c862006-06-15 00:16:37 +000047 case MVT::f32: return "MVT::f32";
48 case MVT::f64: return "MVT::f64";
49 case MVT::f80: return "MVT::f80";
50 case MVT::f128: return "MVT::f128";
51 case MVT::Flag: return "MVT::Flag";
52 case MVT::isVoid:return "MVT::void";
53 case MVT::v8i8: return "MVT::v8i8";
54 case MVT::v4i16: return "MVT::v4i16";
55 case MVT::v2i32: return "MVT::v2i32";
Bill Wendlingeebc8a12007-03-26 07:53:08 +000056 case MVT::v1i64: return "MVT::v1i64";
Evan Chengd7c2c862006-06-15 00:16:37 +000057 case MVT::v16i8: return "MVT::v16i8";
58 case MVT::v8i16: return "MVT::v8i16";
59 case MVT::v4i32: return "MVT::v4i32";
60 case MVT::v2i64: return "MVT::v2i64";
61 case MVT::v2f32: return "MVT::v2f32";
62 case MVT::v4f32: return "MVT::v4f32";
63 case MVT::v2f64: return "MVT::v2f64";
Evan Cheng6b125162006-05-17 20:55:51 +000064 case MVT::iPTR: return "TLI.getPointerTy()";
Chris Lattnerd3464c12003-08-07 23:15:21 +000065 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000066 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000067}
68
Chris Lattner2082ebe2004-08-01 03:55:39 +000069std::string llvm::getEnumName(MVT::ValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000070 switch (T) {
Evan Cheng2618d072006-05-17 20:37:59 +000071 case MVT::Other: return "MVT::Other";
72 case MVT::i1: return "MVT::i1";
73 case MVT::i8: return "MVT::i8";
74 case MVT::i16: return "MVT::i16";
75 case MVT::i32: return "MVT::i32";
76 case MVT::i64: return "MVT::i64";
77 case MVT::i128: return "MVT::i128";
Reid Spencerc4de3de2007-04-01 07:20:02 +000078 case MVT::iAny: return "MVT::iAny";
Evan Cheng2618d072006-05-17 20:37:59 +000079 case MVT::f32: return "MVT::f32";
80 case MVT::f64: return "MVT::f64";
81 case MVT::f80: return "MVT::f80";
82 case MVT::f128: return "MVT::f128";
83 case MVT::Flag: return "MVT::Flag";
84 case MVT::isVoid:return "MVT::isVoid";
85 case MVT::v8i8: return "MVT::v8i8";
86 case MVT::v4i16: return "MVT::v4i16";
87 case MVT::v2i32: return "MVT::v2i32";
Bill Wendlingeebc8a12007-03-26 07:53:08 +000088 case MVT::v1i64: return "MVT::v1i64";
Evan Cheng2618d072006-05-17 20:37:59 +000089 case MVT::v16i8: return "MVT::v16i8";
90 case MVT::v8i16: return "MVT::v8i16";
91 case MVT::v4i32: return "MVT::v4i32";
92 case MVT::v2i64: return "MVT::v2i64";
93 case MVT::v2f32: return "MVT::v2f32";
94 case MVT::v4f32: return "MVT::v4f32";
95 case MVT::v2f64: return "MVT::v2f64";
Evan Cheng6b125162006-05-17 20:55:51 +000096 case MVT::iPTR: return "TLI.getPointerTy()";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000097 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
98 }
99}
100
Chris Lattnerd3464c12003-08-07 23:15:21 +0000101
Chris Lattner2082ebe2004-08-01 03:55:39 +0000102std::ostream &llvm::operator<<(std::ostream &OS, MVT::ValueType T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +0000103 return OS << getName(T);
Chris Lattner45872072003-08-07 05:38:11 +0000104}
105
106
Chris Lattner45872072003-08-07 05:38:11 +0000107/// getTarget - Return the current instance of the Target class.
108///
Evan Cheng2618d072006-05-17 20:37:59 +0000109CodeGenTarget::CodeGenTarget() {
Chris Lattner45872072003-08-07 05:38:11 +0000110 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000111 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000112 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000113 if (Targets.size() != 1)
114 throw std::string("ERROR: Multiple subclasses of Target defined!");
115 TargetRec = Targets[0];
Chris Lattner45872072003-08-07 05:38:11 +0000116}
117
118
119const std::string &CodeGenTarget::getName() const {
120 return TargetRec->getName();
121}
122
123Record *CodeGenTarget::getInstructionSet() const {
124 return TargetRec->getValueAsDef("InstructionSet");
125}
Brian Gaeked0fde302003-11-11 22:41:34 +0000126
Chris Lattner175580c2004-08-14 22:50:53 +0000127/// getAsmWriter - Return the AssemblyWriter definition for this target.
128///
129Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000130 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
131 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000132 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000133 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000134}
135
Chris Lattner26693112004-08-16 01:10:21 +0000136void CodeGenTarget::ReadRegisters() const {
137 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
138 if (Regs.empty())
139 throw std::string("No 'Register' subclasses defined!");
140
141 Registers.reserve(Regs.size());
142 Registers.assign(Regs.begin(), Regs.end());
143}
144
Chris Lattner7a680c62004-08-21 02:24:57 +0000145CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
146 DeclaredSpillSize = R->getValueAsInt("SpillSize");
147 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
148}
149
Chris Lattner26693112004-08-16 01:10:21 +0000150const std::string &CodeGenRegister::getName() const {
151 return TheDef->getName();
152}
153
Chris Lattner056afef2004-08-21 04:05:00 +0000154void CodeGenTarget::ReadRegisterClasses() const {
155 std::vector<Record*> RegClasses =
156 Records.getAllDerivedDefinitions("RegisterClass");
157 if (RegClasses.empty())
158 throw std::string("No 'RegisterClass' subclasses defined!");
159
160 RegisterClasses.reserve(RegClasses.size());
161 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
162}
163
Evan Cheng44a65fa2006-05-16 07:05:30 +0000164std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
165 std::vector<unsigned char> Result;
166 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
167 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
168 const CodeGenRegisterClass &RC = RegisterClasses[i];
169 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
170 if (R == RC.Elements[ei]) {
171 const std::vector<MVT::ValueType> &InVTs = RC.getValueTypes();
172 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
173 Result.push_back(InVTs[i]);
174 }
175 }
176 }
177 return Result;
178}
179
180
Chris Lattner056afef2004-08-21 04:05:00 +0000181CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000182 // Rename anonymous register classes.
183 if (R->getName().size() > 9 && R->getName()[9] == '.') {
184 static unsigned AnonCounter = 0;
185 R->setName("AnonRegClass_"+utostr(AnonCounter++));
186 }
187
Nate Begeman6510b222005-12-01 04:51:06 +0000188 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
189 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
190 Record *Type = TypeList[i];
191 if (!Type->isSubClassOf("ValueType"))
192 throw "RegTypes list member '" + Type->getName() +
193 "' does not derive from the ValueType class!";
194 VTs.push_back(getValueType(Type));
195 }
196 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000197
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000198 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
199 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
200 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000201 if (!Reg->isSubClassOf("Register"))
202 throw "Register Class member '" + Reg->getName() +
203 "' does not derive from the Register class!";
204 Elements.push_back(Reg);
205 }
Nate Begeman6510b222005-12-01 04:51:06 +0000206
207 // Allow targets to override the size in bits of the RegisterClass.
208 unsigned Size = R->getValueAsInt("Size");
209
210 Namespace = R->getValueAsString("Namespace");
211 SpillSize = Size ? Size : MVT::getSizeInBits(VTs[0]);
212 SpillAlignment = R->getValueAsInt("Alignment");
213 MethodBodies = R->getValueAsCode("MethodBodies");
214 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000215}
216
217const std::string &CodeGenRegisterClass::getName() const {
218 return TheDef->getName();
219}
220
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000221void CodeGenTarget::ReadLegalValueTypes() const {
222 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
223 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000224 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
225 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000226
227 // Remove duplicates.
228 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
229 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
230 LegalValueTypes.end()),
231 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000232}
Chris Lattner056afef2004-08-21 04:05:00 +0000233
Chris Lattner175580c2004-08-14 22:50:53 +0000234
Chris Lattnerec352402004-08-01 05:04:00 +0000235void CodeGenTarget::ReadInstructions() const {
236 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000237 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000238 throw std::string("No 'Instruction' subclasses defined!");
239
Chris Lattneraa77d772006-01-27 01:45:06 +0000240 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000241 std::string InstFormatName =
242 getAsmWriter()->getValueAsString("InstFormatName");
243
244 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
245 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
246 Instructions.insert(std::make_pair(Insts[i]->getName(),
247 CodeGenInstruction(Insts[i], AsmStr)));
248 }
Chris Lattnerec352402004-08-01 05:04:00 +0000249}
250
Chris Lattnerd6488672005-01-22 18:58:51 +0000251/// getInstructionsByEnumValue - Return all of the instructions defined by the
252/// target, ordered by their enum value.
253void CodeGenTarget::
254getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
255 &NumberedInstructions) {
Chris Lattneraa77d772006-01-27 01:45:06 +0000256 std::map<std::string, CodeGenInstruction>::const_iterator I;
257 I = getInstructions().find("PHI");
258 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
259 const CodeGenInstruction *PHI = &I->second;
260
261 I = getInstructions().find("INLINEASM");
262 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
263 const CodeGenInstruction *INLINEASM = &I->second;
264
Jim Laskeya683f9b2007-01-26 17:29:20 +0000265 I = getInstructions().find("LABEL");
266 if (I == Instructions.end()) throw "Could not find 'LABEL' instruction!";
267 const CodeGenInstruction *LABEL = &I->second;
268
Chris Lattnerd6488672005-01-22 18:58:51 +0000269 // Print out the rest of the instructions now.
Chris Lattnerd6488672005-01-22 18:58:51 +0000270 NumberedInstructions.push_back(PHI);
Chris Lattneraa77d772006-01-27 01:45:06 +0000271 NumberedInstructions.push_back(INLINEASM);
Jim Laskeya683f9b2007-01-26 17:29:20 +0000272 NumberedInstructions.push_back(LABEL);
Chris Lattnerd6488672005-01-22 18:58:51 +0000273 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
Jim Laskeya683f9b2007-01-26 17:29:20 +0000274 if (&II->second != PHI &&
275 &II->second != INLINEASM &&
276 &II->second != LABEL)
Chris Lattnerd6488672005-01-22 18:58:51 +0000277 NumberedInstructions.push_back(&II->second);
278}
279
280
Misha Brukman35e83cc2004-10-14 05:50:43 +0000281/// isLittleEndianEncoding - Return whether this target encodes its instruction
282/// in little-endian format, i.e. bits laid out in the order [0..n]
283///
284bool CodeGenTarget::isLittleEndianEncoding() const {
285 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
286}
287
Chris Lattner0bb75002006-11-15 02:38:17 +0000288
289
290static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
291 // FIXME: Only supports TIED_TO for now.
292 std::string::size_type pos = CStr.find_first_of('=');
Evan Chenge2ba8972006-11-01 00:27:05 +0000293 assert(pos != std::string::npos && "Unrecognized constraint");
Chris Lattner0bb75002006-11-15 02:38:17 +0000294 std::string Name = CStr.substr(0, pos);
Evan Chenge2ba8972006-11-01 00:27:05 +0000295
Evan Cheng4c2b7a32006-11-01 23:03:11 +0000296 // TIED_TO: $src1 = $dst
Chris Lattner0bb75002006-11-15 02:38:17 +0000297 std::string::size_type wpos = Name.find_first_of(" \t");
298 if (wpos == std::string::npos)
299 throw "Illegal format for tied-to constraint: '" + CStr + "'";
300 std::string DestOpName = Name.substr(0, wpos);
301 std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false);
Evan Chenge2ba8972006-11-01 00:27:05 +0000302
303 Name = CStr.substr(pos+1);
Chris Lattner0bb75002006-11-15 02:38:17 +0000304 wpos = Name.find_first_not_of(" \t");
305 if (wpos == std::string::npos)
306 throw "Illegal format for tied-to constraint: '" + CStr + "'";
307
308 std::pair<unsigned,unsigned> SrcOp =
309 I->ParseOperandName(Name.substr(wpos), false);
310 if (SrcOp > DestOp)
Evan Cheng4c2b7a32006-11-01 23:03:11 +0000311 throw "Illegal tied-to operand constraint '" + CStr + "'";
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000312
Chris Lattner0bb75002006-11-15 02:38:17 +0000313
314 unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
315 // Build the string for the operand.
316 std::string OpConstraint =
Evan Cheng05551222006-12-01 22:57:41 +0000317 "((" + utostr(FlatOpNo) + " << 16) | (1 << TOI::TIED_TO))";
Chris Lattner0bb75002006-11-15 02:38:17 +0000318
319
320 if (!I->OperandList[DestOp.first].Constraints[DestOp.second].empty())
321 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
322 I->OperandList[DestOp.first].Constraints[DestOp.second] = OpConstraint;
Evan Chenge2ba8972006-11-01 00:27:05 +0000323}
324
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000325static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) {
Chris Lattner0bb75002006-11-15 02:38:17 +0000326 // Make sure the constraints list for each operand is large enough to hold
327 // constraint info, even if none is present.
328 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
329 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
330
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000331 if (CStr.empty()) return;
332
Evan Chenge2ba8972006-11-01 00:27:05 +0000333 const std::string delims(",");
334 std::string::size_type bidx, eidx;
335
336 bidx = CStr.find_first_not_of(delims);
337 while (bidx != std::string::npos) {
338 eidx = CStr.find_first_of(delims, bidx);
339 if (eidx == std::string::npos)
340 eidx = CStr.length();
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000341
Chris Lattner0bb75002006-11-15 02:38:17 +0000342 ParseConstraint(CStr.substr(bidx, eidx), I);
Evan Chenge2ba8972006-11-01 00:27:05 +0000343 bidx = CStr.find_first_not_of(delims, eidx);
344 }
Evan Chenge2ba8972006-11-01 00:27:05 +0000345}
346
Chris Lattner175580c2004-08-14 22:50:53 +0000347CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
348 : TheDef(R), AsmString(AsmStr) {
Chris Lattnerec352402004-08-01 05:04:00 +0000349 Name = R->getValueAsString("Name");
350 Namespace = R->getValueAsString("Namespace");
Chris Lattnerec352402004-08-01 05:04:00 +0000351
Chris Lattnerec352402004-08-01 05:04:00 +0000352 isReturn = R->getValueAsBit("isReturn");
353 isBranch = R->getValueAsBit("isBranch");
354 isBarrier = R->getValueAsBit("isBarrier");
355 isCall = R->getValueAsBit("isCall");
Nate Begemancdd66b52004-09-28 21:01:45 +0000356 isLoad = R->getValueAsBit("isLoad");
357 isStore = R->getValueAsBit("isStore");
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000358 bool isTwoAddress = R->getValueAsBit("isTwoAddress");
Chris Lattnera818e922006-11-06 21:44:54 +0000359 isPredicated = false; // set below.
Chris Lattneraad75aa2005-01-02 02:29:04 +0000360 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
361 isCommutable = R->getValueAsBit("isCommutable");
Chris Lattnerec352402004-08-01 05:04:00 +0000362 isTerminator = R->getValueAsBit("isTerminator");
Evan Cheng04677a32007-03-19 06:20:37 +0000363 isReMaterializable = R->getValueAsBit("isReMaterializable");
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000364 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
Chris Lattnere3cbf822005-08-26 20:55:40 +0000365 usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter");
Evan Cheng1c3d19e2005-12-04 08:18:16 +0000366 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
Evan Cheng2b4ea792005-12-26 09:11:45 +0000367 noResults = R->getValueAsBit("noResults");
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000368 hasVariableNumberOfOperands = false;
369
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000370 DagInit *DI;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000371 try {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000372 DI = R->getValueAsDag("OperandList");
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000373 } catch (...) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000374 // Error getting operand list, just ignore it (sparcv9).
Chris Lattner87c59052004-08-01 07:42:39 +0000375 AsmString.clear();
376 OperandList.clear();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000377 return;
378 }
379
380 unsigned MIOperandNo = 0;
381 std::set<std::string> OperandNames;
382 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
383 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
384 if (!Arg)
385 throw "Illegal operand for the '" + R->getName() + "' instruction!";
386
387 Record *Rec = Arg->getDef();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000388 std::string PrintMethod = "printOperand";
389 unsigned NumOps = 1;
Chris Lattner33670792005-11-19 07:48:33 +0000390 DagInit *MIOpInfo = 0;
Nate Begeman86193d12005-12-01 00:12:04 +0000391 if (Rec->isSubClassOf("Operand")) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000392 PrintMethod = Rec->getValueAsString("PrintMethod");
Chris Lattner65303d62005-11-19 07:05:57 +0000393 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
Chris Lattnerd438b532006-11-03 23:45:17 +0000394
395 // Verify that MIOpInfo has an 'ops' root value.
396 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
397 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
398 ->getDef()->getName() != "ops")
399 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
400 "'\n";
401
402 // If we have MIOpInfo, then we have #operands equal to number of entries
403 // in MIOperandInfo.
404 if (unsigned NumArgs = MIOpInfo->getNumArgs())
405 NumOps = NumArgs;
406
Chris Lattnera818e922006-11-06 21:44:54 +0000407 isPredicated |= Rec->isSubClassOf("PredicateOperand");
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000408 } else if (Rec->getName() == "variable_ops") {
409 hasVariableNumberOfOperands = true;
410 continue;
Chris Lattnerf1968392006-11-10 02:01:40 +0000411 } else if (!Rec->isSubClassOf("RegisterClass") &&
412 Rec->getName() != "ptr_rc")
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000413 throw "Unknown operand class '" + Rec->getName() +
414 "' in instruction '" + R->getName() + "' instruction!";
415
Chris Lattnerc4a8b732005-09-14 21:13:50 +0000416 // Check that the operand has a name and that it's unique.
417 if (DI->getArgName(i).empty())
418 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
419 " has no name!";
420 if (!OperandNames.insert(DI->getArgName(i)).second)
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000421 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
422 " has the same name as a previous operand!";
423
Nate Begeman86193d12005-12-01 00:12:04 +0000424 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
425 MIOperandNo, NumOps, MIOpInfo));
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000426 MIOperandNo += NumOps;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000427 }
Evan Chenge2ba8972006-11-01 00:27:05 +0000428
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000429 // Parse Constraints.
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000430 ParseConstraints(R->getValueAsString("Constraints"), this);
431
432 // For backward compatibility: isTwoAddress means operand 1 is tied to
433 // operand 0.
Chris Lattner0bb75002006-11-15 02:38:17 +0000434 if (isTwoAddress) {
435 if (!OperandList[1].Constraints[0].empty())
436 throw R->getName() + ": cannot use isTwoAddress property: instruction "
437 "already has constraint set!";
Evan Cheng05551222006-12-01 22:57:41 +0000438 OperandList[1].Constraints[0] = "((0 << 16) | (1 << TOI::TIED_TO))";
Chris Lattner0bb75002006-11-15 02:38:17 +0000439 }
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000440
441 // Any operands with unset constraints get 0 as their constraint.
442 for (unsigned op = 0, e = OperandList.size(); op != e; ++op)
Chris Lattner0bb75002006-11-15 02:38:17 +0000443 for (unsigned j = 0, e = OperandList[op].MINumOperands; j != e; ++j)
444 if (OperandList[op].Constraints[j].empty())
445 OperandList[op].Constraints[j] = "0";
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000446
447 // Parse the DisableEncoding field.
448 std::string DisableEncoding = R->getValueAsString("DisableEncoding");
449 while (1) {
450 std::string OpName = getToken(DisableEncoding, " ,\t");
451 if (OpName.empty()) break;
452
453 // Figure out which operand this is.
454 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
455
456 // Mark the operand as not-to-be encoded.
457 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
458 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
459 OperandList[Op.first].DoNotEncode[Op.second] = true;
460 }
Chris Lattnerec352402004-08-01 05:04:00 +0000461}
462
463
Chris Lattner87c59052004-08-01 07:42:39 +0000464
465/// getOperandNamed - Return the index of the operand with the specified
466/// non-empty name. If the instruction does not have an operand with the
467/// specified name, throw an exception.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000468///
Chris Lattner87c59052004-08-01 07:42:39 +0000469unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
470 assert(!Name.empty() && "Cannot search for operand with no name!");
471 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
472 if (OperandList[i].Name == Name) return i;
473 throw "Instruction '" + TheDef->getName() +
474 "' does not have an operand named '$" + Name + "'!";
475}
Evan Cheng0fc71982005-12-08 02:00:36 +0000476
Chris Lattner0bb75002006-11-15 02:38:17 +0000477std::pair<unsigned,unsigned>
478CodeGenInstruction::ParseOperandName(const std::string &Op,
479 bool AllowWholeOp) {
480 if (Op.empty() || Op[0] != '$')
481 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
482
483 std::string OpName = Op.substr(1);
484 std::string SubOpName;
485
486 // Check to see if this is $foo.bar.
487 std::string::size_type DotIdx = OpName.find_first_of(".");
488 if (DotIdx != std::string::npos) {
489 SubOpName = OpName.substr(DotIdx+1);
490 if (SubOpName.empty())
491 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
492 OpName = OpName.substr(0, DotIdx);
493 }
494
495 unsigned OpIdx = getOperandNamed(OpName);
496
497 if (SubOpName.empty()) { // If no suboperand name was specified:
498 // If one was needed, throw.
499 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
500 SubOpName.empty())
501 throw TheDef->getName() + ": Illegal to refer to"
502 " whole operand part of complex operand '" + Op + "'";
503
504 // Otherwise, return the operand.
505 return std::make_pair(OpIdx, 0U);
506 }
507
508 // Find the suboperand number involved.
509 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
510 if (MIOpInfo == 0)
511 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
512
513 // Find the operand with the right name.
514 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
515 if (MIOpInfo->getArgName(i) == SubOpName)
516 return std::make_pair(OpIdx, i);
517
518 // Otherwise, didn't find it!
519 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
520}
521
522
523
524
Evan Cheng0fc71982005-12-08 02:00:36 +0000525//===----------------------------------------------------------------------===//
526// ComplexPattern implementation
527//
528ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000529 Ty = ::getValueType(R->getValueAsDef("Ty"));
530 NumOperands = R->getValueAsInt("NumOperands");
531 SelectFunc = R->getValueAsString("SelectFunc");
532 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000533
534 // Parse the properties.
535 Properties = 0;
536 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
537 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
538 if (PropList[i]->getName() == "SDNPHasChain") {
539 Properties |= 1 << SDNPHasChain;
540 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
541 Properties |= 1 << SDNPOptInFlag;
542 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000543 cerr << "Unsupported SD Node property '" << PropList[i]->getName()
544 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000545 exit(1);
546 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000547}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000548
Chris Lattner43fbbc32006-03-24 19:49:31 +0000549//===----------------------------------------------------------------------===//
550// CodeGenIntrinsic Implementation
551//===----------------------------------------------------------------------===//
552
553std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
554 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Chris Lattner8850a1b2006-03-27 22:48:18 +0000555
556 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000557
558 // If we are in the context of a target .td file, get the target info so that
559 // we can decode the current intptr_t.
560 CodeGenTarget *CGT = 0;
561 if (Records.getClass("Target") &&
562 Records.getAllDerivedDefinitions("Target").size() == 1)
563 CGT = new CodeGenTarget();
564
Chris Lattner8850a1b2006-03-27 22:48:18 +0000565 for (unsigned i = 0, e = I.size(); i != e; ++i)
Chris Lattner6a160fb2006-03-28 00:15:00 +0000566 Result.push_back(CodeGenIntrinsic(I[i], CGT));
567 delete CGT;
Chris Lattner8850a1b2006-03-27 22:48:18 +0000568 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000569}
570
Chris Lattner76f8c7c2006-03-28 00:03:08 +0000571CodeGenIntrinsic::CodeGenIntrinsic(Record *R, CodeGenTarget *CGT) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000572 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000573 std::string DefName = R->getName();
574 ModRef = WriteMem;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000575 isOverloaded = false;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000576
577 if (DefName.size() <= 4 ||
578 std::string(DefName.begin(), DefName.begin()+4) != "int_")
579 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
580 EnumName = std::string(DefName.begin()+4, DefName.end());
581 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
582 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
583 TargetPrefix = R->getValueAsString("TargetPrefix");
584 Name = R->getValueAsString("LLVMName");
585 if (Name == "") {
586 // If an explicit name isn't specified, derive one from the DefName.
587 Name = "llvm.";
588 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
589 if (EnumName[i] == '_')
590 Name += '.';
591 else
592 Name += EnumName[i];
593 } else {
594 // Verify it starts with "llvm.".
595 if (Name.size() <= 5 ||
596 std::string(Name.begin(), Name.begin()+5) != "llvm.")
597 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
598 }
599
600 // If TargetPrefix is specified, make sure that Name starts with
601 // "llvm.<targetprefix>.".
602 if (!TargetPrefix.empty()) {
603 if (Name.size() < 6+TargetPrefix.size() ||
604 std::string(Name.begin()+5, Name.begin()+6+TargetPrefix.size())
605 != (TargetPrefix+"."))
606 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
607 TargetPrefix + ".'!";
608 }
609
610 // Parse the list of argument types.
611 ListInit *TypeList = R->getValueAsListInit("Types");
612 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000613 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000614 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
615 ArgTypes.push_back(TyEl->getValueAsString("TypeVal"));
Reid Spencerc4de3de2007-04-01 07:20:02 +0000616 MVT::ValueType VT = getValueType(TyEl->getValueAsDef("VT"), CGT);
617 isOverloaded |= VT == MVT::iAny;
618 ArgVTs.push_back(VT);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000619 ArgTypeDefs.push_back(TyEl);
620 }
621 if (ArgTypes.size() == 0)
622 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000623
Chris Lattner43fbbc32006-03-24 19:49:31 +0000624
625 // Parse the intrinsic properties.
626 ListInit *PropList = R->getValueAsListInit("Properties");
627 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000628 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000629 assert(Property->isSubClassOf("IntrinsicProperty") &&
630 "Expected a property!");
631
Chris Lattner4b2362e2006-04-10 22:02:59 +0000632 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000633 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000634 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000635 ModRef = ReadArgMem;
636 else if (Property->getName() == "IntrReadMem")
637 ModRef = ReadMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000638 else if (Property->getName() == "IntrWriteArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000639 ModRef = WriteArgMem;
640 else if (Property->getName() == "IntrWriteMem")
641 ModRef = WriteMem;
642 else
643 assert(0 && "Unknown property!");
644 }
645}