2017-08-04 06:12:30 +08:00
|
|
|
//===- MipsLongBranch.cpp - Emit long branches ----------------------------===//
|
2012-06-14 09:22:24 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass expands a branch or jump instruction into a long branch if its
|
|
|
|
// offset is too large to fit into its immediate field.
|
|
|
|
//
|
2014-04-30 23:06:25 +08:00
|
|
|
// FIXME: Fix pc-region jump instructions which cross 256MB segment boundaries.
|
2012-06-14 09:22:24 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "MCTargetDesc/MipsABIInfo.h"
|
2012-06-14 09:22:24 +08:00
|
|
|
#include "MCTargetDesc/MipsBaseInfo.h"
|
2014-06-05 21:52:08 +08:00
|
|
|
#include "MCTargetDesc/MipsMCNaCl.h"
|
2017-08-04 06:12:30 +08:00
|
|
|
#include "MCTargetDesc/MipsMCTargetDesc.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "Mips.h"
|
|
|
|
#include "MipsInstrInfo.h"
|
2014-09-03 06:28:02 +08:00
|
|
|
#include "MipsMachineFunction.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "MipsSubtarget.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "MipsTargetMachine.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2012-06-14 09:22:24 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2012-06-14 09:22:24 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2012-06-14 09:22:24 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "llvm/IR/DebugLoc.h"
|
2012-06-14 09:22:24 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-06-14 09:22:24 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iterator>
|
2012-06-14 09:22:24 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "mips-long-branch"
|
|
|
|
|
2012-06-14 09:22:24 +08:00
|
|
|
STATISTIC(LongBranches, "Number of long branches.");
|
|
|
|
|
|
|
|
static cl::opt<bool> SkipLongBranch(
|
|
|
|
"skip-mips-long-branch",
|
|
|
|
cl::init(false),
|
|
|
|
cl::desc("MIPS: Skip long branch pass."),
|
|
|
|
cl::Hidden);
|
|
|
|
|
|
|
|
static cl::opt<bool> ForceLongBranch(
|
|
|
|
"force-mips-long-branch",
|
|
|
|
cl::init(false),
|
|
|
|
cl::desc("MIPS: Expand all branches to long format."),
|
|
|
|
cl::Hidden);
|
|
|
|
|
|
|
|
namespace {
|
2017-02-01 09:22:51 +08:00
|
|
|
|
2017-08-04 06:12:30 +08:00
|
|
|
using Iter = MachineBasicBlock::iterator;
|
|
|
|
using ReverseIter = MachineBasicBlock::reverse_iterator;
|
2012-06-14 09:22:24 +08:00
|
|
|
|
|
|
|
struct MBBInfo {
|
2017-02-01 09:22:51 +08:00
|
|
|
uint64_t Size = 0;
|
|
|
|
uint64_t Address;
|
|
|
|
bool HasLongBranch = false;
|
|
|
|
MachineInstr *Br = nullptr;
|
2012-06-14 09:22:24 +08:00
|
|
|
|
2017-02-01 09:22:51 +08:00
|
|
|
MBBInfo() = default;
|
2012-06-14 09:22:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class MipsLongBranch : public MachineFunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
2017-02-01 09:22:51 +08:00
|
|
|
|
2017-05-19 01:21:13 +08:00
|
|
|
MipsLongBranch()
|
|
|
|
: MachineFunctionPass(ID), ABI(MipsABIInfo::Unknown()) {}
|
2012-06-14 09:22:24 +08:00
|
|
|
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override { return "Mips Long Branch"; }
|
2012-06-14 09:22:24 +08:00
|
|
|
|
2014-04-29 15:58:02 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &F) override;
|
2012-06-14 09:22:24 +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
|
|
|
}
|
|
|
|
|
2012-06-14 09:22:24 +08:00
|
|
|
private:
|
|
|
|
void splitMBB(MachineBasicBlock *MBB);
|
|
|
|
void initMBBInfo();
|
|
|
|
int64_t computeOffset(const MachineInstr *Br);
|
2016-06-12 23:39:02 +08:00
|
|
|
void replaceBranch(MachineBasicBlock &MBB, Iter Br, const DebugLoc &DL,
|
2012-06-14 09:22:24 +08:00
|
|
|
MachineBasicBlock *MBBOpnd);
|
|
|
|
void expandToLongBranch(MBBInfo &Info);
|
|
|
|
|
|
|
|
MachineFunction *MF;
|
|
|
|
SmallVector<MBBInfo, 16> MBBInfos;
|
2012-08-28 11:03:05 +08:00
|
|
|
bool IsPIC;
|
2014-10-25 00:15:27 +08:00
|
|
|
MipsABIInfo ABI;
|
2012-08-28 11:03:05 +08:00
|
|
|
unsigned LongBranchSeqSize;
|
2012-06-14 09:22:24 +08:00
|
|
|
};
|
|
|
|
|
2017-02-01 09:22:51 +08:00
|
|
|
} // end anonymous namespace
|
2012-06-14 09:22:24 +08:00
|
|
|
|
2017-08-04 06:12:30 +08:00
|
|
|
char MipsLongBranch::ID = 0;
|
|
|
|
|
2012-06-14 09:22:24 +08:00
|
|
|
/// Iterate over list of Br's operands and search for a MachineBasicBlock
|
|
|
|
/// operand.
|
|
|
|
static MachineBasicBlock *getTargetMBB(const MachineInstr &Br) {
|
|
|
|
for (unsigned I = 0, E = Br.getDesc().getNumOperands(); I < E; ++I) {
|
|
|
|
const MachineOperand &MO = Br.getOperand(I);
|
|
|
|
|
|
|
|
if (MO.isMBB())
|
|
|
|
return MO.getMBB();
|
|
|
|
}
|
|
|
|
|
2015-01-05 18:15:49 +08:00
|
|
|
llvm_unreachable("This instruction does not have an MBB operand.");
|
2012-06-14 09:22:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Traverse the list of instructions backwards until a non-debug instruction is
|
|
|
|
// found or it reaches E.
|
2016-06-12 23:39:02 +08:00
|
|
|
static ReverseIter getNonDebugInstr(ReverseIter B, const ReverseIter &E) {
|
2012-06-14 09:22:24 +08:00
|
|
|
for (; B != E; ++B)
|
|
|
|
if (!B->isDebugValue())
|
|
|
|
return B;
|
|
|
|
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split MBB if it has two direct jumps/branches.
|
|
|
|
void MipsLongBranch::splitMBB(MachineBasicBlock *MBB) {
|
|
|
|
ReverseIter End = MBB->rend();
|
|
|
|
ReverseIter LastBr = getNonDebugInstr(MBB->rbegin(), End);
|
|
|
|
|
|
|
|
// Return if MBB has no branch instructions.
|
|
|
|
if ((LastBr == End) ||
|
|
|
|
(!LastBr->isConditionalBranch() && !LastBr->isUnconditionalBranch()))
|
|
|
|
return;
|
|
|
|
|
2014-03-02 20:27:27 +08:00
|
|
|
ReverseIter FirstBr = getNonDebugInstr(std::next(LastBr), End);
|
2012-06-14 09:22:24 +08:00
|
|
|
|
|
|
|
// MBB has only one branch instruction if FirstBr is not a branch
|
|
|
|
// instruction.
|
|
|
|
if ((FirstBr == End) ||
|
|
|
|
(!FirstBr->isConditionalBranch() && !FirstBr->isUnconditionalBranch()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(!FirstBr->isIndirectBranch() && "Unexpected indirect branch found.");
|
|
|
|
|
|
|
|
// Create a new MBB. Move instructions in MBB to the newly created MBB.
|
|
|
|
MachineBasicBlock *NewMBB =
|
|
|
|
MF->CreateMachineBasicBlock(MBB->getBasicBlock());
|
|
|
|
|
|
|
|
// Insert NewMBB and fix control flow.
|
|
|
|
MachineBasicBlock *Tgt = getTargetMBB(*FirstBr);
|
|
|
|
NewMBB->transferSuccessors(MBB);
|
2015-12-13 17:26:17 +08:00
|
|
|
NewMBB->removeSuccessor(Tgt, true);
|
2012-06-14 09:22:24 +08:00
|
|
|
MBB->addSuccessor(NewMBB);
|
|
|
|
MBB->addSuccessor(Tgt);
|
2014-03-02 20:27:27 +08:00
|
|
|
MF->insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
|
2012-06-14 09:22:24 +08:00
|
|
|
|
2016-09-12 02:51:28 +08:00
|
|
|
NewMBB->splice(NewMBB->end(), MBB, LastBr.getReverse(), MBB->end());
|
2012-06-14 09:22:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fill MBBInfos.
|
|
|
|
void MipsLongBranch::initMBBInfo() {
|
|
|
|
// Split the MBBs if they have two branches. Each basic block should have at
|
|
|
|
// most one branch after this loop is executed.
|
2016-04-16 04:43:17 +08:00
|
|
|
for (auto &MBB : *MF)
|
|
|
|
splitMBB(&MBB);
|
2012-06-14 09:22:24 +08:00
|
|
|
|
|
|
|
MF->RenumberBlocks();
|
|
|
|
MBBInfos.clear();
|
|
|
|
MBBInfos.resize(MF->size());
|
|
|
|
|
2013-06-07 15:04:14 +08:00
|
|
|
const MipsInstrInfo *TII =
|
2015-01-30 07:27:36 +08:00
|
|
|
static_cast<const MipsInstrInfo *>(MF->getSubtarget().getInstrInfo());
|
2012-06-14 09:22:24 +08:00
|
|
|
for (unsigned I = 0, E = MBBInfos.size(); I < E; ++I) {
|
|
|
|
MachineBasicBlock *MBB = MF->getBlockNumbered(I);
|
|
|
|
|
|
|
|
// Compute size of MBB.
|
|
|
|
for (MachineBasicBlock::instr_iterator MI = MBB->instr_begin();
|
|
|
|
MI != MBB->instr_end(); ++MI)
|
2016-07-29 00:32:22 +08:00
|
|
|
MBBInfos[I].Size += TII->getInstSizeInBytes(*MI);
|
2012-06-14 09:22:24 +08:00
|
|
|
|
|
|
|
// Search for MBB's branch instruction.
|
|
|
|
ReverseIter End = MBB->rend();
|
|
|
|
ReverseIter Br = getNonDebugInstr(MBB->rbegin(), End);
|
|
|
|
|
|
|
|
if ((Br != End) && !Br->isIndirectBranch() &&
|
2016-06-28 22:33:28 +08:00
|
|
|
(Br->isConditionalBranch() || (Br->isUnconditionalBranch() && IsPIC)))
|
2016-09-12 02:51:28 +08:00
|
|
|
MBBInfos[I].Br = &*Br;
|
2012-06-14 09:22:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute offset of branch in number of bytes.
|
|
|
|
int64_t MipsLongBranch::computeOffset(const MachineInstr *Br) {
|
|
|
|
int64_t Offset = 0;
|
|
|
|
int ThisMBB = Br->getParent()->getNumber();
|
|
|
|
int TargetMBB = getTargetMBB(*Br)->getNumber();
|
|
|
|
|
|
|
|
// Compute offset of a forward branch.
|
|
|
|
if (ThisMBB < TargetMBB) {
|
|
|
|
for (int N = ThisMBB + 1; N < TargetMBB; ++N)
|
|
|
|
Offset += MBBInfos[N].Size;
|
|
|
|
|
|
|
|
return Offset + 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute offset of a backward branch.
|
|
|
|
for (int N = ThisMBB; N >= TargetMBB; --N)
|
|
|
|
Offset += MBBInfos[N].Size;
|
|
|
|
|
|
|
|
return -Offset + 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace Br with a branch which has the opposite condition code and a
|
|
|
|
// MachineBasicBlock operand MBBOpnd.
|
|
|
|
void MipsLongBranch::replaceBranch(MachineBasicBlock &MBB, Iter Br,
|
2016-06-12 23:39:02 +08:00
|
|
|
const DebugLoc &DL,
|
|
|
|
MachineBasicBlock *MBBOpnd) {
|
2015-01-30 07:27:36 +08:00
|
|
|
const MipsInstrInfo *TII = static_cast<const MipsInstrInfo *>(
|
|
|
|
MBB.getParent()->getSubtarget().getInstrInfo());
|
2013-05-14 01:43:19 +08:00
|
|
|
unsigned NewOpc = TII->getOppositeBranchOpc(Br->getOpcode());
|
2012-06-14 09:22:24 +08:00
|
|
|
const MCInstrDesc &NewDesc = TII->get(NewOpc);
|
|
|
|
|
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, Br, DL, NewDesc);
|
|
|
|
|
|
|
|
for (unsigned I = 0, E = Br->getDesc().getNumOperands(); I < E; ++I) {
|
|
|
|
MachineOperand &MO = Br->getOperand(I);
|
|
|
|
|
|
|
|
if (!MO.isReg()) {
|
|
|
|
assert(MO.isMBB() && "MBB operand expected.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
MIB.addReg(MO.getReg());
|
|
|
|
}
|
|
|
|
|
|
|
|
MIB.addMBB(MBBOpnd);
|
|
|
|
|
2014-11-22 06:04:35 +08:00
|
|
|
if (Br->hasDelaySlot()) {
|
|
|
|
// Bundle the instruction in the delay slot to the newly created branch
|
|
|
|
// and erase the original branch.
|
|
|
|
assert(Br->isBundledWithSucc());
|
2016-07-16 07:09:47 +08:00
|
|
|
MachineBasicBlock::instr_iterator II = Br.getInstrIterator();
|
2014-11-22 06:04:35 +08:00
|
|
|
MIBundleBuilder(&*MIB).append((++II)->removeFromBundle());
|
|
|
|
}
|
2012-06-14 09:22:24 +08:00
|
|
|
Br->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expand branch instructions to long branches.
|
2015-01-12 20:03:34 +08:00
|
|
|
// TODO: This function has to be fixed for beqz16 and bnez16, because it
|
|
|
|
// currently assumes that all branches have 16-bit offsets, and will produce
|
|
|
|
// wrong code if branches whose allowed offsets are [-128, -126, ..., 126]
|
|
|
|
// are present.
|
2012-06-14 09:22:24 +08:00
|
|
|
void MipsLongBranch::expandToLongBranch(MBBInfo &I) {
|
2012-07-21 11:30:44 +08:00
|
|
|
MachineBasicBlock::iterator Pos;
|
|
|
|
MachineBasicBlock *MBB = I.Br->getParent(), *TgtMBB = getTargetMBB(*I.Br);
|
2012-06-14 09:22:24 +08:00
|
|
|
DebugLoc DL = I.Br->getDebugLoc();
|
2012-07-21 11:30:44 +08:00
|
|
|
const BasicBlock *BB = MBB->getBasicBlock();
|
|
|
|
MachineFunction::iterator FallThroughMBB = ++MachineFunction::iterator(MBB);
|
|
|
|
MachineBasicBlock *LongBrMBB = MF->CreateMachineBasicBlock(BB);
|
2015-01-30 07:27:36 +08:00
|
|
|
const MipsSubtarget &Subtarget =
|
|
|
|
static_cast<const MipsSubtarget &>(MF->getSubtarget());
|
2013-06-07 15:04:14 +08:00
|
|
|
const MipsInstrInfo *TII =
|
2015-01-30 07:27:36 +08:00
|
|
|
static_cast<const MipsInstrInfo *>(Subtarget.getInstrInfo());
|
2013-06-07 15:04:14 +08:00
|
|
|
|
2012-07-21 11:30:44 +08:00
|
|
|
MF->insert(FallThroughMBB, LongBrMBB);
|
2015-12-01 13:29:22 +08:00
|
|
|
MBB->replaceSuccessor(TgtMBB, LongBrMBB);
|
2012-07-21 11:30:44 +08:00
|
|
|
|
|
|
|
if (IsPIC) {
|
|
|
|
MachineBasicBlock *BalTgtMBB = MF->CreateMachineBasicBlock(BB);
|
|
|
|
MF->insert(FallThroughMBB, BalTgtMBB);
|
2017-06-20 03:48:59 +08:00
|
|
|
LongBrMBB->addSuccessor(BalTgtMBB);
|
|
|
|
BalTgtMBB->addSuccessor(TgtMBB);
|
2012-06-14 09:22:24 +08:00
|
|
|
|
2017-12-14 22:55:25 +08:00
|
|
|
// We must select between the MIPS32r6/MIPS64r6 BALC (which is a normal
|
2014-06-13 21:02:52 +08:00
|
|
|
// instruction) and the pre-MIPS32r6/MIPS64r6 definition (which is an
|
|
|
|
// pseudo-instruction wrapping BGEZAL).
|
2017-12-14 22:55:25 +08:00
|
|
|
const unsigned BalOp =
|
|
|
|
Subtarget.hasMips32r6()
|
|
|
|
? Subtarget.inMicroMipsMode() ? Mips::BALC_MMR6 : Mips::BALC
|
|
|
|
: Mips::BAL_BR;
|
2014-06-13 21:02:52 +08:00
|
|
|
|
2014-10-25 00:15:27 +08:00
|
|
|
if (!ABI.IsN64()) {
|
2017-12-14 22:55:25 +08:00
|
|
|
// Pre R6:
|
2012-08-28 11:03:05 +08:00
|
|
|
// $longbr:
|
|
|
|
// addiu $sp, $sp, -8
|
|
|
|
// sw $ra, 0($sp)
|
|
|
|
// lui $at, %hi($tgt - $baltgt)
|
2014-04-30 23:06:25 +08:00
|
|
|
// bal $baltgt
|
2012-08-28 11:03:05 +08:00
|
|
|
// addiu $at, $at, %lo($tgt - $baltgt)
|
2014-04-30 23:06:25 +08:00
|
|
|
// $baltgt:
|
2012-08-28 11:03:05 +08:00
|
|
|
// addu $at, $ra, $at
|
|
|
|
// lw $ra, 0($sp)
|
|
|
|
// jr $at
|
|
|
|
// addiu $sp, $sp, 8
|
|
|
|
// $fallthrough:
|
|
|
|
//
|
|
|
|
|
2017-12-14 22:55:25 +08:00
|
|
|
// R6:
|
|
|
|
// $longbr:
|
|
|
|
// addiu $sp, $sp, -8
|
|
|
|
// sw $ra, 0($sp)
|
|
|
|
// lui $at, %hi($tgt - $baltgt)
|
|
|
|
// addiu $at, $at, %lo($tgt - $baltgt)
|
|
|
|
// balc $baltgt
|
|
|
|
// $baltgt:
|
|
|
|
// addu $at, $ra, $at
|
|
|
|
// lw $ra, 0($sp)
|
|
|
|
// addiu $sp, $sp, 8
|
|
|
|
// jic $at, 0
|
|
|
|
// $fallthrough:
|
|
|
|
|
2012-08-28 11:03:05 +08:00
|
|
|
Pos = LongBrMBB->begin();
|
|
|
|
|
|
|
|
BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
|
|
|
|
.addReg(Mips::SP).addImm(-8);
|
|
|
|
BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SW)).addReg(Mips::RA)
|
|
|
|
.addReg(Mips::SP).addImm(0);
|
2012-12-07 12:23:40 +08:00
|
|
|
|
2014-04-30 23:06:25 +08:00
|
|
|
// LUi and ADDiu instructions create 32-bit offset of the target basic
|
2017-12-14 22:55:25 +08:00
|
|
|
// block from the target of BAL(C) instruction. We cannot use immediate
|
2014-04-30 23:06:25 +08:00
|
|
|
// value for this offset because it cannot be determined accurately when
|
|
|
|
// the program has inline assembly statements. We therefore use the
|
|
|
|
// relocation expressions %hi($tgt-$baltgt) and %lo($tgt-$baltgt) which
|
|
|
|
// are resolved during the fixup, so the values will always be correct.
|
|
|
|
//
|
|
|
|
// Since we cannot create %hi($tgt-$baltgt) and %lo($tgt-$baltgt)
|
|
|
|
// expressions at this point (it is possible only at the MC layer),
|
|
|
|
// we replace LUi and ADDiu with pseudo instructions
|
|
|
|
// LONG_BRANCH_LUi and LONG_BRANCH_ADDiu, and add both basic
|
|
|
|
// blocks as operands to these instructions. When lowering these pseudo
|
|
|
|
// instructions to LUi and ADDiu in the MC layer, we will create
|
|
|
|
// %hi($tgt-$baltgt) and %lo($tgt-$baltgt) expressions and add them as
|
|
|
|
// operands to lowered instructions.
|
|
|
|
|
|
|
|
BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_LUi), Mips::AT)
|
|
|
|
.addMBB(TgtMBB).addMBB(BalTgtMBB);
|
2017-12-14 22:55:25 +08:00
|
|
|
|
|
|
|
MachineInstrBuilder BalInstr =
|
|
|
|
BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB);
|
|
|
|
MachineInstrBuilder ADDiuInstr =
|
|
|
|
BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_ADDiu), Mips::AT)
|
|
|
|
.addReg(Mips::AT)
|
|
|
|
.addMBB(TgtMBB)
|
|
|
|
.addMBB(BalTgtMBB);
|
|
|
|
if (Subtarget.hasMips32r6()) {
|
|
|
|
LongBrMBB->insert(Pos, ADDiuInstr);
|
|
|
|
LongBrMBB->insert(Pos, BalInstr);
|
|
|
|
} else {
|
|
|
|
LongBrMBB->insert(Pos, BalInstr);
|
|
|
|
LongBrMBB->insert(Pos, ADDiuInstr);
|
|
|
|
LongBrMBB->rbegin()->bundleWithPred();
|
|
|
|
}
|
2012-08-28 11:03:05 +08:00
|
|
|
|
|
|
|
Pos = BalTgtMBB->begin();
|
|
|
|
|
|
|
|
BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDu), Mips::AT)
|
|
|
|
.addReg(Mips::RA).addReg(Mips::AT);
|
|
|
|
BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LW), Mips::RA)
|
|
|
|
.addReg(Mips::SP).addImm(0);
|
2017-12-14 22:55:25 +08:00
|
|
|
if (Subtarget.isTargetNaCl())
|
|
|
|
// Bundle-align the target of indirect branch JR.
|
|
|
|
TgtMBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
|
2012-12-07 12:23:40 +08:00
|
|
|
|
2016-06-18 23:39:43 +08:00
|
|
|
// In NaCl, modifying the sp is not allowed in branch delay slot.
|
2017-12-14 22:55:25 +08:00
|
|
|
// For MIPS32R6, we can skip using a delay slot branch.
|
|
|
|
if (Subtarget.isTargetNaCl() || Subtarget.hasMips32r6())
|
2014-06-05 21:52:08 +08:00
|
|
|
BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
|
|
|
|
.addReg(Mips::SP).addImm(8);
|
|
|
|
|
2017-12-14 22:55:25 +08:00
|
|
|
if (Subtarget.hasMips32r6()) {
|
|
|
|
const unsigned JICOp =
|
|
|
|
Subtarget.inMicroMipsMode() ? Mips::JIC_MMR6 : Mips::JIC;
|
|
|
|
BuildMI(*BalTgtMBB, Pos, DL, TII->get(JICOp))
|
|
|
|
.addReg(Mips::AT)
|
|
|
|
.addImm(0);
|
|
|
|
|
|
|
|
} else {
|
2016-06-18 23:39:43 +08:00
|
|
|
BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JR)).addReg(Mips::AT);
|
2014-06-05 21:52:08 +08:00
|
|
|
|
2017-12-14 22:55:25 +08:00
|
|
|
if (Subtarget.isTargetNaCl()) {
|
|
|
|
BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::NOP));
|
|
|
|
} else
|
|
|
|
BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
|
|
|
|
.addReg(Mips::SP)
|
|
|
|
.addImm(8);
|
2016-06-18 23:39:43 +08:00
|
|
|
|
2017-12-14 22:55:25 +08:00
|
|
|
BalTgtMBB->rbegin()->bundleWithPred();
|
|
|
|
}
|
2012-08-28 11:03:05 +08:00
|
|
|
} else {
|
2017-12-14 22:55:25 +08:00
|
|
|
// Pre R6:
|
2012-08-28 11:03:05 +08:00
|
|
|
// $longbr:
|
|
|
|
// daddiu $sp, $sp, -16
|
|
|
|
// sd $ra, 0($sp)
|
2014-05-28 02:53:06 +08:00
|
|
|
// daddiu $at, $zero, %hi($tgt - $baltgt)
|
2012-08-28 11:03:05 +08:00
|
|
|
// dsll $at, $at, 16
|
2014-04-30 23:06:25 +08:00
|
|
|
// bal $baltgt
|
2012-08-28 11:03:05 +08:00
|
|
|
// daddiu $at, $at, %lo($tgt - $baltgt)
|
2014-04-30 23:06:25 +08:00
|
|
|
// $baltgt:
|
2012-08-28 11:03:05 +08:00
|
|
|
// daddu $at, $ra, $at
|
|
|
|
// ld $ra, 0($sp)
|
|
|
|
// jr64 $at
|
|
|
|
// daddiu $sp, $sp, 16
|
|
|
|
// $fallthrough:
|
2017-12-14 22:55:25 +08:00
|
|
|
|
|
|
|
// R6:
|
|
|
|
// $longbr:
|
|
|
|
// daddiu $sp, $sp, -16
|
|
|
|
// sd $ra, 0($sp)
|
|
|
|
// daddiu $at, $zero, %hi($tgt - $baltgt)
|
|
|
|
// dsll $at, $at, 16
|
|
|
|
// daddiu $at, $at, %lo($tgt - $baltgt)
|
|
|
|
// balc $baltgt
|
|
|
|
// $baltgt:
|
|
|
|
// daddu $at, $ra, $at
|
|
|
|
// ld $ra, 0($sp)
|
|
|
|
// daddiu $sp, $sp, 16
|
|
|
|
// jic $at, 0
|
|
|
|
// $fallthrough:
|
2012-08-28 11:03:05 +08:00
|
|
|
|
2014-05-28 02:53:06 +08:00
|
|
|
// We assume the branch is within-function, and that offset is within
|
|
|
|
// +/- 2GB. High 32 bits will therefore always be zero.
|
|
|
|
|
|
|
|
// Note that this will work even if the offset is negative, because
|
|
|
|
// of the +1 modification that's added in that case. For example, if the
|
|
|
|
// offset is -1MB (0xFFFFFFFFFFF00000), the computation for %higher is
|
|
|
|
//
|
|
|
|
// 0xFFFFFFFFFFF00000 + 0x80008000 = 0x000000007FF08000
|
|
|
|
//
|
|
|
|
// and the bits [47:32] are zero. For %highest
|
|
|
|
//
|
|
|
|
// 0xFFFFFFFFFFF00000 + 0x800080008000 = 0x000080007FF08000
|
|
|
|
//
|
|
|
|
// and the bits [63:48] are zero.
|
2012-08-28 11:03:05 +08:00
|
|
|
|
|
|
|
Pos = LongBrMBB->begin();
|
|
|
|
|
|
|
|
BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64)
|
|
|
|
.addReg(Mips::SP_64).addImm(-16);
|
|
|
|
BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SD)).addReg(Mips::RA_64)
|
|
|
|
.addReg(Mips::SP_64).addImm(0);
|
2014-04-30 23:06:25 +08:00
|
|
|
BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_DADDiu),
|
2014-05-28 02:53:06 +08:00
|
|
|
Mips::AT_64).addReg(Mips::ZERO_64)
|
|
|
|
.addMBB(TgtMBB, MipsII::MO_ABS_HI).addMBB(BalTgtMBB);
|
2012-08-28 11:03:05 +08:00
|
|
|
BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DSLL), Mips::AT_64)
|
|
|
|
.addReg(Mips::AT_64).addImm(16);
|
2012-12-07 12:23:40 +08:00
|
|
|
|
2017-12-14 22:55:25 +08:00
|
|
|
MachineInstrBuilder BalInstr =
|
|
|
|
BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB);
|
|
|
|
MachineInstrBuilder DADDiuInstr =
|
|
|
|
BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_DADDiu), Mips::AT_64)
|
|
|
|
.addReg(Mips::AT_64)
|
|
|
|
.addMBB(TgtMBB, MipsII::MO_ABS_LO)
|
|
|
|
.addMBB(BalTgtMBB);
|
|
|
|
if (Subtarget.hasMips32r6()) {
|
|
|
|
LongBrMBB->insert(Pos, DADDiuInstr);
|
|
|
|
LongBrMBB->insert(Pos, BalInstr);
|
|
|
|
} else {
|
|
|
|
LongBrMBB->insert(Pos, BalInstr);
|
|
|
|
LongBrMBB->insert(Pos, DADDiuInstr);
|
|
|
|
LongBrMBB->rbegin()->bundleWithPred();
|
|
|
|
}
|
2012-08-28 11:03:05 +08:00
|
|
|
|
|
|
|
Pos = BalTgtMBB->begin();
|
|
|
|
|
|
|
|
BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDu), Mips::AT_64)
|
|
|
|
.addReg(Mips::RA_64).addReg(Mips::AT_64);
|
|
|
|
BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LD), Mips::RA_64)
|
|
|
|
.addReg(Mips::SP_64).addImm(0);
|
2012-12-07 12:23:40 +08:00
|
|
|
|
2017-12-14 22:55:25 +08:00
|
|
|
if (Subtarget.hasMips64r6()) {
|
|
|
|
BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64)
|
|
|
|
.addReg(Mips::SP_64)
|
|
|
|
.addImm(16);
|
|
|
|
BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JIC64))
|
|
|
|
.addReg(Mips::AT_64)
|
|
|
|
.addImm(0);
|
|
|
|
} else {
|
2016-06-18 23:39:43 +08:00
|
|
|
BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JR64)).addReg(Mips::AT_64);
|
2017-12-14 22:55:25 +08:00
|
|
|
BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64)
|
|
|
|
.addReg(Mips::SP_64)
|
|
|
|
.addImm(16);
|
|
|
|
BalTgtMBB->rbegin()->bundleWithPred();
|
|
|
|
}
|
2012-07-21 11:30:44 +08:00
|
|
|
}
|
2012-11-16 04:05:11 +08:00
|
|
|
|
2014-04-30 23:06:25 +08:00
|
|
|
assert(LongBrMBB->size() + BalTgtMBB->size() == LongBranchSeqSize);
|
2012-07-21 11:30:44 +08:00
|
|
|
} else {
|
2017-12-14 22:55:25 +08:00
|
|
|
// Pre R6: R6:
|
|
|
|
// $longbr: $longbr:
|
|
|
|
// j $tgt bc $tgt
|
|
|
|
// nop $fallthrough
|
2012-07-21 11:30:44 +08:00
|
|
|
// $fallthrough:
|
2012-06-14 09:22:24 +08:00
|
|
|
//
|
2012-07-21 11:30:44 +08:00
|
|
|
Pos = LongBrMBB->begin();
|
|
|
|
LongBrMBB->addSuccessor(TgtMBB);
|
2017-12-14 22:55:25 +08:00
|
|
|
if (Subtarget.hasMips32r6())
|
|
|
|
BuildMI(*LongBrMBB, Pos, DL,
|
|
|
|
TII->get(Subtarget.inMicroMipsMode() ? Mips::BC_MMR6 : Mips::BC))
|
|
|
|
.addMBB(TgtMBB);
|
|
|
|
else
|
|
|
|
MIBundleBuilder(*LongBrMBB, Pos)
|
|
|
|
.append(BuildMI(*MF, DL, TII->get(Mips::J)).addMBB(TgtMBB))
|
|
|
|
.append(BuildMI(*MF, DL, TII->get(Mips::NOP)));
|
2012-11-16 04:05:11 +08:00
|
|
|
|
|
|
|
assert(LongBrMBB->size() == LongBranchSeqSize);
|
2012-06-14 09:22:24 +08:00
|
|
|
}
|
|
|
|
|
2012-07-21 11:30:44 +08:00
|
|
|
if (I.Br->isUnconditionalBranch()) {
|
|
|
|
// Change branch destination.
|
|
|
|
assert(I.Br->getDesc().getNumOperands() == 1);
|
|
|
|
I.Br->RemoveOperand(0);
|
|
|
|
I.Br->addOperand(MachineOperand::CreateMBB(LongBrMBB));
|
|
|
|
} else
|
|
|
|
// Change branch destination and reverse condition.
|
2015-10-20 08:15:20 +08:00
|
|
|
replaceBranch(*MBB, I.Br, DL, &*FallThroughMBB);
|
2012-06-14 09:22:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void emitGPDisp(MachineFunction &F, const MipsInstrInfo *TII) {
|
|
|
|
MachineBasicBlock &MBB = F.front();
|
|
|
|
MachineBasicBlock::iterator I = MBB.begin();
|
|
|
|
DebugLoc DL = MBB.findDebugLoc(MBB.begin());
|
|
|
|
BuildMI(MBB, I, DL, TII->get(Mips::LUi), Mips::V0)
|
|
|
|
.addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI);
|
|
|
|
BuildMI(MBB, I, DL, TII->get(Mips::ADDiu), Mips::V0)
|
|
|
|
.addReg(Mips::V0).addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO);
|
|
|
|
MBB.removeLiveIn(Mips::V0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MipsLongBranch::runOnMachineFunction(MachineFunction &F) {
|
2015-01-30 07:27:36 +08:00
|
|
|
const MipsSubtarget &STI =
|
|
|
|
static_cast<const MipsSubtarget &>(F.getSubtarget());
|
2013-06-07 15:04:14 +08:00
|
|
|
const MipsInstrInfo *TII =
|
2015-01-30 07:27:36 +08:00
|
|
|
static_cast<const MipsInstrInfo *>(STI.getInstrInfo());
|
2017-05-19 01:21:13 +08:00
|
|
|
|
|
|
|
const TargetMachine& TM = F.getTarget();
|
|
|
|
IsPIC = TM.isPositionIndependent();
|
|
|
|
ABI = static_cast<const MipsTargetMachine &>(TM).getABI();
|
|
|
|
|
2017-12-14 22:55:25 +08:00
|
|
|
LongBranchSeqSize = IsPIC ? ((ABI.IsN64() || STI.isTargetNaCl()) ? 10 : 9)
|
|
|
|
: (STI.hasMips32r6() ? 1 : 2);
|
2013-06-07 15:04:14 +08:00
|
|
|
|
2014-07-19 04:29:02 +08:00
|
|
|
if (STI.inMips16Mode() || !STI.enableLongBranchPass())
|
2013-04-10 03:46:01 +08:00
|
|
|
return false;
|
2016-06-28 22:33:28 +08:00
|
|
|
if (IsPIC && static_cast<const MipsTargetMachine &>(TM).getABI().IsO32() &&
|
2012-06-14 09:22:24 +08:00
|
|
|
F.getInfo<MipsFunctionInfo>()->globalBaseRegSet())
|
|
|
|
emitGPDisp(F, TII);
|
|
|
|
|
|
|
|
if (SkipLongBranch)
|
2012-06-19 11:45:29 +08:00
|
|
|
return true;
|
2012-06-14 09:22:24 +08:00
|
|
|
|
|
|
|
MF = &F;
|
|
|
|
initMBBInfo();
|
|
|
|
|
2013-07-04 09:31:24 +08:00
|
|
|
SmallVectorImpl<MBBInfo>::iterator I, E = MBBInfos.end();
|
2012-06-14 09:22:24 +08:00
|
|
|
bool EverMadeChange = false, MadeChange = true;
|
|
|
|
|
|
|
|
while (MadeChange) {
|
|
|
|
MadeChange = false;
|
|
|
|
|
|
|
|
for (I = MBBInfos.begin(); I != E; ++I) {
|
|
|
|
// Skip if this MBB doesn't have a branch or the branch has already been
|
|
|
|
// converted to a long branch.
|
|
|
|
if (!I->Br || I->HasLongBranch)
|
|
|
|
continue;
|
|
|
|
|
2015-01-30 07:27:36 +08:00
|
|
|
int ShVal = STI.inMicroMipsMode() ? 2 : 4;
|
2014-06-05 21:52:08 +08:00
|
|
|
int64_t Offset = computeOffset(I->Br) / ShVal;
|
|
|
|
|
2015-01-30 07:27:36 +08:00
|
|
|
if (STI.isTargetNaCl()) {
|
2014-06-05 21:52:08 +08:00
|
|
|
// The offset calculation does not include sandboxing instructions
|
|
|
|
// that will be added later in the MC layer. Since at this point we
|
|
|
|
// don't know the exact amount of code that "sandboxing" will add, we
|
|
|
|
// conservatively estimate that code will not grow more than 100%.
|
|
|
|
Offset *= 2;
|
|
|
|
}
|
2013-12-01 03:12:28 +08:00
|
|
|
|
2012-08-28 11:03:05 +08:00
|
|
|
// Check if offset fits into 16-bit immediate field of branches.
|
2014-06-05 21:52:08 +08:00
|
|
|
if (!ForceLongBranch && isInt<16>(Offset))
|
2012-08-28 11:03:05 +08:00
|
|
|
continue;
|
2012-06-14 09:22:24 +08:00
|
|
|
|
2012-08-28 11:03:05 +08:00
|
|
|
I->HasLongBranch = true;
|
2012-08-29 02:58:57 +08:00
|
|
|
I->Size += LongBranchSeqSize * 4;
|
2012-06-14 09:22:24 +08:00
|
|
|
++LongBranches;
|
|
|
|
EverMadeChange = MadeChange = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-28 11:03:05 +08:00
|
|
|
if (!EverMadeChange)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Compute basic block addresses.
|
2016-06-28 22:33:28 +08:00
|
|
|
if (IsPIC) {
|
2012-08-28 11:03:05 +08:00
|
|
|
uint64_t Address = 0;
|
|
|
|
|
2012-08-29 02:58:57 +08:00
|
|
|
for (I = MBBInfos.begin(); I != E; Address += I->Size, ++I)
|
2012-08-28 11:03:05 +08:00
|
|
|
I->Address = Address;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the expansion.
|
|
|
|
for (I = MBBInfos.begin(); I != E; ++I)
|
|
|
|
if (I->HasLongBranch)
|
|
|
|
expandToLongBranch(*I);
|
|
|
|
|
|
|
|
MF->RenumberBlocks();
|
2012-06-14 09:22:24 +08:00
|
|
|
|
2012-06-19 11:45:29 +08:00
|
|
|
return true;
|
2012-06-14 09:22:24 +08:00
|
|
|
}
|
2017-02-01 09:22:51 +08:00
|
|
|
|
|
|
|
/// createMipsLongBranchPass - Returns a pass that converts branches to long
|
|
|
|
/// branches.
|
2017-05-19 01:21:13 +08:00
|
|
|
FunctionPass *llvm::createMipsLongBranchPass() { return new MipsLongBranch(); }
|