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