@@ -47,24 +47,7 @@ enum OpCode {
47
47
Ret,
48
48
InsertValue,
49
49
PtrAdd,
50
- Add,
51
- Sub,
52
- Mul,
53
- Or,
54
- And,
55
- Xor,
56
- Shl,
57
- AShr,
58
- FAdd,
59
- FDiv,
60
- FMul,
61
- FRem,
62
- FSub,
63
- LShr,
64
- SDiv,
65
- SRem,
66
- UDiv,
67
- URem,
50
+ BinOp,
68
51
UnimplementedInstruction = 255 , // YKFIXME: Will eventually be deleted.
69
52
};
70
53
@@ -77,6 +60,7 @@ enum OperandKind {
77
60
Arg,
78
61
Global,
79
62
Predicate,
63
+ OpKindBinOp,
80
64
UnimplementedOperand = 255 ,
81
65
};
82
66
@@ -103,6 +87,28 @@ enum CmpPredicate {
103
87
PredSignedLessEqual,
104
88
};
105
89
90
+ // A binary operator.
91
+ enum BinOp {
92
+ BinOpAdd,
93
+ BinOpSub,
94
+ BinOpMul,
95
+ BinOpOr,
96
+ BinOpAnd,
97
+ BinOpXor,
98
+ BinOpShl,
99
+ BinOpAShr,
100
+ BinOpFAdd,
101
+ BinOpFDiv,
102
+ BinOpFMul,
103
+ BinOpFRem,
104
+ BinOpFSub,
105
+ BinOpLShr,
106
+ BinOpSDiv,
107
+ BinOpSRem,
108
+ BinOpUDiv,
109
+ BinOpURem,
110
+ };
111
+
106
112
template <class T > string toString (T *X) {
107
113
string S;
108
114
raw_string_ostream SS (S);
@@ -314,74 +320,87 @@ class YkIRWriter {
314
320
void serialiseBinaryOperation (llvm::BinaryOperator *I,
315
321
ValueLoweringMap &VLMap, unsigned BBIdx,
316
322
unsigned &InstIdx) {
323
+ assert (I->getNumOperands () == 2 );
324
+
325
+ // type index:
317
326
OutStreamer.emitSizeT (typeIndex (I->getType ()));
318
- serialiseBinOpcode (I->getOpcode ());
319
- OutStreamer.emitInt32 (I->getNumOperands ());
320
- for (Value *O : I->operands ()) {
321
- serialiseOperand (I, VLMap, O);
322
- }
327
+ // opcode:
328
+ OutStreamer.emitInt8 (OpCode::BinOp);
329
+ // num operands:
330
+ OutStreamer.emitInt32 (3 );
331
+ // left-hand side:
332
+ serialiseOperand (I, VLMap, I->getOperand (0 ));
333
+ // binary operator:
334
+ serialiseBinOperatorOperand (I->getOpcode ());
335
+ // right-hand side:
336
+ serialiseOperand (I, VLMap, I->getOperand (1 ));
337
+
323
338
VLMap[I] = {BBIdx, InstIdx};
324
339
InstIdx++;
325
340
}
326
341
327
- void serialiseBinOpcode (Instruction::BinaryOps BO) {
342
+ // Serialise a binary operator.
343
+ void serialiseBinOperatorOperand (Instruction::BinaryOps BO) {
344
+ // operand kind:
345
+ OutStreamer.emitInt8 (OperandKind::OpKindBinOp);
346
+ // the operator:
328
347
switch (BO) {
329
348
case Instruction::BinaryOps::Add:
330
- OutStreamer.emitInt8 (OpCode::Add );
349
+ OutStreamer.emitInt8 (BinOp::BinOpAdd );
331
350
break ;
332
351
case Instruction::BinaryOps::Sub:
333
- OutStreamer.emitInt8 (OpCode::Sub );
352
+ OutStreamer.emitInt8 (BinOp::BinOpSub );
334
353
break ;
335
354
case Instruction::BinaryOps::Mul:
336
- OutStreamer.emitInt8 (OpCode::Mul );
355
+ OutStreamer.emitInt8 (BinOp::BinOpMul );
337
356
break ;
338
357
case Instruction::BinaryOps::Or:
339
- OutStreamer.emitInt8 (OpCode::Or );
358
+ OutStreamer.emitInt8 (BinOp::BinOpOr );
340
359
break ;
341
360
case Instruction::BinaryOps::And:
342
- OutStreamer.emitInt8 (OpCode::And );
361
+ OutStreamer.emitInt8 (BinOp::BinOpAnd );
343
362
break ;
344
363
case Instruction::BinaryOps::Xor:
345
- OutStreamer.emitInt8 (OpCode::Xor );
364
+ OutStreamer.emitInt8 (BinOp::BinOpXor );
346
365
break ;
347
366
case Instruction::BinaryOps::Shl:
348
- OutStreamer.emitInt8 (OpCode::Shl );
367
+ OutStreamer.emitInt8 (BinOp::BinOpShl );
349
368
break ;
350
369
case Instruction::BinaryOps::AShr:
351
- OutStreamer.emitInt8 (OpCode::AShr );
370
+ OutStreamer.emitInt8 (BinOp::BinOpAShr );
352
371
break ;
353
372
case Instruction::BinaryOps::FAdd:
354
- OutStreamer.emitInt8 (OpCode::FAdd );
373
+ OutStreamer.emitInt8 (BinOp::BinOpFAdd );
355
374
break ;
356
375
case Instruction::BinaryOps::FDiv:
357
- OutStreamer.emitInt8 (OpCode::FDiv );
376
+ OutStreamer.emitInt8 (BinOp::BinOpFDiv );
358
377
break ;
359
378
case Instruction::BinaryOps::FMul:
360
- OutStreamer.emitInt8 (OpCode::FMul );
379
+ OutStreamer.emitInt8 (BinOp::BinOpFMul );
361
380
break ;
362
381
case Instruction::BinaryOps::FRem:
363
- OutStreamer.emitInt8 (OpCode::FRem );
382
+ OutStreamer.emitInt8 (BinOp::BinOpFRem );
364
383
break ;
365
384
case Instruction::BinaryOps::FSub:
366
- OutStreamer.emitInt8 (OpCode::FSub );
385
+ OutStreamer.emitInt8 (BinOp::BinOpFSub );
367
386
break ;
368
387
case Instruction::BinaryOps::LShr:
369
- OutStreamer.emitInt8 (OpCode::LShr );
388
+ OutStreamer.emitInt8 (BinOp::BinOpLShr );
370
389
break ;
371
390
case Instruction::BinaryOps::SDiv:
372
- OutStreamer.emitInt8 (OpCode::SDiv );
391
+ OutStreamer.emitInt8 (BinOp::BinOpSDiv );
373
392
break ;
374
393
case Instruction::BinaryOps::SRem:
375
- OutStreamer.emitInt8 (OpCode::SRem );
394
+ OutStreamer.emitInt8 (BinOp::BinOpSRem );
376
395
break ;
377
396
case Instruction::BinaryOps::UDiv:
378
- OutStreamer.emitInt8 (OpCode::UDiv );
397
+ OutStreamer.emitInt8 (BinOp::BinOpUDiv );
379
398
break ;
380
399
case Instruction::BinaryOps::URem:
381
- OutStreamer.emitInt8 (OpCode::URem);
382
- break ;
383
- case Instruction::BinaryOps::BinaryOpsEnd:
400
+ OutStreamer.emitInt8 (BinOp::BinOpURem);
384
401
break ;
402
+ default :
403
+ llvm::report_fatal_error (" unknown binary operator" );
385
404
}
386
405
}
387
406
0 commit comments