https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66868
Bug ID: 66868 Summary: [5/6 Regression] wrong code generated with -O3 (dead code removal?) Product: gcc Version: 5.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: doko at gcc dot gnu.org Target Milestone: --- Created attachment 35975 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=35975&action=edit preprocessed source [forwarded from https://bugs.launchpad.net/ubuntu/+source/gcc-5/+bug/1473674] seen in the apt package manager, when built with -O3. Works with -O2 or -O3 -fno-inline-functions -finline-small-functions, on at least x86_64 and powerpc64le. The test CDROMTest.FindPackages from the apt testsuite then fails. built with g++ -c -g -O3 -fPIE -fstack-protector-strong -fPIC upstream writes: There is a Cdrom wrapper: """ class Cdrom : public pkgCdrom { public: bool FindPackages(std::string const &CD, std::vector<std::string> &List, std::vector<std::string> &SList, std::vector<std::string> &SigList, std::vector<std::string> &TransList, std::string &InfoDir) { std::string const startdir = SafeGetCWD(); EXPECT_FALSE(startdir.empty()); bool const result = pkgCdrom::FindPackages(CD, List, SList, SigList, TransList, InfoDir, NULL, 0); ... } """ and a unittest that calls it: """ TEST(CDROMTest,FindPackages) { Cdrom cd; std::string InfoDir; EXPECT_TRUE(cd.FindPackages(path, Packages, Sources, Signatur, Translation, InfoDir)); ... EXPECT_EQ(path + "/.disk/", InfoDir); } """ The actual code for this is apt-pkg/cdrom.cc: """ bool pkgCdrom::FindPackages(string CD, vector<string> &List, vector<string> &SList, vector<string> &SigList, vector<string> &TransList, string &InfoDir, pkgCdromStatus *log, unsigned int Depth) { ... if (DirectoryExists(".disk") == true) { if (InfoDir.empty() == true) InfoDir = CD + ".disk/"; } ... """ So I suspect that the optimizer gets confused that InfoDir is a reference or it gets confused because InfoDir is not used in FindPackages anymore and it assumes its dead code. I tried to create a simplified testcase but failed so far. Whats interessting is that if I add a std::cerr << "debug" line into cdrom.cc lines (or even a "CD = CD"): """ if (DirectoryExists(".disk") == true) { if (InfoDir.empty() == true) { std::cerr << "debug" << std::endl; InfoDir = CD + ".disk/"; } } """ it works (which indicates dead-code elimination to me).