| 1179 | } |
| 1180 | |
| 1181 | LValuePtr LLVMGenerator::Visitor::BuildIfElse(llvm::Value* condition, |
| 1182 | std::function<LValuePtr()> then_func, |
| 1183 | std::function<LValuePtr()> else_func, |
| 1184 | DataTypePtr result_type) { |
| 1185 | llvm::IRBuilder<>* builder = ir_builder(); |
| 1186 | llvm::LLVMContext* context = generator_->context(); |
| 1187 | LLVMTypes* types = generator_->types(); |
| 1188 | |
| 1189 | // Create blocks for the then, else and merge cases. |
| 1190 | llvm::BasicBlock* then_bb = llvm::BasicBlock::Create(*context, "then", function_); |
| 1191 | llvm::BasicBlock* else_bb = llvm::BasicBlock::Create(*context, "else", function_); |
| 1192 | llvm::BasicBlock* merge_bb = llvm::BasicBlock::Create(*context, "merge", function_); |
| 1193 | |
| 1194 | builder->CreateCondBr(condition, then_bb, else_bb); |
| 1195 | |
| 1196 | // Emit the then block. |
| 1197 | builder->SetInsertPoint(then_bb); |
| 1198 | LValuePtr then_lvalue = then_func(); |
| 1199 | builder->CreateBr(merge_bb); |
| 1200 | |
| 1201 | // refresh then_bb for phi (could have changed due to code generation of then_vv). |
| 1202 | then_bb = builder->GetInsertBlock(); |
| 1203 | |
| 1204 | // Emit the else block. |
| 1205 | builder->SetInsertPoint(else_bb); |
| 1206 | LValuePtr else_lvalue = else_func(); |
| 1207 | builder->CreateBr(merge_bb); |
| 1208 | |
| 1209 | // refresh else_bb for phi (could have changed due to code generation of else_vv). |
| 1210 | else_bb = builder->GetInsertBlock(); |
| 1211 | |
| 1212 | // Emit the merge block. |
| 1213 | builder->SetInsertPoint(merge_bb); |
| 1214 | auto llvm_type = types->IRType(result_type->id()); |
| 1215 | llvm::PHINode* result_value = builder->CreatePHI(llvm_type, 2, "res_value"); |
| 1216 | result_value->addIncoming(then_lvalue->data(), then_bb); |
| 1217 | result_value->addIncoming(else_lvalue->data(), else_bb); |
| 1218 | |
| 1219 | LValuePtr ret; |
| 1220 | switch (result_type->id()) { |
| 1221 | case arrow::Type::STRING: |
| 1222 | case arrow::Type::BINARY: { |
| 1223 | llvm::PHINode* result_length; |
| 1224 | result_length = builder->CreatePHI(types->i32_type(), 2, "res_length"); |
| 1225 | result_length->addIncoming(then_lvalue->length(), then_bb); |
| 1226 | result_length->addIncoming(else_lvalue->length(), else_bb); |
| 1227 | ret = std::make_shared<LValue>(result_value, result_length); |
| 1228 | break; |
| 1229 | } |
| 1230 | |
| 1231 | case arrow::Type::DECIMAL: |
| 1232 | ret = generator_->BuildDecimalLValue(result_value, result_type); |
| 1233 | break; |
| 1234 | |
| 1235 | default: |
| 1236 | ret = std::make_shared<LValue>(result_value); |
| 1237 | break; |
| 1238 | } |
nothing calls this directly
no test coverage detected