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