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 | e225e25 | 2016-03-11 17:27:54 +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. |
Quentin Colombet | e225e25 | 2016-03-11 17:27:54 +0000 | [diff] [blame] | 43 | assert(Val.getType()->isSized() && |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 44 | "Don't know how to create an empty vreg"); |
Quentin Colombet | e225e25 | 2016-03-11 17:27:54 +0000 | [diff] [blame] | 45 | assert(!Val.getType()->isAggregateType() && "Not yet implemented"); |
| 46 | unsigned Size = Val.getType()->getPrimitiveSizeInBits(); |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 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 | 13c55e0 | 2016-06-10 20:50:18 +0000 | [diff] [blame] | 64 | bool IRTranslator::translateBinaryOp(unsigned Opcode, 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 | e225e25 | 2016-03-11 17:27:54 +0000 | [diff] [blame] | 69 | unsigned Op0 = getOrCreateVReg(*Inst.getOperand(0)); |
| 70 | unsigned Op1 = getOrCreateVReg(*Inst.getOperand(1)); |
| 71 | unsigned Res = getOrCreateVReg(Inst); |
Tim Northover | 62ae568 | 2016-07-20 19:09:30 +0000 | [diff] [blame] | 72 | MIRBuilder.buildInstr(Opcode, LLT{*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. |
Tom Stellard | b72a65f | 2016-04-14 17:23:33 +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 | dd4b137 | 2016-03-11 17:28:03 +0000 | [diff] [blame] | 85 | bool IRTranslator::translateBr(const Instruction &Inst) { |
| 86 | assert(isa<BranchInst>(Inst) && "Branch expected"); |
| 87 | const BranchInst &BrInst = *cast<BranchInst>(&Inst); |
| 88 | if (BrInst.isUnconditional()) { |
| 89 | const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getOperand(0)); |
| 90 | MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt); |
Tim Northover | 62ae568 | 2016-07-20 19:09:30 +0000 | [diff] [blame] | 91 | MIRBuilder.buildInstr(TargetOpcode::G_BR, LLT{*BrTgt.getType()}, TgtBB); |
Quentin Colombet | dd4b137 | 2016-03-11 17:28:03 +0000 | [diff] [blame] | 92 | } else { |
| 93 | assert(0 && "Not yet implemented"); |
| 94 | } |
| 95 | // Link successors. |
| 96 | MachineBasicBlock &CurBB = MIRBuilder.getMBB(); |
| 97 | for (const BasicBlock *Succ : BrInst.successors()) |
| 98 | CurBB.addSuccessor(&getOrCreateBB(*Succ)); |
| 99 | return true; |
| 100 | } |
| 101 | |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 102 | bool IRTranslator::translate(const Instruction &Inst) { |
Quentin Colombet | a7fae16 | 2016-02-11 17:53:23 +0000 | [diff] [blame] | 103 | MIRBuilder.setDebugLoc(Inst.getDebugLoc()); |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 104 | switch(Inst.getOpcode()) { |
Quentin Colombet | 19df8a1 | 2016-07-21 17:26:41 +0000 | [diff] [blame^] | 105 | // Arithmetic operations. |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 106 | case Instruction::Add: |
Quentin Colombet | 13c55e0 | 2016-06-10 20:50:18 +0000 | [diff] [blame] | 107 | return translateBinaryOp(TargetOpcode::G_ADD, Inst); |
Quentin Colombet | 19df8a1 | 2016-07-21 17:26:41 +0000 | [diff] [blame^] | 108 | // Bitwise operations. |
Quentin Colombet | 7bcc921 | 2016-07-21 15:50:42 +0000 | [diff] [blame] | 109 | case Instruction::And: |
| 110 | return translateBinaryOp(TargetOpcode::G_AND, Inst); |
Quentin Colombet | f2a1909 | 2016-06-10 20:50:35 +0000 | [diff] [blame] | 111 | case Instruction::Or: |
| 112 | return translateBinaryOp(TargetOpcode::G_OR, Inst); |
Quentin Colombet | 19df8a1 | 2016-07-21 17:26:41 +0000 | [diff] [blame^] | 113 | // Branch operations. |
Quentin Colombet | dd4b137 | 2016-03-11 17:28:03 +0000 | [diff] [blame] | 114 | case Instruction::Br: |
| 115 | return translateBr(Inst); |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 116 | case Instruction::Ret: |
| 117 | return translateReturn(Inst); |
| 118 | |
| 119 | default: |
| 120 | llvm_unreachable("Opcode not supported"); |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 121 | } |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
| 125 | void IRTranslator::finalize() { |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 126 | // Release the memory used by the different maps we |
| 127 | // needed during the translation. |
Quentin Colombet | ccd7725 | 2016-02-11 21:48:32 +0000 | [diff] [blame] | 128 | ValToVReg.clear(); |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 129 | Constants.clear(); |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 132 | bool IRTranslator::runOnMachineFunction(MachineFunction &MF) { |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 133 | const Function &F = *MF.getFunction(); |
Quentin Colombet | fd9d0a0 | 2016-02-11 19:59:41 +0000 | [diff] [blame] | 134 | if (F.empty()) |
| 135 | return false; |
Quentin Colombet | ba2a016 | 2016-02-16 19:26:02 +0000 | [diff] [blame] | 136 | CLI = MF.getSubtarget().getCallLowering(); |
Quentin Colombet | 000b580 | 2016-03-11 17:27:51 +0000 | [diff] [blame] | 137 | MIRBuilder.setMF(MF); |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 138 | MRI = &MF.getRegInfo(); |
Quentin Colombet | fd9d0a0 | 2016-02-11 19:59:41 +0000 | [diff] [blame] | 139 | // Setup the arguments. |
Quentin Colombet | 53237a9 | 2016-03-11 17:27:43 +0000 | [diff] [blame] | 140 | MachineBasicBlock &MBB = getOrCreateBB(F.front()); |
Quentin Colombet | 91ebd71 | 2016-03-11 17:27:47 +0000 | [diff] [blame] | 141 | MIRBuilder.setMBB(MBB); |
Quentin Colombet | fd9d0a0 | 2016-02-11 19:59:41 +0000 | [diff] [blame] | 142 | SmallVector<unsigned, 8> VRegArgs; |
| 143 | for (const Argument &Arg: F.args()) |
Quentin Colombet | e225e25 | 2016-03-11 17:27:54 +0000 | [diff] [blame] | 144 | VRegArgs.push_back(getOrCreateVReg(Arg)); |
Quentin Colombet | ba2a016 | 2016-02-16 19:26:02 +0000 | [diff] [blame] | 145 | bool Succeeded = |
Tom Stellard | b72a65f | 2016-04-14 17:23:33 +0000 | [diff] [blame] | 146 | CLI->lowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs); |
Quentin Colombet | fd9d0a0 | 2016-02-11 19:59:41 +0000 | [diff] [blame] | 147 | if (!Succeeded) |
| 148 | report_fatal_error("Unable to lower arguments"); |
| 149 | |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 150 | for (const BasicBlock &BB: F) { |
Quentin Colombet | 53237a9 | 2016-03-11 17:27:43 +0000 | [diff] [blame] | 151 | MachineBasicBlock &MBB = getOrCreateBB(BB); |
Quentin Colombet | 91ebd71 | 2016-03-11 17:27:47 +0000 | [diff] [blame] | 152 | // Set the insertion point of all the following translations to |
| 153 | // the end of this basic block. |
| 154 | MIRBuilder.setMBB(MBB); |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 155 | for (const Instruction &Inst: BB) { |
| 156 | bool Succeeded = translate(Inst); |
| 157 | if (!Succeeded) { |
| 158 | DEBUG(dbgs() << "Cannot translate: " << Inst << '\n'); |
| 159 | report_fatal_error("Unable to translate instruction"); |
| 160 | } |
| 161 | } |
| 162 | } |
Tim Northover | 72eebfa | 2016-07-12 22:23:42 +0000 | [diff] [blame] | 163 | |
| 164 | // Now that the MachineFrameInfo has been configured, no further changes to |
| 165 | // the reserved registers are possible. |
| 166 | MRI->freezeReservedRegs(MF); |
| 167 | |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 168 | return false; |
| 169 | } |