Skip to content

Commit 12293ce

Browse files
committed
Add a unit test IsNoAliasForInstructions
1 parent ee15a70 commit 12293ce

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

llvm/unittests/Analysis/AliasAnalysisTest.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,46 @@ TEST_F(AliasAnalysisTest, PartialAliasOffsetSign) {
364364
EXPECT_EQ(AR, AliasResult::PartialAlias);
365365
EXPECT_EQ(-1, AR.getOffset());
366366
}
367+
368+
TEST_F(AliasAnalysisTest, IsNoAliasForInstructions) {
369+
// Create global variable
370+
auto *GlobalVar = new GlobalVariable(M, Type::getInt32Ty(C), false,
371+
GlobalValue::ExternalLinkage,
372+
ConstantInt::get(Type::getInt32Ty(C), 0),
373+
"g");
374+
GlobalVar->setAlignment(Align(4));
375+
376+
// Create function declarations
377+
FunctionType *VoidFnTy = FunctionType::get(Type::getVoidTy(C), false);
378+
Function::Create(VoidFnTy, Function::ExternalLinkage, "foo", M);
379+
Function::Create(VoidFnTy, Function::ExternalLinkage, "bar", M);
380+
381+
// Create test function
382+
FunctionType *TestFnTy = FunctionType::get(Type::getInt32Ty(C), false);
383+
Function *TestFn = Function::Create(TestFnTy, Function::ExternalLinkage, "test", M);
384+
385+
// Create basic block
386+
BasicBlock *BB = BasicBlock::Create(C, "entry", TestFn);
387+
388+
// Create instructions
389+
auto *Alloca = new AllocaInst(Type::getInt32Ty(C), 0, "p", BB);
390+
auto *Store1 = new StoreInst(ConstantInt::get(Type::getInt32Ty(C), 42), Alloca, BB);
391+
auto *Store2 = new StoreInst(ConstantInt::get(Type::getInt32Ty(C), 100), GlobalVar, BB);
392+
auto *Call1 = CallInst::Create(M.getFunction("foo"), {}, BB);
393+
auto *Call2 = CallInst::Create(M.getFunction("bar"), {}, BB);
394+
auto *Load = new LoadInst(Type::getInt32Ty(C), Alloca, "val", BB);
395+
ReturnInst::Create(C, Load, BB);
396+
397+
auto &AA = getAAResults(*TestFn);
398+
EXPECT_EQ(true, AA.isNoAlias(Store1, Store2));
399+
EXPECT_EQ(false, AA.isNoAlias(Store1, Call1));
400+
EXPECT_EQ(false, AA.isNoAlias(Store1, Load));
401+
EXPECT_EQ(false, AA.isNoAlias(Store2, Call1));
402+
EXPECT_EQ(false, AA.isNoAlias(Call1, Call2));
403+
EXPECT_EQ(true, AA.isNoAlias(Store2, Load));
404+
405+
}
406+
367407
class AAPassInfraTest : public testing::Test {
368408
protected:
369409
LLVMContext C;

0 commit comments

Comments
 (0)