Skip to content

Commit 75cc481

Browse files
committed
Use a macro to factor out some repetitive code.
Similar to the existing macro just above.
1 parent b5502f9 commit 75cc481

File tree

1 file changed

+27
-80
lines changed

1 file changed

+27
-80
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 27 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl<'ll, 'tcx> HasCodegen<'tcx> for Builder<'_, 'll, 'tcx> {
128128
type CodegenCx = CodegenCx<'ll, 'tcx>;
129129
}
130130

131-
macro_rules! builder_methods_for_value_instructions {
131+
macro_rules! math_builder_methods {
132132
($($name:ident($($arg:ident),*) => $llvm_capi:ident),+ $(,)?) => {
133133
$(fn $name(&mut self, $($arg: &'ll Value),*) -> &'ll Value {
134134
unsafe {
@@ -138,6 +138,18 @@ macro_rules! builder_methods_for_value_instructions {
138138
}
139139
}
140140

141+
macro_rules! set_math_builder_methods {
142+
($($name:ident($($arg:ident),*) => ($llvm_capi:ident, $llvm_set_math:ident)),+ $(,)?) => {
143+
$(fn $name(&mut self, $($arg: &'ll Value),*) -> &'ll Value {
144+
unsafe {
145+
let instr = llvm::$llvm_capi(self.llbuilder, $($arg,)* UNNAMED);
146+
llvm::$llvm_set_math(instr);
147+
instr
148+
}
149+
})+
150+
}
151+
}
152+
141153
impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
142154
fn build(cx: &'a CodegenCx<'ll, 'tcx>, llbb: &'ll BasicBlock) -> Self {
143155
let bx = Builder::with_cx(cx);
@@ -273,7 +285,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
273285
}
274286
}
275287

276-
builder_methods_for_value_instructions! {
288+
math_builder_methods! {
277289
add(a, b) => LLVMBuildAdd,
278290
fadd(a, b) => LLVMBuildFAdd,
279291
sub(a, b) => LLVMBuildSub,
@@ -305,84 +317,17 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
305317
unchecked_umul(x, y) => LLVMBuildNUWMul,
306318
}
307319

308-
fn fadd_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
309-
unsafe {
310-
let instr = llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, UNNAMED);
311-
llvm::LLVMRustSetFastMath(instr);
312-
instr
313-
}
314-
}
315-
316-
fn fsub_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
317-
unsafe {
318-
let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, UNNAMED);
319-
llvm::LLVMRustSetFastMath(instr);
320-
instr
321-
}
322-
}
323-
324-
fn fmul_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
325-
unsafe {
326-
let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, UNNAMED);
327-
llvm::LLVMRustSetFastMath(instr);
328-
instr
329-
}
330-
}
331-
332-
fn fdiv_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
333-
unsafe {
334-
let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, UNNAMED);
335-
llvm::LLVMRustSetFastMath(instr);
336-
instr
337-
}
338-
}
339-
340-
fn frem_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
341-
unsafe {
342-
let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, UNNAMED);
343-
llvm::LLVMRustSetFastMath(instr);
344-
instr
345-
}
346-
}
347-
348-
fn fadd_algebraic(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
349-
unsafe {
350-
let instr = llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, UNNAMED);
351-
llvm::LLVMRustSetAlgebraicMath(instr);
352-
instr
353-
}
354-
}
355-
356-
fn fsub_algebraic(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
357-
unsafe {
358-
let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, UNNAMED);
359-
llvm::LLVMRustSetAlgebraicMath(instr);
360-
instr
361-
}
362-
}
363-
364-
fn fmul_algebraic(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
365-
unsafe {
366-
let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, UNNAMED);
367-
llvm::LLVMRustSetAlgebraicMath(instr);
368-
instr
369-
}
370-
}
371-
372-
fn fdiv_algebraic(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
373-
unsafe {
374-
let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, UNNAMED);
375-
llvm::LLVMRustSetAlgebraicMath(instr);
376-
instr
377-
}
378-
}
379-
380-
fn frem_algebraic(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
381-
unsafe {
382-
let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, UNNAMED);
383-
llvm::LLVMRustSetAlgebraicMath(instr);
384-
instr
385-
}
320+
set_math_builder_methods! {
321+
fadd_fast(x, y) => (LLVMBuildFAdd, LLVMRustSetFastMath),
322+
fsub_fast(x, y) => (LLVMBuildFSub, LLVMRustSetFastMath),
323+
fmul_fast(x, y) => (LLVMBuildFMul, LLVMRustSetFastMath),
324+
fdiv_fast(x, y) => (LLVMBuildFDiv, LLVMRustSetFastMath),
325+
frem_fast(x, y) => (LLVMBuildFRem, LLVMRustSetFastMath),
326+
fadd_algebraic(x, y) => (LLVMBuildFAdd, LLVMRustSetAlgebraicMath),
327+
fsub_algebraic(x, y) => (LLVMBuildFSub, LLVMRustSetAlgebraicMath),
328+
fmul_algebraic(x, y) => (LLVMBuildFMul, LLVMRustSetAlgebraicMath),
329+
fdiv_algebraic(x, y) => (LLVMBuildFDiv, LLVMRustSetAlgebraicMath),
330+
frem_algebraic(x, y) => (LLVMBuildFRem, LLVMRustSetAlgebraicMath),
386331
}
387332

388333
fn checked_binop(
@@ -465,6 +410,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
465410
val
466411
}
467412
}
413+
468414
fn to_immediate_scalar(&mut self, val: Self::Value, scalar: abi::Scalar) -> Self::Value {
469415
if scalar.is_bool() {
470416
return self.trunc(val, self.cx().type_i1());
@@ -1166,6 +1112,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
11661112
(val, success)
11671113
}
11681114
}
1115+
11691116
fn atomic_rmw(
11701117
&mut self,
11711118
op: rustc_codegen_ssa::common::AtomicRmwBinOp,

0 commit comments

Comments
 (0)