blob: 2ae1da84a9a0feb9bbd31feacdea2d0745d22c2b [file] [log] [blame]
Chris Lattner59aa81c2002-07-29 21:24:10 +00001//===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Misha Brukman650ba8e2005-04-22 00:00:37 +00006//
John Criswell09344dc2003-10-20 17:47:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner59aa81c2002-07-29 21:24:10 +00008//
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.
12//
Misha Brukman7fdaab42003-07-14 17:20:40 +000013// These classes are separated out of analyze.cpp so that it is more clear which
Chris Lattner59aa81c2002-07-29 21:24:10 +000014// 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.
16//
17//===----------------------------------------------------------------------===//
18
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000019#include "llvm/Analysis/CallGraph.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Module.h"
Chris Lattner9dcd6392004-04-02 20:56:33 +000021#include "llvm/Pass.h"
Dan Gohmand8db3762009-07-15 16:35:29 +000022#include "llvm/Support/raw_ostream.h"
Brian Gaeke960707c2003-11-11 22:41:34 +000023using namespace llvm;
24
Chris Lattner59aa81c2002-07-29 21:24:10 +000025namespace {
Chris Lattner8082c742004-05-27 06:13:36 +000026 /// ExternalFunctionsPassedConstants - This pass prints out call sites to
27 /// external functions that are called with constant arguments. This can be
28 /// useful when looking for standard library functions we should constant fold
29 /// or handle in alias analyses.
Chris Lattner4f2cf032004-09-20 04:48:05 +000030 struct ExternalFunctionsPassedConstants : public ModulePass {
Devang Patel8c78a0b2007-05-03 01:11:54 +000031 static char ID; // Pass ID, replacement for typeid
Owen Andersona7aed182010-08-06 18:33:48 +000032 ExternalFunctionsPassedConstants() : ModulePass(ID) {}
Craig Toppere56917c2014-03-08 08:27:28 +000033 bool runOnModule(Module &M) override {
Chris Lattner13626022009-08-23 06:03:38 +000034 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
35 if (!I->isDeclaration()) continue;
NAKAMURA Takumi6515aef2014-01-20 15:47:15 +000036
Chris Lattner13626022009-08-23 06:03:38 +000037 bool PrintedFn = false;
Chandler Carruthcdf47882014-03-09 03:16:01 +000038 for (User *U : I->users()) {
39 Instruction *UI = dyn_cast<Instruction>(U);
40 if (!UI) continue;
NAKAMURA Takumi6515aef2014-01-20 15:47:15 +000041
Mircea Trofin4213bc72020-04-15 16:50:57 -070042 CallBase *CB = dyn_cast<CallBase>(UI);
43 if (!CB)
44 continue;
NAKAMURA Takumi6515aef2014-01-20 15:47:15 +000045
Mircea Trofin4213bc72020-04-15 16:50:57 -070046 for (auto AI = CB->arg_begin(), E = CB->arg_end(); AI != E; ++AI) {
Chris Lattner13626022009-08-23 06:03:38 +000047 if (!isa<Constant>(*AI)) continue;
48
49 if (!PrintedFn) {
50 errs() << "Function '" << I->getName() << "':\n";
51 PrintedFn = true;
Chris Lattner8082c742004-05-27 06:13:36 +000052 }
Chandler Carruthcdf47882014-03-09 03:16:01 +000053 errs() << *UI;
Chris Lattner13626022009-08-23 06:03:38 +000054 break;
55 }
Chris Lattner8082c742004-05-27 06:13:36 +000056 }
Chris Lattner13626022009-08-23 06:03:38 +000057 }
Chris Lattner8082c742004-05-27 06:13:36 +000058
59 return false;
60 }
61
Craig Toppere56917c2014-03-08 08:27:28 +000062 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner8082c742004-05-27 06:13:36 +000063 AU.setPreservesAll();
64 }
65 };
Dan Gohman12bb5052010-08-20 00:56:16 +000066}
Chris Lattner8082c742004-05-27 06:13:36 +000067
Dan Gohman12bb5052010-08-20 00:56:16 +000068char ExternalFunctionsPassedConstants::ID = 0;
69static RegisterPass<ExternalFunctionsPassedConstants>
Duncan Sands9c40c282008-09-23 12:47:39 +000070 P1("print-externalfnconstants",
71 "Print external fn callsites passed constants");