Skip to content

Commit 121fd41

Browse files
committed
Prevent redundant casts and create fewer LLVM builders
1 parent f058493 commit 121fd41

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+38-30
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
166166
}
167167

168168
fn switch_to_block(&mut self, llbb: Self::BasicBlock) {
169-
*self = Self::build(self.cx, llbb)
169+
unsafe { llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb) }
170170
}
171171

172172
fn ret_void(&mut self) {
@@ -412,10 +412,8 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
412412
}
413413

414414
fn alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
415-
let mut bx = Builder::with_cx(self.cx);
416-
bx.position_at_start(unsafe { llvm::LLVMGetFirstBasicBlock(self.llfn()) });
417415
unsafe {
418-
let alloca = llvm::LLVMBuildAlloca(bx.llbuilder, ty, UNNAMED);
416+
let alloca = llvm::LLVMBuildAlloca(self.llbuilder, ty, UNNAMED);
419417
llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
420418
alloca
421419
}
@@ -566,33 +564,31 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
566564
let start = dest.project_index(self, zero).llval;
567565
let end = dest.project_index(self, count).llval;
568566

567+
let intro_bb = self.llbb();
569568
let header_bb = self.append_sibling_block("repeat_loop_header");
570569
let body_bb = self.append_sibling_block("repeat_loop_body");
571570
let next_bb = self.append_sibling_block("repeat_loop_next");
572571

573572
self.br(header_bb);
574573

575-
let mut header_bx = Self::build(self.cx, header_bb);
576-
let current = header_bx.phi(self.val_ty(start), &[start], &[self.llbb()]);
574+
self.switch_to_block(header_bb);
575+
let current = self.phi(self.val_ty(start), &[start], &[intro_bb]);
577576

578-
let keep_going = header_bx.icmp(IntPredicate::IntNE, current, end);
579-
header_bx.cond_br(keep_going, body_bb, next_bb);
577+
let keep_going = self.icmp(IntPredicate::IntNE, current, end);
578+
self.cond_br(keep_going, body_bb, next_bb);
580579

581-
let mut body_bx = Self::build(self.cx, body_bb);
580+
self.switch_to_block(body_bb);
582581
let align = dest.align.restrict_for_offset(dest.layout.field(self.cx(), 0).size);
583-
cg_elem
584-
.val
585-
.store(&mut body_bx, PlaceRef::new_sized_aligned(current, cg_elem.layout, align));
586-
587-
let next = body_bx.inbounds_gep(
588-
self.backend_type(cg_elem.layout),
589-
current,
590-
&[self.const_usize(1)],
591-
);
592-
body_bx.br(header_bb);
593-
header_bx.add_incoming_to_phi(current, next, body_bb);
582+
cg_elem.val.store(self, PlaceRef::new_sized_aligned(current, cg_elem.layout, align));
583+
584+
let next =
585+
self.inbounds_gep(self.backend_type(cg_elem.layout), current, &[self.const_usize(1)]);
586+
self.br(header_bb);
594587

595-
*self = Self::build(self.cx, next_bb);
588+
self.switch_to_block(header_bb);
589+
self.add_incoming_to_phi(current, next, body_bb);
590+
591+
self.switch_to_block(next_bb);
596592
}
597593

598594
fn range_metadata(&mut self, load: &'ll Value, range: WrappingRange) {
@@ -819,15 +815,33 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
819815
}
820816

821817
fn bitcast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
822-
unsafe { llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, UNNAMED) }
818+
unsafe {
819+
if llvm::LLVMTypeOf(val) != dest_ty {
820+
llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, UNNAMED)
821+
} else {
822+
val
823+
}
824+
}
823825
}
824826

825827
fn intcast(&mut self, val: &'ll Value, dest_ty: &'ll Type, is_signed: bool) -> &'ll Value {
826-
unsafe { llvm::LLVMRustBuildIntCast(self.llbuilder, val, dest_ty, is_signed) }
828+
unsafe {
829+
if llvm::LLVMTypeOf(val) != dest_ty {
830+
llvm::LLVMRustBuildIntCast(self.llbuilder, val, dest_ty, is_signed)
831+
} else {
832+
val
833+
}
834+
}
827835
}
828836

829837
fn pointercast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
830-
unsafe { llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, UNNAMED) }
838+
unsafe {
839+
if llvm::LLVMTypeOf(val) != dest_ty {
840+
llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, UNNAMED)
841+
} else {
842+
val
843+
}
844+
}
831845
}
832846

833847
/* Comparisons */
@@ -1206,12 +1220,6 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
12061220
unsafe { llvm::LLVMGetBasicBlockParent(self.llbb()) }
12071221
}
12081222

1209-
fn position_at_start(&mut self, llbb: &'ll BasicBlock) {
1210-
unsafe {
1211-
llvm::LLVMRustPositionBuilderAtStart(self.llbuilder, llbb);
1212-
}
1213-
}
1214-
12151223
fn align_metadata(&mut self, load: &'ll Value, align: Align) {
12161224
unsafe {
12171225
let v = [self.cx.const_u64(align.bytes())];

0 commit comments

Comments
 (0)