|
| 1 | +//===----- EVMConstantSpiller.cpp - Spill constants to memory --*- C++ -*--===// |
| 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 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +#include "EVMConstantSpiller.h" |
| 13 | +#include "EVMInstrInfo.h" |
| 14 | +#include "EVMSubtarget.h" |
| 15 | +#include "MCTargetDesc/EVMMCTargetDesc.h" |
| 16 | +#include "TargetInfo/EVMTargetInfo.h" |
| 17 | +#include "llvm/CodeGen/MachineModuleInfo.h" |
| 18 | +#include "llvm/CodeGen/Passes.h" |
| 19 | +#include "llvm/CodeGen/TargetInstrInfo.h" |
| 20 | +#include "llvm/CodeGen/TargetSubtargetInfo.h" |
| 21 | +#include "llvm/IR/Module.h" |
| 22 | + |
| 23 | +using namespace llvm; |
| 24 | + |
| 25 | +#define DEBUG_TYPE "evm-spill-constants" |
| 26 | + |
| 27 | +constexpr uint64_t SpillSlotSize = 32; |
| 28 | + |
| 29 | +static cl::opt<unsigned> ConstantSpillThreshold( |
| 30 | + "evm-constant-spill-threshold", cl::Hidden, cl::init(30), |
| 31 | + cl::desc("Minimum number of uses of a constant across the module required " |
| 32 | + "before spilling it to memory is considered profitable")); |
| 33 | + |
| 34 | +static MachineInstr *emitPush(MachineBasicBlock &MBB, |
| 35 | + MachineBasicBlock::iterator InsertBefore, |
| 36 | + const EVMInstrInfo *TII, const APInt &Imm, |
| 37 | + LLVMContext &Ctx, const DebugLoc &DL) { |
| 38 | + unsigned Opc = EVM::getPUSHOpcode(Imm); |
| 39 | + auto MI = BuildMI(MBB, InsertBefore, DL, TII->get(EVM::getStackOpcode(Opc))); |
| 40 | + if (Opc != EVM::PUSH0) |
| 41 | + MI.addCImm(ConstantInt::get(Ctx, Imm)); |
| 42 | + return MI; |
| 43 | +} |
| 44 | + |
| 45 | +uint64_t EVMConstantSpiller::getSpillSize() const { |
| 46 | + return ConstantUseCount.size() * SpillSlotSize; |
| 47 | +} |
| 48 | + |
| 49 | +void EVMConstantSpiller::emitConstantSpills(uint64_t SpillOffset, |
| 50 | + MachineFunction &EntryMF) { |
| 51 | + LLVMContext &Ctx = EntryMF.getFunction().getContext(); |
| 52 | + const EVMInstrInfo *TII = EntryMF.getSubtarget<EVMSubtarget>().getInstrInfo(); |
| 53 | + |
| 54 | + DenseMap<APInt, uint64_t> ConstantToSpillOffset; |
| 55 | + for (const auto &KV : ConstantUseCount) { |
| 56 | + ConstantToSpillOffset[KV.first] = SpillOffset; |
| 57 | + SpillOffset += SpillSlotSize; |
| 58 | + } |
| 59 | + |
| 60 | + // Emit constant stores in prologue of the '__entry' function. |
| 61 | + MachineBasicBlock &SpillMBB = EntryMF.front(); |
| 62 | + auto InsertBefore = SpillMBB.begin(); |
| 63 | + for (const auto &[Imm, Offset] : ConstantToSpillOffset) { |
| 64 | + LLVM_DEBUG({ |
| 65 | + dbgs() << "Spilling constant: " << Imm |
| 66 | + << ", number of uses: " << ConstantUseCount.at(Imm) |
| 67 | + << ", at offset: " << Offset << '\n'; |
| 68 | + }); |
| 69 | + |
| 70 | + BuildMI(SpillMBB, InsertBefore, DebugLoc(), TII->get(EVM::MSTORE_S)); |
| 71 | + emitPush(SpillMBB, InsertBefore, TII, APInt(256, Offset), Ctx, DebugLoc()); |
| 72 | + emitPush(SpillMBB, InsertBefore, TII, Imm, Ctx, DebugLoc()); |
| 73 | + } |
| 74 | + |
| 75 | + // Reload spilled constants. |
| 76 | + for (MachineInstr *MI : ReloadCandidates) { |
| 77 | + const APInt Imm = MI->getOperand(0).getCImm()->getValue().zext(256); |
| 78 | + uint64_t Offset = ConstantToSpillOffset.at(Imm); |
| 79 | + |
| 80 | + MachineBasicBlock *MBB = MI->getParent(); |
| 81 | + emitPush(*MBB, MI, TII, APInt(256, Offset), Ctx, MI->getDebugLoc()); |
| 82 | + auto Load = BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(EVM::MLOAD_S)); |
| 83 | + Load->setAsmPrinterFlag(MachineInstr::ReloadReuse); |
| 84 | + MI->eraseFromParent(); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +static bool shouldSkip(const MachineInstr &MI) { |
| 89 | + if (!EVMInstrInfo::isPush(&MI) || (MI.getOpcode() == EVM::PUSH0_S)) |
| 90 | + return true; |
| 91 | + |
| 92 | + const APInt Imm = MI.getOperand(0).getCImm()->getValue(); |
| 93 | + return Imm.getActiveBits() < 8 * 8; |
| 94 | +} |
| 95 | + |
| 96 | +void EVMConstantSpiller::analyzeModule(Module &M, MachineModuleInfo &MMI) { |
| 97 | + for (Function &F : M) { |
| 98 | + MachineFunction *MF = MMI.getMachineFunction(F); |
| 99 | + if (!MF) |
| 100 | + continue; |
| 101 | + |
| 102 | + for (MachineBasicBlock &MBB : *MF) { |
| 103 | + for (MachineInstr &MI : MBB) { |
| 104 | + if (shouldSkip(MI)) |
| 105 | + continue; |
| 106 | + |
| 107 | + const APInt Imm = MI.getOperand(0).getCImm()->getValue().zext(256); |
| 108 | + if (Imm.isAllOnes()) |
| 109 | + continue; |
| 110 | + |
| 111 | + ConstantUseCount[Imm]++; |
| 112 | + ReloadCandidates.push_back(&MI); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +void EVMConstantSpiller::filterCandidates() { |
| 119 | + SmallVector<APInt> ImmToRemove; |
| 120 | + for (const auto &[Imm, NumUses] : ConstantUseCount) |
| 121 | + if (NumUses < ConstantSpillThreshold) |
| 122 | + ImmToRemove.push_back(Imm); |
| 123 | + |
| 124 | + for (const APInt &Imm : ImmToRemove) |
| 125 | + ConstantUseCount.erase(Imm); |
| 126 | + |
| 127 | + erase_if(ReloadCandidates, [this](const MachineInstr *MI) { |
| 128 | + const APInt &Imm = MI->getOperand(0).getCImm()->getValue().zext(256); |
| 129 | + return !ConstantUseCount.contains(Imm); |
| 130 | + }); |
| 131 | +} |
| 132 | + |
| 133 | +EVMConstantSpiller::EVMConstantSpiller(Module &M, MachineModuleInfo &MMI) { |
| 134 | + analyzeModule(M, MMI); |
| 135 | + filterCandidates(); |
| 136 | +} |
0 commit comments