[PATCH] D16836: [CodeGenPrepare] Don't transform select instructions into branches when both of operands are cheap

Hal Finkel via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 3 04:44:52 PST 2016

hfinkel added a comment.

In http://reviews.llvm.org/D16836#342678, @flyingforyou wrote:

>   for.body4:                                        ; preds = %for.body4, %for.cond1.preheader
>     %indvars.iv = phi i64 [ 0, %for.cond1.preheader ], [ %indvars.iv.next, %for.body4 ]
>     %j.126 = phi i32 [ -1, %for.cond1.preheader ], [ %j.2, %for.body4 ]
>     %scevgep = getelementptr double, double* getelementptr inbounds (%struct.GlobalData, %struct.GlobalData* @global_data, i32 0, i32 0, i32 0), i64 %indvars.iv
>     %3 = load double, double* %scevgep, align 8, !tbaa !5
>     %cmp5 = fcmp olt double %3, 0.000000e+00
>     %tmp = trunc i64 %indvars.iv to i32
>     %j.2 = select i1 %cmp5, i32 %tmp, i32 %j.126
>     %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
>     %exitcond = icmp eq i64 32000, %indvars.iv.next
>     br i1 %exitcond, label %for.cond.cleanup3, label %for.body4
> In this case, %tmp, %j.126 is target for sinking.
> This code will translate to arm64 likes below.
>  after patch.


> We transform just for hiding mov instruction. I don't think it's proper transformation regarding performance.

This makes perfect sense, thanks! Here's the thing: There are several reasons why the cost of an IR instruction might be "free". One reason is that the instruction represents something we already have (like a PHI or an Argument). One reason is that it represents something that can be implicitly folded (like a truncation). Constants that can be used directly as operands also fall into this category.

In this loop, the select has two operands. One is a PHI (free because it represents results already in registers), and the other is a truncation. This truncation is free because it can be folded with its user. The truncation, however, is also of a PHI. And that's the important part.

I think that what you want to do here is, for each operand, walk back through "free" CastInsts, and see if you find a PHI, an Argument. These things should already be in registers, and it is not worth branching.


More information about the llvm-commits mailing list