blob: d911dd97b1ac7e6a7d7628af576c0ecdb8fb9fb8 [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"
Eugene Zelenko342257e2017-01-31 00:56:17 +000013#include "MCTargetDesc/ARMBaseInfo.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 Carruth6bda14b2017-06-06 11:49:48 +000017#include "llvm/ADT/STLExtras.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000018#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/Statistic.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000021#include "llvm/ADT/StringRef.h"
22#include "llvm/CodeGen/MachineBasicBlock.h"
23#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Cheng1be453b2009-08-08 03:21:23 +000025#include "llvm/CodeGen/MachineInstr.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000027#include "llvm/CodeGen/MachineOperand.h"
28#include "llvm/IR/DebugLoc.h"
29#include "llvm/IR/Function.h"
30#include "llvm/MC/MCInstrDesc.h"
31#include "llvm/MC/MCRegisterInfo.h"
Evan Chengf16a1d52009-08-10 07:20:37 +000032#include "llvm/Support/CommandLine.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000033#include "llvm/Support/Compiler.h"
Evan Cheng1be453b2009-08-08 03:21:23 +000034#include "llvm/Support/Debug.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000035#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000036#include "llvm/Support/raw_ostream.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000037#include "llvm/Target/TargetInstrInfo.h"
38#include <algorithm>
39#include <cassert>
40#include <cstdint>
41#include <functional>
42#include <iterator>
Benjamin Kramer82de7d32016-05-27 14:27:24 +000043#include <utility>
Eugene Zelenko342257e2017-01-31 00:56:17 +000044
Evan Cheng1be453b2009-08-08 03:21:23 +000045using namespace llvm;
46
Chandler Carruth84e68b22014-04-22 02:41:26 +000047#define DEBUG_TYPE "t2-reduce-size"
48
Evan Cheng1f5bee12009-08-10 06:57:42 +000049STATISTIC(NumNarrows, "Number of 32-bit instrs reduced to 16-bit ones");
50STATISTIC(Num2Addrs, "Number of 32-bit instrs reduced to 2addr 16-bit ones");
Evan Cheng36064672009-08-11 08:52:18 +000051STATISTIC(NumLdSts, "Number of 32-bit load / store reduced to 16-bit ones");
Evan Cheng1be453b2009-08-08 03:21:23 +000052
Evan Chengcc9ca352009-08-11 21:11:32 +000053static cl::opt<int> ReduceLimit("t2-reduce-limit",
54 cl::init(-1), cl::Hidden);
55static cl::opt<int> ReduceLimit2Addr("t2-reduce-limit2",
56 cl::init(-1), cl::Hidden);
57static cl::opt<int> ReduceLimitLdSt("t2-reduce-limit3",
58 cl::init(-1), cl::Hidden);
Evan Chengf16a1d52009-08-10 07:20:37 +000059
Evan Cheng1be453b2009-08-08 03:21:23 +000060namespace {
Eugene Zelenko342257e2017-01-31 00:56:17 +000061
Evan Cheng1be453b2009-08-08 03:21:23 +000062 /// ReduceTable - A static table with information on mapping from wide
63 /// opcodes to narrow
64 struct ReduceEntry {
Craig Topperca658c22012-03-11 07:16:55 +000065 uint16_t WideOpc; // Wide opcode
66 uint16_t NarrowOpc1; // Narrow opcode to transform to
67 uint16_t NarrowOpc2; // Narrow opcode when it's two-address
Evan Cheng1be453b2009-08-08 03:21:23 +000068 uint8_t Imm1Limit; // Limit of immediate field (bits)
69 uint8_t Imm2Limit; // Limit of immediate field when it's two-address
70 unsigned LowRegs1 : 1; // Only possible if low-registers are used
71 unsigned LowRegs2 : 1; // Only possible if low-registers are used (2addr)
Evan Cheng1e6c2a12009-08-12 01:49:45 +000072 unsigned PredCC1 : 2; // 0 - If predicated, cc is on and vice versa.
Evan Cheng1be453b2009-08-08 03:21:23 +000073 // 1 - No cc field.
Evan Cheng1e6c2a12009-08-12 01:49:45 +000074 // 2 - Always set CPSR.
Evan Chengaee7e492009-08-12 18:35:50 +000075 unsigned PredCC2 : 2;
Bob Wilsona2881ee2011-04-19 18:11:49 +000076 unsigned PartFlag : 1; // 16-bit instruction does partial flag update
Evan Cheng1be453b2009-08-08 03:21:23 +000077 unsigned Special : 1; // Needs to be dealt with specially
Evan Chengddc0cb62012-12-20 19:59:30 +000078 unsigned AvoidMovs: 1; // Avoid movs with shifter operand (for Swift)
Evan Cheng1be453b2009-08-08 03:21:23 +000079 };
80
81 static const ReduceEntry ReduceTable[] = {
Evan Chengddc0cb62012-12-20 19:59:30 +000082 // Wide, Narrow1, Narrow2, imm1,imm2, lo1, lo2, P/C,PF,S,AM
83 { ARM::t2ADCrr, 0, ARM::tADC, 0, 0, 0, 1, 0,0, 0,0,0 },
84 { ARM::t2ADDri, ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 0,0, 0,1,0 },
85 { ARM::t2ADDrr, ARM::tADDrr, ARM::tADDhirr, 0, 0, 1, 0, 0,1, 0,0,0 },
86 { ARM::t2ADDSri,ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 2,2, 0,1,0 },
87 { ARM::t2ADDSrr,ARM::tADDrr, 0, 0, 0, 1, 0, 2,0, 0,1,0 },
88 { ARM::t2ANDrr, 0, ARM::tAND, 0, 0, 0, 1, 0,0, 1,0,0 },
89 { ARM::t2ASRri, ARM::tASRri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
90 { ARM::t2ASRrr, 0, ARM::tASRrr, 0, 0, 0, 1, 0,0, 1,0,1 },
91 { ARM::t2BICrr, 0, ARM::tBIC, 0, 0, 0, 1, 0,0, 1,0,0 },
92 //FIXME: Disable CMN, as CCodes are backwards from compare expectations
93 //{ ARM::t2CMNrr, ARM::tCMN, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
94 { ARM::t2CMNzrr, ARM::tCMNz, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
95 { ARM::t2CMPri, ARM::tCMPi8, 0, 8, 0, 1, 0, 2,0, 0,0,0 },
96 { ARM::t2CMPrr, ARM::tCMPhir, 0, 0, 0, 0, 0, 2,0, 0,1,0 },
97 { ARM::t2EORrr, 0, ARM::tEOR, 0, 0, 0, 1, 0,0, 1,0,0 },
98 // FIXME: adr.n immediate offset must be multiple of 4.
99 //{ ARM::t2LEApcrelJT,ARM::tLEApcrelJT, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
100 { ARM::t2LSLri, ARM::tLSLri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
101 { ARM::t2LSLrr, 0, ARM::tLSLrr, 0, 0, 0, 1, 0,0, 1,0,1 },
102 { ARM::t2LSRri, ARM::tLSRri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
103 { ARM::t2LSRrr, 0, ARM::tLSRrr, 0, 0, 0, 1, 0,0, 1,0,1 },
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000104 { ARM::t2MOVi, ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 1,0,0 },
105 { ARM::t2MOVi16,ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 1,1,0 },
Evan Chengddc0cb62012-12-20 19:59:30 +0000106 // FIXME: Do we need the 16-bit 'S' variant?
107 { ARM::t2MOVr,ARM::tMOVr, 0, 0, 0, 0, 0, 1,0, 0,0,0 },
108 { ARM::t2MUL, 0, ARM::tMUL, 0, 0, 0, 1, 0,0, 1,0,0 },
109 { ARM::t2MVNr, ARM::tMVN, 0, 0, 0, 1, 0, 0,0, 0,0,0 },
110 { ARM::t2ORRrr, 0, ARM::tORR, 0, 0, 0, 1, 0,0, 1,0,0 },
111 { ARM::t2REV, ARM::tREV, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
112 { ARM::t2REV16, ARM::tREV16, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
113 { ARM::t2REVSH, ARM::tREVSH, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
114 { ARM::t2RORrr, 0, ARM::tROR, 0, 0, 0, 1, 0,0, 1,0,0 },
115 { ARM::t2RSBri, ARM::tRSB, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
116 { ARM::t2RSBSri,ARM::tRSB, 0, 0, 0, 1, 0, 2,0, 0,1,0 },
117 { ARM::t2SBCrr, 0, ARM::tSBC, 0, 0, 0, 1, 0,0, 0,0,0 },
118 { ARM::t2SUBri, ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 0,0, 0,0,0 },
119 { ARM::t2SUBrr, ARM::tSUBrr, 0, 0, 0, 1, 0, 0,0, 0,0,0 },
120 { ARM::t2SUBSri,ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 2,2, 0,0,0 },
121 { ARM::t2SUBSrr,ARM::tSUBrr, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
122 { ARM::t2SXTB, ARM::tSXTB, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
123 { ARM::t2SXTH, ARM::tSXTH, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
124 { ARM::t2TSTrr, ARM::tTST, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
125 { ARM::t2UXTB, ARM::tUXTB, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
126 { ARM::t2UXTH, ARM::tUXTH, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
Evan Cheng36064672009-08-11 08:52:18 +0000127
Evan Chengddc0cb62012-12-20 19:59:30 +0000128 // FIXME: Clean this up after splitting each Thumb load / store opcode
129 // into multiple ones.
130 { ARM::t2LDRi12,ARM::tLDRi, ARM::tLDRspi, 5, 8, 1, 0, 0,0, 0,1,0 },
131 { ARM::t2LDRs, ARM::tLDRr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
132 { ARM::t2LDRBi12,ARM::tLDRBi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
133 { ARM::t2LDRBs, ARM::tLDRBr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
134 { ARM::t2LDRHi12,ARM::tLDRHi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
135 { ARM::t2LDRHs, ARM::tLDRHr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
136 { ARM::t2LDRSBs,ARM::tLDRSB, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
137 { ARM::t2LDRSHs,ARM::tLDRSH, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
James Molloy53298a12016-06-07 12:13:34 +0000138 { ARM::t2LDR_POST,ARM::tLDMIA_UPD,0, 0, 0, 1, 0, 0,0, 0,1,0 },
Evan Chengddc0cb62012-12-20 19:59:30 +0000139 { ARM::t2STRi12,ARM::tSTRi, ARM::tSTRspi, 5, 8, 1, 0, 0,0, 0,1,0 },
140 { ARM::t2STRs, ARM::tSTRr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
141 { ARM::t2STRBi12,ARM::tSTRBi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
142 { ARM::t2STRBs, ARM::tSTRBr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
143 { ARM::t2STRHi12,ARM::tSTRHi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
144 { ARM::t2STRHs, ARM::tSTRHr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
James Molloy53298a12016-06-07 12:13:34 +0000145 { ARM::t2STR_POST,ARM::tSTMIA_UPD,0, 0, 0, 1, 0, 0,0, 0,1,0 },
Evan Chengcc9ca352009-08-11 21:11:32 +0000146
Evan Chengddc0cb62012-12-20 19:59:30 +0000147 { ARM::t2LDMIA, ARM::tLDMIA, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
148 { ARM::t2LDMIA_RET,0, ARM::tPOP_RET, 0, 0, 1, 1, 1,1, 0,1,0 },
149 { ARM::t2LDMIA_UPD,ARM::tLDMIA_UPD,ARM::tPOP,0, 0, 1, 1, 1,1, 0,1,0 },
Scott Douglass953f9082015-10-05 14:49:54 +0000150 // ARM::t2STMIA (with no basereg writeback) has no Thumb1 equivalent.
151 // tSTMIA_UPD is a change in semantics which can only be used if the base
152 // register is killed. This difference is correctly handled elsewhere.
153 { ARM::t2STMIA, ARM::tSTMIA_UPD, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
Evan Chengddc0cb62012-12-20 19:59:30 +0000154 { ARM::t2STMIA_UPD,ARM::tSTMIA_UPD, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
155 { ARM::t2STMDB_UPD, 0, ARM::tPUSH, 0, 0, 1, 1, 1,1, 0,1,0 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000156 };
157
Nick Lewycky02d5f772009-10-25 06:33:48 +0000158 class Thumb2SizeReduce : public MachineFunctionPass {
Evan Cheng1be453b2009-08-08 03:21:23 +0000159 public:
160 static char ID;
Evan Cheng1be453b2009-08-08 03:21:23 +0000161
Evan Cheng6ddd7bc2009-08-15 07:59:10 +0000162 const Thumb2InstrInfo *TII;
Bob Wilsona2881ee2011-04-19 18:11:49 +0000163 const ARMSubtarget *STI;
Evan Cheng1be453b2009-08-08 03:21:23 +0000164
Eugene Zelenko342257e2017-01-31 00:56:17 +0000165 Thumb2SizeReduce(std::function<bool(const Function &)> Ftor);
166
Craig Topper6bc27bf2014-03-10 02:09:33 +0000167 bool runOnMachineFunction(MachineFunction &MF) override;
Evan Cheng1be453b2009-08-08 03:21:23 +0000168
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000169 MachineFunctionProperties getRequiredProperties() const override {
170 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000171 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000172 }
173
Mehdi Amini117296c2016-10-01 02:56:57 +0000174 StringRef getPassName() const override {
Evan Cheng1be453b2009-08-08 03:21:23 +0000175 return "Thumb2 instruction size reduction pass";
176 }
177
178 private:
179 /// ReduceOpcodeMap - Maps wide opcode to index of entry in ReduceTable.
180 DenseMap<unsigned, unsigned> ReduceOpcodeMap;
181
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000182 bool canAddPseudoFlagDep(MachineInstr *Use, bool IsSelfLoop);
Bob Wilsona2881ee2011-04-19 18:11:49 +0000183
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000184 bool VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
185 bool is2Addr, ARMCC::CondCodes Pred,
186 bool LiveCPSR, bool &HasCC, bool &CCDead);
187
Evan Cheng36064672009-08-11 08:52:18 +0000188 bool ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
189 const ReduceEntry &Entry);
190
191 bool ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000192 const ReduceEntry &Entry, bool LiveCPSR, bool IsSelfLoop);
Evan Cheng36064672009-08-11 08:52:18 +0000193
Evan Cheng1be453b2009-08-08 03:21:23 +0000194 /// ReduceTo2Addr - Reduce a 32-bit instruction to a 16-bit two-address
195 /// instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000196 bool ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000197 const ReduceEntry &Entry, bool LiveCPSR,
Evan Chengf4807a12011-10-27 21:21:05 +0000198 bool IsSelfLoop);
Evan Cheng1be453b2009-08-08 03:21:23 +0000199
200 /// ReduceToNarrow - Reduce a 32-bit instruction to a 16-bit
201 /// non-two-address instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000202 bool ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000203 const ReduceEntry &Entry, bool LiveCPSR,
Evan Chengf4807a12011-10-27 21:21:05 +0000204 bool IsSelfLoop);
Evan Cheng1be453b2009-08-08 03:21:23 +0000205
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000206 /// ReduceMI - Attempt to reduce MI, return true on success.
207 bool ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000208 bool LiveCPSR, bool IsSelfLoop);
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000209
Evan Cheng1be453b2009-08-08 03:21:23 +0000210 /// ReduceMBB - Reduce width of instructions in the specified basic block.
211 bool ReduceMBB(MachineBasicBlock &MBB);
Quentin Colombet23b404d2012-12-18 22:47:16 +0000212
Evan Chengddc0cb62012-12-20 19:59:30 +0000213 bool OptimizeSize;
Quentin Colombet23b404d2012-12-18 22:47:16 +0000214 bool MinimizeSize;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000215
216 // Last instruction to define CPSR in the current block.
217 MachineInstr *CPSRDef;
218 // Was CPSR last defined by a high latency instruction?
219 // When CPSRDef is null, this refers to CPSR defs in predecessors.
220 bool HighLatencyCPSR;
221
222 struct MBBInfo {
223 // The flags leaving this block have high latency.
Eugene Zelenko342257e2017-01-31 00:56:17 +0000224 bool HighLatencyCPSR = false;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000225 // Has this block been visited yet?
Eugene Zelenko342257e2017-01-31 00:56:17 +0000226 bool Visited = false;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000227
Eugene Zelenko342257e2017-01-31 00:56:17 +0000228 MBBInfo() = default;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000229 };
230
231 SmallVector<MBBInfo, 8> BlockInfo;
Akira Hatanaka4a616192015-06-08 18:50:43 +0000232
233 std::function<bool(const Function &)> PredicateFtor;
Evan Cheng1be453b2009-08-08 03:21:23 +0000234 };
Eugene Zelenko342257e2017-01-31 00:56:17 +0000235
Evan Cheng1be453b2009-08-08 03:21:23 +0000236 char Thumb2SizeReduce::ID = 0;
Eugene Zelenko342257e2017-01-31 00:56:17 +0000237
238} // end anonymous namespace
Evan Cheng1be453b2009-08-08 03:21:23 +0000239
Akira Hatanaka4a616192015-06-08 18:50:43 +0000240Thumb2SizeReduce::Thumb2SizeReduce(std::function<bool(const Function &)> Ftor)
Benjamin Kramer82de7d32016-05-27 14:27:24 +0000241 : MachineFunctionPass(ID), PredicateFtor(std::move(Ftor)) {
Evan Chengddc0cb62012-12-20 19:59:30 +0000242 OptimizeSize = MinimizeSize = false;
Evan Cheng1be453b2009-08-08 03:21:23 +0000243 for (unsigned i = 0, e = array_lengthof(ReduceTable); i != e; ++i) {
244 unsigned FromOpc = ReduceTable[i].WideOpc;
245 if (!ReduceOpcodeMap.insert(std::make_pair(FromOpc, i)).second)
Benjamin Kramer8ceb3232015-10-25 22:28:27 +0000246 llvm_unreachable("Duplicated entries?");
Evan Cheng1be453b2009-08-08 03:21:23 +0000247 }
248}
249
Evan Cheng6cc775f2011-06-28 19:10:37 +0000250static bool HasImplicitCPSRDef(const MCInstrDesc &MCID) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000251 for (const MCPhysReg *Regs = MCID.getImplicitDefs(); *Regs; ++Regs)
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000252 if (*Regs == ARM::CPSR)
253 return true;
254 return false;
255}
256
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000257// Check for a likely high-latency flag def.
258static bool isHighLatencyCPSR(MachineInstr *Def) {
259 switch(Def->getOpcode()) {
260 case ARM::FMSTAT:
261 case ARM::tMUL:
262 return true;
263 }
264 return false;
265}
266
Bob Wilsona2881ee2011-04-19 18:11:49 +0000267/// canAddPseudoFlagDep - For A9 (and other out-of-order) implementations,
268/// the 's' 16-bit instruction partially update CPSR. Abort the
269/// transformation to avoid adding false dependency on last CPSR setting
270/// instruction which hurts the ability for out-of-order execution engine
271/// to do register renaming magic.
272/// This function checks if there is a read-of-write dependency between the
273/// last instruction that defines the CPSR and the current instruction. If there
274/// is, then there is no harm done since the instruction cannot be retired
275/// before the CPSR setting instruction anyway.
276/// Note, we are not doing full dependency analysis here for the sake of compile
277/// time. We're not looking for cases like:
278/// r0 = muls ...
279/// r1 = add.w r0, ...
280/// ...
281/// = mul.w r1
282/// In this case it would have been ok to narrow the mul.w to muls since there
283/// are indirect RAW dependency between the muls and the mul.w
284bool
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000285Thumb2SizeReduce::canAddPseudoFlagDep(MachineInstr *Use, bool FirstInSelfLoop) {
Quentin Colombet23b404d2012-12-18 22:47:16 +0000286 // Disable the check for -Oz (aka OptimizeForSizeHarder).
287 if (MinimizeSize || !STI->avoidCPSRPartialUpdate())
Bob Wilsona2881ee2011-04-19 18:11:49 +0000288 return false;
289
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000290 if (!CPSRDef)
Evan Chengf4807a12011-10-27 21:21:05 +0000291 // If this BB loops back to itself, conservatively avoid narrowing the
292 // first instruction that does partial flag update.
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000293 return HighLatencyCPSR || FirstInSelfLoop;
Evan Chengf4807a12011-10-27 21:21:05 +0000294
Bob Wilsona2881ee2011-04-19 18:11:49 +0000295 SmallSet<unsigned, 2> Defs;
Owen Anderson8c1f17b2014-03-07 22:48:22 +0000296 for (const MachineOperand &MO : CPSRDef->operands()) {
Bob Wilsona2881ee2011-04-19 18:11:49 +0000297 if (!MO.isReg() || MO.isUndef() || MO.isUse())
298 continue;
299 unsigned Reg = MO.getReg();
300 if (Reg == 0 || Reg == ARM::CPSR)
301 continue;
302 Defs.insert(Reg);
303 }
304
Owen Anderson8c1f17b2014-03-07 22:48:22 +0000305 for (const MachineOperand &MO : Use->operands()) {
Bob Wilsona2881ee2011-04-19 18:11:49 +0000306 if (!MO.isReg() || MO.isUndef() || MO.isDef())
307 continue;
308 unsigned Reg = MO.getReg();
309 if (Defs.count(Reg))
310 return false;
311 }
312
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000313 // If the current CPSR has high latency, try to avoid the false dependency.
314 if (HighLatencyCPSR)
315 return true;
316
317 // tMOVi8 usually doesn't start long dependency chains, and there are a lot
318 // of them, so always shrink them when CPSR doesn't have high latency.
319 if (Use->getOpcode() == ARM::t2MOVi ||
320 Use->getOpcode() == ARM::t2MOVi16)
321 return false;
322
Bob Wilsona2881ee2011-04-19 18:11:49 +0000323 // No read-after-write dependency. The narrowing will add false dependency.
324 return true;
325}
326
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000327bool
328Thumb2SizeReduce::VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
329 bool is2Addr, ARMCC::CondCodes Pred,
330 bool LiveCPSR, bool &HasCC, bool &CCDead) {
Evan Chengd461c1c2009-08-09 19:17:19 +0000331 if ((is2Addr && Entry.PredCC2 == 0) ||
332 (!is2Addr && Entry.PredCC1 == 0)) {
333 if (Pred == ARMCC::AL) {
334 // Not predicated, must set CPSR.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000335 if (!HasCC) {
336 // Original instruction was not setting CPSR, but CPSR is not
337 // currently live anyway. It's ok to set it. The CPSR def is
338 // dead though.
339 if (!LiveCPSR) {
340 HasCC = true;
341 CCDead = true;
342 return true;
343 }
344 return false;
345 }
Evan Chengd461c1c2009-08-09 19:17:19 +0000346 } else {
347 // Predicated, must not set CPSR.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000348 if (HasCC)
349 return false;
Evan Chengd461c1c2009-08-09 19:17:19 +0000350 }
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000351 } else if ((is2Addr && Entry.PredCC2 == 2) ||
352 (!is2Addr && Entry.PredCC1 == 2)) {
353 /// Old opcode has an optional def of CPSR.
354 if (HasCC)
355 return true;
Jim Grosbachbc7eeaf2010-09-14 20:35:46 +0000356 // If old opcode does not implicitly define CPSR, then it's not ok since
357 // these new opcodes' CPSR def is not meant to be thrown away. e.g. CMP.
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000358 if (!HasImplicitCPSRDef(MI->getDesc()))
359 return false;
360 HasCC = true;
Evan Chengd461c1c2009-08-09 19:17:19 +0000361 } else {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000362 // 16-bit instruction does not set CPSR.
363 if (HasCC)
364 return false;
Evan Chengd461c1c2009-08-09 19:17:19 +0000365 }
366
367 return true;
368}
369
Evan Chengcc9ca352009-08-11 21:11:32 +0000370static bool VerifyLowRegs(MachineInstr *MI) {
371 unsigned Opc = MI->getOpcode();
Peter Collingbourne85a0e232015-05-05 20:07:10 +0000372 bool isPCOk = (Opc == ARM::t2LDMIA_RET || Opc == ARM::t2LDMIA_UPD);
Tim Northoverba1d7042014-09-10 12:53:28 +0000373 bool isLROk = (Opc == ARM::t2STMDB_UPD);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000374 bool isSPOk = isPCOk || isLROk;
Evan Chengcc9ca352009-08-11 21:11:32 +0000375 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
376 const MachineOperand &MO = MI->getOperand(i);
377 if (!MO.isReg() || MO.isImplicit())
378 continue;
379 unsigned Reg = MO.getReg();
380 if (Reg == 0 || Reg == ARM::CPSR)
381 continue;
382 if (isPCOk && Reg == ARM::PC)
383 continue;
384 if (isLROk && Reg == ARM::LR)
385 continue;
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000386 if (Reg == ARM::SP) {
387 if (isSPOk)
388 continue;
389 if (i == 1 && (Opc == ARM::t2LDRi12 || Opc == ARM::t2STRi12))
390 // Special case for these ldr / str with sp as base register.
391 continue;
392 }
Evan Chengcc9ca352009-08-11 21:11:32 +0000393 if (!isARMLowRegister(Reg))
394 return false;
395 }
396 return true;
397}
398
Evan Cheng1be453b2009-08-08 03:21:23 +0000399bool
Evan Cheng36064672009-08-11 08:52:18 +0000400Thumb2SizeReduce::ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
401 const ReduceEntry &Entry) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000402 if (ReduceLimitLdSt != -1 && ((int)NumLdSts >= ReduceLimitLdSt))
403 return false;
404
Evan Cheng36064672009-08-11 08:52:18 +0000405 unsigned Scale = 1;
406 bool HasImmOffset = false;
407 bool HasShift = false;
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000408 bool HasOffReg = true;
Evan Chengcc9ca352009-08-11 21:11:32 +0000409 bool isLdStMul = false;
Evan Chengcc9ca352009-08-11 21:11:32 +0000410 unsigned Opc = Entry.NarrowOpc1;
411 unsigned OpNum = 3; // First 'rest' of operands.
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000412 uint8_t ImmLimit = Entry.Imm1Limit;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000413
Evan Cheng36064672009-08-11 08:52:18 +0000414 switch (Entry.WideOpc) {
415 default:
416 llvm_unreachable("Unexpected Thumb2 load / store opcode!");
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000417 case ARM::t2LDRi12:
Bill Wendling092a7bd2010-12-14 03:36:38 +0000418 case ARM::t2STRi12:
419 if (MI->getOperand(1).getReg() == ARM::SP) {
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000420 Opc = Entry.NarrowOpc2;
421 ImmLimit = Entry.Imm2Limit;
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000422 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000423
Evan Cheng36064672009-08-11 08:52:18 +0000424 Scale = 4;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000425 HasImmOffset = true;
426 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000427 break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000428 case ARM::t2LDRBi12:
Evan Cheng36064672009-08-11 08:52:18 +0000429 case ARM::t2STRBi12:
Owen Anderson4ebf4712011-02-08 22:39:40 +0000430 HasImmOffset = true;
431 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000432 break;
433 case ARM::t2LDRHi12:
434 case ARM::t2STRHi12:
435 Scale = 2;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000436 HasImmOffset = true;
437 HasOffReg = false;
Evan Cheng36064672009-08-11 08:52:18 +0000438 break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +0000439 case ARM::t2LDRs:
440 case ARM::t2LDRBs:
441 case ARM::t2LDRHs:
Evan Cheng36064672009-08-11 08:52:18 +0000442 case ARM::t2LDRSBs:
443 case ARM::t2LDRSHs:
444 case ARM::t2STRs:
445 case ARM::t2STRBs:
446 case ARM::t2STRHs:
447 HasShift = true;
Evan Chengcc9ca352009-08-11 21:11:32 +0000448 OpNum = 4;
Evan Cheng36064672009-08-11 08:52:18 +0000449 break;
James Molloy53298a12016-06-07 12:13:34 +0000450 case ARM::t2LDR_POST:
451 case ARM::t2STR_POST: {
452 if (!MBB.getParent()->getFunction()->optForMinSize())
453 return false;
454
Tim Northovereaee28b2016-09-19 09:11:09 +0000455 if (!MI->hasOneMemOperand() ||
456 (*MI->memoperands_begin())->getAlignment() < 4)
457 return false;
458
James Molloy53298a12016-06-07 12:13:34 +0000459 // We're creating a completely different type of load/store - LDM from LDR.
460 // For this reason we can't reuse the logic at the end of this function; we
461 // have to implement the MI building here.
462 bool IsStore = Entry.WideOpc == ARM::t2STR_POST;
463 unsigned Rt = MI->getOperand(IsStore ? 1 : 0).getReg();
464 unsigned Rn = MI->getOperand(IsStore ? 0 : 1).getReg();
465 unsigned Offset = MI->getOperand(3).getImm();
466 unsigned PredImm = MI->getOperand(4).getImm();
467 unsigned PredReg = MI->getOperand(5).getReg();
468 assert(isARMLowRegister(Rt));
469 assert(isARMLowRegister(Rn));
470
471 if (Offset != 4)
472 return false;
473
474 // Add the 16-bit load / store instruction.
475 DebugLoc dl = MI->getDebugLoc();
476 auto MIB = BuildMI(MBB, MI, dl, TII->get(Entry.NarrowOpc1))
477 .addReg(Rn, RegState::Define)
478 .addReg(Rn)
479 .addImm(PredImm)
480 .addReg(PredReg)
481 .addReg(Rt, IsStore ? 0 : RegState::Define);
482
483 // Transfer memoperands.
484 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
485
486 // Transfer MI flags.
487 MIB.setMIFlags(MI->getFlags());
488
489 // Kill the old instruction.
Tim Northover28a9e7f2016-06-17 18:40:46 +0000490 MI->eraseFromBundle();
James Molloy53298a12016-06-07 12:13:34 +0000491 ++NumLdSts;
492 return true;
493 }
Peter Collingbourne85a0e232015-05-05 20:07:10 +0000494 case ARM::t2LDMIA: {
Evan Chengcc9ca352009-08-11 21:11:32 +0000495 unsigned BaseReg = MI->getOperand(0).getReg();
Peter Collingbourne85a0e232015-05-05 20:07:10 +0000496 assert(isARMLowRegister(BaseReg));
Bill Wendling092a7bd2010-12-14 03:36:38 +0000497
Jim Grosbach88628e92010-09-07 22:30:53 +0000498 // For the non-writeback version (this one), the base register must be
499 // one of the registers being loaded.
500 bool isOK = false;
Peter Collingbourne85a0e232015-05-05 20:07:10 +0000501 for (unsigned i = 3; i < MI->getNumOperands(); ++i) {
Jim Grosbach88628e92010-09-07 22:30:53 +0000502 if (MI->getOperand(i).getReg() == BaseReg) {
503 isOK = true;
504 break;
505 }
506 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000507
Jim Grosbach88628e92010-09-07 22:30:53 +0000508 if (!isOK)
509 return false;
510
Bob Wilson947f04b2010-03-13 01:08:20 +0000511 OpNum = 0;
512 isLdStMul = true;
513 break;
514 }
Eugene Zelenko342257e2017-01-31 00:56:17 +0000515 case ARM::t2STMIA:
Scott Douglass953f9082015-10-05 14:49:54 +0000516 // If the base register is killed, we don't care what its value is after the
517 // instruction, so we can use an updating STMIA.
518 if (!MI->getOperand(0).isKill())
519 return false;
520
521 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000522 case ARM::t2LDMIA_RET: {
Bob Wilson947f04b2010-03-13 01:08:20 +0000523 unsigned BaseReg = MI->getOperand(1).getReg();
524 if (BaseReg != ARM::SP)
525 return false;
526 Opc = Entry.NarrowOpc2; // tPOP_RET
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000527 OpNum = 2;
Bob Wilson947f04b2010-03-13 01:08:20 +0000528 isLdStMul = true;
529 break;
530 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000531 case ARM::t2LDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000532 case ARM::t2STMIA_UPD:
533 case ARM::t2STMDB_UPD: {
Bob Wilson947f04b2010-03-13 01:08:20 +0000534 OpNum = 0;
Bill Wendling092a7bd2010-12-14 03:36:38 +0000535
Bob Wilson947f04b2010-03-13 01:08:20 +0000536 unsigned BaseReg = MI->getOperand(1).getReg();
Bob Wilson947f04b2010-03-13 01:08:20 +0000537 if (BaseReg == ARM::SP &&
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000538 (Entry.WideOpc == ARM::t2LDMIA_UPD ||
539 Entry.WideOpc == ARM::t2STMDB_UPD)) {
Bob Wilson947f04b2010-03-13 01:08:20 +0000540 Opc = Entry.NarrowOpc2; // tPOP or tPUSH
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000541 OpNum = 2;
542 } else if (!isARMLowRegister(BaseReg) ||
543 (Entry.WideOpc != ARM::t2LDMIA_UPD &&
544 Entry.WideOpc != ARM::t2STMIA_UPD)) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000545 return false;
546 }
Bill Wendling092a7bd2010-12-14 03:36:38 +0000547
Evan Chengcc9ca352009-08-11 21:11:32 +0000548 isLdStMul = true;
549 break;
550 }
Evan Cheng36064672009-08-11 08:52:18 +0000551 }
552
553 unsigned OffsetReg = 0;
554 bool OffsetKill = false;
Pete Cooperf68d5032015-05-01 18:57:32 +0000555 bool OffsetInternal = false;
Evan Cheng36064672009-08-11 08:52:18 +0000556 if (HasShift) {
557 OffsetReg = MI->getOperand(2).getReg();
558 OffsetKill = MI->getOperand(2).isKill();
Pete Cooperf68d5032015-05-01 18:57:32 +0000559 OffsetInternal = MI->getOperand(2).isInternalRead();
Bill Wendling092a7bd2010-12-14 03:36:38 +0000560
Evan Cheng36064672009-08-11 08:52:18 +0000561 if (MI->getOperand(3).getImm())
562 // Thumb1 addressing mode doesn't support shift.
563 return false;
564 }
565
566 unsigned OffsetImm = 0;
567 if (HasImmOffset) {
568 OffsetImm = MI->getOperand(2).getImm();
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000569 unsigned MaxOffset = ((1 << ImmLimit) - 1) * Scale;
Bill Wendling092a7bd2010-12-14 03:36:38 +0000570
571 if ((OffsetImm & (Scale - 1)) || OffsetImm > MaxOffset)
Evan Cheng36064672009-08-11 08:52:18 +0000572 // Make sure the immediate field fits.
573 return false;
574 }
575
576 // Add the 16-bit load / store instruction.
Evan Cheng36064672009-08-11 08:52:18 +0000577 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000578 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, TII->get(Opc));
Scott Douglass953f9082015-10-05 14:49:54 +0000579
580 // tSTMIA_UPD takes a defining register operand. We've already checked that
581 // the register is killed, so mark it as dead here.
582 if (Entry.WideOpc == ARM::t2STMIA)
583 MIB.addReg(MI->getOperand(0).getReg(), RegState::Define | RegState::Dead);
584
Evan Chengcc9ca352009-08-11 21:11:32 +0000585 if (!isLdStMul) {
Diana Picus116bbab2017-01-13 09:58:52 +0000586 MIB.add(MI->getOperand(0));
587 MIB.add(MI->getOperand(1));
Bill Wendling092a7bd2010-12-14 03:36:38 +0000588
589 if (HasImmOffset)
590 MIB.addImm(OffsetImm / Scale);
591
Evan Chengcc9ca352009-08-11 21:11:32 +0000592 assert((!HasShift || OffsetReg) && "Invalid so_reg load / store address!");
593
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000594 if (HasOffReg)
Pete Cooperf68d5032015-05-01 18:57:32 +0000595 MIB.addReg(OffsetReg, getKillRegState(OffsetKill) |
596 getInternalReadRegState(OffsetInternal));
Evan Cheng36064672009-08-11 08:52:18 +0000597 }
Evan Cheng806845d2009-08-11 09:37:40 +0000598
Evan Cheng36064672009-08-11 08:52:18 +0000599 // Transfer the rest of operands.
Evan Cheng36064672009-08-11 08:52:18 +0000600 for (unsigned e = MI->getNumOperands(); OpNum != e; ++OpNum)
Diana Picus116bbab2017-01-13 09:58:52 +0000601 MIB.add(MI->getOperand(OpNum));
Evan Cheng36064672009-08-11 08:52:18 +0000602
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000603 // Transfer memoperands.
Chris Lattner1d0c2572011-04-29 05:24:29 +0000604 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
Evan Cheng2a6c92f2009-11-19 06:32:27 +0000605
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000606 // Transfer MI flags.
607 MIB.setMIFlags(MI->getFlags());
608
Chris Lattnera6f074f2009-08-23 03:41:05 +0000609 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Cheng36064672009-08-11 08:52:18 +0000610
Evan Cheng7fae11b2011-12-14 02:11:42 +0000611 MBB.erase_instr(MI);
Evan Cheng36064672009-08-11 08:52:18 +0000612 ++NumLdSts;
613 return true;
614}
615
Evan Cheng36064672009-08-11 08:52:18 +0000616bool
617Thumb2SizeReduce::ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
618 const ReduceEntry &Entry,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000619 bool LiveCPSR, bool IsSelfLoop) {
Jim Grosbacha8a80672011-06-29 23:25:04 +0000620 unsigned Opc = MI->getOpcode();
621 if (Opc == ARM::t2ADDri) {
622 // If the source register is SP, try to reduce to tADDrSPi, otherwise
623 // it's a normal reduce.
624 if (MI->getOperand(1).getReg() != ARM::SP) {
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000625 if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
Jim Grosbacha8a80672011-06-29 23:25:04 +0000626 return true;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000627 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000628 }
629 // Try to reduce to tADDrSPi.
630 unsigned Imm = MI->getOperand(2).getImm();
631 // The immediate must be in range, the destination register must be a low
Jim Grosbached5134a2011-06-30 02:22:49 +0000632 // reg, the predicate must be "always" and the condition flags must not
633 // be being set.
Jim Grosbach68b0e842011-07-01 19:07:09 +0000634 if (Imm & 3 || Imm > 1020)
Jim Grosbacha8a80672011-06-29 23:25:04 +0000635 return false;
636 if (!isARMLowRegister(MI->getOperand(0).getReg()))
637 return false;
Jim Grosbached5134a2011-06-30 02:22:49 +0000638 if (MI->getOperand(3).getImm() != ARMCC::AL)
639 return false;
Jim Grosbacha8a80672011-06-29 23:25:04 +0000640 const MCInstrDesc &MCID = MI->getDesc();
641 if (MCID.hasOptionalDef() &&
642 MI->getOperand(MCID.getNumOperands()-1).getReg() == ARM::CPSR)
643 return false;
644
Diana Picus4f8c3e12017-01-13 09:37:56 +0000645 MachineInstrBuilder MIB =
646 BuildMI(MBB, MI, MI->getDebugLoc(),
647 TII->get(ARM::tADDrSPi))
Diana Picus116bbab2017-01-13 09:58:52 +0000648 .add(MI->getOperand(0))
649 .add(MI->getOperand(1))
Diana Picus4f8c3e12017-01-13 09:37:56 +0000650 .addImm(Imm / 4) // The tADDrSPi has an implied scale by four.
651 .add(predOps(ARMCC::AL));
Jim Grosbacha8a80672011-06-29 23:25:04 +0000652
653 // Transfer MI flags.
654 MIB.setMIFlags(MI->getFlags());
655
656 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " <<*MIB);
657
Evan Cheng7fae11b2011-12-14 02:11:42 +0000658 MBB.erase_instr(MI);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000659 ++NumNarrows;
660 return true;
661 }
662
Evan Chengcc9ca352009-08-11 21:11:32 +0000663 if (Entry.LowRegs1 && !VerifyLowRegs(MI))
Evan Cheng36064672009-08-11 08:52:18 +0000664 return false;
665
Chad Rosier67336302015-05-22 20:07:34 +0000666 if (MI->mayLoadOrStore())
Evan Cheng36064672009-08-11 08:52:18 +0000667 return ReduceLoadStore(MBB, MI, Entry);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000668
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000669 switch (Opc) {
670 default: break;
Owen Anderson4ebf4712011-02-08 22:39:40 +0000671 case ARM::t2ADDSri:
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000672 case ARM::t2ADDSrr: {
673 unsigned PredReg = 0;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000674 if (getInstrPredicate(*MI, PredReg) == ARMCC::AL) {
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000675 switch (Opc) {
676 default: break;
Eugene Zelenko342257e2017-01-31 00:56:17 +0000677 case ARM::t2ADDSri:
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000678 if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000679 return true;
Justin Bognerb03fd122016-08-17 05:10:15 +0000680 LLVM_FALLTHROUGH;
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000681 case ARM::t2ADDSrr:
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000682 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000683 }
684 }
685 break;
686 }
687 case ARM::t2RSBri:
688 case ARM::t2RSBSri:
Jim Grosbach8b31ef52011-07-27 16:47:19 +0000689 case ARM::t2SXTB:
690 case ARM::t2SXTH:
691 case ARM::t2UXTB:
692 case ARM::t2UXTH:
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000693 if (MI->getOperand(2).getImm() == 0)
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000694 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000695 break;
Anton Korobeynikov25229082009-11-24 00:44:37 +0000696 case ARM::t2MOVi16:
697 // Can convert only 'pure' immediate operands, not immediates obtained as
698 // globals' addresses.
699 if (MI->getOperand(1).isImm())
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000700 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Anton Korobeynikov25229082009-11-24 00:44:37 +0000701 break;
Jim Grosbach327cf8e2010-12-07 20:41:06 +0000702 case ARM::t2CMPrr: {
Jim Grosbach5bae0542010-12-03 23:54:18 +0000703 // Try to reduce to the lo-reg only version first. Why there are two
704 // versions of the instruction is a mystery.
705 // It would be nice to just have two entries in the master table that
706 // are prioritized, but the table assumes a unique entry for each
707 // source insn opcode. So for now, we hack a local entry record to use.
708 static const ReduceEntry NarrowEntry =
Evan Chengddc0cb62012-12-20 19:59:30 +0000709 { ARM::t2CMPrr,ARM::tCMPr, 0, 0, 0, 1, 1,2, 0, 0,1,0 };
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000710 if (ReduceToNarrow(MBB, MI, NarrowEntry, LiveCPSR, IsSelfLoop))
Jim Grosbach5bae0542010-12-03 23:54:18 +0000711 return true;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000712 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Jim Grosbach5bae0542010-12-03 23:54:18 +0000713 }
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000714 }
Evan Cheng36064672009-08-11 08:52:18 +0000715 return false;
716}
717
718bool
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000719Thumb2SizeReduce::ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
720 const ReduceEntry &Entry,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000721 bool LiveCPSR, bool IsSelfLoop) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000722 if (ReduceLimit2Addr != -1 && ((int)Num2Addrs >= ReduceLimit2Addr))
723 return false;
724
Sanjay Patel924879a2015-08-04 15:49:57 +0000725 if (!OptimizeSize && Entry.AvoidMovs && STI->avoidMOVsShifterOperand())
Evan Chengddc0cb62012-12-20 19:59:30 +0000726 // Don't issue movs with shifter operand for some CPUs unless we
Sanjay Patel924879a2015-08-04 15:49:57 +0000727 // are optimizing for size.
Evan Chengddc0cb62012-12-20 19:59:30 +0000728 return false;
729
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000730 unsigned Reg0 = MI->getOperand(0).getReg();
731 unsigned Reg1 = MI->getOperand(1).getReg();
Jim Grosbachc01104d2012-02-24 00:33:36 +0000732 // t2MUL is "special". The tied source operand is second, not first.
733 if (MI->getOpcode() == ARM::t2MUL) {
Jim Grosbach3a21e2c2012-02-24 00:53:11 +0000734 unsigned Reg2 = MI->getOperand(2).getReg();
735 // Early exit if the regs aren't all low regs.
736 if (!isARMLowRegister(Reg0) || !isARMLowRegister(Reg1)
737 || !isARMLowRegister(Reg2))
738 return false;
739 if (Reg0 != Reg2) {
Jim Grosbachc01104d2012-02-24 00:33:36 +0000740 // If the other operand also isn't the same as the destination, we
741 // can't reduce.
742 if (Reg1 != Reg0)
743 return false;
744 // Try to commute the operands to make it a 2-address instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000745 MachineInstr *CommutedMI = TII->commuteInstruction(*MI);
Jim Grosbachc01104d2012-02-24 00:33:36 +0000746 if (!CommutedMI)
747 return false;
748 }
749 } else if (Reg0 != Reg1) {
Bob Wilson279e55f2010-06-24 16:50:20 +0000750 // Try to commute the operands to make it a 2-address instruction.
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000751 unsigned CommOpIdx1 = 1;
752 unsigned CommOpIdx2 = TargetInstrInfo::CommuteAnyOperandIndex;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000753 if (!TII->findCommutedOpIndices(*MI, CommOpIdx1, CommOpIdx2) ||
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000754 MI->getOperand(CommOpIdx2).getReg() != Reg0)
Bob Wilson279e55f2010-06-24 16:50:20 +0000755 return false;
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000756 MachineInstr *CommutedMI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000757 TII->commuteInstruction(*MI, false, CommOpIdx1, CommOpIdx2);
Bob Wilson279e55f2010-06-24 16:50:20 +0000758 if (!CommutedMI)
759 return false;
760 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000761 if (Entry.LowRegs2 && !isARMLowRegister(Reg0))
762 return false;
763 if (Entry.Imm2Limit) {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000764 unsigned Imm = MI->getOperand(2).getImm();
Evan Cheng1be453b2009-08-08 03:21:23 +0000765 unsigned Limit = (1 << Entry.Imm2Limit) - 1;
766 if (Imm > Limit)
767 return false;
768 } else {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000769 unsigned Reg2 = MI->getOperand(2).getReg();
Evan Cheng1be453b2009-08-08 03:21:23 +0000770 if (Entry.LowRegs2 && !isARMLowRegister(Reg2))
771 return false;
772 }
773
Evan Cheng1f5bee12009-08-10 06:57:42 +0000774 // Check if it's possible / necessary to transfer the predicate.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000775 const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc2);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000776 unsigned PredReg = 0;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000777 ARMCC::CondCodes Pred = getInstrPredicate(*MI, PredReg);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000778 bool SkipPred = false;
779 if (Pred != ARMCC::AL) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000780 if (!NewMCID.isPredicable())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000781 // Can't transfer predicate, fail.
782 return false;
783 } else {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000784 SkipPred = !NewMCID.isPredicable();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000785 }
786
Evan Cheng1be453b2009-08-08 03:21:23 +0000787 bool HasCC = false;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000788 bool CCDead = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000789 const MCInstrDesc &MCID = MI->getDesc();
790 if (MCID.hasOptionalDef()) {
791 unsigned NumOps = MCID.getNumOperands();
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000792 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
793 if (HasCC && MI->getOperand(NumOps-1).isDead())
794 CCDead = true;
795 }
Evan Cheng1f5bee12009-08-10 06:57:42 +0000796 if (!VerifyPredAndCC(MI, Entry, true, Pred, LiveCPSR, HasCC, CCDead))
Evan Chengd461c1c2009-08-09 19:17:19 +0000797 return false;
Evan Cheng1be453b2009-08-08 03:21:23 +0000798
Bob Wilsona2881ee2011-04-19 18:11:49 +0000799 // Avoid adding a false dependency on partial flag update by some 16-bit
800 // instructions which has the 's' bit set.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000801 if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000802 canAddPseudoFlagDep(MI, IsSelfLoop))
Bob Wilsona2881ee2011-04-19 18:11:49 +0000803 return false;
804
Evan Cheng1be453b2009-08-08 03:21:23 +0000805 // Add the 16-bit instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000806 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000807 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
Diana Picus116bbab2017-01-13 09:58:52 +0000808 MIB.add(MI->getOperand(0));
Diana Picusa2c59142017-01-13 10:37:37 +0000809 if (NewMCID.hasOptionalDef())
810 MIB.add(HasCC ? t1CondCodeOp(CCDead) : condCodeOp());
Evan Chengd461c1c2009-08-09 19:17:19 +0000811
812 // Transfer the rest of operands.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000813 unsigned NumOps = MCID.getNumOperands();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000814 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000815 if (i < NumOps && MCID.OpInfo[i].isOptionalDef())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000816 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000817 if (SkipPred && MCID.OpInfo[i].isPredicate())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000818 continue;
Diana Picus116bbab2017-01-13 09:58:52 +0000819 MIB.add(MI->getOperand(i));
Evan Cheng1f5bee12009-08-10 06:57:42 +0000820 }
Evan Cheng1be453b2009-08-08 03:21:23 +0000821
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000822 // Transfer MI flags.
823 MIB.setMIFlags(MI->getFlags());
824
Chris Lattnera6f074f2009-08-23 03:41:05 +0000825 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Cheng1be453b2009-08-08 03:21:23 +0000826
Evan Cheng7fae11b2011-12-14 02:11:42 +0000827 MBB.erase_instr(MI);
Evan Cheng1be453b2009-08-08 03:21:23 +0000828 ++Num2Addrs;
Evan Cheng1be453b2009-08-08 03:21:23 +0000829 return true;
830}
831
832bool
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000833Thumb2SizeReduce::ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
834 const ReduceEntry &Entry,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000835 bool LiveCPSR, bool IsSelfLoop) {
Evan Chengcc9ca352009-08-11 21:11:32 +0000836 if (ReduceLimit != -1 && ((int)NumNarrows >= ReduceLimit))
837 return false;
838
Sanjay Patel924879a2015-08-04 15:49:57 +0000839 if (!OptimizeSize && Entry.AvoidMovs && STI->avoidMOVsShifterOperand())
Evan Chengddc0cb62012-12-20 19:59:30 +0000840 // Don't issue movs with shifter operand for some CPUs unless we
Sanjay Patel924879a2015-08-04 15:49:57 +0000841 // are optimizing for size.
Evan Chengddc0cb62012-12-20 19:59:30 +0000842 return false;
843
Evan Chengd461c1c2009-08-09 19:17:19 +0000844 unsigned Limit = ~0U;
845 if (Entry.Imm1Limit)
Jim Grosbacha8a80672011-06-29 23:25:04 +0000846 Limit = (1 << Entry.Imm1Limit) - 1;
Evan Chengd461c1c2009-08-09 19:17:19 +0000847
Evan Cheng6cc775f2011-06-28 19:10:37 +0000848 const MCInstrDesc &MCID = MI->getDesc();
849 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i) {
850 if (MCID.OpInfo[i].isPredicate())
Evan Chengd461c1c2009-08-09 19:17:19 +0000851 continue;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000852 const MachineOperand &MO = MI->getOperand(i);
Evan Chengd461c1c2009-08-09 19:17:19 +0000853 if (MO.isReg()) {
854 unsigned Reg = MO.getReg();
855 if (!Reg || Reg == ARM::CPSR)
856 continue;
857 if (Entry.LowRegs1 && !isARMLowRegister(Reg))
858 return false;
Evan Chengf6a9d062009-08-11 23:00:31 +0000859 } else if (MO.isImm() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +0000860 !MCID.OpInfo[i].isPredicate()) {
Jim Grosbacha8a80672011-06-29 23:25:04 +0000861 if (((unsigned)MO.getImm()) > Limit)
Evan Chengd461c1c2009-08-09 19:17:19 +0000862 return false;
863 }
864 }
865
Evan Cheng1f5bee12009-08-10 06:57:42 +0000866 // Check if it's possible / necessary to transfer the predicate.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000867 const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc1);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000868 unsigned PredReg = 0;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000869 ARMCC::CondCodes Pred = getInstrPredicate(*MI, PredReg);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000870 bool SkipPred = false;
871 if (Pred != ARMCC::AL) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000872 if (!NewMCID.isPredicable())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000873 // Can't transfer predicate, fail.
874 return false;
875 } else {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000876 SkipPred = !NewMCID.isPredicable();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000877 }
878
Evan Chengd461c1c2009-08-09 19:17:19 +0000879 bool HasCC = false;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000880 bool CCDead = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000881 if (MCID.hasOptionalDef()) {
882 unsigned NumOps = MCID.getNumOperands();
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000883 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
884 if (HasCC && MI->getOperand(NumOps-1).isDead())
885 CCDead = true;
886 }
Evan Cheng1f5bee12009-08-10 06:57:42 +0000887 if (!VerifyPredAndCC(MI, Entry, false, Pred, LiveCPSR, HasCC, CCDead))
Evan Chengd461c1c2009-08-09 19:17:19 +0000888 return false;
889
Bob Wilsona2881ee2011-04-19 18:11:49 +0000890 // Avoid adding a false dependency on partial flag update by some 16-bit
891 // instructions which has the 's' bit set.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000892 if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000893 canAddPseudoFlagDep(MI, IsSelfLoop))
Bob Wilsona2881ee2011-04-19 18:11:49 +0000894 return false;
895
Evan Chengd461c1c2009-08-09 19:17:19 +0000896 // Add the 16-bit instruction.
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000897 DebugLoc dl = MI->getDebugLoc();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000898 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
Diana Picus116bbab2017-01-13 09:58:52 +0000899 MIB.add(MI->getOperand(0));
Diana Picusa2c59142017-01-13 10:37:37 +0000900 if (NewMCID.hasOptionalDef())
901 MIB.add(HasCC ? t1CondCodeOp(CCDead) : condCodeOp());
Evan Chengd461c1c2009-08-09 19:17:19 +0000902
903 // Transfer the rest of operands.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000904 unsigned NumOps = MCID.getNumOperands();
Evan Cheng1f5bee12009-08-10 06:57:42 +0000905 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000906 if (i < NumOps && MCID.OpInfo[i].isOptionalDef())
Evan Cheng1f5bee12009-08-10 06:57:42 +0000907 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000908 if ((MCID.getOpcode() == ARM::t2RSBSri ||
Jim Grosbach8b31ef52011-07-27 16:47:19 +0000909 MCID.getOpcode() == ARM::t2RSBri ||
910 MCID.getOpcode() == ARM::t2SXTB ||
911 MCID.getOpcode() == ARM::t2SXTH ||
912 MCID.getOpcode() == ARM::t2UXTB ||
913 MCID.getOpcode() == ARM::t2UXTH) && i == 2)
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000914 // Skip the zero immediate operand, it's now implicit.
915 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000916 bool isPred = (i < NumOps && MCID.OpInfo[i].isPredicate());
Evan Chengf6a9d062009-08-11 23:00:31 +0000917 if (SkipPred && isPred)
918 continue;
919 const MachineOperand &MO = MI->getOperand(i);
Jim Grosbacha8a80672011-06-29 23:25:04 +0000920 if (MO.isReg() && MO.isImplicit() && MO.getReg() == ARM::CPSR)
921 // Skip implicit def of CPSR. Either it's modeled as an optional
922 // def now or it's already an implicit def on the new instruction.
923 continue;
Diana Picus116bbab2017-01-13 09:58:52 +0000924 MIB.add(MO);
Evan Cheng1f5bee12009-08-10 06:57:42 +0000925 }
Evan Cheng6cc775f2011-06-28 19:10:37 +0000926 if (!MCID.isPredicable() && NewMCID.isPredicable())
Diana Picus4f8c3e12017-01-13 09:37:56 +0000927 MIB.add(predOps(ARMCC::AL));
Evan Chengd461c1c2009-08-09 19:17:19 +0000928
Anton Korobeynikovacca7ad2011-03-05 18:43:38 +0000929 // Transfer MI flags.
930 MIB.setMIFlags(MI->getFlags());
931
Chris Lattnera6f074f2009-08-23 03:41:05 +0000932 DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
Evan Chengd461c1c2009-08-09 19:17:19 +0000933
Evan Cheng7fae11b2011-12-14 02:11:42 +0000934 MBB.erase_instr(MI);
Evan Chengd461c1c2009-08-09 19:17:19 +0000935 ++NumNarrows;
936 return true;
Evan Cheng1be453b2009-08-08 03:21:23 +0000937}
938
Bob Wilsona2881ee2011-04-19 18:11:49 +0000939static bool UpdateCPSRDef(MachineInstr &MI, bool LiveCPSR, bool &DefCPSR) {
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000940 bool HasDef = false;
Owen Anderson8c1f17b2014-03-07 22:48:22 +0000941 for (const MachineOperand &MO : MI.operands()) {
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000942 if (!MO.isReg() || MO.isUndef() || MO.isUse())
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000943 continue;
944 if (MO.getReg() != ARM::CPSR)
945 continue;
Bob Wilsona2881ee2011-04-19 18:11:49 +0000946
947 DefCPSR = true;
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000948 if (!MO.isDead())
949 HasDef = true;
950 }
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000951
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000952 return HasDef || LiveCPSR;
953}
954
955static bool UpdateCPSRUse(MachineInstr &MI, bool LiveCPSR) {
Owen Anderson8c1f17b2014-03-07 22:48:22 +0000956 for (const MachineOperand &MO : MI.operands()) {
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000957 if (!MO.isReg() || MO.isUndef() || MO.isDef())
958 continue;
959 if (MO.getReg() != ARM::CPSR)
960 continue;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000961 assert(LiveCPSR && "CPSR liveness tracking is wrong!");
962 if (MO.isKill()) {
963 LiveCPSR = false;
964 break;
965 }
966 }
967
Evan Cheng1e6c2a12009-08-12 01:49:45 +0000968 return LiveCPSR;
Evan Cheng51cbd2d2009-08-10 02:37:24 +0000969}
970
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000971bool Thumb2SizeReduce::ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI,
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000972 bool LiveCPSR, bool IsSelfLoop) {
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000973 unsigned Opcode = MI->getOpcode();
974 DenseMap<unsigned, unsigned>::iterator OPI = ReduceOpcodeMap.find(Opcode);
975 if (OPI == ReduceOpcodeMap.end())
976 return false;
977 const ReduceEntry &Entry = ReduceTable[OPI->second];
978
979 // Don't attempt normal reductions on "special" cases for now.
980 if (Entry.Special)
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000981 return ReduceSpecial(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000982
983 // Try to transform to a 16-bit two-address instruction.
984 if (Entry.NarrowOpc2 &&
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000985 ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000986 return true;
987
988 // Try to transform to a 16-bit non-two-address instruction.
989 if (Entry.NarrowOpc1 &&
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +0000990 ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +0000991 return true;
992
993 return false;
994}
995
Evan Cheng1be453b2009-08-08 03:21:23 +0000996bool Thumb2SizeReduce::ReduceMBB(MachineBasicBlock &MBB) {
997 bool Modified = false;
998
Evan Cheng1f5bee12009-08-10 06:57:42 +0000999 // Yes, CPSR could be livein.
Dan Gohmana1cf9fe2010-04-13 16:53:51 +00001000 bool LiveCPSR = MBB.isLiveIn(ARM::CPSR);
Craig Topper062a2ba2014-04-25 05:30:21 +00001001 MachineInstr *BundleMI = nullptr;
Evan Cheng1f5bee12009-08-10 06:57:42 +00001002
Craig Topper062a2ba2014-04-25 05:30:21 +00001003 CPSRDef = nullptr;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001004 HighLatencyCPSR = false;
1005
1006 // Check predecessors for the latest CPSRDef.
Jim Grosbach537f3ed2014-04-04 02:11:03 +00001007 for (auto *Pred : MBB.predecessors()) {
1008 const MBBInfo &PInfo = BlockInfo[Pred->getNumber()];
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001009 if (!PInfo.Visited) {
1010 // Since blocks are visited in RPO, this must be a back-edge.
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001011 continue;
1012 }
1013 if (PInfo.HighLatencyCPSR) {
1014 HighLatencyCPSR = true;
1015 break;
1016 }
1017 }
1018
Evan Chengf4807a12011-10-27 21:21:05 +00001019 // If this BB loops back to itself, conservatively avoid narrowing the
1020 // first instruction that does partial flag update.
1021 bool IsSelfLoop = MBB.isSuccessor(&MBB);
Jim Grosbach0c509fa2012-04-06 23:43:50 +00001022 MachineBasicBlock::instr_iterator MII = MBB.instr_begin(),E = MBB.instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00001023 MachineBasicBlock::instr_iterator NextMII;
Evan Cheng1be453b2009-08-08 03:21:23 +00001024 for (; MII != E; MII = NextMII) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001025 NextMII = std::next(MII);
Evan Cheng1be453b2009-08-08 03:21:23 +00001026
Evan Cheng51cbd2d2009-08-10 02:37:24 +00001027 MachineInstr *MI = &*MII;
Evan Cheng7fae11b2011-12-14 02:11:42 +00001028 if (MI->isBundle()) {
1029 BundleMI = MI;
1030 continue;
1031 }
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001032 if (MI->isDebugValue())
1033 continue;
Evan Cheng7fae11b2011-12-14 02:11:42 +00001034
Evan Cheng1e6c2a12009-08-12 01:49:45 +00001035 LiveCPSR = UpdateCPSRUse(*MI, LiveCPSR);
1036
Jakob Stoklund Olesen41bbf9c2012-12-18 00:46:39 +00001037 // Does NextMII belong to the same bundle as MI?
1038 bool NextInSameBundle = NextMII != E && NextMII->isBundledWithPred();
1039
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001040 if (ReduceMI(MBB, MI, LiveCPSR, IsSelfLoop)) {
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +00001041 Modified = true;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001042 MachineBasicBlock::instr_iterator I = std::prev(NextMII);
Jakob Stoklund Olesen43b1e132012-12-18 00:13:11 +00001043 MI = &*I;
Jakob Stoklund Olesen41bbf9c2012-12-18 00:46:39 +00001044 // Removing and reinserting the first instruction in a bundle will break
1045 // up the bundle. Fix the bundling if it was broken.
1046 if (NextInSameBundle && !NextMII->isBundledWithPred())
1047 NextMII->bundleWithPred();
Evan Cheng1be453b2009-08-08 03:21:23 +00001048 }
1049
Renato Golinf6ed8bb2016-05-12 12:33:33 +00001050 if (BundleMI && !NextInSameBundle && MI->isInsideBundle()) {
Evan Cheng7fae11b2011-12-14 02:11:42 +00001051 // FIXME: Since post-ra scheduler operates on bundles, the CPSR kill
1052 // marker is only on the BUNDLE instruction. Process the BUNDLE
1053 // instruction as we finish with the bundled instruction to work around
1054 // the inconsistency.
Evan Cheng903231b2011-12-17 01:25:34 +00001055 if (BundleMI->killsRegister(ARM::CPSR))
1056 LiveCPSR = false;
1057 MachineOperand *MO = BundleMI->findRegisterDefOperand(ARM::CPSR);
1058 if (MO && !MO->isDead())
1059 LiveCPSR = true;
Weiming Zhaof66be562014-01-13 18:47:54 +00001060 MO = BundleMI->findRegisterUseOperand(ARM::CPSR);
1061 if (MO && !MO->isKill())
1062 LiveCPSR = true;
Evan Cheng903231b2011-12-17 01:25:34 +00001063 }
Evan Cheng7fae11b2011-12-14 02:11:42 +00001064
Bob Wilsona2881ee2011-04-19 18:11:49 +00001065 bool DefCPSR = false;
1066 LiveCPSR = UpdateCPSRDef(*MI, LiveCPSR, DefCPSR);
Evan Cheng7f8e5632011-12-07 07:15:52 +00001067 if (MI->isCall()) {
Bob Wilsona2881ee2011-04-19 18:11:49 +00001068 // Calls don't really set CPSR.
Craig Topper062a2ba2014-04-25 05:30:21 +00001069 CPSRDef = nullptr;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001070 HighLatencyCPSR = false;
Evan Chengf4807a12011-10-27 21:21:05 +00001071 IsSelfLoop = false;
1072 } else if (DefCPSR) {
Bob Wilsona2881ee2011-04-19 18:11:49 +00001073 // This is the last CPSR defining instruction.
1074 CPSRDef = MI;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001075 HighLatencyCPSR = isHighLatencyCPSR(CPSRDef);
Evan Chengf4807a12011-10-27 21:21:05 +00001076 IsSelfLoop = false;
1077 }
Evan Cheng1be453b2009-08-08 03:21:23 +00001078 }
1079
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001080 MBBInfo &Info = BlockInfo[MBB.getNumber()];
1081 Info.HighLatencyCPSR = HighLatencyCPSR;
1082 Info.Visited = true;
Evan Cheng1be453b2009-08-08 03:21:23 +00001083 return Modified;
1084}
1085
1086bool Thumb2SizeReduce::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor1aa3cf72016-04-26 00:56:36 +00001087 if (PredicateFtor && !PredicateFtor(*MF.getFunction()))
Akira Hatanaka4a616192015-06-08 18:50:43 +00001088 return false;
1089
Eric Christopher1b21f002015-01-29 00:19:33 +00001090 STI = &static_cast<const ARMSubtarget &>(MF.getSubtarget());
Eric Christopher63b44882015-03-05 00:23:40 +00001091 if (STI->isThumb1Only() || STI->prefers32BitThumb())
1092 return false;
1093
Eric Christopher1b21f002015-01-29 00:19:33 +00001094 TII = static_cast<const Thumb2InstrInfo *>(STI->getInstrInfo());
Evan Cheng1be453b2009-08-08 03:21:23 +00001095
Sanjay Patel924879a2015-08-04 15:49:57 +00001096 // Optimizing / minimizing size? Minimizing size implies optimizing for size.
1097 OptimizeSize = MF.getFunction()->optForSize();
1098 MinimizeSize = MF.getFunction()->optForMinSize();
Quentin Colombet23b404d2012-12-18 22:47:16 +00001099
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001100 BlockInfo.clear();
1101 BlockInfo.resize(MF.getNumBlockIDs());
1102
1103 // Visit blocks in reverse post-order so LastCPSRDef is known for all
1104 // predecessors.
1105 ReversePostOrderTraversal<MachineFunction*> RPOT(&MF);
Evan Cheng1be453b2009-08-08 03:21:23 +00001106 bool Modified = false;
Jakob Stoklund Olesen299475e2013-04-04 18:25:36 +00001107 for (ReversePostOrderTraversal<MachineFunction*>::rpo_iterator
1108 I = RPOT.begin(), E = RPOT.end(); I != E; ++I)
1109 Modified |= ReduceMBB(**I);
Evan Cheng1be453b2009-08-08 03:21:23 +00001110 return Modified;
1111}
1112
1113/// createThumb2SizeReductionPass - Returns an instance of the Thumb2 size
1114/// reduction pass.
Akira Hatanaka4a616192015-06-08 18:50:43 +00001115FunctionPass *llvm::createThumb2SizeReductionPass(
1116 std::function<bool(const Function &)> Ftor) {
Benjamin Kramerd3f4c052016-06-12 16:13:55 +00001117 return new Thumb2SizeReduce(std::move(Ftor));
Evan Cheng1be453b2009-08-08 03:21:23 +00001118}