Dan Gohman | c2b7861 | 2010-11-18 18:45:06 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/ExpandISelPseudos.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 | // |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 9 | // Expand Pseudo-instructions produced by ISel. These are usually to allow |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 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. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFunction.h" |
| 17 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/Passes.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/TargetLowering.h" |
| 20 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 24 | #define DEBUG_TYPE "expand-isel-pseudos" |
| 25 | |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 26 | namespace { |
Dan Gohman | c2b7861 | 2010-11-18 18:45:06 +0000 | [diff] [blame] | 27 | class ExpandISelPseudos : public MachineFunctionPass { |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 28 | public: |
| 29 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | c2b7861 | 2010-11-18 18:45:06 +0000 | [diff] [blame] | 30 | ExpandISelPseudos() : MachineFunctionPass(ID) {} |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 31 | |
| 32 | private: |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 33 | bool runOnMachineFunction(MachineFunction &MF) override; |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 34 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 35 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 36 | MachineFunctionPass::getAnalysisUsage(AU); |
| 37 | } |
| 38 | }; |
| 39 | } // end anonymous namespace |
| 40 | |
Dan Gohman | c2b7861 | 2010-11-18 18:45:06 +0000 | [diff] [blame] | 41 | char ExpandISelPseudos::ID = 0; |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 42 | char &llvm::ExpandISelPseudosID = ExpandISelPseudos::ID; |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 43 | INITIALIZE_PASS(ExpandISelPseudos, DEBUG_TYPE, |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 44 | "Expand ISel Pseudo-instructions", false, false) |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 45 | |
Dan Gohman | c2b7861 | 2010-11-18 18:45:06 +0000 | [diff] [blame] | 46 | bool ExpandISelPseudos::runOnMachineFunction(MachineFunction &MF) { |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 47 | bool Changed = false; |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 48 | const TargetLowering *TLI = MF.getSubtarget().getTargetLowering(); |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 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) { |
Duncan P. N. Exon Smith | d83547a | 2015-10-09 18:44:40 +0000 | [diff] [blame] | 52 | MachineBasicBlock *MBB = &*I; |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 53 | for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end(); |
| 54 | MBBI != MBBE; ) { |
Duncan P. N. Exon Smith | a62287b | 2016-06-30 23:09:39 +0000 | [diff] [blame] | 55 | MachineInstr &MI = *MBBI++; |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 56 | |
| 57 | // If MI is a pseudo, expand it. |
Duncan P. N. Exon Smith | a62287b | 2016-06-30 23:09:39 +0000 | [diff] [blame] | 58 | if (MI.usesCustomInsertionHook()) { |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 59 | Changed = true; |
Duncan P. N. Exon Smith | a62287b | 2016-06-30 23:09:39 +0000 | [diff] [blame] | 60 | MachineBasicBlock *NewMBB = TLI->EmitInstrWithCustomInserter(MI, MBB); |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 61 | // The expansion may involve new basic blocks. |
| 62 | if (NewMBB != MBB) { |
| 63 | MBB = NewMBB; |
Duncan P. N. Exon Smith | d83547a | 2015-10-09 18:44:40 +0000 | [diff] [blame] | 64 | I = NewMBB->getIterator(); |
Dan Gohman | 8b67c72 | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 65 | MBBI = NewMBB->begin(); |
| 66 | MBBE = NewMBB->end(); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return Changed; |
| 73 | } |