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