some opt
This commit is contained in:
36
midend/optimize/MemToReg.java
Normal file
36
midend/optimize/MemToReg.java
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user