blob: 8f88ef78828aed09d88c85f828034c47a16f387f [file] [log] [blame]
Eugene Zelenko618c5552017-09-13 21:15:20 +00001//===- PreISelIntrinsicLowering.cpp - Pre-ISel intrinsic lowering pass ----===//
Peter Collingbourne7dd8dbf2016-04-22 21:18:02 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass implements IR lowering for the llvm.load.relative intrinsic.
11//
12//===----------------------------------------------------------------------===//
13
Michael Kuperstein82d5da52016-06-24 20:13:42 +000014#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
Peter Collingbourne7dd8dbf2016-04-22 21:18:02 +000015#include "llvm/CodeGen/Passes.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/IRBuilder.h"
18#include "llvm/IR/Instructions.h"
Peter Collingbourne7dd8dbf2016-04-22 21:18:02 +000019#include "llvm/IR/Module.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000020#include "llvm/IR/Type.h"
21#include "llvm/IR/User.h"
Peter Collingbourne7dd8dbf2016-04-22 21:18:02 +000022#include "llvm/Pass.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000023#include "llvm/Support/Casting.h"
Peter Collingbourne7dd8dbf2016-04-22 21:18:02 +000024
25using namespace llvm;
26
Eugene Zelenko618c5552017-09-13 21:15:20 +000027static bool lowerLoadRelative(Function &F) {
Peter Collingbourne7dd8dbf2016-04-22 21:18:02 +000028 if (F.use_empty())
29 return false;
30
31 bool Changed = false;
32 Type *Int32Ty = Type::getInt32Ty(F.getContext());
33 Type *Int32PtrTy = Int32Ty->getPointerTo();
34 Type *Int8Ty = Type::getInt8Ty(F.getContext());
35
36 for (auto I = F.use_begin(), E = F.use_end(); I != E;) {
37 auto CI = dyn_cast<CallInst>(I->getUser());
38 ++I;
39 if (!CI || CI->getCalledValue() != &F)
40 continue;
41
42 IRBuilder<> B(CI);
43 Value *OffsetPtr =
44 B.CreateGEP(Int8Ty, CI->getArgOperand(0), CI->getArgOperand(1));
45 Value *OffsetPtrI32 = B.CreateBitCast(OffsetPtr, Int32PtrTy);
46 Value *OffsetI32 = B.CreateAlignedLoad(OffsetPtrI32, 4);
47
48 Value *ResultPtr = B.CreateGEP(Int8Ty, CI->getArgOperand(0), OffsetI32);
49
50 CI->replaceAllUsesWith(ResultPtr);
51 CI->eraseFromParent();
52 Changed = true;
53 }
54
55 return Changed;
56}
57
Eugene Zelenko618c5552017-09-13 21:15:20 +000058static bool lowerIntrinsics(Module &M) {
Peter Collingbourne7dd8dbf2016-04-22 21:18:02 +000059 bool Changed = false;
60 for (Function &F : M) {
61 if (F.getName().startswith("llvm.load.relative."))
62 Changed |= lowerLoadRelative(F);
63 }
64 return Changed;
65}
66
Eugene Zelenko618c5552017-09-13 21:15:20 +000067namespace {
68
Michael Kuperstein82d5da52016-06-24 20:13:42 +000069class PreISelIntrinsicLoweringLegacyPass : public ModulePass {
Peter Collingbourne7dd8dbf2016-04-22 21:18:02 +000070public:
71 static char ID;
Eugene Zelenko618c5552017-09-13 21:15:20 +000072
Michael Kuperstein82d5da52016-06-24 20:13:42 +000073 PreISelIntrinsicLoweringLegacyPass() : ModulePass(ID) {}
Peter Collingbourne7dd8dbf2016-04-22 21:18:02 +000074
Eugene Zelenko618c5552017-09-13 21:15:20 +000075 bool runOnModule(Module &M) override { return lowerIntrinsics(M); }
Peter Collingbourne7dd8dbf2016-04-22 21:18:02 +000076};
77
Eugene Zelenko618c5552017-09-13 21:15:20 +000078} // end anonymous namespace
79
Michael Kuperstein82d5da52016-06-24 20:13:42 +000080char PreISelIntrinsicLoweringLegacyPass::ID;
Peter Collingbourne7dd8dbf2016-04-22 21:18:02 +000081
Michael Kuperstein82d5da52016-06-24 20:13:42 +000082INITIALIZE_PASS(PreISelIntrinsicLoweringLegacyPass,
83 "pre-isel-intrinsic-lowering", "Pre-ISel Intrinsic Lowering",
84 false, false)
Peter Collingbourne7dd8dbf2016-04-22 21:18:02 +000085
Eugene Zelenko618c5552017-09-13 21:15:20 +000086ModulePass *llvm::createPreISelIntrinsicLoweringPass() {
Michael Kuperstein82d5da52016-06-24 20:13:42 +000087 return new PreISelIntrinsicLoweringLegacyPass;
Peter Collingbourne7dd8dbf2016-04-22 21:18:02 +000088}
Michael Kuperstein82d5da52016-06-24 20:13:42 +000089
90PreservedAnalyses PreISelIntrinsicLoweringPass::run(Module &M,
91 ModuleAnalysisManager &AM) {
92 if (!lowerIntrinsics(M))
93 return PreservedAnalyses::all();
94 else
95 return PreservedAnalyses::none();
96}