LLVM provides a wide variety of optimization passes. LLVM allows a compiler implementation to decide which optimizations to use, their order, and so on. In this recipe, you will learn how to add IR optimization support.
Do the following steps:
static FunctionPassManager *Global_FP;
Module
object used previously. This can be done in the main()
function as follows:FunctionPassManager My_FP(TheModule);
main()
function as follows:My_FP.add(createBasicAliasAnalysisPass()); My_FP.add(createInstructionCombiningPass()); My_FP.add(createReassociatePass()); My_FP.add(createGVNPass()); My_FP.doInitialization();
Global_FP = &My_FP; Driver();
This PassManager has a run method, which we can run on the function IR generated before returning from Codegen()
of the function definition. This is demonstrated as follows:
Function* FunctionDefnAST::Codegen() { Named_Values.clear(); Function *TheFunction = Func_Decl->Codegen(); if (!TheFunction) return 0; BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction); Builder.SetInsertPoint(BB); if (Value* Return_Value = Body->Codegen()) { Builder.CreateRet(Return_Value); verifyFunction(*TheFunction); Global_FP->run(*TheFunction); returnTheFunction; } TheFunction->eraseFromParent(); return 0; }
This is a lot more beneficial as it optimizes the function in place, improving the code generated for the function body.
18.226.185.87