On Dec 14, 2009, at 2:21 PM, Jeffrey Yasskin wrote:>> >> @"compile-test::*testObj*" = external constant i8* ; >> <i8**> [#uses=1] >> >> define void @"compile-test::__toplevel-main"() { >> entry: >> store i8* null, i8** @"compile-test::*testObj*" > > I'm surprised this store got optimized out, even though LLVM can > optimize away the subsequent load. Writing to an external global > variable is a visible side-effect, and unless there's other undefined > behavior, LLVM shouldn't remove it.Sure it can, llvm can delete any non-volatile redundant load, or any non-volatile redundant store. It doesn't matter whether it is to a global or not, LLVM (as with many compilers) memory models are for single threaded programs. We do try to conform to the C++'0x memory model by not introducing memory accesses where they did not exist before, but deleting non-volatile accesses is always fine. -Chris
On Dec 14, 2009, at 3:50 PMPST, Chris Lattner wrote:> On Dec 14, 2009, at 2:21 PM, Jeffrey Yasskin wrote: >>> >>> @"compile-test::*testObj*" = external constant i8* ; >>> <i8**> [#uses=1] >>> >>> define void @"compile-test::__toplevel-main"() { >>> entry: >>> store i8* null, i8** @"compile-test::*testObj*" >> >> I'm surprised this store got optimized out, even though LLVM can >> optimize away the subsequent load. Writing to an external global >> variable is a visible side-effect, and unless there's other undefined >> behavior, LLVM shouldn't remove it. > > Sure it can, llvm can delete any non-volatile redundant load, or any > non-volatile redundant store. It doesn't matter whether it is to a > global or not, LLVM (as with many compilers) memory models are for > single threaded programs. We do try to conform to the C++'0x memory > model by not introducing memory accesses where they did not exist > before, but deleting non-volatile accesses is always fine.That's true, but it's often hard to figure out a store to a global can be deleted, as a use may be arbitrarily far away, in a different file for example. In this case the global is declared constant, so storing to it is invalid. That is probably why it got removed.
On Dec 14, 2009, at 4:09 PM, Dale Johannesen wrote:>> single threaded programs. We do try to conform to the C++'0x memory >> model by not introducing memory accesses where they did not exist >> before, but deleting non-volatile accesses is always fine. > > That's true, but it's often hard to figure out a store to a global > can be deleted, as a use may be arbitrarily far away, in a different > file for example.Sure, the compiler can only remove it if it can see the redefinition. In practice, stores are only deleted when they are undefined behavior (e.g. the target is read only) or if it sees a subsequent store to the same location (with no uses between them). -Chris