1*9880d681SAndroid Build Coastguard Worker //===-- llvm/CodeGen/GlobalISel/IRTranslator.cpp - IRTranslator --*- C++ -*-==//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker /// \file
10*9880d681SAndroid Build Coastguard Worker /// This file implements the IRTranslator class.
11*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
12*9880d681SAndroid Build Coastguard Worker
13*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/GlobalISel/IRTranslator.h"
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallVector.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/GlobalISel/CallLowering.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constant.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Type.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Value.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetLowering.h"
24*9880d681SAndroid Build Coastguard Worker
25*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "irtranslator"
26*9880d681SAndroid Build Coastguard Worker
27*9880d681SAndroid Build Coastguard Worker using namespace llvm;
28*9880d681SAndroid Build Coastguard Worker
29*9880d681SAndroid Build Coastguard Worker char IRTranslator::ID = 0;
30*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(IRTranslator, "irtranslator", "IRTranslator LLVM IR -> MI",
31*9880d681SAndroid Build Coastguard Worker false, false);
32*9880d681SAndroid Build Coastguard Worker
IRTranslator()33*9880d681SAndroid Build Coastguard Worker IRTranslator::IRTranslator() : MachineFunctionPass(ID), MRI(nullptr) {
34*9880d681SAndroid Build Coastguard Worker initializeIRTranslatorPass(*PassRegistry::getPassRegistry());
35*9880d681SAndroid Build Coastguard Worker }
36*9880d681SAndroid Build Coastguard Worker
getOrCreateVReg(const Value & Val)37*9880d681SAndroid Build Coastguard Worker unsigned IRTranslator::getOrCreateVReg(const Value &Val) {
38*9880d681SAndroid Build Coastguard Worker unsigned &ValReg = ValToVReg[&Val];
39*9880d681SAndroid Build Coastguard Worker // Check if this is the first time we see Val.
40*9880d681SAndroid Build Coastguard Worker if (!ValReg) {
41*9880d681SAndroid Build Coastguard Worker // Fill ValRegsSequence with the sequence of registers
42*9880d681SAndroid Build Coastguard Worker // we need to concat together to produce the value.
43*9880d681SAndroid Build Coastguard Worker assert(Val.getType()->isSized() &&
44*9880d681SAndroid Build Coastguard Worker "Don't know how to create an empty vreg");
45*9880d681SAndroid Build Coastguard Worker assert(!Val.getType()->isAggregateType() && "Not yet implemented");
46*9880d681SAndroid Build Coastguard Worker unsigned Size = Val.getType()->getPrimitiveSizeInBits();
47*9880d681SAndroid Build Coastguard Worker unsigned VReg = MRI->createGenericVirtualRegister(Size);
48*9880d681SAndroid Build Coastguard Worker ValReg = VReg;
49*9880d681SAndroid Build Coastguard Worker assert(!isa<Constant>(Val) && "Not yet implemented");
50*9880d681SAndroid Build Coastguard Worker }
51*9880d681SAndroid Build Coastguard Worker return ValReg;
52*9880d681SAndroid Build Coastguard Worker }
53*9880d681SAndroid Build Coastguard Worker
getOrCreateBB(const BasicBlock & BB)54*9880d681SAndroid Build Coastguard Worker MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock &BB) {
55*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *&MBB = BBToMBB[&BB];
56*9880d681SAndroid Build Coastguard Worker if (!MBB) {
57*9880d681SAndroid Build Coastguard Worker MachineFunction &MF = MIRBuilder.getMF();
58*9880d681SAndroid Build Coastguard Worker MBB = MF.CreateMachineBasicBlock();
59*9880d681SAndroid Build Coastguard Worker MF.push_back(MBB);
60*9880d681SAndroid Build Coastguard Worker }
61*9880d681SAndroid Build Coastguard Worker return *MBB;
62*9880d681SAndroid Build Coastguard Worker }
63*9880d681SAndroid Build Coastguard Worker
translateBinaryOp(unsigned Opcode,const Instruction & Inst)64*9880d681SAndroid Build Coastguard Worker bool IRTranslator::translateBinaryOp(unsigned Opcode, const Instruction &Inst) {
65*9880d681SAndroid Build Coastguard Worker // Get or create a virtual register for each value.
66*9880d681SAndroid Build Coastguard Worker // Unless the value is a Constant => loadimm cst?
67*9880d681SAndroid Build Coastguard Worker // or inline constant each time?
68*9880d681SAndroid Build Coastguard Worker // Creation of a virtual register needs to have a size.
69*9880d681SAndroid Build Coastguard Worker unsigned Op0 = getOrCreateVReg(*Inst.getOperand(0));
70*9880d681SAndroid Build Coastguard Worker unsigned Op1 = getOrCreateVReg(*Inst.getOperand(1));
71*9880d681SAndroid Build Coastguard Worker unsigned Res = getOrCreateVReg(Inst);
72*9880d681SAndroid Build Coastguard Worker MIRBuilder.buildInstr(Opcode, Inst.getType(), Res, Op0, Op1);
73*9880d681SAndroid Build Coastguard Worker return true;
74*9880d681SAndroid Build Coastguard Worker }
75*9880d681SAndroid Build Coastguard Worker
translateReturn(const Instruction & Inst)76*9880d681SAndroid Build Coastguard Worker bool IRTranslator::translateReturn(const Instruction &Inst) {
77*9880d681SAndroid Build Coastguard Worker assert(isa<ReturnInst>(Inst) && "Return expected");
78*9880d681SAndroid Build Coastguard Worker const Value *Ret = cast<ReturnInst>(Inst).getReturnValue();
79*9880d681SAndroid Build Coastguard Worker // The target may mess up with the insertion point, but
80*9880d681SAndroid Build Coastguard Worker // this is not important as a return is the last instruction
81*9880d681SAndroid Build Coastguard Worker // of the block anyway.
82*9880d681SAndroid Build Coastguard Worker return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret));
83*9880d681SAndroid Build Coastguard Worker }
84*9880d681SAndroid Build Coastguard Worker
translateBr(const Instruction & Inst)85*9880d681SAndroid Build Coastguard Worker bool IRTranslator::translateBr(const Instruction &Inst) {
86*9880d681SAndroid Build Coastguard Worker assert(isa<BranchInst>(Inst) && "Branch expected");
87*9880d681SAndroid Build Coastguard Worker const BranchInst &BrInst = *cast<BranchInst>(&Inst);
88*9880d681SAndroid Build Coastguard Worker if (BrInst.isUnconditional()) {
89*9880d681SAndroid Build Coastguard Worker const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getOperand(0));
90*9880d681SAndroid Build Coastguard Worker MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt);
91*9880d681SAndroid Build Coastguard Worker MIRBuilder.buildInstr(TargetOpcode::G_BR, BrTgt.getType(), TgtBB);
92*9880d681SAndroid Build Coastguard Worker } else {
93*9880d681SAndroid Build Coastguard Worker assert(0 && "Not yet implemented");
94*9880d681SAndroid Build Coastguard Worker }
95*9880d681SAndroid Build Coastguard Worker // Link successors.
96*9880d681SAndroid Build Coastguard Worker MachineBasicBlock &CurBB = MIRBuilder.getMBB();
97*9880d681SAndroid Build Coastguard Worker for (const BasicBlock *Succ : BrInst.successors())
98*9880d681SAndroid Build Coastguard Worker CurBB.addSuccessor(&getOrCreateBB(*Succ));
99*9880d681SAndroid Build Coastguard Worker return true;
100*9880d681SAndroid Build Coastguard Worker }
101*9880d681SAndroid Build Coastguard Worker
translate(const Instruction & Inst)102*9880d681SAndroid Build Coastguard Worker bool IRTranslator::translate(const Instruction &Inst) {
103*9880d681SAndroid Build Coastguard Worker MIRBuilder.setDebugLoc(Inst.getDebugLoc());
104*9880d681SAndroid Build Coastguard Worker switch(Inst.getOpcode()) {
105*9880d681SAndroid Build Coastguard Worker case Instruction::Add:
106*9880d681SAndroid Build Coastguard Worker return translateBinaryOp(TargetOpcode::G_ADD, Inst);
107*9880d681SAndroid Build Coastguard Worker case Instruction::Or:
108*9880d681SAndroid Build Coastguard Worker return translateBinaryOp(TargetOpcode::G_OR, Inst);
109*9880d681SAndroid Build Coastguard Worker case Instruction::Br:
110*9880d681SAndroid Build Coastguard Worker return translateBr(Inst);
111*9880d681SAndroid Build Coastguard Worker case Instruction::Ret:
112*9880d681SAndroid Build Coastguard Worker return translateReturn(Inst);
113*9880d681SAndroid Build Coastguard Worker
114*9880d681SAndroid Build Coastguard Worker default:
115*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Opcode not supported");
116*9880d681SAndroid Build Coastguard Worker }
117*9880d681SAndroid Build Coastguard Worker }
118*9880d681SAndroid Build Coastguard Worker
119*9880d681SAndroid Build Coastguard Worker
finalize()120*9880d681SAndroid Build Coastguard Worker void IRTranslator::finalize() {
121*9880d681SAndroid Build Coastguard Worker // Release the memory used by the different maps we
122*9880d681SAndroid Build Coastguard Worker // needed during the translation.
123*9880d681SAndroid Build Coastguard Worker ValToVReg.clear();
124*9880d681SAndroid Build Coastguard Worker Constants.clear();
125*9880d681SAndroid Build Coastguard Worker }
126*9880d681SAndroid Build Coastguard Worker
runOnMachineFunction(MachineFunction & MF)127*9880d681SAndroid Build Coastguard Worker bool IRTranslator::runOnMachineFunction(MachineFunction &MF) {
128*9880d681SAndroid Build Coastguard Worker const Function &F = *MF.getFunction();
129*9880d681SAndroid Build Coastguard Worker if (F.empty())
130*9880d681SAndroid Build Coastguard Worker return false;
131*9880d681SAndroid Build Coastguard Worker CLI = MF.getSubtarget().getCallLowering();
132*9880d681SAndroid Build Coastguard Worker MIRBuilder.setMF(MF);
133*9880d681SAndroid Build Coastguard Worker MRI = &MF.getRegInfo();
134*9880d681SAndroid Build Coastguard Worker // Setup the arguments.
135*9880d681SAndroid Build Coastguard Worker MachineBasicBlock &MBB = getOrCreateBB(F.front());
136*9880d681SAndroid Build Coastguard Worker MIRBuilder.setMBB(MBB);
137*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 8> VRegArgs;
138*9880d681SAndroid Build Coastguard Worker for (const Argument &Arg: F.args())
139*9880d681SAndroid Build Coastguard Worker VRegArgs.push_back(getOrCreateVReg(Arg));
140*9880d681SAndroid Build Coastguard Worker bool Succeeded =
141*9880d681SAndroid Build Coastguard Worker CLI->lowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs);
142*9880d681SAndroid Build Coastguard Worker if (!Succeeded)
143*9880d681SAndroid Build Coastguard Worker report_fatal_error("Unable to lower arguments");
144*9880d681SAndroid Build Coastguard Worker
145*9880d681SAndroid Build Coastguard Worker for (const BasicBlock &BB: F) {
146*9880d681SAndroid Build Coastguard Worker MachineBasicBlock &MBB = getOrCreateBB(BB);
147*9880d681SAndroid Build Coastguard Worker // Set the insertion point of all the following translations to
148*9880d681SAndroid Build Coastguard Worker // the end of this basic block.
149*9880d681SAndroid Build Coastguard Worker MIRBuilder.setMBB(MBB);
150*9880d681SAndroid Build Coastguard Worker for (const Instruction &Inst: BB) {
151*9880d681SAndroid Build Coastguard Worker bool Succeeded = translate(Inst);
152*9880d681SAndroid Build Coastguard Worker if (!Succeeded) {
153*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Cannot translate: " << Inst << '\n');
154*9880d681SAndroid Build Coastguard Worker report_fatal_error("Unable to translate instruction");
155*9880d681SAndroid Build Coastguard Worker }
156*9880d681SAndroid Build Coastguard Worker }
157*9880d681SAndroid Build Coastguard Worker }
158*9880d681SAndroid Build Coastguard Worker
159*9880d681SAndroid Build Coastguard Worker // Now that the MachineFrameInfo has been configured, no further changes to
160*9880d681SAndroid Build Coastguard Worker // the reserved registers are possible.
161*9880d681SAndroid Build Coastguard Worker MRI->freezeReservedRegs(MF);
162*9880d681SAndroid Build Coastguard Worker
163*9880d681SAndroid Build Coastguard Worker return false;
164*9880d681SAndroid Build Coastguard Worker }
165