1 //== llvm/CodeGen/GlobalISel/Localizer.h - Localizer -------------*- C++ -*-==//
3 // 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
7 //===----------------------------------------------------------------------===//
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
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
17 /// By pushing the constants close to their use, we only create small
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_CODEGEN_GLOBALISEL_LOCALIZER_H
22 #define LLVM_CODEGEN_GLOBALISEL_LOCALIZER_H
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
29 // Forward declarations.
30 class MachineRegisterInfo;
31 class TargetTransformInfo;
33 /// This pass implements the localization mechanism described at the
34 /// top of this file. One specificity of the implementation is that
35 /// it will materialize one and only one instance of a constant per
36 /// basic block, thus enabling reuse of that constant within that block.
37 /// Moreover, it only materializes constants in blocks where they
38 /// are used. PHI uses are considered happening at the end of the
39 /// related predecessor.
40 class Localizer : public MachineFunctionPass {
45 /// An input function to decide if the pass should run or not
46 /// on the given MachineFunction.
47 std::function<bool(const MachineFunction &)> DoNotRunPass;
49 /// MRI contains all the register class/bank information that this
50 /// pass uses and updates.
51 MachineRegisterInfo *MRI;
52 /// TTI used for getting remat costs for instructions.
53 TargetTransformInfo *TTI;
55 /// Check whether or not \p MI needs to be moved close to its uses.
56 bool shouldLocalize(const MachineInstr &MI);
58 /// Check if \p MOUse is used in the same basic block as \p Def.
59 /// If the use is in the same block, we say it is local.
60 /// When the use is not local, \p InsertMBB will contain the basic
61 /// block when to insert \p Def to have a local use.
62 static bool isLocalUse(MachineOperand &MOUse, const MachineInstr &Def,
63 MachineBasicBlock *&InsertMBB);
65 /// Initialize the field members using \p MF.
66 void init(MachineFunction &MF);
68 typedef SmallSetVector<MachineInstr *, 32> LocalizedSetVecT;
70 /// Do inter-block localization from the entry block.
71 bool localizeInterBlock(MachineFunction &MF,
72 LocalizedSetVecT &LocalizedInstrs);
74 /// Do intra-block localization of already localized instructions.
75 bool localizeIntraBlock(LocalizedSetVecT &LocalizedInstrs);
79 Localizer(std::function<bool(const MachineFunction &)>);
81 StringRef getPassName() const override { return "Localizer"; }
83 MachineFunctionProperties getRequiredProperties() const override {
84 return MachineFunctionProperties()
85 .set(MachineFunctionProperties::Property::IsSSA)
86 .set(MachineFunctionProperties::Property::Legalized)
87 .set(MachineFunctionProperties::Property::RegBankSelected);
90 void getAnalysisUsage(AnalysisUsage &AU) const override;
92 bool runOnMachineFunction(MachineFunction &MF) override;
95 } // End namespace llvm.