blob: cfc7c3567c5d51d2d8941ebd4b96e2c9d60d6192 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//== llvm/CodeGen/GlobalISel/Localizer.h - Localizer -------------*- C++ -*-==//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9/// \file This file describes the interface of the Localizer pass.
10/// This pass moves/duplicates constant-like instructions close to their uses.
11/// Its primarily goal is to workaround the deficiencies of the fast register
12/// allocator.
13/// With GlobalISel constants are all materialized in the entry block of
14/// a function. However, the fast allocator cannot rematerialize constants and
15/// has a lot more live-ranges to deal with and will most likely end up
16/// spilling a lot.
17/// By pushing the constants close to their use, we only create small
18/// live-ranges.
19//===----------------------------------------------------------------------===//
20
21#ifndef LLVM_CODEGEN_GLOBALISEL_LOCALIZER_H
22#define LLVM_CODEGEN_GLOBALISEL_LOCALIZER_H
23
24#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
25#include "llvm/CodeGen/MachineFunctionPass.h"
26
27namespace llvm {
28// Forward declarations.
29class MachineRegisterInfo;
30
31/// This pass implements the localization mechanism described at the
32/// top of this file. One specificity of the implementation is that
33/// it will materialize one and only one instance of a constant per
34/// basic block, thus enabling reuse of that constant within that block.
35/// Moreover, it only materializes constants in blocks where they
36/// are used. PHI uses are considered happening at the end of the
37/// related predecessor.
38class Localizer : public MachineFunctionPass {
39public:
40 static char ID;
41
42private:
43 /// MRI contains all the register class/bank information that this
44 /// pass uses and updates.
45 MachineRegisterInfo *MRI;
46
47 /// Check whether or not \p MI needs to be moved close to its uses.
48 static bool shouldLocalize(const MachineInstr &MI);
49
50 /// Check if \p MOUse is used in the same basic block as \p Def.
51 /// If the use is in the same block, we say it is local.
52 /// When the use is not local, \p InsertMBB will contain the basic
53 /// block when to insert \p Def to have a local use.
54 static bool isLocalUse(MachineOperand &MOUse, const MachineInstr &Def,
55 MachineBasicBlock *&InsertMBB);
56
57 /// Initialize the field members using \p MF.
58 void init(MachineFunction &MF);
59
60public:
61 Localizer();
62
63 StringRef getPassName() const override { return "Localizer"; }
64
65 MachineFunctionProperties getRequiredProperties() const override {
66 return MachineFunctionProperties()
67 .set(MachineFunctionProperties::Property::IsSSA)
68 .set(MachineFunctionProperties::Property::Legalized)
69 .set(MachineFunctionProperties::Property::RegBankSelected);
70 }
71
Andrew Scullcdfcccc2018-10-05 20:58:37 +010072 void getAnalysisUsage(AnalysisUsage &AU) const override;
73
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010074 bool runOnMachineFunction(MachineFunction &MF) override;
75};
76
77} // End namespace llvm.
78
79#endif