1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 //! Routines for manipulating the control-flow graph.
17 use rustc::middle::region;
21 impl<'tcx> CFG<'tcx> {
22 pub fn block_data(&self, blk: BasicBlock) -> &BasicBlockData<'tcx> {
23 &self.basic_blocks[blk]
26 pub fn block_data_mut(&mut self, blk: BasicBlock) -> &mut BasicBlockData<'tcx> {
27 &mut self.basic_blocks[blk]
30 // llvm.org/PR32488 makes this function use an excess of stack space. Mark
31 // it as #[inline(never)] to keep rustc's stack use in check.
33 pub fn start_new_block(&mut self) -> BasicBlock {
34 self.basic_blocks.push(BasicBlockData::new(None))
37 pub fn start_new_cleanup_block(&mut self) -> BasicBlock {
38 let bb = self.start_new_block();
39 self.block_data_mut(bb).is_cleanup = true;
43 pub fn push(&mut self, block: BasicBlock, statement: Statement<'tcx>) {
44 debug!("push({:?}, {:?})", block, statement);
45 self.block_data_mut(block).statements.push(statement);
48 pub fn push_end_region<'a, 'gcx:'a+'tcx>(&mut self,
49 tcx: ty::TyCtxt<'a, 'gcx, 'tcx>,
51 source_info: SourceInfo,
52 region_scope: region::Scope) {
53 if tcx.sess.emit_end_regions() {
54 self.push(block, Statement {
56 kind: StatementKind::EndRegion(region_scope),
61 pub fn push_assign(&mut self,
63 source_info: SourceInfo,
64 lvalue: &Lvalue<'tcx>,
65 rvalue: Rvalue<'tcx>) {
66 self.push(block, Statement {
68 kind: StatementKind::Assign(lvalue.clone(), rvalue)
72 pub fn push_assign_constant(&mut self,
74 source_info: SourceInfo,
76 constant: Constant<'tcx>) {
77 self.push_assign(block, source_info, temp,
78 Rvalue::Use(Operand::Constant(box constant)));
81 pub fn push_assign_unit(&mut self,
83 source_info: SourceInfo,
84 lvalue: &Lvalue<'tcx>) {
85 self.push_assign(block, source_info, lvalue, Rvalue::Aggregate(
86 box AggregateKind::Tuple, vec![]
90 pub fn terminate(&mut self,
92 source_info: SourceInfo,
93 kind: TerminatorKind<'tcx>) {
94 debug!("terminating block {:?} <- {:?}", block, kind);
95 debug_assert!(self.block_data(block).terminator.is_none(),
96 "terminate: block {:?}={:?} already has a terminator set",
98 self.block_data(block));
99 self.block_data_mut(block).terminator = Some(Terminator {