[LLVMdev] [cfe-dev] conditional operator

Alistair Lynn arplynn at gmail.com
Tue Jul 20 08:28:55 PDT 2010


Hello Jochen-

Your expectation is incorrect, I'm afraid. Typically this would be expanded
to something like this:

   …
   %cmp = …
   br i1 %cmp, label %ternary.true, label %ternary.false
ternary.true:
   %truevalue = …
   br label %ternary.end
ternary.false:
   %falsevalue = …
   br label %ternary.end
ternary.end:
   %value = phi %type [%truevalue, %ternary.true], [%falsevalue, %ternary.false]

This may then be optimised to a select by later passes, if it's considered to be
profitable to do so.

The reason for this is that C requires that only the value which is needed
is evaluated. If one of the selected values has a side effect, this becomes significant.

Alistair

On 20 Jul 2010, at 15:39, Jochen Wilhelmy wrote:

> Hi!
> 
> when using something like
> (a < b) ? a : b
> 
> I would expect that the select instruction is always used.
> But, if a or b are a global variable, a real branch with multiple
> basic blocks is created. Is this a bug or a feature?
> 
> -Jochen
> 
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev





More information about the llvm-dev mailing list