xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/GlobalISel/InstructionSelect.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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"
140b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
158bcb0991SDimitry Andric #include "llvm/CodeGen/GlobalISel/GISelKnownBits.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/Utils.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
208bcb0991SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
228bcb0991SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
260b57cec5SDimitry Andric #include "llvm/Config/config.h"
270b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
280b57cec5SDimitry Andric #include "llvm/IR/Function.h"
290b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
300b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
310b57cec5SDimitry Andric #include "llvm/Support/TargetRegistry.h"
32*5ffd83dbSDimitry Andric #include "llvm/Target/TargetMachine.h"
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric #define DEBUG_TYPE "instruction-select"
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric using namespace llvm;
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric #ifdef LLVM_GISEL_COV_PREFIX
390b57cec5SDimitry Andric static cl::opt<std::string>
400b57cec5SDimitry Andric     CoveragePrefix("gisel-coverage-prefix", cl::init(LLVM_GISEL_COV_PREFIX),
410b57cec5SDimitry Andric                    cl::desc("Record GlobalISel rule coverage files of this "
420b57cec5SDimitry Andric                             "prefix if instrumentation was generated"));
430b57cec5SDimitry Andric #else
440b57cec5SDimitry Andric static const std::string CoveragePrefix = "";
450b57cec5SDimitry Andric #endif
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric char InstructionSelect::ID = 0;
480b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(InstructionSelect, DEBUG_TYPE,
490b57cec5SDimitry Andric                       "Select target instructions out of generic instructions",
500b57cec5SDimitry Andric                       false, false)
510b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
528bcb0991SDimitry Andric INITIALIZE_PASS_DEPENDENCY(GISelKnownBitsAnalysis)
530b57cec5SDimitry Andric INITIALIZE_PASS_END(InstructionSelect, DEBUG_TYPE,
540b57cec5SDimitry Andric                     "Select target instructions out of generic instructions",
550b57cec5SDimitry Andric                     false, false)
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric InstructionSelect::InstructionSelect() : MachineFunctionPass(ID) { }
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric void InstructionSelect::getAnalysisUsage(AnalysisUsage &AU) const {
600b57cec5SDimitry Andric   AU.addRequired<TargetPassConfig>();
618bcb0991SDimitry Andric   AU.addRequired<GISelKnownBitsAnalysis>();
628bcb0991SDimitry Andric   AU.addPreserved<GISelKnownBitsAnalysis>();
630b57cec5SDimitry Andric   getSelectionDAGFallbackAnalysisUsage(AU);
640b57cec5SDimitry Andric   MachineFunctionPass::getAnalysisUsage(AU);
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric bool InstructionSelect::runOnMachineFunction(MachineFunction &MF) {
680b57cec5SDimitry Andric   // If the ISel pipeline failed, do not bother running that pass.
690b57cec5SDimitry Andric   if (MF.getProperties().hasProperty(
700b57cec5SDimitry Andric           MachineFunctionProperties::Property::FailedISel))
710b57cec5SDimitry Andric     return false;
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Selecting function: " << MF.getName() << '\n');
748bcb0991SDimitry Andric   GISelKnownBits &KB = getAnalysis<GISelKnownBitsAnalysis>().get(MF);
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric   const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
778bcb0991SDimitry Andric   InstructionSelector *ISel = MF.getSubtarget().getInstructionSelector();
780b57cec5SDimitry Andric   CodeGenCoverage CoverageInfo;
790b57cec5SDimitry Andric   assert(ISel && "Cannot work without InstructionSelector");
808bcb0991SDimitry Andric   ISel->setupMF(MF, KB, CoverageInfo);
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   // An optimization remark emitter. Used to report failures.
830b57cec5SDimitry Andric   MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   // FIXME: There are many other MF/MFI fields we need to initialize.
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
880b57cec5SDimitry Andric #ifndef NDEBUG
890b57cec5SDimitry Andric   // Check that our input is fully legal: we require the function to have the
900b57cec5SDimitry Andric   // Legalized property, so it should be.
910b57cec5SDimitry Andric   // FIXME: This should be in the MachineVerifier, as the RegBankSelected
920b57cec5SDimitry Andric   // property check already is.
930b57cec5SDimitry Andric   if (!DisableGISelLegalityCheck)
940b57cec5SDimitry Andric     if (const MachineInstr *MI = machineFunctionIsIllegal(MF)) {
950b57cec5SDimitry Andric       reportGISelFailure(MF, TPC, MORE, "gisel-select",
960b57cec5SDimitry Andric                          "instruction is not legal", *MI);
970b57cec5SDimitry Andric       return false;
980b57cec5SDimitry Andric     }
990b57cec5SDimitry Andric   // FIXME: We could introduce new blocks and will need to fix the outer loop.
1000b57cec5SDimitry Andric   // Until then, keep track of the number of blocks to assert that we don't.
1010b57cec5SDimitry Andric   const size_t NumBlocks = MF.size();
1020b57cec5SDimitry Andric #endif
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric   for (MachineBasicBlock *MBB : post_order(&MF)) {
1050b57cec5SDimitry Andric     if (MBB->empty())
1060b57cec5SDimitry Andric       continue;
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric     // Select instructions in reverse block order. We permit erasing so have
1090b57cec5SDimitry Andric     // to resort to manually iterating and recognizing the begin (rend) case.
1100b57cec5SDimitry Andric     bool ReachedBegin = false;
1110b57cec5SDimitry Andric     for (auto MII = std::prev(MBB->end()), Begin = MBB->begin();
1120b57cec5SDimitry Andric          !ReachedBegin;) {
1130b57cec5SDimitry Andric #ifndef NDEBUG
1140b57cec5SDimitry Andric       // Keep track of the insertion range for debug printing.
1150b57cec5SDimitry Andric       const auto AfterIt = std::next(MII);
1160b57cec5SDimitry Andric #endif
1170b57cec5SDimitry Andric       // Select this instruction.
1180b57cec5SDimitry Andric       MachineInstr &MI = *MII;
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric       // And have our iterator point to the next instruction, if there is one.
1210b57cec5SDimitry Andric       if (MII == Begin)
1220b57cec5SDimitry Andric         ReachedBegin = true;
1230b57cec5SDimitry Andric       else
1240b57cec5SDimitry Andric         --MII;
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Selecting: \n  " << MI);
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric       // We could have folded this instruction away already, making it dead.
1290b57cec5SDimitry Andric       // If so, erase it.
1300b57cec5SDimitry Andric       if (isTriviallyDead(MI, MRI)) {
1310b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Is dead; erasing.\n");
1320b57cec5SDimitry Andric         MI.eraseFromParentAndMarkDBGValuesForRemoval();
1330b57cec5SDimitry Andric         continue;
1340b57cec5SDimitry Andric       }
1350b57cec5SDimitry Andric 
1368bcb0991SDimitry Andric       if (!ISel->select(MI)) {
1370b57cec5SDimitry Andric         // FIXME: It would be nice to dump all inserted instructions.  It's
1380b57cec5SDimitry Andric         // not obvious how, esp. considering select() can insert after MI.
1390b57cec5SDimitry Andric         reportGISelFailure(MF, TPC, MORE, "gisel-select", "cannot select", MI);
1400b57cec5SDimitry Andric         return false;
1410b57cec5SDimitry Andric       }
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric       // Dump the range of instructions that MI expanded into.
1440b57cec5SDimitry Andric       LLVM_DEBUG({
1450b57cec5SDimitry Andric         auto InsertedBegin = ReachedBegin ? MBB->begin() : std::next(MII);
1460b57cec5SDimitry Andric         dbgs() << "Into:\n";
1470b57cec5SDimitry Andric         for (auto &InsertedMI : make_range(InsertedBegin, AfterIt))
1480b57cec5SDimitry Andric           dbgs() << "  " << InsertedMI;
1490b57cec5SDimitry Andric         dbgs() << '\n';
1500b57cec5SDimitry Andric       });
1510b57cec5SDimitry Andric     }
1520b57cec5SDimitry Andric   }
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
1550b57cec5SDimitry Andric     if (MBB.empty())
1560b57cec5SDimitry Andric       continue;
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric     // Try to find redundant copies b/w vregs of the same register class.
1590b57cec5SDimitry Andric     bool ReachedBegin = false;
1600b57cec5SDimitry Andric     for (auto MII = std::prev(MBB.end()), Begin = MBB.begin(); !ReachedBegin;) {
1610b57cec5SDimitry Andric       // Select this instruction.
1620b57cec5SDimitry Andric       MachineInstr &MI = *MII;
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric       // And have our iterator point to the next instruction, if there is one.
1650b57cec5SDimitry Andric       if (MII == Begin)
1660b57cec5SDimitry Andric         ReachedBegin = true;
1670b57cec5SDimitry Andric       else
1680b57cec5SDimitry Andric         --MII;
1690b57cec5SDimitry Andric       if (MI.getOpcode() != TargetOpcode::COPY)
1700b57cec5SDimitry Andric         continue;
1718bcb0991SDimitry Andric       Register SrcReg = MI.getOperand(1).getReg();
1728bcb0991SDimitry Andric       Register DstReg = MI.getOperand(0).getReg();
1738bcb0991SDimitry Andric       if (Register::isVirtualRegister(SrcReg) &&
1748bcb0991SDimitry Andric           Register::isVirtualRegister(DstReg)) {
1750b57cec5SDimitry Andric         auto SrcRC = MRI.getRegClass(SrcReg);
1760b57cec5SDimitry Andric         auto DstRC = MRI.getRegClass(DstReg);
1770b57cec5SDimitry Andric         if (SrcRC == DstRC) {
1780b57cec5SDimitry Andric           MRI.replaceRegWith(DstReg, SrcReg);
179*5ffd83dbSDimitry Andric           MI.eraseFromParent();
1800b57cec5SDimitry Andric         }
1810b57cec5SDimitry Andric       }
1820b57cec5SDimitry Andric     }
1830b57cec5SDimitry Andric   }
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric #ifndef NDEBUG
1860b57cec5SDimitry Andric   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1870b57cec5SDimitry Andric   // Now that selection is complete, there are no more generic vregs.  Verify
1880b57cec5SDimitry Andric   // that the size of the now-constrained vreg is unchanged and that it has a
1890b57cec5SDimitry Andric   // register class.
1900b57cec5SDimitry Andric   for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
1918bcb0991SDimitry Andric     unsigned VReg = Register::index2VirtReg(I);
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric     MachineInstr *MI = nullptr;
1940b57cec5SDimitry Andric     if (!MRI.def_empty(VReg))
1950b57cec5SDimitry Andric       MI = &*MRI.def_instr_begin(VReg);
1960b57cec5SDimitry Andric     else if (!MRI.use_empty(VReg))
1970b57cec5SDimitry Andric       MI = &*MRI.use_instr_begin(VReg);
1980b57cec5SDimitry Andric     if (!MI)
1990b57cec5SDimitry Andric       continue;
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric     const TargetRegisterClass *RC = MRI.getRegClassOrNull(VReg);
2020b57cec5SDimitry Andric     if (!RC) {
2030b57cec5SDimitry Andric       reportGISelFailure(MF, TPC, MORE, "gisel-select",
2040b57cec5SDimitry Andric                          "VReg has no regclass after selection", *MI);
2050b57cec5SDimitry Andric       return false;
2060b57cec5SDimitry Andric     }
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric     const LLT Ty = MRI.getType(VReg);
2090b57cec5SDimitry Andric     if (Ty.isValid() && Ty.getSizeInBits() > TRI.getRegSizeInBits(*RC)) {
2100b57cec5SDimitry Andric       reportGISelFailure(
2110b57cec5SDimitry Andric           MF, TPC, MORE, "gisel-select",
2120b57cec5SDimitry Andric           "VReg's low-level type and register class have different sizes", *MI);
2130b57cec5SDimitry Andric       return false;
2140b57cec5SDimitry Andric     }
2150b57cec5SDimitry Andric   }
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric   if (MF.size() != NumBlocks) {
2180b57cec5SDimitry Andric     MachineOptimizationRemarkMissed R("gisel-select", "GISelFailure",
2190b57cec5SDimitry Andric                                       MF.getFunction().getSubprogram(),
2200b57cec5SDimitry Andric                                       /*MBB=*/nullptr);
2210b57cec5SDimitry Andric     R << "inserting blocks is not supported yet";
2220b57cec5SDimitry Andric     reportGISelFailure(MF, TPC, MORE, R);
2230b57cec5SDimitry Andric     return false;
2240b57cec5SDimitry Andric   }
2250b57cec5SDimitry Andric #endif
2268bcb0991SDimitry Andric   // Determine if there are any calls in this machine function. Ported from
2278bcb0991SDimitry Andric   // SelectionDAG.
2288bcb0991SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
2298bcb0991SDimitry Andric   for (const auto &MBB : MF) {
2308bcb0991SDimitry Andric     if (MFI.hasCalls() && MF.hasInlineAsm())
2318bcb0991SDimitry Andric       break;
2328bcb0991SDimitry Andric 
2338bcb0991SDimitry Andric     for (const auto &MI : MBB) {
2348bcb0991SDimitry Andric       if ((MI.isCall() && !MI.isReturn()) || MI.isStackAligningInlineAsm())
2358bcb0991SDimitry Andric         MFI.setHasCalls(true);
2368bcb0991SDimitry Andric       if (MI.isInlineAsm())
2378bcb0991SDimitry Andric         MF.setHasInlineAsm(true);
2388bcb0991SDimitry Andric     }
2398bcb0991SDimitry Andric   }
2408bcb0991SDimitry Andric 
241*5ffd83dbSDimitry Andric   // FIXME: FinalizeISel pass calls finalizeLowering, so it's called twice.
242*5ffd83dbSDimitry Andric   auto &TLI = *MF.getSubtarget().getTargetLowering();
243*5ffd83dbSDimitry Andric   TLI.finalizeLowering(MF);
2448bcb0991SDimitry Andric 
2450b57cec5SDimitry Andric   LLVM_DEBUG({
2460b57cec5SDimitry Andric     dbgs() << "Rules covered by selecting function: " << MF.getName() << ":";
2470b57cec5SDimitry Andric     for (auto RuleID : CoverageInfo.covered())
2480b57cec5SDimitry Andric       dbgs() << " id" << RuleID;
2490b57cec5SDimitry Andric     dbgs() << "\n\n";
2500b57cec5SDimitry Andric   });
2510b57cec5SDimitry Andric   CoverageInfo.emit(CoveragePrefix,
252*5ffd83dbSDimitry Andric                     TLI.getTargetMachine().getTarget().getBackendName());
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric   // If we successfully selected the function nothing is going to use the vreg
2550b57cec5SDimitry Andric   // types after us (otherwise MIRPrinter would need them). Make sure the types
2560b57cec5SDimitry Andric   // disappear.
2570b57cec5SDimitry Andric   MRI.clearVirtRegTypes();
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric   // FIXME: Should we accurately track changes?
2600b57cec5SDimitry Andric   return true;
2610b57cec5SDimitry Andric }
262