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