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