llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[globalopt] doesn't eliminate complex dead load and store case

Open nlewycky opened this issue 17 years ago • 6 comments

Bugzilla Link 4261
Version trunk
OS Linux
CC @lattner,@efriedma-quic,@ekatz

Extended Description

With 'opt -std-compile-opts' LLVM doesn't simplify this:

@​_ZGVZ3foovE1x = internal global i64 0, align 8

define void @​_Z3foov() nounwind {
entry:
        %0 = load i8* bitcast (i64* @​_ZGVZ3foovE1x to i8*), align 8
        %1 = icmp eq i8 %0, 0
        br i1 %1, label %bb1, label %return

bb1:            ; preds = %entry
        store i8 1, i8* bitcast (i64* @​_ZGVZ3foovE1x to i8*), align 8
        ret void

return:         ; preds = %entry
        ret void
}

The thing is that because @​_ZGVZ3foovE1x is internal, we know that those are the only two uses of the global. There are two possible values (without the bitcasts it converts the global into i1) and neither case much matters. The whole thing is dead.

nlewycky avatar May 25 '09 06:05 nlewycky

So the status is as follows:

  1. llvm-g++ is discontinued.
  2. Clang converts _ZGVZ3foovE1x to i1.
  3. Clang still does NOT eliminate _ZGVZ3foovE1x.
  4. Clang produces the same assembly as GCC.

ekatz avatar Nov 18 '19 20:11 ekatz

See also, rdar://4109906 a really old enhancement request for this.

lattner avatar May 27 '09 07:05 lattner

One other random point: the fact that llvm-g++ lowers these guards to i64 is pretty gross. It would be nice to see if we can fix the g++ frontend to emit these as {i8, [7 x i8]} or something, to eliminate the horrible bitcast.

lattner avatar May 26 '09 06:05 lattner

Yes, globalopt should be the one that zaps this sort of thing. However, it intentionally doesn't "booleanize" globals that are pointers. Doing so requires each use to turn into something like:

ptr = global ? &G1 : &G2;

This turns into 3 pic references, which is much worse than:

ptr = global;

-Chris

lattner avatar May 25 '09 22:05 lattner

I think GlobalOpt should be able to do this pretty easily once the global is converted to a boolean: just check that there's only one load used by a single branch, and the two possible destinations are blocks which are identical except for a store to the global.

As for getting it down to a boolean, there's a pretty simple method: check whether the only user of the global is a bitcast to a pointer to a different type, and all the users of the bitcast are loads and stores. If so, the global can be transformed to the type of the bitcast.

efriedma-quic avatar May 25 '09 07:05 efriedma-quic

Current status, re Claude:

The pattern is a C++ magic-static guard variable left over after the real initialization work has already been proven dead/trivial: load the guard, branch on whether it's zero, and if so just set the guard and return — no other work happens. Since the guard is internal (only two uses, both in this function), the whole thing is provably dead: whichever branch you take, the function just returns.

The thread's most useful entry is efriedma-quic's 2009 two-part fix proposal, and ekatz's 2019 status check confirmed part 1 landed (Clang's frontend now emits these guards as i1-ish/i8, not the ugly i64-with-bitcast llvm-g++ produced) but part 2 (actually eliminating the dead check-and-set) never did.

I fed the exact IR from the bug report (updated only for opaque pointers, no logic change — which incidentally means Lattner's separate complaint about the "gross" bitcast i64* to i8* is now moot for unrelated reasons: opaque pointers eliminated that class of bitcast project-wide) through both -passes=globalopt alone and the full -passes='default<O2>' pipeline:

define void @_Z3foov() local_unnamed_addr #0 {
entry:
  %0 = load i8, ptr @_ZGVZ3foovE1x, align 8
  %1 = icmp eq i8 %0, 0
  br i1 %1, label %bb1, label %common.ret
common.ret:
  ret void
bb1:
  store i8 1, ptr @_ZGVZ3foovE1x, align 8
  br label %common.ret
}

Untouched — the load, compare, branch, and store all survive full -O2 today, exactly as ekatz reported in 2019. SimplifyCFG merged the two return blocks into common.ret but didn't go the extra step of noticing the store is provably dead (nothing ever reads the guard again) and the branch condition therefore doesn't matter, so the whole entry/bb1 structure could collapse to just ret void.

For context, I also checked whether modern Clang even still hits this in practice: for a genuinely-trivial function-local static (static Trivial x; with a constant-evaluable initializer), Clang's frontend is smart enough today to skip emitting a guard at all — foo() compiles straight to ret void, no IR-level cleanup needed. So this specific missed optimization only bites when a guard is genuinely required (non-constant initializer) but the compiler can still prove, after other optimizations already stripped the actual init code, that the residual guard machinery is dead. Narrower than in 2009, but real and still unfixed — safe to leave open, and efriedma's original design sketch (recognize "single load feeding one branch whose two destinations are identical except for a store to the global") still reads like a reasonable starting point for anyone who wants to pick it up.

isanbard avatar Sep 02 '26 09:09 isanbard