Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
codegen-anyval.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "codegen/codegen-anyval.h"
16 
17 #include "common/names.h"
18 
19 using namespace impala;
20 using namespace impala_udf;
21 using namespace llvm;
22 
23 const char* CodegenAnyVal::LLVM_BOOLEANVAL_NAME = "struct.impala_udf::BooleanVal";
24 const char* CodegenAnyVal::LLVM_TINYINTVAL_NAME = "struct.impala_udf::TinyIntVal";
25 const char* CodegenAnyVal::LLVM_SMALLINTVAL_NAME = "struct.impala_udf::SmallIntVal";
26 const char* CodegenAnyVal::LLVM_INTVAL_NAME = "struct.impala_udf::IntVal";
27 const char* CodegenAnyVal::LLVM_BIGINTVAL_NAME = "struct.impala_udf::BigIntVal";
28 const char* CodegenAnyVal::LLVM_FLOATVAL_NAME = "struct.impala_udf::FloatVal";
29 const char* CodegenAnyVal::LLVM_DOUBLEVAL_NAME = "struct.impala_udf::DoubleVal";
30 const char* CodegenAnyVal::LLVM_STRINGVAL_NAME = "struct.impala_udf::StringVal";
31 const char* CodegenAnyVal::LLVM_TIMESTAMPVAL_NAME = "struct.impala_udf::TimestampVal";
32 const char* CodegenAnyVal::LLVM_DECIMALVAL_NAME = "struct.impala_udf::DecimalVal";
33 
35  switch(type.type) {
36  case TYPE_BOOLEAN: // i16
37  return cg->smallint_type();
38  case TYPE_TINYINT: // i16
39  return cg->smallint_type();
40  case TYPE_SMALLINT: // i32
41  return cg->int_type();
42  case TYPE_INT: // i64
43  return cg->bigint_type();
44  case TYPE_BIGINT: // { i8, i64 }
45  return StructType::get(cg->tinyint_type(), cg->bigint_type(), NULL);
46  case TYPE_FLOAT: // i64
47  return cg->bigint_type();
48  case TYPE_DOUBLE: // { i8, double }
49  return StructType::get(cg->tinyint_type(), cg->double_type(), NULL);
50  case TYPE_STRING: // { i64, i8* }
51  case TYPE_VARCHAR: // { i64, i8* }
52  case TYPE_CHAR:
53  return StructType::get(cg->bigint_type(), cg->ptr_type(), NULL);
54  case TYPE_TIMESTAMP: // { i64, i64 }
55  return StructType::get(cg->bigint_type(), cg->bigint_type(), NULL);
56  case TYPE_DECIMAL: // %"struct.impala_udf::DecimalVal" (isn't lowered)
57  // = { {i8}, [15 x i8], {i128} }
58  return cg->GetType(LLVM_DECIMALVAL_NAME);
59  default:
60  DCHECK(false) << "Unsupported type: " << type;
61  return NULL;
62  }
63 }
64 
66  return GetLoweredType(cg, type)->getPointerTo();
67 }
68 
70  Type* result;
71  switch(type.type) {
72  case TYPE_BOOLEAN:
73  result = cg->GetType(LLVM_BOOLEANVAL_NAME);
74  break;
75  case TYPE_TINYINT:
76  result = cg->GetType(LLVM_TINYINTVAL_NAME);
77  break;
78  case TYPE_SMALLINT:
79  result = cg->GetType(LLVM_SMALLINTVAL_NAME);
80  break;
81  case TYPE_INT:
82  result = cg->GetType(LLVM_INTVAL_NAME);
83  break;
84  case TYPE_BIGINT:
85  result = cg->GetType(LLVM_BIGINTVAL_NAME);
86  break;
87  case TYPE_FLOAT:
88  result = cg->GetType(LLVM_FLOATVAL_NAME);
89  break;
90  case TYPE_DOUBLE:
91  result = cg->GetType(LLVM_DOUBLEVAL_NAME);
92  break;
93  case TYPE_STRING:
94  case TYPE_VARCHAR:
95  case TYPE_CHAR:
96  result = cg->GetType(LLVM_STRINGVAL_NAME);
97  break;
98  case TYPE_TIMESTAMP:
99  result = cg->GetType(LLVM_TIMESTAMPVAL_NAME);
100  break;
101  case TYPE_DECIMAL:
102  result = cg->GetType(LLVM_DECIMALVAL_NAME);
103  break;
104  default:
105  DCHECK(false) << "Unsupported type: " << type;
106  return NULL;
107  }
108  DCHECK(result != NULL) << type.DebugString();
109  return result;
110 }
111 
113  return GetUnloweredType(cg, type)->getPointerTo();
114 }
115 
117  LlvmCodeGen* cg, LlvmCodeGen::LlvmBuilder* builder, Function* fn,
118  ArrayRef<Value*> args, const char* name, Value* result_ptr) {
119  if (fn->getReturnType()->isVoidTy()) {
120  // Void return type indicates that this function returns a DecimalVal via the first
121  // argument (which should be a DecimalVal*).
122  Function::arg_iterator ret_arg = fn->arg_begin();
123  DCHECK(ret_arg->getType()->isPointerTy());
124  Type* ret_type = ret_arg->getType()->getPointerElementType();
125  DCHECK_EQ(ret_type, cg->GetType(LLVM_DECIMALVAL_NAME));
126 
127  // We need to pass a DecimalVal pointer to 'fn' that will be populated with the result
128  // value. Use 'result_ptr' if specified, otherwise alloca one.
129  Value* ret_ptr = (result_ptr == NULL) ?
130  cg->CreateEntryBlockAlloca(*builder, ret_type, name) : result_ptr;
131  vector<Value*> new_args = args.vec();
132  new_args.insert(new_args.begin(), ret_ptr);
133  builder->CreateCall(fn, new_args);
134 
135  // If 'result_ptr' was specified, we're done. Otherwise load and return the result.
136  if (result_ptr != NULL) return NULL;
137  return builder->CreateLoad(ret_ptr, name);
138  } else {
139  // Function returns *Val normally (note that it could still be returning a DecimalVal,
140  // since we generate non-complaint functions)
141  Value* ret = builder->CreateCall(fn, args, name);
142  if (result_ptr == NULL) return ret;
143  builder->CreateStore(ret, result_ptr);
144  return NULL;
145  }
146 }
147 
149  LlvmCodeGen* cg, LlvmCodeGen::LlvmBuilder* builder, const ColumnType& type,
150  Function* fn, ArrayRef<Value*> args, const char* name, Value* result_ptr) {
151  Value* v = CreateCall(cg, builder, fn, args, name, result_ptr);
152  return CodegenAnyVal(cg, builder, type, v, name);
153 }
154 
156  const ColumnType& type, Value* value, const char* name)
157  : type_(type),
158  value_(value),
159  name_(name),
160  codegen_(codegen),
161  builder_(builder) {
162  Type* value_type = GetLoweredType(codegen, type);
163  if (value_ == NULL) {
164  // No Value* was specified, so allocate one on the stack and load it.
165  Value* ptr = codegen_->CreateEntryBlockAlloca(*builder, value_type);
166  value_ = builder_->CreateLoad(ptr, name_);
167  }
168  DCHECK_EQ(value_->getType(), value_type);
169 }
170 
171 Value* CodegenAnyVal::GetIsNull(const char* name) {
172  switch (type_.type) {
173  case TYPE_BIGINT:
174  case TYPE_DOUBLE: {
175  // Lowered type is of form { i8, * }. Get the i8 value.
176  Value* is_null_i8 = builder_->CreateExtractValue(value_, 0);
177  DCHECK(is_null_i8->getType() == codegen_->tinyint_type());
178  return builder_->CreateTrunc(is_null_i8, codegen_->boolean_type(), name);
179  }
180  case TYPE_DECIMAL: {
181  // Lowered type is of the form { {i8}, ... }
182  uint32_t idxs[] = {0, 0};
183  Value* is_null_i8 = builder_->CreateExtractValue(value_, idxs);
184  DCHECK(is_null_i8->getType() == codegen_->tinyint_type());
185  return builder_->CreateTrunc(is_null_i8, codegen_->boolean_type(), name);
186  }
187  case TYPE_STRING:
188  case TYPE_VARCHAR:
189  case TYPE_CHAR:
190  case TYPE_TIMESTAMP: {
191  // Lowered type is of form { i64, *}. Get the first byte of the i64 value.
192  Value* v = builder_->CreateExtractValue(value_, 0);
193  DCHECK(v->getType() == codegen_->bigint_type());
194  return builder_->CreateTrunc(v, codegen_->boolean_type(), name);
195  }
196  case TYPE_BOOLEAN:
197  case TYPE_TINYINT:
198  case TYPE_SMALLINT:
199  case TYPE_INT:
200  case TYPE_FLOAT:
201  // Lowered type is an integer. Get the first byte.
202  return builder_->CreateTrunc(value_, codegen_->boolean_type(), name);
203  default:
204  DCHECK(false);
205  return NULL;
206  }
207 }
208 
209 void CodegenAnyVal::SetIsNull(Value* is_null) {
210  switch(type_.type) {
211  case TYPE_BIGINT:
212  case TYPE_DOUBLE: {
213  // Lowered type is of form { i8, * }. Set the i8 value to 'is_null'.
214  Value* is_null_ext =
215  builder_->CreateZExt(is_null, codegen_->tinyint_type(), "is_null_ext");
216  value_ = builder_->CreateInsertValue(value_, is_null_ext, 0, name_);
217  break;
218  }
219  case TYPE_DECIMAL: {
220  // Lowered type is of form { {i8}, [15 x i8], {i128} }. Set the i8 value to
221  // 'is_null'.
222  Value* is_null_ext =
223  builder_->CreateZExt(is_null, codegen_->tinyint_type(), "is_null_ext");
224  // Index into the {i8} struct as well as the outer struct.
225  uint32_t idxs[] = {0, 0};
226  value_ = builder_->CreateInsertValue(value_, is_null_ext, idxs, name_);
227  break;
228  }
229  case TYPE_STRING:
230  case TYPE_VARCHAR:
231  case TYPE_CHAR:
232  case TYPE_TIMESTAMP: {
233  // Lowered type is of the form { i64, * }. Set the first byte of the i64 value to
234  // 'is_null'
235  Value* v = builder_->CreateExtractValue(value_, 0);
236  v = builder_->CreateAnd(v, -0x100LL, "masked");
237  Value* is_null_ext = builder_->CreateZExt(is_null, v->getType(), "is_null_ext");
238  v = builder_->CreateOr(v, is_null_ext);
239  value_ = builder_->CreateInsertValue(value_, v, 0, name_);
240  break;
241  }
242  case TYPE_BOOLEAN:
243  case TYPE_TINYINT:
244  case TYPE_SMALLINT:
245  case TYPE_INT:
246  case TYPE_FLOAT: {
247  // Lowered type is an integer. Set the first byte to 'is_null'.
248  value_ = builder_->CreateAnd(value_, -0x100LL, "masked");
249  Value* is_null_ext = builder_->CreateZExt(is_null, value_->getType(), "is_null_ext");
250  value_ = builder_->CreateOr(value_, is_null_ext, name_);
251  break;
252  }
253  default:
254  DCHECK(false) << "NYI: " << type_.DebugString();
255  }
256 }
257 
258 Value* CodegenAnyVal::GetVal(const char* name) {
259  DCHECK(type_.type != TYPE_STRING)
260  << "Use GetPtr and GetLen for StringVal";
261  DCHECK(type_.type != TYPE_VARCHAR)
262  << "Use GetPtr and GetLen for Varchar";
263  DCHECK(type_.type != TYPE_CHAR)
264  << "Use GetPtr and GetLen for Char";
265  DCHECK(type_.type != TYPE_TIMESTAMP)
266  << "Use GetDate and GetTimeOfDay for TimestampVals";
267  switch(type_.type) {
268  case TYPE_BOOLEAN:
269  case TYPE_TINYINT:
270  case TYPE_SMALLINT:
271  case TYPE_INT: {
272  // Lowered type is an integer. Get the high bytes.
273  int num_bits = type_.GetByteSize() * 8;
274  Value* val = GetHighBits(num_bits, value_, name);
275  if (type_.type == TYPE_BOOLEAN) {
276  // Return booleans as i1 (vs. i8)
277  val = builder_->CreateTrunc(val, builder_->getInt1Ty(), name);
278  }
279  return val;
280  }
281  case TYPE_FLOAT: {
282  // Same as above, but we must cast the value to a float.
283  Value* val = GetHighBits(32, value_);
284  return builder_->CreateBitCast(val, codegen_->float_type());
285  }
286  case TYPE_BIGINT:
287  case TYPE_DOUBLE:
288  // Lowered type is of form { i8, * }. Get the second value.
289  return builder_->CreateExtractValue(value_, 1, name);
290  case TYPE_DECIMAL: {
291  // Lowered type is of form { {i8}, [15 x i8], {i128} }. Get the i128 value and
292  // truncate it to the correct size. (The {i128} corresponds to the union of the
293  // different width int types.)
294  uint32_t idxs[] = {2, 0};
295  Value* val = builder_->CreateExtractValue(value_, idxs, name);
296  return builder_->CreateTrunc(val, codegen_->GetType(type_), name);
297  break;
298  }
299  default:
300  DCHECK(false) << "Unsupported type: " << type_;
301  return NULL;
302  }
303 }
304 
305 void CodegenAnyVal::SetVal(Value* val) {
306  DCHECK(type_.type != TYPE_STRING) << "Use SetPtr and SetLen for StringVals";
307  DCHECK(type_.type != TYPE_VARCHAR) << "Use SetPtr and SetLen for StringVals";
308  DCHECK(type_.type != TYPE_CHAR) << "Use SetPtr and SetLen for StringVals";
309  DCHECK(type_.type != TYPE_TIMESTAMP)
310  << "Use SetDate and SetTimeOfDay for TimestampVals";
311  switch(type_.type) {
312  case TYPE_BOOLEAN:
313  case TYPE_TINYINT:
314  case TYPE_SMALLINT:
315  case TYPE_INT: {
316  // Lowered type is an integer. Set the high bytes to 'val'.
317  int num_bits = type_.GetByteSize() * 8;
318  value_ = SetHighBits(num_bits, val, value_, name_);
319  break;
320  }
321  case TYPE_FLOAT:
322  // Same as above, but we must cast 'val' to an integer type.
323  val = builder_->CreateBitCast(val, codegen_->int_type());
324  value_ = SetHighBits(32, val, value_, name_);
325  break;
326  case TYPE_BIGINT:
327  case TYPE_DOUBLE:
328  // Lowered type is of form { i8, * }. Set the second value to 'val'.
329  value_ = builder_->CreateInsertValue(value_, val, 1, name_);
330  break;
331  case TYPE_DECIMAL: {
332  // Lowered type is of the form { {i8}, [15 x i8], {i128} }. Set the i128 value to
333  // 'val'. (The {i128} corresponds to the union of the different width int types.)
334  DCHECK_EQ(val->getType()->getIntegerBitWidth(), type_.GetByteSize() * 8);
335  val = builder_->CreateSExt(val, Type::getIntNTy(codegen_->context(), 128));
336  uint32_t idxs[] = {2, 0};
337  value_ = builder_->CreateInsertValue(value_, val, idxs, name_);
338  break;
339  }
340  default:
341  DCHECK(false) << "Unsupported type: " << type_;
342  }
343 }
344 
345 void CodegenAnyVal::SetVal(bool val) {
346  DCHECK_EQ(type_.type, TYPE_BOOLEAN);
347  SetVal(builder_->getInt1(val));
348 }
349 
350 void CodegenAnyVal::SetVal(int8_t val) {
351  DCHECK_EQ(type_.type, TYPE_TINYINT);
352  SetVal(builder_->getInt8(val));
353 }
354 
355 void CodegenAnyVal::SetVal(int16_t val) {
356  DCHECK_EQ(type_.type, TYPE_SMALLINT);
357  SetVal(builder_->getInt16(val));
358 }
359 
360 void CodegenAnyVal::SetVal(int32_t val) {
361  DCHECK(type_.type == TYPE_INT || type_.type == TYPE_DECIMAL);
362  SetVal(builder_->getInt32(val));
363 }
364 
365 void CodegenAnyVal::SetVal(int64_t val) {
366  DCHECK(type_.type == TYPE_BIGINT || type_.type == TYPE_DECIMAL);
367  SetVal(builder_->getInt64(val));
368 }
369 
371  DCHECK_EQ(type_.type, TYPE_DECIMAL);
372  // TODO: is there a better way to do this?
373  // Set high bits
374  Value* ir_val = ConstantInt::get(codegen_->i128_type(), HighBits(val));
375  ir_val = builder_->CreateShl(ir_val, 64, "tmp");
376  // Set low bits
377  ir_val = builder_->CreateOr(ir_val, LowBits(val), "tmp");
378  SetVal(ir_val);
379 }
380 
381 void CodegenAnyVal::SetVal(float val) {
382  DCHECK_EQ(type_.type, TYPE_FLOAT);
383  SetVal(ConstantFP::get(builder_->getFloatTy(), val));
384 }
385 
386 void CodegenAnyVal::SetVal(double val) {
387  DCHECK_EQ(type_.type, TYPE_DOUBLE);
388  SetVal(ConstantFP::get(builder_->getDoubleTy(), val));
389 }
390 
392  // Set the second pointer value to 'ptr'.
393  DCHECK(type_.IsStringType());
394  return builder_->CreateExtractValue(value_, 1, name_);
395 }
396 
398  // Get the high bytes of the first value.
399  DCHECK(type_.IsStringType());
400  Value* v = builder_->CreateExtractValue(value_, 0);
401  return GetHighBits(32, v);
402 }
403 
404 void CodegenAnyVal::SetPtr(Value* ptr) {
405  // Set the second pointer value to 'ptr'.
406  DCHECK(type_.IsStringType());
407  value_ = builder_->CreateInsertValue(value_, ptr, 1, name_);
408 }
409 
410 void CodegenAnyVal::SetLen(Value* len) {
411  // Set the high bytes of the first value to 'len'.
412  DCHECK(type_.IsStringType());
413  Value* v = builder_->CreateExtractValue(value_, 0);
414  v = SetHighBits(32, len, v);
415  value_ = builder_->CreateInsertValue(value_, v, 0, name_);
416 }
417 
419  // Get the second i64 value.
420  DCHECK_EQ(type_.type, TYPE_TIMESTAMP);
421  return builder_->CreateExtractValue(value_, 1);
422 }
423 
425  // Get the high bytes of the first value.
426  DCHECK_EQ(type_.type, TYPE_TIMESTAMP);
427  Value* v = builder_->CreateExtractValue(value_, 0);
428  return GetHighBits(32, v);
429 }
430 
431 void CodegenAnyVal::SetTimeOfDay(Value* time_of_day) {
432  // Set the second i64 value to 'time_of_day'.
433  DCHECK_EQ(type_.type, TYPE_TIMESTAMP);
434  value_ = builder_->CreateInsertValue(value_, time_of_day, 1, name_);
435 }
436 
437 void CodegenAnyVal::SetDate(Value* date) {
438  // Set the high bytes of the first value to 'date'.
439  DCHECK_EQ(type_.type, TYPE_TIMESTAMP);
440  Value* v = builder_->CreateExtractValue(value_, 0);
441  v = SetHighBits(32, date, v);
442  value_ = builder_->CreateInsertValue(value_, v, 0, name_);
443 }
444 
446  Value* value_ptr = codegen_->CreateEntryBlockAlloca(*builder_, value_->getType());
447  builder_->CreateStore(value_, value_ptr);
448  return builder_->CreateBitCast(value_ptr, GetUnloweredPtrType(codegen_, type_));
449 }
450 
451 void CodegenAnyVal::SetFromRawPtr(Value* raw_ptr) {
452  Value* val_ptr =
453  builder_->CreateBitCast(raw_ptr, codegen_->GetPtrType(type_), "val_ptr");
454  Value* val = builder_->CreateLoad(val_ptr);
455  SetFromRawValue(val);
456 }
457 
458 void CodegenAnyVal::SetFromRawValue(Value* raw_val) {
459  DCHECK_EQ(raw_val->getType(), codegen_->GetType(type_))
460  << endl << LlvmCodeGen::Print(raw_val)
461  << endl << type_ << " => " << LlvmCodeGen::Print(codegen_->GetType(type_));
462  switch (type_.type) {
463  case TYPE_STRING:
464  case TYPE_VARCHAR:
465  case TYPE_CHAR: {
466  // Convert StringValue to StringVal
467  SetPtr(builder_->CreateExtractValue(raw_val, 0, "ptr"));
468  SetLen(builder_->CreateExtractValue(raw_val, 1, "len"));
469  break;
470  }
471  case TYPE_TIMESTAMP: {
472  // Convert TimestampValue to TimestampVal
473  // TimestampValue has type
474  // { boost::posix_time::time_duration, boost::gregorian::date }
475  // = { {{{i64}}}, {{i32}} }
476 
477  // Extract time_of_day i64 from boost::posix_time::time_duration.
478  uint32_t time_of_day_idxs[] = {0, 0, 0, 0};
479  Value* time_of_day =
480  builder_->CreateExtractValue(raw_val, time_of_day_idxs, "time_of_day");
481  DCHECK(time_of_day->getType()->isIntegerTy(64));
482  SetTimeOfDay(time_of_day);
483  // Extract i32 from boost::gregorian::date
484  uint32_t date_idxs[] = {1, 0, 0};
485  Value* date = builder_->CreateExtractValue(raw_val, date_idxs, "date");
486  DCHECK(date->getType()->isIntegerTy(32));
487  SetDate(date);
488  break;
489  }
490  case TYPE_BOOLEAN:
491  case TYPE_TINYINT:
492  case TYPE_SMALLINT:
493  case TYPE_INT:
494  case TYPE_BIGINT:
495  case TYPE_FLOAT:
496  case TYPE_DOUBLE:
497  case TYPE_DECIMAL:
498  // raw_val is a native type
499  SetVal(raw_val);
500  break;
501  default:
502  DCHECK(false) << "NYI: " << type_.DebugString();
503  break;
504  }
505 }
506 
508  Type* raw_type = codegen_->GetType(type_);
509  Value* raw_val = Constant::getNullValue(raw_type);
510  switch (type_.type) {
511  case TYPE_STRING:
512  case TYPE_VARCHAR: {
513  // Convert StringVal to StringValue
514  raw_val = builder_->CreateInsertValue(raw_val, GetPtr(), 0);
515  raw_val = builder_->CreateInsertValue(raw_val, GetLen(), 1);
516  break;
517  }
518  case TYPE_TIMESTAMP: {
519  // Convert TimestampVal to TimestampValue
520  // TimestampValue has type
521  // { boost::posix_time::time_duration, boost::gregorian::date }
522  // = { {{{i64}}}, {{i32}} }
523  uint32_t time_of_day_idxs[] = {0, 0, 0, 0};
524  raw_val = builder_->CreateInsertValue(raw_val, GetTimeOfDay(), time_of_day_idxs);
525  uint32_t date_idxs[] = {1, 0, 0};
526  raw_val = builder_->CreateInsertValue(raw_val, GetDate(), date_idxs);
527  break;
528  }
529  case TYPE_BOOLEAN:
530  case TYPE_TINYINT:
531  case TYPE_SMALLINT:
532  case TYPE_INT:
533  case TYPE_BIGINT:
534  case TYPE_FLOAT:
535  case TYPE_DOUBLE:
536  case TYPE_DECIMAL:
537  // raw_val is a native type
538  raw_val = GetVal();
539  break;
540  default:
541  DCHECK(false) << "NYI: " << type_.DebugString();
542  break;
543  }
544  return raw_val;
545 }
546 
547 void CodegenAnyVal::ToNativePtr(Value* native_ptr) {
548  builder_->CreateStore(ToNativeValue(), native_ptr);
549 }
550 
552  DCHECK_EQ(type_, other->type_);
553  switch (type_.type) {
554  case TYPE_BOOLEAN:
555  case TYPE_TINYINT:
556  case TYPE_SMALLINT:
557  case TYPE_INT:
558  case TYPE_BIGINT:
559  case TYPE_DECIMAL:
560  return builder_->CreateICmpEQ(GetVal(), other->GetVal(), "eq");
561  case TYPE_FLOAT:
562  case TYPE_DOUBLE:
563  return builder_->CreateFCmpUEQ(GetVal(), other->GetVal(), "eq");
564  case TYPE_STRING:
565  case TYPE_VARCHAR: {
566  Function* eq_fn = codegen_->GetFunction(IRFunction::CODEGEN_ANYVAL_STRING_VAL_EQ);
567  return builder_->CreateCall2(
568  eq_fn, GetUnloweredPtr(), other->GetUnloweredPtr(), "eq");
569  }
570  case TYPE_TIMESTAMP: {
571  Function* eq_fn =
572  codegen_->GetFunction(IRFunction::CODEGEN_ANYVAL_TIMESTAMP_VAL_EQ);
573  return builder_->CreateCall2(
574  eq_fn, GetUnloweredPtr(), other->GetUnloweredPtr(), "eq");
575  }
576  default:
577  DCHECK(false) << "NYI: " << type_.DebugString();
578  return NULL;
579  }
580 }
581 
582 Value* CodegenAnyVal::EqToNativePtr(Value* native_ptr) {
583  Value* val = NULL;
584  if (!type_.IsStringType()) {
585  val = builder_->CreateLoad(native_ptr);
586  }
587  switch (type_.type) {
588  case TYPE_NULL:
589  return codegen_->false_value();
590  case TYPE_BOOLEAN:
591  case TYPE_TINYINT:
592  case TYPE_SMALLINT:
593  case TYPE_INT:
594  case TYPE_BIGINT:
595  case TYPE_DECIMAL:
596  return builder_->CreateICmpEQ(GetVal(), val, "cmp_raw");
597  case TYPE_FLOAT:
598  case TYPE_DOUBLE:
599  return builder_->CreateFCmpUEQ(GetVal(), val, "cmp_raw");
600  case TYPE_STRING:
601  case TYPE_VARCHAR: {
602  Function* eq_fn = codegen_->GetFunction(IRFunction::CODEGEN_ANYVAL_STRING_VALUE_EQ);
603  return builder_->CreateCall2(eq_fn, GetUnloweredPtr(), native_ptr, "cmp_raw");
604  }
605  case TYPE_TIMESTAMP: {
606  Function* eq_fn =
607  codegen_->GetFunction(IRFunction::CODEGEN_ANYVAL_TIMESTAMP_VALUE_EQ);
608  return builder_->CreateCall2(eq_fn, GetUnloweredPtr(), native_ptr, "cmp_raw");
609  }
610  default:
611  DCHECK(false) << "NYI: " << type_.DebugString();
612  return NULL;
613  }
614 }
615 
616 Value* CodegenAnyVal::GetHighBits(int num_bits, Value* v, const char* name) {
617  DCHECK_EQ(v->getType()->getIntegerBitWidth(), num_bits * 2);
618  Value* shifted = builder_->CreateAShr(v, num_bits);
619  return builder_->CreateTrunc(
620  shifted, IntegerType::get(codegen_->context(), num_bits));
621 }
622 
623 // Example output: (num_bits = 8)
624 // %1 = zext i1 %src to i16
625 // %2 = shl i16 %1, 8
626 // %3 = and i16 %dst1 255 ; clear the top half of dst
627 // %dst2 = or i16 %3, %2 ; set the top of half of dst to src
628 Value* CodegenAnyVal::SetHighBits(int num_bits, Value* src, Value* dst,
629  const char* name) {
630  DCHECK_LE(src->getType()->getIntegerBitWidth(), num_bits);
631  DCHECK_EQ(dst->getType()->getIntegerBitWidth(), num_bits * 2);
632  Value* extended_src =
633  builder_->CreateZExt(src, IntegerType::get(codegen_->context(), num_bits * 2));
634  Value* shifted_src = builder_->CreateShl(extended_src, num_bits);
635  Value* masked_dst = builder_->CreateAnd(dst, (1LL << num_bits) - 1);
636  return builder_->CreateOr(masked_dst, shifted_src, name);
637 }
638 
639 Value* CodegenAnyVal::GetNullVal(LlvmCodeGen* codegen, const ColumnType& type) {
640  Type* val_type = GetLoweredType(codegen, type);
641  return GetNullVal(codegen, val_type);
642 }
643 
644 Value* CodegenAnyVal::GetNullVal(LlvmCodeGen* codegen, Type* val_type) {
645  if (val_type->isStructTy()) {
646  StructType* struct_type = cast<StructType>(val_type);
647  if (struct_type->getNumElements() == 3) {
648  DCHECK_EQ(val_type, codegen->GetType(LLVM_DECIMALVAL_NAME));
649  // Return the struct { {1}, 0, 0 } (the 'is_null' byte, i.e. the first value's first
650  // byte, is set to 1, the other bytes don't matter)
651  StructType* anyval_struct_type = cast<StructType>(struct_type->getElementType(0));
652  Type* is_null_type = anyval_struct_type->getElementType(0);
653  Value* null_anyval =
654  ConstantStruct::get(anyval_struct_type, ConstantInt::get(is_null_type, 1));
655  Type* type2 = struct_type->getElementType(1);
656  Type* type3 = struct_type->getElementType(2);
657  return ConstantStruct::get(struct_type, null_anyval, Constant::getNullValue(type2),
658  Constant::getNullValue(type3), NULL);
659  }
660  // Return the struct { 1, 0 } (the 'is_null' byte, i.e. the first value's first byte,
661  // is set to 1, the other bytes don't matter)
662  DCHECK_EQ(struct_type->getNumElements(), 2);
663  Type* type1 = struct_type->getElementType(0);
664  DCHECK(type1->isIntegerTy()) << LlvmCodeGen::Print(type1);
665  Type* type2 = struct_type->getElementType(1);
666  return ConstantStruct::get(
667  struct_type, ConstantInt::get(type1, 1), Constant::getNullValue(type2), NULL);
668  }
669  // Return the int 1 ('is_null' byte is 1, other bytes don't matter)
670  DCHECK(val_type->isIntegerTy());
671  return ConstantInt::get(val_type, 1);
672 }
673 
675  LlvmCodeGen::LlvmBuilder* builder, const ColumnType& type, const char* name) {
676  Type* val_type = GetLoweredType(codegen, type);
677  // All zeros => 'is_null' = false
678  Value* value = Constant::getNullValue(val_type);
679  return CodegenAnyVal(codegen, builder, type, value, name);
680 }
void SetFromRawValue(llvm::Value *raw_val)
static llvm::Type * GetLoweredPtrType(LlvmCodeGen *cg, const ColumnType &type)
static CodegenAnyVal CreateCallWrapped(LlvmCodeGen *cg, LlvmCodeGen::LlvmBuilder *builder, const ColumnType &type, llvm::Function *fn, llvm::ArrayRef< llvm::Value * > args, const char *name="", llvm::Value *result_ptr=NULL)
Same as above but wraps the result in a CodegenAnyVal.
llvm::PointerType * GetPtrType(llvm::Type *type)
Return a pointer type to 'type'.
llvm::Value * Eq(CodegenAnyVal *other)
Returns the i1 result of this == other. this and other must be non-null.
void SetLen(llvm::Value *len)
static const char * LLVM_TIMESTAMPVAL_NAME
llvm::Type * bigint_type()
Definition: llvm-codegen.h:389
void SetIsNull(llvm::Value *is_null)
Sets the 'is_null' field of the *Val.
static llvm::Type * GetUnloweredType(LlvmCodeGen *cg, const ColumnType &type)
CodegenAnyVal()
Ctor for created an uninitialized CodegenAnYVal that can be assigned to later.
static const char * LLVM_STRINGVAL_NAME
static llvm::Value * CreateCall(LlvmCodeGen *cg, LlvmCodeGen::LlvmBuilder *builder, llvm::Function *fn, llvm::ArrayRef< llvm::Value * > args, const char *name="", llvm::Value *result_ptr=NULL)
'name' optionally specifies the name of the returned value.
static const char * LLVM_FLOATVAL_NAME
llvm::Value * GetHighBits(int num_bits, llvm::Value *v, const char *name="")
void SetVal(llvm::Value *val)
llvm::Value * ToNativeValue()
llvm::Type * boolean_type()
Simple wrappers to reduce code verbosity.
Definition: llvm-codegen.h:385
static llvm::Type * GetUnloweredPtrType(LlvmCodeGen *cg, const ColumnType &type)
void SetDate(llvm::Value *date)
Setters for TimestampVals.
static const char * LLVM_BIGINTVAL_NAME
static CodegenAnyVal GetNonNullVal(LlvmCodeGen *codegen, LlvmCodeGen::LlvmBuilder *builder, const ColumnType &type, const char *name="")
static const char * LLVM_BOOLEANVAL_NAME
llvm::Value * GetPtr()
Getters for StringVals.
static std::string Print(T *value_or_type)
Returns the string representation of a llvm::Value* or llvm::Type*.
Definition: llvm-codegen.h:326
LLVM code generator. This is the top level object to generate jitted code.
Definition: llvm-codegen.h:107
bool IsStringType() const
Definition: types.h:168
std::string DebugString() const
Definition: types.cc:194
llvm::Type * double_type()
Definition: llvm-codegen.h:391
PrimitiveType type
Definition: types.h:60
llvm::Value * GetUnloweredPtr()
static const char * LLVM_DOUBLEVAL_NAME
llvm::Value * SetHighBits(int num_bits, llvm::Value *src, llvm::Value *dst, const char *name="")
void SetPtr(llvm::Value *ptr)
Setters for StringVals.
void SetTimeOfDay(llvm::Value *time_of_day)
static llvm::Type * GetLoweredType(LlvmCodeGen *cg, const ColumnType &type)
int GetByteSize() const
Returns the byte size of this type. Returns 0 for variable length types.
Definition: types.h:178
llvm::Function * GetFunction(IRFunction::Type)
static const char * LLVM_TINYINTVAL_NAME
llvm::Type * tinyint_type()
Definition: llvm-codegen.h:386
llvm::Value * EqToNativePtr(llvm::Value *native_ptr)
LlvmCodeGen::LlvmBuilder * builder_
llvm::Type * float_type()
Definition: llvm-codegen.h:390
llvm::Value * GetDate()
Getters for TimestampVals.
llvm::Value * false_value()
Definition: llvm-codegen.h:381
uint64_t LowBits(int128_t x)
llvm::Value * GetVal(const char *name="val")
llvm::Type * GetType(const ColumnType &type)
Returns llvm type for the column type.
llvm::Value * GetIsNull(const char *name="is_null")
Gets the 'is_null' field of the *Val.
llvm::Value * GetLen()
static const char * LLVM_DECIMALVAL_NAME
static const char * LLVM_INTVAL_NAME
static llvm::Value * GetNullVal(LlvmCodeGen *codegen, const ColumnType &type)
llvm::Type * smallint_type()
Definition: llvm-codegen.h:387
uint64_t HighBits(int128_t x)
Get the high and low bits of an int128_t.
void SetFromRawPtr(llvm::Value *raw_ptr)
llvm::Type * int_type()
Definition: llvm-codegen.h:388
LlvmCodeGen * codegen_
string name
Definition: cpu-info.cc:50
llvm::LLVMContext & context()
Definition: llvm-codegen.h:214
llvm::AllocaInst * CreateEntryBlockAlloca(llvm::Function *f, const NamedVariable &var)
llvm::Value * GetTimeOfDay()
void ToNativePtr(llvm::Value *native_ptr)
llvm::Type * i128_type()
Definition: llvm-codegen.h:395
llvm::PointerType * ptr_type()
Definition: llvm-codegen.h:393
static const char * LLVM_SMALLINTVAL_NAME
__int128_t int128_t
We use the c++ int128_t type. This is stored using 16 bytes and very performant.