Skip to content

Commit 945e153

Browse files
committed
Auto merge of #134117 - DianQK:gep-i8, r=<try>
[WIP] Canonicalize GEPs Fixes #133979. r? ghost
2 parents b597d2a + 23cc55f commit 945e153

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

compiler/rustc_codegen_ssa/src/mir/place.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,18 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
422422
layout.size
423423
};
424424

425-
let llval = bx.inbounds_gep(bx.cx().backend_type(self.layout), self.val.llval, &[
426-
bx.cx().const_usize(0),
427-
llindex,
428-
]);
425+
let llval = {
426+
let pointee_layout = layout;
427+
let bytes = pointee_layout.size.align_to(pointee_layout.align.abi).bytes();
428+
let rhs = if bytes == 1 {
429+
llindex
430+
} else {
431+
let lhs = llindex;
432+
let rhs = bx.cx().const_usize(bytes);
433+
bx.unchecked_umul(lhs, rhs)
434+
};
435+
bx.inbounds_ptradd(self.val.llval, rhs)
436+
};
429437
let align = self.val.align.restrict_for_offset(offset);
430438
PlaceValue::new_sized(llval, align).with_type(layout)
431439
}

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -925,8 +925,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
925925
// so offsetting a pointer to ZST is a noop.
926926
lhs
927927
} else {
928-
let llty = bx.cx().backend_type(pointee_layout);
929-
bx.inbounds_gep(llty, lhs, &[rhs])
928+
let bytes = pointee_layout.size.align_to(pointee_layout.align.abi).bytes();
929+
let rhs = if bytes == 1 {
930+
rhs
931+
} else {
932+
let lhs = rhs;
933+
let rhs = bx.cx().const_usize(bytes);
934+
bx.unchecked_umul(lhs, rhs)
935+
};
936+
bx.inbounds_ptradd(lhs, rhs)
930937
}
931938
}
932939
mir::BinOp::Shl | mir::BinOp::ShlUnchecked => {

0 commit comments

Comments
 (0)