TargetOptionsTest.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. using namespace llvm;
  10. namespace llvm {
  11. void initializeTestPassPass(PassRegistry &);
  12. }
  13. namespace {
  14. void initLLVM() {
  15. InitializeAllTargets();
  16. InitializeAllTargetMCs();
  17. InitializeAllAsmPrinters();
  18. InitializeAllAsmParsers();
  19. PassRegistry *Registry = PassRegistry::getPassRegistry();
  20. initializeCore(*Registry);
  21. initializeCodeGen(*Registry);
  22. }
  23. /// Create a TargetMachine. We need a target that doesn't have IPRA enabled by
  24. /// default. That turns out to be all targets at the moment, so just use X86.
  25. std::unique_ptr<TargetMachine> createTargetMachine(bool EnableIPRA) {
  26. Triple TargetTriple("x86_64--");
  27. std::string Error;
  28. const Target *T = TargetRegistry::lookupTarget("", TargetTriple, Error);
  29. if (!T)
  30. return nullptr;
  31. TargetOptions Options;
  32. Options.EnableIPRA = EnableIPRA;
  33. return std::unique_ptr<TargetMachine>(T->createTargetMachine(
  34. "X86", "", "", Options, None, None, CodeGenOpt::Aggressive));
  35. }
  36. typedef std::function<void(bool)> TargetOptionsTest;
  37. static void targetOptionsTest(bool EnableIPRA) {
  38. std::unique_ptr<TargetMachine> TM = createTargetMachine(EnableIPRA);
  39. // This test is designed for the X86 backend; stop if it is not available.
  40. if (!TM)
  41. return;
  42. legacy::PassManager PM;
  43. LLVMTargetMachine *LLVMTM = static_cast<LLVMTargetMachine *>(TM.get());
  44. TargetPassConfig *TPC = LLVMTM->createPassConfig(PM);
  45. (void)TPC;
  46. ASSERT_TRUE(TM->Options.EnableIPRA == EnableIPRA);
  47. delete TPC;
  48. }
  49. } // End of anonymous namespace.
  50. TEST(TargetOptionsTest, IPRASetToOff) {
  51. targetOptionsTest(false);
  52. }
  53. TEST(TargetOptionsTest, IPRASetToOn) {
  54. targetOptionsTest(true);
  55. }
  56. int main(int argc, char **argv) {
  57. ::testing::InitGoogleTest(&argc, argv);
  58. initLLVM();
  59. return RUN_ALL_TESTS();
  60. }