LLVM traverse CFG
llvm
Solution
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
Problem
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 !