2010-12-21 01:54:40 +00:00
|
|
|
//===-- ARMConstantIslandPass.cpp - ARM constant islands ------------------===//
|
2007-01-19 07:51:42 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-01-19 07:51:42 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a pass that splits the constant pool up into 'islands'
|
|
|
|
// which are scattered through-out the function. This is required due to the
|
|
|
|
// limited pc-relative displacements that ARM has.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ARM.h"
|
2007-01-25 03:12:46 +00:00
|
|
|
#include "ARMMachineFunctionInfo.h"
|
2011-07-20 23:34:39 +00:00
|
|
|
#include "MCTargetDesc/ARMAddressingModes.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "Thumb2InstrInfo.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2007-01-19 07:51:42 +00:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2009-07-29 02:18:14 +00:00
|
|
|
#include "llvm/CodeGen/MachineJumpTableInfo.h"
|
2012-03-29 23:14:26 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2007-01-19 07:51:42 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2011-12-10 02:55:06 +00:00
|
|
|
#include "llvm/Support/Format.h"
|
2009-08-23 03:41:05 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2009-10-15 20:49:47 +00:00
|
|
|
#include <algorithm>
|
2007-01-19 07:51:42 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:41:26 +00:00
|
|
|
#define DEBUG_TYPE "arm-cp-islands"
|
|
|
|
|
2009-08-14 00:32:16 +00:00
|
|
|
STATISTIC(NumCPEs, "Number of constpool entries");
|
|
|
|
STATISTIC(NumSplit, "Number of uncond branches inserted");
|
|
|
|
STATISTIC(NumCBrFixed, "Number of cond branches fixed");
|
|
|
|
STATISTIC(NumUBrFixed, "Number of uncond branches fixed");
|
|
|
|
STATISTIC(NumTBs, "Number of table branches generated");
|
|
|
|
STATISTIC(NumT2CPShrunk, "Number of Thumb2 constantpool instructions shrunk");
|
2009-08-14 18:31:44 +00:00
|
|
|
STATISTIC(NumT2BrShrunk, "Number of Thumb2 immediate branches shrunk");
|
2009-10-31 23:46:45 +00:00
|
|
|
STATISTIC(NumCBZ, "Number of CBZ / CBNZ formed");
|
2009-11-11 02:47:19 +00:00
|
|
|
STATISTIC(NumJTMoved, "Number of jump table destination blocks moved");
|
2009-11-12 17:25:07 +00:00
|
|
|
STATISTIC(NumJTInserted, "Number of jump table intermediate blocks inserted");
|
2009-11-11 02:47:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
2009-11-17 21:24:11 +00:00
|
|
|
AdjustJumpTableBlocks("arm-adjust-jump-tables", cl::Hidden, cl::init(true),
|
2009-11-11 02:47:19 +00:00
|
|
|
cl::desc("Adjust basic block layout to better use TB[BH]"));
|
2007-01-19 07:51:42 +00:00
|
|
|
|
2011-12-16 16:07:41 +00:00
|
|
|
// FIXME: This option should be removed once it has received sufficient testing.
|
2011-12-12 16:49:37 +00:00
|
|
|
static cl::opt<bool>
|
2011-12-15 22:14:45 +00:00
|
|
|
AlignConstantIslands("arm-align-constant-islands", cl::Hidden, cl::init(true),
|
2011-12-12 16:49:37 +00:00
|
|
|
cl::desc("Align constant islands in code"));
|
|
|
|
|
2011-12-10 02:55:10 +00:00
|
|
|
/// UnknownPadding - Return the worst case padding that could result from
|
|
|
|
/// unknown offset bits. This does not include alignment padding caused by
|
|
|
|
/// known offset bits.
|
|
|
|
///
|
|
|
|
/// @param LogAlign log2(alignment)
|
|
|
|
/// @param KnownBits Number of known low offset bits.
|
|
|
|
static inline unsigned UnknownPadding(unsigned LogAlign, unsigned KnownBits) {
|
|
|
|
if (KnownBits < LogAlign)
|
|
|
|
return (1u << LogAlign) - (1u << KnownBits);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
namespace {
|
2007-02-23 05:02:36 +00:00
|
|
|
/// ARMConstantIslands - Due to limited PC-relative displacements, ARM
|
2007-01-19 07:51:42 +00:00
|
|
|
/// requires constant pool entries to be scattered among the instructions
|
|
|
|
/// inside a function. To do this, it completely ignores the normal LLVM
|
2007-02-23 05:02:36 +00:00
|
|
|
/// constant pool; instead, it places constants wherever it feels like with
|
2007-01-19 07:51:42 +00:00
|
|
|
/// special instructions.
|
|
|
|
///
|
|
|
|
/// The terminology used in this pass includes:
|
|
|
|
/// Islands - Clumps of constants placed in the function.
|
|
|
|
/// Water - Potential places where an island could be formed.
|
|
|
|
/// CPE - A constant pool entry that has been placed somewhere, which
|
|
|
|
/// tracks a list of users.
|
2009-10-25 06:33:48 +00:00
|
|
|
class ARMConstantIslands : public MachineFunctionPass {
|
2011-12-07 01:08:25 +00:00
|
|
|
/// BasicBlockInfo - Information about the offset and size of a single
|
|
|
|
/// basic block.
|
|
|
|
struct BasicBlockInfo {
|
|
|
|
/// Offset - Distance from the beginning of the function to the beginning
|
|
|
|
/// of this basic block.
|
|
|
|
///
|
2012-04-27 22:58:38 +00:00
|
|
|
/// Offsets are computed assuming worst case padding before an aligned
|
|
|
|
/// block. This means that subtracting basic block offsets always gives a
|
|
|
|
/// conservative estimate of the real distance which may be smaller.
|
|
|
|
///
|
|
|
|
/// Because worst case padding is used, the computed offset of an aligned
|
|
|
|
/// block may not actually be aligned.
|
2011-12-07 01:08:25 +00:00
|
|
|
unsigned Offset;
|
|
|
|
|
|
|
|
/// Size - Size of the basic block in bytes. If the block contains
|
|
|
|
/// inline assembly, this is a worst case estimate.
|
|
|
|
///
|
2011-12-08 00:55:02 +00:00
|
|
|
/// The size does not include any alignment padding whether from the
|
|
|
|
/// beginning of the block, or from an aligned jump table at the end.
|
2011-12-07 01:08:25 +00:00
|
|
|
unsigned Size;
|
|
|
|
|
2011-12-08 00:55:02 +00:00
|
|
|
/// KnownBits - The number of low bits in Offset that are known to be
|
|
|
|
/// exact. The remaining bits of Offset are an upper bound.
|
|
|
|
uint8_t KnownBits;
|
|
|
|
|
2011-12-07 04:17:35 +00:00
|
|
|
/// Unalign - When non-zero, the block contains instructions (inline asm)
|
|
|
|
/// of unknown size. The real size may be smaller than Size bytes by a
|
|
|
|
/// multiple of 1 << Unalign.
|
|
|
|
uint8_t Unalign;
|
|
|
|
|
|
|
|
/// PostAlign - When non-zero, the block terminator contains a .align
|
|
|
|
/// directive, so the end of the block is aligned to 1 << PostAlign
|
|
|
|
/// bytes.
|
|
|
|
uint8_t PostAlign;
|
|
|
|
|
2011-12-08 00:55:02 +00:00
|
|
|
BasicBlockInfo() : Offset(0), Size(0), KnownBits(0), Unalign(0),
|
|
|
|
PostAlign(0) {}
|
2011-12-07 01:22:52 +00:00
|
|
|
|
2011-12-10 02:55:10 +00:00
|
|
|
/// Compute the number of known offset bits internally to this block.
|
|
|
|
/// This number should be used to predict worst case padding when
|
|
|
|
/// splitting the block.
|
|
|
|
unsigned internalKnownBits() const {
|
2012-04-30 20:19:00 +00:00
|
|
|
unsigned Bits = Unalign ? Unalign : KnownBits;
|
|
|
|
// If the block size isn't a multiple of the known bits, assume the
|
|
|
|
// worst case padding.
|
|
|
|
if (Size & ((1u << Bits) - 1))
|
2013-05-24 22:23:49 +00:00
|
|
|
Bits = countTrailingZeros(Size);
|
2012-04-30 20:19:00 +00:00
|
|
|
return Bits;
|
2011-12-10 02:55:10 +00:00
|
|
|
}
|
|
|
|
|
2011-12-12 19:25:54 +00:00
|
|
|
/// Compute the offset immediately following this block. If LogAlign is
|
|
|
|
/// specified, return the offset the successor block will get if it has
|
|
|
|
/// this alignment.
|
|
|
|
unsigned postOffset(unsigned LogAlign = 0) const {
|
2011-12-08 00:55:02 +00:00
|
|
|
unsigned PO = Offset + Size;
|
2011-12-12 19:25:54 +00:00
|
|
|
unsigned LA = std::max(unsigned(PostAlign), LogAlign);
|
|
|
|
if (!LA)
|
2011-12-08 00:55:02 +00:00
|
|
|
return PO;
|
|
|
|
// Add alignment padding from the terminator.
|
2012-04-27 22:58:38 +00:00
|
|
|
return PO + UnknownPadding(LA, internalKnownBits());
|
2011-12-08 00:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Compute the number of known low bits of postOffset. If this block
|
|
|
|
/// contains inline asm, the number of known bits drops to the
|
|
|
|
/// instruction alignment. An aligned terminator may increase the number
|
|
|
|
/// of know bits.
|
2011-12-12 19:25:54 +00:00
|
|
|
/// If LogAlign is given, also consider the alignment of the next block.
|
|
|
|
unsigned postKnownBits(unsigned LogAlign = 0) const {
|
|
|
|
return std::max(std::max(unsigned(PostAlign), LogAlign),
|
|
|
|
internalKnownBits());
|
2011-12-08 00:55:02 +00:00
|
|
|
}
|
2011-12-07 01:08:25 +00:00
|
|
|
};
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2011-12-07 01:08:25 +00:00
|
|
|
std::vector<BasicBlockInfo> BBInfo;
|
2007-02-25 00:47:03 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
/// WaterList - A sorted list of basic blocks where islands could be placed
|
|
|
|
/// (i.e. blocks that don't fall through to the following block, due
|
|
|
|
/// to a return, unreachable, or unconditional branch).
|
2007-02-09 23:59:14 +00:00
|
|
|
std::vector<MachineBasicBlock*> WaterList;
|
2007-02-09 20:54:44 +00:00
|
|
|
|
2009-10-15 20:49:47 +00:00
|
|
|
/// NewWaterList - The subset of WaterList that was created since the
|
|
|
|
/// previous iteration by inserting unconditional branches.
|
|
|
|
SmallSet<MachineBasicBlock*, 4> NewWaterList;
|
|
|
|
|
2009-10-12 18:52:13 +00:00
|
|
|
typedef std::vector<MachineBasicBlock*>::iterator water_iterator;
|
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
/// CPUser - One user of a constant pool, keeping the machine instruction
|
|
|
|
/// pointer, the constant pool being referenced, and the max displacement
|
2009-10-15 05:52:29 +00:00
|
|
|
/// allowed from the instruction to the CP. The HighWaterMark records the
|
|
|
|
/// highest basic block where a new CPEntry can be placed. To ensure this
|
|
|
|
/// pass terminates, the CP entries are initially placed at the end of the
|
|
|
|
/// function and then move monotonically to lower addresses. The
|
|
|
|
/// exception to this rule is when the current CP entry for a particular
|
|
|
|
/// CPUser is out of range, but there is another CP entry for the same
|
|
|
|
/// constant value in range. We want to use the existing in-range CP
|
|
|
|
/// entry, but if it later moves out of range, the search for new water
|
|
|
|
/// should resume where it left off. The HighWaterMark is used to record
|
|
|
|
/// that point.
|
2007-01-19 07:51:42 +00:00
|
|
|
struct CPUser {
|
|
|
|
MachineInstr *MI;
|
|
|
|
MachineInstr *CPEMI;
|
2009-10-15 05:52:29 +00:00
|
|
|
MachineBasicBlock *HighWaterMark;
|
2012-01-10 01:34:59 +00:00
|
|
|
private:
|
2007-01-19 07:51:42 +00:00
|
|
|
unsigned MaxDisp;
|
2012-01-10 01:34:59 +00:00
|
|
|
public:
|
2009-07-21 23:56:01 +00:00
|
|
|
bool NegOk;
|
2009-07-23 18:27:47 +00:00
|
|
|
bool IsSoImm;
|
2012-01-10 01:34:59 +00:00
|
|
|
bool KnownAlignment;
|
2009-07-23 18:27:47 +00:00
|
|
|
CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp,
|
|
|
|
bool neg, bool soimm)
|
2012-01-10 01:34:59 +00:00
|
|
|
: MI(mi), CPEMI(cpemi), MaxDisp(maxdisp), NegOk(neg), IsSoImm(soimm),
|
|
|
|
KnownAlignment(false) {
|
2009-10-15 05:52:29 +00:00
|
|
|
HighWaterMark = CPEMI->getParent();
|
|
|
|
}
|
2012-01-10 01:34:59 +00:00
|
|
|
/// getMaxDisp - Returns the maximum displacement supported by MI.
|
|
|
|
/// Correct for unknown alignment.
|
2012-03-31 00:06:44 +00:00
|
|
|
/// Conservatively subtract 2 bytes to handle weird alignment effects.
|
2012-01-10 01:34:59 +00:00
|
|
|
unsigned getMaxDisp() const {
|
2012-03-31 00:06:44 +00:00
|
|
|
return (KnownAlignment ? MaxDisp : MaxDisp - 2) - 2;
|
2012-01-10 01:34:59 +00:00
|
|
|
}
|
2007-01-19 07:51:42 +00:00
|
|
|
};
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
/// CPUsers - Keep track of all of the machine instructions that use various
|
|
|
|
/// constant pools and their max displacement.
|
2007-02-09 23:59:14 +00:00
|
|
|
std::vector<CPUser> CPUsers;
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-02-09 20:54:44 +00:00
|
|
|
/// CPEntry - One per constant pool entry, keeping the machine instruction
|
|
|
|
/// pointer, the constpool index, and the number of CPUser's which
|
|
|
|
/// reference this entry.
|
|
|
|
struct CPEntry {
|
|
|
|
MachineInstr *CPEMI;
|
|
|
|
unsigned CPI;
|
|
|
|
unsigned RefCount;
|
|
|
|
CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0)
|
|
|
|
: CPEMI(cpemi), CPI(cpi), RefCount(rc) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// CPEntries - Keep track of all of the constant pool entry machine
|
2007-02-23 05:02:36 +00:00
|
|
|
/// instructions. For each original constpool index (i.e. those that
|
|
|
|
/// existed upon entry to this pass), it keeps a vector of entries.
|
|
|
|
/// Original elements are cloned as we go along; the clones are
|
|
|
|
/// put in the vector of the original element, but have distinct CPIs.
|
2007-02-09 20:54:44 +00:00
|
|
|
std::vector<std::vector<CPEntry> > CPEntries;
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-25 03:12:46 +00:00
|
|
|
/// ImmBranch - One per immediate branch, keeping the machine instruction
|
|
|
|
/// pointer, conditional or unconditional, the max displacement,
|
|
|
|
/// and (if isCond is true) the corresponding unconditional branch
|
|
|
|
/// opcode.
|
|
|
|
struct ImmBranch {
|
|
|
|
MachineInstr *MI;
|
2007-01-25 23:18:59 +00:00
|
|
|
unsigned MaxDisp : 31;
|
|
|
|
bool isCond : 1;
|
2007-01-25 03:12:46 +00:00
|
|
|
int UncondBr;
|
2007-01-25 23:18:59 +00:00
|
|
|
ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr)
|
|
|
|
: MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {}
|
2007-01-25 03:12:46 +00:00
|
|
|
};
|
|
|
|
|
2007-05-16 05:14:06 +00:00
|
|
|
/// ImmBranches - Keep track of all the immediate branch instructions.
|
2007-01-25 03:12:46 +00:00
|
|
|
///
|
2007-02-09 23:59:14 +00:00
|
|
|
std::vector<ImmBranch> ImmBranches;
|
2007-01-25 03:12:46 +00:00
|
|
|
|
2007-01-30 01:18:38 +00:00
|
|
|
/// PushPopMIs - Keep track of all the Thumb push / pop instructions.
|
|
|
|
///
|
2007-02-09 20:54:44 +00:00
|
|
|
SmallVector<MachineInstr*, 4> PushPopMIs;
|
2007-01-30 01:18:38 +00:00
|
|
|
|
2009-07-29 02:18:14 +00:00
|
|
|
/// T2JumpTables - Keep track of all the Thumb2 jumptable instructions.
|
|
|
|
SmallVector<MachineInstr*, 4> T2JumpTables;
|
|
|
|
|
2007-01-30 01:18:38 +00:00
|
|
|
/// HasFarJump - True if any far jump instruction has been emitted during
|
|
|
|
/// the branch fix up pass.
|
|
|
|
bool HasFarJump;
|
|
|
|
|
2011-12-12 18:16:53 +00:00
|
|
|
MachineFunction *MF;
|
|
|
|
MachineConstantPool *MCP;
|
2012-03-25 23:49:58 +00:00
|
|
|
const ARMBaseInstrInfo *TII;
|
2009-08-01 06:13:52 +00:00
|
|
|
const ARMSubtarget *STI;
|
2007-04-29 19:19:30 +00:00
|
|
|
ARMFunctionInfo *AFI;
|
2007-02-28 23:20:38 +00:00
|
|
|
bool isThumb;
|
2009-07-23 18:27:47 +00:00
|
|
|
bool isThumb1;
|
2009-06-30 18:04:13 +00:00
|
|
|
bool isThumb2;
|
2007-01-19 07:51:42 +00:00
|
|
|
public:
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID;
|
2010-08-06 18:33:48 +00:00
|
|
|
ARMConstantIslands() : MachineFunctionPass(ID) {}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2014-03-10 02:09:33 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2007-01-19 07:51:42 +00:00
|
|
|
|
2014-03-10 02:09:33 +00:00
|
|
|
const char *getPassName() const override {
|
2007-01-25 03:12:46 +00:00
|
|
|
return "ARM constant island placement and branch shortening pass";
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
private:
|
2012-03-23 23:07:03 +00:00
|
|
|
void doInitialPlacement(std::vector<MachineInstr*> &CPEMIs);
|
2007-02-09 20:54:44 +00:00
|
|
|
CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI);
|
2011-12-12 18:45:45 +00:00
|
|
|
unsigned getCPELogAlign(const MachineInstr *CPEMI);
|
2012-03-23 23:07:03 +00:00
|
|
|
void scanFunctionJumpTables();
|
|
|
|
void initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs);
|
|
|
|
MachineBasicBlock *splitBlockBeforeInstr(MachineInstr *MI);
|
|
|
|
void updateForInsertedWaterBlock(MachineBasicBlock *NewBB);
|
|
|
|
void adjustBBOffsetsAfter(MachineBasicBlock *BB);
|
|
|
|
bool decrementCPEReferenceCount(unsigned CPI, MachineInstr* CPEMI);
|
|
|
|
int findInRangeCPEntry(CPUser& U, unsigned UserOffset);
|
|
|
|
bool findAvailableWater(CPUser&U, unsigned UserOffset,
|
|
|
|
water_iterator &WaterIter);
|
|
|
|
void createNewWater(unsigned CPUserIndex, unsigned UserOffset,
|
2009-10-12 21:39:43 +00:00
|
|
|
MachineBasicBlock *&NewMBB);
|
2012-03-23 23:07:03 +00:00
|
|
|
bool handleConstantPoolUser(unsigned CPUserIndex);
|
|
|
|
void removeDeadCPEMI(MachineInstr *CPEMI);
|
|
|
|
bool removeUnusedCPEntries();
|
|
|
|
bool isCPEntryInRange(MachineInstr *MI, unsigned UserOffset,
|
|
|
|
MachineInstr *CPEMI, unsigned Disp, bool NegOk,
|
|
|
|
bool DoDump = false);
|
|
|
|
bool isWaterInRange(unsigned UserOffset, MachineBasicBlock *Water,
|
2011-12-13 00:44:30 +00:00
|
|
|
CPUser &U, unsigned &Growth);
|
2012-03-23 23:07:03 +00:00
|
|
|
bool isBBInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp);
|
|
|
|
bool fixupImmediateBr(ImmBranch &Br);
|
|
|
|
bool fixupConditionalBr(ImmBranch &Br);
|
|
|
|
bool fixupUnconditionalBr(ImmBranch &Br);
|
|
|
|
bool undoLRSpillRestore();
|
2012-01-10 22:32:14 +00:00
|
|
|
bool mayOptimizeThumb2Instruction(const MachineInstr *MI) const;
|
2012-03-23 23:07:03 +00:00
|
|
|
bool optimizeThumb2Instructions();
|
|
|
|
bool optimizeThumb2Branches();
|
|
|
|
bool reorderThumb2JumpTables();
|
|
|
|
bool optimizeThumb2JumpTables();
|
|
|
|
MachineBasicBlock *adjustJTTargetBlockForward(MachineBasicBlock *BB,
|
2009-11-11 02:47:19 +00:00
|
|
|
MachineBasicBlock *JTBB);
|
2007-01-19 07:51:42 +00:00
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
void computeBlockSize(MachineBasicBlock *MBB);
|
|
|
|
unsigned getOffsetOf(MachineInstr *MI) const;
|
|
|
|
unsigned getUserOffset(CPUser&) const;
|
2007-04-29 19:19:30 +00:00
|
|
|
void dumpBBs();
|
2011-12-12 18:16:53 +00:00
|
|
|
void verify();
|
2011-12-09 19:44:39 +00:00
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset,
|
2011-12-09 19:44:39 +00:00
|
|
|
unsigned Disp, bool NegativeOK, bool IsSoImm = false);
|
2012-03-23 23:07:03 +00:00
|
|
|
bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset,
|
2011-12-09 19:44:39 +00:00
|
|
|
const CPUser &U) {
|
2012-03-23 23:07:03 +00:00
|
|
|
return isOffsetInRange(UserOffset, TrialOffset,
|
2012-01-10 01:34:59 +00:00
|
|
|
U.getMaxDisp(), U.NegOk, U.IsSoImm);
|
2011-12-09 19:44:39 +00:00
|
|
|
}
|
2007-01-19 07:51:42 +00:00
|
|
|
};
|
2007-05-03 01:11:54 +00:00
|
|
|
char ARMConstantIslands::ID = 0;
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
|
2007-04-29 19:19:30 +00:00
|
|
|
/// verify - check BBOffsets, BBSizes, alignment of islands
|
2011-12-12 18:16:53 +00:00
|
|
|
void ARMConstantIslands::verify() {
|
2009-07-23 18:27:47 +00:00
|
|
|
#ifndef NDEBUG
|
2011-12-12 18:16:53 +00:00
|
|
|
for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end();
|
2009-07-23 18:27:47 +00:00
|
|
|
MBBI != E; ++MBBI) {
|
|
|
|
MachineBasicBlock *MBB = MBBI;
|
2011-12-08 01:10:05 +00:00
|
|
|
unsigned MBBId = MBB->getNumber();
|
|
|
|
assert(!MBBId || BBInfo[MBBId - 1].postOffset() <= BBInfo[MBBId].Offset);
|
2007-04-29 19:19:30 +00:00
|
|
|
}
|
2012-03-31 00:06:42 +00:00
|
|
|
DEBUG(dbgs() << "Verifying " << CPUsers.size() << " CP users.\n");
|
2009-11-19 23:10:28 +00:00
|
|
|
for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) {
|
|
|
|
CPUser &U = CPUsers[i];
|
2012-03-23 23:07:03 +00:00
|
|
|
unsigned UserOffset = getUserOffset(U);
|
2012-03-31 00:06:44 +00:00
|
|
|
// Verify offset using the real max displacement without the safety
|
|
|
|
// adjustment.
|
|
|
|
if (isCPEntryInRange(U.MI, UserOffset, U.CPEMI, U.getMaxDisp()+2, U.NegOk,
|
2012-03-31 00:06:42 +00:00
|
|
|
/* DoDump = */ true)) {
|
|
|
|
DEBUG(dbgs() << "OK\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
DEBUG(dbgs() << "Out of range.\n");
|
|
|
|
dumpBBs();
|
|
|
|
DEBUG(MF->dump());
|
|
|
|
llvm_unreachable("Constant pool entry out of range!");
|
2009-11-19 23:10:28 +00:00
|
|
|
}
|
2009-11-20 19:37:38 +00:00
|
|
|
#endif
|
2007-04-29 19:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// print block size and offset information - debugging
|
|
|
|
void ARMConstantIslands::dumpBBs() {
|
2011-12-10 02:55:06 +00:00
|
|
|
DEBUG({
|
|
|
|
for (unsigned J = 0, E = BBInfo.size(); J !=E; ++J) {
|
|
|
|
const BasicBlockInfo &BBI = BBInfo[J];
|
|
|
|
dbgs() << format("%08x BB#%u\t", BBI.Offset, J)
|
|
|
|
<< " kb=" << unsigned(BBI.KnownBits)
|
|
|
|
<< " ua=" << unsigned(BBI.Unalign)
|
|
|
|
<< " pa=" << unsigned(BBI.PostAlign)
|
|
|
|
<< format(" size=%#x\n", BBInfo[J].Size);
|
|
|
|
}
|
|
|
|
});
|
2007-04-29 19:19:30 +00:00
|
|
|
}
|
|
|
|
|
2007-01-25 03:12:46 +00:00
|
|
|
/// createARMConstantIslandPass - returns an instance of the constpool
|
|
|
|
/// island pass.
|
2007-01-19 07:51:42 +00:00
|
|
|
FunctionPass *llvm::createARMConstantIslandPass() {
|
|
|
|
return new ARMConstantIslands();
|
|
|
|
}
|
|
|
|
|
2011-12-12 18:16:53 +00:00
|
|
|
bool ARMConstantIslands::runOnMachineFunction(MachineFunction &mf) {
|
|
|
|
MF = &mf;
|
|
|
|
MCP = mf.getConstantPool();
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2011-12-10 02:55:06 +00:00
|
|
|
DEBUG(dbgs() << "***** ARMConstantIslands: "
|
2011-12-12 18:16:53 +00:00
|
|
|
<< MCP->getConstants().size() << " CP entries, aligned to "
|
|
|
|
<< MCP->getConstantPoolAlignment() << " bytes *****\n");
|
2011-12-10 02:55:06 +00:00
|
|
|
|
2012-03-25 23:49:58 +00:00
|
|
|
TII = (const ARMBaseInstrInfo*)MF->getTarget().getInstrInfo();
|
2011-12-12 18:16:53 +00:00
|
|
|
AFI = MF->getInfo<ARMFunctionInfo>();
|
|
|
|
STI = &MF->getTarget().getSubtarget<ARMSubtarget>();
|
2009-08-01 06:13:52 +00:00
|
|
|
|
2007-02-28 23:20:38 +00:00
|
|
|
isThumb = AFI->isThumbFunction();
|
2009-07-23 18:27:47 +00:00
|
|
|
isThumb1 = AFI->isThumb1OnlyFunction();
|
2009-06-30 18:04:13 +00:00
|
|
|
isThumb2 = AFI->isThumb2Function();
|
2007-01-30 01:18:38 +00:00
|
|
|
|
|
|
|
HasFarJump = false;
|
|
|
|
|
2012-03-29 23:14:26 +00:00
|
|
|
// This pass invalidates liveness information when it splits basic blocks.
|
|
|
|
MF->getRegInfo().invalidateLiveness();
|
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Renumber all of the machine basic blocks in the function, guaranteeing that
|
|
|
|
// the numbers agree with the position of the block in the function.
|
2011-12-12 18:16:53 +00:00
|
|
|
MF->RenumberBlocks();
|
2007-01-19 07:51:42 +00:00
|
|
|
|
2009-11-12 17:25:07 +00:00
|
|
|
// Try to reorder and otherwise adjust the block layout to make good use
|
|
|
|
// of the TB[BH] instructions.
|
|
|
|
bool MadeChange = false;
|
|
|
|
if (isThumb2 && AdjustJumpTableBlocks) {
|
2012-03-23 23:07:03 +00:00
|
|
|
scanFunctionJumpTables();
|
|
|
|
MadeChange |= reorderThumb2JumpTables();
|
2009-11-12 17:25:07 +00:00
|
|
|
// Data is out of date, so clear it. It'll be re-computed later.
|
|
|
|
T2JumpTables.clear();
|
|
|
|
// Blocks may have shifted around. Keep the numbering up to date.
|
2011-12-12 18:16:53 +00:00
|
|
|
MF->RenumberBlocks();
|
2009-11-12 17:25:07 +00:00
|
|
|
}
|
|
|
|
|
2009-07-31 18:28:05 +00:00
|
|
|
// Thumb1 functions containing constant pools get 4-byte alignment.
|
2009-07-23 18:27:47 +00:00
|
|
|
// This is so we can keep exact track of where the alignment padding goes.
|
|
|
|
|
2010-01-27 23:37:36 +00:00
|
|
|
// ARM and Thumb2 functions need to be 4-byte aligned.
|
|
|
|
if (!isThumb1)
|
2012-07-06 23:13:38 +00:00
|
|
|
MF->ensureAlignment(2); // 2 = log2(4)
|
2007-04-23 20:09:04 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Perform the initial placement of the constant pool entries. To start with,
|
|
|
|
// we put them all at the end of the function.
|
2007-02-09 23:59:14 +00:00
|
|
|
std::vector<MachineInstr*> CPEMIs;
|
2011-12-12 18:45:45 +00:00
|
|
|
if (!MCP->isEmpty())
|
2012-03-23 23:07:03 +00:00
|
|
|
doInitialPlacement(CPEMIs);
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
/// The next UID to take is the first unused one.
|
2011-01-17 08:03:18 +00:00
|
|
|
AFI->initPICLabelUId(CPEMIs.size());
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Do the initial scan of the function, building up information about the
|
|
|
|
// sizes of each block, the location of all the water, and finding all of the
|
|
|
|
// constant pool users.
|
2012-03-23 23:07:03 +00:00
|
|
|
initializeFunctionInfo(CPEMIs);
|
2007-01-19 07:51:42 +00:00
|
|
|
CPEMIs.clear();
|
2010-07-23 22:50:23 +00:00
|
|
|
DEBUG(dumpBBs());
|
|
|
|
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-04-03 23:39:48 +00:00
|
|
|
/// Remove dead constant pool entries.
|
2012-03-23 23:07:03 +00:00
|
|
|
MadeChange |= removeUnusedCPEntries();
|
2007-04-03 23:39:48 +00:00
|
|
|
|
2007-01-30 01:18:38 +00:00
|
|
|
// Iteratively place constant pool entries and fix up branches until there
|
|
|
|
// is no change.
|
2009-08-07 07:35:21 +00:00
|
|
|
unsigned NoCPIters = 0, NoBRIters = 0;
|
2007-01-30 01:18:38 +00:00
|
|
|
while (true) {
|
2011-12-09 18:20:35 +00:00
|
|
|
DEBUG(dbgs() << "Beginning CP iteration #" << NoCPIters << '\n');
|
2009-08-07 07:35:21 +00:00
|
|
|
bool CPChange = false;
|
2007-01-19 07:51:42 +00:00
|
|
|
for (unsigned i = 0, e = CPUsers.size(); i != e; ++i)
|
2012-03-23 23:07:03 +00:00
|
|
|
CPChange |= handleConstantPoolUser(i);
|
2009-08-07 07:35:21 +00:00
|
|
|
if (CPChange && ++NoCPIters > 30)
|
2012-01-09 22:16:24 +00:00
|
|
|
report_fatal_error("Constant Island pass failed to converge!");
|
2007-07-10 22:00:16 +00:00
|
|
|
DEBUG(dumpBBs());
|
2010-07-07 21:06:51 +00:00
|
|
|
|
2009-10-15 20:49:47 +00:00
|
|
|
// Clear NewWaterList now. If we split a block for branches, it should
|
|
|
|
// appear as "new water" for the next iteration of constant pool placement.
|
|
|
|
NewWaterList.clear();
|
2009-08-07 07:35:21 +00:00
|
|
|
|
2011-12-09 18:20:35 +00:00
|
|
|
DEBUG(dbgs() << "Beginning BR iteration #" << NoBRIters << '\n');
|
2009-08-07 07:35:21 +00:00
|
|
|
bool BRChange = false;
|
2007-01-25 03:12:46 +00:00
|
|
|
for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
|
2012-03-23 23:07:03 +00:00
|
|
|
BRChange |= fixupImmediateBr(ImmBranches[i]);
|
2009-08-07 07:35:21 +00:00
|
|
|
if (BRChange && ++NoBRIters > 30)
|
2012-01-09 22:16:24 +00:00
|
|
|
report_fatal_error("Branch Fix Up pass failed to converge!");
|
2007-07-10 22:00:16 +00:00
|
|
|
DEBUG(dumpBBs());
|
2009-08-07 07:35:21 +00:00
|
|
|
|
|
|
|
if (!CPChange && !BRChange)
|
2007-01-30 01:18:38 +00:00
|
|
|
break;
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
2007-04-03 23:39:48 +00:00
|
|
|
|
2009-08-14 00:32:16 +00:00
|
|
|
// Shrink 32-bit Thumb2 branch, load, and store instructions.
|
2010-08-09 18:35:19 +00:00
|
|
|
if (isThumb2 && !STI->prefers32BitThumb())
|
2012-03-23 23:07:03 +00:00
|
|
|
MadeChange |= optimizeThumb2Instructions();
|
2009-08-01 06:13:52 +00:00
|
|
|
|
2007-04-29 19:19:30 +00:00
|
|
|
// After a while, this might be made debug-only, but it is not expensive.
|
2011-12-12 18:16:53 +00:00
|
|
|
verify();
|
2007-04-29 19:19:30 +00:00
|
|
|
|
2010-07-07 21:06:51 +00:00
|
|
|
// If LR has been forced spilled and no far jump (i.e. BL) has been issued,
|
|
|
|
// undo the spill / restore of LR if possible.
|
2009-07-29 02:18:14 +00:00
|
|
|
if (isThumb && !HasFarJump && AFI->isLRSpilledForFarJump())
|
2012-03-23 23:07:03 +00:00
|
|
|
MadeChange |= undoLRSpillRestore();
|
2007-01-30 01:18:38 +00:00
|
|
|
|
2011-01-30 22:07:39 +00:00
|
|
|
// Save the mapping between original and cloned constpool entries.
|
|
|
|
for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
|
|
|
|
for (unsigned j = 0, je = CPEntries[i].size(); j != je; ++j) {
|
|
|
|
const CPEntry & CPE = CPEntries[i][j];
|
|
|
|
AFI->recordCPEClone(i, CPE.CPI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 18:20:35 +00:00
|
|
|
DEBUG(dbgs() << '\n'; dumpBBs());
|
2010-07-22 02:09:47 +00:00
|
|
|
|
2011-12-07 01:08:25 +00:00
|
|
|
BBInfo.clear();
|
2007-01-19 07:51:42 +00:00
|
|
|
WaterList.clear();
|
|
|
|
CPUsers.clear();
|
2007-02-09 20:54:44 +00:00
|
|
|
CPEntries.clear();
|
2007-01-25 03:12:46 +00:00
|
|
|
ImmBranches.clear();
|
2007-02-09 20:54:44 +00:00
|
|
|
PushPopMIs.clear();
|
2009-07-29 02:18:14 +00:00
|
|
|
T2JumpTables.clear();
|
2007-01-30 01:18:38 +00:00
|
|
|
|
|
|
|
return MadeChange;
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// doInitialPlacement - Perform the initial placement of the constant pool
|
2007-01-19 07:51:42 +00:00
|
|
|
/// entries. To start with, we put them all at the end of the function.
|
2011-12-12 18:16:53 +00:00
|
|
|
void
|
2012-03-23 23:07:03 +00:00
|
|
|
ARMConstantIslands::doInitialPlacement(std::vector<MachineInstr*> &CPEMIs) {
|
2007-01-19 07:51:42 +00:00
|
|
|
// Create the basic block to hold the CPE's.
|
2011-12-12 18:16:53 +00:00
|
|
|
MachineBasicBlock *BB = MF->CreateMachineBasicBlock();
|
|
|
|
MF->push_back(BB);
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2011-12-12 16:49:37 +00:00
|
|
|
// MachineConstantPool measures alignment in bytes. We measure in log2(bytes).
|
2011-12-14 18:49:13 +00:00
|
|
|
unsigned MaxAlign = Log2_32(MCP->getConstantPoolAlignment());
|
2011-12-12 16:49:37 +00:00
|
|
|
|
|
|
|
// Mark the basic block as required by the const-pool.
|
|
|
|
// If AlignConstantIslands isn't set, use 4-byte alignment for everything.
|
|
|
|
BB->setAlignment(AlignConstantIslands ? MaxAlign : 2);
|
|
|
|
|
2011-12-12 18:45:45 +00:00
|
|
|
// The function needs to be as aligned as the basic blocks. The linker may
|
|
|
|
// move functions around based on their alignment.
|
2012-07-06 23:13:38 +00:00
|
|
|
MF->ensureAlignment(BB->getAlignment());
|
2011-12-12 18:45:45 +00:00
|
|
|
|
2011-12-12 16:49:37 +00:00
|
|
|
// Order the entries in BB by descending alignment. That ensures correct
|
|
|
|
// alignment of all entries as long as BB is sufficiently aligned. Keep
|
|
|
|
// track of the insertion point for each alignment. We are going to bucket
|
|
|
|
// sort the entries as they are created.
|
|
|
|
SmallVector<MachineBasicBlock::iterator, 8> InsPoint(MaxAlign + 1, BB->end());
|
2011-12-06 01:43:02 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Add all of the constants from the constant pool to the end block, use an
|
|
|
|
// identity mapping of CPI's to CPE's.
|
2011-12-14 18:49:13 +00:00
|
|
|
const std::vector<MachineConstantPoolEntry> &CPs = MCP->getConstants();
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2012-10-08 16:38:25 +00:00
|
|
|
const DataLayout &TD = *MF->getTarget().getDataLayout();
|
2007-01-19 07:51:42 +00:00
|
|
|
for (unsigned i = 0, e = CPs.size(); i != e; ++i) {
|
2009-05-09 07:06:46 +00:00
|
|
|
unsigned Size = TD.getTypeAllocSize(CPs[i].getType());
|
2011-12-12 16:49:37 +00:00
|
|
|
assert(Size >= 4 && "Too small constant pool entry");
|
|
|
|
unsigned Align = CPs[i].getAlignment();
|
|
|
|
assert(isPowerOf2_32(Align) && "Invalid alignment");
|
|
|
|
// Verify that all constant pool entries are a multiple of their alignment.
|
|
|
|
// If not, we would have to pad them out so that instructions stay aligned.
|
|
|
|
assert((Size % Align) == 0 && "CP Entry not multiple of 4 bytes!");
|
|
|
|
|
|
|
|
// Insert CONSTPOOL_ENTRY before entries with a smaller alignment.
|
|
|
|
unsigned LogAlign = Log2_32(Align);
|
|
|
|
MachineBasicBlock::iterator InsAt = InsPoint[LogAlign];
|
2007-01-19 07:51:42 +00:00
|
|
|
MachineInstr *CPEMI =
|
2011-12-12 16:49:37 +00:00
|
|
|
BuildMI(*BB, InsAt, DebugLoc(), TII->get(ARM::CONSTPOOL_ENTRY))
|
2010-04-02 20:16:16 +00:00
|
|
|
.addImm(i).addConstantPoolIndex(i).addImm(Size);
|
2007-01-19 07:51:42 +00:00
|
|
|
CPEMIs.push_back(CPEMI);
|
2007-02-09 20:54:44 +00:00
|
|
|
|
2011-12-12 16:49:37 +00:00
|
|
|
// Ensure that future entries with higher alignment get inserted before
|
|
|
|
// CPEMI. This is bucket sort with iterators.
|
2011-12-16 23:00:05 +00:00
|
|
|
for (unsigned a = LogAlign + 1; a <= MaxAlign; ++a)
|
2011-12-12 16:49:37 +00:00
|
|
|
if (InsPoint[a] == InsAt)
|
|
|
|
InsPoint[a] = CPEMI;
|
|
|
|
|
2007-02-09 20:54:44 +00:00
|
|
|
// Add a new CPEntry, but no corresponding CPUser yet.
|
|
|
|
std::vector<CPEntry> CPEs;
|
|
|
|
CPEs.push_back(CPEntry(CPEMI, i));
|
|
|
|
CPEntries.push_back(CPEs);
|
2010-06-22 15:08:57 +00:00
|
|
|
++NumCPEs;
|
2012-01-10 01:34:59 +00:00
|
|
|
DEBUG(dbgs() << "Moved CPI#" << i << " to end of function, size = "
|
|
|
|
<< Size << ", align = " << Align <<'\n');
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
2011-12-12 16:49:37 +00:00
|
|
|
DEBUG(BB->dump());
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
|
2007-02-23 05:02:36 +00:00
|
|
|
/// BBHasFallthrough - Return true if the specified basic block can fallthrough
|
2007-01-19 07:51:42 +00:00
|
|
|
/// into the block immediately after it.
|
|
|
|
static bool BBHasFallthrough(MachineBasicBlock *MBB) {
|
|
|
|
// Get the next machine basic block in the function.
|
|
|
|
MachineFunction::iterator MBBI = MBB;
|
2010-06-02 21:53:11 +00:00
|
|
|
// Can't fall off end of function.
|
2014-03-02 12:27:27 +00:00
|
|
|
if (std::next(MBBI) == MBB->getParent()->end())
|
2007-01-19 07:51:42 +00:00
|
|
|
return false;
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2014-03-02 12:27:27 +00:00
|
|
|
MachineBasicBlock *NextBB = std::next(MBBI);
|
2007-01-19 07:51:42 +00:00
|
|
|
for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
|
|
|
|
E = MBB->succ_end(); I != E; ++I)
|
|
|
|
if (*I == NextBB)
|
|
|
|
return true;
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-02-09 20:54:44 +00:00
|
|
|
/// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI,
|
|
|
|
/// look up the corresponding CPEntry.
|
|
|
|
ARMConstantIslands::CPEntry
|
|
|
|
*ARMConstantIslands::findConstPoolEntry(unsigned CPI,
|
|
|
|
const MachineInstr *CPEMI) {
|
|
|
|
std::vector<CPEntry> &CPEs = CPEntries[CPI];
|
|
|
|
// Number of entries per constpool index should be small, just do a
|
|
|
|
// linear search.
|
|
|
|
for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
|
|
|
|
if (CPEs[i].CPEMI == CPEMI)
|
|
|
|
return &CPEs[i];
|
|
|
|
}
|
2014-04-25 05:30:21 +00:00
|
|
|
return nullptr;
|
2007-02-09 20:54:44 +00:00
|
|
|
}
|
|
|
|
|
2011-12-12 18:45:45 +00:00
|
|
|
/// getCPELogAlign - Returns the required alignment of the constant pool entry
|
2011-12-12 19:25:51 +00:00
|
|
|
/// represented by CPEMI. Alignment is measured in log2(bytes) units.
|
2011-12-12 18:45:45 +00:00
|
|
|
unsigned ARMConstantIslands::getCPELogAlign(const MachineInstr *CPEMI) {
|
|
|
|
assert(CPEMI && CPEMI->getOpcode() == ARM::CONSTPOOL_ENTRY);
|
|
|
|
|
|
|
|
// Everything is 4-byte aligned unless AlignConstantIslands is set.
|
|
|
|
if (!AlignConstantIslands)
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
unsigned CPI = CPEMI->getOperand(1).getIndex();
|
|
|
|
assert(CPI < MCP->getConstants().size() && "Invalid constant pool index.");
|
|
|
|
unsigned Align = MCP->getConstants()[CPI].getAlignment();
|
|
|
|
assert(isPowerOf2_32(Align) && "Invalid CPE alignment");
|
|
|
|
return Log2_32(Align);
|
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// scanFunctionJumpTables - Do a scan of the function, building up
|
2009-11-12 17:25:07 +00:00
|
|
|
/// information about the sizes of each block and the locations of all
|
|
|
|
/// the jump tables.
|
2012-03-23 23:07:03 +00:00
|
|
|
void ARMConstantIslands::scanFunctionJumpTables() {
|
2011-12-12 18:16:53 +00:00
|
|
|
for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end();
|
2009-11-12 17:25:07 +00:00
|
|
|
MBBI != E; ++MBBI) {
|
|
|
|
MachineBasicBlock &MBB = *MBBI;
|
|
|
|
|
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
|
2009-11-16 18:58:52 +00:00
|
|
|
I != E; ++I)
|
2011-12-07 07:15:52 +00:00
|
|
|
if (I->isBranch() && I->getOpcode() == ARM::t2BR_JT)
|
2009-11-16 18:58:52 +00:00
|
|
|
T2JumpTables.push_back(I);
|
2009-11-12 17:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// initializeFunctionInfo - Do the initial scan of the function, building up
|
2007-01-19 07:51:42 +00:00
|
|
|
/// information about the sizes of each block, the location of all the water,
|
|
|
|
/// and finding all of the constant pool users.
|
2011-12-12 18:16:53 +00:00
|
|
|
void ARMConstantIslands::
|
2012-03-23 23:07:03 +00:00
|
|
|
initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs) {
|
2011-12-07 04:17:35 +00:00
|
|
|
BBInfo.clear();
|
2011-12-12 18:16:53 +00:00
|
|
|
BBInfo.resize(MF->getNumBlockIDs());
|
2011-12-07 04:17:35 +00:00
|
|
|
|
2011-12-08 00:55:02 +00:00
|
|
|
// First thing, compute the size of all basic blocks, and see if the function
|
|
|
|
// has any inline assembly in it. If so, we have to be conservative about
|
|
|
|
// alignment assumptions, as we don't know for sure the size of any
|
|
|
|
// instructions in the inline assembly.
|
2011-12-12 18:16:53 +00:00
|
|
|
for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I)
|
2012-03-23 23:07:03 +00:00
|
|
|
computeBlockSize(I);
|
2011-12-08 00:55:02 +00:00
|
|
|
|
|
|
|
// The known bits of the entry block offset are determined by the function
|
|
|
|
// alignment.
|
2011-12-12 18:16:53 +00:00
|
|
|
BBInfo.front().KnownBits = MF->getAlignment();
|
2011-12-08 00:55:02 +00:00
|
|
|
|
|
|
|
// Compute block offsets and known bits.
|
2012-03-23 23:07:03 +00:00
|
|
|
adjustBBOffsetsAfter(MF->begin());
|
2011-12-08 00:55:02 +00:00
|
|
|
|
2010-12-21 01:54:40 +00:00
|
|
|
// Now go back through the instructions and build up our data structures.
|
2011-12-12 18:16:53 +00:00
|
|
|
for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end();
|
2007-01-19 07:51:42 +00:00
|
|
|
MBBI != E; ++MBBI) {
|
|
|
|
MachineBasicBlock &MBB = *MBBI;
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// If this block doesn't fall through into the next MBB, then this is
|
|
|
|
// 'water' that a constant pool island could be placed.
|
|
|
|
if (!BBHasFallthrough(&MBB))
|
|
|
|
WaterList.push_back(&MBB);
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
|
|
|
|
I != E; ++I) {
|
2010-06-21 17:49:23 +00:00
|
|
|
if (I->isDebugValue())
|
|
|
|
continue;
|
2011-12-07 04:17:35 +00:00
|
|
|
|
2007-01-25 03:12:46 +00:00
|
|
|
int Opc = I->getOpcode();
|
2011-12-07 07:15:52 +00:00
|
|
|
if (I->isBranch()) {
|
2007-01-25 03:12:46 +00:00
|
|
|
bool isCond = false;
|
|
|
|
unsigned Bits = 0;
|
|
|
|
unsigned Scale = 1;
|
|
|
|
int UOpc = Opc;
|
|
|
|
switch (Opc) {
|
2009-07-29 02:18:14 +00:00
|
|
|
default:
|
|
|
|
continue; // Ignore other JT branches
|
|
|
|
case ARM::t2BR_JT:
|
|
|
|
T2JumpTables.push_back(I);
|
|
|
|
continue; // Does not get an entry in ImmBranches
|
2007-01-25 03:12:46 +00:00
|
|
|
case ARM::Bcc:
|
|
|
|
isCond = true;
|
|
|
|
UOpc = ARM::B;
|
|
|
|
// Fallthrough
|
|
|
|
case ARM::B:
|
|
|
|
Bits = 24;
|
|
|
|
Scale = 4;
|
|
|
|
break;
|
|
|
|
case ARM::tBcc:
|
|
|
|
isCond = true;
|
|
|
|
UOpc = ARM::tB;
|
|
|
|
Bits = 8;
|
|
|
|
Scale = 2;
|
|
|
|
break;
|
|
|
|
case ARM::tB:
|
|
|
|
Bits = 11;
|
|
|
|
Scale = 2;
|
|
|
|
break;
|
2009-06-30 18:04:13 +00:00
|
|
|
case ARM::t2Bcc:
|
|
|
|
isCond = true;
|
|
|
|
UOpc = ARM::t2B;
|
|
|
|
Bits = 20;
|
|
|
|
Scale = 2;
|
|
|
|
break;
|
|
|
|
case ARM::t2B:
|
|
|
|
Bits = 24;
|
|
|
|
Scale = 2;
|
|
|
|
break;
|
2007-01-25 03:12:46 +00:00
|
|
|
}
|
2007-02-01 10:16:15 +00:00
|
|
|
|
|
|
|
// Record this immediate branch.
|
2007-02-03 02:08:34 +00:00
|
|
|
unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
|
2007-02-01 10:16:15 +00:00
|
|
|
ImmBranches.push_back(ImmBranch(I, MaxOffs, isCond, UOpc));
|
2007-01-25 03:12:46 +00:00
|
|
|
}
|
|
|
|
|
2007-01-30 01:18:38 +00:00
|
|
|
if (Opc == ARM::tPUSH || Opc == ARM::tPOP_RET)
|
|
|
|
PushPopMIs.push_back(I);
|
|
|
|
|
2009-07-23 18:27:47 +00:00
|
|
|
if (Opc == ARM::CONSTPOOL_ENTRY)
|
|
|
|
continue;
|
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Scan the instructions for constant pool operands.
|
|
|
|
for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
|
2008-10-03 15:45:36 +00:00
|
|
|
if (I->getOperand(op).isCPI()) {
|
2007-01-19 07:51:42 +00:00
|
|
|
// We found one. The addressing mode tells us the max displacement
|
|
|
|
// from the PC that this instruction permits.
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Basic size info comes from the TSFlags field.
|
2007-02-01 10:16:15 +00:00
|
|
|
unsigned Bits = 0;
|
|
|
|
unsigned Scale = 1;
|
2009-07-21 23:56:01 +00:00
|
|
|
bool NegOk = false;
|
2009-07-23 18:27:47 +00:00
|
|
|
bool IsSoImm = false;
|
|
|
|
|
|
|
|
switch (Opc) {
|
2009-05-12 17:09:30 +00:00
|
|
|
default:
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Unknown addressing mode for CP reference!");
|
2009-07-23 18:27:47 +00:00
|
|
|
|
|
|
|
// Taking the address of a CP entry.
|
|
|
|
case ARM::LEApcrel:
|
|
|
|
// This takes a SoImm, which is 8 bit immediate rotated. We'll
|
|
|
|
// pretend the maximum offset is 255 * 4. Since each instruction
|
2009-11-19 18:23:19 +00:00
|
|
|
// 4 byte wide, this is always correct. We'll check for other
|
2009-07-23 18:27:47 +00:00
|
|
|
// displacements that fits in a SoImm as well.
|
|
|
|
Bits = 8;
|
|
|
|
Scale = 4;
|
2009-07-21 23:56:01 +00:00
|
|
|
NegOk = true;
|
2009-07-23 18:27:47 +00:00
|
|
|
IsSoImm = true;
|
2007-01-19 07:51:42 +00:00
|
|
|
break;
|
2010-12-13 22:51:08 +00:00
|
|
|
case ARM::t2LEApcrel:
|
2009-07-23 18:27:47 +00:00
|
|
|
Bits = 12;
|
2009-07-21 23:56:01 +00:00
|
|
|
NegOk = true;
|
2007-01-19 07:51:42 +00:00
|
|
|
break;
|
2009-07-23 18:27:47 +00:00
|
|
|
case ARM::tLEApcrel:
|
2007-02-01 10:16:15 +00:00
|
|
|
Bits = 8;
|
2009-07-23 18:27:47 +00:00
|
|
|
Scale = 4;
|
2007-01-19 07:51:42 +00:00
|
|
|
break;
|
2009-07-23 18:27:47 +00:00
|
|
|
|
2013-06-04 17:46:15 +00:00
|
|
|
case ARM::LDRBi12:
|
2010-10-26 22:37:02 +00:00
|
|
|
case ARM::LDRi12:
|
2009-07-23 18:27:47 +00:00
|
|
|
case ARM::LDRcp:
|
2011-02-08 22:39:40 +00:00
|
|
|
case ARM::t2LDRpci:
|
2009-07-23 18:27:47 +00:00
|
|
|
Bits = 12; // +-offset_12
|
|
|
|
NegOk = true;
|
2007-01-19 07:51:42 +00:00
|
|
|
break;
|
2009-07-23 18:27:47 +00:00
|
|
|
|
|
|
|
case ARM::tLDRpci:
|
2007-02-01 10:16:15 +00:00
|
|
|
Bits = 8;
|
|
|
|
Scale = 4; // +(offset_8*4)
|
2007-01-24 08:53:17 +00:00
|
|
|
break;
|
2009-07-23 18:27:47 +00:00
|
|
|
|
2009-11-09 00:11:35 +00:00
|
|
|
case ARM::VLDRD:
|
|
|
|
case ARM::VLDRS:
|
2009-07-23 18:27:47 +00:00
|
|
|
Bits = 8;
|
|
|
|
Scale = 4; // +-(offset_8*4)
|
|
|
|
NegOk = true;
|
2009-06-29 07:51:04 +00:00
|
|
|
break;
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
2007-02-01 10:16:15 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Remember that this is a user of a CP entry.
|
2007-12-30 23:10:15 +00:00
|
|
|
unsigned CPI = I->getOperand(op).getIndex();
|
2007-02-09 20:54:44 +00:00
|
|
|
MachineInstr *CPEMI = CPEMIs[CPI];
|
2009-08-14 18:31:44 +00:00
|
|
|
unsigned MaxOffs = ((1 << Bits)-1) * Scale;
|
2009-07-23 18:27:47 +00:00
|
|
|
CPUsers.push_back(CPUser(I, CPEMI, MaxOffs, NegOk, IsSoImm));
|
2007-02-09 20:54:44 +00:00
|
|
|
|
|
|
|
// Increment corresponding CPEntry reference count.
|
|
|
|
CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
|
|
|
|
assert(CPE && "Cannot find a corresponding CPEntry!");
|
|
|
|
CPE->RefCount++;
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Instructions can only use one CP entry, don't bother scanning the
|
|
|
|
// rest of the operands.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// computeBlockSize - Compute the size and some alignment information for MBB.
|
2011-12-07 04:17:35 +00:00
|
|
|
/// This function updates BBInfo directly.
|
2012-03-23 23:07:03 +00:00
|
|
|
void ARMConstantIslands::computeBlockSize(MachineBasicBlock *MBB) {
|
2011-12-07 04:17:35 +00:00
|
|
|
BasicBlockInfo &BBI = BBInfo[MBB->getNumber()];
|
|
|
|
BBI.Size = 0;
|
|
|
|
BBI.Unalign = 0;
|
|
|
|
BBI.PostAlign = 0;
|
|
|
|
|
2011-12-08 00:55:02 +00:00
|
|
|
for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
|
|
|
|
++I) {
|
2011-12-07 04:17:35 +00:00
|
|
|
BBI.Size += TII->GetInstSizeInBytes(I);
|
|
|
|
// For inline asm, GetInstSizeInBytes returns a conservative estimate.
|
|
|
|
// The actual size may be smaller, but still a multiple of the instr size.
|
2011-12-08 01:22:39 +00:00
|
|
|
if (I->isInlineAsm())
|
2011-12-07 04:17:35 +00:00
|
|
|
BBI.Unalign = isThumb ? 1 : 2;
|
2012-01-10 22:32:14 +00:00
|
|
|
// Also consider instructions that may be shrunk later.
|
|
|
|
else if (isThumb && mayOptimizeThumb2Instruction(I))
|
|
|
|
BBI.Unalign = 1;
|
2011-12-07 04:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// tBR_JTr contains a .align 2 directive.
|
2011-12-08 00:55:02 +00:00
|
|
|
if (!MBB->empty() && MBB->back().getOpcode() == ARM::tBR_JTr) {
|
2011-12-07 04:17:35 +00:00
|
|
|
BBI.PostAlign = 2;
|
2012-07-06 23:13:38 +00:00
|
|
|
MBB->getParent()->ensureAlignment(2);
|
2011-12-08 00:55:02 +00:00
|
|
|
}
|
2011-12-07 04:17:35 +00:00
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// getOffsetOf - Return the current offset of the specified machine instruction
|
2007-01-19 07:51:42 +00:00
|
|
|
/// from the start of the function. This offset changes as stuff is moved
|
|
|
|
/// around inside the function.
|
2012-03-23 23:07:03 +00:00
|
|
|
unsigned ARMConstantIslands::getOffsetOf(MachineInstr *MI) const {
|
2007-01-19 07:51:42 +00:00
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// The offset is composed of two things: the sum of the sizes of all MBB's
|
|
|
|
// before this instruction's block, and the offset from the start of the block
|
|
|
|
// it is in.
|
2011-12-07 01:08:25 +00:00
|
|
|
unsigned Offset = BBInfo[MBB->getNumber()].Offset;
|
2007-01-19 07:51:42 +00:00
|
|
|
|
|
|
|
// Sum instructions before MI in MBB.
|
2012-01-31 20:56:55 +00:00
|
|
|
for (MachineBasicBlock::iterator I = MBB->begin(); &*I != MI; ++I) {
|
2007-01-19 07:51:42 +00:00
|
|
|
assert(I != MBB->end() && "Didn't find MI in its own basic block?");
|
2008-04-16 20:10:13 +00:00
|
|
|
Offset += TII->GetInstSizeInBytes(I);
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
2012-01-31 20:56:55 +00:00
|
|
|
return Offset;
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB
|
|
|
|
/// ID.
|
|
|
|
static bool CompareMBBNumbers(const MachineBasicBlock *LHS,
|
|
|
|
const MachineBasicBlock *RHS) {
|
|
|
|
return LHS->getNumber() < RHS->getNumber();
|
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// updateForInsertedWaterBlock - When a block is newly inserted into the
|
2007-01-19 07:51:42 +00:00
|
|
|
/// machine function, it upsets all of the block numbers. Renumber the blocks
|
|
|
|
/// and update the arrays that parallel this numbering.
|
2012-03-23 23:07:03 +00:00
|
|
|
void ARMConstantIslands::updateForInsertedWaterBlock(MachineBasicBlock *NewBB) {
|
2011-02-15 09:23:02 +00:00
|
|
|
// Renumber the MBB's to keep them consecutive.
|
2007-01-19 07:51:42 +00:00
|
|
|
NewBB->getParent()->RenumberBlocks(NewBB);
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2011-12-07 01:08:25 +00:00
|
|
|
// Insert an entry into BBInfo to align it properly with the (newly
|
2007-01-19 07:51:42 +00:00
|
|
|
// renumbered) block numbers.
|
2011-12-07 01:08:25 +00:00
|
|
|
BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
|
2009-05-12 17:09:30 +00:00
|
|
|
|
|
|
|
// Next, update WaterList. Specifically, we need to add NewMBB as having
|
2007-01-19 07:51:42 +00:00
|
|
|
// available water after it.
|
2009-10-12 18:52:13 +00:00
|
|
|
water_iterator IP =
|
2007-01-19 07:51:42 +00:00
|
|
|
std::lower_bound(WaterList.begin(), WaterList.end(), NewBB,
|
|
|
|
CompareMBBNumbers);
|
|
|
|
WaterList.insert(IP, NewBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Split the basic block containing MI into two blocks, which are joined by
|
2009-10-15 20:49:47 +00:00
|
|
|
/// an unconditional branch. Update data structures and renumber blocks to
|
2007-01-31 02:22:22 +00:00
|
|
|
/// account for this change and returns the newly created block.
|
2012-03-23 23:07:03 +00:00
|
|
|
MachineBasicBlock *ARMConstantIslands::splitBlockBeforeInstr(MachineInstr *MI) {
|
2007-01-19 07:51:42 +00:00
|
|
|
MachineBasicBlock *OrigBB = MI->getParent();
|
|
|
|
|
|
|
|
// Create a new MBB for the code after the OrigBB.
|
2009-05-12 17:09:30 +00:00
|
|
|
MachineBasicBlock *NewBB =
|
2011-12-12 18:16:53 +00:00
|
|
|
MF->CreateMachineBasicBlock(OrigBB->getBasicBlock());
|
2007-01-19 07:51:42 +00:00
|
|
|
MachineFunction::iterator MBBI = OrigBB; ++MBBI;
|
2011-12-12 18:16:53 +00:00
|
|
|
MF->insert(MBBI, NewBB);
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Splice the instructions starting with MI over to NewBB.
|
|
|
|
NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end());
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Add an unconditional branch from OrigBB to NewBB.
|
2007-01-31 18:29:27 +00:00
|
|
|
// Note the new unconditional branch is not being recorded.
|
2009-02-13 02:25:56 +00:00
|
|
|
// There doesn't seem to be meaningful DebugInfo available; this doesn't
|
|
|
|
// correspond to anything in the source.
|
2009-07-07 01:16:41 +00:00
|
|
|
unsigned Opc = isThumb ? (isThumb2 ? ARM::t2B : ARM::tB) : ARM::B;
|
2011-09-09 21:48:23 +00:00
|
|
|
if (!isThumb)
|
|
|
|
BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB);
|
|
|
|
else
|
|
|
|
BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB)
|
|
|
|
.addImm(ARMCC::AL).addReg(0);
|
2010-06-22 15:08:57 +00:00
|
|
|
++NumSplit;
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Update the CFG. All succs of OrigBB are now succs of NewBB.
|
2011-12-06 00:51:12 +00:00
|
|
|
NewBB->transferSuccessors(OrigBB);
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// OrigBB branches to NewBB.
|
|
|
|
OrigBB->addSuccessor(NewBB);
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Update internal data structures to account for the newly inserted MBB.
|
2012-03-23 23:07:03 +00:00
|
|
|
// This is almost the same as updateForInsertedWaterBlock, except that
|
2007-02-23 05:02:36 +00:00
|
|
|
// the Water goes after OrigBB, not NewBB.
|
2011-12-12 18:16:53 +00:00
|
|
|
MF->RenumberBlocks(NewBB);
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2011-12-07 01:08:25 +00:00
|
|
|
// Insert an entry into BBInfo to align it properly with the (newly
|
2007-02-23 05:02:36 +00:00
|
|
|
// renumbered) block numbers.
|
2011-12-07 01:08:25 +00:00
|
|
|
BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
|
2007-02-25 00:47:03 +00:00
|
|
|
|
2009-05-12 17:09:30 +00:00
|
|
|
// Next, update WaterList. Specifically, we need to add OrigMBB as having
|
2007-02-23 05:02:36 +00:00
|
|
|
// available water after it (but not if it's already there, which happens
|
|
|
|
// when splitting before a conditional branch that is followed by an
|
|
|
|
// unconditional branch - in that case we want to insert NewBB).
|
2009-10-12 18:52:13 +00:00
|
|
|
water_iterator IP =
|
2007-02-23 05:02:36 +00:00
|
|
|
std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB,
|
|
|
|
CompareMBBNumbers);
|
|
|
|
MachineBasicBlock* WaterBB = *IP;
|
|
|
|
if (WaterBB == OrigBB)
|
2014-03-02 12:27:27 +00:00
|
|
|
WaterList.insert(std::next(IP), NewBB);
|
2007-02-23 05:02:36 +00:00
|
|
|
else
|
|
|
|
WaterList.insert(IP, OrigBB);
|
2009-10-15 20:49:47 +00:00
|
|
|
NewWaterList.insert(OrigBB);
|
2007-02-23 05:02:36 +00:00
|
|
|
|
2010-07-23 22:50:23 +00:00
|
|
|
// Figure out how large the OrigBB is. As the first half of the original
|
|
|
|
// block, it cannot contain a tablejump. The size includes
|
|
|
|
// the new jump we added. (It should be possible to do this without
|
|
|
|
// recounting everything, but it's very confusing, and this is rarely
|
|
|
|
// executed.)
|
2012-03-23 23:07:03 +00:00
|
|
|
computeBlockSize(OrigBB);
|
2007-02-25 00:47:03 +00:00
|
|
|
|
2010-07-23 22:50:23 +00:00
|
|
|
// Figure out how large the NewMBB is. As the second half of the original
|
|
|
|
// block, it may contain a tablejump.
|
2012-03-23 23:07:03 +00:00
|
|
|
computeBlockSize(NewBB);
|
2010-07-23 22:50:23 +00:00
|
|
|
|
2007-02-25 00:47:03 +00:00
|
|
|
// All BBOffsets following these blocks must be modified.
|
2012-03-23 23:07:03 +00:00
|
|
|
adjustBBOffsetsAfter(OrigBB);
|
2007-01-31 02:22:22 +00:00
|
|
|
|
|
|
|
return NewBB;
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// getUserOffset - Compute the offset of U.MI as seen by the hardware
|
2012-01-10 01:34:59 +00:00
|
|
|
/// displacement computation. Update U.KnownAlignment to match its current
|
|
|
|
/// basic block location.
|
2012-03-23 23:07:03 +00:00
|
|
|
unsigned ARMConstantIslands::getUserOffset(CPUser &U) const {
|
|
|
|
unsigned UserOffset = getOffsetOf(U.MI);
|
2012-01-10 01:34:59 +00:00
|
|
|
const BasicBlockInfo &BBI = BBInfo[U.MI->getParent()->getNumber()];
|
|
|
|
unsigned KnownBits = BBI.internalKnownBits();
|
|
|
|
|
|
|
|
// The value read from PC is offset from the actual instruction address.
|
|
|
|
UserOffset += (isThumb ? 4 : 8);
|
|
|
|
|
|
|
|
// Because of inline assembly, we may not know the alignment (mod 4) of U.MI.
|
|
|
|
// Make sure U.getMaxDisp() returns a constrained range.
|
|
|
|
U.KnownAlignment = (KnownBits >= 2);
|
|
|
|
|
|
|
|
// On Thumb, offsets==2 mod 4 are rounded down by the hardware for
|
|
|
|
// purposes of the displacement computation; compensate for that here.
|
|
|
|
// For unknown alignments, getMaxDisp() constrains the range instead.
|
|
|
|
if (isThumb && U.KnownAlignment)
|
|
|
|
UserOffset &= ~3u;
|
|
|
|
|
|
|
|
return UserOffset;
|
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// isOffsetInRange - Checks whether UserOffset (the location of a constant pool
|
2009-05-12 17:09:30 +00:00
|
|
|
/// reference) is within MaxDisp of TrialOffset (a proposed location of a
|
2007-04-29 19:19:30 +00:00
|
|
|
/// constant pool entry).
|
2012-03-23 23:07:03 +00:00
|
|
|
/// UserOffset is computed by getUserOffset above to include PC adjustments. If
|
2012-01-10 01:34:59 +00:00
|
|
|
/// the mod 4 alignment of UserOffset is not known, the uncertainty must be
|
|
|
|
/// subtracted from MaxDisp instead. CPUser::getMaxDisp() does that.
|
2012-03-23 23:07:03 +00:00
|
|
|
bool ARMConstantIslands::isOffsetInRange(unsigned UserOffset,
|
2009-07-23 18:27:47 +00:00
|
|
|
unsigned TrialOffset, unsigned MaxDisp,
|
|
|
|
bool NegativeOK, bool IsSoImm) {
|
2007-02-25 00:47:03 +00:00
|
|
|
if (UserOffset <= TrialOffset) {
|
|
|
|
// User before the Trial.
|
2009-07-23 18:27:47 +00:00
|
|
|
if (TrialOffset - UserOffset <= MaxDisp)
|
|
|
|
return true;
|
2009-07-24 19:31:03 +00:00
|
|
|
// FIXME: Make use full range of soimm values.
|
2007-02-25 00:47:03 +00:00
|
|
|
} else if (NegativeOK) {
|
2009-07-23 18:27:47 +00:00
|
|
|
if (UserOffset - TrialOffset <= MaxDisp)
|
|
|
|
return true;
|
2009-07-24 19:31:03 +00:00
|
|
|
// FIXME: Make use full range of soimm values.
|
2007-02-25 00:47:03 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// isWaterInRange - Returns true if a CPE placed after the specified
|
2007-02-23 05:02:36 +00:00
|
|
|
/// Water (a basic block) will be in range for the specific MI.
|
2011-12-13 00:44:30 +00:00
|
|
|
///
|
|
|
|
/// Compute how much the function will grow by inserting a CPE after Water.
|
2012-03-23 23:07:03 +00:00
|
|
|
bool ARMConstantIslands::isWaterInRange(unsigned UserOffset,
|
2011-12-13 00:44:30 +00:00
|
|
|
MachineBasicBlock* Water, CPUser &U,
|
|
|
|
unsigned &Growth) {
|
|
|
|
unsigned CPELogAlign = getCPELogAlign(U.CPEMI);
|
|
|
|
unsigned CPEOffset = BBInfo[Water->getNumber()].postOffset(CPELogAlign);
|
|
|
|
unsigned NextBlockOffset, NextBlockAlignment;
|
|
|
|
MachineFunction::const_iterator NextBlock = Water;
|
|
|
|
if (++NextBlock == MF->end()) {
|
|
|
|
NextBlockOffset = BBInfo[Water->getNumber()].postOffset();
|
|
|
|
NextBlockAlignment = 0;
|
|
|
|
} else {
|
|
|
|
NextBlockOffset = BBInfo[NextBlock->getNumber()].Offset;
|
|
|
|
NextBlockAlignment = NextBlock->getAlignment();
|
|
|
|
}
|
|
|
|
unsigned Size = U.CPEMI->getOperand(2).getImm();
|
|
|
|
unsigned CPEEnd = CPEOffset + Size;
|
|
|
|
|
|
|
|
// The CPE may be able to hide in the alignment padding before the next
|
|
|
|
// block. It may also cause more padding to be required if it is more aligned
|
|
|
|
// that the next block.
|
|
|
|
if (CPEEnd > NextBlockOffset) {
|
|
|
|
Growth = CPEEnd - NextBlockOffset;
|
|
|
|
// Compute the padding that would go at the end of the CPE to align the next
|
|
|
|
// block.
|
|
|
|
Growth += OffsetToAlignment(CPEEnd, 1u << NextBlockAlignment);
|
|
|
|
|
|
|
|
// If the CPE is to be inserted before the instruction, that will raise
|
2012-03-23 23:07:03 +00:00
|
|
|
// the offset of the instruction. Also account for unknown alignment padding
|
2011-12-13 00:44:30 +00:00
|
|
|
// in blocks between CPE and the user.
|
|
|
|
if (CPEOffset < UserOffset)
|
|
|
|
UserOffset += Growth + UnknownPadding(MF->getAlignment(), CPELogAlign);
|
|
|
|
} else
|
|
|
|
// CPE fits in existing padding.
|
|
|
|
Growth = 0;
|
2007-04-02 20:31:06 +00:00
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
return isOffsetInRange(UserOffset, CPEOffset, U);
|
2007-02-23 05:02:36 +00:00
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// isCPEntryInRange - Returns true if the distance between specific MI and
|
2007-01-31 19:57:44 +00:00
|
|
|
/// specific ConstPool entry instruction can fit in MI's displacement field.
|
2012-03-23 23:07:03 +00:00
|
|
|
bool ARMConstantIslands::isCPEntryInRange(MachineInstr *MI, unsigned UserOffset,
|
2009-07-21 23:56:01 +00:00
|
|
|
MachineInstr *CPEMI, unsigned MaxDisp,
|
|
|
|
bool NegOk, bool DoDump) {
|
2012-03-23 23:07:03 +00:00
|
|
|
unsigned CPEOffset = getOffsetOf(CPEMI);
|
2007-02-01 01:09:47 +00:00
|
|
|
|
2007-02-23 05:02:36 +00:00
|
|
|
if (DoDump) {
|
2011-12-09 18:20:35 +00:00
|
|
|
DEBUG({
|
|
|
|
unsigned Block = MI->getParent()->getNumber();
|
|
|
|
const BasicBlockInfo &BBI = BBInfo[Block];
|
|
|
|
dbgs() << "User of CPE#" << CPEMI->getOperand(0).getImm()
|
|
|
|
<< " max delta=" << MaxDisp
|
2011-12-10 02:55:06 +00:00
|
|
|
<< format(" insn address=%#x", UserOffset)
|
2011-12-09 18:20:35 +00:00
|
|
|
<< " in BB#" << Block << ": "
|
2011-12-10 02:55:06 +00:00
|
|
|
<< format("%#x-%x\t", BBI.Offset, BBI.postOffset()) << *MI
|
|
|
|
<< format("CPE address=%#x offset=%+d: ", CPEOffset,
|
|
|
|
int(CPEOffset-UserOffset));
|
2011-12-09 18:20:35 +00:00
|
|
|
});
|
2007-02-23 05:02:36 +00:00
|
|
|
}
|
2007-01-19 07:51:42 +00:00
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
return isOffsetInRange(UserOffset, CPEOffset, MaxDisp, NegOk);
|
2007-01-31 19:57:44 +00:00
|
|
|
}
|
|
|
|
|
2009-01-28 00:53:34 +00:00
|
|
|
#ifndef NDEBUG
|
2007-02-09 20:54:44 +00:00
|
|
|
/// BBIsJumpedOver - Return true of the specified basic block's only predecessor
|
|
|
|
/// unconditionally branches to its only successor.
|
|
|
|
static bool BBIsJumpedOver(MachineBasicBlock *MBB) {
|
|
|
|
if (MBB->pred_size() != 1 || MBB->succ_size() != 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MachineBasicBlock *Succ = *MBB->succ_begin();
|
|
|
|
MachineBasicBlock *Pred = *MBB->pred_begin();
|
|
|
|
MachineInstr *PredMI = &Pred->back();
|
2009-06-30 18:04:13 +00:00
|
|
|
if (PredMI->getOpcode() == ARM::B || PredMI->getOpcode() == ARM::tB
|
|
|
|
|| PredMI->getOpcode() == ARM::t2B)
|
2007-02-09 20:54:44 +00:00
|
|
|
return PredMI->getOperand(0).getMBB() == Succ;
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-28 00:53:34 +00:00
|
|
|
#endif // NDEBUG
|
2007-02-09 20:54:44 +00:00
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
void ARMConstantIslands::adjustBBOffsetsAfter(MachineBasicBlock *BB) {
|
2012-01-06 21:40:15 +00:00
|
|
|
unsigned BBNum = BB->getNumber();
|
|
|
|
for(unsigned i = BBNum + 1, e = MF->getNumBlockIDs(); i < e; ++i) {
|
2011-12-08 00:55:02 +00:00
|
|
|
// Get the offset and known bits at the end of the layout predecessor.
|
2011-12-12 19:25:54 +00:00
|
|
|
// Include the alignment of the current block.
|
|
|
|
unsigned LogAlign = MF->getBlockNumbered(i)->getAlignment();
|
|
|
|
unsigned Offset = BBInfo[i - 1].postOffset(LogAlign);
|
|
|
|
unsigned KnownBits = BBInfo[i - 1].postKnownBits(LogAlign);
|
2011-12-08 00:55:02 +00:00
|
|
|
|
2012-01-06 21:40:15 +00:00
|
|
|
// This is where block i begins. Stop if the offset is already correct,
|
|
|
|
// and we have updated 2 blocks. This is the maximum number of blocks
|
|
|
|
// changed before calling this function.
|
|
|
|
if (i > BBNum + 2 &&
|
|
|
|
BBInfo[i].Offset == Offset &&
|
|
|
|
BBInfo[i].KnownBits == KnownBits)
|
|
|
|
break;
|
|
|
|
|
2011-12-08 00:55:02 +00:00
|
|
|
BBInfo[i].Offset = Offset;
|
|
|
|
BBInfo[i].KnownBits = KnownBits;
|
2007-04-29 19:19:30 +00:00
|
|
|
}
|
2007-02-25 00:47:03 +00:00
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// decrementCPEReferenceCount - find the constant pool entry with index CPI
|
2007-02-23 05:02:36 +00:00
|
|
|
/// and instruction CPEMI, and decrement its refcount. If the refcount
|
2009-05-12 17:09:30 +00:00
|
|
|
/// becomes 0 remove the entry and instruction. Returns true if we removed
|
2007-02-23 05:02:36 +00:00
|
|
|
/// the entry, false if we didn't.
|
2007-02-01 10:16:15 +00:00
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
bool ARMConstantIslands::decrementCPEReferenceCount(unsigned CPI,
|
|
|
|
MachineInstr *CPEMI) {
|
2007-02-09 20:54:44 +00:00
|
|
|
// Find the old entry. Eliminate it if it is no longer used.
|
2007-04-03 23:39:48 +00:00
|
|
|
CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
|
|
|
|
assert(CPE && "Unexpected!");
|
|
|
|
if (--CPE->RefCount == 0) {
|
2012-03-23 23:07:03 +00:00
|
|
|
removeDeadCPEMI(CPEMI);
|
2014-04-25 05:30:21 +00:00
|
|
|
CPE->CPEMI = nullptr;
|
2010-06-22 15:08:57 +00:00
|
|
|
--NumCPEs;
|
2007-02-23 05:02:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LookForCPEntryInRange - see if the currently referenced CPE is in range;
|
|
|
|
/// if not, see if an in-range clone of the CPE is in range, and if so,
|
|
|
|
/// change the data structures so the user references the clone. Returns:
|
|
|
|
/// 0 = no existing entry found
|
|
|
|
/// 1 = entry found, and there were no code insertions or deletions
|
|
|
|
/// 2 = entry found, and there were code insertions or deletions
|
2012-03-23 23:07:03 +00:00
|
|
|
int ARMConstantIslands::findInRangeCPEntry(CPUser& U, unsigned UserOffset)
|
2007-02-23 05:02:36 +00:00
|
|
|
{
|
|
|
|
MachineInstr *UserMI = U.MI;
|
|
|
|
MachineInstr *CPEMI = U.CPEMI;
|
|
|
|
|
|
|
|
// Check to see if the CPE is already in-range.
|
2012-03-23 23:07:03 +00:00
|
|
|
if (isCPEntryInRange(UserMI, UserOffset, CPEMI, U.getMaxDisp(), U.NegOk,
|
|
|
|
true)) {
|
2011-12-09 18:20:35 +00:00
|
|
|
DEBUG(dbgs() << "In range\n");
|
2007-02-23 05:02:36 +00:00
|
|
|
return 1;
|
2007-02-09 20:54:44 +00:00
|
|
|
}
|
|
|
|
|
2007-02-23 05:02:36 +00:00
|
|
|
// No. Look for previously created clones of the CPE that are in range.
|
2007-12-30 23:10:15 +00:00
|
|
|
unsigned CPI = CPEMI->getOperand(1).getIndex();
|
2007-02-23 05:02:36 +00:00
|
|
|
std::vector<CPEntry> &CPEs = CPEntries[CPI];
|
|
|
|
for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
|
|
|
|
// We already tried this one
|
|
|
|
if (CPEs[i].CPEMI == CPEMI)
|
|
|
|
continue;
|
|
|
|
// Removing CPEs can leave empty entries, skip
|
2014-04-25 05:30:21 +00:00
|
|
|
if (CPEs[i].CPEMI == nullptr)
|
2007-02-23 05:02:36 +00:00
|
|
|
continue;
|
2012-03-23 23:07:03 +00:00
|
|
|
if (isCPEntryInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.getMaxDisp(),
|
2012-01-10 01:34:59 +00:00
|
|
|
U.NegOk)) {
|
2011-12-09 18:20:35 +00:00
|
|
|
DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#"
|
2009-08-23 06:49:22 +00:00
|
|
|
<< CPEs[i].CPI << "\n");
|
2007-02-23 05:02:36 +00:00
|
|
|
// Point the CPUser node to the replacement
|
|
|
|
U.CPEMI = CPEs[i].CPEMI;
|
|
|
|
// Change the CPI in the instruction operand to refer to the clone.
|
|
|
|
for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j)
|
2008-10-03 15:45:36 +00:00
|
|
|
if (UserMI->getOperand(j).isCPI()) {
|
2007-12-30 23:10:15 +00:00
|
|
|
UserMI->getOperand(j).setIndex(CPEs[i].CPI);
|
2007-02-23 05:02:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Adjust the refcount of the clone...
|
|
|
|
CPEs[i].RefCount++;
|
|
|
|
// ...and the original. If we didn't remove the old entry, none of the
|
|
|
|
// addresses changed, so we don't need another pass.
|
2012-03-23 23:07:03 +00:00
|
|
|
return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1;
|
2007-02-23 05:02:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-28 18:41:23 +00:00
|
|
|
/// getUnconditionalBrDisp - Returns the maximum displacement that can fit in
|
|
|
|
/// the specific unconditional branch instruction.
|
|
|
|
static inline unsigned getUnconditionalBrDisp(int Opc) {
|
2009-06-30 18:04:13 +00:00
|
|
|
switch (Opc) {
|
|
|
|
case ARM::tB:
|
|
|
|
return ((1<<10)-1)*2;
|
|
|
|
case ARM::t2B:
|
|
|
|
return ((1<<23)-1)*2;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-08-11 15:33:49 +00:00
|
|
|
|
2009-06-30 18:04:13 +00:00
|
|
|
return ((1<<23)-1)*4;
|
2007-02-28 18:41:23 +00:00
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// findAvailableWater - Look for an existing entry in the WaterList in which
|
2007-02-28 23:20:38 +00:00
|
|
|
/// we can place the CPE referenced from U so it's within range of U's MI.
|
2009-10-15 20:49:47 +00:00
|
|
|
/// Returns true if found, false if not. If it returns true, WaterIter
|
Last week, ARMConstantIslandPass was failing to converge for the
MultiSource/Benchmarks/MiBench/automotive-susan test. The failure has
since been masked by an unrelated change (just randomly), so I don't have
a testcase for this now. Radar 7291928.
The situation where this happened is that a constant pool entry (CPE) was
placed at a lower address than the load that referenced it. There were in
fact 2 CPEs placed at adjacent addresses and referenced by 2 loads that were
close together in the code. The distance from the loads to the CPEs was
right at the limit of what they could handle, so that only one of the CPEs
could be placed within range. On every iteration, the first CPE was found
to be out of range, causing a new CPE to be inserted. The second CPE had
been in range but the newly inserted entry pushed it too far away. Thus the
second CPE was also replaced by a new entry, which in turn pushed the first
CPE out of range. Etc.
Judging from some comments in the code, the initial implementation of this
pass did not support CPEs placed _before_ their references. In the case
where the CPE is placed at a higher address, the key to making the algorithm
terminate is that new CPEs are only inserted at the end of a group of adjacent
CPEs. This is implemented by removing a basic block from the "WaterList"
once it has been used, and then adding the newly inserted CPE block to the
list so that the next insertion will come after it. This avoids the ping-pong
effect where CPEs are repeatedly moved to the beginning of a group of
adjacent CPEs. This does not work when going backwards, however, because the
entries at the end of an adjacent group of CPEs are closer than the CPEs
earlier in the group.
To make this pass terminate, we need to maintain a property that changes can
only happen in some sort of monotonic fashion. The fix used here is to require
that the CPE for a particular constant pool load can only move to lower
addresses. This is a very simple change to the code and should not cause
any significant degradation in the results.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@83902 91177308-0d34-0410-b5e6-96231b3b80d8
2009-10-12 21:23:15 +00:00
|
|
|
/// is set to the WaterList entry. For Thumb, prefer water that will not
|
|
|
|
/// introduce padding to water that will. To ensure that this pass
|
|
|
|
/// terminates, the CPE location for a particular CPUser is only allowed to
|
|
|
|
/// move to a lower address, so search backward from the end of the list and
|
|
|
|
/// prefer the first water that is in range.
|
2012-03-23 23:07:03 +00:00
|
|
|
bool ARMConstantIslands::findAvailableWater(CPUser &U, unsigned UserOffset,
|
2009-10-15 20:49:47 +00:00
|
|
|
water_iterator &WaterIter) {
|
2009-10-12 19:04:03 +00:00
|
|
|
if (WaterList.empty())
|
|
|
|
return false;
|
|
|
|
|
2011-12-13 00:44:30 +00:00
|
|
|
unsigned BestGrowth = ~0u;
|
2014-03-02 12:27:27 +00:00
|
|
|
for (water_iterator IP = std::prev(WaterList.end()), B = WaterList.begin();;
|
2011-12-13 00:44:30 +00:00
|
|
|
--IP) {
|
2009-10-12 19:04:03 +00:00
|
|
|
MachineBasicBlock* WaterBB = *IP;
|
2009-10-15 20:49:47 +00:00
|
|
|
// Check if water is in range and is either at a lower address than the
|
|
|
|
// current "high water mark" or a new water block that was created since
|
|
|
|
// the previous iteration by inserting an unconditional branch. In the
|
|
|
|
// latter case, we want to allow resetting the high water mark back to
|
|
|
|
// this new water since we haven't seen it before. Inserting branches
|
|
|
|
// should be relatively uncommon and when it does happen, we want to be
|
|
|
|
// sure to take advantage of it for all the CPEs near that block, so that
|
|
|
|
// we don't insert more branches than necessary.
|
2011-12-13 00:44:30 +00:00
|
|
|
unsigned Growth;
|
2012-03-23 23:07:03 +00:00
|
|
|
if (isWaterInRange(UserOffset, WaterBB, U, Growth) &&
|
2009-10-15 20:49:47 +00:00
|
|
|
(WaterBB->getNumber() < U.HighWaterMark->getNumber() ||
|
2011-12-13 00:44:30 +00:00
|
|
|
NewWaterList.count(WaterBB)) && Growth < BestGrowth) {
|
|
|
|
// This is the least amount of required padding seen so far.
|
|
|
|
BestGrowth = Growth;
|
|
|
|
WaterIter = IP;
|
|
|
|
DEBUG(dbgs() << "Found water after BB#" << WaterBB->getNumber()
|
|
|
|
<< " Growth=" << Growth << '\n');
|
|
|
|
|
|
|
|
// Keep looking unless it is perfect.
|
|
|
|
if (BestGrowth == 0)
|
2009-10-12 19:04:03 +00:00
|
|
|
return true;
|
2007-02-23 05:02:36 +00:00
|
|
|
}
|
2009-10-12 19:04:03 +00:00
|
|
|
if (IP == B)
|
|
|
|
break;
|
2007-02-23 05:02:36 +00:00
|
|
|
}
|
2011-12-13 00:44:30 +00:00
|
|
|
return BestGrowth != ~0u;
|
2007-02-28 23:20:38 +00:00
|
|
|
}
|
2007-02-23 05:02:36 +00:00
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// createNewWater - No existing WaterList entry will work for
|
2007-02-28 23:20:38 +00:00
|
|
|
/// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the
|
|
|
|
/// block is used if in range, and the conditional branch munged so control
|
|
|
|
/// flow is correct. Otherwise the block is split to create a hole with an
|
2009-10-12 21:39:43 +00:00
|
|
|
/// unconditional branch around it. In either case NewMBB is set to a
|
2007-02-28 23:20:38 +00:00
|
|
|
/// block following which the new island can be inserted (the WaterList
|
|
|
|
/// is not adjusted).
|
2012-03-23 23:07:03 +00:00
|
|
|
void ARMConstantIslands::createNewWater(unsigned CPUserIndex,
|
2009-10-12 21:39:43 +00:00
|
|
|
unsigned UserOffset,
|
|
|
|
MachineBasicBlock *&NewMBB) {
|
2007-02-28 23:20:38 +00:00
|
|
|
CPUser &U = CPUsers[CPUserIndex];
|
|
|
|
MachineInstr *UserMI = U.MI;
|
|
|
|
MachineInstr *CPEMI = U.CPEMI;
|
2011-12-14 23:48:54 +00:00
|
|
|
unsigned CPELogAlign = getCPELogAlign(CPEMI);
|
2007-02-28 23:20:38 +00:00
|
|
|
MachineBasicBlock *UserMBB = UserMI->getParent();
|
2011-12-10 02:55:10 +00:00
|
|
|
const BasicBlockInfo &UserBBI = BBInfo[UserMBB->getNumber()];
|
2007-02-28 23:20:38 +00:00
|
|
|
|
2009-10-15 05:10:36 +00:00
|
|
|
// If the block does not end in an unconditional branch already, and if the
|
|
|
|
// end of the block is within range, make new water there. (The addition
|
|
|
|
// below is for the unconditional branch we will be adding: 4 bytes on ARM +
|
2012-01-10 01:34:59 +00:00
|
|
|
// Thumb2, 2 on Thumb1.
|
2011-12-14 23:48:54 +00:00
|
|
|
if (BBHasFallthrough(UserMBB)) {
|
|
|
|
// Size of branch to insert.
|
|
|
|
unsigned Delta = isThumb1 ? 2 : 4;
|
|
|
|
// Compute the offset where the CPE will begin.
|
2012-04-27 22:58:38 +00:00
|
|
|
unsigned CPEOffset = UserBBI.postOffset(CPELogAlign) + Delta;
|
2011-12-14 23:48:54 +00:00
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
if (isOffsetInRange(UserOffset, CPEOffset, U)) {
|
2011-12-14 23:48:54 +00:00
|
|
|
DEBUG(dbgs() << "Split at end of BB#" << UserMBB->getNumber()
|
|
|
|
<< format(", expected CPE offset %#x\n", CPEOffset));
|
2014-03-02 12:27:27 +00:00
|
|
|
NewMBB = std::next(MachineFunction::iterator(UserMBB));
|
2011-12-14 23:48:54 +00:00
|
|
|
// Add an unconditional branch from UserMBB to fallthrough block. Record
|
|
|
|
// it for branch lengthening; this new branch will not get out of range,
|
|
|
|
// but if the preceding conditional branch is out of range, the targets
|
|
|
|
// will be exchanged, and the altered branch may be out of range, so the
|
|
|
|
// machinery has to know about it.
|
|
|
|
int UncondBr = isThumb ? ((isThumb2) ? ARM::t2B : ARM::tB) : ARM::B;
|
|
|
|
if (!isThumb)
|
|
|
|
BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB);
|
|
|
|
else
|
|
|
|
BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB)
|
|
|
|
.addImm(ARMCC::AL).addReg(0);
|
|
|
|
unsigned MaxDisp = getUnconditionalBrDisp(UncondBr);
|
|
|
|
ImmBranches.push_back(ImmBranch(&UserMBB->back(),
|
|
|
|
MaxDisp, false, UncondBr));
|
|
|
|
BBInfo[UserMBB->getNumber()].Size += Delta;
|
2012-03-23 23:07:03 +00:00
|
|
|
adjustBBOffsetsAfter(UserMBB);
|
2011-12-14 23:48:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-08-12 20:30:05 +00:00
|
|
|
|
2011-12-14 23:48:54 +00:00
|
|
|
// What a big block. Find a place within the block to split it. This is a
|
|
|
|
// little tricky on Thumb1 since instructions are 2 bytes and constant pool
|
|
|
|
// entries are 4 bytes: if instruction I references island CPE, and
|
|
|
|
// instruction I+1 references CPE', it will not work well to put CPE as far
|
|
|
|
// forward as possible, since then CPE' cannot immediately follow it (that
|
|
|
|
// location is 2 bytes farther away from I+1 than CPE was from I) and we'd
|
|
|
|
// need to create a new island. So, we make a first guess, then walk through
|
|
|
|
// the instructions between the one currently being looked at and the
|
|
|
|
// possible insertion point, and make sure any other instructions that
|
|
|
|
// reference CPEs will be able to use the same island area; if not, we back
|
|
|
|
// up the insertion point.
|
|
|
|
|
|
|
|
// Try to split the block so it's fully aligned. Compute the latest split
|
2012-04-27 22:58:38 +00:00
|
|
|
// point where we can add a 4-byte branch instruction, and then align to
|
|
|
|
// LogAlign which is the largest possible alignment in the function.
|
2011-12-14 23:48:54 +00:00
|
|
|
unsigned LogAlign = MF->getAlignment();
|
|
|
|
assert(LogAlign >= CPELogAlign && "Over-aligned constant pool entry");
|
|
|
|
unsigned KnownBits = UserBBI.internalKnownBits();
|
|
|
|
unsigned UPad = UnknownPadding(LogAlign, KnownBits);
|
2012-04-27 22:58:38 +00:00
|
|
|
unsigned BaseInsertOffset = UserOffset + U.getMaxDisp() - UPad;
|
2011-12-14 23:48:54 +00:00
|
|
|
DEBUG(dbgs() << format("Split in middle of big block before %#x",
|
|
|
|
BaseInsertOffset));
|
|
|
|
|
|
|
|
// The 4 in the following is for the unconditional branch we'll be inserting
|
|
|
|
// (allows for long branch on Thumb1). Alignment of the island is handled
|
2012-03-23 23:07:03 +00:00
|
|
|
// inside isOffsetInRange.
|
2011-12-14 23:48:54 +00:00
|
|
|
BaseInsertOffset -= 4;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << format(", adjusted to %#x", BaseInsertOffset)
|
|
|
|
<< " la=" << LogAlign
|
|
|
|
<< " kb=" << KnownBits
|
|
|
|
<< " up=" << UPad << '\n');
|
|
|
|
|
|
|
|
// This could point off the end of the block if we've already got constant
|
|
|
|
// pool entries following this block; only the last one is in the water list.
|
|
|
|
// Back past any possible branches (allow for a conditional and a maximally
|
|
|
|
// long unconditional).
|
2012-04-28 06:21:38 +00:00
|
|
|
if (BaseInsertOffset + 8 >= UserBBI.postOffset()) {
|
|
|
|
BaseInsertOffset = UserBBI.postOffset() - UPad - 8;
|
|
|
|
DEBUG(dbgs() << format("Move inside block: %#x\n", BaseInsertOffset));
|
|
|
|
}
|
2012-04-27 22:58:38 +00:00
|
|
|
unsigned EndInsertOffset = BaseInsertOffset + 4 + UPad +
|
2011-12-14 23:48:54 +00:00
|
|
|
CPEMI->getOperand(2).getImm();
|
|
|
|
MachineBasicBlock::iterator MI = UserMI;
|
|
|
|
++MI;
|
|
|
|
unsigned CPUIndex = CPUserIndex+1;
|
|
|
|
unsigned NumCPUsers = CPUsers.size();
|
2014-04-25 05:30:21 +00:00
|
|
|
MachineInstr *LastIT = nullptr;
|
2011-12-14 23:48:54 +00:00
|
|
|
for (unsigned Offset = UserOffset+TII->GetInstSizeInBytes(UserMI);
|
|
|
|
Offset < BaseInsertOffset;
|
2014-03-02 12:27:27 +00:00
|
|
|
Offset += TII->GetInstSizeInBytes(MI), MI = std::next(MI)) {
|
2012-04-28 06:21:38 +00:00
|
|
|
assert(MI != UserMBB->end() && "Fell off end of block");
|
2011-12-14 23:48:54 +00:00
|
|
|
if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].MI == MI) {
|
|
|
|
CPUser &U = CPUsers[CPUIndex];
|
2012-03-23 23:07:03 +00:00
|
|
|
if (!isOffsetInRange(Offset, EndInsertOffset, U)) {
|
2011-12-14 23:48:54 +00:00
|
|
|
// Shift intertion point by one unit of alignment so it is within reach.
|
|
|
|
BaseInsertOffset -= 1u << LogAlign;
|
|
|
|
EndInsertOffset -= 1u << LogAlign;
|
|
|
|
}
|
|
|
|
// This is overly conservative, as we don't account for CPEMIs being
|
|
|
|
// reused within the block, but it doesn't matter much. Also assume CPEs
|
|
|
|
// are added in order with alignment padding. We may eventually be able
|
|
|
|
// to pack the aligned CPEs better.
|
2012-04-27 22:58:38 +00:00
|
|
|
EndInsertOffset += U.CPEMI->getOperand(2).getImm();
|
2011-12-14 23:48:54 +00:00
|
|
|
CPUIndex++;
|
2007-02-23 05:02:36 +00:00
|
|
|
}
|
2010-08-12 20:30:05 +00:00
|
|
|
|
2011-12-14 23:48:54 +00:00
|
|
|
// Remember the last IT instruction.
|
|
|
|
if (MI->getOpcode() == ARM::t2IT)
|
|
|
|
LastIT = MI;
|
|
|
|
}
|
2010-08-12 20:30:05 +00:00
|
|
|
|
2011-12-14 23:48:54 +00:00
|
|
|
--MI;
|
|
|
|
|
|
|
|
// Avoid splitting an IT block.
|
|
|
|
if (LastIT) {
|
|
|
|
unsigned PredReg = 0;
|
2012-03-27 07:21:54 +00:00
|
|
|
ARMCC::CondCodes CC = getITInstrPredicate(MI, PredReg);
|
2011-12-14 23:48:54 +00:00
|
|
|
if (CC != ARMCC::AL)
|
|
|
|
MI = LastIT;
|
2007-02-28 23:20:38 +00:00
|
|
|
}
|
2012-03-23 23:07:03 +00:00
|
|
|
NewMBB = splitBlockBeforeInstr(MI);
|
2007-02-28 23:20:38 +00:00
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// handleConstantPoolUser - Analyze the specified user, checking to see if it
|
2009-05-12 17:35:29 +00:00
|
|
|
/// is out-of-range. If so, pick up the constant pool value and move it some
|
2007-02-28 23:20:38 +00:00
|
|
|
/// place in-range. Return true if we changed any addresses (thus must run
|
|
|
|
/// another pass of branch lengthening), false otherwise.
|
2012-03-23 23:07:03 +00:00
|
|
|
bool ARMConstantIslands::handleConstantPoolUser(unsigned CPUserIndex) {
|
2007-02-28 23:20:38 +00:00
|
|
|
CPUser &U = CPUsers[CPUserIndex];
|
|
|
|
MachineInstr *UserMI = U.MI;
|
|
|
|
MachineInstr *CPEMI = U.CPEMI;
|
2007-12-30 23:10:15 +00:00
|
|
|
unsigned CPI = CPEMI->getOperand(1).getIndex();
|
2007-02-28 23:20:38 +00:00
|
|
|
unsigned Size = CPEMI->getOperand(2).getImm();
|
2012-01-10 01:34:59 +00:00
|
|
|
// Compute this only once, it's expensive.
|
2012-03-23 23:07:03 +00:00
|
|
|
unsigned UserOffset = getUserOffset(U);
|
2007-04-27 08:14:15 +00:00
|
|
|
|
2007-02-28 23:20:38 +00:00
|
|
|
// See if the current entry is within range, or there is a clone of it
|
|
|
|
// in range.
|
2012-03-23 23:07:03 +00:00
|
|
|
int result = findInRangeCPEntry(U, UserOffset);
|
2007-02-28 23:20:38 +00:00
|
|
|
if (result==1) return false;
|
|
|
|
else if (result==2) return true;
|
|
|
|
|
|
|
|
// No existing clone of this CPE is within range.
|
|
|
|
// We will be generating a new clone. Get a UID for it.
|
2011-01-17 08:03:18 +00:00
|
|
|
unsigned ID = AFI->createPICLabelUId();
|
2007-02-28 23:20:38 +00:00
|
|
|
|
Last week, ARMConstantIslandPass was failing to converge for the
MultiSource/Benchmarks/MiBench/automotive-susan test. The failure has
since been masked by an unrelated change (just randomly), so I don't have
a testcase for this now. Radar 7291928.
The situation where this happened is that a constant pool entry (CPE) was
placed at a lower address than the load that referenced it. There were in
fact 2 CPEs placed at adjacent addresses and referenced by 2 loads that were
close together in the code. The distance from the loads to the CPEs was
right at the limit of what they could handle, so that only one of the CPEs
could be placed within range. On every iteration, the first CPE was found
to be out of range, causing a new CPE to be inserted. The second CPE had
been in range but the newly inserted entry pushed it too far away. Thus the
second CPE was also replaced by a new entry, which in turn pushed the first
CPE out of range. Etc.
Judging from some comments in the code, the initial implementation of this
pass did not support CPEs placed _before_ their references. In the case
where the CPE is placed at a higher address, the key to making the algorithm
terminate is that new CPEs are only inserted at the end of a group of adjacent
CPEs. This is implemented by removing a basic block from the "WaterList"
once it has been used, and then adding the newly inserted CPE block to the
list so that the next insertion will come after it. This avoids the ping-pong
effect where CPEs are repeatedly moved to the beginning of a group of
adjacent CPEs. This does not work when going backwards, however, because the
entries at the end of an adjacent group of CPEs are closer than the CPEs
earlier in the group.
To make this pass terminate, we need to maintain a property that changes can
only happen in some sort of monotonic fashion. The fix used here is to require
that the CPE for a particular constant pool load can only move to lower
addresses. This is a very simple change to the code and should not cause
any significant degradation in the results.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@83902 91177308-0d34-0410-b5e6-96231b3b80d8
2009-10-12 21:23:15 +00:00
|
|
|
// Look for water where we can place this CPE.
|
2011-12-12 18:16:53 +00:00
|
|
|
MachineBasicBlock *NewIsland = MF->CreateMachineBasicBlock();
|
2009-10-15 20:49:47 +00:00
|
|
|
MachineBasicBlock *NewMBB;
|
|
|
|
water_iterator IP;
|
2012-03-23 23:07:03 +00:00
|
|
|
if (findAvailableWater(U, UserOffset, IP)) {
|
2011-12-09 18:20:35 +00:00
|
|
|
DEBUG(dbgs() << "Found water in range\n");
|
2009-10-15 20:49:47 +00:00
|
|
|
MachineBasicBlock *WaterBB = *IP;
|
|
|
|
|
|
|
|
// If the original WaterList entry was "new water" on this iteration,
|
|
|
|
// propagate that to the new island. This is just keeping NewWaterList
|
|
|
|
// updated to match the WaterList, which will be updated below.
|
2012-08-22 15:37:57 +00:00
|
|
|
if (NewWaterList.erase(WaterBB))
|
2009-10-15 20:49:47 +00:00
|
|
|
NewWaterList.insert(NewIsland);
|
2012-08-22 15:37:57 +00:00
|
|
|
|
2009-10-15 20:49:47 +00:00
|
|
|
// The new CPE goes before the following block (NewMBB).
|
2014-03-02 12:27:27 +00:00
|
|
|
NewMBB = std::next(MachineFunction::iterator(WaterBB));
|
2009-10-15 20:49:47 +00:00
|
|
|
|
|
|
|
} else {
|
2007-02-28 23:20:38 +00:00
|
|
|
// No water found.
|
2011-12-09 18:20:35 +00:00
|
|
|
DEBUG(dbgs() << "No water found\n");
|
2012-03-23 23:07:03 +00:00
|
|
|
createNewWater(CPUserIndex, UserOffset, NewMBB);
|
2009-10-15 20:49:47 +00:00
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
// splitBlockBeforeInstr adds to WaterList, which is important when it is
|
2009-10-15 20:49:47 +00:00
|
|
|
// called while handling branches so that the water will be seen on the
|
|
|
|
// next iteration for constant pools, but in this context, we don't want
|
|
|
|
// it. Check for this so it will be removed from the WaterList.
|
|
|
|
// Also remove any entry from NewWaterList.
|
2014-03-02 12:27:27 +00:00
|
|
|
MachineBasicBlock *WaterBB = std::prev(MachineFunction::iterator(NewMBB));
|
2009-10-15 20:49:47 +00:00
|
|
|
IP = std::find(WaterList.begin(), WaterList.end(), WaterBB);
|
|
|
|
if (IP != WaterList.end())
|
|
|
|
NewWaterList.erase(WaterBB);
|
|
|
|
|
|
|
|
// We are adding new water. Update NewWaterList.
|
|
|
|
NewWaterList.insert(NewIsland);
|
2007-02-23 05:02:36 +00:00
|
|
|
}
|
|
|
|
|
2009-10-15 20:49:47 +00:00
|
|
|
// Remove the original WaterList entry; we want subsequent insertions in
|
|
|
|
// this vicinity to go after the one we're about to insert. This
|
|
|
|
// considerably reduces the number of times we have to move the same CPE
|
|
|
|
// more than once and is also important to ensure the algorithm terminates.
|
|
|
|
if (IP != WaterList.end())
|
|
|
|
WaterList.erase(IP);
|
|
|
|
|
2007-02-23 05:02:36 +00:00
|
|
|
// Okay, we know we can put an island before NewMBB now, do it!
|
2011-12-12 18:16:53 +00:00
|
|
|
MF->insert(NewMBB, NewIsland);
|
2007-02-23 05:02:36 +00:00
|
|
|
|
|
|
|
// Update internal data structures to account for the newly inserted MBB.
|
2012-03-23 23:07:03 +00:00
|
|
|
updateForInsertedWaterBlock(NewIsland);
|
2007-02-23 05:02:36 +00:00
|
|
|
|
|
|
|
// Decrement the old entry, and remove it if refcount becomes 0.
|
2012-03-23 23:07:03 +00:00
|
|
|
decrementCPEReferenceCount(CPI, CPEMI);
|
2007-02-23 05:02:36 +00:00
|
|
|
|
|
|
|
// Now that we have an island to add the CPE to, clone the original CPE and
|
|
|
|
// add it to the island.
|
2009-10-15 05:52:29 +00:00
|
|
|
U.HighWaterMark = NewIsland;
|
2010-04-02 20:16:16 +00:00
|
|
|
U.CPEMI = BuildMI(NewIsland, DebugLoc(), TII->get(ARM::CONSTPOOL_ENTRY))
|
2007-01-19 07:51:42 +00:00
|
|
|
.addImm(ID).addConstantPoolIndex(CPI).addImm(Size);
|
2007-02-23 05:02:36 +00:00
|
|
|
CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1));
|
2010-06-22 15:08:57 +00:00
|
|
|
++NumCPEs;
|
2007-02-09 20:54:44 +00:00
|
|
|
|
2011-12-12 18:45:45 +00:00
|
|
|
// Mark the basic block as aligned as required by the const-pool entry.
|
|
|
|
NewIsland->setAlignment(getCPELogAlign(U.CPEMI));
|
2011-12-06 01:43:02 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Increase the size of the island block to account for the new entry.
|
2011-12-07 01:08:25 +00:00
|
|
|
BBInfo[NewIsland->getNumber()].Size += Size;
|
2014-03-02 12:27:27 +00:00
|
|
|
adjustBBOffsetsAfter(std::prev(MachineFunction::iterator(NewIsland)));
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Finally, change the CPI in the instruction operand to be ID.
|
|
|
|
for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i)
|
2008-10-03 15:45:36 +00:00
|
|
|
if (UserMI->getOperand(i).isCPI()) {
|
2007-12-30 23:10:15 +00:00
|
|
|
UserMI->getOperand(i).setIndex(ID);
|
2007-01-19 07:51:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2011-12-09 18:20:35 +00:00
|
|
|
DEBUG(dbgs() << " Moved CPE to #" << ID << " CPI=" << CPI
|
2011-12-10 02:55:06 +00:00
|
|
|
<< format(" offset=%#x\n", BBInfo[NewIsland->getNumber()].Offset));
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// removeDeadCPEMI - Remove a dead constant pool entry instruction. Update
|
2007-04-03 23:39:48 +00:00
|
|
|
/// sizes and offsets of impacted basic blocks.
|
2012-03-23 23:07:03 +00:00
|
|
|
void ARMConstantIslands::removeDeadCPEMI(MachineInstr *CPEMI) {
|
2007-04-03 23:39:48 +00:00
|
|
|
MachineBasicBlock *CPEBB = CPEMI->getParent();
|
2007-04-29 19:19:30 +00:00
|
|
|
unsigned Size = CPEMI->getOperand(2).getImm();
|
|
|
|
CPEMI->eraseFromParent();
|
2011-12-07 01:08:25 +00:00
|
|
|
BBInfo[CPEBB->getNumber()].Size -= Size;
|
2007-04-29 19:19:30 +00:00
|
|
|
// All succeeding offsets have the current size value added in, fix this.
|
2007-04-03 23:39:48 +00:00
|
|
|
if (CPEBB->empty()) {
|
2011-12-12 18:45:45 +00:00
|
|
|
BBInfo[CPEBB->getNumber()].Size = 0;
|
2011-12-06 21:55:35 +00:00
|
|
|
|
2013-02-21 18:37:54 +00:00
|
|
|
// This block no longer needs to be aligned.
|
2011-12-06 21:55:35 +00:00
|
|
|
CPEBB->setAlignment(0);
|
2011-12-12 18:45:45 +00:00
|
|
|
} else
|
|
|
|
// Entries are sorted by descending alignment, so realign from the front.
|
|
|
|
CPEBB->setAlignment(getCPELogAlign(CPEBB->begin()));
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
adjustBBOffsetsAfter(CPEBB);
|
2007-04-29 19:19:30 +00:00
|
|
|
// An island has only one predecessor BB and one successor BB. Check if
|
|
|
|
// this BB's predecessor jumps directly to this BB's successor. This
|
|
|
|
// shouldn't happen currently.
|
|
|
|
assert(!BBIsJumpedOver(CPEBB) && "How did this happen?");
|
|
|
|
// FIXME: remove the empty blocks after all the work is done?
|
2007-04-03 23:39:48 +00:00
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// removeUnusedCPEntries - Remove constant pool entries whose refcounts
|
2007-04-03 23:39:48 +00:00
|
|
|
/// are zero.
|
2012-03-23 23:07:03 +00:00
|
|
|
bool ARMConstantIslands::removeUnusedCPEntries() {
|
2007-04-03 23:39:48 +00:00
|
|
|
unsigned MadeChange = false;
|
|
|
|
for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
|
|
|
|
std::vector<CPEntry> &CPEs = CPEntries[i];
|
|
|
|
for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) {
|
|
|
|
if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) {
|
2012-03-23 23:07:03 +00:00
|
|
|
removeDeadCPEMI(CPEs[j].CPEMI);
|
2014-04-25 05:30:21 +00:00
|
|
|
CPEs[j].CPEMI = nullptr;
|
2007-04-03 23:39:48 +00:00
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
}
|
2009-05-12 17:09:30 +00:00
|
|
|
}
|
2007-04-03 23:39:48 +00:00
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// isBBInRange - Returns true if the distance between specific MI and
|
2007-01-26 20:38:26 +00:00
|
|
|
/// specific BB can fit in MI's displacement field.
|
2012-03-23 23:07:03 +00:00
|
|
|
bool ARMConstantIslands::isBBInRange(MachineInstr *MI,MachineBasicBlock *DestBB,
|
2007-01-31 19:57:44 +00:00
|
|
|
unsigned MaxDisp) {
|
2007-02-28 23:20:38 +00:00
|
|
|
unsigned PCAdj = isThumb ? 4 : 8;
|
2012-03-23 23:07:03 +00:00
|
|
|
unsigned BrOffset = getOffsetOf(MI) + PCAdj;
|
2011-12-07 01:08:25 +00:00
|
|
|
unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
|
2007-01-26 20:38:26 +00:00
|
|
|
|
2011-12-09 18:20:35 +00:00
|
|
|
DEBUG(dbgs() << "Branch of destination BB#" << DestBB->getNumber()
|
2009-08-23 03:41:05 +00:00
|
|
|
<< " from BB#" << MI->getParent()->getNumber()
|
|
|
|
<< " max delta=" << MaxDisp
|
2012-03-23 23:07:03 +00:00
|
|
|
<< " from " << getOffsetOf(MI) << " to " << DestOffset
|
2009-08-23 03:41:05 +00:00
|
|
|
<< " offset " << int(DestOffset-BrOffset) << "\t" << *MI);
|
2007-01-31 19:57:44 +00:00
|
|
|
|
2007-04-29 19:19:30 +00:00
|
|
|
if (BrOffset <= DestOffset) {
|
|
|
|
// Branch before the Dest.
|
|
|
|
if (DestOffset-BrOffset <= MaxDisp)
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (BrOffset-DestOffset <= MaxDisp)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2007-01-26 20:38:26 +00:00
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// fixupImmediateBr - Fix up an immediate branch whose destination is too far
|
2007-01-30 01:18:38 +00:00
|
|
|
/// away to fit in its displacement field.
|
2012-03-23 23:07:03 +00:00
|
|
|
bool ARMConstantIslands::fixupImmediateBr(ImmBranch &Br) {
|
2007-01-30 01:18:38 +00:00
|
|
|
MachineInstr *MI = Br.MI;
|
2007-12-30 23:10:15 +00:00
|
|
|
MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
|
2007-01-30 01:18:38 +00:00
|
|
|
|
2007-01-31 19:57:44 +00:00
|
|
|
// Check to see if the DestBB is already in-range.
|
2012-03-23 23:07:03 +00:00
|
|
|
if (isBBInRange(MI, DestBB, Br.MaxDisp))
|
2007-01-30 01:18:38 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!Br.isCond)
|
2012-03-23 23:07:03 +00:00
|
|
|
return fixupUnconditionalBr(Br);
|
|
|
|
return fixupConditionalBr(Br);
|
2007-01-30 01:18:38 +00:00
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// fixupUnconditionalBr - Fix up an unconditional branch whose destination is
|
2007-02-23 05:02:36 +00:00
|
|
|
/// too far away to fit in its displacement field. If the LR register has been
|
2007-01-30 01:18:38 +00:00
|
|
|
/// spilled in the epilogue, then we can use BL to implement a far jump.
|
2009-05-12 17:35:29 +00:00
|
|
|
/// Otherwise, add an intermediate branch instruction to a branch.
|
2007-01-30 01:18:38 +00:00
|
|
|
bool
|
2012-03-23 23:07:03 +00:00
|
|
|
ARMConstantIslands::fixupUnconditionalBr(ImmBranch &Br) {
|
2007-01-30 01:18:38 +00:00
|
|
|
MachineInstr *MI = Br.MI;
|
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
2009-08-07 05:45:07 +00:00
|
|
|
if (!isThumb1)
|
2012-03-23 23:07:03 +00:00
|
|
|
llvm_unreachable("fixupUnconditionalBr is Thumb1 only!");
|
2007-01-30 01:18:38 +00:00
|
|
|
|
|
|
|
// Use BL to implement far jump.
|
|
|
|
Br.MaxDisp = (1 << 21) * 2;
|
2008-01-11 18:10:50 +00:00
|
|
|
MI->setDesc(TII->get(ARM::tBfar));
|
2011-12-07 01:08:25 +00:00
|
|
|
BBInfo[MBB->getNumber()].Size += 2;
|
2012-03-23 23:07:03 +00:00
|
|
|
adjustBBOffsetsAfter(MBB);
|
2007-01-30 01:18:38 +00:00
|
|
|
HasFarJump = true;
|
2010-06-22 15:08:57 +00:00
|
|
|
++NumUBrFixed;
|
2007-02-03 02:08:34 +00:00
|
|
|
|
2011-12-09 18:20:35 +00:00
|
|
|
DEBUG(dbgs() << " Changed B to long jump " << *MI);
|
2007-02-03 02:08:34 +00:00
|
|
|
|
2007-01-30 01:18:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// fixupConditionalBr - Fix up a conditional branch whose destination is too
|
2007-01-30 01:18:38 +00:00
|
|
|
/// far away to fit in its displacement field. It is converted to an inverse
|
|
|
|
/// conditional branch + an unconditional branch to the destination.
|
2007-01-25 03:12:46 +00:00
|
|
|
bool
|
2012-03-23 23:07:03 +00:00
|
|
|
ARMConstantIslands::fixupConditionalBr(ImmBranch &Br) {
|
2007-01-25 03:12:46 +00:00
|
|
|
MachineInstr *MI = Br.MI;
|
2007-12-30 23:10:15 +00:00
|
|
|
MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
|
2007-01-25 03:12:46 +00:00
|
|
|
|
2009-05-12 17:35:29 +00:00
|
|
|
// Add an unconditional branch to the destination and invert the branch
|
2007-01-30 01:18:38 +00:00
|
|
|
// condition to jump over it:
|
2007-01-25 03:12:46 +00:00
|
|
|
// blt L1
|
|
|
|
// =>
|
|
|
|
// bge L2
|
|
|
|
// b L1
|
|
|
|
// L2:
|
2007-12-30 20:49:49 +00:00
|
|
|
ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImm();
|
2007-01-25 03:12:46 +00:00
|
|
|
CC = ARMCC::getOppositeCondition(CC);
|
2007-07-05 07:18:20 +00:00
|
|
|
unsigned CCReg = MI->getOperand(2).getReg();
|
2007-01-25 03:12:46 +00:00
|
|
|
|
|
|
|
// If the branch is at the end of its MBB and that has a fall-through block,
|
|
|
|
// direct the updated conditional branch to the fall-through block. Otherwise,
|
|
|
|
// split the MBB before the next instruction.
|
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
2007-02-03 02:08:34 +00:00
|
|
|
MachineInstr *BMI = &MBB->back();
|
|
|
|
bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB);
|
2007-01-26 20:38:26 +00:00
|
|
|
|
2010-06-22 15:08:57 +00:00
|
|
|
++NumCBrFixed;
|
2007-02-03 02:08:34 +00:00
|
|
|
if (BMI != MI) {
|
2014-03-02 12:27:27 +00:00
|
|
|
if (std::next(MachineBasicBlock::iterator(MI)) == std::prev(MBB->end()) &&
|
2007-02-03 02:08:34 +00:00
|
|
|
BMI->getOpcode() == Br.UncondBr) {
|
2009-05-12 17:35:29 +00:00
|
|
|
// Last MI in the BB is an unconditional branch. Can we simply invert the
|
2007-01-26 20:38:26 +00:00
|
|
|
// condition and swap destinations:
|
|
|
|
// beq L1
|
|
|
|
// b L2
|
|
|
|
// =>
|
|
|
|
// bne L2
|
|
|
|
// b L1
|
2007-12-30 23:10:15 +00:00
|
|
|
MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB();
|
2012-03-23 23:07:03 +00:00
|
|
|
if (isBBInRange(MI, NewDest, Br.MaxDisp)) {
|
2011-12-09 18:20:35 +00:00
|
|
|
DEBUG(dbgs() << " Invert Bcc condition and swap its destination with "
|
2009-08-23 03:41:05 +00:00
|
|
|
<< *BMI);
|
2007-12-30 23:10:15 +00:00
|
|
|
BMI->getOperand(0).setMBB(DestBB);
|
|
|
|
MI->getOperand(0).setMBB(NewDest);
|
2007-01-26 20:38:26 +00:00
|
|
|
MI->getOperand(1).setImm(CC);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NeedSplit) {
|
2012-03-23 23:07:03 +00:00
|
|
|
splitBlockBeforeInstr(MI);
|
2009-05-12 17:35:29 +00:00
|
|
|
// No need for the branch to the next block. We're adding an unconditional
|
2007-01-26 02:02:39 +00:00
|
|
|
// branch to the destination.
|
2008-04-16 20:10:13 +00:00
|
|
|
int delta = TII->GetInstSizeInBytes(&MBB->back());
|
2011-12-07 01:08:25 +00:00
|
|
|
BBInfo[MBB->getNumber()].Size -= delta;
|
2007-01-26 02:02:39 +00:00
|
|
|
MBB->back().eraseFromParent();
|
2011-12-07 01:08:25 +00:00
|
|
|
// BBInfo[SplitBB].Offset is wrong temporarily, fixed below
|
2007-01-26 02:02:39 +00:00
|
|
|
}
|
2014-03-02 12:27:27 +00:00
|
|
|
MachineBasicBlock *NextBB = std::next(MachineFunction::iterator(MBB));
|
2009-05-12 17:09:30 +00:00
|
|
|
|
2011-12-09 18:20:35 +00:00
|
|
|
DEBUG(dbgs() << " Insert B to BB#" << DestBB->getNumber()
|
2009-08-23 06:49:22 +00:00
|
|
|
<< " also invert condition and change dest. to BB#"
|
|
|
|
<< NextBB->getNumber() << "\n");
|
2007-01-25 03:12:46 +00:00
|
|
|
|
2007-04-23 20:09:04 +00:00
|
|
|
// Insert a new conditional branch and a new unconditional branch.
|
2007-01-25 03:12:46 +00:00
|
|
|
// Also update the ImmBranch as well as adding a new entry for the new branch.
|
2010-04-02 20:16:16 +00:00
|
|
|
BuildMI(MBB, DebugLoc(), TII->get(MI->getOpcode()))
|
2009-02-13 02:25:56 +00:00
|
|
|
.addMBB(NextBB).addImm(CC).addReg(CCReg);
|
2007-01-25 03:12:46 +00:00
|
|
|
Br.MI = &MBB->back();
|
2011-12-07 01:08:25 +00:00
|
|
|
BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back());
|
2011-09-09 23:05:14 +00:00
|
|
|
if (isThumb)
|
|
|
|
BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB)
|
|
|
|
.addImm(ARMCC::AL).addReg(0);
|
|
|
|
else
|
|
|
|
BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB);
|
2011-12-07 01:08:25 +00:00
|
|
|
BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back());
|
2007-01-31 18:29:27 +00:00
|
|
|
unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr);
|
2007-01-25 23:31:04 +00:00
|
|
|
ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr));
|
2007-04-23 20:09:04 +00:00
|
|
|
|
|
|
|
// Remove the old conditional branch. It may or may not still be in MBB.
|
2011-12-07 01:08:25 +00:00
|
|
|
BBInfo[MI->getParent()->getNumber()].Size -= TII->GetInstSizeInBytes(MI);
|
2007-01-25 03:12:46 +00:00
|
|
|
MI->eraseFromParent();
|
2012-03-23 23:07:03 +00:00
|
|
|
adjustBBOffsetsAfter(MBB);
|
2007-01-25 03:12:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-01-30 01:18:38 +00:00
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// undoLRSpillRestore - Remove Thumb push / pop instructions that only spills
|
2009-08-11 21:11:32 +00:00
|
|
|
/// LR / restores LR to pc. FIXME: This is done here because it's only possible
|
|
|
|
/// to do this if tBfar is not used.
|
2012-03-23 23:07:03 +00:00
|
|
|
bool ARMConstantIslands::undoLRSpillRestore() {
|
2007-01-30 01:18:38 +00:00
|
|
|
bool MadeChange = false;
|
|
|
|
for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) {
|
|
|
|
MachineInstr *MI = PushPopMIs[i];
|
2010-03-13 01:08:20 +00:00
|
|
|
// First two operands are predicates.
|
2007-05-15 01:29:07 +00:00
|
|
|
if (MI->getOpcode() == ARM::tPOP_RET &&
|
2010-03-13 01:08:20 +00:00
|
|
|
MI->getOperand(2).getReg() == ARM::PC &&
|
|
|
|
MI->getNumExplicitOperands() == 3) {
|
2011-07-08 21:50:04 +00:00
|
|
|
// Create the new insn and copy the predicate from the old.
|
|
|
|
BuildMI(MI->getParent(), MI->getDebugLoc(), TII->get(ARM::tBX_RET))
|
|
|
|
.addOperand(MI->getOperand(0))
|
|
|
|
.addOperand(MI->getOperand(1));
|
2007-05-15 01:29:07 +00:00
|
|
|
MI->eraseFromParent();
|
|
|
|
MadeChange = true;
|
2007-01-30 01:18:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return MadeChange;
|
|
|
|
}
|
2009-07-29 02:18:14 +00:00
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
// mayOptimizeThumb2Instruction - Returns true if optimizeThumb2Instructions
|
2012-01-10 22:32:14 +00:00
|
|
|
// below may shrink MI.
|
|
|
|
bool
|
|
|
|
ARMConstantIslands::mayOptimizeThumb2Instruction(const MachineInstr *MI) const {
|
|
|
|
switch(MI->getOpcode()) {
|
2012-03-23 23:07:03 +00:00
|
|
|
// optimizeThumb2Instructions.
|
2012-01-10 22:32:14 +00:00
|
|
|
case ARM::t2LEApcrel:
|
|
|
|
case ARM::t2LDRpci:
|
2012-03-23 23:07:03 +00:00
|
|
|
// optimizeThumb2Branches.
|
2012-01-10 22:32:14 +00:00
|
|
|
case ARM::t2B:
|
|
|
|
case ARM::t2Bcc:
|
|
|
|
case ARM::tBcc:
|
2012-03-23 23:07:03 +00:00
|
|
|
// optimizeThumb2JumpTables.
|
2012-01-10 22:32:14 +00:00
|
|
|
case ARM::t2BR_JT:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
bool ARMConstantIslands::optimizeThumb2Instructions() {
|
2009-08-14 00:32:16 +00:00
|
|
|
bool MadeChange = false;
|
|
|
|
|
|
|
|
// Shrink ADR and LDR from constantpool.
|
|
|
|
for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) {
|
|
|
|
CPUser &U = CPUsers[i];
|
|
|
|
unsigned Opcode = U.MI->getOpcode();
|
|
|
|
unsigned NewOpc = 0;
|
|
|
|
unsigned Scale = 1;
|
|
|
|
unsigned Bits = 0;
|
|
|
|
switch (Opcode) {
|
|
|
|
default: break;
|
2010-12-13 22:51:08 +00:00
|
|
|
case ARM::t2LEApcrel:
|
2009-08-14 00:32:16 +00:00
|
|
|
if (isARMLowRegister(U.MI->getOperand(0).getReg())) {
|
|
|
|
NewOpc = ARM::tLEApcrel;
|
|
|
|
Bits = 8;
|
|
|
|
Scale = 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ARM::t2LDRpci:
|
|
|
|
if (isARMLowRegister(U.MI->getOperand(0).getReg())) {
|
|
|
|
NewOpc = ARM::tLDRpci;
|
|
|
|
Bits = 8;
|
|
|
|
Scale = 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NewOpc)
|
|
|
|
continue;
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
unsigned UserOffset = getUserOffset(U);
|
2009-08-14 00:32:16 +00:00
|
|
|
unsigned MaxOffs = ((1 << Bits) - 1) * Scale;
|
2012-01-10 01:34:59 +00:00
|
|
|
|
|
|
|
// Be conservative with inline asm.
|
|
|
|
if (!U.KnownAlignment)
|
|
|
|
MaxOffs -= 2;
|
|
|
|
|
2009-08-14 00:32:16 +00:00
|
|
|
// FIXME: Check if offset is multiple of scale if scale is not 4.
|
2012-03-23 23:07:03 +00:00
|
|
|
if (isCPEntryInRange(U.MI, UserOffset, U.CPEMI, MaxOffs, false, true)) {
|
2012-03-31 00:06:42 +00:00
|
|
|
DEBUG(dbgs() << "Shrink: " << *U.MI);
|
2009-08-14 00:32:16 +00:00
|
|
|
U.MI->setDesc(TII->get(NewOpc));
|
|
|
|
MachineBasicBlock *MBB = U.MI->getParent();
|
2011-12-07 01:08:25 +00:00
|
|
|
BBInfo[MBB->getNumber()].Size -= 2;
|
2012-03-23 23:07:03 +00:00
|
|
|
adjustBBOffsetsAfter(MBB);
|
2009-08-14 00:32:16 +00:00
|
|
|
++NumT2CPShrunk;
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
MadeChange |= optimizeThumb2Branches();
|
|
|
|
MadeChange |= optimizeThumb2JumpTables();
|
2009-08-14 00:32:16 +00:00
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
bool ARMConstantIslands::optimizeThumb2Branches() {
|
2009-08-14 18:31:44 +00:00
|
|
|
bool MadeChange = false;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) {
|
|
|
|
ImmBranch &Br = ImmBranches[i];
|
|
|
|
unsigned Opcode = Br.MI->getOpcode();
|
|
|
|
unsigned NewOpc = 0;
|
|
|
|
unsigned Scale = 1;
|
|
|
|
unsigned Bits = 0;
|
|
|
|
switch (Opcode) {
|
|
|
|
default: break;
|
|
|
|
case ARM::t2B:
|
|
|
|
NewOpc = ARM::tB;
|
|
|
|
Bits = 11;
|
|
|
|
Scale = 2;
|
|
|
|
break;
|
2009-10-31 23:46:45 +00:00
|
|
|
case ARM::t2Bcc: {
|
2009-08-14 18:31:44 +00:00
|
|
|
NewOpc = ARM::tBcc;
|
|
|
|
Bits = 8;
|
2009-10-31 23:46:45 +00:00
|
|
|
Scale = 2;
|
2009-08-14 18:31:44 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-10-31 23:46:45 +00:00
|
|
|
}
|
|
|
|
if (NewOpc) {
|
|
|
|
unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
|
|
|
|
MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB();
|
2012-03-23 23:07:03 +00:00
|
|
|
if (isBBInRange(Br.MI, DestBB, MaxOffs)) {
|
2012-03-31 00:06:42 +00:00
|
|
|
DEBUG(dbgs() << "Shrink branch: " << *Br.MI);
|
2009-10-31 23:46:45 +00:00
|
|
|
Br.MI->setDesc(TII->get(NewOpc));
|
|
|
|
MachineBasicBlock *MBB = Br.MI->getParent();
|
2011-12-07 01:08:25 +00:00
|
|
|
BBInfo[MBB->getNumber()].Size -= 2;
|
2012-03-23 23:07:03 +00:00
|
|
|
adjustBBOffsetsAfter(MBB);
|
2009-10-31 23:46:45 +00:00
|
|
|
++NumT2BrShrunk;
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Opcode = Br.MI->getOpcode();
|
|
|
|
if (Opcode != ARM::tBcc)
|
2009-08-14 18:31:44 +00:00
|
|
|
continue;
|
|
|
|
|
2012-01-14 01:53:46 +00:00
|
|
|
// If the conditional branch doesn't kill CPSR, then CPSR can be liveout
|
|
|
|
// so this transformation is not safe.
|
|
|
|
if (!Br.MI->killsRegister(ARM::CPSR))
|
|
|
|
continue;
|
|
|
|
|
2009-10-31 23:46:45 +00:00
|
|
|
NewOpc = 0;
|
|
|
|
unsigned PredReg = 0;
|
2012-03-27 07:21:54 +00:00
|
|
|
ARMCC::CondCodes Pred = getInstrPredicate(Br.MI, PredReg);
|
2009-10-31 23:46:45 +00:00
|
|
|
if (Pred == ARMCC::EQ)
|
|
|
|
NewOpc = ARM::tCBZ;
|
|
|
|
else if (Pred == ARMCC::NE)
|
|
|
|
NewOpc = ARM::tCBNZ;
|
|
|
|
if (!NewOpc)
|
|
|
|
continue;
|
2009-08-14 18:31:44 +00:00
|
|
|
MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB();
|
2009-10-31 23:46:45 +00:00
|
|
|
// Check if the distance is within 126. Subtract starting offset by 2
|
|
|
|
// because the cmp will be eliminated.
|
2012-03-23 23:07:03 +00:00
|
|
|
unsigned BrOffset = getOffsetOf(Br.MI) + 4 - 2;
|
2011-12-07 01:08:25 +00:00
|
|
|
unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
|
2009-10-31 23:46:45 +00:00
|
|
|
if (BrOffset < DestOffset && (DestOffset - BrOffset) <= 126) {
|
2011-04-01 22:09:28 +00:00
|
|
|
MachineBasicBlock::iterator CmpMI = Br.MI;
|
|
|
|
if (CmpMI != Br.MI->getParent()->begin()) {
|
|
|
|
--CmpMI;
|
|
|
|
if (CmpMI->getOpcode() == ARM::tCMPi8) {
|
|
|
|
unsigned Reg = CmpMI->getOperand(0).getReg();
|
2012-03-27 07:21:54 +00:00
|
|
|
Pred = getInstrPredicate(CmpMI, PredReg);
|
2011-04-01 22:09:28 +00:00
|
|
|
if (Pred == ARMCC::AL &&
|
|
|
|
CmpMI->getOperand(1).getImm() == 0 &&
|
|
|
|
isARMLowRegister(Reg)) {
|
|
|
|
MachineBasicBlock *MBB = Br.MI->getParent();
|
2012-03-31 00:06:42 +00:00
|
|
|
DEBUG(dbgs() << "Fold: " << *CmpMI << " and: " << *Br.MI);
|
2011-04-01 22:09:28 +00:00
|
|
|
MachineInstr *NewBR =
|
|
|
|
BuildMI(*MBB, CmpMI, Br.MI->getDebugLoc(), TII->get(NewOpc))
|
|
|
|
.addReg(Reg).addMBB(DestBB,Br.MI->getOperand(0).getTargetFlags());
|
|
|
|
CmpMI->eraseFromParent();
|
|
|
|
Br.MI->eraseFromParent();
|
|
|
|
Br.MI = NewBR;
|
2011-12-07 01:08:25 +00:00
|
|
|
BBInfo[MBB->getNumber()].Size -= 2;
|
2012-03-23 23:07:03 +00:00
|
|
|
adjustBBOffsetsAfter(MBB);
|
2011-04-01 22:09:28 +00:00
|
|
|
++NumCBZ;
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
2009-10-31 23:46:45 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-14 18:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
2009-08-14 00:32:16 +00:00
|
|
|
}
|
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// optimizeThumb2JumpTables - Use tbb / tbh instructions to generate smaller
|
2009-08-14 00:32:16 +00:00
|
|
|
/// jumptables when it's possible.
|
2012-03-23 23:07:03 +00:00
|
|
|
bool ARMConstantIslands::optimizeThumb2JumpTables() {
|
2009-07-29 02:18:14 +00:00
|
|
|
bool MadeChange = false;
|
|
|
|
|
|
|
|
// FIXME: After the tables are shrunk, can we get rid some of the
|
|
|
|
// constantpool tables?
|
2011-12-12 18:16:53 +00:00
|
|
|
MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
|
2014-04-25 05:30:21 +00:00
|
|
|
if (!MJTI) return false;
|
2010-07-07 21:06:51 +00:00
|
|
|
|
2009-07-29 02:18:14 +00:00
|
|
|
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
|
|
|
|
for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) {
|
|
|
|
MachineInstr *MI = T2JumpTables[i];
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &MCID = MI->getDesc();
|
|
|
|
unsigned NumOps = MCID.getNumOperands();
|
2011-12-07 07:15:52 +00:00
|
|
|
unsigned JTOpIdx = NumOps - (MI->isPredicable() ? 3 : 2);
|
2009-07-29 02:18:14 +00:00
|
|
|
MachineOperand JTOP = MI->getOperand(JTOpIdx);
|
|
|
|
unsigned JTI = JTOP.getIndex();
|
|
|
|
assert(JTI < JT.size());
|
|
|
|
|
2009-11-11 02:47:19 +00:00
|
|
|
bool ByteOk = true;
|
|
|
|
bool HalfWordOk = true;
|
2012-03-23 23:07:03 +00:00
|
|
|
unsigned JTOffset = getOffsetOf(MI) + 4;
|
2009-11-12 17:25:07 +00:00
|
|
|
const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
|
2009-07-29 02:18:14 +00:00
|
|
|
for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) {
|
|
|
|
MachineBasicBlock *MBB = JTBBs[j];
|
2011-12-07 01:08:25 +00:00
|
|
|
unsigned DstOffset = BBInfo[MBB->getNumber()].Offset;
|
2009-07-29 23:20:20 +00:00
|
|
|
// Negative offset is not ok. FIXME: We should change BB layout to make
|
|
|
|
// sure all the branches are forward.
|
2009-07-31 18:28:05 +00:00
|
|
|
if (ByteOk && (DstOffset - JTOffset) > ((1<<8)-1)*2)
|
2009-07-29 02:18:14 +00:00
|
|
|
ByteOk = false;
|
2009-08-01 06:13:52 +00:00
|
|
|
unsigned TBHLimit = ((1<<16)-1)*2;
|
|
|
|
if (HalfWordOk && (DstOffset - JTOffset) > TBHLimit)
|
2009-07-29 02:18:14 +00:00
|
|
|
HalfWordOk = false;
|
|
|
|
if (!ByteOk && !HalfWordOk)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ByteOk || HalfWordOk) {
|
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
unsigned BaseReg = MI->getOperand(0).getReg();
|
|
|
|
bool BaseRegKill = MI->getOperand(0).isKill();
|
|
|
|
if (!BaseRegKill)
|
|
|
|
continue;
|
|
|
|
unsigned IdxReg = MI->getOperand(1).getReg();
|
|
|
|
bool IdxRegKill = MI->getOperand(1).isKill();
|
2010-07-07 22:51:22 +00:00
|
|
|
|
|
|
|
// Scan backwards to find the instruction that defines the base
|
|
|
|
// register. Due to post-RA scheduling, we can't count on it
|
|
|
|
// immediately preceding the branch instruction.
|
2009-07-29 02:18:14 +00:00
|
|
|
MachineBasicBlock::iterator PrevI = MI;
|
2010-07-07 22:51:22 +00:00
|
|
|
MachineBasicBlock::iterator B = MBB->begin();
|
|
|
|
while (PrevI != B && !PrevI->definesRegister(BaseReg))
|
|
|
|
--PrevI;
|
|
|
|
|
|
|
|
// If for some reason we didn't find it, we can't do anything, so
|
|
|
|
// just skip this one.
|
|
|
|
if (!PrevI->definesRegister(BaseReg))
|
2009-07-29 02:18:14 +00:00
|
|
|
continue;
|
|
|
|
|
2010-07-07 22:51:22 +00:00
|
|
|
MachineInstr *AddrMI = PrevI;
|
2009-07-29 02:18:14 +00:00
|
|
|
bool OptOk = true;
|
2010-07-07 21:06:51 +00:00
|
|
|
// Examine the instruction that calculates the jumptable entry address.
|
2010-07-07 22:51:22 +00:00
|
|
|
// Make sure it only defines the base register and kills any uses
|
|
|
|
// other than the index register.
|
2009-07-29 02:18:14 +00:00
|
|
|
for (unsigned k = 0, eee = AddrMI->getNumOperands(); k != eee; ++k) {
|
|
|
|
const MachineOperand &MO = AddrMI->getOperand(k);
|
|
|
|
if (!MO.isReg() || !MO.getReg())
|
|
|
|
continue;
|
|
|
|
if (MO.isDef() && MO.getReg() != BaseReg) {
|
|
|
|
OptOk = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (MO.isUse() && !MO.isKill() && MO.getReg() != IdxReg) {
|
|
|
|
OptOk = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!OptOk)
|
|
|
|
continue;
|
|
|
|
|
2010-12-13 22:51:08 +00:00
|
|
|
// Now scan back again to find the tLEApcrel or t2LEApcrelJT instruction
|
2010-07-07 22:51:22 +00:00
|
|
|
// that gave us the initial base register definition.
|
|
|
|
for (--PrevI; PrevI != B && !PrevI->definesRegister(BaseReg); --PrevI)
|
|
|
|
;
|
|
|
|
|
2010-12-13 22:51:08 +00:00
|
|
|
// The instruction should be a tLEApcrel or t2LEApcrelJT; we want
|
2009-08-14 00:32:16 +00:00
|
|
|
// to delete it as well.
|
2010-07-07 22:51:22 +00:00
|
|
|
MachineInstr *LeaMI = PrevI;
|
2009-08-14 00:32:16 +00:00
|
|
|
if ((LeaMI->getOpcode() != ARM::tLEApcrelJT &&
|
2010-12-13 22:51:08 +00:00
|
|
|
LeaMI->getOpcode() != ARM::t2LEApcrelJT) ||
|
2009-07-29 02:18:14 +00:00
|
|
|
LeaMI->getOperand(0).getReg() != BaseReg)
|
2009-08-01 06:13:52 +00:00
|
|
|
OptOk = false;
|
|
|
|
|
|
|
|
if (!OptOk)
|
|
|
|
continue;
|
|
|
|
|
2012-03-31 00:06:42 +00:00
|
|
|
DEBUG(dbgs() << "Shrink JT: " << *MI << " addr: " << *AddrMI
|
|
|
|
<< " lea: " << *LeaMI);
|
2010-11-29 21:28:32 +00:00
|
|
|
unsigned Opc = ByteOk ? ARM::t2TBB_JT : ARM::t2TBH_JT;
|
2009-08-01 06:13:52 +00:00
|
|
|
MachineInstr *NewJTMI = BuildMI(MBB, MI->getDebugLoc(), TII->get(Opc))
|
|
|
|
.addReg(IdxReg, getKillRegState(IdxRegKill))
|
|
|
|
.addJumpTableIndex(JTI, JTOP.getTargetFlags())
|
|
|
|
.addImm(MI->getOperand(JTOpIdx+1).getImm());
|
2012-03-31 00:06:42 +00:00
|
|
|
DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": " << *NewJTMI);
|
2009-08-01 06:13:52 +00:00
|
|
|
// FIXME: Insert an "ALIGN" instruction to ensure the next instruction
|
|
|
|
// is 2-byte aligned. For now, asm printer will fix it up.
|
|
|
|
unsigned NewSize = TII->GetInstSizeInBytes(NewJTMI);
|
|
|
|
unsigned OrigSize = TII->GetInstSizeInBytes(AddrMI);
|
|
|
|
OrigSize += TII->GetInstSizeInBytes(LeaMI);
|
|
|
|
OrigSize += TII->GetInstSizeInBytes(MI);
|
|
|
|
|
|
|
|
AddrMI->eraseFromParent();
|
|
|
|
LeaMI->eraseFromParent();
|
|
|
|
MI->eraseFromParent();
|
|
|
|
|
|
|
|
int delta = OrigSize - NewSize;
|
2011-12-07 01:08:25 +00:00
|
|
|
BBInfo[MBB->getNumber()].Size -= delta;
|
2012-03-23 23:07:03 +00:00
|
|
|
adjustBBOffsetsAfter(MBB);
|
2009-08-01 06:13:52 +00:00
|
|
|
|
|
|
|
++NumTBs;
|
|
|
|
MadeChange = true;
|
2009-07-29 02:18:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
2009-11-11 02:47:19 +00:00
|
|
|
|
2012-03-23 23:07:03 +00:00
|
|
|
/// reorderThumb2JumpTables - Adjust the function's block layout to ensure that
|
2009-11-16 18:55:47 +00:00
|
|
|
/// jump tables always branch forwards, since that's what tbb and tbh need.
|
2012-03-23 23:07:03 +00:00
|
|
|
bool ARMConstantIslands::reorderThumb2JumpTables() {
|
2009-11-12 17:25:07 +00:00
|
|
|
bool MadeChange = false;
|
|
|
|
|
2011-12-12 18:16:53 +00:00
|
|
|
MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
|
2014-04-25 05:30:21 +00:00
|
|
|
if (!MJTI) return false;
|
2010-07-07 21:06:51 +00:00
|
|
|
|
2009-11-12 17:25:07 +00:00
|
|
|
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
|
|
|
|
for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) {
|
|
|
|
MachineInstr *MI = T2JumpTables[i];
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &MCID = MI->getDesc();
|
|
|
|
unsigned NumOps = MCID.getNumOperands();
|
2011-12-07 07:15:52 +00:00
|
|
|
unsigned JTOpIdx = NumOps - (MI->isPredicable() ? 3 : 2);
|
2009-11-12 17:25:07 +00:00
|
|
|
MachineOperand JTOP = MI->getOperand(JTOpIdx);
|
|
|
|
unsigned JTI = JTOP.getIndex();
|
|
|
|
assert(JTI < JT.size());
|
|
|
|
|
|
|
|
// We prefer if target blocks for the jump table come after the jump
|
|
|
|
// instruction so we can use TB[BH]. Loop through the target blocks
|
|
|
|
// and try to adjust them such that that's true.
|
2009-11-16 18:58:52 +00:00
|
|
|
int JTNumber = MI->getParent()->getNumber();
|
2009-11-12 17:25:07 +00:00
|
|
|
const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
|
|
|
|
for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) {
|
|
|
|
MachineBasicBlock *MBB = JTBBs[j];
|
2009-11-16 18:58:52 +00:00
|
|
|
int DTNumber = MBB->getNumber();
|
2009-11-12 17:25:07 +00:00
|
|
|
|
2009-11-16 18:58:52 +00:00
|
|
|
if (DTNumber < JTNumber) {
|
2009-11-12 17:25:07 +00:00
|
|
|
// The destination precedes the switch. Try to move the block forward
|
|
|
|
// so we have a positive offset.
|
|
|
|
MachineBasicBlock *NewBB =
|
2012-03-23 23:07:03 +00:00
|
|
|
adjustJTTargetBlockForward(MBB, MI->getParent());
|
2009-11-12 17:25:07 +00:00
|
|
|
if (NewBB)
|
2009-11-14 20:10:18 +00:00
|
|
|
MJTI->ReplaceMBBInJumpTable(JTI, JTBBs[j], NewBB);
|
2009-11-12 17:25:07 +00:00
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2009-11-11 02:47:19 +00:00
|
|
|
MachineBasicBlock *ARMConstantIslands::
|
2012-03-23 23:07:03 +00:00
|
|
|
adjustJTTargetBlockForward(MachineBasicBlock *BB, MachineBasicBlock *JTBB) {
|
2010-07-07 22:53:35 +00:00
|
|
|
// If the destination block is terminated by an unconditional branch,
|
2009-11-12 17:25:07 +00:00
|
|
|
// try to move it; otherwise, create a new block following the jump
|
2009-11-16 18:58:52 +00:00
|
|
|
// table that branches back to the actual target. This is a very simple
|
|
|
|
// heuristic. FIXME: We can definitely improve it.
|
2014-04-25 05:30:21 +00:00
|
|
|
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
|
2009-11-12 17:25:07 +00:00
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
2009-11-17 01:21:04 +00:00
|
|
|
SmallVector<MachineOperand, 4> CondPrior;
|
|
|
|
MachineFunction::iterator BBi = BB;
|
2014-03-02 12:27:27 +00:00
|
|
|
MachineFunction::iterator OldPrior = std::prev(BBi);
|
2009-11-14 20:10:18 +00:00
|
|
|
|
2009-11-16 17:10:56 +00:00
|
|
|
// If the block terminator isn't analyzable, don't try to move the block
|
2009-11-17 01:21:04 +00:00
|
|
|
bool B = TII->AnalyzeBranch(*BB, TBB, FBB, Cond);
|
2009-11-16 17:10:56 +00:00
|
|
|
|
2009-11-17 01:21:04 +00:00
|
|
|
// If the block ends in an unconditional branch, move it. The prior block
|
|
|
|
// has to have an analyzable terminator for us to move this one. Be paranoid
|
2009-11-16 18:58:52 +00:00
|
|
|
// and make sure we're not trying to move the entry block of the function.
|
2011-12-12 18:16:53 +00:00
|
|
|
if (!B && Cond.empty() && BB != MF->begin() &&
|
2009-11-17 01:21:04 +00:00
|
|
|
!TII->AnalyzeBranch(*OldPrior, TBB, FBB, CondPrior)) {
|
2009-11-12 17:25:07 +00:00
|
|
|
BB->moveAfter(JTBB);
|
|
|
|
OldPrior->updateTerminator();
|
2009-11-14 20:10:18 +00:00
|
|
|
BB->updateTerminator();
|
2009-11-16 18:58:52 +00:00
|
|
|
// Update numbering to account for the block being moved.
|
2011-12-12 18:16:53 +00:00
|
|
|
MF->RenumberBlocks();
|
2009-11-12 17:25:07 +00:00
|
|
|
++NumJTMoved;
|
2014-04-25 05:30:21 +00:00
|
|
|
return nullptr;
|
2009-11-12 17:25:07 +00:00
|
|
|
}
|
2009-11-11 02:47:19 +00:00
|
|
|
|
|
|
|
// Create a new MBB for the code after the jump BB.
|
|
|
|
MachineBasicBlock *NewBB =
|
2011-12-12 18:16:53 +00:00
|
|
|
MF->CreateMachineBasicBlock(JTBB->getBasicBlock());
|
2009-11-11 02:47:19 +00:00
|
|
|
MachineFunction::iterator MBBI = JTBB; ++MBBI;
|
2011-12-12 18:16:53 +00:00
|
|
|
MF->insert(MBBI, NewBB);
|
2009-11-11 02:47:19 +00:00
|
|
|
|
|
|
|
// Add an unconditional branch from NewBB to BB.
|
|
|
|
// There doesn't seem to be meaningful DebugInfo available; this doesn't
|
|
|
|
// correspond directly to anything in the source.
|
|
|
|
assert (isThumb2 && "Adjusting for TB[BH] but not in Thumb2?");
|
2011-09-09 21:48:23 +00:00
|
|
|
BuildMI(NewBB, DebugLoc(), TII->get(ARM::t2B)).addMBB(BB)
|
|
|
|
.addImm(ARMCC::AL).addReg(0);
|
2009-11-11 02:47:19 +00:00
|
|
|
|
2009-11-14 20:10:18 +00:00
|
|
|
// Update internal data structures to account for the newly inserted MBB.
|
2011-12-12 18:16:53 +00:00
|
|
|
MF->RenumberBlocks(NewBB);
|
2009-11-14 20:10:18 +00:00
|
|
|
|
2009-11-11 02:47:19 +00:00
|
|
|
// Update the CFG.
|
|
|
|
NewBB->addSuccessor(BB);
|
|
|
|
JTBB->removeSuccessor(BB);
|
|
|
|
JTBB->addSuccessor(NewBB);
|
|
|
|
|
2009-11-12 17:25:07 +00:00
|
|
|
++NumJTInserted;
|
2009-11-11 02:47:19 +00:00
|
|
|
return NewBB;
|
|
|
|
}
|