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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 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 | // |
Misha Brukman | bc0e998 | 2003-07-14 17:20:40 +0000 | [diff] [blame] | 14 | // 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] | 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 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | struct InstForestHelper : public FunctionPass { |
| 33 | Function *F; |
| 34 | virtual bool runOnFunction(Function &Func) { F = &Func; return false; } |
| 35 | |
| 36 | void print(std::ostream &OS) const { |
| 37 | std::cout << InstForest<char>(F); |
| 38 | } |
| 39 | |
| 40 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 41 | AU.setPreservesAll(); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | RegisterAnalysis<InstForestHelper> P1("instforest", "InstForest Printer"); |
| 46 | |
| 47 | struct IndVars : public FunctionPass { |
| 48 | Function *F; |
| 49 | LoopInfo *LI; |
| 50 | virtual bool runOnFunction(Function &Func) { |
| 51 | F = &Func; LI = &getAnalysis<LoopInfo>(); |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | void print(std::ostream &OS) const { |
| 56 | for (inst_iterator I = inst_begin(*F), E = inst_end(*F); I != E; ++I) |
| 57 | if (PHINode *PN = dyn_cast<PHINode>(*I)) { |
| 58 | InductionVariable IV(PN, LI); |
| 59 | if (IV.InductionType != InductionVariable::Unknown) |
| 60 | IV.print(OS); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 5f0eb8d | 2002-08-08 19:01:30 +0000 | [diff] [blame] | 65 | AU.addRequired<LoopInfo>(); |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 66 | AU.setPreservesAll(); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | RegisterAnalysis<IndVars> P6("indvars", "Induction Variable Analysis"); |
| 71 | |
| 72 | |
| 73 | struct Exprs : public FunctionPass { |
| 74 | Function *F; |
| 75 | virtual bool runOnFunction(Function &Func) { F = &Func; return false; } |
| 76 | |
| 77 | void print(std::ostream &OS) const { |
| 78 | OS << "Classified expressions for: " << F->getName() << "\n"; |
| 79 | for (inst_iterator I = inst_begin(*F), E = inst_end(*F); I != E; ++I) { |
| 80 | OS << *I; |
| 81 | |
| 82 | if ((*I)->getType() == Type::VoidTy) continue; |
Chris Lattner | ac1ccae | 2003-12-23 09:41:45 +0000 | [diff] [blame] | 83 | ExprType R = ClassifyExpr(*I); |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 84 | if (R.Var == *I) continue; // Doesn't tell us anything |
| 85 | |
| 86 | OS << "\t\tExpr ="; |
| 87 | switch (R.ExprTy) { |
Chris Lattner | c74cb86 | 2002-08-30 22:53:53 +0000 | [diff] [blame] | 88 | case ExprType::ScaledLinear: |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 89 | WriteAsOperand(OS << "(", (Value*)R.Scale) << " ) *"; |
| 90 | // fall through |
Chris Lattner | c74cb86 | 2002-08-30 22:53:53 +0000 | [diff] [blame] | 91 | case ExprType::Linear: |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 92 | WriteAsOperand(OS << "(", R.Var) << " )"; |
| 93 | if (R.Offset == 0) break; |
| 94 | else OS << " +"; |
| 95 | // fall through |
Chris Lattner | c74cb86 | 2002-08-30 22:53:53 +0000 | [diff] [blame] | 96 | case ExprType::Constant: |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 97 | if (R.Offset) WriteAsOperand(OS, (Value*)R.Offset); |
| 98 | else OS << " 0"; |
| 99 | break; |
| 100 | } |
| 101 | OS << "\n\n"; |
| 102 | } |
| 103 | } |
| 104 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 105 | AU.setPreservesAll(); |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | RegisterAnalysis<Exprs> P7("exprs", "Expression Printer"); |
| 110 | } |