1 //===- SelectionDAGISel.cpp - Implement the SelectionDAGISel class --------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This implements the SelectionDAGISel class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/CodeGen/SelectionDAGISel.h" 14 #include "ScheduleDAGSDNodes.h" 15 #include "SelectionDAGBuilder.h" 16 #include "llvm/ADT/APInt.h" 17 #include "llvm/ADT/DenseMap.h" 18 #include "llvm/ADT/PostOrderIterator.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/ADT/SmallPtrSet.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/ADT/Statistic.h" 23 #include "llvm/ADT/StringRef.h" 24 #include "llvm/Analysis/AliasAnalysis.h" 25 #include "llvm/Analysis/BranchProbabilityInfo.h" 26 #include "llvm/Analysis/CFG.h" 27 #include "llvm/Analysis/EHPersonalities.h" 28 #include "llvm/Analysis/LazyBlockFrequencyInfo.h" 29 #include "llvm/Analysis/LegacyDivergenceAnalysis.h" 30 #include "llvm/Analysis/OptimizationRemarkEmitter.h" 31 #include "llvm/Analysis/ProfileSummaryInfo.h" 32 #include "llvm/Analysis/TargetLibraryInfo.h" 33 #include "llvm/Analysis/TargetTransformInfo.h" 34 #include "llvm/CodeGen/CodeGenCommonISel.h" 35 #include "llvm/CodeGen/FastISel.h" 36 #include "llvm/CodeGen/FunctionLoweringInfo.h" 37 #include "llvm/CodeGen/GCMetadata.h" 38 #include "llvm/CodeGen/ISDOpcodes.h" 39 #include "llvm/CodeGen/MachineBasicBlock.h" 40 #include "llvm/CodeGen/MachineFrameInfo.h" 41 #include "llvm/CodeGen/MachineFunction.h" 42 #include "llvm/CodeGen/MachineFunctionPass.h" 43 #include "llvm/CodeGen/MachineInstr.h" 44 #include "llvm/CodeGen/MachineInstrBuilder.h" 45 #include "llvm/CodeGen/MachineMemOperand.h" 46 #include "llvm/CodeGen/MachineModuleInfo.h" 47 #include "llvm/CodeGen/MachineOperand.h" 48 #include "llvm/CodeGen/MachinePassRegistry.h" 49 #include "llvm/CodeGen/MachineRegisterInfo.h" 50 #include "llvm/CodeGen/SchedulerRegistry.h" 51 #include "llvm/CodeGen/SelectionDAG.h" 52 #include "llvm/CodeGen/SelectionDAGNodes.h" 53 #include "llvm/CodeGen/StackProtector.h" 54 #include "llvm/CodeGen/SwiftErrorValueTracking.h" 55 #include "llvm/CodeGen/TargetInstrInfo.h" 56 #include "llvm/CodeGen/TargetLowering.h" 57 #include "llvm/CodeGen/TargetRegisterInfo.h" 58 #include "llvm/CodeGen/TargetSubtargetInfo.h" 59 #include "llvm/CodeGen/ValueTypes.h" 60 #include "llvm/IR/BasicBlock.h" 61 #include "llvm/IR/Constants.h" 62 #include "llvm/IR/DataLayout.h" 63 #include "llvm/IR/DebugInfoMetadata.h" 64 #include "llvm/IR/DebugLoc.h" 65 #include "llvm/IR/DiagnosticInfo.h" 66 #include "llvm/IR/Function.h" 67 #include "llvm/IR/InlineAsm.h" 68 #include "llvm/IR/InstIterator.h" 69 #include "llvm/IR/Instruction.h" 70 #include "llvm/IR/Instructions.h" 71 #include "llvm/IR/IntrinsicInst.h" 72 #include "llvm/IR/Intrinsics.h" 73 #include "llvm/IR/IntrinsicsWebAssembly.h" 74 #include "llvm/IR/Metadata.h" 75 #include "llvm/IR/Statepoint.h" 76 #include "llvm/IR/Type.h" 77 #include "llvm/IR/User.h" 78 #include "llvm/IR/Value.h" 79 #include "llvm/InitializePasses.h" 80 #include "llvm/MC/MCInstrDesc.h" 81 #include "llvm/Pass.h" 82 #include "llvm/Support/BranchProbability.h" 83 #include "llvm/Support/Casting.h" 84 #include "llvm/Support/CodeGen.h" 85 #include "llvm/Support/CommandLine.h" 86 #include "llvm/Support/Compiler.h" 87 #include "llvm/Support/Debug.h" 88 #include "llvm/Support/ErrorHandling.h" 89 #include "llvm/Support/KnownBits.h" 90 #include "llvm/Support/MachineValueType.h" 91 #include "llvm/Support/Timer.h" 92 #include "llvm/Support/raw_ostream.h" 93 #include "llvm/Target/TargetIntrinsicInfo.h" 94 #include "llvm/Target/TargetMachine.h" 95 #include "llvm/Target/TargetOptions.h" 96 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 97 #include <algorithm> 98 #include <cassert> 99 #include <cstdint> 100 #include <iterator> 101 #include <limits> 102 #include <memory> 103 #include <string> 104 #include <utility> 105 #include <vector> 106 107 using namespace llvm; 108 109 #define DEBUG_TYPE "isel" 110 111 STATISTIC(NumFastIselFailures, "Number of instructions fast isel failed on"); 112 STATISTIC(NumFastIselSuccess, "Number of instructions fast isel selected"); 113 STATISTIC(NumFastIselBlocks, "Number of blocks selected entirely by fast isel"); 114 STATISTIC(NumDAGBlocks, "Number of blocks selected using DAG"); 115 STATISTIC(NumDAGIselRetries,"Number of times dag isel has to try another path"); 116 STATISTIC(NumEntryBlocks, "Number of entry blocks encountered"); 117 STATISTIC(NumFastIselFailLowerArguments, 118 "Number of entry blocks where fast isel failed to lower arguments"); 119 120 static cl::opt<int> EnableFastISelAbort( 121 "fast-isel-abort", cl::Hidden, 122 cl::desc("Enable abort calls when \"fast\" instruction selection " 123 "fails to lower an instruction: 0 disable the abort, 1 will " 124 "abort but for args, calls and terminators, 2 will also " 125 "abort for argument lowering, and 3 will never fallback " 126 "to SelectionDAG.")); 127 128 static cl::opt<bool> EnableFastISelFallbackReport( 129 "fast-isel-report-on-fallback", cl::Hidden, 130 cl::desc("Emit a diagnostic when \"fast\" instruction selection " 131 "falls back to SelectionDAG.")); 132 133 static cl::opt<bool> 134 UseMBPI("use-mbpi", 135 cl::desc("use Machine Branch Probability Info"), 136 cl::init(true), cl::Hidden); 137 138 #ifndef NDEBUG 139 static cl::opt<std::string> 140 FilterDAGBasicBlockName("filter-view-dags", cl::Hidden, 141 cl::desc("Only display the basic block whose name " 142 "matches this for all view-*-dags options")); 143 static cl::opt<bool> 144 ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden, 145 cl::desc("Pop up a window to show dags before the first " 146 "dag combine pass")); 147 static cl::opt<bool> 148 ViewLegalizeTypesDAGs("view-legalize-types-dags", cl::Hidden, 149 cl::desc("Pop up a window to show dags before legalize types")); 150 static cl::opt<bool> 151 ViewDAGCombineLT("view-dag-combine-lt-dags", cl::Hidden, 152 cl::desc("Pop up a window to show dags before the post " 153 "legalize types dag combine pass")); 154 static cl::opt<bool> 155 ViewLegalizeDAGs("view-legalize-dags", cl::Hidden, 156 cl::desc("Pop up a window to show dags before legalize")); 157 static cl::opt<bool> 158 ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden, 159 cl::desc("Pop up a window to show dags before the second " 160 "dag combine pass")); 161 static cl::opt<bool> 162 ViewISelDAGs("view-isel-dags", cl::Hidden, 163 cl::desc("Pop up a window to show isel dags as they are selected")); 164 static cl::opt<bool> 165 ViewSchedDAGs("view-sched-dags", cl::Hidden, 166 cl::desc("Pop up a window to show sched dags as they are processed")); 167 static cl::opt<bool> 168 ViewSUnitDAGs("view-sunit-dags", cl::Hidden, 169 cl::desc("Pop up a window to show SUnit dags after they are processed")); 170 #else 171 static const bool ViewDAGCombine1 = false, ViewLegalizeTypesDAGs = false, 172 ViewDAGCombineLT = false, ViewLegalizeDAGs = false, 173 ViewDAGCombine2 = false, ViewISelDAGs = false, 174 ViewSchedDAGs = false, ViewSUnitDAGs = false; 175 #endif 176 177 //===---------------------------------------------------------------------===// 178 /// 179 /// RegisterScheduler class - Track the registration of instruction schedulers. 180 /// 181 //===---------------------------------------------------------------------===// 182 MachinePassRegistry<RegisterScheduler::FunctionPassCtor> 183 RegisterScheduler::Registry; 184 185 //===---------------------------------------------------------------------===// 186 /// 187 /// ISHeuristic command line option for instruction schedulers. 188 /// 189 //===---------------------------------------------------------------------===// 190 static cl::opt<RegisterScheduler::FunctionPassCtor, false, 191 RegisterPassParser<RegisterScheduler>> 192 ISHeuristic("pre-RA-sched", 193 cl::init(&createDefaultScheduler), cl::Hidden, 194 cl::desc("Instruction schedulers available (before register" 195 " allocation):")); 196 197 static RegisterScheduler 198 defaultListDAGScheduler("default", "Best scheduler for the target", 199 createDefaultScheduler); 200 201 namespace llvm { 202 203 //===--------------------------------------------------------------------===// 204 /// This class is used by SelectionDAGISel to temporarily override 205 /// the optimization level on a per-function basis. 206 class OptLevelChanger { 207 SelectionDAGISel &IS; 208 CodeGenOpt::Level SavedOptLevel; 209 bool SavedFastISel; 210 211 public: 212 OptLevelChanger(SelectionDAGISel &ISel, 213 CodeGenOpt::Level NewOptLevel) : IS(ISel) { 214 SavedOptLevel = IS.OptLevel; 215 SavedFastISel = IS.TM.Options.EnableFastISel; 216 if (NewOptLevel == SavedOptLevel) 217 return; 218 IS.OptLevel = NewOptLevel; 219 IS.TM.setOptLevel(NewOptLevel); 220 LLVM_DEBUG(dbgs() << "\nChanging optimization level for Function " 221 << IS.MF->getFunction().getName() << "\n"); 222 LLVM_DEBUG(dbgs() << "\tBefore: -O" << SavedOptLevel << " ; After: -O" 223 << NewOptLevel << "\n"); 224 if (NewOptLevel == CodeGenOpt::None) { 225 IS.TM.setFastISel(IS.TM.getO0WantsFastISel()); 226 LLVM_DEBUG( 227 dbgs() << "\tFastISel is " 228 << (IS.TM.Options.EnableFastISel ? "enabled" : "disabled") 229 << "\n"); 230 } 231 } 232 233 ~OptLevelChanger() { 234 if (IS.OptLevel == SavedOptLevel) 235 return; 236 LLVM_DEBUG(dbgs() << "\nRestoring optimization level for Function " 237 << IS.MF->getFunction().getName() << "\n"); 238 LLVM_DEBUG(dbgs() << "\tBefore: -O" << IS.OptLevel << " ; After: -O" 239 << SavedOptLevel << "\n"); 240 IS.OptLevel = SavedOptLevel; 241 IS.TM.setOptLevel(SavedOptLevel); 242 IS.TM.setFastISel(SavedFastISel); 243 } 244 }; 245 246 //===--------------------------------------------------------------------===// 247 /// createDefaultScheduler - This creates an instruction scheduler appropriate 248 /// for the target. 249 ScheduleDAGSDNodes* createDefaultScheduler(SelectionDAGISel *IS, 250 CodeGenOpt::Level OptLevel) { 251 const TargetLowering *TLI = IS->TLI; 252 const TargetSubtargetInfo &ST = IS->MF->getSubtarget(); 253 254 // Try first to see if the Target has its own way of selecting a scheduler 255 if (auto *SchedulerCtor = ST.getDAGScheduler(OptLevel)) { 256 return SchedulerCtor(IS, OptLevel); 257 } 258 259 if (OptLevel == CodeGenOpt::None || 260 (ST.enableMachineScheduler() && ST.enableMachineSchedDefaultSched()) || 261 TLI->getSchedulingPreference() == Sched::Source) 262 return createSourceListDAGScheduler(IS, OptLevel); 263 if (TLI->getSchedulingPreference() == Sched::RegPressure) 264 return createBURRListDAGScheduler(IS, OptLevel); 265 if (TLI->getSchedulingPreference() == Sched::Hybrid) 266 return createHybridListDAGScheduler(IS, OptLevel); 267 if (TLI->getSchedulingPreference() == Sched::VLIW) 268 return createVLIWDAGScheduler(IS, OptLevel); 269 if (TLI->getSchedulingPreference() == Sched::Fast) 270 return createFastDAGScheduler(IS, OptLevel); 271 if (TLI->getSchedulingPreference() == Sched::Linearize) 272 return createDAGLinearizer(IS, OptLevel); 273 assert(TLI->getSchedulingPreference() == Sched::ILP && 274 "Unknown sched type!"); 275 return createILPListDAGScheduler(IS, OptLevel); 276 } 277 278 } // end namespace llvm 279 280 // EmitInstrWithCustomInserter - This method should be implemented by targets 281 // that mark instructions with the 'usesCustomInserter' flag. These 282 // instructions are special in various ways, which require special support to 283 // insert. The specified MachineInstr is created but not inserted into any 284 // basic blocks, and this method is called to expand it into a sequence of 285 // instructions, potentially also creating new basic blocks and control flow. 286 // When new basic blocks are inserted and the edges from MBB to its successors 287 // are modified, the method should insert pairs of <OldSucc, NewSucc> into the 288 // DenseMap. 289 MachineBasicBlock * 290 TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, 291 MachineBasicBlock *MBB) const { 292 #ifndef NDEBUG 293 dbgs() << "If a target marks an instruction with " 294 "'usesCustomInserter', it must implement " 295 "TargetLowering::EmitInstrWithCustomInserter!\n"; 296 #endif 297 llvm_unreachable(nullptr); 298 } 299 300 void TargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI, 301 SDNode *Node) const { 302 assert(!MI.hasPostISelHook() && 303 "If a target marks an instruction with 'hasPostISelHook', " 304 "it must implement TargetLowering::AdjustInstrPostInstrSelection!"); 305 } 306 307 //===----------------------------------------------------------------------===// 308 // SelectionDAGISel code 309 //===----------------------------------------------------------------------===// 310 311 SelectionDAGISel::SelectionDAGISel(TargetMachine &tm, CodeGenOpt::Level OL) 312 : MachineFunctionPass(ID), TM(tm), FuncInfo(new FunctionLoweringInfo()), 313 SwiftError(new SwiftErrorValueTracking()), 314 CurDAG(new SelectionDAG(tm, OL)), 315 SDB(std::make_unique<SelectionDAGBuilder>(*CurDAG, *FuncInfo, *SwiftError, 316 OL)), 317 OptLevel(OL) { 318 initializeGCModuleInfoPass(*PassRegistry::getPassRegistry()); 319 initializeBranchProbabilityInfoWrapperPassPass( 320 *PassRegistry::getPassRegistry()); 321 initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry()); 322 initializeTargetLibraryInfoWrapperPassPass(*PassRegistry::getPassRegistry()); 323 } 324 325 SelectionDAGISel::~SelectionDAGISel() { 326 delete CurDAG; 327 delete SwiftError; 328 } 329 330 void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const { 331 if (OptLevel != CodeGenOpt::None) 332 AU.addRequired<AAResultsWrapperPass>(); 333 AU.addRequired<GCModuleInfo>(); 334 AU.addRequired<StackProtector>(); 335 AU.addPreserved<GCModuleInfo>(); 336 AU.addRequired<TargetLibraryInfoWrapperPass>(); 337 AU.addRequired<TargetTransformInfoWrapperPass>(); 338 if (UseMBPI && OptLevel != CodeGenOpt::None) 339 AU.addRequired<BranchProbabilityInfoWrapperPass>(); 340 AU.addRequired<ProfileSummaryInfoWrapperPass>(); 341 if (OptLevel != CodeGenOpt::None) 342 LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU); 343 MachineFunctionPass::getAnalysisUsage(AU); 344 } 345 346 static void computeUsesMSVCFloatingPoint(const Triple &TT, const Function &F, 347 MachineModuleInfo &MMI) { 348 // Only needed for MSVC 349 if (!TT.isWindowsMSVCEnvironment()) 350 return; 351 352 // If it's already set, nothing to do. 353 if (MMI.usesMSVCFloatingPoint()) 354 return; 355 356 for (const Instruction &I : instructions(F)) { 357 if (I.getType()->isFPOrFPVectorTy()) { 358 MMI.setUsesMSVCFloatingPoint(true); 359 return; 360 } 361 for (const auto &Op : I.operands()) { 362 if (Op->getType()->isFPOrFPVectorTy()) { 363 MMI.setUsesMSVCFloatingPoint(true); 364 return; 365 } 366 } 367 } 368 } 369 370 bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) { 371 // If we already selected that function, we do not need to run SDISel. 372 if (mf.getProperties().hasProperty( 373 MachineFunctionProperties::Property::Selected)) 374 return false; 375 // Do some sanity-checking on the command-line options. 376 assert((!EnableFastISelAbort || TM.Options.EnableFastISel) && 377 "-fast-isel-abort > 0 requires -fast-isel"); 378 379 const Function &Fn = mf.getFunction(); 380 MF = &mf; 381 382 // Decide what flavour of variable location debug-info will be used, before 383 // we change the optimisation level. 384 UseInstrRefDebugInfo = mf.useDebugInstrRef(); 385 CurDAG->useInstrRefDebugInfo(UseInstrRefDebugInfo); 386 387 // Reset the target options before resetting the optimization 388 // level below. 389 // FIXME: This is a horrible hack and should be processed via 390 // codegen looking at the optimization level explicitly when 391 // it wants to look at it. 392 TM.resetTargetOptions(Fn); 393 // Reset OptLevel to None for optnone functions. 394 CodeGenOpt::Level NewOptLevel = OptLevel; 395 if (OptLevel != CodeGenOpt::None && skipFunction(Fn)) 396 NewOptLevel = CodeGenOpt::None; 397 OptLevelChanger OLC(*this, NewOptLevel); 398 399 TII = MF->getSubtarget().getInstrInfo(); 400 TLI = MF->getSubtarget().getTargetLowering(); 401 RegInfo = &MF->getRegInfo(); 402 LibInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(Fn); 403 GFI = Fn.hasGC() ? &getAnalysis<GCModuleInfo>().getFunctionInfo(Fn) : nullptr; 404 ORE = std::make_unique<OptimizationRemarkEmitter>(&Fn); 405 auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); 406 BlockFrequencyInfo *BFI = nullptr; 407 if (PSI && PSI->hasProfileSummary() && OptLevel != CodeGenOpt::None) 408 BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI(); 409 410 LLVM_DEBUG(dbgs() << "\n\n\n=== " << Fn.getName() << "\n"); 411 412 CurDAG->init(*MF, *ORE, this, LibInfo, 413 getAnalysisIfAvailable<LegacyDivergenceAnalysis>(), PSI, BFI); 414 FuncInfo->set(Fn, *MF, CurDAG); 415 SwiftError->setFunction(*MF); 416 417 // Now get the optional analyzes if we want to. 418 // This is based on the possibly changed OptLevel (after optnone is taken 419 // into account). That's unfortunate but OK because it just means we won't 420 // ask for passes that have been required anyway. 421 422 if (UseMBPI && OptLevel != CodeGenOpt::None) 423 FuncInfo->BPI = &getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI(); 424 else 425 FuncInfo->BPI = nullptr; 426 427 if (OptLevel != CodeGenOpt::None) 428 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); 429 else 430 AA = nullptr; 431 432 SDB->init(GFI, AA, LibInfo); 433 434 MF->setHasInlineAsm(false); 435 436 FuncInfo->SplitCSR = false; 437 438 // We split CSR if the target supports it for the given function 439 // and the function has only return exits. 440 if (OptLevel != CodeGenOpt::None && TLI->supportSplitCSR(MF)) { 441 FuncInfo->SplitCSR = true; 442 443 // Collect all the return blocks. 444 for (const BasicBlock &BB : Fn) { 445 if (!succ_empty(&BB)) 446 continue; 447 448 const Instruction *Term = BB.getTerminator(); 449 if (isa<UnreachableInst>(Term) || isa<ReturnInst>(Term)) 450 continue; 451 452 // Bail out if the exit block is not Return nor Unreachable. 453 FuncInfo->SplitCSR = false; 454 break; 455 } 456 } 457 458 MachineBasicBlock *EntryMBB = &MF->front(); 459 if (FuncInfo->SplitCSR) 460 // This performs initialization so lowering for SplitCSR will be correct. 461 TLI->initializeSplitCSR(EntryMBB); 462 463 SelectAllBasicBlocks(Fn); 464 if (FastISelFailed && EnableFastISelFallbackReport) { 465 DiagnosticInfoISelFallback DiagFallback(Fn); 466 Fn.getContext().diagnose(DiagFallback); 467 } 468 469 // Replace forward-declared registers with the registers containing 470 // the desired value. 471 // Note: it is important that this happens **before** the call to 472 // EmitLiveInCopies, since implementations can skip copies of unused 473 // registers. If we don't apply the reg fixups before, some registers may 474 // appear as unused and will be skipped, resulting in bad MI. 475 MachineRegisterInfo &MRI = MF->getRegInfo(); 476 for (DenseMap<Register, Register>::iterator I = FuncInfo->RegFixups.begin(), 477 E = FuncInfo->RegFixups.end(); 478 I != E; ++I) { 479 Register From = I->first; 480 Register To = I->second; 481 // If To is also scheduled to be replaced, find what its ultimate 482 // replacement is. 483 while (true) { 484 DenseMap<Register, Register>::iterator J = FuncInfo->RegFixups.find(To); 485 if (J == E) 486 break; 487 To = J->second; 488 } 489 // Make sure the new register has a sufficiently constrained register class. 490 if (Register::isVirtualRegister(From) && Register::isVirtualRegister(To)) 491 MRI.constrainRegClass(To, MRI.getRegClass(From)); 492 // Replace it. 493 494 // Replacing one register with another won't touch the kill flags. 495 // We need to conservatively clear the kill flags as a kill on the old 496 // register might dominate existing uses of the new register. 497 if (!MRI.use_empty(To)) 498 MRI.clearKillFlags(From); 499 MRI.replaceRegWith(From, To); 500 } 501 502 // If the first basic block in the function has live ins that need to be 503 // copied into vregs, emit the copies into the top of the block before 504 // emitting the code for the block. 505 const TargetRegisterInfo &TRI = *MF->getSubtarget().getRegisterInfo(); 506 RegInfo->EmitLiveInCopies(EntryMBB, TRI, *TII); 507 508 // Insert copies in the entry block and the return blocks. 509 if (FuncInfo->SplitCSR) { 510 SmallVector<MachineBasicBlock*, 4> Returns; 511 // Collect all the return blocks. 512 for (MachineBasicBlock &MBB : mf) { 513 if (!MBB.succ_empty()) 514 continue; 515 516 MachineBasicBlock::iterator Term = MBB.getFirstTerminator(); 517 if (Term != MBB.end() && Term->isReturn()) { 518 Returns.push_back(&MBB); 519 continue; 520 } 521 } 522 TLI->insertCopiesSplitCSR(EntryMBB, Returns); 523 } 524 525 DenseMap<unsigned, unsigned> LiveInMap; 526 if (!FuncInfo->ArgDbgValues.empty()) 527 for (std::pair<unsigned, unsigned> LI : RegInfo->liveins()) 528 if (LI.second) 529 LiveInMap.insert(LI); 530 531 // Insert DBG_VALUE instructions for function arguments to the entry block. 532 bool InstrRef = MF->useDebugInstrRef(); 533 for (unsigned i = 0, e = FuncInfo->ArgDbgValues.size(); i != e; ++i) { 534 MachineInstr *MI = FuncInfo->ArgDbgValues[e - i - 1]; 535 assert(MI->getOpcode() != TargetOpcode::DBG_VALUE_LIST && 536 "Function parameters should not be described by DBG_VALUE_LIST."); 537 bool hasFI = MI->getOperand(0).isFI(); 538 Register Reg = 539 hasFI ? TRI.getFrameRegister(*MF) : MI->getOperand(0).getReg(); 540 if (Register::isPhysicalRegister(Reg)) 541 EntryMBB->insert(EntryMBB->begin(), MI); 542 else { 543 MachineInstr *Def = RegInfo->getVRegDef(Reg); 544 if (Def) { 545 MachineBasicBlock::iterator InsertPos = Def; 546 // FIXME: VR def may not be in entry block. 547 Def->getParent()->insert(std::next(InsertPos), MI); 548 } else 549 LLVM_DEBUG(dbgs() << "Dropping debug info for dead vreg" 550 << Register::virtReg2Index(Reg) << "\n"); 551 } 552 553 // Don't try and extend through copies in instruction referencing mode. 554 if (InstrRef) 555 continue; 556 557 // If Reg is live-in then update debug info to track its copy in a vreg. 558 DenseMap<unsigned, unsigned>::iterator LDI = LiveInMap.find(Reg); 559 if (LDI != LiveInMap.end()) { 560 assert(!hasFI && "There's no handling of frame pointer updating here yet " 561 "- add if needed"); 562 MachineInstr *Def = RegInfo->getVRegDef(LDI->second); 563 MachineBasicBlock::iterator InsertPos = Def; 564 const MDNode *Variable = MI->getDebugVariable(); 565 const MDNode *Expr = MI->getDebugExpression(); 566 DebugLoc DL = MI->getDebugLoc(); 567 bool IsIndirect = MI->isIndirectDebugValue(); 568 if (IsIndirect) 569 assert(MI->getOperand(1).getImm() == 0 && 570 "DBG_VALUE with nonzero offset"); 571 assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) && 572 "Expected inlined-at fields to agree"); 573 assert(MI->getOpcode() != TargetOpcode::DBG_VALUE_LIST && 574 "Didn't expect to see a DBG_VALUE_LIST here"); 575 // Def is never a terminator here, so it is ok to increment InsertPos. 576 BuildMI(*EntryMBB, ++InsertPos, DL, TII->get(TargetOpcode::DBG_VALUE), 577 IsIndirect, LDI->second, Variable, Expr); 578 579 // If this vreg is directly copied into an exported register then 580 // that COPY instructions also need DBG_VALUE, if it is the only 581 // user of LDI->second. 582 MachineInstr *CopyUseMI = nullptr; 583 for (MachineRegisterInfo::use_instr_iterator 584 UI = RegInfo->use_instr_begin(LDI->second), 585 E = RegInfo->use_instr_end(); UI != E; ) { 586 MachineInstr *UseMI = &*(UI++); 587 if (UseMI->isDebugValue()) continue; 588 if (UseMI->isCopy() && !CopyUseMI && UseMI->getParent() == EntryMBB) { 589 CopyUseMI = UseMI; continue; 590 } 591 // Otherwise this is another use or second copy use. 592 CopyUseMI = nullptr; break; 593 } 594 if (CopyUseMI && 595 TRI.getRegSizeInBits(LDI->second, MRI) == 596 TRI.getRegSizeInBits(CopyUseMI->getOperand(0).getReg(), MRI)) { 597 // Use MI's debug location, which describes where Variable was 598 // declared, rather than whatever is attached to CopyUseMI. 599 MachineInstr *NewMI = 600 BuildMI(*MF, DL, TII->get(TargetOpcode::DBG_VALUE), IsIndirect, 601 CopyUseMI->getOperand(0).getReg(), Variable, Expr); 602 MachineBasicBlock::iterator Pos = CopyUseMI; 603 EntryMBB->insertAfter(Pos, NewMI); 604 } 605 } 606 } 607 608 // For debug-info, in instruction referencing mode, we need to perform some 609 // post-isel maintenence. 610 if (UseInstrRefDebugInfo) 611 MF->finalizeDebugInstrRefs(); 612 613 // Determine if there are any calls in this machine function. 614 MachineFrameInfo &MFI = MF->getFrameInfo(); 615 for (const auto &MBB : *MF) { 616 if (MFI.hasCalls() && MF->hasInlineAsm()) 617 break; 618 619 for (const auto &MI : MBB) { 620 const MCInstrDesc &MCID = TII->get(MI.getOpcode()); 621 if ((MCID.isCall() && !MCID.isReturn()) || 622 MI.isStackAligningInlineAsm()) { 623 MFI.setHasCalls(true); 624 } 625 if (MI.isInlineAsm()) { 626 MF->setHasInlineAsm(true); 627 } 628 } 629 } 630 631 // Determine if there is a call to setjmp in the machine function. 632 MF->setExposesReturnsTwice(Fn.callsFunctionThatReturnsTwice()); 633 634 // Determine if floating point is used for msvc 635 computeUsesMSVCFloatingPoint(TM.getTargetTriple(), Fn, MF->getMMI()); 636 637 // Release function-specific state. SDB and CurDAG are already cleared 638 // at this point. 639 FuncInfo->clear(); 640 641 LLVM_DEBUG(dbgs() << "*** MachineFunction at end of ISel ***\n"); 642 LLVM_DEBUG(MF->print(dbgs())); 643 644 return true; 645 } 646 647 static void reportFastISelFailure(MachineFunction &MF, 648 OptimizationRemarkEmitter &ORE, 649 OptimizationRemarkMissed &R, 650 bool ShouldAbort) { 651 // Print the function name explicitly if we don't have a debug location (which 652 // makes the diagnostic less useful) or if we're going to emit a raw error. 653 if (!R.getLocation().isValid() || ShouldAbort) 654 R << (" (in function: " + MF.getName() + ")").str(); 655 656 if (ShouldAbort) 657 report_fatal_error(Twine(R.getMsg())); 658 659 ORE.emit(R); 660 LLVM_DEBUG(dbgs() << R.getMsg() << "\n"); 661 } 662 663 void SelectionDAGISel::SelectBasicBlock(BasicBlock::const_iterator Begin, 664 BasicBlock::const_iterator End, 665 bool &HadTailCall) { 666 // Allow creating illegal types during DAG building for the basic block. 667 CurDAG->NewNodesMustHaveLegalTypes = false; 668 669 // Lower the instructions. If a call is emitted as a tail call, cease emitting 670 // nodes for this block. 671 for (BasicBlock::const_iterator I = Begin; I != End && !SDB->HasTailCall; ++I) { 672 if (!ElidedArgCopyInstrs.count(&*I)) 673 SDB->visit(*I); 674 } 675 676 // Make sure the root of the DAG is up-to-date. 677 CurDAG->setRoot(SDB->getControlRoot()); 678 HadTailCall = SDB->HasTailCall; 679 SDB->resolveOrClearDbgInfo(); 680 SDB->clear(); 681 682 // Final step, emit the lowered DAG as machine code. 683 CodeGenAndEmitDAG(); 684 } 685 686 void SelectionDAGISel::ComputeLiveOutVRegInfo() { 687 SmallPtrSet<SDNode *, 16> Added; 688 SmallVector<SDNode*, 128> Worklist; 689 690 Worklist.push_back(CurDAG->getRoot().getNode()); 691 Added.insert(CurDAG->getRoot().getNode()); 692 693 KnownBits Known; 694 695 do { 696 SDNode *N = Worklist.pop_back_val(); 697 698 // Otherwise, add all chain operands to the worklist. 699 for (const SDValue &Op : N->op_values()) 700 if (Op.getValueType() == MVT::Other && Added.insert(Op.getNode()).second) 701 Worklist.push_back(Op.getNode()); 702 703 // If this is a CopyToReg with a vreg dest, process it. 704 if (N->getOpcode() != ISD::CopyToReg) 705 continue; 706 707 unsigned DestReg = cast<RegisterSDNode>(N->getOperand(1))->getReg(); 708 if (!Register::isVirtualRegister(DestReg)) 709 continue; 710 711 // Ignore non-integer values. 712 SDValue Src = N->getOperand(2); 713 EVT SrcVT = Src.getValueType(); 714 if (!SrcVT.isInteger()) 715 continue; 716 717 unsigned NumSignBits = CurDAG->ComputeNumSignBits(Src); 718 Known = CurDAG->computeKnownBits(Src); 719 FuncInfo->AddLiveOutRegInfo(DestReg, NumSignBits, Known); 720 } while (!Worklist.empty()); 721 } 722 723 void SelectionDAGISel::CodeGenAndEmitDAG() { 724 StringRef GroupName = "sdag"; 725 StringRef GroupDescription = "Instruction Selection and Scheduling"; 726 std::string BlockName; 727 bool MatchFilterBB = false; (void)MatchFilterBB; 728 #ifndef NDEBUG 729 TargetTransformInfo &TTI = 730 getAnalysis<TargetTransformInfoWrapperPass>().getTTI(*FuncInfo->Fn); 731 #endif 732 733 // Pre-type legalization allow creation of any node types. 734 CurDAG->NewNodesMustHaveLegalTypes = false; 735 736 #ifndef NDEBUG 737 MatchFilterBB = (FilterDAGBasicBlockName.empty() || 738 FilterDAGBasicBlockName == 739 FuncInfo->MBB->getBasicBlock()->getName()); 740 #endif 741 #ifdef NDEBUG 742 if (ViewDAGCombine1 || ViewLegalizeTypesDAGs || ViewDAGCombineLT || 743 ViewLegalizeDAGs || ViewDAGCombine2 || ViewISelDAGs || ViewSchedDAGs || 744 ViewSUnitDAGs) 745 #endif 746 { 747 BlockName = 748 (MF->getName() + ":" + FuncInfo->MBB->getBasicBlock()->getName()).str(); 749 } 750 LLVM_DEBUG(dbgs() << "Initial selection DAG: " 751 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 752 << "'\n"; 753 CurDAG->dump()); 754 755 #ifndef NDEBUG 756 if (TTI.hasBranchDivergence()) 757 CurDAG->VerifyDAGDivergence(); 758 #endif 759 760 if (ViewDAGCombine1 && MatchFilterBB) 761 CurDAG->viewGraph("dag-combine1 input for " + BlockName); 762 763 // Run the DAG combiner in pre-legalize mode. 764 { 765 NamedRegionTimer T("combine1", "DAG Combining 1", GroupName, 766 GroupDescription, TimePassesIsEnabled); 767 CurDAG->Combine(BeforeLegalizeTypes, AA, OptLevel); 768 } 769 770 LLVM_DEBUG(dbgs() << "Optimized lowered selection DAG: " 771 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 772 << "'\n"; 773 CurDAG->dump()); 774 775 #ifndef NDEBUG 776 if (TTI.hasBranchDivergence()) 777 CurDAG->VerifyDAGDivergence(); 778 #endif 779 780 // Second step, hack on the DAG until it only uses operations and types that 781 // the target supports. 782 if (ViewLegalizeTypesDAGs && MatchFilterBB) 783 CurDAG->viewGraph("legalize-types input for " + BlockName); 784 785 bool Changed; 786 { 787 NamedRegionTimer T("legalize_types", "Type Legalization", GroupName, 788 GroupDescription, TimePassesIsEnabled); 789 Changed = CurDAG->LegalizeTypes(); 790 } 791 792 LLVM_DEBUG(dbgs() << "Type-legalized selection DAG: " 793 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 794 << "'\n"; 795 CurDAG->dump()); 796 797 #ifndef NDEBUG 798 if (TTI.hasBranchDivergence()) 799 CurDAG->VerifyDAGDivergence(); 800 #endif 801 802 // Only allow creation of legal node types. 803 CurDAG->NewNodesMustHaveLegalTypes = true; 804 805 if (Changed) { 806 if (ViewDAGCombineLT && MatchFilterBB) 807 CurDAG->viewGraph("dag-combine-lt input for " + BlockName); 808 809 // Run the DAG combiner in post-type-legalize mode. 810 { 811 NamedRegionTimer T("combine_lt", "DAG Combining after legalize types", 812 GroupName, GroupDescription, TimePassesIsEnabled); 813 CurDAG->Combine(AfterLegalizeTypes, AA, OptLevel); 814 } 815 816 LLVM_DEBUG(dbgs() << "Optimized type-legalized selection DAG: " 817 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 818 << "'\n"; 819 CurDAG->dump()); 820 821 #ifndef NDEBUG 822 if (TTI.hasBranchDivergence()) 823 CurDAG->VerifyDAGDivergence(); 824 #endif 825 } 826 827 { 828 NamedRegionTimer T("legalize_vec", "Vector Legalization", GroupName, 829 GroupDescription, TimePassesIsEnabled); 830 Changed = CurDAG->LegalizeVectors(); 831 } 832 833 if (Changed) { 834 LLVM_DEBUG(dbgs() << "Vector-legalized selection DAG: " 835 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 836 << "'\n"; 837 CurDAG->dump()); 838 839 #ifndef NDEBUG 840 if (TTI.hasBranchDivergence()) 841 CurDAG->VerifyDAGDivergence(); 842 #endif 843 844 { 845 NamedRegionTimer T("legalize_types2", "Type Legalization 2", GroupName, 846 GroupDescription, TimePassesIsEnabled); 847 CurDAG->LegalizeTypes(); 848 } 849 850 LLVM_DEBUG(dbgs() << "Vector/type-legalized selection DAG: " 851 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 852 << "'\n"; 853 CurDAG->dump()); 854 855 #ifndef NDEBUG 856 if (TTI.hasBranchDivergence()) 857 CurDAG->VerifyDAGDivergence(); 858 #endif 859 860 if (ViewDAGCombineLT && MatchFilterBB) 861 CurDAG->viewGraph("dag-combine-lv input for " + BlockName); 862 863 // Run the DAG combiner in post-type-legalize mode. 864 { 865 NamedRegionTimer T("combine_lv", "DAG Combining after legalize vectors", 866 GroupName, GroupDescription, TimePassesIsEnabled); 867 CurDAG->Combine(AfterLegalizeVectorOps, AA, OptLevel); 868 } 869 870 LLVM_DEBUG(dbgs() << "Optimized vector-legalized selection DAG: " 871 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 872 << "'\n"; 873 CurDAG->dump()); 874 875 #ifndef NDEBUG 876 if (TTI.hasBranchDivergence()) 877 CurDAG->VerifyDAGDivergence(); 878 #endif 879 } 880 881 if (ViewLegalizeDAGs && MatchFilterBB) 882 CurDAG->viewGraph("legalize input for " + BlockName); 883 884 { 885 NamedRegionTimer T("legalize", "DAG Legalization", GroupName, 886 GroupDescription, TimePassesIsEnabled); 887 CurDAG->Legalize(); 888 } 889 890 LLVM_DEBUG(dbgs() << "Legalized selection DAG: " 891 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 892 << "'\n"; 893 CurDAG->dump()); 894 895 #ifndef NDEBUG 896 if (TTI.hasBranchDivergence()) 897 CurDAG->VerifyDAGDivergence(); 898 #endif 899 900 if (ViewDAGCombine2 && MatchFilterBB) 901 CurDAG->viewGraph("dag-combine2 input for " + BlockName); 902 903 // Run the DAG combiner in post-legalize mode. 904 { 905 NamedRegionTimer T("combine2", "DAG Combining 2", GroupName, 906 GroupDescription, TimePassesIsEnabled); 907 CurDAG->Combine(AfterLegalizeDAG, AA, OptLevel); 908 } 909 910 LLVM_DEBUG(dbgs() << "Optimized legalized selection DAG: " 911 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 912 << "'\n"; 913 CurDAG->dump()); 914 915 #ifndef NDEBUG 916 if (TTI.hasBranchDivergence()) 917 CurDAG->VerifyDAGDivergence(); 918 #endif 919 920 if (OptLevel != CodeGenOpt::None) 921 ComputeLiveOutVRegInfo(); 922 923 if (ViewISelDAGs && MatchFilterBB) 924 CurDAG->viewGraph("isel input for " + BlockName); 925 926 // Third, instruction select all of the operations to machine code, adding the 927 // code to the MachineBasicBlock. 928 { 929 NamedRegionTimer T("isel", "Instruction Selection", GroupName, 930 GroupDescription, TimePassesIsEnabled); 931 DoInstructionSelection(); 932 } 933 934 LLVM_DEBUG(dbgs() << "Selected selection DAG: " 935 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName 936 << "'\n"; 937 CurDAG->dump()); 938 939 if (ViewSchedDAGs && MatchFilterBB) 940 CurDAG->viewGraph("scheduler input for " + BlockName); 941 942 // Schedule machine code. 943 ScheduleDAGSDNodes *Scheduler = CreateScheduler(); 944 { 945 NamedRegionTimer T("sched", "Instruction Scheduling", GroupName, 946 GroupDescription, TimePassesIsEnabled); 947 Scheduler->Run(CurDAG, FuncInfo->MBB); 948 } 949 950 if (ViewSUnitDAGs && MatchFilterBB) 951 Scheduler->viewGraph(); 952 953 // Emit machine code to BB. This can change 'BB' to the last block being 954 // inserted into. 955 MachineBasicBlock *FirstMBB = FuncInfo->MBB, *LastMBB; 956 { 957 NamedRegionTimer T("emit", "Instruction Creation", GroupName, 958 GroupDescription, TimePassesIsEnabled); 959 960 // FuncInfo->InsertPt is passed by reference and set to the end of the 961 // scheduled instructions. 962 LastMBB = FuncInfo->MBB = Scheduler->EmitSchedule(FuncInfo->InsertPt); 963 } 964 965 // If the block was split, make sure we update any references that are used to 966 // update PHI nodes later on. 967 if (FirstMBB != LastMBB) 968 SDB->UpdateSplitBlock(FirstMBB, LastMBB); 969 970 // Free the scheduler state. 971 { 972 NamedRegionTimer T("cleanup", "Instruction Scheduling Cleanup", GroupName, 973 GroupDescription, TimePassesIsEnabled); 974 delete Scheduler; 975 } 976 977 // Free the SelectionDAG state, now that we're finished with it. 978 CurDAG->clear(); 979 } 980 981 namespace { 982 983 /// ISelUpdater - helper class to handle updates of the instruction selection 984 /// graph. 985 class ISelUpdater : public SelectionDAG::DAGUpdateListener { 986 SelectionDAG::allnodes_iterator &ISelPosition; 987 988 public: 989 ISelUpdater(SelectionDAG &DAG, SelectionDAG::allnodes_iterator &isp) 990 : SelectionDAG::DAGUpdateListener(DAG), ISelPosition(isp) {} 991 992 /// NodeDeleted - Handle nodes deleted from the graph. If the node being 993 /// deleted is the current ISelPosition node, update ISelPosition. 994 /// 995 void NodeDeleted(SDNode *N, SDNode *E) override { 996 if (ISelPosition == SelectionDAG::allnodes_iterator(N)) 997 ++ISelPosition; 998 } 999 }; 1000 1001 } // end anonymous namespace 1002 1003 // This function is used to enforce the topological node id property 1004 // leveraged during instruction selection. Before the selection process all 1005 // nodes are given a non-negative id such that all nodes have a greater id than 1006 // their operands. As this holds transitively we can prune checks that a node N 1007 // is a predecessor of M another by not recursively checking through M's 1008 // operands if N's ID is larger than M's ID. This significantly improves 1009 // performance of various legality checks (e.g. IsLegalToFold / UpdateChains). 1010 1011 // However, when we fuse multiple nodes into a single node during the 1012 // selection we may induce a predecessor relationship between inputs and 1013 // outputs of distinct nodes being merged, violating the topological property. 1014 // Should a fused node have a successor which has yet to be selected, 1015 // our legality checks would be incorrect. To avoid this we mark all unselected 1016 // successor nodes, i.e. id != -1, as invalid for pruning by bit-negating (x => 1017 // (-(x+1))) the ids and modify our pruning check to ignore negative Ids of M. 1018 // We use bit-negation to more clearly enforce that node id -1 can only be 1019 // achieved by selected nodes. As the conversion is reversable to the original 1020 // Id, topological pruning can still be leveraged when looking for unselected 1021 // nodes. This method is called internally in all ISel replacement related 1022 // functions. 1023 void SelectionDAGISel::EnforceNodeIdInvariant(SDNode *Node) { 1024 SmallVector<SDNode *, 4> Nodes; 1025 Nodes.push_back(Node); 1026 1027 while (!Nodes.empty()) { 1028 SDNode *N = Nodes.pop_back_val(); 1029 for (auto *U : N->uses()) { 1030 auto UId = U->getNodeId(); 1031 if (UId > 0) { 1032 InvalidateNodeId(U); 1033 Nodes.push_back(U); 1034 } 1035 } 1036 } 1037 } 1038 1039 // InvalidateNodeId - As explained in EnforceNodeIdInvariant, mark a 1040 // NodeId with the equivalent node id which is invalid for topological 1041 // pruning. 1042 void SelectionDAGISel::InvalidateNodeId(SDNode *N) { 1043 int InvalidId = -(N->getNodeId() + 1); 1044 N->setNodeId(InvalidId); 1045 } 1046 1047 // getUninvalidatedNodeId - get original uninvalidated node id. 1048 int SelectionDAGISel::getUninvalidatedNodeId(SDNode *N) { 1049 int Id = N->getNodeId(); 1050 if (Id < -1) 1051 return -(Id + 1); 1052 return Id; 1053 } 1054 1055 void SelectionDAGISel::DoInstructionSelection() { 1056 LLVM_DEBUG(dbgs() << "===== Instruction selection begins: " 1057 << printMBBReference(*FuncInfo->MBB) << " '" 1058 << FuncInfo->MBB->getName() << "'\n"); 1059 1060 PreprocessISelDAG(); 1061 1062 // Select target instructions for the DAG. 1063 { 1064 // Number all nodes with a topological order and set DAGSize. 1065 DAGSize = CurDAG->AssignTopologicalOrder(); 1066 1067 // Create a dummy node (which is not added to allnodes), that adds 1068 // a reference to the root node, preventing it from being deleted, 1069 // and tracking any changes of the root. 1070 HandleSDNode Dummy(CurDAG->getRoot()); 1071 SelectionDAG::allnodes_iterator ISelPosition (CurDAG->getRoot().getNode()); 1072 ++ISelPosition; 1073 1074 // Make sure that ISelPosition gets properly updated when nodes are deleted 1075 // in calls made from this function. 1076 ISelUpdater ISU(*CurDAG, ISelPosition); 1077 1078 // The AllNodes list is now topological-sorted. Visit the 1079 // nodes by starting at the end of the list (the root of the 1080 // graph) and preceding back toward the beginning (the entry 1081 // node). 1082 while (ISelPosition != CurDAG->allnodes_begin()) { 1083 SDNode *Node = &*--ISelPosition; 1084 // Skip dead nodes. DAGCombiner is expected to eliminate all dead nodes, 1085 // but there are currently some corner cases that it misses. Also, this 1086 // makes it theoretically possible to disable the DAGCombiner. 1087 if (Node->use_empty()) 1088 continue; 1089 1090 #ifndef NDEBUG 1091 SmallVector<SDNode *, 4> Nodes; 1092 Nodes.push_back(Node); 1093 1094 while (!Nodes.empty()) { 1095 auto N = Nodes.pop_back_val(); 1096 if (N->getOpcode() == ISD::TokenFactor || N->getNodeId() < 0) 1097 continue; 1098 for (const SDValue &Op : N->op_values()) { 1099 if (Op->getOpcode() == ISD::TokenFactor) 1100 Nodes.push_back(Op.getNode()); 1101 else { 1102 // We rely on topological ordering of node ids for checking for 1103 // cycles when fusing nodes during selection. All unselected nodes 1104 // successors of an already selected node should have a negative id. 1105 // This assertion will catch such cases. If this assertion triggers 1106 // it is likely you using DAG-level Value/Node replacement functions 1107 // (versus equivalent ISEL replacement) in backend-specific 1108 // selections. See comment in EnforceNodeIdInvariant for more 1109 // details. 1110 assert(Op->getNodeId() != -1 && 1111 "Node has already selected predecessor node"); 1112 } 1113 } 1114 } 1115 #endif 1116 1117 // When we are using non-default rounding modes or FP exception behavior 1118 // FP operations are represented by StrictFP pseudo-operations. For 1119 // targets that do not (yet) understand strict FP operations directly, 1120 // we convert them to normal FP opcodes instead at this point. This 1121 // will allow them to be handled by existing target-specific instruction 1122 // selectors. 1123 if (!TLI->isStrictFPEnabled() && Node->isStrictFPOpcode()) { 1124 // For some opcodes, we need to call TLI->getOperationAction using 1125 // the first operand type instead of the result type. Note that this 1126 // must match what SelectionDAGLegalize::LegalizeOp is doing. 1127 EVT ActionVT; 1128 switch (Node->getOpcode()) { 1129 case ISD::STRICT_SINT_TO_FP: 1130 case ISD::STRICT_UINT_TO_FP: 1131 case ISD::STRICT_LRINT: 1132 case ISD::STRICT_LLRINT: 1133 case ISD::STRICT_LROUND: 1134 case ISD::STRICT_LLROUND: 1135 case ISD::STRICT_FSETCC: 1136 case ISD::STRICT_FSETCCS: 1137 ActionVT = Node->getOperand(1).getValueType(); 1138 break; 1139 default: 1140 ActionVT = Node->getValueType(0); 1141 break; 1142 } 1143 if (TLI->getOperationAction(Node->getOpcode(), ActionVT) 1144 == TargetLowering::Expand) 1145 Node = CurDAG->mutateStrictFPToFP(Node); 1146 } 1147 1148 LLVM_DEBUG(dbgs() << "\nISEL: Starting selection on root node: "; 1149 Node->dump(CurDAG)); 1150 1151 Select(Node); 1152 } 1153 1154 CurDAG->setRoot(Dummy.getValue()); 1155 } 1156 1157 LLVM_DEBUG(dbgs() << "\n===== Instruction selection ends:\n"); 1158 1159 PostprocessISelDAG(); 1160 } 1161 1162 static bool hasExceptionPointerOrCodeUser(const CatchPadInst *CPI) { 1163 for (const User *U : CPI->users()) { 1164 if (const IntrinsicInst *EHPtrCall = dyn_cast<IntrinsicInst>(U)) { 1165 Intrinsic::ID IID = EHPtrCall->getIntrinsicID(); 1166 if (IID == Intrinsic::eh_exceptionpointer || 1167 IID == Intrinsic::eh_exceptioncode) 1168 return true; 1169 } 1170 } 1171 return false; 1172 } 1173 1174 // wasm.landingpad.index intrinsic is for associating a landing pad index number 1175 // with a catchpad instruction. Retrieve the landing pad index in the intrinsic 1176 // and store the mapping in the function. 1177 static void mapWasmLandingPadIndex(MachineBasicBlock *MBB, 1178 const CatchPadInst *CPI) { 1179 MachineFunction *MF = MBB->getParent(); 1180 // In case of single catch (...), we don't emit LSDA, so we don't need 1181 // this information. 1182 bool IsSingleCatchAllClause = 1183 CPI->getNumArgOperands() == 1 && 1184 cast<Constant>(CPI->getArgOperand(0))->isNullValue(); 1185 // cathchpads for longjmp use an empty type list, e.g. catchpad within %0 [] 1186 // and they don't need LSDA info 1187 bool IsCatchLongjmp = CPI->getNumArgOperands() == 0; 1188 if (!IsSingleCatchAllClause && !IsCatchLongjmp) { 1189 // Create a mapping from landing pad label to landing pad index. 1190 bool IntrFound = false; 1191 for (const User *U : CPI->users()) { 1192 if (const auto *Call = dyn_cast<IntrinsicInst>(U)) { 1193 Intrinsic::ID IID = Call->getIntrinsicID(); 1194 if (IID == Intrinsic::wasm_landingpad_index) { 1195 Value *IndexArg = Call->getArgOperand(1); 1196 int Index = cast<ConstantInt>(IndexArg)->getZExtValue(); 1197 MF->setWasmLandingPadIndex(MBB, Index); 1198 IntrFound = true; 1199 break; 1200 } 1201 } 1202 } 1203 assert(IntrFound && "wasm.landingpad.index intrinsic not found!"); 1204 (void)IntrFound; 1205 } 1206 } 1207 1208 /// PrepareEHLandingPad - Emit an EH_LABEL, set up live-in registers, and 1209 /// do other setup for EH landing-pad blocks. 1210 bool SelectionDAGISel::PrepareEHLandingPad() { 1211 MachineBasicBlock *MBB = FuncInfo->MBB; 1212 const Constant *PersonalityFn = FuncInfo->Fn->getPersonalityFn(); 1213 const BasicBlock *LLVMBB = MBB->getBasicBlock(); 1214 const TargetRegisterClass *PtrRC = 1215 TLI->getRegClassFor(TLI->getPointerTy(CurDAG->getDataLayout())); 1216 1217 auto Pers = classifyEHPersonality(PersonalityFn); 1218 1219 // Catchpads have one live-in register, which typically holds the exception 1220 // pointer or code. 1221 if (isFuncletEHPersonality(Pers)) { 1222 if (const auto *CPI = dyn_cast<CatchPadInst>(LLVMBB->getFirstNonPHI())) { 1223 if (hasExceptionPointerOrCodeUser(CPI)) { 1224 // Get or create the virtual register to hold the pointer or code. Mark 1225 // the live in physreg and copy into the vreg. 1226 MCPhysReg EHPhysReg = TLI->getExceptionPointerRegister(PersonalityFn); 1227 assert(EHPhysReg && "target lacks exception pointer register"); 1228 MBB->addLiveIn(EHPhysReg); 1229 unsigned VReg = FuncInfo->getCatchPadExceptionPointerVReg(CPI, PtrRC); 1230 BuildMI(*MBB, FuncInfo->InsertPt, SDB->getCurDebugLoc(), 1231 TII->get(TargetOpcode::COPY), VReg) 1232 .addReg(EHPhysReg, RegState::Kill); 1233 } 1234 } 1235 return true; 1236 } 1237 1238 // Add a label to mark the beginning of the landing pad. Deletion of the 1239 // landing pad can thus be detected via the MachineModuleInfo. 1240 MCSymbol *Label = MF->addLandingPad(MBB); 1241 1242 const MCInstrDesc &II = TII->get(TargetOpcode::EH_LABEL); 1243 BuildMI(*MBB, FuncInfo->InsertPt, SDB->getCurDebugLoc(), II) 1244 .addSym(Label); 1245 1246 // If the unwinder does not preserve all registers, ensure that the 1247 // function marks the clobbered registers as used. 1248 const TargetRegisterInfo &TRI = *MF->getSubtarget().getRegisterInfo(); 1249 if (auto *RegMask = TRI.getCustomEHPadPreservedMask(*MF)) 1250 MF->getRegInfo().addPhysRegsUsedFromRegMask(RegMask); 1251 1252 if (Pers == EHPersonality::Wasm_CXX) { 1253 if (const auto *CPI = dyn_cast<CatchPadInst>(LLVMBB->getFirstNonPHI())) 1254 mapWasmLandingPadIndex(MBB, CPI); 1255 } else { 1256 // Assign the call site to the landing pad's begin label. 1257 MF->setCallSiteLandingPad(Label, SDB->LPadToCallSiteMap[MBB]); 1258 // Mark exception register as live in. 1259 if (unsigned Reg = TLI->getExceptionPointerRegister(PersonalityFn)) 1260 FuncInfo->ExceptionPointerVirtReg = MBB->addLiveIn(Reg, PtrRC); 1261 // Mark exception selector register as live in. 1262 if (unsigned Reg = TLI->getExceptionSelectorRegister(PersonalityFn)) 1263 FuncInfo->ExceptionSelectorVirtReg = MBB->addLiveIn(Reg, PtrRC); 1264 } 1265 1266 return true; 1267 } 1268 1269 /// isFoldedOrDeadInstruction - Return true if the specified instruction is 1270 /// side-effect free and is either dead or folded into a generated instruction. 1271 /// Return false if it needs to be emitted. 1272 static bool isFoldedOrDeadInstruction(const Instruction *I, 1273 const FunctionLoweringInfo &FuncInfo) { 1274 return !I->mayWriteToMemory() && // Side-effecting instructions aren't folded. 1275 !I->isTerminator() && // Terminators aren't folded. 1276 !isa<DbgInfoIntrinsic>(I) && // Debug instructions aren't folded. 1277 !I->isEHPad() && // EH pad instructions aren't folded. 1278 !FuncInfo.isExportedInst(I); // Exported instrs must be computed. 1279 } 1280 1281 /// Collect llvm.dbg.declare information. This is done after argument lowering 1282 /// in case the declarations refer to arguments. 1283 static void processDbgDeclares(FunctionLoweringInfo &FuncInfo) { 1284 MachineFunction *MF = FuncInfo.MF; 1285 const DataLayout &DL = MF->getDataLayout(); 1286 for (const BasicBlock &BB : *FuncInfo.Fn) { 1287 for (const Instruction &I : BB) { 1288 const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(&I); 1289 if (!DI) 1290 continue; 1291 1292 assert(DI->getVariable() && "Missing variable"); 1293 assert(DI->getDebugLoc() && "Missing location"); 1294 const Value *Address = DI->getAddress(); 1295 if (!Address) { 1296 LLVM_DEBUG(dbgs() << "processDbgDeclares skipping " << *DI 1297 << " (bad address)\n"); 1298 continue; 1299 } 1300 1301 // Look through casts and constant offset GEPs. These mostly come from 1302 // inalloca. 1303 APInt Offset(DL.getTypeSizeInBits(Address->getType()), 0); 1304 Address = Address->stripAndAccumulateInBoundsConstantOffsets(DL, Offset); 1305 1306 // Check if the variable is a static alloca or a byval or inalloca 1307 // argument passed in memory. If it is not, then we will ignore this 1308 // intrinsic and handle this during isel like dbg.value. 1309 int FI = std::numeric_limits<int>::max(); 1310 if (const auto *AI = dyn_cast<AllocaInst>(Address)) { 1311 auto SI = FuncInfo.StaticAllocaMap.find(AI); 1312 if (SI != FuncInfo.StaticAllocaMap.end()) 1313 FI = SI->second; 1314 } else if (const auto *Arg = dyn_cast<Argument>(Address)) 1315 FI = FuncInfo.getArgumentFrameIndex(Arg); 1316 1317 if (FI == std::numeric_limits<int>::max()) 1318 continue; 1319 1320 DIExpression *Expr = DI->getExpression(); 1321 if (Offset.getBoolValue()) 1322 Expr = DIExpression::prepend(Expr, DIExpression::ApplyOffset, 1323 Offset.getZExtValue()); 1324 LLVM_DEBUG(dbgs() << "processDbgDeclares: setVariableDbgInfo FI=" << FI 1325 << ", " << *DI << "\n"); 1326 MF->setVariableDbgInfo(DI->getVariable(), Expr, FI, DI->getDebugLoc()); 1327 } 1328 } 1329 } 1330 1331 void SelectionDAGISel::SelectAllBasicBlocks(const Function &Fn) { 1332 FastISelFailed = false; 1333 // Initialize the Fast-ISel state, if needed. 1334 FastISel *FastIS = nullptr; 1335 if (TM.Options.EnableFastISel) { 1336 LLVM_DEBUG(dbgs() << "Enabling fast-isel\n"); 1337 FastIS = TLI->createFastISel(*FuncInfo, LibInfo); 1338 if (FastIS) 1339 FastIS->useInstrRefDebugInfo(UseInstrRefDebugInfo); 1340 } 1341 1342 ReversePostOrderTraversal<const Function*> RPOT(&Fn); 1343 1344 // Lower arguments up front. An RPO iteration always visits the entry block 1345 // first. 1346 assert(*RPOT.begin() == &Fn.getEntryBlock()); 1347 ++NumEntryBlocks; 1348 1349 // Set up FuncInfo for ISel. Entry blocks never have PHIs. 1350 FuncInfo->MBB = FuncInfo->MBBMap[&Fn.getEntryBlock()]; 1351 FuncInfo->InsertPt = FuncInfo->MBB->begin(); 1352 1353 CurDAG->setFunctionLoweringInfo(FuncInfo.get()); 1354 1355 if (!FastIS) { 1356 LowerArguments(Fn); 1357 } else { 1358 // See if fast isel can lower the arguments. 1359 FastIS->startNewBlock(); 1360 if (!FastIS->lowerArguments()) { 1361 FastISelFailed = true; 1362 // Fast isel failed to lower these arguments 1363 ++NumFastIselFailLowerArguments; 1364 1365 OptimizationRemarkMissed R("sdagisel", "FastISelFailure", 1366 Fn.getSubprogram(), 1367 &Fn.getEntryBlock()); 1368 R << "FastISel didn't lower all arguments: " 1369 << ore::NV("Prototype", Fn.getType()); 1370 reportFastISelFailure(*MF, *ORE, R, EnableFastISelAbort > 1); 1371 1372 // Use SelectionDAG argument lowering 1373 LowerArguments(Fn); 1374 CurDAG->setRoot(SDB->getControlRoot()); 1375 SDB->clear(); 1376 CodeGenAndEmitDAG(); 1377 } 1378 1379 // If we inserted any instructions at the beginning, make a note of 1380 // where they are, so we can be sure to emit subsequent instructions 1381 // after them. 1382 if (FuncInfo->InsertPt != FuncInfo->MBB->begin()) 1383 FastIS->setLastLocalValue(&*std::prev(FuncInfo->InsertPt)); 1384 else 1385 FastIS->setLastLocalValue(nullptr); 1386 } 1387 1388 bool Inserted = SwiftError->createEntriesInEntryBlock(SDB->getCurDebugLoc()); 1389 1390 if (FastIS && Inserted) 1391 FastIS->setLastLocalValue(&*std::prev(FuncInfo->InsertPt)); 1392 1393 processDbgDeclares(*FuncInfo); 1394 1395 // Iterate over all basic blocks in the function. 1396 StackProtector &SP = getAnalysis<StackProtector>(); 1397 for (const BasicBlock *LLVMBB : RPOT) { 1398 if (OptLevel != CodeGenOpt::None) { 1399 bool AllPredsVisited = true; 1400 for (const BasicBlock *Pred : predecessors(LLVMBB)) { 1401 if (!FuncInfo->VisitedBBs.count(Pred)) { 1402 AllPredsVisited = false; 1403 break; 1404 } 1405 } 1406 1407 if (AllPredsVisited) { 1408 for (const PHINode &PN : LLVMBB->phis()) 1409 FuncInfo->ComputePHILiveOutRegInfo(&PN); 1410 } else { 1411 for (const PHINode &PN : LLVMBB->phis()) 1412 FuncInfo->InvalidatePHILiveOutRegInfo(&PN); 1413 } 1414 1415 FuncInfo->VisitedBBs.insert(LLVMBB); 1416 } 1417 1418 BasicBlock::const_iterator const Begin = 1419 LLVMBB->getFirstNonPHI()->getIterator(); 1420 BasicBlock::const_iterator const End = LLVMBB->end(); 1421 BasicBlock::const_iterator BI = End; 1422 1423 FuncInfo->MBB = FuncInfo->MBBMap[LLVMBB]; 1424 if (!FuncInfo->MBB) 1425 continue; // Some blocks like catchpads have no code or MBB. 1426 1427 // Insert new instructions after any phi or argument setup code. 1428 FuncInfo->InsertPt = FuncInfo->MBB->end(); 1429 1430 // Setup an EH landing-pad block. 1431 FuncInfo->ExceptionPointerVirtReg = 0; 1432 FuncInfo->ExceptionSelectorVirtReg = 0; 1433 if (LLVMBB->isEHPad()) 1434 if (!PrepareEHLandingPad()) 1435 continue; 1436 1437 // Before doing SelectionDAG ISel, see if FastISel has been requested. 1438 if (FastIS) { 1439 if (LLVMBB != &Fn.getEntryBlock()) 1440 FastIS->startNewBlock(); 1441 1442 unsigned NumFastIselRemaining = std::distance(Begin, End); 1443 1444 // Pre-assign swifterror vregs. 1445 SwiftError->preassignVRegs(FuncInfo->MBB, Begin, End); 1446 1447 // Do FastISel on as many instructions as possible. 1448 for (; BI != Begin; --BI) { 1449 const Instruction *Inst = &*std::prev(BI); 1450 1451 // If we no longer require this instruction, skip it. 1452 if (isFoldedOrDeadInstruction(Inst, *FuncInfo) || 1453 ElidedArgCopyInstrs.count(Inst)) { 1454 --NumFastIselRemaining; 1455 continue; 1456 } 1457 1458 // Bottom-up: reset the insert pos at the top, after any local-value 1459 // instructions. 1460 FastIS->recomputeInsertPt(); 1461 1462 // Try to select the instruction with FastISel. 1463 if (FastIS->selectInstruction(Inst)) { 1464 --NumFastIselRemaining; 1465 ++NumFastIselSuccess; 1466 // If fast isel succeeded, skip over all the folded instructions, and 1467 // then see if there is a load right before the selected instructions. 1468 // Try to fold the load if so. 1469 const Instruction *BeforeInst = Inst; 1470 while (BeforeInst != &*Begin) { 1471 BeforeInst = &*std::prev(BasicBlock::const_iterator(BeforeInst)); 1472 if (!isFoldedOrDeadInstruction(BeforeInst, *FuncInfo)) 1473 break; 1474 } 1475 if (BeforeInst != Inst && isa<LoadInst>(BeforeInst) && 1476 BeforeInst->hasOneUse() && 1477 FastIS->tryToFoldLoad(cast<LoadInst>(BeforeInst), Inst)) { 1478 // If we succeeded, don't re-select the load. 1479 LLVM_DEBUG(dbgs() 1480 << "FastISel folded load: " << *BeforeInst << "\n"); 1481 BI = std::next(BasicBlock::const_iterator(BeforeInst)); 1482 --NumFastIselRemaining; 1483 ++NumFastIselSuccess; 1484 } 1485 continue; 1486 } 1487 1488 FastISelFailed = true; 1489 1490 // Then handle certain instructions as single-LLVM-Instruction blocks. 1491 // We cannot separate out GCrelocates to their own blocks since we need 1492 // to keep track of gc-relocates for a particular gc-statepoint. This is 1493 // done by SelectionDAGBuilder::LowerAsSTATEPOINT, called before 1494 // visitGCRelocate. 1495 if (isa<CallInst>(Inst) && !isa<GCStatepointInst>(Inst) && 1496 !isa<GCRelocateInst>(Inst) && !isa<GCResultInst>(Inst)) { 1497 OptimizationRemarkMissed R("sdagisel", "FastISelFailure", 1498 Inst->getDebugLoc(), LLVMBB); 1499 1500 R << "FastISel missed call"; 1501 1502 if (R.isEnabled() || EnableFastISelAbort) { 1503 std::string InstStrStorage; 1504 raw_string_ostream InstStr(InstStrStorage); 1505 InstStr << *Inst; 1506 1507 R << ": " << InstStr.str(); 1508 } 1509 1510 reportFastISelFailure(*MF, *ORE, R, EnableFastISelAbort > 2); 1511 1512 if (!Inst->getType()->isVoidTy() && !Inst->getType()->isTokenTy() && 1513 !Inst->use_empty()) { 1514 Register &R = FuncInfo->ValueMap[Inst]; 1515 if (!R) 1516 R = FuncInfo->CreateRegs(Inst); 1517 } 1518 1519 bool HadTailCall = false; 1520 MachineBasicBlock::iterator SavedInsertPt = FuncInfo->InsertPt; 1521 SelectBasicBlock(Inst->getIterator(), BI, HadTailCall); 1522 1523 // If the call was emitted as a tail call, we're done with the block. 1524 // We also need to delete any previously emitted instructions. 1525 if (HadTailCall) { 1526 FastIS->removeDeadCode(SavedInsertPt, FuncInfo->MBB->end()); 1527 --BI; 1528 break; 1529 } 1530 1531 // Recompute NumFastIselRemaining as Selection DAG instruction 1532 // selection may have handled the call, input args, etc. 1533 unsigned RemainingNow = std::distance(Begin, BI); 1534 NumFastIselFailures += NumFastIselRemaining - RemainingNow; 1535 NumFastIselRemaining = RemainingNow; 1536 continue; 1537 } 1538 1539 OptimizationRemarkMissed R("sdagisel", "FastISelFailure", 1540 Inst->getDebugLoc(), LLVMBB); 1541 1542 bool ShouldAbort = EnableFastISelAbort; 1543 if (Inst->isTerminator()) { 1544 // Use a different message for terminator misses. 1545 R << "FastISel missed terminator"; 1546 // Don't abort for terminator unless the level is really high 1547 ShouldAbort = (EnableFastISelAbort > 2); 1548 } else { 1549 R << "FastISel missed"; 1550 } 1551 1552 if (R.isEnabled() || EnableFastISelAbort) { 1553 std::string InstStrStorage; 1554 raw_string_ostream InstStr(InstStrStorage); 1555 InstStr << *Inst; 1556 R << ": " << InstStr.str(); 1557 } 1558 1559 reportFastISelFailure(*MF, *ORE, R, ShouldAbort); 1560 1561 NumFastIselFailures += NumFastIselRemaining; 1562 break; 1563 } 1564 1565 FastIS->recomputeInsertPt(); 1566 } 1567 1568 if (SP.shouldEmitSDCheck(*LLVMBB)) { 1569 bool FunctionBasedInstrumentation = 1570 TLI->getSSPStackGuardCheck(*Fn.getParent()); 1571 SDB->SPDescriptor.initialize(LLVMBB, FuncInfo->MBBMap[LLVMBB], 1572 FunctionBasedInstrumentation); 1573 } 1574 1575 if (Begin != BI) 1576 ++NumDAGBlocks; 1577 else 1578 ++NumFastIselBlocks; 1579 1580 if (Begin != BI) { 1581 // Run SelectionDAG instruction selection on the remainder of the block 1582 // not handled by FastISel. If FastISel is not run, this is the entire 1583 // block. 1584 bool HadTailCall; 1585 SelectBasicBlock(Begin, BI, HadTailCall); 1586 1587 // But if FastISel was run, we already selected some of the block. 1588 // If we emitted a tail-call, we need to delete any previously emitted 1589 // instruction that follows it. 1590 if (FastIS && HadTailCall && FuncInfo->InsertPt != FuncInfo->MBB->end()) 1591 FastIS->removeDeadCode(FuncInfo->InsertPt, FuncInfo->MBB->end()); 1592 } 1593 1594 if (FastIS) 1595 FastIS->finishBasicBlock(); 1596 FinishBasicBlock(); 1597 FuncInfo->PHINodesToUpdate.clear(); 1598 ElidedArgCopyInstrs.clear(); 1599 } 1600 1601 SP.copyToMachineFrameInfo(MF->getFrameInfo()); 1602 1603 SwiftError->propagateVRegs(); 1604 1605 delete FastIS; 1606 SDB->clearDanglingDebugInfo(); 1607 SDB->SPDescriptor.resetPerFunctionState(); 1608 } 1609 1610 void 1611 SelectionDAGISel::FinishBasicBlock() { 1612 LLVM_DEBUG(dbgs() << "Total amount of phi nodes to update: " 1613 << FuncInfo->PHINodesToUpdate.size() << "\n"; 1614 for (unsigned i = 0, e = FuncInfo->PHINodesToUpdate.size(); i != e; 1615 ++i) dbgs() 1616 << "Node " << i << " : (" << FuncInfo->PHINodesToUpdate[i].first 1617 << ", " << FuncInfo->PHINodesToUpdate[i].second << ")\n"); 1618 1619 // Next, now that we know what the last MBB the LLVM BB expanded is, update 1620 // PHI nodes in successors. 1621 for (unsigned i = 0, e = FuncInfo->PHINodesToUpdate.size(); i != e; ++i) { 1622 MachineInstrBuilder PHI(*MF, FuncInfo->PHINodesToUpdate[i].first); 1623 assert(PHI->isPHI() && 1624 "This is not a machine PHI node that we are updating!"); 1625 if (!FuncInfo->MBB->isSuccessor(PHI->getParent())) 1626 continue; 1627 PHI.addReg(FuncInfo->PHINodesToUpdate[i].second).addMBB(FuncInfo->MBB); 1628 } 1629 1630 // Handle stack protector. 1631 if (SDB->SPDescriptor.shouldEmitFunctionBasedCheckStackProtector()) { 1632 // The target provides a guard check function. There is no need to 1633 // generate error handling code or to split current basic block. 1634 MachineBasicBlock *ParentMBB = SDB->SPDescriptor.getParentMBB(); 1635 1636 // Add load and check to the basicblock. 1637 FuncInfo->MBB = ParentMBB; 1638 FuncInfo->InsertPt = 1639 findSplitPointForStackProtector(ParentMBB, *TII); 1640 SDB->visitSPDescriptorParent(SDB->SPDescriptor, ParentMBB); 1641 CurDAG->setRoot(SDB->getRoot()); 1642 SDB->clear(); 1643 CodeGenAndEmitDAG(); 1644 1645 // Clear the Per-BB State. 1646 SDB->SPDescriptor.resetPerBBState(); 1647 } else if (SDB->SPDescriptor.shouldEmitStackProtector()) { 1648 MachineBasicBlock *ParentMBB = SDB->SPDescriptor.getParentMBB(); 1649 MachineBasicBlock *SuccessMBB = SDB->SPDescriptor.getSuccessMBB(); 1650 1651 // Find the split point to split the parent mbb. At the same time copy all 1652 // physical registers used in the tail of parent mbb into virtual registers 1653 // before the split point and back into physical registers after the split 1654 // point. This prevents us needing to deal with Live-ins and many other 1655 // register allocation issues caused by us splitting the parent mbb. The 1656 // register allocator will clean up said virtual copies later on. 1657 MachineBasicBlock::iterator SplitPoint = 1658 findSplitPointForStackProtector(ParentMBB, *TII); 1659 1660 // Splice the terminator of ParentMBB into SuccessMBB. 1661 SuccessMBB->splice(SuccessMBB->end(), ParentMBB, 1662 SplitPoint, 1663 ParentMBB->end()); 1664 1665 // Add compare/jump on neq/jump to the parent BB. 1666 FuncInfo->MBB = ParentMBB; 1667 FuncInfo->InsertPt = ParentMBB->end(); 1668 SDB->visitSPDescriptorParent(SDB->SPDescriptor, ParentMBB); 1669 CurDAG->setRoot(SDB->getRoot()); 1670 SDB->clear(); 1671 CodeGenAndEmitDAG(); 1672 1673 // CodeGen Failure MBB if we have not codegened it yet. 1674 MachineBasicBlock *FailureMBB = SDB->SPDescriptor.getFailureMBB(); 1675 if (FailureMBB->empty()) { 1676 FuncInfo->MBB = FailureMBB; 1677 FuncInfo->InsertPt = FailureMBB->end(); 1678 SDB->visitSPDescriptorFailure(SDB->SPDescriptor); 1679 CurDAG->setRoot(SDB->getRoot()); 1680 SDB->clear(); 1681 CodeGenAndEmitDAG(); 1682 } 1683 1684 // Clear the Per-BB State. 1685 SDB->SPDescriptor.resetPerBBState(); 1686 } 1687 1688 // Lower each BitTestBlock. 1689 for (auto &BTB : SDB->SL->BitTestCases) { 1690 // Lower header first, if it wasn't already lowered 1691 if (!BTB.Emitted) { 1692 // Set the current basic block to the mbb we wish to insert the code into 1693 FuncInfo->MBB = BTB.Parent; 1694 FuncInfo->InsertPt = FuncInfo->MBB->end(); 1695 // Emit the code 1696 SDB->visitBitTestHeader(BTB, FuncInfo->MBB); 1697 CurDAG->setRoot(SDB->getRoot()); 1698 SDB->clear(); 1699 CodeGenAndEmitDAG(); 1700 } 1701 1702 BranchProbability UnhandledProb = BTB.Prob; 1703 for (unsigned j = 0, ej = BTB.Cases.size(); j != ej; ++j) { 1704 UnhandledProb -= BTB.Cases[j].ExtraProb; 1705 // Set the current basic block to the mbb we wish to insert the code into 1706 FuncInfo->MBB = BTB.Cases[j].ThisBB; 1707 FuncInfo->InsertPt = FuncInfo->MBB->end(); 1708 // Emit the code 1709 1710 // If all cases cover a contiguous range, it is not necessary to jump to 1711 // the default block after the last bit test fails. This is because the 1712 // range check during bit test header creation has guaranteed that every 1713 // case here doesn't go outside the range. In this case, there is no need 1714 // to perform the last bit test, as it will always be true. Instead, make 1715 // the second-to-last bit-test fall through to the target of the last bit 1716 // test, and delete the last bit test. 1717 1718 MachineBasicBlock *NextMBB; 1719 if ((BTB.ContiguousRange || BTB.FallthroughUnreachable) && j + 2 == ej) { 1720 // Second-to-last bit-test with contiguous range or omitted range 1721 // check: fall through to the target of the final bit test. 1722 NextMBB = BTB.Cases[j + 1].TargetBB; 1723 } else if (j + 1 == ej) { 1724 // For the last bit test, fall through to Default. 1725 NextMBB = BTB.Default; 1726 } else { 1727 // Otherwise, fall through to the next bit test. 1728 NextMBB = BTB.Cases[j + 1].ThisBB; 1729 } 1730 1731 SDB->visitBitTestCase(BTB, NextMBB, UnhandledProb, BTB.Reg, BTB.Cases[j], 1732 FuncInfo->MBB); 1733 1734 CurDAG->setRoot(SDB->getRoot()); 1735 SDB->clear(); 1736 CodeGenAndEmitDAG(); 1737 1738 if ((BTB.ContiguousRange || BTB.FallthroughUnreachable) && j + 2 == ej) { 1739 // Since we're not going to use the final bit test, remove it. 1740 BTB.Cases.pop_back(); 1741 break; 1742 } 1743 } 1744 1745 // Update PHI Nodes 1746 for (const std::pair<MachineInstr *, unsigned> &P : 1747 FuncInfo->PHINodesToUpdate) { 1748 MachineInstrBuilder PHI(*MF, P.first); 1749 MachineBasicBlock *PHIBB = PHI->getParent(); 1750 assert(PHI->isPHI() && 1751 "This is not a machine PHI node that we are updating!"); 1752 // This is "default" BB. We have two jumps to it. From "header" BB and 1753 // from last "case" BB, unless the latter was skipped. 1754 if (PHIBB == BTB.Default) { 1755 PHI.addReg(P.second).addMBB(BTB.Parent); 1756 if (!BTB.ContiguousRange) { 1757 PHI.addReg(P.second).addMBB(BTB.Cases.back().ThisBB); 1758 } 1759 } 1760 // One of "cases" BB. 1761 for (const SwitchCG::BitTestCase &BT : BTB.Cases) { 1762 MachineBasicBlock* cBB = BT.ThisBB; 1763 if (cBB->isSuccessor(PHIBB)) 1764 PHI.addReg(P.second).addMBB(cBB); 1765 } 1766 } 1767 } 1768 SDB->SL->BitTestCases.clear(); 1769 1770 // If the JumpTable record is filled in, then we need to emit a jump table. 1771 // Updating the PHI nodes is tricky in this case, since we need to determine 1772 // whether the PHI is a successor of the range check MBB or the jump table MBB 1773 for (unsigned i = 0, e = SDB->SL->JTCases.size(); i != e; ++i) { 1774 // Lower header first, if it wasn't already lowered 1775 if (!SDB->SL->JTCases[i].first.Emitted) { 1776 // Set the current basic block to the mbb we wish to insert the code into 1777 FuncInfo->MBB = SDB->SL->JTCases[i].first.HeaderBB; 1778 FuncInfo->InsertPt = FuncInfo->MBB->end(); 1779 // Emit the code 1780 SDB->visitJumpTableHeader(SDB->SL->JTCases[i].second, 1781 SDB->SL->JTCases[i].first, FuncInfo->MBB); 1782 CurDAG->setRoot(SDB->getRoot()); 1783 SDB->clear(); 1784 CodeGenAndEmitDAG(); 1785 } 1786 1787 // Set the current basic block to the mbb we wish to insert the code into 1788 FuncInfo->MBB = SDB->SL->JTCases[i].second.MBB; 1789 FuncInfo->InsertPt = FuncInfo->MBB->end(); 1790 // Emit the code 1791 SDB->visitJumpTable(SDB->SL->JTCases[i].second); 1792 CurDAG->setRoot(SDB->getRoot()); 1793 SDB->clear(); 1794 CodeGenAndEmitDAG(); 1795 1796 // Update PHI Nodes 1797 for (unsigned pi = 0, pe = FuncInfo->PHINodesToUpdate.size(); 1798 pi != pe; ++pi) { 1799 MachineInstrBuilder PHI(*MF, FuncInfo->PHINodesToUpdate[pi].first); 1800 MachineBasicBlock *PHIBB = PHI->getParent(); 1801 assert(PHI->isPHI() && 1802 "This is not a machine PHI node that we are updating!"); 1803 // "default" BB. We can go there only from header BB. 1804 if (PHIBB == SDB->SL->JTCases[i].second.Default) 1805 PHI.addReg(FuncInfo->PHINodesToUpdate[pi].second) 1806 .addMBB(SDB->SL->JTCases[i].first.HeaderBB); 1807 // JT BB. Just iterate over successors here 1808 if (FuncInfo->MBB->isSuccessor(PHIBB)) 1809 PHI.addReg(FuncInfo->PHINodesToUpdate[pi].second).addMBB(FuncInfo->MBB); 1810 } 1811 } 1812 SDB->SL->JTCases.clear(); 1813 1814 // If we generated any switch lowering information, build and codegen any 1815 // additional DAGs necessary. 1816 for (unsigned i = 0, e = SDB->SL->SwitchCases.size(); i != e; ++i) { 1817 // Set the current basic block to the mbb we wish to insert the code into 1818 FuncInfo->MBB = SDB->SL->SwitchCases[i].ThisBB; 1819 FuncInfo->InsertPt = FuncInfo->MBB->end(); 1820 1821 // Determine the unique successors. 1822 SmallVector<MachineBasicBlock *, 2> Succs; 1823 Succs.push_back(SDB->SL->SwitchCases[i].TrueBB); 1824 if (SDB->SL->SwitchCases[i].TrueBB != SDB->SL->SwitchCases[i].FalseBB) 1825 Succs.push_back(SDB->SL->SwitchCases[i].FalseBB); 1826 1827 // Emit the code. Note that this could result in FuncInfo->MBB being split. 1828 SDB->visitSwitchCase(SDB->SL->SwitchCases[i], FuncInfo->MBB); 1829 CurDAG->setRoot(SDB->getRoot()); 1830 SDB->clear(); 1831 CodeGenAndEmitDAG(); 1832 1833 // Remember the last block, now that any splitting is done, for use in 1834 // populating PHI nodes in successors. 1835 MachineBasicBlock *ThisBB = FuncInfo->MBB; 1836 1837 // Handle any PHI nodes in successors of this chunk, as if we were coming 1838 // from the original BB before switch expansion. Note that PHI nodes can 1839 // occur multiple times in PHINodesToUpdate. We have to be very careful to 1840 // handle them the right number of times. 1841 for (unsigned i = 0, e = Succs.size(); i != e; ++i) { 1842 FuncInfo->MBB = Succs[i]; 1843 FuncInfo->InsertPt = FuncInfo->MBB->end(); 1844 // FuncInfo->MBB may have been removed from the CFG if a branch was 1845 // constant folded. 1846 if (ThisBB->isSuccessor(FuncInfo->MBB)) { 1847 for (MachineBasicBlock::iterator 1848 MBBI = FuncInfo->MBB->begin(), MBBE = FuncInfo->MBB->end(); 1849 MBBI != MBBE && MBBI->isPHI(); ++MBBI) { 1850 MachineInstrBuilder PHI(*MF, MBBI); 1851 // This value for this PHI node is recorded in PHINodesToUpdate. 1852 for (unsigned pn = 0; ; ++pn) { 1853 assert(pn != FuncInfo->PHINodesToUpdate.size() && 1854 "Didn't find PHI entry!"); 1855 if (FuncInfo->PHINodesToUpdate[pn].first == PHI) { 1856 PHI.addReg(FuncInfo->PHINodesToUpdate[pn].second).addMBB(ThisBB); 1857 break; 1858 } 1859 } 1860 } 1861 } 1862 } 1863 } 1864 SDB->SL->SwitchCases.clear(); 1865 } 1866 1867 /// Create the scheduler. If a specific scheduler was specified 1868 /// via the SchedulerRegistry, use it, otherwise select the 1869 /// one preferred by the target. 1870 /// 1871 ScheduleDAGSDNodes *SelectionDAGISel::CreateScheduler() { 1872 return ISHeuristic(this, OptLevel); 1873 } 1874 1875 //===----------------------------------------------------------------------===// 1876 // Helper functions used by the generated instruction selector. 1877 //===----------------------------------------------------------------------===// 1878 // Calls to these methods are generated by tblgen. 1879 1880 /// CheckAndMask - The isel is trying to match something like (and X, 255). If 1881 /// the dag combiner simplified the 255, we still want to match. RHS is the 1882 /// actual value in the DAG on the RHS of an AND, and DesiredMaskS is the value 1883 /// specified in the .td file (e.g. 255). 1884 bool SelectionDAGISel::CheckAndMask(SDValue LHS, ConstantSDNode *RHS, 1885 int64_t DesiredMaskS) const { 1886 const APInt &ActualMask = RHS->getAPIntValue(); 1887 const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS); 1888 1889 // If the actual mask exactly matches, success! 1890 if (ActualMask == DesiredMask) 1891 return true; 1892 1893 // If the actual AND mask is allowing unallowed bits, this doesn't match. 1894 if (!ActualMask.isSubsetOf(DesiredMask)) 1895 return false; 1896 1897 // Otherwise, the DAG Combiner may have proven that the value coming in is 1898 // either already zero or is not demanded. Check for known zero input bits. 1899 APInt NeededMask = DesiredMask & ~ActualMask; 1900 if (CurDAG->MaskedValueIsZero(LHS, NeededMask)) 1901 return true; 1902 1903 // TODO: check to see if missing bits are just not demanded. 1904 1905 // Otherwise, this pattern doesn't match. 1906 return false; 1907 } 1908 1909 /// CheckOrMask - The isel is trying to match something like (or X, 255). If 1910 /// the dag combiner simplified the 255, we still want to match. RHS is the 1911 /// actual value in the DAG on the RHS of an OR, and DesiredMaskS is the value 1912 /// specified in the .td file (e.g. 255). 1913 bool SelectionDAGISel::CheckOrMask(SDValue LHS, ConstantSDNode *RHS, 1914 int64_t DesiredMaskS) const { 1915 const APInt &ActualMask = RHS->getAPIntValue(); 1916 const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS); 1917 1918 // If the actual mask exactly matches, success! 1919 if (ActualMask == DesiredMask) 1920 return true; 1921 1922 // If the actual AND mask is allowing unallowed bits, this doesn't match. 1923 if (!ActualMask.isSubsetOf(DesiredMask)) 1924 return false; 1925 1926 // Otherwise, the DAG Combiner may have proven that the value coming in is 1927 // either already zero or is not demanded. Check for known zero input bits. 1928 APInt NeededMask = DesiredMask & ~ActualMask; 1929 KnownBits Known = CurDAG->computeKnownBits(LHS); 1930 1931 // If all the missing bits in the or are already known to be set, match! 1932 if (NeededMask.isSubsetOf(Known.One)) 1933 return true; 1934 1935 // TODO: check to see if missing bits are just not demanded. 1936 1937 // Otherwise, this pattern doesn't match. 1938 return false; 1939 } 1940 1941 /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated 1942 /// by tblgen. Others should not call it. 1943 void SelectionDAGISel::SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops, 1944 const SDLoc &DL) { 1945 std::vector<SDValue> InOps; 1946 std::swap(InOps, Ops); 1947 1948 Ops.push_back(InOps[InlineAsm::Op_InputChain]); // 0 1949 Ops.push_back(InOps[InlineAsm::Op_AsmString]); // 1 1950 Ops.push_back(InOps[InlineAsm::Op_MDNode]); // 2, !srcloc 1951 Ops.push_back(InOps[InlineAsm::Op_ExtraInfo]); // 3 (SideEffect, AlignStack) 1952 1953 unsigned i = InlineAsm::Op_FirstOperand, e = InOps.size(); 1954 if (InOps[e-1].getValueType() == MVT::Glue) 1955 --e; // Don't process a glue operand if it is here. 1956 1957 while (i != e) { 1958 unsigned Flags = cast<ConstantSDNode>(InOps[i])->getZExtValue(); 1959 if (!InlineAsm::isMemKind(Flags)) { 1960 // Just skip over this operand, copying the operands verbatim. 1961 Ops.insert(Ops.end(), InOps.begin()+i, 1962 InOps.begin()+i+InlineAsm::getNumOperandRegisters(Flags) + 1); 1963 i += InlineAsm::getNumOperandRegisters(Flags) + 1; 1964 } else { 1965 assert(InlineAsm::getNumOperandRegisters(Flags) == 1 && 1966 "Memory operand with multiple values?"); 1967 1968 unsigned TiedToOperand; 1969 if (InlineAsm::isUseOperandTiedToDef(Flags, TiedToOperand)) { 1970 // We need the constraint ID from the operand this is tied to. 1971 unsigned CurOp = InlineAsm::Op_FirstOperand; 1972 Flags = cast<ConstantSDNode>(InOps[CurOp])->getZExtValue(); 1973 for (; TiedToOperand; --TiedToOperand) { 1974 CurOp += InlineAsm::getNumOperandRegisters(Flags)+1; 1975 Flags = cast<ConstantSDNode>(InOps[CurOp])->getZExtValue(); 1976 } 1977 } 1978 1979 // Otherwise, this is a memory operand. Ask the target to select it. 1980 std::vector<SDValue> SelOps; 1981 unsigned ConstraintID = InlineAsm::getMemoryConstraintID(Flags); 1982 if (SelectInlineAsmMemoryOperand(InOps[i+1], ConstraintID, SelOps)) 1983 report_fatal_error("Could not match memory address. Inline asm" 1984 " failure!"); 1985 1986 // Add this to the output node. 1987 unsigned NewFlags = 1988 InlineAsm::getFlagWord(InlineAsm::Kind_Mem, SelOps.size()); 1989 NewFlags = InlineAsm::getFlagWordForMem(NewFlags, ConstraintID); 1990 Ops.push_back(CurDAG->getTargetConstant(NewFlags, DL, MVT::i32)); 1991 llvm::append_range(Ops, SelOps); 1992 i += 2; 1993 } 1994 } 1995 1996 // Add the glue input back if present. 1997 if (e != InOps.size()) 1998 Ops.push_back(InOps.back()); 1999 } 2000 2001 /// findGlueUse - Return use of MVT::Glue value produced by the specified 2002 /// SDNode. 2003 /// 2004 static SDNode *findGlueUse(SDNode *N) { 2005 unsigned FlagResNo = N->getNumValues()-1; 2006 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) { 2007 SDUse &Use = I.getUse(); 2008 if (Use.getResNo() == FlagResNo) 2009 return Use.getUser(); 2010 } 2011 return nullptr; 2012 } 2013 2014 /// findNonImmUse - Return true if "Def" is a predecessor of "Root" via a path 2015 /// beyond "ImmedUse". We may ignore chains as they are checked separately. 2016 static bool findNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse, 2017 bool IgnoreChains) { 2018 SmallPtrSet<const SDNode *, 16> Visited; 2019 SmallVector<const SDNode *, 16> WorkList; 2020 // Only check if we have non-immediate uses of Def. 2021 if (ImmedUse->isOnlyUserOf(Def)) 2022 return false; 2023 2024 // We don't care about paths to Def that go through ImmedUse so mark it 2025 // visited and mark non-def operands as used. 2026 Visited.insert(ImmedUse); 2027 for (const SDValue &Op : ImmedUse->op_values()) { 2028 SDNode *N = Op.getNode(); 2029 // Ignore chain deps (they are validated by 2030 // HandleMergeInputChains) and immediate uses 2031 if ((Op.getValueType() == MVT::Other && IgnoreChains) || N == Def) 2032 continue; 2033 if (!Visited.insert(N).second) 2034 continue; 2035 WorkList.push_back(N); 2036 } 2037 2038 // Initialize worklist to operands of Root. 2039 if (Root != ImmedUse) { 2040 for (const SDValue &Op : Root->op_values()) { 2041 SDNode *N = Op.getNode(); 2042 // Ignore chains (they are validated by HandleMergeInputChains) 2043 if ((Op.getValueType() == MVT::Other && IgnoreChains) || N == Def) 2044 continue; 2045 if (!Visited.insert(N).second) 2046 continue; 2047 WorkList.push_back(N); 2048 } 2049 } 2050 2051 return SDNode::hasPredecessorHelper(Def, Visited, WorkList, 0, true); 2052 } 2053 2054 /// IsProfitableToFold - Returns true if it's profitable to fold the specific 2055 /// operand node N of U during instruction selection that starts at Root. 2056 bool SelectionDAGISel::IsProfitableToFold(SDValue N, SDNode *U, 2057 SDNode *Root) const { 2058 if (OptLevel == CodeGenOpt::None) return false; 2059 return N.hasOneUse(); 2060 } 2061 2062 /// IsLegalToFold - Returns true if the specific operand node N of 2063 /// U can be folded during instruction selection that starts at Root. 2064 bool SelectionDAGISel::IsLegalToFold(SDValue N, SDNode *U, SDNode *Root, 2065 CodeGenOpt::Level OptLevel, 2066 bool IgnoreChains) { 2067 if (OptLevel == CodeGenOpt::None) return false; 2068 2069 // If Root use can somehow reach N through a path that that doesn't contain 2070 // U then folding N would create a cycle. e.g. In the following 2071 // diagram, Root can reach N through X. If N is folded into Root, then 2072 // X is both a predecessor and a successor of U. 2073 // 2074 // [N*] // 2075 // ^ ^ // 2076 // / \ // 2077 // [U*] [X]? // 2078 // ^ ^ // 2079 // \ / // 2080 // \ / // 2081 // [Root*] // 2082 // 2083 // * indicates nodes to be folded together. 2084 // 2085 // If Root produces glue, then it gets (even more) interesting. Since it 2086 // will be "glued" together with its glue use in the scheduler, we need to 2087 // check if it might reach N. 2088 // 2089 // [N*] // 2090 // ^ ^ // 2091 // / \ // 2092 // [U*] [X]? // 2093 // ^ ^ // 2094 // \ \ // 2095 // \ | // 2096 // [Root*] | // 2097 // ^ | // 2098 // f | // 2099 // | / // 2100 // [Y] / // 2101 // ^ / // 2102 // f / // 2103 // | / // 2104 // [GU] // 2105 // 2106 // If GU (glue use) indirectly reaches N (the load), and Root folds N 2107 // (call it Fold), then X is a predecessor of GU and a successor of 2108 // Fold. But since Fold and GU are glued together, this will create 2109 // a cycle in the scheduling graph. 2110 2111 // If the node has glue, walk down the graph to the "lowest" node in the 2112 // glueged set. 2113 EVT VT = Root->getValueType(Root->getNumValues()-1); 2114 while (VT == MVT::Glue) { 2115 SDNode *GU = findGlueUse(Root); 2116 if (!GU) 2117 break; 2118 Root = GU; 2119 VT = Root->getValueType(Root->getNumValues()-1); 2120 2121 // If our query node has a glue result with a use, we've walked up it. If 2122 // the user (which has already been selected) has a chain or indirectly uses 2123 // the chain, HandleMergeInputChains will not consider it. Because of 2124 // this, we cannot ignore chains in this predicate. 2125 IgnoreChains = false; 2126 } 2127 2128 return !findNonImmUse(Root, N.getNode(), U, IgnoreChains); 2129 } 2130 2131 void SelectionDAGISel::Select_INLINEASM(SDNode *N) { 2132 SDLoc DL(N); 2133 2134 std::vector<SDValue> Ops(N->op_begin(), N->op_end()); 2135 SelectInlineAsmMemoryOperands(Ops, DL); 2136 2137 const EVT VTs[] = {MVT::Other, MVT::Glue}; 2138 SDValue New = CurDAG->getNode(N->getOpcode(), DL, VTs, Ops); 2139 New->setNodeId(-1); 2140 ReplaceUses(N, New.getNode()); 2141 CurDAG->RemoveDeadNode(N); 2142 } 2143 2144 void SelectionDAGISel::Select_READ_REGISTER(SDNode *Op) { 2145 SDLoc dl(Op); 2146 MDNodeSDNode *MD = cast<MDNodeSDNode>(Op->getOperand(1)); 2147 const MDString *RegStr = cast<MDString>(MD->getMD()->getOperand(0)); 2148 2149 EVT VT = Op->getValueType(0); 2150 LLT Ty = VT.isSimple() ? getLLTForMVT(VT.getSimpleVT()) : LLT(); 2151 Register Reg = 2152 TLI->getRegisterByName(RegStr->getString().data(), Ty, 2153 CurDAG->getMachineFunction()); 2154 SDValue New = CurDAG->getCopyFromReg( 2155 Op->getOperand(0), dl, Reg, Op->getValueType(0)); 2156 New->setNodeId(-1); 2157 ReplaceUses(Op, New.getNode()); 2158 CurDAG->RemoveDeadNode(Op); 2159 } 2160 2161 void SelectionDAGISel::Select_WRITE_REGISTER(SDNode *Op) { 2162 SDLoc dl(Op); 2163 MDNodeSDNode *MD = cast<MDNodeSDNode>(Op->getOperand(1)); 2164 const MDString *RegStr = cast<MDString>(MD->getMD()->getOperand(0)); 2165 2166 EVT VT = Op->getOperand(2).getValueType(); 2167 LLT Ty = VT.isSimple() ? getLLTForMVT(VT.getSimpleVT()) : LLT(); 2168 2169 Register Reg = TLI->getRegisterByName(RegStr->getString().data(), Ty, 2170 CurDAG->getMachineFunction()); 2171 SDValue New = CurDAG->getCopyToReg( 2172 Op->getOperand(0), dl, Reg, Op->getOperand(2)); 2173 New->setNodeId(-1); 2174 ReplaceUses(Op, New.getNode()); 2175 CurDAG->RemoveDeadNode(Op); 2176 } 2177 2178 void SelectionDAGISel::Select_UNDEF(SDNode *N) { 2179 CurDAG->SelectNodeTo(N, TargetOpcode::IMPLICIT_DEF, N->getValueType(0)); 2180 } 2181 2182 void SelectionDAGISel::Select_FREEZE(SDNode *N) { 2183 // TODO: We don't have FREEZE pseudo-instruction in MachineInstr-level now. 2184 // If FREEZE instruction is added later, the code below must be changed as 2185 // well. 2186 CurDAG->SelectNodeTo(N, TargetOpcode::COPY, N->getValueType(0), 2187 N->getOperand(0)); 2188 } 2189 2190 void SelectionDAGISel::Select_ARITH_FENCE(SDNode *N) { 2191 CurDAG->SelectNodeTo(N, TargetOpcode::ARITH_FENCE, N->getValueType(0), 2192 N->getOperand(0)); 2193 } 2194 2195 /// GetVBR - decode a vbr encoding whose top bit is set. 2196 LLVM_ATTRIBUTE_ALWAYS_INLINE static uint64_t 2197 GetVBR(uint64_t Val, const unsigned char *MatcherTable, unsigned &Idx) { 2198 assert(Val >= 128 && "Not a VBR"); 2199 Val &= 127; // Remove first vbr bit. 2200 2201 unsigned Shift = 7; 2202 uint64_t NextBits; 2203 do { 2204 NextBits = MatcherTable[Idx++]; 2205 Val |= (NextBits&127) << Shift; 2206 Shift += 7; 2207 } while (NextBits & 128); 2208 2209 return Val; 2210 } 2211 2212 /// When a match is complete, this method updates uses of interior chain results 2213 /// to use the new results. 2214 void SelectionDAGISel::UpdateChains( 2215 SDNode *NodeToMatch, SDValue InputChain, 2216 SmallVectorImpl<SDNode *> &ChainNodesMatched, bool isMorphNodeTo) { 2217 SmallVector<SDNode*, 4> NowDeadNodes; 2218 2219 // Now that all the normal results are replaced, we replace the chain and 2220 // glue results if present. 2221 if (!ChainNodesMatched.empty()) { 2222 assert(InputChain.getNode() && 2223 "Matched input chains but didn't produce a chain"); 2224 // Loop over all of the nodes we matched that produced a chain result. 2225 // Replace all the chain results with the final chain we ended up with. 2226 for (unsigned i = 0, e = ChainNodesMatched.size(); i != e; ++i) { 2227 SDNode *ChainNode = ChainNodesMatched[i]; 2228 // If ChainNode is null, it's because we replaced it on a previous 2229 // iteration and we cleared it out of the map. Just skip it. 2230 if (!ChainNode) 2231 continue; 2232 2233 assert(ChainNode->getOpcode() != ISD::DELETED_NODE && 2234 "Deleted node left in chain"); 2235 2236 // Don't replace the results of the root node if we're doing a 2237 // MorphNodeTo. 2238 if (ChainNode == NodeToMatch && isMorphNodeTo) 2239 continue; 2240 2241 SDValue ChainVal = SDValue(ChainNode, ChainNode->getNumValues()-1); 2242 if (ChainVal.getValueType() == MVT::Glue) 2243 ChainVal = ChainVal.getValue(ChainVal->getNumValues()-2); 2244 assert(ChainVal.getValueType() == MVT::Other && "Not a chain?"); 2245 SelectionDAG::DAGNodeDeletedListener NDL( 2246 *CurDAG, [&](SDNode *N, SDNode *E) { 2247 std::replace(ChainNodesMatched.begin(), ChainNodesMatched.end(), N, 2248 static_cast<SDNode *>(nullptr)); 2249 }); 2250 if (ChainNode->getOpcode() != ISD::TokenFactor) 2251 ReplaceUses(ChainVal, InputChain); 2252 2253 // If the node became dead and we haven't already seen it, delete it. 2254 if (ChainNode != NodeToMatch && ChainNode->use_empty() && 2255 !llvm::is_contained(NowDeadNodes, ChainNode)) 2256 NowDeadNodes.push_back(ChainNode); 2257 } 2258 } 2259 2260 if (!NowDeadNodes.empty()) 2261 CurDAG->RemoveDeadNodes(NowDeadNodes); 2262 2263 LLVM_DEBUG(dbgs() << "ISEL: Match complete!\n"); 2264 } 2265 2266 /// HandleMergeInputChains - This implements the OPC_EmitMergeInputChains 2267 /// operation for when the pattern matched at least one node with a chains. The 2268 /// input vector contains a list of all of the chained nodes that we match. We 2269 /// must determine if this is a valid thing to cover (i.e. matching it won't 2270 /// induce cycles in the DAG) and if so, creating a TokenFactor node. that will 2271 /// be used as the input node chain for the generated nodes. 2272 static SDValue 2273 HandleMergeInputChains(SmallVectorImpl<SDNode*> &ChainNodesMatched, 2274 SelectionDAG *CurDAG) { 2275 2276 SmallPtrSet<const SDNode *, 16> Visited; 2277 SmallVector<const SDNode *, 8> Worklist; 2278 SmallVector<SDValue, 3> InputChains; 2279 unsigned int Max = 8192; 2280 2281 // Quick exit on trivial merge. 2282 if (ChainNodesMatched.size() == 1) 2283 return ChainNodesMatched[0]->getOperand(0); 2284 2285 // Add chains that aren't already added (internal). Peek through 2286 // token factors. 2287 std::function<void(const SDValue)> AddChains = [&](const SDValue V) { 2288 if (V.getValueType() != MVT::Other) 2289 return; 2290 if (V->getOpcode() == ISD::EntryToken) 2291 return; 2292 if (!Visited.insert(V.getNode()).second) 2293 return; 2294 if (V->getOpcode() == ISD::TokenFactor) { 2295 for (const SDValue &Op : V->op_values()) 2296 AddChains(Op); 2297 } else 2298 InputChains.push_back(V); 2299 }; 2300 2301 for (auto *N : ChainNodesMatched) { 2302 Worklist.push_back(N); 2303 Visited.insert(N); 2304 } 2305 2306 while (!Worklist.empty()) 2307 AddChains(Worklist.pop_back_val()->getOperand(0)); 2308 2309 // Skip the search if there are no chain dependencies. 2310 if (InputChains.size() == 0) 2311 return CurDAG->getEntryNode(); 2312 2313 // If one of these chains is a successor of input, we must have a 2314 // node that is both the predecessor and successor of the 2315 // to-be-merged nodes. Fail. 2316 Visited.clear(); 2317 for (SDValue V : InputChains) 2318 Worklist.push_back(V.getNode()); 2319 2320 for (auto *N : ChainNodesMatched) 2321 if (SDNode::hasPredecessorHelper(N, Visited, Worklist, Max, true)) 2322 return SDValue(); 2323 2324 // Return merged chain. 2325 if (InputChains.size() == 1) 2326 return InputChains[0]; 2327 return CurDAG->getNode(ISD::TokenFactor, SDLoc(ChainNodesMatched[0]), 2328 MVT::Other, InputChains); 2329 } 2330 2331 /// MorphNode - Handle morphing a node in place for the selector. 2332 SDNode *SelectionDAGISel:: 2333 MorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTList, 2334 ArrayRef<SDValue> Ops, unsigned EmitNodeInfo) { 2335 // It is possible we're using MorphNodeTo to replace a node with no 2336 // normal results with one that has a normal result (or we could be 2337 // adding a chain) and the input could have glue and chains as well. 2338 // In this case we need to shift the operands down. 2339 // FIXME: This is a horrible hack and broken in obscure cases, no worse 2340 // than the old isel though. 2341 int OldGlueResultNo = -1, OldChainResultNo = -1; 2342 2343 unsigned NTMNumResults = Node->getNumValues(); 2344 if (Node->getValueType(NTMNumResults-1) == MVT::Glue) { 2345 OldGlueResultNo = NTMNumResults-1; 2346 if (NTMNumResults != 1 && 2347 Node->getValueType(NTMNumResults-2) == MVT::Other) 2348 OldChainResultNo = NTMNumResults-2; 2349 } else if (Node->getValueType(NTMNumResults-1) == MVT::Other) 2350 OldChainResultNo = NTMNumResults-1; 2351 2352 // Call the underlying SelectionDAG routine to do the transmogrification. Note 2353 // that this deletes operands of the old node that become dead. 2354 SDNode *Res = CurDAG->MorphNodeTo(Node, ~TargetOpc, VTList, Ops); 2355 2356 // MorphNodeTo can operate in two ways: if an existing node with the 2357 // specified operands exists, it can just return it. Otherwise, it 2358 // updates the node in place to have the requested operands. 2359 if (Res == Node) { 2360 // If we updated the node in place, reset the node ID. To the isel, 2361 // this should be just like a newly allocated machine node. 2362 Res->setNodeId(-1); 2363 } 2364 2365 unsigned ResNumResults = Res->getNumValues(); 2366 // Move the glue if needed. 2367 if ((EmitNodeInfo & OPFL_GlueOutput) && OldGlueResultNo != -1 && 2368 (unsigned)OldGlueResultNo != ResNumResults-1) 2369 ReplaceUses(SDValue(Node, OldGlueResultNo), 2370 SDValue(Res, ResNumResults - 1)); 2371 2372 if ((EmitNodeInfo & OPFL_GlueOutput) != 0) 2373 --ResNumResults; 2374 2375 // Move the chain reference if needed. 2376 if ((EmitNodeInfo & OPFL_Chain) && OldChainResultNo != -1 && 2377 (unsigned)OldChainResultNo != ResNumResults-1) 2378 ReplaceUses(SDValue(Node, OldChainResultNo), 2379 SDValue(Res, ResNumResults - 1)); 2380 2381 // Otherwise, no replacement happened because the node already exists. Replace 2382 // Uses of the old node with the new one. 2383 if (Res != Node) { 2384 ReplaceNode(Node, Res); 2385 } else { 2386 EnforceNodeIdInvariant(Res); 2387 } 2388 2389 return Res; 2390 } 2391 2392 /// CheckSame - Implements OP_CheckSame. 2393 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool 2394 CheckSame(const unsigned char *MatcherTable, unsigned &MatcherIndex, SDValue N, 2395 const SmallVectorImpl<std::pair<SDValue, SDNode *>> &RecordedNodes) { 2396 // Accept if it is exactly the same as a previously recorded node. 2397 unsigned RecNo = MatcherTable[MatcherIndex++]; 2398 assert(RecNo < RecordedNodes.size() && "Invalid CheckSame"); 2399 return N == RecordedNodes[RecNo].first; 2400 } 2401 2402 /// CheckChildSame - Implements OP_CheckChildXSame. 2403 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool CheckChildSame( 2404 const unsigned char *MatcherTable, unsigned &MatcherIndex, SDValue N, 2405 const SmallVectorImpl<std::pair<SDValue, SDNode *>> &RecordedNodes, 2406 unsigned ChildNo) { 2407 if (ChildNo >= N.getNumOperands()) 2408 return false; // Match fails if out of range child #. 2409 return ::CheckSame(MatcherTable, MatcherIndex, N.getOperand(ChildNo), 2410 RecordedNodes); 2411 } 2412 2413 /// CheckPatternPredicate - Implements OP_CheckPatternPredicate. 2414 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool 2415 CheckPatternPredicate(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2416 const SelectionDAGISel &SDISel) { 2417 return SDISel.CheckPatternPredicate(MatcherTable[MatcherIndex++]); 2418 } 2419 2420 /// CheckNodePredicate - Implements OP_CheckNodePredicate. 2421 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool 2422 CheckNodePredicate(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2423 const SelectionDAGISel &SDISel, SDNode *N) { 2424 return SDISel.CheckNodePredicate(N, MatcherTable[MatcherIndex++]); 2425 } 2426 2427 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool 2428 CheckOpcode(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2429 SDNode *N) { 2430 uint16_t Opc = MatcherTable[MatcherIndex++]; 2431 Opc |= (unsigned short)MatcherTable[MatcherIndex++] << 8; 2432 return N->getOpcode() == Opc; 2433 } 2434 2435 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool 2436 CheckType(const unsigned char *MatcherTable, unsigned &MatcherIndex, SDValue N, 2437 const TargetLowering *TLI, const DataLayout &DL) { 2438 MVT::SimpleValueType VT = (MVT::SimpleValueType)MatcherTable[MatcherIndex++]; 2439 if (N.getValueType() == VT) return true; 2440 2441 // Handle the case when VT is iPTR. 2442 return VT == MVT::iPTR && N.getValueType() == TLI->getPointerTy(DL); 2443 } 2444 2445 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool 2446 CheckChildType(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2447 SDValue N, const TargetLowering *TLI, const DataLayout &DL, 2448 unsigned ChildNo) { 2449 if (ChildNo >= N.getNumOperands()) 2450 return false; // Match fails if out of range child #. 2451 return ::CheckType(MatcherTable, MatcherIndex, N.getOperand(ChildNo), TLI, 2452 DL); 2453 } 2454 2455 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool 2456 CheckCondCode(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2457 SDValue N) { 2458 return cast<CondCodeSDNode>(N)->get() == 2459 (ISD::CondCode)MatcherTable[MatcherIndex++]; 2460 } 2461 2462 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool 2463 CheckChild2CondCode(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2464 SDValue N) { 2465 if (2 >= N.getNumOperands()) 2466 return false; 2467 return ::CheckCondCode(MatcherTable, MatcherIndex, N.getOperand(2)); 2468 } 2469 2470 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool 2471 CheckValueType(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2472 SDValue N, const TargetLowering *TLI, const DataLayout &DL) { 2473 MVT::SimpleValueType VT = (MVT::SimpleValueType)MatcherTable[MatcherIndex++]; 2474 if (cast<VTSDNode>(N)->getVT() == VT) 2475 return true; 2476 2477 // Handle the case when VT is iPTR. 2478 return VT == MVT::iPTR && cast<VTSDNode>(N)->getVT() == TLI->getPointerTy(DL); 2479 } 2480 2481 // Bit 0 stores the sign of the immediate. The upper bits contain the magnitude 2482 // shifted left by 1. 2483 static uint64_t decodeSignRotatedValue(uint64_t V) { 2484 if ((V & 1) == 0) 2485 return V >> 1; 2486 if (V != 1) 2487 return -(V >> 1); 2488 // There is no such thing as -0 with integers. "-0" really means MININT. 2489 return 1ULL << 63; 2490 } 2491 2492 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool 2493 CheckInteger(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2494 SDValue N) { 2495 int64_t Val = MatcherTable[MatcherIndex++]; 2496 if (Val & 128) 2497 Val = GetVBR(Val, MatcherTable, MatcherIndex); 2498 2499 Val = decodeSignRotatedValue(Val); 2500 2501 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N); 2502 return C && C->getSExtValue() == Val; 2503 } 2504 2505 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool 2506 CheckChildInteger(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2507 SDValue N, unsigned ChildNo) { 2508 if (ChildNo >= N.getNumOperands()) 2509 return false; // Match fails if out of range child #. 2510 return ::CheckInteger(MatcherTable, MatcherIndex, N.getOperand(ChildNo)); 2511 } 2512 2513 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool 2514 CheckAndImm(const unsigned char *MatcherTable, unsigned &MatcherIndex, 2515 SDValue N, const SelectionDAGISel &SDISel) { 2516 int64_t Val = MatcherTable[MatcherIndex++]; 2517 if (Val & 128) 2518 Val = GetVBR(Val, MatcherTable, MatcherIndex); 2519 2520 if (N->getOpcode() != ISD::AND) return false; 2521 2522 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2523 return C && SDISel.CheckAndMask(N.getOperand(0), C, Val); 2524 } 2525 2526 LLVM_ATTRIBUTE_ALWAYS_INLINE static bool 2527 CheckOrImm(const unsigned char *MatcherTable, unsigned &MatcherIndex, SDValue N, 2528 const SelectionDAGISel &SDISel) { 2529 int64_t Val = MatcherTable[MatcherIndex++]; 2530 if (Val & 128) 2531 Val = GetVBR(Val, MatcherTable, MatcherIndex); 2532 2533 if (N->getOpcode() != ISD::OR) return false; 2534 2535 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2536 return C && SDISel.CheckOrMask(N.getOperand(0), C, Val); 2537 } 2538 2539 /// IsPredicateKnownToFail - If we know how and can do so without pushing a 2540 /// scope, evaluate the current node. If the current predicate is known to 2541 /// fail, set Result=true and return anything. If the current predicate is 2542 /// known to pass, set Result=false and return the MatcherIndex to continue 2543 /// with. If the current predicate is unknown, set Result=false and return the 2544 /// MatcherIndex to continue with. 2545 static unsigned IsPredicateKnownToFail(const unsigned char *Table, 2546 unsigned Index, SDValue N, 2547 bool &Result, 2548 const SelectionDAGISel &SDISel, 2549 SmallVectorImpl<std::pair<SDValue, SDNode*>> &RecordedNodes) { 2550 switch (Table[Index++]) { 2551 default: 2552 Result = false; 2553 return Index-1; // Could not evaluate this predicate. 2554 case SelectionDAGISel::OPC_CheckSame: 2555 Result = !::CheckSame(Table, Index, N, RecordedNodes); 2556 return Index; 2557 case SelectionDAGISel::OPC_CheckChild0Same: 2558 case SelectionDAGISel::OPC_CheckChild1Same: 2559 case SelectionDAGISel::OPC_CheckChild2Same: 2560 case SelectionDAGISel::OPC_CheckChild3Same: 2561 Result = !::CheckChildSame(Table, Index, N, RecordedNodes, 2562 Table[Index-1] - SelectionDAGISel::OPC_CheckChild0Same); 2563 return Index; 2564 case SelectionDAGISel::OPC_CheckPatternPredicate: 2565 Result = !::CheckPatternPredicate(Table, Index, SDISel); 2566 return Index; 2567 case SelectionDAGISel::OPC_CheckPredicate: 2568 Result = !::CheckNodePredicate(Table, Index, SDISel, N.getNode()); 2569 return Index; 2570 case SelectionDAGISel::OPC_CheckOpcode: 2571 Result = !::CheckOpcode(Table, Index, N.getNode()); 2572 return Index; 2573 case SelectionDAGISel::OPC_CheckType: 2574 Result = !::CheckType(Table, Index, N, SDISel.TLI, 2575 SDISel.CurDAG->getDataLayout()); 2576 return Index; 2577 case SelectionDAGISel::OPC_CheckTypeRes: { 2578 unsigned Res = Table[Index++]; 2579 Result = !::CheckType(Table, Index, N.getValue(Res), SDISel.TLI, 2580 SDISel.CurDAG->getDataLayout()); 2581 return Index; 2582 } 2583 case SelectionDAGISel::OPC_CheckChild0Type: 2584 case SelectionDAGISel::OPC_CheckChild1Type: 2585 case SelectionDAGISel::OPC_CheckChild2Type: 2586 case SelectionDAGISel::OPC_CheckChild3Type: 2587 case SelectionDAGISel::OPC_CheckChild4Type: 2588 case SelectionDAGISel::OPC_CheckChild5Type: 2589 case SelectionDAGISel::OPC_CheckChild6Type: 2590 case SelectionDAGISel::OPC_CheckChild7Type: 2591 Result = !::CheckChildType( 2592 Table, Index, N, SDISel.TLI, SDISel.CurDAG->getDataLayout(), 2593 Table[Index - 1] - SelectionDAGISel::OPC_CheckChild0Type); 2594 return Index; 2595 case SelectionDAGISel::OPC_CheckCondCode: 2596 Result = !::CheckCondCode(Table, Index, N); 2597 return Index; 2598 case SelectionDAGISel::OPC_CheckChild2CondCode: 2599 Result = !::CheckChild2CondCode(Table, Index, N); 2600 return Index; 2601 case SelectionDAGISel::OPC_CheckValueType: 2602 Result = !::CheckValueType(Table, Index, N, SDISel.TLI, 2603 SDISel.CurDAG->getDataLayout()); 2604 return Index; 2605 case SelectionDAGISel::OPC_CheckInteger: 2606 Result = !::CheckInteger(Table, Index, N); 2607 return Index; 2608 case SelectionDAGISel::OPC_CheckChild0Integer: 2609 case SelectionDAGISel::OPC_CheckChild1Integer: 2610 case SelectionDAGISel::OPC_CheckChild2Integer: 2611 case SelectionDAGISel::OPC_CheckChild3Integer: 2612 case SelectionDAGISel::OPC_CheckChild4Integer: 2613 Result = !::CheckChildInteger(Table, Index, N, 2614 Table[Index-1] - SelectionDAGISel::OPC_CheckChild0Integer); 2615 return Index; 2616 case SelectionDAGISel::OPC_CheckAndImm: 2617 Result = !::CheckAndImm(Table, Index, N, SDISel); 2618 return Index; 2619 case SelectionDAGISel::OPC_CheckOrImm: 2620 Result = !::CheckOrImm(Table, Index, N, SDISel); 2621 return Index; 2622 } 2623 } 2624 2625 namespace { 2626 2627 struct MatchScope { 2628 /// FailIndex - If this match fails, this is the index to continue with. 2629 unsigned FailIndex; 2630 2631 /// NodeStack - The node stack when the scope was formed. 2632 SmallVector<SDValue, 4> NodeStack; 2633 2634 /// NumRecordedNodes - The number of recorded nodes when the scope was formed. 2635 unsigned NumRecordedNodes; 2636 2637 /// NumMatchedMemRefs - The number of matched memref entries. 2638 unsigned NumMatchedMemRefs; 2639 2640 /// InputChain/InputGlue - The current chain/glue 2641 SDValue InputChain, InputGlue; 2642 2643 /// HasChainNodesMatched - True if the ChainNodesMatched list is non-empty. 2644 bool HasChainNodesMatched; 2645 }; 2646 2647 /// \A DAG update listener to keep the matching state 2648 /// (i.e. RecordedNodes and MatchScope) uptodate if the target is allowed to 2649 /// change the DAG while matching. X86 addressing mode matcher is an example 2650 /// for this. 2651 class MatchStateUpdater : public SelectionDAG::DAGUpdateListener 2652 { 2653 SDNode **NodeToMatch; 2654 SmallVectorImpl<std::pair<SDValue, SDNode *>> &RecordedNodes; 2655 SmallVectorImpl<MatchScope> &MatchScopes; 2656 2657 public: 2658 MatchStateUpdater(SelectionDAG &DAG, SDNode **NodeToMatch, 2659 SmallVectorImpl<std::pair<SDValue, SDNode *>> &RN, 2660 SmallVectorImpl<MatchScope> &MS) 2661 : SelectionDAG::DAGUpdateListener(DAG), NodeToMatch(NodeToMatch), 2662 RecordedNodes(RN), MatchScopes(MS) {} 2663 2664 void NodeDeleted(SDNode *N, SDNode *E) override { 2665 // Some early-returns here to avoid the search if we deleted the node or 2666 // if the update comes from MorphNodeTo (MorphNodeTo is the last thing we 2667 // do, so it's unnecessary to update matching state at that point). 2668 // Neither of these can occur currently because we only install this 2669 // update listener during matching a complex patterns. 2670 if (!E || E->isMachineOpcode()) 2671 return; 2672 // Check if NodeToMatch was updated. 2673 if (N == *NodeToMatch) 2674 *NodeToMatch = E; 2675 // Performing linear search here does not matter because we almost never 2676 // run this code. You'd have to have a CSE during complex pattern 2677 // matching. 2678 for (auto &I : RecordedNodes) 2679 if (I.first.getNode() == N) 2680 I.first.setNode(E); 2681 2682 for (auto &I : MatchScopes) 2683 for (auto &J : I.NodeStack) 2684 if (J.getNode() == N) 2685 J.setNode(E); 2686 } 2687 }; 2688 2689 } // end anonymous namespace 2690 2691 void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch, 2692 const unsigned char *MatcherTable, 2693 unsigned TableSize) { 2694 // FIXME: Should these even be selected? Handle these cases in the caller? 2695 switch (NodeToMatch->getOpcode()) { 2696 default: 2697 break; 2698 case ISD::EntryToken: // These nodes remain the same. 2699 case ISD::BasicBlock: 2700 case ISD::Register: 2701 case ISD::RegisterMask: 2702 case ISD::HANDLENODE: 2703 case ISD::MDNODE_SDNODE: 2704 case ISD::TargetConstant: 2705 case ISD::TargetConstantFP: 2706 case ISD::TargetConstantPool: 2707 case ISD::TargetFrameIndex: 2708 case ISD::TargetExternalSymbol: 2709 case ISD::MCSymbol: 2710 case ISD::TargetBlockAddress: 2711 case ISD::TargetJumpTable: 2712 case ISD::TargetGlobalTLSAddress: 2713 case ISD::TargetGlobalAddress: 2714 case ISD::TokenFactor: 2715 case ISD::CopyFromReg: 2716 case ISD::CopyToReg: 2717 case ISD::EH_LABEL: 2718 case ISD::ANNOTATION_LABEL: 2719 case ISD::LIFETIME_START: 2720 case ISD::LIFETIME_END: 2721 case ISD::PSEUDO_PROBE: 2722 NodeToMatch->setNodeId(-1); // Mark selected. 2723 return; 2724 case ISD::AssertSext: 2725 case ISD::AssertZext: 2726 case ISD::AssertAlign: 2727 ReplaceUses(SDValue(NodeToMatch, 0), NodeToMatch->getOperand(0)); 2728 CurDAG->RemoveDeadNode(NodeToMatch); 2729 return; 2730 case ISD::INLINEASM: 2731 case ISD::INLINEASM_BR: 2732 Select_INLINEASM(NodeToMatch); 2733 return; 2734 case ISD::READ_REGISTER: 2735 Select_READ_REGISTER(NodeToMatch); 2736 return; 2737 case ISD::WRITE_REGISTER: 2738 Select_WRITE_REGISTER(NodeToMatch); 2739 return; 2740 case ISD::UNDEF: 2741 Select_UNDEF(NodeToMatch); 2742 return; 2743 case ISD::FREEZE: 2744 Select_FREEZE(NodeToMatch); 2745 return; 2746 case ISD::ARITH_FENCE: 2747 Select_ARITH_FENCE(NodeToMatch); 2748 return; 2749 } 2750 2751 assert(!NodeToMatch->isMachineOpcode() && "Node already selected!"); 2752 2753 // Set up the node stack with NodeToMatch as the only node on the stack. 2754 SmallVector<SDValue, 8> NodeStack; 2755 SDValue N = SDValue(NodeToMatch, 0); 2756 NodeStack.push_back(N); 2757 2758 // MatchScopes - Scopes used when matching, if a match failure happens, this 2759 // indicates where to continue checking. 2760 SmallVector<MatchScope, 8> MatchScopes; 2761 2762 // RecordedNodes - This is the set of nodes that have been recorded by the 2763 // state machine. The second value is the parent of the node, or null if the 2764 // root is recorded. 2765 SmallVector<std::pair<SDValue, SDNode*>, 8> RecordedNodes; 2766 2767 // MatchedMemRefs - This is the set of MemRef's we've seen in the input 2768 // pattern. 2769 SmallVector<MachineMemOperand*, 2> MatchedMemRefs; 2770 2771 // These are the current input chain and glue for use when generating nodes. 2772 // Various Emit operations change these. For example, emitting a copytoreg 2773 // uses and updates these. 2774 SDValue InputChain, InputGlue; 2775 2776 // ChainNodesMatched - If a pattern matches nodes that have input/output 2777 // chains, the OPC_EmitMergeInputChains operation is emitted which indicates 2778 // which ones they are. The result is captured into this list so that we can 2779 // update the chain results when the pattern is complete. 2780 SmallVector<SDNode*, 3> ChainNodesMatched; 2781 2782 LLVM_DEBUG(dbgs() << "ISEL: Starting pattern match\n"); 2783 2784 // Determine where to start the interpreter. Normally we start at opcode #0, 2785 // but if the state machine starts with an OPC_SwitchOpcode, then we 2786 // accelerate the first lookup (which is guaranteed to be hot) with the 2787 // OpcodeOffset table. 2788 unsigned MatcherIndex = 0; 2789 2790 if (!OpcodeOffset.empty()) { 2791 // Already computed the OpcodeOffset table, just index into it. 2792 if (N.getOpcode() < OpcodeOffset.size()) 2793 MatcherIndex = OpcodeOffset[N.getOpcode()]; 2794 LLVM_DEBUG(dbgs() << " Initial Opcode index to " << MatcherIndex << "\n"); 2795 2796 } else if (MatcherTable[0] == OPC_SwitchOpcode) { 2797 // Otherwise, the table isn't computed, but the state machine does start 2798 // with an OPC_SwitchOpcode instruction. Populate the table now, since this 2799 // is the first time we're selecting an instruction. 2800 unsigned Idx = 1; 2801 while (true) { 2802 // Get the size of this case. 2803 unsigned CaseSize = MatcherTable[Idx++]; 2804 if (CaseSize & 128) 2805 CaseSize = GetVBR(CaseSize, MatcherTable, Idx); 2806 if (CaseSize == 0) break; 2807 2808 // Get the opcode, add the index to the table. 2809 uint16_t Opc = MatcherTable[Idx++]; 2810 Opc |= (unsigned short)MatcherTable[Idx++] << 8; 2811 if (Opc >= OpcodeOffset.size()) 2812 OpcodeOffset.resize((Opc+1)*2); 2813 OpcodeOffset[Opc] = Idx; 2814 Idx += CaseSize; 2815 } 2816 2817 // Okay, do the lookup for the first opcode. 2818 if (N.getOpcode() < OpcodeOffset.size()) 2819 MatcherIndex = OpcodeOffset[N.getOpcode()]; 2820 } 2821 2822 while (true) { 2823 assert(MatcherIndex < TableSize && "Invalid index"); 2824 #ifndef NDEBUG 2825 unsigned CurrentOpcodeIndex = MatcherIndex; 2826 #endif 2827 BuiltinOpcodes Opcode = (BuiltinOpcodes)MatcherTable[MatcherIndex++]; 2828 switch (Opcode) { 2829 case OPC_Scope: { 2830 // Okay, the semantics of this operation are that we should push a scope 2831 // then evaluate the first child. However, pushing a scope only to have 2832 // the first check fail (which then pops it) is inefficient. If we can 2833 // determine immediately that the first check (or first several) will 2834 // immediately fail, don't even bother pushing a scope for them. 2835 unsigned FailIndex; 2836 2837 while (true) { 2838 unsigned NumToSkip = MatcherTable[MatcherIndex++]; 2839 if (NumToSkip & 128) 2840 NumToSkip = GetVBR(NumToSkip, MatcherTable, MatcherIndex); 2841 // Found the end of the scope with no match. 2842 if (NumToSkip == 0) { 2843 FailIndex = 0; 2844 break; 2845 } 2846 2847 FailIndex = MatcherIndex+NumToSkip; 2848 2849 unsigned MatcherIndexOfPredicate = MatcherIndex; 2850 (void)MatcherIndexOfPredicate; // silence warning. 2851 2852 // If we can't evaluate this predicate without pushing a scope (e.g. if 2853 // it is a 'MoveParent') or if the predicate succeeds on this node, we 2854 // push the scope and evaluate the full predicate chain. 2855 bool Result; 2856 MatcherIndex = IsPredicateKnownToFail(MatcherTable, MatcherIndex, N, 2857 Result, *this, RecordedNodes); 2858 if (!Result) 2859 break; 2860 2861 LLVM_DEBUG( 2862 dbgs() << " Skipped scope entry (due to false predicate) at " 2863 << "index " << MatcherIndexOfPredicate << ", continuing at " 2864 << FailIndex << "\n"); 2865 ++NumDAGIselRetries; 2866 2867 // Otherwise, we know that this case of the Scope is guaranteed to fail, 2868 // move to the next case. 2869 MatcherIndex = FailIndex; 2870 } 2871 2872 // If the whole scope failed to match, bail. 2873 if (FailIndex == 0) break; 2874 2875 // Push a MatchScope which indicates where to go if the first child fails 2876 // to match. 2877 MatchScope NewEntry; 2878 NewEntry.FailIndex = FailIndex; 2879 NewEntry.NodeStack.append(NodeStack.begin(), NodeStack.end()); 2880 NewEntry.NumRecordedNodes = RecordedNodes.size(); 2881 NewEntry.NumMatchedMemRefs = MatchedMemRefs.size(); 2882 NewEntry.InputChain = InputChain; 2883 NewEntry.InputGlue = InputGlue; 2884 NewEntry.HasChainNodesMatched = !ChainNodesMatched.empty(); 2885 MatchScopes.push_back(NewEntry); 2886 continue; 2887 } 2888 case OPC_RecordNode: { 2889 // Remember this node, it may end up being an operand in the pattern. 2890 SDNode *Parent = nullptr; 2891 if (NodeStack.size() > 1) 2892 Parent = NodeStack[NodeStack.size()-2].getNode(); 2893 RecordedNodes.push_back(std::make_pair(N, Parent)); 2894 continue; 2895 } 2896 2897 case OPC_RecordChild0: case OPC_RecordChild1: 2898 case OPC_RecordChild2: case OPC_RecordChild3: 2899 case OPC_RecordChild4: case OPC_RecordChild5: 2900 case OPC_RecordChild6: case OPC_RecordChild7: { 2901 unsigned ChildNo = Opcode-OPC_RecordChild0; 2902 if (ChildNo >= N.getNumOperands()) 2903 break; // Match fails if out of range child #. 2904 2905 RecordedNodes.push_back(std::make_pair(N->getOperand(ChildNo), 2906 N.getNode())); 2907 continue; 2908 } 2909 case OPC_RecordMemRef: 2910 if (auto *MN = dyn_cast<MemSDNode>(N)) 2911 MatchedMemRefs.push_back(MN->getMemOperand()); 2912 else { 2913 LLVM_DEBUG(dbgs() << "Expected MemSDNode "; N->dump(CurDAG); 2914 dbgs() << '\n'); 2915 } 2916 2917 continue; 2918 2919 case OPC_CaptureGlueInput: 2920 // If the current node has an input glue, capture it in InputGlue. 2921 if (N->getNumOperands() != 0 && 2922 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) 2923 InputGlue = N->getOperand(N->getNumOperands()-1); 2924 continue; 2925 2926 case OPC_MoveChild: { 2927 unsigned ChildNo = MatcherTable[MatcherIndex++]; 2928 if (ChildNo >= N.getNumOperands()) 2929 break; // Match fails if out of range child #. 2930 N = N.getOperand(ChildNo); 2931 NodeStack.push_back(N); 2932 continue; 2933 } 2934 2935 case OPC_MoveChild0: case OPC_MoveChild1: 2936 case OPC_MoveChild2: case OPC_MoveChild3: 2937 case OPC_MoveChild4: case OPC_MoveChild5: 2938 case OPC_MoveChild6: case OPC_MoveChild7: { 2939 unsigned ChildNo = Opcode-OPC_MoveChild0; 2940 if (ChildNo >= N.getNumOperands()) 2941 break; // Match fails if out of range child #. 2942 N = N.getOperand(ChildNo); 2943 NodeStack.push_back(N); 2944 continue; 2945 } 2946 2947 case OPC_MoveParent: 2948 // Pop the current node off the NodeStack. 2949 NodeStack.pop_back(); 2950 assert(!NodeStack.empty() && "Node stack imbalance!"); 2951 N = NodeStack.back(); 2952 continue; 2953 2954 case OPC_CheckSame: 2955 if (!::CheckSame(MatcherTable, MatcherIndex, N, RecordedNodes)) break; 2956 continue; 2957 2958 case OPC_CheckChild0Same: case OPC_CheckChild1Same: 2959 case OPC_CheckChild2Same: case OPC_CheckChild3Same: 2960 if (!::CheckChildSame(MatcherTable, MatcherIndex, N, RecordedNodes, 2961 Opcode-OPC_CheckChild0Same)) 2962 break; 2963 continue; 2964 2965 case OPC_CheckPatternPredicate: 2966 if (!::CheckPatternPredicate(MatcherTable, MatcherIndex, *this)) break; 2967 continue; 2968 case OPC_CheckPredicate: 2969 if (!::CheckNodePredicate(MatcherTable, MatcherIndex, *this, 2970 N.getNode())) 2971 break; 2972 continue; 2973 case OPC_CheckPredicateWithOperands: { 2974 unsigned OpNum = MatcherTable[MatcherIndex++]; 2975 SmallVector<SDValue, 8> Operands; 2976 2977 for (unsigned i = 0; i < OpNum; ++i) 2978 Operands.push_back(RecordedNodes[MatcherTable[MatcherIndex++]].first); 2979 2980 unsigned PredNo = MatcherTable[MatcherIndex++]; 2981 if (!CheckNodePredicateWithOperands(N.getNode(), PredNo, Operands)) 2982 break; 2983 continue; 2984 } 2985 case OPC_CheckComplexPat: { 2986 unsigned CPNum = MatcherTable[MatcherIndex++]; 2987 unsigned RecNo = MatcherTable[MatcherIndex++]; 2988 assert(RecNo < RecordedNodes.size() && "Invalid CheckComplexPat"); 2989 2990 // If target can modify DAG during matching, keep the matching state 2991 // consistent. 2992 std::unique_ptr<MatchStateUpdater> MSU; 2993 if (ComplexPatternFuncMutatesDAG()) 2994 MSU.reset(new MatchStateUpdater(*CurDAG, &NodeToMatch, RecordedNodes, 2995 MatchScopes)); 2996 2997 if (!CheckComplexPattern(NodeToMatch, RecordedNodes[RecNo].second, 2998 RecordedNodes[RecNo].first, CPNum, 2999 RecordedNodes)) 3000 break; 3001 continue; 3002 } 3003 case OPC_CheckOpcode: 3004 if (!::CheckOpcode(MatcherTable, MatcherIndex, N.getNode())) break; 3005 continue; 3006 3007 case OPC_CheckType: 3008 if (!::CheckType(MatcherTable, MatcherIndex, N, TLI, 3009 CurDAG->getDataLayout())) 3010 break; 3011 continue; 3012 3013 case OPC_CheckTypeRes: { 3014 unsigned Res = MatcherTable[MatcherIndex++]; 3015 if (!::CheckType(MatcherTable, MatcherIndex, N.getValue(Res), TLI, 3016 CurDAG->getDataLayout())) 3017 break; 3018 continue; 3019 } 3020 3021 case OPC_SwitchOpcode: { 3022 unsigned CurNodeOpcode = N.getOpcode(); 3023 unsigned SwitchStart = MatcherIndex-1; (void)SwitchStart; 3024 unsigned CaseSize; 3025 while (true) { 3026 // Get the size of this case. 3027 CaseSize = MatcherTable[MatcherIndex++]; 3028 if (CaseSize & 128) 3029 CaseSize = GetVBR(CaseSize, MatcherTable, MatcherIndex); 3030 if (CaseSize == 0) break; 3031 3032 uint16_t Opc = MatcherTable[MatcherIndex++]; 3033 Opc |= (unsigned short)MatcherTable[MatcherIndex++] << 8; 3034 3035 // If the opcode matches, then we will execute this case. 3036 if (CurNodeOpcode == Opc) 3037 break; 3038 3039 // Otherwise, skip over this case. 3040 MatcherIndex += CaseSize; 3041 } 3042 3043 // If no cases matched, bail out. 3044 if (CaseSize == 0) break; 3045 3046 // Otherwise, execute the case we found. 3047 LLVM_DEBUG(dbgs() << " OpcodeSwitch from " << SwitchStart << " to " 3048 << MatcherIndex << "\n"); 3049 continue; 3050 } 3051 3052 case OPC_SwitchType: { 3053 MVT CurNodeVT = N.getSimpleValueType(); 3054 unsigned SwitchStart = MatcherIndex-1; (void)SwitchStart; 3055 unsigned CaseSize; 3056 while (true) { 3057 // Get the size of this case. 3058 CaseSize = MatcherTable[MatcherIndex++]; 3059 if (CaseSize & 128) 3060 CaseSize = GetVBR(CaseSize, MatcherTable, MatcherIndex); 3061 if (CaseSize == 0) break; 3062 3063 MVT CaseVT = (MVT::SimpleValueType)MatcherTable[MatcherIndex++]; 3064 if (CaseVT == MVT::iPTR) 3065 CaseVT = TLI->getPointerTy(CurDAG->getDataLayout()); 3066 3067 // If the VT matches, then we will execute this case. 3068 if (CurNodeVT == CaseVT) 3069 break; 3070 3071 // Otherwise, skip over this case. 3072 MatcherIndex += CaseSize; 3073 } 3074 3075 // If no cases matched, bail out. 3076 if (CaseSize == 0) break; 3077 3078 // Otherwise, execute the case we found. 3079 LLVM_DEBUG(dbgs() << " TypeSwitch[" << EVT(CurNodeVT).getEVTString() 3080 << "] from " << SwitchStart << " to " << MatcherIndex 3081 << '\n'); 3082 continue; 3083 } 3084 case OPC_CheckChild0Type: case OPC_CheckChild1Type: 3085 case OPC_CheckChild2Type: case OPC_CheckChild3Type: 3086 case OPC_CheckChild4Type: case OPC_CheckChild5Type: 3087 case OPC_CheckChild6Type: case OPC_CheckChild7Type: 3088 if (!::CheckChildType(MatcherTable, MatcherIndex, N, TLI, 3089 CurDAG->getDataLayout(), 3090 Opcode - OPC_CheckChild0Type)) 3091 break; 3092 continue; 3093 case OPC_CheckCondCode: 3094 if (!::CheckCondCode(MatcherTable, MatcherIndex, N)) break; 3095 continue; 3096 case OPC_CheckChild2CondCode: 3097 if (!::CheckChild2CondCode(MatcherTable, MatcherIndex, N)) break; 3098 continue; 3099 case OPC_CheckValueType: 3100 if (!::CheckValueType(MatcherTable, MatcherIndex, N, TLI, 3101 CurDAG->getDataLayout())) 3102 break; 3103 continue; 3104 case OPC_CheckInteger: 3105 if (!::CheckInteger(MatcherTable, MatcherIndex, N)) break; 3106 continue; 3107 case OPC_CheckChild0Integer: case OPC_CheckChild1Integer: 3108 case OPC_CheckChild2Integer: case OPC_CheckChild3Integer: 3109 case OPC_CheckChild4Integer: 3110 if (!::CheckChildInteger(MatcherTable, MatcherIndex, N, 3111 Opcode-OPC_CheckChild0Integer)) break; 3112 continue; 3113 case OPC_CheckAndImm: 3114 if (!::CheckAndImm(MatcherTable, MatcherIndex, N, *this)) break; 3115 continue; 3116 case OPC_CheckOrImm: 3117 if (!::CheckOrImm(MatcherTable, MatcherIndex, N, *this)) break; 3118 continue; 3119 case OPC_CheckImmAllOnesV: 3120 if (!ISD::isConstantSplatVectorAllOnes(N.getNode())) 3121 break; 3122 continue; 3123 case OPC_CheckImmAllZerosV: 3124 if (!ISD::isConstantSplatVectorAllZeros(N.getNode())) 3125 break; 3126 continue; 3127 3128 case OPC_CheckFoldableChainNode: { 3129 assert(NodeStack.size() != 1 && "No parent node"); 3130 // Verify that all intermediate nodes between the root and this one have 3131 // a single use (ignoring chains, which are handled in UpdateChains). 3132 bool HasMultipleUses = false; 3133 for (unsigned i = 1, e = NodeStack.size()-1; i != e; ++i) { 3134 unsigned NNonChainUses = 0; 3135 SDNode *NS = NodeStack[i].getNode(); 3136 for (auto UI = NS->use_begin(), UE = NS->use_end(); UI != UE; ++UI) 3137 if (UI.getUse().getValueType() != MVT::Other) 3138 if (++NNonChainUses > 1) { 3139 HasMultipleUses = true; 3140 break; 3141 } 3142 if (HasMultipleUses) break; 3143 } 3144 if (HasMultipleUses) break; 3145 3146 // Check to see that the target thinks this is profitable to fold and that 3147 // we can fold it without inducing cycles in the graph. 3148 if (!IsProfitableToFold(N, NodeStack[NodeStack.size()-2].getNode(), 3149 NodeToMatch) || 3150 !IsLegalToFold(N, NodeStack[NodeStack.size()-2].getNode(), 3151 NodeToMatch, OptLevel, 3152 true/*We validate our own chains*/)) 3153 break; 3154 3155 continue; 3156 } 3157 case OPC_EmitInteger: 3158 case OPC_EmitStringInteger: { 3159 MVT::SimpleValueType VT = 3160 (MVT::SimpleValueType)MatcherTable[MatcherIndex++]; 3161 int64_t Val = MatcherTable[MatcherIndex++]; 3162 if (Val & 128) 3163 Val = GetVBR(Val, MatcherTable, MatcherIndex); 3164 if (Opcode == OPC_EmitInteger) 3165 Val = decodeSignRotatedValue(Val); 3166 RecordedNodes.push_back(std::pair<SDValue, SDNode*>( 3167 CurDAG->getTargetConstant(Val, SDLoc(NodeToMatch), 3168 VT), nullptr)); 3169 continue; 3170 } 3171 case OPC_EmitRegister: { 3172 MVT::SimpleValueType VT = 3173 (MVT::SimpleValueType)MatcherTable[MatcherIndex++]; 3174 unsigned RegNo = MatcherTable[MatcherIndex++]; 3175 RecordedNodes.push_back(std::pair<SDValue, SDNode*>( 3176 CurDAG->getRegister(RegNo, VT), nullptr)); 3177 continue; 3178 } 3179 case OPC_EmitRegister2: { 3180 // For targets w/ more than 256 register names, the register enum 3181 // values are stored in two bytes in the matcher table (just like 3182 // opcodes). 3183 MVT::SimpleValueType VT = 3184 (MVT::SimpleValueType)MatcherTable[MatcherIndex++]; 3185 unsigned RegNo = MatcherTable[MatcherIndex++]; 3186 RegNo |= MatcherTable[MatcherIndex++] << 8; 3187 RecordedNodes.push_back(std::pair<SDValue, SDNode*>( 3188 CurDAG->getRegister(RegNo, VT), nullptr)); 3189 continue; 3190 } 3191 3192 case OPC_EmitConvertToTarget: { 3193 // Convert from IMM/FPIMM to target version. 3194 unsigned RecNo = MatcherTable[MatcherIndex++]; 3195 assert(RecNo < RecordedNodes.size() && "Invalid EmitConvertToTarget"); 3196 SDValue Imm = RecordedNodes[RecNo].first; 3197 3198 if (Imm->getOpcode() == ISD::Constant) { 3199 const ConstantInt *Val=cast<ConstantSDNode>(Imm)->getConstantIntValue(); 3200 Imm = CurDAG->getTargetConstant(*Val, SDLoc(NodeToMatch), 3201 Imm.getValueType()); 3202 } else if (Imm->getOpcode() == ISD::ConstantFP) { 3203 const ConstantFP *Val=cast<ConstantFPSDNode>(Imm)->getConstantFPValue(); 3204 Imm = CurDAG->getTargetConstantFP(*Val, SDLoc(NodeToMatch), 3205 Imm.getValueType()); 3206 } 3207 3208 RecordedNodes.push_back(std::make_pair(Imm, RecordedNodes[RecNo].second)); 3209 continue; 3210 } 3211 3212 case OPC_EmitMergeInputChains1_0: // OPC_EmitMergeInputChains, 1, 0 3213 case OPC_EmitMergeInputChains1_1: // OPC_EmitMergeInputChains, 1, 1 3214 case OPC_EmitMergeInputChains1_2: { // OPC_EmitMergeInputChains, 1, 2 3215 // These are space-optimized forms of OPC_EmitMergeInputChains. 3216 assert(!InputChain.getNode() && 3217 "EmitMergeInputChains should be the first chain producing node"); 3218 assert(ChainNodesMatched.empty() && 3219 "Should only have one EmitMergeInputChains per match"); 3220 3221 // Read all of the chained nodes. 3222 unsigned RecNo = Opcode - OPC_EmitMergeInputChains1_0; 3223 assert(RecNo < RecordedNodes.size() && "Invalid EmitMergeInputChains"); 3224 ChainNodesMatched.push_back(RecordedNodes[RecNo].first.getNode()); 3225 3226 // If the chained node is not the root, we can't fold it if it has 3227 // multiple uses. 3228 // FIXME: What if other value results of the node have uses not matched 3229 // by this pattern? 3230 if (ChainNodesMatched.back() != NodeToMatch && 3231 !RecordedNodes[RecNo].first.hasOneUse()) { 3232 ChainNodesMatched.clear(); 3233 break; 3234 } 3235 3236 // Merge the input chains if they are not intra-pattern references. 3237 InputChain = HandleMergeInputChains(ChainNodesMatched, CurDAG); 3238 3239 if (!InputChain.getNode()) 3240 break; // Failed to merge. 3241 continue; 3242 } 3243 3244 case OPC_EmitMergeInputChains: { 3245 assert(!InputChain.getNode() && 3246 "EmitMergeInputChains should be the first chain producing node"); 3247 // This node gets a list of nodes we matched in the input that have 3248 // chains. We want to token factor all of the input chains to these nodes 3249 // together. However, if any of the input chains is actually one of the 3250 // nodes matched in this pattern, then we have an intra-match reference. 3251 // Ignore these because the newly token factored chain should not refer to 3252 // the old nodes. 3253 unsigned NumChains = MatcherTable[MatcherIndex++]; 3254 assert(NumChains != 0 && "Can't TF zero chains"); 3255 3256 assert(ChainNodesMatched.empty() && 3257 "Should only have one EmitMergeInputChains per match"); 3258 3259 // Read all of the chained nodes. 3260 for (unsigned i = 0; i != NumChains; ++i) { 3261 unsigned RecNo = MatcherTable[MatcherIndex++]; 3262 assert(RecNo < RecordedNodes.size() && "Invalid EmitMergeInputChains"); 3263 ChainNodesMatched.push_back(RecordedNodes[RecNo].first.getNode()); 3264 3265 // If the chained node is not the root, we can't fold it if it has 3266 // multiple uses. 3267 // FIXME: What if other value results of the node have uses not matched 3268 // by this pattern? 3269 if (ChainNodesMatched.back() != NodeToMatch && 3270 !RecordedNodes[RecNo].first.hasOneUse()) { 3271 ChainNodesMatched.clear(); 3272 break; 3273 } 3274 } 3275 3276 // If the inner loop broke out, the match fails. 3277 if (ChainNodesMatched.empty()) 3278 break; 3279 3280 // Merge the input chains if they are not intra-pattern references. 3281 InputChain = HandleMergeInputChains(ChainNodesMatched, CurDAG); 3282 3283 if (!InputChain.getNode()) 3284 break; // Failed to merge. 3285 3286 continue; 3287 } 3288 3289 case OPC_EmitCopyToReg: 3290 case OPC_EmitCopyToReg2: { 3291 unsigned RecNo = MatcherTable[MatcherIndex++]; 3292 assert(RecNo < RecordedNodes.size() && "Invalid EmitCopyToReg"); 3293 unsigned DestPhysReg = MatcherTable[MatcherIndex++]; 3294 if (Opcode == OPC_EmitCopyToReg2) 3295 DestPhysReg |= MatcherTable[MatcherIndex++] << 8; 3296 3297 if (!InputChain.getNode()) 3298 InputChain = CurDAG->getEntryNode(); 3299 3300 InputChain = CurDAG->getCopyToReg(InputChain, SDLoc(NodeToMatch), 3301 DestPhysReg, RecordedNodes[RecNo].first, 3302 InputGlue); 3303 3304 InputGlue = InputChain.getValue(1); 3305 continue; 3306 } 3307 3308 case OPC_EmitNodeXForm: { 3309 unsigned XFormNo = MatcherTable[MatcherIndex++]; 3310 unsigned RecNo = MatcherTable[MatcherIndex++]; 3311 assert(RecNo < RecordedNodes.size() && "Invalid EmitNodeXForm"); 3312 SDValue Res = RunSDNodeXForm(RecordedNodes[RecNo].first, XFormNo); 3313 RecordedNodes.push_back(std::pair<SDValue,SDNode*>(Res, nullptr)); 3314 continue; 3315 } 3316 case OPC_Coverage: { 3317 // This is emitted right before MorphNode/EmitNode. 3318 // So it should be safe to assume that this node has been selected 3319 unsigned index = MatcherTable[MatcherIndex++]; 3320 index |= (MatcherTable[MatcherIndex++] << 8); 3321 dbgs() << "COVERED: " << getPatternForIndex(index) << "\n"; 3322 dbgs() << "INCLUDED: " << getIncludePathForIndex(index) << "\n"; 3323 continue; 3324 } 3325 3326 case OPC_EmitNode: case OPC_MorphNodeTo: 3327 case OPC_EmitNode0: case OPC_EmitNode1: case OPC_EmitNode2: 3328 case OPC_MorphNodeTo0: case OPC_MorphNodeTo1: case OPC_MorphNodeTo2: { 3329 uint16_t TargetOpc = MatcherTable[MatcherIndex++]; 3330 TargetOpc |= (unsigned short)MatcherTable[MatcherIndex++] << 8; 3331 unsigned EmitNodeInfo = MatcherTable[MatcherIndex++]; 3332 // Get the result VT list. 3333 unsigned NumVTs; 3334 // If this is one of the compressed forms, get the number of VTs based 3335 // on the Opcode. Otherwise read the next byte from the table. 3336 if (Opcode >= OPC_MorphNodeTo0 && Opcode <= OPC_MorphNodeTo2) 3337 NumVTs = Opcode - OPC_MorphNodeTo0; 3338 else if (Opcode >= OPC_EmitNode0 && Opcode <= OPC_EmitNode2) 3339 NumVTs = Opcode - OPC_EmitNode0; 3340 else 3341 NumVTs = MatcherTable[MatcherIndex++]; 3342 SmallVector<EVT, 4> VTs; 3343 for (unsigned i = 0; i != NumVTs; ++i) { 3344 MVT::SimpleValueType VT = 3345 (MVT::SimpleValueType)MatcherTable[MatcherIndex++]; 3346 if (VT == MVT::iPTR) 3347 VT = TLI->getPointerTy(CurDAG->getDataLayout()).SimpleTy; 3348 VTs.push_back(VT); 3349 } 3350 3351 if (EmitNodeInfo & OPFL_Chain) 3352 VTs.push_back(MVT::Other); 3353 if (EmitNodeInfo & OPFL_GlueOutput) 3354 VTs.push_back(MVT::Glue); 3355 3356 // This is hot code, so optimize the two most common cases of 1 and 2 3357 // results. 3358 SDVTList VTList; 3359 if (VTs.size() == 1) 3360 VTList = CurDAG->getVTList(VTs[0]); 3361 else if (VTs.size() == 2) 3362 VTList = CurDAG->getVTList(VTs[0], VTs[1]); 3363 else 3364 VTList = CurDAG->getVTList(VTs); 3365 3366 // Get the operand list. 3367 unsigned NumOps = MatcherTable[MatcherIndex++]; 3368 SmallVector<SDValue, 8> Ops; 3369 for (unsigned i = 0; i != NumOps; ++i) { 3370 unsigned RecNo = MatcherTable[MatcherIndex++]; 3371 if (RecNo & 128) 3372 RecNo = GetVBR(RecNo, MatcherTable, MatcherIndex); 3373 3374 assert(RecNo < RecordedNodes.size() && "Invalid EmitNode"); 3375 Ops.push_back(RecordedNodes[RecNo].first); 3376 } 3377 3378 // If there are variadic operands to add, handle them now. 3379 if (EmitNodeInfo & OPFL_VariadicInfo) { 3380 // Determine the start index to copy from. 3381 unsigned FirstOpToCopy = getNumFixedFromVariadicInfo(EmitNodeInfo); 3382 FirstOpToCopy += (EmitNodeInfo & OPFL_Chain) ? 1 : 0; 3383 assert(NodeToMatch->getNumOperands() >= FirstOpToCopy && 3384 "Invalid variadic node"); 3385 // Copy all of the variadic operands, not including a potential glue 3386 // input. 3387 for (unsigned i = FirstOpToCopy, e = NodeToMatch->getNumOperands(); 3388 i != e; ++i) { 3389 SDValue V = NodeToMatch->getOperand(i); 3390 if (V.getValueType() == MVT::Glue) break; 3391 Ops.push_back(V); 3392 } 3393 } 3394 3395 // If this has chain/glue inputs, add them. 3396 if (EmitNodeInfo & OPFL_Chain) 3397 Ops.push_back(InputChain); 3398 if ((EmitNodeInfo & OPFL_GlueInput) && InputGlue.getNode() != nullptr) 3399 Ops.push_back(InputGlue); 3400 3401 // Check whether any matched node could raise an FP exception. Since all 3402 // such nodes must have a chain, it suffices to check ChainNodesMatched. 3403 // We need to perform this check before potentially modifying one of the 3404 // nodes via MorphNode. 3405 bool MayRaiseFPException = 3406 llvm::any_of(ChainNodesMatched, [this](SDNode *N) { 3407 return mayRaiseFPException(N) && !N->getFlags().hasNoFPExcept(); 3408 }); 3409 3410 // Create the node. 3411 MachineSDNode *Res = nullptr; 3412 bool IsMorphNodeTo = Opcode == OPC_MorphNodeTo || 3413 (Opcode >= OPC_MorphNodeTo0 && Opcode <= OPC_MorphNodeTo2); 3414 if (!IsMorphNodeTo) { 3415 // If this is a normal EmitNode command, just create the new node and 3416 // add the results to the RecordedNodes list. 3417 Res = CurDAG->getMachineNode(TargetOpc, SDLoc(NodeToMatch), 3418 VTList, Ops); 3419 3420 // Add all the non-glue/non-chain results to the RecordedNodes list. 3421 for (unsigned i = 0, e = VTs.size(); i != e; ++i) { 3422 if (VTs[i] == MVT::Other || VTs[i] == MVT::Glue) break; 3423 RecordedNodes.push_back(std::pair<SDValue,SDNode*>(SDValue(Res, i), 3424 nullptr)); 3425 } 3426 } else { 3427 assert(NodeToMatch->getOpcode() != ISD::DELETED_NODE && 3428 "NodeToMatch was removed partway through selection"); 3429 SelectionDAG::DAGNodeDeletedListener NDL(*CurDAG, [&](SDNode *N, 3430 SDNode *E) { 3431 CurDAG->salvageDebugInfo(*N); 3432 auto &Chain = ChainNodesMatched; 3433 assert((!E || !is_contained(Chain, N)) && 3434 "Chain node replaced during MorphNode"); 3435 llvm::erase_value(Chain, N); 3436 }); 3437 Res = cast<MachineSDNode>(MorphNode(NodeToMatch, TargetOpc, VTList, 3438 Ops, EmitNodeInfo)); 3439 } 3440 3441 // Set the NoFPExcept flag when no original matched node could 3442 // raise an FP exception, but the new node potentially might. 3443 if (!MayRaiseFPException && mayRaiseFPException(Res)) { 3444 SDNodeFlags Flags = Res->getFlags(); 3445 Flags.setNoFPExcept(true); 3446 Res->setFlags(Flags); 3447 } 3448 3449 // If the node had chain/glue results, update our notion of the current 3450 // chain and glue. 3451 if (EmitNodeInfo & OPFL_GlueOutput) { 3452 InputGlue = SDValue(Res, VTs.size()-1); 3453 if (EmitNodeInfo & OPFL_Chain) 3454 InputChain = SDValue(Res, VTs.size()-2); 3455 } else if (EmitNodeInfo & OPFL_Chain) 3456 InputChain = SDValue(Res, VTs.size()-1); 3457 3458 // If the OPFL_MemRefs glue is set on this node, slap all of the 3459 // accumulated memrefs onto it. 3460 // 3461 // FIXME: This is vastly incorrect for patterns with multiple outputs 3462 // instructions that access memory and for ComplexPatterns that match 3463 // loads. 3464 if (EmitNodeInfo & OPFL_MemRefs) { 3465 // Only attach load or store memory operands if the generated 3466 // instruction may load or store. 3467 const MCInstrDesc &MCID = TII->get(TargetOpc); 3468 bool mayLoad = MCID.mayLoad(); 3469 bool mayStore = MCID.mayStore(); 3470 3471 // We expect to have relatively few of these so just filter them into a 3472 // temporary buffer so that we can easily add them to the instruction. 3473 SmallVector<MachineMemOperand *, 4> FilteredMemRefs; 3474 for (MachineMemOperand *MMO : MatchedMemRefs) { 3475 if (MMO->isLoad()) { 3476 if (mayLoad) 3477 FilteredMemRefs.push_back(MMO); 3478 } else if (MMO->isStore()) { 3479 if (mayStore) 3480 FilteredMemRefs.push_back(MMO); 3481 } else { 3482 FilteredMemRefs.push_back(MMO); 3483 } 3484 } 3485 3486 CurDAG->setNodeMemRefs(Res, FilteredMemRefs); 3487 } 3488 3489 LLVM_DEBUG(if (!MatchedMemRefs.empty() && Res->memoperands_empty()) dbgs() 3490 << " Dropping mem operands\n"; 3491 dbgs() << " " << (IsMorphNodeTo ? "Morphed" : "Created") 3492 << " node: "; 3493 Res->dump(CurDAG);); 3494 3495 // If this was a MorphNodeTo then we're completely done! 3496 if (IsMorphNodeTo) { 3497 // Update chain uses. 3498 UpdateChains(Res, InputChain, ChainNodesMatched, true); 3499 return; 3500 } 3501 continue; 3502 } 3503 3504 case OPC_CompleteMatch: { 3505 // The match has been completed, and any new nodes (if any) have been 3506 // created. Patch up references to the matched dag to use the newly 3507 // created nodes. 3508 unsigned NumResults = MatcherTable[MatcherIndex++]; 3509 3510 for (unsigned i = 0; i != NumResults; ++i) { 3511 unsigned ResSlot = MatcherTable[MatcherIndex++]; 3512 if (ResSlot & 128) 3513 ResSlot = GetVBR(ResSlot, MatcherTable, MatcherIndex); 3514 3515 assert(ResSlot < RecordedNodes.size() && "Invalid CompleteMatch"); 3516 SDValue Res = RecordedNodes[ResSlot].first; 3517 3518 assert(i < NodeToMatch->getNumValues() && 3519 NodeToMatch->getValueType(i) != MVT::Other && 3520 NodeToMatch->getValueType(i) != MVT::Glue && 3521 "Invalid number of results to complete!"); 3522 assert((NodeToMatch->getValueType(i) == Res.getValueType() || 3523 NodeToMatch->getValueType(i) == MVT::iPTR || 3524 Res.getValueType() == MVT::iPTR || 3525 NodeToMatch->getValueType(i).getSizeInBits() == 3526 Res.getValueSizeInBits()) && 3527 "invalid replacement"); 3528 ReplaceUses(SDValue(NodeToMatch, i), Res); 3529 } 3530 3531 // Update chain uses. 3532 UpdateChains(NodeToMatch, InputChain, ChainNodesMatched, false); 3533 3534 // If the root node defines glue, we need to update it to the glue result. 3535 // TODO: This never happens in our tests and I think it can be removed / 3536 // replaced with an assert, but if we do it this the way the change is 3537 // NFC. 3538 if (NodeToMatch->getValueType(NodeToMatch->getNumValues() - 1) == 3539 MVT::Glue && 3540 InputGlue.getNode()) 3541 ReplaceUses(SDValue(NodeToMatch, NodeToMatch->getNumValues() - 1), 3542 InputGlue); 3543 3544 assert(NodeToMatch->use_empty() && 3545 "Didn't replace all uses of the node?"); 3546 CurDAG->RemoveDeadNode(NodeToMatch); 3547 3548 return; 3549 } 3550 } 3551 3552 // If the code reached this point, then the match failed. See if there is 3553 // another child to try in the current 'Scope', otherwise pop it until we 3554 // find a case to check. 3555 LLVM_DEBUG(dbgs() << " Match failed at index " << CurrentOpcodeIndex 3556 << "\n"); 3557 ++NumDAGIselRetries; 3558 while (true) { 3559 if (MatchScopes.empty()) { 3560 CannotYetSelect(NodeToMatch); 3561 return; 3562 } 3563 3564 // Restore the interpreter state back to the point where the scope was 3565 // formed. 3566 MatchScope &LastScope = MatchScopes.back(); 3567 RecordedNodes.resize(LastScope.NumRecordedNodes); 3568 NodeStack.clear(); 3569 NodeStack.append(LastScope.NodeStack.begin(), LastScope.NodeStack.end()); 3570 N = NodeStack.back(); 3571 3572 if (LastScope.NumMatchedMemRefs != MatchedMemRefs.size()) 3573 MatchedMemRefs.resize(LastScope.NumMatchedMemRefs); 3574 MatcherIndex = LastScope.FailIndex; 3575 3576 LLVM_DEBUG(dbgs() << " Continuing at " << MatcherIndex << "\n"); 3577 3578 InputChain = LastScope.InputChain; 3579 InputGlue = LastScope.InputGlue; 3580 if (!LastScope.HasChainNodesMatched) 3581 ChainNodesMatched.clear(); 3582 3583 // Check to see what the offset is at the new MatcherIndex. If it is zero 3584 // we have reached the end of this scope, otherwise we have another child 3585 // in the current scope to try. 3586 unsigned NumToSkip = MatcherTable[MatcherIndex++]; 3587 if (NumToSkip & 128) 3588 NumToSkip = GetVBR(NumToSkip, MatcherTable, MatcherIndex); 3589 3590 // If we have another child in this scope to match, update FailIndex and 3591 // try it. 3592 if (NumToSkip != 0) { 3593 LastScope.FailIndex = MatcherIndex+NumToSkip; 3594 break; 3595 } 3596 3597 // End of this scope, pop it and try the next child in the containing 3598 // scope. 3599 MatchScopes.pop_back(); 3600 } 3601 } 3602 } 3603 3604 /// Return whether the node may raise an FP exception. 3605 bool SelectionDAGISel::mayRaiseFPException(SDNode *N) const { 3606 // For machine opcodes, consult the MCID flag. 3607 if (N->isMachineOpcode()) { 3608 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode()); 3609 return MCID.mayRaiseFPException(); 3610 } 3611 3612 // For ISD opcodes, only StrictFP opcodes may raise an FP 3613 // exception. 3614 if (N->isTargetOpcode()) 3615 return N->isTargetStrictFPOpcode(); 3616 return N->isStrictFPOpcode(); 3617 } 3618 3619 bool SelectionDAGISel::isOrEquivalentToAdd(const SDNode *N) const { 3620 assert(N->getOpcode() == ISD::OR && "Unexpected opcode"); 3621 auto *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 3622 if (!C) 3623 return false; 3624 3625 // Detect when "or" is used to add an offset to a stack object. 3626 if (auto *FN = dyn_cast<FrameIndexSDNode>(N->getOperand(0))) { 3627 MachineFrameInfo &MFI = MF->getFrameInfo(); 3628 Align A = MFI.getObjectAlign(FN->getIndex()); 3629 int32_t Off = C->getSExtValue(); 3630 // If the alleged offset fits in the zero bits guaranteed by 3631 // the alignment, then this or is really an add. 3632 return (Off >= 0) && (((A.value() - 1) & Off) == unsigned(Off)); 3633 } 3634 return false; 3635 } 3636 3637 void SelectionDAGISel::CannotYetSelect(SDNode *N) { 3638 std::string msg; 3639 raw_string_ostream Msg(msg); 3640 Msg << "Cannot select: "; 3641 3642 if (N->getOpcode() != ISD::INTRINSIC_W_CHAIN && 3643 N->getOpcode() != ISD::INTRINSIC_WO_CHAIN && 3644 N->getOpcode() != ISD::INTRINSIC_VOID) { 3645 N->printrFull(Msg, CurDAG); 3646 Msg << "\nIn function: " << MF->getName(); 3647 } else { 3648 bool HasInputChain = N->getOperand(0).getValueType() == MVT::Other; 3649 unsigned iid = 3650 cast<ConstantSDNode>(N->getOperand(HasInputChain))->getZExtValue(); 3651 if (iid < Intrinsic::num_intrinsics) 3652 Msg << "intrinsic %" << Intrinsic::getBaseName((Intrinsic::ID)iid); 3653 else if (const TargetIntrinsicInfo *TII = TM.getIntrinsicInfo()) 3654 Msg << "target intrinsic %" << TII->getName(iid); 3655 else 3656 Msg << "unknown intrinsic #" << iid; 3657 } 3658 report_fatal_error(Twine(Msg.str())); 3659 } 3660 3661 char SelectionDAGISel::ID = 0; 3662