| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#ifndef LLVM_IR_FIXEDPOINTBUILDER_H |
| 15 |
#define LLVM_IR_FIXEDPOINTBUILDER_H |
| 16 |
|
| 17 |
#include "llvm/ADT/APFixedPoint.h" |
| 18 |
#include "llvm/IR/Constant.h" |
| 19 |
#include "llvm/IR/Constants.h" |
| 20 |
#include "llvm/IR/IRBuilder.h" |
| 21 |
#include "llvm/IR/InstrTypes.h" |
| 22 |
#include "llvm/IR/Instruction.h" |
| 23 |
#include "llvm/IR/IntrinsicInst.h" |
| 24 |
#include "llvm/IR/Intrinsics.h" |
| 25 |
#include "llvm/IR/Type.h" |
| 26 |
#include "llvm/IR/Value.h" |
| 27 |
|
| 28 |
namespace llvm { |
| 29 |
|
| 30 |
template <class IRBuilderTy> class FixedPointBuilder { |
| 31 |
IRBuilderTy &B; |
| 32 |
|
| 33 |
Value *Convert(Value *Src, const FixedPointSemantics &SrcSema, |
| 34 |
const FixedPointSemantics &DstSema, bool DstIsInteger) { |
| 35 |
unsigned SrcWidth = SrcSema.getWidth(); |
| 36 |
unsigned DstWidth = DstSema.getWidth(); |
| 37 |
unsigned SrcScale = SrcSema.getScale(); |
| 38 |
unsigned DstScale = DstSema.getScale(); |
| 39 |
bool SrcIsSigned = SrcSema.isSigned(); |
| 40 |
bool DstIsSigned = DstSema.isSigned(); |
| 41 |
|
| 42 |
Type *DstIntTy = B.getIntNTy(DstWidth); |
| 43 |
|
| 44 |
Value *Result = Src; |
| 45 |
unsigned ResultWidth = SrcWidth; |
| 46 |
|
| 47 |
|
| 48 |
if (DstScale < SrcScale) { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
if (DstIsInteger && SrcIsSigned) { |
| 53 |
Value *Zero = Constant::getNullValue(Result->getType()); |
| 54 |
Value *IsNegative = B.CreateICmpSLT(Result, Zero); |
| 55 |
Value *LowBits = ConstantInt::get( |
| 56 |
B.getContext(), APInt::getLowBitsSet(ResultWidth, SrcScale)); |
| 57 |
Value *Rounded = B.CreateAdd(Result, LowBits); |
| 58 |
Result = B.CreateSelect(IsNegative, Rounded, Result); |
| 59 |
} |
| 60 |
|
| 61 |
Result = SrcIsSigned |
| 62 |
? B.CreateAShr(Result, SrcScale - DstScale, "downscale") |
| 63 |
: B.CreateLShr(Result, SrcScale - DstScale, "downscale"); |
| 64 |
} |
| 65 |
|
| 66 |
if (!DstSema.isSaturated()) { |
| 67 |
|
| 68 |
Result = B.CreateIntCast(Result, DstIntTy, SrcIsSigned, "resize"); |
| 69 |
|
| 70 |
|
| 71 |
if (DstScale > SrcScale) |
| 72 |
Result = B.CreateShl(Result, DstScale - SrcScale, "upscale"); |
| 73 |
} else { |
| 74 |
|
| 75 |
if (DstScale > SrcScale) { |
| 76 |
|
| 77 |
ResultWidth = std::max(SrcWidth + DstScale - SrcScale, DstWidth); |
| 78 |
Type *UpscaledTy = B.getIntNTy(ResultWidth); |
| 79 |
Result = B.CreateIntCast(Result, UpscaledTy, SrcIsSigned, "resize"); |
| 80 |
Result = B.CreateShl(Result, DstScale - SrcScale, "upscale"); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
bool LessIntBits = DstSema.getIntegralBits() < SrcSema.getIntegralBits(); |
| 85 |
if (LessIntBits) { |
| 86 |
Value *Max = ConstantInt::get( |
| 87 |
B.getContext(), |
| 88 |
APFixedPoint::getMax(DstSema).getValue().extOrTrunc(ResultWidth)); |
| 89 |
Value *TooHigh = SrcIsSigned ? B.CreateICmpSGT(Result, Max) |
| 90 |
: B.CreateICmpUGT(Result, Max); |
| 91 |
Result = B.CreateSelect(TooHigh, Max, Result, "satmax"); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
if (SrcIsSigned && (LessIntBits || !DstIsSigned)) { |
| 96 |
Value *Min = ConstantInt::get( |
| 97 |
B.getContext(), |
| 98 |
APFixedPoint::getMin(DstSema).getValue().extOrTrunc(ResultWidth)); |
| 99 |
Value *TooLow = B.CreateICmpSLT(Result, Min); |
| 100 |
Result = B.CreateSelect(TooLow, Min, Result, "satmin"); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
if (ResultWidth != DstWidth) |
| 105 |
Result = B.CreateIntCast(Result, DstIntTy, SrcIsSigned, "resize"); |
| 106 |
} |
| 107 |
return Result; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
FixedPointSemantics |
| 113 |
getCommonBinopSemantic(const FixedPointSemantics &LHSSema, |
| 114 |
const FixedPointSemantics &RHSSema) { |
| 115 |
auto C = LHSSema.getCommonSemantics(RHSSema); |
| 116 |
bool BothPadded = |
| 117 |
LHSSema.hasUnsignedPadding() && RHSSema.hasUnsignedPadding(); |
| 118 |
return FixedPointSemantics( |
| 119 |
C.getWidth() + (unsigned)(BothPadded && C.isSaturated()), C.getScale(), |
| 120 |
C.isSigned(), C.isSaturated(), BothPadded); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
Type *getAccommodatingFloatType(Type *Ty, const FixedPointSemantics &Sema) { |
| 127 |
const fltSemantics *FloatSema = &Ty->getFltSemantics(); |
| 128 |
while (!Sema.fitsInFloatSemantics(*FloatSema)) |
| 129 |
FloatSema = APFixedPoint::promoteFloatSemantics(FloatSema); |
| 130 |
return Type::getFloatingPointTy(Ty->getContext(), *FloatSema); |
| 131 |
} |
| 132 |
|
| 133 |
public: |
| 134 |
FixedPointBuilder(IRBuilderTy &Builder) : B(Builder) {} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
Value *CreateFixedToFixed(Value *Src, const FixedPointSemantics &SrcSema, |
| 142 |
const FixedPointSemantics &DstSema) { |
| 143 |
return Convert(Src, SrcSema, DstSema, false); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
Value *CreateFixedToInteger(Value *Src, const FixedPointSemantics &SrcSema, |
| 153 |
unsigned DstWidth, bool DstIsSigned) { |
| 154 |
return Convert( |
| 155 |
Src, SrcSema, |
| 156 |
FixedPointSemantics::GetIntegerSemantics(DstWidth, DstIsSigned), true); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
Value *CreateIntegerToFixed(Value *Src, unsigned SrcIsSigned, |
| 165 |
const FixedPointSemantics &DstSema) { |
| 166 |
return Convert(Src, |
| 167 |
FixedPointSemantics::GetIntegerSemantics( |
| 168 |
Src->getType()->getScalarSizeInBits(), SrcIsSigned), |
| 169 |
DstSema, false); |
| 170 |
} |
| 171 |
|
| 172 |
Value *CreateFixedToFloating(Value *Src, const FixedPointSemantics &SrcSema, |
| 173 |
Type *DstTy) { |
| 174 |
Value *Result; |
| 175 |
Type *OpTy = getAccommodatingFloatType(DstTy, SrcSema); |
| 176 |
|
| 177 |
|
| 178 |
Result = SrcSema.isSigned() ? B.CreateSIToFP(Src, OpTy) |
| 179 |
: B.CreateUIToFP(Src, OpTy); |
| 180 |
|
| 181 |
|
| 182 |
Result = B.CreateFMul(Result, |
| 183 |
ConstantFP::get(OpTy, std::pow(2, -(int)SrcSema.getScale()))); |
| 184 |
if (OpTy != DstTy) |
| 185 |
Result = B.CreateFPTrunc(Result, DstTy); |
| 186 |
return Result; |
| 187 |
} |
| 188 |
|
| 189 |
Value *CreateFloatingToFixed(Value *Src, const FixedPointSemantics &DstSema) { |
| 190 |
bool UseSigned = DstSema.isSigned() || DstSema.hasUnsignedPadding(); |
| 191 |
Value *Result = Src; |
| 192 |
Type *OpTy = getAccommodatingFloatType(Src->getType(), DstSema); |
| 193 |
if (OpTy != Src->getType()) |
| 194 |
Result = B.CreateFPExt(Result, OpTy); |
| 195 |
|
| 196 |
|
| 197 |
Result = B.CreateFMul(Result, |
| 198 |
ConstantFP::get(OpTy, std::pow(2, DstSema.getScale()))); |
| 199 |
|
| 200 |
Type *ResultTy = B.getIntNTy(DstSema.getWidth()); |
| 201 |
if (DstSema.isSaturated()) { |
| 202 |
Intrinsic::ID IID = |
| 203 |
UseSigned ? Intrinsic::fptosi_sat : Intrinsic::fptoui_sat; |
| 204 |
Result = B.CreateIntrinsic(IID, {ResultTy, OpTy}, {Result}); |
| 205 |
} else { |
| 206 |
Result = UseSigned ? B.CreateFPToSI(Result, ResultTy) |
| 207 |
: B.CreateFPToUI(Result, ResultTy); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
if (DstSema.isSaturated() && DstSema.hasUnsignedPadding()) { |
| 213 |
Constant *Zero = Constant::getNullValue(Result->getType()); |
| 214 |
Result = |
| 215 |
B.CreateSelect(B.CreateICmpSLT(Result, Zero), Zero, Result, "satmin"); |
| 216 |
} |
| 217 |
|
| 218 |
return Result; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
Value *CreateAdd(Value *LHS, const FixedPointSemantics &LHSSema, |
| 227 |
Value *RHS, const FixedPointSemantics &RHSSema) { |
| 228 |
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema); |
| 229 |
bool UseSigned = CommonSema.isSigned() || CommonSema.hasUnsignedPadding(); |
| 230 |
|
| 231 |
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema); |
| 232 |
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema); |
| 233 |
|
| 234 |
Value *Result; |
| 235 |
if (CommonSema.isSaturated()) { |
| 236 |
Intrinsic::ID IID = UseSigned ? Intrinsic::sadd_sat : Intrinsic::uadd_sat; |
| 237 |
Result = B.CreateBinaryIntrinsic(IID, WideLHS, WideRHS); |
| 238 |
} else { |
| 239 |
Result = B.CreateAdd(WideLHS, WideRHS); |
| 240 |
} |
| 241 |
|
| 242 |
return CreateFixedToFixed(Result, CommonSema, |
| 243 |
LHSSema.getCommonSemantics(RHSSema)); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
Value *CreateSub(Value *LHS, const FixedPointSemantics &LHSSema, |
| 253 |
Value *RHS, const FixedPointSemantics &RHSSema) { |
| 254 |
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema); |
| 255 |
bool UseSigned = CommonSema.isSigned() || CommonSema.hasUnsignedPadding(); |
| 256 |
|
| 257 |
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema); |
| 258 |
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema); |
| 259 |
|
| 260 |
Value *Result; |
| 261 |
if (CommonSema.isSaturated()) { |
| 262 |
Intrinsic::ID IID = UseSigned ? Intrinsic::ssub_sat : Intrinsic::usub_sat; |
| 263 |
Result = B.CreateBinaryIntrinsic(IID, WideLHS, WideRHS); |
| 264 |
} else { |
| 265 |
Result = B.CreateSub(WideLHS, WideRHS); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
if (CommonSema.isSaturated() && CommonSema.hasUnsignedPadding()) { |
| 271 |
Constant *Zero = Constant::getNullValue(Result->getType()); |
| 272 |
Result = |
| 273 |
B.CreateSelect(B.CreateICmpSLT(Result, Zero), Zero, Result, "satmin"); |
| 274 |
} |
| 275 |
|
| 276 |
return CreateFixedToFixed(Result, CommonSema, |
| 277 |
LHSSema.getCommonSemantics(RHSSema)); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
Value *CreateMul(Value *LHS, const FixedPointSemantics &LHSSema, |
| 287 |
Value *RHS, const FixedPointSemantics &RHSSema) { |
| 288 |
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema); |
| 289 |
bool UseSigned = CommonSema.isSigned() || CommonSema.hasUnsignedPadding(); |
| 290 |
|
| 291 |
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema); |
| 292 |
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema); |
| 293 |
|
| 294 |
Intrinsic::ID IID; |
| 295 |
if (CommonSema.isSaturated()) { |
| 296 |
IID = UseSigned ? Intrinsic::smul_fix_sat : Intrinsic::umul_fix_sat; |
| 297 |
} else { |
| 298 |
IID = UseSigned ? Intrinsic::smul_fix : Intrinsic::umul_fix; |
| 299 |
} |
| 300 |
Value *Result = B.CreateIntrinsic( |
| 301 |
IID, {WideLHS->getType()}, |
| 302 |
{WideLHS, WideRHS, B.getInt32(CommonSema.getScale())}); |
| 303 |
|
| 304 |
return CreateFixedToFixed(Result, CommonSema, |
| 305 |
LHSSema.getCommonSemantics(RHSSema)); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
Value *CreateDiv(Value *LHS, const FixedPointSemantics &LHSSema, |
| 315 |
Value *RHS, const FixedPointSemantics &RHSSema) { |
| 316 |
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema); |
| 317 |
bool UseSigned = CommonSema.isSigned() || CommonSema.hasUnsignedPadding(); |
| 318 |
|
| 319 |
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema); |
| 320 |
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema); |
| 321 |
|
| 322 |
Intrinsic::ID IID; |
| 323 |
if (CommonSema.isSaturated()) { |
| 324 |
IID = UseSigned ? Intrinsic::sdiv_fix_sat : Intrinsic::udiv_fix_sat; |
| 325 |
} else { |
| 326 |
IID = UseSigned ? Intrinsic::sdiv_fix : Intrinsic::udiv_fix; |
| 327 |
} |
| 328 |
Value *Result = B.CreateIntrinsic( |
| 329 |
IID, {WideLHS->getType()}, |
| 330 |
{WideLHS, WideRHS, B.getInt32(CommonSema.getScale())}); |
| 331 |
|
| 332 |
return CreateFixedToFixed(Result, CommonSema, |
| 333 |
LHSSema.getCommonSemantics(RHSSema)); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
Value *CreateShl(Value *LHS, const FixedPointSemantics &LHSSema, Value *RHS) { |
| 342 |
bool UseSigned = LHSSema.isSigned() || LHSSema.hasUnsignedPadding(); |
| 343 |
|
| 344 |
RHS = B.CreateIntCast(RHS, LHS->getType(), false); |
| 345 |
|
| 346 |
Value *Result; |
| 347 |
if (LHSSema.isSaturated()) { |
| 348 |
Intrinsic::ID IID = UseSigned ? Intrinsic::sshl_sat : Intrinsic::ushl_sat; |
| 349 |
Result = B.CreateBinaryIntrinsic(IID, LHS, RHS); |
| 350 |
} else { |
| 351 |
Result = B.CreateShl(LHS, RHS); |
| 352 |
} |
| 353 |
|
| 354 |
return Result; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
Value *CreateShr(Value *LHS, const FixedPointSemantics &LHSSema, Value *RHS) { |
| 363 |
RHS = B.CreateIntCast(RHS, LHS->getType(), false); |
| 364 |
|
| 365 |
return LHSSema.isSigned() ? B.CreateAShr(LHS, RHS) : B.CreateLShr(LHS, RHS); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
Value *CreateEQ(Value *LHS, const FixedPointSemantics &LHSSema, |
| 374 |
Value *RHS, const FixedPointSemantics &RHSSema) { |
| 375 |
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema); |
| 376 |
|
| 377 |
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema); |
| 378 |
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema); |
| 379 |
|
| 380 |
return B.CreateICmpEQ(WideLHS, WideRHS); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
Value *CreateNE(Value *LHS, const FixedPointSemantics &LHSSema, |
| 389 |
Value *RHS, const FixedPointSemantics &RHSSema) { |
| 390 |
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema); |
| 391 |
|
| 392 |
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema); |
| 393 |
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema); |
| 394 |
|
| 395 |
return B.CreateICmpNE(WideLHS, WideRHS); |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
Value *CreateLT(Value *LHS, const FixedPointSemantics &LHSSema, |
| 404 |
Value *RHS, const FixedPointSemantics &RHSSema) { |
| 405 |
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema); |
| 406 |
|
| 407 |
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema); |
| 408 |
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema); |
| 409 |
|
| 410 |
return CommonSema.isSigned() ? B.CreateICmpSLT(WideLHS, WideRHS) |
| 411 |
: B.CreateICmpULT(WideLHS, WideRHS); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
Value *CreateLE(Value *LHS, const FixedPointSemantics &LHSSema, |
| 420 |
Value *RHS, const FixedPointSemantics &RHSSema) { |
| 421 |
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema); |
| 422 |
|
| 423 |
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema); |
| 424 |
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema); |
| 425 |
|
| 426 |
return CommonSema.isSigned() ? B.CreateICmpSLE(WideLHS, WideRHS) |
| 427 |
: B.CreateICmpULE(WideLHS, WideRHS); |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
Value *CreateGT(Value *LHS, const FixedPointSemantics &LHSSema, |
| 436 |
Value *RHS, const FixedPointSemantics &RHSSema) { |
| 437 |
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema); |
| 438 |
|
| 439 |
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema); |
| 440 |
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema); |
| 441 |
|
| 442 |
return CommonSema.isSigned() ? B.CreateICmpSGT(WideLHS, WideRHS) |
| 443 |
: B.CreateICmpUGT(WideLHS, WideRHS); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
Value *CreateGE(Value *LHS, const FixedPointSemantics &LHSSema, |
| 452 |
Value *RHS, const FixedPointSemantics &RHSSema) { |
| 453 |
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema); |
| 454 |
|
| 455 |
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema); |
| 456 |
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema); |
| 457 |
|
| 458 |
return CommonSema.isSigned() ? B.CreateICmpSGE(WideLHS, WideRHS) |
| 459 |
: B.CreateICmpUGE(WideLHS, WideRHS); |
| 460 |
} |
| 461 |
}; |
| 462 |
|
| 463 |
} |
| 464 |
|
| 465 |
#endif |