86d6614df3fd40dfa8369004465752b1f1d77670
[oota-llvm.git] / lib / Target / ARM / ARMInstrInfo.cpp
1 //===- ARMInstrInfo.cpp - ARM Instruction Information -----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the "Instituto Nokia de Tecnologia" and
6 // is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 //
11 // This file contains the ARM implementation of the TargetInstrInfo class.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "ARMInstrInfo.h"
16 #include "ARM.h"
17 #include "ARMAddressingModes.h"
18 #include "ARMGenInstrInfo.inc"
19 #include "ARMMachineFunctionInfo.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/LiveVariables.h"
22 #include "llvm/Support/CommandLine.h"
23 using namespace llvm;
24
25 static cl::opt<bool> EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
26                                   cl::desc("Enable ARM 2-addr to 3-addr conv"));
27
28 ARMInstrInfo::ARMInstrInfo(const ARMSubtarget &STI)
29   : TargetInstrInfo(ARMInsts, sizeof(ARMInsts)/sizeof(ARMInsts[0])),
30     RI(*this, STI) {
31 }
32
33 const TargetRegisterClass *ARMInstrInfo::getPointerRegClass() const {
34   return &ARM::GPRRegClass;
35 }
36
37 /// Return true if the instruction is a register to register move and
38 /// leave the source and dest operands in the passed parameters.
39 ///
40 bool ARMInstrInfo::isMoveInstr(const MachineInstr &MI,
41                                unsigned &SrcReg, unsigned &DstReg) const {
42   MachineOpCode oc = MI.getOpcode();
43   switch (oc) {
44   default:
45     return false;
46   case ARM::FCPYS:
47   case ARM::FCPYD:
48     SrcReg = MI.getOperand(1).getReg();
49     DstReg = MI.getOperand(0).getReg();
50     return true;
51   case ARM::MOVrr:
52   case ARM::tMOVrr:
53     assert(MI.getNumOperands() == 2 && MI.getOperand(0).isRegister() &&
54            MI.getOperand(1).isRegister() &&
55            "Invalid ARM MOV instruction");
56     SrcReg = MI.getOperand(1).getReg();
57     DstReg = MI.getOperand(0).getReg();
58     return true;
59   }
60 }
61
62 unsigned ARMInstrInfo::isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const{
63   switch (MI->getOpcode()) {
64   default: break;
65   case ARM::LDR:
66     if (MI->getOperand(1).isFrameIndex() &&
67         MI->getOperand(2).isReg() &&
68         MI->getOperand(3).isImmediate() && 
69         MI->getOperand(2).getReg() == 0 &&
70         MI->getOperand(3).getImmedValue() == 0) {
71       FrameIndex = MI->getOperand(1).getFrameIndex();
72       return MI->getOperand(0).getReg();
73     }
74     break;
75   case ARM::FLDD:
76   case ARM::FLDS:
77     if (MI->getOperand(1).isFrameIndex() &&
78         MI->getOperand(2).isImmediate() && 
79         MI->getOperand(2).getImmedValue() == 0) {
80       FrameIndex = MI->getOperand(1).getFrameIndex();
81       return MI->getOperand(0).getReg();
82     }
83     break;
84   case ARM::tLDRspi:
85     if (MI->getOperand(1).isFrameIndex() &&
86         MI->getOperand(2).isImmediate() && 
87         MI->getOperand(2).getImmedValue() == 0) {
88       FrameIndex = MI->getOperand(1).getFrameIndex();
89       return MI->getOperand(0).getReg();
90     }
91     break;
92   }
93   return 0;
94 }
95
96 unsigned ARMInstrInfo::isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const {
97   switch (MI->getOpcode()) {
98   default: break;
99   case ARM::STR:
100     if (MI->getOperand(1).isFrameIndex() &&
101         MI->getOperand(2).isReg() &&
102         MI->getOperand(3).isImmediate() && 
103         MI->getOperand(2).getReg() == 0 &&
104         MI->getOperand(3).getImmedValue() == 0) {
105       FrameIndex = MI->getOperand(1).getFrameIndex();
106       return MI->getOperand(0).getReg();
107     }
108     break;
109   case ARM::FSTD:
110   case ARM::FSTS:
111     if (MI->getOperand(1).isFrameIndex() &&
112         MI->getOperand(2).isImmediate() && 
113         MI->getOperand(2).getImmedValue() == 0) {
114       FrameIndex = MI->getOperand(1).getFrameIndex();
115       return MI->getOperand(0).getReg();
116     }
117     break;
118   case ARM::tSTRspi:
119     if (MI->getOperand(1).isFrameIndex() &&
120         MI->getOperand(2).isImmediate() && 
121         MI->getOperand(2).getImmedValue() == 0) {
122       FrameIndex = MI->getOperand(1).getFrameIndex();
123       return MI->getOperand(0).getReg();
124     }
125     break;
126   }
127   return 0;
128 }
129
130 static unsigned getUnindexedOpcode(unsigned Opc) {
131   switch (Opc) {
132   default: break;
133   case ARM::LDR_PRE:
134   case ARM::LDR_POST:
135     return ARM::LDR;
136   case ARM::LDRH_PRE:
137   case ARM::LDRH_POST:
138     return ARM::LDRH;
139   case ARM::LDRB_PRE:
140   case ARM::LDRB_POST:
141     return ARM::LDRB;
142   case ARM::LDRSH_PRE:
143   case ARM::LDRSH_POST:
144     return ARM::LDRSH;
145   case ARM::LDRSB_PRE:
146   case ARM::LDRSB_POST:
147     return ARM::LDRSB;
148   case ARM::STR_PRE:
149   case ARM::STR_POST:
150     return ARM::STR;
151   case ARM::STRH_PRE:
152   case ARM::STRH_POST:
153     return ARM::STRH;
154   case ARM::STRB_PRE:
155   case ARM::STRB_POST:
156     return ARM::STRB;
157   }
158   return 0;
159 }
160
161 MachineInstr *
162 ARMInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
163                                     MachineBasicBlock::iterator &MBBI,
164                                     LiveVariables &LV) const {
165   if (!EnableARM3Addr)
166     return NULL;
167
168   MachineInstr *MI = MBBI;
169   unsigned TSFlags = MI->getInstrDescriptor()->TSFlags;
170   bool isPre = false;
171   switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
172   default: return NULL;
173   case ARMII::IndexModePre:
174     isPre = true;
175     break;
176   case ARMII::IndexModePost:
177     break;
178   }
179
180   // Try spliting an indexed load / store to a un-indexed one plus an add/sub
181   // operation.
182   unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
183   if (MemOpc == 0)
184     return NULL;
185
186   MachineInstr *UpdateMI = NULL;
187   MachineInstr *MemMI = NULL;
188   unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
189   unsigned NumOps = MI->getNumOperands();
190   bool isLoad = (MI->getInstrDescriptor()->Flags & M_LOAD_FLAG) != 0;
191   const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
192   const MachineOperand &Base = MI->getOperand(2);
193   const MachineOperand &Offset = MI->getOperand(NumOps-2);
194   unsigned WBReg = WB.getReg();
195   unsigned BaseReg = Base.getReg();
196   unsigned OffReg = Offset.getReg();
197   unsigned OffImm = MI->getOperand(NumOps-1).getImm();
198   switch (AddrMode) {
199   default:
200     assert(false && "Unknown indexed op!");
201     return NULL;
202   case ARMII::AddrMode2: {
203     bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
204     unsigned Amt = ARM_AM::getAM2Offset(OffImm);
205     if (OffReg == 0) {
206       int SOImmVal = ARM_AM::getSOImmVal(Amt);
207       if (SOImmVal == -1)
208         // Can't encode it in a so_imm operand. This transformation will
209         // add more than 1 instruction. Abandon!
210         return NULL;
211       UpdateMI = BuildMI(get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
212         .addReg(BaseReg).addImm(SOImmVal);
213     } else if (Amt != 0) {
214       ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
215       unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
216       UpdateMI = BuildMI(get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg)
217         .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc);
218     } else 
219       UpdateMI = BuildMI(get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
220         .addReg(BaseReg).addReg(OffReg);
221     break;
222   }
223   case ARMII::AddrMode3 : {
224     bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
225     unsigned Amt = ARM_AM::getAM3Offset(OffImm);
226     if (OffReg == 0)
227       // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
228       UpdateMI = BuildMI(get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
229         .addReg(BaseReg).addImm(Amt);
230     else
231       UpdateMI = BuildMI(get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
232         .addReg(BaseReg).addReg(OffReg);
233     break;
234   }
235   }
236
237   std::vector<MachineInstr*> NewMIs;
238   if (isPre) {
239     if (isLoad)
240       MemMI = BuildMI(get(MemOpc), MI->getOperand(0).getReg())
241         .addReg(WBReg).addReg(0).addImm(0);
242     else
243       MemMI = BuildMI(get(MemOpc)).addReg(MI->getOperand(1).getReg())
244         .addReg(WBReg).addReg(0).addImm(0);
245     NewMIs.push_back(MemMI);
246     NewMIs.push_back(UpdateMI);
247   } else {
248     if (isLoad)
249       MemMI = BuildMI(get(MemOpc), MI->getOperand(0).getReg())
250         .addReg(BaseReg).addReg(0).addImm(0);
251     else
252       MemMI = BuildMI(get(MemOpc)).addReg(MI->getOperand(1).getReg())
253         .addReg(BaseReg).addReg(0).addImm(0);
254     if (WB.isDead())
255       UpdateMI->getOperand(0).setIsDead();
256     NewMIs.push_back(UpdateMI);
257     NewMIs.push_back(MemMI);
258   }
259   
260   // Transfer LiveVariables states, kill / dead info.
261   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
262     MachineOperand &MO = MI->getOperand(i);
263     if (MO.isRegister() && MO.getReg() &&
264         MRegisterInfo::isVirtualRegister(MO.getReg())) {
265       unsigned Reg = MO.getReg();
266       LiveVariables::VarInfo &VI = LV.getVarInfo(Reg);
267       if (MO.isDef()) {
268         MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
269         if (MO.isDead())
270           LV.addVirtualRegisterDead(Reg, NewMI);
271         // Update the defining instruction.
272         if (VI.DefInst == MI)
273           VI.DefInst = NewMI;
274       }
275       if (MO.isUse() && MO.isKill()) {
276         for (unsigned j = 0; j < 2; ++j) {
277           // Look at the two new MI's in reverse order.
278           MachineInstr *NewMI = NewMIs[j];
279           MachineOperand *NMO = NewMI->findRegisterUseOperand(Reg);
280           if (!NMO)
281             continue;
282           LV.addVirtualRegisterKilled(Reg, NewMI);
283           if (VI.removeKill(MI))
284             VI.Kills.push_back(NewMI);
285           break;
286         }
287       }
288     }
289   }
290
291   MFI->insert(MBBI, NewMIs[1]);
292   MFI->insert(MBBI, NewMIs[0]);
293   return NewMIs[0];
294 }
295
296 // Branch analysis.
297 bool ARMInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
298                                  MachineBasicBlock *&FBB,
299                                  std::vector<MachineOperand> &Cond) const {
300   // If the block has no terminators, it just falls into the block after it.
301   MachineBasicBlock::iterator I = MBB.end();
302   if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode()))
303     return false;
304   
305   // Get the last instruction in the block.
306   MachineInstr *LastInst = I;
307   
308   // If there is only one terminator instruction, process it.
309   unsigned LastOpc = LastInst->getOpcode();
310   if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode())) {
311     if (LastOpc == ARM::B || LastOpc == ARM::tB) {
312       TBB = LastInst->getOperand(0).getMachineBasicBlock();
313       return false;
314     }
315     if (LastOpc == ARM::Bcc || LastOpc == ARM::tBcc) {
316       // Block ends with fall-through condbranch.
317       TBB = LastInst->getOperand(0).getMachineBasicBlock();
318       Cond.push_back(LastInst->getOperand(1));
319       return false;
320     }
321     return true;  // Can't handle indirect branch.
322   }
323   
324   // Get the instruction before it if it is a terminator.
325   MachineInstr *SecondLastInst = I;
326   
327   // If there are three terminators, we don't know what sort of block this is.
328   if (SecondLastInst && I != MBB.begin() &&
329       isTerminatorInstr((--I)->getOpcode()))
330     return true;
331   
332   // If the block ends with ARM::B/ARM::tB and a ARM::Bcc/ARM::tBcc, handle it.
333   unsigned SecondLastOpc = SecondLastInst->getOpcode();
334   if ((SecondLastOpc == ARM::Bcc && LastOpc == ARM::B) ||
335       (SecondLastOpc == ARM::tBcc && LastOpc == ARM::tB)) {
336     TBB =  SecondLastInst->getOperand(0).getMachineBasicBlock();
337     Cond.push_back(SecondLastInst->getOperand(1));
338     FBB = LastInst->getOperand(0).getMachineBasicBlock();
339     return false;
340   }
341   
342   // Otherwise, can't handle this.
343   return true;
344 }
345
346
347 void ARMInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
348   MachineFunction &MF = *MBB.getParent();
349   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
350   int BOpc   = AFI->isThumbFunction() ? ARM::tB : ARM::B;
351   int BccOpc = AFI->isThumbFunction() ? ARM::tBcc : ARM::Bcc;
352
353   MachineBasicBlock::iterator I = MBB.end();
354   if (I == MBB.begin()) return;
355   --I;
356   if (I->getOpcode() != BOpc && I->getOpcode() != BccOpc)
357     return;
358   
359   // Remove the branch.
360   I->eraseFromParent();
361   
362   I = MBB.end();
363   
364   if (I == MBB.begin()) return;
365   --I;
366   if (I->getOpcode() != BccOpc)
367     return;
368   
369   // Remove the branch.
370   I->eraseFromParent();
371 }
372
373 void ARMInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
374                                 MachineBasicBlock *FBB,
375                                 const std::vector<MachineOperand> &Cond) const {
376   MachineFunction &MF = *MBB.getParent();
377   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
378   int BOpc   = AFI->isThumbFunction() ? ARM::tB : ARM::B;
379   int BccOpc = AFI->isThumbFunction() ? ARM::tBcc : ARM::Bcc;
380
381   // Shouldn't be a fall through.
382   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
383   assert((Cond.size() == 1 || Cond.size() == 0) &&
384          "ARM branch conditions have two components!");
385   
386   if (FBB == 0) {
387     if (Cond.empty()) // Unconditional branch?
388       BuildMI(&MBB, get(BOpc)).addMBB(TBB);
389     else
390       BuildMI(&MBB, get(BccOpc)).addMBB(TBB).addImm(Cond[0].getImm());
391     return;
392   }
393   
394   // Two-way conditional branch.
395   BuildMI(&MBB, get(BccOpc)).addMBB(TBB).addImm(Cond[0].getImm());
396   BuildMI(&MBB, get(BOpc)).addMBB(FBB);
397 }
398
399 bool ARMInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
400   if (MBB.empty()) return false;
401   
402   switch (MBB.back().getOpcode()) {
403   case ARM::B:
404   case ARM::tB:       // Uncond branch.
405   case ARM::BR_JTr:   // Jumptable branch.
406   case ARM::BR_JTm:   // Jumptable branch through mem.
407   case ARM::BR_JTadd: // Jumptable branch add to pc.
408     return true;
409   default: return false;
410   }
411 }
412
413 bool ARMInstrInfo::
414 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
415   ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
416   Cond[0].setImm(ARMCC::getOppositeCondition(CC));
417   return false;
418 }