<div dir="ltr"><div>Is there a way to stop dead code elimination? Yes of course. Use the results!</div><div><br></div><div>Why would you want to keep useless code, the results of which are not visible to the rest of the program? That's exactly what we want an optimiser framework like LLVM to do.</div><div><br></div><div>If you write a C function like ...</div><div><br></div><div><div>void foo(){</div><div> int v1 = 2 + 10;</div><div> int v2 = v1 + 3;</div><div> int v3 = 3 - 2;</div><div> int v4 = v3 - 5;</div><div> int v5 = 4 ^ 4;</div><div> int v6 = v5 ^ v5;</div><div> int v7 = 5 | v4;</div><div> int v8 = v4 & 2;</div><div> int v9 = 7 | 2;</div><div> int v10 = v8 | v6;</div><div> int v11 = v9 & v10;</div><div> return;</div><div>}</div></div><div><br></div><div>... then Clang (or gcc) will generate just a return instruction (and maybe saving/restoring a frame pointer for debugging purposes, depending on ABI) with any optimisation level of -O1 or more.</div><div><br></div><div>If you use -O0 then a stack frame will be generated, and all variable accesses will be to the stack frame, not registers. Again, for debugging purposes.</div><div><br></div><div><br></div>On Mon, May 11, 2015 at 2:08 PM, Avinash Bole <span dir="ltr"><<a href="mailto:boleavinash@gmail.com" target="_blank">boleavinash@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div>Hi Bruce!!<br><br></div>Im using "C" language. <br><br></div>As you said...,i tried using Global pointer..,but i still am not able to get the Registers exactly as i wanted.<br></div><br></div>I found "-mem2reg" as a better alternative. But Problem is even though the resulting Intermediate file( .ll ) is <br></div>good...,the final assembly file( .s) file is making a lot of DCE.<br><br></div><div>Im Posting an example :<br><br><br></div><div><b><u>Original Intermediate file :</u></b><br></div><div><br>define void @main() #0 {<br> %a = alloca i32, align 4<br> %b = alloca i32, align 4<br> %c = alloca i32, align 4<br> store i32 2, i32* %a, align 4<br> store i32 3, i32* %b, align 4<br> store i32 4, i32* %c, align 4<br> %1 = load i32* %a, align 4<br> %2 = add nsw i32 %1, 10<br> store i32 %2, i32* %a, align 4<br> %3 = load i32* %a, align 4<br> %4 = load i32* %b, align 4<br> %5 = add nsw i32 %3, %4<br> store i32 %5, i32* %a, align 4<br> store i32 5, i32* %a, align 4<br> %6 = load i32* %b, align 4<br> %7 = sub nsw i32 %6, 2<br> store i32 %7, i32* %b, align 4<br> %8 = load i32* %b, align 4<br> %9 = load i32* %a, align 4<br> %10 = sub nsw i32 %8, %9<br> store i32 %10, i32* %b, align 4<br> %11 = load i32* %c, align 4<br> %12 = xor i32 %11, 4<br> store i32 %12, i32* %c, align 4<br> %13 = load i32* %c, align 4<br> %14 = load i32* %c, align 4<br> %15 = xor i32 %13, %14<br> store i32 %15, i32* %c, align 4<br> %16 = load i32* %a, align 4<br> %17 = load i32* %b, align 4<br> %18 = or i32 %16, %17<br> store i32 %18, i32* %a, align 4<br> %19 = load i32* %b, align 4<br> %20 = and i32 %19, 2<br> store i32 %20, i32* %b, align 4<br> store i32 7, i32* %a, align 4<br> %21 = load i32* %a, align 4<br> %22 = or i32 %21, 2<br> %23 = load i32* %b, align 4<br> %24 = load i32* %c, align 4<br> %25 = or i32 %23, %24<br> %26 = and i32 %22, %25<br> store i32 %26, i32* %b, align 4<br> ret void<br>}<br><br><br></div><u><b>After using "-mem2reg"</b></u>:<br><div><br><div>define void @main() #0 {<br> %1 = add nsw i32 2, 10<br> %2 = add nsw i32 %1, 3<br> %3 = sub nsw i32 3, 2<br> %4 = sub nsw i32 %3, 5<br> %5 = xor i32 4, 4<br> %6 = xor i32 %5, %5<br> %7 = or i32 5, %4<br> %8 = and i32 %4, 2<br> %9 = or i32 7, 2<br> %10 = or i32 %8, %6<br> %11 = and i32 %9, %10<br> ret void<br>}<br><br></div><div><u><b>Assembly file:</b></u><br><br> Return;<br><br><br></div><div>As you can see...,all the code is eliminated as dead code...,now is there a possibility to stop Dead code elimination?<br><br></div><div><br></div><div><br></div></div></div>
<br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><br></div></div>