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;
19 use rustc::ty::TyCtxt;
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: TyCtxt<'a, 'gcx, 'tcx>,
51 source_info: SourceInfo,
52 region_scope: region::Scope) {
53 if tcx.emit_end_regions() {
54 if let region::ScopeData::CallSite = region_scope.data() {
55 // The CallSite scope (aka the root scope) is sort of weird, in that it is
56 // supposed to "separate" the "interior" and "exterior" of a closure. Being
57 // that, it is not really a part of the region hierarchy, but for some
58 // reason it *is* considered a part of it.
60 // It should die a hopefully painful death with NLL, so let's leave this hack
61 // for now so that nobody can complain about soundness.
65 self.push(block, Statement {
67 kind: StatementKind::EndRegion(region_scope),
72 pub fn push_assign(&mut self,
74 source_info: SourceInfo,
76 rvalue: Rvalue<'tcx>) {
77 self.push(block, Statement {
79 kind: StatementKind::Assign(place.clone(), rvalue)
83 pub fn push_assign_constant(&mut self,
85 source_info: SourceInfo,
87 constant: Constant<'tcx>) {
88 self.push_assign(block, source_info, temp,
89 Rvalue::Use(Operand::Constant(box constant)));
92 pub fn push_assign_unit(&mut self,
94 source_info: SourceInfo,
95 place: &Place<'tcx>) {
96 self.push_assign(block, source_info, place, Rvalue::Aggregate(
97 box AggregateKind::Tuple, vec![]
101 pub fn terminate(&mut self,
103 source_info: SourceInfo,
104 kind: TerminatorKind<'tcx>) {
105 debug!("terminating block {:?} <- {:?}", block, kind);
106 debug_assert!(self.block_data(block).terminator.is_none(),
107 "terminate: block {:?}={:?} already has a terminator set",
109 self.block_data(block));
110 self.block_data_mut(block).terminator = Some(Terminator {