LLVM  16.0.0git
WebAssemblyLowerRefTypesIntPtrConv.cpp
Go to the documentation of this file.
1 //=== WebAssemblyLowerRefTypesIntPtrConv.cpp -
2 // Lower IntToPtr and PtrToInt on Reference Types ---===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// Lowers IntToPtr and PtrToInt instructions on reference types to
12 /// Trap instructions since they have been allowed to operate
13 /// on non-integral pointers.
14 ///
15 //===----------------------------------------------------------------------===//
16 
18 #include "WebAssembly.h"
19 #include "WebAssemblySubtarget.h"
20 #include "llvm/IR/InstIterator.h"
21 #include "llvm/Pass.h"
22 
23 using namespace llvm;
24 
25 #define DEBUG_TYPE "wasm-lower-reftypes-intptr-conv"
26 
27 namespace {
28 class WebAssemblyLowerRefTypesIntPtrConv final : public FunctionPass {
29  StringRef getPassName() const override {
30  return "WebAssembly Lower RefTypes Int-Ptr Conversions";
31  }
32 
33  bool runOnFunction(Function &MF) override;
34 
35 public:
36  static char ID; // Pass identification
37  WebAssemblyLowerRefTypesIntPtrConv() : FunctionPass(ID) {}
38 };
39 } // end anonymous namespace
40 
42 INITIALIZE_PASS(WebAssemblyLowerRefTypesIntPtrConv, DEBUG_TYPE,
43  "WebAssembly Lower RefTypes Int-Ptr Conversions", false, false)
44 
46  return new WebAssemblyLowerRefTypesIntPtrConv();
47 }
48 
50  LLVM_DEBUG(dbgs() << "********** Lower RefTypes IntPtr Convs **********\n"
51  "********** Function: "
52  << F.getName() << '\n');
53 
54  // This function will check for uses of ptrtoint and inttoptr on reference
55  // types and replace them with a trap instruction.
56  //
57  // We replace the instruction by a trap instruction
58  // and its uses by null in the case of inttoptr and 0 in the
59  // case of ptrtoint.
60  std::set<Instruction *> worklist;
61 
62  for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
63  PtrToIntInst *PTI = dyn_cast<PtrToIntInst>(&*I);
64  IntToPtrInst *ITP = dyn_cast<IntToPtrInst>(&*I);
65  if (!(PTI && WebAssembly::isRefType(PTI->getPointerOperand()->getType())) &&
66  !(ITP && WebAssembly::isRefType(ITP->getDestTy())))
67  continue;
68 
69  UndefValue *U = UndefValue::get(I->getType());
70  I->replaceAllUsesWith(U);
71 
72  Function *TrapIntrin =
73  Intrinsic::getDeclaration(F.getParent(), Intrinsic::debugtrap);
74  CallInst::Create(TrapIntrin, {}, "", &*I);
75 
76  worklist.insert(&*I);
77  }
78 
79  // erase each instruction replaced by trap
80  for (Instruction *I : worklist)
81  I->eraseFromParent();
82 
83  return !worklist.empty();
84 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
WebAssembly.h
llvm::Intrinsic::getDeclaration
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1421
InstIterator.h
llvm::Function
Definition: Function.h:60
Pass.h
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::CastInst::getDestTy
Type * getDestTy() const
Return the destination type, as a convenience.
Definition: InstrTypes.h:683
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
DEBUG_TYPE
#define DEBUG_TYPE
Definition: WebAssemblyLowerRefTypesIntPtrConv.cpp:25
llvm::CallInst::Create
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:1517
llvm::Instruction
Definition: Instruction.h:42
WebAssemblyTypeUtilities.h
llvm::UndefValue::get
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1713
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
INITIALIZE_PASS
INITIALIZE_PASS(WebAssemblyLowerRefTypesIntPtrConv, DEBUG_TYPE, "WebAssembly Lower RefTypes Int-Ptr Conversions", false, false) FunctionPass *llvm
Definition: WebAssemblyLowerRefTypesIntPtrConv.cpp:42
llvm::PtrToIntInst::getPointerOperand
Value * getPointerOperand()
Gets the pointer operand.
Definition: Instructions.h:5231
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::UndefValue
'undef' values are things that do not have specified contents.
Definition: Constants.h:1356
llvm::WebAssembly::isRefType
bool isRefType(const Type *Ty)
Definition: WebAssemblyTypeUtilities.h:82
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
llvm::PtrToIntInst
This class represents a cast from a pointer to an integer.
Definition: Instructions.h:5205
llvm::createWebAssemblyLowerRefTypesIntPtrConv
FunctionPass * createWebAssemblyLowerRefTypesIntPtrConv()
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
WebAssemblySubtarget.h
llvm::inst_end
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:132
llvm::IntToPtrInst
This class represents a cast from an integer to a pointer.
Definition: Instructions.h:5162
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
llvm::inst_begin
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:131
llvm::InstIterator
Definition: InstIterator.h:32
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38