49e798b66440c41bbb963176d386e0075fc99e8c
[oota-llvm.git] / unittests / Transforms / Utils / Cloning.cpp
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
10 #include "llvm/Transforms/Utils/Cloning.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/SmallPtrSet.h"
14 #include "llvm/IR/Argument.h"
15 #include "llvm/IR/Constant.h"
16 #include "llvm/IR/DIBuilder.h"
17 #include "llvm/IR/DebugInfo.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/InstIterator.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/IntrinsicInst.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/Verifier.h"
26 #include "gtest/gtest.h"
27
28 using namespace llvm;
29
30 namespace {
31
32 class CloneInstruction : public ::testing::Test {
33 protected:
34   virtual void SetUp() {
35     V = nullptr;
36   }
37
38   template <typename T>
39   T *clone(T *V1) {
40     Value *V2 = V1->clone();
41     Orig.insert(V1);
42     Clones.insert(V2);
43     return cast<T>(V2);
44   }
45
46   void eraseClones() {
47     DeleteContainerPointers(Clones);
48   }
49
50   virtual void TearDown() {
51     eraseClones();
52     DeleteContainerPointers(Orig);
53     delete V;
54   }
55
56   SmallPtrSet<Value *, 4> Orig;   // Erase on exit
57   SmallPtrSet<Value *, 4> Clones; // Erase in eraseClones
58
59   LLVMContext context;
60   Value *V;
61 };
62
63 TEST_F(CloneInstruction, OverflowBits) {
64   V = new Argument(Type::getInt32Ty(context));
65
66   BinaryOperator *Add = BinaryOperator::Create(Instruction::Add, V, V);
67   BinaryOperator *Sub = BinaryOperator::Create(Instruction::Sub, V, V);
68   BinaryOperator *Mul = BinaryOperator::Create(Instruction::Mul, V, V);
69
70   BinaryOperator *AddClone = this->clone(Add);
71   BinaryOperator *SubClone = this->clone(Sub);
72   BinaryOperator *MulClone = this->clone(Mul);
73
74   EXPECT_FALSE(AddClone->hasNoUnsignedWrap());
75   EXPECT_FALSE(AddClone->hasNoSignedWrap());
76   EXPECT_FALSE(SubClone->hasNoUnsignedWrap());
77   EXPECT_FALSE(SubClone->hasNoSignedWrap());
78   EXPECT_FALSE(MulClone->hasNoUnsignedWrap());
79   EXPECT_FALSE(MulClone->hasNoSignedWrap());
80
81   eraseClones();
82
83   Add->setHasNoUnsignedWrap();
84   Sub->setHasNoUnsignedWrap();
85   Mul->setHasNoUnsignedWrap();
86
87   AddClone = this->clone(Add);
88   SubClone = this->clone(Sub);
89   MulClone = this->clone(Mul);
90
91   EXPECT_TRUE(AddClone->hasNoUnsignedWrap());
92   EXPECT_FALSE(AddClone->hasNoSignedWrap());
93   EXPECT_TRUE(SubClone->hasNoUnsignedWrap());
94   EXPECT_FALSE(SubClone->hasNoSignedWrap());
95   EXPECT_TRUE(MulClone->hasNoUnsignedWrap());
96   EXPECT_FALSE(MulClone->hasNoSignedWrap());
97
98   eraseClones();
99
100   Add->setHasNoSignedWrap();
101   Sub->setHasNoSignedWrap();
102   Mul->setHasNoSignedWrap();
103
104   AddClone = this->clone(Add);
105   SubClone = this->clone(Sub);
106   MulClone = this->clone(Mul);
107
108   EXPECT_TRUE(AddClone->hasNoUnsignedWrap());
109   EXPECT_TRUE(AddClone->hasNoSignedWrap());
110   EXPECT_TRUE(SubClone->hasNoUnsignedWrap());
111   EXPECT_TRUE(SubClone->hasNoSignedWrap());
112   EXPECT_TRUE(MulClone->hasNoUnsignedWrap());
113   EXPECT_TRUE(MulClone->hasNoSignedWrap());
114
115   eraseClones();
116
117   Add->setHasNoUnsignedWrap(false);
118   Sub->setHasNoUnsignedWrap(false);
119   Mul->setHasNoUnsignedWrap(false);
120
121   AddClone = this->clone(Add);
122   SubClone = this->clone(Sub);
123   MulClone = this->clone(Mul);
124
125   EXPECT_FALSE(AddClone->hasNoUnsignedWrap());
126   EXPECT_TRUE(AddClone->hasNoSignedWrap());
127   EXPECT_FALSE(SubClone->hasNoUnsignedWrap());
128   EXPECT_TRUE(SubClone->hasNoSignedWrap());
129   EXPECT_FALSE(MulClone->hasNoUnsignedWrap());
130   EXPECT_TRUE(MulClone->hasNoSignedWrap());
131 }
132
133 TEST_F(CloneInstruction, Inbounds) {
134   V = new Argument(Type::getInt32PtrTy(context));
135
136   Constant *Z = Constant::getNullValue(Type::getInt32Ty(context));
137   std::vector<Value *> ops;
138   ops.push_back(Z);
139   GetElementPtrInst *GEP =
140       GetElementPtrInst::Create(Type::getInt32Ty(context), V, ops);
141   EXPECT_FALSE(this->clone(GEP)->isInBounds());
142
143   GEP->setIsInBounds();
144   EXPECT_TRUE(this->clone(GEP)->isInBounds());
145 }
146
147 TEST_F(CloneInstruction, Exact) {
148   V = new Argument(Type::getInt32Ty(context));
149
150   BinaryOperator *SDiv = BinaryOperator::Create(Instruction::SDiv, V, V);
151   EXPECT_FALSE(this->clone(SDiv)->isExact());
152
153   SDiv->setIsExact(true);
154   EXPECT_TRUE(this->clone(SDiv)->isExact());
155 }
156
157 TEST_F(CloneInstruction, Attributes) {
158   Type *ArgTy1[] = { Type::getInt32PtrTy(context) };
159   FunctionType *FT1 =  FunctionType::get(Type::getVoidTy(context), ArgTy1, false);
160
161   Function *F1 = Function::Create(FT1, Function::ExternalLinkage);
162   BasicBlock *BB = BasicBlock::Create(context, "", F1);
163   IRBuilder<> Builder(BB);
164   Builder.CreateRetVoid();
165
166   Function *F2 = Function::Create(FT1, Function::ExternalLinkage);
167
168   Attribute::AttrKind AK[] = { Attribute::NoCapture };
169   AttributeSet AS = AttributeSet::get(context, 0, AK);
170   Argument *A = F1->arg_begin();
171   A->addAttr(AS);
172
173   SmallVector<ReturnInst*, 4> Returns;
174   ValueToValueMapTy VMap;
175   VMap[A] = UndefValue::get(A->getType());
176
177   CloneFunctionInto(F2, F1, VMap, false, Returns);
178   EXPECT_FALSE(F2->arg_begin()->hasNoCaptureAttr());
179
180   delete F1;
181   delete F2;
182 }
183
184 TEST_F(CloneInstruction, CallingConvention) {
185   Type *ArgTy1[] = { Type::getInt32PtrTy(context) };
186   FunctionType *FT1 =  FunctionType::get(Type::getVoidTy(context), ArgTy1, false);
187
188   Function *F1 = Function::Create(FT1, Function::ExternalLinkage);
189   F1->setCallingConv(CallingConv::Cold);
190   BasicBlock *BB = BasicBlock::Create(context, "", F1);
191   IRBuilder<> Builder(BB);
192   Builder.CreateRetVoid();
193
194   Function *F2 = Function::Create(FT1, Function::ExternalLinkage);
195
196   SmallVector<ReturnInst*, 4> Returns;
197   ValueToValueMapTy VMap;
198   VMap[F1->arg_begin()] = F2->arg_begin();
199
200   CloneFunctionInto(F2, F1, VMap, false, Returns);
201   EXPECT_EQ(CallingConv::Cold, F2->getCallingConv());
202
203   delete F1;
204   delete F2;
205 }
206
207 class CloneFunc : public ::testing::Test {
208 protected:
209   virtual void SetUp() {
210     SetupModule();
211     CreateOldFunc();
212     CreateNewFunc();
213     SetupFinder();
214   }
215
216   virtual void TearDown() {
217     delete Finder;
218   }
219
220   void SetupModule() {
221     M = new Module("", C);
222   }
223
224   void CreateOldFunc() {
225     FunctionType* FuncType = FunctionType::get(Type::getVoidTy(C), false);
226     OldFunc = Function::Create(FuncType, GlobalValue::PrivateLinkage, "f", M);
227     CreateOldFunctionBodyAndDI();
228   }
229
230   void CreateOldFunctionBodyAndDI() {
231     DIBuilder DBuilder(*M);
232     IRBuilder<> IBuilder(C);
233
234     // Function DI
235     DIFile File = DBuilder.createFile("filename.c", "/file/dir/");
236     DITypeArray ParamTypes = DBuilder.getOrCreateTypeArray(None);
237     DICompositeType FuncType = DBuilder.createSubroutineType(File, ParamTypes);
238     DICompileUnit CU = DBuilder.createCompileUnit(dwarf::DW_LANG_C99,
239         "filename.c", "/file/dir", "CloneFunc", false, "", 0);
240
241     DISubprogram Subprogram = DBuilder.createFunction(CU, "f", "f", File, 4,
242         FuncType, true, true, 3, 0, false, OldFunc);
243
244     // Function body
245     BasicBlock* Entry = BasicBlock::Create(C, "", OldFunc);
246     IBuilder.SetInsertPoint(Entry);
247     DebugLoc Loc = DebugLoc::get(3, 2, Subprogram);
248     IBuilder.SetCurrentDebugLocation(Loc);
249     AllocaInst* Alloca = IBuilder.CreateAlloca(IntegerType::getInt32Ty(C));
250     IBuilder.SetCurrentDebugLocation(DebugLoc::get(4, 2, Subprogram));
251     Value* AllocaContent = IBuilder.getInt32(1);
252     Instruction* Store = IBuilder.CreateStore(AllocaContent, Alloca);
253     IBuilder.SetCurrentDebugLocation(DebugLoc::get(5, 2, Subprogram));
254     Instruction* Terminator = IBuilder.CreateRetVoid();
255
256     // Create a local variable around the alloca
257     DIType IntType = DBuilder.createBasicType("int", 32, 0,
258         dwarf::DW_ATE_signed);
259     DIExpression E = DBuilder.createExpression();
260     DIVariable Variable = DBuilder.createLocalVariable(
261       dwarf::DW_TAG_auto_variable, Subprogram, "x", File, 5, IntType, true);
262     DBuilder.insertDeclare(Alloca, Variable, E, Store);
263     DBuilder.insertDbgValueIntrinsic(AllocaContent, 0, Variable, E, Terminator);
264     // Finalize the debug info
265     DBuilder.finalize();
266
267
268     // Create another, empty, compile unit
269     DIBuilder DBuilder2(*M);
270     DBuilder2.createCompileUnit(dwarf::DW_LANG_C99,
271         "extra.c", "/file/dir", "CloneFunc", false, "", 0);
272     DBuilder2.finalize();
273   }
274
275   void CreateNewFunc() {
276     ValueToValueMapTy VMap;
277     NewFunc = CloneFunction(OldFunc, VMap, true, nullptr);
278     M->getFunctionList().push_back(NewFunc);
279   }
280
281   void SetupFinder() {
282     Finder = new DebugInfoFinder();
283     Finder->processModule(*M);
284   }
285
286   LLVMContext C;
287   Function* OldFunc;
288   Function* NewFunc;
289   Module* M;
290   DebugInfoFinder* Finder;
291 };
292
293 // Test that a new, distinct function was created.
294 TEST_F(CloneFunc, NewFunctionCreated) {
295   EXPECT_NE(OldFunc, NewFunc);
296 }
297
298 // Test that a new subprogram entry was added and is pointing to the new
299 // function, while the original subprogram still points to the old one.
300 TEST_F(CloneFunc, Subprogram) {
301   EXPECT_FALSE(verifyModule(*M));
302
303   unsigned SubprogramCount = Finder->subprogram_count();
304   EXPECT_EQ(2U, SubprogramCount);
305
306   auto Iter = Finder->subprograms().begin();
307   DISubprogram Sub1 = cast<MDSubprogram>(*Iter);
308   Iter++;
309   DISubprogram Sub2 = cast<MDSubprogram>(*Iter);
310
311   EXPECT_TRUE((Sub1.getFunction() == OldFunc && Sub2.getFunction() == NewFunc)
312            || (Sub1.getFunction() == NewFunc && Sub2.getFunction() == OldFunc));
313 }
314
315 // Test that the new subprogram entry was not added to the CU which doesn't
316 // contain the old subprogram entry.
317 TEST_F(CloneFunc, SubprogramInRightCU) {
318   EXPECT_FALSE(verifyModule(*M));
319
320   EXPECT_EQ(2U, Finder->compile_unit_count());
321
322   auto Iter = Finder->compile_units().begin();
323   DICompileUnit CU1 = cast<MDCompileUnit>(*Iter);
324   Iter++;
325   DICompileUnit CU2 = cast<MDCompileUnit>(*Iter);
326   EXPECT_TRUE(CU1.getSubprograms().size() == 0 ||
327               CU2.getSubprograms().size() == 0);
328 }
329
330 // Test that instructions in the old function still belong to it in the
331 // metadata, while instruction in the new function belong to the new one.
332 TEST_F(CloneFunc, InstructionOwnership) {
333   EXPECT_FALSE(verifyModule(*M));
334
335   inst_iterator OldIter = inst_begin(OldFunc);
336   inst_iterator OldEnd = inst_end(OldFunc);
337   inst_iterator NewIter = inst_begin(NewFunc);
338   inst_iterator NewEnd = inst_end(NewFunc);
339   while (OldIter != OldEnd && NewIter != NewEnd) {
340     Instruction& OldI = *OldIter;
341     Instruction& NewI = *NewIter;
342     EXPECT_NE(&OldI, &NewI);
343
344     EXPECT_EQ(OldI.hasMetadata(), NewI.hasMetadata());
345     if (OldI.hasMetadata()) {
346       const DebugLoc& OldDL = OldI.getDebugLoc();
347       const DebugLoc& NewDL = NewI.getDebugLoc();
348
349       // Verify that the debug location data is the same
350       EXPECT_EQ(OldDL.getLine(), NewDL.getLine());
351       EXPECT_EQ(OldDL.getCol(), NewDL.getCol());
352
353       // But that they belong to different functions
354       DISubprogram OldSubprogram = cast<MDSubprogram>(OldDL.getScope());
355       DISubprogram NewSubprogram = cast<MDSubprogram>(NewDL.getScope());
356       EXPECT_EQ(OldFunc, OldSubprogram.getFunction());
357       EXPECT_EQ(NewFunc, NewSubprogram.getFunction());
358     }
359
360     ++OldIter;
361     ++NewIter;
362   }
363   EXPECT_EQ(OldEnd, OldIter);
364   EXPECT_EQ(NewEnd, NewIter);
365 }
366
367 // Test that the arguments for debug intrinsics in the new function were
368 // properly cloned
369 TEST_F(CloneFunc, DebugIntrinsics) {
370   EXPECT_FALSE(verifyModule(*M));
371
372   inst_iterator OldIter = inst_begin(OldFunc);
373   inst_iterator OldEnd = inst_end(OldFunc);
374   inst_iterator NewIter = inst_begin(NewFunc);
375   inst_iterator NewEnd = inst_end(NewFunc);
376   while (OldIter != OldEnd && NewIter != NewEnd) {
377     Instruction& OldI = *OldIter;
378     Instruction& NewI = *NewIter;
379     if (DbgDeclareInst* OldIntrin = dyn_cast<DbgDeclareInst>(&OldI)) {
380       DbgDeclareInst* NewIntrin = dyn_cast<DbgDeclareInst>(&NewI);
381       EXPECT_TRUE(NewIntrin);
382
383       // Old address must belong to the old function
384       EXPECT_EQ(OldFunc, cast<AllocaInst>(OldIntrin->getAddress())->
385                          getParent()->getParent());
386       // New address must belong to the new function
387       EXPECT_EQ(NewFunc, cast<AllocaInst>(NewIntrin->getAddress())->
388                          getParent()->getParent());
389
390       // Old variable must belong to the old function
391       EXPECT_EQ(OldFunc, DISubprogram(cast<MDSubprogram>(
392                                           OldIntrin->getVariable()->getScope()))
393                              .getFunction());
394       // New variable must belong to the New function
395       EXPECT_EQ(NewFunc, DISubprogram(cast<MDSubprogram>(
396                                           NewIntrin->getVariable()->getScope()))
397                              .getFunction());
398     } else if (DbgValueInst* OldIntrin = dyn_cast<DbgValueInst>(&OldI)) {
399       DbgValueInst* NewIntrin = dyn_cast<DbgValueInst>(&NewI);
400       EXPECT_TRUE(NewIntrin);
401
402       // Old variable must belong to the old function
403       EXPECT_EQ(OldFunc, DISubprogram(cast<MDSubprogram>(
404                                           OldIntrin->getVariable()->getScope()))
405                              .getFunction());
406       // New variable must belong to the New function
407       EXPECT_EQ(NewFunc, DISubprogram(cast<MDSubprogram>(
408                                           NewIntrin->getVariable()->getScope()))
409                              .getFunction());
410     }
411
412     ++OldIter;
413     ++NewIter;
414   }
415 }
416
417 }