blob: a7b6d20c212f111a0e592f903cc8fd5a8c7dce0a [file] [log] [blame]
Quentin Colombet105cf2b2016-01-20 20:58:56 +00001//===-- llvm/CodeGen/GlobalISel/IRTranslator.cpp - IRTranslator --*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file implements the IRTranslator class.
11//===----------------------------------------------------------------------===//
12
13#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
14
Quentin Colombetfd9d0a02016-02-11 19:59:41 +000015#include "llvm/ADT/SmallVector.h"
Quentin Colombetba2a0162016-02-16 19:26:02 +000016#include "llvm/CodeGen/GlobalISel/CallLowering.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000017#include "llvm/CodeGen/MachineFunction.h"
Tim Northoverbd505462016-07-22 16:59:52 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombet17c494b2016-02-11 17:51:31 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
20#include "llvm/IR/Constant.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000021#include "llvm/IR/Function.h"
Tim Northover5fb414d2016-07-29 22:32:36 +000022#include "llvm/IR/IntrinsicInst.h"
Quentin Colombet17c494b2016-02-11 17:51:31 +000023#include "llvm/IR/Type.h"
24#include "llvm/IR/Value.h"
Tim Northover5fb414d2016-07-29 22:32:36 +000025#include "llvm/Target/TargetIntrinsicInfo.h"
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000026#include "llvm/Target/TargetLowering.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000027
28#define DEBUG_TYPE "irtranslator"
29
Quentin Colombet105cf2b2016-01-20 20:58:56 +000030using namespace llvm;
31
32char IRTranslator::ID = 0;
Quentin Colombet39293d32016-03-08 01:38:55 +000033INITIALIZE_PASS(IRTranslator, "irtranslator", "IRTranslator LLVM IR -> MI",
Tim Northover884b47e2016-07-26 03:29:18 +000034 false, false)
Quentin Colombet105cf2b2016-01-20 20:58:56 +000035
Quentin Colombeta7fae162016-02-11 17:53:23 +000036IRTranslator::IRTranslator() : MachineFunctionPass(ID), MRI(nullptr) {
Quentin Colombet39293d32016-03-08 01:38:55 +000037 initializeIRTranslatorPass(*PassRegistry::getPassRegistry());
Quentin Colombeta7fae162016-02-11 17:53:23 +000038}
39
Quentin Colombete225e252016-03-11 17:27:54 +000040unsigned IRTranslator::getOrCreateVReg(const Value &Val) {
41 unsigned &ValReg = ValToVReg[&Val];
Quentin Colombet17c494b2016-02-11 17:51:31 +000042 // Check if this is the first time we see Val.
Quentin Colombetccd77252016-02-11 21:48:32 +000043 if (!ValReg) {
Quentin Colombet17c494b2016-02-11 17:51:31 +000044 // Fill ValRegsSequence with the sequence of registers
45 // we need to concat together to produce the value.
Quentin Colombete225e252016-03-11 17:27:54 +000046 assert(Val.getType()->isSized() &&
Quentin Colombet17c494b2016-02-11 17:51:31 +000047 "Don't know how to create an empty vreg");
Tim Northoverbd505462016-07-22 16:59:52 +000048 unsigned Size = DL->getTypeSizeInBits(Val.getType());
Quentin Colombet17c494b2016-02-11 17:51:31 +000049 unsigned VReg = MRI->createGenericVirtualRegister(Size);
Quentin Colombetccd77252016-02-11 21:48:32 +000050 ValReg = VReg;
Tim Northover5ed648e2016-08-09 21:28:04 +000051
52 if (auto CV = dyn_cast<Constant>(&Val)) {
53 bool Success = translate(*CV, VReg);
54 if (!Success)
55 report_fatal_error("unable to translate constant");
56 }
Quentin Colombet17c494b2016-02-11 17:51:31 +000057 }
Quentin Colombetccd77252016-02-11 21:48:32 +000058 return ValReg;
Quentin Colombet17c494b2016-02-11 17:51:31 +000059}
60
Tim Northoverad2b7172016-07-26 20:23:26 +000061unsigned IRTranslator::getMemOpAlignment(const Instruction &I) {
62 unsigned Alignment = 0;
63 Type *ValTy = nullptr;
64 if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
65 Alignment = SI->getAlignment();
66 ValTy = SI->getValueOperand()->getType();
67 } else if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
68 Alignment = LI->getAlignment();
69 ValTy = LI->getType();
70 } else
71 llvm_unreachable("unhandled memory instruction");
72
73 return Alignment ? Alignment : DL->getABITypeAlignment(ValTy);
74}
75
Quentin Colombet53237a92016-03-11 17:27:43 +000076MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock &BB) {
77 MachineBasicBlock *&MBB = BBToMBB[&BB];
Quentin Colombet17c494b2016-02-11 17:51:31 +000078 if (!MBB) {
Quentin Colombeta7fae162016-02-11 17:53:23 +000079 MachineFunction &MF = MIRBuilder.getMF();
Quentin Colombet17c494b2016-02-11 17:51:31 +000080 MBB = MF.CreateMachineBasicBlock();
81 MF.push_back(MBB);
82 }
83 return *MBB;
84}
85
Tim Northover357f1be2016-08-10 23:02:41 +000086bool IRTranslator::translateBinaryOp(unsigned Opcode, const User &U) {
Tim Northover0d56e052016-07-29 18:11:21 +000087 // FIXME: handle signed/unsigned wrapping flags.
88
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000089 // Get or create a virtual register for each value.
90 // Unless the value is a Constant => loadimm cst?
91 // or inline constant each time?
92 // Creation of a virtual register needs to have a size.
Tim Northover357f1be2016-08-10 23:02:41 +000093 unsigned Op0 = getOrCreateVReg(*U.getOperand(0));
94 unsigned Op1 = getOrCreateVReg(*U.getOperand(1));
95 unsigned Res = getOrCreateVReg(U);
96 MIRBuilder.buildInstr(Opcode, LLT{*U.getType()})
Tim Northovera51575f2016-07-29 17:43:52 +000097 .addDef(Res)
98 .addUse(Op0)
99 .addUse(Op1);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000100 return true;
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000101}
102
Tim Northoverde3aea0412016-08-17 20:25:25 +0000103bool IRTranslator::translateICmp(const User &U) {
104 const CmpInst &CI = cast<CmpInst>(U);
105 unsigned Op0 = getOrCreateVReg(*CI.getOperand(0));
106 unsigned Op1 = getOrCreateVReg(*CI.getOperand(1));
107 unsigned Res = getOrCreateVReg(CI);
108 CmpInst::Predicate Pred = CI.getPredicate();
109
110 assert(isa<ICmpInst>(CI) && "only integer comparisons supported now");
111 assert(CmpInst::isIntPredicate(Pred) && "only int comparisons supported now");
112 MIRBuilder.buildICmp({LLT{*CI.getType()}, LLT{*CI.getOperand(0)->getType()}},
113 Pred, Res, Op0, Op1);
114 return true;
115}
116
Tim Northover357f1be2016-08-10 23:02:41 +0000117bool IRTranslator::translateRet(const User &U) {
118 const ReturnInst &RI = cast<ReturnInst>(U);
Tim Northover0d56e052016-07-29 18:11:21 +0000119 const Value *Ret = RI.getReturnValue();
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000120 // The target may mess up with the insertion point, but
121 // this is not important as a return is the last instruction
122 // of the block anyway.
Tom Stellardb72a65f2016-04-14 17:23:33 +0000123 return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000124}
125
Tim Northover357f1be2016-08-10 23:02:41 +0000126bool IRTranslator::translateBr(const User &U) {
127 const BranchInst &BrInst = cast<BranchInst>(U);
Tim Northover69c2ba52016-07-29 17:58:00 +0000128 unsigned Succ = 0;
129 if (!BrInst.isUnconditional()) {
130 // We want a G_BRCOND to the true BB followed by an unconditional branch.
131 unsigned Tst = getOrCreateVReg(*BrInst.getCondition());
132 const BasicBlock &TrueTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ++));
133 MachineBasicBlock &TrueBB = getOrCreateBB(TrueTgt);
134 MIRBuilder.buildBrCond(LLT{*BrInst.getCondition()->getType()}, Tst, TrueBB);
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000135 }
Tim Northover69c2ba52016-07-29 17:58:00 +0000136
137 const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ));
138 MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt);
139 MIRBuilder.buildBr(TgtBB);
140
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000141 // Link successors.
142 MachineBasicBlock &CurBB = MIRBuilder.getMBB();
143 for (const BasicBlock *Succ : BrInst.successors())
144 CurBB.addSuccessor(&getOrCreateBB(*Succ));
145 return true;
146}
147
Tim Northover357f1be2016-08-10 23:02:41 +0000148bool IRTranslator::translateLoad(const User &U) {
149 const LoadInst &LI = cast<LoadInst>(U);
Tim Northoverad2b7172016-07-26 20:23:26 +0000150 assert(LI.isSimple() && "only simple loads are supported at the moment");
151
152 MachineFunction &MF = MIRBuilder.getMF();
153 unsigned Res = getOrCreateVReg(LI);
154 unsigned Addr = getOrCreateVReg(*LI.getPointerOperand());
Tim Northover28fdc422016-08-15 21:13:17 +0000155 LLT VTy{*LI.getType(), DL}, PTy{*LI.getPointerOperand()->getType()};
Tim Northoverad2b7172016-07-26 20:23:26 +0000156
157 MIRBuilder.buildLoad(
158 VTy, PTy, Res, Addr,
Tim Northover28fdc422016-08-15 21:13:17 +0000159 *MF.getMachineMemOperand(
160 MachinePointerInfo(LI.getPointerOperand()), MachineMemOperand::MOLoad,
161 DL->getTypeStoreSize(LI.getType()), getMemOpAlignment(LI)));
Tim Northoverad2b7172016-07-26 20:23:26 +0000162 return true;
163}
164
Tim Northover357f1be2016-08-10 23:02:41 +0000165bool IRTranslator::translateStore(const User &U) {
166 const StoreInst &SI = cast<StoreInst>(U);
Tim Northoverad2b7172016-07-26 20:23:26 +0000167 assert(SI.isSimple() && "only simple loads are supported at the moment");
168
169 MachineFunction &MF = MIRBuilder.getMF();
170 unsigned Val = getOrCreateVReg(*SI.getValueOperand());
171 unsigned Addr = getOrCreateVReg(*SI.getPointerOperand());
Tim Northover28fdc422016-08-15 21:13:17 +0000172 LLT VTy{*SI.getValueOperand()->getType(), DL},
Tim Northoverad2b7172016-07-26 20:23:26 +0000173 PTy{*SI.getPointerOperand()->getType()};
174
175 MIRBuilder.buildStore(
176 VTy, PTy, Val, Addr,
Tim Northover28fdc422016-08-15 21:13:17 +0000177 *MF.getMachineMemOperand(
178 MachinePointerInfo(SI.getPointerOperand()),
179 MachineMemOperand::MOStore,
180 DL->getTypeStoreSize(SI.getValueOperand()->getType()),
181 getMemOpAlignment(SI)));
Tim Northoverad2b7172016-07-26 20:23:26 +0000182 return true;
183}
184
Tim Northover357f1be2016-08-10 23:02:41 +0000185bool IRTranslator::translateBitCast(const User &U) {
186 if (LLT{*U.getOperand(0)->getType()} == LLT{*U.getType()}) {
187 unsigned &Reg = ValToVReg[&U];
Tim Northover7552ef52016-08-10 16:51:14 +0000188 if (Reg)
Tim Northover357f1be2016-08-10 23:02:41 +0000189 MIRBuilder.buildCopy(Reg, getOrCreateVReg(*U.getOperand(0)));
Tim Northover7552ef52016-08-10 16:51:14 +0000190 else
Tim Northover357f1be2016-08-10 23:02:41 +0000191 Reg = getOrCreateVReg(*U.getOperand(0));
Tim Northover7c9eba92016-07-25 21:01:29 +0000192 return true;
193 }
Tim Northover357f1be2016-08-10 23:02:41 +0000194 return translateCast(TargetOpcode::G_BITCAST, U);
Tim Northover7c9eba92016-07-25 21:01:29 +0000195}
196
Tim Northover357f1be2016-08-10 23:02:41 +0000197bool IRTranslator::translateCast(unsigned Opcode, const User &U) {
198 unsigned Op = getOrCreateVReg(*U.getOperand(0));
199 unsigned Res = getOrCreateVReg(U);
200 MIRBuilder
201 .buildInstr(Opcode, {LLT{*U.getType()}, LLT{*U.getOperand(0)->getType()}})
Tim Northovera51575f2016-07-29 17:43:52 +0000202 .addDef(Res)
203 .addUse(Op);
Tim Northover7c9eba92016-07-25 21:01:29 +0000204 return true;
205}
206
Tim Northover357f1be2016-08-10 23:02:41 +0000207bool IRTranslator::translateCall(const User &U) {
208 const CallInst &CI = cast<CallInst>(U);
Tim Northover5fb414d2016-07-29 22:32:36 +0000209 auto TII = MIRBuilder.getMF().getTarget().getIntrinsicInfo();
Tim Northover406024a2016-08-10 21:44:01 +0000210 const Function *F = CI.getCalledFunction();
Tim Northover5fb414d2016-07-29 22:32:36 +0000211
Tim Northover406024a2016-08-10 21:44:01 +0000212 if (!F || !F->isIntrinsic()) {
213 // FIXME: handle multiple return values.
214 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
215 SmallVector<unsigned, 8> Args;
216 for (auto &Arg: CI.arg_operands())
217 Args.push_back(getOrCreateVReg(*Arg));
218
219 return CLI->lowerCall(MIRBuilder, CI,
220 F ? 0 : getOrCreateVReg(*CI.getCalledValue()), Res,
221 Args);
222 }
223
224 Intrinsic::ID ID = F->getIntrinsicID();
225 if (TII && ID == Intrinsic::not_intrinsic)
226 ID = static_cast<Intrinsic::ID>(TII->getIntrinsicID(F));
227
228 assert(ID != Intrinsic::not_intrinsic && "unknown intrinsic");
Tim Northover5fb414d2016-07-29 22:32:36 +0000229
230 // Need types (starting with return) & args.
231 SmallVector<LLT, 4> Tys;
232 Tys.emplace_back(*CI.getType());
233 for (auto &Arg : CI.arg_operands())
234 Tys.emplace_back(*Arg->getType());
235
236 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
237 MachineInstrBuilder MIB =
238 MIRBuilder.buildIntrinsic(Tys, ID, Res, !CI.doesNotAccessMemory());
239
240 for (auto &Arg : CI.arg_operands()) {
241 if (ConstantInt *CI = dyn_cast<ConstantInt>(Arg))
242 MIB.addImm(CI->getSExtValue());
243 else
244 MIB.addUse(getOrCreateVReg(*Arg));
245 }
246 return true;
247}
248
Tim Northoverbd505462016-07-22 16:59:52 +0000249bool IRTranslator::translateStaticAlloca(const AllocaInst &AI) {
250 assert(AI.isStaticAlloca() && "only handle static allocas now");
251 MachineFunction &MF = MIRBuilder.getMF();
252 unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
253 unsigned Size =
254 ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
255
Tim Northover8d2f52e2016-07-27 17:47:54 +0000256 // Always allocate at least one byte.
257 Size = std::max(Size, 1u);
258
Tim Northoverbd505462016-07-22 16:59:52 +0000259 unsigned Alignment = AI.getAlignment();
260 if (!Alignment)
261 Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
262
263 unsigned Res = getOrCreateVReg(AI);
Matthias Braun93320392016-07-28 20:13:42 +0000264 int FI = MF.getFrameInfo().CreateStackObject(Size, Alignment, false, &AI);
Tim Northoverbd505462016-07-22 16:59:52 +0000265 MIRBuilder.buildFrameIndex(LLT::pointer(0), Res, FI);
266 return true;
267}
268
Tim Northover357f1be2016-08-10 23:02:41 +0000269bool IRTranslator::translatePHI(const User &U) {
270 const PHINode &PI = cast<PHINode>(U);
Tim Northover97d0cb32016-08-05 17:16:40 +0000271 MachineInstrBuilder MIB = MIRBuilder.buildInstr(TargetOpcode::PHI);
272 MIB.addDef(getOrCreateVReg(PI));
273
274 PendingPHIs.emplace_back(&PI, MIB.getInstr());
275 return true;
276}
277
278void IRTranslator::finishPendingPhis() {
279 for (std::pair<const PHINode *, MachineInstr *> &Phi : PendingPHIs) {
280 const PHINode *PI = Phi.first;
281 MachineInstrBuilder MIB(MIRBuilder.getMF(), Phi.second);
282
283 // All MachineBasicBlocks exist, add them to the PHI. We assume IRTranslator
284 // won't create extra control flow here, otherwise we need to find the
285 // dominating predecessor here (or perhaps force the weirder IRTranslators
286 // to provide a simple boundary).
287 for (unsigned i = 0; i < PI->getNumIncomingValues(); ++i) {
288 assert(BBToMBB[PI->getIncomingBlock(i)]->isSuccessor(MIB->getParent()) &&
289 "I appear to have misunderstood Machine PHIs");
290 MIB.addUse(getOrCreateVReg(*PI->getIncomingValue(i)));
291 MIB.addMBB(BBToMBB[PI->getIncomingBlock(i)]);
292 }
293 }
Tim Northover14e7f732016-08-05 17:50:36 +0000294
295 PendingPHIs.clear();
Tim Northover97d0cb32016-08-05 17:16:40 +0000296}
297
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000298bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombeta7fae162016-02-11 17:53:23 +0000299 MIRBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000300 switch(Inst.getOpcode()) {
Tim Northover357f1be2016-08-10 23:02:41 +0000301#define HANDLE_INST(NUM, OPCODE, CLASS) \
302 case Instruction::OPCODE: return translate##OPCODE(Inst);
303#include "llvm/IR/Instruction.def"
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000304 default:
Tim Northover357f1be2016-08-10 23:02:41 +0000305 llvm_unreachable("unknown opcode");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000306 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000307}
308
Tim Northover5ed648e2016-08-09 21:28:04 +0000309bool IRTranslator::translate(const Constant &C, unsigned Reg) {
Tim Northoverd403a3d2016-08-09 23:01:30 +0000310 if (auto CI = dyn_cast<ConstantInt>(&C))
Tim Northover5ed648e2016-08-09 21:28:04 +0000311 EntryBuilder.buildConstant(LLT{*CI->getType()}, Reg, CI->getZExtValue());
Tim Northoverd403a3d2016-08-09 23:01:30 +0000312 else if (isa<UndefValue>(C))
313 EntryBuilder.buildInstr(TargetOpcode::IMPLICIT_DEF).addDef(Reg);
Tim Northover8e0c53a2016-08-11 21:40:55 +0000314 else if (isa<ConstantPointerNull>(C))
315 EntryBuilder.buildInstr(TargetOpcode::G_CONSTANT, LLT{*C.getType()})
316 .addDef(Reg)
317 .addImm(0);
Tim Northover357f1be2016-08-10 23:02:41 +0000318 else if (auto CE = dyn_cast<ConstantExpr>(&C)) {
319 switch(CE->getOpcode()) {
320#define HANDLE_INST(NUM, OPCODE, CLASS) \
321 case Instruction::OPCODE: return translate##OPCODE(*CE);
322#include "llvm/IR/Instruction.def"
323 default:
324 llvm_unreachable("unknown opcode");
325 }
326 } else
Tim Northoverd403a3d2016-08-09 23:01:30 +0000327 llvm_unreachable("unhandled constant kind");
Tim Northover5ed648e2016-08-09 21:28:04 +0000328
Tim Northoverd403a3d2016-08-09 23:01:30 +0000329 return true;
Tim Northover5ed648e2016-08-09 21:28:04 +0000330}
331
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000332
Tim Northover0d510442016-08-11 16:21:29 +0000333void IRTranslator::finalizeFunction() {
334 finishPendingPhis();
335
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000336 // Release the memory used by the different maps we
337 // needed during the translation.
Quentin Colombetccd77252016-02-11 21:48:32 +0000338 ValToVReg.clear();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000339 Constants.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000340}
341
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000342bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000343 const Function &F = *MF.getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000344 if (F.empty())
345 return false;
Quentin Colombetba2a0162016-02-16 19:26:02 +0000346 CLI = MF.getSubtarget().getCallLowering();
Quentin Colombet000b5802016-03-11 17:27:51 +0000347 MIRBuilder.setMF(MF);
Tim Northover5ed648e2016-08-09 21:28:04 +0000348 EntryBuilder.setMF(MF);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000349 MRI = &MF.getRegInfo();
Tim Northoverbd505462016-07-22 16:59:52 +0000350 DL = &F.getParent()->getDataLayout();
351
Tim Northover14e7f732016-08-05 17:50:36 +0000352 assert(PendingPHIs.empty() && "stale PHIs");
353
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000354 // Setup the arguments.
Quentin Colombet53237a92016-03-11 17:27:43 +0000355 MachineBasicBlock &MBB = getOrCreateBB(F.front());
Quentin Colombet91ebd712016-03-11 17:27:47 +0000356 MIRBuilder.setMBB(MBB);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000357 SmallVector<unsigned, 8> VRegArgs;
358 for (const Argument &Arg: F.args())
Quentin Colombete225e252016-03-11 17:27:54 +0000359 VRegArgs.push_back(getOrCreateVReg(Arg));
Quentin Colombetba2a0162016-02-16 19:26:02 +0000360 bool Succeeded =
Tom Stellardb72a65f2016-04-14 17:23:33 +0000361 CLI->lowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000362 if (!Succeeded)
363 report_fatal_error("Unable to lower arguments");
364
Tim Northover5ed648e2016-08-09 21:28:04 +0000365 // Now that we've got the ABI handling code, it's safe to set a location for
366 // any Constants we find in the IR.
367 if (MBB.empty())
368 EntryBuilder.setMBB(MBB);
369 else
370 EntryBuilder.setInstr(MBB.back(), /* Before */ false);
371
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000372 for (const BasicBlock &BB: F) {
Quentin Colombet53237a92016-03-11 17:27:43 +0000373 MachineBasicBlock &MBB = getOrCreateBB(BB);
Quentin Colombet91ebd712016-03-11 17:27:47 +0000374 // Set the insertion point of all the following translations to
375 // the end of this basic block.
376 MIRBuilder.setMBB(MBB);
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000377 for (const Instruction &Inst: BB) {
378 bool Succeeded = translate(Inst);
379 if (!Succeeded) {
380 DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
381 report_fatal_error("Unable to translate instruction");
382 }
383 }
384 }
Tim Northover72eebfa2016-07-12 22:23:42 +0000385
Tim Northover0d510442016-08-11 16:21:29 +0000386 finalizeFunction();
Tim Northover97d0cb32016-08-05 17:16:40 +0000387
Tim Northover72eebfa2016-07-12 22:23:42 +0000388 // Now that the MachineFrameInfo has been configured, no further changes to
389 // the reserved registers are possible.
390 MRI->freezeReservedRegs(MF);
391
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000392 return false;
393}