llvmir some opt

This commit is contained in:
邓智航
2025-12-10 17:58:17 +08:00
commit 84827838e2
103 changed files with 5838 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
package midend.llvm.instr;
import midend.llvm.type.IrPointerType;
import midend.llvm.type.IrType;
public class AllocateInstr extends IrInstr {
private IrType pointeeType;
public AllocateInstr(IrType pointeeType, String name) { // name即为局部变量的name因为要声明一个局部变量
super(new IrPointerType(pointeeType), name, IrInstrType.ALLOCA);
this.pointeeType = pointeeType;
}
public IrType getPointeeType() {
return pointeeType;
}
public String toString() {
return getName() + " = alloca " + this.pointeeType;
}
}

View File

@@ -0,0 +1,24 @@
package midend.llvm.instr;
import midend.llvm.type.IrInterType;
import midend.llvm.value.IrValue;
public class AluInstr extends IrInstr {
private AluType alutype;
public AluInstr(String name, String op, IrValue left, IrValue right) {
super(IrInterType.INT32, name, IrInstrType.ALU);
this.alutype = AluType.getAluType(op);
addUse(left);
addUse(right);
}
public AluType getAluType() {
return alutype;
}
public String toString() {
return getName() + " = " + alutype.toString() + " " + getType()
+ " " + getUse(0).getName() + ", " + getUse(1).getName();
}
}

View File

@@ -0,0 +1,53 @@
package midend.llvm.instr;
public enum AluType {
ADD,
SUB,
MUL,
SDIV,
SREM,
AND,
OR;
public static AluType getAluType(String op) {
switch (op) {
case "+":
return ADD;
case "-":
return SUB;
case "*":
return MUL;
case "/":
return SDIV;
case "%":
return SREM;
case "&":
return AND;
case "|":
return OR;
default:
return null;
}
}
public String toString() {
switch (this) {
case ADD:
return "add";
case SUB:
return "sub";
case MUL:
return "mul";
case SDIV:
return "sdiv";
case SREM:
return "srem";
case AND:
return "and";
case OR:
return "or";
default:
return null;
}
}
}

View File

@@ -0,0 +1,33 @@
package midend.llvm.instr;
import midend.llvm.value.IrBasicBlock;
import midend.llvm.value.IrValue;
import midend.llvm.type.IrInterType;
public class BranchInstr extends IrInstr {
public BranchInstr(String name, IrValue cond, IrBasicBlock trueBB, IrBasicBlock falseBB) {
super(IrInterType.VOID, name, IrInstrType.BR);
addUse(cond);
addUse(trueBB);
addUse(falseBB);
}
public IrValue getCond() {
return getUse(0);
}
public IrBasicBlock getTrueBB() {
return (IrBasicBlock) getUse(1);
}
public IrBasicBlock getFalseBB() {
return (IrBasicBlock) getUse(2);
}
public String toString() {
return "br i1 " + getCond().getName() +
", label %" + getTrueBB().getName() +
", label %" + getFalseBB().getName();
}
}

View File

@@ -0,0 +1,48 @@
package midend.llvm.instr;
import java.util.ArrayList;
import midend.llvm.value.IrFuncValue;
import midend.llvm.value.IrValue;
public class CallInstr extends IrInstr {
public CallInstr(String name, IrFuncValue func, ArrayList<IrValue> args) {
super(func.getRetType(), name, IrInstrType.CALL);
addUse(func);
for (IrValue arg : args) {
addUse(arg);
}
}
public boolean callVoid() {
return getType().isVoid();
}
public IrFuncValue getCalledFunc() {
return (IrFuncValue) getUse(0);
}
public ArrayList<IrValue> getArgs() {
ArrayList<IrValue> args = new ArrayList<>();
for (int i = 1; i < getNumUses(); i++) {
args.add(getUse(i));
}
return args;
}
public String toString() {
StringBuilder sb = new StringBuilder();
if (!callVoid()) {
sb.append(getName() + " = ");
}
sb.append("call " + getType() + " " + getCalledFunc().getName() + "(");
for (int i = 1; i < getNumUses(); i++) {
sb.append(getUse(i).getType() + " " + getUse(i).getName());
if (i < getNumUses() - 1) {
sb.append(", ");
}
}
sb.append(")");
return sb.toString();
}
}

View File

@@ -0,0 +1,32 @@
package midend.llvm.instr;
import midend.llvm.value.IrValue;
import midend.llvm.type.IrInterType;
public class CmpInstr extends IrInstr {
private CmpType cmpType;
public CmpInstr(String name, String op, IrValue lhs, IrValue rhs) {
super(IrInterType.BOOL, name, IrInstrType.CMP);
cmpType = CmpType.getCmpType(op);
addUse(lhs);
addUse(rhs);
}
public CmpType getCmpType() {
return cmpType;
}
public IrValue getLhs() {
return getUse(0);
}
public IrValue getRhs() {
return getUse(1);
}
public String toString() {
return getName() + " = " + "icmp " + cmpType.toString()
+ " i32 " + getLhs().getName() + ", " + getRhs().getName();
}
}

