1 //== WebAssemblyMemIntrinsicResults.cpp - Optimize memory intrinsic results ==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file implements an optimization pass using memory intrinsic results.
13 /// Calls to memory intrinsics (memcpy, memmove, memset) return the destination
14 /// address. They are in the form of
15 /// %dst_new = call @memcpy %dst, %src, %len
16 /// where %dst and %dst_new registers contain the same value.
18 /// This is to enable an optimization wherein uses of the %dst register used in
19 /// the parameter can be replaced by uses of the %dst_new register used in the
20 /// result, making the %dst register more likely to be single-use, thus more
21 /// likely to be useful to register stackifying, and potentially also exposing
22 /// the call instruction itself to register stackifying. These both can reduce
23 /// local.get/local.set traffic.
25 /// The LLVM intrinsics for these return void so they can't use the returned
26 /// attribute and consequently aren't handled by the OptimizeReturned pass.
28 //===----------------------------------------------------------------------===//
30 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
31 #include "WebAssembly.h"
32 #include "WebAssemblyMachineFunctionInfo.h"
33 #include "WebAssemblySubtarget.h"
34 #include "llvm/Analysis/TargetLibraryInfo.h"
35 #include "llvm/CodeGen/LiveIntervals.h"
36 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
37 #include "llvm/CodeGen/MachineDominators.h"
38 #include "llvm/CodeGen/MachineRegisterInfo.h"
39 #include "llvm/CodeGen/Passes.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/raw_ostream.h"
44 #define DEBUG_TYPE "wasm-mem-intrinsic-results"
47 class WebAssemblyMemIntrinsicResults final : public MachineFunctionPass {
49 static char ID; // Pass identification, replacement for typeid
50 WebAssemblyMemIntrinsicResults() : MachineFunctionPass(ID) {}
52 StringRef getPassName() const override {
53 return "WebAssembly Memory Intrinsic Results";
56 void getAnalysisUsage(AnalysisUsage &AU) const override {
58 AU.addRequired<MachineBlockFrequencyInfo>();
59 AU.addPreserved<MachineBlockFrequencyInfo>();
60 AU.addRequired<MachineDominatorTree>();
61 AU.addPreserved<MachineDominatorTree>();
62 AU.addRequired<LiveIntervals>();
63 AU.addPreserved<SlotIndexes>();
64 AU.addPreserved<LiveIntervals>();
65 AU.addRequired<TargetLibraryInfoWrapperPass>();
66 MachineFunctionPass::getAnalysisUsage(AU);
69 bool runOnMachineFunction(MachineFunction &MF) override;
73 } // end anonymous namespace
75 char WebAssemblyMemIntrinsicResults::ID = 0;
76 INITIALIZE_PASS(WebAssemblyMemIntrinsicResults, DEBUG_TYPE,
77 "Optimize memory intrinsic result values for WebAssembly",
80 FunctionPass *llvm::createWebAssemblyMemIntrinsicResults() {
81 return new WebAssemblyMemIntrinsicResults();
84 // Replace uses of FromReg with ToReg if they are dominated by MI.
85 static bool ReplaceDominatedUses(MachineBasicBlock &MBB, MachineInstr &MI,
86 unsigned FromReg, unsigned ToReg,
87 const MachineRegisterInfo &MRI,
88 MachineDominatorTree &MDT,
92 LiveInterval *FromLI = &LIS.getInterval(FromReg);
93 LiveInterval *ToLI = &LIS.getInterval(ToReg);
95 SlotIndex FromIdx = LIS.getInstructionIndex(MI).getRegSlot();
96 VNInfo *FromVNI = FromLI->getVNInfoAt(FromIdx);
98 SmallVector<SlotIndex, 4> Indices;
100 for (auto I = MRI.use_nodbg_begin(FromReg), E = MRI.use_nodbg_end();
102 MachineOperand &O = *I++;
103 MachineInstr *Where = O.getParent();
105 // Check that MI dominates the instruction in the normal way.
106 if (&MI == Where || !MDT.dominates(&MI, Where))
109 // If this use gets a different value, skip it.
110 SlotIndex WhereIdx = LIS.getInstructionIndex(*Where);
111 VNInfo *WhereVNI = FromLI->getVNInfoAt(WhereIdx);
112 if (WhereVNI && WhereVNI != FromVNI)
115 // Make sure ToReg isn't clobbered before it gets there.
116 VNInfo *ToVNI = ToLI->getVNInfoAt(WhereIdx);
117 if (ToVNI && ToVNI != FromVNI)
121 LLVM_DEBUG(dbgs() << "Setting operand " << O << " in " << *Where << " from "
125 // If the store's def was previously dead, it is no longer.
127 MI.getOperand(0).setIsDead(false);
129 Indices.push_back(WhereIdx.getRegSlot());
134 // Extend ToReg's liveness.
135 LIS.extendToIndices(*ToLI, Indices);
137 // Shrink FromReg's liveness.
138 LIS.shrinkToUses(FromLI);
140 // If we replaced all dominated uses, FromReg is now killed at MI.
141 if (!FromLI->liveAt(FromIdx.getDeadSlot()))
142 MI.addRegisterKilled(FromReg, MBB.getParent()
143 ->getSubtarget<WebAssemblySubtarget>()
150 static bool optimizeCall(MachineBasicBlock &MBB, MachineInstr &MI,
151 const MachineRegisterInfo &MRI,
152 MachineDominatorTree &MDT, LiveIntervals &LIS,
153 const WebAssemblyTargetLowering &TLI,
154 const TargetLibraryInfo &LibInfo) {
155 MachineOperand &Op1 = MI.getOperand(1);
159 StringRef Name(Op1.getSymbolName());
160 bool callReturnsInput = Name == TLI.getLibcallName(RTLIB::MEMCPY) ||
161 Name == TLI.getLibcallName(RTLIB::MEMMOVE) ||
162 Name == TLI.getLibcallName(RTLIB::MEMSET);
163 if (!callReturnsInput)
167 if (!LibInfo.getLibFunc(Name, Func))
170 unsigned FromReg = MI.getOperand(2).getReg();
171 unsigned ToReg = MI.getOperand(0).getReg();
172 if (MRI.getRegClass(FromReg) != MRI.getRegClass(ToReg))
173 report_fatal_error("Memory Intrinsic results: call to builtin function "
174 "with wrong signature, from/to mismatch");
175 return ReplaceDominatedUses(MBB, MI, FromReg, ToReg, MRI, MDT, LIS);
178 bool WebAssemblyMemIntrinsicResults::runOnMachineFunction(MachineFunction &MF) {
180 dbgs() << "********** Memory Intrinsic Results **********\n"
181 << "********** Function: " << MF.getName() << '\n';
184 MachineRegisterInfo &MRI = MF.getRegInfo();
185 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
186 const WebAssemblyTargetLowering &TLI =
187 *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
188 const auto &LibInfo = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
189 LiveIntervals &LIS = getAnalysis<LiveIntervals>();
190 bool Changed = false;
192 // We don't preserve SSA form.
195 assert(MRI.tracksLiveness() &&
196 "MemIntrinsicResults expects liveness tracking");
198 for (auto &MBB : MF) {
199 LLVM_DEBUG(dbgs() << "Basic Block: " << MBB.getName() << '\n');
201 switch (MI.getOpcode()) {
204 case WebAssembly::CALL_I32:
205 case WebAssembly::CALL_I64:
206 Changed |= optimizeCall(MBB, MI, MRI, MDT, LIS, TLI, LibInfo);