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