blob: ffbda5ba5918e02d12934bd6af4b116d0c791412 [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 Colombetccd77252016-02-11 21:48:32 +000033unsigned IRTranslator::getOrCreateVReg(const Value *Val) {
34 unsigned &ValReg = ValToVReg[Val];
Quentin Colombet17c494b2016-02-11 17:51:31 +000035 // Check if this is the first time we see Val.
Quentin Colombetccd77252016-02-11 21:48:32 +000036 if (!ValReg) {
Quentin Colombet17c494b2016-02-11 17:51:31 +000037 // 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);
Quentin Colombetccd77252016-02-11 21:48:32 +000044 ValReg = VReg;
Quentin Colombet4f0ec8d2016-02-11 17:52:28 +000045 assert(!isa<Constant>(Val) && "Not yet implemented");
Quentin Colombet17c494b2016-02-11 17:51:31 +000046 }
Quentin Colombetccd77252016-02-11 21:48:32 +000047 return ValReg;
Quentin Colombet17c494b2016-02-11 17:51:31 +000048}
49
50MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock *BB) {
51 MachineBasicBlock *&MBB = BBToMBB[BB];
52 if (!MBB) {
Quentin Colombeta7fae162016-02-11 17:53:23 +000053 MachineFunction &MF = MIRBuilder.getMF();
Quentin Colombet17c494b2016-02-11 17:51:31 +000054 MBB = MF.CreateMachineBasicBlock();
55 MF.push_back(MBB);
56 }
57 return *MBB;
58}
59
Quentin Colombet105cf2b2016-01-20 20:58:56 +000060bool IRTranslator::translateADD(const Instruction &Inst) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000061 // Get or create a virtual register for each value.
62 // Unless the value is a Constant => loadimm cst?
63 // or inline constant each time?
64 // Creation of a virtual register needs to have a size.
Quentin Colombetccd77252016-02-11 21:48:32 +000065 unsigned Op0 = getOrCreateVReg(Inst.getOperand(0));
66 unsigned Op1 = getOrCreateVReg(Inst.getOperand(1));
67 unsigned Res = getOrCreateVReg(&Inst);
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000068 MIRBuilder.buildInstr(TargetOpcode::G_ADD, Inst.getType(), Res, Op0, Op1);
Quentin Colombet17c494b2016-02-11 17:51:31 +000069 return true;
Quentin Colombet105cf2b2016-01-20 20:58:56 +000070}
71
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000072bool IRTranslator::translateReturn(const Instruction &Inst) {
73 assert(isa<ReturnInst>(Inst) && "Return expected");
74 const Value *Ret = cast<ReturnInst>(Inst).getReturnValue();
75 // The target may mess up with the insertion point, but
76 // this is not important as a return is the last instruction
77 // of the block anyway.
78 return TLI->LowerReturn(MIRBuilder, Ret,
Quentin Colombetccd77252016-02-11 21:48:32 +000079 !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 Colombet74d7d2f2016-02-11 18:53:28 +0000107 TLI = MF.getSubtarget().getTargetLowering();
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 Colombetfd9d0a02016-02-11 19:59:41 +0000116 bool Succeeded = TLI->LowerFormalArguments(MIRBuilder, F.getArgumentList(),
117 VRegArgs);
118 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}