blob: ed0ce8d848ba0678fea2067713bbe44bc28c31c3 [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"
Tim Northover5fb414d2016-07-29 22:32:36 +000022#include "llvm/IR/IntrinsicInst.h"
Quentin Colombet17c494b2016-02-11 17:51:31 +000023#include "llvm/IR/Type.h"
24#include "llvm/IR/Value.h"
Tim Northover5fb414d2016-07-29 22:32:36 +000025#include "llvm/Target/TargetIntrinsicInfo.h"
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000026#include "llvm/Target/TargetLowering.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000027
28#define DEBUG_TYPE "irtranslator"
29
Quentin Colombet105cf2b2016-01-20 20:58:56 +000030using namespace llvm;
31
32char IRTranslator::ID = 0;
Quentin Colombet39293d32016-03-08 01:38:55 +000033INITIALIZE_PASS(IRTranslator, "irtranslator", "IRTranslator LLVM IR -> MI",
Tim Northover884b47e2016-07-26 03:29:18 +000034 false, false)
Quentin Colombet105cf2b2016-01-20 20:58:56 +000035
Quentin Colombeta7fae162016-02-11 17:53:23 +000036IRTranslator::IRTranslator() : MachineFunctionPass(ID), MRI(nullptr) {
Quentin Colombet39293d32016-03-08 01:38:55 +000037 initializeIRTranslatorPass(*PassRegistry::getPassRegistry());
Quentin Colombeta7fae162016-02-11 17:53:23 +000038}
39
Quentin Colombete225e252016-03-11 17:27:54 +000040unsigned IRTranslator::getOrCreateVReg(const Value &Val) {
41 unsigned &ValReg = ValToVReg[&Val];
Quentin Colombet17c494b2016-02-11 17:51:31 +000042 // Check if this is the first time we see Val.
Quentin Colombetccd77252016-02-11 21:48:32 +000043 if (!ValReg) {
Quentin Colombet17c494b2016-02-11 17:51:31 +000044 // Fill ValRegsSequence with the sequence of registers
45 // we need to concat together to produce the value.
Quentin Colombete225e252016-03-11 17:27:54 +000046 assert(Val.getType()->isSized() &&
Quentin Colombet17c494b2016-02-11 17:51:31 +000047 "Don't know how to create an empty vreg");
Tim Northoverbd505462016-07-22 16:59:52 +000048 unsigned Size = DL->getTypeSizeInBits(Val.getType());
Quentin Colombet17c494b2016-02-11 17:51:31 +000049 unsigned VReg = MRI->createGenericVirtualRegister(Size);
Quentin Colombetccd77252016-02-11 21:48:32 +000050 ValReg = VReg;
Tim Northover5ed648e2016-08-09 21:28:04 +000051
52 if (auto CV = dyn_cast<Constant>(&Val)) {
53 bool Success = translate(*CV, VReg);
54 if (!Success)
55 report_fatal_error("unable to translate constant");
56 }
Quentin Colombet17c494b2016-02-11 17:51:31 +000057 }
Quentin Colombetccd77252016-02-11 21:48:32 +000058 return ValReg;
Quentin Colombet17c494b2016-02-11 17:51:31 +000059}
60
Tim Northoverad2b7172016-07-26 20:23:26 +000061unsigned IRTranslator::getMemOpAlignment(const Instruction &I) {
62 unsigned Alignment = 0;
63 Type *ValTy = nullptr;
64 if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
65 Alignment = SI->getAlignment();
66 ValTy = SI->getValueOperand()->getType();
67 } else if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
68 Alignment = LI->getAlignment();
69 ValTy = LI->getType();
70 } else
71 llvm_unreachable("unhandled memory instruction");
72
73 return Alignment ? Alignment : DL->getABITypeAlignment(ValTy);
74}
75
Quentin Colombet53237a92016-03-11 17:27:43 +000076MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock &BB) {
77 MachineBasicBlock *&MBB = BBToMBB[&BB];
Quentin Colombet17c494b2016-02-11 17:51:31 +000078 if (!MBB) {
Quentin Colombeta7fae162016-02-11 17:53:23 +000079 MachineFunction &MF = MIRBuilder.getMF();
Quentin Colombet17c494b2016-02-11 17:51:31 +000080 MBB = MF.CreateMachineBasicBlock();
81 MF.push_back(MBB);
82 }
83 return *MBB;
84}
85
Tim Northover357f1be2016-08-10 23:02:41 +000086bool IRTranslator::translateBinaryOp(unsigned Opcode, const User &U) {
Tim Northover0d56e052016-07-29 18:11:21 +000087 // FIXME: handle signed/unsigned wrapping flags.
88
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000089 // Get or create a virtual register for each value.
90 // Unless the value is a Constant => loadimm cst?
91 // or inline constant each time?
92 // Creation of a virtual register needs to have a size.
Tim Northover357f1be2016-08-10 23:02:41 +000093 unsigned Op0 = getOrCreateVReg(*U.getOperand(0));
94 unsigned Op1 = getOrCreateVReg(*U.getOperand(1));
95 unsigned Res = getOrCreateVReg(U);
96 MIRBuilder.buildInstr(Opcode, LLT{*U.getType()})
Tim Northovera51575f2016-07-29 17:43:52 +000097 .addDef(Res)
98 .addUse(Op0)
99 .addUse(Op1);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000100 return true;
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000101}
102
Tim Northoverde3aea0412016-08-17 20:25:25 +0000103bool IRTranslator::translateICmp(const User &U) {
104 const CmpInst &CI = cast<CmpInst>(U);
105 unsigned Op0 = getOrCreateVReg(*CI.getOperand(0));
106 unsigned Op1 = getOrCreateVReg(*CI.getOperand(1));
107 unsigned Res = getOrCreateVReg(CI);
108 CmpInst::Predicate Pred = CI.getPredicate();
109
110 assert(isa<ICmpInst>(CI) && "only integer comparisons supported now");
111 assert(CmpInst::isIntPredicate(Pred) && "only int comparisons supported now");
112 MIRBuilder.buildICmp({LLT{*CI.getType()}, LLT{*CI.getOperand(0)->getType()}},
113 Pred, Res, Op0, Op1);
114 return true;
115}
116
Tim Northover357f1be2016-08-10 23:02:41 +0000117bool IRTranslator::translateRet(const User &U) {
118 const ReturnInst &RI = cast<ReturnInst>(U);
Tim Northover0d56e052016-07-29 18:11:21 +0000119 const Value *Ret = RI.getReturnValue();
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000120 // The target may mess up with the insertion point, but
121 // this is not important as a return is the last instruction
122 // of the block anyway.
Tom Stellardb72a65f2016-04-14 17:23:33 +0000123 return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000124}
125
Tim Northover357f1be2016-08-10 23:02:41 +0000126bool IRTranslator::translateBr(const User &U) {
127 const BranchInst &BrInst = cast<BranchInst>(U);
Tim Northover69c2ba52016-07-29 17:58:00 +0000128 unsigned Succ = 0;
129 if (!BrInst.isUnconditional()) {
130 // We want a G_BRCOND to the true BB followed by an unconditional branch.
131 unsigned Tst = getOrCreateVReg(*BrInst.getCondition());
132 const BasicBlock &TrueTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ++));
133 MachineBasicBlock &TrueBB = getOrCreateBB(TrueTgt);
134 MIRBuilder.buildBrCond(LLT{*BrInst.getCondition()->getType()}, Tst, TrueBB);
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000135 }
Tim Northover69c2ba52016-07-29 17:58:00 +0000136
137 const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ));
138 MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt);
139 MIRBuilder.buildBr(TgtBB);
140
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000141 // Link successors.
142 MachineBasicBlock &CurBB = MIRBuilder.getMBB();
143 for (const BasicBlock *Succ : BrInst.successors())
144 CurBB.addSuccessor(&getOrCreateBB(*Succ));
145 return true;
146}
147
Tim Northover357f1be2016-08-10 23:02:41 +0000148bool IRTranslator::translateLoad(const User &U) {
149 const LoadInst &LI = cast<LoadInst>(U);
Tim Northoverad2b7172016-07-26 20:23:26 +0000150 assert(LI.isSimple() && "only simple loads are supported at the moment");
151
152 MachineFunction &MF = MIRBuilder.getMF();
153 unsigned Res = getOrCreateVReg(LI);
154 unsigned Addr = getOrCreateVReg(*LI.getPointerOperand());
Tim Northover28fdc422016-08-15 21:13:17 +0000155 LLT VTy{*LI.getType(), DL}, PTy{*LI.getPointerOperand()->getType()};
Tim Northoverad2b7172016-07-26 20:23:26 +0000156
157 MIRBuilder.buildLoad(
158 VTy, PTy, Res, Addr,
Tim Northover28fdc422016-08-15 21:13:17 +0000159 *MF.getMachineMemOperand(
160 MachinePointerInfo(LI.getPointerOperand()), MachineMemOperand::MOLoad,
161 DL->getTypeStoreSize(LI.getType()), getMemOpAlignment(LI)));
Tim Northoverad2b7172016-07-26 20:23:26 +0000162 return true;
163}
164
Tim Northover357f1be2016-08-10 23:02:41 +0000165bool IRTranslator::translateStore(const User &U) {
166 const StoreInst &SI = cast<StoreInst>(U);
Tim Northoverad2b7172016-07-26 20:23:26 +0000167 assert(SI.isSimple() && "only simple loads are supported at the moment");
168
169 MachineFunction &MF = MIRBuilder.getMF();
170 unsigned Val = getOrCreateVReg(*SI.getValueOperand());
171 unsigned Addr = getOrCreateVReg(*SI.getPointerOperand());
Tim Northover28fdc422016-08-15 21:13:17 +0000172 LLT VTy{*SI.getValueOperand()->getType(), DL},
Tim Northoverad2b7172016-07-26 20:23:26 +0000173 PTy{*SI.getPointerOperand()->getType()};
174
175 MIRBuilder.buildStore(
176 VTy, PTy, Val, Addr,
Tim Northover28fdc422016-08-15 21:13:17 +0000177 *MF.getMachineMemOperand(
178 MachinePointerInfo(SI.getPointerOperand()),
179 MachineMemOperand::MOStore,
180 DL->getTypeStoreSize(SI.getValueOperand()->getType()),
181 getMemOpAlignment(SI)));
Tim Northoverad2b7172016-07-26 20:23:26 +0000182 return true;
183}
184
Tim Northover6f80b082016-08-19 17:47:05 +0000185bool IRTranslator::translateExtractValue(const User &U) {
186 const ExtractValueInst &EVI = cast<ExtractValueInst>(U);
187 const Value *Src = EVI.getAggregateOperand();
188 Type *Int32Ty = Type::getInt32Ty(EVI.getContext());
189 SmallVector<Value *, 1> Indices;
190
191 // getIndexedOffsetInType is designed for GEPs, so the first index is the
192 // usual array element rather than looking into the actual aggregate.
193 Indices.push_back(ConstantInt::get(Int32Ty, 0));
194 for (auto Idx : EVI.indices())
195 Indices.push_back(ConstantInt::get(Int32Ty, Idx));
196
197 uint64_t Offset = 8 * DL->getIndexedOffsetInType(Src->getType(), Indices);
198
199 unsigned Res = getOrCreateVReg(EVI);
200 MIRBuilder.buildExtract(LLT{*EVI.getType()}, Res, getOrCreateVReg(*Src),
201 Offset);
202
203 return true;
204}
205
Tim Northover357f1be2016-08-10 23:02:41 +0000206bool IRTranslator::translateBitCast(const User &U) {
207 if (LLT{*U.getOperand(0)->getType()} == LLT{*U.getType()}) {
208 unsigned &Reg = ValToVReg[&U];
Tim Northover7552ef52016-08-10 16:51:14 +0000209 if (Reg)
Tim Northover357f1be2016-08-10 23:02:41 +0000210 MIRBuilder.buildCopy(Reg, getOrCreateVReg(*U.getOperand(0)));
Tim Northover7552ef52016-08-10 16:51:14 +0000211 else
Tim Northover357f1be2016-08-10 23:02:41 +0000212 Reg = getOrCreateVReg(*U.getOperand(0));
Tim Northover7c9eba92016-07-25 21:01:29 +0000213 return true;
214 }
Tim Northover357f1be2016-08-10 23:02:41 +0000215 return translateCast(TargetOpcode::G_BITCAST, U);
Tim Northover7c9eba92016-07-25 21:01:29 +0000216}
217
Tim Northover357f1be2016-08-10 23:02:41 +0000218bool IRTranslator::translateCast(unsigned Opcode, const User &U) {
219 unsigned Op = getOrCreateVReg(*U.getOperand(0));
220 unsigned Res = getOrCreateVReg(U);
221 MIRBuilder
222 .buildInstr(Opcode, {LLT{*U.getType()}, LLT{*U.getOperand(0)->getType()}})
Tim Northovera51575f2016-07-29 17:43:52 +0000223 .addDef(Res)
224 .addUse(Op);
Tim Northover7c9eba92016-07-25 21:01:29 +0000225 return true;
226}
227
Tim Northover91c81732016-08-19 17:17:06 +0000228bool IRTranslator::translateKnownIntrinsic(const CallInst &CI,
229 Intrinsic::ID ID) {
230 unsigned Op = 0;
231 switch (ID) {
232 default: return false;
233 case Intrinsic::uadd_with_overflow: Op = TargetOpcode::G_UADDE; break;
234 case Intrinsic::sadd_with_overflow: Op = TargetOpcode::G_SADDO; break;
235 case Intrinsic::usub_with_overflow: Op = TargetOpcode::G_USUBE; break;
236 case Intrinsic::ssub_with_overflow: Op = TargetOpcode::G_SSUBO; break;
237 case Intrinsic::umul_with_overflow: Op = TargetOpcode::G_UMULO; break;
238 case Intrinsic::smul_with_overflow: Op = TargetOpcode::G_SMULO; break;
239 }
240
241 LLT Ty{*CI.getOperand(0)->getType()};
242 LLT s1 = LLT::scalar(1);
243 unsigned Width = Ty.getSizeInBits();
244 unsigned Res = MRI->createGenericVirtualRegister(Width);
245 unsigned Overflow = MRI->createGenericVirtualRegister(1);
246 auto MIB = MIRBuilder.buildInstr(Op, {Ty, s1})
247 .addDef(Res)
248 .addDef(Overflow)
249 .addUse(getOrCreateVReg(*CI.getOperand(0)))
250 .addUse(getOrCreateVReg(*CI.getOperand(1)));
251
252 if (Op == TargetOpcode::G_UADDE || Op == TargetOpcode::G_USUBE) {
253 unsigned Zero = MRI->createGenericVirtualRegister(1);
254 EntryBuilder.buildConstant(s1, Zero, 0);
255 MIB.addUse(Zero);
256 }
257
258 MIRBuilder.buildSequence(LLT{*CI.getType(), DL}, getOrCreateVReg(CI), Res, 0,
259 Overflow, Width);
260 return true;
261}
262
Tim Northover357f1be2016-08-10 23:02:41 +0000263bool IRTranslator::translateCall(const User &U) {
264 const CallInst &CI = cast<CallInst>(U);
Tim Northover5fb414d2016-07-29 22:32:36 +0000265 auto TII = MIRBuilder.getMF().getTarget().getIntrinsicInfo();
Tim Northover406024a2016-08-10 21:44:01 +0000266 const Function *F = CI.getCalledFunction();
Tim Northover5fb414d2016-07-29 22:32:36 +0000267
Tim Northover406024a2016-08-10 21:44:01 +0000268 if (!F || !F->isIntrinsic()) {
269 // FIXME: handle multiple return values.
270 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
271 SmallVector<unsigned, 8> Args;
272 for (auto &Arg: CI.arg_operands())
273 Args.push_back(getOrCreateVReg(*Arg));
274
275 return CLI->lowerCall(MIRBuilder, CI,
276 F ? 0 : getOrCreateVReg(*CI.getCalledValue()), Res,
277 Args);
278 }
279
280 Intrinsic::ID ID = F->getIntrinsicID();
281 if (TII && ID == Intrinsic::not_intrinsic)
282 ID = static_cast<Intrinsic::ID>(TII->getIntrinsicID(F));
283
284 assert(ID != Intrinsic::not_intrinsic && "unknown intrinsic");
Tim Northover5fb414d2016-07-29 22:32:36 +0000285
Tim Northover91c81732016-08-19 17:17:06 +0000286 if (translateKnownIntrinsic(CI, ID))
287 return true;
288
Tim Northover5fb414d2016-07-29 22:32:36 +0000289 // Need types (starting with return) & args.
290 SmallVector<LLT, 4> Tys;
291 Tys.emplace_back(*CI.getType());
292 for (auto &Arg : CI.arg_operands())
293 Tys.emplace_back(*Arg->getType());
294
295 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
296 MachineInstrBuilder MIB =
297 MIRBuilder.buildIntrinsic(Tys, ID, Res, !CI.doesNotAccessMemory());
298
299 for (auto &Arg : CI.arg_operands()) {
300 if (ConstantInt *CI = dyn_cast<ConstantInt>(Arg))
301 MIB.addImm(CI->getSExtValue());
302 else
303 MIB.addUse(getOrCreateVReg(*Arg));
304 }
305 return true;
306}
307
Tim Northoverbd505462016-07-22 16:59:52 +0000308bool IRTranslator::translateStaticAlloca(const AllocaInst &AI) {
309 assert(AI.isStaticAlloca() && "only handle static allocas now");
310 MachineFunction &MF = MIRBuilder.getMF();
311 unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
312 unsigned Size =
313 ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
314
Tim Northover8d2f52e2016-07-27 17:47:54 +0000315 // Always allocate at least one byte.
316 Size = std::max(Size, 1u);
317
Tim Northoverbd505462016-07-22 16:59:52 +0000318 unsigned Alignment = AI.getAlignment();
319 if (!Alignment)
320 Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
321
322 unsigned Res = getOrCreateVReg(AI);
Matthias Braun93320392016-07-28 20:13:42 +0000323 int FI = MF.getFrameInfo().CreateStackObject(Size, Alignment, false, &AI);
Tim Northoverbd505462016-07-22 16:59:52 +0000324 MIRBuilder.buildFrameIndex(LLT::pointer(0), Res, FI);
325 return true;
326}
327
Tim Northover357f1be2016-08-10 23:02:41 +0000328bool IRTranslator::translatePHI(const User &U) {
329 const PHINode &PI = cast<PHINode>(U);
Tim Northover97d0cb32016-08-05 17:16:40 +0000330 MachineInstrBuilder MIB = MIRBuilder.buildInstr(TargetOpcode::PHI);
331 MIB.addDef(getOrCreateVReg(PI));
332
333 PendingPHIs.emplace_back(&PI, MIB.getInstr());
334 return true;
335}
336
337void IRTranslator::finishPendingPhis() {
338 for (std::pair<const PHINode *, MachineInstr *> &Phi : PendingPHIs) {
339 const PHINode *PI = Phi.first;
340 MachineInstrBuilder MIB(MIRBuilder.getMF(), Phi.second);
341
342 // All MachineBasicBlocks exist, add them to the PHI. We assume IRTranslator
343 // won't create extra control flow here, otherwise we need to find the
344 // dominating predecessor here (or perhaps force the weirder IRTranslators
345 // to provide a simple boundary).
346 for (unsigned i = 0; i < PI->getNumIncomingValues(); ++i) {
347 assert(BBToMBB[PI->getIncomingBlock(i)]->isSuccessor(MIB->getParent()) &&
348 "I appear to have misunderstood Machine PHIs");
349 MIB.addUse(getOrCreateVReg(*PI->getIncomingValue(i)));
350 MIB.addMBB(BBToMBB[PI->getIncomingBlock(i)]);
351 }
352 }
Tim Northover14e7f732016-08-05 17:50:36 +0000353
354 PendingPHIs.clear();
Tim Northover97d0cb32016-08-05 17:16:40 +0000355}
356
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000357bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombeta7fae162016-02-11 17:53:23 +0000358 MIRBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000359 switch(Inst.getOpcode()) {
Tim Northover357f1be2016-08-10 23:02:41 +0000360#define HANDLE_INST(NUM, OPCODE, CLASS) \
361 case Instruction::OPCODE: return translate##OPCODE(Inst);
362#include "llvm/IR/Instruction.def"
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000363 default:
Tim Northover357f1be2016-08-10 23:02:41 +0000364 llvm_unreachable("unknown opcode");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000365 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000366}
367
Tim Northover5ed648e2016-08-09 21:28:04 +0000368bool IRTranslator::translate(const Constant &C, unsigned Reg) {
Tim Northoverd403a3d2016-08-09 23:01:30 +0000369 if (auto CI = dyn_cast<ConstantInt>(&C))
Tim Northover5ed648e2016-08-09 21:28:04 +0000370 EntryBuilder.buildConstant(LLT{*CI->getType()}, Reg, CI->getZExtValue());
Tim Northoverd403a3d2016-08-09 23:01:30 +0000371 else if (isa<UndefValue>(C))
372 EntryBuilder.buildInstr(TargetOpcode::IMPLICIT_DEF).addDef(Reg);
Tim Northover8e0c53a2016-08-11 21:40:55 +0000373 else if (isa<ConstantPointerNull>(C))
374 EntryBuilder.buildInstr(TargetOpcode::G_CONSTANT, LLT{*C.getType()})
375 .addDef(Reg)
376 .addImm(0);
Tim Northover357f1be2016-08-10 23:02:41 +0000377 else if (auto CE = dyn_cast<ConstantExpr>(&C)) {
378 switch(CE->getOpcode()) {
379#define HANDLE_INST(NUM, OPCODE, CLASS) \
380 case Instruction::OPCODE: return translate##OPCODE(*CE);
381#include "llvm/IR/Instruction.def"
382 default:
383 llvm_unreachable("unknown opcode");
384 }
385 } else
Tim Northoverd403a3d2016-08-09 23:01:30 +0000386 llvm_unreachable("unhandled constant kind");
Tim Northover5ed648e2016-08-09 21:28:04 +0000387
Tim Northoverd403a3d2016-08-09 23:01:30 +0000388 return true;
Tim Northover5ed648e2016-08-09 21:28:04 +0000389}
390
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000391
Tim Northover0d510442016-08-11 16:21:29 +0000392void IRTranslator::finalizeFunction() {
393 finishPendingPhis();
394
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000395 // Release the memory used by the different maps we
396 // needed during the translation.
Quentin Colombetccd77252016-02-11 21:48:32 +0000397 ValToVReg.clear();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000398 Constants.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000399}
400
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000401bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000402 const Function &F = *MF.getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000403 if (F.empty())
404 return false;
Quentin Colombetba2a0162016-02-16 19:26:02 +0000405 CLI = MF.getSubtarget().getCallLowering();
Quentin Colombet000b5802016-03-11 17:27:51 +0000406 MIRBuilder.setMF(MF);
Tim Northover5ed648e2016-08-09 21:28:04 +0000407 EntryBuilder.setMF(MF);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000408 MRI = &MF.getRegInfo();
Tim Northoverbd505462016-07-22 16:59:52 +0000409 DL = &F.getParent()->getDataLayout();
410
Tim Northover14e7f732016-08-05 17:50:36 +0000411 assert(PendingPHIs.empty() && "stale PHIs");
412
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000413 // Setup the arguments.
Quentin Colombet53237a92016-03-11 17:27:43 +0000414 MachineBasicBlock &MBB = getOrCreateBB(F.front());
Quentin Colombet91ebd712016-03-11 17:27:47 +0000415 MIRBuilder.setMBB(MBB);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000416 SmallVector<unsigned, 8> VRegArgs;
417 for (const Argument &Arg: F.args())
Quentin Colombete225e252016-03-11 17:27:54 +0000418 VRegArgs.push_back(getOrCreateVReg(Arg));
Quentin Colombetba2a0162016-02-16 19:26:02 +0000419 bool Succeeded =
Tom Stellardb72a65f2016-04-14 17:23:33 +0000420 CLI->lowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000421 if (!Succeeded)
422 report_fatal_error("Unable to lower arguments");
423
Tim Northover5ed648e2016-08-09 21:28:04 +0000424 // Now that we've got the ABI handling code, it's safe to set a location for
425 // any Constants we find in the IR.
426 if (MBB.empty())
427 EntryBuilder.setMBB(MBB);
428 else
429 EntryBuilder.setInstr(MBB.back(), /* Before */ false);
430
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000431 for (const BasicBlock &BB: F) {
Quentin Colombet53237a92016-03-11 17:27:43 +0000432 MachineBasicBlock &MBB = getOrCreateBB(BB);
Quentin Colombet91ebd712016-03-11 17:27:47 +0000433 // Set the insertion point of all the following translations to
434 // the end of this basic block.
435 MIRBuilder.setMBB(MBB);
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000436 for (const Instruction &Inst: BB) {
437 bool Succeeded = translate(Inst);
438 if (!Succeeded) {
439 DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
440 report_fatal_error("Unable to translate instruction");
441 }
442 }
443 }
Tim Northover72eebfa2016-07-12 22:23:42 +0000444
Tim Northover0d510442016-08-11 16:21:29 +0000445 finalizeFunction();
Tim Northover97d0cb32016-08-05 17:16:40 +0000446
Tim Northover72eebfa2016-07-12 22:23:42 +0000447 // Now that the MachineFrameInfo has been configured, no further changes to
448 // the reserved registers are possible.
449 MRI->freezeReservedRegs(MF);
450
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000451 return false;
452}