From 3eb50358e488e81c2b6d41c4237d605792923eb2 Mon Sep 17 00:00:00 2001 From: Irina Popa Date: Tue, 10 Jul 2018 14:34:34 +0300 Subject: [PATCH] rustc_codegen_llvm: use safe references for BasicBlock. --- src/librustc_codegen_llvm/builder.rs | 38 ++++++++++---------- src/librustc_codegen_llvm/llvm/ffi.rs | 49 +++++++++++++------------- src/librustc_codegen_llvm/mir/block.rs | 8 ++--- src/librustc_codegen_llvm/mir/mod.rs | 14 ++++---- 4 files changed, 54 insertions(+), 55 deletions(-) diff --git a/src/librustc_codegen_llvm/builder.rs b/src/librustc_codegen_llvm/builder.rs index 19ae990fa65f..dbc16cbbf0de 100644 --- a/src/librustc_codegen_llvm/builder.rs +++ b/src/librustc_codegen_llvm/builder.rs @@ -12,7 +12,7 @@ use llvm::{AtomicRmwBinOp, AtomicOrdering, SynchronizationScope, AsmDialect}; use llvm::{Opcode, IntPredicate, RealPredicate, False, OperandBundleDef}; -use llvm::{self, BasicBlockRef}; +use llvm::{self, BasicBlock}; use common::*; use type_::Type; use value::Value; @@ -102,7 +102,7 @@ impl Builder<'a, 'll, 'tcx> { } } - pub fn llbb(&self) -> BasicBlockRef { + pub fn llbb(&self) -> &'ll BasicBlock { unsafe { llvm::LLVMGetInsertBlock(self.llbuilder) } @@ -134,13 +134,13 @@ impl Builder<'a, 'll, 'tcx> { } } - pub fn position_at_end(&self, llbb: BasicBlockRef) { + pub fn position_at_end(&self, llbb: &'ll BasicBlock) { unsafe { llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb); } } - pub fn position_at_start(&self, llbb: BasicBlockRef) { + pub fn position_at_start(&self, llbb: &'ll BasicBlock) { unsafe { llvm::LLVMRustPositionBuilderAtStart(self.llbuilder, llbb); } @@ -168,21 +168,21 @@ impl Builder<'a, 'll, 'tcx> { } } - pub fn br(&self, dest: BasicBlockRef) { + pub fn br(&self, dest: &'ll BasicBlock) { self.count_insn("br"); unsafe { llvm::LLVMBuildBr(self.llbuilder, dest); } } - pub fn cond_br(&self, cond: &'ll Value, then_llbb: BasicBlockRef, else_llbb: BasicBlockRef) { + pub fn cond_br(&self, cond: &'ll Value, then_llbb: &'ll BasicBlock, else_llbb: &'ll BasicBlock) { self.count_insn("condbr"); unsafe { llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb); } } - pub fn switch(&self, v: &'ll Value, else_llbb: BasicBlockRef, num_cases: usize) -> &'ll Value { + pub fn switch(&self, v: &'ll Value, else_llbb: &'ll BasicBlock, num_cases: usize) -> &'ll Value { unsafe { llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, num_cases as c_uint) } @@ -198,8 +198,8 @@ impl Builder<'a, 'll, 'tcx> { pub fn invoke(&self, llfn: &'ll Value, args: &[&'ll Value], - then: BasicBlockRef, - catch: BasicBlockRef, + then: &'ll BasicBlock, + catch: &'ll BasicBlock, bundle: Option<&OperandBundleDef>) -> &'ll Value { self.count_insn("invoke"); @@ -830,7 +830,7 @@ impl Builder<'a, 'll, 'tcx> { } } - pub fn phi(&self, ty: &'ll Type, vals: &[&'ll Value], bbs: &[BasicBlockRef]) -> &'ll Value { + pub fn phi(&self, ty: &'ll Type, vals: &[&'ll Value], bbs: &[&'ll BasicBlock]) -> &'ll Value { assert_eq!(vals.len(), bbs.len()); let phi = self.empty_phi(ty); self.count_insn("addincoming"); @@ -1167,10 +1167,11 @@ impl Builder<'a, 'll, 'tcx> { ret.expect("LLVM does not have support for cleanuppad") } - pub fn cleanup_ret(&self, cleanup: &'ll Value, - unwind: Option) -> &'ll Value { + pub fn cleanup_ret( + &self, cleanup: &'ll Value, + unwind: Option<&'ll BasicBlock>, + ) -> &'ll Value { self.count_insn("cleanupret"); - let unwind = unwind.and_then(NonNull::new); let ret = unsafe { llvm::LLVMRustBuildCleanupRet(self.llbuilder, cleanup, unwind) }; @@ -1190,7 +1191,7 @@ impl Builder<'a, 'll, 'tcx> { ret.expect("LLVM does not have support for catchpad") } - pub fn catch_ret(&self, pad: &'ll Value, unwind: BasicBlockRef) -> &'ll Value { + pub fn catch_ret(&self, pad: &'ll Value, unwind: &'ll BasicBlock) -> &'ll Value { self.count_insn("catchret"); let ret = unsafe { llvm::LLVMRustBuildCatchRet(self.llbuilder, pad, unwind) @@ -1201,11 +1202,10 @@ impl Builder<'a, 'll, 'tcx> { pub fn catch_switch( &self, parent: Option<&'ll Value>, - unwind: Option, + unwind: Option<&'ll BasicBlock>, num_handlers: usize, ) -> &'ll Value { self.count_insn("catchswitch"); - let unwind = unwind.and_then(NonNull::new); let name = CString::new("catchswitch").unwrap(); let ret = unsafe { llvm::LLVMRustBuildCatchSwitch(self.llbuilder, parent, unwind, @@ -1215,7 +1215,7 @@ impl Builder<'a, 'll, 'tcx> { ret.expect("LLVM does not have support for catchswitch") } - pub fn add_handler(&self, catch_switch: &'ll Value, handler: BasicBlockRef) { + pub fn add_handler(&self, catch_switch: &'ll Value, handler: &'ll BasicBlock) { unsafe { llvm::LLVMRustAddHandler(catch_switch, handler); } @@ -1260,13 +1260,13 @@ impl Builder<'a, 'll, 'tcx> { } } - pub fn add_case(&self, s: &'ll Value, on_val: &'ll Value, dest: BasicBlockRef) { + pub fn add_case(&self, s: &'ll Value, on_val: &'ll Value, dest: &'ll BasicBlock) { unsafe { llvm::LLVMAddCase(s, on_val, dest) } } - pub fn add_incoming_to_phi(&self, phi: &'ll Value, val: &'ll Value, bb: BasicBlockRef) { + pub fn add_incoming_to_phi(&self, phi: &'ll Value, val: &'ll Value, bb: &'ll BasicBlock) { self.count_insn("addincoming"); unsafe { llvm::LLVMAddIncoming(phi, &val, &bb, 1 as c_uint); diff --git a/src/librustc_codegen_llvm/llvm/ffi.rs b/src/librustc_codegen_llvm/llvm/ffi.rs index bf016bb8e3c6..1b6014b49524 100644 --- a/src/librustc_codegen_llvm/llvm/ffi.rs +++ b/src/librustc_codegen_llvm/llvm/ffi.rs @@ -381,7 +381,6 @@ extern { pub type Type; } extern { pub type Value; } extern { pub type Metadata; } extern { pub type BasicBlock; } -pub type BasicBlockRef = *mut BasicBlock; extern { pub type Builder; } extern { pub type MemoryBuffer; } pub type MemoryBufferRef = *mut MemoryBuffer; @@ -716,18 +715,18 @@ extern "C" { pub fn LLVMGetParam(Fn: &Value, Index: c_uint) -> &Value; // Operations on basic blocks - pub fn LLVMBasicBlockAsValue(BB: BasicBlockRef) -> &'a Value; - pub fn LLVMGetBasicBlockParent(BB: BasicBlockRef) -> &'a Value; + pub fn LLVMBasicBlockAsValue(BB: &BasicBlock) -> &Value; + pub fn LLVMGetBasicBlockParent(BB: &BasicBlock) -> &Value; pub fn LLVMAppendBasicBlockInContext(C: &'a Context, Fn: &'a Value, Name: *const c_char) - -> BasicBlockRef; - pub fn LLVMDeleteBasicBlock(BB: BasicBlockRef); + -> &'a BasicBlock; + pub fn LLVMDeleteBasicBlock(BB: &BasicBlock); // Operations on instructions - pub fn LLVMGetInstructionParent(Inst: &Value) -> BasicBlockRef; - pub fn LLVMGetFirstBasicBlock(Fn: &Value) -> BasicBlockRef; - pub fn LLVMGetFirstInstruction(BB: BasicBlockRef) -> &'a Value; + pub fn LLVMGetInstructionParent(Inst: &Value) -> &BasicBlock; + pub fn LLVMGetFirstBasicBlock(Fn: &Value) -> &BasicBlock; + pub fn LLVMGetFirstInstruction(BB: &BasicBlock) -> &'a Value; pub fn LLVMInstructionEraseFromParent(Inst: &Value); // Operations on call sites @@ -745,15 +744,15 @@ extern "C" { // Operations on phi nodes pub fn LLVMAddIncoming(PhiNode: &'a Value, IncomingValues: *const &'a Value, - IncomingBlocks: *const BasicBlockRef, + IncomingBlocks: *const &'a BasicBlock, Count: c_uint); // Instruction builders pub fn LLVMCreateBuilderInContext(C: &Context) -> &Builder; - pub fn LLVMPositionBuilder(Builder: &'a Builder, Block: BasicBlockRef, Instr: &'a Value); + pub fn LLVMPositionBuilder(Builder: &'a Builder, Block: &'a BasicBlock, Instr: &'a Value); pub fn LLVMPositionBuilderBefore(Builder: &'a Builder, Instr: &'a Value); - pub fn LLVMPositionBuilderAtEnd(Builder: &Builder, Block: BasicBlockRef); - pub fn LLVMGetInsertBlock(Builder: &Builder) -> BasicBlockRef; + pub fn LLVMPositionBuilderAtEnd(Builder: &'a Builder, Block: &'a BasicBlock); + pub fn LLVMGetInsertBlock(Builder: &Builder) -> &BasicBlock; pub fn LLVMDisposeBuilder(Builder: &Builder); // Metadata @@ -765,15 +764,15 @@ extern "C" { pub fn LLVMBuildRetVoid(B: &Builder) -> &Value; pub fn LLVMBuildRet(B: &'a Builder, V: &'a Value) -> &'a Value; pub fn LLVMBuildAggregateRet(B: &'a Builder, RetVals: *const &'a Value, N: c_uint) -> &'a Value; - pub fn LLVMBuildBr(B: &Builder, Dest: BasicBlockRef) -> &Value; + pub fn LLVMBuildBr(B: &'a Builder, Dest: &'a BasicBlock) -> &'a Value; pub fn LLVMBuildCondBr(B: &'a Builder, If: &'a Value, - Then: BasicBlockRef, - Else: BasicBlockRef) + Then: &'a BasicBlock, + Else: &'a BasicBlock) -> &'a Value; pub fn LLVMBuildSwitch(B: &'a Builder, V: &'a Value, - Else: BasicBlockRef, + Else: &'a BasicBlock, NumCases: c_uint) -> &'a Value; pub fn LLVMBuildIndirectBr(B: &'a Builder, Addr: &'a Value, NumDests: c_uint) -> &'a Value; @@ -781,8 +780,8 @@ extern "C" { Fn: &'a Value, Args: *const &'a Value, NumArgs: c_uint, - Then: BasicBlockRef, - Catch: BasicBlockRef, + Then: &'a BasicBlock, + Catch: &'a BasicBlock, Bundle: Option>, Name: *const c_char) -> &'a Value; @@ -803,7 +802,7 @@ extern "C" { -> Option<&'a Value>; pub fn LLVMRustBuildCleanupRet(B: &'a Builder, CleanupPad: &'a Value, - UnwindBB: Option>) + UnwindBB: Option<&'a BasicBlock>) -> Option<&'a Value>; pub fn LLVMRustBuildCatchPad(B: &'a Builder, ParentPad: &'a Value, @@ -811,18 +810,18 @@ extern "C" { Args: *const &'a Value, Name: *const c_char) -> Option<&'a Value>; - pub fn LLVMRustBuildCatchRet(B: &'a Builder, Pad: &'a Value, BB: BasicBlockRef) -> Option<&'a Value>; + pub fn LLVMRustBuildCatchRet(B: &'a Builder, Pad: &'a Value, BB: &'a BasicBlock) -> Option<&'a Value>; pub fn LLVMRustBuildCatchSwitch(Builder: &'a Builder, ParentPad: Option<&'a Value>, - BB: Option>, + BB: Option<&'a BasicBlock>, NumHandlers: c_uint, Name: *const c_char) -> Option<&'a Value>; - pub fn LLVMRustAddHandler(CatchSwitch: &Value, Handler: BasicBlockRef); + pub fn LLVMRustAddHandler(CatchSwitch: &'a Value, Handler: &'a BasicBlock); pub fn LLVMSetPersonalityFn(Func: &'a Value, Pers: &'a Value); // Add a case to the switch instruction - pub fn LLVMAddCase(Switch: &'a Value, OnVal: &'a Value, Dest: BasicBlockRef); + pub fn LLVMAddCase(Switch: &'a Value, OnVal: &'a Value, Dest: &'a BasicBlock); // Add a clause to the landing pad instruction pub fn LLVMAddClause(LandingPad: &'a Value, ClauseVal: &'a Value); @@ -1503,7 +1502,7 @@ extern "C" { AddrOps: *const i64, AddrOpsCount: c_uint, DL: &'a Value, - InsertAtEnd: BasicBlockRef) + InsertAtEnd: &'a BasicBlock) -> &'a Value; pub fn LLVMRustDIBuilderCreateEnumerator(Builder: &DIBuilder, @@ -1691,7 +1690,7 @@ extern "C" { -> OperandBundleDefRef; pub fn LLVMRustFreeOperandBundleDef(Bundle: OperandBundleDefRef); - pub fn LLVMRustPositionBuilderAtStart(B: &Builder, BB: BasicBlockRef); + pub fn LLVMRustPositionBuilderAtStart(B: &'a Builder, BB: &'a BasicBlock); pub fn LLVMRustSetComdat(M: &'a Module, V: &'a Value, Name: *const c_char); pub fn LLVMRustUnsetComdat(V: &Value); diff --git a/src/librustc_codegen_llvm/mir/block.rs b/src/librustc_codegen_llvm/mir/block.rs index 587165dfe77f..684ecfaeec8f 100644 --- a/src/librustc_codegen_llvm/mir/block.rs +++ b/src/librustc_codegen_llvm/mir/block.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use llvm::{self, BasicBlockRef}; +use llvm::{self, BasicBlock}; use rustc::middle::lang_items; use rustc::ty::{self, Ty, TypeFoldable}; use rustc::ty::layout::{self, LayoutOf}; @@ -754,7 +754,7 @@ impl FunctionCx<'a, 'll, 'tcx> { /// Return the landingpad wrapper around the given basic block /// /// No-op in MSVC SEH scheme. - fn landing_pad_to(&mut self, target_bb: mir::BasicBlock) -> BasicBlockRef { + fn landing_pad_to(&mut self, target_bb: mir::BasicBlock) -> &'ll BasicBlock { if let Some(block) = self.landing_pads[target_bb] { return block; } @@ -765,7 +765,7 @@ impl FunctionCx<'a, 'll, 'tcx> { landing_pad } - fn landing_pad_uncached(&mut self, target_bb: BasicBlockRef) -> BasicBlockRef { + fn landing_pad_uncached(&mut self, target_bb: &'ll BasicBlock) -> &'ll BasicBlock { if base::wants_msvc_seh(self.cx.sess()) { span_bug!(self.mir.span, "landing pad was not inserted?") } @@ -790,7 +790,7 @@ impl FunctionCx<'a, 'll, 'tcx> { Type::struct_(cx, &[Type::i8p(cx), Type::i32(cx)], false) } - fn unreachable_block(&mut self) -> BasicBlockRef { + fn unreachable_block(&mut self) -> &'ll BasicBlock { self.unreachable_block.unwrap_or_else(|| { let bl = self.new_block("unreachable"); bl.unreachable(); diff --git a/src/librustc_codegen_llvm/mir/mod.rs b/src/librustc_codegen_llvm/mir/mod.rs index be9cd005013f..8cdd0398eff9 100644 --- a/src/librustc_codegen_llvm/mir/mod.rs +++ b/src/librustc_codegen_llvm/mir/mod.rs @@ -10,7 +10,7 @@ use common::{C_i32, C_null}; use libc::c_uint; -use llvm::{self, BasicBlockRef}; +use llvm::{self, BasicBlock}; use llvm::debuginfo::DIScope; use rustc::ty::{self, Ty, TypeFoldable, UpvarSubsts}; use rustc::ty::layout::{LayoutOf, TyLayout}; @@ -66,7 +66,7 @@ pub struct FunctionCx<'a, 'll: 'a, 'tcx: 'll> { personality_slot: Option>, /// A `Block` for each MIR `BasicBlock` - blocks: IndexVec, + blocks: IndexVec, /// The funclet status of each basic block cleanup_kinds: IndexVec, @@ -77,10 +77,10 @@ pub struct FunctionCx<'a, 'll: 'a, 'tcx: 'll> { /// This stores the landing-pad block for a given BB, computed lazily on GNU /// and eagerly on MSVC. - landing_pads: IndexVec>, + landing_pads: IndexVec>, /// Cached unreachable block - unreachable_block: Option, + unreachable_block: Option<&'ll BasicBlock>, /// The location where each MIR arg/var/tmp/ret is stored. This is /// usually an `PlaceRef` representing an alloca, but not always: @@ -219,7 +219,7 @@ pub fn codegen_mir( // Allocate a `Block` for every basic block, except // the start block, if nothing loops back to it. let reentrant_start_block = !mir.predecessors_for(mir::START_BLOCK).is_empty(); - let block_bxs: IndexVec = + let block_bxs: IndexVec = mir.basic_blocks().indices().map(|bb| { if bb == mir::START_BLOCK && !reentrant_start_block { bx.llbb() @@ -348,8 +348,8 @@ fn create_funclets( mir: &'a Mir<'tcx>, bx: &Builder<'a, 'll, 'tcx>, cleanup_kinds: &IndexVec, - block_bxs: &IndexVec) - -> (IndexVec>, + block_bxs: &IndexVec) + -> (IndexVec>, IndexVec>>) { block_bxs.iter_enumerated().zip(cleanup_kinds).map(|((bb, &llbb), cleanup_kind)| {