Nick Lewycky | 1f71578 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 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 | |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 10 | #include "llvm/Transforms/Utils/Cloning.h" |
| 11 | #include "llvm/ADT/ArrayRef.h" |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/STLExtras.h" |
Quentin Colombet | 4156c98 | 2014-04-22 02:17:11 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Argument.h" |
| 15 | #include "llvm/IR/Constant.h" |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 16 | #include "llvm/IR/DIBuilder.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DebugInfo.h" |
Joey Gouly | 8125929 | 2013-04-10 10:37:38 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Function.h" |
Joey Gouly | 8125929 | 2013-04-10 10:37:38 +0000 | [diff] [blame] | 19 | #include "llvm/IR/IRBuilder.h" |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 20 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Instructions.h" |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 22 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Module.h" |
Duncan P. N. Exon Smith | 4bd905e | 2015-03-30 21:35:14 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 26 | #include "gtest/gtest.h" |
Nick Lewycky | 1f71578 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 29 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 30 | namespace { |
Chandler Carruth | 35e6706 | 2012-06-20 08:39:27 +0000 | [diff] [blame] | 31 | |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 32 | class CloneInstruction : public ::testing::Test { |
| 33 | protected: |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 34 | void SetUp() override { V = nullptr; } |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 35 | |
| 36 | template <typename T> |
| 37 | T *clone(T *V1) { |
| 38 | Value *V2 = V1->clone(); |
Quentin Colombet | 4156c98 | 2014-04-22 02:17:11 +0000 | [diff] [blame] | 39 | Orig.insert(V1); |
| 40 | Clones.insert(V2); |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 41 | return cast<T>(V2); |
| 42 | } |
| 43 | |
Quentin Colombet | 4156c98 | 2014-04-22 02:17:11 +0000 | [diff] [blame] | 44 | void eraseClones() { |
| 45 | DeleteContainerPointers(Clones); |
| 46 | } |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 47 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 48 | void TearDown() override { |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 49 | eraseClones(); |
Quentin Colombet | 4156c98 | 2014-04-22 02:17:11 +0000 | [diff] [blame] | 50 | DeleteContainerPointers(Orig); |
| 51 | delete V; |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Quentin Colombet | 4156c98 | 2014-04-22 02:17:11 +0000 | [diff] [blame] | 54 | SmallPtrSet<Value *, 4> Orig; // Erase on exit |
| 55 | SmallPtrSet<Value *, 4> Clones; // Erase in eraseClones |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 56 | |
Nick Lewycky | 1f71578 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 57 | LLVMContext context; |
Quentin Colombet | 4156c98 | 2014-04-22 02:17:11 +0000 | [diff] [blame] | 58 | Value *V; |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | TEST_F(CloneInstruction, OverflowBits) { |
Quentin Colombet | 4156c98 | 2014-04-22 02:17:11 +0000 | [diff] [blame] | 62 | V = new Argument(Type::getInt32Ty(context)); |
Nick Lewycky | 1f71578 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 63 | |
Quentin Colombet | 4156c98 | 2014-04-22 02:17:11 +0000 | [diff] [blame] | 64 | BinaryOperator *Add = BinaryOperator::Create(Instruction::Add, V, V); |
| 65 | BinaryOperator *Sub = BinaryOperator::Create(Instruction::Sub, V, V); |
| 66 | BinaryOperator *Mul = BinaryOperator::Create(Instruction::Mul, V, V); |
Nick Lewycky | 1f71578 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 67 | |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 68 | BinaryOperator *AddClone = this->clone(Add); |
| 69 | BinaryOperator *SubClone = this->clone(Sub); |
| 70 | BinaryOperator *MulClone = this->clone(Mul); |
| 71 | |
| 72 | EXPECT_FALSE(AddClone->hasNoUnsignedWrap()); |
| 73 | EXPECT_FALSE(AddClone->hasNoSignedWrap()); |
| 74 | EXPECT_FALSE(SubClone->hasNoUnsignedWrap()); |
| 75 | EXPECT_FALSE(SubClone->hasNoSignedWrap()); |
| 76 | EXPECT_FALSE(MulClone->hasNoUnsignedWrap()); |
| 77 | EXPECT_FALSE(MulClone->hasNoSignedWrap()); |
| 78 | |
| 79 | eraseClones(); |
Nick Lewycky | 1f71578 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 80 | |
| 81 | Add->setHasNoUnsignedWrap(); |
| 82 | Sub->setHasNoUnsignedWrap(); |
| 83 | Mul->setHasNoUnsignedWrap(); |
| 84 | |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 85 | AddClone = this->clone(Add); |
| 86 | SubClone = this->clone(Sub); |
| 87 | MulClone = this->clone(Mul); |
| 88 | |
| 89 | EXPECT_TRUE(AddClone->hasNoUnsignedWrap()); |
| 90 | EXPECT_FALSE(AddClone->hasNoSignedWrap()); |
| 91 | EXPECT_TRUE(SubClone->hasNoUnsignedWrap()); |
| 92 | EXPECT_FALSE(SubClone->hasNoSignedWrap()); |
| 93 | EXPECT_TRUE(MulClone->hasNoUnsignedWrap()); |
| 94 | EXPECT_FALSE(MulClone->hasNoSignedWrap()); |
| 95 | |
| 96 | eraseClones(); |
Nick Lewycky | 1f71578 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 97 | |
| 98 | Add->setHasNoSignedWrap(); |
| 99 | Sub->setHasNoSignedWrap(); |
| 100 | Mul->setHasNoSignedWrap(); |
| 101 | |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 102 | AddClone = this->clone(Add); |
| 103 | SubClone = this->clone(Sub); |
| 104 | MulClone = this->clone(Mul); |
| 105 | |
| 106 | EXPECT_TRUE(AddClone->hasNoUnsignedWrap()); |
| 107 | EXPECT_TRUE(AddClone->hasNoSignedWrap()); |
| 108 | EXPECT_TRUE(SubClone->hasNoUnsignedWrap()); |
| 109 | EXPECT_TRUE(SubClone->hasNoSignedWrap()); |
| 110 | EXPECT_TRUE(MulClone->hasNoUnsignedWrap()); |
| 111 | EXPECT_TRUE(MulClone->hasNoSignedWrap()); |
| 112 | |
| 113 | eraseClones(); |
Nick Lewycky | 1f71578 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 114 | |
| 115 | Add->setHasNoUnsignedWrap(false); |
| 116 | Sub->setHasNoUnsignedWrap(false); |
| 117 | Mul->setHasNoUnsignedWrap(false); |
| 118 | |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 119 | AddClone = this->clone(Add); |
| 120 | SubClone = this->clone(Sub); |
| 121 | MulClone = this->clone(Mul); |
| 122 | |
| 123 | EXPECT_FALSE(AddClone->hasNoUnsignedWrap()); |
| 124 | EXPECT_TRUE(AddClone->hasNoSignedWrap()); |
| 125 | EXPECT_FALSE(SubClone->hasNoUnsignedWrap()); |
| 126 | EXPECT_TRUE(SubClone->hasNoSignedWrap()); |
| 127 | EXPECT_FALSE(MulClone->hasNoUnsignedWrap()); |
| 128 | EXPECT_TRUE(MulClone->hasNoSignedWrap()); |
Nick Lewycky | 1f71578 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 131 | TEST_F(CloneInstruction, Inbounds) { |
Quentin Colombet | 4156c98 | 2014-04-22 02:17:11 +0000 | [diff] [blame] | 132 | V = new Argument(Type::getInt32PtrTy(context)); |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 133 | |
Nick Lewycky | 1f71578 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 134 | Constant *Z = Constant::getNullValue(Type::getInt32Ty(context)); |
| 135 | std::vector<Value *> ops; |
| 136 | ops.push_back(Z); |
David Blaikie | b3a3906 | 2015-03-14 21:40:10 +0000 | [diff] [blame] | 137 | GetElementPtrInst *GEP = |
| 138 | GetElementPtrInst::Create(Type::getInt32Ty(context), V, ops); |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 139 | EXPECT_FALSE(this->clone(GEP)->isInBounds()); |
Nick Lewycky | 1f71578 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 140 | |
| 141 | GEP->setIsInBounds(); |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 142 | EXPECT_TRUE(this->clone(GEP)->isInBounds()); |
Nick Lewycky | 1f71578 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 145 | TEST_F(CloneInstruction, Exact) { |
Quentin Colombet | 4156c98 | 2014-04-22 02:17:11 +0000 | [diff] [blame] | 146 | V = new Argument(Type::getInt32Ty(context)); |
Nick Lewycky | 1f71578 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 147 | |
Quentin Colombet | 4156c98 | 2014-04-22 02:17:11 +0000 | [diff] [blame] | 148 | BinaryOperator *SDiv = BinaryOperator::Create(Instruction::SDiv, V, V); |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 149 | EXPECT_FALSE(this->clone(SDiv)->isExact()); |
Nick Lewycky | 1f71578 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 150 | |
| 151 | SDiv->setIsExact(true); |
Nick Lewycky | 84189ab | 2010-03-13 19:58:26 +0000 | [diff] [blame] | 152 | EXPECT_TRUE(this->clone(SDiv)->isExact()); |
Nick Lewycky | 1f71578 | 2009-09-27 21:39:46 +0000 | [diff] [blame] | 153 | } |
Chandler Carruth | 35e6706 | 2012-06-20 08:39:27 +0000 | [diff] [blame] | 154 | |
Joey Gouly | 8125929 | 2013-04-10 10:37:38 +0000 | [diff] [blame] | 155 | TEST_F(CloneInstruction, Attributes) { |
| 156 | Type *ArgTy1[] = { Type::getInt32PtrTy(context) }; |
| 157 | FunctionType *FT1 = FunctionType::get(Type::getVoidTy(context), ArgTy1, false); |
| 158 | |
| 159 | Function *F1 = Function::Create(FT1, Function::ExternalLinkage); |
| 160 | BasicBlock *BB = BasicBlock::Create(context, "", F1); |
| 161 | IRBuilder<> Builder(BB); |
| 162 | Builder.CreateRetVoid(); |
| 163 | |
| 164 | Function *F2 = Function::Create(FT1, Function::ExternalLinkage); |
| 165 | |
| 166 | Attribute::AttrKind AK[] = { Attribute::NoCapture }; |
| 167 | AttributeSet AS = AttributeSet::get(context, 0, AK); |
Duncan P. N. Exon Smith | c8925b1 | 2015-10-20 18:30:20 +0000 | [diff] [blame] | 168 | Argument *A = &*F1->arg_begin(); |
Joey Gouly | 8125929 | 2013-04-10 10:37:38 +0000 | [diff] [blame] | 169 | A->addAttr(AS); |
| 170 | |
| 171 | SmallVector<ReturnInst*, 4> Returns; |
| 172 | ValueToValueMapTy VMap; |
| 173 | VMap[A] = UndefValue::get(A->getType()); |
| 174 | |
| 175 | CloneFunctionInto(F2, F1, VMap, false, Returns); |
| 176 | EXPECT_FALSE(F2->arg_begin()->hasNoCaptureAttr()); |
Joey Gouly | 51f6fb9 | 2013-04-10 23:21:26 +0000 | [diff] [blame] | 177 | |
| 178 | delete F1; |
| 179 | delete F2; |
Joey Gouly | 8125929 | 2013-04-10 10:37:38 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Reid Kleckner | 23798a9 | 2014-03-26 22:26:35 +0000 | [diff] [blame] | 182 | TEST_F(CloneInstruction, CallingConvention) { |
| 183 | Type *ArgTy1[] = { Type::getInt32PtrTy(context) }; |
| 184 | FunctionType *FT1 = FunctionType::get(Type::getVoidTy(context), ArgTy1, false); |
| 185 | |
| 186 | Function *F1 = Function::Create(FT1, Function::ExternalLinkage); |
| 187 | F1->setCallingConv(CallingConv::Cold); |
| 188 | BasicBlock *BB = BasicBlock::Create(context, "", F1); |
| 189 | IRBuilder<> Builder(BB); |
| 190 | Builder.CreateRetVoid(); |
| 191 | |
| 192 | Function *F2 = Function::Create(FT1, Function::ExternalLinkage); |
| 193 | |
| 194 | SmallVector<ReturnInst*, 4> Returns; |
| 195 | ValueToValueMapTy VMap; |
Duncan P. N. Exon Smith | c8925b1 | 2015-10-20 18:30:20 +0000 | [diff] [blame] | 196 | VMap[&*F1->arg_begin()] = &*F2->arg_begin(); |
Reid Kleckner | 23798a9 | 2014-03-26 22:26:35 +0000 | [diff] [blame] | 197 | |
| 198 | CloneFunctionInto(F2, F1, VMap, false, Returns); |
| 199 | EXPECT_EQ(CallingConv::Cold, F2->getCallingConv()); |
| 200 | |
| 201 | delete F1; |
| 202 | delete F2; |
| 203 | } |
| 204 | |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 205 | class CloneFunc : public ::testing::Test { |
| 206 | protected: |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 207 | void SetUp() override { |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 208 | SetupModule(); |
| 209 | CreateOldFunc(); |
| 210 | CreateNewFunc(); |
| 211 | SetupFinder(); |
| 212 | } |
| 213 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 214 | void TearDown() override { delete Finder; } |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 215 | |
| 216 | void SetupModule() { |
| 217 | M = new Module("", C); |
| 218 | } |
| 219 | |
| 220 | void CreateOldFunc() { |
| 221 | FunctionType* FuncType = FunctionType::get(Type::getVoidTy(C), false); |
| 222 | OldFunc = Function::Create(FuncType, GlobalValue::PrivateLinkage, "f", M); |
| 223 | CreateOldFunctionBodyAndDI(); |
| 224 | } |
| 225 | |
| 226 | void CreateOldFunctionBodyAndDI() { |
| 227 | DIBuilder DBuilder(*M); |
| 228 | IRBuilder<> IBuilder(C); |
| 229 | |
| 230 | // Function DI |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 231 | auto *File = DBuilder.createFile("filename.c", "/file/dir/"); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 232 | DITypeRefArray ParamTypes = DBuilder.getOrCreateTypeArray(None); |
| 233 | DISubroutineType *FuncType = |
Eric Christopher | bdafb3c | 2015-10-15 06:56:10 +0000 | [diff] [blame] | 234 | DBuilder.createSubroutineType(ParamTypes); |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 235 | auto *CU = |
| 236 | DBuilder.createCompileUnit(dwarf::DW_LANG_C99, "filename.c", |
| 237 | "/file/dir", "CloneFunc", false, "", 0); |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 238 | |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 239 | auto *Subprogram = DBuilder.createFunction( |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 240 | CU, "f", "f", File, 4, FuncType, true, true, 3, 0, false); |
| 241 | OldFunc->setSubprogram(Subprogram); |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 242 | |
| 243 | // Function body |
| 244 | BasicBlock* Entry = BasicBlock::Create(C, "", OldFunc); |
| 245 | IBuilder.SetInsertPoint(Entry); |
| 246 | DebugLoc Loc = DebugLoc::get(3, 2, Subprogram); |
| 247 | IBuilder.SetCurrentDebugLocation(Loc); |
| 248 | AllocaInst* Alloca = IBuilder.CreateAlloca(IntegerType::getInt32Ty(C)); |
| 249 | IBuilder.SetCurrentDebugLocation(DebugLoc::get(4, 2, Subprogram)); |
| 250 | Value* AllocaContent = IBuilder.getInt32(1); |
| 251 | Instruction* Store = IBuilder.CreateStore(AllocaContent, Alloca); |
| 252 | IBuilder.SetCurrentDebugLocation(DebugLoc::get(5, 2, Subprogram)); |
| 253 | Instruction* Terminator = IBuilder.CreateRetVoid(); |
| 254 | |
| 255 | // Create a local variable around the alloca |
Duncan P. N. Exon Smith | 9928a90 | 2015-04-20 18:52:06 +0000 | [diff] [blame] | 256 | auto *IntType = |
| 257 | DBuilder.createBasicType("int", 32, 0, dwarf::DW_ATE_signed); |
Duncan P. N. Exon Smith | 60635e3 | 2015-04-21 18:44:06 +0000 | [diff] [blame] | 258 | auto *E = DBuilder.createExpression(); |
Duncan P. N. Exon Smith | 1e40dc4 | 2015-07-31 17:55:53 +0000 | [diff] [blame] | 259 | auto *Variable = |
| 260 | DBuilder.createAutoVariable(Subprogram, "x", File, 5, IntType, true); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 261 | auto *DL = DILocation::get(Subprogram->getContext(), 5, 0, Subprogram); |
Duncan P. N. Exon Smith | cd1aecf | 2015-04-15 21:18:07 +0000 | [diff] [blame] | 262 | DBuilder.insertDeclare(Alloca, Variable, E, DL, Store); |
| 263 | DBuilder.insertDbgValueIntrinsic(AllocaContent, 0, Variable, E, DL, |
| 264 | Terminator); |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 265 | // Finalize the debug info |
| 266 | DBuilder.finalize(); |
| 267 | |
| 268 | |
| 269 | // Create another, empty, compile unit |
| 270 | DIBuilder DBuilder2(*M); |
| 271 | DBuilder2.createCompileUnit(dwarf::DW_LANG_C99, |
| 272 | "extra.c", "/file/dir", "CloneFunc", false, "", 0); |
| 273 | DBuilder2.finalize(); |
| 274 | } |
| 275 | |
| 276 | void CreateNewFunc() { |
| 277 | ValueToValueMapTy VMap; |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 278 | NewFunc = CloneFunction(OldFunc, VMap, true, nullptr); |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 279 | M->getFunctionList().push_back(NewFunc); |
| 280 | } |
| 281 | |
| 282 | void SetupFinder() { |
| 283 | Finder = new DebugInfoFinder(); |
| 284 | Finder->processModule(*M); |
| 285 | } |
| 286 | |
| 287 | LLVMContext C; |
| 288 | Function* OldFunc; |
| 289 | Function* NewFunc; |
| 290 | Module* M; |
| 291 | DebugInfoFinder* Finder; |
| 292 | }; |
| 293 | |
| 294 | // Test that a new, distinct function was created. |
| 295 | TEST_F(CloneFunc, NewFunctionCreated) { |
| 296 | EXPECT_NE(OldFunc, NewFunc); |
| 297 | } |
| 298 | |
| 299 | // Test that a new subprogram entry was added and is pointing to the new |
| 300 | // function, while the original subprogram still points to the old one. |
| 301 | TEST_F(CloneFunc, Subprogram) { |
Duncan P. N. Exon Smith | 4bd905e | 2015-03-30 21:35:14 +0000 | [diff] [blame] | 302 | EXPECT_FALSE(verifyModule(*M)); |
| 303 | |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 304 | unsigned SubprogramCount = Finder->subprogram_count(); |
Justin Bogner | f404b93 | 2014-03-12 17:00:52 +0000 | [diff] [blame] | 305 | EXPECT_EQ(2U, SubprogramCount); |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 306 | |
Alon Mishne | ad31215 | 2014-03-18 09:41:07 +0000 | [diff] [blame] | 307 | auto Iter = Finder->subprograms().begin(); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 308 | auto *Sub1 = cast<DISubprogram>(*Iter); |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 309 | Iter++; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 310 | auto *Sub2 = cast<DISubprogram>(*Iter); |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 311 | |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 312 | EXPECT_TRUE( |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 313 | (Sub1 == OldFunc->getSubprogram() && Sub2 == NewFunc->getSubprogram()) || |
| 314 | (Sub1 == NewFunc->getSubprogram() && Sub2 == OldFunc->getSubprogram())); |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // Test that the new subprogram entry was not added to the CU which doesn't |
| 318 | // contain the old subprogram entry. |
| 319 | TEST_F(CloneFunc, SubprogramInRightCU) { |
Duncan P. N. Exon Smith | 4bd905e | 2015-03-30 21:35:14 +0000 | [diff] [blame] | 320 | EXPECT_FALSE(verifyModule(*M)); |
| 321 | |
Justin Bogner | f404b93 | 2014-03-12 17:00:52 +0000 | [diff] [blame] | 322 | EXPECT_EQ(2U, Finder->compile_unit_count()); |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 323 | |
Alon Mishne | ad31215 | 2014-03-18 09:41:07 +0000 | [diff] [blame] | 324 | auto Iter = Finder->compile_units().begin(); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 325 | auto *CU1 = cast<DICompileUnit>(*Iter); |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 326 | Iter++; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 327 | auto *CU2 = cast<DICompileUnit>(*Iter); |
Duncan P. N. Exon Smith | 35ef22c | 2015-04-15 23:19:27 +0000 | [diff] [blame] | 328 | EXPECT_TRUE(CU1->getSubprograms().size() == 0 || |
| 329 | CU2->getSubprograms().size() == 0); |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // Test that instructions in the old function still belong to it in the |
| 333 | // metadata, while instruction in the new function belong to the new one. |
| 334 | TEST_F(CloneFunc, InstructionOwnership) { |
Duncan P. N. Exon Smith | 4bd905e | 2015-03-30 21:35:14 +0000 | [diff] [blame] | 335 | EXPECT_FALSE(verifyModule(*M)); |
| 336 | |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 337 | inst_iterator OldIter = inst_begin(OldFunc); |
| 338 | inst_iterator OldEnd = inst_end(OldFunc); |
| 339 | inst_iterator NewIter = inst_begin(NewFunc); |
| 340 | inst_iterator NewEnd = inst_end(NewFunc); |
| 341 | while (OldIter != OldEnd && NewIter != NewEnd) { |
| 342 | Instruction& OldI = *OldIter; |
| 343 | Instruction& NewI = *NewIter; |
| 344 | EXPECT_NE(&OldI, &NewI); |
| 345 | |
| 346 | EXPECT_EQ(OldI.hasMetadata(), NewI.hasMetadata()); |
| 347 | if (OldI.hasMetadata()) { |
| 348 | const DebugLoc& OldDL = OldI.getDebugLoc(); |
| 349 | const DebugLoc& NewDL = NewI.getDebugLoc(); |
| 350 | |
| 351 | // Verify that the debug location data is the same |
| 352 | EXPECT_EQ(OldDL.getLine(), NewDL.getLine()); |
| 353 | EXPECT_EQ(OldDL.getCol(), NewDL.getCol()); |
Duncan P. N. Exon Smith | 4fff3ec | 2015-03-30 21:05:29 +0000 | [diff] [blame] | 354 | |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 355 | // But that they belong to different functions |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 356 | auto *OldSubprogram = cast<DISubprogram>(OldDL.getScope()); |
| 357 | auto *NewSubprogram = cast<DISubprogram>(NewDL.getScope()); |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 358 | EXPECT_EQ(OldFunc->getSubprogram(), OldSubprogram); |
| 359 | EXPECT_EQ(NewFunc->getSubprogram(), NewSubprogram); |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | ++OldIter; |
| 363 | ++NewIter; |
| 364 | } |
| 365 | EXPECT_EQ(OldEnd, OldIter); |
| 366 | EXPECT_EQ(NewEnd, NewIter); |
| 367 | } |
| 368 | |
| 369 | // Test that the arguments for debug intrinsics in the new function were |
| 370 | // properly cloned |
| 371 | TEST_F(CloneFunc, DebugIntrinsics) { |
Duncan P. N. Exon Smith | 4bd905e | 2015-03-30 21:35:14 +0000 | [diff] [blame] | 372 | EXPECT_FALSE(verifyModule(*M)); |
| 373 | |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 374 | inst_iterator OldIter = inst_begin(OldFunc); |
| 375 | inst_iterator OldEnd = inst_end(OldFunc); |
| 376 | inst_iterator NewIter = inst_begin(NewFunc); |
| 377 | inst_iterator NewEnd = inst_end(NewFunc); |
| 378 | while (OldIter != OldEnd && NewIter != NewEnd) { |
| 379 | Instruction& OldI = *OldIter; |
| 380 | Instruction& NewI = *NewIter; |
| 381 | if (DbgDeclareInst* OldIntrin = dyn_cast<DbgDeclareInst>(&OldI)) { |
| 382 | DbgDeclareInst* NewIntrin = dyn_cast<DbgDeclareInst>(&NewI); |
| 383 | EXPECT_TRUE(NewIntrin); |
| 384 | |
| 385 | // Old address must belong to the old function |
| 386 | EXPECT_EQ(OldFunc, cast<AllocaInst>(OldIntrin->getAddress())-> |
| 387 | getParent()->getParent()); |
| 388 | // New address must belong to the new function |
| 389 | EXPECT_EQ(NewFunc, cast<AllocaInst>(NewIntrin->getAddress())-> |
| 390 | getParent()->getParent()); |
| 391 | |
| 392 | // Old variable must belong to the old function |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 393 | EXPECT_EQ(OldFunc->getSubprogram(), |
| 394 | cast<DISubprogram>(OldIntrin->getVariable()->getScope())); |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 395 | // New variable must belong to the New function |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 396 | EXPECT_EQ(NewFunc->getSubprogram(), |
| 397 | cast<DISubprogram>(NewIntrin->getVariable()->getScope())); |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 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 |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 403 | EXPECT_EQ(OldFunc->getSubprogram(), |
| 404 | cast<DISubprogram>(OldIntrin->getVariable()->getScope())); |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 405 | // New variable must belong to the New function |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 406 | EXPECT_EQ(NewFunc->getSubprogram(), |
| 407 | cast<DISubprogram>(NewIntrin->getVariable()->getScope())); |
Alon Mishne | 07d949f | 2014-03-12 14:42:51 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | ++OldIter; |
| 411 | ++NewIter; |
| 412 | } |
| 413 | } |
| 414 | |
David Majnemer | cda8688 | 2015-06-30 22:14:01 +0000 | [diff] [blame] | 415 | class CloneModule : public ::testing::Test { |
| 416 | protected: |
| 417 | void SetUp() override { |
| 418 | SetupModule(); |
| 419 | CreateOldModule(); |
| 420 | CreateNewModule(); |
| 421 | } |
| 422 | |
| 423 | void SetupModule() { OldM = new Module("", C); } |
| 424 | |
| 425 | void CreateOldModule() { |
Keno Fischer | 7c7c3e3 | 2016-02-13 02:04:29 +0000 | [diff] [blame] | 426 | DIBuilder DBuilder(*OldM); |
David Majnemer | cda8688 | 2015-06-30 22:14:01 +0000 | [diff] [blame] | 427 | IRBuilder<> IBuilder(C); |
| 428 | |
| 429 | auto *FuncType = FunctionType::get(Type::getVoidTy(C), false); |
| 430 | auto *PersFn = Function::Create(FuncType, GlobalValue::ExternalLinkage, |
| 431 | "persfn", OldM); |
| 432 | auto *F = |
| 433 | Function::Create(FuncType, GlobalValue::PrivateLinkage, "f", OldM); |
| 434 | F->setPersonalityFn(PersFn); |
Keno Fischer | 7c7c3e3 | 2016-02-13 02:04:29 +0000 | [diff] [blame] | 435 | |
| 436 | // Create debug info |
| 437 | auto *File = DBuilder.createFile("filename.c", "/file/dir/"); |
| 438 | DITypeRefArray ParamTypes = DBuilder.getOrCreateTypeArray(None); |
| 439 | DISubroutineType *DFuncType = DBuilder.createSubroutineType(ParamTypes); |
| 440 | auto *CU = |
| 441 | DBuilder.createCompileUnit(dwarf::DW_LANG_C99, "filename.c", |
| 442 | "/file/dir", "CloneModule", false, "", 0); |
| 443 | // Function DI |
| 444 | auto *Subprogram = DBuilder.createFunction(CU, "f", "f", File, 4, DFuncType, |
| 445 | true, true, 3, 0, false); |
| 446 | F->setSubprogram(Subprogram); |
| 447 | |
David Majnemer | cda8688 | 2015-06-30 22:14:01 +0000 | [diff] [blame] | 448 | auto *Entry = BasicBlock::Create(C, "", F); |
| 449 | IBuilder.SetInsertPoint(Entry); |
| 450 | IBuilder.CreateRetVoid(); |
Keno Fischer | 7c7c3e3 | 2016-02-13 02:04:29 +0000 | [diff] [blame] | 451 | |
| 452 | // Finalize the debug info |
| 453 | DBuilder.finalize(); |
David Majnemer | cda8688 | 2015-06-30 22:14:01 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 456 | void CreateNewModule() { NewM = llvm::CloneModule(OldM).release(); } |
David Majnemer | cda8688 | 2015-06-30 22:14:01 +0000 | [diff] [blame] | 457 | |
| 458 | LLVMContext C; |
| 459 | Module *OldM; |
| 460 | Module *NewM; |
| 461 | }; |
| 462 | |
| 463 | TEST_F(CloneModule, Verify) { |
| 464 | EXPECT_FALSE(verifyModule(*NewM)); |
| 465 | } |
| 466 | |
Keno Fischer | 7c7c3e3 | 2016-02-13 02:04:29 +0000 | [diff] [blame] | 467 | TEST_F(CloneModule, Subprogram) { |
| 468 | Function *NewF = NewM->getFunction("f"); |
| 469 | DISubprogram *SP = NewF->getSubprogram(); |
| 470 | EXPECT_TRUE(SP != nullptr); |
| 471 | EXPECT_EQ(SP->getName(), "f"); |
| 472 | EXPECT_EQ(SP->getFile()->getFilename(), "filename.c"); |
| 473 | EXPECT_EQ(SP->getLine(), (unsigned)4); |
| 474 | } |
Chandler Carruth | 35e6706 | 2012-06-20 08:39:27 +0000 | [diff] [blame] | 475 | } |