Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: src/llvm2ice.cpp

Issue 205613002: Initial skeleton of Subzero. (Closed) Base URL: https://gerrit.chromium.org/gerrit/p/native_client/pnacl-subzero.git@master
Patch Set: Use non-anonymous structs so that array_lengthof works Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/IceTypes.def ('k') | szdiff.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 //===- subzero/src/llvm2ice.cpp - Driver for testing ----------------------===//
2 //
3 // The Subzero Code Generator
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a driver that uses LLVM capabilities to parse a
11 // bitcode file and build the LLVM IR, and then convert the LLVM basic
12 // blocks, instructions, and operands into their Subzero equivalents.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "IceCfg.h"
17 #include "IceCfgNode.h"
18 #include "IceDefs.h"
19 #include "IceGlobalContext.h"
20 #include "IceInst.h"
21 #include "IceOperand.h"
22 #include "IceTypes.h"
23
24 #include "llvm/IR/Constant.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/Instruction.h"
28 #include "llvm/IR/Instructions.h"
29 #include "llvm/IR/LLVMContext.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/IRReader/IRReader.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_os_ostream.h"
35 #include "llvm/Support/SourceMgr.h"
36
37 #include <fstream>
38 #include <iostream>
39
40 using namespace llvm;
41
42 // Debugging helper
43 template <typename T> static std::string LLVMObjectAsString(const T *O) {
44 std::string Dump;
45 raw_string_ostream Stream(Dump);
46 O->print(Stream);
47 return Stream.str();
48 }
49
50 // Converter from LLVM to ICE. The entry point is the convertFunction method.
51 //
52 // Note: this currently assumes that the given IR was verified to be valid PNaCl
53 // bitcode:
54 // https://developers.google.com/native-client/dev/reference/pnacl-bitcode-abi
55 // If not, all kinds of assertions may fire.
56 //
57 class LLVM2ICEConverter {
58 public:
59 LLVM2ICEConverter(Ice::GlobalContext *Ctx)
60 : Ctx(Ctx), Func(NULL), CurrentNode(NULL) {
61 // All PNaCl pointer widths are 32 bits because of the sandbox
62 // model.
63 SubzeroPointerType = Ice::IceType_i32;
64 }
65
66 Ice::Cfg *convertFunction(const Function *F) {
67 VarMap.clear();
68 NodeMap.clear();
69 Func = new Ice::Cfg(Ctx);
70 Func->setFunctionName(F->getName());
71 Func->setReturnType(convertType(F->getReturnType()));
72 Func->setInternal(F->hasInternalLinkage());
73
74 // The initial definition/use of each arg is the entry node.
75 CurrentNode = mapBasicBlockToNode(&F->getEntryBlock());
76 for (Function::const_arg_iterator ArgI = F->arg_begin(),
77 ArgE = F->arg_end();
78 ArgI != ArgE; ++ArgI) {
79 Func->addArg(mapValueToIceVar(ArgI));
80 }
81
82 // Make an initial pass through the block list just to resolve the
83 // blocks in the original linearized order. Otherwise the ICE
84 // linearized order will be affected by branch targets in
85 // terminator instructions.
86 for (Function::const_iterator BBI = F->begin(), BBE = F->end(); BBI != BBE;
87 ++BBI) {
88 mapBasicBlockToNode(BBI);
89 }
90 for (Function::const_iterator BBI = F->begin(), BBE = F->end(); BBI != BBE;
91 ++BBI) {
92 CurrentNode = mapBasicBlockToNode(BBI);
93 convertBasicBlock(BBI);
94 }
95 Func->setEntryNode(mapBasicBlockToNode(&F->getEntryBlock()));
96 Func->computePredecessors();
97
98 return Func;
99 }
100
101 private:
102 // LLVM values (instructions, etc.) are mapped directly to ICE variables.
103 // mapValueToIceVar has a version that forces an ICE type on the variable,
104 // and a version that just uses convertType on V.
105 Ice::Variable *mapValueToIceVar(const Value *V, Ice::Type IceTy) {
106 if (IceTy == Ice::IceType_void)
107 return NULL;
108 if (VarMap.find(V) == VarMap.end()) {
109 assert(CurrentNode);
110 VarMap[V] = Func->makeVariable(IceTy, CurrentNode, V->getName());
111 }
112 return VarMap[V];
113 }
114
115 Ice::Variable *mapValueToIceVar(const Value *V) {
116 return mapValueToIceVar(V, convertType(V->getType()));
117 }
118
119 Ice::CfgNode *mapBasicBlockToNode(const BasicBlock *BB) {
120 if (NodeMap.find(BB) == NodeMap.end()) {
121 NodeMap[BB] = Func->makeNode(BB->getName());
122 }
123 return NodeMap[BB];
124 }
125
126 Ice::Type convertIntegerType(const IntegerType *IntTy) const {
127 switch (IntTy->getBitWidth()) {
128 case 1:
129 return Ice::IceType_i1;
130 case 8:
131 return Ice::IceType_i8;
132 case 16:
133 return Ice::IceType_i16;
134 case 32:
135 return Ice::IceType_i32;
136 case 64:
137 return Ice::IceType_i64;
138 default:
139 report_fatal_error(std::string("Invalid PNaCl int type: ") +
140 LLVMObjectAsString(IntTy));
141 return Ice::IceType_void;
142 }
143 }
144
145 Ice::Type convertType(const Type *Ty) const {
146 switch (Ty->getTypeID()) {
147 case Type::VoidTyID:
148 return Ice::IceType_void;
149 case Type::IntegerTyID:
150 return convertIntegerType(cast<IntegerType>(Ty));
151 case Type::FloatTyID:
152 return Ice::IceType_f32;
153 case Type::DoubleTyID:
154 return Ice::IceType_f64;
155 case Type::PointerTyID:
156 return SubzeroPointerType;
157 case Type::FunctionTyID:
158 return SubzeroPointerType;
159 default:
160 report_fatal_error(std::string("Invalid PNaCl type: ") +
161 LLVMObjectAsString(Ty));
162 }
163
164 llvm_unreachable("convertType");
165 return Ice::IceType_void;
166 }
167
168 // Given a LLVM instruction and an operand number, produce the Operand this
169 // refers to. If there's no such operand, return NULL.
170 Ice::Operand *convertOperand(const Instruction *Inst, unsigned OpNum) {
171 if (OpNum >= Inst->getNumOperands()) {
172 return NULL;
173 }
174 const Value *Op = Inst->getOperand(OpNum);
175 return convertValue(Op);
176 }
177
178 Ice::Operand *convertValue(const Value *Op) {
179 if (const Constant *Const = dyn_cast<Constant>(Op)) {
180 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Const)) {
181 return Ctx->getConstantSym(convertType(GV->getType()), 0,
182 GV->getName());
183 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(Const)) {
184 return Ctx->getConstantInt(convertIntegerType(CI->getType()),
185 CI->getZExtValue());
186 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Const)) {
187 Ice::Type Type = convertType(CFP->getType());
188 if (Type == Ice::IceType_f32)
189 return Ctx->getConstantFloat(CFP->getValueAPF().convertToFloat());
190 else if (Type == Ice::IceType_f64)
191 return Ctx->getConstantDouble(CFP->getValueAPF().convertToDouble());
192 assert(0 && "Unexpected floating point type");
193 return NULL;
194 } else {
195 assert(0 && "Unhandled constant type");
196 return NULL;
197 }
198 } else {
199 return mapValueToIceVar(Op);
200 }
201 }
202
203 // Note: this currently assumes a 1x1 mapping between LLVM IR and Ice
204 // instructions.
205 Ice::Inst *convertInstruction(const Instruction *Inst) {
206 switch (Inst->getOpcode()) {
207 case Instruction::PHI:
208 return convertPHINodeInstruction(cast<PHINode>(Inst));
209 case Instruction::Br:
210 return convertBrInstruction(cast<BranchInst>(Inst));
211 case Instruction::Ret:
212 return convertRetInstruction(cast<ReturnInst>(Inst));
213 case Instruction::IntToPtr:
214 return convertIntToPtrInstruction(cast<IntToPtrInst>(Inst));
215 case Instruction::PtrToInt:
216 return convertPtrToIntInstruction(cast<PtrToIntInst>(Inst));
217 case Instruction::ICmp:
218 return convertICmpInstruction(cast<ICmpInst>(Inst));
219 case Instruction::FCmp:
220 return convertFCmpInstruction(cast<FCmpInst>(Inst));
221 case Instruction::Select:
222 return convertSelectInstruction(cast<SelectInst>(Inst));
223 case Instruction::Switch:
224 return convertSwitchInstruction(cast<SwitchInst>(Inst));
225 case Instruction::Load:
226 return convertLoadInstruction(cast<LoadInst>(Inst));
227 case Instruction::Store:
228 return convertStoreInstruction(cast<StoreInst>(Inst));
229 case Instruction::ZExt:
230 return convertCastInstruction(cast<ZExtInst>(Inst), Ice::InstCast::Zext);
231 case Instruction::SExt:
232 return convertCastInstruction(cast<SExtInst>(Inst), Ice::InstCast::Sext);
233 case Instruction::Trunc:
234 return convertCastInstruction(cast<TruncInst>(Inst),
235 Ice::InstCast::Trunc);
236 case Instruction::FPTrunc:
237 return convertCastInstruction(cast<FPTruncInst>(Inst),
238 Ice::InstCast::Fptrunc);
239 case Instruction::FPExt:
240 return convertCastInstruction(cast<FPExtInst>(Inst),
241 Ice::InstCast::Fpext);
242 case Instruction::FPToSI:
243 return convertCastInstruction(cast<FPToSIInst>(Inst),
244 Ice::InstCast::Fptosi);
245 case Instruction::FPToUI:
246 return convertCastInstruction(cast<FPToUIInst>(Inst),
247 Ice::InstCast::Fptoui);
248 case Instruction::SIToFP:
249 return convertCastInstruction(cast<SIToFPInst>(Inst),
250 Ice::InstCast::Sitofp);
251 case Instruction::UIToFP:
252 return convertCastInstruction(cast<UIToFPInst>(Inst),
253 Ice::InstCast::Uitofp);
254 case Instruction::BitCast:
255 return convertCastInstruction(cast<BitCastInst>(Inst),
256 Ice::InstCast::Bitcast);
257 case Instruction::Add:
258 return convertArithInstruction(Inst, Ice::InstArithmetic::Add);
259 case Instruction::Sub:
260 return convertArithInstruction(Inst, Ice::InstArithmetic::Sub);
261 case Instruction::Mul:
262 return convertArithInstruction(Inst, Ice::InstArithmetic::Mul);
263 case Instruction::UDiv:
264 return convertArithInstruction(Inst, Ice::InstArithmetic::Udiv);
265 case Instruction::SDiv:
266 return convertArithInstruction(Inst, Ice::InstArithmetic::Sdiv);
267 case Instruction::URem:
268 return convertArithInstruction(Inst, Ice::InstArithmetic::Urem);
269 case Instruction::SRem:
270 return convertArithInstruction(Inst, Ice::InstArithmetic::Srem);
271 case Instruction::Shl:
272 return convertArithInstruction(Inst, Ice::InstArithmetic::Shl);
273 case Instruction::LShr:
274 return convertArithInstruction(Inst, Ice::InstArithmetic::Lshr);
275 case Instruction::AShr:
276 return convertArithInstruction(Inst, Ice::InstArithmetic::Ashr);
277 case Instruction::FAdd:
278 return convertArithInstruction(Inst, Ice::InstArithmetic::Fadd);
279 case Instruction::FSub:
280 return convertArithInstruction(Inst, Ice::InstArithmetic::Fsub);
281 case Instruction::FMul:
282 return convertArithInstruction(Inst, Ice::InstArithmetic::Fmul);
283 case Instruction::FDiv:
284 return convertArithInstruction(Inst, Ice::InstArithmetic::Fdiv);
285 case Instruction::FRem:
286 return convertArithInstruction(Inst, Ice::InstArithmetic::Frem);
287 case Instruction::And:
288 return convertArithInstruction(Inst, Ice::InstArithmetic::And);
289 case Instruction::Or:
290 return convertArithInstruction(Inst, Ice::InstArithmetic::Or);
291 case Instruction::Xor:
292 return convertArithInstruction(Inst, Ice::InstArithmetic::Xor);
293 case Instruction::Call:
294 return convertCallInstruction(cast<CallInst>(Inst));
295 case Instruction::Alloca:
296 return convertAllocaInstruction(cast<AllocaInst>(Inst));
297 case Instruction::Unreachable:
298 return convertUnreachableInstruction(cast<UnreachableInst>(Inst));
299 default:
300 report_fatal_error(std::string("Invalid PNaCl instruction: ") +
301 LLVMObjectAsString(Inst));
302 }
303
304 llvm_unreachable("convertInstruction");
305 return NULL;
306 }
307
308 Ice::Inst *convertLoadInstruction(const LoadInst *Inst) {
309 Ice::Operand *Src = convertOperand(Inst, 0);
310 Ice::Variable *Dest = mapValueToIceVar(Inst);
311 return Ice::InstLoad::create(Func, Dest, Src);
312 }
313
314 Ice::Inst *convertStoreInstruction(const StoreInst *Inst) {
315 Ice::Operand *Addr = convertOperand(Inst, 1);
316 Ice::Operand *Val = convertOperand(Inst, 0);
317 return Ice::InstStore::create(Func, Val, Addr);
318 }
319
320 Ice::Inst *convertArithInstruction(const Instruction *Inst,
321 Ice::InstArithmetic::OpKind Opcode) {
322 const BinaryOperator *BinOp = cast<BinaryOperator>(Inst);
323 Ice::Operand *Src0 = convertOperand(Inst, 0);
324 Ice::Operand *Src1 = convertOperand(Inst, 1);
325 Ice::Variable *Dest = mapValueToIceVar(BinOp);
326 return Ice::InstArithmetic::create(Func, Opcode, Dest, Src0, Src1);
327 }
328
329 Ice::Inst *convertPHINodeInstruction(const PHINode *Inst) {
330 unsigned NumValues = Inst->getNumIncomingValues();
331 Ice::InstPhi *IcePhi =
332 Ice::InstPhi::create(Func, NumValues, mapValueToIceVar(Inst));
333 for (unsigned N = 0, E = NumValues; N != E; ++N) {
334 IcePhi->addArgument(convertOperand(Inst, N),
335 mapBasicBlockToNode(Inst->getIncomingBlock(N)));
336 }
337 return IcePhi;
338 }
339
340 Ice::Inst *convertBrInstruction(const BranchInst *Inst) {
341 if (Inst->isConditional()) {
342 Ice::Operand *Src = convertOperand(Inst, 0);
343 BasicBlock *BBThen = Inst->getSuccessor(0);
344 BasicBlock *BBElse = Inst->getSuccessor(1);
345 Ice::CfgNode *NodeThen = mapBasicBlockToNode(BBThen);
346 Ice::CfgNode *NodeElse = mapBasicBlockToNode(BBElse);
347 return Ice::InstBr::create(Func, Src, NodeThen, NodeElse);
348 } else {
349 BasicBlock *BBSucc = Inst->getSuccessor(0);
350 return Ice::InstBr::create(Func, mapBasicBlockToNode(BBSucc));
351 }
352 }
353
354 Ice::Inst *convertIntToPtrInstruction(const IntToPtrInst *Inst) {
355 Ice::Operand *Src = convertOperand(Inst, 0);
356 Ice::Variable *Dest = mapValueToIceVar(Inst, SubzeroPointerType);
357 return Ice::InstAssign::create(Func, Dest, Src);
358 }
359
360 Ice::Inst *convertPtrToIntInstruction(const PtrToIntInst *Inst) {
361 Ice::Operand *Src = convertOperand(Inst, 0);
362 Ice::Variable *Dest = mapValueToIceVar(Inst);
363 return Ice::InstAssign::create(Func, Dest, Src);
364 }
365
366 Ice::Inst *convertRetInstruction(const ReturnInst *Inst) {
367 Ice::Operand *RetOperand = convertOperand(Inst, 0);
368 if (RetOperand) {
369 return Ice::InstRet::create(Func, RetOperand);
370 } else {
371 return Ice::InstRet::create(Func);
372 }
373 }
374
375 Ice::Inst *convertCastInstruction(const Instruction *Inst,
376 Ice::InstCast::OpKind CastKind) {
377 Ice::Operand *Src = convertOperand(Inst, 0);
378 Ice::Variable *Dest = mapValueToIceVar(Inst);
379 return Ice::InstCast::create(Func, CastKind, Dest, Src);
380 }
381
382 Ice::Inst *convertICmpInstruction(const ICmpInst *Inst) {
383 Ice::Operand *Src0 = convertOperand(Inst, 0);
384 Ice::Operand *Src1 = convertOperand(Inst, 1);
385 Ice::Variable *Dest = mapValueToIceVar(Inst);
386
387 Ice::InstIcmp::ICond Cond;
388 switch (Inst->getPredicate()) {
389 default:
390 llvm_unreachable("ICmpInst predicate");
391 case CmpInst::ICMP_EQ:
392 Cond = Ice::InstIcmp::Eq;
393 break;
394 case CmpInst::ICMP_NE:
395 Cond = Ice::InstIcmp::Ne;
396 break;
397 case CmpInst::ICMP_UGT:
398 Cond = Ice::InstIcmp::Ugt;
399 break;
400 case CmpInst::ICMP_UGE:
401 Cond = Ice::InstIcmp::Uge;
402 break;
403 case CmpInst::ICMP_ULT:
404 Cond = Ice::InstIcmp::Ult;
405 break;
406 case CmpInst::ICMP_ULE:
407 Cond = Ice::InstIcmp::Ule;
408 break;
409 case CmpInst::ICMP_SGT:
410 Cond = Ice::InstIcmp::Sgt;
411 break;
412 case CmpInst::ICMP_SGE:
413 Cond = Ice::InstIcmp::Sge;
414 break;
415 case CmpInst::ICMP_SLT:
416 Cond = Ice::InstIcmp::Slt;
417 break;
418 case CmpInst::ICMP_SLE:
419 Cond = Ice::InstIcmp::Sle;
420 break;
421 }
422
423 return Ice::InstIcmp::create(Func, Cond, Dest, Src0, Src1);
424 }
425
426 Ice::Inst *convertFCmpInstruction(const FCmpInst *Inst) {
427 Ice::Operand *Src0 = convertOperand(Inst, 0);
428 Ice::Operand *Src1 = convertOperand(Inst, 1);
429 Ice::Variable *Dest = mapValueToIceVar(Inst);
430
431 Ice::InstFcmp::FCond Cond;
432 switch (Inst->getPredicate()) {
433
434 default:
435 llvm_unreachable("FCmpInst predicate");
436
437 case CmpInst::FCMP_FALSE:
438 Cond = Ice::InstFcmp::False;
439 break;
440 case CmpInst::FCMP_OEQ:
441 Cond = Ice::InstFcmp::Oeq;
442 break;
443 case CmpInst::FCMP_OGT:
444 Cond = Ice::InstFcmp::Ogt;
445 break;
446 case CmpInst::FCMP_OGE:
447 Cond = Ice::InstFcmp::Oge;
448 break;
449 case CmpInst::FCMP_OLT:
450 Cond = Ice::InstFcmp::Olt;
451 break;
452 case CmpInst::FCMP_OLE:
453 Cond = Ice::InstFcmp::Ole;
454 break;
455 case CmpInst::FCMP_ONE:
456 Cond = Ice::InstFcmp::One;
457 break;
458 case CmpInst::FCMP_ORD:
459 Cond = Ice::InstFcmp::Ord;
460 break;
461 case CmpInst::FCMP_UEQ:
462 Cond = Ice::InstFcmp::Ueq;
463 break;
464 case CmpInst::FCMP_UGT:
465 Cond = Ice::InstFcmp::Ugt;
466 break;
467 case CmpInst::FCMP_UGE:
468 Cond = Ice::InstFcmp::Uge;
469 break;
470 case CmpInst::FCMP_ULT:
471 Cond = Ice::InstFcmp::Ult;
472 break;
473 case CmpInst::FCMP_ULE:
474 Cond = Ice::InstFcmp::Ule;
475 break;
476 case CmpInst::FCMP_UNE:
477 Cond = Ice::InstFcmp::Une;
478 break;
479 case CmpInst::FCMP_UNO:
480 Cond = Ice::InstFcmp::Uno;
481 break;
482 case CmpInst::FCMP_TRUE:
483 Cond = Ice::InstFcmp::True;
484 break;
485 }
486
487 return Ice::InstFcmp::create(Func, Cond, Dest, Src0, Src1);
488 }
489
490 Ice::Inst *convertSelectInstruction(const SelectInst *Inst) {
491 Ice::Variable *Dest = mapValueToIceVar(Inst);
492 Ice::Operand *Cond = convertValue(Inst->getCondition());
493 Ice::Operand *Source1 = convertValue(Inst->getTrueValue());
494 Ice::Operand *Source2 = convertValue(Inst->getFalseValue());
495 return Ice::InstSelect::create(Func, Dest, Cond, Source1, Source2);
496 }
497
498 Ice::Inst *convertSwitchInstruction(const SwitchInst *Inst) {
499 Ice::Operand *Source = convertValue(Inst->getCondition());
500 Ice::CfgNode *LabelDefault = mapBasicBlockToNode(Inst->getDefaultDest());
501 unsigned NumCases = Inst->getNumCases();
502 Ice::InstSwitch *Switch =
503 Ice::InstSwitch::create(Func, NumCases, Source, LabelDefault);
504 unsigned CurrentCase = 0;
505 for (SwitchInst::ConstCaseIt I = Inst->case_begin(), E = Inst->case_end();
506 I != E; ++I, ++CurrentCase) {
507 uint64_t CaseValue = I.getCaseValue()->getZExtValue();
508 Ice::CfgNode *CaseSuccessor = mapBasicBlockToNode(I.getCaseSuccessor());
509 Switch->addBranch(CurrentCase, CaseValue, CaseSuccessor);
510 }
511 return Switch;
512 }
513
514 Ice::Inst *convertCallInstruction(const CallInst *Inst) {
515 Ice::Variable *Dest = mapValueToIceVar(Inst);
516 Ice::Operand *CallTarget = convertValue(Inst->getCalledValue());
517 unsigned NumArgs = Inst->getNumArgOperands();
518 // Note: Subzero doesn't (yet) do anything special with the Tail
519 // flag in the bitcode, i.e. CallInst::isTailCall().
520 Ice::InstCall *NewInst =
521 Ice::InstCall::create(Func, NumArgs, Dest, CallTarget);
522 for (unsigned i = 0; i < NumArgs; ++i) {
523 NewInst->addArg(convertOperand(Inst, i));
524 }
525 return NewInst;
526 }
527
528 Ice::Inst *convertAllocaInstruction(const AllocaInst *Inst) {
529 // PNaCl bitcode only contains allocas of byte-granular objects.
530 Ice::Operand *ByteCount = convertValue(Inst->getArraySize());
531 uint32_t Align = Inst->getAlignment();
532 Ice::Variable *Dest = mapValueToIceVar(Inst, SubzeroPointerType);
533
534 return Ice::InstAlloca::create(Func, ByteCount, Align, Dest);
535 }
536
537 Ice::Inst *convertUnreachableInstruction(const UnreachableInst *Inst) {
538 return Ice::InstUnreachable::create(Func);
539 }
540
541 Ice::CfgNode *convertBasicBlock(const BasicBlock *BB) {
542 Ice::CfgNode *Node = mapBasicBlockToNode(BB);
543 for (BasicBlock::const_iterator II = BB->begin(), II_e = BB->end();
544 II != II_e; ++II) {
545 Ice::Inst *Inst = convertInstruction(II);
546 Node->appendInst(Inst);
547 }
548 return Node;
549 }
550
551 private:
552 // Data
553 Ice::GlobalContext *Ctx;
554 Ice::Cfg *Func;
555 Ice::CfgNode *CurrentNode;
556 Ice::Type SubzeroPointerType;
557 std::map<const Value *, Ice::Variable *> VarMap;
558 std::map<const BasicBlock *, Ice::CfgNode *> NodeMap;
559 };
560
561 static cl::list<Ice::VerboseItem> VerboseList(
562 "verbose", cl::CommaSeparated,
563 cl::desc("Verbose options (can be comma-separated):"),
564 cl::values(
565 clEnumValN(Ice::IceV_Instructions, "inst", "Print basic instructions"),
566 clEnumValN(Ice::IceV_Deleted, "del", "Include deleted instructions"),
567 clEnumValN(Ice::IceV_InstNumbers, "instnum",
568 "Print instruction numbers"),
569 clEnumValN(Ice::IceV_Preds, "pred", "Show predecessors"),
570 clEnumValN(Ice::IceV_Succs, "succ", "Show successors"),
571 clEnumValN(Ice::IceV_Liveness, "live", "Liveness information"),
572 clEnumValN(Ice::IceV_RegManager, "rmgr", "Register manager status"),
573 clEnumValN(Ice::IceV_RegOrigins, "orig", "Physical register origins"),
574 clEnumValN(Ice::IceV_LinearScan, "regalloc", "Linear scan details"),
575 clEnumValN(Ice::IceV_Frame, "frame", "Stack frame layout details"),
576 clEnumValN(Ice::IceV_Timing, "time", "Pass timing details"),
577 clEnumValN(Ice::IceV_All, "all", "Use all verbose options"),
578 clEnumValN(Ice::IceV_None, "none", "No verbosity"), clEnumValEnd));
579 static cl::opt<std::string> IRFilename(cl::Positional, cl::desc("<IR file>"),
580 cl::Required);
581 static cl::opt<std::string> OutputFilename("o",
582 cl::desc("Override output filename"),
583 cl::init("-"),
584 cl::value_desc("filename"));
585 static cl::opt<std::string>
586 TestPrefix("prefix", cl::desc("Prepend a prefix to symbol names for testing"),
587 cl::init(""), cl::value_desc("prefix"));
588 static cl::opt<bool>
589 DisableInternal("external",
590 cl::desc("Disable 'internal' linkage type for testing"));
591 static cl::opt<bool>
592 DisableTranslation("notranslate", cl::desc("Disable Subzero translation"));
593
594 static cl::opt<bool> SubzeroTimingEnabled(
595 "timing", cl::desc("Enable breakdown timing of Subzero translation"));
596
597 int main(int argc, char **argv) {
598 cl::ParseCommandLineOptions(argc, argv);
599
600 // Parse the input LLVM IR file into a module.
601 SMDiagnostic Err;
602 Module *Mod;
603
604 {
605 Ice::Timer T;
606 Mod = ParseIRFile(IRFilename, Err, getGlobalContext());
607
608 if (SubzeroTimingEnabled) {
609 std::cerr << "[Subzero timing] IR Parsing: " << T.getElapsedSec()
610 << " sec\n";
611 }
612 }
613
614 if (!Mod) {
615 Err.print(argv[0], errs());
616 return 1;
617 }
618
619 Ice::VerboseMask VMask = Ice::IceV_None;
620 for (unsigned i = 0; i != VerboseList.size(); ++i)
621 VMask |= VerboseList[i];
622
623 std::ofstream Ofs;
624 if (OutputFilename != "-") {
625 Ofs.open(OutputFilename.c_str(), std::ofstream::out);
626 }
627 raw_os_ostream *Os =
628 new raw_os_ostream(OutputFilename == "-" ? std::cout : Ofs);
629 Os->SetUnbuffered();
630
631 Ice::GlobalContext Ctx(Os, Os, VMask, TestPrefix);
632
633 for (Module::const_iterator I = Mod->begin(), E = Mod->end(); I != E; ++I) {
634 if (I->empty())
635 continue;
636 LLVM2ICEConverter FunctionConverter(&Ctx);
637
638 Ice::Timer TConvert;
639 Ice::Cfg *Func = FunctionConverter.convertFunction(I);
640 if (DisableInternal)
641 Func->setInternal(false);
642
643 if (SubzeroTimingEnabled) {
644 std::cerr << "[Subzero timing] Convert function "
645 << Func->getFunctionName() << ": " << TConvert.getElapsedSec()
646 << " sec\n";
647 }
648
649 if (DisableTranslation) {
650 Func->dump();
651 }
652 }
653
654 return 0;
655 }
OLDNEW
« no previous file with comments | « src/IceTypes.def ('k') | szdiff.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698