blob: a4f0847450927e7df10ddc087d3ff5e57a6cbd46 [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"
Quentin Colombet23b404d2012-12-18 22:47:16 +000025#include "llvm/Function.h" // To access Function attributes
Evan Cheng1be453b2009-08-08 03:21:23 +000026using namespace llvm;
27
Evan Cheng1f5bee12009-08-10 06:57:42 +000028STATISTIC(NumNarrows, "Number of 32-bit instrs reduced to 16-bit ones");
29STATISTIC(Num2Addrs, "Number of 32-bit instrs reduced to 2addr 16-bit ones");
Evan Cheng36064672009-08-11 08:52:18 +000030STATISTIC(NumLdSts, "Number of 32-bit load / store reduced to 16-bit ones");
Evan Cheng1be453b2009-08-08 03:21:23 +000031
Evan Chengcc9ca352009-08-11 21:11:32 +000032static cl::opt<int> ReduceLimit("t2-reduce-limit",
33 cl::init(-1), cl::Hidden);
34static cl::opt<int> ReduceLimit2Addr("t2-reduce-limit2",
35 cl::init(-1), cl::Hidden);
36static cl::opt<int> ReduceLimitLdSt("t2-reduce-limit3",
37 cl::init(-1), cl::Hidden);
Evan Chengf16a1d52009-08-10 07:20:37 +000038
Evan Cheng1be453b2009-08-08 03:21:23 +000039namespace {
40 /// ReduceTable - A static table with information on mapping from wide
41 /// opcodes to narrow
42 struct ReduceEntry {
Craig Topperca658c22012-03-11 07:16:55 +000043 uint16_t WideOpc; // Wide opcode
44 uint16_t NarrowOpc1; // Narrow opcode to transform to
45 uint16_t NarrowOpc2; // Narrow opcode when it's two-address
Evan Cheng1be453b2009-08-08 03:21:23 +000046 uint8_t Imm1Limit; // Limit of immediate field (bits)
47 uint8_t Imm2Limit; // Limit of immediate field when it's two-address
48 unsigned LowRegs1 : 1; // Only possible if low-registers are used
49 unsigned LowRegs2 : 1; // Only possible if low-registers are used (2addr)
Evan Cheng1e6c2a12009-08-12 01:49:45 +000050 unsigned PredCC1 : 2; // 0 - If predicated, cc is on and vice versa.
Evan Cheng1be453b2009-08-08 03:21:23 +000051 // 1 - No cc field.
Evan Cheng1e6c2a12009-08-12 01:49:45 +000052 // 2 - Always set CPSR.
Evan Chengaee7e492009-08-12 18:35:50 +000053 unsigned PredCC2 : 2;
Bob Wilsona2881ee2011-04-19 18:11:49 +000054 unsigned PartFlag : 1; // 16-bit instruction does partial flag update
Evan Cheng1be453b2009-08-08 03:21:23 +000055 unsigned Special : 1; // Needs to be dealt with specially
Evan Chengddc0cb62012-12-20 19:59:30 +000056 unsigned AvoidMovs: 1; // Avoid movs with shifter operand (for Swift)
Evan Cheng1be453b2009-08-08 03:21:23 +000057 };
58
59 static const ReduceEntry ReduceTable[] = {
Evan Chengddc0cb62012-12-20 19:59:30 +000060 // Wide, Narrow1, Narrow2, imm1,imm2, lo1, lo2, P/C,PF,S,AM
61 { ARM::t2ADCrr, 0, ARM::tADC, 0, 0, 0, 1, 0,0, 0,0,0 },
62 { ARM::t2ADDri, ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 0,0, 0,1,0 },
63 { ARM::t2ADDrr, ARM::tADDrr, ARM::tADDhirr, 0, 0, 1, 0, 0,1, 0,0,0 },
64 { ARM::t2ADDSri,ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 2,2, 0,1,0 },
65 { ARM::t2ADDSrr,ARM::tADDrr, 0, 0, 0, 1, 0, 2,0, 0,1,0 },
66 { ARM::t2ANDrr, 0, ARM::tAND, 0, 0, 0, 1, 0,0, 1,0,0 },
67 { ARM::t2ASRri, ARM::tASRri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
68 { ARM::t2ASRrr, 0, ARM::tASRrr, 0, 0, 0, 1, 0,0, 1,0,1 },
69 { ARM::t2BICrr, 0, ARM::tBIC, 0, 0, 0, 1, 0,0, 1,0,0 },
70 //FIXME: Disable CMN, as CCodes are backwards from compare expectations
71 //{ ARM::t2CMNrr, ARM::tCMN, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
72 { ARM::t2CMNzrr, ARM::tCMNz, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
73 { ARM::t2CMPri, ARM::tCMPi8, 0, 8, 0, 1, 0, 2,0, 0,0,0 },
74 { ARM::t2CMPrr, ARM::tCMPhir, 0, 0, 0, 0, 0, 2,0, 0,1,0 },
75 { ARM::t2EORrr, 0, ARM::tEOR, 0, 0, 0, 1, 0,0, 1,0,0 },
76 // FIXME: adr.n immediate offset must be multiple of 4.
77 //{ ARM::t2LEApcrelJT,ARM::tLEApcrelJT, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
78 { ARM::t2LSLri, ARM::tLSLri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
79 { ARM::t2LSLrr, 0, ARM::tLSLrr, 0, 0, 0, 1, 0,0, 1,0,1 },
80 { ARM::t2LSRri, ARM::tLSRri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
81 { ARM::t2LSRrr, 0, ARM::tLSRrr, 0, 0, 0, 1, 0,0, 1,0,1 },
82 // FIXME: tMOVi8 and tMVN also partially update CPSR but they are less
83 // likely to cause issue in the loop. As a size / performance workaround,
84 // they are not marked as such.
85 { ARM::t2MOVi, ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 0,0,0 },
86 { ARM::t2MOVi16,ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 0,1,0 },
87 // FIXME: Do we need the 16-bit 'S' variant?
88 { ARM::t2MOVr,ARM::tMOVr, 0, 0, 0, 0, 0, 1,0, 0,0,0 },
89 { ARM::t2MUL, 0, ARM::tMUL, 0, 0, 0, 1, 0,0, 1,0,0 },
90 { ARM::t2MVNr, ARM::tMVN, 0, 0, 0, 1, 0, 0,0, 0,0,0 },
91 { ARM::t2ORRrr, 0, ARM::tORR, 0, 0, 0, 1, 0,0, 1,0,0 },
92 { ARM::t2REV, ARM::tREV, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
93 { ARM::t2REV16, ARM::tREV16, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
94 { ARM::t2REVSH, ARM::tREVSH, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
95 { ARM::t2RORrr, 0, ARM::tROR, 0, 0, 0, 1, 0,0, 1,0,0 },
96 { ARM::t2RSBri, ARM::tRSB, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
97 { ARM::t2RSBSri,ARM::tRSB, 0, 0, 0, 1, 0, 2,0, 0,1,0 },
98 { ARM::t2SBCrr, 0, ARM::tSBC, 0, 0, 0, 1, 0,0, 0,0,0 },
99 { ARM::t2SUBri, ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 0,0, 0,0,0 },
100 { ARM::t2SUBrr, ARM::tSUBrr, 0, 0, 0, 1, 0, 0,0, 0,0,0 },
101 { ARM::t2SUBSri,ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 2,2, 0,0,0 },
102 { ARM::t2SUBSrr,ARM::tSUBrr, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
103 { ARM::t2SXTB, ARM::tSXTB, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
104 { ARM::t2SXTH, ARM::tSXTH, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
105 { ARM::t2TSTrr, ARM::tTST, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
106 { ARM::t2UXTB, ARM::tUXTB, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
107 { ARM::t2UXTH, ARM::tUXTH, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
Evan Cheng36064672009-08-11 08:52:18 +0000108
Evan Chengddc0cb62012-12-20 19:59:30 +0000109 // FIXME: Clean this up after splitting each Thumb load / store opcode
110 // into multiple ones.
111 { ARM::t2LDRi12,ARM::tLDRi, ARM::tLDRspi, 5, 8, 1, 0, 0,0, 0,1,0 },
112 { ARM::t2LDRs, ARM::tLDRr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
113 { ARM::t2LDRBi12,ARM::tLDRBi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
114 { ARM::t2LDRBs, ARM::tLDRBr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
115 { ARM::t2LDRHi12,ARM::tLDRHi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
116 { ARM::t2LDRHs, ARM::tLDRHr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
117 { ARM::t2LDRSBs,ARM::tLDRSB, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
118 { ARM::t2LDRSHs,ARM::tLDRSH, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
119 { ARM::t2STRi12,ARM::tSTRi, ARM::tSTRspi, 5, 8, 1, 0, 0,0, 0,1,0 },
120 { ARM::t2STRs, ARM::tSTRr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
121 { ARM::t2STRBi12,ARM::tSTRBi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
122 { ARM::t2STRBs, ARM::tSTRBr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
123 { ARM::t2STRHi12,ARM::tSTRHi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
124 { ARM::t2STRHs, ARM::tSTRHr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
Evan Chengcc9ca352009-08-11 21:11:32 +0000125
Evan Chengddc0cb62012-12-20 19:59:30 +0000126 { ARM::t2LDMIA, ARM::tLDMIA, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
127 { ARM::t2LDMIA_RET,0, ARM::tPOP_RET, 0, 0, 1, 1, 1,1, 0,1,0 },
128 { ARM::t2LDMIA_UPD,ARM::tLDMIA_UPD,ARM::tPOP,0, 0, 1, 1, 1,1, 0,1,0 },
129 // ARM::t2STM (with no basereg writeback) has no Thumb1 equivalent
130 { ARM::t2STMIA_UPD,ARM::tSTMIA_UPD, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
131 { ARM::t2STMDB_UPD, 0, ARM::tPUSH, 0, 0, 1, 1, 1,1, 0,1,0 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000132 };
133
Nick Lewycky02d5f772009-10-25 06:33:48 +0000134 class Thumb2SizeReduce : public MachineFunctionPass {
Evan Cheng1be453b2009-08-08 03:21:23 +0000135 public:
136 static char ID;
137 Thumb2SizeReduce();
138
Evan Cheng6ddd7bc2009-08-15 07:59:10 +0000139 const Thumb2InstrInfo *TII;
Bob Wilsona2881ee2011-04-19 18:11:49 +0000140 const ARMSubtarget *STI;
Evan Cheng1be453b2009-08-08 03:21:23 +0000141
142 virtual bool runOnMachineFunction(MachineFunction &MF);
143
144 virtual const char *getPassName() const {
145 return "Thumb2 instruction size reduction pass";
146 }
147
148 private:
149 /// ReduceOpcodeMap - Maps wide opcode to index of entry in ReduceTable.
150 DenseMap<unsigned, unsigned> ReduceOpcodeMap;
151
Evan Chengf4807a12011-10-27 21:21:05 +0000152 bool canAddPseudoFlagDep(MachineInstr *Def, MachineInstr *Use,
153 bool IsSelfLoop);
Bob Wilsona2881ee2011-04-19 18:11:49 +0000154
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000155 bool VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
156 bool is2Addr, ARMCC::CondCodes Pred,
157 bool LiveCPSR, bool &HasCC, bool &CCDead);
158
Evan Cheng36064672009-08-11 08:52:18 +0000159 bool ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
160 const ReduceEntry &Entry);
161
162 bool ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
Bob Wilsona2881ee2011-04-19 18:11:49 +0000163 const ReduceEntry &Entry, bool LiveCPSR,
Evan Chengf4807a12011-10-27 21:21:05 +0000164 MachineInstr *CPSRDef, bool IsSelfLoop);
Evan Cheng36064672009-08-11 08:52:18 +0000165
Evan Cheng1be453b2009-08-08 03:21:23 +0000166 /// ReduceTo2Addr - Reduce a 32-bit instruction to a 16-bit two-address
167 /// instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000168 bool ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
169 const ReduceEntry &Entry,
Evan Chengf4807a12011-10-27 21:21:05 +0000170 bool LiveCPSR, MachineInstr *CPSRDef,
171 bool IsSelfLoop);
Evan Cheng1be453b2009-08-08 03:21:23 +0000172
173 /// ReduceToNarrow - Reduce a 32-bit instruction to a 16-bit
174 /// non-two-address instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000175 bool ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
176 const ReduceEntry &Entry,
Evan Chengf4807a12011-10-27 21:21:05 +0000177 bool LiveCPSR, MachineInstr *CPSRDef,
178 bool IsSelfLoop);
Evan Cheng1be453b2009-08-08 03:21:23 +0000179
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000180 /// ReduceMI - Attempt to reduce MI, return true on success.
181 bool ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI,
182 bool LiveCPSR, MachineInstr *CPSRDef,
183 bool IsSelfLoop);
184
Evan Cheng1be453b2009-08-08 03:21:23 +0000185 /// ReduceMBB - Reduce width of instructions in the specified basic block.
186 bool ReduceMBB(MachineBasicBlock &MBB);
Quentin Colombet23b404d2012-12-18 22:47:16 +0000187
Evan Chengddc0cb62012-12-20 19:59:30 +0000188 bool OptimizeSize;
Quentin Colombet23b404d2012-12-18 22:47:16 +0000189 bool MinimizeSize;
Evan Cheng1be453b2009-08-08 03:21:23 +0000190 };
191 char Thumb2SizeReduce::ID = 0;
192}
193
Owen Andersona7aed182010-08-06 18:33:48 +0000194Thumb2SizeReduce::Thumb2SizeReduce() : MachineFunctionPass(ID) {
Evan Chengddc0cb62012-12-20 19:59:30 +0000195 OptimizeSize = MinimizeSize = false;
Evan Cheng1be453b2009-08-08 03:21:23 +0000196 for (unsigned i = 0, e = array_lengthof(ReduceTable); i != e; ++i) {
197 unsigned FromOpc = ReduceTable[i].WideOpc;
198 if (!ReduceOpcodeMap.insert(std::make_pair(FromOpc, i)).second)
199 assert(false && "Duplicated entries?");
200 }
201}
202
Evan Cheng6cc775f2011-06-28 19:10:37 +0000203static bool HasImplicitCPSRDef(const MCInstrDesc &MCID) {
Craig Topper5a4bcc72012-03-08 08:22:45 +0000204 for (const uint16_t *Regs = MCID.getImplicitDefs(); *Regs; ++Regs)
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000205 if (*Regs == ARM::CPSR)
206 return true;
207 return false;
208}
209
Bob Wilsona2881ee2011-04-19 18:11:49 +0000210/// canAddPseudoFlagDep - For A9 (and other out-of-order) implementations,
211/// the 's' 16-bit instruction partially update CPSR. Abort the
212/// transformation to avoid adding false dependency on last CPSR setting
213/// instruction which hurts the ability for out-of-order execution engine
214/// to do register renaming magic.
215/// This function checks if there is a read-of-write dependency between the
216/// last instruction that defines the CPSR and the current instruction. If there
217/// is, then there is no harm done since the instruction cannot be retired
218/// before the CPSR setting instruction anyway.
219/// Note, we are not doing full dependency analysis here for the sake of compile
220/// time. We're not looking for cases like:
221/// r0 = muls ...
222/// r1 = add.w r0, ...
223/// ...
224/// = mul.w r1
225/// In this case it would have been ok to narrow the mul.w to muls since there
226/// are indirect RAW dependency between the muls and the mul.w
227bool
Evan Chengf4807a12011-10-27 21:21:05 +0000228Thumb2SizeReduce::canAddPseudoFlagDep(MachineInstr *Def, MachineInstr *Use,
229 bool FirstInSelfLoop) {
Quentin Colombet23b404d2012-12-18 22:47:16 +0000230 // Disable the check for -Oz (aka OptimizeForSizeHarder).
231 if (MinimizeSize || !STI->avoidCPSRPartialUpdate())
Bob Wilsona2881ee2011-04-19 18:11:49 +0000232 return false;
233
Evan Chengf4807a12011-10-27 21:21:05 +0000234 if (!Def)
235 // If this BB loops back to itself, conservatively avoid narrowing the
236 // first instruction that does partial flag update.
237 return FirstInSelfLoop;
238
Bob Wilsona2881ee2011-04-19 18:11:49 +0000239 SmallSet<unsigned, 2> Defs;
240 for (unsigned i = 0, e = Def->getNumOperands(); i != e; ++i) {
241 const MachineOperand &MO = Def->getOperand(i);
242 if (!MO.isReg() || MO.isUndef() || MO.isUse())
243 continue;
244 unsigned Reg = MO.getReg();
245 if (Reg == 0 || Reg == ARM::CPSR)
246 continue;
247 Defs.insert(Reg);
248 }
249
250 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
251 const MachineOperand &MO = Use->getOperand(i);
252 if (!MO.isReg() || MO.isUndef() || MO.isDef())
253 continue;
254 unsigned Reg = MO.getReg();
255 if (Defs.count(Reg))
256 return false;
257 }
258
259 // No read-after-write dependency. The narrowing will add false dependency.
260 return true;
261}
262
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000263bool
264Thumb2SizeReduce::VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
265 bool is2Addr, ARMCC::CondCodes Pred,
266 bool LiveCPSR, bool &HasCC, bool &CCDead) {
Evan Chengd461c1c2009-08-09 19:17:19 +0000267 if ((is2Addr && Entry.PredCC2 == 0) ||
268 (!is2Addr && Entry.PredCC1 == 0)) {
269 if (Pred == ARMCC::AL) {
270 // Not predicated, must set CPSR.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000271 if (!HasCC) {
272 // Original instruction was not setting CPSR, but CPSR is not
273 // currently live anyway. It's ok to set it. The CPSR def is
274 // dead though.
275 if (!LiveCPSR) {
276 HasCC = true;
277 CCDead = true;
278 return true;
279 }
280 return false;
281 }
Evan Chengd461c1c2009-08-09 19:17:19 +0000282 } else {
283 // Predicated, must not set CPSR.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000284 if (HasCC)
285 return false;
Evan Chengd461c1c2009-08-09 19:17:19 +0000286 }
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000287 } else if ((is2Addr && Entry.PredCC2 == 2) ||
288 (!is2Addr && Entry.PredCC1 == 2)) {
289 /// Old opcode has an optional def of CPSR.
290 if (HasCC)
291 return true;
Jim Grosbachbc7eeaf2010-09-14 20:35:46 +0000292 // If old opcode does not implicitly define CPSR, then it's not ok since
293 // these new opcodes' CPSR def is not meant to be thrown away. e.g. CMP.
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000294 if (!HasImplicitCPSRDef(MI->getDesc()))
295 return false;
296 HasCC = true;
Evan Chengd461c1c2009-08-09 19:17:19 +0000297 } else {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000298 // 16-bit instruction does not set CPSR.
299 if (HasCC)
300 return false;
Evan Chengd461c1c2009-08-09 19:17:19 +0000301 }
302
303 return true;
304}
305
Evan Chengcc9ca352009-08-11 21:11:32 +0000306static bool VerifyLowRegs(MachineInstr *MI) {
307 unsigned Opc = MI->getOpcode();
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000308 bool isPCOk = (Opc == ARM::t2LDMIA_RET || Opc == ARM::t2LDMIA ||
309 Opc == ARM::t2LDMDB || Opc == ARM::t2LDMIA_UPD ||
Owen Anderson4ebf4712011-02-08 22:39:40 +0000310 Opc == ARM::t2LDMDB_UPD);
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000311 bool isLROk = (Opc == ARM::t2STMIA_UPD || Opc == ARM::t2STMDB_UPD);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000312 bool isSPOk = isPCOk || isLROk;
Evan Chengcc9ca352009-08-11 21:11:32 +0000313 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
314 const MachineOperand &MO = MI->getOperand(i);
315 if (!MO.isReg() || MO.isImplicit())
316 continue;
317 unsigned Reg = MO.getReg();
318 if (Reg == 0 || Reg == ARM::CPSR)
319 continue;
320 if (isPCOk && Reg == ARM::PC)
321 continue;
322 if (isLROk && Reg == ARM::LR)
323 continue;
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000324 if (Reg == ARM::SP) {
325 if (isSPOk)
326 continue;
327 if (i == 1 && (Opc == ARM::t2LDRi12 || Opc == ARM::t2STRi12))
328 // Special case for these ldr / str with sp as base register.
329 continue;
330 }
Evan Chengcc9ca352009-08-11 21:11:32 +0000331 if (!isARMLowRegister(Reg))
332 return false;
333 }
334 return true;
335}
336
Evan Cheng1be453b2009-08-08 03:21:23 +0000337bool
Evan Cheng36064672009-08-11 08:52:18 +0000338Thumb2SizeReduce::ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
339 const ReduceEntry &Entry) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000340 if (ReduceLimitLdSt != -1 && ((int)NumLdSts >= ReduceLimitLdSt))
341 return false;
342
Evan Cheng36064672009-08-11 08:52:18 +0000343 unsigned Scale = 1;
344 bool HasImmOffset = false;
345 bool HasShift = false;
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000346 bool HasOffReg = true;
Evan Chengcc9ca352009-08-11 21:11:32 +0000347 bool isLdStMul = false;
Evan Chengcc9ca352009-08-11 21:11:32 +0000348 unsigned Opc = Entry.NarrowOpc1;
349 unsigned OpNum = 3; // First 'rest' of operands.
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000350 uint8_t ImmLimit = Entry.Imm1Limit;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000351
Evan Cheng36064672009-08-11 08:52:18 +0000352 switch (Entry.WideOpc) {
353 default:
354 llvm_unreachable("Unexpected Thumb2 load / store opcode!");
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000355 case ARM::t2LDRi12:
Bill Wendling092a7bd2010-12-14 03:36:38 +0000356 case ARM::t2STRi12:
357 if (MI->getOperand(1).getReg() == ARM::SP) {
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000358 Opc = Entry.NarrowOpc2;
359 ImmLimit = Entry.Imm2Limit;
360 HasOffReg = false;
361 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000362
Evan Cheng36064672009-08-11 08:52:18 +0000363 Scale = 4;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000364 HasImmOffset = true;
365 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000366 break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000367 case ARM::t2LDRBi12:
Evan Cheng36064672009-08-11 08:52:18 +0000368 case ARM::t2STRBi12:
Owen Anderson4ebf4712011-02-08 22:39:40 +0000369 HasImmOffset = true;
370 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000371 break;
372 case ARM::t2LDRHi12:
373 case ARM::t2STRHi12:
374 Scale = 2;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000375 HasImmOffset = true;
376 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000377 break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000378 case ARM::t2LDRs:
379 case ARM::t2LDRBs:
380 case ARM::t2LDRHs:
Evan Cheng36064672009-08-11 08:52:18 +0000381 case ARM::t2LDRSBs:
382 case ARM::t2LDRSHs:
383 case ARM::t2STRs:
384 case ARM::t2STRBs:
385 case ARM::t2STRHs:
386 HasShift = true;
Evan Chengcc9ca352009-08-11 21:11:32 +0000387 OpNum = 4;
Evan Cheng36064672009-08-11 08:52:18 +0000388 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000389 case ARM::t2LDMIA:
390 case ARM::t2LDMDB: {
Evan Chengcc9ca352009-08-11 21:11:32 +0000391 unsigned BaseReg = MI->getOperand(0).getReg();
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000392 if (!isARMLowRegister(BaseReg) || Entry.WideOpc != ARM::t2LDMIA)
Bob Wilson947f04b2010-03-13 01:08:20 +0000393 return false;
Bill Wendling092a7bd2010-12-14 03:36:38 +0000394
Jim Grosbach88628e92010-09-07 22:30:53 +0000395 // For the non-writeback version (this one), the base register must be
396 // one of the registers being loaded.
397 bool isOK = false;
398 for (unsigned i = 4; i < MI->getNumOperands(); ++i) {
399 if (MI->getOperand(i).getReg() == BaseReg) {
400 isOK = true;
401 break;
402 }
403 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000404
Jim Grosbach88628e92010-09-07 22:30:53 +0000405 if (!isOK)
406 return false;
407
Bob Wilson947f04b2010-03-13 01:08:20 +0000408 OpNum = 0;
409 isLdStMul = true;
410 break;
411 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000412 case ARM::t2LDMIA_RET: {
Bob Wilson947f04b2010-03-13 01:08:20 +0000413 unsigned BaseReg = MI->getOperand(1).getReg();
414 if (BaseReg != ARM::SP)
415 return false;
416 Opc = Entry.NarrowOpc2; // tPOP_RET
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000417 OpNum = 2;
Bob Wilson947f04b2010-03-13 01:08:20 +0000418 isLdStMul = true;
419 break;
420 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000421 case ARM::t2LDMIA_UPD:
422 case ARM::t2LDMDB_UPD:
423 case ARM::t2STMIA_UPD:
424 case ARM::t2STMDB_UPD: {
Bob Wilson947f04b2010-03-13 01:08:20 +0000425 OpNum = 0;
Bill Wendling092a7bd2010-12-14 03:36:38 +0000426
Bob Wilson947f04b2010-03-13 01:08:20 +0000427 unsigned BaseReg = MI->getOperand(1).getReg();
Bob Wilson947f04b2010-03-13 01:08:20 +0000428 if (BaseReg == ARM::SP &&
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000429 (Entry.WideOpc == ARM::t2LDMIA_UPD ||
430 Entry.WideOpc == ARM::t2STMDB_UPD)) {
Bob Wilson947f04b2010-03-13 01:08:20 +0000431 Opc = Entry.NarrowOpc2; // tPOP or tPUSH
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000432 OpNum = 2;
433 } else if (!isARMLowRegister(BaseReg) ||
434 (Entry.WideOpc != ARM::t2LDMIA_UPD &&
435 Entry.WideOpc != ARM::t2STMIA_UPD)) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000436 return false;
437 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000438
Evan Chengcc9ca352009-08-11 21:11:32 +0000439 isLdStMul = true;
440 break;
441 }
Evan Cheng36064672009-08-11 08:52:18 +0000442 }
443
444 unsigned OffsetReg = 0;
445 bool OffsetKill = false;
446 if (HasShift) {
447 OffsetReg = MI->getOperand(2).getReg();
448 OffsetKill = MI->getOperand(2).isKill();
Bill Wendling092a7bd2010-12-14 03:36:38 +0000449
Evan Cheng36064672009-08-11 08:52:18 +0000450 if (MI->getOperand(3).getImm())
451 // Thumb1 addressing mode doesn't support shift.
452 return false;
453 }
454
455 unsigned OffsetImm = 0;
456 if (HasImmOffset) {
457 OffsetImm = MI->getOperand(2).getImm();
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000458 unsigned MaxOffset = ((1 << ImmLimit) - 1) * Scale;
Bill Wendling092a7bd2010-12-14 03:36:38 +0000459
460 if ((OffsetImm & (Scale - 1)) || OffsetImm > MaxOffset)
Evan Cheng36064672009-08-11 08:52:18 +0000461 // Make sure the immediate field fits.
462 return false;
463 }
464
465 // Add the 16-bit load / store instruction.
Evan Cheng36064672009-08-11 08:52:18 +0000466 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000467 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, TII->get(Opc));
Evan Chengcc9ca352009-08-11 21:11:32 +0000468 if (!isLdStMul) {
Owen Anderson99ea8a32010-12-07 00:45:21 +0000469 MIB.addOperand(MI->getOperand(0));
Owen Anderson4ebf4712011-02-08 22:39:40 +0000470 MIB.addOperand(MI->getOperand(1));
Bill Wendling092a7bd2010-12-14 03:36:38 +0000471
472 if (HasImmOffset)
473 MIB.addImm(OffsetImm / Scale);
474
Evan Chengcc9ca352009-08-11 21:11:32 +0000475 assert((!HasShift || OffsetReg) && "Invalid so_reg load / store address!");
476
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000477 if (HasOffReg)
478 MIB.addReg(OffsetReg, getKillRegState(OffsetKill));
Evan Cheng36064672009-08-11 08:52:18 +0000479 }
Evan Cheng806845d2009-08-11 09:37:40 +0000480
Evan Cheng36064672009-08-11 08:52:18 +0000481 // Transfer the rest of operands.
Evan Cheng36064672009-08-11 08:52:18 +0000482 for (unsigned e = MI->getNumOperands(); OpNum != e; ++OpNum)
483 MIB.addOperand(MI->getOperand(OpNum));
484
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000485 // Transfer memoperands.
Chris Lattner1d0c2572011-04-29 05:24:29 +0000486 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000487
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000488 // Transfer MI flags.
489 MIB.setMIFlags(MI->getFlags());
490
Chris Lattnera6f074f2009-08-23 03:41:05 +0000491 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Cheng36064672009-08-11 08:52:18 +0000492
Evan Cheng7fae11b2011-12-14 02:11:42 +0000493 MBB.erase_instr(MI);
Evan Cheng36064672009-08-11 08:52:18 +0000494 ++NumLdSts;
495 return true;
496}
497
Evan Cheng36064672009-08-11 08:52:18 +0000498bool
499Thumb2SizeReduce::ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
500 const ReduceEntry &Entry,
Evan Chengf4807a12011-10-27 21:21:05 +0000501 bool LiveCPSR, MachineInstr *CPSRDef,
502 bool IsSelfLoop) {
Jim Grosbacha8a80672011-06-29 23:25:04 +0000503 unsigned Opc = MI->getOpcode();
504 if (Opc == ARM::t2ADDri) {
505 // If the source register is SP, try to reduce to tADDrSPi, otherwise
506 // it's a normal reduce.
507 if (MI->getOperand(1).getReg() != ARM::SP) {
Evan Chengf4807a12011-10-27 21:21:05 +0000508 if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop))
Jim Grosbacha8a80672011-06-29 23:25:04 +0000509 return true;
Evan Chengf4807a12011-10-27 21:21:05 +0000510 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000511 }
512 // Try to reduce to tADDrSPi.
513 unsigned Imm = MI->getOperand(2).getImm();
514 // The immediate must be in range, the destination register must be a low
Jim Grosbached5134a2011-06-30 02:22:49 +0000515 // reg, the predicate must be "always" and the condition flags must not
516 // be being set.
Jim Grosbach68b0e842011-07-01 19:07:09 +0000517 if (Imm & 3 || Imm > 1020)
Jim Grosbacha8a80672011-06-29 23:25:04 +0000518 return false;
519 if (!isARMLowRegister(MI->getOperand(0).getReg()))
520 return false;
Jim Grosbached5134a2011-06-30 02:22:49 +0000521 if (MI->getOperand(3).getImm() != ARMCC::AL)
522 return false;
Jim Grosbacha8a80672011-06-29 23:25:04 +0000523 const MCInstrDesc &MCID = MI->getDesc();
524 if (MCID.hasOptionalDef() &&
525 MI->getOperand(MCID.getNumOperands()-1).getReg() == ARM::CPSR)
526 return false;
527
Evan Cheng7fae11b2011-12-14 02:11:42 +0000528 MachineInstrBuilder MIB = BuildMI(MBB, MI, MI->getDebugLoc(),
Jim Grosbacha8a80672011-06-29 23:25:04 +0000529 TII->get(ARM::tADDrSPi))
530 .addOperand(MI->getOperand(0))
531 .addOperand(MI->getOperand(1))
532 .addImm(Imm / 4); // The tADDrSPi has an implied scale by four.
Jim Grosbach1b8457a2011-08-24 17:46:13 +0000533 AddDefaultPred(MIB);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000534
535 // Transfer MI flags.
536 MIB.setMIFlags(MI->getFlags());
537
538 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " <<*MIB);
539
Evan Cheng7fae11b2011-12-14 02:11:42 +0000540 MBB.erase_instr(MI);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000541 ++NumNarrows;
542 return true;
543 }
544
Evan Chengcc9ca352009-08-11 21:11:32 +0000545 if (Entry.LowRegs1 && !VerifyLowRegs(MI))
Evan Cheng36064672009-08-11 08:52:18 +0000546 return false;
547
Evan Cheng7f8e5632011-12-07 07:15:52 +0000548 if (MI->mayLoad() || MI->mayStore())
Evan Cheng36064672009-08-11 08:52:18 +0000549 return ReduceLoadStore(MBB, MI, Entry);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000550
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000551 switch (Opc) {
552 default: break;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000553 case ARM::t2ADDSri:
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000554 case ARM::t2ADDSrr: {
555 unsigned PredReg = 0;
556 if (getInstrPredicate(MI, PredReg) == ARMCC::AL) {
557 switch (Opc) {
558 default: break;
559 case ARM::t2ADDSri: {
Evan Chengf4807a12011-10-27 21:21:05 +0000560 if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop))
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000561 return true;
562 // fallthrough
563 }
564 case ARM::t2ADDSrr:
Evan Chengf4807a12011-10-27 21:21:05 +0000565 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000566 }
567 }
568 break;
569 }
570 case ARM::t2RSBri:
571 case ARM::t2RSBSri:
Jim Grosbach8b31ef52011-07-27 16:47:19 +0000572 case ARM::t2SXTB:
573 case ARM::t2SXTH:
574 case ARM::t2UXTB:
575 case ARM::t2UXTH:
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000576 if (MI->getOperand(2).getImm() == 0)
Evan Chengf4807a12011-10-27 21:21:05 +0000577 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000578 break;
Anton Korobeynikov25229082009-11-24 00:44:37 +0000579 case ARM::t2MOVi16:
580 // Can convert only 'pure' immediate operands, not immediates obtained as
581 // globals' addresses.
582 if (MI->getOperand(1).isImm())
Evan Chengf4807a12011-10-27 21:21:05 +0000583 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop);
Anton Korobeynikov25229082009-11-24 00:44:37 +0000584 break;
Jim Grosbach327cf8e2010-12-07 20:41:06 +0000585 case ARM::t2CMPrr: {
Jim Grosbach5bae0542010-12-03 23:54:18 +0000586 // Try to reduce to the lo-reg only version first. Why there are two
587 // versions of the instruction is a mystery.
588 // It would be nice to just have two entries in the master table that
589 // are prioritized, but the table assumes a unique entry for each
590 // source insn opcode. So for now, we hack a local entry record to use.
591 static const ReduceEntry NarrowEntry =
Evan Chengddc0cb62012-12-20 19:59:30 +0000592 { ARM::t2CMPrr,ARM::tCMPr, 0, 0, 0, 1, 1,2, 0, 0,1,0 };
Evan Chengf4807a12011-10-27 21:21:05 +0000593 if (ReduceToNarrow(MBB, MI, NarrowEntry, LiveCPSR, CPSRDef, IsSelfLoop))
Jim Grosbach5bae0542010-12-03 23:54:18 +0000594 return true;
Evan Chengf4807a12011-10-27 21:21:05 +0000595 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop);
Jim Grosbach5bae0542010-12-03 23:54:18 +0000596 }
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000597 }
Evan Cheng36064672009-08-11 08:52:18 +0000598 return false;
599}
600
601bool
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000602Thumb2SizeReduce::ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
603 const ReduceEntry &Entry,
Evan Chengf4807a12011-10-27 21:21:05 +0000604 bool LiveCPSR, MachineInstr *CPSRDef,
605 bool IsSelfLoop) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000606
607 if (ReduceLimit2Addr != -1 && ((int)Num2Addrs >= ReduceLimit2Addr))
608 return false;
609
Evan Chengddc0cb62012-12-20 19:59:30 +0000610 if (!MinimizeSize && !OptimizeSize && Entry.AvoidMovs &&
611 STI->avoidMOVsShifterOperand())
612 // Don't issue movs with shifter operand for some CPUs unless we
613 // are optimizing / minimizing for size.
614 return false;
615
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000616 unsigned Reg0 = MI->getOperand(0).getReg();
617 unsigned Reg1 = MI->getOperand(1).getReg();
Jim Grosbachc01104d2012-02-24 00:33:36 +0000618 // t2MUL is "special". The tied source operand is second, not first.
619 if (MI->getOpcode() == ARM::t2MUL) {
Jim Grosbach3a21e2c2012-02-24 00:53:11 +0000620 unsigned Reg2 = MI->getOperand(2).getReg();
621 // Early exit if the regs aren't all low regs.
622 if (!isARMLowRegister(Reg0) || !isARMLowRegister(Reg1)
623 || !isARMLowRegister(Reg2))
624 return false;
625 if (Reg0 != Reg2) {
Jim Grosbachc01104d2012-02-24 00:33:36 +0000626 // If the other operand also isn't the same as the destination, we
627 // can't reduce.
628 if (Reg1 != Reg0)
629 return false;
630 // Try to commute the operands to make it a 2-address instruction.
631 MachineInstr *CommutedMI = TII->commuteInstruction(MI);
632 if (!CommutedMI)
633 return false;
634 }
635 } else if (Reg0 != Reg1) {
Bob Wilson279e55f2010-06-24 16:50:20 +0000636 // Try to commute the operands to make it a 2-address instruction.
637 unsigned CommOpIdx1, CommOpIdx2;
638 if (!TII->findCommutedOpIndices(MI, CommOpIdx1, CommOpIdx2) ||
639 CommOpIdx1 != 1 || MI->getOperand(CommOpIdx2).getReg() != Reg0)
640 return false;
641 MachineInstr *CommutedMI = TII->commuteInstruction(MI);
642 if (!CommutedMI)
643 return false;
644 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000645 if (Entry.LowRegs2 && !isARMLowRegister(Reg0))
646 return false;
647 if (Entry.Imm2Limit) {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000648 unsigned Imm = MI->getOperand(2).getImm();
Evan Cheng1be453b2009-08-08 03:21:23 +0000649 unsigned Limit = (1 << Entry.Imm2Limit) - 1;
650 if (Imm > Limit)
651 return false;
652 } else {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000653 unsigned Reg2 = MI->getOperand(2).getReg();
Evan Cheng1be453b2009-08-08 03:21:23 +0000654 if (Entry.LowRegs2 && !isARMLowRegister(Reg2))
655 return false;
656 }
657
Evan Cheng1f5bee12009-08-10 06:57:42 +0000658 // Check if it's possible / necessary to transfer the predicate.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000659 const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc2);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000660 unsigned PredReg = 0;
661 ARMCC::CondCodes Pred = getInstrPredicate(MI, PredReg);
662 bool SkipPred = false;
663 if (Pred != ARMCC::AL) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000664 if (!NewMCID.isPredicable())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000665 // Can't transfer predicate, fail.
666 return false;
667 } else {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000668 SkipPred = !NewMCID.isPredicable();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000669 }
670
Evan Cheng1be453b2009-08-08 03:21:23 +0000671 bool HasCC = false;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000672 bool CCDead = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000673 const MCInstrDesc &MCID = MI->getDesc();
674 if (MCID.hasOptionalDef()) {
675 unsigned NumOps = MCID.getNumOperands();
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000676 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
677 if (HasCC && MI->getOperand(NumOps-1).isDead())
678 CCDead = true;
679 }
Evan Cheng1f5bee12009-08-10 06:57:42 +0000680 if (!VerifyPredAndCC(MI, Entry, true, Pred, LiveCPSR, HasCC, CCDead))
Evan Chengd461c1c2009-08-09 19:17:19 +0000681 return false;
Evan Cheng1be453b2009-08-08 03:21:23 +0000682
Bob Wilsona2881ee2011-04-19 18:11:49 +0000683 // Avoid adding a false dependency on partial flag update by some 16-bit
684 // instructions which has the 's' bit set.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000685 if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
Evan Chengf4807a12011-10-27 21:21:05 +0000686 canAddPseudoFlagDep(CPSRDef, MI, IsSelfLoop))
Bob Wilsona2881ee2011-04-19 18:11:49 +0000687 return false;
688
Evan Cheng1be453b2009-08-08 03:21:23 +0000689 // Add the 16-bit instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000690 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000691 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000692 MIB.addOperand(MI->getOperand(0));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000693 if (NewMCID.hasOptionalDef()) {
Evan Cheng6ddd7bc2009-08-15 07:59:10 +0000694 if (HasCC)
695 AddDefaultT1CC(MIB, CCDead);
696 else
697 AddNoT1CC(MIB);
698 }
Evan Chengd461c1c2009-08-09 19:17:19 +0000699
700 // Transfer the rest of operands.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000701 unsigned NumOps = MCID.getNumOperands();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000702 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000703 if (i < NumOps && MCID.OpInfo[i].isOptionalDef())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000704 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000705 if (SkipPred && MCID.OpInfo[i].isPredicate())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000706 continue;
707 MIB.addOperand(MI->getOperand(i));
708 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000709
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000710 // Transfer MI flags.
711 MIB.setMIFlags(MI->getFlags());
712
Chris Lattnera6f074f2009-08-23 03:41:05 +0000713 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Cheng1be453b2009-08-08 03:21:23 +0000714
Evan Cheng7fae11b2011-12-14 02:11:42 +0000715 MBB.erase_instr(MI);
Evan Cheng1be453b2009-08-08 03:21:23 +0000716 ++Num2Addrs;
Evan Cheng1be453b2009-08-08 03:21:23 +0000717 return true;
718}
719
720bool
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000721Thumb2SizeReduce::ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
722 const ReduceEntry &Entry,
Evan Chengf4807a12011-10-27 21:21:05 +0000723 bool LiveCPSR, MachineInstr *CPSRDef,
724 bool IsSelfLoop) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000725 if (ReduceLimit != -1 && ((int)NumNarrows >= ReduceLimit))
726 return false;
727
Evan Chengddc0cb62012-12-20 19:59:30 +0000728 if (!MinimizeSize && !OptimizeSize && Entry.AvoidMovs &&
729 STI->avoidMOVsShifterOperand())
730 // Don't issue movs with shifter operand for some CPUs unless we
731 // are optimizing / minimizing for size.
732 return false;
733
Evan Chengd461c1c2009-08-09 19:17:19 +0000734 unsigned Limit = ~0U;
735 if (Entry.Imm1Limit)
Jim Grosbacha8a80672011-06-29 23:25:04 +0000736 Limit = (1 << Entry.Imm1Limit) - 1;
Evan Chengd461c1c2009-08-09 19:17:19 +0000737
Evan Cheng6cc775f2011-06-28 19:10:37 +0000738 const MCInstrDesc &MCID = MI->getDesc();
739 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i) {
740 if (MCID.OpInfo[i].isPredicate())
Evan Chengd461c1c2009-08-09 19:17:19 +0000741 continue;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000742 const MachineOperand &MO = MI->getOperand(i);
Evan Chengd461c1c2009-08-09 19:17:19 +0000743 if (MO.isReg()) {
744 unsigned Reg = MO.getReg();
745 if (!Reg || Reg == ARM::CPSR)
746 continue;
747 if (Entry.LowRegs1 && !isARMLowRegister(Reg))
748 return false;
Evan Chengf6a9d062009-08-11 23:00:31 +0000749 } else if (MO.isImm() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +0000750 !MCID.OpInfo[i].isPredicate()) {
Jim Grosbacha8a80672011-06-29 23:25:04 +0000751 if (((unsigned)MO.getImm()) > Limit)
Evan Chengd461c1c2009-08-09 19:17:19 +0000752 return false;
753 }
754 }
755
Evan Cheng1f5bee12009-08-10 06:57:42 +0000756 // Check if it's possible / necessary to transfer the predicate.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000757 const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc1);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000758 unsigned PredReg = 0;
759 ARMCC::CondCodes Pred = getInstrPredicate(MI, PredReg);
760 bool SkipPred = false;
761 if (Pred != ARMCC::AL) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000762 if (!NewMCID.isPredicable())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000763 // Can't transfer predicate, fail.
764 return false;
765 } else {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000766 SkipPred = !NewMCID.isPredicable();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000767 }
768
Evan Chengd461c1c2009-08-09 19:17:19 +0000769 bool HasCC = false;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000770 bool CCDead = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000771 if (MCID.hasOptionalDef()) {
772 unsigned NumOps = MCID.getNumOperands();
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000773 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
774 if (HasCC && MI->getOperand(NumOps-1).isDead())
775 CCDead = true;
776 }
Evan Cheng1f5bee12009-08-10 06:57:42 +0000777 if (!VerifyPredAndCC(MI, Entry, false, Pred, LiveCPSR, HasCC, CCDead))
Evan Chengd461c1c2009-08-09 19:17:19 +0000778 return false;
779
Bob Wilsona2881ee2011-04-19 18:11:49 +0000780 // Avoid adding a false dependency on partial flag update by some 16-bit
781 // instructions which has the 's' bit set.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000782 if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
Evan Chengf4807a12011-10-27 21:21:05 +0000783 canAddPseudoFlagDep(CPSRDef, MI, IsSelfLoop))
Bob Wilsona2881ee2011-04-19 18:11:49 +0000784 return false;
785
Evan Chengd461c1c2009-08-09 19:17:19 +0000786 // Add the 16-bit instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000787 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000788 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000789 MIB.addOperand(MI->getOperand(0));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000790 if (NewMCID.hasOptionalDef()) {
Evan Cheng6ddd7bc2009-08-15 07:59:10 +0000791 if (HasCC)
792 AddDefaultT1CC(MIB, CCDead);
793 else
794 AddNoT1CC(MIB);
795 }
Evan Chengd461c1c2009-08-09 19:17:19 +0000796
797 // Transfer the rest of operands.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000798 unsigned NumOps = MCID.getNumOperands();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000799 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000800 if (i < NumOps && MCID.OpInfo[i].isOptionalDef())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000801 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000802 if ((MCID.getOpcode() == ARM::t2RSBSri ||
Jim Grosbach8b31ef52011-07-27 16:47:19 +0000803 MCID.getOpcode() == ARM::t2RSBri ||
804 MCID.getOpcode() == ARM::t2SXTB ||
805 MCID.getOpcode() == ARM::t2SXTH ||
806 MCID.getOpcode() == ARM::t2UXTB ||
807 MCID.getOpcode() == ARM::t2UXTH) && i == 2)
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000808 // Skip the zero immediate operand, it's now implicit.
809 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000810 bool isPred = (i < NumOps && MCID.OpInfo[i].isPredicate());
Evan Chengf6a9d062009-08-11 23:00:31 +0000811 if (SkipPred && isPred)
812 continue;
813 const MachineOperand &MO = MI->getOperand(i);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000814 if (MO.isReg() && MO.isImplicit() && MO.getReg() == ARM::CPSR)
815 // Skip implicit def of CPSR. Either it's modeled as an optional
816 // def now or it's already an implicit def on the new instruction.
817 continue;
818 MIB.addOperand(MO);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000819 }
Evan Cheng6cc775f2011-06-28 19:10:37 +0000820 if (!MCID.isPredicable() && NewMCID.isPredicable())
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000821 AddDefaultPred(MIB);
Evan Chengd461c1c2009-08-09 19:17:19 +0000822
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000823 // Transfer MI flags.
824 MIB.setMIFlags(MI->getFlags());
825
Chris Lattnera6f074f2009-08-23 03:41:05 +0000826 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Chengd461c1c2009-08-09 19:17:19 +0000827
Evan Cheng7fae11b2011-12-14 02:11:42 +0000828 MBB.erase_instr(MI);
Evan Chengd461c1c2009-08-09 19:17:19 +0000829 ++NumNarrows;
830 return true;
Evan Cheng1be453b2009-08-08 03:21:23 +0000831}
832
Bob Wilsona2881ee2011-04-19 18:11:49 +0000833static bool UpdateCPSRDef(MachineInstr &MI, bool LiveCPSR, bool &DefCPSR) {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000834 bool HasDef = false;
835 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
836 const MachineOperand &MO = MI.getOperand(i);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000837 if (!MO.isReg() || MO.isUndef() || MO.isUse())
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000838 continue;
839 if (MO.getReg() != ARM::CPSR)
840 continue;
Bob Wilsona2881ee2011-04-19 18:11:49 +0000841
842 DefCPSR = true;
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000843 if (!MO.isDead())
844 HasDef = true;
845 }
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000846
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000847 return HasDef || LiveCPSR;
848}
849
850static bool UpdateCPSRUse(MachineInstr &MI, bool LiveCPSR) {
851 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
852 const MachineOperand &MO = MI.getOperand(i);
853 if (!MO.isReg() || MO.isUndef() || MO.isDef())
854 continue;
855 if (MO.getReg() != ARM::CPSR)
856 continue;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000857 assert(LiveCPSR && "CPSR liveness tracking is wrong!");
858 if (MO.isKill()) {
859 LiveCPSR = false;
860 break;
861 }
862 }
863
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000864 return LiveCPSR;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000865}
866
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000867bool Thumb2SizeReduce::ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI,
868 bool LiveCPSR, MachineInstr *CPSRDef,
869 bool IsSelfLoop) {
870 unsigned Opcode = MI->getOpcode();
871 DenseMap<unsigned, unsigned>::iterator OPI = ReduceOpcodeMap.find(Opcode);
872 if (OPI == ReduceOpcodeMap.end())
873 return false;
874 const ReduceEntry &Entry = ReduceTable[OPI->second];
875
876 // Don't attempt normal reductions on "special" cases for now.
877 if (Entry.Special)
878 return ReduceSpecial(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop);
879
880 // Try to transform to a 16-bit two-address instruction.
881 if (Entry.NarrowOpc2 &&
882 ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop))
883 return true;
884
885 // Try to transform to a 16-bit non-two-address instruction.
886 if (Entry.NarrowOpc1 &&
887 ReduceToNarrow(MBB, MI, Entry, LiveCPSR, CPSRDef, IsSelfLoop))
888 return true;
889
890 return false;
891}
892
Evan Cheng1be453b2009-08-08 03:21:23 +0000893bool Thumb2SizeReduce::ReduceMBB(MachineBasicBlock &MBB) {
894 bool Modified = false;
895
Evan Cheng1f5bee12009-08-10 06:57:42 +0000896 // Yes, CPSR could be livein.
Dan Gohmana1cf9fe2010-04-13 16:53:51 +0000897 bool LiveCPSR = MBB.isLiveIn(ARM::CPSR);
Bob Wilsona2881ee2011-04-19 18:11:49 +0000898 MachineInstr *CPSRDef = 0;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000899 MachineInstr *BundleMI = 0;
Evan Cheng1f5bee12009-08-10 06:57:42 +0000900
Evan Chengf4807a12011-10-27 21:21:05 +0000901 // If this BB loops back to itself, conservatively avoid narrowing the
902 // first instruction that does partial flag update.
903 bool IsSelfLoop = MBB.isSuccessor(&MBB);
Jim Grosbach0c509fa2012-04-06 23:43:50 +0000904 MachineBasicBlock::instr_iterator MII = MBB.instr_begin(),E = MBB.instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000905 MachineBasicBlock::instr_iterator NextMII;
Evan Cheng1be453b2009-08-08 03:21:23 +0000906 for (; MII != E; MII = NextMII) {
Chris Lattnera48f44d2009-12-03 00:50:42 +0000907 NextMII = llvm::next(MII);
Evan Cheng1be453b2009-08-08 03:21:23 +0000908
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000909 MachineInstr *MI = &*MII;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000910 if (MI->isBundle()) {
911 BundleMI = MI;
912 continue;
913 }
914
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000915 LiveCPSR = UpdateCPSRUse(*MI, LiveCPSR);
916
Jakob Stoklund Olesen41bbf9c2012-12-18 00:46:39 +0000917 // Does NextMII belong to the same bundle as MI?
918 bool NextInSameBundle = NextMII != E && NextMII->isBundledWithPred();
919
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000920 if (ReduceMI(MBB, MI, LiveCPSR, CPSRDef, IsSelfLoop)) {
921 Modified = true;
922 MachineBasicBlock::instr_iterator I = prior(NextMII);
923 MI = &*I;
Jakob Stoklund Olesen41bbf9c2012-12-18 00:46:39 +0000924 // Removing and reinserting the first instruction in a bundle will break
925 // up the bundle. Fix the bundling if it was broken.
926 if (NextInSameBundle && !NextMII->isBundledWithPred())
927 NextMII->bundleWithPred();
Evan Cheng1be453b2009-08-08 03:21:23 +0000928 }
929
Jakob Stoklund Olesen41bbf9c2012-12-18 00:46:39 +0000930 if (!NextInSameBundle && MI->isInsideBundle()) {
Evan Cheng7fae11b2011-12-14 02:11:42 +0000931 // FIXME: Since post-ra scheduler operates on bundles, the CPSR kill
932 // marker is only on the BUNDLE instruction. Process the BUNDLE
933 // instruction as we finish with the bundled instruction to work around
934 // the inconsistency.
Evan Cheng903231b2011-12-17 01:25:34 +0000935 if (BundleMI->killsRegister(ARM::CPSR))
936 LiveCPSR = false;
937 MachineOperand *MO = BundleMI->findRegisterDefOperand(ARM::CPSR);
938 if (MO && !MO->isDead())
939 LiveCPSR = true;
940 }
Evan Cheng7fae11b2011-12-14 02:11:42 +0000941
Bob Wilsona2881ee2011-04-19 18:11:49 +0000942 bool DefCPSR = false;
943 LiveCPSR = UpdateCPSRDef(*MI, LiveCPSR, DefCPSR);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000944 if (MI->isCall()) {
Bob Wilsona2881ee2011-04-19 18:11:49 +0000945 // Calls don't really set CPSR.
946 CPSRDef = 0;
Evan Chengf4807a12011-10-27 21:21:05 +0000947 IsSelfLoop = false;
948 } else if (DefCPSR) {
Bob Wilsona2881ee2011-04-19 18:11:49 +0000949 // This is the last CPSR defining instruction.
950 CPSRDef = MI;
Evan Chengf4807a12011-10-27 21:21:05 +0000951 IsSelfLoop = false;
952 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000953 }
954
955 return Modified;
956}
957
958bool Thumb2SizeReduce::runOnMachineFunction(MachineFunction &MF) {
959 const TargetMachine &TM = MF.getTarget();
Evan Cheng6ddd7bc2009-08-15 07:59:10 +0000960 TII = static_cast<const Thumb2InstrInfo*>(TM.getInstrInfo());
Bob Wilsona2881ee2011-04-19 18:11:49 +0000961 STI = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng1be453b2009-08-08 03:21:23 +0000962
Evan Chengddc0cb62012-12-20 19:59:30 +0000963 // Optimizing / minimizing size?
964 Attribute FnAttrs = MF.getFunction()->getFnAttributes();
965 OptimizeSize = FnAttrs.hasAttribute(Attribute::OptimizeForSize);
966 MinimizeSize = FnAttrs.hasAttribute(Attribute::MinSize);
Quentin Colombet23b404d2012-12-18 22:47:16 +0000967
Evan Cheng1be453b2009-08-08 03:21:23 +0000968 bool Modified = false;
969 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
970 Modified |= ReduceMBB(*I);
971 return Modified;
972}
973
974/// createThumb2SizeReductionPass - Returns an instance of the Thumb2 size
975/// reduction pass.
976FunctionPass *llvm::createThumb2SizeReductionPass() {
977 return new Thumb2SizeReduce();
978}