blob: 8b93f4523f2e23ae7495b7a1fadbf2d44296bbbf [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 Colombetfd9d0a02016-02-11 19:59:41 +000015#include "llvm/ADT/SmallVector.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 Colombet74d7d2f2016-02-11 18:53:28 +000022#include "llvm/Target/TargetLowering.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000023
24#define DEBUG_TYPE "irtranslator"
25
Quentin Colombet105cf2b2016-01-20 20:58:56 +000026using namespace llvm;
27
28char IRTranslator::ID = 0;
29
Quentin Colombeta7fae162016-02-11 17:53:23 +000030IRTranslator::IRTranslator() : MachineFunctionPass(ID), MRI(nullptr) {
31}
32
Quentin Colombet17c494b2016-02-11 17:51:31 +000033const VRegsSequence &IRTranslator::getOrCreateVRegs(const Value *Val) {
34 VRegsSequence &ValRegSequence = ValToVRegs[Val];
35 // Check if this is the first time we see Val.
36 if (ValRegSequence.empty()) {
37 // Fill ValRegsSequence with the sequence of registers
38 // we need to concat together to produce the value.
39 assert(Val->getType()->isSized() &&
40 "Don't know how to create an empty vreg");
41 assert(!Val->getType()->isAggregateType() && "Not yet implemented");
42 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
43 unsigned VReg = MRI->createGenericVirtualRegister(Size);
44 ValRegSequence.push_back(VReg);
Quentin Colombet4f0ec8d2016-02-11 17:52:28 +000045 assert(!isa<Constant>(Val) && "Not yet implemented");
Quentin Colombet17c494b2016-02-11 17:51:31 +000046 }
47 assert(ValRegSequence.size() == 1 &&
48 "We support only one vreg per value at the moment");
49 return ValRegSequence;
50}
51
52MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock *BB) {
53 MachineBasicBlock *&MBB = BBToMBB[BB];
54 if (!MBB) {
Quentin Colombeta7fae162016-02-11 17:53:23 +000055 MachineFunction &MF = MIRBuilder.getMF();
Quentin Colombet17c494b2016-02-11 17:51:31 +000056 MBB = MF.CreateMachineBasicBlock();
57 MF.push_back(MBB);
58 }
59 return *MBB;
60}
61
Quentin Colombet105cf2b2016-01-20 20:58:56 +000062bool IRTranslator::translateADD(const Instruction &Inst) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000063 // Get or create a virtual register for each value.
64 // Unless the value is a Constant => loadimm cst?
65 // or inline constant each time?
66 // Creation of a virtual register needs to have a size.
Quentin Colombet17c494b2016-02-11 17:51:31 +000067 unsigned Op0 = *getOrCreateVRegs(Inst.getOperand(0)).begin();
68 unsigned Op1 = *getOrCreateVRegs(Inst.getOperand(1)).begin();
69 unsigned Res = *getOrCreateVRegs(&Inst).begin();
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000070 MIRBuilder.buildInstr(TargetOpcode::G_ADD, Inst.getType(), Res, Op0, Op1);
Quentin Colombet17c494b2016-02-11 17:51:31 +000071 return true;
Quentin Colombet105cf2b2016-01-20 20:58:56 +000072}
73
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000074bool IRTranslator::translateReturn(const Instruction &Inst) {
75 assert(isa<ReturnInst>(Inst) && "Return expected");
76 const Value *Ret = cast<ReturnInst>(Inst).getReturnValue();
77 // The target may mess up with the insertion point, but
78 // this is not important as a return is the last instruction
79 // of the block anyway.
80 return TLI->LowerReturn(MIRBuilder, Ret,
81 !Ret ? 0 : *getOrCreateVRegs(Ret).begin());
82}
83
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000084bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombeta7fae162016-02-11 17:53:23 +000085 MIRBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000086 switch(Inst.getOpcode()) {
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000087 case Instruction::Add:
88 return translateADD(Inst);
89 case Instruction::Ret:
90 return translateReturn(Inst);
91
92 default:
93 llvm_unreachable("Opcode not supported");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000094 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +000095}
96
97
98void IRTranslator::finalize() {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000099 // Release the memory used by the different maps we
100 // needed during the translation.
101 ValToVRegs.clear();
102 Constants.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000103}
104
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000105bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000106 const Function &F = *MF.getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000107 if (F.empty())
108 return false;
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000109 TLI = MF.getSubtarget().getTargetLowering();
Quentin Colombeta7fae162016-02-11 17:53:23 +0000110 MIRBuilder.setFunction(MF);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000111 MRI = &MF.getRegInfo();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000112 // Setup the arguments.
113 MachineBasicBlock &MBB = getOrCreateBB(&F.front());
114 MIRBuilder.setBasicBlock(MBB);
115 SmallVector<unsigned, 8> VRegArgs;
116 for (const Argument &Arg: F.args())
117 VRegArgs.push_back(*getOrCreateVRegs(&Arg).begin());
118 bool Succeeded = TLI->LowerFormalArguments(MIRBuilder, F.getArgumentList(),
119 VRegArgs);
120 if (!Succeeded)
121 report_fatal_error("Unable to lower arguments");
122
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000123 for (const BasicBlock &BB: F) {
Quentin Colombet17c494b2016-02-11 17:51:31 +0000124 MachineBasicBlock &MBB = getOrCreateBB(&BB);
Quentin Colombeta7fae162016-02-11 17:53:23 +0000125 MIRBuilder.setBasicBlock(MBB);
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000126 for (const Instruction &Inst: BB) {
127 bool Succeeded = translate(Inst);
128 if (!Succeeded) {
129 DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
130 report_fatal_error("Unable to translate instruction");
131 }
132 }
133 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000134 return false;
135}