Search code examples
clangllvmcompiler-optimizationllvm-clangllvm-ir

Modifying optimizations performed by Clang/LLVM compiler


The LLVM backend of the Clang/LLVM compiler runs various pass on the IR (intermediate representation) for optimisations.

  • How to know what all passes are being run under any of -O1/2/3 modes ?
  • Is there a way to remove some of these passes and add some other custom written pass ?
  • Also, are there any dependency between the passes that need to be taken care of ?

Solution

  • How to know what all passes are being run under any of -O1/2/3 modes

    All of the logic on related to optimization pipeline construction is concentrated in PassBuilder.cpp. There you can see direct conditions on optimization levels.

    Is there a way to remove some of these passes and add some other custom written pass

    Sure, just add it to that source file.

    Also, are there any dependency between the passes that need to be taken care of

    LLVM developers try to make all of the passes independent (and they are for the most part, as far as I know). If you want to use information from some pass, that pass should be registered as Analysis (i.e. a pass that does not transform the code, but gathers the info). You can read more about analysis dependency in here.

    I hope this answers your question!