1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- C++ -*-===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file defines stuff that is used to define and "use" Analysis Passes.
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is automatically #included by Pass.h, so:
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//           NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Instead, #include Pass.h
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef LLVM_PASS_ANALYSIS_SUPPORT_H
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LLVM_PASS_ANALYSIS_SUPPORT_H
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/SmallVector.h"
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/StringRef.h"
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <vector>
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace llvm {
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// AnalysisUsage - Represent the analysis usage information of a pass.  This
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// tracks analyses that the pass REQUIRES (must be available when the pass
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// runs), REQUIRES TRANSITIVE (must be available throughout the lifetime of the
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// pass), and analyses that the pass PRESERVES (the pass does not invalidate the
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// results of these analyses).  This information is provided by a pass to the
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Pass infrastructure through the getAnalysisUsage virtual function.
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass AnalysisUsage {
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef SmallVector<AnalysisID, 32> VectorType;
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprivate:
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Sets of analyses required and preserved by a pass
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  VectorType Required, RequiredTransitive, Preserved;
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool PreservesAll;
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AnalysisUsage() : PreservesAll(false) {}
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // addRequired - Add the specified ID to the required set of the usage info
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // for a pass.
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AnalysisUsage &addRequiredID(const void *ID);
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AnalysisUsage &addRequiredID(char &ID);
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  template<class PassClass>
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AnalysisUsage &addRequired() {
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return addRequiredID(PassClass::ID);
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AnalysisUsage &addRequiredTransitiveID(char &ID);
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  template<class PassClass>
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AnalysisUsage &addRequiredTransitive() {
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return addRequiredTransitiveID(PassClass::ID);
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // addPreserved - Add the specified ID to the set of analyses preserved by
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // this pass
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AnalysisUsage &addPreservedID(const void *ID) {
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Preserved.push_back(ID);
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return *this;
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AnalysisUsage &addPreservedID(char &ID) {
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Preserved.push_back(&ID);
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return *this;
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // addPreserved - Add the specified Pass class to the set of analyses
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // preserved by this pass.
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  template<class PassClass>
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AnalysisUsage &addPreserved() {
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Preserved.push_back(&PassClass::ID);
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return *this;
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // addPreserved - Add the Pass with the specified argument string to the set
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // of analyses preserved by this pass. If no such Pass exists, do nothing.
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // This can be useful when a pass is trivially preserved, but may not be
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // linked in. Be careful about spelling!
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AnalysisUsage &addPreserved(StringRef Arg);
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // setPreservesAll - Set by analyses that do not transform their input at all
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void setPreservesAll() { PreservesAll = true; }
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool getPreservesAll() const { return PreservesAll; }
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// setPreservesCFG - This function should be called by the pass, iff they do
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// not:
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///  1. Add or remove basic blocks from the function
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///  2. Modify terminator instructions in any way.
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// This function annotates the AnalysisUsage info object to say that analyses
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// that only depend on the CFG are preserved by this pass.
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void setPreservesCFG();
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const VectorType &getRequiredSet() const { return Required; }
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const VectorType &getRequiredTransitiveSet() const {
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return RequiredTransitive;
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const VectorType &getPreservedSet() const { return Preserved; }
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// AnalysisResolver - Simple interface used by Pass objects to pull all
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// analysis information out of pass manager that is responsible to manage
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// the pass.
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass PMDataManager;
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass AnalysisResolver {
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprivate:
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AnalysisResolver();  // DO NOT IMPLEMENT
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  explicit AnalysisResolver(PMDataManager &P) : PM(P) { }
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  inline PMDataManager &getPMDataManager() { return PM; }
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Find pass that is implementing PI.
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Pass *findImplPass(AnalysisID PI) {
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Pass *ResultPass = 0;
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i < AnalysisImpls.size() ; ++i) {
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (AnalysisImpls[i].first == PI) {
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ResultPass = AnalysisImpls[i].second;
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ResultPass;
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Find pass that is implementing PI. Initialize pass for Function F.
142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Pass *findImplPass(Pass *P, AnalysisID PI, Function &F);
143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void addAnalysisImplsPair(AnalysisID PI, Pass *P) {
14519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (findImplPass(PI) == P)
14619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return;
147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::pair<AnalysisID, Pass*> pir = std::make_pair(PI,P);
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AnalysisImpls.push_back(pir);
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// clearAnalysisImpls - Clear cache that is used to connect a pass to the
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// the analysis (PassInfo).
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void clearAnalysisImpls() {
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AnalysisImpls.clear();
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprivate:
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // AnalysisImpls - This keeps track of which passes implements the interfaces
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // that are required by the current pass (to implement getAnalysis()).
163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<std::pair<AnalysisID, Pass*> > AnalysisImpls;
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // PassManager that is used to resolve analysis info
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PMDataManager &PM;
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// get analysis information that might be around, for example to update it.
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This is different than getAnalysis in that it can fail (if the analysis
172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// results haven't been computed), so should only be used if you can handle
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the case when the analysis is not available.  This method is often used by
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// transformation APIs to update analysis results for a pass automatically as
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the transform is performed.
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<typename AnalysisType>
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAnalysisType *Pass::getAnalysisIfAvailable() const {
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Resolver && "Pass not resident in a PassManager object!");
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const void *PI = &AnalysisType::ID;
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI, true);
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ResultPass == 0) return 0;
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Because the AnalysisType may not be a subclass of pass (for
187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // adjust the return pointer (because the class may multiply inherit, once
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // from pass, once from AnalysisType).
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return (AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getAnalysis<AnalysisType>() - This function is used by subclasses to get
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// to the analysis information that they claim to use by overriding the
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getAnalysisUsage function.
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<typename AnalysisType>
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAnalysisType &Pass::getAnalysis() const {
199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Resolver && "Pass has not been inserted into a PassManager object!");
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getAnalysisID<AnalysisType>(&AnalysisType::ID);
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<typename AnalysisType>
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAnalysisType &Pass::getAnalysisID(AnalysisID PI) const {
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(PI && "getAnalysis for unregistered pass!");
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Resolver&&"Pass has not been inserted into a PassManager object!");
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // PI *must* appear in AnalysisImpls.  Because the number of passes used
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // should be a small number, we just do a linear search over a (dense)
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // vector.
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Pass *ResultPass = Resolver->findImplPass(PI);
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert (ResultPass &&
212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          "getAnalysis*() called on an analysis that was not "
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          "'required' by pass!");
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Because the AnalysisType may not be a subclass of pass (for
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // adjust the return pointer (because the class may multiply inherit, once
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // from pass, once from AnalysisType).
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getAnalysis<AnalysisType>() - This function is used by subclasses to get
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// to the analysis information that they claim to use by overriding the
224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getAnalysisUsage function.
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<typename AnalysisType>
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAnalysisType &Pass::getAnalysis(Function &F) {
228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Resolver &&"Pass has not been inserted into a PassManager object!");
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getAnalysisID<AnalysisType>(&AnalysisType::ID, F);
231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<typename AnalysisType>
234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAnalysisType &Pass::getAnalysisID(AnalysisID PI, Function &F) {
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(PI && "getAnalysis for unregistered pass!");
236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Resolver && "Pass has not been inserted into a PassManager object!");
237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // PI *must* appear in AnalysisImpls.  Because the number of passes used
238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // should be a small number, we just do a linear search over a (dense)
239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // vector.
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Pass *ResultPass = Resolver->findImplPass(this, PI, F);
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(ResultPass && "Unable to find requested analysis info");
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Because the AnalysisType may not be a subclass of pass (for
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // adjust the return pointer (because the class may multiply inherit, once
246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // from pass, once from AnalysisType).
247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // End llvm namespace
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
253