1 //== ---lib/CodeGen/GlobalISel/GICombinerHelper.cpp --------------------- == // 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 #include "llvm/CodeGen/GlobalISel/CombinerHelper.h" 10 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" 11 #include "llvm/CodeGen/GlobalISel/Utils.h" 12 #include "llvm/CodeGen/MachineInstr.h" 13 #include "llvm/CodeGen/MachineRegisterInfo.h" 14 15 #define DEBUG_TYPE "gi-combine" 16 17 using namespace llvm; 18 CombinerHelper(MachineIRBuilder & B)19CombinerHelper::CombinerHelper(MachineIRBuilder &B) : 20 Builder(B), MRI(Builder.getMF().getRegInfo()) {} 21 tryCombineCopy(MachineInstr & MI)22bool CombinerHelper::tryCombineCopy(MachineInstr &MI) { 23 if (MI.getOpcode() != TargetOpcode::COPY) 24 return false; 25 unsigned DstReg = MI.getOperand(0).getReg(); 26 unsigned SrcReg = MI.getOperand(1).getReg(); 27 LLT DstTy = MRI.getType(DstReg); 28 LLT SrcTy = MRI.getType(SrcReg); 29 // Simple Copy Propagation. 30 // a(sx) = COPY b(sx) -> Replace all uses of a with b. 31 if (DstTy.isValid() && SrcTy.isValid() && DstTy == SrcTy) { 32 MI.eraseFromParent(); 33 MRI.replaceRegWith(DstReg, SrcReg); 34 return true; 35 } 36 return false; 37 } 38 tryCombine(MachineInstr & MI)39bool CombinerHelper::tryCombine(MachineInstr &MI) { 40 return tryCombineCopy(MI); 41 } 42