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.getContext());
70 IRB.SetInsertPoint(&AI);
71 auto *NewAI = IRB.CreateAlloca(AI.getAllocatedType(),
72 WebAssembly::WASM_ADDRESS_SPACE_VAR, nullptr,
73 AI.getName() + ".var");
74 // Preserve the original alloca's alignment.
75 NewAI->setAlignment(AI.getAlign());
76
77 // The below is basically equivalent to AI.replaceAllUsesWith(NewAI), but we
78 // cannot use it because it requires the old and new types be the same,
79 // which is not true here because the address spaces are different.
80 if (AI.hasValueHandle())
82 if (AI.isUsedByMetadata())
84 while (!AI.materialized_use_empty()) {
86 U.set(NewAI);
87 }
88
89 AI.eraseFromParent();
90 }
91}
92
93bool WebAssemblyRefTypeMem2LocalImpl::runOnFunction(Function &F) {
94 LLVM_DEBUG(dbgs() << "********** WebAssembly RefType Mem2Local **********\n"
95 "********** Function: "
96 << F.getName() << '\n');
97
98 if (F.getFnAttribute("target-features")
99 .getValueAsString()
100 .contains("+reference-types"))
101 visit(F);
102 return Changed;
103}
104
105bool WebAssemblyRefTypeMem2LocalLegacy::runOnFunction(Function &F) {
106 WebAssemblyRefTypeMem2LocalImpl Impl;
107 return Impl.runOnFunction(F);
108}
109
110PreservedAnalyses
113 WebAssemblyRefTypeMem2LocalImpl Impl;
114 return Impl.runOnFunction(F)
117}
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:275
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:112
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:552
static LLVM_ABI void ValueIsRAUWd(Value *Old, Value *New)
Definition Value.cpp:1316
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:258
bool materialized_use_empty() const
Definition Value.h:351
bool isUsedByMetadata() const
Return true if there is metadata referencing this value.
Definition Value.h:558
use_iterator materialized_use_begin()
Definition Value.h:356
bool hasValueHandle() const
Return true if there is a value handle associated with this value.
Definition Value.h:555
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.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
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
FunctionPass * createWebAssemblyRefTypeMem2LocalLegacyPass()
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.