Skip to content

Commit 4724cd4

Browse files
introduce and use ptradd/inbounds_ptradd instead of gep
1 parent beed25b commit 4724cd4

File tree

6 files changed

+27
-38
lines changed

6 files changed

+27
-38
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -839,11 +839,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
839839
let llptr = if i == 0 {
840840
place.llval
841841
} else {
842-
self.inbounds_gep(
843-
self.type_i8(),
844-
place.llval,
845-
&[self.const_usize(b_offset.bytes())],
846-
)
842+
self.inbounds_ptradd(place.llval, self.const_usize(b_offset.bytes()))
847843
};
848844
let llty = place.layout.scalar_pair_element_gcc_type(self, i);
849845
let load = self.load(llty, llptr, align);

compiler/rustc_codegen_llvm/src/builder.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -603,11 +603,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
603603
let llptr = if i == 0 {
604604
place.llval
605605
} else {
606-
self.inbounds_gep(
607-
self.type_i8(),
608-
place.llval,
609-
&[self.const_usize(b_offset.bytes())],
610-
)
606+
self.inbounds_ptradd(place.llval, self.const_usize(b_offset.bytes()))
611607
};
612608
let llty = place.layout.scalar_pair_element_llvm_type(self, i, false);
613609
let load = self.load(llty, llptr, align);

compiler/rustc_codegen_llvm/src/va_arg.rs

