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
19221922 .bool_br_and,
19231923 .bool_br_or,
19241924 .bool_not,
1925 .bool_and,
1926 .bool_or,
19271925 .call_compile_time,
19281926 .call_nosuspend,
19291927 .call_async,
src/Sema.zig+43-82
......@@ -185,8 +185,6 @@ pub fn analyzeBody(
185185 //.block => try sema.zirBlock(block, inst),
186186 //.suspend_block => try sema.zirSuspendBlock(block, inst),
187187 //.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),
190188 //.bool_br_and => try sema.zirBoolBr(block, inst, false),
191189 //.bool_br_or => try sema.zirBoolBr(block, inst, true),
192190 //.c_import => try sema.zirCImport(block, inst),
......@@ -195,12 +193,12 @@ pub fn analyzeBody(
195193 //.call_compile_time => try sema.zirCall(block, inst, .compile_time, false),
196194 //.call_nosuspend => try sema.zirCall(block, inst, .no_async, false),
197195 //.call_async => try sema.zirCall(block, inst, .async_kw, false),
198 //.cmp_eq => try sema.zirCmp(block, inst, .eq),
199 //.cmp_gt => try sema.zirCmp(block, inst, .gt),
200 //.cmp_gte => try sema.zirCmp(block, inst, .gte),
201 //.cmp_lt => try sema.zirCmp(block, inst, .lt),
202 //.cmp_lte => try sema.zirCmp(block, inst, .lte),
203 //.cmp_neq => try sema.zirCmp(block, inst, .neq),
196 .cmp_eq => try sema.zirCmp(block, inst, .eq),
197 .cmp_gt => try sema.zirCmp(block, inst, .gt),
198 .cmp_gte => try sema.zirCmp(block, inst, .gte),
199 .cmp_lt => try sema.zirCmp(block, inst, .lt),
200 .cmp_lte => try sema.zirCmp(block, inst, .lte),
201 .cmp_neq => try sema.zirCmp(block, inst, .neq),
204202 //.coerce_result_ptr => try sema.zirCoerceResultPtr(block, inst),
205203 //.decl_ref => try sema.zirDeclRef(block, inst),
206204 //.decl_val => try sema.zirDeclVal(block, inst),
......@@ -4669,8 +4667,8 @@ fn zirBitwise(
46694667 return sema.mod.fail(&block.base, src, "invalid operands to binary bitwise expression: '{s}' and '{s}'", .{ @tagName(lhs_ty.zigTypeTag()), @tagName(rhs_ty.zigTypeTag()) });
46704668 }
46714669
4672 if (casted_lhs.value()) |lhs_val| {
4673 if (casted_rhs.value()) |rhs_val| {
4670 if (try sema.resolvePossiblyUndefinedValue(block, lhs_src, casted_lhs)) |lhs_val| {
4671 if (try sema.resolvePossiblyUndefinedValue(block, rhs_src, casted_rhs)) |rhs_val| {
46744672 if (lhs_val.isUndef() or rhs_val.isUndef()) {
46754673 return sema.addConstUndef(resolved_type);
46764674 }
......@@ -4799,8 +4797,8 @@ fn analyzeArithmetic(
47994797 return sema.mod.fail(&block.base, src, "invalid operands to binary expression: '{s}' and '{s}'", .{ @tagName(lhs_ty.zigTypeTag()), @tagName(rhs_ty.zigTypeTag()) });
48004798 }
48014799
4802 if (casted_lhs.value()) |lhs_val| {
4803 if (casted_rhs.value()) |rhs_val| {
4800 if (try sema.resolvePossiblyUndefinedValue(block, lhs_src, casted_lhs)) |lhs_val| {
4801 if (try sema.resolvePossiblyUndefinedValue(block, rhs_src, casted_rhs)) |rhs_val| {
48044802 if (lhs_val.isUndef() or rhs_val.isUndef()) {
48054803 return sema.addConstUndef(resolved_type);
48064804 }
......@@ -5072,8 +5070,8 @@ fn zirCmp(
50725070 const casted_lhs = try sema.coerce(block, resolved_type, lhs, lhs_src);
50735071 const casted_rhs = try sema.coerce(block, resolved_type, rhs, rhs_src);
50745072
5075 if (casted_lhs.value()) |lhs_val| {
5076 if (casted_rhs.value()) |rhs_val| {
5073 if (try sema.resolvePossiblyUndefinedValue(block, lhs_src, casted_lhs)) |lhs_val| {
5074 if (try sema.resolvePossiblyUndefinedValue(block, rhs_src, casted_rhs)) |rhs_val| {
50775075 if (lhs_val.isUndef() or rhs_val.isUndef()) {
50785076 return sema.addConstUndef(resolved_type);
50795077 }
......@@ -5258,45 +5256,6 @@ fn zirBoolNot(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
52585256 return block.addTyOp(.not, bool_type, operand);
52595257}
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
53005259fn zirBoolBr(
53015260 sema: *Sema,
53025261 parent_block: *Scope.Block,
......@@ -6840,8 +6799,8 @@ fn elemPtrArray(
68406799 elem_index: Air.Inst.Ref,
68416800 elem_index_src: LazySrcLoc,
68426801) CompileError!Air.Inst.Ref {
6843 if (array_ptr.value()) |array_ptr_val| {
6844 if (elem_index.value()) |index_val| {
6802 if (try sema.resolveDefinedValue(block, src, array_ptr)) |array_ptr_val| {
6803 if (try sema.resolveDefinedValue(block, src, elem_index)) |index_val| {
68456804 // Both array pointer and index are compile-time known.
68466805 const index_u64 = index_val.toUnsignedInt();
68476806 // @intCast here because it would have been impossible to construct a value that
......@@ -7367,8 +7326,8 @@ fn analyzeSlice(
73677326 var return_ptr_size: std.builtin.TypeInfo.Pointer.Size = .Slice;
73687327 var return_elem_type = elem_type;
73697328 if (end_opt) |end| {
7370 if (end.value()) |end_val| {
7371 if (start.value()) |start_val| {
7329 if (try sema.resolveDefinedValue(block, src, end)) |end_val| {
7330 if (try sema.resolveDefinedValue(block, src, start)) |start_val| {
73727331 const start_u64 = start_val.toUnsignedInt();
73737332 const end_u64 = end_val.toUnsignedInt();
73747333 if (start_u64 > end_u64) {
......@@ -7492,11 +7451,11 @@ fn cmpNumeric(
74927451 // For mixed floats and integers, extract the integer part from the float, cast that to
74937452 // a signed integer with mantissa bits + 1, and if there was any non-integral part of the float,
74947453 // 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|
74967455 lhs_val.compareWithZero(.lt)
74977456 else
74987457 (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|
75007459 rhs_val.compareWithZero(.lt)
75017460 else
75027461 (rhs_ty.isFloat() or rhs_ty.isSignedInt());
......@@ -7505,7 +7464,7 @@ fn cmpNumeric(
75057464 var dest_float_type: ?Type = null;
75067465
75077466 var lhs_bits: usize = undefined;
7508 if (lhs.value()) |lhs_val| {
7467 if (try sema.resolvePossiblyUndefinedValue(block, lhs_src, lhs)) |lhs_val| {
75097468 if (lhs_val.isUndef())
75107469 return sema.addConstUndef(Type.initTag(.bool));
75117470 const is_unsigned = if (lhs_is_float) x: {
......@@ -7540,7 +7499,7 @@ fn cmpNumeric(
75407499 }
75417500
75427501 var rhs_bits: usize = undefined;
7543 if (rhs.value()) |rhs_val| {
7502 if (try sema.resolvePossiblyUndefinedValue(block, rhs_src, rhs)) |rhs_val| {
75447503 if (rhs_val.isUndef())
75457504 return sema.addConstUndef(Type.initTag(.bool));
75467505 const is_unsigned = if (rhs_is_float) x: {
......@@ -7589,8 +7548,8 @@ fn cmpNumeric(
75897548}
75907549
75917550fn wrapOptional(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: Air.Inst.Ref) !Air.Inst.Index {
7592 if (inst.value()) |val| {
7593 return sema.mod.constInst(sema.arena, inst.src, .{ .ty = dest_type, .val = val });
7551 if (try sema.resolvePossiblyUndefinedValue(block, inst_src, inst)) |val| {
7552 return sema.mod.constInst(sema.arena, inst_src, .{ .ty = dest_type, .val = val });
75947553 }
75957554
75967555 try sema.requireRuntimeBlock(block, inst.src);
......@@ -7690,67 +7649,69 @@ fn resolvePeerTypes(
76907649
76917650 var chosen = instructions[0];
76927651 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))
76947655 continue;
7695 if (candidate.ty.zigTypeTag() == .NoReturn)
7656 if (candidate_ty.zigTypeTag() == .NoReturn)
76967657 continue;
7697 if (chosen.ty.zigTypeTag() == .NoReturn) {
7658 if (chosen_ty.zigTypeTag() == .NoReturn) {
76987659 chosen = candidate;
76997660 continue;
77007661 }
7701 if (candidate.ty.zigTypeTag() == .Undefined)
7662 if (candidate_ty.zigTypeTag() == .Undefined)
77027663 continue;
7703 if (chosen.ty.zigTypeTag() == .Undefined) {
7664 if (chosen_ty.zigTypeTag() == .Undefined) {
77047665 chosen = candidate;
77057666 continue;
77067667 }
7707 if (chosen.ty.isInt() and
7708 candidate.ty.isInt() and
7709 chosen.ty.isSignedInt() == candidate.ty.isSignedInt())
7668 if (chosen_ty.isInt() and
7669 candidate_ty.isInt() and
7670 chosen_ty.isSignedInt() == candidate_ty.isSignedInt())
77107671 {
7711 if (chosen.ty.intInfo(target).bits < candidate.ty.intInfo(target).bits) {
7672 if (chosen_ty.intInfo(target).bits < candidate_ty.intInfo(target).bits) {
77127673 chosen = candidate;
77137674 }
77147675 continue;
77157676 }
7716 if (chosen.ty.isFloat() and candidate.ty.isFloat()) {
7717 if (chosen.ty.floatBits(target) < candidate.ty.floatBits(target)) {
7677 if (chosen_ty.isFloat() and candidate_ty.isFloat()) {
7678 if (chosen_ty.floatBits(target) < candidate_ty.floatBits(target)) {
77187679 chosen = candidate;
77197680 }
77207681 continue;
77217682 }
77227683
7723 if (chosen.ty.zigTypeTag() == .ComptimeInt and candidate.ty.isInt()) {
7684 if (chosen_ty.zigTypeTag() == .ComptimeInt and candidate_ty.isInt()) {
77247685 chosen = candidate;
77257686 continue;
77267687 }
77277688
7728 if (chosen.ty.isInt() and candidate.ty.zigTypeTag() == .ComptimeInt) {
7689 if (chosen_ty.isInt() and candidate_ty.zigTypeTag() == .ComptimeInt) {
77297690 continue;
77307691 }
77317692
7732 if (chosen.ty.zigTypeTag() == .ComptimeFloat and candidate.ty.isFloat()) {
7693 if (chosen_ty.zigTypeTag() == .ComptimeFloat and candidate_ty.isFloat()) {
77337694 chosen = candidate;
77347695 continue;
77357696 }
77367697
7737 if (chosen.ty.isFloat() and candidate.ty.zigTypeTag() == .ComptimeFloat) {
7698 if (chosen_ty.isFloat() and candidate_ty.zigTypeTag() == .ComptimeFloat) {
77387699 continue;
77397700 }
77407701
7741 if (chosen.ty.zigTypeTag() == .Enum and candidate.ty.zigTypeTag() == .EnumLiteral) {
7702 if (chosen_ty.zigTypeTag() == .Enum and candidate_ty.zigTypeTag() == .EnumLiteral) {
77427703 continue;
77437704 }
7744 if (chosen.ty.zigTypeTag() == .EnumLiteral and candidate.ty.zigTypeTag() == .Enum) {
7705 if (chosen_ty.zigTypeTag() == .EnumLiteral and candidate_ty.zigTypeTag() == .Enum) {
77457706 chosen = candidate;
77467707 continue;
77477708 }
77487709
77497710 // 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 });
77517712 }
77527713
7753 return chosen.ty;
7714 return sema.getTypeOf(chosen);
77547715}
77557716
77567717fn resolveTypeFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type) CompileError!Type {
src/Zir.zig-12
......@@ -236,15 +236,9 @@ pub const Inst = struct {
236236 /// Implements `suspend {...}`.
237237 /// Uses the `pl_node` union field. Payload is `Block`.
238238 suspend_block,
239 /// Boolean AND. See also `bit_and`.
240 /// Uses the `pl_node` union field. Payload is `Bin`.
241 bool_and,
242239 /// Boolean NOT. See also `bit_not`.
243240 /// Uses the `un_node` field.
244241 bool_not,
245 /// Boolean OR. See also `bit_or`.
246 /// Uses the `pl_node` union field. Payload is `Bin`.
247 bool_or,
248242 /// Short-circuiting boolean `and`. `lhs` is a boolean `Ref` and the other operand
249243 /// is a block, which is evaluated if `lhs` is `true`.
250244 /// Uses the `bool_br` union field.
......@@ -998,8 +992,6 @@ pub const Inst = struct {
998992 .bool_br_and,
999993 .bool_br_or,
1000994 .bool_not,
1001 .bool_and,
1002 .bool_or,
1003995 .breakpoint,
1004996 .fence,
1005997 .call,
......@@ -1248,9 +1240,7 @@ pub const Inst = struct {
12481240 .block = .pl_node,
12491241 .block_inline = .pl_node,
12501242 .suspend_block = .pl_node,
1251 .bool_and = .pl_node,
12521243 .bool_not = .un_node,
1253 .bool_or = .pl_node,
12541244 .bool_br_and = .bool_br,
12551245 .bool_br_or = .bool_br,
12561246 .@"break" = .@"break",
......@@ -2981,8 +2971,6 @@ const Writer = struct {
29812971 .mulwrap,
29822972 .sub,
29832973 .subwrap,
2984 .bool_and,
2985 .bool_or,
29862974 .cmp_lt,
29872975 .cmp_lte,
29882976 .cmp_eq,