1 //===-- llvm/CodeGen/ExpandISelPseudos.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 // Expand Pseudo-instructions produced by ISel. These are usually to allow
10 // the expansion to contain control flow, such as a conditional move
11 // implemented with a conditional branch and a phi, or an atomic operation
12 // implemented with a loop.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/CodeGen/TargetLowering.h"
20 #include "llvm/CodeGen/TargetSubtargetInfo.h"
21 #include "llvm/Support/Debug.h"
24 #define DEBUG_TYPE "expand-isel-pseudos"
27 class ExpandISelPseudos
: public MachineFunctionPass
{
29 static char ID
; // Pass identification, replacement for typeid
30 ExpandISelPseudos() : MachineFunctionPass(ID
) {}
33 bool runOnMachineFunction(MachineFunction
&MF
) override
;
35 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
36 MachineFunctionPass::getAnalysisUsage(AU
);
39 } // end anonymous namespace
41 char ExpandISelPseudos::ID
= 0;
42 char &llvm::ExpandISelPseudosID
= ExpandISelPseudos::ID
;
43 INITIALIZE_PASS(ExpandISelPseudos
, DEBUG_TYPE
,
44 "Expand ISel Pseudo-instructions", false, false)
46 bool ExpandISelPseudos::runOnMachineFunction(MachineFunction
&MF
) {
48 const TargetLowering
*TLI
= MF
.getSubtarget().getTargetLowering();
50 // Iterate through each instruction in the function, looking for pseudos.
51 for (MachineFunction::iterator I
= MF
.begin(), E
= MF
.end(); I
!= E
; ++I
) {
52 MachineBasicBlock
*MBB
= &*I
;
53 for (MachineBasicBlock::iterator MBBI
= MBB
->begin(), MBBE
= MBB
->end();
55 MachineInstr
&MI
= *MBBI
++;
57 // If MI is a pseudo, expand it.
58 if (MI
.usesCustomInsertionHook()) {
60 MachineBasicBlock
*NewMBB
= TLI
->EmitInstrWithCustomInserter(MI
, MBB
);
61 // The expansion may involve new basic blocks.
64 I
= NewMBB
->getIterator();
65 MBBI
= NewMBB
->begin();