LLVM 19.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"
21#include "llvm/Pass.h"
22#include <set>
23
24using namespace llvm;
25
26#define DEBUG_TYPE "wasm-lower-reftypes-intptr-conv"
27
28namespace {
29class WebAssemblyLowerRefTypesIntPtrConv final : public FunctionPass {
30 StringRef getPassName() const override {
31 return "WebAssembly Lower RefTypes Int-Ptr Conversions";
32 }
33
34 bool runOnFunction(Function &MF) override;
35
36public:
37 static char ID; // Pass identification
38 WebAssemblyLowerRefTypesIntPtrConv() : FunctionPass(ID) {}
39};
40} // end anonymous namespace
41
42char WebAssemblyLowerRefTypesIntPtrConv::ID = 0;
43INITIALIZE_PASS(WebAssemblyLowerRefTypesIntPtrConv, DEBUG_TYPE,
44 "WebAssembly Lower RefTypes Int-Ptr Conversions", false, false)
45
47 return new WebAssemblyLowerRefTypesIntPtrConv();
48}
49
50bool WebAssemblyLowerRefTypesIntPtrConv::runOnFunction(Function &F) {
51 LLVM_DEBUG(dbgs() << "********** Lower RefTypes IntPtr Convs **********\n"
52 "********** Function: "
53 << F.getName() << '\n');
54
55 // This function will check for uses of ptrtoint and inttoptr on reference
56 // types and replace them with a trap instruction.
57 //
58 // We replace the instruction by a trap instruction
59 // and its uses by null in the case of inttoptr and 0 in the
60 // case of ptrtoint.
61 std::set<Instruction *> worklist;
62
63 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
64 PtrToIntInst *PTI = dyn_cast<PtrToIntInst>(&*I);
65 IntToPtrInst *ITP = dyn_cast<IntToPtrInst>(&*I);
67 PTI->getPointerOperand()->getType())) &&
69 continue;
70
71 UndefValue *U = UndefValue::get(I->getType());
72 I->replaceAllUsesWith(U);
73
74 Function *TrapIntrin =
75 Intrinsic::getDeclaration(F.getParent(), Intrinsic::debugtrap);
76 CallInst::Create(TrapIntrin, {}, "", I->getIterator());
77
78 worklist.insert(&*I);
79 }
80
81 // erase each instruction replaced by trap
82 for (Instruction *I : worklist)
83 I->eraseFromParent();
84
85 return !worklist.empty();
86}
#define LLVM_DEBUG(X)
Definition: Debug.h:101
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file declares the WebAssembly-specific subclass of TargetSubtarget.
This file contains the declaration of the WebAssembly-specific type parsing utility functions.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr, BasicBlock::iterator InsertBefore)
Type * getDestTy() const
Return the destination type, as a convenience.
Definition: InstrTypes.h:910
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
This class represents a cast from an integer to a pointer.
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
This class represents a cast from a pointer to an integer.
Value * getPointerOperand()
Gets the pointer operand.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
'undef' values are things that do not have specified contents.
Definition: Constants.h:1348
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1808
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1459
bool isWebAssemblyReferenceType(const Type *Ty)
Return true if this is a WebAssembly Reference Type.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
FunctionPass * createWebAssemblyLowerRefTypesIntPtrConv()
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:131
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:132