blob: 145f2e2f38424e9b2d421c44a288aa3e49c1ee5c [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 Colombete225e252016-03-11 17:27:54 +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.
Quentin Colombete225e252016-03-11 17:27:54 +000043 assert(Val.getType()->isSized() &&
Quentin Colombet17c494b2016-02-11 17:51:31 +000044 "Don't know how to create an empty vreg");
Quentin Colombete225e252016-03-11 17:27:54 +000045 assert(!Val.getType()->isAggregateType() && "Not yet implemented");
46 unsigned Size = Val.getType()->getPrimitiveSizeInBits();
Quentin Colombet17c494b2016-02-11 17:51:31 +000047 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
Quentin Colombet53237a92016-03-11 17:27:43 +000054MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock &BB) {
55 MachineBasicBlock *&MBB = BBToMBB[&BB];
Quentin Colombet17c494b2016-02-11 17:51:31 +000056 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 Colombet13c55e02016-06-10 20:50:18 +000064bool IRTranslator::translateBinaryOp(unsigned Opcode, 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 Colombete225e252016-03-11 17:27:54 +000069 unsigned Op0 = getOrCreateVReg(*Inst.getOperand(0));
70 unsigned Op1 = getOrCreateVReg(*Inst.getOperand(1));
71 unsigned Res = getOrCreateVReg(Inst);
Tim Northover62ae5682016-07-20 19:09:30 +000072 MIRBuilder.buildInstr(Opcode, LLT{*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.
Tom Stellardb72a65f2016-04-14 17:23:33 +000082 return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000083}
84
Quentin Colombetdd4b1372016-03-11 17:28:03 +000085bool IRTranslator::translateBr(const Instruction &Inst) {
86 assert(isa<BranchInst>(Inst) && "Branch expected");
87 const BranchInst &BrInst = *cast<BranchInst>(&Inst);
88 if (BrInst.isUnconditional()) {
89 const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getOperand(0));
90 MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt);
Tim Northover62ae5682016-07-20 19:09:30 +000091 MIRBuilder.buildInstr(TargetOpcode::G_BR, LLT{*BrTgt.getType()}, TgtBB);
Quentin Colombetdd4b1372016-03-11 17:28:03 +000092 } else {
93 assert(0 && "Not yet implemented");
94 }
95 // Link successors.
96 MachineBasicBlock &CurBB = MIRBuilder.getMBB();
97 for (const BasicBlock *Succ : BrInst.successors())
98 CurBB.addSuccessor(&getOrCreateBB(*Succ));
99 return true;
100}
101
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000102bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombeta7fae162016-02-11 17:53:23 +0000103 MIRBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000104 switch(Inst.getOpcode()) {
Quentin Colombet19df8a12016-07-21 17:26:41 +0000105 // Arithmetic operations.
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000106 case Instruction::Add:
Quentin Colombet13c55e02016-06-10 20:50:18 +0000107 return translateBinaryOp(TargetOpcode::G_ADD, Inst);
Quentin Colombet19df8a12016-07-21 17:26:41 +0000108 // Bitwise operations.
Quentin Colombet7bcc9212016-07-21 15:50:42 +0000109 case Instruction::And:
110 return translateBinaryOp(TargetOpcode::G_AND, Inst);
Quentin Colombetf2a19092016-06-10 20:50:35 +0000111 case Instruction::Or:
112 return translateBinaryOp(TargetOpcode::G_OR, Inst);
Quentin Colombet19df8a12016-07-21 17:26:41 +0000113 // Branch operations.
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000114 case Instruction::Br:
115 return translateBr(Inst);
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000116 case Instruction::Ret:
117 return translateReturn(Inst);
118
119 default:
120 llvm_unreachable("Opcode not supported");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000121 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000122}
123
124
125void IRTranslator::finalize() {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000126 // Release the memory used by the different maps we
127 // needed during the translation.
Quentin Colombetccd77252016-02-11 21:48:32 +0000128 ValToVReg.clear();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000129 Constants.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000130}
131
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000132bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000133 const Function &F = *MF.getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000134 if (F.empty())
135 return false;
Quentin Colombetba2a0162016-02-16 19:26:02 +0000136 CLI = MF.getSubtarget().getCallLowering();
Quentin Colombet000b5802016-03-11 17:27:51 +0000137 MIRBuilder.setMF(MF);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000138 MRI = &MF.getRegInfo();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000139 // Setup the arguments.
Quentin Colombet53237a92016-03-11 17:27:43 +0000140 MachineBasicBlock &MBB = getOrCreateBB(F.front());
Quentin Colombet91ebd712016-03-11 17:27:47 +0000141 MIRBuilder.setMBB(MBB);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000142 SmallVector<unsigned, 8> VRegArgs;
143 for (const Argument &Arg: F.args())
Quentin Colombete225e252016-03-11 17:27:54 +0000144 VRegArgs.push_back(getOrCreateVReg(Arg));
Quentin Colombetba2a0162016-02-16 19:26:02 +0000145 bool Succeeded =
Tom Stellardb72a65f2016-04-14 17:23:33 +0000146 CLI->lowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000147 if (!Succeeded)
148 report_fatal_error("Unable to lower arguments");
149
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000150 for (const BasicBlock &BB: F) {
Quentin Colombet53237a92016-03-11 17:27:43 +0000151 MachineBasicBlock &MBB = getOrCreateBB(BB);
Quentin Colombet91ebd712016-03-11 17:27:47 +0000152 // Set the insertion point of all the following translations to
153 // the end of this basic block.
154 MIRBuilder.setMBB(MBB);
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000155 for (const Instruction &Inst: BB) {
156 bool Succeeded = translate(Inst);
157 if (!Succeeded) {
158 DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
159 report_fatal_error("Unable to translate instruction");
160 }
161 }
162 }
Tim Northover72eebfa2016-07-12 22:23:42 +0000163
164 // Now that the MachineFrameInfo has been configured, no further changes to
165 // the reserved registers are possible.
166 MRI->freezeReservedRegs(MF);
167
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000168 return false;
169}