LLVM  7.0.0
Nios2ISelLowering.cpp
Go to the documentation of this file.
1 //===-- Nios2ISelLowering.cpp - Nios2 DAG Lowering Implementation ---------===//
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 // This file implements the interfaces that Nios2 uses to lower LLVM code into a
11 // selection DAG.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "Nios2ISelLowering.h"
16 #include "Nios2MachineFunction.h"
17 #include "Nios2TargetMachine.h"
20 
21 using namespace llvm;
22 
23 //===----------------------------------------------------------------------===//
24 // Calling Convention Implementation
25 //===----------------------------------------------------------------------===//
26 
27 #include "Nios2GenCallingConv.inc"
28 
29 SDValue
31  bool IsVarArg,
33  const SmallVectorImpl<SDValue> &OutVals,
34  const SDLoc &DL, SelectionDAG &DAG) const {
35  // CCValAssign - represent the assignment of
36  // the return value to a location
39 
40  // CCState - Info about the registers and stack slot.
41  CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
42  // Analyze return values.
43  CCInfo.CheckReturn(Outs, RetCC_Nios2EABI);
44 
45  SDValue Flag;
46  SmallVector<SDValue, 4> RetOps(1, Chain);
47 
48  // Copy the result values into the output registers.
49  for (unsigned i = 0; i != RVLocs.size(); ++i) {
50  SDValue Val = OutVals[i];
51  CCValAssign &VA = RVLocs[i];
52  assert(VA.isRegLoc() && "Can only return in registers!");
53 
54  if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
55  Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
56 
57  Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
58 
59  // Guarantee that all emitted copies are stuck together with flags.
60  Flag = Chain.getValue(1);
61  RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
62  }
63 
64  if (Flag.getNode())
65  RetOps.push_back(Flag);
66 
67  return DAG.getNode(Nios2ISD::Ret, DL, MVT::Other, RetOps);
68 }
69 
70 // addLiveIn - This helper function adds the specified physical register to the
71 // MachineFunction as a live in value. It also creates a corresponding
72 // virtual register for it.
73 static unsigned addLiveIn(MachineFunction &MF, unsigned PReg,
74  const TargetRegisterClass *RC) {
75  unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
76  MF.getRegInfo().addLiveIn(PReg, VReg);
77  return VReg;
78 }
79 
80 //===----------------------------------------------------------------------===//
81 // Formal Arguments Calling Convention Implementation
82 //===----------------------------------------------------------------------===//
83 
84 // LowerFormalArguments - transform physical registers into virtual registers
85 // and generate load operations for arguments places on the stack.
87  SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
88  const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
89  SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
91  MachineFrameInfo &MFI = MF.getFrameInfo();
92 
93  // Assign locations to all of the incoming arguments.
95  CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
96  *DAG.getContext());
97 
98  CCInfo.AnalyzeFormalArguments(Ins, CC_Nios2);
99 
100  // Used with vargs to acumulate store chains.
101  std::vector<SDValue> OutChains;
102 
103  for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
104  CCValAssign &VA = ArgLocs[i];
105 
106  EVT ValVT = VA.getValVT();
107 
108  // Arguments stored on registers
109  if (VA.isRegLoc()) {
110  MVT RegVT = VA.getLocVT();
111  unsigned ArgReg = VA.getLocReg();
112  const TargetRegisterClass *RC = getRegClassFor(RegVT);
113 
114  // Transform the arguments stored on
115  // physical registers into virtual ones
116  unsigned Reg = addLiveIn(MF, ArgReg, RC);
117  SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
118 
119  // If this is an 8 or 16-bit value, it has been passed promoted
120  // to 32 bits. Insert an assert[sz]ext to capture this, then
121  // truncate to the right size.
122  if (VA.getLocInfo() != CCValAssign::Full) {
123  unsigned Opcode = 0;
124  if (VA.getLocInfo() == CCValAssign::SExt)
125  Opcode = ISD::AssertSext;
126  else if (VA.getLocInfo() == CCValAssign::ZExt)
127  Opcode = ISD::AssertZext;
128  if (Opcode)
129  ArgValue =
130  DAG.getNode(Opcode, DL, RegVT, ArgValue, DAG.getValueType(ValVT));
131  ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
132  }
133 
134  // Handle floating point arguments passed in integer registers.
135  if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
136  (RegVT == MVT::i64 && ValVT == MVT::f64))
137  ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
138  InVals.push_back(ArgValue);
139  } else { // VA.isRegLoc()
140  MVT LocVT = VA.getLocVT();
141 
142  // sanity check
143  assert(VA.isMemLoc());
144 
145  // The stack pointer offset is relative to the caller stack frame.
146  int FI = MFI.CreateFixedObject(ValVT.getSizeInBits() / 8,
147  VA.getLocMemOffset(), true);
148 
149  // Create load nodes to retrieve arguments from the stack
150  SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
151  SDValue Load = DAG.getLoad(
152  LocVT, DL, Chain, FIN,
154  InVals.push_back(Load);
155  OutChains.push_back(Load.getValue(1));
156  }
157  }
158  if (!OutChains.empty()) {
159  OutChains.push_back(Chain);
160  Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
161  }
162 
163  return Chain;
164 }
165 
166 //===----------------------------------------------------------------------===//
167 // TargetLowering Implementation
168 //===----------------------------------------------------------------------===//
169 
171  const Nios2Subtarget &STI)
172  : TargetLowering(TM), Subtarget(&STI) {
173 
174  addRegisterClass(MVT::i32, &Nios2::CPURegsRegClass);
176 }
177 
178 const char *Nios2TargetLowering::getTargetNodeName(unsigned Opcode) const {
179  switch (Opcode) {
180  case Nios2ISD::Hi:
181  return "Nios2ISD::Hi";
182  case Nios2ISD::Lo:
183  return "Nios2ISD::Lo";
184  case Nios2ISD::Ret:
185  return "Nios2ISD::Ret";
186  }
187  return nullptr;
188 }
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:541
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
void addLiveIn(unsigned Reg, unsigned vreg=0)
addLiveIn - Add the specified register as a live-in.
void AnalyzeFormalArguments(const SmallVectorImpl< ISD::InputArg > &Ins, CCAssignFn Fn)
AnalyzeFormalArguments - Analyze an array of argument values, incorporating info about the formals in...
unsigned Reg
virtual const TargetRegisterClass * getRegClassFor(MVT VT) const
Return the register class that should be used for the specified value type.
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
static unsigned addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
bool isMemLoc() const
SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg, const SmallVectorImpl< ISD::OutputArg > &Outs, const SmallVectorImpl< SDValue > &OutVals, const SDLoc &dl, SelectionDAG &DAG) const override
This hook must be implemented to lower outgoing return values, described by the Outs array...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
const DataLayout & getDataLayout() const
Definition: SelectionDAG.h:398
LocInfo getLocInfo() const
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
unsigned getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:292
MachineFunction & getMachineFunction() const
Definition: SelectionDAG.h:395
void computeRegisterProperties(const TargetRegisterInfo *TRI)
Once all of the register classes are added, this allows us to compute derived properties we expose...
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:118
MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
Machine Value Type.
void addRegisterClass(MVT VT, const TargetRegisterClass *RC)
Add the specified register class as an available regclass for the specified value type...
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Nios2TargetLowering(const TargetMachine &TM, const Nios2Subtarget &STI)
const Nios2RegisterInfo * getRegisterInfo() const override
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition: ISDOpcodes.h:57
Extended Value Type.
Definition: ValueTypes.h:34
size_t size() const
Definition: SmallVector.h:53
SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands...
TokenFactor - This node takes multiple tokens as input and produces a single token result...
Definition: ISDOpcodes.h:50
SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, const SmallVectorImpl< ISD::InputArg > &Ins, const SDLoc &dl, SelectionDAG &DAG, SmallVectorImpl< SDValue > &InVals) const override
This hook must be implemented to lower the incoming (formal) arguments, described by the Ins array...
CCState - This class holds information needed while lowering arguments and return values...
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:222
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
CCValAssign - Represent assignment of one arg/retval to a location.
int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable, bool isAliased=false)
Create a new object at a fixed location on the stack.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, unsigned Reg, SDValue N)
Definition: SelectionDAG.h:674
static MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
bool CheckReturn(const SmallVectorImpl< ISD::OutputArg > &Outs, CCAssignFn Fn)
CheckReturn - Analyze the return values of a function, returning true if the return can be performed ...
const char * getTargetNodeName(unsigned Opcode) const override
getTargetNodeName - This method returns the name of a target specific
SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, unsigned Reg, EVT VT)
Definition: SelectionDAG.h:700
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
unsigned getLocMemOffset() const
SDValue getValue(unsigned R) const
bool isRegLoc() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
SDValue getRegister(unsigned Reg, EVT VT)
SDValue getValueType(EVT)
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
unsigned getLocReg() const
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:446
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation...
LLVMContext * getContext() const
Definition: SelectionDAG.h:404
unsigned createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...