/// its pointer, name, and types registered in the class struture.
void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
StartObjCMethod(OMD, OMD->getClassInterface(), OMD->getLocStart());
+ PGO.assignRegionCounters(OMD, CurFn);
assert(isa<CompoundStmt>(OMD->getBody()));
+ RegionCounter Cnt = getPGORegionCounter(OMD->getBody());
+ Cnt.beginRegion(Builder);
EmitCompoundStmtWithoutScope(*cast<CompoundStmt>(OMD->getBody()));
FinishFunction(OMD->getBodyRBrace());
+ PGO.emitWriteoutFunction();
+ PGO.destroyRegionCounters();
}
/// emitStructGetterCall - Call the runtime function to load a property
while (CurPtr < BufferEnd) {
// Read the function name.
const char *FuncStart = CurPtr;
- CurPtr = strchr(CurPtr, ' ');
+ // For Objective-C methods, the name may include whitespace, so search
+ // backward from the end of the line to find the space that separates the
+ // name from the number of counters. (This is a temporary hack since we are
+ // going to completely replace this file format in the near future.)
+ CurPtr = strchr(CurPtr, '\n');
if (!CurPtr) {
ReportBadPGOData(CGM, "pgo data file has malformed function entry");
return;
}
+ while (*--CurPtr != ' ')
+ ;
StringRef FuncName(FuncStart, CurPtr - FuncStart);
// Read the number of counters.
const char *CurPtr = DataBuffer->getBufferStart() + OffsetIter->getValue();
// Skip over the function name.
- CurPtr = strchr(CurPtr, ' ');
+ CurPtr = strchr(CurPtr, '\n');
assert(CurPtr && "pgo-data has corrupted function entry");
+ while (*--CurPtr != ' ')
+ ;
// Read the number of counters.
char *EndPtr;
(*CounterMap)[S->getBody()] = NextCounter++;
Visit(S->getBody());
}
+ void VisitObjCMethodDecl(const ObjCMethodDecl *S) {
+ (*CounterMap)[S->getBody()] = NextCounter++;
+ Visit(S->getBody());
+ }
/// Assign a counter to track the block following a label.
void VisitLabelStmt(const LabelStmt *S) {
(*CounterMap)[S] = NextCounter++;
Visit(S->getBody());
}
+ void VisitObjCMethodDecl(const ObjCMethodDecl *S) {
+ RegionCounter Cnt(PGO, S->getBody());
+ Cnt.beginRegion();
+ (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
+ Visit(S->getBody());
+ }
+
void VisitReturnStmt(const ReturnStmt *S) {
RecordStmtCount(S);
if (S->getRetValue())
MapRegionCounters Walker(RegionCounterMap);
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
Walker.VisitFunctionDecl(FD);
+ else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
+ Walker.VisitObjCMethodDecl(MD);
NumRegionCounters = Walker.NextCounter;
}
ComputeRegionCounts Walker(StmtCountMap, *this);
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
Walker.VisitFunctionDecl(FD);
+ else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
+ Walker.VisitObjCMethodDecl(MD);
}
void CodeGenPGO::emitCounterVariables() {
// RUN: %clang %s -o - -emit-llvm -S -fprofile-instr-use=%S/Inputs/instr-profile.profdata | FileCheck -check-prefix=PGOUSE %s
#ifdef HAVE_FOUNDATION
+
// Use this to build an instrumented version to regenerate the input file.
#import <Foundation/Foundation.h>
+
#else
+
+// Minimal definitions to get this to compile without Foundation.h.
+
+@protocol NSObject
+@end
+
+@interface NSObject <NSObject>
+- (id)init;
++ (id)alloc;
+@end
+
struct NSFastEnumerationState;
-@interface NSArray
+@interface NSArray : NSObject
- (unsigned long) countByEnumeratingWithState: (struct NSFastEnumerationState*) state
objects: (id*) buffer
count: (unsigned long) bufferSize;
// PGOGEN: @[[FOR:__llvm_pgo_ctr[0-9]*]] = private global [2 x i64] zeroinitializer
-// PGOGEN-LABEL: @foreach
-// PGOUSE-LABEL: @foreach
+@interface A : NSObject
++ (void)foreach: (NSArray *)array;
+@end
+
+@implementation A
+// PGOGEN-LABEL: define {{.*}}+[A foreach:]
+// PGOUSE-LABEL: define {{.*}}+[A foreach:]
// PGOGEN: store {{.*}} @[[FOR]], i64 0, i64 0
-void foreach(NSArray *array) {
++ (void)foreach: (NSArray *)array
+{
// PGOGEN: store {{.*}} @[[FOR]], i64 0, i64 1
// FIXME: We don't emit branch weights for this yet.
for (id x in array) {
}
}
+@end
int main(int argc, const char *argv[]) {
+ A *a = [[A alloc] init];
NSArray *array = [NSArray arrayWithObjects: @"0", @"1", (void*)0];
- foreach(array);
+ [A foreach: array];
+ return 0;
}