1 //===- FastISelEmitter.cpp - Generate an instruction selector -------------===// 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 tablegen backend emits code for use by the "fast" instruction 11 // selection algorithm. See the comments at the top of 12 // lib/CodeGen/SelectionDAG/FastISel.cpp for background. 13 // 14 // This file scans through the target's tablegen instruction-info files 15 // and extracts instructions with obvious-looking patterns, and it emits 16 // code to look up these instructions by type and operator. 17 // 18 //===----------------------------------------------------------------------===// 19 20 #include "FastISelEmitter.h" 21 #include "Record.h" 22 #include "llvm/Support/Debug.h" 23 #include "llvm/ADT/VectorExtras.h" 24 using namespace llvm; 25 26 namespace { 27 28 /// InstructionMemo - This class holds additional information about an 29 /// instruction needed to emit code for it. 30 /// 31 struct InstructionMemo { 32 std::string Name; 33 const CodeGenRegisterClass *RC; 34 unsigned char SubRegNo; 35 std::vector<std::string>* PhysRegs; 36 }; 37 38 /// OperandsSignature - This class holds a description of a list of operand 39 /// types. It has utility methods for emitting text based on the operands. 40 /// 41 struct OperandsSignature { 42 std::vector<std::string> Operands; 43 44 bool operator<(const OperandsSignature &O) const { 45 return Operands < O.Operands; 46 } 47 48 bool empty() const { return Operands.empty(); } 49 50 /// initialize - Examine the given pattern and initialize the contents 51 /// of the Operands array accordingly. Return true if all the operands 52 /// are supported, false otherwise. 53 /// 54 bool initialize(TreePatternNode *InstPatNode, 55 const CodeGenTarget &Target, 56 MVT::SimpleValueType VT) { 57 if (!InstPatNode->isLeaf() && 58 InstPatNode->getOperator()->getName() == "imm") { 59 Operands.push_back("i"); 60 return true; 61 } 62 if (!InstPatNode->isLeaf() && 63 InstPatNode->getOperator()->getName() == "fpimm") { 64 Operands.push_back("f"); 65 return true; 66 } 67 68 const CodeGenRegisterClass *DstRC = 0; 69 70 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) { 71 TreePatternNode *Op = InstPatNode->getChild(i); 72 // For now, filter out any operand with a predicate. 73 // For now, filter out any operand with multiple values. 74 if (!Op->getPredicateFns().empty() || 75 Op->getNumTypes() != 1) 76 return false; 77 78 assert(Op->hasTypeSet(0) && "Type infererence not done?"); 79 // For now, all the operands must have the same type. 80 if (Op->getType(0) != VT) 81 return false; 82 83 if (!Op->isLeaf()) { 84 if (Op->getOperator()->getName() == "imm") { 85 Operands.push_back("i"); 86 continue; 87 } 88 if (Op->getOperator()->getName() == "fpimm") { 89 Operands.push_back("f"); 90 continue; 91 } 92 // For now, ignore other non-leaf nodes. 93 return false; 94 } 95 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue()); 96 if (!OpDI) 97 return false; 98 Record *OpLeafRec = OpDI->getDef(); 99 // For now, the only other thing we accept is register operands. 100 101 const CodeGenRegisterClass *RC = 0; 102 if (OpLeafRec->isSubClassOf("RegisterClass")) 103 RC = &Target.getRegisterClass(OpLeafRec); 104 else if (OpLeafRec->isSubClassOf("Register")) 105 RC = Target.getRegisterClassForRegister(OpLeafRec); 106 else 107 return false; 108 // For now, require the register operands' register classes to all 109 // be the same. 110 if (!RC) 111 return false; 112 // For now, all the operands must have the same register class. 113 if (DstRC) { 114 if (DstRC != RC) 115 return false; 116 } else 117 DstRC = RC; 118 Operands.push_back("r"); 119 } 120 return true; 121 } 122 123 void PrintParameters(raw_ostream &OS) const { 124 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 125 if (Operands[i] == "r") { 126 OS << "unsigned Op" << i; 127 } else if (Operands[i] == "i") { 128 OS << "uint64_t imm" << i; 129 } else if (Operands[i] == "f") { 130 OS << "ConstantFP *f" << i; 131 } else { 132 assert("Unknown operand kind!"); 133 abort(); 134 } 135 if (i + 1 != e) 136 OS << ", "; 137 } 138 } 139 140 void PrintArguments(raw_ostream &OS, 141 const std::vector<std::string>& PR) const { 142 assert(PR.size() == Operands.size()); 143 bool PrintedArg = false; 144 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 145 if (PR[i] != "") 146 // Implicit physical register operand. 147 continue; 148 149 if (PrintedArg) 150 OS << ", "; 151 if (Operands[i] == "r") { 152 OS << "Op" << i; 153 PrintedArg = true; 154 } else if (Operands[i] == "i") { 155 OS << "imm" << i; 156 PrintedArg = true; 157 } else if (Operands[i] == "f") { 158 OS << "f" << i; 159 PrintedArg = true; 160 } else { 161 assert("Unknown operand kind!"); 162 abort(); 163 } 164 } 165 } 166 167 void PrintArguments(raw_ostream &OS) const { 168 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 169 if (Operands[i] == "r") { 170 OS << "Op" << i; 171 } else if (Operands[i] == "i") { 172 OS << "imm" << i; 173 } else if (Operands[i] == "f") { 174 OS << "f" << i; 175 } else { 176 assert("Unknown operand kind!"); 177 abort(); 178 } 179 if (i + 1 != e) 180 OS << ", "; 181 } 182 } 183 184 185 void PrintManglingSuffix(raw_ostream &OS, 186 const std::vector<std::string>& PR) const { 187 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 188 if (PR[i] != "") 189 // Implicit physical register operand. e.g. Instruction::Mul expect to 190 // select to a binary op. On x86, mul may take a single operand with 191 // the other operand being implicit. We must emit something that looks 192 // like a binary instruction except for the very inner FastEmitInst_* 193 // call. 194 continue; 195 OS << Operands[i]; 196 } 197 } 198 199 void PrintManglingSuffix(raw_ostream &OS) const { 200 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 201 OS << Operands[i]; 202 } 203 } 204 }; 205 206 class FastISelMap { 207 typedef std::map<std::string, InstructionMemo> PredMap; 208 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap; 209 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap; 210 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap; 211 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap> OperandsOpcodeTypeRetPredMap; 212 213 OperandsOpcodeTypeRetPredMap SimplePatterns; 214 215 std::string InstNS; 216 217 public: 218 explicit FastISelMap(std::string InstNS); 219 220 void CollectPatterns(CodeGenDAGPatterns &CGP); 221 void PrintFunctionDefinitions(raw_ostream &OS); 222 }; 223 224 } 225 226 static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) { 227 return CGP.getSDNodeInfo(Op).getEnumName(); 228 } 229 230 static std::string getLegalCName(std::string OpName) { 231 std::string::size_type pos = OpName.find("::"); 232 if (pos != std::string::npos) 233 OpName.replace(pos, 2, "_"); 234 return OpName; 235 } 236 237 FastISelMap::FastISelMap(std::string instns) 238 : InstNS(instns) { 239 } 240 241 void FastISelMap::CollectPatterns(CodeGenDAGPatterns &CGP) { 242 const CodeGenTarget &Target = CGP.getTargetInfo(); 243 244 // Determine the target's namespace name. 245 InstNS = Target.getInstNamespace() + "::"; 246 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!"); 247 248 // Scan through all the patterns and record the simple ones. 249 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), 250 E = CGP.ptm_end(); I != E; ++I) { 251 const PatternToMatch &Pattern = *I; 252 253 // For now, just look at Instructions, so that we don't have to worry 254 // about emitting multiple instructions for a pattern. 255 TreePatternNode *Dst = Pattern.getDstPattern(); 256 if (Dst->isLeaf()) continue; 257 Record *Op = Dst->getOperator(); 258 if (!Op->isSubClassOf("Instruction")) 259 continue; 260 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op); 261 if (II.OperandList.empty()) 262 continue; 263 264 // For now, ignore multi-instruction patterns. 265 bool MultiInsts = false; 266 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) { 267 TreePatternNode *ChildOp = Dst->getChild(i); 268 if (ChildOp->isLeaf()) 269 continue; 270 if (ChildOp->getOperator()->isSubClassOf("Instruction")) { 271 MultiInsts = true; 272 break; 273 } 274 } 275 if (MultiInsts) 276 continue; 277 278 // For now, ignore instructions where the first operand is not an 279 // output register. 280 const CodeGenRegisterClass *DstRC = 0; 281 unsigned SubRegNo = ~0; 282 if (Op->getName() != "EXTRACT_SUBREG") { 283 Record *Op0Rec = II.OperandList[0].Rec; 284 if (!Op0Rec->isSubClassOf("RegisterClass")) 285 continue; 286 DstRC = &Target.getRegisterClass(Op0Rec); 287 if (!DstRC) 288 continue; 289 } else { 290 SubRegNo = static_cast<IntInit*>( 291 Dst->getChild(1)->getLeafValue())->getValue(); 292 } 293 294 // Inspect the pattern. 295 TreePatternNode *InstPatNode = Pattern.getSrcPattern(); 296 if (!InstPatNode) continue; 297 if (InstPatNode->isLeaf()) continue; 298 299 // Ignore multiple result nodes for now. 300 if (InstPatNode->getNumTypes() > 1) continue; 301 302 Record *InstPatOp = InstPatNode->getOperator(); 303 std::string OpcodeName = getOpcodeName(InstPatOp, CGP); 304 MVT::SimpleValueType RetVT = MVT::isVoid; 305 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0); 306 MVT::SimpleValueType VT = RetVT; 307 if (InstPatNode->getNumChildren()) { 308 assert(InstPatNode->getChild(0)->getNumTypes() == 1); 309 VT = InstPatNode->getChild(0)->getType(0); 310 } 311 312 // For now, filter out instructions which just set a register to 313 // an Operand or an immediate, like MOV32ri. 314 if (InstPatOp->isSubClassOf("Operand")) 315 continue; 316 317 // For now, filter out any instructions with predicates. 318 if (!InstPatNode->getPredicateFns().empty()) 319 continue; 320 321 // Check all the operands. 322 OperandsSignature Operands; 323 if (!Operands.initialize(InstPatNode, Target, VT)) 324 continue; 325 326 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>(); 327 if (!InstPatNode->isLeaf() && 328 (InstPatNode->getOperator()->getName() == "imm" || 329 InstPatNode->getOperator()->getName() == "fpimmm")) 330 PhysRegInputs->push_back(""); 331 else if (!InstPatNode->isLeaf()) { 332 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) { 333 TreePatternNode *Op = InstPatNode->getChild(i); 334 if (!Op->isLeaf()) { 335 PhysRegInputs->push_back(""); 336 continue; 337 } 338 339 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue()); 340 Record *OpLeafRec = OpDI->getDef(); 341 std::string PhysReg; 342 if (OpLeafRec->isSubClassOf("Register")) { 343 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \ 344 "Namespace")->getValue())->getValue(); 345 PhysReg += "::"; 346 347 std::vector<CodeGenRegister> Regs = Target.getRegisters(); 348 for (unsigned i = 0; i < Regs.size(); ++i) { 349 if (Regs[i].TheDef == OpLeafRec) { 350 PhysReg += Regs[i].getName(); 351 break; 352 } 353 } 354 } 355 356 PhysRegInputs->push_back(PhysReg); 357 } 358 } else 359 PhysRegInputs->push_back(""); 360 361 // Get the predicate that guards this pattern. 362 std::string PredicateCheck = Pattern.getPredicateCheck(); 363 364 // Ok, we found a pattern that we can handle. Remember it. 365 InstructionMemo Memo = { 366 Pattern.getDstPattern()->getOperator()->getName(), 367 DstRC, 368 SubRegNo, 369 PhysRegInputs 370 }; 371 assert(!SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck) && 372 "Duplicate pattern!"); 373 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo; 374 } 375 } 376 377 void FastISelMap::PrintFunctionDefinitions(raw_ostream &OS) { 378 // Now emit code for all the patterns that we collected. 379 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(), 380 OE = SimplePatterns.end(); OI != OE; ++OI) { 381 const OperandsSignature &Operands = OI->first; 382 const OpcodeTypeRetPredMap &OTM = OI->second; 383 384 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end(); 385 I != E; ++I) { 386 const std::string &Opcode = I->first; 387 const TypeRetPredMap &TM = I->second; 388 389 OS << "// FastEmit functions for " << Opcode << ".\n"; 390 OS << "\n"; 391 392 // Emit one function for each opcode,type pair. 393 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end(); 394 TI != TE; ++TI) { 395 MVT::SimpleValueType VT = TI->first; 396 const RetPredMap &RM = TI->second; 397 if (RM.size() != 1) { 398 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end(); 399 RI != RE; ++RI) { 400 MVT::SimpleValueType RetVT = RI->first; 401 const PredMap &PM = RI->second; 402 bool HasPred = false; 403 404 OS << "unsigned FastEmit_" 405 << getLegalCName(Opcode) 406 << "_" << getLegalCName(getName(VT)) 407 << "_" << getLegalCName(getName(RetVT)) << "_"; 408 Operands.PrintManglingSuffix(OS); 409 OS << "("; 410 Operands.PrintParameters(OS); 411 OS << ") {\n"; 412 413 // Emit code for each possible instruction. There may be 414 // multiple if there are subtarget concerns. 415 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); 416 PI != PE; ++PI) { 417 std::string PredicateCheck = PI->first; 418 const InstructionMemo &Memo = PI->second; 419 420 if (PredicateCheck.empty()) { 421 assert(!HasPred && 422 "Multiple instructions match, at least one has " 423 "a predicate and at least one doesn't!"); 424 } else { 425 OS << " if (" + PredicateCheck + ") {\n"; 426 OS << " "; 427 HasPred = true; 428 } 429 430 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) { 431 if ((*Memo.PhysRegs)[i] != "") 432 OS << " TII.copyRegToReg(*MBB, MBB->end(), " 433 << (*Memo.PhysRegs)[i] << ", Op" << i << ", " 434 << "TM.getRegisterInfo()->getPhysicalRegisterRegClass(" 435 << (*Memo.PhysRegs)[i] << "), " 436 << "MRI.getRegClass(Op" << i << "), DL);\n"; 437 } 438 439 OS << " return FastEmitInst_"; 440 if (Memo.SubRegNo == (unsigned char)~0) { 441 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs); 442 OS << "(" << InstNS << Memo.Name << ", "; 443 OS << InstNS << Memo.RC->getName() << "RegisterClass"; 444 if (!Operands.empty()) 445 OS << ", "; 446 Operands.PrintArguments(OS, *Memo.PhysRegs); 447 OS << ");\n"; 448 } else { 449 OS << "extractsubreg(" << getName(RetVT); 450 OS << ", Op0, "; 451 OS << (unsigned)Memo.SubRegNo; 452 OS << ");\n"; 453 } 454 455 if (HasPred) 456 OS << " }\n"; 457 458 } 459 // Return 0 if none of the predicates were satisfied. 460 if (HasPred) 461 OS << " return 0;\n"; 462 OS << "}\n"; 463 OS << "\n"; 464 } 465 466 // Emit one function for the type that demultiplexes on return type. 467 OS << "unsigned FastEmit_" 468 << getLegalCName(Opcode) << "_" 469 << getLegalCName(getName(VT)) << "_"; 470 Operands.PrintManglingSuffix(OS); 471 OS << "(MVT RetVT"; 472 if (!Operands.empty()) 473 OS << ", "; 474 Operands.PrintParameters(OS); 475 OS << ") {\nswitch (RetVT.SimpleTy) {\n"; 476 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end(); 477 RI != RE; ++RI) { 478 MVT::SimpleValueType RetVT = RI->first; 479 OS << " case " << getName(RetVT) << ": return FastEmit_" 480 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT)) 481 << "_" << getLegalCName(getName(RetVT)) << "_"; 482 Operands.PrintManglingSuffix(OS); 483 OS << "("; 484 Operands.PrintArguments(OS); 485 OS << ");\n"; 486 } 487 OS << " default: return 0;\n}\n}\n\n"; 488 489 } else { 490 // Non-variadic return type. 491 OS << "unsigned FastEmit_" 492 << getLegalCName(Opcode) << "_" 493 << getLegalCName(getName(VT)) << "_"; 494 Operands.PrintManglingSuffix(OS); 495 OS << "(MVT RetVT"; 496 if (!Operands.empty()) 497 OS << ", "; 498 Operands.PrintParameters(OS); 499 OS << ") {\n"; 500 501 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first) 502 << ")\n return 0;\n"; 503 504 const PredMap &PM = RM.begin()->second; 505 bool HasPred = false; 506 507 // Emit code for each possible instruction. There may be 508 // multiple if there are subtarget concerns. 509 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE; 510 ++PI) { 511 std::string PredicateCheck = PI->first; 512 const InstructionMemo &Memo = PI->second; 513 514 if (PredicateCheck.empty()) { 515 assert(!HasPred && 516 "Multiple instructions match, at least one has " 517 "a predicate and at least one doesn't!"); 518 } else { 519 OS << " if (" + PredicateCheck + ") {\n"; 520 OS << " "; 521 HasPred = true; 522 } 523 524 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) { 525 if ((*Memo.PhysRegs)[i] != "") 526 OS << " TII.copyRegToReg(*MBB, MBB->end(), " 527 << (*Memo.PhysRegs)[i] << ", Op" << i << ", " 528 << "TM.getRegisterInfo()->getPhysicalRegisterRegClass(" 529 << (*Memo.PhysRegs)[i] << "), " 530 << "MRI.getRegClass(Op" << i << "), DL);\n"; 531 } 532 533 OS << " return FastEmitInst_"; 534 535 if (Memo.SubRegNo == (unsigned char)~0) { 536 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs); 537 OS << "(" << InstNS << Memo.Name << ", "; 538 OS << InstNS << Memo.RC->getName() << "RegisterClass"; 539 if (!Operands.empty()) 540 OS << ", "; 541 Operands.PrintArguments(OS, *Memo.PhysRegs); 542 OS << ");\n"; 543 } else { 544 OS << "extractsubreg(RetVT, Op0, "; 545 OS << (unsigned)Memo.SubRegNo; 546 OS << ");\n"; 547 } 548 549 if (HasPred) 550 OS << " }\n"; 551 } 552 553 // Return 0 if none of the predicates were satisfied. 554 if (HasPred) 555 OS << " return 0;\n"; 556 OS << "}\n"; 557 OS << "\n"; 558 } 559 } 560 561 // Emit one function for the opcode that demultiplexes based on the type. 562 OS << "unsigned FastEmit_" 563 << getLegalCName(Opcode) << "_"; 564 Operands.PrintManglingSuffix(OS); 565 OS << "(MVT VT, MVT RetVT"; 566 if (!Operands.empty()) 567 OS << ", "; 568 Operands.PrintParameters(OS); 569 OS << ") {\n"; 570 OS << " switch (VT.SimpleTy) {\n"; 571 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end(); 572 TI != TE; ++TI) { 573 MVT::SimpleValueType VT = TI->first; 574 std::string TypeName = getName(VT); 575 OS << " case " << TypeName << ": return FastEmit_" 576 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_"; 577 Operands.PrintManglingSuffix(OS); 578 OS << "(RetVT"; 579 if (!Operands.empty()) 580 OS << ", "; 581 Operands.PrintArguments(OS); 582 OS << ");\n"; 583 } 584 OS << " default: return 0;\n"; 585 OS << " }\n"; 586 OS << "}\n"; 587 OS << "\n"; 588 } 589 590 OS << "// Top-level FastEmit function.\n"; 591 OS << "\n"; 592 593 // Emit one function for the operand signature that demultiplexes based 594 // on opcode and type. 595 OS << "unsigned FastEmit_"; 596 Operands.PrintManglingSuffix(OS); 597 OS << "(MVT VT, MVT RetVT, unsigned Opcode"; 598 if (!Operands.empty()) 599 OS << ", "; 600 Operands.PrintParameters(OS); 601 OS << ") {\n"; 602 OS << " switch (Opcode) {\n"; 603 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end(); 604 I != E; ++I) { 605 const std::string &Opcode = I->first; 606 607 OS << " case " << Opcode << ": return FastEmit_" 608 << getLegalCName(Opcode) << "_"; 609 Operands.PrintManglingSuffix(OS); 610 OS << "(VT, RetVT"; 611 if (!Operands.empty()) 612 OS << ", "; 613 Operands.PrintArguments(OS); 614 OS << ");\n"; 615 } 616 OS << " default: return 0;\n"; 617 OS << " }\n"; 618 OS << "}\n"; 619 OS << "\n"; 620 } 621 } 622 623 void FastISelEmitter::run(raw_ostream &OS) { 624 const CodeGenTarget &Target = CGP.getTargetInfo(); 625 626 // Determine the target's namespace name. 627 std::string InstNS = Target.getInstNamespace() + "::"; 628 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!"); 629 630 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " + 631 Target.getName() + " target", OS); 632 633 FastISelMap F(InstNS); 634 F.CollectPatterns(CGP); 635 F.PrintFunctionDefinitions(OS); 636 } 637 638 FastISelEmitter::FastISelEmitter(RecordKeeper &R) 639 : Records(R), 640 CGP(R) { 641 } 642 643