Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:43

0001 //===-- RelLookupTableConverterPass.h - Rel Table Conv ----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 /// \file
0010 /// This file implements relative lookup table converter that converts
0011 /// lookup tables to relative lookup tables to make them PIC-friendly.
0012 ///
0013 /// Switch lookup table example:
0014 /// @switch.table.foo = private unnamed_addr constant [3 x i8*]
0015 /// [
0016 /// i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0),
0017 /// i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0),
0018 /// i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0)
0019 /// ], align 8
0020 ///
0021 /// switch.lookup:
0022 ///   %1 = sext i32 %cond to i64
0023 ///   %switch.gep = getelementptr inbounds [3 x i8*],
0024 ///                 [3 x i8*]* @switch.table.foo, i64 0, i64 %1
0025 ///   %switch.load = load i8*, i8** %switch.gep, align 8
0026 ///  ret i8* %switch.load
0027 ///
0028 /// Switch lookup table will become a relative lookup table that
0029 /// consists of relative offsets.
0030 ///
0031 /// @reltable.foo = private unnamed_addr constant [3 x i32]
0032 /// [
0033 /// i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str to i64),
0034 ///                     i64 ptrtoint ([3 x i32]* @reltable.foo to i64)) to i32),
0035 /// i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.1 to i64),
0036 ///                     i64 ptrtoint ([3 x i32]* @reltable.foo to i64)) to i32),
0037 /// i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.2 to i64),
0038 ///                     i64 ptrtoint ([3 x i32]* @reltable.foo to i64)) to i32)
0039 /// ], align 4
0040 ///
0041 /// IR after converting to a relative lookup table:
0042 /// switch.lookup:
0043 ///  %1 = sext i32 %cond to i64
0044 ///  %reltable.shift = shl i64 %1, 2
0045 ///  %reltable.intrinsic = call i8* @llvm.load.relative.i64(
0046 ///                        i8* bitcast ([3 x i32]* @reltable.foo to i8*),
0047 ///                        i64 %reltable.shift)
0048 ///  ret i8* %reltable.intrinsic
0049 //===----------------------------------------------------------------------===//
0050 
0051 #ifndef LLVM_TRANSFORMS_UTILS_RELLOOKUPTABLECONVERTER_H
0052 #define LLVM_TRANSFORMS_UTILS_RELLOOKUPTABLECONVERTER_H
0053 
0054 #include "llvm/IR/PassManager.h"
0055 
0056 namespace llvm {
0057 
0058 class Module;
0059 
0060 // Pass that converts lookup tables to relative lookup tables.
0061 class RelLookupTableConverterPass
0062     : public PassInfoMixin<RelLookupTableConverterPass> {
0063 public:
0064   RelLookupTableConverterPass() = default;
0065 
0066   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
0067 };
0068 
0069 } // end namespace llvm
0070 
0071 #endif // LLVM_TRANSFORMS_UTILS_RELLOOKUPTABLECONVERTER_H