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.<div><br></div><div>1. In the cpp file, put the initilize pass macros:<div><br></div><div>INITIALIZE_PASS_BEGIN(MyPassName, "a-short-space-free-description",</div>
<div>  "A long description", true, true)</div><div>INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)</div><div>INITIALIZE_PASS_END(MyPassName, "a-short-space-free-description",</div><div>  "A long description", true, true)</div>
<div><br></div><div>2. Create the class with the pass dependencies noted in the constructor. </div><div><br></div><div>class MyPassName: public MachineFunctionPass {</div><div>public:</div><div>  static char ID;</div><div>
  MyPassName() {<div>    llvm::initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());</div>  }</div><div>  virtual bool runOnMachineFunction(MachineFunction &mf);</div><div>  virtual void getAnalysisUsage(AnalysisUsage &AU) const;</div>
<div>  virtual void releaseMemory();</div><div>};</div><div>char CodegenPass ::ID = 0;</div><div><br></div><div>3. 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.</div>
<div>"void initializeMyPassNamePass(PassRegistry &Registry);"</div><div><br></div><div>4. 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. </div>
<div><br></div><div>5. 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.</div>
<div><br></div><div>6. 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.</div>
<div><br></div><div>7. 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. </div>
<div><br></div><div>- Writing a codegen pass by,</div><div>Jeff Kunkel</div><div><br><div class="gmail_quote">On Mon, Jan 17, 2011 at 11:44 PM, Qingan Li <span dir="ltr"><<a href="mailto:ww345ww@gmail.com">ww345ww@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><span style="font-family:arial, sans-serif;font-size:13px;border-collapse:collapse">Thanks for your last reply.<br>Could I understand the way to <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">adding</span> <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">a</span> <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">pass</span> (built into the llvm rather than<br>

dynamic loadable) includes:<br>1. Declaring <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">a</span> creator function for this <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">pass</span><br>

2. Implementing the creator function for this <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">pass</span><br>
3. Instantiating this <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">pass</span> and get <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">a</span> object of it<br>

3. Register this <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">pass</span> into the PassRegistry<br>
<br>Then, for <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">a</span> built-into bytecode <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">pass</span>,<br>

        task 1(declaration of the creator) should be done in Scalar.h;<br>        task 2(implementation of the creator) should be done the related<br>mypass.cpp file;<br>        task 3(instantiation of the <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">pass</span> class) should be done in<br>

LinkAllPasses.h;<br>        task 4(registration of the <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">pass</span> into the PassRegistry) should be<br>
done by INITIALIZE_PASS<br>class LiveVariables : public MachineFunctionPass is <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">a</span> case of point.<br>
<br>For <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">a</span> built-into codegen/MachineCode <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">pass</span>,<br>

        task 1 should be done in Passes.h;<br>        task 2 should be done in the related mypass.cpp file;<br>        task 3 should be done in LLVMTargetMachine.cpp<br>        task 4 should be done by INITIALIZE_PASS<br>

 class IntervalAnalylsis: public MachineFunctionPass is <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">a</span> case in point.<br>
<br>I have implemented <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">a</span> new mypass (just for analysis, rather than<br>
transformation) as <span style="background-color:rgb(255, 255, 136);color:rgb(34, 34, 34);background-repeat:initial initial">a</span> subclass of MchineFunctionPass, and finished task 1, 2,<br>
4. But I don't know how to finish task 3, since I failed to make clear how<br>the class IntervalAnalysis did task 3. So I need your help.<br></span><br>-- <br>Best regards,<div><br></div><font color="#888888"><div>Li Qingan</div>
<br>
</font><br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><br></div></div>