blob: 9c2229916bad1c6091c5d046e278e54ee8e710f9 [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"
Benjamin Kramer16132e62015-03-23 18:07:13 +000024#include "llvm/Support/raw_ostream.h"
Craig Toppera9253262014-03-22 23:51:00 +000025#include "llvm/Target/TargetMachine.h"
Evan Cheng1be453b2009-08-08 03:21:23 +000026using namespace llvm;
27
Chandler Carruth84e68b22014-04-22 02:41:26 +000028#define DEBUG_TYPE "t2-reduce-size"
29
Evan Cheng1f5bee12009-08-10 06:57:42 +000030STATISTIC(NumNarrows, "Number of 32-bit instrs reduced to 16-bit ones");
31STATISTIC(Num2Addrs, "Number of 32-bit instrs reduced to 2addr 16-bit ones");
Evan Cheng36064672009-08-11 08:52:18 +000032STATISTIC(NumLdSts, "Number of 32-bit load / store reduced to 16-bit ones");
Evan Cheng1be453b2009-08-08 03:21:23 +000033
Evan Chengcc9ca352009-08-11 21:11:32 +000034static cl::opt<int> ReduceLimit("t2-reduce-limit",
35 cl::init(-1), cl::Hidden);
36static cl::opt<int> ReduceLimit2Addr("t2-reduce-limit2",
37 cl::init(-1), cl::Hidden);
38static cl::opt<int> ReduceLimitLdSt("t2-reduce-limit3",
39 cl::init(-1), cl::Hidden);
Evan Chengf16a1d52009-08-10 07:20:37 +000040
Evan Cheng1be453b2009-08-08 03:21:23 +000041namespace {
42 /// ReduceTable - A static table with information on mapping from wide
43 /// opcodes to narrow
44 struct ReduceEntry {
Craig Topperca658c22012-03-11 07:16:55 +000045 uint16_t WideOpc; // Wide opcode
46 uint16_t NarrowOpc1; // Narrow opcode to transform to
47 uint16_t NarrowOpc2; // Narrow opcode when it's two-address
Evan Cheng1be453b2009-08-08 03:21:23 +000048 uint8_t Imm1Limit; // Limit of immediate field (bits)
49 uint8_t Imm2Limit; // Limit of immediate field when it's two-address
50 unsigned LowRegs1 : 1; // Only possible if low-registers are used
51 unsigned LowRegs2 : 1; // Only possible if low-registers are used (2addr)
Evan Cheng1e6c2a12009-08-12 01:49:45 +000052 unsigned PredCC1 : 2; // 0 - If predicated, cc is on and vice versa.
Evan Cheng1be453b2009-08-08 03:21:23 +000053 // 1 - No cc field.
Evan Cheng1e6c2a12009-08-12 01:49:45 +000054 // 2 - Always set CPSR.
Evan Chengaee7e492009-08-12 18:35:50 +000055 unsigned PredCC2 : 2;
Bob Wilsona2881ee2011-04-19 18:11:49 +000056 unsigned PartFlag : 1; // 16-bit instruction does partial flag update
Evan Cheng1be453b2009-08-08 03:21:23 +000057 unsigned Special : 1; // Needs to be dealt with specially
Evan Chengddc0cb62012-12-20 19:59:30 +000058 unsigned AvoidMovs: 1; // Avoid movs with shifter operand (for Swift)
Evan Cheng1be453b2009-08-08 03:21:23 +000059 };
60
61 static const ReduceEntry ReduceTable[] = {
Evan Chengddc0cb62012-12-20 19:59:30 +000062 // Wide, Narrow1, Narrow2, imm1,imm2, lo1, lo2, P/C,PF,S,AM
63 { ARM::t2ADCrr, 0, ARM::tADC, 0, 0, 0, 1, 0,0, 0,0,0 },
64 { ARM::t2ADDri, ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 0,0, 0,1,0 },
65 { ARM::t2ADDrr, ARM::tADDrr, ARM::tADDhirr, 0, 0, 1, 0, 0,1, 0,0,0 },
66 { ARM::t2ADDSri,ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 2,2, 0,1,0 },
67 { ARM::t2ADDSrr,ARM::tADDrr, 0, 0, 0, 1, 0, 2,0, 0,1,0 },
68 { ARM::t2ANDrr, 0, ARM::tAND, 0, 0, 0, 1, 0,0, 1,0,0 },
69 { ARM::t2ASRri, ARM::tASRri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
70 { ARM::t2ASRrr, 0, ARM::tASRrr, 0, 0, 0, 1, 0,0, 1,0,1 },
71 { ARM::t2BICrr, 0, ARM::tBIC, 0, 0, 0, 1, 0,0, 1,0,0 },
72 //FIXME: Disable CMN, as CCodes are backwards from compare expectations
73 //{ ARM::t2CMNrr, ARM::tCMN, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
74 { ARM::t2CMNzrr, ARM::tCMNz, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
75 { ARM::t2CMPri, ARM::tCMPi8, 0, 8, 0, 1, 0, 2,0, 0,0,0 },
76 { ARM::t2CMPrr, ARM::tCMPhir, 0, 0, 0, 0, 0, 2,0, 0,1,0 },
77 { ARM::t2EORrr, 0, ARM::tEOR, 0, 0, 0, 1, 0,0, 1,0,0 },
78 // FIXME: adr.n immediate offset must be multiple of 4.
79 //{ ARM::t2LEApcrelJT,ARM::tLEApcrelJT, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
80 { ARM::t2LSLri, ARM::tLSLri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
81 { ARM::t2LSLrr, 0, ARM::tLSLrr, 0, 0, 0, 1, 0,0, 1,0,1 },
82 { ARM::t2LSRri, ARM::tLSRri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
83 { ARM::t2LSRrr, 0, ARM::tLSRrr, 0, 0, 0, 1, 0,0, 1,0,1 },
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +000084 { ARM::t2MOVi, ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 1,0,0 },
85 { ARM::t2MOVi16,ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 1,1,0 },
Evan Chengddc0cb62012-12-20 19:59:30 +000086 // FIXME: Do we need the 16-bit 'S' variant?
87 { ARM::t2MOVr,ARM::tMOVr, 0, 0, 0, 0, 0, 1,0, 0,0,0 },
88 { ARM::t2MUL, 0, ARM::tMUL, 0, 0, 0, 1, 0,0, 1,0,0 },
89 { ARM::t2MVNr, ARM::tMVN, 0, 0, 0, 1, 0, 0,0, 0,0,0 },
90 { ARM::t2ORRrr, 0, ARM::tORR, 0, 0, 0, 1, 0,0, 1,0,0 },
91 { ARM::t2REV, ARM::tREV, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
92 { ARM::t2REV16, ARM::tREV16, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
93 { ARM::t2REVSH, ARM::tREVSH, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
94 { ARM::t2RORrr, 0, ARM::tROR, 0, 0, 0, 1, 0,0, 1,0,0 },
95 { ARM::t2RSBri, ARM::tRSB, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
96 { ARM::t2RSBSri,ARM::tRSB, 0, 0, 0, 1, 0, 2,0, 0,1,0 },
97 { ARM::t2SBCrr, 0, ARM::tSBC, 0, 0, 0, 1, 0,0, 0,0,0 },
98 { ARM::t2SUBri, ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 0,0, 0,0,0 },
99 { ARM::t2SUBrr, ARM::tSUBrr, 0, 0, 0, 1, 0, 0,0, 0,0,0 },
100 { ARM::t2SUBSri,ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 2,2, 0,0,0 },
101 { ARM::t2SUBSrr,ARM::tSUBrr, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
102 { ARM::t2SXTB, ARM::tSXTB, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
103 { ARM::t2SXTH, ARM::tSXTH, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
104 { ARM::t2TSTrr, ARM::tTST, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
105 { ARM::t2UXTB, ARM::tUXTB, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
106 { ARM::t2UXTH, ARM::tUXTH, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
Evan Cheng36064672009-08-11 08:52:18 +0000107
Evan Chengddc0cb62012-12-20 19:59:30 +0000108 // FIXME: Clean this up after splitting each Thumb load / store opcode
109 // into multiple ones.
110 { ARM::t2LDRi12,ARM::tLDRi, ARM::tLDRspi, 5, 8, 1, 0, 0,0, 0,1,0 },
111 { ARM::t2LDRs, ARM::tLDRr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
112 { ARM::t2LDRBi12,ARM::tLDRBi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
113 { ARM::t2LDRBs, ARM::tLDRBr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
114 { ARM::t2LDRHi12,ARM::tLDRHi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
115 { ARM::t2LDRHs, ARM::tLDRHr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
116 { ARM::t2LDRSBs,ARM::tLDRSB, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
117 { ARM::t2LDRSHs,ARM::tLDRSH, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
118 { ARM::t2STRi12,ARM::tSTRi, ARM::tSTRspi, 5, 8, 1, 0, 0,0, 0,1,0 },
119 { ARM::t2STRs, ARM::tSTRr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
120 { ARM::t2STRBi12,ARM::tSTRBi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
121 { ARM::t2STRBs, ARM::tSTRBr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
122 { ARM::t2STRHi12,ARM::tSTRHi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
123 { ARM::t2STRHs, ARM::tSTRHr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
Evan Chengcc9ca352009-08-11 21:11:32 +0000124
Evan Chengddc0cb62012-12-20 19:59:30 +0000125 { ARM::t2LDMIA, ARM::tLDMIA, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
126 { ARM::t2LDMIA_RET,0, ARM::tPOP_RET, 0, 0, 1, 1, 1,1, 0,1,0 },
127 { ARM::t2LDMIA_UPD,ARM::tLDMIA_UPD,ARM::tPOP,0, 0, 1, 1, 1,1, 0,1,0 },
Scott Douglass953f9082015-10-05 14:49:54 +0000128 // ARM::t2STMIA (with no basereg writeback) has no Thumb1 equivalent.
129 // tSTMIA_UPD is a change in semantics which can only be used if the base
130 // register is killed. This difference is correctly handled elsewhere.
131 { ARM::t2STMIA, ARM::tSTMIA_UPD, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
Evan Chengddc0cb62012-12-20 19:59:30 +0000132 { ARM::t2STMIA_UPD,ARM::tSTMIA_UPD, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
133 { ARM::t2STMDB_UPD, 0, ARM::tPUSH, 0, 0, 1, 1, 1,1, 0,1,0 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000134 };
135
Nick Lewycky02d5f772009-10-25 06:33:48 +0000136 class Thumb2SizeReduce : public MachineFunctionPass {
Evan Cheng1be453b2009-08-08 03:21:23 +0000137 public:
138 static char ID;
Akira Hatanaka4a616192015-06-08 18:50:43 +0000139 Thumb2SizeReduce(std::function<bool(const Function &)> Ftor);
Evan Cheng1be453b2009-08-08 03:21:23 +0000140
Evan Cheng6ddd7bc2009-08-15 07:59:10 +0000141 const Thumb2InstrInfo *TII;
Bob Wilsona2881ee2011-04-19 18:11:49 +0000142 const ARMSubtarget *STI;
Evan Cheng1be453b2009-08-08 03:21:23 +0000143
Craig Topper6bc27bf2014-03-10 02:09:33 +0000144 bool runOnMachineFunction(MachineFunction &MF) override;
Evan Cheng1be453b2009-08-08 03:21:23 +0000145
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000146 MachineFunctionProperties getRequiredProperties() const override {
147 return MachineFunctionProperties().set(
148 MachineFunctionProperties::Property::AllVRegsAllocated);
149 }
150
Craig Topper6bc27bf2014-03-10 02:09:33 +0000151 const char *getPassName() const override {
Evan Cheng1be453b2009-08-08 03:21:23 +0000152 return "Thumb2 instruction size reduction pass";
153 }
154
155 private:
156 /// ReduceOpcodeMap - Maps wide opcode to index of entry in ReduceTable.
157 DenseMap<unsigned, unsigned> ReduceOpcodeMap;
158
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000159 bool canAddPseudoFlagDep(MachineInstr *Use, bool IsSelfLoop);
Bob Wilsona2881ee2011-04-19 18:11:49 +0000160
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000161 bool VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
162 bool is2Addr, ARMCC::CondCodes Pred,
163 bool LiveCPSR, bool &HasCC, bool &CCDead);
164
Evan Cheng36064672009-08-11 08:52:18 +0000165 bool ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
166 const ReduceEntry &Entry);
167
168 bool ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000169 const ReduceEntry &Entry, bool LiveCPSR, bool IsSelfLoop);
Evan Cheng36064672009-08-11 08:52:18 +0000170
Evan Cheng1be453b2009-08-08 03:21:23 +0000171 /// ReduceTo2Addr - Reduce a 32-bit instruction to a 16-bit two-address
172 /// instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000173 bool ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000174 const ReduceEntry &Entry, bool LiveCPSR,
Evan Chengf4807a12011-10-27 21:21:05 +0000175 bool IsSelfLoop);
Evan Cheng1be453b2009-08-08 03:21:23 +0000176
177 /// ReduceToNarrow - Reduce a 32-bit instruction to a 16-bit
178 /// non-two-address instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000179 bool ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000180 const ReduceEntry &Entry, bool LiveCPSR,
Evan Chengf4807a12011-10-27 21:21:05 +0000181 bool IsSelfLoop);
Evan Cheng1be453b2009-08-08 03:21:23 +0000182
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000183 /// ReduceMI - Attempt to reduce MI, return true on success.
184 bool ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000185 bool LiveCPSR, bool IsSelfLoop);
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000186
Evan Cheng1be453b2009-08-08 03:21:23 +0000187 /// ReduceMBB - Reduce width of instructions in the specified basic block.
188 bool ReduceMBB(MachineBasicBlock &MBB);
Quentin Colombet23b404d2012-12-18 22:47:16 +0000189
Evan Chengddc0cb62012-12-20 19:59:30 +0000190 bool OptimizeSize;
Quentin Colombet23b404d2012-12-18 22:47:16 +0000191 bool MinimizeSize;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000192
193 // Last instruction to define CPSR in the current block.
194 MachineInstr *CPSRDef;
195 // Was CPSR last defined by a high latency instruction?
196 // When CPSRDef is null, this refers to CPSR defs in predecessors.
197 bool HighLatencyCPSR;
198
199 struct MBBInfo {
200 // The flags leaving this block have high latency.
201 bool HighLatencyCPSR;
202 // Has this block been visited yet?
203 bool Visited;
204
205 MBBInfo() : HighLatencyCPSR(false), Visited(false) {}
206 };
207
208 SmallVector<MBBInfo, 8> BlockInfo;
Akira Hatanaka4a616192015-06-08 18:50:43 +0000209
210 std::function<bool(const Function &)> PredicateFtor;
Evan Cheng1be453b2009-08-08 03:21:23 +0000211 };
212 char Thumb2SizeReduce::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000213}
Evan Cheng1be453b2009-08-08 03:21:23 +0000214
Akira Hatanaka4a616192015-06-08 18:50:43 +0000215Thumb2SizeReduce::Thumb2SizeReduce(std::function<bool(const Function &)> Ftor)
216 : MachineFunctionPass(ID), PredicateFtor(Ftor) {
Evan Chengddc0cb62012-12-20 19:59:30 +0000217 OptimizeSize = MinimizeSize = false;
Evan Cheng1be453b2009-08-08 03:21:23 +0000218 for (unsigned i = 0, e = array_lengthof(ReduceTable); i != e; ++i) {
219 unsigned FromOpc = ReduceTable[i].WideOpc;
220 if (!ReduceOpcodeMap.insert(std::make_pair(FromOpc, i)).second)
Benjamin Kramer8ceb3232015-10-25 22:28:27 +0000221 llvm_unreachable("Duplicated entries?");
Evan Cheng1be453b2009-08-08 03:21:23 +0000222 }
223}
224
Evan Cheng6cc775f2011-06-28 19:10:37 +0000225static bool HasImplicitCPSRDef(const MCInstrDesc &MCID) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000226 for (const MCPhysReg *Regs = MCID.getImplicitDefs(); *Regs; ++Regs)
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000227 if (*Regs == ARM::CPSR)
228 return true;
229 return false;
230}
231
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000232// Check for a likely high-latency flag def.
233static bool isHighLatencyCPSR(MachineInstr *Def) {
234 switch(Def->getOpcode()) {
235 case ARM::FMSTAT:
236 case ARM::tMUL:
237 return true;
238 }
239 return false;
240}
241
Bob Wilsona2881ee2011-04-19 18:11:49 +0000242/// canAddPseudoFlagDep - For A9 (and other out-of-order) implementations,
243/// the 's' 16-bit instruction partially update CPSR. Abort the
244/// transformation to avoid adding false dependency on last CPSR setting
245/// instruction which hurts the ability for out-of-order execution engine
246/// to do register renaming magic.
247/// This function checks if there is a read-of-write dependency between the
248/// last instruction that defines the CPSR and the current instruction. If there
249/// is, then there is no harm done since the instruction cannot be retired
250/// before the CPSR setting instruction anyway.
251/// Note, we are not doing full dependency analysis here for the sake of compile
252/// time. We're not looking for cases like:
253/// r0 = muls ...
254/// r1 = add.w r0, ...
255/// ...
256/// = mul.w r1
257/// In this case it would have been ok to narrow the mul.w to muls since there
258/// are indirect RAW dependency between the muls and the mul.w
259bool
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000260Thumb2SizeReduce::canAddPseudoFlagDep(MachineInstr *Use, bool FirstInSelfLoop) {
Quentin Colombet23b404d2012-12-18 22:47:16 +0000261 // Disable the check for -Oz (aka OptimizeForSizeHarder).
262 if (MinimizeSize || !STI->avoidCPSRPartialUpdate())
Bob Wilsona2881ee2011-04-19 18:11:49 +0000263 return false;
264
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000265 if (!CPSRDef)
Evan Chengf4807a12011-10-27 21:21:05 +0000266 // If this BB loops back to itself, conservatively avoid narrowing the
267 // first instruction that does partial flag update.
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000268 return HighLatencyCPSR || FirstInSelfLoop;
Evan Chengf4807a12011-10-27 21:21:05 +0000269
Bob Wilsona2881ee2011-04-19 18:11:49 +0000270 SmallSet<unsigned, 2> Defs;
Owen Anderson8c1f17b2014-03-07 22:48:22 +0000271 for (const MachineOperand &MO : CPSRDef->operands()) {
Bob Wilsona2881ee2011-04-19 18:11:49 +0000272 if (!MO.isReg() || MO.isUndef() || MO.isUse())
273 continue;
274 unsigned Reg = MO.getReg();
275 if (Reg == 0 || Reg == ARM::CPSR)
276 continue;
277 Defs.insert(Reg);
278 }
279
Owen Anderson8c1f17b2014-03-07 22:48:22 +0000280 for (const MachineOperand &MO : Use->operands()) {
Bob Wilsona2881ee2011-04-19 18:11:49 +0000281 if (!MO.isReg() || MO.isUndef() || MO.isDef())
282 continue;
283 unsigned Reg = MO.getReg();
284 if (Defs.count(Reg))
285 return false;
286 }
287
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000288 // If the current CPSR has high latency, try to avoid the false dependency.
289 if (HighLatencyCPSR)
290 return true;
291
292 // tMOVi8 usually doesn't start long dependency chains, and there are a lot
293 // of them, so always shrink them when CPSR doesn't have high latency.
294 if (Use->getOpcode() == ARM::t2MOVi ||
295 Use->getOpcode() == ARM::t2MOVi16)
296 return false;
297
Bob Wilsona2881ee2011-04-19 18:11:49 +0000298 // No read-after-write dependency. The narrowing will add false dependency.
299 return true;
300}
301
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000302bool
303Thumb2SizeReduce::VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
304 bool is2Addr, ARMCC::CondCodes Pred,
305 bool LiveCPSR, bool &HasCC, bool &CCDead) {
Evan Chengd461c1c2009-08-09 19:17:19 +0000306 if ((is2Addr && Entry.PredCC2 == 0) ||
307 (!is2Addr && Entry.PredCC1 == 0)) {
308 if (Pred == ARMCC::AL) {
309 // Not predicated, must set CPSR.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000310 if (!HasCC) {
311 // Original instruction was not setting CPSR, but CPSR is not
312 // currently live anyway. It's ok to set it. The CPSR def is
313 // dead though.
314 if (!LiveCPSR) {
315 HasCC = true;
316 CCDead = true;
317 return true;
318 }
319 return false;
320 }
Evan Chengd461c1c2009-08-09 19:17:19 +0000321 } else {
322 // Predicated, must not set CPSR.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000323 if (HasCC)
324 return false;
Evan Chengd461c1c2009-08-09 19:17:19 +0000325 }
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000326 } else if ((is2Addr && Entry.PredCC2 == 2) ||
327 (!is2Addr && Entry.PredCC1 == 2)) {
328 /// Old opcode has an optional def of CPSR.
329 if (HasCC)
330 return true;
Jim Grosbachbc7eeaf2010-09-14 20:35:46 +0000331 // If old opcode does not implicitly define CPSR, then it's not ok since
332 // these new opcodes' CPSR def is not meant to be thrown away. e.g. CMP.
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000333 if (!HasImplicitCPSRDef(MI->getDesc()))
334 return false;
335 HasCC = true;
Evan Chengd461c1c2009-08-09 19:17:19 +0000336 } else {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000337 // 16-bit instruction does not set CPSR.
338 if (HasCC)
339 return false;
Evan Chengd461c1c2009-08-09 19:17:19 +0000340 }
341
342 return true;
343}
344
Evan Chengcc9ca352009-08-11 21:11:32 +0000345static bool VerifyLowRegs(MachineInstr *MI) {
346 unsigned Opc = MI->getOpcode();
Peter Collingbourne85a0e232015-05-05 20:07:10 +0000347 bool isPCOk = (Opc == ARM::t2LDMIA_RET || Opc == ARM::t2LDMIA_UPD);
Tim Northoverba1d7042014-09-10 12:53:28 +0000348 bool isLROk = (Opc == ARM::t2STMDB_UPD);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000349 bool isSPOk = isPCOk || isLROk;
Evan Chengcc9ca352009-08-11 21:11:32 +0000350 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
351 const MachineOperand &MO = MI->getOperand(i);
352 if (!MO.isReg() || MO.isImplicit())
353 continue;
354 unsigned Reg = MO.getReg();
355 if (Reg == 0 || Reg == ARM::CPSR)
356 continue;
357 if (isPCOk && Reg == ARM::PC)
358 continue;
359 if (isLROk && Reg == ARM::LR)
360 continue;
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000361 if (Reg == ARM::SP) {
362 if (isSPOk)
363 continue;
364 if (i == 1 && (Opc == ARM::t2LDRi12 || Opc == ARM::t2STRi12))
365 // Special case for these ldr / str with sp as base register.
366 continue;
367 }
Evan Chengcc9ca352009-08-11 21:11:32 +0000368 if (!isARMLowRegister(Reg))
369 return false;
370 }
371 return true;
372}
373
Evan Cheng1be453b2009-08-08 03:21:23 +0000374bool
Evan Cheng36064672009-08-11 08:52:18 +0000375Thumb2SizeReduce::ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
376 const ReduceEntry &Entry) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000377 if (ReduceLimitLdSt != -1 && ((int)NumLdSts >= ReduceLimitLdSt))
378 return false;
379
Evan Cheng36064672009-08-11 08:52:18 +0000380 unsigned Scale = 1;
381 bool HasImmOffset = false;
382 bool HasShift = false;
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000383 bool HasOffReg = true;
Evan Chengcc9ca352009-08-11 21:11:32 +0000384 bool isLdStMul = false;
Evan Chengcc9ca352009-08-11 21:11:32 +0000385 unsigned Opc = Entry.NarrowOpc1;
386 unsigned OpNum = 3; // First 'rest' of operands.
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000387 uint8_t ImmLimit = Entry.Imm1Limit;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000388
Evan Cheng36064672009-08-11 08:52:18 +0000389 switch (Entry.WideOpc) {
390 default:
391 llvm_unreachable("Unexpected Thumb2 load / store opcode!");
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000392 case ARM::t2LDRi12:
Bill Wendling092a7bd2010-12-14 03:36:38 +0000393 case ARM::t2STRi12:
394 if (MI->getOperand(1).getReg() == ARM::SP) {
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000395 Opc = Entry.NarrowOpc2;
396 ImmLimit = Entry.Imm2Limit;
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000397 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000398
Evan Cheng36064672009-08-11 08:52:18 +0000399 Scale = 4;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000400 HasImmOffset = true;
401 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000402 break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000403 case ARM::t2LDRBi12:
Evan Cheng36064672009-08-11 08:52:18 +0000404 case ARM::t2STRBi12:
Owen Anderson4ebf4712011-02-08 22:39:40 +0000405 HasImmOffset = true;
406 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000407 break;
408 case ARM::t2LDRHi12:
409 case ARM::t2STRHi12:
410 Scale = 2;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000411 HasImmOffset = true;
412 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000413 break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000414 case ARM::t2LDRs:
415 case ARM::t2LDRBs:
416 case ARM::t2LDRHs:
Evan Cheng36064672009-08-11 08:52:18 +0000417 case ARM::t2LDRSBs:
418 case ARM::t2LDRSHs:
419 case ARM::t2STRs:
420 case ARM::t2STRBs:
421 case ARM::t2STRHs:
422 HasShift = true;
Evan Chengcc9ca352009-08-11 21:11:32 +0000423 OpNum = 4;
Evan Cheng36064672009-08-11 08:52:18 +0000424 break;
Peter Collingbourne85a0e232015-05-05 20:07:10 +0000425 case ARM::t2LDMIA: {
Evan Chengcc9ca352009-08-11 21:11:32 +0000426 unsigned BaseReg = MI->getOperand(0).getReg();
Peter Collingbourne85a0e232015-05-05 20:07:10 +0000427 assert(isARMLowRegister(BaseReg));
Bill Wendling092a7bd2010-12-14 03:36:38 +0000428
Jim Grosbach88628e92010-09-07 22:30:53 +0000429 // For the non-writeback version (this one), the base register must be
430 // one of the registers being loaded.
431 bool isOK = false;
Peter Collingbourne85a0e232015-05-05 20:07:10 +0000432 for (unsigned i = 3; i < MI->getNumOperands(); ++i) {
Jim Grosbach88628e92010-09-07 22:30:53 +0000433 if (MI->getOperand(i).getReg() == BaseReg) {
434 isOK = true;
435 break;
436 }
437 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000438
Jim Grosbach88628e92010-09-07 22:30:53 +0000439 if (!isOK)
440 return false;
441
Bob Wilson947f04b2010-03-13 01:08:20 +0000442 OpNum = 0;
443 isLdStMul = true;
444 break;
445 }
Scott Douglass953f9082015-10-05 14:49:54 +0000446 case ARM::t2STMIA: {
447 // If the base register is killed, we don't care what its value is after the
448 // instruction, so we can use an updating STMIA.
449 if (!MI->getOperand(0).isKill())
450 return false;
451
452 break;
453 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000454 case ARM::t2LDMIA_RET: {
Bob Wilson947f04b2010-03-13 01:08:20 +0000455 unsigned BaseReg = MI->getOperand(1).getReg();
456 if (BaseReg != ARM::SP)
457 return false;
458 Opc = Entry.NarrowOpc2; // tPOP_RET
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000459 OpNum = 2;
Bob Wilson947f04b2010-03-13 01:08:20 +0000460 isLdStMul = true;
461 break;
462 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000463 case ARM::t2LDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000464 case ARM::t2STMIA_UPD:
465 case ARM::t2STMDB_UPD: {
Bob Wilson947f04b2010-03-13 01:08:20 +0000466 OpNum = 0;
Bill Wendling092a7bd2010-12-14 03:36:38 +0000467
Bob Wilson947f04b2010-03-13 01:08:20 +0000468 unsigned BaseReg = MI->getOperand(1).getReg();
Bob Wilson947f04b2010-03-13 01:08:20 +0000469 if (BaseReg == ARM::SP &&
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000470 (Entry.WideOpc == ARM::t2LDMIA_UPD ||
471 Entry.WideOpc == ARM::t2STMDB_UPD)) {
Bob Wilson947f04b2010-03-13 01:08:20 +0000472 Opc = Entry.NarrowOpc2; // tPOP or tPUSH
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000473 OpNum = 2;
474 } else if (!isARMLowRegister(BaseReg) ||
475 (Entry.WideOpc != ARM::t2LDMIA_UPD &&
476 Entry.WideOpc != ARM::t2STMIA_UPD)) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000477 return false;
478 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000479
Evan Chengcc9ca352009-08-11 21:11:32 +0000480 isLdStMul = true;
481 break;
482 }
Evan Cheng36064672009-08-11 08:52:18 +0000483 }
484
485 unsigned OffsetReg = 0;
486 bool OffsetKill = false;
Pete Cooperf68d5032015-05-01 18:57:32 +0000487 bool OffsetInternal = false;
Evan Cheng36064672009-08-11 08:52:18 +0000488 if (HasShift) {
489 OffsetReg = MI->getOperand(2).getReg();
490 OffsetKill = MI->getOperand(2).isKill();
Pete Cooperf68d5032015-05-01 18:57:32 +0000491 OffsetInternal = MI->getOperand(2).isInternalRead();
Bill Wendling092a7bd2010-12-14 03:36:38 +0000492
Evan Cheng36064672009-08-11 08:52:18 +0000493 if (MI->getOperand(3).getImm())
494 // Thumb1 addressing mode doesn't support shift.
495 return false;
496 }
497
498 unsigned OffsetImm = 0;
499 if (HasImmOffset) {
500 OffsetImm = MI->getOperand(2).getImm();
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000501 unsigned MaxOffset = ((1 << ImmLimit) - 1) * Scale;
Bill Wendling092a7bd2010-12-14 03:36:38 +0000502
503 if ((OffsetImm & (Scale - 1)) || OffsetImm > MaxOffset)
Evan Cheng36064672009-08-11 08:52:18 +0000504 // Make sure the immediate field fits.
505 return false;
506 }
507
508 // Add the 16-bit load / store instruction.
Evan Cheng36064672009-08-11 08:52:18 +0000509 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000510 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, TII->get(Opc));
Scott Douglass953f9082015-10-05 14:49:54 +0000511
512 // tSTMIA_UPD takes a defining register operand. We've already checked that
513 // the register is killed, so mark it as dead here.
514 if (Entry.WideOpc == ARM::t2STMIA)
515 MIB.addReg(MI->getOperand(0).getReg(), RegState::Define | RegState::Dead);
516
Evan Chengcc9ca352009-08-11 21:11:32 +0000517 if (!isLdStMul) {
Owen Anderson99ea8a32010-12-07 00:45:21 +0000518 MIB.addOperand(MI->getOperand(0));
Owen Anderson4ebf4712011-02-08 22:39:40 +0000519 MIB.addOperand(MI->getOperand(1));
Bill Wendling092a7bd2010-12-14 03:36:38 +0000520
521 if (HasImmOffset)
522 MIB.addImm(OffsetImm / Scale);
523
Evan Chengcc9ca352009-08-11 21:11:32 +0000524 assert((!HasShift || OffsetReg) && "Invalid so_reg load / store address!");
525
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000526 if (HasOffReg)
Pete Cooperf68d5032015-05-01 18:57:32 +0000527 MIB.addReg(OffsetReg, getKillRegState(OffsetKill) |
528 getInternalReadRegState(OffsetInternal));
Evan Cheng36064672009-08-11 08:52:18 +0000529 }
Evan Cheng806845d2009-08-11 09:37:40 +0000530
Evan Cheng36064672009-08-11 08:52:18 +0000531 // Transfer the rest of operands.
Evan Cheng36064672009-08-11 08:52:18 +0000532 for (unsigned e = MI->getNumOperands(); OpNum != e; ++OpNum)
533 MIB.addOperand(MI->getOperand(OpNum));
534
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000535 // Transfer memoperands.
Chris Lattner1d0c2572011-04-29 05:24:29 +0000536 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000537
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000538 // Transfer MI flags.
539 MIB.setMIFlags(MI->getFlags());
540
Chris Lattnera6f074f2009-08-23 03:41:05 +0000541 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Cheng36064672009-08-11 08:52:18 +0000542
Evan Cheng7fae11b2011-12-14 02:11:42 +0000543 MBB.erase_instr(MI);
Evan Cheng36064672009-08-11 08:52:18 +0000544 ++NumLdSts;
545 return true;
546}
547
Evan Cheng36064672009-08-11 08:52:18 +0000548bool
549Thumb2SizeReduce::ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
550 const ReduceEntry &Entry,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000551 bool LiveCPSR, bool IsSelfLoop) {
Jim Grosbacha8a80672011-06-29 23:25:04 +0000552 unsigned Opc = MI->getOpcode();
553 if (Opc == ARM::t2ADDri) {
554 // If the source register is SP, try to reduce to tADDrSPi, otherwise
555 // it's a normal reduce.
556 if (MI->getOperand(1).getReg() != ARM::SP) {
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000557 if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
Jim Grosbacha8a80672011-06-29 23:25:04 +0000558 return true;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000559 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000560 }
561 // Try to reduce to tADDrSPi.
562 unsigned Imm = MI->getOperand(2).getImm();
563 // The immediate must be in range, the destination register must be a low
Jim Grosbached5134a2011-06-30 02:22:49 +0000564 // reg, the predicate must be "always" and the condition flags must not
565 // be being set.
Jim Grosbach68b0e842011-07-01 19:07:09 +0000566 if (Imm & 3 || Imm > 1020)
Jim Grosbacha8a80672011-06-29 23:25:04 +0000567 return false;
568 if (!isARMLowRegister(MI->getOperand(0).getReg()))
569 return false;
Jim Grosbached5134a2011-06-30 02:22:49 +0000570 if (MI->getOperand(3).getImm() != ARMCC::AL)
571 return false;
Jim Grosbacha8a80672011-06-29 23:25:04 +0000572 const MCInstrDesc &MCID = MI->getDesc();
573 if (MCID.hasOptionalDef() &&
574 MI->getOperand(MCID.getNumOperands()-1).getReg() == ARM::CPSR)
575 return false;
576
Evan Cheng7fae11b2011-12-14 02:11:42 +0000577 MachineInstrBuilder MIB = BuildMI(MBB, MI, MI->getDebugLoc(),
Jim Grosbacha8a80672011-06-29 23:25:04 +0000578 TII->get(ARM::tADDrSPi))
579 .addOperand(MI->getOperand(0))
580 .addOperand(MI->getOperand(1))
581 .addImm(Imm / 4); // The tADDrSPi has an implied scale by four.
Jim Grosbach1b8457a2011-08-24 17:46:13 +0000582 AddDefaultPred(MIB);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000583
584 // Transfer MI flags.
585 MIB.setMIFlags(MI->getFlags());
586
587 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " <<*MIB);
588
Evan Cheng7fae11b2011-12-14 02:11:42 +0000589 MBB.erase_instr(MI);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000590 ++NumNarrows;
591 return true;
592 }
593
Evan Chengcc9ca352009-08-11 21:11:32 +0000594 if (Entry.LowRegs1 && !VerifyLowRegs(MI))
Evan Cheng36064672009-08-11 08:52:18 +0000595 return false;
596
Chad Rosier67336302015-05-22 20:07:34 +0000597 if (MI->mayLoadOrStore())
Evan Cheng36064672009-08-11 08:52:18 +0000598 return ReduceLoadStore(MBB, MI, Entry);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000599
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000600 switch (Opc) {
601 default: break;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000602 case ARM::t2ADDSri:
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000603 case ARM::t2ADDSrr: {
604 unsigned PredReg = 0;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000605 if (getInstrPredicate(*MI, PredReg) == ARMCC::AL) {
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000606 switch (Opc) {
607 default: break;
608 case ARM::t2ADDSri: {
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000609 if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000610 return true;
611 // fallthrough
612 }
613 case ARM::t2ADDSrr:
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000614 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000615 }
616 }
617 break;
618 }
619 case ARM::t2RSBri:
620 case ARM::t2RSBSri:
Jim Grosbach8b31ef52011-07-27 16:47:19 +0000621 case ARM::t2SXTB:
622 case ARM::t2SXTH:
623 case ARM::t2UXTB:
624 case ARM::t2UXTH:
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000625 if (MI->getOperand(2).getImm() == 0)
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000626 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000627 break;
Anton Korobeynikov25229082009-11-24 00:44:37 +0000628 case ARM::t2MOVi16:
629 // Can convert only 'pure' immediate operands, not immediates obtained as
630 // globals' addresses.
631 if (MI->getOperand(1).isImm())
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000632 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Anton Korobeynikov25229082009-11-24 00:44:37 +0000633 break;
Jim Grosbach327cf8e2010-12-07 20:41:06 +0000634 case ARM::t2CMPrr: {
Jim Grosbach5bae0542010-12-03 23:54:18 +0000635 // Try to reduce to the lo-reg only version first. Why there are two
636 // versions of the instruction is a mystery.
637 // It would be nice to just have two entries in the master table that
638 // are prioritized, but the table assumes a unique entry for each
639 // source insn opcode. So for now, we hack a local entry record to use.
640 static const ReduceEntry NarrowEntry =
Evan Chengddc0cb62012-12-20 19:59:30 +0000641 { ARM::t2CMPrr,ARM::tCMPr, 0, 0, 0, 1, 1,2, 0, 0,1,0 };
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000642 if (ReduceToNarrow(MBB, MI, NarrowEntry, LiveCPSR, IsSelfLoop))
Jim Grosbach5bae0542010-12-03 23:54:18 +0000643 return true;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000644 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Jim Grosbach5bae0542010-12-03 23:54:18 +0000645 }
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000646 }
Evan Cheng36064672009-08-11 08:52:18 +0000647 return false;
648}
649
650bool
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000651Thumb2SizeReduce::ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
652 const ReduceEntry &Entry,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000653 bool LiveCPSR, bool IsSelfLoop) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000654
655 if (ReduceLimit2Addr != -1 && ((int)Num2Addrs >= ReduceLimit2Addr))
656 return false;
657
Sanjay Patel924879a2015-08-04 15:49:57 +0000658 if (!OptimizeSize && Entry.AvoidMovs && STI->avoidMOVsShifterOperand())
Evan Chengddc0cb62012-12-20 19:59:30 +0000659 // Don't issue movs with shifter operand for some CPUs unless we
Sanjay Patel924879a2015-08-04 15:49:57 +0000660 // are optimizing for size.
Evan Chengddc0cb62012-12-20 19:59:30 +0000661 return false;
662
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000663 unsigned Reg0 = MI->getOperand(0).getReg();
664 unsigned Reg1 = MI->getOperand(1).getReg();
Jim Grosbachc01104d2012-02-24 00:33:36 +0000665 // t2MUL is "special". The tied source operand is second, not first.
666 if (MI->getOpcode() == ARM::t2MUL) {
Jim Grosbach3a21e2c2012-02-24 00:53:11 +0000667 unsigned Reg2 = MI->getOperand(2).getReg();
668 // Early exit if the regs aren't all low regs.
669 if (!isARMLowRegister(Reg0) || !isARMLowRegister(Reg1)
670 || !isARMLowRegister(Reg2))
671 return false;
672 if (Reg0 != Reg2) {
Jim Grosbachc01104d2012-02-24 00:33:36 +0000673 // If the other operand also isn't the same as the destination, we
674 // can't reduce.
675 if (Reg1 != Reg0)
676 return false;
677 // Try to commute the operands to make it a 2-address instruction.
678 MachineInstr *CommutedMI = TII->commuteInstruction(MI);
679 if (!CommutedMI)
680 return false;
681 }
682 } else if (Reg0 != Reg1) {
Bob Wilson279e55f2010-06-24 16:50:20 +0000683 // Try to commute the operands to make it a 2-address instruction.
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000684 unsigned CommOpIdx1 = 1;
685 unsigned CommOpIdx2 = TargetInstrInfo::CommuteAnyOperandIndex;
Bob Wilson279e55f2010-06-24 16:50:20 +0000686 if (!TII->findCommutedOpIndices(MI, CommOpIdx1, CommOpIdx2) ||
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000687 MI->getOperand(CommOpIdx2).getReg() != Reg0)
Bob Wilson279e55f2010-06-24 16:50:20 +0000688 return false;
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000689 MachineInstr *CommutedMI =
690 TII->commuteInstruction(MI, false, CommOpIdx1, CommOpIdx2);
Bob Wilson279e55f2010-06-24 16:50:20 +0000691 if (!CommutedMI)
692 return false;
693 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000694 if (Entry.LowRegs2 && !isARMLowRegister(Reg0))
695 return false;
696 if (Entry.Imm2Limit) {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000697 unsigned Imm = MI->getOperand(2).getImm();
Evan Cheng1be453b2009-08-08 03:21:23 +0000698 unsigned Limit = (1 << Entry.Imm2Limit) - 1;
699 if (Imm > Limit)
700 return false;
701 } else {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000702 unsigned Reg2 = MI->getOperand(2).getReg();
Evan Cheng1be453b2009-08-08 03:21:23 +0000703 if (Entry.LowRegs2 && !isARMLowRegister(Reg2))
704 return false;
705 }
706
Evan Cheng1f5bee12009-08-10 06:57:42 +0000707 // Check if it's possible / necessary to transfer the predicate.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000708 const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc2);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000709 unsigned PredReg = 0;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000710 ARMCC::CondCodes Pred = getInstrPredicate(*MI, PredReg);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000711 bool SkipPred = false;
712 if (Pred != ARMCC::AL) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000713 if (!NewMCID.isPredicable())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000714 // Can't transfer predicate, fail.
715 return false;
716 } else {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000717 SkipPred = !NewMCID.isPredicable();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000718 }
719
Evan Cheng1be453b2009-08-08 03:21:23 +0000720 bool HasCC = false;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000721 bool CCDead = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000722 const MCInstrDesc &MCID = MI->getDesc();
723 if (MCID.hasOptionalDef()) {
724 unsigned NumOps = MCID.getNumOperands();
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000725 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
726 if (HasCC && MI->getOperand(NumOps-1).isDead())
727 CCDead = true;
728 }
Evan Cheng1f5bee12009-08-10 06:57:42 +0000729 if (!VerifyPredAndCC(MI, Entry, true, Pred, LiveCPSR, HasCC, CCDead))
Evan Chengd461c1c2009-08-09 19:17:19 +0000730 return false;
Evan Cheng1be453b2009-08-08 03:21:23 +0000731
Bob Wilsona2881ee2011-04-19 18:11:49 +0000732 // Avoid adding a false dependency on partial flag update by some 16-bit
733 // instructions which has the 's' bit set.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000734 if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000735 canAddPseudoFlagDep(MI, IsSelfLoop))
Bob Wilsona2881ee2011-04-19 18:11:49 +0000736 return false;
737
Evan Cheng1be453b2009-08-08 03:21:23 +0000738 // Add the 16-bit instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000739 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000740 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000741 MIB.addOperand(MI->getOperand(0));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000742 if (NewMCID.hasOptionalDef()) {
Evan Cheng6ddd7bc2009-08-15 07:59:10 +0000743 if (HasCC)
744 AddDefaultT1CC(MIB, CCDead);
745 else
746 AddNoT1CC(MIB);
747 }
Evan Chengd461c1c2009-08-09 19:17:19 +0000748
749 // Transfer the rest of operands.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000750 unsigned NumOps = MCID.getNumOperands();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000751 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000752 if (i < NumOps && MCID.OpInfo[i].isOptionalDef())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000753 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000754 if (SkipPred && MCID.OpInfo[i].isPredicate())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000755 continue;
756 MIB.addOperand(MI->getOperand(i));
757 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000758
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000759 // Transfer MI flags.
760 MIB.setMIFlags(MI->getFlags());
761
Chris Lattnera6f074f2009-08-23 03:41:05 +0000762 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Cheng1be453b2009-08-08 03:21:23 +0000763
Evan Cheng7fae11b2011-12-14 02:11:42 +0000764 MBB.erase_instr(MI);
Evan Cheng1be453b2009-08-08 03:21:23 +0000765 ++Num2Addrs;
Evan Cheng1be453b2009-08-08 03:21:23 +0000766 return true;
767}
768
769bool
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000770Thumb2SizeReduce::ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
771 const ReduceEntry &Entry,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000772 bool LiveCPSR, bool IsSelfLoop) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000773 if (ReduceLimit != -1 && ((int)NumNarrows >= ReduceLimit))
774 return false;
775
Sanjay Patel924879a2015-08-04 15:49:57 +0000776 if (!OptimizeSize && Entry.AvoidMovs && STI->avoidMOVsShifterOperand())
Evan Chengddc0cb62012-12-20 19:59:30 +0000777 // Don't issue movs with shifter operand for some CPUs unless we
Sanjay Patel924879a2015-08-04 15:49:57 +0000778 // are optimizing for size.
Evan Chengddc0cb62012-12-20 19:59:30 +0000779 return false;
780
Evan Chengd461c1c2009-08-09 19:17:19 +0000781 unsigned Limit = ~0U;
782 if (Entry.Imm1Limit)
Jim Grosbacha8a80672011-06-29 23:25:04 +0000783 Limit = (1 << Entry.Imm1Limit) - 1;
Evan Chengd461c1c2009-08-09 19:17:19 +0000784
Evan Cheng6cc775f2011-06-28 19:10:37 +0000785 const MCInstrDesc &MCID = MI->getDesc();
786 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i) {
787 if (MCID.OpInfo[i].isPredicate())
Evan Chengd461c1c2009-08-09 19:17:19 +0000788 continue;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000789 const MachineOperand &MO = MI->getOperand(i);
Evan Chengd461c1c2009-08-09 19:17:19 +0000790 if (MO.isReg()) {
791 unsigned Reg = MO.getReg();
792 if (!Reg || Reg == ARM::CPSR)
793 continue;
794 if (Entry.LowRegs1 && !isARMLowRegister(Reg))
795 return false;
Evan Chengf6a9d062009-08-11 23:00:31 +0000796 } else if (MO.isImm() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +0000797 !MCID.OpInfo[i].isPredicate()) {
Jim Grosbacha8a80672011-06-29 23:25:04 +0000798 if (((unsigned)MO.getImm()) > Limit)
Evan Chengd461c1c2009-08-09 19:17:19 +0000799 return false;
800 }
801 }
802
Evan Cheng1f5bee12009-08-10 06:57:42 +0000803 // Check if it's possible / necessary to transfer the predicate.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000804 const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc1);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000805 unsigned PredReg = 0;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000806 ARMCC::CondCodes Pred = getInstrPredicate(*MI, PredReg);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000807 bool SkipPred = false;
808 if (Pred != ARMCC::AL) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000809 if (!NewMCID.isPredicable())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000810 // Can't transfer predicate, fail.
811 return false;
812 } else {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000813 SkipPred = !NewMCID.isPredicable();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000814 }
815
Evan Chengd461c1c2009-08-09 19:17:19 +0000816 bool HasCC = false;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000817 bool CCDead = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000818 if (MCID.hasOptionalDef()) {
819 unsigned NumOps = MCID.getNumOperands();
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000820 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
821 if (HasCC && MI->getOperand(NumOps-1).isDead())
822 CCDead = true;
823 }
Evan Cheng1f5bee12009-08-10 06:57:42 +0000824 if (!VerifyPredAndCC(MI, Entry, false, Pred, LiveCPSR, HasCC, CCDead))
Evan Chengd461c1c2009-08-09 19:17:19 +0000825 return false;
826
Bob Wilsona2881ee2011-04-19 18:11:49 +0000827 // Avoid adding a false dependency on partial flag update by some 16-bit
828 // instructions which has the 's' bit set.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000829 if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000830 canAddPseudoFlagDep(MI, IsSelfLoop))
Bob Wilsona2881ee2011-04-19 18:11:49 +0000831 return false;
832
Evan Chengd461c1c2009-08-09 19:17:19 +0000833 // Add the 16-bit instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000834 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000835 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000836 MIB.addOperand(MI->getOperand(0));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000837 if (NewMCID.hasOptionalDef()) {
Evan Cheng6ddd7bc2009-08-15 07:59:10 +0000838 if (HasCC)
839 AddDefaultT1CC(MIB, CCDead);
840 else
841 AddNoT1CC(MIB);
842 }
Evan Chengd461c1c2009-08-09 19:17:19 +0000843
844 // Transfer the rest of operands.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000845 unsigned NumOps = MCID.getNumOperands();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000846 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000847 if (i < NumOps && MCID.OpInfo[i].isOptionalDef())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000848 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000849 if ((MCID.getOpcode() == ARM::t2RSBSri ||
Jim Grosbach8b31ef52011-07-27 16:47:19 +0000850 MCID.getOpcode() == ARM::t2RSBri ||
851 MCID.getOpcode() == ARM::t2SXTB ||
852 MCID.getOpcode() == ARM::t2SXTH ||
853 MCID.getOpcode() == ARM::t2UXTB ||
854 MCID.getOpcode() == ARM::t2UXTH) && i == 2)
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000855 // Skip the zero immediate operand, it's now implicit.
856 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000857 bool isPred = (i < NumOps && MCID.OpInfo[i].isPredicate());
Evan Chengf6a9d062009-08-11 23:00:31 +0000858 if (SkipPred && isPred)
859 continue;
860 const MachineOperand &MO = MI->getOperand(i);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000861 if (MO.isReg() && MO.isImplicit() && MO.getReg() == ARM::CPSR)
862 // Skip implicit def of CPSR. Either it's modeled as an optional
863 // def now or it's already an implicit def on the new instruction.
864 continue;
865 MIB.addOperand(MO);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000866 }
Evan Cheng6cc775f2011-06-28 19:10:37 +0000867 if (!MCID.isPredicable() && NewMCID.isPredicable())
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000868 AddDefaultPred(MIB);
Evan Chengd461c1c2009-08-09 19:17:19 +0000869
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000870 // Transfer MI flags.
871 MIB.setMIFlags(MI->getFlags());
872
Chris Lattnera6f074f2009-08-23 03:41:05 +0000873 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Chengd461c1c2009-08-09 19:17:19 +0000874
Evan Cheng7fae11b2011-12-14 02:11:42 +0000875 MBB.erase_instr(MI);
Evan Chengd461c1c2009-08-09 19:17:19 +0000876 ++NumNarrows;
877 return true;
Evan Cheng1be453b2009-08-08 03:21:23 +0000878}
879
Bob Wilsona2881ee2011-04-19 18:11:49 +0000880static bool UpdateCPSRDef(MachineInstr &MI, bool LiveCPSR, bool &DefCPSR) {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000881 bool HasDef = false;
Owen Anderson8c1f17b2014-03-07 22:48:22 +0000882 for (const MachineOperand &MO : MI.operands()) {
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000883 if (!MO.isReg() || MO.isUndef() || MO.isUse())
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000884 continue;
885 if (MO.getReg() != ARM::CPSR)
886 continue;
Bob Wilsona2881ee2011-04-19 18:11:49 +0000887
888 DefCPSR = true;
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000889 if (!MO.isDead())
890 HasDef = true;
891 }
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000892
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000893 return HasDef || LiveCPSR;
894}
895
896static bool UpdateCPSRUse(MachineInstr &MI, bool LiveCPSR) {
Owen Anderson8c1f17b2014-03-07 22:48:22 +0000897 for (const MachineOperand &MO : MI.operands()) {
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000898 if (!MO.isReg() || MO.isUndef() || MO.isDef())
899 continue;
900 if (MO.getReg() != ARM::CPSR)
901 continue;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000902 assert(LiveCPSR && "CPSR liveness tracking is wrong!");
903 if (MO.isKill()) {
904 LiveCPSR = false;
905 break;
906 }
907 }
908
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000909 return LiveCPSR;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000910}
911
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000912bool Thumb2SizeReduce::ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000913 bool LiveCPSR, bool IsSelfLoop) {
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000914 unsigned Opcode = MI->getOpcode();
915 DenseMap<unsigned, unsigned>::iterator OPI = ReduceOpcodeMap.find(Opcode);
916 if (OPI == ReduceOpcodeMap.end())
917 return false;
918 const ReduceEntry &Entry = ReduceTable[OPI->second];
919
920 // Don't attempt normal reductions on "special" cases for now.
921 if (Entry.Special)
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000922 return ReduceSpecial(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000923
924 // Try to transform to a 16-bit two-address instruction.
925 if (Entry.NarrowOpc2 &&
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000926 ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000927 return true;
928
929 // Try to transform to a 16-bit non-two-address instruction.
930 if (Entry.NarrowOpc1 &&
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000931 ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000932 return true;
933
934 return false;
935}
936
Evan Cheng1be453b2009-08-08 03:21:23 +0000937bool Thumb2SizeReduce::ReduceMBB(MachineBasicBlock &MBB) {
938 bool Modified = false;
939
Evan Cheng1f5bee12009-08-10 06:57:42 +0000940 // Yes, CPSR could be livein.
Dan Gohmana1cf9fe2010-04-13 16:53:51 +0000941 bool LiveCPSR = MBB.isLiveIn(ARM::CPSR);
Craig Topper062a2ba2014-04-25 05:30:21 +0000942 MachineInstr *BundleMI = nullptr;
Evan Cheng1f5bee12009-08-10 06:57:42 +0000943
Craig Topper062a2ba2014-04-25 05:30:21 +0000944 CPSRDef = nullptr;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000945 HighLatencyCPSR = false;
946
947 // Check predecessors for the latest CPSRDef.
Jim Grosbach537f3ed2014-04-04 02:11:03 +0000948 for (auto *Pred : MBB.predecessors()) {
949 const MBBInfo &PInfo = BlockInfo[Pred->getNumber()];
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000950 if (!PInfo.Visited) {
951 // Since blocks are visited in RPO, this must be a back-edge.
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000952 continue;
953 }
954 if (PInfo.HighLatencyCPSR) {
955 HighLatencyCPSR = true;
956 break;
957 }
958 }
959
Evan Chengf4807a12011-10-27 21:21:05 +0000960 // If this BB loops back to itself, conservatively avoid narrowing the
961 // first instruction that does partial flag update.
962 bool IsSelfLoop = MBB.isSuccessor(&MBB);
Jim Grosbach0c509fa2012-04-06 23:43:50 +0000963 MachineBasicBlock::instr_iterator MII = MBB.instr_begin(),E = MBB.instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000964 MachineBasicBlock::instr_iterator NextMII;
Evan Cheng1be453b2009-08-08 03:21:23 +0000965 for (; MII != E; MII = NextMII) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000966 NextMII = std::next(MII);
Evan Cheng1be453b2009-08-08 03:21:23 +0000967
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000968 MachineInstr *MI = &*MII;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000969 if (MI->isBundle()) {
970 BundleMI = MI;
971 continue;
972 }
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000973 if (MI->isDebugValue())
974 continue;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000975
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000976 LiveCPSR = UpdateCPSRUse(*MI, LiveCPSR);
977
Jakob Stoklund Olesen41bbf9c2012-12-18 00:46:39 +0000978 // Does NextMII belong to the same bundle as MI?
979 bool NextInSameBundle = NextMII != E && NextMII->isBundledWithPred();
980
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000981 if (ReduceMI(MBB, MI, LiveCPSR, IsSelfLoop)) {
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000982 Modified = true;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000983 MachineBasicBlock::instr_iterator I = std::prev(NextMII);
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000984 MI = &*I;
Jakob Stoklund Olesen41bbf9c2012-12-18 00:46:39 +0000985 // Removing and reinserting the first instruction in a bundle will break
986 // up the bundle. Fix the bundling if it was broken.
987 if (NextInSameBundle && !NextMII->isBundledWithPred())
988 NextMII->bundleWithPred();
Evan Cheng1be453b2009-08-08 03:21:23 +0000989 }
990
Jakob Stoklund Olesen41bbf9c2012-12-18 00:46:39 +0000991 if (!NextInSameBundle && MI->isInsideBundle()) {
Evan Cheng7fae11b2011-12-14 02:11:42 +0000992 // FIXME: Since post-ra scheduler operates on bundles, the CPSR kill
993 // marker is only on the BUNDLE instruction. Process the BUNDLE
994 // instruction as we finish with the bundled instruction to work around
995 // the inconsistency.
Evan Cheng903231b2011-12-17 01:25:34 +0000996 if (BundleMI->killsRegister(ARM::CPSR))
997 LiveCPSR = false;
998 MachineOperand *MO = BundleMI->findRegisterDefOperand(ARM::CPSR);
999 if (MO && !MO->isDead())
1000 LiveCPSR = true;
Weiming Zhaof66be562014-01-13 18:47:54 +00001001 MO = BundleMI->findRegisterUseOperand(ARM::CPSR);
1002 if (MO && !MO->isKill())
1003 LiveCPSR = true;
Evan Cheng903231b2011-12-17 01:25:34 +00001004 }
Evan Cheng7fae11b2011-12-14 02:11:42 +00001005
Bob Wilsona2881ee2011-04-19 18:11:49 +00001006 bool DefCPSR = false;
1007 LiveCPSR = UpdateCPSRDef(*MI, LiveCPSR, DefCPSR);
Evan Cheng7f8e5632011-12-07 07:15:52 +00001008 if (MI->isCall()) {
Bob Wilsona2881ee2011-04-19 18:11:49 +00001009 // Calls don't really set CPSR.
Craig Topper062a2ba2014-04-25 05:30:21 +00001010 CPSRDef = nullptr;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001011 HighLatencyCPSR = false;
Evan Chengf4807a12011-10-27 21:21:05 +00001012 IsSelfLoop = false;
1013 } else if (DefCPSR) {
Bob Wilsona2881ee2011-04-19 18:11:49 +00001014 // This is the last CPSR defining instruction.
1015 CPSRDef = MI;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001016 HighLatencyCPSR = isHighLatencyCPSR(CPSRDef);
Evan Chengf4807a12011-10-27 21:21:05 +00001017 IsSelfLoop = false;
1018 }
Evan Cheng1be453b2009-08-08 03:21:23 +00001019 }
1020
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001021 MBBInfo &Info = BlockInfo[MBB.getNumber()];
1022 Info.HighLatencyCPSR = HighLatencyCPSR;
1023 Info.Visited = true;
Evan Cheng1be453b2009-08-08 03:21:23 +00001024 return Modified;
1025}
1026
1027bool Thumb2SizeReduce::runOnMachineFunction(MachineFunction &MF) {
Akira Hatanaka4a616192015-06-08 18:50:43 +00001028 if (PredicateFtor && !PredicateFtor(*MF.getFunction()))
1029 return false;
1030
Eric Christopher1b21f002015-01-29 00:19:33 +00001031 STI = &static_cast<const ARMSubtarget &>(MF.getSubtarget());
Eric Christopher63b44882015-03-05 00:23:40 +00001032 if (STI->isThumb1Only() || STI->prefers32BitThumb())
1033 return false;
1034
Eric Christopher1b21f002015-01-29 00:19:33 +00001035 TII = static_cast<const Thumb2InstrInfo *>(STI->getInstrInfo());
Evan Cheng1be453b2009-08-08 03:21:23 +00001036
Sanjay Patel924879a2015-08-04 15:49:57 +00001037 // Optimizing / minimizing size? Minimizing size implies optimizing for size.
1038 OptimizeSize = MF.getFunction()->optForSize();
1039 MinimizeSize = MF.getFunction()->optForMinSize();
Quentin Colombet23b404d2012-12-18 22:47:16 +00001040
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001041 BlockInfo.clear();
1042 BlockInfo.resize(MF.getNumBlockIDs());
1043
1044 // Visit blocks in reverse post-order so LastCPSRDef is known for all
1045 // predecessors.
1046 ReversePostOrderTraversal<MachineFunction*> RPOT(&MF);
Evan Cheng1be453b2009-08-08 03:21:23 +00001047 bool Modified = false;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001048 for (ReversePostOrderTraversal<MachineFunction*>::rpo_iterator
1049 I = RPOT.begin(), E = RPOT.end(); I != E; ++I)
1050 Modified |= ReduceMBB(**I);
Evan Cheng1be453b2009-08-08 03:21:23 +00001051 return Modified;
1052}
1053
1054/// createThumb2SizeReductionPass - Returns an instance of the Thumb2 size
1055/// reduction pass.
Akira Hatanaka4a616192015-06-08 18:50:43 +00001056FunctionPass *llvm::createThumb2SizeReductionPass(
1057 std::function<bool(const Function &)> Ftor) {
1058 return new Thumb2SizeReduce(Ftor);
Evan Cheng1be453b2009-08-08 03:21:23 +00001059}