llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.35k stars 12.14k forks source link

New Pass Manager unable to statically determine the return value of some functions #41678

Open PiJoules opened 5 years ago

PiJoules commented 5 years ago
Bugzilla Link 42333
Version trunk
OS Linux
CC @ekatz,@zygoloid

Extended Description

Found when enabling the new pass manager by default and running test: clang/test/CodeGenCXX/conditional-temporaries.cpp

The test fails under the new PM because it is unable to optimize some return values statically as the legacy PM does. For example:

static int ctorcalls;
static int dtorcalls;

struct A {
  A() : i(0) { ctorcalls++; }
  ~A() { dtorcalls++; }
  int i;

  friend const A& operator<<(const A& a, int n) {
    return a;
  }
};

void g(int) { }
void g(const A&) { }

void f1(bool b) {
  g(b ? A().i : 0);
  g(b || A().i);
  g(b && A().i);
  g(b ? A() << 1 : A() << 2);
}

struct Checker {
  Checker() {
    f1(true);
    f1(false);
  }
};

Checker c;

}

// CHECK-OPT-LABEL: define i32 @&#8203;_Z12getCtorCallsv()
int getCtorCalls() {
  // CHECK-OPT: ret i32 5
  return ctorcalls;
}

Invoking %clang_cc1 -emit-llvm %s -o - -triple=x86_64-apple-darwin9 -O2 is meant to optimize it as a return of 5, but the new PM instead generates:

[[RET:%.*]] = load i32, i32* addrspacecast (i32 addrspace(1)* @&#8203;_ZN12_GLOBAL__N_19ctorcallsE to i32*), align 4
ret i32 [[RET]]

We can see something similar in clang/test/CodeGenCXX/member-function-pointer-calls.cpp where calls to member functions are not optimized out under new PM.

llvmbot commented 3 years ago

mentioned in issue llvm/llvm-project#9522

ekatz commented 4 years ago

I tried to reproduce it by running clang with "-fexperimental-new-pass-manager -O2", and it does produce the desired result. Am I doing something wrong?