37 lines
1.2 KiB
Java
37 lines
1.2 KiB
Java
package midend.optimize;
|
|
|
|
import midend.llvm.value.IrBasicBlock;
|
|
import midend.llvm.value.IrFuncValue;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import midend.llvm.instr.AllocateInstr;
|
|
import midend.llvm.instr.IrInstr;
|
|
import midend.llvm.type.IrArrayType;
|
|
|
|
public class MemToReg extends Optimizer {
|
|
public void optimize() {
|
|
for (IrFuncValue func : getIrModule().getFuncs()) {
|
|
IrBasicBlock entryBlock = func.getBBlock(0);
|
|
for (IrBasicBlock block : func.getBBlocks()) {
|
|
ArrayList<IrInstr> instrs = new ArrayList<>(block.getInstrs());
|
|
for (IrInstr instr : instrs) {
|
|
if (normalAlloca(instr)) {
|
|
AllocateInstr allocInstr = (AllocateInstr) instr;
|
|
PhiInsert phiInsert = new PhiInsert(allocInstr, entryBlock);
|
|
phiInsert.run();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public boolean normalAlloca(IrInstr instr) {
|
|
if (!(instr instanceof AllocateInstr)) {
|
|
return false;
|
|
}
|
|
AllocateInstr allocInstr = (AllocateInstr) instr;
|
|
return !(allocInstr.getPointeeType() instanceof IrArrayType);
|
|
}
|
|
}
|