blob: 00040e92a829546b6eb7acbcfc8f0ee532a3f8c6 [file] [log] [blame]
Matt Arsenault9cac4e62019-06-19 00:25:39 +00001//===-- llvm/CodeGen/FinalizeISel.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//
Matt Arsenault9cac4e62019-06-19 00:25:39 +00009/// This pass expands Pseudo-instructions produced by ISel, fixes register
10/// reservations and may do machine frame information adjustments.
11/// The pseudo instructions are used to allow the expansion to contain control
12/// flow, such as a conditional move implemented with a conditional branch and a
13/// phi, or an atomic operation implemented with a loop.
Dan Gohman8b67c722010-11-16 21:02:37 +000014//
15//===----------------------------------------------------------------------===//
16
Dan Gohman8b67c722010-11-16 21:02:37 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "llvm/CodeGen/Passes.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000020#include "llvm/CodeGen/TargetLowering.h"
21#include "llvm/CodeGen/TargetSubtargetInfo.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080022#include "llvm/InitializePasses.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Support/Debug.h"
Dan Gohman8b67c722010-11-16 21:02:37 +000024using namespace llvm;
25
Matt Arsenault9cac4e62019-06-19 00:25:39 +000026#define DEBUG_TYPE "finalize-isel"
Chandler Carruth1b9dde02014-04-22 02:02:50 +000027
Dan Gohman8b67c722010-11-16 21:02:37 +000028namespace {
Matt Arsenault9cac4e62019-06-19 00:25:39 +000029 class FinalizeISel : public MachineFunctionPass {
Dan Gohman8b67c722010-11-16 21:02:37 +000030 public:
31 static char ID; // Pass identification, replacement for typeid
Matt Arsenault9cac4e62019-06-19 00:25:39 +000032 FinalizeISel() : 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
Matt Arsenault9cac4e62019-06-19 00:25:39 +000043char FinalizeISel::ID = 0;
44char &llvm::FinalizeISelID = FinalizeISel::ID;
45INITIALIZE_PASS(FinalizeISel, DEBUG_TYPE,
46 "Finalize ISel and expand pseudo-instructions", false, false)
Dan Gohman8b67c722010-11-16 21:02:37 +000047
Matt Arsenault9cac4e62019-06-19 00:25:39 +000048bool FinalizeISel::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) {
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +000054 MachineBasicBlock *MBB = &*I;
Dan Gohman8b67c722010-11-16 21:02:37 +000055 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
56 MBBI != MBBE; ) {
Duncan P. N. Exon Smitha62287b2016-06-30 23:09:39 +000057 MachineInstr &MI = *MBBI++;
Dan Gohman8b67c722010-11-16 21:02:37 +000058
59 // If MI is a pseudo, expand it.
Duncan P. N. Exon Smitha62287b2016-06-30 23:09:39 +000060 if (MI.usesCustomInsertionHook()) {
Dan Gohman8b67c722010-11-16 21:02:37 +000061 Changed = true;
Duncan P. N. Exon Smitha62287b2016-06-30 23:09:39 +000062 MachineBasicBlock *NewMBB = TLI->EmitInstrWithCustomInserter(MI, MBB);
Dan Gohman8b67c722010-11-16 21:02:37 +000063 // The expansion may involve new basic blocks.
64 if (NewMBB != MBB) {
65 MBB = NewMBB;
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +000066 I = NewMBB->getIterator();
Dan Gohman8b67c722010-11-16 21:02:37 +000067 MBBI = NewMBB->begin();
68 MBBE = NewMBB->end();
69 }
70 }
71 }
72 }
73
Matt Arsenault9cac4e62019-06-19 00:25:39 +000074 TLI->finalizeLowering(MF);
75
Dan Gohman8b67c722010-11-16 21:02:37 +000076 return Changed;
77}