I'm very confused by the LLVM AliasAnalysis implementation. Say I have this program:
int* key = malloc(4);
*key = 10;
*key = 11;
It gets transformed to IR code like this:
%3 = call noalias i8* @malloc(i64 4) #2
%4 = bitcast i8* %3 to i32*
store i32* %4, i32** %2, align 8
%5 = load i32*, i32** %2, align 8
store i32 10, i32* %5, align 4
%6 = load i32*, i32** %2, align 8
store i32 11, i32* %6, align 4
Than I ask LLVM to print out the alias relationship between %5
and %6
, by using the function static_cast<uint16_t>(AA_->getModRefInfo(FirstStore, MemoryLocation(SecondStorePointer)))
. It then shows that they may alias (as ModRefInfo::Mod
) with each other. Why is LLVM unable to detect that they must alias each other? Is there any way I can fix it?
mayalias
sufficient in code generation to elide the*key = 10;
? That is, during a later stage,mayalias
gets "promoted" based on a more "global" analysis. What happens if you do:volatile int *key = malloc(sizeof(*key));
? It probably should not alias. ORint foo; int *key = &foo;
ORint foo; int * const key = &foo;
ORint * const key = malloc(sizeof(*key));
??? – Edemakey
might point to itself, in which case%5
and%6
really would be different. That should be excluded both by the strict aliasing rule and by the fact thatmalloc
cannot return a pointer to any existing object, but maybe for some reason it isn't being picked up? – Languishload
. – Tetroxidevolatile
. – Essive