authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-10 10:58:00-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-10 10:58:00-05:00
log8e68d43ad373e643797209d59e6f10aa12b4c038
treee2f3995a2f4db98e1a6b093d905defb2827cdcc0
parent2f9fedabf0805a47aba5c348e5369c1c28f6cf21
signaturelock-open Commit is signed but in an unrecognized format.

avoid needlessly creating global constants

This deletes some legacy cruft, and produces leaner object files. Example: ``` var x: i32 = 1234; export fn entry() i32 { return x; } ``` This produces: ``` @x = internal unnamed_addr global i32 1234, align 4 @0 = internal unnamed_addr constant i32* @x, align 8 ``` and @0 is never even used. After this commit, @0 is not produced. This fixes a bug: Zig was creating invalid LLVM IR when one of these globals that shouldn't exist takes the address of a thread local variable. In LLVM 8.0.0rc2, it would produce a linker error. But probably after my bug report is solved it will be caught by the IR verifier. https://bugs.llvm.org/show_bug.cgi?id=40652

1 files changed, 16 insertions(+), 31 deletions(-)

src/codegen.cpp+16-31
......@@ -5762,81 +5762,71 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
57625762 zig_unreachable();
57635763 case ConstPtrSpecialRef:
57645764 {
5765 render_const_val_global(g, const_val, name);
5765 assert(const_val->global_refs != nullptr);
57665766 ConstExprValue *pointee = const_val->data.x_ptr.data.ref.pointee;
57675767 render_const_val(g, pointee, "");
57685768 render_const_val_global(g, pointee, "");
5769 ConstExprValue *other_val = pointee;
5770 const_val->global_refs->llvm_value = LLVMConstBitCast(other_val->global_refs->llvm_global, const_val->type->type_ref);
5771 render_const_val_global(g, const_val, "");
5769 const_val->global_refs->llvm_value = LLVMConstBitCast(pointee->global_refs->llvm_global, const_val->type->type_ref);
57725770 return const_val->global_refs->llvm_value;
57735771 }
57745772 case ConstPtrSpecialBaseArray:
57755773 {
5776 render_const_val_global(g, const_val, name);
5774 assert(const_val->global_refs != nullptr);
57775775 ConstExprValue *array_const_val = const_val->data.x_ptr.data.base_array.array_val;
5778 size_t elem_index = const_val->data.x_ptr.data.base_array.elem_index;
57795776 assert(array_const_val->type->id == ZigTypeIdArray);
5780 if (array_const_val->type->zero_bits) {
5777 if (!type_has_bits(array_const_val->type)) {
57815778 // make this a null pointer
57825779 ZigType *usize = g->builtin_types.entry_usize;
57835780 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
57845781 const_val->type->type_ref);
5785 render_const_val_global(g, const_val, "");
57865782 return const_val->global_refs->llvm_value;
57875783 }
5788 LLVMValueRef uncasted_ptr_val = gen_const_ptr_array_recursive(g, array_const_val,
5789 elem_index);
5784 size_t elem_index = const_val->data.x_ptr.data.base_array.elem_index;
5785 LLVMValueRef uncasted_ptr_val = gen_const_ptr_array_recursive(g, array_const_val, elem_index);
57905786 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
57915787 const_val->global_refs->llvm_value = ptr_val;
5792 render_const_val_global(g, const_val, "");
57935788 return ptr_val;
57945789 }
57955790 case ConstPtrSpecialBaseStruct:
57965791 {
5797 render_const_val_global(g, const_val, name);
5792 assert(const_val->global_refs != nullptr);
57985793 ConstExprValue *struct_const_val = const_val->data.x_ptr.data.base_struct.struct_val;
57995794 assert(struct_const_val->type->id == ZigTypeIdStruct);
5800 if (struct_const_val->type->zero_bits) {
5795 if (!type_has_bits(struct_const_val->type)) {
58015796 // make this a null pointer
58025797 ZigType *usize = g->builtin_types.entry_usize;
58035798 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
58045799 const_val->type->type_ref);
5805 render_const_val_global(g, const_val, "");
58065800 return const_val->global_refs->llvm_value;
58075801 }
58085802 size_t src_field_index = const_val->data.x_ptr.data.base_struct.field_index;
5809 size_t gen_field_index =
5810 struct_const_val->type->data.structure.fields[src_field_index].gen_index;
5803 size_t gen_field_index = struct_const_val->type->data.structure.fields[src_field_index].gen_index;
58115804 LLVMValueRef uncasted_ptr_val = gen_const_ptr_struct_recursive(g, struct_const_val,
58125805 gen_field_index);
58135806 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
58145807 const_val->global_refs->llvm_value = ptr_val;
5815 render_const_val_global(g, const_val, "");
58165808 return ptr_val;
58175809 }
58185810 case ConstPtrSpecialBaseErrorUnionCode:
58195811 {
5820 render_const_val_global(g, const_val, name);
5812 assert(const_val->global_refs != nullptr);
58215813 ConstExprValue *err_union_const_val = const_val->data.x_ptr.data.base_err_union_code.err_union_val;
58225814 assert(err_union_const_val->type->id == ZigTypeIdErrorUnion);
5823 if (err_union_const_val->type->zero_bits) {
5815 if (!type_has_bits(err_union_const_val->type)) {
58245816 // make this a null pointer
58255817 ZigType *usize = g->builtin_types.entry_usize;
58265818 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
58275819 const_val->type->type_ref);
5828 render_const_val_global(g, const_val, "");
58295820 return const_val->global_refs->llvm_value;
58305821 }
58315822 LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_code_recursive(g, err_union_const_val);
58325823 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
58335824 const_val->global_refs->llvm_value = ptr_val;
5834 render_const_val_global(g, const_val, "");
58355825 return ptr_val;
58365826 }
58375827 case ConstPtrSpecialBaseErrorUnionPayload:
58385828 {
5839 render_const_val_global(g, const_val, name);
5829 assert(const_val->global_refs != nullptr);
58405830 ConstExprValue *err_union_const_val = const_val->data.x_ptr.data.base_err_union_payload.err_union_val;
58415831 assert(err_union_const_val->type->id == ZigTypeIdErrorUnion);
58425832 if (err_union_const_val->type->zero_bits) {
......@@ -5844,18 +5834,16 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
58445834 ZigType *usize = g->builtin_types.entry_usize;
58455835 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
58465836 const_val->type->type_ref);
5847 render_const_val_global(g, const_val, "");
58485837 return const_val->global_refs->llvm_value;
58495838 }
58505839 LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_payload_recursive(g, err_union_const_val);
58515840 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
58525841 const_val->global_refs->llvm_value = ptr_val;
5853 render_const_val_global(g, const_val, "");
58545842 return ptr_val;
58555843 }
58565844 case ConstPtrSpecialBaseOptionalPayload:
58575845 {
5858 render_const_val_global(g, const_val, name);
5846 assert(const_val->global_refs != nullptr);
58595847 ConstExprValue *optional_const_val = const_val->data.x_ptr.data.base_optional_payload.optional_val;
58605848 assert(optional_const_val->type->id == ZigTypeIdOptional);
58615849 if (optional_const_val->type->zero_bits) {
......@@ -5863,23 +5851,20 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
58635851 ZigType *usize = g->builtin_types.entry_usize;
58645852 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
58655853 const_val->type->type_ref);
5866 render_const_val_global(g, const_val, "");
58675854 return const_val->global_refs->llvm_value;
58685855 }
58695856 LLVMValueRef uncasted_ptr_val = gen_const_ptr_optional_payload_recursive(g, optional_const_val);
58705857 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
58715858 const_val->global_refs->llvm_value = ptr_val;
5872 render_const_val_global(g, const_val, "");
58735859 return ptr_val;
58745860 }
58755861 case ConstPtrSpecialHardCodedAddr:
58765862 {
5877 render_const_val_global(g, const_val, name);
5863 assert(const_val->global_refs != nullptr);
58785864 uint64_t addr_value = const_val->data.x_ptr.data.hard_coded_addr.addr;
58795865 ZigType *usize = g->builtin_types.entry_usize;
5880 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstInt(usize->type_ref, addr_value, false),
5881 const_val->type->type_ref);
5882 render_const_val_global(g, const_val, "");
5866 const_val->global_refs->llvm_value = LLVMConstIntToPtr(
5867 LLVMConstInt(usize->type_ref, addr_value, false), const_val->type->type_ref);
58835868 return const_val->global_refs->llvm_value;
58845869 }
58855870 case ConstPtrSpecialFunction: