blob: 76f98ff1e5a60f71eef2d02bc19596127eb509d0 [file] [log] [blame]
Quentin Colombet105cf2b2016-01-20 20:58:56 +00001//===-- llvm/CodeGen/GlobalISel/IRTranslator.cpp - IRTranslator --*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file implements the IRTranslator class.
11//===----------------------------------------------------------------------===//
12
13#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
14
Quentin Colombet17c494b2016-02-11 17:51:31 +000015#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000016#include "llvm/CodeGen/MachineFunction.h"
Quentin Colombet17c494b2016-02-11 17:51:31 +000017#include "llvm/CodeGen/MachineRegisterInfo.h"
18#include "llvm/IR/Constant.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000019#include "llvm/IR/Function.h"
Quentin Colombet17c494b2016-02-11 17:51:31 +000020#include "llvm/IR/Type.h"
21#include "llvm/IR/Value.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000022
23#define DEBUG_TYPE "irtranslator"
24
Quentin Colombet105cf2b2016-01-20 20:58:56 +000025using namespace llvm;
26
27char IRTranslator::ID = 0;
28
Quentin Colombet17c494b2016-02-11 17:51:31 +000029const VRegsSequence &IRTranslator::getOrCreateVRegs(const Value *Val) {
30 VRegsSequence &ValRegSequence = ValToVRegs[Val];
31 // Check if this is the first time we see Val.
32 if (ValRegSequence.empty()) {
33 // Fill ValRegsSequence with the sequence of registers
34 // we need to concat together to produce the value.
35 assert(Val->getType()->isSized() &&
36 "Don't know how to create an empty vreg");
37 assert(!Val->getType()->isAggregateType() && "Not yet implemented");
38 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
39 unsigned VReg = MRI->createGenericVirtualRegister(Size);
40 ValRegSequence.push_back(VReg);
Quentin Colombet4f0ec8d2016-02-11 17:52:28 +000041 assert(!isa<Constant>(Val) && "Not yet implemented");
Quentin Colombet17c494b2016-02-11 17:51:31 +000042 }
43 assert(ValRegSequence.size() == 1 &&
44 "We support only one vreg per value at the moment");
45 return ValRegSequence;
46}
47
48MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock *BB) {
49 MachineBasicBlock *&MBB = BBToMBB[BB];
50 if (!MBB) {
51 MachineFunction &MF = MIRBuilder->getMF();
52 MBB = MF.CreateMachineBasicBlock();
53 MF.push_back(MBB);
54 }
55 return *MBB;
56}
57
Quentin Colombet105cf2b2016-01-20 20:58:56 +000058bool IRTranslator::translateADD(const Instruction &Inst) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000059 // Get or create a virtual register for each value.
60 // Unless the value is a Constant => loadimm cst?
61 // or inline constant each time?
62 // Creation of a virtual register needs to have a size.
Quentin Colombet17c494b2016-02-11 17:51:31 +000063 unsigned Op0 = *getOrCreateVRegs(Inst.getOperand(0)).begin();
64 unsigned Op1 = *getOrCreateVRegs(Inst.getOperand(1)).begin();
65 unsigned Res = *getOrCreateVRegs(&Inst).begin();
66 MIRBuilder->buildInstr(TargetOpcode::G_ADD, Res, Op0, Op1);
67 return true;
Quentin Colombet105cf2b2016-01-20 20:58:56 +000068}
69
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000070bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombet17c494b2016-02-11 17:51:31 +000071 MIRBuilder->setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000072 switch(Inst.getOpcode()) {
73 case Instruction::Add: {
74 return translateADD(Inst);
75 default:
76 llvm_unreachable("Opcode not supported");
77 }
78 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +000079}
80
81
82void IRTranslator::finalize() {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000083 // Release the memory used by the different maps we
84 // needed during the translation.
85 ValToVRegs.clear();
86 Constants.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +000087}
88
89IRTranslator::IRTranslator()
90 : MachineFunctionPass(ID) {
91}
92
93bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000094 const Function &F = *MF.getFunction();
Quentin Colombet17c494b2016-02-11 17:51:31 +000095 MIRBuilder->setFunction(MF);
96 MRI = &MF.getRegInfo();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000097 for (const BasicBlock &BB: F) {
Quentin Colombet17c494b2016-02-11 17:51:31 +000098 MachineBasicBlock &MBB = getOrCreateBB(&BB);
99 MIRBuilder->setBasicBlock(MBB);
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000100 for (const Instruction &Inst: BB) {
101 bool Succeeded = translate(Inst);
102 if (!Succeeded) {
103 DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
104 report_fatal_error("Unable to translate instruction");
105 }
106 }
107 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000108 return false;
109}