| Chris Lattner | ea2d52d | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 1 | //===- CodeGenTarget.cpp - CodeGen Target Class Wrapper -------------------===// | 
| Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // | 
| John Criswell | d303203 | 2003-10-20 20:20:30 +0000 | [diff] [blame] | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | 8adcd9f | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
| Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // | 
| John Criswell | d303203 | 2003-10-20 20:20:30 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 9 | // | 
| Chris Lattner | ea2d52d | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 10 | // This class wraps target description classes used by the various code | 
| Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 11 | // 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 Lattner | fce9603 | 2004-08-01 04:04:35 +0000 | [diff] [blame] | 17 | #include "CodeGenTarget.h" | 
| Chris Lattner | 2c58141 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 18 | #include "CodeGenIntrinsics.h" | 
| Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 19 | #include "Record.h" | 
| Chris Lattner | 91c538f | 2004-10-03 19:34:31 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" | 
|  | 21 | #include "llvm/Support/CommandLine.h" | 
| Bill Wendling | 9bfb1e1 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Streams.h" | 
| Chris Lattner | 90c5b9c | 2005-10-14 03:54:49 +0000 | [diff] [blame] | 23 | #include <algorithm> | 
| Chris Lattner | 6847866 | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 24 | using namespace llvm; | 
| Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 |  | 
| Chris Lattner | 91c538f | 2004-10-03 19:34:31 +0000 | [diff] [blame] | 26 | static cl::opt<unsigned> | 
|  | 27 | AsmWriterNum("asmwriternum", cl::init(0), | 
|  | 28 | cl::desc("Make -gen-asm-writer emit assembly writer #N")); | 
|  | 29 |  | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 30 | /// getValueType - Return the MVT::SimpleValueType that the specified TableGen | 
|  | 31 | /// record corresponds to. | 
|  | 32 | MVT::SimpleValueType llvm::getValueType(Record *Rec) { | 
|  | 33 | return (MVT::SimpleValueType)Rec->getValueAsInt("Value"); | 
| Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 34 | } | 
|  | 35 |  | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 36 | std::string llvm::getName(MVT::SimpleValueType T) { | 
| Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 37 | switch (T) { | 
|  | 38 | case MVT::Other: return "UNKNOWN"; | 
| Evan Cheng | 5d038cf | 2006-06-15 00:16:37 +0000 | [diff] [blame] | 39 | case MVT::i1:    return "MVT::i1"; | 
|  | 40 | case MVT::i8:    return "MVT::i8"; | 
|  | 41 | case MVT::i16:   return "MVT::i16"; | 
|  | 42 | case MVT::i32:   return "MVT::i32"; | 
|  | 43 | case MVT::i64:   return "MVT::i64"; | 
|  | 44 | case MVT::i128:  return "MVT::i128"; | 
| Reid Spencer | e67d0c2 | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 45 | case MVT::iAny:  return "MVT::iAny"; | 
| Dan Gohman | febf946 | 2007-08-16 21:57:19 +0000 | [diff] [blame] | 46 | case MVT::fAny:  return "MVT::fAny"; | 
| Evan Cheng | 5d038cf | 2006-06-15 00:16:37 +0000 | [diff] [blame] | 47 | 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"; | 
| Dale Johannesen | 25a00a6 | 2007-09-28 01:08:20 +0000 | [diff] [blame] | 51 | case MVT::ppcf128:  return "MVT::ppcf128"; | 
| Evan Cheng | 5d038cf | 2006-06-15 00:16:37 +0000 | [diff] [blame] | 52 | case MVT::Flag:  return "MVT::Flag"; | 
|  | 53 | case MVT::isVoid:return "MVT::void"; | 
|  | 54 | case MVT::v8i8:  return "MVT::v8i8"; | 
|  | 55 | case MVT::v4i16: return "MVT::v4i16"; | 
|  | 56 | case MVT::v2i32: return "MVT::v2i32"; | 
| Bill Wendling | 98d2104 | 2007-03-26 07:53:08 +0000 | [diff] [blame] | 57 | case MVT::v1i64: return "MVT::v1i64"; | 
| Evan Cheng | 5d038cf | 2006-06-15 00:16:37 +0000 | [diff] [blame] | 58 | case MVT::v16i8: return "MVT::v16i8"; | 
|  | 59 | case MVT::v8i16: return "MVT::v8i16"; | 
|  | 60 | case MVT::v4i32: return "MVT::v4i32"; | 
|  | 61 | case MVT::v2i64: return "MVT::v2i64"; | 
|  | 62 | case MVT::v2f32: return "MVT::v2f32"; | 
|  | 63 | case MVT::v4f32: return "MVT::v4f32"; | 
|  | 64 | case MVT::v2f64: return "MVT::v2f64"; | 
| Christopher Lamb | c9ea294 | 2007-07-26 06:41:18 +0000 | [diff] [blame] | 65 | case MVT::v3i32: return "MVT::v3i32"; | 
|  | 66 | case MVT::v3f32: return "MVT::v3f32"; | 
| Evan Cheng | 318a68e | 2006-05-17 20:55:51 +0000 | [diff] [blame] | 67 | case MVT::iPTR:  return "TLI.getPointerTy()"; | 
| Mon P Wang | 2c839d4 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 68 | case MVT::iPTRAny:  return "TLI.getPointerTy()"; | 
| Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 69 | default: assert(0 && "ILLEGAL VALUE TYPE!"); return ""; | 
|  | 70 | } | 
|  | 71 | } | 
|  | 72 |  | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 73 | std::string llvm::getEnumName(MVT::SimpleValueType T) { | 
| Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 74 | switch (T) { | 
| Evan Cheng | f5ef47f | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 75 | case MVT::Other: return "MVT::Other"; | 
|  | 76 | case MVT::i1:    return "MVT::i1"; | 
|  | 77 | case MVT::i8:    return "MVT::i8"; | 
|  | 78 | case MVT::i16:   return "MVT::i16"; | 
|  | 79 | case MVT::i32:   return "MVT::i32"; | 
|  | 80 | case MVT::i64:   return "MVT::i64"; | 
|  | 81 | case MVT::i128:  return "MVT::i128"; | 
| Reid Spencer | e67d0c2 | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 82 | case MVT::iAny:  return "MVT::iAny"; | 
| Dan Gohman | febf946 | 2007-08-16 21:57:19 +0000 | [diff] [blame] | 83 | case MVT::fAny:  return "MVT::fAny"; | 
| Evan Cheng | f5ef47f | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 84 | case MVT::f32:   return "MVT::f32"; | 
|  | 85 | case MVT::f64:   return "MVT::f64"; | 
|  | 86 | case MVT::f80:   return "MVT::f80"; | 
|  | 87 | case MVT::f128:  return "MVT::f128"; | 
| Dale Johannesen | 25a00a6 | 2007-09-28 01:08:20 +0000 | [diff] [blame] | 88 | case MVT::ppcf128:  return "MVT::ppcf128"; | 
| Evan Cheng | f5ef47f | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 89 | case MVT::Flag:  return "MVT::Flag"; | 
|  | 90 | case MVT::isVoid:return "MVT::isVoid"; | 
|  | 91 | case MVT::v8i8:  return "MVT::v8i8"; | 
|  | 92 | case MVT::v4i16: return "MVT::v4i16"; | 
|  | 93 | case MVT::v2i32: return "MVT::v2i32"; | 
| Bill Wendling | 98d2104 | 2007-03-26 07:53:08 +0000 | [diff] [blame] | 94 | case MVT::v1i64: return "MVT::v1i64"; | 
| Evan Cheng | f5ef47f | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 95 | case MVT::v16i8: return "MVT::v16i8"; | 
|  | 96 | case MVT::v8i16: return "MVT::v8i16"; | 
|  | 97 | case MVT::v4i32: return "MVT::v4i32"; | 
|  | 98 | case MVT::v2i64: return "MVT::v2i64"; | 
|  | 99 | case MVT::v2f32: return "MVT::v2f32"; | 
|  | 100 | case MVT::v4f32: return "MVT::v4f32"; | 
|  | 101 | case MVT::v2f64: return "MVT::v2f64"; | 
| Christopher Lamb | c9ea294 | 2007-07-26 06:41:18 +0000 | [diff] [blame] | 102 | case MVT::v3i32: return "MVT::v3i32"; | 
|  | 103 | case MVT::v3f32: return "MVT::v3f32"; | 
| Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 104 | case MVT::iPTR:  return "MVT::iPTR"; | 
| Mon P Wang | 2c839d4 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 105 | case MVT::iPTRAny:  return "MVT::iPTRAny"; | 
| Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 106 | default: assert(0 && "ILLEGAL VALUE TYPE!"); return ""; | 
|  | 107 | } | 
|  | 108 | } | 
|  | 109 |  | 
| Chris Lattner | 8cab021 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 110 | /// getQualifiedName - Return the name of the specified record, with a | 
|  | 111 | /// namespace qualifier if the record contains one. | 
|  | 112 | /// | 
|  | 113 | std::string llvm::getQualifiedName(const Record *R) { | 
|  | 114 | std::string Namespace = R->getValueAsString("Namespace"); | 
|  | 115 | if (Namespace.empty()) return R->getName(); | 
|  | 116 | return Namespace + "::" + R->getName(); | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 |  | 
|  | 120 |  | 
| Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 121 |  | 
| Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 122 | /// getTarget - Return the current instance of the Target class. | 
|  | 123 | /// | 
| Evan Cheng | f5ef47f | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 124 | CodeGenTarget::CodeGenTarget() { | 
| Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 125 | std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target"); | 
| Misha Brukman | aff10d0 | 2004-06-04 14:59:42 +0000 | [diff] [blame] | 126 | if (Targets.size() == 0) | 
| Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 127 | throw std::string("ERROR: No 'Target' subclasses defined!"); | 
| Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 128 | if (Targets.size() != 1) | 
|  | 129 | throw std::string("ERROR: Multiple subclasses of Target defined!"); | 
|  | 130 | TargetRec = Targets[0]; | 
| Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 131 | } | 
|  | 132 |  | 
|  | 133 |  | 
|  | 134 | const std::string &CodeGenTarget::getName() const { | 
|  | 135 | return TargetRec->getName(); | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | Record *CodeGenTarget::getInstructionSet() const { | 
|  | 139 | return TargetRec->getValueAsDef("InstructionSet"); | 
|  | 140 | } | 
| Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 141 |  | 
| Chris Lattner | 6ffa501 | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 142 | /// getAsmWriter - Return the AssemblyWriter definition for this target. | 
|  | 143 | /// | 
|  | 144 | Record *CodeGenTarget::getAsmWriter() const { | 
| Chris Lattner | 7ad0bed | 2005-10-28 22:49:02 +0000 | [diff] [blame] | 145 | std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters"); | 
|  | 146 | if (AsmWriterNum >= LI.size()) | 
| Chris Lattner | 91c538f | 2004-10-03 19:34:31 +0000 | [diff] [blame] | 147 | throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!"; | 
| Chris Lattner | 7ad0bed | 2005-10-28 22:49:02 +0000 | [diff] [blame] | 148 | return LI[AsmWriterNum]; | 
| Chris Lattner | 6ffa501 | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
| Chris Lattner | 8af61dd | 2004-08-16 01:10:21 +0000 | [diff] [blame] | 151 | void CodeGenTarget::ReadRegisters() const { | 
|  | 152 | std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register"); | 
|  | 153 | if (Regs.empty()) | 
|  | 154 | throw std::string("No 'Register' subclasses defined!"); | 
|  | 155 |  | 
|  | 156 | Registers.reserve(Regs.size()); | 
|  | 157 | Registers.assign(Regs.begin(), Regs.end()); | 
|  | 158 | } | 
|  | 159 |  | 
| Chris Lattner | e34ae99 | 2004-08-21 02:24:57 +0000 | [diff] [blame] | 160 | CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) { | 
|  | 161 | DeclaredSpillSize = R->getValueAsInt("SpillSize"); | 
|  | 162 | DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment"); | 
|  | 163 | } | 
|  | 164 |  | 
| Chris Lattner | 8af61dd | 2004-08-16 01:10:21 +0000 | [diff] [blame] | 165 | const std::string &CodeGenRegister::getName() const { | 
|  | 166 | return TheDef->getName(); | 
|  | 167 | } | 
|  | 168 |  | 
| Chris Lattner | 2a86fab | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 169 | void CodeGenTarget::ReadRegisterClasses() const { | 
|  | 170 | std::vector<Record*> RegClasses = | 
|  | 171 | Records.getAllDerivedDefinitions("RegisterClass"); | 
|  | 172 | if (RegClasses.empty()) | 
|  | 173 | throw std::string("No 'RegisterClass' subclasses defined!"); | 
|  | 174 |  | 
|  | 175 | RegisterClasses.reserve(RegClasses.size()); | 
|  | 176 | RegisterClasses.assign(RegClasses.begin(), RegClasses.end()); | 
|  | 177 | } | 
|  | 178 |  | 
| Evan Cheng | d985d66 | 2006-05-16 07:05:30 +0000 | [diff] [blame] | 179 | std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const { | 
|  | 180 | std::vector<unsigned char> Result; | 
|  | 181 | const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses(); | 
|  | 182 | for (unsigned i = 0, e = RCs.size(); i != e; ++i) { | 
|  | 183 | const CodeGenRegisterClass &RC = RegisterClasses[i]; | 
|  | 184 | for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) { | 
|  | 185 | if (R == RC.Elements[ei]) { | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 186 | const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes(); | 
| Evan Cheng | d985d66 | 2006-05-16 07:05:30 +0000 | [diff] [blame] | 187 | for (unsigned i = 0, e = InVTs.size(); i != e; ++i) | 
|  | 188 | Result.push_back(InVTs[i]); | 
|  | 189 | } | 
|  | 190 | } | 
|  | 191 | } | 
|  | 192 | return Result; | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 |  | 
| Chris Lattner | 2a86fab | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 196 | CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) { | 
| Chris Lattner | 418d8cf | 2005-08-19 18:45:20 +0000 | [diff] [blame] | 197 | // Rename anonymous register classes. | 
|  | 198 | if (R->getName().size() > 9 && R->getName()[9] == '.') { | 
|  | 199 | static unsigned AnonCounter = 0; | 
|  | 200 | R->setName("AnonRegClass_"+utostr(AnonCounter++)); | 
|  | 201 | } | 
|  | 202 |  | 
| Nate Begeman | 006bb04 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 203 | std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes"); | 
|  | 204 | for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { | 
|  | 205 | Record *Type = TypeList[i]; | 
|  | 206 | if (!Type->isSubClassOf("ValueType")) | 
|  | 207 | throw "RegTypes list member '" + Type->getName() + | 
|  | 208 | "' does not derive from the ValueType class!"; | 
|  | 209 | VTs.push_back(getValueType(Type)); | 
|  | 210 | } | 
|  | 211 | assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!"); | 
| Chris Lattner | 73ec2cb | 2005-08-19 19:12:51 +0000 | [diff] [blame] | 212 |  | 
| Chris Lattner | 7ad0bed | 2005-10-28 22:49:02 +0000 | [diff] [blame] | 213 | std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList"); | 
|  | 214 | for (unsigned i = 0, e = RegList.size(); i != e; ++i) { | 
|  | 215 | Record *Reg = RegList[i]; | 
| Chris Lattner | 2a86fab | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 216 | if (!Reg->isSubClassOf("Register")) | 
|  | 217 | throw "Register Class member '" + Reg->getName() + | 
|  | 218 | "' does not derive from the Register class!"; | 
|  | 219 | Elements.push_back(Reg); | 
|  | 220 | } | 
| Nate Begeman | 006bb04 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 221 |  | 
| Christopher Lamb | f274efe | 2007-06-13 22:20:15 +0000 | [diff] [blame] | 222 | std::vector<Record*> SubRegClassList = | 
|  | 223 | R->getValueAsListOfDefs("SubRegClassList"); | 
|  | 224 | for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) { | 
|  | 225 | Record *SubRegClass = SubRegClassList[i]; | 
|  | 226 | if (!SubRegClass->isSubClassOf("RegisterClass")) | 
|  | 227 | throw "Register Class member '" + SubRegClass->getName() + | 
|  | 228 | "' does not derive from the RegisterClass class!"; | 
|  | 229 | SubRegClasses.push_back(SubRegClass); | 
|  | 230 | } | 
|  | 231 |  | 
| Nate Begeman | 006bb04 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 232 | // Allow targets to override the size in bits of the RegisterClass. | 
|  | 233 | unsigned Size = R->getValueAsInt("Size"); | 
|  | 234 |  | 
|  | 235 | Namespace = R->getValueAsString("Namespace"); | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 236 | SpillSize = Size ? Size : MVT(VTs[0]).getSizeInBits(); | 
| Nate Begeman | 006bb04 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 237 | SpillAlignment = R->getValueAsInt("Alignment"); | 
| Evan Cheng | f73fb62 | 2007-09-19 01:35:01 +0000 | [diff] [blame] | 238 | CopyCost = R->getValueAsInt("CopyCost"); | 
| Nate Begeman | 006bb04 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 239 | MethodBodies = R->getValueAsCode("MethodBodies"); | 
|  | 240 | MethodProtos = R->getValueAsCode("MethodProtos"); | 
| Chris Lattner | 2a86fab | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 241 | } | 
|  | 242 |  | 
|  | 243 | const std::string &CodeGenRegisterClass::getName() const { | 
|  | 244 | return TheDef->getName(); | 
|  | 245 | } | 
|  | 246 |  | 
| Chris Lattner | cee994b | 2005-09-08 21:43:21 +0000 | [diff] [blame] | 247 | void CodeGenTarget::ReadLegalValueTypes() const { | 
|  | 248 | const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses(); | 
|  | 249 | for (unsigned i = 0, e = RCs.size(); i != e; ++i) | 
| Nate Begeman | 006bb04 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 250 | for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri) | 
|  | 251 | LegalValueTypes.push_back(RCs[i].VTs[ri]); | 
| Chris Lattner | 90c5b9c | 2005-10-14 03:54:49 +0000 | [diff] [blame] | 252 |  | 
|  | 253 | // Remove duplicates. | 
|  | 254 | std::sort(LegalValueTypes.begin(), LegalValueTypes.end()); | 
|  | 255 | LegalValueTypes.erase(std::unique(LegalValueTypes.begin(), | 
|  | 256 | LegalValueTypes.end()), | 
|  | 257 | LegalValueTypes.end()); | 
| Chris Lattner | cee994b | 2005-09-08 21:43:21 +0000 | [diff] [blame] | 258 | } | 
| Chris Lattner | 2a86fab | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 259 |  | 
| Chris Lattner | 6ffa501 | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 260 |  | 
| Chris Lattner | c860eca | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 261 | void CodeGenTarget::ReadInstructions() const { | 
|  | 262 | std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction"); | 
| Chris Lattner | 543fe4b | 2006-01-27 01:45:06 +0000 | [diff] [blame] | 263 | if (Insts.size() <= 2) | 
| Chris Lattner | c860eca | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 264 | throw std::string("No 'Instruction' subclasses defined!"); | 
|  | 265 |  | 
| Chris Lattner | 543fe4b | 2006-01-27 01:45:06 +0000 | [diff] [blame] | 266 | // Parse the instructions defined in the .td file. | 
| Chris Lattner | 6ffa501 | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 267 | std::string InstFormatName = | 
|  | 268 | getAsmWriter()->getValueAsString("InstFormatName"); | 
|  | 269 |  | 
|  | 270 | for (unsigned i = 0, e = Insts.size(); i != e; ++i) { | 
|  | 271 | std::string AsmStr = Insts[i]->getValueAsString(InstFormatName); | 
|  | 272 | Instructions.insert(std::make_pair(Insts[i]->getName(), | 
|  | 273 | CodeGenInstruction(Insts[i], AsmStr))); | 
|  | 274 | } | 
| Chris Lattner | c860eca | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 275 | } | 
|  | 276 |  | 
| Chris Lattner | 945e865 | 2005-01-22 18:58:51 +0000 | [diff] [blame] | 277 | /// getInstructionsByEnumValue - Return all of the instructions defined by the | 
|  | 278 | /// target, ordered by their enum value. | 
|  | 279 | void CodeGenTarget:: | 
|  | 280 | getInstructionsByEnumValue(std::vector<const CodeGenInstruction*> | 
|  | 281 | &NumberedInstructions) { | 
| Chris Lattner | 543fe4b | 2006-01-27 01:45:06 +0000 | [diff] [blame] | 282 | std::map<std::string, CodeGenInstruction>::const_iterator I; | 
|  | 283 | I = getInstructions().find("PHI"); | 
|  | 284 | if (I == Instructions.end()) throw "Could not find 'PHI' instruction!"; | 
|  | 285 | const CodeGenInstruction *PHI = &I->second; | 
|  | 286 |  | 
|  | 287 | I = getInstructions().find("INLINEASM"); | 
|  | 288 | if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!"; | 
|  | 289 | const CodeGenInstruction *INLINEASM = &I->second; | 
|  | 290 |  | 
| Dan Gohman | fb19f94 | 2008-07-01 00:05:16 +0000 | [diff] [blame] | 291 | I = getInstructions().find("DBG_LABEL"); | 
|  | 292 | if (I == Instructions.end()) throw "Could not find 'DBG_LABEL' instruction!"; | 
|  | 293 | const CodeGenInstruction *DBG_LABEL = &I->second; | 
|  | 294 |  | 
|  | 295 | I = getInstructions().find("EH_LABEL"); | 
|  | 296 | if (I == Instructions.end()) throw "Could not find 'EH_LABEL' instruction!"; | 
|  | 297 | const CodeGenInstruction *EH_LABEL = &I->second; | 
|  | 298 |  | 
|  | 299 | I = getInstructions().find("GC_LABEL"); | 
|  | 300 | if (I == Instructions.end()) throw "Could not find 'GC_LABEL' instruction!"; | 
|  | 301 | const CodeGenInstruction *GC_LABEL = &I->second; | 
| Jim Laskey | 214c582 | 2007-01-26 17:29:20 +0000 | [diff] [blame] | 302 |  | 
| Evan Cheng | efd142a | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 303 | I = getInstructions().find("DECLARE"); | 
|  | 304 | if (I == Instructions.end()) throw "Could not find 'DECLARE' instruction!"; | 
|  | 305 | const CodeGenInstruction *DECLARE = &I->second; | 
|  | 306 |  | 
| Christopher Lamb | cde0ee5 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 307 | I = getInstructions().find("EXTRACT_SUBREG"); | 
|  | 308 | if (I == Instructions.end()) | 
|  | 309 | throw "Could not find 'EXTRACT_SUBREG' instruction!"; | 
|  | 310 | const CodeGenInstruction *EXTRACT_SUBREG = &I->second; | 
|  | 311 |  | 
|  | 312 | I = getInstructions().find("INSERT_SUBREG"); | 
|  | 313 | if (I == Instructions.end()) | 
|  | 314 | throw "Could not find 'INSERT_SUBREG' instruction!"; | 
|  | 315 | const CodeGenInstruction *INSERT_SUBREG = &I->second; | 
|  | 316 |  | 
| Evan Cheng | 0e7b00d | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 317 | I = getInstructions().find("IMPLICIT_DEF"); | 
|  | 318 | if (I == Instructions.end()) | 
|  | 319 | throw "Could not find 'IMPLICIT_DEF' instruction!"; | 
|  | 320 | const CodeGenInstruction *IMPLICIT_DEF = &I->second; | 
|  | 321 |  | 
| Christopher Lamb | d3d0ad3 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 322 | I = getInstructions().find("SUBREG_TO_REG"); | 
|  | 323 | if (I == Instructions.end()) | 
|  | 324 | throw "Could not find 'SUBREG_TO_REG' instruction!"; | 
|  | 325 | const CodeGenInstruction *SUBREG_TO_REG = &I->second; | 
|  | 326 |  | 
| Chris Lattner | 945e865 | 2005-01-22 18:58:51 +0000 | [diff] [blame] | 327 | // Print out the rest of the instructions now. | 
| Chris Lattner | 945e865 | 2005-01-22 18:58:51 +0000 | [diff] [blame] | 328 | NumberedInstructions.push_back(PHI); | 
| Chris Lattner | 543fe4b | 2006-01-27 01:45:06 +0000 | [diff] [blame] | 329 | NumberedInstructions.push_back(INLINEASM); | 
| Dan Gohman | fb19f94 | 2008-07-01 00:05:16 +0000 | [diff] [blame] | 330 | NumberedInstructions.push_back(DBG_LABEL); | 
|  | 331 | NumberedInstructions.push_back(EH_LABEL); | 
|  | 332 | NumberedInstructions.push_back(GC_LABEL); | 
| Evan Cheng | efd142a | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 333 | NumberedInstructions.push_back(DECLARE); | 
| Christopher Lamb | cde0ee5 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 334 | NumberedInstructions.push_back(EXTRACT_SUBREG); | 
|  | 335 | NumberedInstructions.push_back(INSERT_SUBREG); | 
| Evan Cheng | 0e7b00d | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 336 | NumberedInstructions.push_back(IMPLICIT_DEF); | 
| Christopher Lamb | d3d0ad3 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 337 | NumberedInstructions.push_back(SUBREG_TO_REG); | 
| Chris Lattner | 945e865 | 2005-01-22 18:58:51 +0000 | [diff] [blame] | 338 | for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II) | 
| Jim Laskey | 214c582 | 2007-01-26 17:29:20 +0000 | [diff] [blame] | 339 | if (&II->second != PHI && | 
|  | 340 | &II->second != INLINEASM && | 
| Dan Gohman | fb19f94 | 2008-07-01 00:05:16 +0000 | [diff] [blame] | 341 | &II->second != DBG_LABEL && | 
|  | 342 | &II->second != EH_LABEL && | 
|  | 343 | &II->second != GC_LABEL && | 
| Evan Cheng | efd142a | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 344 | &II->second != DECLARE && | 
| Christopher Lamb | cde0ee5 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 345 | &II->second != EXTRACT_SUBREG && | 
| Evan Cheng | 0e7b00d | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 346 | &II->second != INSERT_SUBREG && | 
| Christopher Lamb | d3d0ad3 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 347 | &II->second != IMPLICIT_DEF && | 
|  | 348 | &II->second != SUBREG_TO_REG) | 
| Chris Lattner | 945e865 | 2005-01-22 18:58:51 +0000 | [diff] [blame] | 349 | NumberedInstructions.push_back(&II->second); | 
|  | 350 | } | 
|  | 351 |  | 
|  | 352 |  | 
| Misha Brukman | 243ded5 | 2004-10-14 05:50:43 +0000 | [diff] [blame] | 353 | /// isLittleEndianEncoding - Return whether this target encodes its instruction | 
|  | 354 | /// in little-endian format, i.e. bits laid out in the order [0..n] | 
|  | 355 | /// | 
|  | 356 | bool CodeGenTarget::isLittleEndianEncoding() const { | 
|  | 357 | return getInstructionSet()->getValueAsBit("isLittleEndianEncoding"); | 
|  | 358 | } | 
|  | 359 |  | 
| Evan Cheng | 9b9567b | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 360 | //===----------------------------------------------------------------------===// | 
|  | 361 | // ComplexPattern implementation | 
|  | 362 | // | 
|  | 363 | ComplexPattern::ComplexPattern(Record *R) { | 
| Evan Cheng | c9a6200 | 2005-12-08 02:14:08 +0000 | [diff] [blame] | 364 | Ty          = ::getValueType(R->getValueAsDef("Ty")); | 
|  | 365 | NumOperands = R->getValueAsInt("NumOperands"); | 
|  | 366 | SelectFunc  = R->getValueAsString("SelectFunc"); | 
|  | 367 | RootNodes   = R->getValueAsListOfDefs("RootNodes"); | 
| Evan Cheng | 2022c79 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 368 |  | 
|  | 369 | // Parse the properties. | 
|  | 370 | Properties = 0; | 
|  | 371 | std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties"); | 
|  | 372 | for (unsigned i = 0, e = PropList.size(); i != e; ++i) | 
|  | 373 | if (PropList[i]->getName() == "SDNPHasChain") { | 
|  | 374 | Properties |= 1 << SDNPHasChain; | 
|  | 375 | } else if (PropList[i]->getName() == "SDNPOptInFlag") { | 
|  | 376 | Properties |= 1 << SDNPOptInFlag; | 
| Chris Lattner | 317332f | 2008-01-10 07:59:24 +0000 | [diff] [blame] | 377 | } else if (PropList[i]->getName() == "SDNPMayStore") { | 
|  | 378 | Properties |= 1 << SDNPMayStore; | 
|  | 379 | } else if (PropList[i]->getName() == "SDNPMayLoad") { | 
|  | 380 | Properties |= 1 << SDNPMayLoad; | 
|  | 381 | } else if (PropList[i]->getName() == "SDNPSideEffect") { | 
|  | 382 | Properties |= 1 << SDNPSideEffect; | 
| Mon P Wang | 6a49037 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 383 | } else if (PropList[i]->getName() == "SDNPMemOperand") { | 
|  | 384 | Properties |= 1 << SDNPMemOperand; | 
| Evan Cheng | 2022c79 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 385 | } else { | 
| Bill Wendling | 9bfb1e1 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 386 | cerr << "Unsupported SD Node property '" << PropList[i]->getName() | 
|  | 387 | << "' on ComplexPattern '" << R->getName() << "'!\n"; | 
| Evan Cheng | 2022c79 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 388 | exit(1); | 
|  | 389 | } | 
| Christopher Lamb | 0592cf7 | 2008-01-31 07:27:46 +0000 | [diff] [blame] | 390 |  | 
|  | 391 | // Parse the attributes. | 
|  | 392 | Attributes = 0; | 
|  | 393 | PropList = R->getValueAsListOfDefs("Attributes"); | 
|  | 394 | for (unsigned i = 0, e = PropList.size(); i != e; ++i) | 
|  | 395 | if (PropList[i]->getName() == "CPAttrParentAsRoot") { | 
|  | 396 | Attributes |= 1 << CPAttrParentAsRoot; | 
|  | 397 | } else { | 
|  | 398 | cerr << "Unsupported pattern attribute '" << PropList[i]->getName() | 
|  | 399 | << "' on ComplexPattern '" << R->getName() << "'!\n"; | 
|  | 400 | exit(1); | 
|  | 401 | } | 
| Evan Cheng | 9b9567b | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 402 | } | 
| Evan Cheng | c9a6200 | 2005-12-08 02:14:08 +0000 | [diff] [blame] | 403 |  | 
| Chris Lattner | 2c58141 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 404 | //===----------------------------------------------------------------------===// | 
|  | 405 | // CodeGenIntrinsic Implementation | 
|  | 406 | //===----------------------------------------------------------------------===// | 
|  | 407 |  | 
|  | 408 | std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) { | 
|  | 409 | std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic"); | 
| Chris Lattner | c92f688 | 2006-03-27 22:48:18 +0000 | [diff] [blame] | 410 |  | 
|  | 411 | std::vector<CodeGenIntrinsic> Result; | 
| Chris Lattner | 8a2ae8b7 | 2006-03-28 00:15:00 +0000 | [diff] [blame] | 412 |  | 
| Chris Lattner | c92f688 | 2006-03-27 22:48:18 +0000 | [diff] [blame] | 413 | for (unsigned i = 0, e = I.size(); i != e; ++i) | 
| Dan Gohman | fc4ad7de | 2008-04-03 00:02:49 +0000 | [diff] [blame] | 414 | Result.push_back(CodeGenIntrinsic(I[i])); | 
| Chris Lattner | c92f688 | 2006-03-27 22:48:18 +0000 | [diff] [blame] | 415 | return Result; | 
| Chris Lattner | 2c58141 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 416 | } | 
|  | 417 |  | 
| Dan Gohman | fc4ad7de | 2008-04-03 00:02:49 +0000 | [diff] [blame] | 418 | CodeGenIntrinsic::CodeGenIntrinsic(Record *R) { | 
| Chris Lattner | bbba823 | 2006-03-24 20:25:01 +0000 | [diff] [blame] | 419 | TheDef = R; | 
| Chris Lattner | 2c58141 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 420 | std::string DefName = R->getName(); | 
|  | 421 | ModRef = WriteMem; | 
| Reid Spencer | e67d0c2 | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 422 | isOverloaded = false; | 
| Evan Cheng | 49bad4c | 2008-06-16 20:29:38 +0000 | [diff] [blame] | 423 | isCommutative = false; | 
| Chris Lattner | 2c58141 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 424 |  | 
|  | 425 | if (DefName.size() <= 4 || | 
|  | 426 | std::string(DefName.begin(), DefName.begin()+4) != "int_") | 
|  | 427 | throw "Intrinsic '" + DefName + "' does not start with 'int_'!"; | 
|  | 428 | EnumName = std::string(DefName.begin()+4, DefName.end()); | 
|  | 429 | if (R->getValue("GCCBuiltinName"))  // Ignore a missing GCCBuiltinName field. | 
|  | 430 | GCCBuiltinName = R->getValueAsString("GCCBuiltinName"); | 
|  | 431 | TargetPrefix   = R->getValueAsString("TargetPrefix"); | 
|  | 432 | Name = R->getValueAsString("LLVMName"); | 
|  | 433 | if (Name == "") { | 
|  | 434 | // If an explicit name isn't specified, derive one from the DefName. | 
|  | 435 | Name = "llvm."; | 
|  | 436 | for (unsigned i = 0, e = EnumName.size(); i != e; ++i) | 
|  | 437 | if (EnumName[i] == '_') | 
|  | 438 | Name += '.'; | 
|  | 439 | else | 
|  | 440 | Name += EnumName[i]; | 
|  | 441 | } else { | 
|  | 442 | // Verify it starts with "llvm.". | 
|  | 443 | if (Name.size() <= 5 || | 
|  | 444 | std::string(Name.begin(), Name.begin()+5) != "llvm.") | 
|  | 445 | throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!"; | 
|  | 446 | } | 
|  | 447 |  | 
|  | 448 | // If TargetPrefix is specified, make sure that Name starts with | 
|  | 449 | // "llvm.<targetprefix>.". | 
|  | 450 | if (!TargetPrefix.empty()) { | 
|  | 451 | if (Name.size() < 6+TargetPrefix.size() || | 
|  | 452 | std::string(Name.begin()+5, Name.begin()+6+TargetPrefix.size()) | 
|  | 453 | != (TargetPrefix+".")) | 
|  | 454 | throw "Intrinsic '" + DefName + "' does not start with 'llvm." + | 
|  | 455 | TargetPrefix + ".'!"; | 
|  | 456 | } | 
|  | 457 |  | 
|  | 458 | // Parse the list of argument types. | 
|  | 459 | ListInit *TypeList = R->getValueAsListInit("Types"); | 
|  | 460 | for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) { | 
| Chris Lattner | cbebe46 | 2007-02-27 22:08:27 +0000 | [diff] [blame] | 461 | Record *TyEl = TypeList->getElementAsRecord(i); | 
| Chris Lattner | 2c58141 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 462 | assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!"); | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 463 | MVT::SimpleValueType VT = getValueType(TyEl->getValueAsDef("VT")); | 
| Mon P Wang | 2c839d4 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 464 | isOverloaded |= VT == MVT::iAny || VT == MVT::fAny || VT == MVT::iPTRAny; | 
| Reid Spencer | e67d0c2 | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 465 | ArgVTs.push_back(VT); | 
| Chris Lattner | 2c58141 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 466 | ArgTypeDefs.push_back(TyEl); | 
|  | 467 | } | 
| Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 468 | if (ArgVTs.size() == 0) | 
| Chris Lattner | 2c58141 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 469 | throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!"; | 
| Reid Spencer | e67d0c2 | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 470 |  | 
| Chris Lattner | 2c58141 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 471 |  | 
|  | 472 | // Parse the intrinsic properties. | 
|  | 473 | ListInit *PropList = R->getValueAsListInit("Properties"); | 
|  | 474 | for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) { | 
| Chris Lattner | cbebe46 | 2007-02-27 22:08:27 +0000 | [diff] [blame] | 475 | Record *Property = PropList->getElementAsRecord(i); | 
| Chris Lattner | 2c58141 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 476 | assert(Property->isSubClassOf("IntrinsicProperty") && | 
|  | 477 | "Expected a property!"); | 
|  | 478 |  | 
| Chris Lattner | 989b16e4 | 2006-04-10 22:02:59 +0000 | [diff] [blame] | 479 | if (Property->getName() == "IntrNoMem") | 
| Chris Lattner | 2c58141 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 480 | ModRef = NoMem; | 
| Chris Lattner | 989b16e4 | 2006-04-10 22:02:59 +0000 | [diff] [blame] | 481 | else if (Property->getName() == "IntrReadArgMem") | 
| Chris Lattner | 2c58141 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 482 | ModRef = ReadArgMem; | 
|  | 483 | else if (Property->getName() == "IntrReadMem") | 
|  | 484 | ModRef = ReadMem; | 
| Chris Lattner | 989b16e4 | 2006-04-10 22:02:59 +0000 | [diff] [blame] | 485 | else if (Property->getName() == "IntrWriteArgMem") | 
| Chris Lattner | 2c58141 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 486 | ModRef = WriteArgMem; | 
|  | 487 | else if (Property->getName() == "IntrWriteMem") | 
|  | 488 | ModRef = WriteMem; | 
| Evan Cheng | 49bad4c | 2008-06-16 20:29:38 +0000 | [diff] [blame] | 489 | else if (Property->getName() == "Commutative") | 
|  | 490 | isCommutative = true; | 
| Chris Lattner | 2c58141 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 491 | else | 
|  | 492 | assert(0 && "Unknown property!"); | 
|  | 493 | } | 
|  | 494 | } |