LLVM 24.0.0git
Thumb2SizeReduction.cpp
Go to the documentation of this file.
1//===-- Thumb2SizeReduction.cpp - Thumb2 code size reduction pass -*- C++ -*-=//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "ARM.h"
10#include "ARMBaseInstrInfo.h"
11#include "ARMSubtarget.h"
13#include "Thumb2InstrInfo.h"
14#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/Statistic.h"
20#include "llvm/ADT/StringRef.h"
29#include "llvm/IR/DebugLoc.h"
30#include "llvm/IR/Function.h"
31#include "llvm/MC/MCAsmInfo.h"
32#include "llvm/MC/MCInstrDesc.h"
34#include "llvm/Support/Debug.h"
37#include <cassert>
38#include <cstdint>
39#include <functional>
40#include <iterator>
41#include <utility>
42
43using namespace llvm;
44
45#define DEBUG_TYPE "thumb2-reduce-size"
46#define THUMB2_SIZE_REDUCE_NAME "Thumb2 instruction size reduce pass"
47
48STATISTIC(NumNarrows, "Number of 32-bit instrs reduced to 16-bit ones");
49STATISTIC(Num2Addrs, "Number of 32-bit instrs reduced to 2addr 16-bit ones");
50STATISTIC(NumLdSts, "Number of 32-bit load / store reduced to 16-bit ones");
51
52namespace {
53
54 /// ReduceTable - A static table with information on mapping from wide
55 /// opcodes to narrow
56 struct ReduceEntry {
57 uint16_t WideOpc; // Wide opcode
58 uint16_t NarrowOpc1; // Narrow opcode to transform to
59 uint16_t NarrowOpc2; // Narrow opcode when it's two-address
60 uint8_t Imm1Limit; // Limit of immediate field (bits)
61 uint8_t Imm2Limit; // Limit of immediate field when it's two-address
62 unsigned LowRegs1 : 1; // Only possible if low-registers are used
63 unsigned LowRegs2 : 1; // Only possible if low-registers are used (2addr)
64 unsigned PredCC1 : 2; // 0 - If predicated, cc is on and vice versa.
65 // 1 - No cc field.
66 // 2 - Always set CPSR.
67 unsigned PredCC2 : 2;
68 unsigned PartFlag : 1; // 16-bit instruction does partial flag update
69 unsigned Special : 1; // Needs to be dealt with specially
70 unsigned AvoidMovs: 1; // Avoid movs with shifter operand (for Swift)
71 };
72
73 static const ReduceEntry ReduceTable[] = {
74 // Wide, Narrow1, Narrow2, imm1,imm2, lo1, lo2, P/C,PF,S,AM
75 { ARM::t2ADCrr, 0, ARM::tADC, 0, 0, 0, 1, 0,0, 0,0,0 },
76 { ARM::t2ADDri, ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 0,0, 0,1,0 },
77 { ARM::t2ADDrr, ARM::tADDrr, ARM::tADDhirr, 0, 0, 1, 0, 0,1, 0,0,0 },
78 { ARM::t2ADDSri,ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 2,2, 0,1,0 },
79 { ARM::t2ADDSrr,ARM::tADDrr, 0, 0, 0, 1, 0, 2,0, 0,1,0 },
80 { ARM::t2ANDrr, 0, ARM::tAND, 0, 0, 0, 1, 0,0, 1,0,0 },
81 { ARM::t2ASRri, ARM::tASRri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
82 { ARM::t2ASRrr, 0, ARM::tASRrr, 0, 0, 0, 1, 0,0, 1,0,1 },
83 { ARM::t2BICrr, 0, ARM::tBIC, 0, 0, 0, 1, 0,0, 1,0,0 },
84 { ARM::t2CMNrr, ARM::tCMN, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
85 { ARM::t2CMPri, ARM::tCMPi8, 0, 8, 0, 1, 0, 2,0, 0,0,0 },
86 { ARM::t2CMPrr, ARM::tCMPhir, 0, 0, 0, 0, 0, 2,0, 0,1,0 },
87 { ARM::t2EORrr, 0, ARM::tEOR, 0, 0, 0, 1, 0,0, 1,0,0 },
88 // FIXME: adr.n immediate offset must be multiple of 4.
89 //{ ARM::t2LEApcrelJT,ARM::tLEApcrelJT, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
90 { ARM::t2LSLri, ARM::tLSLri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
91 { ARM::t2LSLrr, 0, ARM::tLSLrr, 0, 0, 0, 1, 0,0, 1,0,1 },
92 { ARM::t2LSRri, ARM::tLSRri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
93 { ARM::t2LSRrr, 0, ARM::tLSRrr, 0, 0, 0, 1, 0,0, 1,0,1 },
94 { ARM::t2MOVi, ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 1,0,0 },
95 { ARM::t2MOVi16,ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 1,1,0 },
96 // FIXME: Do we need the 16-bit 'S' variant?
97 { ARM::t2MOVr,ARM::tMOVr, 0, 0, 0, 0, 0, 1,0, 0,0,0 },
98 { ARM::t2MUL, 0, ARM::tMUL, 0, 0, 0, 1, 0,0, 1,0,0 },
99 { ARM::t2MVNr, ARM::tMVN, 0, 0, 0, 1, 0, 0,0, 0,0,0 },
100 { ARM::t2ORRrr, 0, ARM::tORR, 0, 0, 0, 1, 0,0, 1,0,0 },
101 { ARM::t2REV, ARM::tREV, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
102 { ARM::t2REV16, ARM::tREV16, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
103 { ARM::t2REVSH, ARM::tREVSH, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
104 { ARM::t2RORrr, 0, ARM::tROR, 0, 0, 0, 1, 0,0, 1,0,0 },
105 { ARM::t2RSBri, ARM::tRSB, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
106 { ARM::t2RSBSri,ARM::tRSB, 0, 0, 0, 1, 0, 2,0, 0,1,0 },
107 { ARM::t2SBCrr, 0, ARM::tSBC, 0, 0, 0, 1, 0,0, 0,0,0 },
108 { ARM::t2SUBri, ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 0,0, 0,0,0 },
109 { ARM::t2SUBrr, ARM::tSUBrr, 0, 0, 0, 1, 0, 0,0, 0,0,0 },
110 { ARM::t2SUBSri,ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 2,2, 0,0,0 },
111 { ARM::t2SUBSrr,ARM::tSUBrr, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
112 { ARM::t2SXTB, ARM::tSXTB, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
113 { ARM::t2SXTH, ARM::tSXTH, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
114 { ARM::t2TEQrr, ARM::tEOR, 0, 0, 0, 1, 0, 2,0, 0,1,0 },
115 { ARM::t2TSTrr, ARM::tTST, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
116 { ARM::t2UXTB, ARM::tUXTB, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
117 { ARM::t2UXTH, ARM::tUXTH, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
118
119 // FIXME: Clean this up after splitting each Thumb load / store opcode
120 // into multiple ones.
121 { ARM::t2LDRi12,ARM::tLDRi, ARM::tLDRspi, 5, 8, 1, 0, 0,0, 0,1,0 },
122 { ARM::t2LDRs, ARM::tLDRr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
123 { ARM::t2LDRBi12,ARM::tLDRBi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
124 { ARM::t2LDRBs, ARM::tLDRBr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
125 { ARM::t2LDRHi12,ARM::tLDRHi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
126 { ARM::t2LDRHs, ARM::tLDRHr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
127 { ARM::t2LDRSBs,ARM::tLDRSB, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
128 { ARM::t2LDRSHs,ARM::tLDRSH, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
129 { ARM::t2LDR_POST,ARM::tLDMIA_UPD,0, 0, 0, 1, 0, 0,0, 0,1,0 },
130 { ARM::t2STRi12,ARM::tSTRi, ARM::tSTRspi, 5, 8, 1, 0, 0,0, 0,1,0 },
131 { ARM::t2STRs, ARM::tSTRr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
132 { ARM::t2STRBi12,ARM::tSTRBi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
133 { ARM::t2STRBs, ARM::tSTRBr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
134 { ARM::t2STRHi12,ARM::tSTRHi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
135 { ARM::t2STRHs, ARM::tSTRHr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
136 { ARM::t2STR_POST,ARM::tSTMIA_UPD,0, 0, 0, 1, 0, 0,0, 0,1,0 },
137
138 { ARM::t2LDMIA, ARM::tLDMIA, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
139 { ARM::t2LDMIA_RET,0, ARM::tPOP_RET, 0, 0, 1, 1, 1,1, 0,1,0 },
140 { ARM::t2LDMIA_UPD,ARM::tLDMIA_UPD,ARM::tPOP,0, 0, 1, 1, 1,1, 0,1,0 },
141 // ARM::t2STMIA (with no basereg writeback) has no Thumb1 equivalent.
142 // tSTMIA_UPD is a change in semantics which can only be used if the base
143 // register is killed. This difference is correctly handled elsewhere.
144 { ARM::t2STMIA, ARM::tSTMIA_UPD, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
145 { ARM::t2STMIA_UPD,ARM::tSTMIA_UPD, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
146 { ARM::t2STMDB_UPD, 0, ARM::tPUSH, 0, 0, 1, 1, 1,1, 0,1,0 }
147 };
148
149 class Thumb2SizeReduce : public MachineFunctionPass {
150 public:
151 static char ID;
152
153 const Thumb2InstrInfo *TII;
154 const ARMSubtarget *STI;
155
156 Thumb2SizeReduce(std::function<bool(const Function &)> Ftor = nullptr);
157
158 bool runOnMachineFunction(MachineFunction &MF) override;
159
160 MachineFunctionProperties getRequiredProperties() const override {
161 return MachineFunctionProperties().setNoVRegs();
162 }
163
164 StringRef getPassName() const override {
166 }
167
168 void getAnalysisUsage(AnalysisUsage &AU) const override {
169 AU.addPreserved<MachineRegisterClassInfoWrapperPass>();
171 }
172
173 private:
174 /// ReduceOpcodeMap - Maps wide opcode to index of entry in ReduceTable.
175 DenseMap<unsigned, unsigned> ReduceOpcodeMap;
176
177 bool canAddPseudoFlagDep(const MachineInstr *Use, bool IsSelfLoop);
178
179 bool VerifyPredAndCC(const MachineInstr *MI, const ReduceEntry &Entry,
180 bool is2Addr, ARMCC::CondCodes Pred, bool LiveCPSR,
181 bool &HasCC, bool &CCDead);
182
183 bool ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
184 const ReduceEntry &Entry);
185
186 bool ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
187 const ReduceEntry &Entry, bool LiveCPSR, bool IsSelfLoop);
188
189 /// ReduceTo2Addr - Reduce a 32-bit instruction to a 16-bit two-address
190 /// instruction.
191 bool ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
192 const ReduceEntry &Entry, bool LiveCPSR,
193 bool IsSelfLoop);
194
195 /// ReduceToNarrow - Reduce a 32-bit instruction to a 16-bit
196 /// non-two-address instruction.
197 bool ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
198 const ReduceEntry &Entry, bool LiveCPSR,
199 bool IsSelfLoop);
200
201 /// ReduceMI - Attempt to reduce MI, return true on success.
202 bool ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI, bool LiveCPSR,
203 bool IsSelfLoop, bool SkipPrologueEpilogue);
204
205 /// ReduceMBB - Reduce width of instructions in the specified basic block.
206 bool ReduceMBB(MachineBasicBlock &MBB, bool SkipPrologueEpilogue);
207
208 bool OptimizeSize;
209 bool MinimizeSize;
210
211 // Last instruction to define CPSR in the current block.
212 MachineInstr *CPSRDef;
213 // Was CPSR last defined by a high latency instruction?
214 // When CPSRDef is null, this refers to CPSR defs in predecessors.
215 bool HighLatencyCPSR;
216
217 struct MBBInfo {
218 // The flags leaving this block have high latency.
219 bool HighLatencyCPSR = false;
220 // Has this block been visited yet?
221 bool Visited = false;
222
223 MBBInfo() = default;
224 };
225
226 SmallVector<MBBInfo, 8> BlockInfo;
227
228 std::function<bool(const Function &)> PredicateFtor;
229 };
230
231 char Thumb2SizeReduce::ID = 0;
232
233} // end anonymous namespace
234
236 false)
237
238Thumb2SizeReduce::Thumb2SizeReduce(std::function<bool(const Function &)> Ftor)
239 : MachineFunctionPass(ID), PredicateFtor(std::move(Ftor)) {
240 OptimizeSize = MinimizeSize = false;
241 for (unsigned i = 0, e = std::size(ReduceTable); i != e; ++i) {
242 unsigned FromOpc = ReduceTable[i].WideOpc;
243 if (!ReduceOpcodeMap.insert(std::make_pair(FromOpc, i)).second)
244 llvm_unreachable("Duplicated entries?");
245 }
246}
247
249 return is_contained(MCID.implicit_defs(), ARM::CPSR);
250}
251
252// Check for a likely high-latency flag def.
253static bool isHighLatencyCPSR(const MachineInstr *Def) {
254 switch (Def->getOpcode()) {
255 case ARM::FMSTAT:
256 case ARM::tMUL:
257 return true;
258 }
259 return false;
260}
261
262/// canAddPseudoFlagDep - For A9 (and other out-of-order) implementations,
263/// the 's' 16-bit instruction partially update CPSR. Abort the
264/// transformation to avoid adding false dependency on last CPSR setting
265/// instruction which hurts the ability for out-of-order execution engine
266/// to do register renaming magic.
267/// This function checks if there is a read-of-write dependency between the
268/// last instruction that defines the CPSR and the current instruction. If there
269/// is, then there is no harm done since the instruction cannot be retired
270/// before the CPSR setting instruction anyway.
271/// Note, we are not doing full dependency analysis here for the sake of compile
272/// time. We're not looking for cases like:
273/// r0 = muls ...
274/// r1 = add.w r0, ...
275/// ...
276/// = mul.w r1
277/// In this case it would have been ok to narrow the mul.w to muls since there
278/// are indirect RAW dependency between the muls and the mul.w
279bool Thumb2SizeReduce::canAddPseudoFlagDep(const MachineInstr *Use,
280 bool FirstInSelfLoop) {
281 // Disable the check for -Oz (aka OptimizeForSizeHarder).
282 if (MinimizeSize || !STI->avoidCPSRPartialUpdate())
283 return false;
284
285 if (!CPSRDef)
286 // If this BB loops back to itself, conservatively avoid narrowing the
287 // first instruction that does partial flag update.
288 return HighLatencyCPSR || FirstInSelfLoop;
289
290 SmallSet<unsigned, 2> Defs;
291 for (const MachineOperand &MO : CPSRDef->operands()) {
292 if (!MO.isReg() || MO.isUndef() || MO.isUse())
293 continue;
294 Register Reg = MO.getReg();
295 if (Reg == 0 || Reg == ARM::CPSR)
296 continue;
297 Defs.insert(Reg);
298 }
299
300 for (const MachineOperand &MO : Use->operands()) {
301 if (!MO.isReg() || MO.isUndef() || MO.isDef())
302 continue;
303 Register Reg = MO.getReg();
304 if (Defs.count(Reg))
305 return false;
306 }
307
308 // If the current CPSR has high latency, try to avoid the false dependency.
309 if (HighLatencyCPSR)
310 return true;
311
312 // tMOVi8 usually doesn't start long dependency chains, and there are a lot
313 // of them, so always shrink them when CPSR doesn't have high latency.
314 if (Use->getOpcode() == ARM::t2MOVi ||
315 Use->getOpcode() == ARM::t2MOVi16)
316 return false;
317
318 // No read-after-write dependency. The narrowing will add false dependency.
319 return true;
320}
321
322bool Thumb2SizeReduce::VerifyPredAndCC(const MachineInstr *MI,
323 const ReduceEntry &Entry, bool is2Addr,
324 ARMCC::CondCodes Pred, bool LiveCPSR,
325 bool &HasCC, bool &CCDead) {
326 if ((is2Addr && Entry.PredCC2 == 0) ||
327 (!is2Addr && Entry.PredCC1 == 0)) {
328 if (Pred == ARMCC::AL) {
329 // Not predicated, must set CPSR.
330 if (!HasCC) {
331 // Original instruction was not setting CPSR, but CPSR is not
332 // currently live anyway. It's ok to set it. The CPSR def is
333 // dead though.
334 if (!LiveCPSR) {
335 HasCC = true;
336 CCDead = true;
337 return true;
338 }
339 return false;
340 }
341 } else {
342 // Predicated, must not set CPSR.
343 if (HasCC)
344 return false;
345 }
346 } else if ((is2Addr && Entry.PredCC2 == 2) ||
347 (!is2Addr && Entry.PredCC1 == 2)) {
348 /// Old opcode has an optional def of CPSR.
349 if (HasCC)
350 return true;
351 // If old opcode does not implicitly define CPSR, then it's not ok since
352 // these new opcodes' CPSR def is not meant to be thrown away. e.g. CMP.
353 if (!HasImplicitCPSRDef(MI->getDesc()))
354 return false;
355 HasCC = true;
356 } else {
357 // 16-bit instruction does not set CPSR.
358 if (HasCC)
359 return false;
360 }
361
362 return true;
363}
364
365static bool VerifyLowRegs(const MachineInstr *MI) {
366 unsigned Opc = MI->getOpcode();
367 bool isPCOk = (Opc == ARM::t2LDMIA_RET || Opc == ARM::t2LDMIA_UPD);
368 bool isLROk = (Opc == ARM::t2STMDB_UPD);
369 bool isSPOk = isPCOk || isLROk;
370 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
371 const MachineOperand &MO = MI->getOperand(i);
372 if (!MO.isReg() || MO.isImplicit())
373 continue;
374 Register Reg = MO.getReg();
375 if (Reg == 0 || Reg == ARM::CPSR)
376 continue;
377 if (isPCOk && Reg == ARM::PC)
378 continue;
379 if (isLROk && Reg == ARM::LR)
380 continue;
381 if (Reg == ARM::SP) {
382 if (isSPOk)
383 continue;
384 if (i == 1 && (Opc == ARM::t2LDRi12 || Opc == ARM::t2STRi12))
385 // Special case for these ldr / str with sp as base register.
386 continue;
387 }
388 if (!isARMLowRegister(Reg))
389 return false;
390 }
391 return true;
392}
393
394bool
395Thumb2SizeReduce::ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
396 const ReduceEntry &Entry) {
397 int ReduceLimitLdSt = STI->getCLOpts().t2_reduce_limit3;
398 if (ReduceLimitLdSt != -1 && ((int)NumLdSts >= ReduceLimitLdSt))
399 return false;
400
401 unsigned Scale = 1;
402 bool HasImmOffset = false;
403 bool HasShift = false;
404 bool HasOffReg = true;
405 bool isLdStMul = false;
406 unsigned Opc = Entry.NarrowOpc1;
407 unsigned OpNum = 3; // First 'rest' of operands.
408 uint8_t ImmLimit = Entry.Imm1Limit;
409
410 switch (Entry.WideOpc) {
411 default:
412 llvm_unreachable("Unexpected Thumb2 load / store opcode!");
413 case ARM::t2LDRi12:
414 case ARM::t2STRi12:
415 if (MI->getOperand(1).getReg() == ARM::SP) {
416 Opc = Entry.NarrowOpc2;
417 ImmLimit = Entry.Imm2Limit;
418 }
419
420 Scale = 4;
421 HasImmOffset = true;
422 HasOffReg = false;
423 break;
424 case ARM::t2LDRBi12:
425 case ARM::t2STRBi12:
426 HasImmOffset = true;
427 HasOffReg = false;
428 break;
429 case ARM::t2LDRHi12:
430 case ARM::t2STRHi12:
431 Scale = 2;
432 HasImmOffset = true;
433 HasOffReg = false;
434 break;
435 case ARM::t2LDRs:
436 case ARM::t2LDRBs:
437 case ARM::t2LDRHs:
438 case ARM::t2LDRSBs:
439 case ARM::t2LDRSHs:
440 case ARM::t2STRs:
441 case ARM::t2STRBs:
442 case ARM::t2STRHs:
443 HasShift = true;
444 OpNum = 4;
445 break;
446 case ARM::t2LDR_POST:
447 case ARM::t2STR_POST: {
448 if (!MinimizeSize)
449 return false;
450
451 if (!MI->hasOneMemOperand() ||
452 (*MI->memoperands_begin())->getAlign() < Align(4))
453 return false;
454
455 // We're creating a completely different type of load/store - LDM from LDR.
456 // For this reason we can't reuse the logic at the end of this function; we
457 // have to implement the MI building here.
458 bool IsStore = Entry.WideOpc == ARM::t2STR_POST;
459 Register Rt = MI->getOperand(IsStore ? 1 : 0).getReg();
460 Register Rn = MI->getOperand(IsStore ? 0 : 1).getReg();
461 unsigned Offset = MI->getOperand(3).getImm();
462 unsigned PredImm = MI->getOperand(4).getImm();
463 Register PredReg = MI->getOperand(5).getReg();
466
467 if (Offset != 4)
468 return false;
469
470 // Add the 16-bit load / store instruction.
471 DebugLoc dl = MI->getDebugLoc();
472 auto MIB = BuildMI(MBB, MI, dl, TII->get(Entry.NarrowOpc1))
473 .addReg(Rn, RegState::Define)
474 .addReg(Rn)
475 .addImm(PredImm)
476 .addReg(PredReg)
477 .addReg(Rt, getDefRegState(!IsStore));
478
479 // Transfer memoperands.
480 MIB.setMemRefs(MI->memoperands());
481
482 // Transfer MI flags.
483 MIB.setMIFlags(MI->getFlags());
484
485 // Kill the old instruction.
487 ++NumLdSts;
488 return true;
489 }
490 case ARM::t2LDMIA: {
491 Register BaseReg = MI->getOperand(0).getReg();
492 assert(isARMLowRegister(BaseReg));
493
494 // For the non-writeback version (this one), the base register must be
495 // one of the registers being loaded.
496 bool isOK = false;
497 for (const MachineOperand &MO : llvm::drop_begin(MI->operands(), 3)) {
498 if (MO.getReg() == BaseReg) {
499 isOK = true;
500 break;
501 }
502 }
503
504 if (!isOK)
505 return false;
506
507 OpNum = 0;
508 isLdStMul = true;
509 break;
510 }
511 case ARM::t2STMIA: {
512 // t2STMIA is reduced to tSTMIA_UPD which has writeback. We can only do this
513 // if the base register is killed, as then it doesn't matter what its value
514 // is after the instruction.
515 if (!MI->getOperand(0).isKill())
516 return false;
517
518 // If the base register is in the register list and isn't the lowest
519 // numbered register (i.e. it's in operand 4 onwards) then with writeback
520 // the stored value is unknown, so we can't convert to tSTMIA_UPD.
521 Register BaseReg = MI->getOperand(0).getReg();
522 for (const MachineOperand &MO : llvm::drop_begin(MI->operands(), 4))
523 if (MO.getReg() == BaseReg)
524 return false;
525
526 break;
527 }
528 case ARM::t2LDMIA_RET: {
529 Register BaseReg = MI->getOperand(1).getReg();
530 if (BaseReg != ARM::SP)
531 return false;
532 Opc = Entry.NarrowOpc2; // tPOP_RET
533 OpNum = 2;
534 isLdStMul = true;
535 break;
536 }
537 case ARM::t2LDMIA_UPD:
538 case ARM::t2STMIA_UPD:
539 case ARM::t2STMDB_UPD: {
540 OpNum = 0;
541
542 Register BaseReg = MI->getOperand(1).getReg();
543 if (BaseReg == ARM::SP &&
544 (Entry.WideOpc == ARM::t2LDMIA_UPD ||
545 Entry.WideOpc == ARM::t2STMDB_UPD)) {
546 Opc = Entry.NarrowOpc2; // tPOP or tPUSH
547 OpNum = 2;
548 } else if (!isARMLowRegister(BaseReg) ||
549 (Entry.WideOpc != ARM::t2LDMIA_UPD &&
550 Entry.WideOpc != ARM::t2STMIA_UPD)) {
551 return false;
552 }
553
554 isLdStMul = true;
555 break;
556 }
557 }
558
559 unsigned OffsetReg = 0;
560 bool OffsetKill = false;
561 bool OffsetInternal = false;
562 if (HasShift) {
563 OffsetReg = MI->getOperand(2).getReg();
564 OffsetKill = MI->getOperand(2).isKill();
565 OffsetInternal = MI->getOperand(2).isInternalRead();
566
567 if (MI->getOperand(3).getImm())
568 // Thumb1 addressing mode doesn't support shift.
569 return false;
570 }
571
572 unsigned OffsetImm = 0;
573 if (HasImmOffset) {
574 OffsetImm = MI->getOperand(2).getImm();
575 unsigned MaxOffset = ((1 << ImmLimit) - 1) * Scale;
576
577 if ((OffsetImm & (Scale - 1)) || OffsetImm > MaxOffset)
578 // Make sure the immediate field fits.
579 return false;
580 }
581
582 // Add the 16-bit load / store instruction.
583 DebugLoc dl = MI->getDebugLoc();
584 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, TII->get(Opc));
585
586 // tSTMIA_UPD takes a defining register operand. We've already checked that
587 // the register is killed, so mark it as dead here.
588 if (Entry.WideOpc == ARM::t2STMIA)
589 MIB.addReg(MI->getOperand(0).getReg(), RegState::Define | RegState::Dead);
590
591 if (!isLdStMul) {
592 MIB.add(MI->getOperand(0));
593 MIB.add(MI->getOperand(1));
594
595 if (HasImmOffset)
596 MIB.addImm(OffsetImm / Scale);
597
598 assert((!HasShift || OffsetReg) && "Invalid so_reg load / store address!");
599
600 if (HasOffReg)
601 MIB.addReg(OffsetReg, getKillRegState(OffsetKill) |
602 getInternalReadRegState(OffsetInternal));
603 }
604
605 // Transfer the rest of operands.
606 for (const MachineOperand &MO : llvm::drop_begin(MI->operands(), OpNum))
607 MIB.add(MO);
608
609 // Transfer memoperands.
610 MIB.setMemRefs(MI->memoperands());
611
612 // Transfer MI flags.
613 MIB.setMIFlags(MI->getFlags());
614
615 LLVM_DEBUG(dbgs() << "Converted 32-bit: " << *MI
616 << " to 16-bit: " << *MIB);
617
619 ++NumLdSts;
620 return true;
621}
622
623bool
624Thumb2SizeReduce::ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
625 const ReduceEntry &Entry,
626 bool LiveCPSR, bool IsSelfLoop) {
627 unsigned Opc = MI->getOpcode();
628 if (Opc == ARM::t2ADDri) {
629 // If the source register is SP, try to reduce to tADDrSPi, otherwise
630 // it's a normal reduce.
631 if (MI->getOperand(1).getReg() != ARM::SP) {
632 if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
633 return true;
634 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
635 }
636 // Try to reduce to tADDrSPi.
637 unsigned Imm = MI->getOperand(2).getImm();
638 // The immediate must be in range, the destination register must be a low
639 // reg, the predicate must be "always" and the condition flags must not
640 // be being set.
641 if (Imm & 3 || Imm > 1020)
642 return false;
643 if (!isARMLowRegister(MI->getOperand(0).getReg()))
644 return false;
645 if (MI->getOperand(3).getImm() != ARMCC::AL)
646 return false;
647 const MCInstrDesc &MCID = MI->getDesc();
648 if (MCID.hasOptionalDef() &&
649 MI->getOperand(MCID.getNumOperands()-1).getReg() == ARM::CPSR)
650 return false;
651
652 MachineInstrBuilder MIB =
653 BuildMI(MBB, MI, MI->getDebugLoc(),
654 TII->get(ARM::tADDrSPi))
655 .add(MI->getOperand(0))
656 .add(MI->getOperand(1))
657 .addImm(Imm / 4) // The tADDrSPi has an implied scale by four.
659
660 // Transfer MI flags.
661 MIB.setMIFlags(MI->getFlags());
662
663 LLVM_DEBUG(dbgs() << "Converted 32-bit: " << *MI
664 << " to 16-bit: " << *MIB);
665
667 ++NumNarrows;
668 return true;
669 }
670
671 if (Entry.LowRegs1 && !VerifyLowRegs(MI))
672 return false;
673
674 if (MI->mayLoadOrStore())
675 return ReduceLoadStore(MBB, MI, Entry);
676
677 switch (Opc) {
678 default: break;
679 case ARM::t2ADDSri:
680 case ARM::t2ADDSrr: {
681 Register PredReg;
682 if (getInstrPredicate(*MI, PredReg) == ARMCC::AL) {
683 switch (Opc) {
684 default: break;
685 case ARM::t2ADDSri:
686 if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
687 return true;
688 [[fallthrough]];
689 case ARM::t2ADDSrr:
690 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
691 }
692 }
693 break;
694 }
695 case ARM::t2RSBri:
696 case ARM::t2RSBSri:
697 case ARM::t2SXTB:
698 case ARM::t2SXTH:
699 case ARM::t2UXTB:
700 case ARM::t2UXTH:
701 if (MI->getOperand(2).getImm() == 0)
702 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
703 break;
704 case ARM::t2MOVi16:
705 // Can convert only 'pure' immediate operands, not immediates obtained as
706 // globals' addresses.
707 if (MI->getOperand(1).isImm())
708 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
709 break;
710 case ARM::t2CMPrr: {
711 // Try to reduce to the lo-reg only version first. Why there are two
712 // versions of the instruction is a mystery.
713 // It would be nice to just have two entries in the main table that
714 // are prioritized, but the table assumes a unique entry for each
715 // source insn opcode. So for now, we hack a local entry record to use.
716 static const ReduceEntry NarrowEntry =
717 { ARM::t2CMPrr,ARM::tCMPr, 0, 0, 0, 1, 1,2, 0, 0,1,0 };
718 if (ReduceToNarrow(MBB, MI, NarrowEntry, LiveCPSR, IsSelfLoop))
719 return true;
720 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
721 }
722 case ARM::t2TEQrr: {
723 Register PredReg;
724 // Can only convert to eors if we're not in an IT block.
725 if (getInstrPredicate(*MI, PredReg) != ARMCC::AL)
726 break;
727 // TODO if Operand 0 is not killed but Operand 1 is, then we could write
728 // to Op1 instead.
729 if (MI->getOperand(0).isKill())
730 return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
731 }
732 }
733 return false;
734}
735
736bool
737Thumb2SizeReduce::ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
738 const ReduceEntry &Entry,
739 bool LiveCPSR, bool IsSelfLoop) {
740 int ReduceLimit2Addr = STI->getCLOpts().t2_reduce_limit2;
741 if (ReduceLimit2Addr != -1 && ((int)Num2Addrs >= ReduceLimit2Addr))
742 return false;
743
744 if (!OptimizeSize && Entry.AvoidMovs && STI->avoidMOVsShifterOperand())
745 // Don't issue movs with shifter operand for some CPUs unless we
746 // are optimizing for size.
747 return false;
748
749 Register Reg0 = MI->getOperand(0).getReg();
750 Register Reg1 = MI->getOperand(1).getReg();
751 // t2MUL is "special". The tied source operand is second, not first.
752 if (MI->getOpcode() == ARM::t2MUL) {
753 // MULS can be slower than MUL
754 if (!MinimizeSize && STI->avoidMULS())
755 return false;
756 Register Reg2 = MI->getOperand(2).getReg();
757 // Early exit if the regs aren't all low regs.
758 if (!isARMLowRegister(Reg0) || !isARMLowRegister(Reg1)
759 || !isARMLowRegister(Reg2))
760 return false;
761 if (Reg0 != Reg2) {
762 // If the other operand also isn't the same as the destination, we
763 // can't reduce.
764 if (Reg1 != Reg0)
765 return false;
766 // Try to commute the operands to make it a 2-address instruction.
767 MachineInstr *CommutedMI = TII->commuteInstruction(*MI);
768 if (!CommutedMI)
769 return false;
770 }
771 } else if (Reg0 != Reg1) {
772 // Try to commute the operands to make it a 2-address instruction.
773 unsigned CommOpIdx1 = 1;
774 unsigned CommOpIdx2 = TargetInstrInfo::CommuteAnyOperandIndex;
775 if (!TII->findCommutedOpIndices(*MI, CommOpIdx1, CommOpIdx2) ||
776 MI->getOperand(CommOpIdx2).getReg() != Reg0)
777 return false;
778 MachineInstr *CommutedMI =
779 TII->commuteInstruction(*MI, false, CommOpIdx1, CommOpIdx2);
780 if (!CommutedMI)
781 return false;
782 }
783 if (Entry.LowRegs2 && !isARMLowRegister(Reg0))
784 return false;
785 if (Entry.Imm2Limit) {
786 unsigned Imm = MI->getOperand(2).getImm();
787 unsigned Limit = (1 << Entry.Imm2Limit) - 1;
788 if (Imm > Limit)
789 return false;
790 } else {
791 Register Reg2 = MI->getOperand(2).getReg();
792 if (Entry.LowRegs2 && !isARMLowRegister(Reg2))
793 return false;
794 }
795
796 // Check if it's possible / necessary to transfer the predicate.
797 const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc2);
798 Register PredReg;
800 bool SkipPred = false;
801 if (Pred != ARMCC::AL) {
802 if (!NewMCID.isPredicable())
803 // Can't transfer predicate, fail.
804 return false;
805 } else {
806 SkipPred = !NewMCID.isPredicable();
807 }
808
809 bool HasCC = false;
810 bool CCDead = false;
811 const MCInstrDesc &MCID = MI->getDesc();
812 if (MCID.hasOptionalDef()) {
813 unsigned NumOps = MCID.getNumOperands();
814 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
815 if (HasCC && MI->getOperand(NumOps-1).isDead())
816 CCDead = true;
817 }
818 if (!VerifyPredAndCC(MI, Entry, true, Pred, LiveCPSR, HasCC, CCDead))
819 return false;
820
821 // Avoid adding a false dependency on partial flag update by some 16-bit
822 // instructions which has the 's' bit set.
823 if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
824 canAddPseudoFlagDep(MI, IsSelfLoop))
825 return false;
826
827 // Add the 16-bit instruction.
828 DebugLoc dl = MI->getDebugLoc();
829 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
830 MIB.add(MI->getOperand(0));
831 if (NewMCID.hasOptionalDef())
832 MIB.add(HasCC ? t1CondCodeOp(CCDead) : condCodeOp());
833
834 // Transfer the rest of operands.
835 unsigned NumOps = MCID.getNumOperands();
836 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
837 if (i < NumOps && MCID.operands()[i].isOptionalDef())
838 continue;
839 if (SkipPred && MCID.operands()[i].isPredicate())
840 continue;
841 MIB.add(MI->getOperand(i));
842 }
843
844 // Transfer MI flags.
845 MIB.setMIFlags(MI->getFlags());
846
847 LLVM_DEBUG(dbgs() << "Converted 32-bit: " << *MI
848 << " to 16-bit: " << *MIB);
849
851 ++Num2Addrs;
852 return true;
853}
854
855bool
856Thumb2SizeReduce::ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
857 const ReduceEntry &Entry,
858 bool LiveCPSR, bool IsSelfLoop) {
859 int ReduceLimit = STI->getCLOpts().t2_reduce_limit;
860 if (ReduceLimit != -1 && ((int)NumNarrows >= ReduceLimit))
861 return false;
862
863 if (!OptimizeSize && Entry.AvoidMovs && STI->avoidMOVsShifterOperand())
864 // Don't issue movs with shifter operand for some CPUs unless we
865 // are optimizing for size.
866 return false;
867
868 unsigned Limit = ~0U;
869 if (Entry.Imm1Limit)
870 Limit = (1 << Entry.Imm1Limit) - 1;
871
872 const MCInstrDesc &MCID = MI->getDesc();
873 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i) {
874 if (MCID.operands()[i].isPredicate())
875 continue;
876 const MachineOperand &MO = MI->getOperand(i);
877 if (MO.isReg()) {
878 Register Reg = MO.getReg();
879 if (!Reg || Reg == ARM::CPSR)
880 continue;
881 if (Entry.LowRegs1 && !isARMLowRegister(Reg))
882 return false;
883 } else if (MO.isImm() && !MCID.operands()[i].isPredicate()) {
884 if (((unsigned)MO.getImm()) > Limit)
885 return false;
886 }
887 }
888
889 // Check if it's possible / necessary to transfer the predicate.
890 const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc1);
891 Register PredReg;
893 bool SkipPred = false;
894 if (Pred != ARMCC::AL) {
895 if (!NewMCID.isPredicable())
896 // Can't transfer predicate, fail.
897 return false;
898 } else {
899 SkipPred = !NewMCID.isPredicable();
900 }
901
902 bool HasCC = false;
903 bool CCDead = false;
904 if (MCID.hasOptionalDef()) {
905 unsigned NumOps = MCID.getNumOperands();
906 HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
907 if (HasCC && MI->getOperand(NumOps-1).isDead())
908 CCDead = true;
909 }
910 if (!VerifyPredAndCC(MI, Entry, false, Pred, LiveCPSR, HasCC, CCDead))
911 return false;
912
913 // Avoid adding a false dependency on partial flag update by some 16-bit
914 // instructions which has the 's' bit set.
915 if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
916 canAddPseudoFlagDep(MI, IsSelfLoop))
917 return false;
918
919 // Add the 16-bit instruction.
920 DebugLoc dl = MI->getDebugLoc();
921 MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
922
923 // TEQ is special in that it doesn't define a register but we're converting
924 // it into an EOR which does. So add the first operand as a def and then
925 // again as a use.
926 if (MCID.getOpcode() == ARM::t2TEQrr) {
927 MIB.add(MI->getOperand(0));
928 MIB->getOperand(0).setIsKill(false);
929 MIB->getOperand(0).setIsDef(true);
930 MIB->getOperand(0).setIsDead(true);
931
932 if (NewMCID.hasOptionalDef())
933 MIB.add(HasCC ? t1CondCodeOp(CCDead) : condCodeOp());
934 MIB.add(MI->getOperand(0));
935 } else {
936 MIB.add(MI->getOperand(0));
937 if (NewMCID.hasOptionalDef())
938 MIB.add(HasCC ? t1CondCodeOp(CCDead) : condCodeOp());
939 }
940
941 // Transfer the rest of operands.
942 unsigned NumOps = MCID.getNumOperands();
943 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
944 if (i < NumOps && MCID.operands()[i].isOptionalDef())
945 continue;
946 if ((MCID.getOpcode() == ARM::t2RSBSri ||
947 MCID.getOpcode() == ARM::t2RSBri ||
948 MCID.getOpcode() == ARM::t2SXTB ||
949 MCID.getOpcode() == ARM::t2SXTH ||
950 MCID.getOpcode() == ARM::t2UXTB ||
951 MCID.getOpcode() == ARM::t2UXTH) && i == 2)
952 // Skip the zero immediate operand, it's now implicit.
953 continue;
954 bool isPred = (i < NumOps && MCID.operands()[i].isPredicate());
955 if (SkipPred && isPred)
956 continue;
957 const MachineOperand &MO = MI->getOperand(i);
958 if (MO.isReg() && MO.isImplicit() && MO.getReg() == ARM::CPSR)
959 // Skip implicit def of CPSR. Either it's modeled as an optional
960 // def now or it's already an implicit def on the new instruction.
961 continue;
962 MIB.add(MO);
963 }
964 if (!MCID.isPredicable() && NewMCID.isPredicable())
965 MIB.add(predOps(ARMCC::AL));
966
967 // Transfer MI flags.
968 MIB.setMIFlags(MI->getFlags());
969
970 LLVM_DEBUG(dbgs() << "Converted 32-bit: " << *MI
971 << " to 16-bit: " << *MIB);
972
974 ++NumNarrows;
975 return true;
976}
977
978bool Thumb2SizeReduce::ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI,
979 bool LiveCPSR, bool IsSelfLoop,
980 bool SkipPrologueEpilogue) {
981 unsigned Opcode = MI->getOpcode();
982 auto OPI = ReduceOpcodeMap.find(Opcode);
983 if (OPI == ReduceOpcodeMap.end())
984 return false;
985 if (SkipPrologueEpilogue && (MI->getFlag(MachineInstr::FrameSetup) ||
987 return false;
988 const ReduceEntry &Entry = ReduceTable[OPI->second];
989
990 // Don't attempt normal reductions on "special" cases for now.
991 if (Entry.Special)
992 return ReduceSpecial(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
993
994 // Try to transform to a 16-bit two-address instruction.
995 if (Entry.NarrowOpc2 &&
996 ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
997 return true;
998
999 // Try to transform to a 16-bit non-two-address instruction.
1000 if (Entry.NarrowOpc1 &&
1001 ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
1002 return true;
1003
1004 return false;
1005}
1006
1007bool Thumb2SizeReduce::ReduceMBB(MachineBasicBlock &MBB,
1008 bool SkipPrologueEpilogue) {
1009 bool Modified = false;
1010
1011 // Narrowing to a flag-setting form is only legal where CPSR is dead.
1012 bool LiveCPSR = any_of(MBB.successors(), [](const MachineBasicBlock *Succ) {
1013 return Succ->isLiveIn(ARM::CPSR);
1014 });
1015
1016 DenseMap<const MachineInstr *, bool> CPSRLiveAfter;
1017 for (MachineInstr &MI : reverse(MBB.instrs())) {
1018 if (MI.isBundle() || MI.isDebugInstr())
1019 continue;
1020 if (ReduceOpcodeMap.contains(MI.getOpcode()))
1021 CPSRLiveAfter[&MI] = LiveCPSR;
1022 if (MI.definesRegister(ARM::CPSR, /*TRI=*/nullptr))
1023 LiveCPSR = false;
1024 if (MI.readsRegister(ARM::CPSR, /*TRI=*/nullptr))
1025 LiveCPSR = true;
1026 }
1027
1028 CPSRDef = nullptr;
1029 HighLatencyCPSR = false;
1030
1031 // Check predecessors for the latest CPSRDef.
1032 for (auto *Pred : MBB.predecessors()) {
1033 const MBBInfo &PInfo = BlockInfo[Pred->getNumber()];
1034 if (!PInfo.Visited) {
1035 // Since blocks are visited in RPO, this must be a back-edge.
1036 continue;
1037 }
1038
1039 if (PInfo.HighLatencyCPSR) {
1040 HighLatencyCPSR = true;
1041 break;
1042 }
1043 }
1044
1045 // If this BB loops back to itself, conservatively avoid narrowing the
1046 // first instruction that does partial flag update.
1047 bool IsSelfLoop = MBB.isSuccessor(&MBB);
1050 for (; MII != E; MII = NextMII) {
1051 NextMII = std::next(MII);
1052
1053 MachineInstr *MI = &*MII;
1054 if (MI->isBundle() || MI->isDebugInstr())
1055 continue;
1056
1057 LiveCPSR = CPSRLiveAfter.lookup(MI);
1058
1059 // Does NextMII belong to the same bundle as MI?
1060 bool NextInSameBundle = NextMII != E && NextMII->isBundledWithPred();
1061
1062 if (ReduceMI(MBB, MI, LiveCPSR, IsSelfLoop, SkipPrologueEpilogue)) {
1063 Modified = true;
1064 MachineBasicBlock::instr_iterator I = std::prev(NextMII);
1065 MI = &*I;
1066 // Removing and reinserting the first instruction in a bundle will break
1067 // up the bundle. Fix the bundling if it was broken.
1068 if (NextInSameBundle && !NextMII->isBundledWithPred())
1069 NextMII->bundleWithPred();
1070 }
1071
1072 // Maintain CPSRDef as the most recent CPSR-defining instruction in program
1073 // order, so canAddPseudoFlagDep can consult it. MI is inspected after
1074 // reduction, so a candidate just narrowed to a flag-setting form counts.
1075 if (MI->isCall()) {
1076 // Calls don't really set CPSR.
1077 CPSRDef = nullptr;
1078 HighLatencyCPSR = false;
1079 IsSelfLoop = false;
1080 } else if (MI->definesRegister(ARM::CPSR, /*TRI=*/nullptr)) {
1081 // This is the last CPSR defining instruction.
1082 CPSRDef = MI;
1083 HighLatencyCPSR = isHighLatencyCPSR(CPSRDef);
1084 IsSelfLoop = false;
1085 }
1086 }
1087
1088 MBBInfo &Info = BlockInfo[MBB.getNumber()];
1089 Info.HighLatencyCPSR = HighLatencyCPSR;
1090 Info.Visited = true;
1091 return Modified;
1092}
1093
1094bool Thumb2SizeReduce::runOnMachineFunction(MachineFunction &MF) {
1095 if (PredicateFtor && !PredicateFtor(MF.getFunction()))
1096 return false;
1097
1098 STI = &MF.getSubtarget<ARMSubtarget>();
1099 if (STI->isThumb1Only() || STI->prefers32BitThumb())
1100 return false;
1101
1102 TII = static_cast<const Thumb2InstrInfo *>(STI->getInstrInfo());
1103
1104 // Optimizing / minimizing size? Minimizing size implies optimizing for size.
1105 OptimizeSize = MF.getFunction().hasOptSize();
1106 MinimizeSize = STI->hasMinSize();
1107
1108 BlockInfo.clear();
1109 BlockInfo.resize(MF.getNumBlockIDs());
1110
1111 // Visit blocks in reverse post-order so LastCPSRDef is known for all
1112 // predecessors.
1113 ReversePostOrderTraversal<MachineFunction*> RPOT(&MF);
1114 bool Modified = false;
1115 bool NeedsWinCFI = MF.needsWinCFI();
1116 for (MachineBasicBlock *MBB : RPOT)
1117 Modified |= ReduceMBB(*MBB, /*SkipPrologueEpilogue=*/NeedsWinCFI);
1118 return Modified;
1119}
1120
1121/// createThumb2SizeReductionPass - Returns an instance of the Thumb2 size
1122/// reduction pass.
1124 std::function<bool(const Function &)> Ftor) {
1125 return new Thumb2SizeReduce(std::move(Ftor));
1126}
SDValue Pred
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
const AArch64InstrInfo & TII
uint64_t Imm
MachineBasicBlock & MBB
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
#define DEBUG_TYPE
IRTranslator LLVM IR MI
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
unsigned Opc
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
static bool ReduceMBB(MachineBasicBlock &MBB, const MipsInstrInfo *MipsII)
Reduces width of instructions in the specified basic block.
static ReduceEntryVector ReduceTable
static bool ReduceMI(const MachineBasicBlock::instr_iterator &MII, MachineBasicBlock::instr_iterator &NextMII, const MipsInstrInfo *MipsII)
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
dot regions Print regions of function to dot true view regions View regions of function(with no function bodies)"
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:170
#define LLVM_DEBUG(...)
Definition Debug.h:119
static bool HasImplicitCPSRDef(const MCInstrDesc &MCID)
static bool VerifyLowRegs(const MachineInstr *MI)
static bool isHighLatencyCPSR(const MachineInstr *Def)
#define THUMB2_SIZE_REDUCE_NAME
const ARMBaseInstrInfo * getInstrInfo() const override
bool isThumb1Only() const
const ARMOptions & getCLOpts() const
bool hasMinSize() const
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition DenseMap.h:757
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:766
iterator end()
Definition DenseMap.h:686
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:793
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition Function.h:708
Describe properties that are true of each instruction in the target description file.
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
ArrayRef< MCOperandInfo > operands() const
bool hasOptionalDef() const
Set if this instruction has an optional definition, e.g.
bool isPredicable() const
Return true if this instruction has a predicate operand that controls execution.
unsigned getOpcode() const
Return the opcode number for this descriptor.
instr_iterator erase_instr(MachineInstr *I)
Remove an instruction from the instruction list and delete it.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
Instructions::iterator instr_iterator
iterator_range< succ_iterator > successors()
LLVM_ABI bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
iterator_range< pred_iterator > predecessors()
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool needsWinCFI() const
True if this function uses SEH unwind opcodes and needs an unwind table entry.
Function & getFunction()
Return the LLVM function that this machine code represents.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
const MachineInstrBuilder & setMemRefs(ArrayRef< MachineMemOperand * > MMOs) const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
Representation of each machine instruction.
mop_range operands()
LLVM_ABI void eraseFromBundle()
Unlink 'this' from its basic block and delete it.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void setIsDead(bool Val=true)
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
void setIsKill(bool Val=true)
Register getReg() const
getReg - Returns the register number.
LLVM_ABI void setIsDef(bool Val=true)
Change a def to a use, or a use to a def.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition SmallSet.h:176
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
static const unsigned CommuteAnyOperandIndex
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ Entry
Definition COFF.h:862
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
Definition SFrame.h:77
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
@ Offset
Definition DWP.cpp:577
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr RegState getKillRegState(bool B)
static bool isARMLowRegister(MCRegister Reg)
isARMLowRegister - Returns true if the register is a low register (r0-r7).
constexpr RegState getInternalReadRegState(bool B)
static std::array< MachineOperand, 2 > predOps(ARMCC::CondCodes Pred, unsigned PredReg=0)
Get the operands corresponding to the given Pred value.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
constexpr RegState getDefRegState(bool B)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
ARMCC::CondCodes getInstrPredicate(const MachineInstr &MI, Register &PredReg)
getInstrPredicate - If instruction is predicated, returns its predicate condition,...
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1933
static MachineOperand t1CondCodeOp(bool isDead=false)
Get the operand corresponding to the conditional code result for Thumb1.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
static MachineOperand condCodeOp(unsigned CCReg=0)
Get the operand corresponding to the conditional code result.
FunctionPass * createThumb2SizeReductionPass(std::function< bool(const Function &)> Ftor=nullptr)
createThumb2SizeReductionPass - Returns an instance of the Thumb2 size reduction pass.
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:879