I have modified Kaleidoscope's if-then-else codegen, to support if-then (no else clause), for a non-functional language. I'm unable to get the merge block (after the 'then') to be created in the IR.
I have removed the Phi node creation, because (0) the phi node needs to merge 2 branches of execution and I only really have one branch (1) it's not really required in a non-functional language context? (2) the phi requires two legs of the same type and the statement above the 'if' statement might return a different type.
The if-then codegen is:
static IRBuilder<> Builder(TheContext);
:
Value* IfThenExprAST::Codegen() {
Value* CondV = Cond->Codegen();
CondV = Builder.CreateICmpNE(CondV, Builder.getInt1(0), "ifcond");
Function *TheFunction = Builder.GetInsertBlock()->getParent();
BasicBlock* ThenBB = BasicBlock::Create(TheContext, "then", TheFunction);
BasicBlock* MergeBB = BasicBlock::Create(TheContext, "ifcont");
Builder.CreateCondBr(CondV, ThenBB, MergeBB);
Builder.SetInsertPoint(ThenBB);
llvm::Value* ThenV;
for(std::vector<ExprAST*>::iterator it = Then.begin(); it != Then.end(); ++it)
ThenV = (*it)->Codegen();
Builder.CreateBr(MergeBB);
return CondV;
}
The resulting IR is:
:
br i1 true, label %then, label %ifcont
then: ; preds = %entry
br label %ifcont
:
ret double 1.000000e+01
Notice that there is no 'ifcont' label generated in the IR.
How do I tell the builder to insert the ifcont label after the 'then' clause?