public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/114682] New: const_array[i].b where i is PHI<0,1,2> is not folded
@ 2024-04-10 19:31 pinskia at gcc dot gnu.org
  2024-04-10 19:32 ` [Bug tree-optimization/114682] " pinskia at gcc dot gnu.org
  2024-04-11  6:38 ` rguenth at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-10 19:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114682

            Bug ID: 114682
           Summary: const_array[i].b where i is PHI<0,1,2> is not folded
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
```
#include <cstddef>

typedef struct cts_mode_name2id_st {
    unsigned int id;
    const char *name;
} CTS_MODE_NAME2ID;


/* The value assigned to 0 is the default */
#define CTS_CS1 0
#define CTS_CS2 1
#define CTS_CS3 2


/* OSSL_CIPHER_PARAM_CTS_MODE Values */
# define OSSL_CIPHER_CTS_MODE_CS1 "CS1"
# define OSSL_CIPHER_CTS_MODE_CS2 "CS2"
# define OSSL_CIPHER_CTS_MODE_CS3 "CS3"

static  CTS_MODE_NAME2ID cts_modes[] =
{
    { CTS_CS1, OSSL_CIPHER_CTS_MODE_CS1 },
    { CTS_CS2, OSSL_CIPHER_CTS_MODE_CS2 },
    { CTS_CS3, OSSL_CIPHER_CTS_MODE_CS3 },
};

#define OSSL_NELEM(a) (sizeof(a)/sizeof(a[0]))
int OPENSSL_strcasecmp(const char*, const char*);

int ossl_cipher_cbc_cts_mode_name2id(const char *name)
{
    size_t i;

    for (i = 0; i < OSSL_NELEM(cts_modes); ++i) {
        if (OPENSSL_strcasecmp(name, cts_modes[i].name) == 0)
            return (int)cts_modes[i].id;
    }
    return -1;
}

```

We end up with the following in the .optimized at -O2 or -O3:
```
  # i_3 = PHI <2(4), 0(2), 1(3)>
  _2 = cts_modes[i_3].id;
  _12 = (int) _2;
```

But cts_modes[i_3].id here is could be constant folded by the compiler into the
same constants as i really.

I suspect it is because we don't have a PRE post unrolling pass which could
handle that.

This shows up in openssl
https://github.com/openssl/openssl/blob/master/providers/implementations/ciphers/cipher_cts.c#L55

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug tree-optimization/114682] const_array[i].b where i is PHI<0,1,2> is not folded
  2024-04-10 19:31 [Bug tree-optimization/114682] New: const_array[i].b where i is PHI<0,1,2> is not folded pinskia at gcc dot gnu.org
@ 2024-04-10 19:32 ` pinskia at gcc dot gnu.org
  2024-04-11  6:38 ` rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-10 19:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114682

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note I was imspired to look into this because of
https://github.com/llvm/llvm-project/issues/88274 (note LLVM does a worse job
here and does not constant loads the string either).

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug tree-optimization/114682] const_array[i].b where i is PHI<0,1,2> is not folded
  2024-04-10 19:31 [Bug tree-optimization/114682] New: const_array[i].b where i is PHI<0,1,2> is not folded pinskia at gcc dot gnu.org
  2024-04-10 19:32 ` [Bug tree-optimization/114682] " pinskia at gcc dot gnu.org
@ 2024-04-11  6:38 ` rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-04-11  6:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114682

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2024-04-11

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
Note it isn't necessarily always an improvement.  It's also not really PRE
but FRE when you do PHI translation.  There's an argument that we should
try PHI translation in FRE to at least catch cases like this where
the replacement is simply

  # i_3 = PHI <2(4), 0(2), 1(3)>
  _12 = (int) i_3;

Note there's also that "frankenstein" phiprop pass which does remotely
similar things.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-04-11  6:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-10 19:31 [Bug tree-optimization/114682] New: const_array[i].b where i is PHI<0,1,2> is not folded pinskia at gcc dot gnu.org
2024-04-10 19:32 ` [Bug tree-optimization/114682] " pinskia at gcc dot gnu.org
2024-04-11  6:38 ` rguenth at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).