FunctionTest.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //===- FunctionTest.cpp - Function unit tests -----------------------------===//
  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-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/IR/Function.h"
  9. #include "llvm/IR/Module.h"
  10. #include "gtest/gtest.h"
  11. using namespace llvm;
  12. namespace {
  13. TEST(FunctionTest, hasLazyArguments) {
  14. LLVMContext C;
  15. Type *ArgTypes[] = {Type::getInt8Ty(C), Type::getInt32Ty(C)};
  16. FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), ArgTypes, false);
  17. // Functions start out with lazy arguments.
  18. std::unique_ptr<Function> F(
  19. Function::Create(FTy, GlobalValue::ExternalLinkage, "F"));
  20. EXPECT_TRUE(F->hasLazyArguments());
  21. // Checking for empty or size shouldn't force arguments to be instantiated.
  22. EXPECT_FALSE(F->arg_empty());
  23. EXPECT_TRUE(F->hasLazyArguments());
  24. EXPECT_EQ(2u, F->arg_size());
  25. EXPECT_TRUE(F->hasLazyArguments());
  26. // The argument list should be populated at first access.
  27. (void)F->arg_begin();
  28. EXPECT_FALSE(F->hasLazyArguments());
  29. }
  30. TEST(FunctionTest, stealArgumentListFrom) {
  31. LLVMContext C;
  32. Type *ArgTypes[] = {Type::getInt8Ty(C), Type::getInt32Ty(C)};
  33. FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), ArgTypes, false);
  34. std::unique_ptr<Function> F1(
  35. Function::Create(FTy, GlobalValue::ExternalLinkage, "F1"));
  36. std::unique_ptr<Function> F2(
  37. Function::Create(FTy, GlobalValue::ExternalLinkage, "F1"));
  38. EXPECT_TRUE(F1->hasLazyArguments());
  39. EXPECT_TRUE(F2->hasLazyArguments());
  40. // Steal arguments before they've been accessed. Nothing should change; both
  41. // functions should still have lazy arguments.
  42. //
  43. // steal(empty); drop (empty)
  44. F1->stealArgumentListFrom(*F2);
  45. EXPECT_TRUE(F1->hasLazyArguments());
  46. EXPECT_TRUE(F2->hasLazyArguments());
  47. // Save arguments from F1 for later assertions. F1 won't have lazy arguments
  48. // anymore.
  49. SmallVector<Argument *, 4> Args;
  50. for (Argument &A : F1->args())
  51. Args.push_back(&A);
  52. EXPECT_EQ(2u, Args.size());
  53. EXPECT_FALSE(F1->hasLazyArguments());
  54. // Steal arguments from F1 to F2. F1's arguments should be lazy again.
  55. //
  56. // steal(real); drop (empty)
  57. F2->stealArgumentListFrom(*F1);
  58. EXPECT_TRUE(F1->hasLazyArguments());
  59. EXPECT_FALSE(F2->hasLazyArguments());
  60. unsigned I = 0;
  61. for (Argument &A : F2->args()) {
  62. EXPECT_EQ(Args[I], &A);
  63. I++;
  64. }
  65. EXPECT_EQ(2u, I);
  66. // Check that arguments in F1 don't have pointer equality with the saved ones.
  67. // This also instantiates F1's arguments.
  68. I = 0;
  69. for (Argument &A : F1->args()) {
  70. EXPECT_NE(Args[I], &A);
  71. I++;
  72. }
  73. EXPECT_EQ(2u, I);
  74. EXPECT_FALSE(F1->hasLazyArguments());
  75. EXPECT_FALSE(F2->hasLazyArguments());
  76. // Steal back from F2. F2's arguments should be lazy again.
  77. //
  78. // steal(real); drop (real)
  79. F1->stealArgumentListFrom(*F2);
  80. EXPECT_FALSE(F1->hasLazyArguments());
  81. EXPECT_TRUE(F2->hasLazyArguments());
  82. I = 0;
  83. for (Argument &A : F1->args()) {
  84. EXPECT_EQ(Args[I], &A);
  85. I++;
  86. }
  87. EXPECT_EQ(2u, I);
  88. // Steal from F2 a second time. Now both functions should have lazy
  89. // arguments.
  90. //
  91. // steal(empty); drop (real)
  92. F1->stealArgumentListFrom(*F2);
  93. EXPECT_TRUE(F1->hasLazyArguments());
  94. EXPECT_TRUE(F2->hasLazyArguments());
  95. }
  96. // Test setting and removing section information
  97. TEST(FunctionTest, setSection) {
  98. LLVMContext C;
  99. Module M("test", C);
  100. llvm::Function *F =
  101. Function::Create(llvm::FunctionType::get(llvm::Type::getVoidTy(C), false),
  102. llvm::GlobalValue::ExternalLinkage, "F", &M);
  103. F->setSection(".text.test");
  104. EXPECT_TRUE(F->getSection() == ".text.test");
  105. EXPECT_TRUE(F->hasSection());
  106. F->setSection("");
  107. EXPECT_FALSE(F->hasSection());
  108. F->setSection(".text.test");
  109. F->setSection(".text.test2");
  110. EXPECT_TRUE(F->getSection() == ".text.test2");
  111. EXPECT_TRUE(F->hasSection());
  112. }
  113. TEST(FunctionTest, GetPointerAlignment) {
  114. LLVMContext Context;
  115. Type *VoidType(Type::getVoidTy(Context));
  116. FunctionType *FuncType(FunctionType::get(VoidType, false));
  117. std::unique_ptr<Function> Func(Function::Create(
  118. FuncType, GlobalValue::ExternalLinkage));
  119. EXPECT_EQ(0U, Func->getPointerAlignment(DataLayout("")));
  120. EXPECT_EQ(1U, Func->getPointerAlignment(DataLayout("Fi8")));
  121. EXPECT_EQ(1U, Func->getPointerAlignment(DataLayout("Fn8")));
  122. EXPECT_EQ(2U, Func->getPointerAlignment(DataLayout("Fi16")));
  123. EXPECT_EQ(2U, Func->getPointerAlignment(DataLayout("Fn16")));
  124. EXPECT_EQ(4U, Func->getPointerAlignment(DataLayout("Fi32")));
  125. EXPECT_EQ(4U, Func->getPointerAlignment(DataLayout("Fn32")));
  126. Func->setAlignment(4U);
  127. EXPECT_EQ(0U, Func->getPointerAlignment(DataLayout("")));
  128. EXPECT_EQ(1U, Func->getPointerAlignment(DataLayout("Fi8")));
  129. EXPECT_EQ(4U, Func->getPointerAlignment(DataLayout("Fn8")));
  130. EXPECT_EQ(2U, Func->getPointerAlignment(DataLayout("Fi16")));
  131. EXPECT_EQ(4U, Func->getPointerAlignment(DataLayout("Fn16")));
  132. EXPECT_EQ(4U, Func->getPointerAlignment(DataLayout("Fi32")));
  133. EXPECT_EQ(4U, Func->getPointerAlignment(DataLayout("Fn32")));
  134. }
  135. } // end namespace