1 /****************************************************************************
2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * @file builder_misc.cpp
25 * @brief Implementation for miscellaneous builder functions
29 ******************************************************************************/
30 #include "jit_pch.hpp"
32 #include "common/rdtsc_buckets.h"
36 extern "C" void CallPrint(const char* fmt
, ...);
40 //////////////////////////////////////////////////////////////////////////
41 /// @brief Convert an IEEE 754 32-bit single precision float to an
42 /// 16 bit float with 5 exponent bits and a variable
43 /// number of mantissa bits.
44 /// @param val - 32-bit float
45 /// @todo Maybe move this outside of this file into a header?
46 static uint16_t ConvertFloat32ToFloat16(float val
)
48 uint32_t sign
, exp
, mant
;
51 // Extract the sign, exponent, and mantissa
52 uint32_t uf
= *(uint32_t*)&val
;
53 sign
= (uf
& 0x80000000) >> 31;
54 exp
= (uf
& 0x7F800000) >> 23;
55 mant
= uf
& 0x007FFFFF;
57 // Check for out of range
62 sign
= 1; // set the sign bit for NANs
64 else if (std::isinf(val
))
69 else if (exp
> (0x70 + 0x1E)) // Too big to represent -> max representable value
74 else if ((exp
<= 0x70) && (exp
>= 0x66)) // It's a denorm
77 for (; exp
<= 0x70; mant
>>= 1, exp
++)
82 else if (exp
< 0x66) // Too small to represent -> Zero
89 // Saves bits that will be shifted off for rounding
90 roundBits
= mant
& 0x1FFFu
;
91 // convert exponent and mantissa to 16 bit format
95 // Essentially RTZ, but round up if off by only 1 lsb
96 if (roundBits
== 0x1FFFu
)
100 if ((mant
& 0xC00u
) != 0)
102 // make sure only the needed bits are used
107 uint32_t tmpVal
= (sign
<< 15) | (exp
<< 10) | mant
;
108 return (uint16_t)tmpVal
;
111 //////////////////////////////////////////////////////////////////////////
112 /// @brief Convert an IEEE 754 16-bit float to an 32-bit single precision
114 /// @param val - 16-bit float
115 /// @todo Maybe move this outside of this file into a header?
116 static float ConvertFloat16ToFloat32(uint32_t val
)
119 if ((val
& 0x7fff) == 0)
121 result
= ((uint32_t)(val
& 0x8000)) << 16;
123 else if ((val
& 0x7c00) == 0x7c00)
125 result
= ((val
& 0x3ff) == 0) ? 0x7f800000 : 0x7fc00000;
126 result
|= ((uint32_t)val
& 0x8000) << 16;
130 uint32_t sign
= (val
& 0x8000) << 16;
131 uint32_t mant
= (val
& 0x3ff) << 13;
132 uint32_t exp
= (val
>> 10) & 0x1f;
133 if ((exp
== 0) && (mant
!= 0)) // Adjust exponent and mantissa for denormals
136 while (mant
< (0x400 << 13))
141 mant
&= (0x3ff << 13);
143 exp
= ((exp
- 15 + 127) & 0xff) << 23;
144 result
= sign
| exp
| mant
;
147 return *(float*)&result
;
150 Constant
*Builder::C(bool i
)
152 return ConstantInt::get(IRB()->getInt1Ty(), (i
? 1 : 0));
155 Constant
*Builder::C(char i
)
157 return ConstantInt::get(IRB()->getInt8Ty(), i
);
160 Constant
*Builder::C(uint8_t i
)
162 return ConstantInt::get(IRB()->getInt8Ty(), i
);
165 Constant
*Builder::C(int i
)
167 return ConstantInt::get(IRB()->getInt32Ty(), i
);
170 Constant
*Builder::C(int64_t i
)
172 return ConstantInt::get(IRB()->getInt64Ty(), i
);
175 Constant
*Builder::C(uint16_t i
)
177 return ConstantInt::get(mInt16Ty
,i
);
180 Constant
*Builder::C(uint32_t i
)
182 return ConstantInt::get(IRB()->getInt32Ty(), i
);
185 Constant
*Builder::C(float i
)
187 return ConstantFP::get(IRB()->getFloatTy(), i
);
190 Constant
*Builder::PRED(bool pred
)
192 return ConstantInt::get(IRB()->getInt1Ty(), (pred
? 1 : 0));
195 Value
*Builder::VIMMED1(int i
)
197 return ConstantVector::getSplat(mVWidth
, cast
<ConstantInt
>(C(i
)));
200 Value
*Builder::VIMMED1_16(int i
)
202 return ConstantVector::getSplat(mVWidth16
, cast
<ConstantInt
>(C(i
)));
205 Value
*Builder::VIMMED1(uint32_t i
)
207 return ConstantVector::getSplat(mVWidth
, cast
<ConstantInt
>(C(i
)));
210 Value
*Builder::VIMMED1_16(uint32_t i
)
212 return ConstantVector::getSplat(mVWidth16
, cast
<ConstantInt
>(C(i
)));
215 Value
*Builder::VIMMED1(float i
)
217 return ConstantVector::getSplat(mVWidth
, cast
<ConstantFP
>(C(i
)));
220 Value
*Builder::VIMMED1_16(float i
)
222 return ConstantVector::getSplat(mVWidth16
, cast
<ConstantFP
>(C(i
)));
225 Value
*Builder::VIMMED1(bool i
)
227 return ConstantVector::getSplat(mVWidth
, cast
<ConstantInt
>(C(i
)));
230 Value
*Builder::VIMMED1_16(bool i
)
232 return ConstantVector::getSplat(mVWidth16
, cast
<ConstantInt
>(C(i
)));
235 Value
*Builder::VUNDEF_IPTR()
237 return UndefValue::get(VectorType::get(mInt32PtrTy
,mVWidth
));
240 Value
*Builder::VUNDEF(Type
* t
)
242 return UndefValue::get(VectorType::get(t
, mVWidth
));
245 Value
*Builder::VUNDEF_I()
247 return UndefValue::get(VectorType::get(mInt32Ty
, mVWidth
));
250 Value
*Builder::VUNDEF_I_16()
252 return UndefValue::get(VectorType::get(mInt32Ty
, mVWidth16
));
255 Value
*Builder::VUNDEF_F()
257 return UndefValue::get(VectorType::get(mFP32Ty
, mVWidth
));
260 Value
*Builder::VUNDEF_F_16()
262 return UndefValue::get(VectorType::get(mFP32Ty
, mVWidth16
));
265 Value
*Builder::VUNDEF(Type
*ty
, uint32_t size
)
267 return UndefValue::get(VectorType::get(ty
, size
));
270 Value
*Builder::VBROADCAST(Value
*src
, const llvm::Twine
& name
)
272 // check if src is already a vector
273 if (src
->getType()->isVectorTy())
278 return VECTOR_SPLAT(mVWidth
, src
, name
);
281 Value
*Builder::VBROADCAST_16(Value
*src
)
283 // check if src is already a vector
284 if (src
->getType()->isVectorTy())
289 return VECTOR_SPLAT(mVWidth16
, src
);
292 uint32_t Builder::IMMED(Value
* v
)
294 SWR_ASSERT(isa
<ConstantInt
>(v
));
295 ConstantInt
*pValConst
= cast
<ConstantInt
>(v
);
296 return pValConst
->getZExtValue();
299 int32_t Builder::S_IMMED(Value
* v
)
301 SWR_ASSERT(isa
<ConstantInt
>(v
));
302 ConstantInt
*pValConst
= cast
<ConstantInt
>(v
);
303 return pValConst
->getSExtValue();
306 CallInst
*Builder::CALL(Value
*Callee
, const std::initializer_list
<Value
*> &argsList
, const llvm::Twine
& name
)
308 std::vector
<Value
*> args
;
309 for (auto arg
: argsList
)
311 return CALLA(Callee
, args
, name
);
314 CallInst
*Builder::CALL(Value
*Callee
, Value
* arg
)
316 std::vector
<Value
*> args
;
318 return CALLA(Callee
, args
);
321 CallInst
*Builder::CALL2(Value
*Callee
, Value
* arg1
, Value
* arg2
)
323 std::vector
<Value
*> args
;
324 args
.push_back(arg1
);
325 args
.push_back(arg2
);
326 return CALLA(Callee
, args
);
329 CallInst
*Builder::CALL3(Value
*Callee
, Value
* arg1
, Value
* arg2
, Value
* arg3
)
331 std::vector
<Value
*> args
;
332 args
.push_back(arg1
);
333 args
.push_back(arg2
);
334 args
.push_back(arg3
);
335 return CALLA(Callee
, args
);
338 Value
*Builder::VRCP(Value
*va
, const llvm::Twine
& name
)
340 return FDIV(VIMMED1(1.0f
), va
, name
); // 1 / a
343 Value
*Builder::VPLANEPS(Value
* vA
, Value
* vB
, Value
* vC
, Value
* &vX
, Value
* &vY
)
345 Value
* vOut
= FMADDPS(vA
, vX
, vC
);
346 vOut
= FMADDPS(vB
, vY
, vOut
);
350 //////////////////////////////////////////////////////////////////////////
351 /// @brief insert a JIT call to CallPrint
352 /// - outputs formatted string to both stdout and VS output window
353 /// - DEBUG builds only
355 /// PRINT("index %d = 0x%p\n",{C(lane), pIndex});
356 /// where C(lane) creates a constant value to print, and pIndex is the Value*
357 /// result from a GEP, printing out the pointer to memory
358 /// @param printStr - constant string to print, which includes format specifiers
359 /// @param printArgs - initializer list of Value*'s to print to std out
360 CallInst
*Builder::PRINT(const std::string
&printStr
,const std::initializer_list
<Value
*> &printArgs
)
362 // push the arguments to CallPrint into a vector
363 std::vector
<Value
*> printCallArgs
;
364 // save room for the format string. we still need to modify it for vectors
365 printCallArgs
.resize(1);
367 // search through the format string for special processing
369 std::string
tempStr(printStr
);
370 pos
= tempStr
.find('%', pos
);
371 auto v
= printArgs
.begin();
373 while ((pos
!= std::string::npos
) && (v
!= printArgs
.end()))
376 Type
* pType
= pArg
->getType();
378 if (pType
->isVectorTy())
380 Type
* pContainedType
= pType
->getContainedType(0);
382 if (toupper(tempStr
[pos
+ 1]) == 'X')
385 tempStr
[pos
+ 1] = 'x';
386 tempStr
.insert(pos
+ 2, "%08X ");
389 printCallArgs
.push_back(VEXTRACT(pArg
, C(0)));
391 std::string vectorFormatStr
;
392 for (uint32_t i
= 1; i
< pType
->getVectorNumElements(); ++i
)
394 vectorFormatStr
+= "0x%08X ";
395 printCallArgs
.push_back(VEXTRACT(pArg
, C(i
)));
398 tempStr
.insert(pos
, vectorFormatStr
);
399 pos
+= vectorFormatStr
.size();
401 else if ((tempStr
[pos
+ 1] == 'f') && (pContainedType
->isFloatTy()))
404 for (; i
< (pArg
->getType()->getVectorNumElements()) - 1; i
++)
406 tempStr
.insert(pos
, std::string("%f "));
408 printCallArgs
.push_back(FP_EXT(VEXTRACT(pArg
, C(i
)), Type::getDoubleTy(JM()->mContext
)));
410 printCallArgs
.push_back(FP_EXT(VEXTRACT(pArg
, C(i
)), Type::getDoubleTy(JM()->mContext
)));
412 else if ((tempStr
[pos
+ 1] == 'd') && (pContainedType
->isIntegerTy()))
415 for (; i
< (pArg
->getType()->getVectorNumElements()) - 1; i
++)
417 tempStr
.insert(pos
, std::string("%d "));
419 printCallArgs
.push_back(VEXTRACT(pArg
, C(i
)));
421 printCallArgs
.push_back(VEXTRACT(pArg
, C(i
)));
426 if (toupper(tempStr
[pos
+ 1]) == 'X')
429 tempStr
.insert(pos
+ 1, "x%08");
430 printCallArgs
.push_back(pArg
);
433 // for %f we need to cast float Values to doubles so that they print out correctly
434 else if ((tempStr
[pos
+ 1] == 'f') && (pType
->isFloatTy()))
436 printCallArgs
.push_back(FP_EXT(pArg
, Type::getDoubleTy(JM()->mContext
)));
441 printCallArgs
.push_back(pArg
);
445 // advance to the next arguement
447 pos
= tempStr
.find('%', ++pos
);
450 // create global variable constant string
451 Constant
*constString
= ConstantDataArray::getString(JM()->mContext
,tempStr
,true);
452 GlobalVariable
*gvPtr
= new GlobalVariable(constString
->getType(),true,GlobalValue::InternalLinkage
,constString
,"printStr");
453 JM()->mpCurrentModule
->getGlobalList().push_back(gvPtr
);
455 // get a pointer to the first character in the constant string array
456 std::vector
<Constant
*> geplist
{C(0),C(0)};
457 Constant
*strGEP
= ConstantExpr::getGetElementPtr(nullptr, gvPtr
,geplist
,false);
459 // insert the pointer to the format string in the argument vector
460 printCallArgs
[0] = strGEP
;
462 // get pointer to CallPrint function and insert decl into the module if needed
463 std::vector
<Type
*> args
;
464 args
.push_back(PointerType::get(mInt8Ty
,0));
465 FunctionType
* callPrintTy
= FunctionType::get(Type::getVoidTy(JM()->mContext
),args
,true);
466 Function
*callPrintFn
= cast
<Function
>(JM()->mpCurrentModule
->getOrInsertFunction("CallPrint", callPrintTy
));
468 // if we haven't yet added the symbol to the symbol table
469 if((sys::DynamicLibrary::SearchForAddressOfSymbol("CallPrint")) == nullptr)
471 sys::DynamicLibrary::AddSymbol("CallPrint", (void *)&CallPrint
);
474 // insert a call to CallPrint
475 return CALLA(callPrintFn
,printCallArgs
);
478 //////////////////////////////////////////////////////////////////////////
479 /// @brief Wrapper around PRINT with initializer list.
480 CallInst
* Builder::PRINT(const std::string
&printStr
)
482 return PRINT(printStr
, {});
485 Value
*Builder::EXTRACT_16(Value
*x
, uint32_t imm
)
489 return VSHUFFLE(x
, UndefValue::get(x
->getType()), { 0, 1, 2, 3, 4, 5, 6, 7 });
493 return VSHUFFLE(x
, UndefValue::get(x
->getType()), { 8, 9, 10, 11, 12, 13, 14, 15 });
497 Value
*Builder::JOIN_16(Value
*a
, Value
*b
)
499 return VSHUFFLE(a
, b
, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
502 //////////////////////////////////////////////////////////////////////////
503 /// @brief convert x86 <N x float> mask to llvm <N x i1> mask
504 Value
*Builder::MASK(Value
*vmask
)
506 Value
*src
= BITCAST(vmask
, mSimdInt32Ty
);
507 return ICMP_SLT(src
, VIMMED1(0));
510 Value
*Builder::MASK_16(Value
*vmask
)
512 Value
*src
= BITCAST(vmask
, mSimd16Int32Ty
);
513 return ICMP_SLT(src
, VIMMED1_16(0));
516 //////////////////////////////////////////////////////////////////////////
517 /// @brief convert llvm <N x i1> mask to x86 <N x i32> mask
518 Value
*Builder::VMASK(Value
*mask
)
520 return S_EXT(mask
, mSimdInt32Ty
);
523 Value
*Builder::VMASK_16(Value
*mask
)
525 return S_EXT(mask
, mSimd16Int32Ty
);
528 //////////////////////////////////////////////////////////////////////////
529 /// @brief Generate a VPSHUFB operation in LLVM IR. If not
530 /// supported on the underlying platform, emulate it
531 /// @param a - 256bit SIMD(32x8bit) of 8bit integer values
532 /// @param b - 256bit SIMD(32x8bit) of 8bit integer mask values
533 /// Byte masks in lower 128 lane of b selects 8 bit values from lower
534 /// 128bits of a, and vice versa for the upper lanes. If the mask
535 /// value is negative, '0' is inserted.
536 Value
*Builder::PSHUFB(Value
* a
, Value
* b
)
539 // use avx2 pshufb instruction if available
540 if(JM()->mArch
.AVX2())
546 Constant
* cB
= dyn_cast
<Constant
>(b
);
547 // number of 8 bit elements in b
548 uint32_t numElms
= cast
<VectorType
>(cB
->getType())->getNumElements();
550 Value
* vShuf
= UndefValue::get(VectorType::get(mInt8Ty
, numElms
));
552 // insert an 8 bit value from the high and low lanes of a per loop iteration
554 for(uint32_t i
= 0; i
< numElms
; i
++)
556 ConstantInt
* cLow128b
= cast
<ConstantInt
>(cB
->getAggregateElement(i
));
557 ConstantInt
* cHigh128b
= cast
<ConstantInt
>(cB
->getAggregateElement(i
+ numElms
));
559 // extract values from constant mask
560 char valLow128bLane
= (char)(cLow128b
->getSExtValue());
561 char valHigh128bLane
= (char)(cHigh128b
->getSExtValue());
563 Value
* insertValLow128b
;
564 Value
* insertValHigh128b
;
566 // if the mask value is negative, insert a '0' in the respective output position
567 // otherwise, lookup the value at mask position (bits 3..0 of the respective mask byte) in a and insert in output vector
568 insertValLow128b
= (valLow128bLane
< 0) ? C((char)0) : VEXTRACT(a
, C((valLow128bLane
& 0xF)));
569 insertValHigh128b
= (valHigh128bLane
< 0) ? C((char)0) : VEXTRACT(a
, C((valHigh128bLane
& 0xF) + numElms
));
571 vShuf
= VINSERT(vShuf
, insertValLow128b
, i
);
572 vShuf
= VINSERT(vShuf
, insertValHigh128b
, (i
+ numElms
));
579 //////////////////////////////////////////////////////////////////////////
580 /// @brief Generate a VPSHUFB operation (sign extend 8 8bit values to 32
581 /// bits)in LLVM IR. If not supported on the underlying platform, emulate it
582 /// @param a - 128bit SIMD lane(16x8bit) of 8bit integer values. Only
583 /// lower 8 values are used.
584 Value
*Builder::PMOVSXBD(Value
* a
)
586 // VPMOVSXBD output type
587 Type
* v8x32Ty
= VectorType::get(mInt32Ty
, 8);
588 // Extract 8 values from 128bit lane and sign extend
589 return S_EXT(VSHUFFLE(a
, a
, C
<int>({0, 1, 2, 3, 4, 5, 6, 7})), v8x32Ty
);
592 //////////////////////////////////////////////////////////////////////////
593 /// @brief Generate a VPSHUFB operation (sign extend 8 16bit values to 32
594 /// bits)in LLVM IR. If not supported on the underlying platform, emulate it
595 /// @param a - 128bit SIMD lane(8x16bit) of 16bit integer values.
596 Value
*Builder::PMOVSXWD(Value
* a
)
598 // VPMOVSXWD output type
599 Type
* v8x32Ty
= VectorType::get(mInt32Ty
, 8);
600 // Extract 8 values from 128bit lane and sign extend
601 return S_EXT(VSHUFFLE(a
, a
, C
<int>({0, 1, 2, 3, 4, 5, 6, 7})), v8x32Ty
);
604 //////////////////////////////////////////////////////////////////////////
605 /// @brief Generate a VCVTPH2PS operation (float16->float32 conversion)
606 /// in LLVM IR. If not supported on the underlying platform, emulate it
607 /// @param a - 128bit SIMD lane(8x16bit) of float16 in int16 format.
608 Value
*Builder::CVTPH2PS(Value
* a
, const llvm::Twine
& name
)
610 if (JM()->mArch
.F16C())
612 return VCVTPH2PS(a
, name
);
616 FunctionType
* pFuncTy
= FunctionType::get(mFP32Ty
, mInt16Ty
);
617 Function
* pCvtPh2Ps
= cast
<Function
>(JM()->mpCurrentModule
->getOrInsertFunction("ConvertFloat16ToFloat32", pFuncTy
));
619 if (sys::DynamicLibrary::SearchForAddressOfSymbol("ConvertFloat16ToFloat32") == nullptr)
621 sys::DynamicLibrary::AddSymbol("ConvertFloat16ToFloat32", (void *)&ConvertFloat16ToFloat32
);
624 Value
* pResult
= UndefValue::get(mSimdFP32Ty
);
625 for (uint32_t i
= 0; i
< mVWidth
; ++i
)
627 Value
* pSrc
= VEXTRACT(a
, C(i
));
628 Value
* pConv
= CALL(pCvtPh2Ps
, std::initializer_list
<Value
*>{pSrc
});
629 pResult
= VINSERT(pResult
, pConv
, C(i
));
632 pResult
->setName(name
);
637 //////////////////////////////////////////////////////////////////////////
638 /// @brief Generate a VCVTPS2PH operation (float32->float16 conversion)
639 /// in LLVM IR. If not supported on the underlying platform, emulate it
640 /// @param a - 128bit SIMD lane(8x16bit) of float16 in int16 format.
641 Value
*Builder::CVTPS2PH(Value
* a
, Value
* rounding
)
643 if (JM()->mArch
.F16C())
645 return VCVTPS2PH(a
, rounding
);
649 // call scalar C function for now
650 FunctionType
* pFuncTy
= FunctionType::get(mInt16Ty
, mFP32Ty
);
651 Function
* pCvtPs2Ph
= cast
<Function
>(JM()->mpCurrentModule
->getOrInsertFunction("ConvertFloat32ToFloat16", pFuncTy
));
653 if (sys::DynamicLibrary::SearchForAddressOfSymbol("ConvertFloat32ToFloat16") == nullptr)
655 sys::DynamicLibrary::AddSymbol("ConvertFloat32ToFloat16", (void *)&ConvertFloat32ToFloat16
);
658 Value
* pResult
= UndefValue::get(mSimdInt16Ty
);
659 for (uint32_t i
= 0; i
< mVWidth
; ++i
)
661 Value
* pSrc
= VEXTRACT(a
, C(i
));
662 Value
* pConv
= CALL(pCvtPs2Ph
, std::initializer_list
<Value
*>{pSrc
});
663 pResult
= VINSERT(pResult
, pConv
, C(i
));
670 Value
*Builder::PMAXSD(Value
* a
, Value
* b
)
672 Value
* cmp
= ICMP_SGT(a
, b
);
673 return SELECT(cmp
, a
, b
);
676 Value
*Builder::PMINSD(Value
* a
, Value
* b
)
678 Value
* cmp
= ICMP_SLT(a
, b
);
679 return SELECT(cmp
, a
, b
);
682 Value
*Builder::PMAXUD(Value
* a
, Value
* b
)
684 Value
* cmp
= ICMP_UGT(a
, b
);
685 return SELECT(cmp
, a
, b
);
688 Value
*Builder::PMINUD(Value
* a
, Value
* b
)
690 Value
* cmp
= ICMP_ULT(a
, b
);
691 return SELECT(cmp
, a
, b
);
694 // Helper function to create alloca in entry block of function
695 Value
* Builder::CreateEntryAlloca(Function
* pFunc
, Type
* pType
)
697 auto saveIP
= IRB()->saveIP();
698 IRB()->SetInsertPoint(&pFunc
->getEntryBlock(),
699 pFunc
->getEntryBlock().begin());
700 Value
* pAlloca
= ALLOCA(pType
);
701 if (saveIP
.isSet()) IRB()->restoreIP(saveIP
);
705 Value
* Builder::CreateEntryAlloca(Function
* pFunc
, Type
* pType
, Value
* pArraySize
)
707 auto saveIP
= IRB()->saveIP();
708 IRB()->SetInsertPoint(&pFunc
->getEntryBlock(),
709 pFunc
->getEntryBlock().begin());
710 Value
* pAlloca
= ALLOCA(pType
, pArraySize
);
711 if (saveIP
.isSet()) IRB()->restoreIP(saveIP
);
715 Value
* Builder::VABSPS(Value
* a
)
717 Value
* asInt
= BITCAST(a
, mSimdInt32Ty
);
718 Value
* result
= BITCAST(AND(asInt
, VIMMED1(0x7fffffff)), mSimdFP32Ty
);
722 Value
*Builder::ICLAMP(Value
* src
, Value
* low
, Value
* high
, const llvm::Twine
& name
)
724 Value
*lowCmp
= ICMP_SLT(src
, low
);
725 Value
*ret
= SELECT(lowCmp
, low
, src
);
727 Value
*highCmp
= ICMP_SGT(ret
, high
);
728 ret
= SELECT(highCmp
, high
, ret
, name
);
733 Value
*Builder::FCLAMP(Value
* src
, Value
* low
, Value
* high
)
735 Value
*lowCmp
= FCMP_OLT(src
, low
);
736 Value
*ret
= SELECT(lowCmp
, low
, src
);
738 Value
*highCmp
= FCMP_OGT(ret
, high
);
739 ret
= SELECT(highCmp
, high
, ret
);
744 Value
*Builder::FCLAMP(Value
* src
, float low
, float high
)
746 Value
* result
= VMAXPS(src
, VIMMED1(low
));
747 result
= VMINPS(result
, VIMMED1(high
));
752 Value
*Builder::FMADDPS(Value
* a
, Value
* b
, Value
* c
)
755 // use FMADs if available
756 if(JM()->mArch
.AVX2())
758 vOut
= VFMADDPS(a
, b
, c
);
762 vOut
= FADD(FMUL(a
, b
), c
);
767 //////////////////////////////////////////////////////////////////////////
768 /// @brief pop count on vector mask (e.g. <8 x i1>)
769 Value
* Builder::VPOPCNT(Value
* a
)
771 Value
* b
= BITCAST(VMASK(a
), mSimdFP32Ty
);
772 return POPCNT(VMOVMSKPS(b
));
775 //////////////////////////////////////////////////////////////////////////
776 /// @brief C functions called by LLVM IR
777 //////////////////////////////////////////////////////////////////////////
779 Value
*Builder::VEXTRACTI128(Value
* a
, Constant
* imm8
)
781 bool flag
= !imm8
->isZeroValue();
782 SmallVector
<Constant
*,8> idx
;
783 for (unsigned i
= 0; i
< mVWidth
/ 2; i
++) {
784 idx
.push_back(C(flag
? i
+ mVWidth
/ 2 : i
));
786 return VSHUFFLE(a
, VUNDEF_I(), ConstantVector::get(idx
));
789 Value
*Builder::VINSERTI128(Value
* a
, Value
* b
, Constant
* imm8
)
791 bool flag
= !imm8
->isZeroValue();
792 SmallVector
<Constant
*,8> idx
;
793 for (unsigned i
= 0; i
< mVWidth
; i
++) {
796 Value
*inter
= VSHUFFLE(b
, VUNDEF_I(), ConstantVector::get(idx
));
798 SmallVector
<Constant
*,8> idx2
;
799 for (unsigned i
= 0; i
< mVWidth
/ 2; i
++) {
800 idx2
.push_back(C(flag
? i
: i
+ mVWidth
));
802 for (unsigned i
= mVWidth
/ 2; i
< mVWidth
; i
++) {
803 idx2
.push_back(C(flag
? i
+ mVWidth
/ 2 : i
));
805 return VSHUFFLE(a
, inter
, ConstantVector::get(idx2
));
808 // rdtsc buckets macros
809 void Builder::RDTSC_START(Value
* pBucketMgr
, Value
* pId
)
811 // @todo due to an issue with thread local storage propagation in llvm, we can only safely call into
812 // buckets framework when single threaded
813 if (KNOB_SINGLE_THREADED
)
815 std::vector
<Type
*> args
{
816 PointerType::get(mInt32Ty
, 0), // pBucketMgr
820 FunctionType
* pFuncTy
= FunctionType::get(Type::getVoidTy(JM()->mContext
), args
, false);
821 Function
* pFunc
= cast
<Function
>(JM()->mpCurrentModule
->getOrInsertFunction("BucketManager_StartBucket", pFuncTy
));
822 if (sys::DynamicLibrary::SearchForAddressOfSymbol("BucketManager_StartBucket") == nullptr)
824 sys::DynamicLibrary::AddSymbol("BucketManager_StartBucket", (void*)&BucketManager_StartBucket
);
827 CALL(pFunc
, { pBucketMgr
, pId
});
831 void Builder::RDTSC_STOP(Value
* pBucketMgr
, Value
* pId
)
833 // @todo due to an issue with thread local storage propagation in llvm, we can only safely call into
834 // buckets framework when single threaded
835 if (KNOB_SINGLE_THREADED
)
837 std::vector
<Type
*> args
{
838 PointerType::get(mInt32Ty
, 0), // pBucketMgr
842 FunctionType
* pFuncTy
= FunctionType::get(Type::getVoidTy(JM()->mContext
), args
, false);
843 Function
* pFunc
= cast
<Function
>(JM()->mpCurrentModule
->getOrInsertFunction("BucketManager_StopBucket", pFuncTy
));
844 if (sys::DynamicLibrary::SearchForAddressOfSymbol("BucketManager_StopBucket") == nullptr)
846 sys::DynamicLibrary::AddSymbol("BucketManager_StopBucket", (void*)&BucketManager_StopBucket
);
849 CALL(pFunc
, { pBucketMgr
, pId
});
853 uint32_t Builder::GetTypeSize(Type
* pType
)
855 if (pType
->isStructTy())
857 uint32_t numElems
= pType
->getStructNumElements();
858 Type
* pElemTy
= pType
->getStructElementType(0);
859 return numElems
* GetTypeSize(pElemTy
);
862 if (pType
->isArrayTy())
864 uint32_t numElems
= pType
->getArrayNumElements();
865 Type
* pElemTy
= pType
->getArrayElementType();
866 return numElems
* GetTypeSize(pElemTy
);
869 if (pType
->isIntegerTy())
871 uint32_t bitSize
= pType
->getIntegerBitWidth();
875 if (pType
->isFloatTy())
880 if (pType
->isHalfTy())
885 if (pType
->isDoubleTy())
890 SWR_ASSERT(false, "Unimplemented type.");