Skip to content

Commit 658b758

Browse files
committed
move intrinsic handling to misc to reuse code, refactoring 2/2
1 parent 27639da commit 658b758

File tree

2 files changed

+68
-75
lines changed

2 files changed

+68
-75
lines changed

compiler/rustc_mir_build/src/builder/matches/test.rs

Lines changed: 23 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use tracing::{debug, instrument};
2222

2323
use crate::builder::matches::util::Range;
2424
use crate::builder::matches::{Candidate, MatchPairTree, Test, TestBranch, TestCase, TestKind};
25+
use crate::builder::misc::SpannedCallOperandsExt;
2526
use crate::builder::{BlockAnd, BlockAndExtension, Builder, PlaceBuilder};
2627

2728
impl<'a, 'tcx> Builder<'a, 'tcx> {
@@ -311,73 +312,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
311312
}
312313
}
313314

314-
fn intrinsic_offset(
315-
&mut self,
316-
block: BasicBlock,
317-
ptr: Operand<'tcx>,
318-
offset: Operand<'tcx>,
319-
span: Span,
320-
) -> BlockAnd<Place<'tcx>> {
321-
let tcx = self.tcx;
322-
let source_info = self.source_info(span);
323-
let ptr_ty = ptr.ty(&self.local_decls, tcx);
324-
let pointee_ty = ptr_ty.pointee();
325-
let func = Operand::function_handle(
326-
tcx,
327-
tcx.require_lang_item(LangItem::Offset, Some(span)),
328-
[pointee_ty.into()],
329-
span,
330-
);
331-
332-
let out = self.temp(ptr_ty, span);
333-
let next_block = self.cfg.start_new_block();
334-
self.cfg.terminate(block, source_info, TerminatorKind::Call {
335-
func,
336-
args: [Spanned { node: ptr, span }, Spanned { node: offset, span }].into(),
337-
destination: out,
338-
target: Some(next_block),
339-
unwind: UnwindAction::Continue,
340-
call_source: CallSource::Misc,
341-
fn_span: span,
342-
});
343-
344-
next_block.and(out)
345-
}
346-
347-
fn intrinsic_aggregate_raw_ptr(
348-
&mut self,
349-
block: BasicBlock,
350-
ptr: Operand<'tcx>,
351-
data: Operand<'tcx>,
352-
span: Span,
353-
) -> BlockAnd<Place<'tcx>> {
354-
let tcx = self.tcx;
355-
let source_info = self.source_info(span);
356-
let ptr_ty = ptr.ty(&self.local_decls, tcx);
357-
let pointee_ty = ptr_ty.pointee();
358-
let func = Operand::function_handle(
359-
tcx,
360-
tcx.require_lang_item(LangItem::AggregateRawPtr, Some(span)),
361-
[pointee_ty.into()],
362-
span,
363-
);
364-
365-
let aggregate_ptr_ty = Ty::new_ptr(tcx, Ty::new_slice(tcx, pointee_ty), Mutability::Not);
366-
let out = self.temp(aggregate_ptr_ty, span);
367-
let next_block = self.cfg.start_new_block();
368-
self.cfg.terminate(block, source_info, TerminatorKind::Call {
369-
func,
370-
args: [Spanned { node: ptr, span }, Spanned { node: data, span }].into(),
371-
destination: out,
372-
target: Some(next_block),
373-
unwind: UnwindAction::Continue,
374-
call_source: CallSource::Misc,
375-
fn_span: span,
376-
});
377-
378-
next_block.and(out)
379-
}
380-
381315
fn subslice_sized_range(
382316
&mut self,
383317
mut block: BasicBlock,
@@ -431,21 +365,35 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
431365
Rvalue::Cast(CastKind::PtrToPtr, Operand::Copy(temp_source_ptr), elem_ptr_ty),
432366
);
433367

