write a simple MachineFunctionPass

Hello everyone,

I have written several complex passes till now, but I cannot write a MachineFunctionPass pass. It just gives me segfault. Hence I reduced the pass to the following form :

using namespace llvm;

namespace {
class CFGexplorator : public MachineFunctionPass {
public:
static char ID; // Pass identification, replacement for typeid
CFGexplorator() : MachineFunctionPass(ID) {
//initializeMemDepPrinterPass(*PassRegistry::getPassRegistry());
}

private:
virtual bool runOnMachineFunction(MachineFunction &MF);

virtual void getAnalysisUsage(AnalysisUsage &AU) const {
MachineFunctionPass::getAnalysisUsage(AU);
}
};
} // end anonymous namespace

char CFGexplorator::ID = 0;
static RegisterPass Y(“mycfg”, “mycfg”, false, false);

bool CFGexplorator::runOnMachineFunction(MachineFunction &MF) {
return false;
}

I get the error :
Pass ‘mycfg’ is not initialized.
Verify if there is a pass dependency cycle.
Required Passes:
opt: PassManager.cpp:638: void llvm::PMTopLevelManager::schedulePass(llvm::Pass*): Assertion `PI && “Expected required passes to be initialized”’ failed.

So I have to initialize the pass. But in my all other passes I did not any initialization and I don’t want to use INITIALIZE_PASS since I have to recompile the llvm file that is keeping the pass registration… Is there a way to keep using static RegisterPass for a MachineFunctionPass ? I mention that if I change to FunctionPass, I have no problems and that is weird…

Thank you for any advice !

I found that : “Code generator passes are registered and initialized specially by TargetMachine::addPassesToEmitFile and similar routines, so they cannot generally be run from the opt or bugpoint commands.”…So how I can run a MachineFunctionPass? In the end, I just want to apply a DFS on a CFG of a function. And I need oriented edges and wanted to use MachineBasicBlock::const_succ_iterator. It is there a way to get the CFG with oriented edges by using a FunctionPass?

Thank you !

Hi Alexandru,

I don’t want to use INITIALIZE_PASS since I have to recompile the llvm file that is keeping the pass registration…

I would have encouraged you to use INITIALIZE_PASS to fix this. If you really don’t want to though, I think RegisterPass is still intended to work for MachineFunctionPasses, though this may no longer be well tested. Can you attach a skeleton pass that exhibits the problem you’re seeing?

Cheers,
Lang.