- Add TargetInstrInfo::isIdentical(). It's similar to MachineInstr::isIdentical
[oota-llvm.git] / lib / Target / ARM / Thumb2InstrInfo.cpp
1 //===- Thumb2InstrInfo.cpp - Thumb-2 Instruction Information --------*- C++ -*-===//
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 Thumb-2 implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Thumb2InstrInfo.h"
15 #include "ARM.h"
16 #include "ARMConstantPoolValue.h"
17 #include "ARMAddressingModes.h"
18 #include "ARMGenInstrInfo.inc"
19 #include "ARMMachineFunctionInfo.h"
20 #include "llvm/GlobalValue.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineMemOperand.h"
25 #include "llvm/CodeGen/PseudoSourceValue.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "Thumb2InstrInfo.h"
28
29 using namespace llvm;
30
31 Thumb2InstrInfo::Thumb2InstrInfo(const ARMSubtarget &STI)
32   : ARMBaseInstrInfo(STI), RI(*this, STI) {
33 }
34
35 unsigned Thumb2InstrInfo::getUnindexedOpcode(unsigned Opc) const {
36   // FIXME
37   return 0;
38 }
39
40 bool
41 Thumb2InstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
42   if (MBB.empty()) return false;
43
44   switch (MBB.back().getOpcode()) {
45   case ARM::t2LDM_RET:
46   case ARM::t2B:        // Uncond branch.
47   case ARM::t2BR_JT:    // Jumptable branch.
48   case ARM::t2TBB:      // Table branch byte.
49   case ARM::t2TBH:      // Table branch halfword.
50   case ARM::tBR_JTr:    // Jumptable branch (16-bit version).
51   case ARM::tBX_RET:
52   case ARM::tBX_RET_vararg:
53   case ARM::tPOP_RET:
54   case ARM::tB:
55   case ARM::tBRIND:
56     return true;
57   default:
58     break;
59   }
60
61   return false;
62 }
63
64 bool
65 Thumb2InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
66                               MachineBasicBlock::iterator I,
67                               unsigned DestReg, unsigned SrcReg,
68                               const TargetRegisterClass *DestRC,
69                               const TargetRegisterClass *SrcRC) const {
70   DebugLoc DL = DebugLoc::getUnknownLoc();
71   if (I != MBB.end()) DL = I->getDebugLoc();
72
73   if (DestRC == ARM::GPRRegisterClass &&
74       SrcRC == ARM::GPRRegisterClass) {
75     BuildMI(MBB, I, DL, get(ARM::tMOVgpr2gpr), DestReg).addReg(SrcReg);
76     return true;
77   } else if (DestRC == ARM::GPRRegisterClass &&
78              SrcRC == ARM::tGPRRegisterClass) {
79     BuildMI(MBB, I, DL, get(ARM::tMOVtgpr2gpr), DestReg).addReg(SrcReg);
80     return true;
81   } else if (DestRC == ARM::tGPRRegisterClass &&
82              SrcRC == ARM::GPRRegisterClass) {
83     BuildMI(MBB, I, DL, get(ARM::tMOVgpr2tgpr), DestReg).addReg(SrcReg);
84     return true;
85   }
86
87   // Handle SPR, DPR, and QPR copies.
88   return ARMBaseInstrInfo::copyRegToReg(MBB, I, DestReg, SrcReg, DestRC, SrcRC);
89 }
90
91 void Thumb2InstrInfo::
92 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
93                     unsigned SrcReg, bool isKill, int FI,
94                     const TargetRegisterClass *RC) const {
95   DebugLoc DL = DebugLoc::getUnknownLoc();
96   if (I != MBB.end()) DL = I->getDebugLoc();
97
98   if (RC == ARM::GPRRegisterClass) {
99     MachineFunction &MF = *MBB.getParent();
100     MachineFrameInfo &MFI = *MF.getFrameInfo();
101     MachineMemOperand *MMO =
102       MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FI),
103                               MachineMemOperand::MOStore, 0,
104                               MFI.getObjectSize(FI),
105                               MFI.getObjectAlignment(FI));
106     AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::t2STRi12))
107                    .addReg(SrcReg, getKillRegState(isKill))
108                    .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
109     return;
110   }
111
112   ARMBaseInstrInfo::storeRegToStackSlot(MBB, I, SrcReg, isKill, FI, RC);
113 }
114
115 void Thumb2InstrInfo::
116 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
117                      unsigned DestReg, int FI,
118                      const TargetRegisterClass *RC) const {
119   DebugLoc DL = DebugLoc::getUnknownLoc();
120   if (I != MBB.end()) DL = I->getDebugLoc();
121
122   if (RC == ARM::GPRRegisterClass) {
123     MachineFunction &MF = *MBB.getParent();
124     MachineFrameInfo &MFI = *MF.getFrameInfo();
125     MachineMemOperand *MMO =
126       MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FI),
127                               MachineMemOperand::MOLoad, 0,
128                               MFI.getObjectSize(FI),
129                               MFI.getObjectAlignment(FI));
130     AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::t2LDRi12), DestReg)
131                    .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
132     return;
133   }
134
135   ARMBaseInstrInfo::loadRegFromStackSlot(MBB, I, DestReg, FI, RC);
136 }
137
138 void Thumb2InstrInfo::reMaterialize(MachineBasicBlock &MBB,
139                                     MachineBasicBlock::iterator I,
140                                     unsigned DestReg, unsigned SubIdx,
141                                     const MachineInstr *Orig) const {
142   DebugLoc dl = Orig->getDebugLoc();
143   unsigned Opcode = Orig->getOpcode();
144   switch (Opcode) {
145   default: {
146     MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
147     MI->getOperand(0).setReg(DestReg);
148     MBB.insert(I, MI);
149     break;
150   }
151   case ARM::t2LDRpci_pic: {
152     MachineFunction &MF = *MBB.getParent();
153     ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
154     MachineConstantPool *MCP = MF.getConstantPool();
155     unsigned CPI = Orig->getOperand(1).getIndex();
156     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
157     assert(MCPE.isMachineConstantPoolEntry() &&
158            "Expecting a machine constantpool entry!");
159     ARMConstantPoolValue *ACPV =
160       static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
161     assert(ACPV->isGlobalValue() && "Expecting a GV!");
162     unsigned PCLabelId = AFI->createConstPoolEntryUId();
163     ARMConstantPoolValue *NewCPV =
164       new ARMConstantPoolValue(ACPV->getGV(), PCLabelId, ARMCP::CPValue, 4);
165     CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
166     MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
167                                       DestReg)
168       .addConstantPoolIndex(CPI).addImm(PCLabelId);
169     (*MIB).setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
170     break;
171   }
172   }
173
174   MachineInstr *NewMI = prior(I);
175   NewMI->getOperand(0).setSubReg(SubIdx);
176 }
177
178 bool Thumb2InstrInfo::isIdentical(const MachineInstr *MI0,
179                                   const MachineInstr *MI1,
180                                   const MachineRegisterInfo *MRI) const {
181   unsigned Opcode = MI0->getOpcode();
182   if (Opcode == ARM::t2LDRpci_pic) {
183     const MachineOperand &MO0 = MI0->getOperand(1);
184     const MachineOperand &MO1 = MI1->getOperand(1);
185     if (MO0.getOffset() != MO1.getOffset())
186       return false;
187
188     const MachineFunction *MF = MI0->getParent()->getParent();
189     const MachineConstantPool *MCP = MF->getConstantPool();
190     int CPI0 = MO0.getIndex();
191     int CPI1 = MO1.getIndex();
192     const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
193     const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
194     ARMConstantPoolValue *ACPV0 =
195       static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
196     ARMConstantPoolValue *ACPV1 =
197       static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
198     return ACPV0->hasSameValue(ACPV1);
199   }
200
201   return TargetInstrInfoImpl::isIdentical(MI0, MI1, MRI);
202 }
203
204 void llvm::emitT2RegPlusImmediate(MachineBasicBlock &MBB,
205                                MachineBasicBlock::iterator &MBBI, DebugLoc dl,
206                                unsigned DestReg, unsigned BaseReg, int NumBytes,
207                                ARMCC::CondCodes Pred, unsigned PredReg,
208                                const ARMBaseInstrInfo &TII) {
209   bool isSub = NumBytes < 0;
210   if (isSub) NumBytes = -NumBytes;
211
212   // If profitable, use a movw or movt to materialize the offset.
213   // FIXME: Use the scavenger to grab a scratch register.
214   if (DestReg != ARM::SP && DestReg != BaseReg &&
215       NumBytes >= 4096 &&
216       ARM_AM::getT2SOImmVal(NumBytes) == -1) {
217     bool Fits = false;
218     if (NumBytes < 65536) {
219       // Use a movw to materialize the 16-bit constant.
220       BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), DestReg)
221         .addImm(NumBytes)
222         .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
223       Fits = true;
224     } else if ((NumBytes & 0xffff) == 0) {
225       // Use a movt to materialize the 32-bit constant.
226       BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVTi16), DestReg)
227         .addReg(DestReg)
228         .addImm(NumBytes >> 16)
229         .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
230       Fits = true;
231     }
232
233     if (Fits) {
234       if (isSub) {
235         BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr), DestReg)
236           .addReg(BaseReg, RegState::Kill)
237           .addReg(DestReg, RegState::Kill)
238           .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
239       } else {
240         BuildMI(MBB, MBBI, dl, TII.get(ARM::t2ADDrr), DestReg)
241           .addReg(DestReg, RegState::Kill)
242           .addReg(BaseReg, RegState::Kill)
243         .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
244       }
245       return;
246     }
247   }
248
249   while (NumBytes) {
250     unsigned ThisVal = NumBytes;
251     unsigned Opc = 0;
252     if (DestReg == ARM::SP && BaseReg != ARM::SP) {
253       // mov sp, rn. Note t2MOVr cannot be used.
254       BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr),DestReg).addReg(BaseReg);
255       BaseReg = ARM::SP;
256       continue;
257     }
258
259     if (BaseReg == ARM::SP) {
260       // sub sp, sp, #imm7
261       if (DestReg == ARM::SP && (ThisVal < ((1 << 7)-1) * 4)) {
262         assert((ThisVal & 3) == 0 && "Stack update is not multiple of 4?");
263         Opc = isSub ? ARM::tSUBspi : ARM::tADDspi;
264         // FIXME: Fix Thumb1 immediate encoding.
265         BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
266           .addReg(BaseReg).addImm(ThisVal/4);
267         NumBytes = 0;
268         continue;
269       }
270
271       // sub rd, sp, so_imm
272       Opc = isSub ? ARM::t2SUBrSPi : ARM::t2ADDrSPi;
273       if (ARM_AM::getT2SOImmVal(NumBytes) != -1) {
274         NumBytes = 0;
275       } else {
276         // FIXME: Move this to ARMAddressingModes.h?
277         unsigned RotAmt = CountLeadingZeros_32(ThisVal);
278         ThisVal = ThisVal & ARM_AM::rotr32(0xff000000U, RotAmt);
279         NumBytes &= ~ThisVal;
280         assert(ARM_AM::getT2SOImmVal(ThisVal) != -1 &&
281                "Bit extraction didn't work?");
282       }
283     } else {
284       assert(DestReg != ARM::SP && BaseReg != ARM::SP);
285       Opc = isSub ? ARM::t2SUBri : ARM::t2ADDri;
286       if (ARM_AM::getT2SOImmVal(NumBytes) != -1) {
287         NumBytes = 0;
288       } else if (ThisVal < 4096) {
289         Opc = isSub ? ARM::t2SUBri12 : ARM::t2ADDri12;
290         NumBytes = 0;
291       } else {
292         // FIXME: Move this to ARMAddressingModes.h?
293         unsigned RotAmt = CountLeadingZeros_32(ThisVal);
294         ThisVal = ThisVal & ARM_AM::rotr32(0xff000000U, RotAmt);
295         NumBytes &= ~ThisVal;
296         assert(ARM_AM::getT2SOImmVal(ThisVal) != -1 &&
297                "Bit extraction didn't work?");
298       }
299     }
300
301     // Build the new ADD / SUB.
302     AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
303                                 .addReg(BaseReg, RegState::Kill)
304                                 .addImm(ThisVal)));
305
306     BaseReg = DestReg;
307   }
308 }
309
310 static unsigned
311 negativeOffsetOpcode(unsigned opcode)
312 {
313   switch (opcode) {
314   case ARM::t2LDRi12:   return ARM::t2LDRi8;
315   case ARM::t2LDRHi12:  return ARM::t2LDRHi8;
316   case ARM::t2LDRBi12:  return ARM::t2LDRBi8;
317   case ARM::t2LDRSHi12: return ARM::t2LDRSHi8;
318   case ARM::t2LDRSBi12: return ARM::t2LDRSBi8;
319   case ARM::t2STRi12:   return ARM::t2STRi8;
320   case ARM::t2STRBi12:  return ARM::t2STRBi8;
321   case ARM::t2STRHi12:  return ARM::t2STRHi8;
322
323   case ARM::t2LDRi8:
324   case ARM::t2LDRHi8:
325   case ARM::t2LDRBi8:
326   case ARM::t2LDRSHi8:
327   case ARM::t2LDRSBi8:
328   case ARM::t2STRi8:
329   case ARM::t2STRBi8:
330   case ARM::t2STRHi8:
331     return opcode;
332
333   default:
334     break;
335   }
336
337   return 0;
338 }
339
340 static unsigned
341 positiveOffsetOpcode(unsigned opcode)
342 {
343   switch (opcode) {
344   case ARM::t2LDRi8:   return ARM::t2LDRi12;
345   case ARM::t2LDRHi8:  return ARM::t2LDRHi12;
346   case ARM::t2LDRBi8:  return ARM::t2LDRBi12;
347   case ARM::t2LDRSHi8: return ARM::t2LDRSHi12;
348   case ARM::t2LDRSBi8: return ARM::t2LDRSBi12;
349   case ARM::t2STRi8:   return ARM::t2STRi12;
350   case ARM::t2STRBi8:  return ARM::t2STRBi12;
351   case ARM::t2STRHi8:  return ARM::t2STRHi12;
352
353   case ARM::t2LDRi12:
354   case ARM::t2LDRHi12:
355   case ARM::t2LDRBi12:
356   case ARM::t2LDRSHi12:
357   case ARM::t2LDRSBi12:
358   case ARM::t2STRi12:
359   case ARM::t2STRBi12:
360   case ARM::t2STRHi12:
361     return opcode;
362
363   default:
364     break;
365   }
366
367   return 0;
368 }
369
370 static unsigned
371 immediateOffsetOpcode(unsigned opcode)
372 {
373   switch (opcode) {
374   case ARM::t2LDRs:   return ARM::t2LDRi12;
375   case ARM::t2LDRHs:  return ARM::t2LDRHi12;
376   case ARM::t2LDRBs:  return ARM::t2LDRBi12;
377   case ARM::t2LDRSHs: return ARM::t2LDRSHi12;
378   case ARM::t2LDRSBs: return ARM::t2LDRSBi12;
379   case ARM::t2STRs:   return ARM::t2STRi12;
380   case ARM::t2STRBs:  return ARM::t2STRBi12;
381   case ARM::t2STRHs:  return ARM::t2STRHi12;
382
383   case ARM::t2LDRi12:
384   case ARM::t2LDRHi12:
385   case ARM::t2LDRBi12:
386   case ARM::t2LDRSHi12:
387   case ARM::t2LDRSBi12:
388   case ARM::t2STRi12:
389   case ARM::t2STRBi12:
390   case ARM::t2STRHi12:
391   case ARM::t2LDRi8:
392   case ARM::t2LDRHi8:
393   case ARM::t2LDRBi8:
394   case ARM::t2LDRSHi8:
395   case ARM::t2LDRSBi8:
396   case ARM::t2STRi8:
397   case ARM::t2STRBi8:
398   case ARM::t2STRHi8:
399     return opcode;
400
401   default:
402     break;
403   }
404
405   return 0;
406 }
407
408 bool llvm::rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
409                                unsigned FrameReg, int &Offset,
410                                const ARMBaseInstrInfo &TII) {
411   unsigned Opcode = MI.getOpcode();
412   const TargetInstrDesc &Desc = MI.getDesc();
413   unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
414   bool isSub = false;
415
416   // Memory operands in inline assembly always use AddrModeT2_i12.
417   if (Opcode == ARM::INLINEASM)
418     AddrMode = ARMII::AddrModeT2_i12; // FIXME. mode for thumb2?
419
420   if (Opcode == ARM::t2ADDri || Opcode == ARM::t2ADDri12) {
421     Offset += MI.getOperand(FrameRegIdx+1).getImm();
422
423     bool isSP = FrameReg == ARM::SP;
424     if (Offset == 0) {
425       // Turn it into a move.
426       MI.setDesc(TII.get(ARM::tMOVgpr2gpr));
427       MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
428       MI.RemoveOperand(FrameRegIdx+1);
429       Offset = 0;
430       return true;
431     }
432
433     if (Offset < 0) {
434       Offset = -Offset;
435       isSub = true;
436       MI.setDesc(TII.get(isSP ? ARM::t2SUBrSPi : ARM::t2SUBri));
437     } else {
438       MI.setDesc(TII.get(isSP ? ARM::t2ADDrSPi : ARM::t2ADDri));
439     }
440
441     // Common case: small offset, fits into instruction.
442     if (ARM_AM::getT2SOImmVal(Offset) != -1) {
443       MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
444       MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
445       Offset = 0;
446       return true;
447     }
448     // Another common case: imm12.
449     if (Offset < 4096) {
450       unsigned NewOpc = isSP
451         ? (isSub ? ARM::t2SUBrSPi12 : ARM::t2ADDrSPi12)
452         : (isSub ? ARM::t2SUBri12   : ARM::t2ADDri12);
453       MI.setDesc(TII.get(NewOpc));
454       MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
455       MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
456       Offset = 0;
457       return true;
458     }
459
460     // Otherwise, extract 8 adjacent bits from the immediate into this
461     // t2ADDri/t2SUBri.
462     unsigned RotAmt = CountLeadingZeros_32(Offset);
463     unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xff000000U, RotAmt);
464
465     // We will handle these bits from offset, clear them.
466     Offset &= ~ThisImmVal;
467
468     assert(ARM_AM::getT2SOImmVal(ThisImmVal) != -1 &&
469            "Bit extraction didn't work?");
470     MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
471   } else {
472
473     // AddrMode4 cannot handle any offset.
474     if (AddrMode == ARMII::AddrMode4)
475       return false;
476
477     // AddrModeT2_so cannot handle any offset. If there is no offset
478     // register then we change to an immediate version.
479     unsigned NewOpc = Opcode;
480     if (AddrMode == ARMII::AddrModeT2_so) {
481       unsigned OffsetReg = MI.getOperand(FrameRegIdx+1).getReg();
482       if (OffsetReg != 0) {
483         MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
484         return Offset == 0;
485       }
486
487       MI.RemoveOperand(FrameRegIdx+1);
488       MI.getOperand(FrameRegIdx+1).ChangeToImmediate(0);
489       NewOpc = immediateOffsetOpcode(Opcode);
490       AddrMode = ARMII::AddrModeT2_i12;
491     }
492
493     unsigned NumBits = 0;
494     unsigned Scale = 1;
495     if (AddrMode == ARMII::AddrModeT2_i8 || AddrMode == ARMII::AddrModeT2_i12) {
496       // i8 supports only negative, and i12 supports only positive, so
497       // based on Offset sign convert Opcode to the appropriate
498       // instruction
499       Offset += MI.getOperand(FrameRegIdx+1).getImm();
500       if (Offset < 0) {
501         NewOpc = negativeOffsetOpcode(Opcode);
502         NumBits = 8;
503         isSub = true;
504         Offset = -Offset;
505       } else {
506         NewOpc = positiveOffsetOpcode(Opcode);
507         NumBits = 12;
508       }
509     } else {
510       // VFP and NEON address modes.
511       int InstrOffs = 0;
512       if (AddrMode == ARMII::AddrMode5) {
513         const MachineOperand &OffOp = MI.getOperand(FrameRegIdx+1);
514         InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm());
515         if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub)
516           InstrOffs *= -1;
517       }
518       NumBits = 8;
519       Scale = 4;
520       Offset += InstrOffs * 4;
521       assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
522       if (Offset < 0) {
523         Offset = -Offset;
524         isSub = true;
525       }
526     }
527
528     if (NewOpc != Opcode)
529       MI.setDesc(TII.get(NewOpc));
530
531     MachineOperand &ImmOp = MI.getOperand(FrameRegIdx+1);
532
533     // Attempt to fold address computation
534     // Common case: small offset, fits into instruction.
535     int ImmedOffset = Offset / Scale;
536     unsigned Mask = (1 << NumBits) - 1;
537     if ((unsigned)Offset <= Mask * Scale) {
538       // Replace the FrameIndex with fp/sp
539       MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
540       if (isSub) {
541         if (AddrMode == ARMII::AddrMode5)
542           // FIXME: Not consistent.
543           ImmedOffset |= 1 << NumBits;
544         else
545           ImmedOffset = -ImmedOffset;
546       }
547       ImmOp.ChangeToImmediate(ImmedOffset);
548       Offset = 0;
549       return true;
550     }
551
552     // Otherwise, offset doesn't fit. Pull in what we can to simplify
553     ImmedOffset = ImmedOffset & Mask;
554     if (isSub) {
555       if (AddrMode == ARMII::AddrMode5)
556         // FIXME: Not consistent.
557         ImmedOffset |= 1 << NumBits;
558       else {
559         ImmedOffset = -ImmedOffset;
560         if (ImmedOffset == 0)
561           // Change the opcode back if the encoded offset is zero.
562           MI.setDesc(TII.get(positiveOffsetOpcode(NewOpc)));
563       }
564     }
565     ImmOp.ChangeToImmediate(ImmedOffset);
566     Offset &= ~(Mask*Scale);
567   }
568
569   Offset = (isSub) ? -Offset : Offset;
570   return Offset == 0;
571 }