blob: 0602e48038332968971d055d0ac081a43e7e8322 [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");
85 unsigned VReg = MRI->createGenericVirtualRegister(LLT{*Val.getType(), *DL});
86 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
Tim Northoverc53606e2016-12-07 21:29:15 +0000174bool IRTranslator::translateCompare(const User &U,
175 MachineIRBuilder &MIRBuilder) {
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000176 const CmpInst *CI = dyn_cast<CmpInst>(&U);
177 unsigned Op0 = getOrCreateVReg(*U.getOperand(0));
178 unsigned Op1 = getOrCreateVReg(*U.getOperand(1));
179 unsigned Res = getOrCreateVReg(U);
180 CmpInst::Predicate Pred =
181 CI ? CI->getPredicate() : static_cast<CmpInst::Predicate>(
182 cast<ConstantExpr>(U).getPredicate());
Tim Northoverde3aea0412016-08-17 20:25:25 +0000183
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000184 if (CmpInst::isIntPredicate(Pred))
Tim Northover0f140c72016-09-09 11:46:34 +0000185 MIRBuilder.buildICmp(Pred, Res, Op0, Op1);
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000186 else
Tim Northover0f140c72016-09-09 11:46:34 +0000187 MIRBuilder.buildFCmp(Pred, Res, Op0, Op1);
Tim Northoverd5c23bc2016-08-19 20:48:16 +0000188
Tim Northoverde3aea0412016-08-17 20:25:25 +0000189 return true;
190}
191
Tim Northoverc53606e2016-12-07 21:29:15 +0000192bool IRTranslator::translateRet(const User &U, MachineIRBuilder &MIRBuilder) {
Tim Northover357f1be2016-08-10 23:02:41 +0000193 const ReturnInst &RI = cast<ReturnInst>(U);
Tim Northover0d56e052016-07-29 18:11:21 +0000194 const Value *Ret = RI.getReturnValue();
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000195 // The target may mess up with the insertion point, but
196 // this is not important as a return is the last instruction
197 // of the block anyway.
Tom Stellardb72a65f2016-04-14 17:23:33 +0000198 return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000199}
200
Tim Northoverc53606e2016-12-07 21:29:15 +0000201bool IRTranslator::translateBr(const User &U, MachineIRBuilder &MIRBuilder) {
Tim Northover357f1be2016-08-10 23:02:41 +0000202 const BranchInst &BrInst = cast<BranchInst>(U);
Tim Northover69c2ba52016-07-29 17:58:00 +0000203 unsigned Succ = 0;
204 if (!BrInst.isUnconditional()) {
205 // We want a G_BRCOND to the true BB followed by an unconditional branch.
206 unsigned Tst = getOrCreateVReg(*BrInst.getCondition());
207 const BasicBlock &TrueTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ++));
208 MachineBasicBlock &TrueBB = getOrCreateBB(TrueTgt);
Tim Northover0f140c72016-09-09 11:46:34 +0000209 MIRBuilder.buildBrCond(Tst, TrueBB);
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000210 }
Tim Northover69c2ba52016-07-29 17:58:00 +0000211
212 const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ));
213 MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt);
214 MIRBuilder.buildBr(TgtBB);
215
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000216 // Link successors.
217 MachineBasicBlock &CurBB = MIRBuilder.getMBB();
218 for (const BasicBlock *Succ : BrInst.successors())
219 CurBB.addSuccessor(&getOrCreateBB(*Succ));
220 return true;
221}
222
Kristof Beylseced0712017-01-05 11:28:51 +0000223bool IRTranslator::translateSwitch(const User &U,
224 MachineIRBuilder &MIRBuilder) {
225 // For now, just translate as a chain of conditional branches.
226 // FIXME: could we share most of the logic/code in
227 // SelectionDAGBuilder::visitSwitch between SelectionDAG and GlobalISel?
228 // At first sight, it seems most of the logic in there is independent of
229 // SelectionDAG-specifics and a lot of work went in to optimize switch
230 // lowering in there.
231
232 const SwitchInst &SwInst = cast<SwitchInst>(U);
233 const unsigned SwCondValue = getOrCreateVReg(*SwInst.getCondition());
Tim Northoverb6636fd2017-01-17 22:13:50 +0000234 const BasicBlock *OrigBB = SwInst.getParent();
Kristof Beylseced0712017-01-05 11:28:51 +0000235
236 LLT LLTi1 = LLT(*Type::getInt1Ty(U.getContext()), *DL);
237 for (auto &CaseIt : SwInst.cases()) {
238 const unsigned CaseValueReg = getOrCreateVReg(*CaseIt.getCaseValue());
239 const unsigned Tst = MRI->createGenericVirtualRegister(LLTi1);
240 MIRBuilder.buildICmp(CmpInst::ICMP_EQ, Tst, CaseValueReg, SwCondValue);
Tim Northoverb6636fd2017-01-17 22:13:50 +0000241 MachineBasicBlock &CurMBB = MIRBuilder.getMBB();
242 const BasicBlock *TrueBB = CaseIt.getCaseSuccessor();
243 MachineBasicBlock &TrueMBB = getOrCreateBB(*TrueBB);
Kristof Beylseced0712017-01-05 11:28:51 +0000244
Tim Northoverb6636fd2017-01-17 22:13:50 +0000245 MIRBuilder.buildBrCond(Tst, TrueMBB);
246 CurMBB.addSuccessor(&TrueMBB);
247 addMachineCFGPred({OrigBB, TrueBB}, &CurMBB);
Kristof Beylseced0712017-01-05 11:28:51 +0000248
Tim Northoverb6636fd2017-01-17 22:13:50 +0000249 MachineBasicBlock *FalseMBB =
Kristof Beylseced0712017-01-05 11:28:51 +0000250 MF->CreateMachineBasicBlock(SwInst.getParent());
Tim Northoverb6636fd2017-01-17 22:13:50 +0000251 MF->push_back(FalseMBB);
252 MIRBuilder.buildBr(*FalseMBB);
253 CurMBB.addSuccessor(FalseMBB);
Kristof Beylseced0712017-01-05 11:28:51 +0000254
Tim Northoverb6636fd2017-01-17 22:13:50 +0000255 MIRBuilder.setMBB(*FalseMBB);
Kristof Beylseced0712017-01-05 11:28:51 +0000256 }
257 // handle default case
Tim Northoverb6636fd2017-01-17 22:13:50 +0000258 const BasicBlock *DefaultBB = SwInst.getDefaultDest();
259 MachineBasicBlock &DefaultMBB = getOrCreateBB(*DefaultBB);
260 MIRBuilder.buildBr(DefaultMBB);
261 MachineBasicBlock &CurMBB = MIRBuilder.getMBB();
262 CurMBB.addSuccessor(&DefaultMBB);
263 addMachineCFGPred({OrigBB, DefaultBB}, &CurMBB);
Kristof Beylseced0712017-01-05 11:28:51 +0000264
265 return true;
266}
267
Kristof Beyls65a12c02017-01-30 09:13:18 +0000268bool IRTranslator::translateIndirectBr(const User &U,
269 MachineIRBuilder &MIRBuilder) {
270 const IndirectBrInst &BrInst = cast<IndirectBrInst>(U);
271
272 const unsigned Tgt = getOrCreateVReg(*BrInst.getAddress());
273 MIRBuilder.buildBrIndirect(Tgt);
274
275 // Link successors.
276 MachineBasicBlock &CurBB = MIRBuilder.getMBB();
277 for (const BasicBlock *Succ : BrInst.successors())
278 CurBB.addSuccessor(&getOrCreateBB(*Succ));
279
280 return true;
281}
282
Tim Northoverc53606e2016-12-07 21:29:15 +0000283bool IRTranslator::translateLoad(const User &U, MachineIRBuilder &MIRBuilder) {
Tim Northover357f1be2016-08-10 23:02:41 +0000284 const LoadInst &LI = cast<LoadInst>(U);
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000285
Tim Northover7152dca2016-10-19 15:55:06 +0000286 auto Flags = LI.isVolatile() ? MachineMemOperand::MOVolatile
287 : MachineMemOperand::MONone;
288 Flags |= MachineMemOperand::MOLoad;
Tim Northoverad2b7172016-07-26 20:23:26 +0000289
Tim Northoverad2b7172016-07-26 20:23:26 +0000290 unsigned Res = getOrCreateVReg(LI);
291 unsigned Addr = getOrCreateVReg(*LI.getPointerOperand());
Tim Northover5ae83502016-09-15 09:20:34 +0000292 LLT VTy{*LI.getType(), *DL}, PTy{*LI.getPointerOperand()->getType(), *DL};
Tim Northoverad2b7172016-07-26 20:23:26 +0000293 MIRBuilder.buildLoad(
Tim Northover0f140c72016-09-09 11:46:34 +0000294 Res, Addr,
Tim Northover50db7f412016-12-07 21:17:47 +0000295 *MF->getMachineMemOperand(MachinePointerInfo(LI.getPointerOperand()),
296 Flags, DL->getTypeStoreSize(LI.getType()),
Tim Northover48dfa1a2017-02-13 22:14:16 +0000297 getMemOpAlignment(LI), AAMDNodes(), nullptr,
298 LI.getSynchScope(), LI.getOrdering()));
Tim Northoverad2b7172016-07-26 20:23:26 +0000299 return true;
300}
301
Tim Northoverc53606e2016-12-07 21:29:15 +0000302bool IRTranslator::translateStore(const User &U, MachineIRBuilder &MIRBuilder) {
Tim Northover357f1be2016-08-10 23:02:41 +0000303 const StoreInst &SI = cast<StoreInst>(U);
Tim Northover7152dca2016-10-19 15:55:06 +0000304 auto Flags = SI.isVolatile() ? MachineMemOperand::MOVolatile
305 : MachineMemOperand::MONone;
306 Flags |= MachineMemOperand::MOStore;
Tim Northoverad2b7172016-07-26 20:23:26 +0000307
Tim Northoverad2b7172016-07-26 20:23:26 +0000308 unsigned Val = getOrCreateVReg(*SI.getValueOperand());
309 unsigned Addr = getOrCreateVReg(*SI.getPointerOperand());
Tim Northover5ae83502016-09-15 09:20:34 +0000310 LLT VTy{*SI.getValueOperand()->getType(), *DL},
311 PTy{*SI.getPointerOperand()->getType(), *DL};
Tim Northoverad2b7172016-07-26 20:23:26 +0000312
313 MIRBuilder.buildStore(
Tim Northover50db7f412016-12-07 21:17:47 +0000314 Val, Addr,
315 *MF->getMachineMemOperand(
316 MachinePointerInfo(SI.getPointerOperand()), Flags,
317 DL->getTypeStoreSize(SI.getValueOperand()->getType()),
Tim Northover48dfa1a2017-02-13 22:14:16 +0000318 getMemOpAlignment(SI), AAMDNodes(), nullptr, SI.getSynchScope(),
319 SI.getOrdering()));
Tim Northoverad2b7172016-07-26 20:23:26 +0000320 return true;
321}
322
Tim Northoverc53606e2016-12-07 21:29:15 +0000323bool IRTranslator::translateExtractValue(const User &U,
324 MachineIRBuilder &MIRBuilder) {
Tim Northoverb6046222016-08-19 20:09:03 +0000325 const Value *Src = U.getOperand(0);
326 Type *Int32Ty = Type::getInt32Ty(U.getContext());
Tim Northover6f80b082016-08-19 17:47:05 +0000327 SmallVector<Value *, 1> Indices;
328
329 // getIndexedOffsetInType is designed for GEPs, so the first index is the
330 // usual array element rather than looking into the actual aggregate.
331 Indices.push_back(ConstantInt::get(Int32Ty, 0));
Tim Northoverb6046222016-08-19 20:09:03 +0000332
333 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&U)) {
334 for (auto Idx : EVI->indices())
335 Indices.push_back(ConstantInt::get(Int32Ty, Idx));
336 } else {
337 for (unsigned i = 1; i < U.getNumOperands(); ++i)
338 Indices.push_back(U.getOperand(i));
339 }
Tim Northover6f80b082016-08-19 17:47:05 +0000340
341 uint64_t Offset = 8 * DL->getIndexedOffsetInType(Src->getType(), Indices);
342
Tim Northoverb6046222016-08-19 20:09:03 +0000343 unsigned Res = getOrCreateVReg(U);
Tim Northover0f140c72016-09-09 11:46:34 +0000344 MIRBuilder.buildExtract(Res, Offset, getOrCreateVReg(*Src));
Tim Northover6f80b082016-08-19 17:47:05 +0000345
346 return true;
347}
348
Tim Northoverc53606e2016-12-07 21:29:15 +0000349bool IRTranslator::translateInsertValue(const User &U,
350 MachineIRBuilder &MIRBuilder) {
Tim Northoverb6046222016-08-19 20:09:03 +0000351 const Value *Src = U.getOperand(0);
352 Type *Int32Ty = Type::getInt32Ty(U.getContext());
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000353 SmallVector<Value *, 1> Indices;
354
355 // getIndexedOffsetInType is designed for GEPs, so the first index is the
356 // usual array element rather than looking into the actual aggregate.
357 Indices.push_back(ConstantInt::get(Int32Ty, 0));
Tim Northoverb6046222016-08-19 20:09:03 +0000358
359 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&U)) {
360 for (auto Idx : IVI->indices())
361 Indices.push_back(ConstantInt::get(Int32Ty, Idx));
362 } else {
363 for (unsigned i = 2; i < U.getNumOperands(); ++i)
364 Indices.push_back(U.getOperand(i));
365 }
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000366
367 uint64_t Offset = 8 * DL->getIndexedOffsetInType(Src->getType(), Indices);
368
Tim Northoverb6046222016-08-19 20:09:03 +0000369 unsigned Res = getOrCreateVReg(U);
370 const Value &Inserted = *U.getOperand(1);
Tim Northover0f140c72016-09-09 11:46:34 +0000371 MIRBuilder.buildInsert(Res, getOrCreateVReg(*Src), getOrCreateVReg(Inserted),
372 Offset);
Tim Northoverbbbfb1c2016-08-19 20:08:55 +0000373
374 return true;
375}
376
Tim Northoverc53606e2016-12-07 21:29:15 +0000377bool IRTranslator::translateSelect(const User &U,
378 MachineIRBuilder &MIRBuilder) {
Tim Northover0f140c72016-09-09 11:46:34 +0000379 MIRBuilder.buildSelect(getOrCreateVReg(U), getOrCreateVReg(*U.getOperand(0)),
380 getOrCreateVReg(*U.getOperand(1)),
381 getOrCreateVReg(*U.getOperand(2)));
Tim Northover5a28c362016-08-19 20:09:07 +0000382 return true;
383}
384
Tim Northoverc53606e2016-12-07 21:29:15 +0000385bool IRTranslator::translateBitCast(const User &U,
386 MachineIRBuilder &MIRBuilder) {
Tim Northover5ae83502016-09-15 09:20:34 +0000387 if (LLT{*U.getOperand(0)->getType(), *DL} == LLT{*U.getType(), *DL}) {
Tim Northover357f1be2016-08-10 23:02:41 +0000388 unsigned &Reg = ValToVReg[&U];
Tim Northover7552ef52016-08-10 16:51:14 +0000389 if (Reg)
Tim Northover357f1be2016-08-10 23:02:41 +0000390 MIRBuilder.buildCopy(Reg, getOrCreateVReg(*U.getOperand(0)));
Tim Northover7552ef52016-08-10 16:51:14 +0000391 else
Tim Northover357f1be2016-08-10 23:02:41 +0000392 Reg = getOrCreateVReg(*U.getOperand(0));
Tim Northover7c9eba92016-07-25 21:01:29 +0000393 return true;
394 }
Tim Northoverc53606e2016-12-07 21:29:15 +0000395 return translateCast(TargetOpcode::G_BITCAST, U, MIRBuilder);
Tim Northover7c9eba92016-07-25 21:01:29 +0000396}
397
Tim Northoverc53606e2016-12-07 21:29:15 +0000398bool IRTranslator::translateCast(unsigned Opcode, const User &U,
399 MachineIRBuilder &MIRBuilder) {
Tim Northover357f1be2016-08-10 23:02:41 +0000400 unsigned Op = getOrCreateVReg(*U.getOperand(0));
401 unsigned Res = getOrCreateVReg(U);
Tim Northover0f140c72016-09-09 11:46:34 +0000402 MIRBuilder.buildInstr(Opcode).addDef(Res).addUse(Op);
Tim Northover7c9eba92016-07-25 21:01:29 +0000403 return true;
404}
405
Tim Northoverc53606e2016-12-07 21:29:15 +0000406bool IRTranslator::translateGetElementPtr(const User &U,
407 MachineIRBuilder &MIRBuilder) {
Tim Northovera7653b32016-09-12 11:20:22 +0000408 // FIXME: support vector GEPs.
409 if (U.getType()->isVectorTy())
410 return false;
411
412 Value &Op0 = *U.getOperand(0);
413 unsigned BaseReg = getOrCreateVReg(Op0);
Tim Northover5ae83502016-09-15 09:20:34 +0000414 LLT PtrTy{*Op0.getType(), *DL};
Tim Northovera7653b32016-09-12 11:20:22 +0000415 unsigned PtrSize = DL->getPointerSizeInBits(PtrTy.getAddressSpace());
416 LLT OffsetTy = LLT::scalar(PtrSize);
417
418 int64_t Offset = 0;
419 for (gep_type_iterator GTI = gep_type_begin(&U), E = gep_type_end(&U);
420 GTI != E; ++GTI) {
421 const Value *Idx = GTI.getOperand();
Peter Collingbourne25a40752016-12-02 02:55:30 +0000422 if (StructType *StTy = GTI.getStructTypeOrNull()) {
Tim Northovera7653b32016-09-12 11:20:22 +0000423 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
424 Offset += DL->getStructLayout(StTy)->getElementOffset(Field);
425 continue;
426 } else {
427 uint64_t ElementSize = DL->getTypeAllocSize(GTI.getIndexedType());
428
429 // If this is a scalar constant or a splat vector of constants,
430 // handle it quickly.
431 if (const auto *CI = dyn_cast<ConstantInt>(Idx)) {
432 Offset += ElementSize * CI->getSExtValue();
433 continue;
434 }
435
436 if (Offset != 0) {
437 unsigned NewBaseReg = MRI->createGenericVirtualRegister(PtrTy);
438 unsigned OffsetReg = MRI->createGenericVirtualRegister(OffsetTy);
439 MIRBuilder.buildConstant(OffsetReg, Offset);
440 MIRBuilder.buildGEP(NewBaseReg, BaseReg, OffsetReg);
441
442 BaseReg = NewBaseReg;
443 Offset = 0;
444 }
445
446 // N = N + Idx * ElementSize;
447 unsigned ElementSizeReg = MRI->createGenericVirtualRegister(OffsetTy);
448 MIRBuilder.buildConstant(ElementSizeReg, ElementSize);
449
450 unsigned IdxReg = getOrCreateVReg(*Idx);
451 if (MRI->getType(IdxReg) != OffsetTy) {
452 unsigned NewIdxReg = MRI->createGenericVirtualRegister(OffsetTy);
453 MIRBuilder.buildSExtOrTrunc(NewIdxReg, IdxReg);
454 IdxReg = NewIdxReg;
455 }
456
457 unsigned OffsetReg = MRI->createGenericVirtualRegister(OffsetTy);
458 MIRBuilder.buildMul(OffsetReg, ElementSizeReg, IdxReg);
459
460 unsigned NewBaseReg = MRI->createGenericVirtualRegister(PtrTy);
461 MIRBuilder.buildGEP(NewBaseReg, BaseReg, OffsetReg);
462 BaseReg = NewBaseReg;
463 }
464 }
465
466 if (Offset != 0) {
467 unsigned OffsetReg = MRI->createGenericVirtualRegister(OffsetTy);
468 MIRBuilder.buildConstant(OffsetReg, Offset);
469 MIRBuilder.buildGEP(getOrCreateVReg(U), BaseReg, OffsetReg);
470 return true;
471 }
472
473 MIRBuilder.buildCopy(getOrCreateVReg(U), BaseReg);
474 return true;
475}
476
Tim Northover79f43f12017-01-30 19:33:07 +0000477bool IRTranslator::translateMemfunc(const CallInst &CI,
478 MachineIRBuilder &MIRBuilder,
479 unsigned ID) {
Tim Northover3f186032016-10-18 20:03:45 +0000480 LLT SizeTy{*CI.getArgOperand(2)->getType(), *DL};
Tim Northover79f43f12017-01-30 19:33:07 +0000481 Type *DstTy = CI.getArgOperand(0)->getType();
482 if (cast<PointerType>(DstTy)->getAddressSpace() != 0 ||
Tim Northover3f186032016-10-18 20:03:45 +0000483 SizeTy.getSizeInBits() != DL->getPointerSizeInBits(0))
484 return false;
485
486 SmallVector<CallLowering::ArgInfo, 8> Args;
487 for (int i = 0; i < 3; ++i) {
488 const auto &Arg = CI.getArgOperand(i);
489 Args.emplace_back(getOrCreateVReg(*Arg), Arg->getType());
490 }
491
Tim Northover79f43f12017-01-30 19:33:07 +0000492 const char *Callee;
493 switch (ID) {
494 case Intrinsic::memmove:
495 case Intrinsic::memcpy: {
496 Type *SrcTy = CI.getArgOperand(1)->getType();
497 if(cast<PointerType>(SrcTy)->getAddressSpace() != 0)
498 return false;
499 Callee = ID == Intrinsic::memcpy ? "memcpy" : "memmove";
500 break;
501 }
502 case Intrinsic::memset:
503 Callee = "memset";
504 break;
505 default:
506 return false;
507 }
Tim Northover3f186032016-10-18 20:03:45 +0000508
Tim Northover79f43f12017-01-30 19:33:07 +0000509 return CLI->lowerCall(MIRBuilder, MachineOperand::CreateES(Callee),
Tim Northover3f186032016-10-18 20:03:45 +0000510 CallLowering::ArgInfo(0, CI.getType()), Args);
511}
Tim Northovera7653b32016-09-12 11:20:22 +0000512
Tim Northoverc53606e2016-12-07 21:29:15 +0000513void IRTranslator::getStackGuard(unsigned DstReg,
514 MachineIRBuilder &MIRBuilder) {
Tim Northoverd8b85582017-01-27 21:31:24 +0000515 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
516 MRI->setRegClass(DstReg, TRI->getPointerRegClass(*MF));
Tim Northovercdf23f12016-10-31 18:30:59 +0000517 auto MIB = MIRBuilder.buildInstr(TargetOpcode::LOAD_STACK_GUARD);
518 MIB.addDef(DstReg);
519
Tim Northover50db7f412016-12-07 21:17:47 +0000520 auto &TLI = *MF->getSubtarget().getTargetLowering();
521 Value *Global = TLI.getSDagStackGuard(*MF->getFunction()->getParent());
Tim Northovercdf23f12016-10-31 18:30:59 +0000522 if (!Global)
523 return;
524
525 MachinePointerInfo MPInfo(Global);
Tim Northover50db7f412016-12-07 21:17:47 +0000526 MachineInstr::mmo_iterator MemRefs = MF->allocateMemRefsArray(1);
Tim Northovercdf23f12016-10-31 18:30:59 +0000527 auto Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant |
528 MachineMemOperand::MODereferenceable;
529 *MemRefs =
Tim Northover50db7f412016-12-07 21:17:47 +0000530 MF->getMachineMemOperand(MPInfo, Flags, DL->getPointerSizeInBits() / 8,
531 DL->getPointerABIAlignment());
Tim Northovercdf23f12016-10-31 18:30:59 +0000532 MIB.setMemRefs(MemRefs, MemRefs + 1);
533}
534
Tim Northover1e656ec2016-12-08 22:44:00 +0000535bool IRTranslator::translateOverflowIntrinsic(const CallInst &CI, unsigned Op,
536 MachineIRBuilder &MIRBuilder) {
537 LLT Ty{*CI.getOperand(0)->getType(), *DL};
538 LLT s1 = LLT::scalar(1);
539 unsigned Width = Ty.getSizeInBits();
540 unsigned Res = MRI->createGenericVirtualRegister(Ty);
541 unsigned Overflow = MRI->createGenericVirtualRegister(s1);
542 auto MIB = MIRBuilder.buildInstr(Op)
543 .addDef(Res)
544 .addDef(Overflow)
545 .addUse(getOrCreateVReg(*CI.getOperand(0)))
546 .addUse(getOrCreateVReg(*CI.getOperand(1)));
547
548 if (Op == TargetOpcode::G_UADDE || Op == TargetOpcode::G_USUBE) {
549 unsigned Zero = MRI->createGenericVirtualRegister(s1);
550 EntryBuilder.buildConstant(Zero, 0);
551 MIB.addUse(Zero);
552 }
553
554 MIRBuilder.buildSequence(getOrCreateVReg(CI), Res, 0, Overflow, Width);
555 return true;
556}
557
Tim Northoverc53606e2016-12-07 21:29:15 +0000558bool IRTranslator::translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
559 MachineIRBuilder &MIRBuilder) {
Tim Northover91c81732016-08-19 17:17:06 +0000560 switch (ID) {
Tim Northover1e656ec2016-12-08 22:44:00 +0000561 default:
562 break;
Tim Northover0e011702017-02-10 19:10:38 +0000563 case Intrinsic::lifetime_start:
564 case Intrinsic::lifetime_end:
565 // Stack coloring is not enabled in O0 (which we care about now) so we can
566 // drop these. Make sure someone notices when we start compiling at higher
567 // opts though.
568 if (MF->getTarget().getOptLevel() != CodeGenOpt::None)
569 return false;
570 return true;
Tim Northover09aac4a2017-01-26 23:39:14 +0000571 case Intrinsic::dbg_declare: {
572 const DbgDeclareInst &DI = cast<DbgDeclareInst>(CI);
573 assert(DI.getVariable() && "Missing variable");
574
575 const Value *Address = DI.getAddress();
576 if (!Address || isa<UndefValue>(Address)) {
577 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
578 return true;
579 }
580
581 unsigned Reg = getOrCreateVReg(*Address);
582 auto RegDef = MRI->def_instr_begin(Reg);
583 assert(DI.getVariable()->isValidLocationForIntrinsic(
584 MIRBuilder.getDebugLoc()) &&
585 "Expected inlined-at fields to agree");
586
587 if (RegDef != MRI->def_instr_end() &&
588 RegDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) {
589 MIRBuilder.buildFIDbgValue(RegDef->getOperand(1).getIndex(),
590 DI.getVariable(), DI.getExpression());
591 } else
592 MIRBuilder.buildDirectDbgValue(Reg, DI.getVariable(), DI.getExpression());
Tim Northoverb58346f2016-12-08 22:44:13 +0000593 return true;
Tim Northover09aac4a2017-01-26 23:39:14 +0000594 }
Tim Northoverd0d025a2017-02-07 20:08:59 +0000595 case Intrinsic::vaend:
596 // No target I know of cares about va_end. Certainly no in-tree target
597 // does. Simplest intrinsic ever!
598 return true;
Tim Northoverf19d4672017-02-08 17:57:20 +0000599 case Intrinsic::vastart: {
600 auto &TLI = *MF->getSubtarget().getTargetLowering();
601 Value *Ptr = CI.getArgOperand(0);
602 unsigned ListSize = TLI.getVaListSizeInBits(*DL) / 8;
603
604 MIRBuilder.buildInstr(TargetOpcode::G_VASTART)
605 .addUse(getOrCreateVReg(*Ptr))
606 .addMemOperand(MF->getMachineMemOperand(
607 MachinePointerInfo(Ptr), MachineMemOperand::MOStore, ListSize, 0));
608 return true;
609 }
Tim Northover09aac4a2017-01-26 23:39:14 +0000610 case Intrinsic::dbg_value: {
611 // This form of DBG_VALUE is target-independent.
612 const DbgValueInst &DI = cast<DbgValueInst>(CI);
613 const Value *V = DI.getValue();
614 assert(DI.getVariable()->isValidLocationForIntrinsic(
615 MIRBuilder.getDebugLoc()) &&
616 "Expected inlined-at fields to agree");
617 if (!V) {
618 // Currently the optimizer can produce this; insert an undef to
619 // help debugging. Probably the optimizer should not do this.
620 MIRBuilder.buildIndirectDbgValue(0, DI.getOffset(), DI.getVariable(),
621 DI.getExpression());
622 } else if (const auto *CI = dyn_cast<Constant>(V)) {
623 MIRBuilder.buildConstDbgValue(*CI, DI.getOffset(), DI.getVariable(),
624 DI.getExpression());
625 } else {
626 unsigned Reg = getOrCreateVReg(*V);
627 // FIXME: This does not handle register-indirect values at offset 0. The
628 // direct/indirect thing shouldn't really be handled by something as
629 // implicit as reg+noreg vs reg+imm in the first palce, but it seems
630 // pretty baked in right now.
631 if (DI.getOffset() != 0)
632 MIRBuilder.buildIndirectDbgValue(Reg, DI.getOffset(), DI.getVariable(),
633 DI.getExpression());
634 else
635 MIRBuilder.buildDirectDbgValue(Reg, DI.getVariable(),
636 DI.getExpression());
637 }
638 return true;
639 }
Tim Northover1e656ec2016-12-08 22:44:00 +0000640 case Intrinsic::uadd_with_overflow:
641 return translateOverflowIntrinsic(CI, TargetOpcode::G_UADDE, MIRBuilder);
642 case Intrinsic::sadd_with_overflow:
643 return translateOverflowIntrinsic(CI, TargetOpcode::G_SADDO, MIRBuilder);
644 case Intrinsic::usub_with_overflow:
645 return translateOverflowIntrinsic(CI, TargetOpcode::G_USUBE, MIRBuilder);
646 case Intrinsic::ssub_with_overflow:
647 return translateOverflowIntrinsic(CI, TargetOpcode::G_SSUBO, MIRBuilder);
648 case Intrinsic::umul_with_overflow:
649 return translateOverflowIntrinsic(CI, TargetOpcode::G_UMULO, MIRBuilder);
650 case Intrinsic::smul_with_overflow:
651 return translateOverflowIntrinsic(CI, TargetOpcode::G_SMULO, MIRBuilder);
Tim Northoverb38b4e22017-02-08 23:23:32 +0000652 case Intrinsic::pow:
653 MIRBuilder.buildInstr(TargetOpcode::G_FPOW)
654 .addDef(getOrCreateVReg(CI))
655 .addUse(getOrCreateVReg(*CI.getArgOperand(0)))
656 .addUse(getOrCreateVReg(*CI.getArgOperand(1)));
657 return true;
Tim Northover3f186032016-10-18 20:03:45 +0000658 case Intrinsic::memcpy:
Tim Northover79f43f12017-01-30 19:33:07 +0000659 case Intrinsic::memmove:
660 case Intrinsic::memset:
661 return translateMemfunc(CI, MIRBuilder, ID);
Tim Northovera9105be2016-11-09 22:39:54 +0000662 case Intrinsic::eh_typeid_for: {
663 GlobalValue *GV = ExtractTypeInfo(CI.getArgOperand(0));
664 unsigned Reg = getOrCreateVReg(CI);
Tim Northover50db7f412016-12-07 21:17:47 +0000665 unsigned TypeID = MF->getTypeIDFor(GV);
Tim Northovera9105be2016-11-09 22:39:54 +0000666 MIRBuilder.buildConstant(Reg, TypeID);
667 return true;
668 }
Tim Northover6e904302016-10-18 20:03:51 +0000669 case Intrinsic::objectsize: {
670 // If we don't know by now, we're never going to know.
671 const ConstantInt *Min = cast<ConstantInt>(CI.getArgOperand(1));
672
673 MIRBuilder.buildConstant(getOrCreateVReg(CI), Min->isZero() ? -1ULL : 0);
674 return true;
675 }
Tim Northovercdf23f12016-10-31 18:30:59 +0000676 case Intrinsic::stackguard:
Tim Northoverc53606e2016-12-07 21:29:15 +0000677 getStackGuard(getOrCreateVReg(CI), MIRBuilder);
Tim Northovercdf23f12016-10-31 18:30:59 +0000678 return true;
679 case Intrinsic::stackprotector: {
Tim Northovercdf23f12016-10-31 18:30:59 +0000680 LLT PtrTy{*CI.getArgOperand(0)->getType(), *DL};
681 unsigned GuardVal = MRI->createGenericVirtualRegister(PtrTy);
Tim Northoverc53606e2016-12-07 21:29:15 +0000682 getStackGuard(GuardVal, MIRBuilder);
Tim Northovercdf23f12016-10-31 18:30:59 +0000683
684 AllocaInst *Slot = cast<AllocaInst>(CI.getArgOperand(1));
685 MIRBuilder.buildStore(
686 GuardVal, getOrCreateVReg(*Slot),
Tim Northover50db7f412016-12-07 21:17:47 +0000687 *MF->getMachineMemOperand(
688 MachinePointerInfo::getFixedStack(*MF,
689 getOrCreateFrameIndex(*Slot)),
Tim Northovercdf23f12016-10-31 18:30:59 +0000690 MachineMemOperand::MOStore | MachineMemOperand::MOVolatile,
691 PtrTy.getSizeInBits() / 8, 8));
692 return true;
693 }
Tim Northover91c81732016-08-19 17:17:06 +0000694 }
Tim Northover1e656ec2016-12-08 22:44:00 +0000695 return false;
Tim Northover91c81732016-08-19 17:17:06 +0000696}
697
Tim Northoverc53606e2016-12-07 21:29:15 +0000698bool IRTranslator::translateCall(const User &U, MachineIRBuilder &MIRBuilder) {
Tim Northover357f1be2016-08-10 23:02:41 +0000699 const CallInst &CI = cast<CallInst>(U);
Tim Northover50db7f412016-12-07 21:17:47 +0000700 auto TII = MF->getTarget().getIntrinsicInfo();
Tim Northover406024a2016-08-10 21:44:01 +0000701 const Function *F = CI.getCalledFunction();
Tim Northover5fb414d2016-07-29 22:32:36 +0000702
Tim Northover3babfef2017-01-19 23:59:35 +0000703 if (CI.isInlineAsm())
704 return false;
705
Tim Northover406024a2016-08-10 21:44:01 +0000706 if (!F || !F->isIntrinsic()) {
Tim Northover406024a2016-08-10 21:44:01 +0000707 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
708 SmallVector<unsigned, 8> Args;
709 for (auto &Arg: CI.arg_operands())
710 Args.push_back(getOrCreateVReg(*Arg));
711
Tim Northoverfe5f89b2016-08-29 19:07:08 +0000712 return CLI->lowerCall(MIRBuilder, CI, Res, Args, [&]() {
713 return getOrCreateVReg(*CI.getCalledValue());
714 });
Tim Northover406024a2016-08-10 21:44:01 +0000715 }
716
717 Intrinsic::ID ID = F->getIntrinsicID();
718 if (TII && ID == Intrinsic::not_intrinsic)
719 ID = static_cast<Intrinsic::ID>(TII->getIntrinsicID(F));
720
721 assert(ID != Intrinsic::not_intrinsic && "unknown intrinsic");
Tim Northover5fb414d2016-07-29 22:32:36 +0000722
Tim Northoverc53606e2016-12-07 21:29:15 +0000723 if (translateKnownIntrinsic(CI, ID, MIRBuilder))
Tim Northover91c81732016-08-19 17:17:06 +0000724 return true;
725
Tim Northover5fb414d2016-07-29 22:32:36 +0000726 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
727 MachineInstrBuilder MIB =
Tim Northover0f140c72016-09-09 11:46:34 +0000728 MIRBuilder.buildIntrinsic(ID, Res, !CI.doesNotAccessMemory());
Tim Northover5fb414d2016-07-29 22:32:36 +0000729
730 for (auto &Arg : CI.arg_operands()) {
731 if (ConstantInt *CI = dyn_cast<ConstantInt>(Arg))
732 MIB.addImm(CI->getSExtValue());
733 else
734 MIB.addUse(getOrCreateVReg(*Arg));
735 }
736 return true;
737}
738
Tim Northoverc53606e2016-12-07 21:29:15 +0000739bool IRTranslator::translateInvoke(const User &U,
740 MachineIRBuilder &MIRBuilder) {
Tim Northovera9105be2016-11-09 22:39:54 +0000741 const InvokeInst &I = cast<InvokeInst>(U);
Tim Northover50db7f412016-12-07 21:17:47 +0000742 MCContext &Context = MF->getContext();
Tim Northovera9105be2016-11-09 22:39:54 +0000743
744 const BasicBlock *ReturnBB = I.getSuccessor(0);
745 const BasicBlock *EHPadBB = I.getSuccessor(1);
746
747 const Value *Callee(I.getCalledValue());
748 const Function *Fn = dyn_cast<Function>(Callee);
749 if (isa<InlineAsm>(Callee))
750 return false;
751
752 // FIXME: support invoking patchpoint and statepoint intrinsics.
753 if (Fn && Fn->isIntrinsic())
754 return false;
755
756 // FIXME: support whatever these are.
757 if (I.countOperandBundlesOfType(LLVMContext::OB_deopt))
758 return false;
759
760 // FIXME: support Windows exception handling.
761 if (!isa<LandingPadInst>(EHPadBB->front()))
762 return false;
763
764
Matthias Braund0ee66c2016-12-01 19:32:15 +0000765 // Emit the actual call, bracketed by EH_LABELs so that the MF knows about
Tim Northovera9105be2016-11-09 22:39:54 +0000766 // the region covered by the try.
Matthias Braund0ee66c2016-12-01 19:32:15 +0000767 MCSymbol *BeginSymbol = Context.createTempSymbol();
Tim Northovera9105be2016-11-09 22:39:54 +0000768 MIRBuilder.buildInstr(TargetOpcode::EH_LABEL).addSym(BeginSymbol);
769
770 unsigned Res = I.getType()->isVoidTy() ? 0 : getOrCreateVReg(I);
Tim Northover293f7432017-01-31 18:36:11 +0000771 SmallVector<unsigned, 8> Args;
Tim Northovera9105be2016-11-09 22:39:54 +0000772 for (auto &Arg: I.arg_operands())
Tim Northover293f7432017-01-31 18:36:11 +0000773 Args.push_back(getOrCreateVReg(*Arg));
Tim Northovera9105be2016-11-09 22:39:54 +0000774
Tim Northover293f7432017-01-31 18:36:11 +0000775 CLI->lowerCall(MIRBuilder, I, Res, Args,
776 [&]() { return getOrCreateVReg(*I.getCalledValue()); });
Tim Northovera9105be2016-11-09 22:39:54 +0000777
Matthias Braund0ee66c2016-12-01 19:32:15 +0000778 MCSymbol *EndSymbol = Context.createTempSymbol();
Tim Northovera9105be2016-11-09 22:39:54 +0000779 MIRBuilder.buildInstr(TargetOpcode::EH_LABEL).addSym(EndSymbol);
780
781 // FIXME: track probabilities.
782 MachineBasicBlock &EHPadMBB = getOrCreateBB(*EHPadBB),
783 &ReturnMBB = getOrCreateBB(*ReturnBB);
Tim Northover50db7f412016-12-07 21:17:47 +0000784 MF->addInvoke(&EHPadMBB, BeginSymbol, EndSymbol);
Tim Northovera9105be2016-11-09 22:39:54 +0000785 MIRBuilder.getMBB().addSuccessor(&ReturnMBB);
786 MIRBuilder.getMBB().addSuccessor(&EHPadMBB);
Tim Northoverc6bfa482017-01-31 20:12:18 +0000787 MIRBuilder.buildBr(ReturnMBB);
Tim Northovera9105be2016-11-09 22:39:54 +0000788
789 return true;
790}
791
Tim Northoverc53606e2016-12-07 21:29:15 +0000792bool IRTranslator::translateLandingPad(const User &U,
793 MachineIRBuilder &MIRBuilder) {
Tim Northovera9105be2016-11-09 22:39:54 +0000794 const LandingPadInst &LP = cast<LandingPadInst>(U);
795
796 MachineBasicBlock &MBB = MIRBuilder.getMBB();
Matthias Braund0ee66c2016-12-01 19:32:15 +0000797 addLandingPadInfo(LP, MBB);
Tim Northovera9105be2016-11-09 22:39:54 +0000798
799 MBB.setIsEHPad();
800
801 // If there aren't registers to copy the values into (e.g., during SjLj
802 // exceptions), then don't bother.
Tim Northover50db7f412016-12-07 21:17:47 +0000803 auto &TLI = *MF->getSubtarget().getTargetLowering();
804 const Constant *PersonalityFn = MF->getFunction()->getPersonalityFn();
Tim Northovera9105be2016-11-09 22:39:54 +0000805 if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 &&
806 TLI.getExceptionSelectorRegister(PersonalityFn) == 0)
807 return true;
808
809 // If landingpad's return type is token type, we don't create DAG nodes
810 // for its exception pointer and selector value. The extraction of exception
811 // pointer or selector value from token type landingpads is not currently
812 // supported.
813 if (LP.getType()->isTokenTy())
814 return true;
815
816 // Add a label to mark the beginning of the landing pad. Deletion of the
817 // landing pad can thus be detected via the MachineModuleInfo.
818 MIRBuilder.buildInstr(TargetOpcode::EH_LABEL)
Tim Northover50db7f412016-12-07 21:17:47 +0000819 .addSym(MF->addLandingPad(&MBB));
Tim Northovera9105be2016-11-09 22:39:54 +0000820
Justin Bognera0295312017-01-25 00:16:53 +0000821 SmallVector<LLT, 2> Tys;
822 for (Type *Ty : cast<StructType>(LP.getType())->elements())
823 Tys.push_back(LLT{*Ty, *DL});
824 assert(Tys.size() == 2 && "Only two-valued landingpads are supported");
825
Tim Northovera9105be2016-11-09 22:39:54 +0000826 // Mark exception register as live in.
827 SmallVector<unsigned, 2> Regs;
828 SmallVector<uint64_t, 2> Offsets;
Tim Northovera9105be2016-11-09 22:39:54 +0000829 if (unsigned Reg = TLI.getExceptionPointerRegister(PersonalityFn)) {
Tim Northoverc9bc8a52017-01-27 21:31:17 +0000830 MBB.addLiveIn(Reg);
Justin Bognera0295312017-01-25 00:16:53 +0000831 unsigned VReg = MRI->createGenericVirtualRegister(Tys[0]);
Tim Northovera9105be2016-11-09 22:39:54 +0000832 MIRBuilder.buildCopy(VReg, Reg);
833 Regs.push_back(VReg);
834 Offsets.push_back(0);
835 }
836
837 if (unsigned Reg = TLI.getExceptionSelectorRegister(PersonalityFn)) {
Tim Northoverc9bc8a52017-01-27 21:31:17 +0000838 MBB.addLiveIn(Reg);
Tim Northoverc9449702017-01-30 20:52:42 +0000839
840 // N.b. the exception selector register always has pointer type and may not
841 // match the actual IR-level type in the landingpad so an extra cast is
842 // needed.
843 unsigned PtrVReg = MRI->createGenericVirtualRegister(Tys[0]);
844 MIRBuilder.buildCopy(PtrVReg, Reg);
845
Justin Bognera0295312017-01-25 00:16:53 +0000846 unsigned VReg = MRI->createGenericVirtualRegister(Tys[1]);
Tim Northoverc9449702017-01-30 20:52:42 +0000847 MIRBuilder.buildInstr(TargetOpcode::G_PTRTOINT)
848 .addDef(VReg)
849 .addUse(PtrVReg);
Tim Northovera9105be2016-11-09 22:39:54 +0000850 Regs.push_back(VReg);
Justin Bognera0295312017-01-25 00:16:53 +0000851 Offsets.push_back(Tys[0].getSizeInBits());
Tim Northovera9105be2016-11-09 22:39:54 +0000852 }
853
854 MIRBuilder.buildSequence(getOrCreateVReg(LP), Regs, Offsets);
855 return true;
856}
857
Tim Northoverc3e3f592017-02-03 18:22:45 +0000858bool IRTranslator::translateAlloca(const User &U,
859 MachineIRBuilder &MIRBuilder) {
860 auto &AI = cast<AllocaInst>(U);
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000861
Tim Northoverc3e3f592017-02-03 18:22:45 +0000862 if (AI.isStaticAlloca()) {
863 unsigned Res = getOrCreateVReg(AI);
864 int FI = getOrCreateFrameIndex(AI);
865 MIRBuilder.buildFrameIndex(Res, FI);
866 return true;
867 }
868
869 // Now we're in the harder dynamic case.
870 Type *Ty = AI.getAllocatedType();
871 unsigned Align =
872 std::max((unsigned)DL->getPrefTypeAlignment(Ty), AI.getAlignment());
873
874 unsigned NumElts = getOrCreateVReg(*AI.getArraySize());
875
876 LLT IntPtrTy = LLT::scalar(DL->getPointerSizeInBits());
877 if (MRI->getType(NumElts) != IntPtrTy) {
878 unsigned ExtElts = MRI->createGenericVirtualRegister(IntPtrTy);
879 MIRBuilder.buildZExtOrTrunc(ExtElts, NumElts);
880 NumElts = ExtElts;
881 }
882
883 unsigned AllocSize = MRI->createGenericVirtualRegister(IntPtrTy);
884 unsigned TySize = MRI->createGenericVirtualRegister(IntPtrTy);
Tim Northoverc2f89562017-02-14 20:56:18 +0000885 MIRBuilder.buildConstant(TySize, -DL->getTypeAllocSize(Ty));
Tim Northoverc3e3f592017-02-03 18:22:45 +0000886 MIRBuilder.buildMul(AllocSize, NumElts, TySize);
887
888 LLT PtrTy = LLT{*AI.getType(), *DL};
889 auto &TLI = *MF->getSubtarget().getTargetLowering();
890 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
891
892 unsigned SPTmp = MRI->createGenericVirtualRegister(PtrTy);
893 MIRBuilder.buildCopy(SPTmp, SPReg);
894
Tim Northoverc2f89562017-02-14 20:56:18 +0000895 unsigned AllocTmp = MRI->createGenericVirtualRegister(PtrTy);
896 MIRBuilder.buildGEP(AllocTmp, SPTmp, AllocSize);
Tim Northoverc3e3f592017-02-03 18:22:45 +0000897
898 // Handle alignment. We have to realign if the allocation granule was smaller
899 // than stack alignment, or the specific alloca requires more than stack
900 // alignment.
901 unsigned StackAlign =
902 MF->getSubtarget().getFrameLowering()->getStackAlignment();
903 Align = std::max(Align, StackAlign);
904 if (Align > StackAlign || DL->getTypeAllocSize(Ty) % StackAlign != 0) {
905 // Round the size of the allocation up to the stack alignment size
906 // by add SA-1 to the size. This doesn't overflow because we're computing
907 // an address inside an alloca.
Tim Northoverc2f89562017-02-14 20:56:18 +0000908 unsigned AlignedAlloc = MRI->createGenericVirtualRegister(PtrTy);
909 MIRBuilder.buildPtrMask(AlignedAlloc, AllocTmp, Log2_32(Align));
910 AllocTmp = AlignedAlloc;
Tim Northoverc3e3f592017-02-03 18:22:45 +0000911 }
912
Tim Northoverc2f89562017-02-14 20:56:18 +0000913 MIRBuilder.buildCopy(SPReg, AllocTmp);
914 MIRBuilder.buildCopy(getOrCreateVReg(AI), AllocTmp);
Tim Northoverc3e3f592017-02-03 18:22:45 +0000915
916 MF->getFrameInfo().CreateVariableSizedObject(Align ? Align : 1, &AI);
917 assert(MF->getFrameInfo().hasVarSizedObjects());
Tim Northoverbd505462016-07-22 16:59:52 +0000918 return true;
919}
920
Tim Northover4a652222017-02-15 23:22:33 +0000921bool IRTranslator::translateVAArg(const User &U, MachineIRBuilder &MIRBuilder) {
922 // FIXME: We may need more info about the type. Because of how LLT works,
923 // we're completely discarding the i64/double distinction here (amongst
924 // others). Fortunately the ABIs I know of where that matters don't use va_arg
925 // anyway but that's not guaranteed.
926 MIRBuilder.buildInstr(TargetOpcode::G_VAARG)
927 .addDef(getOrCreateVReg(U))
928 .addUse(getOrCreateVReg(*U.getOperand(0)))
929 .addImm(DL->getABITypeAlignment(U.getType()));
930 return true;
931}
932
Tim Northoverc53606e2016-12-07 21:29:15 +0000933bool IRTranslator::translatePHI(const User &U, MachineIRBuilder &MIRBuilder) {
Tim Northover357f1be2016-08-10 23:02:41 +0000934 const PHINode &PI = cast<PHINode>(U);
Tim Northover25d12862016-09-09 11:47:31 +0000935 auto MIB = MIRBuilder.buildInstr(TargetOpcode::PHI);
Tim Northover97d0cb32016-08-05 17:16:40 +0000936 MIB.addDef(getOrCreateVReg(PI));
937
938 PendingPHIs.emplace_back(&PI, MIB.getInstr());
939 return true;
940}
941
942void IRTranslator::finishPendingPhis() {
943 for (std::pair<const PHINode *, MachineInstr *> &Phi : PendingPHIs) {
944 const PHINode *PI = Phi.first;
Tim Northoverc53606e2016-12-07 21:29:15 +0000945 MachineInstrBuilder MIB(*MF, Phi.second);
Tim Northover97d0cb32016-08-05 17:16:40 +0000946
947 // All MachineBasicBlocks exist, add them to the PHI. We assume IRTranslator
948 // won't create extra control flow here, otherwise we need to find the
949 // dominating predecessor here (or perhaps force the weirder IRTranslators
950 // to provide a simple boundary).
Tim Northoverb6636fd2017-01-17 22:13:50 +0000951 SmallSet<const BasicBlock *, 4> HandledPreds;
952
Tim Northover97d0cb32016-08-05 17:16:40 +0000953 for (unsigned i = 0; i < PI->getNumIncomingValues(); ++i) {
Tim Northoverb6636fd2017-01-17 22:13:50 +0000954 auto IRPred = PI->getIncomingBlock(i);
955 if (HandledPreds.count(IRPred))
956 continue;
957
958 HandledPreds.insert(IRPred);
959 unsigned ValReg = getOrCreateVReg(*PI->getIncomingValue(i));
960 for (auto Pred : getMachinePredBBs({IRPred, PI->getParent()})) {
961 assert(Pred->isSuccessor(MIB->getParent()) &&
962 "incorrect CFG at MachineBasicBlock level");
963 MIB.addUse(ValReg);
964 MIB.addMBB(Pred);
965 }
Tim Northover97d0cb32016-08-05 17:16:40 +0000966 }
967 }
968}
969
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000970bool IRTranslator::translate(const Instruction &Inst) {
Tim Northoverc53606e2016-12-07 21:29:15 +0000971 CurBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000972 switch(Inst.getOpcode()) {
Tim Northover357f1be2016-08-10 23:02:41 +0000973#define HANDLE_INST(NUM, OPCODE, CLASS) \
Tim Northoverc53606e2016-12-07 21:29:15 +0000974 case Instruction::OPCODE: return translate##OPCODE(Inst, CurBuilder);
Tim Northover357f1be2016-08-10 23:02:41 +0000975#include "llvm/IR/Instruction.def"
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000976 default:
Quentin Colombet3bb32cc2016-08-26 23:49:05 +0000977 if (!TPC->isGlobalISelAbortEnabled())
978 return false;
Tim Northover357f1be2016-08-10 23:02:41 +0000979 llvm_unreachable("unknown opcode");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000980 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000981}
982
Tim Northover5ed648e2016-08-09 21:28:04 +0000983bool IRTranslator::translate(const Constant &C, unsigned Reg) {
Tim Northoverd403a3d2016-08-09 23:01:30 +0000984 if (auto CI = dyn_cast<ConstantInt>(&C))
Tim Northovercc35f902016-12-05 21:54:17 +0000985 EntryBuilder.buildConstant(Reg, *CI);
Tim Northoverb16734f2016-08-19 20:09:15 +0000986 else if (auto CF = dyn_cast<ConstantFP>(&C))
Tim Northover0f140c72016-09-09 11:46:34 +0000987 EntryBuilder.buildFConstant(Reg, *CF);
Tim Northoverd403a3d2016-08-09 23:01:30 +0000988 else if (isa<UndefValue>(C))
989 EntryBuilder.buildInstr(TargetOpcode::IMPLICIT_DEF).addDef(Reg);
Tim Northover8e0c53a2016-08-11 21:40:55 +0000990 else if (isa<ConstantPointerNull>(C))
Tim Northover9267ac52016-12-05 21:47:07 +0000991 EntryBuilder.buildConstant(Reg, 0);
Tim Northover032548f2016-09-12 12:10:41 +0000992 else if (auto GV = dyn_cast<GlobalValue>(&C))
993 EntryBuilder.buildGlobalValue(Reg, GV);
Tim Northover357f1be2016-08-10 23:02:41 +0000994 else if (auto CE = dyn_cast<ConstantExpr>(&C)) {
995 switch(CE->getOpcode()) {
996#define HANDLE_INST(NUM, OPCODE, CLASS) \
Tim Northoverc53606e2016-12-07 21:29:15 +0000997 case Instruction::OPCODE: return translate##OPCODE(*CE, EntryBuilder);
Tim Northover357f1be2016-08-10 23:02:41 +0000998#include "llvm/IR/Instruction.def"
999 default:
Quentin Colombet3bb32cc2016-08-26 23:49:05 +00001000 if (!TPC->isGlobalISelAbortEnabled())
1001 return false;
Tim Northover357f1be2016-08-10 23:02:41 +00001002 llvm_unreachable("unknown opcode");
1003 }
Quentin Colombet3bb32cc2016-08-26 23:49:05 +00001004 } else if (!TPC->isGlobalISelAbortEnabled())
1005 return false;
1006 else
Tim Northoverd403a3d2016-08-09 23:01:30 +00001007 llvm_unreachable("unhandled constant kind");
Tim Northover5ed648e2016-08-09 21:28:04 +00001008
Tim Northoverd403a3d2016-08-09 23:01:30 +00001009 return true;
Tim Northover5ed648e2016-08-09 21:28:04 +00001010}
1011
Tim Northover0d510442016-08-11 16:21:29 +00001012void IRTranslator::finalizeFunction() {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +00001013 // Release the memory used by the different maps we
1014 // needed during the translation.
Tim Northover800638f2016-12-05 23:10:19 +00001015 PendingPHIs.clear();
Quentin Colombetccd77252016-02-11 21:48:32 +00001016 ValToVReg.clear();
Tim Northovercdf23f12016-10-31 18:30:59 +00001017 FrameIndices.clear();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +00001018 Constants.clear();
Tim Northoverb6636fd2017-01-17 22:13:50 +00001019 MachinePreds.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +00001020}
1021
Tim Northover50db7f412016-12-07 21:17:47 +00001022bool IRTranslator::runOnMachineFunction(MachineFunction &CurMF) {
1023 MF = &CurMF;
1024 const Function &F = *MF->getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +00001025 if (F.empty())
1026 return false;
Tim Northover50db7f412016-12-07 21:17:47 +00001027 CLI = MF->getSubtarget().getCallLowering();
Tim Northoverc53606e2016-12-07 21:29:15 +00001028 CurBuilder.setMF(*MF);
Tim Northover50db7f412016-12-07 21:17:47 +00001029 EntryBuilder.setMF(*MF);
1030 MRI = &MF->getRegInfo();
Tim Northoverbd505462016-07-22 16:59:52 +00001031 DL = &F.getParent()->getDataLayout();
Quentin Colombet3bb32cc2016-08-26 23:49:05 +00001032 TPC = &getAnalysis<TargetPassConfig>();
Ahmed Bougachaae9dade2017-02-23 21:05:42 +00001033 ORE = make_unique<OptimizationRemarkEmitter>(&F);
Tim Northoverbd505462016-07-22 16:59:52 +00001034
Tim Northover14e7f732016-08-05 17:50:36 +00001035 assert(PendingPHIs.empty() && "stale PHIs");
1036
Ahmed Bougachaeceabdd2017-02-23 23:57:28 +00001037 // Release the per-function state when we return, whether we succeeded or not.
1038 auto FinalizeOnReturn = make_scope_exit([this]() { finalizeFunction(); });
1039
Tim Northover05cc4852016-12-07 21:05:38 +00001040 // Setup a separate basic-block for the arguments and constants, falling
1041 // through to the IR-level Function's entry block.
Tim Northover50db7f412016-12-07 21:17:47 +00001042 MachineBasicBlock *EntryBB = MF->CreateMachineBasicBlock();
1043 MF->push_back(EntryBB);
Tim Northover05cc4852016-12-07 21:05:38 +00001044 EntryBB->addSuccessor(&getOrCreateBB(F.front()));
1045 EntryBuilder.setMBB(*EntryBB);
1046
1047 // Lower the actual args into this basic block.
Quentin Colombetfd9d0a02016-02-11 19:59:41 +00001048 SmallVector<unsigned, 8> VRegArgs;
1049 for (const Argument &Arg: F.args())
Quentin Colombete225e252016-03-11 17:27:54 +00001050 VRegArgs.push_back(getOrCreateVReg(Arg));
Ahmed Bougacha8f9e99b2017-02-24 00:34:41 +00001051 if (!CLI->lowerFormalArguments(EntryBuilder, F, VRegArgs)) {
Ahmed Bougacha7c88a4e2017-02-24 00:34:44 +00001052 OptimizationRemarkMissed R("gisel-irtranslator", "GISelFailure",
1053 MF->getFunction()->getSubprogram(),
Ahmed Bougachaae9dade2017-02-23 21:05:42 +00001054 &MF->getFunction()->getEntryBlock());
1055 R << "unable to lower arguments: " << ore::NV("Prototype", F.getType());
1056 reportTranslationError(*MF, *TPC, *ORE, R);
Ahmed Bougachaae9dade2017-02-23 21:05:42 +00001057 return false;
Quentin Colombet3bb32cc2016-08-26 23:49:05 +00001058 }
Quentin Colombetfd9d0a02016-02-11 19:59:41 +00001059
Tim Northover05cc4852016-12-07 21:05:38 +00001060 // And translate the function!
Quentin Colombet2ecff3b2016-02-10 22:59:27 +00001061 for (const BasicBlock &BB: F) {
Quentin Colombet53237a92016-03-11 17:27:43 +00001062 MachineBasicBlock &MBB = getOrCreateBB(BB);
Quentin Colombet91ebd712016-03-11 17:27:47 +00001063 // Set the insertion point of all the following translations to
1064 // the end of this basic block.
Tim Northoverc53606e2016-12-07 21:29:15 +00001065 CurBuilder.setMBB(MBB);
Tim Northovera9105be2016-11-09 22:39:54 +00001066
Quentin Colombet2ecff3b2016-02-10 22:59:27 +00001067 for (const Instruction &Inst: BB) {
Ahmed Bougacha8f9e99b2017-02-24 00:34:41 +00001068 if (translate(Inst))
1069 continue;
Ahmed Bougachaae9dade2017-02-23 21:05:42 +00001070
Ahmed Bougacha8f9e99b2017-02-24 00:34:41 +00001071 std::string InstStrStorage;
1072 raw_string_ostream InstStr(InstStrStorage);
1073 InstStr << Inst;
1074
1075 OptimizationRemarkMissed R("gisel-irtranslator", "IRTranslatorFailure: ",
1076 &Inst);
1077 R << "unable to translate instruction: " << ore::NV("Opcode", &Inst)
1078 << ": '" << InstStr.str() << "'";
1079 reportTranslationError(*MF, *TPC, *ORE, R);
1080 return false;
Quentin Colombet2ecff3b2016-02-10 22:59:27 +00001081 }
1082 }
Tim Northover72eebfa2016-07-12 22:23:42 +00001083
Ahmed Bougacha4f8dd022017-02-23 23:57:36 +00001084 finishPendingPhis();
Tim Northover97d0cb32016-08-05 17:16:40 +00001085
Ahmed Bougacha4f8dd022017-02-23 23:57:36 +00001086 // Now that the MachineFrameInfo has been configured, no further changes to
1087 // the reserved registers are possible.
1088 MRI->freezeReservedRegs(*MF);
Quentin Colombet327f9422016-12-15 23:32:25 +00001089
Ahmed Bougacha4f8dd022017-02-23 23:57:36 +00001090 // Merge the argument lowering and constants block with its single
1091 // successor, the LLVM-IR entry block. We want the basic block to
1092 // be maximal.
1093 assert(EntryBB->succ_size() == 1 &&
1094 "Custom BB used for lowering should have only one successor");
1095 // Get the successor of the current entry block.
1096 MachineBasicBlock &NewEntryBB = **EntryBB->succ_begin();
1097 assert(NewEntryBB.pred_size() == 1 &&
1098 "LLVM-IR entry block has a predecessor!?");
1099 // Move all the instruction from the current entry block to the
1100 // new entry block.
1101 NewEntryBB.splice(NewEntryBB.begin(), EntryBB, EntryBB->begin(),
1102 EntryBB->end());
Quentin Colombet327f9422016-12-15 23:32:25 +00001103
Ahmed Bougacha4f8dd022017-02-23 23:57:36 +00001104 // Update the live-in information for the new entry block.
1105 for (const MachineBasicBlock::RegisterMaskPair &LiveIn : EntryBB->liveins())
1106 NewEntryBB.addLiveIn(LiveIn);
1107 NewEntryBB.sortUniqueLiveIns();
Quentin Colombet327f9422016-12-15 23:32:25 +00001108
Ahmed Bougacha4f8dd022017-02-23 23:57:36 +00001109 // Get rid of the now empty basic block.
1110 EntryBB->removeSuccessor(&NewEntryBB);
1111 MF->remove(EntryBB);
1112 MF->DeleteMachineBasicBlock(EntryBB);
Quentin Colombet327f9422016-12-15 23:32:25 +00001113
Ahmed Bougacha4f8dd022017-02-23 23:57:36 +00001114 assert(&MF->front() == &NewEntryBB &&
1115 "New entry wasn't next in the list of basic block!");
Tim Northover800638f2016-12-05 23:10:19 +00001116
Quentin Colombet105cf2b2016-01-20 20:58:56 +00001117 return false;
1118}