blob: 4ba21e7ec1d1dd6f655ffa9f4dd5630a74fc34fa [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;
Quentin Colombet39293d32016-03-08 01:38:55 +000030INITIALIZE_PASS(IRTranslator, "irtranslator", "IRTranslator LLVM IR -> MI",
31 false, false);
Quentin Colombet105cf2b2016-01-20 20:58:56 +000032
Quentin Colombeta7fae162016-02-11 17:53:23 +000033IRTranslator::IRTranslator() : MachineFunctionPass(ID), MRI(nullptr) {
Quentin Colombet39293d32016-03-08 01:38:55 +000034 initializeIRTranslatorPass(*PassRegistry::getPassRegistry());
Quentin Colombeta7fae162016-02-11 17:53:23 +000035}
36
Quentin Colombetccd77252016-02-11 21:48:32 +000037unsigned IRTranslator::getOrCreateVReg(const Value *Val) {
38 unsigned &ValReg = ValToVReg[Val];
Quentin Colombet17c494b2016-02-11 17:51:31 +000039 // Check if this is the first time we see Val.
Quentin Colombetccd77252016-02-11 21:48:32 +000040 if (!ValReg) {
Quentin Colombet17c494b2016-02-11 17:51:31 +000041 // Fill ValRegsSequence with the sequence of registers
42 // we need to concat together to produce the value.
43 assert(Val->getType()->isSized() &&
44 "Don't know how to create an empty vreg");
45 assert(!Val->getType()->isAggregateType() && "Not yet implemented");
46 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
47 unsigned VReg = MRI->createGenericVirtualRegister(Size);
Quentin Colombetccd77252016-02-11 21:48:32 +000048 ValReg = VReg;
Quentin Colombet4f0ec8d2016-02-11 17:52:28 +000049 assert(!isa<Constant>(Val) && "Not yet implemented");
Quentin Colombet17c494b2016-02-11 17:51:31 +000050 }
Quentin Colombetccd77252016-02-11 21:48:32 +000051 return ValReg;
Quentin Colombet17c494b2016-02-11 17:51:31 +000052}
53
54MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock *BB) {
55 MachineBasicBlock *&MBB = BBToMBB[BB];
56 if (!MBB) {
Quentin Colombeta7fae162016-02-11 17:53:23 +000057 MachineFunction &MF = MIRBuilder.getMF();
Quentin Colombet17c494b2016-02-11 17:51:31 +000058 MBB = MF.CreateMachineBasicBlock();
59 MF.push_back(MBB);
60 }
61 return *MBB;
62}
63
Quentin Colombet105cf2b2016-01-20 20:58:56 +000064bool IRTranslator::translateADD(const Instruction &Inst) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000065 // Get or create a virtual register for each value.
66 // Unless the value is a Constant => loadimm cst?
67 // or inline constant each time?
68 // Creation of a virtual register needs to have a size.
Quentin Colombetccd77252016-02-11 21:48:32 +000069 unsigned Op0 = getOrCreateVReg(Inst.getOperand(0));
70 unsigned Op1 = getOrCreateVReg(Inst.getOperand(1));
71 unsigned Res = getOrCreateVReg(&Inst);
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000072 MIRBuilder.buildInstr(TargetOpcode::G_ADD, Inst.getType(), Res, Op0, Op1);
Quentin Colombet17c494b2016-02-11 17:51:31 +000073 return true;
Quentin Colombet105cf2b2016-01-20 20:58:56 +000074}
75
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000076bool IRTranslator::translateReturn(const Instruction &Inst) {
77 assert(isa<ReturnInst>(Inst) && "Return expected");
78 const Value *Ret = cast<ReturnInst>(Inst).getReturnValue();
79 // The target may mess up with the insertion point, but
80 // this is not important as a return is the last instruction
81 // of the block anyway.
Quentin Colombetba2a0162016-02-16 19:26:02 +000082 return CLI->LowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(Ret));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000083}
84
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000085bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombeta7fae162016-02-11 17:53:23 +000086 MIRBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000087 switch(Inst.getOpcode()) {
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000088 case Instruction::Add:
89 return translateADD(Inst);
90 case Instruction::Ret:
91 return translateReturn(Inst);
92
93 default:
94 llvm_unreachable("Opcode not supported");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000095 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +000096}
97
98
99void IRTranslator::finalize() {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000100 // Release the memory used by the different maps we
101 // needed during the translation.
Quentin Colombetccd77252016-02-11 21:48:32 +0000102 ValToVReg.clear();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000103 Constants.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000104}
105
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000106bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000107 const Function &F = *MF.getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000108 if (F.empty())
109 return false;
Quentin Colombetba2a0162016-02-16 19:26:02 +0000110 CLI = MF.getSubtarget().getCallLowering();
Quentin Colombeta7fae162016-02-11 17:53:23 +0000111 MIRBuilder.setFunction(MF);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000112 MRI = &MF.getRegInfo();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000113 // Setup the arguments.
114 MachineBasicBlock &MBB = getOrCreateBB(&F.front());
115 MIRBuilder.setBasicBlock(MBB);
116 SmallVector<unsigned, 8> VRegArgs;
117 for (const Argument &Arg: F.args())
Quentin Colombetccd77252016-02-11 21:48:32 +0000118 VRegArgs.push_back(getOrCreateVReg(&Arg));
Quentin Colombetba2a0162016-02-16 19:26:02 +0000119 bool Succeeded =
120 CLI->LowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000121 if (!Succeeded)
122 report_fatal_error("Unable to lower arguments");
123
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000124 for (const BasicBlock &BB: F) {
Quentin Colombet17c494b2016-02-11 17:51:31 +0000125 MachineBasicBlock &MBB = getOrCreateBB(&BB);
Quentin Colombeta7fae162016-02-11 17:53:23 +0000126 MIRBuilder.setBasicBlock(MBB);
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000127 for (const Instruction &Inst: BB) {
128 bool Succeeded = translate(Inst);
129 if (!Succeeded) {
130 DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
131 report_fatal_error("Unable to translate instruction");
132 }
133 }
134 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000135 return false;
136}