blob: cd1264b0e57555fcd4c9346ac382652a4615e898 [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
Vedant Kumard7c93362017-12-14 19:01:04 +000010/* This header must be included after all others so it can provide fallback
11 definitions for stuff missing in system headers. */
12
Xinliang David Li26bcd192015-12-10 18:17:01 +000013#ifndef PROFILE_INSTRPROFILING_PORT_H_
14#define PROFILE_INSTRPROFILING_PORT_H_
15
16#ifdef _MSC_VER
Xinliang David Li01d06102015-12-17 23:37:30 +000017#define COMPILER_RT_ALIGNAS(x) __declspec(align(x))
Xinliang David Liabfd5532015-12-16 03:29:15 +000018#define COMPILER_RT_VISIBILITY
Reid Kleckner652d70f2016-06-17 18:12:50 +000019/* FIXME: selectany does not have the same semantics as weak. */
Xinliang David Liabfd5532015-12-16 03:29:15 +000020#define COMPILER_RT_WEAK __declspec(selectany)
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000021/* Need to include <windows.h> */
Xinliang David Li6c7bddb2016-05-15 16:41:58 +000022#define COMPILER_RT_ALLOCA _alloca
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000023/* Need to include <stdio.h> and <io.h> */
24#define COMPILER_RT_FTRUNCATE(f,l) _chsize(_fileno(f),l)
Xinliang David Li26bcd192015-12-10 18:17:01 +000025#elif __GNUC__
Xinliang David Liabfd5532015-12-16 03:29:15 +000026#define COMPILER_RT_ALIGNAS(x) __attribute__((aligned(x)))
27#define COMPILER_RT_VISIBILITY __attribute__((visibility("hidden")))
28#define COMPILER_RT_WEAK __attribute__((weak))
Xinliang David Li6c7bddb2016-05-15 16:41:58 +000029#define COMPILER_RT_ALLOCA __builtin_alloca
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000030#define COMPILER_RT_FTRUNCATE(f,l) ftruncate(fileno(f),l)
Xinliang David Li26bcd192015-12-10 18:17:01 +000031#endif
32
Xinliang David Li274cb1d2016-05-26 22:15:12 +000033#if defined(__APPLE__)
34#define COMPILER_RT_SEG "__DATA,"
35#else
36#define COMPILER_RT_SEG ""
37#endif
38
39#ifdef _MSC_VER
40#define COMPILER_RT_SECTION(Sect) __declspec(allocate(Sect))
41#else
Xinliang David Liabfd5532015-12-16 03:29:15 +000042#define COMPILER_RT_SECTION(Sect) __attribute__((section(Sect)))
Xinliang David Li274cb1d2016-05-26 22:15:12 +000043#endif
Xinliang David Liabfd5532015-12-16 03:29:15 +000044
Vedant Kumara06e8ca2016-01-29 23:52:11 +000045#define COMPILER_RT_MAX_HOSTLEN 128
Bob Haarman9ee65ac2016-11-29 15:24:00 +000046#ifdef __ORBIS__
Sean Silvae5e819b2016-05-27 06:15:13 +000047#define COMPILER_RT_GETHOSTNAME(Name, Len) ((void)(Name), (void)(Len), (-1))
Vedant Kumara06e8ca2016-01-29 23:52:11 +000048#else
Xinliang David Licf1a8d62016-03-06 04:18:13 +000049#define COMPILER_RT_GETHOSTNAME(Name, Len) lprofGetHostName(Name, Len)
Bob Haarman9ee65ac2016-11-29 15:24:00 +000050#endif
Vedant Kumara06e8ca2016-01-29 23:52:11 +000051
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
Xinliang David Liaeff1512016-07-15 18:48:14 +000087#if defined(_WIN32)
88#define DIR_SEPARATOR '\\'
89#define DIR_SEPARATOR_2 '/'
90#else
91#define DIR_SEPARATOR '/'
92#endif
93
94#ifndef DIR_SEPARATOR_2
95#define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
96#else /* DIR_SEPARATOR_2 */
97#define IS_DIR_SEPARATOR(ch) \
98 (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
99#endif /* DIR_SEPARATOR_2 */
100
Xinliang David Li26bcd192015-12-10 18:17:01 +0000101#define PROF_ERR(Format, ...) \
Xinliang David Li690c31f2016-05-20 05:15:42 +0000102 fprintf(stderr, "LLVM Profile Error: " Format, __VA_ARGS__);
103
104#define PROF_WARN(Format, ...) \
105 fprintf(stderr, "LLVM Profile Warning: " Format, __VA_ARGS__);
Xinliang David Li26bcd192015-12-10 18:17:01 +0000106
Xinliang David Li153e8b62016-06-10 20:35:01 +0000107#define PROF_NOTE(Format, ...) \
108 fprintf(stderr, "LLVM Profile Note: " Format, __VA_ARGS__);
109
Vedant Kumard7c93362017-12-14 19:01:04 +0000110#ifndef MAP_FILE
111#define MAP_FILE 0
112#endif
113
114#ifndef O_BINARY
115#define O_BINARY 0
116#endif
117
Xinliang David Li3e0e6e72015-12-30 19:18:55 +0000118#if defined(__FreeBSD__)
Xinliang David Li26bcd192015-12-10 18:17:01 +0000119
Xinliang David Li3e0e6e72015-12-30 19:18:55 +0000120#include <inttypes.h>
121#include <sys/types.h>
Xinliang David Li26bcd192015-12-10 18:17:01 +0000122
Xinliang David Li3e0e6e72015-12-30 19:18:55 +0000123#else /* defined(__FreeBSD__) */
Xinliang David Li26bcd192015-12-10 18:17:01 +0000124
125#include <inttypes.h>
126#include <stdint.h>
127
128#endif /* defined(__FreeBSD__) && defined(__i386__) */
129
130#endif /* PROFILE_INSTRPROFILING_PORT_H_ */