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