blob: 155a39a044e6ad7c0359acbee16bc437677fe5d7 [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 Northovera7653b32016-09-12 11:20:22 +000023#include "llvm/IR/GetElementPtrTypeIterator.h"
Tim Northover5fb414d2016-07-29 22:32:36 +000024#include "llvm/IR/IntrinsicInst.h"
Quentin Colombet17c494b2016-02-11 17:51:31 +000025#include "llvm/IR/Type.h"
26#include "llvm/IR/Value.h"
Tim Northover5fb414d2016-07-29 22:32:36 +000027#include "llvm/Target/TargetIntrinsicInfo.h"
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000028#include "llvm/Target/TargetLowering.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000029
30#define DEBUG_TYPE "irtranslator"
31
Quentin Colombet105cf2b2016-01-20 20:58:56 +000032using namespace llvm;
33
34char IRTranslator::ID = 0;
Quentin Colombet3bb32cc2016-08-26 23:49:05 +000035INITIALIZE_PASS_BEGIN(IRTranslator, DEBUG_TYPE, "IRTranslator LLVM IR -> MI",
36 false, false)
37INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
38INITIALIZE_PASS_END(IRTranslator, DEBUG_TYPE, "IRTranslator LLVM IR -> MI",
Tim Northover884b47e2016-07-26 03:29:18 +000039 false, false)
Quentin Colombet105cf2b2016-01-20 20:58:56 +000040
Quentin Colombeta7fae162016-02-11 17:53:23 +000041IRTranslator::IRTranslator() : MachineFunctionPass(ID), MRI(nullptr) {
Quentin Colombet39293d32016-03-08 01:38:55 +000042 initializeIRTranslatorPass(*PassRegistry::getPassRegistry());
Quentin Colombeta7fae162016-02-11 17:53:23 +000043}
44
Quentin Colombet3bb32cc2016-08-26 23:49:05 +000045void IRTranslator::getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.addRequired<TargetPassConfig>();
47 MachineFunctionPass::getAnalysisUsage(AU);
48}
49
50
Quentin Colombete225e252016-03-11 17:27:54 +000051unsigned IRTranslator::getOrCreateVReg(const Value &Val) {
52 unsigned &ValReg = ValToVReg[&Val];
Quentin Colombet17c494b2016-02-11 17:51:31 +000053 // Check if this is the first time we see Val.
Quentin Colombetccd77252016-02-11 21:48:32 +000054 if (!ValReg) {
Quentin Colombet17c494b2016-02-11 17:51:31 +000055 // Fill ValRegsSequence with the sequence of registers
56 // we need to concat together to produce the value.
Quentin Colombete225e252016-03-11 17:27:54 +000057 assert(Val.getType()->isSized() &&
Quentin Colombet17c494b2016-02-11 17:51:31 +000058 "Don't know how to create an empty vreg");
Tim Northover5ae83502016-09-15 09:20:34 +000059 unsigned VReg = MRI->createGenericVirtualRegister(LLT{*Val.getType(), *DL});
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);
Tim Northover0f140c72016-09-09 11:46:34 +0000116 MIRBuilder.buildInstr(Opcode).addDef(Res).addUse(Op0).addUse(Op1);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000117 return true;
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000118}
119
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000120bool IRTranslator::translateCompare(const User &U) {
121 const CmpInst *CI = dyn_cast<CmpInst>(&U);
122 unsigned Op0 = getOrCreateVReg(*U.getOperand(0));
123 unsigned Op1 = getOrCreateVReg(*U.getOperand(1));
124 unsigned Res = getOrCreateVReg(U);
125 CmpInst::Predicate Pred =
126 CI ? CI->getPredicate() : static_cast<CmpInst::Predicate>(
127 cast<ConstantExpr>(U).getPredicate());
Tim Northoverde3aea0412016-08-17 20:25:25 +0000128
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000129 if (CmpInst::isIntPredicate(Pred))
Tim Northover0f140c72016-09-09 11:46:34 +0000130 MIRBuilder.buildICmp(Pred, Res, Op0, Op1);
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000131 else
Tim Northover0f140c72016-09-09 11:46:34 +0000132 MIRBuilder.buildFCmp(Pred, Res, Op0, Op1);
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000133
Tim Northoverde3aea0412016-08-17 20:25:25 +0000134 return true;
135}
136
Tim Northover357f1be2016-08-10 23:02:41 +0000137bool IRTranslator::translateRet(const User &U) {
138 const ReturnInst &RI = cast<ReturnInst>(U);
Tim Northover0d56e052016-07-29 18:11:21 +0000139 const Value *Ret = RI.getReturnValue();
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000140 // The target may mess up with the insertion point, but
141 // this is not important as a return is the last instruction
142 // of the block anyway.
Tom Stellardb72a65f2016-04-14 17:23:33 +0000143 return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000144}
145
Tim Northover357f1be2016-08-10 23:02:41 +0000146bool IRTranslator::translateBr(const User &U) {
147 const BranchInst &BrInst = cast<BranchInst>(U);
Tim Northover69c2ba52016-07-29 17:58:00 +0000148 unsigned Succ = 0;
149 if (!BrInst.isUnconditional()) {
150 // We want a G_BRCOND to the true BB followed by an unconditional branch.
151 unsigned Tst = getOrCreateVReg(*BrInst.getCondition());
152 const BasicBlock &TrueTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ++));
153 MachineBasicBlock &TrueBB = getOrCreateBB(TrueTgt);
Tim Northover0f140c72016-09-09 11:46:34 +0000154 MIRBuilder.buildBrCond(Tst, TrueBB);
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000155 }
Tim Northover69c2ba52016-07-29 17:58:00 +0000156
157 const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ));
158 MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt);
159 MIRBuilder.buildBr(TgtBB);
160
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000161 // Link successors.
162 MachineBasicBlock &CurBB = MIRBuilder.getMBB();
163 for (const BasicBlock *Succ : BrInst.successors())
164 CurBB.addSuccessor(&getOrCreateBB(*Succ));
165 return true;
166}
167
Tim Northover357f1be2016-08-10 23:02:41 +0000168bool IRTranslator::translateLoad(const User &U) {
169 const LoadInst &LI = cast<LoadInst>(U);
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000170
171 if (!TPC->isGlobalISelAbortEnabled() && !LI.isSimple())
172 return false;
173
Tim Northoverad2b7172016-07-26 20:23:26 +0000174 assert(LI.isSimple() && "only simple loads are supported at the moment");
175
176 MachineFunction &MF = MIRBuilder.getMF();
177 unsigned Res = getOrCreateVReg(LI);
178 unsigned Addr = getOrCreateVReg(*LI.getPointerOperand());
Tim Northover5ae83502016-09-15 09:20:34 +0000179 LLT VTy{*LI.getType(), *DL}, PTy{*LI.getPointerOperand()->getType(), *DL};
Tim Northoverad2b7172016-07-26 20:23:26 +0000180
181 MIRBuilder.buildLoad(
Tim Northover0f140c72016-09-09 11:46:34 +0000182 Res, Addr,
Tim Northover28fdc422016-08-15 21:13:17 +0000183 *MF.getMachineMemOperand(
184 MachinePointerInfo(LI.getPointerOperand()), MachineMemOperand::MOLoad,
185 DL->getTypeStoreSize(LI.getType()), getMemOpAlignment(LI)));
Tim Northoverad2b7172016-07-26 20:23:26 +0000186 return true;
187}
188
Tim Northover357f1be2016-08-10 23:02:41 +0000189bool IRTranslator::translateStore(const User &U) {
190 const StoreInst &SI = cast<StoreInst>(U);
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000191
192 if (!TPC->isGlobalISelAbortEnabled() && !SI.isSimple())
193 return false;
194
Tim Northoverad2b7172016-07-26 20:23:26 +0000195 assert(SI.isSimple() && "only simple loads are supported at the moment");
196
197 MachineFunction &MF = MIRBuilder.getMF();
198 unsigned Val = getOrCreateVReg(*SI.getValueOperand());
199 unsigned Addr = getOrCreateVReg(*SI.getPointerOperand());
Tim Northover5ae83502016-09-15 09:20:34 +0000200 LLT VTy{*SI.getValueOperand()->getType(), *DL},
201 PTy{*SI.getPointerOperand()->getType(), *DL};
Tim Northoverad2b7172016-07-26 20:23:26 +0000202
203 MIRBuilder.buildStore(
Tim Northover0f140c72016-09-09 11:46:34 +0000204 Val, Addr,
Tim Northover28fdc422016-08-15 21:13:17 +0000205 *MF.getMachineMemOperand(
206 MachinePointerInfo(SI.getPointerOperand()),
207 MachineMemOperand::MOStore,
208 DL->getTypeStoreSize(SI.getValueOperand()->getType()),
209 getMemOpAlignment(SI)));
Tim Northoverad2b7172016-07-26 20:23:26 +0000210 return true;
211}
212
Tim Northover6f80b082016-08-19 17:47:05 +0000213bool IRTranslator::translateExtractValue(const User &U) {
Tim Northoverb6046222016-08-19 20:09:03 +0000214 const Value *Src = U.getOperand(0);
215 Type *Int32Ty = Type::getInt32Ty(U.getContext());
Tim Northover6f80b082016-08-19 17:47:05 +0000216 SmallVector<Value *, 1> Indices;
217
218 // getIndexedOffsetInType is designed for GEPs, so the first index is the
219 // usual array element rather than looking into the actual aggregate.
220 Indices.push_back(ConstantInt::get(Int32Ty, 0));
Tim Northoverb6046222016-08-19 20:09:03 +0000221
222 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&U)) {
223 for (auto Idx : EVI->indices())
224 Indices.push_back(ConstantInt::get(Int32Ty, Idx));
225 } else {
226 for (unsigned i = 1; i < U.getNumOperands(); ++i)
227 Indices.push_back(U.getOperand(i));
228 }
Tim Northover6f80b082016-08-19 17:47:05 +0000229
230 uint64_t Offset = 8 * DL->getIndexedOffsetInType(Src->getType(), Indices);
231
Tim Northoverb6046222016-08-19 20:09:03 +0000232 unsigned Res = getOrCreateVReg(U);
Tim Northover0f140c72016-09-09 11:46:34 +0000233 MIRBuilder.buildExtract(Res, Offset, getOrCreateVReg(*Src));
Tim Northover6f80b082016-08-19 17:47:05 +0000234
235 return true;
236}
237
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000238bool IRTranslator::translateInsertValue(const User &U) {
Tim Northoverb6046222016-08-19 20:09:03 +0000239 const Value *Src = U.getOperand(0);
240 Type *Int32Ty = Type::getInt32Ty(U.getContext());
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000241 SmallVector<Value *, 1> Indices;
242
243 // getIndexedOffsetInType is designed for GEPs, so the first index is the
244 // usual array element rather than looking into the actual aggregate.
245 Indices.push_back(ConstantInt::get(Int32Ty, 0));
Tim Northoverb6046222016-08-19 20:09:03 +0000246
247 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&U)) {
248 for (auto Idx : IVI->indices())
249 Indices.push_back(ConstantInt::get(Int32Ty, Idx));
250 } else {
251 for (unsigned i = 2; i < U.getNumOperands(); ++i)
252 Indices.push_back(U.getOperand(i));
253 }
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000254
255 uint64_t Offset = 8 * DL->getIndexedOffsetInType(Src->getType(), Indices);
256
Tim Northoverb6046222016-08-19 20:09:03 +0000257 unsigned Res = getOrCreateVReg(U);
258 const Value &Inserted = *U.getOperand(1);
Tim Northover0f140c72016-09-09 11:46:34 +0000259 MIRBuilder.buildInsert(Res, getOrCreateVReg(*Src), getOrCreateVReg(Inserted),
260 Offset);
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000261
262 return true;
263}
264
Tim Northover5a28c362016-08-19 20:09:07 +0000265bool IRTranslator::translateSelect(const User &U) {
Tim Northover0f140c72016-09-09 11:46:34 +0000266 MIRBuilder.buildSelect(getOrCreateVReg(U), getOrCreateVReg(*U.getOperand(0)),
267 getOrCreateVReg(*U.getOperand(1)),
268 getOrCreateVReg(*U.getOperand(2)));
Tim Northover5a28c362016-08-19 20:09:07 +0000269 return true;
270}
271
Tim Northover357f1be2016-08-10 23:02:41 +0000272bool IRTranslator::translateBitCast(const User &U) {
Tim Northover5ae83502016-09-15 09:20:34 +0000273 if (LLT{*U.getOperand(0)->getType(), *DL} == LLT{*U.getType(), *DL}) {
Tim Northover357f1be2016-08-10 23:02:41 +0000274 unsigned &Reg = ValToVReg[&U];
Tim Northover7552ef52016-08-10 16:51:14 +0000275 if (Reg)
Tim Northover357f1be2016-08-10 23:02:41 +0000276 MIRBuilder.buildCopy(Reg, getOrCreateVReg(*U.getOperand(0)));
Tim Northover7552ef52016-08-10 16:51:14 +0000277 else
Tim Northover357f1be2016-08-10 23:02:41 +0000278 Reg = getOrCreateVReg(*U.getOperand(0));
Tim Northover7c9eba92016-07-25 21:01:29 +0000279 return true;
280 }
Tim Northover357f1be2016-08-10 23:02:41 +0000281 return translateCast(TargetOpcode::G_BITCAST, U);
Tim Northover7c9eba92016-07-25 21:01:29 +0000282}
283
Tim Northover357f1be2016-08-10 23:02:41 +0000284bool IRTranslator::translateCast(unsigned Opcode, const User &U) {
285 unsigned Op = getOrCreateVReg(*U.getOperand(0));
286 unsigned Res = getOrCreateVReg(U);
Tim Northover0f140c72016-09-09 11:46:34 +0000287 MIRBuilder.buildInstr(Opcode).addDef(Res).addUse(Op);
Tim Northover7c9eba92016-07-25 21:01:29 +0000288 return true;
289}
290
Tim Northovera7653b32016-09-12 11:20:22 +0000291bool IRTranslator::translateGetElementPtr(const User &U) {
292 // FIXME: support vector GEPs.
293 if (U.getType()->isVectorTy())
294 return false;
295
296 Value &Op0 = *U.getOperand(0);
297 unsigned BaseReg = getOrCreateVReg(Op0);
Tim Northover5ae83502016-09-15 09:20:34 +0000298 LLT PtrTy{*Op0.getType(), *DL};
Tim Northovera7653b32016-09-12 11:20:22 +0000299 unsigned PtrSize = DL->getPointerSizeInBits(PtrTy.getAddressSpace());
300 LLT OffsetTy = LLT::scalar(PtrSize);
301
302 int64_t Offset = 0;
303 for (gep_type_iterator GTI = gep_type_begin(&U), E = gep_type_end(&U);
304 GTI != E; ++GTI) {
305 const Value *Idx = GTI.getOperand();
306 if (StructType *StTy = dyn_cast<StructType>(*GTI)) {
307 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
308 Offset += DL->getStructLayout(StTy)->getElementOffset(Field);
309 continue;
310 } else {
311 uint64_t ElementSize = DL->getTypeAllocSize(GTI.getIndexedType());
312
313 // If this is a scalar constant or a splat vector of constants,
314 // handle it quickly.
315 if (const auto *CI = dyn_cast<ConstantInt>(Idx)) {
316 Offset += ElementSize * CI->getSExtValue();
317 continue;
318 }
319
320 if (Offset != 0) {
321 unsigned NewBaseReg = MRI->createGenericVirtualRegister(PtrTy);
322 unsigned OffsetReg = MRI->createGenericVirtualRegister(OffsetTy);
323 MIRBuilder.buildConstant(OffsetReg, Offset);
324 MIRBuilder.buildGEP(NewBaseReg, BaseReg, OffsetReg);
325
326 BaseReg = NewBaseReg;
327 Offset = 0;
328 }
329
330 // N = N + Idx * ElementSize;
331 unsigned ElementSizeReg = MRI->createGenericVirtualRegister(OffsetTy);
332 MIRBuilder.buildConstant(ElementSizeReg, ElementSize);
333
334 unsigned IdxReg = getOrCreateVReg(*Idx);
335 if (MRI->getType(IdxReg) != OffsetTy) {
336 unsigned NewIdxReg = MRI->createGenericVirtualRegister(OffsetTy);
337 MIRBuilder.buildSExtOrTrunc(NewIdxReg, IdxReg);
338 IdxReg = NewIdxReg;
339 }
340
341 unsigned OffsetReg = MRI->createGenericVirtualRegister(OffsetTy);
342 MIRBuilder.buildMul(OffsetReg, ElementSizeReg, IdxReg);
343
344 unsigned NewBaseReg = MRI->createGenericVirtualRegister(PtrTy);
345 MIRBuilder.buildGEP(NewBaseReg, BaseReg, OffsetReg);
346 BaseReg = NewBaseReg;
347 }
348 }
349
350 if (Offset != 0) {
351 unsigned OffsetReg = MRI->createGenericVirtualRegister(OffsetTy);
352 MIRBuilder.buildConstant(OffsetReg, Offset);
353 MIRBuilder.buildGEP(getOrCreateVReg(U), BaseReg, OffsetReg);
354 return true;
355 }
356
357 MIRBuilder.buildCopy(getOrCreateVReg(U), BaseReg);
358 return true;
359}
360
361
Tim Northover91c81732016-08-19 17:17:06 +0000362bool IRTranslator::translateKnownIntrinsic(const CallInst &CI,
363 Intrinsic::ID ID) {
364 unsigned Op = 0;
365 switch (ID) {
366 default: return false;
367 case Intrinsic::uadd_with_overflow: Op = TargetOpcode::G_UADDE; break;
368 case Intrinsic::sadd_with_overflow: Op = TargetOpcode::G_SADDO; break;
369 case Intrinsic::usub_with_overflow: Op = TargetOpcode::G_USUBE; break;
370 case Intrinsic::ssub_with_overflow: Op = TargetOpcode::G_SSUBO; break;
371 case Intrinsic::umul_with_overflow: Op = TargetOpcode::G_UMULO; break;
372 case Intrinsic::smul_with_overflow: Op = TargetOpcode::G_SMULO; break;
373 }
374
Tim Northover5ae83502016-09-15 09:20:34 +0000375 LLT Ty{*CI.getOperand(0)->getType(), *DL};
Tim Northover91c81732016-08-19 17:17:06 +0000376 LLT s1 = LLT::scalar(1);
377 unsigned Width = Ty.getSizeInBits();
Tim Northover0f140c72016-09-09 11:46:34 +0000378 unsigned Res = MRI->createGenericVirtualRegister(Ty);
379 unsigned Overflow = MRI->createGenericVirtualRegister(s1);
380 auto MIB = MIRBuilder.buildInstr(Op)
Tim Northover91c81732016-08-19 17:17:06 +0000381 .addDef(Res)
382 .addDef(Overflow)
383 .addUse(getOrCreateVReg(*CI.getOperand(0)))
384 .addUse(getOrCreateVReg(*CI.getOperand(1)));
385
386 if (Op == TargetOpcode::G_UADDE || Op == TargetOpcode::G_USUBE) {
Tim Northover0f140c72016-09-09 11:46:34 +0000387 unsigned Zero = MRI->createGenericVirtualRegister(s1);
388 EntryBuilder.buildConstant(Zero, 0);
Tim Northover91c81732016-08-19 17:17:06 +0000389 MIB.addUse(Zero);
390 }
391
Tim Northover0f140c72016-09-09 11:46:34 +0000392 MIRBuilder.buildSequence(getOrCreateVReg(CI), Res, 0, Overflow, Width);
Tim Northover91c81732016-08-19 17:17:06 +0000393 return true;
394}
395
Tim Northover357f1be2016-08-10 23:02:41 +0000396bool IRTranslator::translateCall(const User &U) {
397 const CallInst &CI = cast<CallInst>(U);
Tim Northover5fb414d2016-07-29 22:32:36 +0000398 auto TII = MIRBuilder.getMF().getTarget().getIntrinsicInfo();
Tim Northover406024a2016-08-10 21:44:01 +0000399 const Function *F = CI.getCalledFunction();
Tim Northover5fb414d2016-07-29 22:32:36 +0000400
Tim Northover406024a2016-08-10 21:44:01 +0000401 if (!F || !F->isIntrinsic()) {
Tim Northover406024a2016-08-10 21:44:01 +0000402 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
403 SmallVector<unsigned, 8> Args;
404 for (auto &Arg: CI.arg_operands())
405 Args.push_back(getOrCreateVReg(*Arg));
406
Tim Northoverfe5f89b2016-08-29 19:07:08 +0000407 return CLI->lowerCall(MIRBuilder, CI, Res, Args, [&]() {
408 return getOrCreateVReg(*CI.getCalledValue());
409 });
Tim Northover406024a2016-08-10 21:44:01 +0000410 }
411
412 Intrinsic::ID ID = F->getIntrinsicID();
413 if (TII && ID == Intrinsic::not_intrinsic)
414 ID = static_cast<Intrinsic::ID>(TII->getIntrinsicID(F));
415
416 assert(ID != Intrinsic::not_intrinsic && "unknown intrinsic");
Tim Northover5fb414d2016-07-29 22:32:36 +0000417
Tim Northover91c81732016-08-19 17:17:06 +0000418 if (translateKnownIntrinsic(CI, ID))
419 return true;
420
Tim Northover5fb414d2016-07-29 22:32:36 +0000421 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
422 MachineInstrBuilder MIB =
Tim Northover0f140c72016-09-09 11:46:34 +0000423 MIRBuilder.buildIntrinsic(ID, Res, !CI.doesNotAccessMemory());
Tim Northover5fb414d2016-07-29 22:32:36 +0000424
425 for (auto &Arg : CI.arg_operands()) {
426 if (ConstantInt *CI = dyn_cast<ConstantInt>(Arg))
427 MIB.addImm(CI->getSExtValue());
428 else
429 MIB.addUse(getOrCreateVReg(*Arg));
430 }
431 return true;
432}
433
Tim Northoverbd505462016-07-22 16:59:52 +0000434bool IRTranslator::translateStaticAlloca(const AllocaInst &AI) {
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000435 if (!TPC->isGlobalISelAbortEnabled() && !AI.isStaticAlloca())
436 return false;
437
Tim Northoverbd505462016-07-22 16:59:52 +0000438 assert(AI.isStaticAlloca() && "only handle static allocas now");
439 MachineFunction &MF = MIRBuilder.getMF();
440 unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
441 unsigned Size =
442 ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
443
Tim Northover8d2f52e2016-07-27 17:47:54 +0000444 // Always allocate at least one byte.
445 Size = std::max(Size, 1u);
446
Tim Northoverbd505462016-07-22 16:59:52 +0000447 unsigned Alignment = AI.getAlignment();
448 if (!Alignment)
449 Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
450
451 unsigned Res = getOrCreateVReg(AI);
Matthias Braun93320392016-07-28 20:13:42 +0000452 int FI = MF.getFrameInfo().CreateStackObject(Size, Alignment, false, &AI);
Tim Northover0f140c72016-09-09 11:46:34 +0000453 MIRBuilder.buildFrameIndex(Res, FI);
Tim Northoverbd505462016-07-22 16:59:52 +0000454 return true;
455}
456
Tim Northover357f1be2016-08-10 23:02:41 +0000457bool IRTranslator::translatePHI(const User &U) {
458 const PHINode &PI = cast<PHINode>(U);
Tim Northover25d12862016-09-09 11:47:31 +0000459 auto MIB = MIRBuilder.buildInstr(TargetOpcode::PHI);
Tim Northover97d0cb32016-08-05 17:16:40 +0000460 MIB.addDef(getOrCreateVReg(PI));
461
462 PendingPHIs.emplace_back(&PI, MIB.getInstr());
463 return true;
464}
465
466void IRTranslator::finishPendingPhis() {
467 for (std::pair<const PHINode *, MachineInstr *> &Phi : PendingPHIs) {
468 const PHINode *PI = Phi.first;
469 MachineInstrBuilder MIB(MIRBuilder.getMF(), Phi.second);
470
471 // All MachineBasicBlocks exist, add them to the PHI. We assume IRTranslator
472 // won't create extra control flow here, otherwise we need to find the
473 // dominating predecessor here (or perhaps force the weirder IRTranslators
474 // to provide a simple boundary).
475 for (unsigned i = 0; i < PI->getNumIncomingValues(); ++i) {
476 assert(BBToMBB[PI->getIncomingBlock(i)]->isSuccessor(MIB->getParent()) &&
477 "I appear to have misunderstood Machine PHIs");
478 MIB.addUse(getOrCreateVReg(*PI->getIncomingValue(i)));
479 MIB.addMBB(BBToMBB[PI->getIncomingBlock(i)]);
480 }
481 }
Tim Northover14e7f732016-08-05 17:50:36 +0000482
483 PendingPHIs.clear();
Tim Northover97d0cb32016-08-05 17:16:40 +0000484}
485
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000486bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombeta7fae162016-02-11 17:53:23 +0000487 MIRBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000488 switch(Inst.getOpcode()) {
Tim Northover357f1be2016-08-10 23:02:41 +0000489#define HANDLE_INST(NUM, OPCODE, CLASS) \
490 case Instruction::OPCODE: return translate##OPCODE(Inst);
491#include "llvm/IR/Instruction.def"
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000492 default:
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000493 if (!TPC->isGlobalISelAbortEnabled())
494 return false;
Tim Northover357f1be2016-08-10 23:02:41 +0000495 llvm_unreachable("unknown opcode");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000496 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000497}
498
Tim Northover5ed648e2016-08-09 21:28:04 +0000499bool IRTranslator::translate(const Constant &C, unsigned Reg) {
Tim Northoverd403a3d2016-08-09 23:01:30 +0000500 if (auto CI = dyn_cast<ConstantInt>(&C))
Tim Northover0f140c72016-09-09 11:46:34 +0000501 EntryBuilder.buildConstant(Reg, CI->getZExtValue());
Tim Northoverb16734f2016-08-19 20:09:15 +0000502 else if (auto CF = dyn_cast<ConstantFP>(&C))
Tim Northover0f140c72016-09-09 11:46:34 +0000503 EntryBuilder.buildFConstant(Reg, *CF);
Tim Northoverd403a3d2016-08-09 23:01:30 +0000504 else if (isa<UndefValue>(C))
505 EntryBuilder.buildInstr(TargetOpcode::IMPLICIT_DEF).addDef(Reg);
Tim Northover8e0c53a2016-08-11 21:40:55 +0000506 else if (isa<ConstantPointerNull>(C))
Tim Northover0f140c72016-09-09 11:46:34 +0000507 EntryBuilder.buildInstr(TargetOpcode::G_CONSTANT)
Tim Northover8e0c53a2016-08-11 21:40:55 +0000508 .addDef(Reg)
509 .addImm(0);
Tim Northover032548f2016-09-12 12:10:41 +0000510 else if (auto GV = dyn_cast<GlobalValue>(&C))
511 EntryBuilder.buildGlobalValue(Reg, GV);
Tim Northover357f1be2016-08-10 23:02:41 +0000512 else if (auto CE = dyn_cast<ConstantExpr>(&C)) {
513 switch(CE->getOpcode()) {
514#define HANDLE_INST(NUM, OPCODE, CLASS) \
515 case Instruction::OPCODE: return translate##OPCODE(*CE);
516#include "llvm/IR/Instruction.def"
517 default:
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000518 if (!TPC->isGlobalISelAbortEnabled())
519 return false;
Tim Northover357f1be2016-08-10 23:02:41 +0000520 llvm_unreachable("unknown opcode");
521 }
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000522 } else if (!TPC->isGlobalISelAbortEnabled())
523 return false;
524 else
Tim Northoverd403a3d2016-08-09 23:01:30 +0000525 llvm_unreachable("unhandled constant kind");
Tim Northover5ed648e2016-08-09 21:28:04 +0000526
Tim Northoverd403a3d2016-08-09 23:01:30 +0000527 return true;
Tim Northover5ed648e2016-08-09 21:28:04 +0000528}
529
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000530
Tim Northover0d510442016-08-11 16:21:29 +0000531void IRTranslator::finalizeFunction() {
532 finishPendingPhis();
533
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000534 // Release the memory used by the different maps we
535 // needed during the translation.
Quentin Colombetccd77252016-02-11 21:48:32 +0000536 ValToVReg.clear();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000537 Constants.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000538}
539
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000540bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000541 const Function &F = *MF.getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000542 if (F.empty())
543 return false;
Quentin Colombetba2a0162016-02-16 19:26:02 +0000544 CLI = MF.getSubtarget().getCallLowering();
Quentin Colombet000b5802016-03-11 17:27:51 +0000545 MIRBuilder.setMF(MF);
Tim Northover5ed648e2016-08-09 21:28:04 +0000546 EntryBuilder.setMF(MF);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000547 MRI = &MF.getRegInfo();
Tim Northoverbd505462016-07-22 16:59:52 +0000548 DL = &F.getParent()->getDataLayout();
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000549 TPC = &getAnalysis<TargetPassConfig>();
Tim Northoverbd505462016-07-22 16:59:52 +0000550
Tim Northover14e7f732016-08-05 17:50:36 +0000551 assert(PendingPHIs.empty() && "stale PHIs");
552
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000553 // Setup the arguments.
Quentin Colombet53237a92016-03-11 17:27:43 +0000554 MachineBasicBlock &MBB = getOrCreateBB(F.front());
Quentin Colombet91ebd712016-03-11 17:27:47 +0000555 MIRBuilder.setMBB(MBB);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000556 SmallVector<unsigned, 8> VRegArgs;
557 for (const Argument &Arg: F.args())
Quentin Colombete225e252016-03-11 17:27:54 +0000558 VRegArgs.push_back(getOrCreateVReg(Arg));
Tim Northover862758ec2016-09-21 12:57:35 +0000559 bool Succeeded = CLI->lowerFormalArguments(MIRBuilder, F, VRegArgs);
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000560 if (!Succeeded) {
561 if (!TPC->isGlobalISelAbortEnabled()) {
562 MIRBuilder.getMF().getProperties().set(
563 MachineFunctionProperties::Property::FailedISel);
564 return false;
565 }
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000566 report_fatal_error("Unable to lower arguments");
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000567 }
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000568
Tim Northover5ed648e2016-08-09 21:28:04 +0000569 // Now that we've got the ABI handling code, it's safe to set a location for
570 // any Constants we find in the IR.
571 if (MBB.empty())
572 EntryBuilder.setMBB(MBB);
573 else
574 EntryBuilder.setInstr(MBB.back(), /* Before */ false);
575
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000576 for (const BasicBlock &BB: F) {
Quentin Colombet53237a92016-03-11 17:27:43 +0000577 MachineBasicBlock &MBB = getOrCreateBB(BB);
Quentin Colombet91ebd712016-03-11 17:27:47 +0000578 // Set the insertion point of all the following translations to
579 // the end of this basic block.
580 MIRBuilder.setMBB(MBB);
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000581 for (const Instruction &Inst: BB) {
582 bool Succeeded = translate(Inst);
583 if (!Succeeded) {
584 DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000585 if (TPC->isGlobalISelAbortEnabled())
586 report_fatal_error("Unable to translate instruction");
587 MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
588 break;
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000589 }
590 }
591 }
Tim Northover72eebfa2016-07-12 22:23:42 +0000592
Tim Northover0d510442016-08-11 16:21:29 +0000593 finalizeFunction();
Tim Northover97d0cb32016-08-05 17:16:40 +0000594
Tim Northover72eebfa2016-07-12 22:23:42 +0000595 // Now that the MachineFrameInfo has been configured, no further changes to
596 // the reserved registers are possible.
597 MRI->freezeReservedRegs(MF);
598
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000599 return false;
600}