some opt
This commit is contained in:
57
midend/llvm/instr/PhiInstr.java
Normal file
57
midend/llvm/instr/PhiInstr.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package midend.llvm.instr;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import midend.llvm.type.IrType;
|
||||
import midend.llvm.value.IrBasicBlock;
|
||||
import midend.llvm.value.IrValue;
|
||||
|
||||
public class PhiInstr extends IrInstr {
|
||||
private ArrayList<IrBasicBlock> predBBs;
|
||||
|
||||
public PhiInstr(IrType type, IrBasicBlock bblock, String name) {
|
||||
super(type, name, IrInstrType.PHI);
|
||||
this.setBBlock(bblock);
|
||||
this.predBBs = new ArrayList<>(bblock.getPreds());
|
||||
for (IrBasicBlock bb : predBBs) {
|
||||
this.addUse(null);
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<IrBasicBlock> getPredBBs() {
|
||||
return predBBs;
|
||||
}
|
||||
|
||||
public void setValueForPred(IrBasicBlock pred, IrValue value) {
|
||||
int index = predBBs.indexOf(pred);
|
||||
this.setUse(index, value);
|
||||
}
|
||||
|
||||
public void deletePred(IrBasicBlock pred) {
|
||||
int index = predBBs.indexOf(pred);
|
||||
if (index != -1) {
|
||||
predBBs.remove(index);
|
||||
this.deleteUse(index);
|
||||
}
|
||||
}
|
||||
|
||||
public void replacePred(IrBasicBlock oldPred, IrBasicBlock newPred) {
|
||||
//TODO: whether the function is needed
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(getName());
|
||||
sb.append(" = phi ");
|
||||
sb.append(getType().toString());
|
||||
for (int i = 0; i < this.predBBs.size(); i++) {
|
||||
sb.append("[ ");
|
||||
sb.append(getUse(i).getName());
|
||||
sb.append(", %");
|
||||
sb.append(this.predBBs.get(i).getName());
|
||||
sb.append(" ]");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -18,11 +18,10 @@ public class IrUser extends IrValue {
|
||||
}
|
||||
|
||||
public void addUse(IrValue value) {
|
||||
if (value == null) {
|
||||
return;
|
||||
}
|
||||
uses.add(value);
|
||||
value.addUser(this);
|
||||
if (value != null) {
|
||||
value.addUser(this);
|
||||
}
|
||||
}
|
||||
|
||||
public IrValue getUse(int index) {
|
||||
@@ -32,6 +31,44 @@ public class IrUser extends IrValue {
|
||||
return uses.get(index);
|
||||
}
|
||||
|
||||
public void setUse(int index, IrValue value) {
|
||||
if (index >= uses.size() || index < 0) {
|
||||
return;
|
||||
}
|
||||
IrValue oldValue = uses.get(index);
|
||||
if (oldValue != null) {
|
||||
oldValue.deleteUser(this);
|
||||
}
|
||||
uses.set(index, value);
|
||||
if (value != null) {
|
||||
value.addUser(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteUse(IrValue value) {
|
||||
uses.remove(value);
|
||||
if (value != null) {
|
||||
value.deleteUser(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void clearUses() {
|
||||
for (IrValue value : uses) {
|
||||
if (value != null) {
|
||||
value.deleteUser(this);
|
||||
}
|
||||
}
|
||||
uses.clear();
|
||||
}
|
||||
|
||||
public void deleteUse(int index) {
|
||||
if (index >= uses.size() || index < 0) {
|
||||
return;
|
||||
}
|
||||
IrValue value = uses.get(index);
|
||||
deleteUse(value);
|
||||
}
|
||||
|
||||
public int getNumUses() {
|
||||
return uses.size();
|
||||
}
|
||||
|
||||
@@ -35,6 +35,27 @@ public class IrBasicBlock extends IrValue {
|
||||
instr.setBBlock(this);
|
||||
}
|
||||
|
||||
public void addInstr(IrInstr instr, int index) {
|
||||
instrs.add(index, instr);
|
||||
instr.setBBlock(this);
|
||||
}
|
||||
|
||||
public void deleteInstr(IrInstr instr) {
|
||||
instrs.remove(instr);
|
||||
instr.setBBlock(null);
|
||||
instr.clearUses();
|
||||
instr.clearUsers();
|
||||
}
|
||||
|
||||
public void clearAllInstrs() {
|
||||
for (IrInstr instr : instrs) {
|
||||
instr.setBBlock(null);
|
||||
instr.clearUses();
|
||||
instr.clearUsers();
|
||||
}
|
||||
instrs.clear();
|
||||
}
|
||||
|
||||
public IrFuncValue getFunc() {
|
||||
return func;
|
||||
}
|
||||
@@ -87,6 +108,20 @@ public class IrBasicBlock extends IrValue {
|
||||
return instrs;
|
||||
}
|
||||
|
||||
public IrInstr getFirstInstr() {
|
||||
if (instrs.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return instrs.get(0);
|
||||
}
|
||||
|
||||
public IrInstr getLastInstr() {
|
||||
if (instrs.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return instrs.get(instrs.size() - 1);
|
||||
}
|
||||
|
||||
public void addPred(IrBasicBlock bb) {
|
||||
this.preds.add(bb);
|
||||
}
|
||||
@@ -119,6 +154,14 @@ public class IrBasicBlock extends IrValue {
|
||||
return this.directDomi;
|
||||
}
|
||||
|
||||
public HashSet<IrBasicBlock> getDirectDomies() {
|
||||
return directDomies;
|
||||
}
|
||||
|
||||
public HashSet<IrBasicBlock> getDomiFrontier() {
|
||||
return domiFrontier;
|
||||
}
|
||||
|
||||
public void toMips() {
|
||||
new MipsLabel(getMipsLabel());
|
||||
for (IrInstr instr : instrs) {
|
||||
|
||||
@@ -45,6 +45,27 @@ public class IrFuncValue extends IrValue {
|
||||
bblocks.add(bblock);
|
||||
}
|
||||
|
||||
public void deleteBBlock(IrBasicBlock bblock) {
|
||||
bblocks.remove(bblock);
|
||||
bblock.clearAllInstrs();
|
||||
}
|
||||
|
||||
public void deleteDeadBlock() {
|
||||
ArrayList<IrBasicBlock> liveBlocks = new ArrayList<>();
|
||||
ArrayList<IrBasicBlock> deadBlocks = new ArrayList<>();
|
||||
for (IrBasicBlock bb : bblocks) {
|
||||
if (!bb.getPreds().isEmpty() || bb.isEntry()) {
|
||||
liveBlocks.add(bb);
|
||||
} else {
|
||||
deadBlocks.add(bb);
|
||||
}
|
||||
}
|
||||
bblocks = liveBlocks;
|
||||
for (IrBasicBlock bb : deadBlocks) {
|
||||
bb.clearAllInstrs();
|
||||
}
|
||||
}
|
||||
|
||||
public Register getRegister(IrValue value) {
|
||||
return valueRegisterMap.get(value);
|
||||
}
|
||||
|
||||
@@ -32,6 +32,28 @@ public class IrValue {
|
||||
users.add(user);
|
||||
}
|
||||
|
||||
public void deleteUser(IrUser user) {
|
||||
users.remove(user);
|
||||
}
|
||||
|
||||
public void clearUsers() {
|
||||
ArrayList<IrUser> usersCopy = new ArrayList<>(users);
|
||||
for (IrUser user : usersCopy) {
|
||||
user.deleteUse(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void replaceUserToAnother(IrValue newValue) {
|
||||
ArrayList<IrUser> usersCopy = new ArrayList<>(users);
|
||||
for (IrUser user : usersCopy) {
|
||||
for (int i = 0; i < user.getUses().size(); i++) {
|
||||
if (user.getUse(i) == this) {
|
||||
user.setUse(i, newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return type.toString() + " " + name;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user