1 //===-- Thumb2SizeReduction.cpp - Thumb2 code size reduction pass -*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "t2-reduce-size"
12 #include "ARMAddressingModes.h"
13 #include "ARMBaseRegisterInfo.h"
14 #include "ARMBaseInstrInfo.h"
15 #include "Thumb2InstrInfo.h"
16 #include "llvm/CodeGen/MachineInstr.h"
17 #include "llvm/CodeGen/MachineInstrBuilder.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/Statistic.h"
26 STATISTIC(NumNarrows, "Number of 32-bit instrs reduced to 16-bit ones");
27 STATISTIC(Num2Addrs, "Number of 32-bit instrs reduced to 2addr 16-bit ones");
28 STATISTIC(NumLdSts, "Number of 32-bit load / store reduced to 16-bit ones");
30 static cl::opt<int> ReduceLimit("t2-reduce-limit",
31 cl::init(-1), cl::Hidden);
32 static cl::opt<int> ReduceLimit2Addr("t2-reduce-limit2",
33 cl::init(-1), cl::Hidden);
34 static cl::opt<int> ReduceLimitLdSt("t2-reduce-limit3",
35 cl::init(-1), cl::Hidden);
38 /// ReduceTable - A static table with information on mapping from wide
41 unsigned WideOpc; // Wide opcode
42 unsigned NarrowOpc1; // Narrow opcode to transform to
43 unsigned NarrowOpc2; // Narrow opcode when it's two-address
44 uint8_t Imm1Limit; // Limit of immediate field (bits)
45 uint8_t Imm2Limit; // Limit of immediate field when it's two-address
46 unsigned LowRegs1 : 1; // Only possible if low-registers are used
47 unsigned LowRegs2 : 1; // Only possible if low-registers are used (2addr)
48 unsigned PredCC1 : 2; // 0 - If predicated, cc is on and vice versa.
50 // 2 - Always set CPSR.
52 unsigned Special : 1; // Needs to be dealt with specially
55 static const ReduceEntry ReduceTable[] = {
56 // Wide, Narrow1, Narrow2, imm1,imm2, lo1, lo2, P/C, S
57 { ARM::t2ADCrr, 0, ARM::tADC, 0, 0, 0, 1, 0,0, 0 },
58 { ARM::t2ADDri, ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 0,0, 0 },
59 { ARM::t2ADDrr, ARM::tADDrr, ARM::tADDhirr, 0, 0, 1, 0, 0,1, 0 },
60 // Note: immediate scale is 4.
61 { ARM::t2ADDrSPi,ARM::tADDrSPi,0, 8, 0, 1, 0, 1,0, 1 },
62 { ARM::t2ADDSri,ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 2,2, 1 },
63 { ARM::t2ADDSrr,ARM::tADDrr, 0, 0, 0, 1, 0, 2,0, 1 },
64 { ARM::t2ANDrr, 0, ARM::tAND, 0, 0, 0, 1, 0,0, 0 },
65 { ARM::t2ASRri, ARM::tASRri, 0, 5, 0, 1, 0, 0,0, 0 },
66 { ARM::t2ASRrr, 0, ARM::tASRrr, 0, 0, 0, 1, 0,0, 0 },
67 { ARM::t2BICrr, 0, ARM::tBIC, 0, 0, 0, 1, 0,0, 0 },
68 //FIXME: Disable CMN, as CCodes are backwards from compare expectations
69 //{ ARM::t2CMNrr, ARM::tCMN, 0, 0, 0, 1, 0, 2,0, 0 },
70 { ARM::t2CMPri, ARM::tCMPi8, 0, 8, 0, 1, 0, 2,0, 0 },
71 { ARM::t2CMPrr, ARM::tCMPhir, 0, 0, 0, 0, 0, 2,0, 1 },
72 { ARM::t2EORrr, 0, ARM::tEOR, 0, 0, 0, 1, 0,0, 0 },
73 // FIXME: adr.n immediate offset must be multiple of 4.
74 //{ ARM::t2LEApcrelJT,ARM::tLEApcrelJT, 0, 0, 0, 1, 0, 1,0, 0 },
75 { ARM::t2LSLri, ARM::tLSLri, 0, 5, 0, 1, 0, 0,0, 0 },
76 { ARM::t2LSLrr, 0, ARM::tLSLrr, 0, 0, 0, 1, 0,0, 0 },
77 { ARM::t2LSRri, ARM::tLSRri, 0, 5, 0, 1, 0, 0,0, 0 },
78 { ARM::t2LSRrr, 0, ARM::tLSRrr, 0, 0, 0, 1, 0,0, 0 },
79 { ARM::t2MOVi, ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 0 },
80 { ARM::t2MOVi16,ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 1 },
81 // FIXME: Do we need the 16-bit 'S' variant?
82 { ARM::t2MOVr,ARM::tMOVgpr2gpr,0, 0, 0, 0, 0, 1,0, 0 },
83 { ARM::t2MOVCCr,0, ARM::tMOVCCr, 0, 0, 0, 0, 0,1, 0 },
84 { ARM::t2MOVCCi,0, ARM::tMOVCCi, 0, 8, 0, 1, 0,1, 0 },
85 { ARM::t2MUL, 0, ARM::tMUL, 0, 0, 0, 1, 0,0, 0 },
86 { ARM::t2MVNr, ARM::tMVN, 0, 0, 0, 1, 0, 0,0, 0 },
87 { ARM::t2ORRrr, 0, ARM::tORR, 0, 0, 0, 1, 0,0, 0 },
88 { ARM::t2REV, ARM::tREV, 0, 0, 0, 1, 0, 1,0, 0 },
89 { ARM::t2REV16, ARM::tREV16, 0, 0, 0, 1, 0, 1,0, 0 },
90 { ARM::t2REVSH, ARM::tREVSH, 0, 0, 0, 1, 0, 1,0, 0 },
91 { ARM::t2RORrr, 0, ARM::tROR, 0, 0, 0, 1, 0,0, 0 },
92 { ARM::t2RSBri, ARM::tRSB, 0, 0, 0, 1, 0, 0,0, 1 },
93 { ARM::t2RSBSri,ARM::tRSB, 0, 0, 0, 1, 0, 2,0, 1 },
94 { ARM::t2SBCrr, 0, ARM::tSBC, 0, 0, 0, 1, 0,0, 0 },
95 { ARM::t2SUBri, ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 0,0, 0 },
96 { ARM::t2SUBrr, ARM::tSUBrr, 0, 0, 0, 1, 0, 0,0, 0 },
97 { ARM::t2SUBSri,ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 2,2, 0 },
98 { ARM::t2SUBSrr,ARM::tSUBrr, 0, 0, 0, 1, 0, 2,0, 0 },
99 { ARM::t2SXTBr, ARM::tSXTB, 0, 0, 0, 1, 0, 1,0, 0 },
100 { ARM::t2SXTHr, ARM::tSXTH, 0, 0, 0, 1, 0, 1,0, 0 },
101 { ARM::t2TSTrr, ARM::tTST, 0, 0, 0, 1, 0, 2,0, 0 },
102 { ARM::t2UXTBr, ARM::tUXTB, 0, 0, 0, 1, 0, 1,0, 0 },
103 { ARM::t2UXTHr, ARM::tUXTH, 0, 0, 0, 1, 0, 1,0, 0 },
105 // FIXME: Clean this up after splitting each Thumb load / store opcode
106 // into multiple ones.
107 { ARM::t2LDRi12,ARM::tLDRi, ARM::tLDRspi, 5, 8, 1, 0, 0,0, 1 },
108 { ARM::t2LDRs, ARM::tLDR, 0, 0, 0, 1, 0, 0,0, 1 },
109 { ARM::t2LDRBi12,ARM::tLDRBi, 0, 5, 0, 1, 0, 0,0, 1 },
110 { ARM::t2LDRBs, ARM::tLDRB, 0, 0, 0, 1, 0, 0,0, 1 },
111 { ARM::t2LDRHi12,ARM::tLDRHi, 0, 5, 0, 1, 0, 0,0, 1 },
112 { ARM::t2LDRHs, ARM::tLDRH, 0, 0, 0, 1, 0, 0,0, 1 },
113 { ARM::t2LDRSBs,ARM::tLDRSB, 0, 0, 0, 1, 0, 0,0, 1 },
114 { ARM::t2LDRSHs,ARM::tLDRSH, 0, 0, 0, 1, 0, 0,0, 1 },
115 { ARM::t2STRi12,ARM::tSTRi, ARM::tSTRspi, 5, 8, 1, 0, 0,0, 1 },
116 { ARM::t2STRs, ARM::tSTR, 0, 0, 0, 1, 0, 0,0, 1 },
117 { ARM::t2STRBi12,ARM::tSTRBi, 0, 5, 0, 1, 0, 0,0, 1 },
118 { ARM::t2STRBs, ARM::tSTRB, 0, 0, 0, 1, 0, 0,0, 1 },
119 { ARM::t2STRHi12,ARM::tSTRHi, 0, 5, 0, 1, 0, 0,0, 1 },
120 { ARM::t2STRHs, ARM::tSTRH, 0, 0, 0, 1, 0, 0,0, 1 },
122 { ARM::t2LDMIA, ARM::tLDMIA, 0, 0, 0, 1, 1, 1,1, 1 },
123 { ARM::t2LDMIA_RET,0, ARM::tPOP_RET, 0, 0, 1, 1, 1,1, 1 },
124 { ARM::t2LDMIA_UPD,ARM::tLDMIA_UPD,ARM::tPOP,0, 0, 1, 1, 1,1, 1 },
125 // ARM::t2STM (with no basereg writeback) has no Thumb1 equivalent
126 { ARM::t2STMIA_UPD,ARM::tSTMIA_UPD, 0, 0, 0, 1, 1, 1,1, 1 },
127 { ARM::t2STMDB_UPD, 0, ARM::tPUSH, 0, 0, 1, 1, 1,1, 1 },
130 class Thumb2SizeReduce : public MachineFunctionPass {
135 const Thumb2InstrInfo *TII;
137 virtual bool runOnMachineFunction(MachineFunction &MF);
139 virtual const char *getPassName() const {
140 return "Thumb2 instruction size reduction pass";
144 /// ReduceOpcodeMap - Maps wide opcode to index of entry in ReduceTable.
145 DenseMap<unsigned, unsigned> ReduceOpcodeMap;
147 bool VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
148 bool is2Addr, ARMCC::CondCodes Pred,
149 bool LiveCPSR, bool &HasCC, bool &CCDead);
151 bool ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
152 const ReduceEntry &Entry);
154 bool ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
155 const ReduceEntry &Entry, bool LiveCPSR);
157 /// ReduceTo2Addr - Reduce a 32-bit instruction to a 16-bit two-address
159 bool ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
160 const ReduceEntry &Entry,
163 /// ReduceToNarrow - Reduce a 32-bit instruction to a 16-bit
164 /// non-two-address instruction.
165 bool ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
166 const ReduceEntry &Entry,
169 /// ReduceMBB - Reduce width of instructions in the specified basic block.
170 bool ReduceMBB(MachineBasicBlock &MBB);
172 char Thumb2SizeReduce::ID = 0;
175 Thumb2SizeReduce::Thumb2SizeReduce() : MachineFunctionPass(ID) {
176 for (unsigned i = 0, e = array_lengthof(ReduceTable); i != e; ++i) {
177 unsigned FromOpc = ReduceTable[i].WideOpc;
178 if (!ReduceOpcodeMap.insert(std::make_pair(FromOpc, i)).second)
179 assert(false && "Duplicated entries?");
183 static bool HasImplicitCPSRDef(const TargetInstrDesc &TID) {
184 for (const unsigned *Regs = TID.ImplicitDefs; *Regs; ++Regs)
185 if (*Regs == ARM::CPSR)
191 Thumb2SizeReduce::VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
192 bool is2Addr, ARMCC::CondCodes Pred,
193 bool LiveCPSR, bool &HasCC, bool &CCDead) {
194 if ((is2Addr && Entry.PredCC2 == 0) ||
195 (!is2Addr && Entry.PredCC1 == 0)) {
196 if (Pred == ARMCC::AL) {
197 // Not predicated, must set CPSR.
199 // Original instruction was not setting CPSR, but CPSR is not
200 // currently live anyway. It's ok to set it. The CPSR def is
210 // Predicated, must not set CPSR.
214 } else if ((is2Addr && Entry.PredCC2 == 2) ||
215 (!is2Addr && Entry.PredCC1 == 2)) {
216 /// Old opcode has an optional def of CPSR.
219 // If old opcode does not implicitly define CPSR, then it's not ok since
220 // these new opcodes' CPSR def is not meant to be thrown away. e.g. CMP.
221 if (!HasImplicitCPSRDef(MI->getDesc()))
225 // 16-bit instruction does not set CPSR.
233 static bool VerifyLowRegs(MachineInstr *MI) {
234 unsigned Opc = MI->getOpcode();
235 bool isPCOk = (Opc == ARM::t2LDMIA_RET || Opc == ARM::t2LDMIA ||
236 Opc == ARM::t2LDMDB || Opc == ARM::t2LDMIA_UPD ||
237 Opc == ARM::t2LDMDB_UPD || Opc == ARM::t2LDRi12);
238 bool isLROk = (Opc == ARM::t2STMIA_UPD || Opc == ARM::t2STMDB_UPD);
239 bool isSPOk = isPCOk || isLROk || (Opc == ARM::t2ADDrSPi);
240 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
241 const MachineOperand &MO = MI->getOperand(i);
242 if (!MO.isReg() || MO.isImplicit())
244 unsigned Reg = MO.getReg();
245 if (Reg == 0 || Reg == ARM::CPSR)
247 if (isPCOk && Reg == ARM::PC)
249 if (isLROk && Reg == ARM::LR)
251 if (Reg == ARM::SP) {
254 if (i == 1 && (Opc == ARM::t2LDRi12 || Opc == ARM::t2STRi12))
255 // Special case for these ldr / str with sp as base register.
258 if (!isARMLowRegister(Reg))
265 Thumb2SizeReduce::ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
266 const ReduceEntry &Entry) {
267 if (ReduceLimitLdSt != -1 && ((int)NumLdSts >= ReduceLimitLdSt))
271 bool HasBaseReg = true;
272 bool HasImmOffset = false;
273 bool HasShift = false;
274 bool HasOffReg = true;
275 bool isLdStMul = false;
276 bool InsertImmOffset = true;
277 unsigned Opc = Entry.NarrowOpc1;
278 unsigned OpNum = 3; // First 'rest' of operands.
279 uint8_t ImmLimit = Entry.Imm1Limit;
281 switch (Entry.WideOpc) {
283 llvm_unreachable("Unexpected Thumb2 load / store opcode!");
285 case ARM::t2STRi12: {
286 unsigned BaseReg = MI->getOperand(1).getReg();
287 if (BaseReg == ARM::SP) {
288 Opc = Entry.NarrowOpc2;
289 ImmLimit = Entry.Imm2Limit;
293 if (MI->getOperand(2).isImm())
296 if (Entry.WideOpc == ARM::t2LDRi12) {
300 HasImmOffset = false;
301 InsertImmOffset = false;
309 if (MI->getOperand(2).isImm())
312 if (Entry.WideOpc == ARM::t2LDRBi12) {
316 HasImmOffset = false;
317 InsertImmOffset = false;
325 if (MI->getOperand(2).isImm())
328 if (Entry.WideOpc == ARM::t2LDRHi12) {
332 HasImmOffset = false;
333 InsertImmOffset = false;
351 unsigned BaseReg = MI->getOperand(0).getReg();
352 if (!isARMLowRegister(BaseReg) || Entry.WideOpc != ARM::t2LDMIA)
354 // For the non-writeback version (this one), the base register must be
355 // one of the registers being loaded.
357 for (unsigned i = 4; i < MI->getNumOperands(); ++i) {
358 if (MI->getOperand(i).getReg() == BaseReg) {
370 case ARM::t2LDMIA_RET: {
371 unsigned BaseReg = MI->getOperand(1).getReg();
372 if (BaseReg != ARM::SP)
374 Opc = Entry.NarrowOpc2; // tPOP_RET
379 case ARM::t2LDMIA_UPD:
380 case ARM::t2LDMDB_UPD:
381 case ARM::t2STMIA_UPD:
382 case ARM::t2STMDB_UPD: {
384 unsigned BaseReg = MI->getOperand(1).getReg();
385 if (BaseReg == ARM::SP &&
386 (Entry.WideOpc == ARM::t2LDMIA_UPD ||
387 Entry.WideOpc == ARM::t2STMDB_UPD)) {
388 Opc = Entry.NarrowOpc2; // tPOP or tPUSH
390 } else if (!isARMLowRegister(BaseReg) ||
391 (Entry.WideOpc != ARM::t2LDMIA_UPD &&
392 Entry.WideOpc != ARM::t2STMIA_UPD)) {
400 unsigned OffsetReg = 0;
401 bool OffsetKill = false;
403 OffsetReg = MI->getOperand(2).getReg();
404 OffsetKill = MI->getOperand(2).isKill();
405 if (MI->getOperand(3).getImm())
406 // Thumb1 addressing mode doesn't support shift.
410 unsigned OffsetImm = 0;
412 OffsetImm = MI->getOperand(2).getImm();
413 unsigned MaxOffset = ((1 << ImmLimit) - 1) * Scale;
414 if ((OffsetImm & (Scale-1)) || OffsetImm > MaxOffset)
415 // Make sure the immediate field fits.
419 // Add the 16-bit load / store instruction.
420 // FIXME: Thumb1 addressing mode encode both immediate and register offset.
421 DebugLoc dl = MI->getDebugLoc();
422 MachineInstrBuilder MIB = BuildMI(MBB, *MI, dl, TII->get(Opc));
424 MIB.addOperand(MI->getOperand(0));
425 if (HasBaseReg) MIB.addOperand(MI->getOperand(1));
426 if (InsertImmOffset && Opc != ARM::tLDRSB && Opc != ARM::tLDRSH) {
427 // tLDRSB and tLDRSH do not have an immediate offset field. On the other
428 // hand, it must have an offset register.
429 // FIXME: Remove this special case.
430 MIB.addImm(OffsetImm/Scale);
432 assert((!HasShift || OffsetReg) && "Invalid so_reg load / store address!");
435 MIB.addReg(OffsetReg, getKillRegState(OffsetKill));
438 // Transfer the rest of operands.
439 for (unsigned e = MI->getNumOperands(); OpNum != e; ++OpNum)
440 MIB.addOperand(MI->getOperand(OpNum));
442 // Transfer memoperands.
443 (*MIB).setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
445 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
453 Thumb2SizeReduce::ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
454 const ReduceEntry &Entry,
456 if (Entry.LowRegs1 && !VerifyLowRegs(MI))
459 const TargetInstrDesc &TID = MI->getDesc();
460 if (TID.mayLoad() || TID.mayStore())
461 return ReduceLoadStore(MBB, MI, Entry);
463 unsigned Opc = MI->getOpcode();
467 case ARM::t2ADDSrr: {
468 unsigned PredReg = 0;
469 if (getInstrPredicate(MI, PredReg) == ARMCC::AL) {
472 case ARM::t2ADDSri: {
473 if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR))
478 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR);
485 if (MI->getOperand(2).getImm() == 0)
486 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR);
489 // Can convert only 'pure' immediate operands, not immediates obtained as
490 // globals' addresses.
491 if (MI->getOperand(1).isImm())
492 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR);
495 // Try to reduce to the lo-reg only version first. Why there are two
496 // versions of the instruction is a mystery.
497 // It would be nice to just have two entries in the master table that
498 // are prioritized, but the table assumes a unique entry for each
499 // source insn opcode. So for now, we hack a local entry record to use.
500 static const ReduceEntry NarrowEntry =
501 { ARM::t2CMPrr,ARM::tCMPr, 0, 0, 0, 1, 1,2, 0, 1 };
502 if (ReduceToNarrow(MBB, MI, NarrowEntry, LiveCPSR))
504 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR);
506 case ARM::t2ADDrSPi: {
507 static const ReduceEntry NarrowEntry =
508 { ARM::t2ADDrSPi,ARM::tADDspi, 0, 7, 0, 1, 0, 1, 0, 1 };
509 if (MI->getOperand(0).getReg() == ARM::SP)
510 return ReduceToNarrow(MBB, MI, NarrowEntry, LiveCPSR);
511 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR);
518 Thumb2SizeReduce::ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
519 const ReduceEntry &Entry,
522 if (ReduceLimit2Addr != -1 && ((int)Num2Addrs >= ReduceLimit2Addr))
525 unsigned Reg0 = MI->getOperand(0).getReg();
526 unsigned Reg1 = MI->getOperand(1).getReg();
528 // Try to commute the operands to make it a 2-address instruction.
529 unsigned CommOpIdx1, CommOpIdx2;
530 if (!TII->findCommutedOpIndices(MI, CommOpIdx1, CommOpIdx2) ||
531 CommOpIdx1 != 1 || MI->getOperand(CommOpIdx2).getReg() != Reg0)
533 MachineInstr *CommutedMI = TII->commuteInstruction(MI);
537 if (Entry.LowRegs2 && !isARMLowRegister(Reg0))
539 if (Entry.Imm2Limit) {
540 unsigned Imm = MI->getOperand(2).getImm();
541 unsigned Limit = (1 << Entry.Imm2Limit) - 1;
545 unsigned Reg2 = MI->getOperand(2).getReg();
546 if (Entry.LowRegs2 && !isARMLowRegister(Reg2))
550 // Check if it's possible / necessary to transfer the predicate.
551 const TargetInstrDesc &NewTID = TII->get(Entry.NarrowOpc2);
552 unsigned PredReg = 0;
553 ARMCC::CondCodes Pred = getInstrPredicate(MI, PredReg);
554 bool SkipPred = false;
555 if (Pred != ARMCC::AL) {
556 if (!NewTID.isPredicable())
557 // Can't transfer predicate, fail.
560 SkipPred = !NewTID.isPredicable();
565 const TargetInstrDesc &TID = MI->getDesc();
566 if (TID.hasOptionalDef()) {
567 unsigned NumOps = TID.getNumOperands();
568 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
569 if (HasCC && MI->getOperand(NumOps-1).isDead())
572 if (!VerifyPredAndCC(MI, Entry, true, Pred, LiveCPSR, HasCC, CCDead))
575 // Add the 16-bit instruction.
576 DebugLoc dl = MI->getDebugLoc();
577 MachineInstrBuilder MIB = BuildMI(MBB, *MI, dl, NewTID);
578 MIB.addOperand(MI->getOperand(0));
579 if (NewTID.hasOptionalDef()) {
581 AddDefaultT1CC(MIB, CCDead);
586 // Transfer the rest of operands.
587 unsigned NumOps = TID.getNumOperands();
588 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
589 if (i < NumOps && TID.OpInfo[i].isOptionalDef())
591 if (SkipPred && TID.OpInfo[i].isPredicate())
593 MIB.addOperand(MI->getOperand(i));
596 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
604 Thumb2SizeReduce::ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
605 const ReduceEntry &Entry,
607 if (ReduceLimit != -1 && ((int)NumNarrows >= ReduceLimit))
610 unsigned Limit = ~0U;
611 unsigned Scale = (Entry.WideOpc == ARM::t2ADDrSPi) ? 4 : 1;
613 Limit = ((1 << Entry.Imm1Limit) - 1) * Scale;
615 const TargetInstrDesc &TID = MI->getDesc();
616 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
617 if (TID.OpInfo[i].isPredicate())
619 const MachineOperand &MO = MI->getOperand(i);
621 unsigned Reg = MO.getReg();
622 if (!Reg || Reg == ARM::CPSR)
624 if (Entry.WideOpc == ARM::t2ADDrSPi && Reg == ARM::SP)
626 if (Entry.LowRegs1 && !isARMLowRegister(Reg))
628 } else if (MO.isImm() &&
629 !TID.OpInfo[i].isPredicate()) {
630 if (((unsigned)MO.getImm()) > Limit || (MO.getImm() & (Scale-1)) != 0)
635 // Check if it's possible / necessary to transfer the predicate.
636 const TargetInstrDesc &NewTID = TII->get(Entry.NarrowOpc1);
637 unsigned PredReg = 0;
638 ARMCC::CondCodes Pred = getInstrPredicate(MI, PredReg);
639 bool SkipPred = false;
640 if (Pred != ARMCC::AL) {
641 if (!NewTID.isPredicable())
642 // Can't transfer predicate, fail.
645 SkipPred = !NewTID.isPredicable();
650 if (TID.hasOptionalDef()) {
651 unsigned NumOps = TID.getNumOperands();
652 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
653 if (HasCC && MI->getOperand(NumOps-1).isDead())
656 if (!VerifyPredAndCC(MI, Entry, false, Pred, LiveCPSR, HasCC, CCDead))
659 // Add the 16-bit instruction.
660 DebugLoc dl = MI->getDebugLoc();
661 MachineInstrBuilder MIB = BuildMI(MBB, *MI, dl, NewTID);
662 MIB.addOperand(MI->getOperand(0));
663 if (NewTID.hasOptionalDef()) {
665 AddDefaultT1CC(MIB, CCDead);
670 // Transfer the rest of operands.
671 unsigned NumOps = TID.getNumOperands();
672 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
673 if (i < NumOps && TID.OpInfo[i].isOptionalDef())
675 if ((TID.getOpcode() == ARM::t2RSBSri ||
676 TID.getOpcode() == ARM::t2RSBri) && i == 2)
677 // Skip the zero immediate operand, it's now implicit.
679 bool isPred = (i < NumOps && TID.OpInfo[i].isPredicate());
680 if (SkipPred && isPred)
682 const MachineOperand &MO = MI->getOperand(i);
683 if (Scale > 1 && !isPred && MO.isImm())
684 MIB.addImm(MO.getImm() / Scale);
686 if (MO.isReg() && MO.isImplicit() && MO.getReg() == ARM::CPSR)
687 // Skip implicit def of CPSR. Either it's modeled as an optional
688 // def now or it's already an implicit def on the new instruction.
693 if (!TID.isPredicable() && NewTID.isPredicable())
696 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
703 static bool UpdateCPSRDef(MachineInstr &MI, bool LiveCPSR) {
705 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
706 const MachineOperand &MO = MI.getOperand(i);
707 if (!MO.isReg() || MO.isUndef() || MO.isUse())
709 if (MO.getReg() != ARM::CPSR)
715 return HasDef || LiveCPSR;
718 static bool UpdateCPSRUse(MachineInstr &MI, bool LiveCPSR) {
719 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
720 const MachineOperand &MO = MI.getOperand(i);
721 if (!MO.isReg() || MO.isUndef() || MO.isDef())
723 if (MO.getReg() != ARM::CPSR)
725 assert(LiveCPSR && "CPSR liveness tracking is wrong!");
735 bool Thumb2SizeReduce::ReduceMBB(MachineBasicBlock &MBB) {
736 bool Modified = false;
738 // Yes, CPSR could be livein.
739 bool LiveCPSR = MBB.isLiveIn(ARM::CPSR);
741 MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
742 MachineBasicBlock::iterator NextMII;
743 for (; MII != E; MII = NextMII) {
744 NextMII = llvm::next(MII);
746 MachineInstr *MI = &*MII;
747 LiveCPSR = UpdateCPSRUse(*MI, LiveCPSR);
749 unsigned Opcode = MI->getOpcode();
750 DenseMap<unsigned, unsigned>::iterator OPI = ReduceOpcodeMap.find(Opcode);
751 if (OPI != ReduceOpcodeMap.end()) {
752 const ReduceEntry &Entry = ReduceTable[OPI->second];
753 // Ignore "special" cases for now.
755 if (ReduceSpecial(MBB, MI, Entry, LiveCPSR)) {
757 MachineBasicBlock::iterator I = prior(NextMII);
763 // Try to transform to a 16-bit two-address instruction.
764 if (Entry.NarrowOpc2 && ReduceTo2Addr(MBB, MI, Entry, LiveCPSR)) {
766 MachineBasicBlock::iterator I = prior(NextMII);
771 // Try to transform to a 16-bit non-two-address instruction.
772 if (Entry.NarrowOpc1 && ReduceToNarrow(MBB, MI, Entry, LiveCPSR)) {
774 MachineBasicBlock::iterator I = prior(NextMII);
780 LiveCPSR = UpdateCPSRDef(*MI, LiveCPSR);
786 bool Thumb2SizeReduce::runOnMachineFunction(MachineFunction &MF) {
787 const TargetMachine &TM = MF.getTarget();
788 TII = static_cast<const Thumb2InstrInfo*>(TM.getInstrInfo());
790 bool Modified = false;
791 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
792 Modified |= ReduceMBB(*I);
796 /// createThumb2SizeReductionPass - Returns an instance of the Thumb2 size
798 FunctionPass *llvm::createThumb2SizeReductionPass() {
799 return new Thumb2SizeReduce();