23b543175a3e563bb1e9d54d8832f221c731dff1
[oota-llvm.git] / lib / CodeGen / ExpandISelPseudos.cpp
1 //===-- llvm/CodeGen/ExpandISelPseudos.cpp ----------------------*- C++ -*-===//
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 //
10 // Expand Pseudo-instructions produced by ISel. These are usually to allow
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
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Target/TargetLowering.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetSubtargetInfo.h"
24 using namespace llvm;
25
26 #define DEBUG_TYPE "expand-isel-pseudos"
27
28 namespace {
29   class ExpandISelPseudos : public MachineFunctionPass {
30   public:
31     static char ID; // Pass identification, replacement for typeid
32     ExpandISelPseudos() : MachineFunctionPass(ID) {}
33
34   private:
35     bool runOnMachineFunction(MachineFunction &MF) override;
36
37     void getAnalysisUsage(AnalysisUsage &AU) const override {
38       MachineFunctionPass::getAnalysisUsage(AU);
39     }
40   };
41 } // end anonymous namespace
42
43 char ExpandISelPseudos::ID = 0;
44 char &llvm::ExpandISelPseudosID = ExpandISelPseudos::ID;
45 INITIALIZE_PASS(ExpandISelPseudos, "expand-isel-pseudos",
46                 "Expand ISel Pseudo-instructions", false, false)
47
48 bool ExpandISelPseudos::runOnMachineFunction(MachineFunction &MF) {
49   bool Changed = false;
50   const TargetLowering *TLI =
51       MF.getTarget().getSubtargetImpl()->getTargetLowering();
52
53   // Iterate through each instruction in the function, looking for pseudos.
54   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
55     MachineBasicBlock *MBB = I;
56     for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
57          MBBI != MBBE; ) {
58       MachineInstr *MI = MBBI++;
59
60       // If MI is a pseudo, expand it.
61       if (MI->usesCustomInsertionHook()) {
62         Changed = true;
63         MachineBasicBlock *NewMBB =
64           TLI->EmitInstrWithCustomInserter(MI, MBB);
65         // The expansion may involve new basic blocks.
66         if (NewMBB != MBB) {
67           MBB = NewMBB;
68           I = NewMBB;
69           MBBI = NewMBB->begin();
70           MBBE = NewMBB->end();
71         }
72       }
73     }
74   }
75
76   return Changed;
77 }