blob: a5f62cf0f396d01c40e875f8d6ddf243a3967656 [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 Northover756eca32016-07-26 16:45:30 +0000169 MIRBuilder.buildCopy(getOrCreateVReg(CI),
170 getOrCreateVReg(*CI.getOperand(0)));
Tim Northover7c9eba92016-07-25 21:01:29 +0000171 return true;
172 }
173 return translateCast(TargetOpcode::G_BITCAST, CI);
174}
175
176bool IRTranslator::translateCast(unsigned Opcode, const CastInst &CI) {
177 unsigned Op = getOrCreateVReg(*CI.getOperand(0));
178 unsigned Res = getOrCreateVReg(CI);
Tim Northovera51575f2016-07-29 17:43:52 +0000179 MIRBuilder.buildInstr(Opcode, {LLT{*CI.getDestTy()}, LLT{*CI.getSrcTy()}})
180 .addDef(Res)
181 .addUse(Op);
Tim Northover7c9eba92016-07-25 21:01:29 +0000182 return true;
183}
184
Tim Northover5fb414d2016-07-29 22:32:36 +0000185bool IRTranslator::translateCall(const CallInst &CI) {
186 auto TII = MIRBuilder.getMF().getTarget().getIntrinsicInfo();
187 const Function &F = *CI.getCalledFunction();
188 Intrinsic::ID ID = F.getIntrinsicID();
189 if (TII && ID == Intrinsic::not_intrinsic)
190 ID = static_cast<Intrinsic::ID>(TII->getIntrinsicID(&F));
191
192 assert(ID != Intrinsic::not_intrinsic && "FIXME: support real calls");
193
194 // Need types (starting with return) & args.
195 SmallVector<LLT, 4> Tys;
196 Tys.emplace_back(*CI.getType());
197 for (auto &Arg : CI.arg_operands())
198 Tys.emplace_back(*Arg->getType());
199
200 unsigned Res = CI.getType()->isVoidTy() ? 0 : getOrCreateVReg(CI);
201 MachineInstrBuilder MIB =
202 MIRBuilder.buildIntrinsic(Tys, ID, Res, !CI.doesNotAccessMemory());
203
204 for (auto &Arg : CI.arg_operands()) {
205 if (ConstantInt *CI = dyn_cast<ConstantInt>(Arg))
206 MIB.addImm(CI->getSExtValue());
207 else
208 MIB.addUse(getOrCreateVReg(*Arg));
209 }
210 return true;
211}
212
Tim Northoverbd505462016-07-22 16:59:52 +0000213bool IRTranslator::translateStaticAlloca(const AllocaInst &AI) {
214 assert(AI.isStaticAlloca() && "only handle static allocas now");
215 MachineFunction &MF = MIRBuilder.getMF();
216 unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
217 unsigned Size =
218 ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
219
Tim Northover8d2f52e2016-07-27 17:47:54 +0000220 // Always allocate at least one byte.
221 Size = std::max(Size, 1u);
222
Tim Northoverbd505462016-07-22 16:59:52 +0000223 unsigned Alignment = AI.getAlignment();
224 if (!Alignment)
225 Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
226
227 unsigned Res = getOrCreateVReg(AI);
Matthias Braun93320392016-07-28 20:13:42 +0000228 int FI = MF.getFrameInfo().CreateStackObject(Size, Alignment, false, &AI);
Tim Northoverbd505462016-07-22 16:59:52 +0000229 MIRBuilder.buildFrameIndex(LLT::pointer(0), Res, FI);
230 return true;
231}
232
Tim Northover97d0cb32016-08-05 17:16:40 +0000233bool IRTranslator::translatePhi(const PHINode &PI) {
234 MachineInstrBuilder MIB = MIRBuilder.buildInstr(TargetOpcode::PHI);
235 MIB.addDef(getOrCreateVReg(PI));
236
237 PendingPHIs.emplace_back(&PI, MIB.getInstr());
238 return true;
239}
240
241void IRTranslator::finishPendingPhis() {
242 for (std::pair<const PHINode *, MachineInstr *> &Phi : PendingPHIs) {
243 const PHINode *PI = Phi.first;
244 MachineInstrBuilder MIB(MIRBuilder.getMF(), Phi.second);
245
246 // All MachineBasicBlocks exist, add them to the PHI. We assume IRTranslator
247 // won't create extra control flow here, otherwise we need to find the
248 // dominating predecessor here (or perhaps force the weirder IRTranslators
249 // to provide a simple boundary).
250 for (unsigned i = 0; i < PI->getNumIncomingValues(); ++i) {
251 assert(BBToMBB[PI->getIncomingBlock(i)]->isSuccessor(MIB->getParent()) &&
252 "I appear to have misunderstood Machine PHIs");
253 MIB.addUse(getOrCreateVReg(*PI->getIncomingValue(i)));
254 MIB.addMBB(BBToMBB[PI->getIncomingBlock(i)]);
255 }
256 }
Tim Northover14e7f732016-08-05 17:50:36 +0000257
258 PendingPHIs.clear();
Tim Northover97d0cb32016-08-05 17:16:40 +0000259}
260
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000261bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombeta7fae162016-02-11 17:53:23 +0000262 MIRBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000263 switch(Inst.getOpcode()) {
Quentin Colombet19df8a12016-07-21 17:26:41 +0000264 // Arithmetic operations.
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000265 case Instruction::Add:
Tim Northover0d56e052016-07-29 18:11:21 +0000266 return translateBinaryOp(TargetOpcode::G_ADD, cast<BinaryOperator>(Inst));
Quentin Colombet2b59eab2016-07-21 17:26:50 +0000267 case Instruction::Sub:
Tim Northover0d56e052016-07-29 18:11:21 +0000268 return translateBinaryOp(TargetOpcode::G_SUB, cast<BinaryOperator>(Inst));
Tim Northoverbd505462016-07-22 16:59:52 +0000269
Quentin Colombet19df8a12016-07-21 17:26:41 +0000270 // Bitwise operations.
Quentin Colombet7bcc9212016-07-21 15:50:42 +0000271 case Instruction::And:
Tim Northover0d56e052016-07-29 18:11:21 +0000272 return translateBinaryOp(TargetOpcode::G_AND, cast<BinaryOperator>(Inst));
Tim Northover1cfa9192016-08-04 21:39:44 +0000273 case Instruction::Mul:
274 return translateBinaryOp(TargetOpcode::G_MUL, cast<BinaryOperator>(Inst));
Quentin Colombetf2a19092016-06-10 20:50:35 +0000275 case Instruction::Or:
Tim Northover0d56e052016-07-29 18:11:21 +0000276 return translateBinaryOp(TargetOpcode::G_OR, cast<BinaryOperator>(Inst));
Ahmed Bougacha784e3422016-07-29 16:56:20 +0000277 case Instruction::Xor:
Tim Northover0d56e052016-07-29 18:11:21 +0000278 return translateBinaryOp(TargetOpcode::G_XOR, cast<BinaryOperator>(Inst));
Tim Northoverbd505462016-07-22 16:59:52 +0000279
Quentin Colombet19df8a12016-07-21 17:26:41 +0000280 // Branch operations.
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000281 case Instruction::Br:
Tim Northover0d56e052016-07-29 18:11:21 +0000282 return translateBr(cast<BranchInst>(Inst));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000283 case Instruction::Ret:
Tim Northover0d56e052016-07-29 18:11:21 +0000284 return translateReturn(cast<ReturnInst>(Inst));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000285
Tim Northover5fb414d2016-07-29 22:32:36 +0000286 // Calls
287 case Instruction::Call:
288 return translateCall(cast<CallInst>(Inst));
289
Tim Northover06db18f2016-08-04 18:35:17 +0000290 // Casts and allied operations
Tim Northover7c9eba92016-07-25 21:01:29 +0000291 case Instruction::BitCast:
292 return translateBitCast(cast<CastInst>(Inst));
293 case Instruction::IntToPtr:
294 return translateCast(TargetOpcode::G_INTTOPTR, cast<CastInst>(Inst));
295 case Instruction::PtrToInt:
296 return translateCast(TargetOpcode::G_PTRTOINT, cast<CastInst>(Inst));
Tim Northover06db18f2016-08-04 18:35:17 +0000297 case Instruction::Trunc:
298 return translateCast(TargetOpcode::G_TRUNC, cast<CastInst>(Inst));
Tim Northover7c9eba92016-07-25 21:01:29 +0000299
Tim Northoverad2b7172016-07-26 20:23:26 +0000300 // Memory ops.
301 case Instruction::Load:
302 return translateLoad(cast<LoadInst>(Inst));
303 case Instruction::Store:
304 return translateStore(cast<StoreInst>(Inst));
305
Tim Northoverbd505462016-07-22 16:59:52 +0000306 case Instruction::Alloca:
307 return translateStaticAlloca(cast<AllocaInst>(Inst));
308
Tim Northover97d0cb32016-08-05 17:16:40 +0000309 case Instruction::PHI:
310 return translatePhi(cast<PHINode>(Inst));
311
Tim Northover5fc93b72016-07-29 22:41:55 +0000312 case Instruction::Unreachable:
313 return true;
314
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000315 default:
316 llvm_unreachable("Opcode not supported");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000317 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000318}
319
Tim Northover5ed648e2016-08-09 21:28:04 +0000320bool IRTranslator::translate(const Constant &C, unsigned Reg) {
Tim Northoverd403a3d2016-08-09 23:01:30 +0000321 if (auto CI = dyn_cast<ConstantInt>(&C))
Tim Northover5ed648e2016-08-09 21:28:04 +0000322 EntryBuilder.buildConstant(LLT{*CI->getType()}, Reg, CI->getZExtValue());
Tim Northoverd403a3d2016-08-09 23:01:30 +0000323 else if (isa<UndefValue>(C))
324 EntryBuilder.buildInstr(TargetOpcode::IMPLICIT_DEF).addDef(Reg);
325 else
326 llvm_unreachable("unhandled constant kind");
Tim Northover5ed648e2016-08-09 21:28:04 +0000327
Tim Northoverd403a3d2016-08-09 23:01:30 +0000328 return true;
Tim Northover5ed648e2016-08-09 21:28:04 +0000329}
330
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000331
332void IRTranslator::finalize() {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000333 // Release the memory used by the different maps we
334 // needed during the translation.
Quentin Colombetccd77252016-02-11 21:48:32 +0000335 ValToVReg.clear();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000336 Constants.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000337}
338
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000339bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000340 const Function &F = *MF.getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000341 if (F.empty())
342 return false;
Quentin Colombetba2a0162016-02-16 19:26:02 +0000343 CLI = MF.getSubtarget().getCallLowering();
Quentin Colombet000b5802016-03-11 17:27:51 +0000344 MIRBuilder.setMF(MF);
Tim Northover5ed648e2016-08-09 21:28:04 +0000345 EntryBuilder.setMF(MF);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000346 MRI = &MF.getRegInfo();
Tim Northoverbd505462016-07-22 16:59:52 +0000347 DL = &F.getParent()->getDataLayout();
348
Tim Northover14e7f732016-08-05 17:50:36 +0000349 assert(PendingPHIs.empty() && "stale PHIs");
350
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000351 // Setup the arguments.
Quentin Colombet53237a92016-03-11 17:27:43 +0000352 MachineBasicBlock &MBB = getOrCreateBB(F.front());
Quentin Colombet91ebd712016-03-11 17:27:47 +0000353 MIRBuilder.setMBB(MBB);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000354 SmallVector<unsigned, 8> VRegArgs;
355 for (const Argument &Arg: F.args())
Quentin Colombete225e252016-03-11 17:27:54 +0000356 VRegArgs.push_back(getOrCreateVReg(Arg));
Quentin Colombetba2a0162016-02-16 19:26:02 +0000357 bool Succeeded =
Tom Stellardb72a65f2016-04-14 17:23:33 +0000358 CLI->lowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000359 if (!Succeeded)
360 report_fatal_error("Unable to lower arguments");
361
Tim Northover5ed648e2016-08-09 21:28:04 +0000362 // Now that we've got the ABI handling code, it's safe to set a location for
363 // any Constants we find in the IR.
364 if (MBB.empty())
365 EntryBuilder.setMBB(MBB);
366 else
367 EntryBuilder.setInstr(MBB.back(), /* Before */ false);
368
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000369 for (const BasicBlock &BB: F) {
Quentin Colombet53237a92016-03-11 17:27:43 +0000370 MachineBasicBlock &MBB = getOrCreateBB(BB);
Quentin Colombet91ebd712016-03-11 17:27:47 +0000371 // Set the insertion point of all the following translations to
372 // the end of this basic block.
373 MIRBuilder.setMBB(MBB);
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000374 for (const Instruction &Inst: BB) {
375 bool Succeeded = translate(Inst);
376 if (!Succeeded) {
377 DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
378 report_fatal_error("Unable to translate instruction");
379 }
380 }
381 }
Tim Northover72eebfa2016-07-12 22:23:42 +0000382
Tim Northover97d0cb32016-08-05 17:16:40 +0000383 finishPendingPhis();
384
Tim Northover72eebfa2016-07-12 22:23:42 +0000385 // Now that the MachineFrameInfo has been configured, no further changes to
386 // the reserved registers are possible.
387 MRI->freezeReservedRegs(MF);
388
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000389 return false;
390}