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 return SubzeroPointerType; |
| 155 case Type::FunctionTyID: |
| 156 return SubzeroPointerType; |
| 157 default: |
| 158 report_fatal_error(std::string("Invalid PNaCl type: ") + |
| 159 LLVMObjectAsString(Ty)); |
| 160 } |
| 161 |
| 162 llvm_unreachable("convertType"); |
| 163 return IceType_void; |
| 164 } |
| 165 |
| 166 // Given a LLVM instruction and an operand number, produce the IceOperand this |
| 167 // refers to. If there's no such operand, return NULL. |
| 168 IceOperand *convertOperand(const Instruction *Inst, unsigned OpNum) { |
| 169 if (OpNum >= Inst->getNumOperands()) { |
| 170 return NULL; |
| 171 } |
| 172 const Value *Op = Inst->getOperand(OpNum); |
| 173 return convertValue(Op); |
| 174 } |
| 175 |
| 176 IceOperand *convertValue(const Value *Op) { |
| 177 if (const Constant *Const = dyn_cast<Constant>(Op)) { |
| 178 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Const)) { |
| 179 return Cfg->getConstantSym(convertType(GV->getType()), GV, 0, |
| 180 GV->getName()); |
| 181 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(Const)) { |
| 182 return Cfg->getConstantInt(convertIntegerType(CI->getType()), |
| 183 CI->getZExtValue()); |
| 184 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Const)) { |
| 185 IceType Type = convertType(CFP->getType()); |
| 186 if (Type == IceType_f32) |
| 187 return Cfg->getConstantFloat(CFP->getValueAPF().convertToFloat()); |
| 188 else if (Type == IceType_f64) |
| 189 return Cfg->getConstantDouble(CFP->getValueAPF().convertToDouble()); |
| 190 assert(0 && "Unexpected floating point type"); |
| 191 return NULL; |
| 192 } else { |
| 193 assert(0 && "Unhandled constant type"); |
| 194 return NULL; |
| 195 } |
| 196 } else { |
| 197 return mapValueToIceVar(Op); |
| 198 } |
| 199 } |
| 200 |
| 201 // Note: this currently assumes a 1x1 mapping between LLVM IR and Ice |
| 202 // instructions. |
| 203 IceInst *convertInstruction(const Instruction *Inst) { |
| 204 switch (Inst->getOpcode()) { |
| 205 case Instruction::PHI: |
| 206 return convertPHINodeInstruction(cast<PHINode>(Inst)); |
| 207 case Instruction::Br: |
| 208 return convertBrInstruction(cast<BranchInst>(Inst)); |
| 209 case Instruction::Ret: |
| 210 return convertRetInstruction(cast<ReturnInst>(Inst)); |
| 211 case Instruction::IntToPtr: |
| 212 return convertIntToPtrInstruction(cast<IntToPtrInst>(Inst)); |
| 213 case Instruction::PtrToInt: |
| 214 return convertPtrToIntInstruction(cast<PtrToIntInst>(Inst)); |
| 215 case Instruction::ICmp: |
| 216 return convertICmpInstruction(cast<ICmpInst>(Inst)); |
| 217 case Instruction::FCmp: |
| 218 return convertFCmpInstruction(cast<FCmpInst>(Inst)); |
| 219 case Instruction::Select: |
| 220 return convertSelectInstruction(cast<SelectInst>(Inst)); |
| 221 case Instruction::Switch: |
| 222 return convertSwitchInstruction(cast<SwitchInst>(Inst)); |
| 223 case Instruction::Load: |
| 224 return convertLoadInstruction(cast<LoadInst>(Inst)); |
| 225 case Instruction::Store: |
| 226 return convertStoreInstruction(cast<StoreInst>(Inst)); |
| 227 case Instruction::ZExt: |
| 228 return convertCastInstruction(cast<ZExtInst>(Inst), IceInstCast::Zext); |
| 229 case Instruction::SExt: |
| 230 return convertCastInstruction(cast<SExtInst>(Inst), IceInstCast::Sext); |
| 231 case Instruction::Trunc: |
| 232 return convertCastInstruction(cast<TruncInst>(Inst), IceInstCast::Trunc); |
| 233 case Instruction::FPTrunc: |
| 234 return convertCastInstruction(cast<FPTruncInst>(Inst), |
| 235 IceInstCast::Fptrunc); |
| 236 case Instruction::FPExt: |
| 237 return convertCastInstruction(cast<FPExtInst>(Inst), IceInstCast::Fpext); |
| 238 case Instruction::FPToSI: |
| 239 return convertCastInstruction(cast<FPToSIInst>(Inst), |
| 240 IceInstCast::Fptosi); |
| 241 case Instruction::FPToUI: |
| 242 return convertCastInstruction(cast<FPToUIInst>(Inst), |
| 243 IceInstCast::Fptoui); |
| 244 case Instruction::SIToFP: |
| 245 return convertCastInstruction(cast<SIToFPInst>(Inst), |
| 246 IceInstCast::Sitofp); |
| 247 case Instruction::UIToFP: |
| 248 return convertCastInstruction(cast<UIToFPInst>(Inst), |
| 249 IceInstCast::Uitofp); |
| 250 case Instruction::BitCast: |
| 251 return convertCastInstruction(cast<BitCastInst>(Inst), |
| 252 IceInstCast::Bitcast); |
| 253 case Instruction::Add: |
| 254 return convertArithInstruction(Inst, IceInstArithmetic::Add); |
| 255 case Instruction::Sub: |
| 256 return convertArithInstruction(Inst, IceInstArithmetic::Sub); |
| 257 case Instruction::Mul: |
| 258 return convertArithInstruction(Inst, IceInstArithmetic::Mul); |
| 259 case Instruction::UDiv: |
| 260 return convertArithInstruction(Inst, IceInstArithmetic::Udiv); |
| 261 case Instruction::SDiv: |
| 262 return convertArithInstruction(Inst, IceInstArithmetic::Sdiv); |
| 263 case Instruction::URem: |
| 264 return convertArithInstruction(Inst, IceInstArithmetic::Urem); |
| 265 case Instruction::SRem: |
| 266 return convertArithInstruction(Inst, IceInstArithmetic::Srem); |
| 267 case Instruction::Shl: |
| 268 return convertArithInstruction(Inst, IceInstArithmetic::Shl); |
| 269 case Instruction::LShr: |
| 270 return convertArithInstruction(Inst, IceInstArithmetic::Lshr); |
| 271 case Instruction::AShr: |
| 272 return convertArithInstruction(Inst, IceInstArithmetic::Ashr); |
| 273 case Instruction::FAdd: |
| 274 return convertArithInstruction(Inst, IceInstArithmetic::Fadd); |
| 275 case Instruction::FSub: |
| 276 return convertArithInstruction(Inst, IceInstArithmetic::Fsub); |
| 277 case Instruction::FMul: |
| 278 return convertArithInstruction(Inst, IceInstArithmetic::Fmul); |
| 279 case Instruction::FDiv: |
| 280 return convertArithInstruction(Inst, IceInstArithmetic::Fdiv); |
| 281 case Instruction::FRem: |
| 282 return convertArithInstruction(Inst, IceInstArithmetic::Frem); |
| 283 case Instruction::And: |
| 284 return convertArithInstruction(Inst, IceInstArithmetic::And); |
| 285 case Instruction::Or: |
| 286 return convertArithInstruction(Inst, IceInstArithmetic::Or); |
| 287 case Instruction::Xor: |
| 288 return convertArithInstruction(Inst, IceInstArithmetic::Xor); |
| 289 case Instruction::Call: |
| 290 return convertCallInstruction(cast<CallInst>(Inst)); |
| 291 case Instruction::Alloca: |
| 292 return convertAllocaInstruction(cast<AllocaInst>(Inst)); |
| 293 case Instruction::Unreachable: |
| 294 return convertUnreachableInstruction(cast<UnreachableInst>(Inst)); |
| 295 default: |
| 296 report_fatal_error(std::string("Invalid PNaCl instruction: ") + |
| 297 LLVMObjectAsString(Inst)); |
| 298 } |
| 299 |
| 300 llvm_unreachable("convertInstruction"); |
| 301 return NULL; |
| 302 } |
| 303 |
| 304 IceInst *convertLoadInstruction(const LoadInst *Inst) { |
| 305 IceOperand *Src = convertOperand(Inst, 0); |
| 306 IceVariable *Dest = mapValueToIceVar(Inst); |
| 307 return IceInstLoad::create(Cfg, Dest, Src); |
| 308 } |
| 309 |
| 310 IceInst *convertStoreInstruction(const StoreInst *Inst) { |
| 311 IceOperand *Addr = convertOperand(Inst, 1); |
| 312 IceOperand *Val = convertOperand(Inst, 0); |
| 313 return IceInstStore::create(Cfg, Val, Addr); |
| 314 } |
| 315 |
| 316 IceInst *convertArithInstruction(const Instruction *Inst, |
| 317 IceInstArithmetic::OpKind Opcode) { |
| 318 const BinaryOperator *BinOp = cast<BinaryOperator>(Inst); |
| 319 IceOperand *Src0 = convertOperand(Inst, 0); |
| 320 IceOperand *Src1 = convertOperand(Inst, 1); |
| 321 IceVariable *Dest = mapValueToIceVar(BinOp); |
| 322 return IceInstArithmetic::create(Cfg, Opcode, Dest, Src0, Src1); |
| 323 } |
| 324 |
| 325 IceInst *convertPHINodeInstruction(const PHINode *Inst) { |
| 326 unsigned NumValues = Inst->getNumIncomingValues(); |
| 327 IceInstPhi *IcePhi = |
| 328 IceInstPhi::create(Cfg, NumValues, mapValueToIceVar(Inst)); |
| 329 for (unsigned N = 0, E = NumValues; N != E; ++N) { |
| 330 IcePhi->addArgument(convertOperand(Inst, N), |
| 331 mapBasicBlockToNode(Inst->getIncomingBlock(N))); |
| 332 } |
| 333 return IcePhi; |
| 334 } |
| 335 |
| 336 IceInst *convertBrInstruction(const BranchInst *Inst) { |
| 337 if (Inst->isConditional()) { |
| 338 IceOperand *Src = convertOperand(Inst, 0); |
| 339 BasicBlock *BBThen = Inst->getSuccessor(0); |
| 340 BasicBlock *BBElse = Inst->getSuccessor(1); |
| 341 IceCfgNode *NodeThen = mapBasicBlockToNode(BBThen); |
| 342 IceCfgNode *NodeElse = mapBasicBlockToNode(BBElse); |
| 343 return IceInstBr::create(Cfg, Src, NodeThen, NodeElse); |
| 344 } else { |
| 345 BasicBlock *BBSucc = Inst->getSuccessor(0); |
| 346 return IceInstBr::create(Cfg, mapBasicBlockToNode(BBSucc)); |
| 347 } |
| 348 } |
| 349 |
| 350 IceInst *convertIntToPtrInstruction(const IntToPtrInst *Inst) { |
| 351 IceOperand *Src = convertOperand(Inst, 0); |
| 352 IceVariable *Dest = mapValueToIceVar(Inst, SubzeroPointerType); |
| 353 return IceInstAssign::create(Cfg, Dest, Src); |
| 354 } |
| 355 |
| 356 IceInst *convertPtrToIntInstruction(const PtrToIntInst *Inst) { |
| 357 IceOperand *Src = convertOperand(Inst, 0); |
| 358 IceVariable *Dest = mapValueToIceVar(Inst); |
| 359 return IceInstAssign::create(Cfg, Dest, Src); |
| 360 } |
| 361 |
| 362 IceInst *convertRetInstruction(const ReturnInst *Inst) { |
| 363 IceOperand *RetOperand = convertOperand(Inst, 0); |
| 364 if (RetOperand) { |
| 365 return IceInstRet::create(Cfg, RetOperand); |
| 366 } else { |
| 367 return IceInstRet::create(Cfg); |
| 368 } |
| 369 } |
| 370 |
| 371 IceInst *convertCastInstruction(const Instruction *Inst, |
| 372 IceInstCast::OpKind CastKind) { |
| 373 IceOperand *Src = convertOperand(Inst, 0); |
| 374 IceVariable *Dest = mapValueToIceVar(Inst); |
| 375 return IceInstCast::create(Cfg, CastKind, Dest, Src); |
| 376 } |
| 377 |
| 378 IceInst *convertICmpInstruction(const ICmpInst *Inst) { |
| 379 IceOperand *Src0 = convertOperand(Inst, 0); |
| 380 IceOperand *Src1 = convertOperand(Inst, 1); |
| 381 IceVariable *Dest = mapValueToIceVar(Inst); |
| 382 |
| 383 IceInstIcmp::ICond Cond; |
| 384 switch (Inst->getPredicate()) { |
| 385 default: |
| 386 llvm_unreachable("ICmpInst predicate"); |
| 387 case CmpInst::ICMP_EQ: |
| 388 Cond = IceInstIcmp::Eq; |
| 389 break; |
| 390 case CmpInst::ICMP_NE: |
| 391 Cond = IceInstIcmp::Ne; |
| 392 break; |
| 393 case CmpInst::ICMP_UGT: |
| 394 Cond = IceInstIcmp::Ugt; |
| 395 break; |
| 396 case CmpInst::ICMP_UGE: |
| 397 Cond = IceInstIcmp::Uge; |
| 398 break; |
| 399 case CmpInst::ICMP_ULT: |
| 400 Cond = IceInstIcmp::Ult; |
| 401 break; |
| 402 case CmpInst::ICMP_ULE: |
| 403 Cond = IceInstIcmp::Ule; |
| 404 break; |
| 405 case CmpInst::ICMP_SGT: |
| 406 Cond = IceInstIcmp::Sgt; |
| 407 break; |
| 408 case CmpInst::ICMP_SGE: |
| 409 Cond = IceInstIcmp::Sge; |
| 410 break; |
| 411 case CmpInst::ICMP_SLT: |
| 412 Cond = IceInstIcmp::Slt; |
| 413 break; |
| 414 case CmpInst::ICMP_SLE: |
| 415 Cond = IceInstIcmp::Sle; |
| 416 break; |
| 417 } |
| 418 |
| 419 return IceInstIcmp::create(Cfg, Cond, Dest, Src0, Src1); |
| 420 } |
| 421 |
| 422 IceInst *convertFCmpInstruction(const FCmpInst *Inst) { |
| 423 IceOperand *Src0 = convertOperand(Inst, 0); |
| 424 IceOperand *Src1 = convertOperand(Inst, 1); |
| 425 IceVariable *Dest = mapValueToIceVar(Inst); |
| 426 |
| 427 IceInstFcmp::FCond Cond; |
| 428 switch (Inst->getPredicate()) { |
| 429 |
| 430 default: |
| 431 llvm_unreachable("FCmpInst predicate"); |
| 432 |
| 433 case CmpInst::FCMP_FALSE: |
| 434 Cond = IceInstFcmp::False; |
| 435 break; |
| 436 case CmpInst::FCMP_OEQ: |
| 437 Cond = IceInstFcmp::Oeq; |
| 438 break; |
| 439 case CmpInst::FCMP_OGT: |
| 440 Cond = IceInstFcmp::Ogt; |
| 441 break; |
| 442 case CmpInst::FCMP_OGE: |
| 443 Cond = IceInstFcmp::Oge; |
| 444 break; |
| 445 case CmpInst::FCMP_OLT: |
| 446 Cond = IceInstFcmp::Olt; |
| 447 break; |
| 448 case CmpInst::FCMP_OLE: |
| 449 Cond = IceInstFcmp::Ole; |
| 450 break; |
| 451 case CmpInst::FCMP_ONE: |
| 452 Cond = IceInstFcmp::One; |
| 453 break; |
| 454 case CmpInst::FCMP_ORD: |
| 455 Cond = IceInstFcmp::Ord; |
| 456 break; |
| 457 case CmpInst::FCMP_UEQ: |
| 458 Cond = IceInstFcmp::Ueq; |
| 459 break; |
| 460 case CmpInst::FCMP_UGT: |
| 461 Cond = IceInstFcmp::Ugt; |
| 462 break; |
| 463 case CmpInst::FCMP_UGE: |
| 464 Cond = IceInstFcmp::Uge; |
| 465 break; |
| 466 case CmpInst::FCMP_ULT: |
| 467 Cond = IceInstFcmp::Ult; |
| 468 break; |
| 469 case CmpInst::FCMP_ULE: |
| 470 Cond = IceInstFcmp::Ule; |
| 471 break; |
| 472 case CmpInst::FCMP_UNE: |
| 473 Cond = IceInstFcmp::Une; |
| 474 break; |
| 475 case CmpInst::FCMP_UNO: |
| 476 Cond = IceInstFcmp::Uno; |
| 477 break; |
| 478 case CmpInst::FCMP_TRUE: |
| 479 Cond = IceInstFcmp::True; |
| 480 break; |
| 481 } |
| 482 |
| 483 return IceInstFcmp::create(Cfg, Cond, Dest, Src0, Src1); |
| 484 } |
| 485 |
| 486 IceInst *convertSelectInstruction(const SelectInst *Inst) { |
| 487 IceVariable *Dest = mapValueToIceVar(Inst); |
| 488 IceOperand *Cond = convertValue(Inst->getCondition()); |
| 489 IceOperand *Source1 = convertValue(Inst->getTrueValue()); |
| 490 IceOperand *Source2 = convertValue(Inst->getFalseValue()); |
| 491 return IceInstSelect::create(Cfg, Dest, Cond, Source1, Source2); |
| 492 } |
| 493 |
| 494 IceInst *convertSwitchInstruction(const SwitchInst *Inst) { |
| 495 IceOperand *Source = convertValue(Inst->getCondition()); |
| 496 IceCfgNode *LabelDefault = mapBasicBlockToNode(Inst->getDefaultDest()); |
| 497 unsigned NumCases = Inst->getNumCases(); |
| 498 IceInstSwitch *Switch = |
| 499 IceInstSwitch::create(Cfg, NumCases, Source, LabelDefault); |
| 500 unsigned CurrentCase = 0; |
| 501 for (SwitchInst::ConstCaseIt I = Inst->case_begin(), E = Inst->case_end(); |
| 502 I != E; ++I, ++CurrentCase) { |
| 503 uint64_t CaseValue = I.getCaseValue()->getZExtValue(); |
| 504 IceCfgNode *CaseSuccessor = mapBasicBlockToNode(I.getCaseSuccessor()); |
| 505 Switch->addBranch(CurrentCase, CaseValue, CaseSuccessor); |
| 506 } |
| 507 return Switch; |
| 508 } |
| 509 |
| 510 IceInst *convertCallInstruction(const CallInst *Inst) { |
| 511 IceVariable *Dest = mapValueToIceVar(Inst); |
| 512 IceOperand *CallTarget = convertValue(Inst->getCalledValue()); |
| 513 unsigned NumArgs = Inst->getNumArgOperands(); |
| 514 IceInstCall *NewInst = |
| 515 IceInstCall::create(Cfg, NumArgs, Dest, CallTarget, Inst->isTailCall()); |
| 516 for (unsigned i = 0; i < NumArgs; ++i) { |
| 517 NewInst->addArg(convertOperand(Inst, i)); |
| 518 } |
| 519 return NewInst; |
| 520 } |
| 521 |
| 522 IceInst *convertAllocaInstruction(const AllocaInst *Inst) { |
| 523 // PNaCl bitcode only contains allocas of byte-granular objects. |
| 524 IceOperand *ByteCount = convertValue(Inst->getArraySize()); |
| 525 uint32_t Align = Inst->getAlignment(); |
| 526 IceVariable *Dest = mapValueToIceVar(Inst, SubzeroPointerType); |
| 527 |
| 528 return IceInstAlloca::create(Cfg, ByteCount, Align, Dest); |
| 529 } |
| 530 |
| 531 IceInst *convertUnreachableInstruction(const UnreachableInst *Inst) { |
| 532 return IceInstUnreachable::create(Cfg); |
| 533 } |
| 534 |
| 535 IceCfgNode *convertBasicBlock(const BasicBlock *BB) { |
| 536 IceCfgNode *Node = mapBasicBlockToNode(BB); |
| 537 for (BasicBlock::const_iterator II = BB->begin(), II_e = BB->end(); |
| 538 II != II_e; ++II) { |
| 539 IceInst *Inst = convertInstruction(II); |
| 540 Node->appendInst(Inst); |
| 541 } |
| 542 return Node; |
| 543 } |
| 544 |
| 545 private: |
| 546 // Data |
| 547 IceCfg *Cfg; |
| 548 IceCfgNode *CurrentNode; |
| 549 IceType SubzeroPointerType; |
| 550 std::map<const Value *, IceVariable *> VarMap; |
| 551 std::map<const BasicBlock *, IceCfgNode *> NodeMap; |
| 552 }; |
| 553 |
| 554 static cl::list<IceVerbose> VerboseList( |
| 555 "verbose", cl::CommaSeparated, |
| 556 cl::desc("Verbose options (can be comma-separated):"), |
| 557 cl::values( |
| 558 clEnumValN(IceV_Instructions, "inst", "Print basic instructions"), |
| 559 clEnumValN(IceV_Deleted, "del", "Include deleted instructions"), |
| 560 clEnumValN(IceV_InstNumbers, "instnum", "Print instruction numbers"), |
| 561 clEnumValN(IceV_Preds, "pred", "Show predecessors"), |
| 562 clEnumValN(IceV_Succs, "succ", "Show successors"), |
| 563 clEnumValN(IceV_Liveness, "live", "Liveness information"), |
| 564 clEnumValN(IceV_RegManager, "rmgr", "Register manager status"), |
| 565 clEnumValN(IceV_RegOrigins, "orig", "Physical register origins"), |
| 566 clEnumValN(IceV_LinearScan, "regalloc", "Linear scan details"), |
| 567 clEnumValN(IceV_Frame, "frame", "Stack frame layout details"), |
| 568 clEnumValN(IceV_Timing, "time", "Pass timing details"), |
| 569 clEnumValN(IceV_All, "all", "Use all verbose options"), |
| 570 clEnumValN(IceV_None, "none", "No verbosity"), clEnumValEnd)); |
| 571 static cl::opt<std::string> IRFilename(cl::Positional, cl::desc("<IR file>"), |
| 572 cl::Required); |
| 573 static cl::opt<std::string> OutputFilename("o", |
| 574 cl::desc("Override output filename"), |
| 575 cl::init("-"), |
| 576 cl::value_desc("filename")); |
| 577 static cl::opt<std::string> |
| 578 TestPrefix("prefix", cl::desc("Prepend a prefix to symbol names for testing"), |
| 579 cl::init(""), cl::value_desc("prefix")); |
| 580 static cl::opt<bool> |
| 581 DisableInternal("external", |
| 582 cl::desc("Disable 'internal' linkage type for testing")); |
| 583 static cl::opt<bool> |
| 584 DisableTranslation("notranslate", cl::desc("Disable Subzero translation")); |
| 585 |
| 586 static cl::opt<bool> SubzeroTimingEnabled( |
| 587 "timing", cl::desc("Enable breakdown timing of Subzero translation")); |
| 588 |
| 589 int main(int argc, char **argv) { |
| 590 cl::ParseCommandLineOptions(argc, argv); |
| 591 |
| 592 // Parse the input LLVM IR file into a module. |
| 593 SMDiagnostic Err; |
| 594 Module *Mod; |
| 595 |
| 596 { |
| 597 IceTimer T; |
| 598 Mod = ParseIRFile(IRFilename, Err, getGlobalContext()); |
| 599 |
| 600 if (SubzeroTimingEnabled) { |
| 601 std::cerr << "[Subzero timing] IR Parsing: " << T.getElapsedSec() |
| 602 << " sec\n"; |
| 603 } |
| 604 } |
| 605 |
| 606 if (!Mod) { |
| 607 Err.print(argv[0], errs()); |
| 608 return 1; |
| 609 } |
| 610 |
| 611 IceVerboseMask VerboseMask = IceV_None; |
| 612 for (unsigned i = 0; i != VerboseList.size(); ++i) |
| 613 VerboseMask |= VerboseList[i]; |
| 614 |
| 615 std::ofstream Ofs; |
| 616 if (OutputFilename != "-") { |
| 617 Ofs.open(OutputFilename.c_str(), std::ofstream::out); |
| 618 } |
| 619 raw_os_ostream *Os = |
| 620 new raw_os_ostream(OutputFilename == "-" ? std::cout : Ofs); |
| 621 Os->SetUnbuffered(); |
| 622 |
| 623 for (Module::const_iterator I = Mod->begin(), E = Mod->end(); I != E; ++I) { |
| 624 if (I->empty()) |
| 625 continue; |
| 626 LLVM2ICEConverter FunctionConverter; |
| 627 |
| 628 IceTimer TConvert; |
| 629 IceCfg *Cfg = FunctionConverter.convertFunction(I); |
| 630 if (DisableInternal) |
| 631 Cfg->setInternal(false); |
| 632 |
| 633 if (SubzeroTimingEnabled) { |
| 634 std::cerr << "[Subzero timing] Convert function " << Cfg->getName() |
| 635 << ": " << TConvert.getElapsedSec() << " sec\n"; |
| 636 } |
| 637 |
| 638 Cfg->setTestPrefix(TestPrefix); |
| 639 Cfg->Str.Stream = Os; |
| 640 Cfg->Str.setVerbose(VerboseMask); |
| 641 if (DisableTranslation) { |
| 642 Cfg->dump(); |
| 643 } |
| 644 } |
| 645 |
| 646 return 0; |
| 647 } |
OLD | NEW |