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 
parseIR(LLVMContext & C,const char * IR)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 
TEST(LoadsTest,FindAvailableLoadedValueSameBasePtrConstantOffsetsNullAA)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 
TEST(LoadsTest,CanReplacePointersIfEqual)63 TEST(LoadsTest, CanReplacePointersIfEqual) {
64   LLVMContext C;
65   std::unique_ptr<Module> M = parseIR(C,
66                                       R"IR(
67 @y = common global [1 x i32] zeroinitializer, align 4
68 @x = common global [1 x i32] zeroinitializer, align 4
69 
70 declare void @use(i32*)
71 
72 define void @f(i32* %p) {
73   call void @use(i32* getelementptr inbounds ([1 x i32], [1 x i32]* @y, i64 0, i64 0))
74   call void @use(i32* getelementptr inbounds (i32, i32* getelementptr inbounds ([1 x i32], [1 x i32]* @x, i64 0, i64 0), i64 1))
75   ret void
76 }
77 )IR");
78   const auto &DL = M->getDataLayout();
79   auto *GV = M->getNamedValue("f");
80   ASSERT_TRUE(GV);
81   auto *F = dyn_cast<Function>(GV);
82   ASSERT_TRUE(F);
83 
84   // NOTE: the implementation of canReplacePointersIfEqual is incomplete.
85   // Currently the only the cases it returns false for are really sound and
86   // returning true means unknown.
87   Value *P = &*F->arg_begin();
88   auto InstIter = F->front().begin();
89   Value *ConstDerefPtr = *cast<CallInst>(&*InstIter)->arg_begin();
90   // ConstDerefPtr is a constant pointer that is provably de-referenceable. We
91   // can replace an arbitrary pointer with it.
92   EXPECT_TRUE(canReplacePointersIfEqual(P, ConstDerefPtr, DL, nullptr));
93 
94   ++InstIter;
95   Value *ConstUnDerefPtr = *cast<CallInst>(&*InstIter)->arg_begin();
96   // ConstUndDerefPtr is a constant pointer that is provably not
97   // de-referenceable. We cannot replace an arbitrary pointer with it.
98   EXPECT_FALSE(
99       canReplacePointersIfEqual(ConstDerefPtr, ConstUnDerefPtr, DL, nullptr));
100 }
101