Browse Source

Move LLVM optimization procedures to a separate file to aid with organization

gingerBill 4 years ago
parent
commit
7f6a43f0af
2 changed files with 202 additions and 90 deletions
  1. 33 90
      src/llvm_backend.cpp
  2. 169 0
      src/llvm_backend_opt.cpp

+ 33 - 90
src/llvm_backend.cpp

@@ -1,5 +1,6 @@
 #include "llvm_backend.hpp"
 #include "llvm_abi.cpp"
+#include "llvm_backend_opt.cpp"
 
 gb_global lbAddr lb_global_type_info_data           = {};
 gb_global lbAddr lb_global_type_info_member_types   = {};
@@ -12832,7 +12833,6 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da
 	}
 }
 
-
 void lb_generate_code(lbGenerator *gen) {
 	#define TIME_SECTION(str) do { if (build_context.show_more_timings) timings_start_section(&global_timings, str_lit(str)); } while (0)
 
@@ -13160,6 +13160,34 @@ void lb_generate_code(lbGenerator *gen) {
 
 		lb_add_entity(m, e, g);
 		lb_add_member(m, name, g);
+
+		if (m->debug_builder) {
+			String global_name = e->token.string;
+			if (global_name.len != 0 && global_name != "_") {
+				LLVMMetadataRef llvm_file = lb_get_llvm_metadata(m, e->file);
+				LLVMMetadataRef llvm_scope = llvm_file;
+
+				LLVMBool local_to_unit = e->flags & EntityFlag_Static;
+
+				LLVMMetadataRef llvm_expr = LLVMDIBuilderCreateExpression(m->debug_builder, nullptr, 0);
+				LLVMMetadataRef llvm_decl = nullptr;
+
+				u32 align_in_bits = cast(u32)(8*type_align_of(e->type));
+
+
+				LLVMMetadataRef global_variable_metadata = LLVMDIBuilderCreateGlobalVariableExpression(
+					m->debug_builder, llvm_scope,
+					cast(char const *)global_name.text, global_name.len,
+					"", 0, // linkage
+					llvm_file, e->token.pos.line,
+					lb_debug_type(m, e->type),
+					local_to_unit,
+					llvm_expr,
+					llvm_decl,
+					align_in_bits
+				);
+			}
+		}
 	}
 
 
@@ -13227,85 +13255,14 @@ void lb_generate_code(lbGenerator *gen) {
 	defer (LLVMDisposePassManager(default_function_pass_manager));
 
 	LLVMInitializeFunctionPassManager(default_function_pass_manager);
-	{
-		auto dfpm = default_function_pass_manager;
-
-		if (build_context.optimization_level == 0) {
-			LLVMAddMemCpyOptPass(dfpm);
-			LLVMAddPromoteMemoryToRegisterPass(dfpm);
-			LLVMAddMergedLoadStoreMotionPass(dfpm);
-			LLVMAddEarlyCSEPass(dfpm);
-			LLVMAddEarlyCSEMemSSAPass(dfpm);
-			LLVMAddConstantPropagationPass(dfpm);
-			// LLVMAddAggressiveDCEPass(dfpm);
-			LLVMAddMergedLoadStoreMotionPass(dfpm);
-			LLVMAddPromoteMemoryToRegisterPass(dfpm);
-			LLVMAddCFGSimplificationPass(dfpm);
-
-
-			// LLVMAddInstructionCombiningPass(dfpm);
-			LLVMAddSLPVectorizePass(dfpm);
-			LLVMAddLoopVectorizePass(dfpm);
-
-			LLVMAddScalarizerPass(dfpm);
-			LLVMAddLoopIdiomPass(dfpm);
-		} else {
-			bool do_extra_passes = true;
-
-			int repeat_count = cast(int)build_context.optimization_level;
-			do {
-				LLVMAddMemCpyOptPass(dfpm);
-				LLVMAddPromoteMemoryToRegisterPass(dfpm);
-				LLVMAddMergedLoadStoreMotionPass(dfpm);
-				LLVMAddAlignmentFromAssumptionsPass(dfpm);
-				LLVMAddEarlyCSEPass(dfpm);
-				LLVMAddEarlyCSEMemSSAPass(dfpm);
-				LLVMAddConstantPropagationPass(dfpm);
-				if (do_extra_passes) {
-					// LLVMAddAggressiveDCEPass(dfpm);
-				}
-				LLVMAddMergedLoadStoreMotionPass(dfpm);
-				LLVMAddPromoteMemoryToRegisterPass(dfpm);
-				LLVMAddCFGSimplificationPass(dfpm);
-				LLVMAddTailCallEliminationPass(dfpm);
-
-				if (do_extra_passes) {
-					LLVMAddSLPVectorizePass(dfpm);
-					LLVMAddLoopVectorizePass(dfpm);
-
-					LLVMAddConstantPropagationPass(dfpm);
-					LLVMAddScalarizerPass(dfpm);
-					LLVMAddLoopIdiomPass(dfpm);
-
-					// LLVMAddAggressiveInstCombinerPass(dfpm);
-					LLVMAddLowerExpectIntrinsicPass(dfpm);
-
-					LLVMAddDeadStoreEliminationPass(dfpm);
-					LLVMAddReassociatePass(dfpm);
-					LLVMAddAddDiscriminatorsPass(dfpm);
-					LLVMAddPromoteMemoryToRegisterPass(dfpm);
-					LLVMAddCorrelatedValuePropagationPass(dfpm);
-				}
-			} while (repeat_count --> 0);
-		}
-	}
+	lb_populate_function_pass_manager(default_function_pass_manager, false);
 	LLVMFinalizeFunctionPassManager(default_function_pass_manager);
 
 
 	LLVMPassManagerRef default_function_pass_manager_without_memcpy = LLVMCreateFunctionPassManagerForModule(mod);
 	defer (LLVMDisposePassManager(default_function_pass_manager_without_memcpy));
 	LLVMInitializeFunctionPassManager(default_function_pass_manager_without_memcpy);
-	{
-		auto dfpm = default_function_pass_manager_without_memcpy;
-		LLVMAddPromoteMemoryToRegisterPass(dfpm);
-		LLVMAddMergedLoadStoreMotionPass(dfpm);
-		LLVMAddUnifyFunctionExitNodesPass(dfpm);
-		LLVMAddConstantPropagationPass(dfpm);
-		// LLVMAddAggressiveDCEPass(dfpm);
-		LLVMAddMergedLoadStoreMotionPass(dfpm);
-		LLVMAddPromoteMemoryToRegisterPass(dfpm);
-		LLVMAddCFGSimplificationPass(dfpm);
-	}
+	lb_populate_function_pass_manager(default_function_pass_manager_without_memcpy, true);
 	LLVMFinalizeFunctionPassManager(default_function_pass_manager_without_memcpy);
 
 	TIME_SECTION("LLVM Runtime Type Information Creation");
@@ -13630,22 +13587,8 @@ void lb_generate_code(lbGenerator *gen) {
 
 	LLVMPassManagerRef module_pass_manager = LLVMCreatePassManager();
 	defer (LLVMDisposePassManager(module_pass_manager));
-	LLVMAddAlwaysInlinerPass(module_pass_manager);
-	LLVMAddStripDeadPrototypesPass(module_pass_manager);
-	LLVMAddAnalysisPasses(target_machine, module_pass_manager);
-	if (build_context.optimization_level >= 2) {
-		LLVMAddArgumentPromotionPass(module_pass_manager);
-		LLVMAddConstantMergePass(module_pass_manager);
-		LLVMAddGlobalDCEPass(module_pass_manager);
-		LLVMAddDeadArgEliminationPass(module_pass_manager);
-	}
-
-	LLVMPassManagerBuilderRef pass_manager_builder = LLVMPassManagerBuilderCreate();
-	defer (LLVMPassManagerBuilderDispose(pass_manager_builder));
-	LLVMPassManagerBuilderSetOptLevel(pass_manager_builder, build_context.optimization_level);
-	LLVMPassManagerBuilderSetSizeLevel(pass_manager_builder, build_context.optimization_level);
-
-	LLVMPassManagerBuilderPopulateLTOPassManager(pass_manager_builder, module_pass_manager, false, false);
+	lb_populate_module_pass_manager(target_machine, module_pass_manager);
+
 	LLVMRunPassManager(module_pass_manager, mod);
 
 	llvm_error = nullptr;

+ 169 - 0
src/llvm_backend_opt.cpp

@@ -0,0 +1,169 @@
+void lb_populate_function_pass_manager(LLVMPassManagerRef fpm, bool ignore_memcpy_pass) {
+	if (!ignore_memcpy_pass) {
+		LLVMAddMemCpyOptPass(fpm);
+	}
+	if (build_context.optimization_level == 0) {
+		LLVMAddPromoteMemoryToRegisterPass(fpm);
+		LLVMAddMergedLoadStoreMotionPass(fpm);
+		LLVMAddEarlyCSEPass(fpm);
+		// LLVMAddEarlyCSEMemSSAPass(fpm);
+		LLVMAddConstantPropagationPass(fpm);
+		LLVMAddMergedLoadStoreMotionPass(fpm);
+		LLVMAddPromoteMemoryToRegisterPass(fpm);
+		LLVMAddCFGSimplificationPass(fpm);
+
+		// LLVMAddSLPVectorizePass(fpm);
+		// LLVMAddLoopVectorizePass(fpm);
+
+		// LLVMAddScalarizerPass(fpm);
+		// LLVMAddLoopIdiomPass(fpm);
+		return;
+	}
+
+	LLVMAddSCCPPass(fpm);
+
+	LLVMAddPromoteMemoryToRegisterPass(fpm);
+	LLVMAddUnifyFunctionExitNodesPass(fpm);
+
+	LLVMAddCFGSimplificationPass(fpm);
+	LLVMAddScalarReplAggregatesPass(fpm);
+	LLVMAddEarlyCSEPass(fpm);
+	LLVMAddLowerExpectIntrinsicPass(fpm);
+}
+
+void lb_add_function_simplifcation_passes(LLVMPassManagerRef mpm) {
+	LLVMAddScalarReplAggregatesPass(mpm);
+	LLVMAddEarlyCSEMemSSAPass(mpm);
+
+	LLVMAddGVNPass(mpm);
+	LLVMAddCFGSimplificationPass(mpm);
+
+	LLVMAddJumpThreadingPass(mpm);
+
+	if (build_context.optimization_level > 2) {
+		LLVMAddAggressiveInstCombinerPass(mpm);
+	}
+	LLVMAddInstructionCombiningPass(mpm);
+	LLVMAddSimplifyLibCallsPass(mpm);
+
+	LLVMAddTailCallEliminationPass(mpm);
+	LLVMAddCFGSimplificationPass(mpm);
+	LLVMAddReassociatePass(mpm);
+
+	LLVMAddLoopRotatePass(mpm);
+	LLVMAddLICMPass(mpm);
+	LLVMAddLoopUnswitchPass(mpm);
+
+	LLVMAddCFGSimplificationPass(mpm);
+	LLVMAddInstructionCombiningPass(mpm);
+	LLVMAddIndVarSimplifyPass(mpm);
+	LLVMAddLoopIdiomPass(mpm);
+	LLVMAddLoopDeletionPass(mpm);
+
+	LLVMAddLoopUnrollPass(mpm);
+
+	LLVMAddMergedLoadStoreMotionPass(mpm);
+
+	LLVMAddGVNPass(mpm);
+
+	LLVMAddMemCpyOptPass(mpm);
+	LLVMAddSCCPPass(mpm);
+
+	LLVMAddBitTrackingDCEPass(mpm);
+
+	LLVMAddInstructionCombiningPass(mpm);
+	LLVMAddJumpThreadingPass(mpm);
+	LLVMAddCorrelatedValuePropagationPass(mpm);
+	LLVMAddDeadStoreEliminationPass(mpm);
+	LLVMAddLICMPass(mpm);
+
+	LLVMAddLoopRerollPass(mpm);
+	LLVMAddAggressiveDCEPass(mpm);
+	LLVMAddCFGSimplificationPass(mpm);
+	LLVMAddInstructionCombiningPass(mpm);
+}
+
+
+void lb_populate_module_pass_manager(LLVMTargetMachineRef target_machine, LLVMPassManagerRef mpm) {
+	LLVMPassManagerBuilderRef pmb = LLVMPassManagerBuilderCreate();
+	LLVMPassManagerBuilderSetOptLevel(pmb, build_context.optimization_level);
+	LLVMPassManagerBuilderSetSizeLevel(pmb, build_context.optimization_level);
+
+	LLVMPassManagerBuilderPopulateLTOPassManager(pmb, mpm, false, true);
+
+	LLVMAddAlwaysInlinerPass(mpm);
+	LLVMAddStripDeadPrototypesPass(mpm);
+	LLVMAddAnalysisPasses(target_machine, mpm);
+	LLVMAddPruneEHPass(mpm);
+	if (build_context.optimization_level == 0) {
+		// LLVMAddMergeFunctionsPass(mpm);
+		return;
+	}
+
+	LLVMAddGlobalDCEPass(mpm);
+
+	LLVMAddIPSCCPPass(mpm);
+	LLVMAddCalledValuePropagationPass(mpm);
+
+	LLVMAddGlobalOptimizerPass(mpm);
+	LLVMAddDeadArgEliminationPass(mpm);
+
+	// LLVMAddConstantMergePass(mpm); // ???
+	LLVMAddInstructionCombiningPass(mpm);
+	LLVMAddCFGSimplificationPass(mpm);
+
+	LLVMAddPruneEHPass(mpm);
+	LLVMAddFunctionInliningPass(mpm);
+
+	if (build_context.optimization_level > 2) {
+		LLVMAddArgumentPromotionPass(mpm);
+	}
+	lb_add_function_simplifcation_passes(mpm);
+
+	LLVMAddGlobalOptimizerPass(mpm);
+
+	// LLVMAddLowerConstantIntrinsicsPass(mpm);
+
+	LLVMAddLoopRotatePass(mpm);
+
+	LLVMAddLoopVectorizePass(mpm);
+
+	LLVMAddInstructionCombiningPass(mpm);
+	if (build_context.optimization_level >= 2) {
+		LLVMAddEarlyCSEPass(mpm);
+		LLVMAddCorrelatedValuePropagationPass(mpm);
+		LLVMAddLICMPass(mpm);
+		LLVMAddLoopUnswitchPass(mpm);
+		LLVMAddCFGSimplificationPass(mpm);
+		LLVMAddInstructionCombiningPass(mpm);
+	}
+
+	LLVMAddCFGSimplificationPass(mpm);
+
+	LLVMAddSLPVectorizePass(mpm);
+	LLVMAddLICMPass(mpm);
+
+	LLVMAddAlignmentFromAssumptionsPass(mpm);
+
+	LLVMAddStripDeadPrototypesPass(mpm);
+
+	if (build_context.optimization_level >= 2) {
+		LLVMAddGlobalDCEPass(mpm);
+		LLVMAddConstantMergePass(mpm);
+	}
+
+	LLVMAddCFGSimplificationPass(mpm);
+
+
+	#if 0
+		LLVMAddAlwaysInlinerPass(mpm);
+		LLVMAddStripDeadPrototypesPass(mpm);
+		LLVMAddAnalysisPasses(target_machine, mpm);
+		if (build_context.optimization_level >= 2) {
+			LLVMAddArgumentPromotionPass(mpm);
+			LLVMAddConstantMergePass(mpm);
+			LLVMAddGlobalDCEPass(mpm);
+			LLVMAddDeadArgEliminationPass(mpm);
+		}
+	#endif
+}