cae7291

gcc vs clang tailcall optimization comparison

Jan 31st, 2016
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. #define likely(x) __builtin_expect(!!(x), 1)
  2. #define unlikely(x) __builtin_expect(!!(x), 0)
  3.  
  4. void ext(void);
  5.  
  6. void foo(int x) {
  7. if (x > 10) ext();
  8. }
  9.  
  10. void foo_1(int x) {
  11. if (likely(x > 10)) ext();
  12. }
  13.  
  14. void foo_0(int x) {
  15. if (unlikely(x > 10)) ext();
  16. }
  17.  
  18. // -----------------------------------------------------------------------------
  19. // gcc (GCC) 5.3.1 20151207 (Red Hat 5.3.1-2)
  20. // gcc -O3 -mtune=haswell -c -o tailcall.o
  21. //
  22. // tailcall.o: file format elf64-x86-64
  23. //
  24. //
  25. // Disassembly of section .text:
  26. //
  27. // 0000000000000000 <foo>:
  28. // 0: 83 ff 0a cmp $0xa,%edi
  29. // 3: 7f 0b jg 10 <L00>
  30. // 5: c3 retq
  31. // 6: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
  32. // d: 00 00 00
  33. // 10:L00 e9 00 00 00 00 jmpq 15 <L01>
  34. // 15:L01 66 66 2e 0f 1f 84 00 data16 nopw %cs:0x0(%rax,%rax,1)
  35. // 1c: 00 00 00 00
  36. //
  37. // 0000000000000020 <foo_1>:
  38. // 20: 83 ff 0a cmp $0xa,%edi
  39. // 23: 7e 0b jle 30 <L01>
  40. // 25: e9 00 00 00 00 jmpq 2a <L00>
  41. // 2a:L00 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)
  42. // 30:L01 c3 retq
  43. // 31: 66 66 66 66 66 66 2e data16 data16 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
  44. // 38: 0f 1f 84 00 00 00 00
  45. // 3f: 00
  46. //
  47. // 0000000000000040 <foo_0>:
  48. // 40: 83 ff 0a cmp $0xa,%edi
  49. // 43: 7f 0b jg 50 <L00>
  50. // 45: c3 retq
  51. // 46: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
  52. // 4d: 00 00 00
  53. // 50:L00 e9 00 00 00 00 jmpq 55 <foo_0+0x15>
  54.  
  55. // -----------------------------------------------------------------------------
  56. // clang version 3.5.0 (tags/RELEASE_350/final)
  57. // clang -O3 -mtune=haswell -c -o tailcall.o
  58. //
  59. // tailcall.o: file format elf64-x86-64
  60. //
  61. //
  62. // Disassembly of section .text:
  63. //
  64. // 0000000000000000 <foo>:
  65. // 0: 83 ff 0b cmp $0xb,%edi
  66. // 3: 7c 05 jl a <L00>
  67. // 5: e9 00 00 00 00 jmpq a <L00>
  68. // a:L00 c3 retq
  69. // b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)
  70. //
  71. // 0000000000000010 <foo_1>:
  72. // 10: 83 ff 0b cmp $0xb,%edi
  73. // 13: 7c 05 jl 1a <L00>
  74. // 15: e9 00 00 00 00 jmpq 1a <L00>
  75. // 1a:L00 c3 retq
  76. // 1b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)
  77. //
  78. // 0000000000000020 <foo_0>:
  79. // 20: 83 ff 0b cmp $0xb,%edi
  80. // 23: 7d 01 jge 26 <L00>
  81. // 25: c3 retq
  82. // 26:L00 e9 00 00 00 00 jmpq 2b <foo_0+0xb>
  83.  
  84. // -----------------------------------------------------------------------------
  85. // gcc (GCC) 5.3.1 20151207 (Red Hat 5.3.1-2)
  86. // gcc -O3 -mtune=haswell -S -o tailcall.s
  87. // .file "tailcall.c"
  88. // .section .text.unlikely,"ax",@progbits
  89. // .LCOLDB0:
  90. // .text
  91. // .LHOTB0:
  92. // .p2align 4,,15
  93. // .globl foo
  94. // .type foo, @function
  95. // foo:
  96. // .LFB0:
  97. // .cfi_startproc
  98. // cmpl $10, %edi
  99. // jg .L4
  100. // ret
  101. // .p2align 4,,10
  102. // .p2align 3
  103. // .L4:
  104. // jmp ext
  105. // .cfi_endproc
  106. // .LFE0:
  107. // .size foo, .-foo
  108. // .section .text.unlikely
  109. // .LCOLDE0:
  110. // .text
  111. // .LHOTE0:
  112. // .section .text.unlikely
  113. // .LCOLDB1:
  114. // .text
  115. // .LHOTB1:
  116. // .p2align 4,,15
  117. // .globl foo_1
  118. // .type foo_1, @function
  119. // foo_1:
  120. // .LFB1:
  121. // .cfi_startproc
  122. // cmpl $10, %edi
  123. // jle .L5
  124. // jmp ext
  125. // .p2align 4,,10
  126. // .p2align 3
  127. // .L5:
  128. // ret
  129. // .cfi_endproc
  130. // .LFE1:
  131. // .size foo_1, .-foo_1
  132. // .section .text.unlikely
  133. // .LCOLDE1:
  134. // .text
  135. // .LHOTE1:
  136. // .section .text.unlikely
  137. // .LCOLDB2:
  138. // .text
  139. // .LHOTB2:
  140. // .p2align 4,,15
  141. // .globl foo_0
  142. // .type foo_0, @function
  143. // foo_0:
  144. // .LFB2:
  145. // .cfi_startproc
  146. // cmpl $10, %edi
  147. // jg .L9
  148. // ret
  149. // .p2align 4,,10
  150. // .p2align 3
  151. // .L9:
  152. // jmp ext
  153. // .cfi_endproc
  154. // .LFE2:
  155. // .size foo_0, .-foo_0
  156. // .section .text.unlikely
  157. // .LCOLDE2:
  158. // .text
  159. // .LHOTE2:
  160. // .ident "GCC: (GNU) 5.3.1 20151207 (Red Hat 5.3.1-2)"
  161. // .section .note.GNU-stack,"",@progbits
  162.  
  163. // -----------------------------------------------------------------------------
  164. // clang version 3.5.0 (tags/RELEASE_350/final)
  165. // clang -O3 -mtune=haswell -S -o tailcall.s
  166. // .text
  167. // .file "tailcall.c"
  168. // .globl foo
  169. // .align 16, 0x90
  170. // .type foo,@function
  171. // foo: # @foo
  172. // .cfi_startproc
  173. // # BB#0:
  174. // cmpl $11, %edi
  175. // jl .LBB0_1
  176. // # BB#2:
  177. // jmp ext # TAILCALL
  178. // .LBB0_1:
  179. // retq
  180. // .Ltmp0:
  181. // .size foo, .Ltmp0-foo
  182. // .cfi_endproc
  183. //
  184. // .globl foo_1
  185. // .align 16, 0x90
  186. // .type foo_1,@function
  187. // foo_1: # @foo_1
  188. // .cfi_startproc
  189. // # BB#0:
  190. // cmpl $11, %edi
  191. // jl .LBB1_1
  192. // # BB#2:
  193. // jmp ext # TAILCALL
  194. // .LBB1_1:
  195. // retq
  196. // .Ltmp1:
  197. // .size foo_1, .Ltmp1-foo_1
  198. // .cfi_endproc
  199. //
  200. // .globl foo_0
  201. // .align 16, 0x90
  202. // .type foo_0,@function
  203. // foo_0: # @foo_0
  204. // .cfi_startproc
  205. // # BB#0:
  206. // cmpl $11, %edi
  207. // jge .LBB2_2
  208. // # BB#1:
  209. // retq
  210. // .LBB2_2:
  211. // jmp ext # TAILCALL
  212. // .Ltmp2:
  213. // .size foo_0, .Ltmp2-foo_0
  214. // .cfi_endproc
  215. //
  216. //
  217. // .ident "clang version 3.5.0 (tags/RELEASE_350/final)"
  218. // .section ".note.GNU-stack","",@progbits
Advertisement
Add Comment
Please, Sign In to add comment