Cloning.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //===- Cloning.cpp - Unit tests for the Cloner ----------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/Instructions.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/ADT/SmallPtrSet.h"
  12. #include "llvm/Argument.h"
  13. #include "llvm/Constant.h"
  14. #include "llvm/LLVMContext.h"
  15. #include "gtest/gtest.h"
  16. using namespace llvm;
  17. namespace {
  18. class CloneInstruction : public ::testing::Test {
  19. protected:
  20. virtual void SetUp() {
  21. V = NULL;
  22. }
  23. template <typename T>
  24. T *clone(T *V1) {
  25. Value *V2 = V1->clone();
  26. Orig.insert(V1);
  27. Clones.insert(V2);
  28. return cast<T>(V2);
  29. }
  30. void eraseClones() {
  31. DeleteContainerPointers(Clones);
  32. }
  33. virtual void TearDown() {
  34. eraseClones();
  35. DeleteContainerPointers(Orig);
  36. delete V;
  37. }
  38. SmallPtrSet<Value *, 4> Orig; // Erase on exit
  39. SmallPtrSet<Value *, 4> Clones; // Erase in eraseClones
  40. LLVMContext context;
  41. Value *V;
  42. };
  43. TEST_F(CloneInstruction, OverflowBits) {
  44. V = new Argument(Type::getInt32Ty(context));
  45. BinaryOperator *Add = BinaryOperator::Create(Instruction::Add, V, V);
  46. BinaryOperator *Sub = BinaryOperator::Create(Instruction::Sub, V, V);
  47. BinaryOperator *Mul = BinaryOperator::Create(Instruction::Mul, V, V);
  48. BinaryOperator *AddClone = this->clone(Add);
  49. BinaryOperator *SubClone = this->clone(Sub);
  50. BinaryOperator *MulClone = this->clone(Mul);
  51. EXPECT_FALSE(AddClone->hasNoUnsignedWrap());
  52. EXPECT_FALSE(AddClone->hasNoSignedWrap());
  53. EXPECT_FALSE(SubClone->hasNoUnsignedWrap());
  54. EXPECT_FALSE(SubClone->hasNoSignedWrap());
  55. EXPECT_FALSE(MulClone->hasNoUnsignedWrap());
  56. EXPECT_FALSE(MulClone->hasNoSignedWrap());
  57. eraseClones();
  58. Add->setHasNoUnsignedWrap();
  59. Sub->setHasNoUnsignedWrap();
  60. Mul->setHasNoUnsignedWrap();
  61. AddClone = this->clone(Add);
  62. SubClone = this->clone(Sub);
  63. MulClone = this->clone(Mul);
  64. EXPECT_TRUE(AddClone->hasNoUnsignedWrap());
  65. EXPECT_FALSE(AddClone->hasNoSignedWrap());
  66. EXPECT_TRUE(SubClone->hasNoUnsignedWrap());
  67. EXPECT_FALSE(SubClone->hasNoSignedWrap());
  68. EXPECT_TRUE(MulClone->hasNoUnsignedWrap());
  69. EXPECT_FALSE(MulClone->hasNoSignedWrap());
  70. eraseClones();
  71. Add->setHasNoSignedWrap();
  72. Sub->setHasNoSignedWrap();
  73. Mul->setHasNoSignedWrap();
  74. AddClone = this->clone(Add);
  75. SubClone = this->clone(Sub);
  76. MulClone = this->clone(Mul);
  77. EXPECT_TRUE(AddClone->hasNoUnsignedWrap());
  78. EXPECT_TRUE(AddClone->hasNoSignedWrap());
  79. EXPECT_TRUE(SubClone->hasNoUnsignedWrap());
  80. EXPECT_TRUE(SubClone->hasNoSignedWrap());
  81. EXPECT_TRUE(MulClone->hasNoUnsignedWrap());
  82. EXPECT_TRUE(MulClone->hasNoSignedWrap());
  83. eraseClones();
  84. Add->setHasNoUnsignedWrap(false);
  85. Sub->setHasNoUnsignedWrap(false);
  86. Mul->setHasNoUnsignedWrap(false);
  87. AddClone = this->clone(Add);
  88. SubClone = this->clone(Sub);
  89. MulClone = this->clone(Mul);
  90. EXPECT_FALSE(AddClone->hasNoUnsignedWrap());
  91. EXPECT_TRUE(AddClone->hasNoSignedWrap());
  92. EXPECT_FALSE(SubClone->hasNoUnsignedWrap());
  93. EXPECT_TRUE(SubClone->hasNoSignedWrap());
  94. EXPECT_FALSE(MulClone->hasNoUnsignedWrap());
  95. EXPECT_TRUE(MulClone->hasNoSignedWrap());
  96. }
  97. TEST_F(CloneInstruction, Inbounds) {
  98. V = new Argument(Type::getInt32PtrTy(context));
  99. Constant *Z = Constant::getNullValue(Type::getInt32Ty(context));
  100. std::vector<Value *> ops;
  101. ops.push_back(Z);
  102. GetElementPtrInst *GEP = GetElementPtrInst::Create(V, ops);
  103. EXPECT_FALSE(this->clone(GEP)->isInBounds());
  104. GEP->setIsInBounds();
  105. EXPECT_TRUE(this->clone(GEP)->isInBounds());
  106. }
  107. TEST_F(CloneInstruction, Exact) {
  108. V = new Argument(Type::getInt32Ty(context));
  109. BinaryOperator *SDiv = BinaryOperator::Create(Instruction::SDiv, V, V);
  110. EXPECT_FALSE(this->clone(SDiv)->isExact());
  111. SDiv->setIsExact(true);
  112. EXPECT_TRUE(this->clone(SDiv)->isExact());
  113. }
  114. }