LLVM 19.0.0git
WebAssemblyRefTypeMem2Local.cpp
Go to the documentation of this file.
1//=== WebAssemblyRefTypeMem2Local.cpp - WebAssembly RefType Mem2Local -----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Assign reference type allocas to local addrspace (addrspace(1)) so that
11/// their loads and stores can be lowered to local.gets/local.sets.
12///
13//===----------------------------------------------------------------------===//
14
17#include "WebAssembly.h"
18#include "llvm/IR/IRBuilder.h"
19#include "llvm/IR/InstVisitor.h"
20#include "llvm/IR/ValueHandle.h"
21#include "llvm/Pass.h"
22using namespace llvm;
23
24#define DEBUG_TYPE "wasm-ref-type-mem2local"
25
26namespace {
27class WebAssemblyRefTypeMem2Local final
28 : public FunctionPass,
29 public InstVisitor<WebAssemblyRefTypeMem2Local> {
30 StringRef getPassName() const override {
31 return "WebAssembly Reference Types Memory to Local";
32 }
33
34 void getAnalysisUsage(AnalysisUsage &AU) const override {
35 AU.setPreservesCFG();
37 }
38
39 bool runOnFunction(Function &F) override;
40 bool Changed = false;
41
42public:
43 static char ID;
44 WebAssemblyRefTypeMem2Local() : FunctionPass(ID) {}
45
47};
48} // End anonymous namespace
49
50char WebAssemblyRefTypeMem2Local::ID = 0;
51INITIALIZE_PASS(WebAssemblyRefTypeMem2Local, DEBUG_TYPE,
52 "Assign reference type allocas to local address space", true,
53 false)
54
56 return new WebAssemblyRefTypeMem2Local();
57}
58
59void WebAssemblyRefTypeMem2Local::visitAllocaInst(AllocaInst &AI) {
61 Changed = true;
62 IRBuilder<> IRB(AI.getContext());
63 IRB.SetInsertPoint(&AI);
64 auto *NewAI = IRB.CreateAlloca(AI.getAllocatedType(),
66 AI.getName() + ".var");
67
68 // The below is basically equivalent to AI.replaceAllUsesWith(NewAI), but we
69 // cannot use it because it requires the old and new types be the same,
70 // which is not true here because the address spaces are different.
71 if (AI.hasValueHandle())
73 if (AI.isUsedByMetadata())
75 while (!AI.materialized_use_empty()) {
77 U.set(NewAI);
78 }
79
80 AI.eraseFromParent();
81 }
82}
83
84bool WebAssemblyRefTypeMem2Local::runOnFunction(Function &F) {
85 LLVM_DEBUG(dbgs() << "********** WebAssembly RefType Mem2Local **********\n"
86 "********** Function: "
87 << F.getName() << '\n');
88
89 if (F.getFnAttribute("target-features")
90 .getValueAsString()
91 .contains("+reference-types"))
92 visit(F);
93 return Changed;
94}
#define LLVM_DEBUG(X)
Definition: Debug.h:101
#define F(x, y, z)
Definition: MD5.cpp:55
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
#define DEBUG_TYPE
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.
an instruction to allocate memory on the stack
Definition: Instructions.h:59
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
Definition: Instructions.h:125
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:269
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 provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2666
Base class for instruction visitors.
Definition: InstVisitor.h:78
RetTy visitAllocaInst(AllocaInst &I)
Definition: InstVisitor.h:168
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
static void handleRAUW(Value *From, Value *To)
Definition: Metadata.cpp:538
static void ValueIsRAUWd(Value *Old, Value *New)
Definition: Value.cpp:1254
bool materialized_use_empty() const
Definition: Value.h:349
bool isUsedByMetadata() const
Return true if there is metadata referencing this value.
Definition: Value.h:557
use_iterator materialized_use_begin()
Definition: Value.h:356
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1074
bool hasValueHandle() const
Return true if there is a value handle associated with this value.
Definition: Value.h:554
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
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
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
FunctionPass * createWebAssemblyRefTypeMem2Local()