blob: efc9bc3c230446425c8aa9508900ac993e86c6fb [file] [log] [blame]
Evan Cheng1be453b2009-08-08 03:21:23 +00001//===-- Thumb2SizeReduction.cpp - Thumb2 code size reduction pass -*- 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#define DEBUG_TYPE "t2-reduce-size"
11#include "ARM.h"
Evan Cheng1be453b2009-08-08 03:21:23 +000012#include "ARMBaseInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "ARMBaseRegisterInfo.h"
Bob Wilsona2881ee2011-04-19 18:11:49 +000014#include "ARMSubtarget.h"
Evan Chenga20cde32011-07-20 23:34:39 +000015#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "Thumb2InstrInfo.h"
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/Statistic.h"
19#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Cheng1be453b2009-08-08 03:21:23 +000020#include "llvm/CodeGen/MachineInstr.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chengf16a1d52009-08-10 07:20:37 +000022#include "llvm/Support/CommandLine.h"
Evan Cheng1be453b2009-08-08 03:21:23 +000023#include "llvm/Support/Debug.h"
Chris Lattnera6f074f2009-08-23 03:41:05 +000024#include "llvm/Support/raw_ostream.h"
Evan Cheng1be453b2009-08-08 03:21:23 +000025using namespace llvm;
26
Evan Cheng1f5bee12009-08-10 06:57:42 +000027STATISTIC(NumNarrows, "Number of 32-bit instrs reduced to 16-bit ones");
28STATISTIC(Num2Addrs, "Number of 32-bit instrs reduced to 2addr 16-bit ones");
Evan Cheng36064672009-08-11 08:52:18 +000029STATISTIC(NumLdSts, "Number of 32-bit load / store reduced to 16-bit ones");
Evan Cheng1be453b2009-08-08 03:21:23 +000030
Evan Chengcc9ca352009-08-11 21:11:32 +000031static cl::opt<int> ReduceLimit("t2-reduce-limit",
32 cl::init(-1), cl::Hidden);
33static cl::opt<int> ReduceLimit2Addr("t2-reduce-limit2",
34 cl::init(-1), cl::Hidden);
35static cl::opt<int> ReduceLimitLdSt("t2-reduce-limit3",
36 cl::init(-1), cl::Hidden);
Evan Chengf16a1d52009-08-10 07:20:37 +000037
Evan Cheng1be453b2009-08-08 03:21:23 +000038namespace {
39 /// ReduceTable - A static table with information on mapping from wide
40 /// opcodes to narrow
41 struct ReduceEntry {
Craig Topperca658c22012-03-11 07:16:55 +000042 uint16_t WideOpc; // Wide opcode
43 uint16_t NarrowOpc1; // Narrow opcode to transform to
44 uint16_t NarrowOpc2; // Narrow opcode when it's two-address
Evan Cheng1be453b2009-08-08 03:21:23 +000045 uint8_t Imm1Limit; // Limit of immediate field (bits)
46 uint8_t Imm2Limit; // Limit of immediate field when it's two-address
47 unsigned LowRegs1 : 1; // Only possible if low-registers are used
48 unsigned LowRegs2 : 1; // Only possible if low-registers are used (2addr)
Evan Cheng1e6c2a12009-08-12 01:49:45 +000049 unsigned PredCC1 : 2; // 0 - If predicated, cc is on and vice versa.
Evan Cheng1be453b2009-08-08 03:21:23 +000050 // 1 - No cc field.
Evan Cheng1e6c2a12009-08-12 01:49:45 +000051 // 2 - Always set CPSR.
Evan Chengaee7e492009-08-12 18:35:50 +000052 unsigned PredCC2 : 2;
Bob Wilsona2881ee2011-04-19 18:11:49 +000053 unsigned PartFlag : 1; // 16-bit instruction does partial flag update
Evan Cheng1be453b2009-08-08 03:21:23 +000054 unsigned Special : 1; // Needs to be dealt with specially
55 };
56
57 static const ReduceEntry ReduceTable[] = {
Bob Wilsona2881ee2011-04-19 18:11:49 +000058 // Wide, Narrow1, Narrow2, imm1,imm2, lo1, lo2, P/C, PF, S
59 { ARM::t2ADCrr, 0, ARM::tADC, 0, 0, 0, 1, 0,0, 0,0 },
Jim Grosbacha8a80672011-06-29 23:25:04 +000060 { ARM::t2ADDri, ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 0,0, 0,1 },
Bob Wilsona2881ee2011-04-19 18:11:49 +000061 { ARM::t2ADDrr, ARM::tADDrr, ARM::tADDhirr, 0, 0, 1, 0, 0,1, 0,0 },
Bob Wilsona2881ee2011-04-19 18:11:49 +000062 { ARM::t2ADDSri,ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 2,2, 0,1 },
63 { ARM::t2ADDSrr,ARM::tADDrr, 0, 0, 0, 1, 0, 2,0, 0,1 },
64 { ARM::t2ANDrr, 0, ARM::tAND, 0, 0, 0, 1, 0,0, 1,0 },
65 { ARM::t2ASRri, ARM::tASRri, 0, 5, 0, 1, 0, 0,0, 1,0 },
66 { ARM::t2ASRrr, 0, ARM::tASRrr, 0, 0, 0, 1, 0,0, 1,0 },
67 { ARM::t2BICrr, 0, ARM::tBIC, 0, 0, 0, 1, 0,0, 1,0 },
Jim Grosbach267430f2010-01-22 00:08:13 +000068 //FIXME: Disable CMN, as CCodes are backwards from compare expectations
Bob Wilsona2881ee2011-04-19 18:11:49 +000069 //{ ARM::t2CMNrr, ARM::tCMN, 0, 0, 0, 1, 0, 2,0, 0,0 },
Sebastian Pop2420e8b2012-05-04 19:53:56 +000070 { ARM::t2CMNzrr, ARM::tCMNz, 0, 0, 0, 1, 0, 2,0, 0,0 },
Bob Wilsona2881ee2011-04-19 18:11:49 +000071 { ARM::t2CMPri, ARM::tCMPi8, 0, 8, 0, 1, 0, 2,0, 0,0 },
72 { ARM::t2CMPrr, ARM::tCMPhir, 0, 0, 0, 0, 0, 2,0, 0,1 },
73 { ARM::t2EORrr, 0, ARM::tEOR, 0, 0, 0, 1, 0,0, 1,0 },
Evan Chengdb73d682009-08-14 00:32:16 +000074 // FIXME: adr.n immediate offset must be multiple of 4.
Bob Wilsona2881ee2011-04-19 18:11:49 +000075 //{ ARM::t2LEApcrelJT,ARM::tLEApcrelJT, 0, 0, 0, 1, 0, 1,0, 0,0 },
76 { ARM::t2LSLri, ARM::tLSLri, 0, 5, 0, 1, 0, 0,0, 1,0 },
77 { ARM::t2LSLrr, 0, ARM::tLSLrr, 0, 0, 0, 1, 0,0, 1,0 },
78 { ARM::t2LSRri, ARM::tLSRri, 0, 5, 0, 1, 0, 0,0, 1,0 },
79 { ARM::t2LSRrr, 0, ARM::tLSRrr, 0, 0, 0, 1, 0,0, 1,0 },
80 // FIXME: tMOVi8 and tMVN also partially update CPSR but they are less
81 // likely to cause issue in the loop. As a size / performance workaround,
82 // they are not marked as such.
83 { ARM::t2MOVi, ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 0,0 },
84 { ARM::t2MOVi16,ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 0,1 },
Evan Cheng51cbd2d2009-08-10 02:37:24 +000085 // FIXME: Do we need the 16-bit 'S' variant?
Jim Grosbache9cc9012011-06-30 23:38:17 +000086 { ARM::t2MOVr,ARM::tMOVr, 0, 0, 0, 0, 0, 1,0, 0,0 },
Bob Wilsona2881ee2011-04-19 18:11:49 +000087 { ARM::t2MUL, 0, ARM::tMUL, 0, 0, 0, 1, 0,0, 1,0 },
88 { ARM::t2MVNr, ARM::tMVN, 0, 0, 0, 1, 0, 0,0, 0,0 },
89 { ARM::t2ORRrr, 0, ARM::tORR, 0, 0, 0, 1, 0,0, 1,0 },
90 { ARM::t2REV, ARM::tREV, 0, 0, 0, 1, 0, 1,0, 0,0 },
91 { ARM::t2REV16, ARM::tREV16, 0, 0, 0, 1, 0, 1,0, 0,0 },
92 { ARM::t2REVSH, ARM::tREVSH, 0, 0, 0, 1, 0, 1,0, 0,0 },
93 { ARM::t2RORrr, 0, ARM::tROR, 0, 0, 0, 1, 0,0, 1,0 },
94 { ARM::t2RSBri, ARM::tRSB, 0, 0, 0, 1, 0, 0,0, 0,1 },
95 { ARM::t2RSBSri,ARM::tRSB, 0, 0, 0, 1, 0, 2,0, 0,1 },
96 { ARM::t2SBCrr, 0, ARM::tSBC, 0, 0, 0, 1, 0,0, 0,0 },
97 { ARM::t2SUBri, ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 0,0, 0,0 },
98 { ARM::t2SUBrr, ARM::tSUBrr, 0, 0, 0, 1, 0, 0,0, 0,0 },
99 { ARM::t2SUBSri,ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 2,2, 0,0 },
100 { ARM::t2SUBSrr,ARM::tSUBrr, 0, 0, 0, 1, 0, 2,0, 0,0 },
Jim Grosbach8b31ef52011-07-27 16:47:19 +0000101 { ARM::t2SXTB, ARM::tSXTB, 0, 0, 0, 1, 0, 1,0, 0,1 },
102 { ARM::t2SXTH, ARM::tSXTH, 0, 0, 0, 1, 0, 1,0, 0,1 },
Bob Wilsona2881ee2011-04-19 18:11:49 +0000103 { ARM::t2TSTrr, ARM::tTST, 0, 0, 0, 1, 0, 2,0, 0,0 },
Jim Grosbach8b31ef52011-07-27 16:47:19 +0000104 { ARM::t2UXTB, ARM::tUXTB, 0, 0, 0, 1, 0, 1,0, 0,1 },
105 { ARM::t2UXTH, ARM::tUXTH, 0, 0, 0, 1, 0, 1,0, 0,1 },
Evan Cheng36064672009-08-11 08:52:18 +0000106
107 // FIXME: Clean this up after splitting each Thumb load / store opcode
108 // into multiple ones.
Bob Wilsona2881ee2011-04-19 18:11:49 +0000109 { ARM::t2LDRi12,ARM::tLDRi, ARM::tLDRspi, 5, 8, 1, 0, 0,0, 0,1 },
110 { ARM::t2LDRs, ARM::tLDRr, 0, 0, 0, 1, 0, 0,0, 0,1 },
111 { ARM::t2LDRBi12,ARM::tLDRBi, 0, 5, 0, 1, 0, 0,0, 0,1 },
112 { ARM::t2LDRBs, ARM::tLDRBr, 0, 0, 0, 1, 0, 0,0, 0,1 },
113 { ARM::t2LDRHi12,ARM::tLDRHi, 0, 5, 0, 1, 0, 0,0, 0,1 },
114 { ARM::t2LDRHs, ARM::tLDRHr, 0, 0, 0, 1, 0, 0,0, 0,1 },
115 { ARM::t2LDRSBs,ARM::tLDRSB, 0, 0, 0, 1, 0, 0,0, 0,1 },
116 { ARM::t2LDRSHs,ARM::tLDRSH, 0, 0, 0, 1, 0, 0,0, 0,1 },
117 { ARM::t2STRi12,ARM::tSTRi, ARM::tSTRspi, 5, 8, 1, 0, 0,0, 0,1 },
118 { ARM::t2STRs, ARM::tSTRr, 0, 0, 0, 1, 0, 0,0, 0,1 },
119 { ARM::t2STRBi12,ARM::tSTRBi, 0, 5, 0, 1, 0, 0,0, 0,1 },
120 { ARM::t2STRBs, ARM::tSTRBr, 0, 0, 0, 1, 0, 0,0, 0,1 },
121 { ARM::t2STRHi12,ARM::tSTRHi, 0, 5, 0, 1, 0, 0,0, 0,1 },
122 { ARM::t2STRHs, ARM::tSTRHr, 0, 0, 0, 1, 0, 0,0, 0,1 },
Evan Chengcc9ca352009-08-11 21:11:32 +0000123
Bob Wilsona2881ee2011-04-19 18:11:49 +0000124 { ARM::t2LDMIA, ARM::tLDMIA, 0, 0, 0, 1, 1, 1,1, 0,1 },
125 { ARM::t2LDMIA_RET,0, ARM::tPOP_RET, 0, 0, 1, 1, 1,1, 0,1 },
126 { ARM::t2LDMIA_UPD,ARM::tLDMIA_UPD,ARM::tPOP,0, 0, 1, 1, 1,1, 0,1 },
Bob Wilson947f04b2010-03-13 01:08:20 +0000127 // ARM::t2STM (with no basereg writeback) has no Thumb1 equivalent
Bob Wilsona2881ee2011-04-19 18:11:49 +0000128 { ARM::t2STMIA_UPD,ARM::tSTMIA_UPD, 0, 0, 0, 1, 1, 1,1, 0,1 },
129 { ARM::t2STMDB_UPD, 0, ARM::tPUSH, 0, 0, 1, 1, 1,1, 0,1 },
Evan Cheng1be453b2009-08-08 03:21:23 +0000130 };
131
Nick Lewycky02d5f772009-10-25 06:33:48 +0000132 class Thumb2SizeReduce : public MachineFunctionPass {
Evan Cheng1be453b2009-08-08 03:21:23 +0000133 public:
134 static char ID;
135 Thumb2SizeReduce();
136
Evan Cheng6ddd7bc2009-08-15 07:59:10 +0000137 const Thumb2InstrInfo *TII;
Bob Wilsona2881ee2011-04-19 18:11:49 +0000138 const ARMSubtarget *STI;
Evan Cheng1be453b2009-08-08 03:21:23 +0000139
140 virtual bool runOnMachineFunction(MachineFunction &MF);
141
142 virtual const char *getPassName() const {
143 return "Thumb2 instruction size reduction pass";
144 }
145
146 private:
147 /// ReduceOpcodeMap - Maps wide opcode to index of entry in ReduceTable.
148 DenseMap<unsigned, unsigned> ReduceOpcodeMap;
149
Evan Chengf4807a12011-10-27 21:21:05 +0000150 bool canAddPseudoFlagDep(MachineInstr *Def, MachineInstr *Use,
151 bool IsSelfLoop);
Bob Wilsona2881ee2011-04-19 18:11:49 +0000152
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000153 bool VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
154 bool is2Addr, ARMCC::CondCodes Pred,
155 bool LiveCPSR, bool &HasCC, bool &CCDead);
156
Evan Cheng36064672009-08-11 08:52:18 +0000157 bool ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
158 const ReduceEntry &Entry);
159
160 bool ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
Bob Wilsona2881ee2011-04-19 18:11:49 +0000161 const ReduceEntry &Entry, bool LiveCPSR,
Evan Chengf4807a12011-10-27 21:21:05 +0000162 MachineInstr *CPSRDef, bool IsSelfLoop);
Evan Cheng36064672009-08-11 08:52:18 +0000163
Evan Cheng1be453b2009-08-08 03:21:23 +0000164 /// ReduceTo2Addr - Reduce a 32-bit instruction to a 16-bit two-address
165 /// instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000166 bool ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
167 const ReduceEntry &Entry,
Evan Chengf4807a12011-10-27 21:21:05 +0000168 bool LiveCPSR, MachineInstr *CPSRDef,
169 bool IsSelfLoop);
Evan Cheng1be453b2009-08-08 03:21:23 +0000170
171 /// ReduceToNarrow - Reduce a 32-bit instruction to a 16-bit
172 /// non-two-address instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000173 bool ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
174 const ReduceEntry &Entry,
Evan Chengf4807a12011-10-27 21:21:05 +0000175 bool LiveCPSR, MachineInstr *CPSRDef,
176 bool IsSelfLoop);
Evan Cheng1be453b2009-08-08 03:21:23 +0000177
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000178 /// ReduceMI - Attempt to reduce MI, return true on success.
179 bool ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI,
180 bool LiveCPSR, MachineInstr *CPSRDef,
181 bool IsSelfLoop);
182
Evan Cheng1be453b2009-08-08 03:21:23 +0000183 /// ReduceMBB - Reduce width of instructions in the specified basic block.
184 bool ReduceMBB(MachineBasicBlock &MBB);
185 };
186 char Thumb2SizeReduce::ID = 0;
187}
188
Owen Andersona7aed182010-08-06 18:33:48 +0000189Thumb2SizeReduce::Thumb2SizeReduce() : MachineFunctionPass(ID) {
Evan Cheng1be453b2009-08-08 03:21:23 +0000190 for (unsigned i = 0, e = array_lengthof(ReduceTable); i != e; ++i) {
191 unsigned FromOpc = ReduceTable[i].WideOpc;
192 if (!ReduceOpcodeMap.insert(std::make_pair(FromOpc, i)).second)
193 assert(false && "Duplicated entries?");
194 }
195}
196
Evan Cheng6cc775f2011-06-28 19:10:37 +0000197static bool HasImplicitCPSRDef(const MCInstrDesc &MCID) {
Craig Topper5a4bcc72012-03-08 08:22:45 +0000198 for (const uint16_t *Regs = MCID.getImplicitDefs(); *Regs; ++Regs)
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000199 if (*Regs == ARM::CPSR)
200 return true;
201 return false;
202}
203
Bob Wilsona2881ee2011-04-19 18:11:49 +0000204/// canAddPseudoFlagDep - For A9 (and other out-of-order) implementations,
205/// the 's' 16-bit instruction partially update CPSR. Abort the
206/// transformation to avoid adding false dependency on last CPSR setting
207/// instruction which hurts the ability for out-of-order execution engine
208/// to do register renaming magic.
209/// This function checks if there is a read-of-write dependency between the
210/// last instruction that defines the CPSR and the current instruction. If there
211/// is, then there is no harm done since the instruction cannot be retired
212/// before the CPSR setting instruction anyway.
213/// Note, we are not doing full dependency analysis here for the sake of compile
214/// time. We're not looking for cases like:
215/// r0 = muls ...
216/// r1 = add.w r0, ...
217/// ...
218/// = mul.w r1
219/// In this case it would have been ok to narrow the mul.w to muls since there
220/// are indirect RAW dependency between the muls and the mul.w
221bool
Evan Chengf4807a12011-10-27 21:21:05 +0000222Thumb2SizeReduce::canAddPseudoFlagDep(MachineInstr *Def, MachineInstr *Use,
223 bool FirstInSelfLoop) {
224 // FIXME: Disable check for -Oz (aka OptimizeForSizeHarder).
225 if (!STI->avoidCPSRPartialUpdate())
Bob Wilsona2881ee2011-04-19 18:11:49 +0000226 return false;
227
Evan Chengf4807a12011-10-27 21:21:05 +0000228 if (!Def)
229 // If this BB loops back to itself, conservatively avoid narrowing the
230 // first instruction that does partial flag update.
231 return FirstInSelfLoop;
232
Bob Wilsona2881ee2011-04-19 18:11:49 +0000233 SmallSet<unsigned, 2> Defs;
234 for (unsigned i = 0, e = Def->getNumOperands(); i != e; ++i) {
235 const MachineOperand &MO = Def->getOperand(i);
236 if (!MO.isReg() || MO.isUndef() || MO.isUse())
237 continue;
238 unsigned Reg = MO.getReg();
239 if (Reg == 0 || Reg == ARM::CPSR)
240 continue;
241 Defs.insert(Reg);
242 }
243
244 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
245 const MachineOperand &MO = Use->getOperand(i);
246 if (!MO.isReg() || MO.isUndef() || MO.isDef())
247 continue;
248 unsigned Reg = MO.getReg();
249 if (Defs.count(Reg))
250 return false;
251 }
252
253 // No read-after-write dependency. The narrowing will add false dependency.
254 return true;
255}
256
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000257bool
258Thumb2SizeReduce::VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
259 bool is2Addr, ARMCC::CondCodes Pred,
260 bool LiveCPSR, bool &HasCC, bool &CCDead) {
Evan Chengd461c1c2009-08-09 19:17:19 +0000261 if ((is2Addr && Entry.PredCC2 == 0) ||
262 (!is2Addr && Entry.PredCC1 == 0)) {
263 if (Pred == ARMCC::AL) {
264 // Not predicated, must set CPSR.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000265 if (!HasCC) {
266 // Original instruction was not setting CPSR, but CPSR is not
267 // currently live anyway. It's ok to set it. The CPSR def is
268 // dead though.
269 if (!LiveCPSR) {
270 HasCC = true;
271 CCDead = true;
272 return true;
273 }
274 return false;
275 }
Evan Chengd461c1c2009-08-09 19:17:19 +0000276 } else {
277 // Predicated, must not set CPSR.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000278 if (HasCC)
279 return false;
Evan Chengd461c1c2009-08-09 19:17:19 +0000280 }
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000281 } else if ((is2Addr && Entry.PredCC2 == 2) ||
282 (!is2Addr && Entry.PredCC1 == 2)) {
283 /// Old opcode has an optional def of CPSR.
284 if (HasCC)
285 return true;
Jim Grosbachbc7eeaf2010-09-14 20:35:46 +0000286 // If old opcode does not implicitly define CPSR, then it's not ok since
287 // these new opcodes' CPSR def is not meant to be thrown away. e.g. CMP.
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000288 if (!HasImplicitCPSRDef(MI->getDesc()))
289 return false;
290 HasCC = true;
Evan Chengd461c1c2009-08-09 19:17:19 +0000291 } else {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000292 // 16-bit instruction does not set CPSR.
293 if (HasCC)
294 return false;
Evan Chengd461c1c2009-08-09 19:17:19 +0000295 }
296
297 return true;
298}
299
Evan Chengcc9ca352009-08-11 21:11:32 +0000300static bool VerifyLowRegs(MachineInstr *MI) {
301 unsigned Opc = MI->getOpcode();
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000302 bool isPCOk = (Opc == ARM::t2LDMIA_RET || Opc == ARM::t2LDMIA ||
303 Opc == ARM::t2LDMDB || Opc == ARM::t2LDMIA_UPD ||
Owen Anderson4ebf4712011-02-08 22:39:40 +0000304 Opc == ARM::t2LDMDB_UPD);
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000305 bool isLROk = (Opc == ARM::t2STMIA_UPD || Opc == ARM::t2STMDB_UPD);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000306 bool isSPOk = isPCOk || isLROk;
Evan Chengcc9ca352009-08-11 21:11:32 +0000307 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
308 const MachineOperand &MO = MI->getOperand(i);
309 if (!MO.isReg() || MO.isImplicit())
310 continue;
311 unsigned Reg = MO.getReg();
312 if (Reg == 0 || Reg == ARM::CPSR)
313 continue;
314 if (isPCOk && Reg == ARM::PC)
315 continue;
316 if (isLROk && Reg == ARM::LR)
317 continue;
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000318 if (Reg == ARM::SP) {
319 if (isSPOk)
320 continue;
321 if (i == 1 && (Opc == ARM::t2LDRi12 || Opc == ARM::t2STRi12))
322 // Special case for these ldr / str with sp as base register.
323 continue;
324 }
Evan Chengcc9ca352009-08-11 21:11:32 +0000325 if (!isARMLowRegister(Reg))
326 return false;
327 }
328 return true;
329}
330
Evan Cheng1be453b2009-08-08 03:21:23 +0000331bool
Evan Cheng36064672009-08-11 08:52:18 +0000332Thumb2SizeReduce::ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
333 const ReduceEntry &Entry) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000334 if (ReduceLimitLdSt != -1 && ((int)NumLdSts >= ReduceLimitLdSt))
335 return false;
336
Evan Cheng36064672009-08-11 08:52:18 +0000337 unsigned Scale = 1;
338 bool HasImmOffset = false;
339 bool HasShift = false;
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000340 bool HasOffReg = true;
Evan Chengcc9ca352009-08-11 21:11:32 +0000341 bool isLdStMul = false;
Evan Chengcc9ca352009-08-11 21:11:32 +0000342 unsigned Opc = Entry.NarrowOpc1;
343 unsigned OpNum = 3; // First 'rest' of operands.
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000344 uint8_t ImmLimit = Entry.Imm1Limit;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000345
Evan Cheng36064672009-08-11 08:52:18 +0000346 switch (Entry.WideOpc) {
347 default:
348 llvm_unreachable("Unexpected Thumb2 load / store opcode!");
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000349 case ARM::t2LDRi12:
Bill Wendling092a7bd2010-12-14 03:36:38 +0000350 case ARM::t2STRi12:
351 if (MI->getOperand(1).getReg() == ARM::SP) {
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000352 Opc = Entry.NarrowOpc2;
353 ImmLimit = Entry.Imm2Limit;
354 HasOffReg = false;
355 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000356
Evan Cheng36064672009-08-11 08:52:18 +0000357 Scale = 4;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000358 HasImmOffset = true;
359 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000360 break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000361 case ARM::t2LDRBi12:
Evan Cheng36064672009-08-11 08:52:18 +0000362 case ARM::t2STRBi12:
Owen Anderson4ebf4712011-02-08 22:39:40 +0000363 HasImmOffset = true;
364 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000365 break;
366 case ARM::t2LDRHi12:
367 case ARM::t2STRHi12:
368 Scale = 2;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000369 HasImmOffset = true;
370 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000371 break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000372 case ARM::t2LDRs:
373 case ARM::t2LDRBs:
374 case ARM::t2LDRHs:
Evan Cheng36064672009-08-11 08:52:18 +0000375 case ARM::t2LDRSBs:
376 case ARM::t2LDRSHs:
377 case ARM::t2STRs:
378 case ARM::t2STRBs:
379 case ARM::t2STRHs:
380 HasShift = true;
Evan Chengcc9ca352009-08-11 21:11:32 +0000381 OpNum = 4;
Evan Cheng36064672009-08-11 08:52:18 +0000382 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000383 case ARM::t2LDMIA:
384 case ARM::t2LDMDB: {
Evan Chengcc9ca352009-08-11 21:11:32 +0000385 unsigned BaseReg = MI->getOperand(0).getReg();
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000386 if (!isARMLowRegister(BaseReg) || Entry.WideOpc != ARM::t2LDMIA)
Bob Wilson947f04b2010-03-13 01:08:20 +0000387 return false;
Bill Wendling092a7bd2010-12-14 03:36:38 +0000388
Jim Grosbach88628e92010-09-07 22:30:53 +0000389 // For the non-writeback version (this one), the base register must be
390 // one of the registers being loaded.
391 bool isOK = false;
392 for (unsigned i = 4; i < MI->getNumOperands(); ++i) {
393 if (MI->getOperand(i).getReg() == BaseReg) {
394 isOK = true;
395 break;
396 }
397 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000398
Jim Grosbach88628e92010-09-07 22:30:53 +0000399 if (!isOK)
400 return false;
401
Bob Wilson947f04b2010-03-13 01:08:20 +0000402 OpNum = 0;
403 isLdStMul = true;
404 break;
405 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000406 case ARM::t2LDMIA_RET: {
Bob Wilson947f04b2010-03-13 01:08:20 +0000407 unsigned BaseReg = MI->getOperand(1).getReg();
408 if (BaseReg != ARM::SP)
409 return false;
410 Opc = Entry.NarrowOpc2; // tPOP_RET
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000411 OpNum = 2;
Bob Wilson947f04b2010-03-13 01:08:20 +0000412 isLdStMul = true;
413 break;
414 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000415 case ARM::t2LDMIA_UPD:
416 case ARM::t2LDMDB_UPD:
417 case ARM::t2STMIA_UPD:
418 case ARM::t2STMDB_UPD: {
Bob Wilson947f04b2010-03-13 01:08:20 +0000419 OpNum = 0;
Bill Wendling092a7bd2010-12-14 03:36:38 +0000420
Bob Wilson947f04b2010-03-13 01:08:20 +0000421 unsigned BaseReg = MI->getOperand(1).getReg();
Bob Wilson947f04b2010-03-13 01:08:20 +0000422 if (BaseReg == ARM::SP &&
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000423 (Entry.WideOpc == ARM::t2LDMIA_UPD ||
424 Entry.WideOpc == ARM::t2STMDB_UPD)) {
Bob Wilson947f04b2010-03-13 01:08:20 +0000425 Opc = Entry.NarrowOpc2; // tPOP or tPUSH
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000426 OpNum = 2;
427 } else if (!isARMLowRegister(BaseReg) ||
428 (Entry.WideOpc != ARM::t2LDMIA_UPD &&
429 Entry.WideOpc != ARM::t2STMIA_UPD)) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000430 return false;
431 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000432
Evan Chengcc9ca352009-08-11 21:11:32 +0000433 isLdStMul = true;
434 break;
435 }
Evan Cheng36064672009-08-11 08:52:18 +0000436 }
437
438 unsigned OffsetReg = 0;
439 bool OffsetKill = false;
440 if (HasShift) {
441 OffsetReg = MI->getOperand(2).getReg();
442 OffsetKill = MI->getOperand(2).isKill();
Bill Wendling092a7bd2010-12-14 03:36:38 +0000443
Evan Cheng36064672009-08-11 08:52:18 +0000444 if (MI->getOperand(3).getImm())
445 // Thumb1 addressing mode doesn't support shift.
446 return false;
447 }
448
449 unsigned OffsetImm = 0;
450 if (HasImmOffset) {
451 OffsetImm = MI->getOperand(2).getImm();
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000452 unsigned MaxOffset = ((1 << ImmLimit) - 1) * Scale;
Bill Wendling092a7bd2010-12-14 03:36:38 +0000453
454 if ((OffsetImm & (Scale - 1)) || OffsetImm > MaxOffset)
Evan Cheng36064672009-08-11 08:52:18 +0000455 // Make sure the immediate field fits.
456 return false;
457 }
458
459 // Add the 16-bit load / store instruction.
Evan Cheng36064672009-08-11 08:52:18 +0000460 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000461 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, TII->get(Opc));
Evan Chengcc9ca352009-08-11 21:11:32 +0000462 if (!isLdStMul) {
Owen Anderson99ea8a32010-12-07 00:45:21 +0000463 MIB.addOperand(MI->getOperand(0));
Owen Anderson4ebf4712011-02-08 22:39:40 +0000464 MIB.addOperand(MI->getOperand(1));
Bill Wendling092a7bd2010-12-14 03:36:38 +0000465
466 if (HasImmOffset)
467 MIB.addImm(OffsetImm / Scale);
468
Evan Chengcc9ca352009-08-11 21:11:32 +0000469 assert((!HasShift || OffsetReg) && "Invalid so_reg load / store address!");
470
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000471 if (HasOffReg)
472 MIB.addReg(OffsetReg, getKillRegState(OffsetKill));
Evan Cheng36064672009-08-11 08:52:18 +0000473 }
Evan Cheng806845d2009-08-11 09:37:40 +0000474
Evan Cheng36064672009-08-11 08:52:18 +0000475 // Transfer the rest of operands.
Evan Cheng36064672009-08-11 08:52:18 +0000476 for (unsigned e = MI->getNumOperands(); OpNum != e; ++OpNum)
477 MIB.addOperand(MI->getOperand(OpNum));
478
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000479 // Transfer memoperands.
Chris Lattner1d0c2572011-04-29 05:24:29 +0000480 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000481
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000482 // Transfer MI flags.
483 MIB.setMIFlags(MI->getFlags());
484
Chris Lattnera6f074f2009-08-23 03:41:05 +0000485 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Cheng36064672009-08-11 08:52:18 +0000486
Evan Cheng7fae11b2011-12-14 02:11:42 +0000487 MBB.erase_instr(MI);
Evan Cheng36064672009-08-11 08:52:18 +0000488 ++NumLdSts;
489 return true;
490}
491
Evan Cheng36064672009-08-11 08:52:18 +0000492bool
493Thumb2SizeReduce::ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
494 const ReduceEntry &Entry,
Evan Chengf4807a12011-10-27 21:21:05 +0000495 bool LiveCPSR, MachineInstr *CPSRDef,
496 bool IsSelfLoop) {
Jim Grosbacha8a80672011-06-29 23:25:04 +0000497 unsigned Opc = MI->getOpcode();
498 if (Opc == ARM::t2ADDri) {
499 // If the source register is SP, try to reduce to tADDrSPi, otherwise
500 // it's a normal reduce.
501 if (MI->getOperand(1).getReg() != ARM::SP) {
Evan Chengf4807a12011-10-27 21:21:05 +0000502 if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop))
Jim Grosbacha8a80672011-06-29 23:25:04 +0000503 return true;
Evan Chengf4807a12011-10-27 21:21:05 +0000504 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000505 }
506 // Try to reduce to tADDrSPi.
507 unsigned Imm = MI->getOperand(2).getImm();
508 // The immediate must be in range, the destination register must be a low
Jim Grosbached5134a2011-06-30 02:22:49 +0000509 // reg, the predicate must be "always" and the condition flags must not
510 // be being set.
Jim Grosbach68b0e842011-07-01 19:07:09 +0000511 if (Imm & 3 || Imm > 1020)
Jim Grosbacha8a80672011-06-29 23:25:04 +0000512 return false;
513 if (!isARMLowRegister(MI->getOperand(0).getReg()))
514 return false;
Jim Grosbached5134a2011-06-30 02:22:49 +0000515 if (MI->getOperand(3).getImm() != ARMCC::AL)
516 return false;
Jim Grosbacha8a80672011-06-29 23:25:04 +0000517 const MCInstrDesc &MCID = MI->getDesc();
518 if (MCID.hasOptionalDef() &&
519 MI->getOperand(MCID.getNumOperands()-1).getReg() == ARM::CPSR)
520 return false;
521
Evan Cheng7fae11b2011-12-14 02:11:42 +0000522 MachineInstrBuilder MIB = BuildMI(MBB, MI, MI->getDebugLoc(),
Jim Grosbacha8a80672011-06-29 23:25:04 +0000523 TII->get(ARM::tADDrSPi))
524 .addOperand(MI->getOperand(0))
525 .addOperand(MI->getOperand(1))
526 .addImm(Imm / 4); // The tADDrSPi has an implied scale by four.
Jim Grosbach1b8457a2011-08-24 17:46:13 +0000527 AddDefaultPred(MIB);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000528
529 // Transfer MI flags.
530 MIB.setMIFlags(MI->getFlags());
531
532 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " <<*MIB);
533
Evan Cheng7fae11b2011-12-14 02:11:42 +0000534 MBB.erase_instr(MI);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000535 ++NumNarrows;
536 return true;
537 }
538
Evan Chengcc9ca352009-08-11 21:11:32 +0000539 if (Entry.LowRegs1 && !VerifyLowRegs(MI))
Evan Cheng36064672009-08-11 08:52:18 +0000540 return false;
541
Evan Cheng7f8e5632011-12-07 07:15:52 +0000542 if (MI->mayLoad() || MI->mayStore())
Evan Cheng36064672009-08-11 08:52:18 +0000543 return ReduceLoadStore(MBB, MI, Entry);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000544
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000545 switch (Opc) {
546 default: break;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000547 case ARM::t2ADDSri:
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000548 case ARM::t2ADDSrr: {
549 unsigned PredReg = 0;
550 if (getInstrPredicate(MI, PredReg) == ARMCC::AL) {
551 switch (Opc) {
552 default: break;
553 case ARM::t2ADDSri: {
Evan Chengf4807a12011-10-27 21:21:05 +0000554 if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop))
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000555 return true;
556 // fallthrough
557 }
558 case ARM::t2ADDSrr:
Evan Chengf4807a12011-10-27 21:21:05 +0000559 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000560 }
561 }
562 break;
563 }
564 case ARM::t2RSBri:
565 case ARM::t2RSBSri:
Jim Grosbach8b31ef52011-07-27 16:47:19 +0000566 case ARM::t2SXTB:
567 case ARM::t2SXTH:
568 case ARM::t2UXTB:
569 case ARM::t2UXTH:
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000570 if (MI->getOperand(2).getImm() == 0)
Evan Chengf4807a12011-10-27 21:21:05 +0000571 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000572 break;
Anton Korobeynikov25229082009-11-24 00:44:37 +0000573 case ARM::t2MOVi16:
574 // Can convert only 'pure' immediate operands, not immediates obtained as
575 // globals' addresses.
576 if (MI->getOperand(1).isImm())
Evan Chengf4807a12011-10-27 21:21:05 +0000577 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop);
Anton Korobeynikov25229082009-11-24 00:44:37 +0000578 break;
Jim Grosbach327cf8e2010-12-07 20:41:06 +0000579 case ARM::t2CMPrr: {
Jim Grosbach5bae0542010-12-03 23:54:18 +0000580 // Try to reduce to the lo-reg only version first. Why there are two
581 // versions of the instruction is a mystery.
582 // It would be nice to just have two entries in the master table that
583 // are prioritized, but the table assumes a unique entry for each
584 // source insn opcode. So for now, we hack a local entry record to use.
585 static const ReduceEntry NarrowEntry =
Bob Wilsona2881ee2011-04-19 18:11:49 +0000586 { ARM::t2CMPrr,ARM::tCMPr, 0, 0, 0, 1, 1,2, 0, 0,1 };
Evan Chengf4807a12011-10-27 21:21:05 +0000587 if (ReduceToNarrow(MBB, MI, NarrowEntry, LiveCPSR, CPSRDef, IsSelfLoop))
Jim Grosbach5bae0542010-12-03 23:54:18 +0000588 return true;
Evan Chengf4807a12011-10-27 21:21:05 +0000589 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop);
Jim Grosbach5bae0542010-12-03 23:54:18 +0000590 }
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000591 }
Evan Cheng36064672009-08-11 08:52:18 +0000592 return false;
593}
594
595bool
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000596Thumb2SizeReduce::ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
597 const ReduceEntry &Entry,
Evan Chengf4807a12011-10-27 21:21:05 +0000598 bool LiveCPSR, MachineInstr *CPSRDef,
599 bool IsSelfLoop) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000600
601 if (ReduceLimit2Addr != -1 && ((int)Num2Addrs >= ReduceLimit2Addr))
602 return false;
603
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000604 unsigned Reg0 = MI->getOperand(0).getReg();
605 unsigned Reg1 = MI->getOperand(1).getReg();
Jim Grosbachc01104d2012-02-24 00:33:36 +0000606 // t2MUL is "special". The tied source operand is second, not first.
607 if (MI->getOpcode() == ARM::t2MUL) {
Jim Grosbach3a21e2c2012-02-24 00:53:11 +0000608 unsigned Reg2 = MI->getOperand(2).getReg();
609 // Early exit if the regs aren't all low regs.
610 if (!isARMLowRegister(Reg0) || !isARMLowRegister(Reg1)
611 || !isARMLowRegister(Reg2))
612 return false;
613 if (Reg0 != Reg2) {
Jim Grosbachc01104d2012-02-24 00:33:36 +0000614 // If the other operand also isn't the same as the destination, we
615 // can't reduce.
616 if (Reg1 != Reg0)
617 return false;
618 // Try to commute the operands to make it a 2-address instruction.
619 MachineInstr *CommutedMI = TII->commuteInstruction(MI);
620 if (!CommutedMI)
621 return false;
622 }
623 } else if (Reg0 != Reg1) {
Bob Wilson279e55f2010-06-24 16:50:20 +0000624 // Try to commute the operands to make it a 2-address instruction.
625 unsigned CommOpIdx1, CommOpIdx2;
626 if (!TII->findCommutedOpIndices(MI, CommOpIdx1, CommOpIdx2) ||
627 CommOpIdx1 != 1 || MI->getOperand(CommOpIdx2).getReg() != Reg0)
628 return false;
629 MachineInstr *CommutedMI = TII->commuteInstruction(MI);
630 if (!CommutedMI)
631 return false;
632 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000633 if (Entry.LowRegs2 && !isARMLowRegister(Reg0))
634 return false;
635 if (Entry.Imm2Limit) {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000636 unsigned Imm = MI->getOperand(2).getImm();
Evan Cheng1be453b2009-08-08 03:21:23 +0000637 unsigned Limit = (1 << Entry.Imm2Limit) - 1;
638 if (Imm > Limit)
639 return false;
640 } else {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000641 unsigned Reg2 = MI->getOperand(2).getReg();
Evan Cheng1be453b2009-08-08 03:21:23 +0000642 if (Entry.LowRegs2 && !isARMLowRegister(Reg2))
643 return false;
644 }
645
Evan Cheng1f5bee12009-08-10 06:57:42 +0000646 // Check if it's possible / necessary to transfer the predicate.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000647 const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc2);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000648 unsigned PredReg = 0;
649 ARMCC::CondCodes Pred = getInstrPredicate(MI, PredReg);
650 bool SkipPred = false;
651 if (Pred != ARMCC::AL) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000652 if (!NewMCID.isPredicable())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000653 // Can't transfer predicate, fail.
654 return false;
655 } else {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000656 SkipPred = !NewMCID.isPredicable();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000657 }
658
Evan Cheng1be453b2009-08-08 03:21:23 +0000659 bool HasCC = false;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000660 bool CCDead = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000661 const MCInstrDesc &MCID = MI->getDesc();
662 if (MCID.hasOptionalDef()) {
663 unsigned NumOps = MCID.getNumOperands();
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000664 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
665 if (HasCC && MI->getOperand(NumOps-1).isDead())
666 CCDead = true;
667 }
Evan Cheng1f5bee12009-08-10 06:57:42 +0000668 if (!VerifyPredAndCC(MI, Entry, true, Pred, LiveCPSR, HasCC, CCDead))
Evan Chengd461c1c2009-08-09 19:17:19 +0000669 return false;
Evan Cheng1be453b2009-08-08 03:21:23 +0000670
Bob Wilsona2881ee2011-04-19 18:11:49 +0000671 // Avoid adding a false dependency on partial flag update by some 16-bit
672 // instructions which has the 's' bit set.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000673 if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
Evan Chengf4807a12011-10-27 21:21:05 +0000674 canAddPseudoFlagDep(CPSRDef, MI, IsSelfLoop))
Bob Wilsona2881ee2011-04-19 18:11:49 +0000675 return false;
676
Evan Cheng1be453b2009-08-08 03:21:23 +0000677 // Add the 16-bit instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000678 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000679 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000680 MIB.addOperand(MI->getOperand(0));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000681 if (NewMCID.hasOptionalDef()) {
Evan Cheng6ddd7bc2009-08-15 07:59:10 +0000682 if (HasCC)
683 AddDefaultT1CC(MIB, CCDead);
684 else
685 AddNoT1CC(MIB);
686 }
Evan Chengd461c1c2009-08-09 19:17:19 +0000687
688 // Transfer the rest of operands.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000689 unsigned NumOps = MCID.getNumOperands();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000690 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000691 if (i < NumOps && MCID.OpInfo[i].isOptionalDef())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000692 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000693 if (SkipPred && MCID.OpInfo[i].isPredicate())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000694 continue;
695 MIB.addOperand(MI->getOperand(i));
696 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000697
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000698 // Transfer MI flags.
699 MIB.setMIFlags(MI->getFlags());
700
Chris Lattnera6f074f2009-08-23 03:41:05 +0000701 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Cheng1be453b2009-08-08 03:21:23 +0000702
Evan Cheng7fae11b2011-12-14 02:11:42 +0000703 MBB.erase_instr(MI);
Evan Cheng1be453b2009-08-08 03:21:23 +0000704 ++Num2Addrs;
Evan Cheng1be453b2009-08-08 03:21:23 +0000705 return true;
706}
707
708bool
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000709Thumb2SizeReduce::ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
710 const ReduceEntry &Entry,
Evan Chengf4807a12011-10-27 21:21:05 +0000711 bool LiveCPSR, MachineInstr *CPSRDef,
712 bool IsSelfLoop) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000713 if (ReduceLimit != -1 && ((int)NumNarrows >= ReduceLimit))
714 return false;
715
Evan Chengd461c1c2009-08-09 19:17:19 +0000716 unsigned Limit = ~0U;
717 if (Entry.Imm1Limit)
Jim Grosbacha8a80672011-06-29 23:25:04 +0000718 Limit = (1 << Entry.Imm1Limit) - 1;
Evan Chengd461c1c2009-08-09 19:17:19 +0000719
Evan Cheng6cc775f2011-06-28 19:10:37 +0000720 const MCInstrDesc &MCID = MI->getDesc();
721 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i) {
722 if (MCID.OpInfo[i].isPredicate())
Evan Chengd461c1c2009-08-09 19:17:19 +0000723 continue;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000724 const MachineOperand &MO = MI->getOperand(i);
Evan Chengd461c1c2009-08-09 19:17:19 +0000725 if (MO.isReg()) {
726 unsigned Reg = MO.getReg();
727 if (!Reg || Reg == ARM::CPSR)
728 continue;
729 if (Entry.LowRegs1 && !isARMLowRegister(Reg))
730 return false;
Evan Chengf6a9d062009-08-11 23:00:31 +0000731 } else if (MO.isImm() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +0000732 !MCID.OpInfo[i].isPredicate()) {
Jim Grosbacha8a80672011-06-29 23:25:04 +0000733 if (((unsigned)MO.getImm()) > Limit)
Evan Chengd461c1c2009-08-09 19:17:19 +0000734 return false;
735 }
736 }
737
Evan Cheng1f5bee12009-08-10 06:57:42 +0000738 // Check if it's possible / necessary to transfer the predicate.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000739 const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc1);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000740 unsigned PredReg = 0;
741 ARMCC::CondCodes Pred = getInstrPredicate(MI, PredReg);
742 bool SkipPred = false;
743 if (Pred != ARMCC::AL) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000744 if (!NewMCID.isPredicable())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000745 // Can't transfer predicate, fail.
746 return false;
747 } else {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000748 SkipPred = !NewMCID.isPredicable();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000749 }
750
Evan Chengd461c1c2009-08-09 19:17:19 +0000751 bool HasCC = false;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000752 bool CCDead = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000753 if (MCID.hasOptionalDef()) {
754 unsigned NumOps = MCID.getNumOperands();
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000755 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
756 if (HasCC && MI->getOperand(NumOps-1).isDead())
757 CCDead = true;
758 }
Evan Cheng1f5bee12009-08-10 06:57:42 +0000759 if (!VerifyPredAndCC(MI, Entry, false, Pred, LiveCPSR, HasCC, CCDead))
Evan Chengd461c1c2009-08-09 19:17:19 +0000760 return false;
761
Bob Wilsona2881ee2011-04-19 18:11:49 +0000762 // Avoid adding a false dependency on partial flag update by some 16-bit
763 // instructions which has the 's' bit set.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000764 if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
Evan Chengf4807a12011-10-27 21:21:05 +0000765 canAddPseudoFlagDep(CPSRDef, MI, IsSelfLoop))
Bob Wilsona2881ee2011-04-19 18:11:49 +0000766 return false;
767
Evan Chengd461c1c2009-08-09 19:17:19 +0000768 // Add the 16-bit instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000769 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000770 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000771 MIB.addOperand(MI->getOperand(0));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000772 if (NewMCID.hasOptionalDef()) {
Evan Cheng6ddd7bc2009-08-15 07:59:10 +0000773 if (HasCC)
774 AddDefaultT1CC(MIB, CCDead);
775 else
776 AddNoT1CC(MIB);
777 }
Evan Chengd461c1c2009-08-09 19:17:19 +0000778
779 // Transfer the rest of operands.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000780 unsigned NumOps = MCID.getNumOperands();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000781 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000782 if (i < NumOps && MCID.OpInfo[i].isOptionalDef())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000783 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000784 if ((MCID.getOpcode() == ARM::t2RSBSri ||
Jim Grosbach8b31ef52011-07-27 16:47:19 +0000785 MCID.getOpcode() == ARM::t2RSBri ||
786 MCID.getOpcode() == ARM::t2SXTB ||
787 MCID.getOpcode() == ARM::t2SXTH ||
788 MCID.getOpcode() == ARM::t2UXTB ||
789 MCID.getOpcode() == ARM::t2UXTH) && i == 2)
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000790 // Skip the zero immediate operand, it's now implicit.
791 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000792 bool isPred = (i < NumOps && MCID.OpInfo[i].isPredicate());
Evan Chengf6a9d062009-08-11 23:00:31 +0000793 if (SkipPred && isPred)
794 continue;
795 const MachineOperand &MO = MI->getOperand(i);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000796 if (MO.isReg() && MO.isImplicit() && MO.getReg() == ARM::CPSR)
797 // Skip implicit def of CPSR. Either it's modeled as an optional
798 // def now or it's already an implicit def on the new instruction.
799 continue;
800 MIB.addOperand(MO);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000801 }
Evan Cheng6cc775f2011-06-28 19:10:37 +0000802 if (!MCID.isPredicable() && NewMCID.isPredicable())
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000803 AddDefaultPred(MIB);
Evan Chengd461c1c2009-08-09 19:17:19 +0000804
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000805 // Transfer MI flags.
806 MIB.setMIFlags(MI->getFlags());
807
Chris Lattnera6f074f2009-08-23 03:41:05 +0000808 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Chengd461c1c2009-08-09 19:17:19 +0000809
Evan Cheng7fae11b2011-12-14 02:11:42 +0000810 MBB.erase_instr(MI);
Evan Chengd461c1c2009-08-09 19:17:19 +0000811 ++NumNarrows;
812 return true;
Evan Cheng1be453b2009-08-08 03:21:23 +0000813}
814
Bob Wilsona2881ee2011-04-19 18:11:49 +0000815static bool UpdateCPSRDef(MachineInstr &MI, bool LiveCPSR, bool &DefCPSR) {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000816 bool HasDef = false;
817 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
818 const MachineOperand &MO = MI.getOperand(i);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000819 if (!MO.isReg() || MO.isUndef() || MO.isUse())
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000820 continue;
821 if (MO.getReg() != ARM::CPSR)
822 continue;
Bob Wilsona2881ee2011-04-19 18:11:49 +0000823
824 DefCPSR = true;
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000825 if (!MO.isDead())
826 HasDef = true;
827 }
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000828
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000829 return HasDef || LiveCPSR;
830}
831
832static bool UpdateCPSRUse(MachineInstr &MI, bool LiveCPSR) {
833 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
834 const MachineOperand &MO = MI.getOperand(i);
835 if (!MO.isReg() || MO.isUndef() || MO.isDef())
836 continue;
837 if (MO.getReg() != ARM::CPSR)
838 continue;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000839 assert(LiveCPSR && "CPSR liveness tracking is wrong!");
840 if (MO.isKill()) {
841 LiveCPSR = false;
842 break;
843 }
844 }
845
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000846 return LiveCPSR;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000847}
848
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000849bool Thumb2SizeReduce::ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI,
850 bool LiveCPSR, MachineInstr *CPSRDef,
851 bool IsSelfLoop) {
852 unsigned Opcode = MI->getOpcode();
853 DenseMap<unsigned, unsigned>::iterator OPI = ReduceOpcodeMap.find(Opcode);
854 if (OPI == ReduceOpcodeMap.end())
855 return false;
856 const ReduceEntry &Entry = ReduceTable[OPI->second];
857
858 // Don't attempt normal reductions on "special" cases for now.
859 if (Entry.Special)
860 return ReduceSpecial(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop);
861
862 // Try to transform to a 16-bit two-address instruction.
863 if (Entry.NarrowOpc2 &&
864 ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop))
865 return true;
866
867 // Try to transform to a 16-bit non-two-address instruction.
868 if (Entry.NarrowOpc1 &&
869 ReduceToNarrow(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop))
870 return true;
871
872 return false;
873}
874
Evan Cheng1be453b2009-08-08 03:21:23 +0000875bool Thumb2SizeReduce::ReduceMBB(MachineBasicBlock &MBB) {
876 bool Modified = false;
877
Evan Cheng1f5bee12009-08-10 06:57:42 +0000878 // Yes, CPSR could be livein.
Dan Gohmana1cf9fe2010-04-13 16:53:51 +0000879 bool LiveCPSR = MBB.isLiveIn(ARM::CPSR);
Bob Wilsona2881ee2011-04-19 18:11:49 +0000880 MachineInstr *CPSRDef = 0;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000881 MachineInstr *BundleMI = 0;
Evan Cheng1f5bee12009-08-10 06:57:42 +0000882
Evan Chengf4807a12011-10-27 21:21:05 +0000883 // If this BB loops back to itself, conservatively avoid narrowing the
884 // first instruction that does partial flag update.
885 bool IsSelfLoop = MBB.isSuccessor(&MBB);
Jim Grosbach0c509fa2012-04-06 23:43:50 +0000886 MachineBasicBlock::instr_iterator MII = MBB.instr_begin(),E = MBB.instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000887 MachineBasicBlock::instr_iterator NextMII;
Evan Cheng1be453b2009-08-08 03:21:23 +0000888 for (; MII != E; MII = NextMII) {
Chris Lattnera48f44d2009-12-03 00:50:42 +0000889 NextMII = llvm::next(MII);
Evan Cheng1be453b2009-08-08 03:21:23 +0000890
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000891 MachineInstr *MI = &*MII;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000892 if (MI->isBundle()) {
893 BundleMI = MI;
894 continue;
895 }
896
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000897 LiveCPSR = UpdateCPSRUse(*MI, LiveCPSR);
898
Jakob Stoklund Olesen41bbf9c2012-12-18 00:46:39 +0000899 // Does NextMII belong to the same bundle as MI?
900 bool NextInSameBundle = NextMII != E && NextMII->isBundledWithPred();
901
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000902 if (ReduceMI(MBB, MI, LiveCPSR, CPSRDef, IsSelfLoop)) {
903 Modified = true;
904 MachineBasicBlock::instr_iterator I = prior(NextMII);
905 MI = &*I;
Jakob Stoklund Olesen41bbf9c2012-12-18 00:46:39 +0000906 // Removing and reinserting the first instruction in a bundle will break
907 // up the bundle. Fix the bundling if it was broken.
908 if (NextInSameBundle && !NextMII->isBundledWithPred())
909 NextMII->bundleWithPred();
Evan Cheng1be453b2009-08-08 03:21:23 +0000910 }
911
Jakob Stoklund Olesen41bbf9c2012-12-18 00:46:39 +0000912 if (!NextInSameBundle && MI->isInsideBundle()) {
Evan Cheng7fae11b2011-12-14 02:11:42 +0000913 // FIXME: Since post-ra scheduler operates on bundles, the CPSR kill
914 // marker is only on the BUNDLE instruction. Process the BUNDLE
915 // instruction as we finish with the bundled instruction to work around
916 // the inconsistency.
Evan Cheng903231b2011-12-17 01:25:34 +0000917 if (BundleMI->killsRegister(ARM::CPSR))
918 LiveCPSR = false;
919 MachineOperand *MO = BundleMI->findRegisterDefOperand(ARM::CPSR);
920 if (MO && !MO->isDead())
921 LiveCPSR = true;
922 }
Evan Cheng7fae11b2011-12-14 02:11:42 +0000923
Bob Wilsona2881ee2011-04-19 18:11:49 +0000924 bool DefCPSR = false;
925 LiveCPSR = UpdateCPSRDef(*MI, LiveCPSR, DefCPSR);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000926 if (MI->isCall()) {
Bob Wilsona2881ee2011-04-19 18:11:49 +0000927 // Calls don't really set CPSR.
928 CPSRDef = 0;
Evan Chengf4807a12011-10-27 21:21:05 +0000929 IsSelfLoop = false;
930 } else if (DefCPSR) {
Bob Wilsona2881ee2011-04-19 18:11:49 +0000931 // This is the last CPSR defining instruction.
932 CPSRDef = MI;
Evan Chengf4807a12011-10-27 21:21:05 +0000933 IsSelfLoop = false;
934 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000935 }
936
937 return Modified;
938}
939
940bool Thumb2SizeReduce::runOnMachineFunction(MachineFunction &MF) {
941 const TargetMachine &TM = MF.getTarget();
Evan Cheng6ddd7bc2009-08-15 07:59:10 +0000942 TII = static_cast<const Thumb2InstrInfo*>(TM.getInstrInfo());
Bob Wilsona2881ee2011-04-19 18:11:49 +0000943 STI = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng1be453b2009-08-08 03:21:23 +0000944
945 bool Modified = false;
946 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
947 Modified |= ReduceMBB(*I);
948 return Modified;
949}
950
951/// createThumb2SizeReductionPass - Returns an instance of the Thumb2 size
952/// reduction pass.
953FunctionPass *llvm::createThumb2SizeReductionPass() {
954 return new Thumb2SizeReduce();
955}