Skip to content

Commit 56842b2

Browse files
committed
Add a method for emiting a switch.
1 parent b2e6194 commit 56842b2

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

src/librustc_codegen_llvm/builder.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,16 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
170170
v: &'ll Value,
171171
else_llbb: &'ll BasicBlock,
172172
num_cases: usize,
173-
) -> &'ll Value {
174-
unsafe {
173+
cases: impl Iterator<Item = (u128, &'ll BasicBlock)>,
174+
) {
175+
let switch = unsafe {
175176
llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, num_cases as c_uint)
177+
};
178+
for (on_val, dest) in cases {
179+
let on_val = self.const_uint_big(self.val_ty(v), on_val);
180+
unsafe {
181+
llvm::LLVMAddCase(switch, on_val, dest)
182+
}
176183
}
177184
}
178185

@@ -1158,12 +1165,6 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
11581165
}
11591166
}
11601167

1161-
fn add_case(&mut self, s: &'ll Value, on_val: &'ll Value, dest: &'ll BasicBlock) {
1162-
unsafe {
1163-
llvm::LLVMAddCase(s, on_val, dest)
1164-
}
1165-
}
1166-
11671168
fn set_invariant_load(&mut self, load: &'ll Value) {
11681169
unsafe {
11691170
llvm::LLVMSetMetadata(load, llvm::MD_invariant_load as c_uint,

src/librustc_codegen_ssa/mir/block.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,14 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
214214
}
215215
} else {
216216
let (otherwise, targets) = targets.split_last().unwrap();
217-
let switch = bx.switch(discr.immediate(),
218-
helper.llblock(self, *otherwise),
219-
values.len());
220-
let switch_llty = bx.immediate_backend_type(
221-
bx.layout_of(switch_ty)
217+
bx.switch(
218+
discr.immediate(),
219+
helper.llblock(self, *otherwise),
220+
values.len(),
221+
values.iter().zip(targets).map(|(&value, target)| {
222+
(value, helper.llblock(self, *target))
223+
})
222224
);
223-
for (&value, target) in values.iter().zip(targets) {
224-
let llval = bx.const_uint_big(switch_llty, value);
225-
let llbb = helper.llblock(self, *target);
226-
bx.add_case(switch, llval, llbb)
227-
}
228225
}
229226
}
230227

src/librustc_codegen_ssa/traits/builder.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ pub trait BuilderMethods<'a, 'tcx: 'a>:
5050
v: Self::Value,
5151
else_llbb: Self::BasicBlock,
5252
num_cases: usize,
53-
) -> Self::Value;
53+
cases: impl Iterator<Item = (u128, Self::BasicBlock)>,
54+
);
5455
fn invoke(
5556
&mut self,
5657
llfn: Self::Value,
@@ -60,6 +61,7 @@ pub trait BuilderMethods<'a, 'tcx: 'a>:
6061
funclet: Option<&Self::Funclet>,
6162
) -> Self::Value;
6263
fn unreachable(&mut self);
64+
6365
fn add(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value;
6466
fn fadd(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value;
6567
fn fadd_fast(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value;
@@ -242,7 +244,6 @@ pub trait BuilderMethods<'a, 'tcx: 'a>:
242244
order: AtomicOrdering,
243245
) -> Self::Value;
244246
fn atomic_fence(&mut self, order: AtomicOrdering, scope: SynchronizationScope);
245-
fn add_case(&mut self, s: Self::Value, on_val: Self::Value, dest: Self::BasicBlock);
246247
fn set_invariant_load(&mut self, load: Self::Value);
247248

248249
/// Called for `StorageLive`

0 commit comments

Comments
 (0)