LLVM 24.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/Analysis.h"
19#include "llvm/IR/IRBuilder.h"
20#include "llvm/IR/InstVisitor.h"
22#include "llvm/IR/PassManager.h"
23#include "llvm/IR/ValueHandle.h"
24#include "llvm/Pass.h"
25using namespace llvm;
26
27#define DEBUG_TYPE "wasm-ref-type-mem2local"
28
29namespace {
30class WebAssemblyRefTypeMem2LocalImpl
31 : public InstVisitor<WebAssemblyRefTypeMem2LocalImpl> {
32 bool Changed = false;
33
34public:
35 void visitAllocaInst(AllocaInst &AI);
37};
38
39class WebAssemblyRefTypeMem2LocalLegacy final : public FunctionPass {
40 StringRef getPassName() const override {
41 return "WebAssembly Reference Types Memory to Local";
42 }
43
44 void getAnalysisUsage(AnalysisUsage &AU) const override {
45 AU.setPreservesCFG();
47 }
48
49 bool runOnFunction(Function &F) override;
50
51public:
52 static char ID;
53 WebAssemblyRefTypeMem2LocalLegacy() : FunctionPass(ID) {}
54};
55} // End anonymous namespace
56
57char WebAssemblyRefTypeMem2LocalLegacy::ID = 0;
58INITIALIZE_PASS(WebAssemblyRefTypeMem2LocalLegacy, DEBUG_TYPE,
59 "Assign reference type allocas to local address space", true,
60 false)
61
63 return new WebAssemblyRefTypeMem2LocalLegacy();
64}
65
66void WebAssemblyRefTypeMem2LocalImpl::visitAllocaInst(AllocaInst &AI) {
67 if (WebAssembly::isWebAssemblyReferenceType(AI.getAllocatedType())) {
68 Changed = true;
69 IRBuilder<> IRB(&AI);
70 auto *NewAI = IRB.CreateAlloca(AI.getAllocatedType(),
71 WebAssembly::WASM_ADDRESS_SPACE_VAR, nullptr,
72 AI.getName() + ".var");
73 // Preserve the original alloca's alignment.
74 NewAI->setAlignment(AI.getAlign());
75
76 // The below is basically equivalent to AI.replaceAllUsesWith(NewAI), but we
77 // cannot use it because it requires the old and new types be the same,
78 // which is not true here because the address spaces are different.
79 if (AI.hasValueHandle())
81 if (AI.isUsedByMetadata())
83 while (!AI.materialized_use_empty()) {
85 U.set(NewAI);
86 }
87
88 AI.eraseFromParent();
89 }
90}
91
92bool WebAssemblyRefTypeMem2LocalImpl::runOnFunction(Function &F) {
93 LLVM_DEBUG(dbgs() << "********** WebAssembly RefType Mem2Local **********\n"
94 "********** Function: "
95 << F.getName() << '\n');
96
97 if (F.getFnAttribute("target-features")
98 .getValueAsString()
99 .contains("+reference-types"))
100 visit(F);
101 return Changed;
102}
103
104bool WebAssemblyRefTypeMem2LocalLegacy::runOnFunction(Function &F) {
105 WebAssemblyRefTypeMem2LocalImpl Impl;
106 return Impl.runOnFunction(F);
107}
108
109PreservedAnalyses
112 WebAssemblyRefTypeMem2LocalImpl Impl;
113 return Impl.runOnFunction(F)
116}
static bool runOnFunction(Function &F, bool PostInlining)
#define DEBUG_TYPE
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
FunctionAnalysisManager FAM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static void visit(BasicBlock &Start, std::function< bool(BasicBlock *)> op)
#define LLVM_DEBUG(...)
Definition Debug.h:119
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
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:278
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
Base class for instruction visitors.
Definition InstVisitor.h:78
LLVM_ABI 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:113
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
static LLVM_ABI void handleRAUW(Value *From, Value *To)
Definition Metadata.cpp:562
static LLVM_ABI void ValueIsRAUWd(Value *Old, Value *New)
Definition Value.cpp:1297
bool materialized_use_empty() const
Definition Value.h:353
bool isUsedByMetadata() const
Return true if there is metadata referencing this value.
Definition Value.h:560
use_iterator materialized_use_begin()
Definition Value.h:358
bool hasValueHandle() const
Return true if there is a value handle associated with this value.
Definition Value.h:557
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Changed
Pass manager infrastructure for declaring and invalidating analyses.
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
IRBuilder(LLVMContext &, FolderTy, InserterTy) -> IRBuilder< FolderTy, InserterTy >
FunctionPass * createWebAssemblyRefTypeMem2LocalLegacyPass()
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.