Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 1 | //=--- RegUsageInfoPropagate.cpp - Register Usage Informartion Propagation --=// |
| 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 | /// This pass is required to take advantage of the interprocedural register |
| 11 | /// allocation infrastructure. |
| 12 | /// |
| 13 | /// This pass iterates through MachineInstrs in a given MachineFunction and at |
| 14 | /// each callsite queries RegisterUsageInfo for RegMask (calculated based on |
| 15 | /// actual register allocation) of the callee function, if the RegMask detail |
| 16 | /// is available then this pass will update the RegMask of the call instruction. |
| 17 | /// This updated RegMask will be used by the register allocator while allocating |
| 18 | /// the current MachineFunction. |
| 19 | /// |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 23 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Matt Arsenault | ee106fc | 2017-08-24 07:55:13 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineInstr.h" |
| 26 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 27 | #include "llvm/CodeGen/Passes.h" |
| 28 | #include "llvm/CodeGen/RegisterUsageInfo.h" |
| 29 | #include "llvm/IR/Module.h" |
| 30 | #include "llvm/PassAnalysisSupport.h" |
| 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
| 33 | #include "llvm/Target/TargetMachine.h" |
| 34 | #include <map> |
| 35 | #include <string> |
| 36 | |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | |
| 39 | #define DEBUG_TYPE "ip-regalloc" |
| 40 | |
| 41 | #define RUIP_NAME "Register Usage Information Propagation" |
| 42 | |
| 43 | namespace { |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 44 | |
Matthias Braun | 3a7b5a6 | 2018-07-26 00:27:51 +0000 | [diff] [blame] | 45 | class RegUsageInfoPropagation : public MachineFunctionPass { |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 46 | public: |
Matthias Braun | 3a7b5a6 | 2018-07-26 00:27:51 +0000 | [diff] [blame] | 47 | RegUsageInfoPropagation() : MachineFunctionPass(ID) { |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 48 | PassRegistry &Registry = *PassRegistry::getPassRegistry(); |
Matthias Braun | 3a7b5a6 | 2018-07-26 00:27:51 +0000 | [diff] [blame] | 49 | initializeRegUsageInfoPropagationPass(Registry); |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Mehdi Amini | 67f335d | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 52 | StringRef getPassName() const override { return RUIP_NAME; } |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 53 | |
| 54 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 55 | |
Matthias Braun | 3a7b5a6 | 2018-07-26 00:27:51 +0000 | [diff] [blame] | 56 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 57 | AU.addRequired<PhysicalRegisterUsageInfo>(); |
| 58 | AU.setPreservesAll(); |
| 59 | MachineFunctionPass::getAnalysisUsage(AU); |
| 60 | } |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 61 | |
| 62 | static char ID; |
| 63 | |
| 64 | private: |
Matthias Braun | 3a7b5a6 | 2018-07-26 00:27:51 +0000 | [diff] [blame] | 65 | static void setRegMask(MachineInstr &MI, ArrayRef<uint32_t> RegMask) { |
| 66 | assert(RegMask.size() == |
| 67 | MachineOperand::getRegMaskSize(MI.getParent()->getParent() |
| 68 | ->getRegInfo().getTargetRegisterInfo() |
| 69 | ->getNumRegs()) |
| 70 | && "expected register mask size"); |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 71 | for (MachineOperand &MO : MI.operands()) { |
| 72 | if (MO.isRegMask()) |
Matthias Braun | 3a7b5a6 | 2018-07-26 00:27:51 +0000 | [diff] [blame] | 73 | MO.setRegMask(RegMask.data()); |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | }; |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 77 | |
Matthias Braun | 3a7b5a6 | 2018-07-26 00:27:51 +0000 | [diff] [blame] | 78 | } // end of anonymous namespace |
| 79 | |
| 80 | INITIALIZE_PASS_BEGIN(RegUsageInfoPropagation, "reg-usage-propagation", |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 81 | RUIP_NAME, false, false) |
| 82 | INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo) |
Matthias Braun | 3a7b5a6 | 2018-07-26 00:27:51 +0000 | [diff] [blame] | 83 | INITIALIZE_PASS_END(RegUsageInfoPropagation, "reg-usage-propagation", |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 84 | RUIP_NAME, false, false) |
| 85 | |
Matthias Braun | 3a7b5a6 | 2018-07-26 00:27:51 +0000 | [diff] [blame] | 86 | char RegUsageInfoPropagation::ID = 0; |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 87 | |
Matt Arsenault | 1a6aed2 | 2017-08-24 07:55:15 +0000 | [diff] [blame] | 88 | // Assumes call instructions have a single reference to a function. |
Matthias Braun | 3a7b5a6 | 2018-07-26 00:27:51 +0000 | [diff] [blame] | 89 | static const Function *findCalledFunction(const Module &M, |
| 90 | const MachineInstr &MI) { |
| 91 | for (const MachineOperand &MO : MI.operands()) { |
Matt Arsenault | 1a6aed2 | 2017-08-24 07:55:15 +0000 | [diff] [blame] | 92 | if (MO.isGlobal()) |
Matthias Braun | 3a7b5a6 | 2018-07-26 00:27:51 +0000 | [diff] [blame] | 93 | return dyn_cast<const Function>(MO.getGlobal()); |
Matt Arsenault | 1a6aed2 | 2017-08-24 07:55:15 +0000 | [diff] [blame] | 94 | |
| 95 | if (MO.isSymbol()) |
| 96 | return M.getFunction(MO.getSymbolName()); |
| 97 | } |
| 98 | |
| 99 | return nullptr; |
| 100 | } |
| 101 | |
Matthias Braun | 3a7b5a6 | 2018-07-26 00:27:51 +0000 | [diff] [blame] | 102 | bool RegUsageInfoPropagation::runOnMachineFunction(MachineFunction &MF) { |
| 103 | const Module &M = *MF.getFunction().getParent(); |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 104 | PhysicalRegisterUsageInfo *PRUI = &getAnalysis<PhysicalRegisterUsageInfo>(); |
| 105 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 106 | LLVM_DEBUG(dbgs() << " ++++++++++++++++++++ " << getPassName() |
| 107 | << " ++++++++++++++++++++ \n"); |
| 108 | LLVM_DEBUG(dbgs() << "MachineFunction : " << MF.getName() << "\n"); |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 109 | |
Matt Arsenault | ee106fc | 2017-08-24 07:55:13 +0000 | [diff] [blame] | 110 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 111 | if (!MFI.hasCalls() && !MFI.hasTailCall()) |
| 112 | return false; |
| 113 | |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 114 | bool Changed = false; |
| 115 | |
| 116 | for (MachineBasicBlock &MBB : MF) { |
| 117 | for (MachineInstr &MI : MBB) { |
| 118 | if (!MI.isCall()) |
| 119 | continue; |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 120 | LLVM_DEBUG( |
| 121 | dbgs() |
| 122 | << "Call Instruction Before Register Usage Info Propagation : \n"); |
| 123 | LLVM_DEBUG(dbgs() << MI << "\n"); |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 124 | |
Matthias Braun | 3a7b5a6 | 2018-07-26 00:27:51 +0000 | [diff] [blame] | 125 | auto UpdateRegMask = [&](const Function &F) { |
| 126 | const ArrayRef<uint32_t> RegMask = PRUI->getRegUsageInfo(F); |
| 127 | if (RegMask.empty()) |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 128 | return; |
Matthias Braun | 3a7b5a6 | 2018-07-26 00:27:51 +0000 | [diff] [blame] | 129 | setRegMask(MI, RegMask); |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 130 | Changed = true; |
| 131 | }; |
| 132 | |
Matthias Braun | 3a7b5a6 | 2018-07-26 00:27:51 +0000 | [diff] [blame] | 133 | if (const Function *F = findCalledFunction(M, MI)) { |
| 134 | UpdateRegMask(*F); |
Matt Arsenault | 1a6aed2 | 2017-08-24 07:55:15 +0000 | [diff] [blame] | 135 | } else { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 136 | LLVM_DEBUG(dbgs() << "Failed to find call target function\n"); |
Matt Arsenault | 1a6aed2 | 2017-08-24 07:55:15 +0000 | [diff] [blame] | 137 | } |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 138 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 139 | LLVM_DEBUG( |
| 140 | dbgs() << "Call Instruction After Register Usage Info Propagation : " |
| 141 | << MI << '\n'); |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 145 | LLVM_DEBUG( |
| 146 | dbgs() << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" |
| 147 | "++++++ \n"); |
Mehdi Amini | 13c1e25 | 2016-06-10 18:37:21 +0000 | [diff] [blame] | 148 | return Changed; |
| 149 | } |
Matthias Braun | 3a7b5a6 | 2018-07-26 00:27:51 +0000 | [diff] [blame] | 150 | |
| 151 | FunctionPass *llvm::createRegUsageInfoPropPass() { |
| 152 | return new RegUsageInfoPropagation(); |
| 153 | } |