[X86][SSE] Vector integer/float conversion memory folding
[oota-llvm.git] / lib / Target / R600 / SILowerI1Copies.cpp
1 //===-- 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 "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"
29
30 using namespace llvm;
31
32 namespace {
33
34 class SILowerI1Copies : public MachineFunctionPass {
35 public:
36   static char ID;
37
38 public:
39   SILowerI1Copies() : MachineFunctionPass(ID) {
40     initializeSILowerI1CopiesPass(*PassRegistry::getPassRegistry());
41   }
42
43   bool runOnMachineFunction(MachineFunction &MF) override;
44
45   const char *getPassName() const override {
46     return "SI Lower i1 Copies";
47   }
48
49   void getAnalysisUsage(AnalysisUsage &AU) const override {
50     AU.addRequired<MachineDominatorTree>();
51     AU.setPreservesCFG();
52     MachineFunctionPass::getAnalysisUsage(AU);
53   }
54 };
55
56 } // End anonymous namespace.
57
58 INITIALIZE_PASS_BEGIN(SILowerI1Copies, DEBUG_TYPE,
59                       "SI Lower i1 Copies", false, false)
60 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
61 INITIALIZE_PASS_END(SILowerI1Copies, DEBUG_TYPE,
62                     "SI Lower i1 Copies", false, false)
63
64 char SILowerI1Copies::ID = 0;
65
66 char &llvm::SILowerI1CopiesID = SILowerI1Copies::ID;
67
68 FunctionPass *llvm::createSILowerI1CopiesPass() {
69   return new SILowerI1Copies();
70 }
71
72 bool SILowerI1Copies::runOnMachineFunction(MachineFunction &MF) {
73   MachineRegisterInfo &MRI = MF.getRegInfo();
74   const SIInstrInfo *TII =
75       static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
76   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
77   std::vector<unsigned> I1Defs;
78
79   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
80                                                   BI != BE; ++BI) {
81
82     MachineBasicBlock &MBB = *BI;
83     MachineBasicBlock::iterator I, Next;
84     for (I = MBB.begin(); I != MBB.end(); I = Next) {
85       Next = std::next(I);
86       MachineInstr &MI = *I;
87
88       if (MI.getOpcode() == AMDGPU::V_MOV_I1) {
89         I1Defs.push_back(MI.getOperand(0).getReg());
90         MI.setDesc(TII->get(AMDGPU::V_MOV_B32_e32));
91         continue;
92       }
93
94       if (MI.getOpcode() == AMDGPU::V_AND_I1) {
95         I1Defs.push_back(MI.getOperand(0).getReg());
96         MI.setDesc(TII->get(AMDGPU::V_AND_B32_e32));
97         continue;
98       }
99
100       if (MI.getOpcode() == AMDGPU::V_OR_I1) {
101         I1Defs.push_back(MI.getOperand(0).getReg());
102         MI.setDesc(TII->get(AMDGPU::V_OR_B32_e32));
103         continue;
104       }
105
106       if (MI.getOpcode() == AMDGPU::V_XOR_I1) {
107         I1Defs.push_back(MI.getOperand(0).getReg());
108         MI.setDesc(TII->get(AMDGPU::V_XOR_B32_e32));
109         continue;
110       }
111
112       if (MI.getOpcode() != AMDGPU::COPY ||
113           !TargetRegisterInfo::isVirtualRegister(MI.getOperand(0).getReg()) ||
114           !TargetRegisterInfo::isVirtualRegister(MI.getOperand(1).getReg()))
115         continue;
116
117
118       const TargetRegisterClass *DstRC =
119           MRI.getRegClass(MI.getOperand(0).getReg());
120       const TargetRegisterClass *SrcRC =
121           MRI.getRegClass(MI.getOperand(1).getReg());
122
123       if (DstRC == &AMDGPU::VReg_1RegClass &&
124           TRI->getCommonSubClass(SrcRC, &AMDGPU::SGPR_64RegClass)) {
125         I1Defs.push_back(MI.getOperand(0).getReg());
126         BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(AMDGPU::V_CNDMASK_B32_e64))
127                 .addOperand(MI.getOperand(0))
128                 .addImm(0)
129                 .addImm(-1)
130                 .addOperand(MI.getOperand(1));
131         MI.eraseFromParent();
132       } else if (TRI->getCommonSubClass(DstRC, &AMDGPU::SGPR_64RegClass) &&
133                  SrcRC == &AMDGPU::VReg_1RegClass) {
134         BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(AMDGPU::V_CMP_NE_I32_e64))
135                 .addOperand(MI.getOperand(0))
136                 .addOperand(MI.getOperand(1))
137                 .addImm(0);
138         MI.eraseFromParent();
139       }
140     }
141   }
142
143   for (unsigned Reg : I1Defs)
144     MRI.setRegClass(Reg, &AMDGPU::VReg_32RegClass);
145
146   return false;
147 }