Skip to content

Commit 5df2bb1

Browse files
committed
Avoid empty "static_allocas" blocks
When there are no allocas, we don't need a block for them.
1 parent dcd5d14 commit 5df2bb1

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

src/librustc/middle/trans/base.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,7 @@ pub fn alloca_maybe_zeroed(cx: block, ty: Type, name: &str, zero: bool) -> Value
15571557
return llvm::LLVMGetUndef(ty.to_ref());
15581558
}
15591559
}
1560-
let initcx = base::raw_block(cx.fcx, false, cx.fcx.llstaticallocas);
1560+
let initcx = base::raw_block(cx.fcx, false, cx.fcx.get_llstaticallocas());
15611561
let p = Alloca(initcx, ty, name);
15621562
if zero { memzero(initcx, p, ty); }
15631563
p
@@ -1570,21 +1570,18 @@ pub fn arrayalloca(cx: block, ty: Type, v: ValueRef) -> ValueRef {
15701570
return llvm::LLVMGetUndef(ty.to_ref());
15711571
}
15721572
}
1573-
return ArrayAlloca(base::raw_block(cx.fcx, false, cx.fcx.llstaticallocas), ty, v);
1573+
return ArrayAlloca(base::raw_block(cx.fcx, false, cx.fcx.get_llstaticallocas()), ty, v);
15741574
}
15751575

15761576
pub struct BasicBlocks {
15771577
sa: BasicBlockRef,
15781578
}
15791579

1580-
// Creates the standard set of basic blocks for a function
1581-
pub fn mk_standard_basic_blocks(llfn: ValueRef) -> BasicBlocks {
1580+
pub fn mk_staticallocas_basic_block(llfn: ValueRef) -> BasicBlockRef {
15821581
unsafe {
15831582
let cx = task_llcx();
1584-
BasicBlocks {
1585-
sa: str::as_c_str("static_allocas",
1586-
|buf| llvm::LLVMAppendBasicBlockInContext(cx, llfn, buf)),
1587-
}
1583+
str::as_c_str("static_allocas",
1584+
|buf| llvm::LLVMAppendBasicBlockInContext(cx, llfn, buf))
15881585
}
15891586
}
15901587

@@ -1604,7 +1601,7 @@ pub fn make_return_pointer(fcx: fn_ctxt, output_type: ty::t) -> ValueRef {
16041601
llvm::LLVMGetParam(fcx.llfn, 0)
16051602
} else {
16061603
let lloutputtype = type_of::type_of(fcx.ccx, output_type);
1607-
alloca(raw_block(fcx, false, fcx.llstaticallocas), lloutputtype,
1604+
alloca(raw_block(fcx, false, fcx.get_llstaticallocas()), lloutputtype,
16081605
"__make_return_pointer")
16091606
}
16101607
}
@@ -1632,8 +1629,6 @@ pub fn new_fn_ctxt_w_id(ccx: @mut CrateContext,
16321629
id,
16331630
param_substs.repr(ccx.tcx));
16341631

1635-
let llbbs = mk_standard_basic_blocks(llfndecl);
1636-
16371632
let substd_output_type = match param_substs {
16381633
None => output_type,
16391634
Some(substs) => {
@@ -1647,7 +1642,7 @@ pub fn new_fn_ctxt_w_id(ccx: @mut CrateContext,
16471642
llvm::LLVMGetUndef(Type::i8p().to_ref())
16481643
},
16491644
llretptr: None,
1650-
llstaticallocas: llbbs.sa,
1645+
llstaticallocas: None,
16511646
llloadenv: None,
16521647
llreturn: None,
16531648
llself: None,
@@ -1821,14 +1816,24 @@ pub fn build_return_block(fcx: fn_ctxt, ret_cx: block) {
18211816

18221817
pub fn tie_up_header_blocks(fcx: fn_ctxt, lltop: BasicBlockRef) {
18231818
let _icx = push_ctxt("tie_up_header_blocks");
1824-
match fcx.llloadenv {
1819+
let llnext = match fcx.llloadenv {
18251820
Some(ll) => {
1826-
Br(raw_block(fcx, false, fcx.llstaticallocas), ll);
1821+
unsafe {
1822+
llvm::LLVMMoveBasicBlockBefore(ll, lltop);
1823+
}
18271824
Br(raw_block(fcx, false, ll), lltop);
1825+
ll
18281826
}
1829-
None => {
1830-
Br(raw_block(fcx, false, fcx.llstaticallocas), lltop);
1827+
None => lltop
1828+
};
1829+
match fcx.llstaticallocas {
1830+
Some(ll) => {
1831+
unsafe {
1832+
llvm::LLVMMoveBasicBlockBefore(ll, llnext);
1833+
}
1834+
Br(raw_block(fcx, false, ll), llnext);
18311835
}
1836+
None => ()
18321837
}
18331838
}
18341839

src/librustc/middle/trans/common.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pub struct fn_ctxt_ {
178178
// the function, due to LLVM's quirks.
179179
// A block for all the function's static allocas, so that LLVM
180180
// will coalesce them into a single alloca call.
181-
llstaticallocas: BasicBlockRef,
181+
llstaticallocas: Option<BasicBlockRef>,
182182
// A block containing code that copies incoming arguments to space
183183
// already allocated by code in one of the llallocas blocks.
184184
// (LLVM requires that arguments be copied to local allocas before
@@ -251,6 +251,14 @@ impl fn_ctxt_ {
251251
}
252252
}
253253

254+
pub fn get_llstaticallocas(&mut self) -> BasicBlockRef {
255+
if self.llstaticallocas.is_none() {
256+
self.llstaticallocas = Some(base::mk_staticallocas_basic_block(self.llfn));
257+
}
258+
259+
self.llstaticallocas.get()
260+
}
261+
254262
pub fn get_llreturn(&mut self) -> BasicBlockRef {
255263
if self.llreturn.is_none() {
256264
self.llreturn = Some(base::mk_return_basic_block(self.llfn));

src/librustc/middle/trans/foreign.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ fn build_wrap_fn_(ccx: @mut CrateContext,
197197
// the C ABI.
198198
if needs_c_return && !ty::type_is_immediate(ccx.tcx, tys.fn_sig.output) {
199199
let lloutputtype = type_of::type_of(fcx.ccx, tys.fn_sig.output);
200-
fcx.llretptr = Some(alloca(raw_block(fcx, false, fcx.llstaticallocas),
200+
fcx.llretptr = Some(alloca(raw_block(fcx, false, fcx.get_llstaticallocas()),
201201
lloutputtype,
202202
""));
203203
}

0 commit comments

Comments
 (0)