CodeGenInstruction.cpp revision 208954
1//===- CodeGenInstruction.cpp - CodeGen Instruction Class Wrapper ---------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the CodeGenInstruction class. 11// 12//===----------------------------------------------------------------------===// 13 14#include "CodeGenInstruction.h" 15#include "CodeGenTarget.h" 16#include "Record.h" 17#include "llvm/ADT/StringExtras.h" 18#include "llvm/ADT/STLExtras.h" 19#include <set> 20using namespace llvm; 21 22static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) { 23 // EARLY_CLOBBER: @early $reg 24 std::string::size_type wpos = CStr.find_first_of(" \t"); 25 std::string::size_type start = CStr.find_first_not_of(" \t"); 26 std::string Tok = CStr.substr(start, wpos - start); 27 if (Tok == "@earlyclobber") { 28 std::string Name = CStr.substr(wpos+1); 29 wpos = Name.find_first_not_of(" \t"); 30 if (wpos == std::string::npos) 31 throw "Illegal format for @earlyclobber constraint: '" + CStr + "'"; 32 Name = Name.substr(wpos); 33 std::pair<unsigned,unsigned> Op = 34 I->ParseOperandName(Name, false); 35 36 // Build the string for the operand 37 if (!I->OperandList[Op.first].Constraints[Op.second].isNone()) 38 throw "Operand '" + Name + "' cannot have multiple constraints!"; 39 I->OperandList[Op.first].Constraints[Op.second] = 40 CodeGenInstruction::ConstraintInfo::getEarlyClobber(); 41 return; 42 } 43 44 // Only other constraint is "TIED_TO" for now. 45 std::string::size_type pos = CStr.find_first_of('='); 46 assert(pos != std::string::npos && "Unrecognized constraint"); 47 start = CStr.find_first_not_of(" \t"); 48 std::string Name = CStr.substr(start, pos - start); 49 50 // TIED_TO: $src1 = $dst 51 wpos = Name.find_first_of(" \t"); 52 if (wpos == std::string::npos) 53 throw "Illegal format for tied-to constraint: '" + CStr + "'"; 54 std::string DestOpName = Name.substr(0, wpos); 55 std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false); 56 57 Name = CStr.substr(pos+1); 58 wpos = Name.find_first_not_of(" \t"); 59 if (wpos == std::string::npos) 60 throw "Illegal format for tied-to constraint: '" + CStr + "'"; 61 62 std::pair<unsigned,unsigned> SrcOp = 63 I->ParseOperandName(Name.substr(wpos), false); 64 if (SrcOp > DestOp) 65 throw "Illegal tied-to operand constraint '" + CStr + "'"; 66 67 68 unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp); 69 70 if (!I->OperandList[DestOp.first].Constraints[DestOp.second].isNone()) 71 throw "Operand '" + DestOpName + "' cannot have multiple constraints!"; 72 I->OperandList[DestOp.first].Constraints[DestOp.second] = 73 CodeGenInstruction::ConstraintInfo::getTied(FlatOpNo); 74} 75 76static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) { 77 // Make sure the constraints list for each operand is large enough to hold 78 // constraint info, even if none is present. 79 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i) 80 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands); 81 82 if (CStr.empty()) return; 83 84 const std::string delims(","); 85 std::string::size_type bidx, eidx; 86 87 bidx = CStr.find_first_not_of(delims); 88 while (bidx != std::string::npos) { 89 eidx = CStr.find_first_of(delims, bidx); 90 if (eidx == std::string::npos) 91 eidx = CStr.length(); 92 93 ParseConstraint(CStr.substr(bidx, eidx - bidx), I); 94 bidx = CStr.find_first_not_of(delims, eidx); 95 } 96} 97 98CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr) 99 : TheDef(R), AsmString(AsmStr) { 100 Namespace = R->getValueAsString("Namespace"); 101 102 isReturn = R->getValueAsBit("isReturn"); 103 isBranch = R->getValueAsBit("isBranch"); 104 isIndirectBranch = R->getValueAsBit("isIndirectBranch"); 105 isBarrier = R->getValueAsBit("isBarrier"); 106 isCall = R->getValueAsBit("isCall"); 107 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad"); 108 mayLoad = R->getValueAsBit("mayLoad"); 109 mayStore = R->getValueAsBit("mayStore"); 110 bool isTwoAddress = R->getValueAsBit("isTwoAddress"); 111 isPredicable = R->getValueAsBit("isPredicable"); 112 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress"); 113 isCommutable = R->getValueAsBit("isCommutable"); 114 isTerminator = R->getValueAsBit("isTerminator"); 115 isReMaterializable = R->getValueAsBit("isReMaterializable"); 116 hasDelaySlot = R->getValueAsBit("hasDelaySlot"); 117 usesCustomInserter = R->getValueAsBit("usesCustomInserter"); 118 hasCtrlDep = R->getValueAsBit("hasCtrlDep"); 119 isNotDuplicable = R->getValueAsBit("isNotDuplicable"); 120 hasSideEffects = R->getValueAsBit("hasSideEffects"); 121 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects"); 122 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove"); 123 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq"); 124 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq"); 125 hasOptionalDef = false; 126 isVariadic = false; 127 ImplicitDefs = R->getValueAsListOfDefs("Defs"); 128 ImplicitUses = R->getValueAsListOfDefs("Uses"); 129 130 if (neverHasSideEffects + hasSideEffects > 1) 131 throw R->getName() + ": multiple conflicting side-effect flags set!"; 132 133 DagInit *OutDI = R->getValueAsDag("OutOperandList"); 134 135 if (DefInit *Init = dynamic_cast<DefInit*>(OutDI->getOperator())) { 136 if (Init->getDef()->getName() != "outs") 137 throw R->getName() + ": invalid def name for output list: use 'outs'"; 138 } else 139 throw R->getName() + ": invalid output list: use 'outs'"; 140 141 NumDefs = OutDI->getNumArgs(); 142 143 DagInit *InDI = R->getValueAsDag("InOperandList"); 144 if (DefInit *Init = dynamic_cast<DefInit*>(InDI->getOperator())) { 145 if (Init->getDef()->getName() != "ins") 146 throw R->getName() + ": invalid def name for input list: use 'ins'"; 147 } else 148 throw R->getName() + ": invalid input list: use 'ins'"; 149 150 unsigned MIOperandNo = 0; 151 std::set<std::string> OperandNames; 152 for (unsigned i = 0, e = InDI->getNumArgs()+OutDI->getNumArgs(); i != e; ++i){ 153 Init *ArgInit; 154 std::string ArgName; 155 if (i < NumDefs) { 156 ArgInit = OutDI->getArg(i); 157 ArgName = OutDI->getArgName(i); 158 } else { 159 ArgInit = InDI->getArg(i-NumDefs); 160 ArgName = InDI->getArgName(i-NumDefs); 161 } 162 163 DefInit *Arg = dynamic_cast<DefInit*>(ArgInit); 164 if (!Arg) 165 throw "Illegal operand for the '" + R->getName() + "' instruction!"; 166 167 Record *Rec = Arg->getDef(); 168 std::string PrintMethod = "printOperand"; 169 unsigned NumOps = 1; 170 DagInit *MIOpInfo = 0; 171 if (Rec->isSubClassOf("Operand")) { 172 PrintMethod = Rec->getValueAsString("PrintMethod"); 173 MIOpInfo = Rec->getValueAsDag("MIOperandInfo"); 174 175 // Verify that MIOpInfo has an 'ops' root value. 176 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) || 177 dynamic_cast<DefInit*>(MIOpInfo->getOperator()) 178 ->getDef()->getName() != "ops") 179 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() + 180 "'\n"; 181 182 // If we have MIOpInfo, then we have #operands equal to number of entries 183 // in MIOperandInfo. 184 if (unsigned NumArgs = MIOpInfo->getNumArgs()) 185 NumOps = NumArgs; 186 187 if (Rec->isSubClassOf("PredicateOperand")) 188 isPredicable = true; 189 else if (Rec->isSubClassOf("OptionalDefOperand")) 190 hasOptionalDef = true; 191 } else if (Rec->getName() == "variable_ops") { 192 isVariadic = true; 193 continue; 194 } else if (!Rec->isSubClassOf("RegisterClass") && 195 Rec->getName() != "ptr_rc" && Rec->getName() != "unknown") 196 throw "Unknown operand class '" + Rec->getName() + 197 "' in '" + R->getName() + "' instruction!"; 198 199 // Check that the operand has a name and that it's unique. 200 if (ArgName.empty()) 201 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) + 202 " has no name!"; 203 if (!OperandNames.insert(ArgName).second) 204 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) + 205 " has the same name as a previous operand!"; 206 207 OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod, 208 MIOperandNo, NumOps, MIOpInfo)); 209 MIOperandNo += NumOps; 210 } 211 212 // Parse Constraints. 213 ParseConstraints(R->getValueAsString("Constraints"), this); 214 215 // For backward compatibility: isTwoAddress means operand 1 is tied to 216 // operand 0. 217 if (isTwoAddress) { 218 if (!OperandList[1].Constraints[0].isNone()) 219 throw R->getName() + ": cannot use isTwoAddress property: instruction " 220 "already has constraint set!"; 221 OperandList[1].Constraints[0] = 222 CodeGenInstruction::ConstraintInfo::getTied(0); 223 } 224 225 // Parse the DisableEncoding field. 226 std::string DisableEncoding = R->getValueAsString("DisableEncoding"); 227 while (1) { 228 std::string OpName; 229 tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t"); 230 if (OpName.empty()) break; 231 232 // Figure out which operand this is. 233 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false); 234 235 // Mark the operand as not-to-be encoded. 236 if (Op.second >= OperandList[Op.first].DoNotEncode.size()) 237 OperandList[Op.first].DoNotEncode.resize(Op.second+1); 238 OperandList[Op.first].DoNotEncode[Op.second] = true; 239 } 240} 241 242/// getOperandNamed - Return the index of the operand with the specified 243/// non-empty name. If the instruction does not have an operand with the 244/// specified name, throw an exception. 245/// 246unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const { 247 assert(!Name.empty() && "Cannot search for operand with no name!"); 248 for (unsigned i = 0, e = OperandList.size(); i != e; ++i) 249 if (OperandList[i].Name == Name) return i; 250 throw "Instruction '" + TheDef->getName() + 251 "' does not have an operand named '$" + Name + "'!"; 252} 253 254std::pair<unsigned,unsigned> 255CodeGenInstruction::ParseOperandName(const std::string &Op, 256 bool AllowWholeOp) { 257 if (Op.empty() || Op[0] != '$') 258 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'"; 259 260 std::string OpName = Op.substr(1); 261 std::string SubOpName; 262 263 // Check to see if this is $foo.bar. 264 std::string::size_type DotIdx = OpName.find_first_of("."); 265 if (DotIdx != std::string::npos) { 266 SubOpName = OpName.substr(DotIdx+1); 267 if (SubOpName.empty()) 268 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'"; 269 OpName = OpName.substr(0, DotIdx); 270 } 271 272 unsigned OpIdx = getOperandNamed(OpName); 273 274 if (SubOpName.empty()) { // If no suboperand name was specified: 275 // If one was needed, throw. 276 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp && 277 SubOpName.empty()) 278 throw TheDef->getName() + ": Illegal to refer to" 279 " whole operand part of complex operand '" + Op + "'"; 280 281 // Otherwise, return the operand. 282 return std::make_pair(OpIdx, 0U); 283 } 284 285 // Find the suboperand number involved. 286 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo; 287 if (MIOpInfo == 0) 288 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'"; 289 290 // Find the operand with the right name. 291 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i) 292 if (MIOpInfo->getArgName(i) == SubOpName) 293 return std::make_pair(OpIdx, i); 294 295 // Otherwise, didn't find it! 296 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'"; 297} 298 299 300/// HasOneImplicitDefWithKnownVT - If the instruction has at least one 301/// implicit def and it has a known VT, return the VT, otherwise return 302/// MVT::Other. 303MVT::SimpleValueType CodeGenInstruction:: 304HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const { 305 if (ImplicitDefs.empty()) return MVT::Other; 306 307 // Check to see if the first implicit def has a resolvable type. 308 Record *FirstImplicitDef = ImplicitDefs[0]; 309 assert(FirstImplicitDef->isSubClassOf("Register")); 310 const std::vector<MVT::SimpleValueType> &RegVTs = 311 TargetInfo.getRegisterVTs(FirstImplicitDef); 312 if (RegVTs.size() == 1) 313 return RegVTs[0]; 314 return MVT::Other; 315} 316 317