blob: 9f4c1d88e93f56042ed7838c0552514716d9cf8b [file] [log] [blame]
Karl Schimpfe1e013c2014-06-27 09:15:29 -07001//===- 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
14#include "IceConverter.h"
15
16#include "IceCfg.h"
17#include "IceCfgNode.h"
Karl Schimpf8d7abae2014-07-07 14:50:30 -070018#include "IceClFlags.h"
Karl Schimpfe1e013c2014-06-27 09:15:29 -070019#include "IceDefs.h"
20#include "IceGlobalContext.h"
21#include "IceInst.h"
22#include "IceOperand.h"
Karl Schimpfb164d202014-07-11 10:26:34 -070023#include "IceTargetLowering.h"
Karl Schimpfe1e013c2014-06-27 09:15:29 -070024#include "IceTypes.h"
25
26#include "llvm/IR/Constant.h"
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/DataLayout.h"
29#include "llvm/IR/Instruction.h"
30#include "llvm/IR/Instructions.h"
31#include "llvm/IR/LLVMContext.h"
32#include "llvm/IR/Module.h"
33
34#include <iostream>
35
36using namespace llvm;
37
38namespace {
39
40// Debugging helper
41template <typename T> static std::string LLVMObjectAsString(const T *O) {
42 std::string Dump;
43 raw_string_ostream Stream(Dump);
44 O->print(Stream);
45 return Stream.str();
46}
47
48// Converter from LLVM to ICE. The entry point is the convertFunction method.
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//
55class LLVM2ICEConverter {
56public:
57 LLVM2ICEConverter(Ice::GlobalContext *Ctx)
58 : Ctx(Ctx), Func(NULL), CurrentNode(NULL) {
59 // All PNaCl pointer widths are 32 bits because of the sandbox
60 // model.
61 SubzeroPointerType = Ice::IceType_i32;
62 }
63
64 // Caller is expected to delete the returned Ice::Cfg object.
65 Ice::Cfg *convertFunction(const Function *F) {
66 VarMap.clear();
67 NodeMap.clear();
68 Func = new Ice::Cfg(Ctx);
69 Func->setFunctionName(F->getName());
70 Func->setReturnType(convertType(F->getReturnType()));
71 Func->setInternal(F->hasInternalLinkage());
72
73 // The initial definition/use of each arg is the entry node.
74 CurrentNode = mapBasicBlockToNode(&F->getEntryBlock());
75 for (Function::const_arg_iterator ArgI = F->arg_begin(),
76 ArgE = F->arg_end();
77 ArgI != ArgE; ++ArgI) {
78 Func->addArg(mapValueToIceVar(ArgI));
79 }
80
81 // Make an initial pass through the block list just to resolve the
82 // blocks in the original linearized order. Otherwise the ICE
83 // linearized order will be affected by branch targets in
84 // terminator instructions.
85 for (Function::const_iterator BBI = F->begin(), BBE = F->end(); BBI != BBE;
86 ++BBI) {
87 mapBasicBlockToNode(BBI);
88 }
89 for (Function::const_iterator BBI = F->begin(), BBE = F->end(); BBI != BBE;
90 ++BBI) {
91 CurrentNode = mapBasicBlockToNode(BBI);
92 convertBasicBlock(BBI);
93 }
94 Func->setEntryNode(mapBasicBlockToNode(&F->getEntryBlock()));
95 Func->computePredecessors();
96
97 return Func;
98 }
99
100 // convertConstant() does not use Func or require it to be a valid
101 // Ice::Cfg pointer. As such, it's suitable for e.g. constructing
102 // global initializers.
103 Ice::Constant *convertConstant(const Constant *Const) {
104 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Const)) {
105 return Ctx->getConstantSym(convertType(GV->getType()), 0, GV->getName());
106 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(Const)) {
107 return Ctx->getConstantInt(convertIntegerType(CI->getType()),
108 CI->getZExtValue());
109 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Const)) {
110 Ice::Type Type = convertType(CFP->getType());
111 if (Type == Ice::IceType_f32)
112 return Ctx->getConstantFloat(CFP->getValueAPF().convertToFloat());
113 else if (Type == Ice::IceType_f64)
114 return Ctx->getConstantDouble(CFP->getValueAPF().convertToDouble());
115 llvm_unreachable("Unexpected floating point type");
116 return NULL;
117 } else if (const UndefValue *CU = dyn_cast<UndefValue>(Const)) {
118 return Ctx->getConstantUndef(convertType(CU->getType()));
119 } else {
120 llvm_unreachable("Unhandled constant type");
121 return NULL;
122 }
123 }
124
125private:
126 // LLVM values (instructions, etc.) are mapped directly to ICE variables.
127 // mapValueToIceVar has a version that forces an ICE type on the variable,
128 // and a version that just uses convertType on V.
129 Ice::Variable *mapValueToIceVar(const Value *V, Ice::Type IceTy) {
130 if (IceTy == Ice::IceType_void)
131 return NULL;
132 if (VarMap.find(V) == VarMap.end()) {
133 assert(CurrentNode);
134 VarMap[V] = Func->makeVariable(IceTy, CurrentNode, V->getName());
135 }
136 return VarMap[V];
137 }
138
139 Ice::Variable *mapValueToIceVar(const Value *V) {
140 return mapValueToIceVar(V, convertType(V->getType()));
141 }
142
143 Ice::CfgNode *mapBasicBlockToNode(const BasicBlock *BB) {
144 if (NodeMap.find(BB) == NodeMap.end()) {
145 NodeMap[BB] = Func->makeNode(BB->getName());
146 }
147 return NodeMap[BB];
148 }
149
150 Ice::Type convertIntegerType(const IntegerType *IntTy) const {
151 switch (IntTy->getBitWidth()) {
152 case 1:
153 return Ice::IceType_i1;
154 case 8:
155 return Ice::IceType_i8;
156 case 16:
157 return Ice::IceType_i16;
158 case 32:
159 return Ice::IceType_i32;
160 case 64:
161 return Ice::IceType_i64;
162 default:
163 report_fatal_error(std::string("Invalid PNaCl int type: ") +
164 LLVMObjectAsString(IntTy));
165 return Ice::IceType_void;
166 }
167 }
168
Matt Wala928f1292014-07-07 16:50:46 -0700169 Ice::Type convertVectorType(const VectorType *VecTy) const {
170 unsigned NumElements = VecTy->getNumElements();
171 const Type *ElementType = VecTy->getElementType();
172
173 if (ElementType->isFloatTy()) {
174 if (NumElements == 4)
175 return Ice::IceType_v4f32;
176 } else if (ElementType->isIntegerTy()) {
177 switch (cast<IntegerType>(ElementType)->getBitWidth()) {
178 case 1:
179 if (NumElements == 4)
180 return Ice::IceType_v4i1;
181 if (NumElements == 8)
182 return Ice::IceType_v8i1;
183 if (NumElements == 16)
184 return Ice::IceType_v16i1;
185 break;
186 case 8:
187 if (NumElements == 16)
188 return Ice::IceType_v16i8;
189 break;
190 case 16:
191 if (NumElements == 8)
192 return Ice::IceType_v8i16;
193 break;
194 case 32:
195 if (NumElements == 4)
196 return Ice::IceType_v4i32;
197 break;
198 }
199 }
200
201 report_fatal_error(std::string("Unhandled vector type: ") +
202 LLVMObjectAsString(VecTy));
203 return Ice::IceType_void;
204 }
205
Karl Schimpfe1e013c2014-06-27 09:15:29 -0700206 Ice::Type convertType(const Type *Ty) const {
207 switch (Ty->getTypeID()) {
208 case Type::VoidTyID:
209 return Ice::IceType_void;
210 case Type::IntegerTyID:
211 return convertIntegerType(cast<IntegerType>(Ty));
212 case Type::FloatTyID:
213 return Ice::IceType_f32;
214 case Type::DoubleTyID:
215 return Ice::IceType_f64;
216 case Type::PointerTyID:
217 return SubzeroPointerType;
218 case Type::FunctionTyID:
219 return SubzeroPointerType;
Matt Wala928f1292014-07-07 16:50:46 -0700220 case Type::VectorTyID:
221 return convertVectorType(cast<VectorType>(Ty));
Karl Schimpfe1e013c2014-06-27 09:15:29 -0700222 default:
223 report_fatal_error(std::string("Invalid PNaCl type: ") +
224 LLVMObjectAsString(Ty));
225 }
226
227 llvm_unreachable("convertType");
228 return Ice::IceType_void;
229 }
230
231 // Given an LLVM instruction and an operand number, produce the
232 // Ice::Operand this refers to. If there's no such operand, return
233 // NULL.
234 Ice::Operand *convertOperand(const Instruction *Inst, unsigned OpNum) {
235 if (OpNum >= Inst->getNumOperands()) {
236 return NULL;
237 }
238 const Value *Op = Inst->getOperand(OpNum);
239 return convertValue(Op);
240 }
241
242 Ice::Operand *convertValue(const Value *Op) {
243 if (const Constant *Const = dyn_cast<Constant>(Op)) {
244 return convertConstant(Const);
245 } else {
246 return mapValueToIceVar(Op);
247 }
248 }
249
250 // Note: this currently assumes a 1x1 mapping between LLVM IR and Ice
251 // instructions.
252 Ice::Inst *convertInstruction(const Instruction *Inst) {
253 switch (Inst->getOpcode()) {
254 case Instruction::PHI:
255 return convertPHINodeInstruction(cast<PHINode>(Inst));
256 case Instruction::Br:
257 return convertBrInstruction(cast<BranchInst>(Inst));
258 case Instruction::Ret:
259 return convertRetInstruction(cast<ReturnInst>(Inst));
260 case Instruction::IntToPtr:
261 return convertIntToPtrInstruction(cast<IntToPtrInst>(Inst));
262 case Instruction::PtrToInt:
263 return convertPtrToIntInstruction(cast<PtrToIntInst>(Inst));
264 case Instruction::ICmp:
265 return convertICmpInstruction(cast<ICmpInst>(Inst));
266 case Instruction::FCmp:
267 return convertFCmpInstruction(cast<FCmpInst>(Inst));
268 case Instruction::Select:
269 return convertSelectInstruction(cast<SelectInst>(Inst));
270 case Instruction::Switch:
271 return convertSwitchInstruction(cast<SwitchInst>(Inst));
272 case Instruction::Load:
273 return convertLoadInstruction(cast<LoadInst>(Inst));
274 case Instruction::Store:
275 return convertStoreInstruction(cast<StoreInst>(Inst));
276 case Instruction::ZExt:
277 return convertCastInstruction(cast<ZExtInst>(Inst), Ice::InstCast::Zext);
278 case Instruction::SExt:
279 return convertCastInstruction(cast<SExtInst>(Inst), Ice::InstCast::Sext);
280 case Instruction::Trunc:
281 return convertCastInstruction(cast<TruncInst>(Inst),
282 Ice::InstCast::Trunc);
283 case Instruction::FPTrunc:
284 return convertCastInstruction(cast<FPTruncInst>(Inst),
285 Ice::InstCast::Fptrunc);
286 case Instruction::FPExt:
287 return convertCastInstruction(cast<FPExtInst>(Inst),
288 Ice::InstCast::Fpext);
289 case Instruction::FPToSI:
290 return convertCastInstruction(cast<FPToSIInst>(Inst),
291 Ice::InstCast::Fptosi);
292 case Instruction::FPToUI:
293 return convertCastInstruction(cast<FPToUIInst>(Inst),
294 Ice::InstCast::Fptoui);
295 case Instruction::SIToFP:
296 return convertCastInstruction(cast<SIToFPInst>(Inst),
297 Ice::InstCast::Sitofp);
298 case Instruction::UIToFP:
299 return convertCastInstruction(cast<UIToFPInst>(Inst),
300 Ice::InstCast::Uitofp);
301 case Instruction::BitCast:
302 return convertCastInstruction(cast<BitCastInst>(Inst),
303 Ice::InstCast::Bitcast);
304 case Instruction::Add:
305 return convertArithInstruction(Inst, Ice::InstArithmetic::Add);
306 case Instruction::Sub:
307 return convertArithInstruction(Inst, Ice::InstArithmetic::Sub);
308 case Instruction::Mul:
309 return convertArithInstruction(Inst, Ice::InstArithmetic::Mul);
310 case Instruction::UDiv:
311 return convertArithInstruction(Inst, Ice::InstArithmetic::Udiv);
312 case Instruction::SDiv:
313 return convertArithInstruction(Inst, Ice::InstArithmetic::Sdiv);
314 case Instruction::URem:
315 return convertArithInstruction(Inst, Ice::InstArithmetic::Urem);
316 case Instruction::SRem:
317 return convertArithInstruction(Inst, Ice::InstArithmetic::Srem);
318 case Instruction::Shl:
319 return convertArithInstruction(Inst, Ice::InstArithmetic::Shl);
320 case Instruction::LShr:
321 return convertArithInstruction(Inst, Ice::InstArithmetic::Lshr);
322 case Instruction::AShr:
323 return convertArithInstruction(Inst, Ice::InstArithmetic::Ashr);
324 case Instruction::FAdd:
325 return convertArithInstruction(Inst, Ice::InstArithmetic::Fadd);
326 case Instruction::FSub:
327 return convertArithInstruction(Inst, Ice::InstArithmetic::Fsub);
328 case Instruction::FMul:
329 return convertArithInstruction(Inst, Ice::InstArithmetic::Fmul);
330 case Instruction::FDiv:
331 return convertArithInstruction(Inst, Ice::InstArithmetic::Fdiv);
332 case Instruction::FRem:
333 return convertArithInstruction(Inst, Ice::InstArithmetic::Frem);
334 case Instruction::And:
335 return convertArithInstruction(Inst, Ice::InstArithmetic::And);
336 case Instruction::Or:
337 return convertArithInstruction(Inst, Ice::InstArithmetic::Or);
338 case Instruction::Xor:
339 return convertArithInstruction(Inst, Ice::InstArithmetic::Xor);
Matt Wala49889232014-07-18 12:45:09 -0700340 case Instruction::ExtractElement:
341 return convertExtractElementInstruction(cast<ExtractElementInst>(Inst));
342 case Instruction::InsertElement:
343 return convertInsertElementInstruction(cast<InsertElementInst>(Inst));
Karl Schimpfe1e013c2014-06-27 09:15:29 -0700344 case Instruction::Call:
345 return convertCallInstruction(cast<CallInst>(Inst));
346 case Instruction::Alloca:
347 return convertAllocaInstruction(cast<AllocaInst>(Inst));
348 case Instruction::Unreachable:
349 return convertUnreachableInstruction(cast<UnreachableInst>(Inst));
350 default:
351 report_fatal_error(std::string("Invalid PNaCl instruction: ") +
352 LLVMObjectAsString(Inst));
353 }
354
355 llvm_unreachable("convertInstruction");
356 return NULL;
357 }
358
359 Ice::Inst *convertLoadInstruction(const LoadInst *Inst) {
360 Ice::Operand *Src = convertOperand(Inst, 0);
361 Ice::Variable *Dest = mapValueToIceVar(Inst);
362 return Ice::InstLoad::create(Func, Dest, Src);
363 }
364
365 Ice::Inst *convertStoreInstruction(const StoreInst *Inst) {
366 Ice::Operand *Addr = convertOperand(Inst, 1);
367 Ice::Operand *Val = convertOperand(Inst, 0);
368 return Ice::InstStore::create(Func, Val, Addr);
369 }
370
371 Ice::Inst *convertArithInstruction(const Instruction *Inst,
372 Ice::InstArithmetic::OpKind Opcode) {
373 const BinaryOperator *BinOp = cast<BinaryOperator>(Inst);
374 Ice::Operand *Src0 = convertOperand(Inst, 0);
375 Ice::Operand *Src1 = convertOperand(Inst, 1);
376 Ice::Variable *Dest = mapValueToIceVar(BinOp);
377 return Ice::InstArithmetic::create(Func, Opcode, Dest, Src0, Src1);
378 }
379
380 Ice::Inst *convertPHINodeInstruction(const PHINode *Inst) {
381 unsigned NumValues = Inst->getNumIncomingValues();
382 Ice::InstPhi *IcePhi =
383 Ice::InstPhi::create(Func, NumValues, mapValueToIceVar(Inst));
384 for (unsigned N = 0, E = NumValues; N != E; ++N) {
385 IcePhi->addArgument(convertOperand(Inst, N),
386 mapBasicBlockToNode(Inst->getIncomingBlock(N)));
387 }
388 return IcePhi;
389 }
390
391 Ice::Inst *convertBrInstruction(const BranchInst *Inst) {
392 if (Inst->isConditional()) {
393 Ice::Operand *Src = convertOperand(Inst, 0);
394 BasicBlock *BBThen = Inst->getSuccessor(0);
395 BasicBlock *BBElse = Inst->getSuccessor(1);
396 Ice::CfgNode *NodeThen = mapBasicBlockToNode(BBThen);
397 Ice::CfgNode *NodeElse = mapBasicBlockToNode(BBElse);
398 return Ice::InstBr::create(Func, Src, NodeThen, NodeElse);
399 } else {
400 BasicBlock *BBSucc = Inst->getSuccessor(0);
401 return Ice::InstBr::create(Func, mapBasicBlockToNode(BBSucc));
402 }
403 }
404
405 Ice::Inst *convertIntToPtrInstruction(const IntToPtrInst *Inst) {
406 Ice::Operand *Src = convertOperand(Inst, 0);
407 Ice::Variable *Dest = mapValueToIceVar(Inst, SubzeroPointerType);
408 return Ice::InstAssign::create(Func, Dest, Src);
409 }
410
411 Ice::Inst *convertPtrToIntInstruction(const PtrToIntInst *Inst) {
412 Ice::Operand *Src = convertOperand(Inst, 0);
413 Ice::Variable *Dest = mapValueToIceVar(Inst);
414 return Ice::InstAssign::create(Func, Dest, Src);
415 }
416
417 Ice::Inst *convertRetInstruction(const ReturnInst *Inst) {
418 Ice::Operand *RetOperand = convertOperand(Inst, 0);
419 if (RetOperand) {
420 return Ice::InstRet::create(Func, RetOperand);
421 } else {
422 return Ice::InstRet::create(Func);
423 }
424 }
425
426 Ice::Inst *convertCastInstruction(const Instruction *Inst,
427 Ice::InstCast::OpKind CastKind) {
428 Ice::Operand *Src = convertOperand(Inst, 0);
429 Ice::Variable *Dest = mapValueToIceVar(Inst);
430 return Ice::InstCast::create(Func, CastKind, Dest, Src);
431 }
432
433 Ice::Inst *convertICmpInstruction(const ICmpInst *Inst) {
434 Ice::Operand *Src0 = convertOperand(Inst, 0);
435 Ice::Operand *Src1 = convertOperand(Inst, 1);
436 Ice::Variable *Dest = mapValueToIceVar(Inst);
437
438 Ice::InstIcmp::ICond Cond;
439 switch (Inst->getPredicate()) {
440 default:
441 llvm_unreachable("ICmpInst predicate");
442 case CmpInst::ICMP_EQ:
443 Cond = Ice::InstIcmp::Eq;
444 break;
445 case CmpInst::ICMP_NE:
446 Cond = Ice::InstIcmp::Ne;
447 break;
448 case CmpInst::ICMP_UGT:
449 Cond = Ice::InstIcmp::Ugt;
450 break;
451 case CmpInst::ICMP_UGE:
452 Cond = Ice::InstIcmp::Uge;
453 break;
454 case CmpInst::ICMP_ULT:
455 Cond = Ice::InstIcmp::Ult;
456 break;
457 case CmpInst::ICMP_ULE:
458 Cond = Ice::InstIcmp::Ule;
459 break;
460 case CmpInst::ICMP_SGT:
461 Cond = Ice::InstIcmp::Sgt;
462 break;
463 case CmpInst::ICMP_SGE:
464 Cond = Ice::InstIcmp::Sge;
465 break;
466 case CmpInst::ICMP_SLT:
467 Cond = Ice::InstIcmp::Slt;
468 break;
469 case CmpInst::ICMP_SLE:
470 Cond = Ice::InstIcmp::Sle;
471 break;
472 }
473
474 return Ice::InstIcmp::create(Func, Cond, Dest, Src0, Src1);
475 }
476
477 Ice::Inst *convertFCmpInstruction(const FCmpInst *Inst) {
478 Ice::Operand *Src0 = convertOperand(Inst, 0);
479 Ice::Operand *Src1 = convertOperand(Inst, 1);
480 Ice::Variable *Dest = mapValueToIceVar(Inst);
481
482 Ice::InstFcmp::FCond Cond;
483 switch (Inst->getPredicate()) {
484
485 default:
486 llvm_unreachable("FCmpInst predicate");
487
488 case CmpInst::FCMP_FALSE:
489 Cond = Ice::InstFcmp::False;
490 break;
491 case CmpInst::FCMP_OEQ:
492 Cond = Ice::InstFcmp::Oeq;
493 break;
494 case CmpInst::FCMP_OGT:
495 Cond = Ice::InstFcmp::Ogt;
496 break;
497 case CmpInst::FCMP_OGE:
498 Cond = Ice::InstFcmp::Oge;
499 break;
500 case CmpInst::FCMP_OLT:
501 Cond = Ice::InstFcmp::Olt;
502 break;
503 case CmpInst::FCMP_OLE:
504 Cond = Ice::InstFcmp::Ole;
505 break;
506 case CmpInst::FCMP_ONE:
507 Cond = Ice::InstFcmp::One;
508 break;
509 case CmpInst::FCMP_ORD:
510 Cond = Ice::InstFcmp::Ord;
511 break;
512 case CmpInst::FCMP_UEQ:
513 Cond = Ice::InstFcmp::Ueq;
514 break;
515 case CmpInst::FCMP_UGT:
516 Cond = Ice::InstFcmp::Ugt;
517 break;
518 case CmpInst::FCMP_UGE:
519 Cond = Ice::InstFcmp::Uge;
520 break;
521 case CmpInst::FCMP_ULT:
522 Cond = Ice::InstFcmp::Ult;
523 break;
524 case CmpInst::FCMP_ULE:
525 Cond = Ice::InstFcmp::Ule;
526 break;
527 case CmpInst::FCMP_UNE:
528 Cond = Ice::InstFcmp::Une;
529 break;
530 case CmpInst::FCMP_UNO:
531 Cond = Ice::InstFcmp::Uno;
532 break;
533 case CmpInst::FCMP_TRUE:
534 Cond = Ice::InstFcmp::True;
535 break;
536 }
537
538 return Ice::InstFcmp::create(Func, Cond, Dest, Src0, Src1);
539 }
540
Matt Wala49889232014-07-18 12:45:09 -0700541 Ice::Inst *convertExtractElementInstruction(const ExtractElementInst *Inst) {
542 Ice::Variable *Dest = mapValueToIceVar(Inst);
543 Ice::Operand *Source1 = convertValue(Inst->getOperand(0));
544 Ice::Operand *Source2 = convertValue(Inst->getOperand(1));
545 return Ice::InstExtractElement::create(Func, Dest, Source1, Source2);
546 }
547
548 Ice::Inst *convertInsertElementInstruction(const InsertElementInst *Inst) {
549 Ice::Variable *Dest = mapValueToIceVar(Inst);
550 Ice::Operand *Source1 = convertValue(Inst->getOperand(0));
551 Ice::Operand *Source2 = convertValue(Inst->getOperand(1));
552 Ice::Operand *Source3 = convertValue(Inst->getOperand(2));
553 return Ice::InstInsertElement::create(Func, Dest, Source1, Source2,
554 Source3);
555 }
556
Karl Schimpfe1e013c2014-06-27 09:15:29 -0700557 Ice::Inst *convertSelectInstruction(const SelectInst *Inst) {
558 Ice::Variable *Dest = mapValueToIceVar(Inst);
559 Ice::Operand *Cond = convertValue(Inst->getCondition());
560 Ice::Operand *Source1 = convertValue(Inst->getTrueValue());
561 Ice::Operand *Source2 = convertValue(Inst->getFalseValue());
562 return Ice::InstSelect::create(Func, Dest, Cond, Source1, Source2);
563 }
564
565 Ice::Inst *convertSwitchInstruction(const SwitchInst *Inst) {
566 Ice::Operand *Source = convertValue(Inst->getCondition());
567 Ice::CfgNode *LabelDefault = mapBasicBlockToNode(Inst->getDefaultDest());
568 unsigned NumCases = Inst->getNumCases();
569 Ice::InstSwitch *Switch =
570 Ice::InstSwitch::create(Func, NumCases, Source, LabelDefault);
571 unsigned CurrentCase = 0;
572 for (SwitchInst::ConstCaseIt I = Inst->case_begin(), E = Inst->case_end();
573 I != E; ++I, ++CurrentCase) {
574 uint64_t CaseValue = I.getCaseValue()->getZExtValue();
575 Ice::CfgNode *CaseSuccessor = mapBasicBlockToNode(I.getCaseSuccessor());
576 Switch->addBranch(CurrentCase, CaseValue, CaseSuccessor);
577 }
578 return Switch;
579 }
580
581 Ice::Inst *convertCallInstruction(const CallInst *Inst) {
582 Ice::Variable *Dest = mapValueToIceVar(Inst);
583 Ice::Operand *CallTarget = convertValue(Inst->getCalledValue());
584 unsigned NumArgs = Inst->getNumArgOperands();
585 // Note: Subzero doesn't (yet) do anything special with the Tail
586 // flag in the bitcode, i.e. CallInst::isTailCall().
587 Ice::InstCall *NewInst = NULL;
588 const Ice::Intrinsics::FullIntrinsicInfo *Info = NULL;
589
590 if (Ice::ConstantRelocatable *Target =
591 llvm::dyn_cast<Ice::ConstantRelocatable>(CallTarget)) {
592 // Check if this direct call is to an Intrinsic (starts with "llvm.")
593 static const char LLVMPrefix[] = "llvm.";
594 const size_t LLVMPrefixLen = strlen(LLVMPrefix);
595 Ice::IceString Name = Target->getName();
596 if (Name.substr(0, LLVMPrefixLen) == LLVMPrefix) {
597 Ice::IceString NameSuffix = Name.substr(LLVMPrefixLen);
598 Info = Ctx->getIntrinsicsInfo().find(NameSuffix);
599 if (!Info) {
600 report_fatal_error(std::string("Invalid PNaCl intrinsic call: ") +
601 LLVMObjectAsString(Inst));
602 }
603 NewInst = Ice::InstIntrinsicCall::create(Func, NumArgs, Dest,
604 CallTarget, Info->Info);
605 }
606 }
607
608 // Not an intrinsic call.
609 if (NewInst == NULL) {
610 NewInst = Ice::InstCall::create(Func, NumArgs, Dest, CallTarget);
611 }
612 for (unsigned i = 0; i < NumArgs; ++i) {
613 NewInst->addArg(convertOperand(Inst, i));
614 }
615 if (Info) {
616 validateIntrinsicCall(NewInst, Info);
617 }
618 return NewInst;
619 }
620
621 Ice::Inst *convertAllocaInstruction(const AllocaInst *Inst) {
622 // PNaCl bitcode only contains allocas of byte-granular objects.
623 Ice::Operand *ByteCount = convertValue(Inst->getArraySize());
624 uint32_t Align = Inst->getAlignment();
625 Ice::Variable *Dest = mapValueToIceVar(Inst, SubzeroPointerType);
626
627 return Ice::InstAlloca::create(Func, ByteCount, Align, Dest);
628 }
629
630 Ice::Inst *convertUnreachableInstruction(const UnreachableInst * /*Inst*/) {
631 return Ice::InstUnreachable::create(Func);
632 }
633
634 Ice::CfgNode *convertBasicBlock(const BasicBlock *BB) {
635 Ice::CfgNode *Node = mapBasicBlockToNode(BB);
636 for (BasicBlock::const_iterator II = BB->begin(), II_e = BB->end();
637 II != II_e; ++II) {
638 Ice::Inst *Inst = convertInstruction(II);
639 Node->appendInst(Inst);
640 }
641 return Node;
642 }
643
644 void validateIntrinsicCall(const Ice::InstCall *Call,
645 const Ice::Intrinsics::FullIntrinsicInfo *I) {
646 assert(I->NumTypes >= 1);
647 if (I->Signature[0] == Ice::IceType_void) {
648 if (Call->getDest() != NULL) {
649 report_fatal_error(
650 "Return value for intrinsic func w/ void return type.");
651 }
652 } else {
653 if (I->Signature[0] != Call->getDest()->getType()) {
654 report_fatal_error("Mismatched return types.");
655 }
656 }
657 if (Call->getNumArgs() + 1 != I->NumTypes) {
658 std::cerr << "Call->getNumArgs() " << (int)Call->getNumArgs()
659 << " I->NumTypes " << (int)I->NumTypes << "\n";
660 report_fatal_error("Mismatched # of args.");
661 }
662 for (size_t i = 1; i < I->NumTypes; ++i) {
663 if (Call->getArg(i - 1)->getType() != I->Signature[i]) {
664 report_fatal_error("Mismatched argument type.");
665 }
666 }
667 }
668
669private:
670 // Data
671 Ice::GlobalContext *Ctx;
672 Ice::Cfg *Func;
673 Ice::CfgNode *CurrentNode;
674 Ice::Type SubzeroPointerType;
675 std::map<const Value *, Ice::Variable *> VarMap;
676 std::map<const BasicBlock *, Ice::CfgNode *> NodeMap;
677};
678
Karl Schimpf8d7abae2014-07-07 14:50:30 -0700679} // end of anonymous namespace.
Karl Schimpfe1e013c2014-06-27 09:15:29 -0700680
Karl Schimpfb164d202014-07-11 10:26:34 -0700681namespace Ice {
682
683void Converter::convertToIce(Module *Mod) {
684 convertGlobals(Mod);
685 convertFunctions(Mod);
686}
687
688void Converter::convertGlobals(Module *Mod) {
689 OwningPtr<TargetGlobalInitLowering> GlobalLowering(
690 TargetGlobalInitLowering::createLowering(Ctx->getTargetArch(), Ctx));
691 for (Module::const_global_iterator I = Mod->global_begin(),
692 E = Mod->global_end();
693 I != E; ++I) {
694 if (!I->hasInitializer())
695 continue;
696 const llvm::Constant *Initializer = I->getInitializer();
697 IceString Name = I->getName();
698 unsigned Align = I->getAlignment();
699 uint64_t NumElements = 0;
700 const char *Data = NULL;
701 bool IsInternal = I->hasInternalLinkage();
702 bool IsConst = I->isConstant();
703 bool IsZeroInitializer = false;
704
705 if (const ConstantDataArray *CDA =
706 dyn_cast<ConstantDataArray>(Initializer)) {
707 NumElements = CDA->getNumElements();
708 assert(isa<IntegerType>(CDA->getElementType()) &&
709 cast<IntegerType>(CDA->getElementType())->getBitWidth() == 8);
710 Data = CDA->getRawDataValues().data();
711 } else if (isa<ConstantAggregateZero>(Initializer)) {
712 if (const ArrayType *AT = dyn_cast<ArrayType>(Initializer->getType())) {
713 assert(isa<IntegerType>(AT->getElementType()) &&
714 cast<IntegerType>(AT->getElementType())->getBitWidth() == 8);
715 NumElements = AT->getNumElements();
716 IsZeroInitializer = true;
717 } else {
718 llvm_unreachable("Unhandled constant aggregate zero type");
719 }
720 } else {
721 llvm_unreachable("Unhandled global initializer");
722 }
723
724 GlobalLowering->lower(Name, Align, IsInternal, IsConst, IsZeroInitializer,
725 NumElements, Data, Flags.DisableTranslation);
726 }
727 GlobalLowering.reset();
728}
729
730void Converter::convertFunctions(Module *Mod) {
Karl Schimpfe1e013c2014-06-27 09:15:29 -0700731 for (Module::const_iterator I = Mod->begin(), E = Mod->end(); I != E; ++I) {
732 if (I->empty())
733 continue;
734 LLVM2ICEConverter FunctionConverter(Ctx);
735
Karl Schimpfb164d202014-07-11 10:26:34 -0700736 Timer TConvert;
737 Cfg *Fcn = FunctionConverter.convertFunction(I);
Karl Schimpf8d7abae2014-07-07 14:50:30 -0700738 if (Flags.SubzeroTimingEnabled) {
Karl Schimpfe1e013c2014-06-27 09:15:29 -0700739 std::cerr << "[Subzero timing] Convert function "
Karl Schimpf8d7abae2014-07-07 14:50:30 -0700740 << Fcn->getFunctionName() << ": " << TConvert.getElapsedSec()
Karl Schimpfe1e013c2014-06-27 09:15:29 -0700741 << " sec\n";
742 }
Karl Schimpf8d7abae2014-07-07 14:50:30 -0700743 translateFcn(Fcn);
Karl Schimpfe1e013c2014-06-27 09:15:29 -0700744 }
745
Karl Schimpf8d7abae2014-07-07 14:50:30 -0700746 emitConstants();
Karl Schimpfe1e013c2014-06-27 09:15:29 -0700747}
Karl Schimpfb164d202014-07-11 10:26:34 -0700748
749} // end of Ice namespace.