Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 1 | //===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===// |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame^] | 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 | // |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 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 | // |
Misha Brukman | bc0e998 | 2003-07-14 17:20:40 +0000 | [diff] [blame] | 15 | // These classes are separated out of analyze.cpp so that it is more clear which |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 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 { |
Chris Lattner | 5f0eb8d | 2002-08-08 19:01:30 +0000 | [diff] [blame] | 64 | AU.addRequired<LoopInfo>(); |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 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; |
Chris Lattner | c74cb86 | 2002-08-30 22:53:53 +0000 | [diff] [blame] | 82 | ExprType R = ClassifyExpression(*I); |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 83 | if (R.Var == *I) continue; // Doesn't tell us anything |
| 84 | |
| 85 | OS << "\t\tExpr ="; |
| 86 | switch (R.ExprTy) { |
Chris Lattner | c74cb86 | 2002-08-30 22:53:53 +0000 | [diff] [blame] | 87 | case ExprType::ScaledLinear: |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 88 | WriteAsOperand(OS << "(", (Value*)R.Scale) << " ) *"; |
| 89 | // fall through |
Chris Lattner | c74cb86 | 2002-08-30 22:53:53 +0000 | [diff] [blame] | 90 | case ExprType::Linear: |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 91 | WriteAsOperand(OS << "(", R.Var) << " )"; |
| 92 | if (R.Offset == 0) break; |
| 93 | else OS << " +"; |
| 94 | // fall through |
Chris Lattner | c74cb86 | 2002-08-30 22:53:53 +0000 | [diff] [blame] | 95 | case ExprType::Constant: |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 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 | } |