[LLVMdev] identifing mallocs with constant sizes

John Criswell criswell at cs.uiuc.edu
Fri May 11 12:24:20 PDT 2007


Ryan M. Lefever wrote:
> 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?
>   
The only way I know of is to determine what type of constant expression
it is and then evaluate it yourself.  So, you'd have something like this:

if (is a negation constantexpr)
    figure out the negated value of the operand
if (is a cast constantexpr)
    figure out the result of the operand casted to the new type
...
etc, etc.

If anyone knows of a more efficient way, I'd be curious, too.  I've had
to write code that determines the value of a constant expression, too.
> 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
>   
At a glance, this looks good, except that a ConstantInt is also a
Constant, so I don't see the difference between the two cases.

-- John T.

> =============================================================
>
> Regards,
> Ryan
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>   




More information about the llvm-dev mailing list