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