LLVM  6.0.0svn
PreISelIntrinsicLowering.cpp
Go to the documentation of this file.
1 //===- PreISelIntrinsicLowering.cpp - Pre-ISel intrinsic lowering pass ----===//
2 //
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 
15 #include "llvm/CodeGen/Passes.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/IRBuilder.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/Type.h"
21 #include "llvm/IR/User.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Support/Casting.h"
24 
25 using namespace llvm;
26 
27 static bool lowerLoadRelative(Function &F) {
28  if (F.use_empty())
29  return false;
30 
31  bool Changed = false;
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 
58 static bool lowerIntrinsics(Module &M) {
59  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 
67 namespace {
68 
69 class PreISelIntrinsicLoweringLegacyPass : public ModulePass {
70 public:
71  static char ID;
72 
73  PreISelIntrinsicLoweringLegacyPass() : ModulePass(ID) {}
74 
75  bool runOnModule(Module &M) override { return lowerIntrinsics(M); }
76 };
77 
78 } // end anonymous namespace
79 
81 
82 INITIALIZE_PASS(PreISelIntrinsicLoweringLegacyPass,
83  "pre-isel-intrinsic-lowering", "Pre-ISel Intrinsic Lowering",
84  false, false)
85 
87  return new PreISelIntrinsicLoweringLegacyPass;
88 }
89 
92  if (!lowerIntrinsics(M))
93  return PreservedAnalyses::all();
94  else
95  return PreservedAnalyses::none();
96 }
use_iterator use_end()
Definition: Value.h:348
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
This class represents a function call, abstracting a target machine&#39;s calling convention.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
F(f)
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
Definition: Type.cpp:639
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:668
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1448
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:430
static bool lowerLoadRelative(Function &F)
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:156
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
INITIALIZE_PASS(PreISelIntrinsicLoweringLegacyPass, "pre-isel-intrinsic-lowering", "Pre-ISel Intrinsic Lowering", false, false) ModulePass *llvm
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:194
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
Value * CreateGEP(Value *Ptr, ArrayRef< Value *> IdxList, const Twine &Name="")
Definition: IRBuilder.h:1227
Module.h This file contains the declarations for the Module class.
use_iterator use_begin()
Definition: Value.h:340
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:176
#define I(x, y, z)
Definition: MD5.cpp:58
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:225
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
LoadInst * CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name)
Definition: IRBuilder.h:1186
LLVM Value Representation.
Definition: Value.h:73
ModulePass * createPreISelIntrinsicLoweringPass()
This pass lowers the .load.relative intrinsic to instructions.
A container for analyses that lazily runs them and caches their results.
static bool lowerIntrinsics(Module &M)
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:174
bool use_empty() const
Definition: Value.h:328
IntegerType * Int32Ty