xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/GlobalISel/InstructionSelect.cpp (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
10b57cec5SDimitry Andric //===- llvm/CodeGen/GlobalISel/InstructionSelect.cpp - InstructionSelect ---==//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric /// \file
90b57cec5SDimitry Andric /// This file implements the InstructionSelect class.
100b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
130b57cec5SDimitry Andric #include "llvm/ADT/PostOrderIterator.h"
14fe6060f1SDimitry Andric #include "llvm/ADT/ScopeExit.h"
15fe6060f1SDimitry Andric #include "llvm/Analysis/LazyBlockFrequencyInfo.h"
16fe6060f1SDimitry Andric #include "llvm/Analysis/ProfileSummaryInfo.h"
178bcb0991SDimitry Andric #include "llvm/CodeGen/GlobalISel/GISelKnownBits.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/Utils.h"
218bcb0991SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
22349cc55cSDimitry Andric #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
2506c3fb27SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
280b57cec5SDimitry Andric #include "llvm/Config/config.h"
290b57cec5SDimitry Andric #include "llvm/IR/Function.h"
30349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
3181ad6265SDimitry Andric #include "llvm/Support/CodeGenCoverage.h"
320b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
330b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
34*7a6dacacSDimitry Andric #include "llvm/Support/DebugCounter.h"
355ffd83dbSDimitry Andric #include "llvm/Target/TargetMachine.h"
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric #define DEBUG_TYPE "instruction-select"
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric using namespace llvm;
400b57cec5SDimitry Andric 
41*7a6dacacSDimitry Andric DEBUG_COUNTER(GlobalISelCounter, "globalisel",
42*7a6dacacSDimitry Andric               "Controls whether to select function with GlobalISel");
43*7a6dacacSDimitry Andric 
440b57cec5SDimitry Andric #ifdef LLVM_GISEL_COV_PREFIX
450b57cec5SDimitry Andric static cl::opt<std::string>
460b57cec5SDimitry Andric     CoveragePrefix("gisel-coverage-prefix", cl::init(LLVM_GISEL_COV_PREFIX),
470b57cec5SDimitry Andric                    cl::desc("Record GlobalISel rule coverage files of this "
480b57cec5SDimitry Andric                             "prefix if instrumentation was generated"));
490b57cec5SDimitry Andric #else
50e8d8bef9SDimitry Andric static const std::string CoveragePrefix;
510b57cec5SDimitry Andric #endif
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric char InstructionSelect::ID = 0;
540b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(InstructionSelect, DEBUG_TYPE,
550b57cec5SDimitry Andric                       "Select target instructions out of generic instructions",
560b57cec5SDimitry Andric                       false, false)
570b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
588bcb0991SDimitry Andric INITIALIZE_PASS_DEPENDENCY(GISelKnownBitsAnalysis)
59fe6060f1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
60fe6060f1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LazyBlockFrequencyInfoPass)
610b57cec5SDimitry Andric INITIALIZE_PASS_END(InstructionSelect, DEBUG_TYPE,
620b57cec5SDimitry Andric                     "Select target instructions out of generic instructions",
630b57cec5SDimitry Andric                     false, false)
640b57cec5SDimitry Andric 
655f757f3fSDimitry Andric InstructionSelect::InstructionSelect(CodeGenOptLevel OL)
66fe6060f1SDimitry Andric     : MachineFunctionPass(ID), OptLevel(OL) {}
67fe6060f1SDimitry Andric 
68fe6060f1SDimitry Andric // In order not to crash when calling getAnalysis during testing with -run-pass
69fe6060f1SDimitry Andric // we use the default opt level here instead of None, so that the addRequired()
70fe6060f1SDimitry Andric // calls are made in getAnalysisUsage().
71fe6060f1SDimitry Andric InstructionSelect::InstructionSelect()
725f757f3fSDimitry Andric     : MachineFunctionPass(ID), OptLevel(CodeGenOptLevel::Default) {}
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric void InstructionSelect::getAnalysisUsage(AnalysisUsage &AU) const {
750b57cec5SDimitry Andric   AU.addRequired<TargetPassConfig>();
768bcb0991SDimitry Andric   AU.addRequired<GISelKnownBitsAnalysis>();
778bcb0991SDimitry Andric   AU.addPreserved<GISelKnownBitsAnalysis>();
7804eeddc0SDimitry Andric 
795f757f3fSDimitry Andric   if (OptLevel != CodeGenOptLevel::None) {
80fe6060f1SDimitry Andric     AU.addRequired<ProfileSummaryInfoWrapperPass>();
81fe6060f1SDimitry Andric     LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU);
82fe6060f1SDimitry Andric   }
830b57cec5SDimitry Andric   getSelectionDAGFallbackAnalysisUsage(AU);
840b57cec5SDimitry Andric   MachineFunctionPass::getAnalysisUsage(AU);
850b57cec5SDimitry Andric }
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric bool InstructionSelect::runOnMachineFunction(MachineFunction &MF) {
880b57cec5SDimitry Andric   // If the ISel pipeline failed, do not bother running that pass.
890b57cec5SDimitry Andric   if (MF.getProperties().hasProperty(
900b57cec5SDimitry Andric           MachineFunctionProperties::Property::FailedISel))
910b57cec5SDimitry Andric     return false;
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Selecting function: " << MF.getName() << '\n');
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric   const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
968bcb0991SDimitry Andric   InstructionSelector *ISel = MF.getSubtarget().getInstructionSelector();
975f757f3fSDimitry Andric   ISel->setTargetPassConfig(&TPC);
98fe6060f1SDimitry Andric 
995f757f3fSDimitry Andric   CodeGenOptLevel OldOptLevel = OptLevel;
100fe6060f1SDimitry Andric   auto RestoreOptLevel = make_scope_exit([=]() { OptLevel = OldOptLevel; });
1015f757f3fSDimitry Andric   OptLevel = MF.getFunction().hasOptNone() ? CodeGenOptLevel::None
102fe6060f1SDimitry Andric                                            : MF.getTarget().getOptLevel();
103fe6060f1SDimitry Andric 
10404eeddc0SDimitry Andric   GISelKnownBits *KB = &getAnalysis<GISelKnownBitsAnalysis>().get(MF);
1055f757f3fSDimitry Andric   if (OptLevel != CodeGenOptLevel::None) {
106fe6060f1SDimitry Andric     PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
107fe6060f1SDimitry Andric     if (PSI && PSI->hasProfileSummary())
108fe6060f1SDimitry Andric       BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI();
109fe6060f1SDimitry Andric   }
110fe6060f1SDimitry Andric 
1110b57cec5SDimitry Andric   CodeGenCoverage CoverageInfo;
1120b57cec5SDimitry Andric   assert(ISel && "Cannot work without InstructionSelector");
11306c3fb27SDimitry Andric   ISel->setupMF(MF, KB, &CoverageInfo, PSI, BFI);
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   // An optimization remark emitter. Used to report failures.
1160b57cec5SDimitry Andric   MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
1175f757f3fSDimitry Andric   ISel->setRemarkEmitter(&MORE);
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   // FIXME: There are many other MF/MFI fields we need to initialize.
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
1220b57cec5SDimitry Andric #ifndef NDEBUG
1230b57cec5SDimitry Andric   // Check that our input is fully legal: we require the function to have the
1240b57cec5SDimitry Andric   // Legalized property, so it should be.
1250b57cec5SDimitry Andric   // FIXME: This should be in the MachineVerifier, as the RegBankSelected
1260b57cec5SDimitry Andric   // property check already is.
1270b57cec5SDimitry Andric   if (!DisableGISelLegalityCheck)
1280b57cec5SDimitry Andric     if (const MachineInstr *MI = machineFunctionIsIllegal(MF)) {
1290b57cec5SDimitry Andric       reportGISelFailure(MF, TPC, MORE, "gisel-select",
1300b57cec5SDimitry Andric                          "instruction is not legal", *MI);
1310b57cec5SDimitry Andric       return false;
1320b57cec5SDimitry Andric     }
1330b57cec5SDimitry Andric   // FIXME: We could introduce new blocks and will need to fix the outer loop.
1340b57cec5SDimitry Andric   // Until then, keep track of the number of blocks to assert that we don't.
1350b57cec5SDimitry Andric   const size_t NumBlocks = MF.size();
1360b57cec5SDimitry Andric #endif
137349cc55cSDimitry Andric   // Keep track of selected blocks, so we can delete unreachable ones later.
138349cc55cSDimitry Andric   DenseSet<MachineBasicBlock *> SelectedBlocks;
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   for (MachineBasicBlock *MBB : post_order(&MF)) {
141fe6060f1SDimitry Andric     ISel->CurMBB = MBB;
142349cc55cSDimitry Andric     SelectedBlocks.insert(MBB);
1430b57cec5SDimitry Andric     if (MBB->empty())
1440b57cec5SDimitry Andric       continue;
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric     // Select instructions in reverse block order. We permit erasing so have
1470b57cec5SDimitry Andric     // to resort to manually iterating and recognizing the begin (rend) case.
1480b57cec5SDimitry Andric     bool ReachedBegin = false;
1490b57cec5SDimitry Andric     for (auto MII = std::prev(MBB->end()), Begin = MBB->begin();
1500b57cec5SDimitry Andric          !ReachedBegin;) {
1510b57cec5SDimitry Andric #ifndef NDEBUG
1520b57cec5SDimitry Andric       // Keep track of the insertion range for debug printing.
1530b57cec5SDimitry Andric       const auto AfterIt = std::next(MII);
1540b57cec5SDimitry Andric #endif
1550b57cec5SDimitry Andric       // Select this instruction.
1560b57cec5SDimitry Andric       MachineInstr &MI = *MII;
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric       // And have our iterator point to the next instruction, if there is one.
1590b57cec5SDimitry Andric       if (MII == Begin)
1600b57cec5SDimitry Andric         ReachedBegin = true;
1610b57cec5SDimitry Andric       else
1620b57cec5SDimitry Andric         --MII;
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Selecting: \n  " << MI);
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric       // We could have folded this instruction away already, making it dead.
1670b57cec5SDimitry Andric       // If so, erase it.
1680b57cec5SDimitry Andric       if (isTriviallyDead(MI, MRI)) {
1690b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Is dead; erasing.\n");
170bdd1243dSDimitry Andric         salvageDebugInfo(MRI, MI);
1710eae32dcSDimitry Andric         MI.eraseFromParent();
1720b57cec5SDimitry Andric         continue;
1730b57cec5SDimitry Andric       }
1740b57cec5SDimitry Andric 
17506c3fb27SDimitry Andric       // Eliminate hints or G_CONSTANT_FOLD_BARRIER.
17606c3fb27SDimitry Andric       if (isPreISelGenericOptimizationHint(MI.getOpcode()) ||
17706c3fb27SDimitry Andric           MI.getOpcode() == TargetOpcode::G_CONSTANT_FOLD_BARRIER) {
17806c3fb27SDimitry Andric         auto [DstReg, SrcReg] = MI.getFirst2Regs();
179fe6060f1SDimitry Andric 
18006c3fb27SDimitry Andric         // At this point, the destination register class of the op may have
181fe6060f1SDimitry Andric         // been decided.
182fe6060f1SDimitry Andric         //
183fe6060f1SDimitry Andric         // Propagate that through to the source register.
184fe6060f1SDimitry Andric         const TargetRegisterClass *DstRC = MRI.getRegClassOrNull(DstReg);
185fe6060f1SDimitry Andric         if (DstRC)
186fe6060f1SDimitry Andric           MRI.setRegClass(SrcReg, DstRC);
187fe6060f1SDimitry Andric         assert(canReplaceReg(DstReg, SrcReg, MRI) &&
188fe6060f1SDimitry Andric                "Must be able to replace dst with src!");
189fe6060f1SDimitry Andric         MI.eraseFromParent();
190fe6060f1SDimitry Andric         MRI.replaceRegWith(DstReg, SrcReg);
191fe6060f1SDimitry Andric         continue;
192fe6060f1SDimitry Andric       }
193fe6060f1SDimitry Andric 
194bdd1243dSDimitry Andric       if (MI.getOpcode() == TargetOpcode::G_INVOKE_REGION_START) {
195bdd1243dSDimitry Andric         MI.eraseFromParent();
196bdd1243dSDimitry Andric         continue;
197bdd1243dSDimitry Andric       }
198bdd1243dSDimitry Andric 
1998bcb0991SDimitry Andric       if (!ISel->select(MI)) {
2000b57cec5SDimitry Andric         // FIXME: It would be nice to dump all inserted instructions.  It's
2010b57cec5SDimitry Andric         // not obvious how, esp. considering select() can insert after MI.
2020b57cec5SDimitry Andric         reportGISelFailure(MF, TPC, MORE, "gisel-select", "cannot select", MI);
2030b57cec5SDimitry Andric         return false;
2040b57cec5SDimitry Andric       }
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric       // Dump the range of instructions that MI expanded into.
2070b57cec5SDimitry Andric       LLVM_DEBUG({
2080b57cec5SDimitry Andric         auto InsertedBegin = ReachedBegin ? MBB->begin() : std::next(MII);
2090b57cec5SDimitry Andric         dbgs() << "Into:\n";
2100b57cec5SDimitry Andric         for (auto &InsertedMI : make_range(InsertedBegin, AfterIt))
2110b57cec5SDimitry Andric           dbgs() << "  " << InsertedMI;
2120b57cec5SDimitry Andric         dbgs() << '\n';
2130b57cec5SDimitry Andric       });
2140b57cec5SDimitry Andric     }
2150b57cec5SDimitry Andric   }
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
2180b57cec5SDimitry Andric     if (MBB.empty())
2190b57cec5SDimitry Andric       continue;
2200b57cec5SDimitry Andric 
221349cc55cSDimitry Andric     if (!SelectedBlocks.contains(&MBB)) {
222349cc55cSDimitry Andric       // This is an unreachable block and therefore hasn't been selected, since
223349cc55cSDimitry Andric       // the main selection loop above uses a postorder block traversal.
224349cc55cSDimitry Andric       // We delete all the instructions in this block since it's unreachable.
225349cc55cSDimitry Andric       MBB.clear();
226349cc55cSDimitry Andric       // Don't delete the block in case the block has it's address taken or is
227349cc55cSDimitry Andric       // still being referenced by a phi somewhere.
228349cc55cSDimitry Andric       continue;
229349cc55cSDimitry Andric     }
2300b57cec5SDimitry Andric     // Try to find redundant copies b/w vregs of the same register class.
2310b57cec5SDimitry Andric     bool ReachedBegin = false;
2320b57cec5SDimitry Andric     for (auto MII = std::prev(MBB.end()), Begin = MBB.begin(); !ReachedBegin;) {
2330b57cec5SDimitry Andric       // Select this instruction.
2340b57cec5SDimitry Andric       MachineInstr &MI = *MII;
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric       // And have our iterator point to the next instruction, if there is one.
2370b57cec5SDimitry Andric       if (MII == Begin)
2380b57cec5SDimitry Andric         ReachedBegin = true;
2390b57cec5SDimitry Andric       else
2400b57cec5SDimitry Andric         --MII;
2410b57cec5SDimitry Andric       if (MI.getOpcode() != TargetOpcode::COPY)
2420b57cec5SDimitry Andric         continue;
2438bcb0991SDimitry Andric       Register SrcReg = MI.getOperand(1).getReg();
2448bcb0991SDimitry Andric       Register DstReg = MI.getOperand(0).getReg();
245bdd1243dSDimitry Andric       if (SrcReg.isVirtual() && DstReg.isVirtual()) {
2460b57cec5SDimitry Andric         auto SrcRC = MRI.getRegClass(SrcReg);
2470b57cec5SDimitry Andric         auto DstRC = MRI.getRegClass(DstReg);
2480b57cec5SDimitry Andric         if (SrcRC == DstRC) {
2490b57cec5SDimitry Andric           MRI.replaceRegWith(DstReg, SrcReg);
2505ffd83dbSDimitry Andric           MI.eraseFromParent();
2510b57cec5SDimitry Andric         }
2520b57cec5SDimitry Andric       }
2530b57cec5SDimitry Andric     }
2540b57cec5SDimitry Andric   }
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric #ifndef NDEBUG
2570b57cec5SDimitry Andric   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
2580b57cec5SDimitry Andric   // Now that selection is complete, there are no more generic vregs.  Verify
2590b57cec5SDimitry Andric   // that the size of the now-constrained vreg is unchanged and that it has a
2600b57cec5SDimitry Andric   // register class.
2610b57cec5SDimitry Andric   for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
262bdd1243dSDimitry Andric     Register VReg = Register::index2VirtReg(I);
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric     MachineInstr *MI = nullptr;
2650b57cec5SDimitry Andric     if (!MRI.def_empty(VReg))
2660b57cec5SDimitry Andric       MI = &*MRI.def_instr_begin(VReg);
2670eae32dcSDimitry Andric     else if (!MRI.use_empty(VReg)) {
2680b57cec5SDimitry Andric       MI = &*MRI.use_instr_begin(VReg);
2690eae32dcSDimitry Andric       // Debug value instruction is permitted to use undefined vregs.
2700eae32dcSDimitry Andric       if (MI->isDebugValue())
2710eae32dcSDimitry Andric         continue;
2720eae32dcSDimitry Andric     }
2730b57cec5SDimitry Andric     if (!MI)
2740b57cec5SDimitry Andric       continue;
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric     const TargetRegisterClass *RC = MRI.getRegClassOrNull(VReg);
2770b57cec5SDimitry Andric     if (!RC) {
2780b57cec5SDimitry Andric       reportGISelFailure(MF, TPC, MORE, "gisel-select",
2790b57cec5SDimitry Andric                          "VReg has no regclass after selection", *MI);
2800b57cec5SDimitry Andric       return false;
2810b57cec5SDimitry Andric     }
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric     const LLT Ty = MRI.getType(VReg);
2840b57cec5SDimitry Andric     if (Ty.isValid() && Ty.getSizeInBits() > TRI.getRegSizeInBits(*RC)) {
2850b57cec5SDimitry Andric       reportGISelFailure(
2860b57cec5SDimitry Andric           MF, TPC, MORE, "gisel-select",
2870b57cec5SDimitry Andric           "VReg's low-level type and register class have different sizes", *MI);
2880b57cec5SDimitry Andric       return false;
2890b57cec5SDimitry Andric     }
2900b57cec5SDimitry Andric   }
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric   if (MF.size() != NumBlocks) {
2930b57cec5SDimitry Andric     MachineOptimizationRemarkMissed R("gisel-select", "GISelFailure",
2940b57cec5SDimitry Andric                                       MF.getFunction().getSubprogram(),
2950b57cec5SDimitry Andric                                       /*MBB=*/nullptr);
2960b57cec5SDimitry Andric     R << "inserting blocks is not supported yet";
2970b57cec5SDimitry Andric     reportGISelFailure(MF, TPC, MORE, R);
2980b57cec5SDimitry Andric     return false;
2990b57cec5SDimitry Andric   }
3000b57cec5SDimitry Andric #endif
301*7a6dacacSDimitry Andric 
302*7a6dacacSDimitry Andric   if (!DebugCounter::shouldExecute(GlobalISelCounter)) {
303*7a6dacacSDimitry Andric     dbgs() << "Falling back for function " << MF.getName() << "\n";
304*7a6dacacSDimitry Andric     MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
305*7a6dacacSDimitry Andric     return false;
306*7a6dacacSDimitry Andric   }
307*7a6dacacSDimitry Andric 
3088bcb0991SDimitry Andric   // Determine if there are any calls in this machine function. Ported from
3098bcb0991SDimitry Andric   // SelectionDAG.
3108bcb0991SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
3118bcb0991SDimitry Andric   for (const auto &MBB : MF) {
3128bcb0991SDimitry Andric     if (MFI.hasCalls() && MF.hasInlineAsm())
3138bcb0991SDimitry Andric       break;
3148bcb0991SDimitry Andric 
3158bcb0991SDimitry Andric     for (const auto &MI : MBB) {
3168bcb0991SDimitry Andric       if ((MI.isCall() && !MI.isReturn()) || MI.isStackAligningInlineAsm())
3178bcb0991SDimitry Andric         MFI.setHasCalls(true);
3188bcb0991SDimitry Andric       if (MI.isInlineAsm())
3198bcb0991SDimitry Andric         MF.setHasInlineAsm(true);
3208bcb0991SDimitry Andric     }
3218bcb0991SDimitry Andric   }
3228bcb0991SDimitry Andric 
3235ffd83dbSDimitry Andric   // FIXME: FinalizeISel pass calls finalizeLowering, so it's called twice.
3245ffd83dbSDimitry Andric   auto &TLI = *MF.getSubtarget().getTargetLowering();
3255ffd83dbSDimitry Andric   TLI.finalizeLowering(MF);
3268bcb0991SDimitry Andric 
3270b57cec5SDimitry Andric   LLVM_DEBUG({
3280b57cec5SDimitry Andric     dbgs() << "Rules covered by selecting function: " << MF.getName() << ":";
3290b57cec5SDimitry Andric     for (auto RuleID : CoverageInfo.covered())
3300b57cec5SDimitry Andric       dbgs() << " id" << RuleID;
3310b57cec5SDimitry Andric     dbgs() << "\n\n";
3320b57cec5SDimitry Andric   });
3330b57cec5SDimitry Andric   CoverageInfo.emit(CoveragePrefix,
3345ffd83dbSDimitry Andric                     TLI.getTargetMachine().getTarget().getBackendName());
3350b57cec5SDimitry Andric 
3360b57cec5SDimitry Andric   // If we successfully selected the function nothing is going to use the vreg
3370b57cec5SDimitry Andric   // types after us (otherwise MIRPrinter would need them). Make sure the types
3380b57cec5SDimitry Andric   // disappear.
3390b57cec5SDimitry Andric   MRI.clearVirtRegTypes();
3400b57cec5SDimitry Andric 
3410b57cec5SDimitry Andric   // FIXME: Should we accurately track changes?
3420b57cec5SDimitry Andric   return true;
3430b57cec5SDimitry Andric }
344