authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-14 12:38:32-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-20 12:19:16-07:00
log7bb2d13a090f700b3806127a639e164726af8e03
tree125fb82192e9922bb244a7cec947de76436a769c
parent3c5927fb87034affd6af56ecd5d9ae07fe23d690

stage2: remove ZIR instructions bool_and and bool_or

These were unused. I believe this happened with the introduction of bool_br_and and bool_br_or instructions.

3 files changed, 43 insertions(+), 96 deletions(-)

src/AstGen.zig-2
...@@ -1922,8 +1922,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: ast.Node.Index) Inner...@@ -1922,8 +1922,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: ast.Node.Index) Inner
1922 .bool_br_and,1922 .bool_br_and,
1923 .bool_br_or,1923 .bool_br_or,
1924 .bool_not,1924 .bool_not,
1925 .bool_and,
1926 .bool_or,
1927 .call_compile_time,1925 .call_compile_time,
1928 .call_nosuspend,1926 .call_nosuspend,
1929 .call_async,1927 .call_async,
src/Sema.zig+43-82
...@@ -185,8 +185,6 @@ pub fn analyzeBody(...@@ -185,8 +185,6 @@ pub fn analyzeBody(
185 //.block => try sema.zirBlock(block, inst),185 //.block => try sema.zirBlock(block, inst),
186 //.suspend_block => try sema.zirSuspendBlock(block, inst),186 //.suspend_block => try sema.zirSuspendBlock(block, inst),
187 //.bool_not => try sema.zirBoolNot(block, inst),187 //.bool_not => try sema.zirBoolNot(block, inst),
188 //.bool_and => try sema.zirBoolOp(block, inst, false),
189 //.bool_or => try sema.zirBoolOp(block, inst, true),
190 //.bool_br_and => try sema.zirBoolBr(block, inst, false),188 //.bool_br_and => try sema.zirBoolBr(block, inst, false),
191 //.bool_br_or => try sema.zirBoolBr(block, inst, true),189 //.bool_br_or => try sema.zirBoolBr(block, inst, true),
192 //.c_import => try sema.zirCImport(block, inst),190 //.c_import => try sema.zirCImport(block, inst),
...@@ -195,12 +193,12 @@ pub fn analyzeBody(...@@ -195,12 +193,12 @@ pub fn analyzeBody(
195 //.call_compile_time => try sema.zirCall(block, inst, .compile_time, false),193 //.call_compile_time => try sema.zirCall(block, inst, .compile_time, false),
196 //.call_nosuspend => try sema.zirCall(block, inst, .no_async, false),194 //.call_nosuspend => try sema.zirCall(block, inst, .no_async, false),
197 //.call_async => try sema.zirCall(block, inst, .async_kw, false),195 //.call_async => try sema.zirCall(block, inst, .async_kw, false),
198 //.cmp_eq => try sema.zirCmp(block, inst, .eq),196 .cmp_eq => try sema.zirCmp(block, inst, .eq),
199 //.cmp_gt => try sema.zirCmp(block, inst, .gt),197 .cmp_gt => try sema.zirCmp(block, inst, .gt),
200 //.cmp_gte => try sema.zirCmp(block, inst, .gte),198 .cmp_gte => try sema.zirCmp(block, inst, .gte),
201 //.cmp_lt => try sema.zirCmp(block, inst, .lt),199 .cmp_lt => try sema.zirCmp(block, inst, .lt),
202 //.cmp_lte => try sema.zirCmp(block, inst, .lte),200 .cmp_lte => try sema.zirCmp(block, inst, .lte),
203 //.cmp_neq => try sema.zirCmp(block, inst, .neq),201 .cmp_neq => try sema.zirCmp(block, inst, .neq),
204 //.coerce_result_ptr => try sema.zirCoerceResultPtr(block, inst),202 //.coerce_result_ptr => try sema.zirCoerceResultPtr(block, inst),
205 //.decl_ref => try sema.zirDeclRef(block, inst),203 //.decl_ref => try sema.zirDeclRef(block, inst),
206 //.decl_val => try sema.zirDeclVal(block, inst),204 //.decl_val => try sema.zirDeclVal(block, inst),
...@@ -4669,8 +4667,8 @@ fn zirBitwise(...@@ -4669,8 +4667,8 @@ fn zirBitwise(
4669 return sema.mod.fail(&block.base, src, "invalid operands to binary bitwise expression: '{s}' and '{s}'", .{ @tagName(lhs_ty.zigTypeTag()), @tagName(rhs_ty.zigTypeTag()) });4667 return sema.mod.fail(&block.base, src, "invalid operands to binary bitwise expression: '{s}' and '{s}'", .{ @tagName(lhs_ty.zigTypeTag()), @tagName(rhs_ty.zigTypeTag()) });
4670 }4668 }
46714669
4672 if (casted_lhs.value()) |lhs_val| {4670 if (try sema.resolvePossiblyUndefinedValue(block, lhs_src, casted_lhs)) |lhs_val| {
4673 if (casted_rhs.value()) |rhs_val| {4671 if (try sema.resolvePossiblyUndefinedValue(block, rhs_src, casted_rhs)) |rhs_val| {
4674 if (lhs_val.isUndef() or rhs_val.isUndef()) {4672 if (lhs_val.isUndef() or rhs_val.isUndef()) {
4675 return sema.addConstUndef(resolved_type);4673 return sema.addConstUndef(resolved_type);
4676 }4674 }
...@@ -4799,8 +4797,8 @@ fn analyzeArithmetic(...@@ -4799,8 +4797,8 @@ fn analyzeArithmetic(
4799 return sema.mod.fail(&block.base, src, "invalid operands to binary expression: '{s}' and '{s}'", .{ @tagName(lhs_ty.zigTypeTag()), @tagName(rhs_ty.zigTypeTag()) });4797 return sema.mod.fail(&block.base, src, "invalid operands to binary expression: '{s}' and '{s}'", .{ @tagName(lhs_ty.zigTypeTag()), @tagName(rhs_ty.zigTypeTag()) });
4800 }4798 }
48014799
4802 if (casted_lhs.value()) |lhs_val| {4800 if (try sema.resolvePossiblyUndefinedValue(block, lhs_src, casted_lhs)) |lhs_val| {
4803 if (casted_rhs.value()) |rhs_val| {4801 if (try sema.resolvePossiblyUndefinedValue(block, rhs_src, casted_rhs)) |rhs_val| {
4804 if (lhs_val.isUndef() or rhs_val.isUndef()) {4802 if (lhs_val.isUndef() or rhs_val.isUndef()) {
4805 return sema.addConstUndef(resolved_type);4803 return sema.addConstUndef(resolved_type);
4806 }4804 }
...@@ -5072,8 +5070,8 @@ fn zirCmp(...@@ -5072,8 +5070,8 @@ fn zirCmp(
5072 const casted_lhs = try sema.coerce(block, resolved_type, lhs, lhs_src);5070 const casted_lhs = try sema.coerce(block, resolved_type, lhs, lhs_src);
5073 const casted_rhs = try sema.coerce(block, resolved_type, rhs, rhs_src);5071 const casted_rhs = try sema.coerce(block, resolved_type, rhs, rhs_src);
50745072
5075 if (casted_lhs.value()) |lhs_val| {5073 if (try sema.resolvePossiblyUndefinedValue(block, lhs_src, casted_lhs)) |lhs_val| {
5076 if (casted_rhs.value()) |rhs_val| {5074 if (try sema.resolvePossiblyUndefinedValue(block, rhs_src, casted_rhs)) |rhs_val| {
5077 if (lhs_val.isUndef() or rhs_val.isUndef()) {5075 if (lhs_val.isUndef() or rhs_val.isUndef()) {
5078 return sema.addConstUndef(resolved_type);5076 return sema.addConstUndef(resolved_type);
5079 }5077 }
...@@ -5258,45 +5256,6 @@ fn zirBoolNot(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -5258,45 +5256,6 @@ fn zirBoolNot(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
5258 return block.addTyOp(.not, bool_type, operand);5256 return block.addTyOp(.not, bool_type, operand);
5259}5257}
52605258
5261fn zirBoolOp(
5262 sema: *Sema,
5263 block: *Scope.Block,
5264 inst: Zir.Inst.Index,
5265 is_bool_or: bool,
5266) CompileError!Air.Inst.Ref {
5267 const tracy = trace(@src());
5268 defer tracy.end();
5269
5270 const src: LazySrcLoc = .unneeded;
5271 const bool_type = Type.initTag(.bool);
5272 const bin_inst = sema.code.instructions.items(.data)[inst].bin;
5273 const uncasted_lhs = sema.resolveInst(bin_inst.lhs);
5274 const lhs = try sema.coerce(block, bool_type, uncasted_lhs, uncasted_lhs.src);
5275 const uncasted_rhs = sema.resolveInst(bin_inst.rhs);
5276 const rhs = try sema.coerce(block, bool_type, uncasted_rhs, uncasted_rhs.src);
5277
5278 if (lhs.value()) |lhs_val| {
5279 if (rhs.value()) |rhs_val| {
5280 if (is_bool_or) {
5281 if (lhs_val.toBool() or rhs_val.toBool()) {
5282 return Air.Inst.Ref.bool_true;
5283 } else {
5284 return Air.Inst.Ref.bool_false;
5285 }
5286 } else {
5287 if (lhs_val.toBool() and rhs_val.toBool()) {
5288 return Air.Inst.Ref.bool_true;
5289 } else {
5290 return Air.Inst.Ref.bool_false;
5291 }
5292 }
5293 }
5294 }
5295 try sema.requireRuntimeBlock(block, src);
5296 const tag: Air.Inst.Tag = if (is_bool_or) .bool_or else .bool_and;
5297 return block.addBinOp(tag, lhs, rhs);
5298}
5299
5300fn zirBoolBr(5259fn zirBoolBr(
5301 sema: *Sema,5260 sema: *Sema,
5302 parent_block: *Scope.Block,5261 parent_block: *Scope.Block,
...@@ -6840,8 +6799,8 @@ fn elemPtrArray(...@@ -6840,8 +6799,8 @@ fn elemPtrArray(
6840 elem_index: Air.Inst.Ref,6799 elem_index: Air.Inst.Ref,
6841 elem_index_src: LazySrcLoc,6800 elem_index_src: LazySrcLoc,
6842) CompileError!Air.Inst.Ref {6801) CompileError!Air.Inst.Ref {
6843 if (array_ptr.value()) |array_ptr_val| {6802 if (try sema.resolveDefinedValue(block, src, array_ptr)) |array_ptr_val| {
6844 if (elem_index.value()) |index_val| {6803 if (try sema.resolveDefinedValue(block, src, elem_index)) |index_val| {
6845 // Both array pointer and index are compile-time known.6804 // Both array pointer and index are compile-time known.
6846 const index_u64 = index_val.toUnsignedInt();6805 const index_u64 = index_val.toUnsignedInt();
6847 // @intCast here because it would have been impossible to construct a value that6806 // @intCast here because it would have been impossible to construct a value that
...@@ -7367,8 +7326,8 @@ fn analyzeSlice(...@@ -7367,8 +7326,8 @@ fn analyzeSlice(
7367 var return_ptr_size: std.builtin.TypeInfo.Pointer.Size = .Slice;7326 var return_ptr_size: std.builtin.TypeInfo.Pointer.Size = .Slice;
7368 var return_elem_type = elem_type;7327 var return_elem_type = elem_type;
7369 if (end_opt) |end| {7328 if (end_opt) |end| {
7370 if (end.value()) |end_val| {7329 if (try sema.resolveDefinedValue(block, src, end)) |end_val| {
7371 if (start.value()) |start_val| {7330 if (try sema.resolveDefinedValue(block, src, start)) |start_val| {
7372 const start_u64 = start_val.toUnsignedInt();7331 const start_u64 = start_val.toUnsignedInt();
7373 const end_u64 = end_val.toUnsignedInt();7332 const end_u64 = end_val.toUnsignedInt();
7374 if (start_u64 > end_u64) {7333 if (start_u64 > end_u64) {
...@@ -7492,11 +7451,11 @@ fn cmpNumeric(...@@ -7492,11 +7451,11 @@ fn cmpNumeric(
7492 // For mixed floats and integers, extract the integer part from the float, cast that to7451 // For mixed floats and integers, extract the integer part from the float, cast that to
7493 // a signed integer with mantissa bits + 1, and if there was any non-integral part of the float,7452 // a signed integer with mantissa bits + 1, and if there was any non-integral part of the float,
7494 // add/subtract 1.7453 // add/subtract 1.
7495 const lhs_is_signed = if (lhs.value()) |lhs_val|7454 const lhs_is_signed = if (try sema.resolveDefinedValue(block, lhs_src, lhs)) |lhs_val|
7496 lhs_val.compareWithZero(.lt)7455 lhs_val.compareWithZero(.lt)
7497 else7456 else
7498 (lhs_ty.isFloat() or lhs_ty.isSignedInt());7457 (lhs_ty.isFloat() or lhs_ty.isSignedInt());
7499 const rhs_is_signed = if (rhs.value()) |rhs_val|7458 const rhs_is_signed = if (try sema.resolveDefinedValue(block, rhs_src, rhs)) |rhs_val|
7500 rhs_val.compareWithZero(.lt)7459 rhs_val.compareWithZero(.lt)
7501 else7460 else
7502 (rhs_ty.isFloat() or rhs_ty.isSignedInt());7461 (rhs_ty.isFloat() or rhs_ty.isSignedInt());
...@@ -7505,7 +7464,7 @@ fn cmpNumeric(...@@ -7505,7 +7464,7 @@ fn cmpNumeric(
7505 var dest_float_type: ?Type = null;7464 var dest_float_type: ?Type = null;
75067465
7507 var lhs_bits: usize = undefined;7466 var lhs_bits: usize = undefined;
7508 if (lhs.value()) |lhs_val| {7467 if (try sema.resolvePossiblyUndefinedValue(block, lhs_src, lhs)) |lhs_val| {
7509 if (lhs_val.isUndef())7468 if (lhs_val.isUndef())
7510 return sema.addConstUndef(Type.initTag(.bool));7469 return sema.addConstUndef(Type.initTag(.bool));
7511 const is_unsigned = if (lhs_is_float) x: {7470 const is_unsigned = if (lhs_is_float) x: {
...@@ -7540,7 +7499,7 @@ fn cmpNumeric(...@@ -7540,7 +7499,7 @@ fn cmpNumeric(
7540 }7499 }
75417500
7542 var rhs_bits: usize = undefined;7501 var rhs_bits: usize = undefined;
7543 if (rhs.value()) |rhs_val| {7502 if (try sema.resolvePossiblyUndefinedValue(block, rhs_src, rhs)) |rhs_val| {
7544 if (rhs_val.isUndef())7503 if (rhs_val.isUndef())
7545 return sema.addConstUndef(Type.initTag(.bool));7504 return sema.addConstUndef(Type.initTag(.bool));
7546 const is_unsigned = if (rhs_is_float) x: {7505 const is_unsigned = if (rhs_is_float) x: {
...@@ -7589,8 +7548,8 @@ fn cmpNumeric(...@@ -7589,8 +7548,8 @@ fn cmpNumeric(
7589}7548}
75907549
7591fn wrapOptional(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: Air.Inst.Ref) !Air.Inst.Index {7550fn wrapOptional(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: Air.Inst.Ref) !Air.Inst.Index {
7592 if (inst.value()) |val| {7551 if (try sema.resolvePossiblyUndefinedValue(block, inst_src, inst)) |val| {
7593 return sema.mod.constInst(sema.arena, inst.src, .{ .ty = dest_type, .val = val });7552 return sema.mod.constInst(sema.arena, inst_src, .{ .ty = dest_type, .val = val });
7594 }7553 }
75957554
7596 try sema.requireRuntimeBlock(block, inst.src);7555 try sema.requireRuntimeBlock(block, inst.src);
...@@ -7690,67 +7649,69 @@ fn resolvePeerTypes(...@@ -7690,67 +7649,69 @@ fn resolvePeerTypes(
76907649
7691 var chosen = instructions[0];7650 var chosen = instructions[0];
7692 for (instructions[1..]) |candidate| {7651 for (instructions[1..]) |candidate| {
7693 if (candidate.ty.eql(chosen.ty))7652 const candidate_ty = sema.getTypeOf(candidate);
7653 const chosen_ty = sema.getTypeOf(chosen);
7654 if (candidate_ty.eql(chosen_ty))
7694 continue;7655 continue;
7695 if (candidate.ty.zigTypeTag() == .NoReturn)7656 if (candidate_ty.zigTypeTag() == .NoReturn)
7696 continue;7657 continue;
7697 if (chosen.ty.zigTypeTag() == .NoReturn) {7658 if (chosen_ty.zigTypeTag() == .NoReturn) {
7698 chosen = candidate;7659 chosen = candidate;
7699 continue;7660 continue;
7700 }7661 }
7701 if (candidate.ty.zigTypeTag() == .Undefined)7662 if (candidate_ty.zigTypeTag() == .Undefined)
7702 continue;7663 continue;
7703 if (chosen.ty.zigTypeTag() == .Undefined) {7664 if (chosen_ty.zigTypeTag() == .Undefined) {
7704 chosen = candidate;7665 chosen = candidate;
7705 continue;7666 continue;
7706 }7667 }
7707 if (chosen.ty.isInt() and7668 if (chosen_ty.isInt() and
7708 candidate.ty.isInt() and7669 candidate_ty.isInt() and
7709 chosen.ty.isSignedInt() == candidate.ty.isSignedInt())7670 chosen_ty.isSignedInt() == candidate_ty.isSignedInt())
7710 {7671 {
7711 if (chosen.ty.intInfo(target).bits < candidate.ty.intInfo(target).bits) {7672 if (chosen_ty.intInfo(target).bits < candidate_ty.intInfo(target).bits) {
7712 chosen = candidate;7673 chosen = candidate;
7713 }7674 }
7714 continue;7675 continue;
7715 }7676 }
7716 if (chosen.ty.isFloat() and candidate.ty.isFloat()) {7677 if (chosen_ty.isFloat() and candidate_ty.isFloat()) {
7717 if (chosen.ty.floatBits(target) < candidate.ty.floatBits(target)) {7678 if (chosen_ty.floatBits(target) < candidate_ty.floatBits(target)) {
7718 chosen = candidate;7679 chosen = candidate;
7719 }7680 }
7720 continue;7681 continue;
7721 }7682 }
77227683
7723 if (chosen.ty.zigTypeTag() == .ComptimeInt and candidate.ty.isInt()) {7684 if (chosen_ty.zigTypeTag() == .ComptimeInt and candidate_ty.isInt()) {
7724 chosen = candidate;7685 chosen = candidate;
7725 continue;7686 continue;
7726 }7687 }
77277688
7728 if (chosen.ty.isInt() and candidate.ty.zigTypeTag() == .ComptimeInt) {7689 if (chosen_ty.isInt() and candidate_ty.zigTypeTag() == .ComptimeInt) {
7729 continue;7690 continue;
7730 }7691 }
77317692
7732 if (chosen.ty.zigTypeTag() == .ComptimeFloat and candidate.ty.isFloat()) {7693 if (chosen_ty.zigTypeTag() == .ComptimeFloat and candidate_ty.isFloat()) {
7733 chosen = candidate;7694 chosen = candidate;
7734 continue;7695 continue;
7735 }7696 }
77367697
7737 if (chosen.ty.isFloat() and candidate.ty.zigTypeTag() == .ComptimeFloat) {7698 if (chosen_ty.isFloat() and candidate_ty.zigTypeTag() == .ComptimeFloat) {
7738 continue;7699 continue;
7739 }7700 }
77407701
7741 if (chosen.ty.zigTypeTag() == .Enum and candidate.ty.zigTypeTag() == .EnumLiteral) {7702 if (chosen_ty.zigTypeTag() == .Enum and candidate_ty.zigTypeTag() == .EnumLiteral) {
7742 continue;7703 continue;
7743 }7704 }
7744 if (chosen.ty.zigTypeTag() == .EnumLiteral and candidate.ty.zigTypeTag() == .Enum) {7705 if (chosen_ty.zigTypeTag() == .EnumLiteral and candidate_ty.zigTypeTag() == .Enum) {
7745 chosen = candidate;7706 chosen = candidate;
7746 continue;7707 continue;
7747 }7708 }
77487709
7749 // TODO error notes pointing out each type7710 // TODO error notes pointing out each type
7750 return sema.mod.fail(&block.base, src, "incompatible types: '{}' and '{}'", .{ chosen.ty, candidate.ty });7711 return sema.mod.fail(&block.base, src, "incompatible types: '{}' and '{}'", .{ chosen_ty, candidate_ty });
7751 }7712 }
77527713
7753 return chosen.ty;7714 return sema.getTypeOf(chosen);
7754}7715}
77557716
7756fn resolveTypeFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type) CompileError!Type {7717fn resolveTypeFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type) CompileError!Type {
src/Zir.zig-12
...@@ -236,15 +236,9 @@ pub const Inst = struct {...@@ -236,15 +236,9 @@ pub const Inst = struct {
236 /// Implements `suspend {...}`.236 /// Implements `suspend {...}`.
237 /// Uses the `pl_node` union field. Payload is `Block`.237 /// Uses the `pl_node` union field. Payload is `Block`.
238 suspend_block,238 suspend_block,
239 /// Boolean AND. See also `bit_and`.
240 /// Uses the `pl_node` union field. Payload is `Bin`.
241 bool_and,
242 /// Boolean NOT. See also `bit_not`.239 /// Boolean NOT. See also `bit_not`.
243 /// Uses the `un_node` field.240 /// Uses the `un_node` field.
244 bool_not,241 bool_not,
245 /// Boolean OR. See also `bit_or`.
246 /// Uses the `pl_node` union field. Payload is `Bin`.
247 bool_or,
248 /// Short-circuiting boolean `and`. `lhs` is a boolean `Ref` and the other operand242 /// Short-circuiting boolean `and`. `lhs` is a boolean `Ref` and the other operand
249 /// is a block, which is evaluated if `lhs` is `true`.243 /// is a block, which is evaluated if `lhs` is `true`.
250 /// Uses the `bool_br` union field.244 /// Uses the `bool_br` union field.
...@@ -998,8 +992,6 @@ pub const Inst = struct {...@@ -998,8 +992,6 @@ pub const Inst = struct {
998 .bool_br_and,992 .bool_br_and,
999 .bool_br_or,993 .bool_br_or,
1000 .bool_not,994 .bool_not,
1001 .bool_and,
1002 .bool_or,
1003 .breakpoint,995 .breakpoint,
1004 .fence,996 .fence,
1005 .call,997 .call,
...@@ -1248,9 +1240,7 @@ pub const Inst = struct {...@@ -1248,9 +1240,7 @@ pub const Inst = struct {
1248 .block = .pl_node,1240 .block = .pl_node,
1249 .block_inline = .pl_node,1241 .block_inline = .pl_node,
1250 .suspend_block = .pl_node,1242 .suspend_block = .pl_node,
1251 .bool_and = .pl_node,
1252 .bool_not = .un_node,1243 .bool_not = .un_node,
1253 .bool_or = .pl_node,
1254 .bool_br_and = .bool_br,1244 .bool_br_and = .bool_br,
1255 .bool_br_or = .bool_br,1245 .bool_br_or = .bool_br,
1256 .@"break" = .@"break",1246 .@"break" = .@"break",
...@@ -2981,8 +2971,6 @@ const Writer = struct {...@@ -2981,8 +2971,6 @@ const Writer = struct {
2981 .mulwrap,2971 .mulwrap,
2982 .sub,2972 .sub,
2983 .subwrap,2973 .subwrap,
2984 .bool_and,
2985 .bool_or,
2986 .cmp_lt,2974 .cmp_lt,
2987 .cmp_lte,2975 .cmp_lte,
2988 .cmp_eq,2976 .cmp_eq,