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