<div dir="ltr"><div>I agree with John; also, if you decide to go this route, you can reuse the code from CodeGenPrepare::optimizeSelectInst:</div><div><br></div><div><a href="https://github.com/llvm/llvm-project/blob/master/llvm/lib/CodeGen/CodeGenPrepare.cpp#L6065">https://github.com/llvm/llvm-project/blob/master/llvm/lib/CodeGen/CodeGenPrepare.cpp#L6065</a></div><div><br></div><div>Alexey<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jan 30, 2020 at 9:00 PM John Regehr via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Several different passes introduce select instructions, such as <br>
InstCombine, SimplifyCFG, and SROA. Searching for calls to <br>
"CreateSelect" in the source tree should give you a fairly complete list.<br>
<br>
It may be better to let the optimization pipeline proceed undisturbed, <br>
but then run a custom pass at the end that turns selects back into <br>
control flow. This would be an easy pass to write.<br>
<br>
John<br>
<br>
<br>
On 1/30/20 3:31 PM, Charitha Saumya via llvm-dev wrote:<br>
> Hi,<br>
> <br>
> I would like to know if there's a way to avoid select instructions <br>
> during the IR generation. What are the optimization passes that can <br>
> result in a select instruction?<br>
> i.e. I want to preserve branches in my code without disabling any other <br>
> optimizations applicable.<br>
> <br>
> For example,<br>
> void foo(int* x, int* y){<br>
> if(*x > 0){<br>
> *y = *x + 10;<br>
> }<br>
> else{<br>
> *y = *x + 20;<br>
> }<br>
> }<br>
> <br>
> with O1 I get,<br>
> <br>
> define void @foo(i32* nocapture readonly, i32* nocapture) <br>
> local_unnamed_addr #0 {<br>
> %3 = load i32, i32* %0, align 4, !tbaa !2<br>
> %4 = icmp sgt i32 %3, 0<br>
> %5 = select i1 %4, i32 10, i32 20<br>
> %6 = add nsw i32 %5, %3<br>
> store i32 %6, i32* %1, align 4, !tbaa !2<br>
> ret void<br>
> }<br>
> <br>
> But with O0,<br>
> <br>
> define void @foo(i32*, i32*) #0 {<br>
> %3 = alloca i32*, align 8<br>
> %4 = alloca i32*, align 8<br>
> store i32* %0, i32** %3, align 8<br>
> store i32* %1, i32** %4, align 8<br>
> %5 = load i32*, i32** %3, align 8<br>
> %6 = load i32, i32* %5, align 4<br>
> %7 = icmp sgt i32 %6, 0<br>
> br i1 %7, label %8, label %13<br>
> <br>
> ; <label>:8: ; preds = %2<br>
> %9 = load i32*, i32** %3, align 8<br>
> %10 = load i32, i32* %9, align 4<br>
> %11 = add nsw i32 %10, 10<br>
> %12 = load i32*, i32** %4, align 8<br>
> store i32 %11, i32* %12, align 4<br>
> br label %18<br>
> <br>
> ; <label>:13: ; preds = %2<br>
> %14 = load i32*, i32** %3, align 8<br>
> %15 = load i32, i32* %14, align 4<br>
> %16 = add nsw i32 %15, 20<br>
> %17 = load i32*, i32** %4, align 8<br>
> store i32 %16, i32* %17, align 4<br>
> br label %18<br>
> <br>
> ; <label>:18: ; preds = %13, %8<br>
> ret void<br>
> }<br>
> <br>
> Thanks,<br>
> Charitha<br>
> <br>
> <br>
> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
> <br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>