blob: 2f7bc8f6d46ddcdbbbccf17f98baa44288d3e319 [file] [log] [blame]
Dan Gohman668ac2f2010-11-16 21:02:37 +00001//===-- llvm/CodeGen/ExpandPseudos.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//
Dan Gohman76043672010-11-16 21:27:00 +000010// Expand Psuedo-instructions produced by ISel. These are usually to allow
Dan Gohman668ac2f2010-11-16 21:02:37 +000011// 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#define DEBUG_TYPE "expand-pseudos"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/Passes.h"
21#include "llvm/Target/TargetLowering.h"
22#include "llvm/Target/TargetMachine.h"
23#include "llvm/Support/Debug.h"
24using namespace llvm;
25
26namespace {
27 class ExpandPseudos : public MachineFunctionPass {
28 public:
29 static char ID; // Pass identification, replacement for typeid
30 ExpandPseudos() : MachineFunctionPass(ID) {}
31
32 private:
33 virtual bool runOnMachineFunction(MachineFunction &MF);
34
35 const char *getPassName() const {
36 return "Expand CodeGen Pseudo-instructions";
37 }
38
39 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 MachineFunctionPass::getAnalysisUsage(AU);
41 }
42 };
43} // end anonymous namespace
44
45char ExpandPseudos::ID = 0;
46INITIALIZE_PASS_BEGIN(ExpandPseudos, "expand-pseudos",
47 "Expand CodeGen Psueod-instructions", false, false)
48INITIALIZE_PASS_END(ExpandPseudos, "expand-pseudos",
49 "Expand CodeGen Psueod-instructions", false, false)
50
51FunctionPass *llvm::createExpandPseudosPass() {
52 return new ExpandPseudos();
53}
54
55bool ExpandPseudos::runOnMachineFunction(MachineFunction &MF) {
56 bool Changed = false;
57 const TargetLowering *TLI = MF.getTarget().getTargetLowering();
58
59 // Iterate through each instruction in the function, looking for pseudos.
60 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
61 MachineBasicBlock *MBB = I;
62 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
63 MBBI != MBBE; ) {
64 MachineInstr *MI = MBBI++;
65
66 // If MI is a pseudo, expand it.
67 const TargetInstrDesc &TID = MI->getDesc();
68 if (TID.usesCustomInsertionHook()) {
69 Changed = true;
70 MachineBasicBlock *NewMBB =
71 TLI->EmitInstrWithCustomInserter(MI, MBB);
72 // The expansion may involve new basic blocks.
73 if (NewMBB != MBB) {
74 MBB = NewMBB;
75 I = NewMBB;
76 MBBI = NewMBB->begin();
77 MBBE = NewMBB->end();
78 }
79 }
80 }
81 }
82
83 return Changed;
84}