4

I want to apply a DFS traversing algorithm on a CFG of a function. Therefore, I need the internal representation of the CFG. I need oriented edges and spotted MachineBasicBlock::const_succ_iterator. It is there a way to get the CFG with oriented edges by using a FunctionPass, instead of a MachineFunctionPass? The reason why I want this is that I have problems using MachineFunctionPass. I have written several complex passes till now, but I cannot run a MachineFunctionPass pass.

I found that : "A MachineFunctionPass is a part of the LLVM code generator that executes on the machine-dependent representation of each LLVM function in the program. 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?

When I was trying to run with opt a simple MachineFunctionPass, I got 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, so indeed it might be an opt problem.

I have started another pass for CallGraph. I am using CallGraph &CG = getAnalysis<CallGraph>(); efficiently. It is a similar way of getting CFG-s? What I found till now are succ_iterator/succ_begin/succ_end which are from CFG.h, but I think I still have to get the CFG analysis somehow.

Thank you in advance !

Alex
  • 340
  • 4
  • 17
  • How `MachineBasicBlock` is relevant to an IR? And what else do you need besides succ_iterator or pred_iterator? IR is already a CFG. – SK-logic Mar 15 '13 at 15:26
  • Ok. So I can apply directly succ_iterator? I don't need to get somehow the CFG analysis, the internal representation of CFG? If it is already there, I will try to use it and update my question (I had some problems trying this approach). – Alex Mar 15 '13 at 15:35
  • No, you don't need a separate analysis pass to get CFG. There are dedicated passes for obtaining DominatorTree, DominanceFrontier, etc., but CFG is already there by design. If you want examples on how to iterate over CFG, take a look at, say, `DominanceFrontier.cpp`. – SK-logic Mar 15 '13 at 16:08
  • Thank you for the DominanceFrontier.cpp example! – Alex Mar 18 '13 at 14:30

1 Answers1

3

I think you may have some terms mixed up. Basic blocks within each function are already arranged in a kind-of CFG, and LLVM provides you the tools to traverse that. See my answer to this question, for example.

MachineFunction lives on a different level, and unless you're doing something very special, this is not the level you should operate on. It's too low-level, and too target specific. There's some overview of the levels here

Community
  • 1
  • 1
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412