You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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
transferred this issue from llvm/llvm-bugzilla-archive
Dec 3, 2021
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.
The text was updated successfully, but these errors were encountered: