clang/llvm 9 fix - do not eliminate a store to a fake "const"
authorDenys Vlasenko <vda.linux@googlemail.com>
Fri, 25 Oct 2019 10:12:22 +0000 (12:12 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 25 Oct 2019 10:12:22 +0000 (12:12 +0200)
This is *much* better (9 kbytes better) than dropping "*const"
optimization trick.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
coreutils/test.c
include/libbb.h
libbb/appletlib.c
libbb/lineedit.c
shell/ash.c

index 868ffbecbff1b0bb7da6287db6c21327069f1116..a089861307abeaf015d2f56b6d14f9c2867ae08a 100644 (file)
@@ -411,7 +411,7 @@ extern struct test_statics *const test_ptr_to_statics;
 #define leaving         (S.leaving      )
 
 #define INIT_S() do { \
-       (*(struct test_statics**)&test_ptr_to_statics) = xzalloc(sizeof(S)); \
+       (*(struct test_statics**)not_const_pp(&test_ptr_to_statics)) = xzalloc(sizeof(S)); \
        barrier(); \
 } while (0)
 #define DEINIT_S() do { \
index 111d1b790f375f1e0ce3b2aaf69bee356ddbd635..05a5609776e882c699d3dc63e825c2ad17cc0e4b 100644 (file)
@@ -2153,12 +2153,32 @@ struct globals;
  * Magic prevents ptr_to_globals from going into rodata.
  * If you want to assign a value, use SET_PTR_TO_GLOBALS(x) */
 extern struct globals *const ptr_to_globals;
+
+#if defined(__clang_major__) && __clang_major__ >= 9
+/* Clang/llvm drops assignment to "constant" storage. Silently.
+ * Needs serious convincing to not eliminate the store.
+ */
+static ALWAYS_INLINE void* not_const_pp(const void *p)
+{
+       void *pp;
+       __asm__ __volatile__(
+               "# forget that p points to const"
+               : /*outputs*/ "=r" (pp)
+               : /*inputs*/ "0" (p)
+       );
+       return pp;
+}
+#else
+static ALWAYS_INLINE void* not_const_pp(const void *p) { return (void*)p; }
+#endif
+
 /* At least gcc 3.4.6 on mipsel system needs optimization barrier */
 #define barrier() __asm__ __volatile__("":::"memory")
 #define SET_PTR_TO_GLOBALS(x) do { \
-       (*(struct globals**)&ptr_to_globals) = (void*)(x); \
+       (*(struct globals**)not_const_pp(&ptr_to_globals)) = (void*)(x); \
        barrier(); \
 } while (0)
+
 #define FREE_PTR_TO_GLOBALS() do { \
        if (ENABLE_FEATURE_CLEAN_UP) { \
                free(ptr_to_globals); \
index 9fa17cfa1b2c2314870807760c5ab2ebb423616e..f842e73cc28d6a8b8d05bc48f620dd6f3d0bdda4 100644 (file)
@@ -304,7 +304,7 @@ void lbb_prepare(const char *applet
                IF_FEATURE_INDIVIDUAL(, char **argv))
 {
 #ifdef __GLIBC__
-       (*(int **)&bb_errno) = __errno_location();
+       (*(int **)not_const_pp(&bb_errno)) = __errno_location();
        barrier();
 #endif
        applet_name = applet;
index fbabc6c1204813bd6ef9b1d8ad02bce856047578..b1ec52b88c19034acac96b297246320689733c11 100644 (file)
@@ -203,7 +203,7 @@ extern struct lineedit_statics *const lineedit_ptr_to_statics;
 #define delbuf           (S.delbuf          )
 
 #define INIT_S() do { \
-       (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \
+       (*(struct lineedit_statics**)not_const_pp(&lineedit_ptr_to_statics)) = xzalloc(sizeof(S)); \
        barrier(); \
        cmdedit_termw = 80; \
        IF_USERNAME_OR_HOMEDIR(home_pwd_buf = (char*)null_str;) \
index c5588ea667b10c701747854b1df0379b26cee461..4b5eafa7cbc66d07507cc04d951ae4ecd1e6101d 100644 (file)
@@ -489,7 +489,7 @@ extern struct globals_misc *BB_GLOBAL_CONST ash_ptr_to_globals_misc;
 #define random_gen  (G_misc.random_gen )
 #define backgndpid  (G_misc.backgndpid )
 #define INIT_G_misc() do { \
-       (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \
+       (*(struct globals_misc**)not_const_pp(&ash_ptr_to_globals_misc)) = xzalloc(sizeof(G_misc)); \
        barrier(); \
        curdir = nullstr; \
        physdir = nullstr; \
@@ -1542,7 +1542,7 @@ extern struct globals_memstack *BB_GLOBAL_CONST ash_ptr_to_globals_memstack;
 #define g_stacknleft (G_memstack.g_stacknleft)
 #define stackbase    (G_memstack.stackbase   )
 #define INIT_G_memstack() do { \
-       (*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \
+       (*(struct globals_memstack**)not_const_pp(&ash_ptr_to_globals_memstack)) = xzalloc(sizeof(G_memstack)); \
        barrier(); \
        g_stackp = &stackbase; \
        g_stacknxt = stackbase.space; \
@@ -2165,7 +2165,7 @@ extern struct globals_var *BB_GLOBAL_CONST ash_ptr_to_globals_var;
 #endif
 #define INIT_G_var() do { \
        unsigned i; \
-       (*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \
+       (*(struct globals_var**)not_const_pp(&ash_ptr_to_globals_var)) = xzalloc(sizeof(G_var)); \
        barrier(); \
        for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \
                varinit[i].flags    = varinit_data[i].flags; \