LLVM 24.0.0git
ARMAvoidTrailingCall.cpp
Go to the documentation of this file.
1//===-- ARMAvoidTrailingCall.cpp - Trap after trailing calls on Windows ---===//
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// The Windows unwinder finds the function or funclet of a frame from its
10// return address. A call that ends a function or funclet returns into the
11// next one, so put a trap after it.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ARM.h"
16#include "ARMBaseInstrInfo.h"
17#include "ARMSubtarget.h"
18#include "llvm/ADT/STLExtras.h"
21
22using namespace llvm;
23
24#define DEBUG_TYPE "arm-avoid-trailing-call"
25#define ARM_AVOID_TRAILING_CALL_NAME "ARM avoid trailing call"
26
27namespace {
28class ARMAvoidTrailingCall : public MachineFunctionPass {
29public:
30 static char ID;
31 ARMAvoidTrailingCall() : MachineFunctionPass(ID) {}
32 bool runOnMachineFunction(MachineFunction &MF) override;
33 StringRef getPassName() const override {
35 }
36};
37} // end anonymous namespace
38
39char ARMAvoidTrailingCall::ID = 0;
40
42 false, false)
43
45 return new ARMAvoidTrailingCall();
46}
47
48bool ARMAvoidTrailingCall::runOnMachineFunction(MachineFunction &MF) {
49 if (!MF.usesWindowsUnwindInfo())
50 return false;
51
52 const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
53 const ARMBaseInstrInfo &TII = *STI.getInstrInfo();
54 bool Changed = false;
55 for (MachineBasicBlock &MBB : MF) {
56 MachineBasicBlock *Next = MBB.getNextNode();
57 if (Next && !Next->isEHFuncletEntry())
58 continue;
59 auto Last = find_if(reverse(MBB), [](const MachineInstr &MI) {
60 return !MI.isMetaInstruction() && !isSEHInstruction(MI);
61 });
62 // An empty block needs a trap too: an invoke of a noreturn function falls
63 // through to its empty normal destination.
64 bool IsEmpty = Last == MBB.rend();
65 bool IsCall = !IsEmpty && Last->isCall() && !Last->isReturn();
66 if (!IsEmpty && !IsCall)
67 continue;
69 TII.get(STI.isThumb() ? ARM::tTRAP : ARM::TRAP));
70 Changed = true;
71 }
72 return Changed;
73}
#define ARM_AVOID_TRAILING_CALL_NAME
MachineBasicBlock & MBB
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file contains some templates that are useful if you are working with the STL at all.
const ARMBaseInstrInfo * getInstrInfo() const override
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool usesWindowsUnwindInfo() const
True if this function describes its unwind information with Windows SEH unwind opcodes (....
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
static bool isSEHInstruction(const MachineInstr &MI)
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1788
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147
FunctionPass * createARMAvoidTrailingCallPass()