1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 #include "gandiva/function_ir_builder.h"
19
20 namespace gandiva {
21
BuildIfElse(llvm::Value * condition,llvm::Type * return_type,std::function<llvm::Value * ()> then_func,std::function<llvm::Value * ()> else_func)22 llvm::Value* FunctionIRBuilder::BuildIfElse(llvm::Value* condition,
23 llvm::Type* return_type,
24 std::function<llvm::Value*()> then_func,
25 std::function<llvm::Value*()> else_func) {
26 llvm::IRBuilder<>* builder = ir_builder();
27 llvm::Function* function = builder->GetInsertBlock()->getParent();
28 DCHECK_NE(function, nullptr);
29
30 // Create blocks for the then, else and merge cases.
31 llvm::BasicBlock* then_bb = llvm::BasicBlock::Create(*context(), "then", function);
32 llvm::BasicBlock* else_bb = llvm::BasicBlock::Create(*context(), "else", function);
33 llvm::BasicBlock* merge_bb = llvm::BasicBlock::Create(*context(), "merge", function);
34
35 builder->CreateCondBr(condition, then_bb, else_bb);
36
37 // Emit the then block.
38 builder->SetInsertPoint(then_bb);
39 auto then_value = then_func();
40 builder->CreateBr(merge_bb);
41
42 // refresh then_bb for phi (could have changed due to code generation of then_value).
43 then_bb = builder->GetInsertBlock();
44
45 // Emit the else block.
46 builder->SetInsertPoint(else_bb);
47 auto else_value = else_func();
48 builder->CreateBr(merge_bb);
49
50 // refresh else_bb for phi (could have changed due to code generation of else_value).
51 else_bb = builder->GetInsertBlock();
52
53 // Emit the merge block.
54 builder->SetInsertPoint(merge_bb);
55 llvm::PHINode* result_value = builder->CreatePHI(return_type, 2, "res_value");
56 result_value->addIncoming(then_value, then_bb);
57 result_value->addIncoming(else_value, else_bb);
58 return result_value;
59 }
60
BuildFunction(const std::string & function_name,llvm::Type * return_type,std::vector<NamedArg> in_args)61 llvm::Function* FunctionIRBuilder::BuildFunction(const std::string& function_name,
62 llvm::Type* return_type,
63 std::vector<NamedArg> in_args) {
64 std::vector<llvm::Type*> arg_types;
65 for (auto& arg : in_args) {
66 arg_types.push_back(arg.type);
67 }
68 auto prototype = llvm::FunctionType::get(return_type, arg_types, false /*isVarArg*/);
69 auto function = llvm::Function::Create(prototype, llvm::GlobalValue::ExternalLinkage,
70 function_name, module());
71
72 uint32_t i = 0;
73 for (auto& fn_arg : function->args()) {
74 DCHECK_LT(i, in_args.size());
75 fn_arg.setName(in_args[i].name);
76 ++i;
77 }
78 return function;
79 }
80
81 } // namespace gandiva
82