// %v = <some-operand>
// .... %v ...
// %v not used or defined anywhere else
// So, %v can be replaced by the operand
else if ((uses.size() == 1) && (defs != null) && (defs.size() == 1) && (i instanceof CopyInstr)) {
CopyInstr ci = (CopyInstr)i;
Operand src = ci.getSource();
i.markDead();
instrs.remove();
// Fix up use
Map<Operand, Operand> copyMap = new HashMap<Operand, Operand>();
copyMap.put(v, src);
Instr soleUse = uses.get(0);
soleUse.simplifyOperands(copyMap, true);
}
}
// Deal with this code pattern:
// 1: %v = ... (not a copy)
// 2: x = %v
// If %v is not used anywhere else, the result of 1. can be updated to use x and 2. can be removed
//
// NOTE: consider this pattern:
// %v = <operand> (copy instr)
// x = %v
// This code will have been captured in the previous if branch which would have deleted %v = 5
// Hence the check for whether the src def instr is dead
else if (i instanceof CopyInstr) {
CopyInstr ci = (CopyInstr)i;
Operand src = ci.getSource();
if (src instanceof TemporaryVariable) {
TemporaryVariable vsrc = (TemporaryVariable)src;
List<Instr> uses = tmpVarUses.get(vsrc);
List<Instr> defs = tmpVarDefs.get(vsrc);
if ((uses.size() == 1) && (defs.size() == 1)) {
Instr soleDef = defs.get(0);
if (!soleDef.isDead()) {
// Fix up def
((ResultInstr)soleDef).updateResult(ci.getResult());
ci.markDead();
instrs.remove();
}
}
}
}