95 lines · c
1//===---- llvm/unittest/CodeGen/SelectionDAGTestBase.h --------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/Analysis/OptimizationRemarkEmitter.h"10#include "llvm/AsmParser/Parser.h"11#include "llvm/CodeGen/MachineModuleInfo.h"12#include "llvm/CodeGen/TargetLowering.h"13#include "llvm/IR/Module.h"14#include "llvm/MC/TargetRegistry.h"15#include "llvm/Support/SourceMgr.h"16#include "llvm/Support/TargetSelect.h"17#include "llvm/Target/TargetMachine.h"18#include "gtest/gtest.h"19 20using namespace llvm;21 22class SelectionDAGTestBase : public testing::Test {23protected:24 static void SetUpTestCase() {25 InitializeAllTargets();26 InitializeAllTargetMCs();27 }28 29 void SetUp() override {30 StringRef Assembly = "@g = global i32 0\n"31 "@g_alias = alias i32, i32* @g\n"32 "define i32 @f() {\n"33 " %1 = load i32, i32* @g\n"34 " ret i32 %1\n"35 "}";36 37 Triple TargetTriple("aarch64--");38 std::string Error;39 const Target *T = TargetRegistry::lookupTarget("", TargetTriple, Error);40 // FIXME: These tests do not depend on AArch64 specifically, but we have to41 // initialize a target. A skeleton Target for unittests would allow us to42 // always run these tests.43 if (!T)44 GTEST_SKIP();45 46 TargetOptions Options;47 TM = std::unique_ptr<TargetMachine>(48 T->createTargetMachine(TargetTriple, "", "+sve", Options, std::nullopt,49 std::nullopt, CodeGenOptLevel::Aggressive));50 if (!TM)51 GTEST_SKIP();52 53 SMDiagnostic SMError;54 M = parseAssemblyString(Assembly, SMError, Context);55 ASSERT_TRUE(M && "Could not parse module!");56 M->setDataLayout(TM->createDataLayout());57 58 F = M->getFunction("f");59 ASSERT_TRUE(F && "Could not get function f!");60 G = M->getGlobalVariable("g");61 ASSERT_TRUE(G && "Could not get global g!");62 AliasedG = M->getNamedAlias("g_alias");63 ASSERT_TRUE(AliasedG && "Could not get alias g_alias!");64 65 MachineModuleInfo MMI(TM.get());66 67 MF = std::make_unique<MachineFunction>(*F, *TM, *TM->getSubtargetImpl(*F),68 MMI.getContext(), 0);69 70 DAG = std::make_unique<SelectionDAG>(*TM, CodeGenOptLevel::None);71 if (!DAG)72 reportFatalUsageError("Failed to create SelectionDAG?");73 OptimizationRemarkEmitter ORE(F);74 DAG->init(*MF, ORE, nullptr, nullptr, nullptr, nullptr, nullptr, MMI,75 nullptr);76 }77 78 TargetLoweringBase::LegalizeTypeAction getTypeAction(EVT VT) {79 return DAG->getTargetLoweringInfo().getTypeAction(Context, VT);80 }81 82 EVT getTypeToTransformTo(EVT VT) {83 return DAG->getTargetLoweringInfo().getTypeToTransformTo(Context, VT);84 }85 86 LLVMContext Context;87 std::unique_ptr<TargetMachine> TM;88 std::unique_ptr<Module> M;89 Function *F;90 GlobalVariable *G;91 GlobalAlias *AliasedG;92 std::unique_ptr<MachineFunction> MF;93 std::unique_ptr<SelectionDAG> DAG;94};95