Skip to content

Commit 3eb5035

Browse files
committed
rustc_codegen_llvm: use safe references for BasicBlock.
1 parent f375185 commit 3eb5035

File tree

4 files changed

+54
-55
lines changed

4 files changed

+54
-55
lines changed

src/librustc_codegen_llvm/builder.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
use llvm::{AtomicRmwBinOp, AtomicOrdering, SynchronizationScope, AsmDialect};
1414
use llvm::{Opcode, IntPredicate, RealPredicate, False, OperandBundleDef};
15-
use llvm::{self, BasicBlockRef};
15+
use llvm::{self, BasicBlock};
1616
use common::*;
1717
use type_::Type;
1818
use value::Value;
@@ -102,7 +102,7 @@ impl Builder<'a, 'll, 'tcx> {
102102
}
103103
}
104104

105-
pub fn llbb(&self) -> BasicBlockRef {
105+
pub fn llbb(&self) -> &'ll BasicBlock {
106106
unsafe {
107107
llvm::LLVMGetInsertBlock(self.llbuilder)
108108
}
@@ -134,13 +134,13 @@ impl Builder<'a, 'll, 'tcx> {
134134
}
135135
}
136136

137-
pub fn position_at_end(&self, llbb: BasicBlockRef) {
137+
pub fn position_at_end(&self, llbb: &'ll BasicBlock) {
138138
unsafe {
139139
llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb);
140140
}
141141
}
142142

143-
pub fn position_at_start(&self, llbb: BasicBlockRef) {
143+
pub fn position_at_start(&self, llbb: &'ll BasicBlock) {
144144
unsafe {
145145
llvm::LLVMRustPositionBuilderAtStart(self.llbuilder, llbb);
146146
}
@@ -168,21 +168,21 @@ impl Builder<'a, 'll, 'tcx> {
168168
}
169169
}
170170

