|
| 1 | +//===- unittests/CodeGen/BufferSourceTest.cpp - MemoryBuffer source tests -===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "llvm/Analysis/ConstantFolding.h" |
| 10 | +#include "llvm/Analysis/TargetLibraryInfo.h" |
| 11 | +#include "llvm/CodeGen/GlobalISel/CallLowering.h" |
| 12 | +#include "llvm/IR/IRBuilder.h" |
| 13 | +#include "llvm/IR/InstrTypes.h" |
| 14 | +#include "gtest/gtest.h" |
| 15 | + |
| 16 | +using namespace llvm; |
| 17 | + |
| 18 | +namespace { |
| 19 | + |
| 20 | +class ConstantFoldLogf128Fixture |
| 21 | + : public ::testing ::TestWithParam<std::string> { |
| 22 | +protected: |
| 23 | + std::string FuncName; |
| 24 | +}; |
| 25 | + |
| 26 | +TEST_P(ConstantFoldLogf128Fixture, ConstantFoldLogf128) { |
| 27 | + LLVMContext Context; |
| 28 | + IRBuilder<> Builder(Context); |
| 29 | + Module MainModule("Logf128TestModule", Context); |
| 30 | + MainModule.setTargetTriple("aarch64-unknown-linux"); |
| 31 | + |
| 32 | + Type *FP128Ty = Type::getFP128Ty(Context); |
| 33 | + FunctionType *FP128Prototype = FunctionType::get(FP128Ty, false); |
| 34 | + Function *Logf128TestFunction = Function::Create( |
| 35 | + FP128Prototype, Function::ExternalLinkage, "logf128test", MainModule); |
| 36 | + BasicBlock *EntryBlock = |
| 37 | + BasicBlock::Create(Context, "entry", Logf128TestFunction); |
| 38 | + Builder.SetInsertPoint(EntryBlock); |
| 39 | + |
| 40 | + FunctionType *FP128FP128Prototype = |
| 41 | + FunctionType::get(FP128Ty, {FP128Ty}, false); |
| 42 | + Constant *Constant2L = ConstantFP::get128(FP128Ty, 2.0L); |
| 43 | + |
| 44 | + std::string FunctionName = GetParam(); |
| 45 | + Function *Logl = Function::Create( |
| 46 | + FP128FP128Prototype, Function::ExternalLinkage, FunctionName, MainModule); |
| 47 | + CallInst *LoglCall = Builder.CreateCall(Logl, Constant2L); |
| 48 | + |
| 49 | + TargetLibraryInfoImpl TLII(Triple(MainModule.getTargetTriple())); |
| 50 | + TargetLibraryInfo TLI(TLII, Logf128TestFunction); |
| 51 | + Constant *FoldResult = ConstantFoldCall(LoglCall, Logl, Constant2L, &TLI); |
| 52 | + |
| 53 | +#ifndef HAS_LOGF128 |
| 54 | + ASSERT_TRUE(FoldResult == nullptr); |
| 55 | +#else |
| 56 | + auto ConstantLog = dyn_cast<ConstantFP>(FoldResult); |
| 57 | + ASSERT_TRUE(ConstantLog); |
| 58 | + |
| 59 | + APFloat APF = ConstantLog->getValueAPF(); |
| 60 | + char LongDoubleHexString[0xFF]; |
| 61 | + unsigned Size = |
| 62 | + APF.convertToHexString(LongDoubleHexString, 32, true, |
| 63 | + APFloatBase::roundingMode::NearestTiesToAway); |
| 64 | + EXPECT_GT(Size, 0U); |
| 65 | + |
| 66 | + ASSERT_STREQ(LongDoubleHexString, |
| 67 | + std::string("0X1.62E42FEFA39E0000000000000000000P-1").c_str()); |
| 68 | +#endif |
| 69 | +} |
| 70 | + |
| 71 | +INSTANTIATE_TEST_SUITE_P(ConstantFoldLogf128, ConstantFoldLogf128Fixture, |
| 72 | + ::testing::Values("logl", "llvm.log.f128")); |
| 73 | + |
| 74 | +} // end anonymous namespace |
0 commit comments