MCPcopy Create free account
hub / github.com/apache/arrow / BuildIfElse

Method BuildIfElse

cpp/src/gandiva/llvm_generator.cc:1183–1242  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 7

contextMethod · 0.80
typesMethod · 0.80
BuildDecimalLValueMethod · 0.80
ir_builderFunction · 0.70
idMethod · 0.45
dataMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected