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