Home | History | Annotate | Download | only in GlobalISel
      1 //===-- llvm/CodeGen/GlobalISel/CallLowering.h - Call lowering --*- 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 ///
     10 /// \file
     11 /// This file describes how to lower LLVM calls to machine code calls.
     12 ///
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_CODEGEN_GLOBALISEL_CALLLOWERING_H
     16 #define LLVM_CODEGEN_GLOBALISEL_CALLLOWERING_H
     17 
     18 #include "llvm/ADT/SmallVector.h"
     19 #include "llvm/CodeGen/CallingConvLower.h"
     20 #include "llvm/CodeGen/ValueTypes.h"
     21 #include "llvm/IR/Function.h"
     22 #include "llvm/Target/TargetCallingConv.h"
     23 
     24 namespace llvm {
     25 // Forward declarations.
     26 class MachineIRBuilder;
     27 class MachineOperand;
     28 class TargetLowering;
     29 class Value;
     30 
     31 class CallLowering {
     32   const TargetLowering *TLI;
     33 public:
     34   struct ArgInfo {
     35     unsigned Reg;
     36     Type *Ty;
     37     ISD::ArgFlagsTy Flags;
     38     bool IsFixed;
     39 
     40     ArgInfo(unsigned Reg, Type *Ty, ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy{},
     41             bool IsFixed = true)
     42         : Reg(Reg), Ty(Ty), Flags(Flags), IsFixed(IsFixed) {}
     43   };
     44 
     45   /// Argument handling is mostly uniform between the four places that
     46   /// make these decisions: function formal arguments, call
     47   /// instruction args, call instruction returns and function
     48   /// returns. However, once a decision has been made on where an
     49   /// arugment should go, exactly what happens can vary slightly. This
     50   /// class abstracts the differences.
     51   struct ValueHandler {
     52     /// Materialize a VReg containing the address of the specified
     53     /// stack-based object. This is either based on a FrameIndex or
     54     /// direct SP manipulation, depending on the context. \p MPO
     55     /// should be initialized to an appropriate description of the
     56     /// address created.
     57     virtual unsigned getStackAddress(uint64_t Size, int64_t Offset,
     58                                      MachinePointerInfo &MPO) = 0;
     59 
     60     /// The specified value has been assigned to a physical register,
     61     /// handle the appropriate COPY (either to or from) and mark any
     62     /// relevant uses/defines as needed.
     63     virtual void assignValueToReg(unsigned ValVReg, unsigned PhysReg,
     64                                   CCValAssign &VA) = 0;
     65 
     66     /// The specified value has been assigned to a stack
     67     /// location. Load or store it there, with appropriate extension
     68     /// if necessary.
     69     virtual void assignValueToAddress(unsigned ValVReg, unsigned Addr,
     70                                       uint64_t Size, MachinePointerInfo &MPO,
     71                                       CCValAssign &VA) = 0;
     72 
     73     /// Handle custom values, which may be passed into one or more of \p VAs.
     74     /// \return The number of \p VAs that have been assigned after the first
     75     ///         one, and which should therefore be skipped from further
     76     ///         processing.
     77     virtual unsigned assignCustomValue(const ArgInfo &Arg,
     78                                        ArrayRef<CCValAssign> VAs) {
     79       // This is not a pure virtual method because not all targets need to worry
     80       // about custom values.
     81       llvm_unreachable("Custom values not supported");
     82     }
     83 
     84     unsigned extendRegister(unsigned ValReg, CCValAssign &VA);
     85 
     86     virtual bool assignArg(unsigned ValNo, MVT ValVT, MVT LocVT,
     87                            CCValAssign::LocInfo LocInfo, const ArgInfo &Info,
     88                            CCState &State) {
     89       return AssignFn(ValNo, ValVT, LocVT, LocInfo, Info.Flags, State);
     90     }
     91 
     92     ValueHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
     93                  CCAssignFn *AssignFn)
     94       : MIRBuilder(MIRBuilder), MRI(MRI), AssignFn(AssignFn) {}
     95 
     96     virtual ~ValueHandler() {}
     97 
     98     MachineIRBuilder &MIRBuilder;
     99     MachineRegisterInfo &MRI;
    100     CCAssignFn *AssignFn;
    101   };
    102 
    103 protected:
    104   /// Getter for generic TargetLowering class.
    105   const TargetLowering *getTLI() const {
    106     return TLI;
    107   }
    108 
    109   /// Getter for target specific TargetLowering class.
    110   template <class XXXTargetLowering>
    111     const XXXTargetLowering *getTLI() const {
    112     return static_cast<const XXXTargetLowering *>(TLI);
    113   }
    114 
    115 
    116   template <typename FuncInfoTy>
    117   void setArgFlags(ArgInfo &Arg, unsigned OpNum, const DataLayout &DL,
    118                    const FuncInfoTy &FuncInfo) const;
    119 
    120   /// Invoke Handler::assignArg on each of the given \p Args and then use
    121   /// \p Callback to move them to the assigned locations.
    122   ///
    123   /// \return True if everything has succeeded, false otherwise.
    124   bool handleAssignments(MachineIRBuilder &MIRBuilder, ArrayRef<ArgInfo> Args,
    125                          ValueHandler &Callback) const;
    126 
    127 public:
    128   CallLowering(const TargetLowering *TLI) : TLI(TLI) {}
    129   virtual ~CallLowering() {}
    130 
    131   /// This hook must be implemented to lower outgoing return values, described
    132   /// by \p Val, into the specified virtual register \p VReg.
    133   /// This hook is used by GlobalISel.
    134   ///
    135   /// \return True if the lowering succeeds, false otherwise.
    136   virtual bool lowerReturn(MachineIRBuilder &MIRBuilder,
    137                            const Value *Val, unsigned VReg) const {
    138     return false;
    139   }
    140 
    141   /// This hook must be implemented to lower the incoming (formal)
    142   /// arguments, described by \p Args, for GlobalISel. Each argument
    143   /// must end up in the related virtual register described by VRegs.
    144   /// In other words, the first argument should end up in VRegs[0],
    145   /// the second in VRegs[1], and so on.
    146   /// \p MIRBuilder is set to the proper insertion for the argument
    147   /// lowering.
    148   ///
    149   /// \return True if the lowering succeeded, false otherwise.
    150   virtual bool lowerFormalArguments(MachineIRBuilder &MIRBuilder,
    151                                     const Function &F,
    152                                     ArrayRef<unsigned> VRegs) const {
    153     return false;
    154   }
    155 
    156   /// This hook must be implemented to lower the given call instruction,
    157   /// including argument and return value marshalling.
    158   ///
    159   /// \p CallConv is the calling convention to be used for the call.
    160   ///
    161   /// \p Callee is the destination of the call. It should be either a register,
    162   /// globaladdress, or externalsymbol.
    163   ///
    164   /// \p ResTy is the type returned by the function
    165   ///
    166   /// \p ResReg is the generic virtual register that the returned
    167   /// value should be lowered into.
    168   ///
    169   /// \p ArgTys is a list of the types each member of \p ArgRegs has; used by
    170   /// the target to decide which register/stack slot should be allocated.
    171   ///
    172   /// \p ArgRegs is a list of virtual registers containing each argument that
    173   /// needs to be passed.
    174   ///
    175   /// \return true if the lowering succeeded, false otherwise.
    176   virtual bool lowerCall(MachineIRBuilder &MIRBuilder, CallingConv::ID CallConv,
    177                          const MachineOperand &Callee, const ArgInfo &OrigRet,
    178                          ArrayRef<ArgInfo> OrigArgs) const {
    179     return false;
    180   }
    181 
    182   /// Lower the given call instruction, including argument and return value
    183   /// marshalling.
    184   ///
    185   /// \p CI is the call/invoke instruction.
    186   ///
    187   /// \p ResReg is a register where the call's return value should be stored (or
    188   /// 0 if there is no return value).
    189   ///
    190   /// \p ArgRegs is a list of virtual registers containing each argument that
    191   /// needs to be passed.
    192   ///
    193   /// \p GetCalleeReg is a callback to materialize a register for the callee if
    194   /// the target determines it cannot jump to the destination based purely on \p
    195   /// CI. This might be because \p CI is indirect, or because of the limited
    196   /// range of an immediate jump.
    197   ///
    198   /// \return true if the lowering succeeded, false otherwise.
    199   bool lowerCall(MachineIRBuilder &MIRBuilder, ImmutableCallSite CS,
    200                  unsigned ResReg, ArrayRef<unsigned> ArgRegs,
    201                  std::function<unsigned()> GetCalleeReg) const;
    202 };
    203 } // End namespace llvm.
    204 
    205 #endif
    206