forked from stichnot/subzero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
llvm2ice.cpp
625 lines (566 loc) · 21.5 KB
/
llvm2ice.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/* Copyright 2014 The Native Client Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can
* be found in the LICENSE file.
*/
#include "IceCfg.h"
#include "IceCfgNode.h"
#include "IceDefs.h"
#include "IceInst.h"
#include "IceOperand.h"
#include "IceTypes.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SourceMgr.h"
#include <fstream>
#include <iostream>
using namespace llvm;
// Debugging helper
template <typename T> static std::string LLVMObjectAsString(const T *O) {
std::string Dump;
raw_string_ostream Stream(Dump);
O->print(Stream);
return Stream.str();
}
// Converter from LLVM to ICE. The entry point is the convertFunction method.
//
// Note: this currently assumes that the given IR was verified to be valid PNaCl
// bitcode:
// https://developers.google.com/native-client/dev/reference/pnacl-bitcode-abi
// If not, all kinds of assertions may fire.
//
class LLVM2ICEConverter {
public:
LLVM2ICEConverter() : Cfg(NULL), CurrentNode(NULL) {}
IceCfg *convertFunction(const Function *F) {
VariableTranslation.clear();
LabelTranslation.clear();
Cfg = new IceCfg;
Cfg->setName(F->getName());
Cfg->setReturnType(convertType(F->getReturnType()));
// The initial definition/use of each arg is the entry node.
CurrentNode = mapBasicBlockToNode(&F->getEntryBlock());
for (Function::const_arg_iterator ArgI = F->arg_begin(),
ArgE = F->arg_end();
ArgI != ArgE; ++ArgI) {
Cfg->addArg(mapValueToIceVar(ArgI));
}
for (Function::const_iterator BBI = F->begin(), BBE = F->end(); BBI != BBE;
++BBI) {
mapBasicBlockToNode(BBI);
}
for (Function::const_iterator BBI = F->begin(), BBE = F->end(); BBI != BBE;
++BBI) {
CurrentNode = mapBasicBlockToNode(BBI);
convertBasicBlock(BBI);
}
Cfg->setEntryNode(mapBasicBlockToNode(&F->getEntryBlock()));
Cfg->registerEdges();
return Cfg;
}
private:
// LLVM values (instructions, etc.) are mapped directly to ICE variables.
// mapValueToIceVar has a version that forces an ICE type on the variable,
// and a version that just uses convertType on V.
IceVariable *mapValueToIceVar(const Value *V, IceType IceTy) {
if (IceTy == IceType_void)
return NULL;
assert(CurrentNode);
return Cfg->makeVariable(IceTy, CurrentNode,
VariableTranslation.translate(V), V->getName());
}
IceVariable *mapValueToIceVar(const Value *V) {
return mapValueToIceVar(V, convertType(V->getType()));
}
IceCfgNode *mapBasicBlockToNode(const BasicBlock *BB) {
return Cfg->makeNode(LabelTranslation.translate(BB), BB->getName());
}
IceType convertIntegerType(const IntegerType *IntTy) {
switch (IntTy->getBitWidth()) {
case 1:
return IceType_i1;
case 8:
return IceType_i8;
case 16:
return IceType_i16;
case 32:
return IceType_i32;
case 64:
return IceType_i64;
default:
report_fatal_error(std::string("Invalid PNaCl int type: ") +
LLVMObjectAsString(IntTy));
return IceType_void;
}
}
IceType convertType(const Type *Ty) {
switch (Ty->getTypeID()) {
case Type::VoidTyID:
return IceType_void;
case Type::IntegerTyID:
return convertIntegerType(cast<IntegerType>(Ty));
case Type::FloatTyID:
return IceType_f32;
case Type::DoubleTyID:
return IceType_f64;
case Type::PointerTyID: {
const PointerType *PTy = cast<PointerType>(Ty);
return convertType(PTy->getElementType());
return IceType_i32;
}
case Type::FunctionTyID:
return IceType_i32;
default:
report_fatal_error(std::string("Invalid PNaCl type: ") +
LLVMObjectAsString(Ty));
}
llvm_unreachable("convertType");
return IceType_void;
}
// Given a LLVM instruction and an operand number, produce the IceOperand this
// refers to. If there's no such operand, return NULL.
IceOperand *convertOperand(const Instruction *Inst, unsigned OpNum) {
if (OpNum >= Inst->getNumOperands()) {
return NULL;
}
const Value *Op = Inst->getOperand(OpNum);
return convertValue(Op);
}
IceOperand *convertValue(const Value *Op) {
if (const Constant *Const = dyn_cast<Constant>(Op)) {
// For now only constant integers are supported.
// TODO: support all kinds of constants
if (const GlobalValue *GV = dyn_cast<GlobalValue>(Const)) {
return Cfg->getConstant(/*IceType_i32*/ convertType(GV->getType()), GV,
0, GV->getName());
} else if (const ConstantInt *CI = dyn_cast<ConstantInt>(Const)) {
return Cfg->getConstantInt(convertIntegerType(CI->getType()),
CI->getZExtValue());
} else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Const)) {
IceType Type = convertType(CFP->getType());
if (Type == IceType_f32)
return Cfg->getConstantFloat(CFP->getValueAPF().convertToFloat());
else if (Type == IceType_f64)
return Cfg->getConstantDouble(CFP->getValueAPF().convertToDouble());
assert(0 && "Unexpected floating point type");
return NULL;
} else {
assert(0 && "Unhandled constant type");
return NULL;
}
} else {
return mapValueToIceVar(Op);
}
}
// Note: this currently assumes a 1x1 mapping between LLVM IR and Ice
// instructions.
IceInst *convertInstruction(const Instruction *Inst) {
switch (Inst->getOpcode()) {
case Instruction::PHI:
return convertPHINodeInstruction(cast<PHINode>(Inst));
case Instruction::Br:
return convertBrInstruction(cast<BranchInst>(Inst));
case Instruction::Ret:
return convertRetInstruction(cast<ReturnInst>(Inst));
case Instruction::IntToPtr:
return convertIntToPtrInstruction(cast<IntToPtrInst>(Inst));
case Instruction::ICmp:
return convertICmpInstruction(cast<ICmpInst>(Inst));
case Instruction::FCmp:
return convertFCmpInstruction(cast<FCmpInst>(Inst));
case Instruction::Select:
return convertSelectInstruction(cast<SelectInst>(Inst));
case Instruction::Switch:
return convertSwitchInstruction(cast<SwitchInst>(Inst));
case Instruction::Load:
return convertLoadInstruction(cast<LoadInst>(Inst));
case Instruction::Store:
return convertStoreInstruction(cast<StoreInst>(Inst));
case Instruction::ZExt:
return convertCastInstruction(cast<ZExtInst>(Inst), IceInstCast::Zext);
case Instruction::SExt:
return convertCastInstruction(cast<SExtInst>(Inst), IceInstCast::Sext);
case Instruction::Trunc:
return convertCastInstruction(cast<TruncInst>(Inst), IceInstCast::Trunc);
case Instruction::FPTrunc:
return convertCastInstruction(cast<FPTruncInst>(Inst),
IceInstCast::Fptrunc);
case Instruction::FPExt:
return convertCastInstruction(cast<FPExtInst>(Inst), IceInstCast::Fpext);
case Instruction::FPToSI:
return convertCastInstruction(cast<FPToSIInst>(Inst),
IceInstCast::Fptosi);
case Instruction::FPToUI:
return convertCastInstruction(cast<FPToUIInst>(Inst),
IceInstCast::Fptoui);
case Instruction::SIToFP:
return convertCastInstruction(cast<SIToFPInst>(Inst),
IceInstCast::Sitofp);
case Instruction::UIToFP:
return convertCastInstruction(cast<UIToFPInst>(Inst),
IceInstCast::Uitofp);
case Instruction::Add:
return convertArithInstruction(Inst, IceInstArithmetic::Add);
case Instruction::Sub:
return convertArithInstruction(Inst, IceInstArithmetic::Sub);
case Instruction::Mul:
return convertArithInstruction(Inst, IceInstArithmetic::Mul);
case Instruction::UDiv:
return convertArithInstruction(Inst, IceInstArithmetic::Udiv);
case Instruction::SDiv:
return convertArithInstruction(Inst, IceInstArithmetic::Sdiv);
case Instruction::URem:
return convertArithInstruction(Inst, IceInstArithmetic::Urem);
case Instruction::SRem:
return convertArithInstruction(Inst, IceInstArithmetic::Srem);
case Instruction::Shl:
return convertArithInstruction(Inst, IceInstArithmetic::Shl);
case Instruction::LShr:
return convertArithInstruction(Inst, IceInstArithmetic::Lshr);
case Instruction::AShr:
return convertArithInstruction(Inst, IceInstArithmetic::Ashr);
case Instruction::FAdd:
return convertArithInstruction(Inst, IceInstArithmetic::Fadd);
case Instruction::FSub:
return convertArithInstruction(Inst, IceInstArithmetic::Fsub);
case Instruction::FMul:
return convertArithInstruction(Inst, IceInstArithmetic::Fmul);
case Instruction::FDiv:
return convertArithInstruction(Inst, IceInstArithmetic::Fdiv);
case Instruction::FRem:
return convertArithInstruction(Inst, IceInstArithmetic::Frem);
case Instruction::And:
return convertArithInstruction(Inst, IceInstArithmetic::And);
case Instruction::Or:
return convertArithInstruction(Inst, IceInstArithmetic::Or);
case Instruction::Xor:
return convertArithInstruction(Inst, IceInstArithmetic::Xor);
case Instruction::Call:
return convertCallInstruction(cast<CallInst>(Inst));
case Instruction::Alloca:
return convertAllocaInstruction(cast<AllocaInst>(Inst));
default:
report_fatal_error(std::string("Invalid PNaCl instruction: ") +
LLVMObjectAsString(Inst));
}
llvm_unreachable("convertInstruction");
return NULL;
}
IceInst *convertLoadInstruction(const LoadInst *Inst) {
IceOperand *Src = convertOperand(Inst, 0);
// assert(Src->getType() == IceType_i32 && "Expecting loads only from i32");
IceVariable *Dest = mapValueToIceVar(Inst);
return IceInstLoad::create(Cfg, Dest, Src);
}
IceInst *convertStoreInstruction(const StoreInst *Inst) {
IceOperand *Addr = convertOperand(Inst, 1);
// assert(Addr->getType() == IceType_i32 && "Expecting stores only from
// i32");
IceOperand *Val = convertOperand(Inst, 0);
return IceInstStore::create(Cfg, Val, Addr);
}
IceInst *convertArithInstruction(const Instruction *Inst,
IceInstArithmetic::OpKind Opcode) {
const BinaryOperator *BinOp = cast<BinaryOperator>(Inst);
IceOperand *Src0 = convertOperand(Inst, 0);
IceOperand *Src1 = convertOperand(Inst, 1);
IceVariable *Dest = mapValueToIceVar(BinOp);
return IceInstArithmetic::create(Cfg, Opcode, Dest, Src0, Src1);
}
IceInst *convertPHINodeInstruction(const PHINode *Inst) {
unsigned NumValues = Inst->getNumIncomingValues();
IceInstPhi *IcePhi =
IceInstPhi::create(Cfg, NumValues, mapValueToIceVar(Inst));
for (unsigned N = 0, E = NumValues; N != E; ++N) {
IcePhi->addArgument(convertOperand(Inst, N),
mapBasicBlockToNode(Inst->getIncomingBlock(N)));
}
return IcePhi;
}
IceInst *convertBrInstruction(const BranchInst *Inst) {
if (Inst->isConditional()) {
IceOperand *Src = convertOperand(Inst, 0);
BasicBlock *BBThen = Inst->getSuccessor(0);
BasicBlock *BBElse = Inst->getSuccessor(1);
IceCfgNode *NodeThen = mapBasicBlockToNode(BBThen);
IceCfgNode *NodeElse = mapBasicBlockToNode(BBElse);
return IceInstBr::create(Cfg, Src, NodeThen, NodeElse);
} else {
BasicBlock *BBSucc = Inst->getSuccessor(0);
return IceInstBr::create(Cfg, mapBasicBlockToNode(BBSucc));
}
}
IceInst *convertIntToPtrInstruction(const IntToPtrInst *Inst) {
IceOperand *Src = convertOperand(Inst, 0);
IceVariable *Dest = mapValueToIceVar(Inst, IceType_i32);
return IceInstAssign::create(Cfg, Dest, Src);
}
IceInst *convertRetInstruction(const ReturnInst *Inst) {
IceOperand *RetOperand = convertOperand(Inst, 0);
if (RetOperand) {
return IceInstRet::create(Cfg, RetOperand);
} else {
return IceInstRet::create(Cfg);
}
}
IceInst *convertCastInstruction(const Instruction *Inst,
IceInstCast::IceCastKind CastKind) {
IceOperand *Src = convertOperand(Inst, 0);
IceVariable *Dest = mapValueToIceVar(Inst);
return IceInstCast::create(Cfg, CastKind, Dest, Src);
}
IceInst *convertICmpInstruction(const ICmpInst *Inst) {
IceOperand *Src0 = convertOperand(Inst, 0);
IceOperand *Src1 = convertOperand(Inst, 1);
IceVariable *Dest = mapValueToIceVar(Inst);
IceInstIcmp::IceICond Cond;
switch (Inst->getPredicate()) {
default:
llvm_unreachable("ICmpInst predicate");
case CmpInst::ICMP_EQ:
Cond = IceInstIcmp::Eq;
break;
case CmpInst::ICMP_NE:
Cond = IceInstIcmp::Ne;
break;
case CmpInst::ICMP_UGT:
Cond = IceInstIcmp::Ugt;
break;
case CmpInst::ICMP_UGE:
Cond = IceInstIcmp::Uge;
break;
case CmpInst::ICMP_ULT:
Cond = IceInstIcmp::Ult;
break;
case CmpInst::ICMP_ULE:
Cond = IceInstIcmp::Ule;
break;
case CmpInst::ICMP_SGT:
Cond = IceInstIcmp::Sgt;
break;
case CmpInst::ICMP_SGE:
Cond = IceInstIcmp::Sge;
break;
case CmpInst::ICMP_SLT:
Cond = IceInstIcmp::Slt;
break;
case CmpInst::ICMP_SLE:
Cond = IceInstIcmp::Sle;
break;
}
return IceInstIcmp::create(Cfg, Cond, Dest, Src0, Src1);
}
IceInst *convertFCmpInstruction(const FCmpInst *Inst) {
IceOperand *Src0 = convertOperand(Inst, 0);
IceOperand *Src1 = convertOperand(Inst, 1);
IceVariable *Dest = mapValueToIceVar(Inst);
IceInstFcmp::IceFCond Cond;
switch (Inst->getPredicate()) {
default:
llvm_unreachable("FCmpInst predicate");
case CmpInst::FCMP_FALSE:
Cond = IceInstFcmp::False;
break;
case CmpInst::FCMP_OEQ:
Cond = IceInstFcmp::Oeq;
break;
case CmpInst::FCMP_OGT:
Cond = IceInstFcmp::Ogt;
break;
case CmpInst::FCMP_OGE:
Cond = IceInstFcmp::Oge;
break;
case CmpInst::FCMP_OLT:
Cond = IceInstFcmp::Olt;
break;
case CmpInst::FCMP_OLE:
Cond = IceInstFcmp::Ole;
break;
case CmpInst::FCMP_ONE:
Cond = IceInstFcmp::One;
break;
case CmpInst::FCMP_ORD:
Cond = IceInstFcmp::Ord;
break;
case CmpInst::FCMP_UEQ:
Cond = IceInstFcmp::Ueq;
break;
case CmpInst::FCMP_UGT:
Cond = IceInstFcmp::Ugt;
break;
case CmpInst::FCMP_UGE:
Cond = IceInstFcmp::Uge;
break;
case CmpInst::FCMP_ULT:
Cond = IceInstFcmp::Ult;
break;
case CmpInst::FCMP_ULE:
Cond = IceInstFcmp::Ule;
break;
case CmpInst::FCMP_UNE:
Cond = IceInstFcmp::Une;
break;
case CmpInst::FCMP_UNO:
Cond = IceInstFcmp::Uno;
break;
case CmpInst::FCMP_TRUE:
Cond = IceInstFcmp::True;
break;
}
return IceInstFcmp::create(Cfg, Cond, Dest, Src0, Src1);
}
IceInst *convertSelectInstruction(const SelectInst *Inst) {
IceVariable *Dest = mapValueToIceVar(Inst);
IceOperand *Cond = convertValue(Inst->getCondition());
IceOperand *Source1 = convertValue(Inst->getTrueValue());
IceOperand *Source2 = convertValue(Inst->getFalseValue());
return IceInstSelect::create(Cfg, Dest, Cond, Source1, Source2);
}
IceInst *convertSwitchInstruction(const SwitchInst *Inst) {
IceOperand *Source = convertValue(Inst->getCondition());
IceCfgNode *LabelDefault = mapBasicBlockToNode(Inst->getDefaultDest());
unsigned NumCases = Inst->getNumCases();
IceInstSwitch *Switch =
IceInstSwitch::create(Cfg, NumCases, Source, LabelDefault);
unsigned CurrentCase = 0;
for (SwitchInst::ConstCaseIt I = Inst->case_begin(), E = Inst->case_end();
I != E; ++I, ++CurrentCase) {
uint64_t CaseValue = I.getCaseValue()->getZExtValue();
IceCfgNode *CaseSuccessor = mapBasicBlockToNode(I.getCaseSuccessor());
Switch->addBranch(CurrentCase, CaseValue, CaseSuccessor);
}
return Switch;
}
IceInst *convertCallInstruction(const CallInst *Inst) {
IceVariable *Dest = mapValueToIceVar(Inst);
IceOperand *CallTarget = convertValue(Inst->getCalledValue());
unsigned NumArgs = Inst->getNumArgOperands();
IceInstCall *NewInst =
IceInstCall::create(Cfg, NumArgs, Dest, CallTarget, Inst->isTailCall());
for (unsigned i = 0; i < NumArgs; ++i) {
NewInst->addArg(convertOperand(Inst, i));
}
return NewInst;
}
IceInst *convertAllocaInstruction(const AllocaInst *Inst) {
// PNaCl bitcode only contains allocas of byte-granular objects.
IceOperand *ByteCount = convertValue(Inst->getArraySize());
uint32_t Align = Inst->getAlignment();
IceVariable *Dest = mapValueToIceVar(Inst);
return IceInstAlloca::create(Cfg, ByteCount, Align, Dest);
}
IceCfgNode *convertBasicBlock(const BasicBlock *BB) {
IceCfgNode *Node = mapBasicBlockToNode(BB);
for (BasicBlock::const_iterator II = BB->begin(), II_e = BB->end();
II != II_e; ++II) {
IceInst *Inst = convertInstruction(II);
Node->appendInst(Inst);
}
return Node;
}
private:
// Data
IceCfg *Cfg;
IceCfgNode *CurrentNode;
IceValueTranslation<const Value *> VariableTranslation;
IceValueTranslation<const BasicBlock *> LabelTranslation;
};
cl::list<IceVerbose> VerboseList(
"verbose", cl::CommaSeparated,
cl::desc("Verbose options (can be comma-separated):"),
cl::values(
clEnumValN(IceV_Instructions, "inst", "Print basic instructions"),
clEnumValN(IceV_Deleted, "del", "Include deleted instructions"),
clEnumValN(IceV_InstNumbers, "instnum", "Print instruction numbers"),
clEnumValN(IceV_Preds, "pred", "Show predecessors"),
clEnumValN(IceV_Succs, "succ", "Show successors"),
clEnumValN(IceV_Liveness, "live", "Liveness information"),
clEnumValN(IceV_RegManager, "rmgr", "Register manager status"),
clEnumValN(IceV_RegOrigins, "orig", "Physical register origins"),
clEnumValN(IceV_LinearScan, "regalloc", "Linear scan details"),
clEnumValN(IceV_Frame, "frame", "Stack frame layout details"),
clEnumValN(IceV_Timing, "time", "Pass timing details"),
clEnumValN(IceV_All, "all", "Use all verbose options"),
clEnumValN(IceV_None, "none", "No verbosity"), clEnumValEnd));
cl::opt<bool> DisableTranslation("notranslate",
cl::desc("Disable Subzero translation"));
cl::opt<IceTargetArch> TargetArch(
"target", cl::desc("Target architecture:"), cl::init(IceTarget_X8632),
cl::values(clEnumValN(IceTarget_X8632, "x8632", "x86-32"),
clEnumValN(IceTarget_X8632Fast, "x8632fast", "x86-32 fast"),
clEnumValN(IceTarget_X8664, "x8664", "x86-64"),
clEnumValN(IceTarget_ARM32, "arm32", "ARM32"),
clEnumValN(IceTarget_ARM64, "arm64", "ARM64"), clEnumValEnd));
cl::opt<std::string> IRFilename(cl::Positional, cl::desc("<IR file>"),
cl::Required);
static cl::opt<std::string> OutputFilename("o",
cl::desc("Override output filename"),
cl::init("-"),
cl::value_desc("filename"));
static cl::opt<bool> SubzeroTimingEnabled(
"timing", cl::desc("Enable breakdown timing of Subzero translation"));
int main(int argc, char **argv) {
cl::ParseCommandLineOptions(argc, argv);
// Parse the input LLVM IR file into a module.
SMDiagnostic Err;
Module *Mod;
{
IceTimer T;
Mod = ParseIRFile(IRFilename, Err, getGlobalContext());
if (SubzeroTimingEnabled) {
std::cerr << "[Subzero timing] IR Parsing: " << T.getElapsedSec()
<< " sec\n";
}
}
if (!Mod) {
Err.print(argv[0], errs());
return 1;
}
IceVerboseMask VerboseMask = IceV_None;
for (unsigned i = 0; i != VerboseList.size(); ++i)
VerboseMask |= VerboseList[i];
std::ofstream Ofs;
if (OutputFilename != "-") {
Ofs.open(OutputFilename.c_str(), std::ofstream::out);
}
for (Module::const_iterator I = Mod->begin(), E = Mod->end(); I != E; ++I) {
if (I->empty())
continue;
LLVM2ICEConverter FunctionConverter;
IceTimer TConvert;
IceCfg *Cfg = FunctionConverter.convertFunction(I);
if (SubzeroTimingEnabled) {
std::cerr << "[Subzero timing] Convert function " << Cfg->getName()
<< ": " << TConvert.getElapsedSec() << " sec\n";
}
Cfg->Str.Stream = &(OutputFilename == "-" ? std::cout : Ofs);
Cfg->Str.setVerbose(VerboseMask);
if (!DisableTranslation) {
IceTimer TTranslate;
Cfg->translate(TargetArch);
if (SubzeroTimingEnabled) {
std::cerr << "[Subzero timing] Translate function " << Cfg->getName()
<< ": " << TTranslate.getElapsedSec() << " sec\n";
}
if (Cfg->hasError()) {
errs() << "ICE translation error: " << Cfg->getError() << "\n";
}
uint32_t AsmFormat = 0;
IceTimer TEmit;
Cfg->emit(AsmFormat);
if (SubzeroTimingEnabled) {
std::cerr << "[Subzero timing] Emit function " << Cfg->getName() << ": "
<< TEmit.getElapsedSec() << " sec\n";
}
}
}
return 0;
}