+15-23
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ fn emit_direct_ptr_va_arg<'ll, 'tcx>(
4444

4545
let aligned_size = size.align_to(slot_size).bytes() as i32;
4646
let full_direct_size = bx.cx().const_i32(aligned_size);
47-
let next = bx.inbounds_gep(bx.type_i8(), addr, &[full_direct_size]);
47+
let next = bx.inbounds_ptradd(addr, full_direct_size);
4848
bx.store(next, va_list_addr, bx.tcx().data_layout.pointer_align.abi);
4949

5050
if size.bytes() < slot_size.bytes() && bx.tcx().sess.target.endian == Endian::Big {
5151
let adjusted_size = bx.cx().const_i32((slot_size.bytes() - size.bytes()) as i32);
52-
let adjusted = bx.inbounds_gep(bx.type_i8(), addr, &[adjusted_size]);
52+
let adjusted = bx.inbounds_ptradd(addr, adjusted_size);
5353
(adjusted, addr_align)
5454
} else {
5555
(addr, addr_align)
@@ -109,14 +109,10 @@ fn emit_aapcs_va_arg<'ll, 'tcx>(
109109
// Table 3, Mapping of C & C++ built-in data types
110110
let ptr_offset = 8;
111111
let i32_offset = 4;
112-
let gr_top = bx.inbounds_gep(bx.type_i8(), va_list_addr, &[bx.cx.const_usize(ptr_offset)]);
113-
let vr_top = bx.inbounds_gep(bx.type_i8(), va_list_addr, &[bx.cx.const_usize(2 * ptr_offset)]);
114-
let gr_offs = bx.inbounds_gep(bx.type_i8(), va_list_addr, &[bx.cx.const_usize(3 * ptr_offset)]);
115-
let vr_offs = bx.inbounds_gep(
116-
bx.type_i8(),
117-
va_list_addr,
118-
&[bx.cx.const_usize(3 * ptr_offset + i32_offset)],
119-
);
112+
let gr_top = bx.inbounds_ptradd(va_list_addr, bx.cx.const_usize(ptr_offset));
113+
let vr_top = bx.inbounds_ptradd(va_list_addr, bx.cx.const_usize(2 * ptr_offset));
114+
let gr_offs = bx.inbounds_ptradd(va_list_addr, bx.cx.const_usize(3 * ptr_offset));
115+
let vr_offs = bx.inbounds_ptradd(va_list_addr, bx.cx.const_usize(3 * ptr_offset + i32_offset));
120116

121117
let layout = bx.cx.layout_of(target_ty);
122118

@@ -164,11 +160,11 @@ fn emit_aapcs_va_arg<'ll, 'tcx>(
164160
let top = bx.load(top_type, reg_top, dl.pointer_align.abi);
165161

166162
// reg_value = *(@top + reg_off_v);
167-
let mut reg_addr = bx.gep(bx.type_i8(), top, &[reg_off_v]);
163+
let mut reg_addr = bx.ptradd(top, reg_off_v);
168164
if bx.tcx().sess.target.endian == Endian::Big && layout.size.bytes() != slot_size {
169165
// On big-endian systems the value is right-aligned in its slot.
170166
let offset = bx.const_i32((slot_size - layout.size.bytes()) as i32);
171-
reg_addr = bx.gep(bx.type_i8(), reg_addr, &[offset]);
167+
reg_addr = bx.ptradd(reg_addr, offset);
172168
}
173169
let reg_type = layout.llvm_type(bx);
174170
let reg_value = bx.load(reg_type, reg_addr, layout.align.abi);
@@ -210,14 +206,10 @@ fn emit_s390x_va_arg<'ll, 'tcx>(
210206
let i64_offset = 8;
211207
let ptr_offset = 8;
212208
let gpr = va_list_addr;
213-
let fpr = bx.inbounds_gep(bx.type_i8(), va_list_addr, &[bx.cx.const_usize(i64_offset)]);
214-
let overflow_arg_area =
215-
bx.inbounds_gep(bx.type_i8(), va_list_addr, &[bx.cx.const_usize(2 * i64_offset)]);
216-
let reg_save_area = bx.inbounds_gep(
217-
bx.type_i8(),
218-
va_list_addr,
219-
&[bx.cx.const_usize(2 * i64_offset + ptr_offset)],
220-
);
209+
let fpr = bx.inbounds_ptradd(va_list_addr, bx.cx.const_usize(i64_offset));
210+
let overflow_arg_area = bx.inbounds_ptradd(va_list_addr, bx.cx.const_usize(2 * i64_offset));
211+
let reg_save_area =
212+
bx.inbounds_ptradd(va_list_addr, bx.cx.const_usize(2 * i64_offset + ptr_offset));
221213

222214
let layout = bx.cx.layout_of(target_ty);
223215

@@ -248,7 +240,7 @@ fn emit_s390x_va_arg<'ll, 'tcx>(
248240
let reg_ptr_v = bx.load(bx.type_ptr(), reg_save_area, dl.pointer_align.abi);
249241
let scaled_reg_count = bx.mul(reg_count_v, bx.const_u64(8));
250242
let reg_off = bx.add(scaled_reg_count, bx.const_u64(reg_save_index * 8 + reg_padding));
251-
let reg_addr = bx.gep(bx.type_i8(), reg_ptr_v, &[reg_off]);
243+
let reg_addr = bx.ptradd(reg_ptr_v, reg_off);
252244

253245
// Update the register count.
254246
let new_reg_count_v = bx.add(reg_count_v, bx.const_u64(1));
@@ -262,11 +254,11 @@ fn emit_s390x_va_arg<'ll, 'tcx>(
262254
let arg_ptr_v =
263255
bx.load(bx.type_ptr(), overflow_arg_area, bx.tcx().data_layout.pointer_align.abi);
264256
let arg_off = bx.const_u64(padding);
265-
let mem_addr = bx.gep(bx.type_i8(), arg_ptr_v, &[arg_off]);
257+
let mem_addr = bx.ptradd(arg_ptr_v, arg_off);
266258

267259
// Update the argument overflow area pointer.
268260
let arg_size = bx.cx().const_u64(padded_size);
269-
let new_arg_ptr_v = bx.inbounds_gep(bx.type_i8(), arg_ptr_v, &[arg_size]);
261+
let new_arg_ptr_v = bx.inbounds_ptradd(arg_ptr_v, arg_size);
270262
bx.store(new_arg_ptr_v, overflow_arg_area, dl.pointer_align.abi);
271263
bx.br(end);
272264

compiler/rustc_codegen_ssa/src/mir/operand.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandValue<V> {
437437
let align = dest.align;
438438
bx.store_with_flags(val, dest.llval, align, flags);
439439

440-
let llptr =
441-
bx.inbounds_gep(bx.type_i8(), dest.llval, &[bx.const_usize(b_offset.bytes())]);
440+
let llptr = bx.inbounds_ptradd(dest.llval, bx.const_usize(b_offset.bytes()));
442441
let val = bx.from_immediate(b);
443442
let align = dest.align.restrict_for_offset(b_offset);
444443
bx.store_with_flags(val, llptr, align, flags);
@@ -476,7 +475,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandValue<V> {
476475
let address = bx.ptrtoint(alloca, bx.type_isize());
477476
let neg_address = bx.neg(address);
478477
let offset = bx.and(neg_address, align_minus_1);
479-
let dst = bx.inbounds_gep(bx.type_i8(), alloca, &[offset]);
478+
let dst = bx.inbounds_ptradd(alloca, offset);
480479
bx.memcpy(dst, min_align, llptr, min_align, size, MemFlags::empty());
481480

482481
// Store the allocated region and the extra to the indirect place.

compiler/rustc_codegen_ssa/src/mir/place.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
105105
let llval = if offset.bytes() == 0 {
106106
self.llval
107107
} else {
108-
bx.inbounds_gep(bx.type_i8(), self.llval, &[bx.const_usize(offset.bytes())])
108+
bx.inbounds_ptradd(self.llval, bx.const_usize(offset.bytes()))
109109
};
110110
PlaceRef {
111111
llval,
@@ -164,7 +164,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
164164
debug!("struct_field_ptr: DST field offset: {:?}", offset);
165165

166166
// Adjust pointer.
167-
let ptr = bx.gep(bx.cx().type_i8(), self.llval, &[offset]);
167+
let ptr = bx.ptradd(self.llval, offset);
168168

169169
PlaceRef { llval: ptr, llextra: self.llextra, layout: field, align: effective_field_align }
170170
}

compiler/rustc_codegen_ssa/src/traits/builder.rs

+6
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ pub trait BuilderMethods<'a, 'tcx>:
190190
ptr: Self::Value,
191191
indices: &[Self::Value],
192192
) -> Self::Value;
193+
fn ptradd(&mut self, ptr: Self::Value, offset: Self::Value) -> Self::Value {
194+
self.gep(self.cx().type_i8(), ptr, &[offset])
195+
}
196+
fn inbounds_ptradd(&mut self, ptr: Self::Value, offset: Self::Value) -> Self::Value {
197+
self.inbounds_gep(self.cx().type_i8(), ptr, &[offset])
198+
}
193199

194200
fn trunc(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;
195201
fn sext(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;

0 commit comments

Comments
 (0)