To improve code readability of LegUp before each release we try to refactor in the following manner.
First, run the following (sudo apt-get install pmccabe):
cd llvm/lib/Target/Verilog pmccabe *.cpp | sort -n
Note that you should run pmccabe in each directory you have added code, not just the directory shown above. You will see output like the following:
.... 19 19 88 3244 201 GenerateRTL.cpp(3244): legup::GenerateRTL::findAllPipelineStageRegisters 23 23 88 7640 196 GenerateRTL.cpp(7640): legup::GenerateRTL::create_multicycle_paths_to_signal
The pmccabe tool calculates a number representing the complexity of each function. In this case the create_multicycle_paths_to_signal() has a complexity of 23 (the first number in the output). That number is calculated roughly: add +1 for each “for”, “if”, “switch”, “&&”, or “||” in the function. Basically it's a rough measure of the number of “decisions” per function.
For LegUp you should refactor or split up any function with a complexity of 10 or more. We have found that any function with 10 or more decisions typically is too hard to read.
The goal here isn't just to blindly split up the functions to reduce the number. But actually think about what the function is doing and try to intelligently split it up into pieces. Usually as you do this the function becomes much simpler and easier to read. Another important point: do not try to change any functionality while refactoring. The code should be equivalent before and after refactoring. Refactoring is only for making code easier to read and you should be careful not to introduce any bugs during this process.