blob: b5ec303f5d93f17f6b8dbae4f91d9086f9adaed3 [file] [log] [blame]
Dan Gohman8ec9d622010-11-18 18:45:06 +00001//===-- llvm/CodeGen/ExpandISelPseudos.cpp ----------------------*- C++ -*-===//
Dan Gohman668ac2f2010-11-16 21:02:37 +00002//
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
Dan Gohman8ec9d622010-11-18 18:45:06 +000017#define DEBUG_TYPE "expand-isel-pseudos"
Dan Gohman668ac2f2010-11-16 21:02:37 +000018#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 {
Dan Gohman8ec9d622010-11-18 18:45:06 +000027 class ExpandISelPseudos : public MachineFunctionPass {
Dan Gohman668ac2f2010-11-16 21:02:37 +000028 public:
29 static char ID; // Pass identification, replacement for typeid
Dan Gohman8ec9d622010-11-18 18:45:06 +000030 ExpandISelPseudos() : MachineFunctionPass(ID) {}
Dan Gohman668ac2f2010-11-16 21:02:37 +000031
32 private:
33 virtual bool runOnMachineFunction(MachineFunction &MF);
34
35 const char *getPassName() const {
Dan Gohman8ec9d622010-11-18 18:45:06 +000036 return "Expand ISel Pseudo-instructions";
Dan Gohman668ac2f2010-11-16 21:02:37 +000037 }
38
39 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 MachineFunctionPass::getAnalysisUsage(AU);
41 }
42 };
43} // end anonymous namespace
44
Dan Gohman8ec9d622010-11-18 18:45:06 +000045char ExpandISelPseudos::ID = 0;
46INITIALIZE_PASS(ExpandISelPseudos, "expand-isel-pseudos",
Dan Gohman4229c622010-11-18 17:44:17 +000047 "Expand CodeGen Pseudo-instructions", false, false)
Dan Gohman668ac2f2010-11-16 21:02:37 +000048
Dan Gohman8ec9d622010-11-18 18:45:06 +000049FunctionPass *llvm::createExpandISelPseudosPass() {
50 return new ExpandISelPseudos();
Dan Gohman668ac2f2010-11-16 21:02:37 +000051}
52
Dan Gohman8ec9d622010-11-18 18:45:06 +000053bool ExpandISelPseudos::runOnMachineFunction(MachineFunction &MF) {
Dan Gohman668ac2f2010-11-16 21:02:37 +000054 bool Changed = false;
55 const TargetLowering *TLI = MF.getTarget().getTargetLowering();
56
57 // Iterate through each instruction in the function, looking for pseudos.
58 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
59 MachineBasicBlock *MBB = I;
60 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
61 MBBI != MBBE; ) {
62 MachineInstr *MI = MBBI++;
63
64 // If MI is a pseudo, expand it.
65 const TargetInstrDesc &TID = MI->getDesc();
66 if (TID.usesCustomInsertionHook()) {
67 Changed = true;
68 MachineBasicBlock *NewMBB =
69 TLI->EmitInstrWithCustomInserter(MI, MBB);
70 // The expansion may involve new basic blocks.
71 if (NewMBB != MBB) {
72 MBB = NewMBB;
73 I = NewMBB;
74 MBBI = NewMBB->begin();
75 MBBE = NewMBB->end();
76 }
77 }
78 }
79 }
80
81 return Changed;
82}