<div dir="ltr"><div><br></div><div>You are right!</div><div><br></div><div>> <span style="font-size:13px">I believe calling </span><span class="gmail_msg" style="font-family:menlo;font-size:9pt;white-space:pre-wrap">pm_builder.</span><span class="gmail_msg" style="font-family:menlo;font-size:9pt;white-space:pre-wrap">populateModulePassManager(pm); will cause a bunch of passes to be ran as if you had ran 'opt -O2'</span></div><div><span class="gmail_msg" style="font-family:menlo;font-size:9pt;white-space:pre-wrap"><br></span></div><div><span class="gmail_msg" style="font-family:menlo;font-size:9pt;white-space:pre-wrap">I modified like this:</span></div><div><span class="gmail_msg" style="font-family:menlo;font-size:9pt;white-space:pre-wrap"><br></span></div><div><span class="gmail_msg" style="font-family:menlo;font-size:9pt;white-space:pre-wrap"><pre style="font-family:menlo;font-size:9pt"><span style="color:rgb(128,128,128);font-style:italic">// llvm::PassManagerBuilder pm_builder; // (deleted)<br></span><span style="color:rgb(128,128,128);font-style:italic"><br></span><span style="color:rgb(128,128,128);font-style:italic"> </span><span style="color:rgb(0,128,128)">llvm</span>::<span style="color:rgb(0,128,128)">legacy</span>::<span style="color:rgb(0,128,128)">PassManager </span>pm;<br><br><br><span style="color:rgb(128,128,128);font-style:italic">// pm_builder.populateModulePassManager(pm); // (deleted)<br></span><span style="color:rgb(128,128,128);font-style:italic"><br></span><span style="color:rgb(128,128,128);font-style:italic"><br></span><span style="color:rgb(128,128,128);font-style:italic"> </span>pm.add(<span style="color:rgb(0,128,128)">llvm</span>::createFunctionInliningPass()); <span style="color:rgb(128,128,128);font-style:italic">// (added)</span></pre></span></div><div><br></div><div>Then, my code worked the same result of `opt -inline`.</div><div><br></div><div>[output]</div><div><div>; ModuleID = 'my module'</div><div><br></div><div>define private i32 @b() {</div><div>entry:</div><div> %c = call i32 @b()</div><div> %res = add i32 2, %c</div><div> ret i32 %res</div><div>}</div><div><br></div><div>define i32 @main() {</div><div>entrypoint:</div><div> %b = call i32 @b()</div><div> ret i32 %b</div><div>}</div></div><div><br></div><div><br></div><div>Thank you,</div><div>Ryo </div><br><br><div class="gmail_quote"><div dir="ltr">2017年3月31日(金) 15:57 <<a href="mailto:nwtgck@gmail.com">nwtgck@gmail.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="gmail_msg">Thank you very much your fast response! <div class="gmail_msg">When typed '<span style="font-family:menlo;font-size:11px" class="gmail_msg">opt-3.8 -S -O2 main.ll</span>', I got:</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">; ModuleID = 'main.ll'</span></p></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg">
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"></span><br class="gmail_msg"></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">; Function Attrs: norecurse nounwind readnone</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">define i32 @main() #0 {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">entrypoint:</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> ret i32 undef</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"></span><br class="gmail_msg"></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">attributes #0 = { norecurse nounwind readnone }</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><br class="gmail_msg"></span></p></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">This result is the same as my C++ code.</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">I'm not sure my use of `llvm::legacyPassManeger` and `llvm::PassManagerBuilder`. To write inlining in C++, I explored in llvm's Github and other repositories which use LLVM, then I wrote the C++ code. I'd like to be taught the correct use. </span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><br class="gmail_msg"></span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><br class="gmail_msg"></span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">Sincerely,</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">Ryo</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><br class="gmail_msg"></span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><br class="gmail_msg"></span></p><br class="gmail_msg"><div class="gmail_quote gmail_msg"><div dir="ltr" class="gmail_msg">2017年3月31日(金) 15:50 Craig Topper <<a href="mailto:craig.topper@gmail.com" class="gmail_msg" target="_blank">craig.topper@gmail.com</a>>:<br class="gmail_msg"></div></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_quote gmail_msg"><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="gmail_msg">I believe calling <span style="font-family:menlo;font-size:9pt;white-space:pre-wrap" class="gmail_msg">pm_builder.</span><span style="font-family:menlo;font-size:9pt;white-space:pre-wrap" class="gmail_msg">populateModulePassManager(pm); will cause a bunch of passes to be ran as if you had ran 'opt -O2'. What do you get if you run '</span><span style="font-size:11px;font-family:menlo;white-space:pre-wrap" class="gmail_msg">opt-3.8 -S -O2 main.ll'?</span></div><div class="gmail_extra gmail_msg"><br clear="all" class="gmail_msg"><div class="gmail_msg"><div class="m_-605661645165651136m_1246184508237410219gmail_signature gmail_msg" data-smartmail="gmail_signature">~Craig</div></div>
<br class="gmail_msg"><div class="gmail_quote gmail_msg"></div></div><div class="gmail_extra gmail_msg"><div class="gmail_quote gmail_msg">On Thu, Mar 30, 2017 at 11:27 PM, via llvm-dev <span dir="ltr" class="gmail_msg"><<a href="mailto:llvm-dev@lists.llvm.org" class="gmail_msg" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br class="gmail_msg"></div></div><div class="gmail_extra gmail_msg"><div class="gmail_quote gmail_msg"><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="gmail_msg">Hi, I'm Ryo Ota. I'm using LLVM 3.8.1. I have a quesion about inlining function in C++ API.<div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">I'd like to inline some functions in a module in the same way as `opt -inline` command. But my C++ code didn't work what I want to do.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">For example, by using `opt -inline` command,`main.ll` is converted into the `inlined.ll`(<span style="font-family:menlo;font-size:9pt" class="gmail_msg">`opt` command worked wh</span><span style="font-family:menlo;font-size:9pt" class="gmail_msg">at I want to do</span>)</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">[main.ll (Not inlined)]</div><div class="gmail_msg"><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">; ModuleID = 'my module'<br class="gmail_msg"></span><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg"><br class="gmail_msg"></span><span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">define private </span>i32 @a() {<br class="gmail_msg"><span style="font-weight:bold" class="gmail_msg">entry:<br class="gmail_msg"></span><span style="font-weight:bold" class="gmail_msg"> </span>%c <span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">= call </span>i32 @c()<br class="gmail_msg"> <span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">ret </span>i32 %c<br class="gmail_msg">}<br class="gmail_msg"><br class="gmail_msg"><span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">define private </span>i32 @b() {<br class="gmail_msg"><span style="font-weight:bold" class="gmail_msg">entry:<br class="gmail_msg"></span><span style="font-weight:bold" class="gmail_msg"> </span>%a <span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">= call </span>i32 @a()<br class="gmail_msg"> %b <span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">= call </span>i32 @b() <span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">; key-point (infinite-recursive)<br class="gmail_msg"></span><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg"> </span>%res <span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">= add </span>i32 %a, %b<br class="gmail_msg"> <span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">ret </span>i32 %res<br class="gmail_msg">}<br class="gmail_msg"><br class="gmail_msg"><span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">define private </span>i32 @c() {<br class="gmail_msg"><span style="font-weight:bold" class="gmail_msg">entry:<br class="gmail_msg"></span><span style="font-weight:bold" class="gmail_msg"> </span><span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">ret </span>i32 <span style="color:rgb(0,0,255)" class="gmail_msg">2<br class="gmail_msg"></span>}<br class="gmail_msg"><br class="gmail_msg"><span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">define </span>i32 @main() {<br class="gmail_msg"><span style="font-weight:bold" class="gmail_msg">entrypoint:<br class="gmail_msg"></span><span style="font-weight:bold" class="gmail_msg"> </span>%b <span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">= call </span>i32 @b()<br class="gmail_msg"> <span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">ret </span>i32 %b<br class="gmail_msg"></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><span style="font-size:9pt" class="gmail_msg">}</span><span style="font-family:sans-serif;font-size:small" class="gmail_msg"> </span><br class="gmail_msg"></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><span style="font-family:sans-serif;font-size:small" class="gmail_msg"><br class="gmail_msg"></span></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><span style="font-family:sans-serif;font-size:small" class="gmail_msg"><br class="gmail_msg"></span></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><span style="font-family:sans-serif;font-size:small" class="gmail_msg">[inlined.ll] Made by `</span><span style="font-size:11px" class="gmail_msg">opt-3.8 -S -inline main.ll > inlined.ll</span><span style="font-family:sans-serif;font-size:small" class="gmail_msg">` command</span></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">define private </span>i32 @b() {<br class="gmail_msg"><span style="font-weight:bold" class="gmail_msg">entry:<br class="gmail_msg"></span><span style="font-weight:bold" class="gmail_msg"> </span>%b <span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">= call </span>i32 @b()<br class="gmail_msg"> %res <span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">= add </span>i32 <span style="color:rgb(0,0,255)" class="gmail_msg">2</span>, %b <span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">; a is inlined. (a == c, c == 2) <br class="gmail_msg"></span><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg"> </span><span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">ret </span>i32 %res<br class="gmail_msg">}<br class="gmail_msg"><br class="gmail_msg"><span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">define </span>i32 @main() {<br class="gmail_msg"><span style="font-weight:bold" class="gmail_msg">entrypoint:<br class="gmail_msg"></span><span style="font-weight:bold" class="gmail_msg"> </span>%b <span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">= call </span>i32 @b()<br class="gmail_msg"> <span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">ret </span>i32 %b<br class="gmail_msg"><span style="font-size:9pt" class="gmail_msg">}</span><br class="gmail_msg"></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><span style="font-size:9pt" class="gmail_msg"><br class="gmail_msg"></span></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><span style="font-size:9pt" class="gmail_msg"><br class="gmail_msg"></span></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><span style="font-size:9pt" class="gmail_msg">`opt` command worked wh</span><span style="font-size:9pt" class="gmail_msg">at I want to do. But my C++ doesn't work like `opt` command. The following code is my C++ code, which didn't work I what to do. My code has `undef`. `inlined.ll` doen't have `undef`.</span></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><span style="font-size:9pt" class="gmail_msg">@main returns @b(). But below code, @main returns undef. </span><span style="font-size:9pt" class="gmail_msg">I think @b has a infinite-recursive call. But I don't want it to dump `undef`. Could you tell me how to stop inlining of `undef`?</span></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><br class="gmail_msg"></div></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg">[output of C++ doe]</pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg">; ModuleID = 'my module'
; Function Attrs: norecurse nounwind readnone
define i32 @main() #0 {
entrypoint:
ret i32 undef
}
attributes #0 = { norecurse nounwind readnone }<br class="gmail_msg"></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><br class="gmail_msg"></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><br class="gmail_msg"></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg">[C++ code]</pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">/// @brief context<br class="gmail_msg"></span><span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">LLVMContext </span>context;<br class="gmail_msg"><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">/// @brief module<br class="gmail_msg"></span><span style="color:rgb(0,128,128)" class="gmail_msg">std</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">unique_ptr</span><<span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Module</span>> module = <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::make_unique<<span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Module</span>>(<span style="color:rgb(0,128,0);font-weight:bold" class="gmail_msg">"my module"</span>, context);<br class="gmail_msg"><br class="gmail_msg"><span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">PassManagerBuilder </span>pm_builder;<br class="gmail_msg"><br class="gmail_msg"><span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">legacy</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">PassManager </span>pm;<br class="gmail_msg"><br class="gmail_msg">pm_builder.<span style="color:rgb(102,14,122)" class="gmail_msg">Inliner </span>= <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::createFunctionInliningPass();<br class="gmail_msg"><br class="gmail_msg">pm_builder.populateModulePassManager(pm);<br class="gmail_msg"><br class="gmail_msg"><br class="gmail_msg"><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">/// @brief Create function type (void) => i32<br class="gmail_msg"></span><span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">FunctionType </span>*commonfuncType = <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">FunctionType</span>::get(<span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Type</span>::getInt32Ty(context), {}, <span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">false</span>);<br class="gmail_msg"><br class="gmail_msg"><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">/// @brief Create a function (define i32 a())<br class="gmail_msg"></span><span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Function </span>*aFun = <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Function</span>::Create(commonfuncType, <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Function</span>::<span style="color:rgb(102,14,122);font-weight:bold;font-style:italic" class="gmail_msg">PrivateLinkage</span>, <span style="color:rgb(0,128,0);font-weight:bold" class="gmail_msg">"a"</span>, module.get());<br class="gmail_msg"><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">/// @brief Create a function (define i32 b())<br class="gmail_msg"></span><span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Function </span>*bFun = <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Function</span>::Create(commonfuncType, <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Function</span>::<span style="color:rgb(102,14,122);font-weight:bold;font-style:italic" class="gmail_msg">PrivateLinkage</span>, <span style="color:rgb(0,128,0);font-weight:bold" class="gmail_msg">"b"</span>, module.get());<br class="gmail_msg"><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">/// @brief Create a function (define i32 c())<br class="gmail_msg"></span><span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Function </span>*cFun = <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Function</span>::Create(commonfuncType, <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Function</span>::<span style="color:rgb(102,14,122);font-weight:bold;font-style:italic" class="gmail_msg">PrivateLinkage</span>, <span style="color:rgb(0,128,0);font-weight:bold" class="gmail_msg">"c"</span>, module.get());<br class="gmail_msg"><br class="gmail_msg"><br class="gmail_msg"><br class="gmail_msg"><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">// Body of the `a` function<br class="gmail_msg"></span>{<br class="gmail_msg"> <span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">/// @brief Set entry label<br class="gmail_msg"></span><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg"> </span><span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">BasicBlock </span>*block = <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">BasicBlock</span>::Create(context, <span style="color:rgb(0,128,0);font-weight:bold" class="gmail_msg">"entry"</span>, aFun);<br class="gmail_msg"> <span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">/// @brief builder<br class="gmail_msg"></span><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg"> </span><span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">IRBuilder</span><> builder(block);<br class="gmail_msg"> builder.SetInsertPoint(block);<br class="gmail_msg"><br class="gmail_msg"> <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">CallInst </span>*cCall = builder.CreateCall(cFun, {}, <span style="color:rgb(0,128,0);font-weight:bold" class="gmail_msg">"c"</span>);<br class="gmail_msg"><br class="gmail_msg"> <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Value </span>*res = cCall;<br class="gmail_msg"><br class="gmail_msg"><br class="gmail_msg"> builder.CreateRet(res);<br class="gmail_msg"> <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::verifyFunction(*aFun);<br class="gmail_msg">}<br class="gmail_msg"><br class="gmail_msg"><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">// Body of the `b` function<br class="gmail_msg"></span>{<br class="gmail_msg"> <span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">/// @brief Set entry label<br class="gmail_msg"></span><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg"> </span><span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">BasicBlock </span>*block = <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">BasicBlock</span>::Create(context, <span style="color:rgb(0,128,0);font-weight:bold" class="gmail_msg">"entry"</span>, bFun);<br class="gmail_msg"> <span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">/// @brief builder<br class="gmail_msg"></span><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg"> </span><span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">IRBuilder</span><> builder(block);<br class="gmail_msg"> builder.SetInsertPoint(block);<br class="gmail_msg"><br class="gmail_msg"><br class="gmail_msg"> <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">CallInst </span>*aCall = builder.CreateCall(aFun, {}, <span style="color:rgb(0,128,0);font-weight:bold" class="gmail_msg">"a"</span>);<br class="gmail_msg"> <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">CallInst </span>*cCall = builder.CreateCall(bFun, {}, <span style="color:rgb(0,128,0);font-weight:bold" class="gmail_msg">"c"</span>);<br class="gmail_msg"><br class="gmail_msg"> <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Value </span>*res = builder.CreateAdd(aCall, cCall, <span style="color:rgb(0,128,0);font-weight:bold" class="gmail_msg">"res"</span>);<br class="gmail_msg"><br class="gmail_msg"><br class="gmail_msg"> builder.CreateRet(res);<br class="gmail_msg"> <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::verifyFunction(*bFun);<br class="gmail_msg">}<br class="gmail_msg"><br class="gmail_msg"><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">// Body of the `c` function<br class="gmail_msg"></span>{<br class="gmail_msg"> <span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">/// @brief Set entry label<br class="gmail_msg"></span><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg"> </span><span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">BasicBlock </span>*block = <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">BasicBlock</span>::Create(context, <span style="color:rgb(0,128,0);font-weight:bold" class="gmail_msg">"entry"</span>, cFun);<br class="gmail_msg"> <span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">/// @brief builder<br class="gmail_msg"></span><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg"> </span><span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">IRBuilder</span><> builder(block);<br class="gmail_msg"> builder.SetInsertPoint(block);<br class="gmail_msg"><br class="gmail_msg"><br class="gmail_msg"> <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Value </span>*res = <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">ConstantInt</span>::get(<span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Type</span>::getInt32Ty(context), <span style="color:rgb(0,0,255)" class="gmail_msg">2</span>);<br class="gmail_msg"><br class="gmail_msg"> builder.CreateRet(res);<br class="gmail_msg"> <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::verifyFunction(*cFun);<br class="gmail_msg">}<br class="gmail_msg"><br class="gmail_msg"><br class="gmail_msg"><span style="color:rgb(128,128,128);font-style:italic" class="gmail_msg">/// @brief Start of Main function<br class="gmail_msg"></span>{<br class="gmail_msg"> <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">FunctionType </span>*<span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">const </span>func_type = <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">FunctionType</span>::get(<span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Type</span>::getInt32Ty(context), <span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">false</span>);<br class="gmail_msg"> <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Function </span>*<span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">const </span>main_func = <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Function</span>::Create(func_type, <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">Function</span>::<span style="color:rgb(102,14,122);font-weight:bold;font-style:italic" class="gmail_msg">ExternalLinkage</span>, <span style="color:rgb(0,128,0);font-weight:bold" class="gmail_msg">"main"</span>,<br class="gmail_msg"> module.get());<br class="gmail_msg"> <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">BasicBlock </span>*<span style="color:rgb(0,0,128);font-weight:bold" class="gmail_msg">const </span>entry = <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">BasicBlock</span>::Create(context, <span style="color:rgb(0,128,0);font-weight:bold" class="gmail_msg">"entrypoint"</span>, main_func);<br class="gmail_msg"> <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">IRBuilder</span><> builder(entry);<br class="gmail_msg"><br class="gmail_msg"> builder.SetInsertPoint(entry);<br class="gmail_msg"><br class="gmail_msg"> <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::<span style="color:rgb(0,128,128)" class="gmail_msg">CallInst </span>*bCall = builder.CreateCall(bFun, {}, <span style="color:rgb(0,128,0);font-weight:bold" class="gmail_msg">"b"</span>);<br class="gmail_msg"> builder.CreateRet(bCall);<br class="gmail_msg"><br class="gmail_msg"> <span style="color:rgb(0,128,128)" class="gmail_msg">llvm</span>::verifyFunction(*main_func);<br class="gmail_msg">}<br class="gmail_msg"><br class="gmail_msg">pm.run(<span style="color:rgb(0,128,128)" class="gmail_msg">*</span>module);<br class="gmail_msg"><br class="gmail_msg">module<span style="color:rgb(0,128,128)" class="gmail_msg">-></span>dump();</pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><br class="gmail_msg"></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><br class="gmail_msg"></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><br class="gmail_msg"></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg">if you comment-outed `<span style="font-size:9pt" class="gmail_msg">pm.run(</span><span style="font-size:9pt;color:rgb(0,128,128)" class="gmail_msg">*</span><span style="font-size:9pt" class="gmail_msg">module);</span><span style="font-size:9pt" class="gmail_msg">` you have the following output.</span></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><span style="font-size:9pt" class="gmail_msg"><br class="gmail_msg"></span></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg">define private i32 @a() {
entry:
%c = call i32 @c()
ret i32 %c
}
define private i32 @b() {
entry:
%a = call i32 @a()
%c = call i32 @b()
%res = add i32 %a, %c
ret i32 %res
}
define private i32 @c() {
entry:
ret i32 2
}
define i32 @main() {
entrypoint:
%b = call i32 @b()
ret i32 %b
}
</pre><div class="gmail_msg"><br class="gmail_msg"></div><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><span style="font-family:sans-serif;font-size:small" class="gmail_msg"><br class="gmail_msg"></span></pre><pre style="font-family:menlo;font-size:9pt" class="gmail_msg"><br class="gmail_msg"></pre></div><div class="gmail_msg">Sincerely,</div><div class="gmail_msg">Ryo</div></div>
<br class="gmail_msg"></blockquote></div></div><div class="gmail_extra gmail_msg"><div class="gmail_quote gmail_msg"><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">_______________________________________________<br class="gmail_msg">
LLVM Developers mailing list<br class="gmail_msg">
<a href="mailto:llvm-dev@lists.llvm.org" class="gmail_msg" target="_blank">llvm-dev@lists.llvm.org</a><br class="gmail_msg">
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" class="gmail_msg" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class="gmail_msg">
<br class="gmail_msg"></blockquote></div><br class="gmail_msg"></div>
</blockquote></div></div></div></blockquote></div></div>