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