2016-04-19 13:24:47 +08:00
|
|
|
//===-- PatchableFunction.cpp - Patchable prologues for LLVM -------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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 13:24:47 +08: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-09 05:20:15 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2017-11-04 06:32:11 +08:00
|
|
|
#include "llvm/CodeGen/TargetFrameLowering.h"
|
2017-11-08 09:01:31 +08:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-11-17 09:07:10 +08: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-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2016-04-19 13:24:47 +08: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-09 05:20:15 +08:00
|
|
|
MachineFunctionProperties getRequiredProperties() const override {
|
2016-04-19 13:24:47 +08:00
|
|
|
return MachineFunctionProperties().set(
|
2016-08-25 09:27:13 +08:00
|
|
|
MachineFunctionProperties::Property::NoVRegs);
|
2016-04-19 13:24:47 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-14 00:37:29 +08:00
|
|
|
/// Returns true if instruction \p MI will not result in actual machine code
|
|
|
|
/// instructions.
|
|
|
|
static bool doesNotGeneratecode(const MachineInstr &MI) {
|
|
|
|
// TODO: Introduce an MCInstrDesc flag for this
|
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
default: return false;
|
|
|
|
case TargetOpcode::IMPLICIT_DEF:
|
|
|
|
case TargetOpcode::KILL:
|
|
|
|
case TargetOpcode::CFI_INSTRUCTION:
|
|
|
|
case TargetOpcode::EH_LABEL:
|
|
|
|
case TargetOpcode::GC_LABEL:
|
|
|
|
case TargetOpcode::DBG_VALUE:
|
2018-05-09 10:41:08 +08:00
|
|
|
case TargetOpcode::DBG_LABEL:
|
2016-07-14 00:37:29 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 13:24:47 +08:00
|
|
|
bool PatchableFunction::runOnMachineFunction(MachineFunction &MF) {
|
2020-01-03 16:35:47 +08:00
|
|
|
if (MF.getFunction().hasFnAttribute("patchable-function-entry")) {
|
|
|
|
MachineBasicBlock &FirstMBB = *MF.begin();
|
|
|
|
const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
|
2020-02-02 05: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 16:35:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-16 06:22:58 +08:00
|
|
|
if (!MF.getFunction().hasFnAttribute("patchable-function"))
|
2016-04-19 13:24:47 +08:00
|
|
|
return false;
|
|
|
|
|
2016-08-09 05:20:15 +08:00
|
|
|
#ifndef NDEBUG
|
2017-12-16 06:22:58 +08:00
|
|
|
Attribute PatchAttr = MF.getFunction().getFnAttribute("patchable-function");
|
2016-04-19 13:24:47 +08:00
|
|
|
StringRef PatchType = PatchAttr.getValueAsString();
|
2016-08-09 05:20:15 +08:00
|
|
|
assert(PatchType == "prologue-short-redirect" && "Only possibility today!");
|
|
|
|
#endif
|
2016-04-19 13:24:47 +08:00
|
|
|
|
|
|
|
auto &FirstMBB = *MF.begin();
|
2016-07-14 00:37:29 +08:00
|
|
|
MachineBasicBlock::iterator FirstActualI = FirstMBB.begin();
|
|
|
|
for (; doesNotGeneratecode(*FirstActualI); ++FirstActualI)
|
|
|
|
assert(FirstActualI != FirstMBB.end());
|
2016-04-19 13:24:47 +08:00
|
|
|
|
2016-08-09 05:20:15 +08:00
|
|
|
auto *TII = MF.getSubtarget().getInstrInfo();
|
|
|
|
auto MIB = BuildMI(FirstMBB, FirstActualI, FirstActualI->getDebugLoc(),
|
|
|
|
TII->get(TargetOpcode::PATCHABLE_OP))
|
|
|
|
.addImm(2)
|
|
|
|
.addImm(FirstActualI->getOpcode());
|
|
|
|
|
|
|
|
for (auto &MO : FirstActualI->operands())
|
2017-01-13 17:58:52 +08:00
|
|
|
MIB.add(MO);
|
2016-04-19 13:24:47 +08:00
|
|
|
|
2016-08-09 05:20:15 +08:00
|
|
|
FirstActualI->eraseFromParent();
|
2019-09-27 20:54:21 +08:00
|
|
|
MF.ensureAlignment(Align(16));
|
2016-04-19 13:24:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
char PatchableFunction::ID = 0;
|
|
|
|
char &llvm::PatchableFunctionID = PatchableFunction::ID;
|
2016-04-19 14:25:02 +08:00
|
|
|
INITIALIZE_PASS(PatchableFunction, "patchable-function",
|
|
|
|
"Implement the 'patchable-function' attribute", false, false)
|