1 //===- CodeGenRegisters.cpp - Register and RegisterClass Info -------------===//
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 defines structures to encapsulate information gleaned from the
11 // target register and register class definitions.
13 //===----------------------------------------------------------------------===//
15 #include "CodeGenRegisters.h"
16 #include "CodeGenTarget.h"
17 #include "llvm/ADT/StringExtras.h"
21 //===----------------------------------------------------------------------===//
23 //===----------------------------------------------------------------------===//
25 CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
26 CostPerUse = R->getValueAsInt("CostPerUse");
29 const std::string &CodeGenRegister::getName() const {
30 return TheDef->getName();
33 //===----------------------------------------------------------------------===//
34 // CodeGenRegisterClass
35 //===----------------------------------------------------------------------===//
37 CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
38 // Rename anonymous register classes.
39 if (R->getName().size() > 9 && R->getName()[9] == '.') {
40 static unsigned AnonCounter = 0;
41 R->setName("AnonRegClass_"+utostr(AnonCounter++));
44 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
45 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
46 Record *Type = TypeList[i];
47 if (!Type->isSubClassOf("ValueType"))
48 throw "RegTypes list member '" + Type->getName() +
49 "' does not derive from the ValueType class!";
50 VTs.push_back(getValueType(Type));
52 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
54 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
55 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
56 Record *Reg = RegList[i];
57 if (!Reg->isSubClassOf("Register"))
58 throw "Register Class member '" + Reg->getName() +
59 "' does not derive from the Register class!";
60 Elements.push_back(Reg);
63 // SubRegClasses is a list<dag> containing (RC, subregindex, ...) dags.
64 ListInit *SRC = R->getValueAsListInit("SubRegClasses");
65 for (ListInit::const_iterator i = SRC->begin(), e = SRC->end(); i != e; ++i) {
66 DagInit *DAG = dynamic_cast<DagInit*>(*i);
67 if (!DAG) throw "SubRegClasses must contain DAGs";
68 DefInit *DAGOp = dynamic_cast<DefInit*>(DAG->getOperator());
70 if (!DAGOp || !(RCRec = DAGOp->getDef())->isSubClassOf("RegisterClass"))
71 throw "Operator '" + DAG->getOperator()->getAsString() +
72 "' in SubRegClasses is not a RegisterClass";
73 // Iterate over args, all SubRegIndex instances.
74 for (DagInit::const_arg_iterator ai = DAG->arg_begin(), ae = DAG->arg_end();
76 DefInit *Idx = dynamic_cast<DefInit*>(*ai);
78 if (!Idx || !(IdxRec = Idx->getDef())->isSubClassOf("SubRegIndex"))
79 throw "Argument '" + (*ai)->getAsString() +
80 "' in SubRegClasses is not a SubRegIndex";
81 if (!SubRegClasses.insert(std::make_pair(IdxRec, RCRec)).second)
82 throw "SubRegIndex '" + IdxRec->getName() + "' mentioned twice";
86 // Allow targets to override the size in bits of the RegisterClass.
87 unsigned Size = R->getValueAsInt("Size");
89 Namespace = R->getValueAsString("Namespace");
90 SpillSize = Size ? Size : EVT(VTs[0]).getSizeInBits();
91 SpillAlignment = R->getValueAsInt("Alignment");
92 CopyCost = R->getValueAsInt("CopyCost");
93 Allocatable = R->getValueAsBit("isAllocatable");
94 MethodBodies = R->getValueAsCode("MethodBodies");
95 MethodProtos = R->getValueAsCode("MethodProtos");
98 const std::string &CodeGenRegisterClass::getName() const {
99 return TheDef->getName();