llvm_backend_opt.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. void lb_populate_function_pass_manager(LLVMPassManagerRef fpm, bool ignore_memcpy_pass, i32 optimization_level) {
  2. // NOTE(bill): Treat -opt:3 as if it was -opt:2
  3. // TODO(bill): Determine which opt definitions should exist in the first place
  4. optimization_level = gb_clamp(optimization_level, 0, 2);
  5. if (!ignore_memcpy_pass) {
  6. LLVMAddMemCpyOptPass(fpm);
  7. }
  8. LLVMAddPromoteMemoryToRegisterPass(fpm);
  9. LLVMAddMergedLoadStoreMotionPass(fpm);
  10. LLVMAddConstantPropagationPass(fpm);
  11. LLVMAddEarlyCSEPass(fpm);
  12. LLVMAddConstantPropagationPass(fpm);
  13. LLVMAddMergedLoadStoreMotionPass(fpm);
  14. LLVMAddPromoteMemoryToRegisterPass(fpm);
  15. LLVMAddCFGSimplificationPass(fpm);
  16. // LLVMAddSLPVectorizePass(fpm);
  17. // LLVMAddLoopVectorizePass(fpm);
  18. // LLVMAddScalarizerPass(fpm);
  19. // LLVMAddLoopIdiomPass(fpm);
  20. if (optimization_level == 0) {
  21. return;
  22. }
  23. #if 1
  24. LLVMAddSCCPPass(fpm);
  25. LLVMAddPromoteMemoryToRegisterPass(fpm);
  26. LLVMAddUnifyFunctionExitNodesPass(fpm);
  27. LLVMAddCFGSimplificationPass(fpm);
  28. LLVMAddEarlyCSEPass(fpm);
  29. LLVMAddLowerExpectIntrinsicPass(fpm);
  30. #endif
  31. }
  32. void lb_add_function_simplifcation_passes(LLVMPassManagerRef mpm, i32 optimization_level) {
  33. LLVMAddEarlyCSEMemSSAPass(mpm);
  34. LLVMAddGVNPass(mpm);
  35. LLVMAddCFGSimplificationPass(mpm);
  36. LLVMAddJumpThreadingPass(mpm);
  37. // if (optimization_level > 2) {
  38. // LLVMAddAggressiveInstCombinerPass(mpm);
  39. // }
  40. LLVMAddInstructionCombiningPass(mpm);
  41. LLVMAddSimplifyLibCallsPass(mpm);
  42. LLVMAddTailCallEliminationPass(mpm);
  43. LLVMAddCFGSimplificationPass(mpm);
  44. LLVMAddReassociatePass(mpm);
  45. LLVMAddLoopRotatePass(mpm);
  46. LLVMAddLICMPass(mpm);
  47. LLVMAddLoopUnswitchPass(mpm);
  48. LLVMAddCFGSimplificationPass(mpm);
  49. LLVMAddInstructionCombiningPass(mpm);
  50. LLVMAddIndVarSimplifyPass(mpm);
  51. LLVMAddLoopIdiomPass(mpm);
  52. LLVMAddLoopDeletionPass(mpm);
  53. LLVMAddLoopUnrollPass(mpm);
  54. LLVMAddMergedLoadStoreMotionPass(mpm);
  55. LLVMAddGVNPass(mpm);
  56. LLVMAddMemCpyOptPass(mpm);
  57. LLVMAddSCCPPass(mpm);
  58. LLVMAddBitTrackingDCEPass(mpm);
  59. LLVMAddInstructionCombiningPass(mpm);
  60. LLVMAddJumpThreadingPass(mpm);
  61. LLVMAddCorrelatedValuePropagationPass(mpm);
  62. LLVMAddDeadStoreEliminationPass(mpm);
  63. LLVMAddLICMPass(mpm);
  64. LLVMAddLoopRerollPass(mpm);
  65. LLVMAddAggressiveDCEPass(mpm);
  66. LLVMAddCFGSimplificationPass(mpm);
  67. LLVMAddInstructionCombiningPass(mpm);
  68. }
  69. void lb_populate_module_pass_manager(LLVMTargetMachineRef target_machine, LLVMPassManagerRef mpm, i32 optimization_level) {
  70. // NOTE(bill): Treat -opt:3 as if it was -opt:2
  71. // TODO(bill): Determine which opt definitions should exist in the first place
  72. optimization_level = gb_clamp(optimization_level, 0, 2);
  73. if (optimization_level >= 2) {
  74. // NOTE(bill, 2021-03-29: use this causes invalid code generation)
  75. // LLVMPassManagerBuilderRef pmb = LLVMPassManagerBuilderCreate();
  76. // LLVMPassManagerBuilderPopulateModulePassManager(pmb, mpm);
  77. // LLVMPassManagerBuilderPopulateLTOPassManager(pmb, mpm, false, true);
  78. // LLVMPassManagerBuilderSetOptLevel(pmb, optimization_level);
  79. // LLVMPassManagerBuilderSetSizeLevel(pmb, optimization_level);
  80. }
  81. LLVMAddAlwaysInlinerPass(mpm);
  82. LLVMAddStripDeadPrototypesPass(mpm);
  83. LLVMAddAnalysisPasses(target_machine, mpm);
  84. LLVMAddPruneEHPass(mpm);
  85. if (optimization_level == 0) {
  86. return;
  87. }
  88. LLVMAddGlobalDCEPass(mpm);
  89. LLVMAddIPSCCPPass(mpm);
  90. LLVMAddCalledValuePropagationPass(mpm);
  91. LLVMAddGlobalOptimizerPass(mpm);
  92. LLVMAddDeadArgEliminationPass(mpm);
  93. // LLVMAddConstantMergePass(mpm); // ???
  94. LLVMAddInstructionCombiningPass(mpm);
  95. LLVMAddCFGSimplificationPass(mpm);
  96. LLVMAddPruneEHPass(mpm);
  97. if (optimization_level < 2) {
  98. return;
  99. }
  100. LLVMAddFunctionInliningPass(mpm);
  101. lb_add_function_simplifcation_passes(mpm, optimization_level);
  102. LLVMAddGlobalDCEPass(mpm);
  103. LLVMAddGlobalOptimizerPass(mpm);
  104. // LLVMAddLowerConstantIntrinsicsPass(mpm);
  105. LLVMAddLoopRotatePass(mpm);
  106. LLVMAddLoopVectorizePass(mpm);
  107. LLVMAddInstructionCombiningPass(mpm);
  108. if (optimization_level >= 2) {
  109. LLVMAddEarlyCSEPass(mpm);
  110. LLVMAddCorrelatedValuePropagationPass(mpm);
  111. LLVMAddLICMPass(mpm);
  112. LLVMAddLoopUnswitchPass(mpm);
  113. LLVMAddCFGSimplificationPass(mpm);
  114. LLVMAddInstructionCombiningPass(mpm);
  115. }
  116. LLVMAddCFGSimplificationPass(mpm);
  117. LLVMAddSLPVectorizePass(mpm);
  118. LLVMAddLICMPass(mpm);
  119. LLVMAddAlignmentFromAssumptionsPass(mpm);
  120. LLVMAddStripDeadPrototypesPass(mpm);
  121. if (optimization_level >= 2) {
  122. LLVMAddGlobalDCEPass(mpm);
  123. LLVMAddConstantMergePass(mpm);
  124. }
  125. LLVMAddCFGSimplificationPass(mpm);
  126. }