Browse Source

Add minor optimization for `lb_map_cell_index_static`

gingerBill 2 years ago
parent
commit
0d37da54b4
2 changed files with 11 additions and 6 deletions
  1. 0 3
      src/llvm_backend.cpp
  2. 11 3
      src/llvm_backend_stmt.cpp

+ 0 - 3
src/llvm_backend.cpp

@@ -552,9 +552,6 @@ lbValue lb_map_get_proc_for_type(lbModule *m, Type *type) {
 	vs = lb_emit_conv(p, vs, alloc_type_pointer(type->Map.value));
 	hs = lb_emit_conv(p, hs, alloc_type_pointer(t_uintptr));
 
-	// lbValue res =
-	// LLVMBuildRet(p->builder, res.value);
-
 	lb_emit_jump(p, loop_block);
 	lb_start_block(p, loop_block);
 

+ 11 - 3
src/llvm_backend_stmt.cpp

@@ -382,12 +382,20 @@ lbValue lb_map_cell_index_static(lbProcedure *p, Type *type, lbValue cells_ptr,
 		return lb_emit_ptr_offset(p, elems_ptr, index);
 	}
 
-	// TOOD(bill): N power of two optimization to use >> and &
+	lbValue cell_index = {};
+	lbValue data_index = {};
 
 	lbValue size_const = lb_const_int(p->module, t_uintptr, size);
 	lbValue len_const = lb_const_int(p->module, t_uintptr, len);
-	lbValue cell_index = lb_emit_arith(p, Token_Quo, index, len_const, t_uintptr);
-	lbValue data_index = lb_emit_arith(p, Token_Mod, index, len_const, t_uintptr);
+
+	if (is_power_of_two(len)) {
+		u64 log2_len = floor_log2(cast(u64)len);
+		cell_index = log2_len == 0 ? index : lb_emit_arith(p, Token_Shr, index, lb_const_int(p->module, t_uintptr, log2_len), t_uintptr);
+		data_index = lb_emit_arith(p, Token_And, index, lb_const_int(p->module, t_uintptr, len-1), t_uintptr);
+	} else {
+		cell_index = lb_emit_arith(p, Token_Quo, index, len_const, t_uintptr);
+		data_index = lb_emit_arith(p, Token_Mod, index, len_const, t_uintptr);
+	}
 
 	lbValue elems_ptr = lb_emit_conv(p, cells_ptr, t_uintptr);
 	lbValue cell_offset = lb_emit_arith(p, Token_Mul, size_const, cell_index, t_uintptr);