xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/IPO/IPO.cpp (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1 //===-- IPO.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 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the common infrastructure (including C bindings) for
10 // libLLVMIPO.a, which implements several transformations over the LLVM
11 // intermediate representation.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm-c/Transforms/IPO.h"
16 #include "llvm-c/Initialization.h"
17 #include "llvm/IR/LegacyPassManager.h"
18 #include "llvm/InitializePasses.h"
19 #include "llvm/Transforms/IPO.h"
20 #include "llvm/Transforms/IPO/AlwaysInliner.h"
21 #include "llvm/Transforms/IPO/FunctionAttrs.h"
22 
23 using namespace llvm;
24 
25 void llvm::initializeIPO(PassRegistry &Registry) {
26   initializeOpenMPOptCGSCCLegacyPassPass(Registry);
27   initializeAnnotation2MetadataLegacyPass(Registry);
28   initializeCalledValuePropagationLegacyPassPass(Registry);
29   initializeConstantMergeLegacyPassPass(Registry);
30   initializeCrossDSOCFIPass(Registry);
31   initializeDAEPass(Registry);
32   initializeDAHPass(Registry);
33   initializeForceFunctionAttrsLegacyPassPass(Registry);
34   initializeFunctionSpecializationLegacyPassPass(Registry);
35   initializeGlobalDCELegacyPassPass(Registry);
36   initializeGlobalOptLegacyPassPass(Registry);
37   initializeGlobalSplitPass(Registry);
38   initializeHotColdSplittingLegacyPassPass(Registry);
39   initializeIROutlinerLegacyPassPass(Registry);
40   initializeAlwaysInlinerLegacyPassPass(Registry);
41   initializeSimpleInlinerPass(Registry);
42   initializeInferFunctionAttrsLegacyPassPass(Registry);
43   initializeInternalizeLegacyPassPass(Registry);
44   initializeLoopExtractorLegacyPassPass(Registry);
45   initializeBlockExtractorLegacyPassPass(Registry);
46   initializeSingleLoopExtractorPass(Registry);
47   initializeMergeFunctionsLegacyPassPass(Registry);
48   initializePartialInlinerLegacyPassPass(Registry);
49   initializeAttributorLegacyPassPass(Registry);
50   initializeAttributorCGSCCLegacyPassPass(Registry);
51   initializePostOrderFunctionAttrsLegacyPassPass(Registry);
52   initializeReversePostOrderFunctionAttrsLegacyPassPass(Registry);
53   initializePruneEHPass(Registry);
54   initializeIPSCCPLegacyPassPass(Registry);
55   initializeStripDeadPrototypesLegacyPassPass(Registry);
56   initializeStripSymbolsPass(Registry);
57   initializeStripDebugDeclarePass(Registry);
58   initializeStripDeadDebugInfoPass(Registry);
59   initializeStripNonDebugSymbolsPass(Registry);
60   initializeBarrierNoopPass(Registry);
61   initializeEliminateAvailableExternallyLegacyPassPass(Registry);
62 }
63 
64 void LLVMInitializeIPO(LLVMPassRegistryRef R) {
65   initializeIPO(*unwrap(R));
66 }
67 
68 void LLVMAddCalledValuePropagationPass(LLVMPassManagerRef PM) {
69   unwrap(PM)->add(createCalledValuePropagationPass());
70 }
71 
72 void LLVMAddConstantMergePass(LLVMPassManagerRef PM) {
73   unwrap(PM)->add(createConstantMergePass());
74 }
75 
76 void LLVMAddDeadArgEliminationPass(LLVMPassManagerRef PM) {
77   unwrap(PM)->add(createDeadArgEliminationPass());
78 }
79 
80 void LLVMAddFunctionAttrsPass(LLVMPassManagerRef PM) {
81   unwrap(PM)->add(createPostOrderFunctionAttrsLegacyPass());
82 }
83 
84 void LLVMAddFunctionInliningPass(LLVMPassManagerRef PM) {
85   unwrap(PM)->add(createFunctionInliningPass());
86 }
87 
88 void LLVMAddAlwaysInlinerPass(LLVMPassManagerRef PM) {
89   unwrap(PM)->add(llvm::createAlwaysInlinerLegacyPass());
90 }
91 
92 void LLVMAddGlobalDCEPass(LLVMPassManagerRef PM) {
93   unwrap(PM)->add(createGlobalDCEPass());
94 }
95 
96 void LLVMAddGlobalOptimizerPass(LLVMPassManagerRef PM) {
97   unwrap(PM)->add(createGlobalOptimizerPass());
98 }
99 
100 void LLVMAddPruneEHPass(LLVMPassManagerRef PM) {
101   unwrap(PM)->add(createPruneEHPass());
102 }
103 
104 void LLVMAddIPSCCPPass(LLVMPassManagerRef PM) {
105   unwrap(PM)->add(createIPSCCPPass());
106 }
107 
108 void LLVMAddMergeFunctionsPass(LLVMPassManagerRef PM) {
109   unwrap(PM)->add(createMergeFunctionsPass());
110 }
111 
112 void LLVMAddInternalizePass(LLVMPassManagerRef PM, unsigned AllButMain) {
113   auto PreserveMain = [=](const GlobalValue &GV) {
114     return AllButMain && GV.getName() == "main";
115   };
116   unwrap(PM)->add(createInternalizePass(PreserveMain));
117 }
118 
119 void LLVMAddInternalizePassWithMustPreservePredicate(
120     LLVMPassManagerRef PM,
121     void *Context,
122     LLVMBool (*Pred)(LLVMValueRef, void *)) {
123   unwrap(PM)->add(createInternalizePass([=](const GlobalValue &GV) {
124     return Pred(wrap(&GV), Context) == 0 ? false : true;
125   }));
126 }
127 
128 void LLVMAddStripDeadPrototypesPass(LLVMPassManagerRef PM) {
129   unwrap(PM)->add(createStripDeadPrototypesPass());
130 }
131 
132 void LLVMAddStripSymbolsPass(LLVMPassManagerRef PM) {
133   unwrap(PM)->add(createStripSymbolsPass());
134 }
135