compiler-optimization

Can the compiler optimize from heap to stack allocation?

前提是你 提交于 2021-02-05 14:16:04
问题 As far as compiler optimizations go, is it legal and/or possible to change a heap allocation to a stack allocation? Or would that break the as-if rule? For example, say this is the original version of the code { Foo* f = new Foo(); f->do_something(); delete f; } Would a compiler be able to change this to the following { Foo f{}; f.do_something(); } I wouldn't think so, because that would have implications if the original version was relying on things like custom allocators. Does the standard

Generated Assembly For Pointer Arithmetic

▼魔方 西西 提交于 2021-02-05 08:11:09
问题 This is a simple question but I just came across it. In the code snippet below I create three pointers. I know the three will exhibit equivalent behavior (all point to the same thing), but I honestly thought the third action in the code was the most "efficient", meaning that it would generate less assembly instructions to accomplish the same thing as the other two. I assumed that the first two have to first deference a pointer, and then take the memory address of the thing that was

Generated Assembly For Pointer Arithmetic

情到浓时终转凉″ 提交于 2021-02-05 08:10:08
问题 This is a simple question but I just came across it. In the code snippet below I create three pointers. I know the three will exhibit equivalent behavior (all point to the same thing), but I honestly thought the third action in the code was the most "efficient", meaning that it would generate less assembly instructions to accomplish the same thing as the other two. I assumed that the first two have to first deference a pointer, and then take the memory address of the thing that was

GCC optimizations based on integer overflow

别等时光非礼了梦想. 提交于 2021-02-05 06:54:13
问题 Recently I had a discussion about someone who wanted to check for signed int overflow like this if (A + B < 2 * max(A, B)) . Lets ignore for a second that the logic itself is wrong and discuss signed integer overflow in context of C/C++. (Which I believe fully inherits this part of standard from C). What kinds of check that need signed integer overflow will be optimized away by current-ish GCC and which won't? Since the original text wasn't all that well formulated and apparently

GCC optimizations based on integer overflow

怎甘沉沦 提交于 2021-02-05 06:53:24
问题 Recently I had a discussion about someone who wanted to check for signed int overflow like this if (A + B < 2 * max(A, B)) . Lets ignore for a second that the logic itself is wrong and discuss signed integer overflow in context of C/C++. (Which I believe fully inherits this part of standard from C). What kinds of check that need signed integer overflow will be optimized away by current-ish GCC and which won't? Since the original text wasn't all that well formulated and apparently

Conditional move (cmov) in GCC compiler

こ雲淡風輕ζ 提交于 2021-02-04 08:08:25
问题 I saw somewhere that the GCC compiler might prefer sometimes not using conditional mov when converting my code into ASM. What are the cases where it might choose to do something other than conditional mov? 回答1: Compilers often favour if-conversion to cmov when both sides of the branch are short, especially with a ternary so you always assign a C variable. e.g. if(x) y=bar; sometimes doesn't optimize to CMOV but y = x ? bar : y; does use CMOV more often. Especially when y is an array entry

Conditional move (cmov) in GCC compiler

喜你入骨 提交于 2021-02-04 08:06:37
问题 I saw somewhere that the GCC compiler might prefer sometimes not using conditional mov when converting my code into ASM. What are the cases where it might choose to do something other than conditional mov? 回答1: Compilers often favour if-conversion to cmov when both sides of the branch are short, especially with a ternary so you always assign a C variable. e.g. if(x) y=bar; sometimes doesn't optimize to CMOV but y = x ? bar : y; does use CMOV more often. Especially when y is an array entry

Trying to understand clang/gcc __builtin_memset on constant size / aligned pointers

喜你入骨 提交于 2021-01-28 22:06:33
问题 Basically I am trying to understand why both gcc/clang use xmm register for their __builtin_memset even when the memory destination and size are both divisible by sizeof ymm (or zmm for that matter) and the CPU supports AVX2 / AVX512 . and why GCC implements __builtin_memset on medium sized values without any SIMD (again assuming CPU supports SIMD). For example: __builtin_memset(__builtin_assume_aligned(ptr, 64), -1, 64)); Will compile to: vpcmpeqd %xmm0, %xmm0, %xmm0 vmovdqa %xmm0, (%rdi)

What optimization benefit does `pointer[restrict static 1]` bring when declare a pointer like this?

对着背影说爱祢 提交于 2021-01-27 17:13:29
问题 I'm reading through the source code of a library (QNNPack) and noticed this line (https://github.com/pytorch/QNNPACK/blob/24d57f21503ba8ab0f8bb5d24148754a91266b9c/src/q8gemm/6x4-neon.c#L23): void funcName(..., const union some_union_type some_union_arg[restrict static 1]) { // ... } I understand the keyword restrict and static in general, but I'm afraid I do not know the reason behind this. I didn't find anything on Google, perhaps I searched wrong. I'm guessing here it's a way of telling the

What optimization benefit does `pointer[restrict static 1]` bring when declare a pointer like this?

风格不统一 提交于 2021-01-27 16:52:20
问题 I'm reading through the source code of a library (QNNPack) and noticed this line (https://github.com/pytorch/QNNPACK/blob/24d57f21503ba8ab0f8bb5d24148754a91266b9c/src/q8gemm/6x4-neon.c#L23): void funcName(..., const union some_union_type some_union_arg[restrict static 1]) { // ... } I understand the keyword restrict and static in general, but I'm afraid I do not know the reason behind this. I didn't find anything on Google, perhaps I searched wrong. I'm guessing here it's a way of telling the