blob: 57913770bf266b4cf6ce2d703b9002092f977dbf [file] [log] [blame]
Chris Lattner781e6f52002-07-29 21:24:10 +00001//===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
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//
Chris Lattner781e6f52002-07-29 21:24:10 +000010//
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//
Misha Brukmanbc0e9982003-07-14 17:20:40 +000015// These classes are separated out of analyze.cpp so that it is more clear which
Chris Lattner781e6f52002-07-29 21:24:10 +000016// 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
30namespace {
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 {
Chris Lattner5f0eb8d2002-08-08 19:01:30 +000064 AU.addRequired<LoopInfo>();
Chris Lattner781e6f52002-07-29 21:24:10 +000065 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;
Chris Lattnerc74cb862002-08-30 22:53:53 +000082 ExprType R = ClassifyExpression(*I);
Chris Lattner781e6f52002-07-29 21:24:10 +000083 if (R.Var == *I) continue; // Doesn't tell us anything
84
85 OS << "\t\tExpr =";
86 switch (R.ExprTy) {
Chris Lattnerc74cb862002-08-30 22:53:53 +000087 case ExprType::ScaledLinear:
Chris Lattner781e6f52002-07-29 21:24:10 +000088 WriteAsOperand(OS << "(", (Value*)R.Scale) << " ) *";
89 // fall through
Chris Lattnerc74cb862002-08-30 22:53:53 +000090 case ExprType::Linear:
Chris Lattner781e6f52002-07-29 21:24:10 +000091 WriteAsOperand(OS << "(", R.Var) << " )";
92 if (R.Offset == 0) break;
93 else OS << " +";
94 // fall through
Chris Lattnerc74cb862002-08-30 22:53:53 +000095 case ExprType::Constant:
Chris Lattner781e6f52002-07-29 21:24:10 +000096 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}