1//===-- Analysis.cpp ------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm-c/Analysis.h"
11#include "llvm-c/Initialization.h"
12#include "llvm/IR/Module.h"
13#include "llvm/IR/Verifier.h"
14#include "llvm/InitializePasses.h"
15#include "llvm/PassRegistry.h"
16#include "llvm/Support/raw_ostream.h"
17#include <cstring>
18
19using namespace llvm;
20
21/// initializeAnalysis - Initialize all passes linked into the Analysis library.
22void llvm::initializeAnalysis(PassRegistry &Registry) {
23  initializeAAEvalPass(Registry);
24  initializeAliasSetPrinterPass(Registry);
25  initializeBasicAAWrapperPassPass(Registry);
26  initializeBlockFrequencyInfoWrapperPassPass(Registry);
27  initializeBranchProbabilityInfoWrapperPassPass(Registry);
28  initializeCallGraphWrapperPassPass(Registry);
29  initializeCallGraphPrinterPass(Registry);
30  initializeCallGraphViewerPass(Registry);
31  initializeCostModelAnalysisPass(Registry);
32  initializeCFGViewerPass(Registry);
33  initializeCFGPrinterPass(Registry);
34  initializeCFGOnlyViewerPass(Registry);
35  initializeCFGOnlyPrinterPass(Registry);
36  initializeCFLAAWrapperPassPass(Registry);
37  initializeDependenceAnalysisPass(Registry);
38  initializeDelinearizationPass(Registry);
39  initializeDemandedBitsPass(Registry);
40  initializeDivergenceAnalysisPass(Registry);
41  initializeDominanceFrontierPass(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  initializeIVUsersPass(Registry);
53  initializeInstCountPass(Registry);
54  initializeIntervalPartitionPass(Registry);
55  initializeLazyValueInfoPass(Registry);
56  initializeLintPass(Registry);
57  initializeLoopInfoWrapperPassPass(Registry);
58  initializeMemDepPrinterPass(Registry);
59  initializeMemDerefPrinterPass(Registry);
60  initializeMemoryDependenceAnalysisPass(Registry);
61  initializeModuleDebugInfoPrinterPass(Registry);
62  initializeObjCARCAAWrapperPassPass(Registry);
63  initializePostDominatorTreePass(Registry);
64  initializeRegionInfoPassPass(Registry);
65  initializeRegionViewerPass(Registry);
66  initializeRegionPrinterPass(Registry);
67  initializeRegionOnlyViewerPass(Registry);
68  initializeRegionOnlyPrinterPass(Registry);
69  initializeSCEVAAWrapperPassPass(Registry);
70  initializeScalarEvolutionWrapperPassPass(Registry);
71  initializeTargetTransformInfoWrapperPassPass(Registry);
72  initializeTypeBasedAAWrapperPassPass(Registry);
73  initializeScopedNoAliasAAWrapperPassPass(Registry);
74}
75
76void LLVMInitializeAnalysis(LLVMPassRegistryRef R) {
77  initializeAnalysis(*unwrap(R));
78}
79
80void LLVMInitializeIPA(LLVMPassRegistryRef R) {
81  initializeAnalysis(*unwrap(R));
82}
83
84LLVMBool LLVMVerifyModule(LLVMModuleRef M, LLVMVerifierFailureAction Action,
85                          char **OutMessages) {
86  raw_ostream *DebugOS = Action != LLVMReturnStatusAction ? &errs() : nullptr;
87  std::string Messages;
88  raw_string_ostream MsgsOS(Messages);
89
90  LLVMBool Result = verifyModule(*unwrap(M), OutMessages ? &MsgsOS : DebugOS);
91
92  // Duplicate the output to stderr.
93  if (DebugOS && OutMessages)
94    *DebugOS << MsgsOS.str();
95
96  if (Action == LLVMAbortProcessAction && Result)
97    report_fatal_error("Broken module found, compilation aborted!");
98
99  if (OutMessages)
100    *OutMessages = strdup(MsgsOS.str().c_str());
101
102  return Result;
103}
104
105LLVMBool LLVMVerifyFunction(LLVMValueRef Fn, LLVMVerifierFailureAction Action) {
106  LLVMBool Result = verifyFunction(
107      *unwrap<Function>(Fn), Action != LLVMReturnStatusAction ? &errs()
108                                                              : nullptr);
109
110  if (Action == LLVMAbortProcessAction && Result)
111    report_fatal_error("Broken function found, compilation aborted!");
112
113  return Result;
114}
115
116void LLVMViewFunctionCFG(LLVMValueRef Fn) {
117  Function *F = unwrap<Function>(Fn);
118  F->viewCFG();
119}
120
121void LLVMViewFunctionCFGOnly(LLVMValueRef Fn) {
122  Function *F = unwrap<Function>(Fn);
123  F->viewCFGOnly();
124}
125