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" |
Tim Northover | bd50546 | 2016-07-22 16:59:52 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 20 | #include "llvm/IR/Constant.h" |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Function.h" |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Type.h" |
| 23 | #include "llvm/IR/Value.h" |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetLowering.h" |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 25 | |
| 26 | #define DEBUG_TYPE "irtranslator" |
| 27 | |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
| 30 | char IRTranslator::ID = 0; |
Quentin Colombet | 39293d3 | 2016-03-08 01:38:55 +0000 | [diff] [blame] | 31 | INITIALIZE_PASS(IRTranslator, "irtranslator", "IRTranslator LLVM IR -> MI", |
Tim Northover | 884b47e | 2016-07-26 03:29:18 +0000 | [diff] [blame] | 32 | false, false) |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 33 | |
Quentin Colombet | a7fae16 | 2016-02-11 17:53:23 +0000 | [diff] [blame] | 34 | IRTranslator::IRTranslator() : MachineFunctionPass(ID), MRI(nullptr) { |
Quentin Colombet | 39293d3 | 2016-03-08 01:38:55 +0000 | [diff] [blame] | 35 | initializeIRTranslatorPass(*PassRegistry::getPassRegistry()); |
Quentin Colombet | a7fae16 | 2016-02-11 17:53:23 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Quentin Colombet | e225e25 | 2016-03-11 17:27:54 +0000 | [diff] [blame] | 38 | unsigned IRTranslator::getOrCreateVReg(const Value &Val) { |
| 39 | unsigned &ValReg = ValToVReg[&Val]; |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 40 | // Check if this is the first time we see Val. |
Quentin Colombet | ccd7725 | 2016-02-11 21:48:32 +0000 | [diff] [blame] | 41 | if (!ValReg) { |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 42 | // Fill ValRegsSequence with the sequence of registers |
| 43 | // we need to concat together to produce the value. |
Quentin Colombet | e225e25 | 2016-03-11 17:27:54 +0000 | [diff] [blame] | 44 | assert(Val.getType()->isSized() && |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 45 | "Don't know how to create an empty vreg"); |
Quentin Colombet | e225e25 | 2016-03-11 17:27:54 +0000 | [diff] [blame] | 46 | assert(!Val.getType()->isAggregateType() && "Not yet implemented"); |
Tim Northover | bd50546 | 2016-07-22 16:59:52 +0000 | [diff] [blame] | 47 | unsigned Size = DL->getTypeSizeInBits(Val.getType()); |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 48 | unsigned VReg = MRI->createGenericVirtualRegister(Size); |
Quentin Colombet | ccd7725 | 2016-02-11 21:48:32 +0000 | [diff] [blame] | 49 | ValReg = VReg; |
Quentin Colombet | 4f0ec8d | 2016-02-11 17:52:28 +0000 | [diff] [blame] | 50 | assert(!isa<Constant>(Val) && "Not yet implemented"); |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 51 | } |
Quentin Colombet | ccd7725 | 2016-02-11 21:48:32 +0000 | [diff] [blame] | 52 | return ValReg; |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Quentin Colombet | 53237a9 | 2016-03-11 17:27:43 +0000 | [diff] [blame] | 55 | MachineBasicBlock &IRTranslator::getOrCreateBB(const BasicBlock &BB) { |
| 56 | MachineBasicBlock *&MBB = BBToMBB[&BB]; |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 57 | if (!MBB) { |
Quentin Colombet | a7fae16 | 2016-02-11 17:53:23 +0000 | [diff] [blame] | 58 | MachineFunction &MF = MIRBuilder.getMF(); |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 59 | MBB = MF.CreateMachineBasicBlock(); |
| 60 | MF.push_back(MBB); |
| 61 | } |
| 62 | return *MBB; |
| 63 | } |
| 64 | |
Quentin Colombet | 13c55e0 | 2016-06-10 20:50:18 +0000 | [diff] [blame] | 65 | bool IRTranslator::translateBinaryOp(unsigned Opcode, const Instruction &Inst) { |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 66 | // Get or create a virtual register for each value. |
| 67 | // Unless the value is a Constant => loadimm cst? |
| 68 | // or inline constant each time? |
| 69 | // Creation of a virtual register needs to have a size. |
Quentin Colombet | e225e25 | 2016-03-11 17:27:54 +0000 | [diff] [blame] | 70 | unsigned Op0 = getOrCreateVReg(*Inst.getOperand(0)); |
| 71 | unsigned Op1 = getOrCreateVReg(*Inst.getOperand(1)); |
| 72 | unsigned Res = getOrCreateVReg(Inst); |
Tim Northover | 62ae568 | 2016-07-20 19:09:30 +0000 | [diff] [blame] | 73 | MIRBuilder.buildInstr(Opcode, LLT{*Inst.getType()}, Res, Op0, Op1); |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 74 | return true; |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 77 | bool IRTranslator::translateReturn(const Instruction &Inst) { |
| 78 | assert(isa<ReturnInst>(Inst) && "Return expected"); |
| 79 | const Value *Ret = cast<ReturnInst>(Inst).getReturnValue(); |
| 80 | // The target may mess up with the insertion point, but |
| 81 | // this is not important as a return is the last instruction |
| 82 | // of the block anyway. |
Tom Stellard | b72a65f | 2016-04-14 17:23:33 +0000 | [diff] [blame] | 83 | return CLI->lowerReturn(MIRBuilder, Ret, !Ret ? 0 : getOrCreateVReg(*Ret)); |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Quentin Colombet | dd4b137 | 2016-03-11 17:28:03 +0000 | [diff] [blame] | 86 | bool IRTranslator::translateBr(const Instruction &Inst) { |
| 87 | assert(isa<BranchInst>(Inst) && "Branch expected"); |
| 88 | const BranchInst &BrInst = *cast<BranchInst>(&Inst); |
| 89 | if (BrInst.isUnconditional()) { |
| 90 | const BasicBlock &BrTgt = *cast<BasicBlock>(BrInst.getOperand(0)); |
| 91 | MachineBasicBlock &TgtBB = getOrCreateBB(BrTgt); |
Tim Northover | 62ae568 | 2016-07-20 19:09:30 +0000 | [diff] [blame] | 92 | MIRBuilder.buildInstr(TargetOpcode::G_BR, LLT{*BrTgt.getType()}, TgtBB); |
Quentin Colombet | dd4b137 | 2016-03-11 17:28:03 +0000 | [diff] [blame] | 93 | } else { |
| 94 | assert(0 && "Not yet implemented"); |
| 95 | } |
| 96 | // Link successors. |
| 97 | MachineBasicBlock &CurBB = MIRBuilder.getMBB(); |
| 98 | for (const BasicBlock *Succ : BrInst.successors()) |
| 99 | CurBB.addSuccessor(&getOrCreateBB(*Succ)); |
| 100 | return true; |
| 101 | } |
| 102 | |
Tim Northover | 7c9eba9 | 2016-07-25 21:01:29 +0000 | [diff] [blame] | 103 | bool IRTranslator::translateBitCast(const CastInst &CI) { |
| 104 | if (LLT{*CI.getDestTy()} == LLT{*CI.getSrcTy()}) { |
| 105 | MIRBuilder.buildInstr(TargetOpcode::COPY, getOrCreateVReg(CI), |
| 106 | getOrCreateVReg(*CI.getOperand(0))); |
| 107 | return true; |
| 108 | } |
| 109 | return translateCast(TargetOpcode::G_BITCAST, CI); |
| 110 | } |
| 111 | |
| 112 | bool IRTranslator::translateCast(unsigned Opcode, const CastInst &CI) { |
| 113 | unsigned Op = getOrCreateVReg(*CI.getOperand(0)); |
| 114 | unsigned Res = getOrCreateVReg(CI); |
| 115 | MIRBuilder.buildInstr(Opcode, {LLT{*CI.getDestTy()}, LLT{*CI.getSrcTy()}}, |
| 116 | Res, Op); |
| 117 | return true; |
| 118 | } |
| 119 | |
Tim Northover | bd50546 | 2016-07-22 16:59:52 +0000 | [diff] [blame] | 120 | bool IRTranslator::translateStaticAlloca(const AllocaInst &AI) { |
| 121 | assert(AI.isStaticAlloca() && "only handle static allocas now"); |
| 122 | MachineFunction &MF = MIRBuilder.getMF(); |
| 123 | unsigned ElementSize = DL->getTypeStoreSize(AI.getAllocatedType()); |
| 124 | unsigned Size = |
| 125 | ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue(); |
| 126 | |
| 127 | unsigned Alignment = AI.getAlignment(); |
| 128 | if (!Alignment) |
| 129 | Alignment = DL->getABITypeAlignment(AI.getAllocatedType()); |
| 130 | |
| 131 | unsigned Res = getOrCreateVReg(AI); |
| 132 | int FI = MF.getFrameInfo()->CreateStackObject(Size, Alignment, false, &AI); |
| 133 | MIRBuilder.buildFrameIndex(LLT::pointer(0), Res, FI); |
| 134 | return true; |
| 135 | } |
| 136 | |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 137 | bool IRTranslator::translate(const Instruction &Inst) { |
Quentin Colombet | a7fae16 | 2016-02-11 17:53:23 +0000 | [diff] [blame] | 138 | MIRBuilder.setDebugLoc(Inst.getDebugLoc()); |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 139 | switch(Inst.getOpcode()) { |
Quentin Colombet | 19df8a1 | 2016-07-21 17:26:41 +0000 | [diff] [blame] | 140 | // Arithmetic operations. |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 141 | case Instruction::Add: |
Quentin Colombet | 13c55e0 | 2016-06-10 20:50:18 +0000 | [diff] [blame] | 142 | return translateBinaryOp(TargetOpcode::G_ADD, Inst); |
Quentin Colombet | 2b59eab | 2016-07-21 17:26:50 +0000 | [diff] [blame] | 143 | case Instruction::Sub: |
| 144 | return translateBinaryOp(TargetOpcode::G_SUB, Inst); |
Tim Northover | bd50546 | 2016-07-22 16:59:52 +0000 | [diff] [blame] | 145 | |
Quentin Colombet | 19df8a1 | 2016-07-21 17:26:41 +0000 | [diff] [blame] | 146 | // Bitwise operations. |
Quentin Colombet | 7bcc921 | 2016-07-21 15:50:42 +0000 | [diff] [blame] | 147 | case Instruction::And: |
| 148 | return translateBinaryOp(TargetOpcode::G_AND, Inst); |
Quentin Colombet | f2a1909 | 2016-06-10 20:50:35 +0000 | [diff] [blame] | 149 | case Instruction::Or: |
| 150 | return translateBinaryOp(TargetOpcode::G_OR, Inst); |
Tim Northover | bd50546 | 2016-07-22 16:59:52 +0000 | [diff] [blame] | 151 | |
Quentin Colombet | 19df8a1 | 2016-07-21 17:26:41 +0000 | [diff] [blame] | 152 | // Branch operations. |
Quentin Colombet | dd4b137 | 2016-03-11 17:28:03 +0000 | [diff] [blame] | 153 | case Instruction::Br: |
| 154 | return translateBr(Inst); |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 155 | case Instruction::Ret: |
| 156 | return translateReturn(Inst); |
| 157 | |
Tim Northover | 7c9eba9 | 2016-07-25 21:01:29 +0000 | [diff] [blame] | 158 | // Casts |
| 159 | case Instruction::BitCast: |
| 160 | return translateBitCast(cast<CastInst>(Inst)); |
| 161 | case Instruction::IntToPtr: |
| 162 | return translateCast(TargetOpcode::G_INTTOPTR, cast<CastInst>(Inst)); |
| 163 | case Instruction::PtrToInt: |
| 164 | return translateCast(TargetOpcode::G_PTRTOINT, cast<CastInst>(Inst)); |
| 165 | |
Tim Northover | bd50546 | 2016-07-22 16:59:52 +0000 | [diff] [blame] | 166 | case Instruction::Alloca: |
| 167 | return translateStaticAlloca(cast<AllocaInst>(Inst)); |
| 168 | |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 169 | default: |
| 170 | llvm_unreachable("Opcode not supported"); |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 171 | } |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | |
| 175 | void IRTranslator::finalize() { |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 176 | // Release the memory used by the different maps we |
| 177 | // needed during the translation. |
Quentin Colombet | ccd7725 | 2016-02-11 21:48:32 +0000 | [diff] [blame] | 178 | ValToVReg.clear(); |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 179 | Constants.clear(); |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 182 | bool IRTranslator::runOnMachineFunction(MachineFunction &MF) { |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 183 | const Function &F = *MF.getFunction(); |
Quentin Colombet | fd9d0a0 | 2016-02-11 19:59:41 +0000 | [diff] [blame] | 184 | if (F.empty()) |
| 185 | return false; |
Quentin Colombet | ba2a016 | 2016-02-16 19:26:02 +0000 | [diff] [blame] | 186 | CLI = MF.getSubtarget().getCallLowering(); |
Quentin Colombet | 000b580 | 2016-03-11 17:27:51 +0000 | [diff] [blame] | 187 | MIRBuilder.setMF(MF); |
Quentin Colombet | 17c494b | 2016-02-11 17:51:31 +0000 | [diff] [blame] | 188 | MRI = &MF.getRegInfo(); |
Tim Northover | bd50546 | 2016-07-22 16:59:52 +0000 | [diff] [blame] | 189 | DL = &F.getParent()->getDataLayout(); |
| 190 | |
Quentin Colombet | fd9d0a0 | 2016-02-11 19:59:41 +0000 | [diff] [blame] | 191 | // Setup the arguments. |
Quentin Colombet | 53237a9 | 2016-03-11 17:27:43 +0000 | [diff] [blame] | 192 | MachineBasicBlock &MBB = getOrCreateBB(F.front()); |
Quentin Colombet | 91ebd71 | 2016-03-11 17:27:47 +0000 | [diff] [blame] | 193 | MIRBuilder.setMBB(MBB); |
Quentin Colombet | fd9d0a0 | 2016-02-11 19:59:41 +0000 | [diff] [blame] | 194 | SmallVector<unsigned, 8> VRegArgs; |
| 195 | for (const Argument &Arg: F.args()) |
Quentin Colombet | e225e25 | 2016-03-11 17:27:54 +0000 | [diff] [blame] | 196 | VRegArgs.push_back(getOrCreateVReg(Arg)); |
Quentin Colombet | ba2a016 | 2016-02-16 19:26:02 +0000 | [diff] [blame] | 197 | bool Succeeded = |
Tom Stellard | b72a65f | 2016-04-14 17:23:33 +0000 | [diff] [blame] | 198 | CLI->lowerFormalArguments(MIRBuilder, F.getArgumentList(), VRegArgs); |
Quentin Colombet | fd9d0a0 | 2016-02-11 19:59:41 +0000 | [diff] [blame] | 199 | if (!Succeeded) |
| 200 | report_fatal_error("Unable to lower arguments"); |
| 201 | |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 202 | for (const BasicBlock &BB: F) { |
Quentin Colombet | 53237a9 | 2016-03-11 17:27:43 +0000 | [diff] [blame] | 203 | MachineBasicBlock &MBB = getOrCreateBB(BB); |
Quentin Colombet | 91ebd71 | 2016-03-11 17:27:47 +0000 | [diff] [blame] | 204 | // Set the insertion point of all the following translations to |
| 205 | // the end of this basic block. |
| 206 | MIRBuilder.setMBB(MBB); |
Quentin Colombet | 2ecff3b | 2016-02-10 22:59:27 +0000 | [diff] [blame] | 207 | for (const Instruction &Inst: BB) { |
| 208 | bool Succeeded = translate(Inst); |
| 209 | if (!Succeeded) { |
| 210 | DEBUG(dbgs() << "Cannot translate: " << Inst << '\n'); |
| 211 | report_fatal_error("Unable to translate instruction"); |
| 212 | } |
| 213 | } |
| 214 | } |
Tim Northover | 72eebfa | 2016-07-12 22:23:42 +0000 | [diff] [blame] | 215 | |
| 216 | // Now that the MachineFrameInfo has been configured, no further changes to |
| 217 | // the reserved registers are possible. |
| 218 | MRI->freezeReservedRegs(MF); |
| 219 | |
Quentin Colombet | 105cf2b | 2016-01-20 20:58:56 +0000 | [diff] [blame] | 220 | return false; |
| 221 | } |