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