From bd80a8bb87617a25a1fda0338db3b26637b273a0 Mon Sep 17 00:00:00 2001 From: Jay Foad Date: Fri, 17 Apr 2020 14:36:01 +0100 Subject: [PATCH] [GlobalISel][IRTranslator] New helper function translateCopy. NFC. Reviewers: arsenm, volkan, t.p.northover, aditya_nandakumar Subscribers: wdng, rovka, hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D78377 --- .../include/llvm/CodeGen/GlobalISel/IRTranslator.h | 4 ++ llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp | 58 +++++++++------------- 2 files changed, 27 insertions(+), 35 deletions(-) diff --git a/llvm/include/llvm/CodeGen/GlobalISel/IRTranslator.h b/llvm/include/llvm/CodeGen/GlobalISel/IRTranslator.h index 43ad83b..19cb363 100644 --- a/llvm/include/llvm/CodeGen/GlobalISel/IRTranslator.h +++ b/llvm/include/llvm/CodeGen/GlobalISel/IRTranslator.h @@ -202,6 +202,10 @@ private: /// \return true if the materialization succeeded. bool translate(const Constant &C, Register Reg); + // Translate U as a copy of V. + bool translateCopy(const User &U, const Value &V, + MachineIRBuilder &MIRBuilder); + /// Translate an LLVM bitcast into generic IR. Either a COPY or a G_BITCAST is /// emitted. bool translateBitCast(const User &U, MachineIRBuilder &MIRBuilder); diff --git a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp index 230f6d6..4a2cbbb 100644 --- a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp +++ b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp @@ -1022,23 +1022,28 @@ bool IRTranslator::translateSelect(const User &U, return true; } +bool IRTranslator::translateCopy(const User &U, const Value &V, + MachineIRBuilder &MIRBuilder) { + Register Src = getOrCreateVReg(V); + auto &Regs = *VMap.getVRegs(U); + if (Regs.empty()) { + Regs.push_back(Src); + VMap.getOffsets(U)->push_back(0); + } else { + // If we already assigned a vreg for this instruction, we can't change that. + // Emit a copy to satisfy the users we already emitted. + MIRBuilder.buildCopy(Regs[0], Src); + } + return true; +} + bool IRTranslator::translateBitCast(const User &U, MachineIRBuilder &MIRBuilder) { // If we're bitcasting to the source type, we can reuse the source vreg. if (getLLTForType(*U.getOperand(0)->getType(), *DL) == - getLLTForType(*U.getType(), *DL)) { - Register SrcReg = getOrCreateVReg(*U.getOperand(0)); - auto &Regs = *VMap.getVRegs(U); - // If we already assigned a vreg for this bitcast, we can't change that. - // Emit a copy to satisfy the users we already emitted. - if (!Regs.empty()) - MIRBuilder.buildCopy(Regs[0], SrcReg); - else { - Regs.push_back(SrcReg); - VMap.getOffsets(U)->push_back(0); - } - return true; - } + getLLTForType(*U.getType(), *DL)) + return translateCopy(U, *U.getOperand(0), MIRBuilder); + return translateCast(TargetOpcode::G_BITCAST, U, MIRBuilder); } @@ -1897,17 +1902,8 @@ bool IRTranslator::translateInsertElement(const User &U, MachineIRBuilder &MIRBuilder) { // If it is a <1 x Ty> vector, use the scalar as it is // not a legal vector type in LLT. - if (cast(U.getType())->getNumElements() == 1) { - Register Elt = getOrCreateVReg(*U.getOperand(1)); - auto &Regs = *VMap.getVRegs(U); - if (Regs.empty()) { - Regs.push_back(Elt); - VMap.getOffsets(U)->push_back(0); - } else { - MIRBuilder.buildCopy(Regs[0], Elt); - } - return true; - } + if (cast(U.getType())->getNumElements() == 1) + return translateCopy(U, *U.getOperand(1), MIRBuilder); Register Res = getOrCreateVReg(U); Register Val = getOrCreateVReg(*U.getOperand(0)); @@ -1921,17 +1917,9 @@ bool IRTranslator::translateExtractElement(const User &U, MachineIRBuilder &MIRBuilder) { // If it is a <1 x Ty> vector, use the scalar as it is // not a legal vector type in LLT. - if (cast(U.getOperand(0)->getType())->getNumElements() == 1) { - Register Elt = getOrCreateVReg(*U.getOperand(0)); - auto &Regs = *VMap.getVRegs(U); - if (Regs.empty()) { - Regs.push_back(Elt); - VMap.getOffsets(U)->push_back(0); - } else { - MIRBuilder.buildCopy(Regs[0], Elt); - } - return true; - } + if (cast(U.getOperand(0)->getType())->getNumElements() == 1) + return translateCopy(U, *U.getOperand(0), MIRBuilder); + Register Res = getOrCreateVReg(U); Register Val = getOrCreateVReg(*U.getOperand(0)); const auto &TLI = *MF->getSubtarget().getTargetLowering(); -- 2.7.4