llvm.org GIT mirror llvm / 6d73b80
Use unique_ptr to handle ownership of Value*s in Cloning unit tests. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206828 91177308-0d34-0410-b5e6-96231b3b80d8 David Blaikie 5 years ago
1 changed file(s) with 29 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
99 #include "llvm/Transforms/Utils/Cloning.h"
1010 #include "llvm/ADT/ArrayRef.h"
1111 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallPtrSet.h"
1312 #include "llvm/IR/Argument.h"
1413 #include "llvm/IR/Constant.h"
1514 #include "llvm/IR/DebugInfo.h"
2423 #include "llvm/IR/LLVMContext.h"
2524 #include "gtest/gtest.h"
2625
26 #include
27
2728 using namespace llvm;
2829
2930 namespace {
3738 template
3839 T *clone(T *V1) {
3940 Value *V2 = V1->clone();
40 Orig.insert(V1);
41 Clones.insert(V2);
41 std::unique_ptr V(V1);
42 if (!Orig.insert(std::move(V)).second)
43 V.release(); // this wasn't the first time we added the element, so the
44 // set already had ownership
45 Clones.insert(std::unique_ptr(V2));
4246 return cast(V2);
4347 }
4448
45 void eraseClones() {
46 DeleteContainerPointers(Clones);
47 }
49 void eraseClones() { Clones.clear(); }
4850
4951 virtual void TearDown() {
5052 eraseClones();
51 DeleteContainerPointers(Orig);
52 delete V;
53 }
54
55 SmallPtrSet Orig; // Erase on exit
56 SmallPtrSet Clones; // Erase in eraseClones
53 Orig.clear();
54 V.reset();
55 }
56
57 std::set> Orig; // Erase on exit
58 std::set> Clones; // Erase in eraseClones
5759
5860 LLVMContext context;
59 Value *V;
61 std::unique_ptr V;
6062 };
6163
6264 TEST_F(CloneInstruction, OverflowBits) {
63 V = new Argument(Type::getInt32Ty(context));
64
65 BinaryOperator *Add = BinaryOperator::Create(Instruction::Add, V, V);
66 BinaryOperator *Sub = BinaryOperator::Create(Instruction::Sub, V, V);
67 BinaryOperator *Mul = BinaryOperator::Create(Instruction::Mul, V, V);
65 V = make_unique(Type::getInt32Ty(context));
66
67 BinaryOperator *Add =
68 BinaryOperator::Create(Instruction::Add, V.get(), V.get());
69 BinaryOperator *Sub =
70 BinaryOperator::Create(Instruction::Sub, V.get(), V.get());
71 BinaryOperator *Mul =
72 BinaryOperator::Create(Instruction::Mul, V.get(), V.get());
6873
6974 BinaryOperator *AddClone = this->clone(Add);
7075 BinaryOperator *SubClone = this->clone(Sub);
130135 }
131136
132137 TEST_F(CloneInstruction, Inbounds) {
133 V = new Argument(Type::getInt32PtrTy(context));
138 V = make_unique(Type::getInt32PtrTy(context));
134139
135140 Constant *Z = Constant::getNullValue(Type::getInt32Ty(context));
136141 std::vector ops;
137142 ops.push_back(Z);
138 GetElementPtrInst *GEP = GetElementPtrInst::Create(V, ops);
143 GetElementPtrInst *GEP = GetElementPtrInst::Create(V.get(), ops);
139144 EXPECT_FALSE(this->clone(GEP)->isInBounds());
140145
141146 GEP->setIsInBounds();
143148 }
144149
145150 TEST_F(CloneInstruction, Exact) {
146 V = new Argument(Type::getInt32Ty(context));
147
148 BinaryOperator *SDiv = BinaryOperator::Create(Instruction::SDiv, V, V);
151 V = make_unique(Type::getInt32Ty(context));
152
153 BinaryOperator *SDiv =
154 BinaryOperator::Create(Instruction::SDiv, V.get(), V.get());
149155 EXPECT_FALSE(this->clone(SDiv)->isExact());
150156
151157 SDiv->setIsExact(true);