[cfe-dev] Block Local Variables and Alloca's

Prakash Prabhu prakash.prabhu at gmail.com
Sun Mar 28 20:41:25 PDT 2010


Hi,

I have a question with respect to relating the alloca's of local variables
in the LLVM bit-
code to their actual declarations in the source code. Consider for instance
the following
declaration of a variable called 'digest' within a loop (iteration local
variable):

int main()
{
  ...
  for (i=0; i < n; i++)
  {
    unsigned char digest[16];
    ...
  }
}

The LLVM bit-code looks as follows:


define i32 @main(i32 %argc, i8** %argv) noreturn nounwind {
entry:
  ...
  %digest = alloca [16 x i8], align 1             ; <[16 x i8]*> [#uses=2]
  ...

for.cond:
  ...
}

What I would like to is to transform the code above, for certain selective
loops,
to pre-allocate n copies of digest,  using alloca's or malloc/free before
the loop.

I would like modify clang to detect and annotate variables like digest as
being local
to a loop/compound statement, which can then be used by some pass in LLVM's
opt to
find the corresponding alloca and apply the transformation above. Right now,
one crude
way of doing this would be to add calls to annotation functions at places
where the
variables are declared, by detecting VarDecl instances in clang. The later
pass in opt can then
parse the annotation function and figure out the nearest enclosing loop to
do the pre-allocation
 transformation. Is there is better/easier way to do this ?

thanks,
Prakash
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20100328/a63b8c3c/attachment.html>


More information about the cfe-dev mailing list