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 | fd9d0a0 | 2016-02-11 19:59:41 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallVector.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 | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetLowering.h" |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 23 | |
| 24 | #define DEBUG_TYPE "irtranslator" |
| 25 | |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
| 28 | char IRTranslator::ID = 0; |
| 29 | |
Quentin Colombet | a7fae16 | 2016-02-11 17:53:23 +0000 | [diff] [blame] | 30 | IRTranslator::IRTranslator() : MachineFunctionPass(ID), MRI(nullptr) { |
| 31 | } |
| 32 | |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 33 | const VRegsSequence &IRTranslator::getOrCreateVRegs(const Value *Val) { |
| 34 | VRegsSequence &ValRegSequence = ValToVRegs[Val]; |
| 35 | // Check if this is the first time we see Val. |
| 36 | if (ValRegSequence.empty()) { |
| 37 | // Fill ValRegsSequence with the sequence of registers |
| 38 | // we need to concat together to produce the value. |
| 39 | assert(Val->getType()->isSized() && |
| 40 | "Don't know how to create an empty vreg"); |
| 41 | assert(!Val->getType()->isAggregateType() && "Not yet implemented"); |
| 42 | unsigned Size = Val->getType()->getPrimitiveSizeInBits(); |
| 43 | unsigned VReg = MRI->createGenericVirtualRegister(Size); |
| 44 | ValRegSequence.push_back(VReg); |
Quentin Colombet | 4f0ec8d | 2016-02-11 17:52:28 +0000 | [diff] [blame] | 45 | assert(!isa<Constant>(Val) && "Not yet implemented"); |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 46 | } |
| 47 | assert(ValRegSequence.size() == 1 && |
| 48 | "We support only one vreg per value at the moment"); |
| 49 | return ValRegSequence; |
| 50 | } |
| 51 | |
| 52 | MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock *BB) { |
| 53 | MachineBasicBlock *&MBB = BBToMBB[BB]; |
| 54 | if (!MBB) { |
Quentin Colombet | a7fae16 | 2016-02-11 17:53:23 +0000 | [diff] [blame] | 55 | MachineFunction &MF = MIRBuilder.getMF(); |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 56 | MBB = MF.CreateMachineBasicBlock(); |
| 57 | MF.push_back(MBB); |
| 58 | } |
| 59 | return *MBB; |
| 60 | } |
| 61 | |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 62 | bool IRTranslator::translateADD(const Instruction &Inst) { |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 63 | // Get or create a virtual register for each value. |
| 64 | // Unless the value is a Constant => loadimm cst? |
| 65 | // or inline constant each time? |
| 66 | // Creation of a virtual register needs to have a size. |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 67 | unsigned Op0 = *getOrCreateVRegs(Inst.getOperand(0)).begin(); |
| 68 | unsigned Op1 = *getOrCreateVRegs(Inst.getOperand(1)).begin(); |
| 69 | unsigned Res = *getOrCreateVRegs(&Inst).begin(); |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 70 | MIRBuilder.buildInstr(TargetOpcode::G_ADD, Inst.getType(), Res, Op0, Op1); |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 71 | return true; |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 74 | bool IRTranslator::translateReturn(const Instruction &Inst) { |
| 75 | assert(isa<ReturnInst>(Inst) && "Return expected"); |
| 76 | const Value *Ret = cast<ReturnInst>(Inst).getReturnValue(); |
| 77 | // The target may mess up with the insertion point, but |
| 78 | // this is not important as a return is the last instruction |
| 79 | // of the block anyway. |
| 80 | return TLI->LowerReturn(MIRBuilder, Ret, |
| 81 | !Ret ? 0 : *getOrCreateVRegs(Ret).begin()); |
| 82 | } |
| 83 | |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 84 | bool IRTranslator::translate(const Instruction &Inst) { |
Quentin Colombet | a7fae16 | 2016-02-11 17:53:23 +0000 | [diff] [blame] | 85 | MIRBuilder.setDebugLoc(Inst.getDebugLoc()); |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 86 | switch(Inst.getOpcode()) { |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 87 | case Instruction::Add: |
| 88 | return translateADD(Inst); |
| 89 | case Instruction::Ret: |
| 90 | return translateReturn(Inst); |
| 91 | |
| 92 | default: |
| 93 | llvm_unreachable("Opcode not supported"); |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 94 | } |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | |
| 98 | void IRTranslator::finalize() { |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 99 | // Release the memory used by the different maps we |
| 100 | // needed during the translation. |
| 101 | ValToVRegs.clear(); |
| 102 | Constants.clear(); |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 105 | bool IRTranslator::runOnMachineFunction(MachineFunction &MF) { |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 106 | const Function &F = *MF.getFunction(); |
Quentin Colombet | fd9d0a0 | 2016-02-11 19:59:41 +0000 | [diff] [blame] | 107 | if (F.empty()) |
| 108 | return false; |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 109 | TLI = MF.getSubtarget().getTargetLowering(); |
Quentin Colombet | a7fae16 | 2016-02-11 17:53:23 +0000 | [diff] [blame] | 110 | MIRBuilder.setFunction(MF); |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 111 | MRI = &MF.getRegInfo(); |
Quentin Colombet | fd9d0a0 | 2016-02-11 19:59:41 +0000 | [diff] [blame] | 112 | // Setup the arguments. |
| 113 | MachineBasicBlock &MBB = getOrCreateBB(&F.front()); |
| 114 | MIRBuilder.setBasicBlock(MBB); |
| 115 | SmallVector<unsigned, 8> VRegArgs; |
| 116 | for (const Argument &Arg: F.args()) |
| 117 | VRegArgs.push_back(*getOrCreateVRegs(&Arg).begin()); |
| 118 | bool Succeeded = TLI->LowerFormalArguments(MIRBuilder, F.getArgumentList(), |
| 119 | VRegArgs); |
| 120 | if (!Succeeded) |
| 121 | report_fatal_error("Unable to lower arguments"); |
| 122 | |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 123 | for (const BasicBlock &BB: F) { |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 124 | MachineBasicBlock &MBB = getOrCreateBB(&BB); |
Quentin Colombet | a7fae16 | 2016-02-11 17:53:23 +0000 | [diff] [blame] | 125 | MIRBuilder.setBasicBlock(MBB); |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 126 | for (const Instruction &Inst: BB) { |
| 127 | bool Succeeded = translate(Inst); |
| 128 | if (!Succeeded) { |
| 129 | DEBUG(dbgs() << "Cannot translate: " << Inst << '\n'); |
| 130 | report_fatal_error("Unable to translate instruction"); |
| 131 | } |
| 132 | } |
| 133 | } |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 134 | return false; |
| 135 | } |