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