xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Transforms/Utils/RelLookupTableConverter.h (revision 18054d0220cfc8df9c9568c437bd6fbb59d53c3c)
1 //===-- RelLookupTableConverterPass.h - Rel Table Conv ----------*- C++ -*-===//
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 /// \file
10 /// This file implements relative lookup table converter that converts
11 /// lookup tables to relative lookup tables to make them PIC-friendly.
12 ///
13 /// Switch lookup table example:
14 /// @switch.table.foo = private unnamed_addr constant [3 x i8*]
15 /// [
16 /// i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0),
17 /// i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0),
18 /// i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0)
19 /// ], align 8
20 ///
21 /// switch.lookup:
22 ///   %1 = sext i32 %cond to i64
23 ///   %switch.gep = getelementptr inbounds [3 x i8*],
24 ///                 [3 x i8*]* @switch.table.foo, i64 0, i64 %1
25 ///   %switch.load = load i8*, i8** %switch.gep, align 8
26 ///  ret i8* %switch.load
27 ///
28 /// Switch lookup table will become a relative lookup table that
29 /// consists of relative offsets.
30 ///
31 /// @reltable.foo = private unnamed_addr constant [3 x i32]
32 /// [
33 /// i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str to i64),
34 ///                     i64 ptrtoint ([3 x i32]* @reltable.foo to i64)) to i32),
35 /// i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.1 to i64),
36 ///                     i64 ptrtoint ([3 x i32]* @reltable.foo to i64)) to i32),
37 /// i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.2 to i64),
38 ///                     i64 ptrtoint ([3 x i32]* @reltable.foo to i64)) to i32)
39 /// ], align 4
40 ///
41 /// IR after converting to a relative lookup table:
42 /// switch.lookup:
43 ///  %1 = sext i32 %cond to i64
44 ///  %reltable.shift = shl i64 %1, 2
45 ///  %reltable.intrinsic = call i8* @llvm.load.relative.i64(
46 ///                        i8* bitcast ([3 x i32]* @reltable.foo to i8*),
47 ///                        i64 %reltable.shift)
48 ///  ret i8* %reltable.intrinsic
49 //===----------------------------------------------------------------------===//
50 
51 #ifndef LLVM_TRANSFORMS_UTILS_RELLOOKUPTABLECONVERTER_H
52 #define LLVM_TRANSFORMS_UTILS_RELLOOKUPTABLECONVERTER_H
53 
54 #include "llvm/IR/Module.h"
55 #include "llvm/IR/PassManager.h"
56 
57 namespace llvm {
58 
59 // Pass that converts lookup tables to relative lookup tables.
60 class RelLookupTableConverterPass
61     : public PassInfoMixin<RelLookupTableConverterPass> {
62 public:
63   RelLookupTableConverterPass() = default;
64 
65   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
66 };
67 
68 } // end namespace llvm
69 
70 #endif // LLVM_TRANSFORMS_UTILS_RELLOOKUPTABLECONVERTER_H
71