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