[LLVMdev] Why int variable get promoted to i64

陳韋任 chenwj at iis.sinica.edu.tw
Fri Aug 19 06:22:51 PDT 2011


> 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]

  Indeed, the index variable "int i" won't be promoted if it 
is not used in the loop body.

------------- test case --------------
int test(int x[], int y[], int n) {
  int i = 0;
  int sum = 0;
  for( ; i < n; i++) {
    sum += x[2] + y[2];
  }
  return sum;
}
--------------------------------------

  But I don't see why the index variable need to be able to index
64-bits of space. Is compiler required to treat index variable
special?

  Thanks!

Regards,
chenwj

-- 
Wei-Ren Chen (陳韋任)
Computer Systems Lab, Institute of Information Science,
Academia Sinica, Taiwan (R.O.C.)
Tel:886-2-2788-3799 #1667




More information about the llvm-dev mailing list