[PATCH 1/3] Fold __builtin_str{n}{case}cmp functions (version 2)

Richard Biener richard.guenther@gmail.com
Tue Oct 11 10:27:00 GMT 2016


On Tue, Oct 11, 2016 at 11:26 AM, Martin Liška <mliska@suse.cz> wrote:
> On 10/07/2016 12:50 PM, Richard Biener wrote:
>> On Fri, Oct 7, 2016 at 10:39 AM, Martin Liška <mliska@suse.cz> wrote:
>>> I'm resending the patch, where I implemented all builtins mentions in subject
>>> in gimp-fold.c.
>>>
>>> Patch can bootstrap on ppc64le-redhat-linux and survives regression tests.
>>>
>>> Ready to be installed?
>>
>> +       case BUILT_IN_STRNCASECMP:
>> +         {
>> +           r = strncmp (p1, p2, length);
>> +           if (r == 0)
>> +             known_result = true;
>>
>> length might be -1 here -- I think you need to guard against that (but you can
>> handle BUILT_IN_STRCASECMP which you miss?).  Likewise for the strncmp case.
>
> Fixed, I've added comment to STRCASECMP case.
>
>>
>> Also do we know whether the c_getstr () result is '\0'-terminated?  AFAICS these
>> constant foldings were not implemented in builtins.c, I see a STRCMP one in
>> fold-const-call.c though.  I believe the STRING_CST string is not guaranteed to
>> be '\0' terminated (STRING_CST comes with explicit length).
>
> You are absolutely right that we do not have always '\0'-terminated string constants.
> Thus I'll send a patch that would return a string from c_getstr just in case
> string[string_length] == 0 (separate email with patch will be sent).

Maybe add a second output to c_getstr to pass down the string length
in case it is known?

In this case you could use strN* () variants for constant folding.
"not found" would need
to be folded conditional on null termination to avoid folding
undefined behavior.

Richard.

>>
>> +      tree temp = fold_build2_loc (loc, MEM_REF, cst_uchar_node, str1,
>> +                                  off0);
>> +      temp = gimple_build (&stmts, loc, NOP_EXPR, cst_uchar_node, temp);
>>
>> please don't use gimple_build here, there is nothing to simplify for it.  Using
>> a NOP_EXPR is also confusing (to match the API...).  Just do
>> gimple_build_assign (make_ssa_name (...), ..) like other folders do.
>>
>> +      replace_call_with_value (gsi, fold_convert_loc (loc, type, temp));
>>
>> and you'd want to replace the call with the MEM_REF stmt using
>> gsi_replace_with_seq_vops as you fail to set virtual operands properly
>> above (thus you'll get ICEs when this only matches during GIMPLE opts).
>>
>> +  location_t loc = gimple_location (stmt);
>> +  tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
>> +  tree cst_uchar_ptr_node
>> +    = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
>> +  tree off0 = build_int_cst (cst_uchar_ptr_node, 0);
>>
>> it would be nice to not do this tree building if nothign is folded.
>>
>> +    case BUILT_IN_STRCMP:
>> +      return gimple_fold_builtin_string_compare (gsi);
>> +    case BUILT_IN_STRCASECMP:
>> +      return gimple_fold_builtin_string_compare (gsi);
>> +    case BUILT_IN_STRNCMP:
>> +      return gimple_fold_builtin_string_compare (gsi);
>> +    case BUILT_IN_STRNCASECMP:
>> +      return gimple_fold_builtin_string_compare (gsi);
>>
>> please do
>>
>> +    case BUILT_IN_STRCMP:
>> +    case BUILT_IN_STRCASECMP:
>> ...
>> +      return gimple_fold_builtin_string_compare (gsi);
>>
>> Thanks,
>> Richard.
>
> Sure, all notes will be fixed in an email which reply to this one.
>
> Martin
>
>>
>>> Martin
>



More information about the Gcc-patches mailing list