Skip to content

Commit fb95096

Browse files
authored
Merge pull request #549 from sapir/value-counter
Replace stack_var_count and RETURN_VALUE_COUNT with a single counter
2 parents 0475182 + 014a840 commit fb95096

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

Diff for: src/builder.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ use crate::type_of::LayoutGccExt;
4141
// TODO(antoyo)
4242
type Funclet = ();
4343

44-
// TODO(antoyo): remove this variable.
45-
static mut RETURN_VALUE_COUNT: usize = 0;
46-
4744
enum ExtremumOperation {
4845
Max,
4946
Min,
@@ -52,13 +49,18 @@ enum ExtremumOperation {
5249
pub struct Builder<'a: 'gcc, 'gcc, 'tcx> {
5350
pub cx: &'a CodegenCx<'gcc, 'tcx>,
5451
pub block: Block<'gcc>,
55-
stack_var_count: Cell<usize>,
5652
pub location: Option<Location<'gcc>>,
53+
value_counter: Cell<u64>,
5754
}
5855

5956
impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
6057
fn with_cx(cx: &'a CodegenCx<'gcc, 'tcx>, block: Block<'gcc>) -> Self {
61-
Builder { cx, block, stack_var_count: Cell::new(0), location: None }
58+
Builder { cx, block, location: None, value_counter: Cell::new(0) }
59+
}
60+
61+
fn next_value_counter(&self) -> u64 {
62+
self.value_counter.set(self.value_counter.get() + 1);
63+
self.value_counter.get()
6264
}
6365

6466
fn atomic_extremum(
@@ -324,11 +326,10 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
324326
let void_type = self.context.new_type::<()>();
325327
let current_func = self.block.get_function();
326328
if return_type != void_type {
327-
unsafe { RETURN_VALUE_COUNT += 1 };
328329
let result = current_func.new_local(
329330
self.location,
330331
return_type,
331-
&format!("returnValue{}", unsafe { RETURN_VALUE_COUNT }),
332+
&format!("returnValue{}", self.next_value_counter()),
332333
);
333334
self.block.add_assignment(
334335
self.location,
@@ -384,7 +385,6 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
384385
let current_func = self.block.get_function();
385386

386387
if return_type != void_type {
387-
unsafe { RETURN_VALUE_COUNT += 1 };
388388
let return_value = self.cx.context.new_call_through_ptr(self.location, func_ptr, &args);
389389
let return_value = llvm::adjust_intrinsic_return_value(
390390
self,
@@ -397,7 +397,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
397397
let result = current_func.new_local(
398398
self.location,
399399
return_value.get_type(),
400-
&format!("ptrReturnValue{}", unsafe { RETURN_VALUE_COUNT }),
400+
&format!("ptrReturnValue{}", self.next_value_counter()),
401401
);
402402
self.block.add_assignment(self.location, result, return_value);
403403
result.to_rvalue()
@@ -446,11 +446,10 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
446446
let return_type = self.context.new_type::<bool>();
447447
let current_func = self.block.get_function();
448448
// TODO(antoyo): return the new_call() directly? Since the overflow function has no side-effects.
449-
unsafe { RETURN_VALUE_COUNT += 1 };
450449
let result = current_func.new_local(
451450
self.location,
452451
return_type,
453-
&format!("overflowReturnValue{}", unsafe { RETURN_VALUE_COUNT }),
452+
&format!("overflowReturnValue{}", self.next_value_counter()),
454453
);
455454
self.block.add_assignment(
456455
self.location,
@@ -934,9 +933,8 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
934933
fn alloca(&mut self, size: Size, align: Align) -> RValue<'gcc> {
935934
let ty = self.cx.type_array(self.cx.type_i8(), size.bytes()).get_aligned(align.bytes());
936935
// TODO(antoyo): It might be better to return a LValue, but fixing the rustc API is non-trivial.
937-
self.stack_var_count.set(self.stack_var_count.get() + 1);
938936
self.current_func()
939-
.new_local(self.location, ty, &format!("stack_var_{}", self.stack_var_count.get()))
937+
.new_local(self.location, ty, &format!("stack_var_{}", self.next_value_counter()))
940938
.get_address(self.location)
941939
}
942940

@@ -959,11 +957,10 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
959957
};
960958
let ptr = self.context.new_cast(self.location, ptr, aligned_type.make_pointer());
961959
let deref = ptr.dereference(self.location).to_rvalue();
962-
unsafe { RETURN_VALUE_COUNT += 1 };
963960
let loaded_value = function.new_local(
964961
self.location,
965962
aligned_type,
966-
&format!("loadedValue{}", unsafe { RETURN_VALUE_COUNT }),
963+
&format!("loadedValue{}", self.next_value_counter()),
967964
);
968965
block.add_assignment(self.location, loaded_value, deref);
969966
loaded_value.to_rvalue()

0 commit comments

Comments
 (0)