| 22 | namespace gandiva { |
| 23 | |
| 24 | llvm::Value* FunctionIRBuilder::BuildIfElse(llvm::Value* condition, |
| 25 | llvm::Type* return_type, |
| 26 | std::function<llvm::Value*()> then_func, |
| 27 | std::function<llvm::Value*()> else_func) { |
| 28 | llvm::IRBuilder<>* builder = ir_builder(); |
| 29 | llvm::Function* function = builder->GetInsertBlock()->getParent(); |
| 30 | DCHECK_NE(function, nullptr); |
| 31 | |
| 32 | // Create blocks for the then, else and merge cases. |
| 33 | llvm::BasicBlock* then_bb = llvm::BasicBlock::Create(*context(), "then", function); |
| 34 | llvm::BasicBlock* else_bb = llvm::BasicBlock::Create(*context(), "else", function); |
| 35 | llvm::BasicBlock* merge_bb = llvm::BasicBlock::Create(*context(), "merge", function); |
| 36 | |
| 37 | builder->CreateCondBr(condition, then_bb, else_bb); |
| 38 | |
| 39 | // Emit the then block. |
| 40 | builder->SetInsertPoint(then_bb); |
| 41 | auto then_value = then_func(); |
| 42 | builder->CreateBr(merge_bb); |
| 43 | |
| 44 | // refresh then_bb for phi (could have changed due to code generation of then_value). |
| 45 | then_bb = builder->GetInsertBlock(); |
| 46 | |
| 47 | // Emit the else block. |
| 48 | builder->SetInsertPoint(else_bb); |
| 49 | auto else_value = else_func(); |
| 50 | builder->CreateBr(merge_bb); |
| 51 | |
| 52 | // refresh else_bb for phi (could have changed due to code generation of else_value). |
| 53 | else_bb = builder->GetInsertBlock(); |
| 54 | |
| 55 | // Emit the merge block. |
| 56 | builder->SetInsertPoint(merge_bb); |
| 57 | llvm::PHINode* result_value = builder->CreatePHI(return_type, 2, "res_value"); |
| 58 | result_value->addIncoming(then_value, then_bb); |
| 59 | result_value->addIncoming(else_value, else_bb); |
| 60 | return result_value; |
| 61 | } |
| 62 | |
| 63 | llvm::Function* FunctionIRBuilder::BuildFunction(const std::string& function_name, |
| 64 | llvm::Type* return_type, |
nothing calls this directly
no test coverage detected