1 //===-- SILowerI1Copies.cpp - Lower I1 Copies -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 /// i1 values are usually inserted by the CFG Structurize pass and they are
9 /// unique in that they can be copied from VALU to SALU registers.
10 /// This is not possible for any other value type. Since there are no
11 /// MOV instructions for i1, we to use V_CMP_* and V_CNDMASK to move the i1.
13 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "si-i1-copies"
18 #include "AMDGPUSubtarget.h"
19 #include "SIInstrInfo.h"
20 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
21 #include "llvm/CodeGen/MachineDominators.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Target/TargetMachine.h"
34 class SILowerI1Copies : public MachineFunctionPass {
39 SILowerI1Copies() : MachineFunctionPass(ID) {
40 initializeSILowerI1CopiesPass(*PassRegistry::getPassRegistry());
43 bool runOnMachineFunction(MachineFunction &MF) override;
45 const char *getPassName() const override {
46 return "SI Lower i1 Copies";
49 void getAnalysisUsage(AnalysisUsage &AU) const override {
50 AU.addRequired<MachineDominatorTree>();
51 AU.addPreserved<MachineDominatorTree>();
53 MachineFunctionPass::getAnalysisUsage(AU);
57 } // End anonymous namespace.
59 INITIALIZE_PASS_BEGIN(SILowerI1Copies, DEBUG_TYPE,
60 "SI Lower i1 Copies", false, false)
61 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
62 INITIALIZE_PASS_END(SILowerI1Copies, DEBUG_TYPE,
63 "SI Lower i1 Copies", false, false)
65 char SILowerI1Copies::ID = 0;
67 char &llvm::SILowerI1CopiesID = SILowerI1Copies::ID;
69 FunctionPass *llvm::createSILowerI1CopiesPass() {
70 return new SILowerI1Copies();
73 bool SILowerI1Copies::runOnMachineFunction(MachineFunction &MF) {
74 MachineRegisterInfo &MRI = MF.getRegInfo();
75 const SIInstrInfo *TII =
76 static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
77 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
78 std::vector<unsigned> I1Defs;
80 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
83 MachineBasicBlock &MBB = *BI;
84 MachineBasicBlock::iterator I, Next;
85 for (I = MBB.begin(); I != MBB.end(); I = Next) {
87 MachineInstr &MI = *I;
89 if (MI.getOpcode() == AMDGPU::IMPLICIT_DEF) {
90 unsigned Reg = MI.getOperand(0).getReg();
91 const TargetRegisterClass *RC = MRI.getRegClass(Reg);
92 if (RC == &AMDGPU::VReg_1RegClass)
93 MRI.setRegClass(Reg, &AMDGPU::SReg_64RegClass);
97 if (MI.getOpcode() != AMDGPU::COPY)
100 const MachineOperand &Dst = MI.getOperand(0);
101 const MachineOperand &Src = MI.getOperand(1);
103 if (!TargetRegisterInfo::isVirtualRegister(Src.getReg()) ||
104 !TargetRegisterInfo::isVirtualRegister(Dst.getReg()))
107 const TargetRegisterClass *DstRC = MRI.getRegClass(Dst.getReg());
108 const TargetRegisterClass *SrcRC = MRI.getRegClass(Src.getReg());
110 if (DstRC == &AMDGPU::VReg_1RegClass &&
111 TRI->getCommonSubClass(SrcRC, &AMDGPU::SGPR_64RegClass)) {
112 I1Defs.push_back(Dst.getReg());
113 DebugLoc DL = MI.getDebugLoc();
115 MachineInstr *DefInst = MRI.getUniqueVRegDef(Src.getReg());
116 if (DefInst->getOpcode() == AMDGPU::S_MOV_B64) {
117 if (DefInst->getOperand(1).isImm()) {
118 I1Defs.push_back(Dst.getReg());
120 int64_t Val = DefInst->getOperand(1).getImm();
121 assert(Val == 0 || Val == -1);
123 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_MOV_B32_e32))
126 MI.eraseFromParent();
131 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64))
136 MI.eraseFromParent();
137 } else if (TRI->getCommonSubClass(DstRC, &AMDGPU::SGPR_64RegClass) &&
138 SrcRC == &AMDGPU::VReg_1RegClass) {
139 BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(AMDGPU::V_CMP_NE_I32_e64))
143 MI.eraseFromParent();
148 for (unsigned Reg : I1Defs)
149 MRI.setRegClass(Reg, &AMDGPU::VGPR_32RegClass);