1*0b57cec5SDimitry Andric //===-- llvm/CodeGen/GlobalISel/CSEMIRBuilder.cpp - MIBuilder--*- C++ -*-==// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric /// \file 9*0b57cec5SDimitry Andric /// This file implements the CSEMIRBuilder class which CSEs as it builds 10*0b57cec5SDimitry Andric /// instructions. 11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12*0b57cec5SDimitry Andric // 13*0b57cec5SDimitry Andric 14*0b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/CSEMIRBuilder.h" 15*0b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h" 16*0b57cec5SDimitry Andric 17*0b57cec5SDimitry Andric using namespace llvm; 18*0b57cec5SDimitry Andric 19*0b57cec5SDimitry Andric bool CSEMIRBuilder::dominates(MachineBasicBlock::const_iterator A, 20*0b57cec5SDimitry Andric MachineBasicBlock::const_iterator B) const { 21*0b57cec5SDimitry Andric auto MBBEnd = getMBB().end(); 22*0b57cec5SDimitry Andric if (B == MBBEnd) 23*0b57cec5SDimitry Andric return true; 24*0b57cec5SDimitry Andric assert(A->getParent() == B->getParent() && 25*0b57cec5SDimitry Andric "Iterators should be in same block"); 26*0b57cec5SDimitry Andric const MachineBasicBlock *BBA = A->getParent(); 27*0b57cec5SDimitry Andric MachineBasicBlock::const_iterator I = BBA->begin(); 28*0b57cec5SDimitry Andric for (; &*I != A && &*I != B; ++I) 29*0b57cec5SDimitry Andric ; 30*0b57cec5SDimitry Andric return &*I == A; 31*0b57cec5SDimitry Andric } 32*0b57cec5SDimitry Andric 33*0b57cec5SDimitry Andric MachineInstrBuilder 34*0b57cec5SDimitry Andric CSEMIRBuilder::getDominatingInstrForID(FoldingSetNodeID &ID, 35*0b57cec5SDimitry Andric void *&NodeInsertPos) { 36*0b57cec5SDimitry Andric GISelCSEInfo *CSEInfo = getCSEInfo(); 37*0b57cec5SDimitry Andric assert(CSEInfo && "Can't get here without setting CSEInfo"); 38*0b57cec5SDimitry Andric MachineBasicBlock *CurMBB = &getMBB(); 39*0b57cec5SDimitry Andric MachineInstr *MI = 40*0b57cec5SDimitry Andric CSEInfo->getMachineInstrIfExists(ID, CurMBB, NodeInsertPos); 41*0b57cec5SDimitry Andric if (MI) { 42*0b57cec5SDimitry Andric CSEInfo->countOpcodeHit(MI->getOpcode()); 43*0b57cec5SDimitry Andric auto CurrPos = getInsertPt(); 44*0b57cec5SDimitry Andric if (!dominates(MI, CurrPos)) 45*0b57cec5SDimitry Andric CurMBB->splice(CurrPos, CurMBB, MI); 46*0b57cec5SDimitry Andric return MachineInstrBuilder(getMF(), MI); 47*0b57cec5SDimitry Andric } 48*0b57cec5SDimitry Andric return MachineInstrBuilder(); 49*0b57cec5SDimitry Andric } 50*0b57cec5SDimitry Andric 51*0b57cec5SDimitry Andric bool CSEMIRBuilder::canPerformCSEForOpc(unsigned Opc) const { 52*0b57cec5SDimitry Andric const GISelCSEInfo *CSEInfo = getCSEInfo(); 53*0b57cec5SDimitry Andric if (!CSEInfo || !CSEInfo->shouldCSE(Opc)) 54*0b57cec5SDimitry Andric return false; 55*0b57cec5SDimitry Andric return true; 56*0b57cec5SDimitry Andric } 57*0b57cec5SDimitry Andric 58*0b57cec5SDimitry Andric void CSEMIRBuilder::profileDstOp(const DstOp &Op, 59*0b57cec5SDimitry Andric GISelInstProfileBuilder &B) const { 60*0b57cec5SDimitry Andric switch (Op.getDstOpKind()) { 61*0b57cec5SDimitry Andric case DstOp::DstType::Ty_RC: 62*0b57cec5SDimitry Andric B.addNodeIDRegType(Op.getRegClass()); 63*0b57cec5SDimitry Andric break; 64*0b57cec5SDimitry Andric default: 65*0b57cec5SDimitry Andric B.addNodeIDRegType(Op.getLLTTy(*getMRI())); 66*0b57cec5SDimitry Andric break; 67*0b57cec5SDimitry Andric } 68*0b57cec5SDimitry Andric } 69*0b57cec5SDimitry Andric 70*0b57cec5SDimitry Andric void CSEMIRBuilder::profileSrcOp(const SrcOp &Op, 71*0b57cec5SDimitry Andric GISelInstProfileBuilder &B) const { 72*0b57cec5SDimitry Andric switch (Op.getSrcOpKind()) { 73*0b57cec5SDimitry Andric case SrcOp::SrcType::Ty_Predicate: 74*0b57cec5SDimitry Andric B.addNodeIDImmediate(static_cast<int64_t>(Op.getPredicate())); 75*0b57cec5SDimitry Andric break; 76*0b57cec5SDimitry Andric default: 77*0b57cec5SDimitry Andric B.addNodeIDRegType(Op.getReg()); 78*0b57cec5SDimitry Andric break; 79*0b57cec5SDimitry Andric } 80*0b57cec5SDimitry Andric } 81*0b57cec5SDimitry Andric 82*0b57cec5SDimitry Andric void CSEMIRBuilder::profileMBBOpcode(GISelInstProfileBuilder &B, 83*0b57cec5SDimitry Andric unsigned Opc) const { 84*0b57cec5SDimitry Andric // First add the MBB (Local CSE). 85*0b57cec5SDimitry Andric B.addNodeIDMBB(&getMBB()); 86*0b57cec5SDimitry Andric // Then add the opcode. 87*0b57cec5SDimitry Andric B.addNodeIDOpcode(Opc); 88*0b57cec5SDimitry Andric } 89*0b57cec5SDimitry Andric 90*0b57cec5SDimitry Andric void CSEMIRBuilder::profileEverything(unsigned Opc, ArrayRef<DstOp> DstOps, 91*0b57cec5SDimitry Andric ArrayRef<SrcOp> SrcOps, 92*0b57cec5SDimitry Andric Optional<unsigned> Flags, 93*0b57cec5SDimitry Andric GISelInstProfileBuilder &B) const { 94*0b57cec5SDimitry Andric 95*0b57cec5SDimitry Andric profileMBBOpcode(B, Opc); 96*0b57cec5SDimitry Andric // Then add the DstOps. 97*0b57cec5SDimitry Andric profileDstOps(DstOps, B); 98*0b57cec5SDimitry Andric // Then add the SrcOps. 99*0b57cec5SDimitry Andric profileSrcOps(SrcOps, B); 100*0b57cec5SDimitry Andric // Add Flags if passed in. 101*0b57cec5SDimitry Andric if (Flags) 102*0b57cec5SDimitry Andric B.addNodeIDFlag(*Flags); 103*0b57cec5SDimitry Andric } 104*0b57cec5SDimitry Andric 105*0b57cec5SDimitry Andric MachineInstrBuilder CSEMIRBuilder::memoizeMI(MachineInstrBuilder MIB, 106*0b57cec5SDimitry Andric void *NodeInsertPos) { 107*0b57cec5SDimitry Andric assert(canPerformCSEForOpc(MIB->getOpcode()) && 108*0b57cec5SDimitry Andric "Attempting to CSE illegal op"); 109*0b57cec5SDimitry Andric MachineInstr *MIBInstr = MIB; 110*0b57cec5SDimitry Andric getCSEInfo()->insertInstr(MIBInstr, NodeInsertPos); 111*0b57cec5SDimitry Andric return MIB; 112*0b57cec5SDimitry Andric } 113*0b57cec5SDimitry Andric 114*0b57cec5SDimitry Andric bool CSEMIRBuilder::checkCopyToDefsPossible(ArrayRef<DstOp> DstOps) { 115*0b57cec5SDimitry Andric if (DstOps.size() == 1) 116*0b57cec5SDimitry Andric return true; // always possible to emit copy to just 1 vreg. 117*0b57cec5SDimitry Andric 118*0b57cec5SDimitry Andric return std::all_of(DstOps.begin(), DstOps.end(), [](const DstOp &Op) { 119*0b57cec5SDimitry Andric DstOp::DstType DT = Op.getDstOpKind(); 120*0b57cec5SDimitry Andric return DT == DstOp::DstType::Ty_LLT || DT == DstOp::DstType::Ty_RC; 121*0b57cec5SDimitry Andric }); 122*0b57cec5SDimitry Andric } 123*0b57cec5SDimitry Andric 124*0b57cec5SDimitry Andric MachineInstrBuilder 125*0b57cec5SDimitry Andric CSEMIRBuilder::generateCopiesIfRequired(ArrayRef<DstOp> DstOps, 126*0b57cec5SDimitry Andric MachineInstrBuilder &MIB) { 127*0b57cec5SDimitry Andric assert(checkCopyToDefsPossible(DstOps) && 128*0b57cec5SDimitry Andric "Impossible return a single MIB with copies to multiple defs"); 129*0b57cec5SDimitry Andric if (DstOps.size() == 1) { 130*0b57cec5SDimitry Andric const DstOp &Op = DstOps[0]; 131*0b57cec5SDimitry Andric if (Op.getDstOpKind() == DstOp::DstType::Ty_Reg) 132*0b57cec5SDimitry Andric return buildCopy(Op.getReg(), MIB->getOperand(0).getReg()); 133*0b57cec5SDimitry Andric } 134*0b57cec5SDimitry Andric return MIB; 135*0b57cec5SDimitry Andric } 136*0b57cec5SDimitry Andric 137*0b57cec5SDimitry Andric MachineInstrBuilder CSEMIRBuilder::buildInstr(unsigned Opc, 138*0b57cec5SDimitry Andric ArrayRef<DstOp> DstOps, 139*0b57cec5SDimitry Andric ArrayRef<SrcOp> SrcOps, 140*0b57cec5SDimitry Andric Optional<unsigned> Flag) { 141*0b57cec5SDimitry Andric switch (Opc) { 142*0b57cec5SDimitry Andric default: 143*0b57cec5SDimitry Andric break; 144*0b57cec5SDimitry Andric case TargetOpcode::G_ADD: 145*0b57cec5SDimitry Andric case TargetOpcode::G_AND: 146*0b57cec5SDimitry Andric case TargetOpcode::G_ASHR: 147*0b57cec5SDimitry Andric case TargetOpcode::G_LSHR: 148*0b57cec5SDimitry Andric case TargetOpcode::G_MUL: 149*0b57cec5SDimitry Andric case TargetOpcode::G_OR: 150*0b57cec5SDimitry Andric case TargetOpcode::G_SHL: 151*0b57cec5SDimitry Andric case TargetOpcode::G_SUB: 152*0b57cec5SDimitry Andric case TargetOpcode::G_XOR: 153*0b57cec5SDimitry Andric case TargetOpcode::G_UDIV: 154*0b57cec5SDimitry Andric case TargetOpcode::G_SDIV: 155*0b57cec5SDimitry Andric case TargetOpcode::G_UREM: 156*0b57cec5SDimitry Andric case TargetOpcode::G_SREM: { 157*0b57cec5SDimitry Andric // Try to constant fold these. 158*0b57cec5SDimitry Andric assert(SrcOps.size() == 2 && "Invalid sources"); 159*0b57cec5SDimitry Andric assert(DstOps.size() == 1 && "Invalid dsts"); 160*0b57cec5SDimitry Andric if (Optional<APInt> Cst = ConstantFoldBinOp(Opc, SrcOps[0].getReg(), 161*0b57cec5SDimitry Andric SrcOps[1].getReg(), *getMRI())) 162*0b57cec5SDimitry Andric return buildConstant(DstOps[0], Cst->getSExtValue()); 163*0b57cec5SDimitry Andric break; 164*0b57cec5SDimitry Andric } 165*0b57cec5SDimitry Andric } 166*0b57cec5SDimitry Andric bool CanCopy = checkCopyToDefsPossible(DstOps); 167*0b57cec5SDimitry Andric if (!canPerformCSEForOpc(Opc)) 168*0b57cec5SDimitry Andric return MachineIRBuilder::buildInstr(Opc, DstOps, SrcOps, Flag); 169*0b57cec5SDimitry Andric // If we can CSE this instruction, but involves generating copies to multiple 170*0b57cec5SDimitry Andric // regs, give up. This frequently happens to UNMERGEs. 171*0b57cec5SDimitry Andric if (!CanCopy) { 172*0b57cec5SDimitry Andric auto MIB = MachineIRBuilder::buildInstr(Opc, DstOps, SrcOps, Flag); 173*0b57cec5SDimitry Andric // CSEInfo would have tracked this instruction. Remove it from the temporary 174*0b57cec5SDimitry Andric // insts. 175*0b57cec5SDimitry Andric getCSEInfo()->handleRemoveInst(&*MIB); 176*0b57cec5SDimitry Andric return MIB; 177*0b57cec5SDimitry Andric } 178*0b57cec5SDimitry Andric FoldingSetNodeID ID; 179*0b57cec5SDimitry Andric GISelInstProfileBuilder ProfBuilder(ID, *getMRI()); 180*0b57cec5SDimitry Andric void *InsertPos = nullptr; 181*0b57cec5SDimitry Andric profileEverything(Opc, DstOps, SrcOps, Flag, ProfBuilder); 182*0b57cec5SDimitry Andric MachineInstrBuilder MIB = getDominatingInstrForID(ID, InsertPos); 183*0b57cec5SDimitry Andric if (MIB) { 184*0b57cec5SDimitry Andric // Handle generating copies here. 185*0b57cec5SDimitry Andric return generateCopiesIfRequired(DstOps, MIB); 186*0b57cec5SDimitry Andric } 187*0b57cec5SDimitry Andric // This instruction does not exist in the CSEInfo. Build it and CSE it. 188*0b57cec5SDimitry Andric MachineInstrBuilder NewMIB = 189*0b57cec5SDimitry Andric MachineIRBuilder::buildInstr(Opc, DstOps, SrcOps, Flag); 190*0b57cec5SDimitry Andric return memoizeMI(NewMIB, InsertPos); 191*0b57cec5SDimitry Andric } 192*0b57cec5SDimitry Andric 193*0b57cec5SDimitry Andric MachineInstrBuilder CSEMIRBuilder::buildConstant(const DstOp &Res, 194*0b57cec5SDimitry Andric const ConstantInt &Val) { 195*0b57cec5SDimitry Andric constexpr unsigned Opc = TargetOpcode::G_CONSTANT; 196*0b57cec5SDimitry Andric if (!canPerformCSEForOpc(Opc)) 197*0b57cec5SDimitry Andric return MachineIRBuilder::buildConstant(Res, Val); 198*0b57cec5SDimitry Andric 199*0b57cec5SDimitry Andric // For vectors, CSE the element only for now. 200*0b57cec5SDimitry Andric LLT Ty = Res.getLLTTy(*getMRI()); 201*0b57cec5SDimitry Andric if (Ty.isVector()) 202*0b57cec5SDimitry Andric return buildSplatVector(Res, buildConstant(Ty.getElementType(), Val)); 203*0b57cec5SDimitry Andric 204*0b57cec5SDimitry Andric FoldingSetNodeID ID; 205*0b57cec5SDimitry Andric GISelInstProfileBuilder ProfBuilder(ID, *getMRI()); 206*0b57cec5SDimitry Andric void *InsertPos = nullptr; 207*0b57cec5SDimitry Andric profileMBBOpcode(ProfBuilder, Opc); 208*0b57cec5SDimitry Andric profileDstOp(Res, ProfBuilder); 209*0b57cec5SDimitry Andric ProfBuilder.addNodeIDMachineOperand(MachineOperand::CreateCImm(&Val)); 210*0b57cec5SDimitry Andric MachineInstrBuilder MIB = getDominatingInstrForID(ID, InsertPos); 211*0b57cec5SDimitry Andric if (MIB) { 212*0b57cec5SDimitry Andric // Handle generating copies here. 213*0b57cec5SDimitry Andric return generateCopiesIfRequired({Res}, MIB); 214*0b57cec5SDimitry Andric } 215*0b57cec5SDimitry Andric 216*0b57cec5SDimitry Andric MachineInstrBuilder NewMIB = MachineIRBuilder::buildConstant(Res, Val); 217*0b57cec5SDimitry Andric return memoizeMI(NewMIB, InsertPos); 218*0b57cec5SDimitry Andric } 219*0b57cec5SDimitry Andric 220*0b57cec5SDimitry Andric MachineInstrBuilder CSEMIRBuilder::buildFConstant(const DstOp &Res, 221*0b57cec5SDimitry Andric const ConstantFP &Val) { 222*0b57cec5SDimitry Andric constexpr unsigned Opc = TargetOpcode::G_FCONSTANT; 223*0b57cec5SDimitry Andric if (!canPerformCSEForOpc(Opc)) 224*0b57cec5SDimitry Andric return MachineIRBuilder::buildFConstant(Res, Val); 225*0b57cec5SDimitry Andric 226*0b57cec5SDimitry Andric // For vectors, CSE the element only for now. 227*0b57cec5SDimitry Andric LLT Ty = Res.getLLTTy(*getMRI()); 228*0b57cec5SDimitry Andric if (Ty.isVector()) 229*0b57cec5SDimitry Andric return buildSplatVector(Res, buildFConstant(Ty.getElementType(), Val)); 230*0b57cec5SDimitry Andric 231*0b57cec5SDimitry Andric FoldingSetNodeID ID; 232*0b57cec5SDimitry Andric GISelInstProfileBuilder ProfBuilder(ID, *getMRI()); 233*0b57cec5SDimitry Andric void *InsertPos = nullptr; 234*0b57cec5SDimitry Andric profileMBBOpcode(ProfBuilder, Opc); 235*0b57cec5SDimitry Andric profileDstOp(Res, ProfBuilder); 236*0b57cec5SDimitry Andric ProfBuilder.addNodeIDMachineOperand(MachineOperand::CreateFPImm(&Val)); 237*0b57cec5SDimitry Andric MachineInstrBuilder MIB = getDominatingInstrForID(ID, InsertPos); 238*0b57cec5SDimitry Andric if (MIB) { 239*0b57cec5SDimitry Andric // Handle generating copies here. 240*0b57cec5SDimitry Andric return generateCopiesIfRequired({Res}, MIB); 241*0b57cec5SDimitry Andric } 242*0b57cec5SDimitry Andric MachineInstrBuilder NewMIB = MachineIRBuilder::buildFConstant(Res, Val); 243*0b57cec5SDimitry Andric return memoizeMI(NewMIB, InsertPos); 244*0b57cec5SDimitry Andric } 245