[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
//===-- SystemZLongBranch.cpp - Branch lengthening for SystemZ ------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2013-08-05 18:58:53 +08:00
|
|
|
// This pass makes sure that all branches are in range. There are several ways
|
|
|
|
// in which this could be done. One aggressive approach is to assume that all
|
|
|
|
// branches are in range and successively replace those that turn out not
|
|
|
|
// to be in range with a longer form (branch relaxation). A simple
|
|
|
|
// implementation is to continually walk through the function relaxing
|
|
|
|
// branches until no more changes are needed and a fixed point is reached.
|
|
|
|
// However, in the pathological worst case, this implementation is
|
|
|
|
// quadratic in the number of blocks; relaxing branch N can make branch N-1
|
|
|
|
// go out of range, which in turn can make branch N-2 go out of range,
|
|
|
|
// and so on.
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
//
|
|
|
|
// An alternative approach is to assume that all branches must be
|
|
|
|
// converted to their long forms, then reinstate the short forms of
|
|
|
|
// branches that, even under this pessimistic assumption, turn out to be
|
|
|
|
// in range (branch shortening). This too can be implemented as a function
|
|
|
|
// walk that is repeated until a fixed point is reached. In general,
|
|
|
|
// the result of shortening is not as good as that of relaxation, and
|
|
|
|
// shortening is also quadratic in the worst case; shortening branch N
|
|
|
|
// can bring branch N-1 in range of the short form, which in turn can do
|
|
|
|
// the same for branch N-2, and so on. The main advantage of shortening
|
|
|
|
// is that each walk through the function produces valid code, so it is
|
|
|
|
// possible to stop at any point after the first walk. The quadraticness
|
|
|
|
// could therefore be handled with a maximum pass count, although the
|
|
|
|
// question then becomes: what maximum count should be used?
|
|
|
|
//
|
|
|
|
// On SystemZ, long branches are only needed for functions bigger than 64k,
|
|
|
|
// which are relatively rare to begin with, and the long branch sequences
|
|
|
|
// are actually relatively cheap. It therefore doesn't seem worth spending
|
|
|
|
// much compilation time on the problem. Instead, the approach we take is:
|
|
|
|
//
|
2013-05-22 17:57:57 +08:00
|
|
|
// (1) Work out the address that each block would have if no branches
|
|
|
|
// need relaxing. Exit the pass early if all branches are in range
|
|
|
|
// according to this assumption.
|
|
|
|
//
|
|
|
|
// (2) Work out the address that each block would have if all branches
|
|
|
|
// need relaxing.
|
|
|
|
//
|
|
|
|
// (3) Walk through the block calculating the final address of each instruction
|
|
|
|
// and relaxing those that need to be relaxed. For backward branches,
|
|
|
|
// this check uses the final address of the target block, as calculated
|
|
|
|
// earlier in the walk. For forward branches, this check uses the
|
|
|
|
// address of the target block that was calculated in (2). Both checks
|
|
|
|
// give a conservatively-correct range.
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SystemZTargetMachine.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "systemz-long-branch"
|
|
|
|
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
STATISTIC(LongBranches, "Number of long branches.");
|
|
|
|
|
|
|
|
namespace {
|
2014-03-06 18:38:30 +08:00
|
|
|
// Represents positional information about a basic block.
|
|
|
|
struct MBBInfo {
|
|
|
|
// The address that we currently assume the block has.
|
|
|
|
uint64_t Address;
|
|
|
|
|
|
|
|
// The size of the block in bytes, excluding terminators.
|
|
|
|
// This value never changes.
|
|
|
|
uint64_t Size;
|
|
|
|
|
|
|
|
// The minimum alignment of the block, as a log2 value.
|
|
|
|
// This value never changes.
|
|
|
|
unsigned Alignment;
|
|
|
|
|
|
|
|
// The number of terminators in this block. This value never changes.
|
|
|
|
unsigned NumTerminators;
|
|
|
|
|
|
|
|
MBBInfo()
|
|
|
|
: Address(0), Size(0), Alignment(0), NumTerminators(0) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Represents the state of a block terminator.
|
|
|
|
struct TerminatorInfo {
|
|
|
|
// If this terminator is a relaxable branch, this points to the branch
|
|
|
|
// instruction, otherwise it is null.
|
|
|
|
MachineInstr *Branch;
|
|
|
|
|
|
|
|
// The address that we currently assume the terminator has.
|
|
|
|
uint64_t Address;
|
|
|
|
|
|
|
|
// The current size of the terminator in bytes.
|
|
|
|
uint64_t Size;
|
|
|
|
|
|
|
|
// If Branch is nonnull, this is the number of the target block,
|
|
|
|
// otherwise it is unused.
|
|
|
|
unsigned TargetBlock;
|
|
|
|
|
|
|
|
// If Branch is nonnull, this is the length of the longest relaxed form,
|
|
|
|
// otherwise it is zero.
|
|
|
|
unsigned ExtraRelaxSize;
|
|
|
|
|
2014-04-25 13:30:21 +08:00
|
|
|
TerminatorInfo() : Branch(nullptr), Size(0), TargetBlock(0),
|
|
|
|
ExtraRelaxSize(0) {}
|
2014-03-06 18:38:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Used to keep track of the current position while iterating over the blocks.
|
|
|
|
struct BlockPosition {
|
|
|
|
// The address that we assume this position has.
|
|
|
|
uint64_t Address;
|
|
|
|
|
|
|
|
// The number of low bits in Address that are known to be the same
|
|
|
|
// as the runtime address.
|
|
|
|
unsigned KnownBits;
|
|
|
|
|
|
|
|
BlockPosition(unsigned InitialAlignment)
|
|
|
|
: Address(0), KnownBits(InitialAlignment) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SystemZLongBranch : public MachineFunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
SystemZLongBranch(const SystemZTargetMachine &tm)
|
2014-04-25 13:30:21 +08:00
|
|
|
: MachineFunctionPass(ID), TII(nullptr) {}
|
2014-03-06 18:38:30 +08:00
|
|
|
|
2014-03-06 20:03:36 +08:00
|
|
|
const char *getPassName() const override {
|
2014-03-06 18:38:30 +08:00
|
|
|
return "SystemZ Long Branch";
|
|
|
|
}
|
|
|
|
|
2014-04-29 15:58:41 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &F) override;
|
2014-03-06 18:38:30 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void skipNonTerminators(BlockPosition &Position, MBBInfo &Block);
|
|
|
|
void skipTerminator(BlockPosition &Position, TerminatorInfo &Terminator,
|
|
|
|
bool AssumeRelaxed);
|
|
|
|
TerminatorInfo describeTerminator(MachineInstr *MI);
|
|
|
|
uint64_t initMBBInfo();
|
|
|
|
bool mustRelaxBranch(const TerminatorInfo &Terminator, uint64_t Address);
|
|
|
|
bool mustRelaxABranch();
|
|
|
|
void setWorstCaseAddresses();
|
|
|
|
void splitBranchOnCount(MachineInstr *MI, unsigned AddOpcode);
|
|
|
|
void splitCompareBranch(MachineInstr *MI, unsigned CompareOpcode);
|
|
|
|
void relaxBranch(TerminatorInfo &Terminator);
|
|
|
|
void relaxBranches();
|
|
|
|
|
|
|
|
const SystemZInstrInfo *TII;
|
|
|
|
MachineFunction *MF;
|
|
|
|
SmallVector<MBBInfo, 16> MBBs;
|
|
|
|
SmallVector<TerminatorInfo, 16> Terminators;
|
|
|
|
};
|
|
|
|
|
|
|
|
char SystemZLongBranch::ID = 0;
|
|
|
|
|
|
|
|
const uint64_t MaxBackwardRange = 0x10000;
|
|
|
|
const uint64_t MaxForwardRange = 0xfffe;
|
|
|
|
} // end anonymous namespace
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
|
|
|
|
FunctionPass *llvm::createSystemZLongBranchPass(SystemZTargetMachine &TM) {
|
|
|
|
return new SystemZLongBranch(TM);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Position describes the state immediately before Block. Update Block
|
|
|
|
// accordingly and move Position to the end of the block's non-terminator
|
|
|
|
// instructions.
|
|
|
|
void SystemZLongBranch::skipNonTerminators(BlockPosition &Position,
|
|
|
|
MBBInfo &Block) {
|
|
|
|
if (Block.Alignment > Position.KnownBits) {
|
|
|
|
// When calculating the address of Block, we need to conservatively
|
|
|
|
// assume that Block had the worst possible misalignment.
|
|
|
|
Position.Address += ((uint64_t(1) << Block.Alignment) -
|
|
|
|
(uint64_t(1) << Position.KnownBits));
|
|
|
|
Position.KnownBits = Block.Alignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Align the addresses.
|
|
|
|
uint64_t AlignMask = (uint64_t(1) << Block.Alignment) - 1;
|
|
|
|
Position.Address = (Position.Address + AlignMask) & ~AlignMask;
|
|
|
|
|
|
|
|
// Record the block's position.
|
|
|
|
Block.Address = Position.Address;
|
|
|
|
|
|
|
|
// Move past the non-terminators in the block.
|
|
|
|
Position.Address += Block.Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Position describes the state immediately before Terminator.
|
|
|
|
// Update Terminator accordingly and move Position past it.
|
|
|
|
// Assume that Terminator will be relaxed if AssumeRelaxed.
|
|
|
|
void SystemZLongBranch::skipTerminator(BlockPosition &Position,
|
|
|
|
TerminatorInfo &Terminator,
|
|
|
|
bool AssumeRelaxed) {
|
|
|
|
Terminator.Address = Position.Address;
|
|
|
|
Position.Address += Terminator.Size;
|
|
|
|
if (AssumeRelaxed)
|
|
|
|
Position.Address += Terminator.ExtraRelaxSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a description of terminator instruction MI.
|
|
|
|
TerminatorInfo SystemZLongBranch::describeTerminator(MachineInstr *MI) {
|
|
|
|
TerminatorInfo Terminator;
|
|
|
|
Terminator.Size = TII->getInstSizeInBytes(MI);
|
|
|
|
if (MI->isConditionalBranch() || MI->isUnconditionalBranch()) {
|
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
case SystemZ::J:
|
|
|
|
// Relaxes to JG, which is 2 bytes longer.
|
|
|
|
Terminator.ExtraRelaxSize = 2;
|
|
|
|
break;
|
|
|
|
case SystemZ::BRC:
|
2013-05-28 18:13:54 +08:00
|
|
|
// Relaxes to BRCL, which is 2 bytes longer.
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
Terminator.ExtraRelaxSize = 2;
|
|
|
|
break;
|
2013-08-05 19:23:46 +08:00
|
|
|
case SystemZ::BRCT:
|
|
|
|
case SystemZ::BRCTG:
|
|
|
|
// Relaxes to A(G)HI and BRCL, which is 6 bytes longer.
|
|
|
|
Terminator.ExtraRelaxSize = 6;
|
|
|
|
break;
|
2013-05-28 18:41:11 +08:00
|
|
|
case SystemZ::CRJ:
|
2013-09-18 17:56:40 +08:00
|
|
|
case SystemZ::CLRJ:
|
|
|
|
// Relaxes to a C(L)R/BRCL sequence, which is 2 bytes longer.
|
2013-05-28 18:41:11 +08:00
|
|
|
Terminator.ExtraRelaxSize = 2;
|
|
|
|
break;
|
|
|
|
case SystemZ::CGRJ:
|
2013-09-18 17:56:40 +08:00
|
|
|
case SystemZ::CLGRJ:
|
|
|
|
// Relaxes to a C(L)GR/BRCL sequence, which is 4 bytes longer.
|
2013-05-28 18:41:11 +08:00
|
|
|
Terminator.ExtraRelaxSize = 4;
|
|
|
|
break;
|
2013-05-29 19:58:52 +08:00
|
|
|
case SystemZ::CIJ:
|
|
|
|
case SystemZ::CGIJ:
|
|
|
|
// Relaxes to a C(G)HI/BRCL sequence, which is 4 bytes longer.
|
|
|
|
Terminator.ExtraRelaxSize = 4;
|
|
|
|
break;
|
2013-09-18 17:56:40 +08:00
|
|
|
case SystemZ::CLIJ:
|
|
|
|
case SystemZ::CLGIJ:
|
|
|
|
// Relaxes to a CL(G)FI/BRCL sequence, which is 6 bytes longer.
|
|
|
|
Terminator.ExtraRelaxSize = 6;
|
|
|
|
break;
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Unrecognized branch instruction");
|
|
|
|
}
|
2013-05-28 18:13:54 +08:00
|
|
|
Terminator.Branch = MI;
|
|
|
|
Terminator.TargetBlock =
|
|
|
|
TII->getBranchInfo(MI).Target->getMBB()->getNumber();
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
}
|
|
|
|
return Terminator;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill MBBs and Terminators, setting the addresses on the assumption
|
|
|
|
// that no branches need relaxation. Return the size of the function under
|
|
|
|
// this assumption.
|
|
|
|
uint64_t SystemZLongBranch::initMBBInfo() {
|
|
|
|
MF->RenumberBlocks();
|
|
|
|
unsigned NumBlocks = MF->size();
|
|
|
|
|
|
|
|
MBBs.clear();
|
|
|
|
MBBs.resize(NumBlocks);
|
|
|
|
|
|
|
|
Terminators.clear();
|
|
|
|
Terminators.reserve(NumBlocks);
|
|
|
|
|
|
|
|
BlockPosition Position(MF->getAlignment());
|
|
|
|
for (unsigned I = 0; I < NumBlocks; ++I) {
|
|
|
|
MachineBasicBlock *MBB = MF->getBlockNumbered(I);
|
|
|
|
MBBInfo &Block = MBBs[I];
|
|
|
|
|
|
|
|
// Record the alignment, for quick access.
|
|
|
|
Block.Alignment = MBB->getAlignment();
|
|
|
|
|
|
|
|
// Calculate the size of the fixed part of the block.
|
|
|
|
MachineBasicBlock::iterator MI = MBB->begin();
|
|
|
|
MachineBasicBlock::iterator End = MBB->end();
|
|
|
|
while (MI != End && !MI->isTerminator()) {
|
2013-08-05 18:58:53 +08:00
|
|
|
Block.Size += TII->getInstSizeInBytes(MI);
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
++MI;
|
|
|
|
}
|
|
|
|
skipNonTerminators(Position, Block);
|
|
|
|
|
|
|
|
// Add the terminators.
|
|
|
|
while (MI != End) {
|
|
|
|
if (!MI->isDebugValue()) {
|
|
|
|
assert(MI->isTerminator() && "Terminator followed by non-terminator");
|
|
|
|
Terminators.push_back(describeTerminator(MI));
|
|
|
|
skipTerminator(Position, Terminators.back(), false);
|
|
|
|
++Block.NumTerminators;
|
|
|
|
}
|
|
|
|
++MI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Position.Address;
|
|
|
|
}
|
|
|
|
|
2013-05-22 17:57:57 +08:00
|
|
|
// Return true if, under current assumptions, Terminator would need to be
|
|
|
|
// relaxed if it were placed at address Address.
|
|
|
|
bool SystemZLongBranch::mustRelaxBranch(const TerminatorInfo &Terminator,
|
|
|
|
uint64_t Address) {
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
if (!Terminator.Branch)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const MBBInfo &Target = MBBs[Terminator.TargetBlock];
|
2013-05-22 17:57:57 +08:00
|
|
|
if (Address >= Target.Address) {
|
|
|
|
if (Address - Target.Address <= MaxBackwardRange)
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
return false;
|
|
|
|
} else {
|
2013-05-22 17:57:57 +08:00
|
|
|
if (Target.Address - Address <= MaxForwardRange)
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if, under current assumptions, any terminator needs
|
|
|
|
// to be relaxed.
|
|
|
|
bool SystemZLongBranch::mustRelaxABranch() {
|
2014-03-06 19:00:15 +08:00
|
|
|
for (auto &Terminator : Terminators)
|
|
|
|
if (mustRelaxBranch(Terminator, Terminator.Address))
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the address of each block on the assumption that all branches
|
|
|
|
// must be long.
|
|
|
|
void SystemZLongBranch::setWorstCaseAddresses() {
|
|
|
|
SmallVector<TerminatorInfo, 16>::iterator TI = Terminators.begin();
|
|
|
|
BlockPosition Position(MF->getAlignment());
|
2014-03-06 19:00:15 +08:00
|
|
|
for (auto &Block : MBBs) {
|
|
|
|
skipNonTerminators(Position, Block);
|
|
|
|
for (unsigned BTI = 0, BTE = Block.NumTerminators; BTI != BTE; ++BTI) {
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
skipTerminator(Position, *TI, true);
|
|
|
|
++TI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 19:23:46 +08:00
|
|
|
// Split BRANCH ON COUNT MI into the addition given by AddOpcode followed
|
|
|
|
// by a BRCL on the result.
|
|
|
|
void SystemZLongBranch::splitBranchOnCount(MachineInstr *MI,
|
|
|
|
unsigned AddOpcode) {
|
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
DebugLoc DL = MI->getDebugLoc();
|
|
|
|
BuildMI(*MBB, MI, DL, TII->get(AddOpcode))
|
|
|
|
.addOperand(MI->getOperand(0))
|
|
|
|
.addOperand(MI->getOperand(1))
|
|
|
|
.addImm(-1);
|
|
|
|
MachineInstr *BRCL = BuildMI(*MBB, MI, DL, TII->get(SystemZ::BRCL))
|
|
|
|
.addImm(SystemZ::CCMASK_ICMP)
|
|
|
|
.addImm(SystemZ::CCMASK_CMP_NE)
|
|
|
|
.addOperand(MI->getOperand(2));
|
|
|
|
// The implicit use of CC is a killing use.
|
|
|
|
BRCL->addRegisterKilled(SystemZ::CC, &TII->getRegisterInfo());
|
|
|
|
MI->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2013-05-28 18:41:11 +08:00
|
|
|
// Split MI into the comparison given by CompareOpcode followed
|
|
|
|
// a BRCL on the result.
|
|
|
|
void SystemZLongBranch::splitCompareBranch(MachineInstr *MI,
|
|
|
|
unsigned CompareOpcode) {
|
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
DebugLoc DL = MI->getDebugLoc();
|
|
|
|
BuildMI(*MBB, MI, DL, TII->get(CompareOpcode))
|
|
|
|
.addOperand(MI->getOperand(0))
|
|
|
|
.addOperand(MI->getOperand(1));
|
|
|
|
MachineInstr *BRCL = BuildMI(*MBB, MI, DL, TII->get(SystemZ::BRCL))
|
[SystemZ] Be more careful about inverting CC masks
System z branches have a mask to select which of the 4 CC values should
cause the branch to be taken. We can invert a branch by inverting the mask.
However, not all instructions can produce all 4 CC values, so inverting
the branch like this can lead to some oddities. For example, integer
comparisons only produce a CC of 0 (equal), 1 (less) or 2 (greater).
If an integer EQ is reversed to NE before instruction selection,
the branch will test for 1 or 2. If instead the branch is reversed
after instruction selection (by inverting the mask), it will test for
1, 2 or 3. Both are correct, but the second isn't really canonical.
This patch therefore keeps track of which CC values are possible
and uses this when inverting a mask.
Although this is mostly cosmestic, it fixes undefined behavior
for the CIJNLH in branch-08.ll. Another fix would have been
to mask out bit 0 when generating the fused compare and branch,
but the point of this patch is that we shouldn't need to do that
in the first place.
The patch also makes it easier to reuse CC results from other instructions.
llvm-svn: 187495
2013-07-31 20:30:20 +08:00
|
|
|
.addImm(SystemZ::CCMASK_ICMP)
|
2013-05-28 18:41:11 +08:00
|
|
|
.addOperand(MI->getOperand(2))
|
|
|
|
.addOperand(MI->getOperand(3));
|
|
|
|
// The implicit use of CC is a killing use.
|
[SystemZ] Be more careful about inverting CC masks
System z branches have a mask to select which of the 4 CC values should
cause the branch to be taken. We can invert a branch by inverting the mask.
However, not all instructions can produce all 4 CC values, so inverting
the branch like this can lead to some oddities. For example, integer
comparisons only produce a CC of 0 (equal), 1 (less) or 2 (greater).
If an integer EQ is reversed to NE before instruction selection,
the branch will test for 1 or 2. If instead the branch is reversed
after instruction selection (by inverting the mask), it will test for
1, 2 or 3. Both are correct, but the second isn't really canonical.
This patch therefore keeps track of which CC values are possible
and uses this when inverting a mask.
Although this is mostly cosmestic, it fixes undefined behavior
for the CIJNLH in branch-08.ll. Another fix would have been
to mask out bit 0 when generating the fused compare and branch,
but the point of this patch is that we shouldn't need to do that
in the first place.
The patch also makes it easier to reuse CC results from other instructions.
llvm-svn: 187495
2013-07-31 20:30:20 +08:00
|
|
|
BRCL->addRegisterKilled(SystemZ::CC, &TII->getRegisterInfo());
|
2013-05-28 18:41:11 +08:00
|
|
|
MI->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
// Relax the branch described by Terminator.
|
|
|
|
void SystemZLongBranch::relaxBranch(TerminatorInfo &Terminator) {
|
|
|
|
MachineInstr *Branch = Terminator.Branch;
|
|
|
|
switch (Branch->getOpcode()) {
|
2013-05-21 16:48:24 +08:00
|
|
|
case SystemZ::J:
|
|
|
|
Branch->setDesc(TII->get(SystemZ::JG));
|
|
|
|
break;
|
|
|
|
case SystemZ::BRC:
|
|
|
|
Branch->setDesc(TII->get(SystemZ::BRCL));
|
|
|
|
break;
|
2013-08-05 19:23:46 +08:00
|
|
|
case SystemZ::BRCT:
|
|
|
|
splitBranchOnCount(Branch, SystemZ::AHI);
|
|
|
|
break;
|
|
|
|
case SystemZ::BRCTG:
|
|
|
|
splitBranchOnCount(Branch, SystemZ::AGHI);
|
|
|
|
break;
|
2013-05-28 18:41:11 +08:00
|
|
|
case SystemZ::CRJ:
|
|
|
|
splitCompareBranch(Branch, SystemZ::CR);
|
|
|
|
break;
|
|
|
|
case SystemZ::CGRJ:
|
|
|
|
splitCompareBranch(Branch, SystemZ::CGR);
|
|
|
|
break;
|
2013-05-29 19:58:52 +08:00
|
|
|
case SystemZ::CIJ:
|
|
|
|
splitCompareBranch(Branch, SystemZ::CHI);
|
|
|
|
break;
|
|
|
|
case SystemZ::CGIJ:
|
|
|
|
splitCompareBranch(Branch, SystemZ::CGHI);
|
|
|
|
break;
|
2013-09-18 17:56:40 +08:00
|
|
|
case SystemZ::CLRJ:
|
|
|
|
splitCompareBranch(Branch, SystemZ::CLR);
|
|
|
|
break;
|
|
|
|
case SystemZ::CLGRJ:
|
|
|
|
splitCompareBranch(Branch, SystemZ::CLGR);
|
|
|
|
break;
|
|
|
|
case SystemZ::CLIJ:
|
|
|
|
splitCompareBranch(Branch, SystemZ::CLFI);
|
|
|
|
break;
|
|
|
|
case SystemZ::CLGIJ:
|
|
|
|
splitCompareBranch(Branch, SystemZ::CLGFI);
|
|
|
|
break;
|
2013-05-21 16:48:24 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Unrecognized branch");
|
|
|
|
}
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
|
|
|
|
Terminator.Size += Terminator.ExtraRelaxSize;
|
|
|
|
Terminator.ExtraRelaxSize = 0;
|
2014-04-25 13:30:21 +08:00
|
|
|
Terminator.Branch = nullptr;
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
|
|
|
|
++LongBranches;
|
|
|
|
}
|
|
|
|
|
2013-05-22 17:57:57 +08:00
|
|
|
// Run a shortening pass and relax any branches that need to be relaxed.
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
void SystemZLongBranch::relaxBranches() {
|
2013-05-22 17:57:57 +08:00
|
|
|
SmallVector<TerminatorInfo, 16>::iterator TI = Terminators.begin();
|
|
|
|
BlockPosition Position(MF->getAlignment());
|
2014-03-06 19:00:15 +08:00
|
|
|
for (auto &Block : MBBs) {
|
|
|
|
skipNonTerminators(Position, Block);
|
|
|
|
for (unsigned BTI = 0, BTE = Block.NumTerminators; BTI != BTE; ++BTI) {
|
2013-05-22 17:57:57 +08:00
|
|
|
assert(Position.Address <= TI->Address &&
|
|
|
|
"Addresses shouldn't go forwards");
|
|
|
|
if (mustRelaxBranch(*TI, Position.Address))
|
|
|
|
relaxBranch(*TI);
|
|
|
|
skipTerminator(Position, *TI, false);
|
|
|
|
++TI;
|
|
|
|
}
|
|
|
|
}
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SystemZLongBranch::runOnMachineFunction(MachineFunction &F) {
|
2014-08-05 10:39:49 +08:00
|
|
|
TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo());
|
[SystemZ] Add long branch pass
Before this change, the SystemZ backend would use BRCL for all branches
and only consider shortening them to BRC when generating an object file.
E.g. a branch on equal would use the JGE alias of BRCL in assembly output,
but might be shortened to the JE alias of BRC in ELF output. This was
a useful first step, but it had two problems:
(1) The z assembler isn't traditionally supposed to perform branch shortening
or branch relaxation. We followed this rule by not relaxing branches
in assembler input, but that meant that generating assembly code and
then assembling it would not produce the same result as going directly
to object code; the former would give long branches everywhere, whereas
the latter would use short branches where possible.
(2) Other useful branches, like COMPARE AND BRANCH, do not have long forms.
We would need to do something else before supporting them.
(Although COMPARE AND BRANCH does not change the condition codes,
the plan is to model COMPARE AND BRANCH as a CC-clobbering instruction
during codegen, so that we can safely lower it to a separate compare
and long branch where necessary. This is not a valid transformation
for the assembler proper to make.)
This patch therefore moves branch relaxation to a pre-emit pass.
For now, calls are still shortened from BRASL to BRAS by the assembler,
although this too is not really the traditional behaviour.
The first test takes about 1.5s to run, and there are likely to be
more tests in this vein once further branch types are added. The feeling
on IRC was that 1.5s is a bit much for a single test, so I've restricted
it to SystemZ hosts for now.
The patch exposes (and fixes) some typos in the main CodeGen/SystemZ tests.
A later patch will remove the {{g}}s from that directory.
llvm-svn: 182274
2013-05-20 22:23:08 +08:00
|
|
|
MF = &F;
|
|
|
|
uint64_t Size = initMBBInfo();
|
|
|
|
if (Size <= MaxForwardRange || !mustRelaxABranch())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
setWorstCaseAddresses();
|
|
|
|
relaxBranches();
|
|
|
|
return true;
|
|
|
|
}
|