<div dir="ltr">Hi everyone, <div><br></div><div>I am learning llvm IR from official documents, especially the GEP instruction. </div><div><br></div><div>Now I have a problem about the GEP instructions generated by llvm.</div><div><br></div><div>As the example in
<a href="https://llvm.org/docs/LangRef.html#getelementptr-instruction">https://llvm.org/docs/LangRef.html#getelementptr-instruction</a> say, for the function "<b>foo" </b>in the following code example:</div><div><pre style="overflow:auto hidden;font-family:Consolas,"Deja Vu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:0.95em;line-height:16.625px;padding:0.5em;border:1px solid rgb(204,204,204);background-color:rgb(248,248,248);color:rgb(0,0,0)"><span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">struct</span> <span class="gmail-nc" style="color:rgb(14,132,181);font-weight:bold">RT</span> <span class="gmail-p">{</span>
<span class="gmail-kt" style="color:rgb(144,32,0)">char</span> <span class="gmail-n">A</span><span class="gmail-p">;</span>
<span class="gmail-kt" style="color:rgb(144,32,0)">int</span> <span class="gmail-n">B</span><span class="gmail-p">[</span><span class="gmail-mi" style="color:rgb(64,160,112)">10</span><span class="gmail-p">][</span><span class="gmail-mi" style="color:rgb(64,160,112)">20</span><span class="gmail-p">];</span>
<span class="gmail-kt" style="color:rgb(144,32,0)">char</span> <span class="gmail-n">C</span><span class="gmail-p">;</span>
<span class="gmail-p">};</span>
<span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">struct</span> <span class="gmail-nc" style="color:rgb(14,132,181);font-weight:bold">ST</span> <span class="gmail-p">{</span>
<span class="gmail-kt" style="color:rgb(144,32,0)">int</span> <span class="gmail-n">X</span><span class="gmail-p">;</span>
<span class="gmail-kt" style="color:rgb(144,32,0)">double</span> <span class="gmail-n">Y</span><span class="gmail-p">;</span>
<span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">struct</span> <span class="gmail-nc" style="color:rgb(14,132,181);font-weight:bold">RT</span> <span class="gmail-n">Z</span><span class="gmail-p">;</span>
<span class="gmail-p">};</span>
<span class="gmail-kt" style="color:rgb(144,32,0)">int</span> <span class="gmail-o" style="color:rgb(102,102,102)">*</span><span class="gmail-nf" style="color:rgb(6,40,126)">foo</span><span class="gmail-p">(</span><span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">struct</span> <span class="gmail-nc" style="color:rgb(14,132,181);font-weight:bold">ST</span> <span class="gmail-o" style="color:rgb(102,102,102)">*</span><span class="gmail-n">s</span><span class="gmail-p">)</span> <span class="gmail-p">{</span>
<span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">return</span> <span class="gmail-o" style="color:rgb(102,102,102)">&</span><span class="gmail-n">s</span><span class="gmail-p">[</span><span class="gmail-mi" style="color:rgb(64,160,112)">1</span><span class="gmail-p">].</span><span class="gmail-n">Z</span><span class="gmail-p">.</span><span class="gmail-n">B</span><span class="gmail-p">[</span><span class="gmail-mi" style="color:rgb(64,160,112)">5</span><span class="gmail-p">][</span><span class="gmail-mi" style="color:rgb(64,160,112)">13</span><span class="gmail-p">];</span>
<span class="gmail-p">}</span></pre></div><div>the IR could be:</div><div><pre style="overflow:auto hidden;font-family:Consolas,"Deja Vu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:0.95em;line-height:16.625px;padding:0.5em;border:1px solid rgb(204,204,204);background-color:rgb(248,248,248);color:rgb(0,0,0)"><span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">define</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span><span class="gmail-p">*</span> <span class="gmail-vg" style="color:rgb(187,96,213)">@foo</span><span class="gmail-p">(</span><span class="gmail-nv" style="color:rgb(187,96,213)">%struct.ST</span><span class="gmail-p">*</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%s</span><span class="gmail-p">)</span> <span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">nounwind</span> <span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">uwtable</span> <span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">readnone</span> <span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">optsize</span> <span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">ssp</span> <span class="gmail-p">{</span>
<span class="gmail-nl" style="color:rgb(0,32,112);font-weight:bold">entry:</span>
<span class="gmail-nv" style="color:rgb(187,96,213)">%arrayidx</span> <span class="gmail-p">=</span> <span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">getelementptr</span> <span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">inbounds</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%struct.ST</span><span class="gmail-p">,</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%struct.ST</span><span class="gmail-p">*</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%s</span><span class="gmail-p">,</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i64</span> <span class="gmail-m" style="color:rgb(64,160,112)">1</span><span class="gmail-p">,</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span> <span class="gmail-m" style="color:rgb(64,160,112)">2</span><span class="gmail-p">,</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span> <span class="gmail-m" style="color:rgb(64,160,112)">1</span><span class="gmail-p">,</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i64</span> <span class="gmail-m" style="color:rgb(64,160,112)">5</span><span class="gmail-p">,</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i64</span> <span class="gmail-m" style="color:rgb(64,160,112)">13</span>
<span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">ret</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span><span class="gmail-p">*</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%arrayidx</span>
<span class="gmail-p">}</span></pre></div><div>or </div><div><pre style="overflow:auto hidden;font-family:Consolas,"Deja Vu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:0.95em;line-height:16.625px;padding:0.5em;border:1px solid rgb(204,204,204);background-color:rgb(248,248,248);color:rgb(0,0,0)"><span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">define</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span><span class="gmail-p">*</span> <span class="gmail-vg" style="color:rgb(187,96,213)">@foo</span><span class="gmail-p">(</span><span class="gmail-nv" style="color:rgb(187,96,213)">%struct.ST</span><span class="gmail-p">*</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%s</span><span class="gmail-p">)</span> <span class="gmail-p">{</span>
<span class="gmail-nv" style="color:rgb(187,96,213)">%t1</span> <span class="gmail-p">=</span> <span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">getelementptr</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%struct.ST</span><span class="gmail-p">,</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%struct.ST</span><span class="gmail-p">*</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%s</span><span class="gmail-p">,</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span> <span class="gmail-m" style="color:rgb(64,160,112)">1</span> <span class="gmail-c" style="color:rgb(96,160,176);font-style:italic">; yields %struct.ST*:%t1</span>
<span class="gmail-nv" style="color:rgb(187,96,213)">%t2</span> <span class="gmail-p">=</span> <span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">getelementptr</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%struct.ST</span><span class="gmail-p">,</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%struct.ST</span><span class="gmail-p">*</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%t1</span><span class="gmail-p">,</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span> <span class="gmail-m" style="color:rgb(64,160,112)">0</span><span class="gmail-p">,</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span> <span class="gmail-m" style="color:rgb(64,160,112)">2</span> <span class="gmail-c" style="color:rgb(96,160,176);font-style:italic">; yields %struct.RT*:%t2</span>
<span class="gmail-nv" style="color:rgb(187,96,213)">%t3</span> <span class="gmail-p">=</span> <span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">getelementptr</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%struct.RT</span><span class="gmail-p">,</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%struct.RT</span><span class="gmail-p">*</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%t2</span><span class="gmail-p">,</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span> <span class="gmail-m" style="color:rgb(64,160,112)">0</span><span class="gmail-p">,</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span> <span class="gmail-m" style="color:rgb(64,160,112)">1</span> <span class="gmail-c" style="color:rgb(96,160,176);font-style:italic">; yields [10 x [20 x i32]]*:%t3</span>
<span class="gmail-nv" style="color:rgb(187,96,213)">%t4</span> <span class="gmail-p">=</span> <span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">getelementptr</span> <span class="gmail-p">[</span><span class="gmail-m" style="color:rgb(64,160,112)">10</span> <span class="gmail-p">x</span> <span class="gmail-p">[</span><span class="gmail-m" style="color:rgb(64,160,112)">20</span> <span class="gmail-p">x</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span><span class="gmail-p">]],</span> <span class="gmail-p">[</span><span class="gmail-m" style="color:rgb(64,160,112)">10</span> <span class="gmail-p">x</span> <span class="gmail-p">[</span><span class="gmail-m" style="color:rgb(64,160,112)">20</span> <span class="gmail-p">x</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span><span class="gmail-p">]]*</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%t3</span><span class="gmail-p">,</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span> <span class="gmail-m" style="color:rgb(64,160,112)">0</span><span class="gmail-p">,</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span> <span class="gmail-m" style="color:rgb(64,160,112)">5</span> <span class="gmail-c" style="color:rgb(96,160,176);font-style:italic">; yields [20 x i32]*:%t4</span>
<span class="gmail-nv" style="color:rgb(187,96,213)">%t5</span> <span class="gmail-p">=</span> <span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">getelementptr</span> <span class="gmail-p">[</span><span class="gmail-m" style="color:rgb(64,160,112)">20</span> <span class="gmail-p">x</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span><span class="gmail-p">],</span> <span class="gmail-p">[</span><span class="gmail-m" style="color:rgb(64,160,112)">20</span> <span class="gmail-p">x</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span><span class="gmail-p">]*</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%t4</span><span class="gmail-p">,</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span> <span class="gmail-m" style="color:rgb(64,160,112)">0</span><span class="gmail-p">,</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span> <span class="gmail-m" style="color:rgb(64,160,112)">13</span> <span class="gmail-c" style="color:rgb(96,160,176);font-style:italic">; yields i32*:%t5</span>
<span class="gmail-k" style="color:rgb(0,112,32);font-weight:bold">ret</span> <span class="gmail-kt" style="color:rgb(144,32,0)">i32</span><span class="gmail-p">*</span> <span class="gmail-nv" style="color:rgb(187,96,213)">%t5</span>
<span class="gmail-p">}</span></pre></div><div>I wonder when the llvm will generate the former one, and when it will generate the later one?</div><div><br></div><div>Thank you very much!</div><div><br></div><div>Sincerely,</div><div>Shulin</div><div><br></div><div><br></div></div>