https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118729
Bug ID: 118729 Summary: dead store elimination with function attributes for read / write and similar functions Product: gcc Version: 15.0 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: uecker at gcc dot gnu.org Target Milestone: --- In the following example, the initialization (also with -ftrivial-auto-var-init...) is not removed although it is dead. But the compiler can not know this. One solution would be to have some function attributes that tell the compiler how much is read/written to the buffer. But then this would also not sufficient as the second example shows, where everything is known. #define BUFSIZE 4096 void doit(int in_fd, int out_fd) { while (1) { char buf[BUFSIZE] = { 0 }; ssize_t len = read(in_fd, buf, BUFSIZE); if (len <= 0) break; write(out_fd, buf, len); } } --------------------------------------- #define BUFSIZE 4096 int g(void); void doit(char b2[BUFSIZE]) { while (1) { char buf[BUFSIZE] = { 0 }; int len = g(); for (int i = 0; i < len; i++) buf[i] = 0; if (len <= 0) break; for (int i = 0; i < len; i++) b2[i] = buf[i]; } }