From 1fd2d6d88f5f78d879bf38bb3fba7fa2e749d3b0 Mon Sep 17 00:00:00 2001 From: Stefan Weigl-Bosker Date: Thu, 19 Feb 2026 13:13:41 -0500 Subject: [willow]: initial IRBuilder API (#9) - add IRBuilder api - remove `name` field from `Value` - fix some bugs in IList interface - more verifier tests --- willow/lib/IR/IRBuilder.cpp | 210 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 willow/lib/IR/IRBuilder.cpp (limited to 'willow/lib/IR/IRBuilder.cpp') diff --git a/willow/lib/IR/IRBuilder.cpp b/willow/lib/IR/IRBuilder.cpp new file mode 100644 index 0000000..62e7a98 --- /dev/null +++ b/willow/lib/IR/IRBuilder.cpp @@ -0,0 +1,210 @@ +#include + +namespace willow { + +AddInst *IRBuilder::BuildAdd(Type type, Value *lhs, Value *rhs, + std::optional loc) { + auto *inst = new AddInst{type, lhs, rhs, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +MulInst *IRBuilder::BuildMul(Type type, Value *lhs, Value *rhs, + std::optional loc) { + auto *inst = new MulInst{type, lhs, rhs, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +SubInst *IRBuilder::BuildSub(Type type, Value *lhs, Value *rhs, + std::optional loc) { + auto *inst = new SubInst{type, lhs, rhs, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +DivInst *IRBuilder::BuildDiv(Type type, Value *lhs, Value *rhs, + std::optional loc) { + auto *inst = new DivInst{type, lhs, rhs, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +ModInst *IRBuilder::BuildMod(Type type, Value *lhs, Value *rhs, + std::optional loc) { + auto *inst = new ModInst{type, lhs, rhs, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +ShlInst *IRBuilder::BuildShl(Type type, Value *lhs, Value *rhs, + std::optional loc) { + auto *inst = new ShlInst{type, lhs, rhs, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +ShrInst *IRBuilder::BuildShr(Type type, Value *lhs, Value *rhs, + std::optional loc) { + auto *inst = new ShrInst{type, lhs, rhs, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +AshlInst *IRBuilder::BuildAshl(Type type, Value *lhs, Value *rhs, + std::optional loc) { + auto *inst = new AshlInst{type, lhs, rhs, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +AshrInst *IRBuilder::BuildAshr(Type type, Value *lhs, Value *rhs, + std::optional loc) { + auto *inst = new AshrInst{type, lhs, rhs, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +EqInst *IRBuilder::BuildEq(Value *lhs, Value *rhs, + std::optional loc) { + auto *inst = new EqInst{ctx.types().IntType(1), lhs, rhs, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +LtInst *IRBuilder::BuildLt(Value *lhs, Value *rhs, + std::optional loc) { + auto *inst = new LtInst{ctx.types().IntType(1), lhs, rhs, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +GtInst *IRBuilder::BuildGt(Value *lhs, Value *rhs, + std::optional loc) { + auto *inst = new GtInst{ctx.types().IntType(1), lhs, rhs, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +LeInst *IRBuilder::BuildLe(Value *lhs, Value *rhs, + std::optional loc) { + auto *inst = new LeInst{ctx.types().IntType(1), lhs, rhs, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +GeInst *IRBuilder::BuildGe(Value *lhs, Value *rhs, + std::optional loc) { + auto *inst = new GeInst{ctx.types().IntType(1), lhs, rhs, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +AndInst *IRBuilder::BuildAnd(Value *lhs, Value *rhs, + std::optional loc) { + auto *inst = new AndInst{ctx.types().IntType(1), lhs, rhs, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +OrInst *IRBuilder::BuildOr(Value *lhs, Value *rhs, + std::optional loc) { + auto *inst = new OrInst{ctx.types().IntType(1), lhs, rhs, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +NotInst *IRBuilder::BuildNot(Value *val, std::optional loc) { + auto *inst = new NotInst{ctx.types().IntType(1), val, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +JmpInst *IRBuilder::BuildJmp(BasicBlock *dst, std::optional loc) { + + auto *inst = new JmpInst{ctx.types().VoidType(), dst, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +BrInst *IRBuilder::BuildBr(Value *predicate, BasicBlock *truedst, + BasicBlock *falsedst, std::optional loc) { + auto *inst = + new BrInst{ctx.types().VoidType(), predicate, truedst, falsedst, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +RetInst *IRBuilder::BuildRet(Value *val, std::optional loc) { + auto *inst = new RetInst{ctx.types().VoidType(), val, loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} +RetInst *IRBuilder::BuildRet(std::optional loc) { + auto *inst = new RetInst{ctx.types().VoidType(), loc}; + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +PhiInst *IRBuilder::BuildPhi( + Type ty, std::initializer_list> args, + std::optional loc) { + auto *inst = new PhiInst(ty, args, loc); + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +AllocaInst *IRBuilder::BuildAlloca(Type ty, std::optional loc) { + Type pty = ctx.types().PtrType(ty); + auto *inst = new AllocaInst(pty, loc); + insert_point->insertAfter(*inst); + insert_point++; + + return inst; +} + +}; // namespace willow -- cgit v1.2.3