blob: 1a70947a4aa7fa2e95a8a3bb69465e1866b4955c [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
Ahmed Bougachaeceabdd2017-02-23 23:57:28 +000015#include "llvm/ADT/ScopeExit.h"
Tim Northoverb6636fd2017-01-17 22:13:50 +000016#include "llvm/ADT/SmallSet.h"
Quentin Colombetfd9d0a02016-02-11 19:59:41 +000017#include "llvm/ADT/SmallVector.h"
Ahmed Bougachaae9dade2017-02-23 21:05:42 +000018#include "llvm/Analysis/OptimizationDiagnosticInfo.h"
Quentin Colombetba2a0162016-02-16 19:26:02 +000019#include "llvm/CodeGen/GlobalISel/CallLowering.h"
Tim Northovera9105be2016-11-09 22:39:54 +000020#include "llvm/CodeGen/Analysis.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000021#include "llvm/CodeGen/MachineFunction.h"
Tim Northoverbd505462016-07-22 16:59:52 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Tim Northovera9105be2016-11-09 22:39:54 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Quentin Colombet17c494b2016-02-11 17:51:31 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Quentin Colombet3bb32cc2016-08-26 23:49:05 +000025#include "llvm/CodeGen/TargetPassConfig.h"
Quentin Colombet17c494b2016-02-11 17:51:31 +000026#include "llvm/IR/Constant.h"
Tim Northover09aac4a2017-01-26 23:39:14 +000027#include "llvm/IR/DebugInfo.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000028#include "llvm/IR/Function.h"
Tim Northovera7653b32016-09-12 11:20:22 +000029#include "llvm/IR/GetElementPtrTypeIterator.h"
Tim Northover5fb414d2016-07-29 22:32:36 +000030#include "llvm/IR/IntrinsicInst.h"
Quentin Colombet17c494b2016-02-11 17:51:31 +000031#include "llvm/IR/Type.h"
32#include "llvm/IR/Value.h"
Tim Northoverc3e3f592017-02-03 18:22:45 +000033#include "llvm/Target/TargetFrameLowering.h"
Tim Northover5fb414d2016-07-29 22:32:36 +000034#include "llvm/Target/TargetIntrinsicInfo.h"
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000035#include "llvm/Target/TargetLowering.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000036
37#define DEBUG_TYPE "irtranslator"
38
Quentin Colombet105cf2b2016-01-20 20:58:56 +000039using namespace llvm;
40
41char IRTranslator::ID = 0;
Quentin Colombet3bb32cc2016-08-26 23:49:05 +000042INITIALIZE_PASS_BEGIN(IRTranslator, DEBUG_TYPE, "IRTranslator LLVM IR -> MI",
43 false, false)
44INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
45INITIALIZE_PASS_END(IRTranslator, DEBUG_TYPE, "IRTranslator LLVM IR -> MI",
Tim Northover884b47e2016-07-26 03:29:18 +000046 false, false)
Quentin Colombet105cf2b2016-01-20 20:58:56 +000047
Ahmed Bougachaae9dade2017-02-23 21:05:42 +000048static void reportTranslationError(MachineFunction &MF,
49 const TargetPassConfig &TPC,
50 OptimizationRemarkEmitter &ORE,
51 OptimizationRemarkMissed &R) {
52 MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
53
54 // Print the function name explicitly if we don't have a debug location (which
55 // makes the diagnostic less useful) or if we're going to emit a raw error.
56 if (!R.getLocation().isValid() || TPC.isGlobalISelAbortEnabled())
57 R << (" (in function: " + MF.getName() + ")").str();
58
59 if (TPC.isGlobalISelAbortEnabled())
60 report_fatal_error(R.getMsg());
61 else
62 ORE.emit(R);
Tim Northover60f23492016-11-08 01:12:17 +000063}
64
Quentin Colombeta7fae162016-02-11 17:53:23 +000065IRTranslator::IRTranslator() : MachineFunctionPass(ID), MRI(nullptr) {
Quentin Colombet39293d32016-03-08 01:38:55 +000066 initializeIRTranslatorPass(*PassRegistry::getPassRegistry());
Quentin Colombeta7fae162016-02-11 17:53:23 +000067}
68
Quentin Colombet3bb32cc2016-08-26 23:49:05 +000069void IRTranslator::getAnalysisUsage(AnalysisUsage &AU) const {
70 AU.addRequired<TargetPassConfig>();
71 MachineFunctionPass::getAnalysisUsage(AU);
72}
73
74
Quentin Colombete225e252016-03-11 17:27:54 +000075unsigned IRTranslator::getOrCreateVReg(const Value &Val) {
76 unsigned &ValReg = ValToVReg[&Val];
Tim Northover5ed648e2016-08-09 21:28:04 +000077
Tim Northover9e35f1e2017-01-25 20:58:22 +000078 if (ValReg)
79 return ValReg;
80
81 // Fill ValRegsSequence with the sequence of registers
82 // we need to concat together to produce the value.
83 assert(Val.getType()->isSized() &&
84 "Don't know how to create an empty vreg");
Daniel Sanders8ebec372017-03-07 19:21:23 +000085 unsigned VReg = MRI->createGenericVirtualRegister(LLT{*Val.getType(), *DL});
Tim Northover9e35f1e2017-01-25 20:58:22 +000086 ValReg = VReg;
87
88 if (auto CV = dyn_cast<Constant>(&Val)) {
89 bool Success = translate(*CV, VReg);
90 if (!Success) {
Ahmed Bougachaae9dade2017-02-23 21:05:42 +000091 OptimizationRemarkMissed R("gisel-irtranslator", "GISelFailure",
Ahmed Bougacha7c88a4e2017-02-24 00:34:44 +000092 MF->getFunction()->getSubprogram(),
Ahmed Bougachaae9dade2017-02-23 21:05:42 +000093 &MF->getFunction()->getEntryBlock());
94 R << "unable to translate constant: " << ore::NV("Type", Val.getType());
95 reportTranslationError(*MF, *TPC, *ORE, R);
96 return VReg;
Tim Northover5ed648e2016-08-09 21:28:04 +000097 }
Quentin Colombet17c494b2016-02-11 17:51:31 +000098 }
Tim Northover7f3ad2e2017-01-20 23:25:17 +000099
Tim Northover9e35f1e2017-01-25 20:58:22 +0000100 return VReg;
Quentin Colombet17c494b2016-02-11 17:51:31 +0000101}
102
Tim Northovercdf23f12016-10-31 18:30:59 +0000103int IRTranslator::getOrCreateFrameIndex(const AllocaInst &AI) {
104 if (FrameIndices.find(&AI) != FrameIndices.end())
105 return FrameIndices[&AI];
106
Tim Northovercdf23f12016-10-31 18:30:59 +0000107 unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
108 unsigned Size =
109 ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
110
111 // Always allocate at least one byte.
112 Size = std::max(Size, 1u);
113
114 unsigned Alignment = AI.getAlignment();
115 if (!Alignment)
116 Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
117
118 int &FI = FrameIndices[&AI];
Tim Northover50db7f412016-12-07 21:17:47 +0000119 FI = MF->getFrameInfo().CreateStackObject(Size, Alignment, false, &AI);
Tim Northovercdf23f12016-10-31 18:30:59 +0000120 return FI;
121}
122
Tim Northoverad2b7172016-07-26 20:23:26 +0000123unsigned IRTranslator::getMemOpAlignment(const Instruction &I) {
124 unsigned Alignment = 0;
125 Type *ValTy = nullptr;
126 if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
127 Alignment = SI->getAlignment();
128 ValTy = SI->getValueOperand()->getType();
129 } else if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
130 Alignment = LI->getAlignment();
131 ValTy = LI->getType();
Ahmed Bougachaae9dade2017-02-23 21:05:42 +0000132 } else {
133 OptimizationRemarkMissed R("gisel-irtranslator", "", &I);
134 R << "unable to translate memop: " << ore::NV("Opcode", &I);
135 reportTranslationError(*MF, *TPC, *ORE, R);
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000136 return 1;
Ahmed Bougachaae9dade2017-02-23 21:05:42 +0000137 }
Tim Northoverad2b7172016-07-26 20:23:26 +0000138
139 return Alignment ? Alignment : DL->getABITypeAlignment(ValTy);
140}
141
Quentin Colombet53237a92016-03-11 17:27:43 +0000142MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock &BB) {
143 MachineBasicBlock *&MBB = BBToMBB[&BB];
Quentin Colombet17c494b2016-02-11 17:51:31 +0000144 if (!MBB) {
Kristof Beylsa983e7c2017-01-05 13:27:52 +0000145 MBB = MF->CreateMachineBasicBlock(&BB);
Tim Northover50db7f412016-12-07 21:17:47 +0000146 MF->push_back(MBB);
Kristof Beylsa983e7c2017-01-05 13:27:52 +0000147
148 if (BB.hasAddressTaken())
149 MBB->setHasAddressTaken();
Quentin Colombet17c494b2016-02-11 17:51:31 +0000150 }
151 return *MBB;
152}
153
Tim Northoverb6636fd2017-01-17 22:13:50 +0000154void IRTranslator::addMachineCFGPred(CFGEdge Edge, MachineBasicBlock *NewPred) {
155 assert(NewPred && "new predecessor must be a real MachineBasicBlock");
156 MachinePreds[Edge].push_back(NewPred);
157}
158
Tim Northoverc53606e2016-12-07 21:29:15 +0000159bool IRTranslator::translateBinaryOp(unsigned Opcode, const User &U,
160 MachineIRBuilder &MIRBuilder) {
Tim Northover0d56e052016-07-29 18:11:21 +0000161 // FIXME: handle signed/unsigned wrapping flags.
162
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000163 // Get or create a virtual register for each value.
164 // Unless the value is a Constant => loadimm cst?
165 // or inline constant each time?
166 // Creation of a virtual register needs to have a size.
Tim Northover357f1be2016-08-10 23:02:41 +0000167 unsigned Op0 = getOrCreateVReg(*U.getOperand(0));
168 unsigned Op1 = getOrCreateVReg(*U.getOperand(1));
169 unsigned Res = getOrCreateVReg(U);
Tim Northover0f140c72016-09-09 11:46:34 +0000170 MIRBuilder.buildInstr(Opcode).addDef(Res).addUse(Op0).addUse(Op1);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000171 return true;
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000172}
173
Volkan Keles20d3c422017-03-07 18:03:28 +0000174bool IRTranslator::translateFSub(const User &U, MachineIRBuilder &MIRBuilder) {
175 // -0.0 - X --> G_FNEG
176 if (isa<Constant>(U.getOperand(0)) &&
177 U.getOperand(0) == ConstantFP::getZeroValueForNegation(U.getType())) {
178 MIRBuilder.buildInstr(TargetOpcode::G_FNEG)
179 .addDef(getOrCreateVReg(U))
180 .addUse(getOrCreateVReg(*U.getOperand(1)));
181 return true;
182 }
183 return translateBinaryOp(TargetOpcode::G_FSUB, U, MIRBuilder);
184}
185
Tim Northoverc53606e2016-12-07 21:29:15 +0000186bool IRTranslator::translateCompare(const User &U,
187 MachineIRBuilder &MIRBuilder) {
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000188 const CmpInst *CI = dyn_cast<CmpInst>(&U);
189 unsigned Op0 = getOrCreateVReg(*U.getOperand(0));
190 unsigned Op1 = getOrCreateVReg(*U.getOperand(1));
191 unsigned Res = getOrCreateVReg(U);
192 CmpInst::Predicate Pred =
193 CI ? CI->getPredicate() : static_cast<CmpInst::Predicate>(
194 cast<ConstantExpr>(U).getPredicate());
Tim Northoverde3aea0412016-08-17 20:25:25 +0000195
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000196 if (CmpInst::isIntPredicate(Pred))
Tim Northover0f140c72016-09-09 11:46:34 +0000197 MIRBuilder.buildICmp(Pred, Res, Op0, Op1);
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000198 else
Tim Northover0f140c72016-09-09 11:46:34 +0000199 MIRBuilder.buildFCmp(Pred, Res, Op0, Op1);
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000200
Tim Northoverde3aea0412016-08-17 20:25:25 +0000201 return true;
202}
203
Tim Northoverc53606e2016-12-07 21:29:15 +0000204bool IRTranslator::translateRet(const User &U, MachineIRBuilder &MIRBuilder) {
Tim Northover357f1be2016-08-10 23:02:41 +0000205 const ReturnInst &RI = cast<ReturnInst>(U);
Tim Northover0d56e052016-07-29 18:11:21 +0000206 const Value *Ret = RI.getReturnValue();
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000207 // The target may mess up with the insertion point, but
208 // this is not important as a return is the last instruction
209 // of the block anyway.
Tom Stellardb72a65f2016-04-14 17:23:33 +0000210 return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000211}
212
Tim Northoverc53606e2016-12-07 21:29:15 +0000213bool IRTranslator::translateBr(const User &U, MachineIRBuilder &MIRBuilder) {
Tim Northover357f1be2016-08-10 23:02:41 +0000214 const BranchInst &BrInst = cast<BranchInst>(U);
Tim Northover69c2ba52016-07-29 17:58:00 +0000215 unsigned Succ = 0;
216 if (!BrInst.isUnconditional()) {
217 // We want a G_BRCOND to the true BB followed by an unconditional branch.
218 unsigned Tst = getOrCreateVReg(*BrInst.getCondition());
219 const BasicBlock &TrueTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ++));
220 MachineBasicBlock &TrueBB = getOrCreateBB(TrueTgt);
Tim Northover0f140c72016-09-09 11:46:34 +0000221 MIRBuilder.buildBrCond(Tst, TrueBB);
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000222 }
Tim Northover69c2ba52016-07-29 17:58:00 +0000223
224 const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ));
225 MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt);
226 MIRBuilder.buildBr(TgtBB);
227
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000228 // Link successors.
229 MachineBasicBlock &CurBB = MIRBuilder.getMBB();
230 for (const BasicBlock *Succ : BrInst.successors())
231 CurBB.addSuccessor(&getOrCreateBB(*Succ));
232 return true;
233}
234
Kristof Beylseced0712017-01-05 11:28:51 +0000235bool IRTranslator::translateSwitch(const User &U,
236 MachineIRBuilder &MIRBuilder) {
237 // For now, just translate as a chain of conditional branches.
238 // FIXME: could we share most of the logic/code in
239 // SelectionDAGBuilder::visitSwitch between SelectionDAG and GlobalISel?
240 // At first sight, it seems most of the logic in there is independent of
241 // SelectionDAG-specifics and a lot of work went in to optimize switch
242 // lowering in there.
243
244 const SwitchInst &SwInst = cast<SwitchInst>(U);
245 const unsigned SwCondValue = getOrCreateVReg(*SwInst.getCondition());
Tim Northoverb6636fd2017-01-17 22:13:50 +0000246 const BasicBlock *OrigBB = SwInst.getParent();
Kristof Beylseced0712017-01-05 11:28:51 +0000247
Daniel Sanders8ebec372017-03-07 19:21:23 +0000248 LLT LLTi1 = LLT(*Type::getInt1Ty(U.getContext()), *DL);
Kristof Beylseced0712017-01-05 11:28:51 +0000249 for (auto &CaseIt : SwInst.cases()) {
250 const unsigned CaseValueReg = getOrCreateVReg(*CaseIt.getCaseValue());
251 const unsigned Tst = MRI->createGenericVirtualRegister(LLTi1);
252 MIRBuilder.buildICmp(CmpInst::ICMP_EQ, Tst, CaseValueReg, SwCondValue);
Tim Northoverb6636fd2017-01-17 22:13:50 +0000253 MachineBasicBlock &CurMBB = MIRBuilder.getMBB();
254 const BasicBlock *TrueBB = CaseIt.getCaseSuccessor();
255 MachineBasicBlock &TrueMBB = getOrCreateBB(*TrueBB);
Kristof Beylseced0712017-01-05 11:28:51 +0000256
Tim Northoverb6636fd2017-01-17 22:13:50 +0000257 MIRBuilder.buildBrCond(Tst, TrueMBB);
258 CurMBB.addSuccessor(&TrueMBB);
259 addMachineCFGPred({OrigBB, TrueBB}, &CurMBB);
Kristof Beylseced0712017-01-05 11:28:51 +0000260
Tim Northoverb6636fd2017-01-17 22:13:50 +0000261 MachineBasicBlock *FalseMBB =
Kristof Beylseced0712017-01-05 11:28:51 +0000262 MF->CreateMachineBasicBlock(SwInst.getParent());
Tim Northoverb6636fd2017-01-17 22:13:50 +0000263 MF->push_back(FalseMBB);
264 MIRBuilder.buildBr(*FalseMBB);
265 CurMBB.addSuccessor(FalseMBB);
Kristof Beylseced0712017-01-05 11:28:51 +0000266
Tim Northoverb6636fd2017-01-17 22:13:50 +0000267 MIRBuilder.setMBB(*FalseMBB);
Kristof Beylseced0712017-01-05 11:28:51 +0000268 }
269 // handle default case
Tim Northoverb6636fd2017-01-17 22:13:50 +0000270 const BasicBlock *DefaultBB = SwInst.getDefaultDest();
271 MachineBasicBlock &DefaultMBB = getOrCreateBB(*DefaultBB);
272 MIRBuilder.buildBr(DefaultMBB);
273 MachineBasicBlock &CurMBB = MIRBuilder.getMBB();
274 CurMBB.addSuccessor(&DefaultMBB);
275 addMachineCFGPred({OrigBB, DefaultBB}, &CurMBB);
Kristof Beylseced0712017-01-05 11:28:51 +0000276
277 return true;
278}
279
Kristof Beyls65a12c02017-01-30 09:13:18 +0000280bool IRTranslator::translateIndirectBr(const User &U,
281 MachineIRBuilder &MIRBuilder) {
282 const IndirectBrInst &BrInst = cast<IndirectBrInst>(U);
283
284 const unsigned Tgt = getOrCreateVReg(*BrInst.getAddress());
285 MIRBuilder.buildBrIndirect(Tgt);
286
287 // Link successors.
288 MachineBasicBlock &CurBB = MIRBuilder.getMBB();
289 for (const BasicBlock *Succ : BrInst.successors())
290 CurBB.addSuccessor(&getOrCreateBB(*Succ));
291
292 return true;
293}
294
Tim Northoverc53606e2016-12-07 21:29:15 +0000295bool IRTranslator::translateLoad(const User &U, MachineIRBuilder &MIRBuilder) {
Tim Northover357f1be2016-08-10 23:02:41 +0000296 const LoadInst &LI = cast<LoadInst>(U);
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000297
Tim Northover7152dca2016-10-19 15:55:06 +0000298 auto Flags = LI.isVolatile() ? MachineMemOperand::MOVolatile
299 : MachineMemOperand::MONone;
300 Flags |= MachineMemOperand::MOLoad;
Tim Northoverad2b7172016-07-26 20:23:26 +0000301
Tim Northoverad2b7172016-07-26 20:23:26 +0000302 unsigned Res = getOrCreateVReg(LI);
303 unsigned Addr = getOrCreateVReg(*LI.getPointerOperand());
Daniel Sanders8ebec372017-03-07 19:21:23 +0000304 LLT VTy{*LI.getType(), *DL}, PTy{*LI.getPointerOperand()->getType(), *DL};
Tim Northoverad2b7172016-07-26 20:23:26 +0000305 MIRBuilder.buildLoad(
Tim Northover0f140c72016-09-09 11:46:34 +0000306 Res, Addr,
Tim Northover50db7f412016-12-07 21:17:47 +0000307 *MF->getMachineMemOperand(MachinePointerInfo(LI.getPointerOperand()),
308 Flags, DL->getTypeStoreSize(LI.getType()),
Tim Northover48dfa1a2017-02-13 22:14:16 +0000309 getMemOpAlignment(LI), AAMDNodes(), nullptr,
310 LI.getSynchScope(), LI.getOrdering()));
Tim Northoverad2b7172016-07-26 20:23:26 +0000311 return true;
312}
313
Tim Northoverc53606e2016-12-07 21:29:15 +0000314bool IRTranslator::translateStore(const User &U, MachineIRBuilder &MIRBuilder) {
Tim Northover357f1be2016-08-10 23:02:41 +0000315 const StoreInst &SI = cast<StoreInst>(U);
Tim Northover7152dca2016-10-19 15:55:06 +0000316 auto Flags = SI.isVolatile() ? MachineMemOperand::MOVolatile
317 : MachineMemOperand::MONone;
318 Flags |= MachineMemOperand::MOStore;
Tim Northoverad2b7172016-07-26 20:23:26 +0000319
Tim Northoverad2b7172016-07-26 20:23:26 +0000320 unsigned Val = getOrCreateVReg(*SI.getValueOperand());
321 unsigned Addr = getOrCreateVReg(*SI.getPointerOperand());
Daniel Sanders8ebec372017-03-07 19:21:23 +0000322 LLT VTy{*SI.getValueOperand()->getType(), *DL},
323 PTy{*SI.getPointerOperand()->getType(), *DL};
Tim Northoverad2b7172016-07-26 20:23:26 +0000324
325 MIRBuilder.buildStore(
Tim Northover50db7f412016-12-07 21:17:47 +0000326 Val, Addr,
327 *MF->getMachineMemOperand(
328 MachinePointerInfo(SI.getPointerOperand()), Flags,
329 DL->getTypeStoreSize(SI.getValueOperand()->getType()),
Tim Northover48dfa1a2017-02-13 22:14:16 +0000330 getMemOpAlignment(SI), AAMDNodes(), nullptr, SI.getSynchScope(),
331 SI.getOrdering()));
Tim Northoverad2b7172016-07-26 20:23:26 +0000332 return true;
333}
334
Tim Northoverc53606e2016-12-07 21:29:15 +0000335bool IRTranslator::translateExtractValue(const User &U,
336 MachineIRBuilder &MIRBuilder) {
Tim Northoverb6046222016-08-19 20:09:03 +0000337 const Value *Src = U.getOperand(0);
338 Type *Int32Ty = Type::getInt32Ty(U.getContext());
Tim Northover6f80b082016-08-19 17:47:05 +0000339 SmallVector<Value *, 1> Indices;
340
341 // getIndexedOffsetInType is designed for GEPs, so the first index is the
342 // usual array element rather than looking into the actual aggregate.
343 Indices.push_back(ConstantInt::get(Int32Ty, 0));
Tim Northoverb6046222016-08-19 20:09:03 +0000344
345 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&U)) {
346 for (auto Idx : EVI->indices())
347 Indices.push_back(ConstantInt::get(Int32Ty, Idx));
348 } else {
349 for (unsigned i = 1; i < U.getNumOperands(); ++i)
350 Indices.push_back(U.getOperand(i));
351 }
Tim Northover6f80b082016-08-19 17:47:05 +0000352
353 uint64_t Offset = 8 * DL->getIndexedOffsetInType(Src->getType(), Indices);
354
Tim Northoverb6046222016-08-19 20:09:03 +0000355 unsigned Res = getOrCreateVReg(U);
Tim Northoverc2c545b2017-03-06 23:50:28 +0000356 MIRBuilder.buildExtract(Res, getOrCreateVReg(*Src), Offset);
Tim Northover6f80b082016-08-19 17:47:05 +0000357
358 return true;
359}
360
Tim Northoverc53606e2016-12-07 21:29:15 +0000361bool IRTranslator::translateInsertValue(const User &U,
362 MachineIRBuilder &MIRBuilder) {
Tim Northoverb6046222016-08-19 20:09:03 +0000363 const Value *Src = U.getOperand(0);
364 Type *Int32Ty = Type::getInt32Ty(U.getContext());
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000365 SmallVector<Value *, 1> Indices;
366
367 // getIndexedOffsetInType is designed for GEPs, so the first index is the
368 // usual array element rather than looking into the actual aggregate.
369 Indices.push_back(ConstantInt::get(Int32Ty, 0));
Tim Northoverb6046222016-08-19 20:09:03 +0000370
371 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&U)) {
372 for (auto Idx : IVI->indices())
373 Indices.push_back(ConstantInt::get(Int32Ty, Idx));
374 } else {
375 for (unsigned i = 2; i < U.getNumOperands(); ++i)
376 Indices.push_back(U.getOperand(i));
377 }
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000378
379 uint64_t Offset = 8 * DL->getIndexedOffsetInType(Src->getType(), Indices);
380
Tim Northoverb6046222016-08-19 20:09:03 +0000381 unsigned Res = getOrCreateVReg(U);
382 const Value &Inserted = *U.getOperand(1);
Tim Northover0f140c72016-09-09 11:46:34 +0000383 MIRBuilder.buildInsert(Res, getOrCreateVReg(*Src), getOrCreateVReg(Inserted),
384 Offset);
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000385
386 return true;
387}
388
Tim Northoverc53606e2016-12-07 21:29:15 +0000389bool IRTranslator::translateSelect(const User &U,
390 MachineIRBuilder &MIRBuilder) {
Tim Northover0f140c72016-09-09 11:46:34 +0000391 MIRBuilder.buildSelect(getOrCreateVReg(U), getOrCreateVReg(*U.getOperand(0)),
392 getOrCreateVReg(*U.getOperand(1)),
393 getOrCreateVReg(*U.getOperand(2)));
Tim Northover5a28c362016-08-19 20:09:07 +0000394 return true;
395}
396
Tim Northoverc53606e2016-12-07 21:29:15 +0000397bool IRTranslator::translateBitCast(const User &U,
398 MachineIRBuilder &MIRBuilder) {
Ahmed Bougacha5c7924f2017-03-07 20:53:06 +0000399 // If we're bitcasting to the source type, we can reuse the source vreg.
Daniel Sanders8ebec372017-03-07 19:21:23 +0000400 if (LLT{*U.getOperand(0)->getType(), *DL} == LLT{*U.getType(), *DL}) {
Ahmed Bougacha5c7924f2017-03-07 20:53:06 +0000401 // Get the source vreg now, to avoid invalidating ValToVReg.
402 unsigned SrcReg = getOrCreateVReg(*U.getOperand(0));
Tim Northover357f1be2016-08-10 23:02:41 +0000403 unsigned &Reg = ValToVReg[&U];
Ahmed Bougacha5c7924f2017-03-07 20:53:06 +0000404 // If we already assigned a vreg for this bitcast, we can't change that.
405 // Emit a copy to satisfy the users we already emitted.
Tim Northover7552ef52016-08-10 16:51:14 +0000406 if (Reg)
Ahmed Bougacha5c7924f2017-03-07 20:53:06 +0000407 MIRBuilder.buildCopy(Reg, SrcReg);
Tim Northover7552ef52016-08-10 16:51:14 +0000408 else
Ahmed Bougacha5c7924f2017-03-07 20:53:06 +0000409 Reg = SrcReg;
Tim Northover7c9eba92016-07-25 21:01:29 +0000410 return true;
411 }
Tim Northoverc53606e2016-12-07 21:29:15 +0000412 return translateCast(TargetOpcode::G_BITCAST, U, MIRBuilder);
Tim Northover7c9eba92016-07-25 21:01:29 +0000413}
414
Tim Northoverc53606e2016-12-07 21:29:15 +0000415bool IRTranslator::translateCast(unsigned Opcode, const User &U,
416 MachineIRBuilder &MIRBuilder) {
Tim Northover357f1be2016-08-10 23:02:41 +0000417 unsigned Op = getOrCreateVReg(*U.getOperand(0));
418 unsigned Res = getOrCreateVReg(U);
Tim Northover0f140c72016-09-09 11:46:34 +0000419 MIRBuilder.buildInstr(Opcode).addDef(Res).addUse(Op);
Tim Northover7c9eba92016-07-25 21:01:29 +0000420 return true;
421}
422
Tim Northoverc53606e2016-12-07 21:29:15 +0000423bool IRTranslator::translateGetElementPtr(const User &U,
424 MachineIRBuilder &MIRBuilder) {
Tim Northovera7653b32016-09-12 11:20:22 +0000425 // FIXME: support vector GEPs.
426 if (U.getType()->isVectorTy())
427 return false;
428
429 Value &Op0 = *U.getOperand(0);
430 unsigned BaseReg = getOrCreateVReg(Op0);
Daniel Sanders8ebec372017-03-07 19:21:23 +0000431 LLT PtrTy{*Op0.getType(), *DL};
Tim Northovera7653b32016-09-12 11:20:22 +0000432 unsigned PtrSize = DL->getPointerSizeInBits(PtrTy.getAddressSpace());
433 LLT OffsetTy = LLT::scalar(PtrSize);
434
435 int64_t Offset = 0;
436 for (gep_type_iterator GTI = gep_type_begin(&U), E = gep_type_end(&U);
437 GTI != E; ++GTI) {
438 const Value *Idx = GTI.getOperand();
Peter Collingbourne25a40752016-12-02 02:55:30 +0000439 if (StructType *StTy = GTI.getStructTypeOrNull()) {
Tim Northovera7653b32016-09-12 11:20:22 +0000440 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
441 Offset += DL->getStructLayout(StTy)->getElementOffset(Field);
442 continue;
443 } else {
444 uint64_t ElementSize = DL->getTypeAllocSize(GTI.getIndexedType());
445
446 // If this is a scalar constant or a splat vector of constants,
447 // handle it quickly.
448 if (const auto *CI = dyn_cast<ConstantInt>(Idx)) {
449 Offset += ElementSize * CI->getSExtValue();
450 continue;
451 }
452
453 if (Offset != 0) {
454 unsigned NewBaseReg = MRI->createGenericVirtualRegister(PtrTy);
455 unsigned OffsetReg = MRI->createGenericVirtualRegister(OffsetTy);
456 MIRBuilder.buildConstant(OffsetReg, Offset);
457 MIRBuilder.buildGEP(NewBaseReg, BaseReg, OffsetReg);
458
459 BaseReg = NewBaseReg;
460 Offset = 0;
461 }
462
463 // N = N + Idx * ElementSize;
464 unsigned ElementSizeReg = MRI->createGenericVirtualRegister(OffsetTy);
465 MIRBuilder.buildConstant(ElementSizeReg, ElementSize);
466
467 unsigned IdxReg = getOrCreateVReg(*Idx);
468 if (MRI->getType(IdxReg) != OffsetTy) {
469 unsigned NewIdxReg = MRI->createGenericVirtualRegister(OffsetTy);
470 MIRBuilder.buildSExtOrTrunc(NewIdxReg, IdxReg);
471 IdxReg = NewIdxReg;
472 }
473
474 unsigned OffsetReg = MRI->createGenericVirtualRegister(OffsetTy);
475 MIRBuilder.buildMul(OffsetReg, ElementSizeReg, IdxReg);
476
477 unsigned NewBaseReg = MRI->createGenericVirtualRegister(PtrTy);
478 MIRBuilder.buildGEP(NewBaseReg, BaseReg, OffsetReg);
479 BaseReg = NewBaseReg;
480 }
481 }
482
483 if (Offset != 0) {
484 unsigned OffsetReg = MRI->createGenericVirtualRegister(OffsetTy);
485 MIRBuilder.buildConstant(OffsetReg, Offset);
486 MIRBuilder.buildGEP(getOrCreateVReg(U), BaseReg, OffsetReg);
487 return true;
488 }
489
490 MIRBuilder.buildCopy(getOrCreateVReg(U), BaseReg);
491 return true;
492}
493
Tim Northover79f43f12017-01-30 19:33:07 +0000494bool IRTranslator::translateMemfunc(const CallInst &CI,
495 MachineIRBuilder &MIRBuilder,
496 unsigned ID) {
Daniel Sanders8ebec372017-03-07 19:21:23 +0000497 LLT SizeTy{*CI.getArgOperand(2)->getType(), *DL};
Tim Northover79f43f12017-01-30 19:33:07 +0000498 Type *DstTy = CI.getArgOperand(0)->getType();
499 if (cast<PointerType>(DstTy)->getAddressSpace() != 0 ||
Tim Northover3f186032016-10-18 20:03:45 +0000500 SizeTy.getSizeInBits() != DL->getPointerSizeInBits(0))
501 return false;
502
503 SmallVector<CallLowering::ArgInfo, 8> Args;
504 for (int i = 0; i < 3; ++i) {
505 const auto &Arg = CI.getArgOperand(i);
506 Args.emplace_back(getOrCreateVReg(*Arg), Arg->getType());
507 }
508
Tim Northover79f43f12017-01-30 19:33:07 +0000509 const char *Callee;
510 switch (ID) {
511 case Intrinsic::memmove:
512 case Intrinsic::memcpy: {
513 Type *SrcTy = CI.getArgOperand(1)->getType();
514 if(cast<PointerType>(SrcTy)->getAddressSpace() != 0)
515 return false;
516 Callee = ID == Intrinsic::memcpy ? "memcpy" : "memmove";
517 break;
518 }
519 case Intrinsic::memset:
520 Callee = "memset";
521 break;
522 default:
523 return false;
524 }
Tim Northover3f186032016-10-18 20:03:45 +0000525
Tim Northover79f43f12017-01-30 19:33:07 +0000526 return CLI->lowerCall(MIRBuilder, MachineOperand::CreateES(Callee),
Tim Northover3f186032016-10-18 20:03:45 +0000527 CallLowering::ArgInfo(0, CI.getType()), Args);
528}
Tim Northovera7653b32016-09-12 11:20:22 +0000529
Tim Northoverc53606e2016-12-07 21:29:15 +0000530void IRTranslator::getStackGuard(unsigned DstReg,
531 MachineIRBuilder &MIRBuilder) {
Tim Northoverd8b85582017-01-27 21:31:24 +0000532 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
533 MRI->setRegClass(DstReg, TRI->getPointerRegClass(*MF));
Tim Northovercdf23f12016-10-31 18:30:59 +0000534 auto MIB = MIRBuilder.buildInstr(TargetOpcode::LOAD_STACK_GUARD);
535 MIB.addDef(DstReg);
536
Tim Northover50db7f412016-12-07 21:17:47 +0000537 auto &TLI = *MF->getSubtarget().getTargetLowering();
538 Value *Global = TLI.getSDagStackGuard(*MF->getFunction()->getParent());
Tim Northovercdf23f12016-10-31 18:30:59 +0000539 if (!Global)
540 return;
541
542 MachinePointerInfo MPInfo(Global);
Tim Northover50db7f412016-12-07 21:17:47 +0000543 MachineInstr::mmo_iterator MemRefs = MF->allocateMemRefsArray(1);
Tim Northovercdf23f12016-10-31 18:30:59 +0000544 auto Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant |
545 MachineMemOperand::MODereferenceable;
546 *MemRefs =
Tim Northover50db7f412016-12-07 21:17:47 +0000547 MF->getMachineMemOperand(MPInfo, Flags, DL->getPointerSizeInBits() / 8,
548 DL->getPointerABIAlignment());
Tim Northovercdf23f12016-10-31 18:30:59 +0000549 MIB.setMemRefs(MemRefs, MemRefs + 1);
550}
551
Tim Northover1e656ec2016-12-08 22:44:00 +0000552bool IRTranslator::translateOverflowIntrinsic(const CallInst &CI, unsigned Op,
553 MachineIRBuilder &MIRBuilder) {
Daniel Sanders8ebec372017-03-07 19:21:23 +0000554 LLT Ty{*CI.getOperand(0)->getType(), *DL};
Tim Northover1e656ec2016-12-08 22:44:00 +0000555 LLT s1 = LLT::scalar(1);
556 unsigned Width = Ty.getSizeInBits();
557 unsigned Res = MRI->createGenericVirtualRegister(Ty);
558 unsigned Overflow = MRI->createGenericVirtualRegister(s1);
559 auto MIB = MIRBuilder.buildInstr(Op)
560 .addDef(Res)
561 .addDef(Overflow)
562 .addUse(getOrCreateVReg(*CI.getOperand(0)))
563 .addUse(getOrCreateVReg(*CI.getOperand(1)));
564
565 if (Op == TargetOpcode::G_UADDE || Op == TargetOpcode::G_USUBE) {
566 unsigned Zero = MRI->createGenericVirtualRegister(s1);
567 EntryBuilder.buildConstant(Zero, 0);
568 MIB.addUse(Zero);
569 }
570
571 MIRBuilder.buildSequence(getOrCreateVReg(CI), Res, 0, Overflow, Width);
572 return true;
573}
574
Tim Northoverc53606e2016-12-07 21:29:15 +0000575bool IRTranslator::translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
576 MachineIRBuilder &MIRBuilder) {
Tim Northover91c81732016-08-19 17:17:06 +0000577 switch (ID) {
Tim Northover1e656ec2016-12-08 22:44:00 +0000578 default:
579 break;
Tim Northover0e011702017-02-10 19:10:38 +0000580 case Intrinsic::lifetime_start:
581 case Intrinsic::lifetime_end:
582 // Stack coloring is not enabled in O0 (which we care about now) so we can
583 // drop these. Make sure someone notices when we start compiling at higher
584 // opts though.
585 if (MF->getTarget().getOptLevel() != CodeGenOpt::None)
586 return false;
587 return true;
Tim Northover09aac4a2017-01-26 23:39:14 +0000588 case Intrinsic::dbg_declare: {
589 const DbgDeclareInst &DI = cast<DbgDeclareInst>(CI);
590 assert(DI.getVariable() && "Missing variable");
591
592 const Value *Address = DI.getAddress();
593 if (!Address || isa<UndefValue>(Address)) {
594 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
595 return true;
596 }
597
598 unsigned Reg = getOrCreateVReg(*Address);
599 auto RegDef = MRI->def_instr_begin(Reg);
600 assert(DI.getVariable()->isValidLocationForIntrinsic(
601 MIRBuilder.getDebugLoc()) &&
602 "Expected inlined-at fields to agree");
603
604 if (RegDef != MRI->def_instr_end() &&
605 RegDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) {
606 MIRBuilder.buildFIDbgValue(RegDef->getOperand(1).getIndex(),
607 DI.getVariable(), DI.getExpression());
608 } else
609 MIRBuilder.buildDirectDbgValue(Reg, DI.getVariable(), DI.getExpression());
Tim Northoverb58346f2016-12-08 22:44:13 +0000610 return true;
Tim Northover09aac4a2017-01-26 23:39:14 +0000611 }
Tim Northoverd0d025a2017-02-07 20:08:59 +0000612 case Intrinsic::vaend:
613 // No target I know of cares about va_end. Certainly no in-tree target
614 // does. Simplest intrinsic ever!
615 return true;
Tim Northoverf19d4672017-02-08 17:57:20 +0000616 case Intrinsic::vastart: {
617 auto &TLI = *MF->getSubtarget().getTargetLowering();
618 Value *Ptr = CI.getArgOperand(0);
619 unsigned ListSize = TLI.getVaListSizeInBits(*DL) / 8;
620
621 MIRBuilder.buildInstr(TargetOpcode::G_VASTART)
622 .addUse(getOrCreateVReg(*Ptr))
623 .addMemOperand(MF->getMachineMemOperand(
624 MachinePointerInfo(Ptr), MachineMemOperand::MOStore, ListSize, 0));
625 return true;
626 }
Tim Northover09aac4a2017-01-26 23:39:14 +0000627 case Intrinsic::dbg_value: {
628 // This form of DBG_VALUE is target-independent.
629 const DbgValueInst &DI = cast<DbgValueInst>(CI);
630 const Value *V = DI.getValue();
631 assert(DI.getVariable()->isValidLocationForIntrinsic(
632 MIRBuilder.getDebugLoc()) &&
633 "Expected inlined-at fields to agree");
634 if (!V) {
635 // Currently the optimizer can produce this; insert an undef to
636 // help debugging. Probably the optimizer should not do this.
637 MIRBuilder.buildIndirectDbgValue(0, DI.getOffset(), DI.getVariable(),
638 DI.getExpression());
639 } else if (const auto *CI = dyn_cast<Constant>(V)) {
640 MIRBuilder.buildConstDbgValue(*CI, DI.getOffset(), DI.getVariable(),
641 DI.getExpression());
642 } else {
643 unsigned Reg = getOrCreateVReg(*V);
644 // FIXME: This does not handle register-indirect values at offset 0. The
645 // direct/indirect thing shouldn't really be handled by something as
646 // implicit as reg+noreg vs reg+imm in the first palce, but it seems
647 // pretty baked in right now.
648 if (DI.getOffset() != 0)
649 MIRBuilder.buildIndirectDbgValue(Reg, DI.getOffset(), DI.getVariable(),
650 DI.getExpression());
651 else
652 MIRBuilder.buildDirectDbgValue(Reg, DI.getVariable(),
653 DI.getExpression());
654 }
655 return true;
656 }
Tim Northover1e656ec2016-12-08 22:44:00 +0000657 case Intrinsic::uadd_with_overflow:
658 return translateOverflowIntrinsic(CI, TargetOpcode::G_UADDE, MIRBuilder);
659 case Intrinsic::sadd_with_overflow:
660 return translateOverflowIntrinsic(CI, TargetOpcode::G_SADDO, MIRBuilder);
661 case Intrinsic::usub_with_overflow:
662 return translateOverflowIntrinsic(CI, TargetOpcode::G_USUBE, MIRBuilder);
663 case Intrinsic::ssub_with_overflow:
664 return translateOverflowIntrinsic(CI, TargetOpcode::G_SSUBO, MIRBuilder);
665 case Intrinsic::umul_with_overflow:
666 return translateOverflowIntrinsic(CI, TargetOpcode::G_UMULO, MIRBuilder);
667 case Intrinsic::smul_with_overflow:
668 return translateOverflowIntrinsic(CI, TargetOpcode::G_SMULO, MIRBuilder);
Tim Northoverb38b4e22017-02-08 23:23:32 +0000669 case Intrinsic::pow:
670 MIRBuilder.buildInstr(TargetOpcode::G_FPOW)
671 .addDef(getOrCreateVReg(CI))
672 .addUse(getOrCreateVReg(*CI.getArgOperand(0)))
673 .addUse(getOrCreateVReg(*CI.getArgOperand(1)));
674 return true;
Tim Northover3f186032016-10-18 20:03:45 +0000675 case Intrinsic::memcpy:
Tim Northover79f43f12017-01-30 19:33:07 +0000676 case Intrinsic::memmove:
677 case Intrinsic::memset:
678 return translateMemfunc(CI, MIRBuilder, ID);
Tim Northovera9105be2016-11-09 22:39:54 +0000679 case Intrinsic::eh_typeid_for: {
680 GlobalValue *GV = ExtractTypeInfo(CI.getArgOperand(0));
681 unsigned Reg = getOrCreateVReg(CI);
Tim Northover50db7f412016-12-07 21:17:47 +0000682 unsigned TypeID = MF->getTypeIDFor(GV);
Tim Northovera9105be2016-11-09 22:39:54 +0000683 MIRBuilder.buildConstant(Reg, TypeID);
684 return true;
685 }
Tim Northover6e904302016-10-18 20:03:51 +0000686 case Intrinsic::objectsize: {
687 // If we don't know by now, we're never going to know.
688 const ConstantInt *Min = cast<ConstantInt>(CI.getArgOperand(1));
689
690 MIRBuilder.buildConstant(getOrCreateVReg(CI), Min->isZero() ? -1ULL : 0);
691 return true;
692 }
Tim Northovercdf23f12016-10-31 18:30:59 +0000693 case Intrinsic::stackguard:
Tim Northoverc53606e2016-12-07 21:29:15 +0000694 getStackGuard(getOrCreateVReg(CI), MIRBuilder);
Tim Northovercdf23f12016-10-31 18:30:59 +0000695 return true;
696 case Intrinsic::stackprotector: {
Daniel Sanders8ebec372017-03-07 19:21:23 +0000697 LLT PtrTy{*CI.getArgOperand(0)->getType(), *DL};
Tim Northovercdf23f12016-10-31 18:30:59 +0000698 unsigned GuardVal = MRI->createGenericVirtualRegister(PtrTy);
Tim Northoverc53606e2016-12-07 21:29:15 +0000699 getStackGuard(GuardVal, MIRBuilder);
Tim Northovercdf23f12016-10-31 18:30:59 +0000700
701 AllocaInst *Slot = cast<AllocaInst>(CI.getArgOperand(1));
702 MIRBuilder.buildStore(
703 GuardVal, getOrCreateVReg(*Slot),
Tim Northover50db7f412016-12-07 21:17:47 +0000704 *MF->getMachineMemOperand(
705 MachinePointerInfo::getFixedStack(*MF,
706 getOrCreateFrameIndex(*Slot)),
Tim Northovercdf23f12016-10-31 18:30:59 +0000707 MachineMemOperand::MOStore | MachineMemOperand::MOVolatile,
708 PtrTy.getSizeInBits() / 8, 8));
709 return true;
710 }
Tim Northover91c81732016-08-19 17:17:06 +0000711 }
Tim Northover1e656ec2016-12-08 22:44:00 +0000712 return false;
Tim Northover91c81732016-08-19 17:17:06 +0000713}
714
Tim Northoverc53606e2016-12-07 21:29:15 +0000715bool IRTranslator::translateCall(const User &U, MachineIRBuilder &MIRBuilder) {
Tim Northover357f1be2016-08-10 23:02:41 +0000716 const CallInst &CI = cast<CallInst>(U);
Tim Northover50db7f412016-12-07 21:17:47 +0000717 auto TII = MF->getTarget().getIntrinsicInfo();
Tim Northover406024a2016-08-10 21:44:01 +0000718 const Function *F = CI.getCalledFunction();
Tim Northover5fb414d2016-07-29 22:32:36 +0000719
Tim Northover3babfef2017-01-19 23:59:35 +0000720 if (CI.isInlineAsm())
721 return false;
722
Tim Northover406024a2016-08-10 21:44:01 +0000723 if (!F || !F->isIntrinsic()) {
Tim Northover406024a2016-08-10 21:44:01 +0000724 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
725 SmallVector<unsigned, 8> Args;
726 for (auto &Arg: CI.arg_operands())
727 Args.push_back(getOrCreateVReg(*Arg));
728
Tim Northoverfe5f89b2016-08-29 19:07:08 +0000729 return CLI->lowerCall(MIRBuilder, CI, Res, Args, [&]() {
730 return getOrCreateVReg(*CI.getCalledValue());
731 });
Tim Northover406024a2016-08-10 21:44:01 +0000732 }
733
734 Intrinsic::ID ID = F->getIntrinsicID();
735 if (TII && ID == Intrinsic::not_intrinsic)
736 ID = static_cast<Intrinsic::ID>(TII->getIntrinsicID(F));
737
738 assert(ID != Intrinsic::not_intrinsic && "unknown intrinsic");
Tim Northover5fb414d2016-07-29 22:32:36 +0000739
Tim Northoverc53606e2016-12-07 21:29:15 +0000740 if (translateKnownIntrinsic(CI, ID, MIRBuilder))
Tim Northover91c81732016-08-19 17:17:06 +0000741 return true;
742
Tim Northover5fb414d2016-07-29 22:32:36 +0000743 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
744 MachineInstrBuilder MIB =
Tim Northover0f140c72016-09-09 11:46:34 +0000745 MIRBuilder.buildIntrinsic(ID, Res, !CI.doesNotAccessMemory());
Tim Northover5fb414d2016-07-29 22:32:36 +0000746
747 for (auto &Arg : CI.arg_operands()) {
748 if (ConstantInt *CI = dyn_cast<ConstantInt>(Arg))
749 MIB.addImm(CI->getSExtValue());
750 else
751 MIB.addUse(getOrCreateVReg(*Arg));
752 }
753 return true;
754}
755
Tim Northoverc53606e2016-12-07 21:29:15 +0000756bool IRTranslator::translateInvoke(const User &U,
757 MachineIRBuilder &MIRBuilder) {
Tim Northovera9105be2016-11-09 22:39:54 +0000758 const InvokeInst &I = cast<InvokeInst>(U);
Tim Northover50db7f412016-12-07 21:17:47 +0000759 MCContext &Context = MF->getContext();
Tim Northovera9105be2016-11-09 22:39:54 +0000760
761 const BasicBlock *ReturnBB = I.getSuccessor(0);
762 const BasicBlock *EHPadBB = I.getSuccessor(1);
763
764 const Value *Callee(I.getCalledValue());
765 const Function *Fn = dyn_cast<Function>(Callee);
766 if (isa<InlineAsm>(Callee))
767 return false;
768
769 // FIXME: support invoking patchpoint and statepoint intrinsics.
770 if (Fn && Fn->isIntrinsic())
771 return false;
772
773 // FIXME: support whatever these are.
774 if (I.countOperandBundlesOfType(LLVMContext::OB_deopt))
775 return false;
776
777 // FIXME: support Windows exception handling.
778 if (!isa<LandingPadInst>(EHPadBB->front()))
779 return false;
780
781
Matthias Braund0ee66c2016-12-01 19:32:15 +0000782 // Emit the actual call, bracketed by EH_LABELs so that the MF knows about
Tim Northovera9105be2016-11-09 22:39:54 +0000783 // the region covered by the try.
Matthias Braund0ee66c2016-12-01 19:32:15 +0000784 MCSymbol *BeginSymbol = Context.createTempSymbol();
Tim Northovera9105be2016-11-09 22:39:54 +0000785 MIRBuilder.buildInstr(TargetOpcode::EH_LABEL).addSym(BeginSymbol);
786
787 unsigned Res = I.getType()->isVoidTy() ? 0 : getOrCreateVReg(I);
Tim Northover293f7432017-01-31 18:36:11 +0000788 SmallVector<unsigned, 8> Args;
Tim Northovera9105be2016-11-09 22:39:54 +0000789 for (auto &Arg: I.arg_operands())
Tim Northover293f7432017-01-31 18:36:11 +0000790 Args.push_back(getOrCreateVReg(*Arg));
Tim Northovera9105be2016-11-09 22:39:54 +0000791
Tim Northover293f7432017-01-31 18:36:11 +0000792 CLI->lowerCall(MIRBuilder, I, Res, Args,
793 [&]() { return getOrCreateVReg(*I.getCalledValue()); });
Tim Northovera9105be2016-11-09 22:39:54 +0000794
Matthias Braund0ee66c2016-12-01 19:32:15 +0000795 MCSymbol *EndSymbol = Context.createTempSymbol();
Tim Northovera9105be2016-11-09 22:39:54 +0000796 MIRBuilder.buildInstr(TargetOpcode::EH_LABEL).addSym(EndSymbol);
797
798 // FIXME: track probabilities.
799 MachineBasicBlock &EHPadMBB = getOrCreateBB(*EHPadBB),
800 &ReturnMBB = getOrCreateBB(*ReturnBB);
Tim Northover50db7f412016-12-07 21:17:47 +0000801 MF->addInvoke(&EHPadMBB, BeginSymbol, EndSymbol);
Tim Northovera9105be2016-11-09 22:39:54 +0000802 MIRBuilder.getMBB().addSuccessor(&ReturnMBB);
803 MIRBuilder.getMBB().addSuccessor(&EHPadMBB);
Tim Northoverc6bfa482017-01-31 20:12:18 +0000804 MIRBuilder.buildBr(ReturnMBB);
Tim Northovera9105be2016-11-09 22:39:54 +0000805
806 return true;
807}
808
Tim Northoverc53606e2016-12-07 21:29:15 +0000809bool IRTranslator::translateLandingPad(const User &U,
810 MachineIRBuilder &MIRBuilder) {
Tim Northovera9105be2016-11-09 22:39:54 +0000811 const LandingPadInst &LP = cast<LandingPadInst>(U);
812
813 MachineBasicBlock &MBB = MIRBuilder.getMBB();
Matthias Braund0ee66c2016-12-01 19:32:15 +0000814 addLandingPadInfo(LP, MBB);
Tim Northovera9105be2016-11-09 22:39:54 +0000815
816 MBB.setIsEHPad();
817
818 // If there aren't registers to copy the values into (e.g., during SjLj
819 // exceptions), then don't bother.
Tim Northover50db7f412016-12-07 21:17:47 +0000820 auto &TLI = *MF->getSubtarget().getTargetLowering();
821 const Constant *PersonalityFn = MF->getFunction()->getPersonalityFn();
Tim Northovera9105be2016-11-09 22:39:54 +0000822 if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 &&
823 TLI.getExceptionSelectorRegister(PersonalityFn) == 0)
824 return true;
825
826 // If landingpad's return type is token type, we don't create DAG nodes
827 // for its exception pointer and selector value. The extraction of exception
828 // pointer or selector value from token type landingpads is not currently
829 // supported.
830 if (LP.getType()->isTokenTy())
831 return true;
832
833 // Add a label to mark the beginning of the landing pad. Deletion of the
834 // landing pad can thus be detected via the MachineModuleInfo.
835 MIRBuilder.buildInstr(TargetOpcode::EH_LABEL)
Tim Northover50db7f412016-12-07 21:17:47 +0000836 .addSym(MF->addLandingPad(&MBB));
Tim Northovera9105be2016-11-09 22:39:54 +0000837
Justin Bognera0295312017-01-25 00:16:53 +0000838 SmallVector<LLT, 2> Tys;
839 for (Type *Ty : cast<StructType>(LP.getType())->elements())
Daniel Sanders8ebec372017-03-07 19:21:23 +0000840 Tys.push_back(LLT{*Ty, *DL});
Justin Bognera0295312017-01-25 00:16:53 +0000841 assert(Tys.size() == 2 && "Only two-valued landingpads are supported");
842
Tim Northovera9105be2016-11-09 22:39:54 +0000843 // Mark exception register as live in.
844 SmallVector<unsigned, 2> Regs;
845 SmallVector<uint64_t, 2> Offsets;
Tim Northovera9105be2016-11-09 22:39:54 +0000846 if (unsigned Reg = TLI.getExceptionPointerRegister(PersonalityFn)) {
Tim Northoverc9bc8a52017-01-27 21:31:17 +0000847 MBB.addLiveIn(Reg);
Justin Bognera0295312017-01-25 00:16:53 +0000848 unsigned VReg = MRI->createGenericVirtualRegister(Tys[0]);
Tim Northovera9105be2016-11-09 22:39:54 +0000849 MIRBuilder.buildCopy(VReg, Reg);
850 Regs.push_back(VReg);
851 Offsets.push_back(0);
852 }
853
854 if (unsigned Reg = TLI.getExceptionSelectorRegister(PersonalityFn)) {
Tim Northoverc9bc8a52017-01-27 21:31:17 +0000855 MBB.addLiveIn(Reg);
Tim Northoverc9449702017-01-30 20:52:42 +0000856
857 // N.b. the exception selector register always has pointer type and may not
858 // match the actual IR-level type in the landingpad so an extra cast is
859 // needed.
860 unsigned PtrVReg = MRI->createGenericVirtualRegister(Tys[0]);
861 MIRBuilder.buildCopy(PtrVReg, Reg);
862
Justin Bognera0295312017-01-25 00:16:53 +0000863 unsigned VReg = MRI->createGenericVirtualRegister(Tys[1]);
Tim Northoverc9449702017-01-30 20:52:42 +0000864 MIRBuilder.buildInstr(TargetOpcode::G_PTRTOINT)
865 .addDef(VReg)
866 .addUse(PtrVReg);
Tim Northovera9105be2016-11-09 22:39:54 +0000867 Regs.push_back(VReg);
Justin Bognera0295312017-01-25 00:16:53 +0000868 Offsets.push_back(Tys[0].getSizeInBits());
Tim Northovera9105be2016-11-09 22:39:54 +0000869 }
870
871 MIRBuilder.buildSequence(getOrCreateVReg(LP), Regs, Offsets);
872 return true;
873}
874
Tim Northoverc3e3f592017-02-03 18:22:45 +0000875bool IRTranslator::translateAlloca(const User &U,
876 MachineIRBuilder &MIRBuilder) {
877 auto &AI = cast<AllocaInst>(U);
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000878
Tim Northoverc3e3f592017-02-03 18:22:45 +0000879 if (AI.isStaticAlloca()) {
880 unsigned Res = getOrCreateVReg(AI);
881 int FI = getOrCreateFrameIndex(AI);
882 MIRBuilder.buildFrameIndex(Res, FI);
883 return true;
884 }
885
886 // Now we're in the harder dynamic case.
887 Type *Ty = AI.getAllocatedType();
888 unsigned Align =
889 std::max((unsigned)DL->getPrefTypeAlignment(Ty), AI.getAlignment());
890
891 unsigned NumElts = getOrCreateVReg(*AI.getArraySize());
892
893 LLT IntPtrTy = LLT::scalar(DL->getPointerSizeInBits());
894 if (MRI->getType(NumElts) != IntPtrTy) {
895 unsigned ExtElts = MRI->createGenericVirtualRegister(IntPtrTy);
896 MIRBuilder.buildZExtOrTrunc(ExtElts, NumElts);
897 NumElts = ExtElts;
898 }
899
900 unsigned AllocSize = MRI->createGenericVirtualRegister(IntPtrTy);
901 unsigned TySize = MRI->createGenericVirtualRegister(IntPtrTy);
Tim Northoverc2f89562017-02-14 20:56:18 +0000902 MIRBuilder.buildConstant(TySize, -DL->getTypeAllocSize(Ty));
Tim Northoverc3e3f592017-02-03 18:22:45 +0000903 MIRBuilder.buildMul(AllocSize, NumElts, TySize);
904
Daniel Sanders8ebec372017-03-07 19:21:23 +0000905 LLT PtrTy = LLT{*AI.getType(), *DL};
Tim Northoverc3e3f592017-02-03 18:22:45 +0000906 auto &TLI = *MF->getSubtarget().getTargetLowering();
907 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
908
909 unsigned SPTmp = MRI->createGenericVirtualRegister(PtrTy);
910 MIRBuilder.buildCopy(SPTmp, SPReg);
911
Tim Northoverc2f89562017-02-14 20:56:18 +0000912 unsigned AllocTmp = MRI->createGenericVirtualRegister(PtrTy);
913 MIRBuilder.buildGEP(AllocTmp, SPTmp, AllocSize);
Tim Northoverc3e3f592017-02-03 18:22:45 +0000914
915 // Handle alignment. We have to realign if the allocation granule was smaller
916 // than stack alignment, or the specific alloca requires more than stack
917 // alignment.
918 unsigned StackAlign =
919 MF->getSubtarget().getFrameLowering()->getStackAlignment();
920 Align = std::max(Align, StackAlign);
921 if (Align > StackAlign || DL->getTypeAllocSize(Ty) % StackAlign != 0) {
922 // Round the size of the allocation up to the stack alignment size
923 // by add SA-1 to the size. This doesn't overflow because we're computing
924 // an address inside an alloca.
Tim Northoverc2f89562017-02-14 20:56:18 +0000925 unsigned AlignedAlloc = MRI->createGenericVirtualRegister(PtrTy);
926 MIRBuilder.buildPtrMask(AlignedAlloc, AllocTmp, Log2_32(Align));
927 AllocTmp = AlignedAlloc;
Tim Northoverc3e3f592017-02-03 18:22:45 +0000928 }
929
Tim Northoverc2f89562017-02-14 20:56:18 +0000930 MIRBuilder.buildCopy(SPReg, AllocTmp);
931 MIRBuilder.buildCopy(getOrCreateVReg(AI), AllocTmp);
Tim Northoverc3e3f592017-02-03 18:22:45 +0000932
933 MF->getFrameInfo().CreateVariableSizedObject(Align ? Align : 1, &AI);
934 assert(MF->getFrameInfo().hasVarSizedObjects());
Tim Northoverbd505462016-07-22 16:59:52 +0000935 return true;
936}
937
Tim Northover4a652222017-02-15 23:22:33 +0000938bool IRTranslator::translateVAArg(const User &U, MachineIRBuilder &MIRBuilder) {
939 // FIXME: We may need more info about the type. Because of how LLT works,
940 // we're completely discarding the i64/double distinction here (amongst
941 // others). Fortunately the ABIs I know of where that matters don't use va_arg
942 // anyway but that's not guaranteed.
943 MIRBuilder.buildInstr(TargetOpcode::G_VAARG)
944 .addDef(getOrCreateVReg(U))
945 .addUse(getOrCreateVReg(*U.getOperand(0)))
946 .addImm(DL->getABITypeAlignment(U.getType()));
947 return true;
948}
949
Tim Northoverc53606e2016-12-07 21:29:15 +0000950bool IRTranslator::translatePHI(const User &U, MachineIRBuilder &MIRBuilder) {
Tim Northover357f1be2016-08-10 23:02:41 +0000951 const PHINode &PI = cast<PHINode>(U);
Tim Northover25d12862016-09-09 11:47:31 +0000952 auto MIB = MIRBuilder.buildInstr(TargetOpcode::PHI);
Tim Northover97d0cb32016-08-05 17:16:40 +0000953 MIB.addDef(getOrCreateVReg(PI));
954
955 PendingPHIs.emplace_back(&PI, MIB.getInstr());
956 return true;
957}
958
959void IRTranslator::finishPendingPhis() {
960 for (std::pair<const PHINode *, MachineInstr *> &Phi : PendingPHIs) {
961 const PHINode *PI = Phi.first;
Tim Northoverc53606e2016-12-07 21:29:15 +0000962 MachineInstrBuilder MIB(*MF, Phi.second);
Tim Northover97d0cb32016-08-05 17:16:40 +0000963
964 // All MachineBasicBlocks exist, add them to the PHI. We assume IRTranslator
965 // won't create extra control flow here, otherwise we need to find the
966 // dominating predecessor here (or perhaps force the weirder IRTranslators
967 // to provide a simple boundary).
Tim Northoverb6636fd2017-01-17 22:13:50 +0000968 SmallSet<const BasicBlock *, 4> HandledPreds;
969
Tim Northover97d0cb32016-08-05 17:16:40 +0000970 for (unsigned i = 0; i < PI->getNumIncomingValues(); ++i) {
Tim Northoverb6636fd2017-01-17 22:13:50 +0000971 auto IRPred = PI->getIncomingBlock(i);
972 if (HandledPreds.count(IRPred))
973 continue;
974
975 HandledPreds.insert(IRPred);
976 unsigned ValReg = getOrCreateVReg(*PI->getIncomingValue(i));
977 for (auto Pred : getMachinePredBBs({IRPred, PI->getParent()})) {
978 assert(Pred->isSuccessor(MIB->getParent()) &&
979 "incorrect CFG at MachineBasicBlock level");
980 MIB.addUse(ValReg);
981 MIB.addMBB(Pred);
982 }
Tim Northover97d0cb32016-08-05 17:16:40 +0000983 }
984 }
985}
986
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000987bool IRTranslator::translate(const Instruction &Inst) {
Tim Northoverc53606e2016-12-07 21:29:15 +0000988 CurBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000989 switch(Inst.getOpcode()) {
Tim Northover357f1be2016-08-10 23:02:41 +0000990#define HANDLE_INST(NUM, OPCODE, CLASS) \
Tim Northoverc53606e2016-12-07 21:29:15 +0000991 case Instruction::OPCODE: return translate##OPCODE(Inst, CurBuilder);
Tim Northover357f1be2016-08-10 23:02:41 +0000992#include "llvm/IR/Instruction.def"
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000993 default:
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000994 if (!TPC->isGlobalISelAbortEnabled())
995 return false;
Tim Northover357f1be2016-08-10 23:02:41 +0000996 llvm_unreachable("unknown opcode");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000997 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000998}
999
Tim Northover5ed648e2016-08-09 21:28:04 +00001000bool IRTranslator::translate(const Constant &C, unsigned Reg) {
Tim Northoverd403a3d2016-08-09 23:01:30 +00001001 if (auto CI = dyn_cast<ConstantInt>(&C))
Tim Northovercc35f902016-12-05 21:54:17 +00001002 EntryBuilder.buildConstant(Reg, *CI);
Tim Northoverb16734f2016-08-19 20:09:15 +00001003 else if (auto CF = dyn_cast<ConstantFP>(&C))
Tim Northover0f140c72016-09-09 11:46:34 +00001004 EntryBuilder.buildFConstant(Reg, *CF);
Tim Northoverd403a3d2016-08-09 23:01:30 +00001005 else if (isa<UndefValue>(C))
Tim Northover81dafc12017-03-06 18:36:40 +00001006 EntryBuilder.buildUndef(Reg);
Tim Northover8e0c53a2016-08-11 21:40:55 +00001007 else if (isa<ConstantPointerNull>(C))
Tim Northover9267ac52016-12-05 21:47:07 +00001008 EntryBuilder.buildConstant(Reg, 0);
Tim Northover032548f2016-09-12 12:10:41 +00001009 else if (auto GV = dyn_cast<GlobalValue>(&C))
1010 EntryBuilder.buildGlobalValue(Reg, GV);
Tim Northover357f1be2016-08-10 23:02:41 +00001011 else if (auto CE = dyn_cast<ConstantExpr>(&C)) {
1012 switch(CE->getOpcode()) {
1013#define HANDLE_INST(NUM, OPCODE, CLASS) \
Tim Northoverc53606e2016-12-07 21:29:15 +00001014 case Instruction::OPCODE: return translate##OPCODE(*CE, EntryBuilder);
Tim Northover357f1be2016-08-10 23:02:41 +00001015#include "llvm/IR/Instruction.def"
1016 default:
Quentin Colombet3bb32cc2016-08-26 23:49:05 +00001017 if (!TPC->isGlobalISelAbortEnabled())
1018 return false;
Tim Northover357f1be2016-08-10 23:02:41 +00001019 llvm_unreachable("unknown opcode");
1020 }
Quentin Colombet3bb32cc2016-08-26 23:49:05 +00001021 } else if (!TPC->isGlobalISelAbortEnabled())
1022 return false;
1023 else
Tim Northoverd403a3d2016-08-09 23:01:30 +00001024 llvm_unreachable("unhandled constant kind");
Tim Northover5ed648e2016-08-09 21:28:04 +00001025
Tim Northoverd403a3d2016-08-09 23:01:30 +00001026 return true;
Tim Northover5ed648e2016-08-09 21:28:04 +00001027}
1028
Tim Northover0d510442016-08-11 16:21:29 +00001029void IRTranslator::finalizeFunction() {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +00001030 // Release the memory used by the different maps we
1031 // needed during the translation.
Tim Northover800638f2016-12-05 23:10:19 +00001032 PendingPHIs.clear();
Quentin Colombetccd77252016-02-11 21:48:32 +00001033 ValToVReg.clear();
Tim Northovercdf23f12016-10-31 18:30:59 +00001034 FrameIndices.clear();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +00001035 Constants.clear();
Tim Northoverb6636fd2017-01-17 22:13:50 +00001036 MachinePreds.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +00001037}
1038
Tim Northover50db7f412016-12-07 21:17:47 +00001039bool IRTranslator::runOnMachineFunction(MachineFunction &CurMF) {
1040 MF = &CurMF;
1041 const Function &F = *MF->getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +00001042 if (F.empty())
1043 return false;
Tim Northover50db7f412016-12-07 21:17:47 +00001044 CLI = MF->getSubtarget().getCallLowering();
Tim Northoverc53606e2016-12-07 21:29:15 +00001045 CurBuilder.setMF(*MF);
Tim Northover50db7f412016-12-07 21:17:47 +00001046 EntryBuilder.setMF(*MF);
1047 MRI = &MF->getRegInfo();
Tim Northoverbd505462016-07-22 16:59:52 +00001048 DL = &F.getParent()->getDataLayout();
Quentin Colombet3bb32cc2016-08-26 23:49:05 +00001049 TPC = &getAnalysis<TargetPassConfig>();
Ahmed Bougachaae9dade2017-02-23 21:05:42 +00001050 ORE = make_unique<OptimizationRemarkEmitter>(&F);
Tim Northoverbd505462016-07-22 16:59:52 +00001051
Tim Northover14e7f732016-08-05 17:50:36 +00001052 assert(PendingPHIs.empty() && "stale PHIs");
1053
Ahmed Bougachaeceabdd2017-02-23 23:57:28 +00001054 // Release the per-function state when we return, whether we succeeded or not.
1055 auto FinalizeOnReturn = make_scope_exit([this]() { finalizeFunction(); });
1056
Tim Northover05cc4852016-12-07 21:05:38 +00001057 // Setup a separate basic-block for the arguments and constants, falling
1058 // through to the IR-level Function's entry block.
Tim Northover50db7f412016-12-07 21:17:47 +00001059 MachineBasicBlock *EntryBB = MF->CreateMachineBasicBlock();
1060 MF->push_back(EntryBB);
Tim Northover05cc4852016-12-07 21:05:38 +00001061 EntryBB->addSuccessor(&getOrCreateBB(F.front()));
1062 EntryBuilder.setMBB(*EntryBB);
1063
1064 // Lower the actual args into this basic block.
Quentin Colombetfd9d0a02016-02-11 19:59:41 +00001065 SmallVector<unsigned, 8> VRegArgs;
1066 for (const Argument &Arg: F.args())
Quentin Colombete225e252016-03-11 17:27:54 +00001067 VRegArgs.push_back(getOrCreateVReg(Arg));
Ahmed Bougacha8f9e99b2017-02-24 00:34:41 +00001068 if (!CLI->lowerFormalArguments(EntryBuilder, F, VRegArgs)) {
Ahmed Bougacha7c88a4e2017-02-24 00:34:44 +00001069 OptimizationRemarkMissed R("gisel-irtranslator", "GISelFailure",
1070 MF->getFunction()->getSubprogram(),
Ahmed Bougachaae9dade2017-02-23 21:05:42 +00001071 &MF->getFunction()->getEntryBlock());
1072 R << "unable to lower arguments: " << ore::NV("Prototype", F.getType());
1073 reportTranslationError(*MF, *TPC, *ORE, R);
Ahmed Bougachaae9dade2017-02-23 21:05:42 +00001074 return false;
Quentin Colombet3bb32cc2016-08-26 23:49:05 +00001075 }
Quentin Colombetfd9d0a02016-02-11 19:59:41 +00001076
Tim Northover05cc4852016-12-07 21:05:38 +00001077 // And translate the function!
Quentin Colombet2ecff3b2016-02-10 22:59:27 +00001078 for (const BasicBlock &BB: F) {
Quentin Colombet53237a92016-03-11 17:27:43 +00001079 MachineBasicBlock &MBB = getOrCreateBB(BB);
Quentin Colombet91ebd712016-03-11 17:27:47 +00001080 // Set the insertion point of all the following translations to
1081 // the end of this basic block.
Tim Northoverc53606e2016-12-07 21:29:15 +00001082 CurBuilder.setMBB(MBB);
Tim Northovera9105be2016-11-09 22:39:54 +00001083
Quentin Colombet2ecff3b2016-02-10 22:59:27 +00001084 for (const Instruction &Inst: BB) {
Ahmed Bougacha8f9e99b2017-02-24 00:34:41 +00001085 if (translate(Inst))
1086 continue;
Ahmed Bougachaae9dade2017-02-23 21:05:42 +00001087
Ahmed Bougacha8f9e99b2017-02-24 00:34:41 +00001088 std::string InstStrStorage;
1089 raw_string_ostream InstStr(InstStrStorage);
1090 InstStr << Inst;
1091
Ahmed Bougacha7daaf882017-02-24 00:34:47 +00001092 OptimizationRemarkMissed R("gisel-irtranslator", "GISelFailure",
1093 Inst.getDebugLoc(), &BB);
Ahmed Bougacha8f9e99b2017-02-24 00:34:41 +00001094 R << "unable to translate instruction: " << ore::NV("Opcode", &Inst)
1095 << ": '" << InstStr.str() << "'";
1096 reportTranslationError(*MF, *TPC, *ORE, R);
1097 return false;
Quentin Colombet2ecff3b2016-02-10 22:59:27 +00001098 }
1099 }
Tim Northover72eebfa2016-07-12 22:23:42 +00001100
Ahmed Bougacha4f8dd022017-02-23 23:57:36 +00001101 finishPendingPhis();
Tim Northover97d0cb32016-08-05 17:16:40 +00001102
Ahmed Bougacha4f8dd022017-02-23 23:57:36 +00001103 // Now that the MachineFrameInfo has been configured, no further changes to
1104 // the reserved registers are possible.
1105 MRI->freezeReservedRegs(*MF);
Quentin Colombet327f9422016-12-15 23:32:25 +00001106
Ahmed Bougacha4f8dd022017-02-23 23:57:36 +00001107 // Merge the argument lowering and constants block with its single
1108 // successor, the LLVM-IR entry block. We want the basic block to
1109 // be maximal.
1110 assert(EntryBB->succ_size() == 1 &&
1111 "Custom BB used for lowering should have only one successor");
1112 // Get the successor of the current entry block.
1113 MachineBasicBlock &NewEntryBB = **EntryBB->succ_begin();
1114 assert(NewEntryBB.pred_size() == 1 &&
1115 "LLVM-IR entry block has a predecessor!?");
1116 // Move all the instruction from the current entry block to the
1117 // new entry block.
1118 NewEntryBB.splice(NewEntryBB.begin(), EntryBB, EntryBB->begin(),
1119 EntryBB->end());
Quentin Colombet327f9422016-12-15 23:32:25 +00001120
Ahmed Bougacha4f8dd022017-02-23 23:57:36 +00001121 // Update the live-in information for the new entry block.
1122 for (const MachineBasicBlock::RegisterMaskPair &LiveIn : EntryBB->liveins())
1123 NewEntryBB.addLiveIn(LiveIn);
1124 NewEntryBB.sortUniqueLiveIns();
Quentin Colombet327f9422016-12-15 23:32:25 +00001125
Ahmed Bougacha4f8dd022017-02-23 23:57:36 +00001126 // Get rid of the now empty basic block.
1127 EntryBB->removeSuccessor(&NewEntryBB);
1128 MF->remove(EntryBB);
1129 MF->DeleteMachineBasicBlock(EntryBB);
Quentin Colombet327f9422016-12-15 23:32:25 +00001130
Ahmed Bougacha4f8dd022017-02-23 23:57:36 +00001131 assert(&MF->front() == &NewEntryBB &&
1132 "New entry wasn't next in the list of basic block!");
Tim Northover800638f2016-12-05 23:10:19 +00001133
Quentin Colombet105cf2b2016-01-20 20:58:56 +00001134 return false;
1135}