1 //===-- WebAssemblyPeephole.cpp - WebAssembly Peephole Optimiztions -------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// Late peephole optimizations for WebAssembly. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" 16 #include "WebAssembly.h" 17 #include "WebAssemblyMachineFunctionInfo.h" 18 #include "WebAssemblySubtarget.h" 19 #include "llvm/Analysis/TargetLibraryInfo.h" 20 #include "llvm/CodeGen/MachineFunctionPass.h" 21 #include "llvm/CodeGen/MachineInstrBuilder.h" 22 #include "llvm/CodeGen/MachineRegisterInfo.h" 23 using namespace llvm; 24 25 #define DEBUG_TYPE "wasm-peephole" 26 27 static cl::opt<bool> DisableWebAssemblyFallthroughReturnOpt( 28 "disable-wasm-fallthrough-return-opt", cl::Hidden, 29 cl::desc("WebAssembly: Disable fallthrough-return optimizations."), 30 cl::init(false)); 31 32 namespace { 33 class WebAssemblyPeephole final : public MachineFunctionPass { 34 StringRef getPassName() const override { 35 return "WebAssembly late peephole optimizer"; 36 } 37 38 void getAnalysisUsage(AnalysisUsage &AU) const override { 39 AU.setPreservesCFG(); 40 AU.addRequired<TargetLibraryInfoWrapperPass>(); 41 MachineFunctionPass::getAnalysisUsage(AU); 42 } 43 44 bool runOnMachineFunction(MachineFunction &MF) override; 45 46 public: 47 static char ID; 48 WebAssemblyPeephole() : MachineFunctionPass(ID) {} 49 }; 50 } // end anonymous namespace 51 52 char WebAssemblyPeephole::ID = 0; 53 INITIALIZE_PASS(WebAssemblyPeephole, DEBUG_TYPE, 54 "WebAssembly peephole optimizations", false, false) 55 56 FunctionPass *llvm::createWebAssemblyPeephole() { 57 return new WebAssemblyPeephole(); 58 } 59 60 /// If desirable, rewrite NewReg to a drop register. 61 static bool MaybeRewriteToDrop(unsigned OldReg, unsigned NewReg, 62 MachineOperand &MO, WebAssemblyFunctionInfo &MFI, 63 MachineRegisterInfo &MRI) { 64 bool Changed = false; 65 if (OldReg == NewReg) { 66 Changed = true; 67 unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg)); 68 MO.setReg(NewReg); 69 MO.setIsDead(); 70 MFI.stackifyVReg(NewReg); 71 } 72 return Changed; 73 } 74 75 static bool MaybeRewriteToFallthrough(MachineInstr &MI, MachineBasicBlock &MBB, 76 const MachineFunction &MF, 77 WebAssemblyFunctionInfo &MFI, 78 MachineRegisterInfo &MRI, 79 const WebAssemblyInstrInfo &TII, 80 unsigned FallthroughOpc, 81 unsigned CopyLocalOpc) { 82 if (DisableWebAssemblyFallthroughReturnOpt) 83 return false; 84 if (&MBB != &MF.back()) 85 return false; 86 87 MachineBasicBlock::iterator End = MBB.end(); 88 --End; 89 assert(End->getOpcode() == WebAssembly::END_FUNCTION); 90 --End; 91 if (&MI != &*End) 92 return false; 93 94 if (FallthroughOpc != WebAssembly::FALLTHROUGH_RETURN_VOID) { 95 // If the operand isn't stackified, insert a COPY to read the operand and 96 // stackify it. 97 MachineOperand &MO = MI.getOperand(0); 98 unsigned Reg = MO.getReg(); 99 if (!MFI.isVRegStackified(Reg)) { 100 unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg)); 101 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(CopyLocalOpc), NewReg) 102 .addReg(Reg); 103 MO.setReg(NewReg); 104 MFI.stackifyVReg(NewReg); 105 } 106 } 107 108 // Rewrite the return. 109 MI.setDesc(TII.get(FallthroughOpc)); 110 return true; 111 } 112 113 bool WebAssemblyPeephole::runOnMachineFunction(MachineFunction &MF) { 114 LLVM_DEBUG({ 115 dbgs() << "********** Peephole **********\n" 116 << "********** Function: " << MF.getName() << '\n'; 117 }); 118 119 MachineRegisterInfo &MRI = MF.getRegInfo(); 120 WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); 121 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); 122 const WebAssemblyTargetLowering &TLI = 123 *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering(); 124 auto &LibInfo = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); 125 bool Changed = false; 126 127 for (auto &MBB : MF) 128 for (auto &MI : MBB) 129 switch (MI.getOpcode()) { 130 default: 131 break; 132 case WebAssembly::CALL_I32: 133 case WebAssembly::CALL_I64: { 134 MachineOperand &Op1 = MI.getOperand(1); 135 if (Op1.isSymbol()) { 136 StringRef Name(Op1.getSymbolName()); 137 if (Name == TLI.getLibcallName(RTLIB::MEMCPY) || 138 Name == TLI.getLibcallName(RTLIB::MEMMOVE) || 139 Name == TLI.getLibcallName(RTLIB::MEMSET)) { 140 LibFunc Func; 141 if (LibInfo.getLibFunc(Name, Func)) { 142 const auto &Op2 = MI.getOperand(2); 143 if (!Op2.isReg()) 144 report_fatal_error("Peephole: call to builtin function with " 145 "wrong signature, not consuming reg"); 146 MachineOperand &MO = MI.getOperand(0); 147 unsigned OldReg = MO.getReg(); 148 unsigned NewReg = Op2.getReg(); 149 150 if (MRI.getRegClass(NewReg) != MRI.getRegClass(OldReg)) 151 report_fatal_error("Peephole: call to builtin function with " 152 "wrong signature, from/to mismatch"); 153 Changed |= MaybeRewriteToDrop(OldReg, NewReg, MO, MFI, MRI); 154 } 155 } 156 } 157 break; 158 } 159 // Optimize away an explicit void return at the end of the function. 160 case WebAssembly::RETURN_I32: 161 Changed |= MaybeRewriteToFallthrough( 162 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_I32, 163 WebAssembly::COPY_I32); 164 break; 165 case WebAssembly::RETURN_I64: 166 Changed |= MaybeRewriteToFallthrough( 167 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_I64, 168 WebAssembly::COPY_I64); 169 break; 170 case WebAssembly::RETURN_F32: 171 Changed |= MaybeRewriteToFallthrough( 172 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_F32, 173 WebAssembly::COPY_F32); 174 break; 175 case WebAssembly::RETURN_F64: 176 Changed |= MaybeRewriteToFallthrough( 177 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_F64, 178 WebAssembly::COPY_F64); 179 break; 180 case WebAssembly::RETURN_v16i8: 181 Changed |= MaybeRewriteToFallthrough( 182 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v16i8, 183 WebAssembly::COPY_V128); 184 break; 185 case WebAssembly::RETURN_v8i16: 186 Changed |= MaybeRewriteToFallthrough( 187 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v8i16, 188 WebAssembly::COPY_V128); 189 break; 190 case WebAssembly::RETURN_v4i32: 191 Changed |= MaybeRewriteToFallthrough( 192 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v4i32, 193 WebAssembly::COPY_V128); 194 break; 195 case WebAssembly::RETURN_v4f32: 196 Changed |= MaybeRewriteToFallthrough( 197 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v4f32, 198 WebAssembly::COPY_V128); 199 break; 200 case WebAssembly::RETURN_VOID: 201 Changed |= MaybeRewriteToFallthrough( 202 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_VOID, 203 WebAssembly::INSTRUCTION_LIST_END); 204 break; 205 } 206 207 return Changed; 208 } 209