[CMake] Allow overriding MSVC_DIA_SDK_DIR via CMake
[llvm-core.git] / unittests / CodeGen / TargetOptionsTest.cpp
blob6b4d3eda7ee8dbe2121954dbfd3863102cae2790
1 #include "llvm/Target/TargetOptions.h"
2 #include "llvm/CodeGen/TargetPassConfig.h"
3 #include "llvm/IR/LLVMContext.h"
4 #include "llvm/IR/LegacyPassManager.h"
5 #include "llvm/Support/TargetRegistry.h"
6 #include "llvm/Support/TargetSelect.h"
7 #include "llvm/Target/TargetMachine.h"
8 #include "gtest/gtest.h"
10 using namespace llvm;
12 namespace llvm {
13 void initializeTestPassPass(PassRegistry &);
16 namespace {
18 void initLLVM() {
19 InitializeAllTargets();
20 InitializeAllTargetMCs();
21 InitializeAllAsmPrinters();
22 InitializeAllAsmParsers();
24 PassRegistry *Registry = PassRegistry::getPassRegistry();
25 initializeCore(*Registry);
26 initializeCodeGen(*Registry);
29 /// Create a TargetMachine. We need a target that doesn't have IPRA enabled by
30 /// default. That turns out to be all targets at the moment, so just use X86.
31 std::unique_ptr<TargetMachine> createTargetMachine(bool EnableIPRA) {
32 Triple TargetTriple("x86_64--");
33 std::string Error;
34 const Target *T = TargetRegistry::lookupTarget("", TargetTriple, Error);
35 if (!T)
36 return nullptr;
38 TargetOptions Options;
39 Options.EnableIPRA = EnableIPRA;
40 return std::unique_ptr<TargetMachine>(T->createTargetMachine(
41 "X86", "", "", Options, None, None, CodeGenOpt::Aggressive));
44 typedef std::function<void(bool)> TargetOptionsTest;
46 static void targetOptionsTest(bool EnableIPRA) {
47 std::unique_ptr<TargetMachine> TM = createTargetMachine(EnableIPRA);
48 // This test is designed for the X86 backend; stop if it is not available.
49 if (!TM)
50 return;
51 legacy::PassManager PM;
52 LLVMTargetMachine *LLVMTM = static_cast<LLVMTargetMachine *>(TM.get());
54 TargetPassConfig *TPC = LLVMTM->createPassConfig(PM);
55 (void)TPC;
57 ASSERT_TRUE(TM->Options.EnableIPRA == EnableIPRA);
59 delete TPC;
62 } // End of anonymous namespace.
64 TEST(TargetOptionsTest, IPRASetToOff) {
65 targetOptionsTest(false);
68 TEST(TargetOptionsTest, IPRASetToOn) {
69 targetOptionsTest(true);
72 int main(int argc, char **argv) {
73 ::testing::InitGoogleTest(&argc, argv);
74 initLLVM();
75 return RUN_ALL_TESTS();