#include "llvm/Transforms/Utils/RelLookupTableConverter.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
using namespace llvm;
static bool shouldConvertToRelLookupTable(Module &M, GlobalVariable &GV) {
if (!GV.hasInitializer() ||
!GV.isConstant() ||
!GV.hasOneUse())
return false;
GetElementPtrInst *GEP =
dyn_cast<GetElementPtrInst>(GV.use_begin()->getUser());
if (!GEP || !GEP->hasOneUse() ||
GV.getValueType() != GEP->getSourceElementType())
return false;
LoadInst *Load = dyn_cast<LoadInst>(GEP->use_begin()->getUser());
if (!Load || !Load->hasOneUse() ||
Load->getType() != GEP->getResultElementType())
return false;
if (!GV.hasLocalLinkage() ||
!GV.isDSOLocal() ||
!GV.isImplicitDSOLocal())
return false;
ConstantArray *Array = dyn_cast<ConstantArray>(GV.getInitializer());
if (!Array)
return false;
const DataLayout &DL = M.getDataLayout();
Type *ElemType = Array->getType()->getElementType();
if (!ElemType->isPointerTy() || DL.getPointerTypeSizeInBits(ElemType) != 64)
return false;
for (const Use &Op : Array->operands()) {
Constant *ConstOp = cast<Constant>(&Op);
GlobalValue *GVOp;
APInt Offset;
if (!IsConstantOffsetFromGlobal(ConstOp, GVOp, Offset, DL))
return false;
auto *GlovalVarOp = dyn_cast<GlobalVariable>(GVOp);
if (!GlovalVarOp || !GlovalVarOp->isConstant())
return false;
if (!GlovalVarOp->hasLocalLinkage() ||
!GlovalVarOp->isDSOLocal() ||
!GlovalVarOp->isImplicitDSOLocal())
return false;
}
return true;
}
static GlobalVariable *createRelLookupTable(Function &Func,
GlobalVariable &LookupTable) {
Module &M = *Func.getParent();
ConstantArray *LookupTableArr =
cast<ConstantArray>(LookupTable.getInitializer());
unsigned NumElts = LookupTableArr->getType()->getNumElements();
ArrayType *IntArrayTy =
ArrayType::get(Type::getInt32Ty(M.getContext()), NumElts);
GlobalVariable *RelLookupTable = new GlobalVariable(
M, IntArrayTy, LookupTable.isConstant(), LookupTable.getLinkage(),
nullptr, "reltable." + Func.getName(), &LookupTable,
LookupTable.getThreadLocalMode(), LookupTable.getAddressSpace(),
LookupTable.isExternallyInitialized());
uint64_t Idx = 0;
SmallVector<Constant *, 64> RelLookupTableContents(NumElts);
for (Use &Operand : LookupTableArr->operands()) {
Constant *Element = cast<Constant>(Operand);
Type *IntPtrTy = M.getDataLayout().getIntPtrType(M.getContext());
Constant *Base = llvm::ConstantExpr::getPtrToInt(RelLookupTable, IntPtrTy);
Constant *Target = llvm::ConstantExpr::getPtrToInt(Element, IntPtrTy);
Constant *Sub = llvm::ConstantExpr::getSub(Target, Base);
Constant *RelOffset =
llvm::ConstantExpr::getTrunc(Sub, Type::getInt32Ty(M.getContext()));
RelLookupTableContents[Idx++] = RelOffset;
}
Constant *Initializer =
ConstantArray::get(IntArrayTy, RelLookupTableContents);
RelLookupTable->setInitializer(Initializer);
RelLookupTable->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
RelLookupTable->setAlignment(llvm::Align(4));
return RelLookupTable;
}
static void convertToRelLookupTable(GlobalVariable &LookupTable) {
GetElementPtrInst *GEP =
cast<GetElementPtrInst>(LookupTable.use_begin()->getUser());
LoadInst *Load = cast<LoadInst>(GEP->use_begin()->getUser());
Module &M = *LookupTable.getParent();
BasicBlock *BB = GEP->getParent();
IRBuilder<> Builder(BB);
Function &Func = *BB->getParent();
GlobalVariable *RelLookupTable = createRelLookupTable(Func, LookupTable);
Builder.SetInsertPoint(GEP);
Value *Index = GEP->getOperand(2);
IntegerType *IntTy = cast<IntegerType>(Index->getType());
Value *Offset =
Builder.CreateShl(Index, ConstantInt::get(IntTy, 2), "reltable.shift");
Builder.SetInsertPoint(Load);
Function *LoadRelIntrinsic = llvm::Intrinsic::getDeclaration(
&M, Intrinsic::load_relative, {Index->getType()});
Value *Base = Builder.CreateBitCast(RelLookupTable, Builder.getInt8PtrTy());
Value *Result = Builder.CreateCall(LoadRelIntrinsic, {Base, Offset},
"reltable.intrinsic");
if (Load->getType() != Builder.getInt8PtrTy())
Result = Builder.CreateBitCast(Result, Load->getType(), "reltable.bitcast");
Load->replaceAllUsesWith(Result);
Load->eraseFromParent();
GEP->eraseFromParent();
}
static bool convertToRelativeLookupTables(
Module &M, function_ref<TargetTransformInfo &(Function &)> GetTTI) {
for (Function &F : M) {
if (F.isDeclaration())
continue;
if (!GetTTI(F).shouldBuildRelLookupTables())
return false;
break;
}
bool Changed = false;
for (GlobalVariable &GV : llvm::make_early_inc_range(M.globals())) {
if (!shouldConvertToRelLookupTable(M, GV))
continue;
convertToRelLookupTable(GV);
GV.eraseFromParent();
Changed = true;
}
return Changed;
}
PreservedAnalyses RelLookupTableConverterPass::run(Module &M,
ModuleAnalysisManager &AM) {
FunctionAnalysisManager &FAM =
AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
auto GetTTI = [&](Function &F) -> TargetTransformInfo & {
return FAM.getResult<TargetIRAnalysis>(F);
};
if (!convertToRelativeLookupTables(M, GetTTI))
return PreservedAnalyses::all();
PreservedAnalyses PA;
PA.preserveSet<CFGAnalyses>();
return PA;
}