blob: 4ea0951692db7098f20eab6d7dccf11408a3af44 [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();
190 const Function &F = *CI.getCalledFunction();
191 Intrinsic::ID ID = F.getIntrinsicID();
192 if (TII && ID == Intrinsic::not_intrinsic)
193 ID = static_cast<Intrinsic::ID>(TII->getIntrinsicID(&F));
194
195 assert(ID != Intrinsic::not_intrinsic && "FIXME: support real calls");
196
197 // Need types (starting with return) & args.
198 SmallVector<LLT, 4> Tys;
199 Tys.emplace_back(*CI.getType());
200 for (auto &Arg : CI.arg_operands())
201 Tys.emplace_back(*Arg->getType());
202
203 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
204 MachineInstrBuilder MIB =
205 MIRBuilder.buildIntrinsic(Tys, ID, Res, !CI.doesNotAccessMemory());
206
207 for (auto &Arg : CI.arg_operands()) {
208 if (ConstantInt *CI = dyn_cast<ConstantInt>(Arg))
209 MIB.addImm(CI->getSExtValue());
210 else
211 MIB.addUse(getOrCreateVReg(*Arg));
212 }
213 return true;
214}
215
Tim Northoverbd505462016-07-22 16:59:52 +0000216bool IRTranslator::translateStaticAlloca(const AllocaInst &AI) {
217 assert(AI.isStaticAlloca() && "only handle static allocas now");
218 MachineFunction &MF = MIRBuilder.getMF();
219 unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
220 unsigned Size =
221 ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
222
Tim Northover8d2f52e2016-07-27 17:47:54 +0000223 // Always allocate at least one byte.
224 Size = std::max(Size, 1u);
225
Tim Northoverbd505462016-07-22 16:59:52 +0000226 unsigned Alignment = AI.getAlignment();
227 if (!Alignment)
228 Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
229
230 unsigned Res = getOrCreateVReg(AI);
Matthias Braun93320392016-07-28 20:13:42 +0000231 int FI = MF.getFrameInfo().CreateStackObject(Size, Alignment, false, &AI);
Tim Northoverbd505462016-07-22 16:59:52 +0000232 MIRBuilder.buildFrameIndex(LLT::pointer(0), Res, FI);
233 return true;
234}
235
Tim Northover97d0cb32016-08-05 17:16:40 +0000236bool IRTranslator::translatePhi(const PHINode &PI) {
237 MachineInstrBuilder MIB = MIRBuilder.buildInstr(TargetOpcode::PHI);
238 MIB.addDef(getOrCreateVReg(PI));
239
240 PendingPHIs.emplace_back(&PI, MIB.getInstr());
241 return true;
242}
243
244void IRTranslator::finishPendingPhis() {
245 for (std::pair<const PHINode *, MachineInstr *> &Phi : PendingPHIs) {
246 const PHINode *PI = Phi.first;
247 MachineInstrBuilder MIB(MIRBuilder.getMF(), Phi.second);
248
249 // All MachineBasicBlocks exist, add them to the PHI. We assume IRTranslator
250 // won't create extra control flow here, otherwise we need to find the
251 // dominating predecessor here (or perhaps force the weirder IRTranslators
252 // to provide a simple boundary).
253 for (unsigned i = 0; i < PI->getNumIncomingValues(); ++i) {
254 assert(BBToMBB[PI->getIncomingBlock(i)]->isSuccessor(MIB->getParent()) &&
255 "I appear to have misunderstood Machine PHIs");
256 MIB.addUse(getOrCreateVReg(*PI->getIncomingValue(i)));
257 MIB.addMBB(BBToMBB[PI->getIncomingBlock(i)]);
258 }
259 }
Tim Northover14e7f732016-08-05 17:50:36 +0000260
261 PendingPHIs.clear();
Tim Northover97d0cb32016-08-05 17:16:40 +0000262}
263
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000264bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombeta7fae162016-02-11 17:53:23 +0000265 MIRBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000266 switch(Inst.getOpcode()) {
Quentin Colombet19df8a12016-07-21 17:26:41 +0000267 // Arithmetic operations.
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000268 case Instruction::Add:
Tim Northover0d56e052016-07-29 18:11:21 +0000269 return translateBinaryOp(TargetOpcode::G_ADD, cast<BinaryOperator>(Inst));
Quentin Colombet2b59eab2016-07-21 17:26:50 +0000270 case Instruction::Sub:
Tim Northover0d56e052016-07-29 18:11:21 +0000271 return translateBinaryOp(TargetOpcode::G_SUB, cast<BinaryOperator>(Inst));
Tim Northoverbd505462016-07-22 16:59:52 +0000272
Quentin Colombet19df8a12016-07-21 17:26:41 +0000273 // Bitwise operations.
Quentin Colombet7bcc9212016-07-21 15:50:42 +0000274 case Instruction::And:
Tim Northover0d56e052016-07-29 18:11:21 +0000275 return translateBinaryOp(TargetOpcode::G_AND, cast<BinaryOperator>(Inst));
Tim Northover1cfa9192016-08-04 21:39:44 +0000276 case Instruction::Mul:
277 return translateBinaryOp(TargetOpcode::G_MUL, cast<BinaryOperator>(Inst));
Quentin Colombetf2a19092016-06-10 20:50:35 +0000278 case Instruction::Or:
Tim Northover0d56e052016-07-29 18:11:21 +0000279 return translateBinaryOp(TargetOpcode::G_OR, cast<BinaryOperator>(Inst));
Ahmed Bougacha784e3422016-07-29 16:56:20 +0000280 case Instruction::Xor:
Tim Northover0d56e052016-07-29 18:11:21 +0000281 return translateBinaryOp(TargetOpcode::G_XOR, cast<BinaryOperator>(Inst));
Tim Northoverbd505462016-07-22 16:59:52 +0000282
Quentin Colombet19df8a12016-07-21 17:26:41 +0000283 // Branch operations.
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000284 case Instruction::Br:
Tim Northover0d56e052016-07-29 18:11:21 +0000285 return translateBr(cast<BranchInst>(Inst));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000286 case Instruction::Ret:
Tim Northover0d56e052016-07-29 18:11:21 +0000287 return translateReturn(cast<ReturnInst>(Inst));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000288
Tim Northover5fb414d2016-07-29 22:32:36 +0000289 // Calls
290 case Instruction::Call:
291 return translateCall(cast<CallInst>(Inst));
292
Tim Northover06db18f2016-08-04 18:35:17 +0000293 // Casts and allied operations
Tim Northover7c9eba92016-07-25 21:01:29 +0000294 case Instruction::BitCast:
295 return translateBitCast(cast<CastInst>(Inst));
296 case Instruction::IntToPtr:
297 return translateCast(TargetOpcode::G_INTTOPTR, cast<CastInst>(Inst));
298 case Instruction::PtrToInt:
299 return translateCast(TargetOpcode::G_PTRTOINT, cast<CastInst>(Inst));
Tim Northover06db18f2016-08-04 18:35:17 +0000300 case Instruction::Trunc:
301 return translateCast(TargetOpcode::G_TRUNC, cast<CastInst>(Inst));
Tim Northover7c9eba92016-07-25 21:01:29 +0000302
Tim Northoverad2b7172016-07-26 20:23:26 +0000303 // Memory ops.
304 case Instruction::Load:
305 return translateLoad(cast<LoadInst>(Inst));
306 case Instruction::Store:
307 return translateStore(cast<StoreInst>(Inst));
308
Tim Northoverbd505462016-07-22 16:59:52 +0000309 case Instruction::Alloca:
310 return translateStaticAlloca(cast<AllocaInst>(Inst));
311
Tim Northover97d0cb32016-08-05 17:16:40 +0000312 case Instruction::PHI:
313 return translatePhi(cast<PHINode>(Inst));
314
Tim Northover5fc93b72016-07-29 22:41:55 +0000315 case Instruction::Unreachable:
316 return true;
317
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000318 default:
319 llvm_unreachable("Opcode not supported");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000320 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000321}
322
Tim Northover5ed648e2016-08-09 21:28:04 +0000323bool IRTranslator::translate(const Constant &C, unsigned Reg) {
Tim Northoverd403a3d2016-08-09 23:01:30 +0000324 if (auto CI = dyn_cast<ConstantInt>(&C))
Tim Northover5ed648e2016-08-09 21:28:04 +0000325 EntryBuilder.buildConstant(LLT{*CI->getType()}, Reg, CI->getZExtValue());
Tim Northoverd403a3d2016-08-09 23:01:30 +0000326 else if (isa<UndefValue>(C))
327 EntryBuilder.buildInstr(TargetOpcode::IMPLICIT_DEF).addDef(Reg);
328 else
329 llvm_unreachable("unhandled constant kind");
Tim Northover5ed648e2016-08-09 21:28:04 +0000330
Tim Northoverd403a3d2016-08-09 23:01:30 +0000331 return true;
Tim Northover5ed648e2016-08-09 21:28:04 +0000332}
333
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000334
335void IRTranslator::finalize() {
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 Northover97d0cb32016-08-05 17:16:40 +0000386 finishPendingPhis();
387
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}