View File

@@ -0,0 +1,48 @@
package midend.llvm.instr;
public enum CmpType {
EQ,
NE,
SGT,
SGE,
SLT,
SLE;
public static CmpType getCmpType(String op) {
switch (op) {
case "==":
return EQ;
case "!=":
return NE;
case ">":
return SGT;
case ">=":
return SGE;
case "<":
return SLT;
case "<=":
return SLE;
default:
return null;
}
}
public String toString() {
switch (this) {
case EQ:
return "eq";
case NE:
return "ne";
case SGT:
return "sgt";
case SGE:
return "sge";
case SLT:
return "slt";
case SLE:
return "sle";
default:
return null;
}
}
}

View File

@@ -0,0 +1,31 @@
package midend.llvm.instr;
import midend.llvm.type.IrType;
import midend.llvm.value.IrValue;
public class ExtendInstr extends IrInstr {
private IrType targetType;
public ExtendInstr(String name, IrType targetType, IrValue src) {
super(targetType, name, IrInstrType.EXTEND);
this.targetType = targetType;
addUse(src);
}
public IrValue getSrc() {
return getUse(0);
}
public IrType getTargetType() {
return this.targetType;
}
public IrType getSrcType() {
return getSrc().getType();
}
public String toString() {
return getName() + " = zext " + getSrc().getType()
+ " " + getSrc().getName() + " to " + getTargetType();
}
}

View File

@@ -0,0 +1,57 @@
package midend.llvm.instr;
import midend.llvm.type.IrArrayType;
import midend.llvm.type.IrPointerType;
import midend.llvm.type.IrType;
import midend.llvm.value.IrValue;
public class GepInstr extends IrInstr {
public GepInstr(IrValue pointer, IrValue offset, String name) {
super(new IrPointerType(getTargetType(pointer)), name, IrInstrType.GEP);
addUse(pointer);
addUse(offset);
}
public IrValue getPointer() {
return getUse(0);
}
public IrValue getOffset() {
return getUse(1);
}
public String toString() {
IrValue pointer = this.getPointer();
IrValue offset = this.getOffset();
IrPointerType pointerType = (IrPointerType) pointer.getType();
IrType targetType = pointerType.getPointeeType();
if (targetType instanceof IrArrayType arrayType) {
return getName() + " = getelementptr inbounds " +
arrayType + ", " +
pointerType + " " +
pointer.getName() + ", i32 0, " +
offset.getType() + " " +
offset.getName();
} else {
return getName() + " = getelementptr inbounds " +
targetType + ", " +
pointerType + " " +
pointer.getName() + ", " +
offset.getType() + " " +
offset.getName();
}
}
public static IrType getTargetType(IrValue pointer) {
IrType targetType = ((IrPointerType) pointer.getType()).getPointeeType();
if (targetType instanceof IrArrayType arrayType) {
return arrayType.getElementType();
} else if (targetType instanceof IrPointerType pointerType) {
return pointerType.getPointeeType();
} else {
return targetType;
}
}
}

View File

@@ -0,0 +1,17 @@
package midend.llvm.instr;
import midend.llvm.type.IrInterType;
public class GetIntInstr extends IrInstr {
public GetIntInstr(String name) {
super(IrInterType.INT32, name, IrInstrType.IO);
}
public String toString() {
return getName() + " = call i32 @getint()";
}
public static String getIntDecl() {
return "declare i32 @getint()";
}
}

View File

@@ -0,0 +1,28 @@
package midend.llvm.instr;
import midend.llvm.use.IrUser;
import midend.llvm.value.IrBasicBlock;
import midend.llvm.type.IrType;
public class IrInstr extends IrUser {
private IrInstrType type;
private IrBasicBlock block;
public IrInstr(IrType type, String name, IrInstrType instrType) {
super(type, name);
this.type = instrType;
this.block = null;
}
public IrInstrType getInstrType() {
return type;
}
public IrBasicBlock getBBlock() {
return block;
}
public void setBBlock(IrBasicBlock block) {
this.block = block;
}
}

View File

@@ -0,0 +1,18 @@
package midend.llvm.instr;
public enum IrInstrType {
ALU,
CMP,
CALL,
ALLOCA,
LOAD,
STORE,
GEP,
PHI,
EXTEND,
TRUNC,
BR,
RET,
JUMP,
IO
}

View File

