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"
9 
10 using namespace llvm;
11 
12 namespace llvm {
13   void initializeTestPassPass(PassRegistry &);
14 }
15 
16 namespace {
17 
18 void initLLVM() {
19   InitializeAllTargets();
20   InitializeAllTargetMCs();
21   InitializeAllAsmPrinters();
22   InitializeAllAsmParsers();
23 
24   PassRegistry *Registry = PassRegistry::getPassRegistry();
25   initializeCore(*Registry);
26   initializeCodeGen(*Registry);
27 }
28 
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;
37 
38   TargetOptions Options;
39   Options.EnableIPRA = EnableIPRA;
40   return std::unique_ptr<TargetMachine>(T->createTargetMachine(
41       "X86", "", "", Options, None, None, CodeGenOpt::Aggressive));
42 }
43 
44 typedef std::function<void(bool)> TargetOptionsTest;
45 
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());
53 
54   TargetPassConfig *TPC = LLVMTM->createPassConfig(PM);
55   (void)TPC;
56 
57   ASSERT_TRUE(TM->Options.EnableIPRA == EnableIPRA);
58 
59   delete TPC;
60 }
61 
62 } // End of anonymous namespace.
63 
64 TEST(TargetOptionsTest, IPRASetToOff) {
65   targetOptionsTest(false);
66 }
67 
68 TEST(TargetOptionsTest, IPRASetToOn) {
69   targetOptionsTest(true);
70 }
71 
72 int main(int argc, char **argv) {
73   ::testing::InitGoogleTest(&argc, argv);
74   initLLVM();
75   return RUN_ALL_TESTS();
76 }
77