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