171-
pub fn br(&self, dest: BasicBlockRef) {
171+
pub fn br(&self, dest: &'ll BasicBlock) {
172172
self.count_insn("br");
173173
unsafe {
174174
llvm::LLVMBuildBr(self.llbuilder, dest);
175175
}
176176
}
177177

178-
pub fn cond_br(&self, cond: &'ll Value, then_llbb: BasicBlockRef, else_llbb: BasicBlockRef) {
178+
pub fn cond_br(&self, cond: &'ll Value, then_llbb: &'ll BasicBlock, else_llbb: &'ll BasicBlock) {
179179
self.count_insn("condbr");
180180
unsafe {
181181
llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
182182
}
183183
}
184184

185-
pub fn switch(&self, v: &'ll Value, else_llbb: BasicBlockRef, num_cases: usize) -> &'ll Value {
185+
pub fn switch(&self, v: &'ll Value, else_llbb: &'ll BasicBlock, num_cases: usize) -> &'ll Value {
186186
unsafe {
187187
llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, num_cases as c_uint)
188188
}
@@ -198,8 +198,8 @@ impl Builder<'a, 'll, 'tcx> {
198198
pub fn invoke(&self,
199199
llfn: &'ll Value,
200200
args: &[&'ll Value],
201-
then: BasicBlockRef,
202-
catch: BasicBlockRef,
201+
then: &'ll BasicBlock,
202+
catch: &'ll BasicBlock,
203203
bundle: Option<&OperandBundleDef>) -> &'ll Value {
204204
self.count_insn("invoke");
205205

@@ -830,7 +830,7 @@ impl Builder<'a, 'll, 'tcx> {
830830
}
831831
}
832832

833-
pub fn phi(&self, ty: &'ll Type, vals: &[&'ll Value], bbs: &[BasicBlockRef]) -> &'ll Value {
833+
pub fn phi(&self, ty: &'ll Type, vals: &[&'ll Value], bbs: &[&'ll BasicBlock]) -> &'ll Value {
834834
assert_eq!(vals.len(), bbs.len());
835835
let phi = self.empty_phi(ty);
836836
self.count_insn("addincoming");
@@ -1167,10 +1167,11 @@ impl Builder<'a, 'll, 'tcx> {
11671167
ret.expect("LLVM does not have support for cleanuppad")
11681168
}
11691169

1170-
pub fn cleanup_ret(&self, cleanup: &'ll Value,
1171-
unwind: Option<BasicBlockRef>) -> &'ll Value {
1170+
pub fn cleanup_ret(
1171+
&self, cleanup: &'ll Value,
1172+
unwind: Option<&'ll BasicBlock>,
1173+
) -> &'ll Value {
11721174
self.count_insn("cleanupret");
1173-
let unwind = unwind.and_then(NonNull::new);
11741175
let ret = unsafe {
11751176
llvm::LLVMRustBuildCleanupRet(self.llbuilder, cleanup, unwind)
11761177
};
@@ -1190,7 +1191,7 @@ impl Builder<'a, 'll, 'tcx> {
11901191
ret.expect("LLVM does not have support for catchpad")
11911192
}
11921193

1193-
pub fn catch_ret(&self, pad: &'ll Value, unwind: BasicBlockRef) -> &'ll Value {
1194+
pub fn catch_ret(&self, pad: &'ll Value, unwind: &'ll BasicBlock) -> &'ll Value {
11941195
self.count_insn("catchret");
11951196
let ret = unsafe {
11961197
llvm::LLVMRustBuildCatchRet(self.llbuilder, pad, unwind)
@@ -1201,11 +1202,10 @@ impl Builder<'a, 'll, 'tcx> {
12011202
pub fn catch_switch(
12021203
&self,
12031204
parent: Option<&'ll Value>,
1204-
unwind: Option<BasicBlockRef>,
1205+
unwind: Option<&'ll BasicBlock>,
12051206
num_handlers: usize,
12061207
) -> &'ll Value {
12071208
self.count_insn("catchswitch");
1208-
let unwind = unwind.and_then(NonNull::new);
12091209
let name = CString::new("catchswitch").unwrap();
12101210
let ret = unsafe {
12111211
llvm::LLVMRustBuildCatchSwitch(self.llbuilder, parent, unwind,
@@ -1215,7 +1215,7 @@ impl Builder<'a, 'll, 'tcx> {
12151215
ret.expect("LLVM does not have support for catchswitch")
12161216
}
12171217

1218-
pub fn add_handler(&self, catch_switch: &'ll Value, handler: BasicBlockRef) {
1218+
pub fn add_handler(&self, catch_switch: &'ll Value, handler: &'ll BasicBlock) {
12191219
unsafe {
12201220
llvm::LLVMRustAddHandler(catch_switch, handler);
12211221
}
@@ -1260,13 +1260,13 @@ impl Builder<'a, 'll, 'tcx> {
12601260
}
12611261
}
12621262

1263-
pub fn add_case(&self, s: &'ll Value, on_val: &'ll Value, dest: BasicBlockRef) {
1263+
pub fn add_case(&self, s: &'ll Value, on_val: &'ll Value, dest: &'ll BasicBlock) {
12641264
unsafe {
12651265
llvm::LLVMAddCase(s, on_val, dest)
12661266
}
12671267
}
12681268

1269-
pub fn add_incoming_to_phi(&self, phi: &'ll Value, val: &'ll Value, bb: BasicBlockRef) {
1269+
pub fn add_incoming_to_phi(&self, phi: &'ll Value, val: &'ll Value, bb: &'ll BasicBlock) {
12701270
self.count_insn("addincoming");
12711271
unsafe {
12721272
llvm::LLVMAddIncoming(phi, &val, &bb, 1 as c_uint);

src/librustc_codegen_llvm/llvm/ffi.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,6 @@ extern { pub type Type; }
381381
extern { pub type Value; }
382382
extern { pub type Metadata; }
383383
extern { pub type BasicBlock; }
384-
pub type BasicBlockRef = *mut BasicBlock;
385384
extern { pub type Builder; }
386385
extern { pub type MemoryBuffer; }
387386
pub type MemoryBufferRef = *mut MemoryBuffer;
@@ -716,18 +715,18 @@ extern "C" {
716715
pub fn LLVMGetParam(Fn: &Value, Index: c_uint) -> &Value;
717716

718717
// Operations on basic blocks
719-
pub fn LLVMBasicBlockAsValue(BB: BasicBlockRef) -> &'a Value;
720-
pub fn LLVMGetBasicBlockParent(BB: BasicBlockRef) -> &'a Value;
718+
pub fn LLVMBasicBlockAsValue(BB: &BasicBlock) -> &Value;
719+
pub fn LLVMGetBasicBlockParent(BB: &BasicBlock) -> &Value;
721720
pub fn LLVMAppendBasicBlockInContext(C: &'a Context,
722721
Fn: &'a Value,
723722
Name: *const c_char)
724-
-> BasicBlockRef;
725-
pub fn LLVMDeleteBasicBlock(BB: BasicBlockRef);
723+
-> &'a BasicBlock;
724+
pub fn LLVMDeleteBasicBlock(BB: &BasicBlock);
726725

727726
// Operations on instructions
728-
pub fn LLVMGetInstructionParent(Inst: &Value) -> BasicBlockRef;
729-
pub fn LLVMGetFirstBasicBlock(Fn: &Value) -> BasicBlockRef;
730-
pub fn LLVMGetFirstInstruction(BB: BasicBlockRef) -> &'a Value;
727+
pub fn LLVMGetInstructionParent(Inst: &Value) -> &BasicBlock;
728+
pub fn LLVMGetFirstBasicBlock(Fn: &Value) -> &BasicBlock;
729+
pub fn LLVMGetFirstInstruction(BB: &BasicBlock) -> &'a Value;
731730
pub fn LLVMInstructionEraseFromParent(Inst: &Value);
732731

733732
// Operations on call sites
@@ -745,15 +744,15 @@ extern "C" {
745744
// Operations on phi nodes
746745
pub fn LLVMAddIncoming(PhiNode: &'a Value,
747746
IncomingValues: *const &'a Value,
748-
IncomingBlocks: *const BasicBlockRef,
747+
IncomingBlocks: *const &'a BasicBlock,
749748
Count: c_uint);
750749

751750
// Instruction builders
752751
pub fn LLVMCreateBuilderInContext(C: &Context) -> &Builder;
753-
pub fn LLVMPositionBuilder(Builder: &'a Builder, Block: BasicBlockRef, Instr: &'a Value);
752+
pub fn LLVMPositionBuilder(Builder: &'a Builder, Block: &'a BasicBlock, Instr: &'a Value);
754753
pub fn LLVMPositionBuilderBefore(Builder: &'a Builder, Instr: &'a Value);
755-
pub fn LLVMPositionBuilderAtEnd(Builder: &Builder, Block: BasicBlockRef);
756-
pub fn LLVMGetInsertBlock(Builder: &Builder) -> BasicBlockRef;
754+
pub fn LLVMPositionBuilderAtEnd(Builder: &'a Builder, Block: &'a BasicBlock);
755+
pub fn LLVMGetInsertBlock(Builder: &Builder) -> &BasicBlock;
757756
pub fn LLVMDisposeBuilder(Builder: &Builder);
758757

759758
// Metadata
@@ -765,24 +764,24 @@ extern "C" {
765764
pub fn LLVMBuildRetVoid(B: &Builder) -> &Value;
766765
pub fn LLVMBuildRet(B: &'a Builder, V: &'a Value) -> &'a Value;
767766
pub fn LLVMBuildAggregateRet(B: &'a Builder, RetVals: *const &'a Value, N: c_uint) -> &'a Value;
768-
pub fn LLVMBuildBr(B: &Builder, Dest: BasicBlockRef) -> &Value;
767+
pub fn LLVMBuildBr(B: &'a Builder, Dest: &'a BasicBlock) -> &'a Value;
769768
pub fn LLVMBuildCondBr(B: &'a Builder,
770769
If: &'a Value,
771-
Then: BasicBlockRef,
772-
Else: BasicBlockRef)
770+
Then: &'a BasicBlock,
771+
Else: &'a BasicBlock)
773772
-> &'a Value;
774773
pub fn LLVMBuildSwitch(B: &'a Builder,
775774
V: &'a Value,
776-
Else: BasicBlockRef,
775+
Else: &'a BasicBlock,
777776
NumCases: c_uint)
778777
-> &'a Value;
779778
pub fn LLVMBuildIndirectBr(B: &'a Builder, Addr: &'a Value, NumDests: c_uint) -> &'a Value;
780779
pub fn LLVMRustBuildInvoke(B: &'a Builder,
781780
Fn: &'a Value,
782781
Args: *const &'a Value,
783782
NumArgs: c_uint,
784-
Then: BasicBlockRef,
785-
Catch: BasicBlockRef,
783+
Then: &'a BasicBlock,
784+
Catch: &'a BasicBlock,
786785
Bundle: Option<NonNull<OperandBundleDef>>,
787786
Name: *const c_char)
788787
-> &'a Value;
@@ -803,26 +802,26 @@ extern "C" {
803802
-> Option<&'a Value>;
804803
pub fn LLVMRustBuildCleanupRet(B: &'a Builder,
805804
CleanupPad: &'a Value,
806-
UnwindBB: Option<NonNull<BasicBlock>>)
805+
UnwindBB: Option<&'a BasicBlock>)
807806
-> Option<&'a Value>;
808807
pub fn LLVMRustBuildCatchPad(B: &'a Builder,
809808
ParentPad: &'a Value,
810809
ArgCnt: c_uint,
811810
Args: *const &'a Value,
812811
Name: *const c_char)
813812
-> Option<&'a Value>;
814-
pub fn LLVMRustBuildCatchRet(B: &'a Builder, Pad: &'a Value, BB: BasicBlockRef) -> Option<&'a Value>;
813+
pub fn LLVMRustBuildCatchRet(B: &'a Builder, Pad: &'a Value, BB: &'a BasicBlock) -> Option<&'a Value>;
815814
pub fn LLVMRustBuildCatchSwitch(Builder: &'a Builder,
816815
ParentPad: Option<&'a Value>,
817-
BB: Option<NonNull<BasicBlock>>,
816+
BB: Option<&'a BasicBlock>,
818817
NumHandlers: c_uint,
819818
Name: *const c_char)
820819
-> Option<&'a Value>;
821-
pub fn LLVMRustAddHandler(CatchSwitch: &Value, Handler: BasicBlockRef);
820+
pub fn LLVMRustAddHandler(CatchSwitch: &'a Value, Handler: &'a BasicBlock);
822821
pub fn LLVMSetPersonalityFn(Func: &'a Value, Pers: &'a Value);
823822

824823
// Add a case to the switch instruction
825-
pub fn LLVMAddCase(Switch: &'a Value, OnVal: &'a Value, Dest: BasicBlockRef);
824+
pub fn LLVMAddCase(Switch: &'a Value, OnVal: &'a Value, Dest: &'a BasicBlock);
826825

827826
// Add a clause to the landing pad instruction
828827
pub fn LLVMAddClause(LandingPad: &'a Value, ClauseVal: &'a Value);
@@ -1503,7 +1502,7 @@ extern "C" {
15031502
AddrOps: *const i64,
15041503
AddrOpsCount: c_uint,
15051504
DL: &'a Value,
1506-
InsertAtEnd: BasicBlockRef)
1505+
InsertAtEnd: &'a BasicBlock)
15071506
-> &'a Value;
15081507

15091508
pub fn LLVMRustDIBuilderCreateEnumerator(Builder: &DIBuilder,
@@ -1691,7 +1690,7 @@ extern "C" {
16911690
-> OperandBundleDefRef;
16921691
pub fn LLVMRustFreeOperandBundleDef(Bundle: OperandBundleDefRef);
16931692

1694-
pub fn LLVMRustPositionBuilderAtStart(B: &Builder, BB: BasicBlockRef);
1693+
pub fn LLVMRustPositionBuilderAtStart(B: &'a Builder, BB: &'a BasicBlock);
16951694

16961695
pub fn LLVMRustSetComdat(M: &'a Module, V: &'a Value, Name: *const c_char);
16971696
pub fn LLVMRustUnsetComdat(V: &Value);

src/librustc_codegen_llvm/mir/block.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use llvm::{self, BasicBlockRef};
11+
use llvm::{self, BasicBlock};
1212
use rustc::middle::lang_items;
1313
use rustc::ty::{self, Ty, TypeFoldable};
1414
use rustc::ty::layout::{self, LayoutOf};
@@ -754,7 +754,7 @@ impl FunctionCx<'a, 'll, 'tcx> {
754754
/// Return the landingpad wrapper around the given basic block
755755
///
756756
/// No-op in MSVC SEH scheme.
757-
fn landing_pad_to(&mut self, target_bb: mir::BasicBlock) -> BasicBlockRef {
757+
fn landing_pad_to(&mut self, target_bb: mir::BasicBlock) -> &'ll BasicBlock {
758758
if let Some(block) = self.landing_pads[target_bb] {
759759
return block;
760760
}
@@ -765,7 +765,7 @@ impl FunctionCx<'a, 'll, 'tcx> {
765765
landing_pad
766766
}
767767

768-
fn landing_pad_uncached(&mut self, target_bb: BasicBlockRef) -> BasicBlockRef {
768+
fn landing_pad_uncached(&mut self, target_bb: &'ll BasicBlock) -> &'ll BasicBlock {
769769
if base::wants_msvc_seh(self.cx.sess()) {
770770
span_bug!(self.mir.span, "landing pad was not inserted?")
771771
}
@@ -790,7 +790,7 @@ impl FunctionCx<'a, 'll, 'tcx> {
790790
Type::struct_(cx, &[Type::i8p(cx), Type::i32(cx)], false)
791791
}
792792

793-
fn unreachable_block(&mut self) -> BasicBlockRef {
793+
fn unreachable_block(&mut self) -> &'ll BasicBlock {
794794
self.unreachable_block.unwrap_or_else(|| {
795795
let bl = self.new_block("unreachable");
796796
bl.unreachable();

src/librustc_codegen_llvm/mir/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use common::{C_i32, C_null};
1212
use libc::c_uint;
13-
use llvm::{self, BasicBlockRef};
13+
use llvm::{self, BasicBlock};
1414
use llvm::debuginfo::DIScope;
1515
use rustc::ty::{self, Ty, TypeFoldable, UpvarSubsts};
1616
use rustc::ty::layout::{LayoutOf, TyLayout};
@@ -66,7 +66,7 @@ pub struct FunctionCx<'a, 'll: 'a, 'tcx: 'll> {
6666
personality_slot: Option<PlaceRef<'ll, 'tcx>>,
6767

6868
/// A `Block` for each MIR `BasicBlock`
69-
blocks: IndexVec<mir::BasicBlock, BasicBlockRef>,
69+
blocks: IndexVec<mir::BasicBlock, &'ll BasicBlock>,
7070

7171
/// The funclet status of each basic block
7272
cleanup_kinds: IndexVec<mir::BasicBlock, analyze::CleanupKind>,
@@ -77,10 +77,10 @@ pub struct FunctionCx<'a, 'll: 'a, 'tcx: 'll> {
7777

7878
/// This stores the landing-pad block for a given BB, computed lazily on GNU
7979
/// and eagerly on MSVC.
80-
landing_pads: IndexVec<mir::BasicBlock, Option<BasicBlockRef>>,
80+
landing_pads: IndexVec<mir::BasicBlock, Option<&'ll BasicBlock>>,
8181

8282
/// Cached unreachable block
83-
unreachable_block: Option<BasicBlockRef>,
83+
unreachable_block: Option<&'ll BasicBlock>,
8484

8585
/// The location where each MIR arg/var/tmp/ret is stored. This is
8686
/// usually an `PlaceRef` representing an alloca, but not always:
@@ -219,7 +219,7 @@ pub fn codegen_mir(
219219
// Allocate a `Block` for every basic block, except
220220
// the start block, if nothing loops back to it.
221221
let reentrant_start_block = !mir.predecessors_for(mir::START_BLOCK).is_empty();
222-
let block_bxs: IndexVec<mir::BasicBlock, BasicBlockRef> =
222+
let block_bxs: IndexVec<mir::BasicBlock, &'ll BasicBlock> =
223223
mir.basic_blocks().indices().map(|bb| {
224224
if bb == mir::START_BLOCK && !reentrant_start_block {
225225
bx.llbb()
@@ -348,8 +348,8 @@ fn create_funclets(
348348
mir: &'a Mir<'tcx>,
349349
bx: &Builder<'a, 'll, 'tcx>,
350350
cleanup_kinds: &IndexVec<mir::BasicBlock, CleanupKind>,
351-
block_bxs: &IndexVec<mir::BasicBlock, BasicBlockRef>)
352-
-> (IndexVec<mir::BasicBlock, Option<BasicBlockRef>>,
351+
block_bxs: &IndexVec<mir::BasicBlock, &'ll BasicBlock>)
352+
-> (IndexVec<mir::BasicBlock, Option<&'ll BasicBlock>>,
353353
IndexVec<mir::BasicBlock, Option<Funclet<'ll>>>)
354354
{
355355
block_bxs.iter_enumerated().zip(cleanup_kinds).map(|((bb, &llbb), cleanup_kind)| {

0 commit comments

Comments
 (0)