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 "CodeGenDAGPatterns.h" 21 #include "llvm/ADT/SmallString.h" 22 #include "llvm/ADT/StringSwitch.h" 23 #include "llvm/Support/Debug.h" 24 #include "llvm/Support/ErrorHandling.h" 25 #include "llvm/TableGen/Error.h" 26 #include "llvm/TableGen/Record.h" 27 #include "llvm/TableGen/TableGenBackend.h" 28 using namespace llvm; 29 30 31 /// InstructionMemo - This class holds additional information about an 32 /// instruction needed to emit code for it. 33 /// 34 namespace { 35 struct InstructionMemo { 36 std::string Name; 37 const CodeGenRegisterClass *RC; 38 std::string SubRegNo; 39 std::vector<std::string>* PhysRegs; 40 }; 41 } // End anonymous namespace 42 43 /// ImmPredicateSet - This uniques predicates (represented as a string) and 44 /// gives them unique (small) integer ID's that start at 0. 45 namespace { 46 class ImmPredicateSet { 47 DenseMap<TreePattern *, unsigned> ImmIDs; 48 std::vector<TreePredicateFn> PredsByName; 49 public: 50 51 unsigned getIDFor(TreePredicateFn Pred) { 52 unsigned &Entry = ImmIDs[Pred.getOrigPatFragRecord()]; 53 if (Entry == 0) { 54 PredsByName.push_back(Pred); 55 Entry = PredsByName.size(); 56 } 57 return Entry-1; 58 } 59 60 const TreePredicateFn &getPredicate(unsigned i) { 61 assert(i < PredsByName.size()); 62 return PredsByName[i]; 63 } 64 65 typedef std::vector<TreePredicateFn>::const_iterator iterator; 66 iterator begin() const { return PredsByName.begin(); } 67 iterator end() const { return PredsByName.end(); } 68 69 }; 70 } // End anonymous namespace 71 72 /// OperandsSignature - This class holds a description of a list of operand 73 /// types. It has utility methods for emitting text based on the operands. 74 /// 75 namespace { 76 struct OperandsSignature { 77 class OpKind { 78 enum { OK_Reg, OK_FP, OK_Imm, OK_Invalid = -1 }; 79 char Repr; 80 public: 81 82 OpKind() : Repr(OK_Invalid) {} 83 84 bool operator<(OpKind RHS) const { return Repr < RHS.Repr; } 85 bool operator==(OpKind RHS) const { return Repr == RHS.Repr; } 86 87 static OpKind getReg() { OpKind K; K.Repr = OK_Reg; return K; } 88 static OpKind getFP() { OpKind K; K.Repr = OK_FP; return K; } 89 static OpKind getImm(unsigned V) { 90 assert((unsigned)OK_Imm+V < 128 && 91 "Too many integer predicates for the 'Repr' char"); 92 OpKind K; K.Repr = OK_Imm+V; return K; 93 } 94 95 bool isReg() const { return Repr == OK_Reg; } 96 bool isFP() const { return Repr == OK_FP; } 97 bool isImm() const { return Repr >= OK_Imm; } 98 99 unsigned getImmCode() const { assert(isImm()); return Repr-OK_Imm; } 100 101 void printManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates, 102 bool StripImmCodes) const { 103 if (isReg()) 104 OS << 'r'; 105 else if (isFP()) 106 OS << 'f'; 107 else { 108 OS << 'i'; 109 if (!StripImmCodes) 110 if (unsigned Code = getImmCode()) 111 OS << "_" << ImmPredicates.getPredicate(Code-1).getFnName(); 112 } 113 } 114 }; 115 116 117 SmallVector<OpKind, 3> Operands; 118 119 bool operator<(const OperandsSignature &O) const { 120 return Operands < O.Operands; 121 } 122 bool operator==(const OperandsSignature &O) const { 123 return Operands == O.Operands; 124 } 125 126 bool empty() const { return Operands.empty(); } 127 128 bool hasAnyImmediateCodes() const { 129 for (unsigned i = 0, e = Operands.size(); i != e; ++i) 130 if (Operands[i].isImm() && Operands[i].getImmCode() != 0) 131 return true; 132 return false; 133 } 134 135 /// getWithoutImmCodes - Return a copy of this with any immediate codes forced 136 /// to zero. 137 OperandsSignature getWithoutImmCodes() const { 138 OperandsSignature Result; 139 for (unsigned i = 0, e = Operands.size(); i != e; ++i) 140 if (!Operands[i].isImm()) 141 Result.Operands.push_back(Operands[i]); 142 else 143 Result.Operands.push_back(OpKind::getImm(0)); 144 return Result; 145 } 146 147 void emitImmediatePredicate(raw_ostream &OS, ImmPredicateSet &ImmPredicates) { 148 bool EmittedAnything = false; 149 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 150 if (!Operands[i].isImm()) continue; 151 152 unsigned Code = Operands[i].getImmCode(); 153 if (Code == 0) continue; 154 155 if (EmittedAnything) 156 OS << " &&\n "; 157 158 TreePredicateFn PredFn = ImmPredicates.getPredicate(Code-1); 159 160 // Emit the type check. 161 OS << "VT == " 162 << getEnumName(PredFn.getOrigPatFragRecord()->getTree(0)->getType(0)) 163 << " && "; 164 165 166 OS << PredFn.getFnName() << "(imm" << i <<')'; 167 EmittedAnything = true; 168 } 169 } 170 171 /// initialize - Examine the given pattern and initialize the contents 172 /// of the Operands array accordingly. Return true if all the operands 173 /// are supported, false otherwise. 174 /// 175 bool initialize(TreePatternNode *InstPatNode, const CodeGenTarget &Target, 176 MVT::SimpleValueType VT, 177 ImmPredicateSet &ImmediatePredicates, 178 const CodeGenRegisterClass *OrigDstRC) { 179 if (InstPatNode->isLeaf()) 180 return false; 181 182 if (InstPatNode->getOperator()->getName() == "imm") { 183 Operands.push_back(OpKind::getImm(0)); 184 return true; 185 } 186 187 if (InstPatNode->getOperator()->getName() == "fpimm") { 188 Operands.push_back(OpKind::getFP()); 189 return true; 190 } 191 192 const CodeGenRegisterClass *DstRC = nullptr; 193 194 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) { 195 TreePatternNode *Op = InstPatNode->getChild(i); 196 197 // Handle imm operands specially. 198 if (!Op->isLeaf() && Op->getOperator()->getName() == "imm") { 199 unsigned PredNo = 0; 200 if (!Op->getPredicateFns().empty()) { 201 TreePredicateFn PredFn = Op->getPredicateFns()[0]; 202 // If there is more than one predicate weighing in on this operand 203 // then we don't handle it. This doesn't typically happen for 204 // immediates anyway. 205 if (Op->getPredicateFns().size() > 1 || 206 !PredFn.isImmediatePattern()) 207 return false; 208 // Ignore any instruction with 'FastIselShouldIgnore', these are 209 // not needed and just bloat the fast instruction selector. For 210 // example, X86 doesn't need to generate code to match ADD16ri8 since 211 // ADD16ri will do just fine. 212 Record *Rec = PredFn.getOrigPatFragRecord()->getRecord(); 213 if (Rec->getValueAsBit("FastIselShouldIgnore")) 214 return false; 215 216 PredNo = ImmediatePredicates.getIDFor(PredFn)+1; 217 } 218 219 // Handle unmatched immediate sizes here. 220 //if (Op->getType(0) != VT) 221 // return false; 222 223 Operands.push_back(OpKind::getImm(PredNo)); 224 continue; 225 } 226 227 228 // For now, filter out any operand with a predicate. 229 // For now, filter out any operand with multiple values. 230 if (!Op->getPredicateFns().empty() || Op->getNumTypes() != 1) 231 return false; 232 233 if (!Op->isLeaf()) { 234 if (Op->getOperator()->getName() == "fpimm") { 235 Operands.push_back(OpKind::getFP()); 236 continue; 237 } 238 // For now, ignore other non-leaf nodes. 239 return false; 240 } 241 242 assert(Op->hasTypeSet(0) && "Type infererence not done?"); 243 244 // For now, all the operands must have the same type (if they aren't 245 // immediates). Note that this causes us to reject variable sized shifts 246 // on X86. 247 if (Op->getType(0) != VT) 248 return false; 249 250 DefInit *OpDI = dyn_cast<DefInit>(Op->getLeafValue()); 251 if (!OpDI) 252 return false; 253 Record *OpLeafRec = OpDI->getDef(); 254 255 // For now, the only other thing we accept is register operands. 256 const CodeGenRegisterClass *RC = nullptr; 257 if (OpLeafRec->isSubClassOf("RegisterOperand")) 258 OpLeafRec = OpLeafRec->getValueAsDef("RegClass"); 259 if (OpLeafRec->isSubClassOf("RegisterClass")) 260 RC = &Target.getRegisterClass(OpLeafRec); 261 else if (OpLeafRec->isSubClassOf("Register")) 262 RC = Target.getRegBank().getRegClassForRegister(OpLeafRec); 263 else if (OpLeafRec->isSubClassOf("ValueType")) { 264 RC = OrigDstRC; 265 } else 266 return false; 267 268 // For now, this needs to be a register class of some sort. 269 if (!RC) 270 return false; 271 272 // For now, all the operands must have the same register class or be 273 // a strict subclass of the destination. 274 if (DstRC) { 275 if (DstRC != RC && !DstRC->hasSubClass(RC)) 276 return false; 277 } else 278 DstRC = RC; 279 Operands.push_back(OpKind::getReg()); 280 } 281 return true; 282 } 283 284 void PrintParameters(raw_ostream &OS) const { 285 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 286 if (Operands[i].isReg()) { 287 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill"; 288 } else if (Operands[i].isImm()) { 289 OS << "uint64_t imm" << i; 290 } else if (Operands[i].isFP()) { 291 OS << "const ConstantFP *f" << i; 292 } else { 293 llvm_unreachable("Unknown operand kind!"); 294 } 295 if (i + 1 != e) 296 OS << ", "; 297 } 298 } 299 300 void PrintArguments(raw_ostream &OS, 301 const std::vector<std::string> &PR) const { 302 assert(PR.size() == Operands.size()); 303 bool PrintedArg = false; 304 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 305 if (PR[i] != "") 306 // Implicit physical register operand. 307 continue; 308 309 if (PrintedArg) 310 OS << ", "; 311 if (Operands[i].isReg()) { 312 OS << "Op" << i << ", Op" << i << "IsKill"; 313 PrintedArg = true; 314 } else if (Operands[i].isImm()) { 315 OS << "imm" << i; 316 PrintedArg = true; 317 } else if (Operands[i].isFP()) { 318 OS << "f" << i; 319 PrintedArg = true; 320 } else { 321 llvm_unreachable("Unknown operand kind!"); 322 } 323 } 324 } 325 326 void PrintArguments(raw_ostream &OS) const { 327 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 328 if (Operands[i].isReg()) { 329 OS << "Op" << i << ", Op" << i << "IsKill"; 330 } else if (Operands[i].isImm()) { 331 OS << "imm" << i; 332 } else if (Operands[i].isFP()) { 333 OS << "f" << i; 334 } else { 335 llvm_unreachable("Unknown operand kind!"); 336 } 337 if (i + 1 != e) 338 OS << ", "; 339 } 340 } 341 342 343 void PrintManglingSuffix(raw_ostream &OS, const std::vector<std::string> &PR, 344 ImmPredicateSet &ImmPredicates, 345 bool StripImmCodes = false) const { 346 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 347 if (PR[i] != "") 348 // Implicit physical register operand. e.g. Instruction::Mul expect to 349 // select to a binary op. On x86, mul may take a single operand with 350 // the other operand being implicit. We must emit something that looks 351 // like a binary instruction except for the very inner fastEmitInst_* 352 // call. 353 continue; 354 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes); 355 } 356 } 357 358 void PrintManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates, 359 bool StripImmCodes = false) const { 360 for (unsigned i = 0, e = Operands.size(); i != e; ++i) 361 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes); 362 } 363 }; 364 } // End anonymous namespace 365 366 namespace { 367 class FastISelMap { 368 typedef std::map<std::string, InstructionMemo> PredMap; 369 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap; 370 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap; 371 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap; 372 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap> 373 OperandsOpcodeTypeRetPredMap; 374 375 OperandsOpcodeTypeRetPredMap SimplePatterns; 376 377 std::map<OperandsSignature, std::vector<OperandsSignature> > 378 SignaturesWithConstantForms; 379 380 std::string InstNS; 381 ImmPredicateSet ImmediatePredicates; 382 public: 383 explicit FastISelMap(std::string InstNS); 384 385 void collectPatterns(CodeGenDAGPatterns &CGP); 386 void printImmediatePredicates(raw_ostream &OS); 387 void printFunctionDefinitions(raw_ostream &OS); 388 }; 389 } // End anonymous namespace 390 391 static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) { 392 return CGP.getSDNodeInfo(Op).getEnumName(); 393 } 394 395 static std::string getLegalCName(std::string OpName) { 396 std::string::size_type pos = OpName.find("::"); 397 if (pos != std::string::npos) 398 OpName.replace(pos, 2, "_"); 399 return OpName; 400 } 401 402 FastISelMap::FastISelMap(std::string instns) 403 : InstNS(instns) { 404 } 405 406 static std::string PhyRegForNode(TreePatternNode *Op, 407 const CodeGenTarget &Target) { 408 std::string PhysReg; 409 410 if (!Op->isLeaf()) 411 return PhysReg; 412 413 Record *OpLeafRec = cast<DefInit>(Op->getLeafValue())->getDef(); 414 if (!OpLeafRec->isSubClassOf("Register")) 415 return PhysReg; 416 417 PhysReg += cast<StringInit>(OpLeafRec->getValue("Namespace")->getValue()) 418 ->getValue(); 419 PhysReg += "::"; 420 PhysReg += Target.getRegBank().getReg(OpLeafRec)->getName(); 421 return PhysReg; 422 } 423 424 void FastISelMap::collectPatterns(CodeGenDAGPatterns &CGP) { 425 const CodeGenTarget &Target = CGP.getTargetInfo(); 426 427 // Determine the target's namespace name. 428 InstNS = Target.getInstNamespace() + "::"; 429 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!"); 430 431 // Scan through all the patterns and record the simple ones. 432 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), 433 E = CGP.ptm_end(); I != E; ++I) { 434 const PatternToMatch &Pattern = *I; 435 436 // For now, just look at Instructions, so that we don't have to worry 437 // about emitting multiple instructions for a pattern. 438 TreePatternNode *Dst = Pattern.getDstPattern(); 439 if (Dst->isLeaf()) continue; 440 Record *Op = Dst->getOperator(); 441 if (!Op->isSubClassOf("Instruction")) 442 continue; 443 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op); 444 if (II.Operands.empty()) 445 continue; 446 447 // For now, ignore multi-instruction patterns. 448 bool MultiInsts = false; 449 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) { 450 TreePatternNode *ChildOp = Dst->getChild(i); 451 if (ChildOp->isLeaf()) 452 continue; 453 if (ChildOp->getOperator()->isSubClassOf("Instruction")) { 454 MultiInsts = true; 455 break; 456 } 457 } 458 if (MultiInsts) 459 continue; 460 461 // For now, ignore instructions where the first operand is not an 462 // output register. 463 const CodeGenRegisterClass *DstRC = nullptr; 464 std::string SubRegNo; 465 if (Op->getName() != "EXTRACT_SUBREG") { 466 Record *Op0Rec = II.Operands[0].Rec; 467 if (Op0Rec->isSubClassOf("RegisterOperand")) 468 Op0Rec = Op0Rec->getValueAsDef("RegClass"); 469 if (!Op0Rec->isSubClassOf("RegisterClass")) 470 continue; 471 DstRC = &Target.getRegisterClass(Op0Rec); 472 if (!DstRC) 473 continue; 474 } else { 475 // If this isn't a leaf, then continue since the register classes are 476 // a bit too complicated for now. 477 if (!Dst->getChild(1)->isLeaf()) continue; 478 479 DefInit *SR = dyn_cast<DefInit>(Dst->getChild(1)->getLeafValue()); 480 if (SR) 481 SubRegNo = getQualifiedName(SR->getDef()); 482 else 483 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString(); 484 } 485 486 // Inspect the pattern. 487 TreePatternNode *InstPatNode = Pattern.getSrcPattern(); 488 if (!InstPatNode) continue; 489 if (InstPatNode->isLeaf()) continue; 490 491 // Ignore multiple result nodes for now. 492 if (InstPatNode->getNumTypes() > 1) continue; 493 494 Record *InstPatOp = InstPatNode->getOperator(); 495 std::string OpcodeName = getOpcodeName(InstPatOp, CGP); 496 MVT::SimpleValueType RetVT = MVT::isVoid; 497 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0); 498 MVT::SimpleValueType VT = RetVT; 499 if (InstPatNode->getNumChildren()) { 500 assert(InstPatNode->getChild(0)->getNumTypes() == 1); 501 VT = InstPatNode->getChild(0)->getType(0); 502 } 503 504 // For now, filter out any instructions with predicates. 505 if (!InstPatNode->getPredicateFns().empty()) 506 continue; 507 508 // Check all the operands. 509 OperandsSignature Operands; 510 if (!Operands.initialize(InstPatNode, Target, VT, ImmediatePredicates, 511 DstRC)) 512 continue; 513 514 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>(); 515 if (InstPatNode->getOperator()->getName() == "imm" || 516 InstPatNode->getOperator()->getName() == "fpimm") 517 PhysRegInputs->push_back(""); 518 else { 519 // Compute the PhysRegs used by the given pattern, and check that 520 // the mapping from the src to dst patterns is simple. 521 bool FoundNonSimplePattern = false; 522 unsigned DstIndex = 0; 523 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) { 524 std::string PhysReg = PhyRegForNode(InstPatNode->getChild(i), Target); 525 if (PhysReg.empty()) { 526 if (DstIndex >= Dst->getNumChildren() || 527 Dst->getChild(DstIndex)->getName() != 528 InstPatNode->getChild(i)->getName()) { 529 FoundNonSimplePattern = true; 530 break; 531 } 532 ++DstIndex; 533 } 534 535 PhysRegInputs->push_back(PhysReg); 536 } 537 538 if (Op->getName() != "EXTRACT_SUBREG" && DstIndex < Dst->getNumChildren()) 539 FoundNonSimplePattern = true; 540 541 if (FoundNonSimplePattern) 542 continue; 543 } 544 545 // Check if the operands match one of the patterns handled by FastISel. 546 std::string ManglingSuffix; 547 raw_string_ostream SuffixOS(ManglingSuffix); 548 Operands.PrintManglingSuffix(SuffixOS, ImmediatePredicates, true); 549 SuffixOS.flush(); 550 if (!StringSwitch<bool>(ManglingSuffix) 551 .Cases("", "r", "rr", "ri", "rf", true) 552 .Cases("rri", "i", "f", true) 553 .Default(false)) 554 continue; 555 556 // Get the predicate that guards this pattern. 557 std::string PredicateCheck = Pattern.getPredicateCheck(); 558 559 // Ok, we found a pattern that we can handle. Remember it. 560 InstructionMemo Memo = { 561 Pattern.getDstPattern()->getOperator()->getName(), 562 DstRC, 563 SubRegNo, 564 PhysRegInputs 565 }; 566 567 if (SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck)) 568 PrintFatalError(Pattern.getSrcRecord()->getLoc(), 569 "Duplicate record in FastISel table!"); 570 571 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo; 572 573 // If any of the operands were immediates with predicates on them, strip 574 // them down to a signature that doesn't have predicates so that we can 575 // associate them with the stripped predicate version. 576 if (Operands.hasAnyImmediateCodes()) { 577 SignaturesWithConstantForms[Operands.getWithoutImmCodes()] 578 .push_back(Operands); 579 } 580 } 581 } 582 583 void FastISelMap::printImmediatePredicates(raw_ostream &OS) { 584 if (ImmediatePredicates.begin() == ImmediatePredicates.end()) 585 return; 586 587 OS << "\n// FastEmit Immediate Predicate functions.\n"; 588 for (ImmPredicateSet::iterator I = ImmediatePredicates.begin(), 589 E = ImmediatePredicates.end(); I != E; ++I) { 590 OS << "static bool " << I->getFnName() << "(int64_t Imm) {\n"; 591 OS << I->getImmediatePredicateCode() << "\n}\n"; 592 } 593 594 OS << "\n\n"; 595 } 596 597 598 void FastISelMap::printFunctionDefinitions(raw_ostream &OS) { 599 // Now emit code for all the patterns that we collected. 600 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(), 601 OE = SimplePatterns.end(); OI != OE; ++OI) { 602 const OperandsSignature &Operands = OI->first; 603 const OpcodeTypeRetPredMap &OTM = OI->second; 604 605 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end(); 606 I != E; ++I) { 607 const std::string &Opcode = I->first; 608 const TypeRetPredMap &TM = I->second; 609 610 OS << "// FastEmit functions for " << Opcode << ".\n"; 611 OS << "\n"; 612 613 // Emit one function for each opcode,type pair. 614 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end(); 615 TI != TE; ++TI) { 616 MVT::SimpleValueType VT = TI->first; 617 const RetPredMap &RM = TI->second; 618 if (RM.size() != 1) { 619 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end(); 620 RI != RE; ++RI) { 621 MVT::SimpleValueType RetVT = RI->first; 622 const PredMap &PM = RI->second; 623 bool HasPred = false; 624 625 OS << "unsigned fastEmit_" 626 << getLegalCName(Opcode) 627 << "_" << getLegalCName(getName(VT)) 628 << "_" << getLegalCName(getName(RetVT)) << "_"; 629 Operands.PrintManglingSuffix(OS, ImmediatePredicates); 630 OS << "("; 631 Operands.PrintParameters(OS); 632 OS << ") {\n"; 633 634 // Emit code for each possible instruction. There may be 635 // multiple if there are subtarget concerns. 636 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); 637 PI != PE; ++PI) { 638 std::string PredicateCheck = PI->first; 639 const InstructionMemo &Memo = PI->second; 640 641 if (PredicateCheck.empty()) { 642 assert(!HasPred && 643 "Multiple instructions match, at least one has " 644 "a predicate and at least one doesn't!"); 645 } else { 646 OS << " if (" + PredicateCheck + ") {\n"; 647 OS << " "; 648 HasPred = true; 649 } 650 651 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) { 652 if ((*Memo.PhysRegs)[i] != "") 653 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, " 654 << "TII.get(TargetOpcode::COPY), " 655 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n"; 656 } 657 658 OS << " return fastEmitInst_"; 659 if (Memo.SubRegNo.empty()) { 660 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs, 661 ImmediatePredicates, true); 662 OS << "(" << InstNS << Memo.Name << ", "; 663 OS << "&" << InstNS << Memo.RC->getName() << "RegClass"; 664 if (!Operands.empty()) 665 OS << ", "; 666 Operands.PrintArguments(OS, *Memo.PhysRegs); 667 OS << ");\n"; 668 } else { 669 OS << "extractsubreg(" << getName(RetVT); 670 OS << ", Op0, Op0IsKill, " << Memo.SubRegNo << ");\n"; 671 } 672 673 if (HasPred) 674 OS << " }\n"; 675 676 } 677 // Return 0 if none of the predicates were satisfied. 678 if (HasPred) 679 OS << " return 0;\n"; 680 OS << "}\n"; 681 OS << "\n"; 682 } 683 684 // Emit one function for the type that demultiplexes on return type. 685 OS << "unsigned fastEmit_" 686 << getLegalCName(Opcode) << "_" 687 << getLegalCName(getName(VT)) << "_"; 688 Operands.PrintManglingSuffix(OS, ImmediatePredicates); 689 OS << "(MVT RetVT"; 690 if (!Operands.empty()) 691 OS << ", "; 692 Operands.PrintParameters(OS); 693 OS << ") {\nswitch (RetVT.SimpleTy) {\n"; 694 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end(); 695 RI != RE; ++RI) { 696 MVT::SimpleValueType RetVT = RI->first; 697 OS << " case " << getName(RetVT) << ": return fastEmit_" 698 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT)) 699 << "_" << getLegalCName(getName(RetVT)) << "_"; 700 Operands.PrintManglingSuffix(OS, ImmediatePredicates); 701 OS << "("; 702 Operands.PrintArguments(OS); 703 OS << ");\n"; 704 } 705 OS << " default: return 0;\n}\n}\n\n"; 706 707 } else { 708 // Non-variadic return type. 709 OS << "unsigned fastEmit_" 710 << getLegalCName(Opcode) << "_" 711 << getLegalCName(getName(VT)) << "_"; 712 Operands.PrintManglingSuffix(OS, ImmediatePredicates); 713 OS << "(MVT RetVT"; 714 if (!Operands.empty()) 715 OS << ", "; 716 Operands.PrintParameters(OS); 717 OS << ") {\n"; 718 719 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first) 720 << ")\n return 0;\n"; 721 722 const PredMap &PM = RM.begin()->second; 723 bool HasPred = false; 724 725 // Emit code for each possible instruction. There may be 726 // multiple if there are subtarget concerns. 727 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE; 728 ++PI) { 729 std::string PredicateCheck = PI->first; 730 const InstructionMemo &Memo = PI->second; 731 732 if (PredicateCheck.empty()) { 733 assert(!HasPred && 734 "Multiple instructions match, at least one has " 735 "a predicate and at least one doesn't!"); 736 } else { 737 OS << " if (" + PredicateCheck + ") {\n"; 738 OS << " "; 739 HasPred = true; 740 } 741 742 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) { 743 if ((*Memo.PhysRegs)[i] != "") 744 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, " 745 << "TII.get(TargetOpcode::COPY), " 746 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n"; 747 } 748 749 OS << " return fastEmitInst_"; 750 751 if (Memo.SubRegNo.empty()) { 752 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs, 753 ImmediatePredicates, true); 754 OS << "(" << InstNS << Memo.Name << ", "; 755 OS << "&" << InstNS << Memo.RC->getName() << "RegClass"; 756 if (!Operands.empty()) 757 OS << ", "; 758 Operands.PrintArguments(OS, *Memo.PhysRegs); 759 OS << ");\n"; 760 } else { 761 OS << "extractsubreg(RetVT, Op0, Op0IsKill, "; 762 OS << Memo.SubRegNo; 763 OS << ");\n"; 764 } 765 766 if (HasPred) 767 OS << " }\n"; 768 } 769 770 // Return 0 if none of the predicates were satisfied. 771 if (HasPred) 772 OS << " return 0;\n"; 773 OS << "}\n"; 774 OS << "\n"; 775 } 776 } 777 778 // Emit one function for the opcode that demultiplexes based on the type. 779 OS << "unsigned fastEmit_" 780 << getLegalCName(Opcode) << "_"; 781 Operands.PrintManglingSuffix(OS, ImmediatePredicates); 782 OS << "(MVT VT, MVT RetVT"; 783 if (!Operands.empty()) 784 OS << ", "; 785 Operands.PrintParameters(OS); 786 OS << ") {\n"; 787 OS << " switch (VT.SimpleTy) {\n"; 788 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end(); 789 TI != TE; ++TI) { 790 MVT::SimpleValueType VT = TI->first; 791 std::string TypeName = getName(VT); 792 OS << " case " << TypeName << ": return fastEmit_" 793 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_"; 794 Operands.PrintManglingSuffix(OS, ImmediatePredicates); 795 OS << "(RetVT"; 796 if (!Operands.empty()) 797 OS << ", "; 798 Operands.PrintArguments(OS); 799 OS << ");\n"; 800 } 801 OS << " default: return 0;\n"; 802 OS << " }\n"; 803 OS << "}\n"; 804 OS << "\n"; 805 } 806 807 OS << "// Top-level FastEmit function.\n"; 808 OS << "\n"; 809 810 // Emit one function for the operand signature that demultiplexes based 811 // on opcode and type. 812 OS << "unsigned fastEmit_"; 813 Operands.PrintManglingSuffix(OS, ImmediatePredicates); 814 OS << "(MVT VT, MVT RetVT, unsigned Opcode"; 815 if (!Operands.empty()) 816 OS << ", "; 817 Operands.PrintParameters(OS); 818 OS << ") "; 819 if (!Operands.hasAnyImmediateCodes()) 820 OS << "override "; 821 OS << "{\n"; 822 823 // If there are any forms of this signature available that operate on 824 // constrained forms of the immediate (e.g., 32-bit sext immediate in a 825 // 64-bit operand), check them first. 826 827 std::map<OperandsSignature, std::vector<OperandsSignature> >::iterator MI 828 = SignaturesWithConstantForms.find(Operands); 829 if (MI != SignaturesWithConstantForms.end()) { 830 // Unique any duplicates out of the list. 831 std::sort(MI->second.begin(), MI->second.end()); 832 MI->second.erase(std::unique(MI->second.begin(), MI->second.end()), 833 MI->second.end()); 834 835 // Check each in order it was seen. It would be nice to have a good 836 // relative ordering between them, but we're not going for optimality 837 // here. 838 for (unsigned i = 0, e = MI->second.size(); i != e; ++i) { 839 OS << " if ("; 840 MI->second[i].emitImmediatePredicate(OS, ImmediatePredicates); 841 OS << ")\n if (unsigned Reg = fastEmit_"; 842 MI->second[i].PrintManglingSuffix(OS, ImmediatePredicates); 843 OS << "(VT, RetVT, Opcode"; 844 if (!MI->second[i].empty()) 845 OS << ", "; 846 MI->second[i].PrintArguments(OS); 847 OS << "))\n return Reg;\n\n"; 848 } 849 850 // Done with this, remove it. 851 SignaturesWithConstantForms.erase(MI); 852 } 853 854 OS << " switch (Opcode) {\n"; 855 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end(); 856 I != E; ++I) { 857 const std::string &Opcode = I->first; 858 859 OS << " case " << Opcode << ": return fastEmit_" 860 << getLegalCName(Opcode) << "_"; 861 Operands.PrintManglingSuffix(OS, ImmediatePredicates); 862 OS << "(VT, RetVT"; 863 if (!Operands.empty()) 864 OS << ", "; 865 Operands.PrintArguments(OS); 866 OS << ");\n"; 867 } 868 OS << " default: return 0;\n"; 869 OS << " }\n"; 870 OS << "}\n"; 871 OS << "\n"; 872 } 873 874 // TODO: SignaturesWithConstantForms should be empty here. 875 } 876 877 namespace llvm { 878 879 void EmitFastISel(RecordKeeper &RK, raw_ostream &OS) { 880 CodeGenDAGPatterns CGP(RK); 881 const CodeGenTarget &Target = CGP.getTargetInfo(); 882 emitSourceFileHeader("\"Fast\" Instruction Selector for the " + 883 Target.getName() + " target", OS); 884 885 // Determine the target's namespace name. 886 std::string InstNS = Target.getInstNamespace() + "::"; 887 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!"); 888 889 FastISelMap F(InstNS); 890 F.collectPatterns(CGP); 891 F.printImmediatePredicates(OS); 892 F.printFunctionDefinitions(OS); 893 } 894 895 } // End llvm namespace 896