blob: 17509c12447e4e64efad3d495e8e7752bdf93df4 [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
16#define COMPILER_RT_WEAK __declspec(selectany)
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000017/* Need to include <windows.h> */
Xinliang David Li6c7bddb2016-05-15 16:41:58 +000018#define COMPILER_RT_ALLOCA _alloca
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000019/* Need to include <stdio.h> and <io.h> */
20#define COMPILER_RT_FTRUNCATE(f,l) _chsize(_fileno(f),l)
Xinliang David Li26bcd192015-12-10 18:17:01 +000021#elif __GNUC__
Xinliang David Liabfd5532015-12-16 03:29:15 +000022#define COMPILER_RT_ALIGNAS(x) __attribute__((aligned(x)))
23#define COMPILER_RT_VISIBILITY __attribute__((visibility("hidden")))
24#define COMPILER_RT_WEAK __attribute__((weak))
Xinliang David Li6c7bddb2016-05-15 16:41:58 +000025#define COMPILER_RT_ALLOCA __builtin_alloca
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000026#define COMPILER_RT_FTRUNCATE(f,l) ftruncate(fileno(f),l)
Xinliang David Li26bcd192015-12-10 18:17:01 +000027#endif
28
Xinliang David Li274cb1d2016-05-26 22:15:12 +000029#if defined(__APPLE__)
30#define COMPILER_RT_SEG "__DATA,"
31#else
32#define COMPILER_RT_SEG ""
33#endif
34
35#ifdef _MSC_VER
36#define COMPILER_RT_SECTION(Sect) __declspec(allocate(Sect))
37#else
Xinliang David Liabfd5532015-12-16 03:29:15 +000038#define COMPILER_RT_SECTION(Sect) __attribute__((section(Sect)))
Xinliang David Li274cb1d2016-05-26 22:15:12 +000039#endif
Xinliang David Liabfd5532015-12-16 03:29:15 +000040
Vedant Kumara06e8ca2016-01-29 23:52:11 +000041#define COMPILER_RT_MAX_HOSTLEN 128
42#ifdef _MSC_VER
43#define COMPILER_RT_GETHOSTNAME(Name, Len) gethostname(Name, Len)
Paul Robinson595b9692016-05-16 17:22:32 +000044#elif defined(__ORBIS__)
Sean Silvae5e819b2016-05-27 06:15:13 +000045#define COMPILER_RT_GETHOSTNAME(Name, Len) ((void)(Name), (void)(Len), (-1))
Vedant Kumara06e8ca2016-01-29 23:52:11 +000046#else
Xinliang David Licf1a8d62016-03-06 04:18:13 +000047#define COMPILER_RT_GETHOSTNAME(Name, Len) lprofGetHostName(Name, Len)
Vedant Kumara06e8ca2016-01-29 23:52:11 +000048#define COMPILER_RT_HAS_UNAME 1
49#endif
50
Xinliang David Liabfd5532015-12-16 03:29:15 +000051#if COMPILER_RT_HAS_ATOMICS == 1
Xinliang David Lif82944d2015-12-20 19:11:44 +000052#ifdef _MSC_VER
53#include <windows.h>
Ismail Donmez7bf46bf2016-01-30 07:14:31 +000054#if _MSC_VER < 1900
Xinliang David Liec8d0862016-01-28 18:37:43 +000055#define snprintf _snprintf
Ismail Donmez7bf46bf2016-01-30 07:14:31 +000056#endif
Xinliang David Lid1c84b02015-12-20 19:55:15 +000057#if defined(_WIN64)
Xinliang David Lif82944d2015-12-20 19:11:44 +000058#define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV) \
59 (InterlockedCompareExchange64((LONGLONG volatile *)Ptr, (LONGLONG)NewV, \
60 (LONGLONG)OldV) == (LONGLONG)OldV)
Xinliang David Li21d38c52016-05-16 23:01:03 +000061#define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr) \
62 (DomType *)InterlockedExchangeAdd64((LONGLONG volatile *)&PtrVar, \
63 (LONGLONG)sizeof(DomType) * PtrIncr)
Vedant Kumar750a6292016-01-08 00:49:34 +000064#else /* !defined(_WIN64) */
Xinliang David Lid1c84b02015-12-20 19:55:15 +000065#define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV) \
66 (InterlockedCompareExchange((LONG volatile *)Ptr, (LONG)NewV, (LONG)OldV) == \
67 (LONG)OldV)
Xinliang David Li21d38c52016-05-16 23:01:03 +000068#define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr) \
69 (DomType *)InterlockedExchangeAdd((LONG volatile *)&PtrVar, \
70 (LONG)sizeof(DomType) * PtrIncr)
Xinliang David Lif82944d2015-12-20 19:11:44 +000071#endif
Vedant Kumar750a6292016-01-08 00:49:34 +000072#else /* !defined(_MSC_VER) */
Xinliang David Liabfd5532015-12-16 03:29:15 +000073#define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV) \
74 __sync_bool_compare_and_swap(Ptr, OldV, NewV)
Xinliang David Li21d38c52016-05-16 23:01:03 +000075#define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr) \
76 (DomType *)__sync_fetch_and_add((long *)&PtrVar, sizeof(DomType) * PtrIncr)
Xinliang David Lif82944d2015-12-20 19:11:44 +000077#endif
Vedant Kumar750a6292016-01-08 00:49:34 +000078#else /* COMPILER_RT_HAS_ATOMICS != 1 */
Filipe Cabecinhas19aeaf72016-03-07 13:42:17 +000079#include "InstrProfilingUtil.h"
Xinliang David Liabfd5532015-12-16 03:29:15 +000080#define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV) \
Xinliang David Licf1a8d62016-03-06 04:18:13 +000081 lprofBoolCmpXchg((void **)Ptr, OldV, NewV)
Xinliang David Li21d38c52016-05-16 23:01:03 +000082#define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr) \
83 (DomType *)lprofPtrFetchAdd((void **)&PtrVar, sizeof(DomType) * PtrIncr)
Xinliang David Liabfd5532015-12-16 03:29:15 +000084#endif
Xinliang David Li26bcd192015-12-10 18:17:01 +000085
86#define PROF_ERR(Format, ...) \
Xinliang David Li690c31f2016-05-20 05:15:42 +000087 fprintf(stderr, "LLVM Profile Error: " Format, __VA_ARGS__);
88
89#define PROF_WARN(Format, ...) \
90 fprintf(stderr, "LLVM Profile Warning: " Format, __VA_ARGS__);
Xinliang David Li26bcd192015-12-10 18:17:01 +000091
Xinliang David Li153e8b62016-06-10 20:35:01 +000092#define PROF_NOTE(Format, ...) \
93 fprintf(stderr, "LLVM Profile Note: " Format, __VA_ARGS__);
94
Xinliang David Li3e0e6e72015-12-30 19:18:55 +000095#if defined(__FreeBSD__)
Xinliang David Li26bcd192015-12-10 18:17:01 +000096
Xinliang David Li3e0e6e72015-12-30 19:18:55 +000097#include <inttypes.h>
98#include <sys/types.h>
Xinliang David Li26bcd192015-12-10 18:17:01 +000099
Xinliang David Li3e0e6e72015-12-30 19:18:55 +0000100#else /* defined(__FreeBSD__) */
Xinliang David Li26bcd192015-12-10 18:17:01 +0000101
102#include <inttypes.h>
103#include <stdint.h>
104
105#endif /* defined(__FreeBSD__) && defined(__i386__) */
106
107#endif /* PROFILE_INSTRPROFILING_PORT_H_ */