xref: /llvm-project/llvm/include/llvm/CodeGen/GlobalMerge.h (revision ccddd136024305be8b6aa77e4ce576d8e6521529)
1 //===- llvm/CodeGen/GlobalMerge.h -------------------------------*- 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 LLVM_CODEGEN_GLOBALMERGE_H
10 #define LLVM_CODEGEN_GLOBALMERGE_H
11 
12 #include "llvm/IR/PassManager.h"
13 
14 namespace llvm {
15 
16 class TargetMachine;
17 
18 struct GlobalMergeOptions {
19   // FIXME: Infer the maximum possible offset depending on the actual users
20   // (these max offsets are different for the users inside Thumb or ARM
21   // functions), see the code that passes in the offset in the ARM backend
22   // for more information.
23   unsigned MaxOffset = 0;
24   // The minimum size in bytes of each global that should considered in merging.
25   unsigned MinSize = 0;
26   bool GroupByUse = true;
27   bool IgnoreSingleUse = true;
28   bool MergeConst = false;
29   /// Whether we should merge global variables that have external linkage.
30   bool MergeExternal = true;
31   /// Whether we should merge constant global variables.
32   bool MergeConstantGlobals = false;
33   /// Whether we should merge constant global variables aggressively without
34   /// looking at use.
35   bool MergeConstAggressive = false;
36   /// Whether we should try to optimize for size only.
37   /// Currently, this applies a dead simple heuristic: only consider globals
38   /// used in minsize functions for merging.
39   /// FIXME: This could learn about optsize, and be used in the cost model.
40   bool SizeOnly = false;
41 };
42 
43 // FIXME: This pass must run before AsmPrinterPass::doInitialization!
44 class GlobalMergePass : public PassInfoMixin<GlobalMergePass> {
45   const TargetMachine *TM;
46   GlobalMergeOptions Options;
47 
48 public:
49   GlobalMergePass(const TargetMachine *TM, GlobalMergeOptions Options)
50       : TM(TM), Options(Options) {}
51 
52   PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
53 };
54 
55 } // namespace llvm
56 
57 #endif // LLVM_CODEGEN_GLOBALMERGE_H
58