LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 8927 - Invalid constant merging
Summary: Invalid constant merging
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Interprocedural Optimizations (show other bugs)
Version: trunk
Hardware: PC All
: P normal
Assignee: Unassigned LLVM Bugs
URL:
Keywords:
Depends on:
Blocks: 8907
  Show dependency tree
 
Reported: 2011-01-07 08:25 PST by Rafael Ávila de Espíndola
Modified: 2011-01-16 12:23 PST (History)
3 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Rafael Ávila de Espíndola 2011-01-07 08:25:20 PST
The C standard says that

Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space.

With -O0 and some cleanup passes clang compiles

struct foobar {
  int x;
};
static const struct foobar* foo() {
  static const struct foobar d = { 0 };
  return &d;
}
static const struct foobar* bar() {
  static const struct foobar d = { 0 };
  return &d;
}
int zed(const struct foobar *a, const struct foobar *b);
int main() {
  zed(foo(), bar());
}


to

%struct.foobar = type { i32 }
@bar.d = internal constant %struct.foobar zeroinitializer, align 4
@foo.d = internal constant %struct.foobar zeroinitializer, align 4
define i32 @main() nounwind ssp {
entry:
  %call2 = tail call i32 @zed(%struct.foobar* @foo.d, %struct.foobar* @bar.d) nounwind
  ret i32 0
}
declare i32 @zed(%struct.foobar*, %struct.foobar*)


but constmerge (which is included in -O2) transforms it to

%struct.foobar = type { i32 }
@bar.d = internal constant %struct.foobar zeroinitializer, align 4
define i32 @main() nounwind ssp {
entry:
  %call2 = tail call i32 @zed(%struct.foobar* @bar.d, %struct.foobar* @bar.d) nounwind
  ret i32 0
}
declare i32 @zed(%struct.foobar*, %struct.foobar*)

Since the function zed in unknown, it might compare the pointers and now it will get a different result.
Comment 1 Chris Lattner 2011-01-07 12:55:51 PST
Yes, this is a known (to me) problem, aka rdar://4624751

From that bug:

This can be fixed by adding a new 'needs unique address' attribute of some sort to globals.  The front-end could tag things that need a distinct address, the constant merging pass would respect that, and the globalopt pass would remove it if it can show the global is not address taken.

The issue is that we need to be able to distinguish named globals and temporaries:
  const char s1[] = "hello";
  const char *s2 = "hello";

The ".str" global produced by the second example needs to be mergable.  If a global isn't mergable, it can't be dropped into a literal or cstring section by the code generator.
Comment 2 Chris Lattner 2011-01-07 12:56:36 PST
Also, the constant merge pass would not merge them.  It's worth pointing out that GCC 4.2 also has this bug, but isn't as aggressive about merging constants as llvm, so it doesn't trip over it as often.  I imagine (but don't know) that mainline gcc has the same problem.