19a4d2e40a02b6e5fab5ccf1c768a52c76f3aec6aBill Wendling//===-- ARMConstantIslandPass.cpp - ARM constant islands ------------------===// 2a8e2989ece6dc46df59b0768184028257f913843Evan Cheng// 3a8e2989ece6dc46df59b0768184028257f913843Evan Cheng// The LLVM Compiler Infrastructure 4a8e2989ece6dc46df59b0768184028257f913843Evan Cheng// 54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source 64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details. 7a8e2989ece6dc46df59b0768184028257f913843Evan Cheng// 8a8e2989ece6dc46df59b0768184028257f913843Evan Cheng//===----------------------------------------------------------------------===// 9a8e2989ece6dc46df59b0768184028257f913843Evan Cheng// 10a8e2989ece6dc46df59b0768184028257f913843Evan Cheng// This file contains a pass that splits the constant pool up into 'islands' 11a8e2989ece6dc46df59b0768184028257f913843Evan Cheng// which are scattered through-out the function. This is required due to the 12a8e2989ece6dc46df59b0768184028257f913843Evan Cheng// limited pc-relative displacements that ARM has. 13a8e2989ece6dc46df59b0768184028257f913843Evan Cheng// 14a8e2989ece6dc46df59b0768184028257f913843Evan Cheng//===----------------------------------------------------------------------===// 15a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 16a8e2989ece6dc46df59b0768184028257f913843Evan Cheng#define DEBUG_TYPE "arm-cp-islands" 17a8e2989ece6dc46df59b0768184028257f913843Evan Cheng#include "ARM.h" 18af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng#include "ARMMachineFunctionInfo.h" 19719510a178a910feb1db707140011c32a30992c4Evan Cheng#include "Thumb2InstrInfo.h" 20ee04a6d3a40c3017124e3fd89a0db473a2824498Evan Cheng#include "MCTargetDesc/ARMAddressingModes.h" 21a8e2989ece6dc46df59b0768184028257f913843Evan Cheng#include "llvm/CodeGen/MachineConstantPool.h" 22a8e2989ece6dc46df59b0768184028257f913843Evan Cheng#include "llvm/CodeGen/MachineFunctionPass.h" 235657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng#include "llvm/CodeGen/MachineJumpTableInfo.h" 247a4c071cd9f67599eba21e902079d0e85f2abf97Jakob Stoklund Olesen#include "llvm/CodeGen/MachineRegisterInfo.h" 25a8e2989ece6dc46df59b0768184028257f913843Evan Cheng#include "llvm/Target/TargetData.h" 26a8e2989ece6dc46df59b0768184028257f913843Evan Cheng#include "llvm/Target/TargetMachine.h" 27a8e2989ece6dc46df59b0768184028257f913843Evan Cheng#include "llvm/Support/Debug.h" 28c25e7581b9b8088910da31702d4ca21c4734c6d7Torok Edwin#include "llvm/Support/ErrorHandling.h" 292d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen#include "llvm/Support/Format.h" 30705e07f578e2b3af47ddab610feb4e7f2d3063a5Chris Lattner#include "llvm/Support/raw_ostream.h" 31b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson#include "llvm/ADT/SmallSet.h" 32c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng#include "llvm/ADT/SmallVector.h" 33a8e2989ece6dc46df59b0768184028257f913843Evan Cheng#include "llvm/ADT/STLExtras.h" 34a8e2989ece6dc46df59b0768184028257f913843Evan Cheng#include "llvm/ADT/Statistic.h" 351fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach#include "llvm/Support/CommandLine.h" 36b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson#include <algorithm> 37a8e2989ece6dc46df59b0768184028257f913843Evan Chengusing namespace llvm; 38a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 39a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan ChengSTATISTIC(NumCPEs, "Number of constpool entries"); 40a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan ChengSTATISTIC(NumSplit, "Number of uncond branches inserted"); 41a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan ChengSTATISTIC(NumCBrFixed, "Number of cond branches fixed"); 42a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan ChengSTATISTIC(NumUBrFixed, "Number of uncond branches fixed"); 43a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan ChengSTATISTIC(NumTBs, "Number of table branches generated"); 44a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan ChengSTATISTIC(NumT2CPShrunk, "Number of Thumb2 constantpool instructions shrunk"); 4531b99dd76035067b1c3cc6c7e9d663b7b0210938Evan ChengSTATISTIC(NumT2BrShrunk, "Number of Thumb2 immediate branches shrunk"); 46de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan ChengSTATISTIC(NumCBZ, "Number of CBZ / CBNZ formed"); 471fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim GrosbachSTATISTIC(NumJTMoved, "Number of jump table destination blocks moved"); 4880697d1b266cd3cd3b92edd424d66d573db6b4b1Jim GrosbachSTATISTIC(NumJTInserted, "Number of jump table intermediate blocks inserted"); 491fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach 501fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach 511fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbachstatic cl::opt<bool> 52f04777b945e272ccec21c7080bd5480d78a14fd2Jim GrosbachAdjustJumpTableBlocks("arm-adjust-jump-tables", cl::Hidden, cl::init(true), 531fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach cl::desc("Adjust basic block layout to better use TB[BH]")); 54a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 55f5bb45f89564129fcaca25b9dd84750ffc02beedJakob Stoklund Olesen// FIXME: This option should be removed once it has received sufficient testing. 56b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesenstatic cl::opt<bool> 57b6ff6ec85e589b8ad3bede4533dc71a09655bb1eJakob Stoklund OlesenAlignConstantIslands("arm-align-constant-islands", cl::Hidden, cl::init(true), 58b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen cl::desc("Align constant islands in code")); 59b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen 6077caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen/// UnknownPadding - Return the worst case padding that could result from 6177caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen/// unknown offset bits. This does not include alignment padding caused by 6277caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen/// known offset bits. 6377caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen/// 6477caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen/// @param LogAlign log2(alignment) 6577caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen/// @param KnownBits Number of known low offset bits. 6677caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesenstatic inline unsigned UnknownPadding(unsigned LogAlign, unsigned KnownBits) { 6777caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen if (KnownBits < LogAlign) 6877caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen return (1u << LogAlign) - (1u << KnownBits); 6977caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen return 0; 7077caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen} 7177caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen 72a8e2989ece6dc46df59b0768184028257f913843Evan Chengnamespace { 7388e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen /// ARMConstantIslands - Due to limited PC-relative displacements, ARM 74a8e2989ece6dc46df59b0768184028257f913843Evan Cheng /// requires constant pool entries to be scattered among the instructions 75a8e2989ece6dc46df59b0768184028257f913843Evan Cheng /// inside a function. To do this, it completely ignores the normal LLVM 7688e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen /// constant pool; instead, it places constants wherever it feels like with 77a8e2989ece6dc46df59b0768184028257f913843Evan Cheng /// special instructions. 78a8e2989ece6dc46df59b0768184028257f913843Evan Cheng /// 79a8e2989ece6dc46df59b0768184028257f913843Evan Cheng /// The terminology used in this pass includes: 80a8e2989ece6dc46df59b0768184028257f913843Evan Cheng /// Islands - Clumps of constants placed in the function. 81a8e2989ece6dc46df59b0768184028257f913843Evan Cheng /// Water - Potential places where an island could be formed. 82a8e2989ece6dc46df59b0768184028257f913843Evan Cheng /// CPE - A constant pool entry that has been placed somewhere, which 83a8e2989ece6dc46df59b0768184028257f913843Evan Cheng /// tracks a list of users. 846726b6d75a8b679068a58cb954ba97cf9d1690baNick Lewycky class ARMConstantIslands : public MachineFunctionPass { 85a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen /// BasicBlockInfo - Information about the offset and size of a single 86a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen /// basic block. 87a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen struct BasicBlockInfo { 88a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen /// Offset - Distance from the beginning of the function to the beginning 89a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen /// of this basic block. 90a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen /// 91456ff46e668f361f79e8e91f145152089cd7d933Jakob Stoklund Olesen /// Offsets are computed assuming worst case padding before an aligned 92456ff46e668f361f79e8e91f145152089cd7d933Jakob Stoklund Olesen /// block. This means that subtracting basic block offsets always gives a 93456ff46e668f361f79e8e91f145152089cd7d933Jakob Stoklund Olesen /// conservative estimate of the real distance which may be smaller. 94456ff46e668f361f79e8e91f145152089cd7d933Jakob Stoklund Olesen /// 95456ff46e668f361f79e8e91f145152089cd7d933Jakob Stoklund Olesen /// Because worst case padding is used, the computed offset of an aligned 96456ff46e668f361f79e8e91f145152089cd7d933Jakob Stoklund Olesen /// block may not actually be aligned. 97a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen unsigned Offset; 98a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen 99a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen /// Size - Size of the basic block in bytes. If the block contains 100a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen /// inline assembly, this is a worst case estimate. 101a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen /// 102540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen /// The size does not include any alignment padding whether from the 103540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen /// beginning of the block, or from an aligned jump table at the end. 104a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen unsigned Size; 105a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen 106540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen /// KnownBits - The number of low bits in Offset that are known to be 107540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen /// exact. The remaining bits of Offset are an upper bound. 108540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen uint8_t KnownBits; 109540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen 110a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen /// Unalign - When non-zero, the block contains instructions (inline asm) 111a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen /// of unknown size. The real size may be smaller than Size bytes by a 112a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen /// multiple of 1 << Unalign. 113a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen uint8_t Unalign; 114a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen 115a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen /// PostAlign - When non-zero, the block terminator contains a .align 116a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen /// directive, so the end of the block is aligned to 1 << PostAlign 117a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen /// bytes. 118a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen uint8_t PostAlign; 119a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen 120540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen BasicBlockInfo() : Offset(0), Size(0), KnownBits(0), Unalign(0), 121540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen PostAlign(0) {} 1225bb32530bc1adb970eee2ac18508ea2876a2f410Jakob Stoklund Olesen 12377caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen /// Compute the number of known offset bits internally to this block. 12477caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen /// This number should be used to predict worst case padding when 12577caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen /// splitting the block. 12677caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen unsigned internalKnownBits() const { 127887d095fb6c6fb0980ab90a9211fb2420d0da7c0Jakob Stoklund Olesen unsigned Bits = Unalign ? Unalign : KnownBits; 128887d095fb6c6fb0980ab90a9211fb2420d0da7c0Jakob Stoklund Olesen // If the block size isn't a multiple of the known bits, assume the 129887d095fb6c6fb0980ab90a9211fb2420d0da7c0Jakob Stoklund Olesen // worst case padding. 130887d095fb6c6fb0980ab90a9211fb2420d0da7c0Jakob Stoklund Olesen if (Size & ((1u << Bits) - 1)) 131887d095fb6c6fb0980ab90a9211fb2420d0da7c0Jakob Stoklund Olesen Bits = CountTrailingZeros_32(Size); 132887d095fb6c6fb0980ab90a9211fb2420d0da7c0Jakob Stoklund Olesen return Bits; 13377caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen } 13477caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen 1358552821e578d693ec14007b21e9468010485ae76Jakob Stoklund Olesen /// Compute the offset immediately following this block. If LogAlign is 1368552821e578d693ec14007b21e9468010485ae76Jakob Stoklund Olesen /// specified, return the offset the successor block will get if it has 1378552821e578d693ec14007b21e9468010485ae76Jakob Stoklund Olesen /// this alignment. 1388552821e578d693ec14007b21e9468010485ae76Jakob Stoklund Olesen unsigned postOffset(unsigned LogAlign = 0) const { 139540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen unsigned PO = Offset + Size; 1408552821e578d693ec14007b21e9468010485ae76Jakob Stoklund Olesen unsigned LA = std::max(unsigned(PostAlign), LogAlign); 1418552821e578d693ec14007b21e9468010485ae76Jakob Stoklund Olesen if (!LA) 142540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen return PO; 143540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen // Add alignment padding from the terminator. 144456ff46e668f361f79e8e91f145152089cd7d933Jakob Stoklund Olesen return PO + UnknownPadding(LA, internalKnownBits()); 145540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen } 146540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen 147540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen /// Compute the number of known low bits of postOffset. If this block 148540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen /// contains inline asm, the number of known bits drops to the 149540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen /// instruction alignment. An aligned terminator may increase the number 150540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen /// of know bits. 1518552821e578d693ec14007b21e9468010485ae76Jakob Stoklund Olesen /// If LogAlign is given, also consider the alignment of the next block. 1528552821e578d693ec14007b21e9468010485ae76Jakob Stoklund Olesen unsigned postKnownBits(unsigned LogAlign = 0) const { 1538552821e578d693ec14007b21e9468010485ae76Jakob Stoklund Olesen return std::max(std::max(unsigned(PostAlign), LogAlign), 1548552821e578d693ec14007b21e9468010485ae76Jakob Stoklund Olesen internalKnownBits()); 155540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen } 156a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen }; 1578494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 158a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen std::vector<BasicBlockInfo> BBInfo; 15999c49a4b94ffabdd22f55e8274c7f92892e25559Dale Johannesen 160a8e2989ece6dc46df59b0768184028257f913843Evan Cheng /// WaterList - A sorted list of basic blocks where islands could be placed 161a8e2989ece6dc46df59b0768184028257f913843Evan Cheng /// (i.e. blocks that don't fall through to the following block, due 162a8e2989ece6dc46df59b0768184028257f913843Evan Cheng /// to a return, unreachable, or unconditional branch). 163e03cff6812ac41598e2bca7854985dc821a07f44Evan Cheng std::vector<MachineBasicBlock*> WaterList; 164c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng 165b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson /// NewWaterList - The subset of WaterList that was created since the 166b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson /// previous iteration by inserting unconditional branches. 167b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson SmallSet<MachineBasicBlock*, 4> NewWaterList; 168b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson 169034de5f65f9139954cb01d94eaebbaefd294946eBob Wilson typedef std::vector<MachineBasicBlock*>::iterator water_iterator; 170034de5f65f9139954cb01d94eaebbaefd294946eBob Wilson 171a8e2989ece6dc46df59b0768184028257f913843Evan Cheng /// CPUser - One user of a constant pool, keeping the machine instruction 172a8e2989ece6dc46df59b0768184028257f913843Evan Cheng /// pointer, the constant pool being referenced, and the max displacement 173549dda9d82a5ec0a0cbf429a18d33a8e66062424Bob Wilson /// allowed from the instruction to the CP. The HighWaterMark records the 174549dda9d82a5ec0a0cbf429a18d33a8e66062424Bob Wilson /// highest basic block where a new CPEntry can be placed. To ensure this 175549dda9d82a5ec0a0cbf429a18d33a8e66062424Bob Wilson /// pass terminates, the CP entries are initially placed at the end of the 176549dda9d82a5ec0a0cbf429a18d33a8e66062424Bob Wilson /// function and then move monotonically to lower addresses. The 177549dda9d82a5ec0a0cbf429a18d33a8e66062424Bob Wilson /// exception to this rule is when the current CP entry for a particular 178549dda9d82a5ec0a0cbf429a18d33a8e66062424Bob Wilson /// CPUser is out of range, but there is another CP entry for the same 179549dda9d82a5ec0a0cbf429a18d33a8e66062424Bob Wilson /// constant value in range. We want to use the existing in-range CP 180549dda9d82a5ec0a0cbf429a18d33a8e66062424Bob Wilson /// entry, but if it later moves out of range, the search for new water 181549dda9d82a5ec0a0cbf429a18d33a8e66062424Bob Wilson /// should resume where it left off. The HighWaterMark is used to record 182549dda9d82a5ec0a0cbf429a18d33a8e66062424Bob Wilson /// that point. 183a8e2989ece6dc46df59b0768184028257f913843Evan Cheng struct CPUser { 184a8e2989ece6dc46df59b0768184028257f913843Evan Cheng MachineInstr *MI; 185a8e2989ece6dc46df59b0768184028257f913843Evan Cheng MachineInstr *CPEMI; 186549dda9d82a5ec0a0cbf429a18d33a8e66062424Bob Wilson MachineBasicBlock *HighWaterMark; 187dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen private: 188a8e2989ece6dc46df59b0768184028257f913843Evan Cheng unsigned MaxDisp; 189dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen public: 1905d8f1cae76e4e424bf3b4392eddb3f4dbf38662aEvan Cheng bool NegOk; 191d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng bool IsSoImm; 192dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen bool KnownAlignment; 193d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp, 194d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng bool neg, bool soimm) 195dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp), NegOk(neg), IsSoImm(soimm), 196dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen KnownAlignment(false) { 197549dda9d82a5ec0a0cbf429a18d33a8e66062424Bob Wilson HighWaterMark = CPEMI->getParent(); 198549dda9d82a5ec0a0cbf429a18d33a8e66062424Bob Wilson } 199dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen /// getMaxDisp - Returns the maximum displacement supported by MI. 200dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen /// Correct for unknown alignment. 2013ee3661f8f10e7f82094a89c40d9118630ab0a40Jakob Stoklund Olesen /// Conservatively subtract 2 bytes to handle weird alignment effects. 202dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen unsigned getMaxDisp() const { 2033ee3661f8f10e7f82094a89c40d9118630ab0a40Jakob Stoklund Olesen return (KnownAlignment ? MaxDisp : MaxDisp - 2) - 2; 204dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen } 205a8e2989ece6dc46df59b0768184028257f913843Evan Cheng }; 2068494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 207a8e2989ece6dc46df59b0768184028257f913843Evan Cheng /// CPUsers - Keep track of all of the machine instructions that use various 208a8e2989ece6dc46df59b0768184028257f913843Evan Cheng /// constant pools and their max displacement. 209e03cff6812ac41598e2bca7854985dc821a07f44Evan Cheng std::vector<CPUser> CPUsers; 2108494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 211c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng /// CPEntry - One per constant pool entry, keeping the machine instruction 212c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng /// pointer, the constpool index, and the number of CPUser's which 213c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng /// reference this entry. 214c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng struct CPEntry { 215c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng MachineInstr *CPEMI; 216c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng unsigned CPI; 217c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng unsigned RefCount; 218c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0) 219c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng : CPEMI(cpemi), CPI(cpi), RefCount(rc) {} 220c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng }; 221c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng 222c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng /// CPEntries - Keep track of all of the constant pool entry machine 22388e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen /// instructions. For each original constpool index (i.e. those that 22488e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen /// existed upon entry to this pass), it keeps a vector of entries. 22588e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen /// Original elements are cloned as we go along; the clones are 22688e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen /// put in the vector of the original element, but have distinct CPIs. 227c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng std::vector<std::vector<CPEntry> > CPEntries; 2288494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 229af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng /// ImmBranch - One per immediate branch, keeping the machine instruction 230af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng /// pointer, conditional or unconditional, the max displacement, 231af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng /// and (if isCond is true) the corresponding unconditional branch 232af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng /// opcode. 233af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng struct ImmBranch { 234af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng MachineInstr *MI; 235c285414988ba026d01e5d8acc07a21cd06fd5732Evan Cheng unsigned MaxDisp : 31; 236c285414988ba026d01e5d8acc07a21cd06fd5732Evan Cheng bool isCond : 1; 237af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng int UncondBr; 238c285414988ba026d01e5d8acc07a21cd06fd5732Evan Cheng ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr) 239c285414988ba026d01e5d8acc07a21cd06fd5732Evan Cheng : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {} 240af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng }; 241af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng 2422706f9771de8e90ee6379e70afedd91892bb9c8cEvan Cheng /// ImmBranches - Keep track of all the immediate branch instructions. 243af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng /// 244e03cff6812ac41598e2bca7854985dc821a07f44Evan Cheng std::vector<ImmBranch> ImmBranches; 245af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng 246d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng /// PushPopMIs - Keep track of all the Thumb push / pop instructions. 247d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng /// 248c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng SmallVector<MachineInstr*, 4> PushPopMIs; 249d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng 2505657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng /// T2JumpTables - Keep track of all the Thumb2 jumptable instructions. 2515657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng SmallVector<MachineInstr*, 4> T2JumpTables; 2525657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng 253d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng /// HasFarJump - True if any far jump instruction has been emitted during 254d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng /// the branch fix up pass. 255d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng bool HasFarJump; 256d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng 257dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MachineFunction *MF; 258dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MachineConstantPool *MCP; 259acf2077ca497980a066e8e7bb81ceec0de82d5daCraig Topper const ARMBaseInstrInfo *TII; 26025f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng const ARMSubtarget *STI; 2618593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen ARMFunctionInfo *AFI; 262b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen bool isThumb; 263d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng bool isThumb1; 2645e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin bool isThumb2; 265a8e2989ece6dc46df59b0768184028257f913843Evan Cheng public: 2661997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patel static char ID; 26790c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson ARMConstantIslands() : MachineFunctionPass(ID) {} 268794fd75c67a2cdc128d67342c6d88a504d186896Devang Patel 2695657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng virtual bool runOnMachineFunction(MachineFunction &MF); 270a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 271a8e2989ece6dc46df59b0768184028257f913843Evan Cheng virtual const char *getPassName() const { 272af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng return "ARM constant island placement and branch shortening pass"; 273a8e2989ece6dc46df59b0768184028257f913843Evan Cheng } 2748494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 275a8e2989ece6dc46df59b0768184028257f913843Evan Cheng private: 2767a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach void doInitialPlacement(std::vector<MachineInstr*> &CPEMIs); 277c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI); 278cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen unsigned getCPELogAlign(const MachineInstr *CPEMI); 2797a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach void scanFunctionJumpTables(); 2807a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach void initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs); 2817a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach MachineBasicBlock *splitBlockBeforeInstr(MachineInstr *MI); 2827a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach void updateForInsertedWaterBlock(MachineBasicBlock *NewBB); 2837a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach void adjustBBOffsetsAfter(MachineBasicBlock *BB); 2847a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool decrementCPEReferenceCount(unsigned CPI, MachineInstr* CPEMI); 2857a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach int findInRangeCPEntry(CPUser& U, unsigned UserOffset); 2867a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool findAvailableWater(CPUser&U, unsigned UserOffset, 2877a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach water_iterator &WaterIter); 2887a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach void createNewWater(unsigned CPUserIndex, unsigned UserOffset, 289757652c5ba820bbea75d8713d7bf3749e4113778Bob Wilson MachineBasicBlock *&NewMBB); 2907a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool handleConstantPoolUser(unsigned CPUserIndex); 2917a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach void removeDeadCPEMI(MachineInstr *CPEMI); 2927a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool removeUnusedCPEntries(); 2937a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool isCPEntryInRange(MachineInstr *MI, unsigned UserOffset, 2947a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach MachineInstr *CPEMI, unsigned Disp, bool NegOk, 2957a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool DoDump = false); 2967a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool isWaterInRange(unsigned UserOffset, MachineBasicBlock *Water, 2972e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen CPUser &U, unsigned &Growth); 2987a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool isBBInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp); 2997a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool fixupImmediateBr(ImmBranch &Br); 3007a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool fixupConditionalBr(ImmBranch &Br); 3017a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool fixupUnconditionalBr(ImmBranch &Br); 3027a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool undoLRSpillRestore(); 30319d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesen bool mayOptimizeThumb2Instruction(const MachineInstr *MI) const; 3047a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool optimizeThumb2Instructions(); 3057a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool optimizeThumb2Branches(); 3067a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool reorderThumb2JumpTables(); 3077a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool optimizeThumb2JumpTables(); 3087a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach MachineBasicBlock *adjustJTTargetBlockForward(MachineBasicBlock *BB, 3091fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach MachineBasicBlock *JTBB); 310a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 3117a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach void computeBlockSize(MachineBasicBlock *MBB); 3127a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach unsigned getOffsetOf(MachineInstr *MI) const; 3137a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach unsigned getUserOffset(CPUser&) const; 3148593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen void dumpBBs(); 315dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen void verify(); 316493ad6b95d153763b4b4fec456918f6c0d72d1eaJakob Stoklund Olesen 3177a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset, 318493ad6b95d153763b4b4fec456918f6c0d72d1eaJakob Stoklund Olesen unsigned Disp, bool NegativeOK, bool IsSoImm = false); 3197a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset, 320493ad6b95d153763b4b4fec456918f6c0d72d1eaJakob Stoklund Olesen const CPUser &U) { 3217a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach return isOffsetInRange(UserOffset, TrialOffset, 322dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen U.getMaxDisp(), U.NegOk, U.IsSoImm); 323493ad6b95d153763b4b4fec456918f6c0d72d1eaJakob Stoklund Olesen } 324a8e2989ece6dc46df59b0768184028257f913843Evan Cheng }; 3251997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patel char ARMConstantIslands::ID = 0; 326a8e2989ece6dc46df59b0768184028257f913843Evan Cheng} 327a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 3288593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen/// verify - check BBOffsets, BBSizes, alignment of islands 329dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesenvoid ARMConstantIslands::verify() { 330d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng#ifndef NDEBUG 331dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end(); 332d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng MBBI != E; ++MBBI) { 333d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng MachineBasicBlock *MBB = MBBI; 33499486be8bad67f70445df99fd0c07d17eb8b1c05Jakob Stoklund Olesen unsigned MBBId = MBB->getNumber(); 33599486be8bad67f70445df99fd0c07d17eb8b1c05Jakob Stoklund Olesen assert(!MBBId || BBInfo[MBBId - 1].postOffset() <= BBInfo[MBBId].Offset); 3368593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen } 337101c03a8c9042781b6c9ba1fcc366e14fc8534ceJakob Stoklund Olesen DEBUG(dbgs() << "Verifying " << CPUsers.size() << " CP users.\n"); 3384d8e90a026a29a812616690bd77b72e0b5ae6c75Jim Grosbach for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) { 3394d8e90a026a29a812616690bd77b72e0b5ae6c75Jim Grosbach CPUser &U = CPUsers[i]; 3407a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach unsigned UserOffset = getUserOffset(U); 3413ee3661f8f10e7f82094a89c40d9118630ab0a40Jakob Stoklund Olesen // Verify offset using the real max displacement without the safety 3423ee3661f8f10e7f82094a89c40d9118630ab0a40Jakob Stoklund Olesen // adjustment. 3433ee3661f8f10e7f82094a89c40d9118630ab0a40Jakob Stoklund Olesen if (isCPEntryInRange(U.MI, UserOffset, U.CPEMI, U.getMaxDisp()+2, U.NegOk, 344101c03a8c9042781b6c9ba1fcc366e14fc8534ceJakob Stoklund Olesen /* DoDump = */ true)) { 345101c03a8c9042781b6c9ba1fcc366e14fc8534ceJakob Stoklund Olesen DEBUG(dbgs() << "OK\n"); 346101c03a8c9042781b6c9ba1fcc366e14fc8534ceJakob Stoklund Olesen continue; 347101c03a8c9042781b6c9ba1fcc366e14fc8534ceJakob Stoklund Olesen } 348101c03a8c9042781b6c9ba1fcc366e14fc8534ceJakob Stoklund Olesen DEBUG(dbgs() << "Out of range.\n"); 349101c03a8c9042781b6c9ba1fcc366e14fc8534ceJakob Stoklund Olesen dumpBBs(); 350101c03a8c9042781b6c9ba1fcc366e14fc8534ceJakob Stoklund Olesen DEBUG(MF->dump()); 351101c03a8c9042781b6c9ba1fcc366e14fc8534ceJakob Stoklund Olesen llvm_unreachable("Constant pool entry out of range!"); 3524d8e90a026a29a812616690bd77b72e0b5ae6c75Jim Grosbach } 353a9562568e5c2a897e3a51b18700eeb70f0dda48cJim Grosbach#endif 3548593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen} 3558593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen 3568593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen/// print block size and offset information - debugging 3578593e418555fdae21a070357fd8734b4069c17d0Dale Johannesenvoid ARMConstantIslands::dumpBBs() { 3582d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen DEBUG({ 3592d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen for (unsigned J = 0, E = BBInfo.size(); J !=E; ++J) { 3602d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen const BasicBlockInfo &BBI = BBInfo[J]; 3612d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen dbgs() << format("%08x BB#%u\t", BBI.Offset, J) 3622d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen << " kb=" << unsigned(BBI.KnownBits) 3632d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen << " ua=" << unsigned(BBI.Unalign) 3642d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen << " pa=" << unsigned(BBI.PostAlign) 3652d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen << format(" size=%#x\n", BBInfo[J].Size); 3662d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen } 3672d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen }); 3688593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen} 3698593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen 370af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng/// createARMConstantIslandPass - returns an instance of the constpool 371af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng/// island pass. 372a8e2989ece6dc46df59b0768184028257f913843Evan ChengFunctionPass *llvm::createARMConstantIslandPass() { 373a8e2989ece6dc46df59b0768184028257f913843Evan Cheng return new ARMConstantIslands(); 374a8e2989ece6dc46df59b0768184028257f913843Evan Cheng} 375a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 376dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesenbool ARMConstantIslands::runOnMachineFunction(MachineFunction &mf) { 377dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MF = &mf; 378dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MCP = mf.getConstantPool(); 3798494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 3802d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen DEBUG(dbgs() << "***** ARMConstantIslands: " 381dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen << MCP->getConstants().size() << " CP entries, aligned to " 382dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen << MCP->getConstantPoolAlignment() << " bytes *****\n"); 3832d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen 384acf2077ca497980a066e8e7bb81ceec0de82d5daCraig Topper TII = (const ARMBaseInstrInfo*)MF->getTarget().getInstrInfo(); 385dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen AFI = MF->getInfo<ARMFunctionInfo>(); 386dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen STI = &MF->getTarget().getSubtarget<ARMSubtarget>(); 38725f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng 388b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen isThumb = AFI->isThumbFunction(); 389d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng isThumb1 = AFI->isThumb1OnlyFunction(); 3905e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin isThumb2 = AFI->isThumb2Function(); 391d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng 392d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng HasFarJump = false; 393d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng 3947a4c071cd9f67599eba21e902079d0e85f2abf97Jakob Stoklund Olesen // This pass invalidates liveness information when it splits basic blocks. 3957a4c071cd9f67599eba21e902079d0e85f2abf97Jakob Stoklund Olesen MF->getRegInfo().invalidateLiveness(); 3967a4c071cd9f67599eba21e902079d0e85f2abf97Jakob Stoklund Olesen 397a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Renumber all of the machine basic blocks in the function, guaranteeing that 398a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // the numbers agree with the position of the block in the function. 399dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MF->RenumberBlocks(); 400a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 40180697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach // Try to reorder and otherwise adjust the block layout to make good use 40280697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach // of the TB[BH] instructions. 40380697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach bool MadeChange = false; 40480697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach if (isThumb2 && AdjustJumpTableBlocks) { 4057a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach scanFunctionJumpTables(); 4067a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach MadeChange |= reorderThumb2JumpTables(); 40780697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach // Data is out of date, so clear it. It'll be re-computed later. 40880697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach T2JumpTables.clear(); 40980697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach // Blocks may have shifted around. Keep the numbering up to date. 410dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MF->RenumberBlocks(); 41180697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach } 41280697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach 413d26b14c34cbcee1448b86b524578fc51cc979023Evan Cheng // Thumb1 functions containing constant pools get 4-byte alignment. 414d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng // This is so we can keep exact track of where the alignment padding goes. 415d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng 4167d7dab02783fb4f1f5d0cf274c52a4fb059bfbeaChris Lattner // ARM and Thumb2 functions need to be 4-byte aligned. 4177d7dab02783fb4f1f5d0cf274c52a4fb059bfbeaChris Lattner if (!isThumb1) 41870cfaa34645bf8ad232c400f0fe7c48a9d715586Chad Rosier MF->ensureAlignment(2); // 2 = log2(4) 41956c42ef3e43f8eb75c435e781d0ac8251c7588a1Dale Johannesen 420a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Perform the initial placement of the constant pool entries. To start with, 421a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // we put them all at the end of the function. 422e03cff6812ac41598e2bca7854985dc821a07f44Evan Cheng std::vector<MachineInstr*> CPEMIs; 423cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen if (!MCP->isEmpty()) 4247a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach doInitialPlacement(CPEMIs); 4258494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 426a8e2989ece6dc46df59b0768184028257f913843Evan Cheng /// The next UID to take is the first unused one. 4275de5d4b6d0eb3fd379fa571d82f6fa764460b3b8Evan Cheng AFI->initPICLabelUId(CPEMIs.size()); 4288494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 429a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Do the initial scan of the function, building up information about the 430a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // sizes of each block, the location of all the water, and finding all of the 431a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // constant pool users. 4327a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach initializeFunctionInfo(CPEMIs); 433a8e2989ece6dc46df59b0768184028257f913843Evan Cheng CPEMIs.clear(); 4348086d5800dbe8cc392ce217adccfaca915858aedDale Johannesen DEBUG(dumpBBs()); 4358086d5800dbe8cc392ce217adccfaca915858aedDale Johannesen 4368494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 437ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng /// Remove dead constant pool entries. 4387a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach MadeChange |= removeUnusedCPEntries(); 439ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng 440d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng // Iteratively place constant pool entries and fix up branches until there 441d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng // is no change. 442b6879b2b84df2f642cd39f5bf58584c4a39f8080Evan Cheng unsigned NoCPIters = 0, NoBRIters = 0; 443d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng while (true) { 4443c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen DEBUG(dbgs() << "Beginning CP iteration #" << NoCPIters << '\n'); 445b6879b2b84df2f642cd39f5bf58584c4a39f8080Evan Cheng bool CPChange = false; 446a8e2989ece6dc46df59b0768184028257f913843Evan Cheng for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) 4477a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach CPChange |= handleConstantPoolUser(i); 448b6879b2b84df2f642cd39f5bf58584c4a39f8080Evan Cheng if (CPChange && ++NoCPIters > 30) 449169db15717e77f2893d1868ef934ce42c2e9e956Jakob Stoklund Olesen report_fatal_error("Constant Island pass failed to converge!"); 4508202010364e1bfe6111f1ce62f154499b80877fbEvan Cheng DEBUG(dumpBBs()); 45126b8ef53ff2ff1ffd72d140b5be264e32dee0195Jim Grosbach 452b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // Clear NewWaterList now. If we split a block for branches, it should 453b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // appear as "new water" for the next iteration of constant pool placement. 454b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson NewWaterList.clear(); 455b6879b2b84df2f642cd39f5bf58584c4a39f8080Evan Cheng 4563c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen DEBUG(dbgs() << "Beginning BR iteration #" << NoBRIters << '\n'); 457b6879b2b84df2f642cd39f5bf58584c4a39f8080Evan Cheng bool BRChange = false; 458af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) 4597a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach BRChange |= fixupImmediateBr(ImmBranches[i]); 460b6879b2b84df2f642cd39f5bf58584c4a39f8080Evan Cheng if (BRChange && ++NoBRIters > 30) 461169db15717e77f2893d1868ef934ce42c2e9e956Jakob Stoklund Olesen report_fatal_error("Branch Fix Up pass failed to converge!"); 4628202010364e1bfe6111f1ce62f154499b80877fbEvan Cheng DEBUG(dumpBBs()); 463b6879b2b84df2f642cd39f5bf58584c4a39f8080Evan Cheng 464b6879b2b84df2f642cd39f5bf58584c4a39f8080Evan Cheng if (!CPChange && !BRChange) 465d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng break; 466d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng MadeChange = true; 467d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng } 468ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng 469a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng // Shrink 32-bit Thumb2 branch, load, and store instructions. 470e44be6381609d31b2d8879dbd5107e01404fa475Evan Cheng if (isThumb2 && !STI->prefers32BitThumb()) 4717a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach MadeChange |= optimizeThumb2Instructions(); 47225f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng 4738593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen // After a while, this might be made debug-only, but it is not expensive. 474dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen verify(); 4758593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen 47626b8ef53ff2ff1ffd72d140b5be264e32dee0195Jim Grosbach // If LR has been forced spilled and no far jump (i.e. BL) has been issued, 47726b8ef53ff2ff1ffd72d140b5be264e32dee0195Jim Grosbach // undo the spill / restore of LR if possible. 4785657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng if (isThumb && !HasFarJump && AFI->isLRSpilledForFarJump()) 4797a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach MadeChange |= undoLRSpillRestore(); 480d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng 48198b928ea71576c243bdb203879d966be9cde5f3cAnton Korobeynikov // Save the mapping between original and cloned constpool entries. 48298b928ea71576c243bdb203879d966be9cde5f3cAnton Korobeynikov for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) { 48398b928ea71576c243bdb203879d966be9cde5f3cAnton Korobeynikov for (unsigned j = 0, je = CPEntries[i].size(); j != je; ++j) { 48498b928ea71576c243bdb203879d966be9cde5f3cAnton Korobeynikov const CPEntry & CPE = CPEntries[i][j]; 48598b928ea71576c243bdb203879d966be9cde5f3cAnton Korobeynikov AFI->recordCPEClone(i, CPE.CPI); 48698b928ea71576c243bdb203879d966be9cde5f3cAnton Korobeynikov } 48798b928ea71576c243bdb203879d966be9cde5f3cAnton Korobeynikov } 48898b928ea71576c243bdb203879d966be9cde5f3cAnton Korobeynikov 4893c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen DEBUG(dbgs() << '\n'; dumpBBs()); 490b1c857bb7b3d7bc187de4e97439eb212a8604bf0Evan Cheng 491a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen BBInfo.clear(); 492a8e2989ece6dc46df59b0768184028257f913843Evan Cheng WaterList.clear(); 493a8e2989ece6dc46df59b0768184028257f913843Evan Cheng CPUsers.clear(); 494c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng CPEntries.clear(); 495af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng ImmBranches.clear(); 496c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng PushPopMIs.clear(); 4975657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng T2JumpTables.clear(); 498d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng 499d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng return MadeChange; 500a8e2989ece6dc46df59b0768184028257f913843Evan Cheng} 501a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 5027a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// doInitialPlacement - Perform the initial placement of the constant pool 503a8e2989ece6dc46df59b0768184028257f913843Evan Cheng/// entries. To start with, we put them all at the end of the function. 504dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesenvoid 5057a465250564d2ae0cc53ebe58732a751daa43e8dJim GrosbachARMConstantIslands::doInitialPlacement(std::vector<MachineInstr*> &CPEMIs) { 506a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Create the basic block to hold the CPE's. 507dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MachineBasicBlock *BB = MF->CreateMachineBasicBlock(); 508dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MF->push_back(BB); 5098494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 510b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen // MachineConstantPool measures alignment in bytes. We measure in log2(bytes). 5115e46dcbb4b6f6e82f7bb88490800ed09ac02f105Jakob Stoklund Olesen unsigned MaxAlign = Log2_32(MCP->getConstantPoolAlignment()); 512b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen 513b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen // Mark the basic block as required by the const-pool. 514b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen // If AlignConstantIslands isn't set, use 4-byte alignment for everything. 515b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen BB->setAlignment(AlignConstantIslands ? MaxAlign : 2); 516b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen 517cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen // The function needs to be as aligned as the basic blocks. The linker may 518cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen // move functions around based on their alignment. 51970cfaa34645bf8ad232c400f0fe7c48a9d715586Chad Rosier MF->ensureAlignment(BB->getAlignment()); 520cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen 521b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen // Order the entries in BB by descending alignment. That ensures correct 522b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen // alignment of all entries as long as BB is sufficiently aligned. Keep 523b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen // track of the insertion point for each alignment. We are going to bucket 524b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen // sort the entries as they are created. 525b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen SmallVector<MachineBasicBlock::iterator, 8> InsPoint(MaxAlign + 1, BB->end()); 5263e572ac2fbdf6aa538500be07b9b050ac008669eJakob Stoklund Olesen 527a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Add all of the constants from the constant pool to the end block, use an 528a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // identity mapping of CPI's to CPE's. 5295e46dcbb4b6f6e82f7bb88490800ed09ac02f105Jakob Stoklund Olesen const std::vector<MachineConstantPoolEntry> &CPs = MCP->getConstants(); 5308494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 531dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen const TargetData &TD = *MF->getTarget().getTargetData(); 532a8e2989ece6dc46df59b0768184028257f913843Evan Cheng for (unsigned i = 0, e = CPs.size(); i != e; ++i) { 533777d2306b36816a53bc1ae1244c0dc7d998ae691Duncan Sands unsigned Size = TD.getTypeAllocSize(CPs[i].getType()); 534b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen assert(Size >= 4 && "Too small constant pool entry"); 535b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen unsigned Align = CPs[i].getAlignment(); 536b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen assert(isPowerOf2_32(Align) && "Invalid alignment"); 537b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen // Verify that all constant pool entries are a multiple of their alignment. 538b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen // If not, we would have to pad them out so that instructions stay aligned. 539b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen assert((Size % Align) == 0 && "CP Entry not multiple of 4 bytes!"); 540b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen 541b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen // Insert CONSTPOOL_ENTRY before entries with a smaller alignment. 542b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen unsigned LogAlign = Log2_32(Align); 543b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen MachineBasicBlock::iterator InsAt = InsPoint[LogAlign]; 544a8e2989ece6dc46df59b0768184028257f913843Evan Cheng MachineInstr *CPEMI = 545b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen BuildMI(*BB, InsAt, DebugLoc(), TII->get(ARM::CONSTPOOL_ENTRY)) 546c7f3ace20c325521c68335a1689645b43b06ddf0Chris Lattner .addImm(i).addConstantPoolIndex(i).addImm(Size); 547a8e2989ece6dc46df59b0768184028257f913843Evan Cheng CPEMIs.push_back(CPEMI); 548c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng 549b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen // Ensure that future entries with higher alignment get inserted before 550b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen // CPEMI. This is bucket sort with iterators. 551b076fb7762498289718dbe4cb5de03e6e93bd4e7Jakob Stoklund Olesen for (unsigned a = LogAlign + 1; a <= MaxAlign; ++a) 552b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen if (InsPoint[a] == InsAt) 553b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen InsPoint[a] = CPEMI; 554b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen 555c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng // Add a new CPEntry, but no corresponding CPUser yet. 556c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng std::vector<CPEntry> CPEs; 557c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng CPEs.push_back(CPEntry(CPEMI, i)); 558c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng CPEntries.push_back(CPEs); 559fe60104ac97f3a8736dcfbfdf9547c7b7cc7b951Dan Gohman ++NumCPEs; 560dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen DEBUG(dbgs() << "Moved CPI#" << i << " to end of function, size = " 561dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen << Size << ", align = " << Align <<'\n'); 562a8e2989ece6dc46df59b0768184028257f913843Evan Cheng } 563b813f924a749396ffb4a4bd087ee1dbb678551f3Jakob Stoklund Olesen DEBUG(BB->dump()); 564a8e2989ece6dc46df59b0768184028257f913843Evan Cheng} 565a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 56688e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen/// BBHasFallthrough - Return true if the specified basic block can fallthrough 567a8e2989ece6dc46df59b0768184028257f913843Evan Cheng/// into the block immediately after it. 568a8e2989ece6dc46df59b0768184028257f913843Evan Chengstatic bool BBHasFallthrough(MachineBasicBlock *MBB) { 569a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Get the next machine basic block in the function. 570a8e2989ece6dc46df59b0768184028257f913843Evan Cheng MachineFunction::iterator MBBI = MBB; 57118f30e6f5e80787808fe1455742452a5210afe07Jim Grosbach // Can't fall off end of function. 57218f30e6f5e80787808fe1455742452a5210afe07Jim Grosbach if (llvm::next(MBBI) == MBB->getParent()->end()) 573a8e2989ece6dc46df59b0768184028257f913843Evan Cheng return false; 5748494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 5757896c9f436a4eda5ec15e882a7505ba482a2fcd0Chris Lattner MachineBasicBlock *NextBB = llvm::next(MBBI); 576a8e2989ece6dc46df59b0768184028257f913843Evan Cheng for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), 577a8e2989ece6dc46df59b0768184028257f913843Evan Cheng E = MBB->succ_end(); I != E; ++I) 578a8e2989ece6dc46df59b0768184028257f913843Evan Cheng if (*I == NextBB) 579a8e2989ece6dc46df59b0768184028257f913843Evan Cheng return true; 5808494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 581a8e2989ece6dc46df59b0768184028257f913843Evan Cheng return false; 582a8e2989ece6dc46df59b0768184028257f913843Evan Cheng} 583a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 584c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng/// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI, 585c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng/// look up the corresponding CPEntry. 586c99ef085b90b32952b484e8843fb66ad65215b61Evan ChengARMConstantIslands::CPEntry 587c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng*ARMConstantIslands::findConstPoolEntry(unsigned CPI, 588c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng const MachineInstr *CPEMI) { 589c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng std::vector<CPEntry> &CPEs = CPEntries[CPI]; 590c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng // Number of entries per constpool index should be small, just do a 591c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng // linear search. 592c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng for (unsigned i = 0, e = CPEs.size(); i != e; ++i) { 593c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng if (CPEs[i].CPEMI == CPEMI) 594c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng return &CPEs[i]; 595c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng } 596c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng return NULL; 597c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng} 598c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng 599cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen/// getCPELogAlign - Returns the required alignment of the constant pool entry 600bd1ec17caf8fc393b112e6c01fe0e9cd7db213aeJakob Stoklund Olesen/// represented by CPEMI. Alignment is measured in log2(bytes) units. 601cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesenunsigned ARMConstantIslands::getCPELogAlign(const MachineInstr *CPEMI) { 602cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen assert(CPEMI && CPEMI->getOpcode() == ARM::CONSTPOOL_ENTRY); 603cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen 604cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen // Everything is 4-byte aligned unless AlignConstantIslands is set. 605cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen if (!AlignConstantIslands) 606cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen return 2; 607cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen 608cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen unsigned CPI = CPEMI->getOperand(1).getIndex(); 609cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen assert(CPI < MCP->getConstants().size() && "Invalid constant pool index."); 610cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen unsigned Align = MCP->getConstants()[CPI].getAlignment(); 611cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen assert(isPowerOf2_32(Align) && "Invalid CPE alignment"); 612cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen return Log2_32(Align); 613cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen} 614cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen 6157a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// scanFunctionJumpTables - Do a scan of the function, building up 61680697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach/// information about the sizes of each block and the locations of all 61780697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach/// the jump tables. 6187a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachvoid ARMConstantIslands::scanFunctionJumpTables() { 619dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end(); 62080697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach MBBI != E; ++MBBI) { 62180697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach MachineBasicBlock &MBB = *MBBI; 62280697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach 62380697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); 62408cbda56b6a45a2c29bdf3d9668ad614ad86cb77Jim Grosbach I != E; ++I) 6255a96b3dad2f634c9081c8b2b6c2575441dc5a2bdEvan Cheng if (I->isBranch() && I->getOpcode() == ARM::t2BR_JT) 62608cbda56b6a45a2c29bdf3d9668ad614ad86cb77Jim Grosbach T2JumpTables.push_back(I); 62780697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach } 62880697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach} 62980697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach 6307a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// initializeFunctionInfo - Do the initial scan of the function, building up 631a8e2989ece6dc46df59b0768184028257f913843Evan Cheng/// information about the sizes of each block, the location of all the water, 632a8e2989ece6dc46df59b0768184028257f913843Evan Cheng/// and finding all of the constant pool users. 633dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesenvoid ARMConstantIslands:: 6347a465250564d2ae0cc53ebe58732a751daa43e8dJim GrosbachinitializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs) { 635a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen BBInfo.clear(); 636dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen BBInfo.resize(MF->getNumBlockIDs()); 637a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen 638540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen // First thing, compute the size of all basic blocks, and see if the function 639540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen // has any inline assembly in it. If so, we have to be conservative about 640540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen // alignment assumptions, as we don't know for sure the size of any 641540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen // instructions in the inline assembly. 642dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) 6437a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach computeBlockSize(I); 644540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen 645540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen // The known bits of the entry block offset are determined by the function 646540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen // alignment. 647dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen BBInfo.front().KnownBits = MF->getAlignment(); 648540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen 649540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen // Compute block offsets and known bits. 6507a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach adjustBBOffsetsAfter(MF->begin()); 651540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen 6529a4d2e40a02b6e5fab5ccf1c768a52c76f3aec6aBill Wendling // Now go back through the instructions and build up our data structures. 653dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end(); 654a8e2989ece6dc46df59b0768184028257f913843Evan Cheng MBBI != E; ++MBBI) { 655a8e2989ece6dc46df59b0768184028257f913843Evan Cheng MachineBasicBlock &MBB = *MBBI; 6568494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 657a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // If this block doesn't fall through into the next MBB, then this is 658a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // 'water' that a constant pool island could be placed. 659a8e2989ece6dc46df59b0768184028257f913843Evan Cheng if (!BBHasFallthrough(&MBB)) 660a8e2989ece6dc46df59b0768184028257f913843Evan Cheng WaterList.push_back(&MBB); 6618494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 662a8e2989ece6dc46df59b0768184028257f913843Evan Cheng for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); 663a8e2989ece6dc46df59b0768184028257f913843Evan Cheng I != E; ++I) { 6649cfcfeb24bf7182d40d8e8a54e350451b67a1136Jim Grosbach if (I->isDebugValue()) 6659cfcfeb24bf7182d40d8e8a54e350451b67a1136Jim Grosbach continue; 666a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen 667af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng int Opc = I->getOpcode(); 6685a96b3dad2f634c9081c8b2b6c2575441dc5a2bdEvan Cheng if (I->isBranch()) { 669af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng bool isCond = false; 670af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng unsigned Bits = 0; 671af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng unsigned Scale = 1; 672af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng int UOpc = Opc; 673af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng switch (Opc) { 6745657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng default: 6755657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng continue; // Ignore other JT branches 6765657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng case ARM::t2BR_JT: 6775657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng T2JumpTables.push_back(I); 6785657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng continue; // Does not get an entry in ImmBranches 679af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng case ARM::Bcc: 680af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng isCond = true; 681af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng UOpc = ARM::B; 682af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng // Fallthrough 683af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng case ARM::B: 684af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng Bits = 24; 685af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng Scale = 4; 686af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng break; 687af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng case ARM::tBcc: 688af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng isCond = true; 689af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng UOpc = ARM::tB; 690af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng Bits = 8; 691af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng Scale = 2; 692af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng break; 693af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng case ARM::tB: 694af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng Bits = 11; 695af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng Scale = 2; 696af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng break; 6975e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin case ARM::t2Bcc: 6985e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin isCond = true; 6995e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin UOpc = ARM::t2B; 7005e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin Bits = 20; 7015e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin Scale = 2; 7025e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin break; 7035e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin case ARM::t2B: 7045e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin Bits = 24; 7055e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin Scale = 2; 7065e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin break; 707af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng } 708b43216ee4a418257ce5530c02601b77efe86c354Evan Cheng 709b43216ee4a418257ce5530c02601b77efe86c354Evan Cheng // Record this immediate branch. 710bd5d3dbdbe46c7325545a7cb8c891c0347375451Evan Cheng unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale; 711b43216ee4a418257ce5530c02601b77efe86c354Evan Cheng ImmBranches.push_back(ImmBranch(I, MaxOffs, isCond, UOpc)); 712af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng } 713af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng 714d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng if (Opc == ARM::tPUSH || Opc == ARM::tPOP_RET) 715d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng PushPopMIs.push_back(I); 716d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng 717d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng if (Opc == ARM::CONSTPOOL_ENTRY) 718d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng continue; 719d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng 720a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Scan the instructions for constant pool operands. 721a8e2989ece6dc46df59b0768184028257f913843Evan Cheng for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) 722d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman if (I->getOperand(op).isCPI()) { 723a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // We found one. The addressing mode tells us the max displacement 724a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // from the PC that this instruction permits. 7258494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 726a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Basic size info comes from the TSFlags field. 727b43216ee4a418257ce5530c02601b77efe86c354Evan Cheng unsigned Bits = 0; 728b43216ee4a418257ce5530c02601b77efe86c354Evan Cheng unsigned Scale = 1; 7295d8f1cae76e4e424bf3b4392eddb3f4dbf38662aEvan Cheng bool NegOk = false; 730d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng bool IsSoImm = false; 731d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng 732d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng switch (Opc) { 7338494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson default: 734c23197a26f34f559ea9797de51e187087c039c42Torok Edwin llvm_unreachable("Unknown addressing mode for CP reference!"); 735d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng 736d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng // Taking the address of a CP entry. 737d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng case ARM::LEApcrel: 738d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng // This takes a SoImm, which is 8 bit immediate rotated. We'll 739d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng // pretend the maximum offset is 255 * 4. Since each instruction 740dec6de92d1425c88f319179fb4a0b0a23f781df9Jim Grosbach // 4 byte wide, this is always correct. We'll check for other 741d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng // displacements that fits in a SoImm as well. 742d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng Bits = 8; 743d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng Scale = 4; 7445d8f1cae76e4e424bf3b4392eddb3f4dbf38662aEvan Cheng NegOk = true; 745d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng IsSoImm = true; 746a8e2989ece6dc46df59b0768184028257f913843Evan Cheng break; 7476b8719fd7dc527e4c1910ae49ebee61d90907c08Owen Anderson case ARM::t2LEApcrel: 748d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng Bits = 12; 7495d8f1cae76e4e424bf3b4392eddb3f4dbf38662aEvan Cheng NegOk = true; 750a8e2989ece6dc46df59b0768184028257f913843Evan Cheng break; 751d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng case ARM::tLEApcrel: 752b43216ee4a418257ce5530c02601b77efe86c354Evan Cheng Bits = 8; 753d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng Scale = 4; 754a8e2989ece6dc46df59b0768184028257f913843Evan Cheng break; 755d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng 7563e5561247202bae994dd259a2d8dc4eff8f799f3Jim Grosbach case ARM::LDRi12: 757d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng case ARM::LDRcp: 758971b83b67a9812556cdb97bb58aa96fb37af458dOwen Anderson case ARM::t2LDRpci: 759d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng Bits = 12; // +-offset_12 760d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng NegOk = true; 761a8e2989ece6dc46df59b0768184028257f913843Evan Cheng break; 762d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng 763d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng case ARM::tLDRpci: 764b43216ee4a418257ce5530c02601b77efe86c354Evan Cheng Bits = 8; 765b43216ee4a418257ce5530c02601b77efe86c354Evan Cheng Scale = 4; // +(offset_8*4) 766012f2d97b78e4eb9128f1d491f2c177768dbe527Evan Cheng break; 767d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng 768e5165490b7ba24bb2f3043399e0d60e7f3bcf8a5Jim Grosbach case ARM::VLDRD: 769e5165490b7ba24bb2f3043399e0d60e7f3bcf8a5Jim Grosbach case ARM::VLDRS: 770d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng Bits = 8; 771d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng Scale = 4; // +-(offset_8*4) 772d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng NegOk = true; 773055b0310f862b91f33699037ce67d3ab8137c20cEvan Cheng break; 774a8e2989ece6dc46df59b0768184028257f913843Evan Cheng } 775b43216ee4a418257ce5530c02601b77efe86c354Evan Cheng 776a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Remember that this is a user of a CP entry. 7778aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner unsigned CPI = I->getOperand(op).getIndex(); 778c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng MachineInstr *CPEMI = CPEMIs[CPI]; 77931b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng unsigned MaxOffs = ((1 << Bits)-1) * Scale; 780d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng CPUsers.push_back(CPUser(I, CPEMI, MaxOffs, NegOk, IsSoImm)); 781c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng 782c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng // Increment corresponding CPEntry reference count. 783c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng CPEntry *CPE = findConstPoolEntry(CPI, CPEMI); 784c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng assert(CPE && "Cannot find a corresponding CPEntry!"); 785c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng CPE->RefCount++; 7868494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 787a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Instructions can only use one CP entry, don't bother scanning the 788a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // rest of the operands. 789a8e2989ece6dc46df59b0768184028257f913843Evan Cheng break; 790a8e2989ece6dc46df59b0768184028257f913843Evan Cheng } 791a8e2989ece6dc46df59b0768184028257f913843Evan Cheng } 792a8e2989ece6dc46df59b0768184028257f913843Evan Cheng } 793a8e2989ece6dc46df59b0768184028257f913843Evan Cheng} 794a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 7957a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// computeBlockSize - Compute the size and some alignment information for MBB. 796a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen/// This function updates BBInfo directly. 7977a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachvoid ARMConstantIslands::computeBlockSize(MachineBasicBlock *MBB) { 798a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen BasicBlockInfo &BBI = BBInfo[MBB->getNumber()]; 799a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen BBI.Size = 0; 800a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen BBI.Unalign = 0; 801a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen BBI.PostAlign = 0; 802a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen 803540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E; 804540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen ++I) { 805a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen BBI.Size += TII->GetInstSizeInBytes(I); 806a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen // For inline asm, GetInstSizeInBytes returns a conservative estimate. 807a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen // The actual size may be smaller, but still a multiple of the instr size. 808e6f9e9d8365346330c782c82860a6b77df8ee829Jakob Stoklund Olesen if (I->isInlineAsm()) 809a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen BBI.Unalign = isThumb ? 1 : 2; 81019d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesen // Also consider instructions that may be shrunk later. 81119d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesen else if (isThumb && mayOptimizeThumb2Instruction(I)) 81219d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesen BBI.Unalign = 1; 813a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen } 814a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen 815a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen // tBR_JTr contains a .align 2 directive. 816540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen if (!MBB->empty() && MBB->back().getOpcode() == ARM::tBR_JTr) { 817a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen BBI.PostAlign = 2; 81870cfaa34645bf8ad232c400f0fe7c48a9d715586Chad Rosier MBB->getParent()->ensureAlignment(2); 819540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen } 820a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen} 821a26811ec83d00344a739d84f4b8584e5548b94ceJakob Stoklund Olesen 8227a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// getOffsetOf - Return the current offset of the specified machine instruction 823a8e2989ece6dc46df59b0768184028257f913843Evan Cheng/// from the start of the function. This offset changes as stuff is moved 824a8e2989ece6dc46df59b0768184028257f913843Evan Cheng/// around inside the function. 8257a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachunsigned ARMConstantIslands::getOffsetOf(MachineInstr *MI) const { 826a8e2989ece6dc46df59b0768184028257f913843Evan Cheng MachineBasicBlock *MBB = MI->getParent(); 8278494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 828a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // The offset is composed of two things: the sum of the sizes of all MBB's 829a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // before this instruction's block, and the offset from the start of the block 830a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // it is in. 831a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen unsigned Offset = BBInfo[MBB->getNumber()].Offset; 832a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 833a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Sum instructions before MI in MBB. 8340c3cfefca62eeaf1f98a49756e3bcb2c451b3374Jim Grosbach for (MachineBasicBlock::iterator I = MBB->begin(); &*I != MI; ++I) { 835a8e2989ece6dc46df59b0768184028257f913843Evan Cheng assert(I != MBB->end() && "Didn't find MI in its own basic block?"); 83652e724ad7e679ee590f4bd763d55280586a8f1bcNicolas Geoffray Offset += TII->GetInstSizeInBytes(I); 837a8e2989ece6dc46df59b0768184028257f913843Evan Cheng } 8380c3cfefca62eeaf1f98a49756e3bcb2c451b3374Jim Grosbach return Offset; 839a8e2989ece6dc46df59b0768184028257f913843Evan Cheng} 840a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 841a8e2989ece6dc46df59b0768184028257f913843Evan Cheng/// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB 842a8e2989ece6dc46df59b0768184028257f913843Evan Cheng/// ID. 843a8e2989ece6dc46df59b0768184028257f913843Evan Chengstatic bool CompareMBBNumbers(const MachineBasicBlock *LHS, 844a8e2989ece6dc46df59b0768184028257f913843Evan Cheng const MachineBasicBlock *RHS) { 845a8e2989ece6dc46df59b0768184028257f913843Evan Cheng return LHS->getNumber() < RHS->getNumber(); 846a8e2989ece6dc46df59b0768184028257f913843Evan Cheng} 847a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 8487a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// updateForInsertedWaterBlock - When a block is newly inserted into the 849a8e2989ece6dc46df59b0768184028257f913843Evan Cheng/// machine function, it upsets all of the block numbers. Renumber the blocks 850a8e2989ece6dc46df59b0768184028257f913843Evan Cheng/// and update the arrays that parallel this numbering. 8517a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachvoid ARMConstantIslands::updateForInsertedWaterBlock(MachineBasicBlock *NewBB) { 852ab4c366274a582dd8146b2820c6b999cad5fce36Duncan Sands // Renumber the MBB's to keep them consecutive. 853a8e2989ece6dc46df59b0768184028257f913843Evan Cheng NewBB->getParent()->RenumberBlocks(NewBB); 8548494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 855a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen // Insert an entry into BBInfo to align it properly with the (newly 856a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // renumbered) block numbers. 857a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo()); 8588494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 8598494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson // Next, update WaterList. Specifically, we need to add NewMBB as having 860a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // available water after it. 861034de5f65f9139954cb01d94eaebbaefd294946eBob Wilson water_iterator IP = 862a8e2989ece6dc46df59b0768184028257f913843Evan Cheng std::lower_bound(WaterList.begin(), WaterList.end(), NewBB, 863a8e2989ece6dc46df59b0768184028257f913843Evan Cheng CompareMBBNumbers); 864a8e2989ece6dc46df59b0768184028257f913843Evan Cheng WaterList.insert(IP, NewBB); 865a8e2989ece6dc46df59b0768184028257f913843Evan Cheng} 866a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 867a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 868a8e2989ece6dc46df59b0768184028257f913843Evan Cheng/// Split the basic block containing MI into two blocks, which are joined by 869b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson/// an unconditional branch. Update data structures and renumber blocks to 8700c61584d05894a5543f690803191a8b0da05b8fcEvan Cheng/// account for this change and returns the newly created block. 8717a465250564d2ae0cc53ebe58732a751daa43e8dJim GrosbachMachineBasicBlock *ARMConstantIslands::splitBlockBeforeInstr(MachineInstr *MI) { 872a8e2989ece6dc46df59b0768184028257f913843Evan Cheng MachineBasicBlock *OrigBB = MI->getParent(); 873a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 874a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Create a new MBB for the code after the OrigBB. 8758494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson MachineBasicBlock *NewBB = 876dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MF->CreateMachineBasicBlock(OrigBB->getBasicBlock()); 877a8e2989ece6dc46df59b0768184028257f913843Evan Cheng MachineFunction::iterator MBBI = OrigBB; ++MBBI; 878dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MF->insert(MBBI, NewBB); 8798494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 880a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Splice the instructions starting with MI over to NewBB. 881a8e2989ece6dc46df59b0768184028257f913843Evan Cheng NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end()); 8828494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 883a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Add an unconditional branch from OrigBB to NewBB. 884a9b8b8d62c67e96bc4dc2ed25298c539102f8638Evan Cheng // Note the new unconditional branch is not being recorded. 885b67284057ee130114055309eabe0bcd1af13777dDale Johannesen // There doesn't seem to be meaningful DebugInfo available; this doesn't 886b67284057ee130114055309eabe0bcd1af13777dDale Johannesen // correspond to anything in the source. 88758541fd62707b6856beee3db9e254b0110607bb1Evan Cheng unsigned Opc = isThumb ? (isThumb2 ? ARM::t2B : ARM::tB) : ARM::B; 88851f6a7abf27fc92c3d8904c2334feab8b498e8e9Owen Anderson if (!isThumb) 88951f6a7abf27fc92c3d8904c2334feab8b498e8e9Owen Anderson BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB); 89051f6a7abf27fc92c3d8904c2334feab8b498e8e9Owen Anderson else 89151f6a7abf27fc92c3d8904c2334feab8b498e8e9Owen Anderson BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB) 89251f6a7abf27fc92c3d8904c2334feab8b498e8e9Owen Anderson .addImm(ARMCC::AL).addReg(0); 893fe60104ac97f3a8736dcfbfdf9547c7b7cc7b951Dan Gohman ++NumSplit; 8948494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 895a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Update the CFG. All succs of OrigBB are now succs of NewBB. 896e80fba0e6c976039c132c3d5d4fcad569d2259a8Jakob Stoklund Olesen NewBB->transferSuccessors(OrigBB); 8978494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 898a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // OrigBB branches to NewBB. 899a8e2989ece6dc46df59b0768184028257f913843Evan Cheng OrigBB->addSuccessor(NewBB); 9008494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 901a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Update internal data structures to account for the newly inserted MBB. 9027a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach // This is almost the same as updateForInsertedWaterBlock, except that 90388e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // the Water goes after OrigBB, not NewBB. 904dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MF->RenumberBlocks(NewBB); 9058494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 906a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen // Insert an entry into BBInfo to align it properly with the (newly 90788e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // renumbered) block numbers. 908a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo()); 90999c49a4b94ffabdd22f55e8274c7f92892e25559Dale Johannesen 9108494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson // Next, update WaterList. Specifically, we need to add OrigMBB as having 91188e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // available water after it (but not if it's already there, which happens 91288e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // when splitting before a conditional branch that is followed by an 91388e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // unconditional branch - in that case we want to insert NewBB). 914034de5f65f9139954cb01d94eaebbaefd294946eBob Wilson water_iterator IP = 91588e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB, 91688e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen CompareMBBNumbers); 91788e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen MachineBasicBlock* WaterBB = *IP; 91888e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen if (WaterBB == OrigBB) 9197896c9f436a4eda5ec15e882a7505ba482a2fcd0Chris Lattner WaterList.insert(llvm::next(IP), NewBB); 92088e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen else 92188e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen WaterList.insert(IP, OrigBB); 922b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson NewWaterList.insert(OrigBB); 92388e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen 9248086d5800dbe8cc392ce217adccfaca915858aedDale Johannesen // Figure out how large the OrigBB is. As the first half of the original 9258086d5800dbe8cc392ce217adccfaca915858aedDale Johannesen // block, it cannot contain a tablejump. The size includes 9268086d5800dbe8cc392ce217adccfaca915858aedDale Johannesen // the new jump we added. (It should be possible to do this without 9278086d5800dbe8cc392ce217adccfaca915858aedDale Johannesen // recounting everything, but it's very confusing, and this is rarely 9288086d5800dbe8cc392ce217adccfaca915858aedDale Johannesen // executed.) 9297a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach computeBlockSize(OrigBB); 93099c49a4b94ffabdd22f55e8274c7f92892e25559Dale Johannesen 9318086d5800dbe8cc392ce217adccfaca915858aedDale Johannesen // Figure out how large the NewMBB is. As the second half of the original 9328086d5800dbe8cc392ce217adccfaca915858aedDale Johannesen // block, it may contain a tablejump. 9337a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach computeBlockSize(NewBB); 9348086d5800dbe8cc392ce217adccfaca915858aedDale Johannesen 93599c49a4b94ffabdd22f55e8274c7f92892e25559Dale Johannesen // All BBOffsets following these blocks must be modified. 9367a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach adjustBBOffsetsAfter(OrigBB); 9370c61584d05894a5543f690803191a8b0da05b8fcEvan Cheng 9380c61584d05894a5543f690803191a8b0da05b8fcEvan Cheng return NewBB; 939a8e2989ece6dc46df59b0768184028257f913843Evan Cheng} 940a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 9417a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// getUserOffset - Compute the offset of U.MI as seen by the hardware 942dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen/// displacement computation. Update U.KnownAlignment to match its current 943dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen/// basic block location. 9447a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachunsigned ARMConstantIslands::getUserOffset(CPUser &U) const { 9457a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach unsigned UserOffset = getOffsetOf(U.MI); 946dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen const BasicBlockInfo &BBI = BBInfo[U.MI->getParent()->getNumber()]; 947dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen unsigned KnownBits = BBI.internalKnownBits(); 948dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen 949dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen // The value read from PC is offset from the actual instruction address. 950dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen UserOffset += (isThumb ? 4 : 8); 951dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen 952dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen // Because of inline assembly, we may not know the alignment (mod 4) of U.MI. 953dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen // Make sure U.getMaxDisp() returns a constrained range. 954dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen U.KnownAlignment = (KnownBits >= 2); 955dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen 956dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen // On Thumb, offsets==2 mod 4 are rounded down by the hardware for 957dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen // purposes of the displacement computation; compensate for that here. 958dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen // For unknown alignments, getMaxDisp() constrains the range instead. 959dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen if (isThumb && U.KnownAlignment) 960dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen UserOffset &= ~3u; 961dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen 962dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen return UserOffset; 963dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen} 964dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen 9657a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// isOffsetInRange - Checks whether UserOffset (the location of a constant pool 9668494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson/// reference) is within MaxDisp of TrialOffset (a proposed location of a 9678593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen/// constant pool entry). 9687a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// UserOffset is computed by getUserOffset above to include PC adjustments. If 969dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen/// the mod 4 alignment of UserOffset is not known, the uncertainty must be 970dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen/// subtracted from MaxDisp instead. CPUser::getMaxDisp() does that. 9717a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachbool ARMConstantIslands::isOffsetInRange(unsigned UserOffset, 972d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng unsigned TrialOffset, unsigned MaxDisp, 973d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng bool NegativeOK, bool IsSoImm) { 97499c49a4b94ffabdd22f55e8274c7f92892e25559Dale Johannesen if (UserOffset <= TrialOffset) { 97599c49a4b94ffabdd22f55e8274c7f92892e25559Dale Johannesen // User before the Trial. 976d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng if (TrialOffset - UserOffset <= MaxDisp) 977d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng return true; 97840efc251cd7a52dd2375ec95ee38b1be4572178fEvan Cheng // FIXME: Make use full range of soimm values. 97999c49a4b94ffabdd22f55e8274c7f92892e25559Dale Johannesen } else if (NegativeOK) { 980d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng if (UserOffset - TrialOffset <= MaxDisp) 981d3d9d66dd211d1267e764c7294876d9a227f04caEvan Cheng return true; 98240efc251cd7a52dd2375ec95ee38b1be4572178fEvan Cheng // FIXME: Make use full range of soimm values. 98399c49a4b94ffabdd22f55e8274c7f92892e25559Dale Johannesen } 98499c49a4b94ffabdd22f55e8274c7f92892e25559Dale Johannesen return false; 98599c49a4b94ffabdd22f55e8274c7f92892e25559Dale Johannesen} 98699c49a4b94ffabdd22f55e8274c7f92892e25559Dale Johannesen 9877a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// isWaterInRange - Returns true if a CPE placed after the specified 98888e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen/// Water (a basic block) will be in range for the specific MI. 9892e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen/// 9902e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen/// Compute how much the function will grow by inserting a CPE after Water. 9917a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachbool ARMConstantIslands::isWaterInRange(unsigned UserOffset, 9922e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen MachineBasicBlock* Water, CPUser &U, 9932e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen unsigned &Growth) { 9942e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen unsigned CPELogAlign = getCPELogAlign(U.CPEMI); 9952e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen unsigned CPEOffset = BBInfo[Water->getNumber()].postOffset(CPELogAlign); 9962e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen unsigned NextBlockOffset, NextBlockAlignment; 9972e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen MachineFunction::const_iterator NextBlock = Water; 9982e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen if (++NextBlock == MF->end()) { 9992e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen NextBlockOffset = BBInfo[Water->getNumber()].postOffset(); 10002e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen NextBlockAlignment = 0; 10012e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen } else { 10022e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen NextBlockOffset = BBInfo[NextBlock->getNumber()].Offset; 10032e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen NextBlockAlignment = NextBlock->getAlignment(); 10042e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen } 10052e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen unsigned Size = U.CPEMI->getOperand(2).getImm(); 10062e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen unsigned CPEEnd = CPEOffset + Size; 10072e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen 10082e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen // The CPE may be able to hide in the alignment padding before the next 10092e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen // block. It may also cause more padding to be required if it is more aligned 10102e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen // that the next block. 10112e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen if (CPEEnd > NextBlockOffset) { 10122e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen Growth = CPEEnd - NextBlockOffset; 10132e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen // Compute the padding that would go at the end of the CPE to align the next 10142e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen // block. 10152e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen Growth += OffsetToAlignment(CPEEnd, 1u << NextBlockAlignment); 10162e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen 10172e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen // If the CPE is to be inserted before the instruction, that will raise 10187a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach // the offset of the instruction. Also account for unknown alignment padding 10192e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen // in blocks between CPE and the user. 10202e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen if (CPEOffset < UserOffset) 10212e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen UserOffset += Growth + UnknownPadding(MF->getAlignment(), CPELogAlign); 10222e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen } else 10232e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen // CPE fits in existing padding. 10242e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen Growth = 0; 1025d959aa421a8d14fc5aad29141f816db8f4362c7fDale Johannesen 10267a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach return isOffsetInRange(UserOffset, CPEOffset, U); 102788e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen} 102888e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen 10297a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// isCPEntryInRange - Returns true if the distance between specific MI and 1030c0dbec7e1038ee60b8525eb1e8e3eaa6a839bd5bEvan Cheng/// specific ConstPool entry instruction can fit in MI's displacement field. 10317a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachbool ARMConstantIslands::isCPEntryInRange(MachineInstr *MI, unsigned UserOffset, 10325d8f1cae76e4e424bf3b4392eddb3f4dbf38662aEvan Cheng MachineInstr *CPEMI, unsigned MaxDisp, 10335d8f1cae76e4e424bf3b4392eddb3f4dbf38662aEvan Cheng bool NegOk, bool DoDump) { 10347a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach unsigned CPEOffset = getOffsetOf(CPEMI); 10352021abe154e3cac523755b74fc5e62a2c9dad1fcEvan Cheng 103688e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen if (DoDump) { 10373c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen DEBUG({ 10383c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen unsigned Block = MI->getParent()->getNumber(); 10393c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen const BasicBlockInfo &BBI = BBInfo[Block]; 10403c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen dbgs() << "User of CPE#" << CPEMI->getOperand(0).getImm() 10413c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen << " max delta=" << MaxDisp 10422d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen << format(" insn address=%#x", UserOffset) 10433c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen << " in BB#" << Block << ": " 10442d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen << format("%#x-%x\t", BBI.Offset, BBI.postOffset()) << *MI 10452d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen << format("CPE address=%#x offset=%+d: ", CPEOffset, 10462d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen int(CPEOffset-UserOffset)); 10473c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen }); 104888e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen } 1049a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 10507a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach return isOffsetInRange(UserOffset, CPEOffset, MaxDisp, NegOk); 1051c0dbec7e1038ee60b8525eb1e8e3eaa6a839bd5bEvan Cheng} 1052c0dbec7e1038ee60b8525eb1e8e3eaa6a839bd5bEvan Cheng 1053d1e7d9a88b5f4fa5e9a85affc4599c650c591ffbEvan Cheng#ifndef NDEBUG 1054c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng/// BBIsJumpedOver - Return true of the specified basic block's only predecessor 1055c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng/// unconditionally branches to its only successor. 1056c99ef085b90b32952b484e8843fb66ad65215b61Evan Chengstatic bool BBIsJumpedOver(MachineBasicBlock *MBB) { 1057c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng if (MBB->pred_size() != 1 || MBB->succ_size() != 1) 1058c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng return false; 1059c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng 1060c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng MachineBasicBlock *Succ = *MBB->succ_begin(); 1061c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng MachineBasicBlock *Pred = *MBB->pred_begin(); 1062c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng MachineInstr *PredMI = &Pred->back(); 10635e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin if (PredMI->getOpcode() == ARM::B || PredMI->getOpcode() == ARM::tB 10645e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin || PredMI->getOpcode() == ARM::t2B) 1065c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng return PredMI->getOperand(0).getMBB() == Succ; 1066c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng return false; 1067c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng} 1068d1e7d9a88b5f4fa5e9a85affc4599c650c591ffbEvan Cheng#endif // NDEBUG 1069c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng 10707a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachvoid ARMConstantIslands::adjustBBOffsetsAfter(MachineBasicBlock *BB) { 107159ecaae7b61ef0023ed1db4643937f7a57f61d10Jakob Stoklund Olesen unsigned BBNum = BB->getNumber(); 107259ecaae7b61ef0023ed1db4643937f7a57f61d10Jakob Stoklund Olesen for(unsigned i = BBNum + 1, e = MF->getNumBlockIDs(); i < e; ++i) { 1073540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen // Get the offset and known bits at the end of the layout predecessor. 10748552821e578d693ec14007b21e9468010485ae76Jakob Stoklund Olesen // Include the alignment of the current block. 10758552821e578d693ec14007b21e9468010485ae76Jakob Stoklund Olesen unsigned LogAlign = MF->getBlockNumbered(i)->getAlignment(); 10768552821e578d693ec14007b21e9468010485ae76Jakob Stoklund Olesen unsigned Offset = BBInfo[i - 1].postOffset(LogAlign); 10778552821e578d693ec14007b21e9468010485ae76Jakob Stoklund Olesen unsigned KnownBits = BBInfo[i - 1].postKnownBits(LogAlign); 1078540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen 107959ecaae7b61ef0023ed1db4643937f7a57f61d10Jakob Stoklund Olesen // This is where block i begins. Stop if the offset is already correct, 108059ecaae7b61ef0023ed1db4643937f7a57f61d10Jakob Stoklund Olesen // and we have updated 2 blocks. This is the maximum number of blocks 108159ecaae7b61ef0023ed1db4643937f7a57f61d10Jakob Stoklund Olesen // changed before calling this function. 108259ecaae7b61ef0023ed1db4643937f7a57f61d10Jakob Stoklund Olesen if (i > BBNum + 2 && 108359ecaae7b61ef0023ed1db4643937f7a57f61d10Jakob Stoklund Olesen BBInfo[i].Offset == Offset && 108459ecaae7b61ef0023ed1db4643937f7a57f61d10Jakob Stoklund Olesen BBInfo[i].KnownBits == KnownBits) 108559ecaae7b61ef0023ed1db4643937f7a57f61d10Jakob Stoklund Olesen break; 108659ecaae7b61ef0023ed1db4643937f7a57f61d10Jakob Stoklund Olesen 1087540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen BBInfo[i].Offset = Offset; 1088540c6d9d2651310d88eb9a147177ccd52eec7cd5Jakob Stoklund Olesen BBInfo[i].KnownBits = KnownBits; 10898593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen } 109099c49a4b94ffabdd22f55e8274c7f92892e25559Dale Johannesen} 109199c49a4b94ffabdd22f55e8274c7f92892e25559Dale Johannesen 10927a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// decrementCPEReferenceCount - find the constant pool entry with index CPI 109388e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen/// and instruction CPEMI, and decrement its refcount. If the refcount 10948494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson/// becomes 0 remove the entry and instruction. Returns true if we removed 109588e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen/// the entry, false if we didn't. 1096b43216ee4a418257ce5530c02601b77efe86c354Evan Cheng 10977a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachbool ARMConstantIslands::decrementCPEReferenceCount(unsigned CPI, 10987a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach MachineInstr *CPEMI) { 1099c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng // Find the old entry. Eliminate it if it is no longer used. 1100ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng CPEntry *CPE = findConstPoolEntry(CPI, CPEMI); 1101ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng assert(CPE && "Unexpected!"); 1102ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng if (--CPE->RefCount == 0) { 11037a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach removeDeadCPEMI(CPEMI); 1104ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng CPE->CPEMI = NULL; 1105fe60104ac97f3a8736dcfbfdf9547c7b7cc7b951Dan Gohman --NumCPEs; 110688e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen return true; 110788e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen } 110888e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen return false; 110988e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen} 111088e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen 111188e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen/// LookForCPEntryInRange - see if the currently referenced CPE is in range; 111288e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen/// if not, see if an in-range clone of the CPE is in range, and if so, 111388e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen/// change the data structures so the user references the clone. Returns: 111488e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen/// 0 = no existing entry found 111588e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen/// 1 = entry found, and there were no code insertions or deletions 111688e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen/// 2 = entry found, and there were code insertions or deletions 11177a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachint ARMConstantIslands::findInRangeCPEntry(CPUser& U, unsigned UserOffset) 111888e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen{ 111988e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen MachineInstr *UserMI = U.MI; 112088e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen MachineInstr *CPEMI = U.CPEMI; 112188e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen 112288e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // Check to see if the CPE is already in-range. 11237a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach if (isCPEntryInRange(UserMI, UserOffset, CPEMI, U.getMaxDisp(), U.NegOk, 11247a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach true)) { 11253c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen DEBUG(dbgs() << "In range\n"); 112688e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen return 1; 1127c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng } 1128c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng 112988e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // No. Look for previously created clones of the CPE that are in range. 11308aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner unsigned CPI = CPEMI->getOperand(1).getIndex(); 113188e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen std::vector<CPEntry> &CPEs = CPEntries[CPI]; 113288e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen for (unsigned i = 0, e = CPEs.size(); i != e; ++i) { 113388e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // We already tried this one 113488e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen if (CPEs[i].CPEMI == CPEMI) 113588e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen continue; 113688e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // Removing CPEs can leave empty entries, skip 113788e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen if (CPEs[i].CPEMI == NULL) 113888e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen continue; 11397a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach if (isCPEntryInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.getMaxDisp(), 1140dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen U.NegOk)) { 11413c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#" 1142893e1c90a03a53cf13f73849324e83612688428aChris Lattner << CPEs[i].CPI << "\n"); 114388e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // Point the CPUser node to the replacement 114488e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen U.CPEMI = CPEs[i].CPEMI; 114588e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // Change the CPI in the instruction operand to refer to the clone. 114688e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j) 1147d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman if (UserMI->getOperand(j).isCPI()) { 11488aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner UserMI->getOperand(j).setIndex(CPEs[i].CPI); 114988e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen break; 115088e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen } 115188e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // Adjust the refcount of the clone... 115288e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen CPEs[i].RefCount++; 115388e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // ...and the original. If we didn't remove the old entry, none of the 115488e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // addresses changed, so we don't need another pass. 11557a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1; 115688e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen } 115788e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen } 115888e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen return 0; 115988e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen} 116088e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen 1161f1b214d3ca12f2d85f0d092b4920172bcc797bacDale Johannesen/// getUnconditionalBrDisp - Returns the maximum displacement that can fit in 1162f1b214d3ca12f2d85f0d092b4920172bcc797bacDale Johannesen/// the specific unconditional branch instruction. 1163f1b214d3ca12f2d85f0d092b4920172bcc797bacDale Johannesenstatic inline unsigned getUnconditionalBrDisp(int Opc) { 11645e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin switch (Opc) { 11655e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin case ARM::tB: 11665e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin return ((1<<10)-1)*2; 11675e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin case ARM::t2B: 11685e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin return ((1<<23)-1)*2; 11695e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin default: 11705e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin break; 11715e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin } 1172764ab52dd80310a205c9888bf166d09dab858f90Jim Grosbach 11735e47a9a6e46bd271eba058fb831da1a1edf8707cDavid Goodwin return ((1<<23)-1)*4; 1174f1b214d3ca12f2d85f0d092b4920172bcc797bacDale Johannesen} 1175f1b214d3ca12f2d85f0d092b4920172bcc797bacDale Johannesen 11767a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// findAvailableWater - Look for an existing entry in the WaterList in which 1177b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen/// we can place the CPE referenced from U so it's within range of U's MI. 1178b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson/// Returns true if found, false if not. If it returns true, WaterIter 1179f98032ef64a777fe63d390539f901d7d6d4e868cBob Wilson/// is set to the WaterList entry. For Thumb, prefer water that will not 1180f98032ef64a777fe63d390539f901d7d6d4e868cBob Wilson/// introduce padding to water that will. To ensure that this pass 1181f98032ef64a777fe63d390539f901d7d6d4e868cBob Wilson/// terminates, the CPE location for a particular CPUser is only allowed to 1182f98032ef64a777fe63d390539f901d7d6d4e868cBob Wilson/// move to a lower address, so search backward from the end of the list and 1183f98032ef64a777fe63d390539f901d7d6d4e868cBob Wilson/// prefer the first water that is in range. 11847a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachbool ARMConstantIslands::findAvailableWater(CPUser &U, unsigned UserOffset, 1185b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson water_iterator &WaterIter) { 11863b7573517ccd0f8f9e4bc64cf1923bae68e40d3fBob Wilson if (WaterList.empty()) 11873b7573517ccd0f8f9e4bc64cf1923bae68e40d3fBob Wilson return false; 11883b7573517ccd0f8f9e4bc64cf1923bae68e40d3fBob Wilson 11892e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen unsigned BestGrowth = ~0u; 11902e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen for (water_iterator IP = prior(WaterList.end()), B = WaterList.begin();; 11912e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen --IP) { 11923b7573517ccd0f8f9e4bc64cf1923bae68e40d3fBob Wilson MachineBasicBlock* WaterBB = *IP; 1193b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // Check if water is in range and is either at a lower address than the 1194b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // current "high water mark" or a new water block that was created since 1195b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // the previous iteration by inserting an unconditional branch. In the 1196b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // latter case, we want to allow resetting the high water mark back to 1197b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // this new water since we haven't seen it before. Inserting branches 1198b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // should be relatively uncommon and when it does happen, we want to be 1199b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // sure to take advantage of it for all the CPEs near that block, so that 1200b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // we don't insert more branches than necessary. 12012e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen unsigned Growth; 12027a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach if (isWaterInRange(UserOffset, WaterBB, U, Growth) && 1203b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson (WaterBB->getNumber() < U.HighWaterMark->getNumber() || 12042e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen NewWaterList.count(WaterBB)) && Growth < BestGrowth) { 12052e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen // This is the least amount of required padding seen so far. 12062e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen BestGrowth = Growth; 12072e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen WaterIter = IP; 12082e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen DEBUG(dbgs() << "Found water after BB#" << WaterBB->getNumber() 12092e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen << " Growth=" << Growth << '\n'); 12102e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen 12112e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen // Keep looking unless it is perfect. 12122e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen if (BestGrowth == 0) 12133b7573517ccd0f8f9e4bc64cf1923bae68e40d3fBob Wilson return true; 121488e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen } 12153b7573517ccd0f8f9e4bc64cf1923bae68e40d3fBob Wilson if (IP == B) 12163b7573517ccd0f8f9e4bc64cf1923bae68e40d3fBob Wilson break; 121788e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen } 12182e29024d2e3aa23a1ec049c30dbce903e57d5f9bJakob Stoklund Olesen return BestGrowth != ~0u; 1219b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen} 122088e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen 12217a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// createNewWater - No existing WaterList entry will work for 1222b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen/// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the 1223b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen/// block is used if in range, and the conditional branch munged so control 1224b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen/// flow is correct. Otherwise the block is split to create a hole with an 1225757652c5ba820bbea75d8713d7bf3749e4113778Bob Wilson/// unconditional branch around it. In either case NewMBB is set to a 1226b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen/// block following which the new island can be inserted (the WaterList 1227b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen/// is not adjusted). 12287a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachvoid ARMConstantIslands::createNewWater(unsigned CPUserIndex, 1229757652c5ba820bbea75d8713d7bf3749e4113778Bob Wilson unsigned UserOffset, 1230757652c5ba820bbea75d8713d7bf3749e4113778Bob Wilson MachineBasicBlock *&NewMBB) { 1231b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen CPUser &U = CPUsers[CPUserIndex]; 1232b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen MachineInstr *UserMI = U.MI; 1233b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen MachineInstr *CPEMI = U.CPEMI; 1234299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen unsigned CPELogAlign = getCPELogAlign(CPEMI); 1235b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen MachineBasicBlock *UserMBB = UserMI->getParent(); 123677caaf0fc000a1e2e2afe949b6205ea5db668be9Jakob Stoklund Olesen const BasicBlockInfo &UserBBI = BBInfo[UserMBB->getNumber()]; 1237b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen 123836fa5321bacf351f47d6193f56c464e6ab93b934Bob Wilson // If the block does not end in an unconditional branch already, and if the 123936fa5321bacf351f47d6193f56c464e6ab93b934Bob Wilson // end of the block is within range, make new water there. (The addition 124036fa5321bacf351f47d6193f56c464e6ab93b934Bob Wilson // below is for the unconditional branch we will be adding: 4 bytes on ARM + 1241dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen // Thumb2, 2 on Thumb1. 1242299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen if (BBHasFallthrough(UserMBB)) { 1243299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // Size of branch to insert. 1244299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen unsigned Delta = isThumb1 ? 2 : 4; 1245299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // Compute the offset where the CPE will begin. 1246456ff46e668f361f79e8e91f145152089cd7d933Jakob Stoklund Olesen unsigned CPEOffset = UserBBI.postOffset(CPELogAlign) + Delta; 1247299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen 12487a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach if (isOffsetInRange(UserOffset, CPEOffset, U)) { 1249299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen DEBUG(dbgs() << "Split at end of BB#" << UserMBB->getNumber() 1250299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen << format(", expected CPE offset %#x\n", CPEOffset)); 1251299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen NewMBB = llvm::next(MachineFunction::iterator(UserMBB)); 1252299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // Add an unconditional branch from UserMBB to fallthrough block. Record 1253299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // it for branch lengthening; this new branch will not get out of range, 1254299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // but if the preceding conditional branch is out of range, the targets 1255299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // will be exchanged, and the altered branch may be out of range, so the 1256299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // machinery has to know about it. 1257299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen int UncondBr = isThumb ? ((isThumb2) ? ARM::t2B : ARM::tB) : ARM::B; 1258299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen if (!isThumb) 1259299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB); 1260299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen else 1261299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB) 1262299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen .addImm(ARMCC::AL).addReg(0); 1263299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen unsigned MaxDisp = getUnconditionalBrDisp(UncondBr); 1264299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen ImmBranches.push_back(ImmBranch(&UserMBB->back(), 1265299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen MaxDisp, false, UncondBr)); 1266299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen BBInfo[UserMBB->getNumber()].Size += Delta; 12677a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach adjustBBOffsetsAfter(UserMBB); 1268299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen return; 1269299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen } 1270299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen } 1271719510a178a910feb1db707140011c32a30992c4Evan Cheng 1272299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // What a big block. Find a place within the block to split it. This is a 1273299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // little tricky on Thumb1 since instructions are 2 bytes and constant pool 1274299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // entries are 4 bytes: if instruction I references island CPE, and 1275299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // instruction I+1 references CPE', it will not work well to put CPE as far 1276299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // forward as possible, since then CPE' cannot immediately follow it (that 1277299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // location is 2 bytes farther away from I+1 than CPE was from I) and we'd 1278299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // need to create a new island. So, we make a first guess, then walk through 1279299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // the instructions between the one currently being looked at and the 1280299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // possible insertion point, and make sure any other instructions that 1281299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // reference CPEs will be able to use the same island area; if not, we back 1282299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // up the insertion point. 1283299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen 1284299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // Try to split the block so it's fully aligned. Compute the latest split 1285456ff46e668f361f79e8e91f145152089cd7d933Jakob Stoklund Olesen // point where we can add a 4-byte branch instruction, and then align to 1286456ff46e668f361f79e8e91f145152089cd7d933Jakob Stoklund Olesen // LogAlign which is the largest possible alignment in the function. 1287299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen unsigned LogAlign = MF->getAlignment(); 1288299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen assert(LogAlign >= CPELogAlign && "Over-aligned constant pool entry"); 1289299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen unsigned KnownBits = UserBBI.internalKnownBits(); 1290299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen unsigned UPad = UnknownPadding(LogAlign, KnownBits); 1291456ff46e668f361f79e8e91f145152089cd7d933Jakob Stoklund Olesen unsigned BaseInsertOffset = UserOffset + U.getMaxDisp() - UPad; 1292299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen DEBUG(dbgs() << format("Split in middle of big block before %#x", 1293299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen BaseInsertOffset)); 1294299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen 1295299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // The 4 in the following is for the unconditional branch we'll be inserting 1296299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // (allows for long branch on Thumb1). Alignment of the island is handled 12977a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach // inside isOffsetInRange. 1298299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen BaseInsertOffset -= 4; 1299299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen 1300299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen DEBUG(dbgs() << format(", adjusted to %#x", BaseInsertOffset) 1301299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen << " la=" << LogAlign 1302299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen << " kb=" << KnownBits 1303299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen << " up=" << UPad << '\n'); 1304299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen 1305299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // This could point off the end of the block if we've already got constant 1306299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // pool entries following this block; only the last one is in the water list. 1307299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // Back past any possible branches (allow for a conditional and a maximally 1308299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // long unconditional). 13099b10dae0bf3a796ac06e94f072ac67126e30a255Jakob Stoklund Olesen if (BaseInsertOffset + 8 >= UserBBI.postOffset()) { 13109b10dae0bf3a796ac06e94f072ac67126e30a255Jakob Stoklund Olesen BaseInsertOffset = UserBBI.postOffset() - UPad - 8; 13119b10dae0bf3a796ac06e94f072ac67126e30a255Jakob Stoklund Olesen DEBUG(dbgs() << format("Move inside block: %#x\n", BaseInsertOffset)); 13129b10dae0bf3a796ac06e94f072ac67126e30a255Jakob Stoklund Olesen } 1313456ff46e668f361f79e8e91f145152089cd7d933Jakob Stoklund Olesen unsigned EndInsertOffset = BaseInsertOffset + 4 + UPad + 1314299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen CPEMI->getOperand(2).getImm(); 1315299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen MachineBasicBlock::iterator MI = UserMI; 1316299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen ++MI; 1317299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen unsigned CPUIndex = CPUserIndex+1; 1318299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen unsigned NumCPUsers = CPUsers.size(); 1319299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen MachineInstr *LastIT = 0; 1320299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen for (unsigned Offset = UserOffset+TII->GetInstSizeInBytes(UserMI); 1321299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen Offset < BaseInsertOffset; 1322299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen Offset += TII->GetInstSizeInBytes(MI), 1323299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen MI = llvm::next(MI)) { 13249b10dae0bf3a796ac06e94f072ac67126e30a255Jakob Stoklund Olesen assert(MI != UserMBB->end() && "Fell off end of block"); 1325299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].MI == MI) { 1326299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen CPUser &U = CPUsers[CPUIndex]; 13277a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach if (!isOffsetInRange(Offset, EndInsertOffset, U)) { 1328299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // Shift intertion point by one unit of alignment so it is within reach. 1329299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen BaseInsertOffset -= 1u << LogAlign; 1330299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen EndInsertOffset -= 1u << LogAlign; 1331299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen } 1332299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // This is overly conservative, as we don't account for CPEMIs being 1333299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // reused within the block, but it doesn't matter much. Also assume CPEs 1334299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // are added in order with alignment padding. We may eventually be able 1335299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // to pack the aligned CPEs better. 1336456ff46e668f361f79e8e91f145152089cd7d933Jakob Stoklund Olesen EndInsertOffset += U.CPEMI->getOperand(2).getImm(); 1337299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen CPUIndex++; 133888e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen } 1339719510a178a910feb1db707140011c32a30992c4Evan Cheng 1340299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // Remember the last IT instruction. 1341299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen if (MI->getOpcode() == ARM::t2IT) 1342299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen LastIT = MI; 1343299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen } 1344719510a178a910feb1db707140011c32a30992c4Evan Cheng 1345299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen --MI; 1346299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen 1347299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen // Avoid splitting an IT block. 1348299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen if (LastIT) { 1349299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen unsigned PredReg = 0; 1350c89c744b69cecac576317a98322fd295e36e9886Craig Topper ARMCC::CondCodes CC = getITInstrPredicate(MI, PredReg); 1351299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen if (CC != ARMCC::AL) 1352299b059cd675f1e5beac1a2383a44e062e2eef43Jakob Stoklund Olesen MI = LastIT; 1353b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen } 13547a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach NewMBB = splitBlockBeforeInstr(MI); 1355b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen} 1356b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen 13577a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// handleConstantPoolUser - Analyze the specified user, checking to see if it 135839bf051ec2865cd715c41473d2a9fc34ff252534Bob Wilson/// is out-of-range. If so, pick up the constant pool value and move it some 1359b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen/// place in-range. Return true if we changed any addresses (thus must run 1360b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen/// another pass of branch lengthening), false otherwise. 13617a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachbool ARMConstantIslands::handleConstantPoolUser(unsigned CPUserIndex) { 1362b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen CPUser &U = CPUsers[CPUserIndex]; 1363b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen MachineInstr *UserMI = U.MI; 1364b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen MachineInstr *CPEMI = U.CPEMI; 13658aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner unsigned CPI = CPEMI->getOperand(1).getIndex(); 1366b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen unsigned Size = CPEMI->getOperand(2).getImm(); 1367dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen // Compute this only once, it's expensive. 13687a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach unsigned UserOffset = getUserOffset(U); 1369768c9f725bda048b3406b6289beb586ff6e933dfEvan Cheng 1370b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen // See if the current entry is within range, or there is a clone of it 1371b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen // in range. 13727a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach int result = findInRangeCPEntry(U, UserOffset); 1373b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen if (result==1) return false; 1374b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen else if (result==2) return true; 1375b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen 1376b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen // No existing clone of this CPE is within range. 1377b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen // We will be generating a new clone. Get a UID for it. 13785de5d4b6d0eb3fd379fa571d82f6fa764460b3b8Evan Cheng unsigned ID = AFI->createPICLabelUId(); 1379b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen 1380f98032ef64a777fe63d390539f901d7d6d4e868cBob Wilson // Look for water where we can place this CPE. 1381dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MachineBasicBlock *NewIsland = MF->CreateMachineBasicBlock(); 1382b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson MachineBasicBlock *NewMBB; 1383b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson water_iterator IP; 13847a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach if (findAvailableWater(U, UserOffset, IP)) { 13853c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen DEBUG(dbgs() << "Found water in range\n"); 1386b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson MachineBasicBlock *WaterBB = *IP; 1387b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson 1388b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // If the original WaterList entry was "new water" on this iteration, 1389b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // propagate that to the new island. This is just keeping NewWaterList 1390b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // updated to match the WaterList, which will be updated below. 139105d96f98cbd96dab7f4ea1ea4ebe4285597e7e88Benjamin Kramer if (NewWaterList.erase(WaterBB)) 1392b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson NewWaterList.insert(NewIsland); 139305d96f98cbd96dab7f4ea1ea4ebe4285597e7e88Benjamin Kramer 1394b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // The new CPE goes before the following block (NewMBB). 13957896c9f436a4eda5ec15e882a7505ba482a2fcd0Chris Lattner NewMBB = llvm::next(MachineFunction::iterator(WaterBB)); 1396b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson 1397b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson } else { 1398b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen // No water found. 13993c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen DEBUG(dbgs() << "No water found\n"); 14007a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach createNewWater(CPUserIndex, UserOffset, NewMBB); 1401b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson 14027a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach // splitBlockBeforeInstr adds to WaterList, which is important when it is 1403b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // called while handling branches so that the water will be seen on the 1404b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // next iteration for constant pools, but in this context, we don't want 1405b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // it. Check for this so it will be removed from the WaterList. 1406b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // Also remove any entry from NewWaterList. 1407b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson MachineBasicBlock *WaterBB = prior(MachineFunction::iterator(NewMBB)); 1408b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson IP = std::find(WaterList.begin(), WaterList.end(), WaterBB); 1409b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson if (IP != WaterList.end()) 1410b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson NewWaterList.erase(WaterBB); 1411b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson 1412b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // We are adding new water. Update NewWaterList. 1413b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson NewWaterList.insert(NewIsland); 141488e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen } 141588e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen 1416b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // Remove the original WaterList entry; we want subsequent insertions in 1417b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // this vicinity to go after the one we're about to insert. This 1418b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // considerably reduces the number of times we have to move the same CPE 1419b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson // more than once and is also important to ensure the algorithm terminates. 1420b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson if (IP != WaterList.end()) 1421b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson WaterList.erase(IP); 1422b923953733ee6b0f477cd767dcc16f94a0966158Bob Wilson 142388e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // Okay, we know we can put an island before NewMBB now, do it! 1424dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MF->insert(NewMBB, NewIsland); 142588e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen 142688e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // Update internal data structures to account for the newly inserted MBB. 14277a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach updateForInsertedWaterBlock(NewIsland); 142888e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen 142988e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // Decrement the old entry, and remove it if refcount becomes 0. 14307a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach decrementCPEReferenceCount(CPI, CPEMI); 143188e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen 143288e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // Now that we have an island to add the CPE to, clone the original CPE and 143388e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen // add it to the island. 1434549dda9d82a5ec0a0cbf429a18d33a8e66062424Bob Wilson U.HighWaterMark = NewIsland; 1435c7f3ace20c325521c68335a1689645b43b06ddf0Chris Lattner U.CPEMI = BuildMI(NewIsland, DebugLoc(), TII->get(ARM::CONSTPOOL_ENTRY)) 1436a8e2989ece6dc46df59b0768184028257f913843Evan Cheng .addImm(ID).addConstantPoolIndex(CPI).addImm(Size); 143788e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1)); 1438fe60104ac97f3a8736dcfbfdf9547c7b7cc7b951Dan Gohman ++NumCPEs; 1439c99ef085b90b32952b484e8843fb66ad65215b61Evan Cheng 1440cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen // Mark the basic block as aligned as required by the const-pool entry. 1441cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen NewIsland->setAlignment(getCPELogAlign(U.CPEMI)); 14423e572ac2fbdf6aa538500be07b9b050ac008669eJakob Stoklund Olesen 1443a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Increase the size of the island block to account for the new entry. 1444a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen BBInfo[NewIsland->getNumber()].Size += Size; 14457a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach adjustBBOffsetsAfter(llvm::prior(MachineFunction::iterator(NewIsland))); 14468494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 1447a8e2989ece6dc46df59b0768184028257f913843Evan Cheng // Finally, change the CPI in the instruction operand to be ID. 1448a8e2989ece6dc46df59b0768184028257f913843Evan Cheng for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i) 1449d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman if (UserMI->getOperand(i).isCPI()) { 14508aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner UserMI->getOperand(i).setIndex(ID); 1451a8e2989ece6dc46df59b0768184028257f913843Evan Cheng break; 1452a8e2989ece6dc46df59b0768184028257f913843Evan Cheng } 14538494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 14543c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen DEBUG(dbgs() << " Moved CPE to #" << ID << " CPI=" << CPI 14552d5023bbcf816de39e37d9fea6dfa8f7aadb393fJakob Stoklund Olesen << format(" offset=%#x\n", BBInfo[NewIsland->getNumber()].Offset)); 14568494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 1457a8e2989ece6dc46df59b0768184028257f913843Evan Cheng return true; 1458a8e2989ece6dc46df59b0768184028257f913843Evan Cheng} 1459a8e2989ece6dc46df59b0768184028257f913843Evan Cheng 14607a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// removeDeadCPEMI - Remove a dead constant pool entry instruction. Update 1461ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng/// sizes and offsets of impacted basic blocks. 14627a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachvoid ARMConstantIslands::removeDeadCPEMI(MachineInstr *CPEMI) { 1463ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng MachineBasicBlock *CPEBB = CPEMI->getParent(); 14648593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen unsigned Size = CPEMI->getOperand(2).getImm(); 14658593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen CPEMI->eraseFromParent(); 1466a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen BBInfo[CPEBB->getNumber()].Size -= Size; 14678593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen // All succeeding offsets have the current size value added in, fix this. 1468ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng if (CPEBB->empty()) { 1469cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen BBInfo[CPEBB->getNumber()].Size = 0; 1470305e5fe797cbba32d8091d687d47e98c8f3ee1f4Jakob Stoklund Olesen 1471305e5fe797cbba32d8091d687d47e98c8f3ee1f4Jakob Stoklund Olesen // This block no longer needs to be aligned. <rdar://problem/10534709>. 1472305e5fe797cbba32d8091d687d47e98c8f3ee1f4Jakob Stoklund Olesen CPEBB->setAlignment(0); 1473cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen } else 1474cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen // Entries are sorted by descending alignment, so realign from the front. 1475cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen CPEBB->setAlignment(getCPELogAlign(CPEBB->begin())); 1476cca33a3f24106cfdb6cb892b76efc76f1ad91806Jakob Stoklund Olesen 14777a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach adjustBBOffsetsAfter(CPEBB); 14788593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen // An island has only one predecessor BB and one successor BB. Check if 14798593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen // this BB's predecessor jumps directly to this BB's successor. This 14808593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen // shouldn't happen currently. 14818593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen assert(!BBIsJumpedOver(CPEBB) && "How did this happen?"); 14828593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen // FIXME: remove the empty blocks after all the work is done? 1483ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng} 1484ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng 14857a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// removeUnusedCPEntries - Remove constant pool entries whose refcounts 1486ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng/// are zero. 14877a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachbool ARMConstantIslands::removeUnusedCPEntries() { 1488ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng unsigned MadeChange = false; 1489ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) { 1490ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng std::vector<CPEntry> &CPEs = CPEntries[i]; 1491ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) { 1492ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) { 14937a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach removeDeadCPEMI(CPEs[j].CPEMI); 1494ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng CPEs[j].CPEMI = NULL; 1495ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng MadeChange = true; 1496ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng } 1497ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng } 14988494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson } 1499ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng return MadeChange; 1500ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng} 1501ed884f3a2f7225a0ea40fc18964b6f949cac3680Evan Cheng 15027a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// isBBInRange - Returns true if the distance between specific MI and 150343aeab68a69e443c528092b4424a498d813f96b7Evan Cheng/// specific BB can fit in MI's displacement field. 15047a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachbool ARMConstantIslands::isBBInRange(MachineInstr *MI,MachineBasicBlock *DestBB, 1505c0dbec7e1038ee60b8525eb1e8e3eaa6a839bd5bEvan Cheng unsigned MaxDisp) { 1506b71aa2b6ca801d407c702483c412a290ab4af3b2Dale Johannesen unsigned PCAdj = isThumb ? 4 : 8; 15077a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach unsigned BrOffset = getOffsetOf(MI) + PCAdj; 1508a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset; 150943aeab68a69e443c528092b4424a498d813f96b7Evan Cheng 15103c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen DEBUG(dbgs() << "Branch of destination BB#" << DestBB->getNumber() 1511705e07f578e2b3af47ddab610feb4e7f2d3063a5Chris Lattner << " from BB#" << MI->getParent()->getNumber() 1512705e07f578e2b3af47ddab610feb4e7f2d3063a5Chris Lattner << " max delta=" << MaxDisp 15137a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach << " from " << getOffsetOf(MI) << " to " << DestOffset 1514705e07f578e2b3af47ddab610feb4e7f2d3063a5Chris Lattner << " offset " << int(DestOffset-BrOffset) << "\t" << *MI); 1515c0dbec7e1038ee60b8525eb1e8e3eaa6a839bd5bEvan Cheng 15168593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen if (BrOffset <= DestOffset) { 15178593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen // Branch before the Dest. 15188593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen if (DestOffset-BrOffset <= MaxDisp) 15198593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen return true; 15208593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen } else { 15218593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen if (BrOffset-DestOffset <= MaxDisp) 15228593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen return true; 15238593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen } 15248593e418555fdae21a070357fd8734b4069c17d0Dale Johannesen return false; 152543aeab68a69e443c528092b4424a498d813f96b7Evan Cheng} 152643aeab68a69e443c528092b4424a498d813f96b7Evan Cheng 15277a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// fixupImmediateBr - Fix up an immediate branch whose destination is too far 1528d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng/// away to fit in its displacement field. 15297a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachbool ARMConstantIslands::fixupImmediateBr(ImmBranch &Br) { 1530d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng MachineInstr *MI = Br.MI; 15318aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner MachineBasicBlock *DestBB = MI->getOperand(0).getMBB(); 1532d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng 1533c0dbec7e1038ee60b8525eb1e8e3eaa6a839bd5bEvan Cheng // Check to see if the DestBB is already in-range. 15347a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach if (isBBInRange(MI, DestBB, Br.MaxDisp)) 1535d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng return false; 1536d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng 1537d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng if (!Br.isCond) 15387a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach return fixupUnconditionalBr(Br); 15397a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach return fixupConditionalBr(Br); 1540d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng} 1541d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng 15427a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// fixupUnconditionalBr - Fix up an unconditional branch whose destination is 154388e37ae36652546c8e409cab69a1777af63cf095Dale Johannesen/// too far away to fit in its displacement field. If the LR register has been 1544d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng/// spilled in the epilogue, then we can use BL to implement a far jump. 154539bf051ec2865cd715c41473d2a9fc34ff252534Bob Wilson/// Otherwise, add an intermediate branch instruction to a branch. 1546d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Chengbool 15477a465250564d2ae0cc53ebe58732a751daa43e8dJim GrosbachARMConstantIslands::fixupUnconditionalBr(ImmBranch &Br) { 1548d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng MachineInstr *MI = Br.MI; 1549d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng MachineBasicBlock *MBB = MI->getParent(); 155053c67c0218d8420234dd71bf2d9df3388909af77Evan Cheng if (!isThumb1) 15517a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach llvm_unreachable("fixupUnconditionalBr is Thumb1 only!"); 1552d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng 1553d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng // Use BL to implement far jump. 1554d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng Br.MaxDisp = (1 << 21) * 2; 15555080f4d9919d39b367891dc51e739c571a66036cChris Lattner MI->setDesc(TII->get(ARM::tBfar)); 1556a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen BBInfo[MBB->getNumber()].Size += 2; 15577a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach adjustBBOffsetsAfter(MBB); 1558d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng HasFarJump = true; 1559fe60104ac97f3a8736dcfbfdf9547c7b7cc7b951Dan Gohman ++NumUBrFixed; 1560bd5d3dbdbe46c7325545a7cb8c891c0347375451Evan Cheng 15613c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen DEBUG(dbgs() << " Changed B to long jump " << *MI); 1562bd5d3dbdbe46c7325545a7cb8c891c0347375451Evan Cheng 1563d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng return true; 1564d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng} 1565d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng 15667a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// fixupConditionalBr - Fix up a conditional branch whose destination is too 1567d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng/// far away to fit in its displacement field. It is converted to an inverse 1568d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng/// conditional branch + an unconditional branch to the destination. 1569af5cbcb809bdbd30ebacab942994721c134d16a2Evan Chengbool 15707a465250564d2ae0cc53ebe58732a751daa43e8dJim GrosbachARMConstantIslands::fixupConditionalBr(ImmBranch &Br) { 1571af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng MachineInstr *MI = Br.MI; 15728aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner MachineBasicBlock *DestBB = MI->getOperand(0).getMBB(); 1573af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng 157439bf051ec2865cd715c41473d2a9fc34ff252534Bob Wilson // Add an unconditional branch to the destination and invert the branch 1575d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng // condition to jump over it: 1576af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng // blt L1 1577af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng // => 1578af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng // bge L2 1579af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng // b L1 1580af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng // L2: 15819a1ceaedc282f0cae31f2723f4d6c00c7b88fe90Chris Lattner ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImm(); 1582af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng CC = ARMCC::getOppositeCondition(CC); 15830e1d37904abbf7e8c4c478408253dd995f3040f4Evan Cheng unsigned CCReg = MI->getOperand(2).getReg(); 1584af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng 1585af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng // If the branch is at the end of its MBB and that has a fall-through block, 1586af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng // direct the updated conditional branch to the fall-through block. Otherwise, 1587af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng // split the MBB before the next instruction. 1588af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng MachineBasicBlock *MBB = MI->getParent(); 1589bd5d3dbdbe46c7325545a7cb8c891c0347375451Evan Cheng MachineInstr *BMI = &MBB->back(); 1590bd5d3dbdbe46c7325545a7cb8c891c0347375451Evan Cheng bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB); 159143aeab68a69e443c528092b4424a498d813f96b7Evan Cheng 1592fe60104ac97f3a8736dcfbfdf9547c7b7cc7b951Dan Gohman ++NumCBrFixed; 1593bd5d3dbdbe46c7325545a7cb8c891c0347375451Evan Cheng if (BMI != MI) { 15947896c9f436a4eda5ec15e882a7505ba482a2fcd0Chris Lattner if (llvm::next(MachineBasicBlock::iterator(MI)) == prior(MBB->end()) && 1595bd5d3dbdbe46c7325545a7cb8c891c0347375451Evan Cheng BMI->getOpcode() == Br.UncondBr) { 159639bf051ec2865cd715c41473d2a9fc34ff252534Bob Wilson // Last MI in the BB is an unconditional branch. Can we simply invert the 159743aeab68a69e443c528092b4424a498d813f96b7Evan Cheng // condition and swap destinations: 159843aeab68a69e443c528092b4424a498d813f96b7Evan Cheng // beq L1 159943aeab68a69e443c528092b4424a498d813f96b7Evan Cheng // b L2 160043aeab68a69e443c528092b4424a498d813f96b7Evan Cheng // => 160143aeab68a69e443c528092b4424a498d813f96b7Evan Cheng // bne L2 160243aeab68a69e443c528092b4424a498d813f96b7Evan Cheng // b L1 16038aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB(); 16047a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach if (isBBInRange(MI, NewDest, Br.MaxDisp)) { 16053c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen DEBUG(dbgs() << " Invert Bcc condition and swap its destination with " 1606705e07f578e2b3af47ddab610feb4e7f2d3063a5Chris Lattner << *BMI); 16078aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner BMI->getOperand(0).setMBB(DestBB); 16088aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner MI->getOperand(0).setMBB(NewDest); 160943aeab68a69e443c528092b4424a498d813f96b7Evan Cheng MI->getOperand(1).setImm(CC); 161043aeab68a69e443c528092b4424a498d813f96b7Evan Cheng return true; 161143aeab68a69e443c528092b4424a498d813f96b7Evan Cheng } 161243aeab68a69e443c528092b4424a498d813f96b7Evan Cheng } 161343aeab68a69e443c528092b4424a498d813f96b7Evan Cheng } 161443aeab68a69e443c528092b4424a498d813f96b7Evan Cheng 161543aeab68a69e443c528092b4424a498d813f96b7Evan Cheng if (NeedSplit) { 16167a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach splitBlockBeforeInstr(MI); 161739bf051ec2865cd715c41473d2a9fc34ff252534Bob Wilson // No need for the branch to the next block. We're adding an unconditional 1618dd353b8ad747a8731a191848ff5db978a70bd0fbEvan Cheng // branch to the destination. 161952e724ad7e679ee590f4bd763d55280586a8f1bcNicolas Geoffray int delta = TII->GetInstSizeInBytes(&MBB->back()); 1620a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen BBInfo[MBB->getNumber()].Size -= delta; 1621dd353b8ad747a8731a191848ff5db978a70bd0fbEvan Cheng MBB->back().eraseFromParent(); 1622a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen // BBInfo[SplitBB].Offset is wrong temporarily, fixed below 1623dd353b8ad747a8731a191848ff5db978a70bd0fbEvan Cheng } 16247896c9f436a4eda5ec15e882a7505ba482a2fcd0Chris Lattner MachineBasicBlock *NextBB = llvm::next(MachineFunction::iterator(MBB)); 16258494526a2396fd9cde027ab6ae11dd28b54ac90eBob Wilson 16263c4615eef2cb6b1bacb9b5fffe98359f9659f7c2Jakob Stoklund Olesen DEBUG(dbgs() << " Insert B to BB#" << DestBB->getNumber() 1627893e1c90a03a53cf13f73849324e83612688428aChris Lattner << " also invert condition and change dest. to BB#" 1628893e1c90a03a53cf13f73849324e83612688428aChris Lattner << NextBB->getNumber() << "\n"); 1629af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng 163056c42ef3e43f8eb75c435e781d0ac8251c7588a1Dale Johannesen // Insert a new conditional branch and a new unconditional branch. 1631af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng // Also update the ImmBranch as well as adding a new entry for the new branch. 1632c7f3ace20c325521c68335a1689645b43b06ddf0Chris Lattner BuildMI(MBB, DebugLoc(), TII->get(MI->getOpcode())) 1633b67284057ee130114055309eabe0bcd1af13777dDale Johannesen .addMBB(NextBB).addImm(CC).addReg(CCReg); 1634af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng Br.MI = &MBB->back(); 1635a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back()); 1636cd4338fff5cc9427003766519bebcfa213b32d61Owen Anderson if (isThumb) 1637cd4338fff5cc9427003766519bebcfa213b32d61Owen Anderson BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB) 1638cd4338fff5cc9427003766519bebcfa213b32d61Owen Anderson .addImm(ARMCC::AL).addReg(0); 1639cd4338fff5cc9427003766519bebcfa213b32d61Owen Anderson else 1640cd4338fff5cc9427003766519bebcfa213b32d61Owen Anderson BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB); 1641a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back()); 1642a9b8b8d62c67e96bc4dc2ed25298c539102f8638Evan Cheng unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr); 1643a0bf794eb60b6795a121efcb9ff759e9e0955772Evan Cheng ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr)); 164456c42ef3e43f8eb75c435e781d0ac8251c7588a1Dale Johannesen 164556c42ef3e43f8eb75c435e781d0ac8251c7588a1Dale Johannesen // Remove the old conditional branch. It may or may not still be in MBB. 1646a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen BBInfo[MI->getParent()->getNumber()].Size -= TII->GetInstSizeInBytes(MI); 1647af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng MI->eraseFromParent(); 16487a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach adjustBBOffsetsAfter(MBB); 1649af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng return true; 1650af5cbcb809bdbd30ebacab942994721c134d16a2Evan Cheng} 1651d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng 16527a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// undoLRSpillRestore - Remove Thumb push / pop instructions that only spills 16534b322e58b77d16f103d88a3af3a4ebd2675245a0Evan Cheng/// LR / restores LR to pc. FIXME: This is done here because it's only possible 16544b322e58b77d16f103d88a3af3a4ebd2675245a0Evan Cheng/// to do this if tBfar is not used. 16557a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachbool ARMConstantIslands::undoLRSpillRestore() { 1656d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng bool MadeChange = false; 1657d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) { 1658d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng MachineInstr *MI = PushPopMIs[i]; 1659815baebe1c8dc02accf128ae10dff9a1742d3244Bob Wilson // First two operands are predicates. 166044bec52b1b7e9a3ac1efbae90db240b8c1ca2ad4Evan Cheng if (MI->getOpcode() == ARM::tPOP_RET && 1661815baebe1c8dc02accf128ae10dff9a1742d3244Bob Wilson MI->getOperand(2).getReg() == ARM::PC && 1662815baebe1c8dc02accf128ae10dff9a1742d3244Bob Wilson MI->getNumExplicitOperands() == 3) { 166325e6d48220330d17875d0ae0ad6a45597d5ac36cJim Grosbach // Create the new insn and copy the predicate from the old. 166425e6d48220330d17875d0ae0ad6a45597d5ac36cJim Grosbach BuildMI(MI->getParent(), MI->getDebugLoc(), TII->get(ARM::tBX_RET)) 166525e6d48220330d17875d0ae0ad6a45597d5ac36cJim Grosbach .addOperand(MI->getOperand(0)) 166625e6d48220330d17875d0ae0ad6a45597d5ac36cJim Grosbach .addOperand(MI->getOperand(1)); 166744bec52b1b7e9a3ac1efbae90db240b8c1ca2ad4Evan Cheng MI->eraseFromParent(); 166844bec52b1b7e9a3ac1efbae90db240b8c1ca2ad4Evan Cheng MadeChange = true; 1669d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng } 1670d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng } 1671d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng return MadeChange; 1672d1b2c1e88fe4a7728ca9739b0f1c6fd90a19c5fdEvan Cheng} 16735657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng 16747a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach// mayOptimizeThumb2Instruction - Returns true if optimizeThumb2Instructions 167519d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesen// below may shrink MI. 167619d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesenbool 167719d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund OlesenARMConstantIslands::mayOptimizeThumb2Instruction(const MachineInstr *MI) const { 167819d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesen switch(MI->getOpcode()) { 16797a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach // optimizeThumb2Instructions. 168019d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesen case ARM::t2LEApcrel: 168119d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesen case ARM::t2LDRpci: 16827a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach // optimizeThumb2Branches. 168319d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesen case ARM::t2B: 168419d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesen case ARM::t2Bcc: 168519d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesen case ARM::tBcc: 16867a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach // optimizeThumb2JumpTables. 168719d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesen case ARM::t2BR_JT: 168819d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesen return true; 168919d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesen } 169019d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesen return false; 169119d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesen} 169219d0bf3a9273d337b776ca33c284fd2b5da485abJakob Stoklund Olesen 16937a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachbool ARMConstantIslands::optimizeThumb2Instructions() { 1694a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng bool MadeChange = false; 1695a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng 1696a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng // Shrink ADR and LDR from constantpool. 1697a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) { 1698a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng CPUser &U = CPUsers[i]; 1699a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng unsigned Opcode = U.MI->getOpcode(); 1700a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng unsigned NewOpc = 0; 1701a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng unsigned Scale = 1; 1702a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng unsigned Bits = 0; 1703a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng switch (Opcode) { 1704a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng default: break; 17056b8719fd7dc527e4c1910ae49ebee61d90907c08Owen Anderson case ARM::t2LEApcrel: 1706a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng if (isARMLowRegister(U.MI->getOperand(0).getReg())) { 1707a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng NewOpc = ARM::tLEApcrel; 1708a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng Bits = 8; 1709a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng Scale = 4; 1710a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng } 1711a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng break; 1712a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng case ARM::t2LDRpci: 1713a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng if (isARMLowRegister(U.MI->getOperand(0).getReg())) { 1714a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng NewOpc = ARM::tLDRpci; 1715a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng Bits = 8; 1716a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng Scale = 4; 1717a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng } 1718a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng break; 1719a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng } 1720a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng 1721a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng if (!NewOpc) 1722a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng continue; 1723a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng 17247a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach unsigned UserOffset = getUserOffset(U); 1725a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng unsigned MaxOffs = ((1 << Bits) - 1) * Scale; 1726dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen 1727dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen // Be conservative with inline asm. 1728dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen if (!U.KnownAlignment) 1729dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen MaxOffs -= 2; 1730dae412bd320f1522bd1d850be0276212fca0331fJakob Stoklund Olesen 1731a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng // FIXME: Check if offset is multiple of scale if scale is not 4. 17327a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach if (isCPEntryInRange(U.MI, UserOffset, U.CPEMI, MaxOffs, false, true)) { 1733101c03a8c9042781b6c9ba1fcc366e14fc8534ceJakob Stoklund Olesen DEBUG(dbgs() << "Shrink: " << *U.MI); 1734a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng U.MI->setDesc(TII->get(NewOpc)); 1735a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng MachineBasicBlock *MBB = U.MI->getParent(); 1736a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen BBInfo[MBB->getNumber()].Size -= 2; 17377a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach adjustBBOffsetsAfter(MBB); 1738a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng ++NumT2CPShrunk; 1739a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng MadeChange = true; 1740a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng } 1741a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng } 1742a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng 17437a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach MadeChange |= optimizeThumb2Branches(); 17447a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach MadeChange |= optimizeThumb2JumpTables(); 1745a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng return MadeChange; 1746a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng} 1747a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng 17487a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachbool ARMConstantIslands::optimizeThumb2Branches() { 174931b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng bool MadeChange = false; 175031b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng 175131b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) { 175231b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng ImmBranch &Br = ImmBranches[i]; 175331b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng unsigned Opcode = Br.MI->getOpcode(); 175431b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng unsigned NewOpc = 0; 175531b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng unsigned Scale = 1; 175631b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng unsigned Bits = 0; 175731b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng switch (Opcode) { 175831b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng default: break; 175931b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng case ARM::t2B: 176031b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng NewOpc = ARM::tB; 176131b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng Bits = 11; 176231b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng Scale = 2; 176331b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng break; 1764de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng case ARM::t2Bcc: { 176531b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng NewOpc = ARM::tBcc; 176631b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng Bits = 8; 1767de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng Scale = 2; 176831b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng break; 176931b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng } 1770de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng } 1771de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng if (NewOpc) { 1772de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale; 1773de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB(); 17747a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach if (isBBInRange(Br.MI, DestBB, MaxOffs)) { 1775101c03a8c9042781b6c9ba1fcc366e14fc8534ceJakob Stoklund Olesen DEBUG(dbgs() << "Shrink branch: " << *Br.MI); 1776de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng Br.MI->setDesc(TII->get(NewOpc)); 1777de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng MachineBasicBlock *MBB = Br.MI->getParent(); 1778a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen BBInfo[MBB->getNumber()].Size -= 2; 17797a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach adjustBBOffsetsAfter(MBB); 1780de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng ++NumT2BrShrunk; 1781de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng MadeChange = true; 1782de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng } 1783de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng } 1784de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng 1785de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng Opcode = Br.MI->getOpcode(); 1786de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng if (Opcode != ARM::tBcc) 178731b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng continue; 178831b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng 1789bfe8afaaec03795fe6c78daa9817e54c186a699dEvan Cheng // If the conditional branch doesn't kill CPSR, then CPSR can be liveout 1790bfe8afaaec03795fe6c78daa9817e54c186a699dEvan Cheng // so this transformation is not safe. 1791bfe8afaaec03795fe6c78daa9817e54c186a699dEvan Cheng if (!Br.MI->killsRegister(ARM::CPSR)) 1792bfe8afaaec03795fe6c78daa9817e54c186a699dEvan Cheng continue; 1793bfe8afaaec03795fe6c78daa9817e54c186a699dEvan Cheng 1794de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng NewOpc = 0; 1795de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng unsigned PredReg = 0; 1796c89c744b69cecac576317a98322fd295e36e9886Craig Topper ARMCC::CondCodes Pred = getInstrPredicate(Br.MI, PredReg); 1797de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng if (Pred == ARMCC::EQ) 1798de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng NewOpc = ARM::tCBZ; 1799de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng else if (Pred == ARMCC::NE) 1800de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng NewOpc = ARM::tCBNZ; 1801de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng if (!NewOpc) 1802de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng continue; 180331b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB(); 1804de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng // Check if the distance is within 126. Subtract starting offset by 2 1805de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng // because the cmp will be eliminated. 18067a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach unsigned BrOffset = getOffsetOf(Br.MI) + 4 - 2; 1807a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset; 1808de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng if (BrOffset < DestOffset && (DestOffset - BrOffset) <= 126) { 18090539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng MachineBasicBlock::iterator CmpMI = Br.MI; 18100539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng if (CmpMI != Br.MI->getParent()->begin()) { 18110539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng --CmpMI; 18120539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng if (CmpMI->getOpcode() == ARM::tCMPi8) { 18130539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng unsigned Reg = CmpMI->getOperand(0).getReg(); 1814c89c744b69cecac576317a98322fd295e36e9886Craig Topper Pred = getInstrPredicate(CmpMI, PredReg); 18150539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng if (Pred == ARMCC::AL && 18160539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng CmpMI->getOperand(1).getImm() == 0 && 18170539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng isARMLowRegister(Reg)) { 18180539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng MachineBasicBlock *MBB = Br.MI->getParent(); 1819101c03a8c9042781b6c9ba1fcc366e14fc8534ceJakob Stoklund Olesen DEBUG(dbgs() << "Fold: " << *CmpMI << " and: " << *Br.MI); 18200539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng MachineInstr *NewBR = 18210539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng BuildMI(*MBB, CmpMI, Br.MI->getDebugLoc(), TII->get(NewOpc)) 18220539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng .addReg(Reg).addMBB(DestBB,Br.MI->getOperand(0).getTargetFlags()); 18230539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng CmpMI->eraseFromParent(); 18240539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng Br.MI->eraseFromParent(); 18250539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng Br.MI = NewBR; 1826a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen BBInfo[MBB->getNumber()].Size -= 2; 18277a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach adjustBBOffsetsAfter(MBB); 18280539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng ++NumCBZ; 18290539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng MadeChange = true; 18300539c159fb6e3f8d09d4790d8d6a4615c26d3e98Evan Cheng } 1831de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng } 1832de17fb6e4dd8f169f707d58d4e124c8d4d2c7dbaEvan Cheng } 183331b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng } 183431b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng } 183531b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng 183631b99dd76035067b1c3cc6c7e9d663b7b0210938Evan Cheng return MadeChange; 1837a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng} 1838a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng 18397a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// optimizeThumb2JumpTables - Use tbb / tbh instructions to generate smaller 1840a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng/// jumptables when it's possible. 18417a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachbool ARMConstantIslands::optimizeThumb2JumpTables() { 18425657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng bool MadeChange = false; 18435657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng 18445657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng // FIXME: After the tables are shrunk, can we get rid some of the 18455657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng // constantpool tables? 1846dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MachineJumpTableInfo *MJTI = MF->getJumpTableInfo(); 1847b1e803985d3378538ae9cff7eed4102c002d1e22Chris Lattner if (MJTI == 0) return false; 184826b8ef53ff2ff1ffd72d140b5be264e32dee0195Jim Grosbach 18495657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); 18505657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) { 18515657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng MachineInstr *MI = T2JumpTables[i]; 1852e837dead3c8dc3445ef6a0e2322179c57e264a13Evan Cheng const MCInstrDesc &MCID = MI->getDesc(); 1853e837dead3c8dc3445ef6a0e2322179c57e264a13Evan Cheng unsigned NumOps = MCID.getNumOperands(); 18545a96b3dad2f634c9081c8b2b6c2575441dc5a2bdEvan Cheng unsigned JTOpIdx = NumOps - (MI->isPredicable() ? 3 : 2); 18555657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng MachineOperand JTOP = MI->getOperand(JTOpIdx); 18565657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng unsigned JTI = JTOP.getIndex(); 18575657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng assert(JTI < JT.size()); 18585657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng 18591fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach bool ByteOk = true; 18601fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach bool HalfWordOk = true; 18617a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach unsigned JTOffset = getOffsetOf(MI) + 4; 186280697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs; 18635657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) { 18645657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng MachineBasicBlock *MBB = JTBBs[j]; 1865a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen unsigned DstOffset = BBInfo[MBB->getNumber()].Offset; 18668770f747a98194056805f1b7cbcb0b75a7633b87Evan Cheng // Negative offset is not ok. FIXME: We should change BB layout to make 18678770f747a98194056805f1b7cbcb0b75a7633b87Evan Cheng // sure all the branches are forward. 1868d26b14c34cbcee1448b86b524578fc51cc979023Evan Cheng if (ByteOk && (DstOffset - JTOffset) > ((1<<8)-1)*2) 18695657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng ByteOk = false; 187025f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng unsigned TBHLimit = ((1<<16)-1)*2; 187125f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng if (HalfWordOk && (DstOffset - JTOffset) > TBHLimit) 18725657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng HalfWordOk = false; 18735657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng if (!ByteOk && !HalfWordOk) 18745657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng break; 18755657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng } 18765657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng 18775657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng if (ByteOk || HalfWordOk) { 18785657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng MachineBasicBlock *MBB = MI->getParent(); 18795657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng unsigned BaseReg = MI->getOperand(0).getReg(); 18805657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng bool BaseRegKill = MI->getOperand(0).isKill(); 18815657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng if (!BaseRegKill) 18825657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng continue; 18835657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng unsigned IdxReg = MI->getOperand(1).getReg(); 18845657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng bool IdxRegKill = MI->getOperand(1).isKill(); 1885c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach 1886c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach // Scan backwards to find the instruction that defines the base 1887c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach // register. Due to post-RA scheduling, we can't count on it 1888c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach // immediately preceding the branch instruction. 18895657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng MachineBasicBlock::iterator PrevI = MI; 1890c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach MachineBasicBlock::iterator B = MBB->begin(); 1891c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach while (PrevI != B && !PrevI->definesRegister(BaseReg)) 1892c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach --PrevI; 1893c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach 1894c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach // If for some reason we didn't find it, we can't do anything, so 1895c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach // just skip this one. 1896c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach if (!PrevI->definesRegister(BaseReg)) 18975657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng continue; 18985657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng 1899c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach MachineInstr *AddrMI = PrevI; 19005657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng bool OptOk = true; 190126b8ef53ff2ff1ffd72d140b5be264e32dee0195Jim Grosbach // Examine the instruction that calculates the jumptable entry address. 1902c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach // Make sure it only defines the base register and kills any uses 1903c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach // other than the index register. 19045657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng for (unsigned k = 0, eee = AddrMI->getNumOperands(); k != eee; ++k) { 19055657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng const MachineOperand &MO = AddrMI->getOperand(k); 19065657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng if (!MO.isReg() || !MO.getReg()) 19075657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng continue; 19085657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng if (MO.isDef() && MO.getReg() != BaseReg) { 19095657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng OptOk = false; 19105657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng break; 19115657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng } 19125657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng if (MO.isUse() && !MO.isKill() && MO.getReg() != IdxReg) { 19135657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng OptOk = false; 19145657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng break; 19155657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng } 19165657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng } 19175657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng if (!OptOk) 19185657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng continue; 19195657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng 19206b8719fd7dc527e4c1910ae49ebee61d90907c08Owen Anderson // Now scan back again to find the tLEApcrel or t2LEApcrelJT instruction 1921c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach // that gave us the initial base register definition. 1922c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach for (--PrevI; PrevI != B && !PrevI->definesRegister(BaseReg); --PrevI) 1923c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach ; 1924c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach 19256b8719fd7dc527e4c1910ae49ebee61d90907c08Owen Anderson // The instruction should be a tLEApcrel or t2LEApcrelJT; we want 1926a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng // to delete it as well. 1927c7937ae025194cb62ffcd592785c3ad97e7882ceJim Grosbach MachineInstr *LeaMI = PrevI; 1928a1efbbdbf3217598f334a6f39dab84ca06f5de41Evan Cheng if ((LeaMI->getOpcode() != ARM::tLEApcrelJT && 19296b8719fd7dc527e4c1910ae49ebee61d90907c08Owen Anderson LeaMI->getOpcode() != ARM::t2LEApcrelJT) || 19305657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng LeaMI->getOperand(0).getReg() != BaseReg) 193125f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng OptOk = false; 193225f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng 193325f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng if (!OptOk) 193425f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng continue; 193525f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng 1936101c03a8c9042781b6c9ba1fcc366e14fc8534ceJakob Stoklund Olesen DEBUG(dbgs() << "Shrink JT: " << *MI << " addr: " << *AddrMI 1937101c03a8c9042781b6c9ba1fcc366e14fc8534ceJakob Stoklund Olesen << " lea: " << *LeaMI); 1938d092a87ba3f905a6801a0bdf816267329cf0391cJim Grosbach unsigned Opc = ByteOk ? ARM::t2TBB_JT : ARM::t2TBH_JT; 193925f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng MachineInstr *NewJTMI = BuildMI(MBB, MI->getDebugLoc(), TII->get(Opc)) 194025f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng .addReg(IdxReg, getKillRegState(IdxRegKill)) 194125f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng .addJumpTableIndex(JTI, JTOP.getTargetFlags()) 194225f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng .addImm(MI->getOperand(JTOpIdx+1).getImm()); 1943101c03a8c9042781b6c9ba1fcc366e14fc8534ceJakob Stoklund Olesen DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": " << *NewJTMI); 194425f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng // FIXME: Insert an "ALIGN" instruction to ensure the next instruction 194525f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng // is 2-byte aligned. For now, asm printer will fix it up. 194625f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng unsigned NewSize = TII->GetInstSizeInBytes(NewJTMI); 194725f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng unsigned OrigSize = TII->GetInstSizeInBytes(AddrMI); 194825f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng OrigSize += TII->GetInstSizeInBytes(LeaMI); 194925f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng OrigSize += TII->GetInstSizeInBytes(MI); 195025f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng 195125f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng AddrMI->eraseFromParent(); 195225f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng LeaMI->eraseFromParent(); 195325f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng MI->eraseFromParent(); 195425f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng 195525f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng int delta = OrigSize - NewSize; 1956a3f331bd81f9d468654635df4428ecc8d8ebb5eaJakob Stoklund Olesen BBInfo[MBB->getNumber()].Size -= delta; 19577a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach adjustBBOffsetsAfter(MBB); 195825f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng 195925f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng ++NumTBs; 196025f7cfc3cccba6f569f29f79ea533bae960b93c0Evan Cheng MadeChange = true; 19615657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng } 19625657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng } 19635657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng 19645657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng return MadeChange; 19655657c01949dca6c012ac60d242d1a8d2ffdf5603Evan Cheng} 19661fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach 19677a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach/// reorderThumb2JumpTables - Adjust the function's block layout to ensure that 19689249efe4c7451e83fb4c904fd7fcc8edfe08d815Jim Grosbach/// jump tables always branch forwards, since that's what tbb and tbh need. 19697a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbachbool ARMConstantIslands::reorderThumb2JumpTables() { 197080697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach bool MadeChange = false; 197180697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach 1972dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MachineJumpTableInfo *MJTI = MF->getJumpTableInfo(); 1973b1e803985d3378538ae9cff7eed4102c002d1e22Chris Lattner if (MJTI == 0) return false; 197426b8ef53ff2ff1ffd72d140b5be264e32dee0195Jim Grosbach 197580697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); 197680697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) { 197780697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach MachineInstr *MI = T2JumpTables[i]; 1978e837dead3c8dc3445ef6a0e2322179c57e264a13Evan Cheng const MCInstrDesc &MCID = MI->getDesc(); 1979e837dead3c8dc3445ef6a0e2322179c57e264a13Evan Cheng unsigned NumOps = MCID.getNumOperands(); 19805a96b3dad2f634c9081c8b2b6c2575441dc5a2bdEvan Cheng unsigned JTOpIdx = NumOps - (MI->isPredicable() ? 3 : 2); 198180697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach MachineOperand JTOP = MI->getOperand(JTOpIdx); 198280697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach unsigned JTI = JTOP.getIndex(); 198380697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach assert(JTI < JT.size()); 198480697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach 198580697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach // We prefer if target blocks for the jump table come after the jump 198680697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach // instruction so we can use TB[BH]. Loop through the target blocks 198780697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach // and try to adjust them such that that's true. 198808cbda56b6a45a2c29bdf3d9668ad614ad86cb77Jim Grosbach int JTNumber = MI->getParent()->getNumber(); 198980697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs; 199080697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) { 199180697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach MachineBasicBlock *MBB = JTBBs[j]; 199208cbda56b6a45a2c29bdf3d9668ad614ad86cb77Jim Grosbach int DTNumber = MBB->getNumber(); 199380697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach 199408cbda56b6a45a2c29bdf3d9668ad614ad86cb77Jim Grosbach if (DTNumber < JTNumber) { 199580697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach // The destination precedes the switch. Try to move the block forward 199680697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach // so we have a positive offset. 199780697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach MachineBasicBlock *NewBB = 19987a465250564d2ae0cc53ebe58732a751daa43e8dJim Grosbach adjustJTTargetBlockForward(MBB, MI->getParent()); 199980697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach if (NewBB) 200000a6a1f0225b01229134ecee7a853be638703c36Jim Grosbach MJTI->ReplaceMBBInJumpTable(JTI, JTBBs[j], NewBB); 200180697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach MadeChange = true; 200280697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach } 200380697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach } 200480697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach } 200580697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach 200680697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach return MadeChange; 200780697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach} 200880697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach 20091fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim GrosbachMachineBasicBlock *ARMConstantIslands:: 20107a465250564d2ae0cc53ebe58732a751daa43e8dJim GrosbachadjustJTTargetBlockForward(MachineBasicBlock *BB, MachineBasicBlock *JTBB) { 201103e2d44722c1ebd3953007db0579147b46efba18Jim Grosbach // If the destination block is terminated by an unconditional branch, 201280697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach // try to move it; otherwise, create a new block following the jump 201308cbda56b6a45a2c29bdf3d9668ad614ad86cb77Jim Grosbach // table that branches back to the actual target. This is a very simple 201408cbda56b6a45a2c29bdf3d9668ad614ad86cb77Jim Grosbach // heuristic. FIXME: We can definitely improve it. 201580697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach MachineBasicBlock *TBB = 0, *FBB = 0; 201680697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach SmallVector<MachineOperand, 4> Cond; 2017a0a95a3c1cea79757854cf39ba07734b39e2b0e9Jim Grosbach SmallVector<MachineOperand, 4> CondPrior; 2018a0a95a3c1cea79757854cf39ba07734b39e2b0e9Jim Grosbach MachineFunction::iterator BBi = BB; 2019a0a95a3c1cea79757854cf39ba07734b39e2b0e9Jim Grosbach MachineFunction::iterator OldPrior = prior(BBi); 202000a6a1f0225b01229134ecee7a853be638703c36Jim Grosbach 2021ca215e7804ca5d85e19e31850c3ba3d155624e89Jim Grosbach // If the block terminator isn't analyzable, don't try to move the block 2022a0a95a3c1cea79757854cf39ba07734b39e2b0e9Jim Grosbach bool B = TII->AnalyzeBranch(*BB, TBB, FBB, Cond); 2023ca215e7804ca5d85e19e31850c3ba3d155624e89Jim Grosbach 2024a0a95a3c1cea79757854cf39ba07734b39e2b0e9Jim Grosbach // If the block ends in an unconditional branch, move it. The prior block 2025a0a95a3c1cea79757854cf39ba07734b39e2b0e9Jim Grosbach // has to have an analyzable terminator for us to move this one. Be paranoid 202608cbda56b6a45a2c29bdf3d9668ad614ad86cb77Jim Grosbach // and make sure we're not trying to move the entry block of the function. 2027dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen if (!B && Cond.empty() && BB != MF->begin() && 2028a0a95a3c1cea79757854cf39ba07734b39e2b0e9Jim Grosbach !TII->AnalyzeBranch(*OldPrior, TBB, FBB, CondPrior)) { 202980697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach BB->moveAfter(JTBB); 203080697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach OldPrior->updateTerminator(); 203100a6a1f0225b01229134ecee7a853be638703c36Jim Grosbach BB->updateTerminator(); 203208cbda56b6a45a2c29bdf3d9668ad614ad86cb77Jim Grosbach // Update numbering to account for the block being moved. 2033dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MF->RenumberBlocks(); 203480697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach ++NumJTMoved; 203580697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach return NULL; 203680697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach } 20371fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach 20381fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach // Create a new MBB for the code after the jump BB. 20391fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach MachineBasicBlock *NewBB = 2040dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MF->CreateMachineBasicBlock(JTBB->getBasicBlock()); 20411fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach MachineFunction::iterator MBBI = JTBB; ++MBBI; 2042dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MF->insert(MBBI, NewBB); 20431fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach 20441fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach // Add an unconditional branch from NewBB to BB. 20451fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach // There doesn't seem to be meaningful DebugInfo available; this doesn't 20461fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach // correspond directly to anything in the source. 20471fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach assert (isThumb2 && "Adjusting for TB[BH] but not in Thumb2?"); 204851f6a7abf27fc92c3d8904c2334feab8b498e8e9Owen Anderson BuildMI(NewBB, DebugLoc(), TII->get(ARM::t2B)).addMBB(BB) 204951f6a7abf27fc92c3d8904c2334feab8b498e8e9Owen Anderson .addImm(ARMCC::AL).addReg(0); 20501fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach 205100a6a1f0225b01229134ecee7a853be638703c36Jim Grosbach // Update internal data structures to account for the newly inserted MBB. 2052dbf350a5a8f5fb5321a4e11e381f0023fbe61d15Jakob Stoklund Olesen MF->RenumberBlocks(NewBB); 205300a6a1f0225b01229134ecee7a853be638703c36Jim Grosbach 20541fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach // Update the CFG. 20551fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach NewBB->addSuccessor(BB); 20561fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach JTBB->removeSuccessor(BB); 20571fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach JTBB->addSuccessor(NewBB); 20581fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach 205980697d1b266cd3cd3b92edd424d66d573db6b4b1Jim Grosbach ++NumJTInserted; 20601fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach return NewBB; 20611fc7d715aa35703fcc4a87ece718a8c447e8ee72Jim Grosbach} 2062