434-
let updated_ptr = unpack!(
435-
block = self.intrinsic_offset(block, Operand::Copy(temp_elem_ptr), ptr_offset, span)
368+
let updated_ptr = self.temp(elem_ptr_ty, span);
369+
370+
unpack!(
371+
block = self.call_intrinsic(
372+
block,
373+
span,
374+
LangItem::Offset,
375+
&[elem_ty],
376+
span.args([Operand::Copy(temp_elem_ptr), ptr_offset]),
377+
updated_ptr
378+
)
436379
);
380+
437381
let slice_len = self.literal_operand(span, slice_len);
382+
let slice_ptr_ty = Ty::new_imm_ptr(tcx, Ty::new_slice(tcx, elem_ty));
383+
let subslice_ptr = self.temp(slice_ptr_ty, span);
438384

439-
let subslice_ptr = unpack!(
440-
block = self.intrinsic_aggregate_raw_ptr(
385+
unpack!(
386+
block = self.call_intrinsic(
441387
block,
442-
Operand::Copy(updated_ptr),
443-
slice_len,
444-
span
388+
span,
389+
LangItem::AggregateRawPtr,
390+
&[elem_ty],
391+
span.args([Operand::Copy(updated_ptr), slice_len]),
392+
subslice_ptr
445393
)
446394
);
447-
let out = PlaceBuilder::from(subslice_ptr).project(PlaceElem::Deref).to_place(self);
448395

396+
let out = PlaceBuilder::from(subslice_ptr).project(PlaceElem::Deref).to_place(self);
449397
block.and(out)
450398
}
451399

compiler/rustc_mir_build/src/builder/misc.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
//! Miscellaneous builder routines that are not specific to building any particular
22
//! kind of thing.
33
4+
use rustc_hir::LangItem;
45
use rustc_middle::mir::*;
56
use rustc_middle::ty::{self, Ty};
67
use rustc_span::Span;
8+
use rustc_span::source_map::Spanned;
79
use rustc_trait_selection::infer::InferCtxtExt;
810
use tracing::debug;
911

12+
use super::{BlockAnd, BlockAndExtension};
1013
use crate::builder::Builder;
1114

1215
impl<'a, 'tcx> Builder<'a, 'tcx> {
@@ -62,4 +65,46 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6265
Operand::Move(place)
6366
}
6467
}
68+
69+
pub(crate) fn call_intrinsic(
70+
&mut self,
71+
block: BasicBlock,
72+
span: Span,
73+
intrinsic: LangItem,
74+
type_args: &[Ty<'tcx>],
75+
args: Box<[Spanned<Operand<'tcx>>]>,
76+
output: Place<'tcx>,
77+
) -> BlockAnd<()> {
78+
let tcx = self.tcx;
79+
let source_info = self.source_info(span);
80+
let func = Operand::function_handle(
81+
tcx,
82+
tcx.require_lang_item(intrinsic, Some(span)),
83+
type_args.iter().copied().map(Into::into),
84+
span,
85+
);
86+
87+
let next_block = self.cfg.start_new_block();
88+
self.cfg.terminate(block, source_info, TerminatorKind::Call {
89+
func,
90+
args,
91+
destination: output,
92+
target: Some(next_block),
93+
unwind: UnwindAction::Continue,
94+
call_source: CallSource::Misc,
95+
fn_span: span,
96+
});
97+
98+
next_block.unit()
99+
}
100+
}
101+
102+
pub(crate) trait SpannedCallOperandsExt<'tcx> {
103+
fn args(&self, list: impl IntoIterator<Item = Operand<'tcx>>) -> Box<[Spanned<Operand<'tcx>>]>;
104+
}
105+
106+
impl<'tcx> SpannedCallOperandsExt<'tcx> for Span {
107+
fn args(&self, list: impl IntoIterator<Item = Operand<'tcx>>) -> Box<[Spanned<Operand<'tcx>>]> {
108+
list.into_iter().map(move |arg| Spanned { node: arg, span: *self }).collect()
109+
}
65110
}

0 commit comments

Comments
 (0)