Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 1 | //===-- 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 Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame^] | 15 | #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFunction.h" |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame^] | 17 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 18 | #include "llvm/IR/Constant.h" |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Function.h" |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame^] | 20 | #include "llvm/IR/Type.h" |
| 21 | #include "llvm/IR/Value.h" |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 22 | |
| 23 | #define DEBUG_TYPE "irtranslator" |
| 24 | |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
| 27 | char IRTranslator::ID = 0; |
| 28 | |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame^] | 29 | const VRegsSequence &IRTranslator::getOrCreateVRegs(const Value *Val) { |
| 30 | VRegsSequence &ValRegSequence = ValToVRegs[Val]; |
| 31 | // Check if this is the first time we see Val. |
| 32 | if (ValRegSequence.empty()) { |
| 33 | // Fill ValRegsSequence with the sequence of registers |
| 34 | // we need to concat together to produce the value. |
| 35 | assert(Val->getType()->isSized() && |
| 36 | "Don't know how to create an empty vreg"); |
| 37 | assert(!Val->getType()->isAggregateType() && "Not yet implemented"); |
| 38 | unsigned Size = Val->getType()->getPrimitiveSizeInBits(); |
| 39 | unsigned VReg = MRI->createGenericVirtualRegister(Size); |
| 40 | ValRegSequence.push_back(VReg); |
| 41 | assert(isa<Constant>(Val) && "Not yet implemented"); |
| 42 | } |
| 43 | assert(ValRegSequence.size() == 1 && |
| 44 | "We support only one vreg per value at the moment"); |
| 45 | return ValRegSequence; |
| 46 | } |
| 47 | |
| 48 | MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock *BB) { |
| 49 | MachineBasicBlock *&MBB = BBToMBB[BB]; |
| 50 | if (!MBB) { |
| 51 | MachineFunction &MF = MIRBuilder->getMF(); |
| 52 | MBB = MF.CreateMachineBasicBlock(); |
| 53 | MF.push_back(MBB); |
| 54 | } |
| 55 | return *MBB; |
| 56 | } |
| 57 | |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 58 | bool IRTranslator::translateADD(const Instruction &Inst) { |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 59 | // Get or create a virtual register for each value. |
| 60 | // Unless the value is a Constant => loadimm cst? |
| 61 | // or inline constant each time? |
| 62 | // Creation of a virtual register needs to have a size. |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame^] | 63 | unsigned Op0 = *getOrCreateVRegs(Inst.getOperand(0)).begin(); |
| 64 | unsigned Op1 = *getOrCreateVRegs(Inst.getOperand(1)).begin(); |
| 65 | unsigned Res = *getOrCreateVRegs(&Inst).begin(); |
| 66 | MIRBuilder->buildInstr(TargetOpcode::G_ADD, Res, Op0, Op1); |
| 67 | return true; |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 70 | bool IRTranslator::translate(const Instruction &Inst) { |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame^] | 71 | MIRBuilder->setDebugLoc(Inst.getDebugLoc()); |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 72 | switch(Inst.getOpcode()) { |
| 73 | case Instruction::Add: { |
| 74 | return translateADD(Inst); |
| 75 | default: |
| 76 | llvm_unreachable("Opcode not supported"); |
| 77 | } |
| 78 | } |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | |
| 82 | void IRTranslator::finalize() { |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 83 | // Release the memory used by the different maps we |
| 84 | // needed during the translation. |
| 85 | ValToVRegs.clear(); |
| 86 | Constants.clear(); |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | IRTranslator::IRTranslator() |
| 90 | : MachineFunctionPass(ID) { |
| 91 | } |
| 92 | |
| 93 | bool IRTranslator::runOnMachineFunction(MachineFunction &MF) { |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 94 | const Function &F = *MF.getFunction(); |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame^] | 95 | MIRBuilder->setFunction(MF); |
| 96 | MRI = &MF.getRegInfo(); |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 97 | for (const BasicBlock &BB: F) { |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame^] | 98 | MachineBasicBlock &MBB = getOrCreateBB(&BB); |
| 99 | MIRBuilder->setBasicBlock(MBB); |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 100 | for (const Instruction &Inst: BB) { |
| 101 | bool Succeeded = translate(Inst); |
| 102 | if (!Succeeded) { |
| 103 | DEBUG(dbgs() << "Cannot translate: " << Inst << '\n'); |
| 104 | report_fatal_error("Unable to translate instruction"); |
| 105 | } |
| 106 | } |
| 107 | } |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 108 | return false; |
| 109 | } |