2010-12-21 09:54:40 +08:00
|
|
|
//===-- ARMConstantIslandPass.cpp - ARM constant islands ------------------===//
|
2007-01-19 15:51:42 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-01-19 15:51:42 +08: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"
|
2016-07-22 16:39:12 +08:00
|
|
|
#include "ARMBasicBlockInfo.h"
|
2007-01-25 11:12:46 +08:00
|
|
|
#include "ARMMachineFunctionInfo.h"
|
2011-07-21 07:34:39 +08:00
|
|
|
#include "MCTargetDesc/ARMAddressingModes.h"
|
2012-12-04 00:50:05 +08: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 15:51:42 +08:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2009-07-29 10:18:14 +08:00
|
|
|
#include "llvm/CodeGen/MachineJumpTableInfo.h"
|
2012-03-30 07:14:26 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2007-01-19 15:51:42 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-12 04:10:48 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2011-12-10 10:55:06 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2009-08-23 11:41:05 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2009-10-16 04:49:47 +08:00
|
|
|
#include <algorithm>
|
2007-01-19 15:51:42 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "arm-cp-islands"
|
|
|
|
|
2009-08-14 08:32:16 +08: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-15 02:31:44 +08:00
|
|
|
STATISTIC(NumT2BrShrunk, "Number of Thumb2 immediate branches shrunk");
|
2009-11-01 07:46:45 +08:00
|
|
|
STATISTIC(NumCBZ, "Number of CBZ / CBNZ formed");
|
2009-11-11 10:47:19 +08:00
|
|
|
STATISTIC(NumJTMoved, "Number of jump table destination blocks moved");
|
2009-11-13 01:25:07 +08:00
|
|
|
STATISTIC(NumJTInserted, "Number of jump table intermediate blocks inserted");
|
2009-11-11 10:47:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
2009-11-18 05:24:11 +08:00
|
|
|
AdjustJumpTableBlocks("arm-adjust-jump-tables", cl::Hidden, cl::init(true),
|
2009-11-11 10:47:19 +08:00
|
|
|
cl::desc("Adjust basic block layout to better use TB[BH]"));
|
2007-01-19 15:51:42 +08:00
|
|
|
|
2016-02-24 02:39:19 +08:00
|
|
|
static cl::opt<unsigned>
|
|
|
|
CPMaxIteration("arm-constant-island-max-iteration", cl::Hidden, cl::init(30),
|
|
|
|
cl::desc("The max number of iteration for converge"));
|
|
|
|
|
2007-01-19 15:51:42 +08:00
|
|
|
namespace {
|
2007-02-23 13:02:36 +08:00
|
|
|
/// ARMConstantIslands - Due to limited PC-relative displacements, ARM
|
2007-01-19 15:51:42 +08: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 13:02:36 +08:00
|
|
|
/// constant pool; instead, it places constants wherever it feels like with
|
2007-01-19 15:51:42 +08: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 14:33:48 +08:00
|
|
|
class ARMConstantIslands : public MachineFunctionPass {
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2011-12-07 09:08:25 +08:00
|
|
|
std::vector<BasicBlockInfo> BBInfo;
|
2007-02-25 08:47:03 +08:00
|
|
|
|
2007-01-19 15:51:42 +08: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-10 07:59:14 +08:00
|
|
|
std::vector<MachineBasicBlock*> WaterList;
|
2007-02-10 04:54:44 +08:00
|
|
|
|
2009-10-16 04:49:47 +08:00
|
|
|
/// NewWaterList - The subset of WaterList that was created since the
|
|
|
|
/// previous iteration by inserting unconditional branches.
|
|
|
|
SmallSet<MachineBasicBlock*, 4> NewWaterList;
|
|
|
|
|
2009-10-13 02:52:13 +08:00
|
|
|
typedef std::vector<MachineBasicBlock*>::iterator water_iterator;
|
|
|
|
|
2007-01-19 15:51:42 +08: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 13:52:29 +08: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 15:51:42 +08:00
|
|
|
struct CPUser {
|
|
|
|
MachineInstr *MI;
|
|
|
|
MachineInstr *CPEMI;
|
2009-10-15 13:52:29 +08:00
|
|
|
MachineBasicBlock *HighWaterMark;
|
2007-01-19 15:51:42 +08:00
|
|
|
unsigned MaxDisp;
|
2009-07-22 07:56:01 +08:00
|
|
|
bool NegOk;
|
2009-07-24 02:27:47 +08:00
|
|
|
bool IsSoImm;
|
2012-01-10 09:34:59 +08:00
|
|
|
bool KnownAlignment;
|
2009-07-24 02:27:47 +08:00
|
|
|
CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp,
|
|
|
|
bool neg, bool soimm)
|
2012-01-10 09:34:59 +08:00
|
|
|
: MI(mi), CPEMI(cpemi), MaxDisp(maxdisp), NegOk(neg), IsSoImm(soimm),
|
|
|
|
KnownAlignment(false) {
|
2009-10-15 13:52:29 +08:00
|
|
|
HighWaterMark = CPEMI->getParent();
|
|
|
|
}
|
2012-01-10 09:34:59 +08:00
|
|
|
/// getMaxDisp - Returns the maximum displacement supported by MI.
|
|
|
|
/// Correct for unknown alignment.
|
2012-03-31 08:06:44 +08:00
|
|
|
/// Conservatively subtract 2 bytes to handle weird alignment effects.
|
2012-01-10 09:34:59 +08:00
|
|
|
unsigned getMaxDisp() const {
|
2012-03-31 08:06:44 +08:00
|
|
|
return (KnownAlignment ? MaxDisp : MaxDisp - 2) - 2;
|
2012-01-10 09:34:59 +08:00
|
|
|
}
|
2007-01-19 15:51:42 +08:00
|
|
|
};
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2007-01-19 15:51:42 +08:00
|
|
|
/// CPUsers - Keep track of all of the machine instructions that use various
|
|
|
|
/// constant pools and their max displacement.
|
2007-02-10 07:59:14 +08:00
|
|
|
std::vector<CPUser> CPUsers;
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2007-02-10 04:54:44 +08: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
|
2015-06-01 03:22:07 +08: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.
|
|
|
|
///
|
|
|
|
/// The first half of CPEntries contains generic constants, the second half
|
|
|
|
/// contains jump tables. Use getCombinedIndex on a generic CPEMI to look up
|
|
|
|
/// which vector it will be in here.
|
2007-02-10 04:54:44 +08:00
|
|
|
std::vector<std::vector<CPEntry> > CPEntries;
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2015-06-01 03:22:07 +08:00
|
|
|
/// Maps a JT index to the offset in CPEntries containing copies of that
|
|
|
|
/// table. The equivalent map for a CONSTPOOL_ENTRY is the identity.
|
|
|
|
DenseMap<int, int> JumpTableEntryIndices;
|
|
|
|
|
|
|
|
/// Maps a JT index to the LEA that actually uses the index to calculate its
|
|
|
|
/// base address.
|
|
|
|
DenseMap<int, int> JumpTableUserIndices;
|
|
|
|
|
2007-01-25 11:12:46 +08: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-26 07:18:59 +08:00
|
|
|
unsigned MaxDisp : 31;
|
|
|
|
bool isCond : 1;
|
2015-05-19 04:27:55 +08:00
|
|
|
unsigned UncondBr;
|
|
|
|
ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, unsigned ubr)
|
2007-01-26 07:18:59 +08:00
|
|
|
: MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {}
|
2007-01-25 11:12:46 +08:00
|
|
|
};
|
|
|
|
|
2007-05-16 13:14:06 +08:00
|
|
|
/// ImmBranches - Keep track of all the immediate branch instructions.
|
2007-01-25 11:12:46 +08:00
|
|
|
///
|
2007-02-10 07:59:14 +08:00
|
|
|
std::vector<ImmBranch> ImmBranches;
|
2007-01-25 11:12:46 +08:00
|
|
|
|
2007-01-30 09:18:38 +08:00
|
|
|
/// PushPopMIs - Keep track of all the Thumb push / pop instructions.
|
|
|
|
///
|
2007-02-10 04:54:44 +08:00
|
|
|
SmallVector<MachineInstr*, 4> PushPopMIs;
|
2007-01-30 09:18:38 +08:00
|
|
|
|
2009-07-29 10:18:14 +08:00
|
|
|
/// T2JumpTables - Keep track of all the Thumb2 jumptable instructions.
|
|
|
|
SmallVector<MachineInstr*, 4> T2JumpTables;
|
|
|
|
|
2007-01-30 09:18:38 +08:00
|
|
|
/// HasFarJump - True if any far jump instruction has been emitted during
|
|
|
|
/// the branch fix up pass.
|
|
|
|
bool HasFarJump;
|
|
|
|
|
2011-12-13 02:16:53 +08:00
|
|
|
MachineFunction *MF;
|
|
|
|
MachineConstantPool *MCP;
|
2012-03-26 07:49:58 +08:00
|
|
|
const ARMBaseInstrInfo *TII;
|
2009-08-01 14:13:52 +08:00
|
|
|
const ARMSubtarget *STI;
|
2007-04-30 03:19:30 +08:00
|
|
|
ARMFunctionInfo *AFI;
|
2007-03-01 07:20:38 +08:00
|
|
|
bool isThumb;
|
2009-07-24 02:27:47 +08:00
|
|
|
bool isThumb1;
|
2009-07-01 02:04:13 +08:00
|
|
|
bool isThumb2;
|
2007-01-19 15:51:42 +08:00
|
|
|
public:
|
2007-05-03 09:11:54 +08:00
|
|
|
static char ID;
|
2010-08-07 02:33:48 +08:00
|
|
|
ARMConstantIslands() : MachineFunctionPass(ID) {}
|
2007-05-02 05:15:47 +08:00
|
|
|
|
2014-03-10 10:09:33 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2007-01-19 15:51:42 +08:00
|
|
|
|
2016-04-05 01:09:25 +08:00
|
|
|
MachineFunctionProperties getRequiredProperties() const override {
|
|
|
|
return MachineFunctionProperties().set(
|
2016-08-25 09:27:13 +08:00
|
|
|
MachineFunctionProperties::Property::NoVRegs);
|
2016-04-05 01:09:25 +08:00
|
|
|
}
|
|
|
|
|
2014-03-10 10:09:33 +08:00
|
|
|
const char *getPassName() const override {
|
2007-01-25 11:12:46 +08:00
|
|
|
return "ARM constant island placement and branch shortening pass";
|
2007-01-19 15:51:42 +08:00
|
|
|
}
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2007-01-19 15:51:42 +08:00
|
|
|
private:
|
2015-06-01 03:22:07 +08:00
|
|
|
void doInitialConstPlacement(std::vector<MachineInstr *> &CPEMIs);
|
|
|
|
void doInitialJumpTablePlacement(std::vector<MachineInstr *> &CPEMIs);
|
2014-11-14 01:58:51 +08:00
|
|
|
bool BBHasFallthrough(MachineBasicBlock *MBB);
|
2007-02-10 04:54:44 +08:00
|
|
|
CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI);
|
2011-12-13 02:45:45 +08:00
|
|
|
unsigned getCPELogAlign(const MachineInstr *CPEMI);
|
2012-03-24 07:07:03 +08: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);
|
2015-06-01 03:22:07 +08:00
|
|
|
unsigned getCombinedIndex(const MachineInstr *CPEMI);
|
2012-03-24 07:07:03 +08:00
|
|
|
int findInRangeCPEntry(CPUser& U, unsigned UserOffset);
|
|
|
|
bool findAvailableWater(CPUser&U, unsigned UserOffset,
|
2016-02-24 02:39:19 +08:00
|
|
|
water_iterator &WaterIter, bool CloserWater);
|
2012-03-24 07:07:03 +08:00
|
|
|
void createNewWater(unsigned CPUserIndex, unsigned UserOffset,
|
2009-10-13 05:39:43 +08:00
|
|
|
MachineBasicBlock *&NewMBB);
|
2016-02-24 02:39:19 +08:00
|
|
|
bool handleConstantPoolUser(unsigned CPUserIndex, bool CloserWater);
|
2012-03-24 07:07:03 +08:00
|
|
|
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 08:44:30 +08:00
|
|
|
CPUser &U, unsigned &Growth);
|
2012-03-24 07:07:03 +08:00
|
|
|
bool isBBInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp);
|
|
|
|
bool fixupImmediateBr(ImmBranch &Br);
|
|
|
|
bool fixupConditionalBr(ImmBranch &Br);
|
|
|
|
bool fixupUnconditionalBr(ImmBranch &Br);
|
|
|
|
bool undoLRSpillRestore();
|
|
|
|
bool optimizeThumb2Instructions();
|
|
|
|
bool optimizeThumb2Branches();
|
|
|
|
bool reorderThumb2JumpTables();
|
2015-06-01 03:22:07 +08:00
|
|
|
bool preserveBaseRegister(MachineInstr *JumpMI, MachineInstr *LEAMI,
|
|
|
|
unsigned &DeadSize, bool &CanDeleteLEA,
|
|
|
|
bool &BaseRegKill);
|
2012-03-24 07:07:03 +08:00
|
|
|
bool optimizeThumb2JumpTables();
|
|
|
|
MachineBasicBlock *adjustJTTargetBlockForward(MachineBasicBlock *BB,
|
2009-11-11 10:47:19 +08:00
|
|
|
MachineBasicBlock *JTBB);
|
2007-01-19 15:51:42 +08:00
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
unsigned getOffsetOf(MachineInstr *MI) const;
|
|
|
|
unsigned getUserOffset(CPUser&) const;
|
2007-04-30 03:19:30 +08:00
|
|
|
void dumpBBs();
|
2011-12-13 02:16:53 +08:00
|
|
|
void verify();
|
2011-12-10 03:44:39 +08:00
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset,
|
2011-12-10 03:44:39 +08:00
|
|
|
unsigned Disp, bool NegativeOK, bool IsSoImm = false);
|
2012-03-24 07:07:03 +08:00
|
|
|
bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset,
|
2011-12-10 03:44:39 +08:00
|
|
|
const CPUser &U) {
|
2012-03-24 07:07:03 +08:00
|
|
|
return isOffsetInRange(UserOffset, TrialOffset,
|
2012-01-10 09:34:59 +08:00
|
|
|
U.getMaxDisp(), U.NegOk, U.IsSoImm);
|
2011-12-10 03:44:39 +08:00
|
|
|
}
|
2007-01-19 15:51:42 +08:00
|
|
|
};
|
2007-05-03 09:11:54 +08:00
|
|
|
char ARMConstantIslands::ID = 0;
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2007-01-19 15:51:42 +08:00
|
|
|
|
2007-04-30 03:19:30 +08:00
|
|
|
/// verify - check BBOffsets, BBSizes, alignment of islands
|
2011-12-13 02:16:53 +08:00
|
|
|
void ARMConstantIslands::verify() {
|
2009-07-24 02:27:47 +08:00
|
|
|
#ifndef NDEBUG
|
2016-01-04 03:43:40 +08:00
|
|
|
assert(std::is_sorted(MF->begin(), MF->end(),
|
|
|
|
[this](const MachineBasicBlock &LHS,
|
|
|
|
const MachineBasicBlock &RHS) {
|
|
|
|
return BBInfo[LHS.getNumber()].postOffset() <
|
|
|
|
BBInfo[RHS.getNumber()].postOffset();
|
|
|
|
}));
|
2012-03-31 08:06:42 +08:00
|
|
|
DEBUG(dbgs() << "Verifying " << CPUsers.size() << " CP users.\n");
|
2009-11-20 07:10:28 +08:00
|
|
|
for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) {
|
|
|
|
CPUser &U = CPUsers[i];
|
2012-03-24 07:07:03 +08:00
|
|
|
unsigned UserOffset = getUserOffset(U);
|
2012-03-31 08:06:44 +08: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 08:06:42 +08: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-20 07:10:28 +08:00
|
|
|
}
|
2009-11-21 03:37:38 +08:00
|
|
|
#endif
|
2007-04-30 03:19:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// print block size and offset information - debugging
|
|
|
|
void ARMConstantIslands::dumpBBs() {
|
2011-12-10 10:55:06 +08: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-30 03:19:30 +08:00
|
|
|
}
|
|
|
|
|
2007-01-25 11:12:46 +08:00
|
|
|
/// createARMConstantIslandPass - returns an instance of the constpool
|
|
|
|
/// island pass.
|
2007-01-19 15:51:42 +08:00
|
|
|
FunctionPass *llvm::createARMConstantIslandPass() {
|
|
|
|
return new ARMConstantIslands();
|
|
|
|
}
|
|
|
|
|
2011-12-13 02:16:53 +08:00
|
|
|
bool ARMConstantIslands::runOnMachineFunction(MachineFunction &mf) {
|
|
|
|
MF = &mf;
|
|
|
|
MCP = mf.getConstantPool();
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2011-12-10 10:55:06 +08:00
|
|
|
DEBUG(dbgs() << "***** ARMConstantIslands: "
|
2011-12-13 02:16:53 +08:00
|
|
|
<< MCP->getConstants().size() << " CP entries, aligned to "
|
|
|
|
<< MCP->getConstantPoolAlignment() << " bytes *****\n");
|
2011-12-10 10:55:06 +08:00
|
|
|
|
2015-01-29 08:19:33 +08:00
|
|
|
STI = &static_cast<const ARMSubtarget &>(MF->getSubtarget());
|
|
|
|
TII = STI->getInstrInfo();
|
2011-12-13 02:16:53 +08:00
|
|
|
AFI = MF->getInfo<ARMFunctionInfo>();
|
2009-08-01 14:13:52 +08:00
|
|
|
|
2007-03-01 07:20:38 +08:00
|
|
|
isThumb = AFI->isThumbFunction();
|
2009-07-24 02:27:47 +08:00
|
|
|
isThumb1 = AFI->isThumb1OnlyFunction();
|
2009-07-01 02:04:13 +08:00
|
|
|
isThumb2 = AFI->isThumb2Function();
|
2007-01-30 09:18:38 +08:00
|
|
|
|
|
|
|
HasFarJump = false;
|
|
|
|
|
2012-03-30 07:14:26 +08:00
|
|
|
// This pass invalidates liveness information when it splits basic blocks.
|
|
|
|
MF->getRegInfo().invalidateLiveness();
|
|
|
|
|
2007-01-19 15:51:42 +08: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-13 02:16:53 +08:00
|
|
|
MF->RenumberBlocks();
|
2007-01-19 15:51:42 +08:00
|
|
|
|
2009-11-13 01:25:07 +08: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-24 07:07:03 +08:00
|
|
|
scanFunctionJumpTables();
|
|
|
|
MadeChange |= reorderThumb2JumpTables();
|
2009-11-13 01:25:07 +08: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-13 02:16:53 +08:00
|
|
|
MF->RenumberBlocks();
|
2009-11-13 01:25:07 +08:00
|
|
|
}
|
|
|
|
|
2007-01-19 15:51:42 +08: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-10 07:59:14 +08:00
|
|
|
std::vector<MachineInstr*> CPEMIs;
|
2011-12-13 02:45:45 +08:00
|
|
|
if (!MCP->isEmpty())
|
2015-06-01 03:22:07 +08:00
|
|
|
doInitialConstPlacement(CPEMIs);
|
|
|
|
|
|
|
|
if (MF->getJumpTableInfo())
|
|
|
|
doInitialJumpTablePlacement(CPEMIs);
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2007-01-19 15:51:42 +08:00
|
|
|
/// The next UID to take is the first unused one.
|
2011-01-17 16:03:18 +08:00
|
|
|
AFI->initPICLabelUId(CPEMIs.size());
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2007-01-19 15:51:42 +08: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-24 07:07:03 +08:00
|
|
|
initializeFunctionInfo(CPEMIs);
|
2007-01-19 15:51:42 +08:00
|
|
|
CPEMIs.clear();
|
2010-07-24 06:50:23 +08:00
|
|
|
DEBUG(dumpBBs());
|
|
|
|
|
2015-05-02 02:05:59 +08:00
|
|
|
// Functions with jump tables need an alignment of 4 because they use the ADR
|
|
|
|
// instruction, which aligns the PC to 4 bytes before adding an offset.
|
|
|
|
if (!T2JumpTables.empty())
|
|
|
|
MF->ensureAlignment(2);
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2007-04-04 07:39:48 +08:00
|
|
|
/// Remove dead constant pool entries.
|
2012-03-24 07:07:03 +08:00
|
|
|
MadeChange |= removeUnusedCPEntries();
|
2007-04-04 07:39:48 +08:00
|
|
|
|
2007-01-30 09:18:38 +08:00
|
|
|
// Iteratively place constant pool entries and fix up branches until there
|
|
|
|
// is no change.
|
2009-08-07 15:35:21 +08:00
|
|
|
unsigned NoCPIters = 0, NoBRIters = 0;
|
2007-01-30 09:18:38 +08:00
|
|
|
while (true) {
|
2011-12-10 02:20:35 +08:00
|
|
|
DEBUG(dbgs() << "Beginning CP iteration #" << NoCPIters << '\n');
|
2009-08-07 15:35:21 +08:00
|
|
|
bool CPChange = false;
|
2007-01-19 15:51:42 +08:00
|
|
|
for (unsigned i = 0, e = CPUsers.size(); i != e; ++i)
|
2016-02-24 02:39:19 +08:00
|
|
|
// For most inputs, it converges in no more than 5 iterations.
|
2016-04-22 14:37:48 +08:00
|
|
|
// If it doesn't end in 10, the input may have huge BB or many CPEs.
|
|
|
|
// In this case, we will try different heuristics.
|
2016-02-24 02:39:19 +08:00
|
|
|
CPChange |= handleConstantPoolUser(i, NoCPIters >= CPMaxIteration / 2);
|
|
|
|
if (CPChange && ++NoCPIters > CPMaxIteration)
|
2012-01-10 06:16:24 +08:00
|
|
|
report_fatal_error("Constant Island pass failed to converge!");
|
2007-07-11 06:00:16 +08:00
|
|
|
DEBUG(dumpBBs());
|
2010-07-08 05:06:51 +08:00
|
|
|
|
2009-10-16 04:49:47 +08: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 15:35:21 +08:00
|
|
|
|
2011-12-10 02:20:35 +08:00
|
|
|
DEBUG(dbgs() << "Beginning BR iteration #" << NoBRIters << '\n');
|
2009-08-07 15:35:21 +08:00
|
|
|
bool BRChange = false;
|
2007-01-25 11:12:46 +08:00
|
|
|
for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
|
2012-03-24 07:07:03 +08:00
|
|
|
BRChange |= fixupImmediateBr(ImmBranches[i]);
|
2009-08-07 15:35:21 +08:00
|
|
|
if (BRChange && ++NoBRIters > 30)
|
2012-01-10 06:16:24 +08:00
|
|
|
report_fatal_error("Branch Fix Up pass failed to converge!");
|
2007-07-11 06:00:16 +08:00
|
|
|
DEBUG(dumpBBs());
|
2009-08-07 15:35:21 +08:00
|
|
|
|
|
|
|
if (!CPChange && !BRChange)
|
2007-01-30 09:18:38 +08:00
|
|
|
break;
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
2007-04-04 07:39:48 +08:00
|
|
|
|
2016-01-15 18:26:17 +08:00
|
|
|
// Shrink 32-bit Thumb2 load and store instructions.
|
2010-08-10 02:35:19 +08:00
|
|
|
if (isThumb2 && !STI->prefers32BitThumb())
|
2012-03-24 07:07:03 +08:00
|
|
|
MadeChange |= optimizeThumb2Instructions();
|
2009-08-01 14:13:52 +08:00
|
|
|
|
2016-01-15 18:26:17 +08:00
|
|
|
// Shrink 32-bit branch instructions.
|
|
|
|
if (isThumb && STI->hasV8MBaselineOps())
|
|
|
|
MadeChange |= optimizeThumb2Branches();
|
|
|
|
|
|
|
|
// Optimize jump tables using TBB / TBH.
|
|
|
|
if (isThumb2)
|
|
|
|
MadeChange |= optimizeThumb2JumpTables();
|
|
|
|
|
2007-04-30 03:19:30 +08:00
|
|
|
// After a while, this might be made debug-only, but it is not expensive.
|
2011-12-13 02:16:53 +08:00
|
|
|
verify();
|
2007-04-30 03:19:30 +08:00
|
|
|
|
2010-07-08 05:06:51 +08: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 10:18:14 +08:00
|
|
|
if (isThumb && !HasFarJump && AFI->isLRSpilledForFarJump())
|
2012-03-24 07:07:03 +08:00
|
|
|
MadeChange |= undoLRSpillRestore();
|
2007-01-30 09:18:38 +08:00
|
|
|
|
2011-01-31 06:07:39 +08: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];
|
2015-06-01 03:22:07 +08:00
|
|
|
if (CPE.CPEMI && CPE.CPEMI->getOperand(1).isCPI())
|
|
|
|
AFI->recordCPEClone(i, CPE.CPI);
|
2011-01-31 06:07:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-10 02:20:35 +08:00
|
|
|
DEBUG(dbgs() << '\n'; dumpBBs());
|
2010-07-22 10:09:47 +08:00
|
|
|
|
2011-12-07 09:08:25 +08:00
|
|
|
BBInfo.clear();
|
2007-01-19 15:51:42 +08:00
|
|
|
WaterList.clear();
|
|
|
|
CPUsers.clear();
|
2007-02-10 04:54:44 +08:00
|
|
|
CPEntries.clear();
|
2015-06-01 03:22:07 +08:00
|
|
|
JumpTableEntryIndices.clear();
|
|
|
|
JumpTableUserIndices.clear();
|
2007-01-25 11:12:46 +08:00
|
|
|
ImmBranches.clear();
|
2007-02-10 04:54:44 +08:00
|
|
|
PushPopMIs.clear();
|
2009-07-29 10:18:14 +08:00
|
|
|
T2JumpTables.clear();
|
2007-01-30 09:18:38 +08:00
|
|
|
|
|
|
|
return MadeChange;
|
2007-01-19 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
2015-06-01 03:22:07 +08:00
|
|
|
/// \brief Perform the initial placement of the regular constant pool entries.
|
|
|
|
/// To start with, we put them all at the end of the function.
|
2011-12-13 02:16:53 +08:00
|
|
|
void
|
2015-06-01 03:22:07 +08:00
|
|
|
ARMConstantIslands::doInitialConstPlacement(std::vector<MachineInstr*> &CPEMIs) {
|
2007-01-19 15:51:42 +08:00
|
|
|
// Create the basic block to hold the CPE's.
|
2011-12-13 02:16:53 +08:00
|
|
|
MachineBasicBlock *BB = MF->CreateMachineBasicBlock();
|
|
|
|
MF->push_back(BB);
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2011-12-13 00:49:37 +08:00
|
|
|
// MachineConstantPool measures alignment in bytes. We measure in log2(bytes).
|
2011-12-15 02:49:13 +08:00
|
|
|
unsigned MaxAlign = Log2_32(MCP->getConstantPoolAlignment());
|
2011-12-13 00:49:37 +08:00
|
|
|
|
|
|
|
// Mark the basic block as required by the const-pool.
|
2015-04-24 04:31:22 +08:00
|
|
|
BB->setAlignment(MaxAlign);
|
2011-12-13 00:49:37 +08:00
|
|
|
|
2011-12-13 02:45:45 +08:00
|
|
|
// The function needs to be as aligned as the basic blocks. The linker may
|
|
|
|
// move functions around based on their alignment.
|
2012-07-07 07:13:38 +08:00
|
|
|
MF->ensureAlignment(BB->getAlignment());
|
2011-12-13 02:45:45 +08:00
|
|
|
|
2011-12-13 00:49:37 +08: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 09:43:02 +08:00
|
|
|
|
2007-01-19 15:51:42 +08: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-15 02:49:13 +08:00
|
|
|
const std::vector<MachineConstantPoolEntry> &CPs = MCP->getConstants();
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2015-07-16 14:11:10 +08:00
|
|
|
const DataLayout &TD = MF->getDataLayout();
|
2007-01-19 15:51:42 +08:00
|
|
|
for (unsigned i = 0, e = CPs.size(); i != e; ++i) {
|
2009-05-09 15:06:46 +08:00
|
|
|
unsigned Size = TD.getTypeAllocSize(CPs[i].getType());
|
2011-12-13 00:49:37 +08: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 15:51:42 +08:00
|
|
|
MachineInstr *CPEMI =
|
2011-12-13 00:49:37 +08:00
|
|
|
BuildMI(*BB, InsAt, DebugLoc(), TII->get(ARM::CONSTPOOL_ENTRY))
|
2010-04-03 04:16:16 +08:00
|
|
|
.addImm(i).addConstantPoolIndex(i).addImm(Size);
|
2007-01-19 15:51:42 +08:00
|
|
|
CPEMIs.push_back(CPEMI);
|
2007-02-10 04:54:44 +08:00
|
|
|
|
2011-12-13 00:49:37 +08:00
|
|
|
// Ensure that future entries with higher alignment get inserted before
|
|
|
|
// CPEMI. This is bucket sort with iterators.
|
2011-12-17 07:00:05 +08:00
|
|
|
for (unsigned a = LogAlign + 1; a <= MaxAlign; ++a)
|
2011-12-13 00:49:37 +08:00
|
|
|
if (InsPoint[a] == InsAt)
|
|
|
|
InsPoint[a] = CPEMI;
|
|
|
|
|
2007-02-10 04:54:44 +08:00
|
|
|
// Add a new CPEntry, but no corresponding CPUser yet.
|
2014-10-04 02:33:16 +08:00
|
|
|
CPEntries.emplace_back(1, CPEntry(CPEMI, i));
|
2010-06-22 23:08:57 +08:00
|
|
|
++NumCPEs;
|
2012-01-10 09:34:59 +08:00
|
|
|
DEBUG(dbgs() << "Moved CPI#" << i << " to end of function, size = "
|
|
|
|
<< Size << ", align = " << Align <<'\n');
|
2007-01-19 15:51:42 +08:00
|
|
|
}
|
2011-12-13 00:49:37 +08:00
|
|
|
DEBUG(BB->dump());
|
2007-01-19 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
2015-06-01 03:22:07 +08:00
|
|
|
/// \brief Do initial placement of the jump tables. Because Thumb2's TBB and TBH
|
|
|
|
/// instructions can be made more efficient if the jump table immediately
|
|
|
|
/// follows the instruction, it's best to place them immediately next to their
|
|
|
|
/// jumps to begin with. In almost all cases they'll never be moved from that
|
|
|
|
/// position.
|
|
|
|
void ARMConstantIslands::doInitialJumpTablePlacement(
|
|
|
|
std::vector<MachineInstr *> &CPEMIs) {
|
|
|
|
unsigned i = CPEntries.size();
|
|
|
|
auto MJTI = MF->getJumpTableInfo();
|
|
|
|
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
|
|
|
|
|
|
|
|
MachineBasicBlock *LastCorrectlyNumberedBB = nullptr;
|
|
|
|
for (MachineBasicBlock &MBB : *MF) {
|
|
|
|
auto MI = MBB.getLastNonDebugInstr();
|
2015-11-17 00:41:13 +08:00
|
|
|
if (MI == MBB.end())
|
|
|
|
continue;
|
2015-06-01 03:22:07 +08:00
|
|
|
|
|
|
|
unsigned JTOpcode;
|
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
case ARM::BR_JTadd:
|
|
|
|
case ARM::BR_JTr:
|
|
|
|
case ARM::tBR_JTr:
|
|
|
|
case ARM::BR_JTm:
|
|
|
|
JTOpcode = ARM::JUMPTABLE_ADDRS;
|
|
|
|
break;
|
|
|
|
case ARM::t2BR_JT:
|
|
|
|
JTOpcode = ARM::JUMPTABLE_INSTS;
|
|
|
|
break;
|
|
|
|
case ARM::t2TBB_JT:
|
|
|
|
JTOpcode = ARM::JUMPTABLE_TBB;
|
|
|
|
break;
|
|
|
|
case ARM::t2TBH_JT:
|
|
|
|
JTOpcode = ARM::JUMPTABLE_TBH;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned NumOps = MI->getDesc().getNumOperands();
|
|
|
|
MachineOperand JTOp =
|
|
|
|
MI->getOperand(NumOps - (MI->isPredicable() ? 2 : 1));
|
|
|
|
unsigned JTI = JTOp.getIndex();
|
|
|
|
unsigned Size = JT[JTI].MBBs.size() * sizeof(uint32_t);
|
|
|
|
MachineBasicBlock *JumpTableBB = MF->CreateMachineBasicBlock();
|
|
|
|
MF->insert(std::next(MachineFunction::iterator(MBB)), JumpTableBB);
|
|
|
|
MachineInstr *CPEMI = BuildMI(*JumpTableBB, JumpTableBB->begin(),
|
|
|
|
DebugLoc(), TII->get(JTOpcode))
|
|
|
|
.addImm(i++)
|
|
|
|
.addJumpTableIndex(JTI)
|
|
|
|
.addImm(Size);
|
|
|
|
CPEMIs.push_back(CPEMI);
|
|
|
|
CPEntries.emplace_back(1, CPEntry(CPEMI, JTI));
|
|
|
|
JumpTableEntryIndices.insert(std::make_pair(JTI, CPEntries.size() - 1));
|
|
|
|
if (!LastCorrectlyNumberedBB)
|
|
|
|
LastCorrectlyNumberedBB = &MBB;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we did anything then we need to renumber the subsequent blocks.
|
|
|
|
if (LastCorrectlyNumberedBB)
|
|
|
|
MF->RenumberBlocks(LastCorrectlyNumberedBB);
|
|
|
|
}
|
|
|
|
|
2007-02-23 13:02:36 +08:00
|
|
|
/// BBHasFallthrough - Return true if the specified basic block can fallthrough
|
2007-01-19 15:51:42 +08:00
|
|
|
/// into the block immediately after it.
|
2014-11-14 01:58:51 +08:00
|
|
|
bool ARMConstantIslands::BBHasFallthrough(MachineBasicBlock *MBB) {
|
2007-01-19 15:51:42 +08:00
|
|
|
// Get the next machine basic block in the function.
|
2015-10-20 07:25:57 +08:00
|
|
|
MachineFunction::iterator MBBI = MBB->getIterator();
|
2010-06-03 05:53:11 +08:00
|
|
|
// Can't fall off end of function.
|
2014-03-02 20:27:27 +08:00
|
|
|
if (std::next(MBBI) == MBB->getParent()->end())
|
2007-01-19 15:51:42 +08:00
|
|
|
return false;
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2015-10-20 07:25:57 +08:00
|
|
|
MachineBasicBlock *NextBB = &*std::next(MBBI);
|
2016-08-12 11:55:06 +08:00
|
|
|
if (!MBB->isSuccessor(NextBB))
|
2014-11-14 01:58:51 +08:00
|
|
|
return false;
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2014-11-14 01:58:51 +08:00
|
|
|
// Try to analyze the end of the block. A potential fallthrough may already
|
|
|
|
// have an unconditional branch for whatever reason.
|
|
|
|
MachineBasicBlock *TBB, *FBB;
|
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
2016-07-15 22:41:04 +08:00
|
|
|
bool TooDifficult = TII->analyzeBranch(*MBB, TBB, FBB, Cond);
|
2014-11-14 01:58:51 +08:00
|
|
|
return TooDifficult || FBB == nullptr;
|
2007-01-19 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
2007-02-10 04:54:44 +08: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 13:30:21 +08:00
|
|
|
return nullptr;
|
2007-02-10 04:54:44 +08:00
|
|
|
}
|
|
|
|
|
2011-12-13 02:45:45 +08:00
|
|
|
/// getCPELogAlign - Returns the required alignment of the constant pool entry
|
2011-12-13 03:25:51 +08:00
|
|
|
/// represented by CPEMI. Alignment is measured in log2(bytes) units.
|
2011-12-13 02:45:45 +08:00
|
|
|
unsigned ARMConstantIslands::getCPELogAlign(const MachineInstr *CPEMI) {
|
2015-06-01 03:22:07 +08:00
|
|
|
switch (CPEMI->getOpcode()) {
|
|
|
|
case ARM::CONSTPOOL_ENTRY:
|
|
|
|
break;
|
|
|
|
case ARM::JUMPTABLE_TBB:
|
|
|
|
return 0;
|
|
|
|
case ARM::JUMPTABLE_TBH:
|
|
|
|
case ARM::JUMPTABLE_INSTS:
|
|
|
|
return 1;
|
|
|
|
case ARM::JUMPTABLE_ADDRS:
|
|
|
|
return 2;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown constpool entry kind");
|
|
|
|
}
|
2011-12-13 02:45:45 +08:00
|
|
|
|
2015-06-01 03:22:07 +08:00
|
|
|
unsigned CPI = getCombinedIndex(CPEMI);
|
2011-12-13 02:45:45 +08:00
|
|
|
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-24 07:07:03 +08:00
|
|
|
/// scanFunctionJumpTables - Do a scan of the function, building up
|
2009-11-13 01:25:07 +08:00
|
|
|
/// information about the sizes of each block and the locations of all
|
|
|
|
/// the jump tables.
|
2012-03-24 07:07:03 +08:00
|
|
|
void ARMConstantIslands::scanFunctionJumpTables() {
|
2016-07-09 04:21:17 +08:00
|
|
|
for (MachineBasicBlock &MBB : *MF) {
|
|
|
|
for (MachineInstr &I : MBB)
|
|
|
|
if (I.isBranch() && I.getOpcode() == ARM::t2BR_JT)
|
|
|
|
T2JumpTables.push_back(&I);
|
2009-11-13 01:25:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// initializeFunctionInfo - Do the initial scan of the function, building up
|
2007-01-19 15:51:42 +08:00
|
|
|
/// information about the sizes of each block, the location of all the water,
|
|
|
|
/// and finding all of the constant pool users.
|
2011-12-13 02:16:53 +08:00
|
|
|
void ARMConstantIslands::
|
2012-03-24 07:07:03 +08:00
|
|
|
initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs) {
|
2011-12-07 12:17:35 +08:00
|
|
|
|
2016-07-22 16:39:12 +08:00
|
|
|
BBInfo = computeAllBlockSizes(MF);
|
2011-12-08 08:55:02 +08:00
|
|
|
|
|
|
|
// The known bits of the entry block offset are determined by the function
|
|
|
|
// alignment.
|
2011-12-13 02:16:53 +08:00
|
|
|
BBInfo.front().KnownBits = MF->getAlignment();
|
2011-12-08 08:55:02 +08:00
|
|
|
|
|
|
|
// Compute block offsets and known bits.
|
2015-10-20 07:25:57 +08:00
|
|
|
adjustBBOffsetsAfter(&MF->front());
|
2011-12-08 08:55:02 +08:00
|
|
|
|
2010-12-21 09:54:40 +08:00
|
|
|
// Now go back through the instructions and build up our data structures.
|
2016-07-09 04:21:17 +08:00
|
|
|
for (MachineBasicBlock &MBB : *MF) {
|
2007-01-19 15:51:42 +08: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-13 01:09:30 +08:00
|
|
|
|
2016-07-09 04:21:17 +08:00
|
|
|
for (MachineInstr &I : MBB) {
|
|
|
|
if (I.isDebugValue())
|
2010-06-22 01:49:23 +08:00
|
|
|
continue;
|
2011-12-07 12:17:35 +08:00
|
|
|
|
2016-07-09 04:21:17 +08:00
|
|
|
unsigned Opc = I.getOpcode();
|
|
|
|
if (I.isBranch()) {
|
2007-01-25 11:12:46 +08:00
|
|
|
bool isCond = false;
|
|
|
|
unsigned Bits = 0;
|
|
|
|
unsigned Scale = 1;
|
|
|
|
int UOpc = Opc;
|
|
|
|
switch (Opc) {
|
2009-07-29 10:18:14 +08:00
|
|
|
default:
|
|
|
|
continue; // Ignore other JT branches
|
|
|
|
case ARM::t2BR_JT:
|
2016-07-09 04:21:17 +08:00
|
|
|
T2JumpTables.push_back(&I);
|
2009-07-29 10:18:14 +08:00
|
|
|
continue; // Does not get an entry in ImmBranches
|
2007-01-25 11:12:46 +08:00
|
|
|
case ARM::Bcc:
|
|
|
|
isCond = true;
|
|
|
|
UOpc = ARM::B;
|
2016-08-17 13:10:15 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2007-01-25 11:12:46 +08:00
|
|
|
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-07-01 02:04:13 +08: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 11:12:46 +08:00
|
|
|
}
|
2007-02-01 18:16:15 +08:00
|
|
|
|
|
|
|
// Record this immediate branch.
|
2007-02-03 10:08:34 +08:00
|
|
|
unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
|
2016-07-09 04:21:17 +08:00
|
|
|
ImmBranches.push_back(ImmBranch(&I, MaxOffs, isCond, UOpc));
|
2007-01-25 11:12:46 +08:00
|
|
|
}
|
|
|
|
|
2007-01-30 09:18:38 +08:00
|
|
|
if (Opc == ARM::tPUSH || Opc == ARM::tPOP_RET)
|
2016-07-09 04:21:17 +08:00
|
|
|
PushPopMIs.push_back(&I);
|
2007-01-30 09:18:38 +08:00
|
|
|
|
2015-06-01 03:22:07 +08:00
|
|
|
if (Opc == ARM::CONSTPOOL_ENTRY || Opc == ARM::JUMPTABLE_ADDRS ||
|
|
|
|
Opc == ARM::JUMPTABLE_INSTS || Opc == ARM::JUMPTABLE_TBB ||
|
|
|
|
Opc == ARM::JUMPTABLE_TBH)
|
2009-07-24 02:27:47 +08:00
|
|
|
continue;
|
|
|
|
|
2007-01-19 15:51:42 +08:00
|
|
|
// Scan the instructions for constant pool operands.
|
2016-07-09 04:21:17 +08:00
|
|
|
for (unsigned op = 0, e = I.getNumOperands(); op != e; ++op)
|
|
|
|
if (I.getOperand(op).isCPI() || I.getOperand(op).isJTI()) {
|
2007-01-19 15:51:42 +08:00
|
|
|
// We found one. The addressing mode tells us the max displacement
|
|
|
|
// from the PC that this instruction permits.
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2007-01-19 15:51:42 +08:00
|
|
|
// Basic size info comes from the TSFlags field.
|
2007-02-01 18:16:15 +08:00
|
|
|
unsigned Bits = 0;
|
|
|
|
unsigned Scale = 1;
|
2009-07-22 07:56:01 +08:00
|
|
|
bool NegOk = false;
|
2009-07-24 02:27:47 +08:00
|
|
|
bool IsSoImm = false;
|
|
|
|
|
|
|
|
switch (Opc) {
|
2009-05-13 01:09:30 +08:00
|
|
|
default:
|
2009-07-15 00:55:14 +08:00
|
|
|
llvm_unreachable("Unknown addressing mode for CP reference!");
|
2009-07-24 02:27:47 +08:00
|
|
|
|
|
|
|
// Taking the address of a CP entry.
|
|
|
|
case ARM::LEApcrel:
|
2015-06-01 03:22:07 +08:00
|
|
|
case ARM::LEApcrelJT:
|
2009-07-24 02:27:47 +08:00
|
|
|
// This takes a SoImm, which is 8 bit immediate rotated. We'll
|
|
|
|
// pretend the maximum offset is 255 * 4. Since each instruction
|
2009-11-20 02:23:19 +08:00
|
|
|
// 4 byte wide, this is always correct. We'll check for other
|
2009-07-24 02:27:47 +08:00
|
|
|
// displacements that fits in a SoImm as well.
|
|
|
|
Bits = 8;
|
|
|
|
Scale = 4;
|
2009-07-22 07:56:01 +08:00
|
|
|
NegOk = true;
|
2009-07-24 02:27:47 +08:00
|
|
|
IsSoImm = true;
|
2007-01-19 15:51:42 +08:00
|
|
|
break;
|
2010-12-14 06:51:08 +08:00
|
|
|
case ARM::t2LEApcrel:
|
2015-06-01 03:22:07 +08:00
|
|
|
case ARM::t2LEApcrelJT:
|
2009-07-24 02:27:47 +08:00
|
|
|
Bits = 12;
|
2009-07-22 07:56:01 +08:00
|
|
|
NegOk = true;
|
2007-01-19 15:51:42 +08:00
|
|
|
break;
|
2009-07-24 02:27:47 +08:00
|
|
|
case ARM::tLEApcrel:
|
2015-06-01 03:22:07 +08:00
|
|
|
case ARM::tLEApcrelJT:
|
2007-02-01 18:16:15 +08:00
|
|
|
Bits = 8;
|
2009-07-24 02:27:47 +08:00
|
|
|
Scale = 4;
|
2007-01-19 15:51:42 +08:00
|
|
|
break;
|
2009-07-24 02:27:47 +08:00
|
|
|
|
2013-06-05 01:46:15 +08:00
|
|
|
case ARM::LDRBi12:
|
2010-10-27 06:37:02 +08:00
|
|
|
case ARM::LDRi12:
|
2009-07-24 02:27:47 +08:00
|
|
|
case ARM::LDRcp:
|
2011-02-09 06:39:40 +08:00
|
|
|
case ARM::t2LDRpci:
|
2009-07-24 02:27:47 +08:00
|
|
|
Bits = 12; // +-offset_12
|
|
|
|
NegOk = true;
|
2007-01-19 15:51:42 +08:00
|
|
|
break;
|
2009-07-24 02:27:47 +08:00
|
|
|
|
|
|
|
case ARM::tLDRpci:
|
2007-02-01 18:16:15 +08:00
|
|
|
Bits = 8;
|
|
|
|
Scale = 4; // +(offset_8*4)
|
2007-01-24 16:53:17 +08:00
|
|
|
break;
|
2009-07-24 02:27:47 +08:00
|
|
|
|
2009-11-09 08:11:35 +08:00
|
|
|
case ARM::VLDRD:
|
|
|
|
case ARM::VLDRS:
|
2009-07-24 02:27:47 +08:00
|
|
|
Bits = 8;
|
|
|
|
Scale = 4; // +-(offset_8*4)
|
|
|
|
NegOk = true;
|
2009-06-29 15:51:04 +08:00
|
|
|
break;
|
2007-01-19 15:51:42 +08:00
|
|
|
}
|
2007-02-01 18:16:15 +08:00
|
|
|
|
2007-01-19 15:51:42 +08:00
|
|
|
// Remember that this is a user of a CP entry.
|
2016-07-09 04:21:17 +08:00
|
|
|
unsigned CPI = I.getOperand(op).getIndex();
|
|
|
|
if (I.getOperand(op).isJTI()) {
|
2015-06-01 03:22:07 +08:00
|
|
|
JumpTableUserIndices.insert(std::make_pair(CPI, CPUsers.size()));
|
|
|
|
CPI = JumpTableEntryIndices[CPI];
|
|
|
|
}
|
|
|
|
|
2007-02-10 04:54:44 +08:00
|
|
|
MachineInstr *CPEMI = CPEMIs[CPI];
|
2009-08-15 02:31:44 +08:00
|
|
|
unsigned MaxOffs = ((1 << Bits)-1) * Scale;
|
2016-07-09 04:21:17 +08:00
|
|
|
CPUsers.push_back(CPUser(&I, CPEMI, MaxOffs, NegOk, IsSoImm));
|
2007-02-10 04:54:44 +08:00
|
|
|
|
|
|
|
// Increment corresponding CPEntry reference count.
|
|
|
|
CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
|
|
|
|
assert(CPE && "Cannot find a corresponding CPEntry!");
|
|
|
|
CPE->RefCount++;
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2007-01-19 15:51:42 +08:00
|
|
|
// Instructions can only use one CP entry, don't bother scanning the
|
|
|
|
// rest of the operands.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// getOffsetOf - Return the current offset of the specified machine instruction
|
2007-01-19 15:51:42 +08:00
|
|
|
/// from the start of the function. This offset changes as stuff is moved
|
|
|
|
/// around inside the function.
|
2012-03-24 07:07:03 +08:00
|
|
|
unsigned ARMConstantIslands::getOffsetOf(MachineInstr *MI) const {
|
2007-01-19 15:51:42 +08:00
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2007-01-19 15:51:42 +08: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 09:08:25 +08:00
|
|
|
unsigned Offset = BBInfo[MBB->getNumber()].Offset;
|
2007-01-19 15:51:42 +08:00
|
|
|
|
|
|
|
// Sum instructions before MI in MBB.
|
2012-02-01 04:56:55 +08:00
|
|
|
for (MachineBasicBlock::iterator I = MBB->begin(); &*I != MI; ++I) {
|
2007-01-19 15:51:42 +08:00
|
|
|
assert(I != MBB->end() && "Didn't find MI in its own basic block?");
|
2016-07-29 00:32:22 +08:00
|
|
|
Offset += TII->getInstSizeInBytes(*I);
|
2007-01-19 15:51:42 +08:00
|
|
|
}
|
2012-02-01 04:56:55 +08:00
|
|
|
return Offset;
|
2007-01-19 15:51:42 +08: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-24 07:07:03 +08:00
|
|
|
/// updateForInsertedWaterBlock - When a block is newly inserted into the
|
2007-01-19 15:51:42 +08:00
|
|
|
/// machine function, it upsets all of the block numbers. Renumber the blocks
|
|
|
|
/// and update the arrays that parallel this numbering.
|
2012-03-24 07:07:03 +08:00
|
|
|
void ARMConstantIslands::updateForInsertedWaterBlock(MachineBasicBlock *NewBB) {
|
2011-02-15 17:23:02 +08:00
|
|
|
// Renumber the MBB's to keep them consecutive.
|
2007-01-19 15:51:42 +08:00
|
|
|
NewBB->getParent()->RenumberBlocks(NewBB);
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2011-12-07 09:08:25 +08:00
|
|
|
// Insert an entry into BBInfo to align it properly with the (newly
|
2007-01-19 15:51:42 +08:00
|
|
|
// renumbered) block numbers.
|
2011-12-07 09:08:25 +08:00
|
|
|
BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
|
2009-05-13 01:09:30 +08:00
|
|
|
|
|
|
|
// Next, update WaterList. Specifically, we need to add NewMBB as having
|
2007-01-19 15:51:42 +08:00
|
|
|
// available water after it.
|
2009-10-13 02:52:13 +08:00
|
|
|
water_iterator IP =
|
2007-01-19 15:51:42 +08: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-16 04:49:47 +08:00
|
|
|
/// an unconditional branch. Update data structures and renumber blocks to
|
2007-01-31 10:22:22 +08:00
|
|
|
/// account for this change and returns the newly created block.
|
2012-03-24 07:07:03 +08:00
|
|
|
MachineBasicBlock *ARMConstantIslands::splitBlockBeforeInstr(MachineInstr *MI) {
|
2007-01-19 15:51:42 +08:00
|
|
|
MachineBasicBlock *OrigBB = MI->getParent();
|
|
|
|
|
|
|
|
// Create a new MBB for the code after the OrigBB.
|
2009-05-13 01:09:30 +08:00
|
|
|
MachineBasicBlock *NewBB =
|
2011-12-13 02:16:53 +08:00
|
|
|
MF->CreateMachineBasicBlock(OrigBB->getBasicBlock());
|
2015-10-20 07:25:57 +08:00
|
|
|
MachineFunction::iterator MBBI = ++OrigBB->getIterator();
|
2011-12-13 02:16:53 +08:00
|
|
|
MF->insert(MBBI, NewBB);
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2007-01-19 15:51:42 +08:00
|
|
|
// Splice the instructions starting with MI over to NewBB.
|
|
|
|
NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end());
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2007-01-19 15:51:42 +08:00
|
|
|
// Add an unconditional branch from OrigBB to NewBB.
|
2007-02-01 02:29:27 +08:00
|
|
|
// Note the new unconditional branch is not being recorded.
|
2009-02-13 10:25:56 +08:00
|
|
|
// There doesn't seem to be meaningful DebugInfo available; this doesn't
|
|
|
|
// correspond to anything in the source.
|
2009-07-07 09:16:41 +08:00
|
|
|
unsigned Opc = isThumb ? (isThumb2 ? ARM::t2B : ARM::tB) : ARM::B;
|
2011-09-10 05:48:23 +08: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 23:08:57 +08:00
|
|
|
++NumSplit;
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2007-01-19 15:51:42 +08:00
|
|
|
// Update the CFG. All succs of OrigBB are now succs of NewBB.
|
2011-12-06 08:51:12 +08:00
|
|
|
NewBB->transferSuccessors(OrigBB);
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2007-01-19 15:51:42 +08:00
|
|
|
// OrigBB branches to NewBB.
|
|
|
|
OrigBB->addSuccessor(NewBB);
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2007-01-19 15:51:42 +08:00
|
|
|
// Update internal data structures to account for the newly inserted MBB.
|
2012-03-24 07:07:03 +08:00
|
|
|
// This is almost the same as updateForInsertedWaterBlock, except that
|
2007-02-23 13:02:36 +08:00
|
|
|
// the Water goes after OrigBB, not NewBB.
|
2011-12-13 02:16:53 +08:00
|
|
|
MF->RenumberBlocks(NewBB);
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2011-12-07 09:08:25 +08:00
|
|
|
// Insert an entry into BBInfo to align it properly with the (newly
|
2007-02-23 13:02:36 +08:00
|
|
|
// renumbered) block numbers.
|
2011-12-07 09:08:25 +08:00
|
|
|
BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
|
2007-02-25 08:47:03 +08:00
|
|
|
|
2009-05-13 01:09:30 +08:00
|
|
|
// Next, update WaterList. Specifically, we need to add OrigMBB as having
|
2007-02-23 13:02:36 +08: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-13 02:52:13 +08:00
|
|
|
water_iterator IP =
|
2007-02-23 13:02:36 +08:00
|
|
|
std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB,
|
|
|
|
CompareMBBNumbers);
|
|
|
|
MachineBasicBlock* WaterBB = *IP;
|
|
|
|
if (WaterBB == OrigBB)
|
2014-03-02 20:27:27 +08:00
|
|
|
WaterList.insert(std::next(IP), NewBB);
|
2007-02-23 13:02:36 +08:00
|
|
|
else
|
|
|
|
WaterList.insert(IP, OrigBB);
|
2009-10-16 04:49:47 +08:00
|
|
|
NewWaterList.insert(OrigBB);
|
2007-02-23 13:02:36 +08:00
|
|
|
|
2010-07-24 06:50:23 +08: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.)
|
2016-07-22 16:39:12 +08:00
|
|
|
computeBlockSize(MF, OrigBB, BBInfo[OrigBB->getNumber()]);
|
2007-02-25 08:47:03 +08:00
|
|
|
|
2010-07-24 06:50:23 +08:00
|
|
|
// Figure out how large the NewMBB is. As the second half of the original
|
|
|
|
// block, it may contain a tablejump.
|
2016-07-22 16:39:12 +08:00
|
|
|
computeBlockSize(MF, NewBB, BBInfo[NewBB->getNumber()]);
|
2010-07-24 06:50:23 +08:00
|
|
|
|
2007-02-25 08:47:03 +08:00
|
|
|
// All BBOffsets following these blocks must be modified.
|
2012-03-24 07:07:03 +08:00
|
|
|
adjustBBOffsetsAfter(OrigBB);
|
2007-01-31 10:22:22 +08:00
|
|
|
|
|
|
|
return NewBB;
|
2007-01-19 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// getUserOffset - Compute the offset of U.MI as seen by the hardware
|
2012-01-10 09:34:59 +08:00
|
|
|
/// displacement computation. Update U.KnownAlignment to match its current
|
|
|
|
/// basic block location.
|
2012-03-24 07:07:03 +08:00
|
|
|
unsigned ARMConstantIslands::getUserOffset(CPUser &U) const {
|
|
|
|
unsigned UserOffset = getOffsetOf(U.MI);
|
2012-01-10 09:34:59 +08: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-24 07:07:03 +08:00
|
|
|
/// isOffsetInRange - Checks whether UserOffset (the location of a constant pool
|
2009-05-13 01:09:30 +08:00
|
|
|
/// reference) is within MaxDisp of TrialOffset (a proposed location of a
|
2007-04-30 03:19:30 +08:00
|
|
|
/// constant pool entry).
|
2012-03-24 07:07:03 +08:00
|
|
|
/// UserOffset is computed by getUserOffset above to include PC adjustments. If
|
2012-01-10 09:34:59 +08:00
|
|
|
/// the mod 4 alignment of UserOffset is not known, the uncertainty must be
|
|
|
|
/// subtracted from MaxDisp instead. CPUser::getMaxDisp() does that.
|
2012-03-24 07:07:03 +08:00
|
|
|
bool ARMConstantIslands::isOffsetInRange(unsigned UserOffset,
|
2009-07-24 02:27:47 +08:00
|
|
|
unsigned TrialOffset, unsigned MaxDisp,
|
|
|
|
bool NegativeOK, bool IsSoImm) {
|
2007-02-25 08:47:03 +08:00
|
|
|
if (UserOffset <= TrialOffset) {
|
|
|
|
// User before the Trial.
|
2009-07-24 02:27:47 +08:00
|
|
|
if (TrialOffset - UserOffset <= MaxDisp)
|
|
|
|
return true;
|
2009-07-25 03:31:03 +08:00
|
|
|
// FIXME: Make use full range of soimm values.
|
2007-02-25 08:47:03 +08:00
|
|
|
} else if (NegativeOK) {
|
2009-07-24 02:27:47 +08:00
|
|
|
if (UserOffset - TrialOffset <= MaxDisp)
|
|
|
|
return true;
|
2009-07-25 03:31:03 +08:00
|
|
|
// FIXME: Make use full range of soimm values.
|
2007-02-25 08:47:03 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// isWaterInRange - Returns true if a CPE placed after the specified
|
2007-02-23 13:02:36 +08:00
|
|
|
/// Water (a basic block) will be in range for the specific MI.
|
2011-12-13 08:44:30 +08:00
|
|
|
///
|
|
|
|
/// Compute how much the function will grow by inserting a CPE after Water.
|
2012-03-24 07:07:03 +08:00
|
|
|
bool ARMConstantIslands::isWaterInRange(unsigned UserOffset,
|
2011-12-13 08:44:30 +08:00
|
|
|
MachineBasicBlock* Water, CPUser &U,
|
|
|
|
unsigned &Growth) {
|
|
|
|
unsigned CPELogAlign = getCPELogAlign(U.CPEMI);
|
|
|
|
unsigned CPEOffset = BBInfo[Water->getNumber()].postOffset(CPELogAlign);
|
|
|
|
unsigned NextBlockOffset, NextBlockAlignment;
|
2015-10-20 07:25:57 +08:00
|
|
|
MachineFunction::const_iterator NextBlock = Water->getIterator();
|
2011-12-13 08:44:30 +08:00
|
|
|
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.
|
2016-03-31 05:30:00 +08:00
|
|
|
Growth += OffsetToAlignment(CPEEnd, 1ULL << NextBlockAlignment);
|
2011-12-13 08:44:30 +08:00
|
|
|
|
|
|
|
// If the CPE is to be inserted before the instruction, that will raise
|
2012-03-24 07:07:03 +08:00
|
|
|
// the offset of the instruction. Also account for unknown alignment padding
|
2011-12-13 08:44:30 +08: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-03 04:31:06 +08:00
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
return isOffsetInRange(UserOffset, CPEOffset, U);
|
2007-02-23 13:02:36 +08:00
|
|
|
}
|
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// isCPEntryInRange - Returns true if the distance between specific MI and
|
2007-02-01 03:57:44 +08:00
|
|
|
/// specific ConstPool entry instruction can fit in MI's displacement field.
|
2012-03-24 07:07:03 +08:00
|
|
|
bool ARMConstantIslands::isCPEntryInRange(MachineInstr *MI, unsigned UserOffset,
|
2009-07-22 07:56:01 +08:00
|
|
|
MachineInstr *CPEMI, unsigned MaxDisp,
|
|
|
|
bool NegOk, bool DoDump) {
|
2012-03-24 07:07:03 +08:00
|
|
|
unsigned CPEOffset = getOffsetOf(CPEMI);
|
2007-02-01 09:09:47 +08:00
|
|
|
|
2007-02-23 13:02:36 +08:00
|
|
|
if (DoDump) {
|
2011-12-10 02:20:35 +08: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 10:55:06 +08:00
|
|
|
<< format(" insn address=%#x", UserOffset)
|
2011-12-10 02:20:35 +08:00
|
|
|
<< " in BB#" << Block << ": "
|
2011-12-10 10:55:06 +08:00
|
|
|
<< format("%#x-%x\t", BBI.Offset, BBI.postOffset()) << *MI
|
|
|
|
<< format("CPE address=%#x offset=%+d: ", CPEOffset,
|
|
|
|
int(CPEOffset-UserOffset));
|
2011-12-10 02:20:35 +08:00
|
|
|
});
|
2007-02-23 13:02:36 +08:00
|
|
|
}
|
2007-01-19 15:51:42 +08:00
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
return isOffsetInRange(UserOffset, CPEOffset, MaxDisp, NegOk);
|
2007-02-01 03:57:44 +08:00
|
|
|
}
|
|
|
|
|
2009-01-28 08:53:34 +08:00
|
|
|
#ifndef NDEBUG
|
2007-02-10 04:54:44 +08: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-07-01 02:04:13 +08:00
|
|
|
if (PredMI->getOpcode() == ARM::B || PredMI->getOpcode() == ARM::tB
|
|
|
|
|| PredMI->getOpcode() == ARM::t2B)
|
2007-02-10 04:54:44 +08:00
|
|
|
return PredMI->getOperand(0).getMBB() == Succ;
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-28 08:53:34 +08:00
|
|
|
#endif // NDEBUG
|
2007-02-10 04:54:44 +08:00
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
void ARMConstantIslands::adjustBBOffsetsAfter(MachineBasicBlock *BB) {
|
2012-01-07 05:40:15 +08:00
|
|
|
unsigned BBNum = BB->getNumber();
|
|
|
|
for(unsigned i = BBNum + 1, e = MF->getNumBlockIDs(); i < e; ++i) {
|
2011-12-08 08:55:02 +08:00
|
|
|
// Get the offset and known bits at the end of the layout predecessor.
|
2011-12-13 03:25:54 +08: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 08:55:02 +08:00
|
|
|
|
2012-01-07 05:40:15 +08: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 08:55:02 +08:00
|
|
|
BBInfo[i].Offset = Offset;
|
|
|
|
BBInfo[i].KnownBits = KnownBits;
|
2007-04-30 03:19:30 +08:00
|
|
|
}
|
2007-02-25 08:47:03 +08:00
|
|
|
}
|
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// decrementCPEReferenceCount - find the constant pool entry with index CPI
|
2007-02-23 13:02:36 +08:00
|
|
|
/// and instruction CPEMI, and decrement its refcount. If the refcount
|
2009-05-13 01:09:30 +08:00
|
|
|
/// becomes 0 remove the entry and instruction. Returns true if we removed
|
2007-02-23 13:02:36 +08:00
|
|
|
/// the entry, false if we didn't.
|
2007-02-01 18:16:15 +08:00
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
bool ARMConstantIslands::decrementCPEReferenceCount(unsigned CPI,
|
|
|
|
MachineInstr *CPEMI) {
|
2007-02-10 04:54:44 +08:00
|
|
|
// Find the old entry. Eliminate it if it is no longer used.
|
2007-04-04 07:39:48 +08:00
|
|
|
CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
|
|
|
|
assert(CPE && "Unexpected!");
|
|
|
|
if (--CPE->RefCount == 0) {
|
2012-03-24 07:07:03 +08:00
|
|
|
removeDeadCPEMI(CPEMI);
|
2014-04-25 13:30:21 +08:00
|
|
|
CPE->CPEMI = nullptr;
|
2010-06-22 23:08:57 +08:00
|
|
|
--NumCPEs;
|
2007-02-23 13:02:36 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-01 03:22:07 +08:00
|
|
|
unsigned ARMConstantIslands::getCombinedIndex(const MachineInstr *CPEMI) {
|
|
|
|
if (CPEMI->getOperand(1).isCPI())
|
|
|
|
return CPEMI->getOperand(1).getIndex();
|
|
|
|
|
|
|
|
return JumpTableEntryIndices[CPEMI->getOperand(1).getIndex()];
|
|
|
|
}
|
|
|
|
|
2007-02-23 13:02:36 +08:00
|
|
|
/// 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-24 07:07:03 +08:00
|
|
|
int ARMConstantIslands::findInRangeCPEntry(CPUser& U, unsigned UserOffset)
|
2007-02-23 13:02:36 +08:00
|
|
|
{
|
|
|
|
MachineInstr *UserMI = U.MI;
|
|
|
|
MachineInstr *CPEMI = U.CPEMI;
|
|
|
|
|
|
|
|
// Check to see if the CPE is already in-range.
|
2012-03-24 07:07:03 +08:00
|
|
|
if (isCPEntryInRange(UserMI, UserOffset, CPEMI, U.getMaxDisp(), U.NegOk,
|
|
|
|
true)) {
|
2011-12-10 02:20:35 +08:00
|
|
|
DEBUG(dbgs() << "In range\n");
|
2007-02-23 13:02:36 +08:00
|
|
|
return 1;
|
2007-02-10 04:54:44 +08:00
|
|
|
}
|
|
|
|
|
2007-02-23 13:02:36 +08:00
|
|
|
// No. Look for previously created clones of the CPE that are in range.
|
2015-06-01 03:22:07 +08:00
|
|
|
unsigned CPI = getCombinedIndex(CPEMI);
|
2007-02-23 13:02:36 +08: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 13:30:21 +08:00
|
|
|
if (CPEs[i].CPEMI == nullptr)
|
2007-02-23 13:02:36 +08:00
|
|
|
continue;
|
2012-03-24 07:07:03 +08:00
|
|
|
if (isCPEntryInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.getMaxDisp(),
|
2012-01-10 09:34:59 +08:00
|
|
|
U.NegOk)) {
|
2011-12-10 02:20:35 +08:00
|
|
|
DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#"
|
2009-08-23 14:49:22 +08:00
|
|
|
<< CPEs[i].CPI << "\n");
|
2007-02-23 13:02:36 +08: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 23:45:36 +08:00
|
|
|
if (UserMI->getOperand(j).isCPI()) {
|
2007-12-31 07:10:15 +08:00
|
|
|
UserMI->getOperand(j).setIndex(CPEs[i].CPI);
|
2007-02-23 13:02:36 +08: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-24 07:07:03 +08:00
|
|
|
return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1;
|
2007-02-23 13:02:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-01 02:41:23 +08:00
|
|
|
/// getUnconditionalBrDisp - Returns the maximum displacement that can fit in
|
|
|
|
/// the specific unconditional branch instruction.
|
|
|
|
static inline unsigned getUnconditionalBrDisp(int Opc) {
|
2009-07-01 02:04:13 +08:00
|
|
|
switch (Opc) {
|
|
|
|
case ARM::tB:
|
|
|
|
return ((1<<10)-1)*2;
|
|
|
|
case ARM::t2B:
|
|
|
|
return ((1<<23)-1)*2;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-08-11 23:33:49 +08:00
|
|
|
|
2009-07-01 02:04:13 +08:00
|
|
|
return ((1<<23)-1)*4;
|
2007-03-01 02:41:23 +08:00
|
|
|
}
|
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// findAvailableWater - Look for an existing entry in the WaterList in which
|
2007-03-01 07:20:38 +08:00
|
|
|
/// we can place the CPE referenced from U so it's within range of U's MI.
|
2009-10-16 04:49:47 +08: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.
llvm-svn: 83902
2009-10-13 05:23:15 +08: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-24 07:07:03 +08:00
|
|
|
bool ARMConstantIslands::findAvailableWater(CPUser &U, unsigned UserOffset,
|
2016-02-24 02:39:19 +08:00
|
|
|
water_iterator &WaterIter,
|
|
|
|
bool CloserWater) {
|
2009-10-13 03:04:03 +08:00
|
|
|
if (WaterList.empty())
|
|
|
|
return false;
|
|
|
|
|
2011-12-13 08:44:30 +08:00
|
|
|
unsigned BestGrowth = ~0u;
|
2016-02-24 02:39:19 +08:00
|
|
|
// The nearest water without splitting the UserBB is right after it.
|
|
|
|
// If the distance is still large (we have a big BB), then we need to split it
|
|
|
|
// if we don't converge after certain iterations. This helps the following
|
|
|
|
// situation to converge:
|
|
|
|
// BB0:
|
|
|
|
// Big BB
|
|
|
|
// BB1:
|
|
|
|
// Constant Pool
|
|
|
|
// When a CP access is out of range, BB0 may be used as water. However,
|
|
|
|
// inserting islands between BB0 and BB1 makes other accesses out of range.
|
|
|
|
MachineBasicBlock *UserBB = U.MI->getParent();
|
|
|
|
unsigned MinNoSplitDisp =
|
|
|
|
BBInfo[UserBB->getNumber()].postOffset(getCPELogAlign(U.CPEMI));
|
|
|
|
if (CloserWater && MinNoSplitDisp > U.getMaxDisp() / 2)
|
|
|
|
return false;
|
2014-03-02 20:27:27 +08:00
|
|
|
for (water_iterator IP = std::prev(WaterList.end()), B = WaterList.begin();;
|
2011-12-13 08:44:30 +08:00
|
|
|
--IP) {
|
2009-10-13 03:04:03 +08:00
|
|
|
MachineBasicBlock* WaterBB = *IP;
|
2009-10-16 04:49:47 +08: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.
|
2016-02-24 02:39:19 +08:00
|
|
|
// When CloserWater is true, we try to find the lowest address after (or
|
|
|
|
// equal to) user MI's BB no matter of padding growth.
|
2011-12-13 08:44:30 +08:00
|
|
|
unsigned Growth;
|
2012-03-24 07:07:03 +08:00
|
|
|
if (isWaterInRange(UserOffset, WaterBB, U, Growth) &&
|
2009-10-16 04:49:47 +08:00
|
|
|
(WaterBB->getNumber() < U.HighWaterMark->getNumber() ||
|
2014-11-14 01:58:53 +08:00
|
|
|
NewWaterList.count(WaterBB) || WaterBB == U.MI->getParent()) &&
|
|
|
|
Growth < BestGrowth) {
|
2011-12-13 08:44:30 +08:00
|
|
|
// 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');
|
|
|
|
|
2016-02-24 02:39:19 +08:00
|
|
|
if (CloserWater && WaterBB == U.MI->getParent())
|
|
|
|
return true;
|
|
|
|
// Keep looking unless it is perfect and we're not looking for the lowest
|
|
|
|
// possible address.
|
|
|
|
if (!CloserWater && BestGrowth == 0)
|
2009-10-13 03:04:03 +08:00
|
|
|
return true;
|
2007-02-23 13:02:36 +08:00
|
|
|
}
|
2009-10-13 03:04:03 +08:00
|
|
|
if (IP == B)
|
|
|
|
break;
|
2007-02-23 13:02:36 +08:00
|
|
|
}
|
2011-12-13 08:44:30 +08:00
|
|
|
return BestGrowth != ~0u;
|
2007-03-01 07:20:38 +08:00
|
|
|
}
|
2007-02-23 13:02:36 +08:00
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// createNewWater - No existing WaterList entry will work for
|
2007-03-01 07:20:38 +08: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-13 05:39:43 +08:00
|
|
|
/// unconditional branch around it. In either case NewMBB is set to a
|
2007-03-01 07:20:38 +08:00
|
|
|
/// block following which the new island can be inserted (the WaterList
|
|
|
|
/// is not adjusted).
|
2012-03-24 07:07:03 +08:00
|
|
|
void ARMConstantIslands::createNewWater(unsigned CPUserIndex,
|
2009-10-13 05:39:43 +08:00
|
|
|
unsigned UserOffset,
|
|
|
|
MachineBasicBlock *&NewMBB) {
|
2007-03-01 07:20:38 +08:00
|
|
|
CPUser &U = CPUsers[CPUserIndex];
|
|
|
|
MachineInstr *UserMI = U.MI;
|
|
|
|
MachineInstr *CPEMI = U.CPEMI;
|
2011-12-15 07:48:54 +08:00
|
|
|
unsigned CPELogAlign = getCPELogAlign(CPEMI);
|
2007-03-01 07:20:38 +08:00
|
|
|
MachineBasicBlock *UserMBB = UserMI->getParent();
|
2011-12-10 10:55:10 +08:00
|
|
|
const BasicBlockInfo &UserBBI = BBInfo[UserMBB->getNumber()];
|
2007-03-01 07:20:38 +08:00
|
|
|
|
2009-10-15 13:10:36 +08: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 09:34:59 +08:00
|
|
|
// Thumb2, 2 on Thumb1.
|
2011-12-15 07:48:54 +08:00
|
|
|
if (BBHasFallthrough(UserMBB)) {
|
|
|
|
// Size of branch to insert.
|
|
|
|
unsigned Delta = isThumb1 ? 2 : 4;
|
|
|
|
// Compute the offset where the CPE will begin.
|
2012-04-28 06:58:38 +08:00
|
|
|
unsigned CPEOffset = UserBBI.postOffset(CPELogAlign) + Delta;
|
2011-12-15 07:48:54 +08:00
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
if (isOffsetInRange(UserOffset, CPEOffset, U)) {
|
2011-12-15 07:48:54 +08:00
|
|
|
DEBUG(dbgs() << "Split at end of BB#" << UserMBB->getNumber()
|
|
|
|
<< format(", expected CPE offset %#x\n", CPEOffset));
|
2015-10-20 07:25:57 +08:00
|
|
|
NewMBB = &*++UserMBB->getIterator();
|
2011-12-15 07:48:54 +08: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));
|
2016-07-22 16:39:12 +08:00
|
|
|
computeBlockSize(MF, UserMBB, BBInfo[UserMBB->getNumber()]);
|
2012-03-24 07:07:03 +08:00
|
|
|
adjustBBOffsetsAfter(UserMBB);
|
2011-12-15 07:48:54 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-08-13 04:30:05 +08:00
|
|
|
|
2011-12-15 07:48:54 +08: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-28 06:58:38 +08: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-15 07:48:54 +08:00
|
|
|
unsigned LogAlign = MF->getAlignment();
|
|
|
|
assert(LogAlign >= CPELogAlign && "Over-aligned constant pool entry");
|
|
|
|
unsigned KnownBits = UserBBI.internalKnownBits();
|
|
|
|
unsigned UPad = UnknownPadding(LogAlign, KnownBits);
|
2012-04-28 06:58:38 +08:00
|
|
|
unsigned BaseInsertOffset = UserOffset + U.getMaxDisp() - UPad;
|
2011-12-15 07:48:54 +08: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-24 07:07:03 +08:00
|
|
|
// inside isOffsetInRange.
|
2011-12-15 07:48:54 +08: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 14:21:38 +08:00
|
|
|
if (BaseInsertOffset + 8 >= UserBBI.postOffset()) {
|
2014-10-17 09:31:47 +08:00
|
|
|
// Ensure BaseInsertOffset is larger than the offset of the instruction
|
|
|
|
// following UserMI so that the loop which searches for the split point
|
|
|
|
// iterates at least once.
|
|
|
|
BaseInsertOffset =
|
|
|
|
std::max(UserBBI.postOffset() - UPad - 8,
|
2016-07-29 00:32:22 +08:00
|
|
|
UserOffset + TII->getInstSizeInBytes(*UserMI) + 1);
|
2012-04-28 14:21:38 +08:00
|
|
|
DEBUG(dbgs() << format("Move inside block: %#x\n", BaseInsertOffset));
|
|
|
|
}
|
2012-04-28 06:58:38 +08:00
|
|
|
unsigned EndInsertOffset = BaseInsertOffset + 4 + UPad +
|
2011-12-15 07:48:54 +08:00
|
|
|
CPEMI->getOperand(2).getImm();
|
|
|
|
MachineBasicBlock::iterator MI = UserMI;
|
|
|
|
++MI;
|
|
|
|
unsigned CPUIndex = CPUserIndex+1;
|
|
|
|
unsigned NumCPUsers = CPUsers.size();
|
2014-04-25 13:30:21 +08:00
|
|
|
MachineInstr *LastIT = nullptr;
|
2016-07-29 00:32:22 +08:00
|
|
|
for (unsigned Offset = UserOffset + TII->getInstSizeInBytes(*UserMI);
|
2011-12-15 07:48:54 +08:00
|
|
|
Offset < BaseInsertOffset;
|
2016-07-29 00:32:22 +08:00
|
|
|
Offset += TII->getInstSizeInBytes(*MI), MI = std::next(MI)) {
|
2012-04-28 14:21:38 +08:00
|
|
|
assert(MI != UserMBB->end() && "Fell off end of block");
|
2016-07-09 04:21:17 +08:00
|
|
|
if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].MI == &*MI) {
|
2011-12-15 07:48:54 +08:00
|
|
|
CPUser &U = CPUsers[CPUIndex];
|
2012-03-24 07:07:03 +08:00
|
|
|
if (!isOffsetInRange(Offset, EndInsertOffset, U)) {
|
2011-12-15 07:48:54 +08: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-28 06:58:38 +08:00
|
|
|
EndInsertOffset += U.CPEMI->getOperand(2).getImm();
|
2011-12-15 07:48:54 +08:00
|
|
|
CPUIndex++;
|
2007-02-23 13:02:36 +08:00
|
|
|
}
|
2010-08-13 04:30:05 +08:00
|
|
|
|
2011-12-15 07:48:54 +08:00
|
|
|
// Remember the last IT instruction.
|
|
|
|
if (MI->getOpcode() == ARM::t2IT)
|
2016-07-09 04:21:17 +08:00
|
|
|
LastIT = &*MI;
|
2011-12-15 07:48:54 +08:00
|
|
|
}
|
2010-08-13 04:30:05 +08:00
|
|
|
|
2011-12-15 07:48:54 +08:00
|
|
|
--MI;
|
|
|
|
|
|
|
|
// Avoid splitting an IT block.
|
|
|
|
if (LastIT) {
|
|
|
|
unsigned PredReg = 0;
|
2016-02-23 10:46:52 +08:00
|
|
|
ARMCC::CondCodes CC = getITInstrPredicate(*MI, PredReg);
|
2011-12-15 07:48:54 +08:00
|
|
|
if (CC != ARMCC::AL)
|
|
|
|
MI = LastIT;
|
2007-03-01 07:20:38 +08:00
|
|
|
}
|
2014-11-14 01:58:53 +08:00
|
|
|
|
|
|
|
// We really must not split an IT block.
|
|
|
|
DEBUG(unsigned PredReg;
|
2016-02-23 10:46:52 +08:00
|
|
|
assert(!isThumb || getITInstrPredicate(*MI, PredReg) == ARMCC::AL));
|
2014-11-14 01:58:53 +08:00
|
|
|
|
2016-07-09 04:21:17 +08:00
|
|
|
NewMBB = splitBlockBeforeInstr(&*MI);
|
2007-03-01 07:20:38 +08:00
|
|
|
}
|
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// handleConstantPoolUser - Analyze the specified user, checking to see if it
|
2009-05-13 01:35:29 +08:00
|
|
|
/// is out-of-range. If so, pick up the constant pool value and move it some
|
2007-03-01 07:20:38 +08:00
|
|
|
/// place in-range. Return true if we changed any addresses (thus must run
|
|
|
|
/// another pass of branch lengthening), false otherwise.
|
2016-02-24 02:39:19 +08:00
|
|
|
bool ARMConstantIslands::handleConstantPoolUser(unsigned CPUserIndex,
|
|
|
|
bool CloserWater) {
|
2007-03-01 07:20:38 +08:00
|
|
|
CPUser &U = CPUsers[CPUserIndex];
|
|
|
|
MachineInstr *UserMI = U.MI;
|
|
|
|
MachineInstr *CPEMI = U.CPEMI;
|
2015-06-01 03:22:07 +08:00
|
|
|
unsigned CPI = getCombinedIndex(CPEMI);
|
2007-03-01 07:20:38 +08:00
|
|
|
unsigned Size = CPEMI->getOperand(2).getImm();
|
2012-01-10 09:34:59 +08:00
|
|
|
// Compute this only once, it's expensive.
|
2012-03-24 07:07:03 +08:00
|
|
|
unsigned UserOffset = getUserOffset(U);
|
2007-04-27 16:14:15 +08:00
|
|
|
|
2007-03-01 07:20:38 +08:00
|
|
|
// See if the current entry is within range, or there is a clone of it
|
|
|
|
// in range.
|
2012-03-24 07:07:03 +08:00
|
|
|
int result = findInRangeCPEntry(U, UserOffset);
|
2007-03-01 07:20:38 +08: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 16:03:18 +08:00
|
|
|
unsigned ID = AFI->createPICLabelUId();
|
2007-03-01 07:20:38 +08: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.
llvm-svn: 83902
2009-10-13 05:23:15 +08:00
|
|
|
// Look for water where we can place this CPE.
|
2011-12-13 02:16:53 +08:00
|
|
|
MachineBasicBlock *NewIsland = MF->CreateMachineBasicBlock();
|
2009-10-16 04:49:47 +08:00
|
|
|
MachineBasicBlock *NewMBB;
|
|
|
|
water_iterator IP;
|
2016-02-24 02:39:19 +08:00
|
|
|
if (findAvailableWater(U, UserOffset, IP, CloserWater)) {
|
2011-12-10 02:20:35 +08:00
|
|
|
DEBUG(dbgs() << "Found water in range\n");
|
2009-10-16 04:49:47 +08: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 23:37:57 +08:00
|
|
|
if (NewWaterList.erase(WaterBB))
|
2009-10-16 04:49:47 +08:00
|
|
|
NewWaterList.insert(NewIsland);
|
2012-08-22 23:37:57 +08:00
|
|
|
|
2009-10-16 04:49:47 +08:00
|
|
|
// The new CPE goes before the following block (NewMBB).
|
2015-10-20 07:25:57 +08:00
|
|
|
NewMBB = &*++WaterBB->getIterator();
|
2009-10-16 04:49:47 +08:00
|
|
|
} else {
|
2007-03-01 07:20:38 +08:00
|
|
|
// No water found.
|
2011-12-10 02:20:35 +08:00
|
|
|
DEBUG(dbgs() << "No water found\n");
|
2012-03-24 07:07:03 +08:00
|
|
|
createNewWater(CPUserIndex, UserOffset, NewMBB);
|
2009-10-16 04:49:47 +08:00
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
// splitBlockBeforeInstr adds to WaterList, which is important when it is
|
2009-10-16 04:49:47 +08: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.
|
2015-10-20 07:25:57 +08:00
|
|
|
MachineBasicBlock *WaterBB = &*--NewMBB->getIterator();
|
2016-08-12 06:21:41 +08:00
|
|
|
IP = find(WaterList, WaterBB);
|
2009-10-16 04:49:47 +08:00
|
|
|
if (IP != WaterList.end())
|
|
|
|
NewWaterList.erase(WaterBB);
|
|
|
|
|
|
|
|
// We are adding new water. Update NewWaterList.
|
|
|
|
NewWaterList.insert(NewIsland);
|
2007-02-23 13:02:36 +08:00
|
|
|
}
|
|
|
|
|
2009-10-16 04:49:47 +08: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 13:02:36 +08:00
|
|
|
// Okay, we know we can put an island before NewMBB now, do it!
|
2015-10-20 07:25:57 +08:00
|
|
|
MF->insert(NewMBB->getIterator(), NewIsland);
|
2007-02-23 13:02:36 +08:00
|
|
|
|
|
|
|
// Update internal data structures to account for the newly inserted MBB.
|
2012-03-24 07:07:03 +08:00
|
|
|
updateForInsertedWaterBlock(NewIsland);
|
2007-02-23 13:02:36 +08: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 13:52:29 +08:00
|
|
|
U.HighWaterMark = NewIsland;
|
2015-06-01 03:22:07 +08:00
|
|
|
U.CPEMI = BuildMI(NewIsland, DebugLoc(), CPEMI->getDesc())
|
|
|
|
.addImm(ID).addOperand(CPEMI->getOperand(1)).addImm(Size);
|
2007-02-23 13:02:36 +08:00
|
|
|
CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1));
|
2010-06-22 23:08:57 +08:00
|
|
|
++NumCPEs;
|
2007-02-10 04:54:44 +08:00
|
|
|
|
2015-06-01 03:22:07 +08:00
|
|
|
// Decrement the old entry, and remove it if refcount becomes 0.
|
|
|
|
decrementCPEReferenceCount(CPI, CPEMI);
|
|
|
|
|
2011-12-13 02:45:45 +08:00
|
|
|
// Mark the basic block as aligned as required by the const-pool entry.
|
|
|
|
NewIsland->setAlignment(getCPELogAlign(U.CPEMI));
|
2011-12-06 09:43:02 +08:00
|
|
|
|
2007-01-19 15:51:42 +08:00
|
|
|
// Increase the size of the island block to account for the new entry.
|
2011-12-07 09:08:25 +08:00
|
|
|
BBInfo[NewIsland->getNumber()].Size += Size;
|
2015-10-20 07:25:57 +08:00
|
|
|
adjustBBOffsetsAfter(&*--NewIsland->getIterator());
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2007-01-19 15:51:42 +08: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 23:45:36 +08:00
|
|
|
if (UserMI->getOperand(i).isCPI()) {
|
2007-12-31 07:10:15 +08:00
|
|
|
UserMI->getOperand(i).setIndex(ID);
|
2007-01-19 15:51:42 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2011-12-10 02:20:35 +08:00
|
|
|
DEBUG(dbgs() << " Moved CPE to #" << ID << " CPI=" << CPI
|
2011-12-10 10:55:06 +08:00
|
|
|
<< format(" offset=%#x\n", BBInfo[NewIsland->getNumber()].Offset));
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2007-01-19 15:51:42 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// removeDeadCPEMI - Remove a dead constant pool entry instruction. Update
|
2007-04-04 07:39:48 +08:00
|
|
|
/// sizes and offsets of impacted basic blocks.
|
2012-03-24 07:07:03 +08:00
|
|
|
void ARMConstantIslands::removeDeadCPEMI(MachineInstr *CPEMI) {
|
2007-04-04 07:39:48 +08:00
|
|
|
MachineBasicBlock *CPEBB = CPEMI->getParent();
|
2007-04-30 03:19:30 +08:00
|
|
|
unsigned Size = CPEMI->getOperand(2).getImm();
|
|
|
|
CPEMI->eraseFromParent();
|
2011-12-07 09:08:25 +08:00
|
|
|
BBInfo[CPEBB->getNumber()].Size -= Size;
|
2007-04-30 03:19:30 +08:00
|
|
|
// All succeeding offsets have the current size value added in, fix this.
|
2007-04-04 07:39:48 +08:00
|
|
|
if (CPEBB->empty()) {
|
2011-12-13 02:45:45 +08:00
|
|
|
BBInfo[CPEBB->getNumber()].Size = 0;
|
2011-12-07 05:55:35 +08:00
|
|
|
|
2013-02-22 02:37:54 +08:00
|
|
|
// This block no longer needs to be aligned.
|
2011-12-07 05:55:35 +08:00
|
|
|
CPEBB->setAlignment(0);
|
2011-12-13 02:45:45 +08:00
|
|
|
} else
|
|
|
|
// Entries are sorted by descending alignment, so realign from the front.
|
2016-07-09 04:21:17 +08:00
|
|
|
CPEBB->setAlignment(getCPELogAlign(&*CPEBB->begin()));
|
2011-12-13 02:45:45 +08:00
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
adjustBBOffsetsAfter(CPEBB);
|
2007-04-30 03:19:30 +08: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-04 07:39:48 +08:00
|
|
|
}
|
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// removeUnusedCPEntries - Remove constant pool entries whose refcounts
|
2007-04-04 07:39:48 +08:00
|
|
|
/// are zero.
|
2012-03-24 07:07:03 +08:00
|
|
|
bool ARMConstantIslands::removeUnusedCPEntries() {
|
2007-04-04 07:39:48 +08: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-24 07:07:03 +08:00
|
|
|
removeDeadCPEMI(CPEs[j].CPEMI);
|
2014-04-25 13:30:21 +08:00
|
|
|
CPEs[j].CPEMI = nullptr;
|
2007-04-04 07:39:48 +08:00
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
}
|
2009-05-13 01:09:30 +08:00
|
|
|
}
|
2007-04-04 07:39:48 +08:00
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// isBBInRange - Returns true if the distance between specific MI and
|
2007-01-27 04:38:26 +08:00
|
|
|
/// specific BB can fit in MI's displacement field.
|
2012-03-24 07:07:03 +08:00
|
|
|
bool ARMConstantIslands::isBBInRange(MachineInstr *MI,MachineBasicBlock *DestBB,
|
2007-02-01 03:57:44 +08:00
|
|
|
unsigned MaxDisp) {
|
2007-03-01 07:20:38 +08:00
|
|
|
unsigned PCAdj = isThumb ? 4 : 8;
|
2012-03-24 07:07:03 +08:00
|
|
|
unsigned BrOffset = getOffsetOf(MI) + PCAdj;
|
2011-12-07 09:08:25 +08:00
|
|
|
unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
|
2007-01-27 04:38:26 +08:00
|
|
|
|
2011-12-10 02:20:35 +08:00
|
|
|
DEBUG(dbgs() << "Branch of destination BB#" << DestBB->getNumber()
|
2009-08-23 11:41:05 +08:00
|
|
|
<< " from BB#" << MI->getParent()->getNumber()
|
|
|
|
<< " max delta=" << MaxDisp
|
2012-03-24 07:07:03 +08:00
|
|
|
<< " from " << getOffsetOf(MI) << " to " << DestOffset
|
2009-08-23 11:41:05 +08:00
|
|
|
<< " offset " << int(DestOffset-BrOffset) << "\t" << *MI);
|
2007-02-01 03:57:44 +08:00
|
|
|
|
2007-04-30 03:19:30 +08: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-27 04:38:26 +08:00
|
|
|
}
|
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// fixupImmediateBr - Fix up an immediate branch whose destination is too far
|
2007-01-30 09:18:38 +08:00
|
|
|
/// away to fit in its displacement field.
|
2012-03-24 07:07:03 +08:00
|
|
|
bool ARMConstantIslands::fixupImmediateBr(ImmBranch &Br) {
|
2007-01-30 09:18:38 +08:00
|
|
|
MachineInstr *MI = Br.MI;
|
2007-12-31 07:10:15 +08:00
|
|
|
MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
|
2007-01-30 09:18:38 +08:00
|
|
|
|
2007-02-01 03:57:44 +08:00
|
|
|
// Check to see if the DestBB is already in-range.
|
2012-03-24 07:07:03 +08:00
|
|
|
if (isBBInRange(MI, DestBB, Br.MaxDisp))
|
2007-01-30 09:18:38 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!Br.isCond)
|
2012-03-24 07:07:03 +08:00
|
|
|
return fixupUnconditionalBr(Br);
|
|
|
|
return fixupConditionalBr(Br);
|
2007-01-30 09:18:38 +08:00
|
|
|
}
|
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// fixupUnconditionalBr - Fix up an unconditional branch whose destination is
|
2007-02-23 13:02:36 +08:00
|
|
|
/// too far away to fit in its displacement field. If the LR register has been
|
2007-01-30 09:18:38 +08:00
|
|
|
/// spilled in the epilogue, then we can use BL to implement a far jump.
|
2009-05-13 01:35:29 +08:00
|
|
|
/// Otherwise, add an intermediate branch instruction to a branch.
|
2007-01-30 09:18:38 +08:00
|
|
|
bool
|
2012-03-24 07:07:03 +08:00
|
|
|
ARMConstantIslands::fixupUnconditionalBr(ImmBranch &Br) {
|
2007-01-30 09:18:38 +08:00
|
|
|
MachineInstr *MI = Br.MI;
|
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
2009-08-07 13:45:07 +08:00
|
|
|
if (!isThumb1)
|
2012-03-24 07:07:03 +08:00
|
|
|
llvm_unreachable("fixupUnconditionalBr is Thumb1 only!");
|
2007-01-30 09:18:38 +08:00
|
|
|
|
|
|
|
// Use BL to implement far jump.
|
|
|
|
Br.MaxDisp = (1 << 21) * 2;
|
2008-01-12 02:10:50 +08:00
|
|
|
MI->setDesc(TII->get(ARM::tBfar));
|
2011-12-07 09:08:25 +08:00
|
|
|
BBInfo[MBB->getNumber()].Size += 2;
|
2012-03-24 07:07:03 +08:00
|
|
|
adjustBBOffsetsAfter(MBB);
|
2007-01-30 09:18:38 +08:00
|
|
|
HasFarJump = true;
|
2010-06-22 23:08:57 +08:00
|
|
|
++NumUBrFixed;
|
2007-02-03 10:08:34 +08:00
|
|
|
|
2011-12-10 02:20:35 +08:00
|
|
|
DEBUG(dbgs() << " Changed B to long jump " << *MI);
|
2007-02-03 10:08:34 +08:00
|
|
|
|
2007-01-30 09:18:38 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// fixupConditionalBr - Fix up a conditional branch whose destination is too
|
2007-01-30 09:18:38 +08: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 11:12:46 +08:00
|
|
|
bool
|
2012-03-24 07:07:03 +08:00
|
|
|
ARMConstantIslands::fixupConditionalBr(ImmBranch &Br) {
|
2007-01-25 11:12:46 +08:00
|
|
|
MachineInstr *MI = Br.MI;
|
2007-12-31 07:10:15 +08:00
|
|
|
MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
|
2007-01-25 11:12:46 +08:00
|
|
|
|
2009-05-13 01:35:29 +08:00
|
|
|
// Add an unconditional branch to the destination and invert the branch
|
2007-01-30 09:18:38 +08:00
|
|
|
// condition to jump over it:
|
2007-01-25 11:12:46 +08:00
|
|
|
// blt L1
|
|
|
|
// =>
|
|
|
|
// bge L2
|
|
|
|
// b L1
|
|
|
|
// L2:
|
2007-12-31 04:49:49 +08:00
|
|
|
ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImm();
|
2007-01-25 11:12:46 +08:00
|
|
|
CC = ARMCC::getOppositeCondition(CC);
|
2007-07-05 15:18:20 +08:00
|
|
|
unsigned CCReg = MI->getOperand(2).getReg();
|
2007-01-25 11:12:46 +08: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 10:08:34 +08:00
|
|
|
MachineInstr *BMI = &MBB->back();
|
|
|
|
bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB);
|
2007-01-27 04:38:26 +08:00
|
|
|
|
2010-06-22 23:08:57 +08:00
|
|
|
++NumCBrFixed;
|
2007-02-03 10:08:34 +08:00
|
|
|
if (BMI != MI) {
|
2014-03-02 20:27:27 +08:00
|
|
|
if (std::next(MachineBasicBlock::iterator(MI)) == std::prev(MBB->end()) &&
|
2007-02-03 10:08:34 +08:00
|
|
|
BMI->getOpcode() == Br.UncondBr) {
|
2009-05-13 01:35:29 +08:00
|
|
|
// Last MI in the BB is an unconditional branch. Can we simply invert the
|
2007-01-27 04:38:26 +08:00
|
|
|
// condition and swap destinations:
|
|
|
|
// beq L1
|
|
|
|
// b L2
|
|
|
|
// =>
|
|
|
|
// bne L2
|
|
|
|
// b L1
|
2007-12-31 07:10:15 +08:00
|
|
|
MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB();
|
2012-03-24 07:07:03 +08:00
|
|
|
if (isBBInRange(MI, NewDest, Br.MaxDisp)) {
|
2011-12-10 02:20:35 +08:00
|
|
|
DEBUG(dbgs() << " Invert Bcc condition and swap its destination with "
|
2009-08-23 11:41:05 +08:00
|
|
|
<< *BMI);
|
2007-12-31 07:10:15 +08:00
|
|
|
BMI->getOperand(0).setMBB(DestBB);
|
|
|
|
MI->getOperand(0).setMBB(NewDest);
|
2007-01-27 04:38:26 +08:00
|
|
|
MI->getOperand(1).setImm(CC);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NeedSplit) {
|
2012-03-24 07:07:03 +08:00
|
|
|
splitBlockBeforeInstr(MI);
|
2009-05-13 01:35:29 +08:00
|
|
|
// No need for the branch to the next block. We're adding an unconditional
|
2007-01-26 10:02:39 +08:00
|
|
|
// branch to the destination.
|
2016-07-29 00:32:22 +08:00
|
|
|
int delta = TII->getInstSizeInBytes(MBB->back());
|
2011-12-07 09:08:25 +08:00
|
|
|
BBInfo[MBB->getNumber()].Size -= delta;
|
2007-01-26 10:02:39 +08:00
|
|
|
MBB->back().eraseFromParent();
|
2011-12-07 09:08:25 +08:00
|
|
|
// BBInfo[SplitBB].Offset is wrong temporarily, fixed below
|
2007-01-26 10:02:39 +08:00
|
|
|
}
|
2015-10-20 07:25:57 +08:00
|
|
|
MachineBasicBlock *NextBB = &*++MBB->getIterator();
|
2009-05-13 01:09:30 +08:00
|
|
|
|
2011-12-10 02:20:35 +08:00
|
|
|
DEBUG(dbgs() << " Insert B to BB#" << DestBB->getNumber()
|
2009-08-23 14:49:22 +08:00
|
|
|
<< " also invert condition and change dest. to BB#"
|
|
|
|
<< NextBB->getNumber() << "\n");
|
2007-01-25 11:12:46 +08:00
|
|
|
|
2007-04-24 04:09:04 +08:00
|
|
|
// Insert a new conditional branch and a new unconditional branch.
|
2007-01-25 11:12:46 +08:00
|
|
|
// Also update the ImmBranch as well as adding a new entry for the new branch.
|
2010-04-03 04:16:16 +08:00
|
|
|
BuildMI(MBB, DebugLoc(), TII->get(MI->getOpcode()))
|
2009-02-13 10:25:56 +08:00
|
|
|
.addMBB(NextBB).addImm(CC).addReg(CCReg);
|
2007-01-25 11:12:46 +08:00
|
|
|
Br.MI = &MBB->back();
|
2016-07-29 00:32:22 +08:00
|
|
|
BBInfo[MBB->getNumber()].Size += TII->getInstSizeInBytes(MBB->back());
|
2011-09-10 07:05:14 +08: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);
|
2016-07-29 00:32:22 +08:00
|
|
|
BBInfo[MBB->getNumber()].Size += TII->getInstSizeInBytes(MBB->back());
|
2007-02-01 02:29:27 +08:00
|
|
|
unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr);
|
2007-01-26 07:31:04 +08:00
|
|
|
ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr));
|
2007-04-24 04:09:04 +08:00
|
|
|
|
|
|
|
// Remove the old conditional branch. It may or may not still be in MBB.
|
2016-07-29 00:32:22 +08:00
|
|
|
BBInfo[MI->getParent()->getNumber()].Size -= TII->getInstSizeInBytes(*MI);
|
2007-01-25 11:12:46 +08:00
|
|
|
MI->eraseFromParent();
|
2012-03-24 07:07:03 +08:00
|
|
|
adjustBBOffsetsAfter(MBB);
|
2007-01-25 11:12:46 +08:00
|
|
|
return true;
|
|
|
|
}
|
2007-01-30 09:18:38 +08:00
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// undoLRSpillRestore - Remove Thumb push / pop instructions that only spills
|
2009-08-12 05:11:32 +08: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-24 07:07:03 +08:00
|
|
|
bool ARMConstantIslands::undoLRSpillRestore() {
|
2007-01-30 09:18:38 +08:00
|
|
|
bool MadeChange = false;
|
|
|
|
for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) {
|
|
|
|
MachineInstr *MI = PushPopMIs[i];
|
2010-03-13 09:08:20 +08:00
|
|
|
// First two operands are predicates.
|
2007-05-15 09:29:07 +08:00
|
|
|
if (MI->getOpcode() == ARM::tPOP_RET &&
|
2010-03-13 09:08:20 +08:00
|
|
|
MI->getOperand(2).getReg() == ARM::PC &&
|
|
|
|
MI->getNumExplicitOperands() == 3) {
|
2011-07-09 05:50:04 +08: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 09:29:07 +08:00
|
|
|
MI->eraseFromParent();
|
|
|
|
MadeChange = true;
|
2007-01-30 09:18:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return MadeChange;
|
|
|
|
}
|
2009-07-29 10:18:14 +08:00
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
bool ARMConstantIslands::optimizeThumb2Instructions() {
|
2009-08-14 08:32:16 +08: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-14 06:51:08 +08:00
|
|
|
case ARM::t2LEApcrel:
|
2009-08-14 08:32:16 +08: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-24 07:07:03 +08:00
|
|
|
unsigned UserOffset = getUserOffset(U);
|
2009-08-14 08:32:16 +08:00
|
|
|
unsigned MaxOffs = ((1 << Bits) - 1) * Scale;
|
2012-01-10 09:34:59 +08:00
|
|
|
|
|
|
|
// Be conservative with inline asm.
|
|
|
|
if (!U.KnownAlignment)
|
|
|
|
MaxOffs -= 2;
|
|
|
|
|
2009-08-14 08:32:16 +08:00
|
|
|
// FIXME: Check if offset is multiple of scale if scale is not 4.
|
2012-03-24 07:07:03 +08:00
|
|
|
if (isCPEntryInRange(U.MI, UserOffset, U.CPEMI, MaxOffs, false, true)) {
|
2012-03-31 08:06:42 +08:00
|
|
|
DEBUG(dbgs() << "Shrink: " << *U.MI);
|
2009-08-14 08:32:16 +08:00
|
|
|
U.MI->setDesc(TII->get(NewOpc));
|
|
|
|
MachineBasicBlock *MBB = U.MI->getParent();
|
2011-12-07 09:08:25 +08:00
|
|
|
BBInfo[MBB->getNumber()].Size -= 2;
|
2012-03-24 07:07:03 +08:00
|
|
|
adjustBBOffsetsAfter(MBB);
|
2009-08-14 08:32:16 +08:00
|
|
|
++NumT2CPShrunk;
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
bool ARMConstantIslands::optimizeThumb2Branches() {
|
2009-08-15 02:31:44 +08:00
|
|
|
bool MadeChange = false;
|
|
|
|
|
2015-04-24 04:31:35 +08:00
|
|
|
// The order in which branches appear in ImmBranches is approximately their
|
|
|
|
// order within the function body. By visiting later branches first, we reduce
|
|
|
|
// the distance between earlier forward branches and their targets, making it
|
|
|
|
// more likely that the cbn?z optimization, which can only apply to forward
|
|
|
|
// branches, will succeed.
|
|
|
|
for (unsigned i = ImmBranches.size(); i != 0; --i) {
|
|
|
|
ImmBranch &Br = ImmBranches[i-1];
|
2009-08-15 02:31:44 +08:00
|
|
|
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-11-01 07:46:45 +08:00
|
|
|
case ARM::t2Bcc: {
|
2009-08-15 02:31:44 +08:00
|
|
|
NewOpc = ARM::tBcc;
|
|
|
|
Bits = 8;
|
2009-11-01 07:46:45 +08:00
|
|
|
Scale = 2;
|
2009-08-15 02:31:44 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-11-01 07:46:45 +08:00
|
|
|
}
|
|
|
|
if (NewOpc) {
|
|
|
|
unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
|
|
|
|
MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB();
|
2012-03-24 07:07:03 +08:00
|
|
|
if (isBBInRange(Br.MI, DestBB, MaxOffs)) {
|
2012-03-31 08:06:42 +08:00
|
|
|
DEBUG(dbgs() << "Shrink branch: " << *Br.MI);
|
2009-11-01 07:46:45 +08:00
|
|
|
Br.MI->setDesc(TII->get(NewOpc));
|
|
|
|
MachineBasicBlock *MBB = Br.MI->getParent();
|
2011-12-07 09:08:25 +08:00
|
|
|
BBInfo[MBB->getNumber()].Size -= 2;
|
2012-03-24 07:07:03 +08:00
|
|
|
adjustBBOffsetsAfter(MBB);
|
2009-11-01 07:46:45 +08:00
|
|
|
++NumT2BrShrunk;
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Opcode = Br.MI->getOpcode();
|
|
|
|
if (Opcode != ARM::tBcc)
|
2009-08-15 02:31:44 +08:00
|
|
|
continue;
|
|
|
|
|
2012-01-14 09:53:46 +08: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-11-01 07:46:45 +08:00
|
|
|
NewOpc = 0;
|
|
|
|
unsigned PredReg = 0;
|
2016-02-23 10:46:52 +08:00
|
|
|
ARMCC::CondCodes Pred = getInstrPredicate(*Br.MI, PredReg);
|
2009-11-01 07:46:45 +08:00
|
|
|
if (Pred == ARMCC::EQ)
|
|
|
|
NewOpc = ARM::tCBZ;
|
|
|
|
else if (Pred == ARMCC::NE)
|
|
|
|
NewOpc = ARM::tCBNZ;
|
|
|
|
if (!NewOpc)
|
|
|
|
continue;
|
2009-08-15 02:31:44 +08:00
|
|
|
MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB();
|
2009-11-01 07:46:45 +08:00
|
|
|
// Check if the distance is within 126. Subtract starting offset by 2
|
|
|
|
// because the cmp will be eliminated.
|
2012-03-24 07:07:03 +08:00
|
|
|
unsigned BrOffset = getOffsetOf(Br.MI) + 4 - 2;
|
2011-12-07 09:08:25 +08:00
|
|
|
unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
|
2009-11-01 07:46:45 +08:00
|
|
|
if (BrOffset < DestOffset && (DestOffset - BrOffset) <= 126) {
|
2011-04-02 06:09:28 +08:00
|
|
|
MachineBasicBlock::iterator CmpMI = Br.MI;
|
|
|
|
if (CmpMI != Br.MI->getParent()->begin()) {
|
|
|
|
--CmpMI;
|
|
|
|
if (CmpMI->getOpcode() == ARM::tCMPi8) {
|
|
|
|
unsigned Reg = CmpMI->getOperand(0).getReg();
|
2016-02-23 10:46:52 +08:00
|
|
|
Pred = getInstrPredicate(*CmpMI, PredReg);
|
2011-04-02 06:09:28 +08:00
|
|
|
if (Pred == ARMCC::AL &&
|
|
|
|
CmpMI->getOperand(1).getImm() == 0 &&
|
|
|
|
isARMLowRegister(Reg)) {
|
|
|
|
MachineBasicBlock *MBB = Br.MI->getParent();
|
2012-03-31 08:06:42 +08:00
|
|
|
DEBUG(dbgs() << "Fold: " << *CmpMI << " and: " << *Br.MI);
|
2011-04-02 06:09:28 +08: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 09:08:25 +08:00
|
|
|
BBInfo[MBB->getNumber()].Size -= 2;
|
2012-03-24 07:07:03 +08:00
|
|
|
adjustBBOffsetsAfter(MBB);
|
2011-04-02 06:09:28 +08:00
|
|
|
++NumCBZ;
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
2009-11-01 07:46:45 +08:00
|
|
|
}
|
|
|
|
}
|
2009-08-15 02:31:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
2009-08-14 08:32:16 +08:00
|
|
|
}
|
|
|
|
|
2015-06-01 03:22:07 +08:00
|
|
|
static bool isSimpleIndexCalc(MachineInstr &I, unsigned EntryReg,
|
|
|
|
unsigned BaseReg) {
|
|
|
|
if (I.getOpcode() != ARM::t2ADDrs)
|
|
|
|
return false;
|
2015-05-14 04:28:32 +08:00
|
|
|
|
2015-06-01 03:22:07 +08:00
|
|
|
if (I.getOperand(0).getReg() != EntryReg)
|
|
|
|
return false;
|
2015-05-14 04:28:32 +08:00
|
|
|
|
2015-06-01 03:22:07 +08:00
|
|
|
if (I.getOperand(1).getReg() != BaseReg)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: what about CC and IdxReg?
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief While trying to form a TBB/TBH instruction, we may (if the table
|
|
|
|
/// doesn't immediately follow the BR_JT) need access to the start of the
|
|
|
|
/// jump-table. We know one instruction that produces such a register; this
|
|
|
|
/// function works out whether that definition can be preserved to the BR_JT,
|
|
|
|
/// possibly by removing an intervening addition (which is usually needed to
|
|
|
|
/// calculate the actual entry to jump to).
|
|
|
|
bool ARMConstantIslands::preserveBaseRegister(MachineInstr *JumpMI,
|
|
|
|
MachineInstr *LEAMI,
|
|
|
|
unsigned &DeadSize,
|
|
|
|
bool &CanDeleteLEA,
|
|
|
|
bool &BaseRegKill) {
|
|
|
|
if (JumpMI->getParent() != LEAMI->getParent())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Now we hope that we have at least these instructions in the basic block:
|
|
|
|
// BaseReg = t2LEA ...
|
|
|
|
// [...]
|
|
|
|
// EntryReg = t2ADDrs BaseReg, ...
|
|
|
|
// [...]
|
|
|
|
// t2BR_JT EntryReg
|
|
|
|
//
|
|
|
|
// We have to be very conservative about what we recognise here though. The
|
|
|
|
// main perturbing factors to watch out for are:
|
|
|
|
// + Spills at any point in the chain: not direct problems but we would
|
|
|
|
// expect a blocking Def of the spilled register so in practice what we
|
|
|
|
// can do is limited.
|
|
|
|
// + EntryReg == BaseReg: this is the one situation we should allow a Def
|
|
|
|
// of BaseReg, but only if the t2ADDrs can be removed.
|
|
|
|
// + Some instruction other than t2ADDrs computing the entry. Not seen in
|
|
|
|
// the wild, but we should be careful.
|
|
|
|
unsigned EntryReg = JumpMI->getOperand(0).getReg();
|
|
|
|
unsigned BaseReg = LEAMI->getOperand(0).getReg();
|
|
|
|
|
|
|
|
CanDeleteLEA = true;
|
|
|
|
BaseRegKill = false;
|
|
|
|
MachineInstr *RemovableAdd = nullptr;
|
|
|
|
MachineBasicBlock::iterator I(LEAMI);
|
|
|
|
for (++I; &*I != JumpMI; ++I) {
|
|
|
|
if (isSimpleIndexCalc(*I, EntryReg, BaseReg)) {
|
|
|
|
RemovableAdd = &*I;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned K = 0, E = I->getNumOperands(); K != E; ++K) {
|
|
|
|
const MachineOperand &MO = I->getOperand(K);
|
|
|
|
if (!MO.isReg() || !MO.getReg())
|
|
|
|
continue;
|
|
|
|
if (MO.isDef() && MO.getReg() == BaseReg)
|
|
|
|
return false;
|
|
|
|
if (MO.isUse() && MO.getReg() == BaseReg) {
|
|
|
|
BaseRegKill = BaseRegKill || MO.isKill();
|
|
|
|
CanDeleteLEA = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!RemovableAdd)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Check the add really is removable, and that nothing else in the block
|
|
|
|
// clobbers BaseReg.
|
|
|
|
for (++I; &*I != JumpMI; ++I) {
|
|
|
|
for (unsigned K = 0, E = I->getNumOperands(); K != E; ++K) {
|
|
|
|
const MachineOperand &MO = I->getOperand(K);
|
|
|
|
if (!MO.isReg() || !MO.getReg())
|
|
|
|
continue;
|
|
|
|
if (MO.isDef() && MO.getReg() == BaseReg)
|
|
|
|
return false;
|
|
|
|
if (MO.isUse() && MO.getReg() == EntryReg)
|
|
|
|
RemovableAdd = nullptr;
|
2015-05-14 04:28:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 03:22:07 +08:00
|
|
|
if (RemovableAdd) {
|
|
|
|
RemovableAdd->eraseFromParent();
|
|
|
|
DeadSize += 4;
|
|
|
|
} else if (BaseReg == EntryReg) {
|
|
|
|
// The add wasn't removable, but clobbered the base for the TBB. So we can't
|
|
|
|
// preserve it.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We reached the end of the block without seeing another definition of
|
|
|
|
// BaseReg (except, possibly the t2ADDrs, which was removed). BaseReg can be
|
|
|
|
// used in the TBB/TBH if necessary.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns whether CPEMI is the first instruction in the block
|
|
|
|
/// immediately following JTMI (assumed to be a TBB or TBH terminator). If so,
|
|
|
|
/// we can switch the first register to PC and usually remove the address
|
2015-08-09 02:27:36 +08:00
|
|
|
/// calculation that preceded it.
|
2015-06-01 03:22:07 +08:00
|
|
|
static bool jumpTableFollowsTB(MachineInstr *JTMI, MachineInstr *CPEMI) {
|
2015-10-20 07:25:57 +08:00
|
|
|
MachineFunction::iterator MBB = JTMI->getParent()->getIterator();
|
2015-06-01 03:22:07 +08:00
|
|
|
MachineFunction *MF = MBB->getParent();
|
|
|
|
++MBB;
|
|
|
|
|
|
|
|
return MBB != MF->end() && MBB->begin() != MBB->end() &&
|
|
|
|
&*MBB->begin() == CPEMI;
|
2015-05-14 04:28:32 +08:00
|
|
|
}
|
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// optimizeThumb2JumpTables - Use tbb / tbh instructions to generate smaller
|
2009-08-14 08:32:16 +08:00
|
|
|
/// jumptables when it's possible.
|
2012-03-24 07:07:03 +08:00
|
|
|
bool ARMConstantIslands::optimizeThumb2JumpTables() {
|
2009-07-29 10:18:14 +08:00
|
|
|
bool MadeChange = false;
|
|
|
|
|
|
|
|
// FIXME: After the tables are shrunk, can we get rid some of the
|
|
|
|
// constantpool tables?
|
2011-12-13 02:16:53 +08:00
|
|
|
MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
|
2014-04-25 13:30:21 +08:00
|
|
|
if (!MJTI) return false;
|
2010-07-08 05:06:51 +08:00
|
|
|
|
2009-07-29 10:18:14 +08:00
|
|
|
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
|
|
|
|
for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) {
|
|
|
|
MachineInstr *MI = T2JumpTables[i];
|
2011-06-29 03:10:37 +08:00
|
|
|
const MCInstrDesc &MCID = MI->getDesc();
|
|
|
|
unsigned NumOps = MCID.getNumOperands();
|
2015-05-14 04:28:38 +08:00
|
|
|
unsigned JTOpIdx = NumOps - (MI->isPredicable() ? 2 : 1);
|
2009-07-29 10:18:14 +08:00
|
|
|
MachineOperand JTOP = MI->getOperand(JTOpIdx);
|
|
|
|
unsigned JTI = JTOP.getIndex();
|
|
|
|
assert(JTI < JT.size());
|
|
|
|
|
2009-11-11 10:47:19 +08:00
|
|
|
bool ByteOk = true;
|
|
|
|
bool HalfWordOk = true;
|
2012-03-24 07:07:03 +08:00
|
|
|
unsigned JTOffset = getOffsetOf(MI) + 4;
|
2009-11-13 01:25:07 +08:00
|
|
|
const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
|
2009-07-29 10:18:14 +08:00
|
|
|
for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) {
|
|
|
|
MachineBasicBlock *MBB = JTBBs[j];
|
2011-12-07 09:08:25 +08:00
|
|
|
unsigned DstOffset = BBInfo[MBB->getNumber()].Offset;
|
2009-07-30 07:20:20 +08:00
|
|
|
// Negative offset is not ok. FIXME: We should change BB layout to make
|
|
|
|
// sure all the branches are forward.
|
2009-08-01 02:28:05 +08:00
|
|
|
if (ByteOk && (DstOffset - JTOffset) > ((1<<8)-1)*2)
|
2009-07-29 10:18:14 +08:00
|
|
|
ByteOk = false;
|
2009-08-01 14:13:52 +08:00
|
|
|
unsigned TBHLimit = ((1<<16)-1)*2;
|
|
|
|
if (HalfWordOk && (DstOffset - JTOffset) > TBHLimit)
|
2009-07-29 10:18:14 +08:00
|
|
|
HalfWordOk = false;
|
|
|
|
if (!ByteOk && !HalfWordOk)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-06-01 03:22:07 +08:00
|
|
|
if (!ByteOk && !HalfWordOk)
|
|
|
|
continue;
|
2010-07-08 06:51:22 +08:00
|
|
|
|
2015-06-01 03:22:07 +08:00
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
if (!MI->getOperand(0).isKill()) // FIXME: needed now?
|
|
|
|
continue;
|
|
|
|
unsigned IdxReg = MI->getOperand(1).getReg();
|
|
|
|
bool IdxRegKill = MI->getOperand(1).isKill();
|
2009-08-01 14:13:52 +08:00
|
|
|
|
2015-06-01 03:22:07 +08:00
|
|
|
CPUser &User = CPUsers[JumpTableUserIndices[JTI]];
|
|
|
|
unsigned DeadSize = 0;
|
|
|
|
bool CanDeleteLEA = false;
|
|
|
|
bool BaseRegKill = false;
|
|
|
|
bool PreservedBaseReg =
|
|
|
|
preserveBaseRegister(MI, User.MI, DeadSize, CanDeleteLEA, BaseRegKill);
|
2015-05-19 01:10:40 +08:00
|
|
|
|
2015-06-01 03:22:07 +08:00
|
|
|
if (!jumpTableFollowsTB(MI, User.CPEMI) && !PreservedBaseReg)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Shrink JT: " << *MI);
|
|
|
|
MachineInstr *CPEMI = User.CPEMI;
|
|
|
|
unsigned Opc = ByteOk ? ARM::t2TBB_JT : ARM::t2TBH_JT;
|
|
|
|
MachineBasicBlock::iterator MI_JT = MI;
|
|
|
|
MachineInstr *NewJTMI =
|
|
|
|
BuildMI(*MBB, MI_JT, MI->getDebugLoc(), TII->get(Opc))
|
|
|
|
.addReg(User.MI->getOperand(0).getReg(),
|
|
|
|
getKillRegState(BaseRegKill))
|
|
|
|
.addReg(IdxReg, getKillRegState(IdxRegKill))
|
|
|
|
.addJumpTableIndex(JTI, JTOP.getTargetFlags())
|
|
|
|
.addImm(CPEMI->getOperand(0).getImm());
|
|
|
|
DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": " << *NewJTMI);
|
|
|
|
|
|
|
|
unsigned JTOpc = ByteOk ? ARM::JUMPTABLE_TBB : ARM::JUMPTABLE_TBH;
|
|
|
|
CPEMI->setDesc(TII->get(JTOpc));
|
|
|
|
|
|
|
|
if (jumpTableFollowsTB(MI, User.CPEMI)) {
|
|
|
|
NewJTMI->getOperand(0).setReg(ARM::PC);
|
|
|
|
NewJTMI->getOperand(0).setIsKill(false);
|
|
|
|
|
|
|
|
if (CanDeleteLEA) {
|
|
|
|
User.MI->eraseFromParent();
|
|
|
|
DeadSize += 4;
|
|
|
|
|
|
|
|
// The LEA was eliminated, the TBB instruction becomes the only new user
|
|
|
|
// of the jump table.
|
|
|
|
User.MI = NewJTMI;
|
|
|
|
User.MaxDisp = 4;
|
|
|
|
User.NegOk = false;
|
|
|
|
User.IsSoImm = false;
|
|
|
|
User.KnownAlignment = false;
|
|
|
|
} else {
|
|
|
|
// The LEA couldn't be eliminated, so we must add another CPUser to
|
|
|
|
// record the TBB or TBH use.
|
|
|
|
int CPEntryIdx = JumpTableEntryIndices[JTI];
|
|
|
|
auto &CPEs = CPEntries[CPEntryIdx];
|
2016-08-12 08:18:03 +08:00
|
|
|
auto Entry =
|
|
|
|
find_if(CPEs, [&](CPEntry &E) { return E.CPEMI == User.CPEMI; });
|
2015-06-01 03:22:07 +08:00
|
|
|
++Entry->RefCount;
|
|
|
|
CPUsers.emplace_back(CPUser(NewJTMI, User.CPEMI, 4, false, false));
|
|
|
|
}
|
2015-05-22 07:20:55 +08:00
|
|
|
}
|
2015-06-01 03:22:07 +08:00
|
|
|
|
2016-07-29 00:32:22 +08:00
|
|
|
unsigned NewSize = TII->getInstSizeInBytes(*NewJTMI);
|
|
|
|
unsigned OrigSize = TII->getInstSizeInBytes(*MI);
|
2015-06-01 03:22:07 +08:00
|
|
|
MI->eraseFromParent();
|
|
|
|
|
|
|
|
int Delta = OrigSize - NewSize + DeadSize;
|
|
|
|
BBInfo[MBB->getNumber()].Size -= Delta;
|
|
|
|
adjustBBOffsetsAfter(MBB);
|
|
|
|
|
|
|
|
++NumTBs;
|
|
|
|
MadeChange = true;
|
2009-07-29 10:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
2009-11-11 10:47:19 +08:00
|
|
|
|
2012-03-24 07:07:03 +08:00
|
|
|
/// reorderThumb2JumpTables - Adjust the function's block layout to ensure that
|
2009-11-17 02:55:47 +08:00
|
|
|
/// jump tables always branch forwards, since that's what tbb and tbh need.
|
2012-03-24 07:07:03 +08:00
|
|
|
bool ARMConstantIslands::reorderThumb2JumpTables() {
|
2009-11-13 01:25:07 +08:00
|
|
|
bool MadeChange = false;
|
|
|
|
|
2011-12-13 02:16:53 +08:00
|
|
|
MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
|
2014-04-25 13:30:21 +08:00
|
|
|
if (!MJTI) return false;
|
2010-07-08 05:06:51 +08:00
|
|
|
|
2009-11-13 01:25:07 +08:00
|
|
|
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
|
|
|
|
for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) {
|
|
|
|
MachineInstr *MI = T2JumpTables[i];
|
2011-06-29 03:10:37 +08:00
|
|
|
const MCInstrDesc &MCID = MI->getDesc();
|
|
|
|
unsigned NumOps = MCID.getNumOperands();
|
2015-05-14 04:28:38 +08:00
|
|
|
unsigned JTOpIdx = NumOps - (MI->isPredicable() ? 2 : 1);
|
2009-11-13 01:25:07 +08: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-17 02:58:52 +08:00
|
|
|
int JTNumber = MI->getParent()->getNumber();
|
2009-11-13 01:25:07 +08: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-17 02:58:52 +08:00
|
|
|
int DTNumber = MBB->getNumber();
|
2009-11-13 01:25:07 +08:00
|
|
|
|
2009-11-17 02:58:52 +08:00
|
|
|
if (DTNumber < JTNumber) {
|
2009-11-13 01:25:07 +08:00
|
|
|
// The destination precedes the switch. Try to move the block forward
|
|
|
|
// so we have a positive offset.
|
|
|
|
MachineBasicBlock *NewBB =
|
2012-03-24 07:07:03 +08:00
|
|
|
adjustJTTargetBlockForward(MBB, MI->getParent());
|
2009-11-13 01:25:07 +08:00
|
|
|
if (NewBB)
|
2009-11-15 04:10:18 +08:00
|
|
|
MJTI->ReplaceMBBInJumpTable(JTI, JTBBs[j], NewBB);
|
2009-11-13 01:25:07 +08:00
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2009-11-11 10:47:19 +08:00
|
|
|
MachineBasicBlock *ARMConstantIslands::
|
2012-03-24 07:07:03 +08:00
|
|
|
adjustJTTargetBlockForward(MachineBasicBlock *BB, MachineBasicBlock *JTBB) {
|
2010-07-08 06:53:35 +08:00
|
|
|
// If the destination block is terminated by an unconditional branch,
|
2009-11-13 01:25:07 +08:00
|
|
|
// try to move it; otherwise, create a new block following the jump
|
2009-11-17 02:58:52 +08:00
|
|
|
// table that branches back to the actual target. This is a very simple
|
|
|
|
// heuristic. FIXME: We can definitely improve it.
|
2014-04-25 13:30:21 +08:00
|
|
|
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
|
2009-11-13 01:25:07 +08:00
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
2009-11-17 09:21:04 +08:00
|
|
|
SmallVector<MachineOperand, 4> CondPrior;
|
2015-10-20 07:25:57 +08:00
|
|
|
MachineFunction::iterator BBi = BB->getIterator();
|
2014-03-02 20:27:27 +08:00
|
|
|
MachineFunction::iterator OldPrior = std::prev(BBi);
|
2009-11-15 04:10:18 +08:00
|
|
|
|
2009-11-17 01:10:56 +08:00
|
|
|
// If the block terminator isn't analyzable, don't try to move the block
|
2016-07-15 22:41:04 +08:00
|
|
|
bool B = TII->analyzeBranch(*BB, TBB, FBB, Cond);
|
2009-11-17 01:10:56 +08:00
|
|
|
|
2009-11-17 09:21:04 +08: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-17 02:58:52 +08:00
|
|
|
// and make sure we're not trying to move the entry block of the function.
|
2016-02-22 04:39:50 +08:00
|
|
|
if (!B && Cond.empty() && BB != &MF->front() &&
|
2016-07-15 22:41:04 +08:00
|
|
|
!TII->analyzeBranch(*OldPrior, TBB, FBB, CondPrior)) {
|
2009-11-13 01:25:07 +08:00
|
|
|
BB->moveAfter(JTBB);
|
|
|
|
OldPrior->updateTerminator();
|
2009-11-15 04:10:18 +08:00
|
|
|
BB->updateTerminator();
|
2009-11-17 02:58:52 +08:00
|
|
|
// Update numbering to account for the block being moved.
|
2011-12-13 02:16:53 +08:00
|
|
|
MF->RenumberBlocks();
|
2009-11-13 01:25:07 +08:00
|
|
|
++NumJTMoved;
|
2014-04-25 13:30:21 +08:00
|
|
|
return nullptr;
|
2009-11-13 01:25:07 +08:00
|
|
|
}
|
2009-11-11 10:47:19 +08:00
|
|
|
|
|
|
|
// Create a new MBB for the code after the jump BB.
|
|
|
|
MachineBasicBlock *NewBB =
|
2011-12-13 02:16:53 +08:00
|
|
|
MF->CreateMachineBasicBlock(JTBB->getBasicBlock());
|
2015-10-20 07:25:57 +08:00
|
|
|
MachineFunction::iterator MBBI = ++JTBB->getIterator();
|
2011-12-13 02:16:53 +08:00
|
|
|
MF->insert(MBBI, NewBB);
|
2009-11-11 10:47:19 +08: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-10 05:48:23 +08:00
|
|
|
BuildMI(NewBB, DebugLoc(), TII->get(ARM::t2B)).addMBB(BB)
|
|
|
|
.addImm(ARMCC::AL).addReg(0);
|
2009-11-11 10:47:19 +08:00
|
|
|
|
2009-11-15 04:10:18 +08:00
|
|
|
// Update internal data structures to account for the newly inserted MBB.
|
2011-12-13 02:16:53 +08:00
|
|
|
MF->RenumberBlocks(NewBB);
|
2009-11-15 04:10:18 +08:00
|
|
|
|
2009-11-11 10:47:19 +08:00
|
|
|
// Update the CFG.
|
|
|
|
NewBB->addSuccessor(BB);
|
2015-12-01 13:29:22 +08:00
|
|
|
JTBB->replaceSuccessor(BB, NewBB);
|
2009-11-11 10:47:19 +08:00
|
|
|
|
2009-11-13 01:25:07 +08:00
|
|
|
++NumJTInserted;
|
2009-11-11 10:47:19 +08:00
|
|
|
return NewBB;
|
|
|
|
}
|