blob: 4fd8aca4a9da42bc2dad7388669bd4e58d8d4fbb [file] [log] [blame]
Xinliang David Li26bcd192015-12-10 18:17:01 +00001/*===- InstrProfilingPort.h- Support library for PGO instrumentation ------===*\
2|*
3|* The LLVM Compiler Infrastructure
4|*
5|* This file is distributed under the University of Illinois Open Source
6|* License. See LICENSE.TXT for details.
7|*
8\*===----------------------------------------------------------------------===*/
9
10#ifndef PROFILE_INSTRPROFILING_PORT_H_
11#define PROFILE_INSTRPROFILING_PORT_H_
12
13#ifdef _MSC_VER
Xinliang David Li01d06102015-12-17 23:37:30 +000014#define COMPILER_RT_ALIGNAS(x) __declspec(align(x))
Xinliang David Liabfd5532015-12-16 03:29:15 +000015#define COMPILER_RT_VISIBILITY
Reid Kleckner652d70f2016-06-17 18:12:50 +000016/* FIXME: selectany does not have the same semantics as weak. */
Xinliang David Liabfd5532015-12-16 03:29:15 +000017#define COMPILER_RT_WEAK __declspec(selectany)
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000018/* Need to include <windows.h> */
Xinliang David Li6c7bddb2016-05-15 16:41:58 +000019#define COMPILER_RT_ALLOCA _alloca
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000020/* Need to include <stdio.h> and <io.h> */
21#define COMPILER_RT_FTRUNCATE(f,l) _chsize(_fileno(f),l)
Xinliang David Li26bcd192015-12-10 18:17:01 +000022#elif __GNUC__
Xinliang David Liabfd5532015-12-16 03:29:15 +000023#define COMPILER_RT_ALIGNAS(x) __attribute__((aligned(x)))
24#define COMPILER_RT_VISIBILITY __attribute__((visibility("hidden")))
25#define COMPILER_RT_WEAK __attribute__((weak))
Xinliang David Li6c7bddb2016-05-15 16:41:58 +000026#define COMPILER_RT_ALLOCA __builtin_alloca
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000027#define COMPILER_RT_FTRUNCATE(f,l) ftruncate(fileno(f),l)
Xinliang David Li26bcd192015-12-10 18:17:01 +000028#endif
29
Xinliang David Li274cb1d2016-05-26 22:15:12 +000030#if defined(__APPLE__)
31#define COMPILER_RT_SEG "__DATA,"
32#else
33#define COMPILER_RT_SEG ""
34#endif
35
36#ifdef _MSC_VER
37#define COMPILER_RT_SECTION(Sect) __declspec(allocate(Sect))
38#else
Xinliang David Liabfd5532015-12-16 03:29:15 +000039#define COMPILER_RT_SECTION(Sect) __attribute__((section(Sect)))
Xinliang David Li274cb1d2016-05-26 22:15:12 +000040#endif
Xinliang David Liabfd5532015-12-16 03:29:15 +000041
Vedant Kumara06e8ca2016-01-29 23:52:11 +000042#define COMPILER_RT_MAX_HOSTLEN 128
43#ifdef _MSC_VER
44#define COMPILER_RT_GETHOSTNAME(Name, Len) gethostname(Name, Len)
Paul Robinson595b9692016-05-16 17:22:32 +000045#elif defined(__ORBIS__)
Sean Silvae5e819b2016-05-27 06:15:13 +000046#define COMPILER_RT_GETHOSTNAME(Name, Len) ((void)(Name), (void)(Len), (-1))
Vedant Kumara06e8ca2016-01-29 23:52:11 +000047#else
Xinliang David Licf1a8d62016-03-06 04:18:13 +000048#define COMPILER_RT_GETHOSTNAME(Name, Len) lprofGetHostName(Name, Len)
Vedant Kumara06e8ca2016-01-29 23:52:11 +000049#define COMPILER_RT_HAS_UNAME 1
50#endif
51
Xinliang David Liabfd5532015-12-16 03:29:15 +000052#if COMPILER_RT_HAS_ATOMICS == 1
Xinliang David Lif82944d2015-12-20 19:11:44 +000053#ifdef _MSC_VER
54#include <windows.h>
Ismail Donmez7bf46bf2016-01-30 07:14:31 +000055#if _MSC_VER < 1900
Xinliang David Liec8d0862016-01-28 18:37:43 +000056#define snprintf _snprintf
Ismail Donmez7bf46bf2016-01-30 07:14:31 +000057#endif
Xinliang David Lid1c84b02015-12-20 19:55:15 +000058#if defined(_WIN64)
Xinliang David Lif82944d2015-12-20 19:11:44 +000059#define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV) \
60 (InterlockedCompareExchange64((LONGLONG volatile *)Ptr, (LONGLONG)NewV, \
61 (LONGLONG)OldV) == (LONGLONG)OldV)
Xinliang David Li21d38c52016-05-16 23:01:03 +000062#define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr) \
63 (DomType *)InterlockedExchangeAdd64((LONGLONG volatile *)&PtrVar, \
64 (LONGLONG)sizeof(DomType) * PtrIncr)
Vedant Kumar750a6292016-01-08 00:49:34 +000065#else /* !defined(_WIN64) */
Xinliang David Lid1c84b02015-12-20 19:55:15 +000066#define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV) \
67 (InterlockedCompareExchange((LONG volatile *)Ptr, (LONG)NewV, (LONG)OldV) == \
68 (LONG)OldV)
Xinliang David Li21d38c52016-05-16 23:01:03 +000069#define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr) \
70 (DomType *)InterlockedExchangeAdd((LONG volatile *)&PtrVar, \
71 (LONG)sizeof(DomType) * PtrIncr)
Xinliang David Lif82944d2015-12-20 19:11:44 +000072#endif
Vedant Kumar750a6292016-01-08 00:49:34 +000073#else /* !defined(_MSC_VER) */
Xinliang David Liabfd5532015-12-16 03:29:15 +000074#define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV) \
75 __sync_bool_compare_and_swap(Ptr, OldV, NewV)
Xinliang David Li21d38c52016-05-16 23:01:03 +000076#define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr) \
77 (DomType *)__sync_fetch_and_add((long *)&PtrVar, sizeof(DomType) * PtrIncr)
Xinliang David Lif82944d2015-12-20 19:11:44 +000078#endif
Vedant Kumar750a6292016-01-08 00:49:34 +000079#else /* COMPILER_RT_HAS_ATOMICS != 1 */
Filipe Cabecinhas19aeaf72016-03-07 13:42:17 +000080#include "InstrProfilingUtil.h"
Xinliang David Liabfd5532015-12-16 03:29:15 +000081#define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV) \
Xinliang David Licf1a8d62016-03-06 04:18:13 +000082 lprofBoolCmpXchg((void **)Ptr, OldV, NewV)
Xinliang David Li21d38c52016-05-16 23:01:03 +000083#define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr) \
84 (DomType *)lprofPtrFetchAdd((void **)&PtrVar, sizeof(DomType) * PtrIncr)
Xinliang David Liabfd5532015-12-16 03:29:15 +000085#endif
Xinliang David Li26bcd192015-12-10 18:17:01 +000086
87#define PROF_ERR(Format, ...) \
Xinliang David Li690c31f2016-05-20 05:15:42 +000088 fprintf(stderr, "LLVM Profile Error: " Format, __VA_ARGS__);
89
90#define PROF_WARN(Format, ...) \
91 fprintf(stderr, "LLVM Profile Warning: " Format, __VA_ARGS__);
Xinliang David Li26bcd192015-12-10 18:17:01 +000092
Xinliang David Li153e8b62016-06-10 20:35:01 +000093#define PROF_NOTE(Format, ...) \
94 fprintf(stderr, "LLVM Profile Note: " Format, __VA_ARGS__);
95
Xinliang David Li3e0e6e72015-12-30 19:18:55 +000096#if defined(__FreeBSD__)
Xinliang David Li26bcd192015-12-10 18:17:01 +000097
Xinliang David Li3e0e6e72015-12-30 19:18:55 +000098#include <inttypes.h>
99#include <sys/types.h>
Xinliang David Li26bcd192015-12-10 18:17:01 +0000100
Xinliang David Li3e0e6e72015-12-30 19:18:55 +0000101#else /* defined(__FreeBSD__) */
Xinliang David Li26bcd192015-12-10 18:17:01 +0000102
103#include <inttypes.h>
104#include <stdint.h>
105
106#endif /* defined(__FreeBSD__) && defined(__i386__) */
107
108#endif /* PROFILE_INSTRPROFILING_PORT_H_ */