1 | //===-- llvm/CodeGen/FinalizeISel.cpp ---------------------------*- C++ -*-===// |
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 pass expands Pseudo-instructions produced by ISel, fixes register |
10 | /// reservations and may do machine frame information adjustments. |
11 | /// The pseudo instructions are used to allow the expansion to contain control |
12 | /// flow, such as a conditional move implemented with a conditional branch and a |
13 | /// phi, or an atomic operation implemented with a loop. |
14 | // |
15 | //===----------------------------------------------------------------------===// |
16 | |
17 | #include "llvm/CodeGen/MachineFrameInfo.h" |
18 | #include "llvm/CodeGen/MachineFunction.h" |
19 | #include "llvm/CodeGen/MachineFunctionPass.h" |
20 | #include "llvm/CodeGen/TargetInstrInfo.h" |
21 | #include "llvm/CodeGen/TargetLowering.h" |
22 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
23 | #include "llvm/InitializePasses.h" |
24 | using namespace llvm; |
25 | |
26 | #define DEBUG_TYPE "finalize-isel" |
27 | |
28 | namespace { |
29 | class FinalizeISel : public MachineFunctionPass { |
30 | public: |
31 | static char ID; // Pass identification, replacement for typeid |
32 | FinalizeISel() : MachineFunctionPass(ID) {} |
33 | |
34 | private: |
35 | bool runOnMachineFunction(MachineFunction &MF) override; |
36 | |
37 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
38 | MachineFunctionPass::getAnalysisUsage(AU); |
39 | } |
40 | }; |
41 | } // end anonymous namespace |
42 | |
43 | char FinalizeISel::ID = 0; |
44 | char &llvm::FinalizeISelID = FinalizeISel::ID; |
45 | INITIALIZE_PASS(FinalizeISel, DEBUG_TYPE, |
46 | "Finalize ISel and expand pseudo-instructions" , false, false) |
47 | |
48 | bool FinalizeISel::runOnMachineFunction(MachineFunction &MF) { |
49 | bool Changed = false; |
50 | const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); |
51 | const TargetLowering *TLI = MF.getSubtarget().getTargetLowering(); |
52 | |
53 | // Iterate through each instruction in the function, looking for pseudos. |
54 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) { |
55 | MachineBasicBlock *MBB = &*I; |
56 | for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end(); |
57 | MBBI != MBBE; ) { |
58 | MachineInstr &MI = *MBBI++; |
59 | |
60 | // Set AdjustsStack to true if the instruction selector emits a stack |
61 | // frame setup instruction or a stack aligning inlineasm. |
62 | if (TII->isFrameInstr(I: MI) || MI.isStackAligningInlineAsm()) |
63 | MF.getFrameInfo().setAdjustsStack(true); |
64 | |
65 | // If MI is a pseudo, expand it. |
66 | if (MI.usesCustomInsertionHook()) { |
67 | Changed = true; |
68 | MachineBasicBlock *NewMBB = TLI->EmitInstrWithCustomInserter(MI, MBB); |
69 | // The expansion may involve new basic blocks. |
70 | if (NewMBB != MBB) { |
71 | MBB = NewMBB; |
72 | I = NewMBB->getIterator(); |
73 | MBBI = NewMBB->begin(); |
74 | MBBE = NewMBB->end(); |
75 | } |
76 | } |
77 | } |
78 | } |
79 | |
80 | TLI->finalizeLowering(MF); |
81 | |
82 | return Changed; |
83 | } |
84 | |