1 //===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines pass wrappers around LLVM analyses that don't make sense to
10 // be passes. It provides a nice standard pass interface to these classes so
11 // that they can be printed out by analyze.
13 // These classes are separated out of analyze.cpp so that it is more clear which
14 // code is the integral part of the analyze tool, and which part of the code is
15 // just making it so more passes are available.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Analysis/CallGraph.h"
20 #include "llvm/IR/CallSite.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Support/raw_ostream.h"
27 /// ExternalFunctionsPassedConstants - This pass prints out call sites to
28 /// external functions that are called with constant arguments. This can be
29 /// useful when looking for standard library functions we should constant fold
30 /// or handle in alias analyses.
31 struct ExternalFunctionsPassedConstants
: public ModulePass
{
32 static char ID
; // Pass ID, replacement for typeid
33 ExternalFunctionsPassedConstants() : ModulePass(ID
) {}
34 bool runOnModule(Module
&M
) override
{
35 for (Module::iterator I
= M
.begin(), E
= M
.end(); I
!= E
; ++I
) {
36 if (!I
->isDeclaration()) continue;
38 bool PrintedFn
= false;
39 for (User
*U
: I
->users()) {
40 Instruction
*UI
= dyn_cast
<Instruction
>(U
);
43 CallSite
CS(cast
<Value
>(UI
));
46 for (CallSite::arg_iterator AI
= CS
.arg_begin(),
47 E
= CS
.arg_end(); AI
!= E
; ++AI
) {
48 if (!isa
<Constant
>(*AI
)) continue;
51 errs() << "Function '" << I
->getName() << "':\n";
63 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
69 char ExternalFunctionsPassedConstants::ID
= 0;
70 static RegisterPass
<ExternalFunctionsPassedConstants
>
71 P1("print-externalfnconstants",
72 "Print external fn callsites passed constants");