xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/FinalizeISel.cpp (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
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/MachineFunction.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/TargetLowering.h"
20 #include "llvm/CodeGen/TargetSubtargetInfo.h"
21 #include "llvm/InitializePasses.h"
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "finalize-isel"
25 
26 namespace {
27   class FinalizeISel : public MachineFunctionPass {
28   public:
29     static char ID; // Pass identification, replacement for typeid
30     FinalizeISel() : MachineFunctionPass(ID) {}
31 
32   private:
33     bool runOnMachineFunction(MachineFunction &MF) override;
34 
35     void getAnalysisUsage(AnalysisUsage &AU) const override {
36       MachineFunctionPass::getAnalysisUsage(AU);
37     }
38   };
39 } // end anonymous namespace
40 
41 char FinalizeISel::ID = 0;
42 char &llvm::FinalizeISelID = FinalizeISel::ID;
43 INITIALIZE_PASS(FinalizeISel, DEBUG_TYPE,
44                 "Finalize ISel and expand pseudo-instructions", false, false)
45 
46 bool FinalizeISel::runOnMachineFunction(MachineFunction &MF) {
47   bool Changed = false;
48   const TargetLowering *TLI = MF.getSubtarget().getTargetLowering();
49 
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();
54          MBBI != MBBE; ) {
55       MachineInstr &MI = *MBBI++;
56 
57       // If MI is a pseudo, expand it.
58       if (MI.usesCustomInsertionHook()) {
59         Changed = true;
60         MachineBasicBlock *NewMBB = TLI->EmitInstrWithCustomInserter(MI, MBB);
61         // The expansion may involve new basic blocks.
62         if (NewMBB != MBB) {
63           MBB = NewMBB;
64           I = NewMBB->getIterator();
65           MBBI = NewMBB->begin();
66           MBBE = NewMBB->end();
67         }
68       }
69     }
70   }
71 
72   TLI->finalizeLowering(MF);
73 
74   return Changed;
75 }
76