| 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. |
| 11 // | 11 // |
| 12 //===----------------------------------------------------------------------===// | 12 //===----------------------------------------------------------------------===// |
| 13 | 13 |
| 14 #include <iostream> | 14 #include <iostream> |
| 15 | 15 |
| 16 #include "llvm/IR/Constant.h" | 16 #include "llvm/IR/Constant.h" |
| 17 #include "llvm/IR/Constants.h" | 17 #include "llvm/IR/Constants.h" |
| 18 #include "llvm/IR/DataLayout.h" | 18 #include "llvm/IR/DataLayout.h" |
| 19 #include "llvm/IR/Instruction.h" | 19 #include "llvm/IR/Instruction.h" |
| 20 #include "llvm/IR/Instructions.h" | 20 #include "llvm/IR/Instructions.h" |
| 21 #include "llvm/IR/LLVMContext.h" | 21 #include "llvm/IR/LLVMContext.h" |
| 22 #include "llvm/IR/Module.h" | 22 #include "llvm/IR/Module.h" |
| 23 | 23 |
| 24 #include "IceCfg.h" | 24 #include "IceCfg.h" |
| 25 #include "IceCfgNode.h" | 25 #include "IceCfgNode.h" |
| 26 #include "IceClFlags.h" | 26 #include "IceClFlags.h" |
| 27 #include "IceConverter.h" | 27 #include "IceConverter.h" |
| 28 #include "IceDefs.h" | 28 #include "IceDefs.h" |
| 29 #include "IceGlobalContext.h" | 29 #include "IceGlobalContext.h" |
| 30 #include "IceGlobalInits.h" |
| 30 #include "IceInst.h" | 31 #include "IceInst.h" |
| 31 #include "IceOperand.h" | 32 #include "IceOperand.h" |
| 32 #include "IceTargetLowering.h" | 33 #include "IceTargetLowering.h" |
| 33 #include "IceTypes.h" | 34 #include "IceTypes.h" |
| 34 #include "IceTypeConverter.h" | 35 #include "IceTypeConverter.h" |
| 35 | 36 |
| 36 using namespace llvm; | 37 using namespace llvm; |
| 37 | 38 |
| 38 namespace { | 39 namespace { |
| 39 | 40 |
| 40 // Debugging helper | 41 // Debugging helper |
| 41 template <typename T> static std::string LLVMObjectAsString(const T *O) { | 42 template <typename T> static std::string LLVMObjectAsString(const T *O) { |
| 42 std::string Dump; | 43 std::string Dump; |
| 43 raw_string_ostream Stream(Dump); | 44 raw_string_ostream Stream(Dump); |
| 44 O->print(Stream); | 45 O->print(Stream); |
| 45 return Stream.str(); | 46 return Stream.str(); |
| 46 } | 47 } |
| 47 | 48 |
| 48 // Converter from LLVM to ICE. The entry point is the convertFunction method. | 49 // Base class for converting LLVM to ICE. |
| 49 // | |
| 50 // Note: this currently assumes that the given IR was verified to be valid PNaCl | |
| 51 // bitcode: | |
| 52 // https://developers.google.com/native-client/dev/reference/pnacl-bitcode-abi | |
| 53 // If not, all kinds of assertions may fire. | |
| 54 // | |
| 55 class LLVM2ICEConverter { | 50 class LLVM2ICEConverter { |
| 51 LLVM2ICEConverter(const LLVM2ICEConverter &) = delete; |
| 52 LLVM2ICEConverter &operator=(const LLVM2ICEConverter &) = delete; |
| 53 |
| 56 public: | 54 public: |
| 57 LLVM2ICEConverter(Ice::GlobalContext *Ctx, LLVMContext &LLVMContext) | 55 LLVM2ICEConverter(Ice::GlobalContext *Ctx, LLVMContext &LLVMContext) |
| 58 : Ctx(Ctx), Func(NULL), TypeConverter(LLVMContext) {} | 56 : Ctx(Ctx), TypeConverter(LLVMContext) {} |
| 57 |
| 58 protected: |
| 59 // Data |
| 60 Ice::GlobalContext *Ctx; |
| 61 const Ice::TypeConverter TypeConverter; |
| 62 }; |
| 63 |
| 64 // Converter from LLVM functions to ICE. The entry point is the |
| 65 // convertFunction method. |
| 66 // |
| 67 // Note: this currently assumes that the given IR was verified to be |
| 68 // valid PNaCl bitcode. Otherwise, the behavior is undefined. |
| 69 class LLVM2ICEFunctionConverter : LLVM2ICEConverter { |
| 70 LLVM2ICEFunctionConverter(const LLVM2ICEFunctionConverter &) = delete; |
| 71 LLVM2ICEFunctionConverter & |
| 72 operator=(const LLVM2ICEFunctionConverter &) = delete; |
| 73 |
| 74 public: |
| 75 LLVM2ICEFunctionConverter(Ice::GlobalContext *Ctx, LLVMContext &LLVMContext) |
| 76 : LLVM2ICEConverter(Ctx, LLVMContext), Func(NULL) {} |
| 59 | 77 |
| 60 // Caller is expected to delete the returned Ice::Cfg object. | 78 // Caller is expected to delete the returned Ice::Cfg object. |
| 61 Ice::Cfg *convertFunction(const Function *F) { | 79 Ice::Cfg *convertFunction(const Function *F) { |
| 62 static Ice::TimerIdT IDllvmConvert = | 80 static Ice::TimerIdT IDllvmConvert = |
| 63 Ice::GlobalContext::getTimerID("llvmConvert"); | 81 Ice::GlobalContext::getTimerID("llvmConvert"); |
| 64 Ice::TimerMarker T(IDllvmConvert, Ctx); | 82 Ice::TimerMarker T(IDllvmConvert, Ctx); |
| 65 VarMap.clear(); | 83 VarMap.clear(); |
| 66 NodeMap.clear(); | 84 NodeMap.clear(); |
| 67 Func = new Ice::Cfg(Ctx); | 85 Func = new Ice::Cfg(Ctx); |
| 68 Func->setFunctionName(F->getName()); | 86 Func->setFunctionName(F->getName()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 86 Func->setEntryNode(mapBasicBlockToNode(&F->getEntryBlock())); | 104 Func->setEntryNode(mapBasicBlockToNode(&F->getEntryBlock())); |
| 87 Func->computePredecessors(); | 105 Func->computePredecessors(); |
| 88 | 106 |
| 89 return Func; | 107 return Func; |
| 90 } | 108 } |
| 91 | 109 |
| 92 // convertConstant() does not use Func or require it to be a valid | 110 // convertConstant() does not use Func or require it to be a valid |
| 93 // Ice::Cfg pointer. As such, it's suitable for e.g. constructing | 111 // Ice::Cfg pointer. As such, it's suitable for e.g. constructing |
| 94 // global initializers. | 112 // global initializers. |
| 95 Ice::Constant *convertConstant(const Constant *Const) { | 113 Ice::Constant *convertConstant(const Constant *Const) { |
| 96 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Const)) { | 114 if (const auto GV = dyn_cast<GlobalValue>(Const)) { |
| 97 return Ctx->getConstantSym(convertToIceType(GV->getType()), 0, | 115 return Ctx->getConstantSym(convertToIceType(GV->getType()), 0, |
| 98 GV->getName()); | 116 GV->getName()); |
| 99 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(Const)) { | 117 } else if (const auto CI = dyn_cast<ConstantInt>(Const)) { |
| 100 Ice::Type Ty = convertToIceType(CI->getType()); | 118 Ice::Type Ty = convertToIceType(CI->getType()); |
| 101 if (Ty == Ice::IceType_i64) { | 119 if (Ty == Ice::IceType_i64) { |
| 102 return Ctx->getConstantInt64(Ty, CI->getSExtValue()); | 120 return Ctx->getConstantInt64(Ty, CI->getSExtValue()); |
| 103 } else { | 121 } else { |
| 104 return Ctx->getConstantInt32(Ty, CI->getSExtValue()); | 122 return Ctx->getConstantInt32(Ty, CI->getSExtValue()); |
| 105 } | 123 } |
| 106 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Const)) { | 124 } else if (const auto CFP = dyn_cast<ConstantFP>(Const)) { |
| 107 Ice::Type Type = convertToIceType(CFP->getType()); | 125 Ice::Type Type = convertToIceType(CFP->getType()); |
| 108 if (Type == Ice::IceType_f32) | 126 if (Type == Ice::IceType_f32) |
| 109 return Ctx->getConstantFloat(CFP->getValueAPF().convertToFloat()); | 127 return Ctx->getConstantFloat(CFP->getValueAPF().convertToFloat()); |
| 110 else if (Type == Ice::IceType_f64) | 128 else if (Type == Ice::IceType_f64) |
| 111 return Ctx->getConstantDouble(CFP->getValueAPF().convertToDouble()); | 129 return Ctx->getConstantDouble(CFP->getValueAPF().convertToDouble()); |
| 112 llvm_unreachable("Unexpected floating point type"); | 130 llvm_unreachable("Unexpected floating point type"); |
| 113 return NULL; | 131 return NULL; |
| 114 } else if (const UndefValue *CU = dyn_cast<UndefValue>(Const)) { | 132 } else if (const auto CU = dyn_cast<UndefValue>(Const)) { |
| 115 return Ctx->getConstantUndef(convertToIceType(CU->getType())); | 133 return Ctx->getConstantUndef(convertToIceType(CU->getType())); |
| 116 } else { | 134 } else { |
| 117 llvm_unreachable("Unhandled constant type"); | 135 llvm_unreachable("Unhandled constant type"); |
| 118 return NULL; | 136 return NULL; |
| 119 } | 137 } |
| 120 } | 138 } |
| 121 | 139 |
| 122 private: | 140 private: |
| 123 // LLVM values (instructions, etc.) are mapped directly to ICE variables. | 141 // LLVM values (instructions, etc.) are mapped directly to ICE variables. |
| 124 // mapValueToIceVar has a version that forces an ICE type on the variable, | 142 // mapValueToIceVar has a version that forces an ICE type on the variable, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 139 Ice::CfgNode *mapBasicBlockToNode(const BasicBlock *BB) { | 157 Ice::CfgNode *mapBasicBlockToNode(const BasicBlock *BB) { |
| 140 if (NodeMap.find(BB) == NodeMap.end()) { | 158 if (NodeMap.find(BB) == NodeMap.end()) { |
| 141 NodeMap[BB] = Func->makeNode(BB->getName()); | 159 NodeMap[BB] = Func->makeNode(BB->getName()); |
| 142 } | 160 } |
| 143 return NodeMap[BB]; | 161 return NodeMap[BB]; |
| 144 } | 162 } |
| 145 | 163 |
| 146 Ice::Type convertToIceType(Type *LLVMTy) const { | 164 Ice::Type convertToIceType(Type *LLVMTy) const { |
| 147 Ice::Type IceTy = TypeConverter.convertToIceType(LLVMTy); | 165 Ice::Type IceTy = TypeConverter.convertToIceType(LLVMTy); |
| 148 if (IceTy == Ice::IceType_NUM) | 166 if (IceTy == Ice::IceType_NUM) |
| 149 llvm::report_fatal_error(std::string("Invalid PNaCl type ") + | 167 report_fatal_error(std::string("Invalid PNaCl type ") + |
| 150 LLVMObjectAsString(LLVMTy)); | 168 LLVMObjectAsString(LLVMTy)); |
| 151 return IceTy; | 169 return IceTy; |
| 152 } | 170 } |
| 153 | 171 |
| 154 // Given an LLVM instruction and an operand number, produce the | 172 // Given an LLVM instruction and an operand number, produce the |
| 155 // Ice::Operand this refers to. If there's no such operand, return | 173 // Ice::Operand this refers to. If there's no such operand, return |
| 156 // NULL. | 174 // NULL. |
| 157 Ice::Operand *convertOperand(const Instruction *Inst, unsigned OpNum) { | 175 Ice::Operand *convertOperand(const Instruction *Inst, unsigned OpNum) { |
| 158 if (OpNum >= Inst->getNumOperands()) { | 176 if (OpNum >= Inst->getNumOperands()) { |
| 159 return NULL; | 177 return NULL; |
| 160 } | 178 } |
| 161 const Value *Op = Inst->getOperand(OpNum); | 179 const Value *Op = Inst->getOperand(OpNum); |
| 162 return convertValue(Op); | 180 return convertValue(Op); |
| 163 } | 181 } |
| 164 | 182 |
| 165 Ice::Operand *convertValue(const Value *Op) { | 183 Ice::Operand *convertValue(const Value *Op) { |
| 166 if (const Constant *Const = dyn_cast<Constant>(Op)) { | 184 if (const auto Const = dyn_cast<Constant>(Op)) { |
| 167 return convertConstant(Const); | 185 return convertConstant(Const); |
| 168 } else { | 186 } else { |
| 169 return mapValueToIceVar(Op); | 187 return mapValueToIceVar(Op); |
| 170 } | 188 } |
| 171 } | 189 } |
| 172 | 190 |
| 173 // Note: this currently assumes a 1x1 mapping between LLVM IR and Ice | 191 // Note: this currently assumes a 1x1 mapping between LLVM IR and Ice |
| 174 // instructions. | 192 // instructions. |
| 175 Ice::Inst *convertInstruction(const Instruction *Inst) { | 193 Ice::Inst *convertInstruction(const Instruction *Inst) { |
| 176 switch (Inst->getOpcode()) { | 194 switch (Inst->getOpcode()) { |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 } | 304 } |
| 287 | 305 |
| 288 Ice::Inst *convertStoreInstruction(const StoreInst *Inst) { | 306 Ice::Inst *convertStoreInstruction(const StoreInst *Inst) { |
| 289 Ice::Operand *Addr = convertOperand(Inst, 1); | 307 Ice::Operand *Addr = convertOperand(Inst, 1); |
| 290 Ice::Operand *Val = convertOperand(Inst, 0); | 308 Ice::Operand *Val = convertOperand(Inst, 0); |
| 291 return Ice::InstStore::create(Func, Val, Addr); | 309 return Ice::InstStore::create(Func, Val, Addr); |
| 292 } | 310 } |
| 293 | 311 |
| 294 Ice::Inst *convertArithInstruction(const Instruction *Inst, | 312 Ice::Inst *convertArithInstruction(const Instruction *Inst, |
| 295 Ice::InstArithmetic::OpKind Opcode) { | 313 Ice::InstArithmetic::OpKind Opcode) { |
| 296 const BinaryOperator *BinOp = cast<BinaryOperator>(Inst); | 314 const auto BinOp = cast<BinaryOperator>(Inst); |
| 297 Ice::Operand *Src0 = convertOperand(Inst, 0); | 315 Ice::Operand *Src0 = convertOperand(Inst, 0); |
| 298 Ice::Operand *Src1 = convertOperand(Inst, 1); | 316 Ice::Operand *Src1 = convertOperand(Inst, 1); |
| 299 Ice::Variable *Dest = mapValueToIceVar(BinOp); | 317 Ice::Variable *Dest = mapValueToIceVar(BinOp); |
| 300 return Ice::InstArithmetic::create(Func, Opcode, Dest, Src0, Src1); | 318 return Ice::InstArithmetic::create(Func, Opcode, Dest, Src0, Src1); |
| 301 } | 319 } |
| 302 | 320 |
| 303 Ice::Inst *convertPHINodeInstruction(const PHINode *Inst) { | 321 Ice::Inst *convertPHINodeInstruction(const PHINode *Inst) { |
| 304 unsigned NumValues = Inst->getNumIncomingValues(); | 322 unsigned NumValues = Inst->getNumIncomingValues(); |
| 305 Ice::InstPhi *IcePhi = | 323 Ice::InstPhi *IcePhi = |
| 306 Ice::InstPhi::create(Func, NumValues, mapValueToIceVar(Inst)); | 324 Ice::InstPhi::create(Func, NumValues, mapValueToIceVar(Inst)); |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 | 522 |
| 505 Ice::Inst *convertCallInstruction(const CallInst *Inst) { | 523 Ice::Inst *convertCallInstruction(const CallInst *Inst) { |
| 506 Ice::Variable *Dest = mapValueToIceVar(Inst); | 524 Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 507 Ice::Operand *CallTarget = convertValue(Inst->getCalledValue()); | 525 Ice::Operand *CallTarget = convertValue(Inst->getCalledValue()); |
| 508 unsigned NumArgs = Inst->getNumArgOperands(); | 526 unsigned NumArgs = Inst->getNumArgOperands(); |
| 509 // Note: Subzero doesn't (yet) do anything special with the Tail | 527 // Note: Subzero doesn't (yet) do anything special with the Tail |
| 510 // flag in the bitcode, i.e. CallInst::isTailCall(). | 528 // flag in the bitcode, i.e. CallInst::isTailCall(). |
| 511 Ice::InstCall *NewInst = NULL; | 529 Ice::InstCall *NewInst = NULL; |
| 512 const Ice::Intrinsics::FullIntrinsicInfo *Info = NULL; | 530 const Ice::Intrinsics::FullIntrinsicInfo *Info = NULL; |
| 513 | 531 |
| 514 if (Ice::ConstantRelocatable *Target = | 532 if (const auto Target = dyn_cast<Ice::ConstantRelocatable>(CallTarget)) { |
| 515 llvm::dyn_cast<Ice::ConstantRelocatable>(CallTarget)) { | |
| 516 // Check if this direct call is to an Intrinsic (starts with "llvm.") | 533 // Check if this direct call is to an Intrinsic (starts with "llvm.") |
| 517 static const char LLVMPrefix[] = "llvm."; | 534 static const char LLVMPrefix[] = "llvm."; |
| 518 const size_t LLVMPrefixLen = strlen(LLVMPrefix); | 535 const size_t LLVMPrefixLen = strlen(LLVMPrefix); |
| 519 Ice::IceString Name = Target->getName(); | 536 Ice::IceString Name = Target->getName(); |
| 520 if (Name.substr(0, LLVMPrefixLen) == LLVMPrefix) { | 537 if (Name.substr(0, LLVMPrefixLen) == LLVMPrefix) { |
| 521 Ice::IceString NameSuffix = Name.substr(LLVMPrefixLen); | 538 Ice::IceString NameSuffix = Name.substr(LLVMPrefixLen); |
| 522 Info = Ctx->getIntrinsicsInfo().find(NameSuffix); | 539 Info = Ctx->getIntrinsicsInfo().find(NameSuffix); |
| 523 if (!Info) { | 540 if (!Info) { |
| 524 report_fatal_error(std::string("Invalid PNaCl intrinsic call: ") + | 541 report_fatal_error(std::string("Invalid PNaCl intrinsic call: ") + |
| 525 LLVMObjectAsString(Inst)); | 542 LLVMObjectAsString(Inst)); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 << I->getArgType(ArgIndex) | 615 << I->getArgType(ArgIndex) |
| 599 << ". Found: " << Call->getArg(ArgIndex)->getType(); | 616 << ". Found: " << Call->getArg(ArgIndex)->getType(); |
| 600 report_fatal_error(StrBuf.str()); | 617 report_fatal_error(StrBuf.str()); |
| 601 break; | 618 break; |
| 602 } | 619 } |
| 603 } | 620 } |
| 604 } | 621 } |
| 605 | 622 |
| 606 private: | 623 private: |
| 607 // Data | 624 // Data |
| 608 Ice::GlobalContext *Ctx; | |
| 609 Ice::Cfg *Func; | 625 Ice::Cfg *Func; |
| 610 std::map<const Value *, Ice::Variable *> VarMap; | 626 std::map<const Value *, Ice::Variable *> VarMap; |
| 611 std::map<const BasicBlock *, Ice::CfgNode *> NodeMap; | 627 std::map<const BasicBlock *, Ice::CfgNode *> NodeMap; |
| 612 Ice::TypeConverter TypeConverter; | |
| 613 }; | 628 }; |
| 614 | 629 |
| 630 // Converter from LLVM global variables to ICE. The entry point is the |
| 631 // convertGlobalsToIce method. |
| 632 // |
| 633 // Note: this currently assumes that the given IR was verified to be |
| 634 // valid PNaCl bitcode. Othewise, the behavior is undefined. |
| 635 class LLVM2ICEGlobalsConverter : public LLVM2ICEConverter { |
| 636 LLVM2ICEGlobalsConverter(const LLVM2ICEGlobalsConverter &) = delete; |
| 637 LLVM2ICEGlobalsConverter & |
| 638 operator-(const LLVM2ICEGlobalsConverter &) = delete; |
| 639 |
| 640 public: |
| 641 LLVM2ICEGlobalsConverter(Ice::GlobalContext *Ctx, LLVMContext &LLVMContext) |
| 642 : LLVM2ICEConverter(Ctx, LLVMContext) {} |
| 643 |
| 644 ~LLVM2ICEGlobalsConverter() { DeleteContainerSeconds(GlobalVarAddressMap); } |
| 645 |
| 646 /// Converts global variables, and their initializers into ICE global |
| 647 /// addresses, for module Mod. Puts corresponding converted global |
| 648 /// addresses into GlobalAddresses. |
| 649 void convertGlobalsToIce(Module *Mod, |
| 650 Ice::Translator::GlobalAddressList &GlobalAddresses); |
| 651 |
| 652 private: |
| 653 typedef std::map<const GlobalVariable *, Ice::GlobalAddress *> |
| 654 GlobalVarAddressMapType; |
| 655 // Map from global variables to their corresponding global address. |
| 656 GlobalVarAddressMapType GlobalVarAddressMap; |
| 657 |
| 658 // Adds the Initializer to the list of initializers for Global address. |
| 659 void addGlobalInitializer(Ice::GlobalAddress &Global, |
| 660 const Constant *Initializer) { |
| 661 const bool HasOffset = false; |
| 662 const Ice::GlobalAddress::RelocOffsetType Offset = 0; |
| 663 addGlobalInitializer(Global, Initializer, HasOffset, Offset); |
| 664 } |
| 665 |
| 666 // Adds Initializer to the list of initializers for Global |
| 667 // address. HasOffset is true only if Initializer is a relocation |
| 668 // initializer and Offset should be added to the relocation. |
| 669 void addGlobalInitializer(Ice::GlobalAddress &Global, |
| 670 const Constant *Initializer, bool HasOffset, |
| 671 Ice::GlobalAddress::RelocOffsetType Offset); |
| 672 |
| 673 // Returns the global address associated with global variable GV. |
| 674 Ice::GlobalAddress *getGlobalVarAddress(const GlobalVariable *GV) { |
| 675 if (GlobalVarAddressMap.find(GV) == GlobalVarAddressMap.end()) |
| 676 GlobalVarAddressMap[GV] = new Ice::GlobalAddress(); |
| 677 return GlobalVarAddressMap[GV]; |
| 678 } |
| 679 |
| 680 // Converts the given constant C to the corresponding integer |
| 681 // literal it contains. |
| 682 Ice::GlobalAddress::RelocOffsetType |
| 683 getIntegerLiteralConstant(const Value *C) { |
| 684 const auto CI = dyn_cast<ConstantInt>(C); |
| 685 if (CI && CI->getType()->isIntegerTy(32)) |
| 686 return CI->getSExtValue(); |
| 687 |
| 688 std::string Buffer; |
| 689 raw_string_ostream StrBuf(Buffer); |
| 690 StrBuf << "Constant not i32 literal: " << *C; |
| 691 report_fatal_error(StrBuf.str()); |
| 692 return 0; |
| 693 } |
| 694 }; |
| 695 |
| 696 void LLVM2ICEGlobalsConverter::convertGlobalsToIce( |
| 697 Module *Mod, Ice::Translator::GlobalAddressList &GlobalAddresses) { |
| 698 for (Module::const_global_iterator I = Mod->global_begin(), |
| 699 E = Mod->global_end(); |
| 700 I != E; ++I) { |
| 701 if (!I->hasInitializer() && Ctx->getFlags().AllowUninitializedGlobals) |
| 702 continue; |
| 703 |
| 704 const auto GV = dyn_cast<GlobalVariable>(I); |
| 705 assert(GV); |
| 706 Ice::IceString Name = GV->getName(); |
| 707 if (!GV->hasInternalLinkage()) { |
| 708 std::string Buffer; |
| 709 raw_string_ostream StrBuf(Buffer); |
| 710 StrBuf << "Can't define external global address: " << Name; |
| 711 report_fatal_error(StrBuf.str()); |
| 712 } |
| 713 |
| 714 Ice::GlobalAddress *Addr = getGlobalVarAddress(GV); |
| 715 GlobalAddresses.push_back(Addr); |
| 716 Addr->setAlignment(GV->getAlignment()); |
| 717 Addr->setIsConstant(GV->isConstant()); |
| 718 // Note: We allow external for cross tests. |
| 719 Addr->setIsInternal(!GV->isExternallyInitialized()); |
| 720 Addr->setName(Name); |
| 721 |
| 722 const Constant *Initializer = GV->getInitializer(); |
| 723 if (const auto CompoundInit = dyn_cast<ConstantStruct>(Initializer)) { |
| 724 for (ConstantStruct::const_op_iterator I = CompoundInit->op_begin(), |
| 725 E = CompoundInit->op_end(); |
| 726 I != E; ++I) { |
| 727 if (const auto Init = dyn_cast<Constant>(I)) { |
| 728 addGlobalInitializer(*Addr, Init); |
| 729 } |
| 730 } |
| 731 } else { |
| 732 addGlobalInitializer(*Addr, Initializer); |
| 733 } |
| 734 } |
| 735 } |
| 736 |
| 737 void LLVM2ICEGlobalsConverter::addGlobalInitializer( |
| 738 Ice::GlobalAddress &Global, const Constant *Initializer, bool HasOffset, |
| 739 Ice::GlobalAddress::RelocOffsetType Offset) { |
| 740 assert(HasOffset || Offset == 0); |
| 741 |
| 742 if (const auto CDA = dyn_cast<ConstantDataArray>(Initializer)) { |
| 743 assert(!HasOffset && isa<IntegerType>(CDA->getElementType()) && |
| 744 (cast<IntegerType>(CDA->getElementType())->getBitWidth() == 8)); |
| 745 Global.addInitializer(new Ice::GlobalAddress::DataInitializer( |
| 746 CDA->getRawDataValues().data(), CDA->getNumElements())); |
| 747 return; |
| 748 } |
| 749 |
| 750 if (isa<ConstantAggregateZero>(Initializer)) { |
| 751 if (const auto AT = dyn_cast<ArrayType>(Initializer->getType())) { |
| 752 assert(!HasOffset && isa<IntegerType>(AT->getElementType()) && |
| 753 (cast<IntegerType>(AT->getElementType())->getBitWidth() == 8)); |
| 754 Global.addInitializer( |
| 755 new Ice::GlobalAddress::ZeroInitializer(AT->getNumElements())); |
| 756 } else { |
| 757 llvm_unreachable("Unhandled constant aggregate zero type"); |
| 758 } |
| 759 return; |
| 760 } |
| 761 |
| 762 if (const auto Exp = dyn_cast<ConstantExpr>(Initializer)) { |
| 763 switch (Exp->getOpcode()) { |
| 764 case Instruction::Add: |
| 765 assert(!HasOffset); |
| 766 addGlobalInitializer(Global, Exp->getOperand(0), true, |
| 767 getIntegerLiteralConstant(Exp->getOperand(1))); |
| 768 return; |
| 769 case Instruction::PtrToInt: { |
| 770 assert(TypeConverter.convertToIceType(Exp->getType()) == |
| 771 TypeConverter.getIcePointerType()); |
| 772 const auto GV = dyn_cast<GlobalValue>(Exp->getOperand(0)); |
| 773 assert(GV); |
| 774 if (const auto Fcn = dyn_cast<Function>(GV)) { |
| 775 Ice::GlobalAddress::RelocationAddress Addr(Fcn); |
| 776 Global.addInitializer( |
| 777 new Ice::GlobalAddress::RelocInitializer(Addr, Offset)); |
| 778 return; |
| 779 } else if (const auto Var = dyn_cast<GlobalVariable>(GV)) { |
| 780 Ice::GlobalAddress::RelocationAddress Addr(getGlobalVarAddress(Var)); |
| 781 Global.addInitializer( |
| 782 new Ice::GlobalAddress::RelocInitializer(Addr, Offset)); |
| 783 return; |
| 784 } |
| 785 break; |
| 786 } |
| 787 default: |
| 788 break; |
| 789 } |
| 790 } |
| 791 |
| 792 std::string Buffer; |
| 793 raw_string_ostream StrBuf(Buffer); |
| 794 StrBuf << "Unhandled global initializer: " << Initializer; |
| 795 report_fatal_error(StrBuf.str()); |
| 796 } |
| 797 |
| 615 } // end of anonymous namespace | 798 } // end of anonymous namespace |
| 616 | 799 |
| 617 namespace Ice { | 800 namespace Ice { |
| 618 | 801 |
| 619 void Converter::convertToIce() { | 802 void Converter::convertToIce() { |
| 620 static TimerIdT IDconvertToIce = GlobalContext::getTimerID("convertToIce"); | 803 static TimerIdT IDconvertToIce = GlobalContext::getTimerID("convertToIce"); |
| 621 TimerMarker T(IDconvertToIce, Ctx); | 804 TimerMarker T(IDconvertToIce, Ctx); |
| 622 nameUnnamedGlobalAddresses(Mod); | 805 nameUnnamedGlobalAddresses(Mod); |
| 806 nameUnnamedFunctions(Mod); |
| 623 if (!Ctx->getFlags().DisableGlobals) | 807 if (!Ctx->getFlags().DisableGlobals) |
| 624 convertGlobals(Mod); | 808 convertGlobals(Mod); |
| 625 convertFunctions(); | 809 convertFunctions(); |
| 626 } | 810 } |
| 627 | 811 |
| 812 void Converter::convertGlobals(Module *Mod) { |
| 813 LLVM2ICEGlobalsConverter GlobalsConverter(Ctx, Mod->getContext()); |
| 814 Translator::GlobalAddressList GlobalAddresses; |
| 815 GlobalsConverter.convertGlobalsToIce(Mod, GlobalAddresses); |
| 816 lowerGlobals(GlobalAddresses); |
| 817 } |
| 818 |
| 628 void Converter::convertFunctions() { | 819 void Converter::convertFunctions() { |
| 629 for (const Function &I : *Mod) { | 820 for (const Function &I : *Mod) { |
| 630 if (I.empty()) | 821 if (I.empty()) |
| 631 continue; | 822 continue; |
| 632 LLVM2ICEConverter FunctionConverter(Ctx, Mod->getContext()); | 823 LLVM2ICEFunctionConverter FunctionConverter(Ctx, Mod->getContext()); |
| 633 | 824 |
| 634 Cfg *Fcn = FunctionConverter.convertFunction(&I); | 825 Cfg *Fcn = FunctionConverter.convertFunction(&I); |
| 635 translateFcn(Fcn); | 826 translateFcn(Fcn); |
| 636 } | 827 } |
| 637 | 828 |
| 638 emitConstants(); | 829 emitConstants(); |
| 639 } | 830 } |
| 640 | 831 |
| 641 } // end of namespace Ice | 832 } // end of namespace Ice |
| OLD | NEW |