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