Path: blob/main/contrib/llvm-project/llvm/lib/Target/X86/X86AvoidTrailingCall.cpp
35269 views
//===----- X86AvoidTrailingCall.cpp - Insert int3 after trailing calls ----===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// The Windows x64 unwinder decodes the instruction stream during unwinding.9// The unwinder decodes forward from the current PC to detect epilogue code10// patterns.11//12// First, this means that there must be an instruction after every13// call instruction for the unwinder to decode. LLVM must maintain the invariant14// that the last instruction of a function or funclet is not a call, or the15// unwinder may decode into the next function. Similarly, a call may not16// immediately precede an epilogue code pattern. As of this writing, the17// SEH_Epilogue pseudo instruction takes care of that.18//19// Second, all non-tail call jump targets must be within the *half-open*20// interval of the bounds of the function. The unwinder distinguishes between21// internal jump instructions and tail calls in an epilogue sequence by checking22// the jump target against the function bounds from the .pdata section. This23// means that the last regular MBB of an LLVM function must not be empty if24// there are regular jumps targeting it.25//26// This pass upholds these invariants by ensuring that blocks at the end of a27// function or funclet are a) not empty and b) do not end in a CALL instruction.28//29// Unwinder implementation for reference:30// https://github.com/dotnet/coreclr/blob/a9f3fc16483eecfc47fb79c362811d870be02249/src/unwinder/amd64/unwinder_amd64.cpp#L101531//32//===----------------------------------------------------------------------===//3334#include "X86.h"35#include "X86InstrInfo.h"36#include "X86Subtarget.h"37#include "llvm/CodeGen/MachineFunctionPass.h"38#include "llvm/CodeGen/MachineInstrBuilder.h"3940#define AVOIDCALL_DESC "X86 avoid trailing call pass"41#define AVOIDCALL_NAME "x86-avoid-trailing-call"4243#define DEBUG_TYPE AVOIDCALL_NAME4445using namespace llvm;4647namespace {48class X86AvoidTrailingCallPass : public MachineFunctionPass {49public:50X86AvoidTrailingCallPass() : MachineFunctionPass(ID) {}5152bool runOnMachineFunction(MachineFunction &MF) override;5354static char ID;5556private:57StringRef getPassName() const override { return AVOIDCALL_DESC; }58};59} // end anonymous namespace6061char X86AvoidTrailingCallPass::ID = 0;6263FunctionPass *llvm::createX86AvoidTrailingCallPass() {64return new X86AvoidTrailingCallPass();65}6667INITIALIZE_PASS(X86AvoidTrailingCallPass, AVOIDCALL_NAME, AVOIDCALL_DESC, false, false)6869// A real instruction is a non-meta, non-pseudo instruction. Some pseudos70// expand to nothing, and some expand to code. This logic conservatively assumes71// they might expand to nothing.72static bool isCallOrRealInstruction(MachineInstr &MI) {73return MI.isCall() || (!MI.isPseudo() && !MI.isMetaInstruction());74}7576// Return true if this is a call instruction, but not a tail call.77static bool isCallInstruction(const MachineInstr &MI) {78return MI.isCall() && !MI.isReturn();79}8081bool X86AvoidTrailingCallPass::runOnMachineFunction(MachineFunction &MF) {82const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>();83const X86InstrInfo &TII = *STI.getInstrInfo();84assert(STI.isTargetWin64() && "pass only runs on Win64");8586// We don't need to worry about any of the invariants described above if there87// is no unwind info (CFI).88if (!MF.hasWinCFI())89return false;9091// FIXME: Perhaps this pass should also replace SEH_Epilogue by inserting nops92// before epilogues.9394bool Changed = false;95for (MachineBasicBlock &MBB : MF) {96// Look for basic blocks that precede funclet entries or are at the end of97// the function.98MachineBasicBlock *NextMBB = MBB.getNextNode();99if (NextMBB && !NextMBB->isEHFuncletEntry())100continue;101102// Find the last real instruction in this block.103auto LastRealInstr = llvm::find_if(reverse(MBB), isCallOrRealInstruction);104105// If the block is empty or the last real instruction is a call instruction,106// insert an int3. If there is a call instruction, insert the int3 between107// the call and any labels or other meta instructions. If the block is108// empty, insert at block end.109bool IsEmpty = LastRealInstr == MBB.rend();110bool IsCall = !IsEmpty && isCallInstruction(*LastRealInstr);111if (IsEmpty || IsCall) {112LLVM_DEBUG({113if (IsCall) {114dbgs() << "inserting int3 after trailing call instruction:\n";115LastRealInstr->dump();116dbgs() << '\n';117} else {118dbgs() << "inserting int3 in trailing empty MBB:\n";119MBB.dump();120}121});122123MachineBasicBlock::iterator MBBI = MBB.end();124DebugLoc DL;125if (IsCall) {126MBBI = std::next(LastRealInstr.getReverse());127DL = LastRealInstr->getDebugLoc();128}129BuildMI(MBB, MBBI, DL, TII.get(X86::INT3));130Changed = true;131}132}133134return Changed;135}136137138