blob: 3f47c9a6882d20e7c1c0039dfde4e2a8649060f7 [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 Northover62ae5682016-07-20 19:09:30 +000088 MIRBuilder.buildInstr(Opcode, LLT{*Inst.getType()}, Res, Op0, Op1);
Quentin Colombet17c494b2016-02-11 17:51:31 +000089 return true;
Quentin Colombet105cf2b2016-01-20 20:58:56 +000090}
91
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000092bool IRTranslator::translateReturn(const Instruction &Inst) {
93 assert(isa<ReturnInst>(Inst) && "Return expected");
94 const Value *Ret = cast<ReturnInst>(Inst).getReturnValue();
95 // The target may mess up with the insertion point, but
96 // this is not important as a return is the last instruction
97 // of the block anyway.
Tom Stellardb72a65f2016-04-14 17:23:33 +000098 return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000099}
100
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000101bool IRTranslator::translateBr(const Instruction &Inst) {
102 assert(isa<BranchInst>(Inst) && "Branch expected");
103 const BranchInst &BrInst = *cast<BranchInst>(&Inst);
104 if (BrInst.isUnconditional()) {
105 const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getOperand(0));
106 MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt);
Tim Northovercc5f7622016-07-26 16:45:26 +0000107 MIRBuilder.buildBr(TgtBB);
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000108 } else {
109 assert(0 && "Not yet implemented");
110 }
111 // Link successors.
112 MachineBasicBlock &CurBB = MIRBuilder.getMBB();
113 for (const BasicBlock *Succ : BrInst.successors())
114 CurBB.addSuccessor(&getOrCreateBB(*Succ));
115 return true;
116}
117
Tim Northoverad2b7172016-07-26 20:23:26 +0000118bool IRTranslator::translateLoad(const LoadInst &LI) {
119 assert(LI.isSimple() && "only simple loads are supported at the moment");
120
121 MachineFunction &MF = MIRBuilder.getMF();
122 unsigned Res = getOrCreateVReg(LI);
123 unsigned Addr = getOrCreateVReg(*LI.getPointerOperand());
124 LLT VTy{*LI.getType()}, PTy{*LI.getPointerOperand()->getType()};
125
126 MIRBuilder.buildLoad(
127 VTy, PTy, Res, Addr,
128 *MF.getMachineMemOperand(MachinePointerInfo(LI.getPointerOperand()),
129 MachineMemOperand::MOLoad,
130 VTy.getSizeInBits() / 8, getMemOpAlignment(LI)));
131 return true;
132}
133
134bool IRTranslator::translateStore(const StoreInst &SI) {
135 assert(SI.isSimple() && "only simple loads are supported at the moment");
136
137 MachineFunction &MF = MIRBuilder.getMF();
138 unsigned Val = getOrCreateVReg(*SI.getValueOperand());
139 unsigned Addr = getOrCreateVReg(*SI.getPointerOperand());
140 LLT VTy{*SI.getValueOperand()->getType()},
141 PTy{*SI.getPointerOperand()->getType()};
142
143 MIRBuilder.buildStore(
144 VTy, PTy, Val, Addr,
145 *MF.getMachineMemOperand(MachinePointerInfo(SI.getPointerOperand()),
146 MachineMemOperand::MOStore,
147 VTy.getSizeInBits() / 8, getMemOpAlignment(SI)));
148 return true;
149}
150
Tim Northover7c9eba92016-07-25 21:01:29 +0000151bool IRTranslator::translateBitCast(const CastInst &CI) {
152 if (LLT{*CI.getDestTy()} == LLT{*CI.getSrcTy()}) {
Tim Northover756eca32016-07-26 16:45:30 +0000153 MIRBuilder.buildCopy(getOrCreateVReg(CI),
154 getOrCreateVReg(*CI.getOperand(0)));
Tim Northover7c9eba92016-07-25 21:01:29 +0000155 return true;
156 }
157 return translateCast(TargetOpcode::G_BITCAST, CI);
158}
159
160bool IRTranslator::translateCast(unsigned Opcode, const CastInst &CI) {
161 unsigned Op = getOrCreateVReg(*CI.getOperand(0));
162 unsigned Res = getOrCreateVReg(CI);
163 MIRBuilder.buildInstr(Opcode, {LLT{*CI.getDestTy()}, LLT{*CI.getSrcTy()}},
164 Res, Op);
165 return true;
166}
167
Tim Northoverbd505462016-07-22 16:59:52 +0000168bool IRTranslator::translateStaticAlloca(const AllocaInst &AI) {
169 assert(AI.isStaticAlloca() && "only handle static allocas now");
170 MachineFunction &MF = MIRBuilder.getMF();
171 unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
172 unsigned Size =
173 ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
174
Tim Northover8d2f52e2016-07-27 17:47:54 +0000175 // Always allocate at least one byte.
176 Size = std::max(Size, 1u);
177
Tim Northoverbd505462016-07-22 16:59:52 +0000178 unsigned Alignment = AI.getAlignment();
179 if (!Alignment)
180 Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
181
182 unsigned Res = getOrCreateVReg(AI);
Matthias Braun93320392016-07-28 20:13:42 +0000183 int FI = MF.getFrameInfo().CreateStackObject(Size, Alignment, false, &AI);
Tim Northoverbd505462016-07-22 16:59:52 +0000184 MIRBuilder.buildFrameIndex(LLT::pointer(0), Res, FI);
185 return true;
186}
187
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000188bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombeta7fae162016-02-11 17:53:23 +0000189 MIRBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000190 switch(Inst.getOpcode()) {
Quentin Colombet19df8a12016-07-21 17:26:41 +0000191 // Arithmetic operations.
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000192 case Instruction::Add:
Quentin Colombet13c55e02016-06-10 20:50:18 +0000193 return translateBinaryOp(TargetOpcode::G_ADD, Inst);
Quentin Colombet2b59eab2016-07-21 17:26:50 +0000194 case Instruction::Sub:
195 return translateBinaryOp(TargetOpcode::G_SUB, Inst);
Tim Northoverbd505462016-07-22 16:59:52 +0000196
Quentin Colombet19df8a12016-07-21 17:26:41 +0000197 // Bitwise operations.
Quentin Colombet7bcc9212016-07-21 15:50:42 +0000198 case Instruction::And:
199 return translateBinaryOp(TargetOpcode::G_AND, Inst);
Quentin Colombetf2a19092016-06-10 20:50:35 +0000200 case Instruction::Or:
201 return translateBinaryOp(TargetOpcode::G_OR, Inst);
Tim Northoverbd505462016-07-22 16:59:52 +0000202
Quentin Colombet19df8a12016-07-21 17:26:41 +0000203 // Branch operations.
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000204 case Instruction::Br:
205 return translateBr(Inst);
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000206 case Instruction::Ret:
207 return translateReturn(Inst);
208
Tim Northover7c9eba92016-07-25 21:01:29 +0000209 // Casts
210 case Instruction::BitCast:
211 return translateBitCast(cast<CastInst>(Inst));
212 case Instruction::IntToPtr:
213 return translateCast(TargetOpcode::G_INTTOPTR, cast<CastInst>(Inst));
214 case Instruction::PtrToInt:
215 return translateCast(TargetOpcode::G_PTRTOINT, cast<CastInst>(Inst));
216
Tim Northoverad2b7172016-07-26 20:23:26 +0000217 // Memory ops.
218 case Instruction::Load:
219 return translateLoad(cast<LoadInst>(Inst));
220 case Instruction::Store:
221 return translateStore(cast<StoreInst>(Inst));
222
Tim Northoverbd505462016-07-22 16:59:52 +0000223 case Instruction::Alloca:
224 return translateStaticAlloca(cast<AllocaInst>(Inst));
225
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000226 default:
227 llvm_unreachable("Opcode not supported");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000228 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000229}
230
231
232void IRTranslator::finalize() {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000233 // Release the memory used by the different maps we
234 // needed during the translation.
Quentin Colombetccd77252016-02-11 21:48:32 +0000235 ValToVReg.clear();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000236 Constants.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000237}
238
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000239bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000240 const Function &F = *MF.getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000241 if (F.empty())
242 return false;
Quentin Colombetba2a0162016-02-16 19:26:02 +0000243 CLI = MF.getSubtarget().getCallLowering();
Quentin Colombet000b5802016-03-11 17:27:51 +0000244 MIRBuilder.setMF(MF);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000245 MRI = &MF.getRegInfo();
Tim Northoverbd505462016-07-22 16:59:52 +0000246 DL = &F.getParent()->getDataLayout();
247
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000248 // Setup the arguments.
Quentin Colombet53237a92016-03-11 17:27:43 +0000249 MachineBasicBlock &MBB = getOrCreateBB(F.front());
Quentin Colombet91ebd712016-03-11 17:27:47 +0000250 MIRBuilder.setMBB(MBB);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000251 SmallVector<unsigned, 8> VRegArgs;
252 for (const Argument &Arg: F.args())
Quentin Colombete225e252016-03-11 17:27:54 +0000253 VRegArgs.push_back(getOrCreateVReg(Arg));
Quentin Colombetba2a0162016-02-16 19:26:02 +0000254 bool Succeeded =
Tom Stellardb72a65f2016-04-14 17:23:33 +0000255 CLI->lowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000256 if (!Succeeded)
257 report_fatal_error("Unable to lower arguments");
258
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000259 for (const BasicBlock &BB: F) {
Quentin Colombet53237a92016-03-11 17:27:43 +0000260 MachineBasicBlock &MBB = getOrCreateBB(BB);
Quentin Colombet91ebd712016-03-11 17:27:47 +0000261 // Set the insertion point of all the following translations to
262 // the end of this basic block.
263 MIRBuilder.setMBB(MBB);
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000264 for (const Instruction &Inst: BB) {
265 bool Succeeded = translate(Inst);
266 if (!Succeeded) {
267 DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
268 report_fatal_error("Unable to translate instruction");
269 }
270 }
271 }
Tim Northover72eebfa2016-07-12 22:23:42 +0000272
273 // Now that the MachineFrameInfo has been configured, no further changes to
274 // the reserved registers are possible.
275 MRI->freezeReservedRegs(MF);
276
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000277 return false;
278}