This is an archive of the discontinued LLVM Phabricator instance.

Remove function bitcast constants that are not linked in the IR
AbandonedPublic

Authored by mikerice on Jan 7 2022, 10:21 AM.

Details

Reviewers
rjmccall
Summary

There are some cases (in C involving unprototyped functions) where bitcasts
are created and not actually used in the IR. This leaves a use for the
function that isn't in the IR and breaks Function::hasAddressTaken().

The added tests show the cases broken by this.

A simple case like:

int foo() { return 1; }
int main() { return foo(); }

The bitcast is created but not used when simplifyVariadicCallee succeeds.

Similarly when an unprototyped declaration occurs followed by a definition:

void b();
void b() { }

The bitcast constant is added to the GlobalValReplacement list, but may
never actually be needed to replace anything.

For lack of a better idea, this change just keeps track of the possibly unused constants and removes them if not
used at the end of the module.

Diff Detail

Event Timeline

mikerice created this revision.Jan 7 2022, 10:21 AM
mikerice requested review of this revision.Jan 7 2022, 10:21 AM

LLVM clients that want to do use-analyses of globals are supposed to call removeDeadConstantUsers() first. We could do that for every global coming out of IRGen, but I'm not sure that's a good use of compile time. Not creating unnecessary casts seems like at most a best-effort thing.

mikerice abandoned this revision.Jan 7 2022, 7:12 PM

Thanks for looking at this. We've found another case that event this patch doesn't handle so we'll try to deal with this some other way.