Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //== llvm/CodeGen/GlobalISel/Localizer.h - Localizer -------------*- C++ -*-==// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 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 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 24 | #include "llvm/ADT/SetVector.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 25 | #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" |
| 26 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 27 | |
| 28 | namespace llvm { |
| 29 | // Forward declarations. |
| 30 | class MachineRegisterInfo; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 31 | class TargetTransformInfo; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 32 | |
| 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 { |
| 41 | public: |
| 42 | static char ID; |
| 43 | |
| 44 | private: |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 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; |
| 48 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 49 | /// MRI contains all the register class/bank information that this |
| 50 | /// pass uses and updates. |
| 51 | MachineRegisterInfo *MRI; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 52 | /// TTI used for getting remat costs for instructions. |
| 53 | TargetTransformInfo *TTI; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 54 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 55 | /// Check if \p MOUse is used in the same basic block as \p Def. |
| 56 | /// If the use is in the same block, we say it is local. |
| 57 | /// When the use is not local, \p InsertMBB will contain the basic |
| 58 | /// block when to insert \p Def to have a local use. |
| 59 | static bool isLocalUse(MachineOperand &MOUse, const MachineInstr &Def, |
| 60 | MachineBasicBlock *&InsertMBB); |
| 61 | |
| 62 | /// Initialize the field members using \p MF. |
| 63 | void init(MachineFunction &MF); |
| 64 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 65 | typedef SmallSetVector<MachineInstr *, 32> LocalizedSetVecT; |
| 66 | |
| 67 | /// Do inter-block localization from the entry block. |
| 68 | bool localizeInterBlock(MachineFunction &MF, |
| 69 | LocalizedSetVecT &LocalizedInstrs); |
| 70 | |
| 71 | /// Do intra-block localization of already localized instructions. |
| 72 | bool localizeIntraBlock(LocalizedSetVecT &LocalizedInstrs); |
| 73 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 74 | public: |
| 75 | Localizer(); |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 76 | Localizer(std::function<bool(const MachineFunction &)>); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 77 | |
| 78 | StringRef getPassName() const override { return "Localizer"; } |
| 79 | |
| 80 | MachineFunctionProperties getRequiredProperties() const override { |
| 81 | return MachineFunctionProperties() |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 82 | .set(MachineFunctionProperties::Property::IsSSA); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 85 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 86 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 87 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 88 | }; |
| 89 | |
| 90 | } // End namespace llvm. |
| 91 | |
| 92 | #endif |