Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / bolt / lib / Passes / Hugify.cpp
blobd2a64fb97c196d1f02cc211b9046d3f5fd16bd2b
1 //===--- bolt/Passes/Hugify.cpp -------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "bolt/Passes/Hugify.h"
10 #include "llvm/Support/CommandLine.h"
12 #define DEBUG_TYPE "bolt-hugify"
14 using namespace llvm;
16 namespace llvm {
17 namespace bolt {
19 void HugePage::runOnFunctions(BinaryContext &BC) {
20 auto *RtLibrary = BC.getRuntimeLibrary();
21 if (!RtLibrary || !BC.isELF() || !BC.StartFunctionAddress) {
22 return;
25 auto createSimpleFunction =
26 [&](std::string Title, std::vector<MCInst> Instrs) -> BinaryFunction * {
27 BinaryFunction *Func = BC.createInjectedBinaryFunction(Title);
29 std::vector<std::unique_ptr<BinaryBasicBlock>> BBs;
30 BBs.emplace_back(Func->createBasicBlock(nullptr));
31 BBs.back()->addInstructions(Instrs.begin(), Instrs.end());
32 BBs.back()->setCFIState(0);
33 BBs.back()->setOffset(BinaryBasicBlock::INVALID_OFFSET);
35 Func->insertBasicBlocks(nullptr, std::move(BBs),
36 /*UpdateLayout=*/true,
37 /*UpdateCFIState=*/false);
38 Func->updateState(BinaryFunction::State::CFG_Finalized);
39 return Func;
42 const BinaryFunction *const Start =
43 BC.getBinaryFunctionAtAddress(*BC.StartFunctionAddress);
44 assert(Start && "Entry point function not found");
45 const MCSymbol *StartSym = Start->getSymbol();
46 createSimpleFunction("__bolt_hugify_start_program",
47 BC.MIB->createSymbolTrampoline(StartSym, BC.Ctx.get()));
49 } // namespace bolt
50 } // namespace llvm