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