This commit is contained in:
邓智航
2025-12-25 15:07:20 +08:00
parent c94bebf37b
commit a9b8e82fd5
12 changed files with 483 additions and 13 deletions

View File

@@ -0,0 +1,36 @@
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);
}
}