Skip to content

Commit dd50999

Browse files
committed
Split control flow related methods out to ControlFlowBuilderMethods
1 parent 97e43a4 commit dd50999

File tree

2 files changed

+132
-126
lines changed

2 files changed

+132
-126
lines changed

src/librustc_codegen_llvm/builder.rs

+104-102
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,110 @@ impl HasCodegen<'tcx> for Builder<'_, 'll, 'tcx> {
8787
type CodegenCx = CodegenCx<'ll, 'tcx>;
8888
}
8989

90+
impl ControlFlowBuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
91+
fn new_block<'b>(
92+
cx: &'a CodegenCx<'ll, 'tcx>,
93+
llfn: &'ll Value,
94+
name: &'b str
95+
) -> Self {
96+
let mut bx = Builder::with_cx(cx);
97+
let llbb = unsafe {
98+
let name = SmallCStr::new(name);
99+
llvm::LLVMAppendBasicBlockInContext(
100+
cx.llcx,
101+
llfn,
102+
name.as_ptr()
103+
)
104+
};
105+
bx.position_at_end(llbb);
106+
bx
107+
}
108+
109+
fn with_cx(cx: &'a CodegenCx<'ll, 'tcx>) -> Self {
110+
// Create a fresh builder from the crate context.
111+
let llbuilder = unsafe {
112+
llvm::LLVMCreateBuilderInContext(cx.llcx)
113+
};
114+
Builder {
115+
llbuilder,
116+
cx,
117+
}
118+
}
119+
120+
fn build_sibling_block<'b>(&self, name: &'b str) -> Self {
121+
Builder::new_block(self.cx, self.llfn(), name)
122+
}
123+
124+
fn llbb(&self) -> &'ll BasicBlock {
125+
unsafe {
126+
llvm::LLVMGetInsertBlock(self.llbuilder)
127+
}
128+
}
129+
130+
fn position_at_end(&mut self, llbb: &'ll BasicBlock) {
131+
unsafe {
132+
llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb);
133+
}
134+
}
135+
136+
fn ret_void(&mut self) {
137+
self.count_insn("retvoid");
138+
unsafe {
139+
llvm::LLVMBuildRetVoid(self.llbuilder);
140+
}
141+
}
142+
143+
fn ret(&mut self, v: &'ll Value) {
144+
self.count_insn("ret");
145+
unsafe {
146+
llvm::LLVMBuildRet(self.llbuilder, v);
147+
}
148+
}
149+
150+
fn br(&mut self, dest: &'ll BasicBlock) {
151+
self.count_insn("br");
152+
unsafe {
153+
llvm::LLVMBuildBr(self.llbuilder, dest);
154+
}
155+
}
156+
157+
fn cond_br(
158+
&mut self,
159+
cond: &'ll Value,
160+
then_llbb: &'ll BasicBlock,
161+
else_llbb: &'ll BasicBlock,
162+
) {
163+
self.count_insn("condbr");
164+
unsafe {
165+
llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
166+
}
167+
}
168+
169+
fn switch(
170+
&mut self,
171+
v: &'ll Value,
172+
else_llbb: &'ll BasicBlock,
173+
num_cases: usize,
174+
) -> &'ll Value {
175+
unsafe {
176+
llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, num_cases as c_uint)
177+
}
178+
}
179+
180+
fn add_case(&mut self, s: &'ll Value, on_val: &'ll Value, dest: &'ll BasicBlock) {
181+
unsafe {
182+
llvm::LLVMAddCase(s, on_val, dest)
183+
}
184+
}
185+
186+
fn unreachable(&mut self) {
187+
self.count_insn("unreachable");
188+
unsafe {
189+
llvm::LLVMBuildUnreachable(self.llbuilder);
190+
}
191+
}
192+
}
193+
90194
impl MemoryBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
91195
fn alloca(&mut self, ty: &'ll Type, name: &str, align: Align) -> &'ll Value {
92196
let mut bx = Builder::with_cx(self.cx);
@@ -1021,102 +1125,6 @@ impl UnwindBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
10211125
}
10221126

10231127
impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
1024-
fn new_block<'b>(
1025-
cx: &'a CodegenCx<'ll, 'tcx>,
1026-
llfn: &'ll Value,
1027-
name: &'b str
1028-
) -> Self {
1029-
let mut bx = Builder::with_cx(cx);
1030-
let llbb = unsafe {
1031-
let name = SmallCStr::new(name);
1032-
llvm::LLVMAppendBasicBlockInContext(
1033-
cx.llcx,
1034-
llfn,
1035-
name.as_ptr()
1036-
)
1037-
};
1038-
bx.position_at_end(llbb);
1039-
bx
1040-
}
1041-
1042-
fn with_cx(cx: &'a CodegenCx<'ll, 'tcx>) -> Self {
1043-
// Create a fresh builder from the crate context.
1044-
let llbuilder = unsafe {
1045-
llvm::LLVMCreateBuilderInContext(cx.llcx)
1046-
};
1047-
Builder {
1048-
llbuilder,
1049-
cx,
1050-
}
1051-
}
1052-
1053-
fn build_sibling_block<'b>(&self, name: &'b str) -> Self {
1054-
Builder::new_block(self.cx, self.llfn(), name)
1055-
}
1056-
1057-
fn llbb(&self) -> &'ll BasicBlock {
1058-
unsafe {
1059-
llvm::LLVMGetInsertBlock(self.llbuilder)
1060-
}
1061-
}
1062-
1063-
fn position_at_end(&mut self, llbb: &'ll BasicBlock) {
1064-
unsafe {
1065-
llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb);
1066-
}
1067-
}
1068-
1069-
fn ret_void(&mut self) {
1070-
self.count_insn("retvoid");
1071-
unsafe {
1072-
llvm::LLVMBuildRetVoid(self.llbuilder);
1073-
}
1074-
}
1075-
1076-
fn ret(&mut self, v: &'ll Value) {
1077-
self.count_insn("ret");
1078-
unsafe {
1079-
llvm::LLVMBuildRet(self.llbuilder, v);
1080-
}
1081-
}
1082-
1083-
fn br(&mut self, dest: &'ll BasicBlock) {
1084-
self.count_insn("br");
1085-
unsafe {
1086-
llvm::LLVMBuildBr(self.llbuilder, dest);
1087-
}
1088-
}
1089-
1090-
fn cond_br(
1091-
&mut self,
1092-
cond: &'ll Value,
1093-
then_llbb: &'ll BasicBlock,
1094-
else_llbb: &'ll BasicBlock,
1095-
) {
1096-
self.count_insn("condbr");
1097-
unsafe {
1098-
llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
1099-
}
1100-
}
1101-
1102-
fn switch(
1103-
&mut self,
1104-
v: &'ll Value,
1105-
else_llbb: &'ll BasicBlock,
1106-
num_cases: usize,
1107-
) -> &'ll Value {
1108-
unsafe {
1109-
llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, num_cases as c_uint)
1110-
}
1111-
}
1112-
1113-
fn unreachable(&mut self) {
1114-
self.count_insn("unreachable");
1115-
unsafe {
1116-
llvm::LLVMBuildUnreachable(self.llbuilder);
1117-
}
1118-
}
1119-
11201128
/* Miscellaneous instructions */
11211129
fn inline_asm_call(&mut self, asm: &CStr, cons: &CStr,
11221130
inputs: &[&'ll Value], output: &'ll Type,
@@ -1211,12 +1219,6 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
12111219
}
12121220
}
12131221

