1 //===- LoadsTest.cpp - local load analysis unit tests ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Analysis/Loads.h"
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/IR/Instructions.h"
12 #include "llvm/IR/LLVMContext.h"
13 #include "llvm/IR/Module.h"
14 #include "llvm/Support/SourceMgr.h"
15 #include "gtest/gtest.h"
16 
17 using namespace llvm;
18 
19 static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
20   SMDiagnostic Err;
21   std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
22   if (!Mod)
23     Err.print("AnalysisTests", errs());
24   return Mod;
25 }
26 
27 TEST(LoadsTest, FindAvailableLoadedValueSameBasePtrConstantOffsetsNullAA) {
28   LLVMContext C;
29   std::unique_ptr<Module> M = parseIR(C,
30                                       R"IR(
31 %class = type <{ i32, i32 }>
32 
33 define i32 @f() {
34 entry:
35   %o = alloca %class
36   %f1 = getelementptr inbounds %class, %class* %o, i32 0, i32 0
37   store i32 42, i32* %f1
38   %f2 = getelementptr inbounds %class, %class* %o, i32 0, i32 1
39   store i32 43, i32* %f2
40   %v = load i32, i32* %f1
41   ret i32 %v
42 }
43 )IR");
44   auto *GV = M->getNamedValue("f");
45   ASSERT_TRUE(GV);
46   auto *F = dyn_cast<Function>(GV);
47   ASSERT_TRUE(F);
48   Instruction *Inst = &F->front().front();
49   auto *AI = dyn_cast<AllocaInst>(Inst);
50   ASSERT_TRUE(AI);
51   Inst = &*++F->front().rbegin();
52   auto *LI = dyn_cast<LoadInst>(Inst);
53   ASSERT_TRUE(LI);
54   BasicBlock::iterator BBI(LI);
55   Value *Loaded = FindAvailableLoadedValue(
56       LI, LI->getParent(), BBI, 0, nullptr, nullptr);
57   ASSERT_TRUE(Loaded);
58   auto *CI = dyn_cast<ConstantInt>(Loaded);
59   ASSERT_TRUE(CI);
60   ASSERT_TRUE(CI->equalsInt(42));
61 }
62