aa4e829fe5ae08fb3baeaa8e632ad82d700f1856
[oota-llvm.git] / tools / analyze / AnalysisWrappers.cpp
1 //===- 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 //
7 // These classes are separated 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.
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
22 namespace {
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 {
56       AU.addRequired<LoopInfo>();
57       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;
74         ExprType R = ClassifyExpression(*I);
75         if (R.Var == *I) continue;  // Doesn't tell us anything
76       
77         OS << "\t\tExpr =";
78         switch (R.ExprTy) {
79         case ExprType::ScaledLinear:
80           WriteAsOperand(OS << "(", (Value*)R.Scale) << " ) *";
81           // fall through
82         case ExprType::Linear:
83           WriteAsOperand(OS << "(", R.Var) << " )";
84           if (R.Offset == 0) break;
85           else OS << " +";
86           // fall through
87         case ExprType::Constant:
88           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 }