[LLVMdev] identifing mallocs with constant sizes

Ryan M. Lefever lefever at crhc.uiuc.edu
Fri May 11 12:00:17 PDT 2007


I am writing some code to identify malloc instructions with constant 
request sizes and to determine the request size if it is constant.  I 
have two questions.

1) If a malloc is for an array allocation and the array size is a 
ConstantExpr, how can I obtain the value of the ConstantExpr?

2) I am using the following logic to determine if the malloc is for a 
constant request size and to determine what is that size.  Does the 
logic make sense?

============================================================
if (malloc is for non-array allocation){
   -- the request size is constant and is equal to the size of
      allocated type
}
else{
   // this is an array allocation

   if(array size is a Constant){
     -- the request size is constant and equal to the size of the
        allocated type times the constant array size

     if(the array size is a ConstantInt){
       -- the array size can be obtained using getLimitedValue()
     }
     else if(the array size is a ConstantExpr){
       -- there should be some way to evaluate the constant
          expression???
     }
     else if(the array size is a ConstantAggregateZero){
       -- the malloc is for size 0
     }
     else if(the array size is a ConstantFP){
       -- I don't believe this case should happen but if it did,
          the array size could be obtained by calling getValue()
     }
     else if(the array size is a ConstantArray or ConstantPointerNull
             or ConstantStruct or ConstantVector or GlobalValue
             or UndefValue){
       -- this should not happen!
     }
   }
}
in all other cases, the malloc is for a non-constant request size
=============================================================

Regards,
Ryan



More information about the llvm-dev mailing list