//=- WebAssemblyFixBrTableDefaults.cpp - Fix br_table default branch targets -// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// /// /// \file This file implements a pass that eliminates redundant range checks /// guarding br_table instructions. Since jump tables on most targets cannot /// handle out of range indices, LLVM emits these checks before most jump /// tables. But br_table takes a default branch target as an argument, so it /// does not need the range checks. /// //===----------------------------------------------------------------------===// #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" #include "WebAssembly.h" #include "WebAssemblySubtarget.h" #include "llvm/CodeGen/MachineFunction.h" #include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineRegisterInfo.h" #include "llvm/Pass.h" usingnamespacellvm; #define DEBUG_TYPE … namespace { class WebAssemblyFixBrTableDefaults final : public MachineFunctionPass { … }; char WebAssemblyFixBrTableDefaults::ID = …; // Target independent selection dag assumes that it is ok to use PointerTy // as the index for a "switch", whereas Wasm so far only has a 32-bit br_table. // See e.g. SelectionDAGBuilder::visitJumpTableHeader // We have a 64-bit br_table in the tablegen defs as a result, which does get // selected, and thus we get incorrect truncates/extensions happening on // wasm64. Here we fix that. void fixBrTableIndex(MachineInstr &MI, MachineBasicBlock *MBB, MachineFunction &MF) { … } // `MI` is a br_table instruction with a dummy default target argument. This // function finds and adds the default target argument and removes any redundant // range check preceding the br_table. Returns the MBB that the br_table is // moved into so it can be removed from further consideration, or nullptr if the // br_table cannot be optimized. MachineBasicBlock *fixBrTableDefault(MachineInstr &MI, MachineBasicBlock *MBB, MachineFunction &MF) { … } bool WebAssemblyFixBrTableDefaults::runOnMachineFunction(MachineFunction &MF) { … } } // end anonymous namespace INITIALIZE_PASS(…) FunctionPass *llvm::createWebAssemblyFixBrTableDefaults() { … }