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