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 "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), Cfg(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::IceCfg *convertFunction(const Function *F) { |
| 67 VarMap.clear(); |
| 68 NodeMap.clear(); |
| 69 Cfg = new Ice::IceCfg(Ctx); |
| 70 Cfg->setFunctionName(F->getName()); |
| 71 Cfg->setReturnType(convertType(F->getReturnType())); |
| 72 Cfg->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 Cfg->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 Cfg->setEntryNode(mapBasicBlockToNode(&F->getEntryBlock())); |
| 96 Cfg->computePredecessors(); |
| 97 |
| 98 return Cfg; |
| 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::IceType IceTy) { |
| 106 if (IceTy == Ice::IceType_void) |
| 107 return NULL; |
| 108 if (VarMap.find(V) == VarMap.end()) { |
| 109 assert(CurrentNode); |
| 110 VarMap[V] = Cfg->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] = Cfg->makeNode(BB->getName()); |
| 122 } |
| 123 return NodeMap[BB]; |
| 124 } |
| 125 |
| 126 Ice::IceType 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::IceType 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::IceType 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(Cfg, 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(Cfg, 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(Cfg, 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(Cfg, 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(Cfg, Src, NodeThen, NodeElse); |
| 348 } else { |
| 349 BasicBlock *BBSucc = Inst->getSuccessor(0); |
| 350 return Ice::InstBr::create(Cfg, 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(Cfg, 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(Cfg, 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(Cfg, RetOperand); |
| 370 } else { |
| 371 return Ice::InstRet::create(Cfg); |
| 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(Cfg, 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(Cfg, 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(Cfg, 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(Cfg, 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(Cfg, 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 Ice::InstCall *NewInst = Ice::InstCall::create( |
| 519 Cfg, NumArgs, Dest, CallTarget, Inst->isTailCall()); |
| 520 for (unsigned i = 0; i < NumArgs; ++i) { |
| 521 NewInst->addArg(convertOperand(Inst, i)); |
| 522 } |
| 523 return NewInst; |
| 524 } |
| 525 |
| 526 Ice::Inst *convertAllocaInstruction(const AllocaInst *Inst) { |
| 527 // PNaCl bitcode only contains allocas of byte-granular objects. |
| 528 Ice::Operand *ByteCount = convertValue(Inst->getArraySize()); |
| 529 uint32_t Align = Inst->getAlignment(); |
| 530 Ice::Variable *Dest = mapValueToIceVar(Inst, SubzeroPointerType); |
| 531 |
| 532 return Ice::InstAlloca::create(Cfg, ByteCount, Align, Dest); |
| 533 } |
| 534 |
| 535 Ice::Inst *convertUnreachableInstruction(const UnreachableInst *Inst) { |
| 536 return Ice::InstUnreachable::create(Cfg); |
| 537 } |
| 538 |
| 539 Ice::CfgNode *convertBasicBlock(const BasicBlock *BB) { |
| 540 Ice::CfgNode *Node = mapBasicBlockToNode(BB); |
| 541 for (BasicBlock::const_iterator II = BB->begin(), II_e = BB->end(); |
| 542 II != II_e; ++II) { |
| 543 Ice::Inst *Inst = convertInstruction(II); |
| 544 Node->appendInst(Inst); |
| 545 } |
| 546 return Node; |
| 547 } |
| 548 |
| 549 private: |
| 550 // Data |
| 551 Ice::GlobalContext *Ctx; |
| 552 Ice::IceCfg *Cfg; |
| 553 Ice::CfgNode *CurrentNode; |
| 554 Ice::IceType SubzeroPointerType; |
| 555 std::map<const Value *, Ice::Variable *> VarMap; |
| 556 std::map<const BasicBlock *, Ice::CfgNode *> NodeMap; |
| 557 }; |
| 558 |
| 559 static cl::list<Ice::IceVerbose> VerboseList( |
| 560 "verbose", cl::CommaSeparated, |
| 561 cl::desc("Verbose options (can be comma-separated):"), |
| 562 cl::values( |
| 563 clEnumValN(Ice::IceV_Instructions, "inst", "Print basic instructions"), |
| 564 clEnumValN(Ice::IceV_Deleted, "del", "Include deleted instructions"), |
| 565 clEnumValN(Ice::IceV_InstNumbers, "instnum", |
| 566 "Print instruction numbers"), |
| 567 clEnumValN(Ice::IceV_Preds, "pred", "Show predecessors"), |
| 568 clEnumValN(Ice::IceV_Succs, "succ", "Show successors"), |
| 569 clEnumValN(Ice::IceV_Liveness, "live", "Liveness information"), |
| 570 clEnumValN(Ice::IceV_RegManager, "rmgr", "Register manager status"), |
| 571 clEnumValN(Ice::IceV_RegOrigins, "orig", "Physical register origins"), |
| 572 clEnumValN(Ice::IceV_LinearScan, "regalloc", "Linear scan details"), |
| 573 clEnumValN(Ice::IceV_Frame, "frame", "Stack frame layout details"), |
| 574 clEnumValN(Ice::IceV_Timing, "time", "Pass timing details"), |
| 575 clEnumValN(Ice::IceV_All, "all", "Use all verbose options"), |
| 576 clEnumValN(Ice::IceV_None, "none", "No verbosity"), clEnumValEnd)); |
| 577 static cl::opt<std::string> IRFilename(cl::Positional, cl::desc("<IR file>"), |
| 578 cl::Required); |
| 579 static cl::opt<std::string> OutputFilename("o", |
| 580 cl::desc("Override output filename"), |
| 581 cl::init("-"), |
| 582 cl::value_desc("filename")); |
| 583 static cl::opt<std::string> |
| 584 TestPrefix("prefix", cl::desc("Prepend a prefix to symbol names for testing"), |
| 585 cl::init(""), cl::value_desc("prefix")); |
| 586 static cl::opt<bool> |
| 587 DisableInternal("external", |
| 588 cl::desc("Disable 'internal' linkage type for testing")); |
| 589 static cl::opt<bool> |
| 590 DisableTranslation("notranslate", cl::desc("Disable Subzero translation")); |
| 591 |
| 592 static cl::opt<bool> SubzeroTimingEnabled( |
| 593 "timing", cl::desc("Enable breakdown timing of Subzero translation")); |
| 594 |
| 595 int main(int argc, char **argv) { |
| 596 cl::ParseCommandLineOptions(argc, argv); |
| 597 |
| 598 // Parse the input LLVM IR file into a module. |
| 599 SMDiagnostic Err; |
| 600 Module *Mod; |
| 601 |
| 602 { |
| 603 Ice::IceTimer T; |
| 604 Mod = ParseIRFile(IRFilename, Err, getGlobalContext()); |
| 605 |
| 606 if (SubzeroTimingEnabled) { |
| 607 std::cerr << "[Subzero timing] IR Parsing: " << T.getElapsedSec() |
| 608 << " sec\n"; |
| 609 } |
| 610 } |
| 611 |
| 612 if (!Mod) { |
| 613 Err.print(argv[0], errs()); |
| 614 return 1; |
| 615 } |
| 616 |
| 617 Ice::IceVerboseMask VerboseMask = Ice::IceV_None; |
| 618 for (unsigned i = 0; i != VerboseList.size(); ++i) |
| 619 VerboseMask |= VerboseList[i]; |
| 620 |
| 621 std::ofstream Ofs; |
| 622 if (OutputFilename != "-") { |
| 623 Ofs.open(OutputFilename.c_str(), std::ofstream::out); |
| 624 } |
| 625 raw_os_ostream *Os = |
| 626 new raw_os_ostream(OutputFilename == "-" ? std::cout : Ofs); |
| 627 Os->SetUnbuffered(); |
| 628 |
| 629 Ice::GlobalContext Ctx(Os, Os, VerboseMask, TestPrefix); |
| 630 |
| 631 for (Module::const_iterator I = Mod->begin(), E = Mod->end(); I != E; ++I) { |
| 632 if (I->empty()) |
| 633 continue; |
| 634 LLVM2ICEConverter FunctionConverter(&Ctx); |
| 635 |
| 636 Ice::IceTimer TConvert; |
| 637 Ice::IceCfg *Cfg = FunctionConverter.convertFunction(I); |
| 638 if (DisableInternal) |
| 639 Cfg->setInternal(false); |
| 640 |
| 641 if (SubzeroTimingEnabled) { |
| 642 std::cerr << "[Subzero timing] Convert function " |
| 643 << Cfg->getFunctionName() << ": " << TConvert.getElapsedSec() |
| 644 << " sec\n"; |
| 645 } |
| 646 |
| 647 if (DisableTranslation) { |
| 648 Cfg->dump(); |
| 649 } |
| 650 } |
| 651 |
| 652 return 0; |
| 653 } |
OLD | NEW |