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