| 1 |
// Bug c++/83871 - wrong code due to attributes on distinct template |
| 2 |
// specializations |
| 3 |
// Test to verify that an explicit template specifialization does not |
| 4 |
// "inherit" attributes always_inline or noinline from a primary template |
| 5 |
// declared with either. The test disables optimization to verify that |
| 6 |
// always_inline forces inlining. |
| 7 |
// { dg-do compile } |
| 8 |
// { dg-options "-O0 -Wall -fdump-tree-optimized" } |
| 9 |
|
| 10 |
enum Special { }; |
| 11 |
|
| 12 |
template <class T> |
| 13 |
inline void __attribute__ ((always_inline)) |
| 14 |
falways_inline_none () |
| 15 |
{ |
| 16 |
// Primary template should always be inlined, even without optimization. |
| 17 |
asm (""); // induce a no-op "side-effect" |
| 18 |
} |
| 19 |
|
| 20 |
template <> |
| 21 |
inline void |
| 22 |
falways_inline_none<Special>() |
| 23 |
{ |
| 24 |
// The specialization should not be inlined without optimization, even |
| 25 |
// though it's declared inline. |
| 26 |
asm (""); |
| 27 |
} |
| 28 |
|
| 29 |
// Verify that a call to the primary is inlined but one to |
| 30 |
// the explicit specialization is not. |
| 31 |
|
| 32 |
void test_elim_primary_1 (void) |
| 33 |
{ |
| 34 |
// Should be inlined. |
| 35 |
falways_inline_none<void>(); |
| 36 |
// { dg-final { scan-tree-dump-not "falways_inline_none<void> *\\(\\)" "optimized" } } |
| 37 |
} |
| 38 |
|
| 39 |
void test_keep_special_1 (void) |
| 40 |
{ |
| 41 |
// Should not be inlined. |
| 42 |
falways_inline_none<Special>(); |
| 43 |
// { dg-final { scan-tree-dump-times "falways_inline_none<Special> *\\(\\);" 1 "optimized" } } |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
template <class T> |
| 48 |
inline void __attribute__ ((always_inline)) |
| 49 |
falways_inline_noinline () |
| 50 |
{ |
| 51 |
asm (""); // induce a no-op "side-effect" |
| 52 |
} |
| 53 |
|
| 54 |
template <> |
| 55 |
void __attribute__ ((noinline)) |
| 56 |
falways_inline_noinline<Special>() { asm (""); } |
| 57 |
|
| 58 |
// Verify that a call to the primary is inlined but one to |
| 59 |
// the explicit specialization is not. |
| 60 |
|
| 61 |
void test_elim_primary_2 (void) |
| 62 |
{ |
| 63 |
falways_inline_noinline<void>(); |
| 64 |
// { dg-final { scan-tree-dump-not "falways_inline_noinline<void> *\\(\\)" "optimized" } } |
| 65 |
} |
| 66 |
|
| 67 |
void test_keep_special_2 (void) |
| 68 |
{ |
| 69 |
falways_inline_noinline<Special>(); |
| 70 |
// { dg-final { scan-tree-dump-times "falways_inline_noinline<Special> *\\(\\);" 1 "optimized" } } |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
template <class T> |
| 75 |
inline void |
| 76 |
fnone_always_inline () |
| 77 |
{ |
| 78 |
asm (""); // induce a no-op "side-effect" |
| 79 |
} |
| 80 |
|
| 81 |
template <> |
| 82 |
inline void __attribute__ ((always_inline)) |
| 83 |
fnone_always_inline<Special>() { asm (""); } |
| 84 |
|
| 85 |
// Verify that a call to the primary is not inlined but one to |
| 86 |
// the explicit specialization is. |
| 87 |
|
| 88 |
void test_keep_primary_3 (void) |
| 89 |
{ |
| 90 |
fnone_always_inline<void>(); |
| 91 |
// { dg-final { scan-tree-dump-times "fnone_always_inline<void> *\\(\\);" 1 "optimized" } } |
| 92 |
} |
| 93 |
|
| 94 |
void test_elim_special_3 (void) |
| 95 |
{ |
| 96 |
fnone_always_inline<Special>(); |
| 97 |
// { dg-final { scan-tree-dump-not "fnone_always_inline<Special> *\\(\\);" "optimized" } } |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
template <class T> |
| 102 |
void __attribute__ ((noinline)) |
| 103 |
fnoinline_always_inline () |
| 104 |
{ |
| 105 |
asm (""); // induce a no-op "side-effect" |
| 106 |
} |
| 107 |
|
| 108 |
template <> |
| 109 |
inline void __attribute__ ((always_inline)) |
| 110 |
fnoinline_always_inline<Special>() // { dg-bogus "follows declaration" } |
| 111 |
{ |
| 112 |
asm (""); |
| 113 |
} |
| 114 |
|
| 115 |
// Verify that a call to the primary is not inlined but one to |
| 116 |
// the explicit specialization is. |
| 117 |
|
| 118 |
void test_keep_primary_4 (void) |
| 119 |
{ |
| 120 |
fnoinline_always_inline<void>(); |
| 121 |
// { dg-final { scan-tree-dump-times "fnoinline_always_inline<void> *\\(\\);" 1 "optimized" } } |
| 122 |
} |
| 123 |
|
| 124 |
void test_elim_special_4 (void) |
| 125 |
{ |
| 126 |
fnoinline_always_inline<Special>(); |
| 127 |
// { dg-final { scan-tree-dump-not "fnoinline_always_inline<Special> *\\(\\);" "optimized" } } |
| 128 |
} |