adding a codegen pass into llvm

Thanks for your last reply.
Could I understand the way to adding a pass (built into the llvm rather than
dynamic loadable) includes:

  1. Declaring a creator function for this pass
  2. Implementing the creator function for this pass
  3. Instantiating this pass and get a object of it
  4. Register this pass into the PassRegistry

Then, for a built-into bytecode pass,
task 1(declaration of the creator) should be done in Scalar.h;
task 2(implementation of the creator) should be done the related
mypass.cpp file;
task 3(instantiation of the pass class) should be done in
task 4(registration of the pass into the PassRegistry) should be
class LiveVariables : public MachineFunctionPass is a case of point.

For a built-into codegen/MachineCode pass,
task 1 should be done in Passes.h;
task 2 should be done in the related mypass.cpp file;
task 3 should be done in LLVMTargetMachine.cpp
task 4 should be done by INITIALIZE_PASS
class IntervalAnalylsis: public MachineFunctionPass is a case in point.

I have implemented a new mypass (just for analysis, rather than
transformation) as a subclass of MchineFunctionPass, and finished task 1, 2,
4. But I don’t know how to finish task 3, since I failed to make clear how
the class IntervalAnalysis did task 3. So I need your help.

I have created a few codegen passes, and there are quite a few steps to them. Here is how I would sum up the steps.

  1. In the cpp file, put the initilize pass macros:

INITIALIZE_PASS_BEGIN(MyPassName, “a-short-space-free-description”,
“A long description”, true, true)
INITIALIZE_PASS_END(MyPassName, “a-short-space-free-description”,
“A long description”, true, true)

  1. Create the class with the pass dependencies noted in the constructor.

class MyPassName: public MachineFunctionPass {
static char ID;
MyPassName() {
virtual bool runOnMachineFunction(MachineFunction &mf);
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
virtual void releaseMemory();
char CodegenPass ::ID = 0;

  1. Create an initilization function for you pass in Passes.h, and please note the concatenation of the pass name in the center of initialize and Pass.
    “void initializeMyPassNamePass(PassRegistry &Registry);”

  2. If your class is not instantiated in a header, then place an “extern char & MyPassNameID;” into passes.h so other passes can find it’s dependencies.

  3. I am not 100% sure this is necessary, but I do it anyway to be sure. In CodeGen.cpp, place “initializeMyPassNamePass(Registry);” in the “initializeCodeGen” function. It may be possible to use opt, but I have not tired it.

  4. The “LinkAllPasses.h” is more important if you are overriding the Dag-Scheduler or Register allocator or some other important, ever present pass. For a regular pass, other passes will call the initializeMyPassNamePass, and other passes will use Analytical usage to run it.

  5. This is the hardest part. Find out where in this long chain of CodeGen dependencies you can place your pass. If you want to do a pass before register allocation, you may have to add some Analytical usage fields to the first reg-alloc pass which is really SlotIndexes. Perhaps you can put something in between other passes, but it’s difficult. The dependencies are such that I have found it difficult to place a simple analysis pass between some steps.

  • Writing a codegen pass by,
    Jeff Kunkel