blob: a27c2e3c39f6a1dfe00c1e7d5d12980d8d32309d [file] [log] [blame]
Dan Gohmanc2b78612010-11-18 18:45:06 +00001//===-- llvm/CodeGen/ExpandISelPseudos.cpp ----------------------*- C++ -*-===//
Dan Gohman8b67c722010-11-16 21:02:37 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Dan Gohman8b67c722010-11-16 21:02:37 +00006//
7//===----------------------------------------------------------------------===//
8//
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00009// Expand Pseudo-instructions produced by ISel. These are usually to allow
Dan Gohman8b67c722010-11-16 21:02:37 +000010// the expansion to contain control flow, such as a conditional move
11// implemented with a conditional branch and a phi, or an atomic operation
12// implemented with a loop.
13//
14//===----------------------------------------------------------------------===//
15
Dan Gohman8b67c722010-11-16 21:02:37 +000016#include "llvm/CodeGen/MachineFunction.h"
17#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000018#include "llvm/CodeGen/Passes.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000019#include "llvm/CodeGen/TargetLowering.h"
20#include "llvm/CodeGen/TargetSubtargetInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Support/Debug.h"
Dan Gohman8b67c722010-11-16 21:02:37 +000022using namespace llvm;
23
Chandler Carruth1b9dde02014-04-22 02:02:50 +000024#define DEBUG_TYPE "expand-isel-pseudos"
25
Dan Gohman8b67c722010-11-16 21:02:37 +000026namespace {
Dan Gohmanc2b78612010-11-18 18:45:06 +000027 class ExpandISelPseudos : public MachineFunctionPass {
Dan Gohman8b67c722010-11-16 21:02:37 +000028 public:
29 static char ID; // Pass identification, replacement for typeid
Dan Gohmanc2b78612010-11-18 18:45:06 +000030 ExpandISelPseudos() : MachineFunctionPass(ID) {}
Dan Gohman8b67c722010-11-16 21:02:37 +000031
32 private:
Craig Topper4584cd52014-03-07 09:26:03 +000033 bool runOnMachineFunction(MachineFunction &MF) override;
Dan Gohman8b67c722010-11-16 21:02:37 +000034
Craig Topper4584cd52014-03-07 09:26:03 +000035 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dan Gohman8b67c722010-11-16 21:02:37 +000036 MachineFunctionPass::getAnalysisUsage(AU);
37 }
38 };
39} // end anonymous namespace
40
Dan Gohmanc2b78612010-11-18 18:45:06 +000041char ExpandISelPseudos::ID = 0;
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000042char &llvm::ExpandISelPseudosID = ExpandISelPseudos::ID;
Matthias Braun1527baa2017-05-25 21:26:32 +000043INITIALIZE_PASS(ExpandISelPseudos, DEBUG_TYPE,
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000044 "Expand ISel Pseudo-instructions", false, false)
Dan Gohman8b67c722010-11-16 21:02:37 +000045
Dan Gohmanc2b78612010-11-18 18:45:06 +000046bool ExpandISelPseudos::runOnMachineFunction(MachineFunction &MF) {
Dan Gohman8b67c722010-11-16 21:02:37 +000047 bool Changed = false;
Eric Christopherfc6de422014-08-05 02:39:49 +000048 const TargetLowering *TLI = MF.getSubtarget().getTargetLowering();
Dan Gohman8b67c722010-11-16 21:02:37 +000049
50 // Iterate through each instruction in the function, looking for pseudos.
51 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +000052 MachineBasicBlock *MBB = &*I;
Dan Gohman8b67c722010-11-16 21:02:37 +000053 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
54 MBBI != MBBE; ) {
Duncan P. N. Exon Smitha62287b2016-06-30 23:09:39 +000055 MachineInstr &MI = *MBBI++;
Dan Gohman8b67c722010-11-16 21:02:37 +000056
57 // If MI is a pseudo, expand it.
Duncan P. N. Exon Smitha62287b2016-06-30 23:09:39 +000058 if (MI.usesCustomInsertionHook()) {
Dan Gohman8b67c722010-11-16 21:02:37 +000059 Changed = true;
Duncan P. N. Exon Smitha62287b2016-06-30 23:09:39 +000060 MachineBasicBlock *NewMBB = TLI->EmitInstrWithCustomInserter(MI, MBB);
Dan Gohman8b67c722010-11-16 21:02:37 +000061 // The expansion may involve new basic blocks.
62 if (NewMBB != MBB) {
63 MBB = NewMBB;
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +000064 I = NewMBB->getIterator();
Dan Gohman8b67c722010-11-16 21:02:37 +000065 MBBI = NewMBB->begin();
66 MBBE = NewMBB->end();
67 }
68 }
69 }
70 }
71
72 return Changed;
73}