blob: fb1bfdca2691c29c992bbd969129baeef78e15f2 [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
Tim Northoverad2b7172016-07-26 20:23:26 +000055unsigned IRTranslator::getMemOpAlignment(const Instruction &I) {
56 unsigned Alignment = 0;
57 Type *ValTy = nullptr;
58 if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
59 Alignment = SI->getAlignment();
60 ValTy = SI->getValueOperand()->getType();
61 } else if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
62 Alignment = LI->getAlignment();
63 ValTy = LI->getType();
64 } else
65 llvm_unreachable("unhandled memory instruction");
66
67 return Alignment ? Alignment : DL->getABITypeAlignment(ValTy);
68}
69
Quentin Colombet53237a92016-03-11 17:27:43 +000070MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock &BB) {
71 MachineBasicBlock *&MBB = BBToMBB[&BB];
Quentin Colombet17c494b2016-02-11 17:51:31 +000072 if (!MBB) {
Quentin Colombeta7fae162016-02-11 17:53:23 +000073 MachineFunction &MF = MIRBuilder.getMF();
Quentin Colombet17c494b2016-02-11 17:51:31 +000074 MBB = MF.CreateMachineBasicBlock();
75 MF.push_back(MBB);
76 }
77 return *MBB;
78}
79
Quentin Colombet13c55e02016-06-10 20:50:18 +000080bool IRTranslator::translateBinaryOp(unsigned Opcode, const Instruction &Inst) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000081 // Get or create a virtual register for each value.
82 // Unless the value is a Constant => loadimm cst?
83 // or inline constant each time?
84 // Creation of a virtual register needs to have a size.
Quentin Colombete225e252016-03-11 17:27:54 +000085 unsigned Op0 = getOrCreateVReg(*Inst.getOperand(0));
86 unsigned Op1 = getOrCreateVReg(*Inst.getOperand(1));
87 unsigned Res = getOrCreateVReg(Inst);
Tim Northovera51575f2016-07-29 17:43:52 +000088 MIRBuilder.buildInstr(Opcode, LLT{*Inst.getType()})
89 .addDef(Res)
90 .addUse(Op0)
91 .addUse(Op1);
Quentin Colombet17c494b2016-02-11 17:51:31 +000092 return true;
Quentin Colombet105cf2b2016-01-20 20:58:56 +000093}
94
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000095bool IRTranslator::translateReturn(const Instruction &Inst) {
96 assert(isa<ReturnInst>(Inst) && "Return expected");
97 const Value *Ret = cast<ReturnInst>(Inst).getReturnValue();
98 // The target may mess up with the insertion point, but
99 // this is not important as a return is the last instruction
100 // of the block anyway.
Tom Stellardb72a65f2016-04-14 17:23:33 +0000101 return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000102}
103
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000104bool IRTranslator::translateBr(const Instruction &Inst) {
105 assert(isa<BranchInst>(Inst) && "Branch expected");
106 const BranchInst &BrInst = *cast<BranchInst>(&Inst);
107 if (BrInst.isUnconditional()) {
108 const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getOperand(0));
109 MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt);
Tim Northovercc5f7622016-07-26 16:45:26 +0000110 MIRBuilder.buildBr(TgtBB);
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000111 } else {
112 assert(0 && "Not yet implemented");
113 }
114 // Link successors.
115 MachineBasicBlock &CurBB = MIRBuilder.getMBB();
116 for (const BasicBlock *Succ : BrInst.successors())
117 CurBB.addSuccessor(&getOrCreateBB(*Succ));
118 return true;
119}
120
Tim Northoverad2b7172016-07-26 20:23:26 +0000121bool IRTranslator::translateLoad(const LoadInst &LI) {
122 assert(LI.isSimple() && "only simple loads are supported at the moment");
123
124 MachineFunction &MF = MIRBuilder.getMF();
125 unsigned Res = getOrCreateVReg(LI);
126 unsigned Addr = getOrCreateVReg(*LI.getPointerOperand());
127 LLT VTy{*LI.getType()}, PTy{*LI.getPointerOperand()->getType()};
128
129 MIRBuilder.buildLoad(
130 VTy, PTy, Res, Addr,
131 *MF.getMachineMemOperand(MachinePointerInfo(LI.getPointerOperand()),
132 MachineMemOperand::MOLoad,
133 VTy.getSizeInBits() / 8, getMemOpAlignment(LI)));
134 return true;
135}
136
137bool IRTranslator::translateStore(const StoreInst &SI) {
138 assert(SI.isSimple() && "only simple loads are supported at the moment");
139
140 MachineFunction &MF = MIRBuilder.getMF();
141 unsigned Val = getOrCreateVReg(*SI.getValueOperand());
142 unsigned Addr = getOrCreateVReg(*SI.getPointerOperand());
143 LLT VTy{*SI.getValueOperand()->getType()},
144 PTy{*SI.getPointerOperand()->getType()};
145
146 MIRBuilder.buildStore(
147 VTy, PTy, Val, Addr,
148 *MF.getMachineMemOperand(MachinePointerInfo(SI.getPointerOperand()),
149 MachineMemOperand::MOStore,
150 VTy.getSizeInBits() / 8, getMemOpAlignment(SI)));
151 return true;
152}
153
Tim Northover7c9eba92016-07-25 21:01:29 +0000154bool IRTranslator::translateBitCast(const CastInst &CI) {
155 if (LLT{*CI.getDestTy()} == LLT{*CI.getSrcTy()}) {
Tim Northover756eca32016-07-26 16:45:30 +0000156 MIRBuilder.buildCopy(getOrCreateVReg(CI),
157 getOrCreateVReg(*CI.getOperand(0)));
Tim Northover7c9eba92016-07-25 21:01:29 +0000158 return true;
159 }
160 return translateCast(TargetOpcode::G_BITCAST, CI);
161}
162
163bool IRTranslator::translateCast(unsigned Opcode, const CastInst &CI) {
164 unsigned Op = getOrCreateVReg(*CI.getOperand(0));
165 unsigned Res = getOrCreateVReg(CI);
Tim Northovera51575f2016-07-29 17:43:52 +0000166 MIRBuilder.buildInstr(Opcode, {LLT{*CI.getDestTy()}, LLT{*CI.getSrcTy()}})
167 .addDef(Res)
168 .addUse(Op);
Tim Northover7c9eba92016-07-25 21:01:29 +0000169 return true;
170}
171
Tim Northoverbd505462016-07-22 16:59:52 +0000172bool IRTranslator::translateStaticAlloca(const AllocaInst &AI) {
173 assert(AI.isStaticAlloca() && "only handle static allocas now");
174 MachineFunction &MF = MIRBuilder.getMF();
175 unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
176 unsigned Size =
177 ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
178
Tim Northover8d2f52e2016-07-27 17:47:54 +0000179 // Always allocate at least one byte.
180 Size = std::max(Size, 1u);
181
Tim Northoverbd505462016-07-22 16:59:52 +0000182 unsigned Alignment = AI.getAlignment();
183 if (!Alignment)
184 Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
185
186 unsigned Res = getOrCreateVReg(AI);
Matthias Braun93320392016-07-28 20:13:42 +0000187 int FI = MF.getFrameInfo().CreateStackObject(Size, Alignment, false, &AI);
Tim Northoverbd505462016-07-22 16:59:52 +0000188 MIRBuilder.buildFrameIndex(LLT::pointer(0), Res, FI);
189 return true;
190}
191
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000192bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombeta7fae162016-02-11 17:53:23 +0000193 MIRBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000194 switch(Inst.getOpcode()) {
Quentin Colombet19df8a12016-07-21 17:26:41 +0000195 // Arithmetic operations.
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000196 case Instruction::Add:
Quentin Colombet13c55e02016-06-10 20:50:18 +0000197 return translateBinaryOp(TargetOpcode::G_ADD, Inst);
Quentin Colombet2b59eab2016-07-21 17:26:50 +0000198 case Instruction::Sub:
199 return translateBinaryOp(TargetOpcode::G_SUB, Inst);
Tim Northoverbd505462016-07-22 16:59:52 +0000200
Quentin Colombet19df8a12016-07-21 17:26:41 +0000201 // Bitwise operations.
Quentin Colombet7bcc9212016-07-21 15:50:42 +0000202 case Instruction::And:
203 return translateBinaryOp(TargetOpcode::G_AND, Inst);
Quentin Colombetf2a19092016-06-10 20:50:35 +0000204 case Instruction::Or:
205 return translateBinaryOp(TargetOpcode::G_OR, Inst);
Ahmed Bougacha784e3422016-07-29 16:56:20 +0000206 case Instruction::Xor:
207 return translateBinaryOp(TargetOpcode::G_XOR, Inst);
Tim Northoverbd505462016-07-22 16:59:52 +0000208
Quentin Colombet19df8a12016-07-21 17:26:41 +0000209 // Branch operations.
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000210 case Instruction::Br:
211 return translateBr(Inst);
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000212 case Instruction::Ret:
213 return translateReturn(Inst);
214
Tim Northover7c9eba92016-07-25 21:01:29 +0000215 // Casts
216 case Instruction::BitCast:
217 return translateBitCast(cast<CastInst>(Inst));
218 case Instruction::IntToPtr:
219 return translateCast(TargetOpcode::G_INTTOPTR, cast<CastInst>(Inst));
220 case Instruction::PtrToInt:
221 return translateCast(TargetOpcode::G_PTRTOINT, cast<CastInst>(Inst));
222
Tim Northoverad2b7172016-07-26 20:23:26 +0000223 // Memory ops.
224 case Instruction::Load:
225 return translateLoad(cast<LoadInst>(Inst));
226 case Instruction::Store:
227 return translateStore(cast<StoreInst>(Inst));
228
Tim Northoverbd505462016-07-22 16:59:52 +0000229 case Instruction::Alloca:
230 return translateStaticAlloca(cast<AllocaInst>(Inst));
231
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000232 default:
233 llvm_unreachable("Opcode not supported");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000234 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000235}
236
237
238void IRTranslator::finalize() {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000239 // Release the memory used by the different maps we
240 // needed during the translation.
Quentin Colombetccd77252016-02-11 21:48:32 +0000241 ValToVReg.clear();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000242 Constants.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000243}
244
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000245bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000246 const Function &F = *MF.getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000247 if (F.empty())
248 return false;
Quentin Colombetba2a0162016-02-16 19:26:02 +0000249 CLI = MF.getSubtarget().getCallLowering();
Quentin Colombet000b5802016-03-11 17:27:51 +0000250 MIRBuilder.setMF(MF);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000251 MRI = &MF.getRegInfo();
Tim Northoverbd505462016-07-22 16:59:52 +0000252 DL = &F.getParent()->getDataLayout();
253
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000254 // Setup the arguments.
Quentin Colombet53237a92016-03-11 17:27:43 +0000255 MachineBasicBlock &MBB = getOrCreateBB(F.front());
Quentin Colombet91ebd712016-03-11 17:27:47 +0000256 MIRBuilder.setMBB(MBB);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000257 SmallVector<unsigned, 8> VRegArgs;
258 for (const Argument &Arg: F.args())
Quentin Colombete225e252016-03-11 17:27:54 +0000259 VRegArgs.push_back(getOrCreateVReg(Arg));
Quentin Colombetba2a0162016-02-16 19:26:02 +0000260 bool Succeeded =
Tom Stellardb72a65f2016-04-14 17:23:33 +0000261 CLI->lowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000262 if (!Succeeded)
263 report_fatal_error("Unable to lower arguments");
264
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000265 for (const BasicBlock &BB: F) {
Quentin Colombet53237a92016-03-11 17:27:43 +0000266 MachineBasicBlock &MBB = getOrCreateBB(BB);
Quentin Colombet91ebd712016-03-11 17:27:47 +0000267 // Set the insertion point of all the following translations to
268 // the end of this basic block.
269 MIRBuilder.setMBB(MBB);
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000270 for (const Instruction &Inst: BB) {
271 bool Succeeded = translate(Inst);
272 if (!Succeeded) {
273 DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
274 report_fatal_error("Unable to translate instruction");
275 }
276 }
277 }
Tim Northover72eebfa2016-07-12 22:23:42 +0000278
279 // Now that the MachineFrameInfo has been configured, no further changes to
280 // the reserved registers are possible.
281 MRI->freezeReservedRegs(MF);
282
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000283 return false;
284}