1*7f50c15bSThomas Lively //=- WebAssemblyFixBrTableDefaults.cpp - Fix br_table default branch targets -//
2*7f50c15bSThomas Lively //
3*7f50c15bSThomas Lively // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*7f50c15bSThomas Lively // See https://llvm.org/LICENSE.txt for license information.
5*7f50c15bSThomas Lively // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*7f50c15bSThomas Lively //
7*7f50c15bSThomas Lively //===----------------------------------------------------------------------===//
8*7f50c15bSThomas Lively ///
9*7f50c15bSThomas Lively /// \file This file implements a pass that eliminates redundant range checks
10*7f50c15bSThomas Lively /// guarding br_table instructions. Since jump tables on most targets cannot
11*7f50c15bSThomas Lively /// handle out of range indices, LLVM emits these checks before most jump
12*7f50c15bSThomas Lively /// tables. But br_table takes a default branch target as an argument, so it
13*7f50c15bSThomas Lively /// does not need the range checks.
14*7f50c15bSThomas Lively ///
15*7f50c15bSThomas Lively //===----------------------------------------------------------------------===//
16*7f50c15bSThomas Lively 
17*7f50c15bSThomas Lively #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
18*7f50c15bSThomas Lively #include "WebAssembly.h"
19*7f50c15bSThomas Lively #include "llvm/CodeGen/MachineFunction.h"
20*7f50c15bSThomas Lively #include "llvm/CodeGen/MachineFunctionPass.h"
21*7f50c15bSThomas Lively #include "llvm/CodeGen/MachineRegisterInfo.h"
22*7f50c15bSThomas Lively #include "llvm/Pass.h"
23*7f50c15bSThomas Lively 
24*7f50c15bSThomas Lively using namespace llvm;
25*7f50c15bSThomas Lively 
26*7f50c15bSThomas Lively #define DEBUG_TYPE "wasm-fix-br-table-defaults"
27*7f50c15bSThomas Lively 
28*7f50c15bSThomas Lively namespace {
29*7f50c15bSThomas Lively 
30*7f50c15bSThomas Lively class WebAssemblyFixBrTableDefaults final : public MachineFunctionPass {
31*7f50c15bSThomas Lively   StringRef getPassName() const override {
32*7f50c15bSThomas Lively     return "WebAssembly Fix br_table Defaults";
33*7f50c15bSThomas Lively   }
34*7f50c15bSThomas Lively 
35*7f50c15bSThomas Lively   bool runOnMachineFunction(MachineFunction &MF) override;
36*7f50c15bSThomas Lively 
37*7f50c15bSThomas Lively public:
38*7f50c15bSThomas Lively   static char ID; // Pass identification, replacement for typeid
39*7f50c15bSThomas Lively   WebAssemblyFixBrTableDefaults() : MachineFunctionPass(ID) {}
40*7f50c15bSThomas Lively };
41*7f50c15bSThomas Lively 
42*7f50c15bSThomas Lively char WebAssemblyFixBrTableDefaults::ID = 0;
43*7f50c15bSThomas Lively 
44*7f50c15bSThomas Lively // `MI` is a br_table instruction missing its default target argument. This
45*7f50c15bSThomas Lively // function finds and adds the default target argument and removes any redundant
46*7f50c15bSThomas Lively // range check preceding the br_table.
47*7f50c15bSThomas Lively MachineBasicBlock *fixBrTable(MachineInstr &MI, MachineBasicBlock *MBB,
48*7f50c15bSThomas Lively                               MachineFunction &MF) {
49*7f50c15bSThomas Lively   // Get the header block, which contains the redundant range check.
50*7f50c15bSThomas Lively   assert(MBB->pred_size() == 1 && "Expected a single guard predecessor");
51*7f50c15bSThomas Lively   auto *HeaderMBB = *MBB->pred_begin();
52*7f50c15bSThomas Lively 
53*7f50c15bSThomas Lively   // Find the conditional jump to the default target. If it doesn't exist, the
54*7f50c15bSThomas Lively   // default target is unreachable anyway, so we can choose anything.
55*7f50c15bSThomas Lively   auto JumpMII = --HeaderMBB->end();
56*7f50c15bSThomas Lively   while (JumpMII->getOpcode() != WebAssembly::BR_IF &&
57*7f50c15bSThomas Lively          JumpMII != HeaderMBB->begin()) {
58*7f50c15bSThomas Lively     --JumpMII;
59*7f50c15bSThomas Lively   }
60*7f50c15bSThomas Lively   if (JumpMII->getOpcode() == WebAssembly::BR_IF) {
61*7f50c15bSThomas Lively     // Install the default target and remove the jumps in the header.
62*7f50c15bSThomas Lively     auto *DefaultMBB = JumpMII->getOperand(0).getMBB();
63*7f50c15bSThomas Lively     assert(DefaultMBB != MBB && "Expected conditional jump to default target");
64*7f50c15bSThomas Lively     MI.addOperand(MF, MachineOperand::CreateMBB(DefaultMBB));
65*7f50c15bSThomas Lively     HeaderMBB->erase(JumpMII, HeaderMBB->end());
66*7f50c15bSThomas Lively   } else {
67*7f50c15bSThomas Lively     // Arbitrarily choose the first jump target as the default.
68*7f50c15bSThomas Lively     auto *SomeMBB = MI.getOperand(1).getMBB();
69*7f50c15bSThomas Lively     MI.addOperand(MachineOperand::CreateMBB(SomeMBB));
70*7f50c15bSThomas Lively   }
71*7f50c15bSThomas Lively 
72*7f50c15bSThomas Lively   // Splice the jump table into the header.
73*7f50c15bSThomas Lively   HeaderMBB->splice(HeaderMBB->end(), MBB, MBB->begin(), MBB->end());
74*7f50c15bSThomas Lively 
75*7f50c15bSThomas Lively   // Update CFG to skip the old jump table block. Remove shared successors
76*7f50c15bSThomas Lively   // before transferring to avoid duplicated successors.
77*7f50c15bSThomas Lively   HeaderMBB->removeSuccessor(MBB);
78*7f50c15bSThomas Lively   for (auto &Succ : MBB->successors())
79*7f50c15bSThomas Lively     if (HeaderMBB->isSuccessor(Succ))
80*7f50c15bSThomas Lively       HeaderMBB->removeSuccessor(Succ);
81*7f50c15bSThomas Lively   HeaderMBB->transferSuccessorsAndUpdatePHIs(MBB);
82*7f50c15bSThomas Lively 
83*7f50c15bSThomas Lively   // Remove the old jump table block from the function
84*7f50c15bSThomas Lively   MF.erase(MBB);
85*7f50c15bSThomas Lively 
86*7f50c15bSThomas Lively   return HeaderMBB;
87*7f50c15bSThomas Lively }
88*7f50c15bSThomas Lively 
89*7f50c15bSThomas Lively bool WebAssemblyFixBrTableDefaults::runOnMachineFunction(MachineFunction &MF) {
90*7f50c15bSThomas Lively   LLVM_DEBUG(dbgs() << "********** Fixing br_table Default Targets **********\n"
91*7f50c15bSThomas Lively                        "********** Function: "
92*7f50c15bSThomas Lively                     << MF.getName() << '\n');
93*7f50c15bSThomas Lively 
94*7f50c15bSThomas Lively   bool Changed = false;
95*7f50c15bSThomas Lively   SmallPtrSet<MachineBasicBlock *, 16> MBBSet;
96*7f50c15bSThomas Lively   for (auto &MBB : MF)
97*7f50c15bSThomas Lively     MBBSet.insert(&MBB);
98*7f50c15bSThomas Lively 
99*7f50c15bSThomas Lively   while (!MBBSet.empty()) {
100*7f50c15bSThomas Lively     MachineBasicBlock *MBB = *MBBSet.begin();
101*7f50c15bSThomas Lively     MBBSet.erase(MBB);
102*7f50c15bSThomas Lively     for (auto &MI : *MBB) {
103*7f50c15bSThomas Lively       if (WebAssembly::isBrTable(MI)) {
104*7f50c15bSThomas Lively         auto *Fixed = fixBrTable(MI, MBB, MF);
105*7f50c15bSThomas Lively         MBBSet.erase(Fixed);
106*7f50c15bSThomas Lively         Changed = true;
107*7f50c15bSThomas Lively         break;
108*7f50c15bSThomas Lively       }
109*7f50c15bSThomas Lively     }
110*7f50c15bSThomas Lively   }
111*7f50c15bSThomas Lively 
112*7f50c15bSThomas Lively   if (Changed) {
113*7f50c15bSThomas Lively     // We rewrote part of the function; recompute relevant things.
114*7f50c15bSThomas Lively     MF.RenumberBlocks();
115*7f50c15bSThomas Lively     return true;
116*7f50c15bSThomas Lively   }
117*7f50c15bSThomas Lively 
118*7f50c15bSThomas Lively   return false;
119*7f50c15bSThomas Lively }
120*7f50c15bSThomas Lively 
121*7f50c15bSThomas Lively } // end anonymous namespace
122*7f50c15bSThomas Lively 
123*7f50c15bSThomas Lively INITIALIZE_PASS(WebAssemblyFixBrTableDefaults, DEBUG_TYPE,
124*7f50c15bSThomas Lively                 "Removes range checks and sets br_table default targets", false,
125*7f50c15bSThomas Lively                 false);
126*7f50c15bSThomas Lively 
127*7f50c15bSThomas Lively FunctionPass *llvm::createWebAssemblyFixBrTableDefaults() {
128*7f50c15bSThomas Lively   return new WebAssemblyFixBrTableDefaults();
129*7f50c15bSThomas Lively }
130