1 //===-- VEISelLowering.cpp - VE DAG Lowering Implementation ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the interfaces that VE uses to lower LLVM code into a
10 // selection DAG.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "VEISelLowering.h"
15 #include "MCTargetDesc/VEMCExpr.h"
16 #include "VEMachineFunctionInfo.h"
17 #include "VERegisterInfo.h"
18 #include "VETargetMachine.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/CodeGen/CallingConvLower.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/SelectionDAG.h"
27 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/Function.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/KnownBits.h"
33 using namespace llvm;
34 
35 #define DEBUG_TYPE "ve-lower"
36 
37 //===----------------------------------------------------------------------===//
38 // Calling Convention Implementation
39 //===----------------------------------------------------------------------===//
40 
41 static bool allocateFloat(unsigned ValNo, MVT ValVT, MVT LocVT,
42                           CCValAssign::LocInfo LocInfo,
43                           ISD::ArgFlagsTy ArgFlags, CCState &State) {
44   switch (LocVT.SimpleTy) {
45   case MVT::f32: {
46     // Allocate stack like below
47     //    0      4
48     //    +------+------+
49     //    | empty| float|
50     //    +------+------+
51     // Use align=8 for dummy area to align the beginning of these 2 area.
52     State.AllocateStack(4, 8); // for empty area
53     // Use align=4 for value to place it at just after the dummy area.
54     unsigned Offset = State.AllocateStack(4, 4); // for float value area
55     State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
56     return true;
57   }
58   default:
59     return false;
60   }
61 }
62 
63 #include "VEGenCallingConv.inc"
64 
65 bool VETargetLowering::CanLowerReturn(
66     CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
67     const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
68   CCAssignFn *RetCC = RetCC_VE;
69   SmallVector<CCValAssign, 16> RVLocs;
70   CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
71   return CCInfo.CheckReturn(Outs, RetCC);
72 }
73 
74 SDValue
75 VETargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
76                               bool IsVarArg,
77                               const SmallVectorImpl<ISD::OutputArg> &Outs,
78                               const SmallVectorImpl<SDValue> &OutVals,
79                               const SDLoc &DL, SelectionDAG &DAG) const {
80   // CCValAssign - represent the assignment of the return value to locations.
81   SmallVector<CCValAssign, 16> RVLocs;
82 
83   // CCState - Info about the registers and stack slot.
84   CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
85                  *DAG.getContext());
86 
87   // Analyze return values.
88   CCInfo.AnalyzeReturn(Outs, RetCC_VE);
89 
90   SDValue Flag;
91   SmallVector<SDValue, 4> RetOps(1, Chain);
92 
93   // Copy the result values into the output registers.
94   for (unsigned i = 0; i != RVLocs.size(); ++i) {
95     CCValAssign &VA = RVLocs[i];
96     assert(VA.isRegLoc() && "Can only return in registers!");
97     SDValue OutVal = OutVals[i];
98 
99     // Integer return values must be sign or zero extended by the callee.
100     switch (VA.getLocInfo()) {
101     case CCValAssign::Full:
102       break;
103     case CCValAssign::SExt:
104       OutVal = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), OutVal);
105       break;
106     case CCValAssign::ZExt:
107       OutVal = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), OutVal);
108       break;
109     case CCValAssign::AExt:
110       OutVal = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), OutVal);
111       break;
112     default:
113       llvm_unreachable("Unknown loc info!");
114     }
115 
116     assert(!VA.needsCustom() && "Unexpected custom lowering");
117 
118     Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVal, Flag);
119 
120     // Guarantee that all emitted copies are stuck together with flags.
121     Flag = Chain.getValue(1);
122     RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
123   }
124 
125   RetOps[0] = Chain; // Update chain.
126 
127   // Add the flag if we have it.
128   if (Flag.getNode())
129     RetOps.push_back(Flag);
130 
131   return DAG.getNode(VEISD::RET_FLAG, DL, MVT::Other, RetOps);
132 }
133 
134 SDValue VETargetLowering::LowerFormalArguments(
135     SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
136     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
137     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
138   MachineFunction &MF = DAG.getMachineFunction();
139 
140   // Get the base offset of the incoming arguments stack space.
141   unsigned ArgsBaseOffset = 176;
142   // Get the size of the preserved arguments area
143   unsigned ArgsPreserved = 64;
144 
145   // Analyze arguments according to CC_VE.
146   SmallVector<CCValAssign, 16> ArgLocs;
147   CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
148                  *DAG.getContext());
149   // Allocate the preserved area first.
150   CCInfo.AllocateStack(ArgsPreserved, 8);
151   // We already allocated the preserved area, so the stack offset computed
152   // by CC_VE would be correct now.
153   CCInfo.AnalyzeFormalArguments(Ins, CC_VE);
154 
155   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
156     CCValAssign &VA = ArgLocs[i];
157     if (VA.isRegLoc()) {
158       // This argument is passed in a register.
159       // All integer register arguments are promoted by the caller to i64.
160 
161       // Create a virtual register for the promoted live-in value.
162       unsigned VReg =
163           MF.addLiveIn(VA.getLocReg(), getRegClassFor(VA.getLocVT()));
164       SDValue Arg = DAG.getCopyFromReg(Chain, DL, VReg, VA.getLocVT());
165 
166       // Get the high bits for i32 struct elements.
167       if (VA.getValVT() == MVT::i32 && VA.needsCustom())
168         Arg = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), Arg,
169                           DAG.getConstant(32, DL, MVT::i32));
170 
171       // The caller promoted the argument, so insert an Assert?ext SDNode so we
172       // won't promote the value again in this function.
173       switch (VA.getLocInfo()) {
174       case CCValAssign::SExt:
175         Arg = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Arg,
176                           DAG.getValueType(VA.getValVT()));
177         break;
178       case CCValAssign::ZExt:
179         Arg = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Arg,
180                           DAG.getValueType(VA.getValVT()));
181         break;
182       default:
183         break;
184       }
185 
186       // Truncate the register down to the argument type.
187       if (VA.isExtInLoc())
188         Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
189 
190       InVals.push_back(Arg);
191       continue;
192     }
193 
194     // The registers are exhausted. This argument was passed on the stack.
195     assert(VA.isMemLoc());
196     // The CC_VE_Full/Half functions compute stack offsets relative to the
197     // beginning of the arguments area at %fp+176.
198     unsigned Offset = VA.getLocMemOffset() + ArgsBaseOffset;
199     unsigned ValSize = VA.getValVT().getSizeInBits() / 8;
200     int FI = MF.getFrameInfo().CreateFixedObject(ValSize, Offset, true);
201     InVals.push_back(
202         DAG.getLoad(VA.getValVT(), DL, Chain,
203                     DAG.getFrameIndex(FI, getPointerTy(MF.getDataLayout())),
204                     MachinePointerInfo::getFixedStack(MF, FI)));
205   }
206 
207   if (!IsVarArg)
208     return Chain;
209 
210   // This function takes variable arguments, some of which may have been passed
211   // in registers %s0-%s8.
212   //
213   // The va_start intrinsic needs to know the offset to the first variable
214   // argument.
215   // TODO: need to calculate offset correctly once we support f128.
216   unsigned ArgOffset = ArgLocs.size() * 8;
217   VEMachineFunctionInfo *FuncInfo = MF.getInfo<VEMachineFunctionInfo>();
218   // Skip the 176 bytes of register save area.
219   FuncInfo->setVarArgsFrameOffset(ArgOffset + ArgsBaseOffset);
220 
221   return Chain;
222 }
223 
224 // FIXME? Maybe this could be a TableGen attribute on some registers and
225 // this table could be generated automatically from RegInfo.
226 Register VETargetLowering::getRegisterByName(const char *RegName, LLT VT,
227                                              const MachineFunction &MF) const {
228   Register Reg = StringSwitch<Register>(RegName)
229                      .Case("sp", VE::SX11)    // Stack pointer
230                      .Case("fp", VE::SX9)     // Frame pointer
231                      .Case("sl", VE::SX8)     // Stack limit
232                      .Case("lr", VE::SX10)    // Link register
233                      .Case("tp", VE::SX14)    // Thread pointer
234                      .Case("outer", VE::SX12) // Outer regiser
235                      .Case("info", VE::SX17)  // Info area register
236                      .Case("got", VE::SX15)   // Global offset table register
237                      .Case("plt", VE::SX16) // Procedure linkage table register
238                      .Default(0);
239 
240   if (Reg)
241     return Reg;
242 
243   report_fatal_error("Invalid register name global variable");
244 }
245 
246 //===----------------------------------------------------------------------===//
247 // TargetLowering Implementation
248 //===----------------------------------------------------------------------===//
249 
250 SDValue VETargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
251                                     SmallVectorImpl<SDValue> &InVals) const {
252   SelectionDAG &DAG = CLI.DAG;
253   SDLoc DL = CLI.DL;
254   SDValue Chain = CLI.Chain;
255   auto PtrVT = getPointerTy(DAG.getDataLayout());
256 
257   // VE target does not yet support tail call optimization.
258   CLI.IsTailCall = false;
259 
260   // Get the base offset of the outgoing arguments stack space.
261   unsigned ArgsBaseOffset = 176;
262   // Get the size of the preserved arguments area
263   unsigned ArgsPreserved = 8 * 8u;
264 
265   // Analyze operands of the call, assigning locations to each operand.
266   SmallVector<CCValAssign, 16> ArgLocs;
267   CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), ArgLocs,
268                  *DAG.getContext());
269   // Allocate the preserved area first.
270   CCInfo.AllocateStack(ArgsPreserved, 8);
271   // We already allocated the preserved area, so the stack offset computed
272   // by CC_VE would be correct now.
273   CCInfo.AnalyzeCallOperands(CLI.Outs, CC_VE);
274 
275   // VE requires to use both register and stack for varargs or no-prototyped
276   // functions.
277   bool UseBoth = CLI.IsVarArg;
278 
279   // Analyze operands again if it is required to store BOTH.
280   SmallVector<CCValAssign, 16> ArgLocs2;
281   CCState CCInfo2(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(),
282                   ArgLocs2, *DAG.getContext());
283   if (UseBoth)
284     CCInfo2.AnalyzeCallOperands(CLI.Outs, CC_VE2);
285 
286   // Get the size of the outgoing arguments stack space requirement.
287   unsigned ArgsSize = CCInfo.getNextStackOffset();
288 
289   // Keep stack frames 16-byte aligned.
290   ArgsSize = alignTo(ArgsSize, 16);
291 
292   // Adjust the stack pointer to make room for the arguments.
293   // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
294   // with more than 6 arguments.
295   Chain = DAG.getCALLSEQ_START(Chain, ArgsSize, 0, DL);
296 
297   // Collect the set of registers to pass to the function and their values.
298   // This will be emitted as a sequence of CopyToReg nodes glued to the call
299   // instruction.
300   SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
301 
302   // Collect chains from all the memory opeations that copy arguments to the
303   // stack. They must follow the stack pointer adjustment above and precede the
304   // call instruction itself.
305   SmallVector<SDValue, 8> MemOpChains;
306 
307   // VE needs to get address of callee function in a register
308   // So, prepare to copy it to SX12 here.
309 
310   // If the callee is a GlobalAddress node (quite common, every direct call is)
311   // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
312   // Likewise ExternalSymbol -> TargetExternalSymbol.
313   SDValue Callee = CLI.Callee;
314 
315   bool IsPICCall = isPositionIndependent();
316 
317   // PC-relative references to external symbols should go through $stub.
318   // If so, we need to prepare GlobalBaseReg first.
319   const TargetMachine &TM = DAG.getTarget();
320   const Module *Mod = DAG.getMachineFunction().getFunction().getParent();
321   const GlobalValue *GV = nullptr;
322   auto *CalleeG = dyn_cast<GlobalAddressSDNode>(Callee);
323   if (CalleeG)
324     GV = CalleeG->getGlobal();
325   bool Local = TM.shouldAssumeDSOLocal(*Mod, GV);
326   bool UsePlt = !Local;
327   MachineFunction &MF = DAG.getMachineFunction();
328 
329   // Turn GlobalAddress/ExternalSymbol node into a value node
330   // containing the address of them here.
331   if (CalleeG) {
332     if (IsPICCall) {
333       if (UsePlt)
334         Subtarget->getInstrInfo()->getGlobalBaseReg(&MF);
335       Callee = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 0);
336       Callee = DAG.getNode(VEISD::GETFUNPLT, DL, PtrVT, Callee);
337     } else {
338       Callee =
339           makeHiLoPair(Callee, VEMCExpr::VK_VE_HI32, VEMCExpr::VK_VE_LO32, DAG);
340     }
341   } else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
342     if (IsPICCall) {
343       if (UsePlt)
344         Subtarget->getInstrInfo()->getGlobalBaseReg(&MF);
345       Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT, 0);
346       Callee = DAG.getNode(VEISD::GETFUNPLT, DL, PtrVT, Callee);
347     } else {
348       Callee =
349           makeHiLoPair(Callee, VEMCExpr::VK_VE_HI32, VEMCExpr::VK_VE_LO32, DAG);
350     }
351   }
352 
353   RegsToPass.push_back(std::make_pair(VE::SX12, Callee));
354 
355   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
356     CCValAssign &VA = ArgLocs[i];
357     SDValue Arg = CLI.OutVals[i];
358 
359     // Promote the value if needed.
360     switch (VA.getLocInfo()) {
361     default:
362       llvm_unreachable("Unknown location info!");
363     case CCValAssign::Full:
364       break;
365     case CCValAssign::SExt:
366       Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
367       break;
368     case CCValAssign::ZExt:
369       Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
370       break;
371     case CCValAssign::AExt:
372       Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
373       break;
374     }
375 
376     if (VA.isRegLoc()) {
377       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
378       if (!UseBoth)
379         continue;
380       VA = ArgLocs2[i];
381     }
382 
383     assert(VA.isMemLoc());
384 
385     // Create a store off the stack pointer for this argument.
386     SDValue StackPtr = DAG.getRegister(VE::SX11, PtrVT);
387     // The argument area starts at %fp+176 in the callee frame,
388     // %sp+176 in ours.
389     SDValue PtrOff =
390         DAG.getIntPtrConstant(VA.getLocMemOffset() + ArgsBaseOffset, DL);
391     PtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, PtrOff);
392     MemOpChains.push_back(
393         DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo()));
394   }
395 
396   // Emit all stores, make sure they occur before the call.
397   if (!MemOpChains.empty())
398     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
399 
400   // Build a sequence of CopyToReg nodes glued together with token chain and
401   // glue operands which copy the outgoing args into registers. The InGlue is
402   // necessary since all emitted instructions must be stuck together in order
403   // to pass the live physical registers.
404   SDValue InGlue;
405   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
406     Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[i].first,
407                              RegsToPass[i].second, InGlue);
408     InGlue = Chain.getValue(1);
409   }
410 
411   // Build the operands for the call instruction itself.
412   SmallVector<SDValue, 8> Ops;
413   Ops.push_back(Chain);
414   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
415     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
416                                   RegsToPass[i].second.getValueType()));
417 
418   // Add a register mask operand representing the call-preserved registers.
419   const VERegisterInfo *TRI = Subtarget->getRegisterInfo();
420   const uint32_t *Mask =
421       TRI->getCallPreservedMask(DAG.getMachineFunction(), CLI.CallConv);
422   assert(Mask && "Missing call preserved mask for calling convention");
423   Ops.push_back(DAG.getRegisterMask(Mask));
424 
425   // Make sure the CopyToReg nodes are glued to the call instruction which
426   // consumes the registers.
427   if (InGlue.getNode())
428     Ops.push_back(InGlue);
429 
430   // Now the call itself.
431   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
432   Chain = DAG.getNode(VEISD::CALL, DL, NodeTys, Ops);
433   InGlue = Chain.getValue(1);
434 
435   // Revert the stack pointer immediately after the call.
436   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, DL, true),
437                              DAG.getIntPtrConstant(0, DL, true), InGlue, DL);
438   InGlue = Chain.getValue(1);
439 
440   // Now extract the return values. This is more or less the same as
441   // LowerFormalArguments.
442 
443   // Assign locations to each value returned by this call.
444   SmallVector<CCValAssign, 16> RVLocs;
445   CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), RVLocs,
446                  *DAG.getContext());
447 
448   // Set inreg flag manually for codegen generated library calls that
449   // return float.
450   if (CLI.Ins.size() == 1 && CLI.Ins[0].VT == MVT::f32 && !CLI.CB)
451     CLI.Ins[0].Flags.setInReg();
452 
453   RVInfo.AnalyzeCallResult(CLI.Ins, RetCC_VE);
454 
455   // Copy all of the result registers out of their specified physreg.
456   for (unsigned i = 0; i != RVLocs.size(); ++i) {
457     CCValAssign &VA = RVLocs[i];
458     unsigned Reg = VA.getLocReg();
459 
460     // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
461     // reside in the same register in the high and low bits. Reuse the
462     // CopyFromReg previous node to avoid duplicate copies.
463     SDValue RV;
464     if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1)))
465       if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
466         RV = Chain.getValue(0);
467 
468     // But usually we'll create a new CopyFromReg for a different register.
469     if (!RV.getNode()) {
470       RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue);
471       Chain = RV.getValue(1);
472       InGlue = Chain.getValue(2);
473     }
474 
475     // Get the high bits for i32 struct elements.
476     if (VA.getValVT() == MVT::i32 && VA.needsCustom())
477       RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV,
478                        DAG.getConstant(32, DL, MVT::i32));
479 
480     // The callee promoted the return value, so insert an Assert?ext SDNode so
481     // we won't promote the value again in this function.
482     switch (VA.getLocInfo()) {
483     case CCValAssign::SExt:
484       RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV,
485                        DAG.getValueType(VA.getValVT()));
486       break;
487     case CCValAssign::ZExt:
488       RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV,
489                        DAG.getValueType(VA.getValVT()));
490       break;
491     default:
492       break;
493     }
494 
495     // Truncate the register down to the return value type.
496     if (VA.isExtInLoc())
497       RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV);
498 
499     InVals.push_back(RV);
500   }
501 
502   return Chain;
503 }
504 
505 /// isFPImmLegal - Returns true if the target can instruction select the
506 /// specified FP immediate natively. If false, the legalizer will
507 /// materialize the FP immediate as a load from a constant pool.
508 bool VETargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT,
509                                     bool ForCodeSize) const {
510   return VT == MVT::f32 || VT == MVT::f64;
511 }
512 
513 /// Determine if the target supports unaligned memory accesses.
514 ///
515 /// This function returns true if the target allows unaligned memory accesses
516 /// of the specified type in the given address space. If true, it also returns
517 /// whether the unaligned memory access is "fast" in the last argument by
518 /// reference. This is used, for example, in situations where an array
519 /// copy/move/set is converted to a sequence of store operations. Its use
520 /// helps to ensure that such replacements don't generate code that causes an
521 /// alignment error (trap) on the target machine.
522 bool VETargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
523                                                       unsigned AddrSpace,
524                                                       unsigned Align,
525                                                       MachineMemOperand::Flags,
526                                                       bool *Fast) const {
527   if (Fast) {
528     // It's fast anytime on VE
529     *Fast = true;
530   }
531   return true;
532 }
533 
534 VETargetLowering::VETargetLowering(const TargetMachine &TM,
535                                    const VESubtarget &STI)
536     : TargetLowering(TM), Subtarget(&STI) {
537   // Instructions which use registers as conditionals examine all the
538   // bits (as does the pseudo SELECT_CC expansion). I don't think it
539   // matters much whether it's ZeroOrOneBooleanContent, or
540   // ZeroOrNegativeOneBooleanContent, so, arbitrarily choose the
541   // former.
542   setBooleanContents(ZeroOrOneBooleanContent);
543   setBooleanVectorContents(ZeroOrOneBooleanContent);
544 
545   // Set up the register classes.
546   addRegisterClass(MVT::i32, &VE::I32RegClass);
547   addRegisterClass(MVT::i64, &VE::I64RegClass);
548   addRegisterClass(MVT::f32, &VE::F32RegClass);
549   addRegisterClass(MVT::f64, &VE::I64RegClass);
550 
551   /// Load & Store {
552   for (MVT FPVT : MVT::fp_valuetypes()) {
553     for (MVT OtherFPVT : MVT::fp_valuetypes()) {
554       // Turn FP extload into load/fpextend
555       setLoadExtAction(ISD::EXTLOAD, FPVT, OtherFPVT, Expand);
556 
557       // Turn FP truncstore into trunc + store.
558       setTruncStoreAction(FPVT, OtherFPVT, Expand);
559     }
560   }
561 
562   // VE doesn't have i1 sign extending load
563   for (MVT VT : MVT::integer_valuetypes()) {
564     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
565     setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
566     setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
567     setTruncStoreAction(VT, MVT::i1, Expand);
568   }
569   /// } Load & Store
570 
571   // Custom legalize address nodes into LO/HI parts.
572   MVT PtrVT = MVT::getIntegerVT(TM.getPointerSizeInBits(0));
573   setOperationAction(ISD::BlockAddress, PtrVT, Custom);
574   setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
575   setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
576 
577   /// VAARG handling {
578   setOperationAction(ISD::VASTART, MVT::Other, Custom);
579   // VAARG needs to be lowered to access with 8 bytes alignment.
580   setOperationAction(ISD::VAARG, MVT::Other, Custom);
581   // Use the default implementation.
582   setOperationAction(ISD::VACOPY, MVT::Other, Expand);
583   setOperationAction(ISD::VAEND, MVT::Other, Expand);
584   /// } VAARG handling
585 
586   /// Int Ops {
587   for (MVT IntVT : {MVT::i32, MVT::i64}) {
588     // VE has no REM or DIVREM operations.
589     setOperationAction(ISD::UREM, IntVT, Expand);
590     setOperationAction(ISD::SREM, IntVT, Expand);
591     setOperationAction(ISD::SDIVREM, IntVT, Expand);
592     setOperationAction(ISD::UDIVREM, IntVT, Expand);
593 
594     setOperationAction(ISD::CTTZ, IntVT, Expand);
595     setOperationAction(ISD::ROTL, IntVT, Expand);
596     setOperationAction(ISD::ROTR, IntVT, Expand);
597 
598     // Use isel patterns for i32 and i64
599     setOperationAction(ISD::BSWAP, IntVT, Legal);
600     setOperationAction(ISD::CTLZ, IntVT, Legal);
601     setOperationAction(ISD::CTPOP, IntVT, Legal);
602 
603     // Use isel patterns for i64, Promote i32
604     LegalizeAction Act = (IntVT == MVT::i32) ? Promote : Legal;
605     setOperationAction(ISD::BITREVERSE, IntVT, Act);
606   }
607   /// } Int Ops
608 
609   /// Conversion {
610   // VE doesn't have instructions for fp<->uint, so expand them by llvm
611   setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote); // use i64
612   setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote); // use i64
613   setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
614   setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
615 
616   // fp16 not supported
617   for (MVT FPVT : MVT::fp_valuetypes()) {
618     setOperationAction(ISD::FP16_TO_FP, FPVT, Expand);
619     setOperationAction(ISD::FP_TO_FP16, FPVT, Expand);
620   }
621   /// } Conversion
622 
623   setStackPointerRegisterToSaveRestore(VE::SX11);
624 
625   // Set function alignment to 16 bytes
626   setMinFunctionAlignment(Align(16));
627 
628   // VE stores all argument by 8 bytes alignment
629   setMinStackArgumentAlignment(Align(8));
630 
631   computeRegisterProperties(Subtarget->getRegisterInfo());
632 }
633 
634 const char *VETargetLowering::getTargetNodeName(unsigned Opcode) const {
635 #define TARGET_NODE_CASE(NAME)                                                 \
636   case VEISD::NAME:                                                            \
637     return "VEISD::" #NAME;
638   switch ((VEISD::NodeType)Opcode) {
639   case VEISD::FIRST_NUMBER:
640     break;
641     TARGET_NODE_CASE(Lo)
642     TARGET_NODE_CASE(Hi)
643     TARGET_NODE_CASE(GETFUNPLT)
644     TARGET_NODE_CASE(GETTLSADDR)
645     TARGET_NODE_CASE(CALL)
646     TARGET_NODE_CASE(RET_FLAG)
647     TARGET_NODE_CASE(GLOBAL_BASE_REG)
648   }
649 #undef TARGET_NODE_CASE
650   return nullptr;
651 }
652 
653 EVT VETargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
654                                          EVT VT) const {
655   return MVT::i32;
656 }
657 
658 // Convert to a target node and set target flags.
659 SDValue VETargetLowering::withTargetFlags(SDValue Op, unsigned TF,
660                                           SelectionDAG &DAG) const {
661   if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
662     return DAG.getTargetGlobalAddress(GA->getGlobal(), SDLoc(GA),
663                                       GA->getValueType(0), GA->getOffset(), TF);
664 
665   if (const BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op))
666     return DAG.getTargetBlockAddress(BA->getBlockAddress(), Op.getValueType(),
667                                      0, TF);
668 
669   if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op))
670     return DAG.getTargetExternalSymbol(ES->getSymbol(), ES->getValueType(0),
671                                        TF);
672 
673   llvm_unreachable("Unhandled address SDNode");
674 }
675 
676 // Split Op into high and low parts according to HiTF and LoTF.
677 // Return an ADD node combining the parts.
678 SDValue VETargetLowering::makeHiLoPair(SDValue Op, unsigned HiTF, unsigned LoTF,
679                                        SelectionDAG &DAG) const {
680   SDLoc DL(Op);
681   EVT VT = Op.getValueType();
682   SDValue Hi = DAG.getNode(VEISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG));
683   SDValue Lo = DAG.getNode(VEISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG));
684   return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
685 }
686 
687 // Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
688 // or ExternalSymbol SDNode.
689 SDValue VETargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
690   SDLoc DL(Op);
691   EVT PtrVT = Op.getValueType();
692 
693   // Handle PIC mode first. VE needs a got load for every variable!
694   if (isPositionIndependent()) {
695     // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
696     // function has calls.
697     MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
698     MFI.setHasCalls(true);
699     auto GlobalN = dyn_cast<GlobalAddressSDNode>(Op);
700 
701     if (isa<ConstantPoolSDNode>(Op) ||
702         (GlobalN && GlobalN->getGlobal()->hasLocalLinkage())) {
703       // Create following instructions for local linkage PIC code.
704       //     lea %s35, %gotoff_lo(.LCPI0_0)
705       //     and %s35, %s35, (32)0
706       //     lea.sl %s35, %gotoff_hi(.LCPI0_0)(%s35)
707       //     adds.l %s35, %s15, %s35                  ; %s15 is GOT
708       // FIXME: use lea.sl %s35, %gotoff_hi(.LCPI0_0)(%s35, %s15)
709       SDValue HiLo = makeHiLoPair(Op, VEMCExpr::VK_VE_GOTOFF_HI32,
710                                   VEMCExpr::VK_VE_GOTOFF_LO32, DAG);
711       SDValue GlobalBase = DAG.getNode(VEISD::GLOBAL_BASE_REG, DL, PtrVT);
712       return DAG.getNode(ISD::ADD, DL, PtrVT, GlobalBase, HiLo);
713     }
714     // Create following instructions for not local linkage PIC code.
715     //     lea %s35, %got_lo(.LCPI0_0)
716     //     and %s35, %s35, (32)0
717     //     lea.sl %s35, %got_hi(.LCPI0_0)(%s35)
718     //     adds.l %s35, %s15, %s35                  ; %s15 is GOT
719     //     ld     %s35, (,%s35)
720     // FIXME: use lea.sl %s35, %gotoff_hi(.LCPI0_0)(%s35, %s15)
721     SDValue HiLo = makeHiLoPair(Op, VEMCExpr::VK_VE_GOT_HI32,
722                                 VEMCExpr::VK_VE_GOT_LO32, DAG);
723     SDValue GlobalBase = DAG.getNode(VEISD::GLOBAL_BASE_REG, DL, PtrVT);
724     SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, PtrVT, GlobalBase, HiLo);
725     return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), AbsAddr,
726                        MachinePointerInfo::getGOT(DAG.getMachineFunction()));
727   }
728 
729   // This is one of the absolute code models.
730   switch (getTargetMachine().getCodeModel()) {
731   default:
732     llvm_unreachable("Unsupported absolute code model");
733   case CodeModel::Small:
734   case CodeModel::Medium:
735   case CodeModel::Large:
736     // abs64.
737     return makeHiLoPair(Op, VEMCExpr::VK_VE_HI32, VEMCExpr::VK_VE_LO32, DAG);
738   }
739 }
740 
741 /// Custom Lower {
742 
743 SDValue VETargetLowering::LowerGlobalAddress(SDValue Op,
744                                              SelectionDAG &DAG) const {
745   return makeAddress(Op, DAG);
746 }
747 
748 SDValue VETargetLowering::LowerBlockAddress(SDValue Op,
749                                             SelectionDAG &DAG) const {
750   return makeAddress(Op, DAG);
751 }
752 
753 SDValue
754 VETargetLowering::LowerToTLSGeneralDynamicModel(SDValue Op,
755                                                 SelectionDAG &DAG) const {
756   SDLoc dl(Op);
757 
758   // Generate the following code:
759   //   t1: ch,glue = callseq_start t0, 0, 0
760   //   t2: i64,ch,glue = VEISD::GETTLSADDR t1, label, t1:1
761   //   t3: ch,glue = callseq_end t2, 0, 0, t2:2
762   //   t4: i64,ch,glue = CopyFromReg t3, Register:i64 $sx0, t3:1
763   SDValue Label = withTargetFlags(Op, 0, DAG);
764   EVT PtrVT = Op.getValueType();
765 
766   // Lowering the machine isd will make sure everything is in the right
767   // location.
768   SDValue Chain = DAG.getEntryNode();
769   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
770   const uint32_t *Mask = Subtarget->getRegisterInfo()->getCallPreservedMask(
771       DAG.getMachineFunction(), CallingConv::C);
772   Chain = DAG.getCALLSEQ_START(Chain, 64, 0, dl);
773   SDValue Args[] = {Chain, Label, DAG.getRegisterMask(Mask), Chain.getValue(1)};
774   Chain = DAG.getNode(VEISD::GETTLSADDR, dl, NodeTys, Args);
775   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(64, dl, true),
776                              DAG.getIntPtrConstant(0, dl, true),
777                              Chain.getValue(1), dl);
778   Chain = DAG.getCopyFromReg(Chain, dl, VE::SX0, PtrVT, Chain.getValue(1));
779 
780   // GETTLSADDR will be codegen'ed as call. Inform MFI that function has calls.
781   MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
782   MFI.setHasCalls(true);
783 
784   // Also generate code to prepare a GOT register if it is PIC.
785   if (isPositionIndependent()) {
786     MachineFunction &MF = DAG.getMachineFunction();
787     Subtarget->getInstrInfo()->getGlobalBaseReg(&MF);
788   }
789 
790   return Chain;
791 }
792 
793 SDValue VETargetLowering::LowerGlobalTLSAddress(SDValue Op,
794                                                 SelectionDAG &DAG) const {
795   // The current implementation of nld (2.26) doesn't allow local exec model
796   // code described in VE-tls_v1.1.pdf (*1) as its input. Instead, we always
797   // generate the general dynamic model code sequence.
798   //
799   // *1: https://www.nec.com/en/global/prod/hpc/aurora/document/VE-tls_v1.1.pdf
800   return LowerToTLSGeneralDynamicModel(Op, DAG);
801 }
802 
803 SDValue VETargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
804   MachineFunction &MF = DAG.getMachineFunction();
805   VEMachineFunctionInfo *FuncInfo = MF.getInfo<VEMachineFunctionInfo>();
806   auto PtrVT = getPointerTy(DAG.getDataLayout());
807 
808   // Need frame address to find the address of VarArgsFrameIndex.
809   MF.getFrameInfo().setFrameAddressIsTaken(true);
810 
811   // vastart just stores the address of the VarArgsFrameIndex slot into the
812   // memory location argument.
813   SDLoc DL(Op);
814   SDValue Offset =
815       DAG.getNode(ISD::ADD, DL, PtrVT, DAG.getRegister(VE::SX9, PtrVT),
816                   DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset(), DL));
817   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
818   return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1),
819                       MachinePointerInfo(SV));
820 }
821 
822 SDValue VETargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
823   SDNode *Node = Op.getNode();
824   EVT VT = Node->getValueType(0);
825   SDValue InChain = Node->getOperand(0);
826   SDValue VAListPtr = Node->getOperand(1);
827   EVT PtrVT = VAListPtr.getValueType();
828   const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
829   SDLoc DL(Node);
830   SDValue VAList =
831       DAG.getLoad(PtrVT, DL, InChain, VAListPtr, MachinePointerInfo(SV));
832   SDValue Chain = VAList.getValue(1);
833   SDValue NextPtr;
834 
835   if (VT == MVT::f32) {
836     // float --> need special handling like below.
837     //    0      4
838     //    +------+------+
839     //    | empty| float|
840     //    +------+------+
841     // Increment the pointer, VAList, by 8 to the next vaarg.
842     NextPtr =
843         DAG.getNode(ISD::ADD, DL, PtrVT, VAList, DAG.getIntPtrConstant(8, DL));
844     // Then, adjust VAList.
845     unsigned InternalOffset = 4;
846     VAList = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
847                          DAG.getConstant(InternalOffset, DL, PtrVT));
848   } else {
849     // Increment the pointer, VAList, by 8 to the next vaarg.
850     NextPtr =
851         DAG.getNode(ISD::ADD, DL, PtrVT, VAList, DAG.getIntPtrConstant(8, DL));
852   }
853 
854   // Store the incremented VAList to the legalized pointer.
855   InChain = DAG.getStore(Chain, DL, NextPtr, VAListPtr, MachinePointerInfo(SV));
856 
857   // Load the actual argument out of the pointer VAList.
858   // We can't count on greater alignment than the word size.
859   return DAG.getLoad(VT, DL, InChain, VAList, MachinePointerInfo(),
860                      std::min(PtrVT.getSizeInBits(), VT.getSizeInBits()) / 8);
861 }
862 
863 SDValue VETargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
864   switch (Op.getOpcode()) {
865   default:
866     llvm_unreachable("Should not custom lower this!");
867   case ISD::BlockAddress:
868     return LowerBlockAddress(Op, DAG);
869   case ISD::GlobalAddress:
870     return LowerGlobalAddress(Op, DAG);
871   case ISD::GlobalTLSAddress:
872     return LowerGlobalTLSAddress(Op, DAG);
873   case ISD::VASTART:
874     return LowerVASTART(Op, DAG);
875   case ISD::VAARG:
876     return LowerVAARG(Op, DAG);
877   }
878 }
879 /// } Custom Lower
880