blob: fbca421a22c14ba8523e8ea5cf1842766aa10d4d [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"
Tim Northoverbd505462016-07-22 16:59:52 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombet17c494b2016-02-11 17:51:31 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
20#include "llvm/IR/Constant.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000021#include "llvm/IR/Function.h"
Quentin Colombet17c494b2016-02-11 17:51:31 +000022#include "llvm/IR/Type.h"
23#include "llvm/IR/Value.h"
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000024#include "llvm/Target/TargetLowering.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000025
26#define DEBUG_TYPE "irtranslator"
27
Quentin Colombet105cf2b2016-01-20 20:58:56 +000028using namespace llvm;
29
30char IRTranslator::ID = 0;
Quentin Colombet39293d32016-03-08 01:38:55 +000031INITIALIZE_PASS(IRTranslator, "irtranslator", "IRTranslator LLVM IR -> MI",
Tim Northover884b47e2016-07-26 03:29:18 +000032 false, false)
Quentin Colombet105cf2b2016-01-20 20:58:56 +000033
Quentin Colombeta7fae162016-02-11 17:53:23 +000034IRTranslator::IRTranslator() : MachineFunctionPass(ID), MRI(nullptr) {
Quentin Colombet39293d32016-03-08 01:38:55 +000035 initializeIRTranslatorPass(*PassRegistry::getPassRegistry());
Quentin Colombeta7fae162016-02-11 17:53:23 +000036}
37
Quentin Colombete225e252016-03-11 17:27:54 +000038unsigned IRTranslator::getOrCreateVReg(const Value &Val) {
39 unsigned &ValReg = ValToVReg[&Val];
Quentin Colombet17c494b2016-02-11 17:51:31 +000040 // Check if this is the first time we see Val.
Quentin Colombetccd77252016-02-11 21:48:32 +000041 if (!ValReg) {
Quentin Colombet17c494b2016-02-11 17:51:31 +000042 // Fill ValRegsSequence with the sequence of registers
43 // we need to concat together to produce the value.
Quentin Colombete225e252016-03-11 17:27:54 +000044 assert(Val.getType()->isSized() &&
Quentin Colombet17c494b2016-02-11 17:51:31 +000045 "Don't know how to create an empty vreg");
Quentin Colombete225e252016-03-11 17:27:54 +000046 assert(!Val.getType()->isAggregateType() && "Not yet implemented");
Tim Northoverbd505462016-07-22 16:59:52 +000047 unsigned Size = DL->getTypeSizeInBits(Val.getType());
Quentin Colombet17c494b2016-02-11 17:51:31 +000048 unsigned VReg = MRI->createGenericVirtualRegister(Size);
Quentin Colombetccd77252016-02-11 21:48:32 +000049 ValReg = VReg;
Quentin Colombet4f0ec8d2016-02-11 17:52:28 +000050 assert(!isa<Constant>(Val) && "Not yet implemented");
Quentin Colombet17c494b2016-02-11 17:51:31 +000051 }
Quentin Colombetccd77252016-02-11 21:48:32 +000052 return ValReg;
Quentin Colombet17c494b2016-02-11 17:51:31 +000053}
54
Quentin Colombet53237a92016-03-11 17:27:43 +000055MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock &BB) {
56 MachineBasicBlock *&MBB = BBToMBB[&BB];
Quentin Colombet17c494b2016-02-11 17:51:31 +000057 if (!MBB) {
Quentin Colombeta7fae162016-02-11 17:53:23 +000058 MachineFunction &MF = MIRBuilder.getMF();
Quentin Colombet17c494b2016-02-11 17:51:31 +000059 MBB = MF.CreateMachineBasicBlock();
60 MF.push_back(MBB);
61 }
62 return *MBB;
63}
64
Quentin Colombet13c55e02016-06-10 20:50:18 +000065bool IRTranslator::translateBinaryOp(unsigned Opcode, const Instruction &Inst) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000066 // Get or create a virtual register for each value.
67 // Unless the value is a Constant => loadimm cst?
68 // or inline constant each time?
69 // Creation of a virtual register needs to have a size.
Quentin Colombete225e252016-03-11 17:27:54 +000070 unsigned Op0 = getOrCreateVReg(*Inst.getOperand(0));
71 unsigned Op1 = getOrCreateVReg(*Inst.getOperand(1));
72 unsigned Res = getOrCreateVReg(Inst);
Tim Northover62ae5682016-07-20 19:09:30 +000073 MIRBuilder.buildInstr(Opcode, LLT{*Inst.getType()}, Res, Op0, Op1);
Quentin Colombet17c494b2016-02-11 17:51:31 +000074 return true;
Quentin Colombet105cf2b2016-01-20 20:58:56 +000075}
76
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000077bool IRTranslator::translateReturn(const Instruction &Inst) {
78 assert(isa<ReturnInst>(Inst) && "Return expected");
79 const Value *Ret = cast<ReturnInst>(Inst).getReturnValue();
80 // The target may mess up with the insertion point, but
81 // this is not important as a return is the last instruction
82 // of the block anyway.
Tom Stellardb72a65f2016-04-14 17:23:33 +000083 return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000084}
85
Quentin Colombetdd4b1372016-03-11 17:28:03 +000086bool IRTranslator::translateBr(const Instruction &Inst) {
87 assert(isa<BranchInst>(Inst) && "Branch expected");
88 const BranchInst &BrInst = *cast<BranchInst>(&Inst);
89 if (BrInst.isUnconditional()) {
90 const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getOperand(0));
91 MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt);
Tim Northover62ae5682016-07-20 19:09:30 +000092 MIRBuilder.buildInstr(TargetOpcode::G_BR, LLT{*BrTgt.getType()}, TgtBB);
Quentin Colombetdd4b1372016-03-11 17:28:03 +000093 } else {
94 assert(0 && "Not yet implemented");
95 }
96 // Link successors.
97 MachineBasicBlock &CurBB = MIRBuilder.getMBB();
98 for (const BasicBlock *Succ : BrInst.successors())
99 CurBB.addSuccessor(&getOrCreateBB(*Succ));
100 return true;
101}
102
Tim Northover7c9eba92016-07-25 21:01:29 +0000103bool IRTranslator::translateBitCast(const CastInst &CI) {
104 if (LLT{*CI.getDestTy()} == LLT{*CI.getSrcTy()}) {
105 MIRBuilder.buildInstr(TargetOpcode::COPY, getOrCreateVReg(CI),
106 getOrCreateVReg(*CI.getOperand(0)));
107 return true;
108 }
109 return translateCast(TargetOpcode::G_BITCAST, CI);
110}
111
112bool IRTranslator::translateCast(unsigned Opcode, const CastInst &CI) {
113 unsigned Op = getOrCreateVReg(*CI.getOperand(0));
114 unsigned Res = getOrCreateVReg(CI);
115 MIRBuilder.buildInstr(Opcode, {LLT{*CI.getDestTy()}, LLT{*CI.getSrcTy()}},
116 Res, Op);
117 return true;
118}
119
Tim Northoverbd505462016-07-22 16:59:52 +0000120bool IRTranslator::translateStaticAlloca(const AllocaInst &AI) {
121 assert(AI.isStaticAlloca() && "only handle static allocas now");
122 MachineFunction &MF = MIRBuilder.getMF();
123 unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
124 unsigned Size =
125 ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
126
127 unsigned Alignment = AI.getAlignment();
128 if (!Alignment)
129 Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
130
131 unsigned Res = getOrCreateVReg(AI);
132 int FI = MF.getFrameInfo()->CreateStackObject(Size, Alignment, false, &AI);
133 MIRBuilder.buildFrameIndex(LLT::pointer(0), Res, FI);
134 return true;
135}
136
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000137bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombeta7fae162016-02-11 17:53:23 +0000138 MIRBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000139 switch(Inst.getOpcode()) {
Quentin Colombet19df8a12016-07-21 17:26:41 +0000140 // Arithmetic operations.
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000141 case Instruction::Add:
Quentin Colombet13c55e02016-06-10 20:50:18 +0000142 return translateBinaryOp(TargetOpcode::G_ADD, Inst);
Quentin Colombet2b59eab2016-07-21 17:26:50 +0000143 case Instruction::Sub:
144 return translateBinaryOp(TargetOpcode::G_SUB, Inst);
Tim Northoverbd505462016-07-22 16:59:52 +0000145
Quentin Colombet19df8a12016-07-21 17:26:41 +0000146 // Bitwise operations.
Quentin Colombet7bcc9212016-07-21 15:50:42 +0000147 case Instruction::And:
148 return translateBinaryOp(TargetOpcode::G_AND, Inst);
Quentin Colombetf2a19092016-06-10 20:50:35 +0000149 case Instruction::Or:
150 return translateBinaryOp(TargetOpcode::G_OR, Inst);
Tim Northoverbd505462016-07-22 16:59:52 +0000151
Quentin Colombet19df8a12016-07-21 17:26:41 +0000152 // Branch operations.
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000153 case Instruction::Br:
154 return translateBr(Inst);
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000155 case Instruction::Ret:
156 return translateReturn(Inst);
157
Tim Northover7c9eba92016-07-25 21:01:29 +0000158 // Casts
159 case Instruction::BitCast:
160 return translateBitCast(cast<CastInst>(Inst));
161 case Instruction::IntToPtr:
162 return translateCast(TargetOpcode::G_INTTOPTR, cast<CastInst>(Inst));
163 case Instruction::PtrToInt:
164 return translateCast(TargetOpcode::G_PTRTOINT, cast<CastInst>(Inst));
165
Tim Northoverbd505462016-07-22 16:59:52 +0000166 case Instruction::Alloca:
167 return translateStaticAlloca(cast<AllocaInst>(Inst));
168
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000169 default:
170 llvm_unreachable("Opcode not supported");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000171 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000172}
173
174
175void IRTranslator::finalize() {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000176 // Release the memory used by the different maps we
177 // needed during the translation.
Quentin Colombetccd77252016-02-11 21:48:32 +0000178 ValToVReg.clear();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000179 Constants.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000180}
181
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000182bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000183 const Function &F = *MF.getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000184 if (F.empty())
185 return false;
Quentin Colombetba2a0162016-02-16 19:26:02 +0000186 CLI = MF.getSubtarget().getCallLowering();
Quentin Colombet000b5802016-03-11 17:27:51 +0000187 MIRBuilder.setMF(MF);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000188 MRI = &MF.getRegInfo();
Tim Northoverbd505462016-07-22 16:59:52 +0000189 DL = &F.getParent()->getDataLayout();
190
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000191 // Setup the arguments.
Quentin Colombet53237a92016-03-11 17:27:43 +0000192 MachineBasicBlock &MBB = getOrCreateBB(F.front());
Quentin Colombet91ebd712016-03-11 17:27:47 +0000193 MIRBuilder.setMBB(MBB);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000194 SmallVector<unsigned, 8> VRegArgs;
195 for (const Argument &Arg: F.args())
Quentin Colombete225e252016-03-11 17:27:54 +0000196 VRegArgs.push_back(getOrCreateVReg(Arg));
Quentin Colombetba2a0162016-02-16 19:26:02 +0000197 bool Succeeded =
Tom Stellardb72a65f2016-04-14 17:23:33 +0000198 CLI->lowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000199 if (!Succeeded)
200 report_fatal_error("Unable to lower arguments");
201
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000202 for (const BasicBlock &BB: F) {
Quentin Colombet53237a92016-03-11 17:27:43 +0000203 MachineBasicBlock &MBB = getOrCreateBB(BB);
Quentin Colombet91ebd712016-03-11 17:27:47 +0000204 // Set the insertion point of all the following translations to
205 // the end of this basic block.
206 MIRBuilder.setMBB(MBB);
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000207 for (const Instruction &Inst: BB) {
208 bool Succeeded = translate(Inst);
209 if (!Succeeded) {
210 DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
211 report_fatal_error("Unable to translate instruction");
212 }
213 }
214 }
Tim Northover72eebfa2016-07-12 22:23:42 +0000215
216 // Now that the MachineFrameInfo has been configured, no further changes to
217 // the reserved registers are possible.
218 MRI->freezeReservedRegs(MF);
219
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000220 return false;
221}