blob: 6650a1e2073fa7d84a5be72bc062a7ea912d4035 [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 Northover0f140c72016-09-09 11:46:34 +000058 unsigned VReg = MRI->createGenericVirtualRegister(LLT{*Val.getType(), DL});
Quentin Colombetccd77252016-02-11 21:48:32 +000059 ValReg = VReg;
Tim Northover5ed648e2016-08-09 21:28:04 +000060
61 if (auto CV = dyn_cast<Constant>(&Val)) {
62 bool Success = translate(*CV, VReg);
Quentin Colombet3bb32cc2016-08-26 23:49:05 +000063 if (!Success) {
64 if (!TPC->isGlobalISelAbortEnabled()) {
65 MIRBuilder.getMF().getProperties().set(
66 MachineFunctionProperties::Property::FailedISel);
67 return 0;
68 }
Tim Northover5ed648e2016-08-09 21:28:04 +000069 report_fatal_error("unable to translate constant");
Quentin Colombet3bb32cc2016-08-26 23:49:05 +000070 }
Tim Northover5ed648e2016-08-09 21:28:04 +000071 }
Quentin Colombet17c494b2016-02-11 17:51:31 +000072 }
Quentin Colombetccd77252016-02-11 21:48:32 +000073 return ValReg;
Quentin Colombet17c494b2016-02-11 17:51:31 +000074}
75
Tim Northoverad2b7172016-07-26 20:23:26 +000076unsigned IRTranslator::getMemOpAlignment(const Instruction &I) {
77 unsigned Alignment = 0;
78 Type *ValTy = nullptr;
79 if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
80 Alignment = SI->getAlignment();
81 ValTy = SI->getValueOperand()->getType();
82 } else if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
83 Alignment = LI->getAlignment();
84 ValTy = LI->getType();
Quentin Colombet3bb32cc2016-08-26 23:49:05 +000085 } else if (!TPC->isGlobalISelAbortEnabled()) {
86 MIRBuilder.getMF().getProperties().set(
87 MachineFunctionProperties::Property::FailedISel);
88 return 1;
Tim Northoverad2b7172016-07-26 20:23:26 +000089 } else
90 llvm_unreachable("unhandled memory instruction");
91
92 return Alignment ? Alignment : DL->getABITypeAlignment(ValTy);
93}
94
Quentin Colombet53237a92016-03-11 17:27:43 +000095MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock &BB) {
96 MachineBasicBlock *&MBB = BBToMBB[&BB];
Quentin Colombet17c494b2016-02-11 17:51:31 +000097 if (!MBB) {
Quentin Colombeta7fae162016-02-11 17:53:23 +000098 MachineFunction &MF = MIRBuilder.getMF();
Quentin Colombet17c494b2016-02-11 17:51:31 +000099 MBB = MF.CreateMachineBasicBlock();
100 MF.push_back(MBB);
101 }
102 return *MBB;
103}
104
Tim Northover357f1be2016-08-10 23:02:41 +0000105bool IRTranslator::translateBinaryOp(unsigned Opcode, const User &U) {
Tim Northover0d56e052016-07-29 18:11:21 +0000106 // FIXME: handle signed/unsigned wrapping flags.
107
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000108 // Get or create a virtual register for each value.
109 // Unless the value is a Constant => loadimm cst?
110 // or inline constant each time?
111 // Creation of a virtual register needs to have a size.
Tim Northover357f1be2016-08-10 23:02:41 +0000112 unsigned Op0 = getOrCreateVReg(*U.getOperand(0));
113 unsigned Op1 = getOrCreateVReg(*U.getOperand(1));
114 unsigned Res = getOrCreateVReg(U);
Tim Northover0f140c72016-09-09 11:46:34 +0000115 MIRBuilder.buildInstr(Opcode).addDef(Res).addUse(Op0).addUse(Op1);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000116 return true;
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000117}
118
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000119bool IRTranslator::translateCompare(const User &U) {
120 const CmpInst *CI = dyn_cast<CmpInst>(&U);
121 unsigned Op0 = getOrCreateVReg(*U.getOperand(0));
122 unsigned Op1 = getOrCreateVReg(*U.getOperand(1));
123 unsigned Res = getOrCreateVReg(U);
124 CmpInst::Predicate Pred =
125 CI ? CI->getPredicate() : static_cast<CmpInst::Predicate>(
126 cast<ConstantExpr>(U).getPredicate());
Tim Northoverde3aea0412016-08-17 20:25:25 +0000127
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000128 if (CmpInst::isIntPredicate(Pred))
Tim Northover0f140c72016-09-09 11:46:34 +0000129 MIRBuilder.buildICmp(Pred, Res, Op0, Op1);
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000130 else
Tim Northover0f140c72016-09-09 11:46:34 +0000131 MIRBuilder.buildFCmp(Pred, Res, Op0, Op1);
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000132
Tim Northoverde3aea0412016-08-17 20:25:25 +0000133 return true;
134}
135
Tim Northover357f1be2016-08-10 23:02:41 +0000136bool IRTranslator::translateRet(const User &U) {
137 const ReturnInst &RI = cast<ReturnInst>(U);
Tim Northover0d56e052016-07-29 18:11:21 +0000138 const Value *Ret = RI.getReturnValue();
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000139 // The target may mess up with the insertion point, but
140 // this is not important as a return is the last instruction
141 // of the block anyway.
Tom Stellardb72a65f2016-04-14 17:23:33 +0000142 return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000143}
144
Tim Northover357f1be2016-08-10 23:02:41 +0000145bool IRTranslator::translateBr(const User &U) {
146 const BranchInst &BrInst = cast<BranchInst>(U);
Tim Northover69c2ba52016-07-29 17:58:00 +0000147 unsigned Succ = 0;
148 if (!BrInst.isUnconditional()) {
149 // We want a G_BRCOND to the true BB followed by an unconditional branch.
150 unsigned Tst = getOrCreateVReg(*BrInst.getCondition());
151 const BasicBlock &TrueTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ++));
152 MachineBasicBlock &TrueBB = getOrCreateBB(TrueTgt);
Tim Northover0f140c72016-09-09 11:46:34 +0000153 MIRBuilder.buildBrCond(Tst, TrueBB);
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000154 }
Tim Northover69c2ba52016-07-29 17:58:00 +0000155
156 const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ));
157 MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt);
158 MIRBuilder.buildBr(TgtBB);
159
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000160 // Link successors.
161 MachineBasicBlock &CurBB = MIRBuilder.getMBB();
162 for (const BasicBlock *Succ : BrInst.successors())
163 CurBB.addSuccessor(&getOrCreateBB(*Succ));
164 return true;
165}
166
Tim Northover357f1be2016-08-10 23:02:41 +0000167bool IRTranslator::translateLoad(const User &U) {
168 const LoadInst &LI = cast<LoadInst>(U);
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000169
170 if (!TPC->isGlobalISelAbortEnabled() && !LI.isSimple())
171 return false;
172
Tim Northoverad2b7172016-07-26 20:23:26 +0000173 assert(LI.isSimple() && "only simple loads are supported at the moment");
174
175 MachineFunction &MF = MIRBuilder.getMF();
176 unsigned Res = getOrCreateVReg(LI);
177 unsigned Addr = getOrCreateVReg(*LI.getPointerOperand());
Tim Northover28fdc422016-08-15 21:13:17 +0000178 LLT VTy{*LI.getType(), DL}, PTy{*LI.getPointerOperand()->getType()};
Tim Northoverad2b7172016-07-26 20:23:26 +0000179
180 MIRBuilder.buildLoad(
Tim Northover0f140c72016-09-09 11:46:34 +0000181 Res, Addr,
Tim Northover28fdc422016-08-15 21:13:17 +0000182 *MF.getMachineMemOperand(
183 MachinePointerInfo(LI.getPointerOperand()), MachineMemOperand::MOLoad,
184 DL->getTypeStoreSize(LI.getType()), getMemOpAlignment(LI)));
Tim Northoverad2b7172016-07-26 20:23:26 +0000185 return true;
186}
187
Tim Northover357f1be2016-08-10 23:02:41 +0000188bool IRTranslator::translateStore(const User &U) {
189 const StoreInst &SI = cast<StoreInst>(U);
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000190
191 if (!TPC->isGlobalISelAbortEnabled() && !SI.isSimple())
192 return false;
193
Tim Northoverad2b7172016-07-26 20:23:26 +0000194 assert(SI.isSimple() && "only simple loads are supported at the moment");
195
196 MachineFunction &MF = MIRBuilder.getMF();
197 unsigned Val = getOrCreateVReg(*SI.getValueOperand());
198 unsigned Addr = getOrCreateVReg(*SI.getPointerOperand());
Tim Northover28fdc422016-08-15 21:13:17 +0000199 LLT VTy{*SI.getValueOperand()->getType(), DL},
Tim Northoverad2b7172016-07-26 20:23:26 +0000200 PTy{*SI.getPointerOperand()->getType()};
201
202 MIRBuilder.buildStore(
Tim Northover0f140c72016-09-09 11:46:34 +0000203 Val, Addr,
Tim Northover28fdc422016-08-15 21:13:17 +0000204 *MF.getMachineMemOperand(
205 MachinePointerInfo(SI.getPointerOperand()),
206 MachineMemOperand::MOStore,
207 DL->getTypeStoreSize(SI.getValueOperand()->getType()),
208 getMemOpAlignment(SI)));
Tim Northoverad2b7172016-07-26 20:23:26 +0000209 return true;
210}
211
Tim Northover6f80b082016-08-19 17:47:05 +0000212bool IRTranslator::translateExtractValue(const User &U) {
Tim Northoverb6046222016-08-19 20:09:03 +0000213 const Value *Src = U.getOperand(0);
214 Type *Int32Ty = Type::getInt32Ty(U.getContext());
Tim Northover6f80b082016-08-19 17:47:05 +0000215 SmallVector<Value *, 1> Indices;
216
217 // getIndexedOffsetInType is designed for GEPs, so the first index is the
218 // usual array element rather than looking into the actual aggregate.
219 Indices.push_back(ConstantInt::get(Int32Ty, 0));
Tim Northoverb6046222016-08-19 20:09:03 +0000220
221 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&U)) {
222 for (auto Idx : EVI->indices())
223 Indices.push_back(ConstantInt::get(Int32Ty, Idx));
224 } else {
225 for (unsigned i = 1; i < U.getNumOperands(); ++i)
226 Indices.push_back(U.getOperand(i));
227 }
Tim Northover6f80b082016-08-19 17:47:05 +0000228
229 uint64_t Offset = 8 * DL->getIndexedOffsetInType(Src->getType(), Indices);
230
Tim Northoverb6046222016-08-19 20:09:03 +0000231 unsigned Res = getOrCreateVReg(U);
Tim Northover0f140c72016-09-09 11:46:34 +0000232 MIRBuilder.buildExtract(Res, Offset, getOrCreateVReg(*Src));
Tim Northover6f80b082016-08-19 17:47:05 +0000233
234 return true;
235}
236
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000237bool IRTranslator::translateInsertValue(const User &U) {
Tim Northoverb6046222016-08-19 20:09:03 +0000238 const Value *Src = U.getOperand(0);
239 Type *Int32Ty = Type::getInt32Ty(U.getContext());
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000240 SmallVector<Value *, 1> Indices;
241
242 // getIndexedOffsetInType is designed for GEPs, so the first index is the
243 // usual array element rather than looking into the actual aggregate.
244 Indices.push_back(ConstantInt::get(Int32Ty, 0));
Tim Northoverb6046222016-08-19 20:09:03 +0000245
246 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&U)) {
247 for (auto Idx : IVI->indices())
248 Indices.push_back(ConstantInt::get(Int32Ty, Idx));
249 } else {
250 for (unsigned i = 2; i < U.getNumOperands(); ++i)
251 Indices.push_back(U.getOperand(i));
252 }
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000253
254 uint64_t Offset = 8 * DL->getIndexedOffsetInType(Src->getType(), Indices);
255
Tim Northoverb6046222016-08-19 20:09:03 +0000256 unsigned Res = getOrCreateVReg(U);
257 const Value &Inserted = *U.getOperand(1);
Tim Northover0f140c72016-09-09 11:46:34 +0000258 MIRBuilder.buildInsert(Res, getOrCreateVReg(*Src), getOrCreateVReg(Inserted),
259 Offset);
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000260
261 return true;
262}
263
Tim Northover5a28c362016-08-19 20:09:07 +0000264bool IRTranslator::translateSelect(const User &U) {
Tim Northover0f140c72016-09-09 11:46:34 +0000265 MIRBuilder.buildSelect(getOrCreateVReg(U), getOrCreateVReg(*U.getOperand(0)),
266 getOrCreateVReg(*U.getOperand(1)),
267 getOrCreateVReg(*U.getOperand(2)));
Tim Northover5a28c362016-08-19 20:09:07 +0000268 return true;
269}
270
Tim Northover357f1be2016-08-10 23:02:41 +0000271bool IRTranslator::translateBitCast(const User &U) {
272 if (LLT{*U.getOperand(0)->getType()} == LLT{*U.getType()}) {
273 unsigned &Reg = ValToVReg[&U];
Tim Northover7552ef52016-08-10 16:51:14 +0000274 if (Reg)
Tim Northover357f1be2016-08-10 23:02:41 +0000275 MIRBuilder.buildCopy(Reg, getOrCreateVReg(*U.getOperand(0)));
Tim Northover7552ef52016-08-10 16:51:14 +0000276 else
Tim Northover357f1be2016-08-10 23:02:41 +0000277 Reg = getOrCreateVReg(*U.getOperand(0));
Tim Northover7c9eba92016-07-25 21:01:29 +0000278 return true;
279 }
Tim Northover357f1be2016-08-10 23:02:41 +0000280 return translateCast(TargetOpcode::G_BITCAST, U);
Tim Northover7c9eba92016-07-25 21:01:29 +0000281}
282
Tim Northover357f1be2016-08-10 23:02:41 +0000283bool IRTranslator::translateCast(unsigned Opcode, const User &U) {
284 unsigned Op = getOrCreateVReg(*U.getOperand(0));
285 unsigned Res = getOrCreateVReg(U);
Tim Northover0f140c72016-09-09 11:46:34 +0000286 MIRBuilder.buildInstr(Opcode).addDef(Res).addUse(Op);
Tim Northover7c9eba92016-07-25 21:01:29 +0000287 return true;
288}
289
Tim Northover91c81732016-08-19 17:17:06 +0000290bool IRTranslator::translateKnownIntrinsic(const CallInst &CI,
291 Intrinsic::ID ID) {
292 unsigned Op = 0;
293 switch (ID) {
294 default: return false;
295 case Intrinsic::uadd_with_overflow: Op = TargetOpcode::G_UADDE; break;
296 case Intrinsic::sadd_with_overflow: Op = TargetOpcode::G_SADDO; break;
297 case Intrinsic::usub_with_overflow: Op = TargetOpcode::G_USUBE; break;
298 case Intrinsic::ssub_with_overflow: Op = TargetOpcode::G_SSUBO; break;
299 case Intrinsic::umul_with_overflow: Op = TargetOpcode::G_UMULO; break;
300 case Intrinsic::smul_with_overflow: Op = TargetOpcode::G_SMULO; break;
301 }
302
303 LLT Ty{*CI.getOperand(0)->getType()};
304 LLT s1 = LLT::scalar(1);
305 unsigned Width = Ty.getSizeInBits();
Tim Northover0f140c72016-09-09 11:46:34 +0000306 unsigned Res = MRI->createGenericVirtualRegister(Ty);
307 unsigned Overflow = MRI->createGenericVirtualRegister(s1);
308 auto MIB = MIRBuilder.buildInstr(Op)
Tim Northover91c81732016-08-19 17:17:06 +0000309 .addDef(Res)
310 .addDef(Overflow)
311 .addUse(getOrCreateVReg(*CI.getOperand(0)))
312 .addUse(getOrCreateVReg(*CI.getOperand(1)));
313
314 if (Op == TargetOpcode::G_UADDE || Op == TargetOpcode::G_USUBE) {
Tim Northover0f140c72016-09-09 11:46:34 +0000315 unsigned Zero = MRI->createGenericVirtualRegister(s1);
316 EntryBuilder.buildConstant(Zero, 0);
Tim Northover91c81732016-08-19 17:17:06 +0000317 MIB.addUse(Zero);
318 }
319
Tim Northover0f140c72016-09-09 11:46:34 +0000320 MIRBuilder.buildSequence(getOrCreateVReg(CI), Res, 0, Overflow, Width);
Tim Northover91c81732016-08-19 17:17:06 +0000321 return true;
322}
323
Tim Northover357f1be2016-08-10 23:02:41 +0000324bool IRTranslator::translateCall(const User &U) {
325 const CallInst &CI = cast<CallInst>(U);
Tim Northover5fb414d2016-07-29 22:32:36 +0000326 auto TII = MIRBuilder.getMF().getTarget().getIntrinsicInfo();
Tim Northover406024a2016-08-10 21:44:01 +0000327 const Function *F = CI.getCalledFunction();
Tim Northover5fb414d2016-07-29 22:32:36 +0000328
Tim Northover406024a2016-08-10 21:44:01 +0000329 if (!F || !F->isIntrinsic()) {
330 // FIXME: handle multiple return values.
331 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
332 SmallVector<unsigned, 8> Args;
333 for (auto &Arg: CI.arg_operands())
334 Args.push_back(getOrCreateVReg(*Arg));
335
Tim Northoverfe5f89b2016-08-29 19:07:08 +0000336 return CLI->lowerCall(MIRBuilder, CI, Res, Args, [&]() {
337 return getOrCreateVReg(*CI.getCalledValue());
338 });
Tim Northover406024a2016-08-10 21:44:01 +0000339 }
340
341 Intrinsic::ID ID = F->getIntrinsicID();
342 if (TII && ID == Intrinsic::not_intrinsic)
343 ID = static_cast<Intrinsic::ID>(TII->getIntrinsicID(F));
344
345 assert(ID != Intrinsic::not_intrinsic && "unknown intrinsic");
Tim Northover5fb414d2016-07-29 22:32:36 +0000346
Tim Northover91c81732016-08-19 17:17:06 +0000347 if (translateKnownIntrinsic(CI, ID))
348 return true;
349
Tim Northover5fb414d2016-07-29 22:32:36 +0000350 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
351 MachineInstrBuilder MIB =
Tim Northover0f140c72016-09-09 11:46:34 +0000352 MIRBuilder.buildIntrinsic(ID, Res, !CI.doesNotAccessMemory());
Tim Northover5fb414d2016-07-29 22:32:36 +0000353
354 for (auto &Arg : CI.arg_operands()) {
355 if (ConstantInt *CI = dyn_cast<ConstantInt>(Arg))
356 MIB.addImm(CI->getSExtValue());
357 else
358 MIB.addUse(getOrCreateVReg(*Arg));
359 }
360 return true;
361}
362
Tim Northoverbd505462016-07-22 16:59:52 +0000363bool IRTranslator::translateStaticAlloca(const AllocaInst &AI) {
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000364 if (!TPC->isGlobalISelAbortEnabled() && !AI.isStaticAlloca())
365 return false;
366
Tim Northoverbd505462016-07-22 16:59:52 +0000367 assert(AI.isStaticAlloca() && "only handle static allocas now");
368 MachineFunction &MF = MIRBuilder.getMF();
369 unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
370 unsigned Size =
371 ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
372
Tim Northover8d2f52e2016-07-27 17:47:54 +0000373 // Always allocate at least one byte.
374 Size = std::max(Size, 1u);
375
Tim Northoverbd505462016-07-22 16:59:52 +0000376 unsigned Alignment = AI.getAlignment();
377 if (!Alignment)
378 Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
379
380 unsigned Res = getOrCreateVReg(AI);
Matthias Braun93320392016-07-28 20:13:42 +0000381 int FI = MF.getFrameInfo().CreateStackObject(Size, Alignment, false, &AI);
Tim Northover0f140c72016-09-09 11:46:34 +0000382 MIRBuilder.buildFrameIndex(Res, FI);
Tim Northoverbd505462016-07-22 16:59:52 +0000383 return true;
384}
385
Tim Northover357f1be2016-08-10 23:02:41 +0000386bool IRTranslator::translatePHI(const User &U) {
387 const PHINode &PI = cast<PHINode>(U);
Tim Northover25d12862016-09-09 11:47:31 +0000388 auto MIB = MIRBuilder.buildInstr(TargetOpcode::PHI);
Tim Northover97d0cb32016-08-05 17:16:40 +0000389 MIB.addDef(getOrCreateVReg(PI));
390
391 PendingPHIs.emplace_back(&PI, MIB.getInstr());
392 return true;
393}
394
395void IRTranslator::finishPendingPhis() {
396 for (std::pair<const PHINode *, MachineInstr *> &Phi : PendingPHIs) {
397 const PHINode *PI = Phi.first;
398 MachineInstrBuilder MIB(MIRBuilder.getMF(), Phi.second);
399
400 // All MachineBasicBlocks exist, add them to the PHI. We assume IRTranslator
401 // won't create extra control flow here, otherwise we need to find the
402 // dominating predecessor here (or perhaps force the weirder IRTranslators
403 // to provide a simple boundary).
404 for (unsigned i = 0; i < PI->getNumIncomingValues(); ++i) {
405 assert(BBToMBB[PI->getIncomingBlock(i)]->isSuccessor(MIB->getParent()) &&
406 "I appear to have misunderstood Machine PHIs");
407 MIB.addUse(getOrCreateVReg(*PI->getIncomingValue(i)));
408 MIB.addMBB(BBToMBB[PI->getIncomingBlock(i)]);
409 }
410 }
Tim Northover14e7f732016-08-05 17:50:36 +0000411
412 PendingPHIs.clear();
Tim Northover97d0cb32016-08-05 17:16:40 +0000413}
414
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000415bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombeta7fae162016-02-11 17:53:23 +0000416 MIRBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000417 switch(Inst.getOpcode()) {
Tim Northover357f1be2016-08-10 23:02:41 +0000418#define HANDLE_INST(NUM, OPCODE, CLASS) \
419 case Instruction::OPCODE: return translate##OPCODE(Inst);
420#include "llvm/IR/Instruction.def"
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000421 default:
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000422 if (!TPC->isGlobalISelAbortEnabled())
423 return false;
Tim Northover357f1be2016-08-10 23:02:41 +0000424 llvm_unreachable("unknown opcode");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000425 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000426}
427
Tim Northover5ed648e2016-08-09 21:28:04 +0000428bool IRTranslator::translate(const Constant &C, unsigned Reg) {
Tim Northoverd403a3d2016-08-09 23:01:30 +0000429 if (auto CI = dyn_cast<ConstantInt>(&C))
Tim Northover0f140c72016-09-09 11:46:34 +0000430 EntryBuilder.buildConstant(Reg, CI->getZExtValue());
Tim Northoverb16734f2016-08-19 20:09:15 +0000431 else if (auto CF = dyn_cast<ConstantFP>(&C))
Tim Northover0f140c72016-09-09 11:46:34 +0000432 EntryBuilder.buildFConstant(Reg, *CF);
Tim Northoverd403a3d2016-08-09 23:01:30 +0000433 else if (isa<UndefValue>(C))
434 EntryBuilder.buildInstr(TargetOpcode::IMPLICIT_DEF).addDef(Reg);
Tim Northover8e0c53a2016-08-11 21:40:55 +0000435 else if (isa<ConstantPointerNull>(C))
Tim Northover0f140c72016-09-09 11:46:34 +0000436 EntryBuilder.buildInstr(TargetOpcode::G_CONSTANT)
Tim Northover8e0c53a2016-08-11 21:40:55 +0000437 .addDef(Reg)
438 .addImm(0);
Tim Northover357f1be2016-08-10 23:02:41 +0000439 else if (auto CE = dyn_cast<ConstantExpr>(&C)) {
440 switch(CE->getOpcode()) {
441#define HANDLE_INST(NUM, OPCODE, CLASS) \
442 case Instruction::OPCODE: return translate##OPCODE(*CE);
443#include "llvm/IR/Instruction.def"
444 default:
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000445 if (!TPC->isGlobalISelAbortEnabled())
446 return false;
Tim Northover357f1be2016-08-10 23:02:41 +0000447 llvm_unreachable("unknown opcode");
448 }
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000449 } else if (!TPC->isGlobalISelAbortEnabled())
450 return false;
451 else
Tim Northoverd403a3d2016-08-09 23:01:30 +0000452 llvm_unreachable("unhandled constant kind");
Tim Northover5ed648e2016-08-09 21:28:04 +0000453
Tim Northoverd403a3d2016-08-09 23:01:30 +0000454 return true;
Tim Northover5ed648e2016-08-09 21:28:04 +0000455}
456
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000457
Tim Northover0d510442016-08-11 16:21:29 +0000458void IRTranslator::finalizeFunction() {
459 finishPendingPhis();
460
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000461 // Release the memory used by the different maps we
462 // needed during the translation.
Quentin Colombetccd77252016-02-11 21:48:32 +0000463 ValToVReg.clear();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000464 Constants.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000465}
466
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000467bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000468 const Function &F = *MF.getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000469 if (F.empty())
470 return false;
Quentin Colombetba2a0162016-02-16 19:26:02 +0000471 CLI = MF.getSubtarget().getCallLowering();
Quentin Colombet000b5802016-03-11 17:27:51 +0000472 MIRBuilder.setMF(MF);
Tim Northover5ed648e2016-08-09 21:28:04 +0000473 EntryBuilder.setMF(MF);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000474 MRI = &MF.getRegInfo();
Tim Northoverbd505462016-07-22 16:59:52 +0000475 DL = &F.getParent()->getDataLayout();
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000476 TPC = &getAnalysis<TargetPassConfig>();
Tim Northoverbd505462016-07-22 16:59:52 +0000477
Tim Northover14e7f732016-08-05 17:50:36 +0000478 assert(PendingPHIs.empty() && "stale PHIs");
479
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000480 // Setup the arguments.
Quentin Colombet53237a92016-03-11 17:27:43 +0000481 MachineBasicBlock &MBB = getOrCreateBB(F.front());
Quentin Colombet91ebd712016-03-11 17:27:47 +0000482 MIRBuilder.setMBB(MBB);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000483 SmallVector<unsigned, 8> VRegArgs;
484 for (const Argument &Arg: F.args())
Quentin Colombete225e252016-03-11 17:27:54 +0000485 VRegArgs.push_back(getOrCreateVReg(Arg));
Quentin Colombetba2a0162016-02-16 19:26:02 +0000486 bool Succeeded =
Tom Stellardb72a65f2016-04-14 17:23:33 +0000487 CLI->lowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs);
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000488 if (!Succeeded) {
489 if (!TPC->isGlobalISelAbortEnabled()) {
490 MIRBuilder.getMF().getProperties().set(
491 MachineFunctionProperties::Property::FailedISel);
492 return false;
493 }
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000494 report_fatal_error("Unable to lower arguments");
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000495 }
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000496
Tim Northover5ed648e2016-08-09 21:28:04 +0000497 // Now that we've got the ABI handling code, it's safe to set a location for
498 // any Constants we find in the IR.
499 if (MBB.empty())
500 EntryBuilder.setMBB(MBB);
501 else
502 EntryBuilder.setInstr(MBB.back(), /* Before */ false);
503
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000504 for (const BasicBlock &BB: F) {
Quentin Colombet53237a92016-03-11 17:27:43 +0000505 MachineBasicBlock &MBB = getOrCreateBB(BB);
Quentin Colombet91ebd712016-03-11 17:27:47 +0000506 // Set the insertion point of all the following translations to
507 // the end of this basic block.
508 MIRBuilder.setMBB(MBB);
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000509 for (const Instruction &Inst: BB) {
510 bool Succeeded = translate(Inst);
511 if (!Succeeded) {
512 DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000513 if (TPC->isGlobalISelAbortEnabled())
514 report_fatal_error("Unable to translate instruction");
515 MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
516 break;
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000517 }
518 }
519 }
Tim Northover72eebfa2016-07-12 22:23:42 +0000520
Tim Northover0d510442016-08-11 16:21:29 +0000521 finalizeFunction();
Tim Northover97d0cb32016-08-05 17:16:40 +0000522
Tim Northover72eebfa2016-07-12 22:23:42 +0000523 // Now that the MachineFrameInfo has been configured, no further changes to
524 // the reserved registers are possible.
525 MRI->freezeReservedRegs(MF);
526
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000527 return false;
528}