Missed strlen optimizations

Hello,

Code: https://godbolt.org/g/EG4Wi6

unsigned fff3(void) {
char buf[10] = “”;
return strlen(buf);
}

Since we are memset-ing before strlen call, we could replace strlen with just 0.

Has LLVM any API to get “last instruction before strlen” which modifies strlen argument “buf”? So we can check “yes, it is memset there, replace strlen with zero” ?

Thanks

Is:

Use *last = nullptr;
for (Use &U : Src->uses())
last = &U;
last->getUser()->dump();

Or any better solution?

Maybe nicer…
auto i = Src->uses().begin();
std::advance(i, Src->getNumUses() - 1);
i->getUser()->dump();

This will not work if it’s memory.

Yu’d need to use MemorySSA or MemoryDependenceAnalysis to get the last instruction to modify a block of memory.