blob: aa4e829fe5ae08fb3baeaa8e632ad82d700f1856 [file] [log] [blame]
Chris Lattner781e6f52002-07-29 21:24:10 +00001//===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===//
2//
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.
6//
Misha Brukmanbc0e9982003-07-14 17:20:40 +00007// These classes are separated out of analyze.cpp so that it is more clear which
Chris Lattner781e6f52002-07-29 21:24:10 +00008// 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.
10//
11//===----------------------------------------------------------------------===//
12
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"
21
22namespace {
23 struct InstForestHelper : public FunctionPass {
24 Function *F;
25 virtual bool runOnFunction(Function &Func) { F = &Func; return false; }
26
27 void print(std::ostream &OS) const {
28 std::cout << InstForest<char>(F);
29 }
30
31 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
32 AU.setPreservesAll();
33 }
34 };
35
36 RegisterAnalysis<InstForestHelper> P1("instforest", "InstForest Printer");
37
38 struct IndVars : public FunctionPass {
39 Function *F;
40 LoopInfo *LI;
41 virtual bool runOnFunction(Function &Func) {
42 F = &Func; LI = &getAnalysis<LoopInfo>();
43 return false;
44 }
45
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)
51 IV.print(OS);
52 }
53 }
54
55 void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5f0eb8d2002-08-08 19:01:30 +000056 AU.addRequired<LoopInfo>();
Chris Lattner781e6f52002-07-29 21:24:10 +000057 AU.setPreservesAll();
58 }
59 };
60
61 RegisterAnalysis<IndVars> P6("indvars", "Induction Variable Analysis");
62
63
64 struct Exprs : public FunctionPass {
65 Function *F;
66 virtual bool runOnFunction(Function &Func) { F = &Func; return false; }
67
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) {
71 OS << *I;
72
73 if ((*I)->getType() == Type::VoidTy) continue;
Chris Lattnerc74cb862002-08-30 22:53:53 +000074 ExprType R = ClassifyExpression(*I);
Chris Lattner781e6f52002-07-29 21:24:10 +000075 if (R.Var == *I) continue; // Doesn't tell us anything
76
77 OS << "\t\tExpr =";
78 switch (R.ExprTy) {
Chris Lattnerc74cb862002-08-30 22:53:53 +000079 case ExprType::ScaledLinear:
Chris Lattner781e6f52002-07-29 21:24:10 +000080 WriteAsOperand(OS << "(", (Value*)R.Scale) << " ) *";
81 // fall through
Chris Lattnerc74cb862002-08-30 22:53:53 +000082 case ExprType::Linear:
Chris Lattner781e6f52002-07-29 21:24:10 +000083 WriteAsOperand(OS << "(", R.Var) << " )";
84 if (R.Offset == 0) break;
85 else OS << " +";
86 // fall through
Chris Lattnerc74cb862002-08-30 22:53:53 +000087 case ExprType::Constant:
Chris Lattner781e6f52002-07-29 21:24:10 +000088 if (R.Offset) WriteAsOperand(OS, (Value*)R.Offset);
89 else OS << " 0";
90 break;
91 }
92 OS << "\n\n";
93 }
94 }
95 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
96 AU.setPreservesAll();
97 }
98 };
99
100 RegisterAnalysis<Exprs> P7("exprs", "Expression Printer");
101}