Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions llvm/lib/Target/X86/X86.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#ifndef LLVM_LIB_TARGET_X86_X86_H
#define LLVM_LIB_TARGET_X86_X86_H

#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
#include "llvm/IR/Analysis.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Support/CodeGen.h"
Expand Down Expand Up @@ -104,7 +105,19 @@ FunctionPass *createX86LowerTileCopyPass();
/// CALL instruction. The pass does the same for each funclet as well. This
/// ensures that the open interval of function start and end PCs contains all
/// return addresses for the benefit of the Windows x64 unwinder.
FunctionPass *createX86AvoidTrailingCallPass();
class X86AvoidTrailingCallPass
: public PassInfoMixin<X86AvoidTrailingCallPass> {
private:
const X86TargetMachine *TM;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Premerge CI is failing because TM is unused. We should be able to remove it from here, the constructor, and drop the this argument to the constructor call for this pass in X86PassRegistry.def.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks!


public:
X86AvoidTrailingCallPass(const X86TargetMachine *TM) : TM(TM) {}
PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM);
static bool isRequired() { return true; }
};

FunctionPass *createX86AvoidTrailingCallLegacyPass();

/// Return a pass that optimizes the code-size of x86 call sequences. This is
/// done by replacing esp-relative movs with pushes.
Expand Down Expand Up @@ -222,7 +235,7 @@ void initializeX86FixupInstTuningPassPass(PassRegistry &);
void initializeX86FixupVectorConstantsPassPass(PassRegistry &);
void initializeWinEHStatePassPass(PassRegistry &);
void initializeX86AvoidSFBPassPass(PassRegistry &);
void initializeX86AvoidTrailingCallPassPass(PassRegistry &);
void initializeX86AvoidTrailingCallLegacyPassPass(PassRegistry &);
void initializeX86CallFrameOptimizationPass(PassRegistry &);
void initializeX86CmovConverterPassPass(PassRegistry &);
void initializeX86DAGToDAGISelLegacyPass(PassRegistry &);
Expand Down
33 changes: 26 additions & 7 deletions llvm/lib/Target/X86/X86AvoidTrailingCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include "X86Subtarget.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/IR/Analysis.h"
#include "llvm/IR/PassManager.h"

#define AVOIDCALL_DESC "X86 avoid trailing call pass"
#define AVOIDCALL_NAME "x86-avoid-trailing-call"
Expand All @@ -46,9 +48,9 @@
using namespace llvm;

namespace {
class X86AvoidTrailingCallPass : public MachineFunctionPass {
class X86AvoidTrailingCallLegacyPass : public MachineFunctionPass {
public:
X86AvoidTrailingCallPass() : MachineFunctionPass(ID) {}
X86AvoidTrailingCallLegacyPass() : MachineFunctionPass(ID) {}

bool runOnMachineFunction(MachineFunction &MF) override;

Expand All @@ -59,13 +61,14 @@ class X86AvoidTrailingCallPass : public MachineFunctionPass {
};
} // end anonymous namespace

char X86AvoidTrailingCallPass::ID = 0;
char X86AvoidTrailingCallLegacyPass::ID = 0;

FunctionPass *llvm::createX86AvoidTrailingCallPass() {
return new X86AvoidTrailingCallPass();
FunctionPass *llvm::createX86AvoidTrailingCallLegacyPass() {
return new X86AvoidTrailingCallLegacyPass();
}

INITIALIZE_PASS(X86AvoidTrailingCallPass, AVOIDCALL_NAME, AVOIDCALL_DESC, false, false)
INITIALIZE_PASS(X86AvoidTrailingCallLegacyPass, AVOIDCALL_NAME, AVOIDCALL_DESC,
false, false)

// A real instruction is a non-meta, non-pseudo instruction. Some pseudos
// expand to nothing, and some expand to code. This logic conservatively assumes
Expand All @@ -79,7 +82,7 @@ static bool isCallInstruction(const MachineInstr &MI) {
return MI.isCall() && !MI.isReturn();
}

bool X86AvoidTrailingCallPass::runOnMachineFunction(MachineFunction &MF) {
bool UpdatedOnX86AvoidTrailingCallPass(MachineFunction &MF) {
const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>();
const X86InstrInfo &TII = *STI.getInstrInfo();
assert(STI.isTargetWin64() && "pass only runs on Win64");
Expand Down Expand Up @@ -134,3 +137,19 @@ bool X86AvoidTrailingCallPass::runOnMachineFunction(MachineFunction &MF) {

return Changed;
}

bool X86AvoidTrailingCallLegacyPass::runOnMachineFunction(MachineFunction &MF) {
return UpdatedOnX86AvoidTrailingCallPass(MF);
}

PreservedAnalyses
X86AvoidTrailingCallPass::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
bool Changed = UpdatedOnX86AvoidTrailingCallPass(MF);
if (!Changed)
return PreservedAnalyses::all();

PreservedAnalyses PA = PreservedAnalyses::none();
PA.preserveSet<CFGAnalyses>();
return PA;
}
2 changes: 1 addition & 1 deletion llvm/lib/Target/X86/X86PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ DUMMY_FUNCTION_PASS("x86-winehstate", WinEHStatePass())
#ifndef MACHINE_FUNCTION_PASS
#define MACHINE_FUNCTION_PASS(NAME, CREATE_PASS)
#endif
MACHINE_FUNCTION_PASS("x86-avoid-trailing-call", X86AvoidTrailingCallPass(this))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep alphabetical order.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Done.

MACHINE_FUNCTION_PASS("x86-isel", X86ISelDAGToDAGPass(*this))
#undef MACHINE_FUNCTION_PASS

#ifndef DUMMY_MACHINE_FUNCTION_PASS
#define DUMMY_MACHINE_FUNCTION_PASS(NAME, PASS_NAME)
#endif
DUMMY_MACHINE_FUNCTION_PASS("x86-avoid-SFB", X86AvoidSFBPass())
DUMMY_MACHINE_FUNCTION_PASS("x86-avoid-trailing-call", X86AvoidTrailingCallPass())
DUMMY_MACHINE_FUNCTION_PASS("x86-cf-opt", X86CallFrameOptimization())
DUMMY_MACHINE_FUNCTION_PASS("x86-cmov-conversion", X86CmovConverterPass())
DUMMY_MACHINE_FUNCTION_PASS("x86-codege", FPS())
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/X86/X86TargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ extern "C" LLVM_C_ABI void LLVMInitializeX86Target() {
initializeX86ExecutionDomainFixPass(PR);
initializeX86DomainReassignmentPass(PR);
initializeX86AvoidSFBPassPass(PR);
initializeX86AvoidTrailingCallPassPass(PR);
initializeX86AvoidTrailingCallLegacyPassPass(PR);
initializeX86SpeculativeLoadHardeningPassPass(PR);
initializeX86SpeculativeExecutionSideEffectSuppressionPass(PR);
initializeX86FlagsCopyLoweringPassPass(PR);
Expand Down Expand Up @@ -589,7 +589,7 @@ void X86PassConfig::addPreEmitPass2() {
// Insert extra int3 instructions after trailing call instructions to avoid
// issues in the unwinder.
if (TT.isOSWindows() && TT.isX86_64())
addPass(createX86AvoidTrailingCallPass());
addPass(createX86AvoidTrailingCallLegacyPass());

// Verify basic block incoming and outgoing cfa offset and register values and
// correct CFA calculation rule where needed by inserting appropriate CFI
Expand Down
Loading