Skip to content

Commit 54242b1

Browse files
committed
Add a try_as_constant+try_as_local helper
No behaviour changes.
1 parent cd0c944 commit 54242b1

File tree

1 file changed

+19
-19
lines changed
  • compiler/rustc_mir_transform/src

1 file changed

+19
-19
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -953,13 +953,8 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
953953
was_updated = true;
954954
}
955955

956-
if was_updated {
957-
if let Some(const_) = self.try_as_constant(fields[0]) {
958-
field_ops[FieldIdx::ZERO] = Operand::Constant(Box::new(const_));
959-
} else if let Some(local) = self.try_as_local(fields[0], location) {
960-
field_ops[FieldIdx::ZERO] = Operand::Copy(Place::from(local));
961-
self.reused_locals.insert(local);
962-
}
956+
if was_updated && let Some(op) = self.try_as_operand(fields[0], location) {
957+
field_ops[FieldIdx::ZERO] = op;
963958
}
964959
}
965960

@@ -969,11 +964,8 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
969964
let first = fields[0];
970965
if fields.iter().all(|&v| v == first) {
971966
let len = ty::Const::from_target_usize(self.tcx, fields.len().try_into().unwrap());
972-
if let Some(const_) = self.try_as_constant(first) {
973-
*rvalue = Rvalue::Repeat(Operand::Constant(Box::new(const_)), len);
974-
} else if let Some(local) = self.try_as_local(first, location) {
975-
*rvalue = Rvalue::Repeat(Operand::Copy(local.into()), len);
976-
self.reused_locals.insert(local);
967+
if let Some(op) = self.try_as_operand(first, location) {
968+
*rvalue = Rvalue::Repeat(op, len);
977969
}
978970
return Some(self.insert(Value::Repeat(first, len)));
979971
}
@@ -1178,13 +1170,8 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
11781170
}
11791171
}
11801172

1181-
if was_updated {
1182-
if let Some(const_) = self.try_as_constant(value) {
1183-
*operand = Operand::Constant(Box::new(const_));
1184-
} else if let Some(local) = self.try_as_local(value, location) {
1185-
*operand = Operand::Copy(local.into());
1186-
self.reused_locals.insert(local);
1187-
}
1173+
if was_updated && let Some(op) = self.try_as_operand(value, location) {
1174+
*operand = op;
11881175
}
11891176

11901177
Some(self.insert(Value::Cast { kind: *kind, value, from, to }))
@@ -1300,6 +1287,19 @@ fn op_to_prop_const<'tcx>(
13001287
}
13011288

13021289
impl<'tcx> VnState<'_, 'tcx> {
1290+
/// If either [`Self::try_as_constant`] as [`Self::try_as_local`] succeeds,
1291+
/// returns that result as an [`Operand`].
1292+
fn try_as_operand(&mut self, index: VnIndex, location: Location) -> Option<Operand<'tcx>> {
1293+
if let Some(const_) = self.try_as_constant(index) {
1294+
Some(Operand::Constant(Box::new(const_)))
1295+
} else if let Some(local) = self.try_as_local(index, location) {
1296+
self.reused_locals.insert(local);
1297+
Some(Operand::Copy(local.into()))
1298+
} else {
1299+
None
1300+
}
1301+
}
1302+
13031303
/// If `index` is a `Value::Constant`, return the `Constant` to be put in the MIR.
13041304
fn try_as_constant(&mut self, index: VnIndex) -> Option<ConstOperand<'tcx>> {
13051305
// This was already constant in MIR, do not change it.

0 commit comments

Comments
 (0)