1 //===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===//
3 // This file defines pass wrappers around LLVM analyses that don't make sense to
4 // be passes. It provides a nice standard pass interface to these classes so
5 // that they can be printed out by analyze.
7 // These classes are seperated out of analyze.cpp so that it is more clear which
8 // code is the integral part of the analyze tool, and which part of the code is
9 // just making it so more passes are available.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/iPHINode.h"
14 #include "llvm/Type.h"
15 #include "llvm/Assembly/Writer.h"
16 #include "llvm/Analysis/InstForest.h"
17 #include "llvm/Analysis/Expressions.h"
18 #include "llvm/Analysis/InductionVariable.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/Support/InstIterator.h"
23 struct InstForestHelper : public FunctionPass {
25 virtual bool runOnFunction(Function &Func) { F = &Func; return false; }
27 void print(std::ostream &OS) const {
28 std::cout << InstForest<char>(F);
31 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
36 RegisterAnalysis<InstForestHelper> P1("instforest", "InstForest Printer");
38 struct IndVars : public FunctionPass {
41 virtual bool runOnFunction(Function &Func) {
42 F = &Func; LI = &getAnalysis<LoopInfo>();
46 void print(std::ostream &OS) const {
47 for (inst_iterator I = inst_begin(*F), E = inst_end(*F); I != E; ++I)
48 if (PHINode *PN = dyn_cast<PHINode>(*I)) {
49 InductionVariable IV(PN, LI);
50 if (IV.InductionType != InductionVariable::Unknown)
55 void getAnalysisUsage(AnalysisUsage &AU) const {
56 AU.addRequired<LoopInfo>();
61 RegisterAnalysis<IndVars> P6("indvars", "Induction Variable Analysis");
64 struct Exprs : public FunctionPass {
66 virtual bool runOnFunction(Function &Func) { F = &Func; return false; }
68 void print(std::ostream &OS) const {
69 OS << "Classified expressions for: " << F->getName() << "\n";
70 for (inst_iterator I = inst_begin(*F), E = inst_end(*F); I != E; ++I) {
73 if ((*I)->getType() == Type::VoidTy) continue;
74 ExprType R = ClassifyExpression(*I);
75 if (R.Var == *I) continue; // Doesn't tell us anything
79 case ExprType::ScaledLinear:
80 WriteAsOperand(OS << "(", (Value*)R.Scale) << " ) *";
82 case ExprType::Linear:
83 WriteAsOperand(OS << "(", R.Var) << " )";
84 if (R.Offset == 0) break;
87 case ExprType::Constant:
88 if (R.Offset) WriteAsOperand(OS, (Value*)R.Offset);
95 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
100 RegisterAnalysis<Exprs> P7("exprs", "Expression Printer");