Skip to content

Commit 2a13c0c

Browse files
s-barannikovgithub-actions[bot]
authored andcommitted
Automerge: [TableGen][CodeEmitter] Refactor addCodeToMergeInOperand (NFC) (#158674)
* Use streams to avoid dealing with std::string * Print operand masks in hex * Make the output more succinct
2 parents 1802d76 + 3c7c892 commit 2a13c0c

File tree

3 files changed

+39
-105
lines changed

3 files changed

+39
-105
lines changed

llvm/test/TableGen/HwModeEncodeDecode3.td

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -230,28 +230,22 @@ def unrelated: Instruction {
230230
// ENCODER: default: llvm_unreachable("Unhandled HwMode");
231231
// ENCODER: case 0: {
232232
// ENCODER: op = getMachineOpValue(MI, MI.getOperand(0), Fixups, STI);
233-
// ENCODER: op &= UINT64_C(240);
234-
// ENCODER: Value |= op;
233+
// ENCODER: Value |= (op & 0xf0);
235234
// ENCODER: break;
236235
// ENCODER: }
237236
// ENCODER: case 1: {
238237
// ENCODER: op = getMachineOpValue(MI, MI.getOperand(0), Fixups, STI);
239-
// ENCODER: op &= UINT64_C(240);
240-
// ENCODER: Value |= op;
238+
// ENCODER: Value |= (op & 0xf0);
241239
// ENCODER: break;
242240
// ENCODER: }
243241
// ENCODER: case 2: {
244242
// ENCODER: op = getMachineOpValue(MI, MI.getOperand(0), Fixups, STI);
245-
// ENCODER: op &= UINT64_C(255);
246-
// ENCODER: op <<= 8;
247-
// ENCODER: Value |= op;
243+
// ENCODER: Value |= (op & 0xff) << 8;
248244
// ENCODER: break;
249245
// ENCODER: }
250246
// ENCODER: case 3: {
251247
// ENCODER: op = getMachineOpValue(MI, MI.getOperand(0), Fixups, STI);
252-
// ENCODER: op &= UINT64_C(255);
253-
// ENCODER: op <<= 24;
254-
// ENCODER: Value |= op;
248+
// ENCODER: Value |= (op & 0xff) << 24;
255249
// ENCODER: break;
256250
// ENCODER: }
257251
// ENCODER-LABEL: case ::baz: {
@@ -265,7 +259,6 @@ def unrelated: Instruction {
265259
// ENCODER: default: llvm_unreachable("Unhandled HwMode");
266260
// ENCODER: case 2: {
267261
// ENCODER: op = getMachineOpValue(MI, MI.getOperand(0), Fixups, STI);
268-
// ENCODER: op &= UINT64_C(240);
269-
// ENCODER: Value |= op;
262+
// ENCODER: Value |= (op & 0xf0);
270263
// ENCODER: break;
271264
// ENCODER: }

llvm/test/TableGen/RegisterEncoder.td

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ def foo1 : Instruction {
3030

3131
// CHECK: case ::foo1: {
3232
// CHECK: op = barEncoder
33-
// CHECK: op &= UINT64_C(255);
34-
// CHECK: Value |= op;
33+
// CHECK: Value |= (op & 0xff);
3534
// CHECK: break;
3635
// CHECK: }
3736

@@ -57,10 +56,8 @@ def foo2 : Instruction {
5756

5857
// CHECK: case ::foo2: {
5958
// CHECK: op = barEncoder
60-
// CHECK: op &= UINT64_C(15);
61-
// CHECK: Value |= op;
59+
// CHECK: Value |= (op & 0xf);
6260
// CHECK: op = barEncoder
63-
// CHECK: op &= UINT64_C(15);
64-
// CHECK: Value |= op;
61+
// CHECK: Value |= (op & 0xf) << 4;
6562
// CHECK: break;
6663
// CHECK: }

llvm/utils/TableGen/CodeEmitterGen.cpp

Lines changed: 31 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "llvm/ADT/ArrayRef.h"
3232
#include "llvm/ADT/StringExtras.h"
3333
#include "llvm/Support/Casting.h"
34+
#include "llvm/Support/Format.h"
3435
#include "llvm/Support/FormatVariadic.h"
3536
#include "llvm/Support/raw_ostream.h"
3637
#include "llvm/TableGen/Error.h"
@@ -139,58 +140,28 @@ bool CodeEmitterGen::addCodeToMergeInOperand(const Record *R,
139140
StringRef EncoderMethodName =
140141
CGI.Operands[SO.first].EncoderMethodNames[SO.second];
141142

142-
if (UseAPInt)
143-
Case += " op.clearAllBits();\n";
143+
raw_string_ostream OS(Case);
144+
indent Indent(6);
145+
146+
OS << Indent << "// op: " << VarName << '\n';
144147

145-
Case += " // op: " + VarName + "\n";
148+
if (UseAPInt)
149+
OS << Indent << "op.clearAllBits();\n";
146150

147-
// If the source operand has a custom encoder, use it.
148151
if (!EncoderMethodName.empty()) {
149-
raw_string_ostream CaseOS(Case);
150-
CaseOS << indent(6);
151152
if (UseAPInt)
152-
CaseOS << EncoderMethodName << "(MI, " << OpIdx << ", op";
153+
OS << Indent << EncoderMethodName << "(MI, " << OpIdx
154+
<< ", op, Fixups, STI);\n";
153155
else
154-
CaseOS << "op = " << EncoderMethodName << "(MI, " << OpIdx;
155-
CaseOS << ", Fixups, STI);\n";
156+
OS << Indent << "op = " << EncoderMethodName << "(MI, " << OpIdx
157+
<< ", Fixups, STI);\n";
156158
} else {
157-
if (UseAPInt) {
158-
Case +=
159-
" getMachineOpValue(MI, MI.getOperand(" + utostr(OpIdx) + ")";
160-
Case += ", op, Fixups, STI";
161-
} else {
162-
Case += " op = getMachineOpValue(MI, MI.getOperand(" +
163-
utostr(OpIdx) + ")";
164-
Case += ", Fixups, STI";
165-
}
166-
Case += ");\n";
167-
}
168-
169-
// Precalculate the number of lits this variable contributes to in the
170-
// operand. If there is a single lit (consecutive range of bits) we can use a
171-
// destructive sequence on APInt that reduces memory allocations.
172-
int NumOperandLits = 0;
173-
for (int TmpBit = Bit; TmpBit >= 0;) {
174-
int VarBit = getVariableBit(VarName, BI, TmpBit);
175-
176-
// If this bit isn't from a variable, skip it.
177-
if (VarBit == -1) {
178-
--TmpBit;
179-
continue;
180-
}
181-
182-
// Figure out the consecutive range of bits covered by this operand, in
183-
// order to generate better encoding code.
184-
int BeginVarBit = VarBit;
185-
int N = 1;
186-
for (--TmpBit; TmpBit >= 0;) {
187-
VarBit = getVariableBit(VarName, BI, TmpBit);
188-
if (VarBit == -1 || VarBit != (BeginVarBit - N))
189-
break;
190-
++N;
191-
--TmpBit;
192-
}
193-
++NumOperandLits;
159+
if (UseAPInt)
160+
OS << Indent << "getMachineOpValue(MI, MI.getOperand(" << OpIdx
161+
<< "), op, Fixups, STI);\n";
162+
else
163+
OS << Indent << "op = getMachineOpValue(MI, MI.getOperand(" << OpIdx
164+
<< "), Fixups, STI);\n";
194165
}
195166

196167
unsigned BitOffset = -1;
@@ -216,52 +187,25 @@ bool CodeEmitterGen::addCodeToMergeInOperand(const Record *R,
216187
--Bit;
217188
}
218189

219-
std::string MaskStr;
220-
int OpShift;
221-
222190
unsigned LoBit = BeginVarBit - N + 1;
223-
unsigned HiBit = LoBit + N;
224191
unsigned LoInstBit = BeginInstBit - N + 1;
225192
BitOffset = LoInstBit;
226193
if (UseAPInt) {
227-
std::string ExtractStr;
228-
if (N >= 64) {
229-
ExtractStr = "op.extractBits(" + itostr(HiBit - LoBit) + ", " +
230-
itostr(LoBit) + ")";
231-
Case += " Value.insertBits(" + ExtractStr + ", " +
232-
itostr(LoInstBit) + ");\n";
233-
} else {
234-
ExtractStr = "op.extractBitsAsZExtValue(" + itostr(HiBit - LoBit) +
235-
", " + itostr(LoBit) + ")";
236-
Case += " Value.insertBits(" + ExtractStr + ", " +
237-
itostr(LoInstBit) + ", " + itostr(HiBit - LoBit) + ");\n";
238-
}
194+
if (N > 64)
195+
OS << Indent << "Value.insertBits(op.extractBits(" << N << ", " << LoBit
196+
<< "), " << LoInstBit << ");\n";
197+
else
198+
OS << Indent << "Value.insertBits(op.extractBitsAsZExtValue(" << N
199+
<< ", " << LoBit << "), " << LoInstBit << ", " << N << ");\n";
239200
} else {
240-
uint64_t OpMask = ~(uint64_t)0 >> (64 - N);
241-
OpShift = BeginVarBit - N + 1;
242-
OpMask <<= OpShift;
243-
MaskStr = "UINT64_C(" + utostr(OpMask) + ")";
244-
OpShift = BeginInstBit - BeginVarBit;
245-
246-
if (NumOperandLits == 1) {
247-
Case += " op &= " + MaskStr + ";\n";
248-
if (OpShift > 0) {
249-
Case += " op <<= " + itostr(OpShift) + ";\n";
250-
} else if (OpShift < 0) {
251-
Case += " op >>= " + itostr(-OpShift) + ";\n";
252-
}
253-
Case += " Value |= op;\n";
254-
} else {
255-
if (OpShift > 0) {
256-
Case += " Value |= (op & " + MaskStr + ") << " +
257-
itostr(OpShift) + ";\n";
258-
} else if (OpShift < 0) {
259-
Case += " Value |= (op & " + MaskStr + ") >> " +
260-
itostr(-OpShift) + ";\n";
261-
} else {
262-
Case += " Value |= (op & " + MaskStr + ");\n";
263-
}
264-
}
201+
uint64_t OpMask = maskTrailingOnes<uint64_t>(N) << LoBit;
202+
OS << Indent << "Value |= (op & " << format_hex(OpMask, 0) << ')';
203+
int OpShift = BeginInstBit - BeginVarBit;
204+
if (OpShift > 0)
205+
OS << " << " << OpShift;
206+
else if (OpShift < 0)
207+
OS << " >> " << -OpShift;
208+
OS << ";\n";
265209
}
266210
}
267211

0 commit comments

Comments
 (0)