blob: b0751c0bb5ef82deee6b75d21bbd1dd3c14613b9 [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"
Quentin Colombet17c494b2016-02-11 17:51:31 +000022#include "llvm/IR/Type.h"
23#include "llvm/IR/Value.h"
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000024#include "llvm/Target/TargetLowering.h"
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000025
26#define DEBUG_TYPE "irtranslator"
27
Quentin Colombet105cf2b2016-01-20 20:58:56 +000028using namespace llvm;
29
30char IRTranslator::ID = 0;
Quentin Colombet39293d32016-03-08 01:38:55 +000031INITIALIZE_PASS(IRTranslator, "irtranslator", "IRTranslator LLVM IR -> MI",
Tim Northover884b47e2016-07-26 03:29:18 +000032 false, false)
Quentin Colombet105cf2b2016-01-20 20:58:56 +000033
Quentin Colombeta7fae162016-02-11 17:53:23 +000034IRTranslator::IRTranslator() : MachineFunctionPass(ID), MRI(nullptr) {
Quentin Colombet39293d32016-03-08 01:38:55 +000035 initializeIRTranslatorPass(*PassRegistry::getPassRegistry());
Quentin Colombeta7fae162016-02-11 17:53:23 +000036}
37
Quentin Colombete225e252016-03-11 17:27:54 +000038unsigned IRTranslator::getOrCreateVReg(const Value &Val) {
39 unsigned &ValReg = ValToVReg[&Val];
Quentin Colombet17c494b2016-02-11 17:51:31 +000040 // Check if this is the first time we see Val.
Quentin Colombetccd77252016-02-11 21:48:32 +000041 if (!ValReg) {
Quentin Colombet17c494b2016-02-11 17:51:31 +000042 // Fill ValRegsSequence with the sequence of registers
43 // we need to concat together to produce the value.
Quentin Colombete225e252016-03-11 17:27:54 +000044 assert(Val.getType()->isSized() &&
Quentin Colombet17c494b2016-02-11 17:51:31 +000045 "Don't know how to create an empty vreg");
Quentin Colombete225e252016-03-11 17:27:54 +000046 assert(!Val.getType()->isAggregateType() && "Not yet implemented");
Tim Northoverbd505462016-07-22 16:59:52 +000047 unsigned Size = DL->getTypeSizeInBits(Val.getType());
Quentin Colombet17c494b2016-02-11 17:51:31 +000048 unsigned VReg = MRI->createGenericVirtualRegister(Size);
Quentin Colombetccd77252016-02-11 21:48:32 +000049 ValReg = VReg;
Quentin Colombet4f0ec8d2016-02-11 17:52:28 +000050 assert(!isa<Constant>(Val) && "Not yet implemented");
Quentin Colombet17c494b2016-02-11 17:51:31 +000051 }
Quentin Colombetccd77252016-02-11 21:48:32 +000052 return ValReg;
Quentin Colombet17c494b2016-02-11 17:51:31 +000053}
54
Tim Northoverad2b7172016-07-26 20:23:26 +000055unsigned IRTranslator::getMemOpAlignment(const Instruction &I) {
56 unsigned Alignment = 0;
57 Type *ValTy = nullptr;
58 if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
59 Alignment = SI->getAlignment();
60 ValTy = SI->getValueOperand()->getType();
61 } else if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
62 Alignment = LI->getAlignment();
63 ValTy = LI->getType();
64 } else
65 llvm_unreachable("unhandled memory instruction");
66
67 return Alignment ? Alignment : DL->getABITypeAlignment(ValTy);
68}
69
Quentin Colombet53237a92016-03-11 17:27:43 +000070MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock &BB) {
71 MachineBasicBlock *&MBB = BBToMBB[&BB];
Quentin Colombet17c494b2016-02-11 17:51:31 +000072 if (!MBB) {
Quentin Colombeta7fae162016-02-11 17:53:23 +000073 MachineFunction &MF = MIRBuilder.getMF();
Quentin Colombet17c494b2016-02-11 17:51:31 +000074 MBB = MF.CreateMachineBasicBlock();
75 MF.push_back(MBB);
76 }
77 return *MBB;
78}
79
Tim Northover0d56e052016-07-29 18:11:21 +000080bool IRTranslator::translateBinaryOp(unsigned Opcode,
81 const BinaryOperator &Inst) {
82 // FIXME: handle signed/unsigned wrapping flags.
83
Quentin Colombet2ecff3b2016-02-10 22:59:27 +000084 // Get or create a virtual register for each value.
85 // Unless the value is a Constant => loadimm cst?
86 // or inline constant each time?
87 // Creation of a virtual register needs to have a size.
Quentin Colombete225e252016-03-11 17:27:54 +000088 unsigned Op0 = getOrCreateVReg(*Inst.getOperand(0));
89 unsigned Op1 = getOrCreateVReg(*Inst.getOperand(1));
90 unsigned Res = getOrCreateVReg(Inst);
Tim Northovera51575f2016-07-29 17:43:52 +000091 MIRBuilder.buildInstr(Opcode, LLT{*Inst.getType()})
92 .addDef(Res)
93 .addUse(Op0)
94 .addUse(Op1);
Quentin Colombet17c494b2016-02-11 17:51:31 +000095 return true;
Quentin Colombet105cf2b2016-01-20 20:58:56 +000096}
97
Tim Northover0d56e052016-07-29 18:11:21 +000098bool IRTranslator::translateReturn(const ReturnInst &RI) {
99 const Value *Ret = RI.getReturnValue();
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000100 // The target may mess up with the insertion point, but
101 // this is not important as a return is the last instruction
102 // of the block anyway.
Tom Stellardb72a65f2016-04-14 17:23:33 +0000103 return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000104}
105
Tim Northover0d56e052016-07-29 18:11:21 +0000106bool IRTranslator::translateBr(const BranchInst &BrInst) {
Tim Northover69c2ba52016-07-29 17:58:00 +0000107 unsigned Succ = 0;
108 if (!BrInst.isUnconditional()) {
109 // We want a G_BRCOND to the true BB followed by an unconditional branch.
110 unsigned Tst = getOrCreateVReg(*BrInst.getCondition());
111 const BasicBlock &TrueTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ++));
112 MachineBasicBlock &TrueBB = getOrCreateBB(TrueTgt);
113 MIRBuilder.buildBrCond(LLT{*BrInst.getCondition()->getType()}, Tst, TrueBB);
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000114 }
Tim Northover69c2ba52016-07-29 17:58:00 +0000115
116 const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getSuccessor(Succ));
117 MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt);
118 MIRBuilder.buildBr(TgtBB);
119
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000120 // Link successors.
121 MachineBasicBlock &CurBB = MIRBuilder.getMBB();
122 for (const BasicBlock *Succ : BrInst.successors())
123 CurBB.addSuccessor(&getOrCreateBB(*Succ));
124 return true;
125}
126
Tim Northoverad2b7172016-07-26 20:23:26 +0000127bool IRTranslator::translateLoad(const LoadInst &LI) {
128 assert(LI.isSimple() && "only simple loads are supported at the moment");
129
130 MachineFunction &MF = MIRBuilder.getMF();
131 unsigned Res = getOrCreateVReg(LI);
132 unsigned Addr = getOrCreateVReg(*LI.getPointerOperand());
133 LLT VTy{*LI.getType()}, PTy{*LI.getPointerOperand()->getType()};
134
135 MIRBuilder.buildLoad(
136 VTy, PTy, Res, Addr,
137 *MF.getMachineMemOperand(MachinePointerInfo(LI.getPointerOperand()),
138 MachineMemOperand::MOLoad,
139 VTy.getSizeInBits() / 8, getMemOpAlignment(LI)));
140 return true;
141}
142
143bool IRTranslator::translateStore(const StoreInst &SI) {
144 assert(SI.isSimple() && "only simple loads are supported at the moment");
145
146 MachineFunction &MF = MIRBuilder.getMF();
147 unsigned Val = getOrCreateVReg(*SI.getValueOperand());
148 unsigned Addr = getOrCreateVReg(*SI.getPointerOperand());
149 LLT VTy{*SI.getValueOperand()->getType()},
150 PTy{*SI.getPointerOperand()->getType()};
151
152 MIRBuilder.buildStore(
153 VTy, PTy, Val, Addr,
154 *MF.getMachineMemOperand(MachinePointerInfo(SI.getPointerOperand()),
155 MachineMemOperand::MOStore,
156 VTy.getSizeInBits() / 8, getMemOpAlignment(SI)));
157 return true;
158}
159
Tim Northover7c9eba92016-07-25 21:01:29 +0000160bool IRTranslator::translateBitCast(const CastInst &CI) {
161 if (LLT{*CI.getDestTy()} == LLT{*CI.getSrcTy()}) {
Tim Northover756eca32016-07-26 16:45:30 +0000162 MIRBuilder.buildCopy(getOrCreateVReg(CI),
163 getOrCreateVReg(*CI.getOperand(0)));
Tim Northover7c9eba92016-07-25 21:01:29 +0000164 return true;
165 }
166 return translateCast(TargetOpcode::G_BITCAST, CI);
167}
168
169bool IRTranslator::translateCast(unsigned Opcode, const CastInst &CI) {
170 unsigned Op = getOrCreateVReg(*CI.getOperand(0));
171 unsigned Res = getOrCreateVReg(CI);
Tim Northovera51575f2016-07-29 17:43:52 +0000172 MIRBuilder.buildInstr(Opcode, {LLT{*CI.getDestTy()}, LLT{*CI.getSrcTy()}})
173 .addDef(Res)
174 .addUse(Op);
Tim Northover7c9eba92016-07-25 21:01:29 +0000175 return true;
176}
177
Tim Northoverbd505462016-07-22 16:59:52 +0000178bool IRTranslator::translateStaticAlloca(const AllocaInst &AI) {
179 assert(AI.isStaticAlloca() && "only handle static allocas now");
180 MachineFunction &MF = MIRBuilder.getMF();
181 unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType());
182 unsigned Size =
183 ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
184
Tim Northover8d2f52e2016-07-27 17:47:54 +0000185 // Always allocate at least one byte.
186 Size = std::max(Size, 1u);
187
Tim Northoverbd505462016-07-22 16:59:52 +0000188 unsigned Alignment = AI.getAlignment();
189 if (!Alignment)
190 Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
191
192 unsigned Res = getOrCreateVReg(AI);
Matthias Braun93320392016-07-28 20:13:42 +0000193 int FI = MF.getFrameInfo().CreateStackObject(Size, Alignment, false, &AI);
Tim Northoverbd505462016-07-22 16:59:52 +0000194 MIRBuilder.buildFrameIndex(LLT::pointer(0), Res, FI);
195 return true;
196}
197
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000198bool IRTranslator::translate(const Instruction &Inst) {
Quentin Colombeta7fae162016-02-11 17:53:23 +0000199 MIRBuilder.setDebugLoc(Inst.getDebugLoc());
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000200 switch(Inst.getOpcode()) {
Quentin Colombet19df8a12016-07-21 17:26:41 +0000201 // Arithmetic operations.
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000202 case Instruction::Add:
Tim Northover0d56e052016-07-29 18:11:21 +0000203 return translateBinaryOp(TargetOpcode::G_ADD, cast<BinaryOperator>(Inst));
Quentin Colombet2b59eab2016-07-21 17:26:50 +0000204 case Instruction::Sub:
Tim Northover0d56e052016-07-29 18:11:21 +0000205 return translateBinaryOp(TargetOpcode::G_SUB, cast<BinaryOperator>(Inst));
Tim Northoverbd505462016-07-22 16:59:52 +0000206
Quentin Colombet19df8a12016-07-21 17:26:41 +0000207 // Bitwise operations.
Quentin Colombet7bcc9212016-07-21 15:50:42 +0000208 case Instruction::And:
Tim Northover0d56e052016-07-29 18:11:21 +0000209 return translateBinaryOp(TargetOpcode::G_AND, cast<BinaryOperator>(Inst));
Quentin Colombetf2a19092016-06-10 20:50:35 +0000210 case Instruction::Or:
Tim Northover0d56e052016-07-29 18:11:21 +0000211 return translateBinaryOp(TargetOpcode::G_OR, cast<BinaryOperator>(Inst));
Ahmed Bougacha784e3422016-07-29 16:56:20 +0000212 case Instruction::Xor:
Tim Northover0d56e052016-07-29 18:11:21 +0000213 return translateBinaryOp(TargetOpcode::G_XOR, cast<BinaryOperator>(Inst));
Tim Northoverbd505462016-07-22 16:59:52 +0000214
Quentin Colombet19df8a12016-07-21 17:26:41 +0000215 // Branch operations.
Quentin Colombetdd4b1372016-03-11 17:28:03 +0000216 case Instruction::Br:
Tim Northover0d56e052016-07-29 18:11:21 +0000217 return translateBr(cast<BranchInst>(Inst));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000218 case Instruction::Ret:
Tim Northover0d56e052016-07-29 18:11:21 +0000219 return translateReturn(cast<ReturnInst>(Inst));
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000220
Tim Northover7c9eba92016-07-25 21:01:29 +0000221 // Casts
222 case Instruction::BitCast:
223 return translateBitCast(cast<CastInst>(Inst));
224 case Instruction::IntToPtr:
225 return translateCast(TargetOpcode::G_INTTOPTR, cast<CastInst>(Inst));
226 case Instruction::PtrToInt:
227 return translateCast(TargetOpcode::G_PTRTOINT, cast<CastInst>(Inst));
228
Tim Northoverad2b7172016-07-26 20:23:26 +0000229 // Memory ops.
230 case Instruction::Load:
231 return translateLoad(cast<LoadInst>(Inst));
232 case Instruction::Store:
233 return translateStore(cast<StoreInst>(Inst));
234
Tim Northoverbd505462016-07-22 16:59:52 +0000235 case Instruction::Alloca:
236 return translateStaticAlloca(cast<AllocaInst>(Inst));
237
Quentin Colombet74d7d2f2016-02-11 18:53:28 +0000238 default:
239 llvm_unreachable("Opcode not supported");
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000240 }
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000241}
242
243
244void IRTranslator::finalize() {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000245 // Release the memory used by the different maps we
246 // needed during the translation.
Quentin Colombetccd77252016-02-11 21:48:32 +0000247 ValToVReg.clear();
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000248 Constants.clear();
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000249}
250
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000251bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000252 const Function &F = *MF.getFunction();
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000253 if (F.empty())
254 return false;
Quentin Colombetba2a0162016-02-16 19:26:02 +0000255 CLI = MF.getSubtarget().getCallLowering();
Quentin Colombet000b5802016-03-11 17:27:51 +0000256 MIRBuilder.setMF(MF);
Quentin Colombet17c494b2016-02-11 17:51:31 +0000257 MRI = &MF.getRegInfo();
Tim Northoverbd505462016-07-22 16:59:52 +0000258 DL = &F.getParent()->getDataLayout();
259
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000260 // Setup the arguments.
Quentin Colombet53237a92016-03-11 17:27:43 +0000261 MachineBasicBlock &MBB = getOrCreateBB(F.front());
Quentin Colombet91ebd712016-03-11 17:27:47 +0000262 MIRBuilder.setMBB(MBB);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000263 SmallVector<unsigned, 8> VRegArgs;
264 for (const Argument &Arg: F.args())
Quentin Colombete225e252016-03-11 17:27:54 +0000265 VRegArgs.push_back(getOrCreateVReg(Arg));
Quentin Colombetba2a0162016-02-16 19:26:02 +0000266 bool Succeeded =
Tom Stellardb72a65f2016-04-14 17:23:33 +0000267 CLI->lowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs);
Quentin Colombetfd9d0a02016-02-11 19:59:41 +0000268 if (!Succeeded)
269 report_fatal_error("Unable to lower arguments");
270
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000271 for (const BasicBlock &BB: F) {
Quentin Colombet53237a92016-03-11 17:27:43 +0000272 MachineBasicBlock &MBB = getOrCreateBB(BB);
Quentin Colombet91ebd712016-03-11 17:27:47 +0000273 // Set the insertion point of all the following translations to
274 // the end of this basic block.
275 MIRBuilder.setMBB(MBB);
Quentin Colombet2ecff3b2016-02-10 22:59:27 +0000276 for (const Instruction &Inst: BB) {
277 bool Succeeded = translate(Inst);
278 if (!Succeeded) {
279 DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
280 report_fatal_error("Unable to translate instruction");
281 }
282 }
283 }
Tim Northover72eebfa2016-07-12 22:23:42 +0000284
285 // Now that the MachineFrameInfo has been configured, no further changes to
286 // the reserved registers are possible.
287 MRI->freezeReservedRegs(MF);
288
Quentin Colombet105cf2b2016-01-20 20:58:56 +0000289 return false;
290}