1 //===- bolt/Passes/SplitFunctions.h - Split function code -------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
8 
9 #ifndef BOLT_PASSES_SPLIT_FUNCTIONS_H
10 #define BOLT_PASSES_SPLIT_FUNCTIONS_H
11 
12 #include "bolt/Passes/BinaryPasses.h"
13 #include "llvm/Support/CommandLine.h"
14 #include <atomic>
15 
16 namespace llvm {
17 namespace bolt {
18 
19 /// Split function code in multiple parts.
20 class SplitFunctions : public BinaryFunctionPass {
21 private:
22   /// Split function body into fragments.
23   template <typename SplitStrategy>
24   void splitFunction(BinaryFunction &Function, SplitStrategy Strategy = {});
25 
26   /// Map basic block labels to their trampoline block labels.
27   using TrampolineSetType = DenseMap<const MCSymbol *, const MCSymbol *>;
28 
29   using BasicBlockOrderType = BinaryFunction::BasicBlockOrderType;
30 
31   /// Create trampoline landing pads for exception handling code to guarantee
32   /// that every landing pad is placed in the same function fragment as the
33   /// corresponding thrower block. The trampoline landing pad, when created,
34   /// will redirect the execution to the real landing pad in a different
35   /// fragment.
36   TrampolineSetType createEHTrampolines(BinaryFunction &Function) const;
37 
38   /// Merge trampolines into \p Layout without trampolines. The merge will place
39   /// a trampoline immediately before its destination. Used to revert the effect
40   /// of trampolines after createEHTrampolines().
41   BasicBlockOrderType
42   mergeEHTrampolines(BinaryFunction &BF, BasicBlockOrderType &Layout,
43                      const TrampolineSetType &Trampolines) const;
44 
45   std::atomic<uint64_t> SplitBytesHot{0ull};
46   std::atomic<uint64_t> SplitBytesCold{0ull};
47 
48 public:
SplitFunctions(const cl::opt<bool> & PrintPass)49   explicit SplitFunctions(const cl::opt<bool> &PrintPass)
50       : BinaryFunctionPass(PrintPass) {}
51 
52   bool shouldOptimize(const BinaryFunction &BF) const override;
53 
getName()54   const char *getName() const override { return "split-functions"; }
55 
56   void runOnFunctions(BinaryContext &BC) override;
57 };
58 
59 } // namespace bolt
60 } // namespace llvm
61 
62 #endif
63