1 //===- LoadsTest.cpp - local load analysis unit tests ---------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Analysis/Loads.h"
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/IR/Constants.h"
12 #include "llvm/IR/Instructions.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "gtest/gtest.h"
20 static std::unique_ptr
<Module
> parseIR(LLVMContext
&C
, const char *IR
) {
22 std::unique_ptr
<Module
> Mod
= parseAssemblyString(IR
, Err
, C
);
24 Err
.print("AnalysisTests", errs());
28 TEST(LoadsTest
, FindAvailableLoadedValueSameBasePtrConstantOffsetsNullAA
) {
30 std::unique_ptr
<Module
> M
= parseIR(C
,
32 target datalayout = "p
:64:64:64:32"
33 %class = type <{ i32, i32 }>
38 %f1 = getelementptr inbounds %class, %class* %o, i32 0, i32 0
39 store i32 42, i32* %f1
40 %f2 = getelementptr inbounds %class, %class* %o, i32 0, i32 1
41 store i32 43, i32* %f2
42 %v = load i32, i32* %f1
46 auto *GV
= M
->getNamedValue("f");
48 auto *F
= dyn_cast
<Function
>(GV
);
50 Instruction
*Inst
= &F
->front().front();
51 auto *AI
= dyn_cast
<AllocaInst
>(Inst
);
53 Inst
= &*++F
->front().rbegin();
54 auto *LI
= dyn_cast
<LoadInst
>(Inst
);
56 BasicBlock::iterator
BBI(LI
);
57 Value
*Loaded
= FindAvailableLoadedValue(
58 LI
, LI
->getParent(), BBI
, 0, nullptr, nullptr);
60 auto *CI
= dyn_cast
<ConstantInt
>(Loaded
);
62 ASSERT_TRUE(CI
->equalsInt(42));
65 TEST(LoadsTest
, CanReplacePointersIfEqual
) {
67 std::unique_ptr
<Module
> M
= parseIR(C
,
69 @y = common global [1 x i32] zeroinitializer, align 4
70 @x = common global [1 x i32] zeroinitializer, align 4
72 declare void @use(i32*)
74 define void @f(i32* %p) {
75 call void @use(i32* getelementptr inbounds ([1 x i32], [1 x i32]* @y, i64 0, i64 0))
76 call void @use(i32* getelementptr inbounds (i32, i32* getelementptr inbounds ([1 x i32], [1 x i32]* @x, i64 0, i64 0), i64 1))
80 const auto &DL
= M
->getDataLayout();
81 auto *GV
= M
->getNamedValue("f");
83 auto *F
= dyn_cast
<Function
>(GV
);
86 // NOTE: the implementation of canReplacePointersIfEqual is incomplete.
87 // Currently the only the cases it returns false for are really sound and
88 // returning true means unknown.
89 Value
*P
= &*F
->arg_begin();
90 auto InstIter
= F
->front().begin();
91 Value
*ConstDerefPtr
= *cast
<CallInst
>(&*InstIter
)->arg_begin();
92 // ConstDerefPtr is a constant pointer that is provably de-referenceable. We
93 // can replace an arbitrary pointer with it.
94 EXPECT_TRUE(canReplacePointersIfEqual(P
, ConstDerefPtr
, DL
, nullptr));
97 Value
*ConstUnDerefPtr
= *cast
<CallInst
>(&*InstIter
)->arg_begin();
98 // ConstUndDerefPtr is a constant pointer that is provably not
99 // de-referenceable. We cannot replace an arbitrary pointer with it.
101 canReplacePointersIfEqual(ConstDerefPtr
, ConstUnDerefPtr
, DL
, nullptr));