Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 1 | //===-- 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 Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 10 | #include "ARM.h" |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 11 | #include "ARMBaseInstrInfo.h" |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 12 | #include "ARMSubtarget.h" |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 13 | #include "MCTargetDesc/ARMBaseInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "Thumb2InstrInfo.h" |
| 15 | #include "llvm/ADT/DenseMap.h" |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/PostOrderIterator.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallSet.h" |
| 19 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringRef.h" |
| 22 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 23 | #include "llvm/CodeGen/MachineFunction.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineInstr.h" |
| 26 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineOperand.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/TargetInstrInfo.h" |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DebugLoc.h" |
| 30 | #include "llvm/IR/Function.h" |
| 31 | #include "llvm/MC/MCInstrDesc.h" |
| 32 | #include "llvm/MC/MCRegisterInfo.h" |
Evan Cheng | f16a1d5 | 2009-08-10 07:20:37 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Compiler.h" |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ErrorHandling.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 38 | #include <algorithm> |
| 39 | #include <cassert> |
| 40 | #include <cstdint> |
| 41 | #include <functional> |
| 42 | #include <iterator> |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 43 | #include <utility> |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 44 | |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 47 | #define DEBUG_TYPE "t2-reduce-size" |
David Green | 110844d | 2017-12-19 12:19:08 +0000 | [diff] [blame] | 48 | #define THUMB2_SIZE_REDUCE_NAME "Thumb2 instruction size reduce pass" |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 49 | |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 50 | STATISTIC(NumNarrows, "Number of 32-bit instrs reduced to 16-bit ones"); |
| 51 | STATISTIC(Num2Addrs, "Number of 32-bit instrs reduced to 2addr 16-bit ones"); |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 52 | STATISTIC(NumLdSts, "Number of 32-bit load / store reduced to 16-bit ones"); |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 53 | |
Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 54 | static cl::opt<int> ReduceLimit("t2-reduce-limit", |
| 55 | cl::init(-1), cl::Hidden); |
| 56 | static cl::opt<int> ReduceLimit2Addr("t2-reduce-limit2", |
| 57 | cl::init(-1), cl::Hidden); |
| 58 | static cl::opt<int> ReduceLimitLdSt("t2-reduce-limit3", |
| 59 | cl::init(-1), cl::Hidden); |
Evan Cheng | f16a1d5 | 2009-08-10 07:20:37 +0000 | [diff] [blame] | 60 | |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 61 | namespace { |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 62 | |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 63 | /// ReduceTable - A static table with information on mapping from wide |
| 64 | /// opcodes to narrow |
| 65 | struct ReduceEntry { |
Craig Topper | ca658c2 | 2012-03-11 07:16:55 +0000 | [diff] [blame] | 66 | uint16_t WideOpc; // Wide opcode |
| 67 | uint16_t NarrowOpc1; // Narrow opcode to transform to |
| 68 | uint16_t NarrowOpc2; // Narrow opcode when it's two-address |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 69 | uint8_t Imm1Limit; // Limit of immediate field (bits) |
| 70 | uint8_t Imm2Limit; // Limit of immediate field when it's two-address |
| 71 | unsigned LowRegs1 : 1; // Only possible if low-registers are used |
| 72 | unsigned LowRegs2 : 1; // Only possible if low-registers are used (2addr) |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 73 | unsigned PredCC1 : 2; // 0 - If predicated, cc is on and vice versa. |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 74 | // 1 - No cc field. |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 75 | // 2 - Always set CPSR. |
Evan Cheng | aee7e49 | 2009-08-12 18:35:50 +0000 | [diff] [blame] | 76 | unsigned PredCC2 : 2; |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 77 | unsigned PartFlag : 1; // 16-bit instruction does partial flag update |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 78 | unsigned Special : 1; // Needs to be dealt with specially |
Evan Cheng | ddc0cb6 | 2012-12-20 19:59:30 +0000 | [diff] [blame] | 79 | unsigned AvoidMovs: 1; // Avoid movs with shifter operand (for Swift) |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | static const ReduceEntry ReduceTable[] = { |
Evan Cheng | ddc0cb6 | 2012-12-20 19:59:30 +0000 | [diff] [blame] | 83 | // Wide, Narrow1, Narrow2, imm1,imm2, lo1, lo2, P/C,PF,S,AM |
| 84 | { ARM::t2ADCrr, 0, ARM::tADC, 0, 0, 0, 1, 0,0, 0,0,0 }, |
| 85 | { ARM::t2ADDri, ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 0,0, 0,1,0 }, |
| 86 | { ARM::t2ADDrr, ARM::tADDrr, ARM::tADDhirr, 0, 0, 1, 0, 0,1, 0,0,0 }, |
| 87 | { ARM::t2ADDSri,ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 2,2, 0,1,0 }, |
| 88 | { ARM::t2ADDSrr,ARM::tADDrr, 0, 0, 0, 1, 0, 2,0, 0,1,0 }, |
| 89 | { ARM::t2ANDrr, 0, ARM::tAND, 0, 0, 0, 1, 0,0, 1,0,0 }, |
| 90 | { ARM::t2ASRri, ARM::tASRri, 0, 5, 0, 1, 0, 0,0, 1,0,1 }, |
| 91 | { ARM::t2ASRrr, 0, ARM::tASRrr, 0, 0, 0, 1, 0,0, 1,0,1 }, |
| 92 | { ARM::t2BICrr, 0, ARM::tBIC, 0, 0, 0, 1, 0,0, 1,0,0 }, |
| 93 | //FIXME: Disable CMN, as CCodes are backwards from compare expectations |
| 94 | //{ ARM::t2CMNrr, ARM::tCMN, 0, 0, 0, 1, 0, 2,0, 0,0,0 }, |
| 95 | { ARM::t2CMNzrr, ARM::tCMNz, 0, 0, 0, 1, 0, 2,0, 0,0,0 }, |
| 96 | { ARM::t2CMPri, ARM::tCMPi8, 0, 8, 0, 1, 0, 2,0, 0,0,0 }, |
| 97 | { ARM::t2CMPrr, ARM::tCMPhir, 0, 0, 0, 0, 0, 2,0, 0,1,0 }, |
| 98 | { ARM::t2EORrr, 0, ARM::tEOR, 0, 0, 0, 1, 0,0, 1,0,0 }, |
| 99 | // FIXME: adr.n immediate offset must be multiple of 4. |
| 100 | //{ ARM::t2LEApcrelJT,ARM::tLEApcrelJT, 0, 0, 0, 1, 0, 1,0, 0,0,0 }, |
| 101 | { ARM::t2LSLri, ARM::tLSLri, 0, 5, 0, 1, 0, 0,0, 1,0,1 }, |
| 102 | { ARM::t2LSLrr, 0, ARM::tLSLrr, 0, 0, 0, 1, 0,0, 1,0,1 }, |
| 103 | { ARM::t2LSRri, ARM::tLSRri, 0, 5, 0, 1, 0, 0,0, 1,0,1 }, |
| 104 | { ARM::t2LSRrr, 0, ARM::tLSRrr, 0, 0, 0, 1, 0,0, 1,0,1 }, |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 105 | { ARM::t2MOVi, ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 1,0,0 }, |
| 106 | { ARM::t2MOVi16,ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 1,1,0 }, |
Evan Cheng | ddc0cb6 | 2012-12-20 19:59:30 +0000 | [diff] [blame] | 107 | // FIXME: Do we need the 16-bit 'S' variant? |
| 108 | { ARM::t2MOVr,ARM::tMOVr, 0, 0, 0, 0, 0, 1,0, 0,0,0 }, |
| 109 | { ARM::t2MUL, 0, ARM::tMUL, 0, 0, 0, 1, 0,0, 1,0,0 }, |
| 110 | { ARM::t2MVNr, ARM::tMVN, 0, 0, 0, 1, 0, 0,0, 0,0,0 }, |
| 111 | { ARM::t2ORRrr, 0, ARM::tORR, 0, 0, 0, 1, 0,0, 1,0,0 }, |
| 112 | { ARM::t2REV, ARM::tREV, 0, 0, 0, 1, 0, 1,0, 0,0,0 }, |
| 113 | { ARM::t2REV16, ARM::tREV16, 0, 0, 0, 1, 0, 1,0, 0,0,0 }, |
| 114 | { ARM::t2REVSH, ARM::tREVSH, 0, 0, 0, 1, 0, 1,0, 0,0,0 }, |
| 115 | { ARM::t2RORrr, 0, ARM::tROR, 0, 0, 0, 1, 0,0, 1,0,0 }, |
| 116 | { ARM::t2RSBri, ARM::tRSB, 0, 0, 0, 1, 0, 0,0, 0,1,0 }, |
| 117 | { ARM::t2RSBSri,ARM::tRSB, 0, 0, 0, 1, 0, 2,0, 0,1,0 }, |
| 118 | { ARM::t2SBCrr, 0, ARM::tSBC, 0, 0, 0, 1, 0,0, 0,0,0 }, |
| 119 | { ARM::t2SUBri, ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 0,0, 0,0,0 }, |
| 120 | { ARM::t2SUBrr, ARM::tSUBrr, 0, 0, 0, 1, 0, 0,0, 0,0,0 }, |
| 121 | { ARM::t2SUBSri,ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 2,2, 0,0,0 }, |
| 122 | { ARM::t2SUBSrr,ARM::tSUBrr, 0, 0, 0, 1, 0, 2,0, 0,0,0 }, |
| 123 | { ARM::t2SXTB, ARM::tSXTB, 0, 0, 0, 1, 0, 1,0, 0,1,0 }, |
| 124 | { ARM::t2SXTH, ARM::tSXTH, 0, 0, 0, 1, 0, 1,0, 0,1,0 }, |
| 125 | { ARM::t2TSTrr, ARM::tTST, 0, 0, 0, 1, 0, 2,0, 0,0,0 }, |
| 126 | { ARM::t2UXTB, ARM::tUXTB, 0, 0, 0, 1, 0, 1,0, 0,1,0 }, |
| 127 | { ARM::t2UXTH, ARM::tUXTH, 0, 0, 0, 1, 0, 1,0, 0,1,0 }, |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 128 | |
Evan Cheng | ddc0cb6 | 2012-12-20 19:59:30 +0000 | [diff] [blame] | 129 | // FIXME: Clean this up after splitting each Thumb load / store opcode |
| 130 | // into multiple ones. |
| 131 | { ARM::t2LDRi12,ARM::tLDRi, ARM::tLDRspi, 5, 8, 1, 0, 0,0, 0,1,0 }, |
| 132 | { ARM::t2LDRs, ARM::tLDRr, 0, 0, 0, 1, 0, 0,0, 0,1,0 }, |
| 133 | { ARM::t2LDRBi12,ARM::tLDRBi, 0, 5, 0, 1, 0, 0,0, 0,1,0 }, |
| 134 | { ARM::t2LDRBs, ARM::tLDRBr, 0, 0, 0, 1, 0, 0,0, 0,1,0 }, |
| 135 | { ARM::t2LDRHi12,ARM::tLDRHi, 0, 5, 0, 1, 0, 0,0, 0,1,0 }, |
| 136 | { ARM::t2LDRHs, ARM::tLDRHr, 0, 0, 0, 1, 0, 0,0, 0,1,0 }, |
| 137 | { ARM::t2LDRSBs,ARM::tLDRSB, 0, 0, 0, 1, 0, 0,0, 0,1,0 }, |
| 138 | { ARM::t2LDRSHs,ARM::tLDRSH, 0, 0, 0, 1, 0, 0,0, 0,1,0 }, |
James Molloy | 53298a1 | 2016-06-07 12:13:34 +0000 | [diff] [blame] | 139 | { ARM::t2LDR_POST,ARM::tLDMIA_UPD,0, 0, 0, 1, 0, 0,0, 0,1,0 }, |
Evan Cheng | ddc0cb6 | 2012-12-20 19:59:30 +0000 | [diff] [blame] | 140 | { ARM::t2STRi12,ARM::tSTRi, ARM::tSTRspi, 5, 8, 1, 0, 0,0, 0,1,0 }, |
| 141 | { ARM::t2STRs, ARM::tSTRr, 0, 0, 0, 1, 0, 0,0, 0,1,0 }, |
| 142 | { ARM::t2STRBi12,ARM::tSTRBi, 0, 5, 0, 1, 0, 0,0, 0,1,0 }, |
| 143 | { ARM::t2STRBs, ARM::tSTRBr, 0, 0, 0, 1, 0, 0,0, 0,1,0 }, |
| 144 | { ARM::t2STRHi12,ARM::tSTRHi, 0, 5, 0, 1, 0, 0,0, 0,1,0 }, |
| 145 | { ARM::t2STRHs, ARM::tSTRHr, 0, 0, 0, 1, 0, 0,0, 0,1,0 }, |
James Molloy | 53298a1 | 2016-06-07 12:13:34 +0000 | [diff] [blame] | 146 | { ARM::t2STR_POST,ARM::tSTMIA_UPD,0, 0, 0, 1, 0, 0,0, 0,1,0 }, |
Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 147 | |
Evan Cheng | ddc0cb6 | 2012-12-20 19:59:30 +0000 | [diff] [blame] | 148 | { ARM::t2LDMIA, ARM::tLDMIA, 0, 0, 0, 1, 1, 1,1, 0,1,0 }, |
| 149 | { ARM::t2LDMIA_RET,0, ARM::tPOP_RET, 0, 0, 1, 1, 1,1, 0,1,0 }, |
| 150 | { ARM::t2LDMIA_UPD,ARM::tLDMIA_UPD,ARM::tPOP,0, 0, 1, 1, 1,1, 0,1,0 }, |
Scott Douglass | 953f908 | 2015-10-05 14:49:54 +0000 | [diff] [blame] | 151 | // ARM::t2STMIA (with no basereg writeback) has no Thumb1 equivalent. |
| 152 | // tSTMIA_UPD is a change in semantics which can only be used if the base |
| 153 | // register is killed. This difference is correctly handled elsewhere. |
| 154 | { ARM::t2STMIA, ARM::tSTMIA_UPD, 0, 0, 0, 1, 1, 1,1, 0,1,0 }, |
Evan Cheng | ddc0cb6 | 2012-12-20 19:59:30 +0000 | [diff] [blame] | 155 | { ARM::t2STMIA_UPD,ARM::tSTMIA_UPD, 0, 0, 0, 1, 1, 1,1, 0,1,0 }, |
| 156 | { ARM::t2STMDB_UPD, 0, ARM::tPUSH, 0, 0, 1, 1, 1,1, 0,1,0 } |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 159 | class Thumb2SizeReduce : public MachineFunctionPass { |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 160 | public: |
| 161 | static char ID; |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 162 | |
Evan Cheng | 6ddd7bc | 2009-08-15 07:59:10 +0000 | [diff] [blame] | 163 | const Thumb2InstrInfo *TII; |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 164 | const ARMSubtarget *STI; |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 165 | |
David Green | 110844d | 2017-12-19 12:19:08 +0000 | [diff] [blame] | 166 | Thumb2SizeReduce(std::function<bool(const Function &)> Ftor = nullptr); |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 167 | |
Craig Topper | 6bc27bf | 2014-03-10 02:09:33 +0000 | [diff] [blame] | 168 | bool runOnMachineFunction(MachineFunction &MF) override; |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 169 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 170 | MachineFunctionProperties getRequiredProperties() const override { |
| 171 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 172 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 175 | StringRef getPassName() const override { |
David Green | 110844d | 2017-12-19 12:19:08 +0000 | [diff] [blame] | 176 | return THUMB2_SIZE_REDUCE_NAME; |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | private: |
| 180 | /// ReduceOpcodeMap - Maps wide opcode to index of entry in ReduceTable. |
| 181 | DenseMap<unsigned, unsigned> ReduceOpcodeMap; |
| 182 | |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 183 | bool canAddPseudoFlagDep(MachineInstr *Use, bool IsSelfLoop); |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 184 | |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 185 | bool VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry, |
| 186 | bool is2Addr, ARMCC::CondCodes Pred, |
| 187 | bool LiveCPSR, bool &HasCC, bool &CCDead); |
| 188 | |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 189 | bool ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI, |
| 190 | const ReduceEntry &Entry); |
| 191 | |
| 192 | bool ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI, |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 193 | const ReduceEntry &Entry, bool LiveCPSR, bool IsSelfLoop); |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 194 | |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 195 | /// ReduceTo2Addr - Reduce a 32-bit instruction to a 16-bit two-address |
| 196 | /// instruction. |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 197 | bool ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI, |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 198 | const ReduceEntry &Entry, bool LiveCPSR, |
Evan Cheng | f4807a1 | 2011-10-27 21:21:05 +0000 | [diff] [blame] | 199 | bool IsSelfLoop); |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 200 | |
| 201 | /// ReduceToNarrow - Reduce a 32-bit instruction to a 16-bit |
| 202 | /// non-two-address instruction. |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 203 | bool ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI, |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 204 | const ReduceEntry &Entry, bool LiveCPSR, |
Evan Cheng | f4807a1 | 2011-10-27 21:21:05 +0000 | [diff] [blame] | 205 | bool IsSelfLoop); |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 206 | |
Jakob Stoklund Olesen | 43b1e13 | 2012-12-18 00:13:11 +0000 | [diff] [blame] | 207 | /// ReduceMI - Attempt to reduce MI, return true on success. |
| 208 | bool ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI, |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 209 | bool LiveCPSR, bool IsSelfLoop); |
Jakob Stoklund Olesen | 43b1e13 | 2012-12-18 00:13:11 +0000 | [diff] [blame] | 210 | |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 211 | /// ReduceMBB - Reduce width of instructions in the specified basic block. |
| 212 | bool ReduceMBB(MachineBasicBlock &MBB); |
Quentin Colombet | 23b404d | 2012-12-18 22:47:16 +0000 | [diff] [blame] | 213 | |
Evan Cheng | ddc0cb6 | 2012-12-20 19:59:30 +0000 | [diff] [blame] | 214 | bool OptimizeSize; |
Quentin Colombet | 23b404d | 2012-12-18 22:47:16 +0000 | [diff] [blame] | 215 | bool MinimizeSize; |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 216 | |
| 217 | // Last instruction to define CPSR in the current block. |
| 218 | MachineInstr *CPSRDef; |
| 219 | // Was CPSR last defined by a high latency instruction? |
| 220 | // When CPSRDef is null, this refers to CPSR defs in predecessors. |
| 221 | bool HighLatencyCPSR; |
| 222 | |
| 223 | struct MBBInfo { |
| 224 | // The flags leaving this block have high latency. |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 225 | bool HighLatencyCPSR = false; |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 226 | // Has this block been visited yet? |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 227 | bool Visited = false; |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 228 | |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 229 | MBBInfo() = default; |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | SmallVector<MBBInfo, 8> BlockInfo; |
Akira Hatanaka | 4a61619 | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 233 | |
| 234 | std::function<bool(const Function &)> PredicateFtor; |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 235 | }; |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 236 | |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 237 | char Thumb2SizeReduce::ID = 0; |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 238 | |
| 239 | } // end anonymous namespace |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 240 | |
David Green | 110844d | 2017-12-19 12:19:08 +0000 | [diff] [blame] | 241 | INITIALIZE_PASS(Thumb2SizeReduce, DEBUG_TYPE, THUMB2_SIZE_REDUCE_NAME, false, |
| 242 | false) |
| 243 | |
Akira Hatanaka | 4a61619 | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 244 | Thumb2SizeReduce::Thumb2SizeReduce(std::function<bool(const Function &)> Ftor) |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 245 | : MachineFunctionPass(ID), PredicateFtor(std::move(Ftor)) { |
Evan Cheng | ddc0cb6 | 2012-12-20 19:59:30 +0000 | [diff] [blame] | 246 | OptimizeSize = MinimizeSize = false; |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 247 | for (unsigned i = 0, e = array_lengthof(ReduceTable); i != e; ++i) { |
| 248 | unsigned FromOpc = ReduceTable[i].WideOpc; |
| 249 | if (!ReduceOpcodeMap.insert(std::make_pair(FromOpc, i)).second) |
Benjamin Kramer | 8ceb323 | 2015-10-25 22:28:27 +0000 | [diff] [blame] | 250 | llvm_unreachable("Duplicated entries?"); |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 254 | static bool HasImplicitCPSRDef(const MCInstrDesc &MCID) { |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 255 | for (const MCPhysReg *Regs = MCID.getImplicitDefs(); *Regs; ++Regs) |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 256 | if (*Regs == ARM::CPSR) |
| 257 | return true; |
| 258 | return false; |
| 259 | } |
| 260 | |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 261 | // Check for a likely high-latency flag def. |
| 262 | static bool isHighLatencyCPSR(MachineInstr *Def) { |
| 263 | switch(Def->getOpcode()) { |
| 264 | case ARM::FMSTAT: |
| 265 | case ARM::tMUL: |
| 266 | return true; |
| 267 | } |
| 268 | return false; |
| 269 | } |
| 270 | |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 271 | /// canAddPseudoFlagDep - For A9 (and other out-of-order) implementations, |
| 272 | /// the 's' 16-bit instruction partially update CPSR. Abort the |
| 273 | /// transformation to avoid adding false dependency on last CPSR setting |
| 274 | /// instruction which hurts the ability for out-of-order execution engine |
| 275 | /// to do register renaming magic. |
| 276 | /// This function checks if there is a read-of-write dependency between the |
| 277 | /// last instruction that defines the CPSR and the current instruction. If there |
| 278 | /// is, then there is no harm done since the instruction cannot be retired |
| 279 | /// before the CPSR setting instruction anyway. |
| 280 | /// Note, we are not doing full dependency analysis here for the sake of compile |
| 281 | /// time. We're not looking for cases like: |
| 282 | /// r0 = muls ... |
| 283 | /// r1 = add.w r0, ... |
| 284 | /// ... |
| 285 | /// = mul.w r1 |
| 286 | /// In this case it would have been ok to narrow the mul.w to muls since there |
| 287 | /// are indirect RAW dependency between the muls and the mul.w |
| 288 | bool |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 289 | Thumb2SizeReduce::canAddPseudoFlagDep(MachineInstr *Use, bool FirstInSelfLoop) { |
Quentin Colombet | 23b404d | 2012-12-18 22:47:16 +0000 | [diff] [blame] | 290 | // Disable the check for -Oz (aka OptimizeForSizeHarder). |
| 291 | if (MinimizeSize || !STI->avoidCPSRPartialUpdate()) |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 292 | return false; |
| 293 | |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 294 | if (!CPSRDef) |
Evan Cheng | f4807a1 | 2011-10-27 21:21:05 +0000 | [diff] [blame] | 295 | // If this BB loops back to itself, conservatively avoid narrowing the |
| 296 | // first instruction that does partial flag update. |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 297 | return HighLatencyCPSR || FirstInSelfLoop; |
Evan Cheng | f4807a1 | 2011-10-27 21:21:05 +0000 | [diff] [blame] | 298 | |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 299 | SmallSet<unsigned, 2> Defs; |
Owen Anderson | 8c1f17b | 2014-03-07 22:48:22 +0000 | [diff] [blame] | 300 | for (const MachineOperand &MO : CPSRDef->operands()) { |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 301 | if (!MO.isReg() || MO.isUndef() || MO.isUse()) |
| 302 | continue; |
| 303 | unsigned Reg = MO.getReg(); |
| 304 | if (Reg == 0 || Reg == ARM::CPSR) |
| 305 | continue; |
| 306 | Defs.insert(Reg); |
| 307 | } |
| 308 | |
Owen Anderson | 8c1f17b | 2014-03-07 22:48:22 +0000 | [diff] [blame] | 309 | for (const MachineOperand &MO : Use->operands()) { |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 310 | if (!MO.isReg() || MO.isUndef() || MO.isDef()) |
| 311 | continue; |
| 312 | unsigned Reg = MO.getReg(); |
| 313 | if (Defs.count(Reg)) |
| 314 | return false; |
| 315 | } |
| 316 | |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 317 | // If the current CPSR has high latency, try to avoid the false dependency. |
| 318 | if (HighLatencyCPSR) |
| 319 | return true; |
| 320 | |
| 321 | // tMOVi8 usually doesn't start long dependency chains, and there are a lot |
| 322 | // of them, so always shrink them when CPSR doesn't have high latency. |
| 323 | if (Use->getOpcode() == ARM::t2MOVi || |
| 324 | Use->getOpcode() == ARM::t2MOVi16) |
| 325 | return false; |
| 326 | |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 327 | // No read-after-write dependency. The narrowing will add false dependency. |
| 328 | return true; |
| 329 | } |
| 330 | |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 331 | bool |
| 332 | Thumb2SizeReduce::VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry, |
| 333 | bool is2Addr, ARMCC::CondCodes Pred, |
| 334 | bool LiveCPSR, bool &HasCC, bool &CCDead) { |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 335 | if ((is2Addr && Entry.PredCC2 == 0) || |
| 336 | (!is2Addr && Entry.PredCC1 == 0)) { |
| 337 | if (Pred == ARMCC::AL) { |
| 338 | // Not predicated, must set CPSR. |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 339 | if (!HasCC) { |
| 340 | // Original instruction was not setting CPSR, but CPSR is not |
| 341 | // currently live anyway. It's ok to set it. The CPSR def is |
| 342 | // dead though. |
| 343 | if (!LiveCPSR) { |
| 344 | HasCC = true; |
| 345 | CCDead = true; |
| 346 | return true; |
| 347 | } |
| 348 | return false; |
| 349 | } |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 350 | } else { |
| 351 | // Predicated, must not set CPSR. |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 352 | if (HasCC) |
| 353 | return false; |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 354 | } |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 355 | } else if ((is2Addr && Entry.PredCC2 == 2) || |
| 356 | (!is2Addr && Entry.PredCC1 == 2)) { |
| 357 | /// Old opcode has an optional def of CPSR. |
| 358 | if (HasCC) |
| 359 | return true; |
Jim Grosbach | bc7eeaf | 2010-09-14 20:35:46 +0000 | [diff] [blame] | 360 | // If old opcode does not implicitly define CPSR, then it's not ok since |
| 361 | // these new opcodes' CPSR def is not meant to be thrown away. e.g. CMP. |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 362 | if (!HasImplicitCPSRDef(MI->getDesc())) |
| 363 | return false; |
| 364 | HasCC = true; |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 365 | } else { |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 366 | // 16-bit instruction does not set CPSR. |
| 367 | if (HasCC) |
| 368 | return false; |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | return true; |
| 372 | } |
| 373 | |
Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 374 | static bool VerifyLowRegs(MachineInstr *MI) { |
| 375 | unsigned Opc = MI->getOpcode(); |
Peter Collingbourne | 85a0e23 | 2015-05-05 20:07:10 +0000 | [diff] [blame] | 376 | bool isPCOk = (Opc == ARM::t2LDMIA_RET || Opc == ARM::t2LDMIA_UPD); |
Tim Northover | ba1d704 | 2014-09-10 12:53:28 +0000 | [diff] [blame] | 377 | bool isLROk = (Opc == ARM::t2STMDB_UPD); |
Jim Grosbach | a8a8067 | 2011-06-29 23:25:04 +0000 | [diff] [blame] | 378 | bool isSPOk = isPCOk || isLROk; |
Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 379 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 380 | const MachineOperand &MO = MI->getOperand(i); |
| 381 | if (!MO.isReg() || MO.isImplicit()) |
| 382 | continue; |
| 383 | unsigned Reg = MO.getReg(); |
| 384 | if (Reg == 0 || Reg == ARM::CPSR) |
| 385 | continue; |
| 386 | if (isPCOk && Reg == ARM::PC) |
| 387 | continue; |
| 388 | if (isLROk && Reg == ARM::LR) |
| 389 | continue; |
Evan Cheng | 2a6c92f | 2009-11-19 06:32:27 +0000 | [diff] [blame] | 390 | if (Reg == ARM::SP) { |
| 391 | if (isSPOk) |
| 392 | continue; |
| 393 | if (i == 1 && (Opc == ARM::t2LDRi12 || Opc == ARM::t2STRi12)) |
| 394 | // Special case for these ldr / str with sp as base register. |
| 395 | continue; |
| 396 | } |
Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 397 | if (!isARMLowRegister(Reg)) |
| 398 | return false; |
| 399 | } |
| 400 | return true; |
| 401 | } |
| 402 | |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 403 | bool |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 404 | Thumb2SizeReduce::ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI, |
| 405 | const ReduceEntry &Entry) { |
Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 406 | if (ReduceLimitLdSt != -1 && ((int)NumLdSts >= ReduceLimitLdSt)) |
| 407 | return false; |
| 408 | |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 409 | unsigned Scale = 1; |
| 410 | bool HasImmOffset = false; |
| 411 | bool HasShift = false; |
Evan Cheng | 2a6c92f | 2009-11-19 06:32:27 +0000 | [diff] [blame] | 412 | bool HasOffReg = true; |
Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 413 | bool isLdStMul = false; |
Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 414 | unsigned Opc = Entry.NarrowOpc1; |
| 415 | unsigned OpNum = 3; // First 'rest' of operands. |
Evan Cheng | 2a6c92f | 2009-11-19 06:32:27 +0000 | [diff] [blame] | 416 | uint8_t ImmLimit = Entry.Imm1Limit; |
Bill Wendling | a68e3a5 | 2010-11-16 01:16:36 +0000 | [diff] [blame] | 417 | |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 418 | switch (Entry.WideOpc) { |
| 419 | default: |
| 420 | llvm_unreachable("Unexpected Thumb2 load / store opcode!"); |
Jakob Stoklund Olesen | b3de7b1 | 2012-08-28 03:11:27 +0000 | [diff] [blame] | 421 | case ARM::t2LDRi12: |
Bill Wendling | 092a7bd | 2010-12-14 03:36:38 +0000 | [diff] [blame] | 422 | case ARM::t2STRi12: |
| 423 | if (MI->getOperand(1).getReg() == ARM::SP) { |
Evan Cheng | 2a6c92f | 2009-11-19 06:32:27 +0000 | [diff] [blame] | 424 | Opc = Entry.NarrowOpc2; |
| 425 | ImmLimit = Entry.Imm2Limit; |
Evan Cheng | 2a6c92f | 2009-11-19 06:32:27 +0000 | [diff] [blame] | 426 | } |
Bill Wendling | 092a7bd | 2010-12-14 03:36:38 +0000 | [diff] [blame] | 427 | |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 428 | Scale = 4; |
Owen Anderson | 4ebf471 | 2011-02-08 22:39:40 +0000 | [diff] [blame] | 429 | HasImmOffset = true; |
| 430 | HasOffReg = false; |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 431 | break; |
Jakob Stoklund Olesen | b3de7b1 | 2012-08-28 03:11:27 +0000 | [diff] [blame] | 432 | case ARM::t2LDRBi12: |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 433 | case ARM::t2STRBi12: |
Owen Anderson | 4ebf471 | 2011-02-08 22:39:40 +0000 | [diff] [blame] | 434 | HasImmOffset = true; |
| 435 | HasOffReg = false; |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 436 | break; |
| 437 | case ARM::t2LDRHi12: |
| 438 | case ARM::t2STRHi12: |
| 439 | Scale = 2; |
Owen Anderson | 4ebf471 | 2011-02-08 22:39:40 +0000 | [diff] [blame] | 440 | HasImmOffset = true; |
| 441 | HasOffReg = false; |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 442 | break; |
Jakob Stoklund Olesen | b3de7b1 | 2012-08-28 03:11:27 +0000 | [diff] [blame] | 443 | case ARM::t2LDRs: |
| 444 | case ARM::t2LDRBs: |
| 445 | case ARM::t2LDRHs: |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 446 | case ARM::t2LDRSBs: |
| 447 | case ARM::t2LDRSHs: |
| 448 | case ARM::t2STRs: |
| 449 | case ARM::t2STRBs: |
| 450 | case ARM::t2STRHs: |
| 451 | HasShift = true; |
Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 452 | OpNum = 4; |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 453 | break; |
James Molloy | 53298a1 | 2016-06-07 12:13:34 +0000 | [diff] [blame] | 454 | case ARM::t2LDR_POST: |
| 455 | case ARM::t2STR_POST: { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 456 | if (!MBB.getParent()->getFunction().optForMinSize()) |
James Molloy | 53298a1 | 2016-06-07 12:13:34 +0000 | [diff] [blame] | 457 | return false; |
| 458 | |
Tim Northover | eaee28b | 2016-09-19 09:11:09 +0000 | [diff] [blame] | 459 | if (!MI->hasOneMemOperand() || |
| 460 | (*MI->memoperands_begin())->getAlignment() < 4) |
| 461 | return false; |
| 462 | |
James Molloy | 53298a1 | 2016-06-07 12:13:34 +0000 | [diff] [blame] | 463 | // We're creating a completely different type of load/store - LDM from LDR. |
| 464 | // For this reason we can't reuse the logic at the end of this function; we |
| 465 | // have to implement the MI building here. |
| 466 | bool IsStore = Entry.WideOpc == ARM::t2STR_POST; |
| 467 | unsigned Rt = MI->getOperand(IsStore ? 1 : 0).getReg(); |
| 468 | unsigned Rn = MI->getOperand(IsStore ? 0 : 1).getReg(); |
| 469 | unsigned Offset = MI->getOperand(3).getImm(); |
| 470 | unsigned PredImm = MI->getOperand(4).getImm(); |
| 471 | unsigned PredReg = MI->getOperand(5).getReg(); |
| 472 | assert(isARMLowRegister(Rt)); |
| 473 | assert(isARMLowRegister(Rn)); |
| 474 | |
| 475 | if (Offset != 4) |
| 476 | return false; |
| 477 | |
| 478 | // Add the 16-bit load / store instruction. |
| 479 | DebugLoc dl = MI->getDebugLoc(); |
| 480 | auto MIB = BuildMI(MBB, MI, dl, TII->get(Entry.NarrowOpc1)) |
| 481 | .addReg(Rn, RegState::Define) |
| 482 | .addReg(Rn) |
| 483 | .addImm(PredImm) |
| 484 | .addReg(PredReg) |
| 485 | .addReg(Rt, IsStore ? 0 : RegState::Define); |
| 486 | |
| 487 | // Transfer memoperands. |
| 488 | MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end()); |
| 489 | |
| 490 | // Transfer MI flags. |
| 491 | MIB.setMIFlags(MI->getFlags()); |
| 492 | |
| 493 | // Kill the old instruction. |
Tim Northover | 28a9e7f | 2016-06-17 18:40:46 +0000 | [diff] [blame] | 494 | MI->eraseFromBundle(); |
James Molloy | 53298a1 | 2016-06-07 12:13:34 +0000 | [diff] [blame] | 495 | ++NumLdSts; |
| 496 | return true; |
| 497 | } |
Peter Collingbourne | 85a0e23 | 2015-05-05 20:07:10 +0000 | [diff] [blame] | 498 | case ARM::t2LDMIA: { |
Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 499 | unsigned BaseReg = MI->getOperand(0).getReg(); |
Peter Collingbourne | 85a0e23 | 2015-05-05 20:07:10 +0000 | [diff] [blame] | 500 | assert(isARMLowRegister(BaseReg)); |
Bill Wendling | 092a7bd | 2010-12-14 03:36:38 +0000 | [diff] [blame] | 501 | |
Jim Grosbach | 88628e9 | 2010-09-07 22:30:53 +0000 | [diff] [blame] | 502 | // For the non-writeback version (this one), the base register must be |
| 503 | // one of the registers being loaded. |
| 504 | bool isOK = false; |
Peter Collingbourne | 85a0e23 | 2015-05-05 20:07:10 +0000 | [diff] [blame] | 505 | for (unsigned i = 3; i < MI->getNumOperands(); ++i) { |
Jim Grosbach | 88628e9 | 2010-09-07 22:30:53 +0000 | [diff] [blame] | 506 | if (MI->getOperand(i).getReg() == BaseReg) { |
| 507 | isOK = true; |
| 508 | break; |
| 509 | } |
| 510 | } |
Bill Wendling | 092a7bd | 2010-12-14 03:36:38 +0000 | [diff] [blame] | 511 | |
Jim Grosbach | 88628e9 | 2010-09-07 22:30:53 +0000 | [diff] [blame] | 512 | if (!isOK) |
| 513 | return false; |
| 514 | |
Bob Wilson | 947f04b | 2010-03-13 01:08:20 +0000 | [diff] [blame] | 515 | OpNum = 0; |
| 516 | isLdStMul = true; |
| 517 | break; |
| 518 | } |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 519 | case ARM::t2STMIA: |
Scott Douglass | 953f908 | 2015-10-05 14:49:54 +0000 | [diff] [blame] | 520 | // If the base register is killed, we don't care what its value is after the |
| 521 | // instruction, so we can use an updating STMIA. |
| 522 | if (!MI->getOperand(0).isKill()) |
| 523 | return false; |
| 524 | |
| 525 | break; |
Bill Wendling | a68e3a5 | 2010-11-16 01:16:36 +0000 | [diff] [blame] | 526 | case ARM::t2LDMIA_RET: { |
Bob Wilson | 947f04b | 2010-03-13 01:08:20 +0000 | [diff] [blame] | 527 | unsigned BaseReg = MI->getOperand(1).getReg(); |
| 528 | if (BaseReg != ARM::SP) |
| 529 | return false; |
| 530 | Opc = Entry.NarrowOpc2; // tPOP_RET |
Bill Wendling | a68e3a5 | 2010-11-16 01:16:36 +0000 | [diff] [blame] | 531 | OpNum = 2; |
Bob Wilson | 947f04b | 2010-03-13 01:08:20 +0000 | [diff] [blame] | 532 | isLdStMul = true; |
| 533 | break; |
| 534 | } |
Bill Wendling | a68e3a5 | 2010-11-16 01:16:36 +0000 | [diff] [blame] | 535 | case ARM::t2LDMIA_UPD: |
Bill Wendling | a68e3a5 | 2010-11-16 01:16:36 +0000 | [diff] [blame] | 536 | case ARM::t2STMIA_UPD: |
| 537 | case ARM::t2STMDB_UPD: { |
Bob Wilson | 947f04b | 2010-03-13 01:08:20 +0000 | [diff] [blame] | 538 | OpNum = 0; |
Bill Wendling | 092a7bd | 2010-12-14 03:36:38 +0000 | [diff] [blame] | 539 | |
Bob Wilson | 947f04b | 2010-03-13 01:08:20 +0000 | [diff] [blame] | 540 | unsigned BaseReg = MI->getOperand(1).getReg(); |
Bob Wilson | 947f04b | 2010-03-13 01:08:20 +0000 | [diff] [blame] | 541 | if (BaseReg == ARM::SP && |
Bill Wendling | a68e3a5 | 2010-11-16 01:16:36 +0000 | [diff] [blame] | 542 | (Entry.WideOpc == ARM::t2LDMIA_UPD || |
| 543 | Entry.WideOpc == ARM::t2STMDB_UPD)) { |
Bob Wilson | 947f04b | 2010-03-13 01:08:20 +0000 | [diff] [blame] | 544 | Opc = Entry.NarrowOpc2; // tPOP or tPUSH |
Bill Wendling | a68e3a5 | 2010-11-16 01:16:36 +0000 | [diff] [blame] | 545 | OpNum = 2; |
| 546 | } else if (!isARMLowRegister(BaseReg) || |
| 547 | (Entry.WideOpc != ARM::t2LDMIA_UPD && |
| 548 | Entry.WideOpc != ARM::t2STMIA_UPD)) { |
Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 549 | return false; |
| 550 | } |
Bill Wendling | 092a7bd | 2010-12-14 03:36:38 +0000 | [diff] [blame] | 551 | |
Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 552 | isLdStMul = true; |
| 553 | break; |
| 554 | } |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | unsigned OffsetReg = 0; |
| 558 | bool OffsetKill = false; |
Pete Cooper | f68d503 | 2015-05-01 18:57:32 +0000 | [diff] [blame] | 559 | bool OffsetInternal = false; |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 560 | if (HasShift) { |
| 561 | OffsetReg = MI->getOperand(2).getReg(); |
| 562 | OffsetKill = MI->getOperand(2).isKill(); |
Pete Cooper | f68d503 | 2015-05-01 18:57:32 +0000 | [diff] [blame] | 563 | OffsetInternal = MI->getOperand(2).isInternalRead(); |
Bill Wendling | 092a7bd | 2010-12-14 03:36:38 +0000 | [diff] [blame] | 564 | |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 565 | if (MI->getOperand(3).getImm()) |
| 566 | // Thumb1 addressing mode doesn't support shift. |
| 567 | return false; |
| 568 | } |
| 569 | |
| 570 | unsigned OffsetImm = 0; |
| 571 | if (HasImmOffset) { |
| 572 | OffsetImm = MI->getOperand(2).getImm(); |
Evan Cheng | 2a6c92f | 2009-11-19 06:32:27 +0000 | [diff] [blame] | 573 | unsigned MaxOffset = ((1 << ImmLimit) - 1) * Scale; |
Bill Wendling | 092a7bd | 2010-12-14 03:36:38 +0000 | [diff] [blame] | 574 | |
| 575 | if ((OffsetImm & (Scale - 1)) || OffsetImm > MaxOffset) |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 576 | // Make sure the immediate field fits. |
| 577 | return false; |
| 578 | } |
| 579 | |
| 580 | // Add the 16-bit load / store instruction. |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 581 | DebugLoc dl = MI->getDebugLoc(); |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 582 | MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, TII->get(Opc)); |
Scott Douglass | 953f908 | 2015-10-05 14:49:54 +0000 | [diff] [blame] | 583 | |
| 584 | // tSTMIA_UPD takes a defining register operand. We've already checked that |
| 585 | // the register is killed, so mark it as dead here. |
| 586 | if (Entry.WideOpc == ARM::t2STMIA) |
| 587 | MIB.addReg(MI->getOperand(0).getReg(), RegState::Define | RegState::Dead); |
| 588 | |
Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 589 | if (!isLdStMul) { |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 590 | MIB.add(MI->getOperand(0)); |
| 591 | MIB.add(MI->getOperand(1)); |
Bill Wendling | 092a7bd | 2010-12-14 03:36:38 +0000 | [diff] [blame] | 592 | |
| 593 | if (HasImmOffset) |
| 594 | MIB.addImm(OffsetImm / Scale); |
| 595 | |
Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 596 | assert((!HasShift || OffsetReg) && "Invalid so_reg load / store address!"); |
| 597 | |
Evan Cheng | 2a6c92f | 2009-11-19 06:32:27 +0000 | [diff] [blame] | 598 | if (HasOffReg) |
Pete Cooper | f68d503 | 2015-05-01 18:57:32 +0000 | [diff] [blame] | 599 | MIB.addReg(OffsetReg, getKillRegState(OffsetKill) | |
| 600 | getInternalReadRegState(OffsetInternal)); |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 601 | } |
Evan Cheng | 806845d | 2009-08-11 09:37:40 +0000 | [diff] [blame] | 602 | |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 603 | // Transfer the rest of operands. |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 604 | for (unsigned e = MI->getNumOperands(); OpNum != e; ++OpNum) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 605 | MIB.add(MI->getOperand(OpNum)); |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 606 | |
Evan Cheng | 2a6c92f | 2009-11-19 06:32:27 +0000 | [diff] [blame] | 607 | // Transfer memoperands. |
Chris Lattner | 1d0c257 | 2011-04-29 05:24:29 +0000 | [diff] [blame] | 608 | MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end()); |
Evan Cheng | 2a6c92f | 2009-11-19 06:32:27 +0000 | [diff] [blame] | 609 | |
Anton Korobeynikov | acca7ad | 2011-03-05 18:43:38 +0000 | [diff] [blame] | 610 | // Transfer MI flags. |
| 611 | MIB.setMIFlags(MI->getFlags()); |
| 612 | |
Chris Lattner | a6f074f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 613 | DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB); |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 614 | |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 615 | MBB.erase_instr(MI); |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 616 | ++NumLdSts; |
| 617 | return true; |
| 618 | } |
| 619 | |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 620 | bool |
| 621 | Thumb2SizeReduce::ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI, |
| 622 | const ReduceEntry &Entry, |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 623 | bool LiveCPSR, bool IsSelfLoop) { |
Jim Grosbach | a8a8067 | 2011-06-29 23:25:04 +0000 | [diff] [blame] | 624 | unsigned Opc = MI->getOpcode(); |
| 625 | if (Opc == ARM::t2ADDri) { |
| 626 | // If the source register is SP, try to reduce to tADDrSPi, otherwise |
| 627 | // it's a normal reduce. |
| 628 | if (MI->getOperand(1).getReg() != ARM::SP) { |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 629 | if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop)) |
Jim Grosbach | a8a8067 | 2011-06-29 23:25:04 +0000 | [diff] [blame] | 630 | return true; |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 631 | return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop); |
Jim Grosbach | a8a8067 | 2011-06-29 23:25:04 +0000 | [diff] [blame] | 632 | } |
| 633 | // Try to reduce to tADDrSPi. |
| 634 | unsigned Imm = MI->getOperand(2).getImm(); |
| 635 | // The immediate must be in range, the destination register must be a low |
Jim Grosbach | ed5134a | 2011-06-30 02:22:49 +0000 | [diff] [blame] | 636 | // reg, the predicate must be "always" and the condition flags must not |
| 637 | // be being set. |
Jim Grosbach | 68b0e84 | 2011-07-01 19:07:09 +0000 | [diff] [blame] | 638 | if (Imm & 3 || Imm > 1020) |
Jim Grosbach | a8a8067 | 2011-06-29 23:25:04 +0000 | [diff] [blame] | 639 | return false; |
| 640 | if (!isARMLowRegister(MI->getOperand(0).getReg())) |
| 641 | return false; |
Jim Grosbach | ed5134a | 2011-06-30 02:22:49 +0000 | [diff] [blame] | 642 | if (MI->getOperand(3).getImm() != ARMCC::AL) |
| 643 | return false; |
Jim Grosbach | a8a8067 | 2011-06-29 23:25:04 +0000 | [diff] [blame] | 644 | const MCInstrDesc &MCID = MI->getDesc(); |
| 645 | if (MCID.hasOptionalDef() && |
| 646 | MI->getOperand(MCID.getNumOperands()-1).getReg() == ARM::CPSR) |
| 647 | return false; |
| 648 | |
Diana Picus | 4f8c3e1 | 2017-01-13 09:37:56 +0000 | [diff] [blame] | 649 | MachineInstrBuilder MIB = |
| 650 | BuildMI(MBB, MI, MI->getDebugLoc(), |
| 651 | TII->get(ARM::tADDrSPi)) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 652 | .add(MI->getOperand(0)) |
| 653 | .add(MI->getOperand(1)) |
Diana Picus | 4f8c3e1 | 2017-01-13 09:37:56 +0000 | [diff] [blame] | 654 | .addImm(Imm / 4) // The tADDrSPi has an implied scale by four. |
| 655 | .add(predOps(ARMCC::AL)); |
Jim Grosbach | a8a8067 | 2011-06-29 23:25:04 +0000 | [diff] [blame] | 656 | |
| 657 | // Transfer MI flags. |
| 658 | MIB.setMIFlags(MI->getFlags()); |
| 659 | |
| 660 | DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " <<*MIB); |
| 661 | |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 662 | MBB.erase_instr(MI); |
Jim Grosbach | a8a8067 | 2011-06-29 23:25:04 +0000 | [diff] [blame] | 663 | ++NumNarrows; |
| 664 | return true; |
| 665 | } |
| 666 | |
Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 667 | if (Entry.LowRegs1 && !VerifyLowRegs(MI)) |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 668 | return false; |
| 669 | |
Chad Rosier | 6733630 | 2015-05-22 20:07:34 +0000 | [diff] [blame] | 670 | if (MI->mayLoadOrStore()) |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 671 | return ReduceLoadStore(MBB, MI, Entry); |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 672 | |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 673 | switch (Opc) { |
| 674 | default: break; |
Owen Anderson | 4ebf471 | 2011-02-08 22:39:40 +0000 | [diff] [blame] | 675 | case ARM::t2ADDSri: |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 676 | case ARM::t2ADDSrr: { |
| 677 | unsigned PredReg = 0; |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 678 | if (getInstrPredicate(*MI, PredReg) == ARMCC::AL) { |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 679 | switch (Opc) { |
| 680 | default: break; |
Eugene Zelenko | 342257e | 2017-01-31 00:56:17 +0000 | [diff] [blame] | 681 | case ARM::t2ADDSri: |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 682 | if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop)) |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 683 | return true; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 684 | LLVM_FALLTHROUGH; |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 685 | case ARM::t2ADDSrr: |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 686 | return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop); |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | break; |
| 690 | } |
| 691 | case ARM::t2RSBri: |
| 692 | case ARM::t2RSBSri: |
Jim Grosbach | 8b31ef5 | 2011-07-27 16:47:19 +0000 | [diff] [blame] | 693 | case ARM::t2SXTB: |
| 694 | case ARM::t2SXTH: |
| 695 | case ARM::t2UXTB: |
| 696 | case ARM::t2UXTH: |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 697 | if (MI->getOperand(2).getImm() == 0) |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 698 | return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop); |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 699 | break; |
Anton Korobeynikov | 2522908 | 2009-11-24 00:44:37 +0000 | [diff] [blame] | 700 | case ARM::t2MOVi16: |
| 701 | // Can convert only 'pure' immediate operands, not immediates obtained as |
| 702 | // globals' addresses. |
| 703 | if (MI->getOperand(1).isImm()) |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 704 | return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop); |
Anton Korobeynikov | 2522908 | 2009-11-24 00:44:37 +0000 | [diff] [blame] | 705 | break; |
Jim Grosbach | 327cf8e | 2010-12-07 20:41:06 +0000 | [diff] [blame] | 706 | case ARM::t2CMPrr: { |
Jim Grosbach | 5bae054 | 2010-12-03 23:54:18 +0000 | [diff] [blame] | 707 | // Try to reduce to the lo-reg only version first. Why there are two |
| 708 | // versions of the instruction is a mystery. |
| 709 | // It would be nice to just have two entries in the master table that |
| 710 | // are prioritized, but the table assumes a unique entry for each |
| 711 | // source insn opcode. So for now, we hack a local entry record to use. |
| 712 | static const ReduceEntry NarrowEntry = |
Evan Cheng | ddc0cb6 | 2012-12-20 19:59:30 +0000 | [diff] [blame] | 713 | { ARM::t2CMPrr,ARM::tCMPr, 0, 0, 0, 1, 1,2, 0, 0,1,0 }; |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 714 | if (ReduceToNarrow(MBB, MI, NarrowEntry, LiveCPSR, IsSelfLoop)) |
Jim Grosbach | 5bae054 | 2010-12-03 23:54:18 +0000 | [diff] [blame] | 715 | return true; |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 716 | return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop); |
Jim Grosbach | 5bae054 | 2010-12-03 23:54:18 +0000 | [diff] [blame] | 717 | } |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 718 | } |
Evan Cheng | 3606467 | 2009-08-11 08:52:18 +0000 | [diff] [blame] | 719 | return false; |
| 720 | } |
| 721 | |
| 722 | bool |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 723 | Thumb2SizeReduce::ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI, |
| 724 | const ReduceEntry &Entry, |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 725 | bool LiveCPSR, bool IsSelfLoop) { |
Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 726 | if (ReduceLimit2Addr != -1 && ((int)Num2Addrs >= ReduceLimit2Addr)) |
| 727 | return false; |
| 728 | |
Sanjay Patel | 924879a | 2015-08-04 15:49:57 +0000 | [diff] [blame] | 729 | if (!OptimizeSize && Entry.AvoidMovs && STI->avoidMOVsShifterOperand()) |
Evan Cheng | ddc0cb6 | 2012-12-20 19:59:30 +0000 | [diff] [blame] | 730 | // Don't issue movs with shifter operand for some CPUs unless we |
Sanjay Patel | 924879a | 2015-08-04 15:49:57 +0000 | [diff] [blame] | 731 | // are optimizing for size. |
Evan Cheng | ddc0cb6 | 2012-12-20 19:59:30 +0000 | [diff] [blame] | 732 | return false; |
| 733 | |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 734 | unsigned Reg0 = MI->getOperand(0).getReg(); |
| 735 | unsigned Reg1 = MI->getOperand(1).getReg(); |
Jim Grosbach | c01104d | 2012-02-24 00:33:36 +0000 | [diff] [blame] | 736 | // t2MUL is "special". The tied source operand is second, not first. |
| 737 | if (MI->getOpcode() == ARM::t2MUL) { |
Jim Grosbach | 3a21e2c | 2012-02-24 00:53:11 +0000 | [diff] [blame] | 738 | unsigned Reg2 = MI->getOperand(2).getReg(); |
| 739 | // Early exit if the regs aren't all low regs. |
| 740 | if (!isARMLowRegister(Reg0) || !isARMLowRegister(Reg1) |
| 741 | || !isARMLowRegister(Reg2)) |
| 742 | return false; |
| 743 | if (Reg0 != Reg2) { |
Jim Grosbach | c01104d | 2012-02-24 00:33:36 +0000 | [diff] [blame] | 744 | // If the other operand also isn't the same as the destination, we |
| 745 | // can't reduce. |
| 746 | if (Reg1 != Reg0) |
| 747 | return false; |
| 748 | // Try to commute the operands to make it a 2-address instruction. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 749 | MachineInstr *CommutedMI = TII->commuteInstruction(*MI); |
Jim Grosbach | c01104d | 2012-02-24 00:33:36 +0000 | [diff] [blame] | 750 | if (!CommutedMI) |
| 751 | return false; |
| 752 | } |
| 753 | } else if (Reg0 != Reg1) { |
Bob Wilson | 279e55f | 2010-06-24 16:50:20 +0000 | [diff] [blame] | 754 | // Try to commute the operands to make it a 2-address instruction. |
Andrew Kaylor | 16c4da0 | 2015-09-28 20:33:22 +0000 | [diff] [blame] | 755 | unsigned CommOpIdx1 = 1; |
| 756 | unsigned CommOpIdx2 = TargetInstrInfo::CommuteAnyOperandIndex; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 757 | if (!TII->findCommutedOpIndices(*MI, CommOpIdx1, CommOpIdx2) || |
Andrew Kaylor | 16c4da0 | 2015-09-28 20:33:22 +0000 | [diff] [blame] | 758 | MI->getOperand(CommOpIdx2).getReg() != Reg0) |
Bob Wilson | 279e55f | 2010-06-24 16:50:20 +0000 | [diff] [blame] | 759 | return false; |
Andrew Kaylor | 16c4da0 | 2015-09-28 20:33:22 +0000 | [diff] [blame] | 760 | MachineInstr *CommutedMI = |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 761 | TII->commuteInstruction(*MI, false, CommOpIdx1, CommOpIdx2); |
Bob Wilson | 279e55f | 2010-06-24 16:50:20 +0000 | [diff] [blame] | 762 | if (!CommutedMI) |
| 763 | return false; |
| 764 | } |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 765 | if (Entry.LowRegs2 && !isARMLowRegister(Reg0)) |
| 766 | return false; |
| 767 | if (Entry.Imm2Limit) { |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 768 | unsigned Imm = MI->getOperand(2).getImm(); |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 769 | unsigned Limit = (1 << Entry.Imm2Limit) - 1; |
| 770 | if (Imm > Limit) |
| 771 | return false; |
| 772 | } else { |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 773 | unsigned Reg2 = MI->getOperand(2).getReg(); |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 774 | if (Entry.LowRegs2 && !isARMLowRegister(Reg2)) |
| 775 | return false; |
| 776 | } |
| 777 | |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 778 | // Check if it's possible / necessary to transfer the predicate. |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 779 | const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc2); |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 780 | unsigned PredReg = 0; |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 781 | ARMCC::CondCodes Pred = getInstrPredicate(*MI, PredReg); |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 782 | bool SkipPred = false; |
| 783 | if (Pred != ARMCC::AL) { |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 784 | if (!NewMCID.isPredicable()) |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 785 | // Can't transfer predicate, fail. |
| 786 | return false; |
| 787 | } else { |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 788 | SkipPred = !NewMCID.isPredicable(); |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 791 | bool HasCC = false; |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 792 | bool CCDead = false; |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 793 | const MCInstrDesc &MCID = MI->getDesc(); |
| 794 | if (MCID.hasOptionalDef()) { |
| 795 | unsigned NumOps = MCID.getNumOperands(); |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 796 | HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR); |
| 797 | if (HasCC && MI->getOperand(NumOps-1).isDead()) |
| 798 | CCDead = true; |
| 799 | } |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 800 | if (!VerifyPredAndCC(MI, Entry, true, Pred, LiveCPSR, HasCC, CCDead)) |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 801 | return false; |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 802 | |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 803 | // Avoid adding a false dependency on partial flag update by some 16-bit |
| 804 | // instructions which has the 's' bit set. |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 805 | if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC && |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 806 | canAddPseudoFlagDep(MI, IsSelfLoop)) |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 807 | return false; |
| 808 | |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 809 | // Add the 16-bit instruction. |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 810 | DebugLoc dl = MI->getDebugLoc(); |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 811 | MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID); |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 812 | MIB.add(MI->getOperand(0)); |
Diana Picus | a2c5914 | 2017-01-13 10:37:37 +0000 | [diff] [blame] | 813 | if (NewMCID.hasOptionalDef()) |
| 814 | MIB.add(HasCC ? t1CondCodeOp(CCDead) : condCodeOp()); |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 815 | |
| 816 | // Transfer the rest of operands. |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 817 | unsigned NumOps = MCID.getNumOperands(); |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 818 | for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) { |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 819 | if (i < NumOps && MCID.OpInfo[i].isOptionalDef()) |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 820 | continue; |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 821 | if (SkipPred && MCID.OpInfo[i].isPredicate()) |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 822 | continue; |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 823 | MIB.add(MI->getOperand(i)); |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 824 | } |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 825 | |
Anton Korobeynikov | acca7ad | 2011-03-05 18:43:38 +0000 | [diff] [blame] | 826 | // Transfer MI flags. |
| 827 | MIB.setMIFlags(MI->getFlags()); |
| 828 | |
Chris Lattner | a6f074f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 829 | DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB); |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 830 | |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 831 | MBB.erase_instr(MI); |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 832 | ++Num2Addrs; |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 833 | return true; |
| 834 | } |
| 835 | |
| 836 | bool |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 837 | Thumb2SizeReduce::ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI, |
| 838 | const ReduceEntry &Entry, |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 839 | bool LiveCPSR, bool IsSelfLoop) { |
Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 840 | if (ReduceLimit != -1 && ((int)NumNarrows >= ReduceLimit)) |
| 841 | return false; |
| 842 | |
Sanjay Patel | 924879a | 2015-08-04 15:49:57 +0000 | [diff] [blame] | 843 | if (!OptimizeSize && Entry.AvoidMovs && STI->avoidMOVsShifterOperand()) |
Evan Cheng | ddc0cb6 | 2012-12-20 19:59:30 +0000 | [diff] [blame] | 844 | // Don't issue movs with shifter operand for some CPUs unless we |
Sanjay Patel | 924879a | 2015-08-04 15:49:57 +0000 | [diff] [blame] | 845 | // are optimizing for size. |
Evan Cheng | ddc0cb6 | 2012-12-20 19:59:30 +0000 | [diff] [blame] | 846 | return false; |
| 847 | |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 848 | unsigned Limit = ~0U; |
| 849 | if (Entry.Imm1Limit) |
Jim Grosbach | a8a8067 | 2011-06-29 23:25:04 +0000 | [diff] [blame] | 850 | Limit = (1 << Entry.Imm1Limit) - 1; |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 851 | |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 852 | const MCInstrDesc &MCID = MI->getDesc(); |
| 853 | for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i) { |
| 854 | if (MCID.OpInfo[i].isPredicate()) |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 855 | continue; |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 856 | const MachineOperand &MO = MI->getOperand(i); |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 857 | if (MO.isReg()) { |
| 858 | unsigned Reg = MO.getReg(); |
| 859 | if (!Reg || Reg == ARM::CPSR) |
| 860 | continue; |
| 861 | if (Entry.LowRegs1 && !isARMLowRegister(Reg)) |
| 862 | return false; |
Evan Cheng | f6a9d06 | 2009-08-11 23:00:31 +0000 | [diff] [blame] | 863 | } else if (MO.isImm() && |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 864 | !MCID.OpInfo[i].isPredicate()) { |
Jim Grosbach | a8a8067 | 2011-06-29 23:25:04 +0000 | [diff] [blame] | 865 | if (((unsigned)MO.getImm()) > Limit) |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 866 | return false; |
| 867 | } |
| 868 | } |
| 869 | |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 870 | // Check if it's possible / necessary to transfer the predicate. |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 871 | const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc1); |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 872 | unsigned PredReg = 0; |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 873 | ARMCC::CondCodes Pred = getInstrPredicate(*MI, PredReg); |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 874 | bool SkipPred = false; |
| 875 | if (Pred != ARMCC::AL) { |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 876 | if (!NewMCID.isPredicable()) |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 877 | // Can't transfer predicate, fail. |
| 878 | return false; |
| 879 | } else { |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 880 | SkipPred = !NewMCID.isPredicable(); |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 883 | bool HasCC = false; |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 884 | bool CCDead = false; |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 885 | if (MCID.hasOptionalDef()) { |
| 886 | unsigned NumOps = MCID.getNumOperands(); |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 887 | HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR); |
| 888 | if (HasCC && MI->getOperand(NumOps-1).isDead()) |
| 889 | CCDead = true; |
| 890 | } |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 891 | if (!VerifyPredAndCC(MI, Entry, false, Pred, LiveCPSR, HasCC, CCDead)) |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 892 | return false; |
| 893 | |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 894 | // Avoid adding a false dependency on partial flag update by some 16-bit |
| 895 | // instructions which has the 's' bit set. |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 896 | if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC && |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 897 | canAddPseudoFlagDep(MI, IsSelfLoop)) |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 898 | return false; |
| 899 | |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 900 | // Add the 16-bit instruction. |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 901 | DebugLoc dl = MI->getDebugLoc(); |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 902 | MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID); |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 903 | MIB.add(MI->getOperand(0)); |
Diana Picus | a2c5914 | 2017-01-13 10:37:37 +0000 | [diff] [blame] | 904 | if (NewMCID.hasOptionalDef()) |
| 905 | MIB.add(HasCC ? t1CondCodeOp(CCDead) : condCodeOp()); |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 906 | |
| 907 | // Transfer the rest of operands. |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 908 | unsigned NumOps = MCID.getNumOperands(); |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 909 | for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) { |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 910 | if (i < NumOps && MCID.OpInfo[i].isOptionalDef()) |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 911 | continue; |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 912 | if ((MCID.getOpcode() == ARM::t2RSBSri || |
Jim Grosbach | 8b31ef5 | 2011-07-27 16:47:19 +0000 | [diff] [blame] | 913 | MCID.getOpcode() == ARM::t2RSBri || |
| 914 | MCID.getOpcode() == ARM::t2SXTB || |
| 915 | MCID.getOpcode() == ARM::t2SXTH || |
| 916 | MCID.getOpcode() == ARM::t2UXTB || |
| 917 | MCID.getOpcode() == ARM::t2UXTH) && i == 2) |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 918 | // Skip the zero immediate operand, it's now implicit. |
| 919 | continue; |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 920 | bool isPred = (i < NumOps && MCID.OpInfo[i].isPredicate()); |
Evan Cheng | f6a9d06 | 2009-08-11 23:00:31 +0000 | [diff] [blame] | 921 | if (SkipPred && isPred) |
| 922 | continue; |
| 923 | const MachineOperand &MO = MI->getOperand(i); |
Jim Grosbach | a8a8067 | 2011-06-29 23:25:04 +0000 | [diff] [blame] | 924 | if (MO.isReg() && MO.isImplicit() && MO.getReg() == ARM::CPSR) |
| 925 | // Skip implicit def of CPSR. Either it's modeled as an optional |
| 926 | // def now or it's already an implicit def on the new instruction. |
| 927 | continue; |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 928 | MIB.add(MO); |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 929 | } |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 930 | if (!MCID.isPredicable() && NewMCID.isPredicable()) |
Diana Picus | 4f8c3e1 | 2017-01-13 09:37:56 +0000 | [diff] [blame] | 931 | MIB.add(predOps(ARMCC::AL)); |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 932 | |
Anton Korobeynikov | acca7ad | 2011-03-05 18:43:38 +0000 | [diff] [blame] | 933 | // Transfer MI flags. |
| 934 | MIB.setMIFlags(MI->getFlags()); |
| 935 | |
Chris Lattner | a6f074f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 936 | DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB); |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 937 | |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 938 | MBB.erase_instr(MI); |
Evan Cheng | d461c1c | 2009-08-09 19:17:19 +0000 | [diff] [blame] | 939 | ++NumNarrows; |
| 940 | return true; |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 943 | static bool UpdateCPSRDef(MachineInstr &MI, bool LiveCPSR, bool &DefCPSR) { |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 944 | bool HasDef = false; |
Owen Anderson | 8c1f17b | 2014-03-07 22:48:22 +0000 | [diff] [blame] | 945 | for (const MachineOperand &MO : MI.operands()) { |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 946 | if (!MO.isReg() || MO.isUndef() || MO.isUse()) |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 947 | continue; |
| 948 | if (MO.getReg() != ARM::CPSR) |
| 949 | continue; |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 950 | |
| 951 | DefCPSR = true; |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 952 | if (!MO.isDead()) |
| 953 | HasDef = true; |
| 954 | } |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 955 | |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 956 | return HasDef || LiveCPSR; |
| 957 | } |
| 958 | |
| 959 | static bool UpdateCPSRUse(MachineInstr &MI, bool LiveCPSR) { |
Owen Anderson | 8c1f17b | 2014-03-07 22:48:22 +0000 | [diff] [blame] | 960 | for (const MachineOperand &MO : MI.operands()) { |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 961 | if (!MO.isReg() || MO.isUndef() || MO.isDef()) |
| 962 | continue; |
| 963 | if (MO.getReg() != ARM::CPSR) |
| 964 | continue; |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 965 | assert(LiveCPSR && "CPSR liveness tracking is wrong!"); |
| 966 | if (MO.isKill()) { |
| 967 | LiveCPSR = false; |
| 968 | break; |
| 969 | } |
| 970 | } |
| 971 | |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 972 | return LiveCPSR; |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Jakob Stoklund Olesen | 43b1e13 | 2012-12-18 00:13:11 +0000 | [diff] [blame] | 975 | bool Thumb2SizeReduce::ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI, |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 976 | bool LiveCPSR, bool IsSelfLoop) { |
Jakob Stoklund Olesen | 43b1e13 | 2012-12-18 00:13:11 +0000 | [diff] [blame] | 977 | unsigned Opcode = MI->getOpcode(); |
| 978 | DenseMap<unsigned, unsigned>::iterator OPI = ReduceOpcodeMap.find(Opcode); |
| 979 | if (OPI == ReduceOpcodeMap.end()) |
| 980 | return false; |
| 981 | const ReduceEntry &Entry = ReduceTable[OPI->second]; |
| 982 | |
| 983 | // Don't attempt normal reductions on "special" cases for now. |
| 984 | if (Entry.Special) |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 985 | return ReduceSpecial(MBB, MI, Entry, LiveCPSR, IsSelfLoop); |
Jakob Stoklund Olesen | 43b1e13 | 2012-12-18 00:13:11 +0000 | [diff] [blame] | 986 | |
| 987 | // Try to transform to a 16-bit two-address instruction. |
| 988 | if (Entry.NarrowOpc2 && |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 989 | ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop)) |
Jakob Stoklund Olesen | 43b1e13 | 2012-12-18 00:13:11 +0000 | [diff] [blame] | 990 | return true; |
| 991 | |
| 992 | // Try to transform to a 16-bit non-two-address instruction. |
| 993 | if (Entry.NarrowOpc1 && |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 994 | ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop)) |
Jakob Stoklund Olesen | 43b1e13 | 2012-12-18 00:13:11 +0000 | [diff] [blame] | 995 | return true; |
| 996 | |
| 997 | return false; |
| 998 | } |
| 999 | |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 1000 | bool Thumb2SizeReduce::ReduceMBB(MachineBasicBlock &MBB) { |
| 1001 | bool Modified = false; |
| 1002 | |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 1003 | // Yes, CPSR could be livein. |
Dan Gohman | a1cf9fe | 2010-04-13 16:53:51 +0000 | [diff] [blame] | 1004 | bool LiveCPSR = MBB.isLiveIn(ARM::CPSR); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1005 | MachineInstr *BundleMI = nullptr; |
Evan Cheng | 1f5bee1 | 2009-08-10 06:57:42 +0000 | [diff] [blame] | 1006 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1007 | CPSRDef = nullptr; |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 1008 | HighLatencyCPSR = false; |
| 1009 | |
| 1010 | // Check predecessors for the latest CPSRDef. |
Jim Grosbach | 537f3ed | 2014-04-04 02:11:03 +0000 | [diff] [blame] | 1011 | for (auto *Pred : MBB.predecessors()) { |
| 1012 | const MBBInfo &PInfo = BlockInfo[Pred->getNumber()]; |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 1013 | if (!PInfo.Visited) { |
| 1014 | // Since blocks are visited in RPO, this must be a back-edge. |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 1015 | continue; |
| 1016 | } |
| 1017 | if (PInfo.HighLatencyCPSR) { |
| 1018 | HighLatencyCPSR = true; |
| 1019 | break; |
| 1020 | } |
| 1021 | } |
| 1022 | |
Evan Cheng | f4807a1 | 2011-10-27 21:21:05 +0000 | [diff] [blame] | 1023 | // If this BB loops back to itself, conservatively avoid narrowing the |
| 1024 | // first instruction that does partial flag update. |
| 1025 | bool IsSelfLoop = MBB.isSuccessor(&MBB); |
Jim Grosbach | 0c509fa | 2012-04-06 23:43:50 +0000 | [diff] [blame] | 1026 | MachineBasicBlock::instr_iterator MII = MBB.instr_begin(),E = MBB.instr_end(); |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 1027 | MachineBasicBlock::instr_iterator NextMII; |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 1028 | for (; MII != E; MII = NextMII) { |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1029 | NextMII = std::next(MII); |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 1030 | |
Evan Cheng | 51cbd2d | 2009-08-10 02:37:24 +0000 | [diff] [blame] | 1031 | MachineInstr *MI = &*MII; |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 1032 | if (MI->isBundle()) { |
| 1033 | BundleMI = MI; |
| 1034 | continue; |
| 1035 | } |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 1036 | if (MI->isDebugValue()) |
| 1037 | continue; |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 1038 | |
Evan Cheng | 1e6c2a1 | 2009-08-12 01:49:45 +0000 | [diff] [blame] | 1039 | LiveCPSR = UpdateCPSRUse(*MI, LiveCPSR); |
| 1040 | |
Jakob Stoklund Olesen | 41bbf9c | 2012-12-18 00:46:39 +0000 | [diff] [blame] | 1041 | // Does NextMII belong to the same bundle as MI? |
| 1042 | bool NextInSameBundle = NextMII != E && NextMII->isBundledWithPred(); |
| 1043 | |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 1044 | if (ReduceMI(MBB, MI, LiveCPSR, IsSelfLoop)) { |
Jakob Stoklund Olesen | 43b1e13 | 2012-12-18 00:13:11 +0000 | [diff] [blame] | 1045 | Modified = true; |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1046 | MachineBasicBlock::instr_iterator I = std::prev(NextMII); |
Jakob Stoklund Olesen | 43b1e13 | 2012-12-18 00:13:11 +0000 | [diff] [blame] | 1047 | MI = &*I; |
Jakob Stoklund Olesen | 41bbf9c | 2012-12-18 00:46:39 +0000 | [diff] [blame] | 1048 | // Removing and reinserting the first instruction in a bundle will break |
| 1049 | // up the bundle. Fix the bundling if it was broken. |
| 1050 | if (NextInSameBundle && !NextMII->isBundledWithPred()) |
| 1051 | NextMII->bundleWithPred(); |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Renato Golin | f6ed8bb | 2016-05-12 12:33:33 +0000 | [diff] [blame] | 1054 | if (BundleMI && !NextInSameBundle && MI->isInsideBundle()) { |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 1055 | // FIXME: Since post-ra scheduler operates on bundles, the CPSR kill |
| 1056 | // marker is only on the BUNDLE instruction. Process the BUNDLE |
| 1057 | // instruction as we finish with the bundled instruction to work around |
| 1058 | // the inconsistency. |
Evan Cheng | 903231b | 2011-12-17 01:25:34 +0000 | [diff] [blame] | 1059 | if (BundleMI->killsRegister(ARM::CPSR)) |
| 1060 | LiveCPSR = false; |
| 1061 | MachineOperand *MO = BundleMI->findRegisterDefOperand(ARM::CPSR); |
| 1062 | if (MO && !MO->isDead()) |
| 1063 | LiveCPSR = true; |
Weiming Zhao | f66be56 | 2014-01-13 18:47:54 +0000 | [diff] [blame] | 1064 | MO = BundleMI->findRegisterUseOperand(ARM::CPSR); |
| 1065 | if (MO && !MO->isKill()) |
| 1066 | LiveCPSR = true; |
Evan Cheng | 903231b | 2011-12-17 01:25:34 +0000 | [diff] [blame] | 1067 | } |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 1068 | |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 1069 | bool DefCPSR = false; |
| 1070 | LiveCPSR = UpdateCPSRDef(*MI, LiveCPSR, DefCPSR); |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 1071 | if (MI->isCall()) { |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 1072 | // Calls don't really set CPSR. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1073 | CPSRDef = nullptr; |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 1074 | HighLatencyCPSR = false; |
Evan Cheng | f4807a1 | 2011-10-27 21:21:05 +0000 | [diff] [blame] | 1075 | IsSelfLoop = false; |
| 1076 | } else if (DefCPSR) { |
Bob Wilson | a2881ee | 2011-04-19 18:11:49 +0000 | [diff] [blame] | 1077 | // This is the last CPSR defining instruction. |
| 1078 | CPSRDef = MI; |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 1079 | HighLatencyCPSR = isHighLatencyCPSR(CPSRDef); |
Evan Cheng | f4807a1 | 2011-10-27 21:21:05 +0000 | [diff] [blame] | 1080 | IsSelfLoop = false; |
| 1081 | } |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 1084 | MBBInfo &Info = BlockInfo[MBB.getNumber()]; |
| 1085 | Info.HighLatencyCPSR = HighLatencyCPSR; |
| 1086 | Info.Visited = true; |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 1087 | return Modified; |
| 1088 | } |
| 1089 | |
| 1090 | bool Thumb2SizeReduce::runOnMachineFunction(MachineFunction &MF) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1091 | if (PredicateFtor && !PredicateFtor(MF.getFunction())) |
Akira Hatanaka | 4a61619 | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 1092 | return false; |
| 1093 | |
Eric Christopher | 1b21f00 | 2015-01-29 00:19:33 +0000 | [diff] [blame] | 1094 | STI = &static_cast<const ARMSubtarget &>(MF.getSubtarget()); |
Eric Christopher | 63b4488 | 2015-03-05 00:23:40 +0000 | [diff] [blame] | 1095 | if (STI->isThumb1Only() || STI->prefers32BitThumb()) |
| 1096 | return false; |
| 1097 | |
Eric Christopher | 1b21f00 | 2015-01-29 00:19:33 +0000 | [diff] [blame] | 1098 | TII = static_cast<const Thumb2InstrInfo *>(STI->getInstrInfo()); |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 1099 | |
Sanjay Patel | 924879a | 2015-08-04 15:49:57 +0000 | [diff] [blame] | 1100 | // Optimizing / minimizing size? Minimizing size implies optimizing for size. |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1101 | OptimizeSize = MF.getFunction().optForSize(); |
| 1102 | MinimizeSize = MF.getFunction().optForMinSize(); |
Quentin Colombet | 23b404d | 2012-12-18 22:47:16 +0000 | [diff] [blame] | 1103 | |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 1104 | BlockInfo.clear(); |
| 1105 | BlockInfo.resize(MF.getNumBlockIDs()); |
| 1106 | |
| 1107 | // Visit blocks in reverse post-order so LastCPSRDef is known for all |
| 1108 | // predecessors. |
| 1109 | ReversePostOrderTraversal<MachineFunction*> RPOT(&MF); |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 1110 | bool Modified = false; |
Jakob Stoklund Olesen | 299475e | 2013-04-04 18:25:36 +0000 | [diff] [blame] | 1111 | for (ReversePostOrderTraversal<MachineFunction*>::rpo_iterator |
| 1112 | I = RPOT.begin(), E = RPOT.end(); I != E; ++I) |
| 1113 | Modified |= ReduceMBB(**I); |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 1114 | return Modified; |
| 1115 | } |
| 1116 | |
| 1117 | /// createThumb2SizeReductionPass - Returns an instance of the Thumb2 size |
| 1118 | /// reduction pass. |
Akira Hatanaka | 4a61619 | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 1119 | FunctionPass *llvm::createThumb2SizeReductionPass( |
| 1120 | std::function<bool(const Function &)> Ftor) { |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 1121 | return new Thumb2SizeReduce(std::move(Ftor)); |
Evan Cheng | 1be453b | 2009-08-08 03:21:23 +0000 | [diff] [blame] | 1122 | } |