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