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
17using namespace llvm;
18
19/// initializeAnalysis - Initialize all passes linked into the Analysis library.
20void 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
67LLVMBool 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(M: *unwrap(P: M), OS: 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(reason: "Broken module found, compilation aborted!");
81
82 if (OutMessages)
83 *OutMessages = strdup(s: MsgsOS.str().c_str());
84
85 return Result;
86}
87
88LLVMBool LLVMVerifyFunction(LLVMValueRef Fn, LLVMVerifierFailureAction Action) {
89 LLVMBool Result = verifyFunction(
90 F: *unwrap<Function>(P: Fn), OS: Action != LLVMReturnStatusAction ? &errs()
91 : nullptr);
92
93 if (Action == LLVMAbortProcessAction && Result)
94 report_fatal_error(reason: "Broken function found, compilation aborted!");
95
96 return Result;
97}
98
99void LLVMViewFunctionCFG(LLVMValueRef Fn) {
100 Function *F = unwrap<Function>(P: Fn);
101 F->viewCFG();
102}
103
104void LLVMViewFunctionCFGOnly(LLVMValueRef Fn) {
105 Function *F = unwrap<Function>(P: Fn);
106 F->viewCFGOnly();
107}
108