17f50c15bSThomas Lively //=- WebAssemblyFixBrTableDefaults.cpp - Fix br_table default branch targets -// 27f50c15bSThomas Lively // 37f50c15bSThomas Lively // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 47f50c15bSThomas Lively // See https://llvm.org/LICENSE.txt for license information. 57f50c15bSThomas Lively // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 67f50c15bSThomas Lively // 77f50c15bSThomas Lively //===----------------------------------------------------------------------===// 87f50c15bSThomas Lively /// 97f50c15bSThomas Lively /// \file This file implements a pass that eliminates redundant range checks 107f50c15bSThomas Lively /// guarding br_table instructions. Since jump tables on most targets cannot 117f50c15bSThomas Lively /// handle out of range indices, LLVM emits these checks before most jump 127f50c15bSThomas Lively /// tables. But br_table takes a default branch target as an argument, so it 137f50c15bSThomas Lively /// does not need the range checks. 147f50c15bSThomas Lively /// 157f50c15bSThomas Lively //===----------------------------------------------------------------------===// 167f50c15bSThomas Lively 177f50c15bSThomas Lively #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" 187f50c15bSThomas Lively #include "WebAssembly.h" 197f50c15bSThomas Lively #include "llvm/CodeGen/MachineFunction.h" 207f50c15bSThomas Lively #include "llvm/CodeGen/MachineFunctionPass.h" 217f50c15bSThomas Lively #include "llvm/CodeGen/MachineRegisterInfo.h" 227f50c15bSThomas Lively #include "llvm/Pass.h" 237f50c15bSThomas Lively 247f50c15bSThomas Lively using namespace llvm; 257f50c15bSThomas Lively 267f50c15bSThomas Lively #define DEBUG_TYPE "wasm-fix-br-table-defaults" 277f50c15bSThomas Lively 287f50c15bSThomas Lively namespace { 297f50c15bSThomas Lively 307f50c15bSThomas Lively class WebAssemblyFixBrTableDefaults final : public MachineFunctionPass { 317f50c15bSThomas Lively StringRef getPassName() const override { 327f50c15bSThomas Lively return "WebAssembly Fix br_table Defaults"; 337f50c15bSThomas Lively } 347f50c15bSThomas Lively 357f50c15bSThomas Lively bool runOnMachineFunction(MachineFunction &MF) override; 367f50c15bSThomas Lively 377f50c15bSThomas Lively public: 387f50c15bSThomas Lively static char ID; // Pass identification, replacement for typeid 397f50c15bSThomas Lively WebAssemblyFixBrTableDefaults() : MachineFunctionPass(ID) {} 407f50c15bSThomas Lively }; 417f50c15bSThomas Lively 427f50c15bSThomas Lively char WebAssemblyFixBrTableDefaults::ID = 0; 437f50c15bSThomas Lively 447f50c15bSThomas Lively // `MI` is a br_table instruction missing its default target argument. This 457f50c15bSThomas Lively // function finds and adds the default target argument and removes any redundant 467f50c15bSThomas Lively // range check preceding the br_table. 477f50c15bSThomas Lively MachineBasicBlock *fixBrTable(MachineInstr &MI, MachineBasicBlock *MBB, 487f50c15bSThomas Lively MachineFunction &MF) { 497f50c15bSThomas Lively // Get the header block, which contains the redundant range check. 507f50c15bSThomas Lively assert(MBB->pred_size() == 1 && "Expected a single guard predecessor"); 517f50c15bSThomas Lively auto *HeaderMBB = *MBB->pred_begin(); 527f50c15bSThomas Lively 537f50c15bSThomas Lively // Find the conditional jump to the default target. If it doesn't exist, the 547f50c15bSThomas Lively // default target is unreachable anyway, so we can choose anything. 55*49754dcfSThomas Lively MachineBasicBlock *TBB = nullptr, *FBB = nullptr; 56*49754dcfSThomas Lively SmallVector<MachineOperand, 2> Cond; 57*49754dcfSThomas Lively const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); 58*49754dcfSThomas Lively TII.analyzeBranch(*HeaderMBB, TBB, FBB, Cond); 59*49754dcfSThomas Lively 60*49754dcfSThomas Lively // Here are the possible outcomes. '_' is nullptr, `J` is the jump table block 61*49754dcfSThomas Lively // aka MBB, 'D' is the default block. 62*49754dcfSThomas Lively // 63*49754dcfSThomas Lively // TBB | FBB | Meaning 64*49754dcfSThomas Lively // _ | _ | No default block, header falls through to jump table 65*49754dcfSThomas Lively // J | _ | No default block, header jumps to the jump table 66*49754dcfSThomas Lively // D | _ | Header jumps to the default and falls through to the jump table 67*49754dcfSThomas Lively // D | J | Header jumps to the default and also to the jump table 68*49754dcfSThomas Lively if (TBB && TBB != MBB) { 69*49754dcfSThomas Lively // Install the default target. 70*49754dcfSThomas Lively assert((FBB == nullptr || FBB == MBB) && 71*49754dcfSThomas Lively "Expected jump or fallthrough to br_table block"); 72*49754dcfSThomas Lively MI.addOperand(MF, MachineOperand::CreateMBB(TBB)); 737f50c15bSThomas Lively } else { 747f50c15bSThomas Lively // Arbitrarily choose the first jump target as the default. 757f50c15bSThomas Lively auto *SomeMBB = MI.getOperand(1).getMBB(); 767f50c15bSThomas Lively MI.addOperand(MachineOperand::CreateMBB(SomeMBB)); 777f50c15bSThomas Lively } 787f50c15bSThomas Lively 79*49754dcfSThomas Lively // Remove any branches from the header and splice in the jump table instead 80*49754dcfSThomas Lively TII.removeBranch(*HeaderMBB, nullptr); 817f50c15bSThomas Lively HeaderMBB->splice(HeaderMBB->end(), MBB, MBB->begin(), MBB->end()); 827f50c15bSThomas Lively 837f50c15bSThomas Lively // Update CFG to skip the old jump table block. Remove shared successors 847f50c15bSThomas Lively // before transferring to avoid duplicated successors. 857f50c15bSThomas Lively HeaderMBB->removeSuccessor(MBB); 867f50c15bSThomas Lively for (auto &Succ : MBB->successors()) 877f50c15bSThomas Lively if (HeaderMBB->isSuccessor(Succ)) 887f50c15bSThomas Lively HeaderMBB->removeSuccessor(Succ); 897f50c15bSThomas Lively HeaderMBB->transferSuccessorsAndUpdatePHIs(MBB); 907f50c15bSThomas Lively 917f50c15bSThomas Lively // Remove the old jump table block from the function 927f50c15bSThomas Lively MF.erase(MBB); 937f50c15bSThomas Lively 947f50c15bSThomas Lively return HeaderMBB; 957f50c15bSThomas Lively } 967f50c15bSThomas Lively 977f50c15bSThomas Lively bool WebAssemblyFixBrTableDefaults::runOnMachineFunction(MachineFunction &MF) { 987f50c15bSThomas Lively LLVM_DEBUG(dbgs() << "********** Fixing br_table Default Targets **********\n" 997f50c15bSThomas Lively "********** Function: " 1007f50c15bSThomas Lively << MF.getName() << '\n'); 1017f50c15bSThomas Lively 1027f50c15bSThomas Lively bool Changed = false; 1037f50c15bSThomas Lively SmallPtrSet<MachineBasicBlock *, 16> MBBSet; 1047f50c15bSThomas Lively for (auto &MBB : MF) 1057f50c15bSThomas Lively MBBSet.insert(&MBB); 1067f50c15bSThomas Lively 1077f50c15bSThomas Lively while (!MBBSet.empty()) { 1087f50c15bSThomas Lively MachineBasicBlock *MBB = *MBBSet.begin(); 1097f50c15bSThomas Lively MBBSet.erase(MBB); 1107f50c15bSThomas Lively for (auto &MI : *MBB) { 1117f50c15bSThomas Lively if (WebAssembly::isBrTable(MI)) { 1127f50c15bSThomas Lively auto *Fixed = fixBrTable(MI, MBB, MF); 1137f50c15bSThomas Lively MBBSet.erase(Fixed); 1147f50c15bSThomas Lively Changed = true; 1157f50c15bSThomas Lively break; 1167f50c15bSThomas Lively } 1177f50c15bSThomas Lively } 1187f50c15bSThomas Lively } 1197f50c15bSThomas Lively 1207f50c15bSThomas Lively if (Changed) { 1217f50c15bSThomas Lively // We rewrote part of the function; recompute relevant things. 1227f50c15bSThomas Lively MF.RenumberBlocks(); 1237f50c15bSThomas Lively return true; 1247f50c15bSThomas Lively } 1257f50c15bSThomas Lively 1267f50c15bSThomas Lively return false; 1277f50c15bSThomas Lively } 1287f50c15bSThomas Lively 1297f50c15bSThomas Lively } // end anonymous namespace 1307f50c15bSThomas Lively 1317f50c15bSThomas Lively INITIALIZE_PASS(WebAssemblyFixBrTableDefaults, DEBUG_TYPE, 1327f50c15bSThomas Lively "Removes range checks and sets br_table default targets", false, 1332f671c42SMikael Holmen false) 1347f50c15bSThomas Lively 1357f50c15bSThomas Lively FunctionPass *llvm::createWebAssemblyFixBrTableDefaults() { 1367f50c15bSThomas Lively return new WebAssemblyFixBrTableDefaults(); 1377f50c15bSThomas Lively } 138