[ARM] Mark VMOVRRD with the ExtractSubreg property and implement the related
[oota-llvm.git] / lib / Target / ARM / ARMInstrInfo.cpp
1 //===-- ARMInstrInfo.cpp - ARM Instruction Information --------------------===//
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 //
10 // This file contains the ARM implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ARMInstrInfo.h"
15 #include "ARM.h"
16 #include "ARMConstantPoolValue.h"
17 #include "ARMMachineFunctionInfo.h"
18 #include "ARMTargetMachine.h"
19 #include "MCTargetDesc/ARMAddressingModes.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/CodeGen/LiveVariables.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineJumpTableInfo.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/GlobalVariable.h"
28 #include "llvm/MC/MCAsmInfo.h"
29 #include "llvm/MC/MCInst.h"
30 using namespace llvm;
31
32 ARMInstrInfo::ARMInstrInfo(const ARMSubtarget &STI)
33   : ARMBaseInstrInfo(STI), RI(STI) {
34 }
35
36 /// getNoopForMachoTarget - Return the noop instruction to use for a noop.
37 void ARMInstrInfo::getNoopForMachoTarget(MCInst &NopInst) const {
38   if (hasNOP()) {
39     NopInst.setOpcode(ARM::HINT);
40     NopInst.addOperand(MCOperand::CreateImm(0));
41     NopInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
42     NopInst.addOperand(MCOperand::CreateReg(0));
43   } else {
44     NopInst.setOpcode(ARM::MOVr);
45     NopInst.addOperand(MCOperand::CreateReg(ARM::R0));
46     NopInst.addOperand(MCOperand::CreateReg(ARM::R0));
47     NopInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
48     NopInst.addOperand(MCOperand::CreateReg(0));
49     NopInst.addOperand(MCOperand::CreateReg(0));
50   }
51 }
52
53 unsigned ARMInstrInfo::getUnindexedOpcode(unsigned Opc) const {
54   switch (Opc) {
55   default: break;
56   case ARM::LDR_PRE_IMM:
57   case ARM::LDR_PRE_REG:
58   case ARM::LDR_POST_IMM:
59   case ARM::LDR_POST_REG:
60     return ARM::LDRi12;
61   case ARM::LDRH_PRE:
62   case ARM::LDRH_POST:
63     return ARM::LDRH;
64   case ARM::LDRB_PRE_IMM:
65   case ARM::LDRB_PRE_REG:
66   case ARM::LDRB_POST_IMM:
67   case ARM::LDRB_POST_REG:
68     return ARM::LDRBi12;
69   case ARM::LDRSH_PRE:
70   case ARM::LDRSH_POST:
71     return ARM::LDRSH;
72   case ARM::LDRSB_PRE:
73   case ARM::LDRSB_POST:
74     return ARM::LDRSB;
75   case ARM::STR_PRE_IMM:
76   case ARM::STR_PRE_REG:
77   case ARM::STR_POST_IMM:
78   case ARM::STR_POST_REG:
79     return ARM::STRi12;
80   case ARM::STRH_PRE:
81   case ARM::STRH_POST:
82     return ARM::STRH;
83   case ARM::STRB_PRE_IMM:
84   case ARM::STRB_PRE_REG:
85   case ARM::STRB_POST_IMM:
86   case ARM::STRB_POST_REG:
87     return ARM::STRBi12;
88   }
89
90   return 0;
91 }
92
93 void ARMInstrInfo::expandLoadStackGuard(MachineBasicBlock::iterator MI,
94                                         Reloc::Model RM) const {
95   if (RM == Reloc::PIC_)
96     expandLoadStackGuardBase(MI, ARM::LDRLIT_ga_pcrel, ARM::LDRi12, RM);
97   else
98     expandLoadStackGuardBase(MI, ARM::LDRLIT_ga_abs, ARM::LDRi12, RM);
99 }
100
101 bool ARMInstrInfo::getRegSequenceLikeInputs(
102     const MachineInstr &MI, unsigned DefIdx,
103     SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
104   assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
105   assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
106
107   switch (MI.getOpcode()) {
108   case ARM::VMOVDRR:
109     // dX = VMOVDRR rY, rZ
110     // is the same as:
111     // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
112     // Populate the InputRegs accordingly.
113     // rY
114     const MachineOperand *MOReg = &MI.getOperand(1);
115     InputRegs.push_back(
116         RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0));
117     // rZ
118     MOReg = &MI.getOperand(2);
119     InputRegs.push_back(
120         RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1));
121     return true;
122   }
123   llvm_unreachable("Target dependent opcode missing");
124 }
125
126 bool ARMInstrInfo::getExtractSubregLikeInputs(
127     const MachineInstr &MI, unsigned DefIdx,
128     RegSubRegPairAndIdx &InputReg) const {
129   assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
130   assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
131
132   switch (MI.getOpcode()) {
133   case ARM::VMOVRRD:
134     // rX, rY = VMOVRRD dZ
135     // is the same as:
136     // rX = EXTRACT_SUBREG dZ, ssub_0
137     // rY = EXTRACT_SUBREG dZ, ssub_1
138     const MachineOperand &MOReg = MI.getOperand(2);
139     InputReg.Reg = MOReg.getReg();
140     InputReg.SubReg = MOReg.getSubReg();
141     InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
142     return true;
143   }
144   llvm_unreachable("Target dependent opcode missing");
145 }
146
147 namespace {
148   /// ARMCGBR - Create Global Base Reg pass. This initializes the PIC
149   /// global base register for ARM ELF.
150   struct ARMCGBR : public MachineFunctionPass {
151     static char ID;
152     ARMCGBR() : MachineFunctionPass(ID) {}
153
154     bool runOnMachineFunction(MachineFunction &MF) override {
155       ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
156       if (AFI->getGlobalBaseReg() == 0)
157         return false;
158
159       const ARMTargetMachine *TM =
160         static_cast<const ARMTargetMachine *>(&MF.getTarget());
161       if (TM->getRelocationModel() != Reloc::PIC_)
162         return false;
163
164       LLVMContext *Context = &MF.getFunction()->getContext();
165       unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
166       unsigned PCAdj = TM->getSubtarget<ARMSubtarget>().isThumb() ? 4 : 8;
167       ARMConstantPoolValue *CPV = ARMConstantPoolSymbol::Create(
168           *Context, "_GLOBAL_OFFSET_TABLE_", ARMPCLabelIndex, PCAdj);
169
170       unsigned Align =
171           TM->getSubtargetImpl()->getDataLayout()->getPrefTypeAlignment(
172               Type::getInt32PtrTy(*Context));
173       unsigned Idx = MF.getConstantPool()->getConstantPoolIndex(CPV, Align);
174
175       MachineBasicBlock &FirstMBB = MF.front();
176       MachineBasicBlock::iterator MBBI = FirstMBB.begin();
177       DebugLoc DL = FirstMBB.findDebugLoc(MBBI);
178       unsigned TempReg =
179           MF.getRegInfo().createVirtualRegister(&ARM::rGPRRegClass);
180       unsigned Opc = TM->getSubtarget<ARMSubtarget>().isThumb2() ?
181                      ARM::t2LDRpci : ARM::LDRcp;
182       const TargetInstrInfo &TII = *TM->getSubtargetImpl()->getInstrInfo();
183       MachineInstrBuilder MIB = BuildMI(FirstMBB, MBBI, DL,
184                                         TII.get(Opc), TempReg)
185                                 .addConstantPoolIndex(Idx);
186       if (Opc == ARM::LDRcp)
187         MIB.addImm(0);
188       AddDefaultPred(MIB);
189
190       // Fix the GOT address by adding pc.
191       unsigned GlobalBaseReg = AFI->getGlobalBaseReg();
192       Opc = TM->getSubtarget<ARMSubtarget>().isThumb2() ? ARM::tPICADD
193                                                         : ARM::PICADD;
194       MIB = BuildMI(FirstMBB, MBBI, DL, TII.get(Opc), GlobalBaseReg)
195                 .addReg(TempReg)
196                 .addImm(ARMPCLabelIndex);
197       if (Opc == ARM::PICADD)
198         AddDefaultPred(MIB);
199
200
201       return true;
202     }
203
204     const char *getPassName() const override {
205       return "ARM PIC Global Base Reg Initialization";
206     }
207
208     void getAnalysisUsage(AnalysisUsage &AU) const override {
209       AU.setPreservesCFG();
210       MachineFunctionPass::getAnalysisUsage(AU);
211     }
212   };
213 }
214
215 char ARMCGBR::ID = 0;
216 FunctionPass*
217 llvm::createARMGlobalBaseRegPass() { return new ARMCGBR(); }