blob: 772d7f71bb37e375551b9e9f06516e2281b8be5e [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Support/Debug.h"
Dan Gohman8b67c722010-11-16 21:02:37 +000023using namespace llvm;
24
Matt Arsenault9cac4e62019-06-19 00:25:39 +000025#define DEBUG_TYPE "finalize-isel"
Chandler Carruth1b9dde02014-04-22 02:02:50 +000026
Dan Gohman8b67c722010-11-16 21:02:37 +000027namespace {
Matt Arsenault9cac4e62019-06-19 00:25:39 +000028 class FinalizeISel : public MachineFunctionPass {
Dan Gohman8b67c722010-11-16 21:02:37 +000029 public:
30 static char ID; // Pass identification, replacement for typeid
Matt Arsenault9cac4e62019-06-19 00:25:39 +000031 FinalizeISel() : 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
Matt Arsenault9cac4e62019-06-19 00:25:39 +000042char FinalizeISel::ID = 0;
43char &llvm::FinalizeISelID = FinalizeISel::ID;
44INITIALIZE_PASS(FinalizeISel, DEBUG_TYPE,
45 "Finalize ISel and expand pseudo-instructions", false, false)
Dan Gohman8b67c722010-11-16 21:02:37 +000046
Matt Arsenault9cac4e62019-06-19 00:25:39 +000047bool FinalizeISel::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
Matt Arsenault9cac4e62019-06-19 00:25:39 +000073 TLI->finalizeLowering(MF);
74
Dan Gohman8b67c722010-11-16 21:02:37 +000075 return Changed;
76}