Skip to content

Commit 02cb1d4

Browse files
committed
Delete obviously-unreachable blocks
Some blocks won't be translated at all because they aren't reachable at the LLVM level, these need to be dealt with because they lack a terminator and therefore trigger an LLVM assertion. Other blocks aren't reachable because of codegen-time optimistions, for example not dropping types that don't need it, often resulting in blocks with no predecessors. We'll clean those up as well.
1 parent c70bc3a commit 02cb1d4

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/librustc_trans/basic_block.rs

+6
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ impl BasicBlock {
4949
_ => None
5050
}
5151
}
52+
53+
pub fn delete(self) {
54+
unsafe {
55+
llvm::LLVMDeleteBasicBlock(self.0);
56+
}
57+
}
5258
}

src/librustc_trans/mir/mod.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use common::{self, Block, BlockAndBuilder, FunctionContext};
1919
use std::ops::Deref;
2020
use std::rc::Rc;
2121

22+
use trans::basic_block::BasicBlock;
23+
2224
use rustc_data_structures::bitvec::BitVector;
2325

2426
use self::lvalue::{LvalueRef, get_dataptr, get_meta};
@@ -170,19 +172,20 @@ pub fn trans_mir<'blk, 'tcx: 'blk>(fcx: &'blk FunctionContext<'blk, 'tcx>) {
170172
mircx.trans_block(bb);
171173
}
172174

173-
// Add unreachable instructions at the end of unreachable blocks
174-
// so they're actually terminated.
175-
// TODO: Remove the blocks from the function
175+
// Remove blocks that haven't been visited, or have no
176+
// predecessors.
176177
for &bb in &mir_blocks {
178+
let block = mircx.blocks[bb.index()];
179+
let block = BasicBlock(block.llbb);
180+
// Unreachable block
177181
if !visited.contains(bb.index()) {
178-
mircx.blocks[bb.index()].build().unreachable();
182+
block.delete();
183+
} else if block.pred_iter().count() == 0 {
184+
block.delete();
179185
}
180186
}
181187

182-
183188
fcx.cleanup();
184-
185-
debug!("trans_mir: {:?}", ::trans::value::Value(fcx.llfn));
186189
}
187190

188191
/// Produce, for each argument, a `ValueRef` pointing at the

0 commit comments

Comments
 (0)