How to invoke simplifycfg from code

Simplifycfg doesn’t get run by default even with -O3, but ‘opt -simplifycfg’ can do it. I’m looking to add this functionality to an optimizer program that uses the llvm libraries. What’s the best way to do this? I don’t really mind whether it becomes available as a command line option as in opt, or I need to hardcode it as always on.

To make it available as a command line option… I’m looking at the code for opt, but it makes no reference to simplifycfg anywhere in the code. I’m guessing it does something else that gets that option linked in and somehow thereby connects that to activating the actual pass; how is this done?

Alternatively in an attempt to hardwire it I got as far as FPM.add(new SimplifyCFGPass); but that fails to compile because SimplifyCFGPass isn’t of a compatible type, which indeed it isn’t; what should I be doing here?

If you look at adding simplify cfg to your pass pipeline - look at toy.cpp - I believe it adds simplifyCFG pass as

TheFPM->add(createCFGSimplificationPass());

Aditya

Hi Russell,

Can you run your IR though opt with -O3 and -print-after-all and see when the block becomes empty? PassManagerBuilder::populateModulePassManager has, fairly near the end:

  MPM.add(createCFGSimplificationPass());

but, other things do run afterward. Maybe we need another one of these closer to the end?

One other thing to realize is that an empty block like this might not actually turn up in any generated machine code because of how MachineBlockPlacement (and other MI-level passes) work.

-Hal

Okay, as far as I can see looking through the output of that, the empty block isn’t created near the end, it exists at the start and never goes away.

From: "Russell Wallace" <russell.wallace@gmail.com>
To: "Hal Finkel" <hfinkel@anl.gov>
Cc: "llvm-dev" <llvm-dev@lists.llvm.org>
Sent: Sunday, September 20, 2015 8:15:19 AM
Subject: Re: [llvm-dev] How to invoke simplifycfg from code

Okay, as far as I can see looking through the output of that, the
empty block isn't created near the end, it exists at the start and
never goes away.

But running SimplifyCFG has a stand-alone pass removes it? That seems like a bug.

-Hal

Yes, that is what I was thinking. Should I report it as a bug?

From: "Russell Wallace" <russell.wallace@gmail.com>
To: "Hal Finkel" <hfinkel@anl.gov>
Cc: "llvm-dev" <llvm-dev@lists.llvm.org>
Sent: Sunday, September 20, 2015 8:34:07 AM
Subject: Re: [llvm-dev] How to invoke simplifycfg from code

Yes, that is what I was thinking. Should I report it as a bug?

Please do. If you can attach the relevant IR it will be easier for us to figure out what is going on.

-Hal

Done.

https://llvm.org/bugs/show_bug.cgi?id=24885