This is part of the series started by D9375, but the first Clang patch. It teaches Clang to emit llvm.noalias calls and noalias metadata to represent local restrict-qualified pointers.
There are a few design considerations:
First, a local restrict-qualified pointer can have multiple values assigned to it, and they all need to fall into the same "noalias scope".
Second, the set of local scopes relevant to a given block cannot be determined without first visiting all restrict-qualified declarations and generating their corresponding scope MDNodes. Because the aliasing assumptions apply "retroactively" (to all accesses within the block, not just those sequenced after the declaration statement), we need to generate all MDNodes before applying the metadata. When we visit the declaration as the variable is being emitted we have an opportunity to record the address so that we can wrap all assigned values with an llvm.noalias call.
In this implementation, I'm generating the metadata nodes when we visit the decl when the local address is created (but before the initializer is evaluated), and adding the address to a map of address that, when stored to, are wrapped in llvm.noalias calls. To apply the metadata, we need to record all memory instructions in each block (and parent blocks, etc.). To avoid incurring this expense when no restrict-qualified decls are present, I have a pre-check that enables this recording of memory accesses (a small recursive visitor that looks for restrict-qualified decls).
After we're done emitting a block, we iterate over the memory-access instructions generated for it, and add metadata for all relevant noalias scopes of variables within that block.
This should really be a subroutine of EmitAutoVarAlloca; that will implicitly pick this logic up for a bunch of less standard places in IRGen that create local variables. Please make it a static function (if possible) called EmitNoAliasScope and call it immediately after EmitVarAnnotations.