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 
initializeIPO(PassRegistry & Registry)25 void llvm::initializeIPO(PassRegistry &Registry) {
26   initializeOpenMPOptLegacyPassPass(Registry);
27   initializeArgPromotionPass(Registry);
28   initializeAnnotation2MetadataLegacyPass(Registry);
29   initializeCalledValuePropagationLegacyPassPass(Registry);
30   initializeConstantMergeLegacyPassPass(Registry);
31   initializeCrossDSOCFIPass(Registry);
32   initializeDAEPass(Registry);
33   initializeDAHPass(Registry);
34   initializeForceFunctionAttrsLegacyPassPass(Registry);
35   initializeGlobalDCELegacyPassPass(Registry);
36   initializeGlobalOptLegacyPassPass(Registry);
37   initializeGlobalSplitPass(Registry);
38   initializeHotColdSplittingLegacyPassPass(Registry);
39   initializeAlwaysInlinerLegacyPassPass(Registry);
40   initializeSimpleInlinerPass(Registry);
41   initializeInferFunctionAttrsLegacyPassPass(Registry);
42   initializeInternalizeLegacyPassPass(Registry);
43   initializeLoopExtractorLegacyPassPass(Registry);
44   initializeBlockExtractorLegacyPassPass(Registry);
45   initializeSingleLoopExtractorPass(Registry);
46   initializeLowerTypeTestsPass(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   initializeSampleProfileLoaderLegacyPassPass(Registry);
63   initializeFunctionImportLegacyPassPass(Registry);
64   initializeWholeProgramDevirtPass(Registry);
65 }
66 
LLVMInitializeIPO(LLVMPassRegistryRef R)67 void LLVMInitializeIPO(LLVMPassRegistryRef R) {
68   initializeIPO(*unwrap(R));
69 }
70 
LLVMAddArgumentPromotionPass(LLVMPassManagerRef PM)71 void LLVMAddArgumentPromotionPass(LLVMPassManagerRef PM) {
72   unwrap(PM)->add(createArgumentPromotionPass());
73 }
74 
LLVMAddCalledValuePropagationPass(LLVMPassManagerRef PM)75 void LLVMAddCalledValuePropagationPass(LLVMPassManagerRef PM) {
76   unwrap(PM)->add(createCalledValuePropagationPass());
77 }
78 
LLVMAddConstantMergePass(LLVMPassManagerRef PM)79 void LLVMAddConstantMergePass(LLVMPassManagerRef PM) {
80   unwrap(PM)->add(createConstantMergePass());
81 }
82 
LLVMAddDeadArgEliminationPass(LLVMPassManagerRef PM)83 void LLVMAddDeadArgEliminationPass(LLVMPassManagerRef PM) {
84   unwrap(PM)->add(createDeadArgEliminationPass());
85 }
86 
LLVMAddFunctionAttrsPass(LLVMPassManagerRef PM)87 void LLVMAddFunctionAttrsPass(LLVMPassManagerRef PM) {
88   unwrap(PM)->add(createPostOrderFunctionAttrsLegacyPass());
89 }
90 
LLVMAddFunctionInliningPass(LLVMPassManagerRef PM)91 void LLVMAddFunctionInliningPass(LLVMPassManagerRef PM) {
92   unwrap(PM)->add(createFunctionInliningPass());
93 }
94 
LLVMAddAlwaysInlinerPass(LLVMPassManagerRef PM)95 void LLVMAddAlwaysInlinerPass(LLVMPassManagerRef PM) {
96   unwrap(PM)->add(llvm::createAlwaysInlinerLegacyPass());
97 }
98 
LLVMAddGlobalDCEPass(LLVMPassManagerRef PM)99 void LLVMAddGlobalDCEPass(LLVMPassManagerRef PM) {
100   unwrap(PM)->add(createGlobalDCEPass());
101 }
102 
LLVMAddGlobalOptimizerPass(LLVMPassManagerRef PM)103 void LLVMAddGlobalOptimizerPass(LLVMPassManagerRef PM) {
104   unwrap(PM)->add(createGlobalOptimizerPass());
105 }
106 
LLVMAddPruneEHPass(LLVMPassManagerRef PM)107 void LLVMAddPruneEHPass(LLVMPassManagerRef PM) {
108   unwrap(PM)->add(createPruneEHPass());
109 }
110 
LLVMAddIPSCCPPass(LLVMPassManagerRef PM)111 void LLVMAddIPSCCPPass(LLVMPassManagerRef PM) {
112   unwrap(PM)->add(createIPSCCPPass());
113 }
114 
LLVMAddMergeFunctionsPass(LLVMPassManagerRef PM)115 void LLVMAddMergeFunctionsPass(LLVMPassManagerRef PM) {
116   unwrap(PM)->add(createMergeFunctionsPass());
117 }
118 
LLVMAddInternalizePass(LLVMPassManagerRef PM,unsigned AllButMain)119 void LLVMAddInternalizePass(LLVMPassManagerRef PM, unsigned AllButMain) {
120   auto PreserveMain = [=](const GlobalValue &GV) {
121     return AllButMain && GV.getName() == "main";
122   };
123   unwrap(PM)->add(createInternalizePass(PreserveMain));
124 }
125 
LLVMAddInternalizePassWithMustPreservePredicate(LLVMPassManagerRef PM,void * Context,LLVMBool (* Pred)(LLVMValueRef,void *))126 void LLVMAddInternalizePassWithMustPreservePredicate(
127     LLVMPassManagerRef PM,
128     void *Context,
129     LLVMBool (*Pred)(LLVMValueRef, void *)) {
130   unwrap(PM)->add(createInternalizePass([=](const GlobalValue &GV) {
131     return Pred(wrap(&GV), Context) == 0 ? false : true;
132   }));
133 }
134 
LLVMAddStripDeadPrototypesPass(LLVMPassManagerRef PM)135 void LLVMAddStripDeadPrototypesPass(LLVMPassManagerRef PM) {
136   unwrap(PM)->add(createStripDeadPrototypesPass());
137 }
138 
LLVMAddStripSymbolsPass(LLVMPassManagerRef PM)139 void LLVMAddStripSymbolsPass(LLVMPassManagerRef PM) {
140   unwrap(PM)->add(createStripSymbolsPass());
141 }
142