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