Skip to content

Commit 31cc4c0

Browse files
committed
Emit getelementptr inbounds nuw for pointer::add()
1 parent 5e9d8a7 commit 31cc4c0

File tree

7 files changed

+55
-15
lines changed

7 files changed

+55
-15
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ use crate::abi::FnAbiLlvmExt;
3232
use crate::attributes;
3333
use crate::common::Funclet;
3434
use crate::context::{CodegenCx, SimpleCx};
35-
use crate::llvm::{self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, GEPNoWrapFlags, Metadata, True};
35+
use crate::llvm::{
36+
self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, GEPNoWrapFlags, Metadata, True,
37+
};
3638
use crate::type_::Type;
3739
use crate::type_of::LayoutLlvmExt;
3840
use crate::value::Value;
@@ -939,6 +941,25 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
939941
}
940942
}
941943

944+
fn inbounds_nuw_gep(
945+
&mut self,
946+
ty: &'ll Type,
947+
ptr: &'ll Value,
948+
indices: &[&'ll Value],
949+
) -> &'ll Value {
950+
unsafe {
951+
llvm::LLVMBuildGEPWithNoWrapFlags(
952+
self.llbuilder,
953+
ty,
954+
ptr,
955+
indices.as_ptr(),
956+
indices.len() as c_uint,
957+
UNNAMED,
958+
GEPNoWrapFlags::InBounds | GEPNoWrapFlags::NUW,
959+
)
960+
}
961+
}
962+
942963
/* Casts */
943964
fn trunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
944965
unsafe { llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty, UNNAMED) }

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -664,9 +664,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
664664
lhs.layout.ty,
665665
),
666666

667-
(OperandValue::Immediate(lhs_val), OperandValue::Immediate(rhs_val)) => {
668-
self.codegen_scalar_binop(bx, op, lhs_val, rhs_val, lhs.layout.ty)
669-
}
667+
(OperandValue::Immediate(lhs_val), OperandValue::Immediate(rhs_val)) => self
668+
.codegen_scalar_binop(
669+
bx,
670+
op,
671+
lhs_val,
672+
rhs_val,
673+
lhs.layout.ty,
674+
rhs.layout.ty,
675+
),
670676

