Cloning.cpp 14 KB

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