6c4c99f5a3055bf2b5f3e20907be479c77bbb72c
[oota-llvm.git] / tools / opt / AnalysisWrappers.cpp
1 //===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines pass wrappers around LLVM analyses that don't make sense to
11 // be passes.  It provides a nice standard pass interface to these classes so
12 // that they can be printed out by analyze.
13 //
14 // These classes are separated out of analyze.cpp so that it is more clear which
15 // code is the integral part of the analyze tool, and which part of the code is
16 // just making it so more passes are available.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #include "llvm/iPHINode.h"
21 #include "llvm/Type.h"
22 #include "llvm/Assembly/Writer.h"
23 #include "llvm/Analysis/InstForest.h"
24 #include "llvm/Analysis/Expressions.h"
25 #include "llvm/Analysis/InductionVariable.h"
26 #include "llvm/Analysis/LoopInfo.h"
27 #include "llvm/Support/InstIterator.h"
28
29 namespace {
30   struct InstForestHelper : public FunctionPass {
31     Function *F;
32     virtual bool runOnFunction(Function &Func) { F = &Func; return false; }
33
34     void print(std::ostream &OS) const {
35       std::cout << InstForest<char>(F);
36     }
37     
38     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
39       AU.setPreservesAll();
40     }
41   };
42
43   RegisterAnalysis<InstForestHelper> P1("instforest", "InstForest Printer");
44
45   struct IndVars : public FunctionPass {
46     Function *F;
47     LoopInfo *LI;
48     virtual bool runOnFunction(Function &Func) {
49       F = &Func; LI = &getAnalysis<LoopInfo>();
50       return false;
51     }
52
53     void print(std::ostream &OS) const {
54       for (inst_iterator I = inst_begin(*F), E = inst_end(*F); I != E; ++I)
55         if (PHINode *PN = dyn_cast<PHINode>(*I)) {
56           InductionVariable IV(PN, LI);
57           if (IV.InductionType != InductionVariable::Unknown)
58             IV.print(OS);
59         }
60     }
61     
62     void getAnalysisUsage(AnalysisUsage &AU) const {
63       AU.addRequired<LoopInfo>();
64       AU.setPreservesAll();
65     }
66   };
67
68   RegisterAnalysis<IndVars> P6("indvars", "Induction Variable Analysis");
69
70
71   struct Exprs : public FunctionPass {
72     Function *F;
73     virtual bool runOnFunction(Function &Func) { F = &Func; return false; }
74
75     void print(std::ostream &OS) const {
76       OS << "Classified expressions for: " << F->getName() << "\n";
77       for (inst_iterator I = inst_begin(*F), E = inst_end(*F); I != E; ++I) {
78         OS << *I;
79       
80         if ((*I)->getType() == Type::VoidTy) continue;
81         ExprType R = ClassifyExpression(*I);
82         if (R.Var == *I) continue;  // Doesn't tell us anything
83       
84         OS << "\t\tExpr =";
85         switch (R.ExprTy) {
86         case ExprType::ScaledLinear:
87           WriteAsOperand(OS << "(", (Value*)R.Scale) << " ) *";
88           // fall through
89         case ExprType::Linear:
90           WriteAsOperand(OS << "(", R.Var) << " )";
91           if (R.Offset == 0) break;
92           else OS << " +";
93           // fall through
94         case ExprType::Constant:
95           if (R.Offset) WriteAsOperand(OS, (Value*)R.Offset);
96           else OS << " 0";
97           break;
98         }
99         OS << "\n\n";
100       }
101     }
102     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
103       AU.setPreservesAll();
104     }
105   };
106
107   RegisterAnalysis<Exprs> P7("exprs", "Expression Printer");
108 }