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 11:07:00-05:00
log7a8f391b0fa397eea4a9602a0132e0247d0f67b7
treebec959df7cd42b36bea7ab80c93bda4283eee155
parentbcbcb2e9ffbfef63d3692ea45eea2e2babacc3a9
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...@@ -5762,81 +5762,71 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
5762 zig_unreachable();5762 zig_unreachable();
5763 case ConstPtrSpecialRef:5763 case ConstPtrSpecialRef:
5764 {5764 {
5765 render_const_val_global(g, const_val, name);5765 assert(const_val->global_refs != nullptr);
5766 ConstExprValue *pointee = const_val->data.x_ptr.data.ref.pointee;5766 ConstExprValue *pointee = const_val->data.x_ptr.data.ref.pointee;
5767 render_const_val(g, pointee, "");5767 render_const_val(g, pointee, "");
5768 render_const_val_global(g, pointee, "");5768 render_const_val_global(g, pointee, "");
5769 ConstExprValue *other_val = pointee;5769 const_val->global_refs->llvm_value = LLVMConstBitCast(pointee->global_refs->llvm_global, const_val->type->type_ref);
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, "");
5772 return const_val->global_refs->llvm_value;5770 return const_val->global_refs->llvm_value;
5773 }5771 }
5774 case ConstPtrSpecialBaseArray:5772 case ConstPtrSpecialBaseArray:
5775 {5773 {
5776 render_const_val_global(g, const_val, name);5774 assert(const_val->global_refs != nullptr);
5777 ConstExprValue *array_const_val = const_val->data.x_ptr.data.base_array.array_val;5775 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;
5779 assert(array_const_val->type->id == ZigTypeIdArray);5776 assert(array_const_val->type->id == ZigTypeIdArray);
5780 if (array_const_val->type->zero_bits) {5777 if (!type_has_bits(array_const_val->type)) {
5781 // make this a null pointer5778 // make this a null pointer
5782 ZigType *usize = g->builtin_types.entry_usize;5779 ZigType *usize = g->builtin_types.entry_usize;
5783 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),5780 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
5784 const_val->type->type_ref);5781 const_val->type->type_ref);
5785 render_const_val_global(g, const_val, "");
5786 return const_val->global_refs->llvm_value;5782 return const_val->global_refs->llvm_value;
5787 }5783 }
5788 LLVMValueRef uncasted_ptr_val = gen_const_ptr_array_recursive(g, array_const_val,5784 size_t elem_index = const_val->data.x_ptr.data.base_array.elem_index;
5789 elem_index);5785 LLVMValueRef uncasted_ptr_val = gen_const_ptr_array_recursive(g, array_const_val, elem_index);
5790 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);5786 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
5791 const_val->global_refs->llvm_value = ptr_val;5787 const_val->global_refs->llvm_value = ptr_val;
5792 render_const_val_global(g, const_val, "");
5793 return ptr_val;5788 return ptr_val;
5794 }5789 }
5795 case ConstPtrSpecialBaseStruct:5790 case ConstPtrSpecialBaseStruct:
5796 {5791 {
5797 render_const_val_global(g, const_val, name);5792 assert(const_val->global_refs != nullptr);
5798 ConstExprValue *struct_const_val = const_val->data.x_ptr.data.base_struct.struct_val;5793 ConstExprValue *struct_const_val = const_val->data.x_ptr.data.base_struct.struct_val;
5799 assert(struct_const_val->type->id == ZigTypeIdStruct);5794 assert(struct_const_val->type->id == ZigTypeIdStruct);
5800 if (struct_const_val->type->zero_bits) {5795 if (!type_has_bits(struct_const_val->type)) {
5801 // make this a null pointer5796 // make this a null pointer
5802 ZigType *usize = g->builtin_types.entry_usize;5797 ZigType *usize = g->builtin_types.entry_usize;
5803 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),5798 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
5804 const_val->type->type_ref);5799 const_val->type->type_ref);
5805 render_const_val_global(g, const_val, "");
5806 return const_val->global_refs->llvm_value;5800 return const_val->global_refs->llvm_value;
5807 }5801 }
5808 size_t src_field_index = const_val->data.x_ptr.data.base_struct.field_index;5802 size_t src_field_index = const_val->data.x_ptr.data.base_struct.field_index;
5809 size_t gen_field_index =5803 size_t gen_field_index = struct_const_val->type->data.structure.fields[src_field_index].gen_index;
5810 struct_const_val->type->data.structure.fields[src_field_index].gen_index;
5811 LLVMValueRef uncasted_ptr_val = gen_const_ptr_struct_recursive(g, struct_const_val,5804 LLVMValueRef uncasted_ptr_val = gen_const_ptr_struct_recursive(g, struct_const_val,
5812 gen_field_index);5805 gen_field_index);
5813 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);5806 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
5814 const_val->global_refs->llvm_value = ptr_val;5807 const_val->global_refs->llvm_value = ptr_val;
5815 render_const_val_global(g, const_val, "");
5816 return ptr_val;5808 return ptr_val;
5817 }5809 }
5818 case ConstPtrSpecialBaseErrorUnionCode:5810 case ConstPtrSpecialBaseErrorUnionCode:
5819 {5811 {
5820 render_const_val_global(g, const_val, name);5812 assert(const_val->global_refs != nullptr);
5821 ConstExprValue *err_union_const_val = const_val->data.x_ptr.data.base_err_union_code.err_union_val;5813 ConstExprValue *err_union_const_val = const_val->data.x_ptr.data.base_err_union_code.err_union_val;
5822 assert(err_union_const_val->type->id == ZigTypeIdErrorUnion);5814 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)) {
5824 // make this a null pointer5816 // make this a null pointer
5825 ZigType *usize = g->builtin_types.entry_usize;5817 ZigType *usize = g->builtin_types.entry_usize;
5826 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),5818 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
5827 const_val->type->type_ref);5819 const_val->type->type_ref);
5828 render_const_val_global(g, const_val, "");
5829 return const_val->global_refs->llvm_value;5820 return const_val->global_refs->llvm_value;
5830 }5821 }
5831 LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_code_recursive(g, err_union_const_val);5822 LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_code_recursive(g, err_union_const_val);
5832 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);5823 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
5833 const_val->global_refs->llvm_value = ptr_val;5824 const_val->global_refs->llvm_value = ptr_val;
5834 render_const_val_global(g, const_val, "");
5835 return ptr_val;5825 return ptr_val;
5836 }5826 }
5837 case ConstPtrSpecialBaseErrorUnionPayload:5827 case ConstPtrSpecialBaseErrorUnionPayload:
5838 {5828 {
5839 render_const_val_global(g, const_val, name);5829 assert(const_val->global_refs != nullptr);
5840 ConstExprValue *err_union_const_val = const_val->data.x_ptr.data.base_err_union_payload.err_union_val;5830 ConstExprValue *err_union_const_val = const_val->data.x_ptr.data.base_err_union_payload.err_union_val;
5841 assert(err_union_const_val->type->id == ZigTypeIdErrorUnion);5831 assert(err_union_const_val->type->id == ZigTypeIdErrorUnion);
5842 if (err_union_const_val->type->zero_bits) {5832 if (err_union_const_val->type->zero_bits) {
...@@ -5844,18 +5834,16 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -5844,18 +5834,16 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
5844 ZigType *usize = g->builtin_types.entry_usize;5834 ZigType *usize = g->builtin_types.entry_usize;
5845 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),5835 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
5846 const_val->type->type_ref);5836 const_val->type->type_ref);
5847 render_const_val_global(g, const_val, "");
5848 return const_val->global_refs->llvm_value;5837 return const_val->global_refs->llvm_value;
5849 }5838 }
5850 LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_payload_recursive(g, err_union_const_val);5839 LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_payload_recursive(g, err_union_const_val);
5851 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);5840 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
5852 const_val->global_refs->llvm_value = ptr_val;5841 const_val->global_refs->llvm_value = ptr_val;
5853 render_const_val_global(g, const_val, "");
5854 return ptr_val;5842 return ptr_val;
5855 }5843 }
5856 case ConstPtrSpecialBaseOptionalPayload:5844 case ConstPtrSpecialBaseOptionalPayload:
5857 {5845 {
5858 render_const_val_global(g, const_val, name);5846 assert(const_val->global_refs != nullptr);
5859 ConstExprValue *optional_const_val = const_val->data.x_ptr.data.base_optional_payload.optional_val;5847 ConstExprValue *optional_const_val = const_val->data.x_ptr.data.base_optional_payload.optional_val;
5860 assert(optional_const_val->type->id == ZigTypeIdOptional);5848 assert(optional_const_val->type->id == ZigTypeIdOptional);
5861 if (optional_const_val->type->zero_bits) {5849 if (optional_const_val->type->zero_bits) {
...@@ -5863,23 +5851,20 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -5863,23 +5851,20 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
5863 ZigType *usize = g->builtin_types.entry_usize;5851 ZigType *usize = g->builtin_types.entry_usize;
5864 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),5852 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
5865 const_val->type->type_ref);5853 const_val->type->type_ref);
5866 render_const_val_global(g, const_val, "");
5867 return const_val->global_refs->llvm_value;5854 return const_val->global_refs->llvm_value;
5868 }5855 }
5869 LLVMValueRef uncasted_ptr_val = gen_const_ptr_optional_payload_recursive(g, optional_const_val);5856 LLVMValueRef uncasted_ptr_val = gen_const_ptr_optional_payload_recursive(g, optional_const_val);
5870 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);5857 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
5871 const_val->global_refs->llvm_value = ptr_val;5858 const_val->global_refs->llvm_value = ptr_val;
5872 render_const_val_global(g, const_val, "");
5873 return ptr_val;5859 return ptr_val;
5874 }5860 }
5875 case ConstPtrSpecialHardCodedAddr:5861 case ConstPtrSpecialHardCodedAddr:
5876 {5862 {
5877 render_const_val_global(g, const_val, name);5863 assert(const_val->global_refs != nullptr);
5878 uint64_t addr_value = const_val->data.x_ptr.data.hard_coded_addr.addr;5864 uint64_t addr_value = const_val->data.x_ptr.data.hard_coded_addr.addr;
5879 ZigType *usize = g->builtin_types.entry_usize;5865 ZigType *usize = g->builtin_types.entry_usize;
5880 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstInt(usize->type_ref, addr_value, false),5866 const_val->global_refs->llvm_value = LLVMConstIntToPtr(
5881 const_val->type->type_ref);5867 LLVMConstInt(usize->type_ref, addr_value, false), const_val->type->type_ref);
5882 render_const_val_global(g, const_val, "");
5883 return const_val->global_refs->llvm_value;5868 return const_val->global_refs->llvm_value;
5884 }5869 }
5885 case ConstPtrSpecialFunction:5870 case ConstPtrSpecialFunction: