LLVM 23.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"
65#include "llvm/IR/BasicBlock.h"
66#include "llvm/IR/Constants.h"
67#include "llvm/IR/DataLayout.h"
68#include "llvm/IR/DebugInfo.h"
70#include "llvm/IR/DebugLoc.h"
73#include "llvm/IR/Function.h"
74#include "llvm/IR/InlineAsm.h"
76#include "llvm/IR/Instruction.h"
79#include "llvm/IR/Intrinsics.h"
80#include "llvm/IR/IntrinsicsWebAssembly.h"
81#include "llvm/IR/Metadata.h"
82#include "llvm/IR/Module.h"
83#include "llvm/IR/PrintPasses.h"
84#include "llvm/IR/Statepoint.h"
85#include "llvm/IR/Type.h"
86#include "llvm/IR/User.h"
87#include "llvm/IR/Value.h"
89#include "llvm/MC/MCInstrDesc.h"
90#include "llvm/Pass.h"
96#include "llvm/Support/Debug.h"
99#include "llvm/Support/Timer.h"
104#include <cassert>
105#include <cstdint>
106#include <iterator>
107#include <limits>
108#include <memory>
109#include <optional>
110#include <string>
111#include <utility>
112#include <vector>
113
114using namespace llvm;
115
116#define DEBUG_TYPE "isel"
117#define ISEL_DUMP_DEBUG_TYPE DEBUG_TYPE "-dump"
118
119STATISTIC(NumFastIselFailures, "Number of instructions fast isel failed on");
120STATISTIC(NumFastIselSuccess, "Number of instructions fast isel selected");
121STATISTIC(NumFastIselBlocks, "Number of blocks selected entirely by fast isel");
122STATISTIC(NumDAGBlocks, "Number of blocks selected using DAG");
123STATISTIC(NumDAGIselRetries,"Number of times dag isel has to try another path");
124STATISTIC(NumEntryBlocks, "Number of entry blocks encountered");
125STATISTIC(NumFastIselFailLowerArguments,
126 "Number of entry blocks where fast isel failed to lower arguments");
127
129 "fast-isel-abort", cl::Hidden,
130 cl::desc("Enable abort calls when \"fast\" instruction selection "
131 "fails to lower an instruction: 0 disable the abort, 1 will "
132 "abort but for args, calls and terminators, 2 will also "
133 "abort for argument lowering, and 3 will never fallback "
134 "to SelectionDAG."));
135
137 "fast-isel-report-on-fallback", cl::Hidden,
138 cl::desc("Emit a diagnostic when \"fast\" instruction selection "
139 "falls back to SelectionDAG."));
140
141static cl::opt<bool>
142UseMBPI("use-mbpi",
143 cl::desc("use Machine Branch Probability Info"),
144 cl::init(true), cl::Hidden);
145
146#ifndef NDEBUG
147static cl::opt<bool>
148 DumpSortedDAG("dump-sorted-dags", cl::Hidden,
149 cl::desc("Print DAGs with sorted nodes in debug dump"),
150 cl::init(false));
151
154 cl::desc("Only display the basic block whose name "
155 "matches this for all view-*-dags options"));
156static cl::opt<bool>
157ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
158 cl::desc("Pop up a window to show dags before the first "
159 "dag combine pass"));
160static cl::opt<bool>
161ViewLegalizeTypesDAGs("view-legalize-types-dags", cl::Hidden,
162 cl::desc("Pop up a window to show dags before legalize types"));
163static cl::opt<bool>
164 ViewDAGCombineLT("view-dag-combine-lt-dags", cl::Hidden,
165 cl::desc("Pop up a window to show dags before the post "
166 "legalize types dag combine pass"));
167static cl::opt<bool>
168 ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
169 cl::desc("Pop up a window to show dags before legalize"));
170static cl::opt<bool>
171ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
172 cl::desc("Pop up a window to show dags before the second "
173 "dag combine pass"));
174static cl::opt<bool>
175ViewISelDAGs("view-isel-dags", cl::Hidden,
176 cl::desc("Pop up a window to show isel dags as they are selected"));
177static cl::opt<bool>
178ViewSchedDAGs("view-sched-dags", cl::Hidden,
179 cl::desc("Pop up a window to show sched dags as they are processed"));
180static cl::opt<bool>
181ViewSUnitDAGs("view-sunit-dags", cl::Hidden,
182 cl::desc("Pop up a window to show SUnit dags after they are processed"));
183#else
184static const bool ViewDAGCombine1 = false, ViewLegalizeTypesDAGs = false,
185 ViewDAGCombineLT = false, ViewLegalizeDAGs = false,
186 ViewDAGCombine2 = false, ViewISelDAGs = false,
187 ViewSchedDAGs = false, ViewSUnitDAGs = false;
188#endif
189
190#ifndef NDEBUG
191#define ISEL_DUMP(X) \
192 do { \
193 if (llvm::DebugFlag && \
194 (isCurrentDebugType(DEBUG_TYPE) || \
195 (isCurrentDebugType(ISEL_DUMP_DEBUG_TYPE) && MatchFilterFuncName))) { \
196 X; \
197 } \
198 } while (false)
199#else
200#define ISEL_DUMP(X) do { } while (false)
201#endif
202
203//===---------------------------------------------------------------------===//
204///
205/// RegisterScheduler class - Track the registration of instruction schedulers.
206///
207//===---------------------------------------------------------------------===//
210
211//===---------------------------------------------------------------------===//
212///
213/// ISHeuristic command line option for instruction schedulers.
214///
215//===---------------------------------------------------------------------===//
218ISHeuristic("pre-RA-sched",
220 cl::desc("Instruction schedulers available (before register"
221 " allocation):"));
222
224defaultListDAGScheduler("default", "Best scheduler for the target",
226
227static bool dontUseFastISelFor(const Function &Fn) {
228 // Don't enable FastISel for functions with swiftasync Arguments.
229 // Debug info on those is reliant on good Argument lowering, and FastISel is
230 // not capable of lowering the entire function. Mixing the two selectors tend
231 // to result in poor lowering of Arguments.
232 return any_of(Fn.args(), [](const Argument &Arg) {
233 return Arg.hasAttribute(Attribute::AttrKind::SwiftAsync);
234 });
235}
236
237static bool maintainPGOProfile(const TargetMachine &TM,
238 CodeGenOptLevel OptLevel) {
239 if (OptLevel != CodeGenOptLevel::None)
240 return true;
241 if (TM.getPGOOption()) {
242 const PGOOptions &Options = *TM.getPGOOption();
243 return Options.Action == PGOOptions::PGOAction::IRUse ||
246 }
247 return false;
248}
249
250namespace llvm {
251
252 //===--------------------------------------------------------------------===//
253 /// This class is used by SelectionDAGISel to temporarily override
254 /// the optimization level on a per-function basis.
257 CodeGenOptLevel SavedOptLevel;
258 bool SavedFastISel;
259
260 public:
262 : IS(ISel) {
263 SavedOptLevel = IS.OptLevel;
264 SavedFastISel = IS.TM.Options.EnableFastISel;
265 if (NewOptLevel != SavedOptLevel) {
266 IS.OptLevel = NewOptLevel;
267 IS.TM.setOptLevel(NewOptLevel);
268 LLVM_DEBUG(dbgs() << "\nChanging optimization level for Function "
269 << IS.MF->getFunction().getName() << "\n");
270 LLVM_DEBUG(dbgs() << "\tBefore: -O" << static_cast<int>(SavedOptLevel)
271 << " ; After: -O" << static_cast<int>(NewOptLevel)
272 << "\n");
273 if (NewOptLevel == CodeGenOptLevel::None)
274 IS.TM.setFastISel(IS.TM.getO0WantsFastISel());
275 }
276 if (dontUseFastISelFor(IS.MF->getFunction()))
277 IS.TM.setFastISel(false);
279 dbgs() << "\tFastISel is "
280 << (IS.TM.Options.EnableFastISel ? "enabled" : "disabled")
281 << "\n");
282 }
283
285 if (IS.OptLevel == SavedOptLevel)
286 return;
287 LLVM_DEBUG(dbgs() << "\nRestoring optimization level for Function "
288 << IS.MF->getFunction().getName() << "\n");
289 LLVM_DEBUG(dbgs() << "\tBefore: -O" << static_cast<int>(IS.OptLevel)
290 << " ; After: -O" << static_cast<int>(SavedOptLevel) << "\n");
291 IS.OptLevel = SavedOptLevel;
292 IS.TM.setOptLevel(SavedOptLevel);
293 IS.TM.setFastISel(SavedFastISel);
294 }
295 };
296
297 //===--------------------------------------------------------------------===//
298 /// createDefaultScheduler - This creates an instruction scheduler appropriate
299 /// for the target.
301 CodeGenOptLevel OptLevel) {
302 const TargetLowering *TLI = IS->TLI;
303 const TargetSubtargetInfo &ST = IS->MF->getSubtarget();
304
305 // Try first to see if the Target has its own way of selecting a scheduler
306 if (auto *SchedulerCtor = ST.getDAGScheduler(OptLevel)) {
307 return SchedulerCtor(IS, OptLevel);
308 }
309
310 if (OptLevel == CodeGenOptLevel::None ||
311 (ST.enableMachineScheduler() && ST.enableMachineSchedDefaultSched()) ||
313 return createSourceListDAGScheduler(IS, OptLevel);
315 return createBURRListDAGScheduler(IS, OptLevel);
317 return createHybridListDAGScheduler(IS, OptLevel);
319 return createVLIWDAGScheduler(IS, OptLevel);
321 return createFastDAGScheduler(IS, OptLevel);
323 return createDAGLinearizer(IS, OptLevel);
325 "Unknown sched type!");
326 return createILPListDAGScheduler(IS, OptLevel);
327 }
328
329} // end namespace llvm
330
333 MachineBasicBlock *MBB) const {
334 switch (MI.getOpcode()) {
335 case TargetOpcode::STATEPOINT:
336 // As an implementation detail, STATEPOINT shares the STACKMAP format at
337 // this point in the process. We diverge later.
338 case TargetOpcode::STACKMAP:
339 case TargetOpcode::PATCHPOINT:
340 return emitPatchPoint(MI, MBB);
341 default:
342 break;
343 }
344
345#ifndef NDEBUG
346 dbgs() << "If a target marks an instruction with "
347 "'usesCustomInserter', it must implement "
348 "TargetLowering::EmitInstrWithCustomInserter!\n";
349#endif
350 llvm_unreachable(nullptr);
351}
352
354 SDNode *Node) const {
355 assert(!MI.hasPostISelHook() &&
356 "If a target marks an instruction with 'hasPostISelHook', "
357 "it must implement TargetLowering::AdjustInstrPostInstrSelection!");
358}
359
360//===----------------------------------------------------------------------===//
361// SelectionDAGISel code
362//===----------------------------------------------------------------------===//
363
373
375 // If we already selected that function, we do not need to run SDISel.
376 if (MF.getProperties().hasSelected())
377 return false;
378
379 // Do some sanity-checking on the command-line options.
380 if (EnableFastISelAbort && !Selector->TM.Options.EnableFastISel)
381 reportFatalUsageError("-fast-isel-abort > 0 requires -fast-isel");
382
383 // Decide what flavour of variable location debug-info will be used, before
384 // we change the optimisation level.
386
387 // Reset the target options before resetting the optimization
388 // level below.
389 // FIXME: This is a horrible hack and should be processed via
390 // codegen looking at the optimization level explicitly when
391 // it wants to look at it.
392 Selector->TM.resetTargetOptions(MF.getFunction());
393 // Reset OptLevel to None for optnone functions.
394 CodeGenOptLevel NewOptLevel = skipFunction(MF.getFunction())
396 : Selector->OptLevel;
397
398 Selector->MF = &MF;
399 OptLevelChanger OLC(*Selector, NewOptLevel);
400 Selector->initializeAnalysisResults(*this);
401 return Selector->runOnMachineFunction(MF);
402}
403
417
419
421 CodeGenOptLevel OptLevel = Selector->OptLevel;
422 bool RegisterPGOPasses = maintainPGOProfile(Selector->TM, Selector->OptLevel);
423 if (OptLevel != CodeGenOptLevel::None)
431 if (UseMBPI && RegisterPGOPasses)
434 // AssignmentTrackingAnalysis only runs if assignment tracking is enabled for
435 // the module.
438 if (RegisterPGOPasses)
440
442
444}
445
449 // If we already selected that function, we do not need to run SDISel.
450 if (MF.getProperties().hasSelected())
451 return PreservedAnalyses::all();
452
453 // Do some sanity-checking on the command-line options.
454 if (EnableFastISelAbort && !Selector->TM.Options.EnableFastISel)
455 reportFatalUsageError("-fast-isel-abort > 0 requires -fast-isel");
456
457 // Decide what flavour of variable location debug-info will be used, before
458 // we change the optimisation level.
460
461 // Reset the target options before resetting the optimization
462 // level below.
463 // FIXME: This is a horrible hack and should be processed via
464 // codegen looking at the optimization level explicitly when
465 // it wants to look at it.
466 Selector->TM.resetTargetOptions(MF.getFunction());
467 // Reset OptLevel to None for optnone functions.
468 // TODO: Add a function analysis to handle this.
469 Selector->MF = &MF;
470 // Reset OptLevel to None for optnone functions.
471 CodeGenOptLevel NewOptLevel = MF.getFunction().hasOptNone()
473 : Selector->OptLevel;
474
475 OptLevelChanger OLC(*Selector, NewOptLevel);
476 Selector->initializeAnalysisResults(MFAM);
477 Selector->runOnMachineFunction(MF);
478
480}
481
485 .getManager();
487 Function &Fn = MF->getFunction();
488#ifndef NDEBUG
489 FuncName = Fn.getName();
491#else
493#endif
494
495 const TargetSubtargetInfo &Subtarget = MF->getSubtarget();
496 bool RegisterPGOPasses = maintainPGOProfile(TM, OptLevel);
497 TII = Subtarget.getInstrInfo();
498 TLI = Subtarget.getTargetLowering();
499 RegInfo = &MF->getRegInfo();
500 LibInfo = &FAM.getResult<TargetLibraryAnalysis>(Fn);
501
502 GFI = Fn.hasGC() ? &FAM.getResult<GCFunctionAnalysis>(Fn) : nullptr;
503 ORE = std::make_unique<OptimizationRemarkEmitter>(&Fn);
504 AC = &FAM.getResult<AssumptionAnalysis>(Fn);
505 auto *PSI = MAMP.getCachedResult<ProfileSummaryAnalysis>(*Fn.getParent());
506 BlockFrequencyInfo *BFI = nullptr;
507 FAM.getResult<BlockFrequencyAnalysis>(Fn);
508 if (PSI && PSI->hasProfileSummary() && RegisterPGOPasses)
509 BFI = &FAM.getResult<BlockFrequencyAnalysis>(Fn);
510
511 FunctionVarLocs const *FnVarLocs = nullptr;
513 FnVarLocs = &FAM.getResult<DebugAssignmentTrackingAnalysis>(Fn);
514
515 auto *UA = FAM.getCachedResult<UniformityInfoAnalysis>(Fn);
517 MAMP.getCachedResult<MachineModuleAnalysis>(*Fn.getParent())->getMMI();
518
519 const LibcallLoweringModuleAnalysisResult *LibcallResult =
520 MAMP.getCachedResult<LibcallLoweringModuleAnalysis>(*Fn.getParent());
521 if (!LibcallResult) {
523 "' analysis required");
524 }
525
526 LibcallLowering = &LibcallResult->getLibcallLowering(Subtarget);
527 CurDAG->init(*MF, *ORE, MFAM, LibInfo, LibcallLowering, UA, PSI, BFI, MMI,
528 FnVarLocs);
529
530 // Now get the optional analyzes if we want to.
531 // This is based on the possibly changed OptLevel (after optnone is taken
532 // into account). That's unfortunate but OK because it just means we won't
533 // ask for passes that have been required anyway.
534
535 if (UseMBPI && RegisterPGOPasses)
536 FuncInfo->BPI = &FAM.getResult<BranchProbabilityAnalysis>(Fn);
537 else
538 FuncInfo->BPI = nullptr;
539
541 BatchAA.emplace(FAM.getResult<AAManager>(Fn));
542 else
543 BatchAA = std::nullopt;
544
545 SP = &FAM.getResult<SSPLayoutAnalysis>(Fn);
546
547 TTI = &FAM.getResult<TargetIRAnalysis>(Fn);
548
549 HwMode = Subtarget.getHwMode();
550}
551
553 Function &Fn = MF->getFunction();
554#ifndef NDEBUG
555 FuncName = Fn.getName();
557#else
559#endif
560
561 const TargetSubtargetInfo &Subtarget = MF->getSubtarget();
562
563 bool RegisterPGOPasses = maintainPGOProfile(TM, OptLevel);
564 TII = Subtarget.getInstrInfo();
565 TLI = Subtarget.getTargetLowering();
566 RegInfo = &MF->getRegInfo();
568
569 GFI = Fn.hasGC() ? &MFP.getAnalysis<GCModuleInfo>().getFunctionInfo(Fn)
570 : nullptr;
571 ORE = std::make_unique<OptimizationRemarkEmitter>(&Fn);
572 AC = &MFP.getAnalysis<AssumptionCacheTracker>().getAssumptionCache(Fn);
573 auto *PSI = &MFP.getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
574 BlockFrequencyInfo *BFI = nullptr;
575 if (PSI && PSI->hasProfileSummary() && RegisterPGOPasses)
576 BFI = &MFP.getAnalysis<LazyBlockFrequencyInfoPass>().getBFI();
577
578 FunctionVarLocs const *FnVarLocs = nullptr;
580 FnVarLocs = MFP.getAnalysis<AssignmentTrackingAnalysis>().getResults();
581
582 UniformityInfo *UA = nullptr;
583 if (auto *UAPass = MFP.getAnalysisIfAvailable<UniformityInfoWrapperPass>())
584 UA = &UAPass->getUniformityInfo();
585
588
590 &MFP.getAnalysis<LibcallLoweringInfoWrapper>().getLibcallLowering(
591 *Fn.getParent(), Subtarget);
592
593 CurDAG->init(*MF, *ORE, &MFP, LibInfo, LibcallLowering, UA, PSI, BFI, MMI,
594 FnVarLocs);
595
596 // Now get the optional analyzes if we want to.
597 // This is based on the possibly changed OptLevel (after optnone is taken
598 // into account). That's unfortunate but OK because it just means we won't
599 // ask for passes that have been required anyway.
600
601 if (UseMBPI && RegisterPGOPasses)
602 FuncInfo->BPI =
604 else
605 FuncInfo->BPI = nullptr;
606
609 else
610 BatchAA = std::nullopt;
611
612 SP = &MFP.getAnalysis<StackProtector>().getLayoutInfo();
613
615
616 HwMode = Subtarget.getHwMode();
617}
618
620 SwiftError->setFunction(mf);
621 const Function &Fn = mf.getFunction();
622
623 bool InstrRef = mf.useDebugInstrRef();
624
625 FuncInfo->set(MF->getFunction(), *MF, CurDAG);
626
627 ISEL_DUMP(dbgs() << "\n\n\n=== " << FuncName << '\n');
628
629 SDB->init(GFI, getBatchAA(), AC, LibInfo, *TTI);
630
631 MF->setHasInlineAsm(false);
632
633 FuncInfo->SplitCSR = false;
634
635 // We split CSR if the target supports it for the given function
636 // and the function has only return exits.
637 if (OptLevel != CodeGenOptLevel::None && TLI->supportSplitCSR(MF)) {
638 FuncInfo->SplitCSR = true;
639
640 // Collect all the return blocks.
641 for (const BasicBlock &BB : Fn) {
642 if (!succ_empty(&BB))
643 continue;
644
645 const Instruction *Term = BB.getTerminator();
646 if (isa<UnreachableInst>(Term) || isa<ReturnInst>(Term))
647 continue;
648
649 // Bail out if the exit block is not Return nor Unreachable.
650 FuncInfo->SplitCSR = false;
651 break;
652 }
653 }
654
655 MachineBasicBlock *EntryMBB = &MF->front();
656 if (FuncInfo->SplitCSR)
657 // This performs initialization so lowering for SplitCSR will be correct.
658 TLI->initializeSplitCSR(EntryMBB);
659
660 SelectAllBasicBlocks(Fn);
662 DiagnosticInfoISelFallback DiagFallback(Fn);
663 Fn.getContext().diagnose(DiagFallback);
664 }
665
666 // Replace forward-declared registers with the registers containing
667 // the desired value.
668 // Note: it is important that this happens **before** the call to
669 // EmitLiveInCopies, since implementations can skip copies of unused
670 // registers. If we don't apply the reg fixups before, some registers may
671 // appear as unused and will be skipped, resulting in bad MI.
672 MachineRegisterInfo &MRI = MF->getRegInfo();
673 for (DenseMap<Register, Register>::iterator I = FuncInfo->RegFixups.begin(),
674 E = FuncInfo->RegFixups.end();
675 I != E; ++I) {
676 Register From = I->first;
677 Register To = I->second;
678 // If To is also scheduled to be replaced, find what its ultimate
679 // replacement is.
680 while (true) {
681 DenseMap<Register, Register>::iterator J = FuncInfo->RegFixups.find(To);
682 if (J == E)
683 break;
684 To = J->second;
685 }
686 // Make sure the new register has a sufficiently constrained register class.
687 if (From.isVirtual() && To.isVirtual())
688 MRI.constrainRegClass(To, MRI.getRegClass(From));
689 // Replace it.
690
691 // Replacing one register with another won't touch the kill flags.
692 // We need to conservatively clear the kill flags as a kill on the old
693 // register might dominate existing uses of the new register.
694 if (!MRI.use_empty(To))
695 MRI.clearKillFlags(From);
696 MRI.replaceRegWith(From, To);
697 }
698
699 // If the first basic block in the function has live ins that need to be
700 // copied into vregs, emit the copies into the top of the block before
701 // emitting the code for the block.
702 const TargetRegisterInfo &TRI = *MF->getSubtarget().getRegisterInfo();
703 RegInfo->EmitLiveInCopies(EntryMBB, TRI, *TII);
704
705 // Insert copies in the entry block and the return blocks.
706 if (FuncInfo->SplitCSR) {
708 // Collect all the return blocks.
709 for (MachineBasicBlock &MBB : mf) {
710 if (!MBB.succ_empty())
711 continue;
712
713 MachineBasicBlock::iterator Term = MBB.getFirstTerminator();
714 if (Term != MBB.end() && Term->isReturn()) {
715 Returns.push_back(&MBB);
716 continue;
717 }
718 }
719 TLI->insertCopiesSplitCSR(EntryMBB, Returns);
720 }
721
723 if (!FuncInfo->ArgDbgValues.empty())
724 for (std::pair<MCRegister, Register> LI : RegInfo->liveins())
725 if (LI.second)
726 LiveInMap.insert(LI);
727
728 // Insert DBG_VALUE instructions for function arguments to the entry block.
729 for (unsigned i = 0, e = FuncInfo->ArgDbgValues.size(); i != e; ++i) {
730 MachineInstr *MI = FuncInfo->ArgDbgValues[e - i - 1];
731 assert(MI->getOpcode() != TargetOpcode::DBG_VALUE_LIST &&
732 "Function parameters should not be described by DBG_VALUE_LIST.");
733 bool hasFI = MI->getDebugOperand(0).isFI();
734 Register Reg =
735 hasFI ? TRI.getFrameRegister(*MF) : MI->getDebugOperand(0).getReg();
736 if (Reg.isPhysical())
737 EntryMBB->insert(EntryMBB->begin(), MI);
738 else {
739 MachineInstr *Def = RegInfo->getVRegDef(Reg);
740 if (Def) {
741 MachineBasicBlock::iterator InsertPos = Def;
742 // FIXME: VR def may not be in entry block.
743 Def->getParent()->insert(std::next(InsertPos), MI);
744 } else
745 LLVM_DEBUG(dbgs() << "Dropping debug info for dead vreg"
746 << printReg(Reg) << '\n');
747 }
748
749 // Don't try and extend through copies in instruction referencing mode.
750 if (InstrRef)
751 continue;
752
753 // If Reg is live-in then update debug info to track its copy in a vreg.
754 if (!Reg.isPhysical())
755 continue;
757 if (LDI != LiveInMap.end()) {
758 assert(!hasFI && "There's no handling of frame pointer updating here yet "
759 "- add if needed");
760 MachineInstr *Def = RegInfo->getVRegDef(LDI->second);
761 MachineBasicBlock::iterator InsertPos = Def;
762 const MDNode *Variable = MI->getDebugVariable();
763 const MDNode *Expr = MI->getDebugExpression();
764 DebugLoc DL = MI->getDebugLoc();
765 bool IsIndirect = MI->isIndirectDebugValue();
766 if (IsIndirect)
767 assert(MI->getDebugOffset().getImm() == 0 &&
768 "DBG_VALUE with nonzero offset");
769 assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
770 "Expected inlined-at fields to agree");
771 assert(MI->getOpcode() != TargetOpcode::DBG_VALUE_LIST &&
772 "Didn't expect to see a DBG_VALUE_LIST here");
773 // Def is never a terminator here, so it is ok to increment InsertPos.
774 BuildMI(*EntryMBB, ++InsertPos, DL, TII->get(TargetOpcode::DBG_VALUE),
775 IsIndirect, LDI->second, Variable, Expr);
776
777 // If this vreg is directly copied into an exported register then
778 // that COPY instructions also need DBG_VALUE, if it is the only
779 // user of LDI->second.
780 MachineInstr *CopyUseMI = nullptr;
781 for (MachineInstr &UseMI : RegInfo->use_instructions(LDI->second)) {
782 if (UseMI.isDebugValue())
783 continue;
784 if (UseMI.isCopy() && !CopyUseMI && UseMI.getParent() == EntryMBB) {
785 CopyUseMI = &UseMI;
786 continue;
787 }
788 // Otherwise this is another use or second copy use.
789 CopyUseMI = nullptr;
790 break;
791 }
792 if (CopyUseMI &&
793 TRI.getRegSizeInBits(LDI->second, MRI) ==
794 TRI.getRegSizeInBits(CopyUseMI->getOperand(0).getReg(), MRI)) {
795 // Use MI's debug location, which describes where Variable was
796 // declared, rather than whatever is attached to CopyUseMI.
797 MachineInstr *NewMI =
798 BuildMI(*MF, DL, TII->get(TargetOpcode::DBG_VALUE), IsIndirect,
799 CopyUseMI->getOperand(0).getReg(), Variable, Expr);
800 MachineBasicBlock::iterator Pos = CopyUseMI;
801 EntryMBB->insertAfter(Pos, NewMI);
802 }
803 }
804 }
805
806 // For debug-info, in instruction referencing mode, we need to perform some
807 // post-isel maintenence.
808 if (MF->useDebugInstrRef())
809 MF->finalizeDebugInstrRefs();
810
811 // Determine if there are any calls in this machine function.
812 MachineFrameInfo &MFI = MF->getFrameInfo();
813 for (const auto &MBB : *MF) {
814 if (MFI.hasCalls() && MF->hasInlineAsm())
815 break;
816
817 for (const auto &MI : MBB) {
818 const MCInstrDesc &MCID = TII->get(MI.getOpcode());
819 if ((MCID.isCall() && !MCID.isReturn()) ||
820 MI.isStackAligningInlineAsm()) {
821 MFI.setHasCalls(true);
822 }
823 if (MI.isInlineAsm()) {
824 MF->setHasInlineAsm(true);
825 }
826 }
827 }
828
829 // Release function-specific state. SDB and CurDAG are already cleared
830 // at this point.
831 FuncInfo->clear();
832
833 ISEL_DUMP(dbgs() << "*** MachineFunction at end of ISel ***\n");
834 ISEL_DUMP(MF->print(dbgs()));
835
836 return true;
837}
838
842 bool ShouldAbort) {
843 // Print the function name explicitly if we don't have a debug location (which
844 // makes the diagnostic less useful) or if we're going to emit a raw error.
845 if (!R.getLocation().isValid() || ShouldAbort)
846 R << (" (in function: " + MF.getName() + ")").str();
847
848 if (ShouldAbort)
849 reportFatalUsageError(Twine(R.getMsg()));
850
851 ORE.emit(R);
852 LLVM_DEBUG(dbgs() << R.getMsg() << "\n");
853}
854
855// Detect any fake uses that follow a tail call and move them before the tail
856// call. Ignore fake uses that use values that are def'd by or after the tail
857// call.
861 if (--I == Begin || !isa<ReturnInst>(*I))
862 return;
863 // Detect whether there are any fake uses trailing a (potential) tail call.
864 bool HaveFakeUse = false;
865 bool HaveTailCall = false;
866 do {
867 if (const CallInst *CI = dyn_cast<CallInst>(--I))
868 if (CI->isTailCall()) {
869 HaveTailCall = true;
870 break;
871 }
873 if (II->getIntrinsicID() == Intrinsic::fake_use)
874 HaveFakeUse = true;
875 } while (I != Begin);
876
877 // If we didn't find any tail calls followed by fake uses, we are done.
878 if (!HaveTailCall || !HaveFakeUse)
879 return;
880
882 // Record the fake uses we found so we can move them to the front of the
883 // tail call. Ignore them if they use a value that is def'd by or after
884 // the tail call.
885 for (BasicBlock::iterator Inst = I; Inst != End; Inst++) {
886 if (IntrinsicInst *FakeUse = dyn_cast<IntrinsicInst>(Inst);
887 FakeUse && FakeUse->getIntrinsicID() == Intrinsic::fake_use) {
888 if (auto UsedDef = dyn_cast<Instruction>(FakeUse->getOperand(0));
889 !UsedDef || UsedDef->getParent() != I->getParent() ||
890 UsedDef->comesBefore(&*I))
891 FakeUses.push_back(FakeUse);
892 }
893 }
894
895 for (auto *Inst : FakeUses)
896 Inst->moveBefore(*Inst->getParent(), I);
897}
898
899void SelectionDAGISel::SelectBasicBlock(BasicBlock::const_iterator Begin,
901 bool &HadTailCall) {
902 // Allow creating illegal types during DAG building for the basic block.
903 CurDAG->NewNodesMustHaveLegalTypes = false;
904
905 // Lower the instructions. If a call is emitted as a tail call, cease emitting
906 // nodes for this block. If an instruction is elided, don't emit it, but do
907 // handle any debug-info attached to it.
908 for (BasicBlock::const_iterator I = Begin; I != End && !SDB->HasTailCall; ++I) {
909 if (!ElidedArgCopyInstrs.count(&*I))
910 SDB->visit(*I);
911 else
912 SDB->visitDbgInfo(*I);
913 }
914
915 // Make sure the root of the DAG is up-to-date.
916 CurDAG->setRoot(SDB->getControlRoot());
917 HadTailCall = SDB->HasTailCall;
918 SDB->resolveOrClearDbgInfo();
919 SDB->clear();
920
921 // Final step, emit the lowered DAG as machine code.
922 CodeGenAndEmitDAG();
923}
924
925void SelectionDAGISel::ComputeLiveOutVRegInfo() {
926 SmallPtrSet<SDNode *, 16> Added;
928
929 Worklist.push_back(CurDAG->getRoot().getNode());
930 Added.insert(CurDAG->getRoot().getNode());
931
932 KnownBits Known;
933
934 do {
935 SDNode *N = Worklist.pop_back_val();
936
937 // Otherwise, add all chain operands to the worklist.
938 for (const SDValue &Op : N->op_values())
939 if (Op.getValueType() == MVT::Other && Added.insert(Op.getNode()).second)
940 Worklist.push_back(Op.getNode());
941
942 // If this is a CopyToReg with a vreg dest, process it.
943 if (N->getOpcode() != ISD::CopyToReg)
944 continue;
945
946 Register DestReg = cast<RegisterSDNode>(N->getOperand(1))->getReg();
947 if (!DestReg.isVirtual())
948 continue;
949
950 // Ignore non-integer values.
951 SDValue Src = N->getOperand(2);
952 EVT SrcVT = Src.getValueType();
953 if (!SrcVT.isInteger())
954 continue;
955
956 unsigned NumSignBits = CurDAG->ComputeNumSignBits(Src);
957 Known = CurDAG->computeKnownBits(Src);
958 FuncInfo->AddLiveOutRegInfo(DestReg, NumSignBits, Known);
959 } while (!Worklist.empty());
960}
961
962void SelectionDAGISel::CodeGenAndEmitDAG() {
963 StringRef GroupName = "sdag";
964 StringRef GroupDescription = "Instruction Selection and Scheduling";
965 std::string BlockName;
966 bool MatchFilterBB = false;
967 (void)MatchFilterBB;
968
969 // Pre-type legalization allow creation of any node types.
970 CurDAG->NewNodesMustHaveLegalTypes = false;
971
972#ifndef NDEBUG
973 MatchFilterBB = (FilterDAGBasicBlockName.empty() ||
975 FuncInfo->MBB->getBasicBlock()->getName());
976#endif
977#ifdef NDEBUG
981#endif
982 {
983 BlockName =
984 (MF->getName() + ":" + FuncInfo->MBB->getBasicBlock()->getName()).str();
985 }
986 ISEL_DUMP(dbgs() << "\nInitial selection DAG: "
987 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
988 << "'\n";
989 CurDAG->dump(DumpSortedDAG));
990
991#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
992 if (TTI->hasBranchDivergence())
993 CurDAG->VerifyDAGDivergence();
994#endif
995
996 if (ViewDAGCombine1 && MatchFilterBB)
997 CurDAG->viewGraph("dag-combine1 input for " + BlockName);
998
999 // Run the DAG combiner in pre-legalize mode.
1000 {
1001 NamedRegionTimer T("combine1", "DAG Combining 1", GroupName,
1002 GroupDescription, TimePassesIsEnabled);
1004 }
1005
1006 ISEL_DUMP(dbgs() << "\nOptimized lowered selection DAG: "
1007 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
1008 << "'\n";
1009 CurDAG->dump(DumpSortedDAG));
1010
1011#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
1012 if (TTI->hasBranchDivergence())
1013 CurDAG->VerifyDAGDivergence();
1014#endif
1015
1016 // Second step, hack on the DAG until it only uses operations and types that
1017 // the target supports.
1018 if (ViewLegalizeTypesDAGs && MatchFilterBB)
1019 CurDAG->viewGraph("legalize-types input for " + BlockName);
1020
1021 bool Changed;
1022 {
1023 NamedRegionTimer T("legalize_types", "Type Legalization", GroupName,
1024 GroupDescription, TimePassesIsEnabled);
1025 Changed = CurDAG->LegalizeTypes();
1026 }
1027
1028 ISEL_DUMP(dbgs() << "\nType-legalized selection DAG: "
1029 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
1030 << "'\n";
1031 CurDAG->dump(DumpSortedDAG));
1032
1033#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
1034 if (TTI->hasBranchDivergence())
1035 CurDAG->VerifyDAGDivergence();
1036#endif
1037
1038 // Only allow creation of legal node types.
1039 CurDAG->NewNodesMustHaveLegalTypes = true;
1040
1041 if (Changed) {
1042 if (ViewDAGCombineLT && MatchFilterBB)
1043 CurDAG->viewGraph("dag-combine-lt input for " + BlockName);
1044
1045 // Run the DAG combiner in post-type-legalize mode.
1046 {
1047 NamedRegionTimer T("combine_lt", "DAG Combining after legalize types",
1048 GroupName, GroupDescription, TimePassesIsEnabled);
1050 }
1051
1052 ISEL_DUMP(dbgs() << "\nOptimized type-legalized selection DAG: "
1053 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
1054 << "'\n";
1055 CurDAG->dump(DumpSortedDAG));
1056
1057#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
1058 if (TTI->hasBranchDivergence())
1059 CurDAG->VerifyDAGDivergence();
1060#endif
1061 }
1062
1063 {
1064 NamedRegionTimer T("legalize_vec", "Vector Legalization", GroupName,
1065 GroupDescription, TimePassesIsEnabled);
1066 Changed = CurDAG->LegalizeVectors();
1067 }
1068
1069 if (Changed) {
1070 ISEL_DUMP(dbgs() << "\nVector-legalized selection DAG: "
1071 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
1072 << "'\n";
1073 CurDAG->dump(DumpSortedDAG));
1074
1075#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
1076 if (TTI->hasBranchDivergence())
1077 CurDAG->VerifyDAGDivergence();
1078#endif
1079
1080 {
1081 NamedRegionTimer T("legalize_types2", "Type Legalization 2", GroupName,
1082 GroupDescription, TimePassesIsEnabled);
1083 CurDAG->LegalizeTypes();
1084 }
1085
1086 ISEL_DUMP(dbgs() << "\nVector/type-legalized selection DAG: "
1087 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
1088 << "'\n";
1089 CurDAG->dump(DumpSortedDAG));
1090
1091#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
1092 if (TTI->hasBranchDivergence())
1093 CurDAG->VerifyDAGDivergence();
1094#endif
1095
1096 if (ViewDAGCombineLT && MatchFilterBB)
1097 CurDAG->viewGraph("dag-combine-lv input for " + BlockName);
1098
1099 // Run the DAG combiner in post-type-legalize mode.
1100 {
1101 NamedRegionTimer T("combine_lv", "DAG Combining after legalize vectors",
1102 GroupName, GroupDescription, TimePassesIsEnabled);
1104 }
1105
1106 ISEL_DUMP(dbgs() << "\nOptimized vector-legalized selection DAG: "
1107 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
1108 << "'\n";
1109 CurDAG->dump(DumpSortedDAG));
1110
1111#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
1112 if (TTI->hasBranchDivergence())
1113 CurDAG->VerifyDAGDivergence();
1114#endif
1115 }
1116
1117 if (ViewLegalizeDAGs && MatchFilterBB)
1118 CurDAG->viewGraph("legalize input for " + BlockName);
1119
1120 {
1121 NamedRegionTimer T("legalize", "DAG Legalization", GroupName,
1122 GroupDescription, TimePassesIsEnabled);
1123 CurDAG->Legalize();
1124 }
1125
1126 ISEL_DUMP(dbgs() << "\nLegalized selection DAG: "
1127 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
1128 << "'\n";
1129 CurDAG->dump(DumpSortedDAG));
1130
1131#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
1132 if (TTI->hasBranchDivergence())
1133 CurDAG->VerifyDAGDivergence();
1134#endif
1135
1136 if (ViewDAGCombine2 && MatchFilterBB)
1137 CurDAG->viewGraph("dag-combine2 input for " + BlockName);
1138
1139 // Run the DAG combiner in post-legalize mode.
1140 {
1141 NamedRegionTimer T("combine2", "DAG Combining 2", GroupName,
1142 GroupDescription, TimePassesIsEnabled);
1144 }
1145
1146 ISEL_DUMP(dbgs() << "\nOptimized legalized selection DAG: "
1147 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
1148 << "'\n";
1149 CurDAG->dump(DumpSortedDAG));
1150
1151#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
1152 if (TTI->hasBranchDivergence())
1153 CurDAG->VerifyDAGDivergence();
1154#endif
1155
1157 ComputeLiveOutVRegInfo();
1158
1159 if (ViewISelDAGs && MatchFilterBB)
1160 CurDAG->viewGraph("isel input for " + BlockName);
1161
1162 // Third, instruction select all of the operations to machine code, adding the
1163 // code to the MachineBasicBlock.
1164 {
1165 NamedRegionTimer T("isel", "Instruction Selection", GroupName,
1166 GroupDescription, TimePassesIsEnabled);
1167 DoInstructionSelection();
1168 }
1169
1170 ISEL_DUMP(dbgs() << "\nSelected selection DAG: "
1171 << printMBBReference(*FuncInfo->MBB) << " '" << BlockName
1172 << "'\n";
1173 CurDAG->dump(DumpSortedDAG));
1174
1175 if (ViewSchedDAGs && MatchFilterBB)
1176 CurDAG->viewGraph("scheduler input for " + BlockName);
1177
1178 // Schedule machine code.
1179 ScheduleDAGSDNodes *Scheduler = CreateScheduler();
1180 {
1181 NamedRegionTimer T("sched", "Instruction Scheduling", GroupName,
1182 GroupDescription, TimePassesIsEnabled);
1183 Scheduler->Run(CurDAG, FuncInfo->MBB);
1184 }
1185
1186 if (ViewSUnitDAGs && MatchFilterBB)
1187 Scheduler->viewGraph();
1188
1189 // Emit machine code to BB. This can change 'BB' to the last block being
1190 // inserted into.
1191 MachineBasicBlock *FirstMBB = FuncInfo->MBB, *LastMBB;
1192 {
1193 NamedRegionTimer T("emit", "Instruction Creation", GroupName,
1194 GroupDescription, TimePassesIsEnabled);
1195
1196 // FuncInfo->InsertPt is passed by reference and set to the end of the
1197 // scheduled instructions.
1198 LastMBB = FuncInfo->MBB = Scheduler->EmitSchedule(FuncInfo->InsertPt);
1199 }
1200
1201 // If the block was split, make sure we update any references that are used to
1202 // update PHI nodes later on.
1203 if (FirstMBB != LastMBB)
1204 SDB->UpdateSplitBlock(FirstMBB, LastMBB);
1205
1206 // Free the scheduler state.
1207 {
1208 NamedRegionTimer T("cleanup", "Instruction Scheduling Cleanup", GroupName,
1209 GroupDescription, TimePassesIsEnabled);
1210 delete Scheduler;
1211 }
1212
1213 // Free the SelectionDAG state, now that we're finished with it.
1214 CurDAG->clear();
1215}
1216
1217namespace {
1218
1219/// ISelUpdater - helper class to handle updates of the instruction selection
1220/// graph.
1221class ISelUpdater : public SelectionDAG::DAGUpdateListener {
1222 SelectionDAG::allnodes_iterator &ISelPosition;
1223
1224public:
1225 ISelUpdater(SelectionDAG &DAG, SelectionDAG::allnodes_iterator &isp)
1226 : SelectionDAG::DAGUpdateListener(DAG), ISelPosition(isp) {}
1227
1228 /// NodeDeleted - Handle nodes deleted from the graph. If the node being
1229 /// deleted is the current ISelPosition node, update ISelPosition.
1230 ///
1231 void NodeDeleted(SDNode *N, SDNode *E) override {
1232 if (ISelPosition == SelectionDAG::allnodes_iterator(N))
1233 ++ISelPosition;
1234 }
1235
1236 /// NodeInserted - Handle new nodes inserted into the graph: propagate
1237 /// metadata from root nodes that also applies to new nodes, in case the root
1238 /// is later deleted.
1239 void NodeInserted(SDNode *N) override {
1240 SDNode *CurNode = &*ISelPosition;
1241 if (MDNode *MD = DAG.getPCSections(CurNode))
1242 DAG.addPCSections(N, MD);
1243 if (MDNode *MMRA = DAG.getMMRAMetadata(CurNode))
1244 DAG.addMMRAMetadata(N, MMRA);
1245 }
1246};
1247
1248} // end anonymous namespace
1249
1250// This function is used to enforce the topological node id property
1251// leveraged during instruction selection. Before the selection process all
1252// nodes are given a non-negative id such that all nodes have a greater id than
1253// their operands. As this holds transitively we can prune checks that a node N
1254// is a predecessor of M another by not recursively checking through M's
1255// operands if N's ID is larger than M's ID. This significantly improves
1256// performance of various legality checks (e.g. IsLegalToFold / UpdateChains).
1257
1258// However, when we fuse multiple nodes into a single node during the
1259// selection we may induce a predecessor relationship between inputs and
1260// outputs of distinct nodes being merged, violating the topological property.
1261// Should a fused node have a successor which has yet to be selected,
1262// our legality checks would be incorrect. To avoid this we mark all unselected
1263// successor nodes, i.e. id != -1, as invalid for pruning by bit-negating (x =>
1264// (-(x+1))) the ids and modify our pruning check to ignore negative Ids of M.
1265// We use bit-negation to more clearly enforce that node id -1 can only be
1266// achieved by selected nodes. As the conversion is reversable to the original
1267// Id, topological pruning can still be leveraged when looking for unselected
1268// nodes. This method is called internally in all ISel replacement related
1269// functions.
1272 Nodes.push_back(Node);
1273
1274 while (!Nodes.empty()) {
1275 SDNode *N = Nodes.pop_back_val();
1276 for (auto *U : N->users()) {
1277 auto UId = U->getNodeId();
1278 if (UId > 0) {
1280 Nodes.push_back(U);
1281 }
1282 }
1283 }
1284}
1285
1286// InvalidateNodeId - As explained in EnforceNodeIdInvariant, mark a
1287// NodeId with the equivalent node id which is invalid for topological
1288// pruning.
1290 int InvalidId = -(N->getNodeId() + 1);
1291 N->setNodeId(InvalidId);
1292}
1293
1294// getUninvalidatedNodeId - get original uninvalidated node id.
1296 int Id = N->getNodeId();
1297 if (Id < -1)
1298 return -(Id + 1);
1299 return Id;
1300}
1301
1302void SelectionDAGISel::DoInstructionSelection() {
1303 LLVM_DEBUG(dbgs() << "===== Instruction selection begins: "
1304 << printMBBReference(*FuncInfo->MBB) << " '"
1305 << FuncInfo->MBB->getName() << "'\n");
1306
1308
1309 // Select target instructions for the DAG.
1310 {
1311 // Number all nodes with a topological order and set DAGSize.
1313
1314 // Create a dummy node (which is not added to allnodes), that adds
1315 // a reference to the root node, preventing it from being deleted,
1316 // and tracking any changes of the root.
1317 HandleSDNode Dummy(CurDAG->getRoot());
1319 ++ISelPosition;
1320
1321 // Make sure that ISelPosition gets properly updated when nodes are deleted
1322 // in calls made from this function. New nodes inherit relevant metadata.
1323 ISelUpdater ISU(*CurDAG, ISelPosition);
1324
1325 // The AllNodes list is now topological-sorted. Visit the
1326 // nodes by starting at the end of the list (the root of the
1327 // graph) and preceding back toward the beginning (the entry
1328 // node).
1329 while (ISelPosition != CurDAG->allnodes_begin()) {
1330 SDNode *Node = &*--ISelPosition;
1331 // Skip dead nodes. DAGCombiner is expected to eliminate all dead nodes,
1332 // but there are currently some corner cases that it misses. Also, this
1333 // makes it theoretically possible to disable the DAGCombiner.
1334 if (Node->use_empty())
1335 continue;
1336
1337#ifndef NDEBUG
1339 Nodes.push_back(Node);
1340
1341 while (!Nodes.empty()) {
1342 auto N = Nodes.pop_back_val();
1343 if (N->getOpcode() == ISD::TokenFactor || N->getNodeId() < 0)
1344 continue;
1345 for (const SDValue &Op : N->op_values()) {
1346 if (Op->getOpcode() == ISD::TokenFactor)
1347 Nodes.push_back(Op.getNode());
1348 else {
1349 // We rely on topological ordering of node ids for checking for
1350 // cycles when fusing nodes during selection. All unselected nodes
1351 // successors of an already selected node should have a negative id.
1352 // This assertion will catch such cases. If this assertion triggers
1353 // it is likely you using DAG-level Value/Node replacement functions
1354 // (versus equivalent ISEL replacement) in backend-specific
1355 // selections. See comment in EnforceNodeIdInvariant for more
1356 // details.
1357 assert(Op->getNodeId() != -1 &&
1358 "Node has already selected predecessor node");
1359 }
1360 }
1361 }
1362#endif
1363
1364 // When we are using non-default rounding modes or FP exception behavior
1365 // FP operations are represented by StrictFP pseudo-operations. For
1366 // targets that do not (yet) understand strict FP operations directly,
1367 // we convert them to normal FP opcodes instead at this point. This
1368 // will allow them to be handled by existing target-specific instruction
1369 // selectors.
1370 if (!TLI->isStrictFPEnabled() && Node->isStrictFPOpcode()) {
1371 // For some opcodes, we need to call TLI->getOperationAction using
1372 // the first operand type instead of the result type. Note that this
1373 // must match what SelectionDAGLegalize::LegalizeOp is doing.
1374 EVT ActionVT;
1375 switch (Node->getOpcode()) {
1378 case ISD::STRICT_LRINT:
1379 case ISD::STRICT_LLRINT:
1380 case ISD::STRICT_LROUND:
1382 case ISD::STRICT_FSETCC:
1384 ActionVT = Node->getOperand(1).getValueType();
1385 break;
1386 default:
1387 ActionVT = Node->getValueType(0);
1388 break;
1389 }
1390 if (TLI->getOperationAction(Node->getOpcode(), ActionVT)
1392 Node = CurDAG->mutateStrictFPToFP(Node);
1393 }
1394
1395 LLVM_DEBUG(dbgs() << "\nISEL: Starting selection on root node: ";
1396 Node->dump(CurDAG));
1397
1398 Select(Node);
1399 }
1400
1401 CurDAG->setRoot(Dummy.getValue());
1402 }
1403
1404 LLVM_DEBUG(dbgs() << "\n===== Instruction selection ends:\n");
1405
1407}
1408
1410 for (const User *U : CPI->users()) {
1411 if (const IntrinsicInst *EHPtrCall = dyn_cast<IntrinsicInst>(U)) {
1412 Intrinsic::ID IID = EHPtrCall->getIntrinsicID();
1413 if (IID == Intrinsic::eh_exceptionpointer ||
1414 IID == Intrinsic::eh_exceptioncode)
1415 return true;
1416 }
1417 }
1418 return false;
1419}
1420
1421// wasm.landingpad.index intrinsic is for associating a landing pad index number
1422// with a catchpad instruction. Retrieve the landing pad index in the intrinsic
1423// and store the mapping in the function.
1425 const CatchPadInst *CPI) {
1426 MachineFunction *MF = MBB->getParent();
1427 // In case of single catch (...), we don't emit LSDA, so we don't need
1428 // this information.
1429 bool IsSingleCatchAllClause =
1430 CPI->arg_size() == 1 &&
1431 cast<Constant>(CPI->getArgOperand(0))->isNullValue();
1432 // cathchpads for longjmp use an empty type list, e.g. catchpad within %0 []
1433 // and they don't need LSDA info
1434 bool IsCatchLongjmp = CPI->arg_size() == 0;
1435 if (!IsSingleCatchAllClause && !IsCatchLongjmp) {
1436 // Create a mapping from landing pad label to landing pad index.
1437 bool IntrFound = false;
1438 for (const User *U : CPI->users()) {
1439 if (const auto *Call = dyn_cast<IntrinsicInst>(U)) {
1440 Intrinsic::ID IID = Call->getIntrinsicID();
1441 if (IID == Intrinsic::wasm_landingpad_index) {
1442 Value *IndexArg = Call->getArgOperand(1);
1443 int Index = cast<ConstantInt>(IndexArg)->getZExtValue();
1444 MF->setWasmLandingPadIndex(MBB, Index);
1445 IntrFound = true;
1446 break;
1447 }
1448 }
1449 }
1450 assert(IntrFound && "wasm.landingpad.index intrinsic not found!");
1451 (void)IntrFound;
1452 }
1453}
1454
1455/// PrepareEHLandingPad - Emit an EH_LABEL, set up live-in registers, and
1456/// do other setup for EH landing-pad blocks.
1457bool SelectionDAGISel::PrepareEHLandingPad() {
1458 MachineBasicBlock *MBB = FuncInfo->MBB;
1459 const Constant *PersonalityFn = FuncInfo->Fn->getPersonalityFn();
1460 const BasicBlock *LLVMBB = MBB->getBasicBlock();
1461 const TargetRegisterClass *PtrRC =
1462 TLI->getRegClassFor(TLI->getPointerTy(CurDAG->getDataLayout()));
1463
1464 auto Pers = classifyEHPersonality(PersonalityFn);
1465
1466 // Catchpads have one live-in register, which typically holds the exception
1467 // pointer or code.
1468 if (isFuncletEHPersonality(Pers)) {
1469 if (const auto *CPI = dyn_cast<CatchPadInst>(LLVMBB->getFirstNonPHIIt())) {
1471 // Get or create the virtual register to hold the pointer or code. Mark
1472 // the live in physreg and copy into the vreg.
1473 MCRegister EHPhysReg = TLI->getExceptionPointerRegister(PersonalityFn);
1474 assert(EHPhysReg && "target lacks exception pointer register");
1475 MBB->addLiveIn(EHPhysReg);
1476 Register VReg = FuncInfo->getCatchPadExceptionPointerVReg(CPI, PtrRC);
1477 BuildMI(*MBB, FuncInfo->InsertPt, SDB->getCurDebugLoc(),
1478 TII->get(TargetOpcode::COPY), VReg)
1479 .addReg(EHPhysReg, RegState::Kill);
1480 }
1481 }
1482 return true;
1483 }
1484
1485 // Add a label to mark the beginning of the landing pad. Deletion of the
1486 // landing pad can thus be detected via the MachineModuleInfo.
1487 MCSymbol *Label = MF->addLandingPad(MBB);
1488
1489 const MCInstrDesc &II = TII->get(TargetOpcode::EH_LABEL);
1490 BuildMI(*MBB, FuncInfo->InsertPt, SDB->getCurDebugLoc(), II)
1491 .addSym(Label);
1492
1493 // If the unwinder does not preserve all registers, ensure that the
1494 // function marks the clobbered registers as used.
1495 const TargetRegisterInfo &TRI = *MF->getSubtarget().getRegisterInfo();
1496 if (auto *RegMask = TRI.getCustomEHPadPreservedMask(*MF))
1497 MF->getRegInfo().addPhysRegsUsedFromRegMask(RegMask);
1498
1499 if (Pers == EHPersonality::Wasm_CXX) {
1500 if (const auto *CPI = dyn_cast<CatchPadInst>(LLVMBB->getFirstNonPHIIt()))
1502 } else {
1503 // Assign the call site to the landing pad's begin label.
1504 MF->setCallSiteLandingPad(Label, SDB->LPadToCallSiteMap[MBB]);
1505 // Mark exception register as live in.
1506 if (MCRegister Reg = TLI->getExceptionPointerRegister(PersonalityFn))
1507 FuncInfo->ExceptionPointerVirtReg = MBB->addLiveIn(Reg, PtrRC);
1508 // Mark exception selector register as live in.
1509 if (MCRegister Reg = TLI->getExceptionSelectorRegister(PersonalityFn))
1510 FuncInfo->ExceptionSelectorVirtReg = MBB->addLiveIn(Reg, PtrRC);
1511 }
1512
1513 return true;
1514}
1515
1516// Mark and Report IPToState for each Block under IsEHa
1517void SelectionDAGISel::reportIPToStateForBlocks(MachineFunction *MF) {
1518 llvm::WinEHFuncInfo *EHInfo = MF->getWinEHFuncInfo();
1519 if (!EHInfo)
1520 return;
1521 for (MachineBasicBlock &MBB : *MF) {
1522 const BasicBlock *BB = MBB.getBasicBlock();
1523 int State = EHInfo->BlockToStateMap[BB];
1524 if (BB->getFirstMayFaultInst()) {
1525 // Report IP range only for blocks with Faulty inst
1526 auto MBBb = MBB.getFirstNonPHI();
1527
1528 if (MBBb == MBB.end())
1529 continue;
1530
1531 MachineInstr *MIb = &*MBBb;
1532 if (MIb->isTerminator())
1533 continue;
1534
1535 // Insert EH Labels
1536 MCSymbol *BeginLabel = MF->getContext().createTempSymbol();
1537 MCSymbol *EndLabel = MF->getContext().createTempSymbol();
1538 EHInfo->addIPToStateRange(State, BeginLabel, EndLabel);
1539 BuildMI(MBB, MBBb, SDB->getCurDebugLoc(),
1540 TII->get(TargetOpcode::EH_LABEL))
1541 .addSym(BeginLabel);
1542 auto MBBe = MBB.instr_end();
1543 MachineInstr *MIe = &*(--MBBe);
1544 // insert before (possible multiple) terminators
1545 while (MIe->isTerminator())
1546 MIe = &*(--MBBe);
1547 ++MBBe;
1548 BuildMI(MBB, MBBe, SDB->getCurDebugLoc(),
1549 TII->get(TargetOpcode::EH_LABEL))
1550 .addSym(EndLabel);
1551 }
1552 }
1553}
1554
1555/// isFoldedOrDeadInstruction - Return true if the specified instruction is
1556/// side-effect free and is either dead or folded into a generated instruction.
1557/// Return false if it needs to be emitted.
1559 const FunctionLoweringInfo &FuncInfo) {
1560 return !I->mayWriteToMemory() && // Side-effecting instructions aren't folded.
1561 !I->isTerminator() && // Terminators aren't folded.
1562 !I->isEHPad() && // EH pad instructions aren't folded.
1563 !FuncInfo.isExportedInst(I); // Exported instrs must be computed.
1564}
1565
1567 const Value *Arg, DIExpression *Expr,
1568 DILocalVariable *Var,
1569 DebugLoc DbgLoc) {
1570 if (!Expr->isEntryValue() || !isa<Argument>(Arg))
1571 return false;
1572
1573 auto ArgIt = FuncInfo.ValueMap.find(Arg);
1574 if (ArgIt == FuncInfo.ValueMap.end())
1575 return false;
1576 Register ArgVReg = ArgIt->getSecond();
1577
1578 // Find the corresponding livein physical register to this argument.
1579 for (auto [PhysReg, VirtReg] : FuncInfo.RegInfo->liveins())
1580 if (VirtReg == ArgVReg) {
1581 // Append an op deref to account for the fact that this is a dbg_declare.
1582 Expr = DIExpression::append(Expr, dwarf::DW_OP_deref);
1583 FuncInfo.MF->setVariableDbgInfo(Var, Expr, PhysReg, DbgLoc);
1584 LLVM_DEBUG(dbgs() << "processDbgDeclare: setVariableDbgInfo Var=" << *Var
1585 << ", Expr=" << *Expr << ", MCRegister=" << PhysReg
1586 << ", DbgLoc=" << DbgLoc << "\n");
1587 return true;
1588 }
1589 return false;
1590}
1591
1593 const Value *Address, DIExpression *Expr,
1594 DILocalVariable *Var, DebugLoc DbgLoc) {
1595 if (!Address) {
1596 LLVM_DEBUG(dbgs() << "processDbgDeclares skipping " << *Var
1597 << " (bad address)\n");
1598 return false;
1599 }
1600
1601 if (processIfEntryValueDbgDeclare(FuncInfo, Address, Expr, Var, DbgLoc))
1602 return true;
1603
1604 if (!Address->getType()->isPointerTy())
1605 return false;
1606
1607 MachineFunction *MF = FuncInfo.MF;
1608 const DataLayout &DL = MF->getDataLayout();
1609
1610 assert(Var && "Missing variable");
1611 assert(DbgLoc && "Missing location");
1612
1613 // Look through casts and constant offset GEPs. These mostly come from
1614 // inalloca.
1615 APInt Offset(DL.getIndexTypeSizeInBits(Address->getType()), 0);
1616 Address = Address->stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
1617
1618 // Check if the variable is a static alloca or a byval or inalloca
1619 // argument passed in memory. If it is not, then we will ignore this
1620 // intrinsic and handle this during isel like dbg.value.
1621 int FI = std::numeric_limits<int>::max();
1622 if (const auto *AI = dyn_cast<AllocaInst>(Address)) {
1623 auto SI = FuncInfo.StaticAllocaMap.find(AI);
1624 if (SI != FuncInfo.StaticAllocaMap.end())
1625 FI = SI->second;
1626 } else if (const auto *Arg = dyn_cast<Argument>(Address))
1627 FI = FuncInfo.getArgumentFrameIndex(Arg);
1628
1629 if (FI == std::numeric_limits<int>::max())
1630 return false;
1631
1632 if (Offset.getBoolValue())
1634 Offset.getZExtValue());
1635
1636 LLVM_DEBUG(dbgs() << "processDbgDeclare: setVariableDbgInfo Var=" << *Var
1637 << ", Expr=" << *Expr << ", FI=" << FI
1638 << ", DbgLoc=" << DbgLoc << "\n");
1639 MF->setVariableDbgInfo(Var, Expr, FI, DbgLoc);
1640 return true;
1641}
1642
1643/// Collect llvm.dbg.declare information. This is done after argument lowering
1644/// in case the declarations refer to arguments.
1646 for (const auto &I : instructions(*FuncInfo.Fn)) {
1647 for (const DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {
1649 processDbgDeclare(FuncInfo, DVR.getVariableLocationOp(0),
1650 DVR.getExpression(), DVR.getVariable(),
1651 DVR.getDebugLoc()))
1652 FuncInfo.PreprocessedDVRDeclares.insert(&DVR);
1653 }
1654 }
1655}
1656
1657/// Collect single location variable information generated with assignment
1658/// tracking. This is done after argument lowering in case the declarations
1659/// refer to arguments.
1661 FunctionVarLocs const *FnVarLocs) {
1662 for (auto It = FnVarLocs->single_locs_begin(),
1663 End = FnVarLocs->single_locs_end();
1664 It != End; ++It) {
1665 assert(!It->Values.hasArgList() && "Single loc variadic ops not supported");
1666 processDbgDeclare(FuncInfo, It->Values.getVariableLocationOp(0), It->Expr,
1667 FnVarLocs->getDILocalVariable(It->VariableID), It->DL);
1668 }
1669}
1670
1671void SelectionDAGISel::SelectAllBasicBlocks(const Function &Fn) {
1672 FastISelFailed = false;
1673 // Initialize the Fast-ISel state, if needed.
1674 FastISel *FastIS = nullptr;
1675 if (TM.Options.EnableFastISel) {
1676 LLVM_DEBUG(dbgs() << "Enabling fast-isel\n");
1677 FastIS = TLI->createFastISel(*FuncInfo, LibInfo, LibcallLowering);
1678 }
1679
1680 ReversePostOrderTraversal<const Function*> RPOT(&Fn);
1681
1682 // Lower arguments up front. An RPO iteration always visits the entry block
1683 // first.
1684 assert(*RPOT.begin() == &Fn.getEntryBlock());
1685 ++NumEntryBlocks;
1686
1687 // Set up FuncInfo for ISel. Entry blocks never have PHIs.
1688 FuncInfo->MBB = FuncInfo->getMBB(&Fn.getEntryBlock());
1689 FuncInfo->InsertPt = FuncInfo->MBB->begin();
1690
1691 CurDAG->setFunctionLoweringInfo(FuncInfo.get());
1692
1693 if (!FastIS) {
1694 LowerArguments(Fn);
1695 } else {
1696 // See if fast isel can lower the arguments.
1697 FastIS->startNewBlock();
1698 if (!FastIS->lowerArguments()) {
1699 FastISelFailed = true;
1700 // Fast isel failed to lower these arguments
1701 ++NumFastIselFailLowerArguments;
1702
1703 OptimizationRemarkMissed R("sdagisel", "FastISelFailure",
1704 Fn.getSubprogram(),
1705 &Fn.getEntryBlock());
1706 R << "FastISel didn't lower all arguments: "
1707 << ore::NV("Prototype", Fn.getFunctionType());
1709
1710 // Use SelectionDAG argument lowering
1711 LowerArguments(Fn);
1712 CurDAG->setRoot(SDB->getControlRoot());
1713 SDB->clear();
1714 CodeGenAndEmitDAG();
1715 }
1716
1717 // If we inserted any instructions at the beginning, make a note of
1718 // where they are, so we can be sure to emit subsequent instructions
1719 // after them.
1720 if (FuncInfo->InsertPt != FuncInfo->MBB->begin())
1721 FastIS->setLastLocalValue(&*std::prev(FuncInfo->InsertPt));
1722 else
1723 FastIS->setLastLocalValue(nullptr);
1724 }
1725
1726 bool Inserted = SwiftError->createEntriesInEntryBlock(SDB->getCurDebugLoc());
1727
1728 if (FastIS && Inserted)
1729 FastIS->setLastLocalValue(&*std::prev(FuncInfo->InsertPt));
1730
1732 assert(CurDAG->getFunctionVarLocs() &&
1733 "expected AssignmentTrackingAnalysis pass results");
1734 processSingleLocVars(*FuncInfo, CurDAG->getFunctionVarLocs());
1735 } else {
1737 }
1738
1739 // Iterate over all basic blocks in the function.
1740 FuncInfo->VisitedBBs.assign(Fn.getMaxBlockNumber(), false);
1741 for (const BasicBlock *LLVMBB : RPOT) {
1743 bool AllPredsVisited = true;
1744 for (const BasicBlock *Pred : predecessors(LLVMBB)) {
1745 if (!FuncInfo->VisitedBBs[Pred->getNumber()]) {
1746 AllPredsVisited = false;
1747 break;
1748 }
1749 }
1750
1751 if (AllPredsVisited) {
1752 for (const PHINode &PN : LLVMBB->phis())
1753 FuncInfo->ComputePHILiveOutRegInfo(&PN);
1754 } else {
1755 for (const PHINode &PN : LLVMBB->phis())
1756 FuncInfo->InvalidatePHILiveOutRegInfo(&PN);
1757 }
1758
1759 FuncInfo->VisitedBBs[LLVMBB->getNumber()] = true;
1760 }
1761
1762 // Fake uses that follow tail calls are dropped. To avoid this, move
1763 // such fake uses in front of the tail call, provided they don't
1764 // use anything def'd by or after the tail call.
1765 {
1766 BasicBlock::iterator BBStart =
1767 const_cast<BasicBlock *>(LLVMBB)->getFirstNonPHIIt();
1768 BasicBlock::iterator BBEnd = const_cast<BasicBlock *>(LLVMBB)->end();
1769 preserveFakeUses(BBStart, BBEnd);
1770 }
1771
1772 BasicBlock::const_iterator const Begin = LLVMBB->getFirstNonPHIIt();
1773 BasicBlock::const_iterator const End = LLVMBB->end();
1775
1776 FuncInfo->MBB = FuncInfo->getMBB(LLVMBB);
1777 if (!FuncInfo->MBB)
1778 continue; // Some blocks like catchpads have no code or MBB.
1779
1780 // Insert new instructions after any phi or argument setup code.
1781 FuncInfo->InsertPt = FuncInfo->MBB->end();
1782
1783 // Setup an EH landing-pad block.
1784 FuncInfo->ExceptionPointerVirtReg = Register();
1785 FuncInfo->ExceptionSelectorVirtReg = Register();
1786 if (LLVMBB->isEHPad()) {
1787 if (!PrepareEHLandingPad())
1788 continue;
1789
1790 if (!FastIS) {
1791 SDValue NewRoot = TLI->lowerEHPadEntry(CurDAG->getRoot(),
1792 SDB->getCurSDLoc(), *CurDAG);
1793 if (NewRoot && NewRoot != CurDAG->getRoot())
1794 CurDAG->setRoot(NewRoot);
1795 }
1796 }
1797
1798 // Before doing SelectionDAG ISel, see if FastISel has been requested.
1799 if (FastIS) {
1800 if (LLVMBB != &Fn.getEntryBlock())
1801 FastIS->startNewBlock();
1802
1803 unsigned NumFastIselRemaining = std::distance(Begin, End);
1804
1805 // Pre-assign swifterror vregs.
1806 SwiftError->preassignVRegs(FuncInfo->MBB, Begin, End);
1807
1808 // Do FastISel on as many instructions as possible.
1809 for (; BI != Begin; --BI) {
1810 const Instruction *Inst = &*std::prev(BI);
1811
1812 // If we no longer require this instruction, skip it.
1813 if (isFoldedOrDeadInstruction(Inst, *FuncInfo) ||
1814 ElidedArgCopyInstrs.count(Inst)) {
1815 --NumFastIselRemaining;
1816 FastIS->handleDbgInfo(Inst);
1817 continue;
1818 }
1819
1820 // Bottom-up: reset the insert pos at the top, after any local-value
1821 // instructions.
1822 FastIS->recomputeInsertPt();
1823
1824 // Try to select the instruction with FastISel.
1825 if (FastIS->selectInstruction(Inst)) {
1826 --NumFastIselRemaining;
1827 ++NumFastIselSuccess;
1828
1829 FastIS->handleDbgInfo(Inst);
1830 // If fast isel succeeded, skip over all the folded instructions, and
1831 // then see if there is a load right before the selected instructions.
1832 // Try to fold the load if so.
1833 const Instruction *BeforeInst = Inst;
1834 while (BeforeInst != &*Begin) {
1835 BeforeInst = &*std::prev(BasicBlock::const_iterator(BeforeInst));
1836 if (!isFoldedOrDeadInstruction(BeforeInst, *FuncInfo))
1837 break;
1838 }
1839 if (BeforeInst != Inst && isa<LoadInst>(BeforeInst) &&
1840 BeforeInst->hasOneUse() &&
1841 FastIS->tryToFoldLoad(cast<LoadInst>(BeforeInst), Inst)) {
1842 // If we succeeded, don't re-select the load.
1844 << "FastISel folded load: " << *BeforeInst << "\n");
1845 FastIS->handleDbgInfo(BeforeInst);
1846 BI = std::next(BasicBlock::const_iterator(BeforeInst));
1847 --NumFastIselRemaining;
1848 ++NumFastIselSuccess;
1849 }
1850 continue;
1851 }
1852
1853 FastISelFailed = true;
1854
1855 // Then handle certain instructions as single-LLVM-Instruction blocks.
1856 // We cannot separate out GCrelocates to their own blocks since we need
1857 // to keep track of gc-relocates for a particular gc-statepoint. This is
1858 // done by SelectionDAGBuilder::LowerAsSTATEPOINT, called before
1859 // visitGCRelocate.
1860 if (isa<CallInst>(Inst) && !isa<GCStatepointInst>(Inst) &&
1861 !isa<GCRelocateInst>(Inst) && !isa<GCResultInst>(Inst)) {
1862 OptimizationRemarkMissed R("sdagisel", "FastISelFailure",
1863 Inst->getDebugLoc(), LLVMBB);
1864
1865 R << "FastISel missed call";
1866
1867 if (R.isEnabled() || EnableFastISelAbort) {
1868 std::string InstStrStorage;
1869 raw_string_ostream InstStr(InstStrStorage);
1870 InstStr << *Inst;
1871
1872 R << ": " << InstStrStorage;
1873 }
1874
1876
1877 // If the call has operand bundles, then it's best if they are handled
1878 // together with the call instead of selecting the call as its own
1879 // block.
1880 if (cast<CallInst>(Inst)->hasOperandBundles()) {
1881 NumFastIselFailures += NumFastIselRemaining;
1882 break;
1883 }
1884
1885 if (!Inst->getType()->isVoidTy() && !Inst->getType()->isTokenTy() &&
1886 !Inst->use_empty()) {
1887 Register &R = FuncInfo->ValueMap[Inst];
1888 if (!R)
1889 R = FuncInfo->CreateRegs(Inst);
1890 }
1891
1892 bool HadTailCall = false;
1893 MachineBasicBlock::iterator SavedInsertPt = FuncInfo->InsertPt;
1894 SelectBasicBlock(Inst->getIterator(), BI, HadTailCall);
1895
1896 // If the call was emitted as a tail call, we're done with the block.
1897 // We also need to delete any previously emitted instructions.
1898 if (HadTailCall) {
1899 FastIS->removeDeadCode(SavedInsertPt, FuncInfo->MBB->end());
1900 --BI;
1901 break;
1902 }
1903
1904 // Recompute NumFastIselRemaining as Selection DAG instruction
1905 // selection may have handled the call, input args, etc.
1906 unsigned RemainingNow = std::distance(Begin, BI);
1907 NumFastIselFailures += NumFastIselRemaining - RemainingNow;
1908 NumFastIselRemaining = RemainingNow;
1909 continue;
1910 }
1911
1912 OptimizationRemarkMissed R("sdagisel", "FastISelFailure",
1913 Inst->getDebugLoc(), LLVMBB);
1914
1915 bool ShouldAbort = EnableFastISelAbort;
1916 if (Inst->isTerminator()) {
1917 // Use a different message for terminator misses.
1918 R << "FastISel missed terminator";
1919 // Don't abort for terminator unless the level is really high
1920 ShouldAbort = (EnableFastISelAbort > 2);
1921 } else {
1922 R << "FastISel missed";
1923 }
1924
1925 if (R.isEnabled() || EnableFastISelAbort) {
1926 std::string InstStrStorage;
1927 raw_string_ostream InstStr(InstStrStorage);
1928 InstStr << *Inst;
1929 R << ": " << InstStrStorage;
1930 }
1931
1932 reportFastISelFailure(*MF, *ORE, R, ShouldAbort);
1933
1934 NumFastIselFailures += NumFastIselRemaining;
1935 break;
1936 }
1937
1938 FastIS->recomputeInsertPt();
1939 }
1940
1941 if (SP->shouldEmitSDCheck(*LLVMBB)) {
1942 bool FunctionBasedInstrumentation =
1943 TLI->getSSPStackGuardCheck(*Fn.getParent(), *LibcallLowering) &&
1944 Fn.hasMinSize();
1945 SDB->SPDescriptor.initialize(LLVMBB, FuncInfo->getMBB(LLVMBB),
1946 FunctionBasedInstrumentation);
1947 }
1948
1949 if (Begin != BI)
1950 ++NumDAGBlocks;
1951 else
1952 ++NumFastIselBlocks;
1953
1954 if (Begin != BI) {
1955 // Run SelectionDAG instruction selection on the remainder of the block
1956 // not handled by FastISel. If FastISel is not run, this is the entire
1957 // block.
1958 bool HadTailCall;
1959 SelectBasicBlock(Begin, BI, HadTailCall);
1960
1961 // But if FastISel was run, we already selected some of the block.
1962 // If we emitted a tail-call, we need to delete any previously emitted
1963 // instruction that follows it.
1964 if (FastIS && HadTailCall && FuncInfo->InsertPt != FuncInfo->MBB->end())
1965 FastIS->removeDeadCode(FuncInfo->InsertPt, FuncInfo->MBB->end());
1966 }
1967
1968 if (FastIS)
1969 FastIS->finishBasicBlock();
1970 FinishBasicBlock();
1971 FuncInfo->PHINodesToUpdate.clear();
1972 ElidedArgCopyInstrs.clear();
1973 }
1974
1975 // AsynchEH: Report Block State under -AsynchEH
1976 if (Fn.getParent()->getModuleFlag("eh-asynch"))
1977 reportIPToStateForBlocks(MF);
1978
1979 SP->copyToMachineFrameInfo(MF->getFrameInfo());
1980
1981 SwiftError->propagateVRegs();
1982
1983 delete FastIS;
1984 SDB->clearDanglingDebugInfo();
1985 SDB->SPDescriptor.resetPerFunctionState();
1986}
1987
1988void
1989SelectionDAGISel::FinishBasicBlock() {
1990 LLVM_DEBUG(dbgs() << "Total amount of phi nodes to update: "
1991 << FuncInfo->PHINodesToUpdate.size() << "\n";
1992 for (unsigned i = 0, e = FuncInfo->PHINodesToUpdate.size(); i != e;
1993 ++i) dbgs()
1994 << "Node " << i << " : (" << FuncInfo->PHINodesToUpdate[i].first
1995 << ", " << printReg(FuncInfo->PHINodesToUpdate[i].second)
1996 << ")\n");
1997
1998 // Next, now that we know what the last MBB the LLVM BB expanded is, update
1999 // PHI nodes in successors.
2000 for (unsigned i = 0, e = FuncInfo->PHINodesToUpdate.size(); i != e; ++i) {
2001 MachineInstrBuilder PHI(*MF, FuncInfo->PHINodesToUpdate[i].first);
2002 assert(PHI->isPHI() &&
2003 "This is not a machine PHI node that we are updating!");
2004 if (!FuncInfo->MBB->isSuccessor(PHI->getParent()))
2005 continue;
2006 PHI.addReg(FuncInfo->PHINodesToUpdate[i].second).addMBB(FuncInfo->MBB);
2007 }
2008
2009 // Handle stack protector.
2010 if (SDB->SPDescriptor.shouldEmitFunctionBasedCheckStackProtector()) {
2011 // The target provides a guard check function. There is no need to
2012 // generate error handling code or to split current basic block.
2013 MachineBasicBlock *ParentMBB = SDB->SPDescriptor.getParentMBB();
2014
2015 // Add load and check to the basicblock.
2016 FuncInfo->MBB = ParentMBB;
2017 FuncInfo->InsertPt = findSplitPointForStackProtector(ParentMBB, *TII);
2018 SDB->visitSPDescriptorParent(SDB->SPDescriptor, ParentMBB);
2019 CurDAG->setRoot(SDB->getRoot());
2020 SDB->clear();
2021 CodeGenAndEmitDAG();
2022
2023 // Clear the Per-BB State.
2024 SDB->SPDescriptor.resetPerBBState();
2025 } else if (SDB->SPDescriptor.shouldEmitStackProtector()) {
2026 MachineBasicBlock *ParentMBB = SDB->SPDescriptor.getParentMBB();
2027 MachineBasicBlock *SuccessMBB = SDB->SPDescriptor.getSuccessMBB();
2028
2029 // Find the split point to split the parent mbb. At the same time copy all
2030 // physical registers used in the tail of parent mbb into virtual registers
2031 // before the split point and back into physical registers after the split
2032 // point. This prevents us needing to deal with Live-ins and many other
2033 // register allocation issues caused by us splitting the parent mbb. The
2034 // register allocator will clean up said virtual copies later on.
2035 MachineBasicBlock::iterator SplitPoint =
2037
2038 // Splice the terminator of ParentMBB into SuccessMBB.
2039 SuccessMBB->splice(SuccessMBB->end(), ParentMBB, SplitPoint,
2040 ParentMBB->end());
2041
2042 // Add compare/jump on neq/jump to the parent BB.
2043 FuncInfo->MBB = ParentMBB;
2044 FuncInfo->InsertPt = ParentMBB->end();
2045 SDB->visitSPDescriptorParent(SDB->SPDescriptor, ParentMBB);
2046 CurDAG->setRoot(SDB->getRoot());
2047 SDB->clear();
2048 CodeGenAndEmitDAG();
2049
2050 // CodeGen Failure MBB if we have not codegened it yet.
2051 MachineBasicBlock *FailureMBB = SDB->SPDescriptor.getFailureMBB();
2052 if (FailureMBB->empty()) {
2053 FuncInfo->MBB = FailureMBB;
2054 FuncInfo->InsertPt = FailureMBB->end();
2055 SDB->visitSPDescriptorFailure(SDB->SPDescriptor);
2056 CurDAG->setRoot(SDB->getRoot());
2057 SDB->clear();
2058 CodeGenAndEmitDAG();
2059 }
2060
2061 // Clear the Per-BB State.
2062 SDB->SPDescriptor.resetPerBBState();
2063 }
2064
2065 // Lower each BitTestBlock.
2066 for (auto &BTB : SDB->SL->BitTestCases) {
2067 // Lower header first, if it wasn't already lowered
2068 if (!BTB.Emitted) {
2069 // Set the current basic block to the mbb we wish to insert the code into
2070 FuncInfo->MBB = BTB.Parent;
2071 FuncInfo->InsertPt = FuncInfo->MBB->end();
2072 // Emit the code
2073 SDB->visitBitTestHeader(BTB, FuncInfo->MBB);
2074 CurDAG->setRoot(SDB->getRoot());
2075 SDB->clear();
2076 CodeGenAndEmitDAG();
2077 }
2078
2079 BranchProbability UnhandledProb = BTB.Prob;
2080 for (unsigned j = 0, ej = BTB.Cases.size(); j != ej; ++j) {
2081 UnhandledProb -= BTB.Cases[j].ExtraProb;
2082 // Set the current basic block to the mbb we wish to insert the code into
2083 FuncInfo->MBB = BTB.Cases[j].ThisBB;
2084 FuncInfo->InsertPt = FuncInfo->MBB->end();
2085 // Emit the code
2086
2087 // If all cases cover a contiguous range, it is not necessary to jump to
2088 // the default block after the last bit test fails. This is because the
2089 // range check during bit test header creation has guaranteed that every
2090 // case here doesn't go outside the range. In this case, there is no need
2091 // to perform the last bit test, as it will always be true. Instead, make
2092 // the second-to-last bit-test fall through to the target of the last bit
2093 // test, and delete the last bit test.
2094
2095 MachineBasicBlock *NextMBB;
2096 if ((BTB.ContiguousRange || BTB.FallthroughUnreachable) && j + 2 == ej) {
2097 // Second-to-last bit-test with contiguous range or omitted range
2098 // check: fall through to the target of the final bit test.
2099 NextMBB = BTB.Cases[j + 1].TargetBB;
2100 } else if (j + 1 == ej) {
2101 // For the last bit test, fall through to Default.
2102 NextMBB = BTB.Default;
2103 } else {
2104 // Otherwise, fall through to the next bit test.
2105 NextMBB = BTB.Cases[j + 1].ThisBB;
2106 }
2107
2108 SDB->visitBitTestCase(BTB, NextMBB, UnhandledProb, BTB.Reg, BTB.Cases[j],
2109 FuncInfo->MBB);
2110
2111 CurDAG->setRoot(SDB->getRoot());
2112 SDB->clear();
2113 CodeGenAndEmitDAG();
2114
2115 if ((BTB.ContiguousRange || BTB.FallthroughUnreachable) && j + 2 == ej) {
2116 // Since we're not going to use the final bit test, remove it.
2117 BTB.Cases.pop_back();
2118 break;
2119 }
2120 }
2121
2122 // Update PHI Nodes
2123 for (const std::pair<MachineInstr *, Register> &P :
2124 FuncInfo->PHINodesToUpdate) {
2125 MachineInstrBuilder PHI(*MF, P.first);
2126 MachineBasicBlock *PHIBB = PHI->getParent();
2127 assert(PHI->isPHI() &&
2128 "This is not a machine PHI node that we are updating!");
2129 // This is "default" BB. We have two jumps to it. From "header" BB and
2130 // from last "case" BB, unless the latter was skipped.
2131 if (PHIBB == BTB.Default) {
2132 PHI.addReg(P.second).addMBB(BTB.Parent);
2133 if (!BTB.ContiguousRange) {
2134 PHI.addReg(P.second).addMBB(BTB.Cases.back().ThisBB);
2135 }
2136 }
2137 // One of "cases" BB.
2138 for (const SwitchCG::BitTestCase &BT : BTB.Cases) {
2139 MachineBasicBlock* cBB = BT.ThisBB;
2140 if (cBB->isSuccessor(PHIBB))
2141 PHI.addReg(P.second).addMBB(cBB);
2142 }
2143 }
2144 }
2145 SDB->SL->BitTestCases.clear();
2146
2147 // If the JumpTable record is filled in, then we need to emit a jump table.
2148 // Updating the PHI nodes is tricky in this case, since we need to determine
2149 // whether the PHI is a successor of the range check MBB or the jump table MBB
2150 for (unsigned i = 0, e = SDB->SL->JTCases.size(); i != e; ++i) {
2151 // Lower header first, if it wasn't already lowered
2152 if (!SDB->SL->JTCases[i].first.Emitted) {
2153 // Set the current basic block to the mbb we wish to insert the code into
2154 FuncInfo->MBB = SDB->SL->JTCases[i].first.HeaderBB;
2155 FuncInfo->InsertPt = FuncInfo->MBB->end();
2156 // Emit the code
2157 SDB->visitJumpTableHeader(SDB->SL->JTCases[i].second,
2158 SDB->SL->JTCases[i].first, FuncInfo->MBB);
2159 CurDAG->setRoot(SDB->getRoot());
2160 SDB->clear();
2161 CodeGenAndEmitDAG();
2162 }
2163
2164 // Set the current basic block to the mbb we wish to insert the code into
2165 FuncInfo->MBB = SDB->SL->JTCases[i].second.MBB;
2166 FuncInfo->InsertPt = FuncInfo->MBB->end();
2167 // Emit the code
2168 SDB->visitJumpTable(SDB->SL->JTCases[i].second);
2169 CurDAG->setRoot(SDB->getRoot());
2170 SDB->clear();
2171 CodeGenAndEmitDAG();
2172
2173 // Update PHI Nodes
2174 for (unsigned pi = 0, pe = FuncInfo->PHINodesToUpdate.size();
2175 pi != pe; ++pi) {
2176 MachineInstrBuilder PHI(*MF, FuncInfo->PHINodesToUpdate[pi].first);
2177 MachineBasicBlock *PHIBB = PHI->getParent();
2178 assert(PHI->isPHI() &&
2179 "This is not a machine PHI node that we are updating!");
2180 // "default" BB. We can go there only from header BB.
2181 if (PHIBB == SDB->SL->JTCases[i].second.Default)
2182 PHI.addReg(FuncInfo->PHINodesToUpdate[pi].second)
2183 .addMBB(SDB->SL->JTCases[i].first.HeaderBB);
2184 // JT BB. Just iterate over successors here
2185 if (FuncInfo->MBB->isSuccessor(PHIBB))
2186 PHI.addReg(FuncInfo->PHINodesToUpdate[pi].second).addMBB(FuncInfo->MBB);
2187 }
2188 }
2189 SDB->SL->JTCases.clear();
2190
2191 // If we generated any switch lowering information, build and codegen any
2192 // additional DAGs necessary.
2193 for (unsigned i = 0, e = SDB->SL->SwitchCases.size(); i != e; ++i) {
2194 // Set the current basic block to the mbb we wish to insert the code into
2195 FuncInfo->MBB = SDB->SL->SwitchCases[i].ThisBB;
2196 FuncInfo->InsertPt = FuncInfo->MBB->end();
2197
2198 // Determine the unique successors.
2200 Succs.push_back(SDB->SL->SwitchCases[i].TrueBB);
2201 if (SDB->SL->SwitchCases[i].TrueBB != SDB->SL->SwitchCases[i].FalseBB)
2202 Succs.push_back(SDB->SL->SwitchCases[i].FalseBB);
2203
2204 // Emit the code. Note that this could result in FuncInfo->MBB being split.
2205 SDB->visitSwitchCase(SDB->SL->SwitchCases[i], FuncInfo->MBB);
2206 CurDAG->setRoot(SDB->getRoot());
2207 SDB->clear();
2208 CodeGenAndEmitDAG();
2209
2210 // Remember the last block, now that any splitting is done, for use in
2211 // populating PHI nodes in successors.
2212 MachineBasicBlock *ThisBB = FuncInfo->MBB;
2213
2214 // Handle any PHI nodes in successors of this chunk, as if we were coming
2215 // from the original BB before switch expansion. Note that PHI nodes can
2216 // occur multiple times in PHINodesToUpdate. We have to be very careful to
2217 // handle them the right number of times.
2218 for (MachineBasicBlock *Succ : Succs) {
2219 FuncInfo->MBB = Succ;
2220 FuncInfo->InsertPt = FuncInfo->MBB->end();
2221 // FuncInfo->MBB may have been removed from the CFG if a branch was
2222 // constant folded.
2223 if (ThisBB->isSuccessor(FuncInfo->MBB)) {
2225 MBBI = FuncInfo->MBB->begin(), MBBE = FuncInfo->MBB->end();
2226 MBBI != MBBE && MBBI->isPHI(); ++MBBI) {
2227 MachineInstrBuilder PHI(*MF, MBBI);
2228 // This value for this PHI node is recorded in PHINodesToUpdate.
2229 for (unsigned pn = 0; ; ++pn) {
2230 assert(pn != FuncInfo->PHINodesToUpdate.size() &&
2231 "Didn't find PHI entry!");
2232 if (FuncInfo->PHINodesToUpdate[pn].first == PHI) {
2233 PHI.addReg(FuncInfo->PHINodesToUpdate[pn].second).addMBB(ThisBB);
2234 break;
2235 }
2236 }
2237 }
2238 }
2239 }
2240 }
2241 SDB->SL->SwitchCases.clear();
2242}
2243
2244/// Create the scheduler. If a specific scheduler was specified
2245/// via the SchedulerRegistry, use it, otherwise select the
2246/// one preferred by the target.
2247///
2248ScheduleDAGSDNodes *SelectionDAGISel::CreateScheduler() {
2249 return ISHeuristic(this, OptLevel);
2250}
2251
2252//===----------------------------------------------------------------------===//
2253// Helper functions used by the generated instruction selector.
2254//===----------------------------------------------------------------------===//
2255// Calls to these methods are generated by tblgen.
2256
2257/// CheckAndMask - The isel is trying to match something like (and X, 255). If
2258/// the dag combiner simplified the 255, we still want to match. RHS is the
2259/// actual value in the DAG on the RHS of an AND, and DesiredMaskS is the value
2260/// specified in the .td file (e.g. 255).
2262 int64_t DesiredMaskS) const {
2263 const APInt &ActualMask = RHS->getAPIntValue();
2264 // TODO: Avoid implicit trunc?
2265 // See https://github.com/llvm/llvm-project/issues/112510.
2266 const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS,
2267 /*isSigned=*/false, /*implicitTrunc=*/true);
2268
2269 // If the actual mask exactly matches, success!
2270 if (ActualMask == DesiredMask)
2271 return true;
2272
2273 // If the actual AND mask is allowing unallowed bits, this doesn't match.
2274 if (!ActualMask.isSubsetOf(DesiredMask))
2275 return false;
2276
2277 // Otherwise, the DAG Combiner may have proven that the value coming in is
2278 // either already zero or is not demanded. Check for known zero input bits.
2279 APInt NeededMask = DesiredMask & ~ActualMask;
2280 if (CurDAG->MaskedValueIsZero(LHS, NeededMask))
2281 return true;
2282
2283 // TODO: check to see if missing bits are just not demanded.
2284
2285 // Otherwise, this pattern doesn't match.
2286 return false;
2287}
2288
2289/// CheckOrMask - The isel is trying to match something like (or X, 255). If
2290/// the dag combiner simplified the 255, we still want to match. RHS is the
2291/// actual value in the DAG on the RHS of an OR, and DesiredMaskS is the value
2292/// specified in the .td file (e.g. 255).
2294 int64_t DesiredMaskS) const {
2295 const APInt &ActualMask = RHS->getAPIntValue();
2296 // TODO: Avoid implicit trunc?
2297 // See https://github.com/llvm/llvm-project/issues/112510.
2298 const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS,
2299 /*isSigned=*/false, /*implicitTrunc=*/true);
2300
2301 // If the actual mask exactly matches, success!
2302 if (ActualMask == DesiredMask)
2303 return true;
2304
2305 // If the actual AND mask is allowing unallowed bits, this doesn't match.
2306 if (!ActualMask.isSubsetOf(DesiredMask))
2307 return false;
2308
2309 // Otherwise, the DAG Combiner may have proven that the value coming in is
2310 // either already zero or is not demanded. Check for known zero input bits.
2311 APInt NeededMask = DesiredMask & ~ActualMask;
2312 KnownBits Known = CurDAG->computeKnownBits(LHS);
2313
2314 // If all the missing bits in the or are already known to be set, match!
2315 if (NeededMask.isSubsetOf(Known.One))
2316 return true;
2317
2318 // TODO: check to see if missing bits are just not demanded.
2319
2320 // Otherwise, this pattern doesn't match.
2321 return false;
2322}
2323
2324/// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
2325/// by tblgen. Others should not call it.
2327 const SDLoc &DL) {
2328 // Change the vector of SDValue into a list of SDNodeHandle for x86 might call
2329 // replaceAllUses when matching address.
2330
2331 std::list<HandleSDNode> Handles;
2332
2333 Handles.emplace_back(Ops[InlineAsm::Op_InputChain]); // 0
2334 Handles.emplace_back(Ops[InlineAsm::Op_AsmString]); // 1
2335 Handles.emplace_back(Ops[InlineAsm::Op_MDNode]); // 2, !srcloc
2336 Handles.emplace_back(
2337 Ops[InlineAsm::Op_ExtraInfo]); // 3 (SideEffect, AlignStack)
2338
2339 unsigned i = InlineAsm::Op_FirstOperand, e = Ops.size();
2340 if (Ops[e - 1].getValueType() == MVT::Glue)
2341 --e; // Don't process a glue operand if it is here.
2342
2343 while (i != e) {
2344 InlineAsm::Flag Flags(Ops[i]->getAsZExtVal());
2345 if (!Flags.isMemKind() && !Flags.isFuncKind()) {
2346 // Just skip over this operand, copying the operands verbatim.
2347 Handles.insert(Handles.end(), Ops.begin() + i,
2348 Ops.begin() + i + Flags.getNumOperandRegisters() + 1);
2349 i += Flags.getNumOperandRegisters() + 1;
2350 } else {
2351 assert(Flags.getNumOperandRegisters() == 1 &&
2352 "Memory operand with multiple values?");
2353
2354 unsigned TiedToOperand;
2355 if (Flags.isUseOperandTiedToDef(TiedToOperand)) {
2356 // We need the constraint ID from the operand this is tied to.
2357 unsigned CurOp = InlineAsm::Op_FirstOperand;
2358 Flags = InlineAsm::Flag(Ops[CurOp]->getAsZExtVal());
2359 for (; TiedToOperand; --TiedToOperand) {
2360 CurOp += Flags.getNumOperandRegisters() + 1;
2361 Flags = InlineAsm::Flag(Ops[CurOp]->getAsZExtVal());
2362 }
2363 }
2364
2365 // Otherwise, this is a memory operand. Ask the target to select it.
2366 std::vector<SDValue> SelOps;
2367 const InlineAsm::ConstraintCode ConstraintID =
2368 Flags.getMemoryConstraintID();
2369 if (SelectInlineAsmMemoryOperand(Ops[i + 1], ConstraintID, SelOps))
2370 report_fatal_error("Could not match memory address. Inline asm"
2371 " failure!");
2372
2373 // Add this to the output node.
2374 Flags = InlineAsm::Flag(Flags.isMemKind() ? InlineAsm::Kind::Mem
2376 SelOps.size());
2377 Flags.setMemConstraint(ConstraintID);
2378 Handles.emplace_back(CurDAG->getTargetConstant(Flags, DL, MVT::i32));
2379 llvm::append_range(Handles, SelOps);
2380 i += 2;
2381 }
2382 }
2383
2384 // Add the glue input back if present.
2385 if (e != Ops.size())
2386 Handles.emplace_back(Ops.back());
2387
2388 Ops.clear();
2389 for (auto &handle : Handles)
2390 Ops.push_back(handle.getValue());
2391}
2392
2393/// findNonImmUse - Return true if "Def" is a predecessor of "Root" via a path
2394/// beyond "ImmedUse". We may ignore chains as they are checked separately.
2395static bool findNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
2396 bool IgnoreChains) {
2399 // Only check if we have non-immediate uses of Def.
2400 if (ImmedUse->isOnlyUserOf(Def))
2401 return false;
2402
2403 // We don't care about paths to Def that go through ImmedUse so mark it
2404 // visited and mark non-def operands as used.
2405 Visited.insert(ImmedUse);
2406 for (const SDValue &Op : ImmedUse->op_values()) {
2407 SDNode *N = Op.getNode();
2408 // Ignore chain deps (they are validated by
2409 // HandleMergeInputChains) and immediate uses
2410 if ((Op.getValueType() == MVT::Other && IgnoreChains) || N == Def)
2411 continue;
2412 if (!Visited.insert(N).second)
2413 continue;
2414 WorkList.push_back(N);
2415 }
2416
2417 // Initialize worklist to operands of Root.
2418 if (Root != ImmedUse) {
2419 for (const SDValue &Op : Root->op_values()) {
2420 SDNode *N = Op.getNode();
2421 // Ignore chains (they are validated by HandleMergeInputChains)
2422 if ((Op.getValueType() == MVT::Other && IgnoreChains) || N == Def)
2423 continue;
2424 if (!Visited.insert(N).second)
2425 continue;
2426 WorkList.push_back(N);
2427 }
2428 }
2429
2430 return SDNode::hasPredecessorHelper(Def, Visited, WorkList, 0, true);
2431}
2432
2433/// IsProfitableToFold - Returns true if it's profitable to fold the specific
2434/// operand node N of U during instruction selection that starts at Root.
2436 SDNode *Root) const {
2438 return false;
2439 return N.hasOneUse();
2440}
2441
2442/// IsLegalToFold - Returns true if the specific operand node N of
2443/// U can be folded during instruction selection that starts at Root.
2446 bool IgnoreChains) {
2448 return false;
2449
2450 // If Root use can somehow reach N through a path that doesn't contain
2451 // U then folding N would create a cycle. e.g. In the following
2452 // diagram, Root can reach N through X. If N is folded into Root, then
2453 // X is both a predecessor and a successor of U.
2454 //
2455 // [N*] //
2456 // ^ ^ //
2457 // / \ //
2458 // [U*] [X]? //
2459 // ^ ^ //
2460 // \ / //
2461 // \ / //
2462 // [Root*] //
2463 //
2464 // * indicates nodes to be folded together.
2465 //
2466 // If Root produces glue, then it gets (even more) interesting. Since it
2467 // will be "glued" together with its glue use in the scheduler, we need to
2468 // check if it might reach N.
2469 //
2470 // [N*] //
2471 // ^ ^ //
2472 // / \ //
2473 // [U*] [X]? //
2474 // ^ ^ //
2475 // \ \ //
2476 // \ | //
2477 // [Root*] | //
2478 // ^ | //
2479 // f | //
2480 // | / //
2481 // [Y] / //
2482 // ^ / //
2483 // f / //
2484 // | / //
2485 // [GU] //
2486 //
2487 // If GU (glue use) indirectly reaches N (the load), and Root folds N
2488 // (call it Fold), then X is a predecessor of GU and a successor of
2489 // Fold. But since Fold and GU are glued together, this will create
2490 // a cycle in the scheduling graph.
2491
2492 // If the node has glue, walk down the graph to the "lowest" node in the
2493 // glued set.
2494 EVT VT = Root->getValueType(Root->getNumValues()-1);
2495 while (VT == MVT::Glue) {
2496 SDNode *GU = Root->getGluedUser();
2497 if (!GU)
2498 break;
2499 Root = GU;
2500 VT = Root->getValueType(Root->getNumValues()-1);
2501
2502 // If our query node has a glue result with a use, we've walked up it. If
2503 // the user (which has already been selected) has a chain or indirectly uses
2504 // the chain, HandleMergeInputChains will not consider it. Because of
2505 // this, we cannot ignore chains in this predicate.
2506 IgnoreChains = false;
2507 }
2508
2509 return !findNonImmUse(Root, N.getNode(), U, IgnoreChains);
2510}
2511
2512void SelectionDAGISel::Select_INLINEASM(SDNode *N) {
2513 SDLoc DL(N);
2514
2515 std::vector<SDValue> Ops(N->op_begin(), N->op_end());
2517
2518 const EVT VTs[] = {MVT::Other, MVT::Glue};
2519 SDValue New = CurDAG->getNode(N->getOpcode(), DL, VTs, Ops);
2520 New->setNodeId(-1);
2521 ReplaceUses(N, New.getNode());
2523}
2524
2525void SelectionDAGISel::Select_READ_REGISTER(SDNode *Op) {
2526 SDLoc dl(Op);
2527 MDNodeSDNode *MD = cast<MDNodeSDNode>(Op->getOperand(1));
2528 const MDString *RegStr = cast<MDString>(MD->getMD()->getOperand(0));
2529
2530 EVT VT = Op->getValueType(0);
2531 LLT Ty = VT.isSimple() ? getLLTForMVT(VT.getSimpleVT()) : LLT();
2532
2533 const MachineFunction &MF = CurDAG->getMachineFunction();
2534 Register Reg = TLI->getRegisterByName(RegStr->getString().data(), Ty, MF);
2535
2536 SDValue New;
2537 if (!Reg) {
2538 const Function &Fn = MF.getFunction();
2539 Fn.getContext().diagnose(DiagnosticInfoGenericWithLoc(
2540 "invalid register \"" + Twine(RegStr->getString().data()) +
2541 "\" for llvm.read_register",
2542 Fn, Op->getDebugLoc()));
2543 New =
2544 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0);
2545 ReplaceUses(SDValue(Op, 1), Op->getOperand(0));
2546 } else {
2547 New =
2548 CurDAG->getCopyFromReg(Op->getOperand(0), dl, Reg, Op->getValueType(0));
2549 }
2550
2551 New->setNodeId(-1);
2552 ReplaceUses(Op, New.getNode());
2553 CurDAG->RemoveDeadNode(Op);
2554}
2555
2556void SelectionDAGISel::Select_WRITE_REGISTER(SDNode *Op) {
2557 SDLoc dl(Op);
2558 MDNodeSDNode *MD = cast<MDNodeSDNode>(Op->getOperand(1));
2559 const MDString *RegStr = cast<MDString>(MD->getMD()->getOperand(0));
2560
2561 EVT VT = Op->getOperand(2).getValueType();
2562 LLT Ty = VT.isSimple() ? getLLTForMVT(VT.getSimpleVT()) : LLT();
2563
2564 const MachineFunction &MF = CurDAG->getMachineFunction();
2565 Register Reg = TLI->getRegisterByName(RegStr->getString().data(), Ty, MF);
2566
2567 if (!Reg) {
2568 const Function &Fn = MF.getFunction();
2569 Fn.getContext().diagnose(DiagnosticInfoGenericWithLoc(
2570 "invalid register \"" + Twine(RegStr->getString().data()) +
2571 "\" for llvm.write_register",
2572 Fn, Op->getDebugLoc()));
2573 ReplaceUses(SDValue(Op, 0), Op->getOperand(0));
2574 } else {
2575 SDValue New =
2576 CurDAG->getCopyToReg(Op->getOperand(0), dl, Reg, Op->getOperand(2));
2577 New->setNodeId(-1);
2578 ReplaceUses(Op, New.getNode());
2579 }
2580
2581 CurDAG->RemoveDeadNode(Op);
2582}
2583
2584void SelectionDAGISel::Select_UNDEF(SDNode *N) {
2585 CurDAG->SelectNodeTo(N, TargetOpcode::IMPLICIT_DEF, N->getValueType(0));
2586}
2587
2588// Use the generic target FAKE_USE target opcode. The chain operand
2589// must come last, because InstrEmitter::AddOperand() requires it.
2590void SelectionDAGISel::Select_FAKE_USE(SDNode *N) {
2591 CurDAG->SelectNodeTo(N, TargetOpcode::FAKE_USE, N->getValueType(0),
2592 N->getOperand(1), N->getOperand(0));
2593}
2594
2595void SelectionDAGISel::Select_RELOC_NONE(SDNode *N) {
2596 CurDAG->SelectNodeTo(N, TargetOpcode::RELOC_NONE, N->getValueType(0),
2597 N->getOperand(1), N->getOperand(0));
2598}
2599
2600void SelectionDAGISel::Select_FREEZE(SDNode *N) {
2601 // TODO: We don't have FREEZE pseudo-instruction in MachineInstr-level now.
2602 // If FREEZE instruction is added later, the code below must be changed as
2603 // well.
2604 CurDAG->SelectNodeTo(N, TargetOpcode::COPY, N->getValueType(0),
2605 N->getOperand(0));
2606}
2607
2608void SelectionDAGISel::Select_ARITH_FENCE(SDNode *N) {
2609 CurDAG->SelectNodeTo(N, TargetOpcode::ARITH_FENCE, N->getValueType(0),
2610 N->getOperand(0));
2611}
2612
2613void SelectionDAGISel::Select_MEMBARRIER(SDNode *N) {
2614 CurDAG->SelectNodeTo(N, TargetOpcode::MEMBARRIER, N->getValueType(0),
2615 N->getOperand(0));
2616}
2617
2618void SelectionDAGISel::Select_CONVERGENCECTRL_ANCHOR(SDNode *N) {
2619 CurDAG->SelectNodeTo(N, TargetOpcode::CONVERGENCECTRL_ANCHOR,
2620 N->getValueType(0));
2621}
2622
2623void SelectionDAGISel::Select_CONVERGENCECTRL_ENTRY(SDNode *N) {
2624 CurDAG->SelectNodeTo(N, TargetOpcode::CONVERGENCECTRL_ENTRY,
2625 N->getValueType(0));
2626}
2627
2628void SelectionDAGISel::Select_CONVERGENCECTRL_LOOP(SDNode *N) {
2629 CurDAG->SelectNodeTo(N, TargetOpcode::CONVERGENCECTRL_LOOP,
2630 N->getValueType(0), N->getOperand(0));
2631}
2632
2633void SelectionDAGISel::pushStackMapLiveVariable(SmallVectorImpl<SDValue> &Ops,
2634 SDValue OpVal, SDLoc DL) {
2635 SDNode *OpNode = OpVal.getNode();
2636
2637 // FrameIndex nodes should have been directly emitted to TargetFrameIndex
2638 // nodes at DAG-construction time.
2639 assert(OpNode->getOpcode() != ISD::FrameIndex);
2640
2641 if (OpNode->getOpcode() == ISD::Constant) {
2642 Ops.push_back(
2643 CurDAG->getTargetConstant(StackMaps::ConstantOp, DL, MVT::i64));
2644 Ops.push_back(CurDAG->getTargetConstant(OpNode->getAsZExtVal(), DL,
2645 OpVal.getValueType()));
2646 } else {
2647 Ops.push_back(OpVal);
2648 }
2649}
2650
2651void SelectionDAGISel::Select_STACKMAP(SDNode *N) {
2653 auto *It = N->op_begin();
2654 SDLoc DL(N);
2655
2656 // Stash the chain and glue operands so we can move them to the end.
2657 SDValue Chain = *It++;
2658 SDValue InGlue = *It++;
2659
2660 // <id> operand.
2661 SDValue ID = *It++;
2662 assert(ID.getValueType() == MVT::i64);
2663 Ops.push_back(ID);
2664
2665 // <numShadowBytes> operand.
2666 SDValue Shad = *It++;
2667 assert(Shad.getValueType() == MVT::i32);
2668 Ops.push_back(Shad);
2669
2670 // Live variable operands.
2671 for (; It != N->op_end(); It++)
2672 pushStackMapLiveVariable(Ops, *It, DL);
2673
2674 Ops.push_back(Chain);
2675 Ops.push_back(InGlue);
2676
2677 SDVTList NodeTys = CurDAG->getVTList(MVT::Other, MVT::Glue);
2678 CurDAG->SelectNodeTo(N, TargetOpcode::STACKMAP, NodeTys, Ops);
2679}
2680
2681void SelectionDAGISel::Select_PATCHPOINT(SDNode *N) {
2683 auto *It = N->op_begin();
2684 SDLoc DL(N);
2685
2686 // Cache arguments that will be moved to the end in the target node.
2687 SDValue Chain = *It++;
2688 std::optional<SDValue> Glue;
2689 if (It->getValueType() == MVT::Glue)
2690 Glue = *It++;
2691 SDValue RegMask = *It++;
2692
2693 // <id> operand.
2694 SDValue ID = *It++;
2695 assert(ID.getValueType() == MVT::i64);
2696 Ops.push_back(ID);
2697
2698 // <numShadowBytes> operand.
2699 SDValue Shad = *It++;
2700 assert(Shad.getValueType() == MVT::i32);
2701 Ops.push_back(Shad);
2702
2703 // Add the callee.
2704 Ops.push_back(*It++);
2705
2706 // Add <numArgs>.
2707 SDValue NumArgs = *It++;
2708 assert(NumArgs.getValueType() == MVT::i32);
2709 Ops.push_back(NumArgs);
2710
2711 // Calling convention.
2712 Ops.push_back(*It++);
2713
2714 // Push the args for the call.
2715 for (uint64_t I = NumArgs->getAsZExtVal(); I != 0; I--)
2716 Ops.push_back(*It++);
2717
2718 // Now push the live variables.
2719 for (; It != N->op_end(); It++)
2720 pushStackMapLiveVariable(Ops, *It, DL);
2721
2722 // Finally, the regmask, chain and (if present) glue are moved to the end.
2723 Ops.push_back(RegMask);
2724 Ops.push_back(Chain);
2725 if (Glue.has_value())
2726 Ops.push_back(*Glue);
2727
2728 SDVTList NodeTys = N->getVTList();
2729 CurDAG->SelectNodeTo(N, TargetOpcode::PATCHPOINT, NodeTys, Ops);
2730}
2731
2732/// GetVBR - decode a vbr encoding whose top bit is set.
2733LLVM_ATTRIBUTE_ALWAYS_INLINE static uint64_t
2734GetVBR(uint64_t Val, const uint8_t *MatcherTable, unsigned &Idx) {
2735 assert(Val >= 128 && "Not a VBR");
2736 Val &= 127; // Remove first vbr bit.
2737
2738 unsigned Shift = 7;
2739 uint64_t NextBits;
2740 do {
2741 NextBits = MatcherTable[Idx++];
2742 Val |= (NextBits&127) << Shift;
2743 Shift += 7;
2744 } while (NextBits & 128);
2745
2746 return Val;
2747}
2748
2749LLVM_ATTRIBUTE_ALWAYS_INLINE static int64_t
2750GetSignedVBR(const unsigned char *MatcherTable, unsigned &Idx) {
2751 int64_t Val = 0;
2752 unsigned Shift = 0;
2753 uint64_t NextBits;
2754 do {
2755 NextBits = MatcherTable[Idx++];
2756 Val |= (NextBits & 127) << Shift;
2757 Shift += 7;
2758 } while (NextBits & 128);
2759
2760 if (Shift < 64 && (NextBits & 0x40))
2761 Val |= UINT64_MAX << Shift;
2762
2763 return Val;
2764}
2765
2766/// getSimpleVT - Decode a value in MatcherTable, if it's a VBR encoded value,
2767/// use GetVBR to decode it.
2769getSimpleVT(const uint8_t *MatcherTable, unsigned &MatcherIndex) {
2770 unsigned SimpleVT = MatcherTable[MatcherIndex++];
2771 if (SimpleVT & 128)
2772 SimpleVT = GetVBR(SimpleVT, MatcherTable, MatcherIndex);
2773
2774 return static_cast<MVT::SimpleValueType>(SimpleVT);
2775}
2776
2777/// Decode a HwMode VT in MatcherTable by calling getValueTypeForHwMode.
2779getHwModeVT(const uint8_t *MatcherTable, unsigned &MatcherIndex,
2780 const SelectionDAGISel &SDISel) {
2781 unsigned Index = MatcherTable[MatcherIndex++];
2782 return SDISel.getValueTypeForHwMode(Index);
2783}
2784
2785void SelectionDAGISel::Select_JUMP_TABLE_DEBUG_INFO(SDNode *N) {
2786 SDLoc dl(N);
2787 CurDAG->SelectNodeTo(N, TargetOpcode::JUMP_TABLE_DEBUG_INFO, MVT::Glue,
2788 CurDAG->getTargetConstant(N->getConstantOperandVal(1),
2789 dl, MVT::i64, true));
2790}
2791
2792/// When a match is complete, this method updates uses of interior chain results
2793/// to use the new results.
2794void SelectionDAGISel::UpdateChains(
2795 SDNode *NodeToMatch, SDValue InputChain,
2796 SmallVectorImpl<SDNode *> &ChainNodesMatched, bool isMorphNodeTo) {
2797 SmallVector<SDNode*, 4> NowDeadNodes;
2798
2799 // Now that all the normal results are replaced, we replace the chain and
2800 // glue results if present.
2801 if (!ChainNodesMatched.empty()) {
2802 assert(InputChain.getNode() &&
2803 "Matched input chains but didn't produce a chain");
2804 // Loop over all of the nodes we matched that produced a chain result.
2805 // Replace all the chain results with the final chain we ended up with.
2806 for (unsigned i = 0, e = ChainNodesMatched.size(); i != e; ++i) {
2807 SDNode *ChainNode = ChainNodesMatched[i];
2808 // If ChainNode is null, it's because we replaced it on a previous
2809 // iteration and we cleared it out of the map. Just skip it.
2810 if (!ChainNode)
2811 continue;
2812
2813 assert(ChainNode->getOpcode() != ISD::DELETED_NODE &&
2814 "Deleted node left in chain");
2815
2816 // Don't replace the results of the root node if we're doing a
2817 // MorphNodeTo.
2818 if (ChainNode == NodeToMatch && isMorphNodeTo)
2819 continue;
2820
2821 SDValue ChainVal = SDValue(ChainNode, ChainNode->getNumValues()-1);
2822 if (ChainVal.getValueType() == MVT::Glue)
2823 ChainVal = ChainVal.getValue(ChainVal->getNumValues()-2);
2824 assert(ChainVal.getValueType() == MVT::Other && "Not a chain?");
2825 SelectionDAG::DAGNodeDeletedListener NDL(
2826 *CurDAG, [&](SDNode *N, SDNode *E) {
2827 llvm::replace(ChainNodesMatched, N, static_cast<SDNode *>(nullptr));
2828 });
2829 if (ChainNode->getOpcode() != ISD::TokenFactor)
2830 ReplaceUses(ChainVal, InputChain);
2831
2832 // If the node became dead and we haven't already seen it, delete it.
2833 if (ChainNode != NodeToMatch && ChainNode->use_empty() &&
2834 !llvm::is_contained(NowDeadNodes, ChainNode))
2835 NowDeadNodes.push_back(ChainNode);
2836 }
2837 }
2838
2839 if (!NowDeadNodes.empty())
2840 CurDAG->RemoveDeadNodes(NowDeadNodes);
2841
2842 LLVM_DEBUG(dbgs() << "ISEL: Match complete!\n");
2843}
2844
2845/// HandleMergeInputChains - This implements the OPC_EmitMergeInputChains
2846/// operation for when the pattern matched at least one node with a chains. The
2847/// input vector contains a list of all of the chained nodes that we match. We
2848/// must determine if this is a valid thing to cover (i.e. matching it won't
2849/// induce cycles in the DAG) and if so, creating a TokenFactor node. that will
2850/// be used as the input node chain for the generated nodes.
2851static SDValue
2853 SDValue InputGlue, SelectionDAG *CurDAG) {
2854
2857 SmallVector<SDValue, 3> InputChains;
2858 unsigned int Max = 8192;
2859
2860 // Quick exit on trivial merge.
2861 if (ChainNodesMatched.size() == 1)
2862 return ChainNodesMatched[0]->getOperand(0);
2863
2864 // Add chains that aren't already added (internal). Peek through
2865 // token factors.
2866 std::function<void(const SDValue)> AddChains = [&](const SDValue V) {
2867 if (V.getValueType() != MVT::Other)
2868 return;
2869 if (V->getOpcode() == ISD::EntryToken)
2870 return;
2871 if (!Visited.insert(V.getNode()).second)
2872 return;
2873 if (V->getOpcode() == ISD::TokenFactor) {
2874 for (const SDValue &Op : V->op_values())
2875 AddChains(Op);
2876 } else
2877 InputChains.push_back(V);
2878 };
2879
2880 for (auto *N : ChainNodesMatched) {
2881 Worklist.push_back(N);
2882 Visited.insert(N);
2883 }
2884
2885 while (!Worklist.empty())
2886 AddChains(Worklist.pop_back_val()->getOperand(0));
2887
2888 // Skip the search if there are no chain dependencies.
2889 if (InputChains.size() == 0)
2890 return CurDAG->getEntryNode();
2891
2892 // If one of these chains is a successor of input, we must have a
2893 // node that is both the predecessor and successor of the
2894 // to-be-merged nodes. Fail.
2895 Visited.clear();
2896 for (SDValue V : InputChains) {
2897 // If we need to create a TokenFactor, and any of the input chain nodes will
2898 // also be glued to the output, we cannot merge the chains. The TokenFactor
2899 // would prevent the glue from being honored.
2900 if (InputChains.size() != 1 &&
2901 V->getValueType(V->getNumValues() - 1) == MVT::Glue &&
2902 InputGlue.getNode() == V.getNode())
2903 return SDValue();
2904 Worklist.push_back(V.getNode());
2905 }
2906
2907 for (auto *N : ChainNodesMatched)
2908 if (SDNode::hasPredecessorHelper(N, Visited, Worklist, Max, true))
2909 return SDValue();
2910
2911 // Return merged chain.
2912 if (InputChains.size() == 1)
2913 return InputChains[0];
2914 return CurDAG->getNode(ISD::TokenFactor, SDLoc(ChainNodesMatched[0]),
2915 MVT::Other, InputChains);
2916}
2917
2918/// MorphNode - Handle morphing a node in place for the selector.
2919SDNode *SelectionDAGISel::
2920MorphNode(SDNode *Node, unsigned TargetOpc, SDVTList VTList,
2921 ArrayRef<SDValue> Ops, unsigned EmitNodeInfo) {
2922 // It is possible we're using MorphNodeTo to replace a node with no
2923 // normal results with one that has a normal result (or we could be
2924 // adding a chain) and the input could have glue and chains as well.
2925 // In this case we need to shift the operands down.
2926 // FIXME: This is a horrible hack and broken in obscure cases, no worse
2927 // than the old isel though.
2928 int OldGlueResultNo = -1, OldChainResultNo = -1;
2929
2930 unsigned NTMNumResults = Node->getNumValues();
2931 if (Node->getValueType(NTMNumResults-1) == MVT::Glue) {
2932 OldGlueResultNo = NTMNumResults-1;
2933 if (NTMNumResults != 1 &&
2934 Node->getValueType(NTMNumResults-2) == MVT::Other)
2935 OldChainResultNo = NTMNumResults-2;
2936 } else if (Node->getValueType(NTMNumResults-1) == MVT::Other)
2937 OldChainResultNo = NTMNumResults-1;
2938
2939 // Call the underlying SelectionDAG routine to do the transmogrification. Note
2940 // that this deletes operands of the old node that become dead.
2941 SDNode *Res = CurDAG->MorphNodeTo(Node, ~TargetOpc, VTList, Ops);
2942
2943 // MorphNodeTo can operate in two ways: if an existing node with the
2944 // specified operands exists, it can just return it. Otherwise, it
2945 // updates the node in place to have the requested operands.
2946 if (Res == Node) {
2947 // If we updated the node in place, reset the node ID. To the isel,
2948 // this should be just like a newly allocated machine node.
2949 Res->setNodeId(-1);
2950 }
2951
2952 unsigned ResNumResults = Res->getNumValues();
2953 // Move the glue if needed.
2954 if ((EmitNodeInfo & OPFL_GlueOutput) && OldGlueResultNo != -1 &&
2955 static_cast<unsigned>(OldGlueResultNo) != ResNumResults - 1)
2956 ReplaceUses(SDValue(Node, OldGlueResultNo),
2957 SDValue(Res, ResNumResults - 1));
2958
2959 if ((EmitNodeInfo & OPFL_GlueOutput) != 0)
2960 --ResNumResults;
2961
2962 // Move the chain reference if needed.
2963 if ((EmitNodeInfo & OPFL_Chain) && OldChainResultNo != -1 &&
2964 static_cast<unsigned>(OldChainResultNo) != ResNumResults - 1)
2965 ReplaceUses(SDValue(Node, OldChainResultNo),
2966 SDValue(Res, ResNumResults - 1));
2967
2968 // Otherwise, no replacement happened because the node already exists. Replace
2969 // Uses of the old node with the new one.
2970 if (Res != Node) {
2971 ReplaceNode(Node, Res);
2972 } else {
2974 }
2975
2976 return Res;
2977}
2978
2979/// CheckSame - Implements OP_CheckSame.
2981CheckSame(const uint8_t *MatcherTable, unsigned &MatcherIndex, SDValue N,
2982 const SmallVectorImpl<std::pair<SDValue, SDNode *>> &RecordedNodes) {
2983 // Accept if it is exactly the same as a previously recorded node.
2984 unsigned RecNo = MatcherTable[MatcherIndex++];
2985 assert(RecNo < RecordedNodes.size() && "Invalid CheckSame");
2986 return N == RecordedNodes[RecNo].first;
2987}
2988
2989/// CheckChildSame - Implements OP_CheckChildXSame.
2991 const uint8_t *MatcherTable, unsigned &MatcherIndex, SDValue N,
2992 const SmallVectorImpl<std::pair<SDValue, SDNode *>> &RecordedNodes,
2993 unsigned ChildNo) {
2994 if (ChildNo >= N.getNumOperands())
2995 return false; // Match fails if out of range child #.
2996 return ::CheckSame(MatcherTable, MatcherIndex, N.getOperand(ChildNo),
2997 RecordedNodes);
2998}
2999
3000/// CheckPatternPredicate - Implements OP_CheckPatternPredicate.
3002CheckPatternPredicate(unsigned Opcode, const uint8_t *MatcherTable,
3003 unsigned &MatcherIndex, const SelectionDAGISel &SDISel) {
3004 bool TwoBytePredNo =
3006 unsigned PredNo =
3007 TwoBytePredNo || Opcode == SelectionDAGISel::OPC_CheckPatternPredicate
3008 ? MatcherTable[MatcherIndex++]
3010 if (TwoBytePredNo)
3011 PredNo |= MatcherTable[MatcherIndex++] << 8;
3012 return SDISel.CheckPatternPredicate(PredNo);
3013}
3014
3015/// CheckNodePredicate - Implements OP_CheckNodePredicate.
3017CheckNodePredicate(unsigned Opcode, const uint8_t *MatcherTable,
3018 unsigned &MatcherIndex, const SelectionDAGISel &SDISel,
3019 SDValue Op) {
3020 unsigned PredNo = Opcode == SelectionDAGISel::OPC_CheckPredicate
3021 ? MatcherTable[MatcherIndex++]
3023 return SDISel.CheckNodePredicate(Op, PredNo);
3024}
3025
3027CheckOpcode(const uint8_t *MatcherTable, unsigned &MatcherIndex, SDNode *N) {
3028 uint16_t Opc = MatcherTable[MatcherIndex++];
3029 Opc |= static_cast<uint16_t>(MatcherTable[MatcherIndex++]) << 8;
3030 return N->getOpcode() == Opc;
3031}
3032
3034 SDValue N,
3035 const TargetLowering *TLI,
3036 const DataLayout &DL) {
3037 if (N.getValueType() == VT)
3038 return true;
3039
3040 // Handle the case when VT is iPTR.
3041 return VT == MVT::iPTR && N.getValueType() == TLI->getPointerTy(DL);
3042}
3043
3046 const DataLayout &DL, unsigned ChildNo) {
3047 if (ChildNo >= N.getNumOperands())
3048 return false; // Match fails if out of range child #.
3049 return ::CheckType(VT, N.getOperand(ChildNo), TLI, DL);
3050}
3051
3053CheckCondCode(const uint8_t *MatcherTable, unsigned &MatcherIndex, SDValue N) {
3054 return cast<CondCodeSDNode>(N)->get() ==
3055 static_cast<ISD::CondCode>(MatcherTable[MatcherIndex++]);
3056}
3057
3059CheckChild2CondCode(const uint8_t *MatcherTable, unsigned &MatcherIndex,
3060 SDValue N) {
3061 if (2 >= N.getNumOperands())
3062 return false;
3063 return ::CheckCondCode(MatcherTable, MatcherIndex, N.getOperand(2));
3064}
3065
3067CheckValueType(const uint8_t *MatcherTable, unsigned &MatcherIndex, SDValue N,
3068 const TargetLowering *TLI, const DataLayout &DL) {
3069 MVT::SimpleValueType VT = getSimpleVT(MatcherTable, MatcherIndex);
3070 if (cast<VTSDNode>(N)->getVT() == VT)
3071 return true;
3072
3073 // Handle the case when VT is iPTR.
3074 return VT == MVT::iPTR && cast<VTSDNode>(N)->getVT() == TLI->getPointerTy(DL);
3075}
3076
3078CheckInteger(const uint8_t *MatcherTable, unsigned &MatcherIndex, SDValue N) {
3079 int64_t Val = GetSignedVBR(MatcherTable, MatcherIndex);
3080
3082 return C && C->getAPIntValue().trySExtValue() == Val;
3083}
3084
3086CheckChildInteger(const uint8_t *MatcherTable, unsigned &MatcherIndex,
3087 SDValue N, unsigned ChildNo) {
3088 if (ChildNo >= N.getNumOperands())
3089 return false; // Match fails if out of range child #.
3090 return ::CheckInteger(MatcherTable, MatcherIndex, N.getOperand(ChildNo));
3091}
3092
3094CheckAndImm(const uint8_t *MatcherTable, unsigned &MatcherIndex, SDValue N,
3095 const SelectionDAGISel &SDISel) {
3096 int64_t Val = MatcherTable[MatcherIndex++];
3097 if (Val & 128)
3098 Val = GetVBR(Val, MatcherTable, MatcherIndex);
3099
3100 if (N->getOpcode() != ISD::AND) return false;
3101
3102 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
3103 return C && SDISel.CheckAndMask(N.getOperand(0), C, Val);
3104}
3105
3107CheckOrImm(const uint8_t *MatcherTable, unsigned &MatcherIndex, SDValue N,
3108 const SelectionDAGISel &SDISel) {
3109 int64_t Val = MatcherTable[MatcherIndex++];
3110 if (Val & 128)
3111 Val = GetVBR(Val, MatcherTable, MatcherIndex);
3112
3113 if (N->getOpcode() != ISD::OR) return false;
3114
3115 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
3116 return C && SDISel.CheckOrMask(N.getOperand(0), C, Val);
3117}
3118
3119/// IsPredicateKnownToFail - If we know how and can do so without pushing a
3120/// scope, evaluate the current node. If the current predicate is known to
3121/// fail, set Result=true and return anything. If the current predicate is
3122/// known to pass, set Result=false and return the MatcherIndex to continue
3123/// with. If the current predicate is unknown, set Result=false and return the
3124/// MatcherIndex to continue with.
3126 const uint8_t *Table, unsigned Index, SDValue N, bool &Result,
3127 const SelectionDAGISel &SDISel,
3128 SmallVectorImpl<std::pair<SDValue, SDNode *>> &RecordedNodes) {
3129 unsigned Opcode = Table[Index++];
3130 switch (Opcode) {
3131 default:
3132 Result = false;
3133 return Index-1; // Could not evaluate this predicate.
3135 Result = !::CheckSame(Table, Index, N, RecordedNodes);
3136 return Index;
3141 Result = !::CheckChildSame(Table, Index, N, RecordedNodes,
3142 Table[Index-1] - SelectionDAGISel::OPC_CheckChild0Same);
3143 return Index;
3154 Result = !::CheckPatternPredicate(Opcode, Table, Index, SDISel);
3155 return Index;
3165 Result = !::CheckNodePredicate(Opcode, Table, Index, SDISel, N);
3166 return Index;
3168 Result = !::CheckOpcode(Table, Index, N.getNode());
3169 return Index;
3174 MVT VT;
3175 switch (Opcode) {
3177 VT = MVT::i32;
3178 break;
3180 VT = MVT::i64;
3181 break;
3183 VT = getHwModeVT(Table, Index, SDISel);
3184 break;
3185 default:
3186 VT = getSimpleVT(Table, Index);
3187 break;
3188 }
3189 Result = !::CheckType(VT.SimpleTy, N, SDISel.TLI,
3190 SDISel.CurDAG->getDataLayout());
3191 return Index;
3192 }
3195 unsigned Res = Table[Index++];
3197 ? getHwModeVT(Table, Index, SDISel)
3198 : getSimpleVT(Table, Index);
3199 Result = !::CheckType(VT.SimpleTy, N.getValue(Res), SDISel.TLI,
3200 SDISel.CurDAG->getDataLayout());
3201 return Index;
3202 }
3235 MVT VT;
3236 unsigned ChildNo;
3239 VT = MVT::i32;
3241 } else if (Opcode >= SelectionDAGISel::OPC_CheckChild0TypeI64 &&
3243 VT = MVT::i64;
3245 } else if (Opcode >= SelectionDAGISel::OPC_CheckChild0TypeByHwMode &&
3247 VT = getHwModeVT(Table, Index, SDISel);
3249 } else {
3250 VT = getSimpleVT(Table, Index);
3251 ChildNo = Opcode - SelectionDAGISel::OPC_CheckChild0Type;
3252 }
3253 Result = !::CheckChildType(VT.SimpleTy, N, SDISel.TLI,
3254 SDISel.CurDAG->getDataLayout(), ChildNo);
3255 return Index;
3256 }
3258 Result = !::CheckCondCode(Table, Index, N);
3259 return Index;
3261 Result = !::CheckChild2CondCode(Table, Index, N);
3262 return Index;
3264 Result = !::CheckValueType(Table, Index, N, SDISel.TLI,
3265 SDISel.CurDAG->getDataLayout());
3266 return Index;
3268 Result = !::CheckInteger(Table, Index, N);
3269 return Index;
3275 Result = !::CheckChildInteger(Table, Index, N,
3277 return Index;
3279 Result = !::CheckAndImm(Table, Index, N, SDISel);
3280 return Index;
3282 Result = !::CheckOrImm(Table, Index, N, SDISel);
3283 return Index;
3284 }
3285}
3286
3287namespace {
3288
3289struct MatchScope {
3290 /// FailIndex - If this match fails, this is the index to continue with.
3291 unsigned FailIndex;
3292
3293 /// NodeStack - The node stack when the scope was formed.
3294 SmallVector<SDValue, 4> NodeStack;
3295
3296 /// NumRecordedNodes - The number of recorded nodes when the scope was formed.
3297 unsigned NumRecordedNodes;
3298
3299 /// NumMatchedMemRefs - The number of matched memref entries.
3300 unsigned NumMatchedMemRefs;
3301
3302 /// InputChain/InputGlue - The current chain/glue
3303 SDValue InputChain, InputGlue;
3304
3305 /// HasChainNodesMatched - True if the ChainNodesMatched list is non-empty.
3306 bool HasChainNodesMatched;
3307};
3308
3309/// \A DAG update listener to keep the matching state
3310/// (i.e. RecordedNodes and MatchScope) uptodate if the target is allowed to
3311/// change the DAG while matching. X86 addressing mode matcher is an example
3312/// for this.
3313class MatchStateUpdater : public SelectionDAG::DAGUpdateListener
3314{
3315 SDNode **NodeToMatch;
3316 SmallVectorImpl<std::pair<SDValue, SDNode *>> &RecordedNodes;
3317 SmallVectorImpl<MatchScope> &MatchScopes;
3318
3319public:
3320 MatchStateUpdater(SelectionDAG &DAG, SDNode **NodeToMatch,
3321 SmallVectorImpl<std::pair<SDValue, SDNode *>> &RN,
3322 SmallVectorImpl<MatchScope> &MS)
3323 : SelectionDAG::DAGUpdateListener(DAG), NodeToMatch(NodeToMatch),
3324 RecordedNodes(RN), MatchScopes(MS) {}
3325
3326 void NodeDeleted(SDNode *N, SDNode *E) override {
3327 // Some early-returns here to avoid the search if we deleted the node or
3328 // if the update comes from MorphNodeTo (MorphNodeTo is the last thing we
3329 // do, so it's unnecessary to update matching state at that point).
3330 // Neither of these can occur currently because we only install this
3331 // update listener during matching a complex patterns.
3332 if (!E || E->isMachineOpcode())
3333 return;
3334 // Check if NodeToMatch was updated.
3335 if (N == *NodeToMatch)
3336 *NodeToMatch = E;
3337 // Performing linear search here does not matter because we almost never
3338 // run this code. You'd have to have a CSE during complex pattern
3339 // matching.
3340 for (auto &I : RecordedNodes)
3341 if (I.first.getNode() == N)
3342 I.first.setNode(E);
3343
3344 for (auto &I : MatchScopes)
3345 for (auto &J : I.NodeStack)
3346 if (J.getNode() == N)
3347 J.setNode(E);
3348 }
3349};
3350
3351} // end anonymous namespace
3352
3354 const uint8_t *MatcherTable,
3355 unsigned TableSize) {
3356 // FIXME: Should these even be selected? Handle these cases in the caller?
3357 switch (NodeToMatch->getOpcode()) {
3358 default:
3359 break;
3360 case ISD::EntryToken: // These nodes remain the same.
3361 case ISD::BasicBlock:
3362 case ISD::Register:
3363 case ISD::RegisterMask:
3364 case ISD::HANDLENODE:
3365 case ISD::MDNODE_SDNODE:
3371 case ISD::MCSymbol:
3376 case ISD::TokenFactor:
3377 case ISD::CopyFromReg:
3378 case ISD::CopyToReg:
3379 case ISD::EH_LABEL:
3382 case ISD::LIFETIME_END:
3383 case ISD::PSEUDO_PROBE:
3385 NodeToMatch->setNodeId(-1); // Mark selected.
3386 return;
3387 case ISD::AssertSext:
3388 case ISD::AssertZext:
3390 case ISD::AssertAlign:
3391 ReplaceUses(SDValue(NodeToMatch, 0), NodeToMatch->getOperand(0));
3392 CurDAG->RemoveDeadNode(NodeToMatch);
3393 return;
3394 case ISD::INLINEASM:
3395 case ISD::INLINEASM_BR:
3396 Select_INLINEASM(NodeToMatch);
3397 return;
3398 case ISD::READ_REGISTER:
3399 Select_READ_REGISTER(NodeToMatch);
3400 return;
3402 Select_WRITE_REGISTER(NodeToMatch);
3403 return;
3404 case ISD::POISON:
3405 case ISD::UNDEF:
3406 Select_UNDEF(NodeToMatch);
3407 return;
3408 case ISD::FAKE_USE:
3409 Select_FAKE_USE(NodeToMatch);
3410 return;
3411 case ISD::RELOC_NONE:
3412 Select_RELOC_NONE(NodeToMatch);
3413 return;
3414 case ISD::FREEZE:
3415 Select_FREEZE(NodeToMatch);
3416 return;
3417 case ISD::ARITH_FENCE:
3418 Select_ARITH_FENCE(NodeToMatch);
3419 return;
3420 case ISD::MEMBARRIER:
3421 Select_MEMBARRIER(NodeToMatch);
3422 return;
3423 case ISD::STACKMAP:
3424 Select_STACKMAP(NodeToMatch);
3425 return;
3426 case ISD::PATCHPOINT:
3427 Select_PATCHPOINT(NodeToMatch);
3428 return;
3430 Select_JUMP_TABLE_DEBUG_INFO(NodeToMatch);
3431 return;
3433 Select_CONVERGENCECTRL_ANCHOR(NodeToMatch);
3434 return;
3436 Select_CONVERGENCECTRL_ENTRY(NodeToMatch);
3437 return;
3439 Select_CONVERGENCECTRL_LOOP(NodeToMatch);
3440 return;
3441 }
3442
3443 assert(!NodeToMatch->isMachineOpcode() && "Node already selected!");
3444
3445 // Set up the node stack with NodeToMatch as the only node on the stack.
3446 SmallVector<SDValue, 8> NodeStack;
3447 SDValue N = SDValue(NodeToMatch, 0);
3448 NodeStack.push_back(N);
3449
3450 // MatchScopes - Scopes used when matching, if a match failure happens, this
3451 // indicates where to continue checking.
3452 SmallVector<MatchScope, 8> MatchScopes;
3453
3454 // RecordedNodes - This is the set of nodes that have been recorded by the
3455 // state machine. The second value is the parent of the node, or null if the
3456 // root is recorded.
3458
3459 // MatchedMemRefs - This is the set of MemRef's we've seen in the input
3460 // pattern.
3462
3463 // These are the current input chain and glue for use when generating nodes.
3464 // Various Emit operations change these. For example, emitting a copytoreg
3465 // uses and updates these.
3466 SDValue InputChain, InputGlue, DeactivationSymbol;
3467
3468 // ChainNodesMatched - If a pattern matches nodes that have input/output
3469 // chains, the OPC_EmitMergeInputChains operation is emitted which indicates
3470 // which ones they are. The result is captured into this list so that we can
3471 // update the chain results when the pattern is complete.
3472 SmallVector<SDNode*, 3> ChainNodesMatched;
3473
3474 LLVM_DEBUG(dbgs() << "ISEL: Starting pattern match\n");
3475
3476 // Determine where to start the interpreter. Normally we start at opcode #0,
3477 // but if the state machine starts with an OPC_SwitchOpcode, then we
3478 // accelerate the first lookup (which is guaranteed to be hot) with the
3479 // OpcodeOffset table.
3480 unsigned MatcherIndex = 0;
3481
3482 if (!OpcodeOffset.empty()) {
3483 // Already computed the OpcodeOffset table, just index into it.
3484 if (N.getOpcode() < OpcodeOffset.size())
3485 MatcherIndex = OpcodeOffset[N.getOpcode()];
3486 LLVM_DEBUG(dbgs() << " Initial Opcode index to " << MatcherIndex << "\n");
3487
3488 } else if (MatcherTable[0] == OPC_SwitchOpcode) {
3489 // Otherwise, the table isn't computed, but the state machine does start
3490 // with an OPC_SwitchOpcode instruction. Populate the table now, since this
3491 // is the first time we're selecting an instruction.
3492 unsigned Idx = 1;
3493 while (true) {
3494 // Get the size of this case.
3495 unsigned CaseSize = MatcherTable[Idx++];
3496 if (CaseSize & 128)
3497 CaseSize = GetVBR(CaseSize, MatcherTable, Idx);
3498 if (CaseSize == 0) break;
3499
3500 // Get the opcode, add the index to the table.
3501 uint16_t Opc = MatcherTable[Idx++];
3502 Opc |= static_cast<uint16_t>(MatcherTable[Idx++]) << 8;
3503 if (Opc >= OpcodeOffset.size())
3504 OpcodeOffset.resize((Opc+1)*2);
3505 OpcodeOffset[Opc] = Idx;
3506 Idx += CaseSize;
3507 }
3508
3509 // Okay, do the lookup for the first opcode.
3510 if (N.getOpcode() < OpcodeOffset.size())
3511 MatcherIndex = OpcodeOffset[N.getOpcode()];
3512 }
3513
3514 while (true) {
3515 assert(MatcherIndex < TableSize && "Invalid index");
3516#ifndef NDEBUG
3517 unsigned CurrentOpcodeIndex = MatcherIndex;
3518#endif
3519 BuiltinOpcodes Opcode =
3520 static_cast<BuiltinOpcodes>(MatcherTable[MatcherIndex++]);
3521 switch (Opcode) {
3522 case OPC_Scope: {
3523 // Okay, the semantics of this operation are that we should push a scope
3524 // then evaluate the first child. However, pushing a scope only to have
3525 // the first check fail (which then pops it) is inefficient. If we can
3526 // determine immediately that the first check (or first several) will
3527 // immediately fail, don't even bother pushing a scope for them.
3528 unsigned FailIndex;
3529
3530 while (true) {
3531 unsigned NumToSkip = MatcherTable[MatcherIndex++];
3532 if (NumToSkip & 128)
3533 NumToSkip = GetVBR(NumToSkip, MatcherTable, MatcherIndex);
3534 // Found the end of the scope with no match.
3535 if (NumToSkip == 0) {
3536 FailIndex = 0;
3537 break;
3538 }
3539
3540 FailIndex = MatcherIndex+NumToSkip;
3541
3542 unsigned MatcherIndexOfPredicate = MatcherIndex;
3543 (void)MatcherIndexOfPredicate; // silence warning.
3544
3545 // If we can't evaluate this predicate without pushing a scope (e.g. if
3546 // it is a 'MoveParent') or if the predicate succeeds on this node, we
3547 // push the scope and evaluate the full predicate chain.
3548 bool Result;
3549 MatcherIndex = IsPredicateKnownToFail(MatcherTable, MatcherIndex, N,
3550 Result, *this, RecordedNodes);
3551 if (!Result)
3552 break;
3553
3554 LLVM_DEBUG(
3555 dbgs() << " Skipped scope entry (due to false predicate) at "
3556 << "index " << MatcherIndexOfPredicate << ", continuing at "
3557 << FailIndex << "\n");
3558 ++NumDAGIselRetries;
3559
3560 // Otherwise, we know that this case of the Scope is guaranteed to fail,
3561 // move to the next case.
3562 MatcherIndex = FailIndex;
3563 }
3564
3565 // If the whole scope failed to match, bail.
3566 if (FailIndex == 0) break;
3567
3568 // Push a MatchScope which indicates where to go if the first child fails
3569 // to match.
3570 MatchScope NewEntry;
3571 NewEntry.FailIndex = FailIndex;
3572 NewEntry.NodeStack.append(NodeStack.begin(), NodeStack.end());
3573 NewEntry.NumRecordedNodes = RecordedNodes.size();
3574 NewEntry.NumMatchedMemRefs = MatchedMemRefs.size();
3575 NewEntry.InputChain = InputChain;
3576 NewEntry.InputGlue = InputGlue;
3577 NewEntry.HasChainNodesMatched = !ChainNodesMatched.empty();
3578 MatchScopes.push_back(NewEntry);
3579 continue;
3580 }
3581 case OPC_RecordNode: {
3582 // Remember this node, it may end up being an operand in the pattern.
3583 SDNode *Parent = nullptr;
3584 if (NodeStack.size() > 1)
3585 Parent = NodeStack[NodeStack.size()-2].getNode();
3586 RecordedNodes.emplace_back(N, Parent);
3587 continue;
3588 }
3589
3594 unsigned ChildNo = Opcode-OPC_RecordChild0;
3595 if (ChildNo >= N.getNumOperands())
3596 break; // Match fails if out of range child #.
3597
3598 RecordedNodes.emplace_back(N->getOperand(ChildNo), N.getNode());
3599 continue;
3600 }
3601 case OPC_RecordMemRef:
3602 if (auto *MN = dyn_cast<MemSDNode>(N))
3603 MatchedMemRefs.push_back(MN->getMemOperand());
3604 else {
3605 LLVM_DEBUG(dbgs() << "Expected MemSDNode "; N->dump(CurDAG);
3606 dbgs() << '\n');
3607 }
3608
3609 continue;
3610
3612 // If the current node has an input glue, capture it in InputGlue.
3613 if (N->getNumOperands() != 0 &&
3614 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue)
3615 InputGlue = N->getOperand(N->getNumOperands()-1);
3616 continue;
3617
3619 // If the current node has a deactivation symbol, capture it in
3620 // DeactivationSymbol.
3621 if (N->getNumOperands() != 0 &&
3622 N->getOperand(N->getNumOperands() - 1).getOpcode() ==
3624 DeactivationSymbol = N->getOperand(N->getNumOperands() - 1);
3625 continue;
3626
3627 case OPC_MoveChild: {
3628 unsigned ChildNo = MatcherTable[MatcherIndex++];
3629 if (ChildNo >= N.getNumOperands())
3630 break; // Match fails if out of range child #.
3631 N = N.getOperand(ChildNo);
3632 NodeStack.push_back(N);
3633 continue;
3634 }
3635
3636 case OPC_MoveChild0: case OPC_MoveChild1:
3637 case OPC_MoveChild2: case OPC_MoveChild3:
3638 case OPC_MoveChild4: case OPC_MoveChild5:
3639 case OPC_MoveChild6: case OPC_MoveChild7: {
3640 unsigned ChildNo = Opcode-OPC_MoveChild0;
3641 if (ChildNo >= N.getNumOperands())
3642 break; // Match fails if out of range child #.
3643 N = N.getOperand(ChildNo);
3644 NodeStack.push_back(N);
3645 continue;
3646 }
3647
3648 case OPC_MoveSibling:
3649 case OPC_MoveSibling0:
3650 case OPC_MoveSibling1:
3651 case OPC_MoveSibling2:
3652 case OPC_MoveSibling3:
3653 case OPC_MoveSibling4:
3654 case OPC_MoveSibling5:
3655 case OPC_MoveSibling6:
3656 case OPC_MoveSibling7: {
3657 // Pop the current node off the NodeStack.
3658 NodeStack.pop_back();
3659 assert(!NodeStack.empty() && "Node stack imbalance!");
3660 N = NodeStack.back();
3661
3662 unsigned SiblingNo = Opcode == OPC_MoveSibling
3663 ? MatcherTable[MatcherIndex++]
3664 : Opcode - OPC_MoveSibling0;
3665 if (SiblingNo >= N.getNumOperands())
3666 break; // Match fails if out of range sibling #.
3667 N = N.getOperand(SiblingNo);
3668 NodeStack.push_back(N);
3669 continue;
3670 }
3671 case OPC_MoveParent:
3672 // Pop the current node off the NodeStack.
3673 NodeStack.pop_back();
3674 assert(!NodeStack.empty() && "Node stack imbalance!");
3675 N = NodeStack.back();
3676 continue;
3677
3678 case OPC_CheckSame:
3679 if (!::CheckSame(MatcherTable, MatcherIndex, N, RecordedNodes)) break;
3680 continue;
3681
3684 if (!::CheckChildSame(MatcherTable, MatcherIndex, N, RecordedNodes,
3685 Opcode-OPC_CheckChild0Same))
3686 break;
3687 continue;
3688
3699 if (!::CheckPatternPredicate(Opcode, MatcherTable, MatcherIndex, *this))
3700 break;
3701 continue;
3710 case OPC_CheckPredicate:
3711 if (!::CheckNodePredicate(Opcode, MatcherTable, MatcherIndex, *this, N))
3712 break;
3713 continue;
3715 unsigned OpNum = MatcherTable[MatcherIndex++];
3716 SmallVector<SDValue, 8> Operands;
3717
3718 for (unsigned i = 0; i < OpNum; ++i)
3719 Operands.push_back(RecordedNodes[MatcherTable[MatcherIndex++]].first);
3720
3721 unsigned PredNo = MatcherTable[MatcherIndex++];
3722 if (!CheckNodePredicateWithOperands(N, PredNo, Operands))
3723 break;
3724 continue;
3725 }
3734 case OPC_CheckComplexPat7: {
3735 unsigned CPNum = Opcode == OPC_CheckComplexPat
3736 ? MatcherTable[MatcherIndex++]
3737 : Opcode - OPC_CheckComplexPat0;
3738 unsigned RecNo = MatcherTable[MatcherIndex++];
3739 assert(RecNo < RecordedNodes.size() && "Invalid CheckComplexPat");
3740
3741 // If target can modify DAG during matching, keep the matching state
3742 // consistent.
3743 std::unique_ptr<MatchStateUpdater> MSU;
3745 MSU.reset(new MatchStateUpdater(*CurDAG, &NodeToMatch, RecordedNodes,
3746 MatchScopes));
3747
3748 if (!CheckComplexPattern(NodeToMatch, RecordedNodes[RecNo].second,
3749 RecordedNodes[RecNo].first, CPNum,
3750 RecordedNodes))
3751 break;
3752 continue;
3753 }
3754 case OPC_CheckOpcode:
3755 if (!::CheckOpcode(MatcherTable, MatcherIndex, N.getNode())) break;
3756 continue;
3757
3758 case OPC_CheckType:
3759 case OPC_CheckTypeI32:
3760 case OPC_CheckTypeI64:
3761 case OPC_CheckTypeByHwMode: {
3762 MVT VT;
3763 switch (Opcode) {
3764 case OPC_CheckTypeI32:
3765 VT = MVT::i32;
3766 break;
3767 case OPC_CheckTypeI64:
3768 VT = MVT::i64;
3769 break;
3771 VT = getHwModeVT(MatcherTable, MatcherIndex, *this);
3772 break;
3773 default:
3774 VT = getSimpleVT(MatcherTable, MatcherIndex);
3775 break;
3776 }
3777 if (!::CheckType(VT.SimpleTy, N, TLI, CurDAG->getDataLayout()))
3778 break;
3779 continue;
3780 }
3781
3782 case OPC_CheckTypeRes:
3784 unsigned Res = MatcherTable[MatcherIndex++];
3785 MVT VT = Opcode == OPC_CheckTypeResByHwMode
3786 ? getHwModeVT(MatcherTable, MatcherIndex, *this)
3787 : getSimpleVT(MatcherTable, MatcherIndex);
3788 if (!::CheckType(VT.SimpleTy, N.getValue(Res), TLI,
3789 CurDAG->getDataLayout()))
3790 break;
3791 continue;
3792 }
3793
3794 case OPC_SwitchOpcode: {
3795 unsigned CurNodeOpcode = N.getOpcode();
3796 unsigned SwitchStart = MatcherIndex-1; (void)SwitchStart;
3797 unsigned CaseSize;
3798 while (true) {
3799 // Get the size of this case.
3800 CaseSize = MatcherTable[MatcherIndex++];
3801 if (CaseSize & 128)
3802 CaseSize = GetVBR(CaseSize, MatcherTable, MatcherIndex);
3803 if (CaseSize == 0) break;
3804
3805 uint16_t Opc = MatcherTable[MatcherIndex++];
3806 Opc |= static_cast<uint16_t>(MatcherTable[MatcherIndex++]) << 8;
3807
3808 // If the opcode matches, then we will execute this case.
3809 if (CurNodeOpcode == Opc)
3810 break;
3811
3812 // Otherwise, skip over this case.
3813 MatcherIndex += CaseSize;
3814 }
3815
3816 // If no cases matched, bail out.
3817 if (CaseSize == 0) break;
3818
3819 // Otherwise, execute the case we found.
3820 LLVM_DEBUG(dbgs() << " OpcodeSwitch from " << SwitchStart << " to "
3821 << MatcherIndex << "\n");
3822 continue;
3823 }
3824
3825 case OPC_SwitchType: {
3826 MVT CurNodeVT = N.getSimpleValueType();
3827 unsigned SwitchStart = MatcherIndex-1; (void)SwitchStart;
3828 unsigned CaseSize;
3829 while (true) {
3830 // Get the size of this case.
3831 CaseSize = MatcherTable[MatcherIndex++];
3832 if (CaseSize & 128)
3833 CaseSize = GetVBR(CaseSize, MatcherTable, MatcherIndex);
3834 if (CaseSize == 0) break;
3835
3836 MVT CaseVT = getSimpleVT(MatcherTable, MatcherIndex);
3837 if (CaseVT == MVT::iPTR)
3838 CaseVT = TLI->getPointerTy(CurDAG->getDataLayout());
3839
3840 // If the VT matches, then we will execute this case.
3841 if (CurNodeVT == CaseVT)
3842 break;
3843
3844 // Otherwise, skip over this case.
3845 MatcherIndex += CaseSize;
3846 }
3847
3848 // If no cases matched, bail out.
3849 if (CaseSize == 0) break;
3850
3851 // Otherwise, execute the case we found.
3852 LLVM_DEBUG(dbgs() << " TypeSwitch[" << CurNodeVT
3853 << "] from " << SwitchStart << " to " << MatcherIndex
3854 << '\n');
3855 continue;
3856 }
3882 unsigned ChildNo;
3885 VT = MVT::i32;
3887 } else if (Opcode >= SelectionDAGISel::OPC_CheckChild0TypeI64 &&
3889 VT = MVT::i64;
3891 } else {
3892 VT = getSimpleVT(MatcherTable, MatcherIndex);
3893 ChildNo = Opcode - SelectionDAGISel::OPC_CheckChild0Type;
3894 }
3895 if (!::CheckChildType(VT, N, TLI, CurDAG->getDataLayout(), ChildNo))
3896 break;
3897 continue;
3898 }
3907 MVT VT = getHwModeVT(MatcherTable, MatcherIndex, *this);
3908 unsigned ChildNo = Opcode - OPC_CheckChild0TypeByHwMode;
3909 if (!::CheckChildType(VT.SimpleTy, N, TLI, CurDAG->getDataLayout(),
3910 ChildNo))
3911 break;
3912 continue;
3913 }
3914 case OPC_CheckCondCode:
3915 if (!::CheckCondCode(MatcherTable, MatcherIndex, N)) break;
3916 continue;
3918 if (!::CheckChild2CondCode(MatcherTable, MatcherIndex, N)) break;
3919 continue;
3920 case OPC_CheckValueType:
3921 if (!::CheckValueType(MatcherTable, MatcherIndex, N, TLI,
3922 CurDAG->getDataLayout()))
3923 break;
3924 continue;
3925 case OPC_CheckInteger:
3926 if (!::CheckInteger(MatcherTable, MatcherIndex, N)) break;
3927 continue;
3931 if (!::CheckChildInteger(MatcherTable, MatcherIndex, N,
3932 Opcode-OPC_CheckChild0Integer)) break;
3933 continue;
3934 case OPC_CheckAndImm:
3935 if (!::CheckAndImm(MatcherTable, MatcherIndex, N, *this)) break;
3936 continue;
3937 case OPC_CheckOrImm:
3938 if (!::CheckOrImm(MatcherTable, MatcherIndex, N, *this)) break;
3939 continue;
3941 if (!ISD::isConstantSplatVectorAllOnes(N.getNode()))
3942 break;
3943 continue;
3945 if (!ISD::isConstantSplatVectorAllZeros(N.getNode()))
3946 break;
3947 continue;
3948
3950 assert(NodeStack.size() != 1 && "No parent node");
3951 // Verify that all intermediate nodes between the root and this one have
3952 // a single use (ignoring chains, which are handled in UpdateChains).
3953 bool HasMultipleUses = false;
3954 for (unsigned i = 1, e = NodeStack.size()-1; i != e; ++i) {
3955 unsigned NNonChainUses = 0;
3956 SDNode *NS = NodeStack[i].getNode();
3957 for (const SDUse &U : NS->uses())
3958 if (U.getValueType() != MVT::Other)
3959 if (++NNonChainUses > 1) {
3960 HasMultipleUses = true;
3961 break;
3962 }
3963 if (HasMultipleUses) break;
3964 }
3965 if (HasMultipleUses) break;
3966
3967 // Check to see that the target thinks this is profitable to fold and that
3968 // we can fold it without inducing cycles in the graph.
3969 if (!IsProfitableToFold(N, NodeStack[NodeStack.size()-2].getNode(),
3970 NodeToMatch) ||
3971 !IsLegalToFold(N, NodeStack[NodeStack.size()-2].getNode(),
3972 NodeToMatch, OptLevel,
3973 true/*We validate our own chains*/))
3974 break;
3975
3976 continue;
3977 }
3978 case OPC_EmitInteger:
3979 case OPC_EmitIntegerI8:
3980 case OPC_EmitIntegerI16:
3981 case OPC_EmitIntegerI32:
3982 case OPC_EmitIntegerI64:
3984 MVT VT;
3985 switch (Opcode) {
3986 case OPC_EmitIntegerI8:
3987 VT = MVT::i8;
3988 break;
3989 case OPC_EmitIntegerI16:
3990 VT = MVT::i16;
3991 break;
3992 case OPC_EmitIntegerI32:
3993 VT = MVT::i32;
3994 break;
3995 case OPC_EmitIntegerI64:
3996 VT = MVT::i64;
3997 break;
3999 VT = getHwModeVT(MatcherTable, MatcherIndex, *this);
4000 break;
4001 default:
4002 VT = getSimpleVT(MatcherTable, MatcherIndex);
4003 break;
4004 }
4005 int64_t Val = GetSignedVBR(MatcherTable, MatcherIndex);
4006 Val = SignExtend64(Val, MVT(VT).getFixedSizeInBits());
4007 RecordedNodes.emplace_back(
4008 CurDAG->getSignedConstant(Val, SDLoc(NodeToMatch), VT.SimpleTy,
4009 /*isTarget=*/true),
4010 nullptr);
4011 continue;
4012 }
4013
4014 case OPC_EmitRegister:
4018 MVT VT;
4019 switch (Opcode) {
4021 VT = MVT::i32;
4022 break;
4024 VT = MVT::i64;
4025 break;
4027 VT = getHwModeVT(MatcherTable, MatcherIndex, *this);
4028 break;
4029 default:
4030 VT = getSimpleVT(MatcherTable, MatcherIndex);
4031 break;
4032 }
4033 unsigned RegNo = MatcherTable[MatcherIndex++];
4034 RecordedNodes.emplace_back(CurDAG->getRegister(RegNo, VT), nullptr);
4035 continue;
4036 }
4037 case OPC_EmitRegister2:
4039 // For targets w/ more than 256 register names, the register enum
4040 // values are stored in two bytes in the matcher table (just like
4041 // opcodes).
4042 MVT VT = Opcode == OPC_EmitRegisterByHwMode2
4043 ? getHwModeVT(MatcherTable, MatcherIndex, *this)
4044 : getSimpleVT(MatcherTable, MatcherIndex);
4045 unsigned RegNo = MatcherTable[MatcherIndex++];
4046 RegNo |= MatcherTable[MatcherIndex++] << 8;
4047 RecordedNodes.emplace_back(CurDAG->getRegister(RegNo, VT), nullptr);
4048 continue;
4049 }
4050
4060 // Convert from IMM/FPIMM to target version.
4061 unsigned RecNo = Opcode == OPC_EmitConvertToTarget
4062 ? MatcherTable[MatcherIndex++]
4063 : Opcode - OPC_EmitConvertToTarget0;
4064 assert(RecNo < RecordedNodes.size() && "Invalid EmitConvertToTarget");
4065 SDValue Imm = RecordedNodes[RecNo].first;
4066
4067 if (Imm->getOpcode() == ISD::Constant) {
4068 const ConstantInt *Val=cast<ConstantSDNode>(Imm)->getConstantIntValue();
4069 Imm = CurDAG->getTargetConstant(*Val, SDLoc(NodeToMatch),
4070 Imm.getValueType());
4071 } else if (Imm->getOpcode() == ISD::ConstantFP) {
4072 const ConstantFP *Val=cast<ConstantFPSDNode>(Imm)->getConstantFPValue();
4073 Imm = CurDAG->getTargetConstantFP(*Val, SDLoc(NodeToMatch),
4074 Imm.getValueType());
4075 }
4076
4077 RecordedNodes.emplace_back(Imm, RecordedNodes[RecNo].second);
4078 continue;
4079 }
4080
4081 case OPC_EmitMergeInputChains1_0: // OPC_EmitMergeInputChains, 1, 0
4082 case OPC_EmitMergeInputChains1_1: // OPC_EmitMergeInputChains, 1, 1
4083 case OPC_EmitMergeInputChains1_2: { // OPC_EmitMergeInputChains, 1, 2
4084 // These are space-optimized forms of OPC_EmitMergeInputChains.
4085 assert(!InputChain.getNode() &&
4086 "EmitMergeInputChains should be the first chain producing node");
4087 assert(ChainNodesMatched.empty() &&
4088 "Should only have one EmitMergeInputChains per match");
4089
4090 // Read all of the chained nodes.
4091 unsigned RecNo = Opcode - OPC_EmitMergeInputChains1_0;
4092 assert(RecNo < RecordedNodes.size() && "Invalid EmitMergeInputChains");
4093 ChainNodesMatched.push_back(RecordedNodes[RecNo].first.getNode());
4094
4095 // If the chained node is not the root, we can't fold it if it has
4096 // multiple uses.
4097 // FIXME: What if other value results of the node have uses not matched
4098 // by this pattern?
4099 if (ChainNodesMatched.back() != NodeToMatch &&
4100 !RecordedNodes[RecNo].first.hasOneUse()) {
4101 ChainNodesMatched.clear();
4102 break;
4103 }
4104
4105 // Merge the input chains if they are not intra-pattern references.
4106 InputChain = HandleMergeInputChains(ChainNodesMatched, InputGlue, CurDAG);
4107
4108 if (!InputChain.getNode())
4109 break; // Failed to merge.
4110 continue;
4111 }
4112
4114 assert(!InputChain.getNode() &&
4115 "EmitMergeInputChains should be the first chain producing node");
4116 // This node gets a list of nodes we matched in the input that have
4117 // chains. We want to token factor all of the input chains to these nodes
4118 // together. However, if any of the input chains is actually one of the
4119 // nodes matched in this pattern, then we have an intra-match reference.
4120 // Ignore these because the newly token factored chain should not refer to
4121 // the old nodes.
4122 unsigned NumChains = MatcherTable[MatcherIndex++];
4123 assert(NumChains != 0 && "Can't TF zero chains");
4124
4125 assert(ChainNodesMatched.empty() &&
4126 "Should only have one EmitMergeInputChains per match");
4127
4128 // Read all of the chained nodes.
4129 for (unsigned i = 0; i != NumChains; ++i) {
4130 unsigned RecNo = MatcherTable[MatcherIndex++];
4131 assert(RecNo < RecordedNodes.size() && "Invalid EmitMergeInputChains");
4132 ChainNodesMatched.push_back(RecordedNodes[RecNo].first.getNode());
4133
4134 // If the chained node is not the root, we can't fold it if it has
4135 // multiple uses.
4136 // FIXME: What if other value results of the node have uses not matched
4137 // by this pattern?
4138 if (ChainNodesMatched.back() != NodeToMatch &&
4139 !RecordedNodes[RecNo].first.hasOneUse()) {
4140 ChainNodesMatched.clear();
4141 break;
4142 }
4143 }
4144
4145 // If the inner loop broke out, the match fails.
4146 if (ChainNodesMatched.empty())
4147 break;
4148
4149 // Merge the input chains if they are not intra-pattern references.
4150 InputChain = HandleMergeInputChains(ChainNodesMatched, InputGlue, CurDAG);
4151
4152 if (!InputChain.getNode())
4153 break; // Failed to merge.
4154
4155 continue;
4156 }
4157
4158 case OPC_EmitCopyToReg:
4159 case OPC_EmitCopyToReg0:
4160 case OPC_EmitCopyToReg1:
4161 case OPC_EmitCopyToReg2:
4162 case OPC_EmitCopyToReg3:
4163 case OPC_EmitCopyToReg4:
4164 case OPC_EmitCopyToReg5:
4165 case OPC_EmitCopyToReg6:
4166 case OPC_EmitCopyToReg7:
4168 unsigned RecNo =
4169 Opcode >= OPC_EmitCopyToReg0 && Opcode <= OPC_EmitCopyToReg7
4170 ? Opcode - OPC_EmitCopyToReg0
4171 : MatcherTable[MatcherIndex++];
4172 assert(RecNo < RecordedNodes.size() && "Invalid EmitCopyToReg");
4173 unsigned DestPhysReg = MatcherTable[MatcherIndex++];
4174 if (Opcode == OPC_EmitCopyToRegTwoByte)
4175 DestPhysReg |= MatcherTable[MatcherIndex++] << 8;
4176
4177 if (!InputChain.getNode())
4178 InputChain = CurDAG->getEntryNode();
4179
4180 InputChain = CurDAG->getCopyToReg(InputChain, SDLoc(NodeToMatch),
4181 DestPhysReg, RecordedNodes[RecNo].first,
4182 InputGlue);
4183
4184 InputGlue = InputChain.getValue(1);
4185 continue;
4186 }
4187
4188 case OPC_EmitNodeXForm: {
4189 unsigned XFormNo = MatcherTable[MatcherIndex++];
4190 unsigned RecNo = MatcherTable[MatcherIndex++];
4191 assert(RecNo < RecordedNodes.size() && "Invalid EmitNodeXForm");
4192 SDValue Res = RunSDNodeXForm(RecordedNodes[RecNo].first, XFormNo);
4193 RecordedNodes.emplace_back(Res, nullptr);
4194 continue;
4195 }
4196 case OPC_Coverage: {
4197 // This is emitted right before MorphNode/EmitNode.
4198 // So it should be safe to assume that this node has been selected
4199 unsigned index = MatcherTable[MatcherIndex++];
4200 index |= (MatcherTable[MatcherIndex++] << 8);
4201 index |= (MatcherTable[MatcherIndex++] << 16);
4202 index |= (MatcherTable[MatcherIndex++] << 24);
4203 dbgs() << "COVERED: " << getPatternForIndex(index) << "\n";
4204 dbgs() << "INCLUDED: " << getIncludePathForIndex(index) << "\n";
4205 continue;
4206 }
4207
4208 case OPC_EmitNode:
4210 case OPC_EmitNode0:
4211 case OPC_EmitNode1:
4212 case OPC_EmitNode2:
4213 case OPC_EmitNode1None:
4214 case OPC_EmitNode2None:
4215 case OPC_EmitNode0Chain:
4216 case OPC_EmitNode1Chain:
4217 case OPC_EmitNode2Chain:
4218 case OPC_MorphNodeTo:
4220 case OPC_MorphNodeTo0:
4221 case OPC_MorphNodeTo1:
4222 case OPC_MorphNodeTo2:
4232 uint16_t TargetOpc = MatcherTable[MatcherIndex++];
4233 TargetOpc |= static_cast<uint16_t>(MatcherTable[MatcherIndex++]) << 8;
4234 unsigned EmitNodeInfo;
4235 if (Opcode >= OPC_EmitNode1None && Opcode <= OPC_EmitNode2Chain) {
4236 if (Opcode >= OPC_EmitNode0Chain && Opcode <= OPC_EmitNode2Chain)
4237 EmitNodeInfo = OPFL_Chain;
4238 else
4239 EmitNodeInfo = OPFL_None;
4240 } else if (Opcode >= OPC_MorphNodeTo1None &&
4241 Opcode <= OPC_MorphNodeTo2GlueOutput) {
4242 if (Opcode >= OPC_MorphNodeTo0Chain && Opcode <= OPC_MorphNodeTo2Chain)
4243 EmitNodeInfo = OPFL_Chain;
4244 else if (Opcode >= OPC_MorphNodeTo1GlueInput &&
4245 Opcode <= OPC_MorphNodeTo2GlueInput)
4246 EmitNodeInfo = OPFL_GlueInput;
4247 else if (Opcode >= OPC_MorphNodeTo1GlueOutput &&
4249 EmitNodeInfo = OPFL_GlueOutput;
4250 else
4251 EmitNodeInfo = OPFL_None;
4252 } else
4253 EmitNodeInfo = MatcherTable[MatcherIndex++];
4254 // Get the result VT list.
4255 unsigned NumVTs;
4256 // If this is one of the compressed forms, get the number of VTs based
4257 // on the Opcode. Otherwise read the next byte from the table.
4258 if (Opcode >= OPC_MorphNodeTo0 && Opcode <= OPC_MorphNodeTo2)
4259 NumVTs = Opcode - OPC_MorphNodeTo0;
4260 else if (Opcode >= OPC_MorphNodeTo1None && Opcode <= OPC_MorphNodeTo2None)
4261 NumVTs = Opcode - OPC_MorphNodeTo1None + 1;
4262 else if (Opcode >= OPC_MorphNodeTo0Chain &&
4263 Opcode <= OPC_MorphNodeTo2Chain)
4264 NumVTs = Opcode - OPC_MorphNodeTo0Chain;
4265 else if (Opcode >= OPC_MorphNodeTo1GlueInput &&
4266 Opcode <= OPC_MorphNodeTo2GlueInput)
4267 NumVTs = Opcode - OPC_MorphNodeTo1GlueInput + 1;
4268 else if (Opcode >= OPC_MorphNodeTo1GlueOutput &&
4270 NumVTs = Opcode - OPC_MorphNodeTo1GlueOutput + 1;
4271 else if (Opcode >= OPC_EmitNode0 && Opcode <= OPC_EmitNode2)
4272 NumVTs = Opcode - OPC_EmitNode0;
4273 else if (Opcode >= OPC_EmitNode1None && Opcode <= OPC_EmitNode2None)
4274 NumVTs = Opcode - OPC_EmitNode1None + 1;
4275 else if (Opcode >= OPC_EmitNode0Chain && Opcode <= OPC_EmitNode2Chain)
4276 NumVTs = Opcode - OPC_EmitNode0Chain;
4277 else
4278 NumVTs = MatcherTable[MatcherIndex++];
4280 if (Opcode == OPC_EmitNodeByHwMode || Opcode == OPC_MorphNodeToByHwMode) {
4281 for (unsigned i = 0; i != NumVTs; ++i) {
4282 MVT VT = getHwModeVT(MatcherTable, MatcherIndex, *this);
4283 if (VT == MVT::iPTR)
4284 VT = TLI->getPointerTy(CurDAG->getDataLayout());
4285 VTs.push_back(VT);
4286 }
4287 } else {
4288 for (unsigned i = 0; i != NumVTs; ++i) {
4289 MVT::SimpleValueType VT = getSimpleVT(MatcherTable, MatcherIndex);
4290 if (VT == MVT::iPTR)
4291 VT = TLI->getPointerTy(CurDAG->getDataLayout()).SimpleTy;
4292 VTs.push_back(VT);
4293 }
4294 }
4295
4296 if (EmitNodeInfo & OPFL_Chain)
4297 VTs.push_back(MVT::Other);
4298 if (EmitNodeInfo & OPFL_GlueOutput)
4299 VTs.push_back(MVT::Glue);
4300
4301 // This is hot code, so optimize the two most common cases of 1 and 2
4302 // results.
4303 SDVTList VTList;
4304 if (VTs.size() == 1)
4305 VTList = CurDAG->getVTList(VTs[0]);
4306 else if (VTs.size() == 2)
4307 VTList = CurDAG->getVTList(VTs[0], VTs[1]);
4308 else
4309 VTList = CurDAG->getVTList(VTs);
4310
4311 // Get the operand list.
4312 unsigned NumOps = MatcherTable[MatcherIndex++];
4314 for (unsigned i = 0; i != NumOps; ++i) {
4315 unsigned RecNo = MatcherTable[MatcherIndex++];
4316 if (RecNo & 128)
4317 RecNo = GetVBR(RecNo, MatcherTable, MatcherIndex);
4318
4319 assert(RecNo < RecordedNodes.size() && "Invalid EmitNode");
4320 Ops.push_back(RecordedNodes[RecNo].first);
4321 }
4322
4323 // If there are variadic operands to add, handle them now.
4324 if (EmitNodeInfo & OPFL_VariadicInfo) {
4325 // Determine the start index to copy from.
4326 unsigned FirstOpToCopy = getNumFixedFromVariadicInfo(EmitNodeInfo);
4327 FirstOpToCopy += (EmitNodeInfo & OPFL_Chain) ? 1 : 0;
4328 assert(NodeToMatch->getNumOperands() >= FirstOpToCopy &&
4329 "Invalid variadic node");
4330 // Copy all of the variadic operands, not including a potential glue
4331 // input.
4332 for (unsigned i = FirstOpToCopy, e = NodeToMatch->getNumOperands();
4333 i != e; ++i) {
4334 SDValue V = NodeToMatch->getOperand(i);
4335 if (V.getValueType() == MVT::Glue) break;
4336 Ops.push_back(V);
4337 }
4338 }
4339
4340 // If this has chain/glue inputs, add them.
4341 if (EmitNodeInfo & OPFL_Chain)
4342 Ops.push_back(InputChain);
4343 if (DeactivationSymbol.getNode() != nullptr)
4344 Ops.push_back(DeactivationSymbol);
4345 if ((EmitNodeInfo & OPFL_GlueInput) && InputGlue.getNode() != nullptr)
4346 Ops.push_back(InputGlue);
4347
4348 // Check whether any matched node could raise an FP exception. Since all
4349 // such nodes must have a chain, it suffices to check ChainNodesMatched.
4350 // We need to perform this check before potentially modifying one of the
4351 // nodes via MorphNode.
4352 bool MayRaiseFPException =
4353 llvm::any_of(ChainNodesMatched, [this](SDNode *N) {
4354 return mayRaiseFPException(N) && !N->getFlags().hasNoFPExcept();
4355 });
4356
4357 // Create the node.
4358 MachineSDNode *Res = nullptr;
4359 bool IsMorphNodeTo =
4360 Opcode == OPC_MorphNodeTo || Opcode == OPC_MorphNodeToByHwMode ||
4361 (Opcode >= OPC_MorphNodeTo0 && Opcode <= OPC_MorphNodeTo2GlueOutput);
4362 if (!IsMorphNodeTo) {
4363 // If this is a normal EmitNode command, just create the new node and
4364 // add the results to the RecordedNodes list.
4365 Res = CurDAG->getMachineNode(TargetOpc, SDLoc(NodeToMatch),
4366 VTList, Ops);
4367
4368 // Add all the non-glue/non-chain results to the RecordedNodes list.
4369 for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
4370 if (VTs[i] == MVT::Other || VTs[i] == MVT::Glue) break;
4371 RecordedNodes.emplace_back(SDValue(Res, i), nullptr);
4372 }
4373 } else {
4374 assert(NodeToMatch->getOpcode() != ISD::DELETED_NODE &&
4375 "NodeToMatch was removed partway through selection");
4377 SDNode *E) {
4378 CurDAG->salvageDebugInfo(*N);
4379 auto &Chain = ChainNodesMatched;
4380 assert((!E || !is_contained(Chain, N)) &&
4381 "Chain node replaced during MorphNode");
4382 llvm::erase(Chain, N);
4383 });
4384 Res = cast<MachineSDNode>(MorphNode(NodeToMatch, TargetOpc, VTList,
4385 Ops, EmitNodeInfo));
4386 }
4387
4388 // Set the NoFPExcept flag when no original matched node could
4389 // raise an FP exception, but the new node potentially might.
4390 if (!MayRaiseFPException && mayRaiseFPException(Res))
4391 Res->setFlags(Res->getFlags() | SDNodeFlags::NoFPExcept);
4392
4393 // If the node had chain/glue results, update our notion of the current
4394 // chain and glue.
4395 if (EmitNodeInfo & OPFL_GlueOutput) {
4396 InputGlue = SDValue(Res, VTs.size()-1);
4397 if (EmitNodeInfo & OPFL_Chain)
4398 InputChain = SDValue(Res, VTs.size()-2);
4399 } else if (EmitNodeInfo & OPFL_Chain)
4400 InputChain = SDValue(Res, VTs.size()-1);
4401
4402 // If the OPFL_MemRefs glue is set on this node, slap all of the
4403 // accumulated memrefs onto it.
4404 //
4405 // FIXME: This is vastly incorrect for patterns with multiple outputs
4406 // instructions that access memory and for ComplexPatterns that match
4407 // loads.
4408 if (EmitNodeInfo & OPFL_MemRefs) {
4409 // Only attach load or store memory operands if the generated
4410 // instruction may load or store.
4411 const MCInstrDesc &MCID = TII->get(TargetOpc);
4412 bool mayLoad = MCID.mayLoad();
4413 bool mayStore = MCID.mayStore();
4414
4415 // We expect to have relatively few of these so just filter them into a
4416 // temporary buffer so that we can easily add them to the instruction.
4418 for (MachineMemOperand *MMO : MatchedMemRefs) {
4419 if (MMO->isLoad()) {
4420 if (mayLoad)
4421 FilteredMemRefs.push_back(MMO);
4422 } else if (MMO->isStore()) {
4423 if (mayStore)
4424 FilteredMemRefs.push_back(MMO);
4425 } else {
4426 FilteredMemRefs.push_back(MMO);
4427 }
4428 }
4429
4430 CurDAG->setNodeMemRefs(Res, FilteredMemRefs);
4431 }
4432
4433 LLVM_DEBUG({
4434 if (!MatchedMemRefs.empty() && Res->memoperands_empty())
4435 dbgs() << " Dropping mem operands\n";
4436 dbgs() << " " << (IsMorphNodeTo ? "Morphed" : "Created") << " node: ";
4437 Res->dump(CurDAG);
4438 });
4439
4440 // If this was a MorphNodeTo then we're completely done!
4441 if (IsMorphNodeTo) {
4442 // Update chain uses.
4443 UpdateChains(Res, InputChain, ChainNodesMatched, true);
4444 return;
4445 }
4446 continue;
4447 }
4448
4449 case OPC_CompleteMatch: {
4450 // The match has been completed, and any new nodes (if any) have been
4451 // created. Patch up references to the matched dag to use the newly
4452 // created nodes.
4453 unsigned NumResults = MatcherTable[MatcherIndex++];
4454
4455 for (unsigned i = 0; i != NumResults; ++i) {
4456 unsigned ResSlot = MatcherTable[MatcherIndex++];
4457 if (ResSlot & 128)
4458 ResSlot = GetVBR(ResSlot, MatcherTable, MatcherIndex);
4459
4460 assert(ResSlot < RecordedNodes.size() && "Invalid CompleteMatch");
4461 SDValue Res = RecordedNodes[ResSlot].first;
4462
4463 assert(i < NodeToMatch->getNumValues() &&
4464 NodeToMatch->getValueType(i) != MVT::Other &&
4465 NodeToMatch->getValueType(i) != MVT::Glue &&
4466 "Invalid number of results to complete!");
4467 assert((NodeToMatch->getValueType(i) == Res.getValueType() ||
4468 NodeToMatch->getValueType(i) == MVT::iPTR ||
4469 Res.getValueType() == MVT::iPTR ||
4470 NodeToMatch->getValueType(i).getSizeInBits() ==
4471 Res.getValueSizeInBits()) &&
4472 "invalid replacement");
4473 ReplaceUses(SDValue(NodeToMatch, i), Res);
4474 }
4475
4476 // Update chain uses.
4477 UpdateChains(NodeToMatch, InputChain, ChainNodesMatched, false);
4478
4479 // If the root node defines glue, we need to update it to the glue result.
4480 // TODO: This never happens in our tests and I think it can be removed /
4481 // replaced with an assert, but if we do it this the way the change is
4482 // NFC.
4483 if (NodeToMatch->getValueType(NodeToMatch->getNumValues() - 1) ==
4484 MVT::Glue &&
4485 InputGlue.getNode())
4486 ReplaceUses(SDValue(NodeToMatch, NodeToMatch->getNumValues() - 1),
4487 InputGlue);
4488
4489 assert(NodeToMatch->use_empty() &&
4490 "Didn't replace all uses of the node?");
4491 CurDAG->RemoveDeadNode(NodeToMatch);
4492
4493 return;
4494 }
4495 }
4496
4497 // If the code reached this point, then the match failed. See if there is
4498 // another child to try in the current 'Scope', otherwise pop it until we
4499 // find a case to check.
4500 LLVM_DEBUG(dbgs() << " Match failed at index " << CurrentOpcodeIndex
4501 << "\n");
4502 ++NumDAGIselRetries;
4503 while (true) {
4504 if (MatchScopes.empty()) {
4505 CannotYetSelect(NodeToMatch);
4506 return;
4507 }
4508
4509 // Restore the interpreter state back to the point where the scope was
4510 // formed.
4511 MatchScope &LastScope = MatchScopes.back();
4512 RecordedNodes.resize(LastScope.NumRecordedNodes);
4513 NodeStack.assign(LastScope.NodeStack.begin(), LastScope.NodeStack.end());
4514 N = NodeStack.back();
4515
4516 if (LastScope.NumMatchedMemRefs != MatchedMemRefs.size())
4517 MatchedMemRefs.resize(LastScope.NumMatchedMemRefs);
4518 MatcherIndex = LastScope.FailIndex;
4519
4520 LLVM_DEBUG(dbgs() << " Continuing at " << MatcherIndex << "\n");
4521
4522 InputChain = LastScope.InputChain;
4523 InputGlue = LastScope.InputGlue;
4524 if (!LastScope.HasChainNodesMatched)
4525 ChainNodesMatched.clear();
4526
4527 // Check to see what the offset is at the new MatcherIndex. If it is zero
4528 // we have reached the end of this scope, otherwise we have another child
4529 // in the current scope to try.
4530 unsigned NumToSkip = MatcherTable[MatcherIndex++];
4531 if (NumToSkip & 128)
4532 NumToSkip = GetVBR(NumToSkip, MatcherTable, MatcherIndex);
4533
4534 // If we have another child in this scope to match, update FailIndex and
4535 // try it.
4536 if (NumToSkip != 0) {
4537 LastScope.FailIndex = MatcherIndex+NumToSkip;
4538 break;
4539 }
4540
4541 // End of this scope, pop it and try the next child in the containing
4542 // scope.
4543 MatchScopes.pop_back();
4544 }
4545 }
4546}
4547
4548/// Return whether the node may raise an FP exception.
4550 // For machine opcodes, consult the MCID flag.
4551 if (N->isMachineOpcode()) {
4552 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
4553 return MCID.mayRaiseFPException();
4554 }
4555
4556 // For ISD opcodes, only StrictFP opcodes may raise an FP
4557 // exception.
4558 if (N->isTargetOpcode()) {
4559 const SelectionDAGTargetInfo &TSI = CurDAG->getSelectionDAGInfo();
4560 return TSI.mayRaiseFPException(N->getOpcode());
4561 }
4562 return N->isStrictFPOpcode();
4563}
4564
4566 assert(N->getOpcode() == ISD::OR && "Unexpected opcode");
4567 auto *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4568 if (!C)
4569 return false;
4570
4571 // Detect when "or" is used to add an offset to a stack object.
4572 if (auto *FN = dyn_cast<FrameIndexSDNode>(N->getOperand(0))) {
4573 MachineFrameInfo &MFI = MF->getFrameInfo();
4574 Align A = MFI.getObjectAlign(FN->getIndex());
4575 int32_t Off = C->getSExtValue();
4576 // If the alleged offset fits in the zero bits guaranteed by
4577 // the alignment, then this or is really an add.
4578 return (Off >= 0) && (((A.value() - 1) & Off) == unsigned(Off));
4579 }
4580 return false;
4581}
4582
4583void SelectionDAGISel::CannotYetSelect(SDNode *N) {
4584 std::string msg;
4585 raw_string_ostream Msg(msg);
4586 Msg << "Cannot select: ";
4587
4588 Msg.enable_colors(errs().has_colors());
4589
4590 if (N->getOpcode() != ISD::INTRINSIC_W_CHAIN &&
4591 N->getOpcode() != ISD::INTRINSIC_WO_CHAIN &&
4592 N->getOpcode() != ISD::INTRINSIC_VOID) {
4593 N->printrFull(Msg, CurDAG);
4594 Msg << "\nIn function: " << MF->getName();
4595 } else {
4596 bool HasInputChain = N->getOperand(0).getValueType() == MVT::Other;
4597 unsigned iid = N->getConstantOperandVal(HasInputChain);
4598 if (iid < Intrinsic::num_intrinsics)
4599 Msg << "intrinsic %" << Intrinsic::getBaseName((Intrinsic::ID)iid);
4600 else
4601 Msg << "unknown intrinsic #" << iid;
4602 }
4603 report_fatal_error(Twine(msg));
4604}
unsigned const MachineRegisterInfo * MRI
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
MachineInstrBuilder & UseMI
return SDValue()
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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
BitTracker BT
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:356
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
This file defines the FastISel class.
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static LVOptions Options
Definition LVOptions.cpp:25
#define I(x, y, z)
Definition MD5.cpp:57
PostRA Machine Instruction Scheduler
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
This file contains the declarations for metadata subclasses.
#define T
uint64_t IntrinsicInst * II
#define P(N)
FunctionAnalysisManager FAM
if(PassOpts->AAPipeline)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
static Type * getValueType(Value *V)
Returns the type of the given value/instruction V.
This file contains some templates that are useful if you are working with the STL at all.
static LLVM_ATTRIBUTE_ALWAYS_INLINE bool CheckInteger(const uint8_t *MatcherTable, unsigned &MatcherIndex, SDValue N)
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 uint8_t *MatcherTable, unsigned &MatcherIndex, const SelectionDAGISel &SDISel)
CheckPatternPredicate - Implements OP_CheckPatternPredicate.
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 cl::opt< bool > DumpSortedDAG("dump-sorted-dags", cl::Hidden, cl::desc("Print DAGs with sorted nodes in debug dump"), cl::init(false))
static LLVM_ATTRIBUTE_ALWAYS_INLINE bool CheckChild2CondCode(const uint8_t *MatcherTable, unsigned &MatcherIndex, SDValue N)
static void reportFastISelFailure(MachineFunction &MF, OptimizationRemarkEmitter &ORE, OptimizationRemarkMissed &R, bool ShouldAbort)
static LLVM_ATTRIBUTE_ALWAYS_INLINE bool CheckCondCode(const uint8_t *MatcherTable, unsigned &MatcherIndex, SDValue N)
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 LLVM_ATTRIBUTE_ALWAYS_INLINE MVT getHwModeVT(const uint8_t *MatcherTable, unsigned &MatcherIndex, const SelectionDAGISel &SDISel)
Decode a HwMode VT in MatcherTable by calling getValueTypeForHwMode.
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 LLVM_ATTRIBUTE_ALWAYS_INLINE bool CheckValueType(const uint8_t *MatcherTable, unsigned &MatcherIndex, SDValue N, const TargetLowering *TLI, const DataLayout &DL)
static void processSingleLocVars(FunctionLoweringInfo &FuncInfo, FunctionVarLocs const *FnVarLocs)
Collect single location variable information generated with assignment tracking.
static LLVM_ATTRIBUTE_ALWAYS_INLINE bool CheckChildInteger(const uint8_t *MatcherTable, unsigned &MatcherIndex, SDValue N, unsigned ChildNo)
static LLVM_ATTRIBUTE_ALWAYS_INLINE bool CheckAndImm(const uint8_t *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 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 uint64_t GetVBR(uint64_t Val, const uint8_t *MatcherTable, unsigned &Idx)
GetVBR - decode a vbr encoding whose top bit is set.
static void preserveFakeUses(BasicBlock::iterator Begin, BasicBlock::iterator End)
static SDValue HandleMergeInputChains(const SmallVectorImpl< SDNode * > &ChainNodesMatched, SDValue InputGlue, SelectionDAG *CurDAG)
HandleMergeInputChains - This implements the OPC_EmitMergeInputChains operation for when the pattern ...
static LLVM_ATTRIBUTE_ALWAYS_INLINE int64_t GetSignedVBR(const unsigned char *MatcherTable, unsigned &Idx)
static LLVM_ATTRIBUTE_ALWAYS_INLINE bool CheckSame(const uint8_t *MatcherTable, unsigned &MatcherIndex, SDValue N, const SmallVectorImpl< std::pair< SDValue, SDNode * > > &RecordedNodes)
CheckSame - Implements OP_CheckSame.
static LLVM_ATTRIBUTE_ALWAYS_INLINE bool CheckType(MVT::SimpleValueType VT, SDValue N, const TargetLowering *TLI, const DataLayout &DL)
static bool hasExceptionPointerOrCodeUser(const CatchPadInst *CPI)
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 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 LLVM_ATTRIBUTE_ALWAYS_INLINE bool CheckNodePredicate(unsigned Opcode, const uint8_t *MatcherTable, unsigned &MatcherIndex, const SelectionDAGISel &SDISel, SDValue Op)
CheckNodePredicate - Implements OP_CheckNodePredicate.
static LLVM_ATTRIBUTE_ALWAYS_INLINE bool CheckOrImm(const uint8_t *MatcherTable, unsigned &MatcherIndex, SDValue N, const SelectionDAGISel &SDISel)
static bool maintainPGOProfile(const TargetMachine &TM, CodeGenOptLevel OptLevel)
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 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 LLVM_ATTRIBUTE_ALWAYS_INLINE bool CheckChildSame(const uint8_t *MatcherTable, unsigned &MatcherIndex, SDValue N, const SmallVectorImpl< std::pair< SDValue, SDNode * > > &RecordedNodes, unsigned ChildNo)
CheckChildSame - Implements OP_CheckChildXSame.
static unsigned IsPredicateKnownToFail(const uint8_t *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 LLVM_ATTRIBUTE_ALWAYS_INLINE MVT::SimpleValueType getSimpleVT(const uint8_t *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 CheckOpcode(const uint8_t *MatcherTable, unsigned &MatcherIndex, SDNode *N)
static bool isFoldedOrDeadInstruction(const Instruction *I, const FunctionLoweringInfo &FuncInfo)
isFoldedOrDeadInstruction - Return true if the specified instruction is side-effect free and is eithe...
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
This file describes how to lower LLVM code to machine code.
This pass exposes codegen information to IR-level passes.
LLVM IR instance of the generic uniformity analysis.
A manager for alias analyses.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Class for arbitrary precision integers.
Definition APInt.h:78
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1258
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
A function analysis which provides an AssumptionCache.
An immutable pass that tracks lazily created AssumptionCache objects.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:483
unsigned getNumber() const
Definition BasicBlock.h:95
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition BasicBlock.h:539
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
bool isEHPad() const
Return true if this basic block is an exception handling block.
Definition BasicBlock.h:718
LLVM_ABI const Instruction * getFirstMayFaultInst() const
Returns the first potential AsynchEH faulty instruction currently it checks for loads/stores (which m...
Analysis pass which computes BlockFrequencyInfo.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Analysis pass which computes BranchProbabilityInfo.
Legacy analysis pass which computes BranchProbabilityInfo.
This class represents a function call, abstracting a target machine's calling convention.
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:282
This is the shared class of boolean and integer constants.
Definition Constants.h:87
DWARF expression.
LLVM_ABI bool isEntryValue() const
Check if the expression consists of exactly one entry value operand.
static LLVM_ABI DIExpression * append(const DIExpression *Expr, ArrayRef< uint64_t > Ops)
Append the opcodes Ops to DIExpr.
static LLVM_ABI DIExpression * prepend(const DIExpression *Expr, uint8_t Flags, int64_t Offset=0)
Prepend DIExpr with a deref and offset operation and optionally turn it into a stack value or/and an ...
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Record of a variable value-assignment, aka a non instruction representation of the dbg....
A debug info location.
Definition DebugLoc.h:123
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:74
iterator end()
Definition DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:241
Diagnostic information for ISel fallback path.
void setLastLocalValue(MachineInstr *I)
Update the position of the last instruction emitted for materializing constants for use in the curren...
Definition FastISel.h:238
void handleDbgInfo(const Instruction *II)
Target-independent lowering of non-instruction debug info associated with this instruction.
bool tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst)
We're checking to see if we can fold LI into FoldInst.
void removeDeadCode(MachineBasicBlock::iterator I, MachineBasicBlock::iterator E)
Remove all dead instructions between the I and E.
Definition FastISel.cpp:410
void startNewBlock()
Set the current block to which generated machine instructions will be appended.
Definition FastISel.cpp:123
bool selectInstruction(const Instruction *I)
Do "fast" instruction selection for the given LLVM IR instruction and append the generated machine in...
void finishBasicBlock()
Flush the local value map.
Definition FastISel.cpp:136
void recomputeInsertPt()
Reset InsertPt to prepare for inserting instructions into the current block.
Definition FastISel.cpp:401
bool lowerArguments()
Do "fast" instruction selection for function arguments and append the machine instructions to the cur...
Definition FastISel.cpp:138
unsigned arg_size() const
arg_size - Return the number of funcletpad arguments.
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th funcletpad argument.
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
SmallPtrSet< const DbgVariableRecord *, 8 > PreprocessedDVRDeclares
Collection of dbg_declare instructions handled after argument lowering and before ISel proper.
DenseMap< const AllocaInst *, int > StaticAllocaMap
StaticAllocaMap - Keep track of frame indices for fixed sized allocas in the entry block.
int getArgumentFrameIndex(const Argument *A)
getArgumentFrameIndex - Get frame index for the byval argument.
bool isExportedInst(const Value *V) const
isExportedInst - Return true if the specified value is an instruction exported from its block.
DenseMap< const Value *, Register > ValueMap
ValueMap - Since we emit code for the function a basic block at a time, we must remember which virtua...
MachineRegisterInfo * RegInfo
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition Pass.cpp:188
Data structure describing the variable locations in a function.
const VarLocInfo * single_locs_begin() const
DILocalVariable * getDILocalVariable(const VarLocInfo *Loc) const
Return the DILocalVariable for the location definition represented by ID.
const VarLocInfo * single_locs_end() const
One past the last single-location variable location definition.
const BasicBlock & getEntryBlock() const
Definition Function.h:807
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition Function.h:209
unsigned getMaxBlockNumber() const
Return a value larger than the largest block number.
Definition Function.h:826
iterator_range< arg_iterator > args()
Definition Function.h:890
DISubprogram * getSubprogram() const
Get the attached subprogram.
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:703
bool hasGC() const
hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm to use during code generatio...
Definition Function.h:344
bool hasOptNone() const
Do not optimize this function (-O0).
Definition Function.h:700
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:359
An analysis pass which caches information about the Function.
Definition GCMetadata.h:214
An analysis pass which caches information about the entire Module.
Definition GCMetadata.h:237
Module * getParent()
Get the module that this global value is contained inside of...
This class is used to form a handle around another node that is persistent and is updated across invo...
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
bool isTerminator() const
A wrapper class for inspecting calls to intrinsic functions.
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
This is an alternative analysis pass to BlockFrequencyInfoWrapperPass.
static void getLazyBFIAnalysisUsage(AnalysisUsage &AU)
Helper for client passes to set up the analysis usage on behalf of this pass.
Record a mapping from subtarget to LibcallLoweringInfo.
const LibcallLoweringInfo & getLibcallLowering(const TargetSubtargetInfo &Subtarget) const
Describe properties that are true of each instruction in the target description file.
virtual unsigned getHwMode(enum HwModeType type=HwMode_Default) const
HwMode ID corresponding to the 'type' parameter is retrieved from the HwMode bit set of the current s...
const MDNode * getMD() const
Metadata node.
Definition Metadata.h:1078
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1442
LLVM_ABI StringRef getString() const
Definition Metadata.cpp:624
Machine Value Type.
SimpleValueType SimpleTy
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator insertAfter(iterator I, MachineInstr *MI)
Insert MI into the instruction list after I.
LLVM_ABI bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
bool hasCalls() const
Return true if the current function has any function calls.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
bool useDebugInstrRef() const
Returns true if the function's variable locations are tracked with instruction referencing.
void setWasmLandingPadIndex(const MachineBasicBlock *LPad, unsigned Index)
Map the landing pad to its index. Used for Wasm exception handling.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
void setUseDebugInstrRef(bool UseInstrRef)
Set whether this function will use instruction referencing or not.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
Function & getFunction()
Return the LLVM function that this machine code represents.
bool shouldUseDebugInstrRef() const
Determine whether, in the current machine configuration, we should use instruction referencing or not...
const MachineFunctionProperties & getProperties() const
Get the function properties.
void setVariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr, int Slot, const DILocation *Loc)
Collect information used to emit debugging information of a variable in a stack slot.
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
bool isTerminator(QueryType Type=AnyInBundle) const
Returns true if this instruction part of the terminator for a basic block.
const MachineOperand & getOperand(unsigned i) const
A description of a memory reference used in the backend.
An analysis that produces MachineModuleInfo for a module.
This class contains meta information specific to a module.
Register getReg() const
getReg - Returns the register number.
MachinePassRegistry - Track the registration of machine passes.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
ArrayRef< std::pair< MCRegister, Register > > liveins() const
An SDNode that represents everything that will be needed to construct a MachineInstr.
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
Definition Module.cpp:358
This class is used by SelectionDAGISel to temporarily override the optimization level on a per-functi...
OptLevelChanger(SelectionDAGISel &ISel, CodeGenOptLevel NewOptLevel)
The optimization diagnostic interface.
LLVM_ABI void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
Diagnostic information for missed-optimization remarks.
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
AnalysisType * getAnalysisIfAvailable() const
getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to get analysis information tha...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
RegisterPassParser class - Handle the addition of new machine passes.
ScheduleDAGSDNodes *(*)(SelectionDAGISel *, CodeGenOptLevel) FunctionPassCtor
static LLVM_ABI MachinePassRegistry< FunctionPassCtor > Registry
RegisterScheduler class - Track the registration of instruction schedulers.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
bool isMachineOpcode() const
Test if this node has a post-isel opcode, directly corresponding to a MachineInstr opcode.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
SDNode * getGluedUser() const
If this node has a glue value with a user, return the user (there is at most one).
LLVM_ABI bool isOnlyUserOf(const SDNode *N) const
Return true if this node is the only use of N.
iterator_range< value_op_iterator > op_values() const
iterator_range< use_iterator > uses()
void setNodeId(int Id)
Set unique node id.
static bool hasPredecessorHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallVectorImpl< const SDNode * > &Worklist, unsigned int MaxSteps=0, bool TopologicalPrune=false)
Returns true if N is a predecessor of any node in Worklist.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
bool use_empty() const
Return true if there are no uses of this node.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
unsigned getNumOperands() const
Return the number of values used by this operation.
const SDValue & getOperand(unsigned Num) const
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
Represents a use of a SDNode.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
ScheduleDAGSDNodes - A ScheduleDAG for scheduling SDNode-based DAGs.
SelectionDAGBuilder - This is the common target-independent lowering implementation that is parameter...
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
SelectionDAGISelLegacy(char &ID, std::unique_ptr< SelectionDAGISel > S)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
SelectionDAGISel - This is the common base class used for SelectionDAG-based pattern-matching instruc...
std::optional< BatchAAResults > BatchAA
std::unique_ptr< FunctionLoweringInfo > FuncInfo
SmallPtrSet< const Instruction *, 4 > ElidedArgCopyInstrs
virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, InlineAsm::ConstraintCode ConstraintID, std::vector< SDValue > &OutOps)
SelectInlineAsmMemoryOperand - Select the specified address as a target addressing mode,...
bool CheckOrMask(SDValue LHS, ConstantSDNode *RHS, int64_t DesiredMaskS) const
CheckOrMask - The isel is trying to match something like (or X, 255).
void initializeAnalysisResults(MachineFunctionAnalysisManager &MFAM)
const TargetTransformInfo * TTI
virtual bool CheckNodePredicate(SDValue Op, unsigned PredNo) const
CheckNodePredicate - This function is generated by tblgen in the target.
MachineModuleInfo * MMI
virtual bool CheckNodePredicateWithOperands(SDValue Op, unsigned PredNo, ArrayRef< SDValue > Operands) const
CheckNodePredicateWithOperands - This function is generated by tblgen in the target.
const TargetLowering * TLI
virtual void PostprocessISelDAG()
PostprocessISelDAG() - This hook allows the target to hack on the graph right after selection.
std::unique_ptr< OptimizationRemarkEmitter > ORE
Current optimization remark emitter.
MachineRegisterInfo * RegInfo
unsigned DAGSize
DAGSize - Size of DAG being instruction selected.
bool isOrEquivalentToAdd(const SDNode *N) const
virtual bool CheckComplexPattern(SDNode *Root, SDNode *Parent, SDValue N, unsigned PatternNo, SmallVectorImpl< std::pair< SDValue, SDNode * > > &Result)
virtual bool CheckPatternPredicate(unsigned PredNo) const
CheckPatternPredicate - This function is generated by tblgen in the target.
static int getNumFixedFromVariadicInfo(unsigned Flags)
getNumFixedFromVariadicInfo - Transform an EmitNode flags word into the number of fixed arity values ...
const TargetLibraryInfo * LibInfo
static int getUninvalidatedNodeId(SDNode *N)
const TargetInstrInfo * TII
std::unique_ptr< SwiftErrorValueTracking > SwiftError
static void EnforceNodeIdInvariant(SDNode *N)
void SelectCodeCommon(SDNode *NodeToMatch, const uint8_t *MatcherTable, unsigned TableSize)
void ReplaceUses(SDValue F, SDValue T)
ReplaceUses - replace all uses of the old node F with the use of the new node T.
virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const
IsProfitableToFold - Returns true if it's profitable to fold the specific operand node N of U during ...
virtual SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo)
virtual MVT getValueTypeForHwMode(unsigned Index) const
bool MatchFilterFuncName
True if the function currently processing is in the function printing list (i.e.
void SelectInlineAsmMemoryOperands(std::vector< SDValue > &Ops, const SDLoc &DL)
SelectInlineAsmMemoryOperands - Calls to this are automatically generated by tblgen.
static bool IsLegalToFold(SDValue N, SDNode *U, SDNode *Root, CodeGenOptLevel OptLevel, bool IgnoreChains=false)
IsLegalToFold - Returns true if the specific operand node N of U can be folded during instruction sel...
virtual bool ComplexPatternFuncMutatesDAG() const
Return true if complex patterns for this target can mutate the DAG.
virtual void PreprocessISelDAG()
PreprocessISelDAG - This hook allows targets to hack on the graph before instruction selection starts...
BatchAAResults * getBatchAA() const
Returns a (possibly null) pointer to the current BatchAAResults.
bool CheckAndMask(SDValue LHS, ConstantSDNode *RHS, int64_t DesiredMaskS) const
CheckAndMask - The isel is trying to match something like (and X, 255).
virtual StringRef getPatternForIndex(unsigned index)
getPatternForIndex - Patterns selected by tablegen during ISEL
bool mayRaiseFPException(SDNode *Node) const
Return whether the node may raise an FP exception.
std::unique_ptr< SelectionDAGBuilder > SDB
void ReplaceNode(SDNode *F, SDNode *T)
Replace all uses of F with T, then remove F from the DAG.
const LibcallLoweringInfo * LibcallLowering
SelectionDAGISel(TargetMachine &tm, CodeGenOptLevel OL=CodeGenOptLevel::Default)
virtual bool runOnMachineFunction(MachineFunction &mf)
static void InvalidateNodeId(SDNode *N)
virtual StringRef getIncludePathForIndex(unsigned index)
getIncludePathForIndex - get the td source location of pattern instantiation
Targets can subclass this to parameterize the SelectionDAG lowering and instruction selection process...
virtual bool mayRaiseFPException(unsigned Opcode) const
Returns true if a node with the given target-specific opcode may raise a floating-point exception.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
allnodes_const_iterator allnodes_begin() const
const DataLayout & getDataLayout() const
LLVM_ABI void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI unsigned AssignTopologicalOrder()
Topological-sort the AllNodes list and a assign a unique node id for each node in the DAG based on th...
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
ilist< SDNode >::iterator allnodes_iterator
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
reference emplace_back(ArgTypes &&... Args)
void resize(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:140
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
MachineBasicBlock * emitPatchPoint(MachineInstr &MI, MachineBasicBlock *MBB) const
Replace/modify any TargetFrameIndex operands with a targte-dependent sequence of memory operands that...
Sched::Preference getSchedulingPreference() const
Return target scheduling preference.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual void AdjustInstrPostInstrSelection(MachineInstr &MI, SDNode *Node) const
This method should be implemented by targets that mark instructions with the 'hasPostISelHook' flag.
virtual MachineBasicBlock * EmitInstrWithCustomInserter(MachineInstr &MI, MachineBasicBlock *MBB) const
This method should be implemented by targets that mark instructions with the 'usesCustomInserter' fla...
Primary interface to the complete machine description for the target machine.
const std::optional< PGOOptions > & getPGOOption() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetLowering * getTargetLowering() const
Wrapper pass for TargetTransformInfo.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
bool isTokenTy() const
Return true if this is 'token'.
Definition Type.h:234
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
Analysis pass which computes UniformityInfo.
Legacy analysis pass which computes a CycleInfo.
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
iterator_range< user_iterator > users()
Definition Value.h:426
bool use_empty() const
Definition Value.h:346
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
self_iterator getIterator()
Definition ilist_node.h:123
A raw_ostream that writes to an std::string.
CallInst * Call
Changed
#define UINT64_MAX
Definition DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI bool isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are ~0 ...
@ TargetConstantPool
Definition ISDOpcodes.h:189
@ CONVERGENCECTRL_ANCHOR
The llvm.experimental.convergence.* intrinsics.
@ MDNODE_SDNODE
MDNODE_SDNODE - This is a node that holdes an MDNode*, which is used to reference metadata in the IR.
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:511
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition ISDOpcodes.h:45
@ POISON
POISON - A poison node.
Definition ISDOpcodes.h:236
@ JUMP_TABLE_DEBUG_INFO
JUMP_TABLE_DEBUG_INFO - Jumptable debug info.
@ TargetBlockAddress
Definition ISDOpcodes.h:191
@ DEACTIVATION_SYMBOL
Untyped node storing deactivation symbol reference (DeactivationSymbolSDNode).
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:220
@ MEMBARRIER
MEMBARRIER - Compiler barrier only; generate a no-op.
@ FAKE_USE
FAKE_USE represents a use of the operand but does not do anything.
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
@ ANNOTATION_LABEL
ANNOTATION_LABEL - Represents a mid basic block label used by annotations.
@ STRICT_UINT_TO_FP
Definition ISDOpcodes.h:485
@ TargetExternalSymbol
Definition ISDOpcodes.h:190
@ CONVERGENCECTRL_ENTRY
@ TargetJumpTable
Definition ISDOpcodes.h:188
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:233
@ AssertAlign
AssertAlign - These nodes record if a register contains a value that has a known alignment and the tr...
Definition ISDOpcodes.h:69
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:230
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition ISDOpcodes.h:185
@ ARITH_FENCE
ARITH_FENCE - This corresponds to a arithmetic fence intrinsic.
@ AssertNoFPClass
AssertNoFPClass - These nodes record if a register contains a float value that is known to be not som...
Definition ISDOpcodes.h:78
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition ISDOpcodes.h:48
@ READ_REGISTER
READ_REGISTER, WRITE_REGISTER - This node represents llvm.register on the DAG, which implements the n...
Definition ISDOpcodes.h:139
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition ISDOpcodes.h:224
@ TargetConstantFP
Definition ISDOpcodes.h:180
@ PATCHPOINT
The llvm.experimental.patchpoint.
@ TargetFrameIndex
Definition ISDOpcodes.h:187
@ LIFETIME_START
This corresponds to the llvm.lifetime.
@ STRICT_SINT_TO_FP
STRICT_[US]INT_TO_FP - Convert a signed or unsigned integer to a floating point value.
Definition ISDOpcodes.h:484
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
@ INLINEASM_BR
INLINEASM_BR - Branching version of inline asm. Used by asm-goto.
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition ISDOpcodes.h:179
@ RELOC_NONE
Issue a no-op relocation against a given symbol at the current location.
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:738
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:205
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
@ STACKMAP
The llvm.experimental.stackmap intrinsic.
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:241
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ CONVERGENCECTRL_LOOP
@ INLINEASM
INLINEASM - Represents an inline asm block.
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:213
@ TargetGlobalTLSAddress
Definition ISDOpcodes.h:186
LLVM_ABI bool isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are 0 o...
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
LLVM_ABI StringRef getBaseName(ID id)
Return the LLVM name for an intrinsic, without encoded types for overloading, such as "llvm....
@ Kill
The last use of a register.
initializer< Ty > init(const Ty &Val)
DiagnosticInfoOptimizationBase::Argument NV
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
iterator end() const
Definition BasicBlock.h:89
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
GenericUniformityInfo< SSAContext > UniformityInfo
LLVM_ABI ScheduleDAGSDNodes * createDefaultScheduler(SelectionDAGISel *IS, CodeGenOptLevel OptLevel)
createDefaultScheduler - This creates an instruction scheduler appropriate for the target.
@ Offset
Definition DWP.cpp:532
OuterAnalysisManagerProxy< ModuleAnalysisManager, MachineFunction > ModuleAnalysisManagerMachineFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
bool succ_empty(const Instruction *I)
Definition CFG.h:257
LLVM_ABI ScheduleDAGSDNodes * createBURRListDAGScheduler(SelectionDAGISel *IS, CodeGenOptLevel OptLevel)
createBURRListDAGScheduler - This creates a bottom up register usage reduction list scheduler.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI ScheduleDAGSDNodes * createHybridListDAGScheduler(SelectionDAGISel *IS, CodeGenOptLevel)
createHybridListDAGScheduler - This creates a bottom up register pressure aware list scheduler that m...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2198
MachineBasicBlock::iterator findSplitPointForStackProtector(MachineBasicBlock *BB, const TargetInstrInfo &TII)
Find the split point at which to splice the end of BB into its success stack protector check machine ...
LLVM_ABI bool TimePassesIsEnabled
If the user specifies the -time-passes argument on an LLVM tool command line then the value of this b...
Definition IRReader.cpp:27
LLVM_ABI LLT getLLTForMVT(MVT Ty)
Get a rough equivalent of an LLT for a given MVT.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI void initializeGCModuleInfoPass(PassRegistry &)
LLVM_ABI ScheduleDAGSDNodes * createFastDAGScheduler(SelectionDAGISel *IS, CodeGenOptLevel OptLevel)
createFastDAGScheduler - This creates a "fast" scheduler.
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
Definition STLExtras.h:2190
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1744
LLVM_ABI ScheduleDAGSDNodes * createDAGLinearizer(SelectionDAGISel *IS, CodeGenOptLevel OptLevel)
createDAGLinearizer - This creates a "no-scheduling" scheduler which linearize the DAG using topologi...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
bool isFunctionInPrintList(StringRef FunctionName)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:82
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
bool isFuncletEHPersonality(EHPersonality Pers)
Returns true if this is a personality function that invokes handler funclets (which must return to it...
@ AfterLegalizeDAG
Definition DAGCombine.h:19
@ AfterLegalizeVectorOps
Definition DAGCombine.h:18
@ BeforeLegalizeTypes
Definition DAGCombine.h:16
@ AfterLegalizeTypes
Definition DAGCombine.h:17
LLVM_ABI ScheduleDAGSDNodes * createSourceListDAGScheduler(SelectionDAGISel *IS, CodeGenOptLevel OptLevel)
createSourceListDAGScheduler - This creates a bottom up list scheduler that schedules nodes in source...
LLVM_ABI bool isAssignmentTrackingEnabled(const Module &M)
Return true if assignment tracking is enabled for module M.
void replace(R &&Range, const T &OldValue, const T &NewValue)
Provide wrappers to std::replace which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1908
DWARFExpression::Operation Op
LLVM_ABI void initializeAAResultsWrapperPassPass(PassRegistry &)
LLVM_ABI void initializeTargetLibraryInfoWrapperPassPass(PassRegistry &)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1915
LLVM_ABI ScheduleDAGSDNodes * createILPListDAGScheduler(SelectionDAGISel *IS, CodeGenOptLevel)
createILPListDAGScheduler - This creates a bottom up register pressure aware list scheduler that trie...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto predecessors(const MachineBasicBlock *BB)
LLVM_ABI void initializeBranchProbabilityInfoWrapperPassPass(PassRegistry &)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1945
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
LLVM_ABI ScheduleDAGSDNodes * createVLIWDAGScheduler(SelectionDAGISel *IS, CodeGenOptLevel OptLevel)
createVLIWDAGScheduler - Scheduler for VLIW targets.
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Extended Value Type.
Definition ValueTypes.h:35
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:137
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:373
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:316
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:152
A struct capturing PGO tunables.
Definition PGOOptions.h:22
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
Clients of various APIs that cause global effects on the DAG can optionally implement this interface.
void addIPToStateRange(const InvokeInst *II, MCSymbol *InvokeBegin, MCSymbol *InvokeEnd)
DenseMap< const BasicBlock *, int > BlockToStateMap