LLVM is an excellent compiler that supports multi-source languages, multiple backgrounds, and the analysis, transformation, and optimization of the whole and entire life cycle of the program. LLVM Intermediate representation (intermediaterepresentation, referred to as IR) as the intermediate language of LLVM, has a very important effect on the support of various LLVM features. There are a lot of times we need to analyze Llvmir, LLVM official documentation: http://llvm.org/docs/ Writinganllvmpass.html, which introduces a basic step, and an introduction to some of the commonly used passes, where the examples are simple, simply print out the function's name. Therefore, in order to deepen the study of pass, the following will analyze two instances: 1, write a pass print module, Function, basicblock, variable values or some properties. 1) See the official documentation for all the steps, and create a new folder under the/llvm/lib/transforms directory (take Basicblockname as an example). Create two new files under the directory one is the BasicBlockName.cpp file that needs to be used, and the other one is makefile. You will also need to add the name of the newly created folder to the makefile inside of the/llvm/lib/transforms directory parallel_dirs after the file name. This allows you to edit the CPP file. 2) Add the following code to the CPP file: 1#include "llvm/pass.h" 2#include "llvm/ir/function.h" 3#include "llvm/ir/ Module.h " 4#include" llvm/ir/basicblock.h " 5#include" llvm/ir/instruction.h " 6#include" llvm/ir/ Instructions.h " 7#include" llvm/support/raw_ostream.h " 8 9 usingnamespace LLVM; 10 11namespace 12{ 13 struct Basicblockname:publicmodulepass 14 { 15 Static char ID; 16 basicblockname (): Modulepass (ID) {} 17 virtual boolrunonmodule (Module &m) & nbsp;18 { 19 for (Module::iterator mit=m.begin (), Mite=m.end (); Mit!=mite; mit++) 20 { 21 errs () << "Function:" <<Mit-> GetName () << "\ n"; 22 for (Llvm::function::iterator Fit =mit->begin (), Fite = Mit->end (); Fit!=fite; fit++) 23 { 24 llvm::basicblock* BB =fit; 25& nbsp errs () << "----Basicblock:" <<bb->getname () << "\ n"; 26 if (bb->size () >1) 27 { 28& nbsp for (Basicblock::iteratorbit=bb->begin (), Bite=bb->end (); Bit!=bite; bit++) 29 { 30 errs () << "--------" <<Bit-> Getoperand (0)->getname () << "| " 31 <<bit->getoperand (0)->getvaluename () < < "|" 32 <<bit->getoperand (0)->getvalueid () << "|" 33 <<bit->getoperand (0) Getnumuses () << "* * end\n"; 34 } 35 } 36 } 37 } 38 returnfalse; 39 } 40 }; 41} 42 43 charbasicblockname::id = 0; 44 staticregisterpass Y ("Basicblock", "Bbname", False,false); 3) Explanation of the code: the 13th line defines a baiscblockname structure that inherits from Modulepass, and 17 rows is the function that implements it, and 19, 22, 28 use a for loop to iterate over the IR pair for different levels, respectively.Like, the code getname, GetValue and other functions get the desired value and then print it out. 4) You need to make a click below the build folder of the compiled LLVM, you will generate the basicblockname.so file under Release+asserts/lib and then use the official documentation, Opt-load ***/ Basicblockname.so-basicblock TEST.BC >/dev/null so you can print out the corresponding name.
2. Remove an unused instruction from IR. 1) All the steps are consistent with the above, the source code is as follows: 1 #include "llvm/pass.h" 2 #include "llvm/ir/function.h" 3 #include "llvm/ir/mo Dule.h " 4 #include" llvm/ir/basicblock.h " 5 #include" llvm/ir/instruction.h " 6 #include" llvm/ir/inst Ructions.h " 7 #include" llvm/support/raw_ostream.h " 8 #include" llvm/transforms/utils/local.h " 9 10 using namespace LLVM; 11 12 namespace 13 { 14 structdeleteinstruction:public basicblockpass 15 &nbs P { 16 static char ID; 17 deleteinstruction (): Basicblockpass (ID) {} 18 & nbsp;19 Virtual Boolrunonbasicblock (Basicblock &bb) 20 { 21 errs () << "do Nothing "<<" \ n "; 22 bool change = false; 23 intsign = 0; 24 intcount = 0; 25 errs () << "initilized erasecount:" <<count<<"\ n"; 26 for (basicblock::iterator DI = Bb.begin ();D i!=bb.end ();) 27 { 28 & nbsp instruction *inst = di++; 29 if (Isinstructiontriviallydead (Inst)) 30 { 31 & nbsp; inst->erasefromparent (); 32 change = true; 33 } 34 sign++; 35 count++; 36 37 errs () << "before Erasecount:" <<count<< "\ n"; 38 count = 0; 39 for (Basicblock::iterator DI =bb.begin ();D i!=bb.end ();) 40 { 41 & nbsp DI++; 42 count++; 43 44 errs () << "after Erasecount:" <<count<< "\ n"; 45 return change; 46 return false; 47 } 48 }; 49} 50 51 chardeleteinstruction::id = 0; 52 staticregisterpass X ("Deleteinstruction", "DI", False,false); 2) Code interpretation: Because of the operation of the instruction, so only need to inherit Basicblockpass, 29 lines to the instruction will be used to judge, judging function from the 8th row of the header file, 31 rows Delete will delete the unused instructions, Since currently do not know how to save the modified to the disk, all in 39 new added a loop to the deletion of the instruction count, inconsistent before and after the deletion is successful. 3) Operation Result: do nothing initilized erase count:0 Before erase count:11 after erase count:10 It is important to note that variables are not initialized and used in the source program, and this is not the case.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.