Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid constant merging #9299

Closed
llvmbot opened this issue Jan 7, 2011 · 2 comments
Closed

Invalid constant merging #9299

llvmbot opened this issue Jan 7, 2011 · 2 comments
Labels
bugzilla Issues migrated from bugzilla ipo Interprocedural optimizations

Comments

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 7, 2011

Bugzilla Link 8927
Resolution FIXED
Resolved on Jan 16, 2011 12:23
Version trunk
OS All
Blocks #9279
Reporter LLVM Bugzilla Contributor
CC @lattner

Extended Description

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.

@lattner
Copy link
Collaborator

lattner commented Jan 7, 2011

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.

@lattner
Copy link
Collaborator

lattner commented Jan 7, 2011

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.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 3, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla ipo Interprocedural optimizations
Projects
None yet
Development

No branches or pull requests

2 participants