1214-
fn add_case(&mut self, s: &'ll Value, on_val: &'ll Value, dest: &'ll BasicBlock) {
1215-
unsafe {
1216-
llvm::LLVMAddCase(s, on_val, dest)
1217-
}
1218-
}
1219-
12201222
fn set_invariant_load(&mut self, load: &'ll Value) {
12211223
unsafe {
12221224
llvm::LLVMSetMetadata(load, llvm::MD_invariant_load as c_uint,

src/librustc_codegen_ssa/traits/builder.rs

+28-24
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@ pub enum OverflowOp {
2323
Mul,
2424
}
2525

26+
pub trait ControlFlowBuilderMethods<'a, 'tcx: 'a>: HasCodegen<'tcx> {
27+
fn new_block<'b>(cx: &'a Self::CodegenCx, llfn: Self::Value, name: &'b str) -> Self;
28+
fn with_cx(cx: &'a Self::CodegenCx) -> Self;
29+
fn build_sibling_block<'b>(&self, name: &'b str) -> Self;
30+
fn llbb(&self) -> Self::BasicBlock;
31+
32+
fn position_at_end(&mut self, llbb: Self::BasicBlock);
33+
fn ret_void(&mut self);
34+
fn ret(&mut self, v: Self::Value);
35+
fn br(&mut self, dest: Self::BasicBlock);
36+
fn cond_br(
37+
&mut self,
38+
cond: Self::Value,
39+
then_llbb: Self::BasicBlock,
40+
else_llbb: Self::BasicBlock,
41+
);
42+
fn switch(
43+
&mut self,
44+
v: Self::Value,
45+
else_llbb: Self::BasicBlock,
46+
num_cases: usize,
47+
) -> Self::Value;
48+
fn add_case(&mut self, s: Self::Value, on_val: Self::Value, dest: Self::BasicBlock);
49+
fn unreachable(&mut self);
50+
}
51+
2652
pub trait MemoryBuilderMethods<'tcx>: HasCodegen<'tcx> {
2753
// Stack allocations
2854
fn alloca(&mut self, ty: Self::Type, name: &str, align: Align) -> Self::Value;
@@ -226,33 +252,12 @@ pub trait BuilderMethods<'a, 'tcx: 'a>:
226252
+ IntrinsicCallMethods<'tcx>
227253
+ AsmBuilderMethods<'tcx>
228254
+ StaticBuilderMethods<'tcx>
255+
256+
+ ControlFlowBuilderMethods<'a, 'tcx>
229257
+ MemoryBuilderMethods<'tcx>
230258
+ NumBuilderMethods<'tcx>
231259
+ UnwindBuilderMethods<'tcx>
232260
{
233-
fn new_block<'b>(cx: &'a Self::CodegenCx, llfn: Self::Value, name: &'b str) -> Self;
234-
fn with_cx(cx: &'a Self::CodegenCx) -> Self;
235-
fn build_sibling_block<'b>(&self, name: &'b str) -> Self;
236-
fn llbb(&self) -> Self::BasicBlock;
237-
238-
fn position_at_end(&mut self, llbb: Self::BasicBlock);
239-
fn ret_void(&mut self);
240-
fn ret(&mut self, v: Self::Value);
241-
fn br(&mut self, dest: Self::BasicBlock);
242-
fn cond_br(
243-
&mut self,
244-
cond: Self::Value,
245-
then_llbb: Self::BasicBlock,
246-
else_llbb: Self::BasicBlock,
247-
);
248-
fn switch(
249-
&mut self,
250-
v: Self::Value,
251-
else_llbb: Self::BasicBlock,
252-
num_cases: usize,
253-
) -> Self::Value;
254-
fn unreachable(&mut self);
255-
256261
fn inline_asm_call(
257262
&mut self,
258263
asm: &CStr,
@@ -277,7 +282,6 @@ pub trait BuilderMethods<'a, 'tcx: 'a>:
277282
fn extract_value(&mut self, agg_val: Self::Value, idx: u64) -> Self::Value;
278283
fn insert_value(&mut self, agg_val: Self::Value, elt: Self::Value, idx: u64) -> Self::Value;
279284

280-
fn add_case(&mut self, s: Self::Value, on_val: Self::Value, dest: Self::BasicBlock);
281285
fn set_invariant_load(&mut self, load: Self::Value);
282286

283287
/// Called for `StorageLive`

0 commit comments

Comments
 (0)