llvm.org GIT mirror llvm / 3c074f6
add support for array abbreviations. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36754 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
3 changed file(s) with 101 addition(s) and 59 deletion(s). Raw diff Collapse all Expand all
8383 unsigned Enc : 3; // The encoding to use.
8484 public:
8585 enum Encoding {
86 FixedWidth = 1, // A fixed with field, Val specifies number of bits.
87 VBR = 2 // A VBR field where Val specifies the width of each chunk.
86 Fixed = 1, // A fixed with field, Val specifies number of bits.
87 VBR = 2, // A VBR field where Val specifies the width of each chunk.
88 Array = 3 // A sequence of fields, next field species elt encoding.
8889 };
8990
9091 BitCodeAbbrevOp(uint64_t V) : Val(V), IsLiteral(true) {}
91 BitCodeAbbrevOp(Encoding E, uint64_t Data)
92 BitCodeAbbrevOp(Encoding E, uint64_t Data = 0)
9293 : Val(Data), IsLiteral(false), Enc(E) {}
94
9395
9496 bool isLiteral() const { return IsLiteral; }
9597 bool isEncoding() const { return !IsLiteral; }
99101
100102 // Accessors for encoding info.
101103 Encoding getEncoding() const { assert(isEncoding()); return (Encoding)Enc; }
102 uint64_t getEncodingData() const { assert(isEncoding()); return Val; }
104 uint64_t getEncodingData() const {
105 assert(isEncoding() && hasEncodingData());
106 return Val;
107 }
103108
104109 bool hasEncodingData() const { return hasEncodingData(getEncoding()); }
105110 static bool hasEncodingData(Encoding E) {
106 return true;
111 switch (E) {
112 default: assert(0 && "Unknown encoding");
113 case Fixed:
114 case VBR:
115 return true;
116 case Array:
117 return false;
118 }
107119 }
108120 };
109121
273273 // Record Processing
274274 //===--------------------------------------------------------------------===//
275275
276 private:
277 void ReadAbbreviatedField(const BitCodeAbbrevOp &Op,
278 SmallVectorImpl &Vals) {
279 if (Op.isLiteral()) {
280 // If the abbrev specifies the literal value to use, use it.
281 Vals.push_back(Op.getLiteralValue());
282 } else {
283 // Decode the value as we are commanded.
284 switch (Op.getEncoding()) {
285 default: assert(0 && "Unknown encoding!");
286 case BitCodeAbbrevOp::Fixed:
287 Vals.push_back(Read(Op.getEncodingData()));
288 break;
289 case BitCodeAbbrevOp::VBR:
290 Vals.push_back(ReadVBR64(Op.getEncodingData()));
291 break;
292 }
293 }
294 }
295 public:
276296 unsigned ReadRecord(unsigned AbbrevID, SmallVectorImpl &Vals) {
277297 if (AbbrevID == bitc::UNABBREV_RECORD) {
278298 unsigned Code = ReadVBR(6);
288308
289309 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
290310 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
291 if (Op.isLiteral()) {
292 // If the abbrev specifies the literal value to use, use it.
293 Vals.push_back(Op.getLiteralValue());
311 if (Op.isLiteral() || Op.getEncoding() != BitCodeAbbrevOp::Array) {
312 ReadAbbreviatedField(Op, Vals);
294313 } else {
295 // Decode the value as we are commanded.
296 switch (Op.getEncoding()) {
297 default: assert(0 && "Unknown encoding!");
298 case BitCodeAbbrevOp::FixedWidth:
299 Vals.push_back(Read(Op.getEncodingData()));
300 break;
301 case BitCodeAbbrevOp::VBR:
302 Vals.push_back(ReadVBR64(Op.getEncodingData()));
303 break;
304 }
314 // Array case. Read the number of elements as a vbr6.
315 unsigned NumElts = ReadVBR(6);
316
317 // Get the element encoding.
318 assert(i+2 == e && "array op not second to last?");
319 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
320
321 // Read all the elements.
322 for (; NumElts; --NumElts)
323 ReadAbbreviatedField(EltEnc, Vals);
305324 }
306325 }
307326
325344 }
326345
327346 BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
328 if (BitCodeAbbrevOp::hasEncodingData(E)) {
347 if (BitCodeAbbrevOp::hasEncodingData(E))
329348 Abbv->Add(BitCodeAbbrevOp(E, ReadVBR64(5)));
330 } else {
331 assert(0 && "unimp");
332 }
349 else
350 Abbv->Add(BitCodeAbbrevOp(E));
333351 }
334352 CurAbbrevs.push_back(Abbv);
335353 }
188188 // Record Emission
189189 //===--------------------------------------------------------------------===//
190190
191 private:
192 /// EmitAbbreviatedField - Emit a single scalar field value with the specified
193 /// encoding.
194 template
195 void EmitAbbreviatedField(const BitCodeAbbrevOp &Op, uintty V) {
196 if (Op.isLiteral()) {
197 // If the abbrev specifies the literal value to use, don't emit
198 // anything.
199 assert(V == Op.getLiteralValue() &&
200 "Invalid abbrev for record!");
201 return;
202 }
203
204 // Encode the value as we are commanded.
205 switch (Op.getEncoding()) {
206 default: assert(0 && "Unknown encoding!");
207 case BitCodeAbbrevOp::Fixed:
208 Emit(V, Op.getEncodingData());
209 break;
210 case BitCodeAbbrevOp::VBR:
211 EmitVBR(V, Op.getEncodingData());
212 break;
213 }
214 }
215 public:
216
191217 /// EmitRecord - Emit the specified record to the stream, using an abbrev if
192218 /// we have one to compress the output.
193219 void EmitRecord(unsigned Code, SmallVectorImpl &Vals,
206232 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
207233 assert(RecordIdx < Vals.size() && "Invalid abbrev/record");
208234 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
209 uint64_t RecordVal = Vals[RecordIdx];
210
211 if (Op.isLiteral()) {
212 // If the abbrev specifies the literal value to use, don't emit
213 // anything.
214 assert(RecordVal == Op.getLiteralValue() &&
215 "Invalid abbrev for record!");
235 if (Op.isLiteral() || Op.getEncoding() != BitCodeAbbrevOp::Array) {
236 EmitAbbreviatedField(Op, Vals[RecordIdx]);
216237 ++RecordIdx;
217238 } else {
218 // Encode the value as we are commanded.
219 switch (Op.getEncoding()) {
220 default: assert(0 && "Unknown encoding!");
221 case BitCodeAbbrevOp::FixedWidth:
222 Emit64(RecordVal, Op.getEncodingData());
223 ++RecordIdx;
224 break;
225 case BitCodeAbbrevOp::VBR:
226 EmitVBR64(RecordVal, Op.getEncodingData());
227 ++RecordIdx;
228 break;
229 }
239 // Array case.
240 assert(i+2 == e && "array op not second to last?");
241 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
242
243 // Emit a vbr6 to indicate the number of elements present.
244 EmitVBR(Vals.size()-RecordIdx, 6);
245
246 // Emit each field.
247 for (; RecordIdx != Vals.size(); ++RecordIdx)
248 EmitAbbreviatedField(EltEnc, Vals[RecordIdx]);
230249 }
231250 }
232251 assert(RecordIdx == Vals.size() && "Not all record operands emitted!");
259278 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
260279 assert(RecordIdx < Vals.size() && "Invalid abbrev/record");
261280 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
262 unsigned RecordVal = Vals[RecordIdx];
263281
264 if (Op.isLiteral()) {
265 // If the abbrev specifies the literal value to use, don't emit
266 // anything.
267 assert(RecordVal == Op.getLiteralValue() &&
268 "Invalid abbrev for record!");
282 if (Op.isLiteral() || Op.getEncoding() != BitCodeAbbrevOp::Array) {
283 EmitAbbreviatedField(Op, Vals[RecordIdx]);
269284 ++RecordIdx;
270285 } else {
271 // Encode the value as we are commanded.
272 switch (Op.getEncoding()) {
273 default: assert(0 && "Unknown encoding!");
274 case BitCodeAbbrevOp::FixedWidth:
275 Emit(RecordVal, Op.getEncodingData());
276 ++RecordIdx;
277 break;
278 case BitCodeAbbrevOp::VBR:
279 EmitVBR(RecordVal, Op.getEncodingData());
280 ++RecordIdx;
281 break;
282 }
286 assert(i+2 == e && "array op not second to last?");
287 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
288
289 // Emit a vbr6 to indicate the number of elements present.
290 EmitVBR(Vals.size()-RecordIdx, 6);
291
292 // Emit each field.
293 for (; RecordIdx != Vals.size(); ++RecordIdx)
294 EmitAbbreviatedField(EltEnc, Vals[RecordIdx]);
283295 }
284296 }
285297 assert(RecordIdx == Vals.size() && "Not all record operands emitted!");