<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Aug 22, 2015, at 7:27 AM, Xiangyang Guo <<a href="mailto:xguo6@ncsu.edu" class="">xguo6@ncsu.edu</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_extra">Hi, Mehdi,</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">For example, I have this very simple source code:</div><div class="gmail_extra"><div class="gmail_extra">void foo( int n, int array_x[])</div><div class="gmail_extra">{</div><div class="gmail_extra"> for (int i=0; i < n; i++)</div><div class="gmail_extra"><span class="Apple-tab-span" style="white-space:pre"> </span> array_x[i] = i; </div><div class="gmail_extra">}</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">After I use "clang -emit-llvm -o bc_from_clang.bc -c <a href="http://try.cc" class="">try.cc</a>", I get bc_from_clang.bc. With my code (using LLVM IRbuilder API), I get bc_from_api.bc. Attachment please find thse two files. I also past the IR here.</div><div class="gmail_extra">******************************** Clang Generate IR Start ***********************************************************</div><div class="gmail_extra"><div class="gmail_extra">; ModuleID = 'bc_from_clang.bc'</div><div class="gmail_extra">target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"</div><div class="gmail_extra">target triple = "x86_64-unknown-linux-gnu"</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">; Function Attrs: nounwind uwtable</div><div class="gmail_extra">define void @_Z3fooiPi(i32 %n, i32* %array_x) #0 {</div><div class="gmail_extra"> %1 = alloca i32, align 4</div><div class="gmail_extra"> %2 = alloca i32*, align 8</div><div class="gmail_extra"> %i = alloca i32, align 4</div><div class="gmail_extra"> store i32 %n, i32* %1, align 4</div><div class="gmail_extra"> store i32* %array_x, i32** %2, align 8</div><div class="gmail_extra"> store i32 0, i32* %i, align 4</div><div class="gmail_extra"> br label %3</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">; <label>:3 ; preds = %13, %0</div><div class="gmail_extra"> %4 = load i32, i32* %i, align 4</div><div class="gmail_extra"> %5 = load i32, i32* %1, align 4</div><div class="gmail_extra"> %6 = icmp slt i32 %4, %5</div><div class="gmail_extra"> br i1 %6, label %7, label %16</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">; <label>:7 ; preds = %3</div><div class="gmail_extra"> %8 = load i32, i32* %i, align 4</div><div class="gmail_extra"> %9 = load i32, i32* %i, align 4</div><div class="gmail_extra"> %10 = sext i32 %9 to i64</div><div class="gmail_extra"> %11 = load i32*, i32** %2, align 8</div><div class="gmail_extra"> %12 = getelementptr inbounds i32, i32* %11, i64 %10</div><div class="gmail_extra"> store i32 %8, i32* %12, align 4</div><div class="gmail_extra"> br label %13</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">; <label>:13 ; preds = %7</div><div class="gmail_extra"> %14 = load i32, i32* %i, align 4</div><div class="gmail_extra"> %15 = add nsw i32 %14, 1</div><div class="gmail_extra"> store i32 %15, i32* %i, align 4</div><div class="gmail_extra"> br label %3</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">; <label>:16 ; preds = %3</div><div class="gmail_extra"> ret void</div><div class="gmail_extra">}</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">attributes #0 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">!llvm.ident = !{!0}</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">!0 = !{!"clang version 3.8.0 (trunk 245730) (llvm/trunk 245727)"}</div><div class=""><br class=""></div></div><div class="gmail_extra">******************************** Clang Generate IR End ***********************************************************<br class=""></div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">******************************** API Generate IR Start ***********************************************************<br class=""></div><div class="gmail_extra"><div class="gmail_extra">; ModuleID = 'bc_from_api.bc'</div><div class="gmail_extra">target triple = "x86_64-unkown-linux-gnu"</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">; Function Attrs: nounwind</div><div class="gmail_extra">define void @_Z3fooiPi(i32 %n, i32* %array_x) #0 {</div><div class="gmail_extra">entry:</div><div class="gmail_extra"> %n.addr = alloca i32, align 4</div><div class="gmail_extra"> %array_x.addr = alloca i32*, align 8</div><div class="gmail_extra"> %i = alloca i32, align 4</div><div class="gmail_extra"> store i32 %n, i32* %n.addr, align 4</div><div class="gmail_extra"> store i32* %array_x, i32** %array_x.addr, align 8</div><div class="gmail_extra"> store i32 0, i32* %i, align 4</div><div class="gmail_extra"> br label %for.cond</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">for.cond: ; preds = %for.inc, %entry</div><div class="gmail_extra"> %0 = load i32, i32* %i, align 4</div><div class="gmail_extra"> %1 = load i32, i32* %n.addr, align 4</div><div class="gmail_extra"> %cmp = icmp slt i32 %0, %1</div><div class="gmail_extra"> br i1 %cmp, label %for.body, label %for.end</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">for.body: ; preds = %for.cond</div><div class="gmail_extra"> %2 = load i32, i32* %i, align 4</div><div class="gmail_extra"> %3 = load i32, i32* %i, align 4</div><div class="gmail_extra"> %idxprom = sext i32 %3 to i64</div><div class="gmail_extra"> %4 = load i32*, i32** %array_x.addr, align 8</div><div class="gmail_extra"> %arrayidx = getelementptr inbounds i32, i32* %4, i64 %idxprom</div><div class="gmail_extra"> store i32 %2, i32* %arrayidx, align 4</div><div class="gmail_extra"> br label %for.inc</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">for.inc: ; preds = %for.body</div><div class="gmail_extra"> %5 = load i32, i32* %i, align 4</div><div class="gmail_extra"> %inc = add i32 %5, 1</div><div class="gmail_extra"> store i32 %inc, i32* %i, align 4</div><div class="gmail_extra"> br label %for.cond</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">for.end: ; preds = %for.cond</div><div class="gmail_extra"> ret void</div><div class="gmail_extra">}</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">attributes #0 = { nounwind }</div><div class=""><br class=""></div></div><div class="gmail_extra">******************************** API Generate IR End ***********************************************************<br class=""></div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">Then I use "opt file.bc -mem2reg -loops -loop-simplify -loop-rotate -lcssa -indvars -loop-unroll -unroll-count=4 -irce -simplifycfg -S" to run both .bc files.</div><div class="gmail_extra">The first .bc file give me this:</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">***************************** Clang Generate IR with LoopUnrolling Start**********************************************</div><div class="gmail_extra"><div class="gmail_extra">; ModuleID = 'bc_from_clang.bc'</div><div class="gmail_extra">target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"</div><div class="gmail_extra">target triple = "x86_64-unknown-linux-gnu"</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">; Function Attrs: nounwind uwtable</div><div class="gmail_extra">define void @_Z3fooiPi(i32 %n, i32* %array_x) #0 {</div><div class="gmail_extra"> %1 = icmp slt i32 0, %n</div><div class="gmail_extra"> br i1 %1, label %.<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__lr.ph&d=BQMFaQ&c=eEvniauFctOgLOKGJOplqw&r=v-ruWq0KCv2O3thJZiK6naxuXK8mQHZUmGq5FBtAmZ4&m=9pD2qNHBiPJscAs-ZzWT7q61NeZNcwsY8Io52JnaI3E&s=OaVJWcn8TI03qp9lgqeWMIVMySoyBIPowkHDEyR7lvQ&e=" class="">lr.ph</a>, label %._crit_edge</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">.<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__lr.ph&d=BQMFaQ&c=eEvniauFctOgLOKGJOplqw&r=v-ruWq0KCv2O3thJZiK6naxuXK8mQHZUmGq5FBtAmZ4&m=9pD2qNHBiPJscAs-ZzWT7q61NeZNcwsY8Io52JnaI3E&s=OaVJWcn8TI03qp9lgqeWMIVMySoyBIPowkHDEyR7lvQ&e=" class="">lr.ph</a>: ; preds = %0</div><div class="gmail_extra"> %2 = add i32 %n, -1</div><div class="gmail_extra"> %xtraiter = and i32 %n, 3</div><div class="gmail_extra"> %lcmp.mod = icmp ne i32 %xtraiter, 0</div><div class="gmail_extra"> br i1 %lcmp.mod, label %3, label %.lr.ph.split</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">; <label>:3 ; preds = %3, %.<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__lr.ph&d=BQMFaQ&c=eEvniauFctOgLOKGJOplqw&r=v-ruWq0KCv2O3thJZiK6naxuXK8mQHZUmGq5FBtAmZ4&m=9pD2qNHBiPJscAs-ZzWT7q61NeZNcwsY8Io52JnaI3E&s=OaVJWcn8TI03qp9lgqeWMIVMySoyBIPowkHDEyR7lvQ&e=" class="">lr.ph</a></div><div class="gmail_extra"> %indvars.iv.prol = phi i64 [ 0, %.<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__lr.ph&d=BQMFaQ&c=eEvniauFctOgLOKGJOplqw&r=v-ruWq0KCv2O3thJZiK6naxuXK8mQHZUmGq5FBtAmZ4&m=9pD2qNHBiPJscAs-ZzWT7q61NeZNcwsY8Io52JnaI3E&s=OaVJWcn8TI03qp9lgqeWMIVMySoyBIPowkHDEyR7lvQ&e=" class="">lr.ph</a> ], [ %indvars.iv.next.prol, %3 ]</div><div class="gmail_extra"> %prol.iter = phi i32 [ %xtraiter, %.<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__lr.ph&d=BQMFaQ&c=eEvniauFctOgLOKGJOplqw&r=v-ruWq0KCv2O3thJZiK6naxuXK8mQHZUmGq5FBtAmZ4&m=9pD2qNHBiPJscAs-ZzWT7q61NeZNcwsY8Io52JnaI3E&s=OaVJWcn8TI03qp9lgqeWMIVMySoyBIPowkHDEyR7lvQ&e=" class="">lr.ph</a> ], [ %prol.iter.sub, %3 ]</div><div class="gmail_extra"> %4 = getelementptr inbounds i32, i32* %array_x, i64 %indvars.iv.prol</div><div class="gmail_extra"> %5 = trunc i64 %indvars.iv.prol to i32</div><div class="gmail_extra"> store i32 %5, i32* %4, align 4</div><div class="gmail_extra"> %indvars.iv.next.prol = add nuw nsw i64 %indvars.iv.prol, 1</div><div class="gmail_extra"> %lftr.wideiv.prol = trunc i64 %indvars.iv.next.prol to i32</div><div class="gmail_extra"> %exitcond.prol = icmp ne i32 %lftr.wideiv.prol, %n</div><div class="gmail_extra"> %prol.iter.sub = sub i32 %prol.iter, 1</div><div class="gmail_extra"> %prol.iter.cmp = icmp ne i32 %prol.iter.sub, 0</div><div class="gmail_extra"> br i1 %prol.iter.cmp, label %3, label %.lr.ph.split, !llvm.loop !1</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">.lr.ph.split: ; preds = %3, %.<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__lr.ph&d=BQMFaQ&c=eEvniauFctOgLOKGJOplqw&r=v-ruWq0KCv2O3thJZiK6naxuXK8mQHZUmGq5FBtAmZ4&m=9pD2qNHBiPJscAs-ZzWT7q61NeZNcwsY8Io52JnaI3E&s=OaVJWcn8TI03qp9lgqeWMIVMySoyBIPowkHDEyR7lvQ&e=" class="">lr.ph</a></div><div class="gmail_extra"> %indvars.iv.unr = phi i64 [ 0, %.<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__lr.ph&d=BQMFaQ&c=eEvniauFctOgLOKGJOplqw&r=v-ruWq0KCv2O3thJZiK6naxuXK8mQHZUmGq5FBtAmZ4&m=9pD2qNHBiPJscAs-ZzWT7q61NeZNcwsY8Io52JnaI3E&s=OaVJWcn8TI03qp9lgqeWMIVMySoyBIPowkHDEyR7lvQ&e=" class="">lr.ph</a> ], [ %indvars.iv.next.prol, %3 ]</div><div class="gmail_extra"> %6 = icmp ult i32 %2, 3</div><div class="gmail_extra"> br i1 %6, label %._crit_edge, label %.lr.ph.split.split</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">.lr.ph.split.split: ; preds = %.lr.ph.split, %.lr.ph.split.split</div><div class="gmail_extra"> %indvars.iv = phi i64 [ %indvars.iv.next.3, %.lr.ph.split.split ], [ %indvars.iv.unr, %.lr.ph.split ]</div><div class="gmail_extra"> %7 = getelementptr inbounds i32, i32* %array_x, i64 %indvars.iv</div><div class="gmail_extra"> %8 = trunc i64 %indvars.iv to i32</div><div class="gmail_extra"> store i32 %8, i32* %7, align 4</div><div class="gmail_extra"> %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1</div><div class="gmail_extra"> %lftr.wideiv = trunc i64 %indvars.iv.next to i32</div><div class="gmail_extra"> %9 = getelementptr inbounds i32, i32* %array_x, i64 %indvars.iv.next</div><div class="gmail_extra"> %10 = trunc i64 %indvars.iv.next to i32</div><div class="gmail_extra"> store i32 %10, i32* %9, align 4</div><div class="gmail_extra"> %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv.next, 1</div><div class="gmail_extra"> %lftr.wideiv.1 = trunc i64 %indvars.iv.next.1 to i32</div><div class="gmail_extra"> %11 = getelementptr inbounds i32, i32* %array_x, i64 %indvars.iv.next.1</div><div class="gmail_extra"> %12 = trunc i64 %indvars.iv.next.1 to i32</div><div class="gmail_extra"> store i32 %12, i32* %11, align 4</div><div class="gmail_extra"> %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv.next.1, 1</div><div class="gmail_extra"> %lftr.wideiv.2 = trunc i64 %indvars.iv.next.2 to i32</div><div class="gmail_extra"> %13 = getelementptr inbounds i32, i32* %array_x, i64 %indvars.iv.next.2</div><div class="gmail_extra"> %14 = trunc i64 %indvars.iv.next.2 to i32</div><div class="gmail_extra"> store i32 %14, i32* %13, align 4</div><div class="gmail_extra"> %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv.next.2, 1</div><div class="gmail_extra"> %lftr.wideiv.3 = trunc i64 %indvars.iv.next.3 to i32</div><div class="gmail_extra"> %exitcond.3 = icmp ne i32 %lftr.wideiv.3, %n</div><div class="gmail_extra"> br i1 %exitcond.3, label %.lr.ph.split.split, label %._crit_edge</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">._crit_edge: ; preds = %.lr.ph.split, %.lr.ph.split.split, %0</div><div class="gmail_extra"> ret void</div><div class="gmail_extra">}</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">attributes #0 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">!llvm.ident = !{!0}</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">!0 = !{!"clang version 3.8.0 (trunk 245730) (llvm/trunk 245727)"}</div><div class="gmail_extra">!1 = distinct !{!1, !2}</div><div class="gmail_extra">!2 = !{!"llvm.loop.unroll.disable"}</div><div class=""><br class=""></div></div><div class="gmail_extra">******************************Clang Generate IR with LoopUnrolling End***********************************************</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">The second .bc file gives me this:</div><div class="gmail_extra">******************************API Generate IR with LoopUnrolling Start*************************************************</div><div class="gmail_extra"><div class="gmail_extra">; ModuleID = 'bc_from_api.bc'</div><div class="gmail_extra">target triple = "x86_64-unkown-linux-gnu"</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">; Function Attrs: nounwind</div><div class="gmail_extra">define void @_Z3fooiPi(i32 %n, i32* %array_x) #0 {</div><div class="gmail_extra">entry:</div><div class="gmail_extra"> %cmp.1 = icmp slt i32 0, %n</div><div class="gmail_extra"> br i1 %cmp.1, label %for.body, label %for.end</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">for.body: ; preds = %entry, %for.body.3</div><div class="gmail_extra"> %i.02 = phi i32 [ %inc.3, %for.body.3 ], [ 0, %entry ]</div><div class="gmail_extra"> %idxprom = sext i32 %i.02 to i64</div><div class="gmail_extra"> %arrayidx = getelementptr inbounds i32, i32* %array_x, i64 %idxprom</div><div class="gmail_extra"> store i32 %i.02, i32* %arrayidx, align 4</div><div class="gmail_extra"> %inc = add nuw nsw i32 %i.02, 1</div><div class="gmail_extra"> %cmp = icmp slt i32 %inc, %n</div><div class="gmail_extra"> br i1 %cmp, label %for.body.1, label %for.end</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">for.end: ; preds = %for.body, %for.body.1, %for.body.2, %for.body.3, %entry</div><div class="gmail_extra"> ret void</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">for.body.1: ; preds = %for.body</div><div class="gmail_extra"> %idxprom.1 = sext i32 %inc to i64</div><div class="gmail_extra"> %arrayidx.1 = getelementptr inbounds i32, i32* %array_x, i64 %idxprom.1</div><div class="gmail_extra"> store i32 %inc, i32* %arrayidx.1, align 4</div><div class="gmail_extra"> %inc.1 = add nuw nsw i32 %inc, 1</div><div class="gmail_extra"> %cmp.1.3 = icmp slt i32 %inc.1, %n</div><div class="gmail_extra"> br i1 %cmp.1.3, label %for.body.2, label %for.end</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">for.body.2: ; preds = %for.body.1</div><div class="gmail_extra"> %idxprom.2 = sext i32 %inc.1 to i64</div><div class="gmail_extra"> %arrayidx.2 = getelementptr inbounds i32, i32* %array_x, i64 %idxprom.2</div><div class="gmail_extra"> store i32 %inc.1, i32* %arrayidx.2, align 4</div><div class="gmail_extra"> %inc.2 = add nuw nsw i32 %inc.1, 1</div><div class="gmail_extra"> %cmp.2 = icmp slt i32 %inc.2, %n</div><div class="gmail_extra"> br i1 %cmp.2, label %for.body.3, label %for.end</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">for.body.3: ; preds = %for.body.2</div><div class="gmail_extra"> %idxprom.3 = sext i32 %inc.2 to i64</div><div class="gmail_extra"> %arrayidx.3 = getelementptr inbounds i32, i32* %array_x, i64 %idxprom.3</div><div class="gmail_extra"> store i32 %inc.2, i32* %arrayidx.3, align 4</div><div class="gmail_extra"> %inc.3 = add nuw nsw i32 %inc.2, 1</div><div class="gmail_extra"> %cmp.3 = icmp slt i32 %inc.3, %n</div><div class="gmail_extra"> br i1 %cmp.3, label %for.body, label %for.end</div><div class="gmail_extra">}</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">attributes #0 = { nounwind }</div></div><div class="gmail_extra">******************************API Generate IR with LoopUnrolling End**************************************************</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">Sorry I post two many code here. Can you give me any suggestion? </div></div></div></div></blockquote><div><br class=""></div><div>Yes, use an online service like pastebin :)</div><div><br class=""></div><div>You don’t have defined the DataLayout in the API cases, it should help to do so.</div><div><br class=""></div><div>— </div><div>Mehdi</div><div><br class=""></div></div></body></html>