xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/Analysis.cpp (revision acb1f1269c6f4ff89a0d28ba742f6687e9ef779d)
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   initializeInstCountLegacyPassPass(Registry);
54   initializeIntervalPartitionPass(Registry);
55   initializeIRSimilarityIdentifierWrapperPassPass(Registry);
56   initializeLazyBranchProbabilityInfoPassPass(Registry);
57   initializeLazyBlockFrequencyInfoPassPass(Registry);
58   initializeLazyValueInfoWrapperPassPass(Registry);
59   initializeLazyValueInfoPrinterPass(Registry);
60   initializeLegacyDivergenceAnalysisPass(Registry);
61   initializeLintLegacyPassPass(Registry);
62   initializeLoopInfoWrapperPassPass(Registry);
63   initializeMemDepPrinterPass(Registry);
64   initializeMemDerefPrinterPass(Registry);
65   initializeMemoryDependenceWrapperPassPass(Registry);
66   initializeModuleDebugInfoLegacyPrinterPass(Registry);
67   initializeModuleSummaryIndexWrapperPassPass(Registry);
68   initializeMustExecutePrinterPass(Registry);
69   initializeMustBeExecutedContextPrinterPass(Registry);
70   initializeObjCARCAAWrapperPassPass(Registry);
71   initializeOptimizationRemarkEmitterWrapperPassPass(Registry);
72   initializePhiValuesWrapperPassPass(Registry);
73   initializePostDominatorTreeWrapperPassPass(Registry);
74   initializeRegionInfoPassPass(Registry);
75   initializeRegionViewerPass(Registry);
76   initializeRegionPrinterPass(Registry);
77   initializeRegionOnlyViewerPass(Registry);
78   initializeRegionOnlyPrinterPass(Registry);
79   initializeSCEVAAWrapperPassPass(Registry);
80   initializeScalarEvolutionWrapperPassPass(Registry);
81   initializeStackSafetyGlobalInfoWrapperPassPass(Registry);
82   initializeStackSafetyInfoWrapperPassPass(Registry);
83   initializeTargetTransformInfoWrapperPassPass(Registry);
84   initializeTypeBasedAAWrapperPassPass(Registry);
85   initializeScopedNoAliasAAWrapperPassPass(Registry);
86   initializeLCSSAVerificationPassPass(Registry);
87   initializeMemorySSAWrapperPassPass(Registry);
88   initializeMemorySSAPrinterLegacyPassPass(Registry);
89 }
90 
91 void LLVMInitializeAnalysis(LLVMPassRegistryRef R) {
92   initializeAnalysis(*unwrap(R));
93 }
94 
95 void LLVMInitializeIPA(LLVMPassRegistryRef R) {
96   initializeAnalysis(*unwrap(R));
97 }
98 
99 LLVMBool LLVMVerifyModule(LLVMModuleRef M, LLVMVerifierFailureAction Action,
100                           char **OutMessages) {
101   raw_ostream *DebugOS = Action != LLVMReturnStatusAction ? &errs() : nullptr;
102   std::string Messages;
103   raw_string_ostream MsgsOS(Messages);
104 
105   LLVMBool Result = verifyModule(*unwrap(M), OutMessages ? &MsgsOS : DebugOS);
106 
107   // Duplicate the output to stderr.
108   if (DebugOS && OutMessages)
109     *DebugOS << MsgsOS.str();
110 
111   if (Action == LLVMAbortProcessAction && Result)
112     report_fatal_error("Broken module found, compilation aborted!");
113 
114   if (OutMessages)
115     *OutMessages = strdup(MsgsOS.str().c_str());
116 
117   return Result;
118 }
119 
120 LLVMBool LLVMVerifyFunction(LLVMValueRef Fn, LLVMVerifierFailureAction Action) {
121   LLVMBool Result = verifyFunction(
122       *unwrap<Function>(Fn), Action != LLVMReturnStatusAction ? &errs()
123                                                               : nullptr);
124 
125   if (Action == LLVMAbortProcessAction && Result)
126     report_fatal_error("Broken function found, compilation aborted!");
127 
128   return Result;
129 }
130 
131 void LLVMViewFunctionCFG(LLVMValueRef Fn) {
132   Function *F = unwrap<Function>(Fn);
133   F->viewCFG();
134 }
135 
136 void LLVMViewFunctionCFGOnly(LLVMValueRef Fn) {
137   Function *F = unwrap<Function>(Fn);
138   F->viewCFGOnly();
139 }
140