| Bill Wendling | 18581a4 | 2010-12-21 01:54:40 +0000 | [diff] [blame] | 1 | //===-- ARMConstantIslandPass.cpp - ARM constant islands ------------------===// |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains a pass that splits the constant pool up into 'islands' |
| 11 | // which are scattered through-out the function. This is required due to the |
| 12 | // limited pc-relative displacements that ARM has. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 16 | #include "ARM.h" |
| Sjoerd Meijer | 5c0ef83 | 2016-07-22 08:39:12 +0000 | [diff] [blame] | 17 | #include "ARMBasicBlockInfo.h" |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 18 | #include "ARMMachineFunctionInfo.h" |
| Evan Cheng | a20cde3 | 2011-07-20 23:34:39 +0000 | [diff] [blame] | 19 | #include "MCTargetDesc/ARMAddressingModes.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "Thumb2InstrInfo.h" |
| 21 | #include "llvm/ADT/STLExtras.h" |
| 22 | #include "llvm/ADT/SmallSet.h" |
| 23 | #include "llvm/ADT/SmallVector.h" |
| 24 | #include "llvm/ADT/Statistic.h" |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 26 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| Evan Cheng | c6d70ae | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| Jakob Stoklund Olesen | d8af9a5 | 2012-03-29 23:14:26 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DataLayout.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
| Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ErrorHandling.h" |
| Jakob Stoklund Olesen | b373452 | 2011-12-10 02:55:06 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Format.h" |
| Chris Lattner | a6f074f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetMachine.h" |
| Bob Wilson | 2f9be50 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 36 | #include <algorithm> |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | |
| Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 39 | #define DEBUG_TYPE "arm-cp-islands" |
| 40 | |
| Evan Cheng | db73d68 | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 41 | STATISTIC(NumCPEs, "Number of constpool entries"); |
| 42 | STATISTIC(NumSplit, "Number of uncond branches inserted"); |
| 43 | STATISTIC(NumCBrFixed, "Number of cond branches fixed"); |
| 44 | STATISTIC(NumUBrFixed, "Number of uncond branches fixed"); |
| 45 | STATISTIC(NumTBs, "Number of table branches generated"); |
| 46 | STATISTIC(NumT2CPShrunk, "Number of Thumb2 constantpool instructions shrunk"); |
| Evan Cheng | e41903b | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 47 | STATISTIC(NumT2BrShrunk, "Number of Thumb2 immediate branches shrunk"); |
| Evan Cheng | 6f29ad9 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 48 | STATISTIC(NumCBZ, "Number of CBZ / CBNZ formed"); |
| Jim Grosbach | 8d92ec4 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 49 | STATISTIC(NumJTMoved, "Number of jump table destination blocks moved"); |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 50 | STATISTIC(NumJTInserted, "Number of jump table intermediate blocks inserted"); |
| Jim Grosbach | 8d92ec4 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 51 | |
| 52 | |
| 53 | static cl::opt<bool> |
| Jim Grosbach | cdde77c | 2009-11-17 21:24:11 +0000 | [diff] [blame] | 54 | AdjustJumpTableBlocks("arm-adjust-jump-tables", cl::Hidden, cl::init(true), |
| Jim Grosbach | 8d92ec4 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 55 | cl::desc("Adjust basic block layout to better use TB[BH]")); |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 56 | |
| Weiming Zhao | 5e0c3eb | 2016-02-23 18:39:19 +0000 | [diff] [blame] | 57 | static cl::opt<unsigned> |
| 58 | CPMaxIteration("arm-constant-island-max-iteration", cl::Hidden, cl::init(30), |
| 59 | cl::desc("The max number of iteration for converge")); |
| 60 | |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 61 | static cl::opt<bool> SynthesizeThumb1TBB( |
| 62 | "arm-synthesize-thumb-1-tbb", cl::Hidden, cl::init(true), |
| 63 | cl::desc("Use compressed jump tables in Thumb-1 by synthesizing an " |
| 64 | "equivalent to the TBB/TBH instructions")); |
| 65 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 66 | namespace { |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 67 | /// ARMConstantIslands - Due to limited PC-relative displacements, ARM |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 68 | /// requires constant pool entries to be scattered among the instructions |
| 69 | /// inside a function. To do this, it completely ignores the normal LLVM |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 70 | /// constant pool; instead, it places constants wherever it feels like with |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 71 | /// special instructions. |
| 72 | /// |
| 73 | /// The terminology used in this pass includes: |
| 74 | /// Islands - Clumps of constants placed in the function. |
| 75 | /// Water - Potential places where an island could be formed. |
| 76 | /// CPE - A constant pool entry that has been placed somewhere, which |
| 77 | /// tracks a list of users. |
| Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 78 | class ARMConstantIslands : public MachineFunctionPass { |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 79 | |
| 80 | std::vector<BasicBlockInfo> BBInfo; |
| Dale Johannesen | 01ee575 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 81 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 82 | /// WaterList - A sorted list of basic blocks where islands could be placed |
| 83 | /// (i.e. blocks that don't fall through to the following block, due |
| 84 | /// to a return, unreachable, or unconditional branch). |
| Evan Cheng | 540f5e0 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 85 | std::vector<MachineBasicBlock*> WaterList; |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 86 | |
| Bob Wilson | 2f9be50 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 87 | /// NewWaterList - The subset of WaterList that was created since the |
| 88 | /// previous iteration by inserting unconditional branches. |
| 89 | SmallSet<MachineBasicBlock*, 4> NewWaterList; |
| 90 | |
| Bob Wilson | c7a3cf4 | 2009-10-12 18:52:13 +0000 | [diff] [blame] | 91 | typedef std::vector<MachineBasicBlock*>::iterator water_iterator; |
| 92 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 93 | /// CPUser - One user of a constant pool, keeping the machine instruction |
| 94 | /// pointer, the constant pool being referenced, and the max displacement |
| Bob Wilson | 68ead6c | 2009-10-15 05:52:29 +0000 | [diff] [blame] | 95 | /// allowed from the instruction to the CP. The HighWaterMark records the |
| 96 | /// highest basic block where a new CPEntry can be placed. To ensure this |
| 97 | /// pass terminates, the CP entries are initially placed at the end of the |
| 98 | /// function and then move monotonically to lower addresses. The |
| 99 | /// exception to this rule is when the current CP entry for a particular |
| 100 | /// CPUser is out of range, but there is another CP entry for the same |
| 101 | /// constant value in range. We want to use the existing in-range CP |
| 102 | /// entry, but if it later moves out of range, the search for new water |
| 103 | /// should resume where it left off. The HighWaterMark is used to record |
| 104 | /// that point. |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 105 | struct CPUser { |
| 106 | MachineInstr *MI; |
| 107 | MachineInstr *CPEMI; |
| Bob Wilson | 68ead6c | 2009-10-15 05:52:29 +0000 | [diff] [blame] | 108 | MachineBasicBlock *HighWaterMark; |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 109 | unsigned MaxDisp; |
| Evan Cheng | 87aaa19 | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 110 | bool NegOk; |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 111 | bool IsSoImm; |
| Jakob Stoklund Olesen | f09a316 | 2012-01-10 01:34:59 +0000 | [diff] [blame] | 112 | bool KnownAlignment; |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 113 | CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp, |
| 114 | bool neg, bool soimm) |
| Jakob Stoklund Olesen | f09a316 | 2012-01-10 01:34:59 +0000 | [diff] [blame] | 115 | : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp), NegOk(neg), IsSoImm(soimm), |
| 116 | KnownAlignment(false) { |
| Bob Wilson | 68ead6c | 2009-10-15 05:52:29 +0000 | [diff] [blame] | 117 | HighWaterMark = CPEMI->getParent(); |
| 118 | } |
| Jakob Stoklund Olesen | f09a316 | 2012-01-10 01:34:59 +0000 | [diff] [blame] | 119 | /// getMaxDisp - Returns the maximum displacement supported by MI. |
| 120 | /// Correct for unknown alignment. |
| Jakob Stoklund Olesen | d915503 | 2012-03-31 00:06:44 +0000 | [diff] [blame] | 121 | /// Conservatively subtract 2 bytes to handle weird alignment effects. |
| Jakob Stoklund Olesen | f09a316 | 2012-01-10 01:34:59 +0000 | [diff] [blame] | 122 | unsigned getMaxDisp() const { |
| Jakob Stoklund Olesen | d915503 | 2012-03-31 00:06:44 +0000 | [diff] [blame] | 123 | return (KnownAlignment ? MaxDisp : MaxDisp - 2) - 2; |
| Jakob Stoklund Olesen | f09a316 | 2012-01-10 01:34:59 +0000 | [diff] [blame] | 124 | } |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 125 | }; |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 126 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 127 | /// CPUsers - Keep track of all of the machine instructions that use various |
| 128 | /// constant pools and their max displacement. |
| Evan Cheng | 540f5e0 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 129 | std::vector<CPUser> CPUsers; |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 130 | |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 131 | /// CPEntry - One per constant pool entry, keeping the machine instruction |
| 132 | /// pointer, the constpool index, and the number of CPUser's which |
| 133 | /// reference this entry. |
| 134 | struct CPEntry { |
| 135 | MachineInstr *CPEMI; |
| 136 | unsigned CPI; |
| 137 | unsigned RefCount; |
| 138 | CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0) |
| 139 | : CPEMI(cpemi), CPI(cpi), RefCount(rc) {} |
| 140 | }; |
| 141 | |
| 142 | /// CPEntries - Keep track of all of the constant pool entry machine |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 143 | /// instructions. For each original constpool index (i.e. those that existed |
| 144 | /// upon entry to this pass), it keeps a vector of entries. Original |
| 145 | /// elements are cloned as we go along; the clones are put in the vector of |
| 146 | /// the original element, but have distinct CPIs. |
| 147 | /// |
| 148 | /// The first half of CPEntries contains generic constants, the second half |
| 149 | /// contains jump tables. Use getCombinedIndex on a generic CPEMI to look up |
| 150 | /// which vector it will be in here. |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 151 | std::vector<std::vector<CPEntry> > CPEntries; |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 152 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 153 | /// Maps a JT index to the offset in CPEntries containing copies of that |
| 154 | /// table. The equivalent map for a CONSTPOOL_ENTRY is the identity. |
| 155 | DenseMap<int, int> JumpTableEntryIndices; |
| 156 | |
| 157 | /// Maps a JT index to the LEA that actually uses the index to calculate its |
| 158 | /// base address. |
| 159 | DenseMap<int, int> JumpTableUserIndices; |
| 160 | |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 161 | /// ImmBranch - One per immediate branch, keeping the machine instruction |
| 162 | /// pointer, conditional or unconditional, the max displacement, |
| 163 | /// and (if isCond is true) the corresponding unconditional branch |
| 164 | /// opcode. |
| 165 | struct ImmBranch { |
| 166 | MachineInstr *MI; |
| Evan Cheng | 010ae38 | 2007-01-25 23:18:59 +0000 | [diff] [blame] | 167 | unsigned MaxDisp : 31; |
| 168 | bool isCond : 1; |
| Matthias Braun | fa3872e | 2015-05-18 20:27:55 +0000 | [diff] [blame] | 169 | unsigned UncondBr; |
| 170 | ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, unsigned ubr) |
| Evan Cheng | 010ae38 | 2007-01-25 23:18:59 +0000 | [diff] [blame] | 171 | : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {} |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
| Evan Cheng | c95f95b | 2007-05-16 05:14:06 +0000 | [diff] [blame] | 174 | /// ImmBranches - Keep track of all the immediate branch instructions. |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 175 | /// |
| Evan Cheng | 540f5e0 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 176 | std::vector<ImmBranch> ImmBranches; |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 177 | |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 178 | /// PushPopMIs - Keep track of all the Thumb push / pop instructions. |
| 179 | /// |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 180 | SmallVector<MachineInstr*, 4> PushPopMIs; |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 181 | |
| Evan Cheng | c6d70ae | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 182 | /// T2JumpTables - Keep track of all the Thumb2 jumptable instructions. |
| 183 | SmallVector<MachineInstr*, 4> T2JumpTables; |
| 184 | |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 185 | /// HasFarJump - True if any far jump instruction has been emitted during |
| 186 | /// the branch fix up pass. |
| 187 | bool HasFarJump; |
| 188 | |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 189 | MachineFunction *MF; |
| 190 | MachineConstantPool *MCP; |
| Craig Topper | 07720d8 | 2012-03-25 23:49:58 +0000 | [diff] [blame] | 191 | const ARMBaseInstrInfo *TII; |
| Evan Cheng | e64f48b | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 192 | const ARMSubtarget *STI; |
| Dale Johannesen | 4a00cf3 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 193 | ARMFunctionInfo *AFI; |
| Dale Johannesen | 962fa8e | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 194 | bool isThumb; |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 195 | bool isThumb1; |
| David Goodwin | 27303cd | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 196 | bool isThumb2; |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 197 | bool isPositionIndependentOrROPI; |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 198 | public: |
| Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 199 | static char ID; |
| Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 200 | ARMConstantIslands() : MachineFunctionPass(ID) {} |
| Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 201 | |
| Craig Topper | 6bc27bf | 2014-03-10 02:09:33 +0000 | [diff] [blame] | 202 | bool runOnMachineFunction(MachineFunction &MF) override; |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 203 | |
| Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 204 | MachineFunctionProperties getRequiredProperties() const override { |
| 205 | return MachineFunctionProperties().set( |
| Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 206 | MachineFunctionProperties::Property::NoVRegs); |
| Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 209 | StringRef getPassName() const override { |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 210 | return "ARM constant island placement and branch shortening pass"; |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 211 | } |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 212 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 213 | private: |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 214 | void doInitialConstPlacement(std::vector<MachineInstr *> &CPEMIs); |
| 215 | void doInitialJumpTablePlacement(std::vector<MachineInstr *> &CPEMIs); |
| Tim Northover | ab85dcc | 2014-11-13 17:58:51 +0000 | [diff] [blame] | 216 | bool BBHasFallthrough(MachineBasicBlock *MBB); |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 217 | CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI); |
| Jakob Stoklund Olesen | 17c27a8 | 2011-12-12 18:45:45 +0000 | [diff] [blame] | 218 | unsigned getCPELogAlign(const MachineInstr *CPEMI); |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 219 | void scanFunctionJumpTables(); |
| 220 | void initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs); |
| 221 | MachineBasicBlock *splitBlockBeforeInstr(MachineInstr *MI); |
| 222 | void updateForInsertedWaterBlock(MachineBasicBlock *NewBB); |
| 223 | void adjustBBOffsetsAfter(MachineBasicBlock *BB); |
| 224 | bool decrementCPEReferenceCount(unsigned CPI, MachineInstr* CPEMI); |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 225 | unsigned getCombinedIndex(const MachineInstr *CPEMI); |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 226 | int findInRangeCPEntry(CPUser& U, unsigned UserOffset); |
| 227 | bool findAvailableWater(CPUser&U, unsigned UserOffset, |
| Weiming Zhao | 5e0c3eb | 2016-02-23 18:39:19 +0000 | [diff] [blame] | 228 | water_iterator &WaterIter, bool CloserWater); |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 229 | void createNewWater(unsigned CPUserIndex, unsigned UserOffset, |
| Bob Wilson | 3250e77 | 2009-10-12 21:39:43 +0000 | [diff] [blame] | 230 | MachineBasicBlock *&NewMBB); |
| Weiming Zhao | 5e0c3eb | 2016-02-23 18:39:19 +0000 | [diff] [blame] | 231 | bool handleConstantPoolUser(unsigned CPUserIndex, bool CloserWater); |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 232 | void removeDeadCPEMI(MachineInstr *CPEMI); |
| 233 | bool removeUnusedCPEntries(); |
| 234 | bool isCPEntryInRange(MachineInstr *MI, unsigned UserOffset, |
| 235 | MachineInstr *CPEMI, unsigned Disp, bool NegOk, |
| 236 | bool DoDump = false); |
| 237 | bool isWaterInRange(unsigned UserOffset, MachineBasicBlock *Water, |
| Jakob Stoklund Olesen | bfa576f | 2011-12-13 00:44:30 +0000 | [diff] [blame] | 238 | CPUser &U, unsigned &Growth); |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 239 | bool isBBInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp); |
| 240 | bool fixupImmediateBr(ImmBranch &Br); |
| 241 | bool fixupConditionalBr(ImmBranch &Br); |
| 242 | bool fixupUnconditionalBr(ImmBranch &Br); |
| 243 | bool undoLRSpillRestore(); |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 244 | bool optimizeThumb2Instructions(); |
| 245 | bool optimizeThumb2Branches(); |
| 246 | bool reorderThumb2JumpTables(); |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 247 | bool preserveBaseRegister(MachineInstr *JumpMI, MachineInstr *LEAMI, |
| 248 | unsigned &DeadSize, bool &CanDeleteLEA, |
| 249 | bool &BaseRegKill); |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 250 | bool optimizeThumb2JumpTables(); |
| 251 | MachineBasicBlock *adjustJTTargetBlockForward(MachineBasicBlock *BB, |
| Jim Grosbach | 8d92ec4 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 252 | MachineBasicBlock *JTBB); |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 253 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 254 | unsigned getOffsetOf(MachineInstr *MI) const; |
| 255 | unsigned getUserOffset(CPUser&) const; |
| Dale Johannesen | 4a00cf3 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 256 | void dumpBBs(); |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 257 | void verify(); |
| Jakob Stoklund Olesen | f857236 | 2011-12-09 19:44:39 +0000 | [diff] [blame] | 258 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 259 | bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset, |
| Jakob Stoklund Olesen | f857236 | 2011-12-09 19:44:39 +0000 | [diff] [blame] | 260 | unsigned Disp, bool NegativeOK, bool IsSoImm = false); |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 261 | bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset, |
| Jakob Stoklund Olesen | f857236 | 2011-12-09 19:44:39 +0000 | [diff] [blame] | 262 | const CPUser &U) { |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 263 | return isOffsetInRange(UserOffset, TrialOffset, |
| Jakob Stoklund Olesen | f09a316 | 2012-01-10 01:34:59 +0000 | [diff] [blame] | 264 | U.getMaxDisp(), U.NegOk, U.IsSoImm); |
| Jakob Stoklund Olesen | f857236 | 2011-12-09 19:44:39 +0000 | [diff] [blame] | 265 | } |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 266 | }; |
| Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 267 | char ARMConstantIslands::ID = 0; |
| Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 268 | } |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 269 | |
| Dale Johannesen | 4a00cf3 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 270 | /// verify - check BBOffsets, BBSizes, alignment of islands |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 271 | void ARMConstantIslands::verify() { |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 272 | #ifndef NDEBUG |
| Craig Topper | e30b8ca | 2016-01-03 19:43:40 +0000 | [diff] [blame] | 273 | assert(std::is_sorted(MF->begin(), MF->end(), |
| 274 | [this](const MachineBasicBlock &LHS, |
| 275 | const MachineBasicBlock &RHS) { |
| 276 | return BBInfo[LHS.getNumber()].postOffset() < |
| 277 | BBInfo[RHS.getNumber()].postOffset(); |
| 278 | })); |
| Jakob Stoklund Olesen | 24bb3d5 | 2012-03-31 00:06:42 +0000 | [diff] [blame] | 279 | DEBUG(dbgs() << "Verifying " << CPUsers.size() << " CP users.\n"); |
| Jim Grosbach | b73918c | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 280 | for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) { |
| 281 | CPUser &U = CPUsers[i]; |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 282 | unsigned UserOffset = getUserOffset(U); |
| Jakob Stoklund Olesen | d915503 | 2012-03-31 00:06:44 +0000 | [diff] [blame] | 283 | // Verify offset using the real max displacement without the safety |
| 284 | // adjustment. |
| 285 | if (isCPEntryInRange(U.MI, UserOffset, U.CPEMI, U.getMaxDisp()+2, U.NegOk, |
| Jakob Stoklund Olesen | 24bb3d5 | 2012-03-31 00:06:42 +0000 | [diff] [blame] | 286 | /* DoDump = */ true)) { |
| 287 | DEBUG(dbgs() << "OK\n"); |
| 288 | continue; |
| 289 | } |
| 290 | DEBUG(dbgs() << "Out of range.\n"); |
| 291 | dumpBBs(); |
| 292 | DEBUG(MF->dump()); |
| 293 | llvm_unreachable("Constant pool entry out of range!"); |
| Jim Grosbach | b73918c | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 294 | } |
| Jim Grosbach | 6c3b711 | 2009-11-20 19:37:38 +0000 | [diff] [blame] | 295 | #endif |
| Dale Johannesen | 4a00cf3 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /// print block size and offset information - debugging |
| 299 | void ARMConstantIslands::dumpBBs() { |
| Jakob Stoklund Olesen | b373452 | 2011-12-10 02:55:06 +0000 | [diff] [blame] | 300 | DEBUG({ |
| 301 | for (unsigned J = 0, E = BBInfo.size(); J !=E; ++J) { |
| 302 | const BasicBlockInfo &BBI = BBInfo[J]; |
| 303 | dbgs() << format("%08x BB#%u\t", BBI.Offset, J) |
| 304 | << " kb=" << unsigned(BBI.KnownBits) |
| 305 | << " ua=" << unsigned(BBI.Unalign) |
| 306 | << " pa=" << unsigned(BBI.PostAlign) |
| 307 | << format(" size=%#x\n", BBInfo[J].Size); |
| 308 | } |
| 309 | }); |
| Dale Johannesen | 4a00cf3 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 312 | /// createARMConstantIslandPass - returns an instance of the constpool |
| 313 | /// island pass. |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 314 | FunctionPass *llvm::createARMConstantIslandPass() { |
| 315 | return new ARMConstantIslands(); |
| 316 | } |
| 317 | |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 318 | bool ARMConstantIslands::runOnMachineFunction(MachineFunction &mf) { |
| 319 | MF = &mf; |
| 320 | MCP = mf.getConstantPool(); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 321 | |
| Jakob Stoklund Olesen | b373452 | 2011-12-10 02:55:06 +0000 | [diff] [blame] | 322 | DEBUG(dbgs() << "***** ARMConstantIslands: " |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 323 | << MCP->getConstants().size() << " CP entries, aligned to " |
| 324 | << MCP->getConstantPoolAlignment() << " bytes *****\n"); |
| Jakob Stoklund Olesen | b373452 | 2011-12-10 02:55:06 +0000 | [diff] [blame] | 325 | |
| Eric Christopher | 1b21f00 | 2015-01-29 00:19:33 +0000 | [diff] [blame] | 326 | STI = &static_cast<const ARMSubtarget &>(MF->getSubtarget()); |
| 327 | TII = STI->getInstrInfo(); |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 328 | isPositionIndependentOrROPI = |
| 329 | STI->getTargetLowering()->isPositionIndependent() || STI->isROPI(); |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 330 | AFI = MF->getInfo<ARMFunctionInfo>(); |
| Evan Cheng | e64f48b | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 331 | |
| Dale Johannesen | 962fa8e | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 332 | isThumb = AFI->isThumbFunction(); |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 333 | isThumb1 = AFI->isThumb1OnlyFunction(); |
| David Goodwin | 27303cd | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 334 | isThumb2 = AFI->isThumb2Function(); |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 335 | |
| 336 | HasFarJump = false; |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 337 | bool GenerateTBB = isThumb2 || (isThumb1 && SynthesizeThumb1TBB); |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 338 | |
| Jakob Stoklund Olesen | d8af9a5 | 2012-03-29 23:14:26 +0000 | [diff] [blame] | 339 | // This pass invalidates liveness information when it splits basic blocks. |
| 340 | MF->getRegInfo().invalidateLiveness(); |
| 341 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 342 | // Renumber all of the machine basic blocks in the function, guaranteeing that |
| 343 | // the numbers agree with the position of the block in the function. |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 344 | MF->RenumberBlocks(); |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 345 | |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 346 | // Try to reorder and otherwise adjust the block layout to make good use |
| 347 | // of the TB[BH] instructions. |
| 348 | bool MadeChange = false; |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 349 | if (GenerateTBB && AdjustJumpTableBlocks) { |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 350 | scanFunctionJumpTables(); |
| 351 | MadeChange |= reorderThumb2JumpTables(); |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 352 | // Data is out of date, so clear it. It'll be re-computed later. |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 353 | T2JumpTables.clear(); |
| 354 | // Blocks may have shifted around. Keep the numbering up to date. |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 355 | MF->RenumberBlocks(); |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 358 | // Perform the initial placement of the constant pool entries. To start with, |
| 359 | // we put them all at the end of the function. |
| Evan Cheng | 540f5e0 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 360 | std::vector<MachineInstr*> CPEMIs; |
| Jakob Stoklund Olesen | 17c27a8 | 2011-12-12 18:45:45 +0000 | [diff] [blame] | 361 | if (!MCP->isEmpty()) |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 362 | doInitialConstPlacement(CPEMIs); |
| 363 | |
| 364 | if (MF->getJumpTableInfo()) |
| 365 | doInitialJumpTablePlacement(CPEMIs); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 366 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 367 | /// The next UID to take is the first unused one. |
| Evan Cheng | dfce83c | 2011-01-17 08:03:18 +0000 | [diff] [blame] | 368 | AFI->initPICLabelUId(CPEMIs.size()); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 369 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 370 | // Do the initial scan of the function, building up information about the |
| 371 | // sizes of each block, the location of all the water, and finding all of the |
| 372 | // constant pool users. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 373 | initializeFunctionInfo(CPEMIs); |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 374 | CPEMIs.clear(); |
| Dale Johannesen | c17dd57 | 2010-07-23 22:50:23 +0000 | [diff] [blame] | 375 | DEBUG(dumpBBs()); |
| 376 | |
| Peter Collingbourne | d27d3a1 | 2015-05-01 18:05:59 +0000 | [diff] [blame] | 377 | // Functions with jump tables need an alignment of 4 because they use the ADR |
| 378 | // instruction, which aligns the PC to 4 bytes before adding an offset. |
| 379 | if (!T2JumpTables.empty()) |
| 380 | MF->ensureAlignment(2); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 381 | |
| Evan Cheng | 3c68d4e | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 382 | /// Remove dead constant pool entries. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 383 | MadeChange |= removeUnusedCPEntries(); |
| Evan Cheng | 3c68d4e | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 384 | |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 385 | // Iteratively place constant pool entries and fix up branches until there |
| 386 | // is no change. |
| Evan Cheng | 82ff022 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 387 | unsigned NoCPIters = 0, NoBRIters = 0; |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 388 | while (true) { |
| Jakob Stoklund Olesen | 5f5fa12 | 2011-12-09 18:20:35 +0000 | [diff] [blame] | 389 | DEBUG(dbgs() << "Beginning CP iteration #" << NoCPIters << '\n'); |
| Evan Cheng | 82ff022 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 390 | bool CPChange = false; |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 391 | for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) |
| Weiming Zhao | 5e0c3eb | 2016-02-23 18:39:19 +0000 | [diff] [blame] | 392 | // For most inputs, it converges in no more than 5 iterations. |
| David Majnemer | 68318e0 | 2016-04-22 06:37:48 +0000 | [diff] [blame] | 393 | // If it doesn't end in 10, the input may have huge BB or many CPEs. |
| 394 | // In this case, we will try different heuristics. |
| Weiming Zhao | 5e0c3eb | 2016-02-23 18:39:19 +0000 | [diff] [blame] | 395 | CPChange |= handleConstantPoolUser(i, NoCPIters >= CPMaxIteration / 2); |
| 396 | if (CPChange && ++NoCPIters > CPMaxIteration) |
| Jakob Stoklund Olesen | 1a80e3a | 2012-01-09 22:16:24 +0000 | [diff] [blame] | 397 | report_fatal_error("Constant Island pass failed to converge!"); |
| Evan Cheng | 94579db | 2007-07-10 22:00:16 +0000 | [diff] [blame] | 398 | DEBUG(dumpBBs()); |
| Jim Grosbach | e4ba2aa | 2010-07-07 21:06:51 +0000 | [diff] [blame] | 399 | |
| Bob Wilson | 2f9be50 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 400 | // Clear NewWaterList now. If we split a block for branches, it should |
| 401 | // appear as "new water" for the next iteration of constant pool placement. |
| 402 | NewWaterList.clear(); |
| Evan Cheng | 82ff022 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 403 | |
| Jakob Stoklund Olesen | 5f5fa12 | 2011-12-09 18:20:35 +0000 | [diff] [blame] | 404 | DEBUG(dbgs() << "Beginning BR iteration #" << NoBRIters << '\n'); |
| Evan Cheng | 82ff022 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 405 | bool BRChange = false; |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 406 | for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 407 | BRChange |= fixupImmediateBr(ImmBranches[i]); |
| Evan Cheng | 82ff022 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 408 | if (BRChange && ++NoBRIters > 30) |
| Jakob Stoklund Olesen | 1a80e3a | 2012-01-09 22:16:24 +0000 | [diff] [blame] | 409 | report_fatal_error("Branch Fix Up pass failed to converge!"); |
| Evan Cheng | 94579db | 2007-07-10 22:00:16 +0000 | [diff] [blame] | 410 | DEBUG(dumpBBs()); |
| Evan Cheng | 82ff022 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 411 | |
| 412 | if (!CPChange && !BRChange) |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 413 | break; |
| 414 | MadeChange = true; |
| 415 | } |
| Evan Cheng | 3c68d4e | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 416 | |
| Bradley Smith | a118910 | 2016-01-15 10:26:17 +0000 | [diff] [blame] | 417 | // Shrink 32-bit Thumb2 load and store instructions. |
| Evan Cheng | ce8fb68 | 2010-08-09 18:35:19 +0000 | [diff] [blame] | 418 | if (isThumb2 && !STI->prefers32BitThumb()) |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 419 | MadeChange |= optimizeThumb2Instructions(); |
| Evan Cheng | e64f48b | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 420 | |
| Bradley Smith | a118910 | 2016-01-15 10:26:17 +0000 | [diff] [blame] | 421 | // Shrink 32-bit branch instructions. |
| 422 | if (isThumb && STI->hasV8MBaselineOps()) |
| 423 | MadeChange |= optimizeThumb2Branches(); |
| 424 | |
| 425 | // Optimize jump tables using TBB / TBH. |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 426 | if (GenerateTBB) |
| Bradley Smith | a118910 | 2016-01-15 10:26:17 +0000 | [diff] [blame] | 427 | MadeChange |= optimizeThumb2JumpTables(); |
| 428 | |
| Dale Johannesen | 4a00cf3 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 429 | // After a while, this might be made debug-only, but it is not expensive. |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 430 | verify(); |
| Dale Johannesen | 4a00cf3 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 431 | |
| Jim Grosbach | e4ba2aa | 2010-07-07 21:06:51 +0000 | [diff] [blame] | 432 | // If LR has been forced spilled and no far jump (i.e. BL) has been issued, |
| 433 | // undo the spill / restore of LR if possible. |
| Evan Cheng | c6d70ae | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 434 | if (isThumb && !HasFarJump && AFI->isLRSpilledForFarJump()) |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 435 | MadeChange |= undoLRSpillRestore(); |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 436 | |
| Anton Korobeynikov | 221f4fa | 2011-01-30 22:07:39 +0000 | [diff] [blame] | 437 | // Save the mapping between original and cloned constpool entries. |
| 438 | for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) { |
| 439 | for (unsigned j = 0, je = CPEntries[i].size(); j != je; ++j) { |
| 440 | const CPEntry & CPE = CPEntries[i][j]; |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 441 | if (CPE.CPEMI && CPE.CPEMI->getOperand(1).isCPI()) |
| 442 | AFI->recordCPEClone(i, CPE.CPI); |
| Anton Korobeynikov | 221f4fa | 2011-01-30 22:07:39 +0000 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
| Jakob Stoklund Olesen | 5f5fa12 | 2011-12-09 18:20:35 +0000 | [diff] [blame] | 446 | DEBUG(dbgs() << '\n'; dumpBBs()); |
| Evan Cheng | 3fabe07 | 2010-07-22 02:09:47 +0000 | [diff] [blame] | 447 | |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 448 | BBInfo.clear(); |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 449 | WaterList.clear(); |
| 450 | CPUsers.clear(); |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 451 | CPEntries.clear(); |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 452 | JumpTableEntryIndices.clear(); |
| 453 | JumpTableUserIndices.clear(); |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 454 | ImmBranches.clear(); |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 455 | PushPopMIs.clear(); |
| Evan Cheng | c6d70ae | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 456 | T2JumpTables.clear(); |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 457 | |
| 458 | return MadeChange; |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 461 | /// \brief Perform the initial placement of the regular constant pool entries. |
| 462 | /// To start with, we put them all at the end of the function. |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 463 | void |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 464 | ARMConstantIslands::doInitialConstPlacement(std::vector<MachineInstr*> &CPEMIs) { |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 465 | // Create the basic block to hold the CPE's. |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 466 | MachineBasicBlock *BB = MF->CreateMachineBasicBlock(); |
| 467 | MF->push_back(BB); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 468 | |
| Jakob Stoklund Olesen | b5f52aa | 2011-12-12 16:49:37 +0000 | [diff] [blame] | 469 | // MachineConstantPool measures alignment in bytes. We measure in log2(bytes). |
| Jakob Stoklund Olesen | e5585e8 | 2011-12-14 18:49:13 +0000 | [diff] [blame] | 470 | unsigned MaxAlign = Log2_32(MCP->getConstantPoolAlignment()); |
| Jakob Stoklund Olesen | b5f52aa | 2011-12-12 16:49:37 +0000 | [diff] [blame] | 471 | |
| 472 | // Mark the basic block as required by the const-pool. |
| Peter Collingbourne | 1213918 | 2015-04-23 20:31:22 +0000 | [diff] [blame] | 473 | BB->setAlignment(MaxAlign); |
| Jakob Stoklund Olesen | b5f52aa | 2011-12-12 16:49:37 +0000 | [diff] [blame] | 474 | |
| Jakob Stoklund Olesen | 17c27a8 | 2011-12-12 18:45:45 +0000 | [diff] [blame] | 475 | // The function needs to be as aligned as the basic blocks. The linker may |
| 476 | // move functions around based on their alignment. |
| Chad Rosier | 73b0282 | 2012-07-06 23:13:38 +0000 | [diff] [blame] | 477 | MF->ensureAlignment(BB->getAlignment()); |
| Jakob Stoklund Olesen | 17c27a8 | 2011-12-12 18:45:45 +0000 | [diff] [blame] | 478 | |
| Jakob Stoklund Olesen | b5f52aa | 2011-12-12 16:49:37 +0000 | [diff] [blame] | 479 | // Order the entries in BB by descending alignment. That ensures correct |
| 480 | // alignment of all entries as long as BB is sufficiently aligned. Keep |
| 481 | // track of the insertion point for each alignment. We are going to bucket |
| 482 | // sort the entries as they are created. |
| 483 | SmallVector<MachineBasicBlock::iterator, 8> InsPoint(MaxAlign + 1, BB->end()); |
| Jakob Stoklund Olesen | 2e05db2 | 2011-12-06 01:43:02 +0000 | [diff] [blame] | 484 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 485 | // Add all of the constants from the constant pool to the end block, use an |
| 486 | // identity mapping of CPI's to CPE's. |
| Jakob Stoklund Olesen | e5585e8 | 2011-12-14 18:49:13 +0000 | [diff] [blame] | 487 | const std::vector<MachineConstantPoolEntry> &CPs = MCP->getConstants(); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 488 | |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 489 | const DataLayout &TD = MF->getDataLayout(); |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 490 | for (unsigned i = 0, e = CPs.size(); i != e; ++i) { |
| Duncan Sands | af9eaa8 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 491 | unsigned Size = TD.getTypeAllocSize(CPs[i].getType()); |
| Jakob Stoklund Olesen | b5f52aa | 2011-12-12 16:49:37 +0000 | [diff] [blame] | 492 | assert(Size >= 4 && "Too small constant pool entry"); |
| 493 | unsigned Align = CPs[i].getAlignment(); |
| 494 | assert(isPowerOf2_32(Align) && "Invalid alignment"); |
| 495 | // Verify that all constant pool entries are a multiple of their alignment. |
| 496 | // If not, we would have to pad them out so that instructions stay aligned. |
| 497 | assert((Size % Align) == 0 && "CP Entry not multiple of 4 bytes!"); |
| 498 | |
| 499 | // Insert CONSTPOOL_ENTRY before entries with a smaller alignment. |
| 500 | unsigned LogAlign = Log2_32(Align); |
| 501 | MachineBasicBlock::iterator InsAt = InsPoint[LogAlign]; |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 502 | MachineInstr *CPEMI = |
| Jakob Stoklund Olesen | b5f52aa | 2011-12-12 16:49:37 +0000 | [diff] [blame] | 503 | BuildMI(*BB, InsAt, DebugLoc(), TII->get(ARM::CONSTPOOL_ENTRY)) |
| Chris Lattner | 6f306d7 | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 504 | .addImm(i).addConstantPoolIndex(i).addImm(Size); |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 505 | CPEMIs.push_back(CPEMI); |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 506 | |
| Jakob Stoklund Olesen | b5f52aa | 2011-12-12 16:49:37 +0000 | [diff] [blame] | 507 | // Ensure that future entries with higher alignment get inserted before |
| 508 | // CPEMI. This is bucket sort with iterators. |
| Jakob Stoklund Olesen | 9790187 | 2011-12-16 23:00:05 +0000 | [diff] [blame] | 509 | for (unsigned a = LogAlign + 1; a <= MaxAlign; ++a) |
| Jakob Stoklund Olesen | b5f52aa | 2011-12-12 16:49:37 +0000 | [diff] [blame] | 510 | if (InsPoint[a] == InsAt) |
| 511 | InsPoint[a] = CPEMI; |
| 512 | |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 513 | // Add a new CPEntry, but no corresponding CPUser yet. |
| Benjamin Kramer | e12a6ba | 2014-10-03 18:33:16 +0000 | [diff] [blame] | 514 | CPEntries.emplace_back(1, CPEntry(CPEMI, i)); |
| Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 515 | ++NumCPEs; |
| Jakob Stoklund Olesen | f09a316 | 2012-01-10 01:34:59 +0000 | [diff] [blame] | 516 | DEBUG(dbgs() << "Moved CPI#" << i << " to end of function, size = " |
| 517 | << Size << ", align = " << Align <<'\n'); |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 518 | } |
| Jakob Stoklund Olesen | b5f52aa | 2011-12-12 16:49:37 +0000 | [diff] [blame] | 519 | DEBUG(BB->dump()); |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 522 | /// \brief Do initial placement of the jump tables. Because Thumb2's TBB and TBH |
| 523 | /// instructions can be made more efficient if the jump table immediately |
| 524 | /// follows the instruction, it's best to place them immediately next to their |
| 525 | /// jumps to begin with. In almost all cases they'll never be moved from that |
| 526 | /// position. |
| 527 | void ARMConstantIslands::doInitialJumpTablePlacement( |
| 528 | std::vector<MachineInstr *> &CPEMIs) { |
| 529 | unsigned i = CPEntries.size(); |
| 530 | auto MJTI = MF->getJumpTableInfo(); |
| 531 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 532 | |
| 533 | MachineBasicBlock *LastCorrectlyNumberedBB = nullptr; |
| 534 | for (MachineBasicBlock &MBB : *MF) { |
| 535 | auto MI = MBB.getLastNonDebugInstr(); |
| Petr Pavlu | a770379 | 2015-11-16 16:41:13 +0000 | [diff] [blame] | 536 | if (MI == MBB.end()) |
| 537 | continue; |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 538 | |
| 539 | unsigned JTOpcode; |
| 540 | switch (MI->getOpcode()) { |
| 541 | default: |
| 542 | continue; |
| 543 | case ARM::BR_JTadd: |
| 544 | case ARM::BR_JTr: |
| 545 | case ARM::tBR_JTr: |
| 546 | case ARM::BR_JTm: |
| 547 | JTOpcode = ARM::JUMPTABLE_ADDRS; |
| 548 | break; |
| 549 | case ARM::t2BR_JT: |
| 550 | JTOpcode = ARM::JUMPTABLE_INSTS; |
| 551 | break; |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 552 | case ARM::tTBB_JT: |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 553 | case ARM::t2TBB_JT: |
| 554 | JTOpcode = ARM::JUMPTABLE_TBB; |
| 555 | break; |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 556 | case ARM::tTBH_JT: |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 557 | case ARM::t2TBH_JT: |
| 558 | JTOpcode = ARM::JUMPTABLE_TBH; |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | unsigned NumOps = MI->getDesc().getNumOperands(); |
| 563 | MachineOperand JTOp = |
| 564 | MI->getOperand(NumOps - (MI->isPredicable() ? 2 : 1)); |
| 565 | unsigned JTI = JTOp.getIndex(); |
| 566 | unsigned Size = JT[JTI].MBBs.size() * sizeof(uint32_t); |
| 567 | MachineBasicBlock *JumpTableBB = MF->CreateMachineBasicBlock(); |
| 568 | MF->insert(std::next(MachineFunction::iterator(MBB)), JumpTableBB); |
| 569 | MachineInstr *CPEMI = BuildMI(*JumpTableBB, JumpTableBB->begin(), |
| 570 | DebugLoc(), TII->get(JTOpcode)) |
| 571 | .addImm(i++) |
| 572 | .addJumpTableIndex(JTI) |
| 573 | .addImm(Size); |
| 574 | CPEMIs.push_back(CPEMI); |
| 575 | CPEntries.emplace_back(1, CPEntry(CPEMI, JTI)); |
| 576 | JumpTableEntryIndices.insert(std::make_pair(JTI, CPEntries.size() - 1)); |
| 577 | if (!LastCorrectlyNumberedBB) |
| 578 | LastCorrectlyNumberedBB = &MBB; |
| 579 | } |
| 580 | |
| 581 | // If we did anything then we need to renumber the subsequent blocks. |
| 582 | if (LastCorrectlyNumberedBB) |
| 583 | MF->RenumberBlocks(LastCorrectlyNumberedBB); |
| 584 | } |
| 585 | |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 586 | /// BBHasFallthrough - Return true if the specified basic block can fallthrough |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 587 | /// into the block immediately after it. |
| Tim Northover | ab85dcc | 2014-11-13 17:58:51 +0000 | [diff] [blame] | 588 | bool ARMConstantIslands::BBHasFallthrough(MachineBasicBlock *MBB) { |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 589 | // Get the next machine basic block in the function. |
| Duncan P. N. Exon Smith | 9f9559e | 2015-10-19 23:25:57 +0000 | [diff] [blame] | 590 | MachineFunction::iterator MBBI = MBB->getIterator(); |
| Jim Grosbach | 84511e1 | 2010-06-02 21:53:11 +0000 | [diff] [blame] | 591 | // Can't fall off end of function. |
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 592 | if (std::next(MBBI) == MBB->getParent()->end()) |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 593 | return false; |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 594 | |
| Duncan P. N. Exon Smith | 9f9559e | 2015-10-19 23:25:57 +0000 | [diff] [blame] | 595 | MachineBasicBlock *NextBB = &*std::next(MBBI); |
| David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 596 | if (!MBB->isSuccessor(NextBB)) |
| Tim Northover | ab85dcc | 2014-11-13 17:58:51 +0000 | [diff] [blame] | 597 | return false; |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 598 | |
| Tim Northover | ab85dcc | 2014-11-13 17:58:51 +0000 | [diff] [blame] | 599 | // Try to analyze the end of the block. A potential fallthrough may already |
| 600 | // have an unconditional branch for whatever reason. |
| 601 | MachineBasicBlock *TBB, *FBB; |
| 602 | SmallVector<MachineOperand, 4> Cond; |
| Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 603 | bool TooDifficult = TII->analyzeBranch(*MBB, TBB, FBB, Cond); |
| Tim Northover | ab85dcc | 2014-11-13 17:58:51 +0000 | [diff] [blame] | 604 | return TooDifficult || FBB == nullptr; |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 607 | /// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI, |
| 608 | /// look up the corresponding CPEntry. |
| 609 | ARMConstantIslands::CPEntry |
| 610 | *ARMConstantIslands::findConstPoolEntry(unsigned CPI, |
| 611 | const MachineInstr *CPEMI) { |
| 612 | std::vector<CPEntry> &CPEs = CPEntries[CPI]; |
| 613 | // Number of entries per constpool index should be small, just do a |
| 614 | // linear search. |
| 615 | for (unsigned i = 0, e = CPEs.size(); i != e; ++i) { |
| 616 | if (CPEs[i].CPEMI == CPEMI) |
| 617 | return &CPEs[i]; |
| 618 | } |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 619 | return nullptr; |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| Jakob Stoklund Olesen | 17c27a8 | 2011-12-12 18:45:45 +0000 | [diff] [blame] | 622 | /// getCPELogAlign - Returns the required alignment of the constant pool entry |
| Jakob Stoklund Olesen | 0863de4 | 2011-12-12 19:25:51 +0000 | [diff] [blame] | 623 | /// represented by CPEMI. Alignment is measured in log2(bytes) units. |
| Jakob Stoklund Olesen | 17c27a8 | 2011-12-12 18:45:45 +0000 | [diff] [blame] | 624 | unsigned ARMConstantIslands::getCPELogAlign(const MachineInstr *CPEMI) { |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 625 | switch (CPEMI->getOpcode()) { |
| 626 | case ARM::CONSTPOOL_ENTRY: |
| 627 | break; |
| 628 | case ARM::JUMPTABLE_TBB: |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 629 | return isThumb1 ? 2 : 0; |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 630 | case ARM::JUMPTABLE_TBH: |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 631 | return isThumb1 ? 2 : 1; |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 632 | case ARM::JUMPTABLE_INSTS: |
| 633 | return 1; |
| 634 | case ARM::JUMPTABLE_ADDRS: |
| 635 | return 2; |
| 636 | default: |
| 637 | llvm_unreachable("unknown constpool entry kind"); |
| 638 | } |
| Jakob Stoklund Olesen | 17c27a8 | 2011-12-12 18:45:45 +0000 | [diff] [blame] | 639 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 640 | unsigned CPI = getCombinedIndex(CPEMI); |
| Jakob Stoklund Olesen | 17c27a8 | 2011-12-12 18:45:45 +0000 | [diff] [blame] | 641 | assert(CPI < MCP->getConstants().size() && "Invalid constant pool index."); |
| 642 | unsigned Align = MCP->getConstants()[CPI].getAlignment(); |
| 643 | assert(isPowerOf2_32(Align) && "Invalid CPE alignment"); |
| 644 | return Log2_32(Align); |
| 645 | } |
| 646 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 647 | /// scanFunctionJumpTables - Do a scan of the function, building up |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 648 | /// information about the sizes of each block and the locations of all |
| 649 | /// the jump tables. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 650 | void ARMConstantIslands::scanFunctionJumpTables() { |
| Duncan P. N. Exon Smith | 29c5249 | 2016-07-08 20:21:17 +0000 | [diff] [blame] | 651 | for (MachineBasicBlock &MBB : *MF) { |
| 652 | for (MachineInstr &I : MBB) |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 653 | if (I.isBranch() && |
| 654 | (I.getOpcode() == ARM::t2BR_JT || I.getOpcode() == ARM::tBR_JTr)) |
| Duncan P. N. Exon Smith | 29c5249 | 2016-07-08 20:21:17 +0000 | [diff] [blame] | 655 | T2JumpTables.push_back(&I); |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 659 | /// initializeFunctionInfo - Do the initial scan of the function, building up |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 660 | /// information about the sizes of each block, the location of all the water, |
| 661 | /// and finding all of the constant pool users. |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 662 | void ARMConstantIslands:: |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 663 | initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs) { |
| Jakob Stoklund Olesen | 97c8571 | 2011-12-07 04:17:35 +0000 | [diff] [blame] | 664 | |
| Sjoerd Meijer | 5c0ef83 | 2016-07-22 08:39:12 +0000 | [diff] [blame] | 665 | BBInfo = computeAllBlockSizes(MF); |
| Jakob Stoklund Olesen | 2a82333 | 2011-12-08 00:55:02 +0000 | [diff] [blame] | 666 | |
| 667 | // The known bits of the entry block offset are determined by the function |
| 668 | // alignment. |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 669 | BBInfo.front().KnownBits = MF->getAlignment(); |
| Jakob Stoklund Olesen | 2a82333 | 2011-12-08 00:55:02 +0000 | [diff] [blame] | 670 | |
| 671 | // Compute block offsets and known bits. |
| Duncan P. N. Exon Smith | 9f9559e | 2015-10-19 23:25:57 +0000 | [diff] [blame] | 672 | adjustBBOffsetsAfter(&MF->front()); |
| Jakob Stoklund Olesen | 2a82333 | 2011-12-08 00:55:02 +0000 | [diff] [blame] | 673 | |
| Bill Wendling | 18581a4 | 2010-12-21 01:54:40 +0000 | [diff] [blame] | 674 | // Now go back through the instructions and build up our data structures. |
| Duncan P. N. Exon Smith | 29c5249 | 2016-07-08 20:21:17 +0000 | [diff] [blame] | 675 | for (MachineBasicBlock &MBB : *MF) { |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 676 | // If this block doesn't fall through into the next MBB, then this is |
| 677 | // 'water' that a constant pool island could be placed. |
| 678 | if (!BBHasFallthrough(&MBB)) |
| 679 | WaterList.push_back(&MBB); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 680 | |
| Duncan P. N. Exon Smith | 29c5249 | 2016-07-08 20:21:17 +0000 | [diff] [blame] | 681 | for (MachineInstr &I : MBB) { |
| 682 | if (I.isDebugValue()) |
| Jim Grosbach | 97c8a6a | 2010-06-21 17:49:23 +0000 | [diff] [blame] | 683 | continue; |
| Jakob Stoklund Olesen | 97c8571 | 2011-12-07 04:17:35 +0000 | [diff] [blame] | 684 | |
| Duncan P. N. Exon Smith | 29c5249 | 2016-07-08 20:21:17 +0000 | [diff] [blame] | 685 | unsigned Opc = I.getOpcode(); |
| 686 | if (I.isBranch()) { |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 687 | bool isCond = false; |
| 688 | unsigned Bits = 0; |
| 689 | unsigned Scale = 1; |
| 690 | int UOpc = Opc; |
| 691 | switch (Opc) { |
| Evan Cheng | c6d70ae | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 692 | default: |
| 693 | continue; // Ignore other JT branches |
| Evan Cheng | c6d70ae | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 694 | case ARM::t2BR_JT: |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 695 | case ARM::tBR_JTr: |
| Duncan P. N. Exon Smith | 29c5249 | 2016-07-08 20:21:17 +0000 | [diff] [blame] | 696 | T2JumpTables.push_back(&I); |
| Evan Cheng | c6d70ae | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 697 | continue; // Does not get an entry in ImmBranches |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 698 | case ARM::Bcc: |
| 699 | isCond = true; |
| 700 | UOpc = ARM::B; |
| Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 701 | LLVM_FALLTHROUGH; |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 702 | case ARM::B: |
| 703 | Bits = 24; |
| 704 | Scale = 4; |
| 705 | break; |
| 706 | case ARM::tBcc: |
| 707 | isCond = true; |
| 708 | UOpc = ARM::tB; |
| 709 | Bits = 8; |
| 710 | Scale = 2; |
| 711 | break; |
| 712 | case ARM::tB: |
| 713 | Bits = 11; |
| 714 | Scale = 2; |
| 715 | break; |
| David Goodwin | 27303cd | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 716 | case ARM::t2Bcc: |
| 717 | isCond = true; |
| 718 | UOpc = ARM::t2B; |
| 719 | Bits = 20; |
| 720 | Scale = 2; |
| 721 | break; |
| 722 | case ARM::t2B: |
| 723 | Bits = 24; |
| 724 | Scale = 2; |
| 725 | break; |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 726 | } |
| Evan Cheng | f9a4c69 | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 727 | |
| 728 | // Record this immediate branch. |
| Evan Cheng | 36d559d | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 729 | unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale; |
| Duncan P. N. Exon Smith | 29c5249 | 2016-07-08 20:21:17 +0000 | [diff] [blame] | 730 | ImmBranches.push_back(ImmBranch(&I, MaxOffs, isCond, UOpc)); |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 733 | if (Opc == ARM::tPUSH || Opc == ARM::tPOP_RET) |
| Duncan P. N. Exon Smith | 29c5249 | 2016-07-08 20:21:17 +0000 | [diff] [blame] | 734 | PushPopMIs.push_back(&I); |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 735 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 736 | if (Opc == ARM::CONSTPOOL_ENTRY || Opc == ARM::JUMPTABLE_ADDRS || |
| 737 | Opc == ARM::JUMPTABLE_INSTS || Opc == ARM::JUMPTABLE_TBB || |
| 738 | Opc == ARM::JUMPTABLE_TBH) |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 739 | continue; |
| 740 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 741 | // Scan the instructions for constant pool operands. |
| Duncan P. N. Exon Smith | 29c5249 | 2016-07-08 20:21:17 +0000 | [diff] [blame] | 742 | for (unsigned op = 0, e = I.getNumOperands(); op != e; ++op) |
| 743 | if (I.getOperand(op).isCPI() || I.getOperand(op).isJTI()) { |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 744 | // We found one. The addressing mode tells us the max displacement |
| 745 | // from the PC that this instruction permits. |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 746 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 747 | // Basic size info comes from the TSFlags field. |
| Evan Cheng | f9a4c69 | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 748 | unsigned Bits = 0; |
| 749 | unsigned Scale = 1; |
| Evan Cheng | 87aaa19 | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 750 | bool NegOk = false; |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 751 | bool IsSoImm = false; |
| 752 | |
| 753 | switch (Opc) { |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 754 | default: |
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 755 | llvm_unreachable("Unknown addressing mode for CP reference!"); |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 756 | |
| 757 | // Taking the address of a CP entry. |
| 758 | case ARM::LEApcrel: |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 759 | case ARM::LEApcrelJT: |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 760 | // This takes a SoImm, which is 8 bit immediate rotated. We'll |
| 761 | // pretend the maximum offset is 255 * 4. Since each instruction |
| Jim Grosbach | 36a5bf8 | 2009-11-19 18:23:19 +0000 | [diff] [blame] | 762 | // 4 byte wide, this is always correct. We'll check for other |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 763 | // displacements that fits in a SoImm as well. |
| Evan Cheng | f9a4c69 | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 764 | Bits = 8; |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 765 | Scale = 4; |
| 766 | NegOk = true; |
| 767 | IsSoImm = true; |
| 768 | break; |
| Owen Anderson | 9a4d428 | 2010-12-13 22:51:08 +0000 | [diff] [blame] | 769 | case ARM::t2LEApcrel: |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 770 | case ARM::t2LEApcrelJT: |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 771 | Bits = 12; |
| Evan Cheng | 87aaa19 | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 772 | NegOk = true; |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 773 | break; |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 774 | case ARM::tLEApcrel: |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 775 | case ARM::tLEApcrelJT: |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 776 | Bits = 8; |
| 777 | Scale = 4; |
| 778 | break; |
| 779 | |
| David Majnemer | 452f1f9 | 2013-06-04 17:46:15 +0000 | [diff] [blame] | 780 | case ARM::LDRBi12: |
| Jim Grosbach | 1e4d9a1 | 2010-10-26 22:37:02 +0000 | [diff] [blame] | 781 | case ARM::LDRi12: |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 782 | case ARM::LDRcp: |
| Owen Anderson | 4ebf471 | 2011-02-08 22:39:40 +0000 | [diff] [blame] | 783 | case ARM::t2LDRpci: |
| James Molloy | 9abb2fa | 2016-09-26 07:26:24 +0000 | [diff] [blame] | 784 | case ARM::t2LDRHpci: |
| Evan Cheng | fd52299 | 2007-02-01 20:44:52 +0000 | [diff] [blame] | 785 | Bits = 12; // +-offset_12 |
| Evan Cheng | 87aaa19 | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 786 | NegOk = true; |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 787 | break; |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 788 | |
| 789 | case ARM::tLDRpci: |
| Evan Cheng | f9a4c69 | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 790 | Bits = 8; |
| 791 | Scale = 4; // +(offset_8*4) |
| Evan Cheng | 1526ba5 | 2007-01-24 08:53:17 +0000 | [diff] [blame] | 792 | break; |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 793 | |
| Jim Grosbach | d7cf55c | 2009-11-09 00:11:35 +0000 | [diff] [blame] | 794 | case ARM::VLDRD: |
| 795 | case ARM::VLDRS: |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 796 | Bits = 8; |
| 797 | Scale = 4; // +-(offset_8*4) |
| 798 | NegOk = true; |
| Evan Cheng | b23b50d | 2009-06-29 07:51:04 +0000 | [diff] [blame] | 799 | break; |
| James Molloy | 9abb2fa | 2016-09-26 07:26:24 +0000 | [diff] [blame] | 800 | |
| 801 | case ARM::tLDRHi: |
| 802 | Bits = 5; |
| 803 | Scale = 2; // +(offset_5*2) |
| 804 | break; |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 805 | } |
| Evan Cheng | f9a4c69 | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 806 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 807 | // Remember that this is a user of a CP entry. |
| Duncan P. N. Exon Smith | 29c5249 | 2016-07-08 20:21:17 +0000 | [diff] [blame] | 808 | unsigned CPI = I.getOperand(op).getIndex(); |
| 809 | if (I.getOperand(op).isJTI()) { |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 810 | JumpTableUserIndices.insert(std::make_pair(CPI, CPUsers.size())); |
| 811 | CPI = JumpTableEntryIndices[CPI]; |
| 812 | } |
| 813 | |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 814 | MachineInstr *CPEMI = CPEMIs[CPI]; |
| Evan Cheng | e41903b | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 815 | unsigned MaxOffs = ((1 << Bits)-1) * Scale; |
| Duncan P. N. Exon Smith | 29c5249 | 2016-07-08 20:21:17 +0000 | [diff] [blame] | 816 | CPUsers.push_back(CPUser(&I, CPEMI, MaxOffs, NegOk, IsSoImm)); |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 817 | |
| 818 | // Increment corresponding CPEntry reference count. |
| 819 | CPEntry *CPE = findConstPoolEntry(CPI, CPEMI); |
| 820 | assert(CPE && "Cannot find a corresponding CPEntry!"); |
| 821 | CPE->RefCount++; |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 822 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 823 | // Instructions can only use one CP entry, don't bother scanning the |
| 824 | // rest of the operands. |
| 825 | break; |
| 826 | } |
| 827 | } |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 831 | /// getOffsetOf - Return the current offset of the specified machine instruction |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 832 | /// from the start of the function. This offset changes as stuff is moved |
| 833 | /// around inside the function. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 834 | unsigned ARMConstantIslands::getOffsetOf(MachineInstr *MI) const { |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 835 | MachineBasicBlock *MBB = MI->getParent(); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 836 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 837 | // The offset is composed of two things: the sum of the sizes of all MBB's |
| 838 | // before this instruction's block, and the offset from the start of the block |
| 839 | // it is in. |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 840 | unsigned Offset = BBInfo[MBB->getNumber()].Offset; |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 841 | |
| 842 | // Sum instructions before MI in MBB. |
| Jim Grosbach | 44091c2 | 2012-01-31 20:56:55 +0000 | [diff] [blame] | 843 | for (MachineBasicBlock::iterator I = MBB->begin(); &*I != MI; ++I) { |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 844 | assert(I != MBB->end() && "Didn't find MI in its own basic block?"); |
| Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 845 | Offset += TII->getInstSizeInBytes(*I); |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 846 | } |
| Jim Grosbach | 44091c2 | 2012-01-31 20:56:55 +0000 | [diff] [blame] | 847 | return Offset; |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | /// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB |
| 851 | /// ID. |
| 852 | static bool CompareMBBNumbers(const MachineBasicBlock *LHS, |
| 853 | const MachineBasicBlock *RHS) { |
| 854 | return LHS->getNumber() < RHS->getNumber(); |
| 855 | } |
| 856 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 857 | /// updateForInsertedWaterBlock - When a block is newly inserted into the |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 858 | /// machine function, it upsets all of the block numbers. Renumber the blocks |
| 859 | /// and update the arrays that parallel this numbering. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 860 | void ARMConstantIslands::updateForInsertedWaterBlock(MachineBasicBlock *NewBB) { |
| Duncan Sands | 75b5d27 | 2011-02-15 09:23:02 +0000 | [diff] [blame] | 861 | // Renumber the MBB's to keep them consecutive. |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 862 | NewBB->getParent()->RenumberBlocks(NewBB); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 863 | |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 864 | // Insert an entry into BBInfo to align it properly with the (newly |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 865 | // renumbered) block numbers. |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 866 | BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo()); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 867 | |
| 868 | // Next, update WaterList. Specifically, we need to add NewMBB as having |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 869 | // available water after it. |
| Bob Wilson | c7a3cf4 | 2009-10-12 18:52:13 +0000 | [diff] [blame] | 870 | water_iterator IP = |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 871 | std::lower_bound(WaterList.begin(), WaterList.end(), NewBB, |
| 872 | CompareMBBNumbers); |
| 873 | WaterList.insert(IP, NewBB); |
| 874 | } |
| 875 | |
| 876 | |
| 877 | /// Split the basic block containing MI into two blocks, which are joined by |
| Bob Wilson | 2f9be50 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 878 | /// an unconditional branch. Update data structures and renumber blocks to |
| Evan Cheng | 345877e | 2007-01-31 02:22:22 +0000 | [diff] [blame] | 879 | /// account for this change and returns the newly created block. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 880 | MachineBasicBlock *ARMConstantIslands::splitBlockBeforeInstr(MachineInstr *MI) { |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 881 | MachineBasicBlock *OrigBB = MI->getParent(); |
| 882 | |
| 883 | // Create a new MBB for the code after the OrigBB. |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 884 | MachineBasicBlock *NewBB = |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 885 | MF->CreateMachineBasicBlock(OrigBB->getBasicBlock()); |
| Duncan P. N. Exon Smith | 9f9559e | 2015-10-19 23:25:57 +0000 | [diff] [blame] | 886 | MachineFunction::iterator MBBI = ++OrigBB->getIterator(); |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 887 | MF->insert(MBBI, NewBB); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 888 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 889 | // Splice the instructions starting with MI over to NewBB. |
| 890 | NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end()); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 891 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 892 | // Add an unconditional branch from OrigBB to NewBB. |
| Evan Cheng | 7169bd8 | 2007-01-31 18:29:27 +0000 | [diff] [blame] | 893 | // Note the new unconditional branch is not being recorded. |
| Dale Johannesen | 7647da6 | 2009-02-13 02:25:56 +0000 | [diff] [blame] | 894 | // There doesn't seem to be meaningful DebugInfo available; this doesn't |
| 895 | // correspond to anything in the source. |
| Evan Cheng | 7c94343 | 2009-07-07 01:16:41 +0000 | [diff] [blame] | 896 | unsigned Opc = isThumb ? (isThumb2 ? ARM::t2B : ARM::tB) : ARM::B; |
| Owen Anderson | 29cfe6c | 2011-09-09 21:48:23 +0000 | [diff] [blame] | 897 | if (!isThumb) |
| 898 | BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB); |
| 899 | else |
| 900 | BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB) |
| 901 | .addImm(ARMCC::AL).addReg(0); |
| Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 902 | ++NumSplit; |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 903 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 904 | // Update the CFG. All succs of OrigBB are now succs of NewBB. |
| Jakob Stoklund Olesen | 2608157 | 2011-12-06 00:51:12 +0000 | [diff] [blame] | 905 | NewBB->transferSuccessors(OrigBB); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 906 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 907 | // OrigBB branches to NewBB. |
| 908 | OrigBB->addSuccessor(NewBB); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 909 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 910 | // Update internal data structures to account for the newly inserted MBB. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 911 | // This is almost the same as updateForInsertedWaterBlock, except that |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 912 | // the Water goes after OrigBB, not NewBB. |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 913 | MF->RenumberBlocks(NewBB); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 914 | |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 915 | // Insert an entry into BBInfo to align it properly with the (newly |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 916 | // renumbered) block numbers. |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 917 | BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo()); |
| Dale Johannesen | 01ee575 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 918 | |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 919 | // Next, update WaterList. Specifically, we need to add OrigMBB as having |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 920 | // available water after it (but not if it's already there, which happens |
| 921 | // when splitting before a conditional branch that is followed by an |
| 922 | // unconditional branch - in that case we want to insert NewBB). |
| Bob Wilson | c7a3cf4 | 2009-10-12 18:52:13 +0000 | [diff] [blame] | 923 | water_iterator IP = |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 924 | std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB, |
| 925 | CompareMBBNumbers); |
| 926 | MachineBasicBlock* WaterBB = *IP; |
| 927 | if (WaterBB == OrigBB) |
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 928 | WaterList.insert(std::next(IP), NewBB); |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 929 | else |
| 930 | WaterList.insert(IP, OrigBB); |
| Bob Wilson | 2f9be50 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 931 | NewWaterList.insert(OrigBB); |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 932 | |
| Dale Johannesen | c17dd57 | 2010-07-23 22:50:23 +0000 | [diff] [blame] | 933 | // Figure out how large the OrigBB is. As the first half of the original |
| 934 | // block, it cannot contain a tablejump. The size includes |
| 935 | // the new jump we added. (It should be possible to do this without |
| 936 | // recounting everything, but it's very confusing, and this is rarely |
| 937 | // executed.) |
| Sjoerd Meijer | 5c0ef83 | 2016-07-22 08:39:12 +0000 | [diff] [blame] | 938 | computeBlockSize(MF, OrigBB, BBInfo[OrigBB->getNumber()]); |
| Dale Johannesen | 01ee575 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 939 | |
| Dale Johannesen | c17dd57 | 2010-07-23 22:50:23 +0000 | [diff] [blame] | 940 | // Figure out how large the NewMBB is. As the second half of the original |
| 941 | // block, it may contain a tablejump. |
| Sjoerd Meijer | 5c0ef83 | 2016-07-22 08:39:12 +0000 | [diff] [blame] | 942 | computeBlockSize(MF, NewBB, BBInfo[NewBB->getNumber()]); |
| Dale Johannesen | c17dd57 | 2010-07-23 22:50:23 +0000 | [diff] [blame] | 943 | |
| Dale Johannesen | 01ee575 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 944 | // All BBOffsets following these blocks must be modified. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 945 | adjustBBOffsetsAfter(OrigBB); |
| Evan Cheng | 345877e | 2007-01-31 02:22:22 +0000 | [diff] [blame] | 946 | |
| 947 | return NewBB; |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 950 | /// getUserOffset - Compute the offset of U.MI as seen by the hardware |
| Jakob Stoklund Olesen | f09a316 | 2012-01-10 01:34:59 +0000 | [diff] [blame] | 951 | /// displacement computation. Update U.KnownAlignment to match its current |
| 952 | /// basic block location. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 953 | unsigned ARMConstantIslands::getUserOffset(CPUser &U) const { |
| 954 | unsigned UserOffset = getOffsetOf(U.MI); |
| Jakob Stoklund Olesen | f09a316 | 2012-01-10 01:34:59 +0000 | [diff] [blame] | 955 | const BasicBlockInfo &BBI = BBInfo[U.MI->getParent()->getNumber()]; |
| 956 | unsigned KnownBits = BBI.internalKnownBits(); |
| 957 | |
| 958 | // The value read from PC is offset from the actual instruction address. |
| 959 | UserOffset += (isThumb ? 4 : 8); |
| 960 | |
| 961 | // Because of inline assembly, we may not know the alignment (mod 4) of U.MI. |
| 962 | // Make sure U.getMaxDisp() returns a constrained range. |
| 963 | U.KnownAlignment = (KnownBits >= 2); |
| 964 | |
| 965 | // On Thumb, offsets==2 mod 4 are rounded down by the hardware for |
| 966 | // purposes of the displacement computation; compensate for that here. |
| 967 | // For unknown alignments, getMaxDisp() constrains the range instead. |
| 968 | if (isThumb && U.KnownAlignment) |
| 969 | UserOffset &= ~3u; |
| 970 | |
| 971 | return UserOffset; |
| 972 | } |
| 973 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 974 | /// isOffsetInRange - Checks whether UserOffset (the location of a constant pool |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 975 | /// reference) is within MaxDisp of TrialOffset (a proposed location of a |
| Dale Johannesen | 4a00cf3 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 976 | /// constant pool entry). |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 977 | /// UserOffset is computed by getUserOffset above to include PC adjustments. If |
| Jakob Stoklund Olesen | f09a316 | 2012-01-10 01:34:59 +0000 | [diff] [blame] | 978 | /// the mod 4 alignment of UserOffset is not known, the uncertainty must be |
| 979 | /// subtracted from MaxDisp instead. CPUser::getMaxDisp() does that. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 980 | bool ARMConstantIslands::isOffsetInRange(unsigned UserOffset, |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 981 | unsigned TrialOffset, unsigned MaxDisp, |
| 982 | bool NegativeOK, bool IsSoImm) { |
| Dale Johannesen | 01ee575 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 983 | if (UserOffset <= TrialOffset) { |
| 984 | // User before the Trial. |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 985 | if (TrialOffset - UserOffset <= MaxDisp) |
| 986 | return true; |
| Evan Cheng | c26c76e | 2009-07-24 19:31:03 +0000 | [diff] [blame] | 987 | // FIXME: Make use full range of soimm values. |
| Dale Johannesen | 01ee575 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 988 | } else if (NegativeOK) { |
| Evan Cheng | d2919a1 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 989 | if (UserOffset - TrialOffset <= MaxDisp) |
| 990 | return true; |
| Evan Cheng | c26c76e | 2009-07-24 19:31:03 +0000 | [diff] [blame] | 991 | // FIXME: Make use full range of soimm values. |
| Dale Johannesen | 01ee575 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 992 | } |
| 993 | return false; |
| 994 | } |
| 995 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 996 | /// isWaterInRange - Returns true if a CPE placed after the specified |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 997 | /// Water (a basic block) will be in range for the specific MI. |
| Jakob Stoklund Olesen | bfa576f | 2011-12-13 00:44:30 +0000 | [diff] [blame] | 998 | /// |
| 999 | /// Compute how much the function will grow by inserting a CPE after Water. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1000 | bool ARMConstantIslands::isWaterInRange(unsigned UserOffset, |
| Jakob Stoklund Olesen | bfa576f | 2011-12-13 00:44:30 +0000 | [diff] [blame] | 1001 | MachineBasicBlock* Water, CPUser &U, |
| 1002 | unsigned &Growth) { |
| 1003 | unsigned CPELogAlign = getCPELogAlign(U.CPEMI); |
| 1004 | unsigned CPEOffset = BBInfo[Water->getNumber()].postOffset(CPELogAlign); |
| 1005 | unsigned NextBlockOffset, NextBlockAlignment; |
| Duncan P. N. Exon Smith | 9f9559e | 2015-10-19 23:25:57 +0000 | [diff] [blame] | 1006 | MachineFunction::const_iterator NextBlock = Water->getIterator(); |
| Jakob Stoklund Olesen | bfa576f | 2011-12-13 00:44:30 +0000 | [diff] [blame] | 1007 | if (++NextBlock == MF->end()) { |
| 1008 | NextBlockOffset = BBInfo[Water->getNumber()].postOffset(); |
| 1009 | NextBlockAlignment = 0; |
| 1010 | } else { |
| 1011 | NextBlockOffset = BBInfo[NextBlock->getNumber()].Offset; |
| 1012 | NextBlockAlignment = NextBlock->getAlignment(); |
| 1013 | } |
| 1014 | unsigned Size = U.CPEMI->getOperand(2).getImm(); |
| 1015 | unsigned CPEEnd = CPEOffset + Size; |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1016 | |
| Jakob Stoklund Olesen | bfa576f | 2011-12-13 00:44:30 +0000 | [diff] [blame] | 1017 | // The CPE may be able to hide in the alignment padding before the next |
| 1018 | // block. It may also cause more padding to be required if it is more aligned |
| 1019 | // that the next block. |
| 1020 | if (CPEEnd > NextBlockOffset) { |
| 1021 | Growth = CPEEnd - NextBlockOffset; |
| 1022 | // Compute the padding that would go at the end of the CPE to align the next |
| 1023 | // block. |
| Aaron Ballman | ef0fe1e | 2016-03-30 21:30:00 +0000 | [diff] [blame] | 1024 | Growth += OffsetToAlignment(CPEEnd, 1ULL << NextBlockAlignment); |
| Jakob Stoklund Olesen | bfa576f | 2011-12-13 00:44:30 +0000 | [diff] [blame] | 1025 | |
| 1026 | // If the CPE is to be inserted before the instruction, that will raise |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1027 | // the offset of the instruction. Also account for unknown alignment padding |
| Jakob Stoklund Olesen | bfa576f | 2011-12-13 00:44:30 +0000 | [diff] [blame] | 1028 | // in blocks between CPE and the user. |
| 1029 | if (CPEOffset < UserOffset) |
| 1030 | UserOffset += Growth + UnknownPadding(MF->getAlignment(), CPELogAlign); |
| 1031 | } else |
| 1032 | // CPE fits in existing padding. |
| 1033 | Growth = 0; |
| Dale Johannesen | d13786d | 2007-04-02 20:31:06 +0000 | [diff] [blame] | 1034 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1035 | return isOffsetInRange(UserOffset, CPEOffset, U); |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1038 | /// isCPEntryInRange - Returns true if the distance between specific MI and |
| Evan Cheng | 1f3fc4b | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 1039 | /// specific ConstPool entry instruction can fit in MI's displacement field. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1040 | bool ARMConstantIslands::isCPEntryInRange(MachineInstr *MI, unsigned UserOffset, |
| Evan Cheng | 87aaa19 | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 1041 | MachineInstr *CPEMI, unsigned MaxDisp, |
| 1042 | bool NegOk, bool DoDump) { |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1043 | unsigned CPEOffset = getOffsetOf(CPEMI); |
| Evan Cheng | 234e031 | 2007-02-01 01:09:47 +0000 | [diff] [blame] | 1044 | |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1045 | if (DoDump) { |
| Jakob Stoklund Olesen | 5f5fa12 | 2011-12-09 18:20:35 +0000 | [diff] [blame] | 1046 | DEBUG({ |
| 1047 | unsigned Block = MI->getParent()->getNumber(); |
| 1048 | const BasicBlockInfo &BBI = BBInfo[Block]; |
| 1049 | dbgs() << "User of CPE#" << CPEMI->getOperand(0).getImm() |
| 1050 | << " max delta=" << MaxDisp |
| Jakob Stoklund Olesen | b373452 | 2011-12-10 02:55:06 +0000 | [diff] [blame] | 1051 | << format(" insn address=%#x", UserOffset) |
| Jakob Stoklund Olesen | 5f5fa12 | 2011-12-09 18:20:35 +0000 | [diff] [blame] | 1052 | << " in BB#" << Block << ": " |
| Jakob Stoklund Olesen | b373452 | 2011-12-10 02:55:06 +0000 | [diff] [blame] | 1053 | << format("%#x-%x\t", BBI.Offset, BBI.postOffset()) << *MI |
| 1054 | << format("CPE address=%#x offset=%+d: ", CPEOffset, |
| 1055 | int(CPEOffset-UserOffset)); |
| Jakob Stoklund Olesen | 5f5fa12 | 2011-12-09 18:20:35 +0000 | [diff] [blame] | 1056 | }); |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1057 | } |
| Evan Cheng | 1f3fc4b | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 1058 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1059 | return isOffsetInRange(UserOffset, CPEOffset, MaxDisp, NegOk); |
| Evan Cheng | 1f3fc4b | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| Evan Cheng | e451097 | 2009-01-28 00:53:34 +0000 | [diff] [blame] | 1062 | #ifndef NDEBUG |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 1063 | /// BBIsJumpedOver - Return true of the specified basic block's only predecessor |
| 1064 | /// unconditionally branches to its only successor. |
| 1065 | static bool BBIsJumpedOver(MachineBasicBlock *MBB) { |
| 1066 | if (MBB->pred_size() != 1 || MBB->succ_size() != 1) |
| 1067 | return false; |
| 1068 | |
| 1069 | MachineBasicBlock *Succ = *MBB->succ_begin(); |
| 1070 | MachineBasicBlock *Pred = *MBB->pred_begin(); |
| 1071 | MachineInstr *PredMI = &Pred->back(); |
| David Goodwin | 27303cd | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 1072 | if (PredMI->getOpcode() == ARM::B || PredMI->getOpcode() == ARM::tB |
| 1073 | || PredMI->getOpcode() == ARM::t2B) |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 1074 | return PredMI->getOperand(0).getMBB() == Succ; |
| 1075 | return false; |
| 1076 | } |
| Evan Cheng | e451097 | 2009-01-28 00:53:34 +0000 | [diff] [blame] | 1077 | #endif // NDEBUG |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 1078 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1079 | void ARMConstantIslands::adjustBBOffsetsAfter(MachineBasicBlock *BB) { |
| Jakob Stoklund Olesen | 6905111 | 2012-01-06 21:40:15 +0000 | [diff] [blame] | 1080 | unsigned BBNum = BB->getNumber(); |
| 1081 | for(unsigned i = BBNum + 1, e = MF->getNumBlockIDs(); i < e; ++i) { |
| Jakob Stoklund Olesen | 2a82333 | 2011-12-08 00:55:02 +0000 | [diff] [blame] | 1082 | // Get the offset and known bits at the end of the layout predecessor. |
| Jakob Stoklund Olesen | 91a7bcb | 2011-12-12 19:25:54 +0000 | [diff] [blame] | 1083 | // Include the alignment of the current block. |
| 1084 | unsigned LogAlign = MF->getBlockNumbered(i)->getAlignment(); |
| 1085 | unsigned Offset = BBInfo[i - 1].postOffset(LogAlign); |
| 1086 | unsigned KnownBits = BBInfo[i - 1].postKnownBits(LogAlign); |
| Jakob Stoklund Olesen | 2a82333 | 2011-12-08 00:55:02 +0000 | [diff] [blame] | 1087 | |
| Jakob Stoklund Olesen | 6905111 | 2012-01-06 21:40:15 +0000 | [diff] [blame] | 1088 | // This is where block i begins. Stop if the offset is already correct, |
| 1089 | // and we have updated 2 blocks. This is the maximum number of blocks |
| 1090 | // changed before calling this function. |
| 1091 | if (i > BBNum + 2 && |
| 1092 | BBInfo[i].Offset == Offset && |
| 1093 | BBInfo[i].KnownBits == KnownBits) |
| 1094 | break; |
| 1095 | |
| Jakob Stoklund Olesen | 2a82333 | 2011-12-08 00:55:02 +0000 | [diff] [blame] | 1096 | BBInfo[i].Offset = Offset; |
| 1097 | BBInfo[i].KnownBits = KnownBits; |
| Dale Johannesen | 4a00cf3 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1098 | } |
| Dale Johannesen | 01ee575 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1101 | /// decrementCPEReferenceCount - find the constant pool entry with index CPI |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1102 | /// and instruction CPEMI, and decrement its refcount. If the refcount |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1103 | /// becomes 0 remove the entry and instruction. Returns true if we removed |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1104 | /// the entry, false if we didn't. |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1105 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1106 | bool ARMConstantIslands::decrementCPEReferenceCount(unsigned CPI, |
| 1107 | MachineInstr *CPEMI) { |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 1108 | // Find the old entry. Eliminate it if it is no longer used. |
| Evan Cheng | 3c68d4e | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1109 | CPEntry *CPE = findConstPoolEntry(CPI, CPEMI); |
| 1110 | assert(CPE && "Unexpected!"); |
| 1111 | if (--CPE->RefCount == 0) { |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1112 | removeDeadCPEMI(CPEMI); |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1113 | CPE->CPEMI = nullptr; |
| Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1114 | --NumCPEs; |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1115 | return true; |
| 1116 | } |
| 1117 | return false; |
| 1118 | } |
| 1119 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1120 | unsigned ARMConstantIslands::getCombinedIndex(const MachineInstr *CPEMI) { |
| 1121 | if (CPEMI->getOperand(1).isCPI()) |
| 1122 | return CPEMI->getOperand(1).getIndex(); |
| 1123 | |
| 1124 | return JumpTableEntryIndices[CPEMI->getOperand(1).getIndex()]; |
| 1125 | } |
| 1126 | |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1127 | /// LookForCPEntryInRange - see if the currently referenced CPE is in range; |
| 1128 | /// if not, see if an in-range clone of the CPE is in range, and if so, |
| 1129 | /// change the data structures so the user references the clone. Returns: |
| 1130 | /// 0 = no existing entry found |
| 1131 | /// 1 = entry found, and there were no code insertions or deletions |
| 1132 | /// 2 = entry found, and there were code insertions or deletions |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1133 | int ARMConstantIslands::findInRangeCPEntry(CPUser& U, unsigned UserOffset) |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1134 | { |
| 1135 | MachineInstr *UserMI = U.MI; |
| 1136 | MachineInstr *CPEMI = U.CPEMI; |
| 1137 | |
| 1138 | // Check to see if the CPE is already in-range. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1139 | if (isCPEntryInRange(UserMI, UserOffset, CPEMI, U.getMaxDisp(), U.NegOk, |
| 1140 | true)) { |
| Jakob Stoklund Olesen | 5f5fa12 | 2011-12-09 18:20:35 +0000 | [diff] [blame] | 1141 | DEBUG(dbgs() << "In range\n"); |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1142 | return 1; |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1145 | // No. Look for previously created clones of the CPE that are in range. |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1146 | unsigned CPI = getCombinedIndex(CPEMI); |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1147 | std::vector<CPEntry> &CPEs = CPEntries[CPI]; |
| 1148 | for (unsigned i = 0, e = CPEs.size(); i != e; ++i) { |
| 1149 | // We already tried this one |
| 1150 | if (CPEs[i].CPEMI == CPEMI) |
| 1151 | continue; |
| 1152 | // Removing CPEs can leave empty entries, skip |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1153 | if (CPEs[i].CPEMI == nullptr) |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1154 | continue; |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1155 | if (isCPEntryInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.getMaxDisp(), |
| Jakob Stoklund Olesen | f09a316 | 2012-01-10 01:34:59 +0000 | [diff] [blame] | 1156 | U.NegOk)) { |
| Jakob Stoklund Olesen | 5f5fa12 | 2011-12-09 18:20:35 +0000 | [diff] [blame] | 1157 | DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#" |
| Chris Lattner | af29ea6 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 1158 | << CPEs[i].CPI << "\n"); |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1159 | // Point the CPUser node to the replacement |
| 1160 | U.CPEMI = CPEs[i].CPEMI; |
| 1161 | // Change the CPI in the instruction operand to refer to the clone. |
| 1162 | for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j) |
| Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1163 | if (UserMI->getOperand(j).isCPI()) { |
| Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1164 | UserMI->getOperand(j).setIndex(CPEs[i].CPI); |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1165 | break; |
| 1166 | } |
| 1167 | // Adjust the refcount of the clone... |
| 1168 | CPEs[i].RefCount++; |
| 1169 | // ...and the original. If we didn't remove the old entry, none of the |
| 1170 | // addresses changed, so we don't need another pass. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1171 | return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1; |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1172 | } |
| 1173 | } |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
| Dale Johannesen | 440995b | 2007-02-28 18:41:23 +0000 | [diff] [blame] | 1177 | /// getUnconditionalBrDisp - Returns the maximum displacement that can fit in |
| 1178 | /// the specific unconditional branch instruction. |
| 1179 | static inline unsigned getUnconditionalBrDisp(int Opc) { |
| David Goodwin | 27303cd | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 1180 | switch (Opc) { |
| 1181 | case ARM::tB: |
| 1182 | return ((1<<10)-1)*2; |
| 1183 | case ARM::t2B: |
| 1184 | return ((1<<23)-1)*2; |
| 1185 | default: |
| 1186 | break; |
| 1187 | } |
| Jim Grosbach | f24f9d9 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 1188 | |
| David Goodwin | 27303cd | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 1189 | return ((1<<23)-1)*4; |
| Dale Johannesen | 440995b | 2007-02-28 18:41:23 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1192 | /// findAvailableWater - Look for an existing entry in the WaterList in which |
| Dale Johannesen | 962fa8e | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1193 | /// we can place the CPE referenced from U so it's within range of U's MI. |
| Bob Wilson | 2f9be50 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1194 | /// Returns true if found, false if not. If it returns true, WaterIter |
| Bob Wilson | cc121aa | 2009-10-12 21:23:15 +0000 | [diff] [blame] | 1195 | /// is set to the WaterList entry. For Thumb, prefer water that will not |
| 1196 | /// introduce padding to water that will. To ensure that this pass |
| 1197 | /// terminates, the CPE location for a particular CPUser is only allowed to |
| 1198 | /// move to a lower address, so search backward from the end of the list and |
| 1199 | /// prefer the first water that is in range. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1200 | bool ARMConstantIslands::findAvailableWater(CPUser &U, unsigned UserOffset, |
| Weiming Zhao | 5e0c3eb | 2016-02-23 18:39:19 +0000 | [diff] [blame] | 1201 | water_iterator &WaterIter, |
| 1202 | bool CloserWater) { |
| Bob Wilson | 3a7326e | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1203 | if (WaterList.empty()) |
| 1204 | return false; |
| 1205 | |
| Jakob Stoklund Olesen | bfa576f | 2011-12-13 00:44:30 +0000 | [diff] [blame] | 1206 | unsigned BestGrowth = ~0u; |
| Weiming Zhao | 5e0c3eb | 2016-02-23 18:39:19 +0000 | [diff] [blame] | 1207 | // The nearest water without splitting the UserBB is right after it. |
| 1208 | // If the distance is still large (we have a big BB), then we need to split it |
| 1209 | // if we don't converge after certain iterations. This helps the following |
| 1210 | // situation to converge: |
| 1211 | // BB0: |
| 1212 | // Big BB |
| 1213 | // BB1: |
| 1214 | // Constant Pool |
| 1215 | // When a CP access is out of range, BB0 may be used as water. However, |
| 1216 | // inserting islands between BB0 and BB1 makes other accesses out of range. |
| 1217 | MachineBasicBlock *UserBB = U.MI->getParent(); |
| 1218 | unsigned MinNoSplitDisp = |
| 1219 | BBInfo[UserBB->getNumber()].postOffset(getCPELogAlign(U.CPEMI)); |
| 1220 | if (CloserWater && MinNoSplitDisp > U.getMaxDisp() / 2) |
| 1221 | return false; |
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1222 | for (water_iterator IP = std::prev(WaterList.end()), B = WaterList.begin();; |
| Jakob Stoklund Olesen | bfa576f | 2011-12-13 00:44:30 +0000 | [diff] [blame] | 1223 | --IP) { |
| Bob Wilson | 3a7326e | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1224 | MachineBasicBlock* WaterBB = *IP; |
| Bob Wilson | 2f9be50 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1225 | // Check if water is in range and is either at a lower address than the |
| 1226 | // current "high water mark" or a new water block that was created since |
| 1227 | // the previous iteration by inserting an unconditional branch. In the |
| 1228 | // latter case, we want to allow resetting the high water mark back to |
| 1229 | // this new water since we haven't seen it before. Inserting branches |
| 1230 | // should be relatively uncommon and when it does happen, we want to be |
| 1231 | // sure to take advantage of it for all the CPEs near that block, so that |
| 1232 | // we don't insert more branches than necessary. |
| Weiming Zhao | 5e0c3eb | 2016-02-23 18:39:19 +0000 | [diff] [blame] | 1233 | // When CloserWater is true, we try to find the lowest address after (or |
| 1234 | // equal to) user MI's BB no matter of padding growth. |
| Jakob Stoklund Olesen | bfa576f | 2011-12-13 00:44:30 +0000 | [diff] [blame] | 1235 | unsigned Growth; |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1236 | if (isWaterInRange(UserOffset, WaterBB, U, Growth) && |
| Bob Wilson | 2f9be50 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1237 | (WaterBB->getNumber() < U.HighWaterMark->getNumber() || |
| Tim Northover | 631cc9c | 2014-11-13 17:58:53 +0000 | [diff] [blame] | 1238 | NewWaterList.count(WaterBB) || WaterBB == U.MI->getParent()) && |
| 1239 | Growth < BestGrowth) { |
| Jakob Stoklund Olesen | bfa576f | 2011-12-13 00:44:30 +0000 | [diff] [blame] | 1240 | // This is the least amount of required padding seen so far. |
| 1241 | BestGrowth = Growth; |
| 1242 | WaterIter = IP; |
| 1243 | DEBUG(dbgs() << "Found water after BB#" << WaterBB->getNumber() |
| 1244 | << " Growth=" << Growth << '\n'); |
| 1245 | |
| Weiming Zhao | 5e0c3eb | 2016-02-23 18:39:19 +0000 | [diff] [blame] | 1246 | if (CloserWater && WaterBB == U.MI->getParent()) |
| 1247 | return true; |
| 1248 | // Keep looking unless it is perfect and we're not looking for the lowest |
| 1249 | // possible address. |
| 1250 | if (!CloserWater && BestGrowth == 0) |
| Bob Wilson | 3a7326e | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1251 | return true; |
| Dale Johannesen | 962fa8e | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1252 | } |
| Bob Wilson | 3a7326e | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1253 | if (IP == B) |
| 1254 | break; |
| Dale Johannesen | 962fa8e | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1255 | } |
| Jakob Stoklund Olesen | bfa576f | 2011-12-13 00:44:30 +0000 | [diff] [blame] | 1256 | return BestGrowth != ~0u; |
| Dale Johannesen | 962fa8e | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1259 | /// createNewWater - No existing WaterList entry will work for |
| Dale Johannesen | 962fa8e | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1260 | /// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the |
| 1261 | /// block is used if in range, and the conditional branch munged so control |
| 1262 | /// flow is correct. Otherwise the block is split to create a hole with an |
| Bob Wilson | 3250e77 | 2009-10-12 21:39:43 +0000 | [diff] [blame] | 1263 | /// unconditional branch around it. In either case NewMBB is set to a |
| Dale Johannesen | 962fa8e | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1264 | /// block following which the new island can be inserted (the WaterList |
| 1265 | /// is not adjusted). |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1266 | void ARMConstantIslands::createNewWater(unsigned CPUserIndex, |
| Bob Wilson | 3250e77 | 2009-10-12 21:39:43 +0000 | [diff] [blame] | 1267 | unsigned UserOffset, |
| 1268 | MachineBasicBlock *&NewMBB) { |
| Dale Johannesen | 962fa8e | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1269 | CPUser &U = CPUsers[CPUserIndex]; |
| 1270 | MachineInstr *UserMI = U.MI; |
| 1271 | MachineInstr *CPEMI = U.CPEMI; |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1272 | unsigned CPELogAlign = getCPELogAlign(CPEMI); |
| Dale Johannesen | 962fa8e | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1273 | MachineBasicBlock *UserMBB = UserMI->getParent(); |
| Jakob Stoklund Olesen | 146ac7b | 2011-12-10 02:55:10 +0000 | [diff] [blame] | 1274 | const BasicBlockInfo &UserBBI = BBInfo[UserMBB->getNumber()]; |
| Dale Johannesen | 962fa8e | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1275 | |
| Bob Wilson | b4f2a85 | 2009-10-15 05:10:36 +0000 | [diff] [blame] | 1276 | // If the block does not end in an unconditional branch already, and if the |
| 1277 | // end of the block is within range, make new water there. (The addition |
| 1278 | // below is for the unconditional branch we will be adding: 4 bytes on ARM + |
| Jakob Stoklund Olesen | f09a316 | 2012-01-10 01:34:59 +0000 | [diff] [blame] | 1279 | // Thumb2, 2 on Thumb1. |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1280 | if (BBHasFallthrough(UserMBB)) { |
| 1281 | // Size of branch to insert. |
| 1282 | unsigned Delta = isThumb1 ? 2 : 4; |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1283 | // Compute the offset where the CPE will begin. |
| Jakob Stoklund Olesen | 5f0d1b4 | 2012-04-27 22:58:38 +0000 | [diff] [blame] | 1284 | unsigned CPEOffset = UserBBI.postOffset(CPELogAlign) + Delta; |
| Dale Johannesen | 4a00cf3 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1285 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1286 | if (isOffsetInRange(UserOffset, CPEOffset, U)) { |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1287 | DEBUG(dbgs() << "Split at end of BB#" << UserMBB->getNumber() |
| 1288 | << format(", expected CPE offset %#x\n", CPEOffset)); |
| Duncan P. N. Exon Smith | 9f9559e | 2015-10-19 23:25:57 +0000 | [diff] [blame] | 1289 | NewMBB = &*++UserMBB->getIterator(); |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1290 | // Add an unconditional branch from UserMBB to fallthrough block. Record |
| 1291 | // it for branch lengthening; this new branch will not get out of range, |
| 1292 | // but if the preceding conditional branch is out of range, the targets |
| 1293 | // will be exchanged, and the altered branch may be out of range, so the |
| 1294 | // machinery has to know about it. |
| 1295 | int UncondBr = isThumb ? ((isThumb2) ? ARM::t2B : ARM::tB) : ARM::B; |
| 1296 | if (!isThumb) |
| 1297 | BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB); |
| 1298 | else |
| 1299 | BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB) |
| 1300 | .addImm(ARMCC::AL).addReg(0); |
| 1301 | unsigned MaxDisp = getUnconditionalBrDisp(UncondBr); |
| 1302 | ImmBranches.push_back(ImmBranch(&UserMBB->back(), |
| 1303 | MaxDisp, false, UncondBr)); |
| Sjoerd Meijer | 5c0ef83 | 2016-07-22 08:39:12 +0000 | [diff] [blame] | 1304 | computeBlockSize(MF, UserMBB, BBInfo[UserMBB->getNumber()]); |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1305 | adjustBBOffsetsAfter(UserMBB); |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1306 | return; |
| Dale Johannesen | 962fa8e | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1307 | } |
| Dale Johannesen | 962fa8e | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1308 | } |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1309 | |
| 1310 | // What a big block. Find a place within the block to split it. This is a |
| 1311 | // little tricky on Thumb1 since instructions are 2 bytes and constant pool |
| 1312 | // entries are 4 bytes: if instruction I references island CPE, and |
| 1313 | // instruction I+1 references CPE', it will not work well to put CPE as far |
| 1314 | // forward as possible, since then CPE' cannot immediately follow it (that |
| 1315 | // location is 2 bytes farther away from I+1 than CPE was from I) and we'd |
| 1316 | // need to create a new island. So, we make a first guess, then walk through |
| 1317 | // the instructions between the one currently being looked at and the |
| 1318 | // possible insertion point, and make sure any other instructions that |
| 1319 | // reference CPEs will be able to use the same island area; if not, we back |
| 1320 | // up the insertion point. |
| 1321 | |
| 1322 | // Try to split the block so it's fully aligned. Compute the latest split |
| Jakob Stoklund Olesen | 5f0d1b4 | 2012-04-27 22:58:38 +0000 | [diff] [blame] | 1323 | // point where we can add a 4-byte branch instruction, and then align to |
| 1324 | // LogAlign which is the largest possible alignment in the function. |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1325 | unsigned LogAlign = MF->getAlignment(); |
| 1326 | assert(LogAlign >= CPELogAlign && "Over-aligned constant pool entry"); |
| 1327 | unsigned KnownBits = UserBBI.internalKnownBits(); |
| 1328 | unsigned UPad = UnknownPadding(LogAlign, KnownBits); |
| Jakob Stoklund Olesen | 5f0d1b4 | 2012-04-27 22:58:38 +0000 | [diff] [blame] | 1329 | unsigned BaseInsertOffset = UserOffset + U.getMaxDisp() - UPad; |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1330 | DEBUG(dbgs() << format("Split in middle of big block before %#x", |
| 1331 | BaseInsertOffset)); |
| 1332 | |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1333 | // The 4 in the following is for the unconditional branch we'll be inserting |
| 1334 | // (allows for long branch on Thumb1). Alignment of the island is handled |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1335 | // inside isOffsetInRange. |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1336 | BaseInsertOffset -= 4; |
| 1337 | |
| 1338 | DEBUG(dbgs() << format(", adjusted to %#x", BaseInsertOffset) |
| 1339 | << " la=" << LogAlign |
| 1340 | << " kb=" << KnownBits |
| 1341 | << " up=" << UPad << '\n'); |
| 1342 | |
| 1343 | // This could point off the end of the block if we've already got constant |
| 1344 | // pool entries following this block; only the last one is in the water list. |
| 1345 | // Back past any possible branches (allow for a conditional and a maximally |
| 1346 | // long unconditional). |
| Jakob Stoklund Olesen | ae7521d | 2012-04-28 06:21:38 +0000 | [diff] [blame] | 1347 | if (BaseInsertOffset + 8 >= UserBBI.postOffset()) { |
| Akira Hatanaka | 0d0c781 | 2014-10-17 01:31:47 +0000 | [diff] [blame] | 1348 | // Ensure BaseInsertOffset is larger than the offset of the instruction |
| 1349 | // following UserMI so that the loop which searches for the split point |
| 1350 | // iterates at least once. |
| 1351 | BaseInsertOffset = |
| 1352 | std::max(UserBBI.postOffset() - UPad - 8, |
| Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 1353 | UserOffset + TII->getInstSizeInBytes(*UserMI) + 1); |
| Jakob Stoklund Olesen | ae7521d | 2012-04-28 06:21:38 +0000 | [diff] [blame] | 1354 | DEBUG(dbgs() << format("Move inside block: %#x\n", BaseInsertOffset)); |
| 1355 | } |
| Jakob Stoklund Olesen | 5f0d1b4 | 2012-04-27 22:58:38 +0000 | [diff] [blame] | 1356 | unsigned EndInsertOffset = BaseInsertOffset + 4 + UPad + |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1357 | CPEMI->getOperand(2).getImm(); |
| 1358 | MachineBasicBlock::iterator MI = UserMI; |
| 1359 | ++MI; |
| 1360 | unsigned CPUIndex = CPUserIndex+1; |
| 1361 | unsigned NumCPUsers = CPUsers.size(); |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1362 | MachineInstr *LastIT = nullptr; |
| Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 1363 | for (unsigned Offset = UserOffset + TII->getInstSizeInBytes(*UserMI); |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1364 | Offset < BaseInsertOffset; |
| Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 1365 | Offset += TII->getInstSizeInBytes(*MI), MI = std::next(MI)) { |
| Jakob Stoklund Olesen | ae7521d | 2012-04-28 06:21:38 +0000 | [diff] [blame] | 1366 | assert(MI != UserMBB->end() && "Fell off end of block"); |
| Duncan P. N. Exon Smith | 29c5249 | 2016-07-08 20:21:17 +0000 | [diff] [blame] | 1367 | if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].MI == &*MI) { |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1368 | CPUser &U = CPUsers[CPUIndex]; |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1369 | if (!isOffsetInRange(Offset, EndInsertOffset, U)) { |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1370 | // Shift intertion point by one unit of alignment so it is within reach. |
| 1371 | BaseInsertOffset -= 1u << LogAlign; |
| 1372 | EndInsertOffset -= 1u << LogAlign; |
| 1373 | } |
| 1374 | // This is overly conservative, as we don't account for CPEMIs being |
| 1375 | // reused within the block, but it doesn't matter much. Also assume CPEs |
| 1376 | // are added in order with alignment padding. We may eventually be able |
| 1377 | // to pack the aligned CPEs better. |
| Jakob Stoklund Olesen | 5f0d1b4 | 2012-04-27 22:58:38 +0000 | [diff] [blame] | 1378 | EndInsertOffset += U.CPEMI->getOperand(2).getImm(); |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1379 | CPUIndex++; |
| 1380 | } |
| 1381 | |
| 1382 | // Remember the last IT instruction. |
| 1383 | if (MI->getOpcode() == ARM::t2IT) |
| Duncan P. N. Exon Smith | 29c5249 | 2016-07-08 20:21:17 +0000 | [diff] [blame] | 1384 | LastIT = &*MI; |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | --MI; |
| 1388 | |
| 1389 | // Avoid splitting an IT block. |
| 1390 | if (LastIT) { |
| 1391 | unsigned PredReg = 0; |
| Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1392 | ARMCC::CondCodes CC = getITInstrPredicate(*MI, PredReg); |
| Jakob Stoklund Olesen | 9efd7eb | 2011-12-14 23:48:54 +0000 | [diff] [blame] | 1393 | if (CC != ARMCC::AL) |
| 1394 | MI = LastIT; |
| 1395 | } |
| Tim Northover | 631cc9c | 2014-11-13 17:58:53 +0000 | [diff] [blame] | 1396 | |
| 1397 | // We really must not split an IT block. |
| 1398 | DEBUG(unsigned PredReg; |
| Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1399 | assert(!isThumb || getITInstrPredicate(*MI, PredReg) == ARMCC::AL)); |
| Tim Northover | 631cc9c | 2014-11-13 17:58:53 +0000 | [diff] [blame] | 1400 | |
| Duncan P. N. Exon Smith | 29c5249 | 2016-07-08 20:21:17 +0000 | [diff] [blame] | 1401 | NewMBB = splitBlockBeforeInstr(&*MI); |
| Dale Johannesen | 962fa8e | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1404 | /// handleConstantPoolUser - Analyze the specified user, checking to see if it |
| Bob Wilson | ce8cfb4 | 2009-05-12 17:35:29 +0000 | [diff] [blame] | 1405 | /// is out-of-range. If so, pick up the constant pool value and move it some |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1406 | /// place in-range. Return true if we changed any addresses (thus must run |
| 1407 | /// another pass of branch lengthening), false otherwise. |
| Weiming Zhao | 5e0c3eb | 2016-02-23 18:39:19 +0000 | [diff] [blame] | 1408 | bool ARMConstantIslands::handleConstantPoolUser(unsigned CPUserIndex, |
| 1409 | bool CloserWater) { |
| Dale Johannesen | 440995b | 2007-02-28 18:41:23 +0000 | [diff] [blame] | 1410 | CPUser &U = CPUsers[CPUserIndex]; |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1411 | MachineInstr *UserMI = U.MI; |
| 1412 | MachineInstr *CPEMI = U.CPEMI; |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1413 | unsigned CPI = getCombinedIndex(CPEMI); |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1414 | unsigned Size = CPEMI->getOperand(2).getImm(); |
| Jakob Stoklund Olesen | f09a316 | 2012-01-10 01:34:59 +0000 | [diff] [blame] | 1415 | // Compute this only once, it's expensive. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1416 | unsigned UserOffset = getUserOffset(U); |
| Evan Cheng | d9990f0 | 2007-04-27 08:14:15 +0000 | [diff] [blame] | 1417 | |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1418 | // See if the current entry is within range, or there is a clone of it |
| 1419 | // in range. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1420 | int result = findInRangeCPEntry(U, UserOffset); |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1421 | if (result==1) return false; |
| 1422 | else if (result==2) return true; |
| 1423 | |
| 1424 | // No existing clone of this CPE is within range. |
| 1425 | // We will be generating a new clone. Get a UID for it. |
| Evan Cheng | dfce83c | 2011-01-17 08:03:18 +0000 | [diff] [blame] | 1426 | unsigned ID = AFI->createPICLabelUId(); |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1427 | |
| Bob Wilson | cc121aa | 2009-10-12 21:23:15 +0000 | [diff] [blame] | 1428 | // Look for water where we can place this CPE. |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 1429 | MachineBasicBlock *NewIsland = MF->CreateMachineBasicBlock(); |
| Bob Wilson | 2f9be50 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1430 | MachineBasicBlock *NewMBB; |
| 1431 | water_iterator IP; |
| Weiming Zhao | 5e0c3eb | 2016-02-23 18:39:19 +0000 | [diff] [blame] | 1432 | if (findAvailableWater(U, UserOffset, IP, CloserWater)) { |
| Jakob Stoklund Olesen | 5f5fa12 | 2011-12-09 18:20:35 +0000 | [diff] [blame] | 1433 | DEBUG(dbgs() << "Found water in range\n"); |
| Bob Wilson | 2f9be50 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1434 | MachineBasicBlock *WaterBB = *IP; |
| 1435 | |
| 1436 | // If the original WaterList entry was "new water" on this iteration, |
| 1437 | // propagate that to the new island. This is just keeping NewWaterList |
| 1438 | // updated to match the WaterList, which will be updated below. |
| Benjamin Kramer | f29db27 | 2012-08-22 15:37:57 +0000 | [diff] [blame] | 1439 | if (NewWaterList.erase(WaterBB)) |
| Bob Wilson | 2f9be50 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1440 | NewWaterList.insert(NewIsland); |
| Benjamin Kramer | f29db27 | 2012-08-22 15:37:57 +0000 | [diff] [blame] | 1441 | |
| Bob Wilson | 2f9be50 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1442 | // The new CPE goes before the following block (NewMBB). |
| Duncan P. N. Exon Smith | 9f9559e | 2015-10-19 23:25:57 +0000 | [diff] [blame] | 1443 | NewMBB = &*++WaterBB->getIterator(); |
| Bob Wilson | 2f9be50 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1444 | } else { |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1445 | // No water found. |
| Jakob Stoklund Olesen | 5f5fa12 | 2011-12-09 18:20:35 +0000 | [diff] [blame] | 1446 | DEBUG(dbgs() << "No water found\n"); |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1447 | createNewWater(CPUserIndex, UserOffset, NewMBB); |
| Bob Wilson | 2f9be50 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1448 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1449 | // splitBlockBeforeInstr adds to WaterList, which is important when it is |
| Bob Wilson | 2f9be50 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1450 | // called while handling branches so that the water will be seen on the |
| 1451 | // next iteration for constant pools, but in this context, we don't want |
| 1452 | // it. Check for this so it will be removed from the WaterList. |
| 1453 | // Also remove any entry from NewWaterList. |
| Duncan P. N. Exon Smith | 9f9559e | 2015-10-19 23:25:57 +0000 | [diff] [blame] | 1454 | MachineBasicBlock *WaterBB = &*--NewMBB->getIterator(); |
| David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 1455 | IP = find(WaterList, WaterBB); |
| Bob Wilson | 2f9be50 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1456 | if (IP != WaterList.end()) |
| 1457 | NewWaterList.erase(WaterBB); |
| 1458 | |
| 1459 | // We are adding new water. Update NewWaterList. |
| 1460 | NewWaterList.insert(NewIsland); |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| Bob Wilson | 2f9be50 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1463 | // Remove the original WaterList entry; we want subsequent insertions in |
| 1464 | // this vicinity to go after the one we're about to insert. This |
| 1465 | // considerably reduces the number of times we have to move the same CPE |
| 1466 | // more than once and is also important to ensure the algorithm terminates. |
| 1467 | if (IP != WaterList.end()) |
| 1468 | WaterList.erase(IP); |
| 1469 | |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1470 | // Okay, we know we can put an island before NewMBB now, do it! |
| Duncan P. N. Exon Smith | 9f9559e | 2015-10-19 23:25:57 +0000 | [diff] [blame] | 1471 | MF->insert(NewMBB->getIterator(), NewIsland); |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1472 | |
| 1473 | // Update internal data structures to account for the newly inserted MBB. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1474 | updateForInsertedWaterBlock(NewIsland); |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1475 | |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1476 | // Now that we have an island to add the CPE to, clone the original CPE and |
| 1477 | // add it to the island. |
| Bob Wilson | 68ead6c | 2009-10-15 05:52:29 +0000 | [diff] [blame] | 1478 | U.HighWaterMark = NewIsland; |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1479 | U.CPEMI = BuildMI(NewIsland, DebugLoc(), CPEMI->getDesc()) |
| 1480 | .addImm(ID).addOperand(CPEMI->getOperand(1)).addImm(Size); |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1481 | CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1)); |
| Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1482 | ++NumCPEs; |
| Evan Cheng | 8b7700f | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 1483 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1484 | // Decrement the old entry, and remove it if refcount becomes 0. |
| 1485 | decrementCPEReferenceCount(CPI, CPEMI); |
| 1486 | |
| Jakob Stoklund Olesen | 17c27a8 | 2011-12-12 18:45:45 +0000 | [diff] [blame] | 1487 | // Mark the basic block as aligned as required by the const-pool entry. |
| 1488 | NewIsland->setAlignment(getCPELogAlign(U.CPEMI)); |
| Jakob Stoklund Olesen | 2e05db2 | 2011-12-06 01:43:02 +0000 | [diff] [blame] | 1489 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1490 | // Increase the size of the island block to account for the new entry. |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1491 | BBInfo[NewIsland->getNumber()].Size += Size; |
| Duncan P. N. Exon Smith | 9f9559e | 2015-10-19 23:25:57 +0000 | [diff] [blame] | 1492 | adjustBBOffsetsAfter(&*--NewIsland->getIterator()); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1493 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1494 | // Finally, change the CPI in the instruction operand to be ID. |
| 1495 | for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i) |
| Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1496 | if (UserMI->getOperand(i).isCPI()) { |
| Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1497 | UserMI->getOperand(i).setIndex(ID); |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1498 | break; |
| 1499 | } |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1500 | |
| Jakob Stoklund Olesen | 5f5fa12 | 2011-12-09 18:20:35 +0000 | [diff] [blame] | 1501 | DEBUG(dbgs() << " Moved CPE to #" << ID << " CPI=" << CPI |
| Jakob Stoklund Olesen | b373452 | 2011-12-10 02:55:06 +0000 | [diff] [blame] | 1502 | << format(" offset=%#x\n", BBInfo[NewIsland->getNumber()].Offset)); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1503 | |
| Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1504 | return true; |
| 1505 | } |
| 1506 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1507 | /// removeDeadCPEMI - Remove a dead constant pool entry instruction. Update |
| Evan Cheng | 3c68d4e | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1508 | /// sizes and offsets of impacted basic blocks. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1509 | void ARMConstantIslands::removeDeadCPEMI(MachineInstr *CPEMI) { |
| Evan Cheng | 3c68d4e | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1510 | MachineBasicBlock *CPEBB = CPEMI->getParent(); |
| Dale Johannesen | 4a00cf3 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1511 | unsigned Size = CPEMI->getOperand(2).getImm(); |
| 1512 | CPEMI->eraseFromParent(); |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1513 | BBInfo[CPEBB->getNumber()].Size -= Size; |
| Dale Johannesen | 4a00cf3 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1514 | // All succeeding offsets have the current size value added in, fix this. |
| Evan Cheng | 3c68d4e | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1515 | if (CPEBB->empty()) { |
| Jakob Stoklund Olesen | 17c27a8 | 2011-12-12 18:45:45 +0000 | [diff] [blame] | 1516 | BBInfo[CPEBB->getNumber()].Size = 0; |
| Jakob Stoklund Olesen | 2fa7448 | 2011-12-06 21:55:35 +0000 | [diff] [blame] | 1517 | |
| Evan Cheng | ab28b9a | 2013-02-21 18:37:54 +0000 | [diff] [blame] | 1518 | // This block no longer needs to be aligned. |
| Jakob Stoklund Olesen | 2fa7448 | 2011-12-06 21:55:35 +0000 | [diff] [blame] | 1519 | CPEBB->setAlignment(0); |
| Jakob Stoklund Olesen | 17c27a8 | 2011-12-12 18:45:45 +0000 | [diff] [blame] | 1520 | } else |
| 1521 | // Entries are sorted by descending alignment, so realign from the front. |
| Duncan P. N. Exon Smith | 29c5249 | 2016-07-08 20:21:17 +0000 | [diff] [blame] | 1522 | CPEBB->setAlignment(getCPELogAlign(&*CPEBB->begin())); |
| Jakob Stoklund Olesen | 17c27a8 | 2011-12-12 18:45:45 +0000 | [diff] [blame] | 1523 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1524 | adjustBBOffsetsAfter(CPEBB); |
| Dale Johannesen | 4a00cf3 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1525 | // An island has only one predecessor BB and one successor BB. Check if |
| 1526 | // this BB's predecessor jumps directly to this BB's successor. This |
| 1527 | // shouldn't happen currently. |
| 1528 | assert(!BBIsJumpedOver(CPEBB) && "How did this happen?"); |
| 1529 | // FIXME: remove the empty blocks after all the work is done? |
| Evan Cheng | 3c68d4e | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1532 | /// removeUnusedCPEntries - Remove constant pool entries whose refcounts |
| Evan Cheng | 3c68d4e | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1533 | /// are zero. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1534 | bool ARMConstantIslands::removeUnusedCPEntries() { |
| Evan Cheng | 3c68d4e | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1535 | unsigned MadeChange = false; |
| 1536 | for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) { |
| 1537 | std::vector<CPEntry> &CPEs = CPEntries[i]; |
| 1538 | for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) { |
| 1539 | if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) { |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1540 | removeDeadCPEMI(CPEs[j].CPEMI); |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1541 | CPEs[j].CPEMI = nullptr; |
| Evan Cheng | 3c68d4e | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1542 | MadeChange = true; |
| 1543 | } |
| 1544 | } |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1545 | } |
| Evan Cheng | 3c68d4e | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1546 | return MadeChange; |
| 1547 | } |
| 1548 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1549 | /// isBBInRange - Returns true if the distance between specific MI and |
| Evan Cheng | 3c9dc6b | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1550 | /// specific BB can fit in MI's displacement field. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1551 | bool ARMConstantIslands::isBBInRange(MachineInstr *MI,MachineBasicBlock *DestBB, |
| Evan Cheng | 1f3fc4b | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 1552 | unsigned MaxDisp) { |
| Dale Johannesen | 962fa8e | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1553 | unsigned PCAdj = isThumb ? 4 : 8; |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1554 | unsigned BrOffset = getOffsetOf(MI) + PCAdj; |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1555 | unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset; |
| Evan Cheng | 3c9dc6b | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1556 | |
| Jakob Stoklund Olesen | 5f5fa12 | 2011-12-09 18:20:35 +0000 | [diff] [blame] | 1557 | DEBUG(dbgs() << "Branch of destination BB#" << DestBB->getNumber() |
| Chris Lattner | a6f074f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 1558 | << " from BB#" << MI->getParent()->getNumber() |
| 1559 | << " max delta=" << MaxDisp |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1560 | << " from " << getOffsetOf(MI) << " to " << DestOffset |
| Chris Lattner | a6f074f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 1561 | << " offset " << int(DestOffset-BrOffset) << "\t" << *MI); |
| Evan Cheng | 1f3fc4b | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 1562 | |
| Dale Johannesen | 4a00cf3 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1563 | if (BrOffset <= DestOffset) { |
| 1564 | // Branch before the Dest. |
| 1565 | if (DestOffset-BrOffset <= MaxDisp) |
| 1566 | return true; |
| 1567 | } else { |
| 1568 | if (BrOffset-DestOffset <= MaxDisp) |
| 1569 | return true; |
| 1570 | } |
| 1571 | return false; |
| Evan Cheng | 3c9dc6b | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1574 | /// fixupImmediateBr - Fix up an immediate branch whose destination is too far |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1575 | /// away to fit in its displacement field. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1576 | bool ARMConstantIslands::fixupImmediateBr(ImmBranch &Br) { |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1577 | MachineInstr *MI = Br.MI; |
| Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1578 | MachineBasicBlock *DestBB = MI->getOperand(0).getMBB(); |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1579 | |
| Evan Cheng | 1f3fc4b | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 1580 | // Check to see if the DestBB is already in-range. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1581 | if (isBBInRange(MI, DestBB, Br.MaxDisp)) |
| Evan Cheng | 3c9dc6b | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1582 | return false; |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1583 | |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1584 | if (!Br.isCond) |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1585 | return fixupUnconditionalBr(Br); |
| 1586 | return fixupConditionalBr(Br); |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1587 | } |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1588 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1589 | /// fixupUnconditionalBr - Fix up an unconditional branch whose destination is |
| Dale Johannesen | e18b13b | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1590 | /// too far away to fit in its displacement field. If the LR register has been |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1591 | /// spilled in the epilogue, then we can use BL to implement a far jump. |
| Bob Wilson | ce8cfb4 | 2009-05-12 17:35:29 +0000 | [diff] [blame] | 1592 | /// Otherwise, add an intermediate branch instruction to a branch. |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1593 | bool |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1594 | ARMConstantIslands::fixupUnconditionalBr(ImmBranch &Br) { |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1595 | MachineInstr *MI = Br.MI; |
| 1596 | MachineBasicBlock *MBB = MI->getParent(); |
| Evan Cheng | 317bd7a | 2009-08-07 05:45:07 +0000 | [diff] [blame] | 1597 | if (!isThumb1) |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1598 | llvm_unreachable("fixupUnconditionalBr is Thumb1 only!"); |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1599 | |
| 1600 | // Use BL to implement far jump. |
| 1601 | Br.MaxDisp = (1 << 21) * 2; |
| Chris Lattner | 5968751 | 2008-01-11 18:10:50 +0000 | [diff] [blame] | 1602 | MI->setDesc(TII->get(ARM::tBfar)); |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1603 | BBInfo[MBB->getNumber()].Size += 2; |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1604 | adjustBBOffsetsAfter(MBB); |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1605 | HasFarJump = true; |
| Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1606 | ++NumUBrFixed; |
| Evan Cheng | 36d559d | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1607 | |
| Jakob Stoklund Olesen | 5f5fa12 | 2011-12-09 18:20:35 +0000 | [diff] [blame] | 1608 | DEBUG(dbgs() << " Changed B to long jump " << *MI); |
| Evan Cheng | 36d559d | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1609 | |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1610 | return true; |
| 1611 | } |
| 1612 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1613 | /// fixupConditionalBr - Fix up a conditional branch whose destination is too |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1614 | /// far away to fit in its displacement field. It is converted to an inverse |
| 1615 | /// conditional branch + an unconditional branch to the destination. |
| 1616 | bool |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1617 | ARMConstantIslands::fixupConditionalBr(ImmBranch &Br) { |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1618 | MachineInstr *MI = Br.MI; |
| Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1619 | MachineBasicBlock *DestBB = MI->getOperand(0).getMBB(); |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1620 | |
| Bob Wilson | ce8cfb4 | 2009-05-12 17:35:29 +0000 | [diff] [blame] | 1621 | // Add an unconditional branch to the destination and invert the branch |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1622 | // condition to jump over it: |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1623 | // blt L1 |
| 1624 | // => |
| 1625 | // bge L2 |
| 1626 | // b L1 |
| 1627 | // L2: |
| Chris Lattner | 5c46378 | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 1628 | ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImm(); |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1629 | CC = ARMCC::getOppositeCondition(CC); |
| Evan Cheng | 94f04c6 | 2007-07-05 07:18:20 +0000 | [diff] [blame] | 1630 | unsigned CCReg = MI->getOperand(2).getReg(); |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1631 | |
| 1632 | // If the branch is at the end of its MBB and that has a fall-through block, |
| 1633 | // direct the updated conditional branch to the fall-through block. Otherwise, |
| 1634 | // split the MBB before the next instruction. |
| 1635 | MachineBasicBlock *MBB = MI->getParent(); |
| Evan Cheng | 36d559d | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1636 | MachineInstr *BMI = &MBB->back(); |
| 1637 | bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB); |
| Evan Cheng | 3c9dc6b | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1638 | |
| Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1639 | ++NumCBrFixed; |
| Evan Cheng | 36d559d | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1640 | if (BMI != MI) { |
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1641 | if (std::next(MachineBasicBlock::iterator(MI)) == std::prev(MBB->end()) && |
| Evan Cheng | 36d559d | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1642 | BMI->getOpcode() == Br.UncondBr) { |
| Bob Wilson | ce8cfb4 | 2009-05-12 17:35:29 +0000 | [diff] [blame] | 1643 | // Last MI in the BB is an unconditional branch. Can we simply invert the |
| Evan Cheng | 3c9dc6b | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1644 | // condition and swap destinations: |
| 1645 | // beq L1 |
| 1646 | // b L2 |
| 1647 | // => |
| 1648 | // bne L2 |
| 1649 | // b L1 |
| Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1650 | MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB(); |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1651 | if (isBBInRange(MI, NewDest, Br.MaxDisp)) { |
| Jakob Stoklund Olesen | 5f5fa12 | 2011-12-09 18:20:35 +0000 | [diff] [blame] | 1652 | DEBUG(dbgs() << " Invert Bcc condition and swap its destination with " |
| Chris Lattner | a6f074f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 1653 | << *BMI); |
| Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1654 | BMI->getOperand(0).setMBB(DestBB); |
| 1655 | MI->getOperand(0).setMBB(NewDest); |
| Evan Cheng | 3c9dc6b | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1656 | MI->getOperand(1).setImm(CC); |
| 1657 | return true; |
| 1658 | } |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | if (NeedSplit) { |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1663 | splitBlockBeforeInstr(MI); |
| Bob Wilson | ce8cfb4 | 2009-05-12 17:35:29 +0000 | [diff] [blame] | 1664 | // No need for the branch to the next block. We're adding an unconditional |
| Evan Cheng | 1e270b6 | 2007-01-26 02:02:39 +0000 | [diff] [blame] | 1665 | // branch to the destination. |
| Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 1666 | int delta = TII->getInstSizeInBytes(MBB->back()); |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1667 | BBInfo[MBB->getNumber()].Size -= delta; |
| Evan Cheng | 1e270b6 | 2007-01-26 02:02:39 +0000 | [diff] [blame] | 1668 | MBB->back().eraseFromParent(); |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1669 | // BBInfo[SplitBB].Offset is wrong temporarily, fixed below |
| Evan Cheng | 1e270b6 | 2007-01-26 02:02:39 +0000 | [diff] [blame] | 1670 | } |
| Duncan P. N. Exon Smith | 9f9559e | 2015-10-19 23:25:57 +0000 | [diff] [blame] | 1671 | MachineBasicBlock *NextBB = &*++MBB->getIterator(); |
| Bob Wilson | 2f4e56f | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1672 | |
| Jakob Stoklund Olesen | 5f5fa12 | 2011-12-09 18:20:35 +0000 | [diff] [blame] | 1673 | DEBUG(dbgs() << " Insert B to BB#" << DestBB->getNumber() |
| Chris Lattner | af29ea6 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 1674 | << " also invert condition and change dest. to BB#" |
| 1675 | << NextBB->getNumber() << "\n"); |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1676 | |
| Dale Johannesen | fdfb757 | 2007-04-23 20:09:04 +0000 | [diff] [blame] | 1677 | // Insert a new conditional branch and a new unconditional branch. |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1678 | // Also update the ImmBranch as well as adding a new entry for the new branch. |
| Chris Lattner | 6f306d7 | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 1679 | BuildMI(MBB, DebugLoc(), TII->get(MI->getOpcode())) |
| Dale Johannesen | 7647da6 | 2009-02-13 02:25:56 +0000 | [diff] [blame] | 1680 | .addMBB(NextBB).addImm(CC).addReg(CCReg); |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1681 | Br.MI = &MBB->back(); |
| Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 1682 | BBInfo[MBB->getNumber()].Size += TII->getInstSizeInBytes(MBB->back()); |
| Owen Anderson | 93cd318 | 2011-09-09 23:05:14 +0000 | [diff] [blame] | 1683 | if (isThumb) |
| 1684 | BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB) |
| 1685 | .addImm(ARMCC::AL).addReg(0); |
| 1686 | else |
| 1687 | BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB); |
| Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 1688 | BBInfo[MBB->getNumber()].Size += TII->getInstSizeInBytes(MBB->back()); |
| Evan Cheng | 7169bd8 | 2007-01-31 18:29:27 +0000 | [diff] [blame] | 1689 | unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr); |
| Evan Cheng | 1d13898 | 2007-01-25 23:31:04 +0000 | [diff] [blame] | 1690 | ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr)); |
| Dale Johannesen | fdfb757 | 2007-04-23 20:09:04 +0000 | [diff] [blame] | 1691 | |
| 1692 | // Remove the old conditional branch. It may or may not still be in MBB. |
| Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 1693 | BBInfo[MI->getParent()->getNumber()].Size -= TII->getInstSizeInBytes(*MI); |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1694 | MI->eraseFromParent(); |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1695 | adjustBBOffsetsAfter(MBB); |
| Evan Cheng | 22c7cf5 | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1696 | return true; |
| 1697 | } |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1698 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1699 | /// undoLRSpillRestore - Remove Thumb push / pop instructions that only spills |
| Evan Cheng | cc9ca35 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 1700 | /// LR / restores LR to pc. FIXME: This is done here because it's only possible |
| 1701 | /// to do this if tBfar is not used. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1702 | bool ARMConstantIslands::undoLRSpillRestore() { |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1703 | bool MadeChange = false; |
| 1704 | for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) { |
| 1705 | MachineInstr *MI = PushPopMIs[i]; |
| Bob Wilson | 947f04b | 2010-03-13 01:08:20 +0000 | [diff] [blame] | 1706 | // First two operands are predicates. |
| Evan Cheng | 0f7cbe8 | 2007-05-15 01:29:07 +0000 | [diff] [blame] | 1707 | if (MI->getOpcode() == ARM::tPOP_RET && |
| Bob Wilson | 947f04b | 2010-03-13 01:08:20 +0000 | [diff] [blame] | 1708 | MI->getOperand(2).getReg() == ARM::PC && |
| 1709 | MI->getNumExplicitOperands() == 3) { |
| Jim Grosbach | 7471937 | 2011-07-08 21:50:04 +0000 | [diff] [blame] | 1710 | // Create the new insn and copy the predicate from the old. |
| 1711 | BuildMI(MI->getParent(), MI->getDebugLoc(), TII->get(ARM::tBX_RET)) |
| 1712 | .addOperand(MI->getOperand(0)) |
| 1713 | .addOperand(MI->getOperand(1)); |
| Evan Cheng | 0f7cbe8 | 2007-05-15 01:29:07 +0000 | [diff] [blame] | 1714 | MI->eraseFromParent(); |
| 1715 | MadeChange = true; |
| Evan Cheng | 7fa6964 | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1716 | } |
| 1717 | } |
| 1718 | return MadeChange; |
| 1719 | } |
| Evan Cheng | c6d70ae | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1720 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1721 | bool ARMConstantIslands::optimizeThumb2Instructions() { |
| Evan Cheng | db73d68 | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1722 | bool MadeChange = false; |
| 1723 | |
| 1724 | // Shrink ADR and LDR from constantpool. |
| 1725 | for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) { |
| 1726 | CPUser &U = CPUsers[i]; |
| 1727 | unsigned Opcode = U.MI->getOpcode(); |
| 1728 | unsigned NewOpc = 0; |
| 1729 | unsigned Scale = 1; |
| 1730 | unsigned Bits = 0; |
| 1731 | switch (Opcode) { |
| 1732 | default: break; |
| Owen Anderson | 9a4d428 | 2010-12-13 22:51:08 +0000 | [diff] [blame] | 1733 | case ARM::t2LEApcrel: |
| Evan Cheng | db73d68 | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1734 | if (isARMLowRegister(U.MI->getOperand(0).getReg())) { |
| 1735 | NewOpc = ARM::tLEApcrel; |
| 1736 | Bits = 8; |
| 1737 | Scale = 4; |
| 1738 | } |
| 1739 | break; |
| 1740 | case ARM::t2LDRpci: |
| 1741 | if (isARMLowRegister(U.MI->getOperand(0).getReg())) { |
| 1742 | NewOpc = ARM::tLDRpci; |
| 1743 | Bits = 8; |
| 1744 | Scale = 4; |
| 1745 | } |
| 1746 | break; |
| 1747 | } |
| 1748 | |
| 1749 | if (!NewOpc) |
| 1750 | continue; |
| 1751 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1752 | unsigned UserOffset = getUserOffset(U); |
| Evan Cheng | db73d68 | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1753 | unsigned MaxOffs = ((1 << Bits) - 1) * Scale; |
| Jakob Stoklund Olesen | f09a316 | 2012-01-10 01:34:59 +0000 | [diff] [blame] | 1754 | |
| 1755 | // Be conservative with inline asm. |
| 1756 | if (!U.KnownAlignment) |
| 1757 | MaxOffs -= 2; |
| 1758 | |
| Evan Cheng | db73d68 | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1759 | // FIXME: Check if offset is multiple of scale if scale is not 4. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1760 | if (isCPEntryInRange(U.MI, UserOffset, U.CPEMI, MaxOffs, false, true)) { |
| Jakob Stoklund Olesen | 24bb3d5 | 2012-03-31 00:06:42 +0000 | [diff] [blame] | 1761 | DEBUG(dbgs() << "Shrink: " << *U.MI); |
| Evan Cheng | db73d68 | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1762 | U.MI->setDesc(TII->get(NewOpc)); |
| 1763 | MachineBasicBlock *MBB = U.MI->getParent(); |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1764 | BBInfo[MBB->getNumber()].Size -= 2; |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1765 | adjustBBOffsetsAfter(MBB); |
| Evan Cheng | db73d68 | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1766 | ++NumT2CPShrunk; |
| 1767 | MadeChange = true; |
| 1768 | } |
| 1769 | } |
| 1770 | |
| Evan Cheng | db73d68 | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1771 | return MadeChange; |
| 1772 | } |
| 1773 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1774 | bool ARMConstantIslands::optimizeThumb2Branches() { |
| Evan Cheng | e41903b | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1775 | bool MadeChange = false; |
| 1776 | |
| Peter Collingbourne | 167668f | 2015-04-23 20:31:35 +0000 | [diff] [blame] | 1777 | // The order in which branches appear in ImmBranches is approximately their |
| 1778 | // order within the function body. By visiting later branches first, we reduce |
| 1779 | // the distance between earlier forward branches and their targets, making it |
| 1780 | // more likely that the cbn?z optimization, which can only apply to forward |
| 1781 | // branches, will succeed. |
| 1782 | for (unsigned i = ImmBranches.size(); i != 0; --i) { |
| 1783 | ImmBranch &Br = ImmBranches[i-1]; |
| Evan Cheng | e41903b | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1784 | unsigned Opcode = Br.MI->getOpcode(); |
| 1785 | unsigned NewOpc = 0; |
| 1786 | unsigned Scale = 1; |
| 1787 | unsigned Bits = 0; |
| 1788 | switch (Opcode) { |
| 1789 | default: break; |
| 1790 | case ARM::t2B: |
| 1791 | NewOpc = ARM::tB; |
| 1792 | Bits = 11; |
| 1793 | Scale = 2; |
| 1794 | break; |
| Evan Cheng | 6f29ad9 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1795 | case ARM::t2Bcc: { |
| Evan Cheng | e41903b | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1796 | NewOpc = ARM::tBcc; |
| 1797 | Bits = 8; |
| Evan Cheng | 6f29ad9 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1798 | Scale = 2; |
| Evan Cheng | e41903b | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1799 | break; |
| 1800 | } |
| Evan Cheng | 6f29ad9 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1801 | } |
| 1802 | if (NewOpc) { |
| 1803 | unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale; |
| 1804 | MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB(); |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1805 | if (isBBInRange(Br.MI, DestBB, MaxOffs)) { |
| Jakob Stoklund Olesen | 24bb3d5 | 2012-03-31 00:06:42 +0000 | [diff] [blame] | 1806 | DEBUG(dbgs() << "Shrink branch: " << *Br.MI); |
| Evan Cheng | 6f29ad9 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1807 | Br.MI->setDesc(TII->get(NewOpc)); |
| 1808 | MachineBasicBlock *MBB = Br.MI->getParent(); |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1809 | BBInfo[MBB->getNumber()].Size -= 2; |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1810 | adjustBBOffsetsAfter(MBB); |
| Evan Cheng | 6f29ad9 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1811 | ++NumT2BrShrunk; |
| 1812 | MadeChange = true; |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | Opcode = Br.MI->getOpcode(); |
| 1817 | if (Opcode != ARM::tBcc) |
| Evan Cheng | e41903b | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1818 | continue; |
| 1819 | |
| Evan Cheng | 6bb9525 | 2012-01-14 01:53:46 +0000 | [diff] [blame] | 1820 | // If the conditional branch doesn't kill CPSR, then CPSR can be liveout |
| 1821 | // so this transformation is not safe. |
| 1822 | if (!Br.MI->killsRegister(ARM::CPSR)) |
| 1823 | continue; |
| 1824 | |
| Evan Cheng | 6f29ad9 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1825 | NewOpc = 0; |
| 1826 | unsigned PredReg = 0; |
| Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1827 | ARMCC::CondCodes Pred = getInstrPredicate(*Br.MI, PredReg); |
| Evan Cheng | 6f29ad9 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1828 | if (Pred == ARMCC::EQ) |
| 1829 | NewOpc = ARM::tCBZ; |
| 1830 | else if (Pred == ARMCC::NE) |
| 1831 | NewOpc = ARM::tCBNZ; |
| 1832 | if (!NewOpc) |
| 1833 | continue; |
| Evan Cheng | e41903b | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1834 | MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB(); |
| Evan Cheng | 6f29ad9 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1835 | // Check if the distance is within 126. Subtract starting offset by 2 |
| 1836 | // because the cmp will be eliminated. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1837 | unsigned BrOffset = getOffsetOf(Br.MI) + 4 - 2; |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1838 | unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset; |
| Evan Cheng | 6f29ad9 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1839 | if (BrOffset < DestOffset && (DestOffset - BrOffset) <= 126) { |
| Evan Cheng | 88530e6 | 2011-04-01 22:09:28 +0000 | [diff] [blame] | 1840 | MachineBasicBlock::iterator CmpMI = Br.MI; |
| 1841 | if (CmpMI != Br.MI->getParent()->begin()) { |
| 1842 | --CmpMI; |
| 1843 | if (CmpMI->getOpcode() == ARM::tCMPi8) { |
| 1844 | unsigned Reg = CmpMI->getOperand(0).getReg(); |
| Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1845 | Pred = getInstrPredicate(*CmpMI, PredReg); |
| Evan Cheng | 88530e6 | 2011-04-01 22:09:28 +0000 | [diff] [blame] | 1846 | if (Pred == ARMCC::AL && |
| 1847 | CmpMI->getOperand(1).getImm() == 0 && |
| 1848 | isARMLowRegister(Reg)) { |
| 1849 | MachineBasicBlock *MBB = Br.MI->getParent(); |
| Jakob Stoklund Olesen | 24bb3d5 | 2012-03-31 00:06:42 +0000 | [diff] [blame] | 1850 | DEBUG(dbgs() << "Fold: " << *CmpMI << " and: " << *Br.MI); |
| Evan Cheng | 88530e6 | 2011-04-01 22:09:28 +0000 | [diff] [blame] | 1851 | MachineInstr *NewBR = |
| 1852 | BuildMI(*MBB, CmpMI, Br.MI->getDebugLoc(), TII->get(NewOpc)) |
| 1853 | .addReg(Reg).addMBB(DestBB,Br.MI->getOperand(0).getTargetFlags()); |
| 1854 | CmpMI->eraseFromParent(); |
| 1855 | Br.MI->eraseFromParent(); |
| 1856 | Br.MI = NewBR; |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1857 | BBInfo[MBB->getNumber()].Size -= 2; |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1858 | adjustBBOffsetsAfter(MBB); |
| Evan Cheng | 88530e6 | 2011-04-01 22:09:28 +0000 | [diff] [blame] | 1859 | ++NumCBZ; |
| 1860 | MadeChange = true; |
| 1861 | } |
| Evan Cheng | 6f29ad9 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1862 | } |
| 1863 | } |
| Evan Cheng | e41903b | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1864 | } |
| 1865 | } |
| 1866 | |
| 1867 | return MadeChange; |
| Evan Cheng | db73d68 | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1870 | static bool isSimpleIndexCalc(MachineInstr &I, unsigned EntryReg, |
| 1871 | unsigned BaseReg) { |
| 1872 | if (I.getOpcode() != ARM::t2ADDrs) |
| 1873 | return false; |
| Tim Northover | 688f7bb | 2015-05-13 20:28:32 +0000 | [diff] [blame] | 1874 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1875 | if (I.getOperand(0).getReg() != EntryReg) |
| 1876 | return false; |
| Tim Northover | 688f7bb | 2015-05-13 20:28:32 +0000 | [diff] [blame] | 1877 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1878 | if (I.getOperand(1).getReg() != BaseReg) |
| 1879 | return false; |
| Tim Northover | 688f7bb | 2015-05-13 20:28:32 +0000 | [diff] [blame] | 1880 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1881 | // FIXME: what about CC and IdxReg? |
| 1882 | return true; |
| 1883 | } |
| Tim Northover | 688f7bb | 2015-05-13 20:28:32 +0000 | [diff] [blame] | 1884 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1885 | /// \brief While trying to form a TBB/TBH instruction, we may (if the table |
| 1886 | /// doesn't immediately follow the BR_JT) need access to the start of the |
| 1887 | /// jump-table. We know one instruction that produces such a register; this |
| 1888 | /// function works out whether that definition can be preserved to the BR_JT, |
| 1889 | /// possibly by removing an intervening addition (which is usually needed to |
| 1890 | /// calculate the actual entry to jump to). |
| 1891 | bool ARMConstantIslands::preserveBaseRegister(MachineInstr *JumpMI, |
| 1892 | MachineInstr *LEAMI, |
| 1893 | unsigned &DeadSize, |
| 1894 | bool &CanDeleteLEA, |
| 1895 | bool &BaseRegKill) { |
| 1896 | if (JumpMI->getParent() != LEAMI->getParent()) |
| 1897 | return false; |
| Tim Northover | 688f7bb | 2015-05-13 20:28:32 +0000 | [diff] [blame] | 1898 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1899 | // Now we hope that we have at least these instructions in the basic block: |
| 1900 | // BaseReg = t2LEA ... |
| 1901 | // [...] |
| 1902 | // EntryReg = t2ADDrs BaseReg, ... |
| 1903 | // [...] |
| 1904 | // t2BR_JT EntryReg |
| 1905 | // |
| 1906 | // We have to be very conservative about what we recognise here though. The |
| 1907 | // main perturbing factors to watch out for are: |
| 1908 | // + Spills at any point in the chain: not direct problems but we would |
| 1909 | // expect a blocking Def of the spilled register so in practice what we |
| 1910 | // can do is limited. |
| 1911 | // + EntryReg == BaseReg: this is the one situation we should allow a Def |
| 1912 | // of BaseReg, but only if the t2ADDrs can be removed. |
| 1913 | // + Some instruction other than t2ADDrs computing the entry. Not seen in |
| 1914 | // the wild, but we should be careful. |
| 1915 | unsigned EntryReg = JumpMI->getOperand(0).getReg(); |
| 1916 | unsigned BaseReg = LEAMI->getOperand(0).getReg(); |
| 1917 | |
| 1918 | CanDeleteLEA = true; |
| 1919 | BaseRegKill = false; |
| 1920 | MachineInstr *RemovableAdd = nullptr; |
| 1921 | MachineBasicBlock::iterator I(LEAMI); |
| 1922 | for (++I; &*I != JumpMI; ++I) { |
| 1923 | if (isSimpleIndexCalc(*I, EntryReg, BaseReg)) { |
| 1924 | RemovableAdd = &*I; |
| 1925 | break; |
| 1926 | } |
| 1927 | |
| 1928 | for (unsigned K = 0, E = I->getNumOperands(); K != E; ++K) { |
| 1929 | const MachineOperand &MO = I->getOperand(K); |
| 1930 | if (!MO.isReg() || !MO.getReg()) |
| 1931 | continue; |
| 1932 | if (MO.isDef() && MO.getReg() == BaseReg) |
| 1933 | return false; |
| 1934 | if (MO.isUse() && MO.getReg() == BaseReg) { |
| 1935 | BaseRegKill = BaseRegKill || MO.isKill(); |
| 1936 | CanDeleteLEA = false; |
| 1937 | } |
| Tim Northover | 688f7bb | 2015-05-13 20:28:32 +0000 | [diff] [blame] | 1938 | } |
| 1939 | } |
| 1940 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1941 | if (!RemovableAdd) |
| 1942 | return true; |
| Tim Northover | 688f7bb | 2015-05-13 20:28:32 +0000 | [diff] [blame] | 1943 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1944 | // Check the add really is removable, and that nothing else in the block |
| 1945 | // clobbers BaseReg. |
| 1946 | for (++I; &*I != JumpMI; ++I) { |
| 1947 | for (unsigned K = 0, E = I->getNumOperands(); K != E; ++K) { |
| 1948 | const MachineOperand &MO = I->getOperand(K); |
| 1949 | if (!MO.isReg() || !MO.getReg()) |
| 1950 | continue; |
| 1951 | if (MO.isDef() && MO.getReg() == BaseReg) |
| 1952 | return false; |
| 1953 | if (MO.isUse() && MO.getReg() == EntryReg) |
| 1954 | RemovableAdd = nullptr; |
| 1955 | } |
| 1956 | } |
| Tim Northover | 688f7bb | 2015-05-13 20:28:32 +0000 | [diff] [blame] | 1957 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1958 | if (RemovableAdd) { |
| 1959 | RemovableAdd->eraseFromParent(); |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 1960 | DeadSize += isThumb2 ? 4 : 2; |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1961 | } else if (BaseReg == EntryReg) { |
| 1962 | // The add wasn't removable, but clobbered the base for the TBB. So we can't |
| 1963 | // preserve it. |
| 1964 | return false; |
| 1965 | } |
| Tim Northover | 688f7bb | 2015-05-13 20:28:32 +0000 | [diff] [blame] | 1966 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1967 | // We reached the end of the block without seeing another definition of |
| 1968 | // BaseReg (except, possibly the t2ADDrs, which was removed). BaseReg can be |
| 1969 | // used in the TBB/TBH if necessary. |
| 1970 | return true; |
| 1971 | } |
| Tim Northover | 688f7bb | 2015-05-13 20:28:32 +0000 | [diff] [blame] | 1972 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1973 | /// \brief Returns whether CPEMI is the first instruction in the block |
| 1974 | /// immediately following JTMI (assumed to be a TBB or TBH terminator). If so, |
| 1975 | /// we can switch the first register to PC and usually remove the address |
| Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1976 | /// calculation that preceded it. |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1977 | static bool jumpTableFollowsTB(MachineInstr *JTMI, MachineInstr *CPEMI) { |
| Duncan P. N. Exon Smith | 9f9559e | 2015-10-19 23:25:57 +0000 | [diff] [blame] | 1978 | MachineFunction::iterator MBB = JTMI->getParent()->getIterator(); |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 1979 | MachineFunction *MF = MBB->getParent(); |
| 1980 | ++MBB; |
| 1981 | |
| 1982 | return MBB != MF->end() && MBB->begin() != MBB->end() && |
| 1983 | &*MBB->begin() == CPEMI; |
| Tim Northover | 688f7bb | 2015-05-13 20:28:32 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1986 | /// optimizeThumb2JumpTables - Use tbb / tbh instructions to generate smaller |
| Evan Cheng | db73d68 | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1987 | /// jumptables when it's possible. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 1988 | bool ARMConstantIslands::optimizeThumb2JumpTables() { |
| Evan Cheng | c6d70ae | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1989 | bool MadeChange = false; |
| 1990 | |
| 1991 | // FIXME: After the tables are shrunk, can we get rid some of the |
| 1992 | // constantpool tables? |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 1993 | MachineJumpTableInfo *MJTI = MF->getJumpTableInfo(); |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1994 | if (!MJTI) return false; |
| Jim Grosbach | e4ba2aa | 2010-07-07 21:06:51 +0000 | [diff] [blame] | 1995 | |
| Evan Cheng | c6d70ae | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1996 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 1997 | for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) { |
| 1998 | MachineInstr *MI = T2JumpTables[i]; |
| Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1999 | const MCInstrDesc &MCID = MI->getDesc(); |
| 2000 | unsigned NumOps = MCID.getNumOperands(); |
| Tim Northover | 4998a47 | 2015-05-13 20:28:38 +0000 | [diff] [blame] | 2001 | unsigned JTOpIdx = NumOps - (MI->isPredicable() ? 2 : 1); |
| Evan Cheng | c6d70ae | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 2002 | MachineOperand JTOP = MI->getOperand(JTOpIdx); |
| 2003 | unsigned JTI = JTOP.getIndex(); |
| 2004 | assert(JTI < JT.size()); |
| 2005 | |
| Jim Grosbach | 8d92ec4 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 2006 | bool ByteOk = true; |
| 2007 | bool HalfWordOk = true; |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 2008 | unsigned JTOffset = getOffsetOf(MI) + 4; |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 2009 | const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs; |
| Evan Cheng | c6d70ae | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 2010 | for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) { |
| 2011 | MachineBasicBlock *MBB = JTBBs[j]; |
| Jakob Stoklund Olesen | e2b3ff2 | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 2012 | unsigned DstOffset = BBInfo[MBB->getNumber()].Offset; |
| Evan Cheng | e3493a9 | 2009-07-29 23:20:20 +0000 | [diff] [blame] | 2013 | // Negative offset is not ok. FIXME: We should change BB layout to make |
| 2014 | // sure all the branches are forward. |
| Evan Cheng | f6d0fa3 | 2009-07-31 18:28:05 +0000 | [diff] [blame] | 2015 | if (ByteOk && (DstOffset - JTOffset) > ((1<<8)-1)*2) |
| Evan Cheng | c6d70ae | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 2016 | ByteOk = false; |
| Evan Cheng | e64f48b | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 2017 | unsigned TBHLimit = ((1<<16)-1)*2; |
| Evan Cheng | e64f48b | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 2018 | if (HalfWordOk && (DstOffset - JTOffset) > TBHLimit) |
| Evan Cheng | c6d70ae | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 2019 | HalfWordOk = false; |
| 2020 | if (!ByteOk && !HalfWordOk) |
| 2021 | break; |
| 2022 | } |
| 2023 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 2024 | if (!ByteOk && !HalfWordOk) |
| 2025 | continue; |
| Jim Grosbach | 40eda10 | 2010-07-07 22:51:22 +0000 | [diff] [blame] | 2026 | |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 2027 | CPUser &User = CPUsers[JumpTableUserIndices[JTI]]; |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 2028 | MachineBasicBlock *MBB = MI->getParent(); |
| 2029 | if (!MI->getOperand(0).isKill()) // FIXME: needed now? |
| 2030 | continue; |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 2031 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 2032 | unsigned DeadSize = 0; |
| 2033 | bool CanDeleteLEA = false; |
| 2034 | bool BaseRegKill = false; |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 2035 | |
| 2036 | unsigned IdxReg = ~0U; |
| 2037 | bool IdxRegKill = true; |
| 2038 | if (isThumb2) { |
| 2039 | IdxReg = MI->getOperand(1).getReg(); |
| 2040 | IdxRegKill = MI->getOperand(1).isKill(); |
| 2041 | |
| 2042 | bool PreservedBaseReg = |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 2043 | preserveBaseRegister(MI, User.MI, DeadSize, CanDeleteLEA, BaseRegKill); |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 2044 | if (!jumpTableFollowsTB(MI, User.CPEMI) && !PreservedBaseReg) |
| 2045 | continue; |
| 2046 | } else { |
| 2047 | // We're in thumb-1 mode, so we must have something like: |
| 2048 | // %idx = tLSLri %idx, 2 |
| 2049 | // %base = tLEApcrelJT |
| 2050 | // %t = tLDRr %idx, %base |
| 2051 | unsigned BaseReg = User.MI->getOperand(0).getReg(); |
| James Molloy | 2bae864 | 2016-10-22 09:58:37 +0000 | [diff] [blame] | 2052 | |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 2053 | if (User.MI->getIterator() == User.MI->getParent()->begin()) |
| 2054 | continue; |
| 2055 | MachineInstr *Shift = User.MI->getPrevNode(); |
| 2056 | if (Shift->getOpcode() != ARM::tLSLri || |
| 2057 | Shift->getOperand(3).getImm() != 2 || |
| 2058 | !Shift->getOperand(2).isKill()) |
| 2059 | continue; |
| 2060 | IdxReg = Shift->getOperand(2).getReg(); |
| 2061 | unsigned ShiftedIdxReg = Shift->getOperand(0).getReg(); |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 2062 | |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 2063 | MachineInstr *Load = User.MI->getNextNode(); |
| 2064 | if (Load->getOpcode() != ARM::tLDRr) |
| 2065 | continue; |
| 2066 | if (Load->getOperand(1).getReg() != ShiftedIdxReg || |
| 2067 | Load->getOperand(2).getReg() != BaseReg || |
| 2068 | !Load->getOperand(1).isKill()) |
| 2069 | continue; |
| 2070 | |
| 2071 | // If we're in PIC mode, there should be another ADD following. |
| 2072 | if (isPositionIndependentOrROPI) { |
| 2073 | MachineInstr *Add = Load->getNextNode(); |
| 2074 | if (Add->getOpcode() != ARM::tADDrr || |
| 2075 | Add->getOperand(2).getReg() != Load->getOperand(0).getReg() || |
| 2076 | Add->getOperand(3).getReg() != BaseReg || |
| 2077 | !Add->getOperand(2).isKill()) |
| 2078 | continue; |
| 2079 | if (Add->getOperand(0).getReg() != MI->getOperand(0).getReg()) |
| 2080 | continue; |
| 2081 | |
| 2082 | Add->eraseFromParent(); |
| 2083 | DeadSize += 2; |
| 2084 | } else { |
| 2085 | if (Load->getOperand(0).getReg() != MI->getOperand(0).getReg()) |
| 2086 | continue; |
| 2087 | } |
| 2088 | |
| 2089 | |
| 2090 | // Now safe to delete the load and lsl. The LEA will be removed later. |
| 2091 | CanDeleteLEA = true; |
| 2092 | Shift->eraseFromParent(); |
| 2093 | Load->eraseFromParent(); |
| 2094 | DeadSize += 4; |
| 2095 | } |
| 2096 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 2097 | DEBUG(dbgs() << "Shrink JT: " << *MI); |
| 2098 | MachineInstr *CPEMI = User.CPEMI; |
| 2099 | unsigned Opc = ByteOk ? ARM::t2TBB_JT : ARM::t2TBH_JT; |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 2100 | if (!isThumb2) |
| 2101 | Opc = ByteOk ? ARM::tTBB_JT : ARM::tTBH_JT; |
| 2102 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 2103 | MachineBasicBlock::iterator MI_JT = MI; |
| 2104 | MachineInstr *NewJTMI = |
| Chad Rosier | 620fb22 | 2014-12-12 23:27:40 +0000 | [diff] [blame] | 2105 | BuildMI(*MBB, MI_JT, MI->getDebugLoc(), TII->get(Opc)) |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 2106 | .addReg(User.MI->getOperand(0).getReg(), |
| 2107 | getKillRegState(BaseRegKill)) |
| 2108 | .addReg(IdxReg, getKillRegState(IdxRegKill)) |
| 2109 | .addJumpTableIndex(JTI, JTOP.getTargetFlags()) |
| 2110 | .addImm(CPEMI->getOperand(0).getImm()); |
| 2111 | DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": " << *NewJTMI); |
| Evan Cheng | e64f48b | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 2112 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 2113 | unsigned JTOpc = ByteOk ? ARM::JUMPTABLE_TBB : ARM::JUMPTABLE_TBH; |
| 2114 | CPEMI->setDesc(TII->get(JTOpc)); |
| Evan Cheng | e64f48b | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 2115 | |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 2116 | if (jumpTableFollowsTB(MI, User.CPEMI)) { |
| 2117 | NewJTMI->getOperand(0).setReg(ARM::PC); |
| 2118 | NewJTMI->getOperand(0).setIsKill(false); |
| 2119 | |
| 2120 | if (CanDeleteLEA) { |
| 2121 | User.MI->eraseFromParent(); |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 2122 | DeadSize += isThumb2 ? 4 : 2; |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 2123 | |
| 2124 | // The LEA was eliminated, the TBB instruction becomes the only new user |
| 2125 | // of the jump table. |
| 2126 | User.MI = NewJTMI; |
| 2127 | User.MaxDisp = 4; |
| 2128 | User.NegOk = false; |
| 2129 | User.IsSoImm = false; |
| 2130 | User.KnownAlignment = false; |
| 2131 | } else { |
| 2132 | // The LEA couldn't be eliminated, so we must add another CPUser to |
| 2133 | // record the TBB or TBH use. |
| 2134 | int CPEntryIdx = JumpTableEntryIndices[JTI]; |
| 2135 | auto &CPEs = CPEntries[CPEntryIdx]; |
| David Majnemer | 562e829 | 2016-08-12 00:18:03 +0000 | [diff] [blame] | 2136 | auto Entry = |
| 2137 | find_if(CPEs, [&](CPEntry &E) { return E.CPEMI == User.CPEMI; }); |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 2138 | ++Entry->RefCount; |
| 2139 | CPUsers.emplace_back(CPUser(NewJTMI, User.CPEMI, 4, false, false)); |
| 2140 | } |
| Evan Cheng | c6d70ae | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 2141 | } |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 2142 | |
| Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 2143 | unsigned NewSize = TII->getInstSizeInBytes(*NewJTMI); |
| 2144 | unsigned OrigSize = TII->getInstSizeInBytes(*MI); |
| Tim Northover | a603c40 | 2015-05-31 19:22:07 +0000 | [diff] [blame] | 2145 | MI->eraseFromParent(); |
| 2146 | |
| 2147 | int Delta = OrigSize - NewSize + DeadSize; |
| 2148 | BBInfo[MBB->getNumber()].Size -= Delta; |
| 2149 | adjustBBOffsetsAfter(MBB); |
| 2150 | |
| 2151 | ++NumTBs; |
| 2152 | MadeChange = true; |
| Evan Cheng | c6d70ae | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
| 2155 | return MadeChange; |
| 2156 | } |
| Jim Grosbach | 8d92ec4 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 2157 | |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 2158 | /// reorderThumb2JumpTables - Adjust the function's block layout to ensure that |
| Jim Grosbach | 87b0f0d | 2009-11-16 18:55:47 +0000 | [diff] [blame] | 2159 | /// jump tables always branch forwards, since that's what tbb and tbh need. |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 2160 | bool ARMConstantIslands::reorderThumb2JumpTables() { |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 2161 | bool MadeChange = false; |
| 2162 | |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 2163 | MachineJumpTableInfo *MJTI = MF->getJumpTableInfo(); |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2164 | if (!MJTI) return false; |
| Jim Grosbach | e4ba2aa | 2010-07-07 21:06:51 +0000 | [diff] [blame] | 2165 | |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 2166 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 2167 | for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) { |
| 2168 | MachineInstr *MI = T2JumpTables[i]; |
| Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 2169 | const MCInstrDesc &MCID = MI->getDesc(); |
| 2170 | unsigned NumOps = MCID.getNumOperands(); |
| Tim Northover | 4998a47 | 2015-05-13 20:28:38 +0000 | [diff] [blame] | 2171 | unsigned JTOpIdx = NumOps - (MI->isPredicable() ? 2 : 1); |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 2172 | MachineOperand JTOP = MI->getOperand(JTOpIdx); |
| 2173 | unsigned JTI = JTOP.getIndex(); |
| 2174 | assert(JTI < JT.size()); |
| 2175 | |
| 2176 | // We prefer if target blocks for the jump table come after the jump |
| 2177 | // instruction so we can use TB[BH]. Loop through the target blocks |
| 2178 | // and try to adjust them such that that's true. |
| Jim Grosbach | 9785e59 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 2179 | int JTNumber = MI->getParent()->getNumber(); |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 2180 | const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs; |
| 2181 | for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) { |
| 2182 | MachineBasicBlock *MBB = JTBBs[j]; |
| Jim Grosbach | 9785e59 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 2183 | int DTNumber = MBB->getNumber(); |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 2184 | |
| Jim Grosbach | 9785e59 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 2185 | if (DTNumber < JTNumber) { |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 2186 | // The destination precedes the switch. Try to move the block forward |
| 2187 | // so we have a positive offset. |
| 2188 | MachineBasicBlock *NewBB = |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 2189 | adjustJTTargetBlockForward(MBB, MI->getParent()); |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 2190 | if (NewBB) |
| Jim Grosbach | 43d2108 | 2009-11-14 20:10:18 +0000 | [diff] [blame] | 2191 | MJTI->ReplaceMBBInJumpTable(JTI, JTBBs[j], NewBB); |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 2192 | MadeChange = true; |
| 2193 | } |
| 2194 | } |
| 2195 | } |
| 2196 | |
| 2197 | return MadeChange; |
| 2198 | } |
| 2199 | |
| Jim Grosbach | 8d92ec4 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 2200 | MachineBasicBlock *ARMConstantIslands:: |
| Jim Grosbach | 190e7b6 | 2012-03-23 23:07:03 +0000 | [diff] [blame] | 2201 | adjustJTTargetBlockForward(MachineBasicBlock *BB, MachineBasicBlock *JTBB) { |
| Jim Grosbach | 73ef80f | 2010-07-07 22:53:35 +0000 | [diff] [blame] | 2202 | // If the destination block is terminated by an unconditional branch, |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 2203 | // try to move it; otherwise, create a new block following the jump |
| Jim Grosbach | 9785e59 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 2204 | // table that branches back to the actual target. This is a very simple |
| 2205 | // heuristic. FIXME: We can definitely improve it. |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2206 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 2207 | SmallVector<MachineOperand, 4> Cond; |
| Jim Grosbach | af1ad30 | 2009-11-17 01:21:04 +0000 | [diff] [blame] | 2208 | SmallVector<MachineOperand, 4> CondPrior; |
| Duncan P. N. Exon Smith | 9f9559e | 2015-10-19 23:25:57 +0000 | [diff] [blame] | 2209 | MachineFunction::iterator BBi = BB->getIterator(); |
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 2210 | MachineFunction::iterator OldPrior = std::prev(BBi); |
| Jim Grosbach | 43d2108 | 2009-11-14 20:10:18 +0000 | [diff] [blame] | 2211 | |
| Jim Grosbach | 47d5e33 | 2009-11-16 17:10:56 +0000 | [diff] [blame] | 2212 | // If the block terminator isn't analyzable, don't try to move the block |
| Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 2213 | bool B = TII->analyzeBranch(*BB, TBB, FBB, Cond); |
| Jim Grosbach | 47d5e33 | 2009-11-16 17:10:56 +0000 | [diff] [blame] | 2214 | |
| Jim Grosbach | af1ad30 | 2009-11-17 01:21:04 +0000 | [diff] [blame] | 2215 | // If the block ends in an unconditional branch, move it. The prior block |
| 2216 | // has to have an analyzable terminator for us to move this one. Be paranoid |
| Jim Grosbach | 9785e59 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 2217 | // and make sure we're not trying to move the entry block of the function. |
| Duncan P. N. Exon Smith | e9bc579 | 2016-02-21 20:39:50 +0000 | [diff] [blame] | 2218 | if (!B && Cond.empty() && BB != &MF->front() && |
| Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 2219 | !TII->analyzeBranch(*OldPrior, TBB, FBB, CondPrior)) { |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 2220 | BB->moveAfter(JTBB); |
| 2221 | OldPrior->updateTerminator(); |
| Jim Grosbach | 43d2108 | 2009-11-14 20:10:18 +0000 | [diff] [blame] | 2222 | BB->updateTerminator(); |
| Jim Grosbach | 9785e59 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 2223 | // Update numbering to account for the block being moved. |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 2224 | MF->RenumberBlocks(); |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 2225 | ++NumJTMoved; |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2226 | return nullptr; |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 2227 | } |
| Jim Grosbach | 8d92ec4 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 2228 | |
| 2229 | // Create a new MBB for the code after the jump BB. |
| 2230 | MachineBasicBlock *NewBB = |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 2231 | MF->CreateMachineBasicBlock(JTBB->getBasicBlock()); |
| Duncan P. N. Exon Smith | 9f9559e | 2015-10-19 23:25:57 +0000 | [diff] [blame] | 2232 | MachineFunction::iterator MBBI = ++JTBB->getIterator(); |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 2233 | MF->insert(MBBI, NewBB); |
| Jim Grosbach | 8d92ec4 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 2234 | |
| 2235 | // Add an unconditional branch from NewBB to BB. |
| 2236 | // There doesn't seem to be meaningful DebugInfo available; this doesn't |
| 2237 | // correspond directly to anything in the source. |
| James Molloy | 70a3d6d | 2016-11-01 13:37:41 +0000 | [diff] [blame] | 2238 | if (isThumb2) |
| 2239 | BuildMI(NewBB, DebugLoc(), TII->get(ARM::t2B)) |
| 2240 | .addMBB(BB) |
| 2241 | .addImm(ARMCC::AL) |
| 2242 | .addReg(0); |
| 2243 | else |
| 2244 | BuildMI(NewBB, DebugLoc(), TII->get(ARM::tB)) |
| 2245 | .addMBB(BB) |
| 2246 | .addImm(ARMCC::AL) |
| 2247 | .addReg(0); |
| Jim Grosbach | 8d92ec4 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 2248 | |
| Jim Grosbach | 43d2108 | 2009-11-14 20:10:18 +0000 | [diff] [blame] | 2249 | // Update internal data structures to account for the newly inserted MBB. |
| Jakob Stoklund Olesen | 2a75997 | 2011-12-12 18:16:53 +0000 | [diff] [blame] | 2250 | MF->RenumberBlocks(NewBB); |
| Jim Grosbach | 43d2108 | 2009-11-14 20:10:18 +0000 | [diff] [blame] | 2251 | |
| Jim Grosbach | 8d92ec4 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 2252 | // Update the CFG. |
| 2253 | NewBB->addSuccessor(BB); |
| Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 2254 | JTBB->replaceSuccessor(BB, NewBB); |
| Jim Grosbach | 8d92ec4 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 2255 | |
| Jim Grosbach | 5d57714 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 2256 | ++NumJTInserted; |
| Jim Grosbach | 8d92ec4 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 2257 | return NewBB; |
| 2258 | } |