Skip to content

Commit 1007b93

Browse files
committed
[TableGen][CodeEmitter] Refactor addCodeToMergeInOperand (NFC)
* Use streams to avoid dealing with std::string * Print operand masks in hex * Make the output more succinct
1 parent 0237477 commit 1007b93

File tree

1 file changed

+31
-87
lines changed

1 file changed

+31
-87
lines changed

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)