Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 1 | //===-- WebAssemblyRegNumbering.cpp - Register Numbering ------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [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 |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// This file implements a pass which assigns WebAssembly register |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 11 | /// numbers for CodeGen virtual registers. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include "WebAssembly.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 17 | #include "WebAssemblyMachineFunctionInfo.h" |
| 18 | #include "WebAssemblySubtarget.h" |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 19 | #include "WebAssemblyUtilities.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SCCIterator.h" |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Dan Gohman | 8394756 | 2016-01-20 05:54:22 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFunction.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 24 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 25 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 26 | #include "llvm/CodeGen/Passes.h" |
| 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | using namespace llvm; |
| 30 | |
| 31 | #define DEBUG_TYPE "wasm-reg-numbering" |
| 32 | |
| 33 | namespace { |
| 34 | class WebAssemblyRegNumbering final : public MachineFunctionPass { |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 35 | StringRef getPassName() const override { |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 36 | return "WebAssembly Register Numbering"; |
| 37 | } |
| 38 | |
| 39 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 40 | AU.setPreservesCFG(); |
| 41 | MachineFunctionPass::getAnalysisUsage(AU); |
| 42 | } |
| 43 | |
| 44 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 45 | |
| 46 | public: |
| 47 | static char ID; // Pass identification, replacement for typeid |
| 48 | WebAssemblyRegNumbering() : MachineFunctionPass(ID) {} |
| 49 | }; |
| 50 | } // end anonymous namespace |
| 51 | |
| 52 | char WebAssemblyRegNumbering::ID = 0; |
Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 53 | INITIALIZE_PASS(WebAssemblyRegNumbering, DEBUG_TYPE, |
| 54 | "Assigns WebAssembly register numbers for virtual registers", |
| 55 | false, false) |
| 56 | |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 57 | FunctionPass *llvm::createWebAssemblyRegNumbering() { |
| 58 | return new WebAssemblyRegNumbering(); |
| 59 | } |
| 60 | |
| 61 | bool WebAssemblyRegNumbering::runOnMachineFunction(MachineFunction &MF) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 62 | LLVM_DEBUG(dbgs() << "********** Register Numbering **********\n" |
| 63 | "********** Function: " |
| 64 | << MF.getName() << '\n'); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 65 | |
| 66 | WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); |
| 67 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 68 | |
| 69 | MFI.initWARegs(); |
| 70 | |
| 71 | // WebAssembly argument registers are in the same index space as local |
| 72 | // variables. Assign the numbers for them first. |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 73 | MachineBasicBlock &EntryMBB = MF.front(); |
| 74 | for (MachineInstr &MI : EntryMBB) { |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 75 | if (!WebAssembly::isArgument(MI)) |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 76 | break; |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 77 | |
| 78 | int64_t Imm = MI.getOperand(1).getImm(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 79 | LLVM_DEBUG(dbgs() << "Arg VReg " << MI.getOperand(0).getReg() |
| 80 | << " -> WAReg " << Imm << "\n"); |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 81 | MFI.setWAReg(MI.getOperand(0).getReg(), Imm); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // Then assign regular WebAssembly registers for all remaining used |
Dan Gohman | 08d58bc | 2015-12-23 00:22:04 +0000 | [diff] [blame] | 85 | // virtual registers. TODO: Consider sorting the registers by frequency of |
| 86 | // use, to maximize usage of small immediate fields. |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 87 | unsigned NumVRegs = MF.getRegInfo().getNumVirtRegs(); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 88 | unsigned NumStackRegs = 0; |
Derek Schuff | 6519468 | 2016-01-23 01:20:43 +0000 | [diff] [blame] | 89 | // Start the numbering for locals after the arg regs |
| 90 | unsigned CurReg = MFI.getParams().size(); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 91 | for (unsigned VRegIdx = 0; VRegIdx < NumVRegs; ++VRegIdx) { |
| 92 | unsigned VReg = TargetRegisterInfo::index2VirtReg(VRegIdx); |
Dan Gohman | 899cb5a | 2016-01-25 16:48:44 +0000 | [diff] [blame] | 93 | // Skip unused registers. |
| 94 | if (MRI.use_empty(VReg)) |
| 95 | continue; |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 96 | // Handle stackified registers. |
| 97 | if (MFI.isVRegStackified(VReg)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 98 | LLVM_DEBUG(dbgs() << "VReg " << VReg << " -> WAReg " |
| 99 | << (INT32_MIN | NumStackRegs) << "\n"); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 100 | MFI.setWAReg(VReg, INT32_MIN | NumStackRegs++); |
| 101 | continue; |
| 102 | } |
Derek Schuff | 6519468 | 2016-01-23 01:20:43 +0000 | [diff] [blame] | 103 | if (MFI.getWAReg(VReg) == WebAssemblyFunctionInfo::UnusedReg) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 104 | LLVM_DEBUG(dbgs() << "VReg " << VReg << " -> WAReg " << CurReg << "\n"); |
Derek Schuff | 6519468 | 2016-01-23 01:20:43 +0000 | [diff] [blame] | 105 | MFI.setWAReg(VReg, CurReg++); |
| 106 | } |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 107 | } |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 108 | |
| 109 | return true; |
| 110 | } |