Skip to content

failing to optimize out memory allocation that is only used to feed into @bcmp calls #155927

@zygoloid

Description

@zygoloid

Testcase:

#include <vector>
bool f(const std::vector<int>& v) {
  return v == std::vector<int>{42};
}

We should, but don't, optimize out the allocation / deallocation here. We get IR that resembles this after optimization:

  %call = tail call noalias noundef nonnull dereferenceable(4) ptr @_Znwm(i64 noundef 4) #5
  store i32 42, ptr %call, align 4
  ; ...
  %bcmp = tail call i32 @bcmp(ptr noundef nonnull dereferenceable(4) %1, ptr noundef nonnull dereferenceable(4) %call, i64 
  ; ...
  tail call void @_ZdlPvm(ptr noundef nonnull %call, i64 noundef 4) #6

... with no other non-assumption uses of %call. It seems to me that we should be able to convert the @bcmp call to a load and an icmp, or perhaps to promote the constant argument to a global, and either way we should then be able to remove the heap allocation.

Similar testcase with the C++ standard library usage removed:

#include <stdlib.h>
#include <strings.h>
int f(int *p) {
  int *q = (int*)malloc(4);
  *q = 42;
  int r = bcmp(p, q, 4);
  free(q);
  return r == 0;
}

Again, we are unable to remove the heap allocation here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions