You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

optimization.h 9.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // Copyright 2017 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // -----------------------------------------------------------------------------
  17. // File: optimization.h
  18. // -----------------------------------------------------------------------------
  19. //
  20. // This header file defines portable macros for performance optimization.
  21. #ifndef ABSL_BASE_OPTIMIZATION_H_
  22. #define ABSL_BASE_OPTIMIZATION_H_
  23. #include <assert.h>
  24. #include "absl/base/config.h"
  25. // ABSL_BLOCK_TAIL_CALL_OPTIMIZATION
  26. //
  27. // Instructs the compiler to avoid optimizing tail-call recursion. This macro is
  28. // useful when you wish to preserve the existing function order within a stack
  29. // trace for logging, debugging, or profiling purposes.
  30. //
  31. // Example:
  32. //
  33. // int f() {
  34. // int result = g();
  35. // ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
  36. // return result;
  37. // }
  38. #if defined(__pnacl__)
  39. #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() \
  40. if (volatile int x = 0) \
  41. { \
  42. (void)x; \
  43. }
  44. #elif defined(__clang__)
  45. // Clang will not tail call given inline volatile assembly.
  46. #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
  47. #elif defined(__GNUC__)
  48. // GCC will not tail call given inline volatile assembly.
  49. #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
  50. #elif defined(_MSC_VER)
  51. #include <intrin.h>
  52. // The __nop() intrinsic blocks the optimisation.
  53. #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __nop()
  54. #else
  55. #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() \
  56. if (volatile int x = 0) \
  57. { \
  58. (void)x; \
  59. }
  60. #endif
  61. // ABSL_CACHELINE_SIZE
  62. //
  63. // Explicitly defines the size of the L1 cache for purposes of alignment.
  64. // Setting the cacheline size allows you to specify that certain objects be
  65. // aligned on a cacheline boundary with `ABSL_CACHELINE_ALIGNED` declarations.
  66. // (See below.)
  67. //
  68. // NOTE: this macro should be replaced with the following C++17 features, when
  69. // those are generally available:
  70. //
  71. // * `std::hardware_constructive_interference_size`
  72. // * `std::hardware_destructive_interference_size`
  73. //
  74. // See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html
  75. // for more information.
  76. #if defined(__GNUC__)
  77. // Cache line alignment
  78. #if defined(__i386__) || defined(__x86_64__)
  79. #define ABSL_CACHELINE_SIZE 64
  80. #elif defined(__powerpc64__)
  81. #define ABSL_CACHELINE_SIZE 128
  82. #elif defined(__aarch64__)
  83. // We would need to read special register ctr_el0 to find out L1 dcache size.
  84. // This value is a good estimate based on a real aarch64 machine.
  85. #define ABSL_CACHELINE_SIZE 64
  86. #elif defined(__arm__)
  87. // Cache line sizes for ARM: These values are not strictly correct since
  88. // cache line sizes depend on implementations, not architectures. There
  89. // are even implementations with cache line sizes configurable at boot
  90. // time.
  91. #if defined(__ARM_ARCH_5T__)
  92. #define ABSL_CACHELINE_SIZE 32
  93. #elif defined(__ARM_ARCH_7A__)
  94. #define ABSL_CACHELINE_SIZE 64
  95. #endif
  96. #endif
  97. #ifndef ABSL_CACHELINE_SIZE
  98. // A reasonable default guess. Note that overestimates tend to waste more
  99. // space, while underestimates tend to waste more time.
  100. #define ABSL_CACHELINE_SIZE 64
  101. #endif
  102. // ABSL_CACHELINE_ALIGNED
  103. //
  104. // Indicates that the declared object be cache aligned using
  105. // `ABSL_CACHELINE_SIZE` (see above). Cacheline aligning objects allows you to
  106. // load a set of related objects in the L1 cache for performance improvements.
  107. // Cacheline aligning objects properly allows constructive memory sharing and
  108. // prevents destructive (or "false") memory sharing.
  109. //
  110. // NOTE: callers should replace uses of this macro with `alignas()` using
  111. // `std::hardware_constructive_interference_size` and/or
  112. // `std::hardware_destructive_interference_size` when C++17 becomes available to
  113. // them.
  114. //
  115. // See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html
  116. // for more information.
  117. //
  118. // On some compilers, `ABSL_CACHELINE_ALIGNED` expands to an `__attribute__`
  119. // or `__declspec` attribute. For compilers where this is not known to work,
  120. // the macro expands to nothing.
  121. //
  122. // No further guarantees are made here. The result of applying the macro
  123. // to variables and types is always implementation-defined.
  124. //
  125. // WARNING: It is easy to use this attribute incorrectly, even to the point
  126. // of causing bugs that are difficult to diagnose, crash, etc. It does not
  127. // of itself guarantee that objects are aligned to a cache line.
  128. //
  129. // NOTE: Some compilers are picky about the locations of annotations such as
  130. // this attribute, so prefer to put it at the beginning of your declaration.
  131. // For example,
  132. //
  133. // ABSL_CACHELINE_ALIGNED static Foo* foo = ...
  134. //
  135. // class ABSL_CACHELINE_ALIGNED Bar { ...
  136. //
  137. // Recommendations:
  138. //
  139. // 1) Consult compiler documentation; this comment is not kept in sync as
  140. // toolchains evolve.
  141. // 2) Verify your use has the intended effect. This often requires inspecting
  142. // the generated machine code.
  143. // 3) Prefer applying this attribute to individual variables. Avoid
  144. // applying it to types. This tends to localize the effect.
  145. #define ABSL_CACHELINE_ALIGNED __attribute__((aligned(ABSL_CACHELINE_SIZE)))
  146. #elif defined(_MSC_VER)
  147. #define ABSL_CACHELINE_SIZE 64
  148. #define ABSL_CACHELINE_ALIGNED __declspec(align(ABSL_CACHELINE_SIZE))
  149. #else
  150. #define ABSL_CACHELINE_SIZE 64
  151. #define ABSL_CACHELINE_ALIGNED
  152. #endif
  153. // ABSL_PREDICT_TRUE, ABSL_PREDICT_FALSE
  154. //
  155. // Enables the compiler to prioritize compilation using static analysis for
  156. // likely paths within a boolean branch.
  157. //
  158. // Example:
  159. //
  160. // if (ABSL_PREDICT_TRUE(expression)) {
  161. // return result; // Faster if more likely
  162. // } else {
  163. // return 0;
  164. // }
  165. //
  166. // Compilers can use the information that a certain branch is not likely to be
  167. // taken (for instance, a CHECK failure) to optimize for the common case in
  168. // the absence of better information (ie. compiling gcc with `-fprofile-arcs`).
  169. //
  170. // Recommendation: Modern CPUs dynamically predict branch execution paths,
  171. // typically with accuracy greater than 97%. As a result, annotating every
  172. // branch in a codebase is likely counterproductive; however, annotating
  173. // specific branches that are both hot and consistently mispredicted is likely
  174. // to yield performance improvements.
  175. #if ABSL_HAVE_BUILTIN(__builtin_expect) || \
  176. (defined(__GNUC__) && !defined(__clang__))
  177. #define ABSL_PREDICT_FALSE(x) (__builtin_expect(false || (x), false))
  178. #define ABSL_PREDICT_TRUE(x) (__builtin_expect(false || (x), true))
  179. #else
  180. #define ABSL_PREDICT_FALSE(x) (x)
  181. #define ABSL_PREDICT_TRUE(x) (x)
  182. #endif
  183. // ABSL_ASSUME(cond)
  184. //
  185. // Informs the compiler that a condition is always true and that it can assume
  186. // it to be true for optimization purposes.
  187. //
  188. // WARNING: If the condition is false, the program can produce undefined and
  189. // potentially dangerous behavior.
  190. //
  191. // In !NDEBUG mode, the condition is checked with an assert().
  192. //
  193. // NOTE: The expression must not have side effects, as it may only be evaluated
  194. // in some compilation modes and not others. Some compilers may issue a warning
  195. // if the compiler cannot prove the expression has no side effects. For example,
  196. // the expression should not use a function call since the compiler cannot prove
  197. // that a function call does not have side effects.
  198. //
  199. // Example:
  200. //
  201. // int x = ...;
  202. // ABSL_ASSUME(x >= 0);
  203. // // The compiler can optimize the division to a simple right shift using the
  204. // // assumption specified above.
  205. // int y = x / 16;
  206. //
  207. #if !defined(NDEBUG)
  208. #define ABSL_ASSUME(cond) assert(cond)
  209. #elif ABSL_HAVE_BUILTIN(__builtin_assume)
  210. #define ABSL_ASSUME(cond) __builtin_assume(cond)
  211. #elif defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_unreachable)
  212. #define ABSL_ASSUME(cond) \
  213. do \
  214. { \
  215. if (!(cond)) \
  216. __builtin_unreachable(); \
  217. } while (0)
  218. #elif defined(_MSC_VER)
  219. #define ABSL_ASSUME(cond) __assume(cond)
  220. #else
  221. #define ABSL_ASSUME(cond) \
  222. do \
  223. { \
  224. static_cast<void>(false && (cond)); \
  225. } while (0)
  226. #endif
  227. // ABSL_INTERNAL_UNIQUE_SMALL_NAME(cond)
  228. // This macro forces small unique name on a static file level symbols like
  229. // static local variables or static functions. This is intended to be used in
  230. // macro definitions to optimize the cost of generated code. Do NOT use it on
  231. // symbols exported from translation unit since it may cause a link time
  232. // conflict.
  233. //
  234. // Example:
  235. //
  236. // #define MY_MACRO(txt)
  237. // namespace {
  238. // char VeryVeryLongVarName[] ABSL_INTERNAL_UNIQUE_SMALL_NAME() = txt;
  239. // const char* VeryVeryLongFuncName() ABSL_INTERNAL_UNIQUE_SMALL_NAME();
  240. // const char* VeryVeryLongFuncName() { return txt; }
  241. // }
  242. //
  243. #if defined(__GNUC__)
  244. #define ABSL_INTERNAL_UNIQUE_SMALL_NAME2(x) #x
  245. #define ABSL_INTERNAL_UNIQUE_SMALL_NAME1(x) ABSL_INTERNAL_UNIQUE_SMALL_NAME2(x)
  246. #define ABSL_INTERNAL_UNIQUE_SMALL_NAME() \
  247. asm(ABSL_INTERNAL_UNIQUE_SMALL_NAME1(.absl.__COUNTER__))
  248. #else
  249. #define ABSL_INTERNAL_UNIQUE_SMALL_NAME()
  250. #endif
  251. #endif // ABSL_BASE_OPTIMIZATION_H_