-
Notifications
You must be signed in to change notification settings - Fork 53
/
0079-RISCV-Allow-RISCVAsmBackend-writeNopData-to-generate.patch
108 lines (97 loc) · 3.63 KB
/
0079-RISCV-Allow-RISCVAsmBackend-writeNopData-to-generate.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Alex Bradbury <[email protected]>
Subject: [RISCV] Allow RISCVAsmBackend::writeNopData to generate c.nop when
supported
When the compressed instruction set is enabled, the 16-bit c.nop can be
generated if necessary.
Differential Revision: https://reviews.llvm.org/D41221
Patch by Shiva Chen.
---
lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp | 26 ++++++++++++++++-------
test/MC/RISCV/cnop.s | 26 +++++++++++++++++++++++
2 files changed, 44 insertions(+), 8 deletions(-)
create mode 100644 test/MC/RISCV/cnop.s
diff --git a/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp b/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
index 6e06a4975e2..3dcd36f1b71 100644
--- a/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
+++ b/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
@@ -27,12 +27,13 @@ using namespace llvm;
namespace {
class RISCVAsmBackend : public MCAsmBackend {
+ const MCSubtargetInfo &STI;
uint8_t OSABI;
bool Is64Bit;
public:
- RISCVAsmBackend(uint8_t OSABI, bool Is64Bit)
- : MCAsmBackend(), OSABI(OSABI), Is64Bit(Is64Bit) {}
+ RISCVAsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI, bool Is64Bit)
+ : MCAsmBackend(), STI(STI), OSABI(OSABI), Is64Bit(Is64Bit) {}
~RISCVAsmBackend() override {}
void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
@@ -88,15 +89,24 @@ public:
};
bool RISCVAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
- // Once support for the compressed instruction set is added, we will be able
- // to conditionally support 16-bit NOPs
- if ((Count % 4) != 0)
+ bool HasStdExtC = STI.getFeatureBits()[RISCV::FeatureStdExtC];
+ unsigned MinNopLen = HasStdExtC ? 2 : 4;
+
+ if ((Count % MinNopLen) != 0)
return false;
- // The canonical nop on RISC-V is addi x0, x0, 0
- for (uint64_t i = 0; i < Count; i += 4)
+ // The canonical nop on RISC-V is addi x0, x0, 0.
+ uint64_t Nop32Count = Count / 4;
+ for (uint64_t i = Nop32Count; i != 0; --i)
OW->write32(0x13);
+ // The canonical nop on RVC is c.nop.
+ if (HasStdExtC) {
+ uint64_t Nop16Count = (Count - Nop32Count * 4) / 2;
+ for (uint64_t i = Nop16Count; i != 0; --i)
+ OW->write16(0x01);
+ }
+
return true;
}
@@ -235,5 +245,5 @@ MCAsmBackend *llvm::createRISCVAsmBackend(const Target &T,
const MCTargetOptions &Options) {
const Triple &TT = STI.getTargetTriple();
uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
- return new RISCVAsmBackend(OSABI, TT.isArch64Bit());
+ return new RISCVAsmBackend(STI, OSABI, TT.isArch64Bit());
}
diff --git a/test/MC/RISCV/cnop.s b/test/MC/RISCV/cnop.s
new file mode 100644
index 00000000000..8d526263724
--- /dev/null
+++ b/test/MC/RISCV/cnop.s
@@ -0,0 +1,26 @@
+# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+c < %s \
+# RUN: | llvm-objdump -mattr=+c -d - | FileCheck -check-prefix=CHECK-INST %s
+
+# alpha and main are 8 byte alignment
+# but the alpha function's size is 6
+# So assembler will insert a c.nop to make sure 8 byte alignment.
+
+ .text
+ .p2align 3
+ .type alpha,@function
+alpha:
+# BB#0:
+ addi sp, sp, -16
+ c.lw a0, 0(a0)
+# CHECK-INST: c.nop
+.Lfunc_end0:
+ .size alpha, .Lfunc_end0-alpha
+ # -- End function
+ .globl main
+ .p2align 3
+ .type main,@function
+main: # @main
+# BB#0:
+.Lfunc_end1:
+ .size main, .Lfunc_end1-main
+ # -- End function
--
2.16.2