for (MachineFunction::iterator bb = MF.begin(); bb != MF.end(); ++bb) { // If the basic block has a single predecessor, // then its parent is the same as the parent of the predecessor. if (bb->getPrevNode() && bb->getPrevNode()->getTerminator()->getNumSuccessors() == 1) { MachineBasicBlock* parent = bb->getParent(); // Do something with parent } }
MachineBasicBlock* bb = ...; // some basic block MachineFunction* mf = bb->getParent(); llvm::errs() << "Parent function: " << mf->getName() << "\n";In this example, we have a single basic block and we use the getParent function to access its parent (i.e. containing function). We then print the name of the containing function using the llvm::errs() stream.