Skip to content

Commit 999119d

Browse files
committed
[M68k] add 32 bit branch instrs and allow x20 and later CPUs to relax to 32 bit versions
1 parent 5cd6e21 commit 999119d

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

llvm/lib/Target/M68k/M68kInstrControl.td

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ foreach cc = [ "cc", "ls", "lt", "eq", "mi", "ne", "ge",
190190
def B#cc#"16"
191191
: MxBcc<cc, MxBrTarget16, (descend 0b0000, 0b0000),
192192
(operand "$dst", 16, (encoder "encodePCRelImm<16>"))>;
193+
194+
let Predicates = [AtLeastM68020] in {
195+
def B#cc#"32"
196+
: MxBcc<cc, MxBrTarget32, (descend 0b1111, 0b1111),
197+
(operand "$dst", 16, (encoder "encodePCRelImm<32>"))>;
198+
} // AtLeastM68020
193199
}
194200

195201
foreach cc = [ "cc", "ls", "lt", "eq", "mi", "ne", "ge",
@@ -223,6 +229,12 @@ def BRA8 : MxBra<MxBrTarget8,
223229
def BRA16 : MxBra<MxBrTarget16, (descend 0b0000, 0b0000),
224230
(operand "$dst", 16, (encoder "encodePCRelImm<16>"))>;
225231

232+
let Predicates = [AtLeastM68020] in {
233+
def BRA32 : MxBra<MxBrTarget16, (descend 0b1111, 0b1111),
234+
(operand "$dst", 32, (encoder "encodePCRelImm<32>"),
235+
(decoder "DecodeImm32"))>;
236+
} // AtLeastM68020
237+
226238
def : Pat<(br bb:$target), (BRA8 MxBrTarget8:$target)>;
227239

228240
/// -------------------------------------------------
@@ -250,9 +262,11 @@ def BSR8 : MxBsr<MxBrTarget8, MxType8,
250262
def BSR16 : MxBsr<MxBrTarget16, MxType16, (descend 0b0000, 0b0000),
251263
(operand "$dst", 16, (encoder "encodePCRelImm<16>"))>;
252264

265+
let Predicates = [AtLeastM68020] in {
253266
def BSR32 : MxBsr<MxBrTarget32, MxType32, (descend 0b1111, 0b1111),
254267
(operand "$dst", 32, (encoder "encodePCRelImm<32>"),
255268
(decoder "DecodeImm32"))>;
269+
} // AtLeastM68020
256270

257271
//===----------------------------------------------------------------------===//
258272
// Call

llvm/lib/Target/M68k/MCTargetDesc/M68kAsmBackend.cpp

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@ using namespace llvm;
3939
namespace {
4040

4141
class M68kAsmBackend : public MCAsmBackend {
42+
bool Allows32BitBranch;
4243

4344
public:
44-
M68kAsmBackend(const Target &T) : MCAsmBackend(llvm::endianness::big) {}
45+
M68kAsmBackend(const Target &T, const MCSubtargetInfo &STI)
46+
: MCAsmBackend(llvm::endianness::big),
47+
Allows32BitBranch(llvm::StringSwitch<bool>(STI.getCPU())
48+
.CasesLower("m68020", "m68030", "m68040", true)
49+
.Default(false)) {}
4550

4651
unsigned getNumFixupKinds() const override { return 0; }
4752

@@ -129,6 +134,36 @@ static unsigned getRelaxedOpcodeBranch(const MCInst &Inst) {
129134
return M68k::Ble16;
130135
case M68k::Bvs8:
131136
return M68k::Bvs16;
137+
case M68k::BRA16:
138+
return M68k::BRA32;
139+
case M68k::Bcc16:
140+
return M68k::Bcc32;
141+
case M68k::Bls16:
142+
return M68k::Bls32;
143+
case M68k::Blt16:
144+
return M68k::Blt32;
145+
case M68k::Beq16:
146+
return M68k::Beq32;
147+
case M68k::Bmi16:
148+
return M68k::Bmi32;
149+
case M68k::Bne16:
150+
return M68k::Bne32;
151+
case M68k::Bge16:
152+
return M68k::Bge32;
153+
case M68k::Bcs16:
154+
return M68k::Bcs32;
155+
case M68k::Bpl16:
156+
return M68k::Bpl32;
157+
case M68k::Bgt16:
158+
return M68k::Bgt32;
159+
case M68k::Bhi16:
160+
return M68k::Bhi32;
161+
case M68k::Bvc16:
162+
return M68k::Bvc32;
163+
case M68k::Ble16:
164+
return M68k::Ble32;
165+
case M68k::Bvs16:
166+
return M68k::Bvs32;
132167
}
133168
}
134169

@@ -168,7 +203,7 @@ bool M68kAsmBackend::mayNeedRelaxation(const MCInst &Inst,
168203
bool M68kAsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup,
169204
uint64_t Value) const {
170205
// TODO Newer CPU can use 32 bit offsets, so check for this when ready
171-
if (!isInt<16>(Value)) {
206+
if (!isInt<32>(Value) || (!Allows32BitBranch && !isInt<16>(Value))) {
172207
llvm_unreachable("Cannot relax the instruction, value does not fit");
173208
}
174209
// Relax if the value is too big for a (signed) i8. This means that byte-wide
@@ -178,7 +213,13 @@ bool M68kAsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup,
178213
// A branch to the immediately following instruction automatically
179214
// uses the 16-bit displacement format because the 8-bit
180215
// displacement field contains $00 (zero offset).
181-
return Value == 0 || !isInt<8>(Value);
216+
unsigned int KindLog2Size = getFixupKindLog2Size(Fixup.getKind());
217+
bool FixupFieldTooSmall = false;
218+
if (!isInt<8>(Value) && KindLog2Size == 0)
219+
FixupFieldTooSmall |= true;
220+
if (!isInt<16>(Value) && KindLog2Size <= 1)
221+
FixupFieldTooSmall |= true;
222+
return Value == 0 || FixupFieldTooSmall;
182223
}
183224

184225
// NOTE Can tblgen help at all here to verify there aren't other instructions
@@ -218,8 +259,8 @@ namespace {
218259
class M68kELFAsmBackend : public M68kAsmBackend {
219260
public:
220261
uint8_t OSABI;
221-
M68kELFAsmBackend(const Target &T, uint8_t OSABI)
222-
: M68kAsmBackend(T), OSABI(OSABI) {}
262+
M68kELFAsmBackend(const Target &T, const MCSubtargetInfo &STI, uint8_t OSABI)
263+
: M68kAsmBackend(T, STI), OSABI(OSABI) {}
223264

224265
std::unique_ptr<MCObjectTargetWriter>
225266
createObjectTargetWriter() const override {
@@ -235,5 +276,5 @@ MCAsmBackend *llvm::createM68kAsmBackend(const Target &T,
235276
const MCTargetOptions &Options) {
236277
const Triple &TheTriple = STI.getTargetTriple();
237278
uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS());
238-
return new M68kELFAsmBackend(T, OSABI);
279+
return new M68kELFAsmBackend(T, STI, OSABI);
239280
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
; RUN: llvm-mc -triple=m68k --mcpu=m68020 -motorola-integers -filetype=obj < %s \
2+
; RUN: | llvm-objdump -d - | FileCheck %s
3+
4+
; CHECK-LABEL: <RELAXED_32>:
5+
RELAXED_32:
6+
; CHECK: bra $ff
7+
bra .LBB3_1
8+
.space 0x20000 ; Greater than u16::MAX.
9+
.LBB3_1:
10+
add.l #0, %d0
11+
rts

0 commit comments

Comments
 (0)