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