@@ -0,0 +1,20 @@
package midend.llvm.instr;
import midend.llvm.value.IrBasicBlock;
import midend.llvm.type.IrInterType;
public class JumpInstr extends IrInstr {
public JumpInstr(IrBasicBlock targetBlock) {
super(IrInterType.VOID, "jump", IrInstrType.JUMP);
addUse(targetBlock);
}
public IrBasicBlock getTargetBlock() {
return (IrBasicBlock) getUse(0);
}
public String toString() {
return "br label " + "%" + getTargetBlock().getName();
}
}
// TODO:所有的指令的基本块设置还需完善

View File

@@ -0,0 +1,20 @@
package midend.llvm.instr;
import midend.llvm.type.IrPointerType;
import midend.llvm.value.IrValue;
public class LoadInstr extends IrInstr {
public LoadInstr(IrValue pointer, String name) {
super(((IrPointerType) pointer.getType()).getPointeeType(), name, IrInstrType.LOAD);
addUse(pointer);
}
public IrValue getPointer() {
return getUse(0);
}
public String toString() {
return getName() + " = load " + getType() + ", "
+ getPointer().getType() + " " + getPointer().getName();
}
}

View File

@@ -0,0 +1,19 @@
package midend.llvm.instr;
import midend.llvm.type.IrInterType;
import midend.llvm.value.IrValue;
public class PutChInstr extends IrInstr {
public PutChInstr(String name, IrValue putValue) {
super(IrInterType.VOID, name, IrInstrType.IO);
addUse(putValue);
}
public String toString() {
return "call void @putch(i32 " + getUses().get(0).getName() + ")";
}
public static String putChDecl() {
return "declare void @putch(i32)";
}
}

View File

@@ -0,0 +1,19 @@
package midend.llvm.instr;
import midend.llvm.type.IrInterType;
import midend.llvm.value.IrValue;
public class PutIntInstr extends IrInstr {
public PutIntInstr(String name, IrValue putValue) {
super(IrInterType.VOID, name, IrInstrType.IO);
addUse(putValue);
}
public String toString() {
return "call void @putint(i32 " + getUses().get(0).getName() + ")";
}
public static String putIntDecl() {
return "declare void @putint(i32)";
}
}

View File

@@ -0,0 +1,26 @@
package midend.llvm.instr;
import midend.llvm.type.IrInterType;
import midend.llvm.type.IrPointerType;
import midend.llvm.constant.IrConstantStr;
public class PutStrInstr extends IrInstr {
private IrConstantStr strVal;
public PutStrInstr(String name, IrConstantStr putValue) {
super(IrInterType.VOID, name, IrInstrType.IO);
addUse(putValue);
strVal = putValue;
}
public static String putStrDecl() {
return "declare void @putstr(i8*)";
}
public String toString() {
IrPointerType ptrType = (IrPointerType) strVal.getType();
return "call void @putstr(i8* getelementptr inbounds ("
+ ptrType.getPointeeType() + ", " + ptrType +
" " + strVal.getName() + ", i32 0, i32 0))";
}
}

View File

@@ -0,0 +1,20 @@
package midend.llvm.instr;
import midend.llvm.type.IrInterType;
import midend.llvm.value.IrValue;
public class ReturnInstr extends IrInstr {
public ReturnInstr(IrValue retValue) {
super(IrInterType.VOID, "ret", IrInstrType.RET);
addUse(retValue);
}
public IrValue getRetValue() {
return getUse(0);
}
public String toString() {
return getName() + (getRetValue() == null ? " void" :
" " + getRetValue().getType() + " " + getRetValue().getName());
}
}

View File

@@ -0,0 +1,25 @@
package midend.llvm.instr;
import midend.llvm.value.IrValue;
import midend.llvm.type.IrInterType;
public class StoreInstr extends IrInstr {
public StoreInstr(IrValue value, IrValue pointer) {
super(IrInterType.VOID, "store", IrInstrType.STORE);
addUse(value);
addUse(pointer);
}
public IrValue getValue() {
return getUse(0);
}
public IrValue getPointer() {
return getUse(1);
}
public String toString() {
return getName() + " " + getValue().getType() + " " + getValue().getName()
+ ", " + getPointer().getType() + " " + getPointer().getName();
}
}

View File

@@ -0,0 +1,31 @@
package midend.llvm.instr;
import midend.llvm.type.IrType;
import midend.llvm.value.IrValue;
public class TruncInstr extends IrInstr {
private IrType targetType;
public TruncInstr(IrType targetType, IrValue value, String name) {
super(targetType, name, IrInstrType.TRUNC);
addUse(value);
this.targetType = targetType;
}
public IrValue getSrc() {
return getUse(0);
}
public IrType getTargetType() {
return targetType;
}
public IrType getSrcType() {
return getSrc().getType();
}
public String toString() {
return getName() + " = trunc " + getSrcType()
+ " " + getSrc().getName() + " to " + getTargetType();
}
}