1bf02399aSReid Kleckner //===----- X86AvoidTrailingCall.cpp - Insert int3 after trailing calls ----===//
2bf02399aSReid Kleckner //
3bf02399aSReid Kleckner // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bf02399aSReid Kleckner // See https://llvm.org/LICENSE.txt for license information.
5bf02399aSReid Kleckner // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bf02399aSReid Kleckner //
7bf02399aSReid Kleckner //===----------------------------------------------------------------------===//
8bf02399aSReid Kleckner //
95ff5ddd0SReid Kleckner // The Windows x64 unwinder decodes the instruction stream during unwinding.
105ff5ddd0SReid Kleckner // The unwinder decodes forward from the current PC to detect epilogue code
115ff5ddd0SReid Kleckner // patterns.
125ff5ddd0SReid Kleckner //
135ff5ddd0SReid Kleckner // First, this means that there must be an instruction after every
145ff5ddd0SReid Kleckner // call instruction for the unwinder to decode. LLVM must maintain the invariant
155ff5ddd0SReid Kleckner // that the last instruction of a function or funclet is not a call, or the
165ff5ddd0SReid Kleckner // unwinder may decode into the next function. Similarly, a call may not
175ff5ddd0SReid Kleckner // immediately precede an epilogue code pattern. As of this writing, the
185ff5ddd0SReid Kleckner // SEH_Epilogue pseudo instruction takes care of that.
195ff5ddd0SReid Kleckner //
205ff5ddd0SReid Kleckner // Second, all non-tail call jump targets must be within the *half-open*
215ff5ddd0SReid Kleckner // interval of the bounds of the function. The unwinder distinguishes between
225ff5ddd0SReid Kleckner // internal jump instructions and tail calls in an epilogue sequence by checking
235ff5ddd0SReid Kleckner // the jump target against the function bounds from the .pdata section. This
245ff5ddd0SReid Kleckner // means that the last regular MBB of an LLVM function must not be empty if
255ff5ddd0SReid Kleckner // there are regular jumps targeting it.
265ff5ddd0SReid Kleckner //
275ff5ddd0SReid Kleckner // This pass upholds these invariants by ensuring that blocks at the end of a
285ff5ddd0SReid Kleckner // function or funclet are a) not empty and b) do not end in a CALL instruction.
295ff5ddd0SReid Kleckner //
305ff5ddd0SReid Kleckner // Unwinder implementation for reference:
315ff5ddd0SReid Kleckner // https://github.com/dotnet/coreclr/blob/a9f3fc16483eecfc47fb79c362811d870be02249/src/unwinder/amd64/unwinder_amd64.cpp#L1015
32bf02399aSReid Kleckner //
33bf02399aSReid Kleckner //===----------------------------------------------------------------------===//
34bf02399aSReid Kleckner
35bf02399aSReid Kleckner #include "X86.h"
36bf02399aSReid Kleckner #include "X86InstrInfo.h"
37bf02399aSReid Kleckner #include "X86Subtarget.h"
38*989f1c72Sserge-sans-paille #include "llvm/CodeGen/MachineFunctionPass.h"
39bf02399aSReid Kleckner #include "llvm/CodeGen/MachineInstrBuilder.h"
40bf02399aSReid Kleckner
415ff5ddd0SReid Kleckner #define AVOIDCALL_DESC "X86 avoid trailing call pass"
425ff5ddd0SReid Kleckner #define AVOIDCALL_NAME "x86-avoid-trailing-call"
435ff5ddd0SReid Kleckner
445ff5ddd0SReid Kleckner #define DEBUG_TYPE AVOIDCALL_NAME
45bf02399aSReid Kleckner
46bf02399aSReid Kleckner using namespace llvm;
47bf02399aSReid Kleckner
48bf02399aSReid Kleckner namespace {
49bf02399aSReid Kleckner class X86AvoidTrailingCallPass : public MachineFunctionPass {
50bf02399aSReid Kleckner public:
X86AvoidTrailingCallPass()51bf02399aSReid Kleckner X86AvoidTrailingCallPass() : MachineFunctionPass(ID) {}
52bf02399aSReid Kleckner
53bf02399aSReid Kleckner bool runOnMachineFunction(MachineFunction &MF) override;
54bf02399aSReid Kleckner
55bf02399aSReid Kleckner static char ID;
565ff5ddd0SReid Kleckner
575ff5ddd0SReid Kleckner private:
getPassName() const585ff5ddd0SReid Kleckner StringRef getPassName() const override { return AVOIDCALL_DESC; }
59bf02399aSReid Kleckner };
605ff5ddd0SReid Kleckner } // end anonymous namespace
61bf02399aSReid Kleckner
62bf02399aSReid Kleckner char X86AvoidTrailingCallPass::ID = 0;
63bf02399aSReid Kleckner
createX86AvoidTrailingCallPass()64bf02399aSReid Kleckner FunctionPass *llvm::createX86AvoidTrailingCallPass() {
65bf02399aSReid Kleckner return new X86AvoidTrailingCallPass();
66bf02399aSReid Kleckner }
67bf02399aSReid Kleckner
INITIALIZE_PASS(X86AvoidTrailingCallPass,AVOIDCALL_NAME,AVOIDCALL_DESC,false,false)685ff5ddd0SReid Kleckner INITIALIZE_PASS(X86AvoidTrailingCallPass, AVOIDCALL_NAME, AVOIDCALL_DESC, false, false)
695ff5ddd0SReid Kleckner
70bf02399aSReid Kleckner // A real instruction is a non-meta, non-pseudo instruction. Some pseudos
71bf02399aSReid Kleckner // expand to nothing, and some expand to code. This logic conservatively assumes
72bf02399aSReid Kleckner // they might expand to nothing.
7378c27a3cSMarkus Böck static bool isCallOrRealInstruction(MachineInstr &MI) {
7478c27a3cSMarkus Böck return MI.isCall() || (!MI.isPseudo() && !MI.isMetaInstruction());
75bf02399aSReid Kleckner }
76bf02399aSReid Kleckner
77bf02399aSReid Kleckner // Return true if this is a call instruction, but not a tail call.
isCallInstruction(const MachineInstr & MI)78bf02399aSReid Kleckner static bool isCallInstruction(const MachineInstr &MI) {
79bf02399aSReid Kleckner return MI.isCall() && !MI.isReturn();
80bf02399aSReid Kleckner }
81bf02399aSReid Kleckner
runOnMachineFunction(MachineFunction & MF)82bf02399aSReid Kleckner bool X86AvoidTrailingCallPass::runOnMachineFunction(MachineFunction &MF) {
83bf02399aSReid Kleckner const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>();
84bf02399aSReid Kleckner const X86InstrInfo &TII = *STI.getInstrInfo();
85bf02399aSReid Kleckner assert(STI.isTargetWin64() && "pass only runs on Win64");
86bf02399aSReid Kleckner
875ff5ddd0SReid Kleckner // We don't need to worry about any of the invariants described above if there
885ff5ddd0SReid Kleckner // is no unwind info (CFI).
895ff5ddd0SReid Kleckner if (!MF.hasWinCFI())
905ff5ddd0SReid Kleckner return false;
915ff5ddd0SReid Kleckner
92bf02399aSReid Kleckner // FIXME: Perhaps this pass should also replace SEH_Epilogue by inserting nops
93bf02399aSReid Kleckner // before epilogues.
94bf02399aSReid Kleckner
95bf02399aSReid Kleckner bool Changed = false;
96bf02399aSReid Kleckner for (MachineBasicBlock &MBB : MF) {
97bf02399aSReid Kleckner // Look for basic blocks that precede funclet entries or are at the end of
98bf02399aSReid Kleckner // the function.
99bf02399aSReid Kleckner MachineBasicBlock *NextMBB = MBB.getNextNode();
100bf02399aSReid Kleckner if (NextMBB && !NextMBB->isEHFuncletEntry())
101bf02399aSReid Kleckner continue;
102bf02399aSReid Kleckner
1035ff5ddd0SReid Kleckner // Find the last real instruction in this block.
10478c27a3cSMarkus Böck auto LastRealInstr = llvm::find_if(reverse(MBB), isCallOrRealInstruction);
105bf02399aSReid Kleckner
1065ff5ddd0SReid Kleckner // If the block is empty or the last real instruction is a call instruction,
1075ff5ddd0SReid Kleckner // insert an int3. If there is a call instruction, insert the int3 between
1085ff5ddd0SReid Kleckner // the call and any labels or other meta instructions. If the block is
1095ff5ddd0SReid Kleckner // empty, insert at block end.
1105ff5ddd0SReid Kleckner bool IsEmpty = LastRealInstr == MBB.rend();
1115ff5ddd0SReid Kleckner bool IsCall = !IsEmpty && isCallInstruction(*LastRealInstr);
1125ff5ddd0SReid Kleckner if (IsEmpty || IsCall) {
113bf02399aSReid Kleckner LLVM_DEBUG({
1145ff5ddd0SReid Kleckner if (IsCall) {
115bf02399aSReid Kleckner dbgs() << "inserting int3 after trailing call instruction:\n";
116bf02399aSReid Kleckner LastRealInstr->dump();
117bf02399aSReid Kleckner dbgs() << '\n';
1185ff5ddd0SReid Kleckner } else {
1195ff5ddd0SReid Kleckner dbgs() << "inserting int3 in trailing empty MBB:\n";
1205ff5ddd0SReid Kleckner MBB.dump();
1215ff5ddd0SReid Kleckner }
122bf02399aSReid Kleckner });
123bf02399aSReid Kleckner
1245ff5ddd0SReid Kleckner MachineBasicBlock::iterator MBBI = MBB.end();
1255ff5ddd0SReid Kleckner DebugLoc DL;
1265ff5ddd0SReid Kleckner if (IsCall) {
1275ff5ddd0SReid Kleckner MBBI = std::next(LastRealInstr.getReverse());
1285ff5ddd0SReid Kleckner DL = LastRealInstr->getDebugLoc();
1295ff5ddd0SReid Kleckner }
1305ff5ddd0SReid Kleckner BuildMI(MBB, MBBI, DL, TII.get(X86::INT3));
131bf02399aSReid Kleckner Changed = true;
132bf02399aSReid Kleckner }
133bf02399aSReid Kleckner }
134bf02399aSReid Kleckner
135bf02399aSReid Kleckner return Changed;
136bf02399aSReid Kleckner }
137