Hi, all I found in some cases the int variable get promoted to i64, although I think it should i32. I use the online demo (http://llvm.org/demo). And below is the test case. ------------- test case ------------- 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; } ------------------------------------- No matter the optimization level is standard or none, int varible always get promoted to i64. After some discussions on IRC, Duncan found GCC also does such promotion. So it's not LLVM-only optimization. Anyone knows why? 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
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/c64b2680/attachment.html>
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>
> 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
Maybe Matching Threads
- [LLVMdev] Why int variable get promoted to i64
- [LLVMdev] Why int variable get promoted to i64
- [LLVMdev] [PATCH] parallel loop awareness to the LoopVectorizer
- [LLVMdev] [PATCH] parallel loop awareness to the LoopVectorizer
- [ScalarEvolution][SCEV] no-wrap flags dependent on order of getSCEV() calls