blob: 05b1e6a24e65042a0f18a5a24ffa9e4b3704c1e5 [file] [log] [blame]
Dan Gohmanc2b78612010-11-18 18:45:06 +00001//===-- llvm/CodeGen/ExpandISelPseudos.cpp ----------------------*- C++ -*-===//
Dan Gohman8b67c722010-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//
Chris Lattner0ab5e2c2011-04-15 05:18:47 +000010// Expand Pseudo-instructions produced by ISel. These are usually to allow
Dan Gohman8b67c722010-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
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/CodeGen/Passes.h"
Dan Gohman8b67c722010-11-16 21:02:37 +000018#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Support/Debug.h"
Dan Gohman8b67c722010-11-16 21:02:37 +000021#include "llvm/Target/TargetLowering.h"
22#include "llvm/Target/TargetMachine.h"
Eric Christopherd9134482014-08-04 21:25:23 +000023#include "llvm/Target/TargetSubtargetInfo.h"
Dan Gohman8b67c722010-11-16 21:02:37 +000024using namespace llvm;
25
Chandler Carruth1b9dde02014-04-22 02:02:50 +000026#define DEBUG_TYPE "expand-isel-pseudos"
27
Dan Gohman8b67c722010-11-16 21:02:37 +000028namespace {
Dan Gohmanc2b78612010-11-18 18:45:06 +000029 class ExpandISelPseudos : public MachineFunctionPass {
Dan Gohman8b67c722010-11-16 21:02:37 +000030 public:
31 static char ID; // Pass identification, replacement for typeid
Dan Gohmanc2b78612010-11-18 18:45:06 +000032 ExpandISelPseudos() : MachineFunctionPass(ID) {}
Dan Gohman8b67c722010-11-16 21:02:37 +000033
34 private:
Craig Topper4584cd52014-03-07 09:26:03 +000035 bool runOnMachineFunction(MachineFunction &MF) override;
Dan Gohman8b67c722010-11-16 21:02:37 +000036
Craig Topper4584cd52014-03-07 09:26:03 +000037 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dan Gohman8b67c722010-11-16 21:02:37 +000038 MachineFunctionPass::getAnalysisUsage(AU);
39 }
40 };
41} // end anonymous namespace
42
Dan Gohmanc2b78612010-11-18 18:45:06 +000043char ExpandISelPseudos::ID = 0;
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000044char &llvm::ExpandISelPseudosID = ExpandISelPseudos::ID;
Dan Gohmanc2b78612010-11-18 18:45:06 +000045INITIALIZE_PASS(ExpandISelPseudos, "expand-isel-pseudos",
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000046 "Expand ISel Pseudo-instructions", false, false)
Dan Gohman8b67c722010-11-16 21:02:37 +000047
Dan Gohmanc2b78612010-11-18 18:45:06 +000048bool ExpandISelPseudos::runOnMachineFunction(MachineFunction &MF) {
Dan Gohman8b67c722010-11-16 21:02:37 +000049 bool Changed = false;
Eric Christopherfc6de422014-08-05 02:39:49 +000050 const TargetLowering *TLI = MF.getSubtarget().getTargetLowering();
Dan Gohman8b67c722010-11-16 21:02:37 +000051
52 // Iterate through each instruction in the function, looking for pseudos.
53 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
54 MachineBasicBlock *MBB = I;
55 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
56 MBBI != MBBE; ) {
57 MachineInstr *MI = MBBI++;
58
59 // If MI is a pseudo, expand it.
Evan Cheng7f8e5632011-12-07 07:15:52 +000060 if (MI->usesCustomInsertionHook()) {
Dan Gohman8b67c722010-11-16 21:02:37 +000061 Changed = true;
62 MachineBasicBlock *NewMBB =
63 TLI->EmitInstrWithCustomInserter(MI, MBB);
64 // The expansion may involve new basic blocks.
65 if (NewMBB != MBB) {
66 MBB = NewMBB;
67 I = NewMBB;
68 MBBI = NewMBB->begin();
69 MBBE = NewMBB->end();
70 }
71 }
72 }
73 }
74
75 return Changed;
76}