2016-04-19 05:24:47 +00:00
|
|
|
//===-- PatchableFunction.cpp - Patchable prologues for LLVM -------------===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-04-19 05:24:47 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements edits function bodies in place to support the
|
|
|
|
// "patchable-function" attribute.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2016-08-08 21:20:15 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2017-11-08 01:01:31 +00:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-11-17 01:07:10 +00:00
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-13 13:15:01 -08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2022-03-15 10:54:19 +01:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/PassRegistry.h"
|
2016-04-19 05:24:47 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct PatchableFunction : public MachineFunctionPass {
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
PatchableFunction() : MachineFunctionPass(ID) {
|
|
|
|
initializePatchableFunctionPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &F) override;
|
2016-08-08 21:20:15 +00:00
|
|
|
MachineFunctionProperties getRequiredProperties() const override {
|
2016-04-19 05:24:47 +00:00
|
|
|
return MachineFunctionProperties().set(
|
2016-08-25 01:27:13 +00:00
|
|
|
MachineFunctionProperties::Property::NoVRegs);
|
2016-04-19 05:24:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PatchableFunction::runOnMachineFunction(MachineFunction &MF) {
|
2024-01-22 14:19:08 -05:00
|
|
|
MachineBasicBlock &FirstMBB = *MF.begin();
|
|
|
|
|
2020-01-03 00:35:47 -08:00
|
|
|
if (MF.getFunction().hasFnAttribute("patchable-function-entry")) {
|
|
|
|
const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
|
2020-02-01 13:28:19 -08:00
|
|
|
// The initial .loc covers PATCHABLE_FUNCTION_ENTER.
|
|
|
|
BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(),
|
|
|
|
TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER));
|
2020-01-03 00:35:47 -08:00
|
|
|
return true;
|
2024-01-22 14:19:08 -05:00
|
|
|
} else if (MF.getFunction().hasFnAttribute("patchable-function")) {
|
2016-08-08 21:20:15 +00:00
|
|
|
#ifndef NDEBUG
|
2024-01-22 14:19:08 -05:00
|
|
|
Attribute PatchAttr = MF.getFunction().getFnAttribute("patchable-function");
|
|
|
|
StringRef PatchType = PatchAttr.getValueAsString();
|
|
|
|
assert(PatchType == "prologue-short-redirect" && "Only possibility today!");
|
2016-08-08 21:20:15 +00:00
|
|
|
#endif
|
2024-01-22 14:19:08 -05:00
|
|
|
auto *TII = MF.getSubtarget().getInstrInfo();
|
|
|
|
BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(),
|
|
|
|
TII->get(TargetOpcode::PATCHABLE_OP))
|
|
|
|
.addImm(2);
|
2022-11-04 16:34:23 -04:00
|
|
|
MF.ensureAlignment(Align(16));
|
|
|
|
return true;
|
|
|
|
}
|
2024-01-22 14:19:08 -05:00
|
|
|
return false;
|
2016-04-19 05:24:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char PatchableFunction::ID = 0;
|
|
|
|
char &llvm::PatchableFunctionID = PatchableFunction::ID;
|
2016-04-19 06:25:02 +00:00
|
|
|
INITIALIZE_PASS(PatchableFunction, "patchable-function",
|
|
|
|
"Implement the 'patchable-function' attribute", false, false)
|