1//===-- llvm/CodeGen/GlobalISel/IRTranslator.h - 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 declares the IRTranslator pass. 11/// This pass is responsible for translating LLVM IR into MachineInstr. 12/// It uses target hooks to lower the ABI but aside from that, the pass 13/// generated code is generic. This is the default translator used for 14/// GlobalISel. 15/// 16/// \todo Replace the comments with actual doxygen comments. 17//===----------------------------------------------------------------------===// 18 19#ifndef LLVM_CODEGEN_GLOBALISEL_IRTRANSLATOR_H 20#define LLVM_CODEGEN_GLOBALISEL_IRTRANSLATOR_H 21 22#include "Types.h" 23#include "llvm/ADT/DenseMap.h" 24#include "llvm/ADT/SetVector.h" 25#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" 26#include "llvm/CodeGen/MachineFunctionPass.h" 27 28namespace llvm { 29// Forward declarations. 30class BasicBlock; 31class CallLowering; 32class Constant; 33class Instruction; 34class MachineBasicBlock; 35class MachineFunction; 36class MachineInstr; 37class MachineRegisterInfo; 38 39// Technically the pass should run on an hypothetical MachineModule, 40// since it should translate Global into some sort of MachineGlobal. 41// The MachineGlobal should ultimately just be a transfer of ownership of 42// the interesting bits that are relevant to represent a global value. 43// That being said, we could investigate what would it cost to just duplicate 44// the information from the LLVM IR. 45// The idea is that ultimately we would be able to free up the memory used 46// by the LLVM IR as soon as the translation is over. 47class IRTranslator : public MachineFunctionPass { 48public: 49 static char ID; 50 51private: 52 /// Interface used to lower the everything related to calls. 53 const CallLowering *CLI; 54 /// Mapping of the values of the current LLVM IR function 55 /// to the related virtual registers. 56 ValueToVReg ValToVReg; 57 // Constants are special because when we encounter one, 58 // we do not know at first where to insert the definition since 59 // this depends on all its uses. 60 // Thus, we will insert the sequences to materialize them when 61 // we know all their users. 62 // In the meantime, just keep it in a set. 63 // Note: Constants that end up as immediate in the related instructions, 64 // do not appear in that map. 65 SmallSetVector<const Constant *, 8> Constants; 66 67 DenseMap<const BasicBlock *, MachineBasicBlock *> BBToMBB; 68 69 /// Methods for translating form LLVM IR to MachineInstr. 70 /// \see ::translate for general information on the translate methods. 71 /// @{ 72 73 /// Translate \p Inst into its corresponding MachineInstr instruction(s). 74 /// Insert the newly translated instruction(s) right where the MIRBuilder 75 /// is set. 76 /// 77 /// The general algorithm is: 78 /// 1. Look for a virtual register for each operand or 79 /// create one. 80 /// 2 Update the ValToVReg accordingly. 81 /// 2.alt. For constant arguments, if they are compile time constants, 82 /// produce an immediate in the right operand and do not touch 83 /// ValToReg. Actually we will go with a virtual register for each 84 /// constants because it may be expensive to actually materialize the 85 /// constant. Moreover, if the constant spans on several instructions, 86 /// CSE may not catch them. 87 /// => Update ValToVReg and remember that we saw a constant in Constants. 88 /// We will materialize all the constants in finalize. 89 /// Note: we would need to do something so that we can recognize such operand 90 /// as constants. 91 /// 3. Create the generic instruction. 92 /// 93 /// \return true if the translation succeeded. 94 bool translate(const Instruction &Inst); 95 96 /// Translate \p Inst into a binary operation \p Opcode. 97 /// \pre \p Inst is a binary operation. 98 bool translateBinaryOp(unsigned Opcode, const Instruction &Inst); 99 100 /// Translate branch (br) instruction. 101 /// \pre \p Inst is a branch instruction. 102 bool translateBr(const Instruction &Inst); 103 104 /// Translate return (ret) instruction. 105 /// The target needs to implement CallLowering::lowerReturn for 106 /// this to succeed. 107 /// \pre \p Inst is a return instruction. 108 bool translateReturn(const Instruction &Inst); 109 /// @} 110 111 // Builder for machine instruction a la IRBuilder. 112 // I.e., compared to regular MIBuilder, this one also inserts the instruction 113 // in the current block, it can creates block, etc., basically a kind of 114 // IRBuilder, but for Machine IR. 115 MachineIRBuilder MIRBuilder; 116 117 /// MachineRegisterInfo used to create virtual registers. 118 MachineRegisterInfo *MRI; 119 120 // * Insert all the code needed to materialize the constants 121 // at the proper place. E.g., Entry block or dominator block 122 // of each constant depending on how fancy we want to be. 123 // * Clear the different maps. 124 void finalize(); 125 126 /// Get the VReg that represents \p Val. 127 /// If such VReg does not exist, it is created. 128 unsigned getOrCreateVReg(const Value &Val); 129 130 /// Get the MachineBasicBlock that represents \p BB. 131 /// If such basic block does not exist, it is created. 132 MachineBasicBlock &getOrCreateBB(const BasicBlock &BB); 133 134public: 135 // Ctor, nothing fancy. 136 IRTranslator(); 137 138 const char *getPassName() const override { 139 return "IRTranslator"; 140 } 141 142 // Algo: 143 // CallLowering = MF.subtarget.getCallLowering() 144 // F = MF.getParent() 145 // MIRBuilder.reset(MF) 146 // MIRBuilder.getOrCreateBB(F.getEntryBB()) 147 // CallLowering->translateArguments(MIRBuilder, F, ValToVReg) 148 // for each bb in F 149 // MIRBuilder.getOrCreateBB(bb) 150 // for each inst in bb 151 // if (!translate(MIRBuilder, inst, ValToVReg, ConstantToSequence)) 152 // report_fatal_error(“Don’t know how to translate input"); 153 // finalize() 154 bool runOnMachineFunction(MachineFunction &MF) override; 155}; 156 157} // End namespace llvm. 158#endif 159