> On Nov 16, 2017, at 5:55 PM, Martin Sebor <mse...@gmail.com> wrote: >> >> A. for strncmp (s1, s2, n) >> if one of "s1" or "s2" is a constant string, "n" is a constant, and >> larger than the length of the constant string: >> change strncmp (s1, s2, n) to strcmp (s1, s2); > > Here and I think in some (all?) the other cases below, N doesn't > strictly have to be constant but can be in a range whose lower > bound is greater than the string length. Yes, I agree. If the value range info is available, we can relax N to be an expression whose value can be determined to be larger than the length of the constant string.
Do you know when the value range info is available in GCC, and the interface to use it? > > FWIW, I also recently noticed bug 82950 in a related area. For the opportunities mentioned in PR82950, I also realized during my implementation of B in tree-ssa-strlen.c. the major thing missing there is the “content” of the string is NOT recorded in the strinfo, only the “length” of the string is recorded. So, the information is Not completely available now to support this opportunity. However, for PR 83026, the information should be enough in tree-ssa-strlen.c, I can add the support this in tree-ssa-strlen.c by using the length info of the string recorded in strinfo. > >> >> B. for strncmp (s1, s2, n) (!)= 0 or strcmp (s1, s2) (!)= 0 >> if the result is ONLY used to do a simple equality test against zero, >> one of "s1" or "s2" is a small constant string, n is a constant, and the >> other non-constant string is guaranteed to not read beyond the end of the >> string: >> change strncmp (s1, s2, n) or strcmp (s1, s2) to corresponding memcmp >> (s1, s2, n); >> > > It's probably not important but I'm not sure I understand what you > mean by > > "the other non-constant string is guaranteed to not read beyond > the end of the string.” We need to be sure that we can read valid memory from the non-constant string after the null-character when change to corresponding memcmp. > >> (NOTE, currently, memcmp(s1, s2, N) (!)=0 has been optimized to a >> simple sequence to access all bytes and accumulate the overall result in GCC >> by >> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52171 >> ) >> as a result, such str(n)cmp call would like to be replaced by simple >> sequences to access all types and accumulate the overall results. > > Thinking about this case made me realize there's another opportunity > here that could be exploited in tree-ssa-strlen even for non-constant > strings by relying on its knowledge of string lengths. I opened bug > 83026 with the details. Yes, I agree. and will add this in my implementation in Part B. should be very straightforward to implement. > >> >> C. for strcmp (s1, s2), strncmp (s1, s2, n), and memcmp (s1, s2, n) >> if the result is NOT used to do simple equality test against zero, one >> of "s1" or "s2" is a small constant string, n is a constant, and the Min >> value of the length of the constant string and "n" is smaller than a >> predefined threshold T, >> inline the call by a byte-to-byte comparision sequence to avoid calling >> overhead. > > The str{,n}cmp and memcmp cases must be treated differently because > the former compares up to N bytes while the latter exactly N bytes. > With that, I'm wondering if the precondition "one of "s1" or "s2" > is a small constant string" is necessary for memcmp. I.e., why > not inline it regardless of whether s1 or s2 are constant? I agree. memcmp can ONLY check on “n”, if its small enough, should be inlined. in addition to this, for strncmp, if both s1 and s2 are NOT constant, but if “n” is small enough, we can inline it too, right? > >> >> A would like to be put in gimple fold phase (in routine >> "gimple_fold_builtin_string_compare" of gimple-fold.c) > > Except for constant strings gimple-fold doesn't know about string > lengths so it will only be able to do so much. I'm wondering what > the benefits are of doing the transformation there instead of just > in tree-ssa-strlen (along with B). As Jeff mentioned in another email, “Most passes call into the folder when they make noteable changes to a statement. So if a later pass exposes one argument as a string constant your code for "A" should get a chance to fold the call.” We can see whether this is enough or not, If not enough, adding A to B is very easy thing to do. thanks a lot for your comments and suggestions. Qing