1 //===-- llvm/CodeGen/FinalizeISel.cpp ---------------------------*- C++ -*-===//
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 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.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/CodeGen/TargetLowering.h"
21 #include "llvm/CodeGen/TargetSubtargetInfo.h"
22 #include "llvm/InitializePasses.h"
23 #include "llvm/Support/Debug.h"
26 #define DEBUG_TYPE "finalize-isel"
29 class FinalizeISel
: public MachineFunctionPass
{
31 static char ID
; // Pass identification, replacement for typeid
32 FinalizeISel() : MachineFunctionPass(ID
) {}
35 bool runOnMachineFunction(MachineFunction
&MF
) override
;
37 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
38 MachineFunctionPass::getAnalysisUsage(AU
);
41 } // end anonymous namespace
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)
48 bool FinalizeISel::runOnMachineFunction(MachineFunction
&MF
) {
50 const TargetLowering
*TLI
= MF
.getSubtarget().getTargetLowering();
52 // Iterate through each instruction in the function, looking for pseudos.
53 for (MachineFunction::iterator I
= MF
.begin(), E
= MF
.end(); I
!= E
; ++I
) {
54 MachineBasicBlock
*MBB
= &*I
;
55 for (MachineBasicBlock::iterator MBBI
= MBB
->begin(), MBBE
= MBB
->end();
57 MachineInstr
&MI
= *MBBI
++;
59 // If MI is a pseudo, expand it.
60 if (MI
.usesCustomInsertionHook()) {
62 MachineBasicBlock
*NewMBB
= TLI
->EmitInstrWithCustomInserter(MI
, MBB
);
63 // The expansion may involve new basic blocks.
66 I
= NewMBB
->getIterator();
67 MBBI
= NewMBB
->begin();
74 TLI
->finalizeLowering(MF
);