blob: db19235995befcf62ff839443f32ac73980a420b [file] [log] [blame]
Tom Stellard1bd80722014-04-30 15:31:33 +00001//===-- SILowerI1Copies.cpp - Lower I1 Copies -----------------------------===//
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/// 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.
12///
13//===----------------------------------------------------------------------===//
14//
15
16#define DEBUG_TYPE "si-i1-copies"
17#include "AMDGPU.h"
18#include "SIInstrInfo.h"
19#include "llvm/CodeGen/LiveIntervalAnalysis.h"
20#include "llvm/CodeGen/MachineDominators.h"
21#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/IR/LLVMContext.h"
25#include "llvm/IR/Function.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Target/TargetMachine.h"
28
29using namespace llvm;
30
31namespace {
32
33class SILowerI1Copies : public MachineFunctionPass {
34public:
35 static char ID;
36
37public:
38 SILowerI1Copies() : MachineFunctionPass(ID) {
39 initializeSILowerI1CopiesPass(*PassRegistry::getPassRegistry());
40 }
41
42 virtual bool runOnMachineFunction(MachineFunction &MF) override;
43
44 virtual const char *getPassName() const override {
45 return "SI Lower il Copies";
46 }
47
48 virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
49 AU.addRequired<MachineDominatorTree>();
50 AU.setPreservesCFG();
51 MachineFunctionPass::getAnalysisUsage(AU);
52 }
53};
54
55} // End anonymous namespace.
56
57INITIALIZE_PASS_BEGIN(SILowerI1Copies, DEBUG_TYPE,
58 "SI Lower il Copies", false, false)
59INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
60INITIALIZE_PASS_END(SILowerI1Copies, DEBUG_TYPE,
61 "SI Lower il Copies", false, false)
62
63char SILowerI1Copies::ID = 0;
64
65char &llvm::SILowerI1CopiesID = SILowerI1Copies::ID;
66
67FunctionPass *llvm::createSILowerI1CopiesPass() {
68 return new SILowerI1Copies();
69}
70
71bool SILowerI1Copies::runOnMachineFunction(MachineFunction &MF) {
72 MachineRegisterInfo &MRI = MF.getRegInfo();
73 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
74 MF.getTarget().getInstrInfo());
75 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
Tom Stellard365a2b42014-05-15 14:41:50 +000076 std::vector<unsigned> I1Defs;
Tom Stellard1bd80722014-04-30 15:31:33 +000077
78 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
79 BI != BE; ++BI) {
80
81 MachineBasicBlock &MBB = *BI;
82 MachineBasicBlock::iterator I, Next;
83 for (I = MBB.begin(); I != MBB.end(); I = Next) {
84 Next = std::next(I);
85 MachineInstr &MI = *I;
86
87 if (MI.getOpcode() == AMDGPU::V_MOV_I1) {
Tom Stellard365a2b42014-05-15 14:41:50 +000088 I1Defs.push_back(MI.getOperand(0).getReg());
Tom Stellard1bd80722014-04-30 15:31:33 +000089 MI.setDesc(TII->get(AMDGPU::V_MOV_B32_e32));
90 continue;
91 }
92
Tom Stellard365a2b42014-05-15 14:41:50 +000093 if (MI.getOpcode() == AMDGPU::V_AND_I1) {
94 I1Defs.push_back(MI.getOperand(0).getReg());
95 MI.setDesc(TII->get(AMDGPU::V_AND_B32_e32));
96 continue;
97 }
98
99 if (MI.getOpcode() == AMDGPU::V_OR_I1) {
100 I1Defs.push_back(MI.getOperand(0).getReg());
101 MI.setDesc(TII->get(AMDGPU::V_OR_B32_e32));
102 continue;
103 }
104
Tom Stellard54a3b652014-07-21 14:01:10 +0000105 if (MI.getOpcode() == AMDGPU::V_XOR_I1) {
106 I1Defs.push_back(MI.getOperand(0).getReg());
107 MI.setDesc(TII->get(AMDGPU::V_XOR_B32_e32));
108 continue;
109 }
110
Tom Stellard1bd80722014-04-30 15:31:33 +0000111 if (MI.getOpcode() != AMDGPU::COPY ||
112 !TargetRegisterInfo::isVirtualRegister(MI.getOperand(0).getReg()) ||
113 !TargetRegisterInfo::isVirtualRegister(MI.getOperand(1).getReg()))
114 continue;
115
116
117 const TargetRegisterClass *DstRC =
118 MRI.getRegClass(MI.getOperand(0).getReg());
119 const TargetRegisterClass *SrcRC =
120 MRI.getRegClass(MI.getOperand(1).getReg());
121
122 if (DstRC == &AMDGPU::VReg_1RegClass &&
123 TRI->getCommonSubClass(SrcRC, &AMDGPU::SGPR_64RegClass)) {
Tom Stellard365a2b42014-05-15 14:41:50 +0000124 I1Defs.push_back(MI.getOperand(0).getReg());
Tom Stellard1bd80722014-04-30 15:31:33 +0000125 BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(AMDGPU::V_CNDMASK_B32_e64))
126 .addOperand(MI.getOperand(0))
127 .addImm(0)
128 .addImm(-1)
129 .addOperand(MI.getOperand(1))
130 .addImm(0)
131 .addImm(0)
132 .addImm(0)
133 .addImm(0);
134 MI.eraseFromParent();
135 } else if (TRI->getCommonSubClass(DstRC, &AMDGPU::SGPR_64RegClass) &&
136 SrcRC == &AMDGPU::VReg_1RegClass) {
137 BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(AMDGPU::V_CMP_NE_I32_e64))
138 .addOperand(MI.getOperand(0))
139 .addImm(0)
140 .addOperand(MI.getOperand(1))
141 .addImm(0)
142 .addImm(0)
143 .addImm(0)
144 .addImm(0);
145 MI.eraseFromParent();
146 }
Tom Stellard1bd80722014-04-30 15:31:33 +0000147 }
148 }
Tom Stellard365a2b42014-05-15 14:41:50 +0000149
150 for (unsigned Reg : I1Defs)
151 MRI.setRegClass(Reg, &AMDGPU::VReg_32RegClass);
152
Tom Stellard1bd80722014-04-30 15:31:33 +0000153 return false;
154}