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/IR/Module.h"
11 #include "llvm/IR/Verifier.h"
12 #include "llvm/InitializePasses.h"
13 #include "llvm/PassRegistry.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include <cstring>
16
17 using namespace llvm;
18
19 /// initializeAnalysis - Initialize all passes linked into the Analysis library.
initializeAnalysis(PassRegistry & Registry)20 void llvm::initializeAnalysis(PassRegistry &Registry) {
21 initializeBasicAAWrapperPassPass(Registry);
22 initializeBlockFrequencyInfoWrapperPassPass(Registry);
23 initializeBranchProbabilityInfoWrapperPassPass(Registry);
24 initializeCallGraphWrapperPassPass(Registry);
25 initializeCallGraphDOTPrinterPass(Registry);
26 initializeCallGraphViewerPass(Registry);
27 initializeCycleInfoWrapperPassPass(Registry);
28 initializeDependenceAnalysisWrapperPassPass(Registry);
29 initializeDominanceFrontierWrapperPassPass(Registry);
30 initializeDomViewerWrapperPassPass(Registry);
31 initializeDomPrinterWrapperPassPass(Registry);
32 initializeDomOnlyViewerWrapperPassPass(Registry);
33 initializePostDomViewerWrapperPassPass(Registry);
34 initializeDomOnlyPrinterWrapperPassPass(Registry);
35 initializePostDomPrinterWrapperPassPass(Registry);
36 initializePostDomOnlyViewerWrapperPassPass(Registry);
37 initializePostDomOnlyPrinterWrapperPassPass(Registry);
38 initializeAAResultsWrapperPassPass(Registry);
39 initializeGlobalsAAWrapperPassPass(Registry);
40 initializeIVUsersWrapperPassPass(Registry);
41 initializeIRSimilarityIdentifierWrapperPassPass(Registry);
42 initializeLazyBranchProbabilityInfoPassPass(Registry);
43 initializeLazyBlockFrequencyInfoPassPass(Registry);
44 initializeLazyValueInfoWrapperPassPass(Registry);
45 initializeLoopInfoWrapperPassPass(Registry);
46 initializeMemoryDependenceWrapperPassPass(Registry);
47 initializeModuleSummaryIndexWrapperPassPass(Registry);
48 initializeOptimizationRemarkEmitterWrapperPassPass(Registry);
49 initializePhiValuesWrapperPassPass(Registry);
50 initializePostDominatorTreeWrapperPassPass(Registry);
51 initializeRegionInfoPassPass(Registry);
52 initializeRegionViewerPass(Registry);
53 initializeRegionPrinterPass(Registry);
54 initializeRegionOnlyViewerPass(Registry);
55 initializeRegionOnlyPrinterPass(Registry);
56 initializeSCEVAAWrapperPassPass(Registry);
57 initializeScalarEvolutionWrapperPassPass(Registry);
58 initializeStackSafetyGlobalInfoWrapperPassPass(Registry);
59 initializeStackSafetyInfoWrapperPassPass(Registry);
60 initializeTargetTransformInfoWrapperPassPass(Registry);
61 initializeTypeBasedAAWrapperPassPass(Registry);
62 initializeScopedNoAliasAAWrapperPassPass(Registry);
63 initializeLCSSAVerificationPassPass(Registry);
64 initializeMemorySSAWrapperPassPass(Registry);
65 }
66
LLVMVerifyModule(LLVMModuleRef M,LLVMVerifierFailureAction Action,char ** OutMessages)67 LLVMBool LLVMVerifyModule(LLVMModuleRef M, LLVMVerifierFailureAction Action,
68 char **OutMessages) {
69 raw_ostream *DebugOS = Action != LLVMReturnStatusAction ? &errs() : nullptr;
70 std::string Messages;
71 raw_string_ostream MsgsOS(Messages);
72
73 LLVMBool Result = verifyModule(*unwrap(M), OutMessages ? &MsgsOS : DebugOS);
74
75 // Duplicate the output to stderr.
76 if (DebugOS && OutMessages)
77 *DebugOS << MsgsOS.str();
78
79 if (Action == LLVMAbortProcessAction && Result)
80 report_fatal_error("Broken module found, compilation aborted!");
81
82 if (OutMessages)
83 *OutMessages = strdup(MsgsOS.str().c_str());
84
85 return Result;
86 }
87
LLVMVerifyFunction(LLVMValueRef Fn,LLVMVerifierFailureAction Action)88 LLVMBool LLVMVerifyFunction(LLVMValueRef Fn, LLVMVerifierFailureAction Action) {
89 LLVMBool Result = verifyFunction(
90 *unwrap<Function>(Fn), Action != LLVMReturnStatusAction ? &errs()
91 : nullptr);
92
93 if (Action == LLVMAbortProcessAction && Result)
94 report_fatal_error("Broken function found, compilation aborted!");
95
96 return Result;
97 }
98
LLVMViewFunctionCFG(LLVMValueRef Fn)99 void LLVMViewFunctionCFG(LLVMValueRef Fn) {
100 Function *F = unwrap<Function>(Fn);
101 F->viewCFG();
102 }
103
LLVMViewFunctionCFGOnly(LLVMValueRef Fn)104 void LLVMViewFunctionCFGOnly(LLVMValueRef Fn) {
105 Function *F = unwrap<Function>(Fn);
106 F->viewCFGOnly();
107 }
108