#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/Constant.h"
-#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DIBuilder.h"
+#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
-#include "llvm/IR/IRBuilder.h"
-#include "llvm/IR/Module.h"
#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/Verifier.h"
#include "gtest/gtest.h"
using namespace llvm;
class CloneInstruction : public ::testing::Test {
protected:
- virtual void SetUp() {
- V = NULL;
- }
+ void SetUp() override { V = nullptr; }
template <typename T>
T *clone(T *V1) {
DeleteContainerPointers(Clones);
}
- virtual void TearDown() {
+ void TearDown() override {
eraseClones();
DeleteContainerPointers(Orig);
delete V;
Constant *Z = Constant::getNullValue(Type::getInt32Ty(context));
std::vector<Value *> ops;
ops.push_back(Z);
- GetElementPtrInst *GEP = GetElementPtrInst::Create(V, ops);
+ GetElementPtrInst *GEP =
+ GetElementPtrInst::Create(Type::getInt32Ty(context), V, ops);
EXPECT_FALSE(this->clone(GEP)->isInBounds());
GEP->setIsInBounds();
delete F2;
}
+TEST_F(CloneInstruction, CallingConvention) {
+ Type *ArgTy1[] = { Type::getInt32PtrTy(context) };
+ FunctionType *FT1 = FunctionType::get(Type::getVoidTy(context), ArgTy1, false);
+
+ Function *F1 = Function::Create(FT1, Function::ExternalLinkage);
+ F1->setCallingConv(CallingConv::Cold);
+ BasicBlock *BB = BasicBlock::Create(context, "", F1);
+ IRBuilder<> Builder(BB);
+ Builder.CreateRetVoid();
+
+ Function *F2 = Function::Create(FT1, Function::ExternalLinkage);
+
+ SmallVector<ReturnInst*, 4> Returns;
+ ValueToValueMapTy VMap;
+ VMap[F1->arg_begin()] = F2->arg_begin();
+
+ CloneFunctionInto(F2, F1, VMap, false, Returns);
+ EXPECT_EQ(CallingConv::Cold, F2->getCallingConv());
+
+ delete F1;
+ delete F2;
+}
+
class CloneFunc : public ::testing::Test {
protected:
- virtual void SetUp() {
+ void SetUp() override {
SetupModule();
CreateOldFunc();
CreateNewFunc();
SetupFinder();
}
- virtual void TearDown() {
- delete Finder;
- }
+ void TearDown() override { delete Finder; }
void SetupModule() {
M = new Module("", C);
IRBuilder<> IBuilder(C);
// Function DI
- DIFile File = DBuilder.createFile("filename.c", "/file/dir/");
- DIArray ParamTypes = DBuilder.getOrCreateArray(ArrayRef<Value*>());
- DICompositeType FuncType = DBuilder.createSubroutineType(File, ParamTypes);
- DICompileUnit CU = DBuilder.createCompileUnit(dwarf::DW_LANG_C99,
- "filename.c", "/file/dir", "CloneFunc", false, "", 0);
+ auto *File = DBuilder.createFile("filename.c", "/file/dir/");
+ DITypeRefArray ParamTypes = DBuilder.getOrCreateTypeArray(None);
+ DISubroutineType *FuncType =
+ DBuilder.createSubroutineType(ParamTypes);
+ auto *CU =
+ DBuilder.createCompileUnit(dwarf::DW_LANG_C99, "filename.c",
+ "/file/dir", "CloneFunc", false, "", 0);
- DISubprogram Subprogram = DBuilder.createFunction(CU, "f", "f", File, 4,
- FuncType, true, true, 3, 0, false, OldFunc);
+ auto *Subprogram = DBuilder.createFunction(
+ CU, "f", "f", File, 4, FuncType, true, true, 3, 0, false, OldFunc);
// Function body
BasicBlock* Entry = BasicBlock::Create(C, "", OldFunc);
Instruction* Terminator = IBuilder.CreateRetVoid();
// Create a local variable around the alloca
- DIType IntType = DBuilder.createBasicType("int", 32, 0,
- dwarf::DW_ATE_signed);
- DIVariable Variable = DBuilder.createLocalVariable(
- dwarf::DW_TAG_auto_variable, Subprogram, "x", File, 5, IntType, true);
- DBuilder.insertDeclare(Alloca, Variable, Store);
- DBuilder.insertDbgValueIntrinsic(AllocaContent, 0, Variable, Terminator);
+ auto *IntType =
+ DBuilder.createBasicType("int", 32, 0, dwarf::DW_ATE_signed);
+ auto *E = DBuilder.createExpression();
+ auto *Variable =
+ DBuilder.createAutoVariable(Subprogram, "x", File, 5, IntType, true);
+ auto *DL = DILocation::get(Subprogram->getContext(), 5, 0, Subprogram);
+ DBuilder.insertDeclare(Alloca, Variable, E, DL, Store);
+ DBuilder.insertDbgValueIntrinsic(AllocaContent, 0, Variable, E, DL,
+ Terminator);
// Finalize the debug info
DBuilder.finalize();
void CreateNewFunc() {
ValueToValueMapTy VMap;
- NewFunc = CloneFunction(OldFunc, VMap, true, NULL);
+ NewFunc = CloneFunction(OldFunc, VMap, true, nullptr);
M->getFunctionList().push_back(NewFunc);
}
// Test that a new subprogram entry was added and is pointing to the new
// function, while the original subprogram still points to the old one.
TEST_F(CloneFunc, Subprogram) {
+ EXPECT_FALSE(verifyModule(*M));
+
unsigned SubprogramCount = Finder->subprogram_count();
- EXPECT_EQ(2, SubprogramCount);
+ EXPECT_EQ(2U, SubprogramCount);
- DebugInfoFinder::iterator Iter = Finder->subprogram_begin();
- DISubprogram Sub1(*Iter);
- EXPECT_TRUE(Sub1.Verify());
+ auto Iter = Finder->subprograms().begin();
+ auto *Sub1 = cast<DISubprogram>(*Iter);
Iter++;
- DISubprogram Sub2(*Iter);
- EXPECT_TRUE(Sub2.Verify());
+ auto *Sub2 = cast<DISubprogram>(*Iter);
- EXPECT_TRUE((Sub1.getFunction() == OldFunc && Sub2.getFunction() == NewFunc)
- || (Sub1.getFunction() == NewFunc && Sub2.getFunction() == OldFunc));
+ EXPECT_TRUE(
+ (Sub1->getFunction() == OldFunc && Sub2->getFunction() == NewFunc) ||
+ (Sub1->getFunction() == NewFunc && Sub2->getFunction() == OldFunc));
}
// Test that the new subprogram entry was not added to the CU which doesn't
// contain the old subprogram entry.
TEST_F(CloneFunc, SubprogramInRightCU) {
- EXPECT_EQ(2, Finder->compile_unit_count());
+ EXPECT_FALSE(verifyModule(*M));
- DebugInfoFinder::iterator Iter = Finder->compile_unit_begin();
- DICompileUnit CU1(*Iter);
- EXPECT_TRUE(CU1.Verify());
+ EXPECT_EQ(2U, Finder->compile_unit_count());
+
+ auto Iter = Finder->compile_units().begin();
+ auto *CU1 = cast<DICompileUnit>(*Iter);
Iter++;
- DICompileUnit CU2(*Iter);
- EXPECT_TRUE(CU2.Verify());
- EXPECT_TRUE(CU1.getSubprograms().getNumElements() == 0
- || CU2.getSubprograms().getNumElements() == 0);
+ auto *CU2 = cast<DICompileUnit>(*Iter);
+ EXPECT_TRUE(CU1->getSubprograms().size() == 0 ||
+ CU2->getSubprograms().size() == 0);
}
// Test that instructions in the old function still belong to it in the
// metadata, while instruction in the new function belong to the new one.
TEST_F(CloneFunc, InstructionOwnership) {
+ EXPECT_FALSE(verifyModule(*M));
+
inst_iterator OldIter = inst_begin(OldFunc);
inst_iterator OldEnd = inst_end(OldFunc);
inst_iterator NewIter = inst_begin(NewFunc);
// Verify that the debug location data is the same
EXPECT_EQ(OldDL.getLine(), NewDL.getLine());
EXPECT_EQ(OldDL.getCol(), NewDL.getCol());
-
+
// But that they belong to different functions
- DISubprogram OldSubprogram(OldDL.getScope(C));
- DISubprogram NewSubprogram(NewDL.getScope(C));
- EXPECT_TRUE(OldSubprogram.Verify());
- EXPECT_TRUE(NewSubprogram.Verify());
- EXPECT_EQ(OldFunc, OldSubprogram.getFunction());
- EXPECT_EQ(NewFunc, NewSubprogram.getFunction());
+ auto *OldSubprogram = cast<DISubprogram>(OldDL.getScope());
+ auto *NewSubprogram = cast<DISubprogram>(NewDL.getScope());
+ EXPECT_EQ(OldFunc, OldSubprogram->getFunction());
+ EXPECT_EQ(NewFunc, NewSubprogram->getFunction());
}
++OldIter;
// Test that the arguments for debug intrinsics in the new function were
// properly cloned
TEST_F(CloneFunc, DebugIntrinsics) {
+ EXPECT_FALSE(verifyModule(*M));
+
inst_iterator OldIter = inst_begin(OldFunc);
inst_iterator OldEnd = inst_end(OldFunc);
inst_iterator NewIter = inst_begin(NewFunc);
getParent()->getParent());
// Old variable must belong to the old function
- EXPECT_EQ(OldFunc, DISubprogram(DIVariable(OldIntrin->getVariable())
- .getContext()).getFunction());
+ EXPECT_EQ(OldFunc,
+ cast<DISubprogram>(OldIntrin->getVariable()->getScope())
+ ->getFunction());
// New variable must belong to the New function
- EXPECT_EQ(NewFunc, DISubprogram(DIVariable(NewIntrin->getVariable())
- .getContext()).getFunction());
+ EXPECT_EQ(NewFunc,
+ cast<DISubprogram>(NewIntrin->getVariable()->getScope())
+ ->getFunction());
} else if (DbgValueInst* OldIntrin = dyn_cast<DbgValueInst>(&OldI)) {
DbgValueInst* NewIntrin = dyn_cast<DbgValueInst>(&NewI);
EXPECT_TRUE(NewIntrin);
// Old variable must belong to the old function
- EXPECT_EQ(OldFunc, DISubprogram(DIVariable(OldIntrin->getVariable())
- .getContext()).getFunction());
+ EXPECT_EQ(OldFunc,
+ cast<DISubprogram>(OldIntrin->getVariable()->getScope())
+ ->getFunction());
// New variable must belong to the New function
- EXPECT_EQ(NewFunc, DISubprogram(DIVariable(NewIntrin->getVariable())
- .getContext()).getFunction());
+ EXPECT_EQ(NewFunc,
+ cast<DISubprogram>(NewIntrin->getVariable()->getScope())
+ ->getFunction());
}
++OldIter;
}
}
+class CloneModule : public ::testing::Test {
+protected:
+ void SetUp() override {
+ SetupModule();
+ CreateOldModule();
+ CreateNewModule();
+ }
+
+ void SetupModule() { OldM = new Module("", C); }
+
+ void CreateOldModule() {
+ IRBuilder<> IBuilder(C);
+
+ auto *FuncType = FunctionType::get(Type::getVoidTy(C), false);
+ auto *PersFn = Function::Create(FuncType, GlobalValue::ExternalLinkage,
+ "persfn", OldM);
+ auto *F =
+ Function::Create(FuncType, GlobalValue::PrivateLinkage, "f", OldM);
+ F->setPersonalityFn(PersFn);
+ auto *Entry = BasicBlock::Create(C, "", F);
+ IBuilder.SetInsertPoint(Entry);
+ IBuilder.CreateRetVoid();
+ }
+
+ void CreateNewModule() { NewM = llvm::CloneModule(OldM); }
+
+ LLVMContext C;
+ Module *OldM;
+ Module *NewM;
+};
+
+TEST_F(CloneModule, Verify) {
+ EXPECT_FALSE(verifyModule(*NewM));
+}
+
}