blob: 64316a15b8dd94010c00687213dd300392a0f2f8 [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"
Quentin Colombet3bb32cc2016-08-26 23:49:05 +000020#include "llvm/CodeGen/TargetPassConfig.h"
Quentin Colombet17c494b2016-02-11 17:51:31 +000021#include "llvm/IR/Constant.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000022#include "llvm/IR/Function.h"
Tim Northover5fb414d2016-07-29 22:32:36 +000023#include "llvm/IR/IntrinsicInst.h"
Quentin Colombet17c494b2016-02-11 17:51:31 +000024#include "llvm/IR/Type.h"
25#include "llvm/IR/Value.h"
Tim Northover5fb414d2016-07-29 22:32:36 +000026#include "llvm/Target/TargetIntrinsicInfo.h"
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000027#include "llvm/Target/TargetLowering.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000028
29#define DEBUG_TYPE "irtranslator"
30
Quentin Colombet105cf2b2016-01-20 20:58:56 +000031using namespace llvm;
32
33char IRTranslator::ID = 0;
Quentin Colombet3bb32cc2016-08-26 23:49:05 +000034INITIALIZE_PASS_BEGIN(IRTranslator, DEBUG_TYPE, "IRTranslator LLVM IR -> MI",
35 false, false)
36INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
37INITIALIZE_PASS_END(IRTranslator, DEBUG_TYPE, "IRTranslator LLVM IR -> MI",
Tim Northover884b47e2016-07-26 03:29:18 +000038 false, false)
Quentin Colombet105cf2b2016-01-20 20:58:56 +000039
Quentin Colombeta7fae162016-02-11 17:53:23 +000040IRTranslator::IRTranslator() : MachineFunctionPass(ID), MRI(nullptr) {
Quentin Colombet39293d32016-03-08 01:38:55 +000041 initializeIRTranslatorPass(*PassRegistry::getPassRegistry());
Quentin Colombeta7fae162016-02-11 17:53:23 +000042}
43
Quentin Colombet3bb32cc2016-08-26 23:49:05 +000044void IRTranslator::getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.addRequired<TargetPassConfig>();
46 MachineFunctionPass::getAnalysisUsage(AU);
47}
48
49
Quentin Colombete225e252016-03-11 17:27:54 +000050unsigned IRTranslator::getOrCreateVReg(const Value &Val) {
51 unsigned &ValReg = ValToVReg[&Val];
Quentin Colombet17c494b2016-02-11 17:51:31 +000052 // Check if this is the first time we see Val.
Quentin Colombetccd77252016-02-11 21:48:32 +000053 if (!ValReg) {
Quentin Colombet17c494b2016-02-11 17:51:31 +000054 // Fill ValRegsSequence with the sequence of registers
55 // we need to concat together to produce the value.
Quentin Colombete225e252016-03-11 17:27:54 +000056 assert(Val.getType()->isSized() &&
Quentin Colombet17c494b2016-02-11 17:51:31 +000057 "Don't know how to create an empty vreg");
Tim Northoverbd505462016-07-22 16:59:52 +000058 unsigned Size = DL->getTypeSizeInBits(Val.getType());
Quentin Colombet17c494b2016-02-11 17:51:31 +000059 unsigned VReg = MRI->createGenericVirtualRegister(Size);
Quentin Colombetccd77252016-02-11 21:48:32 +000060 ValReg = VReg;
Tim Northover5ed648e2016-08-09 21:28:04 +000061
62 if (auto CV = dyn_cast<Constant>(&Val)) {
63 bool Success = translate(*CV, VReg);
Quentin Colombet3bb32cc2016-08-26 23:49:05 +000064 if (!Success) {
65 if (!TPC->isGlobalISelAbortEnabled()) {
66 MIRBuilder.getMF().getProperties().set(
67 MachineFunctionProperties::Property::FailedISel);
68 return 0;
69 }
Tim Northover5ed648e2016-08-09 21:28:04 +000070 report_fatal_error("unable to translate constant");
Quentin Colombet3bb32cc2016-08-26 23:49:05 +000071 }
Tim Northover5ed648e2016-08-09 21:28:04 +000072 }
Quentin Colombet17c494b2016-02-11 17:51:31 +000073 }
Quentin Colombetccd77252016-02-11 21:48:32 +000074 return ValReg;
Quentin Colombet17c494b2016-02-11 17:51:31 +000075}
76
Tim Northoverad2b7172016-07-26 20:23:26 +000077unsigned IRTranslator::getMemOpAlignment(const Instruction &I) {
78 unsigned Alignment = 0;
79 Type *ValTy = nullptr;
80 if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
81 Alignment = SI->getAlignment();
82 ValTy = SI->getValueOperand()->getType();
83 } else if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
84 Alignment = LI->getAlignment();
85 ValTy = LI->getType();
Quentin Colombet3bb32cc2016-08-26 23:49:05 +000086 } else if (!TPC->isGlobalISelAbortEnabled()) {
87 MIRBuilder.getMF().getProperties().set(
88 MachineFunctionProperties::Property::FailedISel);
89 return 1;
Tim Northoverad2b7172016-07-26 20:23:26 +000090 } else
91 llvm_unreachable("unhandled memory instruction");
92
93 return Alignment ? Alignment : DL->getABITypeAlignment(ValTy);
94}
95
Quentin Colombet53237a92016-03-11 17:27:43 +000096MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock &BB) {
97 MachineBasicBlock *&MBB = BBToMBB[&BB];
Quentin Colombet17c494b2016-02-11 17:51:31 +000098 if (!MBB) {
Quentin Colombeta7fae162016-02-11 17:53:23 +000099 MachineFunction &MF = MIRBuilder.getMF();
Quentin Colombet17c494b2016-02-11 17:51:31 +0000100 MBB = MF.CreateMachineBasicBlock();
101 MF.push_back(MBB);
102 }
103 return *MBB;
104}
105
Tim Northover357f1be2016-08-10 23:02:41 +0000106bool IRTranslator::translateBinaryOp(unsigned Opcode, const User &U) {
Tim Northover0d56e052016-07-29 18:11:21 +0000107 // FIXME: handle signed/unsigned wrapping flags.
108
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000109 // Get or create a virtual register for each value.
110 // Unless the value is a Constant => loadimm cst?
111 // or inline constant each time?
112 // Creation of a virtual register needs to have a size.
Tim Northover357f1be2016-08-10 23:02:41 +0000113 unsigned Op0 = getOrCreateVReg(*U.getOperand(0));
114 unsigned Op1 = getOrCreateVReg(*U.getOperand(1));
115 unsigned Res = getOrCreateVReg(U);
116 MIRBuilder.buildInstr(Opcode, LLT{*U.getType()})
Tim Northovera51575f2016-07-29 17:43:52 +0000117 .addDef(Res)
118 .addUse(Op0)
119 .addUse(Op1);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000120 return true;
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000121}
122
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000123bool IRTranslator::translateCompare(const User &U) {
124 const CmpInst *CI = dyn_cast<CmpInst>(&U);
125 unsigned Op0 = getOrCreateVReg(*U.getOperand(0));
126 unsigned Op1 = getOrCreateVReg(*U.getOperand(1));
127 unsigned Res = getOrCreateVReg(U);
128 CmpInst::Predicate Pred =
129 CI ? CI->getPredicate() : static_cast<CmpInst::Predicate>(
130 cast<ConstantExpr>(U).getPredicate());
Tim Northoverde3aea0412016-08-17 20:25:25 +0000131
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000132 if (CmpInst::isIntPredicate(Pred))
133 MIRBuilder.buildICmp(
134 {LLT{*U.getType()}, LLT{*U.getOperand(0)->getType()}}, Pred, Res, Op0,
135 Op1);
136 else
137 MIRBuilder.buildFCmp(
138 {LLT{*U.getType()}, LLT{*U.getOperand(0)->getType()}}, Pred, Res, Op0,
139 Op1);
140
Tim Northoverde3aea0412016-08-17 20:25:25 +0000141 return true;
142}
143
Tim Northover357f1be2016-08-10 23:02:41 +0000144bool IRTranslator::translateRet(const User &U) {
145 const ReturnInst &RI = cast<ReturnInst>(U);
Tim Northover0d56e052016-07-29 18:11:21 +0000146 const Value *Ret = RI.getReturnValue();
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000147 // The target may mess up with the insertion point, but
148 // this is not important as a return is the last instruction
149 // of the block anyway.
Tom Stellardb72a65f2016-04-14 17:23:33 +0000150 return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000151}
152
Tim Northover357f1be2016-08-10 23:02:41 +0000153bool IRTranslator::translateBr(const User &U) {
154 const BranchInst &BrInst = cast<BranchInst>(U);
Tim Northover69c2ba52016-07-29 17:58:00 +0000155 unsigned Succ = 0;
156 if (!BrInst.isUnconditional()) {
157 // We want a G_BRCOND to the true BB followed by an unconditional branch.
158 unsigned Tst = getOrCreateVReg(*BrInst.getCondition());
159 const BasicBlock &TrueTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ++));
160 MachineBasicBlock &TrueBB = getOrCreateBB(TrueTgt);
161 MIRBuilder.buildBrCond(LLT{*BrInst.getCondition()->getType()}, Tst, TrueBB);
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000162 }
Tim Northover69c2ba52016-07-29 17:58:00 +0000163
164 const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ));
165 MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt);
166 MIRBuilder.buildBr(TgtBB);
167
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000168 // Link successors.
169 MachineBasicBlock &CurBB = MIRBuilder.getMBB();
170 for (const BasicBlock *Succ : BrInst.successors())
171 CurBB.addSuccessor(&getOrCreateBB(*Succ));
172 return true;
173}
174
Tim Northover357f1be2016-08-10 23:02:41 +0000175bool IRTranslator::translateLoad(const User &U) {
176 const LoadInst &LI = cast<LoadInst>(U);
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000177
178 if (!TPC->isGlobalISelAbortEnabled() && !LI.isSimple())
179 return false;
180
Tim Northoverad2b7172016-07-26 20:23:26 +0000181 assert(LI.isSimple() && "only simple loads are supported at the moment");
182
183 MachineFunction &MF = MIRBuilder.getMF();
184 unsigned Res = getOrCreateVReg(LI);
185 unsigned Addr = getOrCreateVReg(*LI.getPointerOperand());
Tim Northover28fdc422016-08-15 21:13:17 +0000186 LLT VTy{*LI.getType(), DL}, PTy{*LI.getPointerOperand()->getType()};
Tim Northoverad2b7172016-07-26 20:23:26 +0000187
188 MIRBuilder.buildLoad(
189 VTy, PTy, Res, Addr,
Tim Northover28fdc422016-08-15 21:13:17 +0000190 *MF.getMachineMemOperand(
191 MachinePointerInfo(LI.getPointerOperand()), MachineMemOperand::MOLoad,
192 DL->getTypeStoreSize(LI.getType()), getMemOpAlignment(LI)));
Tim Northoverad2b7172016-07-26 20:23:26 +0000193 return true;
194}
195
Tim Northover357f1be2016-08-10 23:02:41 +0000196bool IRTranslator::translateStore(const User &U) {
197 const StoreInst &SI = cast<StoreInst>(U);
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000198
199 if (!TPC->isGlobalISelAbortEnabled() && !SI.isSimple())
200 return false;
201
Tim Northoverad2b7172016-07-26 20:23:26 +0000202 assert(SI.isSimple() && "only simple loads are supported at the moment");
203
204 MachineFunction &MF = MIRBuilder.getMF();
205 unsigned Val = getOrCreateVReg(*SI.getValueOperand());
206 unsigned Addr = getOrCreateVReg(*SI.getPointerOperand());
Tim Northover28fdc422016-08-15 21:13:17 +0000207 LLT VTy{*SI.getValueOperand()->getType(), DL},
Tim Northoverad2b7172016-07-26 20:23:26 +0000208 PTy{*SI.getPointerOperand()->getType()};
209
210 MIRBuilder.buildStore(
211 VTy, PTy, Val, Addr,
Tim Northover28fdc422016-08-15 21:13:17 +0000212 *MF.getMachineMemOperand(
213 MachinePointerInfo(SI.getPointerOperand()),
214 MachineMemOperand::MOStore,
215 DL->getTypeStoreSize(SI.getValueOperand()->getType()),
216 getMemOpAlignment(SI)));
Tim Northoverad2b7172016-07-26 20:23:26 +0000217 return true;
218}
219
Tim Northover6f80b082016-08-19 17:47:05 +0000220bool IRTranslator::translateExtractValue(const User &U) {
Tim Northoverb6046222016-08-19 20:09:03 +0000221 const Value *Src = U.getOperand(0);
222 Type *Int32Ty = Type::getInt32Ty(U.getContext());
Tim Northover6f80b082016-08-19 17:47:05 +0000223 SmallVector<Value *, 1> Indices;
224
225 // getIndexedOffsetInType is designed for GEPs, so the first index is the
226 // usual array element rather than looking into the actual aggregate.
227 Indices.push_back(ConstantInt::get(Int32Ty, 0));
Tim Northoverb6046222016-08-19 20:09:03 +0000228
229 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&U)) {
230 for (auto Idx : EVI->indices())
231 Indices.push_back(ConstantInt::get(Int32Ty, Idx));
232 } else {
233 for (unsigned i = 1; i < U.getNumOperands(); ++i)
234 Indices.push_back(U.getOperand(i));
235 }
Tim Northover6f80b082016-08-19 17:47:05 +0000236
237 uint64_t Offset = 8 * DL->getIndexedOffsetInType(Src->getType(), Indices);
238
Tim Northoverb6046222016-08-19 20:09:03 +0000239 unsigned Res = getOrCreateVReg(U);
240 MIRBuilder.buildExtract(LLT{*U.getType(), DL}, Res, Offset,
Tim Northover26b76f22016-08-19 18:32:14 +0000241 LLT{*Src->getType(), DL}, getOrCreateVReg(*Src));
Tim Northover6f80b082016-08-19 17:47:05 +0000242
243 return true;
244}
245
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000246bool IRTranslator::translateInsertValue(const User &U) {
Tim Northoverb6046222016-08-19 20:09:03 +0000247 const Value *Src = U.getOperand(0);
248 Type *Int32Ty = Type::getInt32Ty(U.getContext());
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000249 SmallVector<Value *, 1> Indices;
250
251 // getIndexedOffsetInType is designed for GEPs, so the first index is the
252 // usual array element rather than looking into the actual aggregate.
253 Indices.push_back(ConstantInt::get(Int32Ty, 0));
Tim Northoverb6046222016-08-19 20:09:03 +0000254
255 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&U)) {
256 for (auto Idx : IVI->indices())
257 Indices.push_back(ConstantInt::get(Int32Ty, Idx));
258 } else {
259 for (unsigned i = 2; i < U.getNumOperands(); ++i)
260 Indices.push_back(U.getOperand(i));
261 }
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000262
263 uint64_t Offset = 8 * DL->getIndexedOffsetInType(Src->getType(), Indices);
264
Tim Northoverb6046222016-08-19 20:09:03 +0000265 unsigned Res = getOrCreateVReg(U);
266 const Value &Inserted = *U.getOperand(1);
267 MIRBuilder.buildInsert(LLT{*U.getType(), DL}, Res, getOrCreateVReg(*Src),
268 LLT{*Inserted.getType(), DL},
269 getOrCreateVReg(Inserted), Offset);
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000270
271 return true;
272}
273
Tim Northover5a28c362016-08-19 20:09:07 +0000274bool IRTranslator::translateSelect(const User &U) {
275 MIRBuilder.buildSelect(
276 LLT{*U.getType()}, getOrCreateVReg(U), getOrCreateVReg(*U.getOperand(0)),
277 getOrCreateVReg(*U.getOperand(1)), getOrCreateVReg(*U.getOperand(2)));
278 return true;
279}
280
Tim Northover357f1be2016-08-10 23:02:41 +0000281bool IRTranslator::translateBitCast(const User &U) {
282 if (LLT{*U.getOperand(0)->getType()} == LLT{*U.getType()}) {
283 unsigned &Reg = ValToVReg[&U];
Tim Northover7552ef52016-08-10 16:51:14 +0000284 if (Reg)
Tim Northover357f1be2016-08-10 23:02:41 +0000285 MIRBuilder.buildCopy(Reg, getOrCreateVReg(*U.getOperand(0)));
Tim Northover7552ef52016-08-10 16:51:14 +0000286 else
Tim Northover357f1be2016-08-10 23:02:41 +0000287 Reg = getOrCreateVReg(*U.getOperand(0));
Tim Northover7c9eba92016-07-25 21:01:29 +0000288 return true;
289 }
Tim Northover357f1be2016-08-10 23:02:41 +0000290 return translateCast(TargetOpcode::G_BITCAST, U);
Tim Northover7c9eba92016-07-25 21:01:29 +0000291}
292
Tim Northover357f1be2016-08-10 23:02:41 +0000293bool IRTranslator::translateCast(unsigned Opcode, const User &U) {
294 unsigned Op = getOrCreateVReg(*U.getOperand(0));
295 unsigned Res = getOrCreateVReg(U);
296 MIRBuilder
297 .buildInstr(Opcode, {LLT{*U.getType()}, LLT{*U.getOperand(0)->getType()}})
Tim Northovera51575f2016-07-29 17:43:52 +0000298 .addDef(Res)
299 .addUse(Op);
Tim Northover7c9eba92016-07-25 21:01:29 +0000300 return true;
301}
302
Tim Northover91c81732016-08-19 17:17:06 +0000303bool IRTranslator::translateKnownIntrinsic(const CallInst &CI,
304 Intrinsic::ID ID) {
305 unsigned Op = 0;
306 switch (ID) {
307 default: return false;
308 case Intrinsic::uadd_with_overflow: Op = TargetOpcode::G_UADDE; break;
309 case Intrinsic::sadd_with_overflow: Op = TargetOpcode::G_SADDO; break;
310 case Intrinsic::usub_with_overflow: Op = TargetOpcode::G_USUBE; break;
311 case Intrinsic::ssub_with_overflow: Op = TargetOpcode::G_SSUBO; break;
312 case Intrinsic::umul_with_overflow: Op = TargetOpcode::G_UMULO; break;
313 case Intrinsic::smul_with_overflow: Op = TargetOpcode::G_SMULO; break;
314 }
315
316 LLT Ty{*CI.getOperand(0)->getType()};
317 LLT s1 = LLT::scalar(1);
318 unsigned Width = Ty.getSizeInBits();
319 unsigned Res = MRI->createGenericVirtualRegister(Width);
320 unsigned Overflow = MRI->createGenericVirtualRegister(1);
321 auto MIB = MIRBuilder.buildInstr(Op, {Ty, s1})
322 .addDef(Res)
323 .addDef(Overflow)
324 .addUse(getOrCreateVReg(*CI.getOperand(0)))
325 .addUse(getOrCreateVReg(*CI.getOperand(1)));
326
327 if (Op == TargetOpcode::G_UADDE || Op == TargetOpcode::G_USUBE) {
328 unsigned Zero = MRI->createGenericVirtualRegister(1);
329 EntryBuilder.buildConstant(s1, Zero, 0);
330 MIB.addUse(Zero);
331 }
332
Tim Northover26b76f22016-08-19 18:32:14 +0000333 MIRBuilder.buildSequence(LLT{*CI.getType(), DL}, getOrCreateVReg(CI), Ty, Res,
334 0, s1, Overflow, Width);
Tim Northover91c81732016-08-19 17:17:06 +0000335 return true;
336}
337
Tim Northover357f1be2016-08-10 23:02:41 +0000338bool IRTranslator::translateCall(const User &U) {
339 const CallInst &CI = cast<CallInst>(U);
Tim Northover5fb414d2016-07-29 22:32:36 +0000340 auto TII = MIRBuilder.getMF().getTarget().getIntrinsicInfo();
Tim Northover406024a2016-08-10 21:44:01 +0000341 const Function *F = CI.getCalledFunction();
Tim Northover5fb414d2016-07-29 22:32:36 +0000342
Tim Northover406024a2016-08-10 21:44:01 +0000343 if (!F || !F->isIntrinsic()) {
344 // FIXME: handle multiple return values.
345 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
346 SmallVector<unsigned, 8> Args;
347 for (auto &Arg: CI.arg_operands())
348 Args.push_back(getOrCreateVReg(*Arg));
349
350 return CLI->lowerCall(MIRBuilder, CI,
351 F ? 0 : getOrCreateVReg(*CI.getCalledValue()), Res,
352 Args);
353 }
354
355 Intrinsic::ID ID = F->getIntrinsicID();
356 if (TII && ID == Intrinsic::not_intrinsic)
357 ID = static_cast<Intrinsic::ID>(TII->getIntrinsicID(F));
358
359 assert(ID != Intrinsic::not_intrinsic && "unknown intrinsic");
Tim Northover5fb414d2016-07-29 22:32:36 +0000360
Tim Northover91c81732016-08-19 17:17:06 +0000361 if (translateKnownIntrinsic(CI, ID))
362 return true;
363
Tim Northover5fb414d2016-07-29 22:32:36 +0000364 // Need types (starting with return) & args.
365 SmallVector<LLT, 4> Tys;
366 Tys.emplace_back(*CI.getType());
367 for (auto &Arg : CI.arg_operands())
368 Tys.emplace_back(*Arg->getType());
369
370 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
371 MachineInstrBuilder MIB =
372 MIRBuilder.buildIntrinsic(Tys, ID, Res, !CI.doesNotAccessMemory());
373
374 for (auto &Arg : CI.arg_operands()) {
375 if (ConstantInt *CI = dyn_cast<ConstantInt>(Arg))
376 MIB.addImm(CI->getSExtValue());
377 else
378 MIB.addUse(getOrCreateVReg(*Arg));
379 }
380 return true;
381}
382
Tim Northoverbd505462016-07-22 16:59:52 +0000383bool IRTranslator::translateStaticAlloca(const AllocaInst &AI) {
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000384 if (!TPC->isGlobalISelAbortEnabled() && !AI.isStaticAlloca())
385 return false;
386
Tim Northoverbd505462016-07-22 16:59:52 +0000387 assert(AI.isStaticAlloca() && "only handle static allocas now");
388 MachineFunction &MF = MIRBuilder.getMF();
389 unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
390 unsigned Size =
391 ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
392
Tim Northover8d2f52e2016-07-27 17:47:54 +0000393 // Always allocate at least one byte.
394 Size = std::max(Size, 1u);
395
Tim Northoverbd505462016-07-22 16:59:52 +0000396 unsigned Alignment = AI.getAlignment();
397 if (!Alignment)
398 Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
399
400 unsigned Res = getOrCreateVReg(AI);
Matthias Braun93320392016-07-28 20:13:42 +0000401 int FI = MF.getFrameInfo().CreateStackObject(Size, Alignment, false, &AI);
Tim Northoverbd505462016-07-22 16:59:52 +0000402 MIRBuilder.buildFrameIndex(LLT::pointer(0), Res, FI);
403 return true;
404}
405
Tim Northover357f1be2016-08-10 23:02:41 +0000406bool IRTranslator::translatePHI(const User &U) {
407 const PHINode &PI = cast<PHINode>(U);
Tim Northover97d0cb32016-08-05 17:16:40 +0000408 MachineInstrBuilder MIB = MIRBuilder.buildInstr(TargetOpcode::PHI);
409 MIB.addDef(getOrCreateVReg(PI));
410
411 PendingPHIs.emplace_back(&PI, MIB.getInstr());
412 return true;
413}
414
415void IRTranslator::finishPendingPhis() {
416 for (std::pair<const PHINode *, MachineInstr *> &Phi : PendingPHIs) {
417 const PHINode *PI = Phi.first;
418 MachineInstrBuilder MIB(MIRBuilder.getMF(), Phi.second);
419
420 // All MachineBasicBlocks exist, add them to the PHI. We assume IRTranslator
421 // won't create extra control flow here, otherwise we need to find the
422 // dominating predecessor here (or perhaps force the weirder IRTranslators
423 // to provide a simple boundary).
424 for (unsigned i = 0; i < PI->getNumIncomingValues(); ++i) {
425 assert(BBToMBB[PI->getIncomingBlock(i)]->isSuccessor(MIB->getParent()) &&
426 "I appear to have misunderstood Machine PHIs");
427 MIB.addUse(getOrCreateVReg(*PI->getIncomingValue(i)));
428 MIB.addMBB(BBToMBB[PI->getIncomingBlock(i)]);
429 }
430 }
Tim Northover14e7f732016-08-05 17:50:36 +0000431
432 PendingPHIs.clear();
Tim Northover97d0cb32016-08-05 17:16:40 +0000433}
434
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000435bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombeta7fae162016-02-11 17:53:23 +0000436 MIRBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000437 switch(Inst.getOpcode()) {
Tim Northover357f1be2016-08-10 23:02:41 +0000438#define HANDLE_INST(NUM, OPCODE, CLASS) \
439 case Instruction::OPCODE: return translate##OPCODE(Inst);
440#include "llvm/IR/Instruction.def"
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000441 default:
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000442 if (!TPC->isGlobalISelAbortEnabled())
443 return false;
Tim Northover357f1be2016-08-10 23:02:41 +0000444 llvm_unreachable("unknown opcode");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000445 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000446}
447
Tim Northover5ed648e2016-08-09 21:28:04 +0000448bool IRTranslator::translate(const Constant &C, unsigned Reg) {
Tim Northoverd403a3d2016-08-09 23:01:30 +0000449 if (auto CI = dyn_cast<ConstantInt>(&C))
Tim Northover5ed648e2016-08-09 21:28:04 +0000450 EntryBuilder.buildConstant(LLT{*CI->getType()}, Reg, CI->getZExtValue());
Tim Northoverb16734f2016-08-19 20:09:15 +0000451 else if (auto CF = dyn_cast<ConstantFP>(&C))
452 EntryBuilder.buildFConstant(LLT{*CF->getType()}, Reg, *CF);
Tim Northoverd403a3d2016-08-09 23:01:30 +0000453 else if (isa<UndefValue>(C))
454 EntryBuilder.buildInstr(TargetOpcode::IMPLICIT_DEF).addDef(Reg);
Tim Northover8e0c53a2016-08-11 21:40:55 +0000455 else if (isa<ConstantPointerNull>(C))
456 EntryBuilder.buildInstr(TargetOpcode::G_CONSTANT, LLT{*C.getType()})
457 .addDef(Reg)
458 .addImm(0);
Tim Northover357f1be2016-08-10 23:02:41 +0000459 else if (auto CE = dyn_cast<ConstantExpr>(&C)) {
460 switch(CE->getOpcode()) {
461#define HANDLE_INST(NUM, OPCODE, CLASS) \
462 case Instruction::OPCODE: return translate##OPCODE(*CE);
463#include "llvm/IR/Instruction.def"
464 default:
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000465 if (!TPC->isGlobalISelAbortEnabled())
466 return false;
Tim Northover357f1be2016-08-10 23:02:41 +0000467 llvm_unreachable("unknown opcode");
468 }
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000469 } else if (!TPC->isGlobalISelAbortEnabled())
470 return false;
471 else
Tim Northoverd403a3d2016-08-09 23:01:30 +0000472 llvm_unreachable("unhandled constant kind");
Tim Northover5ed648e2016-08-09 21:28:04 +0000473
Tim Northoverd403a3d2016-08-09 23:01:30 +0000474 return true;
Tim Northover5ed648e2016-08-09 21:28:04 +0000475}
476
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000477
Tim Northover0d510442016-08-11 16:21:29 +0000478void IRTranslator::finalizeFunction() {
479 finishPendingPhis();
480
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000481 // Release the memory used by the different maps we
482 // needed during the translation.
Quentin Colombetccd77252016-02-11 21:48:32 +0000483 ValToVReg.clear();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000484 Constants.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000485}
486
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000487bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000488 const Function &F = *MF.getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000489 if (F.empty())
490 return false;
Quentin Colombetba2a0162016-02-16 19:26:02 +0000491 CLI = MF.getSubtarget().getCallLowering();
Quentin Colombet000b5802016-03-11 17:27:51 +0000492 MIRBuilder.setMF(MF);
Tim Northover5ed648e2016-08-09 21:28:04 +0000493 EntryBuilder.setMF(MF);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000494 MRI = &MF.getRegInfo();
Tim Northoverbd505462016-07-22 16:59:52 +0000495 DL = &F.getParent()->getDataLayout();
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000496 TPC = &getAnalysis<TargetPassConfig>();
Tim Northoverbd505462016-07-22 16:59:52 +0000497
Tim Northover14e7f732016-08-05 17:50:36 +0000498 assert(PendingPHIs.empty() && "stale PHIs");
499
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000500 // Setup the arguments.
Quentin Colombet53237a92016-03-11 17:27:43 +0000501 MachineBasicBlock &MBB = getOrCreateBB(F.front());
Quentin Colombet91ebd712016-03-11 17:27:47 +0000502 MIRBuilder.setMBB(MBB);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000503 SmallVector<unsigned, 8> VRegArgs;
504 for (const Argument &Arg: F.args())
Quentin Colombete225e252016-03-11 17:27:54 +0000505 VRegArgs.push_back(getOrCreateVReg(Arg));
Quentin Colombetba2a0162016-02-16 19:26:02 +0000506 bool Succeeded =
Tom Stellardb72a65f2016-04-14 17:23:33 +0000507 CLI->lowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs);
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000508 if (!Succeeded) {
509 if (!TPC->isGlobalISelAbortEnabled()) {
510 MIRBuilder.getMF().getProperties().set(
511 MachineFunctionProperties::Property::FailedISel);
512 return false;
513 }
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000514 report_fatal_error("Unable to lower arguments");
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000515 }
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000516
Tim Northover5ed648e2016-08-09 21:28:04 +0000517 // Now that we've got the ABI handling code, it's safe to set a location for
518 // any Constants we find in the IR.
519 if (MBB.empty())
520 EntryBuilder.setMBB(MBB);
521 else
522 EntryBuilder.setInstr(MBB.back(), /* Before */ false);
523
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000524 for (const BasicBlock &BB: F) {
Quentin Colombet53237a92016-03-11 17:27:43 +0000525 MachineBasicBlock &MBB = getOrCreateBB(BB);
Quentin Colombet91ebd712016-03-11 17:27:47 +0000526 // Set the insertion point of all the following translations to
527 // the end of this basic block.
528 MIRBuilder.setMBB(MBB);
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000529 for (const Instruction &Inst: BB) {
530 bool Succeeded = translate(Inst);
531 if (!Succeeded) {
532 DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000533 if (TPC->isGlobalISelAbortEnabled())
534 report_fatal_error("Unable to translate instruction");
535 MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
536 break;
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000537 }
538 }
539 }
Tim Northover72eebfa2016-07-12 22:23:42 +0000540
Tim Northover0d510442016-08-11 16:21:29 +0000541 finalizeFunction();
Tim Northover97d0cb32016-08-05 17:16:40 +0000542
Tim Northover72eebfa2016-07-12 22:23:42 +0000543 // Now that the MachineFrameInfo has been configured, no further changes to
544 // the reserved registers are possible.
545 MRI->freezeReservedRegs(MF);
546
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000547 return false;
548}