[LLVMdev] Why int variable get promoted to i64

Joe Abbey joe.abbey at gmail.com
Fri Aug 19 06:16:07 PDT 2011


The code in clang that performs this promotion for array subscript expressions is in CGExpr.cpp:

LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
   
  ...snip...

  // Extend or truncate the index type to 32 or 64-bits.
  if (Idx->getType() != IntPtrTy)
    Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, "idxprom");
 
Cheers,

Joe

On Aug 19, 2011, at 8:16 AM, Joe Abbey wrote:

> Because you are compiling for a 64-bit system which uses LP64.  As such pointers are 64-bit in length... while integers remain defined as 32-bits. 
> 
> So why 64-bits for i?  The compiler is treating index variables special.  Index variables need to be able to index 64-bits of space and in fact you'll see the promotion here:
> 
> for.body:                                         ; preds = %for.cond
>   %tmp2 = load i32* %i                            ; <i32> [#uses=1]
>   %tmp3 = load i32** %x.addr                      ; <i32*> [#uses=1]
>   %idxprom = sext i32 %tmp2 to i64                ; <i64> [#uses=1]
>   %arrayidx = getelementptr inbounds i32* %tmp3, i64 %idxprom ; <i32*> [#uses=1]
> 
> Cheers,
> 
> Joe
> 
> On Aug 19, 2011, at 5:32 AM, 陳韋任 wrote:
> 
>> int test(int x[], int y[], int n) {
>>  int i = 0;
>>  int sum = 0;
>>  for ( ; i < n; i++) {
>>    sum += x[i] * y[i];
>>  }
>>  return sum;
>> }
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110819/feced595/attachment.html>


More information about the llvm-dev mailing list