+//===----------------------------------------------------------------------===//
+// Analysis Group Implementation Code
+//===----------------------------------------------------------------------===//
+
+struct AnalysisGroupInfo {
+ const PassInfo *DefaultImpl;
+ std::set<const PassInfo *> Implementations;
+ AnalysisGroupInfo() : DefaultImpl(0) {}
+};
+
+static std::map<const PassInfo *, AnalysisGroupInfo> *AnalysisGroupInfoMap = 0;
+
+// RegisterAGBase implementation
+//
+RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
+ const std::type_info *Pass, bool isDefault)
+ : ImplementationInfo(0), isDefaultImplementation(isDefault) {
+
+ InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
+ if (InterfaceInfo == 0) { // First reference to Interface, add it now.
+ InterfaceInfo = // Create the new PassInfo for the interface...
+ new PassInfo("", "", Interface, PassInfo::AnalysisGroup, 0, 0);
+ registerPass(InterfaceInfo);
+ PIObj = 0;
+ }
+ assert(InterfaceInfo->getPassType() == PassInfo::AnalysisGroup &&
+ "Trying to join an analysis group that is a normal pass!");
+
+ if (Pass) {
+ ImplementationInfo = Pass::lookupPassInfo(*Pass);
+ assert(ImplementationInfo &&
+ "Must register pass before adding to AnalysisGroup!");
+
+ // Make sure we keep track of the fact that the implementation implements
+ // the interface.
+ PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
+ IIPI->addInterfaceImplemented(InterfaceInfo);
+
+ // Lazily allocate to avoid nasty initialization order dependencies
+ if (AnalysisGroupInfoMap == 0)
+ AnalysisGroupInfoMap = new std::map<const PassInfo *,AnalysisGroupInfo>();
+
+ AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
+ assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
+ "Cannot add a pass to the same analysis group more than once!");
+ AGI.Implementations.insert(ImplementationInfo);
+ if (isDefault) {
+ assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
+ "Default implementation for analysis group already specified!");
+ assert(ImplementationInfo->getNormalCtor() &&
+ "Cannot specify pass as default if it does not have a default ctor");
+ AGI.DefaultImpl = ImplementationInfo;
+ InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
+ }
+ }
+}
+
+void RegisterAGBase::setGroupName(const char *Name) {
+ assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
+ InterfaceInfo->setPassName(Name);
+}
+
+RegisterAGBase::~RegisterAGBase() {
+ if (ImplementationInfo) {
+ assert(AnalysisGroupInfoMap && "Inserted into map, but map doesn't exist?");
+ AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
+
+ assert(AGI.Implementations.count(ImplementationInfo) &&
+ "Pass not a member of analysis group?");
+
+ if (AGI.DefaultImpl == ImplementationInfo)
+ AGI.DefaultImpl = 0;
+
+ AGI.Implementations.erase(ImplementationInfo);
+
+ // Last member of this analysis group? Unregister PassInfo, delete map entry
+ if (AGI.Implementations.empty()) {
+ assert(AGI.DefaultImpl == 0 &&
+ "Default implementation didn't unregister?");
+ AnalysisGroupInfoMap->erase(InterfaceInfo);
+ if (AnalysisGroupInfoMap->empty()) { // Delete map if empty
+ delete AnalysisGroupInfoMap;
+ AnalysisGroupInfoMap = 0;
+ }
+
+ unregisterPass(InterfaceInfo);
+ }
+ }
+}