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