blob: 788823d18d1da99eb5c2e1bfbd107a26f37b9cfd [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");
Quentin Colombete225e252016-03-11 17:27:54 +000048 assert(!Val.getType()->isAggregateType() && "Not yet implemented");
Tim Northoverbd505462016-07-22 16:59:52 +000049 unsigned Size = DL->getTypeSizeInBits(Val.getType());
Quentin Colombet17c494b2016-02-11 17:51:31 +000050 unsigned VReg = MRI->createGenericVirtualRegister(Size);
Quentin Colombetccd77252016-02-11 21:48:32 +000051 ValReg = VReg;
Tim Northover5ed648e2016-08-09 21:28:04 +000052
53 if (auto CV = dyn_cast<Constant>(&Val)) {
54 bool Success = translate(*CV, VReg);
55 if (!Success)
56 report_fatal_error("unable to translate constant");
57 }
Quentin Colombet17c494b2016-02-11 17:51:31 +000058 }
Quentin Colombetccd77252016-02-11 21:48:32 +000059 return ValReg;
Quentin Colombet17c494b2016-02-11 17:51:31 +000060}
61
Tim Northoverad2b7172016-07-26 20:23:26 +000062unsigned IRTranslator::getMemOpAlignment(const Instruction &I) {
63 unsigned Alignment = 0;
64 Type *ValTy = nullptr;
65 if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
66 Alignment = SI->getAlignment();
67 ValTy = SI->getValueOperand()->getType();
68 } else if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
69 Alignment = LI->getAlignment();
70 ValTy = LI->getType();
71 } else
72 llvm_unreachable("unhandled memory instruction");
73
74 return Alignment ? Alignment : DL->getABITypeAlignment(ValTy);
75}
76
Quentin Colombet53237a92016-03-11 17:27:43 +000077MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock &BB) {
78 MachineBasicBlock *&MBB = BBToMBB[&BB];
Quentin Colombet17c494b2016-02-11 17:51:31 +000079 if (!MBB) {
Quentin Colombeta7fae162016-02-11 17:53:23 +000080 MachineFunction &MF = MIRBuilder.getMF();
Quentin Colombet17c494b2016-02-11 17:51:31 +000081 MBB = MF.CreateMachineBasicBlock();
82 MF.push_back(MBB);
83 }
84 return *MBB;
85}
86
Tim Northover0d56e052016-07-29 18:11:21 +000087bool IRTranslator::translateBinaryOp(unsigned Opcode,
88 const BinaryOperator &Inst) {
89 // FIXME: handle signed/unsigned wrapping flags.
90
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000091 // Get or create a virtual register for each value.
92 // Unless the value is a Constant => loadimm cst?
93 // or inline constant each time?
94 // Creation of a virtual register needs to have a size.
Quentin Colombete225e252016-03-11 17:27:54 +000095 unsigned Op0 = getOrCreateVReg(*Inst.getOperand(0));
96 unsigned Op1 = getOrCreateVReg(*Inst.getOperand(1));
97 unsigned Res = getOrCreateVReg(Inst);
Tim Northovera51575f2016-07-29 17:43:52 +000098 MIRBuilder.buildInstr(Opcode, LLT{*Inst.getType()})
99 .addDef(Res)
100 .addUse(Op0)
101 .addUse(Op1);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000102 return true;
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000103}
104
Tim Northover0d56e052016-07-29 18:11:21 +0000105bool IRTranslator::translateReturn(const ReturnInst &RI) {
106 const Value *Ret = RI.getReturnValue();
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000107 // The target may mess up with the insertion point, but
108 // this is not important as a return is the last instruction
109 // of the block anyway.
Tom Stellardb72a65f2016-04-14 17:23:33 +0000110 return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000111}
112
Tim Northover0d56e052016-07-29 18:11:21 +0000113bool IRTranslator::translateBr(const BranchInst &BrInst) {
Tim Northover69c2ba52016-07-29 17:58:00 +0000114 unsigned Succ = 0;
115 if (!BrInst.isUnconditional()) {
116 // We want a G_BRCOND to the true BB followed by an unconditional branch.
117 unsigned Tst = getOrCreateVReg(*BrInst.getCondition());
118 const BasicBlock &TrueTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ++));
119 MachineBasicBlock &TrueBB = getOrCreateBB(TrueTgt);
120 MIRBuilder.buildBrCond(LLT{*BrInst.getCondition()->getType()}, Tst, TrueBB);
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000121 }
Tim Northover69c2ba52016-07-29 17:58:00 +0000122
123 const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ));
124 MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt);
125 MIRBuilder.buildBr(TgtBB);
126
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000127 // Link successors.
128 MachineBasicBlock &CurBB = MIRBuilder.getMBB();
129 for (const BasicBlock *Succ : BrInst.successors())
130 CurBB.addSuccessor(&getOrCreateBB(*Succ));
131 return true;
132}
133
Tim Northoverad2b7172016-07-26 20:23:26 +0000134bool IRTranslator::translateLoad(const LoadInst &LI) {
135 assert(LI.isSimple() && "only simple loads are supported at the moment");
136
137 MachineFunction &MF = MIRBuilder.getMF();
138 unsigned Res = getOrCreateVReg(LI);
139 unsigned Addr = getOrCreateVReg(*LI.getPointerOperand());
140 LLT VTy{*LI.getType()}, PTy{*LI.getPointerOperand()->getType()};
141
142 MIRBuilder.buildLoad(
143 VTy, PTy, Res, Addr,
144 *MF.getMachineMemOperand(MachinePointerInfo(LI.getPointerOperand()),
145 MachineMemOperand::MOLoad,
146 VTy.getSizeInBits() / 8, getMemOpAlignment(LI)));
147 return true;
148}
149
150bool IRTranslator::translateStore(const StoreInst &SI) {
151 assert(SI.isSimple() && "only simple loads are supported at the moment");
152
153 MachineFunction &MF = MIRBuilder.getMF();
154 unsigned Val = getOrCreateVReg(*SI.getValueOperand());
155 unsigned Addr = getOrCreateVReg(*SI.getPointerOperand());
156 LLT VTy{*SI.getValueOperand()->getType()},
157 PTy{*SI.getPointerOperand()->getType()};
158
159 MIRBuilder.buildStore(
160 VTy, PTy, Val, Addr,
161 *MF.getMachineMemOperand(MachinePointerInfo(SI.getPointerOperand()),
162 MachineMemOperand::MOStore,
163 VTy.getSizeInBits() / 8, getMemOpAlignment(SI)));
164 return true;
165}
166
Tim Northover7c9eba92016-07-25 21:01:29 +0000167bool IRTranslator::translateBitCast(const CastInst &CI) {
168 if (LLT{*CI.getDestTy()} == LLT{*CI.getSrcTy()}) {
Tim Northover7552ef52016-08-10 16:51:14 +0000169 unsigned &Reg = ValToVReg[&CI];
170 if (Reg)
171 MIRBuilder.buildCopy(Reg, getOrCreateVReg(*CI.getOperand(0)));
172 else
173 Reg = getOrCreateVReg(*CI.getOperand(0));
Tim Northover7c9eba92016-07-25 21:01:29 +0000174 return true;
175 }
176 return translateCast(TargetOpcode::G_BITCAST, CI);
177}
178
179bool IRTranslator::translateCast(unsigned Opcode, const CastInst &CI) {
180 unsigned Op = getOrCreateVReg(*CI.getOperand(0));
181 unsigned Res = getOrCreateVReg(CI);
Tim Northovera51575f2016-07-29 17:43:52 +0000182 MIRBuilder.buildInstr(Opcode, {LLT{*CI.getDestTy()}, LLT{*CI.getSrcTy()}})
183 .addDef(Res)
184 .addUse(Op);
Tim Northover7c9eba92016-07-25 21:01:29 +0000185 return true;
186}
187
Tim Northover5fb414d2016-07-29 22:32:36 +0000188bool IRTranslator::translateCall(const CallInst &CI) {
189 auto TII = MIRBuilder.getMF().getTarget().getIntrinsicInfo();
Tim Northover406024a2016-08-10 21:44:01 +0000190 const Function *F = CI.getCalledFunction();
Tim Northover5fb414d2016-07-29 22:32:36 +0000191
Tim Northover406024a2016-08-10 21:44:01 +0000192 if (!F || !F->isIntrinsic()) {
193 // FIXME: handle multiple return values.
194 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
195 SmallVector<unsigned, 8> Args;
196 for (auto &Arg: CI.arg_operands())
197 Args.push_back(getOrCreateVReg(*Arg));
198
199 return CLI->lowerCall(MIRBuilder, CI,
200 F ? 0 : getOrCreateVReg(*CI.getCalledValue()), Res,
201 Args);
202 }
203
204 Intrinsic::ID ID = F->getIntrinsicID();
205 if (TII && ID == Intrinsic::not_intrinsic)
206 ID = static_cast<Intrinsic::ID>(TII->getIntrinsicID(F));
207
208 assert(ID != Intrinsic::not_intrinsic && "unknown intrinsic");
Tim Northover5fb414d2016-07-29 22:32:36 +0000209
210 // Need types (starting with return) & args.
211 SmallVector<LLT, 4> Tys;
212 Tys.emplace_back(*CI.getType());
213 for (auto &Arg : CI.arg_operands())
214 Tys.emplace_back(*Arg->getType());
215
216 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
217 MachineInstrBuilder MIB =
218 MIRBuilder.buildIntrinsic(Tys, ID, Res, !CI.doesNotAccessMemory());
219
220 for (auto &Arg : CI.arg_operands()) {
221 if (ConstantInt *CI = dyn_cast<ConstantInt>(Arg))
222 MIB.addImm(CI->getSExtValue());
223 else
224 MIB.addUse(getOrCreateVReg(*Arg));
225 }
226 return true;
227}
228
Tim Northoverbd505462016-07-22 16:59:52 +0000229bool IRTranslator::translateStaticAlloca(const AllocaInst &AI) {
230 assert(AI.isStaticAlloca() && "only handle static allocas now");
231 MachineFunction &MF = MIRBuilder.getMF();
232 unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
233 unsigned Size =
234 ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
235
Tim Northover8d2f52e2016-07-27 17:47:54 +0000236 // Always allocate at least one byte.
237 Size = std::max(Size, 1u);
238
Tim Northoverbd505462016-07-22 16:59:52 +0000239 unsigned Alignment = AI.getAlignment();
240 if (!Alignment)
241 Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
242
243 unsigned Res = getOrCreateVReg(AI);
Matthias Braun93320392016-07-28 20:13:42 +0000244 int FI = MF.getFrameInfo().CreateStackObject(Size, Alignment, false, &AI);
Tim Northoverbd505462016-07-22 16:59:52 +0000245 MIRBuilder.buildFrameIndex(LLT::pointer(0), Res, FI);
246 return true;
247}
248
Tim Northover97d0cb32016-08-05 17:16:40 +0000249bool IRTranslator::translatePhi(const PHINode &PI) {
250 MachineInstrBuilder MIB = MIRBuilder.buildInstr(TargetOpcode::PHI);
251 MIB.addDef(getOrCreateVReg(PI));
252
253 PendingPHIs.emplace_back(&PI, MIB.getInstr());
254 return true;
255}
256
257void IRTranslator::finishPendingPhis() {
258 for (std::pair<const PHINode *, MachineInstr *> &Phi : PendingPHIs) {
259 const PHINode *PI = Phi.first;
260 MachineInstrBuilder MIB(MIRBuilder.getMF(), Phi.second);
261
262 // All MachineBasicBlocks exist, add them to the PHI. We assume IRTranslator
263 // won't create extra control flow here, otherwise we need to find the
264 // dominating predecessor here (or perhaps force the weirder IRTranslators
265 // to provide a simple boundary).
266 for (unsigned i = 0; i < PI->getNumIncomingValues(); ++i) {
267 assert(BBToMBB[PI->getIncomingBlock(i)]->isSuccessor(MIB->getParent()) &&
268 "I appear to have misunderstood Machine PHIs");
269 MIB.addUse(getOrCreateVReg(*PI->getIncomingValue(i)));
270 MIB.addMBB(BBToMBB[PI->getIncomingBlock(i)]);
271 }
272 }
Tim Northover14e7f732016-08-05 17:50:36 +0000273
274 PendingPHIs.clear();
Tim Northover97d0cb32016-08-05 17:16:40 +0000275}
276
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000277bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombeta7fae162016-02-11 17:53:23 +0000278 MIRBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000279 switch(Inst.getOpcode()) {
Quentin Colombet19df8a12016-07-21 17:26:41 +0000280 // Arithmetic operations.
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000281 case Instruction::Add:
Tim Northover0d56e052016-07-29 18:11:21 +0000282 return translateBinaryOp(TargetOpcode::G_ADD, cast<BinaryOperator>(Inst));
Quentin Colombet2b59eab2016-07-21 17:26:50 +0000283 case Instruction::Sub:
Tim Northover0d56e052016-07-29 18:11:21 +0000284 return translateBinaryOp(TargetOpcode::G_SUB, cast<BinaryOperator>(Inst));
Tim Northoverbd505462016-07-22 16:59:52 +0000285
Quentin Colombet19df8a12016-07-21 17:26:41 +0000286 // Bitwise operations.
Quentin Colombet7bcc9212016-07-21 15:50:42 +0000287 case Instruction::And:
Tim Northover0d56e052016-07-29 18:11:21 +0000288 return translateBinaryOp(TargetOpcode::G_AND, cast<BinaryOperator>(Inst));
Tim Northover1cfa9192016-08-04 21:39:44 +0000289 case Instruction::Mul:
290 return translateBinaryOp(TargetOpcode::G_MUL, cast<BinaryOperator>(Inst));
Quentin Colombetf2a19092016-06-10 20:50:35 +0000291 case Instruction::Or:
Tim Northover0d56e052016-07-29 18:11:21 +0000292 return translateBinaryOp(TargetOpcode::G_OR, cast<BinaryOperator>(Inst));
Ahmed Bougacha784e3422016-07-29 16:56:20 +0000293 case Instruction::Xor:
Tim Northover0d56e052016-07-29 18:11:21 +0000294 return translateBinaryOp(TargetOpcode::G_XOR, cast<BinaryOperator>(Inst));
Tim Northoverbd505462016-07-22 16:59:52 +0000295
Quentin Colombet19df8a12016-07-21 17:26:41 +0000296 // Branch operations.
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000297 case Instruction::Br:
Tim Northover0d56e052016-07-29 18:11:21 +0000298 return translateBr(cast<BranchInst>(Inst));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000299 case Instruction::Ret:
Tim Northover0d56e052016-07-29 18:11:21 +0000300 return translateReturn(cast<ReturnInst>(Inst));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000301
Tim Northover5fb414d2016-07-29 22:32:36 +0000302 // Calls
303 case Instruction::Call:
304 return translateCall(cast<CallInst>(Inst));
305
Tim Northover06db18f2016-08-04 18:35:17 +0000306 // Casts and allied operations
Tim Northover7c9eba92016-07-25 21:01:29 +0000307 case Instruction::BitCast:
308 return translateBitCast(cast<CastInst>(Inst));
309 case Instruction::IntToPtr:
310 return translateCast(TargetOpcode::G_INTTOPTR, cast<CastInst>(Inst));
311 case Instruction::PtrToInt:
312 return translateCast(TargetOpcode::G_PTRTOINT, cast<CastInst>(Inst));
Tim Northover06db18f2016-08-04 18:35:17 +0000313 case Instruction::Trunc:
314 return translateCast(TargetOpcode::G_TRUNC, cast<CastInst>(Inst));
Tim Northover7c9eba92016-07-25 21:01:29 +0000315
Tim Northoverad2b7172016-07-26 20:23:26 +0000316 // Memory ops.
317 case Instruction::Load:
318 return translateLoad(cast<LoadInst>(Inst));
319 case Instruction::Store:
320 return translateStore(cast<StoreInst>(Inst));
321
Tim Northoverbd505462016-07-22 16:59:52 +0000322 case Instruction::Alloca:
323 return translateStaticAlloca(cast<AllocaInst>(Inst));
324
Tim Northover97d0cb32016-08-05 17:16:40 +0000325 case Instruction::PHI:
326 return translatePhi(cast<PHINode>(Inst));
327
Tim Northover5fc93b72016-07-29 22:41:55 +0000328 case Instruction::Unreachable:
329 return true;
330
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000331 default:
332 llvm_unreachable("Opcode not supported");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000333 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000334}
335
Tim Northover5ed648e2016-08-09 21:28:04 +0000336bool IRTranslator::translate(const Constant &C, unsigned Reg) {
Tim Northoverd403a3d2016-08-09 23:01:30 +0000337 if (auto CI = dyn_cast<ConstantInt>(&C))
Tim Northover5ed648e2016-08-09 21:28:04 +0000338 EntryBuilder.buildConstant(LLT{*CI->getType()}, Reg, CI->getZExtValue());
Tim Northoverd403a3d2016-08-09 23:01:30 +0000339 else if (isa<UndefValue>(C))
340 EntryBuilder.buildInstr(TargetOpcode::IMPLICIT_DEF).addDef(Reg);
341 else
342 llvm_unreachable("unhandled constant kind");
Tim Northover5ed648e2016-08-09 21:28:04 +0000343
Tim Northoverd403a3d2016-08-09 23:01:30 +0000344 return true;
Tim Northover5ed648e2016-08-09 21:28:04 +0000345}
346
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000347
348void IRTranslator::finalize() {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000349 // Release the memory used by the different maps we
350 // needed during the translation.
Quentin Colombetccd77252016-02-11 21:48:32 +0000351 ValToVReg.clear();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000352 Constants.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000353}
354
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000355bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000356 const Function &F = *MF.getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000357 if (F.empty())
358 return false;
Quentin Colombetba2a0162016-02-16 19:26:02 +0000359 CLI = MF.getSubtarget().getCallLowering();
Quentin Colombet000b5802016-03-11 17:27:51 +0000360 MIRBuilder.setMF(MF);
Tim Northover5ed648e2016-08-09 21:28:04 +0000361 EntryBuilder.setMF(MF);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000362 MRI = &MF.getRegInfo();
Tim Northoverbd505462016-07-22 16:59:52 +0000363 DL = &F.getParent()->getDataLayout();
364
Tim Northover14e7f732016-08-05 17:50:36 +0000365 assert(PendingPHIs.empty() && "stale PHIs");
366
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000367 // Setup the arguments.
Quentin Colombet53237a92016-03-11 17:27:43 +0000368 MachineBasicBlock &MBB = getOrCreateBB(F.front());
Quentin Colombet91ebd712016-03-11 17:27:47 +0000369 MIRBuilder.setMBB(MBB);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000370 SmallVector<unsigned, 8> VRegArgs;
371 for (const Argument &Arg: F.args())
Quentin Colombete225e252016-03-11 17:27:54 +0000372 VRegArgs.push_back(getOrCreateVReg(Arg));
Quentin Colombetba2a0162016-02-16 19:26:02 +0000373 bool Succeeded =
Tom Stellardb72a65f2016-04-14 17:23:33 +0000374 CLI->lowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000375 if (!Succeeded)
376 report_fatal_error("Unable to lower arguments");
377
Tim Northover5ed648e2016-08-09 21:28:04 +0000378 // Now that we've got the ABI handling code, it's safe to set a location for
379 // any Constants we find in the IR.
380 if (MBB.empty())
381 EntryBuilder.setMBB(MBB);
382 else
383 EntryBuilder.setInstr(MBB.back(), /* Before */ false);
384
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000385 for (const BasicBlock &BB: F) {
Quentin Colombet53237a92016-03-11 17:27:43 +0000386 MachineBasicBlock &MBB = getOrCreateBB(BB);
Quentin Colombet91ebd712016-03-11 17:27:47 +0000387 // Set the insertion point of all the following translations to
388 // the end of this basic block.
389 MIRBuilder.setMBB(MBB);
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000390 for (const Instruction &Inst: BB) {
391 bool Succeeded = translate(Inst);
392 if (!Succeeded) {
393 DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
394 report_fatal_error("Unable to translate instruction");
395 }
396 }
397 }
Tim Northover72eebfa2016-07-12 22:23:42 +0000398
Tim Northover97d0cb32016-08-05 17:16:40 +0000399 finishPendingPhis();
400
Tim Northover72eebfa2016-07-12 22:23:42 +0000401 // Now that the MachineFrameInfo has been configured, no further changes to
402 // the reserved registers are possible.
403 MRI->freezeReservedRegs(MF);
404
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000405 return false;
406}