blob: 4b5b97e41f322daa54eedb4696a8c21c26810a0d [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- Annotation.cpp - Implement the Annotation Classes -----------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner8dc89a32001-08-23 17:07:56 +00009//
10// This file implements the AnnotationManager class.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer551ccae2004-09-01 22:55:40 +000014#include "llvm/Support/Annotation.h"
Chris Lattnerc055a912006-10-04 22:13:11 +000015#include "llvm/Support/ManagedStatic.h"
Owen Anderson521db562009-06-22 22:44:15 +000016#include "llvm/System/RWMutex.h"
Misha Brukman710d1ce2004-11-09 04:27:19 +000017#include <map>
Nuno Lopes440954c2008-11-27 16:42:44 +000018#include <cstring>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Chris Lattner1cd4c722004-02-26 07:24:18 +000021Annotation::~Annotation() {} // Designed to be subclassed
22
23Annotable::~Annotable() { // Virtual because it's designed to be subclassed...
24 Annotation *A = AnnotationList;
25 while (A) {
26 Annotation *Next = A->getNext();
27 delete A;
28 A = Next;
29 }
30}
31
Nuno Lopes4ba9aac2008-11-27 16:37:02 +000032namespace {
33 class StrCmp {
34 public:
Nick Lewycky28ea4f62008-12-08 00:45:02 +000035 bool operator()(const char *a, const char *b) const {
Nuno Lopes4ba9aac2008-11-27 16:37:02 +000036 return strcmp(a, b) < 0;
37 }
38 };
39}
40
41typedef std::map<const char*, unsigned, StrCmp> IDMapType;
Owen Anderson5ec56cc2009-06-30 05:33:46 +000042static volatile sys::cas_flag IDCounter = 0; // Unique ID counter
Chris Lattner8dc89a32001-08-23 17:07:56 +000043
44// Static member to ensure initialiation on demand.
Chris Lattnerc055a912006-10-04 22:13:11 +000045static ManagedStatic<IDMapType> IDMap;
Owen Anderson521db562009-06-22 22:44:15 +000046static ManagedStatic<sys::SmartRWMutex<true> > AnnotationsLock;
Chris Lattner8dc89a32001-08-23 17:07:56 +000047
48// On demand annotation creation support...
Chris Lattnerc0f483d2001-09-07 16:44:01 +000049typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
Chris Lattner01e770a2003-05-22 21:59:35 +000050typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
Chris Lattner3fa61eb2003-01-13 00:52:43 +000051
Owen Anderson521db562009-06-22 22:44:15 +000052static ManagedStatic<FactMapType> TheFactMap;
Chris Lattner3fa61eb2003-01-13 00:52:43 +000053static FactMapType &getFactMap() {
Chris Lattner3fa61eb2003-01-13 00:52:43 +000054 return *TheFactMap;
55}
56
57static void eraseFromFactMap(unsigned ID) {
Owen Anderson521db562009-06-22 22:44:15 +000058 sys::SmartScopedWriter<true> Writer(&*AnnotationsLock);
Chris Lattner3fa61eb2003-01-13 00:52:43 +000059 TheFactMap->erase(ID);
Chris Lattner3fa61eb2003-01-13 00:52:43 +000060}
Chris Lattner8dc89a32001-08-23 17:07:56 +000061
Nuno Lopes08d67c72008-11-26 00:00:44 +000062AnnotationID AnnotationManager::getID(const char *Name) { // Name -> ID
Owen Anderson521db562009-06-22 22:44:15 +000063 AnnotationsLock->reader_acquire();
Chris Lattnerc055a912006-10-04 22:13:11 +000064 IDMapType::iterator I = IDMap->find(Name);
Owen Anderson521db562009-06-22 22:44:15 +000065 IDMapType::iterator E = IDMap->end();
66 AnnotationsLock->reader_release();
67
68 if (I == E) {
69 sys::SmartScopedWriter<true> Writer(&*AnnotationsLock);
70 I = IDMap->find(Name);
Owen Andersoncb737342009-06-26 18:09:03 +000071 if (I == IDMap->end()) {
72 unsigned newCount = sys::AtomicIncrement(&IDCounter);
73 (*IDMap)[Name] = newCount-1; // Add a new element
74 return AnnotationID(newCount-1);
75 } else
76 return AnnotationID(I->second);
Chris Lattner8dc89a32001-08-23 17:07:56 +000077 }
Dan Gohmanb5660dc2008-02-20 16:44:09 +000078 return AnnotationID(I->second);
Chris Lattner8dc89a32001-08-23 17:07:56 +000079}
80
Chris Lattner82072d42001-09-09 21:02:38 +000081// getID - Name -> ID + registration of a factory function for demand driven
82// annotation support.
Nuno Lopes08d67c72008-11-26 00:00:44 +000083AnnotationID AnnotationManager::getID(const char *Name, Factory Fact,
Misha Brukman710d1ce2004-11-09 04:27:19 +000084 void *Data) {
Chris Lattner82072d42001-09-09 21:02:38 +000085 AnnotationID Result(getID(Name));
86 registerAnnotationFactory(Result, Fact, Data);
Misha Brukmanf976c852005-04-21 22:55:34 +000087 return Result;
Chris Lattner82072d42001-09-09 21:02:38 +000088}
89
Chris Lattner8dc89a32001-08-23 17:07:56 +000090// getName - This function is especially slow, but that's okay because it should
91// only be used for debugging.
92//
Nuno Lopes08d67c72008-11-26 00:00:44 +000093const char *AnnotationManager::getName(AnnotationID ID) { // ID -> Name
Owen Anderson521db562009-06-22 22:44:15 +000094 sys::SmartScopedReader<true> Reader(&*AnnotationsLock);
Chris Lattnerc055a912006-10-04 22:13:11 +000095 IDMapType &TheMap = *IDMap;
Chris Lattner8dc89a32001-08-23 17:07:56 +000096 for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
97 assert(I != TheMap.end() && "Annotation ID is unknown!");
98 if (I->second == ID.ID) return I->first;
99 }
100}
101
Chris Lattner8dc89a32001-08-23 17:07:56 +0000102// registerAnnotationFactory - This method is used to register a callback
Misha Brukmanf976c852005-04-21 22:55:34 +0000103// function used to create an annotation on demand if it is needed by the
Chris Lattner8dc89a32001-08-23 17:07:56 +0000104// Annotable::findOrCreateAnnotation method.
105//
Misha Brukman710d1ce2004-11-09 04:27:19 +0000106void AnnotationManager::registerAnnotationFactory(AnnotationID ID, AnnFactory F,
107 void *ExtraData) {
Owen Anderson521db562009-06-22 22:44:15 +0000108 if (F) {
109 sys::SmartScopedWriter<true> Writer(&*AnnotationsLock);
Chris Lattner01e770a2003-05-22 21:59:35 +0000110 getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
Owen Anderson521db562009-06-22 22:44:15 +0000111 } else {
Chris Lattner3fa61eb2003-01-13 00:52:43 +0000112 eraseFromFactMap(ID.ID);
Owen Anderson521db562009-06-22 22:44:15 +0000113 }
Chris Lattner8dc89a32001-08-23 17:07:56 +0000114}
115
116// createAnnotation - Create an annotation of the specified ID for the
117// specified object, using a register annotation creation function.
118//
Misha Brukmanf976c852005-04-21 22:55:34 +0000119Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
Misha Brukman710d1ce2004-11-09 04:27:19 +0000120 const Annotable *Obj) {
Owen Anderson521db562009-06-22 22:44:15 +0000121 AnnotationsLock->reader_acquire();
Chris Lattner8dc89a32001-08-23 17:07:56 +0000122 FactMapType::iterator I = getFactMap().find(ID.ID);
Owen Anderson521db562009-06-22 22:44:15 +0000123 if (I == getFactMap().end()) {
124 AnnotationsLock->reader_release();
125 return 0;
126 }
127
128 AnnotationsLock->reader_release();
Chris Lattnerda8f0042001-08-27 05:19:10 +0000129 return I->second.first(ID, Obj, I->second.second);
Chris Lattner8dc89a32001-08-23 17:07:56 +0000130}