xref: /llvm-project/llvm/unittests/CodeGen/TargetOptionsTest.cpp (revision 07cda257f84ba773ee6dd0b140889c59f6459f39)
1 #include "llvm/Target/TargetOptions.h"
2 #include "llvm/IR/LLVMContext.h"
3 #include "llvm/IR/LegacyPassManager.h"
4 #include "llvm/Support/TargetRegistry.h"
5 #include "llvm/Support/TargetSelect.h"
6 #include "llvm/Target/TargetMachine.h"
7 #include "gtest/gtest.h"
8 
9 using namespace llvm;
10 
11 namespace llvm {
12   void initializeTestPassPass(PassRegistry &);
13 }
14 
15 namespace {
16 
17 void initLLVM() {
18   InitializeAllTargets();
19   InitializeAllTargetMCs();
20   InitializeAllAsmPrinters();
21   InitializeAllAsmParsers();
22 
23   PassRegistry *Registry = PassRegistry::getPassRegistry();
24   initializeCore(*Registry);
25   initializeCodeGen(*Registry);
26 }
27 
28 /// Create a TargetMachine. We need a target that doesn't have IPRA enabled by
29 /// default. That turns out to be all targets at the moment, so just use X86.
30 std::unique_ptr<TargetMachine> createTargetMachine(bool EnableIPRA) {
31   Triple TargetTriple("x86_64--");
32   std::string Error;
33   const Target *T = TargetRegistry::lookupTarget("", TargetTriple, Error);
34   if (!T)
35     return nullptr;
36 
37   TargetOptions Options;
38   Options.EnableIPRA = EnableIPRA;
39   return std::unique_ptr<TargetMachine>(T->createTargetMachine(
40       "X86", "", "", Options, None, None, CodeGenOpt::Aggressive));
41 }
42 
43 typedef std::function<void(bool)> TargetOptionsTest;
44 
45 static void targetOptionsTest(bool EnableIPRA) {
46   LLVMContext Context;
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);
53 
54   TargetPassConfig &TPC = *LLVMTM.createPassConfig(PM);
55   (void)TPC;
56 
57   ASSERT_TRUE(TM->Options.EnableIPRA == EnableIPRA);
58 }
59 
60 } // End of anonymous namespace.
61 
62 TEST(TargetOptionsTest, IPRASetToOff) {
63   targetOptionsTest(false);
64 }
65 
66 TEST(TargetOptionsTest, IPRASetToOn) {
67   targetOptionsTest(true);
68 }
69 
70 int main(int argc, char **argv) {
71   ::testing::InitGoogleTest(&argc, argv);
72   initLLVM();
73   return RUN_ALL_TESTS();
74 }
75