1 //===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
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.
19 //===----------------------------------------------------------------------===//
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"
31 struct InstForestHelper : public FunctionPass {
33 virtual bool runOnFunction(Function &Func) { F = &Func; return false; }
35 void print(std::ostream &OS) const {
36 std::cout << InstForest<char>(F);
39 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
44 RegisterAnalysis<InstForestHelper> P1("instforest", "InstForest Printer");
46 struct IndVars : public FunctionPass {
49 virtual bool runOnFunction(Function &Func) {
50 F = &Func; LI = &getAnalysis<LoopInfo>();
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)
63 void getAnalysisUsage(AnalysisUsage &AU) const {
64 AU.addRequired<LoopInfo>();
69 RegisterAnalysis<IndVars> P6("indvars", "Induction Variable Analysis");
72 struct Exprs : public FunctionPass {
74 virtual bool runOnFunction(Function &Func) { F = &Func; return false; }
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) {
81 if ((*I)->getType() == Type::VoidTy) continue;
82 ExprType R = ClassifyExpression(*I);
83 if (R.Var == *I) continue; // Doesn't tell us anything
87 case ExprType::ScaledLinear:
88 WriteAsOperand(OS << "(", (Value*)R.Scale) << " ) *";
90 case ExprType::Linear:
91 WriteAsOperand(OS << "(", R.Var) << " )";
92 if (R.Offset == 0) break;
95 case ExprType::Constant:
96 if (R.Offset) WriteAsOperand(OS, (Value*)R.Offset);
103 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
104 AU.setPreservesAll();
108 RegisterAnalysis<Exprs> P7("exprs", "Expression Printer");