1 //===- llvm/CodeGen/GlobalISel/InstructionSelect.cpp - InstructionSelect ---==//
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
7 //===----------------------------------------------------------------------===//
9 /// This file implements the InstructionSelect class.
10 //===----------------------------------------------------------------------===//
12 #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
13 #include "llvm/ADT/PostOrderIterator.h"
14 #include "llvm/ADT/ScopeExit.h"
15 #include "llvm/Analysis/LazyBlockFrequencyInfo.h"
16 #include "llvm/Analysis/ProfileSummaryInfo.h"
17 #include "llvm/CodeGen/GlobalISel/GISelKnownBits.h"
18 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
19 #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
20 #include "llvm/CodeGen/GlobalISel/Utils.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/CodeGen/TargetLowering.h"
25 #include "llvm/CodeGen/TargetOpcodes.h"
26 #include "llvm/CodeGen/TargetPassConfig.h"
27 #include "llvm/CodeGen/TargetSubtargetInfo.h"
28 #include "llvm/Config/config.h"
29 #include "llvm/IR/Function.h"
30 #include "llvm/MC/TargetRegistry.h"
31 #include "llvm/Support/CodeGenCoverage.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Target/TargetMachine.h"
36 #define DEBUG_TYPE "instruction-select"
40 #ifdef LLVM_GISEL_COV_PREFIX
41 static cl::opt
<std::string
>
42 CoveragePrefix("gisel-coverage-prefix", cl::init(LLVM_GISEL_COV_PREFIX
),
43 cl::desc("Record GlobalISel rule coverage files of this "
44 "prefix if instrumentation was generated"));
46 static const std::string CoveragePrefix
;
49 char InstructionSelect::ID
= 0;
50 INITIALIZE_PASS_BEGIN(InstructionSelect
, DEBUG_TYPE
,
51 "Select target instructions out of generic instructions",
53 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig
)
54 INITIALIZE_PASS_DEPENDENCY(GISelKnownBitsAnalysis
)
55 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass
)
56 INITIALIZE_PASS_DEPENDENCY(LazyBlockFrequencyInfoPass
)
57 INITIALIZE_PASS_END(InstructionSelect
, DEBUG_TYPE
,
58 "Select target instructions out of generic instructions",
61 InstructionSelect::InstructionSelect(CodeGenOptLevel OL
)
62 : MachineFunctionPass(ID
), OptLevel(OL
) {}
64 // In order not to crash when calling getAnalysis during testing with -run-pass
65 // we use the default opt level here instead of None, so that the addRequired()
66 // calls are made in getAnalysisUsage().
67 InstructionSelect::InstructionSelect()
68 : MachineFunctionPass(ID
), OptLevel(CodeGenOptLevel::Default
) {}
70 void InstructionSelect::getAnalysisUsage(AnalysisUsage
&AU
) const {
71 AU
.addRequired
<TargetPassConfig
>();
72 AU
.addRequired
<GISelKnownBitsAnalysis
>();
73 AU
.addPreserved
<GISelKnownBitsAnalysis
>();
75 if (OptLevel
!= CodeGenOptLevel::None
) {
76 AU
.addRequired
<ProfileSummaryInfoWrapperPass
>();
77 LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU
);
79 getSelectionDAGFallbackAnalysisUsage(AU
);
80 MachineFunctionPass::getAnalysisUsage(AU
);
83 bool InstructionSelect::runOnMachineFunction(MachineFunction
&MF
) {
84 // If the ISel pipeline failed, do not bother running that pass.
85 if (MF
.getProperties().hasProperty(
86 MachineFunctionProperties::Property::FailedISel
))
89 LLVM_DEBUG(dbgs() << "Selecting function: " << MF
.getName() << '\n');
91 const TargetPassConfig
&TPC
= getAnalysis
<TargetPassConfig
>();
92 InstructionSelector
*ISel
= MF
.getSubtarget().getInstructionSelector();
93 ISel
->setTargetPassConfig(&TPC
);
95 CodeGenOptLevel OldOptLevel
= OptLevel
;
96 auto RestoreOptLevel
= make_scope_exit([=]() { OptLevel
= OldOptLevel
; });
97 OptLevel
= MF
.getFunction().hasOptNone() ? CodeGenOptLevel::None
98 : MF
.getTarget().getOptLevel();
100 GISelKnownBits
*KB
= &getAnalysis
<GISelKnownBitsAnalysis
>().get(MF
);
101 if (OptLevel
!= CodeGenOptLevel::None
) {
102 PSI
= &getAnalysis
<ProfileSummaryInfoWrapperPass
>().getPSI();
103 if (PSI
&& PSI
->hasProfileSummary())
104 BFI
= &getAnalysis
<LazyBlockFrequencyInfoPass
>().getBFI();
107 CodeGenCoverage CoverageInfo
;
108 assert(ISel
&& "Cannot work without InstructionSelector");
109 ISel
->setupMF(MF
, KB
, &CoverageInfo
, PSI
, BFI
);
111 // An optimization remark emitter. Used to report failures.
112 MachineOptimizationRemarkEmitter
MORE(MF
, /*MBFI=*/nullptr);
113 ISel
->setRemarkEmitter(&MORE
);
115 // FIXME: There are many other MF/MFI fields we need to initialize.
117 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
119 // Check that our input is fully legal: we require the function to have the
120 // Legalized property, so it should be.
121 // FIXME: This should be in the MachineVerifier, as the RegBankSelected
122 // property check already is.
123 if (!DisableGISelLegalityCheck
)
124 if (const MachineInstr
*MI
= machineFunctionIsIllegal(MF
)) {
125 reportGISelFailure(MF
, TPC
, MORE
, "gisel-select",
126 "instruction is not legal", *MI
);
129 // FIXME: We could introduce new blocks and will need to fix the outer loop.
130 // Until then, keep track of the number of blocks to assert that we don't.
131 const size_t NumBlocks
= MF
.size();
133 // Keep track of selected blocks, so we can delete unreachable ones later.
134 DenseSet
<MachineBasicBlock
*> SelectedBlocks
;
136 for (MachineBasicBlock
*MBB
: post_order(&MF
)) {
138 SelectedBlocks
.insert(MBB
);
142 // Select instructions in reverse block order. We permit erasing so have
143 // to resort to manually iterating and recognizing the begin (rend) case.
144 bool ReachedBegin
= false;
145 for (auto MII
= std::prev(MBB
->end()), Begin
= MBB
->begin();
148 // Keep track of the insertion range for debug printing.
149 const auto AfterIt
= std::next(MII
);
151 // Select this instruction.
152 MachineInstr
&MI
= *MII
;
154 // And have our iterator point to the next instruction, if there is one.
160 LLVM_DEBUG(dbgs() << "Selecting: \n " << MI
);
162 // We could have folded this instruction away already, making it dead.
164 if (isTriviallyDead(MI
, MRI
)) {
165 LLVM_DEBUG(dbgs() << "Is dead; erasing.\n");
166 salvageDebugInfo(MRI
, MI
);
167 MI
.eraseFromParent();
171 // Eliminate hints or G_CONSTANT_FOLD_BARRIER.
172 if (isPreISelGenericOptimizationHint(MI
.getOpcode()) ||
173 MI
.getOpcode() == TargetOpcode::G_CONSTANT_FOLD_BARRIER
) {
174 auto [DstReg
, SrcReg
] = MI
.getFirst2Regs();
176 // At this point, the destination register class of the op may have
179 // Propagate that through to the source register.
180 const TargetRegisterClass
*DstRC
= MRI
.getRegClassOrNull(DstReg
);
182 MRI
.setRegClass(SrcReg
, DstRC
);
183 assert(canReplaceReg(DstReg
, SrcReg
, MRI
) &&
184 "Must be able to replace dst with src!");
185 MI
.eraseFromParent();
186 MRI
.replaceRegWith(DstReg
, SrcReg
);
190 if (MI
.getOpcode() == TargetOpcode::G_INVOKE_REGION_START
) {
191 MI
.eraseFromParent();
195 if (!ISel
->select(MI
)) {
196 // FIXME: It would be nice to dump all inserted instructions. It's
197 // not obvious how, esp. considering select() can insert after MI.
198 reportGISelFailure(MF
, TPC
, MORE
, "gisel-select", "cannot select", MI
);
202 // Dump the range of instructions that MI expanded into.
204 auto InsertedBegin
= ReachedBegin
? MBB
->begin() : std::next(MII
);
206 for (auto &InsertedMI
: make_range(InsertedBegin
, AfterIt
))
207 dbgs() << " " << InsertedMI
;
213 for (MachineBasicBlock
&MBB
: MF
) {
217 if (!SelectedBlocks
.contains(&MBB
)) {
218 // This is an unreachable block and therefore hasn't been selected, since
219 // the main selection loop above uses a postorder block traversal.
220 // We delete all the instructions in this block since it's unreachable.
222 // Don't delete the block in case the block has it's address taken or is
223 // still being referenced by a phi somewhere.
226 // Try to find redundant copies b/w vregs of the same register class.
227 bool ReachedBegin
= false;
228 for (auto MII
= std::prev(MBB
.end()), Begin
= MBB
.begin(); !ReachedBegin
;) {
229 // Select this instruction.
230 MachineInstr
&MI
= *MII
;
232 // And have our iterator point to the next instruction, if there is one.
237 if (MI
.getOpcode() != TargetOpcode::COPY
)
239 Register SrcReg
= MI
.getOperand(1).getReg();
240 Register DstReg
= MI
.getOperand(0).getReg();
241 if (SrcReg
.isVirtual() && DstReg
.isVirtual()) {
242 auto SrcRC
= MRI
.getRegClass(SrcReg
);
243 auto DstRC
= MRI
.getRegClass(DstReg
);
244 if (SrcRC
== DstRC
) {
245 MRI
.replaceRegWith(DstReg
, SrcReg
);
246 MI
.eraseFromParent();
253 const TargetRegisterInfo
&TRI
= *MF
.getSubtarget().getRegisterInfo();
254 // Now that selection is complete, there are no more generic vregs. Verify
255 // that the size of the now-constrained vreg is unchanged and that it has a
257 for (unsigned I
= 0, E
= MRI
.getNumVirtRegs(); I
!= E
; ++I
) {
258 Register VReg
= Register::index2VirtReg(I
);
260 MachineInstr
*MI
= nullptr;
261 if (!MRI
.def_empty(VReg
))
262 MI
= &*MRI
.def_instr_begin(VReg
);
263 else if (!MRI
.use_empty(VReg
)) {
264 MI
= &*MRI
.use_instr_begin(VReg
);
265 // Debug value instruction is permitted to use undefined vregs.
266 if (MI
->isDebugValue())
272 const TargetRegisterClass
*RC
= MRI
.getRegClassOrNull(VReg
);
274 reportGISelFailure(MF
, TPC
, MORE
, "gisel-select",
275 "VReg has no regclass after selection", *MI
);
279 const LLT Ty
= MRI
.getType(VReg
);
280 if (Ty
.isValid() && Ty
.getSizeInBits() > TRI
.getRegSizeInBits(*RC
)) {
282 MF
, TPC
, MORE
, "gisel-select",
283 "VReg's low-level type and register class have different sizes", *MI
);
288 if (MF
.size() != NumBlocks
) {
289 MachineOptimizationRemarkMissed
R("gisel-select", "GISelFailure",
290 MF
.getFunction().getSubprogram(),
292 R
<< "inserting blocks is not supported yet";
293 reportGISelFailure(MF
, TPC
, MORE
, R
);
297 // Determine if there are any calls in this machine function. Ported from
299 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
300 for (const auto &MBB
: MF
) {
301 if (MFI
.hasCalls() && MF
.hasInlineAsm())
304 for (const auto &MI
: MBB
) {
305 if ((MI
.isCall() && !MI
.isReturn()) || MI
.isStackAligningInlineAsm())
306 MFI
.setHasCalls(true);
307 if (MI
.isInlineAsm())
308 MF
.setHasInlineAsm(true);
312 // FIXME: FinalizeISel pass calls finalizeLowering, so it's called twice.
313 auto &TLI
= *MF
.getSubtarget().getTargetLowering();
314 TLI
.finalizeLowering(MF
);
317 dbgs() << "Rules covered by selecting function: " << MF
.getName() << ":";
318 for (auto RuleID
: CoverageInfo
.covered())
319 dbgs() << " id" << RuleID
;
322 CoverageInfo
.emit(CoveragePrefix
,
323 TLI
.getTargetMachine().getTarget().getBackendName());
325 // If we successfully selected the function nothing is going to use the vreg
326 // types after us (otherwise MIRPrinter would need them). Make sure the types
328 MRI
.clearVirtRegTypes();
330 // FIXME: Should we accurately track changes?