<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">The code in clang that performs this promotion for array subscript expressions is in CGExpr.cpp:<div><br></div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Menlo; ">LValue CodeGenFunction::EmitArraySubscriptExpr(<span style="color: #b930a1">const</span> ArraySubscriptExpr *E) {</div></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Menlo; ">   </div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Menlo; ">  ...snip...</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Menlo; "><br></div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Menlo; color: rgb(0, 131, 17); "><span style="color: #000000">  </span>// Extend or truncate the index type to 32 or 64-bits.</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Menlo; ">  <span style="color: #b930a1">if</span> (Idx->getType() != IntPtrTy)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Menlo; ">    Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, <span style="color: #ce2f24">"idxprom"</span>);</div><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px"> </p><div>Cheers,</div><div><br></div><div>Joe</div><div><br></div><div><div>On Aug 19, 2011, at 8:16 AM, Joe Abbey wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">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. <div><br></div><div>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:<div><br></div><div><span class="Apple-style-span" style="font-family: Times; "><pre>for.body:                                         ; preds = %for.cond
  %tmp2 = <span class="llvm_keyword" style="font-weight: bold; color: blue; ">load</span> <span class="llvm_type" style="font-style: oblique; color: green; ">i32</span>* %i                            ; <<span class="llvm_type" style="font-style: oblique; color: green; ">i32</span>> [#uses=1]
  %tmp3 = <span class="llvm_keyword" style="font-weight: bold; color: blue; ">load</span> <span class="llvm_type" style="font-style: oblique; color: green; ">i32</span>** %x.addr                      ; <<span class="llvm_type" style="font-style: oblique; color: green; ">i32</span>*> [#uses=1]
  %idxprom = <span class="llvm_keyword" style="font-weight: bold; color: blue; ">sext</span> <span class="llvm_type" style="font-style: oblique; color: green; ">i32</span> %tmp2 <span class="llvm_keyword" style="font-weight: bold; color: blue; ">to</span> <span class="llvm_type" style="font-style: oblique; color: green; ">i64</span>                ; <<span class="llvm_type" style="font-style: oblique; color: green; ">i64</span>> [#uses=1]
  %arrayidx = <span class="llvm_keyword" style="font-weight: bold; color: blue; ">getelementptr</span> inbounds <span class="llvm_type" style="font-style: oblique; color: green; ">i32</span>* %tmp3, <span class="llvm_type" style="font-style: oblique; color: green; ">i64</span> %idxprom ; <<span class="llvm_type" style="font-style: oblique; color: green; ">i32</span>*> [#uses=1]</pre></span><div><div><br></div><div>Cheers,</div><div><br></div><div>Joe</div><div><br><div><div>On Aug 19, 2011, at 5:32 AM, 陳韋任 wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; ">int test(int x[], int y[], int n) {<br> int i = 0;<br> int sum = 0;<br> for ( ; i < n; i++) {<br>   sum += x[i] * y[i];<br> }<br> return sum;<br>}</span></blockquote></div><br></div></div></div></div></div></blockquote></div><br></div></body></html>