blob: 1a243c5488767e1eb49b4662cd20987aef0ff905 [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) {
Tim Northoverb6046222016-08-19 20:09:03 +0000186 const Value *Src = U.getOperand(0);
187 Type *Int32Ty = Type::getInt32Ty(U.getContext());
Tim Northover6f80b082016-08-19 17:47:05 +0000188 SmallVector<Value *, 1> Indices;
189
190 // getIndexedOffsetInType is designed for GEPs, so the first index is the
191 // usual array element rather than looking into the actual aggregate.
192 Indices.push_back(ConstantInt::get(Int32Ty, 0));
Tim Northoverb6046222016-08-19 20:09:03 +0000193
194 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&U)) {
195 for (auto Idx : EVI->indices())
196 Indices.push_back(ConstantInt::get(Int32Ty, Idx));
197 } else {
198 for (unsigned i = 1; i < U.getNumOperands(); ++i)
199 Indices.push_back(U.getOperand(i));
200 }
Tim Northover6f80b082016-08-19 17:47:05 +0000201
202 uint64_t Offset = 8 * DL->getIndexedOffsetInType(Src->getType(), Indices);
203
Tim Northoverb6046222016-08-19 20:09:03 +0000204 unsigned Res = getOrCreateVReg(U);
205 MIRBuilder.buildExtract(LLT{*U.getType(), DL}, Res, Offset,
Tim Northover26b76f22016-08-19 18:32:14 +0000206 LLT{*Src->getType(), DL}, getOrCreateVReg(*Src));
Tim Northover6f80b082016-08-19 17:47:05 +0000207
208 return true;
209}
210
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000211bool IRTranslator::translateInsertValue(const User &U) {
Tim Northoverb6046222016-08-19 20:09:03 +0000212 const Value *Src = U.getOperand(0);
213 Type *Int32Ty = Type::getInt32Ty(U.getContext());
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000214 SmallVector<Value *, 1> Indices;
215
216 // getIndexedOffsetInType is designed for GEPs, so the first index is the
217 // usual array element rather than looking into the actual aggregate.
218 Indices.push_back(ConstantInt::get(Int32Ty, 0));
Tim Northoverb6046222016-08-19 20:09:03 +0000219
220 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&U)) {
221 for (auto Idx : IVI->indices())
222 Indices.push_back(ConstantInt::get(Int32Ty, Idx));
223 } else {
224 for (unsigned i = 2; i < U.getNumOperands(); ++i)
225 Indices.push_back(U.getOperand(i));
226 }
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000227
228 uint64_t Offset = 8 * DL->getIndexedOffsetInType(Src->getType(), Indices);
229
Tim Northoverb6046222016-08-19 20:09:03 +0000230 unsigned Res = getOrCreateVReg(U);
231 const Value &Inserted = *U.getOperand(1);
232 MIRBuilder.buildInsert(LLT{*U.getType(), DL}, Res, getOrCreateVReg(*Src),
233 LLT{*Inserted.getType(), DL},
234 getOrCreateVReg(Inserted), Offset);
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000235
236 return true;
237}
238
Tim Northover5a28c362016-08-19 20:09:07 +0000239bool IRTranslator::translateSelect(const User &U) {
240 MIRBuilder.buildSelect(
241 LLT{*U.getType()}, getOrCreateVReg(U), getOrCreateVReg(*U.getOperand(0)),
242 getOrCreateVReg(*U.getOperand(1)), getOrCreateVReg(*U.getOperand(2)));
243 return true;
244}
245
Tim Northover357f1be2016-08-10 23:02:41 +0000246bool IRTranslator::translateBitCast(const User &U) {
247 if (LLT{*U.getOperand(0)->getType()} == LLT{*U.getType()}) {
248 unsigned &Reg = ValToVReg[&U];
Tim Northover7552ef52016-08-10 16:51:14 +0000249 if (Reg)
Tim Northover357f1be2016-08-10 23:02:41 +0000250 MIRBuilder.buildCopy(Reg, getOrCreateVReg(*U.getOperand(0)));
Tim Northover7552ef52016-08-10 16:51:14 +0000251 else
Tim Northover357f1be2016-08-10 23:02:41 +0000252 Reg = getOrCreateVReg(*U.getOperand(0));
Tim Northover7c9eba92016-07-25 21:01:29 +0000253 return true;
254 }
Tim Northover357f1be2016-08-10 23:02:41 +0000255 return translateCast(TargetOpcode::G_BITCAST, U);
Tim Northover7c9eba92016-07-25 21:01:29 +0000256}
257
Tim Northover357f1be2016-08-10 23:02:41 +0000258bool IRTranslator::translateCast(unsigned Opcode, const User &U) {
259 unsigned Op = getOrCreateVReg(*U.getOperand(0));
260 unsigned Res = getOrCreateVReg(U);
261 MIRBuilder
262 .buildInstr(Opcode, {LLT{*U.getType()}, LLT{*U.getOperand(0)->getType()}})
Tim Northovera51575f2016-07-29 17:43:52 +0000263 .addDef(Res)
264 .addUse(Op);
Tim Northover7c9eba92016-07-25 21:01:29 +0000265 return true;
266}
267
Tim Northover91c81732016-08-19 17:17:06 +0000268bool IRTranslator::translateKnownIntrinsic(const CallInst &CI,
269 Intrinsic::ID ID) {
270 unsigned Op = 0;
271 switch (ID) {
272 default: return false;
273 case Intrinsic::uadd_with_overflow: Op = TargetOpcode::G_UADDE; break;
274 case Intrinsic::sadd_with_overflow: Op = TargetOpcode::G_SADDO; break;
275 case Intrinsic::usub_with_overflow: Op = TargetOpcode::G_USUBE; break;
276 case Intrinsic::ssub_with_overflow: Op = TargetOpcode::G_SSUBO; break;
277 case Intrinsic::umul_with_overflow: Op = TargetOpcode::G_UMULO; break;
278 case Intrinsic::smul_with_overflow: Op = TargetOpcode::G_SMULO; break;
279 }
280
281 LLT Ty{*CI.getOperand(0)->getType()};
282 LLT s1 = LLT::scalar(1);
283 unsigned Width = Ty.getSizeInBits();
284 unsigned Res = MRI->createGenericVirtualRegister(Width);
285 unsigned Overflow = MRI->createGenericVirtualRegister(1);
286 auto MIB = MIRBuilder.buildInstr(Op, {Ty, s1})
287 .addDef(Res)
288 .addDef(Overflow)
289 .addUse(getOrCreateVReg(*CI.getOperand(0)))
290 .addUse(getOrCreateVReg(*CI.getOperand(1)));
291
292 if (Op == TargetOpcode::G_UADDE || Op == TargetOpcode::G_USUBE) {
293 unsigned Zero = MRI->createGenericVirtualRegister(1);
294 EntryBuilder.buildConstant(s1, Zero, 0);
295 MIB.addUse(Zero);
296 }
297
Tim Northover26b76f22016-08-19 18:32:14 +0000298 MIRBuilder.buildSequence(LLT{*CI.getType(), DL}, getOrCreateVReg(CI), Ty, Res,
299 0, s1, Overflow, Width);
Tim Northover91c81732016-08-19 17:17:06 +0000300 return true;
301}
302
Tim Northover357f1be2016-08-10 23:02:41 +0000303bool IRTranslator::translateCall(const User &U) {
304 const CallInst &CI = cast<CallInst>(U);
Tim Northover5fb414d2016-07-29 22:32:36 +0000305 auto TII = MIRBuilder.getMF().getTarget().getIntrinsicInfo();
Tim Northover406024a2016-08-10 21:44:01 +0000306 const Function *F = CI.getCalledFunction();
Tim Northover5fb414d2016-07-29 22:32:36 +0000307
Tim Northover406024a2016-08-10 21:44:01 +0000308 if (!F || !F->isIntrinsic()) {
309 // FIXME: handle multiple return values.
310 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
311 SmallVector<unsigned, 8> Args;
312 for (auto &Arg: CI.arg_operands())
313 Args.push_back(getOrCreateVReg(*Arg));
314
315 return CLI->lowerCall(MIRBuilder, CI,
316 F ? 0 : getOrCreateVReg(*CI.getCalledValue()), Res,
317 Args);
318 }
319
320 Intrinsic::ID ID = F->getIntrinsicID();
321 if (TII && ID == Intrinsic::not_intrinsic)
322 ID = static_cast<Intrinsic::ID>(TII->getIntrinsicID(F));
323
324 assert(ID != Intrinsic::not_intrinsic && "unknown intrinsic");
Tim Northover5fb414d2016-07-29 22:32:36 +0000325
Tim Northover91c81732016-08-19 17:17:06 +0000326 if (translateKnownIntrinsic(CI, ID))
327 return true;
328
Tim Northover5fb414d2016-07-29 22:32:36 +0000329 // Need types (starting with return) & args.
330 SmallVector<LLT, 4> Tys;
331 Tys.emplace_back(*CI.getType());
332 for (auto &Arg : CI.arg_operands())
333 Tys.emplace_back(*Arg->getType());
334
335 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
336 MachineInstrBuilder MIB =
337 MIRBuilder.buildIntrinsic(Tys, ID, Res, !CI.doesNotAccessMemory());
338
339 for (auto &Arg : CI.arg_operands()) {
340 if (ConstantInt *CI = dyn_cast<ConstantInt>(Arg))
341 MIB.addImm(CI->getSExtValue());
342 else
343 MIB.addUse(getOrCreateVReg(*Arg));
344 }
345 return true;
346}
347
Tim Northoverbd505462016-07-22 16:59:52 +0000348bool IRTranslator::translateStaticAlloca(const AllocaInst &AI) {
349 assert(AI.isStaticAlloca() && "only handle static allocas now");
350 MachineFunction &MF = MIRBuilder.getMF();
351 unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
352 unsigned Size =
353 ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
354
Tim Northover8d2f52e2016-07-27 17:47:54 +0000355 // Always allocate at least one byte.
356 Size = std::max(Size, 1u);
357
Tim Northoverbd505462016-07-22 16:59:52 +0000358 unsigned Alignment = AI.getAlignment();
359 if (!Alignment)
360 Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
361
362 unsigned Res = getOrCreateVReg(AI);
Matthias Braun93320392016-07-28 20:13:42 +0000363 int FI = MF.getFrameInfo().CreateStackObject(Size, Alignment, false, &AI);
Tim Northoverbd505462016-07-22 16:59:52 +0000364 MIRBuilder.buildFrameIndex(LLT::pointer(0), Res, FI);
365 return true;
366}
367
Tim Northover357f1be2016-08-10 23:02:41 +0000368bool IRTranslator::translatePHI(const User &U) {
369 const PHINode &PI = cast<PHINode>(U);
Tim Northover97d0cb32016-08-05 17:16:40 +0000370 MachineInstrBuilder MIB = MIRBuilder.buildInstr(TargetOpcode::PHI);
371 MIB.addDef(getOrCreateVReg(PI));
372
373 PendingPHIs.emplace_back(&PI, MIB.getInstr());
374 return true;
375}
376
377void IRTranslator::finishPendingPhis() {
378 for (std::pair<const PHINode *, MachineInstr *> &Phi : PendingPHIs) {
379 const PHINode *PI = Phi.first;
380 MachineInstrBuilder MIB(MIRBuilder.getMF(), Phi.second);
381
382 // All MachineBasicBlocks exist, add them to the PHI. We assume IRTranslator
383 // won't create extra control flow here, otherwise we need to find the
384 // dominating predecessor here (or perhaps force the weirder IRTranslators
385 // to provide a simple boundary).
386 for (unsigned i = 0; i < PI->getNumIncomingValues(); ++i) {
387 assert(BBToMBB[PI->getIncomingBlock(i)]->isSuccessor(MIB->getParent()) &&
388 "I appear to have misunderstood Machine PHIs");
389 MIB.addUse(getOrCreateVReg(*PI->getIncomingValue(i)));
390 MIB.addMBB(BBToMBB[PI->getIncomingBlock(i)]);
391 }
392 }
Tim Northover14e7f732016-08-05 17:50:36 +0000393
394 PendingPHIs.clear();
Tim Northover97d0cb32016-08-05 17:16:40 +0000395}
396
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000397bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombeta7fae162016-02-11 17:53:23 +0000398 MIRBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000399 switch(Inst.getOpcode()) {
Tim Northover357f1be2016-08-10 23:02:41 +0000400#define HANDLE_INST(NUM, OPCODE, CLASS) \
401 case Instruction::OPCODE: return translate##OPCODE(Inst);
402#include "llvm/IR/Instruction.def"
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000403 default:
Tim Northover357f1be2016-08-10 23:02:41 +0000404 llvm_unreachable("unknown opcode");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000405 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000406}
407
Tim Northover5ed648e2016-08-09 21:28:04 +0000408bool IRTranslator::translate(const Constant &C, unsigned Reg) {
Tim Northoverd403a3d2016-08-09 23:01:30 +0000409 if (auto CI = dyn_cast<ConstantInt>(&C))
Tim Northover5ed648e2016-08-09 21:28:04 +0000410 EntryBuilder.buildConstant(LLT{*CI->getType()}, Reg, CI->getZExtValue());
Tim Northoverb16734f2016-08-19 20:09:15 +0000411 else if (auto CF = dyn_cast<ConstantFP>(&C))
412 EntryBuilder.buildFConstant(LLT{*CF->getType()}, Reg, *CF);
Tim Northoverd403a3d2016-08-09 23:01:30 +0000413 else if (isa<UndefValue>(C))
414 EntryBuilder.buildInstr(TargetOpcode::IMPLICIT_DEF).addDef(Reg);
Tim Northover8e0c53a2016-08-11 21:40:55 +0000415 else if (isa<ConstantPointerNull>(C))
416 EntryBuilder.buildInstr(TargetOpcode::G_CONSTANT, LLT{*C.getType()})
417 .addDef(Reg)
418 .addImm(0);
Tim Northover357f1be2016-08-10 23:02:41 +0000419 else if (auto CE = dyn_cast<ConstantExpr>(&C)) {
420 switch(CE->getOpcode()) {
421#define HANDLE_INST(NUM, OPCODE, CLASS) \
422 case Instruction::OPCODE: return translate##OPCODE(*CE);
423#include "llvm/IR/Instruction.def"
424 default:
425 llvm_unreachable("unknown opcode");
426 }
427 } else
Tim Northoverd403a3d2016-08-09 23:01:30 +0000428 llvm_unreachable("unhandled constant kind");
Tim Northover5ed648e2016-08-09 21:28:04 +0000429
Tim Northoverd403a3d2016-08-09 23:01:30 +0000430 return true;
Tim Northover5ed648e2016-08-09 21:28:04 +0000431}
432
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000433
Tim Northover0d510442016-08-11 16:21:29 +0000434void IRTranslator::finalizeFunction() {
435 finishPendingPhis();
436
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000437 // Release the memory used by the different maps we
438 // needed during the translation.
Quentin Colombetccd77252016-02-11 21:48:32 +0000439 ValToVReg.clear();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000440 Constants.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000441}
442
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000443bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000444 const Function &F = *MF.getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000445 if (F.empty())
446 return false;
Quentin Colombetba2a0162016-02-16 19:26:02 +0000447 CLI = MF.getSubtarget().getCallLowering();
Quentin Colombet000b5802016-03-11 17:27:51 +0000448 MIRBuilder.setMF(MF);
Tim Northover5ed648e2016-08-09 21:28:04 +0000449 EntryBuilder.setMF(MF);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000450 MRI = &MF.getRegInfo();
Tim Northoverbd505462016-07-22 16:59:52 +0000451 DL = &F.getParent()->getDataLayout();
452
Tim Northover14e7f732016-08-05 17:50:36 +0000453 assert(PendingPHIs.empty() && "stale PHIs");
454
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000455 // Setup the arguments.
Quentin Colombet53237a92016-03-11 17:27:43 +0000456 MachineBasicBlock &MBB = getOrCreateBB(F.front());
Quentin Colombet91ebd712016-03-11 17:27:47 +0000457 MIRBuilder.setMBB(MBB);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000458 SmallVector<unsigned, 8> VRegArgs;
459 for (const Argument &Arg: F.args())
Quentin Colombete225e252016-03-11 17:27:54 +0000460 VRegArgs.push_back(getOrCreateVReg(Arg));
Quentin Colombetba2a0162016-02-16 19:26:02 +0000461 bool Succeeded =
Tom Stellardb72a65f2016-04-14 17:23:33 +0000462 CLI->lowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000463 if (!Succeeded)
464 report_fatal_error("Unable to lower arguments");
465
Tim Northover5ed648e2016-08-09 21:28:04 +0000466 // Now that we've got the ABI handling code, it's safe to set a location for
467 // any Constants we find in the IR.
468 if (MBB.empty())
469 EntryBuilder.setMBB(MBB);
470 else
471 EntryBuilder.setInstr(MBB.back(), /* Before */ false);
472
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000473 for (const BasicBlock &BB: F) {
Quentin Colombet53237a92016-03-11 17:27:43 +0000474 MachineBasicBlock &MBB = getOrCreateBB(BB);
Quentin Colombet91ebd712016-03-11 17:27:47 +0000475 // Set the insertion point of all the following translations to
476 // the end of this basic block.
477 MIRBuilder.setMBB(MBB);
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000478 for (const Instruction &Inst: BB) {
479 bool Succeeded = translate(Inst);
480 if (!Succeeded) {
481 DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
482 report_fatal_error("Unable to translate instruction");
483 }
484 }
485 }
Tim Northover72eebfa2016-07-12 22:23:42 +0000486
Tim Northover0d510442016-08-11 16:21:29 +0000487 finalizeFunction();
Tim Northover97d0cb32016-08-05 17:16:40 +0000488
Tim Northover72eebfa2016-07-12 22:23:42 +0000489 // Now that the MachineFrameInfo has been configured, no further changes to
490 // the reserved registers are possible.
491 MRI->freezeReservedRegs(MF);
492
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000493 return false;
494}