|
7 | 7 | //===----------------------------------------------------------------------===//
|
8 | 8 |
|
9 | 9 | #include "llvm/SandboxIR/Utils.h"
|
| 10 | +#include "llvm/Analysis/AssumptionCache.h" |
| 11 | +#include "llvm/Analysis/BasicAliasAnalysis.h" |
| 12 | +#include "llvm/Analysis/LoopInfo.h" |
| 13 | +#include "llvm/Analysis/TargetLibraryInfo.h" |
10 | 14 | #include "llvm/AsmParser/Parser.h"
|
11 | 15 | #include "llvm/IR/BasicBlock.h"
|
12 | 16 | #include "llvm/IR/DataLayout.h"
|
| 17 | +#include "llvm/IR/Dominators.h" |
13 | 18 | #include "llvm/IR/Function.h"
|
14 | 19 | #include "llvm/IR/Instruction.h"
|
15 | 20 | #include "llvm/IR/Module.h"
|
@@ -55,3 +60,77 @@ define void @foo(ptr %arg0) {
|
55 | 60 | EXPECT_EQ(sandboxir::Utils::memoryLocationGetOrNone(Ld),
|
56 | 61 | MemoryLocation::getOrNone(LLVMLd));
|
57 | 62 | }
|
| 63 | + |
| 64 | +TEST_F(UtilsTest, GetPointerDiffInBytes) { |
| 65 | + parseIR(C, R"IR( |
| 66 | +define void @foo(ptr %ptr) { |
| 67 | + %gep0 = getelementptr inbounds float, ptr %ptr, i64 0 |
| 68 | + %gep1 = getelementptr inbounds float, ptr %ptr, i64 1 |
| 69 | + %gep2 = getelementptr inbounds float, ptr %ptr, i64 2 |
| 70 | + %gep3 = getelementptr inbounds float, ptr %ptr, i64 3 |
| 71 | +
|
| 72 | + %ld0 = load float, ptr %gep0 |
| 73 | + %ld1 = load float, ptr %gep1 |
| 74 | + %ld2 = load float, ptr %gep2 |
| 75 | + %ld3 = load float, ptr %gep3 |
| 76 | +
|
| 77 | + %v2ld0 = load <2 x float>, ptr %gep0 |
| 78 | + %v2ld1 = load <2 x float>, ptr %gep1 |
| 79 | + %v2ld2 = load <2 x float>, ptr %gep2 |
| 80 | + %v2ld3 = load <2 x float>, ptr %gep3 |
| 81 | +
|
| 82 | + %v3ld0 = load <3 x float>, ptr %gep0 |
| 83 | + %v3ld1 = load <3 x float>, ptr %gep1 |
| 84 | + %v3ld2 = load <3 x float>, ptr %gep2 |
| 85 | + %v3ld3 = load <3 x float>, ptr %gep3 |
| 86 | + ret void |
| 87 | +} |
| 88 | +)IR"); |
| 89 | + llvm::Function &LLVMF = *M->getFunction("foo"); |
| 90 | + DominatorTree DT(LLVMF); |
| 91 | + TargetLibraryInfoImpl TLII; |
| 92 | + TargetLibraryInfo TLI(TLII); |
| 93 | + DataLayout DL(M->getDataLayout()); |
| 94 | + AssumptionCache AC(LLVMF); |
| 95 | + BasicAAResult BAA(DL, LLVMF, TLI, AC, &DT); |
| 96 | + AAResults AA(TLI); |
| 97 | + AA.addAAResult(BAA); |
| 98 | + LoopInfo LI(DT); |
| 99 | + ScalarEvolution SE(LLVMF, TLI, AC, DT, LI); |
| 100 | + sandboxir::Context Ctx(C); |
| 101 | + |
| 102 | + auto &F = *Ctx.createFunction(&LLVMF); |
| 103 | + auto &BB = *F.begin(); |
| 104 | + auto It = std::next(BB.begin(), 4); |
| 105 | + auto *L0 = cast<sandboxir::LoadInst>(&*It++); |
| 106 | + auto *L1 = cast<sandboxir::LoadInst>(&*It++); |
| 107 | + auto *L2 = cast<sandboxir::LoadInst>(&*It++); |
| 108 | + [[maybe_unused]] auto *L3 = cast<sandboxir::LoadInst>(&*It++); |
| 109 | + |
| 110 | + auto *V2L0 = cast<sandboxir::LoadInst>(&*It++); |
| 111 | + auto *V2L1 = cast<sandboxir::LoadInst>(&*It++); |
| 112 | + auto *V2L2 = cast<sandboxir::LoadInst>(&*It++); |
| 113 | + auto *V2L3 = cast<sandboxir::LoadInst>(&*It++); |
| 114 | + |
| 115 | + [[maybe_unused]] auto *V3L0 = cast<sandboxir::LoadInst>(&*It++); |
| 116 | + auto *V3L1 = cast<sandboxir::LoadInst>(&*It++); |
| 117 | + [[maybe_unused]] auto *V3L2 = cast<sandboxir::LoadInst>(&*It++); |
| 118 | + [[maybe_unused]] auto *V3L3 = cast<sandboxir::LoadInst>(&*It++); |
| 119 | + |
| 120 | + // getPointerDiffInBytes |
| 121 | + EXPECT_EQ(*sandboxir::Utils::getPointerDiffInBytes(L0, L1, SE, DL), 4); |
| 122 | + EXPECT_EQ(*sandboxir::Utils::getPointerDiffInBytes(L0, L2, SE, DL), 8); |
| 123 | + EXPECT_EQ(*sandboxir::Utils::getPointerDiffInBytes(L1, L0, SE, DL), -4); |
| 124 | + EXPECT_EQ(*sandboxir::Utils::getPointerDiffInBytes(L0, V2L0, SE, DL), 0); |
| 125 | + |
| 126 | + EXPECT_EQ(*sandboxir::Utils::getPointerDiffInBytes(L0, V2L1, SE, DL), 4); |
| 127 | + EXPECT_EQ(*sandboxir::Utils::getPointerDiffInBytes(L0, V3L1, SE, DL), 4); |
| 128 | + EXPECT_EQ(*sandboxir::Utils::getPointerDiffInBytes(V2L0, V2L2, SE, DL), 8); |
| 129 | + EXPECT_EQ(*sandboxir::Utils::getPointerDiffInBytes(V2L0, V2L3, SE, DL), 12); |
| 130 | + EXPECT_EQ(*sandboxir::Utils::getPointerDiffInBytes(V2L3, V2L0, SE, DL), -12); |
| 131 | + |
| 132 | + // atLowerAddress |
| 133 | + EXPECT_TRUE(sandboxir::Utils::atLowerAddress(L0, L1, SE, DL)); |
| 134 | + EXPECT_FALSE(sandboxir::Utils::atLowerAddress(L1, L0, SE, DL)); |
| 135 | + EXPECT_FALSE(sandboxir::Utils::atLowerAddress(L3, V3L3, SE, DL)); |
| 136 | +} |
0 commit comments