authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-08 02:09:26-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-08 02:09:26-05:00
logd4a93dbac5b38bda79dc26872fcb87980ab4d272
tree0810382264abd8a46af275f9fe4cd981be99bffd
parent7d0fb281fee16d9c99f61c5bce090018228ae6df

IR: omit debug safety checks in for loop codegen


4 files changed, 34 insertions(+), 24 deletions(-)

src/all_types.hpp+1
......@@ -1523,6 +1523,7 @@ struct IrInstructionBinOp {
15231523 IrInstruction *op1;
15241524 IrBinOp op_id;
15251525 IrInstruction *op2;
1526 bool safety_check_on;
15261527};
15271528
15281529struct IrInstructionDeclVar {
src/codegen.cpp+8-6
......@@ -812,6 +812,9 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
812812
813813 assert(op1->type_entry == op2->type_entry);
814814
815 bool want_debug_safety = bin_op_instruction->safety_check_on &&
816 ir_want_debug_safety(g, &bin_op_instruction->base);
817
815818 LLVMValueRef op1_value = ir_llvm_value(g, op1);
816819 LLVMValueRef op2_value = ir_llvm_value(g, op2);
817820 switch (op_id) {
......@@ -859,7 +862,7 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
859862 bool is_wrapping = (op_id == IrBinOpAddWrap);
860863 if (is_wrapping) {
861864 return LLVMBuildAdd(g->builder, op1_value, op2_value, "");
862 } else if (ir_want_debug_safety(g, &bin_op_instruction->base)) {
865 } else if (want_debug_safety) {
863866 return gen_overflow_op(g, op1->type_entry, AddSubMulAdd, op1_value, op2_value);
864867 } else if (op1->type_entry->data.integral.is_signed) {
865868 return LLVMBuildNSWAdd(g->builder, op1_value, op2_value, "");
......@@ -882,7 +885,7 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
882885 bool is_wrapping = (op_id == IrBinOpBitShiftLeftWrap);
883886 if (is_wrapping) {
884887 return LLVMBuildShl(g->builder, op1_value, op2_value, "");
885 } else if (ir_want_debug_safety(g, &bin_op_instruction->base)) {
888 } else if (want_debug_safety) {
886889 return gen_overflow_shl_op(g, op1->type_entry, op1_value, op2_value);
887890 } else if (op1->type_entry->data.integral.is_signed) {
888891 return ZigLLVMBuildNSWShl(g->builder, op1_value, op2_value, "");
......@@ -905,7 +908,7 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
905908 bool is_wrapping = (op_id == IrBinOpSubWrap);
906909 if (is_wrapping) {
907910 return LLVMBuildSub(g->builder, op1_value, op2_value, "");
908 } else if (ir_want_debug_safety(g, &bin_op_instruction->base)) {
911 } else if (want_debug_safety) {
909912 return gen_overflow_op(g, op1->type_entry, AddSubMulSub, op1_value, op2_value);
910913 } else if (op1->type_entry->data.integral.is_signed) {
911914 return LLVMBuildNSWSub(g->builder, op1_value, op2_value, "");
......@@ -923,7 +926,7 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
923926 bool is_wrapping = (op_id == IrBinOpMultWrap);
924927 if (is_wrapping) {
925928 return LLVMBuildMul(g->builder, op1_value, op2_value, "");
926 } else if (ir_want_debug_safety(g, &bin_op_instruction->base)) {
929 } else if (want_debug_safety) {
927930 return gen_overflow_op(g, op1->type_entry, AddSubMulMul, op1_value, op2_value);
928931 } else if (op1->type_entry->data.integral.is_signed) {
929932 return LLVMBuildNSWMul(g->builder, op1_value, op2_value, "");
......@@ -934,8 +937,7 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
934937 zig_unreachable();
935938 }
936939 case IrBinOpDiv:
937 return gen_div(g, ir_want_debug_safety(g, &bin_op_instruction->base),
938 op1_value, op2_value, op1->type_entry, false);
940 return gen_div(g, want_debug_safety, op1_value, op2_value, op1->type_entry, false);
939941 case IrBinOpMod:
940942 if (op1->type_entry->id == TypeTableEntryIdFloat) {
941943 return LLVMBuildFRem(g->builder, op1_value, op2_value, "");
src/ir.cpp+22-18
......@@ -567,12 +567,13 @@ static IrInstruction *ir_build_const_c_str_lit(IrBuilder *irb, Scope *scope, Ast
567567}
568568
569569static IrInstruction *ir_build_bin_op(IrBuilder *irb, Scope *scope, AstNode *source_node, IrBinOp op_id,
570 IrInstruction *op1, IrInstruction *op2)
570 IrInstruction *op1, IrInstruction *op2, bool safety_check_on)
571571{
572572 IrInstructionBinOp *bin_op_instruction = ir_build_instruction<IrInstructionBinOp>(irb, scope, source_node);
573573 bin_op_instruction->op_id = op_id;
574574 bin_op_instruction->op1 = op1;
575575 bin_op_instruction->op2 = op2;
576 bin_op_instruction->safety_check_on = safety_check_on;
576577
577578 ir_ref_instruction(op1);
578579 ir_ref_instruction(op2);
......@@ -581,10 +582,10 @@ static IrInstruction *ir_build_bin_op(IrBuilder *irb, Scope *scope, AstNode *sou
581582}
582583
583584static IrInstruction *ir_build_bin_op_from(IrBuilder *irb, IrInstruction *old_instruction, IrBinOp op_id,
584 IrInstruction *op1, IrInstruction *op2)
585 IrInstruction *op1, IrInstruction *op2, bool safety_check_on)
585586{
586587 IrInstruction *new_instruction = ir_build_bin_op(irb, old_instruction->scope,
587 old_instruction->source_node, op_id, op1, op2);
588 old_instruction->source_node, op_id, op1, op2, safety_check_on);
588589 ir_link_new_instruction(new_instruction, old_instruction);
589590 return new_instruction;
590591}
......@@ -1455,7 +1456,7 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
14551456static IrInstruction *ir_gen_bin_op_id(IrBuilder *irb, Scope *scope, AstNode *node, IrBinOp op_id) {
14561457 IrInstruction *op1 = ir_gen_node(irb, node->data.bin_op_expr.op1, scope);
14571458 IrInstruction *op2 = ir_gen_node(irb, node->data.bin_op_expr.op2, scope);
1458 return ir_build_bin_op(irb, scope, node, op_id, op1, op2);
1459 return ir_build_bin_op(irb, scope, node, op_id, op1, op2, true);
14591460}
14601461
14611462static IrInstruction *ir_gen_assign(IrBuilder *irb, Scope *scope, AstNode *node) {
......@@ -1479,7 +1480,7 @@ static IrInstruction *ir_gen_assign_op(IrBuilder *irb, Scope *scope, AstNode *no
14791480 IrInstruction *op2 = ir_gen_node(irb, node->data.bin_op_expr.op2, scope);
14801481 if (op2 == irb->codegen->invalid_instruction)
14811482 return op2;
1482 IrInstruction *result = ir_build_bin_op(irb, scope, node, op_id, op1, op2);
1483 IrInstruction *result = ir_build_bin_op(irb, scope, node, op_id, op1, op2, true);
14831484 ir_build_store_ptr(irb, scope, node, lvalue, result);
14841485 return ir_build_const_void(irb, scope, node);
14851486}
......@@ -2322,11 +2323,11 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
23222323
23232324 ir_set_cursor_at_end(irb, cond_block);
23242325 IrInstruction *index_val = ir_build_load_ptr(irb, child_scope, node, index_ptr);
2325 IrInstruction *cond = ir_build_bin_op(irb, child_scope, node, IrBinOpCmpLessThan, index_val, len_val);
2326 IrInstruction *cond = ir_build_bin_op(irb, child_scope, node, IrBinOpCmpLessThan, index_val, len_val, false);
23262327 ir_build_cond_br(irb, child_scope, node, cond, body_block, end_block, is_inline);
23272328
23282329 ir_set_cursor_at_end(irb, body_block);
2329 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, child_scope, node, array_val_ptr, index_val, true);
2330 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, child_scope, node, array_val_ptr, index_val, false);
23302331 IrInstruction *elem_val;
23312332 if (node->data.for_expr.elem_is_ptr) {
23322333 elem_val = elem_ptr;
......@@ -2345,7 +2346,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
23452346 ir_build_br(irb, child_scope, node, continue_block, is_inline);
23462347
23472348 ir_set_cursor_at_end(irb, continue_block);
2348 IrInstruction *new_index_val = ir_build_bin_op(irb, child_scope, node, IrBinOpAdd, index_val, one);
2349 IrInstruction *new_index_val = ir_build_bin_op(irb, child_scope, node, IrBinOpAdd, index_val, one, false);
23492350 ir_build_store_ptr(irb, child_scope, node, index_ptr, new_index_val);
23502351 ir_build_br(irb, child_scope, node, cond_block, is_inline);
23512352
......@@ -2654,13 +2655,13 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
26542655 IrInstruction *end_value_const = ir_build_static_eval(irb, scope, start_node, end_value);
26552656
26562657 IrInstruction *lower_range_ok = ir_build_bin_op(irb, scope, item_node, IrBinOpCmpGreaterOrEq,
2657 target_value, start_value_const);
2658 target_value, start_value_const, false);
26582659 IrInstruction *upper_range_ok = ir_build_bin_op(irb, scope, item_node, IrBinOpCmpLessOrEq,
2659 target_value, end_value_const);
2660 target_value, end_value_const, false);
26602661 IrInstruction *both_ok = ir_build_bin_op(irb, scope, item_node, IrBinOpBoolAnd,
2661 lower_range_ok, upper_range_ok);
2662 lower_range_ok, upper_range_ok, false);
26622663 if (ok_bit) {
2663 ok_bit = ir_build_bin_op(irb, scope, item_node, IrBinOpBoolOr, both_ok, ok_bit);
2664 ok_bit = ir_build_bin_op(irb, scope, item_node, IrBinOpBoolOr, both_ok, ok_bit, false);
26642665 } else {
26652666 ok_bit = both_ok;
26662667 }
......@@ -2670,9 +2671,9 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
26702671 return irb->codegen->invalid_instruction;
26712672
26722673 IrInstruction *cmp_ok = ir_build_bin_op(irb, scope, item_node, IrBinOpCmpEq,
2673 item_value, target_value);
2674 item_value, target_value, false);
26742675 if (ok_bit) {
2675 ok_bit = ir_build_bin_op(irb, scope, item_node, IrBinOpBoolOr, cmp_ok, ok_bit);
2676 ok_bit = ir_build_bin_op(irb, scope, item_node, IrBinOpBoolOr, cmp_ok, ok_bit, false);
26762677 } else {
26772678 ok_bit = cmp_ok;
26782679 }
......@@ -4027,7 +4028,8 @@ static TypeTableEntry *ir_analyze_bin_op_bool(IrAnalyze *ira, IrInstructionBinOp
40274028 return bool_type;
40284029 }
40294030
4030 ir_build_bin_op_from(&ira->new_irb, &bin_op_instruction->base, bin_op_instruction->op_id, casted_op1, casted_op2);
4031 ir_build_bin_op_from(&ira->new_irb, &bin_op_instruction->base, bin_op_instruction->op_id,
4032 casted_op1, casted_op2, bin_op_instruction->safety_check_on);
40314033 return bool_type;
40324034}
40334035
......@@ -4145,7 +4147,8 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
41454147 return ira->codegen->builtin_types.entry_bool;
41464148 }
41474149
4148 ir_build_bin_op_from(&ira->new_irb, &bin_op_instruction->base, op_id, casted_op1, casted_op2);
4150 ir_build_bin_op_from(&ira->new_irb, &bin_op_instruction->base, op_id,
4151 casted_op1, casted_op2, bin_op_instruction->safety_check_on);
41494152
41504153 return ira->codegen->builtin_types.entry_bool;
41514154}
......@@ -4311,7 +4314,8 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
43114314
43124315 }
43134316
4314 ir_build_bin_op_from(&ira->new_irb, &bin_op_instruction->base, op_id, casted_op1, casted_op2);
4317 ir_build_bin_op_from(&ira->new_irb, &bin_op_instruction->base, op_id,
4318 casted_op1, casted_op2, bin_op_instruction->safety_check_on);
43154319 return resolved_type;
43164320}
43174321
......@@ -5345,7 +5349,7 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
53455349 if (casted_elem_index == ira->codegen->invalid_instruction)
53465350 return ira->codegen->builtin_types.entry_invalid;
53475351
5348 bool safety_check_on = true;
5352 bool safety_check_on = elem_ptr_instruction->safety_check_on;
53495353 if (casted_elem_index->static_value.special != ConstValSpecialRuntime) {
53505354 uint64_t index = casted_elem_index->static_value.data.x_bignum.data.x_uint;
53515355 if (array_type->id == TypeTableEntryIdArray) {
src/ir_print.cpp+3
......@@ -314,6 +314,9 @@ static void ir_print_bin_op(IrPrint *irp, IrInstructionBinOp *bin_op_instruction
314314 ir_print_other_instruction(irp, bin_op_instruction->op1);
315315 fprintf(irp->f, " %s ", ir_bin_op_id_str(bin_op_instruction->op_id));
316316 ir_print_other_instruction(irp, bin_op_instruction->op2);
317 if (!bin_op_instruction->safety_check_on) {
318 fprintf(irp->f, " // no safety");
319 }
317320}
318321
319322static void ir_print_decl_var(IrPrint *irp, IrInstructionDeclVar *decl_var_instruction) {