Dan Gohman | 8ec9d62 | 2010-11-18 18:45:06 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/ExpandISelPseudos.cpp ----------------------*- C++ -*-===// |
Dan Gohman | 668ac2f | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 10 | // Expand Pseudo-instructions produced by ISel. These are usually to allow |
Dan Gohman | 668ac2f | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 11 | // the expansion to contain control flow, such as a conditional move |
| 12 | // implemented with a conditional branch and a phi, or an atomic operation |
| 13 | // implemented with a loop. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Dan Gohman | 668ac2f | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFunction.h" |
| 18 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/Passes.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/TargetLowering.h" |
| 21 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
Dan Gohman | 668ac2f | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Chandler Carruth | 8677f2f | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 25 | #define DEBUG_TYPE "expand-isel-pseudos" |
| 26 | |
Dan Gohman | 668ac2f | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 27 | namespace { |
Dan Gohman | 8ec9d62 | 2010-11-18 18:45:06 +0000 | [diff] [blame] | 28 | class ExpandISelPseudos : public MachineFunctionPass { |
Dan Gohman | 668ac2f | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 29 | public: |
| 30 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | 8ec9d62 | 2010-11-18 18:45:06 +0000 | [diff] [blame] | 31 | ExpandISelPseudos() : MachineFunctionPass(ID) {} |
Dan Gohman | 668ac2f | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 32 | |
| 33 | private: |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 34 | bool runOnMachineFunction(MachineFunction &MF) override; |
Dan Gohman | 668ac2f | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 35 | |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 36 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | 668ac2f | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 37 | MachineFunctionPass::getAnalysisUsage(AU); |
| 38 | } |
| 39 | }; |
| 40 | } // end anonymous namespace |
| 41 | |
Dan Gohman | 8ec9d62 | 2010-11-18 18:45:06 +0000 | [diff] [blame] | 42 | char ExpandISelPseudos::ID = 0; |
Andrew Trick | 1dd8c85 | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 43 | char &llvm::ExpandISelPseudosID = ExpandISelPseudos::ID; |
Matthias Braun | 94c4904 | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 44 | INITIALIZE_PASS(ExpandISelPseudos, DEBUG_TYPE, |
Andrew Trick | 1dd8c85 | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 45 | "Expand ISel Pseudo-instructions", false, false) |
Dan Gohman | 668ac2f | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 46 | |
Dan Gohman | 8ec9d62 | 2010-11-18 18:45:06 +0000 | [diff] [blame] | 47 | bool ExpandISelPseudos::runOnMachineFunction(MachineFunction &MF) { |
Dan Gohman | 668ac2f | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 48 | bool Changed = false; |
Eric Christopher | 6035518 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 49 | const TargetLowering *TLI = MF.getSubtarget().getTargetLowering(); |
Dan Gohman | 668ac2f | 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 | 030cf8e | 2015-10-09 18:44:40 +0000 | [diff] [blame] | 53 | MachineBasicBlock *MBB = &*I; |
Dan Gohman | 668ac2f | 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 | b6b73f6 | 2016-06-30 23:09:39 +0000 | [diff] [blame] | 56 | MachineInstr &MI = *MBBI++; |
Dan Gohman | 668ac2f | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 57 | |
| 58 | // If MI is a pseudo, expand it. |
Duncan P. N. Exon Smith | b6b73f6 | 2016-06-30 23:09:39 +0000 | [diff] [blame] | 59 | if (MI.usesCustomInsertionHook()) { |
Dan Gohman | 668ac2f | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 60 | Changed = true; |
Duncan P. N. Exon Smith | b6b73f6 | 2016-06-30 23:09:39 +0000 | [diff] [blame] | 61 | MachineBasicBlock *NewMBB = TLI->EmitInstrWithCustomInserter(MI, MBB); |
Dan Gohman | 668ac2f | 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 | 030cf8e | 2015-10-09 18:44:40 +0000 | [diff] [blame] | 65 | I = NewMBB->getIterator(); |
Dan Gohman | 668ac2f | 2010-11-16 21:02:37 +0000 | [diff] [blame] | 66 | MBBI = NewMBB->begin(); |
| 67 | MBBE = NewMBB->end(); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return Changed; |
| 74 | } |