LLVM  9.0.0svn
WebAssemblyEHRestoreStackPointer.cpp
Go to the documentation of this file.
1 //===-- WebAssemblyEHRestoreStackPointer.cpp - __stack_pointer restoration ===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// After the stack is unwound due to a thrown exception, the __stack_pointer
11 /// global can point to an invalid address. This inserts instructions that
12 /// restore __stack_pointer global.
13 ///
14 //===----------------------------------------------------------------------===//
15 
17 #include "WebAssembly.h"
18 #include "WebAssemblySubtarget.h"
19 #include "WebAssemblyUtilities.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "wasm-eh-restore-stack-pointer"
25 
26 namespace {
27 class WebAssemblyEHRestoreStackPointer final : public MachineFunctionPass {
28 public:
29  static char ID; // Pass identification, replacement for typeid
30  WebAssemblyEHRestoreStackPointer() : MachineFunctionPass(ID) {}
31 
32  StringRef getPassName() const override {
33  return "WebAssembly Restore Stack Pointer for Exception Handling";
34  }
35 
36  void getAnalysisUsage(AnalysisUsage &AU) const override {
37  AU.setPreservesCFG();
39  }
40 
41  bool runOnMachineFunction(MachineFunction &MF) override;
42 };
43 } // end anonymous namespace
44 
46 INITIALIZE_PASS(WebAssemblyEHRestoreStackPointer, DEBUG_TYPE,
47  "Restore Stack Pointer for Exception Handling", true, false)
48 
50  return new WebAssemblyEHRestoreStackPointer();
51 }
52 
53 bool WebAssemblyEHRestoreStackPointer::runOnMachineFunction(
54  MachineFunction &MF) {
55  LLVM_DEBUG(dbgs() << "********** EH Restore Stack Pointer **********\n"
56  "********** Function: "
57  << MF.getName() << '\n');
58 
59  const auto *FrameLowering = static_cast<const WebAssemblyFrameLowering *>(
61  if (!FrameLowering->needsPrologForEH(MF))
62  return false;
63  bool Changed = false;
64 
65  for (auto &MBB : MF) {
66  if (!MBB.isEHPad())
67  continue;
68  Changed = true;
69 
70  // Insert __stack_pointer restoring instructions at the beginning of each EH
71  // pad, after the catch instruction. (Catch instructions may have been
72  // reordered, and catch_all instructions have not been inserted yet, but
73  // those cases are handled in LateEHPrepare).
74  //
75  // Here it is safe to assume that SP32 holds the latest value of
76  // __stack_pointer, because the only exception for this case is when a
77  // function uses the red zone, but that only happens with leaf functions,
78  // and we don't restore __stack_pointer in leaf functions anyway.
79  auto InsertPos = MBB.begin();
80  if (MBB.begin()->getOpcode() == WebAssembly::CATCH)
81  InsertPos++;
82  FrameLowering->writeSPToGlobal(WebAssembly::SP32, MF, MBB, InsertPos,
83  MBB.begin()->getDebugLoc());
84  }
85  return Changed;
86 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
FunctionPass * createWebAssemblyEHRestoreStackPointer()
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
This file contains the declaration of the WebAssembly-specific utility functions. ...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
This file provides WebAssembly-specific target descriptions.
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
This file declares the WebAssembly-specific subclass of TargetSubtarget.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:285
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
virtual const TargetFrameLowering * getFrameLowering() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
#define LLVM_DEBUG(X)
Definition: Debug.h:122
INITIALIZE_PASS(WebAssemblyEHRestoreStackPointer, DEBUG_TYPE, "Restore Stack Pointer for Exception Handling", true, false) FunctionPass *llvm