Matt Arsenault | 9cac4e6 | 2019-06-19 00:25:39 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/FinalizeISel.cpp ---------------------------*- C++ -*-===// |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Matt Arsenault | 9cac4e6 | 2019-06-19 00:25:39 +0000 | [diff] [blame] | 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. |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFunction.h" |
| 18 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/Passes.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/TargetLowering.h" |
| 21 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame] | 22 | #include "llvm/InitializePasses.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Matt Arsenault | 9cac4e6 | 2019-06-19 00:25:39 +0000 | [diff] [blame] | 26 | #define DEBUG_TYPE "finalize-isel" |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 27 | |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 28 | namespace { |
Matt Arsenault | 9cac4e6 | 2019-06-19 00:25:39 +0000 | [diff] [blame] | 29 | class FinalizeISel : public MachineFunctionPass { |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 30 | public: |
| 31 | static char ID; // Pass identification, replacement for typeid |
Matt Arsenault | 9cac4e6 | 2019-06-19 00:25:39 +0000 | [diff] [blame] | 32 | FinalizeISel() : MachineFunctionPass(ID) {} |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 33 | |
| 34 | private: |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 35 | bool runOnMachineFunction(MachineFunction &MF) override; |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 36 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 37 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 38 | MachineFunctionPass::getAnalysisUsage(AU); |
| 39 | } |
| 40 | }; |
| 41 | } // end anonymous namespace |
| 42 | |
Matt Arsenault | 9cac4e6 | 2019-06-19 00:25:39 +0000 | [diff] [blame] | 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) |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 47 | |
Matt Arsenault | 9cac4e6 | 2019-06-19 00:25:39 +0000 | [diff] [blame] | 48 | bool FinalizeISel::runOnMachineFunction(MachineFunction &MF) { |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 49 | bool Changed = false; |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 50 | const TargetLowering *TLI = MF.getSubtarget().getTargetLowering(); |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 51 | |
| 52 | // Iterate through each instruction in the function, looking for pseudos. |
| 53 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) { |
Duncan P. N. Exon Smith | d83547a | 2015-10-09 18:44:40 +0000 | [diff] [blame] | 54 | MachineBasicBlock *MBB = &*I; |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 55 | for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end(); |
| 56 | MBBI != MBBE; ) { |
Duncan P. N. Exon Smith | a62287b | 2016-06-30 23:09:39 +0000 | [diff] [blame] | 57 | MachineInstr &MI = *MBBI++; |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 58 | |
| 59 | // If MI is a pseudo, expand it. |
Duncan P. N. Exon Smith | a62287b | 2016-06-30 23:09:39 +0000 | [diff] [blame] | 60 | if (MI.usesCustomInsertionHook()) { |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 61 | Changed = true; |
Duncan P. N. Exon Smith | a62287b | 2016-06-30 23:09:39 +0000 | [diff] [blame] | 62 | MachineBasicBlock *NewMBB = TLI->EmitInstrWithCustomInserter(MI, MBB); |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 63 | // The expansion may involve new basic blocks. |
| 64 | if (NewMBB != MBB) { |
| 65 | MBB = NewMBB; |
Duncan P. N. Exon Smith | d83547a | 2015-10-09 18:44:40 +0000 | [diff] [blame] | 66 | I = NewMBB->getIterator(); |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 67 | MBBI = NewMBB->begin(); |
| 68 | MBBE = NewMBB->end(); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
Matt Arsenault | 9cac4e6 | 2019-06-19 00:25:39 +0000 | [diff] [blame] | 74 | TLI->finalizeLowering(MF); |
| 75 | |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 76 | return Changed; |
| 77 | } |