1 //===-- Analysis.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 #include "llvm-c/Analysis.h" 10 #include "llvm-c/Initialization.h" 11 #include "llvm/IR/Module.h" 12 #include "llvm/IR/Verifier.h" 13 #include "llvm/InitializePasses.h" 14 #include "llvm/PassRegistry.h" 15 #include "llvm/Support/raw_ostream.h" 16 #include <cstring> 17 18 using namespace llvm; 19 20 /// initializeAnalysis - Initialize all passes linked into the Analysis library. 21 void llvm::initializeAnalysis(PassRegistry &Registry) { 22 initializeAAEvalLegacyPassPass(Registry); 23 initializeAliasSetPrinterPass(Registry); 24 initializeBasicAAWrapperPassPass(Registry); 25 initializeBlockFrequencyInfoWrapperPassPass(Registry); 26 initializeBranchProbabilityInfoWrapperPassPass(Registry); 27 initializeCallGraphWrapperPassPass(Registry); 28 initializeCallGraphDOTPrinterPass(Registry); 29 initializeCallGraphPrinterLegacyPassPass(Registry); 30 initializeCallGraphViewerPass(Registry); 31 initializeCostModelAnalysisPass(Registry); 32 initializeCFGViewerLegacyPassPass(Registry); 33 initializeCFGPrinterLegacyPassPass(Registry); 34 initializeCFGOnlyViewerLegacyPassPass(Registry); 35 initializeCFGOnlyPrinterLegacyPassPass(Registry); 36 initializeCFLAndersAAWrapperPassPass(Registry); 37 initializeCFLSteensAAWrapperPassPass(Registry); 38 initializeDependenceAnalysisWrapperPassPass(Registry); 39 initializeDelinearizationPass(Registry); 40 initializeDemandedBitsWrapperPassPass(Registry); 41 initializeDominanceFrontierWrapperPassPass(Registry); 42 initializeDomViewerPass(Registry); 43 initializeDomPrinterPass(Registry); 44 initializeDomOnlyViewerPass(Registry); 45 initializePostDomViewerPass(Registry); 46 initializeDomOnlyPrinterPass(Registry); 47 initializePostDomPrinterPass(Registry); 48 initializePostDomOnlyViewerPass(Registry); 49 initializePostDomOnlyPrinterPass(Registry); 50 initializeAAResultsWrapperPassPass(Registry); 51 initializeGlobalsAAWrapperPassPass(Registry); 52 initializeIVUsersWrapperPassPass(Registry); 53 initializeInstCountPass(Registry); 54 initializeIntervalPartitionPass(Registry); 55 initializeLazyBranchProbabilityInfoPassPass(Registry); 56 initializeLazyBlockFrequencyInfoPassPass(Registry); 57 initializeLazyValueInfoWrapperPassPass(Registry); 58 initializeLazyValueInfoPrinterPass(Registry); 59 initializeLegacyDivergenceAnalysisPass(Registry); 60 initializeLintPass(Registry); 61 initializeLoopInfoWrapperPassPass(Registry); 62 initializeMemDepPrinterPass(Registry); 63 initializeMemDerefPrinterPass(Registry); 64 initializeMemoryDependenceWrapperPassPass(Registry); 65 initializeModuleDebugInfoPrinterPass(Registry); 66 initializeModuleSummaryIndexWrapperPassPass(Registry); 67 initializeMustExecutePrinterPass(Registry); 68 initializeObjCARCAAWrapperPassPass(Registry); 69 initializeOptimizationRemarkEmitterWrapperPassPass(Registry); 70 initializePhiValuesWrapperPassPass(Registry); 71 initializePostDominatorTreeWrapperPassPass(Registry); 72 initializeRegionInfoPassPass(Registry); 73 initializeRegionViewerPass(Registry); 74 initializeRegionPrinterPass(Registry); 75 initializeRegionOnlyViewerPass(Registry); 76 initializeRegionOnlyPrinterPass(Registry); 77 initializeSCEVAAWrapperPassPass(Registry); 78 initializeScalarEvolutionWrapperPassPass(Registry); 79 initializeStackSafetyGlobalInfoWrapperPassPass(Registry); 80 initializeStackSafetyInfoWrapperPassPass(Registry); 81 initializeTargetTransformInfoWrapperPassPass(Registry); 82 initializeTypeBasedAAWrapperPassPass(Registry); 83 initializeScopedNoAliasAAWrapperPassPass(Registry); 84 initializeLCSSAVerificationPassPass(Registry); 85 initializeMemorySSAWrapperPassPass(Registry); 86 initializeMemorySSAPrinterLegacyPassPass(Registry); 87 } 88 89 void LLVMInitializeAnalysis(LLVMPassRegistryRef R) { 90 initializeAnalysis(*unwrap(R)); 91 } 92 93 void LLVMInitializeIPA(LLVMPassRegistryRef R) { 94 initializeAnalysis(*unwrap(R)); 95 } 96 97 LLVMBool LLVMVerifyModule(LLVMModuleRef M, LLVMVerifierFailureAction Action, 98 char **OutMessages) { 99 raw_ostream *DebugOS = Action != LLVMReturnStatusAction ? &errs() : nullptr; 100 std::string Messages; 101 raw_string_ostream MsgsOS(Messages); 102 103 LLVMBool Result = verifyModule(*unwrap(M), OutMessages ? &MsgsOS : DebugOS); 104 105 // Duplicate the output to stderr. 106 if (DebugOS && OutMessages) 107 *DebugOS << MsgsOS.str(); 108 109 if (Action == LLVMAbortProcessAction && Result) 110 report_fatal_error("Broken module found, compilation aborted!"); 111 112 if (OutMessages) 113 *OutMessages = strdup(MsgsOS.str().c_str()); 114 115 return Result; 116 } 117 118 LLVMBool LLVMVerifyFunction(LLVMValueRef Fn, LLVMVerifierFailureAction Action) { 119 LLVMBool Result = verifyFunction( 120 *unwrap<Function>(Fn), Action != LLVMReturnStatusAction ? &errs() 121 : nullptr); 122 123 if (Action == LLVMAbortProcessAction && Result) 124 report_fatal_error("Broken function found, compilation aborted!"); 125 126 return Result; 127 } 128 129 void LLVMViewFunctionCFG(LLVMValueRef Fn) { 130 Function *F = unwrap<Function>(Fn); 131 F->viewCFG(); 132 } 133 134 void LLVMViewFunctionCFGOnly(LLVMValueRef Fn) { 135 Function *F = unwrap<Function>(Fn); 136 F->viewCFGOnly(); 137 } 138