1 //===- llvm/CodeGen/GlobalISel/InstructionSelect.cpp - InstructionSelect ---==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file implements the InstructionSelect class.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
14 #include "llvm/ADT/PostOrderIterator.h"
15 #include "llvm/ADT/ScopeExit.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
18 #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
19 #include "llvm/CodeGen/GlobalISel/Utils.h"
20 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/TargetLowering.h"
23 #include "llvm/CodeGen/TargetPassConfig.h"
24 #include "llvm/CodeGen/TargetSubtargetInfo.h"
25 #include "llvm/Config/config.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/TargetRegistry.h"
32 #define DEBUG_TYPE "instruction-select"
36 #ifdef LLVM_GISEL_COV_PREFIX
37 static cl::opt<std::string>
38 CoveragePrefix("gisel-coverage-prefix", cl::init(LLVM_GISEL_COV_PREFIX),
39 cl::desc("Record GlobalISel rule coverage files of this "
40 "prefix if instrumentation was generated"));
42 static const std::string CoveragePrefix = "";
45 char InstructionSelect::ID = 0;
46 INITIALIZE_PASS_BEGIN(InstructionSelect, DEBUG_TYPE,
47 "Select target instructions out of generic instructions",
49 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
50 INITIALIZE_PASS_END(InstructionSelect, DEBUG_TYPE,
51 "Select target instructions out of generic instructions",
54 InstructionSelect::InstructionSelect() : MachineFunctionPass(ID) {
55 initializeInstructionSelectPass(*PassRegistry::getPassRegistry());
58 void InstructionSelect::getAnalysisUsage(AnalysisUsage &AU) const {
59 AU.addRequired<TargetPassConfig>();
60 MachineFunctionPass::getAnalysisUsage(AU);
63 bool InstructionSelect::runOnMachineFunction(MachineFunction &MF) {
64 const MachineRegisterInfo &MRI = MF.getRegInfo();
66 // No matter what happens, whether we successfully select the function or not,
67 // nothing is going to use the vreg types after us. Make sure they disappear.
68 auto ClearVRegTypesOnReturn =
69 make_scope_exit([&]() { MRI.getVRegToType().clear(); });
71 // If the ISel pipeline failed, do not bother running that pass.
72 if (MF.getProperties().hasProperty(
73 MachineFunctionProperties::Property::FailedISel))
76 DEBUG(dbgs() << "Selecting function: " << MF.getName() << '\n');
78 const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
79 const InstructionSelector *ISel = MF.getSubtarget().getInstructionSelector();
80 CodeGenCoverage CoverageInfo;
81 assert(ISel && "Cannot work without InstructionSelector");
83 // An optimization remark emitter. Used to report failures.
84 MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
86 // FIXME: There are many other MF/MFI fields we need to initialize.
89 // Check that our input is fully legal: we require the function to have the
90 // Legalized property, so it should be.
91 // FIXME: This should be in the MachineVerifier, but it can't use the
92 // LegalizerInfo as it's currently in the separate GlobalISel library.
93 // The RegBankSelected property is already checked in the verifier. Note
94 // that it has the same layering problem, but we only use inline methods so
95 // end up not needing to link against the GlobalISel library.
96 if (const LegalizerInfo *MLI = MF.getSubtarget().getLegalizerInfo())
97 for (MachineBasicBlock &MBB : MF)
98 for (MachineInstr &MI : MBB)
99 if (isPreISelGenericOpcode(MI.getOpcode()) && !MLI->isLegal(MI, MRI)) {
100 reportGISelFailure(MF, TPC, MORE, "gisel-select",
101 "instruction is not legal", MI);
106 // FIXME: We could introduce new blocks and will need to fix the outer loop.
107 // Until then, keep track of the number of blocks to assert that we don't.
108 const size_t NumBlocks = MF.size();
110 for (MachineBasicBlock *MBB : post_order(&MF)) {
114 // Select instructions in reverse block order. We permit erasing so have
115 // to resort to manually iterating and recognizing the begin (rend) case.
116 bool ReachedBegin = false;
117 for (auto MII = std::prev(MBB->end()), Begin = MBB->begin();
120 // Keep track of the insertion range for debug printing.
121 const auto AfterIt = std::next(MII);
123 // Select this instruction.
124 MachineInstr &MI = *MII;
126 // And have our iterator point to the next instruction, if there is one.
132 DEBUG(dbgs() << "Selecting: \n " << MI);
134 // We could have folded this instruction away already, making it dead.
136 if (isTriviallyDead(MI, MRI)) {
137 DEBUG(dbgs() << "Is dead; erasing.\n");
138 MI.eraseFromParentAndMarkDBGValuesForRemoval();
142 if (!ISel->select(MI, CoverageInfo)) {
143 // FIXME: It would be nice to dump all inserted instructions. It's
144 // not obvious how, esp. considering select() can insert after MI.
145 reportGISelFailure(MF, TPC, MORE, "gisel-select", "cannot select", MI);
149 // Dump the range of instructions that MI expanded into.
151 auto InsertedBegin = ReachedBegin ? MBB->begin() : std::next(MII);
153 for (auto &InsertedMI : make_range(InsertedBegin, AfterIt))
154 dbgs() << " " << InsertedMI;
160 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
162 // Now that selection is complete, there are no more generic vregs. Verify
163 // that the size of the now-constrained vreg is unchanged and that it has a
165 for (auto &VRegToType : MRI.getVRegToType()) {
166 unsigned VReg = VRegToType.first;
167 auto *RC = MRI.getRegClassOrNull(VReg);
168 MachineInstr *MI = nullptr;
169 if (!MRI.def_empty(VReg))
170 MI = &*MRI.def_instr_begin(VReg);
171 else if (!MRI.use_empty(VReg))
172 MI = &*MRI.use_instr_begin(VReg);
175 reportGISelFailure(MF, TPC, MORE, "gisel-select",
176 "VReg has no regclass after selection", *MI);
181 if (VRegToType.second.isValid() &&
182 VRegToType.second.getSizeInBits() > TRI.getRegSizeInBits(*RC)) {
183 reportGISelFailure(MF, TPC, MORE, "gisel-select",
184 "VReg has explicit size different from class size",
190 if (MF.size() != NumBlocks) {
191 MachineOptimizationRemarkMissed R("gisel-select", "GISelFailure",
192 MF.getFunction().getSubprogram(),
194 R << "inserting blocks is not supported yet";
195 reportGISelFailure(MF, TPC, MORE, R);
199 auto &TLI = *MF.getSubtarget().getTargetLowering();
200 TLI.finalizeLowering(MF);
202 CoverageInfo.emit(CoveragePrefix,
209 // FIXME: Should we accurately track changes?