This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RFC: folding of literal array refs


Hi, 

recently I noticed that code like 

 if ("foo"[0] == 'g')
   return 1;
 return 0;

is properly const folded, while 

 if (((const char*)"foo")[0] == 'g')
   return 1;
 return 0;

is not. This is because the types don't match. the first variant has a 
"readonly" in the type node, the 2nd one doesn't. 


the patch below fixes this, but I'm unsure if its the most correct approach 
(no regtest failures): 

--- fold-const.c        (revision 113296)
+++ fold-const.c        (working copy)
@@ -12220,7 +12220,8 @@
        }
 
       if (string
-         && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (string))
+         && lang_hooks.types_compatible_p (TREE_TYPE (exp), 
+               TREE_TYPE (TREE_TYPE (string)))
          && TREE_CODE (string) == STRING_CST
          && TREE_CODE (index) == INTEGER_CST
          && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0


Comments?

Thanks,
Dirk


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]