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