[LLVMdev] if-conversion

797 views
Skip to first unread message

Ralf Karrenberg

unread,
Jan 20, 2009, 5:29:05 AM1/20/09
to llv...@cs.uiuc.edu
Hello everyone,

I'd like to know if there is an optimization in llvm doing something
like an if-conversion on the IR. I only found IfConversion.cpp which
appears to only provide the desired functionality on machine code during
code-generation.

I want to transform branches into serial code with select-instructions
as a pre-processing step for further transformations.

If there is no such pass, I will possibly write one and share it
(although I have no idea how to do so yet), so please let me know :).

Cheers,
Ralf
_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

Eli Friedman

unread,
Jan 20, 2009, 11:25:30 AM1/20/09
to LLVM Developers Mailing List
On Tue, Jan 20, 2009 at 2:29 AM, Ralf Karrenberg <Cha...@gmx.de> wrote:
> I want to transform branches into serial code with select-instructions
> as a pre-processing step for further transformations.

SimplifyCFG should be doing this transform in very simple cases.
Depending on what you're doing, though, its might not be aggressive
enough.

-Eli

Devang Patel

unread,
Jan 20, 2009, 1:34:52 PM1/20/09
to LLVM Developers Mailing List

On Jan 20, 2009, at 2:29 AM, Ralf Karrenberg wrote:

> Hello everyone,
>
> I'd like to know if there is an optimization in llvm doing something
> like an if-conversion on the IR. I only found IfConversion.cpp which
> appears to only provide the desired functionality on machine code
> during
> code-generation.
>
> I want to transform branches into serial code with select-instructions
> as a pre-processing step for further transformations.
>
> If there is no such pass, I will possibly write one and share it
> (although I have no idea how to do so yet), so please let me know :).

There is not any pass that can transform branches into serial code
with select-instructions. Simplify CFG can handle simple cases of
selecting one value based on a condition, but its goal is to simplify
cfg. This may not be enough for your needs.

If you write a separate pass, we'd like to integrate it in mainline
llvm sources.

-
Devang

Ralf Karrenberg

unread,
Jan 20, 2009, 3:28:31 PM1/20/09
to LLVM Developers Mailing List
> If you write a separate pass, we'd like to integrate it in mainline
> llvm sources.

Thank you for your answers. I will do my best to develop something which
could be useful for more people :).

Cheers,
Ralf

John Criswell

unread,
Jan 20, 2009, 3:51:07 PM1/20/09
to LLVM Developers Mailing List
Ralf Karrenberg wrote:
>> If you write a separate pass, we'd like to integrate it in mainline
>> llvm sources.
>>
>
> Thank you for your answers. I will do my best to develop something which
> could be useful for more people :).
>
If you're looking for sources on how to do if-conversion, there's an
algorithm for it in the book "Optimizing Compilers for Modern
Architectures," Chapter 7. That algorithm, in turn, relies upon
computing control dependence. There's an algorithm for computing that
in the same chapter, and there's another algorithm for control
dependence in the following paper:

http://portal.acm.org/citation.cfm?id=115372.115320&coll=portal&dl=ACM&CFID=18980569&CFTOKEN=50678055

My recommendation is to write two passes: one that computes control
dependence and another that performs if-conversion. A
control-dependence analysis pass is useful for other analysis and
transform passes.

-- John T.

RobBishop

unread,
Nov 6, 2013, 1:50:09 PM11/6/13
to llv...@cs.uiuc.edu
Hi all,

Sorry to dig up an old thread but I wondered what the status of
if-conversion in LLVM is. Has any work been done towards handling this as a
transform pass on the IR?

I'm looking to implement an if-conversion pass and wanted to ensure that I'm
not duplicating work. Is this something that others would also find useful?

Rob



--
View this message in context: http://llvm.1065342.n5.nabble.com/if-conversion-tp2349p62937.html
Sent from the LLVM - Dev mailing list archive at Nabble.com.

Ralf Karrenberg

unread,
Nov 10, 2013, 1:56:32 PM11/10/13
to RobBishop, llv...@cs.uiuc.edu
Hi Rob,

I chose to answer on the list since from time to time people come back
to this.

That said, I did implement the generic variant of if-conversion that is
called "control-flow to data-flow conversion" as a basis for SIMD
vectorization. Essentially, the conversion removes all control flow
except for loop back edges and replaces it by masks and blend (select)
operations.

Details on the algorithm can be found in our paper on "Whole-Function
Vectorization" (CGO 2011). The old, LLVM-based implementation of the
algorithm is still online at github I believe. A completely rewritten
one will be released along with submission of my PhD thesis at the end
of the year.

That said, if you are only looking for if-conversion of code with
limited complexity (e.g. no side effects, no loops), it is really simple:
- Compute masks for every block (entry mask: disjunction of incoming
masks, exit masks: conjunctions of entry mask and (negated) condition).
- Replace each phi by a select that uses the entry mask of the
corresponding block.
- Order blocks topologically by data dependencies, remove outgoing
edges, create unconditional branches from each block to the next in the
list.

Cheers,
Ralf

Jonas Wagner

unread,
Nov 11, 2013, 5:49:20 AM11/11/13
to LLVM
Hi,

Sorry to dig up an old thread but I wondered what the status of
if-conversion in LLVM is. Has any work been done towards handling this as a
transform pass on the IR?

As far as I know, some if-conversion is done in SimplifyCFG: Have a look at the SpeculativelyExecuteBB function in lib/Transforms/Utils/SimplifyCFG.cpp.

Cheers,
Jonas

Evan Cheng

unread,
Nov 12, 2013, 12:06:02 PM11/12/13
to Jonas Wagner, LLVM
SimplifyCFG is mostly doing canonicalization. It does some obvious if-conversion. More general if-conversions require target info so it's done in MI passes. LLVM has an early if-converter which uses machinetracemetric and a late one after regalloc. 

Evan

Sent from my iPad
Reply all
Reply to author
Forward
0 new messages