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 initializeAnnotation2MetadataLegacyPass(Registry); 27 initializeCalledValuePropagationLegacyPassPass(Registry); 28 initializeConstantMergeLegacyPassPass(Registry); 29 initializeCrossDSOCFIPass(Registry); 30 initializeDAEPass(Registry); 31 initializeDAHPass(Registry); 32 initializeForceFunctionAttrsLegacyPassPass(Registry); 33 initializeGlobalDCELegacyPassPass(Registry); 34 initializeGlobalOptLegacyPassPass(Registry); 35 initializeGlobalSplitPass(Registry); 36 initializeHotColdSplittingLegacyPassPass(Registry); 37 initializeIROutlinerLegacyPassPass(Registry); 38 initializeAlwaysInlinerLegacyPassPass(Registry); 39 initializeSimpleInlinerPass(Registry); 40 initializeInferFunctionAttrsLegacyPassPass(Registry); 41 initializeInternalizeLegacyPassPass(Registry); 42 initializeLoopExtractorLegacyPassPass(Registry); 43 initializeSingleLoopExtractorPass(Registry); 44 initializeMergeFunctionsLegacyPassPass(Registry); 45 initializePartialInlinerLegacyPassPass(Registry); 46 initializeAttributorLegacyPassPass(Registry); 47 initializeAttributorCGSCCLegacyPassPass(Registry); 48 initializePostOrderFunctionAttrsLegacyPassPass(Registry); 49 initializeReversePostOrderFunctionAttrsLegacyPassPass(Registry); 50 initializeIPSCCPLegacyPassPass(Registry); 51 initializeStripDeadPrototypesLegacyPassPass(Registry); 52 initializeStripSymbolsPass(Registry); 53 initializeStripDebugDeclarePass(Registry); 54 initializeStripDeadDebugInfoPass(Registry); 55 initializeStripNonDebugSymbolsPass(Registry); 56 initializeBarrierNoopPass(Registry); 57 initializeEliminateAvailableExternallyLegacyPassPass(Registry); 58 } 59 60 void LLVMInitializeIPO(LLVMPassRegistryRef R) { 61 initializeIPO(*unwrap(R)); 62 } 63 64 void LLVMAddCalledValuePropagationPass(LLVMPassManagerRef PM) { 65 unwrap(PM)->add(createCalledValuePropagationPass()); 66 } 67 68 void LLVMAddConstantMergePass(LLVMPassManagerRef PM) { 69 unwrap(PM)->add(createConstantMergePass()); 70 } 71 72 void LLVMAddDeadArgEliminationPass(LLVMPassManagerRef PM) { 73 unwrap(PM)->add(createDeadArgEliminationPass()); 74 } 75 76 void LLVMAddFunctionAttrsPass(LLVMPassManagerRef PM) { 77 unwrap(PM)->add(createPostOrderFunctionAttrsLegacyPass()); 78 } 79 80 void LLVMAddFunctionInliningPass(LLVMPassManagerRef PM) { 81 unwrap(PM)->add(createFunctionInliningPass()); 82 } 83 84 void LLVMAddAlwaysInlinerPass(LLVMPassManagerRef PM) { 85 unwrap(PM)->add(llvm::createAlwaysInlinerLegacyPass()); 86 } 87 88 void LLVMAddGlobalDCEPass(LLVMPassManagerRef PM) { 89 unwrap(PM)->add(createGlobalDCEPass()); 90 } 91 92 void LLVMAddGlobalOptimizerPass(LLVMPassManagerRef PM) { 93 unwrap(PM)->add(createGlobalOptimizerPass()); 94 } 95 96 void LLVMAddIPSCCPPass(LLVMPassManagerRef PM) { 97 unwrap(PM)->add(createIPSCCPPass()); 98 } 99 100 void LLVMAddMergeFunctionsPass(LLVMPassManagerRef PM) { 101 unwrap(PM)->add(createMergeFunctionsPass()); 102 } 103 104 void LLVMAddInternalizePass(LLVMPassManagerRef PM, unsigned AllButMain) { 105 auto PreserveMain = [=](const GlobalValue &GV) { 106 return AllButMain && GV.getName() == "main"; 107 }; 108 unwrap(PM)->add(createInternalizePass(PreserveMain)); 109 } 110 111 void LLVMAddInternalizePassWithMustPreservePredicate( 112 LLVMPassManagerRef PM, 113 void *Context, 114 LLVMBool (*Pred)(LLVMValueRef, void *)) { 115 unwrap(PM)->add(createInternalizePass([=](const GlobalValue &GV) { 116 return Pred(wrap(&GV), Context) == 0 ? false : true; 117 })); 118 } 119 120 void LLVMAddStripDeadPrototypesPass(LLVMPassManagerRef PM) { 121 unwrap(PM)->add(createStripDeadPrototypesPass()); 122 } 123 124 void LLVMAddStripSymbolsPass(LLVMPassManagerRef PM) { 125 unwrap(PM)->add(createStripSymbolsPass()); 126 } 127