blob: 0ec79c2e69f94ed148014cb9838e288d3c364cd1 [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"
Eric Christopherd9134482014-08-04 21:25:23 +000022#include "llvm/Target/TargetSubtargetInfo.h"
Dan Gohman8b67c722010-11-16 21:02:37 +000023using namespace llvm;
24
Chandler Carruth1b9dde02014-04-22 02:02:50 +000025#define DEBUG_TYPE "expand-isel-pseudos"
26
Dan Gohman8b67c722010-11-16 21:02:37 +000027namespace {
Dan Gohmanc2b78612010-11-18 18:45:06 +000028 class ExpandISelPseudos : public MachineFunctionPass {
Dan Gohman8b67c722010-11-16 21:02:37 +000029 public:
30 static char ID; // Pass identification, replacement for typeid
Dan Gohmanc2b78612010-11-18 18:45:06 +000031 ExpandISelPseudos() : MachineFunctionPass(ID) {}
Dan Gohman8b67c722010-11-16 21:02:37 +000032
33 private:
Craig Topper4584cd52014-03-07 09:26:03 +000034 bool runOnMachineFunction(MachineFunction &MF) override;
Dan Gohman8b67c722010-11-16 21:02:37 +000035
Craig Topper4584cd52014-03-07 09:26:03 +000036 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dan Gohman8b67c722010-11-16 21:02:37 +000037 MachineFunctionPass::getAnalysisUsage(AU);
38 }
39 };
40} // end anonymous namespace
41
Dan Gohmanc2b78612010-11-18 18:45:06 +000042char ExpandISelPseudos::ID = 0;
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000043char &llvm::ExpandISelPseudosID = ExpandISelPseudos::ID;
Dan Gohmanc2b78612010-11-18 18:45:06 +000044INITIALIZE_PASS(ExpandISelPseudos, "expand-isel-pseudos",
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000045 "Expand ISel Pseudo-instructions", false, false)
Dan Gohman8b67c722010-11-16 21:02:37 +000046
Dan Gohmanc2b78612010-11-18 18:45:06 +000047bool ExpandISelPseudos::runOnMachineFunction(MachineFunction &MF) {
Dan Gohman8b67c722010-11-16 21:02:37 +000048 bool Changed = false;
Eric Christopherfc6de422014-08-05 02:39:49 +000049 const TargetLowering *TLI = MF.getSubtarget().getTargetLowering();
Dan Gohman8b67c722010-11-16 21:02:37 +000050
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 Smithd83547a2015-10-09 18:44:40 +000053 MachineBasicBlock *MBB = &*I;
Dan Gohman8b67c722010-11-16 21:02:37 +000054 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
55 MBBI != MBBE; ) {
Duncan P. N. Exon Smitha62287b2016-06-30 23:09:39 +000056 MachineInstr &MI = *MBBI++;
Dan Gohman8b67c722010-11-16 21:02:37 +000057
58 // If MI is a pseudo, expand it.
Duncan P. N. Exon Smitha62287b2016-06-30 23:09:39 +000059 if (MI.usesCustomInsertionHook()) {
Dan Gohman8b67c722010-11-16 21:02:37 +000060 Changed = true;
Duncan P. N. Exon Smitha62287b2016-06-30 23:09:39 +000061 MachineBasicBlock *NewMBB = TLI->EmitInstrWithCustomInserter(MI, MBB);
Dan Gohman8b67c722010-11-16 21:02:37 +000062 // The expansion may involve new basic blocks.
63 if (NewMBB != MBB) {
64 MBB = NewMBB;
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +000065 I = NewMBB->getIterator();
Dan Gohman8b67c722010-11-16 21:02:37 +000066 MBBI = NewMBB->begin();
67 MBBE = NewMBB->end();
68 }
69 }
70 }
71 }
72
73 return Changed;
74}