Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceConverter.cpp - Converts LLVM to Ice ---------------===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the LLVM to ICE converter. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 14 | #include <iostream> |
| 15 | |
| 16 | #include "llvm/IR/Constant.h" |
| 17 | #include "llvm/IR/Constants.h" |
| 18 | #include "llvm/IR/DataLayout.h" |
| 19 | #include "llvm/IR/Instruction.h" |
| 20 | #include "llvm/IR/Instructions.h" |
| 21 | #include "llvm/IR/LLVMContext.h" |
| 22 | #include "llvm/IR/Module.h" |
| 23 | |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 24 | #include "IceCfg.h" |
| 25 | #include "IceCfgNode.h" |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 26 | #include "IceClFlags.h" |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 27 | #include "IceConverter.h" |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 28 | #include "IceDefs.h" |
| 29 | #include "IceGlobalContext.h" |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 30 | #include "IceGlobalInits.h" |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 31 | #include "IceInst.h" |
| 32 | #include "IceOperand.h" |
Karl Schimpf | b164d20 | 2014-07-11 10:26:34 -0700 | [diff] [blame] | 33 | #include "IceTargetLowering.h" |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 34 | #include "IceTypes.h" |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 35 | #include "IceTypeConverter.h" |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 36 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 37 | // TODO(kschimpf): Remove two namespaces being visible at once. |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
| 40 | namespace { |
| 41 | |
| 42 | // Debugging helper |
| 43 | template <typename T> static std::string LLVMObjectAsString(const T *O) { |
| 44 | std::string Dump; |
| 45 | raw_string_ostream Stream(Dump); |
| 46 | O->print(Stream); |
| 47 | return Stream.str(); |
| 48 | } |
| 49 | |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 50 | // Base class for converting LLVM to ICE. |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 51 | class LLVM2ICEConverter { |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 52 | LLVM2ICEConverter(const LLVM2ICEConverter &) = delete; |
| 53 | LLVM2ICEConverter &operator=(const LLVM2ICEConverter &) = delete; |
| 54 | |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 55 | public: |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 56 | LLVM2ICEConverter(Ice::Converter &Converter) |
| 57 | : Converter(Converter), Ctx(Converter.getContext()), |
| 58 | TypeConverter(Converter.getModule()->getContext()) {} |
| 59 | |
| 60 | Ice::Converter &getConverter() const { return Converter; } |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 61 | |
| 62 | protected: |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 63 | Ice::Converter &Converter; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 64 | Ice::GlobalContext *Ctx; |
| 65 | const Ice::TypeConverter TypeConverter; |
| 66 | }; |
| 67 | |
| 68 | // Converter from LLVM functions to ICE. The entry point is the |
| 69 | // convertFunction method. |
| 70 | // |
| 71 | // Note: this currently assumes that the given IR was verified to be |
| 72 | // valid PNaCl bitcode. Otherwise, the behavior is undefined. |
| 73 | class LLVM2ICEFunctionConverter : LLVM2ICEConverter { |
| 74 | LLVM2ICEFunctionConverter(const LLVM2ICEFunctionConverter &) = delete; |
| 75 | LLVM2ICEFunctionConverter & |
| 76 | operator=(const LLVM2ICEFunctionConverter &) = delete; |
| 77 | |
| 78 | public: |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 79 | LLVM2ICEFunctionConverter(Ice::Converter &Converter) |
| 80 | : LLVM2ICEConverter(Converter), Func(nullptr) {} |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 81 | |
| 82 | // Caller is expected to delete the returned Ice::Cfg object. |
| 83 | Ice::Cfg *convertFunction(const Function *F) { |
| 84 | VarMap.clear(); |
| 85 | NodeMap.clear(); |
| 86 | Func = new Ice::Cfg(Ctx); |
| 87 | Func->setFunctionName(F->getName()); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 88 | Func->setReturnType(convertToIceType(F->getReturnType())); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 89 | Func->setInternal(F->hasInternalLinkage()); |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 90 | Ice::TimerMarker T(Ice::TimerStack::TT_llvmConvert, Func); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 91 | |
| 92 | // The initial definition/use of each arg is the entry node. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 93 | for (auto ArgI = F->arg_begin(), ArgE = F->arg_end(); ArgI != ArgE; |
| 94 | ++ArgI) { |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 95 | Func->addArg(mapValueToIceVar(ArgI)); |
| 96 | } |
| 97 | |
| 98 | // Make an initial pass through the block list just to resolve the |
| 99 | // blocks in the original linearized order. Otherwise the ICE |
| 100 | // linearized order will be affected by branch targets in |
| 101 | // terminator instructions. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 102 | for (const BasicBlock &BBI : *F) |
| 103 | mapBasicBlockToNode(&BBI); |
| 104 | for (const BasicBlock &BBI : *F) |
| 105 | convertBasicBlock(&BBI); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 106 | Func->setEntryNode(mapBasicBlockToNode(&F->getEntryBlock())); |
| 107 | Func->computePredecessors(); |
| 108 | |
| 109 | return Func; |
| 110 | } |
| 111 | |
| 112 | // 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 |
| 114 | // global initializers. |
| 115 | Ice::Constant *convertConstant(const Constant *Const) { |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 116 | if (const auto GV = dyn_cast<GlobalValue>(Const)) { |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 117 | Ice::GlobalDeclaration *Decl = getConverter().getGlobalDeclaration(GV); |
| 118 | const Ice::RelocOffsetT Offset = 0; |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 119 | return Ctx->getConstantSym(Offset, Decl->getName(), |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 120 | Decl->getSuppressMangling()); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 121 | } else if (const auto CI = dyn_cast<ConstantInt>(Const)) { |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 122 | Ice::Type Ty = convertToIceType(CI->getType()); |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 123 | return Ctx->getConstantInt(Ty, CI->getSExtValue()); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 124 | } else if (const auto CFP = dyn_cast<ConstantFP>(Const)) { |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 125 | Ice::Type Type = convertToIceType(CFP->getType()); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 126 | if (Type == Ice::IceType_f32) |
| 127 | return Ctx->getConstantFloat(CFP->getValueAPF().convertToFloat()); |
| 128 | else if (Type == Ice::IceType_f64) |
| 129 | return Ctx->getConstantDouble(CFP->getValueAPF().convertToDouble()); |
| 130 | llvm_unreachable("Unexpected floating point type"); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 131 | return nullptr; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 132 | } else if (const auto CU = dyn_cast<UndefValue>(Const)) { |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 133 | return Ctx->getConstantUndef(convertToIceType(CU->getType())); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 134 | } else { |
| 135 | llvm_unreachable("Unhandled constant type"); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 136 | return nullptr; |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | private: |
| 141 | // LLVM values (instructions, etc.) are mapped directly to ICE variables. |
| 142 | // mapValueToIceVar has a version that forces an ICE type on the variable, |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 143 | // and a version that just uses convertToIceType on V. |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 144 | Ice::Variable *mapValueToIceVar(const Value *V, Ice::Type IceTy) { |
| 145 | if (IceTy == Ice::IceType_void) |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 146 | return nullptr; |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 147 | if (VarMap.find(V) == VarMap.end()) { |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 148 | VarMap[V] = Func->makeVariable(IceTy); |
| 149 | if (ALLOW_DUMP) |
| 150 | VarMap[V]->setName(Func, V->getName()); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 151 | } |
| 152 | return VarMap[V]; |
| 153 | } |
| 154 | |
| 155 | Ice::Variable *mapValueToIceVar(const Value *V) { |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 156 | return mapValueToIceVar(V, convertToIceType(V->getType())); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | Ice::CfgNode *mapBasicBlockToNode(const BasicBlock *BB) { |
| 160 | if (NodeMap.find(BB) == NodeMap.end()) { |
Jim Stichnoth | 668a7a3 | 2014-12-10 15:32:25 -0800 | [diff] [blame] | 161 | NodeMap[BB] = Func->makeNode(); |
| 162 | if (ALLOW_DUMP) |
| 163 | NodeMap[BB]->setName(BB->getName()); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 164 | } |
| 165 | return NodeMap[BB]; |
| 166 | } |
| 167 | |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 168 | Ice::Type convertToIceType(Type *LLVMTy) const { |
| 169 | Ice::Type IceTy = TypeConverter.convertToIceType(LLVMTy); |
| 170 | if (IceTy == Ice::IceType_NUM) |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 171 | report_fatal_error(std::string("Invalid PNaCl type ") + |
| 172 | LLVMObjectAsString(LLVMTy)); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 173 | return IceTy; |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // Given an LLVM instruction and an operand number, produce the |
| 177 | // Ice::Operand this refers to. If there's no such operand, return |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 178 | // nullptr. |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 179 | Ice::Operand *convertOperand(const Instruction *Inst, unsigned OpNum) { |
| 180 | if (OpNum >= Inst->getNumOperands()) { |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 181 | return nullptr; |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 182 | } |
| 183 | const Value *Op = Inst->getOperand(OpNum); |
| 184 | return convertValue(Op); |
| 185 | } |
| 186 | |
| 187 | Ice::Operand *convertValue(const Value *Op) { |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 188 | if (const auto Const = dyn_cast<Constant>(Op)) { |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 189 | return convertConstant(Const); |
| 190 | } else { |
| 191 | return mapValueToIceVar(Op); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // Note: this currently assumes a 1x1 mapping between LLVM IR and Ice |
| 196 | // instructions. |
| 197 | Ice::Inst *convertInstruction(const Instruction *Inst) { |
| 198 | switch (Inst->getOpcode()) { |
| 199 | case Instruction::PHI: |
| 200 | return convertPHINodeInstruction(cast<PHINode>(Inst)); |
| 201 | case Instruction::Br: |
| 202 | return convertBrInstruction(cast<BranchInst>(Inst)); |
| 203 | case Instruction::Ret: |
| 204 | return convertRetInstruction(cast<ReturnInst>(Inst)); |
| 205 | case Instruction::IntToPtr: |
| 206 | return convertIntToPtrInstruction(cast<IntToPtrInst>(Inst)); |
| 207 | case Instruction::PtrToInt: |
| 208 | return convertPtrToIntInstruction(cast<PtrToIntInst>(Inst)); |
| 209 | case Instruction::ICmp: |
| 210 | return convertICmpInstruction(cast<ICmpInst>(Inst)); |
| 211 | case Instruction::FCmp: |
| 212 | return convertFCmpInstruction(cast<FCmpInst>(Inst)); |
| 213 | case Instruction::Select: |
| 214 | return convertSelectInstruction(cast<SelectInst>(Inst)); |
| 215 | case Instruction::Switch: |
| 216 | return convertSwitchInstruction(cast<SwitchInst>(Inst)); |
| 217 | case Instruction::Load: |
| 218 | return convertLoadInstruction(cast<LoadInst>(Inst)); |
| 219 | case Instruction::Store: |
| 220 | return convertStoreInstruction(cast<StoreInst>(Inst)); |
| 221 | case Instruction::ZExt: |
| 222 | return convertCastInstruction(cast<ZExtInst>(Inst), Ice::InstCast::Zext); |
| 223 | case Instruction::SExt: |
| 224 | return convertCastInstruction(cast<SExtInst>(Inst), Ice::InstCast::Sext); |
| 225 | case Instruction::Trunc: |
| 226 | return convertCastInstruction(cast<TruncInst>(Inst), |
| 227 | Ice::InstCast::Trunc); |
| 228 | case Instruction::FPTrunc: |
| 229 | return convertCastInstruction(cast<FPTruncInst>(Inst), |
| 230 | Ice::InstCast::Fptrunc); |
| 231 | case Instruction::FPExt: |
| 232 | return convertCastInstruction(cast<FPExtInst>(Inst), |
| 233 | Ice::InstCast::Fpext); |
| 234 | case Instruction::FPToSI: |
| 235 | return convertCastInstruction(cast<FPToSIInst>(Inst), |
| 236 | Ice::InstCast::Fptosi); |
| 237 | case Instruction::FPToUI: |
| 238 | return convertCastInstruction(cast<FPToUIInst>(Inst), |
| 239 | Ice::InstCast::Fptoui); |
| 240 | case Instruction::SIToFP: |
| 241 | return convertCastInstruction(cast<SIToFPInst>(Inst), |
| 242 | Ice::InstCast::Sitofp); |
| 243 | case Instruction::UIToFP: |
| 244 | return convertCastInstruction(cast<UIToFPInst>(Inst), |
| 245 | Ice::InstCast::Uitofp); |
| 246 | case Instruction::BitCast: |
| 247 | return convertCastInstruction(cast<BitCastInst>(Inst), |
| 248 | Ice::InstCast::Bitcast); |
| 249 | case Instruction::Add: |
| 250 | return convertArithInstruction(Inst, Ice::InstArithmetic::Add); |
| 251 | case Instruction::Sub: |
| 252 | return convertArithInstruction(Inst, Ice::InstArithmetic::Sub); |
| 253 | case Instruction::Mul: |
| 254 | return convertArithInstruction(Inst, Ice::InstArithmetic::Mul); |
| 255 | case Instruction::UDiv: |
| 256 | return convertArithInstruction(Inst, Ice::InstArithmetic::Udiv); |
| 257 | case Instruction::SDiv: |
| 258 | return convertArithInstruction(Inst, Ice::InstArithmetic::Sdiv); |
| 259 | case Instruction::URem: |
| 260 | return convertArithInstruction(Inst, Ice::InstArithmetic::Urem); |
| 261 | case Instruction::SRem: |
| 262 | return convertArithInstruction(Inst, Ice::InstArithmetic::Srem); |
| 263 | case Instruction::Shl: |
| 264 | return convertArithInstruction(Inst, Ice::InstArithmetic::Shl); |
| 265 | case Instruction::LShr: |
| 266 | return convertArithInstruction(Inst, Ice::InstArithmetic::Lshr); |
| 267 | case Instruction::AShr: |
| 268 | return convertArithInstruction(Inst, Ice::InstArithmetic::Ashr); |
| 269 | case Instruction::FAdd: |
| 270 | return convertArithInstruction(Inst, Ice::InstArithmetic::Fadd); |
| 271 | case Instruction::FSub: |
| 272 | return convertArithInstruction(Inst, Ice::InstArithmetic::Fsub); |
| 273 | case Instruction::FMul: |
| 274 | return convertArithInstruction(Inst, Ice::InstArithmetic::Fmul); |
| 275 | case Instruction::FDiv: |
| 276 | return convertArithInstruction(Inst, Ice::InstArithmetic::Fdiv); |
| 277 | case Instruction::FRem: |
| 278 | return convertArithInstruction(Inst, Ice::InstArithmetic::Frem); |
| 279 | case Instruction::And: |
| 280 | return convertArithInstruction(Inst, Ice::InstArithmetic::And); |
| 281 | case Instruction::Or: |
| 282 | return convertArithInstruction(Inst, Ice::InstArithmetic::Or); |
| 283 | case Instruction::Xor: |
| 284 | return convertArithInstruction(Inst, Ice::InstArithmetic::Xor); |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 285 | case Instruction::ExtractElement: |
| 286 | return convertExtractElementInstruction(cast<ExtractElementInst>(Inst)); |
| 287 | case Instruction::InsertElement: |
| 288 | return convertInsertElementInstruction(cast<InsertElementInst>(Inst)); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 289 | case Instruction::Call: |
| 290 | return convertCallInstruction(cast<CallInst>(Inst)); |
| 291 | case Instruction::Alloca: |
| 292 | return convertAllocaInstruction(cast<AllocaInst>(Inst)); |
| 293 | case Instruction::Unreachable: |
| 294 | return convertUnreachableInstruction(cast<UnreachableInst>(Inst)); |
| 295 | default: |
| 296 | report_fatal_error(std::string("Invalid PNaCl instruction: ") + |
| 297 | LLVMObjectAsString(Inst)); |
| 298 | } |
| 299 | |
| 300 | llvm_unreachable("convertInstruction"); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 301 | return nullptr; |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | Ice::Inst *convertLoadInstruction(const LoadInst *Inst) { |
| 305 | Ice::Operand *Src = convertOperand(Inst, 0); |
| 306 | Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 307 | return Ice::InstLoad::create(Func, Dest, Src); |
| 308 | } |
| 309 | |
| 310 | Ice::Inst *convertStoreInstruction(const StoreInst *Inst) { |
| 311 | Ice::Operand *Addr = convertOperand(Inst, 1); |
| 312 | Ice::Operand *Val = convertOperand(Inst, 0); |
| 313 | return Ice::InstStore::create(Func, Val, Addr); |
| 314 | } |
| 315 | |
| 316 | Ice::Inst *convertArithInstruction(const Instruction *Inst, |
| 317 | Ice::InstArithmetic::OpKind Opcode) { |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 318 | const auto BinOp = cast<BinaryOperator>(Inst); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 319 | Ice::Operand *Src0 = convertOperand(Inst, 0); |
| 320 | Ice::Operand *Src1 = convertOperand(Inst, 1); |
| 321 | Ice::Variable *Dest = mapValueToIceVar(BinOp); |
| 322 | return Ice::InstArithmetic::create(Func, Opcode, Dest, Src0, Src1); |
| 323 | } |
| 324 | |
| 325 | Ice::Inst *convertPHINodeInstruction(const PHINode *Inst) { |
| 326 | unsigned NumValues = Inst->getNumIncomingValues(); |
| 327 | Ice::InstPhi *IcePhi = |
| 328 | Ice::InstPhi::create(Func, NumValues, mapValueToIceVar(Inst)); |
| 329 | for (unsigned N = 0, E = NumValues; N != E; ++N) { |
| 330 | IcePhi->addArgument(convertOperand(Inst, N), |
| 331 | mapBasicBlockToNode(Inst->getIncomingBlock(N))); |
| 332 | } |
| 333 | return IcePhi; |
| 334 | } |
| 335 | |
| 336 | Ice::Inst *convertBrInstruction(const BranchInst *Inst) { |
| 337 | if (Inst->isConditional()) { |
| 338 | Ice::Operand *Src = convertOperand(Inst, 0); |
| 339 | BasicBlock *BBThen = Inst->getSuccessor(0); |
| 340 | BasicBlock *BBElse = Inst->getSuccessor(1); |
| 341 | Ice::CfgNode *NodeThen = mapBasicBlockToNode(BBThen); |
| 342 | Ice::CfgNode *NodeElse = mapBasicBlockToNode(BBElse); |
| 343 | return Ice::InstBr::create(Func, Src, NodeThen, NodeElse); |
| 344 | } else { |
| 345 | BasicBlock *BBSucc = Inst->getSuccessor(0); |
| 346 | return Ice::InstBr::create(Func, mapBasicBlockToNode(BBSucc)); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | Ice::Inst *convertIntToPtrInstruction(const IntToPtrInst *Inst) { |
| 351 | Ice::Operand *Src = convertOperand(Inst, 0); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 352 | Ice::Variable *Dest = |
| 353 | mapValueToIceVar(Inst, TypeConverter.getIcePointerType()); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 354 | return Ice::InstAssign::create(Func, Dest, Src); |
| 355 | } |
| 356 | |
| 357 | Ice::Inst *convertPtrToIntInstruction(const PtrToIntInst *Inst) { |
| 358 | Ice::Operand *Src = convertOperand(Inst, 0); |
| 359 | Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 360 | return Ice::InstAssign::create(Func, Dest, Src); |
| 361 | } |
| 362 | |
| 363 | Ice::Inst *convertRetInstruction(const ReturnInst *Inst) { |
| 364 | Ice::Operand *RetOperand = convertOperand(Inst, 0); |
| 365 | if (RetOperand) { |
| 366 | return Ice::InstRet::create(Func, RetOperand); |
| 367 | } else { |
| 368 | return Ice::InstRet::create(Func); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | Ice::Inst *convertCastInstruction(const Instruction *Inst, |
| 373 | Ice::InstCast::OpKind CastKind) { |
| 374 | Ice::Operand *Src = convertOperand(Inst, 0); |
| 375 | Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 376 | return Ice::InstCast::create(Func, CastKind, Dest, Src); |
| 377 | } |
| 378 | |
| 379 | Ice::Inst *convertICmpInstruction(const ICmpInst *Inst) { |
| 380 | Ice::Operand *Src0 = convertOperand(Inst, 0); |
| 381 | Ice::Operand *Src1 = convertOperand(Inst, 1); |
| 382 | Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 383 | |
| 384 | Ice::InstIcmp::ICond Cond; |
| 385 | switch (Inst->getPredicate()) { |
| 386 | default: |
| 387 | llvm_unreachable("ICmpInst predicate"); |
| 388 | case CmpInst::ICMP_EQ: |
| 389 | Cond = Ice::InstIcmp::Eq; |
| 390 | break; |
| 391 | case CmpInst::ICMP_NE: |
| 392 | Cond = Ice::InstIcmp::Ne; |
| 393 | break; |
| 394 | case CmpInst::ICMP_UGT: |
| 395 | Cond = Ice::InstIcmp::Ugt; |
| 396 | break; |
| 397 | case CmpInst::ICMP_UGE: |
| 398 | Cond = Ice::InstIcmp::Uge; |
| 399 | break; |
| 400 | case CmpInst::ICMP_ULT: |
| 401 | Cond = Ice::InstIcmp::Ult; |
| 402 | break; |
| 403 | case CmpInst::ICMP_ULE: |
| 404 | Cond = Ice::InstIcmp::Ule; |
| 405 | break; |
| 406 | case CmpInst::ICMP_SGT: |
| 407 | Cond = Ice::InstIcmp::Sgt; |
| 408 | break; |
| 409 | case CmpInst::ICMP_SGE: |
| 410 | Cond = Ice::InstIcmp::Sge; |
| 411 | break; |
| 412 | case CmpInst::ICMP_SLT: |
| 413 | Cond = Ice::InstIcmp::Slt; |
| 414 | break; |
| 415 | case CmpInst::ICMP_SLE: |
| 416 | Cond = Ice::InstIcmp::Sle; |
| 417 | break; |
| 418 | } |
| 419 | |
| 420 | return Ice::InstIcmp::create(Func, Cond, Dest, Src0, Src1); |
| 421 | } |
| 422 | |
| 423 | Ice::Inst *convertFCmpInstruction(const FCmpInst *Inst) { |
| 424 | Ice::Operand *Src0 = convertOperand(Inst, 0); |
| 425 | Ice::Operand *Src1 = convertOperand(Inst, 1); |
| 426 | Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 427 | |
| 428 | Ice::InstFcmp::FCond Cond; |
| 429 | switch (Inst->getPredicate()) { |
| 430 | |
| 431 | default: |
| 432 | llvm_unreachable("FCmpInst predicate"); |
| 433 | |
| 434 | case CmpInst::FCMP_FALSE: |
| 435 | Cond = Ice::InstFcmp::False; |
| 436 | break; |
| 437 | case CmpInst::FCMP_OEQ: |
| 438 | Cond = Ice::InstFcmp::Oeq; |
| 439 | break; |
| 440 | case CmpInst::FCMP_OGT: |
| 441 | Cond = Ice::InstFcmp::Ogt; |
| 442 | break; |
| 443 | case CmpInst::FCMP_OGE: |
| 444 | Cond = Ice::InstFcmp::Oge; |
| 445 | break; |
| 446 | case CmpInst::FCMP_OLT: |
| 447 | Cond = Ice::InstFcmp::Olt; |
| 448 | break; |
| 449 | case CmpInst::FCMP_OLE: |
| 450 | Cond = Ice::InstFcmp::Ole; |
| 451 | break; |
| 452 | case CmpInst::FCMP_ONE: |
| 453 | Cond = Ice::InstFcmp::One; |
| 454 | break; |
| 455 | case CmpInst::FCMP_ORD: |
| 456 | Cond = Ice::InstFcmp::Ord; |
| 457 | break; |
| 458 | case CmpInst::FCMP_UEQ: |
| 459 | Cond = Ice::InstFcmp::Ueq; |
| 460 | break; |
| 461 | case CmpInst::FCMP_UGT: |
| 462 | Cond = Ice::InstFcmp::Ugt; |
| 463 | break; |
| 464 | case CmpInst::FCMP_UGE: |
| 465 | Cond = Ice::InstFcmp::Uge; |
| 466 | break; |
| 467 | case CmpInst::FCMP_ULT: |
| 468 | Cond = Ice::InstFcmp::Ult; |
| 469 | break; |
| 470 | case CmpInst::FCMP_ULE: |
| 471 | Cond = Ice::InstFcmp::Ule; |
| 472 | break; |
| 473 | case CmpInst::FCMP_UNE: |
| 474 | Cond = Ice::InstFcmp::Une; |
| 475 | break; |
| 476 | case CmpInst::FCMP_UNO: |
| 477 | Cond = Ice::InstFcmp::Uno; |
| 478 | break; |
| 479 | case CmpInst::FCMP_TRUE: |
| 480 | Cond = Ice::InstFcmp::True; |
| 481 | break; |
| 482 | } |
| 483 | |
| 484 | return Ice::InstFcmp::create(Func, Cond, Dest, Src0, Src1); |
| 485 | } |
| 486 | |
Matt Wala | 4988923 | 2014-07-18 12:45:09 -0700 | [diff] [blame] | 487 | Ice::Inst *convertExtractElementInstruction(const ExtractElementInst *Inst) { |
| 488 | Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 489 | Ice::Operand *Source1 = convertValue(Inst->getOperand(0)); |
| 490 | Ice::Operand *Source2 = convertValue(Inst->getOperand(1)); |
| 491 | return Ice::InstExtractElement::create(Func, Dest, Source1, Source2); |
| 492 | } |
| 493 | |
| 494 | Ice::Inst *convertInsertElementInstruction(const InsertElementInst *Inst) { |
| 495 | Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 496 | Ice::Operand *Source1 = convertValue(Inst->getOperand(0)); |
| 497 | Ice::Operand *Source2 = convertValue(Inst->getOperand(1)); |
| 498 | Ice::Operand *Source3 = convertValue(Inst->getOperand(2)); |
| 499 | return Ice::InstInsertElement::create(Func, Dest, Source1, Source2, |
| 500 | Source3); |
| 501 | } |
| 502 | |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 503 | Ice::Inst *convertSelectInstruction(const SelectInst *Inst) { |
| 504 | Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 505 | Ice::Operand *Cond = convertValue(Inst->getCondition()); |
| 506 | Ice::Operand *Source1 = convertValue(Inst->getTrueValue()); |
| 507 | Ice::Operand *Source2 = convertValue(Inst->getFalseValue()); |
| 508 | return Ice::InstSelect::create(Func, Dest, Cond, Source1, Source2); |
| 509 | } |
| 510 | |
| 511 | Ice::Inst *convertSwitchInstruction(const SwitchInst *Inst) { |
| 512 | Ice::Operand *Source = convertValue(Inst->getCondition()); |
| 513 | Ice::CfgNode *LabelDefault = mapBasicBlockToNode(Inst->getDefaultDest()); |
| 514 | unsigned NumCases = Inst->getNumCases(); |
| 515 | Ice::InstSwitch *Switch = |
| 516 | Ice::InstSwitch::create(Func, NumCases, Source, LabelDefault); |
| 517 | unsigned CurrentCase = 0; |
| 518 | for (SwitchInst::ConstCaseIt I = Inst->case_begin(), E = Inst->case_end(); |
| 519 | I != E; ++I, ++CurrentCase) { |
Jim Stichnoth | cabfa30 | 2014-09-03 15:19:12 -0700 | [diff] [blame] | 520 | uint64_t CaseValue = I.getCaseValue()->getSExtValue(); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 521 | Ice::CfgNode *CaseSuccessor = mapBasicBlockToNode(I.getCaseSuccessor()); |
| 522 | Switch->addBranch(CurrentCase, CaseValue, CaseSuccessor); |
| 523 | } |
| 524 | return Switch; |
| 525 | } |
| 526 | |
| 527 | Ice::Inst *convertCallInstruction(const CallInst *Inst) { |
| 528 | Ice::Variable *Dest = mapValueToIceVar(Inst); |
| 529 | Ice::Operand *CallTarget = convertValue(Inst->getCalledValue()); |
| 530 | unsigned NumArgs = Inst->getNumArgOperands(); |
| 531 | // Note: Subzero doesn't (yet) do anything special with the Tail |
| 532 | // flag in the bitcode, i.e. CallInst::isTailCall(). |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 533 | Ice::InstCall *NewInst = nullptr; |
| 534 | const Ice::Intrinsics::FullIntrinsicInfo *Info = nullptr; |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 535 | |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 536 | if (const auto Target = dyn_cast<Ice::ConstantRelocatable>(CallTarget)) { |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 537 | // Check if this direct call is to an Intrinsic (starts with "llvm.") |
| 538 | static const char LLVMPrefix[] = "llvm."; |
| 539 | const size_t LLVMPrefixLen = strlen(LLVMPrefix); |
| 540 | Ice::IceString Name = Target->getName(); |
| 541 | if (Name.substr(0, LLVMPrefixLen) == LLVMPrefix) { |
| 542 | Ice::IceString NameSuffix = Name.substr(LLVMPrefixLen); |
| 543 | Info = Ctx->getIntrinsicsInfo().find(NameSuffix); |
| 544 | if (!Info) { |
| 545 | report_fatal_error(std::string("Invalid PNaCl intrinsic call: ") + |
| 546 | LLVMObjectAsString(Inst)); |
| 547 | } |
| 548 | NewInst = Ice::InstIntrinsicCall::create(Func, NumArgs, Dest, |
| 549 | CallTarget, Info->Info); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | // Not an intrinsic call. |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 554 | if (NewInst == nullptr) { |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 555 | NewInst = Ice::InstCall::create(Func, NumArgs, Dest, CallTarget, |
| 556 | Inst->isTailCall()); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 557 | } |
| 558 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 559 | NewInst->addArg(convertOperand(Inst, i)); |
| 560 | } |
| 561 | if (Info) { |
| 562 | validateIntrinsicCall(NewInst, Info); |
| 563 | } |
| 564 | return NewInst; |
| 565 | } |
| 566 | |
| 567 | Ice::Inst *convertAllocaInstruction(const AllocaInst *Inst) { |
| 568 | // PNaCl bitcode only contains allocas of byte-granular objects. |
| 569 | Ice::Operand *ByteCount = convertValue(Inst->getArraySize()); |
| 570 | uint32_t Align = Inst->getAlignment(); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 571 | Ice::Variable *Dest = |
| 572 | mapValueToIceVar(Inst, TypeConverter.getIcePointerType()); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 573 | |
| 574 | return Ice::InstAlloca::create(Func, ByteCount, Align, Dest); |
| 575 | } |
| 576 | |
| 577 | Ice::Inst *convertUnreachableInstruction(const UnreachableInst * /*Inst*/) { |
| 578 | return Ice::InstUnreachable::create(Func); |
| 579 | } |
| 580 | |
| 581 | Ice::CfgNode *convertBasicBlock(const BasicBlock *BB) { |
| 582 | Ice::CfgNode *Node = mapBasicBlockToNode(BB); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 583 | for (const Instruction &II : *BB) { |
| 584 | Ice::Inst *Inst = convertInstruction(&II); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 585 | Node->appendInst(Inst); |
| 586 | } |
| 587 | return Node; |
| 588 | } |
| 589 | |
| 590 | void validateIntrinsicCall(const Ice::InstCall *Call, |
| 591 | const Ice::Intrinsics::FullIntrinsicInfo *I) { |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 592 | Ice::SizeT ArgIndex = 0; |
| 593 | switch (I->validateCall(Call, ArgIndex)) { |
| 594 | default: |
| 595 | report_fatal_error("Unknown validation error for intrinsic call"); |
| 596 | break; |
| 597 | case Ice::Intrinsics::IsValidCall: |
| 598 | break; |
| 599 | case Ice::Intrinsics::BadReturnType: { |
| 600 | std::string Buffer; |
| 601 | raw_string_ostream StrBuf(Buffer); |
| 602 | StrBuf << "Intrinsic call expects return type " << I->getReturnType() |
| 603 | << ". Found: " << Call->getReturnType(); |
| 604 | report_fatal_error(StrBuf.str()); |
| 605 | break; |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 606 | } |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 607 | case Ice::Intrinsics::WrongNumOfArgs: { |
| 608 | std::string Buffer; |
| 609 | raw_string_ostream StrBuf(Buffer); |
| 610 | StrBuf << "Intrinsic call expects " << I->getNumArgs() |
| 611 | << ". Found: " << Call->getNumArgs(); |
| 612 | report_fatal_error(StrBuf.str()); |
| 613 | break; |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 614 | } |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 615 | case Ice::Intrinsics::WrongCallArgType: { |
| 616 | std::string Buffer; |
| 617 | raw_string_ostream StrBuf(Buffer); |
| 618 | StrBuf << "Intrinsic call argument " << ArgIndex << " expects type " |
| 619 | << I->getArgType(ArgIndex) |
| 620 | << ". Found: " << Call->getArg(ArgIndex)->getType(); |
| 621 | report_fatal_error(StrBuf.str()); |
| 622 | break; |
| 623 | } |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
| 627 | private: |
| 628 | // Data |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 629 | Ice::Cfg *Func; |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 630 | std::map<const Value *, Ice::Variable *> VarMap; |
| 631 | std::map<const BasicBlock *, Ice::CfgNode *> NodeMap; |
| 632 | }; |
| 633 | |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 634 | // Converter from LLVM global variables to ICE. The entry point is the |
| 635 | // convertGlobalsToIce method. |
| 636 | // |
| 637 | // Note: this currently assumes that the given IR was verified to be |
| 638 | // valid PNaCl bitcode. Othewise, the behavior is undefined. |
| 639 | class LLVM2ICEGlobalsConverter : public LLVM2ICEConverter { |
| 640 | LLVM2ICEGlobalsConverter(const LLVM2ICEGlobalsConverter &) = delete; |
| 641 | LLVM2ICEGlobalsConverter & |
| 642 | operator-(const LLVM2ICEGlobalsConverter &) = delete; |
| 643 | |
| 644 | public: |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 645 | LLVM2ICEGlobalsConverter(Ice::Converter &Converter) |
| 646 | : LLVM2ICEConverter(Converter) {} |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 647 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 648 | /// Converts global variables, and their initializers into ICE |
| 649 | /// global variable declarations, for module Mod. Puts corresponding |
| 650 | /// converted declarations into VariableDeclarations. |
| 651 | void convertGlobalsToIce( |
| 652 | Module *Mod, |
| 653 | Ice::Translator::VariableDeclarationListType &VariableDeclarations); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 654 | |
| 655 | private: |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 656 | // Adds the Initializer to the list of initializers for the Global |
| 657 | // variable declaraation. |
| 658 | void addGlobalInitializer(Ice::VariableDeclaration &Global, |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 659 | const Constant *Initializer) { |
| 660 | const bool HasOffset = false; |
Jan Voung | c0d965f | 2014-11-04 16:55:01 -0800 | [diff] [blame] | 661 | const Ice::RelocOffsetT Offset = 0; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 662 | addGlobalInitializer(Global, Initializer, HasOffset, Offset); |
| 663 | } |
| 664 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 665 | // Adds Initializer to the list of initializers for Global variable |
| 666 | // declaration. HasOffset is true only if Initializer is a |
| 667 | // relocation initializer and Offset should be added to the |
| 668 | // relocation. |
| 669 | void addGlobalInitializer(Ice::VariableDeclaration &Global, |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 670 | const Constant *Initializer, bool HasOffset, |
Jan Voung | c0d965f | 2014-11-04 16:55:01 -0800 | [diff] [blame] | 671 | Ice::RelocOffsetT Offset); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 672 | |
| 673 | // Converts the given constant C to the corresponding integer |
| 674 | // literal it contains. |
Jan Voung | c0d965f | 2014-11-04 16:55:01 -0800 | [diff] [blame] | 675 | Ice::RelocOffsetT getIntegerLiteralConstant(const Value *C) { |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 676 | const auto CI = dyn_cast<ConstantInt>(C); |
| 677 | if (CI && CI->getType()->isIntegerTy(32)) |
| 678 | return CI->getSExtValue(); |
| 679 | |
| 680 | std::string Buffer; |
| 681 | raw_string_ostream StrBuf(Buffer); |
| 682 | StrBuf << "Constant not i32 literal: " << *C; |
| 683 | report_fatal_error(StrBuf.str()); |
| 684 | return 0; |
| 685 | } |
| 686 | }; |
| 687 | |
| 688 | void LLVM2ICEGlobalsConverter::convertGlobalsToIce( |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 689 | Module *Mod, |
| 690 | Ice::Translator::VariableDeclarationListType &VariableDeclarations) { |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 691 | for (Module::const_global_iterator I = Mod->global_begin(), |
| 692 | E = Mod->global_end(); |
| 693 | I != E; ++I) { |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 694 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 695 | const GlobalVariable *GV = I; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 696 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 697 | Ice::GlobalDeclaration *Var = getConverter().getGlobalDeclaration(GV); |
| 698 | Ice::VariableDeclaration* VarDecl = cast<Ice::VariableDeclaration>(Var); |
| 699 | VariableDeclarations.push_back(VarDecl); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 700 | |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 701 | if (!GV->hasInternalLinkage() && GV->hasInitializer()) { |
| 702 | std::string Buffer; |
| 703 | raw_string_ostream StrBuf(Buffer); |
| 704 | StrBuf << "Can't define external global declaration: " << GV->getName(); |
| 705 | report_fatal_error(StrBuf.str()); |
| 706 | } |
| 707 | |
| 708 | if (!GV->hasInitializer()) { |
| 709 | if (Ctx->getFlags().AllowUninitializedGlobals) |
| 710 | continue; |
| 711 | else { |
| 712 | std::string Buffer; |
| 713 | raw_string_ostream StrBuf(Buffer); |
| 714 | StrBuf << "Global declaration missing initializer: " << GV->getName(); |
| 715 | report_fatal_error(StrBuf.str()); |
| 716 | } |
| 717 | } |
| 718 | |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 719 | const Constant *Initializer = GV->getInitializer(); |
| 720 | if (const auto CompoundInit = dyn_cast<ConstantStruct>(Initializer)) { |
| 721 | for (ConstantStruct::const_op_iterator I = CompoundInit->op_begin(), |
| 722 | E = CompoundInit->op_end(); |
| 723 | I != E; ++I) { |
| 724 | if (const auto Init = dyn_cast<Constant>(I)) { |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 725 | addGlobalInitializer(*VarDecl, Init); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | } else { |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 729 | addGlobalInitializer(*VarDecl, Initializer); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | void LLVM2ICEGlobalsConverter::addGlobalInitializer( |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 735 | Ice::VariableDeclaration &Global, const Constant *Initializer, |
Jan Voung | c0d965f | 2014-11-04 16:55:01 -0800 | [diff] [blame] | 736 | bool HasOffset, Ice::RelocOffsetT Offset) { |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 737 | (void)HasOffset; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 738 | assert(HasOffset || Offset == 0); |
| 739 | |
| 740 | if (const auto CDA = dyn_cast<ConstantDataArray>(Initializer)) { |
| 741 | assert(!HasOffset && isa<IntegerType>(CDA->getElementType()) && |
| 742 | (cast<IntegerType>(CDA->getElementType())->getBitWidth() == 8)); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 743 | Global.addInitializer(new Ice::VariableDeclaration::DataInitializer( |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 744 | CDA->getRawDataValues().data(), CDA->getNumElements())); |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | if (isa<ConstantAggregateZero>(Initializer)) { |
| 749 | if (const auto AT = dyn_cast<ArrayType>(Initializer->getType())) { |
| 750 | assert(!HasOffset && isa<IntegerType>(AT->getElementType()) && |
| 751 | (cast<IntegerType>(AT->getElementType())->getBitWidth() == 8)); |
| 752 | Global.addInitializer( |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 753 | new Ice::VariableDeclaration::ZeroInitializer(AT->getNumElements())); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 754 | } else { |
| 755 | llvm_unreachable("Unhandled constant aggregate zero type"); |
| 756 | } |
| 757 | return; |
| 758 | } |
| 759 | |
| 760 | if (const auto Exp = dyn_cast<ConstantExpr>(Initializer)) { |
| 761 | switch (Exp->getOpcode()) { |
| 762 | case Instruction::Add: |
| 763 | assert(!HasOffset); |
| 764 | addGlobalInitializer(Global, Exp->getOperand(0), true, |
| 765 | getIntegerLiteralConstant(Exp->getOperand(1))); |
| 766 | return; |
| 767 | case Instruction::PtrToInt: { |
| 768 | assert(TypeConverter.convertToIceType(Exp->getType()) == |
| 769 | TypeConverter.getIcePointerType()); |
| 770 | const auto GV = dyn_cast<GlobalValue>(Exp->getOperand(0)); |
| 771 | assert(GV); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 772 | const Ice::GlobalDeclaration *Addr = |
| 773 | getConverter().getGlobalDeclaration(GV); |
| 774 | Global.addInitializer( |
| 775 | new Ice::VariableDeclaration::RelocInitializer(Addr, Offset)); |
| 776 | return; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 777 | } |
| 778 | default: |
| 779 | break; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | std::string Buffer; |
| 784 | raw_string_ostream StrBuf(Buffer); |
| 785 | StrBuf << "Unhandled global initializer: " << Initializer; |
| 786 | report_fatal_error(StrBuf.str()); |
| 787 | } |
| 788 | |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 789 | } // end of anonymous namespace |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 790 | |
Karl Schimpf | b164d20 | 2014-07-11 10:26:34 -0700 | [diff] [blame] | 791 | namespace Ice { |
| 792 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 793 | void Converter::nameUnnamedGlobalVariables(Module *Mod) { |
| 794 | const IceString &GlobalPrefix = Flags.DefaultGlobalPrefix; |
| 795 | if (GlobalPrefix.empty()) |
| 796 | return; |
| 797 | uint32_t NameIndex = 0; |
| 798 | Ostream &errs = Ctx->getStrDump(); |
| 799 | for (auto V = Mod->global_begin(), E = Mod->global_end(); V != E; ++V) { |
| 800 | if (!V->hasName()) { |
| 801 | V->setName(createUnnamedName(GlobalPrefix, NameIndex)); |
| 802 | ++NameIndex; |
| 803 | } else { |
| 804 | checkIfUnnamedNameSafe(V->getName(), "global", GlobalPrefix, errs); |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | void Converter::nameUnnamedFunctions(Module *Mod) { |
| 810 | const IceString &FunctionPrefix = Flags.DefaultFunctionPrefix; |
| 811 | if (FunctionPrefix.empty()) |
| 812 | return; |
| 813 | uint32_t NameIndex = 0; |
| 814 | Ostream &errs = Ctx->getStrDump(); |
| 815 | for (Function &F : *Mod) { |
| 816 | if (!F.hasName()) { |
| 817 | F.setName(createUnnamedName(FunctionPrefix, NameIndex)); |
| 818 | ++NameIndex; |
| 819 | } else { |
| 820 | checkIfUnnamedNameSafe(F.getName(), "function", FunctionPrefix, errs); |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 825 | void Converter::convertToIce() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 826 | TimerMarker T(TimerStack::TT_convertToIce, Ctx); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 827 | nameUnnamedGlobalVariables(Mod); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 828 | nameUnnamedFunctions(Mod); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 829 | installGlobalDeclarations(Mod); |
Jim Stichnoth | 2a063e2 | 2014-10-08 11:24:51 -0700 | [diff] [blame] | 830 | convertGlobals(Mod); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 831 | convertFunctions(); |
Karl Schimpf | b164d20 | 2014-07-11 10:26:34 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 834 | GlobalDeclaration *Converter::getGlobalDeclaration(const GlobalValue *V) { |
| 835 | GlobalDeclarationMapType::const_iterator Pos = GlobalDeclarationMap.find(V); |
| 836 | if (Pos == GlobalDeclarationMap.end()) { |
| 837 | std::string Buffer; |
| 838 | raw_string_ostream StrBuf(Buffer); |
| 839 | StrBuf << "Can't find global declaration for: " << V->getName(); |
| 840 | report_fatal_error(StrBuf.str()); |
| 841 | } |
| 842 | return Pos->second; |
| 843 | } |
| 844 | |
| 845 | void Converter::installGlobalDeclarations(Module *Mod) { |
| 846 | const TypeConverter Converter(Mod->getContext()); |
| 847 | // Install function declarations. |
| 848 | for (const Function &Func : *Mod) { |
| 849 | FuncSigType Signature; |
| 850 | FunctionType *FuncType = Func.getFunctionType(); |
| 851 | Signature.setReturnType( |
| 852 | Converter.convertToIceType(FuncType->getReturnType())); |
| 853 | for (size_t I = 0; I < FuncType->getNumParams(); ++I) { |
| 854 | Signature.appendArgType( |
| 855 | Converter.convertToIceType(FuncType->getParamType(I))); |
| 856 | } |
| 857 | FunctionDeclaration *IceFunc = FunctionDeclaration::create( |
| 858 | Ctx, Signature, Func.getCallingConv(), Func.getLinkage(), Func.empty()); |
| 859 | IceFunc->setName(Func.getName()); |
| 860 | GlobalDeclarationMap[&Func] = IceFunc; |
| 861 | } |
| 862 | // Install global variable declarations. |
| 863 | for (Module::const_global_iterator I = Mod->global_begin(), |
| 864 | E = Mod->global_end(); |
| 865 | I != E; ++I) { |
| 866 | const GlobalVariable *GV = I; |
| 867 | VariableDeclaration *Var = VariableDeclaration::create(Ctx); |
| 868 | Var->setName(GV->getName()); |
| 869 | Var->setAlignment(GV->getAlignment()); |
| 870 | Var->setIsConstant(GV->isConstant()); |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 871 | Var->setLinkage(GV->getLinkage()); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 872 | GlobalDeclarationMap[GV] = Var; |
| 873 | } |
| 874 | } |
| 875 | |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 876 | void Converter::convertGlobals(Module *Mod) { |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 877 | LLVM2ICEGlobalsConverter GlobalsConverter(*this); |
| 878 | Translator::VariableDeclarationListType VariableDeclarations; |
| 879 | GlobalsConverter.convertGlobalsToIce(Mod, VariableDeclarations); |
| 880 | lowerGlobals(VariableDeclarations); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 883 | void Converter::convertFunctions() { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 884 | TimerStackIdT StackID = GlobalContext::TSK_Funcs; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 885 | for (const Function &I : *Mod) { |
| 886 | if (I.empty()) |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 887 | continue; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 888 | |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 889 | TimerIdT TimerID = 0; |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 890 | if (ALLOW_DUMP && Ctx->getFlags().TimeEachFunction) { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 891 | TimerID = Ctx->getTimerID(StackID, I.getName()); |
| 892 | Ctx->pushTimer(TimerID, StackID); |
| 893 | } |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 894 | LLVM2ICEFunctionConverter FunctionConverter(*this); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 895 | |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 896 | Cfg *Fcn = FunctionConverter.convertFunction(&I); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 897 | translateFcn(Fcn); |
Jim Stichnoth | 1c44d81 | 2014-12-08 14:57:52 -0800 | [diff] [blame] | 898 | if (ALLOW_DUMP && Ctx->getFlags().TimeEachFunction) |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 899 | Ctx->popTimer(TimerID, StackID); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 902 | emitConstants(); |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 903 | } |
Karl Schimpf | b164d20 | 2014-07-11 10:26:34 -0700 | [diff] [blame] | 904 | |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 905 | } // end of namespace Ice |