| OLD | NEW |
| 1 //===- subzero/src/IceConverter.cpp - Converts LLVM to Ice ---------------===// | 1 //===- subzero/src/IceConverter.cpp - Converts LLVM to Ice ---------------===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This file implements the LLVM to ICE converter. | 10 // This file implements the LLVM to ICE converter. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 | 41 |
| 42 // Debugging helper | 42 // Debugging helper |
| 43 template <typename T> static std::string LLVMObjectAsString(const T *O) { | 43 template <typename T> static std::string LLVMObjectAsString(const T *O) { |
| 44 std::string Dump; | 44 std::string Dump; |
| 45 raw_string_ostream Stream(Dump); | 45 raw_string_ostream Stream(Dump); |
| 46 O->print(Stream); | 46 O->print(Stream); |
| 47 return Stream.str(); | 47 return Stream.str(); |
| 48 } | 48 } |
| 49 | 49 |
| 50 // Base class for converting LLVM to ICE. | 50 // Base class for converting LLVM to ICE. |
| 51 // TODO(stichnot): Redesign Converter, LLVM2ICEConverter, |
| 52 // LLVM2ICEFunctionConverter, and LLVM2ICEGlobalsConverter with |
| 53 // respect to Translator. In particular, the unique_ptr ownership |
| 54 // rules in LLVM2ICEFunctionConverter. |
| 51 class LLVM2ICEConverter { | 55 class LLVM2ICEConverter { |
| 52 LLVM2ICEConverter(const LLVM2ICEConverter &) = delete; | 56 LLVM2ICEConverter(const LLVM2ICEConverter &) = delete; |
| 53 LLVM2ICEConverter &operator=(const LLVM2ICEConverter &) = delete; | 57 LLVM2ICEConverter &operator=(const LLVM2ICEConverter &) = delete; |
| 54 | 58 |
| 55 public: | 59 public: |
| 56 LLVM2ICEConverter(Ice::Converter &Converter) | 60 LLVM2ICEConverter(Ice::Converter &Converter) |
| 57 : Converter(Converter), Ctx(Converter.getContext()), | 61 : Converter(Converter), Ctx(Converter.getContext()), |
| 58 TypeConverter(Converter.getModule()->getContext()) {} | 62 TypeConverter(Converter.getModule()->getContext()) {} |
| 59 | 63 |
| 60 Ice::Converter &getConverter() const { return Converter; } | 64 Ice::Converter &getConverter() const { return Converter; } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 72 // valid PNaCl bitcode. Otherwise, the behavior is undefined. | 76 // valid PNaCl bitcode. Otherwise, the behavior is undefined. |
| 73 class LLVM2ICEFunctionConverter : LLVM2ICEConverter { | 77 class LLVM2ICEFunctionConverter : LLVM2ICEConverter { |
| 74 LLVM2ICEFunctionConverter(const LLVM2ICEFunctionConverter &) = delete; | 78 LLVM2ICEFunctionConverter(const LLVM2ICEFunctionConverter &) = delete; |
| 75 LLVM2ICEFunctionConverter & | 79 LLVM2ICEFunctionConverter & |
| 76 operator=(const LLVM2ICEFunctionConverter &) = delete; | 80 operator=(const LLVM2ICEFunctionConverter &) = delete; |
| 77 | 81 |
| 78 public: | 82 public: |
| 79 LLVM2ICEFunctionConverter(Ice::Converter &Converter) | 83 LLVM2ICEFunctionConverter(Ice::Converter &Converter) |
| 80 : LLVM2ICEConverter(Converter), Func(nullptr) {} | 84 : LLVM2ICEConverter(Converter), Func(nullptr) {} |
| 81 | 85 |
| 82 // Caller is expected to delete the returned Ice::Cfg object. | 86 void convertFunction(const Function *F) { |
| 83 Ice::Cfg *convertFunction(const Function *F) { | 87 Func = Ice::Cfg::create(Ctx); |
| 88 Ice::Cfg::setCurrentCfg(Func.get()); |
| 89 |
| 84 VarMap.clear(); | 90 VarMap.clear(); |
| 85 NodeMap.clear(); | 91 NodeMap.clear(); |
| 86 Func = Ice::Cfg::create(Ctx); | |
| 87 Func->setFunctionName(F->getName()); | 92 Func->setFunctionName(F->getName()); |
| 88 Func->setReturnType(convertToIceType(F->getReturnType())); | 93 Func->setReturnType(convertToIceType(F->getReturnType())); |
| 89 Func->setInternal(F->hasInternalLinkage()); | 94 Func->setInternal(F->hasInternalLinkage()); |
| 90 Ice::TimerMarker T(Ice::TimerStack::TT_llvmConvert, Func); | 95 Ice::TimerMarker T(Ice::TimerStack::TT_llvmConvert, Func.get()); |
| 91 | 96 |
| 92 // The initial definition/use of each arg is the entry node. | 97 // The initial definition/use of each arg is the entry node. |
| 93 for (auto ArgI = F->arg_begin(), ArgE = F->arg_end(); ArgI != ArgE; | 98 for (auto ArgI = F->arg_begin(), ArgE = F->arg_end(); ArgI != ArgE; |
| 94 ++ArgI) { | 99 ++ArgI) { |
| 95 Func->addArg(mapValueToIceVar(ArgI)); | 100 Func->addArg(mapValueToIceVar(ArgI)); |
| 96 } | 101 } |
| 97 | 102 |
| 98 // Make an initial pass through the block list just to resolve the | 103 // Make an initial pass through the block list just to resolve the |
| 99 // blocks in the original linearized order. Otherwise the ICE | 104 // blocks in the original linearized order. Otherwise the ICE |
| 100 // linearized order will be affected by branch targets in | 105 // linearized order will be affected by branch targets in |
| 101 // terminator instructions. | 106 // terminator instructions. |
| 102 for (const BasicBlock &BBI : *F) | 107 for (const BasicBlock &BBI : *F) |
| 103 mapBasicBlockToNode(&BBI); | 108 mapBasicBlockToNode(&BBI); |
| 104 for (const BasicBlock &BBI : *F) | 109 for (const BasicBlock &BBI : *F) |
| 105 convertBasicBlock(&BBI); | 110 convertBasicBlock(&BBI); |
| 106 Func->setEntryNode(mapBasicBlockToNode(&F->getEntryBlock())); | 111 Func->setEntryNode(mapBasicBlockToNode(&F->getEntryBlock())); |
| 107 Func->computePredecessors(); | 112 Func->computePredecessors(); |
| 108 | 113 |
| 109 return Func; | 114 Ice::Cfg::setCurrentCfg(nullptr); |
| 115 Converter.translateFcn(std::move(Func)); |
| 110 } | 116 } |
| 111 | 117 |
| 112 // convertConstant() does not use Func or require it to be a valid | 118 // convertConstant() does not use Func or require it to be a valid |
| 113 // Ice::Cfg pointer. As such, it's suitable for e.g. constructing | 119 // Ice::Cfg pointer. As such, it's suitable for e.g. constructing |
| 114 // global initializers. | 120 // global initializers. |
| 115 Ice::Constant *convertConstant(const Constant *Const) { | 121 Ice::Constant *convertConstant(const Constant *Const) { |
| 116 if (const auto GV = dyn_cast<GlobalValue>(Const)) { | 122 if (const auto GV = dyn_cast<GlobalValue>(Const)) { |
| 117 Ice::GlobalDeclaration *Decl = getConverter().getGlobalDeclaration(GV); | 123 Ice::GlobalDeclaration *Decl = getConverter().getGlobalDeclaration(GV); |
| 118 const Ice::RelocOffsetT Offset = 0; | 124 const Ice::RelocOffsetT Offset = 0; |
| 119 return Ctx->getConstantSym(Offset, Decl->getName(), | 125 return Ctx->getConstantSym(Offset, Decl->getName(), |
| (...skipping 20 matching lines...) Expand all Loading... |
| 140 private: | 146 private: |
| 141 // LLVM values (instructions, etc.) are mapped directly to ICE variables. | 147 // LLVM values (instructions, etc.) are mapped directly to ICE variables. |
| 142 // mapValueToIceVar has a version that forces an ICE type on the variable, | 148 // mapValueToIceVar has a version that forces an ICE type on the variable, |
| 143 // and a version that just uses convertToIceType on V. | 149 // and a version that just uses convertToIceType on V. |
| 144 Ice::Variable *mapValueToIceVar(const Value *V, Ice::Type IceTy) { | 150 Ice::Variable *mapValueToIceVar(const Value *V, Ice::Type IceTy) { |
| 145 if (IceTy == Ice::IceType_void) | 151 if (IceTy == Ice::IceType_void) |
| 146 return nullptr; | 152 return nullptr; |
| 147 if (VarMap.find(V) == VarMap.end()) { | 153 if (VarMap.find(V) == VarMap.end()) { |
| 148 VarMap[V] = Func->makeVariable(IceTy); | 154 VarMap[V] = Func->makeVariable(IceTy); |
| 149 if (ALLOW_DUMP) | 155 if (ALLOW_DUMP) |
| 150 VarMap[V]->setName(Func, V->getName()); | 156 VarMap[V]->setName(Func.get(), V->getName()); |
| 151 } | 157 } |
| 152 return VarMap[V]; | 158 return VarMap[V]; |
| 153 } | 159 } |
| 154 | 160 |
| 155 Ice::Variable *mapValueToIceVar(const Value *V) { | 161 Ice::Variable *mapValueToIceVar(const Value *V) { |
| 156 return mapValueToIceVar(V, convertToIceType(V->getType())); | 162 return mapValueToIceVar(V, convertToIceType(V->getType())); |
| 157 } | 163 } |
| 158 | 164 |
| 159 Ice::CfgNode *mapBasicBlockToNode(const BasicBlock *BB) { | 165 Ice::CfgNode *mapBasicBlockToNode(const BasicBlock *BB) { |
| 160 if (NodeMap.find(BB) == NodeMap.end()) { | 166 if (NodeMap.find(BB) == NodeMap.end()) { |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 LLVMObjectAsString(Inst)); | 303 LLVMObjectAsString(Inst)); |
| 298 } | 304 } |
| 299 | 305 |
| 300 llvm_unreachable("convertInstruction"); | 306 llvm_unreachable("convertInstruction"); |
| 301 return nullptr; | 307 return nullptr; |
| 302 } | 308 } |
| 303 | 309 |
| 304 Ice::Inst *convertLoadInstruction(const LoadInst *Inst) { | 310 Ice::Inst *convertLoadInstruction(const LoadInst *Inst) { |
| 305 Ice::Operand *Src = convertOperand(Inst, 0); | 311 Ice::Operand *Src = convertOperand(Inst, 0); |
| 306 Ice::Variable *Dest = mapValueToIceVar(Inst); | 312 Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 307 return Ice::InstLoad::create(Func, Dest, Src); | 313 return Ice::InstLoad::create(Func.get(), Dest, Src); |
| 308 } | 314 } |
| 309 | 315 |
| 310 Ice::Inst *convertStoreInstruction(const StoreInst *Inst) { | 316 Ice::Inst *convertStoreInstruction(const StoreInst *Inst) { |
| 311 Ice::Operand *Addr = convertOperand(Inst, 1); | 317 Ice::Operand *Addr = convertOperand(Inst, 1); |
| 312 Ice::Operand *Val = convertOperand(Inst, 0); | 318 Ice::Operand *Val = convertOperand(Inst, 0); |
| 313 return Ice::InstStore::create(Func, Val, Addr); | 319 return Ice::InstStore::create(Func.get(), Val, Addr); |
| 314 } | 320 } |
| 315 | 321 |
| 316 Ice::Inst *convertArithInstruction(const Instruction *Inst, | 322 Ice::Inst *convertArithInstruction(const Instruction *Inst, |
| 317 Ice::InstArithmetic::OpKind Opcode) { | 323 Ice::InstArithmetic::OpKind Opcode) { |
| 318 const auto BinOp = cast<BinaryOperator>(Inst); | 324 const auto BinOp = cast<BinaryOperator>(Inst); |
| 319 Ice::Operand *Src0 = convertOperand(Inst, 0); | 325 Ice::Operand *Src0 = convertOperand(Inst, 0); |
| 320 Ice::Operand *Src1 = convertOperand(Inst, 1); | 326 Ice::Operand *Src1 = convertOperand(Inst, 1); |
| 321 Ice::Variable *Dest = mapValueToIceVar(BinOp); | 327 Ice::Variable *Dest = mapValueToIceVar(BinOp); |
| 322 return Ice::InstArithmetic::create(Func, Opcode, Dest, Src0, Src1); | 328 return Ice::InstArithmetic::create(Func.get(), Opcode, Dest, Src0, Src1); |
| 323 } | 329 } |
| 324 | 330 |
| 325 Ice::Inst *convertPHINodeInstruction(const PHINode *Inst) { | 331 Ice::Inst *convertPHINodeInstruction(const PHINode *Inst) { |
| 326 unsigned NumValues = Inst->getNumIncomingValues(); | 332 unsigned NumValues = Inst->getNumIncomingValues(); |
| 327 Ice::InstPhi *IcePhi = | 333 Ice::InstPhi *IcePhi = |
| 328 Ice::InstPhi::create(Func, NumValues, mapValueToIceVar(Inst)); | 334 Ice::InstPhi::create(Func.get(), NumValues, mapValueToIceVar(Inst)); |
| 329 for (unsigned N = 0, E = NumValues; N != E; ++N) { | 335 for (unsigned N = 0, E = NumValues; N != E; ++N) { |
| 330 IcePhi->addArgument(convertOperand(Inst, N), | 336 IcePhi->addArgument(convertOperand(Inst, N), |
| 331 mapBasicBlockToNode(Inst->getIncomingBlock(N))); | 337 mapBasicBlockToNode(Inst->getIncomingBlock(N))); |
| 332 } | 338 } |
| 333 return IcePhi; | 339 return IcePhi; |
| 334 } | 340 } |
| 335 | 341 |
| 336 Ice::Inst *convertBrInstruction(const BranchInst *Inst) { | 342 Ice::Inst *convertBrInstruction(const BranchInst *Inst) { |
| 337 if (Inst->isConditional()) { | 343 if (Inst->isConditional()) { |
| 338 Ice::Operand *Src = convertOperand(Inst, 0); | 344 Ice::Operand *Src = convertOperand(Inst, 0); |
| 339 BasicBlock *BBThen = Inst->getSuccessor(0); | 345 BasicBlock *BBThen = Inst->getSuccessor(0); |
| 340 BasicBlock *BBElse = Inst->getSuccessor(1); | 346 BasicBlock *BBElse = Inst->getSuccessor(1); |
| 341 Ice::CfgNode *NodeThen = mapBasicBlockToNode(BBThen); | 347 Ice::CfgNode *NodeThen = mapBasicBlockToNode(BBThen); |
| 342 Ice::CfgNode *NodeElse = mapBasicBlockToNode(BBElse); | 348 Ice::CfgNode *NodeElse = mapBasicBlockToNode(BBElse); |
| 343 return Ice::InstBr::create(Func, Src, NodeThen, NodeElse); | 349 return Ice::InstBr::create(Func.get(), Src, NodeThen, NodeElse); |
| 344 } else { | 350 } else { |
| 345 BasicBlock *BBSucc = Inst->getSuccessor(0); | 351 BasicBlock *BBSucc = Inst->getSuccessor(0); |
| 346 return Ice::InstBr::create(Func, mapBasicBlockToNode(BBSucc)); | 352 return Ice::InstBr::create(Func.get(), mapBasicBlockToNode(BBSucc)); |
| 347 } | 353 } |
| 348 } | 354 } |
| 349 | 355 |
| 350 Ice::Inst *convertIntToPtrInstruction(const IntToPtrInst *Inst) { | 356 Ice::Inst *convertIntToPtrInstruction(const IntToPtrInst *Inst) { |
| 351 Ice::Operand *Src = convertOperand(Inst, 0); | 357 Ice::Operand *Src = convertOperand(Inst, 0); |
| 352 Ice::Variable *Dest = mapValueToIceVar(Inst, Ice::getPointerType()); | 358 Ice::Variable *Dest = mapValueToIceVar(Inst, Ice::getPointerType()); |
| 353 return Ice::InstAssign::create(Func, Dest, Src); | 359 return Ice::InstAssign::create(Func.get(), Dest, Src); |
| 354 } | 360 } |
| 355 | 361 |
| 356 Ice::Inst *convertPtrToIntInstruction(const PtrToIntInst *Inst) { | 362 Ice::Inst *convertPtrToIntInstruction(const PtrToIntInst *Inst) { |
| 357 Ice::Operand *Src = convertOperand(Inst, 0); | 363 Ice::Operand *Src = convertOperand(Inst, 0); |
| 358 Ice::Variable *Dest = mapValueToIceVar(Inst); | 364 Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 359 return Ice::InstAssign::create(Func, Dest, Src); | 365 return Ice::InstAssign::create(Func.get(), Dest, Src); |
| 360 } | 366 } |
| 361 | 367 |
| 362 Ice::Inst *convertRetInstruction(const ReturnInst *Inst) { | 368 Ice::Inst *convertRetInstruction(const ReturnInst *Inst) { |
| 363 Ice::Operand *RetOperand = convertOperand(Inst, 0); | 369 Ice::Operand *RetOperand = convertOperand(Inst, 0); |
| 364 if (RetOperand) { | 370 if (RetOperand) { |
| 365 return Ice::InstRet::create(Func, RetOperand); | 371 return Ice::InstRet::create(Func.get(), RetOperand); |
| 366 } else { | 372 } else { |
| 367 return Ice::InstRet::create(Func); | 373 return Ice::InstRet::create(Func.get()); |
| 368 } | 374 } |
| 369 } | 375 } |
| 370 | 376 |
| 371 Ice::Inst *convertCastInstruction(const Instruction *Inst, | 377 Ice::Inst *convertCastInstruction(const Instruction *Inst, |
| 372 Ice::InstCast::OpKind CastKind) { | 378 Ice::InstCast::OpKind CastKind) { |
| 373 Ice::Operand *Src = convertOperand(Inst, 0); | 379 Ice::Operand *Src = convertOperand(Inst, 0); |
| 374 Ice::Variable *Dest = mapValueToIceVar(Inst); | 380 Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 375 return Ice::InstCast::create(Func, CastKind, Dest, Src); | 381 return Ice::InstCast::create(Func.get(), CastKind, Dest, Src); |
| 376 } | 382 } |
| 377 | 383 |
| 378 Ice::Inst *convertICmpInstruction(const ICmpInst *Inst) { | 384 Ice::Inst *convertICmpInstruction(const ICmpInst *Inst) { |
| 379 Ice::Operand *Src0 = convertOperand(Inst, 0); | 385 Ice::Operand *Src0 = convertOperand(Inst, 0); |
| 380 Ice::Operand *Src1 = convertOperand(Inst, 1); | 386 Ice::Operand *Src1 = convertOperand(Inst, 1); |
| 381 Ice::Variable *Dest = mapValueToIceVar(Inst); | 387 Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 382 | 388 |
| 383 Ice::InstIcmp::ICond Cond; | 389 Ice::InstIcmp::ICond Cond; |
| 384 switch (Inst->getPredicate()) { | 390 switch (Inst->getPredicate()) { |
| 385 default: | 391 default: |
| (...skipping 23 matching lines...) Expand all Loading... |
| 409 Cond = Ice::InstIcmp::Sge; | 415 Cond = Ice::InstIcmp::Sge; |
| 410 break; | 416 break; |
| 411 case CmpInst::ICMP_SLT: | 417 case CmpInst::ICMP_SLT: |
| 412 Cond = Ice::InstIcmp::Slt; | 418 Cond = Ice::InstIcmp::Slt; |
| 413 break; | 419 break; |
| 414 case CmpInst::ICMP_SLE: | 420 case CmpInst::ICMP_SLE: |
| 415 Cond = Ice::InstIcmp::Sle; | 421 Cond = Ice::InstIcmp::Sle; |
| 416 break; | 422 break; |
| 417 } | 423 } |
| 418 | 424 |
| 419 return Ice::InstIcmp::create(Func, Cond, Dest, Src0, Src1); | 425 return Ice::InstIcmp::create(Func.get(), Cond, Dest, Src0, Src1); |
| 420 } | 426 } |
| 421 | 427 |
| 422 Ice::Inst *convertFCmpInstruction(const FCmpInst *Inst) { | 428 Ice::Inst *convertFCmpInstruction(const FCmpInst *Inst) { |
| 423 Ice::Operand *Src0 = convertOperand(Inst, 0); | 429 Ice::Operand *Src0 = convertOperand(Inst, 0); |
| 424 Ice::Operand *Src1 = convertOperand(Inst, 1); | 430 Ice::Operand *Src1 = convertOperand(Inst, 1); |
| 425 Ice::Variable *Dest = mapValueToIceVar(Inst); | 431 Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 426 | 432 |
| 427 Ice::InstFcmp::FCond Cond; | 433 Ice::InstFcmp::FCond Cond; |
| 428 switch (Inst->getPredicate()) { | 434 switch (Inst->getPredicate()) { |
| 429 | 435 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 Cond = Ice::InstFcmp::Une; | 479 Cond = Ice::InstFcmp::Une; |
| 474 break; | 480 break; |
| 475 case CmpInst::FCMP_UNO: | 481 case CmpInst::FCMP_UNO: |
| 476 Cond = Ice::InstFcmp::Uno; | 482 Cond = Ice::InstFcmp::Uno; |
| 477 break; | 483 break; |
| 478 case CmpInst::FCMP_TRUE: | 484 case CmpInst::FCMP_TRUE: |
| 479 Cond = Ice::InstFcmp::True; | 485 Cond = Ice::InstFcmp::True; |
| 480 break; | 486 break; |
| 481 } | 487 } |
| 482 | 488 |
| 483 return Ice::InstFcmp::create(Func, Cond, Dest, Src0, Src1); | 489 return Ice::InstFcmp::create(Func.get(), Cond, Dest, Src0, Src1); |
| 484 } | 490 } |
| 485 | 491 |
| 486 Ice::Inst *convertExtractElementInstruction(const ExtractElementInst *Inst) { | 492 Ice::Inst *convertExtractElementInstruction(const ExtractElementInst *Inst) { |
| 487 Ice::Variable *Dest = mapValueToIceVar(Inst); | 493 Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 488 Ice::Operand *Source1 = convertValue(Inst->getOperand(0)); | 494 Ice::Operand *Source1 = convertValue(Inst->getOperand(0)); |
| 489 Ice::Operand *Source2 = convertValue(Inst->getOperand(1)); | 495 Ice::Operand *Source2 = convertValue(Inst->getOperand(1)); |
| 490 return Ice::InstExtractElement::create(Func, Dest, Source1, Source2); | 496 return Ice::InstExtractElement::create(Func.get(), Dest, Source1, Source2); |
| 491 } | 497 } |
| 492 | 498 |
| 493 Ice::Inst *convertInsertElementInstruction(const InsertElementInst *Inst) { | 499 Ice::Inst *convertInsertElementInstruction(const InsertElementInst *Inst) { |
| 494 Ice::Variable *Dest = mapValueToIceVar(Inst); | 500 Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 495 Ice::Operand *Source1 = convertValue(Inst->getOperand(0)); | 501 Ice::Operand *Source1 = convertValue(Inst->getOperand(0)); |
| 496 Ice::Operand *Source2 = convertValue(Inst->getOperand(1)); | 502 Ice::Operand *Source2 = convertValue(Inst->getOperand(1)); |
| 497 Ice::Operand *Source3 = convertValue(Inst->getOperand(2)); | 503 Ice::Operand *Source3 = convertValue(Inst->getOperand(2)); |
| 498 return Ice::InstInsertElement::create(Func, Dest, Source1, Source2, | 504 return Ice::InstInsertElement::create(Func.get(), Dest, Source1, Source2, |
| 499 Source3); | 505 Source3); |
| 500 } | 506 } |
| 501 | 507 |
| 502 Ice::Inst *convertSelectInstruction(const SelectInst *Inst) { | 508 Ice::Inst *convertSelectInstruction(const SelectInst *Inst) { |
| 503 Ice::Variable *Dest = mapValueToIceVar(Inst); | 509 Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 504 Ice::Operand *Cond = convertValue(Inst->getCondition()); | 510 Ice::Operand *Cond = convertValue(Inst->getCondition()); |
| 505 Ice::Operand *Source1 = convertValue(Inst->getTrueValue()); | 511 Ice::Operand *Source1 = convertValue(Inst->getTrueValue()); |
| 506 Ice::Operand *Source2 = convertValue(Inst->getFalseValue()); | 512 Ice::Operand *Source2 = convertValue(Inst->getFalseValue()); |
| 507 return Ice::InstSelect::create(Func, Dest, Cond, Source1, Source2); | 513 return Ice::InstSelect::create(Func.get(), Dest, Cond, Source1, Source2); |
| 508 } | 514 } |
| 509 | 515 |
| 510 Ice::Inst *convertSwitchInstruction(const SwitchInst *Inst) { | 516 Ice::Inst *convertSwitchInstruction(const SwitchInst *Inst) { |
| 511 Ice::Operand *Source = convertValue(Inst->getCondition()); | 517 Ice::Operand *Source = convertValue(Inst->getCondition()); |
| 512 Ice::CfgNode *LabelDefault = mapBasicBlockToNode(Inst->getDefaultDest()); | 518 Ice::CfgNode *LabelDefault = mapBasicBlockToNode(Inst->getDefaultDest()); |
| 513 unsigned NumCases = Inst->getNumCases(); | 519 unsigned NumCases = Inst->getNumCases(); |
| 514 Ice::InstSwitch *Switch = | 520 Ice::InstSwitch *Switch = |
| 515 Ice::InstSwitch::create(Func, NumCases, Source, LabelDefault); | 521 Ice::InstSwitch::create(Func.get(), NumCases, Source, LabelDefault); |
| 516 unsigned CurrentCase = 0; | 522 unsigned CurrentCase = 0; |
| 517 for (SwitchInst::ConstCaseIt I = Inst->case_begin(), E = Inst->case_end(); | 523 for (SwitchInst::ConstCaseIt I = Inst->case_begin(), E = Inst->case_end(); |
| 518 I != E; ++I, ++CurrentCase) { | 524 I != E; ++I, ++CurrentCase) { |
| 519 uint64_t CaseValue = I.getCaseValue()->getSExtValue(); | 525 uint64_t CaseValue = I.getCaseValue()->getSExtValue(); |
| 520 Ice::CfgNode *CaseSuccessor = mapBasicBlockToNode(I.getCaseSuccessor()); | 526 Ice::CfgNode *CaseSuccessor = mapBasicBlockToNode(I.getCaseSuccessor()); |
| 521 Switch->addBranch(CurrentCase, CaseValue, CaseSuccessor); | 527 Switch->addBranch(CurrentCase, CaseValue, CaseSuccessor); |
| 522 } | 528 } |
| 523 return Switch; | 529 return Switch; |
| 524 } | 530 } |
| 525 | 531 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 537 static const char LLVMPrefix[] = "llvm."; | 543 static const char LLVMPrefix[] = "llvm."; |
| 538 const size_t LLVMPrefixLen = strlen(LLVMPrefix); | 544 const size_t LLVMPrefixLen = strlen(LLVMPrefix); |
| 539 Ice::IceString Name = Target->getName(); | 545 Ice::IceString Name = Target->getName(); |
| 540 if (Name.substr(0, LLVMPrefixLen) == LLVMPrefix) { | 546 if (Name.substr(0, LLVMPrefixLen) == LLVMPrefix) { |
| 541 Ice::IceString NameSuffix = Name.substr(LLVMPrefixLen); | 547 Ice::IceString NameSuffix = Name.substr(LLVMPrefixLen); |
| 542 Info = Ctx->getIntrinsicsInfo().find(NameSuffix); | 548 Info = Ctx->getIntrinsicsInfo().find(NameSuffix); |
| 543 if (!Info) { | 549 if (!Info) { |
| 544 report_fatal_error(std::string("Invalid PNaCl intrinsic call: ") + | 550 report_fatal_error(std::string("Invalid PNaCl intrinsic call: ") + |
| 545 LLVMObjectAsString(Inst)); | 551 LLVMObjectAsString(Inst)); |
| 546 } | 552 } |
| 547 NewInst = Ice::InstIntrinsicCall::create(Func, NumArgs, Dest, | 553 NewInst = Ice::InstIntrinsicCall::create(Func.get(), NumArgs, Dest, |
| 548 CallTarget, Info->Info); | 554 CallTarget, Info->Info); |
| 549 } | 555 } |
| 550 } | 556 } |
| 551 | 557 |
| 552 // Not an intrinsic call. | 558 // Not an intrinsic call. |
| 553 if (NewInst == nullptr) { | 559 if (NewInst == nullptr) { |
| 554 NewInst = Ice::InstCall::create(Func, NumArgs, Dest, CallTarget, | 560 NewInst = Ice::InstCall::create(Func.get(), NumArgs, Dest, CallTarget, |
| 555 Inst->isTailCall()); | 561 Inst->isTailCall()); |
| 556 } | 562 } |
| 557 for (unsigned i = 0; i < NumArgs; ++i) { | 563 for (unsigned i = 0; i < NumArgs; ++i) { |
| 558 NewInst->addArg(convertOperand(Inst, i)); | 564 NewInst->addArg(convertOperand(Inst, i)); |
| 559 } | 565 } |
| 560 if (Info) { | 566 if (Info) { |
| 561 validateIntrinsicCall(NewInst, Info); | 567 validateIntrinsicCall(NewInst, Info); |
| 562 } | 568 } |
| 563 return NewInst; | 569 return NewInst; |
| 564 } | 570 } |
| 565 | 571 |
| 566 Ice::Inst *convertAllocaInstruction(const AllocaInst *Inst) { | 572 Ice::Inst *convertAllocaInstruction(const AllocaInst *Inst) { |
| 567 // PNaCl bitcode only contains allocas of byte-granular objects. | 573 // PNaCl bitcode only contains allocas of byte-granular objects. |
| 568 Ice::Operand *ByteCount = convertValue(Inst->getArraySize()); | 574 Ice::Operand *ByteCount = convertValue(Inst->getArraySize()); |
| 569 uint32_t Align = Inst->getAlignment(); | 575 uint32_t Align = Inst->getAlignment(); |
| 570 Ice::Variable *Dest = mapValueToIceVar(Inst, Ice::getPointerType()); | 576 Ice::Variable *Dest = mapValueToIceVar(Inst, Ice::getPointerType()); |
| 571 | 577 |
| 572 return Ice::InstAlloca::create(Func, ByteCount, Align, Dest); | 578 return Ice::InstAlloca::create(Func.get(), ByteCount, Align, Dest); |
| 573 } | 579 } |
| 574 | 580 |
| 575 Ice::Inst *convertUnreachableInstruction(const UnreachableInst * /*Inst*/) { | 581 Ice::Inst *convertUnreachableInstruction(const UnreachableInst * /*Inst*/) { |
| 576 return Ice::InstUnreachable::create(Func); | 582 return Ice::InstUnreachable::create(Func.get()); |
| 577 } | 583 } |
| 578 | 584 |
| 579 Ice::CfgNode *convertBasicBlock(const BasicBlock *BB) { | 585 Ice::CfgNode *convertBasicBlock(const BasicBlock *BB) { |
| 580 Ice::CfgNode *Node = mapBasicBlockToNode(BB); | 586 Ice::CfgNode *Node = mapBasicBlockToNode(BB); |
| 581 for (const Instruction &II : *BB) { | 587 for (const Instruction &II : *BB) { |
| 582 Ice::Inst *Inst = convertInstruction(&II); | 588 Ice::Inst *Inst = convertInstruction(&II); |
| 583 Node->appendInst(Inst); | 589 Node->appendInst(Inst); |
| 584 } | 590 } |
| 585 return Node; | 591 return Node; |
| 586 } | 592 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 614 << I->getArgType(ArgIndex) | 620 << I->getArgType(ArgIndex) |
| 615 << ". Found: " << Call->getArg(ArgIndex)->getType(); | 621 << ". Found: " << Call->getArg(ArgIndex)->getType(); |
| 616 report_fatal_error(StrBuf.str()); | 622 report_fatal_error(StrBuf.str()); |
| 617 break; | 623 break; |
| 618 } | 624 } |
| 619 } | 625 } |
| 620 } | 626 } |
| 621 | 627 |
| 622 private: | 628 private: |
| 623 // Data | 629 // Data |
| 624 Ice::Cfg *Func; | 630 std::unique_ptr<Ice::Cfg> Func; |
| 625 std::map<const Value *, Ice::Variable *> VarMap; | 631 std::map<const Value *, Ice::Variable *> VarMap; |
| 626 std::map<const BasicBlock *, Ice::CfgNode *> NodeMap; | 632 std::map<const BasicBlock *, Ice::CfgNode *> NodeMap; |
| 627 }; | 633 }; |
| 628 | 634 |
| 629 // Converter from LLVM global variables to ICE. The entry point is the | 635 // Converter from LLVM global variables to ICE. The entry point is the |
| 630 // convertGlobalsToIce method. | 636 // convertGlobalsToIce method. |
| 631 // | 637 // |
| 632 // Note: this currently assumes that the given IR was verified to be | 638 // Note: this currently assumes that the given IR was verified to be |
| 633 // valid PNaCl bitcode. Othewise, the behavior is undefined. | 639 // valid PNaCl bitcode. Othewise, the behavior is undefined. |
| 634 class LLVM2ICEGlobalsConverter : public LLVM2ICEConverter { | 640 class LLVM2ICEGlobalsConverter : public LLVM2ICEConverter { |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 865 } | 871 } |
| 866 | 872 |
| 867 void Converter::convertGlobals(Module *Mod) { | 873 void Converter::convertGlobals(Module *Mod) { |
| 868 LLVM2ICEGlobalsConverter GlobalsConverter(*this); | 874 LLVM2ICEGlobalsConverter GlobalsConverter(*this); |
| 869 VariableDeclarationList VariableDeclarations; | 875 VariableDeclarationList VariableDeclarations; |
| 870 GlobalsConverter.convertGlobalsToIce(Mod, VariableDeclarations); | 876 GlobalsConverter.convertGlobalsToIce(Mod, VariableDeclarations); |
| 871 lowerGlobals(VariableDeclarations); | 877 lowerGlobals(VariableDeclarations); |
| 872 } | 878 } |
| 873 | 879 |
| 874 void Converter::convertFunctions() { | 880 void Converter::convertFunctions() { |
| 875 TimerStackIdT StackID = GlobalContext::TSK_Funcs; | 881 const TimerStackIdT StackID = GlobalContext::TSK_Funcs; |
| 876 for (const Function &I : *Mod) { | 882 for (const Function &I : *Mod) { |
| 877 if (I.empty()) | 883 if (I.empty()) |
| 878 continue; | 884 continue; |
| 879 | 885 |
| 880 TimerIdT TimerID = 0; | 886 TimerIdT TimerID = 0; |
| 881 if (ALLOW_DUMP && Ctx->getFlags().TimeEachFunction) { | 887 const bool TimeThisFunction = |
| 888 ALLOW_DUMP && Ctx->getFlags().TimeEachFunction; |
| 889 if (TimeThisFunction) { |
| 882 TimerID = Ctx->getTimerID(StackID, I.getName()); | 890 TimerID = Ctx->getTimerID(StackID, I.getName()); |
| 883 Ctx->pushTimer(TimerID, StackID); | 891 Ctx->pushTimer(TimerID, StackID); |
| 884 } | 892 } |
| 885 LLVM2ICEFunctionConverter FunctionConverter(*this); | 893 LLVM2ICEFunctionConverter FunctionConverter(*this); |
| 886 | 894 FunctionConverter.convertFunction(&I); |
| 887 Cfg *Fcn = FunctionConverter.convertFunction(&I); | 895 if (TimeThisFunction) |
| 888 translateFcn(Fcn); | |
| 889 if (ALLOW_DUMP && Ctx->getFlags().TimeEachFunction) | |
| 890 Ctx->popTimer(TimerID, StackID); | 896 Ctx->popTimer(TimerID, StackID); |
| 891 } | 897 } |
| 892 } | 898 } |
| 893 | 899 |
| 894 } // end of namespace Ice | 900 } // end of namespace Ice |
| OLD | NEW |