blob: 6792f8b1f80d8fdc4c15b13242d7b22e09366f29 [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
Evan Cheng1be453b2009-08-08 03:21:23 +000010#include "ARM.h"
Evan Cheng1be453b2009-08-08 03:21:23 +000011#include "ARMBaseInstrInfo.h"
Bob Wilsona2881ee2011-04-19 18:11:49 +000012#include "ARMSubtarget.h"
Evan Chenga20cde32011-07-20 23:34:39 +000013#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "Thumb2InstrInfo.h"
15#include "llvm/ADT/DenseMap.h"
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +000016#include "llvm/ADT/PostOrderIterator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/Statistic.h"
18#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Cheng1be453b2009-08-08 03:21:23 +000019#include "llvm/CodeGen/MachineInstr.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Function.h" // To access Function attributes
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"
Craig Toppera9253262014-03-22 23:51:00 +000024#include "llvm/Target/TargetMachine.h"
Evan Cheng1be453b2009-08-08 03:21:23 +000025using namespace llvm;
26
Chandler Carruth84e68b22014-04-22 02:41:26 +000027#define DEBUG_TYPE "t2-reduce-size"
28
Evan Cheng1f5bee12009-08-10 06:57:42 +000029STATISTIC(NumNarrows, "Number of 32-bit instrs reduced to 16-bit ones");
30STATISTIC(Num2Addrs, "Number of 32-bit instrs reduced to 2addr 16-bit ones");
Evan Cheng36064672009-08-11 08:52:18 +000031STATISTIC(NumLdSts, "Number of 32-bit load / store reduced to 16-bit ones");
Evan Cheng1be453b2009-08-08 03:21:23 +000032
Evan Chengcc9ca352009-08-11 21:11:32 +000033static cl::opt<int> ReduceLimit("t2-reduce-limit",
34 cl::init(-1), cl::Hidden);
35static cl::opt<int> ReduceLimit2Addr("t2-reduce-limit2",
36 cl::init(-1), cl::Hidden);
37static cl::opt<int> ReduceLimitLdSt("t2-reduce-limit3",
38 cl::init(-1), cl::Hidden);
Evan Chengf16a1d52009-08-10 07:20:37 +000039
Evan Cheng1be453b2009-08-08 03:21:23 +000040namespace {
41 /// ReduceTable - A static table with information on mapping from wide
42 /// opcodes to narrow
43 struct ReduceEntry {
Craig Topperca658c22012-03-11 07:16:55 +000044 uint16_t WideOpc; // Wide opcode
45 uint16_t NarrowOpc1; // Narrow opcode to transform to
46 uint16_t NarrowOpc2; // Narrow opcode when it's two-address
Evan Cheng1be453b2009-08-08 03:21:23 +000047 uint8_t Imm1Limit; // Limit of immediate field (bits)
48 uint8_t Imm2Limit; // Limit of immediate field when it's two-address
49 unsigned LowRegs1 : 1; // Only possible if low-registers are used
50 unsigned LowRegs2 : 1; // Only possible if low-registers are used (2addr)
Evan Cheng1e6c2a12009-08-12 01:49:45 +000051 unsigned PredCC1 : 2; // 0 - If predicated, cc is on and vice versa.
Evan Cheng1be453b2009-08-08 03:21:23 +000052 // 1 - No cc field.
Evan Cheng1e6c2a12009-08-12 01:49:45 +000053 // 2 - Always set CPSR.
Evan Chengaee7e492009-08-12 18:35:50 +000054 unsigned PredCC2 : 2;
Bob Wilsona2881ee2011-04-19 18:11:49 +000055 unsigned PartFlag : 1; // 16-bit instruction does partial flag update
Evan Cheng1be453b2009-08-08 03:21:23 +000056 unsigned Special : 1; // Needs to be dealt with specially
Evan Chengddc0cb62012-12-20 19:59:30 +000057 unsigned AvoidMovs: 1; // Avoid movs with shifter operand (for Swift)
Evan Cheng1be453b2009-08-08 03:21:23 +000058 };
59
60 static const ReduceEntry ReduceTable[] = {
Evan Chengddc0cb62012-12-20 19:59:30 +000061 // Wide, Narrow1, Narrow2, imm1,imm2, lo1, lo2, P/C,PF,S,AM
62 { ARM::t2ADCrr, 0, ARM::tADC, 0, 0, 0, 1, 0,0, 0,0,0 },
63 { ARM::t2ADDri, ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 0,0, 0,1,0 },
64 { ARM::t2ADDrr, ARM::tADDrr, ARM::tADDhirr, 0, 0, 1, 0, 0,1, 0,0,0 },
65 { ARM::t2ADDSri,ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 2,2, 0,1,0 },
66 { ARM::t2ADDSrr,ARM::tADDrr, 0, 0, 0, 1, 0, 2,0, 0,1,0 },
67 { ARM::t2ANDrr, 0, ARM::tAND, 0, 0, 0, 1, 0,0, 1,0,0 },
68 { ARM::t2ASRri, ARM::tASRri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
69 { ARM::t2ASRrr, 0, ARM::tASRrr, 0, 0, 0, 1, 0,0, 1,0,1 },
70 { ARM::t2BICrr, 0, ARM::tBIC, 0, 0, 0, 1, 0,0, 1,0,0 },
71 //FIXME: Disable CMN, as CCodes are backwards from compare expectations
72 //{ ARM::t2CMNrr, ARM::tCMN, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
73 { ARM::t2CMNzrr, ARM::tCMNz, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
74 { ARM::t2CMPri, ARM::tCMPi8, 0, 8, 0, 1, 0, 2,0, 0,0,0 },
75 { ARM::t2CMPrr, ARM::tCMPhir, 0, 0, 0, 0, 0, 2,0, 0,1,0 },
76 { ARM::t2EORrr, 0, ARM::tEOR, 0, 0, 0, 1, 0,0, 1,0,0 },
77 // FIXME: adr.n immediate offset must be multiple of 4.
78 //{ ARM::t2LEApcrelJT,ARM::tLEApcrelJT, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
79 { ARM::t2LSLri, ARM::tLSLri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
80 { ARM::t2LSLrr, 0, ARM::tLSLrr, 0, 0, 0, 1, 0,0, 1,0,1 },
81 { ARM::t2LSRri, ARM::tLSRri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
82 { ARM::t2LSRrr, 0, ARM::tLSRrr, 0, 0, 0, 1, 0,0, 1,0,1 },
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +000083 { ARM::t2MOVi, ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 1,0,0 },
84 { ARM::t2MOVi16,ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 1,1,0 },
Evan Chengddc0cb62012-12-20 19:59:30 +000085 // FIXME: Do we need the 16-bit 'S' variant?
86 { ARM::t2MOVr,ARM::tMOVr, 0, 0, 0, 0, 0, 1,0, 0,0,0 },
87 { ARM::t2MUL, 0, ARM::tMUL, 0, 0, 0, 1, 0,0, 1,0,0 },
88 { ARM::t2MVNr, ARM::tMVN, 0, 0, 0, 1, 0, 0,0, 0,0,0 },
89 { ARM::t2ORRrr, 0, ARM::tORR, 0, 0, 0, 1, 0,0, 1,0,0 },
90 { ARM::t2REV, ARM::tREV, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
91 { ARM::t2REV16, ARM::tREV16, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
92 { ARM::t2REVSH, ARM::tREVSH, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
93 { ARM::t2RORrr, 0, ARM::tROR, 0, 0, 0, 1, 0,0, 1,0,0 },
94 { ARM::t2RSBri, ARM::tRSB, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
95 { ARM::t2RSBSri,ARM::tRSB, 0, 0, 0, 1, 0, 2,0, 0,1,0 },
96 { ARM::t2SBCrr, 0, ARM::tSBC, 0, 0, 0, 1, 0,0, 0,0,0 },
97 { ARM::t2SUBri, ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 0,0, 0,0,0 },
98 { ARM::t2SUBrr, ARM::tSUBrr, 0, 0, 0, 1, 0, 0,0, 0,0,0 },
99 { ARM::t2SUBSri,ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 2,2, 0,0,0 },
100 { ARM::t2SUBSrr,ARM::tSUBrr, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
101 { ARM::t2SXTB, ARM::tSXTB, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
102 { ARM::t2SXTH, ARM::tSXTH, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
103 { ARM::t2TSTrr, ARM::tTST, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
104 { ARM::t2UXTB, ARM::tUXTB, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
105 { ARM::t2UXTH, ARM::tUXTH, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
Evan Cheng36064672009-08-11 08:52:18 +0000106
Evan Chengddc0cb62012-12-20 19:59:30 +0000107 // FIXME: Clean this up after splitting each Thumb load / store opcode
108 // into multiple ones.
109 { ARM::t2LDRi12,ARM::tLDRi, ARM::tLDRspi, 5, 8, 1, 0, 0,0, 0,1,0 },
110 { ARM::t2LDRs, ARM::tLDRr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
111 { ARM::t2LDRBi12,ARM::tLDRBi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
112 { ARM::t2LDRBs, ARM::tLDRBr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
113 { ARM::t2LDRHi12,ARM::tLDRHi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
114 { ARM::t2LDRHs, ARM::tLDRHr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
115 { ARM::t2LDRSBs,ARM::tLDRSB, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
116 { ARM::t2LDRSHs,ARM::tLDRSH, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
117 { ARM::t2STRi12,ARM::tSTRi, ARM::tSTRspi, 5, 8, 1, 0, 0,0, 0,1,0 },
118 { ARM::t2STRs, ARM::tSTRr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
119 { ARM::t2STRBi12,ARM::tSTRBi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
120 { ARM::t2STRBs, ARM::tSTRBr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
121 { ARM::t2STRHi12,ARM::tSTRHi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
122 { ARM::t2STRHs, ARM::tSTRHr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
Evan Chengcc9ca352009-08-11 21:11:32 +0000123
Evan Chengddc0cb62012-12-20 19:59:30 +0000124 { ARM::t2LDMIA, ARM::tLDMIA, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
125 { ARM::t2LDMIA_RET,0, ARM::tPOP_RET, 0, 0, 1, 1, 1,1, 0,1,0 },
126 { ARM::t2LDMIA_UPD,ARM::tLDMIA_UPD,ARM::tPOP,0, 0, 1, 1, 1,1, 0,1,0 },
127 // ARM::t2STM (with no basereg writeback) has no Thumb1 equivalent
128 { ARM::t2STMIA_UPD,ARM::tSTMIA_UPD, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
129 { ARM::t2STMDB_UPD, 0, ARM::tPUSH, 0, 0, 1, 1, 1,1, 0,1,0 }
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
Craig Topper6bc27bf2014-03-10 02:09:33 +0000140 bool runOnMachineFunction(MachineFunction &MF) override;
Evan Cheng1be453b2009-08-08 03:21:23 +0000141
Craig Topper6bc27bf2014-03-10 02:09:33 +0000142 const char *getPassName() const override {
Evan Cheng1be453b2009-08-08 03:21:23 +0000143 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
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000150 bool canAddPseudoFlagDep(MachineInstr *Use, bool IsSelfLoop);
Bob Wilsona2881ee2011-04-19 18:11:49 +0000151
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000152 bool VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
153 bool is2Addr, ARMCC::CondCodes Pred,
154 bool LiveCPSR, bool &HasCC, bool &CCDead);
155
Evan Cheng36064672009-08-11 08:52:18 +0000156 bool ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
157 const ReduceEntry &Entry);
158
159 bool ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000160 const ReduceEntry &Entry, bool LiveCPSR, bool IsSelfLoop);
Evan Cheng36064672009-08-11 08:52:18 +0000161
Evan Cheng1be453b2009-08-08 03:21:23 +0000162 /// ReduceTo2Addr - Reduce a 32-bit instruction to a 16-bit two-address
163 /// instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000164 bool ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000165 const ReduceEntry &Entry, bool LiveCPSR,
Evan Chengf4807a12011-10-27 21:21:05 +0000166 bool IsSelfLoop);
Evan Cheng1be453b2009-08-08 03:21:23 +0000167
168 /// ReduceToNarrow - Reduce a 32-bit instruction to a 16-bit
169 /// non-two-address instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000170 bool ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000171 const ReduceEntry &Entry, bool LiveCPSR,
Evan Chengf4807a12011-10-27 21:21:05 +0000172 bool IsSelfLoop);
Evan Cheng1be453b2009-08-08 03:21:23 +0000173
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000174 /// ReduceMI - Attempt to reduce MI, return true on success.
175 bool ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000176 bool LiveCPSR, bool IsSelfLoop);
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000177
Evan Cheng1be453b2009-08-08 03:21:23 +0000178 /// ReduceMBB - Reduce width of instructions in the specified basic block.
179 bool ReduceMBB(MachineBasicBlock &MBB);
Quentin Colombet23b404d2012-12-18 22:47:16 +0000180
Evan Chengddc0cb62012-12-20 19:59:30 +0000181 bool OptimizeSize;
Quentin Colombet23b404d2012-12-18 22:47:16 +0000182 bool MinimizeSize;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000183
184 // Last instruction to define CPSR in the current block.
185 MachineInstr *CPSRDef;
186 // Was CPSR last defined by a high latency instruction?
187 // When CPSRDef is null, this refers to CPSR defs in predecessors.
188 bool HighLatencyCPSR;
189
190 struct MBBInfo {
191 // The flags leaving this block have high latency.
192 bool HighLatencyCPSR;
193 // Has this block been visited yet?
194 bool Visited;
195
196 MBBInfo() : HighLatencyCPSR(false), Visited(false) {}
197 };
198
199 SmallVector<MBBInfo, 8> BlockInfo;
Evan Cheng1be453b2009-08-08 03:21:23 +0000200 };
201 char Thumb2SizeReduce::ID = 0;
202}
203
Owen Andersona7aed182010-08-06 18:33:48 +0000204Thumb2SizeReduce::Thumb2SizeReduce() : MachineFunctionPass(ID) {
Evan Chengddc0cb62012-12-20 19:59:30 +0000205 OptimizeSize = MinimizeSize = false;
Evan Cheng1be453b2009-08-08 03:21:23 +0000206 for (unsigned i = 0, e = array_lengthof(ReduceTable); i != e; ++i) {
207 unsigned FromOpc = ReduceTable[i].WideOpc;
208 if (!ReduceOpcodeMap.insert(std::make_pair(FromOpc, i)).second)
209 assert(false && "Duplicated entries?");
210 }
211}
212
Evan Cheng6cc775f2011-06-28 19:10:37 +0000213static bool HasImplicitCPSRDef(const MCInstrDesc &MCID) {
Craig Topper5a4bcc72012-03-08 08:22:45 +0000214 for (const uint16_t *Regs = MCID.getImplicitDefs(); *Regs; ++Regs)
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000215 if (*Regs == ARM::CPSR)
216 return true;
217 return false;
218}
219
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000220// Check for a likely high-latency flag def.
221static bool isHighLatencyCPSR(MachineInstr *Def) {
222 switch(Def->getOpcode()) {
223 case ARM::FMSTAT:
224 case ARM::tMUL:
225 return true;
226 }
227 return false;
228}
229
Bob Wilsona2881ee2011-04-19 18:11:49 +0000230/// canAddPseudoFlagDep - For A9 (and other out-of-order) implementations,
231/// the 's' 16-bit instruction partially update CPSR. Abort the
232/// transformation to avoid adding false dependency on last CPSR setting
233/// instruction which hurts the ability for out-of-order execution engine
234/// to do register renaming magic.
235/// This function checks if there is a read-of-write dependency between the
236/// last instruction that defines the CPSR and the current instruction. If there
237/// is, then there is no harm done since the instruction cannot be retired
238/// before the CPSR setting instruction anyway.
239/// Note, we are not doing full dependency analysis here for the sake of compile
240/// time. We're not looking for cases like:
241/// r0 = muls ...
242/// r1 = add.w r0, ...
243/// ...
244/// = mul.w r1
245/// In this case it would have been ok to narrow the mul.w to muls since there
246/// are indirect RAW dependency between the muls and the mul.w
247bool
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000248Thumb2SizeReduce::canAddPseudoFlagDep(MachineInstr *Use, bool FirstInSelfLoop) {
Quentin Colombet23b404d2012-12-18 22:47:16 +0000249 // Disable the check for -Oz (aka OptimizeForSizeHarder).
250 if (MinimizeSize || !STI->avoidCPSRPartialUpdate())
Bob Wilsona2881ee2011-04-19 18:11:49 +0000251 return false;
252
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000253 if (!CPSRDef)
Evan Chengf4807a12011-10-27 21:21:05 +0000254 // If this BB loops back to itself, conservatively avoid narrowing the
255 // first instruction that does partial flag update.
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000256 return HighLatencyCPSR || FirstInSelfLoop;
Evan Chengf4807a12011-10-27 21:21:05 +0000257
Bob Wilsona2881ee2011-04-19 18:11:49 +0000258 SmallSet<unsigned, 2> Defs;
Owen Anderson8c1f17b2014-03-07 22:48:22 +0000259 for (const MachineOperand &MO : CPSRDef->operands()) {
Bob Wilsona2881ee2011-04-19 18:11:49 +0000260 if (!MO.isReg() || MO.isUndef() || MO.isUse())
261 continue;
262 unsigned Reg = MO.getReg();
263 if (Reg == 0 || Reg == ARM::CPSR)
264 continue;
265 Defs.insert(Reg);
266 }
267
Owen Anderson8c1f17b2014-03-07 22:48:22 +0000268 for (const MachineOperand &MO : Use->operands()) {
Bob Wilsona2881ee2011-04-19 18:11:49 +0000269 if (!MO.isReg() || MO.isUndef() || MO.isDef())
270 continue;
271 unsigned Reg = MO.getReg();
272 if (Defs.count(Reg))
273 return false;
274 }
275
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000276 // If the current CPSR has high latency, try to avoid the false dependency.
277 if (HighLatencyCPSR)
278 return true;
279
280 // tMOVi8 usually doesn't start long dependency chains, and there are a lot
281 // of them, so always shrink them when CPSR doesn't have high latency.
282 if (Use->getOpcode() == ARM::t2MOVi ||
283 Use->getOpcode() == ARM::t2MOVi16)
284 return false;
285
Bob Wilsona2881ee2011-04-19 18:11:49 +0000286 // No read-after-write dependency. The narrowing will add false dependency.
287 return true;
288}
289
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000290bool
291Thumb2SizeReduce::VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
292 bool is2Addr, ARMCC::CondCodes Pred,
293 bool LiveCPSR, bool &HasCC, bool &CCDead) {
Evan Chengd461c1c2009-08-09 19:17:19 +0000294 if ((is2Addr && Entry.PredCC2 == 0) ||
295 (!is2Addr && Entry.PredCC1 == 0)) {
296 if (Pred == ARMCC::AL) {
297 // Not predicated, must set CPSR.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000298 if (!HasCC) {
299 // Original instruction was not setting CPSR, but CPSR is not
300 // currently live anyway. It's ok to set it. The CPSR def is
301 // dead though.
302 if (!LiveCPSR) {
303 HasCC = true;
304 CCDead = true;
305 return true;
306 }
307 return false;
308 }
Evan Chengd461c1c2009-08-09 19:17:19 +0000309 } else {
310 // Predicated, must not set CPSR.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000311 if (HasCC)
312 return false;
Evan Chengd461c1c2009-08-09 19:17:19 +0000313 }
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000314 } else if ((is2Addr && Entry.PredCC2 == 2) ||
315 (!is2Addr && Entry.PredCC1 == 2)) {
316 /// Old opcode has an optional def of CPSR.
317 if (HasCC)
318 return true;
Jim Grosbachbc7eeaf2010-09-14 20:35:46 +0000319 // If old opcode does not implicitly define CPSR, then it's not ok since
320 // these new opcodes' CPSR def is not meant to be thrown away. e.g. CMP.
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000321 if (!HasImplicitCPSRDef(MI->getDesc()))
322 return false;
323 HasCC = true;
Evan Chengd461c1c2009-08-09 19:17:19 +0000324 } else {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000325 // 16-bit instruction does not set CPSR.
326 if (HasCC)
327 return false;
Evan Chengd461c1c2009-08-09 19:17:19 +0000328 }
329
330 return true;
331}
332
Evan Chengcc9ca352009-08-11 21:11:32 +0000333static bool VerifyLowRegs(MachineInstr *MI) {
334 unsigned Opc = MI->getOpcode();
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000335 bool isPCOk = (Opc == ARM::t2LDMIA_RET || Opc == ARM::t2LDMIA ||
336 Opc == ARM::t2LDMDB || Opc == ARM::t2LDMIA_UPD ||
Owen Anderson4ebf4712011-02-08 22:39:40 +0000337 Opc == ARM::t2LDMDB_UPD);
Tim Northoverba1d7042014-09-10 12:53:28 +0000338 bool isLROk = (Opc == ARM::t2STMDB_UPD);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000339 bool isSPOk = isPCOk || isLROk;
Evan Chengcc9ca352009-08-11 21:11:32 +0000340 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
341 const MachineOperand &MO = MI->getOperand(i);
342 if (!MO.isReg() || MO.isImplicit())
343 continue;
344 unsigned Reg = MO.getReg();
345 if (Reg == 0 || Reg == ARM::CPSR)
346 continue;
347 if (isPCOk && Reg == ARM::PC)
348 continue;
349 if (isLROk && Reg == ARM::LR)
350 continue;
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000351 if (Reg == ARM::SP) {
352 if (isSPOk)
353 continue;
354 if (i == 1 && (Opc == ARM::t2LDRi12 || Opc == ARM::t2STRi12))
355 // Special case for these ldr / str with sp as base register.
356 continue;
357 }
Evan Chengcc9ca352009-08-11 21:11:32 +0000358 if (!isARMLowRegister(Reg))
359 return false;
360 }
361 return true;
362}
363
Evan Cheng1be453b2009-08-08 03:21:23 +0000364bool
Evan Cheng36064672009-08-11 08:52:18 +0000365Thumb2SizeReduce::ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
366 const ReduceEntry &Entry) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000367 if (ReduceLimitLdSt != -1 && ((int)NumLdSts >= ReduceLimitLdSt))
368 return false;
369
Evan Cheng36064672009-08-11 08:52:18 +0000370 unsigned Scale = 1;
371 bool HasImmOffset = false;
372 bool HasShift = false;
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000373 bool HasOffReg = true;
Evan Chengcc9ca352009-08-11 21:11:32 +0000374 bool isLdStMul = false;
Evan Chengcc9ca352009-08-11 21:11:32 +0000375 unsigned Opc = Entry.NarrowOpc1;
376 unsigned OpNum = 3; // First 'rest' of operands.
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000377 uint8_t ImmLimit = Entry.Imm1Limit;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000378
Evan Cheng36064672009-08-11 08:52:18 +0000379 switch (Entry.WideOpc) {
380 default:
381 llvm_unreachable("Unexpected Thumb2 load / store opcode!");
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000382 case ARM::t2LDRi12:
Bill Wendling092a7bd2010-12-14 03:36:38 +0000383 case ARM::t2STRi12:
384 if (MI->getOperand(1).getReg() == ARM::SP) {
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000385 Opc = Entry.NarrowOpc2;
386 ImmLimit = Entry.Imm2Limit;
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000387 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000388
Evan Cheng36064672009-08-11 08:52:18 +0000389 Scale = 4;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000390 HasImmOffset = true;
391 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000392 break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000393 case ARM::t2LDRBi12:
Evan Cheng36064672009-08-11 08:52:18 +0000394 case ARM::t2STRBi12:
Owen Anderson4ebf4712011-02-08 22:39:40 +0000395 HasImmOffset = true;
396 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000397 break;
398 case ARM::t2LDRHi12:
399 case ARM::t2STRHi12:
400 Scale = 2;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000401 HasImmOffset = true;
402 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000403 break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000404 case ARM::t2LDRs:
405 case ARM::t2LDRBs:
406 case ARM::t2LDRHs:
Evan Cheng36064672009-08-11 08:52:18 +0000407 case ARM::t2LDRSBs:
408 case ARM::t2LDRSHs:
409 case ARM::t2STRs:
410 case ARM::t2STRBs:
411 case ARM::t2STRHs:
412 HasShift = true;
Evan Chengcc9ca352009-08-11 21:11:32 +0000413 OpNum = 4;
Evan Cheng36064672009-08-11 08:52:18 +0000414 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000415 case ARM::t2LDMIA:
416 case ARM::t2LDMDB: {
Evan Chengcc9ca352009-08-11 21:11:32 +0000417 unsigned BaseReg = MI->getOperand(0).getReg();
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000418 if (!isARMLowRegister(BaseReg) || Entry.WideOpc != ARM::t2LDMIA)
Bob Wilson947f04b2010-03-13 01:08:20 +0000419 return false;
Bill Wendling092a7bd2010-12-14 03:36:38 +0000420
Jim Grosbach88628e92010-09-07 22:30:53 +0000421 // For the non-writeback version (this one), the base register must be
422 // one of the registers being loaded.
423 bool isOK = false;
424 for (unsigned i = 4; i < MI->getNumOperands(); ++i) {
425 if (MI->getOperand(i).getReg() == BaseReg) {
426 isOK = true;
427 break;
428 }
429 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000430
Jim Grosbach88628e92010-09-07 22:30:53 +0000431 if (!isOK)
432 return false;
433
Bob Wilson947f04b2010-03-13 01:08:20 +0000434 OpNum = 0;
435 isLdStMul = true;
436 break;
437 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000438 case ARM::t2LDMIA_RET: {
Bob Wilson947f04b2010-03-13 01:08:20 +0000439 unsigned BaseReg = MI->getOperand(1).getReg();
440 if (BaseReg != ARM::SP)
441 return false;
442 Opc = Entry.NarrowOpc2; // tPOP_RET
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000443 OpNum = 2;
Bob Wilson947f04b2010-03-13 01:08:20 +0000444 isLdStMul = true;
445 break;
446 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000447 case ARM::t2LDMIA_UPD:
448 case ARM::t2LDMDB_UPD:
449 case ARM::t2STMIA_UPD:
450 case ARM::t2STMDB_UPD: {
Bob Wilson947f04b2010-03-13 01:08:20 +0000451 OpNum = 0;
Bill Wendling092a7bd2010-12-14 03:36:38 +0000452
Bob Wilson947f04b2010-03-13 01:08:20 +0000453 unsigned BaseReg = MI->getOperand(1).getReg();
Bob Wilson947f04b2010-03-13 01:08:20 +0000454 if (BaseReg == ARM::SP &&
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000455 (Entry.WideOpc == ARM::t2LDMIA_UPD ||
456 Entry.WideOpc == ARM::t2STMDB_UPD)) {
Bob Wilson947f04b2010-03-13 01:08:20 +0000457 Opc = Entry.NarrowOpc2; // tPOP or tPUSH
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000458 OpNum = 2;
459 } else if (!isARMLowRegister(BaseReg) ||
460 (Entry.WideOpc != ARM::t2LDMIA_UPD &&
461 Entry.WideOpc != ARM::t2STMIA_UPD)) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000462 return false;
463 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000464
Evan Chengcc9ca352009-08-11 21:11:32 +0000465 isLdStMul = true;
466 break;
467 }
Evan Cheng36064672009-08-11 08:52:18 +0000468 }
469
470 unsigned OffsetReg = 0;
471 bool OffsetKill = false;
472 if (HasShift) {
473 OffsetReg = MI->getOperand(2).getReg();
474 OffsetKill = MI->getOperand(2).isKill();
Bill Wendling092a7bd2010-12-14 03:36:38 +0000475
Evan Cheng36064672009-08-11 08:52:18 +0000476 if (MI->getOperand(3).getImm())
477 // Thumb1 addressing mode doesn't support shift.
478 return false;
479 }
480
481 unsigned OffsetImm = 0;
482 if (HasImmOffset) {
483 OffsetImm = MI->getOperand(2).getImm();
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000484 unsigned MaxOffset = ((1 << ImmLimit) - 1) * Scale;
Bill Wendling092a7bd2010-12-14 03:36:38 +0000485
486 if ((OffsetImm & (Scale - 1)) || OffsetImm > MaxOffset)
Evan Cheng36064672009-08-11 08:52:18 +0000487 // Make sure the immediate field fits.
488 return false;
489 }
490
491 // Add the 16-bit load / store instruction.
Evan Cheng36064672009-08-11 08:52:18 +0000492 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000493 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, TII->get(Opc));
Evan Chengcc9ca352009-08-11 21:11:32 +0000494 if (!isLdStMul) {
Owen Anderson99ea8a32010-12-07 00:45:21 +0000495 MIB.addOperand(MI->getOperand(0));
Owen Anderson4ebf4712011-02-08 22:39:40 +0000496 MIB.addOperand(MI->getOperand(1));
Bill Wendling092a7bd2010-12-14 03:36:38 +0000497
498 if (HasImmOffset)
499 MIB.addImm(OffsetImm / Scale);
500
Evan Chengcc9ca352009-08-11 21:11:32 +0000501 assert((!HasShift || OffsetReg) && "Invalid so_reg load / store address!");
502
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000503 if (HasOffReg)
504 MIB.addReg(OffsetReg, getKillRegState(OffsetKill));
Evan Cheng36064672009-08-11 08:52:18 +0000505 }
Evan Cheng806845d2009-08-11 09:37:40 +0000506
Evan Cheng36064672009-08-11 08:52:18 +0000507 // Transfer the rest of operands.
Evan Cheng36064672009-08-11 08:52:18 +0000508 for (unsigned e = MI->getNumOperands(); OpNum != e; ++OpNum)
509 MIB.addOperand(MI->getOperand(OpNum));
510
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000511 // Transfer memoperands.
Chris Lattner1d0c2572011-04-29 05:24:29 +0000512 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000513
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000514 // Transfer MI flags.
515 MIB.setMIFlags(MI->getFlags());
516
Chris Lattnera6f074f2009-08-23 03:41:05 +0000517 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Cheng36064672009-08-11 08:52:18 +0000518
Evan Cheng7fae11b2011-12-14 02:11:42 +0000519 MBB.erase_instr(MI);
Evan Cheng36064672009-08-11 08:52:18 +0000520 ++NumLdSts;
521 return true;
522}
523
Evan Cheng36064672009-08-11 08:52:18 +0000524bool
525Thumb2SizeReduce::ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
526 const ReduceEntry &Entry,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000527 bool LiveCPSR, bool IsSelfLoop) {
Jim Grosbacha8a80672011-06-29 23:25:04 +0000528 unsigned Opc = MI->getOpcode();
529 if (Opc == ARM::t2ADDri) {
530 // If the source register is SP, try to reduce to tADDrSPi, otherwise
531 // it's a normal reduce.
532 if (MI->getOperand(1).getReg() != ARM::SP) {
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000533 if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
Jim Grosbacha8a80672011-06-29 23:25:04 +0000534 return true;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000535 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000536 }
537 // Try to reduce to tADDrSPi.
538 unsigned Imm = MI->getOperand(2).getImm();
539 // The immediate must be in range, the destination register must be a low
Jim Grosbached5134a2011-06-30 02:22:49 +0000540 // reg, the predicate must be "always" and the condition flags must not
541 // be being set.
Jim Grosbach68b0e842011-07-01 19:07:09 +0000542 if (Imm & 3 || Imm > 1020)
Jim Grosbacha8a80672011-06-29 23:25:04 +0000543 return false;
544 if (!isARMLowRegister(MI->getOperand(0).getReg()))
545 return false;
Jim Grosbached5134a2011-06-30 02:22:49 +0000546 if (MI->getOperand(3).getImm() != ARMCC::AL)
547 return false;
Jim Grosbacha8a80672011-06-29 23:25:04 +0000548 const MCInstrDesc &MCID = MI->getDesc();
549 if (MCID.hasOptionalDef() &&
550 MI->getOperand(MCID.getNumOperands()-1).getReg() == ARM::CPSR)
551 return false;
552
Evan Cheng7fae11b2011-12-14 02:11:42 +0000553 MachineInstrBuilder MIB = BuildMI(MBB, MI, MI->getDebugLoc(),
Jim Grosbacha8a80672011-06-29 23:25:04 +0000554 TII->get(ARM::tADDrSPi))
555 .addOperand(MI->getOperand(0))
556 .addOperand(MI->getOperand(1))
557 .addImm(Imm / 4); // The tADDrSPi has an implied scale by four.
Jim Grosbach1b8457a2011-08-24 17:46:13 +0000558 AddDefaultPred(MIB);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000559
560 // Transfer MI flags.
561 MIB.setMIFlags(MI->getFlags());
562
563 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " <<*MIB);
564
Evan Cheng7fae11b2011-12-14 02:11:42 +0000565 MBB.erase_instr(MI);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000566 ++NumNarrows;
567 return true;
568 }
569
Evan Chengcc9ca352009-08-11 21:11:32 +0000570 if (Entry.LowRegs1 && !VerifyLowRegs(MI))
Evan Cheng36064672009-08-11 08:52:18 +0000571 return false;
572
Evan Cheng7f8e5632011-12-07 07:15:52 +0000573 if (MI->mayLoad() || MI->mayStore())
Evan Cheng36064672009-08-11 08:52:18 +0000574 return ReduceLoadStore(MBB, MI, Entry);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000575
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000576 switch (Opc) {
577 default: break;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000578 case ARM::t2ADDSri:
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000579 case ARM::t2ADDSrr: {
580 unsigned PredReg = 0;
581 if (getInstrPredicate(MI, PredReg) == ARMCC::AL) {
582 switch (Opc) {
583 default: break;
584 case ARM::t2ADDSri: {
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000585 if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000586 return true;
587 // fallthrough
588 }
589 case ARM::t2ADDSrr:
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000590 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000591 }
592 }
593 break;
594 }
595 case ARM::t2RSBri:
596 case ARM::t2RSBSri:
Jim Grosbach8b31ef52011-07-27 16:47:19 +0000597 case ARM::t2SXTB:
598 case ARM::t2SXTH:
599 case ARM::t2UXTB:
600 case ARM::t2UXTH:
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000601 if (MI->getOperand(2).getImm() == 0)
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000602 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000603 break;
Anton Korobeynikov25229082009-11-24 00:44:37 +0000604 case ARM::t2MOVi16:
605 // Can convert only 'pure' immediate operands, not immediates obtained as
606 // globals' addresses.
607 if (MI->getOperand(1).isImm())
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000608 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Anton Korobeynikov25229082009-11-24 00:44:37 +0000609 break;
Jim Grosbach327cf8e2010-12-07 20:41:06 +0000610 case ARM::t2CMPrr: {
Jim Grosbach5bae0542010-12-03 23:54:18 +0000611 // Try to reduce to the lo-reg only version first. Why there are two
612 // versions of the instruction is a mystery.
613 // It would be nice to just have two entries in the master table that
614 // are prioritized, but the table assumes a unique entry for each
615 // source insn opcode. So for now, we hack a local entry record to use.
616 static const ReduceEntry NarrowEntry =
Evan Chengddc0cb62012-12-20 19:59:30 +0000617 { ARM::t2CMPrr,ARM::tCMPr, 0, 0, 0, 1, 1,2, 0, 0,1,0 };
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000618 if (ReduceToNarrow(MBB, MI, NarrowEntry, LiveCPSR, IsSelfLoop))
Jim Grosbach5bae0542010-12-03 23:54:18 +0000619 return true;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000620 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Jim Grosbach5bae0542010-12-03 23:54:18 +0000621 }
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000622 }
Evan Cheng36064672009-08-11 08:52:18 +0000623 return false;
624}
625
626bool
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000627Thumb2SizeReduce::ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
628 const ReduceEntry &Entry,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000629 bool LiveCPSR, bool IsSelfLoop) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000630
631 if (ReduceLimit2Addr != -1 && ((int)Num2Addrs >= ReduceLimit2Addr))
632 return false;
633
Evan Chengddc0cb62012-12-20 19:59:30 +0000634 if (!MinimizeSize && !OptimizeSize && Entry.AvoidMovs &&
635 STI->avoidMOVsShifterOperand())
636 // Don't issue movs with shifter operand for some CPUs unless we
637 // are optimizing / minimizing for size.
638 return false;
639
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000640 unsigned Reg0 = MI->getOperand(0).getReg();
641 unsigned Reg1 = MI->getOperand(1).getReg();
Jim Grosbachc01104d2012-02-24 00:33:36 +0000642 // t2MUL is "special". The tied source operand is second, not first.
643 if (MI->getOpcode() == ARM::t2MUL) {
Jim Grosbach3a21e2c2012-02-24 00:53:11 +0000644 unsigned Reg2 = MI->getOperand(2).getReg();
645 // Early exit if the regs aren't all low regs.
646 if (!isARMLowRegister(Reg0) || !isARMLowRegister(Reg1)
647 || !isARMLowRegister(Reg2))
648 return false;
649 if (Reg0 != Reg2) {
Jim Grosbachc01104d2012-02-24 00:33:36 +0000650 // If the other operand also isn't the same as the destination, we
651 // can't reduce.
652 if (Reg1 != Reg0)
653 return false;
654 // Try to commute the operands to make it a 2-address instruction.
655 MachineInstr *CommutedMI = TII->commuteInstruction(MI);
656 if (!CommutedMI)
657 return false;
658 }
659 } else if (Reg0 != Reg1) {
Bob Wilson279e55f2010-06-24 16:50:20 +0000660 // Try to commute the operands to make it a 2-address instruction.
661 unsigned CommOpIdx1, CommOpIdx2;
662 if (!TII->findCommutedOpIndices(MI, CommOpIdx1, CommOpIdx2) ||
663 CommOpIdx1 != 1 || MI->getOperand(CommOpIdx2).getReg() != Reg0)
664 return false;
665 MachineInstr *CommutedMI = TII->commuteInstruction(MI);
666 if (!CommutedMI)
667 return false;
668 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000669 if (Entry.LowRegs2 && !isARMLowRegister(Reg0))
670 return false;
671 if (Entry.Imm2Limit) {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000672 unsigned Imm = MI->getOperand(2).getImm();
Evan Cheng1be453b2009-08-08 03:21:23 +0000673 unsigned Limit = (1 << Entry.Imm2Limit) - 1;
674 if (Imm > Limit)
675 return false;
676 } else {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000677 unsigned Reg2 = MI->getOperand(2).getReg();
Evan Cheng1be453b2009-08-08 03:21:23 +0000678 if (Entry.LowRegs2 && !isARMLowRegister(Reg2))
679 return false;
680 }
681
Evan Cheng1f5bee12009-08-10 06:57:42 +0000682 // Check if it's possible / necessary to transfer the predicate.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000683 const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc2);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000684 unsigned PredReg = 0;
685 ARMCC::CondCodes Pred = getInstrPredicate(MI, PredReg);
686 bool SkipPred = false;
687 if (Pred != ARMCC::AL) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000688 if (!NewMCID.isPredicable())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000689 // Can't transfer predicate, fail.
690 return false;
691 } else {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000692 SkipPred = !NewMCID.isPredicable();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000693 }
694
Evan Cheng1be453b2009-08-08 03:21:23 +0000695 bool HasCC = false;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000696 bool CCDead = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000697 const MCInstrDesc &MCID = MI->getDesc();
698 if (MCID.hasOptionalDef()) {
699 unsigned NumOps = MCID.getNumOperands();
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000700 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
701 if (HasCC && MI->getOperand(NumOps-1).isDead())
702 CCDead = true;
703 }
Evan Cheng1f5bee12009-08-10 06:57:42 +0000704 if (!VerifyPredAndCC(MI, Entry, true, Pred, LiveCPSR, HasCC, CCDead))
Evan Chengd461c1c2009-08-09 19:17:19 +0000705 return false;
Evan Cheng1be453b2009-08-08 03:21:23 +0000706
Bob Wilsona2881ee2011-04-19 18:11:49 +0000707 // Avoid adding a false dependency on partial flag update by some 16-bit
708 // instructions which has the 's' bit set.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000709 if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000710 canAddPseudoFlagDep(MI, IsSelfLoop))
Bob Wilsona2881ee2011-04-19 18:11:49 +0000711 return false;
712
Evan Cheng1be453b2009-08-08 03:21:23 +0000713 // Add the 16-bit instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000714 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000715 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000716 MIB.addOperand(MI->getOperand(0));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000717 if (NewMCID.hasOptionalDef()) {
Evan Cheng6ddd7bc2009-08-15 07:59:10 +0000718 if (HasCC)
719 AddDefaultT1CC(MIB, CCDead);
720 else
721 AddNoT1CC(MIB);
722 }
Evan Chengd461c1c2009-08-09 19:17:19 +0000723
724 // Transfer the rest of operands.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000725 unsigned NumOps = MCID.getNumOperands();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000726 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000727 if (i < NumOps && MCID.OpInfo[i].isOptionalDef())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000728 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000729 if (SkipPred && MCID.OpInfo[i].isPredicate())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000730 continue;
731 MIB.addOperand(MI->getOperand(i));
732 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000733
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000734 // Transfer MI flags.
735 MIB.setMIFlags(MI->getFlags());
736
Chris Lattnera6f074f2009-08-23 03:41:05 +0000737 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Cheng1be453b2009-08-08 03:21:23 +0000738
Evan Cheng7fae11b2011-12-14 02:11:42 +0000739 MBB.erase_instr(MI);
Evan Cheng1be453b2009-08-08 03:21:23 +0000740 ++Num2Addrs;
Evan Cheng1be453b2009-08-08 03:21:23 +0000741 return true;
742}
743
744bool
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000745Thumb2SizeReduce::ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
746 const ReduceEntry &Entry,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000747 bool LiveCPSR, bool IsSelfLoop) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000748 if (ReduceLimit != -1 && ((int)NumNarrows >= ReduceLimit))
749 return false;
750
Evan Chengddc0cb62012-12-20 19:59:30 +0000751 if (!MinimizeSize && !OptimizeSize && Entry.AvoidMovs &&
752 STI->avoidMOVsShifterOperand())
753 // Don't issue movs with shifter operand for some CPUs unless we
754 // are optimizing / minimizing for size.
755 return false;
756
Evan Chengd461c1c2009-08-09 19:17:19 +0000757 unsigned Limit = ~0U;
758 if (Entry.Imm1Limit)
Jim Grosbacha8a80672011-06-29 23:25:04 +0000759 Limit = (1 << Entry.Imm1Limit) - 1;
Evan Chengd461c1c2009-08-09 19:17:19 +0000760
Evan Cheng6cc775f2011-06-28 19:10:37 +0000761 const MCInstrDesc &MCID = MI->getDesc();
762 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i) {
763 if (MCID.OpInfo[i].isPredicate())
Evan Chengd461c1c2009-08-09 19:17:19 +0000764 continue;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000765 const MachineOperand &MO = MI->getOperand(i);
Evan Chengd461c1c2009-08-09 19:17:19 +0000766 if (MO.isReg()) {
767 unsigned Reg = MO.getReg();
768 if (!Reg || Reg == ARM::CPSR)
769 continue;
770 if (Entry.LowRegs1 && !isARMLowRegister(Reg))
771 return false;
Evan Chengf6a9d062009-08-11 23:00:31 +0000772 } else if (MO.isImm() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +0000773 !MCID.OpInfo[i].isPredicate()) {
Jim Grosbacha8a80672011-06-29 23:25:04 +0000774 if (((unsigned)MO.getImm()) > Limit)
Evan Chengd461c1c2009-08-09 19:17:19 +0000775 return false;
776 }
777 }
778
Evan Cheng1f5bee12009-08-10 06:57:42 +0000779 // Check if it's possible / necessary to transfer the predicate.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000780 const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc1);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000781 unsigned PredReg = 0;
782 ARMCC::CondCodes Pred = getInstrPredicate(MI, PredReg);
783 bool SkipPred = false;
784 if (Pred != ARMCC::AL) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000785 if (!NewMCID.isPredicable())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000786 // Can't transfer predicate, fail.
787 return false;
788 } else {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000789 SkipPred = !NewMCID.isPredicable();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000790 }
791
Evan Chengd461c1c2009-08-09 19:17:19 +0000792 bool HasCC = false;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000793 bool CCDead = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000794 if (MCID.hasOptionalDef()) {
795 unsigned NumOps = MCID.getNumOperands();
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000796 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
797 if (HasCC && MI->getOperand(NumOps-1).isDead())
798 CCDead = true;
799 }
Evan Cheng1f5bee12009-08-10 06:57:42 +0000800 if (!VerifyPredAndCC(MI, Entry, false, Pred, LiveCPSR, HasCC, CCDead))
Evan Chengd461c1c2009-08-09 19:17:19 +0000801 return false;
802
Bob Wilsona2881ee2011-04-19 18:11:49 +0000803 // Avoid adding a false dependency on partial flag update by some 16-bit
804 // instructions which has the 's' bit set.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000805 if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000806 canAddPseudoFlagDep(MI, IsSelfLoop))
Bob Wilsona2881ee2011-04-19 18:11:49 +0000807 return false;
808
Evan Chengd461c1c2009-08-09 19:17:19 +0000809 // Add the 16-bit instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000810 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000811 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000812 MIB.addOperand(MI->getOperand(0));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000813 if (NewMCID.hasOptionalDef()) {
Evan Cheng6ddd7bc2009-08-15 07:59:10 +0000814 if (HasCC)
815 AddDefaultT1CC(MIB, CCDead);
816 else
817 AddNoT1CC(MIB);
818 }
Evan Chengd461c1c2009-08-09 19:17:19 +0000819
820 // Transfer the rest of operands.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000821 unsigned NumOps = MCID.getNumOperands();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000822 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000823 if (i < NumOps && MCID.OpInfo[i].isOptionalDef())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000824 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000825 if ((MCID.getOpcode() == ARM::t2RSBSri ||
Jim Grosbach8b31ef52011-07-27 16:47:19 +0000826 MCID.getOpcode() == ARM::t2RSBri ||
827 MCID.getOpcode() == ARM::t2SXTB ||
828 MCID.getOpcode() == ARM::t2SXTH ||
829 MCID.getOpcode() == ARM::t2UXTB ||
830 MCID.getOpcode() == ARM::t2UXTH) && i == 2)
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000831 // Skip the zero immediate operand, it's now implicit.
832 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000833 bool isPred = (i < NumOps && MCID.OpInfo[i].isPredicate());
Evan Chengf6a9d062009-08-11 23:00:31 +0000834 if (SkipPred && isPred)
835 continue;
836 const MachineOperand &MO = MI->getOperand(i);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000837 if (MO.isReg() && MO.isImplicit() && MO.getReg() == ARM::CPSR)
838 // Skip implicit def of CPSR. Either it's modeled as an optional
839 // def now or it's already an implicit def on the new instruction.
840 continue;
841 MIB.addOperand(MO);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000842 }
Evan Cheng6cc775f2011-06-28 19:10:37 +0000843 if (!MCID.isPredicable() && NewMCID.isPredicable())
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000844 AddDefaultPred(MIB);
Evan Chengd461c1c2009-08-09 19:17:19 +0000845
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000846 // Transfer MI flags.
847 MIB.setMIFlags(MI->getFlags());
848
Chris Lattnera6f074f2009-08-23 03:41:05 +0000849 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Chengd461c1c2009-08-09 19:17:19 +0000850
Evan Cheng7fae11b2011-12-14 02:11:42 +0000851 MBB.erase_instr(MI);
Evan Chengd461c1c2009-08-09 19:17:19 +0000852 ++NumNarrows;
853 return true;
Evan Cheng1be453b2009-08-08 03:21:23 +0000854}
855
Bob Wilsona2881ee2011-04-19 18:11:49 +0000856static bool UpdateCPSRDef(MachineInstr &MI, bool LiveCPSR, bool &DefCPSR) {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000857 bool HasDef = false;
Owen Anderson8c1f17b2014-03-07 22:48:22 +0000858 for (const MachineOperand &MO : MI.operands()) {
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000859 if (!MO.isReg() || MO.isUndef() || MO.isUse())
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000860 continue;
861 if (MO.getReg() != ARM::CPSR)
862 continue;
Bob Wilsona2881ee2011-04-19 18:11:49 +0000863
864 DefCPSR = true;
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000865 if (!MO.isDead())
866 HasDef = true;
867 }
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000868
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000869 return HasDef || LiveCPSR;
870}
871
872static bool UpdateCPSRUse(MachineInstr &MI, bool LiveCPSR) {
Owen Anderson8c1f17b2014-03-07 22:48:22 +0000873 for (const MachineOperand &MO : MI.operands()) {
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000874 if (!MO.isReg() || MO.isUndef() || MO.isDef())
875 continue;
876 if (MO.getReg() != ARM::CPSR)
877 continue;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000878 assert(LiveCPSR && "CPSR liveness tracking is wrong!");
879 if (MO.isKill()) {
880 LiveCPSR = false;
881 break;
882 }
883 }
884
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000885 return LiveCPSR;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000886}
887
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000888bool Thumb2SizeReduce::ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000889 bool LiveCPSR, bool IsSelfLoop) {
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000890 unsigned Opcode = MI->getOpcode();
891 DenseMap<unsigned, unsigned>::iterator OPI = ReduceOpcodeMap.find(Opcode);
892 if (OPI == ReduceOpcodeMap.end())
893 return false;
894 const ReduceEntry &Entry = ReduceTable[OPI->second];
895
896 // Don't attempt normal reductions on "special" cases for now.
897 if (Entry.Special)
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000898 return ReduceSpecial(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000899
900 // Try to transform to a 16-bit two-address instruction.
901 if (Entry.NarrowOpc2 &&
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000902 ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000903 return true;
904
905 // Try to transform to a 16-bit non-two-address instruction.
906 if (Entry.NarrowOpc1 &&
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000907 ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000908 return true;
909
910 return false;
911}
912
Evan Cheng1be453b2009-08-08 03:21:23 +0000913bool Thumb2SizeReduce::ReduceMBB(MachineBasicBlock &MBB) {
914 bool Modified = false;
915
Evan Cheng1f5bee12009-08-10 06:57:42 +0000916 // Yes, CPSR could be livein.
Dan Gohmana1cf9fe2010-04-13 16:53:51 +0000917 bool LiveCPSR = MBB.isLiveIn(ARM::CPSR);
Craig Topper062a2ba2014-04-25 05:30:21 +0000918 MachineInstr *BundleMI = nullptr;
Evan Cheng1f5bee12009-08-10 06:57:42 +0000919
Craig Topper062a2ba2014-04-25 05:30:21 +0000920 CPSRDef = nullptr;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000921 HighLatencyCPSR = false;
922
923 // Check predecessors for the latest CPSRDef.
Jim Grosbach537f3ed2014-04-04 02:11:03 +0000924 for (auto *Pred : MBB.predecessors()) {
925 const MBBInfo &PInfo = BlockInfo[Pred->getNumber()];
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000926 if (!PInfo.Visited) {
927 // Since blocks are visited in RPO, this must be a back-edge.
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000928 continue;
929 }
930 if (PInfo.HighLatencyCPSR) {
931 HighLatencyCPSR = true;
932 break;
933 }
934 }
935
Evan Chengf4807a12011-10-27 21:21:05 +0000936 // If this BB loops back to itself, conservatively avoid narrowing the
937 // first instruction that does partial flag update.
938 bool IsSelfLoop = MBB.isSuccessor(&MBB);
Jim Grosbach0c509fa2012-04-06 23:43:50 +0000939 MachineBasicBlock::instr_iterator MII = MBB.instr_begin(),E = MBB.instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000940 MachineBasicBlock::instr_iterator NextMII;
Evan Cheng1be453b2009-08-08 03:21:23 +0000941 for (; MII != E; MII = NextMII) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000942 NextMII = std::next(MII);
Evan Cheng1be453b2009-08-08 03:21:23 +0000943
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000944 MachineInstr *MI = &*MII;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000945 if (MI->isBundle()) {
946 BundleMI = MI;
947 continue;
948 }
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000949 if (MI->isDebugValue())
950 continue;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000951
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000952 LiveCPSR = UpdateCPSRUse(*MI, LiveCPSR);
953
Jakob Stoklund Olesen41bbf9c2012-12-18 00:46:39 +0000954 // Does NextMII belong to the same bundle as MI?
955 bool NextInSameBundle = NextMII != E && NextMII->isBundledWithPred();
956
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000957 if (ReduceMI(MBB, MI, LiveCPSR, IsSelfLoop)) {
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000958 Modified = true;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000959 MachineBasicBlock::instr_iterator I = std::prev(NextMII);
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000960 MI = &*I;
Jakob Stoklund Olesen41bbf9c2012-12-18 00:46:39 +0000961 // Removing and reinserting the first instruction in a bundle will break
962 // up the bundle. Fix the bundling if it was broken.
963 if (NextInSameBundle && !NextMII->isBundledWithPred())
964 NextMII->bundleWithPred();
Evan Cheng1be453b2009-08-08 03:21:23 +0000965 }
966
Jakob Stoklund Olesen41bbf9c2012-12-18 00:46:39 +0000967 if (!NextInSameBundle && MI->isInsideBundle()) {
Evan Cheng7fae11b2011-12-14 02:11:42 +0000968 // FIXME: Since post-ra scheduler operates on bundles, the CPSR kill
969 // marker is only on the BUNDLE instruction. Process the BUNDLE
970 // instruction as we finish with the bundled instruction to work around
971 // the inconsistency.
Evan Cheng903231b2011-12-17 01:25:34 +0000972 if (BundleMI->killsRegister(ARM::CPSR))
973 LiveCPSR = false;
974 MachineOperand *MO = BundleMI->findRegisterDefOperand(ARM::CPSR);
975 if (MO && !MO->isDead())
976 LiveCPSR = true;
Weiming Zhaof66be562014-01-13 18:47:54 +0000977 MO = BundleMI->findRegisterUseOperand(ARM::CPSR);
978 if (MO && !MO->isKill())
979 LiveCPSR = true;
Evan Cheng903231b2011-12-17 01:25:34 +0000980 }
Evan Cheng7fae11b2011-12-14 02:11:42 +0000981
Bob Wilsona2881ee2011-04-19 18:11:49 +0000982 bool DefCPSR = false;
983 LiveCPSR = UpdateCPSRDef(*MI, LiveCPSR, DefCPSR);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000984 if (MI->isCall()) {
Bob Wilsona2881ee2011-04-19 18:11:49 +0000985 // Calls don't really set CPSR.
Craig Topper062a2ba2014-04-25 05:30:21 +0000986 CPSRDef = nullptr;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000987 HighLatencyCPSR = false;
Evan Chengf4807a12011-10-27 21:21:05 +0000988 IsSelfLoop = false;
989 } else if (DefCPSR) {
Bob Wilsona2881ee2011-04-19 18:11:49 +0000990 // This is the last CPSR defining instruction.
991 CPSRDef = MI;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000992 HighLatencyCPSR = isHighLatencyCPSR(CPSRDef);
Evan Chengf4807a12011-10-27 21:21:05 +0000993 IsSelfLoop = false;
994 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000995 }
996
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000997 MBBInfo &Info = BlockInfo[MBB.getNumber()];
998 Info.HighLatencyCPSR = HighLatencyCPSR;
999 Info.Visited = true;
Evan Cheng1be453b2009-08-08 03:21:23 +00001000 return Modified;
1001}
1002
1003bool Thumb2SizeReduce::runOnMachineFunction(MachineFunction &MF) {
Eric Christopher1b21f002015-01-29 00:19:33 +00001004 STI = &static_cast<const ARMSubtarget &>(MF.getSubtarget());
Eric Christopher63b44882015-03-05 00:23:40 +00001005 if (STI->isThumb1Only() || STI->prefers32BitThumb())
1006 return false;
1007
Eric Christopher1b21f002015-01-29 00:19:33 +00001008 TII = static_cast<const Thumb2InstrInfo *>(STI->getInstrInfo());
Evan Cheng1be453b2009-08-08 03:21:23 +00001009
Evan Chengddc0cb62012-12-20 19:59:30 +00001010 // Optimizing / minimizing size?
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00001011 OptimizeSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
1012 MinimizeSize = MF.getFunction()->hasFnAttribute(Attribute::MinSize);
Quentin Colombet23b404d2012-12-18 22:47:16 +00001013
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001014 BlockInfo.clear();
1015 BlockInfo.resize(MF.getNumBlockIDs());
1016
1017 // Visit blocks in reverse post-order so LastCPSRDef is known for all
1018 // predecessors.
1019 ReversePostOrderTraversal<MachineFunction*> RPOT(&MF);
Evan Cheng1be453b2009-08-08 03:21:23 +00001020 bool Modified = false;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001021 for (ReversePostOrderTraversal<MachineFunction*>::rpo_iterator
1022 I = RPOT.begin(), E = RPOT.end(); I != E; ++I)
1023 Modified |= ReduceMBB(**I);
Evan Cheng1be453b2009-08-08 03:21:23 +00001024 return Modified;
1025}
1026
1027/// createThumb2SizeReductionPass - Returns an instance of the Thumb2 size
1028/// reduction pass.
1029FunctionPass *llvm::createThumb2SizeReductionPass() {
1030 return new Thumb2SizeReduce();
1031}