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 "ARMBaseRegisterInfo.h"
13 #include "ARMBaseInstrInfo.h"
14 #include "Thumb2InstrInfo.h"
15 #include "llvm/CodeGen/MachineInstr.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/Statistic.h"
24 STATISTIC(NumNarrows, "Number of 32-bit instructions reduced to 16-bit ones");
25 STATISTIC(Num2Addrs, "Number of 32-bit instructions reduced to 2-address");
28 /// ReduceTable - A static table with information on mapping from wide
31 unsigned WideOpc; // Wide opcode
32 unsigned NarrowOpc1; // Narrow opcode to transform to
33 unsigned NarrowOpc2; // Narrow opcode when it's two-address
34 uint8_t Imm1Limit; // Limit of immediate field (bits)
35 uint8_t Imm2Limit; // Limit of immediate field when it's two-address
36 unsigned LowRegs1 : 1; // Only possible if low-registers are used
37 unsigned LowRegs2 : 1; // Only possible if low-registers are used (2addr)
38 unsigned PredCC1 : 1; // 0 - If predicated, cc is on and vice versa.
41 unsigned Special : 1; // Needs to be dealt with specially
44 static const ReduceEntry ReduceTable[] = {
45 // Wide, Narrow1, Narrow2, imm1,imm2, lo1, lo2, P/C, S
46 { ARM::t2ADCrr, ARM::tADC, 0, 0, 0, 1, 0, 0,0, 0 },
47 // FIXME: t2ADDS variants.
48 { ARM::t2ADDri, ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 0,0, 0 },
49 { ARM::t2ADDrr, ARM::tADDrr, ARM::tADDhirr, 0, 0, 1, 0, 0,1, 0 },
50 { ARM::t2ANDrr, ARM::tAND, 0, 0, 0, 1, 0, 0,0, 0 },
51 { ARM::t2ASRri, ARM::tASRri, 0, 5, 0, 1, 0, 0,0, 0 },
52 { ARM::t2ASRrr, ARM::tASRrr, 0, 0, 0, 1, 0, 0,0, 0 },
53 { ARM::t2BICrr, ARM::tBIC, 0, 0, 0, 1, 0, 0,0, 0 },
54 { ARM::t2CMNrr, ARM::tCMN, 0, 0, 0, 1, 0, 1,0, 0 },
55 { ARM::t2CMPri, ARM::tCMPi8, 0, 8, 0, 1, 0, 1,0, 0 },
56 { ARM::t2CMPrr, ARM::tCMPhir, 0, 0, 0, 0, 0, 1,0, 0 },
57 { ARM::t2CMPzri,ARM::tCMPzi8, 0, 8, 0, 1, 0, 1,0, 0 },
58 { ARM::t2CMPzrr,ARM::tCMPzhir,0, 0, 0, 0, 0, 1,0, 0 },
59 { ARM::t2EORrr, ARM::tEOR, 0, 0, 0, 1, 0, 0,0, 0 },
60 { ARM::t2LSLri, ARM::tLSLri, 0, 5, 0, 1, 0, 0,0, 0 },
61 { ARM::t2LSLrr, ARM::tLSLrr, 0, 0, 0, 1, 0, 0,0, 0 },
62 { ARM::t2LSRri, ARM::tLSRri, 0, 5, 0, 1, 0, 0,0, 0 },
63 { ARM::t2LSRrr, ARM::tLSRrr, 0, 0, 0, 1, 0, 0,0, 0 },
64 { ARM::t2MOVi, ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 0 },
65 // FIXME: Do we need the 16-bit 'S' variant?
66 { ARM::t2MOVr,ARM::tMOVgpr2gpr,0, 0, 0, 0, 0, 1,0, 0 },
67 { ARM::t2MUL, ARM::tMUL, 0, 0, 0, 1, 0, 0,0, 0 },
68 { ARM::t2MVNr, ARM::tMVN, 0, 0, 0, 1, 0, 0,0, 0 },
69 { ARM::t2ORRrr, ARM::tORR, 0, 0, 0, 1, 0, 0,0, 0 },
70 { ARM::t2REV, ARM::tREV, 0, 0, 0, 1, 0, 0,0, 0 },
71 { ARM::t2REV16, ARM::tREV16, 0, 0, 0, 1, 0, 0,0, 0 },
72 { ARM::t2REVSH, ARM::tREVSH, 0, 0, 0, 1, 0, 0,0, 0 },
73 { ARM::t2RORrr, ARM::tROR, 0, 0, 0, 1, 0, 0,0, 0 },
74 // FIXME: T2RSBri immediate must be zero. Also need entry for T2RSBS
75 //{ ARM::t2RSBri, ARM::tRSB, 0, 0, 0, 1, 0, 0,0, 0 },
76 { ARM::t2SUBri, ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 0,0, 0 },
77 { ARM::t2SUBrr, ARM::tSUBrr, 0, 0, 0, 1, 0, 0,0, 0 },
78 { ARM::t2SXTBr, ARM::tSXTB, 0, 0, 0, 1, 0, 1,0, 0 },
79 { ARM::t2SXTHr, ARM::tSXTH, 0, 0, 0, 1, 0, 1,0, 0 },
80 { ARM::t2TSTrr, ARM::tTST, 0, 0, 0, 1, 0, 1,0, 0 },
81 { ARM::t2UXTBr, ARM::tUXTB, 0, 0, 0, 1, 0, 1,0, 0 },
82 { ARM::t2UXTHr, ARM::tUXTH, 0, 0, 0, 1, 0, 1,0, 0 }
85 class VISIBILITY_HIDDEN Thumb2SizeReduce : public MachineFunctionPass {
90 const TargetInstrInfo *TII;
92 virtual bool runOnMachineFunction(MachineFunction &MF);
94 virtual const char *getPassName() const {
95 return "Thumb2 instruction size reduction pass";
99 /// ReduceOpcodeMap - Maps wide opcode to index of entry in ReduceTable.
100 DenseMap<unsigned, unsigned> ReduceOpcodeMap;
102 /// ReduceTo2Addr - Reduce a 32-bit instruction to a 16-bit two-address
104 bool ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
105 const ReduceEntry &Entry,
108 /// ReduceToNarrow - Reduce a 32-bit instruction to a 16-bit
109 /// non-two-address instruction.
110 bool ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
111 const ReduceEntry &Entry,
114 /// ReduceMBB - Reduce width of instructions in the specified basic block.
115 bool ReduceMBB(MachineBasicBlock &MBB);
117 char Thumb2SizeReduce::ID = 0;
120 Thumb2SizeReduce::Thumb2SizeReduce() : MachineFunctionPass(&ID) {
121 for (unsigned i = 0, e = array_lengthof(ReduceTable); i != e; ++i) {
122 unsigned FromOpc = ReduceTable[i].WideOpc;
123 if (!ReduceOpcodeMap.insert(std::make_pair(FromOpc, i)).second)
124 assert(false && "Duplicated entries?");
128 static bool VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
129 bool is2Addr, bool LiveCPSR,
130 bool &HasCC, bool &CCDead) {
131 unsigned PredReg = 0;
132 ARMCC::CondCodes Pred = getInstrPredicate(MI, PredReg);
133 if ((is2Addr && Entry.PredCC2 == 0) ||
134 (!is2Addr && Entry.PredCC1 == 0)) {
135 if (Pred == ARMCC::AL) {
136 // Not predicated, must set CPSR.
138 // Original instruction was not setting CPSR, but CPSR is not
139 // currently live anyway. It's ok to set it. The CPSR def is
149 // Predicated, must not set CPSR.
154 // 16-bit instruction does not set CPSR.
163 Thumb2SizeReduce::ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
164 const ReduceEntry &Entry,
166 const TargetInstrDesc &TID = MI->getDesc();
167 unsigned Reg0 = MI->getOperand(0).getReg();
168 unsigned Reg1 = MI->getOperand(1).getReg();
171 if (Entry.LowRegs2 && !isARMLowRegister(Reg0))
173 if (Entry.Imm2Limit) {
174 unsigned Imm = MI->getOperand(2).getImm();
175 unsigned Limit = (1 << Entry.Imm2Limit) - 1;
179 unsigned Reg2 = MI->getOperand(2).getReg();
180 if (Entry.LowRegs2 && !isARMLowRegister(Reg2))
186 if (TID.hasOptionalDef()) {
187 unsigned NumOps = TID.getNumOperands();
188 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
189 if (HasCC && MI->getOperand(NumOps-1).isDead())
192 if (!VerifyPredAndCC(MI, Entry, true, LiveCPSR, HasCC, CCDead))
195 // Add the 16-bit instruction.
196 DebugLoc dl = MI->getDebugLoc();
197 MachineInstrBuilder MIB = BuildMI(MBB, *MI, dl, TII->get(Entry.NarrowOpc2));
198 MIB.addOperand(MI->getOperand(0));
200 AddDefaultT1CC(MIB, CCDead);
202 // Transfer the rest of operands.
203 unsigned NumOps = TID.getNumOperands();
204 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i)
205 if (!(i < NumOps && TID.OpInfo[i].isOptionalDef()))
206 MIB.addOperand(MI->getOperand(i));
208 DOUT << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB;
217 Thumb2SizeReduce::ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
218 const ReduceEntry &Entry,
220 unsigned Limit = ~0U;
222 Limit = (1 << Entry.Imm1Limit) - 1;
224 const TargetInstrDesc &TID = MI->getDesc();
225 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
226 if (TID.OpInfo[i].isPredicate())
228 const MachineOperand &MO = MI->getOperand(i);
230 unsigned Reg = MO.getReg();
231 if (!Reg || Reg == ARM::CPSR)
233 if (Entry.LowRegs1 && !isARMLowRegister(Reg))
235 } else if (MO.isImm()) {
236 if (MO.getImm() > Limit)
243 if (TID.hasOptionalDef()) {
244 unsigned NumOps = TID.getNumOperands();
245 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
246 if (HasCC && MI->getOperand(NumOps-1).isDead())
249 if (!VerifyPredAndCC(MI, Entry, false, LiveCPSR, HasCC, CCDead))
252 // Add the 16-bit instruction.
253 DebugLoc dl = MI->getDebugLoc();
254 MachineInstrBuilder MIB = BuildMI(MBB, *MI, dl, TII->get(Entry.NarrowOpc1));
255 MIB.addOperand(MI->getOperand(0));
257 AddDefaultT1CC(MIB, CCDead);
259 // Transfer the rest of operands.
260 unsigned NumOps = TID.getNumOperands();
261 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i)
262 if (!(i < NumOps && TID.OpInfo[i].isOptionalDef()))
263 MIB.addOperand(MI->getOperand(i));
266 DOUT << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB;
274 static bool UpdateCPSRLiveness(MachineInstr &MI, bool LiveCPSR) {
276 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
277 const MachineOperand &MO = MI.getOperand(i);
278 if (!MO.isReg() || MO.isUndef())
280 if (MO.getReg() != ARM::CPSR)
288 assert(LiveCPSR && "CPSR liveness tracking is wrong!");
295 return HasDef || LiveCPSR;
298 bool Thumb2SizeReduce::ReduceMBB(MachineBasicBlock &MBB) {
299 bool Modified = false;
301 // FIXME: Track whether CPSR is live. If not, then it's possible to convert
302 // one that doesn't set CPSR to one that does.
303 bool LiveCPSR = false;
304 MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
305 MachineBasicBlock::iterator NextMII = next(MII);
306 for (; MII != E; MII = NextMII) {
309 MachineInstr *MI = &*MII;
310 unsigned Opcode = MI->getOpcode();
311 DenseMap<unsigned, unsigned>::iterator OPI = ReduceOpcodeMap.find(Opcode);
312 if (OPI != ReduceOpcodeMap.end()) {
313 const ReduceEntry &Entry = ReduceTable[OPI->second];
314 // Ignore "special" cases for now.
318 // Try to transform to a 16-bit two-address instruction.
319 if (Entry.NarrowOpc2 && ReduceTo2Addr(MBB, MI, Entry, LiveCPSR)) {
321 MachineBasicBlock::iterator I = prior(NextMII);
326 // Try to transform ro a 16-bit non-two-address instruction.
327 if (Entry.NarrowOpc1 && ReduceToNarrow(MBB, MI, Entry, LiveCPSR))
332 LiveCPSR = UpdateCPSRLiveness(*MI, LiveCPSR);
338 bool Thumb2SizeReduce::runOnMachineFunction(MachineFunction &MF) {
339 const TargetMachine &TM = MF.getTarget();
340 TII = TM.getInstrInfo();
342 bool Modified = false;
343 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
344 Modified |= ReduceMBB(*I);
348 /// createThumb2SizeReductionPass - Returns an instance of the Thumb2 size
350 FunctionPass *llvm::createThumb2SizeReductionPass() {
351 return new Thumb2SizeReduce();