671677
_ => bug!(),
672678
};
@@ -887,10 +893,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
887893
op: mir::BinOp,
888894
lhs: Bx::Value,
889895
rhs: Bx::Value,
890-
input_ty: Ty<'tcx>,
896+
lhs_ty: Ty<'tcx>,
897+
rhs_ty: Ty<'tcx>,
891898
) -> Bx::Value {
892-
let is_float = input_ty.is_floating_point();
893-
let is_signed = input_ty.is_signed();
899+
let is_float = lhs_ty.is_floating_point();
900+
let is_signed = lhs_ty.is_signed();
894901
match op {
895902
mir::BinOp::Add => {
896903
if is_float {
@@ -956,17 +963,21 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
956963
mir::BinOp::BitAnd => bx.and(lhs, rhs),
957964
mir::BinOp::BitXor => bx.xor(lhs, rhs),
958965
mir::BinOp::Offset => {
959-
let pointee_type = input_ty
966+
let pointee_type = lhs_ty
960967
.builtin_deref(true)
961-
.unwrap_or_else(|| bug!("deref of non-pointer {:?}", input_ty));
968+
.unwrap_or_else(|| bug!("deref of non-pointer {:?}", lhs_ty));
962969
let pointee_layout = bx.cx().layout_of(pointee_type);
963970
if pointee_layout.is_zst() {
964971
// `Offset` works in terms of the size of pointee,
965972
// so offsetting a pointer to ZST is a noop.
966973
lhs
967974
} else {
968975
let llty = bx.cx().backend_type(pointee_layout);
969-
bx.inbounds_gep(llty, lhs, &[rhs])
976+
if !rhs_ty.is_signed() {
977+
bx.inbounds_nuw_gep(llty, lhs, &[rhs])
978+
} else {
979+
bx.inbounds_gep(llty, lhs, &[rhs])
980+
}
970981
}
971982
}
972983
mir::BinOp::Shl | mir::BinOp::ShlUnchecked => {

compiler/rustc_codegen_ssa/src/traits/builder.rs

+8
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,14 @@ pub trait BuilderMethods<'a, 'tcx>:
332332
ptr: Self::Value,
333333
indices: &[Self::Value],
334334
) -> Self::Value;
335+
fn inbounds_nuw_gep(
336+
&mut self,
337+
ty: Self::Type,
338+
ptr: Self::Value,
339+
indices: &[Self::Value],
340+
) -> Self::Value {
341+
self.inbounds_gep(ty, ptr, indices)
342+
}
335343
fn ptradd(&mut self, ptr: Self::Value, offset: Self::Value) -> Self::Value {
336344
self.gep(self.cx().type_i8(), ptr, &[offset])
337345
}

tests/codegen/gep-index.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn index_on_struct(a: &[Foo], index: usize) -> &Foo {
1818
// CHECK-LABEL: @offset_on_struct(
1919
#[no_mangle]
2020
fn offset_on_struct(a: *const Foo, index: usize) -> *const Foo {
21-
// CHECK: getelementptr inbounds %Foo, ptr %a, {{i64|i32}} %index
21+
// CHECK: getelementptr inbounds{{( nuw)?}} %Foo, ptr %a, {{i64|i32}} %index
2222
unsafe { a.add(index) }
2323
}
2424

@@ -32,6 +32,6 @@ fn index_on_i32(a: &[i32], index: usize) -> &i32 {
3232
// CHECK-LABEL: @offset_on_i32(
3333
#[no_mangle]
3434
fn offset_on_i32(a: *const i32, index: usize) -> *const i32 {
35-
// CHECK: getelementptr inbounds i32, ptr %a, {{i64|i32}} %index
35+
// CHECK: getelementptr inbounds{{( nuw)?}} i32, ptr %a, {{i64|i32}} %index
3636
unsafe { a.add(index) }
3737
}

tests/codegen/intrinsics/offset.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub unsafe fn offset_isize(p: *const u32, d: isize) -> *const u32 {
2727
// CHECK-SAME: (ptr noundef %p, [[SIZE]] noundef %d)
2828
#[no_mangle]
2929
pub unsafe fn offset_usize(p: *const u64, d: usize) -> *const u64 {
30-
// CHECK: %[[R:.*]] = getelementptr inbounds i64, ptr %p, [[SIZE]] %d
30+
// CHECK: %[[R:.*]] = getelementptr inbounds{{( nuw)?}} i64, ptr %p, [[SIZE]] %d
3131
// CHECK-NEXT: ret ptr %[[R]]
3232
offset(p, d)
3333
}

tests/codegen/intrinsics/ptr_metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub unsafe fn dyn_byte_offset(
2828
p: *const dyn std::fmt::Debug,
2929
n: usize,
3030
) -> *const dyn std::fmt::Debug {
31-
// CHECK: %[[Q:.+]] = getelementptr inbounds i8, ptr %p.0, i64 %n
31+
// CHECK: %[[Q:.+]] = getelementptr inbounds{{( nuw)?}} i8, ptr %p.0, i64 %n
3232
// CHECK: %[[TEMP1:.+]] = insertvalue { ptr, ptr } poison, ptr %[[Q]], 0
3333
// CHECK: %[[TEMP2:.+]] = insertvalue { ptr, ptr } %[[TEMP1]], ptr %p.1, 1
3434
// CHECK: ret { ptr, ptr } %[[TEMP2]]

tests/codegen/ptr-arithmetic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// CHECK-SAME: [[WORD:i[0-9]+]] noundef %n)
77
#[no_mangle]
88
pub unsafe fn i32_add(p: *const i32, n: usize) -> *const i32 {
9-
// CHECK: %[[TEMP:.+]] = getelementptr inbounds i32, ptr %p, [[WORD]] %n
9+
// CHECK: %[[TEMP:.+]] = getelementptr inbounds{{( nuw)?}} i32, ptr %p, [[WORD]] %n
1010
// CHECK: ret ptr %[[TEMP]]
1111
p.add(n)
1212
}

0 commit comments

Comments
 (0)