blob: 8b9a5b5bf048cea68f6bb7fa9c7cec3e1cd48800 [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 Colombetba2a0162016-02-16 19:26:02 +000016#include "llvm/CodeGen/GlobalISel/CallLowering.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000017#include "llvm/CodeGen/MachineFunction.h"
Quentin Colombet17c494b2016-02-11 17:51:31 +000018#include "llvm/CodeGen/MachineRegisterInfo.h"
19#include "llvm/IR/Constant.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000020#include "llvm/IR/Function.h"
Quentin Colombet17c494b2016-02-11 17:51:31 +000021#include "llvm/IR/Type.h"
22#include "llvm/IR/Value.h"
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000023#include "llvm/Target/TargetLowering.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000024
25#define DEBUG_TYPE "irtranslator"
26
Quentin Colombet105cf2b2016-01-20 20:58:56 +000027using namespace llvm;
28
29char IRTranslator::ID = 0;
30
Quentin Colombeta7fae162016-02-11 17:53:23 +000031IRTranslator::IRTranslator() : MachineFunctionPass(ID), MRI(nullptr) {
32}
33
Quentin Colombetccd77252016-02-11 21:48:32 +000034unsigned IRTranslator::getOrCreateVReg(const Value *Val) {
35 unsigned &ValReg = ValToVReg[Val];
Quentin Colombet17c494b2016-02-11 17:51:31 +000036 // Check if this is the first time we see Val.
Quentin Colombetccd77252016-02-11 21:48:32 +000037 if (!ValReg) {
Quentin Colombet17c494b2016-02-11 17:51:31 +000038 // Fill ValRegsSequence with the sequence of registers
39 // we need to concat together to produce the value.
40 assert(Val->getType()->isSized() &&
41 "Don't know how to create an empty vreg");
42 assert(!Val->getType()->isAggregateType() && "Not yet implemented");
43 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
44 unsigned VReg = MRI->createGenericVirtualRegister(Size);
Quentin Colombetccd77252016-02-11 21:48:32 +000045 ValReg = VReg;
Quentin Colombet4f0ec8d2016-02-11 17:52:28 +000046 assert(!isa<Constant>(Val) && "Not yet implemented");
Quentin Colombet17c494b2016-02-11 17:51:31 +000047 }
Quentin Colombetccd77252016-02-11 21:48:32 +000048 return ValReg;
Quentin Colombet17c494b2016-02-11 17:51:31 +000049}
50
51MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock *BB) {
52 MachineBasicBlock *&MBB = BBToMBB[BB];
53 if (!MBB) {
Quentin Colombeta7fae162016-02-11 17:53:23 +000054 MachineFunction &MF = MIRBuilder.getMF();
Quentin Colombet17c494b2016-02-11 17:51:31 +000055 MBB = MF.CreateMachineBasicBlock();
56 MF.push_back(MBB);
57 }
58 return *MBB;
59}
60
Quentin Colombet105cf2b2016-01-20 20:58:56 +000061bool IRTranslator::translateADD(const Instruction &Inst) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000062 // Get or create a virtual register for each value.
63 // Unless the value is a Constant => loadimm cst?
64 // or inline constant each time?
65 // Creation of a virtual register needs to have a size.
Quentin Colombetccd77252016-02-11 21:48:32 +000066 unsigned Op0 = getOrCreateVReg(Inst.getOperand(0));
67 unsigned Op1 = getOrCreateVReg(Inst.getOperand(1));
68 unsigned Res = getOrCreateVReg(&Inst);
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000069 MIRBuilder.buildInstr(TargetOpcode::G_ADD, Inst.getType(), Res, Op0, Op1);
Quentin Colombet17c494b2016-02-11 17:51:31 +000070 return true;
Quentin Colombet105cf2b2016-01-20 20:58:56 +000071}
72
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000073bool IRTranslator::translateReturn(const Instruction &Inst) {
74 assert(isa<ReturnInst>(Inst) && "Return expected");
75 const Value *Ret = cast<ReturnInst>(Inst).getReturnValue();
76 // The target may mess up with the insertion point, but
77 // this is not important as a return is the last instruction
78 // of the block anyway.
Quentin Colombetba2a0162016-02-16 19:26:02 +000079 return CLI->LowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(Ret));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000080}
81
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000082bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombeta7fae162016-02-11 17:53:23 +000083 MIRBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000084 switch(Inst.getOpcode()) {
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000085 case Instruction::Add:
86 return translateADD(Inst);
87 case Instruction::Ret:
88 return translateReturn(Inst);
89
90 default:
91 llvm_unreachable("Opcode not supported");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000092 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +000093}
94
95
96void IRTranslator::finalize() {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000097 // Release the memory used by the different maps we
98 // needed during the translation.
Quentin Colombetccd77252016-02-11 21:48:32 +000099 ValToVReg.clear();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000100 Constants.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000101}
102
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000103bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000104 const Function &F = *MF.getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000105 if (F.empty())
106 return false;
Quentin Colombetba2a0162016-02-16 19:26:02 +0000107 CLI = MF.getSubtarget().getCallLowering();
Quentin Colombeta7fae162016-02-11 17:53:23 +0000108 MIRBuilder.setFunction(MF);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000109 MRI = &MF.getRegInfo();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000110 // Setup the arguments.
111 MachineBasicBlock &MBB = getOrCreateBB(&F.front());
112 MIRBuilder.setBasicBlock(MBB);
113 SmallVector<unsigned, 8> VRegArgs;
114 for (const Argument &Arg: F.args())
Quentin Colombetccd77252016-02-11 21:48:32 +0000115 VRegArgs.push_back(getOrCreateVReg(&Arg));
Quentin Colombetba2a0162016-02-16 19:26:02 +0000116 bool Succeeded =
117 CLI->LowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000118 if (!Succeeded)
119 report_fatal_error("Unable to lower arguments");
120
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000121 for (const BasicBlock &BB: F) {
Quentin Colombet17c494b2016-02-11 17:51:31 +0000122 MachineBasicBlock &MBB = getOrCreateBB(&BB);
Quentin Colombeta7fae162016-02-11 17:53:23 +0000123 MIRBuilder.setBasicBlock(MBB);
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000124 for (const Instruction &Inst: BB) {
125 bool Succeeded = translate(Inst);
126 if (!Succeeded) {
127 DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
128 report_fatal_error("Unable to translate instruction");
129 }
130 }
131 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000132 return false;
133}