blob: 881e3cb25776efb27a9122d925479be0dfb97b67 [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"
Benjamin Kramer82de7d32016-05-27 14:27:24 +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"
Benjamin Kramer82de7d32016-05-27 14:27:24 +000026#include <utility>
Evan Cheng1be453b2009-08-08 03:21:23 +000027using namespace llvm;
28
Chandler Carruth84e68b22014-04-22 02:41:26 +000029#define DEBUG_TYPE "t2-reduce-size"
30
Evan Cheng1f5bee12009-08-10 06:57:42 +000031STATISTIC(NumNarrows, "Number of 32-bit instrs reduced to 16-bit ones");
32STATISTIC(Num2Addrs, "Number of 32-bit instrs reduced to 2addr 16-bit ones");
Evan Cheng36064672009-08-11 08:52:18 +000033STATISTIC(NumLdSts, "Number of 32-bit load / store reduced to 16-bit ones");
Evan Cheng1be453b2009-08-08 03:21:23 +000034
Evan Chengcc9ca352009-08-11 21:11:32 +000035static cl::opt<int> ReduceLimit("t2-reduce-limit",
36 cl::init(-1), cl::Hidden);
37static cl::opt<int> ReduceLimit2Addr("t2-reduce-limit2",
38 cl::init(-1), cl::Hidden);
39static cl::opt<int> ReduceLimitLdSt("t2-reduce-limit3",
40 cl::init(-1), cl::Hidden);
Evan Chengf16a1d52009-08-10 07:20:37 +000041
Evan Cheng1be453b2009-08-08 03:21:23 +000042namespace {
43 /// ReduceTable - A static table with information on mapping from wide
44 /// opcodes to narrow
45 struct ReduceEntry {
Craig Topperca658c22012-03-11 07:16:55 +000046 uint16_t WideOpc; // Wide opcode
47 uint16_t NarrowOpc1; // Narrow opcode to transform to
48 uint16_t NarrowOpc2; // Narrow opcode when it's two-address
Evan Cheng1be453b2009-08-08 03:21:23 +000049 uint8_t Imm1Limit; // Limit of immediate field (bits)
50 uint8_t Imm2Limit; // Limit of immediate field when it's two-address
51 unsigned LowRegs1 : 1; // Only possible if low-registers are used
52 unsigned LowRegs2 : 1; // Only possible if low-registers are used (2addr)
Evan Cheng1e6c2a12009-08-12 01:49:45 +000053 unsigned PredCC1 : 2; // 0 - If predicated, cc is on and vice versa.
Evan Cheng1be453b2009-08-08 03:21:23 +000054 // 1 - No cc field.
Evan Cheng1e6c2a12009-08-12 01:49:45 +000055 // 2 - Always set CPSR.
Evan Chengaee7e492009-08-12 18:35:50 +000056 unsigned PredCC2 : 2;
Bob Wilsona2881ee2011-04-19 18:11:49 +000057 unsigned PartFlag : 1; // 16-bit instruction does partial flag update
Evan Cheng1be453b2009-08-08 03:21:23 +000058 unsigned Special : 1; // Needs to be dealt with specially
Evan Chengddc0cb62012-12-20 19:59:30 +000059 unsigned AvoidMovs: 1; // Avoid movs with shifter operand (for Swift)
Evan Cheng1be453b2009-08-08 03:21:23 +000060 };
61
62 static const ReduceEntry ReduceTable[] = {
Evan Chengddc0cb62012-12-20 19:59:30 +000063 // Wide, Narrow1, Narrow2, imm1,imm2, lo1, lo2, P/C,PF,S,AM
64 { ARM::t2ADCrr, 0, ARM::tADC, 0, 0, 0, 1, 0,0, 0,0,0 },
65 { ARM::t2ADDri, ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 0,0, 0,1,0 },
66 { ARM::t2ADDrr, ARM::tADDrr, ARM::tADDhirr, 0, 0, 1, 0, 0,1, 0,0,0 },
67 { ARM::t2ADDSri,ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 2,2, 0,1,0 },
68 { ARM::t2ADDSrr,ARM::tADDrr, 0, 0, 0, 1, 0, 2,0, 0,1,0 },
69 { ARM::t2ANDrr, 0, ARM::tAND, 0, 0, 0, 1, 0,0, 1,0,0 },
70 { ARM::t2ASRri, ARM::tASRri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
71 { ARM::t2ASRrr, 0, ARM::tASRrr, 0, 0, 0, 1, 0,0, 1,0,1 },
72 { ARM::t2BICrr, 0, ARM::tBIC, 0, 0, 0, 1, 0,0, 1,0,0 },
73 //FIXME: Disable CMN, as CCodes are backwards from compare expectations
74 //{ ARM::t2CMNrr, ARM::tCMN, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
75 { ARM::t2CMNzrr, ARM::tCMNz, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
76 { ARM::t2CMPri, ARM::tCMPi8, 0, 8, 0, 1, 0, 2,0, 0,0,0 },
77 { ARM::t2CMPrr, ARM::tCMPhir, 0, 0, 0, 0, 0, 2,0, 0,1,0 },
78 { ARM::t2EORrr, 0, ARM::tEOR, 0, 0, 0, 1, 0,0, 1,0,0 },
79 // FIXME: adr.n immediate offset must be multiple of 4.
80 //{ ARM::t2LEApcrelJT,ARM::tLEApcrelJT, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
81 { ARM::t2LSLri, ARM::tLSLri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
82 { ARM::t2LSLrr, 0, ARM::tLSLrr, 0, 0, 0, 1, 0,0, 1,0,1 },
83 { ARM::t2LSRri, ARM::tLSRri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
84 { ARM::t2LSRrr, 0, ARM::tLSRrr, 0, 0, 0, 1, 0,0, 1,0,1 },
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +000085 { ARM::t2MOVi, ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 1,0,0 },
86 { ARM::t2MOVi16,ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 1,1,0 },
Evan Chengddc0cb62012-12-20 19:59:30 +000087 // FIXME: Do we need the 16-bit 'S' variant?
88 { ARM::t2MOVr,ARM::tMOVr, 0, 0, 0, 0, 0, 1,0, 0,0,0 },
89 { ARM::t2MUL, 0, ARM::tMUL, 0, 0, 0, 1, 0,0, 1,0,0 },
90 { ARM::t2MVNr, ARM::tMVN, 0, 0, 0, 1, 0, 0,0, 0,0,0 },
91 { ARM::t2ORRrr, 0, ARM::tORR, 0, 0, 0, 1, 0,0, 1,0,0 },
92 { ARM::t2REV, ARM::tREV, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
93 { ARM::t2REV16, ARM::tREV16, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
94 { ARM::t2REVSH, ARM::tREVSH, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
95 { ARM::t2RORrr, 0, ARM::tROR, 0, 0, 0, 1, 0,0, 1,0,0 },
96 { ARM::t2RSBri, ARM::tRSB, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
97 { ARM::t2RSBSri,ARM::tRSB, 0, 0, 0, 1, 0, 2,0, 0,1,0 },
98 { ARM::t2SBCrr, 0, ARM::tSBC, 0, 0, 0, 1, 0,0, 0,0,0 },
99 { ARM::t2SUBri, ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 0,0, 0,0,0 },
100 { ARM::t2SUBrr, ARM::tSUBrr, 0, 0, 0, 1, 0, 0,0, 0,0,0 },
101 { ARM::t2SUBSri,ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 2,2, 0,0,0 },
102 { ARM::t2SUBSrr,ARM::tSUBrr, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
103 { ARM::t2SXTB, ARM::tSXTB, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
104 { ARM::t2SXTH, ARM::tSXTH, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
105 { ARM::t2TSTrr, ARM::tTST, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
106 { ARM::t2UXTB, ARM::tUXTB, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
107 { ARM::t2UXTH, ARM::tUXTH, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
Evan Cheng36064672009-08-11 08:52:18 +0000108
Evan Chengddc0cb62012-12-20 19:59:30 +0000109 // FIXME: Clean this up after splitting each Thumb load / store opcode
110 // into multiple ones.
111 { ARM::t2LDRi12,ARM::tLDRi, ARM::tLDRspi, 5, 8, 1, 0, 0,0, 0,1,0 },
112 { ARM::t2LDRs, ARM::tLDRr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
113 { ARM::t2LDRBi12,ARM::tLDRBi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
114 { ARM::t2LDRBs, ARM::tLDRBr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
115 { ARM::t2LDRHi12,ARM::tLDRHi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
116 { ARM::t2LDRHs, ARM::tLDRHr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
117 { ARM::t2LDRSBs,ARM::tLDRSB, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
118 { ARM::t2LDRSHs,ARM::tLDRSH, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
James Molloy53298a12016-06-07 12:13:34 +0000119 { ARM::t2LDR_POST,ARM::tLDMIA_UPD,0, 0, 0, 1, 0, 0,0, 0,1,0 },
Evan Chengddc0cb62012-12-20 19:59:30 +0000120 { ARM::t2STRi12,ARM::tSTRi, ARM::tSTRspi, 5, 8, 1, 0, 0,0, 0,1,0 },
121 { ARM::t2STRs, ARM::tSTRr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
122 { ARM::t2STRBi12,ARM::tSTRBi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
123 { ARM::t2STRBs, ARM::tSTRBr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
124 { ARM::t2STRHi12,ARM::tSTRHi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
125 { ARM::t2STRHs, ARM::tSTRHr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
James Molloy53298a12016-06-07 12:13:34 +0000126 { ARM::t2STR_POST,ARM::tSTMIA_UPD,0, 0, 0, 1, 0, 0,0, 0,1,0 },
Evan Chengcc9ca352009-08-11 21:11:32 +0000127
Evan Chengddc0cb62012-12-20 19:59:30 +0000128 { ARM::t2LDMIA, ARM::tLDMIA, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
129 { ARM::t2LDMIA_RET,0, ARM::tPOP_RET, 0, 0, 1, 1, 1,1, 0,1,0 },
130 { ARM::t2LDMIA_UPD,ARM::tLDMIA_UPD,ARM::tPOP,0, 0, 1, 1, 1,1, 0,1,0 },
Scott Douglass953f9082015-10-05 14:49:54 +0000131 // ARM::t2STMIA (with no basereg writeback) has no Thumb1 equivalent.
132 // tSTMIA_UPD is a change in semantics which can only be used if the base
133 // register is killed. This difference is correctly handled elsewhere.
134 { ARM::t2STMIA, ARM::tSTMIA_UPD, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
Evan Chengddc0cb62012-12-20 19:59:30 +0000135 { ARM::t2STMIA_UPD,ARM::tSTMIA_UPD, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
136 { ARM::t2STMDB_UPD, 0, ARM::tPUSH, 0, 0, 1, 1, 1,1, 0,1,0 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000137 };
138
Nick Lewycky02d5f772009-10-25 06:33:48 +0000139 class Thumb2SizeReduce : public MachineFunctionPass {
Evan Cheng1be453b2009-08-08 03:21:23 +0000140 public:
141 static char ID;
Akira Hatanaka4a616192015-06-08 18:50:43 +0000142 Thumb2SizeReduce(std::function<bool(const Function &)> Ftor);
Evan Cheng1be453b2009-08-08 03:21:23 +0000143
Evan Cheng6ddd7bc2009-08-15 07:59:10 +0000144 const Thumb2InstrInfo *TII;
Bob Wilsona2881ee2011-04-19 18:11:49 +0000145 const ARMSubtarget *STI;
Evan Cheng1be453b2009-08-08 03:21:23 +0000146
Craig Topper6bc27bf2014-03-10 02:09:33 +0000147 bool runOnMachineFunction(MachineFunction &MF) override;
Evan Cheng1be453b2009-08-08 03:21:23 +0000148
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000149 MachineFunctionProperties getRequiredProperties() const override {
150 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000151 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000152 }
153
Mehdi Amini117296c2016-10-01 02:56:57 +0000154 StringRef getPassName() const override {
Evan Cheng1be453b2009-08-08 03:21:23 +0000155 return "Thumb2 instruction size reduction pass";
156 }
157
158 private:
159 /// ReduceOpcodeMap - Maps wide opcode to index of entry in ReduceTable.
160 DenseMap<unsigned, unsigned> ReduceOpcodeMap;
161
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000162 bool canAddPseudoFlagDep(MachineInstr *Use, bool IsSelfLoop);
Bob Wilsona2881ee2011-04-19 18:11:49 +0000163
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000164 bool VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
165 bool is2Addr, ARMCC::CondCodes Pred,
166 bool LiveCPSR, bool &HasCC, bool &CCDead);
167
Evan Cheng36064672009-08-11 08:52:18 +0000168 bool ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
169 const ReduceEntry &Entry);
170
171 bool ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000172 const ReduceEntry &Entry, bool LiveCPSR, bool IsSelfLoop);
Evan Cheng36064672009-08-11 08:52:18 +0000173
Evan Cheng1be453b2009-08-08 03:21:23 +0000174 /// ReduceTo2Addr - Reduce a 32-bit instruction to a 16-bit two-address
175 /// instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000176 bool ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000177 const ReduceEntry &Entry, bool LiveCPSR,
Evan Chengf4807a12011-10-27 21:21:05 +0000178 bool IsSelfLoop);
Evan Cheng1be453b2009-08-08 03:21:23 +0000179
180 /// ReduceToNarrow - Reduce a 32-bit instruction to a 16-bit
181 /// non-two-address instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000182 bool ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000183 const ReduceEntry &Entry, bool LiveCPSR,
Evan Chengf4807a12011-10-27 21:21:05 +0000184 bool IsSelfLoop);
Evan Cheng1be453b2009-08-08 03:21:23 +0000185
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000186 /// ReduceMI - Attempt to reduce MI, return true on success.
187 bool ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000188 bool LiveCPSR, bool IsSelfLoop);
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000189
Evan Cheng1be453b2009-08-08 03:21:23 +0000190 /// ReduceMBB - Reduce width of instructions in the specified basic block.
191 bool ReduceMBB(MachineBasicBlock &MBB);
Quentin Colombet23b404d2012-12-18 22:47:16 +0000192
Evan Chengddc0cb62012-12-20 19:59:30 +0000193 bool OptimizeSize;
Quentin Colombet23b404d2012-12-18 22:47:16 +0000194 bool MinimizeSize;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000195
196 // Last instruction to define CPSR in the current block.
197 MachineInstr *CPSRDef;
198 // Was CPSR last defined by a high latency instruction?
199 // When CPSRDef is null, this refers to CPSR defs in predecessors.
200 bool HighLatencyCPSR;
201
202 struct MBBInfo {
203 // The flags leaving this block have high latency.
204 bool HighLatencyCPSR;
205 // Has this block been visited yet?
206 bool Visited;
207
208 MBBInfo() : HighLatencyCPSR(false), Visited(false) {}
209 };
210
211 SmallVector<MBBInfo, 8> BlockInfo;
Akira Hatanaka4a616192015-06-08 18:50:43 +0000212
213 std::function<bool(const Function &)> PredicateFtor;
Evan Cheng1be453b2009-08-08 03:21:23 +0000214 };
215 char Thumb2SizeReduce::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000216}
Evan Cheng1be453b2009-08-08 03:21:23 +0000217
Akira Hatanaka4a616192015-06-08 18:50:43 +0000218Thumb2SizeReduce::Thumb2SizeReduce(std::function<bool(const Function &)> Ftor)
Benjamin Kramer82de7d32016-05-27 14:27:24 +0000219 : MachineFunctionPass(ID), PredicateFtor(std::move(Ftor)) {
Evan Chengddc0cb62012-12-20 19:59:30 +0000220 OptimizeSize = MinimizeSize = false;
Evan Cheng1be453b2009-08-08 03:21:23 +0000221 for (unsigned i = 0, e = array_lengthof(ReduceTable); i != e; ++i) {
222 unsigned FromOpc = ReduceTable[i].WideOpc;
223 if (!ReduceOpcodeMap.insert(std::make_pair(FromOpc, i)).second)
Benjamin Kramer8ceb3232015-10-25 22:28:27 +0000224 llvm_unreachable("Duplicated entries?");
Evan Cheng1be453b2009-08-08 03:21:23 +0000225 }
226}
227
Evan Cheng6cc775f2011-06-28 19:10:37 +0000228static bool HasImplicitCPSRDef(const MCInstrDesc &MCID) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000229 for (const MCPhysReg *Regs = MCID.getImplicitDefs(); *Regs; ++Regs)
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000230 if (*Regs == ARM::CPSR)
231 return true;
232 return false;
233}
234
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000235// Check for a likely high-latency flag def.
236static bool isHighLatencyCPSR(MachineInstr *Def) {
237 switch(Def->getOpcode()) {
238 case ARM::FMSTAT:
239 case ARM::tMUL:
240 return true;
241 }
242 return false;
243}
244
Bob Wilsona2881ee2011-04-19 18:11:49 +0000245/// canAddPseudoFlagDep - For A9 (and other out-of-order) implementations,
246/// the 's' 16-bit instruction partially update CPSR. Abort the
247/// transformation to avoid adding false dependency on last CPSR setting
248/// instruction which hurts the ability for out-of-order execution engine
249/// to do register renaming magic.
250/// This function checks if there is a read-of-write dependency between the
251/// last instruction that defines the CPSR and the current instruction. If there
252/// is, then there is no harm done since the instruction cannot be retired
253/// before the CPSR setting instruction anyway.
254/// Note, we are not doing full dependency analysis here for the sake of compile
255/// time. We're not looking for cases like:
256/// r0 = muls ...
257/// r1 = add.w r0, ...
258/// ...
259/// = mul.w r1
260/// In this case it would have been ok to narrow the mul.w to muls since there
261/// are indirect RAW dependency between the muls and the mul.w
262bool
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000263Thumb2SizeReduce::canAddPseudoFlagDep(MachineInstr *Use, bool FirstInSelfLoop) {
Quentin Colombet23b404d2012-12-18 22:47:16 +0000264 // Disable the check for -Oz (aka OptimizeForSizeHarder).
265 if (MinimizeSize || !STI->avoidCPSRPartialUpdate())
Bob Wilsona2881ee2011-04-19 18:11:49 +0000266 return false;
267
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000268 if (!CPSRDef)
Evan Chengf4807a12011-10-27 21:21:05 +0000269 // If this BB loops back to itself, conservatively avoid narrowing the
270 // first instruction that does partial flag update.
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000271 return HighLatencyCPSR || FirstInSelfLoop;
Evan Chengf4807a12011-10-27 21:21:05 +0000272
Bob Wilsona2881ee2011-04-19 18:11:49 +0000273 SmallSet<unsigned, 2> Defs;
Owen Anderson8c1f17b2014-03-07 22:48:22 +0000274 for (const MachineOperand &MO : CPSRDef->operands()) {
Bob Wilsona2881ee2011-04-19 18:11:49 +0000275 if (!MO.isReg() || MO.isUndef() || MO.isUse())
276 continue;
277 unsigned Reg = MO.getReg();
278 if (Reg == 0 || Reg == ARM::CPSR)
279 continue;
280 Defs.insert(Reg);
281 }
282
Owen Anderson8c1f17b2014-03-07 22:48:22 +0000283 for (const MachineOperand &MO : Use->operands()) {
Bob Wilsona2881ee2011-04-19 18:11:49 +0000284 if (!MO.isReg() || MO.isUndef() || MO.isDef())
285 continue;
286 unsigned Reg = MO.getReg();
287 if (Defs.count(Reg))
288 return false;
289 }
290
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000291 // If the current CPSR has high latency, try to avoid the false dependency.
292 if (HighLatencyCPSR)
293 return true;
294
295 // tMOVi8 usually doesn't start long dependency chains, and there are a lot
296 // of them, so always shrink them when CPSR doesn't have high latency.
297 if (Use->getOpcode() == ARM::t2MOVi ||
298 Use->getOpcode() == ARM::t2MOVi16)
299 return false;
300
Bob Wilsona2881ee2011-04-19 18:11:49 +0000301 // No read-after-write dependency. The narrowing will add false dependency.
302 return true;
303}
304
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000305bool
306Thumb2SizeReduce::VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
307 bool is2Addr, ARMCC::CondCodes Pred,
308 bool LiveCPSR, bool &HasCC, bool &CCDead) {
Evan Chengd461c1c2009-08-09 19:17:19 +0000309 if ((is2Addr && Entry.PredCC2 == 0) ||
310 (!is2Addr && Entry.PredCC1 == 0)) {
311 if (Pred == ARMCC::AL) {
312 // Not predicated, must set CPSR.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000313 if (!HasCC) {
314 // Original instruction was not setting CPSR, but CPSR is not
315 // currently live anyway. It's ok to set it. The CPSR def is
316 // dead though.
317 if (!LiveCPSR) {
318 HasCC = true;
319 CCDead = true;
320 return true;
321 }
322 return false;
323 }
Evan Chengd461c1c2009-08-09 19:17:19 +0000324 } else {
325 // Predicated, must not set CPSR.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000326 if (HasCC)
327 return false;
Evan Chengd461c1c2009-08-09 19:17:19 +0000328 }
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000329 } else if ((is2Addr && Entry.PredCC2 == 2) ||
330 (!is2Addr && Entry.PredCC1 == 2)) {
331 /// Old opcode has an optional def of CPSR.
332 if (HasCC)
333 return true;
Jim Grosbachbc7eeaf2010-09-14 20:35:46 +0000334 // If old opcode does not implicitly define CPSR, then it's not ok since
335 // these new opcodes' CPSR def is not meant to be thrown away. e.g. CMP.
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000336 if (!HasImplicitCPSRDef(MI->getDesc()))
337 return false;
338 HasCC = true;
Evan Chengd461c1c2009-08-09 19:17:19 +0000339 } else {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000340 // 16-bit instruction does not set CPSR.
341 if (HasCC)
342 return false;
Evan Chengd461c1c2009-08-09 19:17:19 +0000343 }
344
345 return true;
346}
347
Evan Chengcc9ca352009-08-11 21:11:32 +0000348static bool VerifyLowRegs(MachineInstr *MI) {
349 unsigned Opc = MI->getOpcode();
Peter Collingbourne85a0e232015-05-05 20:07:10 +0000350 bool isPCOk = (Opc == ARM::t2LDMIA_RET || Opc == ARM::t2LDMIA_UPD);
Tim Northoverba1d7042014-09-10 12:53:28 +0000351 bool isLROk = (Opc == ARM::t2STMDB_UPD);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000352 bool isSPOk = isPCOk || isLROk;
Evan Chengcc9ca352009-08-11 21:11:32 +0000353 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
354 const MachineOperand &MO = MI->getOperand(i);
355 if (!MO.isReg() || MO.isImplicit())
356 continue;
357 unsigned Reg = MO.getReg();
358 if (Reg == 0 || Reg == ARM::CPSR)
359 continue;
360 if (isPCOk && Reg == ARM::PC)
361 continue;
362 if (isLROk && Reg == ARM::LR)
363 continue;
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000364 if (Reg == ARM::SP) {
365 if (isSPOk)
366 continue;
367 if (i == 1 && (Opc == ARM::t2LDRi12 || Opc == ARM::t2STRi12))
368 // Special case for these ldr / str with sp as base register.
369 continue;
370 }
Evan Chengcc9ca352009-08-11 21:11:32 +0000371 if (!isARMLowRegister(Reg))
372 return false;
373 }
374 return true;
375}
376
Evan Cheng1be453b2009-08-08 03:21:23 +0000377bool
Evan Cheng36064672009-08-11 08:52:18 +0000378Thumb2SizeReduce::ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
379 const ReduceEntry &Entry) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000380 if (ReduceLimitLdSt != -1 && ((int)NumLdSts >= ReduceLimitLdSt))
381 return false;
382
Evan Cheng36064672009-08-11 08:52:18 +0000383 unsigned Scale = 1;
384 bool HasImmOffset = false;
385 bool HasShift = false;
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000386 bool HasOffReg = true;
Evan Chengcc9ca352009-08-11 21:11:32 +0000387 bool isLdStMul = false;
Evan Chengcc9ca352009-08-11 21:11:32 +0000388 unsigned Opc = Entry.NarrowOpc1;
389 unsigned OpNum = 3; // First 'rest' of operands.
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000390 uint8_t ImmLimit = Entry.Imm1Limit;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000391
Evan Cheng36064672009-08-11 08:52:18 +0000392 switch (Entry.WideOpc) {
393 default:
394 llvm_unreachable("Unexpected Thumb2 load / store opcode!");
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000395 case ARM::t2LDRi12:
Bill Wendling092a7bd2010-12-14 03:36:38 +0000396 case ARM::t2STRi12:
397 if (MI->getOperand(1).getReg() == ARM::SP) {
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000398 Opc = Entry.NarrowOpc2;
399 ImmLimit = Entry.Imm2Limit;
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000400 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000401
Evan Cheng36064672009-08-11 08:52:18 +0000402 Scale = 4;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000403 HasImmOffset = true;
404 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000405 break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000406 case ARM::t2LDRBi12:
Evan Cheng36064672009-08-11 08:52:18 +0000407 case ARM::t2STRBi12:
Owen Anderson4ebf4712011-02-08 22:39:40 +0000408 HasImmOffset = true;
409 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000410 break;
411 case ARM::t2LDRHi12:
412 case ARM::t2STRHi12:
413 Scale = 2;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000414 HasImmOffset = true;
415 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000416 break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000417 case ARM::t2LDRs:
418 case ARM::t2LDRBs:
419 case ARM::t2LDRHs:
Evan Cheng36064672009-08-11 08:52:18 +0000420 case ARM::t2LDRSBs:
421 case ARM::t2LDRSHs:
422 case ARM::t2STRs:
423 case ARM::t2STRBs:
424 case ARM::t2STRHs:
425 HasShift = true;
Evan Chengcc9ca352009-08-11 21:11:32 +0000426 OpNum = 4;
Evan Cheng36064672009-08-11 08:52:18 +0000427 break;
James Molloy53298a12016-06-07 12:13:34 +0000428 case ARM::t2LDR_POST:
429 case ARM::t2STR_POST: {
430 if (!MBB.getParent()->getFunction()->optForMinSize())
431 return false;
432
Tim Northovereaee28b2016-09-19 09:11:09 +0000433 if (!MI->hasOneMemOperand() ||
434 (*MI->memoperands_begin())->getAlignment() < 4)
435 return false;
436
James Molloy53298a12016-06-07 12:13:34 +0000437 // We're creating a completely different type of load/store - LDM from LDR.
438 // For this reason we can't reuse the logic at the end of this function; we
439 // have to implement the MI building here.
440 bool IsStore = Entry.WideOpc == ARM::t2STR_POST;
441 unsigned Rt = MI->getOperand(IsStore ? 1 : 0).getReg();
442 unsigned Rn = MI->getOperand(IsStore ? 0 : 1).getReg();
443 unsigned Offset = MI->getOperand(3).getImm();
444 unsigned PredImm = MI->getOperand(4).getImm();
445 unsigned PredReg = MI->getOperand(5).getReg();
446 assert(isARMLowRegister(Rt));
447 assert(isARMLowRegister(Rn));
448
449 if (Offset != 4)
450 return false;
451
452 // Add the 16-bit load / store instruction.
453 DebugLoc dl = MI->getDebugLoc();
454 auto MIB = BuildMI(MBB, MI, dl, TII->get(Entry.NarrowOpc1))
455 .addReg(Rn, RegState::Define)
456 .addReg(Rn)
457 .addImm(PredImm)
458 .addReg(PredReg)
459 .addReg(Rt, IsStore ? 0 : RegState::Define);
460
461 // Transfer memoperands.
462 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
463
464 // Transfer MI flags.
465 MIB.setMIFlags(MI->getFlags());
466
467 // Kill the old instruction.
Tim Northover28a9e7f2016-06-17 18:40:46 +0000468 MI->eraseFromBundle();
James Molloy53298a12016-06-07 12:13:34 +0000469 ++NumLdSts;
470 return true;
471 }
Peter Collingbourne85a0e232015-05-05 20:07:10 +0000472 case ARM::t2LDMIA: {
Evan Chengcc9ca352009-08-11 21:11:32 +0000473 unsigned BaseReg = MI->getOperand(0).getReg();
Peter Collingbourne85a0e232015-05-05 20:07:10 +0000474 assert(isARMLowRegister(BaseReg));
Bill Wendling092a7bd2010-12-14 03:36:38 +0000475
Jim Grosbach88628e92010-09-07 22:30:53 +0000476 // For the non-writeback version (this one), the base register must be
477 // one of the registers being loaded.
478 bool isOK = false;
Peter Collingbourne85a0e232015-05-05 20:07:10 +0000479 for (unsigned i = 3; i < MI->getNumOperands(); ++i) {
Jim Grosbach88628e92010-09-07 22:30:53 +0000480 if (MI->getOperand(i).getReg() == BaseReg) {
481 isOK = true;
482 break;
483 }
484 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000485
Jim Grosbach88628e92010-09-07 22:30:53 +0000486 if (!isOK)
487 return false;
488
Bob Wilson947f04b2010-03-13 01:08:20 +0000489 OpNum = 0;
490 isLdStMul = true;
491 break;
492 }
Scott Douglass953f9082015-10-05 14:49:54 +0000493 case ARM::t2STMIA: {
494 // If the base register is killed, we don't care what its value is after the
495 // instruction, so we can use an updating STMIA.
496 if (!MI->getOperand(0).isKill())
497 return false;
498
499 break;
500 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000501 case ARM::t2LDMIA_RET: {
Bob Wilson947f04b2010-03-13 01:08:20 +0000502 unsigned BaseReg = MI->getOperand(1).getReg();
503 if (BaseReg != ARM::SP)
504 return false;
505 Opc = Entry.NarrowOpc2; // tPOP_RET
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000506 OpNum = 2;
Bob Wilson947f04b2010-03-13 01:08:20 +0000507 isLdStMul = true;
508 break;
509 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000510 case ARM::t2LDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000511 case ARM::t2STMIA_UPD:
512 case ARM::t2STMDB_UPD: {
Bob Wilson947f04b2010-03-13 01:08:20 +0000513 OpNum = 0;
Bill Wendling092a7bd2010-12-14 03:36:38 +0000514
Bob Wilson947f04b2010-03-13 01:08:20 +0000515 unsigned BaseReg = MI->getOperand(1).getReg();
Bob Wilson947f04b2010-03-13 01:08:20 +0000516 if (BaseReg == ARM::SP &&
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000517 (Entry.WideOpc == ARM::t2LDMIA_UPD ||
518 Entry.WideOpc == ARM::t2STMDB_UPD)) {
Bob Wilson947f04b2010-03-13 01:08:20 +0000519 Opc = Entry.NarrowOpc2; // tPOP or tPUSH
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000520 OpNum = 2;
521 } else if (!isARMLowRegister(BaseReg) ||
522 (Entry.WideOpc != ARM::t2LDMIA_UPD &&
523 Entry.WideOpc != ARM::t2STMIA_UPD)) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000524 return false;
525 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000526
Evan Chengcc9ca352009-08-11 21:11:32 +0000527 isLdStMul = true;
528 break;
529 }
Evan Cheng36064672009-08-11 08:52:18 +0000530 }
531
532 unsigned OffsetReg = 0;
533 bool OffsetKill = false;
Pete Cooperf68d5032015-05-01 18:57:32 +0000534 bool OffsetInternal = false;
Evan Cheng36064672009-08-11 08:52:18 +0000535 if (HasShift) {
536 OffsetReg = MI->getOperand(2).getReg();
537 OffsetKill = MI->getOperand(2).isKill();
Pete Cooperf68d5032015-05-01 18:57:32 +0000538 OffsetInternal = MI->getOperand(2).isInternalRead();
Bill Wendling092a7bd2010-12-14 03:36:38 +0000539
Evan Cheng36064672009-08-11 08:52:18 +0000540 if (MI->getOperand(3).getImm())
541 // Thumb1 addressing mode doesn't support shift.
542 return false;
543 }
544
545 unsigned OffsetImm = 0;
546 if (HasImmOffset) {
547 OffsetImm = MI->getOperand(2).getImm();
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000548 unsigned MaxOffset = ((1 << ImmLimit) - 1) * Scale;
Bill Wendling092a7bd2010-12-14 03:36:38 +0000549
550 if ((OffsetImm & (Scale - 1)) || OffsetImm > MaxOffset)
Evan Cheng36064672009-08-11 08:52:18 +0000551 // Make sure the immediate field fits.
552 return false;
553 }
554
555 // Add the 16-bit load / store instruction.
Evan Cheng36064672009-08-11 08:52:18 +0000556 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000557 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, TII->get(Opc));
Scott Douglass953f9082015-10-05 14:49:54 +0000558
559 // tSTMIA_UPD takes a defining register operand. We've already checked that
560 // the register is killed, so mark it as dead here.
561 if (Entry.WideOpc == ARM::t2STMIA)
562 MIB.addReg(MI->getOperand(0).getReg(), RegState::Define | RegState::Dead);
563
Evan Chengcc9ca352009-08-11 21:11:32 +0000564 if (!isLdStMul) {
Owen Anderson99ea8a32010-12-07 00:45:21 +0000565 MIB.addOperand(MI->getOperand(0));
Owen Anderson4ebf4712011-02-08 22:39:40 +0000566 MIB.addOperand(MI->getOperand(1));
Bill Wendling092a7bd2010-12-14 03:36:38 +0000567
568 if (HasImmOffset)
569 MIB.addImm(OffsetImm / Scale);
570
Evan Chengcc9ca352009-08-11 21:11:32 +0000571 assert((!HasShift || OffsetReg) && "Invalid so_reg load / store address!");
572
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000573 if (HasOffReg)
Pete Cooperf68d5032015-05-01 18:57:32 +0000574 MIB.addReg(OffsetReg, getKillRegState(OffsetKill) |
575 getInternalReadRegState(OffsetInternal));
Evan Cheng36064672009-08-11 08:52:18 +0000576 }
Evan Cheng806845d2009-08-11 09:37:40 +0000577
Evan Cheng36064672009-08-11 08:52:18 +0000578 // Transfer the rest of operands.
Evan Cheng36064672009-08-11 08:52:18 +0000579 for (unsigned e = MI->getNumOperands(); OpNum != e; ++OpNum)
580 MIB.addOperand(MI->getOperand(OpNum));
581
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000582 // Transfer memoperands.
Chris Lattner1d0c2572011-04-29 05:24:29 +0000583 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000584
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000585 // Transfer MI flags.
586 MIB.setMIFlags(MI->getFlags());
587
Chris Lattnera6f074f2009-08-23 03:41:05 +0000588 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Cheng36064672009-08-11 08:52:18 +0000589
Evan Cheng7fae11b2011-12-14 02:11:42 +0000590 MBB.erase_instr(MI);
Evan Cheng36064672009-08-11 08:52:18 +0000591 ++NumLdSts;
592 return true;
593}
594
Evan Cheng36064672009-08-11 08:52:18 +0000595bool
596Thumb2SizeReduce::ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
597 const ReduceEntry &Entry,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000598 bool LiveCPSR, bool IsSelfLoop) {
Jim Grosbacha8a80672011-06-29 23:25:04 +0000599 unsigned Opc = MI->getOpcode();
600 if (Opc == ARM::t2ADDri) {
601 // If the source register is SP, try to reduce to tADDrSPi, otherwise
602 // it's a normal reduce.
603 if (MI->getOperand(1).getReg() != ARM::SP) {
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000604 if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
Jim Grosbacha8a80672011-06-29 23:25:04 +0000605 return true;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000606 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000607 }
608 // Try to reduce to tADDrSPi.
609 unsigned Imm = MI->getOperand(2).getImm();
610 // The immediate must be in range, the destination register must be a low
Jim Grosbached5134a2011-06-30 02:22:49 +0000611 // reg, the predicate must be "always" and the condition flags must not
612 // be being set.
Jim Grosbach68b0e842011-07-01 19:07:09 +0000613 if (Imm & 3 || Imm > 1020)
Jim Grosbacha8a80672011-06-29 23:25:04 +0000614 return false;
615 if (!isARMLowRegister(MI->getOperand(0).getReg()))
616 return false;
Jim Grosbached5134a2011-06-30 02:22:49 +0000617 if (MI->getOperand(3).getImm() != ARMCC::AL)
618 return false;
Jim Grosbacha8a80672011-06-29 23:25:04 +0000619 const MCInstrDesc &MCID = MI->getDesc();
620 if (MCID.hasOptionalDef() &&
621 MI->getOperand(MCID.getNumOperands()-1).getReg() == ARM::CPSR)
622 return false;
623
Diana Picus4f8c3e12017-01-13 09:37:56 +0000624 MachineInstrBuilder MIB =
625 BuildMI(MBB, MI, MI->getDebugLoc(),
626 TII->get(ARM::tADDrSPi))
627 .addOperand(MI->getOperand(0))
628 .addOperand(MI->getOperand(1))
629 .addImm(Imm / 4) // The tADDrSPi has an implied scale by four.
630 .add(predOps(ARMCC::AL));
Jim Grosbacha8a80672011-06-29 23:25:04 +0000631
632 // Transfer MI flags.
633 MIB.setMIFlags(MI->getFlags());
634
635 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " <<*MIB);
636
Evan Cheng7fae11b2011-12-14 02:11:42 +0000637 MBB.erase_instr(MI);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000638 ++NumNarrows;
639 return true;
640 }
641
Evan Chengcc9ca352009-08-11 21:11:32 +0000642 if (Entry.LowRegs1 && !VerifyLowRegs(MI))
Evan Cheng36064672009-08-11 08:52:18 +0000643 return false;
644
Chad Rosier67336302015-05-22 20:07:34 +0000645 if (MI->mayLoadOrStore())
Evan Cheng36064672009-08-11 08:52:18 +0000646 return ReduceLoadStore(MBB, MI, Entry);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000647
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000648 switch (Opc) {
649 default: break;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000650 case ARM::t2ADDSri:
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000651 case ARM::t2ADDSrr: {
652 unsigned PredReg = 0;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000653 if (getInstrPredicate(*MI, PredReg) == ARMCC::AL) {
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000654 switch (Opc) {
655 default: break;
656 case ARM::t2ADDSri: {
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000657 if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000658 return true;
Justin Bognerb03fd122016-08-17 05:10:15 +0000659 LLVM_FALLTHROUGH;
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000660 }
661 case ARM::t2ADDSrr:
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000662 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000663 }
664 }
665 break;
666 }
667 case ARM::t2RSBri:
668 case ARM::t2RSBSri:
Jim Grosbach8b31ef52011-07-27 16:47:19 +0000669 case ARM::t2SXTB:
670 case ARM::t2SXTH:
671 case ARM::t2UXTB:
672 case ARM::t2UXTH:
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000673 if (MI->getOperand(2).getImm() == 0)
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000674 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000675 break;
Anton Korobeynikov25229082009-11-24 00:44:37 +0000676 case ARM::t2MOVi16:
677 // Can convert only 'pure' immediate operands, not immediates obtained as
678 // globals' addresses.
679 if (MI->getOperand(1).isImm())
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000680 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Anton Korobeynikov25229082009-11-24 00:44:37 +0000681 break;
Jim Grosbach327cf8e2010-12-07 20:41:06 +0000682 case ARM::t2CMPrr: {
Jim Grosbach5bae0542010-12-03 23:54:18 +0000683 // Try to reduce to the lo-reg only version first. Why there are two
684 // versions of the instruction is a mystery.
685 // It would be nice to just have two entries in the master table that
686 // are prioritized, but the table assumes a unique entry for each
687 // source insn opcode. So for now, we hack a local entry record to use.
688 static const ReduceEntry NarrowEntry =
Evan Chengddc0cb62012-12-20 19:59:30 +0000689 { ARM::t2CMPrr,ARM::tCMPr, 0, 0, 0, 1, 1,2, 0, 0,1,0 };
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000690 if (ReduceToNarrow(MBB, MI, NarrowEntry, LiveCPSR, IsSelfLoop))
Jim Grosbach5bae0542010-12-03 23:54:18 +0000691 return true;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000692 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Jim Grosbach5bae0542010-12-03 23:54:18 +0000693 }
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000694 }
Evan Cheng36064672009-08-11 08:52:18 +0000695 return false;
696}
697
698bool
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000699Thumb2SizeReduce::ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
700 const ReduceEntry &Entry,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000701 bool LiveCPSR, bool IsSelfLoop) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000702
703 if (ReduceLimit2Addr != -1 && ((int)Num2Addrs >= ReduceLimit2Addr))
704 return false;
705
Sanjay Patel924879a2015-08-04 15:49:57 +0000706 if (!OptimizeSize && Entry.AvoidMovs && STI->avoidMOVsShifterOperand())
Evan Chengddc0cb62012-12-20 19:59:30 +0000707 // Don't issue movs with shifter operand for some CPUs unless we
Sanjay Patel924879a2015-08-04 15:49:57 +0000708 // are optimizing for size.
Evan Chengddc0cb62012-12-20 19:59:30 +0000709 return false;
710
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000711 unsigned Reg0 = MI->getOperand(0).getReg();
712 unsigned Reg1 = MI->getOperand(1).getReg();
Jim Grosbachc01104d2012-02-24 00:33:36 +0000713 // t2MUL is "special". The tied source operand is second, not first.
714 if (MI->getOpcode() == ARM::t2MUL) {
Jim Grosbach3a21e2c2012-02-24 00:53:11 +0000715 unsigned Reg2 = MI->getOperand(2).getReg();
716 // Early exit if the regs aren't all low regs.
717 if (!isARMLowRegister(Reg0) || !isARMLowRegister(Reg1)
718 || !isARMLowRegister(Reg2))
719 return false;
720 if (Reg0 != Reg2) {
Jim Grosbachc01104d2012-02-24 00:33:36 +0000721 // If the other operand also isn't the same as the destination, we
722 // can't reduce.
723 if (Reg1 != Reg0)
724 return false;
725 // Try to commute the operands to make it a 2-address instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000726 MachineInstr *CommutedMI = TII->commuteInstruction(*MI);
Jim Grosbachc01104d2012-02-24 00:33:36 +0000727 if (!CommutedMI)
728 return false;
729 }
730 } else if (Reg0 != Reg1) {
Bob Wilson279e55f2010-06-24 16:50:20 +0000731 // Try to commute the operands to make it a 2-address instruction.
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000732 unsigned CommOpIdx1 = 1;
733 unsigned CommOpIdx2 = TargetInstrInfo::CommuteAnyOperandIndex;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000734 if (!TII->findCommutedOpIndices(*MI, CommOpIdx1, CommOpIdx2) ||
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000735 MI->getOperand(CommOpIdx2).getReg() != Reg0)
Bob Wilson279e55f2010-06-24 16:50:20 +0000736 return false;
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000737 MachineInstr *CommutedMI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000738 TII->commuteInstruction(*MI, false, CommOpIdx1, CommOpIdx2);
Bob Wilson279e55f2010-06-24 16:50:20 +0000739 if (!CommutedMI)
740 return false;
741 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000742 if (Entry.LowRegs2 && !isARMLowRegister(Reg0))
743 return false;
744 if (Entry.Imm2Limit) {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000745 unsigned Imm = MI->getOperand(2).getImm();
Evan Cheng1be453b2009-08-08 03:21:23 +0000746 unsigned Limit = (1 << Entry.Imm2Limit) - 1;
747 if (Imm > Limit)
748 return false;
749 } else {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000750 unsigned Reg2 = MI->getOperand(2).getReg();
Evan Cheng1be453b2009-08-08 03:21:23 +0000751 if (Entry.LowRegs2 && !isARMLowRegister(Reg2))
752 return false;
753 }
754
Evan Cheng1f5bee12009-08-10 06:57:42 +0000755 // Check if it's possible / necessary to transfer the predicate.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000756 const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc2);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000757 unsigned PredReg = 0;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000758 ARMCC::CondCodes Pred = getInstrPredicate(*MI, PredReg);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000759 bool SkipPred = false;
760 if (Pred != ARMCC::AL) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000761 if (!NewMCID.isPredicable())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000762 // Can't transfer predicate, fail.
763 return false;
764 } else {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000765 SkipPred = !NewMCID.isPredicable();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000766 }
767
Evan Cheng1be453b2009-08-08 03:21:23 +0000768 bool HasCC = false;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000769 bool CCDead = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000770 const MCInstrDesc &MCID = MI->getDesc();
771 if (MCID.hasOptionalDef()) {
772 unsigned NumOps = MCID.getNumOperands();
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000773 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
774 if (HasCC && MI->getOperand(NumOps-1).isDead())
775 CCDead = true;
776 }
Evan Cheng1f5bee12009-08-10 06:57:42 +0000777 if (!VerifyPredAndCC(MI, Entry, true, Pred, LiveCPSR, HasCC, CCDead))
Evan Chengd461c1c2009-08-09 19:17:19 +0000778 return false;
Evan Cheng1be453b2009-08-08 03:21:23 +0000779
Bob Wilsona2881ee2011-04-19 18:11:49 +0000780 // Avoid adding a false dependency on partial flag update by some 16-bit
781 // instructions which has the 's' bit set.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000782 if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000783 canAddPseudoFlagDep(MI, IsSelfLoop))
Bob Wilsona2881ee2011-04-19 18:11:49 +0000784 return false;
785
Evan Cheng1be453b2009-08-08 03:21:23 +0000786 // Add the 16-bit instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000787 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000788 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000789 MIB.addOperand(MI->getOperand(0));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000790 if (NewMCID.hasOptionalDef()) {
Evan Cheng6ddd7bc2009-08-15 07:59:10 +0000791 if (HasCC)
792 AddDefaultT1CC(MIB, CCDead);
793 else
794 AddNoT1CC(MIB);
795 }
Evan Chengd461c1c2009-08-09 19:17:19 +0000796
797 // Transfer the rest of operands.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000798 unsigned NumOps = MCID.getNumOperands();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000799 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000800 if (i < NumOps && MCID.OpInfo[i].isOptionalDef())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000801 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000802 if (SkipPred && MCID.OpInfo[i].isPredicate())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000803 continue;
804 MIB.addOperand(MI->getOperand(i));
805 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000806
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000807 // Transfer MI flags.
808 MIB.setMIFlags(MI->getFlags());
809
Chris Lattnera6f074f2009-08-23 03:41:05 +0000810 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Cheng1be453b2009-08-08 03:21:23 +0000811
Evan Cheng7fae11b2011-12-14 02:11:42 +0000812 MBB.erase_instr(MI);
Evan Cheng1be453b2009-08-08 03:21:23 +0000813 ++Num2Addrs;
Evan Cheng1be453b2009-08-08 03:21:23 +0000814 return true;
815}
816
817bool
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000818Thumb2SizeReduce::ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
819 const ReduceEntry &Entry,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000820 bool LiveCPSR, bool IsSelfLoop) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000821 if (ReduceLimit != -1 && ((int)NumNarrows >= ReduceLimit))
822 return false;
823
Sanjay Patel924879a2015-08-04 15:49:57 +0000824 if (!OptimizeSize && Entry.AvoidMovs && STI->avoidMOVsShifterOperand())
Evan Chengddc0cb62012-12-20 19:59:30 +0000825 // Don't issue movs with shifter operand for some CPUs unless we
Sanjay Patel924879a2015-08-04 15:49:57 +0000826 // are optimizing for size.
Evan Chengddc0cb62012-12-20 19:59:30 +0000827 return false;
828
Evan Chengd461c1c2009-08-09 19:17:19 +0000829 unsigned Limit = ~0U;
830 if (Entry.Imm1Limit)
Jim Grosbacha8a80672011-06-29 23:25:04 +0000831 Limit = (1 << Entry.Imm1Limit) - 1;
Evan Chengd461c1c2009-08-09 19:17:19 +0000832
Evan Cheng6cc775f2011-06-28 19:10:37 +0000833 const MCInstrDesc &MCID = MI->getDesc();
834 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i) {
835 if (MCID.OpInfo[i].isPredicate())
Evan Chengd461c1c2009-08-09 19:17:19 +0000836 continue;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000837 const MachineOperand &MO = MI->getOperand(i);
Evan Chengd461c1c2009-08-09 19:17:19 +0000838 if (MO.isReg()) {
839 unsigned Reg = MO.getReg();
840 if (!Reg || Reg == ARM::CPSR)
841 continue;
842 if (Entry.LowRegs1 && !isARMLowRegister(Reg))
843 return false;
Evan Chengf6a9d062009-08-11 23:00:31 +0000844 } else if (MO.isImm() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +0000845 !MCID.OpInfo[i].isPredicate()) {
Jim Grosbacha8a80672011-06-29 23:25:04 +0000846 if (((unsigned)MO.getImm()) > Limit)
Evan Chengd461c1c2009-08-09 19:17:19 +0000847 return false;
848 }
849 }
850
Evan Cheng1f5bee12009-08-10 06:57:42 +0000851 // Check if it's possible / necessary to transfer the predicate.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000852 const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc1);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000853 unsigned PredReg = 0;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000854 ARMCC::CondCodes Pred = getInstrPredicate(*MI, PredReg);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000855 bool SkipPred = false;
856 if (Pred != ARMCC::AL) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000857 if (!NewMCID.isPredicable())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000858 // Can't transfer predicate, fail.
859 return false;
860 } else {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000861 SkipPred = !NewMCID.isPredicable();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000862 }
863
Evan Chengd461c1c2009-08-09 19:17:19 +0000864 bool HasCC = false;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000865 bool CCDead = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000866 if (MCID.hasOptionalDef()) {
867 unsigned NumOps = MCID.getNumOperands();
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000868 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
869 if (HasCC && MI->getOperand(NumOps-1).isDead())
870 CCDead = true;
871 }
Evan Cheng1f5bee12009-08-10 06:57:42 +0000872 if (!VerifyPredAndCC(MI, Entry, false, Pred, LiveCPSR, HasCC, CCDead))
Evan Chengd461c1c2009-08-09 19:17:19 +0000873 return false;
874
Bob Wilsona2881ee2011-04-19 18:11:49 +0000875 // Avoid adding a false dependency on partial flag update by some 16-bit
876 // instructions which has the 's' bit set.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000877 if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000878 canAddPseudoFlagDep(MI, IsSelfLoop))
Bob Wilsona2881ee2011-04-19 18:11:49 +0000879 return false;
880
Evan Chengd461c1c2009-08-09 19:17:19 +0000881 // Add the 16-bit instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000882 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000883 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000884 MIB.addOperand(MI->getOperand(0));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000885 if (NewMCID.hasOptionalDef()) {
Evan Cheng6ddd7bc2009-08-15 07:59:10 +0000886 if (HasCC)
887 AddDefaultT1CC(MIB, CCDead);
888 else
889 AddNoT1CC(MIB);
890 }
Evan Chengd461c1c2009-08-09 19:17:19 +0000891
892 // Transfer the rest of operands.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000893 unsigned NumOps = MCID.getNumOperands();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000894 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000895 if (i < NumOps && MCID.OpInfo[i].isOptionalDef())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000896 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000897 if ((MCID.getOpcode() == ARM::t2RSBSri ||
Jim Grosbach8b31ef52011-07-27 16:47:19 +0000898 MCID.getOpcode() == ARM::t2RSBri ||
899 MCID.getOpcode() == ARM::t2SXTB ||
900 MCID.getOpcode() == ARM::t2SXTH ||
901 MCID.getOpcode() == ARM::t2UXTB ||
902 MCID.getOpcode() == ARM::t2UXTH) && i == 2)
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000903 // Skip the zero immediate operand, it's now implicit.
904 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000905 bool isPred = (i < NumOps && MCID.OpInfo[i].isPredicate());
Evan Chengf6a9d062009-08-11 23:00:31 +0000906 if (SkipPred && isPred)
907 continue;
908 const MachineOperand &MO = MI->getOperand(i);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000909 if (MO.isReg() && MO.isImplicit() && MO.getReg() == ARM::CPSR)
910 // Skip implicit def of CPSR. Either it's modeled as an optional
911 // def now or it's already an implicit def on the new instruction.
912 continue;
913 MIB.addOperand(MO);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000914 }
Evan Cheng6cc775f2011-06-28 19:10:37 +0000915 if (!MCID.isPredicable() && NewMCID.isPredicable())
Diana Picus4f8c3e12017-01-13 09:37:56 +0000916 MIB.add(predOps(ARMCC::AL));
Evan Chengd461c1c2009-08-09 19:17:19 +0000917
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000918 // Transfer MI flags.
919 MIB.setMIFlags(MI->getFlags());
920
Chris Lattnera6f074f2009-08-23 03:41:05 +0000921 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Chengd461c1c2009-08-09 19:17:19 +0000922
Evan Cheng7fae11b2011-12-14 02:11:42 +0000923 MBB.erase_instr(MI);
Evan Chengd461c1c2009-08-09 19:17:19 +0000924 ++NumNarrows;
925 return true;
Evan Cheng1be453b2009-08-08 03:21:23 +0000926}
927
Bob Wilsona2881ee2011-04-19 18:11:49 +0000928static bool UpdateCPSRDef(MachineInstr &MI, bool LiveCPSR, bool &DefCPSR) {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000929 bool HasDef = false;
Owen Anderson8c1f17b2014-03-07 22:48:22 +0000930 for (const MachineOperand &MO : MI.operands()) {
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000931 if (!MO.isReg() || MO.isUndef() || MO.isUse())
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000932 continue;
933 if (MO.getReg() != ARM::CPSR)
934 continue;
Bob Wilsona2881ee2011-04-19 18:11:49 +0000935
936 DefCPSR = true;
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000937 if (!MO.isDead())
938 HasDef = true;
939 }
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000940
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000941 return HasDef || LiveCPSR;
942}
943
944static bool UpdateCPSRUse(MachineInstr &MI, bool LiveCPSR) {
Owen Anderson8c1f17b2014-03-07 22:48:22 +0000945 for (const MachineOperand &MO : MI.operands()) {
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000946 if (!MO.isReg() || MO.isUndef() || MO.isDef())
947 continue;
948 if (MO.getReg() != ARM::CPSR)
949 continue;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000950 assert(LiveCPSR && "CPSR liveness tracking is wrong!");
951 if (MO.isKill()) {
952 LiveCPSR = false;
953 break;
954 }
955 }
956
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000957 return LiveCPSR;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000958}
959
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000960bool Thumb2SizeReduce::ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000961 bool LiveCPSR, bool IsSelfLoop) {
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000962 unsigned Opcode = MI->getOpcode();
963 DenseMap<unsigned, unsigned>::iterator OPI = ReduceOpcodeMap.find(Opcode);
964 if (OPI == ReduceOpcodeMap.end())
965 return false;
966 const ReduceEntry &Entry = ReduceTable[OPI->second];
967
968 // Don't attempt normal reductions on "special" cases for now.
969 if (Entry.Special)
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000970 return ReduceSpecial(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000971
972 // Try to transform to a 16-bit two-address instruction.
973 if (Entry.NarrowOpc2 &&
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000974 ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000975 return true;
976
977 // Try to transform to a 16-bit non-two-address instruction.
978 if (Entry.NarrowOpc1 &&
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000979 ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000980 return true;
981
982 return false;
983}
984
Evan Cheng1be453b2009-08-08 03:21:23 +0000985bool Thumb2SizeReduce::ReduceMBB(MachineBasicBlock &MBB) {
986 bool Modified = false;
987
Evan Cheng1f5bee12009-08-10 06:57:42 +0000988 // Yes, CPSR could be livein.
Dan Gohmana1cf9fe2010-04-13 16:53:51 +0000989 bool LiveCPSR = MBB.isLiveIn(ARM::CPSR);
Craig Topper062a2ba2014-04-25 05:30:21 +0000990 MachineInstr *BundleMI = nullptr;
Evan Cheng1f5bee12009-08-10 06:57:42 +0000991
Craig Topper062a2ba2014-04-25 05:30:21 +0000992 CPSRDef = nullptr;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000993 HighLatencyCPSR = false;
994
995 // Check predecessors for the latest CPSRDef.
Jim Grosbach537f3ed2014-04-04 02:11:03 +0000996 for (auto *Pred : MBB.predecessors()) {
997 const MBBInfo &PInfo = BlockInfo[Pred->getNumber()];
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000998 if (!PInfo.Visited) {
999 // Since blocks are visited in RPO, this must be a back-edge.
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001000 continue;
1001 }
1002 if (PInfo.HighLatencyCPSR) {
1003 HighLatencyCPSR = true;
1004 break;
1005 }
1006 }
1007
Evan Chengf4807a12011-10-27 21:21:05 +00001008 // If this BB loops back to itself, conservatively avoid narrowing the
1009 // first instruction that does partial flag update.
1010 bool IsSelfLoop = MBB.isSuccessor(&MBB);
Jim Grosbach0c509fa2012-04-06 23:43:50 +00001011 MachineBasicBlock::instr_iterator MII = MBB.instr_begin(),E = MBB.instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00001012 MachineBasicBlock::instr_iterator NextMII;
Evan Cheng1be453b2009-08-08 03:21:23 +00001013 for (; MII != E; MII = NextMII) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001014 NextMII = std::next(MII);
Evan Cheng1be453b2009-08-08 03:21:23 +00001015
Evan Cheng51cbd2d2009-08-10 02:37:24 +00001016 MachineInstr *MI = &*MII;
Evan Cheng7fae11b2011-12-14 02:11:42 +00001017 if (MI->isBundle()) {
1018 BundleMI = MI;
1019 continue;
1020 }
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001021 if (MI->isDebugValue())
1022 continue;
Evan Cheng7fae11b2011-12-14 02:11:42 +00001023
Evan Cheng1e6c2a12009-08-12 01:49:45 +00001024 LiveCPSR = UpdateCPSRUse(*MI, LiveCPSR);
1025
Jakob Stoklund Olesen41bbf9c2012-12-18 00:46:39 +00001026 // Does NextMII belong to the same bundle as MI?
1027 bool NextInSameBundle = NextMII != E && NextMII->isBundledWithPred();
1028
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001029 if (ReduceMI(MBB, MI, LiveCPSR, IsSelfLoop)) {
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +00001030 Modified = true;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001031 MachineBasicBlock::instr_iterator I = std::prev(NextMII);
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +00001032 MI = &*I;
Jakob Stoklund Olesen41bbf9c2012-12-18 00:46:39 +00001033 // Removing and reinserting the first instruction in a bundle will break
1034 // up the bundle. Fix the bundling if it was broken.
1035 if (NextInSameBundle && !NextMII->isBundledWithPred())
1036 NextMII->bundleWithPred();
Evan Cheng1be453b2009-08-08 03:21:23 +00001037 }
1038
Renato Golinf6ed8bb2016-05-12 12:33:33 +00001039 if (BundleMI && !NextInSameBundle && MI->isInsideBundle()) {
Evan Cheng7fae11b2011-12-14 02:11:42 +00001040 // FIXME: Since post-ra scheduler operates on bundles, the CPSR kill
1041 // marker is only on the BUNDLE instruction. Process the BUNDLE
1042 // instruction as we finish with the bundled instruction to work around
1043 // the inconsistency.
Evan Cheng903231b2011-12-17 01:25:34 +00001044 if (BundleMI->killsRegister(ARM::CPSR))
1045 LiveCPSR = false;
1046 MachineOperand *MO = BundleMI->findRegisterDefOperand(ARM::CPSR);
1047 if (MO && !MO->isDead())
1048 LiveCPSR = true;
Weiming Zhaof66be562014-01-13 18:47:54 +00001049 MO = BundleMI->findRegisterUseOperand(ARM::CPSR);
1050 if (MO && !MO->isKill())
1051 LiveCPSR = true;
Evan Cheng903231b2011-12-17 01:25:34 +00001052 }
Evan Cheng7fae11b2011-12-14 02:11:42 +00001053
Bob Wilsona2881ee2011-04-19 18:11:49 +00001054 bool DefCPSR = false;
1055 LiveCPSR = UpdateCPSRDef(*MI, LiveCPSR, DefCPSR);
Evan Cheng7f8e5632011-12-07 07:15:52 +00001056 if (MI->isCall()) {
Bob Wilsona2881ee2011-04-19 18:11:49 +00001057 // Calls don't really set CPSR.
Craig Topper062a2ba2014-04-25 05:30:21 +00001058 CPSRDef = nullptr;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001059 HighLatencyCPSR = false;
Evan Chengf4807a12011-10-27 21:21:05 +00001060 IsSelfLoop = false;
1061 } else if (DefCPSR) {
Bob Wilsona2881ee2011-04-19 18:11:49 +00001062 // This is the last CPSR defining instruction.
1063 CPSRDef = MI;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001064 HighLatencyCPSR = isHighLatencyCPSR(CPSRDef);
Evan Chengf4807a12011-10-27 21:21:05 +00001065 IsSelfLoop = false;
1066 }
Evan Cheng1be453b2009-08-08 03:21:23 +00001067 }
1068
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001069 MBBInfo &Info = BlockInfo[MBB.getNumber()];
1070 Info.HighLatencyCPSR = HighLatencyCPSR;
1071 Info.Visited = true;
Evan Cheng1be453b2009-08-08 03:21:23 +00001072 return Modified;
1073}
1074
1075bool Thumb2SizeReduce::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor1aa3cf72016-04-26 00:56:36 +00001076 if (PredicateFtor && !PredicateFtor(*MF.getFunction()))
Akira Hatanaka4a616192015-06-08 18:50:43 +00001077 return false;
1078
Eric Christopher1b21f002015-01-29 00:19:33 +00001079 STI = &static_cast<const ARMSubtarget &>(MF.getSubtarget());
Eric Christopher63b44882015-03-05 00:23:40 +00001080 if (STI->isThumb1Only() || STI->prefers32BitThumb())
1081 return false;
1082
Eric Christopher1b21f002015-01-29 00:19:33 +00001083 TII = static_cast<const Thumb2InstrInfo *>(STI->getInstrInfo());
Evan Cheng1be453b2009-08-08 03:21:23 +00001084
Sanjay Patel924879a2015-08-04 15:49:57 +00001085 // Optimizing / minimizing size? Minimizing size implies optimizing for size.
1086 OptimizeSize = MF.getFunction()->optForSize();
1087 MinimizeSize = MF.getFunction()->optForMinSize();
Quentin Colombet23b404d2012-12-18 22:47:16 +00001088
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001089 BlockInfo.clear();
1090 BlockInfo.resize(MF.getNumBlockIDs());
1091
1092 // Visit blocks in reverse post-order so LastCPSRDef is known for all
1093 // predecessors.
1094 ReversePostOrderTraversal<MachineFunction*> RPOT(&MF);
Evan Cheng1be453b2009-08-08 03:21:23 +00001095 bool Modified = false;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001096 for (ReversePostOrderTraversal<MachineFunction*>::rpo_iterator
1097 I = RPOT.begin(), E = RPOT.end(); I != E; ++I)
1098 Modified |= ReduceMBB(**I);
Evan Cheng1be453b2009-08-08 03:21:23 +00001099 return Modified;
1100}
1101
1102/// createThumb2SizeReductionPass - Returns an instance of the Thumb2 size
1103/// reduction pass.
Akira Hatanaka4a616192015-06-08 18:50:43 +00001104FunctionPass *llvm::createThumb2SizeReductionPass(
1105 std::function<bool(const Function &)> Ftor) {
Benjamin Kramerd3f4c052016-06-12 16:13:55 +00001106 return new Thumb2SizeReduce(std::move(Ftor));
Evan Cheng1be453b2009-08-08 03:21:23 +00001107}