1 //===-- PPCISelLowering.cpp - PPC 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 PPCISelLowering class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "PPCISelLowering.h"
14 #include "MCTargetDesc/PPCPredicates.h"
15 #include "PPC.h"
16 #include "PPCCCState.h"
17 #include "PPCCallingConv.h"
18 #include "PPCFrameLowering.h"
19 #include "PPCInstrInfo.h"
20 #include "PPCMachineFunctionInfo.h"
21 #include "PPCPerfectShuffle.h"
22 #include "PPCRegisterInfo.h"
23 #include "PPCSubtarget.h"
24 #include "PPCTargetMachine.h"
25 #include "llvm/ADT/APFloat.h"
26 #include "llvm/ADT/APInt.h"
27 #include "llvm/ADT/ArrayRef.h"
28 #include "llvm/ADT/DenseMap.h"
29 #include "llvm/ADT/None.h"
30 #include "llvm/ADT/STLExtras.h"
31 #include "llvm/ADT/SmallPtrSet.h"
32 #include "llvm/ADT/SmallSet.h"
33 #include "llvm/ADT/SmallVector.h"
34 #include "llvm/ADT/Statistic.h"
35 #include "llvm/ADT/StringRef.h"
36 #include "llvm/ADT/StringSwitch.h"
37 #include "llvm/CodeGen/CallingConvLower.h"
38 #include "llvm/CodeGen/ISDOpcodes.h"
39 #include "llvm/CodeGen/MachineBasicBlock.h"
40 #include "llvm/CodeGen/MachineFrameInfo.h"
41 #include "llvm/CodeGen/MachineFunction.h"
42 #include "llvm/CodeGen/MachineInstr.h"
43 #include "llvm/CodeGen/MachineInstrBuilder.h"
44 #include "llvm/CodeGen/MachineJumpTableInfo.h"
45 #include "llvm/CodeGen/MachineLoopInfo.h"
46 #include "llvm/CodeGen/MachineMemOperand.h"
47 #include "llvm/CodeGen/MachineModuleInfo.h"
48 #include "llvm/CodeGen/MachineOperand.h"
49 #include "llvm/CodeGen/MachineRegisterInfo.h"
50 #include "llvm/CodeGen/RuntimeLibcalls.h"
51 #include "llvm/CodeGen/SelectionDAG.h"
52 #include "llvm/CodeGen/SelectionDAGNodes.h"
53 #include "llvm/CodeGen/TargetInstrInfo.h"
54 #include "llvm/CodeGen/TargetLowering.h"
55 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
56 #include "llvm/CodeGen/TargetRegisterInfo.h"
57 #include "llvm/CodeGen/ValueTypes.h"
58 #include "llvm/IR/CallingConv.h"
59 #include "llvm/IR/Constant.h"
60 #include "llvm/IR/Constants.h"
61 #include "llvm/IR/DataLayout.h"
62 #include "llvm/IR/DebugLoc.h"
63 #include "llvm/IR/DerivedTypes.h"
64 #include "llvm/IR/Function.h"
65 #include "llvm/IR/GlobalValue.h"
66 #include "llvm/IR/IRBuilder.h"
67 #include "llvm/IR/Instructions.h"
68 #include "llvm/IR/Intrinsics.h"
69 #include "llvm/IR/IntrinsicsPowerPC.h"
70 #include "llvm/IR/Module.h"
71 #include "llvm/IR/Type.h"
72 #include "llvm/IR/Use.h"
73 #include "llvm/IR/Value.h"
74 #include "llvm/MC/MCContext.h"
75 #include "llvm/MC/MCExpr.h"
76 #include "llvm/MC/MCRegisterInfo.h"
77 #include "llvm/MC/MCSectionXCOFF.h"
78 #include "llvm/MC/MCSymbolXCOFF.h"
79 #include "llvm/Support/AtomicOrdering.h"
80 #include "llvm/Support/BranchProbability.h"
81 #include "llvm/Support/Casting.h"
82 #include "llvm/Support/CodeGen.h"
83 #include "llvm/Support/CommandLine.h"
84 #include "llvm/Support/Compiler.h"
85 #include "llvm/Support/Debug.h"
86 #include "llvm/Support/ErrorHandling.h"
87 #include "llvm/Support/Format.h"
88 #include "llvm/Support/KnownBits.h"
89 #include "llvm/Support/MachineValueType.h"
90 #include "llvm/Support/MathExtras.h"
91 #include "llvm/Support/raw_ostream.h"
92 #include "llvm/Target/TargetMachine.h"
93 #include "llvm/Target/TargetOptions.h"
94 #include <algorithm>
95 #include <cassert>
96 #include <cstdint>
97 #include <iterator>
98 #include <list>
99 #include <utility>
100 #include <vector>
101 
102 using namespace llvm;
103 
104 #define DEBUG_TYPE "ppc-lowering"
105 
106 static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
107 cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
108 
109 static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
110 cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
111 
112 static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
113 cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
114 
115 static cl::opt<bool> DisableSCO("disable-ppc-sco",
116 cl::desc("disable sibling call optimization on ppc"), cl::Hidden);
117 
118 static cl::opt<bool> DisableInnermostLoopAlign32("disable-ppc-innermost-loop-align32",
119 cl::desc("don't always align innermost loop to 32 bytes on ppc"), cl::Hidden);
120 
121 static cl::opt<bool> UseAbsoluteJumpTables("ppc-use-absolute-jumptables",
122 cl::desc("use absolute jump tables on ppc"), cl::Hidden);
123 
124 STATISTIC(NumTailCalls, "Number of tail calls");
125 STATISTIC(NumSiblingCalls, "Number of sibling calls");
126 STATISTIC(ShufflesHandledWithVPERM, "Number of shuffles lowered to a VPERM");
127 STATISTIC(NumDynamicAllocaProbed, "Number of dynamic stack allocation probed");
128 
129 static bool isNByteElemShuffleMask(ShuffleVectorSDNode *, unsigned, int);
130 
131 static SDValue widenVec(SelectionDAG &DAG, SDValue Vec, const SDLoc &dl);
132 
133 // FIXME: Remove this once the bug has been fixed!
134 extern cl::opt<bool> ANDIGlueBug;
135 
136 PPCTargetLowering::PPCTargetLowering(const PPCTargetMachine &TM,
137                                      const PPCSubtarget &STI)
138     : TargetLowering(TM), Subtarget(STI) {
139   // Initialize map that relates the PPC addressing modes to the computed flags
140   // of a load/store instruction. The map is used to determine the optimal
141   // addressing mode when selecting load and stores.
142   initializeAddrModeMap();
143   // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
144   // arguments are at least 4/8 bytes aligned.
145   bool isPPC64 = Subtarget.isPPC64();
146   setMinStackArgumentAlignment(isPPC64 ? Align(8) : Align(4));
147 
148   // Set up the register classes.
149   addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
150   if (!useSoftFloat()) {
151     if (hasSPE()) {
152       addRegisterClass(MVT::f32, &PPC::GPRCRegClass);
153       // EFPU2 APU only supports f32
154       if (!Subtarget.hasEFPU2())
155         addRegisterClass(MVT::f64, &PPC::SPERCRegClass);
156     } else {
157       addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
158       addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
159     }
160   }
161 
162   // Match BITREVERSE to customized fast code sequence in the td file.
163   setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
164   setOperationAction(ISD::BITREVERSE, MVT::i64, Legal);
165 
166   // Sub-word ATOMIC_CMP_SWAP need to ensure that the input is zero-extended.
167   setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
168 
169   // PowerPC has an i16 but no i8 (or i1) SEXTLOAD.
170   for (MVT VT : MVT::integer_valuetypes()) {
171     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
172     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Expand);
173   }
174 
175   if (Subtarget.isISA3_0()) {
176     setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Legal);
177     setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Legal);
178     setTruncStoreAction(MVT::f64, MVT::f16, Legal);
179     setTruncStoreAction(MVT::f32, MVT::f16, Legal);
180   } else {
181     // No extending loads from f16 or HW conversions back and forth.
182     setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand);
183     setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
184     setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
185     setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand);
186     setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
187     setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
188     setTruncStoreAction(MVT::f64, MVT::f16, Expand);
189     setTruncStoreAction(MVT::f32, MVT::f16, Expand);
190   }
191 
192   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
193 
194   // PowerPC has pre-inc load and store's.
195   setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
196   setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
197   setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
198   setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
199   setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
200   setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
201   setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
202   setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
203   setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
204   setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
205   if (!Subtarget.hasSPE()) {
206     setIndexedLoadAction(ISD::PRE_INC, MVT::f32, Legal);
207     setIndexedLoadAction(ISD::PRE_INC, MVT::f64, Legal);
208     setIndexedStoreAction(ISD::PRE_INC, MVT::f32, Legal);
209     setIndexedStoreAction(ISD::PRE_INC, MVT::f64, Legal);
210   }
211 
212   // PowerPC uses ADDC/ADDE/SUBC/SUBE to propagate carry.
213   const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 };
214   for (MVT VT : ScalarIntVTs) {
215     setOperationAction(ISD::ADDC, VT, Legal);
216     setOperationAction(ISD::ADDE, VT, Legal);
217     setOperationAction(ISD::SUBC, VT, Legal);
218     setOperationAction(ISD::SUBE, VT, Legal);
219   }
220 
221   if (Subtarget.useCRBits()) {
222     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
223 
224     if (isPPC64 || Subtarget.hasFPCVT()) {
225       setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::i1, Promote);
226       AddPromotedToType(ISD::STRICT_SINT_TO_FP, MVT::i1,
227                         isPPC64 ? MVT::i64 : MVT::i32);
228       setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::i1, Promote);
229       AddPromotedToType(ISD::STRICT_UINT_TO_FP, MVT::i1,
230                         isPPC64 ? MVT::i64 : MVT::i32);
231 
232       setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
233       AddPromotedToType (ISD::SINT_TO_FP, MVT::i1,
234                          isPPC64 ? MVT::i64 : MVT::i32);
235       setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
236       AddPromotedToType(ISD::UINT_TO_FP, MVT::i1,
237                         isPPC64 ? MVT::i64 : MVT::i32);
238 
239       setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::i1, Promote);
240       AddPromotedToType(ISD::STRICT_FP_TO_SINT, MVT::i1,
241                         isPPC64 ? MVT::i64 : MVT::i32);
242       setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::i1, Promote);
243       AddPromotedToType(ISD::STRICT_FP_TO_UINT, MVT::i1,
244                         isPPC64 ? MVT::i64 : MVT::i32);
245 
246       setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
247       AddPromotedToType(ISD::FP_TO_SINT, MVT::i1,
248                         isPPC64 ? MVT::i64 : MVT::i32);
249       setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
250       AddPromotedToType(ISD::FP_TO_UINT, MVT::i1,
251                         isPPC64 ? MVT::i64 : MVT::i32);
252     } else {
253       setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::i1, Custom);
254       setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::i1, Custom);
255       setOperationAction(ISD::SINT_TO_FP, MVT::i1, Custom);
256       setOperationAction(ISD::UINT_TO_FP, MVT::i1, Custom);
257     }
258 
259     // PowerPC does not support direct load/store of condition registers.
260     setOperationAction(ISD::LOAD, MVT::i1, Custom);
261     setOperationAction(ISD::STORE, MVT::i1, Custom);
262 
263     // FIXME: Remove this once the ANDI glue bug is fixed:
264     if (ANDIGlueBug)
265       setOperationAction(ISD::TRUNCATE, MVT::i1, Custom);
266 
267     for (MVT VT : MVT::integer_valuetypes()) {
268       setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
269       setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
270       setTruncStoreAction(VT, MVT::i1, Expand);
271     }
272 
273     addRegisterClass(MVT::i1, &PPC::CRBITRCRegClass);
274   }
275 
276   // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
277   // PPC (the libcall is not available).
278   setOperationAction(ISD::FP_TO_SINT, MVT::ppcf128, Custom);
279   setOperationAction(ISD::FP_TO_UINT, MVT::ppcf128, Custom);
280   setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::ppcf128, Custom);
281   setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::ppcf128, Custom);
282 
283   // We do not currently implement these libm ops for PowerPC.
284   setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
285   setOperationAction(ISD::FCEIL,  MVT::ppcf128, Expand);
286   setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
287   setOperationAction(ISD::FRINT,  MVT::ppcf128, Expand);
288   setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
289   setOperationAction(ISD::FREM, MVT::ppcf128, Expand);
290 
291   // PowerPC has no SREM/UREM instructions unless we are on P9
292   // On P9 we may use a hardware instruction to compute the remainder.
293   // When the result of both the remainder and the division is required it is
294   // more efficient to compute the remainder from the result of the division
295   // rather than use the remainder instruction. The instructions are legalized
296   // directly because the DivRemPairsPass performs the transformation at the IR
297   // level.
298   if (Subtarget.isISA3_0()) {
299     setOperationAction(ISD::SREM, MVT::i32, Legal);
300     setOperationAction(ISD::UREM, MVT::i32, Legal);
301     setOperationAction(ISD::SREM, MVT::i64, Legal);
302     setOperationAction(ISD::UREM, MVT::i64, Legal);
303   } else {
304     setOperationAction(ISD::SREM, MVT::i32, Expand);
305     setOperationAction(ISD::UREM, MVT::i32, Expand);
306     setOperationAction(ISD::SREM, MVT::i64, Expand);
307     setOperationAction(ISD::UREM, MVT::i64, Expand);
308   }
309 
310   // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
311   setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
312   setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
313   setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
314   setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
315   setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
316   setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
317   setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
318   setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
319 
320   // Handle constrained floating-point operations of scalar.
321   // TODO: Handle SPE specific operation.
322   setOperationAction(ISD::STRICT_FADD, MVT::f32, Legal);
323   setOperationAction(ISD::STRICT_FSUB, MVT::f32, Legal);
324   setOperationAction(ISD::STRICT_FMUL, MVT::f32, Legal);
325   setOperationAction(ISD::STRICT_FDIV, MVT::f32, Legal);
326   setOperationAction(ISD::STRICT_FP_ROUND, MVT::f32, Legal);
327 
328   setOperationAction(ISD::STRICT_FADD, MVT::f64, Legal);
329   setOperationAction(ISD::STRICT_FSUB, MVT::f64, Legal);
330   setOperationAction(ISD::STRICT_FMUL, MVT::f64, Legal);
331   setOperationAction(ISD::STRICT_FDIV, MVT::f64, Legal);
332 
333   if (!Subtarget.hasSPE()) {
334     setOperationAction(ISD::STRICT_FMA, MVT::f32, Legal);
335     setOperationAction(ISD::STRICT_FMA, MVT::f64, Legal);
336   }
337 
338   if (Subtarget.hasVSX()) {
339     setOperationAction(ISD::STRICT_FRINT, MVT::f32, Legal);
340     setOperationAction(ISD::STRICT_FRINT, MVT::f64, Legal);
341   }
342 
343   if (Subtarget.hasFSQRT()) {
344     setOperationAction(ISD::STRICT_FSQRT, MVT::f32, Legal);
345     setOperationAction(ISD::STRICT_FSQRT, MVT::f64, Legal);
346   }
347 
348   if (Subtarget.hasFPRND()) {
349     setOperationAction(ISD::STRICT_FFLOOR, MVT::f32, Legal);
350     setOperationAction(ISD::STRICT_FCEIL,  MVT::f32, Legal);
351     setOperationAction(ISD::STRICT_FTRUNC, MVT::f32, Legal);
352     setOperationAction(ISD::STRICT_FROUND, MVT::f32, Legal);
353 
354     setOperationAction(ISD::STRICT_FFLOOR, MVT::f64, Legal);
355     setOperationAction(ISD::STRICT_FCEIL,  MVT::f64, Legal);
356     setOperationAction(ISD::STRICT_FTRUNC, MVT::f64, Legal);
357     setOperationAction(ISD::STRICT_FROUND, MVT::f64, Legal);
358   }
359 
360   // We don't support sin/cos/sqrt/fmod/pow
361   setOperationAction(ISD::FSIN , MVT::f64, Expand);
362   setOperationAction(ISD::FCOS , MVT::f64, Expand);
363   setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
364   setOperationAction(ISD::FREM , MVT::f64, Expand);
365   setOperationAction(ISD::FPOW , MVT::f64, Expand);
366   setOperationAction(ISD::FSIN , MVT::f32, Expand);
367   setOperationAction(ISD::FCOS , MVT::f32, Expand);
368   setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
369   setOperationAction(ISD::FREM , MVT::f32, Expand);
370   setOperationAction(ISD::FPOW , MVT::f32, Expand);
371   if (Subtarget.hasSPE()) {
372     setOperationAction(ISD::FMA  , MVT::f64, Expand);
373     setOperationAction(ISD::FMA  , MVT::f32, Expand);
374   } else {
375     setOperationAction(ISD::FMA  , MVT::f64, Legal);
376     setOperationAction(ISD::FMA  , MVT::f32, Legal);
377   }
378 
379   if (Subtarget.hasSPE())
380     setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
381 
382   setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
383 
384   // If we're enabling GP optimizations, use hardware square root
385   if (!Subtarget.hasFSQRT() &&
386       !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTE() &&
387         Subtarget.hasFRE()))
388     setOperationAction(ISD::FSQRT, MVT::f64, Expand);
389 
390   if (!Subtarget.hasFSQRT() &&
391       !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTES() &&
392         Subtarget.hasFRES()))
393     setOperationAction(ISD::FSQRT, MVT::f32, Expand);
394 
395   if (Subtarget.hasFCPSGN()) {
396     setOperationAction(ISD::FCOPYSIGN, MVT::f64, Legal);
397     setOperationAction(ISD::FCOPYSIGN, MVT::f32, Legal);
398   } else {
399     setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
400     setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
401   }
402 
403   if (Subtarget.hasFPRND()) {
404     setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
405     setOperationAction(ISD::FCEIL,  MVT::f64, Legal);
406     setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
407     setOperationAction(ISD::FROUND, MVT::f64, Legal);
408 
409     setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
410     setOperationAction(ISD::FCEIL,  MVT::f32, Legal);
411     setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
412     setOperationAction(ISD::FROUND, MVT::f32, Legal);
413   }
414 
415   // PowerPC does not have BSWAP, but we can use vector BSWAP instruction xxbrd
416   // to speed up scalar BSWAP64.
417   // CTPOP or CTTZ were introduced in P8/P9 respectively
418   setOperationAction(ISD::BSWAP, MVT::i32  , Expand);
419   if (Subtarget.hasP9Vector() && Subtarget.isPPC64())
420     setOperationAction(ISD::BSWAP, MVT::i64  , Custom);
421   else
422     setOperationAction(ISD::BSWAP, MVT::i64  , Expand);
423   if (Subtarget.isISA3_0()) {
424     setOperationAction(ISD::CTTZ , MVT::i32  , Legal);
425     setOperationAction(ISD::CTTZ , MVT::i64  , Legal);
426   } else {
427     setOperationAction(ISD::CTTZ , MVT::i32  , Expand);
428     setOperationAction(ISD::CTTZ , MVT::i64  , Expand);
429   }
430 
431   if (Subtarget.hasPOPCNTD() == PPCSubtarget::POPCNTD_Fast) {
432     setOperationAction(ISD::CTPOP, MVT::i32  , Legal);
433     setOperationAction(ISD::CTPOP, MVT::i64  , Legal);
434   } else {
435     setOperationAction(ISD::CTPOP, MVT::i32  , Expand);
436     setOperationAction(ISD::CTPOP, MVT::i64  , Expand);
437   }
438 
439   // PowerPC does not have ROTR
440   setOperationAction(ISD::ROTR, MVT::i32   , Expand);
441   setOperationAction(ISD::ROTR, MVT::i64   , Expand);
442 
443   if (!Subtarget.useCRBits()) {
444     // PowerPC does not have Select
445     setOperationAction(ISD::SELECT, MVT::i32, Expand);
446     setOperationAction(ISD::SELECT, MVT::i64, Expand);
447     setOperationAction(ISD::SELECT, MVT::f32, Expand);
448     setOperationAction(ISD::SELECT, MVT::f64, Expand);
449   }
450 
451   // PowerPC wants to turn select_cc of FP into fsel when possible.
452   setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
453   setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
454 
455   // PowerPC wants to optimize integer setcc a bit
456   if (!Subtarget.useCRBits())
457     setOperationAction(ISD::SETCC, MVT::i32, Custom);
458 
459   if (Subtarget.hasFPU()) {
460     setOperationAction(ISD::STRICT_FSETCC, MVT::f32, Legal);
461     setOperationAction(ISD::STRICT_FSETCC, MVT::f64, Legal);
462     setOperationAction(ISD::STRICT_FSETCC, MVT::f128, Legal);
463 
464     setOperationAction(ISD::STRICT_FSETCCS, MVT::f32, Legal);
465     setOperationAction(ISD::STRICT_FSETCCS, MVT::f64, Legal);
466     setOperationAction(ISD::STRICT_FSETCCS, MVT::f128, Legal);
467   }
468 
469   // PowerPC does not have BRCOND which requires SetCC
470   if (!Subtarget.useCRBits())
471     setOperationAction(ISD::BRCOND, MVT::Other, Expand);
472 
473   setOperationAction(ISD::BR_JT,  MVT::Other, Expand);
474 
475   if (Subtarget.hasSPE()) {
476     // SPE has built-in conversions
477     setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::i32, Legal);
478     setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::i32, Legal);
479     setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::i32, Legal);
480     setOperationAction(ISD::FP_TO_SINT, MVT::i32, Legal);
481     setOperationAction(ISD::SINT_TO_FP, MVT::i32, Legal);
482     setOperationAction(ISD::UINT_TO_FP, MVT::i32, Legal);
483 
484     // SPE supports signaling compare of f32/f64.
485     setOperationAction(ISD::STRICT_FSETCCS, MVT::f32, Legal);
486     setOperationAction(ISD::STRICT_FSETCCS, MVT::f64, Legal);
487   } else {
488     // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
489     setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::i32, Custom);
490     setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
491 
492     // PowerPC does not have [U|S]INT_TO_FP
493     setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::i32, Expand);
494     setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::i32, Expand);
495     setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
496     setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
497   }
498 
499   if (Subtarget.hasDirectMove() && isPPC64) {
500     setOperationAction(ISD::BITCAST, MVT::f32, Legal);
501     setOperationAction(ISD::BITCAST, MVT::i32, Legal);
502     setOperationAction(ISD::BITCAST, MVT::i64, Legal);
503     setOperationAction(ISD::BITCAST, MVT::f64, Legal);
504     if (TM.Options.UnsafeFPMath) {
505       setOperationAction(ISD::LRINT, MVT::f64, Legal);
506       setOperationAction(ISD::LRINT, MVT::f32, Legal);
507       setOperationAction(ISD::LLRINT, MVT::f64, Legal);
508       setOperationAction(ISD::LLRINT, MVT::f32, Legal);
509       setOperationAction(ISD::LROUND, MVT::f64, Legal);
510       setOperationAction(ISD::LROUND, MVT::f32, Legal);
511       setOperationAction(ISD::LLROUND, MVT::f64, Legal);
512       setOperationAction(ISD::LLROUND, MVT::f32, Legal);
513     }
514   } else {
515     setOperationAction(ISD::BITCAST, MVT::f32, Expand);
516     setOperationAction(ISD::BITCAST, MVT::i32, Expand);
517     setOperationAction(ISD::BITCAST, MVT::i64, Expand);
518     setOperationAction(ISD::BITCAST, MVT::f64, Expand);
519   }
520 
521   // We cannot sextinreg(i1).  Expand to shifts.
522   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
523 
524   // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
525   // SjLj exception handling but a light-weight setjmp/longjmp replacement to
526   // support continuation, user-level threading, and etc.. As a result, no
527   // other SjLj exception interfaces are implemented and please don't build
528   // your own exception handling based on them.
529   // LLVM/Clang supports zero-cost DWARF exception handling.
530   setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
531   setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
532 
533   // We want to legalize GlobalAddress and ConstantPool nodes into the
534   // appropriate instructions to materialize the address.
535   setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
536   setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
537   setOperationAction(ISD::BlockAddress,  MVT::i32, Custom);
538   setOperationAction(ISD::ConstantPool,  MVT::i32, Custom);
539   setOperationAction(ISD::JumpTable,     MVT::i32, Custom);
540   setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
541   setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
542   setOperationAction(ISD::BlockAddress,  MVT::i64, Custom);
543   setOperationAction(ISD::ConstantPool,  MVT::i64, Custom);
544   setOperationAction(ISD::JumpTable,     MVT::i64, Custom);
545 
546   // TRAP is legal.
547   setOperationAction(ISD::TRAP, MVT::Other, Legal);
548 
549   // TRAMPOLINE is custom lowered.
550   setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
551   setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
552 
553   // VASTART needs to be custom lowered to use the VarArgsFrameIndex
554   setOperationAction(ISD::VASTART           , MVT::Other, Custom);
555 
556   if (Subtarget.is64BitELFABI()) {
557     // VAARG always uses double-word chunks, so promote anything smaller.
558     setOperationAction(ISD::VAARG, MVT::i1, Promote);
559     AddPromotedToType(ISD::VAARG, MVT::i1, MVT::i64);
560     setOperationAction(ISD::VAARG, MVT::i8, Promote);
561     AddPromotedToType(ISD::VAARG, MVT::i8, MVT::i64);
562     setOperationAction(ISD::VAARG, MVT::i16, Promote);
563     AddPromotedToType(ISD::VAARG, MVT::i16, MVT::i64);
564     setOperationAction(ISD::VAARG, MVT::i32, Promote);
565     AddPromotedToType(ISD::VAARG, MVT::i32, MVT::i64);
566     setOperationAction(ISD::VAARG, MVT::Other, Expand);
567   } else if (Subtarget.is32BitELFABI()) {
568     // VAARG is custom lowered with the 32-bit SVR4 ABI.
569     setOperationAction(ISD::VAARG, MVT::Other, Custom);
570     setOperationAction(ISD::VAARG, MVT::i64, Custom);
571   } else
572     setOperationAction(ISD::VAARG, MVT::Other, Expand);
573 
574   // VACOPY is custom lowered with the 32-bit SVR4 ABI.
575   if (Subtarget.is32BitELFABI())
576     setOperationAction(ISD::VACOPY            , MVT::Other, Custom);
577   else
578     setOperationAction(ISD::VACOPY            , MVT::Other, Expand);
579 
580   // Use the default implementation.
581   setOperationAction(ISD::VAEND             , MVT::Other, Expand);
582   setOperationAction(ISD::STACKSAVE         , MVT::Other, Expand);
583   setOperationAction(ISD::STACKRESTORE      , MVT::Other, Custom);
584   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32  , Custom);
585   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64  , Custom);
586   setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, MVT::i32, Custom);
587   setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, MVT::i64, Custom);
588   setOperationAction(ISD::EH_DWARF_CFA, MVT::i32, Custom);
589   setOperationAction(ISD::EH_DWARF_CFA, MVT::i64, Custom);
590 
591   // We want to custom lower some of our intrinsics.
592   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
593 
594   // To handle counter-based loop conditions.
595   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
596 
597   setOperationAction(ISD::INTRINSIC_VOID, MVT::i8, Custom);
598   setOperationAction(ISD::INTRINSIC_VOID, MVT::i16, Custom);
599   setOperationAction(ISD::INTRINSIC_VOID, MVT::i32, Custom);
600   setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
601 
602   // Comparisons that require checking two conditions.
603   if (Subtarget.hasSPE()) {
604     setCondCodeAction(ISD::SETO, MVT::f32, Expand);
605     setCondCodeAction(ISD::SETO, MVT::f64, Expand);
606     setCondCodeAction(ISD::SETUO, MVT::f32, Expand);
607     setCondCodeAction(ISD::SETUO, MVT::f64, Expand);
608   }
609   setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
610   setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
611   setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
612   setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
613   setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
614   setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
615   setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
616   setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
617   setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
618   setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
619   setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
620   setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
621 
622   setOperationAction(ISD::STRICT_FP_EXTEND, MVT::f32, Legal);
623   setOperationAction(ISD::STRICT_FP_EXTEND, MVT::f64, Legal);
624 
625   if (Subtarget.has64BitSupport()) {
626     // They also have instructions for converting between i64 and fp.
627     setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::i64, Custom);
628     setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::i64, Expand);
629     setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::i64, Custom);
630     setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::i64, Expand);
631     setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
632     setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
633     setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
634     setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
635     // This is just the low 32 bits of a (signed) fp->i64 conversion.
636     // We cannot do this with Promote because i64 is not a legal type.
637     setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::i32, Custom);
638     setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
639 
640     if (Subtarget.hasLFIWAX() || Subtarget.isPPC64()) {
641       setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
642       setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::i32, Custom);
643     }
644   } else {
645     // PowerPC does not have FP_TO_UINT on 32-bit implementations.
646     if (Subtarget.hasSPE()) {
647       setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::i32, Legal);
648       setOperationAction(ISD::FP_TO_UINT, MVT::i32, Legal);
649     } else {
650       setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::i32, Expand);
651       setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
652     }
653   }
654 
655   // With the instructions enabled under FPCVT, we can do everything.
656   if (Subtarget.hasFPCVT()) {
657     if (Subtarget.has64BitSupport()) {
658       setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::i64, Custom);
659       setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::i64, Custom);
660       setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::i64, Custom);
661       setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::i64, Custom);
662       setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
663       setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
664       setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
665       setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
666     }
667 
668     setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::i32, Custom);
669     setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::i32, Custom);
670     setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::i32, Custom);
671     setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::i32, Custom);
672     setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
673     setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
674     setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
675     setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
676   }
677 
678   if (Subtarget.use64BitRegs()) {
679     // 64-bit PowerPC implementations can support i64 types directly
680     addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
681     // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
682     setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
683     // 64-bit PowerPC wants to expand i128 shifts itself.
684     setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
685     setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
686     setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
687   } else {
688     // 32-bit PowerPC wants to expand i64 shifts itself.
689     setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
690     setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
691     setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
692   }
693 
694   // PowerPC has better expansions for funnel shifts than the generic
695   // TargetLowering::expandFunnelShift.
696   if (Subtarget.has64BitSupport()) {
697     setOperationAction(ISD::FSHL, MVT::i64, Custom);
698     setOperationAction(ISD::FSHR, MVT::i64, Custom);
699   }
700   setOperationAction(ISD::FSHL, MVT::i32, Custom);
701   setOperationAction(ISD::FSHR, MVT::i32, Custom);
702 
703   if (Subtarget.hasVSX()) {
704     setOperationAction(ISD::FMAXNUM_IEEE, MVT::f64, Legal);
705     setOperationAction(ISD::FMAXNUM_IEEE, MVT::f32, Legal);
706     setOperationAction(ISD::FMINNUM_IEEE, MVT::f64, Legal);
707     setOperationAction(ISD::FMINNUM_IEEE, MVT::f32, Legal);
708   }
709 
710   if (Subtarget.hasAltivec()) {
711     for (MVT VT : { MVT::v16i8, MVT::v8i16, MVT::v4i32 }) {
712       setOperationAction(ISD::SADDSAT, VT, Legal);
713       setOperationAction(ISD::SSUBSAT, VT, Legal);
714       setOperationAction(ISD::UADDSAT, VT, Legal);
715       setOperationAction(ISD::USUBSAT, VT, Legal);
716     }
717     // First set operation action for all vector types to expand. Then we
718     // will selectively turn on ones that can be effectively codegen'd.
719     for (MVT VT : MVT::fixedlen_vector_valuetypes()) {
720       // add/sub are legal for all supported vector VT's.
721       setOperationAction(ISD::ADD, VT, Legal);
722       setOperationAction(ISD::SUB, VT, Legal);
723 
724       // For v2i64, these are only valid with P8Vector. This is corrected after
725       // the loop.
726       if (VT.getSizeInBits() <= 128 && VT.getScalarSizeInBits() <= 64) {
727         setOperationAction(ISD::SMAX, VT, Legal);
728         setOperationAction(ISD::SMIN, VT, Legal);
729         setOperationAction(ISD::UMAX, VT, Legal);
730         setOperationAction(ISD::UMIN, VT, Legal);
731       }
732       else {
733         setOperationAction(ISD::SMAX, VT, Expand);
734         setOperationAction(ISD::SMIN, VT, Expand);
735         setOperationAction(ISD::UMAX, VT, Expand);
736         setOperationAction(ISD::UMIN, VT, Expand);
737       }
738 
739       if (Subtarget.hasVSX()) {
740         setOperationAction(ISD::FMAXNUM, VT, Legal);
741         setOperationAction(ISD::FMINNUM, VT, Legal);
742       }
743 
744       // Vector instructions introduced in P8
745       if (Subtarget.hasP8Altivec() && (VT.SimpleTy != MVT::v1i128)) {
746         setOperationAction(ISD::CTPOP, VT, Legal);
747         setOperationAction(ISD::CTLZ, VT, Legal);
748       }
749       else {
750         setOperationAction(ISD::CTPOP, VT, Expand);
751         setOperationAction(ISD::CTLZ, VT, Expand);
752       }
753 
754       // Vector instructions introduced in P9
755       if (Subtarget.hasP9Altivec() && (VT.SimpleTy != MVT::v1i128))
756         setOperationAction(ISD::CTTZ, VT, Legal);
757       else
758         setOperationAction(ISD::CTTZ, VT, Expand);
759 
760       // We promote all shuffles to v16i8.
761       setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
762       AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
763 
764       // We promote all non-typed operations to v4i32.
765       setOperationAction(ISD::AND   , VT, Promote);
766       AddPromotedToType (ISD::AND   , VT, MVT::v4i32);
767       setOperationAction(ISD::OR    , VT, Promote);
768       AddPromotedToType (ISD::OR    , VT, MVT::v4i32);
769       setOperationAction(ISD::XOR   , VT, Promote);
770       AddPromotedToType (ISD::XOR   , VT, MVT::v4i32);
771       setOperationAction(ISD::LOAD  , VT, Promote);
772       AddPromotedToType (ISD::LOAD  , VT, MVT::v4i32);
773       setOperationAction(ISD::SELECT, VT, Promote);
774       AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
775       setOperationAction(ISD::VSELECT, VT, Legal);
776       setOperationAction(ISD::SELECT_CC, VT, Promote);
777       AddPromotedToType (ISD::SELECT_CC, VT, MVT::v4i32);
778       setOperationAction(ISD::STORE, VT, Promote);
779       AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
780 
781       // No other operations are legal.
782       setOperationAction(ISD::MUL , VT, Expand);
783       setOperationAction(ISD::SDIV, VT, Expand);
784       setOperationAction(ISD::SREM, VT, Expand);
785       setOperationAction(ISD::UDIV, VT, Expand);
786       setOperationAction(ISD::UREM, VT, Expand);
787       setOperationAction(ISD::FDIV, VT, Expand);
788       setOperationAction(ISD::FREM, VT, Expand);
789       setOperationAction(ISD::FNEG, VT, Expand);
790       setOperationAction(ISD::FSQRT, VT, Expand);
791       setOperationAction(ISD::FLOG, VT, Expand);
792       setOperationAction(ISD::FLOG10, VT, Expand);
793       setOperationAction(ISD::FLOG2, VT, Expand);
794       setOperationAction(ISD::FEXP, VT, Expand);
795       setOperationAction(ISD::FEXP2, VT, Expand);
796       setOperationAction(ISD::FSIN, VT, Expand);
797       setOperationAction(ISD::FCOS, VT, Expand);
798       setOperationAction(ISD::FABS, VT, Expand);
799       setOperationAction(ISD::FFLOOR, VT, Expand);
800       setOperationAction(ISD::FCEIL,  VT, Expand);
801       setOperationAction(ISD::FTRUNC, VT, Expand);
802       setOperationAction(ISD::FRINT,  VT, Expand);
803       setOperationAction(ISD::FNEARBYINT, VT, Expand);
804       setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
805       setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
806       setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
807       setOperationAction(ISD::MULHU, VT, Expand);
808       setOperationAction(ISD::MULHS, VT, Expand);
809       setOperationAction(ISD::UMUL_LOHI, VT, Expand);
810       setOperationAction(ISD::SMUL_LOHI, VT, Expand);
811       setOperationAction(ISD::UDIVREM, VT, Expand);
812       setOperationAction(ISD::SDIVREM, VT, Expand);
813       setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
814       setOperationAction(ISD::FPOW, VT, Expand);
815       setOperationAction(ISD::BSWAP, VT, Expand);
816       setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
817       setOperationAction(ISD::ROTL, VT, Expand);
818       setOperationAction(ISD::ROTR, VT, Expand);
819 
820       for (MVT InnerVT : MVT::fixedlen_vector_valuetypes()) {
821         setTruncStoreAction(VT, InnerVT, Expand);
822         setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
823         setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
824         setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
825       }
826     }
827     setOperationAction(ISD::SELECT_CC, MVT::v4i32, Expand);
828     if (!Subtarget.hasP8Vector()) {
829       setOperationAction(ISD::SMAX, MVT::v2i64, Expand);
830       setOperationAction(ISD::SMIN, MVT::v2i64, Expand);
831       setOperationAction(ISD::UMAX, MVT::v2i64, Expand);
832       setOperationAction(ISD::UMIN, MVT::v2i64, Expand);
833     }
834 
835     // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
836     // with merges, splats, etc.
837     setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
838 
839     // Vector truncates to sub-word integer that fit in an Altivec/VSX register
840     // are cheap, so handle them before they get expanded to scalar.
841     setOperationAction(ISD::TRUNCATE, MVT::v8i8, Custom);
842     setOperationAction(ISD::TRUNCATE, MVT::v4i8, Custom);
843     setOperationAction(ISD::TRUNCATE, MVT::v2i8, Custom);
844     setOperationAction(ISD::TRUNCATE, MVT::v4i16, Custom);
845     setOperationAction(ISD::TRUNCATE, MVT::v2i16, Custom);
846 
847     setOperationAction(ISD::AND   , MVT::v4i32, Legal);
848     setOperationAction(ISD::OR    , MVT::v4i32, Legal);
849     setOperationAction(ISD::XOR   , MVT::v4i32, Legal);
850     setOperationAction(ISD::LOAD  , MVT::v4i32, Legal);
851     setOperationAction(ISD::SELECT, MVT::v4i32,
852                        Subtarget.useCRBits() ? Legal : Expand);
853     setOperationAction(ISD::STORE , MVT::v4i32, Legal);
854     setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::v4i32, Legal);
855     setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::v4i32, Legal);
856     setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v4i32, Legal);
857     setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v4i32, Legal);
858     setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
859     setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
860     setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
861     setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
862     setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
863     setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
864     setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
865     setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
866 
867     // Custom lowering ROTL v1i128 to VECTOR_SHUFFLE v16i8.
868     setOperationAction(ISD::ROTL, MVT::v1i128, Custom);
869     // With hasAltivec set, we can lower ISD::ROTL to vrl(b|h|w).
870     if (Subtarget.hasAltivec())
871       for (auto VT : {MVT::v4i32, MVT::v8i16, MVT::v16i8})
872         setOperationAction(ISD::ROTL, VT, Legal);
873     // With hasP8Altivec set, we can lower ISD::ROTL to vrld.
874     if (Subtarget.hasP8Altivec())
875       setOperationAction(ISD::ROTL, MVT::v2i64, Legal);
876 
877     addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
878     addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
879     addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
880     addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
881 
882     setOperationAction(ISD::MUL, MVT::v4f32, Legal);
883     setOperationAction(ISD::FMA, MVT::v4f32, Legal);
884 
885     if (Subtarget.hasVSX()) {
886       setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
887       setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
888       setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
889     }
890 
891     if (Subtarget.hasP8Altivec())
892       setOperationAction(ISD::MUL, MVT::v4i32, Legal);
893     else
894       setOperationAction(ISD::MUL, MVT::v4i32, Custom);
895 
896     if (Subtarget.isISA3_1()) {
897       setOperationAction(ISD::MUL, MVT::v2i64, Legal);
898       setOperationAction(ISD::MULHS, MVT::v2i64, Legal);
899       setOperationAction(ISD::MULHU, MVT::v2i64, Legal);
900       setOperationAction(ISD::MULHS, MVT::v4i32, Legal);
901       setOperationAction(ISD::MULHU, MVT::v4i32, Legal);
902       setOperationAction(ISD::UDIV, MVT::v2i64, Legal);
903       setOperationAction(ISD::SDIV, MVT::v2i64, Legal);
904       setOperationAction(ISD::UDIV, MVT::v4i32, Legal);
905       setOperationAction(ISD::SDIV, MVT::v4i32, Legal);
906       setOperationAction(ISD::UREM, MVT::v2i64, Legal);
907       setOperationAction(ISD::SREM, MVT::v2i64, Legal);
908       setOperationAction(ISD::UREM, MVT::v4i32, Legal);
909       setOperationAction(ISD::SREM, MVT::v4i32, Legal);
910       setOperationAction(ISD::UREM, MVT::v1i128, Legal);
911       setOperationAction(ISD::SREM, MVT::v1i128, Legal);
912       setOperationAction(ISD::UDIV, MVT::v1i128, Legal);
913       setOperationAction(ISD::SDIV, MVT::v1i128, Legal);
914       setOperationAction(ISD::ROTL, MVT::v1i128, Legal);
915     }
916 
917     setOperationAction(ISD::MUL, MVT::v8i16, Legal);
918     setOperationAction(ISD::MUL, MVT::v16i8, Custom);
919 
920     setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
921     setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
922 
923     setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
924     setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
925     setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
926     setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
927 
928     // Altivec does not contain unordered floating-point compare instructions
929     setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
930     setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
931     setCondCodeAction(ISD::SETO,   MVT::v4f32, Expand);
932     setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
933 
934     if (Subtarget.hasVSX()) {
935       setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
936       setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal);
937       if (Subtarget.hasP8Vector()) {
938         setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
939         setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal);
940       }
941       if (Subtarget.hasDirectMove() && isPPC64) {
942         setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Legal);
943         setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Legal);
944         setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Legal);
945         setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Legal);
946         setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Legal);
947         setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Legal);
948         setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
949         setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
950       }
951       setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal);
952 
953       // The nearbyint variants are not allowed to raise the inexact exception
954       // so we can only code-gen them with unsafe math.
955       if (TM.Options.UnsafeFPMath) {
956         setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
957         setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
958       }
959 
960       setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
961       setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
962       setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
963       setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
964       setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
965       setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
966       setOperationAction(ISD::FROUND, MVT::f64, Legal);
967       setOperationAction(ISD::FRINT, MVT::f64, Legal);
968 
969       setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
970       setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
971       setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
972       setOperationAction(ISD::FROUND, MVT::f32, Legal);
973       setOperationAction(ISD::FRINT, MVT::f32, Legal);
974 
975       setOperationAction(ISD::MUL, MVT::v2f64, Legal);
976       setOperationAction(ISD::FMA, MVT::v2f64, Legal);
977 
978       setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
979       setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
980 
981       // Share the Altivec comparison restrictions.
982       setCondCodeAction(ISD::SETUO, MVT::v2f64, Expand);
983       setCondCodeAction(ISD::SETUEQ, MVT::v2f64, Expand);
984       setCondCodeAction(ISD::SETO,   MVT::v2f64, Expand);
985       setCondCodeAction(ISD::SETONE, MVT::v2f64, Expand);
986 
987       setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
988       setOperationAction(ISD::STORE, MVT::v2f64, Legal);
989 
990       setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Legal);
991 
992       if (Subtarget.hasP8Vector())
993         addRegisterClass(MVT::f32, &PPC::VSSRCRegClass);
994 
995       addRegisterClass(MVT::f64, &PPC::VSFRCRegClass);
996 
997       addRegisterClass(MVT::v4i32, &PPC::VSRCRegClass);
998       addRegisterClass(MVT::v4f32, &PPC::VSRCRegClass);
999       addRegisterClass(MVT::v2f64, &PPC::VSRCRegClass);
1000 
1001       if (Subtarget.hasP8Altivec()) {
1002         setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1003         setOperationAction(ISD::SRA, MVT::v2i64, Legal);
1004         setOperationAction(ISD::SRL, MVT::v2i64, Legal);
1005 
1006         // 128 bit shifts can be accomplished via 3 instructions for SHL and
1007         // SRL, but not for SRA because of the instructions available:
1008         // VS{RL} and VS{RL}O. However due to direct move costs, it's not worth
1009         // doing
1010         setOperationAction(ISD::SHL, MVT::v1i128, Expand);
1011         setOperationAction(ISD::SRL, MVT::v1i128, Expand);
1012         setOperationAction(ISD::SRA, MVT::v1i128, Expand);
1013 
1014         setOperationAction(ISD::SETCC, MVT::v2i64, Legal);
1015       }
1016       else {
1017         setOperationAction(ISD::SHL, MVT::v2i64, Expand);
1018         setOperationAction(ISD::SRA, MVT::v2i64, Expand);
1019         setOperationAction(ISD::SRL, MVT::v2i64, Expand);
1020 
1021         setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
1022 
1023         // VSX v2i64 only supports non-arithmetic operations.
1024         setOperationAction(ISD::ADD, MVT::v2i64, Expand);
1025         setOperationAction(ISD::SUB, MVT::v2i64, Expand);
1026       }
1027 
1028       if (Subtarget.isISA3_1())
1029         setOperationAction(ISD::SETCC, MVT::v1i128, Legal);
1030       else
1031         setOperationAction(ISD::SETCC, MVT::v1i128, Expand);
1032 
1033       setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
1034       AddPromotedToType (ISD::LOAD, MVT::v2i64, MVT::v2f64);
1035       setOperationAction(ISD::STORE, MVT::v2i64, Promote);
1036       AddPromotedToType (ISD::STORE, MVT::v2i64, MVT::v2f64);
1037 
1038       setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Legal);
1039 
1040       setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v2i64, Legal);
1041       setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v2i64, Legal);
1042       setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::v2i64, Legal);
1043       setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::v2i64, Legal);
1044       setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
1045       setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
1046       setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
1047       setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
1048 
1049       // Custom handling for partial vectors of integers converted to
1050       // floating point. We already have optimal handling for v2i32 through
1051       // the DAG combine, so those aren't necessary.
1052       setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v2i8, Custom);
1053       setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v4i8, Custom);
1054       setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v2i16, Custom);
1055       setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v4i16, Custom);
1056       setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v2i8, Custom);
1057       setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v4i8, Custom);
1058       setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v2i16, Custom);
1059       setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v4i16, Custom);
1060       setOperationAction(ISD::UINT_TO_FP, MVT::v2i8, Custom);
1061       setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Custom);
1062       setOperationAction(ISD::UINT_TO_FP, MVT::v2i16, Custom);
1063       setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
1064       setOperationAction(ISD::SINT_TO_FP, MVT::v2i8, Custom);
1065       setOperationAction(ISD::SINT_TO_FP, MVT::v4i8, Custom);
1066       setOperationAction(ISD::SINT_TO_FP, MVT::v2i16, Custom);
1067       setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
1068 
1069       setOperationAction(ISD::FNEG, MVT::v4f32, Legal);
1070       setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
1071       setOperationAction(ISD::FABS, MVT::v4f32, Legal);
1072       setOperationAction(ISD::FABS, MVT::v2f64, Legal);
1073       setOperationAction(ISD::FCOPYSIGN, MVT::v4f32, Legal);
1074       setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Legal);
1075 
1076       if (Subtarget.hasDirectMove())
1077         setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
1078       setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
1079 
1080       // Handle constrained floating-point operations of vector.
1081       // The predictor is `hasVSX` because altivec instruction has
1082       // no exception but VSX vector instruction has.
1083       setOperationAction(ISD::STRICT_FADD, MVT::v4f32, Legal);
1084       setOperationAction(ISD::STRICT_FSUB, MVT::v4f32, Legal);
1085       setOperationAction(ISD::STRICT_FMUL, MVT::v4f32, Legal);
1086       setOperationAction(ISD::STRICT_FDIV, MVT::v4f32, Legal);
1087       setOperationAction(ISD::STRICT_FMA, MVT::v4f32, Legal);
1088       setOperationAction(ISD::STRICT_FSQRT, MVT::v4f32, Legal);
1089       setOperationAction(ISD::STRICT_FMAXNUM, MVT::v4f32, Legal);
1090       setOperationAction(ISD::STRICT_FMINNUM, MVT::v4f32, Legal);
1091       setOperationAction(ISD::STRICT_FRINT, MVT::v4f32, Legal);
1092       setOperationAction(ISD::STRICT_FFLOOR, MVT::v4f32, Legal);
1093       setOperationAction(ISD::STRICT_FCEIL,  MVT::v4f32, Legal);
1094       setOperationAction(ISD::STRICT_FTRUNC, MVT::v4f32, Legal);
1095       setOperationAction(ISD::STRICT_FROUND, MVT::v4f32, Legal);
1096 
1097       setOperationAction(ISD::STRICT_FADD, MVT::v2f64, Legal);
1098       setOperationAction(ISD::STRICT_FSUB, MVT::v2f64, Legal);
1099       setOperationAction(ISD::STRICT_FMUL, MVT::v2f64, Legal);
1100       setOperationAction(ISD::STRICT_FDIV, MVT::v2f64, Legal);
1101       setOperationAction(ISD::STRICT_FMA, MVT::v2f64, Legal);
1102       setOperationAction(ISD::STRICT_FSQRT, MVT::v2f64, Legal);
1103       setOperationAction(ISD::STRICT_FMAXNUM, MVT::v2f64, Legal);
1104       setOperationAction(ISD::STRICT_FMINNUM, MVT::v2f64, Legal);
1105       setOperationAction(ISD::STRICT_FRINT, MVT::v2f64, Legal);
1106       setOperationAction(ISD::STRICT_FFLOOR, MVT::v2f64, Legal);
1107       setOperationAction(ISD::STRICT_FCEIL,  MVT::v2f64, Legal);
1108       setOperationAction(ISD::STRICT_FTRUNC, MVT::v2f64, Legal);
1109       setOperationAction(ISD::STRICT_FROUND, MVT::v2f64, Legal);
1110 
1111       addRegisterClass(MVT::v2i64, &PPC::VSRCRegClass);
1112       addRegisterClass(MVT::f128, &PPC::VRRCRegClass);
1113 
1114       for (MVT FPT : MVT::fp_valuetypes())
1115         setLoadExtAction(ISD::EXTLOAD, MVT::f128, FPT, Expand);
1116 
1117       // Expand the SELECT to SELECT_CC
1118       setOperationAction(ISD::SELECT, MVT::f128, Expand);
1119 
1120       setTruncStoreAction(MVT::f128, MVT::f64, Expand);
1121       setTruncStoreAction(MVT::f128, MVT::f32, Expand);
1122 
1123       // No implementation for these ops for PowerPC.
1124       setOperationAction(ISD::FSIN, MVT::f128, Expand);
1125       setOperationAction(ISD::FCOS, MVT::f128, Expand);
1126       setOperationAction(ISD::FPOW, MVT::f128, Expand);
1127       setOperationAction(ISD::FPOWI, MVT::f128, Expand);
1128       setOperationAction(ISD::FREM, MVT::f128, Expand);
1129     }
1130 
1131     if (Subtarget.hasP8Altivec()) {
1132       addRegisterClass(MVT::v2i64, &PPC::VRRCRegClass);
1133       addRegisterClass(MVT::v1i128, &PPC::VRRCRegClass);
1134     }
1135 
1136     if (Subtarget.hasP9Vector()) {
1137       setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
1138       setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
1139 
1140       // 128 bit shifts can be accomplished via 3 instructions for SHL and
1141       // SRL, but not for SRA because of the instructions available:
1142       // VS{RL} and VS{RL}O.
1143       setOperationAction(ISD::SHL, MVT::v1i128, Legal);
1144       setOperationAction(ISD::SRL, MVT::v1i128, Legal);
1145       setOperationAction(ISD::SRA, MVT::v1i128, Expand);
1146 
1147       setOperationAction(ISD::FADD, MVT::f128, Legal);
1148       setOperationAction(ISD::FSUB, MVT::f128, Legal);
1149       setOperationAction(ISD::FDIV, MVT::f128, Legal);
1150       setOperationAction(ISD::FMUL, MVT::f128, Legal);
1151       setOperationAction(ISD::FP_EXTEND, MVT::f128, Legal);
1152 
1153       setOperationAction(ISD::FMA, MVT::f128, Legal);
1154       setCondCodeAction(ISD::SETULT, MVT::f128, Expand);
1155       setCondCodeAction(ISD::SETUGT, MVT::f128, Expand);
1156       setCondCodeAction(ISD::SETUEQ, MVT::f128, Expand);
1157       setCondCodeAction(ISD::SETOGE, MVT::f128, Expand);
1158       setCondCodeAction(ISD::SETOLE, MVT::f128, Expand);
1159       setCondCodeAction(ISD::SETONE, MVT::f128, Expand);
1160 
1161       setOperationAction(ISD::FTRUNC, MVT::f128, Legal);
1162       setOperationAction(ISD::FRINT, MVT::f128, Legal);
1163       setOperationAction(ISD::FFLOOR, MVT::f128, Legal);
1164       setOperationAction(ISD::FCEIL, MVT::f128, Legal);
1165       setOperationAction(ISD::FNEARBYINT, MVT::f128, Legal);
1166       setOperationAction(ISD::FROUND, MVT::f128, Legal);
1167 
1168       setOperationAction(ISD::FP_ROUND, MVT::f64, Legal);
1169       setOperationAction(ISD::FP_ROUND, MVT::f32, Legal);
1170       setOperationAction(ISD::BITCAST, MVT::i128, Custom);
1171 
1172       // Handle constrained floating-point operations of fp128
1173       setOperationAction(ISD::STRICT_FADD, MVT::f128, Legal);
1174       setOperationAction(ISD::STRICT_FSUB, MVT::f128, Legal);
1175       setOperationAction(ISD::STRICT_FMUL, MVT::f128, Legal);
1176       setOperationAction(ISD::STRICT_FDIV, MVT::f128, Legal);
1177       setOperationAction(ISD::STRICT_FMA, MVT::f128, Legal);
1178       setOperationAction(ISD::STRICT_FSQRT, MVT::f128, Legal);
1179       setOperationAction(ISD::STRICT_FP_EXTEND, MVT::f128, Legal);
1180       setOperationAction(ISD::STRICT_FP_ROUND, MVT::f64, Legal);
1181       setOperationAction(ISD::STRICT_FP_ROUND, MVT::f32, Legal);
1182       setOperationAction(ISD::STRICT_FRINT, MVT::f128, Legal);
1183       setOperationAction(ISD::STRICT_FNEARBYINT, MVT::f128, Legal);
1184       setOperationAction(ISD::STRICT_FFLOOR, MVT::f128, Legal);
1185       setOperationAction(ISD::STRICT_FCEIL, MVT::f128, Legal);
1186       setOperationAction(ISD::STRICT_FTRUNC, MVT::f128, Legal);
1187       setOperationAction(ISD::STRICT_FROUND, MVT::f128, Legal);
1188       setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
1189       setOperationAction(ISD::BSWAP, MVT::v8i16, Legal);
1190       setOperationAction(ISD::BSWAP, MVT::v4i32, Legal);
1191       setOperationAction(ISD::BSWAP, MVT::v2i64, Legal);
1192       setOperationAction(ISD::BSWAP, MVT::v1i128, Legal);
1193     } else if (Subtarget.hasVSX()) {
1194       setOperationAction(ISD::LOAD, MVT::f128, Promote);
1195       setOperationAction(ISD::STORE, MVT::f128, Promote);
1196 
1197       AddPromotedToType(ISD::LOAD, MVT::f128, MVT::v4i32);
1198       AddPromotedToType(ISD::STORE, MVT::f128, MVT::v4i32);
1199 
1200       // Set FADD/FSUB as libcall to avoid the legalizer to expand the
1201       // fp_to_uint and int_to_fp.
1202       setOperationAction(ISD::FADD, MVT::f128, LibCall);
1203       setOperationAction(ISD::FSUB, MVT::f128, LibCall);
1204 
1205       setOperationAction(ISD::FMUL, MVT::f128, Expand);
1206       setOperationAction(ISD::FDIV, MVT::f128, Expand);
1207       setOperationAction(ISD::FNEG, MVT::f128, Expand);
1208       setOperationAction(ISD::FABS, MVT::f128, Expand);
1209       setOperationAction(ISD::FSQRT, MVT::f128, Expand);
1210       setOperationAction(ISD::FMA, MVT::f128, Expand);
1211       setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
1212 
1213       // Expand the fp_extend if the target type is fp128.
1214       setOperationAction(ISD::FP_EXTEND, MVT::f128, Expand);
1215       setOperationAction(ISD::STRICT_FP_EXTEND, MVT::f128, Expand);
1216 
1217       // Expand the fp_round if the source type is fp128.
1218       for (MVT VT : {MVT::f32, MVT::f64}) {
1219         setOperationAction(ISD::FP_ROUND, VT, Custom);
1220         setOperationAction(ISD::STRICT_FP_ROUND, VT, Custom);
1221       }
1222 
1223       setOperationAction(ISD::SETCC, MVT::f128, Custom);
1224       setOperationAction(ISD::STRICT_FSETCC, MVT::f128, Custom);
1225       setOperationAction(ISD::STRICT_FSETCCS, MVT::f128, Custom);
1226       setOperationAction(ISD::BR_CC, MVT::f128, Expand);
1227 
1228       // Lower following f128 select_cc pattern:
1229       // select_cc x, y, tv, fv, cc -> select_cc (setcc x, y, cc), 0, tv, fv, NE
1230       setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
1231 
1232       // We need to handle f128 SELECT_CC with integer result type.
1233       setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1234       setOperationAction(ISD::SELECT_CC, MVT::i64, isPPC64 ? Custom : Expand);
1235     }
1236 
1237     if (Subtarget.hasP9Altivec()) {
1238       setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
1239       setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
1240 
1241       setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8,  Legal);
1242       setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Legal);
1243       setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i32, Legal);
1244       setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8,  Legal);
1245       setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Legal);
1246       setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Legal);
1247       setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i64, Legal);
1248     }
1249 
1250     if (Subtarget.isISA3_1())
1251       setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
1252   }
1253 
1254   if (Subtarget.pairedVectorMemops()) {
1255     addRegisterClass(MVT::v256i1, &PPC::VSRpRCRegClass);
1256     setOperationAction(ISD::LOAD, MVT::v256i1, Custom);
1257     setOperationAction(ISD::STORE, MVT::v256i1, Custom);
1258   }
1259   if (Subtarget.hasMMA()) {
1260     addRegisterClass(MVT::v512i1, &PPC::UACCRCRegClass);
1261     setOperationAction(ISD::LOAD, MVT::v512i1, Custom);
1262     setOperationAction(ISD::STORE, MVT::v512i1, Custom);
1263     setOperationAction(ISD::BUILD_VECTOR, MVT::v512i1, Custom);
1264   }
1265 
1266   if (Subtarget.has64BitSupport())
1267     setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
1268 
1269   if (Subtarget.isISA3_1())
1270     setOperationAction(ISD::SRA, MVT::v1i128, Legal);
1271 
1272   setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, isPPC64 ? Legal : Custom);
1273 
1274   if (!isPPC64) {
1275     setOperationAction(ISD::ATOMIC_LOAD,  MVT::i64, Expand);
1276     setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
1277   }
1278 
1279   setBooleanContents(ZeroOrOneBooleanContent);
1280 
1281   if (Subtarget.hasAltivec()) {
1282     // Altivec instructions set fields to all zeros or all ones.
1283     setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
1284   }
1285 
1286   if (!isPPC64) {
1287     // These libcalls are not available in 32-bit.
1288     setLibcallName(RTLIB::SHL_I128, nullptr);
1289     setLibcallName(RTLIB::SRL_I128, nullptr);
1290     setLibcallName(RTLIB::SRA_I128, nullptr);
1291   }
1292 
1293   if (!isPPC64)
1294     setMaxAtomicSizeInBitsSupported(32);
1295 
1296   setStackPointerRegisterToSaveRestore(isPPC64 ? PPC::X1 : PPC::R1);
1297 
1298   // We have target-specific dag combine patterns for the following nodes:
1299   setTargetDAGCombine(ISD::ADD);
1300   setTargetDAGCombine(ISD::SHL);
1301   setTargetDAGCombine(ISD::SRA);
1302   setTargetDAGCombine(ISD::SRL);
1303   setTargetDAGCombine(ISD::MUL);
1304   setTargetDAGCombine(ISD::FMA);
1305   setTargetDAGCombine(ISD::SINT_TO_FP);
1306   setTargetDAGCombine(ISD::BUILD_VECTOR);
1307   if (Subtarget.hasFPCVT())
1308     setTargetDAGCombine(ISD::UINT_TO_FP);
1309   setTargetDAGCombine(ISD::LOAD);
1310   setTargetDAGCombine(ISD::STORE);
1311   setTargetDAGCombine(ISD::BR_CC);
1312   if (Subtarget.useCRBits())
1313     setTargetDAGCombine(ISD::BRCOND);
1314   setTargetDAGCombine(ISD::BSWAP);
1315   setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
1316   setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
1317   setTargetDAGCombine(ISD::INTRINSIC_VOID);
1318 
1319   setTargetDAGCombine(ISD::SIGN_EXTEND);
1320   setTargetDAGCombine(ISD::ZERO_EXTEND);
1321   setTargetDAGCombine(ISD::ANY_EXTEND);
1322 
1323   setTargetDAGCombine(ISD::TRUNCATE);
1324   setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
1325 
1326 
1327   if (Subtarget.useCRBits()) {
1328     setTargetDAGCombine(ISD::TRUNCATE);
1329     setTargetDAGCombine(ISD::SETCC);
1330     setTargetDAGCombine(ISD::SELECT_CC);
1331   }
1332 
1333   if (Subtarget.hasP9Altivec()) {
1334     setTargetDAGCombine(ISD::ABS);
1335     setTargetDAGCombine(ISD::VSELECT);
1336   }
1337 
1338   setLibcallName(RTLIB::LOG_F128, "logf128");
1339   setLibcallName(RTLIB::LOG2_F128, "log2f128");
1340   setLibcallName(RTLIB::LOG10_F128, "log10f128");
1341   setLibcallName(RTLIB::EXP_F128, "expf128");
1342   setLibcallName(RTLIB::EXP2_F128, "exp2f128");
1343   setLibcallName(RTLIB::SIN_F128, "sinf128");
1344   setLibcallName(RTLIB::COS_F128, "cosf128");
1345   setLibcallName(RTLIB::POW_F128, "powf128");
1346   setLibcallName(RTLIB::FMIN_F128, "fminf128");
1347   setLibcallName(RTLIB::FMAX_F128, "fmaxf128");
1348   setLibcallName(RTLIB::REM_F128, "fmodf128");
1349   setLibcallName(RTLIB::SQRT_F128, "sqrtf128");
1350   setLibcallName(RTLIB::CEIL_F128, "ceilf128");
1351   setLibcallName(RTLIB::FLOOR_F128, "floorf128");
1352   setLibcallName(RTLIB::TRUNC_F128, "truncf128");
1353   setLibcallName(RTLIB::ROUND_F128, "roundf128");
1354   setLibcallName(RTLIB::LROUND_F128, "lroundf128");
1355   setLibcallName(RTLIB::LLROUND_F128, "llroundf128");
1356   setLibcallName(RTLIB::RINT_F128, "rintf128");
1357   setLibcallName(RTLIB::LRINT_F128, "lrintf128");
1358   setLibcallName(RTLIB::LLRINT_F128, "llrintf128");
1359   setLibcallName(RTLIB::NEARBYINT_F128, "nearbyintf128");
1360   setLibcallName(RTLIB::FMA_F128, "fmaf128");
1361 
1362   // With 32 condition bits, we don't need to sink (and duplicate) compares
1363   // aggressively in CodeGenPrep.
1364   if (Subtarget.useCRBits()) {
1365     setHasMultipleConditionRegisters();
1366     setJumpIsExpensive();
1367   }
1368 
1369   setMinFunctionAlignment(Align(4));
1370 
1371   switch (Subtarget.getCPUDirective()) {
1372   default: break;
1373   case PPC::DIR_970:
1374   case PPC::DIR_A2:
1375   case PPC::DIR_E500:
1376   case PPC::DIR_E500mc:
1377   case PPC::DIR_E5500:
1378   case PPC::DIR_PWR4:
1379   case PPC::DIR_PWR5:
1380   case PPC::DIR_PWR5X:
1381   case PPC::DIR_PWR6:
1382   case PPC::DIR_PWR6X:
1383   case PPC::DIR_PWR7:
1384   case PPC::DIR_PWR8:
1385   case PPC::DIR_PWR9:
1386   case PPC::DIR_PWR10:
1387   case PPC::DIR_PWR_FUTURE:
1388     setPrefLoopAlignment(Align(16));
1389     setPrefFunctionAlignment(Align(16));
1390     break;
1391   }
1392 
1393   if (Subtarget.enableMachineScheduler())
1394     setSchedulingPreference(Sched::Source);
1395   else
1396     setSchedulingPreference(Sched::Hybrid);
1397 
1398   computeRegisterProperties(STI.getRegisterInfo());
1399 
1400   // The Freescale cores do better with aggressive inlining of memcpy and
1401   // friends. GCC uses same threshold of 128 bytes (= 32 word stores).
1402   if (Subtarget.getCPUDirective() == PPC::DIR_E500mc ||
1403       Subtarget.getCPUDirective() == PPC::DIR_E5500) {
1404     MaxStoresPerMemset = 32;
1405     MaxStoresPerMemsetOptSize = 16;
1406     MaxStoresPerMemcpy = 32;
1407     MaxStoresPerMemcpyOptSize = 8;
1408     MaxStoresPerMemmove = 32;
1409     MaxStoresPerMemmoveOptSize = 8;
1410   } else if (Subtarget.getCPUDirective() == PPC::DIR_A2) {
1411     // The A2 also benefits from (very) aggressive inlining of memcpy and
1412     // friends. The overhead of a the function call, even when warm, can be
1413     // over one hundred cycles.
1414     MaxStoresPerMemset = 128;
1415     MaxStoresPerMemcpy = 128;
1416     MaxStoresPerMemmove = 128;
1417     MaxLoadsPerMemcmp = 128;
1418   } else {
1419     MaxLoadsPerMemcmp = 8;
1420     MaxLoadsPerMemcmpOptSize = 4;
1421   }
1422 
1423   IsStrictFPEnabled = true;
1424 
1425   // Let the subtarget (CPU) decide if a predictable select is more expensive
1426   // than the corresponding branch. This information is used in CGP to decide
1427   // when to convert selects into branches.
1428   PredictableSelectIsExpensive = Subtarget.isPredictableSelectIsExpensive();
1429 }
1430 
1431 // *********************************** NOTE ************************************
1432 // For selecting load and store instructions, the addressing modes are defined
1433 // as ComplexPatterns in PPCInstrInfo.td, which are then utilized in the TD
1434 // patterns to match the load the store instructions.
1435 //
1436 // The TD definitions for the addressing modes correspond to their respective
1437 // Select<AddrMode>Form() function in PPCISelDAGToDAG.cpp. These functions rely
1438 // on SelectOptimalAddrMode(), which calls computeMOFlags() to compute the
1439 // address mode flags of a particular node. Afterwards, the computed address
1440 // flags are passed into getAddrModeForFlags() in order to retrieve the optimal
1441 // addressing mode. SelectOptimalAddrMode() then sets the Base and Displacement
1442 // accordingly, based on the preferred addressing mode.
1443 //
1444 // Within PPCISelLowering.h, there are two enums: MemOpFlags and AddrMode.
1445 // MemOpFlags contains all the possible flags that can be used to compute the
1446 // optimal addressing mode for load and store instructions.
1447 // AddrMode contains all the possible load and store addressing modes available
1448 // on Power (such as DForm, DSForm, DQForm, XForm, etc.)
1449 //
1450 // When adding new load and store instructions, it is possible that new address
1451 // flags may need to be added into MemOpFlags, and a new addressing mode will
1452 // need to be added to AddrMode. An entry of the new addressing mode (consisting
1453 // of the minimal and main distinguishing address flags for the new load/store
1454 // instructions) will need to be added into initializeAddrModeMap() below.
1455 // Finally, when adding new addressing modes, the getAddrModeForFlags() will
1456 // need to be updated to account for selecting the optimal addressing mode.
1457 // *****************************************************************************
1458 /// Initialize the map that relates the different addressing modes of the load
1459 /// and store instructions to a set of flags. This ensures the load/store
1460 /// instruction is correctly matched during instruction selection.
1461 void PPCTargetLowering::initializeAddrModeMap() {
1462   AddrModesMap[PPC::AM_DForm] = {
1463       // LWZ, STW
1464       PPC::MOF_ZExt | PPC::MOF_RPlusSImm16 | PPC::MOF_WordInt,
1465       PPC::MOF_ZExt | PPC::MOF_RPlusLo | PPC::MOF_WordInt,
1466       PPC::MOF_ZExt | PPC::MOF_NotAddNorCst | PPC::MOF_WordInt,
1467       PPC::MOF_ZExt | PPC::MOF_AddrIsSImm32 | PPC::MOF_WordInt,
1468       // LBZ, LHZ, STB, STH
1469       PPC::MOF_ZExt | PPC::MOF_RPlusSImm16 | PPC::MOF_SubWordInt,
1470       PPC::MOF_ZExt | PPC::MOF_RPlusLo | PPC::MOF_SubWordInt,
1471       PPC::MOF_ZExt | PPC::MOF_NotAddNorCst | PPC::MOF_SubWordInt,
1472       PPC::MOF_ZExt | PPC::MOF_AddrIsSImm32 | PPC::MOF_SubWordInt,
1473       // LHA
1474       PPC::MOF_SExt | PPC::MOF_RPlusSImm16 | PPC::MOF_SubWordInt,
1475       PPC::MOF_SExt | PPC::MOF_RPlusLo | PPC::MOF_SubWordInt,
1476       PPC::MOF_SExt | PPC::MOF_NotAddNorCst | PPC::MOF_SubWordInt,
1477       PPC::MOF_SExt | PPC::MOF_AddrIsSImm32 | PPC::MOF_SubWordInt,
1478       // LFS, LFD, STFS, STFD
1479       PPC::MOF_RPlusSImm16 | PPC::MOF_ScalarFloat | PPC::MOF_SubtargetBeforeP9,
1480       PPC::MOF_RPlusLo | PPC::MOF_ScalarFloat | PPC::MOF_SubtargetBeforeP9,
1481       PPC::MOF_NotAddNorCst | PPC::MOF_ScalarFloat | PPC::MOF_SubtargetBeforeP9,
1482       PPC::MOF_AddrIsSImm32 | PPC::MOF_ScalarFloat | PPC::MOF_SubtargetBeforeP9,
1483   };
1484   AddrModesMap[PPC::AM_DSForm] = {
1485       // LWA
1486       PPC::MOF_SExt | PPC::MOF_RPlusSImm16Mult4 | PPC::MOF_WordInt,
1487       PPC::MOF_SExt | PPC::MOF_NotAddNorCst | PPC::MOF_WordInt,
1488       PPC::MOF_SExt | PPC::MOF_AddrIsSImm32 | PPC::MOF_WordInt,
1489       // LD, STD
1490       PPC::MOF_RPlusSImm16Mult4 | PPC::MOF_DoubleWordInt,
1491       PPC::MOF_NotAddNorCst | PPC::MOF_DoubleWordInt,
1492       PPC::MOF_AddrIsSImm32 | PPC::MOF_DoubleWordInt,
1493       // DFLOADf32, DFLOADf64, DSTOREf32, DSTOREf64
1494       PPC::MOF_RPlusSImm16Mult4 | PPC::MOF_ScalarFloat | PPC::MOF_SubtargetP9,
1495       PPC::MOF_NotAddNorCst | PPC::MOF_ScalarFloat | PPC::MOF_SubtargetP9,
1496       PPC::MOF_AddrIsSImm32 | PPC::MOF_ScalarFloat | PPC::MOF_SubtargetP9,
1497   };
1498   AddrModesMap[PPC::AM_DQForm] = {
1499       // LXV, STXV
1500       PPC::MOF_RPlusSImm16Mult16 | PPC::MOF_Vector | PPC::MOF_SubtargetP9,
1501       PPC::MOF_NotAddNorCst | PPC::MOF_Vector | PPC::MOF_SubtargetP9,
1502       PPC::MOF_AddrIsSImm32 | PPC::MOF_Vector | PPC::MOF_SubtargetP9,
1503       PPC::MOF_RPlusSImm16Mult16 | PPC::MOF_Vector256 | PPC::MOF_SubtargetP10,
1504       PPC::MOF_NotAddNorCst | PPC::MOF_Vector256 | PPC::MOF_SubtargetP10,
1505       PPC::MOF_AddrIsSImm32 | PPC::MOF_Vector256 | PPC::MOF_SubtargetP10,
1506   };
1507 }
1508 
1509 /// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1510 /// the desired ByVal argument alignment.
1511 static void getMaxByValAlign(Type *Ty, Align &MaxAlign, Align MaxMaxAlign) {
1512   if (MaxAlign == MaxMaxAlign)
1513     return;
1514   if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1515     if (MaxMaxAlign >= 32 &&
1516         VTy->getPrimitiveSizeInBits().getFixedSize() >= 256)
1517       MaxAlign = Align(32);
1518     else if (VTy->getPrimitiveSizeInBits().getFixedSize() >= 128 &&
1519              MaxAlign < 16)
1520       MaxAlign = Align(16);
1521   } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1522     Align EltAlign;
1523     getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign);
1524     if (EltAlign > MaxAlign)
1525       MaxAlign = EltAlign;
1526   } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
1527     for (auto *EltTy : STy->elements()) {
1528       Align EltAlign;
1529       getMaxByValAlign(EltTy, EltAlign, MaxMaxAlign);
1530       if (EltAlign > MaxAlign)
1531         MaxAlign = EltAlign;
1532       if (MaxAlign == MaxMaxAlign)
1533         break;
1534     }
1535   }
1536 }
1537 
1538 /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1539 /// function arguments in the caller parameter area.
1540 unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty,
1541                                                   const DataLayout &DL) const {
1542   // 16byte and wider vectors are passed on 16byte boundary.
1543   // The rest is 8 on PPC64 and 4 on PPC32 boundary.
1544   Align Alignment = Subtarget.isPPC64() ? Align(8) : Align(4);
1545   if (Subtarget.hasAltivec())
1546     getMaxByValAlign(Ty, Alignment, Align(16));
1547   return Alignment.value();
1548 }
1549 
1550 bool PPCTargetLowering::useSoftFloat() const {
1551   return Subtarget.useSoftFloat();
1552 }
1553 
1554 bool PPCTargetLowering::hasSPE() const {
1555   return Subtarget.hasSPE();
1556 }
1557 
1558 bool PPCTargetLowering::preferIncOfAddToSubOfNot(EVT VT) const {
1559   return VT.isScalarInteger();
1560 }
1561 
1562 const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
1563   switch ((PPCISD::NodeType)Opcode) {
1564   case PPCISD::FIRST_NUMBER:    break;
1565   case PPCISD::FSEL:            return "PPCISD::FSEL";
1566   case PPCISD::XSMAXCDP:        return "PPCISD::XSMAXCDP";
1567   case PPCISD::XSMINCDP:        return "PPCISD::XSMINCDP";
1568   case PPCISD::FCFID:           return "PPCISD::FCFID";
1569   case PPCISD::FCFIDU:          return "PPCISD::FCFIDU";
1570   case PPCISD::FCFIDS:          return "PPCISD::FCFIDS";
1571   case PPCISD::FCFIDUS:         return "PPCISD::FCFIDUS";
1572   case PPCISD::FCTIDZ:          return "PPCISD::FCTIDZ";
1573   case PPCISD::FCTIWZ:          return "PPCISD::FCTIWZ";
1574   case PPCISD::FCTIDUZ:         return "PPCISD::FCTIDUZ";
1575   case PPCISD::FCTIWUZ:         return "PPCISD::FCTIWUZ";
1576   case PPCISD::FP_TO_UINT_IN_VSR:
1577                                 return "PPCISD::FP_TO_UINT_IN_VSR,";
1578   case PPCISD::FP_TO_SINT_IN_VSR:
1579                                 return "PPCISD::FP_TO_SINT_IN_VSR";
1580   case PPCISD::FRE:             return "PPCISD::FRE";
1581   case PPCISD::FRSQRTE:         return "PPCISD::FRSQRTE";
1582   case PPCISD::FTSQRT:
1583     return "PPCISD::FTSQRT";
1584   case PPCISD::FSQRT:
1585     return "PPCISD::FSQRT";
1586   case PPCISD::STFIWX:          return "PPCISD::STFIWX";
1587   case PPCISD::VPERM:           return "PPCISD::VPERM";
1588   case PPCISD::XXSPLT:          return "PPCISD::XXSPLT";
1589   case PPCISD::XXSPLTI_SP_TO_DP:
1590     return "PPCISD::XXSPLTI_SP_TO_DP";
1591   case PPCISD::XXSPLTI32DX:
1592     return "PPCISD::XXSPLTI32DX";
1593   case PPCISD::VECINSERT:       return "PPCISD::VECINSERT";
1594   case PPCISD::XXPERMDI:        return "PPCISD::XXPERMDI";
1595   case PPCISD::VECSHL:          return "PPCISD::VECSHL";
1596   case PPCISD::CMPB:            return "PPCISD::CMPB";
1597   case PPCISD::Hi:              return "PPCISD::Hi";
1598   case PPCISD::Lo:              return "PPCISD::Lo";
1599   case PPCISD::TOC_ENTRY:       return "PPCISD::TOC_ENTRY";
1600   case PPCISD::ATOMIC_CMP_SWAP_8: return "PPCISD::ATOMIC_CMP_SWAP_8";
1601   case PPCISD::ATOMIC_CMP_SWAP_16: return "PPCISD::ATOMIC_CMP_SWAP_16";
1602   case PPCISD::DYNALLOC:        return "PPCISD::DYNALLOC";
1603   case PPCISD::DYNAREAOFFSET:   return "PPCISD::DYNAREAOFFSET";
1604   case PPCISD::PROBED_ALLOCA:   return "PPCISD::PROBED_ALLOCA";
1605   case PPCISD::GlobalBaseReg:   return "PPCISD::GlobalBaseReg";
1606   case PPCISD::SRL:             return "PPCISD::SRL";
1607   case PPCISD::SRA:             return "PPCISD::SRA";
1608   case PPCISD::SHL:             return "PPCISD::SHL";
1609   case PPCISD::SRA_ADDZE:       return "PPCISD::SRA_ADDZE";
1610   case PPCISD::CALL:            return "PPCISD::CALL";
1611   case PPCISD::CALL_NOP:        return "PPCISD::CALL_NOP";
1612   case PPCISD::CALL_NOTOC:      return "PPCISD::CALL_NOTOC";
1613   case PPCISD::MTCTR:           return "PPCISD::MTCTR";
1614   case PPCISD::BCTRL:           return "PPCISD::BCTRL";
1615   case PPCISD::BCTRL_LOAD_TOC:  return "PPCISD::BCTRL_LOAD_TOC";
1616   case PPCISD::RET_FLAG:        return "PPCISD::RET_FLAG";
1617   case PPCISD::READ_TIME_BASE:  return "PPCISD::READ_TIME_BASE";
1618   case PPCISD::EH_SJLJ_SETJMP:  return "PPCISD::EH_SJLJ_SETJMP";
1619   case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
1620   case PPCISD::MFOCRF:          return "PPCISD::MFOCRF";
1621   case PPCISD::MFVSR:           return "PPCISD::MFVSR";
1622   case PPCISD::MTVSRA:          return "PPCISD::MTVSRA";
1623   case PPCISD::MTVSRZ:          return "PPCISD::MTVSRZ";
1624   case PPCISD::SINT_VEC_TO_FP:  return "PPCISD::SINT_VEC_TO_FP";
1625   case PPCISD::UINT_VEC_TO_FP:  return "PPCISD::UINT_VEC_TO_FP";
1626   case PPCISD::SCALAR_TO_VECTOR_PERMUTED:
1627     return "PPCISD::SCALAR_TO_VECTOR_PERMUTED";
1628   case PPCISD::ANDI_rec_1_EQ_BIT:
1629     return "PPCISD::ANDI_rec_1_EQ_BIT";
1630   case PPCISD::ANDI_rec_1_GT_BIT:
1631     return "PPCISD::ANDI_rec_1_GT_BIT";
1632   case PPCISD::VCMP:            return "PPCISD::VCMP";
1633   case PPCISD::VCMP_rec:        return "PPCISD::VCMP_rec";
1634   case PPCISD::LBRX:            return "PPCISD::LBRX";
1635   case PPCISD::STBRX:           return "PPCISD::STBRX";
1636   case PPCISD::LFIWAX:          return "PPCISD::LFIWAX";
1637   case PPCISD::LFIWZX:          return "PPCISD::LFIWZX";
1638   case PPCISD::LXSIZX:          return "PPCISD::LXSIZX";
1639   case PPCISD::STXSIX:          return "PPCISD::STXSIX";
1640   case PPCISD::VEXTS:           return "PPCISD::VEXTS";
1641   case PPCISD::LXVD2X:          return "PPCISD::LXVD2X";
1642   case PPCISD::STXVD2X:         return "PPCISD::STXVD2X";
1643   case PPCISD::LOAD_VEC_BE:     return "PPCISD::LOAD_VEC_BE";
1644   case PPCISD::STORE_VEC_BE:    return "PPCISD::STORE_VEC_BE";
1645   case PPCISD::ST_VSR_SCAL_INT:
1646                                 return "PPCISD::ST_VSR_SCAL_INT";
1647   case PPCISD::COND_BRANCH:     return "PPCISD::COND_BRANCH";
1648   case PPCISD::BDNZ:            return "PPCISD::BDNZ";
1649   case PPCISD::BDZ:             return "PPCISD::BDZ";
1650   case PPCISD::MFFS:            return "PPCISD::MFFS";
1651   case PPCISD::FADDRTZ:         return "PPCISD::FADDRTZ";
1652   case PPCISD::TC_RETURN:       return "PPCISD::TC_RETURN";
1653   case PPCISD::CR6SET:          return "PPCISD::CR6SET";
1654   case PPCISD::CR6UNSET:        return "PPCISD::CR6UNSET";
1655   case PPCISD::PPC32_GOT:       return "PPCISD::PPC32_GOT";
1656   case PPCISD::PPC32_PICGOT:    return "PPCISD::PPC32_PICGOT";
1657   case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
1658   case PPCISD::LD_GOT_TPREL_L:  return "PPCISD::LD_GOT_TPREL_L";
1659   case PPCISD::ADD_TLS:         return "PPCISD::ADD_TLS";
1660   case PPCISD::ADDIS_TLSGD_HA:  return "PPCISD::ADDIS_TLSGD_HA";
1661   case PPCISD::ADDI_TLSGD_L:    return "PPCISD::ADDI_TLSGD_L";
1662   case PPCISD::GET_TLS_ADDR:    return "PPCISD::GET_TLS_ADDR";
1663   case PPCISD::ADDI_TLSGD_L_ADDR: return "PPCISD::ADDI_TLSGD_L_ADDR";
1664   case PPCISD::TLSGD_AIX:       return "PPCISD::TLSGD_AIX";
1665   case PPCISD::ADDIS_TLSLD_HA:  return "PPCISD::ADDIS_TLSLD_HA";
1666   case PPCISD::ADDI_TLSLD_L:    return "PPCISD::ADDI_TLSLD_L";
1667   case PPCISD::GET_TLSLD_ADDR:  return "PPCISD::GET_TLSLD_ADDR";
1668   case PPCISD::ADDI_TLSLD_L_ADDR: return "PPCISD::ADDI_TLSLD_L_ADDR";
1669   case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
1670   case PPCISD::ADDI_DTPREL_L:   return "PPCISD::ADDI_DTPREL_L";
1671   case PPCISD::PADDI_DTPREL:
1672     return "PPCISD::PADDI_DTPREL";
1673   case PPCISD::VADD_SPLAT:      return "PPCISD::VADD_SPLAT";
1674   case PPCISD::SC:              return "PPCISD::SC";
1675   case PPCISD::CLRBHRB:         return "PPCISD::CLRBHRB";
1676   case PPCISD::MFBHRBE:         return "PPCISD::MFBHRBE";
1677   case PPCISD::RFEBB:           return "PPCISD::RFEBB";
1678   case PPCISD::XXSWAPD:         return "PPCISD::XXSWAPD";
1679   case PPCISD::SWAP_NO_CHAIN:   return "PPCISD::SWAP_NO_CHAIN";
1680   case PPCISD::VABSD:           return "PPCISD::VABSD";
1681   case PPCISD::BUILD_FP128:     return "PPCISD::BUILD_FP128";
1682   case PPCISD::BUILD_SPE64:     return "PPCISD::BUILD_SPE64";
1683   case PPCISD::EXTRACT_SPE:     return "PPCISD::EXTRACT_SPE";
1684   case PPCISD::EXTSWSLI:        return "PPCISD::EXTSWSLI";
1685   case PPCISD::LD_VSX_LH:       return "PPCISD::LD_VSX_LH";
1686   case PPCISD::FP_EXTEND_HALF:  return "PPCISD::FP_EXTEND_HALF";
1687   case PPCISD::MAT_PCREL_ADDR:  return "PPCISD::MAT_PCREL_ADDR";
1688   case PPCISD::TLS_DYNAMIC_MAT_PCREL_ADDR:
1689     return "PPCISD::TLS_DYNAMIC_MAT_PCREL_ADDR";
1690   case PPCISD::TLS_LOCAL_EXEC_MAT_ADDR:
1691     return "PPCISD::TLS_LOCAL_EXEC_MAT_ADDR";
1692   case PPCISD::ACC_BUILD:       return "PPCISD::ACC_BUILD";
1693   case PPCISD::PAIR_BUILD:      return "PPCISD::PAIR_BUILD";
1694   case PPCISD::EXTRACT_VSX_REG: return "PPCISD::EXTRACT_VSX_REG";
1695   case PPCISD::XXMFACC:         return "PPCISD::XXMFACC";
1696   case PPCISD::LD_SPLAT:        return "PPCISD::LD_SPLAT";
1697   case PPCISD::FNMSUB:          return "PPCISD::FNMSUB";
1698   case PPCISD::STRICT_FADDRTZ:
1699     return "PPCISD::STRICT_FADDRTZ";
1700   case PPCISD::STRICT_FCTIDZ:
1701     return "PPCISD::STRICT_FCTIDZ";
1702   case PPCISD::STRICT_FCTIWZ:
1703     return "PPCISD::STRICT_FCTIWZ";
1704   case PPCISD::STRICT_FCTIDUZ:
1705     return "PPCISD::STRICT_FCTIDUZ";
1706   case PPCISD::STRICT_FCTIWUZ:
1707     return "PPCISD::STRICT_FCTIWUZ";
1708   case PPCISD::STRICT_FCFID:
1709     return "PPCISD::STRICT_FCFID";
1710   case PPCISD::STRICT_FCFIDU:
1711     return "PPCISD::STRICT_FCFIDU";
1712   case PPCISD::STRICT_FCFIDS:
1713     return "PPCISD::STRICT_FCFIDS";
1714   case PPCISD::STRICT_FCFIDUS:
1715     return "PPCISD::STRICT_FCFIDUS";
1716   case PPCISD::LXVRZX:          return "PPCISD::LXVRZX";
1717   }
1718   return nullptr;
1719 }
1720 
1721 EVT PPCTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &C,
1722                                           EVT VT) const {
1723   if (!VT.isVector())
1724     return Subtarget.useCRBits() ? MVT::i1 : MVT::i32;
1725 
1726   return VT.changeVectorElementTypeToInteger();
1727 }
1728 
1729 bool PPCTargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1730   assert(VT.isFloatingPoint() && "Non-floating-point FMA?");
1731   return true;
1732 }
1733 
1734 //===----------------------------------------------------------------------===//
1735 // Node matching predicates, for use by the tblgen matching code.
1736 //===----------------------------------------------------------------------===//
1737 
1738 /// isFloatingPointZero - Return true if this is 0.0 or -0.0.
1739 static bool isFloatingPointZero(SDValue Op) {
1740   if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
1741     return CFP->getValueAPF().isZero();
1742   else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
1743     // Maybe this has already been legalized into the constant pool?
1744     if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
1745       if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
1746         return CFP->getValueAPF().isZero();
1747   }
1748   return false;
1749 }
1750 
1751 /// isConstantOrUndef - Op is either an undef node or a ConstantSDNode.  Return
1752 /// true if Op is undef or if it matches the specified value.
1753 static bool isConstantOrUndef(int Op, int Val) {
1754   return Op < 0 || Op == Val;
1755 }
1756 
1757 /// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
1758 /// VPKUHUM instruction.
1759 /// The ShuffleKind distinguishes between big-endian operations with
1760 /// two different inputs (0), either-endian operations with two identical
1761 /// inputs (1), and little-endian operations with two different inputs (2).
1762 /// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
1763 bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
1764                                SelectionDAG &DAG) {
1765   bool IsLE = DAG.getDataLayout().isLittleEndian();
1766   if (ShuffleKind == 0) {
1767     if (IsLE)
1768       return false;
1769     for (unsigned i = 0; i != 16; ++i)
1770       if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
1771         return false;
1772   } else if (ShuffleKind == 2) {
1773     if (!IsLE)
1774       return false;
1775     for (unsigned i = 0; i != 16; ++i)
1776       if (!isConstantOrUndef(N->getMaskElt(i), i*2))
1777         return false;
1778   } else if (ShuffleKind == 1) {
1779     unsigned j = IsLE ? 0 : 1;
1780     for (unsigned i = 0; i != 8; ++i)
1781       if (!isConstantOrUndef(N->getMaskElt(i),    i*2+j) ||
1782           !isConstantOrUndef(N->getMaskElt(i+8),  i*2+j))
1783         return false;
1784   }
1785   return true;
1786 }
1787 
1788 /// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
1789 /// VPKUWUM instruction.
1790 /// The ShuffleKind distinguishes between big-endian operations with
1791 /// two different inputs (0), either-endian operations with two identical
1792 /// inputs (1), and little-endian operations with two different inputs (2).
1793 /// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
1794 bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
1795                                SelectionDAG &DAG) {
1796   bool IsLE = DAG.getDataLayout().isLittleEndian();
1797   if (ShuffleKind == 0) {
1798     if (IsLE)
1799       return false;
1800     for (unsigned i = 0; i != 16; i += 2)
1801       if (!isConstantOrUndef(N->getMaskElt(i  ),  i*2+2) ||
1802           !isConstantOrUndef(N->getMaskElt(i+1),  i*2+3))
1803         return false;
1804   } else if (ShuffleKind == 2) {
1805     if (!IsLE)
1806       return false;
1807     for (unsigned i = 0; i != 16; i += 2)
1808       if (!isConstantOrUndef(N->getMaskElt(i  ),  i*2) ||
1809           !isConstantOrUndef(N->getMaskElt(i+1),  i*2+1))
1810         return false;
1811   } else if (ShuffleKind == 1) {
1812     unsigned j = IsLE ? 0 : 2;
1813     for (unsigned i = 0; i != 8; i += 2)
1814       if (!isConstantOrUndef(N->getMaskElt(i  ),  i*2+j)   ||
1815           !isConstantOrUndef(N->getMaskElt(i+1),  i*2+j+1) ||
1816           !isConstantOrUndef(N->getMaskElt(i+8),  i*2+j)   ||
1817           !isConstantOrUndef(N->getMaskElt(i+9),  i*2+j+1))
1818         return false;
1819   }
1820   return true;
1821 }
1822 
1823 /// isVPKUDUMShuffleMask - Return true if this is the shuffle mask for a
1824 /// VPKUDUM instruction, AND the VPKUDUM instruction exists for the
1825 /// current subtarget.
1826 ///
1827 /// The ShuffleKind distinguishes between big-endian operations with
1828 /// two different inputs (0), either-endian operations with two identical
1829 /// inputs (1), and little-endian operations with two different inputs (2).
1830 /// For the latter, the input operands are swapped (see PPCInstrAltivec.td).
1831 bool PPC::isVPKUDUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind,
1832                                SelectionDAG &DAG) {
1833   const PPCSubtarget& Subtarget =
1834       static_cast<const PPCSubtarget&>(DAG.getSubtarget());
1835   if (!Subtarget.hasP8Vector())
1836     return false;
1837 
1838   bool IsLE = DAG.getDataLayout().isLittleEndian();
1839   if (ShuffleKind == 0) {
1840     if (IsLE)
1841       return false;
1842     for (unsigned i = 0; i != 16; i += 4)
1843       if (!isConstantOrUndef(N->getMaskElt(i  ),  i*2+4) ||
1844           !isConstantOrUndef(N->getMaskElt(i+1),  i*2+5) ||
1845           !isConstantOrUndef(N->getMaskElt(i+2),  i*2+6) ||
1846           !isConstantOrUndef(N->getMaskElt(i+3),  i*2+7))
1847         return false;
1848   } else if (ShuffleKind == 2) {
1849     if (!IsLE)
1850       return false;
1851     for (unsigned i = 0; i != 16; i += 4)
1852       if (!isConstantOrUndef(N->getMaskElt(i  ),  i*2) ||
1853           !isConstantOrUndef(N->getMaskElt(i+1),  i*2+1) ||
1854           !isConstantOrUndef(N->getMaskElt(i+2),  i*2+2) ||
1855           !isConstantOrUndef(N->getMaskElt(i+3),  i*2+3))
1856         return false;
1857   } else if (ShuffleKind == 1) {
1858     unsigned j = IsLE ? 0 : 4;
1859     for (unsigned i = 0; i != 8; i += 4)
1860       if (!isConstantOrUndef(N->getMaskElt(i  ),  i*2+j)   ||
1861           !isConstantOrUndef(N->getMaskElt(i+1),  i*2+j+1) ||
1862           !isConstantOrUndef(N->getMaskElt(i+2),  i*2+j+2) ||
1863           !isConstantOrUndef(N->getMaskElt(i+3),  i*2+j+3) ||
1864           !isConstantOrUndef(N->getMaskElt(i+8),  i*2+j)   ||
1865           !isConstantOrUndef(N->getMaskElt(i+9),  i*2+j+1) ||
1866           !isConstantOrUndef(N->getMaskElt(i+10), i*2+j+2) ||
1867           !isConstantOrUndef(N->getMaskElt(i+11), i*2+j+3))
1868         return false;
1869   }
1870   return true;
1871 }
1872 
1873 /// isVMerge - Common function, used to match vmrg* shuffles.
1874 ///
1875 static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
1876                      unsigned LHSStart, unsigned RHSStart) {
1877   if (N->getValueType(0) != MVT::v16i8)
1878     return false;
1879   assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
1880          "Unsupported merge size!");
1881 
1882   for (unsigned i = 0; i != 8/UnitSize; ++i)     // Step over units
1883     for (unsigned j = 0; j != UnitSize; ++j) {   // Step over bytes within unit
1884       if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
1885                              LHSStart+j+i*UnitSize) ||
1886           !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
1887                              RHSStart+j+i*UnitSize))
1888         return false;
1889     }
1890   return true;
1891 }
1892 
1893 /// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
1894 /// a VMRGL* instruction with the specified unit size (1,2 or 4 bytes).
1895 /// The ShuffleKind distinguishes between big-endian merges with two
1896 /// different inputs (0), either-endian merges with two identical inputs (1),
1897 /// and little-endian merges with two different inputs (2).  For the latter,
1898 /// the input operands are swapped (see PPCInstrAltivec.td).
1899 bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
1900                              unsigned ShuffleKind, SelectionDAG &DAG) {
1901   if (DAG.getDataLayout().isLittleEndian()) {
1902     if (ShuffleKind == 1) // unary
1903       return isVMerge(N, UnitSize, 0, 0);
1904     else if (ShuffleKind == 2) // swapped
1905       return isVMerge(N, UnitSize, 0, 16);
1906     else
1907       return false;
1908   } else {
1909     if (ShuffleKind == 1) // unary
1910       return isVMerge(N, UnitSize, 8, 8);
1911     else if (ShuffleKind == 0) // normal
1912       return isVMerge(N, UnitSize, 8, 24);
1913     else
1914       return false;
1915   }
1916 }
1917 
1918 /// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
1919 /// a VMRGH* instruction with the specified unit size (1,2 or 4 bytes).
1920 /// The ShuffleKind distinguishes between big-endian merges with two
1921 /// different inputs (0), either-endian merges with two identical inputs (1),
1922 /// and little-endian merges with two different inputs (2).  For the latter,
1923 /// the input operands are swapped (see PPCInstrAltivec.td).
1924 bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
1925                              unsigned ShuffleKind, SelectionDAG &DAG) {
1926   if (DAG.getDataLayout().isLittleEndian()) {
1927     if (ShuffleKind == 1) // unary
1928       return isVMerge(N, UnitSize, 8, 8);
1929     else if (ShuffleKind == 2) // swapped
1930       return isVMerge(N, UnitSize, 8, 24);
1931     else
1932       return false;
1933   } else {
1934     if (ShuffleKind == 1) // unary
1935       return isVMerge(N, UnitSize, 0, 0);
1936     else if (ShuffleKind == 0) // normal
1937       return isVMerge(N, UnitSize, 0, 16);
1938     else
1939       return false;
1940   }
1941 }
1942 
1943 /**
1944  * Common function used to match vmrgew and vmrgow shuffles
1945  *
1946  * The indexOffset determines whether to look for even or odd words in
1947  * the shuffle mask. This is based on the of the endianness of the target
1948  * machine.
1949  *   - Little Endian:
1950  *     - Use offset of 0 to check for odd elements
1951  *     - Use offset of 4 to check for even elements
1952  *   - Big Endian:
1953  *     - Use offset of 0 to check for even elements
1954  *     - Use offset of 4 to check for odd elements
1955  * A detailed description of the vector element ordering for little endian and
1956  * big endian can be found at
1957  * http://www.ibm.com/developerworks/library/l-ibm-xl-c-cpp-compiler/index.html
1958  * Targeting your applications - what little endian and big endian IBM XL C/C++
1959  * compiler differences mean to you
1960  *
1961  * The mask to the shuffle vector instruction specifies the indices of the
1962  * elements from the two input vectors to place in the result. The elements are
1963  * numbered in array-access order, starting with the first vector. These vectors
1964  * are always of type v16i8, thus each vector will contain 16 elements of size
1965  * 8. More info on the shuffle vector can be found in the
1966  * http://llvm.org/docs/LangRef.html#shufflevector-instruction
1967  * Language Reference.
1968  *
1969  * The RHSStartValue indicates whether the same input vectors are used (unary)
1970  * or two different input vectors are used, based on the following:
1971  *   - If the instruction uses the same vector for both inputs, the range of the
1972  *     indices will be 0 to 15. In this case, the RHSStart value passed should
1973  *     be 0.
1974  *   - If the instruction has two different vectors then the range of the
1975  *     indices will be 0 to 31. In this case, the RHSStart value passed should
1976  *     be 16 (indices 0-15 specify elements in the first vector while indices 16
1977  *     to 31 specify elements in the second vector).
1978  *
1979  * \param[in] N The shuffle vector SD Node to analyze
1980  * \param[in] IndexOffset Specifies whether to look for even or odd elements
1981  * \param[in] RHSStartValue Specifies the starting index for the righthand input
1982  * vector to the shuffle_vector instruction
1983  * \return true iff this shuffle vector represents an even or odd word merge
1984  */
1985 static bool isVMerge(ShuffleVectorSDNode *N, unsigned IndexOffset,
1986                      unsigned RHSStartValue) {
1987   if (N->getValueType(0) != MVT::v16i8)
1988     return false;
1989 
1990   for (unsigned i = 0; i < 2; ++i)
1991     for (unsigned j = 0; j < 4; ++j)
1992       if (!isConstantOrUndef(N->getMaskElt(i*4+j),
1993                              i*RHSStartValue+j+IndexOffset) ||
1994           !isConstantOrUndef(N->getMaskElt(i*4+j+8),
1995                              i*RHSStartValue+j+IndexOffset+8))
1996         return false;
1997   return true;
1998 }
1999 
2000 /**
2001  * Determine if the specified shuffle mask is suitable for the vmrgew or
2002  * vmrgow instructions.
2003  *
2004  * \param[in] N The shuffle vector SD Node to analyze
2005  * \param[in] CheckEven Check for an even merge (true) or an odd merge (false)
2006  * \param[in] ShuffleKind Identify the type of merge:
2007  *   - 0 = big-endian merge with two different inputs;
2008  *   - 1 = either-endian merge with two identical inputs;
2009  *   - 2 = little-endian merge with two different inputs (inputs are swapped for
2010  *     little-endian merges).
2011  * \param[in] DAG The current SelectionDAG
2012  * \return true iff this shuffle mask
2013  */
2014 bool PPC::isVMRGEOShuffleMask(ShuffleVectorSDNode *N, bool CheckEven,
2015                               unsigned ShuffleKind, SelectionDAG &DAG) {
2016   if (DAG.getDataLayout().isLittleEndian()) {
2017     unsigned indexOffset = CheckEven ? 4 : 0;
2018     if (ShuffleKind == 1) // Unary
2019       return isVMerge(N, indexOffset, 0);
2020     else if (ShuffleKind == 2) // swapped
2021       return isVMerge(N, indexOffset, 16);
2022     else
2023       return false;
2024   }
2025   else {
2026     unsigned indexOffset = CheckEven ? 0 : 4;
2027     if (ShuffleKind == 1) // Unary
2028       return isVMerge(N, indexOffset, 0);
2029     else if (ShuffleKind == 0) // Normal
2030       return isVMerge(N, indexOffset, 16);
2031     else
2032       return false;
2033   }
2034   return false;
2035 }
2036 
2037 /// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
2038 /// amount, otherwise return -1.
2039 /// The ShuffleKind distinguishes between big-endian operations with two
2040 /// different inputs (0), either-endian operations with two identical inputs
2041 /// (1), and little-endian operations with two different inputs (2).  For the
2042 /// latter, the input operands are swapped (see PPCInstrAltivec.td).
2043 int PPC::isVSLDOIShuffleMask(SDNode *N, unsigned ShuffleKind,
2044                              SelectionDAG &DAG) {
2045   if (N->getValueType(0) != MVT::v16i8)
2046     return -1;
2047 
2048   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2049 
2050   // Find the first non-undef value in the shuffle mask.
2051   unsigned i;
2052   for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
2053     /*search*/;
2054 
2055   if (i == 16) return -1;  // all undef.
2056 
2057   // Otherwise, check to see if the rest of the elements are consecutively
2058   // numbered from this value.
2059   unsigned ShiftAmt = SVOp->getMaskElt(i);
2060   if (ShiftAmt < i) return -1;
2061 
2062   ShiftAmt -= i;
2063   bool isLE = DAG.getDataLayout().isLittleEndian();
2064 
2065   if ((ShuffleKind == 0 && !isLE) || (ShuffleKind == 2 && isLE)) {
2066     // Check the rest of the elements to see if they are consecutive.
2067     for (++i; i != 16; ++i)
2068       if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
2069         return -1;
2070   } else if (ShuffleKind == 1) {
2071     // Check the rest of the elements to see if they are consecutive.
2072     for (++i; i != 16; ++i)
2073       if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
2074         return -1;
2075   } else
2076     return -1;
2077 
2078   if (isLE)
2079     ShiftAmt = 16 - ShiftAmt;
2080 
2081   return ShiftAmt;
2082 }
2083 
2084 /// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
2085 /// specifies a splat of a single element that is suitable for input to
2086 /// one of the splat operations (VSPLTB/VSPLTH/VSPLTW/XXSPLTW/LXVDSX/etc.).
2087 bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
2088   assert(N->getValueType(0) == MVT::v16i8 && isPowerOf2_32(EltSize) &&
2089          EltSize <= 8 && "Can only handle 1,2,4,8 byte element sizes");
2090 
2091   // The consecutive indices need to specify an element, not part of two
2092   // different elements.  So abandon ship early if this isn't the case.
2093   if (N->getMaskElt(0) % EltSize != 0)
2094     return false;
2095 
2096   // This is a splat operation if each element of the permute is the same, and
2097   // if the value doesn't reference the second vector.
2098   unsigned ElementBase = N->getMaskElt(0);
2099 
2100   // FIXME: Handle UNDEF elements too!
2101   if (ElementBase >= 16)
2102     return false;
2103 
2104   // Check that the indices are consecutive, in the case of a multi-byte element
2105   // splatted with a v16i8 mask.
2106   for (unsigned i = 1; i != EltSize; ++i)
2107     if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
2108       return false;
2109 
2110   for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
2111     if (N->getMaskElt(i) < 0) continue;
2112     for (unsigned j = 0; j != EltSize; ++j)
2113       if (N->getMaskElt(i+j) != N->getMaskElt(j))
2114         return false;
2115   }
2116   return true;
2117 }
2118 
2119 /// Check that the mask is shuffling N byte elements. Within each N byte
2120 /// element of the mask, the indices could be either in increasing or
2121 /// decreasing order as long as they are consecutive.
2122 /// \param[in] N the shuffle vector SD Node to analyze
2123 /// \param[in] Width the element width in bytes, could be 2/4/8/16 (HalfWord/
2124 /// Word/DoubleWord/QuadWord).
2125 /// \param[in] StepLen the delta indices number among the N byte element, if
2126 /// the mask is in increasing/decreasing order then it is 1/-1.
2127 /// \return true iff the mask is shuffling N byte elements.
2128 static bool isNByteElemShuffleMask(ShuffleVectorSDNode *N, unsigned Width,
2129                                    int StepLen) {
2130   assert((Width == 2 || Width == 4 || Width == 8 || Width == 16) &&
2131          "Unexpected element width.");
2132   assert((StepLen == 1 || StepLen == -1) && "Unexpected element width.");
2133 
2134   unsigned NumOfElem = 16 / Width;
2135   unsigned MaskVal[16]; //  Width is never greater than 16
2136   for (unsigned i = 0; i < NumOfElem; ++i) {
2137     MaskVal[0] = N->getMaskElt(i * Width);
2138     if ((StepLen == 1) && (MaskVal[0] % Width)) {
2139       return false;
2140     } else if ((StepLen == -1) && ((MaskVal[0] + 1) % Width)) {
2141       return false;
2142     }
2143 
2144     for (unsigned int j = 1; j < Width; ++j) {
2145       MaskVal[j] = N->getMaskElt(i * Width + j);
2146       if (MaskVal[j] != MaskVal[j-1] + StepLen) {
2147         return false;
2148       }
2149     }
2150   }
2151 
2152   return true;
2153 }
2154 
2155 bool PPC::isXXINSERTWMask(ShuffleVectorSDNode *N, unsigned &ShiftElts,
2156                           unsigned &InsertAtByte, bool &Swap, bool IsLE) {
2157   if (!isNByteElemShuffleMask(N, 4, 1))
2158     return false;
2159 
2160   // Now we look at mask elements 0,4,8,12
2161   unsigned M0 = N->getMaskElt(0) / 4;
2162   unsigned M1 = N->getMaskElt(4) / 4;
2163   unsigned M2 = N->getMaskElt(8) / 4;
2164   unsigned M3 = N->getMaskElt(12) / 4;
2165   unsigned LittleEndianShifts[] = { 2, 1, 0, 3 };
2166   unsigned BigEndianShifts[] = { 3, 0, 1, 2 };
2167 
2168   // Below, let H and L be arbitrary elements of the shuffle mask
2169   // where H is in the range [4,7] and L is in the range [0,3].
2170   // H, 1, 2, 3 or L, 5, 6, 7
2171   if ((M0 > 3 && M1 == 1 && M2 == 2 && M3 == 3) ||
2172       (M0 < 4 && M1 == 5 && M2 == 6 && M3 == 7)) {
2173     ShiftElts = IsLE ? LittleEndianShifts[M0 & 0x3] : BigEndianShifts[M0 & 0x3];
2174     InsertAtByte = IsLE ? 12 : 0;
2175     Swap = M0 < 4;
2176     return true;
2177   }
2178   // 0, H, 2, 3 or 4, L, 6, 7
2179   if ((M1 > 3 && M0 == 0 && M2 == 2 && M3 == 3) ||
2180       (M1 < 4 && M0 == 4 && M2 == 6 && M3 == 7)) {
2181     ShiftElts = IsLE ? LittleEndianShifts[M1 & 0x3] : BigEndianShifts[M1 & 0x3];
2182     InsertAtByte = IsLE ? 8 : 4;
2183     Swap = M1 < 4;
2184     return true;
2185   }
2186   // 0, 1, H, 3 or 4, 5, L, 7
2187   if ((M2 > 3 && M0 == 0 && M1 == 1 && M3 == 3) ||
2188       (M2 < 4 && M0 == 4 && M1 == 5 && M3 == 7)) {
2189     ShiftElts = IsLE ? LittleEndianShifts[M2 & 0x3] : BigEndianShifts[M2 & 0x3];
2190     InsertAtByte = IsLE ? 4 : 8;
2191     Swap = M2 < 4;
2192     return true;
2193   }
2194   // 0, 1, 2, H or 4, 5, 6, L
2195   if ((M3 > 3 && M0 == 0 && M1 == 1 && M2 == 2) ||
2196       (M3 < 4 && M0 == 4 && M1 == 5 && M2 == 6)) {
2197     ShiftElts = IsLE ? LittleEndianShifts[M3 & 0x3] : BigEndianShifts[M3 & 0x3];
2198     InsertAtByte = IsLE ? 0 : 12;
2199     Swap = M3 < 4;
2200     return true;
2201   }
2202 
2203   // If both vector operands for the shuffle are the same vector, the mask will
2204   // contain only elements from the first one and the second one will be undef.
2205   if (N->getOperand(1).isUndef()) {
2206     ShiftElts = 0;
2207     Swap = true;
2208     unsigned XXINSERTWSrcElem = IsLE ? 2 : 1;
2209     if (M0 == XXINSERTWSrcElem && M1 == 1 && M2 == 2 && M3 == 3) {
2210       InsertAtByte = IsLE ? 12 : 0;
2211       return true;
2212     }
2213     if (M0 == 0 && M1 == XXINSERTWSrcElem && M2 == 2 && M3 == 3) {
2214       InsertAtByte = IsLE ? 8 : 4;
2215       return true;
2216     }
2217     if (M0 == 0 && M1 == 1 && M2 == XXINSERTWSrcElem && M3 == 3) {
2218       InsertAtByte = IsLE ? 4 : 8;
2219       return true;
2220     }
2221     if (M0 == 0 && M1 == 1 && M2 == 2 && M3 == XXINSERTWSrcElem) {
2222       InsertAtByte = IsLE ? 0 : 12;
2223       return true;
2224     }
2225   }
2226 
2227   return false;
2228 }
2229 
2230 bool PPC::isXXSLDWIShuffleMask(ShuffleVectorSDNode *N, unsigned &ShiftElts,
2231                                bool &Swap, bool IsLE) {
2232   assert(N->getValueType(0) == MVT::v16i8 && "Shuffle vector expects v16i8");
2233   // Ensure each byte index of the word is consecutive.
2234   if (!isNByteElemShuffleMask(N, 4, 1))
2235     return false;
2236 
2237   // Now we look at mask elements 0,4,8,12, which are the beginning of words.
2238   unsigned M0 = N->getMaskElt(0) / 4;
2239   unsigned M1 = N->getMaskElt(4) / 4;
2240   unsigned M2 = N->getMaskElt(8) / 4;
2241   unsigned M3 = N->getMaskElt(12) / 4;
2242 
2243   // If both vector operands for the shuffle are the same vector, the mask will
2244   // contain only elements from the first one and the second one will be undef.
2245   if (N->getOperand(1).isUndef()) {
2246     assert(M0 < 4 && "Indexing into an undef vector?");
2247     if (M1 != (M0 + 1) % 4 || M2 != (M1 + 1) % 4 || M3 != (M2 + 1) % 4)
2248       return false;
2249 
2250     ShiftElts = IsLE ? (4 - M0) % 4 : M0;
2251     Swap = false;
2252     return true;
2253   }
2254 
2255   // Ensure each word index of the ShuffleVector Mask is consecutive.
2256   if (M1 != (M0 + 1) % 8 || M2 != (M1 + 1) % 8 || M3 != (M2 + 1) % 8)
2257     return false;
2258 
2259   if (IsLE) {
2260     if (M0 == 0 || M0 == 7 || M0 == 6 || M0 == 5) {
2261       // Input vectors don't need to be swapped if the leading element
2262       // of the result is one of the 3 left elements of the second vector
2263       // (or if there is no shift to be done at all).
2264       Swap = false;
2265       ShiftElts = (8 - M0) % 8;
2266     } else if (M0 == 4 || M0 == 3 || M0 == 2 || M0 == 1) {
2267       // Input vectors need to be swapped if the leading element
2268       // of the result is one of the 3 left elements of the first vector
2269       // (or if we're shifting by 4 - thereby simply swapping the vectors).
2270       Swap = true;
2271       ShiftElts = (4 - M0) % 4;
2272     }
2273 
2274     return true;
2275   } else {                                          // BE
2276     if (M0 == 0 || M0 == 1 || M0 == 2 || M0 == 3) {
2277       // Input vectors don't need to be swapped if the leading element
2278       // of the result is one of the 4 elements of the first vector.
2279       Swap = false;
2280       ShiftElts = M0;
2281     } else if (M0 == 4 || M0 == 5 || M0 == 6 || M0 == 7) {
2282       // Input vectors need to be swapped if the leading element
2283       // of the result is one of the 4 elements of the right vector.
2284       Swap = true;
2285       ShiftElts = M0 - 4;
2286     }
2287 
2288     return true;
2289   }
2290 }
2291 
2292 bool static isXXBRShuffleMaskHelper(ShuffleVectorSDNode *N, int Width) {
2293   assert(N->getValueType(0) == MVT::v16i8 && "Shuffle vector expects v16i8");
2294 
2295   if (!isNByteElemShuffleMask(N, Width, -1))
2296     return false;
2297 
2298   for (int i = 0; i < 16; i += Width)
2299     if (N->getMaskElt(i) != i + Width - 1)
2300       return false;
2301 
2302   return true;
2303 }
2304 
2305 bool PPC::isXXBRHShuffleMask(ShuffleVectorSDNode *N) {
2306   return isXXBRShuffleMaskHelper(N, 2);
2307 }
2308 
2309 bool PPC::isXXBRWShuffleMask(ShuffleVectorSDNode *N) {
2310   return isXXBRShuffleMaskHelper(N, 4);
2311 }
2312 
2313 bool PPC::isXXBRDShuffleMask(ShuffleVectorSDNode *N) {
2314   return isXXBRShuffleMaskHelper(N, 8);
2315 }
2316 
2317 bool PPC::isXXBRQShuffleMask(ShuffleVectorSDNode *N) {
2318   return isXXBRShuffleMaskHelper(N, 16);
2319 }
2320 
2321 /// Can node \p N be lowered to an XXPERMDI instruction? If so, set \p Swap
2322 /// if the inputs to the instruction should be swapped and set \p DM to the
2323 /// value for the immediate.
2324 /// Specifically, set \p Swap to true only if \p N can be lowered to XXPERMDI
2325 /// AND element 0 of the result comes from the first input (LE) or second input
2326 /// (BE). Set \p DM to the calculated result (0-3) only if \p N can be lowered.
2327 /// \return true iff the given mask of shuffle node \p N is a XXPERMDI shuffle
2328 /// mask.
2329 bool PPC::isXXPERMDIShuffleMask(ShuffleVectorSDNode *N, unsigned &DM,
2330                                bool &Swap, bool IsLE) {
2331   assert(N->getValueType(0) == MVT::v16i8 && "Shuffle vector expects v16i8");
2332 
2333   // Ensure each byte index of the double word is consecutive.
2334   if (!isNByteElemShuffleMask(N, 8, 1))
2335     return false;
2336 
2337   unsigned M0 = N->getMaskElt(0) / 8;
2338   unsigned M1 = N->getMaskElt(8) / 8;
2339   assert(((M0 | M1) < 4) && "A mask element out of bounds?");
2340 
2341   // If both vector operands for the shuffle are the same vector, the mask will
2342   // contain only elements from the first one and the second one will be undef.
2343   if (N->getOperand(1).isUndef()) {
2344     if ((M0 | M1) < 2) {
2345       DM = IsLE ? (((~M1) & 1) << 1) + ((~M0) & 1) : (M0 << 1) + (M1 & 1);
2346       Swap = false;
2347       return true;
2348     } else
2349       return false;
2350   }
2351 
2352   if (IsLE) {
2353     if (M0 > 1 && M1 < 2) {
2354       Swap = false;
2355     } else if (M0 < 2 && M1 > 1) {
2356       M0 = (M0 + 2) % 4;
2357       M1 = (M1 + 2) % 4;
2358       Swap = true;
2359     } else
2360       return false;
2361 
2362     // Note: if control flow comes here that means Swap is already set above
2363     DM = (((~M1) & 1) << 1) + ((~M0) & 1);
2364     return true;
2365   } else { // BE
2366     if (M0 < 2 && M1 > 1) {
2367       Swap = false;
2368     } else if (M0 > 1 && M1 < 2) {
2369       M0 = (M0 + 2) % 4;
2370       M1 = (M1 + 2) % 4;
2371       Swap = true;
2372     } else
2373       return false;
2374 
2375     // Note: if control flow comes here that means Swap is already set above
2376     DM = (M0 << 1) + (M1 & 1);
2377     return true;
2378   }
2379 }
2380 
2381 
2382 /// getSplatIdxForPPCMnemonics - Return the splat index as a value that is
2383 /// appropriate for PPC mnemonics (which have a big endian bias - namely
2384 /// elements are counted from the left of the vector register).
2385 unsigned PPC::getSplatIdxForPPCMnemonics(SDNode *N, unsigned EltSize,
2386                                          SelectionDAG &DAG) {
2387   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2388   assert(isSplatShuffleMask(SVOp, EltSize));
2389   if (DAG.getDataLayout().isLittleEndian())
2390     return (16 / EltSize) - 1 - (SVOp->getMaskElt(0) / EltSize);
2391   else
2392     return SVOp->getMaskElt(0) / EltSize;
2393 }
2394 
2395 /// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
2396 /// by using a vspltis[bhw] instruction of the specified element size, return
2397 /// the constant being splatted.  The ByteSize field indicates the number of
2398 /// bytes of each element [124] -> [bhw].
2399 SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2400   SDValue OpVal(nullptr, 0);
2401 
2402   // If ByteSize of the splat is bigger than the element size of the
2403   // build_vector, then we have a case where we are checking for a splat where
2404   // multiple elements of the buildvector are folded together into a single
2405   // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
2406   unsigned EltSize = 16/N->getNumOperands();
2407   if (EltSize < ByteSize) {
2408     unsigned Multiple = ByteSize/EltSize;   // Number of BV entries per spltval.
2409     SDValue UniquedVals[4];
2410     assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
2411 
2412     // See if all of the elements in the buildvector agree across.
2413     for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2414       if (N->getOperand(i).isUndef()) continue;
2415       // If the element isn't a constant, bail fully out.
2416       if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
2417 
2418       if (!UniquedVals[i&(Multiple-1)].getNode())
2419         UniquedVals[i&(Multiple-1)] = N->getOperand(i);
2420       else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
2421         return SDValue();  // no match.
2422     }
2423 
2424     // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
2425     // either constant or undef values that are identical for each chunk.  See
2426     // if these chunks can form into a larger vspltis*.
2427 
2428     // Check to see if all of the leading entries are either 0 or -1.  If
2429     // neither, then this won't fit into the immediate field.
2430     bool LeadingZero = true;
2431     bool LeadingOnes = true;
2432     for (unsigned i = 0; i != Multiple-1; ++i) {
2433       if (!UniquedVals[i].getNode()) continue;  // Must have been undefs.
2434 
2435       LeadingZero &= isNullConstant(UniquedVals[i]);
2436       LeadingOnes &= isAllOnesConstant(UniquedVals[i]);
2437     }
2438     // Finally, check the least significant entry.
2439     if (LeadingZero) {
2440       if (!UniquedVals[Multiple-1].getNode())
2441         return DAG.getTargetConstant(0, SDLoc(N), MVT::i32);  // 0,0,0,undef
2442       int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
2443       if (Val < 16)                                   // 0,0,0,4 -> vspltisw(4)
2444         return DAG.getTargetConstant(Val, SDLoc(N), MVT::i32);
2445     }
2446     if (LeadingOnes) {
2447       if (!UniquedVals[Multiple-1].getNode())
2448         return DAG.getTargetConstant(~0U, SDLoc(N), MVT::i32); // -1,-1,-1,undef
2449       int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
2450       if (Val >= -16)                            // -1,-1,-1,-2 -> vspltisw(-2)
2451         return DAG.getTargetConstant(Val, SDLoc(N), MVT::i32);
2452     }
2453 
2454     return SDValue();
2455   }
2456 
2457   // Check to see if this buildvec has a single non-undef value in its elements.
2458   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2459     if (N->getOperand(i).isUndef()) continue;
2460     if (!OpVal.getNode())
2461       OpVal = N->getOperand(i);
2462     else if (OpVal != N->getOperand(i))
2463       return SDValue();
2464   }
2465 
2466   if (!OpVal.getNode()) return SDValue();  // All UNDEF: use implicit def.
2467 
2468   unsigned ValSizeInBytes = EltSize;
2469   uint64_t Value = 0;
2470   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
2471     Value = CN->getZExtValue();
2472   } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
2473     assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
2474     Value = FloatToBits(CN->getValueAPF().convertToFloat());
2475   }
2476 
2477   // If the splat value is larger than the element value, then we can never do
2478   // this splat.  The only case that we could fit the replicated bits into our
2479   // immediate field for would be zero, and we prefer to use vxor for it.
2480   if (ValSizeInBytes < ByteSize) return SDValue();
2481 
2482   // If the element value is larger than the splat value, check if it consists
2483   // of a repeated bit pattern of size ByteSize.
2484   if (!APInt(ValSizeInBytes * 8, Value).isSplat(ByteSize * 8))
2485     return SDValue();
2486 
2487   // Properly sign extend the value.
2488   int MaskVal = SignExtend32(Value, ByteSize * 8);
2489 
2490   // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
2491   if (MaskVal == 0) return SDValue();
2492 
2493   // Finally, if this value fits in a 5 bit sext field, return it
2494   if (SignExtend32<5>(MaskVal) == MaskVal)
2495     return DAG.getTargetConstant(MaskVal, SDLoc(N), MVT::i32);
2496   return SDValue();
2497 }
2498 
2499 //===----------------------------------------------------------------------===//
2500 //  Addressing Mode Selection
2501 //===----------------------------------------------------------------------===//
2502 
2503 /// isIntS16Immediate - This method tests to see if the node is either a 32-bit
2504 /// or 64-bit immediate, and if the value can be accurately represented as a
2505 /// sign extension from a 16-bit value.  If so, this returns true and the
2506 /// immediate.
2507 bool llvm::isIntS16Immediate(SDNode *N, int16_t &Imm) {
2508   if (!isa<ConstantSDNode>(N))
2509     return false;
2510 
2511   Imm = (int16_t)cast<ConstantSDNode>(N)->getZExtValue();
2512   if (N->getValueType(0) == MVT::i32)
2513     return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
2514   else
2515     return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
2516 }
2517 bool llvm::isIntS16Immediate(SDValue Op, int16_t &Imm) {
2518   return isIntS16Immediate(Op.getNode(), Imm);
2519 }
2520 
2521 /// Used when computing address flags for selecting loads and stores.
2522 /// If we have an OR, check if the LHS and RHS are provably disjoint.
2523 /// An OR of two provably disjoint values is equivalent to an ADD.
2524 /// Most PPC load/store instructions compute the effective address as a sum,
2525 /// so doing this conversion is useful.
2526 static bool provablyDisjointOr(SelectionDAG &DAG, const SDValue &N) {
2527   if (N.getOpcode() != ISD::OR)
2528     return false;
2529   KnownBits LHSKnown = DAG.computeKnownBits(N.getOperand(0));
2530   if (!LHSKnown.Zero.getBoolValue())
2531     return false;
2532   KnownBits RHSKnown = DAG.computeKnownBits(N.getOperand(1));
2533   return (~(LHSKnown.Zero | RHSKnown.Zero) == 0);
2534 }
2535 
2536 /// SelectAddressEVXRegReg - Given the specified address, check to see if it can
2537 /// be represented as an indexed [r+r] operation.
2538 bool PPCTargetLowering::SelectAddressEVXRegReg(SDValue N, SDValue &Base,
2539                                                SDValue &Index,
2540                                                SelectionDAG &DAG) const {
2541   for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
2542       UI != E; ++UI) {
2543     if (MemSDNode *Memop = dyn_cast<MemSDNode>(*UI)) {
2544       if (Memop->getMemoryVT() == MVT::f64) {
2545           Base = N.getOperand(0);
2546           Index = N.getOperand(1);
2547           return true;
2548       }
2549     }
2550   }
2551   return false;
2552 }
2553 
2554 /// isIntS34Immediate - This method tests if value of node given can be
2555 /// accurately represented as a sign extension from a 34-bit value.  If so,
2556 /// this returns true and the immediate.
2557 bool llvm::isIntS34Immediate(SDNode *N, int64_t &Imm) {
2558   if (!isa<ConstantSDNode>(N))
2559     return false;
2560 
2561   Imm = (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
2562   return isInt<34>(Imm);
2563 }
2564 bool llvm::isIntS34Immediate(SDValue Op, int64_t &Imm) {
2565   return isIntS34Immediate(Op.getNode(), Imm);
2566 }
2567 
2568 /// SelectAddressRegReg - Given the specified addressed, check to see if it
2569 /// can be represented as an indexed [r+r] operation.  Returns false if it
2570 /// can be more efficiently represented as [r+imm]. If \p EncodingAlignment is
2571 /// non-zero and N can be represented by a base register plus a signed 16-bit
2572 /// displacement, make a more precise judgement by checking (displacement % \p
2573 /// EncodingAlignment).
2574 bool PPCTargetLowering::SelectAddressRegReg(
2575     SDValue N, SDValue &Base, SDValue &Index, SelectionDAG &DAG,
2576     MaybeAlign EncodingAlignment) const {
2577   // If we have a PC Relative target flag don't select as [reg+reg]. It will be
2578   // a [pc+imm].
2579   if (SelectAddressPCRel(N, Base))
2580     return false;
2581 
2582   int16_t Imm = 0;
2583   if (N.getOpcode() == ISD::ADD) {
2584     // Is there any SPE load/store (f64), which can't handle 16bit offset?
2585     // SPE load/store can only handle 8-bit offsets.
2586     if (hasSPE() && SelectAddressEVXRegReg(N, Base, Index, DAG))
2587         return true;
2588     if (isIntS16Immediate(N.getOperand(1), Imm) &&
2589         (!EncodingAlignment || isAligned(*EncodingAlignment, Imm)))
2590       return false; // r+i
2591     if (N.getOperand(1).getOpcode() == PPCISD::Lo)
2592       return false;    // r+i
2593 
2594     Base = N.getOperand(0);
2595     Index = N.getOperand(1);
2596     return true;
2597   } else if (N.getOpcode() == ISD::OR) {
2598     if (isIntS16Immediate(N.getOperand(1), Imm) &&
2599         (!EncodingAlignment || isAligned(*EncodingAlignment, Imm)))
2600       return false; // r+i can fold it if we can.
2601 
2602     // If this is an or of disjoint bitfields, we can codegen this as an add
2603     // (for better address arithmetic) if the LHS and RHS of the OR are provably
2604     // disjoint.
2605     KnownBits LHSKnown = DAG.computeKnownBits(N.getOperand(0));
2606 
2607     if (LHSKnown.Zero.getBoolValue()) {
2608       KnownBits RHSKnown = DAG.computeKnownBits(N.getOperand(1));
2609       // If all of the bits are known zero on the LHS or RHS, the add won't
2610       // carry.
2611       if (~(LHSKnown.Zero | RHSKnown.Zero) == 0) {
2612         Base = N.getOperand(0);
2613         Index = N.getOperand(1);
2614         return true;
2615       }
2616     }
2617   }
2618 
2619   return false;
2620 }
2621 
2622 // If we happen to be doing an i64 load or store into a stack slot that has
2623 // less than a 4-byte alignment, then the frame-index elimination may need to
2624 // use an indexed load or store instruction (because the offset may not be a
2625 // multiple of 4). The extra register needed to hold the offset comes from the
2626 // register scavenger, and it is possible that the scavenger will need to use
2627 // an emergency spill slot. As a result, we need to make sure that a spill slot
2628 // is allocated when doing an i64 load/store into a less-than-4-byte-aligned
2629 // stack slot.
2630 static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
2631   // FIXME: This does not handle the LWA case.
2632   if (VT != MVT::i64)
2633     return;
2634 
2635   // NOTE: We'll exclude negative FIs here, which come from argument
2636   // lowering, because there are no known test cases triggering this problem
2637   // using packed structures (or similar). We can remove this exclusion if
2638   // we find such a test case. The reason why this is so test-case driven is
2639   // because this entire 'fixup' is only to prevent crashes (from the
2640   // register scavenger) on not-really-valid inputs. For example, if we have:
2641   //   %a = alloca i1
2642   //   %b = bitcast i1* %a to i64*
2643   //   store i64* a, i64 b
2644   // then the store should really be marked as 'align 1', but is not. If it
2645   // were marked as 'align 1' then the indexed form would have been
2646   // instruction-selected initially, and the problem this 'fixup' is preventing
2647   // won't happen regardless.
2648   if (FrameIdx < 0)
2649     return;
2650 
2651   MachineFunction &MF = DAG.getMachineFunction();
2652   MachineFrameInfo &MFI = MF.getFrameInfo();
2653 
2654   if (MFI.getObjectAlign(FrameIdx) >= Align(4))
2655     return;
2656 
2657   PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2658   FuncInfo->setHasNonRISpills();
2659 }
2660 
2661 /// Returns true if the address N can be represented by a base register plus
2662 /// a signed 16-bit displacement [r+imm], and if it is not better
2663 /// represented as reg+reg.  If \p EncodingAlignment is non-zero, only accept
2664 /// displacements that are multiples of that value.
2665 bool PPCTargetLowering::SelectAddressRegImm(
2666     SDValue N, SDValue &Disp, SDValue &Base, SelectionDAG &DAG,
2667     MaybeAlign EncodingAlignment) const {
2668   // FIXME dl should come from parent load or store, not from address
2669   SDLoc dl(N);
2670 
2671   // If we have a PC Relative target flag don't select as [reg+imm]. It will be
2672   // a [pc+imm].
2673   if (SelectAddressPCRel(N, Base))
2674     return false;
2675 
2676   // If this can be more profitably realized as r+r, fail.
2677   if (SelectAddressRegReg(N, Disp, Base, DAG, EncodingAlignment))
2678     return false;
2679 
2680   if (N.getOpcode() == ISD::ADD) {
2681     int16_t imm = 0;
2682     if (isIntS16Immediate(N.getOperand(1), imm) &&
2683         (!EncodingAlignment || isAligned(*EncodingAlignment, imm))) {
2684       Disp = DAG.getTargetConstant(imm, dl, N.getValueType());
2685       if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
2686         Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
2687         fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
2688       } else {
2689         Base = N.getOperand(0);
2690       }
2691       return true; // [r+i]
2692     } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
2693       // Match LOAD (ADD (X, Lo(G))).
2694       assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
2695              && "Cannot handle constant offsets yet!");
2696       Disp = N.getOperand(1).getOperand(0);  // The global address.
2697       assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
2698              Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
2699              Disp.getOpcode() == ISD::TargetConstantPool ||
2700              Disp.getOpcode() == ISD::TargetJumpTable);
2701       Base = N.getOperand(0);
2702       return true;  // [&g+r]
2703     }
2704   } else if (N.getOpcode() == ISD::OR) {
2705     int16_t imm = 0;
2706     if (isIntS16Immediate(N.getOperand(1), imm) &&
2707         (!EncodingAlignment || isAligned(*EncodingAlignment, imm))) {
2708       // If this is an or of disjoint bitfields, we can codegen this as an add
2709       // (for better address arithmetic) if the LHS and RHS of the OR are
2710       // provably disjoint.
2711       KnownBits LHSKnown = DAG.computeKnownBits(N.getOperand(0));
2712 
2713       if ((LHSKnown.Zero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
2714         // If all of the bits are known zero on the LHS or RHS, the add won't
2715         // carry.
2716         if (FrameIndexSDNode *FI =
2717               dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
2718           Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
2719           fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
2720         } else {
2721           Base = N.getOperand(0);
2722         }
2723         Disp = DAG.getTargetConstant(imm, dl, N.getValueType());
2724         return true;
2725       }
2726     }
2727   } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
2728     // Loading from a constant address.
2729 
2730     // If this address fits entirely in a 16-bit sext immediate field, codegen
2731     // this as "d, 0"
2732     int16_t Imm;
2733     if (isIntS16Immediate(CN, Imm) &&
2734         (!EncodingAlignment || isAligned(*EncodingAlignment, Imm))) {
2735       Disp = DAG.getTargetConstant(Imm, dl, CN->getValueType(0));
2736       Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
2737                              CN->getValueType(0));
2738       return true;
2739     }
2740 
2741     // Handle 32-bit sext immediates with LIS + addr mode.
2742     if ((CN->getValueType(0) == MVT::i32 ||
2743          (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
2744         (!EncodingAlignment ||
2745          isAligned(*EncodingAlignment, CN->getZExtValue()))) {
2746       int Addr = (int)CN->getZExtValue();
2747 
2748       // Otherwise, break this down into an LIS + disp.
2749       Disp = DAG.getTargetConstant((short)Addr, dl, MVT::i32);
2750 
2751       Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, dl,
2752                                    MVT::i32);
2753       unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
2754       Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
2755       return true;
2756     }
2757   }
2758 
2759   Disp = DAG.getTargetConstant(0, dl, getPointerTy(DAG.getDataLayout()));
2760   if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
2761     Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
2762     fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
2763   } else
2764     Base = N;
2765   return true;      // [r+0]
2766 }
2767 
2768 /// Similar to the 16-bit case but for instructions that take a 34-bit
2769 /// displacement field (prefixed loads/stores).
2770 bool PPCTargetLowering::SelectAddressRegImm34(SDValue N, SDValue &Disp,
2771                                               SDValue &Base,
2772                                               SelectionDAG &DAG) const {
2773   // Only on 64-bit targets.
2774   if (N.getValueType() != MVT::i64)
2775     return false;
2776 
2777   SDLoc dl(N);
2778   int64_t Imm = 0;
2779 
2780   if (N.getOpcode() == ISD::ADD) {
2781     if (!isIntS34Immediate(N.getOperand(1), Imm))
2782       return false;
2783     Disp = DAG.getTargetConstant(Imm, dl, N.getValueType());
2784     if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0)))
2785       Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
2786     else
2787       Base = N.getOperand(0);
2788     return true;
2789   }
2790 
2791   if (N.getOpcode() == ISD::OR) {
2792     if (!isIntS34Immediate(N.getOperand(1), Imm))
2793       return false;
2794     // If this is an or of disjoint bitfields, we can codegen this as an add
2795     // (for better address arithmetic) if the LHS and RHS of the OR are
2796     // provably disjoint.
2797     KnownBits LHSKnown = DAG.computeKnownBits(N.getOperand(0));
2798     if ((LHSKnown.Zero.getZExtValue() | ~(uint64_t)Imm) != ~0ULL)
2799       return false;
2800     if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0)))
2801       Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
2802     else
2803       Base = N.getOperand(0);
2804     Disp = DAG.getTargetConstant(Imm, dl, N.getValueType());
2805     return true;
2806   }
2807 
2808   if (isIntS34Immediate(N, Imm)) { // If the address is a 34-bit const.
2809     Disp = DAG.getTargetConstant(Imm, dl, N.getValueType());
2810     Base = DAG.getRegister(PPC::ZERO8, N.getValueType());
2811     return true;
2812   }
2813 
2814   return false;
2815 }
2816 
2817 /// SelectAddressRegRegOnly - Given the specified addressed, force it to be
2818 /// represented as an indexed [r+r] operation.
2819 bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
2820                                                 SDValue &Index,
2821                                                 SelectionDAG &DAG) const {
2822   // Check to see if we can easily represent this as an [r+r] address.  This
2823   // will fail if it thinks that the address is more profitably represented as
2824   // reg+imm, e.g. where imm = 0.
2825   if (SelectAddressRegReg(N, Base, Index, DAG))
2826     return true;
2827 
2828   // If the address is the result of an add, we will utilize the fact that the
2829   // address calculation includes an implicit add.  However, we can reduce
2830   // register pressure if we do not materialize a constant just for use as the
2831   // index register.  We only get rid of the add if it is not an add of a
2832   // value and a 16-bit signed constant and both have a single use.
2833   int16_t imm = 0;
2834   if (N.getOpcode() == ISD::ADD &&
2835       (!isIntS16Immediate(N.getOperand(1), imm) ||
2836        !N.getOperand(1).hasOneUse() || !N.getOperand(0).hasOneUse())) {
2837     Base = N.getOperand(0);
2838     Index = N.getOperand(1);
2839     return true;
2840   }
2841 
2842   // Otherwise, do it the hard way, using R0 as the base register.
2843   Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
2844                          N.getValueType());
2845   Index = N;
2846   return true;
2847 }
2848 
2849 template <typename Ty> static bool isValidPCRelNode(SDValue N) {
2850   Ty *PCRelCand = dyn_cast<Ty>(N);
2851   return PCRelCand && (PCRelCand->getTargetFlags() & PPCII::MO_PCREL_FLAG);
2852 }
2853 
2854 /// Returns true if this address is a PC Relative address.
2855 /// PC Relative addresses are marked with the flag PPCII::MO_PCREL_FLAG
2856 /// or if the node opcode is PPCISD::MAT_PCREL_ADDR.
2857 bool PPCTargetLowering::SelectAddressPCRel(SDValue N, SDValue &Base) const {
2858   // This is a materialize PC Relative node. Always select this as PC Relative.
2859   Base = N;
2860   if (N.getOpcode() == PPCISD::MAT_PCREL_ADDR)
2861     return true;
2862   if (isValidPCRelNode<ConstantPoolSDNode>(N) ||
2863       isValidPCRelNode<GlobalAddressSDNode>(N) ||
2864       isValidPCRelNode<JumpTableSDNode>(N) ||
2865       isValidPCRelNode<BlockAddressSDNode>(N))
2866     return true;
2867   return false;
2868 }
2869 
2870 /// Returns true if we should use a direct load into vector instruction
2871 /// (such as lxsd or lfd), instead of a load into gpr + direct move sequence.
2872 static bool usePartialVectorLoads(SDNode *N, const PPCSubtarget& ST) {
2873 
2874   // If there are any other uses other than scalar to vector, then we should
2875   // keep it as a scalar load -> direct move pattern to prevent multiple
2876   // loads.
2877   LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
2878   if (!LD)
2879     return false;
2880 
2881   EVT MemVT = LD->getMemoryVT();
2882   if (!MemVT.isSimple())
2883     return false;
2884   switch(MemVT.getSimpleVT().SimpleTy) {
2885   case MVT::i64:
2886     break;
2887   case MVT::i32:
2888     if (!ST.hasP8Vector())
2889       return false;
2890     break;
2891   case MVT::i16:
2892   case MVT::i8:
2893     if (!ST.hasP9Vector())
2894       return false;
2895     break;
2896   default:
2897     return false;
2898   }
2899 
2900   SDValue LoadedVal(N, 0);
2901   if (!LoadedVal.hasOneUse())
2902     return false;
2903 
2904   for (SDNode::use_iterator UI = LD->use_begin(), UE = LD->use_end();
2905        UI != UE; ++UI)
2906     if (UI.getUse().get().getResNo() == 0 &&
2907         UI->getOpcode() != ISD::SCALAR_TO_VECTOR &&
2908         UI->getOpcode() != PPCISD::SCALAR_TO_VECTOR_PERMUTED)
2909       return false;
2910 
2911   return true;
2912 }
2913 
2914 /// getPreIndexedAddressParts - returns true by value, base pointer and
2915 /// offset pointer and addressing mode by reference if the node's address
2916 /// can be legally represented as pre-indexed load / store address.
2917 bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
2918                                                   SDValue &Offset,
2919                                                   ISD::MemIndexedMode &AM,
2920                                                   SelectionDAG &DAG) const {
2921   if (DisablePPCPreinc) return false;
2922 
2923   bool isLoad = true;
2924   SDValue Ptr;
2925   EVT VT;
2926   unsigned Alignment;
2927   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
2928     Ptr = LD->getBasePtr();
2929     VT = LD->getMemoryVT();
2930     Alignment = LD->getAlignment();
2931   } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
2932     Ptr = ST->getBasePtr();
2933     VT  = ST->getMemoryVT();
2934     Alignment = ST->getAlignment();
2935     isLoad = false;
2936   } else
2937     return false;
2938 
2939   // Do not generate pre-inc forms for specific loads that feed scalar_to_vector
2940   // instructions because we can fold these into a more efficient instruction
2941   // instead, (such as LXSD).
2942   if (isLoad && usePartialVectorLoads(N, Subtarget)) {
2943     return false;
2944   }
2945 
2946   // PowerPC doesn't have preinc load/store instructions for vectors
2947   if (VT.isVector())
2948     return false;
2949 
2950   if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
2951     // Common code will reject creating a pre-inc form if the base pointer
2952     // is a frame index, or if N is a store and the base pointer is either
2953     // the same as or a predecessor of the value being stored.  Check for
2954     // those situations here, and try with swapped Base/Offset instead.
2955     bool Swap = false;
2956 
2957     if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
2958       Swap = true;
2959     else if (!isLoad) {
2960       SDValue Val = cast<StoreSDNode>(N)->getValue();
2961       if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
2962         Swap = true;
2963     }
2964 
2965     if (Swap)
2966       std::swap(Base, Offset);
2967 
2968     AM = ISD::PRE_INC;
2969     return true;
2970   }
2971 
2972   // LDU/STU can only handle immediates that are a multiple of 4.
2973   if (VT != MVT::i64) {
2974     if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, None))
2975       return false;
2976   } else {
2977     // LDU/STU need an address with at least 4-byte alignment.
2978     if (Alignment < 4)
2979       return false;
2980 
2981     if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, Align(4)))
2982       return false;
2983   }
2984 
2985   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
2986     // PPC64 doesn't have lwau, but it does have lwaux.  Reject preinc load of
2987     // sext i32 to i64 when addr mode is r+i.
2988     if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
2989         LD->getExtensionType() == ISD::SEXTLOAD &&
2990         isa<ConstantSDNode>(Offset))
2991       return false;
2992   }
2993 
2994   AM = ISD::PRE_INC;
2995   return true;
2996 }
2997 
2998 //===----------------------------------------------------------------------===//
2999 //  LowerOperation implementation
3000 //===----------------------------------------------------------------------===//
3001 
3002 /// Return true if we should reference labels using a PICBase, set the HiOpFlags
3003 /// and LoOpFlags to the target MO flags.
3004 static void getLabelAccessInfo(bool IsPIC, const PPCSubtarget &Subtarget,
3005                                unsigned &HiOpFlags, unsigned &LoOpFlags,
3006                                const GlobalValue *GV = nullptr) {
3007   HiOpFlags = PPCII::MO_HA;
3008   LoOpFlags = PPCII::MO_LO;
3009 
3010   // Don't use the pic base if not in PIC relocation model.
3011   if (IsPIC) {
3012     HiOpFlags |= PPCII::MO_PIC_FLAG;
3013     LoOpFlags |= PPCII::MO_PIC_FLAG;
3014   }
3015 }
3016 
3017 static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
3018                              SelectionDAG &DAG) {
3019   SDLoc DL(HiPart);
3020   EVT PtrVT = HiPart.getValueType();
3021   SDValue Zero = DAG.getConstant(0, DL, PtrVT);
3022 
3023   SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
3024   SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
3025 
3026   // With PIC, the first instruction is actually "GR+hi(&G)".
3027   if (isPIC)
3028     Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
3029                      DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
3030 
3031   // Generate non-pic code that has direct accesses to the constant pool.
3032   // The address of the global is just (hi(&g)+lo(&g)).
3033   return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
3034 }
3035 
3036 static void setUsesTOCBasePtr(MachineFunction &MF) {
3037   PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
3038   FuncInfo->setUsesTOCBasePtr();
3039 }
3040 
3041 static void setUsesTOCBasePtr(SelectionDAG &DAG) {
3042   setUsesTOCBasePtr(DAG.getMachineFunction());
3043 }
3044 
3045 SDValue PPCTargetLowering::getTOCEntry(SelectionDAG &DAG, const SDLoc &dl,
3046                                        SDValue GA) const {
3047   const bool Is64Bit = Subtarget.isPPC64();
3048   EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
3049   SDValue Reg = Is64Bit ? DAG.getRegister(PPC::X2, VT)
3050                         : Subtarget.isAIXABI()
3051                               ? DAG.getRegister(PPC::R2, VT)
3052                               : DAG.getNode(PPCISD::GlobalBaseReg, dl, VT);
3053   SDValue Ops[] = { GA, Reg };
3054   return DAG.getMemIntrinsicNode(
3055       PPCISD::TOC_ENTRY, dl, DAG.getVTList(VT, MVT::Other), Ops, VT,
3056       MachinePointerInfo::getGOT(DAG.getMachineFunction()), None,
3057       MachineMemOperand::MOLoad);
3058 }
3059 
3060 SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
3061                                              SelectionDAG &DAG) const {
3062   EVT PtrVT = Op.getValueType();
3063   ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3064   const Constant *C = CP->getConstVal();
3065 
3066   // 64-bit SVR4 ABI and AIX ABI code are always position-independent.
3067   // The actual address of the GlobalValue is stored in the TOC.
3068   if (Subtarget.is64BitELFABI() || Subtarget.isAIXABI()) {
3069     if (Subtarget.isUsingPCRelativeCalls()) {
3070       SDLoc DL(CP);
3071       EVT Ty = getPointerTy(DAG.getDataLayout());
3072       SDValue ConstPool = DAG.getTargetConstantPool(
3073           C, Ty, CP->getAlign(), CP->getOffset(), PPCII::MO_PCREL_FLAG);
3074       return DAG.getNode(PPCISD::MAT_PCREL_ADDR, DL, Ty, ConstPool);
3075     }
3076     setUsesTOCBasePtr(DAG);
3077     SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlign(), 0);
3078     return getTOCEntry(DAG, SDLoc(CP), GA);
3079   }
3080 
3081   unsigned MOHiFlag, MOLoFlag;
3082   bool IsPIC = isPositionIndependent();
3083   getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag);
3084 
3085   if (IsPIC && Subtarget.isSVR4ABI()) {
3086     SDValue GA =
3087         DAG.getTargetConstantPool(C, PtrVT, CP->getAlign(), PPCII::MO_PIC_FLAG);
3088     return getTOCEntry(DAG, SDLoc(CP), GA);
3089   }
3090 
3091   SDValue CPIHi =
3092       DAG.getTargetConstantPool(C, PtrVT, CP->getAlign(), 0, MOHiFlag);
3093   SDValue CPILo =
3094       DAG.getTargetConstantPool(C, PtrVT, CP->getAlign(), 0, MOLoFlag);
3095   return LowerLabelRef(CPIHi, CPILo, IsPIC, DAG);
3096 }
3097 
3098 // For 64-bit PowerPC, prefer the more compact relative encodings.
3099 // This trades 32 bits per jump table entry for one or two instructions
3100 // on the jump site.
3101 unsigned PPCTargetLowering::getJumpTableEncoding() const {
3102   if (isJumpTableRelative())
3103     return MachineJumpTableInfo::EK_LabelDifference32;
3104 
3105   return TargetLowering::getJumpTableEncoding();
3106 }
3107 
3108 bool PPCTargetLowering::isJumpTableRelative() const {
3109   if (UseAbsoluteJumpTables)
3110     return false;
3111   if (Subtarget.isPPC64() || Subtarget.isAIXABI())
3112     return true;
3113   return TargetLowering::isJumpTableRelative();
3114 }
3115 
3116 SDValue PPCTargetLowering::getPICJumpTableRelocBase(SDValue Table,
3117                                                     SelectionDAG &DAG) const {
3118   if (!Subtarget.isPPC64() || Subtarget.isAIXABI())
3119     return TargetLowering::getPICJumpTableRelocBase(Table, DAG);
3120 
3121   switch (getTargetMachine().getCodeModel()) {
3122   case CodeModel::Small:
3123   case CodeModel::Medium:
3124     return TargetLowering::getPICJumpTableRelocBase(Table, DAG);
3125   default:
3126     return DAG.getNode(PPCISD::GlobalBaseReg, SDLoc(),
3127                        getPointerTy(DAG.getDataLayout()));
3128   }
3129 }
3130 
3131 const MCExpr *
3132 PPCTargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
3133                                                 unsigned JTI,
3134                                                 MCContext &Ctx) const {
3135   if (!Subtarget.isPPC64() || Subtarget.isAIXABI())
3136     return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
3137 
3138   switch (getTargetMachine().getCodeModel()) {
3139   case CodeModel::Small:
3140   case CodeModel::Medium:
3141     return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
3142   default:
3143     return MCSymbolRefExpr::create(MF->getPICBaseSymbol(), Ctx);
3144   }
3145 }
3146 
3147 SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
3148   EVT PtrVT = Op.getValueType();
3149   JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3150 
3151   // isUsingPCRelativeCalls() returns true when PCRelative is enabled
3152   if (Subtarget.isUsingPCRelativeCalls()) {
3153     SDLoc DL(JT);
3154     EVT Ty = getPointerTy(DAG.getDataLayout());
3155     SDValue GA =
3156         DAG.getTargetJumpTable(JT->getIndex(), Ty, PPCII::MO_PCREL_FLAG);
3157     SDValue MatAddr = DAG.getNode(PPCISD::MAT_PCREL_ADDR, DL, Ty, GA);
3158     return MatAddr;
3159   }
3160 
3161   // 64-bit SVR4 ABI and AIX ABI code are always position-independent.
3162   // The actual address of the GlobalValue is stored in the TOC.
3163   if (Subtarget.is64BitELFABI() || Subtarget.isAIXABI()) {
3164     setUsesTOCBasePtr(DAG);
3165     SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
3166     return getTOCEntry(DAG, SDLoc(JT), GA);
3167   }
3168 
3169   unsigned MOHiFlag, MOLoFlag;
3170   bool IsPIC = isPositionIndependent();
3171   getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag);
3172 
3173   if (IsPIC && Subtarget.isSVR4ABI()) {
3174     SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
3175                                         PPCII::MO_PIC_FLAG);
3176     return getTOCEntry(DAG, SDLoc(GA), GA);
3177   }
3178 
3179   SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
3180   SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
3181   return LowerLabelRef(JTIHi, JTILo, IsPIC, DAG);
3182 }
3183 
3184 SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
3185                                              SelectionDAG &DAG) const {
3186   EVT PtrVT = Op.getValueType();
3187   BlockAddressSDNode *BASDN = cast<BlockAddressSDNode>(Op);
3188   const BlockAddress *BA = BASDN->getBlockAddress();
3189 
3190   // isUsingPCRelativeCalls() returns true when PCRelative is enabled
3191   if (Subtarget.isUsingPCRelativeCalls()) {
3192     SDLoc DL(BASDN);
3193     EVT Ty = getPointerTy(DAG.getDataLayout());
3194     SDValue GA = DAG.getTargetBlockAddress(BA, Ty, BASDN->getOffset(),
3195                                            PPCII::MO_PCREL_FLAG);
3196     SDValue MatAddr = DAG.getNode(PPCISD::MAT_PCREL_ADDR, DL, Ty, GA);
3197     return MatAddr;
3198   }
3199 
3200   // 64-bit SVR4 ABI and AIX ABI code are always position-independent.
3201   // The actual BlockAddress is stored in the TOC.
3202   if (Subtarget.is64BitELFABI() || Subtarget.isAIXABI()) {
3203     setUsesTOCBasePtr(DAG);
3204     SDValue GA = DAG.getTargetBlockAddress(BA, PtrVT, BASDN->getOffset());
3205     return getTOCEntry(DAG, SDLoc(BASDN), GA);
3206   }
3207 
3208   // 32-bit position-independent ELF stores the BlockAddress in the .got.
3209   if (Subtarget.is32BitELFABI() && isPositionIndependent())
3210     return getTOCEntry(
3211         DAG, SDLoc(BASDN),
3212         DAG.getTargetBlockAddress(BA, PtrVT, BASDN->getOffset()));
3213 
3214   unsigned MOHiFlag, MOLoFlag;
3215   bool IsPIC = isPositionIndependent();
3216   getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag);
3217   SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
3218   SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
3219   return LowerLabelRef(TgtBAHi, TgtBALo, IsPIC, DAG);
3220 }
3221 
3222 SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
3223                                               SelectionDAG &DAG) const {
3224   if (Subtarget.isAIXABI())
3225     return LowerGlobalTLSAddressAIX(Op, DAG);
3226 
3227   return LowerGlobalTLSAddressLinux(Op, DAG);
3228 }
3229 
3230 SDValue PPCTargetLowering::LowerGlobalTLSAddressAIX(SDValue Op,
3231                                                     SelectionDAG &DAG) const {
3232   GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3233 
3234   if (DAG.getTarget().useEmulatedTLS())
3235     report_fatal_error("Emulated TLS is not yet supported on AIX");
3236 
3237   SDLoc dl(GA);
3238   const GlobalValue *GV = GA->getGlobal();
3239   EVT PtrVT = getPointerTy(DAG.getDataLayout());
3240 
3241   // The general-dynamic model is the only access model supported for now, so
3242   // all the GlobalTLSAddress nodes are lowered with this model.
3243   // We need to generate two TOC entries, one for the variable offset, one for
3244   // the region handle. The global address for the TOC entry of the region
3245   // handle is created with the MO_TLSGDM_FLAG flag and the global address
3246   // for the TOC entry of the variable offset is created with MO_TLSGD_FLAG.
3247   SDValue VariableOffsetTGA =
3248       DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, PPCII::MO_TLSGD_FLAG);
3249   SDValue RegionHandleTGA =
3250       DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, PPCII::MO_TLSGDM_FLAG);
3251   SDValue VariableOffset = getTOCEntry(DAG, dl, VariableOffsetTGA);
3252   SDValue RegionHandle = getTOCEntry(DAG, dl, RegionHandleTGA);
3253   return DAG.getNode(PPCISD::TLSGD_AIX, dl, PtrVT, VariableOffset,
3254                      RegionHandle);
3255 }
3256 
3257 SDValue PPCTargetLowering::LowerGlobalTLSAddressLinux(SDValue Op,
3258                                                       SelectionDAG &DAG) const {
3259   // FIXME: TLS addresses currently use medium model code sequences,
3260   // which is the most useful form.  Eventually support for small and
3261   // large models could be added if users need it, at the cost of
3262   // additional complexity.
3263   GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3264   if (DAG.getTarget().useEmulatedTLS())
3265     return LowerToTLSEmulatedModel(GA, DAG);
3266 
3267   SDLoc dl(GA);
3268   const GlobalValue *GV = GA->getGlobal();
3269   EVT PtrVT = getPointerTy(DAG.getDataLayout());
3270   bool is64bit = Subtarget.isPPC64();
3271   const Module *M = DAG.getMachineFunction().getFunction().getParent();
3272   PICLevel::Level picLevel = M->getPICLevel();
3273 
3274   const TargetMachine &TM = getTargetMachine();
3275   TLSModel::Model Model = TM.getTLSModel(GV);
3276 
3277   if (Model == TLSModel::LocalExec) {
3278     if (Subtarget.isUsingPCRelativeCalls()) {
3279       SDValue TLSReg = DAG.getRegister(PPC::X13, MVT::i64);
3280       SDValue TGA = DAG.getTargetGlobalAddress(
3281           GV, dl, PtrVT, 0, (PPCII::MO_PCREL_FLAG | PPCII::MO_TPREL_FLAG));
3282       SDValue MatAddr =
3283           DAG.getNode(PPCISD::TLS_LOCAL_EXEC_MAT_ADDR, dl, PtrVT, TGA);
3284       return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TLSReg, MatAddr);
3285     }
3286 
3287     SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
3288                                                PPCII::MO_TPREL_HA);
3289     SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
3290                                                PPCII::MO_TPREL_LO);
3291     SDValue TLSReg = is64bit ? DAG.getRegister(PPC::X13, MVT::i64)
3292                              : DAG.getRegister(PPC::R2, MVT::i32);
3293 
3294     SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
3295     return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
3296   }
3297 
3298   if (Model == TLSModel::InitialExec) {
3299     bool IsPCRel = Subtarget.isUsingPCRelativeCalls();
3300     SDValue TGA = DAG.getTargetGlobalAddress(
3301         GV, dl, PtrVT, 0, IsPCRel ? PPCII::MO_GOT_TPREL_PCREL_FLAG : 0);
3302     SDValue TGATLS = DAG.getTargetGlobalAddress(
3303         GV, dl, PtrVT, 0,
3304         IsPCRel ? (PPCII::MO_TLS | PPCII::MO_PCREL_FLAG) : PPCII::MO_TLS);
3305     SDValue TPOffset;
3306     if (IsPCRel) {
3307       SDValue MatPCRel = DAG.getNode(PPCISD::MAT_PCREL_ADDR, dl, PtrVT, TGA);
3308       TPOffset = DAG.getLoad(MVT::i64, dl, DAG.getEntryNode(), MatPCRel,
3309                              MachinePointerInfo());
3310     } else {
3311       SDValue GOTPtr;
3312       if (is64bit) {
3313         setUsesTOCBasePtr(DAG);
3314         SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
3315         GOTPtr =
3316             DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl, PtrVT, GOTReg, TGA);
3317       } else {
3318         if (!TM.isPositionIndependent())
3319           GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT);
3320         else if (picLevel == PICLevel::SmallPIC)
3321           GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT);
3322         else
3323           GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
3324       }
3325       TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl, PtrVT, TGA, GOTPtr);
3326     }
3327     return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
3328   }
3329 
3330   if (Model == TLSModel::GeneralDynamic) {
3331     if (Subtarget.isUsingPCRelativeCalls()) {
3332       SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
3333                                                PPCII::MO_GOT_TLSGD_PCREL_FLAG);
3334       return DAG.getNode(PPCISD::TLS_DYNAMIC_MAT_PCREL_ADDR, dl, PtrVT, TGA);
3335     }
3336 
3337     SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
3338     SDValue GOTPtr;
3339     if (is64bit) {
3340       setUsesTOCBasePtr(DAG);
3341       SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
3342       GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
3343                                    GOTReg, TGA);
3344     } else {
3345       if (picLevel == PICLevel::SmallPIC)
3346         GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT);
3347       else
3348         GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
3349     }
3350     return DAG.getNode(PPCISD::ADDI_TLSGD_L_ADDR, dl, PtrVT,
3351                        GOTPtr, TGA, TGA);
3352   }
3353 
3354   if (Model == TLSModel::LocalDynamic) {
3355     if (Subtarget.isUsingPCRelativeCalls()) {
3356       SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
3357                                                PPCII::MO_GOT_TLSLD_PCREL_FLAG);
3358       SDValue MatPCRel =
3359           DAG.getNode(PPCISD::TLS_DYNAMIC_MAT_PCREL_ADDR, dl, PtrVT, TGA);
3360       return DAG.getNode(PPCISD::PADDI_DTPREL, dl, PtrVT, MatPCRel, TGA);
3361     }
3362 
3363     SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
3364     SDValue GOTPtr;
3365     if (is64bit) {
3366       setUsesTOCBasePtr(DAG);
3367       SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
3368       GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
3369                            GOTReg, TGA);
3370     } else {
3371       if (picLevel == PICLevel::SmallPIC)
3372         GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT);
3373       else
3374         GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
3375     }
3376     SDValue TLSAddr = DAG.getNode(PPCISD::ADDI_TLSLD_L_ADDR, dl,
3377                                   PtrVT, GOTPtr, TGA, TGA);
3378     SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl,
3379                                       PtrVT, TLSAddr, TGA);
3380     return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
3381   }
3382 
3383   llvm_unreachable("Unknown TLS model!");
3384 }
3385 
3386 SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
3387                                               SelectionDAG &DAG) const {
3388   EVT PtrVT = Op.getValueType();
3389   GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
3390   SDLoc DL(GSDN);
3391   const GlobalValue *GV = GSDN->getGlobal();
3392 
3393   // 64-bit SVR4 ABI & AIX ABI code is always position-independent.
3394   // The actual address of the GlobalValue is stored in the TOC.
3395   if (Subtarget.is64BitELFABI() || Subtarget.isAIXABI()) {
3396     if (Subtarget.isUsingPCRelativeCalls()) {
3397       EVT Ty = getPointerTy(DAG.getDataLayout());
3398       if (isAccessedAsGotIndirect(Op)) {
3399         SDValue GA = DAG.getTargetGlobalAddress(GV, DL, Ty, GSDN->getOffset(),
3400                                                 PPCII::MO_PCREL_FLAG |
3401                                                     PPCII::MO_GOT_FLAG);
3402         SDValue MatPCRel = DAG.getNode(PPCISD::MAT_PCREL_ADDR, DL, Ty, GA);
3403         SDValue Load = DAG.getLoad(MVT::i64, DL, DAG.getEntryNode(), MatPCRel,
3404                                    MachinePointerInfo());
3405         return Load;
3406       } else {
3407         SDValue GA = DAG.getTargetGlobalAddress(GV, DL, Ty, GSDN->getOffset(),
3408                                                 PPCII::MO_PCREL_FLAG);
3409         return DAG.getNode(PPCISD::MAT_PCREL_ADDR, DL, Ty, GA);
3410       }
3411     }
3412     setUsesTOCBasePtr(DAG);
3413     SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
3414     return getTOCEntry(DAG, DL, GA);
3415   }
3416 
3417   unsigned MOHiFlag, MOLoFlag;
3418   bool IsPIC = isPositionIndependent();
3419   getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag, GV);
3420 
3421   if (IsPIC && Subtarget.isSVR4ABI()) {
3422     SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT,
3423                                             GSDN->getOffset(),
3424                                             PPCII::MO_PIC_FLAG);
3425     return getTOCEntry(DAG, DL, GA);
3426   }
3427 
3428   SDValue GAHi =
3429     DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
3430   SDValue GALo =
3431     DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
3432 
3433   return LowerLabelRef(GAHi, GALo, IsPIC, DAG);
3434 }
3435 
3436 SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
3437   bool IsStrict = Op->isStrictFPOpcode();
3438   ISD::CondCode CC =
3439       cast<CondCodeSDNode>(Op.getOperand(IsStrict ? 3 : 2))->get();
3440   SDValue LHS = Op.getOperand(IsStrict ? 1 : 0);
3441   SDValue RHS = Op.getOperand(IsStrict ? 2 : 1);
3442   SDValue Chain = IsStrict ? Op.getOperand(0) : SDValue();
3443   EVT LHSVT = LHS.getValueType();
3444   SDLoc dl(Op);
3445 
3446   // Soften the setcc with libcall if it is fp128.
3447   if (LHSVT == MVT::f128) {
3448     assert(!Subtarget.hasP9Vector() &&
3449            "SETCC for f128 is already legal under Power9!");
3450     softenSetCCOperands(DAG, LHSVT, LHS, RHS, CC, dl, LHS, RHS, Chain,
3451                         Op->getOpcode() == ISD::STRICT_FSETCCS);
3452     if (RHS.getNode())
3453       LHS = DAG.getNode(ISD::SETCC, dl, Op.getValueType(), LHS, RHS,
3454                         DAG.getCondCode(CC));
3455     if (IsStrict)
3456       return DAG.getMergeValues({LHS, Chain}, dl);
3457     return LHS;
3458   }
3459 
3460   assert(!IsStrict && "Don't know how to handle STRICT_FSETCC!");
3461 
3462   if (Op.getValueType() == MVT::v2i64) {
3463     // When the operands themselves are v2i64 values, we need to do something
3464     // special because VSX has no underlying comparison operations for these.
3465     if (LHS.getValueType() == MVT::v2i64) {
3466       // Equality can be handled by casting to the legal type for Altivec
3467       // comparisons, everything else needs to be expanded.
3468       if (CC == ISD::SETEQ || CC == ISD::SETNE) {
3469         return DAG.getNode(
3470             ISD::BITCAST, dl, MVT::v2i64,
3471             DAG.getSetCC(dl, MVT::v4i32,
3472                          DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, LHS),
3473                          DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, RHS), CC));
3474       }
3475 
3476       return SDValue();
3477     }
3478 
3479     // We handle most of these in the usual way.
3480     return Op;
3481   }
3482 
3483   // If we're comparing for equality to zero, expose the fact that this is
3484   // implemented as a ctlz/srl pair on ppc, so that the dag combiner can
3485   // fold the new nodes.
3486   if (SDValue V = lowerCmpEqZeroToCtlzSrl(Op, DAG))
3487     return V;
3488 
3489   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) {
3490     // Leave comparisons against 0 and -1 alone for now, since they're usually
3491     // optimized.  FIXME: revisit this when we can custom lower all setcc
3492     // optimizations.
3493     if (C->isAllOnesValue() || C->isNullValue())
3494       return SDValue();
3495   }
3496 
3497   // If we have an integer seteq/setne, turn it into a compare against zero
3498   // by xor'ing the rhs with the lhs, which is faster than setting a
3499   // condition register, reading it back out, and masking the correct bit.  The
3500   // normal approach here uses sub to do this instead of xor.  Using xor exposes
3501   // the result to other bit-twiddling opportunities.
3502   if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
3503     EVT VT = Op.getValueType();
3504     SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, LHS, RHS);
3505     return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, dl, LHSVT), CC);
3506   }
3507   return SDValue();
3508 }
3509 
3510 SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
3511   SDNode *Node = Op.getNode();
3512   EVT VT = Node->getValueType(0);
3513   EVT PtrVT = getPointerTy(DAG.getDataLayout());
3514   SDValue InChain = Node->getOperand(0);
3515   SDValue VAListPtr = Node->getOperand(1);
3516   const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
3517   SDLoc dl(Node);
3518 
3519   assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
3520 
3521   // gpr_index
3522   SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
3523                                     VAListPtr, MachinePointerInfo(SV), MVT::i8);
3524   InChain = GprIndex.getValue(1);
3525 
3526   if (VT == MVT::i64) {
3527     // Check if GprIndex is even
3528     SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
3529                                  DAG.getConstant(1, dl, MVT::i32));
3530     SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
3531                                 DAG.getConstant(0, dl, MVT::i32), ISD::SETNE);
3532     SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
3533                                           DAG.getConstant(1, dl, MVT::i32));
3534     // Align GprIndex to be even if it isn't
3535     GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
3536                            GprIndex);
3537   }
3538 
3539   // fpr index is 1 byte after gpr
3540   SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
3541                                DAG.getConstant(1, dl, MVT::i32));
3542 
3543   // fpr
3544   SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
3545                                     FprPtr, MachinePointerInfo(SV), MVT::i8);
3546   InChain = FprIndex.getValue(1);
3547 
3548   SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
3549                                        DAG.getConstant(8, dl, MVT::i32));
3550 
3551   SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
3552                                         DAG.getConstant(4, dl, MVT::i32));
3553 
3554   // areas
3555   SDValue OverflowArea =
3556       DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr, MachinePointerInfo());
3557   InChain = OverflowArea.getValue(1);
3558 
3559   SDValue RegSaveArea =
3560       DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr, MachinePointerInfo());
3561   InChain = RegSaveArea.getValue(1);
3562 
3563   // select overflow_area if index > 8
3564   SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
3565                             DAG.getConstant(8, dl, MVT::i32), ISD::SETLT);
3566 
3567   // adjustment constant gpr_index * 4/8
3568   SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
3569                                     VT.isInteger() ? GprIndex : FprIndex,
3570                                     DAG.getConstant(VT.isInteger() ? 4 : 8, dl,
3571                                                     MVT::i32));
3572 
3573   // OurReg = RegSaveArea + RegConstant
3574   SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
3575                                RegConstant);
3576 
3577   // Floating types are 32 bytes into RegSaveArea
3578   if (VT.isFloatingPoint())
3579     OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
3580                          DAG.getConstant(32, dl, MVT::i32));
3581 
3582   // increase {f,g}pr_index by 1 (or 2 if VT is i64)
3583   SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
3584                                    VT.isInteger() ? GprIndex : FprIndex,
3585                                    DAG.getConstant(VT == MVT::i64 ? 2 : 1, dl,
3586                                                    MVT::i32));
3587 
3588   InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
3589                               VT.isInteger() ? VAListPtr : FprPtr,
3590                               MachinePointerInfo(SV), MVT::i8);
3591 
3592   // determine if we should load from reg_save_area or overflow_area
3593   SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
3594 
3595   // increase overflow_area by 4/8 if gpr/fpr > 8
3596   SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
3597                                           DAG.getConstant(VT.isInteger() ? 4 : 8,
3598                                           dl, MVT::i32));
3599 
3600   OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
3601                              OverflowAreaPlusN);
3602 
3603   InChain = DAG.getTruncStore(InChain, dl, OverflowArea, OverflowAreaPtr,
3604                               MachinePointerInfo(), MVT::i32);
3605 
3606   return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo());
3607 }
3608 
3609 SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
3610   assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
3611 
3612   // We have to copy the entire va_list struct:
3613   // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
3614   return DAG.getMemcpy(Op.getOperand(0), Op, Op.getOperand(1), Op.getOperand(2),
3615                        DAG.getConstant(12, SDLoc(Op), MVT::i32), Align(8),
3616                        false, true, false, MachinePointerInfo(),
3617                        MachinePointerInfo());
3618 }
3619 
3620 SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
3621                                                   SelectionDAG &DAG) const {
3622   if (Subtarget.isAIXABI())
3623     report_fatal_error("ADJUST_TRAMPOLINE operation is not supported on AIX.");
3624 
3625   return Op.getOperand(0);
3626 }
3627 
3628 SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
3629                                                 SelectionDAG &DAG) const {
3630   if (Subtarget.isAIXABI())
3631     report_fatal_error("INIT_TRAMPOLINE operation is not supported on AIX.");
3632 
3633   SDValue Chain = Op.getOperand(0);
3634   SDValue Trmp = Op.getOperand(1); // trampoline
3635   SDValue FPtr = Op.getOperand(2); // nested function
3636   SDValue Nest = Op.getOperand(3); // 'nest' parameter value
3637   SDLoc dl(Op);
3638 
3639   EVT PtrVT = getPointerTy(DAG.getDataLayout());
3640   bool isPPC64 = (PtrVT == MVT::i64);
3641   Type *IntPtrTy = DAG.getDataLayout().getIntPtrType(*DAG.getContext());
3642 
3643   TargetLowering::ArgListTy Args;
3644   TargetLowering::ArgListEntry Entry;
3645 
3646   Entry.Ty = IntPtrTy;
3647   Entry.Node = Trmp; Args.push_back(Entry);
3648 
3649   // TrampSize == (isPPC64 ? 48 : 40);
3650   Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40, dl,
3651                                isPPC64 ? MVT::i64 : MVT::i32);
3652   Args.push_back(Entry);
3653 
3654   Entry.Node = FPtr; Args.push_back(Entry);
3655   Entry.Node = Nest; Args.push_back(Entry);
3656 
3657   // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
3658   TargetLowering::CallLoweringInfo CLI(DAG);
3659   CLI.setDebugLoc(dl).setChain(Chain).setLibCallee(
3660       CallingConv::C, Type::getVoidTy(*DAG.getContext()),
3661       DAG.getExternalSymbol("__trampoline_setup", PtrVT), std::move(Args));
3662 
3663   std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
3664   return CallResult.second;
3665 }
3666 
3667 SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
3668   MachineFunction &MF = DAG.getMachineFunction();
3669   PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
3670   EVT PtrVT = getPointerTy(MF.getDataLayout());
3671 
3672   SDLoc dl(Op);
3673 
3674   if (Subtarget.isPPC64() || Subtarget.isAIXABI()) {
3675     // vastart just stores the address of the VarArgsFrameIndex slot into the
3676     // memory location argument.
3677     SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
3678     const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
3679     return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
3680                         MachinePointerInfo(SV));
3681   }
3682 
3683   // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
3684   // We suppose the given va_list is already allocated.
3685   //
3686   // typedef struct {
3687   //  char gpr;     /* index into the array of 8 GPRs
3688   //                 * stored in the register save area
3689   //                 * gpr=0 corresponds to r3,
3690   //                 * gpr=1 to r4, etc.
3691   //                 */
3692   //  char fpr;     /* index into the array of 8 FPRs
3693   //                 * stored in the register save area
3694   //                 * fpr=0 corresponds to f1,
3695   //                 * fpr=1 to f2, etc.
3696   //                 */
3697   //  char *overflow_arg_area;
3698   //                /* location on stack that holds
3699   //                 * the next overflow argument
3700   //                 */
3701   //  char *reg_save_area;
3702   //               /* where r3:r10 and f1:f8 (if saved)
3703   //                * are stored
3704   //                */
3705   // } va_list[1];
3706 
3707   SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), dl, MVT::i32);
3708   SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), dl, MVT::i32);
3709   SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
3710                                             PtrVT);
3711   SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
3712                                  PtrVT);
3713 
3714   uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
3715   SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, dl, PtrVT);
3716 
3717   uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
3718   SDValue ConstStackOffset = DAG.getConstant(StackOffset, dl, PtrVT);
3719 
3720   uint64_t FPROffset = 1;
3721   SDValue ConstFPROffset = DAG.getConstant(FPROffset, dl, PtrVT);
3722 
3723   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
3724 
3725   // Store first byte : number of int regs
3726   SDValue firstStore =
3727       DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR, Op.getOperand(1),
3728                         MachinePointerInfo(SV), MVT::i8);
3729   uint64_t nextOffset = FPROffset;
3730   SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
3731                                   ConstFPROffset);
3732 
3733   // Store second byte : number of float regs
3734   SDValue secondStore =
3735       DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
3736                         MachinePointerInfo(SV, nextOffset), MVT::i8);
3737   nextOffset += StackOffset;
3738   nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
3739 
3740   // Store second word : arguments given on stack
3741   SDValue thirdStore = DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
3742                                     MachinePointerInfo(SV, nextOffset));
3743   nextOffset += FrameOffset;
3744   nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
3745 
3746   // Store third word : arguments given in registers
3747   return DAG.getStore(thirdStore, dl, FR, nextPtr,
3748                       MachinePointerInfo(SV, nextOffset));
3749 }
3750 
3751 /// FPR - The set of FP registers that should be allocated for arguments
3752 /// on Darwin and AIX.
3753 static const MCPhysReg FPR[] = {PPC::F1,  PPC::F2,  PPC::F3, PPC::F4, PPC::F5,
3754                                 PPC::F6,  PPC::F7,  PPC::F8, PPC::F9, PPC::F10,
3755                                 PPC::F11, PPC::F12, PPC::F13};
3756 
3757 /// CalculateStackSlotSize - Calculates the size reserved for this argument on
3758 /// the stack.
3759 static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
3760                                        unsigned PtrByteSize) {
3761   unsigned ArgSize = ArgVT.getStoreSize();
3762   if (Flags.isByVal())
3763     ArgSize = Flags.getByValSize();
3764 
3765   // Round up to multiples of the pointer size, except for array members,
3766   // which are always packed.
3767   if (!Flags.isInConsecutiveRegs())
3768     ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
3769 
3770   return ArgSize;
3771 }
3772 
3773 /// CalculateStackSlotAlignment - Calculates the alignment of this argument
3774 /// on the stack.
3775 static Align CalculateStackSlotAlignment(EVT ArgVT, EVT OrigVT,
3776                                          ISD::ArgFlagsTy Flags,
3777                                          unsigned PtrByteSize) {
3778   Align Alignment(PtrByteSize);
3779 
3780   // Altivec parameters are padded to a 16 byte boundary.
3781   if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
3782       ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
3783       ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64 ||
3784       ArgVT == MVT::v1i128 || ArgVT == MVT::f128)
3785     Alignment = Align(16);
3786 
3787   // ByVal parameters are aligned as requested.
3788   if (Flags.isByVal()) {
3789     auto BVAlign = Flags.getNonZeroByValAlign();
3790     if (BVAlign > PtrByteSize) {
3791       if (BVAlign.value() % PtrByteSize != 0)
3792         llvm_unreachable(
3793             "ByVal alignment is not a multiple of the pointer size");
3794 
3795       Alignment = BVAlign;
3796     }
3797   }
3798 
3799   // Array members are always packed to their original alignment.
3800   if (Flags.isInConsecutiveRegs()) {
3801     // If the array member was split into multiple registers, the first
3802     // needs to be aligned to the size of the full type.  (Except for
3803     // ppcf128, which is only aligned as its f64 components.)
3804     if (Flags.isSplit() && OrigVT != MVT::ppcf128)
3805       Alignment = Align(OrigVT.getStoreSize());
3806     else
3807       Alignment = Align(ArgVT.getStoreSize());
3808   }
3809 
3810   return Alignment;
3811 }
3812 
3813 /// CalculateStackSlotUsed - Return whether this argument will use its
3814 /// stack slot (instead of being passed in registers).  ArgOffset,
3815 /// AvailableFPRs, and AvailableVRs must hold the current argument
3816 /// position, and will be updated to account for this argument.
3817 static bool CalculateStackSlotUsed(EVT ArgVT, EVT OrigVT, ISD::ArgFlagsTy Flags,
3818                                    unsigned PtrByteSize, unsigned LinkageSize,
3819                                    unsigned ParamAreaSize, unsigned &ArgOffset,
3820                                    unsigned &AvailableFPRs,
3821                                    unsigned &AvailableVRs) {
3822   bool UseMemory = false;
3823 
3824   // Respect alignment of argument on the stack.
3825   Align Alignment =
3826       CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
3827   ArgOffset = alignTo(ArgOffset, Alignment);
3828   // If there's no space left in the argument save area, we must
3829   // use memory (this check also catches zero-sized arguments).
3830   if (ArgOffset >= LinkageSize + ParamAreaSize)
3831     UseMemory = true;
3832 
3833   // Allocate argument on the stack.
3834   ArgOffset += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
3835   if (Flags.isInConsecutiveRegsLast())
3836     ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
3837   // If we overran the argument save area, we must use memory
3838   // (this check catches arguments passed partially in memory)
3839   if (ArgOffset > LinkageSize + ParamAreaSize)
3840     UseMemory = true;
3841 
3842   // However, if the argument is actually passed in an FPR or a VR,
3843   // we don't use memory after all.
3844   if (!Flags.isByVal()) {
3845     if (ArgVT == MVT::f32 || ArgVT == MVT::f64)
3846       if (AvailableFPRs > 0) {
3847         --AvailableFPRs;
3848         return false;
3849       }
3850     if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
3851         ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
3852         ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64 ||
3853         ArgVT == MVT::v1i128 || ArgVT == MVT::f128)
3854       if (AvailableVRs > 0) {
3855         --AvailableVRs;
3856         return false;
3857       }
3858   }
3859 
3860   return UseMemory;
3861 }
3862 
3863 /// EnsureStackAlignment - Round stack frame size up from NumBytes to
3864 /// ensure minimum alignment required for target.
3865 static unsigned EnsureStackAlignment(const PPCFrameLowering *Lowering,
3866                                      unsigned NumBytes) {
3867   return alignTo(NumBytes, Lowering->getStackAlign());
3868 }
3869 
3870 SDValue PPCTargetLowering::LowerFormalArguments(
3871     SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
3872     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
3873     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
3874   if (Subtarget.isAIXABI())
3875     return LowerFormalArguments_AIX(Chain, CallConv, isVarArg, Ins, dl, DAG,
3876                                     InVals);
3877   if (Subtarget.is64BitELFABI())
3878     return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins, dl, DAG,
3879                                        InVals);
3880   assert(Subtarget.is32BitELFABI());
3881   return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins, dl, DAG,
3882                                      InVals);
3883 }
3884 
3885 SDValue PPCTargetLowering::LowerFormalArguments_32SVR4(
3886     SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
3887     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
3888     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
3889 
3890   // 32-bit SVR4 ABI Stack Frame Layout:
3891   //              +-----------------------------------+
3892   //        +-->  |            Back chain             |
3893   //        |     +-----------------------------------+
3894   //        |     | Floating-point register save area |
3895   //        |     +-----------------------------------+
3896   //        |     |    General register save area     |
3897   //        |     +-----------------------------------+
3898   //        |     |          CR save word             |
3899   //        |     +-----------------------------------+
3900   //        |     |         VRSAVE save word          |
3901   //        |     +-----------------------------------+
3902   //        |     |         Alignment padding         |
3903   //        |     +-----------------------------------+
3904   //        |     |     Vector register save area     |
3905   //        |     +-----------------------------------+
3906   //        |     |       Local variable space        |
3907   //        |     +-----------------------------------+
3908   //        |     |        Parameter list area        |
3909   //        |     +-----------------------------------+
3910   //        |     |           LR save word            |
3911   //        |     +-----------------------------------+
3912   // SP-->  +---  |            Back chain             |
3913   //              +-----------------------------------+
3914   //
3915   // Specifications:
3916   //   System V Application Binary Interface PowerPC Processor Supplement
3917   //   AltiVec Technology Programming Interface Manual
3918 
3919   MachineFunction &MF = DAG.getMachineFunction();
3920   MachineFrameInfo &MFI = MF.getFrameInfo();
3921   PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
3922 
3923   EVT PtrVT = getPointerTy(MF.getDataLayout());
3924   // Potential tail calls could cause overwriting of argument stack slots.
3925   bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
3926                        (CallConv == CallingConv::Fast));
3927   const Align PtrAlign(4);
3928 
3929   // Assign locations to all of the incoming arguments.
3930   SmallVector<CCValAssign, 16> ArgLocs;
3931   PPCCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3932                  *DAG.getContext());
3933 
3934   // Reserve space for the linkage area on the stack.
3935   unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
3936   CCInfo.AllocateStack(LinkageSize, PtrAlign);
3937   if (useSoftFloat())
3938     CCInfo.PreAnalyzeFormalArguments(Ins);
3939 
3940   CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
3941   CCInfo.clearWasPPCF128();
3942 
3943   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3944     CCValAssign &VA = ArgLocs[i];
3945 
3946     // Arguments stored in registers.
3947     if (VA.isRegLoc()) {
3948       const TargetRegisterClass *RC;
3949       EVT ValVT = VA.getValVT();
3950 
3951       switch (ValVT.getSimpleVT().SimpleTy) {
3952         default:
3953           llvm_unreachable("ValVT not supported by formal arguments Lowering");
3954         case MVT::i1:
3955         case MVT::i32:
3956           RC = &PPC::GPRCRegClass;
3957           break;
3958         case MVT::f32:
3959           if (Subtarget.hasP8Vector())
3960             RC = &PPC::VSSRCRegClass;
3961           else if (Subtarget.hasSPE())
3962             RC = &PPC::GPRCRegClass;
3963           else
3964             RC = &PPC::F4RCRegClass;
3965           break;
3966         case MVT::f64:
3967           if (Subtarget.hasVSX())
3968             RC = &PPC::VSFRCRegClass;
3969           else if (Subtarget.hasSPE())
3970             // SPE passes doubles in GPR pairs.
3971             RC = &PPC::GPRCRegClass;
3972           else
3973             RC = &PPC::F8RCRegClass;
3974           break;
3975         case MVT::v16i8:
3976         case MVT::v8i16:
3977         case MVT::v4i32:
3978           RC = &PPC::VRRCRegClass;
3979           break;
3980         case MVT::v4f32:
3981           RC = &PPC::VRRCRegClass;
3982           break;
3983         case MVT::v2f64:
3984         case MVT::v2i64:
3985           RC = &PPC::VRRCRegClass;
3986           break;
3987       }
3988 
3989       SDValue ArgValue;
3990       // Transform the arguments stored in physical registers into
3991       // virtual ones.
3992       if (VA.getLocVT() == MVT::f64 && Subtarget.hasSPE()) {
3993         assert(i + 1 < e && "No second half of double precision argument");
3994         unsigned RegLo = MF.addLiveIn(VA.getLocReg(), RC);
3995         unsigned RegHi = MF.addLiveIn(ArgLocs[++i].getLocReg(), RC);
3996         SDValue ArgValueLo = DAG.getCopyFromReg(Chain, dl, RegLo, MVT::i32);
3997         SDValue ArgValueHi = DAG.getCopyFromReg(Chain, dl, RegHi, MVT::i32);
3998         if (!Subtarget.isLittleEndian())
3999           std::swap (ArgValueLo, ArgValueHi);
4000         ArgValue = DAG.getNode(PPCISD::BUILD_SPE64, dl, MVT::f64, ArgValueLo,
4001                                ArgValueHi);
4002       } else {
4003         unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
4004         ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
4005                                       ValVT == MVT::i1 ? MVT::i32 : ValVT);
4006         if (ValVT == MVT::i1)
4007           ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue);
4008       }
4009 
4010       InVals.push_back(ArgValue);
4011     } else {
4012       // Argument stored in memory.
4013       assert(VA.isMemLoc());
4014 
4015       // Get the extended size of the argument type in stack
4016       unsigned ArgSize = VA.getLocVT().getStoreSize();
4017       // Get the actual size of the argument type
4018       unsigned ObjSize = VA.getValVT().getStoreSize();
4019       unsigned ArgOffset = VA.getLocMemOffset();
4020       // Stack objects in PPC32 are right justified.
4021       ArgOffset += ArgSize - ObjSize;
4022       int FI = MFI.CreateFixedObject(ArgSize, ArgOffset, isImmutable);
4023 
4024       // Create load nodes to retrieve arguments from the stack.
4025       SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
4026       InVals.push_back(
4027           DAG.getLoad(VA.getValVT(), dl, Chain, FIN, MachinePointerInfo()));
4028     }
4029   }
4030 
4031   // Assign locations to all of the incoming aggregate by value arguments.
4032   // Aggregates passed by value are stored in the local variable space of the
4033   // caller's stack frame, right above the parameter list area.
4034   SmallVector<CCValAssign, 16> ByValArgLocs;
4035   CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
4036                       ByValArgLocs, *DAG.getContext());
4037 
4038   // Reserve stack space for the allocations in CCInfo.
4039   CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrAlign);
4040 
4041   CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
4042 
4043   // Area that is at least reserved in the caller of this function.
4044   unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
4045   MinReservedArea = std::max(MinReservedArea, LinkageSize);
4046 
4047   // Set the size that is at least reserved in caller of this function.  Tail
4048   // call optimized function's reserved stack space needs to be aligned so that
4049   // taking the difference between two stack areas will result in an aligned
4050   // stack.
4051   MinReservedArea =
4052       EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea);
4053   FuncInfo->setMinReservedArea(MinReservedArea);
4054 
4055   SmallVector<SDValue, 8> MemOps;
4056 
4057   // If the function takes variable number of arguments, make a frame index for
4058   // the start of the first vararg value... for expansion of llvm.va_start.
4059   if (isVarArg) {
4060     static const MCPhysReg GPArgRegs[] = {
4061       PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4062       PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4063     };
4064     const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
4065 
4066     static const MCPhysReg FPArgRegs[] = {
4067       PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
4068       PPC::F8
4069     };
4070     unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
4071 
4072     if (useSoftFloat() || hasSPE())
4073        NumFPArgRegs = 0;
4074 
4075     FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs));
4076     FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs));
4077 
4078     // Make room for NumGPArgRegs and NumFPArgRegs.
4079     int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
4080                 NumFPArgRegs * MVT(MVT::f64).getSizeInBits()/8;
4081 
4082     FuncInfo->setVarArgsStackOffset(
4083       MFI.CreateFixedObject(PtrVT.getSizeInBits()/8,
4084                             CCInfo.getNextStackOffset(), true));
4085 
4086     FuncInfo->setVarArgsFrameIndex(
4087         MFI.CreateStackObject(Depth, Align(8), false));
4088     SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
4089 
4090     // The fixed integer arguments of a variadic function are stored to the
4091     // VarArgsFrameIndex on the stack so that they may be loaded by
4092     // dereferencing the result of va_next.
4093     for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
4094       // Get an existing live-in vreg, or add a new one.
4095       unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
4096       if (!VReg)
4097         VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
4098 
4099       SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
4100       SDValue Store =
4101           DAG.getStore(Val.getValue(1), dl, Val, FIN, MachinePointerInfo());
4102       MemOps.push_back(Store);
4103       // Increment the address by four for the next argument to store
4104       SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, dl, PtrVT);
4105       FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
4106     }
4107 
4108     // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
4109     // is set.
4110     // The double arguments are stored to the VarArgsFrameIndex
4111     // on the stack.
4112     for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
4113       // Get an existing live-in vreg, or add a new one.
4114       unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
4115       if (!VReg)
4116         VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
4117 
4118       SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
4119       SDValue Store =
4120           DAG.getStore(Val.getValue(1), dl, Val, FIN, MachinePointerInfo());
4121       MemOps.push_back(Store);
4122       // Increment the address by eight for the next argument to store
4123       SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8, dl,
4124                                          PtrVT);
4125       FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
4126     }
4127   }
4128 
4129   if (!MemOps.empty())
4130     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
4131 
4132   return Chain;
4133 }
4134 
4135 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
4136 // value to MVT::i64 and then truncate to the correct register size.
4137 SDValue PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags,
4138                                              EVT ObjectVT, SelectionDAG &DAG,
4139                                              SDValue ArgVal,
4140                                              const SDLoc &dl) const {
4141   if (Flags.isSExt())
4142     ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
4143                          DAG.getValueType(ObjectVT));
4144   else if (Flags.isZExt())
4145     ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
4146                          DAG.getValueType(ObjectVT));
4147 
4148   return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal);
4149 }
4150 
4151 SDValue PPCTargetLowering::LowerFormalArguments_64SVR4(
4152     SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
4153     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
4154     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
4155   // TODO: add description of PPC stack frame format, or at least some docs.
4156   //
4157   bool isELFv2ABI = Subtarget.isELFv2ABI();
4158   bool isLittleEndian = Subtarget.isLittleEndian();
4159   MachineFunction &MF = DAG.getMachineFunction();
4160   MachineFrameInfo &MFI = MF.getFrameInfo();
4161   PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
4162 
4163   assert(!(CallConv == CallingConv::Fast && isVarArg) &&
4164          "fastcc not supported on varargs functions");
4165 
4166   EVT PtrVT = getPointerTy(MF.getDataLayout());
4167   // Potential tail calls could cause overwriting of argument stack slots.
4168   bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
4169                        (CallConv == CallingConv::Fast));
4170   unsigned PtrByteSize = 8;
4171   unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
4172 
4173   static const MCPhysReg GPR[] = {
4174     PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4175     PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4176   };
4177   static const MCPhysReg VR[] = {
4178     PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4179     PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4180   };
4181 
4182   const unsigned Num_GPR_Regs = array_lengthof(GPR);
4183   const unsigned Num_FPR_Regs = useSoftFloat() ? 0 : 13;
4184   const unsigned Num_VR_Regs  = array_lengthof(VR);
4185 
4186   // Do a first pass over the arguments to determine whether the ABI
4187   // guarantees that our caller has allocated the parameter save area
4188   // on its stack frame.  In the ELFv1 ABI, this is always the case;
4189   // in the ELFv2 ABI, it is true if this is a vararg function or if
4190   // any parameter is located in a stack slot.
4191 
4192   bool HasParameterArea = !isELFv2ABI || isVarArg;
4193   unsigned ParamAreaSize = Num_GPR_Regs * PtrByteSize;
4194   unsigned NumBytes = LinkageSize;
4195   unsigned AvailableFPRs = Num_FPR_Regs;
4196   unsigned AvailableVRs = Num_VR_Regs;
4197   for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
4198     if (Ins[i].Flags.isNest())
4199       continue;
4200 
4201     if (CalculateStackSlotUsed(Ins[i].VT, Ins[i].ArgVT, Ins[i].Flags,
4202                                PtrByteSize, LinkageSize, ParamAreaSize,
4203                                NumBytes, AvailableFPRs, AvailableVRs))
4204       HasParameterArea = true;
4205   }
4206 
4207   // Add DAG nodes to load the arguments or copy them out of registers.  On
4208   // entry to a function on PPC, the arguments start after the linkage area,
4209   // although the first ones are often in registers.
4210 
4211   unsigned ArgOffset = LinkageSize;
4212   unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
4213   SmallVector<SDValue, 8> MemOps;
4214   Function::const_arg_iterator FuncArg = MF.getFunction().arg_begin();
4215   unsigned CurArgIdx = 0;
4216   for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
4217     SDValue ArgVal;
4218     bool needsLoad = false;
4219     EVT ObjectVT = Ins[ArgNo].VT;
4220     EVT OrigVT = Ins[ArgNo].ArgVT;
4221     unsigned ObjSize = ObjectVT.getStoreSize();
4222     unsigned ArgSize = ObjSize;
4223     ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
4224     if (Ins[ArgNo].isOrigArg()) {
4225       std::advance(FuncArg, Ins[ArgNo].getOrigArgIndex() - CurArgIdx);
4226       CurArgIdx = Ins[ArgNo].getOrigArgIndex();
4227     }
4228     // We re-align the argument offset for each argument, except when using the
4229     // fast calling convention, when we need to make sure we do that only when
4230     // we'll actually use a stack slot.
4231     unsigned CurArgOffset;
4232     Align Alignment;
4233     auto ComputeArgOffset = [&]() {
4234       /* Respect alignment of argument on the stack.  */
4235       Alignment =
4236           CalculateStackSlotAlignment(ObjectVT, OrigVT, Flags, PtrByteSize);
4237       ArgOffset = alignTo(ArgOffset, Alignment);
4238       CurArgOffset = ArgOffset;
4239     };
4240 
4241     if (CallConv != CallingConv::Fast) {
4242       ComputeArgOffset();
4243 
4244       /* Compute GPR index associated with argument offset.  */
4245       GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
4246       GPR_idx = std::min(GPR_idx, Num_GPR_Regs);
4247     }
4248 
4249     // FIXME the codegen can be much improved in some cases.
4250     // We do not have to keep everything in memory.
4251     if (Flags.isByVal()) {
4252       assert(Ins[ArgNo].isOrigArg() && "Byval arguments cannot be implicit");
4253 
4254       if (CallConv == CallingConv::Fast)
4255         ComputeArgOffset();
4256 
4257       // ObjSize is the true size, ArgSize rounded up to multiple of registers.
4258       ObjSize = Flags.getByValSize();
4259       ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
4260       // Empty aggregate parameters do not take up registers.  Examples:
4261       //   struct { } a;
4262       //   union  { } b;
4263       //   int c[0];
4264       // etc.  However, we have to provide a place-holder in InVals, so
4265       // pretend we have an 8-byte item at the current address for that
4266       // purpose.
4267       if (!ObjSize) {
4268         int FI = MFI.CreateFixedObject(PtrByteSize, ArgOffset, true);
4269         SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
4270         InVals.push_back(FIN);
4271         continue;
4272       }
4273 
4274       // Create a stack object covering all stack doublewords occupied
4275       // by the argument.  If the argument is (fully or partially) on
4276       // the stack, or if the argument is fully in registers but the
4277       // caller has allocated the parameter save anyway, we can refer
4278       // directly to the caller's stack frame.  Otherwise, create a
4279       // local copy in our own frame.
4280       int FI;
4281       if (HasParameterArea ||
4282           ArgSize + ArgOffset > LinkageSize + Num_GPR_Regs * PtrByteSize)
4283         FI = MFI.CreateFixedObject(ArgSize, ArgOffset, false, true);
4284       else
4285         FI = MFI.CreateStackObject(ArgSize, Alignment, false);
4286       SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
4287 
4288       // Handle aggregates smaller than 8 bytes.
4289       if (ObjSize < PtrByteSize) {
4290         // The value of the object is its address, which differs from the
4291         // address of the enclosing doubleword on big-endian systems.
4292         SDValue Arg = FIN;
4293         if (!isLittleEndian) {
4294           SDValue ArgOff = DAG.getConstant(PtrByteSize - ObjSize, dl, PtrVT);
4295           Arg = DAG.getNode(ISD::ADD, dl, ArgOff.getValueType(), Arg, ArgOff);
4296         }
4297         InVals.push_back(Arg);
4298 
4299         if (GPR_idx != Num_GPR_Regs) {
4300           unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
4301           FuncInfo->addLiveInAttr(VReg, Flags);
4302           SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
4303           SDValue Store;
4304 
4305           if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
4306             EVT ObjType = (ObjSize == 1 ? MVT::i8 :
4307                            (ObjSize == 2 ? MVT::i16 : MVT::i32));
4308             Store = DAG.getTruncStore(Val.getValue(1), dl, Val, Arg,
4309                                       MachinePointerInfo(&*FuncArg), ObjType);
4310           } else {
4311             // For sizes that don't fit a truncating store (3, 5, 6, 7),
4312             // store the whole register as-is to the parameter save area
4313             // slot.
4314             Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
4315                                  MachinePointerInfo(&*FuncArg));
4316           }
4317 
4318           MemOps.push_back(Store);
4319         }
4320         // Whether we copied from a register or not, advance the offset
4321         // into the parameter save area by a full doubleword.
4322         ArgOffset += PtrByteSize;
4323         continue;
4324       }
4325 
4326       // The value of the object is its address, which is the address of
4327       // its first stack doubleword.
4328       InVals.push_back(FIN);
4329 
4330       // Store whatever pieces of the object are in registers to memory.
4331       for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
4332         if (GPR_idx == Num_GPR_Regs)
4333           break;
4334 
4335         unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
4336         FuncInfo->addLiveInAttr(VReg, Flags);
4337         SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
4338         SDValue Addr = FIN;
4339         if (j) {
4340           SDValue Off = DAG.getConstant(j, dl, PtrVT);
4341           Addr = DAG.getNode(ISD::ADD, dl, Off.getValueType(), Addr, Off);
4342         }
4343         SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, Addr,
4344                                      MachinePointerInfo(&*FuncArg, j));
4345         MemOps.push_back(Store);
4346         ++GPR_idx;
4347       }
4348       ArgOffset += ArgSize;
4349       continue;
4350     }
4351 
4352     switch (ObjectVT.getSimpleVT().SimpleTy) {
4353     default: llvm_unreachable("Unhandled argument type!");
4354     case MVT::i1:
4355     case MVT::i32:
4356     case MVT::i64:
4357       if (Flags.isNest()) {
4358         // The 'nest' parameter, if any, is passed in R11.
4359         unsigned VReg = MF.addLiveIn(PPC::X11, &PPC::G8RCRegClass);
4360         ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
4361 
4362         if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
4363           ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
4364 
4365         break;
4366       }
4367 
4368       // These can be scalar arguments or elements of an integer array type
4369       // passed directly.  Clang may use those instead of "byval" aggregate
4370       // types to avoid forcing arguments to memory unnecessarily.
4371       if (GPR_idx != Num_GPR_Regs) {
4372         unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
4373         FuncInfo->addLiveInAttr(VReg, Flags);
4374         ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
4375 
4376         if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
4377           // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
4378           // value to MVT::i64 and then truncate to the correct register size.
4379           ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
4380       } else {
4381         if (CallConv == CallingConv::Fast)
4382           ComputeArgOffset();
4383 
4384         needsLoad = true;
4385         ArgSize = PtrByteSize;
4386       }
4387       if (CallConv != CallingConv::Fast || needsLoad)
4388         ArgOffset += 8;
4389       break;
4390 
4391     case MVT::f32:
4392     case MVT::f64:
4393       // These can be scalar arguments or elements of a float array type
4394       // passed directly.  The latter are used to implement ELFv2 homogenous
4395       // float aggregates.
4396       if (FPR_idx != Num_FPR_Regs) {
4397         unsigned VReg;
4398 
4399         if (ObjectVT == MVT::f32)
4400           VReg = MF.addLiveIn(FPR[FPR_idx],
4401                               Subtarget.hasP8Vector()
4402                                   ? &PPC::VSSRCRegClass
4403                                   : &PPC::F4RCRegClass);
4404         else
4405           VReg = MF.addLiveIn(FPR[FPR_idx], Subtarget.hasVSX()
4406                                                 ? &PPC::VSFRCRegClass
4407                                                 : &PPC::F8RCRegClass);
4408 
4409         ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
4410         ++FPR_idx;
4411       } else if (GPR_idx != Num_GPR_Regs && CallConv != CallingConv::Fast) {
4412         // FIXME: We may want to re-enable this for CallingConv::Fast on the P8
4413         // once we support fp <-> gpr moves.
4414 
4415         // This can only ever happen in the presence of f32 array types,
4416         // since otherwise we never run out of FPRs before running out
4417         // of GPRs.
4418         unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass);
4419         FuncInfo->addLiveInAttr(VReg, Flags);
4420         ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
4421 
4422         if (ObjectVT == MVT::f32) {
4423           if ((ArgOffset % PtrByteSize) == (isLittleEndian ? 4 : 0))
4424             ArgVal = DAG.getNode(ISD::SRL, dl, MVT::i64, ArgVal,
4425                                  DAG.getConstant(32, dl, MVT::i32));
4426           ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
4427         }
4428 
4429         ArgVal = DAG.getNode(ISD::BITCAST, dl, ObjectVT, ArgVal);
4430       } else {
4431         if (CallConv == CallingConv::Fast)
4432           ComputeArgOffset();
4433 
4434         needsLoad = true;
4435       }
4436 
4437       // When passing an array of floats, the array occupies consecutive
4438       // space in the argument area; only round up to the next doubleword
4439       // at the end of the array.  Otherwise, each float takes 8 bytes.
4440       if (CallConv != CallingConv::Fast || needsLoad) {
4441         ArgSize = Flags.isInConsecutiveRegs() ? ObjSize : PtrByteSize;
4442         ArgOffset += ArgSize;
4443         if (Flags.isInConsecutiveRegsLast())
4444           ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
4445       }
4446       break;
4447     case MVT::v4f32:
4448     case MVT::v4i32:
4449     case MVT::v8i16:
4450     case MVT::v16i8:
4451     case MVT::v2f64:
4452     case MVT::v2i64:
4453     case MVT::v1i128:
4454     case MVT::f128:
4455       // These can be scalar arguments or elements of a vector array type
4456       // passed directly.  The latter are used to implement ELFv2 homogenous
4457       // vector aggregates.
4458       if (VR_idx != Num_VR_Regs) {
4459         unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
4460         ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
4461         ++VR_idx;
4462       } else {
4463         if (CallConv == CallingConv::Fast)
4464           ComputeArgOffset();
4465         needsLoad = true;
4466       }
4467       if (CallConv != CallingConv::Fast || needsLoad)
4468         ArgOffset += 16;
4469       break;
4470     }
4471 
4472     // We need to load the argument to a virtual register if we determined
4473     // above that we ran out of physical registers of the appropriate type.
4474     if (needsLoad) {
4475       if (ObjSize < ArgSize && !isLittleEndian)
4476         CurArgOffset += ArgSize - ObjSize;
4477       int FI = MFI.CreateFixedObject(ObjSize, CurArgOffset, isImmutable);
4478       SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
4479       ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo());
4480     }
4481 
4482     InVals.push_back(ArgVal);
4483   }
4484 
4485   // Area that is at least reserved in the caller of this function.
4486   unsigned MinReservedArea;
4487   if (HasParameterArea)
4488     MinReservedArea = std::max(ArgOffset, LinkageSize + 8 * PtrByteSize);
4489   else
4490     MinReservedArea = LinkageSize;
4491 
4492   // Set the size that is at least reserved in caller of this function.  Tail
4493   // call optimized functions' reserved stack space needs to be aligned so that
4494   // taking the difference between two stack areas will result in an aligned
4495   // stack.
4496   MinReservedArea =
4497       EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea);
4498   FuncInfo->setMinReservedArea(MinReservedArea);
4499 
4500   // If the function takes variable number of arguments, make a frame index for
4501   // the start of the first vararg value... for expansion of llvm.va_start.
4502   // On ELFv2ABI spec, it writes:
4503   // C programs that are intended to be *portable* across different compilers
4504   // and architectures must use the header file <stdarg.h> to deal with variable
4505   // argument lists.
4506   if (isVarArg && MFI.hasVAStart()) {
4507     int Depth = ArgOffset;
4508 
4509     FuncInfo->setVarArgsFrameIndex(
4510       MFI.CreateFixedObject(PtrByteSize, Depth, true));
4511     SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
4512 
4513     // If this function is vararg, store any remaining integer argument regs
4514     // to their spots on the stack so that they may be loaded by dereferencing
4515     // the result of va_next.
4516     for (GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
4517          GPR_idx < Num_GPR_Regs; ++GPR_idx) {
4518       unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
4519       SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
4520       SDValue Store =
4521           DAG.getStore(Val.getValue(1), dl, Val, FIN, MachinePointerInfo());
4522       MemOps.push_back(Store);
4523       // Increment the address by four for the next argument to store
4524       SDValue PtrOff = DAG.getConstant(PtrByteSize, dl, PtrVT);
4525       FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
4526     }
4527   }
4528 
4529   if (!MemOps.empty())
4530     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
4531 
4532   return Chain;
4533 }
4534 
4535 /// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
4536 /// adjusted to accommodate the arguments for the tailcall.
4537 static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
4538                                    unsigned ParamSize) {
4539 
4540   if (!isTailCall) return 0;
4541 
4542   PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
4543   unsigned CallerMinReservedArea = FI->getMinReservedArea();
4544   int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
4545   // Remember only if the new adjustment is bigger.
4546   if (SPDiff < FI->getTailCallSPDelta())
4547     FI->setTailCallSPDelta(SPDiff);
4548 
4549   return SPDiff;
4550 }
4551 
4552 static bool isFunctionGlobalAddress(SDValue Callee);
4553 
4554 static bool callsShareTOCBase(const Function *Caller, SDValue Callee,
4555                               const TargetMachine &TM) {
4556   // It does not make sense to call callsShareTOCBase() with a caller that
4557   // is PC Relative since PC Relative callers do not have a TOC.
4558 #ifndef NDEBUG
4559   const PPCSubtarget *STICaller = &TM.getSubtarget<PPCSubtarget>(*Caller);
4560   assert(!STICaller->isUsingPCRelativeCalls() &&
4561          "PC Relative callers do not have a TOC and cannot share a TOC Base");
4562 #endif
4563 
4564   // Callee is either a GlobalAddress or an ExternalSymbol. ExternalSymbols
4565   // don't have enough information to determine if the caller and callee share
4566   // the same  TOC base, so we have to pessimistically assume they don't for
4567   // correctness.
4568   GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
4569   if (!G)
4570     return false;
4571 
4572   const GlobalValue *GV = G->getGlobal();
4573 
4574   // If the callee is preemptable, then the static linker will use a plt-stub
4575   // which saves the toc to the stack, and needs a nop after the call
4576   // instruction to convert to a toc-restore.
4577   if (!TM.shouldAssumeDSOLocal(*Caller->getParent(), GV))
4578     return false;
4579 
4580   // Functions with PC Relative enabled may clobber the TOC in the same DSO.
4581   // We may need a TOC restore in the situation where the caller requires a
4582   // valid TOC but the callee is PC Relative and does not.
4583   const Function *F = dyn_cast<Function>(GV);
4584   const GlobalAlias *Alias = dyn_cast<GlobalAlias>(GV);
4585 
4586   // If we have an Alias we can try to get the function from there.
4587   if (Alias) {
4588     const GlobalObject *GlobalObj = Alias->getBaseObject();
4589     F = dyn_cast<Function>(GlobalObj);
4590   }
4591 
4592   // If we still have no valid function pointer we do not have enough
4593   // information to determine if the callee uses PC Relative calls so we must
4594   // assume that it does.
4595   if (!F)
4596     return false;
4597 
4598   // If the callee uses PC Relative we cannot guarantee that the callee won't
4599   // clobber the TOC of the caller and so we must assume that the two
4600   // functions do not share a TOC base.
4601   const PPCSubtarget *STICallee = &TM.getSubtarget<PPCSubtarget>(*F);
4602   if (STICallee->isUsingPCRelativeCalls())
4603     return false;
4604 
4605   // If the GV is not a strong definition then we need to assume it can be
4606   // replaced by another function at link time. The function that replaces
4607   // it may not share the same TOC as the caller since the callee may be
4608   // replaced by a PC Relative version of the same function.
4609   if (!GV->isStrongDefinitionForLinker())
4610     return false;
4611 
4612   // The medium and large code models are expected to provide a sufficiently
4613   // large TOC to provide all data addressing needs of a module with a
4614   // single TOC.
4615   if (CodeModel::Medium == TM.getCodeModel() ||
4616       CodeModel::Large == TM.getCodeModel())
4617     return true;
4618 
4619   // Any explicitly-specified sections and section prefixes must also match.
4620   // Also, if we're using -ffunction-sections, then each function is always in
4621   // a different section (the same is true for COMDAT functions).
4622   if (TM.getFunctionSections() || GV->hasComdat() || Caller->hasComdat() ||
4623       GV->getSection() != Caller->getSection())
4624     return false;
4625   if (const auto *F = dyn_cast<Function>(GV)) {
4626     if (F->getSectionPrefix() != Caller->getSectionPrefix())
4627       return false;
4628   }
4629 
4630   return true;
4631 }
4632 
4633 static bool
4634 needStackSlotPassParameters(const PPCSubtarget &Subtarget,
4635                             const SmallVectorImpl<ISD::OutputArg> &Outs) {
4636   assert(Subtarget.is64BitELFABI());
4637 
4638   const unsigned PtrByteSize = 8;
4639   const unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
4640 
4641   static const MCPhysReg GPR[] = {
4642     PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4643     PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4644   };
4645   static const MCPhysReg VR[] = {
4646     PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4647     PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4648   };
4649 
4650   const unsigned NumGPRs = array_lengthof(GPR);
4651   const unsigned NumFPRs = 13;
4652   const unsigned NumVRs = array_lengthof(VR);
4653   const unsigned ParamAreaSize = NumGPRs * PtrByteSize;
4654 
4655   unsigned NumBytes = LinkageSize;
4656   unsigned AvailableFPRs = NumFPRs;
4657   unsigned AvailableVRs = NumVRs;
4658 
4659   for (const ISD::OutputArg& Param : Outs) {
4660     if (Param.Flags.isNest()) continue;
4661 
4662     if (CalculateStackSlotUsed(Param.VT, Param.ArgVT, Param.Flags, PtrByteSize,
4663                                LinkageSize, ParamAreaSize, NumBytes,
4664                                AvailableFPRs, AvailableVRs))
4665       return true;
4666   }
4667   return false;
4668 }
4669 
4670 static bool hasSameArgumentList(const Function *CallerFn, const CallBase &CB) {
4671   if (CB.arg_size() != CallerFn->arg_size())
4672     return false;
4673 
4674   auto CalleeArgIter = CB.arg_begin();
4675   auto CalleeArgEnd = CB.arg_end();
4676   Function::const_arg_iterator CallerArgIter = CallerFn->arg_begin();
4677 
4678   for (; CalleeArgIter != CalleeArgEnd; ++CalleeArgIter, ++CallerArgIter) {
4679     const Value* CalleeArg = *CalleeArgIter;
4680     const Value* CallerArg = &(*CallerArgIter);
4681     if (CalleeArg == CallerArg)
4682       continue;
4683 
4684     // e.g. @caller([4 x i64] %a, [4 x i64] %b) {
4685     //        tail call @callee([4 x i64] undef, [4 x i64] %b)
4686     //      }
4687     // 1st argument of callee is undef and has the same type as caller.
4688     if (CalleeArg->getType() == CallerArg->getType() &&
4689         isa<UndefValue>(CalleeArg))
4690       continue;
4691 
4692     return false;
4693   }
4694 
4695   return true;
4696 }
4697 
4698 // Returns true if TCO is possible between the callers and callees
4699 // calling conventions.
4700 static bool
4701 areCallingConvEligibleForTCO_64SVR4(CallingConv::ID CallerCC,
4702                                     CallingConv::ID CalleeCC) {
4703   // Tail calls are possible with fastcc and ccc.
4704   auto isTailCallableCC  = [] (CallingConv::ID CC){
4705       return  CC == CallingConv::C || CC == CallingConv::Fast;
4706   };
4707   if (!isTailCallableCC(CallerCC) || !isTailCallableCC(CalleeCC))
4708     return false;
4709 
4710   // We can safely tail call both fastcc and ccc callees from a c calling
4711   // convention caller. If the caller is fastcc, we may have less stack space
4712   // than a non-fastcc caller with the same signature so disable tail-calls in
4713   // that case.
4714   return CallerCC == CallingConv::C || CallerCC == CalleeCC;
4715 }
4716 
4717 bool PPCTargetLowering::IsEligibleForTailCallOptimization_64SVR4(
4718     SDValue Callee, CallingConv::ID CalleeCC, const CallBase *CB, bool isVarArg,
4719     const SmallVectorImpl<ISD::OutputArg> &Outs,
4720     const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG) const {
4721   bool TailCallOpt = getTargetMachine().Options.GuaranteedTailCallOpt;
4722 
4723   if (DisableSCO && !TailCallOpt) return false;
4724 
4725   // Variadic argument functions are not supported.
4726   if (isVarArg) return false;
4727 
4728   auto &Caller = DAG.getMachineFunction().getFunction();
4729   // Check that the calling conventions are compatible for tco.
4730   if (!areCallingConvEligibleForTCO_64SVR4(Caller.getCallingConv(), CalleeCC))
4731     return false;
4732 
4733   // Caller contains any byval parameter is not supported.
4734   if (any_of(Ins, [](const ISD::InputArg &IA) { return IA.Flags.isByVal(); }))
4735     return false;
4736 
4737   // Callee contains any byval parameter is not supported, too.
4738   // Note: This is a quick work around, because in some cases, e.g.
4739   // caller's stack size > callee's stack size, we are still able to apply
4740   // sibling call optimization. For example, gcc is able to do SCO for caller1
4741   // in the following example, but not for caller2.
4742   //   struct test {
4743   //     long int a;
4744   //     char ary[56];
4745   //   } gTest;
4746   //   __attribute__((noinline)) int callee(struct test v, struct test *b) {
4747   //     b->a = v.a;
4748   //     return 0;
4749   //   }
4750   //   void caller1(struct test a, struct test c, struct test *b) {
4751   //     callee(gTest, b); }
4752   //   void caller2(struct test *b) { callee(gTest, b); }
4753   if (any_of(Outs, [](const ISD::OutputArg& OA) { return OA.Flags.isByVal(); }))
4754     return false;
4755 
4756   // If callee and caller use different calling conventions, we cannot pass
4757   // parameters on stack since offsets for the parameter area may be different.
4758   if (Caller.getCallingConv() != CalleeCC &&
4759       needStackSlotPassParameters(Subtarget, Outs))
4760     return false;
4761 
4762   // All variants of 64-bit ELF ABIs without PC-Relative addressing require that
4763   // the caller and callee share the same TOC for TCO/SCO. If the caller and
4764   // callee potentially have different TOC bases then we cannot tail call since
4765   // we need to restore the TOC pointer after the call.
4766   // ref: https://bugzilla.mozilla.org/show_bug.cgi?id=973977
4767   // We cannot guarantee this for indirect calls or calls to external functions.
4768   // When PC-Relative addressing is used, the concept of the TOC is no longer
4769   // applicable so this check is not required.
4770   // Check first for indirect calls.
4771   if (!Subtarget.isUsingPCRelativeCalls() &&
4772       !isFunctionGlobalAddress(Callee) && !isa<ExternalSymbolSDNode>(Callee))
4773     return false;
4774 
4775   // Check if we share the TOC base.
4776   if (!Subtarget.isUsingPCRelativeCalls() &&
4777       !callsShareTOCBase(&Caller, Callee, getTargetMachine()))
4778     return false;
4779 
4780   // TCO allows altering callee ABI, so we don't have to check further.
4781   if (CalleeCC == CallingConv::Fast && TailCallOpt)
4782     return true;
4783 
4784   if (DisableSCO) return false;
4785 
4786   // If callee use the same argument list that caller is using, then we can
4787   // apply SCO on this case. If it is not, then we need to check if callee needs
4788   // stack for passing arguments.
4789   // PC Relative tail calls may not have a CallBase.
4790   // If there is no CallBase we cannot verify if we have the same argument
4791   // list so assume that we don't have the same argument list.
4792   if (CB && !hasSameArgumentList(&Caller, *CB) &&
4793       needStackSlotPassParameters(Subtarget, Outs))
4794     return false;
4795   else if (!CB && needStackSlotPassParameters(Subtarget, Outs))
4796     return false;
4797 
4798   return true;
4799 }
4800 
4801 /// IsEligibleForTailCallOptimization - Check whether the call is eligible
4802 /// for tail call optimization. Targets which want to do tail call
4803 /// optimization should implement this function.
4804 bool
4805 PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
4806                                                      CallingConv::ID CalleeCC,
4807                                                      bool isVarArg,
4808                                       const SmallVectorImpl<ISD::InputArg> &Ins,
4809                                                      SelectionDAG& DAG) const {
4810   if (!getTargetMachine().Options.GuaranteedTailCallOpt)
4811     return false;
4812 
4813   // Variable argument functions are not supported.
4814   if (isVarArg)
4815     return false;
4816 
4817   MachineFunction &MF = DAG.getMachineFunction();
4818   CallingConv::ID CallerCC = MF.getFunction().getCallingConv();
4819   if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
4820     // Functions containing by val parameters are not supported.
4821     for (unsigned i = 0; i != Ins.size(); i++) {
4822        ISD::ArgFlagsTy Flags = Ins[i].Flags;
4823        if (Flags.isByVal()) return false;
4824     }
4825 
4826     // Non-PIC/GOT tail calls are supported.
4827     if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
4828       return true;
4829 
4830     // At the moment we can only do local tail calls (in same module, hidden
4831     // or protected) if we are generating PIC.
4832     if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
4833       return G->getGlobal()->hasHiddenVisibility()
4834           || G->getGlobal()->hasProtectedVisibility();
4835   }
4836 
4837   return false;
4838 }
4839 
4840 /// isCallCompatibleAddress - Return the immediate to use if the specified
4841 /// 32-bit value is representable in the immediate field of a BxA instruction.
4842 static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
4843   ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4844   if (!C) return nullptr;
4845 
4846   int Addr = C->getZExtValue();
4847   if ((Addr & 3) != 0 ||  // Low 2 bits are implicitly zero.
4848       SignExtend32<26>(Addr) != Addr)
4849     return nullptr;  // Top 6 bits have to be sext of immediate.
4850 
4851   return DAG
4852       .getConstant(
4853           (int)C->getZExtValue() >> 2, SDLoc(Op),
4854           DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()))
4855       .getNode();
4856 }
4857 
4858 namespace {
4859 
4860 struct TailCallArgumentInfo {
4861   SDValue Arg;
4862   SDValue FrameIdxOp;
4863   int FrameIdx = 0;
4864 
4865   TailCallArgumentInfo() = default;
4866 };
4867 
4868 } // end anonymous namespace
4869 
4870 /// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
4871 static void StoreTailCallArgumentsToStackSlot(
4872     SelectionDAG &DAG, SDValue Chain,
4873     const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
4874     SmallVectorImpl<SDValue> &MemOpChains, const SDLoc &dl) {
4875   for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
4876     SDValue Arg = TailCallArgs[i].Arg;
4877     SDValue FIN = TailCallArgs[i].FrameIdxOp;
4878     int FI = TailCallArgs[i].FrameIdx;
4879     // Store relative to framepointer.
4880     MemOpChains.push_back(DAG.getStore(
4881         Chain, dl, Arg, FIN,
4882         MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI)));
4883   }
4884 }
4885 
4886 /// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
4887 /// the appropriate stack slot for the tail call optimized function call.
4888 static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG, SDValue Chain,
4889                                              SDValue OldRetAddr, SDValue OldFP,
4890                                              int SPDiff, const SDLoc &dl) {
4891   if (SPDiff) {
4892     // Calculate the new stack slot for the return address.
4893     MachineFunction &MF = DAG.getMachineFunction();
4894     const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
4895     const PPCFrameLowering *FL = Subtarget.getFrameLowering();
4896     bool isPPC64 = Subtarget.isPPC64();
4897     int SlotSize = isPPC64 ? 8 : 4;
4898     int NewRetAddrLoc = SPDiff + FL->getReturnSaveOffset();
4899     int NewRetAddr = MF.getFrameInfo().CreateFixedObject(SlotSize,
4900                                                          NewRetAddrLoc, true);
4901     EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
4902     SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
4903     Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
4904                          MachinePointerInfo::getFixedStack(MF, NewRetAddr));
4905   }
4906   return Chain;
4907 }
4908 
4909 /// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
4910 /// the position of the argument.
4911 static void
4912 CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
4913                          SDValue Arg, int SPDiff, unsigned ArgOffset,
4914                      SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
4915   int Offset = ArgOffset + SPDiff;
4916   uint32_t OpSize = (Arg.getValueSizeInBits() + 7) / 8;
4917   int FI = MF.getFrameInfo().CreateFixedObject(OpSize, Offset, true);
4918   EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
4919   SDValue FIN = DAG.getFrameIndex(FI, VT);
4920   TailCallArgumentInfo Info;
4921   Info.Arg = Arg;
4922   Info.FrameIdxOp = FIN;
4923   Info.FrameIdx = FI;
4924   TailCallArguments.push_back(Info);
4925 }
4926 
4927 /// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
4928 /// stack slot. Returns the chain as result and the loaded frame pointers in
4929 /// LROpOut/FPOpout. Used when tail calling.
4930 SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(
4931     SelectionDAG &DAG, int SPDiff, SDValue Chain, SDValue &LROpOut,
4932     SDValue &FPOpOut, const SDLoc &dl) const {
4933   if (SPDiff) {
4934     // Load the LR and FP stack slot for later adjusting.
4935     EVT VT = Subtarget.isPPC64() ? MVT::i64 : MVT::i32;
4936     LROpOut = getReturnAddrFrameIndex(DAG);
4937     LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo());
4938     Chain = SDValue(LROpOut.getNode(), 1);
4939   }
4940   return Chain;
4941 }
4942 
4943 /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
4944 /// by "Src" to address "Dst" of size "Size".  Alignment information is
4945 /// specified by the specific parameter attribute. The copy will be passed as
4946 /// a byval function parameter.
4947 /// Sometimes what we are copying is the end of a larger object, the part that
4948 /// does not fit in registers.
4949 static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
4950                                          SDValue Chain, ISD::ArgFlagsTy Flags,
4951                                          SelectionDAG &DAG, const SDLoc &dl) {
4952   SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
4953   return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
4954                        Flags.getNonZeroByValAlign(), false, false, false,
4955                        MachinePointerInfo(), MachinePointerInfo());
4956 }
4957 
4958 /// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
4959 /// tail calls.
4960 static void LowerMemOpCallTo(
4961     SelectionDAG &DAG, MachineFunction &MF, SDValue Chain, SDValue Arg,
4962     SDValue PtrOff, int SPDiff, unsigned ArgOffset, bool isPPC64,
4963     bool isTailCall, bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
4964     SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments, const SDLoc &dl) {
4965   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
4966   if (!isTailCall) {
4967     if (isVector) {
4968       SDValue StackPtr;
4969       if (isPPC64)
4970         StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
4971       else
4972         StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
4973       PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
4974                            DAG.getConstant(ArgOffset, dl, PtrVT));
4975     }
4976     MemOpChains.push_back(
4977         DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
4978     // Calculate and remember argument location.
4979   } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
4980                                   TailCallArguments);
4981 }
4982 
4983 static void
4984 PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
4985                 const SDLoc &dl, int SPDiff, unsigned NumBytes, SDValue LROp,
4986                 SDValue FPOp,
4987                 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
4988   // Emit a sequence of copyto/copyfrom virtual registers for arguments that
4989   // might overwrite each other in case of tail call optimization.
4990   SmallVector<SDValue, 8> MemOpChains2;
4991   // Do not flag preceding copytoreg stuff together with the following stuff.
4992   InFlag = SDValue();
4993   StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
4994                                     MemOpChains2, dl);
4995   if (!MemOpChains2.empty())
4996     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains2);
4997 
4998   // Store the return address to the appropriate stack slot.
4999   Chain = EmitTailCallStoreFPAndRetAddr(DAG, Chain, LROp, FPOp, SPDiff, dl);
5000 
5001   // Emit callseq_end just before tailcall node.
5002   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
5003                              DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
5004   InFlag = Chain.getValue(1);
5005 }
5006 
5007 // Is this global address that of a function that can be called by name? (as
5008 // opposed to something that must hold a descriptor for an indirect call).
5009 static bool isFunctionGlobalAddress(SDValue Callee) {
5010   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
5011     if (Callee.getOpcode() == ISD::GlobalTLSAddress ||
5012         Callee.getOpcode() == ISD::TargetGlobalTLSAddress)
5013       return false;
5014 
5015     return G->getGlobal()->getValueType()->isFunctionTy();
5016   }
5017 
5018   return false;
5019 }
5020 
5021 SDValue PPCTargetLowering::LowerCallResult(
5022     SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
5023     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
5024     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
5025   SmallVector<CCValAssign, 16> RVLocs;
5026   CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
5027                     *DAG.getContext());
5028 
5029   CCRetInfo.AnalyzeCallResult(
5030       Ins, (Subtarget.isSVR4ABI() && CallConv == CallingConv::Cold)
5031                ? RetCC_PPC_Cold
5032                : RetCC_PPC);
5033 
5034   // Copy all of the result registers out of their specified physreg.
5035   for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
5036     CCValAssign &VA = RVLocs[i];
5037     assert(VA.isRegLoc() && "Can only return in registers!");
5038 
5039     SDValue Val;
5040 
5041     if (Subtarget.hasSPE() && VA.getLocVT() == MVT::f64) {
5042       SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
5043                                       InFlag);
5044       Chain = Lo.getValue(1);
5045       InFlag = Lo.getValue(2);
5046       VA = RVLocs[++i]; // skip ahead to next loc
5047       SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
5048                                       InFlag);
5049       Chain = Hi.getValue(1);
5050       InFlag = Hi.getValue(2);
5051       if (!Subtarget.isLittleEndian())
5052         std::swap (Lo, Hi);
5053       Val = DAG.getNode(PPCISD::BUILD_SPE64, dl, MVT::f64, Lo, Hi);
5054     } else {
5055       Val = DAG.getCopyFromReg(Chain, dl,
5056                                VA.getLocReg(), VA.getLocVT(), InFlag);
5057       Chain = Val.getValue(1);
5058       InFlag = Val.getValue(2);
5059     }
5060 
5061     switch (VA.getLocInfo()) {
5062     default: llvm_unreachable("Unknown loc info!");
5063     case CCValAssign::Full: break;
5064     case CCValAssign::AExt:
5065       Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
5066       break;
5067     case CCValAssign::ZExt:
5068       Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
5069                         DAG.getValueType(VA.getValVT()));
5070       Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
5071       break;
5072     case CCValAssign::SExt:
5073       Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
5074                         DAG.getValueType(VA.getValVT()));
5075       Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
5076       break;
5077     }
5078 
5079     InVals.push_back(Val);
5080   }
5081 
5082   return Chain;
5083 }
5084 
5085 static bool isIndirectCall(const SDValue &Callee, SelectionDAG &DAG,
5086                            const PPCSubtarget &Subtarget, bool isPatchPoint) {
5087   // PatchPoint calls are not indirect.
5088   if (isPatchPoint)
5089     return false;
5090 
5091   if (isFunctionGlobalAddress(Callee) || isa<ExternalSymbolSDNode>(Callee))
5092     return false;
5093 
5094   // Darwin, and 32-bit ELF can use a BLA. The descriptor based ABIs can not
5095   // becuase the immediate function pointer points to a descriptor instead of
5096   // a function entry point. The ELFv2 ABI cannot use a BLA because the function
5097   // pointer immediate points to the global entry point, while the BLA would
5098   // need to jump to the local entry point (see rL211174).
5099   if (!Subtarget.usesFunctionDescriptors() && !Subtarget.isELFv2ABI() &&
5100       isBLACompatibleAddress(Callee, DAG))
5101     return false;
5102 
5103   return true;
5104 }
5105 
5106 // AIX and 64-bit ELF ABIs w/o PCRel require a TOC save/restore around calls.
5107 static inline bool isTOCSaveRestoreRequired(const PPCSubtarget &Subtarget) {
5108   return Subtarget.isAIXABI() ||
5109          (Subtarget.is64BitELFABI() && !Subtarget.isUsingPCRelativeCalls());
5110 }
5111 
5112 static unsigned getCallOpcode(PPCTargetLowering::CallFlags CFlags,
5113                               const Function &Caller,
5114                               const SDValue &Callee,
5115                               const PPCSubtarget &Subtarget,
5116                               const TargetMachine &TM) {
5117   if (CFlags.IsTailCall)
5118     return PPCISD::TC_RETURN;
5119 
5120   // This is a call through a function pointer.
5121   if (CFlags.IsIndirect) {
5122     // AIX and the 64-bit ELF ABIs need to maintain the TOC pointer accross
5123     // indirect calls. The save of the caller's TOC pointer to the stack will be
5124     // inserted into the DAG as part of call lowering. The restore of the TOC
5125     // pointer is modeled by using a pseudo instruction for the call opcode that
5126     // represents the 2 instruction sequence of an indirect branch and link,
5127     // immediately followed by a load of the TOC pointer from the the stack save
5128     // slot into gpr2. For 64-bit ELFv2 ABI with PCRel, do not restore the TOC
5129     // as it is not saved or used.
5130     return isTOCSaveRestoreRequired(Subtarget) ? PPCISD::BCTRL_LOAD_TOC
5131                                                : PPCISD::BCTRL;
5132   }
5133 
5134   if (Subtarget.isUsingPCRelativeCalls()) {
5135     assert(Subtarget.is64BitELFABI() && "PC Relative is only on ELF ABI.");
5136     return PPCISD::CALL_NOTOC;
5137   }
5138 
5139   // The ABIs that maintain a TOC pointer accross calls need to have a nop
5140   // immediately following the call instruction if the caller and callee may
5141   // have different TOC bases. At link time if the linker determines the calls
5142   // may not share a TOC base, the call is redirected to a trampoline inserted
5143   // by the linker. The trampoline will (among other things) save the callers
5144   // TOC pointer at an ABI designated offset in the linkage area and the linker
5145   // will rewrite the nop to be a load of the TOC pointer from the linkage area
5146   // into gpr2.
5147   if (Subtarget.isAIXABI() || Subtarget.is64BitELFABI())
5148     return callsShareTOCBase(&Caller, Callee, TM) ? PPCISD::CALL
5149                                                   : PPCISD::CALL_NOP;
5150 
5151   return PPCISD::CALL;
5152 }
5153 
5154 static SDValue transformCallee(const SDValue &Callee, SelectionDAG &DAG,
5155                                const SDLoc &dl, const PPCSubtarget &Subtarget) {
5156   if (!Subtarget.usesFunctionDescriptors() && !Subtarget.isELFv2ABI())
5157     if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG))
5158       return SDValue(Dest, 0);
5159 
5160   // Returns true if the callee is local, and false otherwise.
5161   auto isLocalCallee = [&]() {
5162     const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
5163     const Module *Mod = DAG.getMachineFunction().getFunction().getParent();
5164     const GlobalValue *GV = G ? G->getGlobal() : nullptr;
5165 
5166     return DAG.getTarget().shouldAssumeDSOLocal(*Mod, GV) &&
5167            !dyn_cast_or_null<GlobalIFunc>(GV);
5168   };
5169 
5170   // The PLT is only used in 32-bit ELF PIC mode.  Attempting to use the PLT in
5171   // a static relocation model causes some versions of GNU LD (2.17.50, at
5172   // least) to force BSS-PLT, instead of secure-PLT, even if all objects are
5173   // built with secure-PLT.
5174   bool UsePlt =
5175       Subtarget.is32BitELFABI() && !isLocalCallee() &&
5176       Subtarget.getTargetMachine().getRelocationModel() == Reloc::PIC_;
5177 
5178   const auto getAIXFuncEntryPointSymbolSDNode = [&](const GlobalValue *GV) {
5179     const TargetMachine &TM = Subtarget.getTargetMachine();
5180     const TargetLoweringObjectFile *TLOF = TM.getObjFileLowering();
5181     MCSymbolXCOFF *S =
5182         cast<MCSymbolXCOFF>(TLOF->getFunctionEntryPointSymbol(GV, TM));
5183 
5184     MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
5185     return DAG.getMCSymbol(S, PtrVT);
5186   };
5187 
5188   if (isFunctionGlobalAddress(Callee)) {
5189     const GlobalValue *GV = cast<GlobalAddressSDNode>(Callee)->getGlobal();
5190 
5191     if (Subtarget.isAIXABI()) {
5192       assert(!isa<GlobalIFunc>(GV) && "IFunc is not supported on AIX.");
5193       return getAIXFuncEntryPointSymbolSDNode(GV);
5194     }
5195     return DAG.getTargetGlobalAddress(GV, dl, Callee.getValueType(), 0,
5196                                       UsePlt ? PPCII::MO_PLT : 0);
5197   }
5198 
5199   if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
5200     const char *SymName = S->getSymbol();
5201     if (Subtarget.isAIXABI()) {
5202       // If there exists a user-declared function whose name is the same as the
5203       // ExternalSymbol's, then we pick up the user-declared version.
5204       const Module *Mod = DAG.getMachineFunction().getFunction().getParent();
5205       if (const Function *F =
5206               dyn_cast_or_null<Function>(Mod->getNamedValue(SymName)))
5207         return getAIXFuncEntryPointSymbolSDNode(F);
5208 
5209       // On AIX, direct function calls reference the symbol for the function's
5210       // entry point, which is named by prepending a "." before the function's
5211       // C-linkage name. A Qualname is returned here because an external
5212       // function entry point is a csect with XTY_ER property.
5213       const auto getExternalFunctionEntryPointSymbol = [&](StringRef SymName) {
5214         auto &Context = DAG.getMachineFunction().getMMI().getContext();
5215         MCSectionXCOFF *Sec = Context.getXCOFFSection(
5216             (Twine(".") + Twine(SymName)).str(), SectionKind::getMetadata(),
5217             XCOFF::CsectProperties(XCOFF::XMC_PR, XCOFF::XTY_ER));
5218         return Sec->getQualNameSymbol();
5219       };
5220 
5221       SymName = getExternalFunctionEntryPointSymbol(SymName)->getName().data();
5222     }
5223     return DAG.getTargetExternalSymbol(SymName, Callee.getValueType(),
5224                                        UsePlt ? PPCII::MO_PLT : 0);
5225   }
5226 
5227   // No transformation needed.
5228   assert(Callee.getNode() && "What no callee?");
5229   return Callee;
5230 }
5231 
5232 static SDValue getOutputChainFromCallSeq(SDValue CallSeqStart) {
5233   assert(CallSeqStart.getOpcode() == ISD::CALLSEQ_START &&
5234          "Expected a CALLSEQ_STARTSDNode.");
5235 
5236   // The last operand is the chain, except when the node has glue. If the node
5237   // has glue, then the last operand is the glue, and the chain is the second
5238   // last operand.
5239   SDValue LastValue = CallSeqStart.getValue(CallSeqStart->getNumValues() - 1);
5240   if (LastValue.getValueType() != MVT::Glue)
5241     return LastValue;
5242 
5243   return CallSeqStart.getValue(CallSeqStart->getNumValues() - 2);
5244 }
5245 
5246 // Creates the node that moves a functions address into the count register
5247 // to prepare for an indirect call instruction.
5248 static void prepareIndirectCall(SelectionDAG &DAG, SDValue &Callee,
5249                                 SDValue &Glue, SDValue &Chain,
5250                                 const SDLoc &dl) {
5251   SDValue MTCTROps[] = {Chain, Callee, Glue};
5252   EVT ReturnTypes[] = {MVT::Other, MVT::Glue};
5253   Chain = DAG.getNode(PPCISD::MTCTR, dl, makeArrayRef(ReturnTypes, 2),
5254                       makeArrayRef(MTCTROps, Glue.getNode() ? 3 : 2));
5255   // The glue is the second value produced.
5256   Glue = Chain.getValue(1);
5257 }
5258 
5259 static void prepareDescriptorIndirectCall(SelectionDAG &DAG, SDValue &Callee,
5260                                           SDValue &Glue, SDValue &Chain,
5261                                           SDValue CallSeqStart,
5262                                           const CallBase *CB, const SDLoc &dl,
5263                                           bool hasNest,
5264                                           const PPCSubtarget &Subtarget) {
5265   // Function pointers in the 64-bit SVR4 ABI do not point to the function
5266   // entry point, but to the function descriptor (the function entry point
5267   // address is part of the function descriptor though).
5268   // The function descriptor is a three doubleword structure with the
5269   // following fields: function entry point, TOC base address and
5270   // environment pointer.
5271   // Thus for a call through a function pointer, the following actions need
5272   // to be performed:
5273   //   1. Save the TOC of the caller in the TOC save area of its stack
5274   //      frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
5275   //   2. Load the address of the function entry point from the function
5276   //      descriptor.
5277   //   3. Load the TOC of the callee from the function descriptor into r2.
5278   //   4. Load the environment pointer from the function descriptor into
5279   //      r11.
5280   //   5. Branch to the function entry point address.
5281   //   6. On return of the callee, the TOC of the caller needs to be
5282   //      restored (this is done in FinishCall()).
5283   //
5284   // The loads are scheduled at the beginning of the call sequence, and the
5285   // register copies are flagged together to ensure that no other
5286   // operations can be scheduled in between. E.g. without flagging the
5287   // copies together, a TOC access in the caller could be scheduled between
5288   // the assignment of the callee TOC and the branch to the callee, which leads
5289   // to incorrect code.
5290 
5291   // Start by loading the function address from the descriptor.
5292   SDValue LDChain = getOutputChainFromCallSeq(CallSeqStart);
5293   auto MMOFlags = Subtarget.hasInvariantFunctionDescriptors()
5294                       ? (MachineMemOperand::MODereferenceable |
5295                          MachineMemOperand::MOInvariant)
5296                       : MachineMemOperand::MONone;
5297 
5298   MachinePointerInfo MPI(CB ? CB->getCalledOperand() : nullptr);
5299 
5300   // Registers used in building the DAG.
5301   const MCRegister EnvPtrReg = Subtarget.getEnvironmentPointerRegister();
5302   const MCRegister TOCReg = Subtarget.getTOCPointerRegister();
5303 
5304   // Offsets of descriptor members.
5305   const unsigned TOCAnchorOffset = Subtarget.descriptorTOCAnchorOffset();
5306   const unsigned EnvPtrOffset = Subtarget.descriptorEnvironmentPointerOffset();
5307 
5308   const MVT RegVT = Subtarget.isPPC64() ? MVT::i64 : MVT::i32;
5309   const unsigned Alignment = Subtarget.isPPC64() ? 8 : 4;
5310 
5311   // One load for the functions entry point address.
5312   SDValue LoadFuncPtr = DAG.getLoad(RegVT, dl, LDChain, Callee, MPI,
5313                                     Alignment, MMOFlags);
5314 
5315   // One for loading the TOC anchor for the module that contains the called
5316   // function.
5317   SDValue TOCOff = DAG.getIntPtrConstant(TOCAnchorOffset, dl);
5318   SDValue AddTOC = DAG.getNode(ISD::ADD, dl, RegVT, Callee, TOCOff);
5319   SDValue TOCPtr =
5320       DAG.getLoad(RegVT, dl, LDChain, AddTOC,
5321                   MPI.getWithOffset(TOCAnchorOffset), Alignment, MMOFlags);
5322 
5323   // One for loading the environment pointer.
5324   SDValue PtrOff = DAG.getIntPtrConstant(EnvPtrOffset, dl);
5325   SDValue AddPtr = DAG.getNode(ISD::ADD, dl, RegVT, Callee, PtrOff);
5326   SDValue LoadEnvPtr =
5327       DAG.getLoad(RegVT, dl, LDChain, AddPtr,
5328                   MPI.getWithOffset(EnvPtrOffset), Alignment, MMOFlags);
5329 
5330 
5331   // Then copy the newly loaded TOC anchor to the TOC pointer.
5332   SDValue TOCVal = DAG.getCopyToReg(Chain, dl, TOCReg, TOCPtr, Glue);
5333   Chain = TOCVal.getValue(0);
5334   Glue = TOCVal.getValue(1);
5335 
5336   // If the function call has an explicit 'nest' parameter, it takes the
5337   // place of the environment pointer.
5338   assert((!hasNest || !Subtarget.isAIXABI()) &&
5339          "Nest parameter is not supported on AIX.");
5340   if (!hasNest) {
5341     SDValue EnvVal = DAG.getCopyToReg(Chain, dl, EnvPtrReg, LoadEnvPtr, Glue);
5342     Chain = EnvVal.getValue(0);
5343     Glue = EnvVal.getValue(1);
5344   }
5345 
5346   // The rest of the indirect call sequence is the same as the non-descriptor
5347   // DAG.
5348   prepareIndirectCall(DAG, LoadFuncPtr, Glue, Chain, dl);
5349 }
5350 
5351 static void
5352 buildCallOperands(SmallVectorImpl<SDValue> &Ops,
5353                   PPCTargetLowering::CallFlags CFlags, const SDLoc &dl,
5354                   SelectionDAG &DAG,
5355                   SmallVector<std::pair<unsigned, SDValue>, 8> &RegsToPass,
5356                   SDValue Glue, SDValue Chain, SDValue &Callee, int SPDiff,
5357                   const PPCSubtarget &Subtarget) {
5358   const bool IsPPC64 = Subtarget.isPPC64();
5359   // MVT for a general purpose register.
5360   const MVT RegVT = IsPPC64 ? MVT::i64 : MVT::i32;
5361 
5362   // First operand is always the chain.
5363   Ops.push_back(Chain);
5364 
5365   // If it's a direct call pass the callee as the second operand.
5366   if (!CFlags.IsIndirect)
5367     Ops.push_back(Callee);
5368   else {
5369     assert(!CFlags.IsPatchPoint && "Patch point calls are not indirect.");
5370 
5371     // For the TOC based ABIs, we have saved the TOC pointer to the linkage area
5372     // on the stack (this would have been done in `LowerCall_64SVR4` or
5373     // `LowerCall_AIX`). The call instruction is a pseudo instruction that
5374     // represents both the indirect branch and a load that restores the TOC
5375     // pointer from the linkage area. The operand for the TOC restore is an add
5376     // of the TOC save offset to the stack pointer. This must be the second
5377     // operand: after the chain input but before any other variadic arguments.
5378     // For 64-bit ELFv2 ABI with PCRel, do not restore the TOC as it is not
5379     // saved or used.
5380     if (isTOCSaveRestoreRequired(Subtarget)) {
5381       const MCRegister StackPtrReg = Subtarget.getStackPointerRegister();
5382 
5383       SDValue StackPtr = DAG.getRegister(StackPtrReg, RegVT);
5384       unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset();
5385       SDValue TOCOff = DAG.getIntPtrConstant(TOCSaveOffset, dl);
5386       SDValue AddTOC = DAG.getNode(ISD::ADD, dl, RegVT, StackPtr, TOCOff);
5387       Ops.push_back(AddTOC);
5388     }
5389 
5390     // Add the register used for the environment pointer.
5391     if (Subtarget.usesFunctionDescriptors() && !CFlags.HasNest)
5392       Ops.push_back(DAG.getRegister(Subtarget.getEnvironmentPointerRegister(),
5393                                     RegVT));
5394 
5395 
5396     // Add CTR register as callee so a bctr can be emitted later.
5397     if (CFlags.IsTailCall)
5398       Ops.push_back(DAG.getRegister(IsPPC64 ? PPC::CTR8 : PPC::CTR, RegVT));
5399   }
5400 
5401   // If this is a tail call add stack pointer delta.
5402   if (CFlags.IsTailCall)
5403     Ops.push_back(DAG.getConstant(SPDiff, dl, MVT::i32));
5404 
5405   // Add argument registers to the end of the list so that they are known live
5406   // into the call.
5407   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
5408     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
5409                                   RegsToPass[i].second.getValueType()));
5410 
5411   // We cannot add R2/X2 as an operand here for PATCHPOINT, because there is
5412   // no way to mark dependencies as implicit here.
5413   // We will add the R2/X2 dependency in EmitInstrWithCustomInserter.
5414   if ((Subtarget.is64BitELFABI() || Subtarget.isAIXABI()) &&
5415        !CFlags.IsPatchPoint && !Subtarget.isUsingPCRelativeCalls())
5416     Ops.push_back(DAG.getRegister(Subtarget.getTOCPointerRegister(), RegVT));
5417 
5418   // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
5419   if (CFlags.IsVarArg && Subtarget.is32BitELFABI())
5420     Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
5421 
5422   // Add a register mask operand representing the call-preserved registers.
5423   const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
5424   const uint32_t *Mask =
5425       TRI->getCallPreservedMask(DAG.getMachineFunction(), CFlags.CallConv);
5426   assert(Mask && "Missing call preserved mask for calling convention");
5427   Ops.push_back(DAG.getRegisterMask(Mask));
5428 
5429   // If the glue is valid, it is the last operand.
5430   if (Glue.getNode())
5431     Ops.push_back(Glue);
5432 }
5433 
5434 SDValue PPCTargetLowering::FinishCall(
5435     CallFlags CFlags, const SDLoc &dl, SelectionDAG &DAG,
5436     SmallVector<std::pair<unsigned, SDValue>, 8> &RegsToPass, SDValue Glue,
5437     SDValue Chain, SDValue CallSeqStart, SDValue &Callee, int SPDiff,
5438     unsigned NumBytes, const SmallVectorImpl<ISD::InputArg> &Ins,
5439     SmallVectorImpl<SDValue> &InVals, const CallBase *CB) const {
5440 
5441   if ((Subtarget.is64BitELFABI() && !Subtarget.isUsingPCRelativeCalls()) ||
5442       Subtarget.isAIXABI())
5443     setUsesTOCBasePtr(DAG);
5444 
5445   unsigned CallOpc =
5446       getCallOpcode(CFlags, DAG.getMachineFunction().getFunction(), Callee,
5447                     Subtarget, DAG.getTarget());
5448 
5449   if (!CFlags.IsIndirect)
5450     Callee = transformCallee(Callee, DAG, dl, Subtarget);
5451   else if (Subtarget.usesFunctionDescriptors())
5452     prepareDescriptorIndirectCall(DAG, Callee, Glue, Chain, CallSeqStart, CB,
5453                                   dl, CFlags.HasNest, Subtarget);
5454   else
5455     prepareIndirectCall(DAG, Callee, Glue, Chain, dl);
5456 
5457   // Build the operand list for the call instruction.
5458   SmallVector<SDValue, 8> Ops;
5459   buildCallOperands(Ops, CFlags, dl, DAG, RegsToPass, Glue, Chain, Callee,
5460                     SPDiff, Subtarget);
5461 
5462   // Emit tail call.
5463   if (CFlags.IsTailCall) {
5464     // Indirect tail call when using PC Relative calls do not have the same
5465     // constraints.
5466     assert(((Callee.getOpcode() == ISD::Register &&
5467              cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
5468             Callee.getOpcode() == ISD::TargetExternalSymbol ||
5469             Callee.getOpcode() == ISD::TargetGlobalAddress ||
5470             isa<ConstantSDNode>(Callee) ||
5471             (CFlags.IsIndirect && Subtarget.isUsingPCRelativeCalls())) &&
5472            "Expecting a global address, external symbol, absolute value, "
5473            "register or an indirect tail call when PC Relative calls are "
5474            "used.");
5475     // PC Relative calls also use TC_RETURN as the way to mark tail calls.
5476     assert(CallOpc == PPCISD::TC_RETURN &&
5477            "Unexpected call opcode for a tail call.");
5478     DAG.getMachineFunction().getFrameInfo().setHasTailCall();
5479     return DAG.getNode(CallOpc, dl, MVT::Other, Ops);
5480   }
5481 
5482   std::array<EVT, 2> ReturnTypes = {{MVT::Other, MVT::Glue}};
5483   Chain = DAG.getNode(CallOpc, dl, ReturnTypes, Ops);
5484   DAG.addNoMergeSiteInfo(Chain.getNode(), CFlags.NoMerge);
5485   Glue = Chain.getValue(1);
5486 
5487   // When performing tail call optimization the callee pops its arguments off
5488   // the stack. Account for this here so these bytes can be pushed back on in
5489   // PPCFrameLowering::eliminateCallFramePseudoInstr.
5490   int BytesCalleePops = (CFlags.CallConv == CallingConv::Fast &&
5491                          getTargetMachine().Options.GuaranteedTailCallOpt)
5492                             ? NumBytes
5493                             : 0;
5494 
5495   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
5496                              DAG.getIntPtrConstant(BytesCalleePops, dl, true),
5497                              Glue, dl);
5498   Glue = Chain.getValue(1);
5499 
5500   return LowerCallResult(Chain, Glue, CFlags.CallConv, CFlags.IsVarArg, Ins, dl,
5501                          DAG, InVals);
5502 }
5503 
5504 SDValue
5505 PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
5506                              SmallVectorImpl<SDValue> &InVals) const {
5507   SelectionDAG &DAG                     = CLI.DAG;
5508   SDLoc &dl                             = CLI.DL;
5509   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
5510   SmallVectorImpl<SDValue> &OutVals     = CLI.OutVals;
5511   SmallVectorImpl<ISD::InputArg> &Ins   = CLI.Ins;
5512   SDValue Chain                         = CLI.Chain;
5513   SDValue Callee                        = CLI.Callee;
5514   bool &isTailCall                      = CLI.IsTailCall;
5515   CallingConv::ID CallConv              = CLI.CallConv;
5516   bool isVarArg                         = CLI.IsVarArg;
5517   bool isPatchPoint                     = CLI.IsPatchPoint;
5518   const CallBase *CB                    = CLI.CB;
5519 
5520   if (isTailCall) {
5521     if (Subtarget.useLongCalls() && !(CB && CB->isMustTailCall()))
5522       isTailCall = false;
5523     else if (Subtarget.isSVR4ABI() && Subtarget.isPPC64())
5524       isTailCall = IsEligibleForTailCallOptimization_64SVR4(
5525           Callee, CallConv, CB, isVarArg, Outs, Ins, DAG);
5526     else
5527       isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
5528                                                      Ins, DAG);
5529     if (isTailCall) {
5530       ++NumTailCalls;
5531       if (!getTargetMachine().Options.GuaranteedTailCallOpt)
5532         ++NumSiblingCalls;
5533 
5534       // PC Relative calls no longer guarantee that the callee is a Global
5535       // Address Node. The callee could be an indirect tail call in which
5536       // case the SDValue for the callee could be a load (to load the address
5537       // of a function pointer) or it may be a register copy (to move the
5538       // address of the callee from a function parameter into a virtual
5539       // register). It may also be an ExternalSymbolSDNode (ex memcopy).
5540       assert((Subtarget.isUsingPCRelativeCalls() ||
5541               isa<GlobalAddressSDNode>(Callee)) &&
5542              "Callee should be an llvm::Function object.");
5543 
5544       LLVM_DEBUG(dbgs() << "TCO caller: " << DAG.getMachineFunction().getName()
5545                         << "\nTCO callee: ");
5546       LLVM_DEBUG(Callee.dump());
5547     }
5548   }
5549 
5550   if (!isTailCall && CB && CB->isMustTailCall())
5551     report_fatal_error("failed to perform tail call elimination on a call "
5552                        "site marked musttail");
5553 
5554   // When long calls (i.e. indirect calls) are always used, calls are always
5555   // made via function pointer. If we have a function name, first translate it
5556   // into a pointer.
5557   if (Subtarget.useLongCalls() && isa<GlobalAddressSDNode>(Callee) &&
5558       !isTailCall)
5559     Callee = LowerGlobalAddress(Callee, DAG);
5560 
5561   CallFlags CFlags(
5562       CallConv, isTailCall, isVarArg, isPatchPoint,
5563       isIndirectCall(Callee, DAG, Subtarget, isPatchPoint),
5564       // hasNest
5565       Subtarget.is64BitELFABI() &&
5566           any_of(Outs, [](ISD::OutputArg Arg) { return Arg.Flags.isNest(); }),
5567       CLI.NoMerge);
5568 
5569   if (Subtarget.isAIXABI())
5570     return LowerCall_AIX(Chain, Callee, CFlags, Outs, OutVals, Ins, dl, DAG,
5571                          InVals, CB);
5572 
5573   assert(Subtarget.isSVR4ABI());
5574   if (Subtarget.isPPC64())
5575     return LowerCall_64SVR4(Chain, Callee, CFlags, Outs, OutVals, Ins, dl, DAG,
5576                             InVals, CB);
5577   return LowerCall_32SVR4(Chain, Callee, CFlags, Outs, OutVals, Ins, dl, DAG,
5578                           InVals, CB);
5579 }
5580 
5581 SDValue PPCTargetLowering::LowerCall_32SVR4(
5582     SDValue Chain, SDValue Callee, CallFlags CFlags,
5583     const SmallVectorImpl<ISD::OutputArg> &Outs,
5584     const SmallVectorImpl<SDValue> &OutVals,
5585     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
5586     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
5587     const CallBase *CB) const {
5588   // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
5589   // of the 32-bit SVR4 ABI stack frame layout.
5590 
5591   const CallingConv::ID CallConv = CFlags.CallConv;
5592   const bool IsVarArg = CFlags.IsVarArg;
5593   const bool IsTailCall = CFlags.IsTailCall;
5594 
5595   assert((CallConv == CallingConv::C ||
5596           CallConv == CallingConv::Cold ||
5597           CallConv == CallingConv::Fast) && "Unknown calling convention!");
5598 
5599   const Align PtrAlign(4);
5600 
5601   MachineFunction &MF = DAG.getMachineFunction();
5602 
5603   // Mark this function as potentially containing a function that contains a
5604   // tail call. As a consequence the frame pointer will be used for dynamicalloc
5605   // and restoring the callers stack pointer in this functions epilog. This is
5606   // done because by tail calling the called function might overwrite the value
5607   // in this function's (MF) stack pointer stack slot 0(SP).
5608   if (getTargetMachine().Options.GuaranteedTailCallOpt &&
5609       CallConv == CallingConv::Fast)
5610     MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
5611 
5612   // Count how many bytes are to be pushed on the stack, including the linkage
5613   // area, parameter list area and the part of the local variable space which
5614   // contains copies of aggregates which are passed by value.
5615 
5616   // Assign locations to all of the outgoing arguments.
5617   SmallVector<CCValAssign, 16> ArgLocs;
5618   PPCCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
5619 
5620   // Reserve space for the linkage area on the stack.
5621   CCInfo.AllocateStack(Subtarget.getFrameLowering()->getLinkageSize(),
5622                        PtrAlign);
5623   if (useSoftFloat())
5624     CCInfo.PreAnalyzeCallOperands(Outs);
5625 
5626   if (IsVarArg) {
5627     // Handle fixed and variable vector arguments differently.
5628     // Fixed vector arguments go into registers as long as registers are
5629     // available. Variable vector arguments always go into memory.
5630     unsigned NumArgs = Outs.size();
5631 
5632     for (unsigned i = 0; i != NumArgs; ++i) {
5633       MVT ArgVT = Outs[i].VT;
5634       ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
5635       bool Result;
5636 
5637       if (Outs[i].IsFixed) {
5638         Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
5639                                CCInfo);
5640       } else {
5641         Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
5642                                       ArgFlags, CCInfo);
5643       }
5644 
5645       if (Result) {
5646 #ifndef NDEBUG
5647         errs() << "Call operand #" << i << " has unhandled type "
5648              << EVT(ArgVT).getEVTString() << "\n";
5649 #endif
5650         llvm_unreachable(nullptr);
5651       }
5652     }
5653   } else {
5654     // All arguments are treated the same.
5655     CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
5656   }
5657   CCInfo.clearWasPPCF128();
5658 
5659   // Assign locations to all of the outgoing aggregate by value arguments.
5660   SmallVector<CCValAssign, 16> ByValArgLocs;
5661   CCState CCByValInfo(CallConv, IsVarArg, MF, ByValArgLocs, *DAG.getContext());
5662 
5663   // Reserve stack space for the allocations in CCInfo.
5664   CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrAlign);
5665 
5666   CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
5667 
5668   // Size of the linkage area, parameter list area and the part of the local
5669   // space variable where copies of aggregates which are passed by value are
5670   // stored.
5671   unsigned NumBytes = CCByValInfo.getNextStackOffset();
5672 
5673   // Calculate by how many bytes the stack has to be adjusted in case of tail
5674   // call optimization.
5675   int SPDiff = CalculateTailCallSPDiff(DAG, IsTailCall, NumBytes);
5676 
5677   // Adjust the stack pointer for the new arguments...
5678   // These operations are automatically eliminated by the prolog/epilog pass
5679   Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
5680   SDValue CallSeqStart = Chain;
5681 
5682   // Load the return address and frame pointer so it can be moved somewhere else
5683   // later.
5684   SDValue LROp, FPOp;
5685   Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, dl);
5686 
5687   // Set up a copy of the stack pointer for use loading and storing any
5688   // arguments that may not fit in the registers available for argument
5689   // passing.
5690   SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
5691 
5692   SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
5693   SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
5694   SmallVector<SDValue, 8> MemOpChains;
5695 
5696   bool seenFloatArg = false;
5697   // Walk the register/memloc assignments, inserting copies/loads.
5698   // i - Tracks the index into the list of registers allocated for the call
5699   // RealArgIdx - Tracks the index into the list of actual function arguments
5700   // j - Tracks the index into the list of byval arguments
5701   for (unsigned i = 0, RealArgIdx = 0, j = 0, e = ArgLocs.size();
5702        i != e;
5703        ++i, ++RealArgIdx) {
5704     CCValAssign &VA = ArgLocs[i];
5705     SDValue Arg = OutVals[RealArgIdx];
5706     ISD::ArgFlagsTy Flags = Outs[RealArgIdx].Flags;
5707 
5708     if (Flags.isByVal()) {
5709       // Argument is an aggregate which is passed by value, thus we need to
5710       // create a copy of it in the local variable space of the current stack
5711       // frame (which is the stack frame of the caller) and pass the address of
5712       // this copy to the callee.
5713       assert((j < ByValArgLocs.size()) && "Index out of bounds!");
5714       CCValAssign &ByValVA = ByValArgLocs[j++];
5715       assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
5716 
5717       // Memory reserved in the local variable space of the callers stack frame.
5718       unsigned LocMemOffset = ByValVA.getLocMemOffset();
5719 
5720       SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
5721       PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(MF.getDataLayout()),
5722                            StackPtr, PtrOff);
5723 
5724       // Create a copy of the argument in the local area of the current
5725       // stack frame.
5726       SDValue MemcpyCall =
5727         CreateCopyOfByValArgument(Arg, PtrOff,
5728                                   CallSeqStart.getNode()->getOperand(0),
5729                                   Flags, DAG, dl);
5730 
5731       // This must go outside the CALLSEQ_START..END.
5732       SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall, NumBytes, 0,
5733                                                      SDLoc(MemcpyCall));
5734       DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
5735                              NewCallSeqStart.getNode());
5736       Chain = CallSeqStart = NewCallSeqStart;
5737 
5738       // Pass the address of the aggregate copy on the stack either in a
5739       // physical register or in the parameter list area of the current stack
5740       // frame to the callee.
5741       Arg = PtrOff;
5742     }
5743 
5744     // When useCRBits() is true, there can be i1 arguments.
5745     // It is because getRegisterType(MVT::i1) => MVT::i1,
5746     // and for other integer types getRegisterType() => MVT::i32.
5747     // Extend i1 and ensure callee will get i32.
5748     if (Arg.getValueType() == MVT::i1)
5749       Arg = DAG.getNode(Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5750                         dl, MVT::i32, Arg);
5751 
5752     if (VA.isRegLoc()) {
5753       seenFloatArg |= VA.getLocVT().isFloatingPoint();
5754       // Put argument in a physical register.
5755       if (Subtarget.hasSPE() && Arg.getValueType() == MVT::f64) {
5756         bool IsLE = Subtarget.isLittleEndian();
5757         SDValue SVal = DAG.getNode(PPCISD::EXTRACT_SPE, dl, MVT::i32, Arg,
5758                         DAG.getIntPtrConstant(IsLE ? 0 : 1, dl));
5759         RegsToPass.push_back(std::make_pair(VA.getLocReg(), SVal.getValue(0)));
5760         SVal = DAG.getNode(PPCISD::EXTRACT_SPE, dl, MVT::i32, Arg,
5761                            DAG.getIntPtrConstant(IsLE ? 1 : 0, dl));
5762         RegsToPass.push_back(std::make_pair(ArgLocs[++i].getLocReg(),
5763                              SVal.getValue(0)));
5764       } else
5765         RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
5766     } else {
5767       // Put argument in the parameter list area of the current stack frame.
5768       assert(VA.isMemLoc());
5769       unsigned LocMemOffset = VA.getLocMemOffset();
5770 
5771       if (!IsTailCall) {
5772         SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
5773         PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(MF.getDataLayout()),
5774                              StackPtr, PtrOff);
5775 
5776         MemOpChains.push_back(
5777             DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
5778       } else {
5779         // Calculate and remember argument location.
5780         CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
5781                                  TailCallArguments);
5782       }
5783     }
5784   }
5785 
5786   if (!MemOpChains.empty())
5787     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
5788 
5789   // Build a sequence of copy-to-reg nodes chained together with token chain
5790   // and flag operands which copy the outgoing args into the appropriate regs.
5791   SDValue InFlag;
5792   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
5793     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
5794                              RegsToPass[i].second, InFlag);
5795     InFlag = Chain.getValue(1);
5796   }
5797 
5798   // Set CR bit 6 to true if this is a vararg call with floating args passed in
5799   // registers.
5800   if (IsVarArg) {
5801     SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
5802     SDValue Ops[] = { Chain, InFlag };
5803 
5804     Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
5805                         dl, VTs, makeArrayRef(Ops, InFlag.getNode() ? 2 : 1));
5806 
5807     InFlag = Chain.getValue(1);
5808   }
5809 
5810   if (IsTailCall)
5811     PrepareTailCall(DAG, InFlag, Chain, dl, SPDiff, NumBytes, LROp, FPOp,
5812                     TailCallArguments);
5813 
5814   return FinishCall(CFlags, dl, DAG, RegsToPass, InFlag, Chain, CallSeqStart,
5815                     Callee, SPDiff, NumBytes, Ins, InVals, CB);
5816 }
5817 
5818 // Copy an argument into memory, being careful to do this outside the
5819 // call sequence for the call to which the argument belongs.
5820 SDValue PPCTargetLowering::createMemcpyOutsideCallSeq(
5821     SDValue Arg, SDValue PtrOff, SDValue CallSeqStart, ISD::ArgFlagsTy Flags,
5822     SelectionDAG &DAG, const SDLoc &dl) const {
5823   SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
5824                         CallSeqStart.getNode()->getOperand(0),
5825                         Flags, DAG, dl);
5826   // The MEMCPY must go outside the CALLSEQ_START..END.
5827   int64_t FrameSize = CallSeqStart.getConstantOperandVal(1);
5828   SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall, FrameSize, 0,
5829                                                  SDLoc(MemcpyCall));
5830   DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
5831                          NewCallSeqStart.getNode());
5832   return NewCallSeqStart;
5833 }
5834 
5835 SDValue PPCTargetLowering::LowerCall_64SVR4(
5836     SDValue Chain, SDValue Callee, CallFlags CFlags,
5837     const SmallVectorImpl<ISD::OutputArg> &Outs,
5838     const SmallVectorImpl<SDValue> &OutVals,
5839     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
5840     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
5841     const CallBase *CB) const {
5842   bool isELFv2ABI = Subtarget.isELFv2ABI();
5843   bool isLittleEndian = Subtarget.isLittleEndian();
5844   unsigned NumOps = Outs.size();
5845   bool IsSibCall = false;
5846   bool IsFastCall = CFlags.CallConv == CallingConv::Fast;
5847 
5848   EVT PtrVT = getPointerTy(DAG.getDataLayout());
5849   unsigned PtrByteSize = 8;
5850 
5851   MachineFunction &MF = DAG.getMachineFunction();
5852 
5853   if (CFlags.IsTailCall && !getTargetMachine().Options.GuaranteedTailCallOpt)
5854     IsSibCall = true;
5855 
5856   // Mark this function as potentially containing a function that contains a
5857   // tail call. As a consequence the frame pointer will be used for dynamicalloc
5858   // and restoring the callers stack pointer in this functions epilog. This is
5859   // done because by tail calling the called function might overwrite the value
5860   // in this function's (MF) stack pointer stack slot 0(SP).
5861   if (getTargetMachine().Options.GuaranteedTailCallOpt && IsFastCall)
5862     MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
5863 
5864   assert(!(IsFastCall && CFlags.IsVarArg) &&
5865          "fastcc not supported on varargs functions");
5866 
5867   // Count how many bytes are to be pushed on the stack, including the linkage
5868   // area, and parameter passing area.  On ELFv1, the linkage area is 48 bytes
5869   // reserved space for [SP][CR][LR][2 x unused][TOC]; on ELFv2, the linkage
5870   // area is 32 bytes reserved space for [SP][CR][LR][TOC].
5871   unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
5872   unsigned NumBytes = LinkageSize;
5873   unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
5874 
5875   static const MCPhysReg GPR[] = {
5876     PPC::X3, PPC::X4, PPC::X5, PPC::X6,
5877     PPC::X7, PPC::X8, PPC::X9, PPC::X10,
5878   };
5879   static const MCPhysReg VR[] = {
5880     PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
5881     PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
5882   };
5883 
5884   const unsigned NumGPRs = array_lengthof(GPR);
5885   const unsigned NumFPRs = useSoftFloat() ? 0 : 13;
5886   const unsigned NumVRs  = array_lengthof(VR);
5887 
5888   // On ELFv2, we can avoid allocating the parameter area if all the arguments
5889   // can be passed to the callee in registers.
5890   // For the fast calling convention, there is another check below.
5891   // Note: We should keep consistent with LowerFormalArguments_64SVR4()
5892   bool HasParameterArea = !isELFv2ABI || CFlags.IsVarArg || IsFastCall;
5893   if (!HasParameterArea) {
5894     unsigned ParamAreaSize = NumGPRs * PtrByteSize;
5895     unsigned AvailableFPRs = NumFPRs;
5896     unsigned AvailableVRs = NumVRs;
5897     unsigned NumBytesTmp = NumBytes;
5898     for (unsigned i = 0; i != NumOps; ++i) {
5899       if (Outs[i].Flags.isNest()) continue;
5900       if (CalculateStackSlotUsed(Outs[i].VT, Outs[i].ArgVT, Outs[i].Flags,
5901                                  PtrByteSize, LinkageSize, ParamAreaSize,
5902                                  NumBytesTmp, AvailableFPRs, AvailableVRs))
5903         HasParameterArea = true;
5904     }
5905   }
5906 
5907   // When using the fast calling convention, we don't provide backing for
5908   // arguments that will be in registers.
5909   unsigned NumGPRsUsed = 0, NumFPRsUsed = 0, NumVRsUsed = 0;
5910 
5911   // Avoid allocating parameter area for fastcc functions if all the arguments
5912   // can be passed in the registers.
5913   if (IsFastCall)
5914     HasParameterArea = false;
5915 
5916   // Add up all the space actually used.
5917   for (unsigned i = 0; i != NumOps; ++i) {
5918     ISD::ArgFlagsTy Flags = Outs[i].Flags;
5919     EVT ArgVT = Outs[i].VT;
5920     EVT OrigVT = Outs[i].ArgVT;
5921 
5922     if (Flags.isNest())
5923       continue;
5924 
5925     if (IsFastCall) {
5926       if (Flags.isByVal()) {
5927         NumGPRsUsed += (Flags.getByValSize()+7)/8;
5928         if (NumGPRsUsed > NumGPRs)
5929           HasParameterArea = true;
5930       } else {
5931         switch (ArgVT.getSimpleVT().SimpleTy) {
5932         default: llvm_unreachable("Unexpected ValueType for argument!");
5933         case MVT::i1:
5934         case MVT::i32:
5935         case MVT::i64:
5936           if (++NumGPRsUsed <= NumGPRs)
5937             continue;
5938           break;
5939         case MVT::v4i32:
5940         case MVT::v8i16:
5941         case MVT::v16i8:
5942         case MVT::v2f64:
5943         case MVT::v2i64:
5944         case MVT::v1i128:
5945         case MVT::f128:
5946           if (++NumVRsUsed <= NumVRs)
5947             continue;
5948           break;
5949         case MVT::v4f32:
5950           if (++NumVRsUsed <= NumVRs)
5951             continue;
5952           break;
5953         case MVT::f32:
5954         case MVT::f64:
5955           if (++NumFPRsUsed <= NumFPRs)
5956             continue;
5957           break;
5958         }
5959         HasParameterArea = true;
5960       }
5961     }
5962 
5963     /* Respect alignment of argument on the stack.  */
5964     auto Alignement =
5965         CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
5966     NumBytes = alignTo(NumBytes, Alignement);
5967 
5968     NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
5969     if (Flags.isInConsecutiveRegsLast())
5970       NumBytes = ((NumBytes + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
5971   }
5972 
5973   unsigned NumBytesActuallyUsed = NumBytes;
5974 
5975   // In the old ELFv1 ABI,
5976   // the prolog code of the callee may store up to 8 GPR argument registers to
5977   // the stack, allowing va_start to index over them in memory if its varargs.
5978   // Because we cannot tell if this is needed on the caller side, we have to
5979   // conservatively assume that it is needed.  As such, make sure we have at
5980   // least enough stack space for the caller to store the 8 GPRs.
5981   // In the ELFv2 ABI, we allocate the parameter area iff a callee
5982   // really requires memory operands, e.g. a vararg function.
5983   if (HasParameterArea)
5984     NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize);
5985   else
5986     NumBytes = LinkageSize;
5987 
5988   // Tail call needs the stack to be aligned.
5989   if (getTargetMachine().Options.GuaranteedTailCallOpt && IsFastCall)
5990     NumBytes = EnsureStackAlignment(Subtarget.getFrameLowering(), NumBytes);
5991 
5992   int SPDiff = 0;
5993 
5994   // Calculate by how many bytes the stack has to be adjusted in case of tail
5995   // call optimization.
5996   if (!IsSibCall)
5997     SPDiff = CalculateTailCallSPDiff(DAG, CFlags.IsTailCall, NumBytes);
5998 
5999   // To protect arguments on the stack from being clobbered in a tail call,
6000   // force all the loads to happen before doing any other lowering.
6001   if (CFlags.IsTailCall)
6002     Chain = DAG.getStackArgumentTokenFactor(Chain);
6003 
6004   // Adjust the stack pointer for the new arguments...
6005   // These operations are automatically eliminated by the prolog/epilog pass
6006   if (!IsSibCall)
6007     Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
6008   SDValue CallSeqStart = Chain;
6009 
6010   // Load the return address and frame pointer so it can be move somewhere else
6011   // later.
6012   SDValue LROp, FPOp;
6013   Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, dl);
6014 
6015   // Set up a copy of the stack pointer for use loading and storing any
6016   // arguments that may not fit in the registers available for argument
6017   // passing.
6018   SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
6019 
6020   // Figure out which arguments are going to go in registers, and which in
6021   // memory.  Also, if this is a vararg function, floating point operations
6022   // must be stored to our stack, and loaded into integer regs as well, if
6023   // any integer regs are available for argument passing.
6024   unsigned ArgOffset = LinkageSize;
6025 
6026   SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
6027   SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
6028 
6029   SmallVector<SDValue, 8> MemOpChains;
6030   for (unsigned i = 0; i != NumOps; ++i) {
6031     SDValue Arg = OutVals[i];
6032     ISD::ArgFlagsTy Flags = Outs[i].Flags;
6033     EVT ArgVT = Outs[i].VT;
6034     EVT OrigVT = Outs[i].ArgVT;
6035 
6036     // PtrOff will be used to store the current argument to the stack if a
6037     // register cannot be found for it.
6038     SDValue PtrOff;
6039 
6040     // We re-align the argument offset for each argument, except when using the
6041     // fast calling convention, when we need to make sure we do that only when
6042     // we'll actually use a stack slot.
6043     auto ComputePtrOff = [&]() {
6044       /* Respect alignment of argument on the stack.  */
6045       auto Alignment =
6046           CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
6047       ArgOffset = alignTo(ArgOffset, Alignment);
6048 
6049       PtrOff = DAG.getConstant(ArgOffset, dl, StackPtr.getValueType());
6050 
6051       PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
6052     };
6053 
6054     if (!IsFastCall) {
6055       ComputePtrOff();
6056 
6057       /* Compute GPR index associated with argument offset.  */
6058       GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
6059       GPR_idx = std::min(GPR_idx, NumGPRs);
6060     }
6061 
6062     // Promote integers to 64-bit values.
6063     if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) {
6064       // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
6065       unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
6066       Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
6067     }
6068 
6069     // FIXME memcpy is used way more than necessary.  Correctness first.
6070     // Note: "by value" is code for passing a structure by value, not
6071     // basic types.
6072     if (Flags.isByVal()) {
6073       // Note: Size includes alignment padding, so
6074       //   struct x { short a; char b; }
6075       // will have Size = 4.  With #pragma pack(1), it will have Size = 3.
6076       // These are the proper values we need for right-justifying the
6077       // aggregate in a parameter register.
6078       unsigned Size = Flags.getByValSize();
6079 
6080       // An empty aggregate parameter takes up no storage and no
6081       // registers.
6082       if (Size == 0)
6083         continue;
6084 
6085       if (IsFastCall)
6086         ComputePtrOff();
6087 
6088       // All aggregates smaller than 8 bytes must be passed right-justified.
6089       if (Size==1 || Size==2 || Size==4) {
6090         EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
6091         if (GPR_idx != NumGPRs) {
6092           SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
6093                                         MachinePointerInfo(), VT);
6094           MemOpChains.push_back(Load.getValue(1));
6095           RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
6096 
6097           ArgOffset += PtrByteSize;
6098           continue;
6099         }
6100       }
6101 
6102       if (GPR_idx == NumGPRs && Size < 8) {
6103         SDValue AddPtr = PtrOff;
6104         if (!isLittleEndian) {
6105           SDValue Const = DAG.getConstant(PtrByteSize - Size, dl,
6106                                           PtrOff.getValueType());
6107           AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
6108         }
6109         Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
6110                                                           CallSeqStart,
6111                                                           Flags, DAG, dl);
6112         ArgOffset += PtrByteSize;
6113         continue;
6114       }
6115       // Copy entire object into memory.  There are cases where gcc-generated
6116       // code assumes it is there, even if it could be put entirely into
6117       // registers.  (This is not what the doc says.)
6118 
6119       // FIXME: The above statement is likely due to a misunderstanding of the
6120       // documents.  All arguments must be copied into the parameter area BY
6121       // THE CALLEE in the event that the callee takes the address of any
6122       // formal argument.  That has not yet been implemented.  However, it is
6123       // reasonable to use the stack area as a staging area for the register
6124       // load.
6125 
6126       // Skip this for small aggregates, as we will use the same slot for a
6127       // right-justified copy, below.
6128       if (Size >= 8)
6129         Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
6130                                                           CallSeqStart,
6131                                                           Flags, DAG, dl);
6132 
6133       // When a register is available, pass a small aggregate right-justified.
6134       if (Size < 8 && GPR_idx != NumGPRs) {
6135         // The easiest way to get this right-justified in a register
6136         // is to copy the structure into the rightmost portion of a
6137         // local variable slot, then load the whole slot into the
6138         // register.
6139         // FIXME: The memcpy seems to produce pretty awful code for
6140         // small aggregates, particularly for packed ones.
6141         // FIXME: It would be preferable to use the slot in the
6142         // parameter save area instead of a new local variable.
6143         SDValue AddPtr = PtrOff;
6144         if (!isLittleEndian) {
6145           SDValue Const = DAG.getConstant(8 - Size, dl, PtrOff.getValueType());
6146           AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
6147         }
6148         Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
6149                                                           CallSeqStart,
6150                                                           Flags, DAG, dl);
6151 
6152         // Load the slot into the register.
6153         SDValue Load =
6154             DAG.getLoad(PtrVT, dl, Chain, PtrOff, MachinePointerInfo());
6155         MemOpChains.push_back(Load.getValue(1));
6156         RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
6157 
6158         // Done with this argument.
6159         ArgOffset += PtrByteSize;
6160         continue;
6161       }
6162 
6163       // For aggregates larger than PtrByteSize, copy the pieces of the
6164       // object that fit into registers from the parameter save area.
6165       for (unsigned j=0; j<Size; j+=PtrByteSize) {
6166         SDValue Const = DAG.getConstant(j, dl, PtrOff.getValueType());
6167         SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
6168         if (GPR_idx != NumGPRs) {
6169           SDValue Load =
6170               DAG.getLoad(PtrVT, dl, Chain, AddArg, MachinePointerInfo());
6171           MemOpChains.push_back(Load.getValue(1));
6172           RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
6173           ArgOffset += PtrByteSize;
6174         } else {
6175           ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
6176           break;
6177         }
6178       }
6179       continue;
6180     }
6181 
6182     switch (Arg.getSimpleValueType().SimpleTy) {
6183     default: llvm_unreachable("Unexpected ValueType for argument!");
6184     case MVT::i1:
6185     case MVT::i32:
6186     case MVT::i64:
6187       if (Flags.isNest()) {
6188         // The 'nest' parameter, if any, is passed in R11.
6189         RegsToPass.push_back(std::make_pair(PPC::X11, Arg));
6190         break;
6191       }
6192 
6193       // These can be scalar arguments or elements of an integer array type
6194       // passed directly.  Clang may use those instead of "byval" aggregate
6195       // types to avoid forcing arguments to memory unnecessarily.
6196       if (GPR_idx != NumGPRs) {
6197         RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
6198       } else {
6199         if (IsFastCall)
6200           ComputePtrOff();
6201 
6202         assert(HasParameterArea &&
6203                "Parameter area must exist to pass an argument in memory.");
6204         LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
6205                          true, CFlags.IsTailCall, false, MemOpChains,
6206                          TailCallArguments, dl);
6207         if (IsFastCall)
6208           ArgOffset += PtrByteSize;
6209       }
6210       if (!IsFastCall)
6211         ArgOffset += PtrByteSize;
6212       break;
6213     case MVT::f32:
6214     case MVT::f64: {
6215       // These can be scalar arguments or elements of a float array type
6216       // passed directly.  The latter are used to implement ELFv2 homogenous
6217       // float aggregates.
6218 
6219       // Named arguments go into FPRs first, and once they overflow, the
6220       // remaining arguments go into GPRs and then the parameter save area.
6221       // Unnamed arguments for vararg functions always go to GPRs and
6222       // then the parameter save area.  For now, put all arguments to vararg
6223       // routines always in both locations (FPR *and* GPR or stack slot).
6224       bool NeedGPROrStack = CFlags.IsVarArg || FPR_idx == NumFPRs;
6225       bool NeededLoad = false;
6226 
6227       // First load the argument into the next available FPR.
6228       if (FPR_idx != NumFPRs)
6229         RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
6230 
6231       // Next, load the argument into GPR or stack slot if needed.
6232       if (!NeedGPROrStack)
6233         ;
6234       else if (GPR_idx != NumGPRs && !IsFastCall) {
6235         // FIXME: We may want to re-enable this for CallingConv::Fast on the P8
6236         // once we support fp <-> gpr moves.
6237 
6238         // In the non-vararg case, this can only ever happen in the
6239         // presence of f32 array types, since otherwise we never run
6240         // out of FPRs before running out of GPRs.
6241         SDValue ArgVal;
6242 
6243         // Double values are always passed in a single GPR.
6244         if (Arg.getValueType() != MVT::f32) {
6245           ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
6246 
6247         // Non-array float values are extended and passed in a GPR.
6248         } else if (!Flags.isInConsecutiveRegs()) {
6249           ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
6250           ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
6251 
6252         // If we have an array of floats, we collect every odd element
6253         // together with its predecessor into one GPR.
6254         } else if (ArgOffset % PtrByteSize != 0) {
6255           SDValue Lo, Hi;
6256           Lo = DAG.getNode(ISD::BITCAST, dl, MVT::i32, OutVals[i - 1]);
6257           Hi = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
6258           if (!isLittleEndian)
6259             std::swap(Lo, Hi);
6260           ArgVal = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
6261 
6262         // The final element, if even, goes into the first half of a GPR.
6263         } else if (Flags.isInConsecutiveRegsLast()) {
6264           ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
6265           ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
6266           if (!isLittleEndian)
6267             ArgVal = DAG.getNode(ISD::SHL, dl, MVT::i64, ArgVal,
6268                                  DAG.getConstant(32, dl, MVT::i32));
6269 
6270         // Non-final even elements are skipped; they will be handled
6271         // together the with subsequent argument on the next go-around.
6272         } else
6273           ArgVal = SDValue();
6274 
6275         if (ArgVal.getNode())
6276           RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], ArgVal));
6277       } else {
6278         if (IsFastCall)
6279           ComputePtrOff();
6280 
6281         // Single-precision floating-point values are mapped to the
6282         // second (rightmost) word of the stack doubleword.
6283         if (Arg.getValueType() == MVT::f32 &&
6284             !isLittleEndian && !Flags.isInConsecutiveRegs()) {
6285           SDValue ConstFour = DAG.getConstant(4, dl, PtrOff.getValueType());
6286           PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
6287         }
6288 
6289         assert(HasParameterArea &&
6290                "Parameter area must exist to pass an argument in memory.");
6291         LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
6292                          true, CFlags.IsTailCall, false, MemOpChains,
6293                          TailCallArguments, dl);
6294 
6295         NeededLoad = true;
6296       }
6297       // When passing an array of floats, the array occupies consecutive
6298       // space in the argument area; only round up to the next doubleword
6299       // at the end of the array.  Otherwise, each float takes 8 bytes.
6300       if (!IsFastCall || NeededLoad) {
6301         ArgOffset += (Arg.getValueType() == MVT::f32 &&
6302                       Flags.isInConsecutiveRegs()) ? 4 : 8;
6303         if (Flags.isInConsecutiveRegsLast())
6304           ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
6305       }
6306       break;
6307     }
6308     case MVT::v4f32:
6309     case MVT::v4i32:
6310     case MVT::v8i16:
6311     case MVT::v16i8:
6312     case MVT::v2f64:
6313     case MVT::v2i64:
6314     case MVT::v1i128:
6315     case MVT::f128:
6316       // These can be scalar arguments or elements of a vector array type
6317       // passed directly.  The latter are used to implement ELFv2 homogenous
6318       // vector aggregates.
6319 
6320       // For a varargs call, named arguments go into VRs or on the stack as
6321       // usual; unnamed arguments always go to the stack or the corresponding
6322       // GPRs when within range.  For now, we always put the value in both
6323       // locations (or even all three).
6324       if (CFlags.IsVarArg) {
6325         assert(HasParameterArea &&
6326                "Parameter area must exist if we have a varargs call.");
6327         // We could elide this store in the case where the object fits
6328         // entirely in R registers.  Maybe later.
6329         SDValue Store =
6330             DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo());
6331         MemOpChains.push_back(Store);
6332         if (VR_idx != NumVRs) {
6333           SDValue Load =
6334               DAG.getLoad(MVT::v4f32, dl, Store, PtrOff, MachinePointerInfo());
6335           MemOpChains.push_back(Load.getValue(1));
6336           RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
6337         }
6338         ArgOffset += 16;
6339         for (unsigned i=0; i<16; i+=PtrByteSize) {
6340           if (GPR_idx == NumGPRs)
6341             break;
6342           SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
6343                                    DAG.getConstant(i, dl, PtrVT));
6344           SDValue Load =
6345               DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo());
6346           MemOpChains.push_back(Load.getValue(1));
6347           RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
6348         }
6349         break;
6350       }
6351 
6352       // Non-varargs Altivec params go into VRs or on the stack.
6353       if (VR_idx != NumVRs) {
6354         RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
6355       } else {
6356         if (IsFastCall)
6357           ComputePtrOff();
6358 
6359         assert(HasParameterArea &&
6360                "Parameter area must exist to pass an argument in memory.");
6361         LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
6362                          true, CFlags.IsTailCall, true, MemOpChains,
6363                          TailCallArguments, dl);
6364         if (IsFastCall)
6365           ArgOffset += 16;
6366       }
6367 
6368       if (!IsFastCall)
6369         ArgOffset += 16;
6370       break;
6371     }
6372   }
6373 
6374   assert((!HasParameterArea || NumBytesActuallyUsed == ArgOffset) &&
6375          "mismatch in size of parameter area");
6376   (void)NumBytesActuallyUsed;
6377 
6378   if (!MemOpChains.empty())
6379     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
6380 
6381   // Check if this is an indirect call (MTCTR/BCTRL).
6382   // See prepareDescriptorIndirectCall and buildCallOperands for more
6383   // information about calls through function pointers in the 64-bit SVR4 ABI.
6384   if (CFlags.IsIndirect) {
6385     // For 64-bit ELFv2 ABI with PCRel, do not save the TOC of the
6386     // caller in the TOC save area.
6387     if (isTOCSaveRestoreRequired(Subtarget)) {
6388       assert(!CFlags.IsTailCall && "Indirect tails calls not supported");
6389       // Load r2 into a virtual register and store it to the TOC save area.
6390       setUsesTOCBasePtr(DAG);
6391       SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
6392       // TOC save area offset.
6393       unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset();
6394       SDValue PtrOff = DAG.getIntPtrConstant(TOCSaveOffset, dl);
6395       SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
6396       Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr,
6397                            MachinePointerInfo::getStack(
6398                                DAG.getMachineFunction(), TOCSaveOffset));
6399     }
6400     // In the ELFv2 ABI, R12 must contain the address of an indirect callee.
6401     // This does not mean the MTCTR instruction must use R12; it's easier
6402     // to model this as an extra parameter, so do that.
6403     if (isELFv2ABI && !CFlags.IsPatchPoint)
6404       RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
6405   }
6406 
6407   // Build a sequence of copy-to-reg nodes chained together with token chain
6408   // and flag operands which copy the outgoing args into the appropriate regs.
6409   SDValue InFlag;
6410   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
6411     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
6412                              RegsToPass[i].second, InFlag);
6413     InFlag = Chain.getValue(1);
6414   }
6415 
6416   if (CFlags.IsTailCall && !IsSibCall)
6417     PrepareTailCall(DAG, InFlag, Chain, dl, SPDiff, NumBytes, LROp, FPOp,
6418                     TailCallArguments);
6419 
6420   return FinishCall(CFlags, dl, DAG, RegsToPass, InFlag, Chain, CallSeqStart,
6421                     Callee, SPDiff, NumBytes, Ins, InVals, CB);
6422 }
6423 
6424 // Returns true when the shadow of a general purpose argument register
6425 // in the parameter save area is aligned to at least 'RequiredAlign'.
6426 static bool isGPRShadowAligned(MCPhysReg Reg, Align RequiredAlign) {
6427   assert(RequiredAlign.value() <= 16 &&
6428          "Required alignment greater than stack alignment.");
6429   switch (Reg) {
6430   default:
6431     report_fatal_error("called on invalid register.");
6432   case PPC::R5:
6433   case PPC::R9:
6434   case PPC::X3:
6435   case PPC::X5:
6436   case PPC::X7:
6437   case PPC::X9:
6438     // These registers are 16 byte aligned which is the most strict aligment
6439     // we can support.
6440     return true;
6441   case PPC::R3:
6442   case PPC::R7:
6443   case PPC::X4:
6444   case PPC::X6:
6445   case PPC::X8:
6446   case PPC::X10:
6447     // The shadow of these registers in the PSA is 8 byte aligned.
6448     return RequiredAlign <= 8;
6449   case PPC::R4:
6450   case PPC::R6:
6451   case PPC::R8:
6452   case PPC::R10:
6453     return RequiredAlign <= 4;
6454   }
6455 }
6456 
6457 static bool CC_AIX(unsigned ValNo, MVT ValVT, MVT LocVT,
6458                    CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
6459                    CCState &S) {
6460   AIXCCState &State = static_cast<AIXCCState &>(S);
6461   const PPCSubtarget &Subtarget = static_cast<const PPCSubtarget &>(
6462       State.getMachineFunction().getSubtarget());
6463   const bool IsPPC64 = Subtarget.isPPC64();
6464   const Align PtrAlign = IsPPC64 ? Align(8) : Align(4);
6465   const MVT RegVT = IsPPC64 ? MVT::i64 : MVT::i32;
6466 
6467   if (ValVT == MVT::f128)
6468     report_fatal_error("f128 is unimplemented on AIX.");
6469 
6470   if (ArgFlags.isNest())
6471     report_fatal_error("Nest arguments are unimplemented.");
6472 
6473   static const MCPhysReg GPR_32[] = {// 32-bit registers.
6474                                      PPC::R3, PPC::R4, PPC::R5, PPC::R6,
6475                                      PPC::R7, PPC::R8, PPC::R9, PPC::R10};
6476   static const MCPhysReg GPR_64[] = {// 64-bit registers.
6477                                      PPC::X3, PPC::X4, PPC::X5, PPC::X6,
6478                                      PPC::X7, PPC::X8, PPC::X9, PPC::X10};
6479 
6480   static const MCPhysReg VR[] = {// Vector registers.
6481                                  PPC::V2,  PPC::V3,  PPC::V4,  PPC::V5,
6482                                  PPC::V6,  PPC::V7,  PPC::V8,  PPC::V9,
6483                                  PPC::V10, PPC::V11, PPC::V12, PPC::V13};
6484 
6485   if (ArgFlags.isByVal()) {
6486     if (ArgFlags.getNonZeroByValAlign() > PtrAlign)
6487       report_fatal_error("Pass-by-value arguments with alignment greater than "
6488                          "register width are not supported.");
6489 
6490     const unsigned ByValSize = ArgFlags.getByValSize();
6491 
6492     // An empty aggregate parameter takes up no storage and no registers,
6493     // but needs a MemLoc for a stack slot for the formal arguments side.
6494     if (ByValSize == 0) {
6495       State.addLoc(CCValAssign::getMem(ValNo, MVT::INVALID_SIMPLE_VALUE_TYPE,
6496                                        State.getNextStackOffset(), RegVT,
6497                                        LocInfo));
6498       return false;
6499     }
6500 
6501     const unsigned StackSize = alignTo(ByValSize, PtrAlign);
6502     unsigned Offset = State.AllocateStack(StackSize, PtrAlign);
6503     for (const unsigned E = Offset + StackSize; Offset < E;
6504          Offset += PtrAlign.value()) {
6505       if (unsigned Reg = State.AllocateReg(IsPPC64 ? GPR_64 : GPR_32))
6506         State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, RegVT, LocInfo));
6507       else {
6508         State.addLoc(CCValAssign::getMem(ValNo, MVT::INVALID_SIMPLE_VALUE_TYPE,
6509                                          Offset, MVT::INVALID_SIMPLE_VALUE_TYPE,
6510                                          LocInfo));
6511         break;
6512       }
6513     }
6514     return false;
6515   }
6516 
6517   // Arguments always reserve parameter save area.
6518   switch (ValVT.SimpleTy) {
6519   default:
6520     report_fatal_error("Unhandled value type for argument.");
6521   case MVT::i64:
6522     // i64 arguments should have been split to i32 for PPC32.
6523     assert(IsPPC64 && "PPC32 should have split i64 values.");
6524     LLVM_FALLTHROUGH;
6525   case MVT::i1:
6526   case MVT::i32: {
6527     const unsigned Offset = State.AllocateStack(PtrAlign.value(), PtrAlign);
6528     // AIX integer arguments are always passed in register width.
6529     if (ValVT.getFixedSizeInBits() < RegVT.getFixedSizeInBits())
6530       LocInfo = ArgFlags.isSExt() ? CCValAssign::LocInfo::SExt
6531                                   : CCValAssign::LocInfo::ZExt;
6532     if (unsigned Reg = State.AllocateReg(IsPPC64 ? GPR_64 : GPR_32))
6533       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, RegVT, LocInfo));
6534     else
6535       State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, RegVT, LocInfo));
6536 
6537     return false;
6538   }
6539   case MVT::f32:
6540   case MVT::f64: {
6541     // Parameter save area (PSA) is reserved even if the float passes in fpr.
6542     const unsigned StoreSize = LocVT.getStoreSize();
6543     // Floats are always 4-byte aligned in the PSA on AIX.
6544     // This includes f64 in 64-bit mode for ABI compatibility.
6545     const unsigned Offset =
6546         State.AllocateStack(IsPPC64 ? 8 : StoreSize, Align(4));
6547     unsigned FReg = State.AllocateReg(FPR);
6548     if (FReg)
6549       State.addLoc(CCValAssign::getReg(ValNo, ValVT, FReg, LocVT, LocInfo));
6550 
6551     // Reserve and initialize GPRs or initialize the PSA as required.
6552     for (unsigned I = 0; I < StoreSize; I += PtrAlign.value()) {
6553       if (unsigned Reg = State.AllocateReg(IsPPC64 ? GPR_64 : GPR_32)) {
6554         assert(FReg && "An FPR should be available when a GPR is reserved.");
6555         if (State.isVarArg()) {
6556           // Successfully reserved GPRs are only initialized for vararg calls.
6557           // Custom handling is required for:
6558           //   f64 in PPC32 needs to be split into 2 GPRs.
6559           //   f32 in PPC64 needs to occupy only lower 32 bits of 64-bit GPR.
6560           State.addLoc(
6561               CCValAssign::getCustomReg(ValNo, ValVT, Reg, RegVT, LocInfo));
6562         }
6563       } else {
6564         // If there are insufficient GPRs, the PSA needs to be initialized.
6565         // Initialization occurs even if an FPR was initialized for
6566         // compatibility with the AIX XL compiler. The full memory for the
6567         // argument will be initialized even if a prior word is saved in GPR.
6568         // A custom memLoc is used when the argument also passes in FPR so
6569         // that the callee handling can skip over it easily.
6570         State.addLoc(
6571             FReg ? CCValAssign::getCustomMem(ValNo, ValVT, Offset, LocVT,
6572                                              LocInfo)
6573                  : CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
6574         break;
6575       }
6576     }
6577 
6578     return false;
6579   }
6580   case MVT::v4f32:
6581   case MVT::v4i32:
6582   case MVT::v8i16:
6583   case MVT::v16i8:
6584   case MVT::v2i64:
6585   case MVT::v2f64:
6586   case MVT::v1i128: {
6587     const unsigned VecSize = 16;
6588     const Align VecAlign(VecSize);
6589 
6590     if (!State.isVarArg()) {
6591       // If there are vector registers remaining we don't consume any stack
6592       // space.
6593       if (unsigned VReg = State.AllocateReg(VR)) {
6594         State.addLoc(CCValAssign::getReg(ValNo, ValVT, VReg, LocVT, LocInfo));
6595         return false;
6596       }
6597       // Vectors passed on the stack do not shadow GPRs or FPRs even though they
6598       // might be allocated in the portion of the PSA that is shadowed by the
6599       // GPRs.
6600       const unsigned Offset = State.AllocateStack(VecSize, VecAlign);
6601       State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
6602       return false;
6603     }
6604 
6605     const unsigned PtrSize = IsPPC64 ? 8 : 4;
6606     ArrayRef<MCPhysReg> GPRs = IsPPC64 ? GPR_64 : GPR_32;
6607 
6608     unsigned NextRegIndex = State.getFirstUnallocated(GPRs);
6609     // Burn any underaligned registers and their shadowed stack space until
6610     // we reach the required alignment.
6611     while (NextRegIndex != GPRs.size() &&
6612            !isGPRShadowAligned(GPRs[NextRegIndex], VecAlign)) {
6613       // Shadow allocate register and its stack shadow.
6614       unsigned Reg = State.AllocateReg(GPRs);
6615       State.AllocateStack(PtrSize, PtrAlign);
6616       assert(Reg && "Allocating register unexpectedly failed.");
6617       (void)Reg;
6618       NextRegIndex = State.getFirstUnallocated(GPRs);
6619     }
6620 
6621     // Vectors that are passed as fixed arguments are handled differently.
6622     // They are passed in VRs if any are available (unlike arguments passed
6623     // through ellipses) and shadow GPRs (unlike arguments to non-vaarg
6624     // functions)
6625     if (State.isFixed(ValNo)) {
6626       if (unsigned VReg = State.AllocateReg(VR)) {
6627         State.addLoc(CCValAssign::getReg(ValNo, ValVT, VReg, LocVT, LocInfo));
6628         // Shadow allocate GPRs and stack space even though we pass in a VR.
6629         for (unsigned I = 0; I != VecSize; I += PtrSize)
6630           State.AllocateReg(GPRs);
6631         State.AllocateStack(VecSize, VecAlign);
6632         return false;
6633       }
6634       // No vector registers remain so pass on the stack.
6635       const unsigned Offset = State.AllocateStack(VecSize, VecAlign);
6636       State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
6637       return false;
6638     }
6639 
6640     // If all GPRS are consumed then we pass the argument fully on the stack.
6641     if (NextRegIndex == GPRs.size()) {
6642       const unsigned Offset = State.AllocateStack(VecSize, VecAlign);
6643       State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
6644       return false;
6645     }
6646 
6647     // Corner case for 32-bit codegen. We have 2 registers to pass the first
6648     // half of the argument, and then need to pass the remaining half on the
6649     // stack.
6650     if (GPRs[NextRegIndex] == PPC::R9) {
6651       const unsigned Offset = State.AllocateStack(VecSize, VecAlign);
6652       State.addLoc(
6653           CCValAssign::getCustomMem(ValNo, ValVT, Offset, LocVT, LocInfo));
6654 
6655       const unsigned FirstReg = State.AllocateReg(PPC::R9);
6656       const unsigned SecondReg = State.AllocateReg(PPC::R10);
6657       assert(FirstReg && SecondReg &&
6658              "Allocating R9 or R10 unexpectedly failed.");
6659       State.addLoc(
6660           CCValAssign::getCustomReg(ValNo, ValVT, FirstReg, RegVT, LocInfo));
6661       State.addLoc(
6662           CCValAssign::getCustomReg(ValNo, ValVT, SecondReg, RegVT, LocInfo));
6663       return false;
6664     }
6665 
6666     // We have enough GPRs to fully pass the vector argument, and we have
6667     // already consumed any underaligned registers. Start with the custom
6668     // MemLoc and then the custom RegLocs.
6669     const unsigned Offset = State.AllocateStack(VecSize, VecAlign);
6670     State.addLoc(
6671         CCValAssign::getCustomMem(ValNo, ValVT, Offset, LocVT, LocInfo));
6672     for (unsigned I = 0; I != VecSize; I += PtrSize) {
6673       const unsigned Reg = State.AllocateReg(GPRs);
6674       assert(Reg && "Failed to allocated register for vararg vector argument");
6675       State.addLoc(
6676           CCValAssign::getCustomReg(ValNo, ValVT, Reg, RegVT, LocInfo));
6677     }
6678     return false;
6679   }
6680   }
6681   return true;
6682 }
6683 
6684 static const TargetRegisterClass *getRegClassForSVT(MVT::SimpleValueType SVT,
6685                                                     bool IsPPC64) {
6686   assert((IsPPC64 || SVT != MVT::i64) &&
6687          "i64 should have been split for 32-bit codegen.");
6688 
6689   switch (SVT) {
6690   default:
6691     report_fatal_error("Unexpected value type for formal argument");
6692   case MVT::i1:
6693   case MVT::i32:
6694   case MVT::i64:
6695     return IsPPC64 ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6696   case MVT::f32:
6697     return &PPC::F4RCRegClass;
6698   case MVT::f64:
6699     return &PPC::F8RCRegClass;
6700   case MVT::v4f32:
6701   case MVT::v4i32:
6702   case MVT::v8i16:
6703   case MVT::v16i8:
6704   case MVT::v2i64:
6705   case MVT::v2f64:
6706   case MVT::v1i128:
6707     return &PPC::VRRCRegClass;
6708   }
6709 }
6710 
6711 static SDValue truncateScalarIntegerArg(ISD::ArgFlagsTy Flags, EVT ValVT,
6712                                         SelectionDAG &DAG, SDValue ArgValue,
6713                                         MVT LocVT, const SDLoc &dl) {
6714   assert(ValVT.isScalarInteger() && LocVT.isScalarInteger());
6715   assert(ValVT.getFixedSizeInBits() < LocVT.getFixedSizeInBits());
6716 
6717   if (Flags.isSExt())
6718     ArgValue = DAG.getNode(ISD::AssertSext, dl, LocVT, ArgValue,
6719                            DAG.getValueType(ValVT));
6720   else if (Flags.isZExt())
6721     ArgValue = DAG.getNode(ISD::AssertZext, dl, LocVT, ArgValue,
6722                            DAG.getValueType(ValVT));
6723 
6724   return DAG.getNode(ISD::TRUNCATE, dl, ValVT, ArgValue);
6725 }
6726 
6727 static unsigned mapArgRegToOffsetAIX(unsigned Reg, const PPCFrameLowering *FL) {
6728   const unsigned LASize = FL->getLinkageSize();
6729 
6730   if (PPC::GPRCRegClass.contains(Reg)) {
6731     assert(Reg >= PPC::R3 && Reg <= PPC::R10 &&
6732            "Reg must be a valid argument register!");
6733     return LASize + 4 * (Reg - PPC::R3);
6734   }
6735 
6736   if (PPC::G8RCRegClass.contains(Reg)) {
6737     assert(Reg >= PPC::X3 && Reg <= PPC::X10 &&
6738            "Reg must be a valid argument register!");
6739     return LASize + 8 * (Reg - PPC::X3);
6740   }
6741 
6742   llvm_unreachable("Only general purpose registers expected.");
6743 }
6744 
6745 //   AIX ABI Stack Frame Layout:
6746 //
6747 //   Low Memory +--------------------------------------------+
6748 //   SP   +---> | Back chain                                 | ---+
6749 //        |     +--------------------------------------------+    |
6750 //        |     | Saved Condition Register                   |    |
6751 //        |     +--------------------------------------------+    |
6752 //        |     | Saved Linkage Register                     |    |
6753 //        |     +--------------------------------------------+    | Linkage Area
6754 //        |     | Reserved for compilers                     |    |
6755 //        |     +--------------------------------------------+    |
6756 //        |     | Reserved for binders                       |    |
6757 //        |     +--------------------------------------------+    |
6758 //        |     | Saved TOC pointer                          | ---+
6759 //        |     +--------------------------------------------+
6760 //        |     | Parameter save area                        |
6761 //        |     +--------------------------------------------+
6762 //        |     | Alloca space                               |
6763 //        |     +--------------------------------------------+
6764 //        |     | Local variable space                       |
6765 //        |     +--------------------------------------------+
6766 //        |     | Float/int conversion temporary             |
6767 //        |     +--------------------------------------------+
6768 //        |     | Save area for AltiVec registers            |
6769 //        |     +--------------------------------------------+
6770 //        |     | AltiVec alignment padding                  |
6771 //        |     +--------------------------------------------+
6772 //        |     | Save area for VRSAVE register              |
6773 //        |     +--------------------------------------------+
6774 //        |     | Save area for General Purpose registers    |
6775 //        |     +--------------------------------------------+
6776 //        |     | Save area for Floating Point registers     |
6777 //        |     +--------------------------------------------+
6778 //        +---- | Back chain                                 |
6779 // High Memory  +--------------------------------------------+
6780 //
6781 //  Specifications:
6782 //  AIX 7.2 Assembler Language Reference
6783 //  Subroutine linkage convention
6784 
6785 SDValue PPCTargetLowering::LowerFormalArguments_AIX(
6786     SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
6787     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
6788     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
6789 
6790   assert((CallConv == CallingConv::C || CallConv == CallingConv::Cold ||
6791           CallConv == CallingConv::Fast) &&
6792          "Unexpected calling convention!");
6793 
6794   if (getTargetMachine().Options.GuaranteedTailCallOpt)
6795     report_fatal_error("Tail call support is unimplemented on AIX.");
6796 
6797   if (useSoftFloat())
6798     report_fatal_error("Soft float support is unimplemented on AIX.");
6799 
6800   const PPCSubtarget &Subtarget =
6801       static_cast<const PPCSubtarget &>(DAG.getSubtarget());
6802 
6803   const bool IsPPC64 = Subtarget.isPPC64();
6804   const unsigned PtrByteSize = IsPPC64 ? 8 : 4;
6805 
6806   // Assign locations to all of the incoming arguments.
6807   SmallVector<CCValAssign, 16> ArgLocs;
6808   MachineFunction &MF = DAG.getMachineFunction();
6809   MachineFrameInfo &MFI = MF.getFrameInfo();
6810   PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
6811   AIXCCState CCInfo(CallConv, isVarArg, MF, ArgLocs, *DAG.getContext());
6812 
6813   const EVT PtrVT = getPointerTy(MF.getDataLayout());
6814   // Reserve space for the linkage area on the stack.
6815   const unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
6816   CCInfo.AllocateStack(LinkageSize, Align(PtrByteSize));
6817   CCInfo.AnalyzeFormalArguments(Ins, CC_AIX);
6818 
6819   SmallVector<SDValue, 8> MemOps;
6820 
6821   for (size_t I = 0, End = ArgLocs.size(); I != End; /* No increment here */) {
6822     CCValAssign &VA = ArgLocs[I++];
6823     MVT LocVT = VA.getLocVT();
6824     MVT ValVT = VA.getValVT();
6825     ISD::ArgFlagsTy Flags = Ins[VA.getValNo()].Flags;
6826     // For compatibility with the AIX XL compiler, the float args in the
6827     // parameter save area are initialized even if the argument is available
6828     // in register.  The caller is required to initialize both the register
6829     // and memory, however, the callee can choose to expect it in either.
6830     // The memloc is dismissed here because the argument is retrieved from
6831     // the register.
6832     if (VA.isMemLoc() && VA.needsCustom() && ValVT.isFloatingPoint())
6833       continue;
6834 
6835     auto HandleMemLoc = [&]() {
6836       const unsigned LocSize = LocVT.getStoreSize();
6837       const unsigned ValSize = ValVT.getStoreSize();
6838       assert((ValSize <= LocSize) &&
6839              "Object size is larger than size of MemLoc");
6840       int CurArgOffset = VA.getLocMemOffset();
6841       // Objects are right-justified because AIX is big-endian.
6842       if (LocSize > ValSize)
6843         CurArgOffset += LocSize - ValSize;
6844       // Potential tail calls could cause overwriting of argument stack slots.
6845       const bool IsImmutable =
6846           !(getTargetMachine().Options.GuaranteedTailCallOpt &&
6847             (CallConv == CallingConv::Fast));
6848       int FI = MFI.CreateFixedObject(ValSize, CurArgOffset, IsImmutable);
6849       SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
6850       SDValue ArgValue =
6851           DAG.getLoad(ValVT, dl, Chain, FIN, MachinePointerInfo());
6852       InVals.push_back(ArgValue);
6853     };
6854 
6855     // Vector arguments to VaArg functions are passed both on the stack, and
6856     // in any available GPRs. Load the value from the stack and add the GPRs
6857     // as live ins.
6858     if (VA.isMemLoc() && VA.needsCustom()) {
6859       assert(ValVT.isVector() && "Unexpected Custom MemLoc type.");
6860       assert(isVarArg && "Only use custom memloc for vararg.");
6861       // ValNo of the custom MemLoc, so we can compare it to the ValNo of the
6862       // matching custom RegLocs.
6863       const unsigned OriginalValNo = VA.getValNo();
6864       (void)OriginalValNo;
6865 
6866       auto HandleCustomVecRegLoc = [&]() {
6867         assert(I != End && ArgLocs[I].isRegLoc() && ArgLocs[I].needsCustom() &&
6868                "Missing custom RegLoc.");
6869         VA = ArgLocs[I++];
6870         assert(VA.getValVT().isVector() &&
6871                "Unexpected Val type for custom RegLoc.");
6872         assert(VA.getValNo() == OriginalValNo &&
6873                "ValNo mismatch between custom MemLoc and RegLoc.");
6874         MVT::SimpleValueType SVT = VA.getLocVT().SimpleTy;
6875         MF.addLiveIn(VA.getLocReg(), getRegClassForSVT(SVT, IsPPC64));
6876       };
6877 
6878       HandleMemLoc();
6879       // In 64-bit there will be exactly 2 custom RegLocs that follow, and in
6880       // in 32-bit there will be 2 custom RegLocs if we are passing in R9 and
6881       // R10.
6882       HandleCustomVecRegLoc();
6883       HandleCustomVecRegLoc();
6884 
6885       // If we are targeting 32-bit, there might be 2 extra custom RegLocs if
6886       // we passed the vector in R5, R6, R7 and R8.
6887       if (I != End && ArgLocs[I].isRegLoc() && ArgLocs[I].needsCustom()) {
6888         assert(!IsPPC64 &&
6889                "Only 2 custom RegLocs expected for 64-bit codegen.");
6890         HandleCustomVecRegLoc();
6891         HandleCustomVecRegLoc();
6892       }
6893 
6894       continue;
6895     }
6896 
6897     if (VA.isRegLoc()) {
6898       if (VA.getValVT().isScalarInteger())
6899         FuncInfo->appendParameterType(PPCFunctionInfo::FixedType);
6900       else if (VA.getValVT().isFloatingPoint() && !VA.getValVT().isVector())
6901         FuncInfo->appendParameterType(VA.getValVT().SimpleTy == MVT::f32
6902                                           ? PPCFunctionInfo::ShortFloatPoint
6903                                           : PPCFunctionInfo::LongFloatPoint);
6904     }
6905 
6906     if (Flags.isByVal() && VA.isMemLoc()) {
6907       const unsigned Size =
6908           alignTo(Flags.getByValSize() ? Flags.getByValSize() : PtrByteSize,
6909                   PtrByteSize);
6910       const int FI = MF.getFrameInfo().CreateFixedObject(
6911           Size, VA.getLocMemOffset(), /* IsImmutable */ false,
6912           /* IsAliased */ true);
6913       SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
6914       InVals.push_back(FIN);
6915 
6916       continue;
6917     }
6918 
6919     if (Flags.isByVal()) {
6920       assert(VA.isRegLoc() && "MemLocs should already be handled.");
6921 
6922       const MCPhysReg ArgReg = VA.getLocReg();
6923       const PPCFrameLowering *FL = Subtarget.getFrameLowering();
6924 
6925       if (Flags.getNonZeroByValAlign() > PtrByteSize)
6926         report_fatal_error("Over aligned byvals not supported yet.");
6927 
6928       const unsigned StackSize = alignTo(Flags.getByValSize(), PtrByteSize);
6929       const int FI = MF.getFrameInfo().CreateFixedObject(
6930           StackSize, mapArgRegToOffsetAIX(ArgReg, FL), /* IsImmutable */ false,
6931           /* IsAliased */ true);
6932       SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
6933       InVals.push_back(FIN);
6934 
6935       // Add live ins for all the RegLocs for the same ByVal.
6936       const TargetRegisterClass *RegClass =
6937           IsPPC64 ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6938 
6939       auto HandleRegLoc = [&, RegClass, LocVT](const MCPhysReg PhysReg,
6940                                                unsigned Offset) {
6941         const unsigned VReg = MF.addLiveIn(PhysReg, RegClass);
6942         // Since the callers side has left justified the aggregate in the
6943         // register, we can simply store the entire register into the stack
6944         // slot.
6945         SDValue CopyFrom = DAG.getCopyFromReg(Chain, dl, VReg, LocVT);
6946         // The store to the fixedstack object is needed becuase accessing a
6947         // field of the ByVal will use a gep and load. Ideally we will optimize
6948         // to extracting the value from the register directly, and elide the
6949         // stores when the arguments address is not taken, but that will need to
6950         // be future work.
6951         SDValue Store = DAG.getStore(
6952             CopyFrom.getValue(1), dl, CopyFrom,
6953             DAG.getObjectPtrOffset(dl, FIN, TypeSize::Fixed(Offset)),
6954             MachinePointerInfo::getFixedStack(MF, FI, Offset));
6955 
6956         MemOps.push_back(Store);
6957       };
6958 
6959       unsigned Offset = 0;
6960       HandleRegLoc(VA.getLocReg(), Offset);
6961       Offset += PtrByteSize;
6962       for (; Offset != StackSize && ArgLocs[I].isRegLoc();
6963            Offset += PtrByteSize) {
6964         assert(ArgLocs[I].getValNo() == VA.getValNo() &&
6965                "RegLocs should be for ByVal argument.");
6966 
6967         const CCValAssign RL = ArgLocs[I++];
6968         HandleRegLoc(RL.getLocReg(), Offset);
6969         FuncInfo->appendParameterType(PPCFunctionInfo::FixedType);
6970       }
6971 
6972       if (Offset != StackSize) {
6973         assert(ArgLocs[I].getValNo() == VA.getValNo() &&
6974                "Expected MemLoc for remaining bytes.");
6975         assert(ArgLocs[I].isMemLoc() && "Expected MemLoc for remaining bytes.");
6976         // Consume the MemLoc.The InVal has already been emitted, so nothing
6977         // more needs to be done.
6978         ++I;
6979       }
6980 
6981       continue;
6982     }
6983 
6984     if (VA.isRegLoc() && !VA.needsCustom()) {
6985       MVT::SimpleValueType SVT = ValVT.SimpleTy;
6986       unsigned VReg =
6987           MF.addLiveIn(VA.getLocReg(), getRegClassForSVT(SVT, IsPPC64));
6988       SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, LocVT);
6989       if (ValVT.isScalarInteger() &&
6990           (ValVT.getFixedSizeInBits() < LocVT.getFixedSizeInBits())) {
6991         ArgValue =
6992             truncateScalarIntegerArg(Flags, ValVT, DAG, ArgValue, LocVT, dl);
6993       }
6994       InVals.push_back(ArgValue);
6995       continue;
6996     }
6997     if (VA.isMemLoc()) {
6998       HandleMemLoc();
6999       continue;
7000     }
7001   }
7002 
7003   // On AIX a minimum of 8 words is saved to the parameter save area.
7004   const unsigned MinParameterSaveArea = 8 * PtrByteSize;
7005   // Area that is at least reserved in the caller of this function.
7006   unsigned CallerReservedArea =
7007       std::max(CCInfo.getNextStackOffset(), LinkageSize + MinParameterSaveArea);
7008 
7009   // Set the size that is at least reserved in caller of this function. Tail
7010   // call optimized function's reserved stack space needs to be aligned so
7011   // that taking the difference between two stack areas will result in an
7012   // aligned stack.
7013   CallerReservedArea =
7014       EnsureStackAlignment(Subtarget.getFrameLowering(), CallerReservedArea);
7015   FuncInfo->setMinReservedArea(CallerReservedArea);
7016 
7017   if (isVarArg) {
7018     FuncInfo->setVarArgsFrameIndex(
7019         MFI.CreateFixedObject(PtrByteSize, CCInfo.getNextStackOffset(), true));
7020     SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
7021 
7022     static const MCPhysReg GPR_32[] = {PPC::R3, PPC::R4, PPC::R5, PPC::R6,
7023                                        PPC::R7, PPC::R8, PPC::R9, PPC::R10};
7024 
7025     static const MCPhysReg GPR_64[] = {PPC::X3, PPC::X4, PPC::X5, PPC::X6,
7026                                        PPC::X7, PPC::X8, PPC::X9, PPC::X10};
7027     const unsigned NumGPArgRegs = array_lengthof(IsPPC64 ? GPR_64 : GPR_32);
7028 
7029     // The fixed integer arguments of a variadic function are stored to the
7030     // VarArgsFrameIndex on the stack so that they may be loaded by
7031     // dereferencing the result of va_next.
7032     for (unsigned GPRIndex =
7033              (CCInfo.getNextStackOffset() - LinkageSize) / PtrByteSize;
7034          GPRIndex < NumGPArgRegs; ++GPRIndex) {
7035 
7036       const unsigned VReg =
7037           IsPPC64 ? MF.addLiveIn(GPR_64[GPRIndex], &PPC::G8RCRegClass)
7038                   : MF.addLiveIn(GPR_32[GPRIndex], &PPC::GPRCRegClass);
7039 
7040       SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
7041       SDValue Store =
7042           DAG.getStore(Val.getValue(1), dl, Val, FIN, MachinePointerInfo());
7043       MemOps.push_back(Store);
7044       // Increment the address for the next argument to store.
7045       SDValue PtrOff = DAG.getConstant(PtrByteSize, dl, PtrVT);
7046       FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
7047     }
7048   }
7049 
7050   if (!MemOps.empty())
7051     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
7052 
7053   return Chain;
7054 }
7055 
7056 SDValue PPCTargetLowering::LowerCall_AIX(
7057     SDValue Chain, SDValue Callee, CallFlags CFlags,
7058     const SmallVectorImpl<ISD::OutputArg> &Outs,
7059     const SmallVectorImpl<SDValue> &OutVals,
7060     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
7061     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
7062     const CallBase *CB) const {
7063   // See PPCTargetLowering::LowerFormalArguments_AIX() for a description of the
7064   // AIX ABI stack frame layout.
7065 
7066   assert((CFlags.CallConv == CallingConv::C ||
7067           CFlags.CallConv == CallingConv::Cold ||
7068           CFlags.CallConv == CallingConv::Fast) &&
7069          "Unexpected calling convention!");
7070 
7071   if (CFlags.IsPatchPoint)
7072     report_fatal_error("This call type is unimplemented on AIX.");
7073 
7074   const PPCSubtarget& Subtarget =
7075       static_cast<const PPCSubtarget&>(DAG.getSubtarget());
7076 
7077   MachineFunction &MF = DAG.getMachineFunction();
7078   SmallVector<CCValAssign, 16> ArgLocs;
7079   AIXCCState CCInfo(CFlags.CallConv, CFlags.IsVarArg, MF, ArgLocs,
7080                     *DAG.getContext());
7081 
7082   // Reserve space for the linkage save area (LSA) on the stack.
7083   // In both PPC32 and PPC64 there are 6 reserved slots in the LSA:
7084   //   [SP][CR][LR][2 x reserved][TOC].
7085   // The LSA is 24 bytes (6x4) in PPC32 and 48 bytes (6x8) in PPC64.
7086   const unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
7087   const bool IsPPC64 = Subtarget.isPPC64();
7088   const EVT PtrVT = getPointerTy(DAG.getDataLayout());
7089   const unsigned PtrByteSize = IsPPC64 ? 8 : 4;
7090   CCInfo.AllocateStack(LinkageSize, Align(PtrByteSize));
7091   CCInfo.AnalyzeCallOperands(Outs, CC_AIX);
7092 
7093   // The prolog code of the callee may store up to 8 GPR argument registers to
7094   // the stack, allowing va_start to index over them in memory if the callee
7095   // is variadic.
7096   // Because we cannot tell if this is needed on the caller side, we have to
7097   // conservatively assume that it is needed.  As such, make sure we have at
7098   // least enough stack space for the caller to store the 8 GPRs.
7099   const unsigned MinParameterSaveAreaSize = 8 * PtrByteSize;
7100   const unsigned NumBytes = std::max(LinkageSize + MinParameterSaveAreaSize,
7101                                      CCInfo.getNextStackOffset());
7102 
7103   // Adjust the stack pointer for the new arguments...
7104   // These operations are automatically eliminated by the prolog/epilog pass.
7105   Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
7106   SDValue CallSeqStart = Chain;
7107 
7108   SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
7109   SmallVector<SDValue, 8> MemOpChains;
7110 
7111   // Set up a copy of the stack pointer for loading and storing any
7112   // arguments that may not fit in the registers available for argument
7113   // passing.
7114   const SDValue StackPtr = IsPPC64 ? DAG.getRegister(PPC::X1, MVT::i64)
7115                                    : DAG.getRegister(PPC::R1, MVT::i32);
7116 
7117   for (unsigned I = 0, E = ArgLocs.size(); I != E;) {
7118     const unsigned ValNo = ArgLocs[I].getValNo();
7119     SDValue Arg = OutVals[ValNo];
7120     ISD::ArgFlagsTy Flags = Outs[ValNo].Flags;
7121 
7122     if (Flags.isByVal()) {
7123       const unsigned ByValSize = Flags.getByValSize();
7124 
7125       // Nothing to do for zero-sized ByVals on the caller side.
7126       if (!ByValSize) {
7127         ++I;
7128         continue;
7129       }
7130 
7131       auto GetLoad = [&](EVT VT, unsigned LoadOffset) {
7132         return DAG.getExtLoad(
7133             ISD::ZEXTLOAD, dl, PtrVT, Chain,
7134             (LoadOffset != 0)
7135                 ? DAG.getObjectPtrOffset(dl, Arg, TypeSize::Fixed(LoadOffset))
7136                 : Arg,
7137             MachinePointerInfo(), VT);
7138       };
7139 
7140       unsigned LoadOffset = 0;
7141 
7142       // Initialize registers, which are fully occupied by the by-val argument.
7143       while (LoadOffset + PtrByteSize <= ByValSize && ArgLocs[I].isRegLoc()) {
7144         SDValue Load = GetLoad(PtrVT, LoadOffset);
7145         MemOpChains.push_back(Load.getValue(1));
7146         LoadOffset += PtrByteSize;
7147         const CCValAssign &ByValVA = ArgLocs[I++];
7148         assert(ByValVA.getValNo() == ValNo &&
7149                "Unexpected location for pass-by-value argument.");
7150         RegsToPass.push_back(std::make_pair(ByValVA.getLocReg(), Load));
7151       }
7152 
7153       if (LoadOffset == ByValSize)
7154         continue;
7155 
7156       // There must be one more loc to handle the remainder.
7157       assert(ArgLocs[I].getValNo() == ValNo &&
7158              "Expected additional location for by-value argument.");
7159 
7160       if (ArgLocs[I].isMemLoc()) {
7161         assert(LoadOffset < ByValSize && "Unexpected memloc for by-val arg.");
7162         const CCValAssign &ByValVA = ArgLocs[I++];
7163         ISD::ArgFlagsTy MemcpyFlags = Flags;
7164         // Only memcpy the bytes that don't pass in register.
7165         MemcpyFlags.setByValSize(ByValSize - LoadOffset);
7166         Chain = CallSeqStart = createMemcpyOutsideCallSeq(
7167             (LoadOffset != 0)
7168                 ? DAG.getObjectPtrOffset(dl, Arg, TypeSize::Fixed(LoadOffset))
7169                 : Arg,
7170             DAG.getObjectPtrOffset(dl, StackPtr,
7171                                    TypeSize::Fixed(ByValVA.getLocMemOffset())),
7172             CallSeqStart, MemcpyFlags, DAG, dl);
7173         continue;
7174       }
7175 
7176       // Initialize the final register residue.
7177       // Any residue that occupies the final by-val arg register must be
7178       // left-justified on AIX. Loads must be a power-of-2 size and cannot be
7179       // larger than the ByValSize. For example: a 7 byte by-val arg requires 4,
7180       // 2 and 1 byte loads.
7181       const unsigned ResidueBytes = ByValSize % PtrByteSize;
7182       assert(ResidueBytes != 0 && LoadOffset + PtrByteSize > ByValSize &&
7183              "Unexpected register residue for by-value argument.");
7184       SDValue ResidueVal;
7185       for (unsigned Bytes = 0; Bytes != ResidueBytes;) {
7186         const unsigned N = PowerOf2Floor(ResidueBytes - Bytes);
7187         const MVT VT =
7188             N == 1 ? MVT::i8
7189                    : ((N == 2) ? MVT::i16 : (N == 4 ? MVT::i32 : MVT::i64));
7190         SDValue Load = GetLoad(VT, LoadOffset);
7191         MemOpChains.push_back(Load.getValue(1));
7192         LoadOffset += N;
7193         Bytes += N;
7194 
7195         // By-val arguments are passed left-justfied in register.
7196         // Every load here needs to be shifted, otherwise a full register load
7197         // should have been used.
7198         assert(PtrVT.getSimpleVT().getSizeInBits() > (Bytes * 8) &&
7199                "Unexpected load emitted during handling of pass-by-value "
7200                "argument.");
7201         unsigned NumSHLBits = PtrVT.getSimpleVT().getSizeInBits() - (Bytes * 8);
7202         EVT ShiftAmountTy =
7203             getShiftAmountTy(Load->getValueType(0), DAG.getDataLayout());
7204         SDValue SHLAmt = DAG.getConstant(NumSHLBits, dl, ShiftAmountTy);
7205         SDValue ShiftedLoad =
7206             DAG.getNode(ISD::SHL, dl, Load.getValueType(), Load, SHLAmt);
7207         ResidueVal = ResidueVal ? DAG.getNode(ISD::OR, dl, PtrVT, ResidueVal,
7208                                               ShiftedLoad)
7209                                 : ShiftedLoad;
7210       }
7211 
7212       const CCValAssign &ByValVA = ArgLocs[I++];
7213       RegsToPass.push_back(std::make_pair(ByValVA.getLocReg(), ResidueVal));
7214       continue;
7215     }
7216 
7217     CCValAssign &VA = ArgLocs[I++];
7218     const MVT LocVT = VA.getLocVT();
7219     const MVT ValVT = VA.getValVT();
7220 
7221     switch (VA.getLocInfo()) {
7222     default:
7223       report_fatal_error("Unexpected argument extension type.");
7224     case CCValAssign::Full:
7225       break;
7226     case CCValAssign::ZExt:
7227       Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
7228       break;
7229     case CCValAssign::SExt:
7230       Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
7231       break;
7232     }
7233 
7234     if (VA.isRegLoc() && !VA.needsCustom()) {
7235       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
7236       continue;
7237     }
7238 
7239     // Vector arguments passed to VarArg functions need custom handling when
7240     // they are passed (at least partially) in GPRs.
7241     if (VA.isMemLoc() && VA.needsCustom() && ValVT.isVector()) {
7242       assert(CFlags.IsVarArg && "Custom MemLocs only used for Vector args.");
7243       // Store value to its stack slot.
7244       SDValue PtrOff =
7245           DAG.getConstant(VA.getLocMemOffset(), dl, StackPtr.getValueType());
7246       PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
7247       SDValue Store =
7248           DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo());
7249       MemOpChains.push_back(Store);
7250       const unsigned OriginalValNo = VA.getValNo();
7251       // Then load the GPRs from the stack
7252       unsigned LoadOffset = 0;
7253       auto HandleCustomVecRegLoc = [&]() {
7254         assert(I != E && "Unexpected end of CCvalAssigns.");
7255         assert(ArgLocs[I].isRegLoc() && ArgLocs[I].needsCustom() &&
7256                "Expected custom RegLoc.");
7257         CCValAssign RegVA = ArgLocs[I++];
7258         assert(RegVA.getValNo() == OriginalValNo &&
7259                "Custom MemLoc ValNo and custom RegLoc ValNo must match.");
7260         SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
7261                                   DAG.getConstant(LoadOffset, dl, PtrVT));
7262         SDValue Load = DAG.getLoad(PtrVT, dl, Store, Add, MachinePointerInfo());
7263         MemOpChains.push_back(Load.getValue(1));
7264         RegsToPass.push_back(std::make_pair(RegVA.getLocReg(), Load));
7265         LoadOffset += PtrByteSize;
7266       };
7267 
7268       // In 64-bit there will be exactly 2 custom RegLocs that follow, and in
7269       // in 32-bit there will be 2 custom RegLocs if we are passing in R9 and
7270       // R10.
7271       HandleCustomVecRegLoc();
7272       HandleCustomVecRegLoc();
7273 
7274       if (I != E && ArgLocs[I].isRegLoc() && ArgLocs[I].needsCustom() &&
7275           ArgLocs[I].getValNo() == OriginalValNo) {
7276         assert(!IsPPC64 &&
7277                "Only 2 custom RegLocs expected for 64-bit codegen.");
7278         HandleCustomVecRegLoc();
7279         HandleCustomVecRegLoc();
7280       }
7281 
7282       continue;
7283     }
7284 
7285     if (VA.isMemLoc()) {
7286       SDValue PtrOff =
7287           DAG.getConstant(VA.getLocMemOffset(), dl, StackPtr.getValueType());
7288       PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
7289       MemOpChains.push_back(
7290           DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()));
7291 
7292       continue;
7293     }
7294 
7295     if (!ValVT.isFloatingPoint())
7296       report_fatal_error(
7297           "Unexpected register handling for calling convention.");
7298 
7299     // Custom handling is used for GPR initializations for vararg float
7300     // arguments.
7301     assert(VA.isRegLoc() && VA.needsCustom() && CFlags.IsVarArg &&
7302            LocVT.isInteger() &&
7303            "Custom register handling only expected for VarArg.");
7304 
7305     SDValue ArgAsInt =
7306         DAG.getBitcast(MVT::getIntegerVT(ValVT.getSizeInBits()), Arg);
7307 
7308     if (Arg.getValueType().getStoreSize() == LocVT.getStoreSize())
7309       // f32 in 32-bit GPR
7310       // f64 in 64-bit GPR
7311       RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgAsInt));
7312     else if (Arg.getValueType().getFixedSizeInBits() <
7313              LocVT.getFixedSizeInBits())
7314       // f32 in 64-bit GPR.
7315       RegsToPass.push_back(std::make_pair(
7316           VA.getLocReg(), DAG.getZExtOrTrunc(ArgAsInt, dl, LocVT)));
7317     else {
7318       // f64 in two 32-bit GPRs
7319       // The 2 GPRs are marked custom and expected to be adjacent in ArgLocs.
7320       assert(Arg.getValueType() == MVT::f64 && CFlags.IsVarArg && !IsPPC64 &&
7321              "Unexpected custom register for argument!");
7322       CCValAssign &GPR1 = VA;
7323       SDValue MSWAsI64 = DAG.getNode(ISD::SRL, dl, MVT::i64, ArgAsInt,
7324                                      DAG.getConstant(32, dl, MVT::i8));
7325       RegsToPass.push_back(std::make_pair(
7326           GPR1.getLocReg(), DAG.getZExtOrTrunc(MSWAsI64, dl, MVT::i32)));
7327 
7328       if (I != E) {
7329         // If only 1 GPR was available, there will only be one custom GPR and
7330         // the argument will also pass in memory.
7331         CCValAssign &PeekArg = ArgLocs[I];
7332         if (PeekArg.isRegLoc() && PeekArg.getValNo() == PeekArg.getValNo()) {
7333           assert(PeekArg.needsCustom() && "A second custom GPR is expected.");
7334           CCValAssign &GPR2 = ArgLocs[I++];
7335           RegsToPass.push_back(std::make_pair(
7336               GPR2.getLocReg(), DAG.getZExtOrTrunc(ArgAsInt, dl, MVT::i32)));
7337         }
7338       }
7339     }
7340   }
7341 
7342   if (!MemOpChains.empty())
7343     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
7344 
7345   // For indirect calls, we need to save the TOC base to the stack for
7346   // restoration after the call.
7347   if (CFlags.IsIndirect) {
7348     assert(!CFlags.IsTailCall && "Indirect tail-calls not supported.");
7349     const MCRegister TOCBaseReg = Subtarget.getTOCPointerRegister();
7350     const MCRegister StackPtrReg = Subtarget.getStackPointerRegister();
7351     const MVT PtrVT = Subtarget.isPPC64() ? MVT::i64 : MVT::i32;
7352     const unsigned TOCSaveOffset =
7353         Subtarget.getFrameLowering()->getTOCSaveOffset();
7354 
7355     setUsesTOCBasePtr(DAG);
7356     SDValue Val = DAG.getCopyFromReg(Chain, dl, TOCBaseReg, PtrVT);
7357     SDValue PtrOff = DAG.getIntPtrConstant(TOCSaveOffset, dl);
7358     SDValue StackPtr = DAG.getRegister(StackPtrReg, PtrVT);
7359     SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
7360     Chain = DAG.getStore(
7361         Val.getValue(1), dl, Val, AddPtr,
7362         MachinePointerInfo::getStack(DAG.getMachineFunction(), TOCSaveOffset));
7363   }
7364 
7365   // Build a sequence of copy-to-reg nodes chained together with token chain
7366   // and flag operands which copy the outgoing args into the appropriate regs.
7367   SDValue InFlag;
7368   for (auto Reg : RegsToPass) {
7369     Chain = DAG.getCopyToReg(Chain, dl, Reg.first, Reg.second, InFlag);
7370     InFlag = Chain.getValue(1);
7371   }
7372 
7373   const int SPDiff = 0;
7374   return FinishCall(CFlags, dl, DAG, RegsToPass, InFlag, Chain, CallSeqStart,
7375                     Callee, SPDiff, NumBytes, Ins, InVals, CB);
7376 }
7377 
7378 bool
7379 PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
7380                                   MachineFunction &MF, bool isVarArg,
7381                                   const SmallVectorImpl<ISD::OutputArg> &Outs,
7382                                   LLVMContext &Context) const {
7383   SmallVector<CCValAssign, 16> RVLocs;
7384   CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
7385   return CCInfo.CheckReturn(
7386       Outs, (Subtarget.isSVR4ABI() && CallConv == CallingConv::Cold)
7387                 ? RetCC_PPC_Cold
7388                 : RetCC_PPC);
7389 }
7390 
7391 SDValue
7392 PPCTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
7393                                bool isVarArg,
7394                                const SmallVectorImpl<ISD::OutputArg> &Outs,
7395                                const SmallVectorImpl<SDValue> &OutVals,
7396                                const SDLoc &dl, SelectionDAG &DAG) const {
7397   SmallVector<CCValAssign, 16> RVLocs;
7398   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
7399                  *DAG.getContext());
7400   CCInfo.AnalyzeReturn(Outs,
7401                        (Subtarget.isSVR4ABI() && CallConv == CallingConv::Cold)
7402                            ? RetCC_PPC_Cold
7403                            : RetCC_PPC);
7404 
7405   SDValue Flag;
7406   SmallVector<SDValue, 4> RetOps(1, Chain);
7407 
7408   // Copy the result values into the output registers.
7409   for (unsigned i = 0, RealResIdx = 0; i != RVLocs.size(); ++i, ++RealResIdx) {
7410     CCValAssign &VA = RVLocs[i];
7411     assert(VA.isRegLoc() && "Can only return in registers!");
7412 
7413     SDValue Arg = OutVals[RealResIdx];
7414 
7415     switch (VA.getLocInfo()) {
7416     default: llvm_unreachable("Unknown loc info!");
7417     case CCValAssign::Full: break;
7418     case CCValAssign::AExt:
7419       Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
7420       break;
7421     case CCValAssign::ZExt:
7422       Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
7423       break;
7424     case CCValAssign::SExt:
7425       Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
7426       break;
7427     }
7428     if (Subtarget.hasSPE() && VA.getLocVT() == MVT::f64) {
7429       bool isLittleEndian = Subtarget.isLittleEndian();
7430       // Legalize ret f64 -> ret 2 x i32.
7431       SDValue SVal =
7432           DAG.getNode(PPCISD::EXTRACT_SPE, dl, MVT::i32, Arg,
7433                       DAG.getIntPtrConstant(isLittleEndian ? 0 : 1, dl));
7434       Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), SVal, Flag);
7435       RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
7436       SVal = DAG.getNode(PPCISD::EXTRACT_SPE, dl, MVT::i32, Arg,
7437                          DAG.getIntPtrConstant(isLittleEndian ? 1 : 0, dl));
7438       Flag = Chain.getValue(1);
7439       VA = RVLocs[++i]; // skip ahead to next loc
7440       Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), SVal, Flag);
7441     } else
7442       Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
7443     Flag = Chain.getValue(1);
7444     RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
7445   }
7446 
7447   RetOps[0] = Chain;  // Update chain.
7448 
7449   // Add the flag if we have it.
7450   if (Flag.getNode())
7451     RetOps.push_back(Flag);
7452 
7453   return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, RetOps);
7454 }
7455 
7456 SDValue
7457 PPCTargetLowering::LowerGET_DYNAMIC_AREA_OFFSET(SDValue Op,
7458                                                 SelectionDAG &DAG) const {
7459   SDLoc dl(Op);
7460 
7461   // Get the correct type for integers.
7462   EVT IntVT = Op.getValueType();
7463 
7464   // Get the inputs.
7465   SDValue Chain = Op.getOperand(0);
7466   SDValue FPSIdx = getFramePointerFrameIndex(DAG);
7467   // Build a DYNAREAOFFSET node.
7468   SDValue Ops[2] = {Chain, FPSIdx};
7469   SDVTList VTs = DAG.getVTList(IntVT);
7470   return DAG.getNode(PPCISD::DYNAREAOFFSET, dl, VTs, Ops);
7471 }
7472 
7473 SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op,
7474                                              SelectionDAG &DAG) const {
7475   // When we pop the dynamic allocation we need to restore the SP link.
7476   SDLoc dl(Op);
7477 
7478   // Get the correct type for pointers.
7479   EVT PtrVT = getPointerTy(DAG.getDataLayout());
7480 
7481   // Construct the stack pointer operand.
7482   bool isPPC64 = Subtarget.isPPC64();
7483   unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
7484   SDValue StackPtr = DAG.getRegister(SP, PtrVT);
7485 
7486   // Get the operands for the STACKRESTORE.
7487   SDValue Chain = Op.getOperand(0);
7488   SDValue SaveSP = Op.getOperand(1);
7489 
7490   // Load the old link SP.
7491   SDValue LoadLinkSP =
7492       DAG.getLoad(PtrVT, dl, Chain, StackPtr, MachinePointerInfo());
7493 
7494   // Restore the stack pointer.
7495   Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
7496 
7497   // Store the old link SP.
7498   return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo());
7499 }
7500 
7501 SDValue PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG &DAG) const {
7502   MachineFunction &MF = DAG.getMachineFunction();
7503   bool isPPC64 = Subtarget.isPPC64();
7504   EVT PtrVT = getPointerTy(MF.getDataLayout());
7505 
7506   // Get current frame pointer save index.  The users of this index will be
7507   // primarily DYNALLOC instructions.
7508   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
7509   int RASI = FI->getReturnAddrSaveIndex();
7510 
7511   // If the frame pointer save index hasn't been defined yet.
7512   if (!RASI) {
7513     // Find out what the fix offset of the frame pointer save area.
7514     int LROffset = Subtarget.getFrameLowering()->getReturnSaveOffset();
7515     // Allocate the frame index for frame pointer save area.
7516     RASI = MF.getFrameInfo().CreateFixedObject(isPPC64? 8 : 4, LROffset, false);
7517     // Save the result.
7518     FI->setReturnAddrSaveIndex(RASI);
7519   }
7520   return DAG.getFrameIndex(RASI, PtrVT);
7521 }
7522 
7523 SDValue
7524 PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
7525   MachineFunction &MF = DAG.getMachineFunction();
7526   bool isPPC64 = Subtarget.isPPC64();
7527   EVT PtrVT = getPointerTy(MF.getDataLayout());
7528 
7529   // Get current frame pointer save index.  The users of this index will be
7530   // primarily DYNALLOC instructions.
7531   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
7532   int FPSI = FI->getFramePointerSaveIndex();
7533 
7534   // If the frame pointer save index hasn't been defined yet.
7535   if (!FPSI) {
7536     // Find out what the fix offset of the frame pointer save area.
7537     int FPOffset = Subtarget.getFrameLowering()->getFramePointerSaveOffset();
7538     // Allocate the frame index for frame pointer save area.
7539     FPSI = MF.getFrameInfo().CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
7540     // Save the result.
7541     FI->setFramePointerSaveIndex(FPSI);
7542   }
7543   return DAG.getFrameIndex(FPSI, PtrVT);
7544 }
7545 
7546 SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
7547                                                    SelectionDAG &DAG) const {
7548   MachineFunction &MF = DAG.getMachineFunction();
7549   // Get the inputs.
7550   SDValue Chain = Op.getOperand(0);
7551   SDValue Size  = Op.getOperand(1);
7552   SDLoc dl(Op);
7553 
7554   // Get the correct type for pointers.
7555   EVT PtrVT = getPointerTy(DAG.getDataLayout());
7556   // Negate the size.
7557   SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
7558                                 DAG.getConstant(0, dl, PtrVT), Size);
7559   // Construct a node for the frame pointer save index.
7560   SDValue FPSIdx = getFramePointerFrameIndex(DAG);
7561   SDValue Ops[3] = { Chain, NegSize, FPSIdx };
7562   SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
7563   if (hasInlineStackProbe(MF))
7564     return DAG.getNode(PPCISD::PROBED_ALLOCA, dl, VTs, Ops);
7565   return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops);
7566 }
7567 
7568 SDValue PPCTargetLowering::LowerEH_DWARF_CFA(SDValue Op,
7569                                                      SelectionDAG &DAG) const {
7570   MachineFunction &MF = DAG.getMachineFunction();
7571 
7572   bool isPPC64 = Subtarget.isPPC64();
7573   EVT PtrVT = getPointerTy(DAG.getDataLayout());
7574 
7575   int FI = MF.getFrameInfo().CreateFixedObject(isPPC64 ? 8 : 4, 0, false);
7576   return DAG.getFrameIndex(FI, PtrVT);
7577 }
7578 
7579 SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
7580                                                SelectionDAG &DAG) const {
7581   SDLoc DL(Op);
7582   return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
7583                      DAG.getVTList(MVT::i32, MVT::Other),
7584                      Op.getOperand(0), Op.getOperand(1));
7585 }
7586 
7587 SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
7588                                                 SelectionDAG &DAG) const {
7589   SDLoc DL(Op);
7590   return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
7591                      Op.getOperand(0), Op.getOperand(1));
7592 }
7593 
7594 SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
7595   if (Op.getValueType().isVector())
7596     return LowerVectorLoad(Op, DAG);
7597 
7598   assert(Op.getValueType() == MVT::i1 &&
7599          "Custom lowering only for i1 loads");
7600 
7601   // First, load 8 bits into 32 bits, then truncate to 1 bit.
7602 
7603   SDLoc dl(Op);
7604   LoadSDNode *LD = cast<LoadSDNode>(Op);
7605 
7606   SDValue Chain = LD->getChain();
7607   SDValue BasePtr = LD->getBasePtr();
7608   MachineMemOperand *MMO = LD->getMemOperand();
7609 
7610   SDValue NewLD =
7611       DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(DAG.getDataLayout()), Chain,
7612                      BasePtr, MVT::i8, MMO);
7613   SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD);
7614 
7615   SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) };
7616   return DAG.getMergeValues(Ops, dl);
7617 }
7618 
7619 SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
7620   if (Op.getOperand(1).getValueType().isVector())
7621     return LowerVectorStore(Op, DAG);
7622 
7623   assert(Op.getOperand(1).getValueType() == MVT::i1 &&
7624          "Custom lowering only for i1 stores");
7625 
7626   // First, zero extend to 32 bits, then use a truncating store to 8 bits.
7627 
7628   SDLoc dl(Op);
7629   StoreSDNode *ST = cast<StoreSDNode>(Op);
7630 
7631   SDValue Chain = ST->getChain();
7632   SDValue BasePtr = ST->getBasePtr();
7633   SDValue Value = ST->getValue();
7634   MachineMemOperand *MMO = ST->getMemOperand();
7635 
7636   Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(DAG.getDataLayout()),
7637                       Value);
7638   return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO);
7639 }
7640 
7641 // FIXME: Remove this once the ANDI glue bug is fixed:
7642 SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
7643   assert(Op.getValueType() == MVT::i1 &&
7644          "Custom lowering only for i1 results");
7645 
7646   SDLoc DL(Op);
7647   return DAG.getNode(PPCISD::ANDI_rec_1_GT_BIT, DL, MVT::i1, Op.getOperand(0));
7648 }
7649 
7650 SDValue PPCTargetLowering::LowerTRUNCATEVector(SDValue Op,
7651                                                SelectionDAG &DAG) const {
7652 
7653   // Implements a vector truncate that fits in a vector register as a shuffle.
7654   // We want to legalize vector truncates down to where the source fits in
7655   // a vector register (and target is therefore smaller than vector register
7656   // size).  At that point legalization will try to custom lower the sub-legal
7657   // result and get here - where we can contain the truncate as a single target
7658   // operation.
7659 
7660   // For example a trunc <2 x i16> to <2 x i8> could be visualized as follows:
7661   //   <MSB1|LSB1, MSB2|LSB2> to <LSB1, LSB2>
7662   //
7663   // We will implement it for big-endian ordering as this (where x denotes
7664   // undefined):
7665   //   < MSB1|LSB1, MSB2|LSB2, uu, uu, uu, uu, uu, uu> to
7666   //   < LSB1, LSB2, u, u, u, u, u, u, u, u, u, u, u, u, u, u>
7667   //
7668   // The same operation in little-endian ordering will be:
7669   //   <uu, uu, uu, uu, uu, uu, LSB2|MSB2, LSB1|MSB1> to
7670   //   <u, u, u, u, u, u, u, u, u, u, u, u, u, u, LSB2, LSB1>
7671 
7672   EVT TrgVT = Op.getValueType();
7673   assert(TrgVT.isVector() && "Vector type expected.");
7674   unsigned TrgNumElts = TrgVT.getVectorNumElements();
7675   EVT EltVT = TrgVT.getVectorElementType();
7676   if (!isOperationCustom(Op.getOpcode(), TrgVT) ||
7677       TrgVT.getSizeInBits() > 128 || !isPowerOf2_32(TrgNumElts) ||
7678       !isPowerOf2_32(EltVT.getSizeInBits()))
7679     return SDValue();
7680 
7681   SDValue N1 = Op.getOperand(0);
7682   EVT SrcVT = N1.getValueType();
7683   unsigned SrcSize = SrcVT.getSizeInBits();
7684   if (SrcSize > 256 ||
7685       !isPowerOf2_32(SrcVT.getVectorNumElements()) ||
7686       !isPowerOf2_32(SrcVT.getVectorElementType().getSizeInBits()))
7687     return SDValue();
7688   if (SrcSize == 256 && SrcVT.getVectorNumElements() < 2)
7689     return SDValue();
7690 
7691   unsigned WideNumElts = 128 / EltVT.getSizeInBits();
7692   EVT WideVT = EVT::getVectorVT(*DAG.getContext(), EltVT, WideNumElts);
7693 
7694   SDLoc DL(Op);
7695   SDValue Op1, Op2;
7696   if (SrcSize == 256) {
7697     EVT VecIdxTy = getVectorIdxTy(DAG.getDataLayout());
7698     EVT SplitVT =
7699         N1.getValueType().getHalfNumVectorElementsVT(*DAG.getContext());
7700     unsigned SplitNumElts = SplitVT.getVectorNumElements();
7701     Op1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SplitVT, N1,
7702                       DAG.getConstant(0, DL, VecIdxTy));
7703     Op2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SplitVT, N1,
7704                       DAG.getConstant(SplitNumElts, DL, VecIdxTy));
7705   }
7706   else {
7707     Op1 = SrcSize == 128 ? N1 : widenVec(DAG, N1, DL);
7708     Op2 = DAG.getUNDEF(WideVT);
7709   }
7710 
7711   // First list the elements we want to keep.
7712   unsigned SizeMult = SrcSize / TrgVT.getSizeInBits();
7713   SmallVector<int, 16> ShuffV;
7714   if (Subtarget.isLittleEndian())
7715     for (unsigned i = 0; i < TrgNumElts; ++i)
7716       ShuffV.push_back(i * SizeMult);
7717   else
7718     for (unsigned i = 1; i <= TrgNumElts; ++i)
7719       ShuffV.push_back(i * SizeMult - 1);
7720 
7721   // Populate the remaining elements with undefs.
7722   for (unsigned i = TrgNumElts; i < WideNumElts; ++i)
7723     // ShuffV.push_back(i + WideNumElts);
7724     ShuffV.push_back(WideNumElts + 1);
7725 
7726   Op1 = DAG.getNode(ISD::BITCAST, DL, WideVT, Op1);
7727   Op2 = DAG.getNode(ISD::BITCAST, DL, WideVT, Op2);
7728   return DAG.getVectorShuffle(WideVT, DL, Op1, Op2, ShuffV);
7729 }
7730 
7731 /// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
7732 /// possible.
7733 SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
7734   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
7735   EVT ResVT = Op.getValueType();
7736   EVT CmpVT = Op.getOperand(0).getValueType();
7737   SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
7738   SDValue TV  = Op.getOperand(2), FV  = Op.getOperand(3);
7739   SDLoc dl(Op);
7740 
7741   // Without power9-vector, we don't have native instruction for f128 comparison.
7742   // Following transformation to libcall is needed for setcc:
7743   // select_cc lhs, rhs, tv, fv, cc -> select_cc (setcc cc, x, y), 0, tv, fv, NE
7744   if (!Subtarget.hasP9Vector() && CmpVT == MVT::f128) {
7745     SDValue Z = DAG.getSetCC(
7746         dl, getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), CmpVT),
7747         LHS, RHS, CC);
7748     SDValue Zero = DAG.getConstant(0, dl, Z.getValueType());
7749     return DAG.getSelectCC(dl, Z, Zero, TV, FV, ISD::SETNE);
7750   }
7751 
7752   // Not FP, or using SPE? Not a fsel.
7753   if (!CmpVT.isFloatingPoint() || !TV.getValueType().isFloatingPoint() ||
7754       Subtarget.hasSPE())
7755     return Op;
7756 
7757   SDNodeFlags Flags = Op.getNode()->getFlags();
7758 
7759   // We have xsmaxcdp/xsmincdp which are OK to emit even in the
7760   // presence of infinities.
7761   if (Subtarget.hasP9Vector() && LHS == TV && RHS == FV) {
7762     switch (CC) {
7763     default:
7764       break;
7765     case ISD::SETOGT:
7766     case ISD::SETGT:
7767       return DAG.getNode(PPCISD::XSMAXCDP, dl, Op.getValueType(), LHS, RHS);
7768     case ISD::SETOLT:
7769     case ISD::SETLT:
7770       return DAG.getNode(PPCISD::XSMINCDP, dl, Op.getValueType(), LHS, RHS);
7771     }
7772   }
7773 
7774   // We might be able to do better than this under some circumstances, but in
7775   // general, fsel-based lowering of select is a finite-math-only optimization.
7776   // For more information, see section F.3 of the 2.06 ISA specification.
7777   // With ISA 3.0
7778   if ((!DAG.getTarget().Options.NoInfsFPMath && !Flags.hasNoInfs()) ||
7779       (!DAG.getTarget().Options.NoNaNsFPMath && !Flags.hasNoNaNs()))
7780     return Op;
7781 
7782   // If the RHS of the comparison is a 0.0, we don't need to do the
7783   // subtraction at all.
7784   SDValue Sel1;
7785   if (isFloatingPointZero(RHS))
7786     switch (CC) {
7787     default: break;       // SETUO etc aren't handled by fsel.
7788     case ISD::SETNE:
7789       std::swap(TV, FV);
7790       LLVM_FALLTHROUGH;
7791     case ISD::SETEQ:
7792       if (LHS.getValueType() == MVT::f32)   // Comparison is always 64-bits
7793         LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
7794       Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
7795       if (Sel1.getValueType() == MVT::f32)   // Comparison is always 64-bits
7796         Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
7797       return DAG.getNode(PPCISD::FSEL, dl, ResVT,
7798                          DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
7799     case ISD::SETULT:
7800     case ISD::SETLT:
7801       std::swap(TV, FV);  // fsel is natively setge, swap operands for setlt
7802       LLVM_FALLTHROUGH;
7803     case ISD::SETOGE:
7804     case ISD::SETGE:
7805       if (LHS.getValueType() == MVT::f32)   // Comparison is always 64-bits
7806         LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
7807       return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
7808     case ISD::SETUGT:
7809     case ISD::SETGT:
7810       std::swap(TV, FV);  // fsel is natively setge, swap operands for setlt
7811       LLVM_FALLTHROUGH;
7812     case ISD::SETOLE:
7813     case ISD::SETLE:
7814       if (LHS.getValueType() == MVT::f32)   // Comparison is always 64-bits
7815         LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
7816       return DAG.getNode(PPCISD::FSEL, dl, ResVT,
7817                          DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
7818     }
7819 
7820   SDValue Cmp;
7821   switch (CC) {
7822   default: break;       // SETUO etc aren't handled by fsel.
7823   case ISD::SETNE:
7824     std::swap(TV, FV);
7825     LLVM_FALLTHROUGH;
7826   case ISD::SETEQ:
7827     Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, Flags);
7828     if (Cmp.getValueType() == MVT::f32)   // Comparison is always 64-bits
7829       Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
7830     Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
7831     if (Sel1.getValueType() == MVT::f32)   // Comparison is always 64-bits
7832       Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
7833     return DAG.getNode(PPCISD::FSEL, dl, ResVT,
7834                        DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
7835   case ISD::SETULT:
7836   case ISD::SETLT:
7837     Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, Flags);
7838     if (Cmp.getValueType() == MVT::f32)   // Comparison is always 64-bits
7839       Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
7840     return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
7841   case ISD::SETOGE:
7842   case ISD::SETGE:
7843     Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, Flags);
7844     if (Cmp.getValueType() == MVT::f32)   // Comparison is always 64-bits
7845       Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
7846     return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
7847   case ISD::SETUGT:
7848   case ISD::SETGT:
7849     Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS, Flags);
7850     if (Cmp.getValueType() == MVT::f32)   // Comparison is always 64-bits
7851       Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
7852     return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
7853   case ISD::SETOLE:
7854   case ISD::SETLE:
7855     Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS, Flags);
7856     if (Cmp.getValueType() == MVT::f32)   // Comparison is always 64-bits
7857       Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
7858     return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
7859   }
7860   return Op;
7861 }
7862 
7863 static unsigned getPPCStrictOpcode(unsigned Opc) {
7864   switch (Opc) {
7865   default:
7866     llvm_unreachable("No strict version of this opcode!");
7867   case PPCISD::FCTIDZ:
7868     return PPCISD::STRICT_FCTIDZ;
7869   case PPCISD::FCTIWZ:
7870     return PPCISD::STRICT_FCTIWZ;
7871   case PPCISD::FCTIDUZ:
7872     return PPCISD::STRICT_FCTIDUZ;
7873   case PPCISD::FCTIWUZ:
7874     return PPCISD::STRICT_FCTIWUZ;
7875   case PPCISD::FCFID:
7876     return PPCISD::STRICT_FCFID;
7877   case PPCISD::FCFIDU:
7878     return PPCISD::STRICT_FCFIDU;
7879   case PPCISD::FCFIDS:
7880     return PPCISD::STRICT_FCFIDS;
7881   case PPCISD::FCFIDUS:
7882     return PPCISD::STRICT_FCFIDUS;
7883   }
7884 }
7885 
7886 static SDValue convertFPToInt(SDValue Op, SelectionDAG &DAG,
7887                               const PPCSubtarget &Subtarget) {
7888   SDLoc dl(Op);
7889   bool IsStrict = Op->isStrictFPOpcode();
7890   bool IsSigned = Op.getOpcode() == ISD::FP_TO_SINT ||
7891                   Op.getOpcode() == ISD::STRICT_FP_TO_SINT;
7892 
7893   // TODO: Any other flags to propagate?
7894   SDNodeFlags Flags;
7895   Flags.setNoFPExcept(Op->getFlags().hasNoFPExcept());
7896 
7897   // For strict nodes, source is the second operand.
7898   SDValue Src = Op.getOperand(IsStrict ? 1 : 0);
7899   SDValue Chain = IsStrict ? Op.getOperand(0) : SDValue();
7900   assert(Src.getValueType().isFloatingPoint());
7901   if (Src.getValueType() == MVT::f32) {
7902     if (IsStrict) {
7903       Src =
7904           DAG.getNode(ISD::STRICT_FP_EXTEND, dl,
7905                       DAG.getVTList(MVT::f64, MVT::Other), {Chain, Src}, Flags);
7906       Chain = Src.getValue(1);
7907     } else
7908       Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
7909   }
7910   SDValue Conv;
7911   unsigned Opc = ISD::DELETED_NODE;
7912   switch (Op.getSimpleValueType().SimpleTy) {
7913   default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
7914   case MVT::i32:
7915     Opc = IsSigned ? PPCISD::FCTIWZ
7916                    : (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ : PPCISD::FCTIDZ);
7917     break;
7918   case MVT::i64:
7919     assert((IsSigned || Subtarget.hasFPCVT()) &&
7920            "i64 FP_TO_UINT is supported only with FPCVT");
7921     Opc = IsSigned ? PPCISD::FCTIDZ : PPCISD::FCTIDUZ;
7922   }
7923   if (IsStrict) {
7924     Opc = getPPCStrictOpcode(Opc);
7925     Conv = DAG.getNode(Opc, dl, DAG.getVTList(MVT::f64, MVT::Other),
7926                        {Chain, Src}, Flags);
7927   } else {
7928     Conv = DAG.getNode(Opc, dl, MVT::f64, Src);
7929   }
7930   return Conv;
7931 }
7932 
7933 void PPCTargetLowering::LowerFP_TO_INTForReuse(SDValue Op, ReuseLoadInfo &RLI,
7934                                                SelectionDAG &DAG,
7935                                                const SDLoc &dl) const {
7936   SDValue Tmp = convertFPToInt(Op, DAG, Subtarget);
7937   bool IsSigned = Op.getOpcode() == ISD::FP_TO_SINT ||
7938                   Op.getOpcode() == ISD::STRICT_FP_TO_SINT;
7939   bool IsStrict = Op->isStrictFPOpcode();
7940 
7941   // Convert the FP value to an int value through memory.
7942   bool i32Stack = Op.getValueType() == MVT::i32 && Subtarget.hasSTFIWX() &&
7943                   (IsSigned || Subtarget.hasFPCVT());
7944   SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
7945   int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
7946   MachinePointerInfo MPI =
7947       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
7948 
7949   // Emit a store to the stack slot.
7950   SDValue Chain = IsStrict ? Tmp.getValue(1) : DAG.getEntryNode();
7951   Align Alignment(DAG.getEVTAlign(Tmp.getValueType()));
7952   if (i32Stack) {
7953     MachineFunction &MF = DAG.getMachineFunction();
7954     Alignment = Align(4);
7955     MachineMemOperand *MMO =
7956         MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, Alignment);
7957     SDValue Ops[] = { Chain, Tmp, FIPtr };
7958     Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
7959               DAG.getVTList(MVT::Other), Ops, MVT::i32, MMO);
7960   } else
7961     Chain = DAG.getStore(Chain, dl, Tmp, FIPtr, MPI, Alignment);
7962 
7963   // Result is a load from the stack slot.  If loading 4 bytes, make sure to
7964   // add in a bias on big endian.
7965   if (Op.getValueType() == MVT::i32 && !i32Stack) {
7966     FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
7967                         DAG.getConstant(4, dl, FIPtr.getValueType()));
7968     MPI = MPI.getWithOffset(Subtarget.isLittleEndian() ? 0 : 4);
7969   }
7970 
7971   RLI.Chain = Chain;
7972   RLI.Ptr = FIPtr;
7973   RLI.MPI = MPI;
7974   RLI.Alignment = Alignment;
7975 }
7976 
7977 /// Custom lowers floating point to integer conversions to use
7978 /// the direct move instructions available in ISA 2.07 to avoid the
7979 /// need for load/store combinations.
7980 SDValue PPCTargetLowering::LowerFP_TO_INTDirectMove(SDValue Op,
7981                                                     SelectionDAG &DAG,
7982                                                     const SDLoc &dl) const {
7983   SDValue Conv = convertFPToInt(Op, DAG, Subtarget);
7984   SDValue Mov = DAG.getNode(PPCISD::MFVSR, dl, Op.getValueType(), Conv);
7985   if (Op->isStrictFPOpcode())
7986     return DAG.getMergeValues({Mov, Conv.getValue(1)}, dl);
7987   else
7988     return Mov;
7989 }
7990 
7991 SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
7992                                           const SDLoc &dl) const {
7993   bool IsStrict = Op->isStrictFPOpcode();
7994   bool IsSigned = Op.getOpcode() == ISD::FP_TO_SINT ||
7995                   Op.getOpcode() == ISD::STRICT_FP_TO_SINT;
7996   SDValue Src = Op.getOperand(IsStrict ? 1 : 0);
7997   EVT SrcVT = Src.getValueType();
7998   EVT DstVT = Op.getValueType();
7999 
8000   // FP to INT conversions are legal for f128.
8001   if (SrcVT == MVT::f128)
8002     return Subtarget.hasP9Vector() ? Op : SDValue();
8003 
8004   // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
8005   // PPC (the libcall is not available).
8006   if (SrcVT == MVT::ppcf128) {
8007     if (DstVT == MVT::i32) {
8008       // TODO: Conservatively pass only nofpexcept flag here. Need to check and
8009       // set other fast-math flags to FP operations in both strict and
8010       // non-strict cases. (FP_TO_SINT, FSUB)
8011       SDNodeFlags Flags;
8012       Flags.setNoFPExcept(Op->getFlags().hasNoFPExcept());
8013 
8014       if (IsSigned) {
8015         SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::f64, Src,
8016                                  DAG.getIntPtrConstant(0, dl));
8017         SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::f64, Src,
8018                                  DAG.getIntPtrConstant(1, dl));
8019 
8020         // Add the two halves of the long double in round-to-zero mode, and use
8021         // a smaller FP_TO_SINT.
8022         if (IsStrict) {
8023           SDValue Res = DAG.getNode(PPCISD::STRICT_FADDRTZ, dl,
8024                                     DAG.getVTList(MVT::f64, MVT::Other),
8025                                     {Op.getOperand(0), Lo, Hi}, Flags);
8026           return DAG.getNode(ISD::STRICT_FP_TO_SINT, dl,
8027                              DAG.getVTList(MVT::i32, MVT::Other),
8028                              {Res.getValue(1), Res}, Flags);
8029         } else {
8030           SDValue Res = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
8031           return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
8032         }
8033       } else {
8034         const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
8035         APFloat APF = APFloat(APFloat::PPCDoubleDouble(), APInt(128, TwoE31));
8036         SDValue Cst = DAG.getConstantFP(APF, dl, SrcVT);
8037         SDValue SignMask = DAG.getConstant(0x80000000, dl, DstVT);
8038         if (IsStrict) {
8039           // Sel = Src < 0x80000000
8040           // FltOfs = select Sel, 0.0, 0x80000000
8041           // IntOfs = select Sel, 0, 0x80000000
8042           // Result = fp_to_sint(Src - FltOfs) ^ IntOfs
8043           SDValue Chain = Op.getOperand(0);
8044           EVT SetCCVT =
8045               getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), SrcVT);
8046           EVT DstSetCCVT =
8047               getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), DstVT);
8048           SDValue Sel = DAG.getSetCC(dl, SetCCVT, Src, Cst, ISD::SETLT,
8049                                      Chain, true);
8050           Chain = Sel.getValue(1);
8051 
8052           SDValue FltOfs = DAG.getSelect(
8053               dl, SrcVT, Sel, DAG.getConstantFP(0.0, dl, SrcVT), Cst);
8054           Sel = DAG.getBoolExtOrTrunc(Sel, dl, DstSetCCVT, DstVT);
8055 
8056           SDValue Val = DAG.getNode(ISD::STRICT_FSUB, dl,
8057                                     DAG.getVTList(SrcVT, MVT::Other),
8058                                     {Chain, Src, FltOfs}, Flags);
8059           Chain = Val.getValue(1);
8060           SDValue SInt = DAG.getNode(ISD::STRICT_FP_TO_SINT, dl,
8061                                      DAG.getVTList(DstVT, MVT::Other),
8062                                      {Chain, Val}, Flags);
8063           Chain = SInt.getValue(1);
8064           SDValue IntOfs = DAG.getSelect(
8065               dl, DstVT, Sel, DAG.getConstant(0, dl, DstVT), SignMask);
8066           SDValue Result = DAG.getNode(ISD::XOR, dl, DstVT, SInt, IntOfs);
8067           return DAG.getMergeValues({Result, Chain}, dl);
8068         } else {
8069           // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
8070           // FIXME: generated code sucks.
8071           SDValue True = DAG.getNode(ISD::FSUB, dl, MVT::ppcf128, Src, Cst);
8072           True = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, True);
8073           True = DAG.getNode(ISD::ADD, dl, MVT::i32, True, SignMask);
8074           SDValue False = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Src);
8075           return DAG.getSelectCC(dl, Src, Cst, True, False, ISD::SETGE);
8076         }
8077       }
8078     }
8079 
8080     return SDValue();
8081   }
8082 
8083   if (Subtarget.hasDirectMove() && Subtarget.isPPC64())
8084     return LowerFP_TO_INTDirectMove(Op, DAG, dl);
8085 
8086   ReuseLoadInfo RLI;
8087   LowerFP_TO_INTForReuse(Op, RLI, DAG, dl);
8088 
8089   return DAG.getLoad(Op.getValueType(), dl, RLI.Chain, RLI.Ptr, RLI.MPI,
8090                      RLI.Alignment, RLI.MMOFlags(), RLI.AAInfo, RLI.Ranges);
8091 }
8092 
8093 // We're trying to insert a regular store, S, and then a load, L. If the
8094 // incoming value, O, is a load, we might just be able to have our load use the
8095 // address used by O. However, we don't know if anything else will store to
8096 // that address before we can load from it. To prevent this situation, we need
8097 // to insert our load, L, into the chain as a peer of O. To do this, we give L
8098 // the same chain operand as O, we create a token factor from the chain results
8099 // of O and L, and we replace all uses of O's chain result with that token
8100 // factor (see spliceIntoChain below for this last part).
8101 bool PPCTargetLowering::canReuseLoadAddress(SDValue Op, EVT MemVT,
8102                                             ReuseLoadInfo &RLI,
8103                                             SelectionDAG &DAG,
8104                                             ISD::LoadExtType ET) const {
8105   // Conservatively skip reusing for constrained FP nodes.
8106   if (Op->isStrictFPOpcode())
8107     return false;
8108 
8109   SDLoc dl(Op);
8110   bool ValidFPToUint = Op.getOpcode() == ISD::FP_TO_UINT &&
8111                        (Subtarget.hasFPCVT() || Op.getValueType() == MVT::i32);
8112   if (ET == ISD::NON_EXTLOAD &&
8113       (ValidFPToUint || Op.getOpcode() == ISD::FP_TO_SINT) &&
8114       isOperationLegalOrCustom(Op.getOpcode(),
8115                                Op.getOperand(0).getValueType())) {
8116 
8117     LowerFP_TO_INTForReuse(Op, RLI, DAG, dl);
8118     return true;
8119   }
8120 
8121   LoadSDNode *LD = dyn_cast<LoadSDNode>(Op);
8122   if (!LD || LD->getExtensionType() != ET || LD->isVolatile() ||
8123       LD->isNonTemporal())
8124     return false;
8125   if (LD->getMemoryVT() != MemVT)
8126     return false;
8127 
8128   // If the result of the load is an illegal type, then we can't build a
8129   // valid chain for reuse since the legalised loads and token factor node that
8130   // ties the legalised loads together uses a different output chain then the
8131   // illegal load.
8132   if (!isTypeLegal(LD->getValueType(0)))
8133     return false;
8134 
8135   RLI.Ptr = LD->getBasePtr();
8136   if (LD->isIndexed() && !LD->getOffset().isUndef()) {
8137     assert(LD->getAddressingMode() == ISD::PRE_INC &&
8138            "Non-pre-inc AM on PPC?");
8139     RLI.Ptr = DAG.getNode(ISD::ADD, dl, RLI.Ptr.getValueType(), RLI.Ptr,
8140                           LD->getOffset());
8141   }
8142 
8143   RLI.Chain = LD->getChain();
8144   RLI.MPI = LD->getPointerInfo();
8145   RLI.IsDereferenceable = LD->isDereferenceable();
8146   RLI.IsInvariant = LD->isInvariant();
8147   RLI.Alignment = LD->getAlign();
8148   RLI.AAInfo = LD->getAAInfo();
8149   RLI.Ranges = LD->getRanges();
8150 
8151   RLI.ResChain = SDValue(LD, LD->isIndexed() ? 2 : 1);
8152   return true;
8153 }
8154 
8155 // Given the head of the old chain, ResChain, insert a token factor containing
8156 // it and NewResChain, and make users of ResChain now be users of that token
8157 // factor.
8158 // TODO: Remove and use DAG::makeEquivalentMemoryOrdering() instead.
8159 void PPCTargetLowering::spliceIntoChain(SDValue ResChain,
8160                                         SDValue NewResChain,
8161                                         SelectionDAG &DAG) const {
8162   if (!ResChain)
8163     return;
8164 
8165   SDLoc dl(NewResChain);
8166 
8167   SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8168                            NewResChain, DAG.getUNDEF(MVT::Other));
8169   assert(TF.getNode() != NewResChain.getNode() &&
8170          "A new TF really is required here");
8171 
8172   DAG.ReplaceAllUsesOfValueWith(ResChain, TF);
8173   DAG.UpdateNodeOperands(TF.getNode(), ResChain, NewResChain);
8174 }
8175 
8176 /// Analyze profitability of direct move
8177 /// prefer float load to int load plus direct move
8178 /// when there is no integer use of int load
8179 bool PPCTargetLowering::directMoveIsProfitable(const SDValue &Op) const {
8180   SDNode *Origin = Op.getOperand(0).getNode();
8181   if (Origin->getOpcode() != ISD::LOAD)
8182     return true;
8183 
8184   // If there is no LXSIBZX/LXSIHZX, like Power8,
8185   // prefer direct move if the memory size is 1 or 2 bytes.
8186   MachineMemOperand *MMO = cast<LoadSDNode>(Origin)->getMemOperand();
8187   if (!Subtarget.hasP9Vector() && MMO->getSize() <= 2)
8188     return true;
8189 
8190   for (SDNode::use_iterator UI = Origin->use_begin(),
8191                             UE = Origin->use_end();
8192        UI != UE; ++UI) {
8193 
8194     // Only look at the users of the loaded value.
8195     if (UI.getUse().get().getResNo() != 0)
8196       continue;
8197 
8198     if (UI->getOpcode() != ISD::SINT_TO_FP &&
8199         UI->getOpcode() != ISD::UINT_TO_FP &&
8200         UI->getOpcode() != ISD::STRICT_SINT_TO_FP &&
8201         UI->getOpcode() != ISD::STRICT_UINT_TO_FP)
8202       return true;
8203   }
8204 
8205   return false;
8206 }
8207 
8208 static SDValue convertIntToFP(SDValue Op, SDValue Src, SelectionDAG &DAG,
8209                               const PPCSubtarget &Subtarget,
8210                               SDValue Chain = SDValue()) {
8211   bool IsSigned = Op.getOpcode() == ISD::SINT_TO_FP ||
8212                   Op.getOpcode() == ISD::STRICT_SINT_TO_FP;
8213   SDLoc dl(Op);
8214 
8215   // TODO: Any other flags to propagate?
8216   SDNodeFlags Flags;
8217   Flags.setNoFPExcept(Op->getFlags().hasNoFPExcept());
8218 
8219   // If we have FCFIDS, then use it when converting to single-precision.
8220   // Otherwise, convert to double-precision and then round.
8221   bool IsSingle = Op.getValueType() == MVT::f32 && Subtarget.hasFPCVT();
8222   unsigned ConvOpc = IsSingle ? (IsSigned ? PPCISD::FCFIDS : PPCISD::FCFIDUS)
8223                               : (IsSigned ? PPCISD::FCFID : PPCISD::FCFIDU);
8224   EVT ConvTy = IsSingle ? MVT::f32 : MVT::f64;
8225   if (Op->isStrictFPOpcode()) {
8226     if (!Chain)
8227       Chain = Op.getOperand(0);
8228     return DAG.getNode(getPPCStrictOpcode(ConvOpc), dl,
8229                        DAG.getVTList(ConvTy, MVT::Other), {Chain, Src}, Flags);
8230   } else
8231     return DAG.getNode(ConvOpc, dl, ConvTy, Src);
8232 }
8233 
8234 /// Custom lowers integer to floating point conversions to use
8235 /// the direct move instructions available in ISA 2.07 to avoid the
8236 /// need for load/store combinations.
8237 SDValue PPCTargetLowering::LowerINT_TO_FPDirectMove(SDValue Op,
8238                                                     SelectionDAG &DAG,
8239                                                     const SDLoc &dl) const {
8240   assert((Op.getValueType() == MVT::f32 ||
8241           Op.getValueType() == MVT::f64) &&
8242          "Invalid floating point type as target of conversion");
8243   assert(Subtarget.hasFPCVT() &&
8244          "Int to FP conversions with direct moves require FPCVT");
8245   SDValue Src = Op.getOperand(Op->isStrictFPOpcode() ? 1 : 0);
8246   bool WordInt = Src.getSimpleValueType().SimpleTy == MVT::i32;
8247   bool Signed = Op.getOpcode() == ISD::SINT_TO_FP ||
8248                 Op.getOpcode() == ISD::STRICT_SINT_TO_FP;
8249   unsigned MovOpc = (WordInt && !Signed) ? PPCISD::MTVSRZ : PPCISD::MTVSRA;
8250   SDValue Mov = DAG.getNode(MovOpc, dl, MVT::f64, Src);
8251   return convertIntToFP(Op, Mov, DAG, Subtarget);
8252 }
8253 
8254 static SDValue widenVec(SelectionDAG &DAG, SDValue Vec, const SDLoc &dl) {
8255 
8256   EVT VecVT = Vec.getValueType();
8257   assert(VecVT.isVector() && "Expected a vector type.");
8258   assert(VecVT.getSizeInBits() < 128 && "Vector is already full width.");
8259 
8260   EVT EltVT = VecVT.getVectorElementType();
8261   unsigned WideNumElts = 128 / EltVT.getSizeInBits();
8262   EVT WideVT = EVT::getVectorVT(*DAG.getContext(), EltVT, WideNumElts);
8263 
8264   unsigned NumConcat = WideNumElts / VecVT.getVectorNumElements();
8265   SmallVector<SDValue, 16> Ops(NumConcat);
8266   Ops[0] = Vec;
8267   SDValue UndefVec = DAG.getUNDEF(VecVT);
8268   for (unsigned i = 1; i < NumConcat; ++i)
8269     Ops[i] = UndefVec;
8270 
8271   return DAG.getNode(ISD::CONCAT_VECTORS, dl, WideVT, Ops);
8272 }
8273 
8274 SDValue PPCTargetLowering::LowerINT_TO_FPVector(SDValue Op, SelectionDAG &DAG,
8275                                                 const SDLoc &dl) const {
8276   bool IsStrict = Op->isStrictFPOpcode();
8277   unsigned Opc = Op.getOpcode();
8278   SDValue Src = Op.getOperand(IsStrict ? 1 : 0);
8279   assert((Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP ||
8280           Opc == ISD::STRICT_UINT_TO_FP || Opc == ISD::STRICT_SINT_TO_FP) &&
8281          "Unexpected conversion type");
8282   assert((Op.getValueType() == MVT::v2f64 || Op.getValueType() == MVT::v4f32) &&
8283          "Supports conversions to v2f64/v4f32 only.");
8284 
8285   // TODO: Any other flags to propagate?
8286   SDNodeFlags Flags;
8287   Flags.setNoFPExcept(Op->getFlags().hasNoFPExcept());
8288 
8289   bool SignedConv = Opc == ISD::SINT_TO_FP || Opc == ISD::STRICT_SINT_TO_FP;
8290   bool FourEltRes = Op.getValueType() == MVT::v4f32;
8291 
8292   SDValue Wide = widenVec(DAG, Src, dl);
8293   EVT WideVT = Wide.getValueType();
8294   unsigned WideNumElts = WideVT.getVectorNumElements();
8295   MVT IntermediateVT = FourEltRes ? MVT::v4i32 : MVT::v2i64;
8296 
8297   SmallVector<int, 16> ShuffV;
8298   for (unsigned i = 0; i < WideNumElts; ++i)
8299     ShuffV.push_back(i + WideNumElts);
8300 
8301   int Stride = FourEltRes ? WideNumElts / 4 : WideNumElts / 2;
8302   int SaveElts = FourEltRes ? 4 : 2;
8303   if (Subtarget.isLittleEndian())
8304     for (int i = 0; i < SaveElts; i++)
8305       ShuffV[i * Stride] = i;
8306   else
8307     for (int i = 1; i <= SaveElts; i++)
8308       ShuffV[i * Stride - 1] = i - 1;
8309 
8310   SDValue ShuffleSrc2 =
8311       SignedConv ? DAG.getUNDEF(WideVT) : DAG.getConstant(0, dl, WideVT);
8312   SDValue Arrange = DAG.getVectorShuffle(WideVT, dl, Wide, ShuffleSrc2, ShuffV);
8313 
8314   SDValue Extend;
8315   if (SignedConv) {
8316     Arrange = DAG.getBitcast(IntermediateVT, Arrange);
8317     EVT ExtVT = Src.getValueType();
8318     if (Subtarget.hasP9Altivec())
8319       ExtVT = EVT::getVectorVT(*DAG.getContext(), WideVT.getVectorElementType(),
8320                                IntermediateVT.getVectorNumElements());
8321 
8322     Extend = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, IntermediateVT, Arrange,
8323                          DAG.getValueType(ExtVT));
8324   } else
8325     Extend = DAG.getNode(ISD::BITCAST, dl, IntermediateVT, Arrange);
8326 
8327   if (IsStrict)
8328     return DAG.getNode(Opc, dl, DAG.getVTList(Op.getValueType(), MVT::Other),
8329                        {Op.getOperand(0), Extend}, Flags);
8330 
8331   return DAG.getNode(Opc, dl, Op.getValueType(), Extend);
8332 }
8333 
8334 SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
8335                                           SelectionDAG &DAG) const {
8336   SDLoc dl(Op);
8337   bool IsSigned = Op.getOpcode() == ISD::SINT_TO_FP ||
8338                   Op.getOpcode() == ISD::STRICT_SINT_TO_FP;
8339   bool IsStrict = Op->isStrictFPOpcode();
8340   SDValue Src = Op.getOperand(IsStrict ? 1 : 0);
8341   SDValue Chain = IsStrict ? Op.getOperand(0) : DAG.getEntryNode();
8342 
8343   // TODO: Any other flags to propagate?
8344   SDNodeFlags Flags;
8345   Flags.setNoFPExcept(Op->getFlags().hasNoFPExcept());
8346 
8347   EVT InVT = Src.getValueType();
8348   EVT OutVT = Op.getValueType();
8349   if (OutVT.isVector() && OutVT.isFloatingPoint() &&
8350       isOperationCustom(Op.getOpcode(), InVT))
8351     return LowerINT_TO_FPVector(Op, DAG, dl);
8352 
8353   // Conversions to f128 are legal.
8354   if (Op.getValueType() == MVT::f128)
8355     return Subtarget.hasP9Vector() ? Op : SDValue();
8356 
8357   // Don't handle ppc_fp128 here; let it be lowered to a libcall.
8358   if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
8359     return SDValue();
8360 
8361   if (Src.getValueType() == MVT::i1) {
8362     SDValue Sel = DAG.getNode(ISD::SELECT, dl, Op.getValueType(), Src,
8363                               DAG.getConstantFP(1.0, dl, Op.getValueType()),
8364                               DAG.getConstantFP(0.0, dl, Op.getValueType()));
8365     if (IsStrict)
8366       return DAG.getMergeValues({Sel, Chain}, dl);
8367     else
8368       return Sel;
8369   }
8370 
8371   // If we have direct moves, we can do all the conversion, skip the store/load
8372   // however, without FPCVT we can't do most conversions.
8373   if (Subtarget.hasDirectMove() && directMoveIsProfitable(Op) &&
8374       Subtarget.isPPC64() && Subtarget.hasFPCVT())
8375     return LowerINT_TO_FPDirectMove(Op, DAG, dl);
8376 
8377   assert((IsSigned || Subtarget.hasFPCVT()) &&
8378          "UINT_TO_FP is supported only with FPCVT");
8379 
8380   if (Src.getValueType() == MVT::i64) {
8381     SDValue SINT = Src;
8382     // When converting to single-precision, we actually need to convert
8383     // to double-precision first and then round to single-precision.
8384     // To avoid double-rounding effects during that operation, we have
8385     // to prepare the input operand.  Bits that might be truncated when
8386     // converting to double-precision are replaced by a bit that won't
8387     // be lost at this stage, but is below the single-precision rounding
8388     // position.
8389     //
8390     // However, if -enable-unsafe-fp-math is in effect, accept double
8391     // rounding to avoid the extra overhead.
8392     if (Op.getValueType() == MVT::f32 &&
8393         !Subtarget.hasFPCVT() &&
8394         !DAG.getTarget().Options.UnsafeFPMath) {
8395 
8396       // Twiddle input to make sure the low 11 bits are zero.  (If this
8397       // is the case, we are guaranteed the value will fit into the 53 bit
8398       // mantissa of an IEEE double-precision value without rounding.)
8399       // If any of those low 11 bits were not zero originally, make sure
8400       // bit 12 (value 2048) is set instead, so that the final rounding
8401       // to single-precision gets the correct result.
8402       SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
8403                                   SINT, DAG.getConstant(2047, dl, MVT::i64));
8404       Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
8405                           Round, DAG.getConstant(2047, dl, MVT::i64));
8406       Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
8407       Round = DAG.getNode(ISD::AND, dl, MVT::i64,
8408                           Round, DAG.getConstant(-2048, dl, MVT::i64));
8409 
8410       // However, we cannot use that value unconditionally: if the magnitude
8411       // of the input value is small, the bit-twiddling we did above might
8412       // end up visibly changing the output.  Fortunately, in that case, we
8413       // don't need to twiddle bits since the original input will convert
8414       // exactly to double-precision floating-point already.  Therefore,
8415       // construct a conditional to use the original value if the top 11
8416       // bits are all sign-bit copies, and use the rounded value computed
8417       // above otherwise.
8418       SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
8419                                  SINT, DAG.getConstant(53, dl, MVT::i32));
8420       Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
8421                          Cond, DAG.getConstant(1, dl, MVT::i64));
8422       Cond = DAG.getSetCC(
8423           dl,
8424           getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i64),
8425           Cond, DAG.getConstant(1, dl, MVT::i64), ISD::SETUGT);
8426 
8427       SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
8428     }
8429 
8430     ReuseLoadInfo RLI;
8431     SDValue Bits;
8432 
8433     MachineFunction &MF = DAG.getMachineFunction();
8434     if (canReuseLoadAddress(SINT, MVT::i64, RLI, DAG)) {
8435       Bits = DAG.getLoad(MVT::f64, dl, RLI.Chain, RLI.Ptr, RLI.MPI,
8436                          RLI.Alignment, RLI.MMOFlags(), RLI.AAInfo, RLI.Ranges);
8437       spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
8438     } else if (Subtarget.hasLFIWAX() &&
8439                canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::SEXTLOAD)) {
8440       MachineMemOperand *MMO =
8441         MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
8442                                 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
8443       SDValue Ops[] = { RLI.Chain, RLI.Ptr };
8444       Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWAX, dl,
8445                                      DAG.getVTList(MVT::f64, MVT::Other),
8446                                      Ops, MVT::i32, MMO);
8447       spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
8448     } else if (Subtarget.hasFPCVT() &&
8449                canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::ZEXTLOAD)) {
8450       MachineMemOperand *MMO =
8451         MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
8452                                 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
8453       SDValue Ops[] = { RLI.Chain, RLI.Ptr };
8454       Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWZX, dl,
8455                                      DAG.getVTList(MVT::f64, MVT::Other),
8456                                      Ops, MVT::i32, MMO);
8457       spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG);
8458     } else if (((Subtarget.hasLFIWAX() &&
8459                  SINT.getOpcode() == ISD::SIGN_EXTEND) ||
8460                 (Subtarget.hasFPCVT() &&
8461                  SINT.getOpcode() == ISD::ZERO_EXTEND)) &&
8462                SINT.getOperand(0).getValueType() == MVT::i32) {
8463       MachineFrameInfo &MFI = MF.getFrameInfo();
8464       EVT PtrVT = getPointerTy(DAG.getDataLayout());
8465 
8466       int FrameIdx = MFI.CreateStackObject(4, Align(4), false);
8467       SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
8468 
8469       SDValue Store = DAG.getStore(Chain, dl, SINT.getOperand(0), FIdx,
8470                                    MachinePointerInfo::getFixedStack(
8471                                        DAG.getMachineFunction(), FrameIdx));
8472       Chain = Store;
8473 
8474       assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
8475              "Expected an i32 store");
8476 
8477       RLI.Ptr = FIdx;
8478       RLI.Chain = Chain;
8479       RLI.MPI =
8480           MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
8481       RLI.Alignment = Align(4);
8482 
8483       MachineMemOperand *MMO =
8484         MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
8485                                 RLI.Alignment, RLI.AAInfo, RLI.Ranges);
8486       SDValue Ops[] = { RLI.Chain, RLI.Ptr };
8487       Bits = DAG.getMemIntrinsicNode(SINT.getOpcode() == ISD::ZERO_EXTEND ?
8488                                      PPCISD::LFIWZX : PPCISD::LFIWAX,
8489                                      dl, DAG.getVTList(MVT::f64, MVT::Other),
8490                                      Ops, MVT::i32, MMO);
8491       Chain = Bits.getValue(1);
8492     } else
8493       Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
8494 
8495     SDValue FP = convertIntToFP(Op, Bits, DAG, Subtarget, Chain);
8496     if (IsStrict)
8497       Chain = FP.getValue(1);
8498 
8499     if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT()) {
8500       if (IsStrict)
8501         FP = DAG.getNode(ISD::STRICT_FP_ROUND, dl,
8502                          DAG.getVTList(MVT::f32, MVT::Other),
8503                          {Chain, FP, DAG.getIntPtrConstant(0, dl)}, Flags);
8504       else
8505         FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP,
8506                          DAG.getIntPtrConstant(0, dl));
8507     }
8508     return FP;
8509   }
8510 
8511   assert(Src.getValueType() == MVT::i32 &&
8512          "Unhandled INT_TO_FP type in custom expander!");
8513   // Since we only generate this in 64-bit mode, we can take advantage of
8514   // 64-bit registers.  In particular, sign extend the input value into the
8515   // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
8516   // then lfd it and fcfid it.
8517   MachineFunction &MF = DAG.getMachineFunction();
8518   MachineFrameInfo &MFI = MF.getFrameInfo();
8519   EVT PtrVT = getPointerTy(MF.getDataLayout());
8520 
8521   SDValue Ld;
8522   if (Subtarget.hasLFIWAX() || Subtarget.hasFPCVT()) {
8523     ReuseLoadInfo RLI;
8524     bool ReusingLoad;
8525     if (!(ReusingLoad = canReuseLoadAddress(Src, MVT::i32, RLI, DAG))) {
8526       int FrameIdx = MFI.CreateStackObject(4, Align(4), false);
8527       SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
8528 
8529       SDValue Store = DAG.getStore(Chain, dl, Src, FIdx,
8530                                    MachinePointerInfo::getFixedStack(
8531                                        DAG.getMachineFunction(), FrameIdx));
8532       Chain = Store;
8533 
8534       assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
8535              "Expected an i32 store");
8536 
8537       RLI.Ptr = FIdx;
8538       RLI.Chain = Chain;
8539       RLI.MPI =
8540           MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
8541       RLI.Alignment = Align(4);
8542     }
8543 
8544     MachineMemOperand *MMO =
8545       MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4,
8546                               RLI.Alignment, RLI.AAInfo, RLI.Ranges);
8547     SDValue Ops[] = { RLI.Chain, RLI.Ptr };
8548     Ld = DAG.getMemIntrinsicNode(IsSigned ? PPCISD::LFIWAX : PPCISD::LFIWZX, dl,
8549                                  DAG.getVTList(MVT::f64, MVT::Other), Ops,
8550                                  MVT::i32, MMO);
8551     Chain = Ld.getValue(1);
8552     if (ReusingLoad)
8553       spliceIntoChain(RLI.ResChain, Ld.getValue(1), DAG);
8554   } else {
8555     assert(Subtarget.isPPC64() &&
8556            "i32->FP without LFIWAX supported only on PPC64");
8557 
8558     int FrameIdx = MFI.CreateStackObject(8, Align(8), false);
8559     SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
8560 
8561     SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64, Src);
8562 
8563     // STD the extended value into the stack slot.
8564     SDValue Store = DAG.getStore(
8565         Chain, dl, Ext64, FIdx,
8566         MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx));
8567     Chain = Store;
8568 
8569     // Load the value as a double.
8570     Ld = DAG.getLoad(
8571         MVT::f64, dl, Chain, FIdx,
8572         MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx));
8573     Chain = Ld.getValue(1);
8574   }
8575 
8576   // FCFID it and return it.
8577   SDValue FP = convertIntToFP(Op, Ld, DAG, Subtarget, Chain);
8578   if (IsStrict)
8579     Chain = FP.getValue(1);
8580   if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT()) {
8581     if (IsStrict)
8582       FP = DAG.getNode(ISD::STRICT_FP_ROUND, dl,
8583                        DAG.getVTList(MVT::f32, MVT::Other),
8584                        {Chain, FP, DAG.getIntPtrConstant(0, dl)}, Flags);
8585     else
8586       FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP,
8587                        DAG.getIntPtrConstant(0, dl));
8588   }
8589   return FP;
8590 }
8591 
8592 SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8593                                             SelectionDAG &DAG) const {
8594   SDLoc dl(Op);
8595   /*
8596    The rounding mode is in bits 30:31 of FPSR, and has the following
8597    settings:
8598      00 Round to nearest
8599      01 Round to 0
8600      10 Round to +inf
8601      11 Round to -inf
8602 
8603   FLT_ROUNDS, on the other hand, expects the following:
8604     -1 Undefined
8605      0 Round to 0
8606      1 Round to nearest
8607      2 Round to +inf
8608      3 Round to -inf
8609 
8610   To perform the conversion, we do:
8611     ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
8612   */
8613 
8614   MachineFunction &MF = DAG.getMachineFunction();
8615   EVT VT = Op.getValueType();
8616   EVT PtrVT = getPointerTy(MF.getDataLayout());
8617 
8618   // Save FP Control Word to register
8619   SDValue Chain = Op.getOperand(0);
8620   SDValue MFFS = DAG.getNode(PPCISD::MFFS, dl, {MVT::f64, MVT::Other}, Chain);
8621   Chain = MFFS.getValue(1);
8622 
8623   SDValue CWD;
8624   if (isTypeLegal(MVT::i64)) {
8625     CWD = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32,
8626                       DAG.getNode(ISD::BITCAST, dl, MVT::i64, MFFS));
8627   } else {
8628     // Save FP register to stack slot
8629     int SSFI = MF.getFrameInfo().CreateStackObject(8, Align(8), false);
8630     SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
8631     Chain = DAG.getStore(Chain, dl, MFFS, StackSlot, MachinePointerInfo());
8632 
8633     // Load FP Control Word from low 32 bits of stack slot.
8634     assert(hasBigEndianPartOrdering(MVT::i64, MF.getDataLayout()) &&
8635            "Stack slot adjustment is valid only on big endian subtargets!");
8636     SDValue Four = DAG.getConstant(4, dl, PtrVT);
8637     SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
8638     CWD = DAG.getLoad(MVT::i32, dl, Chain, Addr, MachinePointerInfo());
8639     Chain = CWD.getValue(1);
8640   }
8641 
8642   // Transform as necessary
8643   SDValue CWD1 =
8644     DAG.getNode(ISD::AND, dl, MVT::i32,
8645                 CWD, DAG.getConstant(3, dl, MVT::i32));
8646   SDValue CWD2 =
8647     DAG.getNode(ISD::SRL, dl, MVT::i32,
8648                 DAG.getNode(ISD::AND, dl, MVT::i32,
8649                             DAG.getNode(ISD::XOR, dl, MVT::i32,
8650                                         CWD, DAG.getConstant(3, dl, MVT::i32)),
8651                             DAG.getConstant(3, dl, MVT::i32)),
8652                 DAG.getConstant(1, dl, MVT::i32));
8653 
8654   SDValue RetVal =
8655     DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
8656 
8657   RetVal =
8658       DAG.getNode((VT.getSizeInBits() < 16 ? ISD::TRUNCATE : ISD::ZERO_EXTEND),
8659                   dl, VT, RetVal);
8660 
8661   return DAG.getMergeValues({RetVal, Chain}, dl);
8662 }
8663 
8664 SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
8665   EVT VT = Op.getValueType();
8666   unsigned BitWidth = VT.getSizeInBits();
8667   SDLoc dl(Op);
8668   assert(Op.getNumOperands() == 3 &&
8669          VT == Op.getOperand(1).getValueType() &&
8670          "Unexpected SHL!");
8671 
8672   // Expand into a bunch of logical ops.  Note that these ops
8673   // depend on the PPC behavior for oversized shift amounts.
8674   SDValue Lo = Op.getOperand(0);
8675   SDValue Hi = Op.getOperand(1);
8676   SDValue Amt = Op.getOperand(2);
8677   EVT AmtVT = Amt.getValueType();
8678 
8679   SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
8680                              DAG.getConstant(BitWidth, dl, AmtVT), Amt);
8681   SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
8682   SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
8683   SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
8684   SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
8685                              DAG.getConstant(-BitWidth, dl, AmtVT));
8686   SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
8687   SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
8688   SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
8689   SDValue OutOps[] = { OutLo, OutHi };
8690   return DAG.getMergeValues(OutOps, dl);
8691 }
8692 
8693 SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
8694   EVT VT = Op.getValueType();
8695   SDLoc dl(Op);
8696   unsigned BitWidth = VT.getSizeInBits();
8697   assert(Op.getNumOperands() == 3 &&
8698          VT == Op.getOperand(1).getValueType() &&
8699          "Unexpected SRL!");
8700 
8701   // Expand into a bunch of logical ops.  Note that these ops
8702   // depend on the PPC behavior for oversized shift amounts.
8703   SDValue Lo = Op.getOperand(0);
8704   SDValue Hi = Op.getOperand(1);
8705   SDValue Amt = Op.getOperand(2);
8706   EVT AmtVT = Amt.getValueType();
8707 
8708   SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
8709                              DAG.getConstant(BitWidth, dl, AmtVT), Amt);
8710   SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
8711   SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
8712   SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
8713   SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
8714                              DAG.getConstant(-BitWidth, dl, AmtVT));
8715   SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
8716   SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
8717   SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
8718   SDValue OutOps[] = { OutLo, OutHi };
8719   return DAG.getMergeValues(OutOps, dl);
8720 }
8721 
8722 SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
8723   SDLoc dl(Op);
8724   EVT VT = Op.getValueType();
8725   unsigned BitWidth = VT.getSizeInBits();
8726   assert(Op.getNumOperands() == 3 &&
8727          VT == Op.getOperand(1).getValueType() &&
8728          "Unexpected SRA!");
8729 
8730   // Expand into a bunch of logical ops, followed by a select_cc.
8731   SDValue Lo = Op.getOperand(0);
8732   SDValue Hi = Op.getOperand(1);
8733   SDValue Amt = Op.getOperand(2);
8734   EVT AmtVT = Amt.getValueType();
8735 
8736   SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
8737                              DAG.getConstant(BitWidth, dl, AmtVT), Amt);
8738   SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
8739   SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
8740   SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
8741   SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
8742                              DAG.getConstant(-BitWidth, dl, AmtVT));
8743   SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
8744   SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
8745   SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, dl, AmtVT),
8746                                   Tmp4, Tmp6, ISD::SETLE);
8747   SDValue OutOps[] = { OutLo, OutHi };
8748   return DAG.getMergeValues(OutOps, dl);
8749 }
8750 
8751 SDValue PPCTargetLowering::LowerFunnelShift(SDValue Op,
8752                                             SelectionDAG &DAG) const {
8753   SDLoc dl(Op);
8754   EVT VT = Op.getValueType();
8755   unsigned BitWidth = VT.getSizeInBits();
8756 
8757   bool IsFSHL = Op.getOpcode() == ISD::FSHL;
8758   SDValue X = Op.getOperand(0);
8759   SDValue Y = Op.getOperand(1);
8760   SDValue Z = Op.getOperand(2);
8761   EVT AmtVT = Z.getValueType();
8762 
8763   // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
8764   // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
8765   // This is simpler than TargetLowering::expandFunnelShift because we can rely
8766   // on PowerPC shift by BW being well defined.
8767   Z = DAG.getNode(ISD::AND, dl, AmtVT, Z,
8768                   DAG.getConstant(BitWidth - 1, dl, AmtVT));
8769   SDValue SubZ =
8770       DAG.getNode(ISD::SUB, dl, AmtVT, DAG.getConstant(BitWidth, dl, AmtVT), Z);
8771   X = DAG.getNode(PPCISD::SHL, dl, VT, X, IsFSHL ? Z : SubZ);
8772   Y = DAG.getNode(PPCISD::SRL, dl, VT, Y, IsFSHL ? SubZ : Z);
8773   return DAG.getNode(ISD::OR, dl, VT, X, Y);
8774 }
8775 
8776 //===----------------------------------------------------------------------===//
8777 // Vector related lowering.
8778 //
8779 
8780 /// getCanonicalConstSplat - Build a canonical splat immediate of Val with an
8781 /// element size of SplatSize. Cast the result to VT.
8782 static SDValue getCanonicalConstSplat(uint64_t Val, unsigned SplatSize, EVT VT,
8783                                       SelectionDAG &DAG, const SDLoc &dl) {
8784   static const MVT VTys[] = { // canonical VT to use for each size.
8785     MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
8786   };
8787 
8788   EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
8789 
8790   // For a splat with all ones, turn it to vspltisb 0xFF to canonicalize.
8791   if (Val == ((1LLU << (SplatSize * 8)) - 1)) {
8792     SplatSize = 1;
8793     Val = 0xFF;
8794   }
8795 
8796   EVT CanonicalVT = VTys[SplatSize-1];
8797 
8798   // Build a canonical splat for this value.
8799   return DAG.getBitcast(ReqVT, DAG.getConstant(Val, dl, CanonicalVT));
8800 }
8801 
8802 /// BuildIntrinsicOp - Return a unary operator intrinsic node with the
8803 /// specified intrinsic ID.
8804 static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op, SelectionDAG &DAG,
8805                                 const SDLoc &dl, EVT DestVT = MVT::Other) {
8806   if (DestVT == MVT::Other) DestVT = Op.getValueType();
8807   return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
8808                      DAG.getConstant(IID, dl, MVT::i32), Op);
8809 }
8810 
8811 /// BuildIntrinsicOp - Return a binary operator intrinsic node with the
8812 /// specified intrinsic ID.
8813 static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
8814                                 SelectionDAG &DAG, const SDLoc &dl,
8815                                 EVT DestVT = MVT::Other) {
8816   if (DestVT == MVT::Other) DestVT = LHS.getValueType();
8817   return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
8818                      DAG.getConstant(IID, dl, MVT::i32), LHS, RHS);
8819 }
8820 
8821 /// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
8822 /// specified intrinsic ID.
8823 static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
8824                                 SDValue Op2, SelectionDAG &DAG, const SDLoc &dl,
8825                                 EVT DestVT = MVT::Other) {
8826   if (DestVT == MVT::Other) DestVT = Op0.getValueType();
8827   return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
8828                      DAG.getConstant(IID, dl, MVT::i32), Op0, Op1, Op2);
8829 }
8830 
8831 /// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
8832 /// amount.  The result has the specified value type.
8833 static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt, EVT VT,
8834                            SelectionDAG &DAG, const SDLoc &dl) {
8835   // Force LHS/RHS to be the right type.
8836   LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
8837   RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
8838 
8839   int Ops[16];
8840   for (unsigned i = 0; i != 16; ++i)
8841     Ops[i] = i + Amt;
8842   SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
8843   return DAG.getNode(ISD::BITCAST, dl, VT, T);
8844 }
8845 
8846 /// Do we have an efficient pattern in a .td file for this node?
8847 ///
8848 /// \param V - pointer to the BuildVectorSDNode being matched
8849 /// \param HasDirectMove - does this subtarget have VSR <-> GPR direct moves?
8850 ///
8851 /// There are some patterns where it is beneficial to keep a BUILD_VECTOR
8852 /// node as a BUILD_VECTOR node rather than expanding it. The patterns where
8853 /// the opposite is true (expansion is beneficial) are:
8854 /// - The node builds a vector out of integers that are not 32 or 64-bits
8855 /// - The node builds a vector out of constants
8856 /// - The node is a "load-and-splat"
8857 /// In all other cases, we will choose to keep the BUILD_VECTOR.
8858 static bool haveEfficientBuildVectorPattern(BuildVectorSDNode *V,
8859                                             bool HasDirectMove,
8860                                             bool HasP8Vector) {
8861   EVT VecVT = V->getValueType(0);
8862   bool RightType = VecVT == MVT::v2f64 ||
8863     (HasP8Vector && VecVT == MVT::v4f32) ||
8864     (HasDirectMove && (VecVT == MVT::v2i64 || VecVT == MVT::v4i32));
8865   if (!RightType)
8866     return false;
8867 
8868   bool IsSplat = true;
8869   bool IsLoad = false;
8870   SDValue Op0 = V->getOperand(0);
8871 
8872   // This function is called in a block that confirms the node is not a constant
8873   // splat. So a constant BUILD_VECTOR here means the vector is built out of
8874   // different constants.
8875   if (V->isConstant())
8876     return false;
8877   for (int i = 0, e = V->getNumOperands(); i < e; ++i) {
8878     if (V->getOperand(i).isUndef())
8879       return false;
8880     // We want to expand nodes that represent load-and-splat even if the
8881     // loaded value is a floating point truncation or conversion to int.
8882     if (V->getOperand(i).getOpcode() == ISD::LOAD ||
8883         (V->getOperand(i).getOpcode() == ISD::FP_ROUND &&
8884          V->getOperand(i).getOperand(0).getOpcode() == ISD::LOAD) ||
8885         (V->getOperand(i).getOpcode() == ISD::FP_TO_SINT &&
8886          V->getOperand(i).getOperand(0).getOpcode() == ISD::LOAD) ||
8887         (V->getOperand(i).getOpcode() == ISD::FP_TO_UINT &&
8888          V->getOperand(i).getOperand(0).getOpcode() == ISD::LOAD))
8889       IsLoad = true;
8890     // If the operands are different or the input is not a load and has more
8891     // uses than just this BV node, then it isn't a splat.
8892     if (V->getOperand(i) != Op0 ||
8893         (!IsLoad && !V->isOnlyUserOf(V->getOperand(i).getNode())))
8894       IsSplat = false;
8895   }
8896   return !(IsSplat && IsLoad);
8897 }
8898 
8899 // Lower BITCAST(f128, (build_pair i64, i64)) to BUILD_FP128.
8900 SDValue PPCTargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
8901 
8902   SDLoc dl(Op);
8903   SDValue Op0 = Op->getOperand(0);
8904 
8905   if ((Op.getValueType() != MVT::f128) ||
8906       (Op0.getOpcode() != ISD::BUILD_PAIR) ||
8907       (Op0.getOperand(0).getValueType() != MVT::i64) ||
8908       (Op0.getOperand(1).getValueType() != MVT::i64))
8909     return SDValue();
8910 
8911   return DAG.getNode(PPCISD::BUILD_FP128, dl, MVT::f128, Op0.getOperand(0),
8912                      Op0.getOperand(1));
8913 }
8914 
8915 static const SDValue *getNormalLoadInput(const SDValue &Op, bool &IsPermuted) {
8916   const SDValue *InputLoad = &Op;
8917   if (InputLoad->getOpcode() == ISD::BITCAST)
8918     InputLoad = &InputLoad->getOperand(0);
8919   if (InputLoad->getOpcode() == ISD::SCALAR_TO_VECTOR ||
8920       InputLoad->getOpcode() == PPCISD::SCALAR_TO_VECTOR_PERMUTED) {
8921     IsPermuted = InputLoad->getOpcode() == PPCISD::SCALAR_TO_VECTOR_PERMUTED;
8922     InputLoad = &InputLoad->getOperand(0);
8923   }
8924   if (InputLoad->getOpcode() != ISD::LOAD)
8925     return nullptr;
8926   LoadSDNode *LD = cast<LoadSDNode>(*InputLoad);
8927   return ISD::isNormalLoad(LD) ? InputLoad : nullptr;
8928 }
8929 
8930 // Convert the argument APFloat to a single precision APFloat if there is no
8931 // loss in information during the conversion to single precision APFloat and the
8932 // resulting number is not a denormal number. Return true if successful.
8933 bool llvm::convertToNonDenormSingle(APFloat &ArgAPFloat) {
8934   APFloat APFloatToConvert = ArgAPFloat;
8935   bool LosesInfo = true;
8936   APFloatToConvert.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
8937                            &LosesInfo);
8938   bool Success = (!LosesInfo && !APFloatToConvert.isDenormal());
8939   if (Success)
8940     ArgAPFloat = APFloatToConvert;
8941   return Success;
8942 }
8943 
8944 // Bitcast the argument APInt to a double and convert it to a single precision
8945 // APFloat, bitcast the APFloat to an APInt and assign it to the original
8946 // argument if there is no loss in information during the conversion from
8947 // double to single precision APFloat and the resulting number is not a denormal
8948 // number. Return true if successful.
8949 bool llvm::convertToNonDenormSingle(APInt &ArgAPInt) {
8950   double DpValue = ArgAPInt.bitsToDouble();
8951   APFloat APFloatDp(DpValue);
8952   bool Success = convertToNonDenormSingle(APFloatDp);
8953   if (Success)
8954     ArgAPInt = APFloatDp.bitcastToAPInt();
8955   return Success;
8956 }
8957 
8958 // Nondestructive check for convertTonNonDenormSingle.
8959 bool llvm::checkConvertToNonDenormSingle(APFloat &ArgAPFloat) {
8960   // Only convert if it loses info, since XXSPLTIDP should
8961   // handle the other case.
8962   APFloat APFloatToConvert = ArgAPFloat;
8963   bool LosesInfo = true;
8964   APFloatToConvert.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
8965                            &LosesInfo);
8966 
8967   return (!LosesInfo && !APFloatToConvert.isDenormal());
8968 }
8969 
8970 // If this is a case we can't handle, return null and let the default
8971 // expansion code take care of it.  If we CAN select this case, and if it
8972 // selects to a single instruction, return Op.  Otherwise, if we can codegen
8973 // this case more efficiently than a constant pool load, lower it to the
8974 // sequence of ops that should be used.
8975 SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
8976                                              SelectionDAG &DAG) const {
8977   SDLoc dl(Op);
8978   BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8979   assert(BVN && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
8980 
8981   // Check if this is a splat of a constant value.
8982   APInt APSplatBits, APSplatUndef;
8983   unsigned SplatBitSize;
8984   bool HasAnyUndefs;
8985   bool BVNIsConstantSplat =
8986       BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
8987                            HasAnyUndefs, 0, !Subtarget.isLittleEndian());
8988 
8989   // If it is a splat of a double, check if we can shrink it to a 32 bit
8990   // non-denormal float which when converted back to double gives us the same
8991   // double. This is to exploit the XXSPLTIDP instruction.
8992   // If we lose precision, we use XXSPLTI32DX.
8993   if (BVNIsConstantSplat && (SplatBitSize == 64) &&
8994       Subtarget.hasPrefixInstrs()) {
8995     // Check the type first to short-circuit so we don't modify APSplatBits if
8996     // this block isn't executed.
8997     if ((Op->getValueType(0) == MVT::v2f64) &&
8998         convertToNonDenormSingle(APSplatBits)) {
8999       SDValue SplatNode = DAG.getNode(
9000           PPCISD::XXSPLTI_SP_TO_DP, dl, MVT::v2f64,
9001           DAG.getTargetConstant(APSplatBits.getZExtValue(), dl, MVT::i32));
9002       return DAG.getBitcast(Op.getValueType(), SplatNode);
9003     } else {
9004       // We may lose precision, so we have to use XXSPLTI32DX.
9005 
9006       uint32_t Hi =
9007           (uint32_t)((APSplatBits.getZExtValue() & 0xFFFFFFFF00000000LL) >> 32);
9008       uint32_t Lo =
9009           (uint32_t)(APSplatBits.getZExtValue() & 0xFFFFFFFF);
9010       SDValue SplatNode = DAG.getUNDEF(MVT::v2i64);
9011 
9012       if (!Hi || !Lo)
9013         // If either load is 0, then we should generate XXLXOR to set to 0.
9014         SplatNode = DAG.getTargetConstant(0, dl, MVT::v2i64);
9015 
9016       if (Hi)
9017         SplatNode = DAG.getNode(
9018             PPCISD::XXSPLTI32DX, dl, MVT::v2i64, SplatNode,
9019             DAG.getTargetConstant(0, dl, MVT::i32),
9020             DAG.getTargetConstant(Hi, dl, MVT::i32));
9021 
9022       if (Lo)
9023         SplatNode =
9024             DAG.getNode(PPCISD::XXSPLTI32DX, dl, MVT::v2i64, SplatNode,
9025                         DAG.getTargetConstant(1, dl, MVT::i32),
9026                         DAG.getTargetConstant(Lo, dl, MVT::i32));
9027 
9028       return DAG.getBitcast(Op.getValueType(), SplatNode);
9029     }
9030   }
9031 
9032   if (!BVNIsConstantSplat || SplatBitSize > 32) {
9033 
9034     bool IsPermutedLoad = false;
9035     const SDValue *InputLoad =
9036         getNormalLoadInput(Op.getOperand(0), IsPermutedLoad);
9037     // Handle load-and-splat patterns as we have instructions that will do this
9038     // in one go.
9039     if (InputLoad && DAG.isSplatValue(Op, true)) {
9040       LoadSDNode *LD = cast<LoadSDNode>(*InputLoad);
9041 
9042       // We have handling for 4 and 8 byte elements.
9043       unsigned ElementSize = LD->getMemoryVT().getScalarSizeInBits();
9044 
9045       // Checking for a single use of this load, we have to check for vector
9046       // width (128 bits) / ElementSize uses (since each operand of the
9047       // BUILD_VECTOR is a separate use of the value.
9048       unsigned NumUsesOfInputLD = 128 / ElementSize;
9049       for (SDValue BVInOp : Op->ops())
9050         if (BVInOp.isUndef())
9051           NumUsesOfInputLD--;
9052       assert(NumUsesOfInputLD > 0 && "No uses of input LD of a build_vector?");
9053       if (InputLoad->getNode()->hasNUsesOfValue(NumUsesOfInputLD, 0) &&
9054           ((Subtarget.hasVSX() && ElementSize == 64) ||
9055            (Subtarget.hasP9Vector() && ElementSize == 32))) {
9056         SDValue Ops[] = {
9057           LD->getChain(),    // Chain
9058           LD->getBasePtr(),  // Ptr
9059           DAG.getValueType(Op.getValueType()) // VT
9060         };
9061         SDValue LdSplt = DAG.getMemIntrinsicNode(
9062             PPCISD::LD_SPLAT, dl, DAG.getVTList(Op.getValueType(), MVT::Other),
9063             Ops, LD->getMemoryVT(), LD->getMemOperand());
9064         // Replace all uses of the output chain of the original load with the
9065         // output chain of the new load.
9066         DAG.ReplaceAllUsesOfValueWith(InputLoad->getValue(1),
9067                                       LdSplt.getValue(1));
9068         return LdSplt;
9069       }
9070     }
9071 
9072     // In 64BIT mode BUILD_VECTOR nodes that are not constant splats of up to
9073     // 32-bits can be lowered to VSX instructions under certain conditions.
9074     // Without VSX, there is no pattern more efficient than expanding the node.
9075     if (Subtarget.hasVSX() && Subtarget.isPPC64() &&
9076         haveEfficientBuildVectorPattern(BVN, Subtarget.hasDirectMove(),
9077                                         Subtarget.hasP8Vector()))
9078       return Op;
9079     return SDValue();
9080   }
9081 
9082   uint64_t SplatBits = APSplatBits.getZExtValue();
9083   uint64_t SplatUndef = APSplatUndef.getZExtValue();
9084   unsigned SplatSize = SplatBitSize / 8;
9085 
9086   // First, handle single instruction cases.
9087 
9088   // All zeros?
9089   if (SplatBits == 0) {
9090     // Canonicalize all zero vectors to be v4i32.
9091     if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
9092       SDValue Z = DAG.getConstant(0, dl, MVT::v4i32);
9093       Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
9094     }
9095     return Op;
9096   }
9097 
9098   // We have XXSPLTIW for constant splats four bytes wide.
9099   // Given vector length is a multiple of 4, 2-byte splats can be replaced
9100   // with 4-byte splats. We replicate the SplatBits in case of 2-byte splat to
9101   // make a 4-byte splat element. For example: 2-byte splat of 0xABAB can be
9102   // turned into a 4-byte splat of 0xABABABAB.
9103   if (Subtarget.hasPrefixInstrs() && SplatSize == 2)
9104     return getCanonicalConstSplat(SplatBits | (SplatBits << 16), SplatSize * 2,
9105                                   Op.getValueType(), DAG, dl);
9106 
9107   if (Subtarget.hasPrefixInstrs() && SplatSize == 4)
9108     return getCanonicalConstSplat(SplatBits, SplatSize, Op.getValueType(), DAG,
9109                                   dl);
9110 
9111   // We have XXSPLTIB for constant splats one byte wide.
9112   if (Subtarget.hasP9Vector() && SplatSize == 1)
9113     return getCanonicalConstSplat(SplatBits, SplatSize, Op.getValueType(), DAG,
9114                                   dl);
9115 
9116   // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
9117   int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
9118                     (32-SplatBitSize));
9119   if (SextVal >= -16 && SextVal <= 15)
9120     return getCanonicalConstSplat(SextVal, SplatSize, Op.getValueType(), DAG,
9121                                   dl);
9122 
9123   // Two instruction sequences.
9124 
9125   // If this value is in the range [-32,30] and is even, use:
9126   //     VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
9127   // If this value is in the range [17,31] and is odd, use:
9128   //     VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
9129   // If this value is in the range [-31,-17] and is odd, use:
9130   //     VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
9131   // Note the last two are three-instruction sequences.
9132   if (SextVal >= -32 && SextVal <= 31) {
9133     // To avoid having these optimizations undone by constant folding,
9134     // we convert to a pseudo that will be expanded later into one of
9135     // the above forms.
9136     SDValue Elt = DAG.getConstant(SextVal, dl, MVT::i32);
9137     EVT VT = (SplatSize == 1 ? MVT::v16i8 :
9138               (SplatSize == 2 ? MVT::v8i16 : MVT::v4i32));
9139     SDValue EltSize = DAG.getConstant(SplatSize, dl, MVT::i32);
9140     SDValue RetVal = DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
9141     if (VT == Op.getValueType())
9142       return RetVal;
9143     else
9144       return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), RetVal);
9145   }
9146 
9147   // If this is 0x8000_0000 x 4, turn into vspltisw + vslw.  If it is
9148   // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000).  This is important
9149   // for fneg/fabs.
9150   if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
9151     // Make -1 and vspltisw -1:
9152     SDValue OnesV = getCanonicalConstSplat(-1, 4, MVT::v4i32, DAG, dl);
9153 
9154     // Make the VSLW intrinsic, computing 0x8000_0000.
9155     SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
9156                                    OnesV, DAG, dl);
9157 
9158     // xor by OnesV to invert it.
9159     Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
9160     return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
9161   }
9162 
9163   // Check to see if this is a wide variety of vsplti*, binop self cases.
9164   static const signed char SplatCsts[] = {
9165     -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
9166     -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
9167   };
9168 
9169   for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
9170     // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
9171     // cases which are ambiguous (e.g. formation of 0x8000_0000).  'vsplti -1'
9172     int i = SplatCsts[idx];
9173 
9174     // Figure out what shift amount will be used by altivec if shifted by i in
9175     // this splat size.
9176     unsigned TypeShiftAmt = i & (SplatBitSize-1);
9177 
9178     // vsplti + shl self.
9179     if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
9180       SDValue Res = getCanonicalConstSplat(i, SplatSize, MVT::Other, DAG, dl);
9181       static const unsigned IIDs[] = { // Intrinsic to use for each size.
9182         Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
9183         Intrinsic::ppc_altivec_vslw
9184       };
9185       Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
9186       return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
9187     }
9188 
9189     // vsplti + srl self.
9190     if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
9191       SDValue Res = getCanonicalConstSplat(i, SplatSize, MVT::Other, DAG, dl);
9192       static const unsigned IIDs[] = { // Intrinsic to use for each size.
9193         Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
9194         Intrinsic::ppc_altivec_vsrw
9195       };
9196       Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
9197       return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
9198     }
9199 
9200     // vsplti + rol self.
9201     if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
9202                          ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
9203       SDValue Res = getCanonicalConstSplat(i, SplatSize, MVT::Other, DAG, dl);
9204       static const unsigned IIDs[] = { // Intrinsic to use for each size.
9205         Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
9206         Intrinsic::ppc_altivec_vrlw
9207       };
9208       Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
9209       return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
9210     }
9211 
9212     // t = vsplti c, result = vsldoi t, t, 1
9213     if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
9214       SDValue T = getCanonicalConstSplat(i, SplatSize, MVT::v16i8, DAG, dl);
9215       unsigned Amt = Subtarget.isLittleEndian() ? 15 : 1;
9216       return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl);
9217     }
9218     // t = vsplti c, result = vsldoi t, t, 2
9219     if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
9220       SDValue T = getCanonicalConstSplat(i, SplatSize, MVT::v16i8, DAG, dl);
9221       unsigned Amt = Subtarget.isLittleEndian() ? 14 : 2;
9222       return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl);
9223     }
9224     // t = vsplti c, result = vsldoi t, t, 3
9225     if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
9226       SDValue T = getCanonicalConstSplat(i, SplatSize, MVT::v16i8, DAG, dl);
9227       unsigned Amt = Subtarget.isLittleEndian() ? 13 : 3;
9228       return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl);
9229     }
9230   }
9231 
9232   return SDValue();
9233 }
9234 
9235 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
9236 /// the specified operations to build the shuffle.
9237 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
9238                                       SDValue RHS, SelectionDAG &DAG,
9239                                       const SDLoc &dl) {
9240   unsigned OpNum = (PFEntry >> 26) & 0x0F;
9241   unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
9242   unsigned RHSID = (PFEntry >>  0) & ((1 << 13)-1);
9243 
9244   enum {
9245     OP_COPY = 0,  // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
9246     OP_VMRGHW,
9247     OP_VMRGLW,
9248     OP_VSPLTISW0,
9249     OP_VSPLTISW1,
9250     OP_VSPLTISW2,
9251     OP_VSPLTISW3,
9252     OP_VSLDOI4,
9253     OP_VSLDOI8,
9254     OP_VSLDOI12
9255   };
9256 
9257   if (OpNum == OP_COPY) {
9258     if (LHSID == (1*9+2)*9+3) return LHS;
9259     assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
9260     return RHS;
9261   }
9262 
9263   SDValue OpLHS, OpRHS;
9264   OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
9265   OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
9266 
9267   int ShufIdxs[16];
9268   switch (OpNum) {
9269   default: llvm_unreachable("Unknown i32 permute!");
9270   case OP_VMRGHW:
9271     ShufIdxs[ 0] =  0; ShufIdxs[ 1] =  1; ShufIdxs[ 2] =  2; ShufIdxs[ 3] =  3;
9272     ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
9273     ShufIdxs[ 8] =  4; ShufIdxs[ 9] =  5; ShufIdxs[10] =  6; ShufIdxs[11] =  7;
9274     ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
9275     break;
9276   case OP_VMRGLW:
9277     ShufIdxs[ 0] =  8; ShufIdxs[ 1] =  9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
9278     ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
9279     ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
9280     ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
9281     break;
9282   case OP_VSPLTISW0:
9283     for (unsigned i = 0; i != 16; ++i)
9284       ShufIdxs[i] = (i&3)+0;
9285     break;
9286   case OP_VSPLTISW1:
9287     for (unsigned i = 0; i != 16; ++i)
9288       ShufIdxs[i] = (i&3)+4;
9289     break;
9290   case OP_VSPLTISW2:
9291     for (unsigned i = 0; i != 16; ++i)
9292       ShufIdxs[i] = (i&3)+8;
9293     break;
9294   case OP_VSPLTISW3:
9295     for (unsigned i = 0; i != 16; ++i)
9296       ShufIdxs[i] = (i&3)+12;
9297     break;
9298   case OP_VSLDOI4:
9299     return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
9300   case OP_VSLDOI8:
9301     return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
9302   case OP_VSLDOI12:
9303     return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
9304   }
9305   EVT VT = OpLHS.getValueType();
9306   OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
9307   OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
9308   SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
9309   return DAG.getNode(ISD::BITCAST, dl, VT, T);
9310 }
9311 
9312 /// lowerToVINSERTB - Return the SDValue if this VECTOR_SHUFFLE can be handled
9313 /// by the VINSERTB instruction introduced in ISA 3.0, else just return default
9314 /// SDValue.
9315 SDValue PPCTargetLowering::lowerToVINSERTB(ShuffleVectorSDNode *N,
9316                                            SelectionDAG &DAG) const {
9317   const unsigned BytesInVector = 16;
9318   bool IsLE = Subtarget.isLittleEndian();
9319   SDLoc dl(N);
9320   SDValue V1 = N->getOperand(0);
9321   SDValue V2 = N->getOperand(1);
9322   unsigned ShiftElts = 0, InsertAtByte = 0;
9323   bool Swap = false;
9324 
9325   // Shifts required to get the byte we want at element 7.
9326   unsigned LittleEndianShifts[] = {8, 7,  6,  5,  4,  3,  2,  1,
9327                                    0, 15, 14, 13, 12, 11, 10, 9};
9328   unsigned BigEndianShifts[] = {9, 10, 11, 12, 13, 14, 15, 0,
9329                                 1, 2,  3,  4,  5,  6,  7,  8};
9330 
9331   ArrayRef<int> Mask = N->getMask();
9332   int OriginalOrder[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
9333 
9334   // For each mask element, find out if we're just inserting something
9335   // from V2 into V1 or vice versa.
9336   // Possible permutations inserting an element from V2 into V1:
9337   //   X, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
9338   //   0, X, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
9339   //   ...
9340   //   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, X
9341   // Inserting from V1 into V2 will be similar, except mask range will be
9342   // [16,31].
9343 
9344   bool FoundCandidate = false;
9345   // If both vector operands for the shuffle are the same vector, the mask
9346   // will contain only elements from the first one and the second one will be
9347   // undef.
9348   unsigned VINSERTBSrcElem = IsLE ? 8 : 7;
9349   // Go through the mask of half-words to find an element that's being moved
9350   // from one vector to the other.
9351   for (unsigned i = 0; i < BytesInVector; ++i) {
9352     unsigned CurrentElement = Mask[i];
9353     // If 2nd operand is undefined, we should only look for element 7 in the
9354     // Mask.
9355     if (V2.isUndef() && CurrentElement != VINSERTBSrcElem)
9356       continue;
9357 
9358     bool OtherElementsInOrder = true;
9359     // Examine the other elements in the Mask to see if they're in original
9360     // order.
9361     for (unsigned j = 0; j < BytesInVector; ++j) {
9362       if (j == i)
9363         continue;
9364       // If CurrentElement is from V1 [0,15], then we the rest of the Mask to be
9365       // from V2 [16,31] and vice versa.  Unless the 2nd operand is undefined,
9366       // in which we always assume we're always picking from the 1st operand.
9367       int MaskOffset =
9368           (!V2.isUndef() && CurrentElement < BytesInVector) ? BytesInVector : 0;
9369       if (Mask[j] != OriginalOrder[j] + MaskOffset) {
9370         OtherElementsInOrder = false;
9371         break;
9372       }
9373     }
9374     // If other elements are in original order, we record the number of shifts
9375     // we need to get the element we want into element 7. Also record which byte
9376     // in the vector we should insert into.
9377     if (OtherElementsInOrder) {
9378       // If 2nd operand is undefined, we assume no shifts and no swapping.
9379       if (V2.isUndef()) {
9380         ShiftElts = 0;
9381         Swap = false;
9382       } else {
9383         // Only need the last 4-bits for shifts because operands will be swapped if CurrentElement is >= 2^4.
9384         ShiftElts = IsLE ? LittleEndianShifts[CurrentElement & 0xF]
9385                          : BigEndianShifts[CurrentElement & 0xF];
9386         Swap = CurrentElement < BytesInVector;
9387       }
9388       InsertAtByte = IsLE ? BytesInVector - (i + 1) : i;
9389       FoundCandidate = true;
9390       break;
9391     }
9392   }
9393 
9394   if (!FoundCandidate)
9395     return SDValue();
9396 
9397   // Candidate found, construct the proper SDAG sequence with VINSERTB,
9398   // optionally with VECSHL if shift is required.
9399   if (Swap)
9400     std::swap(V1, V2);
9401   if (V2.isUndef())
9402     V2 = V1;
9403   if (ShiftElts) {
9404     SDValue Shl = DAG.getNode(PPCISD::VECSHL, dl, MVT::v16i8, V2, V2,
9405                               DAG.getConstant(ShiftElts, dl, MVT::i32));
9406     return DAG.getNode(PPCISD::VECINSERT, dl, MVT::v16i8, V1, Shl,
9407                        DAG.getConstant(InsertAtByte, dl, MVT::i32));
9408   }
9409   return DAG.getNode(PPCISD::VECINSERT, dl, MVT::v16i8, V1, V2,
9410                      DAG.getConstant(InsertAtByte, dl, MVT::i32));
9411 }
9412 
9413 /// lowerToVINSERTH - Return the SDValue if this VECTOR_SHUFFLE can be handled
9414 /// by the VINSERTH instruction introduced in ISA 3.0, else just return default
9415 /// SDValue.
9416 SDValue PPCTargetLowering::lowerToVINSERTH(ShuffleVectorSDNode *N,
9417                                            SelectionDAG &DAG) const {
9418   const unsigned NumHalfWords = 8;
9419   const unsigned BytesInVector = NumHalfWords * 2;
9420   // Check that the shuffle is on half-words.
9421   if (!isNByteElemShuffleMask(N, 2, 1))
9422     return SDValue();
9423 
9424   bool IsLE = Subtarget.isLittleEndian();
9425   SDLoc dl(N);
9426   SDValue V1 = N->getOperand(0);
9427   SDValue V2 = N->getOperand(1);
9428   unsigned ShiftElts = 0, InsertAtByte = 0;
9429   bool Swap = false;
9430 
9431   // Shifts required to get the half-word we want at element 3.
9432   unsigned LittleEndianShifts[] = {4, 3, 2, 1, 0, 7, 6, 5};
9433   unsigned BigEndianShifts[] = {5, 6, 7, 0, 1, 2, 3, 4};
9434 
9435   uint32_t Mask = 0;
9436   uint32_t OriginalOrderLow = 0x1234567;
9437   uint32_t OriginalOrderHigh = 0x89ABCDEF;
9438   // Now we look at mask elements 0,2,4,6,8,10,12,14.  Pack the mask into a
9439   // 32-bit space, only need 4-bit nibbles per element.
9440   for (unsigned i = 0; i < NumHalfWords; ++i) {
9441     unsigned MaskShift = (NumHalfWords - 1 - i) * 4;
9442     Mask |= ((uint32_t)(N->getMaskElt(i * 2) / 2) << MaskShift);
9443   }
9444 
9445   // For each mask element, find out if we're just inserting something
9446   // from V2 into V1 or vice versa.  Possible permutations inserting an element
9447   // from V2 into V1:
9448   //   X, 1, 2, 3, 4, 5, 6, 7
9449   //   0, X, 2, 3, 4, 5, 6, 7
9450   //   0, 1, X, 3, 4, 5, 6, 7
9451   //   0, 1, 2, X, 4, 5, 6, 7
9452   //   0, 1, 2, 3, X, 5, 6, 7
9453   //   0, 1, 2, 3, 4, X, 6, 7
9454   //   0, 1, 2, 3, 4, 5, X, 7
9455   //   0, 1, 2, 3, 4, 5, 6, X
9456   // Inserting from V1 into V2 will be similar, except mask range will be [8,15].
9457 
9458   bool FoundCandidate = false;
9459   // Go through the mask of half-words to find an element that's being moved
9460   // from one vector to the other.
9461   for (unsigned i = 0; i < NumHalfWords; ++i) {
9462     unsigned MaskShift = (NumHalfWords - 1 - i) * 4;
9463     uint32_t MaskOneElt = (Mask >> MaskShift) & 0xF;
9464     uint32_t MaskOtherElts = ~(0xF << MaskShift);
9465     uint32_t TargetOrder = 0x0;
9466 
9467     // If both vector operands for the shuffle are the same vector, the mask
9468     // will contain only elements from the first one and the second one will be
9469     // undef.
9470     if (V2.isUndef()) {
9471       ShiftElts = 0;
9472       unsigned VINSERTHSrcElem = IsLE ? 4 : 3;
9473       TargetOrder = OriginalOrderLow;
9474       Swap = false;
9475       // Skip if not the correct element or mask of other elements don't equal
9476       // to our expected order.
9477       if (MaskOneElt == VINSERTHSrcElem &&
9478           (Mask & MaskOtherElts) == (TargetOrder & MaskOtherElts)) {
9479         InsertAtByte = IsLE ? BytesInVector - (i + 1) * 2 : i * 2;
9480         FoundCandidate = true;
9481         break;
9482       }
9483     } else { // If both operands are defined.
9484       // Target order is [8,15] if the current mask is between [0,7].
9485       TargetOrder =
9486           (MaskOneElt < NumHalfWords) ? OriginalOrderHigh : OriginalOrderLow;
9487       // Skip if mask of other elements don't equal our expected order.
9488       if ((Mask & MaskOtherElts) == (TargetOrder & MaskOtherElts)) {
9489         // We only need the last 3 bits for the number of shifts.
9490         ShiftElts = IsLE ? LittleEndianShifts[MaskOneElt & 0x7]
9491                          : BigEndianShifts[MaskOneElt & 0x7];
9492         InsertAtByte = IsLE ? BytesInVector - (i + 1) * 2 : i * 2;
9493         Swap = MaskOneElt < NumHalfWords;
9494         FoundCandidate = true;
9495         break;
9496       }
9497     }
9498   }
9499 
9500   if (!FoundCandidate)
9501     return SDValue();
9502 
9503   // Candidate found, construct the proper SDAG sequence with VINSERTH,
9504   // optionally with VECSHL if shift is required.
9505   if (Swap)
9506     std::swap(V1, V2);
9507   if (V2.isUndef())
9508     V2 = V1;
9509   SDValue Conv1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
9510   if (ShiftElts) {
9511     // Double ShiftElts because we're left shifting on v16i8 type.
9512     SDValue Shl = DAG.getNode(PPCISD::VECSHL, dl, MVT::v16i8, V2, V2,
9513                               DAG.getConstant(2 * ShiftElts, dl, MVT::i32));
9514     SDValue Conv2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, Shl);
9515     SDValue Ins = DAG.getNode(PPCISD::VECINSERT, dl, MVT::v8i16, Conv1, Conv2,
9516                               DAG.getConstant(InsertAtByte, dl, MVT::i32));
9517     return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Ins);
9518   }
9519   SDValue Conv2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
9520   SDValue Ins = DAG.getNode(PPCISD::VECINSERT, dl, MVT::v8i16, Conv1, Conv2,
9521                             DAG.getConstant(InsertAtByte, dl, MVT::i32));
9522   return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Ins);
9523 }
9524 
9525 /// lowerToXXSPLTI32DX - Return the SDValue if this VECTOR_SHUFFLE can be
9526 /// handled by the XXSPLTI32DX instruction introduced in ISA 3.1, otherwise
9527 /// return the default SDValue.
9528 SDValue PPCTargetLowering::lowerToXXSPLTI32DX(ShuffleVectorSDNode *SVN,
9529                                               SelectionDAG &DAG) const {
9530   // The LHS and RHS may be bitcasts to v16i8 as we canonicalize shuffles
9531   // to v16i8. Peek through the bitcasts to get the actual operands.
9532   SDValue LHS = peekThroughBitcasts(SVN->getOperand(0));
9533   SDValue RHS = peekThroughBitcasts(SVN->getOperand(1));
9534 
9535   auto ShuffleMask = SVN->getMask();
9536   SDValue VecShuffle(SVN, 0);
9537   SDLoc DL(SVN);
9538 
9539   // Check that we have a four byte shuffle.
9540   if (!isNByteElemShuffleMask(SVN, 4, 1))
9541     return SDValue();
9542 
9543   // Canonicalize the RHS being a BUILD_VECTOR when lowering to xxsplti32dx.
9544   if (RHS->getOpcode() != ISD::BUILD_VECTOR) {
9545     std::swap(LHS, RHS);
9546     VecShuffle = DAG.getCommutedVectorShuffle(*SVN);
9547     ShuffleMask = cast<ShuffleVectorSDNode>(VecShuffle)->getMask();
9548   }
9549 
9550   // Ensure that the RHS is a vector of constants.
9551   BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(RHS.getNode());
9552   if (!BVN)
9553     return SDValue();
9554 
9555   // Check if RHS is a splat of 4-bytes (or smaller).
9556   APInt APSplatValue, APSplatUndef;
9557   unsigned SplatBitSize;
9558   bool HasAnyUndefs;
9559   if (!BVN->isConstantSplat(APSplatValue, APSplatUndef, SplatBitSize,
9560                             HasAnyUndefs, 0, !Subtarget.isLittleEndian()) ||
9561       SplatBitSize > 32)
9562     return SDValue();
9563 
9564   // Check that the shuffle mask matches the semantics of XXSPLTI32DX.
9565   // The instruction splats a constant C into two words of the source vector
9566   // producing { C, Unchanged, C, Unchanged } or { Unchanged, C, Unchanged, C }.
9567   // Thus we check that the shuffle mask is the equivalent  of
9568   // <0, [4-7], 2, [4-7]> or <[4-7], 1, [4-7], 3> respectively.
9569   // Note: the check above of isNByteElemShuffleMask() ensures that the bytes
9570   // within each word are consecutive, so we only need to check the first byte.
9571   SDValue Index;
9572   bool IsLE = Subtarget.isLittleEndian();
9573   if ((ShuffleMask[0] == 0 && ShuffleMask[8] == 8) &&
9574       (ShuffleMask[4] % 4 == 0 && ShuffleMask[12] % 4 == 0 &&
9575        ShuffleMask[4] > 15 && ShuffleMask[12] > 15))
9576     Index = DAG.getTargetConstant(IsLE ? 0 : 1, DL, MVT::i32);
9577   else if ((ShuffleMask[4] == 4 && ShuffleMask[12] == 12) &&
9578            (ShuffleMask[0] % 4 == 0 && ShuffleMask[8] % 4 == 0 &&
9579             ShuffleMask[0] > 15 && ShuffleMask[8] > 15))
9580     Index = DAG.getTargetConstant(IsLE ? 1 : 0, DL, MVT::i32);
9581   else
9582     return SDValue();
9583 
9584   // If the splat is narrower than 32-bits, we need to get the 32-bit value
9585   // for XXSPLTI32DX.
9586   unsigned SplatVal = APSplatValue.getZExtValue();
9587   for (; SplatBitSize < 32; SplatBitSize <<= 1)
9588     SplatVal |= (SplatVal << SplatBitSize);
9589 
9590   SDValue SplatNode = DAG.getNode(
9591       PPCISD::XXSPLTI32DX, DL, MVT::v2i64, DAG.getBitcast(MVT::v2i64, LHS),
9592       Index, DAG.getTargetConstant(SplatVal, DL, MVT::i32));
9593   return DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, SplatNode);
9594 }
9595 
9596 /// LowerROTL - Custom lowering for ROTL(v1i128) to vector_shuffle(v16i8).
9597 /// We lower ROTL(v1i128) to vector_shuffle(v16i8) only if shift amount is
9598 /// a multiple of 8. Otherwise convert it to a scalar rotation(i128)
9599 /// i.e (or (shl x, C1), (srl x, 128-C1)).
9600 SDValue PPCTargetLowering::LowerROTL(SDValue Op, SelectionDAG &DAG) const {
9601   assert(Op.getOpcode() == ISD::ROTL && "Should only be called for ISD::ROTL");
9602   assert(Op.getValueType() == MVT::v1i128 &&
9603          "Only set v1i128 as custom, other type shouldn't reach here!");
9604   SDLoc dl(Op);
9605   SDValue N0 = peekThroughBitcasts(Op.getOperand(0));
9606   SDValue N1 = peekThroughBitcasts(Op.getOperand(1));
9607   unsigned SHLAmt = N1.getConstantOperandVal(0);
9608   if (SHLAmt % 8 == 0) {
9609     SmallVector<int, 16> Mask(16, 0);
9610     std::iota(Mask.begin(), Mask.end(), 0);
9611     std::rotate(Mask.begin(), Mask.begin() + SHLAmt / 8, Mask.end());
9612     if (SDValue Shuffle =
9613             DAG.getVectorShuffle(MVT::v16i8, dl, DAG.getBitcast(MVT::v16i8, N0),
9614                                  DAG.getUNDEF(MVT::v16i8), Mask))
9615       return DAG.getNode(ISD::BITCAST, dl, MVT::v1i128, Shuffle);
9616   }
9617   SDValue ArgVal = DAG.getBitcast(MVT::i128, N0);
9618   SDValue SHLOp = DAG.getNode(ISD::SHL, dl, MVT::i128, ArgVal,
9619                               DAG.getConstant(SHLAmt, dl, MVT::i32));
9620   SDValue SRLOp = DAG.getNode(ISD::SRL, dl, MVT::i128, ArgVal,
9621                               DAG.getConstant(128 - SHLAmt, dl, MVT::i32));
9622   SDValue OROp = DAG.getNode(ISD::OR, dl, MVT::i128, SHLOp, SRLOp);
9623   return DAG.getNode(ISD::BITCAST, dl, MVT::v1i128, OROp);
9624 }
9625 
9626 /// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE.  If this
9627 /// is a shuffle we can handle in a single instruction, return it.  Otherwise,
9628 /// return the code it can be lowered into.  Worst case, it can always be
9629 /// lowered into a vperm.
9630 SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
9631                                                SelectionDAG &DAG) const {
9632   SDLoc dl(Op);
9633   SDValue V1 = Op.getOperand(0);
9634   SDValue V2 = Op.getOperand(1);
9635   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
9636 
9637   // Any nodes that were combined in the target-independent combiner prior
9638   // to vector legalization will not be sent to the target combine. Try to
9639   // combine it here.
9640   if (SDValue NewShuffle = combineVectorShuffle(SVOp, DAG)) {
9641     if (!isa<ShuffleVectorSDNode>(NewShuffle))
9642       return NewShuffle;
9643     Op = NewShuffle;
9644     SVOp = cast<ShuffleVectorSDNode>(Op);
9645     V1 = Op.getOperand(0);
9646     V2 = Op.getOperand(1);
9647   }
9648   EVT VT = Op.getValueType();
9649   bool isLittleEndian = Subtarget.isLittleEndian();
9650 
9651   unsigned ShiftElts, InsertAtByte;
9652   bool Swap = false;
9653 
9654   // If this is a load-and-splat, we can do that with a single instruction
9655   // in some cases. However if the load has multiple uses, we don't want to
9656   // combine it because that will just produce multiple loads.
9657   bool IsPermutedLoad = false;
9658   const SDValue *InputLoad = getNormalLoadInput(V1, IsPermutedLoad);
9659   if (InputLoad && Subtarget.hasVSX() && V2.isUndef() &&
9660       (PPC::isSplatShuffleMask(SVOp, 4) || PPC::isSplatShuffleMask(SVOp, 8)) &&
9661       InputLoad->hasOneUse()) {
9662     bool IsFourByte = PPC::isSplatShuffleMask(SVOp, 4);
9663     int SplatIdx =
9664       PPC::getSplatIdxForPPCMnemonics(SVOp, IsFourByte ? 4 : 8, DAG);
9665 
9666     // The splat index for permuted loads will be in the left half of the vector
9667     // which is strictly wider than the loaded value by 8 bytes. So we need to
9668     // adjust the splat index to point to the correct address in memory.
9669     if (IsPermutedLoad) {
9670       assert((isLittleEndian || IsFourByte) &&
9671              "Unexpected size for permuted load on big endian target");
9672       SplatIdx += IsFourByte ? 2 : 1;
9673       assert((SplatIdx < (IsFourByte ? 4 : 2)) &&
9674              "Splat of a value outside of the loaded memory");
9675     }
9676 
9677     LoadSDNode *LD = cast<LoadSDNode>(*InputLoad);
9678     // For 4-byte load-and-splat, we need Power9.
9679     if ((IsFourByte && Subtarget.hasP9Vector()) || !IsFourByte) {
9680       uint64_t Offset = 0;
9681       if (IsFourByte)
9682         Offset = isLittleEndian ? (3 - SplatIdx) * 4 : SplatIdx * 4;
9683       else
9684         Offset = isLittleEndian ? (1 - SplatIdx) * 8 : SplatIdx * 8;
9685 
9686       // If the width of the load is the same as the width of the splat,
9687       // loading with an offset would load the wrong memory.
9688       if (LD->getValueType(0).getSizeInBits() == (IsFourByte ? 32 : 64))
9689         Offset = 0;
9690 
9691       SDValue BasePtr = LD->getBasePtr();
9692       if (Offset != 0)
9693         BasePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
9694                               BasePtr, DAG.getIntPtrConstant(Offset, dl));
9695       SDValue Ops[] = {
9696         LD->getChain(),    // Chain
9697         BasePtr,           // BasePtr
9698         DAG.getValueType(Op.getValueType()) // VT
9699       };
9700       SDVTList VTL =
9701         DAG.getVTList(IsFourByte ? MVT::v4i32 : MVT::v2i64, MVT::Other);
9702       SDValue LdSplt =
9703         DAG.getMemIntrinsicNode(PPCISD::LD_SPLAT, dl, VTL,
9704                                 Ops, LD->getMemoryVT(), LD->getMemOperand());
9705       DAG.ReplaceAllUsesOfValueWith(InputLoad->getValue(1), LdSplt.getValue(1));
9706       if (LdSplt.getValueType() != SVOp->getValueType(0))
9707         LdSplt = DAG.getBitcast(SVOp->getValueType(0), LdSplt);
9708       return LdSplt;
9709     }
9710   }
9711   if (Subtarget.hasP9Vector() &&
9712       PPC::isXXINSERTWMask(SVOp, ShiftElts, InsertAtByte, Swap,
9713                            isLittleEndian)) {
9714     if (Swap)
9715       std::swap(V1, V2);
9716     SDValue Conv1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
9717     SDValue Conv2 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V2);
9718     if (ShiftElts) {
9719       SDValue Shl = DAG.getNode(PPCISD::VECSHL, dl, MVT::v4i32, Conv2, Conv2,
9720                                 DAG.getConstant(ShiftElts, dl, MVT::i32));
9721       SDValue Ins = DAG.getNode(PPCISD::VECINSERT, dl, MVT::v4i32, Conv1, Shl,
9722                                 DAG.getConstant(InsertAtByte, dl, MVT::i32));
9723       return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Ins);
9724     }
9725     SDValue Ins = DAG.getNode(PPCISD::VECINSERT, dl, MVT::v4i32, Conv1, Conv2,
9726                               DAG.getConstant(InsertAtByte, dl, MVT::i32));
9727     return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Ins);
9728   }
9729 
9730   if (Subtarget.hasPrefixInstrs()) {
9731     SDValue SplatInsertNode;
9732     if ((SplatInsertNode = lowerToXXSPLTI32DX(SVOp, DAG)))
9733       return SplatInsertNode;
9734   }
9735 
9736   if (Subtarget.hasP9Altivec()) {
9737     SDValue NewISDNode;
9738     if ((NewISDNode = lowerToVINSERTH(SVOp, DAG)))
9739       return NewISDNode;
9740 
9741     if ((NewISDNode = lowerToVINSERTB(SVOp, DAG)))
9742       return NewISDNode;
9743   }
9744 
9745   if (Subtarget.hasVSX() &&
9746       PPC::isXXSLDWIShuffleMask(SVOp, ShiftElts, Swap, isLittleEndian)) {
9747     if (Swap)
9748       std::swap(V1, V2);
9749     SDValue Conv1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
9750     SDValue Conv2 =
9751         DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V2.isUndef() ? V1 : V2);
9752 
9753     SDValue Shl = DAG.getNode(PPCISD::VECSHL, dl, MVT::v4i32, Conv1, Conv2,
9754                               DAG.getConstant(ShiftElts, dl, MVT::i32));
9755     return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Shl);
9756   }
9757 
9758   if (Subtarget.hasVSX() &&
9759     PPC::isXXPERMDIShuffleMask(SVOp, ShiftElts, Swap, isLittleEndian)) {
9760     if (Swap)
9761       std::swap(V1, V2);
9762     SDValue Conv1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1);
9763     SDValue Conv2 =
9764         DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2.isUndef() ? V1 : V2);
9765 
9766     SDValue PermDI = DAG.getNode(PPCISD::XXPERMDI, dl, MVT::v2i64, Conv1, Conv2,
9767                               DAG.getConstant(ShiftElts, dl, MVT::i32));
9768     return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, PermDI);
9769   }
9770 
9771   if (Subtarget.hasP9Vector()) {
9772      if (PPC::isXXBRHShuffleMask(SVOp)) {
9773       SDValue Conv = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
9774       SDValue ReveHWord = DAG.getNode(ISD::BSWAP, dl, MVT::v8i16, Conv);
9775       return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, ReveHWord);
9776     } else if (PPC::isXXBRWShuffleMask(SVOp)) {
9777       SDValue Conv = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
9778       SDValue ReveWord = DAG.getNode(ISD::BSWAP, dl, MVT::v4i32, Conv);
9779       return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, ReveWord);
9780     } else if (PPC::isXXBRDShuffleMask(SVOp)) {
9781       SDValue Conv = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1);
9782       SDValue ReveDWord = DAG.getNode(ISD::BSWAP, dl, MVT::v2i64, Conv);
9783       return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, ReveDWord);
9784     } else if (PPC::isXXBRQShuffleMask(SVOp)) {
9785       SDValue Conv = DAG.getNode(ISD::BITCAST, dl, MVT::v1i128, V1);
9786       SDValue ReveQWord = DAG.getNode(ISD::BSWAP, dl, MVT::v1i128, Conv);
9787       return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, ReveQWord);
9788     }
9789   }
9790 
9791   if (Subtarget.hasVSX()) {
9792     if (V2.isUndef() && PPC::isSplatShuffleMask(SVOp, 4)) {
9793       int SplatIdx = PPC::getSplatIdxForPPCMnemonics(SVOp, 4, DAG);
9794 
9795       SDValue Conv = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
9796       SDValue Splat = DAG.getNode(PPCISD::XXSPLT, dl, MVT::v4i32, Conv,
9797                                   DAG.getConstant(SplatIdx, dl, MVT::i32));
9798       return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Splat);
9799     }
9800 
9801     // Left shifts of 8 bytes are actually swaps. Convert accordingly.
9802     if (V2.isUndef() && PPC::isVSLDOIShuffleMask(SVOp, 1, DAG) == 8) {
9803       SDValue Conv = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
9804       SDValue Swap = DAG.getNode(PPCISD::SWAP_NO_CHAIN, dl, MVT::v2f64, Conv);
9805       return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Swap);
9806     }
9807   }
9808 
9809   // Cases that are handled by instructions that take permute immediates
9810   // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
9811   // selected by the instruction selector.
9812   if (V2.isUndef()) {
9813     if (PPC::isSplatShuffleMask(SVOp, 1) ||
9814         PPC::isSplatShuffleMask(SVOp, 2) ||
9815         PPC::isSplatShuffleMask(SVOp, 4) ||
9816         PPC::isVPKUWUMShuffleMask(SVOp, 1, DAG) ||
9817         PPC::isVPKUHUMShuffleMask(SVOp, 1, DAG) ||
9818         PPC::isVSLDOIShuffleMask(SVOp, 1, DAG) != -1 ||
9819         PPC::isVMRGLShuffleMask(SVOp, 1, 1, DAG) ||
9820         PPC::isVMRGLShuffleMask(SVOp, 2, 1, DAG) ||
9821         PPC::isVMRGLShuffleMask(SVOp, 4, 1, DAG) ||
9822         PPC::isVMRGHShuffleMask(SVOp, 1, 1, DAG) ||
9823         PPC::isVMRGHShuffleMask(SVOp, 2, 1, DAG) ||
9824         PPC::isVMRGHShuffleMask(SVOp, 4, 1, DAG) ||
9825         (Subtarget.hasP8Altivec() && (
9826          PPC::isVPKUDUMShuffleMask(SVOp, 1, DAG) ||
9827          PPC::isVMRGEOShuffleMask(SVOp, true, 1, DAG) ||
9828          PPC::isVMRGEOShuffleMask(SVOp, false, 1, DAG)))) {
9829       return Op;
9830     }
9831   }
9832 
9833   // Altivec has a variety of "shuffle immediates" that take two vector inputs
9834   // and produce a fixed permutation.  If any of these match, do not lower to
9835   // VPERM.
9836   unsigned int ShuffleKind = isLittleEndian ? 2 : 0;
9837   if (PPC::isVPKUWUMShuffleMask(SVOp, ShuffleKind, DAG) ||
9838       PPC::isVPKUHUMShuffleMask(SVOp, ShuffleKind, DAG) ||
9839       PPC::isVSLDOIShuffleMask(SVOp, ShuffleKind, DAG) != -1 ||
9840       PPC::isVMRGLShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
9841       PPC::isVMRGLShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
9842       PPC::isVMRGLShuffleMask(SVOp, 4, ShuffleKind, DAG) ||
9843       PPC::isVMRGHShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
9844       PPC::isVMRGHShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
9845       PPC::isVMRGHShuffleMask(SVOp, 4, ShuffleKind, DAG) ||
9846       (Subtarget.hasP8Altivec() && (
9847        PPC::isVPKUDUMShuffleMask(SVOp, ShuffleKind, DAG) ||
9848        PPC::isVMRGEOShuffleMask(SVOp, true, ShuffleKind, DAG) ||
9849        PPC::isVMRGEOShuffleMask(SVOp, false, ShuffleKind, DAG))))
9850     return Op;
9851 
9852   // Check to see if this is a shuffle of 4-byte values.  If so, we can use our
9853   // perfect shuffle table to emit an optimal matching sequence.
9854   ArrayRef<int> PermMask = SVOp->getMask();
9855 
9856   unsigned PFIndexes[4];
9857   bool isFourElementShuffle = true;
9858   for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
9859     unsigned EltNo = 8;   // Start out undef.
9860     for (unsigned j = 0; j != 4; ++j) {  // Intra-element byte.
9861       if (PermMask[i*4+j] < 0)
9862         continue;   // Undef, ignore it.
9863 
9864       unsigned ByteSource = PermMask[i*4+j];
9865       if ((ByteSource & 3) != j) {
9866         isFourElementShuffle = false;
9867         break;
9868       }
9869 
9870       if (EltNo == 8) {
9871         EltNo = ByteSource/4;
9872       } else if (EltNo != ByteSource/4) {
9873         isFourElementShuffle = false;
9874         break;
9875       }
9876     }
9877     PFIndexes[i] = EltNo;
9878   }
9879 
9880   // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
9881   // perfect shuffle vector to determine if it is cost effective to do this as
9882   // discrete instructions, or whether we should use a vperm.
9883   // For now, we skip this for little endian until such time as we have a
9884   // little-endian perfect shuffle table.
9885   if (isFourElementShuffle && !isLittleEndian) {
9886     // Compute the index in the perfect shuffle table.
9887     unsigned PFTableIndex =
9888       PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
9889 
9890     unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
9891     unsigned Cost  = (PFEntry >> 30);
9892 
9893     // Determining when to avoid vperm is tricky.  Many things affect the cost
9894     // of vperm, particularly how many times the perm mask needs to be computed.
9895     // For example, if the perm mask can be hoisted out of a loop or is already
9896     // used (perhaps because there are multiple permutes with the same shuffle
9897     // mask?) the vperm has a cost of 1.  OTOH, hoisting the permute mask out of
9898     // the loop requires an extra register.
9899     //
9900     // As a compromise, we only emit discrete instructions if the shuffle can be
9901     // generated in 3 or fewer operations.  When we have loop information
9902     // available, if this block is within a loop, we should avoid using vperm
9903     // for 3-operation perms and use a constant pool load instead.
9904     if (Cost < 3)
9905       return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
9906   }
9907 
9908   // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
9909   // vector that will get spilled to the constant pool.
9910   if (V2.isUndef()) V2 = V1;
9911 
9912   // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
9913   // that it is in input element units, not in bytes.  Convert now.
9914 
9915   // For little endian, the order of the input vectors is reversed, and
9916   // the permutation mask is complemented with respect to 31.  This is
9917   // necessary to produce proper semantics with the big-endian-biased vperm
9918   // instruction.
9919   EVT EltVT = V1.getValueType().getVectorElementType();
9920   unsigned BytesPerElement = EltVT.getSizeInBits()/8;
9921 
9922   SmallVector<SDValue, 16> ResultMask;
9923   for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
9924     unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
9925 
9926     for (unsigned j = 0; j != BytesPerElement; ++j)
9927       if (isLittleEndian)
9928         ResultMask.push_back(DAG.getConstant(31 - (SrcElt*BytesPerElement + j),
9929                                              dl, MVT::i32));
9930       else
9931         ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement + j, dl,
9932                                              MVT::i32));
9933   }
9934 
9935   ShufflesHandledWithVPERM++;
9936   SDValue VPermMask = DAG.getBuildVector(MVT::v16i8, dl, ResultMask);
9937   LLVM_DEBUG(dbgs() << "Emitting a VPERM for the following shuffle:\n");
9938   LLVM_DEBUG(SVOp->dump());
9939   LLVM_DEBUG(dbgs() << "With the following permute control vector:\n");
9940   LLVM_DEBUG(VPermMask.dump());
9941 
9942   if (isLittleEndian)
9943     return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
9944                        V2, V1, VPermMask);
9945   else
9946     return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
9947                        V1, V2, VPermMask);
9948 }
9949 
9950 /// getVectorCompareInfo - Given an intrinsic, return false if it is not a
9951 /// vector comparison.  If it is, return true and fill in Opc/isDot with
9952 /// information about the intrinsic.
9953 static bool getVectorCompareInfo(SDValue Intrin, int &CompareOpc,
9954                                  bool &isDot, const PPCSubtarget &Subtarget) {
9955   unsigned IntrinsicID =
9956       cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
9957   CompareOpc = -1;
9958   isDot = false;
9959   switch (IntrinsicID) {
9960   default:
9961     return false;
9962   // Comparison predicates.
9963   case Intrinsic::ppc_altivec_vcmpbfp_p:
9964     CompareOpc = 966;
9965     isDot = true;
9966     break;
9967   case Intrinsic::ppc_altivec_vcmpeqfp_p:
9968     CompareOpc = 198;
9969     isDot = true;
9970     break;
9971   case Intrinsic::ppc_altivec_vcmpequb_p:
9972     CompareOpc = 6;
9973     isDot = true;
9974     break;
9975   case Intrinsic::ppc_altivec_vcmpequh_p:
9976     CompareOpc = 70;
9977     isDot = true;
9978     break;
9979   case Intrinsic::ppc_altivec_vcmpequw_p:
9980     CompareOpc = 134;
9981     isDot = true;
9982     break;
9983   case Intrinsic::ppc_altivec_vcmpequd_p:
9984     if (Subtarget.hasP8Altivec()) {
9985       CompareOpc = 199;
9986       isDot = true;
9987     } else
9988       return false;
9989     break;
9990   case Intrinsic::ppc_altivec_vcmpneb_p:
9991   case Intrinsic::ppc_altivec_vcmpneh_p:
9992   case Intrinsic::ppc_altivec_vcmpnew_p:
9993   case Intrinsic::ppc_altivec_vcmpnezb_p:
9994   case Intrinsic::ppc_altivec_vcmpnezh_p:
9995   case Intrinsic::ppc_altivec_vcmpnezw_p:
9996     if (Subtarget.hasP9Altivec()) {
9997       switch (IntrinsicID) {
9998       default:
9999         llvm_unreachable("Unknown comparison intrinsic.");
10000       case Intrinsic::ppc_altivec_vcmpneb_p:
10001         CompareOpc = 7;
10002         break;
10003       case Intrinsic::ppc_altivec_vcmpneh_p:
10004         CompareOpc = 71;
10005         break;
10006       case Intrinsic::ppc_altivec_vcmpnew_p:
10007         CompareOpc = 135;
10008         break;
10009       case Intrinsic::ppc_altivec_vcmpnezb_p:
10010         CompareOpc = 263;
10011         break;
10012       case Intrinsic::ppc_altivec_vcmpnezh_p:
10013         CompareOpc = 327;
10014         break;
10015       case Intrinsic::ppc_altivec_vcmpnezw_p:
10016         CompareOpc = 391;
10017         break;
10018       }
10019       isDot = true;
10020     } else
10021       return false;
10022     break;
10023   case Intrinsic::ppc_altivec_vcmpgefp_p:
10024     CompareOpc = 454;
10025     isDot = true;
10026     break;
10027   case Intrinsic::ppc_altivec_vcmpgtfp_p:
10028     CompareOpc = 710;
10029     isDot = true;
10030     break;
10031   case Intrinsic::ppc_altivec_vcmpgtsb_p:
10032     CompareOpc = 774;
10033     isDot = true;
10034     break;
10035   case Intrinsic::ppc_altivec_vcmpgtsh_p:
10036     CompareOpc = 838;
10037     isDot = true;
10038     break;
10039   case Intrinsic::ppc_altivec_vcmpgtsw_p:
10040     CompareOpc = 902;
10041     isDot = true;
10042     break;
10043   case Intrinsic::ppc_altivec_vcmpgtsd_p:
10044     if (Subtarget.hasP8Altivec()) {
10045       CompareOpc = 967;
10046       isDot = true;
10047     } else
10048       return false;
10049     break;
10050   case Intrinsic::ppc_altivec_vcmpgtub_p:
10051     CompareOpc = 518;
10052     isDot = true;
10053     break;
10054   case Intrinsic::ppc_altivec_vcmpgtuh_p:
10055     CompareOpc = 582;
10056     isDot = true;
10057     break;
10058   case Intrinsic::ppc_altivec_vcmpgtuw_p:
10059     CompareOpc = 646;
10060     isDot = true;
10061     break;
10062   case Intrinsic::ppc_altivec_vcmpgtud_p:
10063     if (Subtarget.hasP8Altivec()) {
10064       CompareOpc = 711;
10065       isDot = true;
10066     } else
10067       return false;
10068     break;
10069 
10070   case Intrinsic::ppc_altivec_vcmpequq:
10071   case Intrinsic::ppc_altivec_vcmpgtsq:
10072   case Intrinsic::ppc_altivec_vcmpgtuq:
10073     if (!Subtarget.isISA3_1())
10074       return false;
10075     switch (IntrinsicID) {
10076     default:
10077       llvm_unreachable("Unknown comparison intrinsic.");
10078     case Intrinsic::ppc_altivec_vcmpequq:
10079       CompareOpc = 455;
10080       break;
10081     case Intrinsic::ppc_altivec_vcmpgtsq:
10082       CompareOpc = 903;
10083       break;
10084     case Intrinsic::ppc_altivec_vcmpgtuq:
10085       CompareOpc = 647;
10086       break;
10087     }
10088     break;
10089 
10090   // VSX predicate comparisons use the same infrastructure
10091   case Intrinsic::ppc_vsx_xvcmpeqdp_p:
10092   case Intrinsic::ppc_vsx_xvcmpgedp_p:
10093   case Intrinsic::ppc_vsx_xvcmpgtdp_p:
10094   case Intrinsic::ppc_vsx_xvcmpeqsp_p:
10095   case Intrinsic::ppc_vsx_xvcmpgesp_p:
10096   case Intrinsic::ppc_vsx_xvcmpgtsp_p:
10097     if (Subtarget.hasVSX()) {
10098       switch (IntrinsicID) {
10099       case Intrinsic::ppc_vsx_xvcmpeqdp_p:
10100         CompareOpc = 99;
10101         break;
10102       case Intrinsic::ppc_vsx_xvcmpgedp_p:
10103         CompareOpc = 115;
10104         break;
10105       case Intrinsic::ppc_vsx_xvcmpgtdp_p:
10106         CompareOpc = 107;
10107         break;
10108       case Intrinsic::ppc_vsx_xvcmpeqsp_p:
10109         CompareOpc = 67;
10110         break;
10111       case Intrinsic::ppc_vsx_xvcmpgesp_p:
10112         CompareOpc = 83;
10113         break;
10114       case Intrinsic::ppc_vsx_xvcmpgtsp_p:
10115         CompareOpc = 75;
10116         break;
10117       }
10118       isDot = true;
10119     } else
10120       return false;
10121     break;
10122 
10123   // Normal Comparisons.
10124   case Intrinsic::ppc_altivec_vcmpbfp:
10125     CompareOpc = 966;
10126     break;
10127   case Intrinsic::ppc_altivec_vcmpeqfp:
10128     CompareOpc = 198;
10129     break;
10130   case Intrinsic::ppc_altivec_vcmpequb:
10131     CompareOpc = 6;
10132     break;
10133   case Intrinsic::ppc_altivec_vcmpequh:
10134     CompareOpc = 70;
10135     break;
10136   case Intrinsic::ppc_altivec_vcmpequw:
10137     CompareOpc = 134;
10138     break;
10139   case Intrinsic::ppc_altivec_vcmpequd:
10140     if (Subtarget.hasP8Altivec())
10141       CompareOpc = 199;
10142     else
10143       return false;
10144     break;
10145   case Intrinsic::ppc_altivec_vcmpneb:
10146   case Intrinsic::ppc_altivec_vcmpneh:
10147   case Intrinsic::ppc_altivec_vcmpnew:
10148   case Intrinsic::ppc_altivec_vcmpnezb:
10149   case Intrinsic::ppc_altivec_vcmpnezh:
10150   case Intrinsic::ppc_altivec_vcmpnezw:
10151     if (Subtarget.hasP9Altivec())
10152       switch (IntrinsicID) {
10153       default:
10154         llvm_unreachable("Unknown comparison intrinsic.");
10155       case Intrinsic::ppc_altivec_vcmpneb:
10156         CompareOpc = 7;
10157         break;
10158       case Intrinsic::ppc_altivec_vcmpneh:
10159         CompareOpc = 71;
10160         break;
10161       case Intrinsic::ppc_altivec_vcmpnew:
10162         CompareOpc = 135;
10163         break;
10164       case Intrinsic::ppc_altivec_vcmpnezb:
10165         CompareOpc = 263;
10166         break;
10167       case Intrinsic::ppc_altivec_vcmpnezh:
10168         CompareOpc = 327;
10169         break;
10170       case Intrinsic::ppc_altivec_vcmpnezw:
10171         CompareOpc = 391;
10172         break;
10173       }
10174     else
10175       return false;
10176     break;
10177   case Intrinsic::ppc_altivec_vcmpgefp:
10178     CompareOpc = 454;
10179     break;
10180   case Intrinsic::ppc_altivec_vcmpgtfp:
10181     CompareOpc = 710;
10182     break;
10183   case Intrinsic::ppc_altivec_vcmpgtsb:
10184     CompareOpc = 774;
10185     break;
10186   case Intrinsic::ppc_altivec_vcmpgtsh:
10187     CompareOpc = 838;
10188     break;
10189   case Intrinsic::ppc_altivec_vcmpgtsw:
10190     CompareOpc = 902;
10191     break;
10192   case Intrinsic::ppc_altivec_vcmpgtsd:
10193     if (Subtarget.hasP8Altivec())
10194       CompareOpc = 967;
10195     else
10196       return false;
10197     break;
10198   case Intrinsic::ppc_altivec_vcmpgtub:
10199     CompareOpc = 518;
10200     break;
10201   case Intrinsic::ppc_altivec_vcmpgtuh:
10202     CompareOpc = 582;
10203     break;
10204   case Intrinsic::ppc_altivec_vcmpgtuw:
10205     CompareOpc = 646;
10206     break;
10207   case Intrinsic::ppc_altivec_vcmpgtud:
10208     if (Subtarget.hasP8Altivec())
10209       CompareOpc = 711;
10210     else
10211       return false;
10212     break;
10213   case Intrinsic::ppc_altivec_vcmpequq_p:
10214   case Intrinsic::ppc_altivec_vcmpgtsq_p:
10215   case Intrinsic::ppc_altivec_vcmpgtuq_p:
10216     if (!Subtarget.isISA3_1())
10217       return false;
10218     switch (IntrinsicID) {
10219     default:
10220       llvm_unreachable("Unknown comparison intrinsic.");
10221     case Intrinsic::ppc_altivec_vcmpequq_p:
10222       CompareOpc = 455;
10223       break;
10224     case Intrinsic::ppc_altivec_vcmpgtsq_p:
10225       CompareOpc = 903;
10226       break;
10227     case Intrinsic::ppc_altivec_vcmpgtuq_p:
10228       CompareOpc = 647;
10229       break;
10230     }
10231     isDot = true;
10232     break;
10233   }
10234   return true;
10235 }
10236 
10237 /// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
10238 /// lower, do it, otherwise return null.
10239 SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
10240                                                    SelectionDAG &DAG) const {
10241   unsigned IntrinsicID =
10242     cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10243 
10244   SDLoc dl(Op);
10245 
10246   switch (IntrinsicID) {
10247   case Intrinsic::thread_pointer:
10248     // Reads the thread pointer register, used for __builtin_thread_pointer.
10249     if (Subtarget.isPPC64())
10250       return DAG.getRegister(PPC::X13, MVT::i64);
10251     return DAG.getRegister(PPC::R2, MVT::i32);
10252 
10253   case Intrinsic::ppc_mma_disassemble_acc:
10254   case Intrinsic::ppc_vsx_disassemble_pair: {
10255     int NumVecs = 2;
10256     SDValue WideVec = Op.getOperand(1);
10257     if (IntrinsicID == Intrinsic::ppc_mma_disassemble_acc) {
10258       NumVecs = 4;
10259       WideVec = DAG.getNode(PPCISD::XXMFACC, dl, MVT::v512i1, WideVec);
10260     }
10261     SmallVector<SDValue, 4> RetOps;
10262     for (int VecNo = 0; VecNo < NumVecs; VecNo++) {
10263       SDValue Extract = DAG.getNode(
10264           PPCISD::EXTRACT_VSX_REG, dl, MVT::v16i8, WideVec,
10265           DAG.getConstant(Subtarget.isLittleEndian() ? NumVecs - 1 - VecNo
10266                                                      : VecNo,
10267                           dl, getPointerTy(DAG.getDataLayout())));
10268       RetOps.push_back(Extract);
10269     }
10270     return DAG.getMergeValues(RetOps, dl);
10271   }
10272   }
10273 
10274   // If this is a lowered altivec predicate compare, CompareOpc is set to the
10275   // opcode number of the comparison.
10276   int CompareOpc;
10277   bool isDot;
10278   if (!getVectorCompareInfo(Op, CompareOpc, isDot, Subtarget))
10279     return SDValue();    // Don't custom lower most intrinsics.
10280 
10281   // If this is a non-dot comparison, make the VCMP node and we are done.
10282   if (!isDot) {
10283     SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
10284                               Op.getOperand(1), Op.getOperand(2),
10285                               DAG.getConstant(CompareOpc, dl, MVT::i32));
10286     return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
10287   }
10288 
10289   // Create the PPCISD altivec 'dot' comparison node.
10290   SDValue Ops[] = {
10291     Op.getOperand(2),  // LHS
10292     Op.getOperand(3),  // RHS
10293     DAG.getConstant(CompareOpc, dl, MVT::i32)
10294   };
10295   EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
10296   SDValue CompNode = DAG.getNode(PPCISD::VCMP_rec, dl, VTs, Ops);
10297 
10298   // Now that we have the comparison, emit a copy from the CR to a GPR.
10299   // This is flagged to the above dot comparison.
10300   SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
10301                                 DAG.getRegister(PPC::CR6, MVT::i32),
10302                                 CompNode.getValue(1));
10303 
10304   // Unpack the result based on how the target uses it.
10305   unsigned BitNo;   // Bit # of CR6.
10306   bool InvertBit;   // Invert result?
10307   switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
10308   default:  // Can't happen, don't crash on invalid number though.
10309   case 0:   // Return the value of the EQ bit of CR6.
10310     BitNo = 0; InvertBit = false;
10311     break;
10312   case 1:   // Return the inverted value of the EQ bit of CR6.
10313     BitNo = 0; InvertBit = true;
10314     break;
10315   case 2:   // Return the value of the LT bit of CR6.
10316     BitNo = 2; InvertBit = false;
10317     break;
10318   case 3:   // Return the inverted value of the LT bit of CR6.
10319     BitNo = 2; InvertBit = true;
10320     break;
10321   }
10322 
10323   // Shift the bit into the low position.
10324   Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
10325                       DAG.getConstant(8 - (3 - BitNo), dl, MVT::i32));
10326   // Isolate the bit.
10327   Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
10328                       DAG.getConstant(1, dl, MVT::i32));
10329 
10330   // If we are supposed to, toggle the bit.
10331   if (InvertBit)
10332     Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
10333                         DAG.getConstant(1, dl, MVT::i32));
10334   return Flags;
10335 }
10336 
10337 SDValue PPCTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
10338                                                SelectionDAG &DAG) const {
10339   // SelectionDAGBuilder::visitTargetIntrinsic may insert one extra chain to
10340   // the beginning of the argument list.
10341   int ArgStart = isa<ConstantSDNode>(Op.getOperand(0)) ? 0 : 1;
10342   SDLoc DL(Op);
10343   switch (cast<ConstantSDNode>(Op.getOperand(ArgStart))->getZExtValue()) {
10344   case Intrinsic::ppc_cfence: {
10345     assert(ArgStart == 1 && "llvm.ppc.cfence must carry a chain argument.");
10346     assert(Subtarget.isPPC64() && "Only 64-bit is supported for now.");
10347     return SDValue(DAG.getMachineNode(PPC::CFENCE8, DL, MVT::Other,
10348                                       DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64,
10349                                                   Op.getOperand(ArgStart + 1)),
10350                                       Op.getOperand(0)),
10351                    0);
10352   }
10353   default:
10354     break;
10355   }
10356   return SDValue();
10357 }
10358 
10359 // Lower scalar BSWAP64 to xxbrd.
10360 SDValue PPCTargetLowering::LowerBSWAP(SDValue Op, SelectionDAG &DAG) const {
10361   SDLoc dl(Op);
10362   if (!Subtarget.isPPC64())
10363     return Op;
10364   // MTVSRDD
10365   Op = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64, Op.getOperand(0),
10366                    Op.getOperand(0));
10367   // XXBRD
10368   Op = DAG.getNode(ISD::BSWAP, dl, MVT::v2i64, Op);
10369   // MFVSRD
10370   int VectorIndex = 0;
10371   if (Subtarget.isLittleEndian())
10372     VectorIndex = 1;
10373   Op = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64, Op,
10374                    DAG.getTargetConstant(VectorIndex, dl, MVT::i32));
10375   return Op;
10376 }
10377 
10378 // ATOMIC_CMP_SWAP for i8/i16 needs to zero-extend its input since it will be
10379 // compared to a value that is atomically loaded (atomic loads zero-extend).
10380 SDValue PPCTargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op,
10381                                                 SelectionDAG &DAG) const {
10382   assert(Op.getOpcode() == ISD::ATOMIC_CMP_SWAP &&
10383          "Expecting an atomic compare-and-swap here.");
10384   SDLoc dl(Op);
10385   auto *AtomicNode = cast<AtomicSDNode>(Op.getNode());
10386   EVT MemVT = AtomicNode->getMemoryVT();
10387   if (MemVT.getSizeInBits() >= 32)
10388     return Op;
10389 
10390   SDValue CmpOp = Op.getOperand(2);
10391   // If this is already correctly zero-extended, leave it alone.
10392   auto HighBits = APInt::getHighBitsSet(32, 32 - MemVT.getSizeInBits());
10393   if (DAG.MaskedValueIsZero(CmpOp, HighBits))
10394     return Op;
10395 
10396   // Clear the high bits of the compare operand.
10397   unsigned MaskVal = (1 << MemVT.getSizeInBits()) - 1;
10398   SDValue NewCmpOp =
10399     DAG.getNode(ISD::AND, dl, MVT::i32, CmpOp,
10400                 DAG.getConstant(MaskVal, dl, MVT::i32));
10401 
10402   // Replace the existing compare operand with the properly zero-extended one.
10403   SmallVector<SDValue, 4> Ops;
10404   for (int i = 0, e = AtomicNode->getNumOperands(); i < e; i++)
10405     Ops.push_back(AtomicNode->getOperand(i));
10406   Ops[2] = NewCmpOp;
10407   MachineMemOperand *MMO = AtomicNode->getMemOperand();
10408   SDVTList Tys = DAG.getVTList(MVT::i32, MVT::Other);
10409   auto NodeTy =
10410     (MemVT == MVT::i8) ? PPCISD::ATOMIC_CMP_SWAP_8 : PPCISD::ATOMIC_CMP_SWAP_16;
10411   return DAG.getMemIntrinsicNode(NodeTy, dl, Tys, Ops, MemVT, MMO);
10412 }
10413 
10414 SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
10415                                                  SelectionDAG &DAG) const {
10416   SDLoc dl(Op);
10417   // Create a stack slot that is 16-byte aligned.
10418   MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
10419   int FrameIdx = MFI.CreateStackObject(16, Align(16), false);
10420   EVT PtrVT = getPointerTy(DAG.getDataLayout());
10421   SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
10422 
10423   // Store the input value into Value#0 of the stack slot.
10424   SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
10425                                MachinePointerInfo());
10426   // Load it out.
10427   return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo());
10428 }
10429 
10430 SDValue PPCTargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
10431                                                   SelectionDAG &DAG) const {
10432   assert(Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
10433          "Should only be called for ISD::INSERT_VECTOR_ELT");
10434 
10435   ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(2));
10436 
10437   EVT VT = Op.getValueType();
10438   SDLoc dl(Op);
10439   SDValue V1 = Op.getOperand(0);
10440   SDValue V2 = Op.getOperand(1);
10441   SDValue V3 = Op.getOperand(2);
10442 
10443   if (VT == MVT::v2f64 && C)
10444     return Op;
10445 
10446   if (Subtarget.isISA3_1()) {
10447     // On P10, we have legal lowering for constant and variable indices for
10448     // integer vectors.
10449     if (VT == MVT::v16i8 || VT == MVT::v8i16 || VT == MVT::v4i32 ||
10450         VT == MVT::v2i64)
10451       return DAG.getNode(PPCISD::VECINSERT, dl, VT, V1, V2, V3);
10452     // For f32 and f64 vectors, we have legal lowering for variable indices.
10453     // For f32 we also have legal lowering when the element is loaded from
10454     // memory.
10455     if (VT == MVT::v4f32 || VT == MVT::v2f64) {
10456       if (!C || (VT == MVT::v4f32 && dyn_cast<LoadSDNode>(V2)))
10457         return DAG.getNode(PPCISD::VECINSERT, dl, VT, V1, V2, V3);
10458       return Op;
10459     }
10460   }
10461 
10462   // Before P10, we have legal lowering for constant indices but not for
10463   // variable ones.
10464   if (!C)
10465     return SDValue();
10466 
10467   // We can use MTVSRZ + VECINSERT for v8i16 and v16i8 types.
10468   if (VT == MVT::v8i16 || VT == MVT::v16i8) {
10469     SDValue Mtvsrz = DAG.getNode(PPCISD::MTVSRZ, dl, VT, V2);
10470     unsigned BytesInEachElement = VT.getVectorElementType().getSizeInBits() / 8;
10471     unsigned InsertAtElement = C->getZExtValue();
10472     unsigned InsertAtByte = InsertAtElement * BytesInEachElement;
10473     if (Subtarget.isLittleEndian()) {
10474       InsertAtByte = (16 - BytesInEachElement) - InsertAtByte;
10475     }
10476     return DAG.getNode(PPCISD::VECINSERT, dl, VT, V1, Mtvsrz,
10477                        DAG.getConstant(InsertAtByte, dl, MVT::i32));
10478   }
10479   return Op;
10480 }
10481 
10482 SDValue PPCTargetLowering::LowerVectorLoad(SDValue Op,
10483                                            SelectionDAG &DAG) const {
10484   SDLoc dl(Op);
10485   LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
10486   SDValue LoadChain = LN->getChain();
10487   SDValue BasePtr = LN->getBasePtr();
10488   EVT VT = Op.getValueType();
10489 
10490   if (VT != MVT::v256i1 && VT != MVT::v512i1)
10491     return Op;
10492 
10493   // Type v256i1 is used for pairs and v512i1 is used for accumulators.
10494   // Here we create 2 or 4 v16i8 loads to load the pair or accumulator value in
10495   // 2 or 4 vsx registers.
10496   assert((VT != MVT::v512i1 || Subtarget.hasMMA()) &&
10497          "Type unsupported without MMA");
10498   assert((VT != MVT::v256i1 || Subtarget.pairedVectorMemops()) &&
10499          "Type unsupported without paired vector support");
10500   Align Alignment = LN->getAlign();
10501   SmallVector<SDValue, 4> Loads;
10502   SmallVector<SDValue, 4> LoadChains;
10503   unsigned NumVecs = VT.getSizeInBits() / 128;
10504   for (unsigned Idx = 0; Idx < NumVecs; ++Idx) {
10505     SDValue Load =
10506         DAG.getLoad(MVT::v16i8, dl, LoadChain, BasePtr,
10507                     LN->getPointerInfo().getWithOffset(Idx * 16),
10508                     commonAlignment(Alignment, Idx * 16),
10509                     LN->getMemOperand()->getFlags(), LN->getAAInfo());
10510     BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
10511                           DAG.getConstant(16, dl, BasePtr.getValueType()));
10512     Loads.push_back(Load);
10513     LoadChains.push_back(Load.getValue(1));
10514   }
10515   if (Subtarget.isLittleEndian()) {
10516     std::reverse(Loads.begin(), Loads.end());
10517     std::reverse(LoadChains.begin(), LoadChains.end());
10518   }
10519   SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
10520   SDValue Value =
10521       DAG.getNode(VT == MVT::v512i1 ? PPCISD::ACC_BUILD : PPCISD::PAIR_BUILD,
10522                   dl, VT, Loads);
10523   SDValue RetOps[] = {Value, TF};
10524   return DAG.getMergeValues(RetOps, dl);
10525 }
10526 
10527 SDValue PPCTargetLowering::LowerVectorStore(SDValue Op,
10528                                             SelectionDAG &DAG) const {
10529   SDLoc dl(Op);
10530   StoreSDNode *SN = cast<StoreSDNode>(Op.getNode());
10531   SDValue StoreChain = SN->getChain();
10532   SDValue BasePtr = SN->getBasePtr();
10533   SDValue Value = SN->getValue();
10534   EVT StoreVT = Value.getValueType();
10535 
10536   if (StoreVT != MVT::v256i1 && StoreVT != MVT::v512i1)
10537     return Op;
10538 
10539   // Type v256i1 is used for pairs and v512i1 is used for accumulators.
10540   // Here we create 2 or 4 v16i8 stores to store the pair or accumulator
10541   // underlying registers individually.
10542   assert((StoreVT != MVT::v512i1 || Subtarget.hasMMA()) &&
10543          "Type unsupported without MMA");
10544   assert((StoreVT != MVT::v256i1 || Subtarget.pairedVectorMemops()) &&
10545          "Type unsupported without paired vector support");
10546   Align Alignment = SN->getAlign();
10547   SmallVector<SDValue, 4> Stores;
10548   unsigned NumVecs = 2;
10549   if (StoreVT == MVT::v512i1) {
10550     Value = DAG.getNode(PPCISD::XXMFACC, dl, MVT::v512i1, Value);
10551     NumVecs = 4;
10552   }
10553   for (unsigned Idx = 0; Idx < NumVecs; ++Idx) {
10554     unsigned VecNum = Subtarget.isLittleEndian() ? NumVecs - 1 - Idx : Idx;
10555     SDValue Elt = DAG.getNode(PPCISD::EXTRACT_VSX_REG, dl, MVT::v16i8, Value,
10556                               DAG.getConstant(VecNum, dl, getPointerTy(DAG.getDataLayout())));
10557     SDValue Store =
10558         DAG.getStore(StoreChain, dl, Elt, BasePtr,
10559                      SN->getPointerInfo().getWithOffset(Idx * 16),
10560                      commonAlignment(Alignment, Idx * 16),
10561                      SN->getMemOperand()->getFlags(), SN->getAAInfo());
10562     BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
10563                           DAG.getConstant(16, dl, BasePtr.getValueType()));
10564     Stores.push_back(Store);
10565   }
10566   SDValue TF = DAG.getTokenFactor(dl, Stores);
10567   return TF;
10568 }
10569 
10570 SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10571   SDLoc dl(Op);
10572   if (Op.getValueType() == MVT::v4i32) {
10573     SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
10574 
10575     SDValue Zero = getCanonicalConstSplat(0, 1, MVT::v4i32, DAG, dl);
10576     // +16 as shift amt.
10577     SDValue Neg16 = getCanonicalConstSplat(-16, 4, MVT::v4i32, DAG, dl);
10578     SDValue RHSSwap =   // = vrlw RHS, 16
10579       BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
10580 
10581     // Shrinkify inputs to v8i16.
10582     LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
10583     RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
10584     RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
10585 
10586     // Low parts multiplied together, generating 32-bit results (we ignore the
10587     // top parts).
10588     SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
10589                                         LHS, RHS, DAG, dl, MVT::v4i32);
10590 
10591     SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
10592                                       LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
10593     // Shift the high parts up 16 bits.
10594     HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
10595                               Neg16, DAG, dl);
10596     return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
10597   } else if (Op.getValueType() == MVT::v16i8) {
10598     SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
10599     bool isLittleEndian = Subtarget.isLittleEndian();
10600 
10601     // Multiply the even 8-bit parts, producing 16-bit sums.
10602     SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
10603                                            LHS, RHS, DAG, dl, MVT::v8i16);
10604     EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
10605 
10606     // Multiply the odd 8-bit parts, producing 16-bit sums.
10607     SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
10608                                           LHS, RHS, DAG, dl, MVT::v8i16);
10609     OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
10610 
10611     // Merge the results together.  Because vmuleub and vmuloub are
10612     // instructions with a big-endian bias, we must reverse the
10613     // element numbering and reverse the meaning of "odd" and "even"
10614     // when generating little endian code.
10615     int Ops[16];
10616     for (unsigned i = 0; i != 8; ++i) {
10617       if (isLittleEndian) {
10618         Ops[i*2  ] = 2*i;
10619         Ops[i*2+1] = 2*i+16;
10620       } else {
10621         Ops[i*2  ] = 2*i+1;
10622         Ops[i*2+1] = 2*i+1+16;
10623       }
10624     }
10625     if (isLittleEndian)
10626       return DAG.getVectorShuffle(MVT::v16i8, dl, OddParts, EvenParts, Ops);
10627     else
10628       return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
10629   } else {
10630     llvm_unreachable("Unknown mul to lower!");
10631   }
10632 }
10633 
10634 SDValue PPCTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
10635   bool IsStrict = Op->isStrictFPOpcode();
10636   if (Op.getOperand(IsStrict ? 1 : 0).getValueType() == MVT::f128 &&
10637       !Subtarget.hasP9Vector())
10638     return SDValue();
10639 
10640   return Op;
10641 }
10642 
10643 // Custom lowering for fpext vf32 to v2f64
10644 SDValue PPCTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
10645 
10646   assert(Op.getOpcode() == ISD::FP_EXTEND &&
10647          "Should only be called for ISD::FP_EXTEND");
10648 
10649   // FIXME: handle extends from half precision float vectors on P9.
10650   // We only want to custom lower an extend from v2f32 to v2f64.
10651   if (Op.getValueType() != MVT::v2f64 ||
10652       Op.getOperand(0).getValueType() != MVT::v2f32)
10653     return SDValue();
10654 
10655   SDLoc dl(Op);
10656   SDValue Op0 = Op.getOperand(0);
10657 
10658   switch (Op0.getOpcode()) {
10659   default:
10660     return SDValue();
10661   case ISD::EXTRACT_SUBVECTOR: {
10662     assert(Op0.getNumOperands() == 2 &&
10663            isa<ConstantSDNode>(Op0->getOperand(1)) &&
10664            "Node should have 2 operands with second one being a constant!");
10665 
10666     if (Op0.getOperand(0).getValueType() != MVT::v4f32)
10667       return SDValue();
10668 
10669     // Custom lower is only done for high or low doubleword.
10670     int Idx = cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue();
10671     if (Idx % 2 != 0)
10672       return SDValue();
10673 
10674     // Since input is v4f32, at this point Idx is either 0 or 2.
10675     // Shift to get the doubleword position we want.
10676     int DWord = Idx >> 1;
10677 
10678     // High and low word positions are different on little endian.
10679     if (Subtarget.isLittleEndian())
10680       DWord ^= 0x1;
10681 
10682     return DAG.getNode(PPCISD::FP_EXTEND_HALF, dl, MVT::v2f64,
10683                        Op0.getOperand(0), DAG.getConstant(DWord, dl, MVT::i32));
10684   }
10685   case ISD::FADD:
10686   case ISD::FMUL:
10687   case ISD::FSUB: {
10688     SDValue NewLoad[2];
10689     for (unsigned i = 0, ie = Op0.getNumOperands(); i != ie; ++i) {
10690       // Ensure both input are loads.
10691       SDValue LdOp = Op0.getOperand(i);
10692       if (LdOp.getOpcode() != ISD::LOAD)
10693         return SDValue();
10694       // Generate new load node.
10695       LoadSDNode *LD = cast<LoadSDNode>(LdOp);
10696       SDValue LoadOps[] = {LD->getChain(), LD->getBasePtr()};
10697       NewLoad[i] = DAG.getMemIntrinsicNode(
10698           PPCISD::LD_VSX_LH, dl, DAG.getVTList(MVT::v4f32, MVT::Other), LoadOps,
10699           LD->getMemoryVT(), LD->getMemOperand());
10700     }
10701     SDValue NewOp =
10702         DAG.getNode(Op0.getOpcode(), SDLoc(Op0), MVT::v4f32, NewLoad[0],
10703                     NewLoad[1], Op0.getNode()->getFlags());
10704     return DAG.getNode(PPCISD::FP_EXTEND_HALF, dl, MVT::v2f64, NewOp,
10705                        DAG.getConstant(0, dl, MVT::i32));
10706   }
10707   case ISD::LOAD: {
10708     LoadSDNode *LD = cast<LoadSDNode>(Op0);
10709     SDValue LoadOps[] = {LD->getChain(), LD->getBasePtr()};
10710     SDValue NewLd = DAG.getMemIntrinsicNode(
10711         PPCISD::LD_VSX_LH, dl, DAG.getVTList(MVT::v4f32, MVT::Other), LoadOps,
10712         LD->getMemoryVT(), LD->getMemOperand());
10713     return DAG.getNode(PPCISD::FP_EXTEND_HALF, dl, MVT::v2f64, NewLd,
10714                        DAG.getConstant(0, dl, MVT::i32));
10715   }
10716   }
10717   llvm_unreachable("ERROR:Should return for all cases within swtich.");
10718 }
10719 
10720 /// LowerOperation - Provide custom lowering hooks for some operations.
10721 ///
10722 SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
10723   switch (Op.getOpcode()) {
10724   default: llvm_unreachable("Wasn't expecting to be able to lower this!");
10725   case ISD::ConstantPool:       return LowerConstantPool(Op, DAG);
10726   case ISD::BlockAddress:       return LowerBlockAddress(Op, DAG);
10727   case ISD::GlobalAddress:      return LowerGlobalAddress(Op, DAG);
10728   case ISD::GlobalTLSAddress:   return LowerGlobalTLSAddress(Op, DAG);
10729   case ISD::JumpTable:          return LowerJumpTable(Op, DAG);
10730   case ISD::STRICT_FSETCC:
10731   case ISD::STRICT_FSETCCS:
10732   case ISD::SETCC:              return LowerSETCC(Op, DAG);
10733   case ISD::INIT_TRAMPOLINE:    return LowerINIT_TRAMPOLINE(Op, DAG);
10734   case ISD::ADJUST_TRAMPOLINE:  return LowerADJUST_TRAMPOLINE(Op, DAG);
10735 
10736   // Variable argument lowering.
10737   case ISD::VASTART:            return LowerVASTART(Op, DAG);
10738   case ISD::VAARG:              return LowerVAARG(Op, DAG);
10739   case ISD::VACOPY:             return LowerVACOPY(Op, DAG);
10740 
10741   case ISD::STACKRESTORE:       return LowerSTACKRESTORE(Op, DAG);
10742   case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
10743   case ISD::GET_DYNAMIC_AREA_OFFSET:
10744     return LowerGET_DYNAMIC_AREA_OFFSET(Op, DAG);
10745 
10746   // Exception handling lowering.
10747   case ISD::EH_DWARF_CFA:       return LowerEH_DWARF_CFA(Op, DAG);
10748   case ISD::EH_SJLJ_SETJMP:     return lowerEH_SJLJ_SETJMP(Op, DAG);
10749   case ISD::EH_SJLJ_LONGJMP:    return lowerEH_SJLJ_LONGJMP(Op, DAG);
10750 
10751   case ISD::LOAD:               return LowerLOAD(Op, DAG);
10752   case ISD::STORE:              return LowerSTORE(Op, DAG);
10753   case ISD::TRUNCATE:           return LowerTRUNCATE(Op, DAG);
10754   case ISD::SELECT_CC:          return LowerSELECT_CC(Op, DAG);
10755   case ISD::STRICT_FP_TO_UINT:
10756   case ISD::STRICT_FP_TO_SINT:
10757   case ISD::FP_TO_UINT:
10758   case ISD::FP_TO_SINT:         return LowerFP_TO_INT(Op, DAG, SDLoc(Op));
10759   case ISD::STRICT_UINT_TO_FP:
10760   case ISD::STRICT_SINT_TO_FP:
10761   case ISD::UINT_TO_FP:
10762   case ISD::SINT_TO_FP:         return LowerINT_TO_FP(Op, DAG);
10763   case ISD::FLT_ROUNDS_:        return LowerFLT_ROUNDS_(Op, DAG);
10764 
10765   // Lower 64-bit shifts.
10766   case ISD::SHL_PARTS:          return LowerSHL_PARTS(Op, DAG);
10767   case ISD::SRL_PARTS:          return LowerSRL_PARTS(Op, DAG);
10768   case ISD::SRA_PARTS:          return LowerSRA_PARTS(Op, DAG);
10769 
10770   case ISD::FSHL:               return LowerFunnelShift(Op, DAG);
10771   case ISD::FSHR:               return LowerFunnelShift(Op, DAG);
10772 
10773   // Vector-related lowering.
10774   case ISD::BUILD_VECTOR:       return LowerBUILD_VECTOR(Op, DAG);
10775   case ISD::VECTOR_SHUFFLE:     return LowerVECTOR_SHUFFLE(Op, DAG);
10776   case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
10777   case ISD::SCALAR_TO_VECTOR:   return LowerSCALAR_TO_VECTOR(Op, DAG);
10778   case ISD::INSERT_VECTOR_ELT:  return LowerINSERT_VECTOR_ELT(Op, DAG);
10779   case ISD::MUL:                return LowerMUL(Op, DAG);
10780   case ISD::FP_EXTEND:          return LowerFP_EXTEND(Op, DAG);
10781   case ISD::STRICT_FP_ROUND:
10782   case ISD::FP_ROUND:
10783     return LowerFP_ROUND(Op, DAG);
10784   case ISD::ROTL:               return LowerROTL(Op, DAG);
10785 
10786   // For counter-based loop handling.
10787   case ISD::INTRINSIC_W_CHAIN:  return SDValue();
10788 
10789   case ISD::BITCAST:            return LowerBITCAST(Op, DAG);
10790 
10791   // Frame & Return address.
10792   case ISD::RETURNADDR:         return LowerRETURNADDR(Op, DAG);
10793   case ISD::FRAMEADDR:          return LowerFRAMEADDR(Op, DAG);
10794 
10795   case ISD::INTRINSIC_VOID:
10796     return LowerINTRINSIC_VOID(Op, DAG);
10797   case ISD::BSWAP:
10798     return LowerBSWAP(Op, DAG);
10799   case ISD::ATOMIC_CMP_SWAP:
10800     return LowerATOMIC_CMP_SWAP(Op, DAG);
10801   }
10802 }
10803 
10804 void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
10805                                            SmallVectorImpl<SDValue>&Results,
10806                                            SelectionDAG &DAG) const {
10807   SDLoc dl(N);
10808   switch (N->getOpcode()) {
10809   default:
10810     llvm_unreachable("Do not know how to custom type legalize this operation!");
10811   case ISD::READCYCLECOUNTER: {
10812     SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
10813     SDValue RTB = DAG.getNode(PPCISD::READ_TIME_BASE, dl, VTs, N->getOperand(0));
10814 
10815     Results.push_back(
10816         DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, RTB, RTB.getValue(1)));
10817     Results.push_back(RTB.getValue(2));
10818     break;
10819   }
10820   case ISD::INTRINSIC_W_CHAIN: {
10821     if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
10822         Intrinsic::loop_decrement)
10823       break;
10824 
10825     assert(N->getValueType(0) == MVT::i1 &&
10826            "Unexpected result type for CTR decrement intrinsic");
10827     EVT SVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
10828                                  N->getValueType(0));
10829     SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
10830     SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
10831                                  N->getOperand(1));
10832 
10833     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewInt));
10834     Results.push_back(NewInt.getValue(1));
10835     break;
10836   }
10837   case ISD::VAARG: {
10838     if (!Subtarget.isSVR4ABI() || Subtarget.isPPC64())
10839       return;
10840 
10841     EVT VT = N->getValueType(0);
10842 
10843     if (VT == MVT::i64) {
10844       SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG);
10845 
10846       Results.push_back(NewNode);
10847       Results.push_back(NewNode.getValue(1));
10848     }
10849     return;
10850   }
10851   case ISD::STRICT_FP_TO_SINT:
10852   case ISD::STRICT_FP_TO_UINT:
10853   case ISD::FP_TO_SINT:
10854   case ISD::FP_TO_UINT:
10855     // LowerFP_TO_INT() can only handle f32 and f64.
10856     if (N->getOperand(N->isStrictFPOpcode() ? 1 : 0).getValueType() ==
10857         MVT::ppcf128)
10858       return;
10859     Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
10860     return;
10861   case ISD::TRUNCATE: {
10862     if (!N->getValueType(0).isVector())
10863       return;
10864     SDValue Lowered = LowerTRUNCATEVector(SDValue(N, 0), DAG);
10865     if (Lowered)
10866       Results.push_back(Lowered);
10867     return;
10868   }
10869   case ISD::FSHL:
10870   case ISD::FSHR:
10871     // Don't handle funnel shifts here.
10872     return;
10873   case ISD::BITCAST:
10874     // Don't handle bitcast here.
10875     return;
10876   case ISD::FP_EXTEND:
10877     SDValue Lowered = LowerFP_EXTEND(SDValue(N, 0), DAG);
10878     if (Lowered)
10879       Results.push_back(Lowered);
10880     return;
10881   }
10882 }
10883 
10884 //===----------------------------------------------------------------------===//
10885 //  Other Lowering Code
10886 //===----------------------------------------------------------------------===//
10887 
10888 static Instruction* callIntrinsic(IRBuilder<> &Builder, Intrinsic::ID Id) {
10889   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10890   Function *Func = Intrinsic::getDeclaration(M, Id);
10891   return Builder.CreateCall(Func, {});
10892 }
10893 
10894 // The mappings for emitLeading/TrailingFence is taken from
10895 // http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
10896 Instruction *PPCTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
10897                                                  Instruction *Inst,
10898                                                  AtomicOrdering Ord) const {
10899   if (Ord == AtomicOrdering::SequentiallyConsistent)
10900     return callIntrinsic(Builder, Intrinsic::ppc_sync);
10901   if (isReleaseOrStronger(Ord))
10902     return callIntrinsic(Builder, Intrinsic::ppc_lwsync);
10903   return nullptr;
10904 }
10905 
10906 Instruction *PPCTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
10907                                                   Instruction *Inst,
10908                                                   AtomicOrdering Ord) const {
10909   if (Inst->hasAtomicLoad() && isAcquireOrStronger(Ord)) {
10910     // See http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html and
10911     // http://www.rdrop.com/users/paulmck/scalability/paper/N2745r.2011.03.04a.html
10912     // and http://www.cl.cam.ac.uk/~pes20/cppppc/ for justification.
10913     if (isa<LoadInst>(Inst) && Subtarget.isPPC64())
10914       return Builder.CreateCall(
10915           Intrinsic::getDeclaration(
10916               Builder.GetInsertBlock()->getParent()->getParent(),
10917               Intrinsic::ppc_cfence, {Inst->getType()}),
10918           {Inst});
10919     // FIXME: Can use isync for rmw operation.
10920     return callIntrinsic(Builder, Intrinsic::ppc_lwsync);
10921   }
10922   return nullptr;
10923 }
10924 
10925 MachineBasicBlock *
10926 PPCTargetLowering::EmitAtomicBinary(MachineInstr &MI, MachineBasicBlock *BB,
10927                                     unsigned AtomicSize,
10928                                     unsigned BinOpcode,
10929                                     unsigned CmpOpcode,
10930                                     unsigned CmpPred) const {
10931   // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
10932   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
10933 
10934   auto LoadMnemonic = PPC::LDARX;
10935   auto StoreMnemonic = PPC::STDCX;
10936   switch (AtomicSize) {
10937   default:
10938     llvm_unreachable("Unexpected size of atomic entity");
10939   case 1:
10940     LoadMnemonic = PPC::LBARX;
10941     StoreMnemonic = PPC::STBCX;
10942     assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4");
10943     break;
10944   case 2:
10945     LoadMnemonic = PPC::LHARX;
10946     StoreMnemonic = PPC::STHCX;
10947     assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4");
10948     break;
10949   case 4:
10950     LoadMnemonic = PPC::LWARX;
10951     StoreMnemonic = PPC::STWCX;
10952     break;
10953   case 8:
10954     LoadMnemonic = PPC::LDARX;
10955     StoreMnemonic = PPC::STDCX;
10956     break;
10957   }
10958 
10959   const BasicBlock *LLVM_BB = BB->getBasicBlock();
10960   MachineFunction *F = BB->getParent();
10961   MachineFunction::iterator It = ++BB->getIterator();
10962 
10963   Register dest = MI.getOperand(0).getReg();
10964   Register ptrA = MI.getOperand(1).getReg();
10965   Register ptrB = MI.getOperand(2).getReg();
10966   Register incr = MI.getOperand(3).getReg();
10967   DebugLoc dl = MI.getDebugLoc();
10968 
10969   MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
10970   MachineBasicBlock *loop2MBB =
10971     CmpOpcode ? F->CreateMachineBasicBlock(LLVM_BB) : nullptr;
10972   MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
10973   F->insert(It, loopMBB);
10974   if (CmpOpcode)
10975     F->insert(It, loop2MBB);
10976   F->insert(It, exitMBB);
10977   exitMBB->splice(exitMBB->begin(), BB,
10978                   std::next(MachineBasicBlock::iterator(MI)), BB->end());
10979   exitMBB->transferSuccessorsAndUpdatePHIs(BB);
10980 
10981   MachineRegisterInfo &RegInfo = F->getRegInfo();
10982   Register TmpReg = (!BinOpcode) ? incr :
10983     RegInfo.createVirtualRegister( AtomicSize == 8 ? &PPC::G8RCRegClass
10984                                            : &PPC::GPRCRegClass);
10985 
10986   //  thisMBB:
10987   //   ...
10988   //   fallthrough --> loopMBB
10989   BB->addSuccessor(loopMBB);
10990 
10991   //  loopMBB:
10992   //   l[wd]arx dest, ptr
10993   //   add r0, dest, incr
10994   //   st[wd]cx. r0, ptr
10995   //   bne- loopMBB
10996   //   fallthrough --> exitMBB
10997 
10998   // For max/min...
10999   //  loopMBB:
11000   //   l[wd]arx dest, ptr
11001   //   cmpl?[wd] incr, dest
11002   //   bgt exitMBB
11003   //  loop2MBB:
11004   //   st[wd]cx. dest, ptr
11005   //   bne- loopMBB
11006   //   fallthrough --> exitMBB
11007 
11008   BB = loopMBB;
11009   BuildMI(BB, dl, TII->get(LoadMnemonic), dest)
11010     .addReg(ptrA).addReg(ptrB);
11011   if (BinOpcode)
11012     BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
11013   if (CmpOpcode) {
11014     // Signed comparisons of byte or halfword values must be sign-extended.
11015     if (CmpOpcode == PPC::CMPW && AtomicSize < 4) {
11016       Register ExtReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass);
11017       BuildMI(BB, dl, TII->get(AtomicSize == 1 ? PPC::EXTSB : PPC::EXTSH),
11018               ExtReg).addReg(dest);
11019       BuildMI(BB, dl, TII->get(CmpOpcode), PPC::CR0)
11020         .addReg(incr).addReg(ExtReg);
11021     } else
11022       BuildMI(BB, dl, TII->get(CmpOpcode), PPC::CR0)
11023         .addReg(incr).addReg(dest);
11024 
11025     BuildMI(BB, dl, TII->get(PPC::BCC))
11026       .addImm(CmpPred).addReg(PPC::CR0).addMBB(exitMBB);
11027     BB->addSuccessor(loop2MBB);
11028     BB->addSuccessor(exitMBB);
11029     BB = loop2MBB;
11030   }
11031   BuildMI(BB, dl, TII->get(StoreMnemonic))
11032     .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
11033   BuildMI(BB, dl, TII->get(PPC::BCC))
11034     .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
11035   BB->addSuccessor(loopMBB);
11036   BB->addSuccessor(exitMBB);
11037 
11038   //  exitMBB:
11039   //   ...
11040   BB = exitMBB;
11041   return BB;
11042 }
11043 
11044 static bool isSignExtended(MachineInstr &MI, const PPCInstrInfo *TII) {
11045   switch(MI.getOpcode()) {
11046   default:
11047     return false;
11048   case PPC::COPY:
11049     return TII->isSignExtended(MI);
11050   case PPC::LHA:
11051   case PPC::LHA8:
11052   case PPC::LHAU:
11053   case PPC::LHAU8:
11054   case PPC::LHAUX:
11055   case PPC::LHAUX8:
11056   case PPC::LHAX:
11057   case PPC::LHAX8:
11058   case PPC::LWA:
11059   case PPC::LWAUX:
11060   case PPC::LWAX:
11061   case PPC::LWAX_32:
11062   case PPC::LWA_32:
11063   case PPC::PLHA:
11064   case PPC::PLHA8:
11065   case PPC::PLHA8pc:
11066   case PPC::PLHApc:
11067   case PPC::PLWA:
11068   case PPC::PLWA8:
11069   case PPC::PLWA8pc:
11070   case PPC::PLWApc:
11071   case PPC::EXTSB:
11072   case PPC::EXTSB8:
11073   case PPC::EXTSB8_32_64:
11074   case PPC::EXTSB8_rec:
11075   case PPC::EXTSB_rec:
11076   case PPC::EXTSH:
11077   case PPC::EXTSH8:
11078   case PPC::EXTSH8_32_64:
11079   case PPC::EXTSH8_rec:
11080   case PPC::EXTSH_rec:
11081   case PPC::EXTSW:
11082   case PPC::EXTSWSLI:
11083   case PPC::EXTSWSLI_32_64:
11084   case PPC::EXTSWSLI_32_64_rec:
11085   case PPC::EXTSWSLI_rec:
11086   case PPC::EXTSW_32:
11087   case PPC::EXTSW_32_64:
11088   case PPC::EXTSW_32_64_rec:
11089   case PPC::EXTSW_rec:
11090   case PPC::SRAW:
11091   case PPC::SRAWI:
11092   case PPC::SRAWI_rec:
11093   case PPC::SRAW_rec:
11094     return true;
11095   }
11096   return false;
11097 }
11098 
11099 MachineBasicBlock *PPCTargetLowering::EmitPartwordAtomicBinary(
11100     MachineInstr &MI, MachineBasicBlock *BB,
11101     bool is8bit, // operation
11102     unsigned BinOpcode, unsigned CmpOpcode, unsigned CmpPred) const {
11103   // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
11104   const PPCInstrInfo *TII = Subtarget.getInstrInfo();
11105 
11106   // If this is a signed comparison and the value being compared is not known
11107   // to be sign extended, sign extend it here.
11108   DebugLoc dl = MI.getDebugLoc();
11109   MachineFunction *F = BB->getParent();
11110   MachineRegisterInfo &RegInfo = F->getRegInfo();
11111   Register incr = MI.getOperand(3).getReg();
11112   bool IsSignExtended = Register::isVirtualRegister(incr) &&
11113     isSignExtended(*RegInfo.getVRegDef(incr), TII);
11114 
11115   if (CmpOpcode == PPC::CMPW && !IsSignExtended) {
11116     Register ValueReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass);
11117     BuildMI(*BB, MI, dl, TII->get(is8bit ? PPC::EXTSB : PPC::EXTSH), ValueReg)
11118         .addReg(MI.getOperand(3).getReg());
11119     MI.getOperand(3).setReg(ValueReg);
11120   }
11121   // If we support part-word atomic mnemonics, just use them
11122   if (Subtarget.hasPartwordAtomics())
11123     return EmitAtomicBinary(MI, BB, is8bit ? 1 : 2, BinOpcode, CmpOpcode,
11124                             CmpPred);
11125 
11126   // In 64 bit mode we have to use 64 bits for addresses, even though the
11127   // lwarx/stwcx are 32 bits.  With the 32-bit atomics we can use address
11128   // registers without caring whether they're 32 or 64, but here we're
11129   // doing actual arithmetic on the addresses.
11130   bool is64bit = Subtarget.isPPC64();
11131   bool isLittleEndian = Subtarget.isLittleEndian();
11132   unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
11133 
11134   const BasicBlock *LLVM_BB = BB->getBasicBlock();
11135   MachineFunction::iterator It = ++BB->getIterator();
11136 
11137   Register dest = MI.getOperand(0).getReg();
11138   Register ptrA = MI.getOperand(1).getReg();
11139   Register ptrB = MI.getOperand(2).getReg();
11140 
11141   MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
11142   MachineBasicBlock *loop2MBB =
11143       CmpOpcode ? F->CreateMachineBasicBlock(LLVM_BB) : nullptr;
11144   MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
11145   F->insert(It, loopMBB);
11146   if (CmpOpcode)
11147     F->insert(It, loop2MBB);
11148   F->insert(It, exitMBB);
11149   exitMBB->splice(exitMBB->begin(), BB,
11150                   std::next(MachineBasicBlock::iterator(MI)), BB->end());
11151   exitMBB->transferSuccessorsAndUpdatePHIs(BB);
11152 
11153   const TargetRegisterClass *RC =
11154       is64bit ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
11155   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
11156 
11157   Register PtrReg = RegInfo.createVirtualRegister(RC);
11158   Register Shift1Reg = RegInfo.createVirtualRegister(GPRC);
11159   Register ShiftReg =
11160       isLittleEndian ? Shift1Reg : RegInfo.createVirtualRegister(GPRC);
11161   Register Incr2Reg = RegInfo.createVirtualRegister(GPRC);
11162   Register MaskReg = RegInfo.createVirtualRegister(GPRC);
11163   Register Mask2Reg = RegInfo.createVirtualRegister(GPRC);
11164   Register Mask3Reg = RegInfo.createVirtualRegister(GPRC);
11165   Register Tmp2Reg = RegInfo.createVirtualRegister(GPRC);
11166   Register Tmp3Reg = RegInfo.createVirtualRegister(GPRC);
11167   Register Tmp4Reg = RegInfo.createVirtualRegister(GPRC);
11168   Register TmpDestReg = RegInfo.createVirtualRegister(GPRC);
11169   Register Ptr1Reg;
11170   Register TmpReg =
11171       (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(GPRC);
11172 
11173   //  thisMBB:
11174   //   ...
11175   //   fallthrough --> loopMBB
11176   BB->addSuccessor(loopMBB);
11177 
11178   // The 4-byte load must be aligned, while a char or short may be
11179   // anywhere in the word.  Hence all this nasty bookkeeping code.
11180   //   add ptr1, ptrA, ptrB [copy if ptrA==0]
11181   //   rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
11182   //   xori shift, shift1, 24 [16]
11183   //   rlwinm ptr, ptr1, 0, 0, 29
11184   //   slw incr2, incr, shift
11185   //   li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
11186   //   slw mask, mask2, shift
11187   //  loopMBB:
11188   //   lwarx tmpDest, ptr
11189   //   add tmp, tmpDest, incr2
11190   //   andc tmp2, tmpDest, mask
11191   //   and tmp3, tmp, mask
11192   //   or tmp4, tmp3, tmp2
11193   //   stwcx. tmp4, ptr
11194   //   bne- loopMBB
11195   //   fallthrough --> exitMBB
11196   //   srw dest, tmpDest, shift
11197   if (ptrA != ZeroReg) {
11198     Ptr1Reg = RegInfo.createVirtualRegister(RC);
11199     BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
11200         .addReg(ptrA)
11201         .addReg(ptrB);
11202   } else {
11203     Ptr1Reg = ptrB;
11204   }
11205   // We need use 32-bit subregister to avoid mismatch register class in 64-bit
11206   // mode.
11207   BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg)
11208       .addReg(Ptr1Reg, 0, is64bit ? PPC::sub_32 : 0)
11209       .addImm(3)
11210       .addImm(27)
11211       .addImm(is8bit ? 28 : 27);
11212   if (!isLittleEndian)
11213     BuildMI(BB, dl, TII->get(PPC::XORI), ShiftReg)
11214         .addReg(Shift1Reg)
11215         .addImm(is8bit ? 24 : 16);
11216   if (is64bit)
11217     BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
11218         .addReg(Ptr1Reg)
11219         .addImm(0)
11220         .addImm(61);
11221   else
11222     BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
11223         .addReg(Ptr1Reg)
11224         .addImm(0)
11225         .addImm(0)
11226         .addImm(29);
11227   BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg).addReg(incr).addReg(ShiftReg);
11228   if (is8bit)
11229     BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
11230   else {
11231     BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
11232     BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
11233         .addReg(Mask3Reg)
11234         .addImm(65535);
11235   }
11236   BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
11237       .addReg(Mask2Reg)
11238       .addReg(ShiftReg);
11239 
11240   BB = loopMBB;
11241   BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
11242       .addReg(ZeroReg)
11243       .addReg(PtrReg);
11244   if (BinOpcode)
11245     BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
11246         .addReg(Incr2Reg)
11247         .addReg(TmpDestReg);
11248   BuildMI(BB, dl, TII->get(PPC::ANDC), Tmp2Reg)
11249       .addReg(TmpDestReg)
11250       .addReg(MaskReg);
11251   BuildMI(BB, dl, TII->get(PPC::AND), Tmp3Reg).addReg(TmpReg).addReg(MaskReg);
11252   if (CmpOpcode) {
11253     // For unsigned comparisons, we can directly compare the shifted values.
11254     // For signed comparisons we shift and sign extend.
11255     Register SReg = RegInfo.createVirtualRegister(GPRC);
11256     BuildMI(BB, dl, TII->get(PPC::AND), SReg)
11257         .addReg(TmpDestReg)
11258         .addReg(MaskReg);
11259     unsigned ValueReg = SReg;
11260     unsigned CmpReg = Incr2Reg;
11261     if (CmpOpcode == PPC::CMPW) {
11262       ValueReg = RegInfo.createVirtualRegister(GPRC);
11263       BuildMI(BB, dl, TII->get(PPC::SRW), ValueReg)
11264           .addReg(SReg)
11265           .addReg(ShiftReg);
11266       Register ValueSReg = RegInfo.createVirtualRegister(GPRC);
11267       BuildMI(BB, dl, TII->get(is8bit ? PPC::EXTSB : PPC::EXTSH), ValueSReg)
11268           .addReg(ValueReg);
11269       ValueReg = ValueSReg;
11270       CmpReg = incr;
11271     }
11272     BuildMI(BB, dl, TII->get(CmpOpcode), PPC::CR0)
11273         .addReg(CmpReg)
11274         .addReg(ValueReg);
11275     BuildMI(BB, dl, TII->get(PPC::BCC))
11276         .addImm(CmpPred)
11277         .addReg(PPC::CR0)
11278         .addMBB(exitMBB);
11279     BB->addSuccessor(loop2MBB);
11280     BB->addSuccessor(exitMBB);
11281     BB = loop2MBB;
11282   }
11283   BuildMI(BB, dl, TII->get(PPC::OR), Tmp4Reg).addReg(Tmp3Reg).addReg(Tmp2Reg);
11284   BuildMI(BB, dl, TII->get(PPC::STWCX))
11285       .addReg(Tmp4Reg)
11286       .addReg(ZeroReg)
11287       .addReg(PtrReg);
11288   BuildMI(BB, dl, TII->get(PPC::BCC))
11289       .addImm(PPC::PRED_NE)
11290       .addReg(PPC::CR0)
11291       .addMBB(loopMBB);
11292   BB->addSuccessor(loopMBB);
11293   BB->addSuccessor(exitMBB);
11294 
11295   //  exitMBB:
11296   //   ...
11297   BB = exitMBB;
11298   BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest)
11299       .addReg(TmpDestReg)
11300       .addReg(ShiftReg);
11301   return BB;
11302 }
11303 
11304 llvm::MachineBasicBlock *
11305 PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr &MI,
11306                                     MachineBasicBlock *MBB) const {
11307   DebugLoc DL = MI.getDebugLoc();
11308   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
11309   const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo();
11310 
11311   MachineFunction *MF = MBB->getParent();
11312   MachineRegisterInfo &MRI = MF->getRegInfo();
11313 
11314   const BasicBlock *BB = MBB->getBasicBlock();
11315   MachineFunction::iterator I = ++MBB->getIterator();
11316 
11317   Register DstReg = MI.getOperand(0).getReg();
11318   const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
11319   assert(TRI->isTypeLegalForClass(*RC, MVT::i32) && "Invalid destination!");
11320   Register mainDstReg = MRI.createVirtualRegister(RC);
11321   Register restoreDstReg = MRI.createVirtualRegister(RC);
11322 
11323   MVT PVT = getPointerTy(MF->getDataLayout());
11324   assert((PVT == MVT::i64 || PVT == MVT::i32) &&
11325          "Invalid Pointer Size!");
11326   // For v = setjmp(buf), we generate
11327   //
11328   // thisMBB:
11329   //  SjLjSetup mainMBB
11330   //  bl mainMBB
11331   //  v_restore = 1
11332   //  b sinkMBB
11333   //
11334   // mainMBB:
11335   //  buf[LabelOffset] = LR
11336   //  v_main = 0
11337   //
11338   // sinkMBB:
11339   //  v = phi(main, restore)
11340   //
11341 
11342   MachineBasicBlock *thisMBB = MBB;
11343   MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
11344   MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
11345   MF->insert(I, mainMBB);
11346   MF->insert(I, sinkMBB);
11347 
11348   MachineInstrBuilder MIB;
11349 
11350   // Transfer the remainder of BB and its successor edges to sinkMBB.
11351   sinkMBB->splice(sinkMBB->begin(), MBB,
11352                   std::next(MachineBasicBlock::iterator(MI)), MBB->end());
11353   sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
11354 
11355   // Note that the structure of the jmp_buf used here is not compatible
11356   // with that used by libc, and is not designed to be. Specifically, it
11357   // stores only those 'reserved' registers that LLVM does not otherwise
11358   // understand how to spill. Also, by convention, by the time this
11359   // intrinsic is called, Clang has already stored the frame address in the
11360   // first slot of the buffer and stack address in the third. Following the
11361   // X86 target code, we'll store the jump address in the second slot. We also
11362   // need to save the TOC pointer (R2) to handle jumps between shared
11363   // libraries, and that will be stored in the fourth slot. The thread
11364   // identifier (R13) is not affected.
11365 
11366   // thisMBB:
11367   const int64_t LabelOffset = 1 * PVT.getStoreSize();
11368   const int64_t TOCOffset   = 3 * PVT.getStoreSize();
11369   const int64_t BPOffset    = 4 * PVT.getStoreSize();
11370 
11371   // Prepare IP either in reg.
11372   const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
11373   Register LabelReg = MRI.createVirtualRegister(PtrRC);
11374   Register BufReg = MI.getOperand(1).getReg();
11375 
11376   if (Subtarget.is64BitELFABI()) {
11377     setUsesTOCBasePtr(*MBB->getParent());
11378     MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
11379               .addReg(PPC::X2)
11380               .addImm(TOCOffset)
11381               .addReg(BufReg)
11382               .cloneMemRefs(MI);
11383   }
11384 
11385   // Naked functions never have a base pointer, and so we use r1. For all
11386   // other functions, this decision must be delayed until during PEI.
11387   unsigned BaseReg;
11388   if (MF->getFunction().hasFnAttribute(Attribute::Naked))
11389     BaseReg = Subtarget.isPPC64() ? PPC::X1 : PPC::R1;
11390   else
11391     BaseReg = Subtarget.isPPC64() ? PPC::BP8 : PPC::BP;
11392 
11393   MIB = BuildMI(*thisMBB, MI, DL,
11394                 TII->get(Subtarget.isPPC64() ? PPC::STD : PPC::STW))
11395             .addReg(BaseReg)
11396             .addImm(BPOffset)
11397             .addReg(BufReg)
11398             .cloneMemRefs(MI);
11399 
11400   // Setup
11401   MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
11402   MIB.addRegMask(TRI->getNoPreservedMask());
11403 
11404   BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
11405 
11406   MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
11407           .addMBB(mainMBB);
11408   MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
11409 
11410   thisMBB->addSuccessor(mainMBB, BranchProbability::getZero());
11411   thisMBB->addSuccessor(sinkMBB, BranchProbability::getOne());
11412 
11413   // mainMBB:
11414   //  mainDstReg = 0
11415   MIB =
11416       BuildMI(mainMBB, DL,
11417               TII->get(Subtarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
11418 
11419   // Store IP
11420   if (Subtarget.isPPC64()) {
11421     MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
11422             .addReg(LabelReg)
11423             .addImm(LabelOffset)
11424             .addReg(BufReg);
11425   } else {
11426     MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
11427             .addReg(LabelReg)
11428             .addImm(LabelOffset)
11429             .addReg(BufReg);
11430   }
11431   MIB.cloneMemRefs(MI);
11432 
11433   BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
11434   mainMBB->addSuccessor(sinkMBB);
11435 
11436   // sinkMBB:
11437   BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11438           TII->get(PPC::PHI), DstReg)
11439     .addReg(mainDstReg).addMBB(mainMBB)
11440     .addReg(restoreDstReg).addMBB(thisMBB);
11441 
11442   MI.eraseFromParent();
11443   return sinkMBB;
11444 }
11445 
11446 MachineBasicBlock *
11447 PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr &MI,
11448                                      MachineBasicBlock *MBB) const {
11449   DebugLoc DL = MI.getDebugLoc();
11450   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
11451 
11452   MachineFunction *MF = MBB->getParent();
11453   MachineRegisterInfo &MRI = MF->getRegInfo();
11454 
11455   MVT PVT = getPointerTy(MF->getDataLayout());
11456   assert((PVT == MVT::i64 || PVT == MVT::i32) &&
11457          "Invalid Pointer Size!");
11458 
11459   const TargetRegisterClass *RC =
11460     (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
11461   Register Tmp = MRI.createVirtualRegister(RC);
11462   // Since FP is only updated here but NOT referenced, it's treated as GPR.
11463   unsigned FP  = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
11464   unsigned SP  = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
11465   unsigned BP =
11466       (PVT == MVT::i64)
11467           ? PPC::X30
11468           : (Subtarget.isSVR4ABI() && isPositionIndependent() ? PPC::R29
11469                                                               : PPC::R30);
11470 
11471   MachineInstrBuilder MIB;
11472 
11473   const int64_t LabelOffset = 1 * PVT.getStoreSize();
11474   const int64_t SPOffset    = 2 * PVT.getStoreSize();
11475   const int64_t TOCOffset   = 3 * PVT.getStoreSize();
11476   const int64_t BPOffset    = 4 * PVT.getStoreSize();
11477 
11478   Register BufReg = MI.getOperand(0).getReg();
11479 
11480   // Reload FP (the jumped-to function may not have had a
11481   // frame pointer, and if so, then its r31 will be restored
11482   // as necessary).
11483   if (PVT == MVT::i64) {
11484     MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
11485             .addImm(0)
11486             .addReg(BufReg);
11487   } else {
11488     MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
11489             .addImm(0)
11490             .addReg(BufReg);
11491   }
11492   MIB.cloneMemRefs(MI);
11493 
11494   // Reload IP
11495   if (PVT == MVT::i64) {
11496     MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
11497             .addImm(LabelOffset)
11498             .addReg(BufReg);
11499   } else {
11500     MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
11501             .addImm(LabelOffset)
11502             .addReg(BufReg);
11503   }
11504   MIB.cloneMemRefs(MI);
11505 
11506   // Reload SP
11507   if (PVT == MVT::i64) {
11508     MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
11509             .addImm(SPOffset)
11510             .addReg(BufReg);
11511   } else {
11512     MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
11513             .addImm(SPOffset)
11514             .addReg(BufReg);
11515   }
11516   MIB.cloneMemRefs(MI);
11517 
11518   // Reload BP
11519   if (PVT == MVT::i64) {
11520     MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
11521             .addImm(BPOffset)
11522             .addReg(BufReg);
11523   } else {
11524     MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
11525             .addImm(BPOffset)
11526             .addReg(BufReg);
11527   }
11528   MIB.cloneMemRefs(MI);
11529 
11530   // Reload TOC
11531   if (PVT == MVT::i64 && Subtarget.isSVR4ABI()) {
11532     setUsesTOCBasePtr(*MBB->getParent());
11533     MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
11534               .addImm(TOCOffset)
11535               .addReg(BufReg)
11536               .cloneMemRefs(MI);
11537   }
11538 
11539   // Jump
11540   BuildMI(*MBB, MI, DL,
11541           TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
11542   BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
11543 
11544   MI.eraseFromParent();
11545   return MBB;
11546 }
11547 
11548 bool PPCTargetLowering::hasInlineStackProbe(MachineFunction &MF) const {
11549   // If the function specifically requests inline stack probes, emit them.
11550   if (MF.getFunction().hasFnAttribute("probe-stack"))
11551     return MF.getFunction().getFnAttribute("probe-stack").getValueAsString() ==
11552            "inline-asm";
11553   return false;
11554 }
11555 
11556 unsigned PPCTargetLowering::getStackProbeSize(MachineFunction &MF) const {
11557   const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
11558   unsigned StackAlign = TFI->getStackAlignment();
11559   assert(StackAlign >= 1 && isPowerOf2_32(StackAlign) &&
11560          "Unexpected stack alignment");
11561   // The default stack probe size is 4096 if the function has no
11562   // stack-probe-size attribute.
11563   unsigned StackProbeSize = 4096;
11564   const Function &Fn = MF.getFunction();
11565   if (Fn.hasFnAttribute("stack-probe-size"))
11566     Fn.getFnAttribute("stack-probe-size")
11567         .getValueAsString()
11568         .getAsInteger(0, StackProbeSize);
11569   // Round down to the stack alignment.
11570   StackProbeSize &= ~(StackAlign - 1);
11571   return StackProbeSize ? StackProbeSize : StackAlign;
11572 }
11573 
11574 // Lower dynamic stack allocation with probing. `emitProbedAlloca` is splitted
11575 // into three phases. In the first phase, it uses pseudo instruction
11576 // PREPARE_PROBED_ALLOCA to get the future result of actual FramePointer and
11577 // FinalStackPtr. In the second phase, it generates a loop for probing blocks.
11578 // At last, it uses pseudo instruction DYNAREAOFFSET to get the future result of
11579 // MaxCallFrameSize so that it can calculate correct data area pointer.
11580 MachineBasicBlock *
11581 PPCTargetLowering::emitProbedAlloca(MachineInstr &MI,
11582                                     MachineBasicBlock *MBB) const {
11583   const bool isPPC64 = Subtarget.isPPC64();
11584   MachineFunction *MF = MBB->getParent();
11585   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
11586   DebugLoc DL = MI.getDebugLoc();
11587   const unsigned ProbeSize = getStackProbeSize(*MF);
11588   const BasicBlock *ProbedBB = MBB->getBasicBlock();
11589   MachineRegisterInfo &MRI = MF->getRegInfo();
11590   // The CFG of probing stack looks as
11591   //         +-----+
11592   //         | MBB |
11593   //         +--+--+
11594   //            |
11595   //       +----v----+
11596   //  +--->+ TestMBB +---+
11597   //  |    +----+----+   |
11598   //  |         |        |
11599   //  |   +-----v----+   |
11600   //  +---+ BlockMBB |   |
11601   //      +----------+   |
11602   //                     |
11603   //       +---------+   |
11604   //       | TailMBB +<--+
11605   //       +---------+
11606   // In MBB, calculate previous frame pointer and final stack pointer.
11607   // In TestMBB, test if sp is equal to final stack pointer, if so, jump to
11608   // TailMBB. In BlockMBB, update the sp atomically and jump back to TestMBB.
11609   // TailMBB is spliced via \p MI.
11610   MachineBasicBlock *TestMBB = MF->CreateMachineBasicBlock(ProbedBB);
11611   MachineBasicBlock *TailMBB = MF->CreateMachineBasicBlock(ProbedBB);
11612   MachineBasicBlock *BlockMBB = MF->CreateMachineBasicBlock(ProbedBB);
11613 
11614   MachineFunction::iterator MBBIter = ++MBB->getIterator();
11615   MF->insert(MBBIter, TestMBB);
11616   MF->insert(MBBIter, BlockMBB);
11617   MF->insert(MBBIter, TailMBB);
11618 
11619   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
11620   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
11621 
11622   Register DstReg = MI.getOperand(0).getReg();
11623   Register NegSizeReg = MI.getOperand(1).getReg();
11624   Register SPReg = isPPC64 ? PPC::X1 : PPC::R1;
11625   Register FinalStackPtr = MRI.createVirtualRegister(isPPC64 ? G8RC : GPRC);
11626   Register FramePointer = MRI.createVirtualRegister(isPPC64 ? G8RC : GPRC);
11627   Register ActualNegSizeReg = MRI.createVirtualRegister(isPPC64 ? G8RC : GPRC);
11628 
11629   // Since value of NegSizeReg might be realigned in prologepilog, insert a
11630   // PREPARE_PROBED_ALLOCA pseudo instruction to get actual FramePointer and
11631   // NegSize.
11632   unsigned ProbeOpc;
11633   if (!MRI.hasOneNonDBGUse(NegSizeReg))
11634     ProbeOpc =
11635         isPPC64 ? PPC::PREPARE_PROBED_ALLOCA_64 : PPC::PREPARE_PROBED_ALLOCA_32;
11636   else
11637     // By introducing PREPARE_PROBED_ALLOCA_NEGSIZE_OPT, ActualNegSizeReg
11638     // and NegSizeReg will be allocated in the same phyreg to avoid
11639     // redundant copy when NegSizeReg has only one use which is current MI and
11640     // will be replaced by PREPARE_PROBED_ALLOCA then.
11641     ProbeOpc = isPPC64 ? PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_64
11642                        : PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_32;
11643   BuildMI(*MBB, {MI}, DL, TII->get(ProbeOpc), FramePointer)
11644       .addDef(ActualNegSizeReg)
11645       .addReg(NegSizeReg)
11646       .add(MI.getOperand(2))
11647       .add(MI.getOperand(3));
11648 
11649   // Calculate final stack pointer, which equals to SP + ActualNegSize.
11650   BuildMI(*MBB, {MI}, DL, TII->get(isPPC64 ? PPC::ADD8 : PPC::ADD4),
11651           FinalStackPtr)
11652       .addReg(SPReg)
11653       .addReg(ActualNegSizeReg);
11654 
11655   // Materialize a scratch register for update.
11656   int64_t NegProbeSize = -(int64_t)ProbeSize;
11657   assert(isInt<32>(NegProbeSize) && "Unhandled probe size!");
11658   Register ScratchReg = MRI.createVirtualRegister(isPPC64 ? G8RC : GPRC);
11659   if (!isInt<16>(NegProbeSize)) {
11660     Register TempReg = MRI.createVirtualRegister(isPPC64 ? G8RC : GPRC);
11661     BuildMI(*MBB, {MI}, DL, TII->get(isPPC64 ? PPC::LIS8 : PPC::LIS), TempReg)
11662         .addImm(NegProbeSize >> 16);
11663     BuildMI(*MBB, {MI}, DL, TII->get(isPPC64 ? PPC::ORI8 : PPC::ORI),
11664             ScratchReg)
11665         .addReg(TempReg)
11666         .addImm(NegProbeSize & 0xFFFF);
11667   } else
11668     BuildMI(*MBB, {MI}, DL, TII->get(isPPC64 ? PPC::LI8 : PPC::LI), ScratchReg)
11669         .addImm(NegProbeSize);
11670 
11671   {
11672     // Probing leading residual part.
11673     Register Div = MRI.createVirtualRegister(isPPC64 ? G8RC : GPRC);
11674     BuildMI(*MBB, {MI}, DL, TII->get(isPPC64 ? PPC::DIVD : PPC::DIVW), Div)
11675         .addReg(ActualNegSizeReg)
11676         .addReg(ScratchReg);
11677     Register Mul = MRI.createVirtualRegister(isPPC64 ? G8RC : GPRC);
11678     BuildMI(*MBB, {MI}, DL, TII->get(isPPC64 ? PPC::MULLD : PPC::MULLW), Mul)
11679         .addReg(Div)
11680         .addReg(ScratchReg);
11681     Register NegMod = MRI.createVirtualRegister(isPPC64 ? G8RC : GPRC);
11682     BuildMI(*MBB, {MI}, DL, TII->get(isPPC64 ? PPC::SUBF8 : PPC::SUBF), NegMod)
11683         .addReg(Mul)
11684         .addReg(ActualNegSizeReg);
11685     BuildMI(*MBB, {MI}, DL, TII->get(isPPC64 ? PPC::STDUX : PPC::STWUX), SPReg)
11686         .addReg(FramePointer)
11687         .addReg(SPReg)
11688         .addReg(NegMod);
11689   }
11690 
11691   {
11692     // Remaining part should be multiple of ProbeSize.
11693     Register CmpResult = MRI.createVirtualRegister(&PPC::CRRCRegClass);
11694     BuildMI(TestMBB, DL, TII->get(isPPC64 ? PPC::CMPD : PPC::CMPW), CmpResult)
11695         .addReg(SPReg)
11696         .addReg(FinalStackPtr);
11697     BuildMI(TestMBB, DL, TII->get(PPC::BCC))
11698         .addImm(PPC::PRED_EQ)
11699         .addReg(CmpResult)
11700         .addMBB(TailMBB);
11701     TestMBB->addSuccessor(BlockMBB);
11702     TestMBB->addSuccessor(TailMBB);
11703   }
11704 
11705   {
11706     // Touch the block.
11707     // |P...|P...|P...
11708     BuildMI(BlockMBB, DL, TII->get(isPPC64 ? PPC::STDUX : PPC::STWUX), SPReg)
11709         .addReg(FramePointer)
11710         .addReg(SPReg)
11711         .addReg(ScratchReg);
11712     BuildMI(BlockMBB, DL, TII->get(PPC::B)).addMBB(TestMBB);
11713     BlockMBB->addSuccessor(TestMBB);
11714   }
11715 
11716   // Calculation of MaxCallFrameSize is deferred to prologepilog, use
11717   // DYNAREAOFFSET pseudo instruction to get the future result.
11718   Register MaxCallFrameSizeReg =
11719       MRI.createVirtualRegister(isPPC64 ? G8RC : GPRC);
11720   BuildMI(TailMBB, DL,
11721           TII->get(isPPC64 ? PPC::DYNAREAOFFSET8 : PPC::DYNAREAOFFSET),
11722           MaxCallFrameSizeReg)
11723       .add(MI.getOperand(2))
11724       .add(MI.getOperand(3));
11725   BuildMI(TailMBB, DL, TII->get(isPPC64 ? PPC::ADD8 : PPC::ADD4), DstReg)
11726       .addReg(SPReg)
11727       .addReg(MaxCallFrameSizeReg);
11728 
11729   // Splice instructions after MI to TailMBB.
11730   TailMBB->splice(TailMBB->end(), MBB,
11731                   std::next(MachineBasicBlock::iterator(MI)), MBB->end());
11732   TailMBB->transferSuccessorsAndUpdatePHIs(MBB);
11733   MBB->addSuccessor(TestMBB);
11734 
11735   // Delete the pseudo instruction.
11736   MI.eraseFromParent();
11737 
11738   ++NumDynamicAllocaProbed;
11739   return TailMBB;
11740 }
11741 
11742 MachineBasicBlock *
11743 PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
11744                                                MachineBasicBlock *BB) const {
11745   if (MI.getOpcode() == TargetOpcode::STACKMAP ||
11746       MI.getOpcode() == TargetOpcode::PATCHPOINT) {
11747     if (Subtarget.is64BitELFABI() &&
11748         MI.getOpcode() == TargetOpcode::PATCHPOINT &&
11749         !Subtarget.isUsingPCRelativeCalls()) {
11750       // Call lowering should have added an r2 operand to indicate a dependence
11751       // on the TOC base pointer value. It can't however, because there is no
11752       // way to mark the dependence as implicit there, and so the stackmap code
11753       // will confuse it with a regular operand. Instead, add the dependence
11754       // here.
11755       MI.addOperand(MachineOperand::CreateReg(PPC::X2, false, true));
11756     }
11757 
11758     return emitPatchPoint(MI, BB);
11759   }
11760 
11761   if (MI.getOpcode() == PPC::EH_SjLj_SetJmp32 ||
11762       MI.getOpcode() == PPC::EH_SjLj_SetJmp64) {
11763     return emitEHSjLjSetJmp(MI, BB);
11764   } else if (MI.getOpcode() == PPC::EH_SjLj_LongJmp32 ||
11765              MI.getOpcode() == PPC::EH_SjLj_LongJmp64) {
11766     return emitEHSjLjLongJmp(MI, BB);
11767   }
11768 
11769   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
11770 
11771   // To "insert" these instructions we actually have to insert their
11772   // control-flow patterns.
11773   const BasicBlock *LLVM_BB = BB->getBasicBlock();
11774   MachineFunction::iterator It = ++BB->getIterator();
11775 
11776   MachineFunction *F = BB->getParent();
11777 
11778   if (MI.getOpcode() == PPC::SELECT_CC_I4 ||
11779       MI.getOpcode() == PPC::SELECT_CC_I8 || MI.getOpcode() == PPC::SELECT_I4 ||
11780       MI.getOpcode() == PPC::SELECT_I8) {
11781     SmallVector<MachineOperand, 2> Cond;
11782     if (MI.getOpcode() == PPC::SELECT_CC_I4 ||
11783         MI.getOpcode() == PPC::SELECT_CC_I8)
11784       Cond.push_back(MI.getOperand(4));
11785     else
11786       Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
11787     Cond.push_back(MI.getOperand(1));
11788 
11789     DebugLoc dl = MI.getDebugLoc();
11790     TII->insertSelect(*BB, MI, dl, MI.getOperand(0).getReg(), Cond,
11791                       MI.getOperand(2).getReg(), MI.getOperand(3).getReg());
11792   } else if (MI.getOpcode() == PPC::SELECT_CC_F4 ||
11793              MI.getOpcode() == PPC::SELECT_CC_F8 ||
11794              MI.getOpcode() == PPC::SELECT_CC_F16 ||
11795              MI.getOpcode() == PPC::SELECT_CC_VRRC ||
11796              MI.getOpcode() == PPC::SELECT_CC_VSFRC ||
11797              MI.getOpcode() == PPC::SELECT_CC_VSSRC ||
11798              MI.getOpcode() == PPC::SELECT_CC_VSRC ||
11799              MI.getOpcode() == PPC::SELECT_CC_SPE4 ||
11800              MI.getOpcode() == PPC::SELECT_CC_SPE ||
11801              MI.getOpcode() == PPC::SELECT_F4 ||
11802              MI.getOpcode() == PPC::SELECT_F8 ||
11803              MI.getOpcode() == PPC::SELECT_F16 ||
11804              MI.getOpcode() == PPC::SELECT_SPE ||
11805              MI.getOpcode() == PPC::SELECT_SPE4 ||
11806              MI.getOpcode() == PPC::SELECT_VRRC ||
11807              MI.getOpcode() == PPC::SELECT_VSFRC ||
11808              MI.getOpcode() == PPC::SELECT_VSSRC ||
11809              MI.getOpcode() == PPC::SELECT_VSRC) {
11810     // The incoming instruction knows the destination vreg to set, the
11811     // condition code register to branch on, the true/false values to
11812     // select between, and a branch opcode to use.
11813 
11814     //  thisMBB:
11815     //  ...
11816     //   TrueVal = ...
11817     //   cmpTY ccX, r1, r2
11818     //   bCC copy1MBB
11819     //   fallthrough --> copy0MBB
11820     MachineBasicBlock *thisMBB = BB;
11821     MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11822     MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
11823     DebugLoc dl = MI.getDebugLoc();
11824     F->insert(It, copy0MBB);
11825     F->insert(It, sinkMBB);
11826 
11827     // Transfer the remainder of BB and its successor edges to sinkMBB.
11828     sinkMBB->splice(sinkMBB->begin(), BB,
11829                     std::next(MachineBasicBlock::iterator(MI)), BB->end());
11830     sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11831 
11832     // Next, add the true and fallthrough blocks as its successors.
11833     BB->addSuccessor(copy0MBB);
11834     BB->addSuccessor(sinkMBB);
11835 
11836     if (MI.getOpcode() == PPC::SELECT_I4 || MI.getOpcode() == PPC::SELECT_I8 ||
11837         MI.getOpcode() == PPC::SELECT_F4 || MI.getOpcode() == PPC::SELECT_F8 ||
11838         MI.getOpcode() == PPC::SELECT_F16 ||
11839         MI.getOpcode() == PPC::SELECT_SPE4 ||
11840         MI.getOpcode() == PPC::SELECT_SPE ||
11841         MI.getOpcode() == PPC::SELECT_VRRC ||
11842         MI.getOpcode() == PPC::SELECT_VSFRC ||
11843         MI.getOpcode() == PPC::SELECT_VSSRC ||
11844         MI.getOpcode() == PPC::SELECT_VSRC) {
11845       BuildMI(BB, dl, TII->get(PPC::BC))
11846           .addReg(MI.getOperand(1).getReg())
11847           .addMBB(sinkMBB);
11848     } else {
11849       unsigned SelectPred = MI.getOperand(4).getImm();
11850       BuildMI(BB, dl, TII->get(PPC::BCC))
11851           .addImm(SelectPred)
11852           .addReg(MI.getOperand(1).getReg())
11853           .addMBB(sinkMBB);
11854     }
11855 
11856     //  copy0MBB:
11857     //   %FalseValue = ...
11858     //   # fallthrough to sinkMBB
11859     BB = copy0MBB;
11860 
11861     // Update machine-CFG edges
11862     BB->addSuccessor(sinkMBB);
11863 
11864     //  sinkMBB:
11865     //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11866     //  ...
11867     BB = sinkMBB;
11868     BuildMI(*BB, BB->begin(), dl, TII->get(PPC::PHI), MI.getOperand(0).getReg())
11869         .addReg(MI.getOperand(3).getReg())
11870         .addMBB(copy0MBB)
11871         .addReg(MI.getOperand(2).getReg())
11872         .addMBB(thisMBB);
11873   } else if (MI.getOpcode() == PPC::ReadTB) {
11874     // To read the 64-bit time-base register on a 32-bit target, we read the
11875     // two halves. Should the counter have wrapped while it was being read, we
11876     // need to try again.
11877     // ...
11878     // readLoop:
11879     // mfspr Rx,TBU # load from TBU
11880     // mfspr Ry,TB  # load from TB
11881     // mfspr Rz,TBU # load from TBU
11882     // cmpw crX,Rx,Rz # check if 'old'='new'
11883     // bne readLoop   # branch if they're not equal
11884     // ...
11885 
11886     MachineBasicBlock *readMBB = F->CreateMachineBasicBlock(LLVM_BB);
11887     MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
11888     DebugLoc dl = MI.getDebugLoc();
11889     F->insert(It, readMBB);
11890     F->insert(It, sinkMBB);
11891 
11892     // Transfer the remainder of BB and its successor edges to sinkMBB.
11893     sinkMBB->splice(sinkMBB->begin(), BB,
11894                     std::next(MachineBasicBlock::iterator(MI)), BB->end());
11895     sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11896 
11897     BB->addSuccessor(readMBB);
11898     BB = readMBB;
11899 
11900     MachineRegisterInfo &RegInfo = F->getRegInfo();
11901     Register ReadAgainReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass);
11902     Register LoReg = MI.getOperand(0).getReg();
11903     Register HiReg = MI.getOperand(1).getReg();
11904 
11905     BuildMI(BB, dl, TII->get(PPC::MFSPR), HiReg).addImm(269);
11906     BuildMI(BB, dl, TII->get(PPC::MFSPR), LoReg).addImm(268);
11907     BuildMI(BB, dl, TII->get(PPC::MFSPR), ReadAgainReg).addImm(269);
11908 
11909     Register CmpReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass);
11910 
11911     BuildMI(BB, dl, TII->get(PPC::CMPW), CmpReg)
11912         .addReg(HiReg)
11913         .addReg(ReadAgainReg);
11914     BuildMI(BB, dl, TII->get(PPC::BCC))
11915         .addImm(PPC::PRED_NE)
11916         .addReg(CmpReg)
11917         .addMBB(readMBB);
11918 
11919     BB->addSuccessor(readMBB);
11920     BB->addSuccessor(sinkMBB);
11921   } else if (MI.getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
11922     BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
11923   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
11924     BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
11925   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
11926     BB = EmitAtomicBinary(MI, BB, 4, PPC::ADD4);
11927   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
11928     BB = EmitAtomicBinary(MI, BB, 8, PPC::ADD8);
11929 
11930   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
11931     BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
11932   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
11933     BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
11934   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
11935     BB = EmitAtomicBinary(MI, BB, 4, PPC::AND);
11936   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
11937     BB = EmitAtomicBinary(MI, BB, 8, PPC::AND8);
11938 
11939   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
11940     BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
11941   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
11942     BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
11943   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
11944     BB = EmitAtomicBinary(MI, BB, 4, PPC::OR);
11945   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
11946     BB = EmitAtomicBinary(MI, BB, 8, PPC::OR8);
11947 
11948   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
11949     BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
11950   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
11951     BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
11952   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
11953     BB = EmitAtomicBinary(MI, BB, 4, PPC::XOR);
11954   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
11955     BB = EmitAtomicBinary(MI, BB, 8, PPC::XOR8);
11956 
11957   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
11958     BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::NAND);
11959   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
11960     BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::NAND);
11961   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
11962     BB = EmitAtomicBinary(MI, BB, 4, PPC::NAND);
11963   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
11964     BB = EmitAtomicBinary(MI, BB, 8, PPC::NAND8);
11965 
11966   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
11967     BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
11968   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
11969     BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
11970   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
11971     BB = EmitAtomicBinary(MI, BB, 4, PPC::SUBF);
11972   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
11973     BB = EmitAtomicBinary(MI, BB, 8, PPC::SUBF8);
11974 
11975   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MIN_I8)
11976     BB = EmitPartwordAtomicBinary(MI, BB, true, 0, PPC::CMPW, PPC::PRED_GE);
11977   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MIN_I16)
11978     BB = EmitPartwordAtomicBinary(MI, BB, false, 0, PPC::CMPW, PPC::PRED_GE);
11979   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MIN_I32)
11980     BB = EmitAtomicBinary(MI, BB, 4, 0, PPC::CMPW, PPC::PRED_GE);
11981   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MIN_I64)
11982     BB = EmitAtomicBinary(MI, BB, 8, 0, PPC::CMPD, PPC::PRED_GE);
11983 
11984   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MAX_I8)
11985     BB = EmitPartwordAtomicBinary(MI, BB, true, 0, PPC::CMPW, PPC::PRED_LE);
11986   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MAX_I16)
11987     BB = EmitPartwordAtomicBinary(MI, BB, false, 0, PPC::CMPW, PPC::PRED_LE);
11988   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MAX_I32)
11989     BB = EmitAtomicBinary(MI, BB, 4, 0, PPC::CMPW, PPC::PRED_LE);
11990   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MAX_I64)
11991     BB = EmitAtomicBinary(MI, BB, 8, 0, PPC::CMPD, PPC::PRED_LE);
11992 
11993   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMIN_I8)
11994     BB = EmitPartwordAtomicBinary(MI, BB, true, 0, PPC::CMPLW, PPC::PRED_GE);
11995   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMIN_I16)
11996     BB = EmitPartwordAtomicBinary(MI, BB, false, 0, PPC::CMPLW, PPC::PRED_GE);
11997   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMIN_I32)
11998     BB = EmitAtomicBinary(MI, BB, 4, 0, PPC::CMPLW, PPC::PRED_GE);
11999   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMIN_I64)
12000     BB = EmitAtomicBinary(MI, BB, 8, 0, PPC::CMPLD, PPC::PRED_GE);
12001 
12002   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMAX_I8)
12003     BB = EmitPartwordAtomicBinary(MI, BB, true, 0, PPC::CMPLW, PPC::PRED_LE);
12004   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMAX_I16)
12005     BB = EmitPartwordAtomicBinary(MI, BB, false, 0, PPC::CMPLW, PPC::PRED_LE);
12006   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMAX_I32)
12007     BB = EmitAtomicBinary(MI, BB, 4, 0, PPC::CMPLW, PPC::PRED_LE);
12008   else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMAX_I64)
12009     BB = EmitAtomicBinary(MI, BB, 8, 0, PPC::CMPLD, PPC::PRED_LE);
12010 
12011   else if (MI.getOpcode() == PPC::ATOMIC_SWAP_I8)
12012     BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
12013   else if (MI.getOpcode() == PPC::ATOMIC_SWAP_I16)
12014     BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
12015   else if (MI.getOpcode() == PPC::ATOMIC_SWAP_I32)
12016     BB = EmitAtomicBinary(MI, BB, 4, 0);
12017   else if (MI.getOpcode() == PPC::ATOMIC_SWAP_I64)
12018     BB = EmitAtomicBinary(MI, BB, 8, 0);
12019   else if (MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
12020            MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I64 ||
12021            (Subtarget.hasPartwordAtomics() &&
12022             MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I8) ||
12023            (Subtarget.hasPartwordAtomics() &&
12024             MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I16)) {
12025     bool is64bit = MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
12026 
12027     auto LoadMnemonic = PPC::LDARX;
12028     auto StoreMnemonic = PPC::STDCX;
12029     switch (MI.getOpcode()) {
12030     default:
12031       llvm_unreachable("Compare and swap of unknown size");
12032     case PPC::ATOMIC_CMP_SWAP_I8:
12033       LoadMnemonic = PPC::LBARX;
12034       StoreMnemonic = PPC::STBCX;
12035       assert(Subtarget.hasPartwordAtomics() && "No support partword atomics.");
12036       break;
12037     case PPC::ATOMIC_CMP_SWAP_I16:
12038       LoadMnemonic = PPC::LHARX;
12039       StoreMnemonic = PPC::STHCX;
12040       assert(Subtarget.hasPartwordAtomics() && "No support partword atomics.");
12041       break;
12042     case PPC::ATOMIC_CMP_SWAP_I32:
12043       LoadMnemonic = PPC::LWARX;
12044       StoreMnemonic = PPC::STWCX;
12045       break;
12046     case PPC::ATOMIC_CMP_SWAP_I64:
12047       LoadMnemonic = PPC::LDARX;
12048       StoreMnemonic = PPC::STDCX;
12049       break;
12050     }
12051     Register dest = MI.getOperand(0).getReg();
12052     Register ptrA = MI.getOperand(1).getReg();
12053     Register ptrB = MI.getOperand(2).getReg();
12054     Register oldval = MI.getOperand(3).getReg();
12055     Register newval = MI.getOperand(4).getReg();
12056     DebugLoc dl = MI.getDebugLoc();
12057 
12058     MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
12059     MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
12060     MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
12061     MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
12062     F->insert(It, loop1MBB);
12063     F->insert(It, loop2MBB);
12064     F->insert(It, midMBB);
12065     F->insert(It, exitMBB);
12066     exitMBB->splice(exitMBB->begin(), BB,
12067                     std::next(MachineBasicBlock::iterator(MI)), BB->end());
12068     exitMBB->transferSuccessorsAndUpdatePHIs(BB);
12069 
12070     //  thisMBB:
12071     //   ...
12072     //   fallthrough --> loopMBB
12073     BB->addSuccessor(loop1MBB);
12074 
12075     // loop1MBB:
12076     //   l[bhwd]arx dest, ptr
12077     //   cmp[wd] dest, oldval
12078     //   bne- midMBB
12079     // loop2MBB:
12080     //   st[bhwd]cx. newval, ptr
12081     //   bne- loopMBB
12082     //   b exitBB
12083     // midMBB:
12084     //   st[bhwd]cx. dest, ptr
12085     // exitBB:
12086     BB = loop1MBB;
12087     BuildMI(BB, dl, TII->get(LoadMnemonic), dest).addReg(ptrA).addReg(ptrB);
12088     BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
12089         .addReg(oldval)
12090         .addReg(dest);
12091     BuildMI(BB, dl, TII->get(PPC::BCC))
12092         .addImm(PPC::PRED_NE)
12093         .addReg(PPC::CR0)
12094         .addMBB(midMBB);
12095     BB->addSuccessor(loop2MBB);
12096     BB->addSuccessor(midMBB);
12097 
12098     BB = loop2MBB;
12099     BuildMI(BB, dl, TII->get(StoreMnemonic))
12100         .addReg(newval)
12101         .addReg(ptrA)
12102         .addReg(ptrB);
12103     BuildMI(BB, dl, TII->get(PPC::BCC))
12104         .addImm(PPC::PRED_NE)
12105         .addReg(PPC::CR0)
12106         .addMBB(loop1MBB);
12107     BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
12108     BB->addSuccessor(loop1MBB);
12109     BB->addSuccessor(exitMBB);
12110 
12111     BB = midMBB;
12112     BuildMI(BB, dl, TII->get(StoreMnemonic))
12113         .addReg(dest)
12114         .addReg(ptrA)
12115         .addReg(ptrB);
12116     BB->addSuccessor(exitMBB);
12117 
12118     //  exitMBB:
12119     //   ...
12120     BB = exitMBB;
12121   } else if (MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
12122              MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
12123     // We must use 64-bit registers for addresses when targeting 64-bit,
12124     // since we're actually doing arithmetic on them.  Other registers
12125     // can be 32-bit.
12126     bool is64bit = Subtarget.isPPC64();
12127     bool isLittleEndian = Subtarget.isLittleEndian();
12128     bool is8bit = MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
12129 
12130     Register dest = MI.getOperand(0).getReg();
12131     Register ptrA = MI.getOperand(1).getReg();
12132     Register ptrB = MI.getOperand(2).getReg();
12133     Register oldval = MI.getOperand(3).getReg();
12134     Register newval = MI.getOperand(4).getReg();
12135     DebugLoc dl = MI.getDebugLoc();
12136 
12137     MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
12138     MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
12139     MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
12140     MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
12141     F->insert(It, loop1MBB);
12142     F->insert(It, loop2MBB);
12143     F->insert(It, midMBB);
12144     F->insert(It, exitMBB);
12145     exitMBB->splice(exitMBB->begin(), BB,
12146                     std::next(MachineBasicBlock::iterator(MI)), BB->end());
12147     exitMBB->transferSuccessorsAndUpdatePHIs(BB);
12148 
12149     MachineRegisterInfo &RegInfo = F->getRegInfo();
12150     const TargetRegisterClass *RC =
12151         is64bit ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
12152     const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
12153 
12154     Register PtrReg = RegInfo.createVirtualRegister(RC);
12155     Register Shift1Reg = RegInfo.createVirtualRegister(GPRC);
12156     Register ShiftReg =
12157         isLittleEndian ? Shift1Reg : RegInfo.createVirtualRegister(GPRC);
12158     Register NewVal2Reg = RegInfo.createVirtualRegister(GPRC);
12159     Register NewVal3Reg = RegInfo.createVirtualRegister(GPRC);
12160     Register OldVal2Reg = RegInfo.createVirtualRegister(GPRC);
12161     Register OldVal3Reg = RegInfo.createVirtualRegister(GPRC);
12162     Register MaskReg = RegInfo.createVirtualRegister(GPRC);
12163     Register Mask2Reg = RegInfo.createVirtualRegister(GPRC);
12164     Register Mask3Reg = RegInfo.createVirtualRegister(GPRC);
12165     Register Tmp2Reg = RegInfo.createVirtualRegister(GPRC);
12166     Register Tmp4Reg = RegInfo.createVirtualRegister(GPRC);
12167     Register TmpDestReg = RegInfo.createVirtualRegister(GPRC);
12168     Register Ptr1Reg;
12169     Register TmpReg = RegInfo.createVirtualRegister(GPRC);
12170     Register ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
12171     //  thisMBB:
12172     //   ...
12173     //   fallthrough --> loopMBB
12174     BB->addSuccessor(loop1MBB);
12175 
12176     // The 4-byte load must be aligned, while a char or short may be
12177     // anywhere in the word.  Hence all this nasty bookkeeping code.
12178     //   add ptr1, ptrA, ptrB [copy if ptrA==0]
12179     //   rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
12180     //   xori shift, shift1, 24 [16]
12181     //   rlwinm ptr, ptr1, 0, 0, 29
12182     //   slw newval2, newval, shift
12183     //   slw oldval2, oldval,shift
12184     //   li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
12185     //   slw mask, mask2, shift
12186     //   and newval3, newval2, mask
12187     //   and oldval3, oldval2, mask
12188     // loop1MBB:
12189     //   lwarx tmpDest, ptr
12190     //   and tmp, tmpDest, mask
12191     //   cmpw tmp, oldval3
12192     //   bne- midMBB
12193     // loop2MBB:
12194     //   andc tmp2, tmpDest, mask
12195     //   or tmp4, tmp2, newval3
12196     //   stwcx. tmp4, ptr
12197     //   bne- loop1MBB
12198     //   b exitBB
12199     // midMBB:
12200     //   stwcx. tmpDest, ptr
12201     // exitBB:
12202     //   srw dest, tmpDest, shift
12203     if (ptrA != ZeroReg) {
12204       Ptr1Reg = RegInfo.createVirtualRegister(RC);
12205       BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
12206           .addReg(ptrA)
12207           .addReg(ptrB);
12208     } else {
12209       Ptr1Reg = ptrB;
12210     }
12211 
12212     // We need use 32-bit subregister to avoid mismatch register class in 64-bit
12213     // mode.
12214     BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg)
12215         .addReg(Ptr1Reg, 0, is64bit ? PPC::sub_32 : 0)
12216         .addImm(3)
12217         .addImm(27)
12218         .addImm(is8bit ? 28 : 27);
12219     if (!isLittleEndian)
12220       BuildMI(BB, dl, TII->get(PPC::XORI), ShiftReg)
12221           .addReg(Shift1Reg)
12222           .addImm(is8bit ? 24 : 16);
12223     if (is64bit)
12224       BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
12225           .addReg(Ptr1Reg)
12226           .addImm(0)
12227           .addImm(61);
12228     else
12229       BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
12230           .addReg(Ptr1Reg)
12231           .addImm(0)
12232           .addImm(0)
12233           .addImm(29);
12234     BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
12235         .addReg(newval)
12236         .addReg(ShiftReg);
12237     BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
12238         .addReg(oldval)
12239         .addReg(ShiftReg);
12240     if (is8bit)
12241       BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
12242     else {
12243       BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
12244       BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
12245           .addReg(Mask3Reg)
12246           .addImm(65535);
12247     }
12248     BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
12249         .addReg(Mask2Reg)
12250         .addReg(ShiftReg);
12251     BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
12252         .addReg(NewVal2Reg)
12253         .addReg(MaskReg);
12254     BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
12255         .addReg(OldVal2Reg)
12256         .addReg(MaskReg);
12257 
12258     BB = loop1MBB;
12259     BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
12260         .addReg(ZeroReg)
12261         .addReg(PtrReg);
12262     BuildMI(BB, dl, TII->get(PPC::AND), TmpReg)
12263         .addReg(TmpDestReg)
12264         .addReg(MaskReg);
12265     BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
12266         .addReg(TmpReg)
12267         .addReg(OldVal3Reg);
12268     BuildMI(BB, dl, TII->get(PPC::BCC))
12269         .addImm(PPC::PRED_NE)
12270         .addReg(PPC::CR0)
12271         .addMBB(midMBB);
12272     BB->addSuccessor(loop2MBB);
12273     BB->addSuccessor(midMBB);
12274 
12275     BB = loop2MBB;
12276     BuildMI(BB, dl, TII->get(PPC::ANDC), Tmp2Reg)
12277         .addReg(TmpDestReg)
12278         .addReg(MaskReg);
12279     BuildMI(BB, dl, TII->get(PPC::OR), Tmp4Reg)
12280         .addReg(Tmp2Reg)
12281         .addReg(NewVal3Reg);
12282     BuildMI(BB, dl, TII->get(PPC::STWCX))
12283         .addReg(Tmp4Reg)
12284         .addReg(ZeroReg)
12285         .addReg(PtrReg);
12286     BuildMI(BB, dl, TII->get(PPC::BCC))
12287         .addImm(PPC::PRED_NE)
12288         .addReg(PPC::CR0)
12289         .addMBB(loop1MBB);
12290     BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
12291     BB->addSuccessor(loop1MBB);
12292     BB->addSuccessor(exitMBB);
12293 
12294     BB = midMBB;
12295     BuildMI(BB, dl, TII->get(PPC::STWCX))
12296         .addReg(TmpDestReg)
12297         .addReg(ZeroReg)
12298         .addReg(PtrReg);
12299     BB->addSuccessor(exitMBB);
12300 
12301     //  exitMBB:
12302     //   ...
12303     BB = exitMBB;
12304     BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest)
12305         .addReg(TmpReg)
12306         .addReg(ShiftReg);
12307   } else if (MI.getOpcode() == PPC::FADDrtz) {
12308     // This pseudo performs an FADD with rounding mode temporarily forced
12309     // to round-to-zero.  We emit this via custom inserter since the FPSCR
12310     // is not modeled at the SelectionDAG level.
12311     Register Dest = MI.getOperand(0).getReg();
12312     Register Src1 = MI.getOperand(1).getReg();
12313     Register Src2 = MI.getOperand(2).getReg();
12314     DebugLoc dl = MI.getDebugLoc();
12315 
12316     MachineRegisterInfo &RegInfo = F->getRegInfo();
12317     Register MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
12318 
12319     // Save FPSCR value.
12320     BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
12321 
12322     // Set rounding mode to round-to-zero.
12323     BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1))
12324         .addImm(31)
12325         .addReg(PPC::RM, RegState::ImplicitDefine);
12326 
12327     BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0))
12328         .addImm(30)
12329         .addReg(PPC::RM, RegState::ImplicitDefine);
12330 
12331     // Perform addition.
12332     auto MIB = BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest)
12333                    .addReg(Src1)
12334                    .addReg(Src2);
12335     if (MI.getFlag(MachineInstr::NoFPExcept))
12336       MIB.setMIFlag(MachineInstr::NoFPExcept);
12337 
12338     // Restore FPSCR value.
12339     BuildMI(*BB, MI, dl, TII->get(PPC::MTFSFb)).addImm(1).addReg(MFFSReg);
12340   } else if (MI.getOpcode() == PPC::ANDI_rec_1_EQ_BIT ||
12341              MI.getOpcode() == PPC::ANDI_rec_1_GT_BIT ||
12342              MI.getOpcode() == PPC::ANDI_rec_1_EQ_BIT8 ||
12343              MI.getOpcode() == PPC::ANDI_rec_1_GT_BIT8) {
12344     unsigned Opcode = (MI.getOpcode() == PPC::ANDI_rec_1_EQ_BIT8 ||
12345                        MI.getOpcode() == PPC::ANDI_rec_1_GT_BIT8)
12346                           ? PPC::ANDI8_rec
12347                           : PPC::ANDI_rec;
12348     bool IsEQ = (MI.getOpcode() == PPC::ANDI_rec_1_EQ_BIT ||
12349                  MI.getOpcode() == PPC::ANDI_rec_1_EQ_BIT8);
12350 
12351     MachineRegisterInfo &RegInfo = F->getRegInfo();
12352     Register Dest = RegInfo.createVirtualRegister(
12353         Opcode == PPC::ANDI_rec ? &PPC::GPRCRegClass : &PPC::G8RCRegClass);
12354 
12355     DebugLoc Dl = MI.getDebugLoc();
12356     BuildMI(*BB, MI, Dl, TII->get(Opcode), Dest)
12357         .addReg(MI.getOperand(1).getReg())
12358         .addImm(1);
12359     BuildMI(*BB, MI, Dl, TII->get(TargetOpcode::COPY),
12360             MI.getOperand(0).getReg())
12361         .addReg(IsEQ ? PPC::CR0EQ : PPC::CR0GT);
12362   } else if (MI.getOpcode() == PPC::TCHECK_RET) {
12363     DebugLoc Dl = MI.getDebugLoc();
12364     MachineRegisterInfo &RegInfo = F->getRegInfo();
12365     Register CRReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass);
12366     BuildMI(*BB, MI, Dl, TII->get(PPC::TCHECK), CRReg);
12367     BuildMI(*BB, MI, Dl, TII->get(TargetOpcode::COPY),
12368             MI.getOperand(0).getReg())
12369         .addReg(CRReg);
12370   } else if (MI.getOpcode() == PPC::TBEGIN_RET) {
12371     DebugLoc Dl = MI.getDebugLoc();
12372     unsigned Imm = MI.getOperand(1).getImm();
12373     BuildMI(*BB, MI, Dl, TII->get(PPC::TBEGIN)).addImm(Imm);
12374     BuildMI(*BB, MI, Dl, TII->get(TargetOpcode::COPY),
12375             MI.getOperand(0).getReg())
12376         .addReg(PPC::CR0EQ);
12377   } else if (MI.getOpcode() == PPC::SETRNDi) {
12378     DebugLoc dl = MI.getDebugLoc();
12379     Register OldFPSCRReg = MI.getOperand(0).getReg();
12380 
12381     // Save FPSCR value.
12382     BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), OldFPSCRReg);
12383 
12384     // The floating point rounding mode is in the bits 62:63 of FPCSR, and has
12385     // the following settings:
12386     //   00 Round to nearest
12387     //   01 Round to 0
12388     //   10 Round to +inf
12389     //   11 Round to -inf
12390 
12391     // When the operand is immediate, using the two least significant bits of
12392     // the immediate to set the bits 62:63 of FPSCR.
12393     unsigned Mode = MI.getOperand(1).getImm();
12394     BuildMI(*BB, MI, dl, TII->get((Mode & 1) ? PPC::MTFSB1 : PPC::MTFSB0))
12395         .addImm(31)
12396         .addReg(PPC::RM, RegState::ImplicitDefine);
12397 
12398     BuildMI(*BB, MI, dl, TII->get((Mode & 2) ? PPC::MTFSB1 : PPC::MTFSB0))
12399         .addImm(30)
12400         .addReg(PPC::RM, RegState::ImplicitDefine);
12401   } else if (MI.getOpcode() == PPC::SETRND) {
12402     DebugLoc dl = MI.getDebugLoc();
12403 
12404     // Copy register from F8RCRegClass::SrcReg to G8RCRegClass::DestReg
12405     // or copy register from G8RCRegClass::SrcReg to F8RCRegClass::DestReg.
12406     // If the target doesn't have DirectMove, we should use stack to do the
12407     // conversion, because the target doesn't have the instructions like mtvsrd
12408     // or mfvsrd to do this conversion directly.
12409     auto copyRegFromG8RCOrF8RC = [&] (unsigned DestReg, unsigned SrcReg) {
12410       if (Subtarget.hasDirectMove()) {
12411         BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), DestReg)
12412           .addReg(SrcReg);
12413       } else {
12414         // Use stack to do the register copy.
12415         unsigned StoreOp = PPC::STD, LoadOp = PPC::LFD;
12416         MachineRegisterInfo &RegInfo = F->getRegInfo();
12417         const TargetRegisterClass *RC = RegInfo.getRegClass(SrcReg);
12418         if (RC == &PPC::F8RCRegClass) {
12419           // Copy register from F8RCRegClass to G8RCRegclass.
12420           assert((RegInfo.getRegClass(DestReg) == &PPC::G8RCRegClass) &&
12421                  "Unsupported RegClass.");
12422 
12423           StoreOp = PPC::STFD;
12424           LoadOp = PPC::LD;
12425         } else {
12426           // Copy register from G8RCRegClass to F8RCRegclass.
12427           assert((RegInfo.getRegClass(SrcReg) == &PPC::G8RCRegClass) &&
12428                  (RegInfo.getRegClass(DestReg) == &PPC::F8RCRegClass) &&
12429                  "Unsupported RegClass.");
12430         }
12431 
12432         MachineFrameInfo &MFI = F->getFrameInfo();
12433         int FrameIdx = MFI.CreateStackObject(8, Align(8), false);
12434 
12435         MachineMemOperand *MMOStore = F->getMachineMemOperand(
12436             MachinePointerInfo::getFixedStack(*F, FrameIdx, 0),
12437             MachineMemOperand::MOStore, MFI.getObjectSize(FrameIdx),
12438             MFI.getObjectAlign(FrameIdx));
12439 
12440         // Store the SrcReg into the stack.
12441         BuildMI(*BB, MI, dl, TII->get(StoreOp))
12442           .addReg(SrcReg)
12443           .addImm(0)
12444           .addFrameIndex(FrameIdx)
12445           .addMemOperand(MMOStore);
12446 
12447         MachineMemOperand *MMOLoad = F->getMachineMemOperand(
12448             MachinePointerInfo::getFixedStack(*F, FrameIdx, 0),
12449             MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIdx),
12450             MFI.getObjectAlign(FrameIdx));
12451 
12452         // Load from the stack where SrcReg is stored, and save to DestReg,
12453         // so we have done the RegClass conversion from RegClass::SrcReg to
12454         // RegClass::DestReg.
12455         BuildMI(*BB, MI, dl, TII->get(LoadOp), DestReg)
12456           .addImm(0)
12457           .addFrameIndex(FrameIdx)
12458           .addMemOperand(MMOLoad);
12459       }
12460     };
12461 
12462     Register OldFPSCRReg = MI.getOperand(0).getReg();
12463 
12464     // Save FPSCR value.
12465     BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), OldFPSCRReg);
12466 
12467     // When the operand is gprc register, use two least significant bits of the
12468     // register and mtfsf instruction to set the bits 62:63 of FPSCR.
12469     //
12470     // copy OldFPSCRTmpReg, OldFPSCRReg
12471     // (INSERT_SUBREG ExtSrcReg, (IMPLICIT_DEF ImDefReg), SrcOp, 1)
12472     // rldimi NewFPSCRTmpReg, ExtSrcReg, OldFPSCRReg, 0, 62
12473     // copy NewFPSCRReg, NewFPSCRTmpReg
12474     // mtfsf 255, NewFPSCRReg
12475     MachineOperand SrcOp = MI.getOperand(1);
12476     MachineRegisterInfo &RegInfo = F->getRegInfo();
12477     Register OldFPSCRTmpReg = RegInfo.createVirtualRegister(&PPC::G8RCRegClass);
12478 
12479     copyRegFromG8RCOrF8RC(OldFPSCRTmpReg, OldFPSCRReg);
12480 
12481     Register ImDefReg = RegInfo.createVirtualRegister(&PPC::G8RCRegClass);
12482     Register ExtSrcReg = RegInfo.createVirtualRegister(&PPC::G8RCRegClass);
12483 
12484     // The first operand of INSERT_SUBREG should be a register which has
12485     // subregisters, we only care about its RegClass, so we should use an
12486     // IMPLICIT_DEF register.
12487     BuildMI(*BB, MI, dl, TII->get(TargetOpcode::IMPLICIT_DEF), ImDefReg);
12488     BuildMI(*BB, MI, dl, TII->get(PPC::INSERT_SUBREG), ExtSrcReg)
12489       .addReg(ImDefReg)
12490       .add(SrcOp)
12491       .addImm(1);
12492 
12493     Register NewFPSCRTmpReg = RegInfo.createVirtualRegister(&PPC::G8RCRegClass);
12494     BuildMI(*BB, MI, dl, TII->get(PPC::RLDIMI), NewFPSCRTmpReg)
12495       .addReg(OldFPSCRTmpReg)
12496       .addReg(ExtSrcReg)
12497       .addImm(0)
12498       .addImm(62);
12499 
12500     Register NewFPSCRReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
12501     copyRegFromG8RCOrF8RC(NewFPSCRReg, NewFPSCRTmpReg);
12502 
12503     // The mask 255 means that put the 32:63 bits of NewFPSCRReg to the 32:63
12504     // bits of FPSCR.
12505     BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF))
12506       .addImm(255)
12507       .addReg(NewFPSCRReg)
12508       .addImm(0)
12509       .addImm(0);
12510   } else if (MI.getOpcode() == PPC::SETFLM) {
12511     DebugLoc Dl = MI.getDebugLoc();
12512 
12513     // Result of setflm is previous FPSCR content, so we need to save it first.
12514     Register OldFPSCRReg = MI.getOperand(0).getReg();
12515     BuildMI(*BB, MI, Dl, TII->get(PPC::MFFS), OldFPSCRReg);
12516 
12517     // Put bits in 32:63 to FPSCR.
12518     Register NewFPSCRReg = MI.getOperand(1).getReg();
12519     BuildMI(*BB, MI, Dl, TII->get(PPC::MTFSF))
12520         .addImm(255)
12521         .addReg(NewFPSCRReg)
12522         .addImm(0)
12523         .addImm(0);
12524   } else if (MI.getOpcode() == PPC::PROBED_ALLOCA_32 ||
12525              MI.getOpcode() == PPC::PROBED_ALLOCA_64) {
12526     return emitProbedAlloca(MI, BB);
12527   } else {
12528     llvm_unreachable("Unexpected instr type to insert");
12529   }
12530 
12531   MI.eraseFromParent(); // The pseudo instruction is gone now.
12532   return BB;
12533 }
12534 
12535 //===----------------------------------------------------------------------===//
12536 // Target Optimization Hooks
12537 //===----------------------------------------------------------------------===//
12538 
12539 static int getEstimateRefinementSteps(EVT VT, const PPCSubtarget &Subtarget) {
12540   // For the estimates, convergence is quadratic, so we essentially double the
12541   // number of digits correct after every iteration. For both FRE and FRSQRTE,
12542   // the minimum architected relative accuracy is 2^-5. When hasRecipPrec(),
12543   // this is 2^-14. IEEE float has 23 digits and double has 52 digits.
12544   int RefinementSteps = Subtarget.hasRecipPrec() ? 1 : 3;
12545   if (VT.getScalarType() == MVT::f64)
12546     RefinementSteps++;
12547   return RefinementSteps;
12548 }
12549 
12550 SDValue PPCTargetLowering::getSqrtInputTest(SDValue Op, SelectionDAG &DAG,
12551                                             const DenormalMode &Mode) const {
12552   // We only have VSX Vector Test for software Square Root.
12553   EVT VT = Op.getValueType();
12554   if (!isTypeLegal(MVT::i1) ||
12555       (VT != MVT::f64 &&
12556        ((VT != MVT::v2f64 && VT != MVT::v4f32) || !Subtarget.hasVSX())))
12557     return TargetLowering::getSqrtInputTest(Op, DAG, Mode);
12558 
12559   SDLoc DL(Op);
12560   // The output register of FTSQRT is CR field.
12561   SDValue FTSQRT = DAG.getNode(PPCISD::FTSQRT, DL, MVT::i32, Op);
12562   // ftsqrt BF,FRB
12563   // Let e_b be the unbiased exponent of the double-precision
12564   // floating-point operand in register FRB.
12565   // fe_flag is set to 1 if either of the following conditions occurs.
12566   //   - The double-precision floating-point operand in register FRB is a zero,
12567   //     a NaN, or an infinity, or a negative value.
12568   //   - e_b is less than or equal to -970.
12569   // Otherwise fe_flag is set to 0.
12570   // Both VSX and non-VSX versions would set EQ bit in the CR if the number is
12571   // not eligible for iteration. (zero/negative/infinity/nan or unbiased
12572   // exponent is less than -970)
12573   SDValue SRIdxVal = DAG.getTargetConstant(PPC::sub_eq, DL, MVT::i32);
12574   return SDValue(DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL, MVT::i1,
12575                                     FTSQRT, SRIdxVal),
12576                  0);
12577 }
12578 
12579 SDValue
12580 PPCTargetLowering::getSqrtResultForDenormInput(SDValue Op,
12581                                                SelectionDAG &DAG) const {
12582   // We only have VSX Vector Square Root.
12583   EVT VT = Op.getValueType();
12584   if (VT != MVT::f64 &&
12585       ((VT != MVT::v2f64 && VT != MVT::v4f32) || !Subtarget.hasVSX()))
12586     return TargetLowering::getSqrtResultForDenormInput(Op, DAG);
12587 
12588   return DAG.getNode(PPCISD::FSQRT, SDLoc(Op), VT, Op);
12589 }
12590 
12591 SDValue PPCTargetLowering::getSqrtEstimate(SDValue Operand, SelectionDAG &DAG,
12592                                            int Enabled, int &RefinementSteps,
12593                                            bool &UseOneConstNR,
12594                                            bool Reciprocal) const {
12595   EVT VT = Operand.getValueType();
12596   if ((VT == MVT::f32 && Subtarget.hasFRSQRTES()) ||
12597       (VT == MVT::f64 && Subtarget.hasFRSQRTE()) ||
12598       (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
12599       (VT == MVT::v2f64 && Subtarget.hasVSX())) {
12600     if (RefinementSteps == ReciprocalEstimate::Unspecified)
12601       RefinementSteps = getEstimateRefinementSteps(VT, Subtarget);
12602 
12603     // The Newton-Raphson computation with a single constant does not provide
12604     // enough accuracy on some CPUs.
12605     UseOneConstNR = !Subtarget.needsTwoConstNR();
12606     return DAG.getNode(PPCISD::FRSQRTE, SDLoc(Operand), VT, Operand);
12607   }
12608   return SDValue();
12609 }
12610 
12611 SDValue PPCTargetLowering::getRecipEstimate(SDValue Operand, SelectionDAG &DAG,
12612                                             int Enabled,
12613                                             int &RefinementSteps) const {
12614   EVT VT = Operand.getValueType();
12615   if ((VT == MVT::f32 && Subtarget.hasFRES()) ||
12616       (VT == MVT::f64 && Subtarget.hasFRE()) ||
12617       (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
12618       (VT == MVT::v2f64 && Subtarget.hasVSX())) {
12619     if (RefinementSteps == ReciprocalEstimate::Unspecified)
12620       RefinementSteps = getEstimateRefinementSteps(VT, Subtarget);
12621     return DAG.getNode(PPCISD::FRE, SDLoc(Operand), VT, Operand);
12622   }
12623   return SDValue();
12624 }
12625 
12626 unsigned PPCTargetLowering::combineRepeatedFPDivisors() const {
12627   // Note: This functionality is used only when unsafe-fp-math is enabled, and
12628   // on cores with reciprocal estimates (which are used when unsafe-fp-math is
12629   // enabled for division), this functionality is redundant with the default
12630   // combiner logic (once the division -> reciprocal/multiply transformation
12631   // has taken place). As a result, this matters more for older cores than for
12632   // newer ones.
12633 
12634   // Combine multiple FDIVs with the same divisor into multiple FMULs by the
12635   // reciprocal if there are two or more FDIVs (for embedded cores with only
12636   // one FP pipeline) for three or more FDIVs (for generic OOO cores).
12637   switch (Subtarget.getCPUDirective()) {
12638   default:
12639     return 3;
12640   case PPC::DIR_440:
12641   case PPC::DIR_A2:
12642   case PPC::DIR_E500:
12643   case PPC::DIR_E500mc:
12644   case PPC::DIR_E5500:
12645     return 2;
12646   }
12647 }
12648 
12649 // isConsecutiveLSLoc needs to work even if all adds have not yet been
12650 // collapsed, and so we need to look through chains of them.
12651 static void getBaseWithConstantOffset(SDValue Loc, SDValue &Base,
12652                                      int64_t& Offset, SelectionDAG &DAG) {
12653   if (DAG.isBaseWithConstantOffset(Loc)) {
12654     Base = Loc.getOperand(0);
12655     Offset += cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue();
12656 
12657     // The base might itself be a base plus an offset, and if so, accumulate
12658     // that as well.
12659     getBaseWithConstantOffset(Loc.getOperand(0), Base, Offset, DAG);
12660   }
12661 }
12662 
12663 static bool isConsecutiveLSLoc(SDValue Loc, EVT VT, LSBaseSDNode *Base,
12664                             unsigned Bytes, int Dist,
12665                             SelectionDAG &DAG) {
12666   if (VT.getSizeInBits() / 8 != Bytes)
12667     return false;
12668 
12669   SDValue BaseLoc = Base->getBasePtr();
12670   if (Loc.getOpcode() == ISD::FrameIndex) {
12671     if (BaseLoc.getOpcode() != ISD::FrameIndex)
12672       return false;
12673     const MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
12674     int FI  = cast<FrameIndexSDNode>(Loc)->getIndex();
12675     int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
12676     int FS  = MFI.getObjectSize(FI);
12677     int BFS = MFI.getObjectSize(BFI);
12678     if (FS != BFS || FS != (int)Bytes) return false;
12679     return MFI.getObjectOffset(FI) == (MFI.getObjectOffset(BFI) + Dist*Bytes);
12680   }
12681 
12682   SDValue Base1 = Loc, Base2 = BaseLoc;
12683   int64_t Offset1 = 0, Offset2 = 0;
12684   getBaseWithConstantOffset(Loc, Base1, Offset1, DAG);
12685   getBaseWithConstantOffset(BaseLoc, Base2, Offset2, DAG);
12686   if (Base1 == Base2 && Offset1 == (Offset2 + Dist * Bytes))
12687     return true;
12688 
12689   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12690   const GlobalValue *GV1 = nullptr;
12691   const GlobalValue *GV2 = nullptr;
12692   Offset1 = 0;
12693   Offset2 = 0;
12694   bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
12695   bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
12696   if (isGA1 && isGA2 && GV1 == GV2)
12697     return Offset1 == (Offset2 + Dist*Bytes);
12698   return false;
12699 }
12700 
12701 // Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
12702 // not enforce equality of the chain operands.
12703 static bool isConsecutiveLS(SDNode *N, LSBaseSDNode *Base,
12704                             unsigned Bytes, int Dist,
12705                             SelectionDAG &DAG) {
12706   if (LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(N)) {
12707     EVT VT = LS->getMemoryVT();
12708     SDValue Loc = LS->getBasePtr();
12709     return isConsecutiveLSLoc(Loc, VT, Base, Bytes, Dist, DAG);
12710   }
12711 
12712   if (N->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
12713     EVT VT;
12714     switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
12715     default: return false;
12716     case Intrinsic::ppc_altivec_lvx:
12717     case Intrinsic::ppc_altivec_lvxl:
12718     case Intrinsic::ppc_vsx_lxvw4x:
12719     case Intrinsic::ppc_vsx_lxvw4x_be:
12720       VT = MVT::v4i32;
12721       break;
12722     case Intrinsic::ppc_vsx_lxvd2x:
12723     case Intrinsic::ppc_vsx_lxvd2x_be:
12724       VT = MVT::v2f64;
12725       break;
12726     case Intrinsic::ppc_altivec_lvebx:
12727       VT = MVT::i8;
12728       break;
12729     case Intrinsic::ppc_altivec_lvehx:
12730       VT = MVT::i16;
12731       break;
12732     case Intrinsic::ppc_altivec_lvewx:
12733       VT = MVT::i32;
12734       break;
12735     }
12736 
12737     return isConsecutiveLSLoc(N->getOperand(2), VT, Base, Bytes, Dist, DAG);
12738   }
12739 
12740   if (N->getOpcode() == ISD::INTRINSIC_VOID) {
12741     EVT VT;
12742     switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
12743     default: return false;
12744     case Intrinsic::ppc_altivec_stvx:
12745     case Intrinsic::ppc_altivec_stvxl:
12746     case Intrinsic::ppc_vsx_stxvw4x:
12747       VT = MVT::v4i32;
12748       break;
12749     case Intrinsic::ppc_vsx_stxvd2x:
12750       VT = MVT::v2f64;
12751       break;
12752     case Intrinsic::ppc_vsx_stxvw4x_be:
12753       VT = MVT::v4i32;
12754       break;
12755     case Intrinsic::ppc_vsx_stxvd2x_be:
12756       VT = MVT::v2f64;
12757       break;
12758     case Intrinsic::ppc_altivec_stvebx:
12759       VT = MVT::i8;
12760       break;
12761     case Intrinsic::ppc_altivec_stvehx:
12762       VT = MVT::i16;
12763       break;
12764     case Intrinsic::ppc_altivec_stvewx:
12765       VT = MVT::i32;
12766       break;
12767     }
12768 
12769     return isConsecutiveLSLoc(N->getOperand(3), VT, Base, Bytes, Dist, DAG);
12770   }
12771 
12772   return false;
12773 }
12774 
12775 // Return true is there is a nearyby consecutive load to the one provided
12776 // (regardless of alignment). We search up and down the chain, looking though
12777 // token factors and other loads (but nothing else). As a result, a true result
12778 // indicates that it is safe to create a new consecutive load adjacent to the
12779 // load provided.
12780 static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
12781   SDValue Chain = LD->getChain();
12782   EVT VT = LD->getMemoryVT();
12783 
12784   SmallSet<SDNode *, 16> LoadRoots;
12785   SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
12786   SmallSet<SDNode *, 16> Visited;
12787 
12788   // First, search up the chain, branching to follow all token-factor operands.
12789   // If we find a consecutive load, then we're done, otherwise, record all
12790   // nodes just above the top-level loads and token factors.
12791   while (!Queue.empty()) {
12792     SDNode *ChainNext = Queue.pop_back_val();
12793     if (!Visited.insert(ChainNext).second)
12794       continue;
12795 
12796     if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(ChainNext)) {
12797       if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
12798         return true;
12799 
12800       if (!Visited.count(ChainLD->getChain().getNode()))
12801         Queue.push_back(ChainLD->getChain().getNode());
12802     } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
12803       for (const SDUse &O : ChainNext->ops())
12804         if (!Visited.count(O.getNode()))
12805           Queue.push_back(O.getNode());
12806     } else
12807       LoadRoots.insert(ChainNext);
12808   }
12809 
12810   // Second, search down the chain, starting from the top-level nodes recorded
12811   // in the first phase. These top-level nodes are the nodes just above all
12812   // loads and token factors. Starting with their uses, recursively look though
12813   // all loads (just the chain uses) and token factors to find a consecutive
12814   // load.
12815   Visited.clear();
12816   Queue.clear();
12817 
12818   for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
12819        IE = LoadRoots.end(); I != IE; ++I) {
12820     Queue.push_back(*I);
12821 
12822     while (!Queue.empty()) {
12823       SDNode *LoadRoot = Queue.pop_back_val();
12824       if (!Visited.insert(LoadRoot).second)
12825         continue;
12826 
12827       if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(LoadRoot))
12828         if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
12829           return true;
12830 
12831       for (SDNode::use_iterator UI = LoadRoot->use_begin(),
12832            UE = LoadRoot->use_end(); UI != UE; ++UI)
12833         if (((isa<MemSDNode>(*UI) &&
12834             cast<MemSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
12835             UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
12836           Queue.push_back(*UI);
12837     }
12838   }
12839 
12840   return false;
12841 }
12842 
12843 /// This function is called when we have proved that a SETCC node can be replaced
12844 /// by subtraction (and other supporting instructions) so that the result of
12845 /// comparison is kept in a GPR instead of CR. This function is purely for
12846 /// codegen purposes and has some flags to guide the codegen process.
12847 static SDValue generateEquivalentSub(SDNode *N, int Size, bool Complement,
12848                                      bool Swap, SDLoc &DL, SelectionDAG &DAG) {
12849   assert(N->getOpcode() == ISD::SETCC && "ISD::SETCC Expected.");
12850 
12851   // Zero extend the operands to the largest legal integer. Originally, they
12852   // must be of a strictly smaller size.
12853   auto Op0 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, N->getOperand(0),
12854                          DAG.getConstant(Size, DL, MVT::i32));
12855   auto Op1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, N->getOperand(1),
12856                          DAG.getConstant(Size, DL, MVT::i32));
12857 
12858   // Swap if needed. Depends on the condition code.
12859   if (Swap)
12860     std::swap(Op0, Op1);
12861 
12862   // Subtract extended integers.
12863   auto SubNode = DAG.getNode(ISD::SUB, DL, MVT::i64, Op0, Op1);
12864 
12865   // Move the sign bit to the least significant position and zero out the rest.
12866   // Now the least significant bit carries the result of original comparison.
12867   auto Shifted = DAG.getNode(ISD::SRL, DL, MVT::i64, SubNode,
12868                              DAG.getConstant(Size - 1, DL, MVT::i32));
12869   auto Final = Shifted;
12870 
12871   // Complement the result if needed. Based on the condition code.
12872   if (Complement)
12873     Final = DAG.getNode(ISD::XOR, DL, MVT::i64, Shifted,
12874                         DAG.getConstant(1, DL, MVT::i64));
12875 
12876   return DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Final);
12877 }
12878 
12879 SDValue PPCTargetLowering::ConvertSETCCToSubtract(SDNode *N,
12880                                                   DAGCombinerInfo &DCI) const {
12881   assert(N->getOpcode() == ISD::SETCC && "ISD::SETCC Expected.");
12882 
12883   SelectionDAG &DAG = DCI.DAG;
12884   SDLoc DL(N);
12885 
12886   // Size of integers being compared has a critical role in the following
12887   // analysis, so we prefer to do this when all types are legal.
12888   if (!DCI.isAfterLegalizeDAG())
12889     return SDValue();
12890 
12891   // If all users of SETCC extend its value to a legal integer type
12892   // then we replace SETCC with a subtraction
12893   for (SDNode::use_iterator UI = N->use_begin(),
12894        UE = N->use_end(); UI != UE; ++UI) {
12895     if (UI->getOpcode() != ISD::ZERO_EXTEND)
12896       return SDValue();
12897   }
12898 
12899   ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
12900   auto OpSize = N->getOperand(0).getValueSizeInBits();
12901 
12902   unsigned Size = DAG.getDataLayout().getLargestLegalIntTypeSizeInBits();
12903 
12904   if (OpSize < Size) {
12905     switch (CC) {
12906     default: break;
12907     case ISD::SETULT:
12908       return generateEquivalentSub(N, Size, false, false, DL, DAG);
12909     case ISD::SETULE:
12910       return generateEquivalentSub(N, Size, true, true, DL, DAG);
12911     case ISD::SETUGT:
12912       return generateEquivalentSub(N, Size, false, true, DL, DAG);
12913     case ISD::SETUGE:
12914       return generateEquivalentSub(N, Size, true, false, DL, DAG);
12915     }
12916   }
12917 
12918   return SDValue();
12919 }
12920 
12921 SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N,
12922                                                   DAGCombinerInfo &DCI) const {
12923   SelectionDAG &DAG = DCI.DAG;
12924   SDLoc dl(N);
12925 
12926   assert(Subtarget.useCRBits() && "Expecting to be tracking CR bits");
12927   // If we're tracking CR bits, we need to be careful that we don't have:
12928   //   trunc(binary-ops(zext(x), zext(y)))
12929   // or
12930   //   trunc(binary-ops(binary-ops(zext(x), zext(y)), ...)
12931   // such that we're unnecessarily moving things into GPRs when it would be
12932   // better to keep them in CR bits.
12933 
12934   // Note that trunc here can be an actual i1 trunc, or can be the effective
12935   // truncation that comes from a setcc or select_cc.
12936   if (N->getOpcode() == ISD::TRUNCATE &&
12937       N->getValueType(0) != MVT::i1)
12938     return SDValue();
12939 
12940   if (N->getOperand(0).getValueType() != MVT::i32 &&
12941       N->getOperand(0).getValueType() != MVT::i64)
12942     return SDValue();
12943 
12944   if (N->getOpcode() == ISD::SETCC ||
12945       N->getOpcode() == ISD::SELECT_CC) {
12946     // If we're looking at a comparison, then we need to make sure that the
12947     // high bits (all except for the first) don't matter the result.
12948     ISD::CondCode CC =
12949       cast<CondCodeSDNode>(N->getOperand(
12950         N->getOpcode() == ISD::SETCC ? 2 : 4))->get();
12951     unsigned OpBits = N->getOperand(0).getValueSizeInBits();
12952 
12953     if (ISD::isSignedIntSetCC(CC)) {
12954       if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits ||
12955           DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits)
12956         return SDValue();
12957     } else if (ISD::isUnsignedIntSetCC(CC)) {
12958       if (!DAG.MaskedValueIsZero(N->getOperand(0),
12959                                  APInt::getHighBitsSet(OpBits, OpBits-1)) ||
12960           !DAG.MaskedValueIsZero(N->getOperand(1),
12961                                  APInt::getHighBitsSet(OpBits, OpBits-1)))
12962         return (N->getOpcode() == ISD::SETCC ? ConvertSETCCToSubtract(N, DCI)
12963                                              : SDValue());
12964     } else {
12965       // This is neither a signed nor an unsigned comparison, just make sure
12966       // that the high bits are equal.
12967       KnownBits Op1Known = DAG.computeKnownBits(N->getOperand(0));
12968       KnownBits Op2Known = DAG.computeKnownBits(N->getOperand(1));
12969 
12970       // We don't really care about what is known about the first bit (if
12971       // anything), so pretend that it is known zero for both to ensure they can
12972       // be compared as constants.
12973       Op1Known.Zero.setBit(0); Op1Known.One.clearBit(0);
12974       Op2Known.Zero.setBit(0); Op2Known.One.clearBit(0);
12975 
12976       if (!Op1Known.isConstant() || !Op2Known.isConstant() ||
12977           Op1Known.getConstant() != Op2Known.getConstant())
12978         return SDValue();
12979     }
12980   }
12981 
12982   // We now know that the higher-order bits are irrelevant, we just need to
12983   // make sure that all of the intermediate operations are bit operations, and
12984   // all inputs are extensions.
12985   if (N->getOperand(0).getOpcode() != ISD::AND &&
12986       N->getOperand(0).getOpcode() != ISD::OR  &&
12987       N->getOperand(0).getOpcode() != ISD::XOR &&
12988       N->getOperand(0).getOpcode() != ISD::SELECT &&
12989       N->getOperand(0).getOpcode() != ISD::SELECT_CC &&
12990       N->getOperand(0).getOpcode() != ISD::TRUNCATE &&
12991       N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND &&
12992       N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND &&
12993       N->getOperand(0).getOpcode() != ISD::ANY_EXTEND)
12994     return SDValue();
12995 
12996   if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) &&
12997       N->getOperand(1).getOpcode() != ISD::AND &&
12998       N->getOperand(1).getOpcode() != ISD::OR  &&
12999       N->getOperand(1).getOpcode() != ISD::XOR &&
13000       N->getOperand(1).getOpcode() != ISD::SELECT &&
13001       N->getOperand(1).getOpcode() != ISD::SELECT_CC &&
13002       N->getOperand(1).getOpcode() != ISD::TRUNCATE &&
13003       N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND &&
13004       N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND &&
13005       N->getOperand(1).getOpcode() != ISD::ANY_EXTEND)
13006     return SDValue();
13007 
13008   SmallVector<SDValue, 4> Inputs;
13009   SmallVector<SDValue, 8> BinOps, PromOps;
13010   SmallPtrSet<SDNode *, 16> Visited;
13011 
13012   for (unsigned i = 0; i < 2; ++i) {
13013     if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
13014           N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
13015           N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
13016           N->getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
13017         isa<ConstantSDNode>(N->getOperand(i)))
13018       Inputs.push_back(N->getOperand(i));
13019     else
13020       BinOps.push_back(N->getOperand(i));
13021 
13022     if (N->getOpcode() == ISD::TRUNCATE)
13023       break;
13024   }
13025 
13026   // Visit all inputs, collect all binary operations (and, or, xor and
13027   // select) that are all fed by extensions.
13028   while (!BinOps.empty()) {
13029     SDValue BinOp = BinOps.pop_back_val();
13030 
13031     if (!Visited.insert(BinOp.getNode()).second)
13032       continue;
13033 
13034     PromOps.push_back(BinOp);
13035 
13036     for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
13037       // The condition of the select is not promoted.
13038       if (BinOp.getOpcode() == ISD::SELECT && i == 0)
13039         continue;
13040       if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
13041         continue;
13042 
13043       if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
13044             BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
13045             BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
13046            BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
13047           isa<ConstantSDNode>(BinOp.getOperand(i))) {
13048         Inputs.push_back(BinOp.getOperand(i));
13049       } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
13050                  BinOp.getOperand(i).getOpcode() == ISD::OR  ||
13051                  BinOp.getOperand(i).getOpcode() == ISD::XOR ||
13052                  BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
13053                  BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC ||
13054                  BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
13055                  BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
13056                  BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
13057                  BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) {
13058         BinOps.push_back(BinOp.getOperand(i));
13059       } else {
13060         // We have an input that is not an extension or another binary
13061         // operation; we'll abort this transformation.
13062         return SDValue();
13063       }
13064     }
13065   }
13066 
13067   // Make sure that this is a self-contained cluster of operations (which
13068   // is not quite the same thing as saying that everything has only one
13069   // use).
13070   for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
13071     if (isa<ConstantSDNode>(Inputs[i]))
13072       continue;
13073 
13074     for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
13075                               UE = Inputs[i].getNode()->use_end();
13076          UI != UE; ++UI) {
13077       SDNode *User = *UI;
13078       if (User != N && !Visited.count(User))
13079         return SDValue();
13080 
13081       // Make sure that we're not going to promote the non-output-value
13082       // operand(s) or SELECT or SELECT_CC.
13083       // FIXME: Although we could sometimes handle this, and it does occur in
13084       // practice that one of the condition inputs to the select is also one of
13085       // the outputs, we currently can't deal with this.
13086       if (User->getOpcode() == ISD::SELECT) {
13087         if (User->getOperand(0) == Inputs[i])
13088           return SDValue();
13089       } else if (User->getOpcode() == ISD::SELECT_CC) {
13090         if (User->getOperand(0) == Inputs[i] ||
13091             User->getOperand(1) == Inputs[i])
13092           return SDValue();
13093       }
13094     }
13095   }
13096 
13097   for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
13098     for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
13099                               UE = PromOps[i].getNode()->use_end();
13100          UI != UE; ++UI) {
13101       SDNode *User = *UI;
13102       if (User != N && !Visited.count(User))
13103         return SDValue();
13104 
13105       // Make sure that we're not going to promote the non-output-value
13106       // operand(s) or SELECT or SELECT_CC.
13107       // FIXME: Although we could sometimes handle this, and it does occur in
13108       // practice that one of the condition inputs to the select is also one of
13109       // the outputs, we currently can't deal with this.
13110       if (User->getOpcode() == ISD::SELECT) {
13111         if (User->getOperand(0) == PromOps[i])
13112           return SDValue();
13113       } else if (User->getOpcode() == ISD::SELECT_CC) {
13114         if (User->getOperand(0) == PromOps[i] ||
13115             User->getOperand(1) == PromOps[i])
13116           return SDValue();
13117       }
13118     }
13119   }
13120 
13121   // Replace all inputs with the extension operand.
13122   for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
13123     // Constants may have users outside the cluster of to-be-promoted nodes,
13124     // and so we need to replace those as we do the promotions.
13125     if (isa<ConstantSDNode>(Inputs[i]))
13126       continue;
13127     else
13128       DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0));
13129   }
13130 
13131   std::list<HandleSDNode> PromOpHandles;
13132   for (auto &PromOp : PromOps)
13133     PromOpHandles.emplace_back(PromOp);
13134 
13135   // Replace all operations (these are all the same, but have a different
13136   // (i1) return type). DAG.getNode will validate that the types of
13137   // a binary operator match, so go through the list in reverse so that
13138   // we've likely promoted both operands first. Any intermediate truncations or
13139   // extensions disappear.
13140   while (!PromOpHandles.empty()) {
13141     SDValue PromOp = PromOpHandles.back().getValue();
13142     PromOpHandles.pop_back();
13143 
13144     if (PromOp.getOpcode() == ISD::TRUNCATE ||
13145         PromOp.getOpcode() == ISD::SIGN_EXTEND ||
13146         PromOp.getOpcode() == ISD::ZERO_EXTEND ||
13147         PromOp.getOpcode() == ISD::ANY_EXTEND) {
13148       if (!isa<ConstantSDNode>(PromOp.getOperand(0)) &&
13149           PromOp.getOperand(0).getValueType() != MVT::i1) {
13150         // The operand is not yet ready (see comment below).
13151         PromOpHandles.emplace_front(PromOp);
13152         continue;
13153       }
13154 
13155       SDValue RepValue = PromOp.getOperand(0);
13156       if (isa<ConstantSDNode>(RepValue))
13157         RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue);
13158 
13159       DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue);
13160       continue;
13161     }
13162 
13163     unsigned C;
13164     switch (PromOp.getOpcode()) {
13165     default:             C = 0; break;
13166     case ISD::SELECT:    C = 1; break;
13167     case ISD::SELECT_CC: C = 2; break;
13168     }
13169 
13170     if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
13171          PromOp.getOperand(C).getValueType() != MVT::i1) ||
13172         (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
13173          PromOp.getOperand(C+1).getValueType() != MVT::i1)) {
13174       // The to-be-promoted operands of this node have not yet been
13175       // promoted (this should be rare because we're going through the
13176       // list backward, but if one of the operands has several users in
13177       // this cluster of to-be-promoted nodes, it is possible).
13178       PromOpHandles.emplace_front(PromOp);
13179       continue;
13180     }
13181 
13182     SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
13183                                 PromOp.getNode()->op_end());
13184 
13185     // If there are any constant inputs, make sure they're replaced now.
13186     for (unsigned i = 0; i < 2; ++i)
13187       if (isa<ConstantSDNode>(Ops[C+i]))
13188         Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]);
13189 
13190     DAG.ReplaceAllUsesOfValueWith(PromOp,
13191       DAG.getNode(PromOp.getOpcode(), dl, MVT::i1, Ops));
13192   }
13193 
13194   // Now we're left with the initial truncation itself.
13195   if (N->getOpcode() == ISD::TRUNCATE)
13196     return N->getOperand(0);
13197 
13198   // Otherwise, this is a comparison. The operands to be compared have just
13199   // changed type (to i1), but everything else is the same.
13200   return SDValue(N, 0);
13201 }
13202 
13203 SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
13204                                                   DAGCombinerInfo &DCI) const {
13205   SelectionDAG &DAG = DCI.DAG;
13206   SDLoc dl(N);
13207 
13208   // If we're tracking CR bits, we need to be careful that we don't have:
13209   //   zext(binary-ops(trunc(x), trunc(y)))
13210   // or
13211   //   zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...)
13212   // such that we're unnecessarily moving things into CR bits that can more
13213   // efficiently stay in GPRs. Note that if we're not certain that the high
13214   // bits are set as required by the final extension, we still may need to do
13215   // some masking to get the proper behavior.
13216 
13217   // This same functionality is important on PPC64 when dealing with
13218   // 32-to-64-bit extensions; these occur often when 32-bit values are used as
13219   // the return values of functions. Because it is so similar, it is handled
13220   // here as well.
13221 
13222   if (N->getValueType(0) != MVT::i32 &&
13223       N->getValueType(0) != MVT::i64)
13224     return SDValue();
13225 
13226   if (!((N->getOperand(0).getValueType() == MVT::i1 && Subtarget.useCRBits()) ||
13227         (N->getOperand(0).getValueType() == MVT::i32 && Subtarget.isPPC64())))
13228     return SDValue();
13229 
13230   if (N->getOperand(0).getOpcode() != ISD::AND &&
13231       N->getOperand(0).getOpcode() != ISD::OR  &&
13232       N->getOperand(0).getOpcode() != ISD::XOR &&
13233       N->getOperand(0).getOpcode() != ISD::SELECT &&
13234       N->getOperand(0).getOpcode() != ISD::SELECT_CC)
13235     return SDValue();
13236 
13237   SmallVector<SDValue, 4> Inputs;
13238   SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps;
13239   SmallPtrSet<SDNode *, 16> Visited;
13240 
13241   // Visit all inputs, collect all binary operations (and, or, xor and
13242   // select) that are all fed by truncations.
13243   while (!BinOps.empty()) {
13244     SDValue BinOp = BinOps.pop_back_val();
13245 
13246     if (!Visited.insert(BinOp.getNode()).second)
13247       continue;
13248 
13249     PromOps.push_back(BinOp);
13250 
13251     for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
13252       // The condition of the select is not promoted.
13253       if (BinOp.getOpcode() == ISD::SELECT && i == 0)
13254         continue;
13255       if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
13256         continue;
13257 
13258       if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
13259           isa<ConstantSDNode>(BinOp.getOperand(i))) {
13260         Inputs.push_back(BinOp.getOperand(i));
13261       } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
13262                  BinOp.getOperand(i).getOpcode() == ISD::OR  ||
13263                  BinOp.getOperand(i).getOpcode() == ISD::XOR ||
13264                  BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
13265                  BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) {
13266         BinOps.push_back(BinOp.getOperand(i));
13267       } else {
13268         // We have an input that is not a truncation or another binary
13269         // operation; we'll abort this transformation.
13270         return SDValue();
13271       }
13272     }
13273   }
13274 
13275   // The operands of a select that must be truncated when the select is
13276   // promoted because the operand is actually part of the to-be-promoted set.
13277   DenseMap<SDNode *, EVT> SelectTruncOp[2];
13278 
13279   // Make sure that this is a self-contained cluster of operations (which
13280   // is not quite the same thing as saying that everything has only one
13281   // use).
13282   for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
13283     if (isa<ConstantSDNode>(Inputs[i]))
13284       continue;
13285 
13286     for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
13287                               UE = Inputs[i].getNode()->use_end();
13288          UI != UE; ++UI) {
13289       SDNode *User = *UI;
13290       if (User != N && !Visited.count(User))
13291         return SDValue();
13292 
13293       // If we're going to promote the non-output-value operand(s) or SELECT or
13294       // SELECT_CC, record them for truncation.
13295       if (User->getOpcode() == ISD::SELECT) {
13296         if (User->getOperand(0) == Inputs[i])
13297           SelectTruncOp[0].insert(std::make_pair(User,
13298                                     User->getOperand(0).getValueType()));
13299       } else if (User->getOpcode() == ISD::SELECT_CC) {
13300         if (User->getOperand(0) == Inputs[i])
13301           SelectTruncOp[0].insert(std::make_pair(User,
13302                                     User->getOperand(0).getValueType()));
13303         if (User->getOperand(1) == Inputs[i])
13304           SelectTruncOp[1].insert(std::make_pair(User,
13305                                     User->getOperand(1).getValueType()));
13306       }
13307     }
13308   }
13309 
13310   for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
13311     for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
13312                               UE = PromOps[i].getNode()->use_end();
13313          UI != UE; ++UI) {
13314       SDNode *User = *UI;
13315       if (User != N && !Visited.count(User))
13316         return SDValue();
13317 
13318       // If we're going to promote the non-output-value operand(s) or SELECT or
13319       // SELECT_CC, record them for truncation.
13320       if (User->getOpcode() == ISD::SELECT) {
13321         if (User->getOperand(0) == PromOps[i])
13322           SelectTruncOp[0].insert(std::make_pair(User,
13323                                     User->getOperand(0).getValueType()));
13324       } else if (User->getOpcode() == ISD::SELECT_CC) {
13325         if (User->getOperand(0) == PromOps[i])
13326           SelectTruncOp[0].insert(std::make_pair(User,
13327                                     User->getOperand(0).getValueType()));
13328         if (User->getOperand(1) == PromOps[i])
13329           SelectTruncOp[1].insert(std::make_pair(User,
13330                                     User->getOperand(1).getValueType()));
13331       }
13332     }
13333   }
13334 
13335   unsigned PromBits = N->getOperand(0).getValueSizeInBits();
13336   bool ReallyNeedsExt = false;
13337   if (N->getOpcode() != ISD::ANY_EXTEND) {
13338     // If all of the inputs are not already sign/zero extended, then
13339     // we'll still need to do that at the end.
13340     for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
13341       if (isa<ConstantSDNode>(Inputs[i]))
13342         continue;
13343 
13344       unsigned OpBits =
13345         Inputs[i].getOperand(0).getValueSizeInBits();
13346       assert(PromBits < OpBits && "Truncation not to a smaller bit count?");
13347 
13348       if ((N->getOpcode() == ISD::ZERO_EXTEND &&
13349            !DAG.MaskedValueIsZero(Inputs[i].getOperand(0),
13350                                   APInt::getHighBitsSet(OpBits,
13351                                                         OpBits-PromBits))) ||
13352           (N->getOpcode() == ISD::SIGN_EXTEND &&
13353            DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) <
13354              (OpBits-(PromBits-1)))) {
13355         ReallyNeedsExt = true;
13356         break;
13357       }
13358     }
13359   }
13360 
13361   // Replace all inputs, either with the truncation operand, or a
13362   // truncation or extension to the final output type.
13363   for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
13364     // Constant inputs need to be replaced with the to-be-promoted nodes that
13365     // use them because they might have users outside of the cluster of
13366     // promoted nodes.
13367     if (isa<ConstantSDNode>(Inputs[i]))
13368       continue;
13369 
13370     SDValue InSrc = Inputs[i].getOperand(0);
13371     if (Inputs[i].getValueType() == N->getValueType(0))
13372       DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc);
13373     else if (N->getOpcode() == ISD::SIGN_EXTEND)
13374       DAG.ReplaceAllUsesOfValueWith(Inputs[i],
13375         DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0)));
13376     else if (N->getOpcode() == ISD::ZERO_EXTEND)
13377       DAG.ReplaceAllUsesOfValueWith(Inputs[i],
13378         DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0)));
13379     else
13380       DAG.ReplaceAllUsesOfValueWith(Inputs[i],
13381         DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0)));
13382   }
13383 
13384   std::list<HandleSDNode> PromOpHandles;
13385   for (auto &PromOp : PromOps)
13386     PromOpHandles.emplace_back(PromOp);
13387 
13388   // Replace all operations (these are all the same, but have a different
13389   // (promoted) return type). DAG.getNode will validate that the types of
13390   // a binary operator match, so go through the list in reverse so that
13391   // we've likely promoted both operands first.
13392   while (!PromOpHandles.empty()) {
13393     SDValue PromOp = PromOpHandles.back().getValue();
13394     PromOpHandles.pop_back();
13395 
13396     unsigned C;
13397     switch (PromOp.getOpcode()) {
13398     default:             C = 0; break;
13399     case ISD::SELECT:    C = 1; break;
13400     case ISD::SELECT_CC: C = 2; break;
13401     }
13402 
13403     if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
13404          PromOp.getOperand(C).getValueType() != N->getValueType(0)) ||
13405         (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
13406          PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) {
13407       // The to-be-promoted operands of this node have not yet been
13408       // promoted (this should be rare because we're going through the
13409       // list backward, but if one of the operands has several users in
13410       // this cluster of to-be-promoted nodes, it is possible).
13411       PromOpHandles.emplace_front(PromOp);
13412       continue;
13413     }
13414 
13415     // For SELECT and SELECT_CC nodes, we do a similar check for any
13416     // to-be-promoted comparison inputs.
13417     if (PromOp.getOpcode() == ISD::SELECT ||
13418         PromOp.getOpcode() == ISD::SELECT_CC) {
13419       if ((SelectTruncOp[0].count(PromOp.getNode()) &&
13420            PromOp.getOperand(0).getValueType() != N->getValueType(0)) ||
13421           (SelectTruncOp[1].count(PromOp.getNode()) &&
13422            PromOp.getOperand(1).getValueType() != N->getValueType(0))) {
13423         PromOpHandles.emplace_front(PromOp);
13424         continue;
13425       }
13426     }
13427 
13428     SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
13429                                 PromOp.getNode()->op_end());
13430 
13431     // If this node has constant inputs, then they'll need to be promoted here.
13432     for (unsigned i = 0; i < 2; ++i) {
13433       if (!isa<ConstantSDNode>(Ops[C+i]))
13434         continue;
13435       if (Ops[C+i].getValueType() == N->getValueType(0))
13436         continue;
13437 
13438       if (N->getOpcode() == ISD::SIGN_EXTEND)
13439         Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
13440       else if (N->getOpcode() == ISD::ZERO_EXTEND)
13441         Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
13442       else
13443         Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
13444     }
13445 
13446     // If we've promoted the comparison inputs of a SELECT or SELECT_CC,
13447     // truncate them again to the original value type.
13448     if (PromOp.getOpcode() == ISD::SELECT ||
13449         PromOp.getOpcode() == ISD::SELECT_CC) {
13450       auto SI0 = SelectTruncOp[0].find(PromOp.getNode());
13451       if (SI0 != SelectTruncOp[0].end())
13452         Ops[0] = DAG.getNode(ISD::TRUNCATE, dl, SI0->second, Ops[0]);
13453       auto SI1 = SelectTruncOp[1].find(PromOp.getNode());
13454       if (SI1 != SelectTruncOp[1].end())
13455         Ops[1] = DAG.getNode(ISD::TRUNCATE, dl, SI1->second, Ops[1]);
13456     }
13457 
13458     DAG.ReplaceAllUsesOfValueWith(PromOp,
13459       DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0), Ops));
13460   }
13461 
13462   // Now we're left with the initial extension itself.
13463   if (!ReallyNeedsExt)
13464     return N->getOperand(0);
13465 
13466   // To zero extend, just mask off everything except for the first bit (in the
13467   // i1 case).
13468   if (N->getOpcode() == ISD::ZERO_EXTEND)
13469     return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0),
13470                        DAG.getConstant(APInt::getLowBitsSet(
13471                                          N->getValueSizeInBits(0), PromBits),
13472                                        dl, N->getValueType(0)));
13473 
13474   assert(N->getOpcode() == ISD::SIGN_EXTEND &&
13475          "Invalid extension type");
13476   EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0), DAG.getDataLayout());
13477   SDValue ShiftCst =
13478       DAG.getConstant(N->getValueSizeInBits(0) - PromBits, dl, ShiftAmountTy);
13479   return DAG.getNode(
13480       ISD::SRA, dl, N->getValueType(0),
13481       DAG.getNode(ISD::SHL, dl, N->getValueType(0), N->getOperand(0), ShiftCst),
13482       ShiftCst);
13483 }
13484 
13485 SDValue PPCTargetLowering::combineSetCC(SDNode *N,
13486                                         DAGCombinerInfo &DCI) const {
13487   assert(N->getOpcode() == ISD::SETCC &&
13488          "Should be called with a SETCC node");
13489 
13490   ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
13491   if (CC == ISD::SETNE || CC == ISD::SETEQ) {
13492     SDValue LHS = N->getOperand(0);
13493     SDValue RHS = N->getOperand(1);
13494 
13495     // If there is a '0 - y' pattern, canonicalize the pattern to the RHS.
13496     if (LHS.getOpcode() == ISD::SUB && isNullConstant(LHS.getOperand(0)) &&
13497         LHS.hasOneUse())
13498       std::swap(LHS, RHS);
13499 
13500     // x == 0-y --> x+y == 0
13501     // x != 0-y --> x+y != 0
13502     if (RHS.getOpcode() == ISD::SUB && isNullConstant(RHS.getOperand(0)) &&
13503         RHS.hasOneUse()) {
13504       SDLoc DL(N);
13505       SelectionDAG &DAG = DCI.DAG;
13506       EVT VT = N->getValueType(0);
13507       EVT OpVT = LHS.getValueType();
13508       SDValue Add = DAG.getNode(ISD::ADD, DL, OpVT, LHS, RHS.getOperand(1));
13509       return DAG.getSetCC(DL, VT, Add, DAG.getConstant(0, DL, OpVT), CC);
13510     }
13511   }
13512 
13513   return DAGCombineTruncBoolExt(N, DCI);
13514 }
13515 
13516 // Is this an extending load from an f32 to an f64?
13517 static bool isFPExtLoad(SDValue Op) {
13518   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op.getNode()))
13519     return LD->getExtensionType() == ISD::EXTLOAD &&
13520       Op.getValueType() == MVT::f64;
13521   return false;
13522 }
13523 
13524 /// Reduces the number of fp-to-int conversion when building a vector.
13525 ///
13526 /// If this vector is built out of floating to integer conversions,
13527 /// transform it to a vector built out of floating point values followed by a
13528 /// single floating to integer conversion of the vector.
13529 /// Namely  (build_vector (fptosi $A), (fptosi $B), ...)
13530 /// becomes (fptosi (build_vector ($A, $B, ...)))
13531 SDValue PPCTargetLowering::
13532 combineElementTruncationToVectorTruncation(SDNode *N,
13533                                            DAGCombinerInfo &DCI) const {
13534   assert(N->getOpcode() == ISD::BUILD_VECTOR &&
13535          "Should be called with a BUILD_VECTOR node");
13536 
13537   SelectionDAG &DAG = DCI.DAG;
13538   SDLoc dl(N);
13539 
13540   SDValue FirstInput = N->getOperand(0);
13541   assert(FirstInput.getOpcode() == PPCISD::MFVSR &&
13542          "The input operand must be an fp-to-int conversion.");
13543 
13544   // This combine happens after legalization so the fp_to_[su]i nodes are
13545   // already converted to PPCSISD nodes.
13546   unsigned FirstConversion = FirstInput.getOperand(0).getOpcode();
13547   if (FirstConversion == PPCISD::FCTIDZ ||
13548       FirstConversion == PPCISD::FCTIDUZ ||
13549       FirstConversion == PPCISD::FCTIWZ ||
13550       FirstConversion == PPCISD::FCTIWUZ) {
13551     bool IsSplat = true;
13552     bool Is32Bit = FirstConversion == PPCISD::FCTIWZ ||
13553       FirstConversion == PPCISD::FCTIWUZ;
13554     EVT SrcVT = FirstInput.getOperand(0).getValueType();
13555     SmallVector<SDValue, 4> Ops;
13556     EVT TargetVT = N->getValueType(0);
13557     for (int i = 0, e = N->getNumOperands(); i < e; ++i) {
13558       SDValue NextOp = N->getOperand(i);
13559       if (NextOp.getOpcode() != PPCISD::MFVSR)
13560         return SDValue();
13561       unsigned NextConversion = NextOp.getOperand(0).getOpcode();
13562       if (NextConversion != FirstConversion)
13563         return SDValue();
13564       // If we are converting to 32-bit integers, we need to add an FP_ROUND.
13565       // This is not valid if the input was originally double precision. It is
13566       // also not profitable to do unless this is an extending load in which
13567       // case doing this combine will allow us to combine consecutive loads.
13568       if (Is32Bit && !isFPExtLoad(NextOp.getOperand(0).getOperand(0)))
13569         return SDValue();
13570       if (N->getOperand(i) != FirstInput)
13571         IsSplat = false;
13572     }
13573 
13574     // If this is a splat, we leave it as-is since there will be only a single
13575     // fp-to-int conversion followed by a splat of the integer. This is better
13576     // for 32-bit and smaller ints and neutral for 64-bit ints.
13577     if (IsSplat)
13578       return SDValue();
13579 
13580     // Now that we know we have the right type of node, get its operands
13581     for (int i = 0, e = N->getNumOperands(); i < e; ++i) {
13582       SDValue In = N->getOperand(i).getOperand(0);
13583       if (Is32Bit) {
13584         // For 32-bit values, we need to add an FP_ROUND node (if we made it
13585         // here, we know that all inputs are extending loads so this is safe).
13586         if (In.isUndef())
13587           Ops.push_back(DAG.getUNDEF(SrcVT));
13588         else {
13589           SDValue Trunc = DAG.getNode(ISD::FP_ROUND, dl,
13590                                       MVT::f32, In.getOperand(0),
13591                                       DAG.getIntPtrConstant(1, dl));
13592           Ops.push_back(Trunc);
13593         }
13594       } else
13595         Ops.push_back(In.isUndef() ? DAG.getUNDEF(SrcVT) : In.getOperand(0));
13596     }
13597 
13598     unsigned Opcode;
13599     if (FirstConversion == PPCISD::FCTIDZ ||
13600         FirstConversion == PPCISD::FCTIWZ)
13601       Opcode = ISD::FP_TO_SINT;
13602     else
13603       Opcode = ISD::FP_TO_UINT;
13604 
13605     EVT NewVT = TargetVT == MVT::v2i64 ? MVT::v2f64 : MVT::v4f32;
13606     SDValue BV = DAG.getBuildVector(NewVT, dl, Ops);
13607     return DAG.getNode(Opcode, dl, TargetVT, BV);
13608   }
13609   return SDValue();
13610 }
13611 
13612 /// Reduce the number of loads when building a vector.
13613 ///
13614 /// Building a vector out of multiple loads can be converted to a load
13615 /// of the vector type if the loads are consecutive. If the loads are
13616 /// consecutive but in descending order, a shuffle is added at the end
13617 /// to reorder the vector.
13618 static SDValue combineBVOfConsecutiveLoads(SDNode *N, SelectionDAG &DAG) {
13619   assert(N->getOpcode() == ISD::BUILD_VECTOR &&
13620          "Should be called with a BUILD_VECTOR node");
13621 
13622   SDLoc dl(N);
13623 
13624   // Return early for non byte-sized type, as they can't be consecutive.
13625   if (!N->getValueType(0).getVectorElementType().isByteSized())
13626     return SDValue();
13627 
13628   bool InputsAreConsecutiveLoads = true;
13629   bool InputsAreReverseConsecutive = true;
13630   unsigned ElemSize = N->getValueType(0).getScalarType().getStoreSize();
13631   SDValue FirstInput = N->getOperand(0);
13632   bool IsRoundOfExtLoad = false;
13633 
13634   if (FirstInput.getOpcode() == ISD::FP_ROUND &&
13635       FirstInput.getOperand(0).getOpcode() == ISD::LOAD) {
13636     LoadSDNode *LD = dyn_cast<LoadSDNode>(FirstInput.getOperand(0));
13637     IsRoundOfExtLoad = LD->getExtensionType() == ISD::EXTLOAD;
13638   }
13639   // Not a build vector of (possibly fp_rounded) loads.
13640   if ((!IsRoundOfExtLoad && FirstInput.getOpcode() != ISD::LOAD) ||
13641       N->getNumOperands() == 1)
13642     return SDValue();
13643 
13644   for (int i = 1, e = N->getNumOperands(); i < e; ++i) {
13645     // If any inputs are fp_round(extload), they all must be.
13646     if (IsRoundOfExtLoad && N->getOperand(i).getOpcode() != ISD::FP_ROUND)
13647       return SDValue();
13648 
13649     SDValue NextInput = IsRoundOfExtLoad ? N->getOperand(i).getOperand(0) :
13650       N->getOperand(i);
13651     if (NextInput.getOpcode() != ISD::LOAD)
13652       return SDValue();
13653 
13654     SDValue PreviousInput =
13655       IsRoundOfExtLoad ? N->getOperand(i-1).getOperand(0) : N->getOperand(i-1);
13656     LoadSDNode *LD1 = dyn_cast<LoadSDNode>(PreviousInput);
13657     LoadSDNode *LD2 = dyn_cast<LoadSDNode>(NextInput);
13658 
13659     // If any inputs are fp_round(extload), they all must be.
13660     if (IsRoundOfExtLoad && LD2->getExtensionType() != ISD::EXTLOAD)
13661       return SDValue();
13662 
13663     if (!isConsecutiveLS(LD2, LD1, ElemSize, 1, DAG))
13664       InputsAreConsecutiveLoads = false;
13665     if (!isConsecutiveLS(LD1, LD2, ElemSize, 1, DAG))
13666       InputsAreReverseConsecutive = false;
13667 
13668     // Exit early if the loads are neither consecutive nor reverse consecutive.
13669     if (!InputsAreConsecutiveLoads && !InputsAreReverseConsecutive)
13670       return SDValue();
13671   }
13672 
13673   assert(!(InputsAreConsecutiveLoads && InputsAreReverseConsecutive) &&
13674          "The loads cannot be both consecutive and reverse consecutive.");
13675 
13676   SDValue FirstLoadOp =
13677     IsRoundOfExtLoad ? FirstInput.getOperand(0) : FirstInput;
13678   SDValue LastLoadOp =
13679     IsRoundOfExtLoad ? N->getOperand(N->getNumOperands()-1).getOperand(0) :
13680                        N->getOperand(N->getNumOperands()-1);
13681 
13682   LoadSDNode *LD1 = dyn_cast<LoadSDNode>(FirstLoadOp);
13683   LoadSDNode *LDL = dyn_cast<LoadSDNode>(LastLoadOp);
13684   if (InputsAreConsecutiveLoads) {
13685     assert(LD1 && "Input needs to be a LoadSDNode.");
13686     return DAG.getLoad(N->getValueType(0), dl, LD1->getChain(),
13687                        LD1->getBasePtr(), LD1->getPointerInfo(),
13688                        LD1->getAlignment());
13689   }
13690   if (InputsAreReverseConsecutive) {
13691     assert(LDL && "Input needs to be a LoadSDNode.");
13692     SDValue Load = DAG.getLoad(N->getValueType(0), dl, LDL->getChain(),
13693                                LDL->getBasePtr(), LDL->getPointerInfo(),
13694                                LDL->getAlignment());
13695     SmallVector<int, 16> Ops;
13696     for (int i = N->getNumOperands() - 1; i >= 0; i--)
13697       Ops.push_back(i);
13698 
13699     return DAG.getVectorShuffle(N->getValueType(0), dl, Load,
13700                                 DAG.getUNDEF(N->getValueType(0)), Ops);
13701   }
13702   return SDValue();
13703 }
13704 
13705 // This function adds the required vector_shuffle needed to get
13706 // the elements of the vector extract in the correct position
13707 // as specified by the CorrectElems encoding.
13708 static SDValue addShuffleForVecExtend(SDNode *N, SelectionDAG &DAG,
13709                                       SDValue Input, uint64_t Elems,
13710                                       uint64_t CorrectElems) {
13711   SDLoc dl(N);
13712 
13713   unsigned NumElems = Input.getValueType().getVectorNumElements();
13714   SmallVector<int, 16> ShuffleMask(NumElems, -1);
13715 
13716   // Knowing the element indices being extracted from the original
13717   // vector and the order in which they're being inserted, just put
13718   // them at element indices required for the instruction.
13719   for (unsigned i = 0; i < N->getNumOperands(); i++) {
13720     if (DAG.getDataLayout().isLittleEndian())
13721       ShuffleMask[CorrectElems & 0xF] = Elems & 0xF;
13722     else
13723       ShuffleMask[(CorrectElems & 0xF0) >> 4] = (Elems & 0xF0) >> 4;
13724     CorrectElems = CorrectElems >> 8;
13725     Elems = Elems >> 8;
13726   }
13727 
13728   SDValue Shuffle =
13729       DAG.getVectorShuffle(Input.getValueType(), dl, Input,
13730                            DAG.getUNDEF(Input.getValueType()), ShuffleMask);
13731 
13732   EVT VT = N->getValueType(0);
13733   SDValue Conv = DAG.getBitcast(VT, Shuffle);
13734 
13735   EVT ExtVT = EVT::getVectorVT(*DAG.getContext(),
13736                                Input.getValueType().getVectorElementType(),
13737                                VT.getVectorNumElements());
13738   return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT, Conv,
13739                      DAG.getValueType(ExtVT));
13740 }
13741 
13742 // Look for build vector patterns where input operands come from sign
13743 // extended vector_extract elements of specific indices. If the correct indices
13744 // aren't used, add a vector shuffle to fix up the indices and create
13745 // SIGN_EXTEND_INREG node which selects the vector sign extend instructions
13746 // during instruction selection.
13747 static SDValue combineBVOfVecSExt(SDNode *N, SelectionDAG &DAG) {
13748   // This array encodes the indices that the vector sign extend instructions
13749   // extract from when extending from one type to another for both BE and LE.
13750   // The right nibble of each byte corresponds to the LE incides.
13751   // and the left nibble of each byte corresponds to the BE incides.
13752   // For example: 0x3074B8FC  byte->word
13753   // For LE: the allowed indices are: 0x0,0x4,0x8,0xC
13754   // For BE: the allowed indices are: 0x3,0x7,0xB,0xF
13755   // For example: 0x000070F8  byte->double word
13756   // For LE: the allowed indices are: 0x0,0x8
13757   // For BE: the allowed indices are: 0x7,0xF
13758   uint64_t TargetElems[] = {
13759       0x3074B8FC, // b->w
13760       0x000070F8, // b->d
13761       0x10325476, // h->w
13762       0x00003074, // h->d
13763       0x00001032, // w->d
13764   };
13765 
13766   uint64_t Elems = 0;
13767   int Index;
13768   SDValue Input;
13769 
13770   auto isSExtOfVecExtract = [&](SDValue Op) -> bool {
13771     if (!Op)
13772       return false;
13773     if (Op.getOpcode() != ISD::SIGN_EXTEND &&
13774         Op.getOpcode() != ISD::SIGN_EXTEND_INREG)
13775       return false;
13776 
13777     // A SIGN_EXTEND_INREG might be fed by an ANY_EXTEND to produce a value
13778     // of the right width.
13779     SDValue Extract = Op.getOperand(0);
13780     if (Extract.getOpcode() == ISD::ANY_EXTEND)
13781       Extract = Extract.getOperand(0);
13782     if (Extract.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13783       return false;
13784 
13785     ConstantSDNode *ExtOp = dyn_cast<ConstantSDNode>(Extract.getOperand(1));
13786     if (!ExtOp)
13787       return false;
13788 
13789     Index = ExtOp->getZExtValue();
13790     if (Input && Input != Extract.getOperand(0))
13791       return false;
13792 
13793     if (!Input)
13794       Input = Extract.getOperand(0);
13795 
13796     Elems = Elems << 8;
13797     Index = DAG.getDataLayout().isLittleEndian() ? Index : Index << 4;
13798     Elems |= Index;
13799 
13800     return true;
13801   };
13802 
13803   // If the build vector operands aren't sign extended vector extracts,
13804   // of the same input vector, then return.
13805   for (unsigned i = 0; i < N->getNumOperands(); i++) {
13806     if (!isSExtOfVecExtract(N->getOperand(i))) {
13807       return SDValue();
13808     }
13809   }
13810 
13811   // If the vector extract indicies are not correct, add the appropriate
13812   // vector_shuffle.
13813   int TgtElemArrayIdx;
13814   int InputSize = Input.getValueType().getScalarSizeInBits();
13815   int OutputSize = N->getValueType(0).getScalarSizeInBits();
13816   if (InputSize + OutputSize == 40)
13817     TgtElemArrayIdx = 0;
13818   else if (InputSize + OutputSize == 72)
13819     TgtElemArrayIdx = 1;
13820   else if (InputSize + OutputSize == 48)
13821     TgtElemArrayIdx = 2;
13822   else if (InputSize + OutputSize == 80)
13823     TgtElemArrayIdx = 3;
13824   else if (InputSize + OutputSize == 96)
13825     TgtElemArrayIdx = 4;
13826   else
13827     return SDValue();
13828 
13829   uint64_t CorrectElems = TargetElems[TgtElemArrayIdx];
13830   CorrectElems = DAG.getDataLayout().isLittleEndian()
13831                      ? CorrectElems & 0x0F0F0F0F0F0F0F0F
13832                      : CorrectElems & 0xF0F0F0F0F0F0F0F0;
13833   if (Elems != CorrectElems) {
13834     return addShuffleForVecExtend(N, DAG, Input, Elems, CorrectElems);
13835   }
13836 
13837   // Regular lowering will catch cases where a shuffle is not needed.
13838   return SDValue();
13839 }
13840 
13841 // Look for the pattern of a load from a narrow width to i128, feeding
13842 // into a BUILD_VECTOR of v1i128. Replace this sequence with a PPCISD node
13843 // (LXVRZX). This node represents a zero extending load that will be matched
13844 // to the Load VSX Vector Rightmost instructions.
13845 static SDValue combineBVZEXTLOAD(SDNode *N, SelectionDAG &DAG) {
13846   SDLoc DL(N);
13847 
13848   // This combine is only eligible for a BUILD_VECTOR of v1i128.
13849   if (N->getValueType(0) != MVT::v1i128)
13850     return SDValue();
13851 
13852   SDValue Operand = N->getOperand(0);
13853   // Proceed with the transformation if the operand to the BUILD_VECTOR
13854   // is a load instruction.
13855   if (Operand.getOpcode() != ISD::LOAD)
13856     return SDValue();
13857 
13858   LoadSDNode *LD = dyn_cast<LoadSDNode>(Operand);
13859   EVT MemoryType = LD->getMemoryVT();
13860 
13861   // This transformation is only valid if the we are loading either a byte,
13862   // halfword, word, or doubleword.
13863   bool ValidLDType = MemoryType == MVT::i8 || MemoryType == MVT::i16 ||
13864                      MemoryType == MVT::i32 || MemoryType == MVT::i64;
13865 
13866   // Ensure that the load from the narrow width is being zero extended to i128.
13867   if (!ValidLDType ||
13868       (LD->getExtensionType() != ISD::ZEXTLOAD &&
13869        LD->getExtensionType() != ISD::EXTLOAD))
13870     return SDValue();
13871 
13872   SDValue LoadOps[] = {
13873       LD->getChain(), LD->getBasePtr(),
13874       DAG.getIntPtrConstant(MemoryType.getScalarSizeInBits(), DL)};
13875 
13876   return DAG.getMemIntrinsicNode(PPCISD::LXVRZX, DL,
13877                                  DAG.getVTList(MVT::v1i128, MVT::Other),
13878                                  LoadOps, MemoryType, LD->getMemOperand());
13879 }
13880 
13881 SDValue PPCTargetLowering::DAGCombineBuildVector(SDNode *N,
13882                                                  DAGCombinerInfo &DCI) const {
13883   assert(N->getOpcode() == ISD::BUILD_VECTOR &&
13884          "Should be called with a BUILD_VECTOR node");
13885 
13886   SelectionDAG &DAG = DCI.DAG;
13887   SDLoc dl(N);
13888 
13889   if (!Subtarget.hasVSX())
13890     return SDValue();
13891 
13892   // The target independent DAG combiner will leave a build_vector of
13893   // float-to-int conversions intact. We can generate MUCH better code for
13894   // a float-to-int conversion of a vector of floats.
13895   SDValue FirstInput = N->getOperand(0);
13896   if (FirstInput.getOpcode() == PPCISD::MFVSR) {
13897     SDValue Reduced = combineElementTruncationToVectorTruncation(N, DCI);
13898     if (Reduced)
13899       return Reduced;
13900   }
13901 
13902   // If we're building a vector out of consecutive loads, just load that
13903   // vector type.
13904   SDValue Reduced = combineBVOfConsecutiveLoads(N, DAG);
13905   if (Reduced)
13906     return Reduced;
13907 
13908   // If we're building a vector out of extended elements from another vector
13909   // we have P9 vector integer extend instructions. The code assumes legal
13910   // input types (i.e. it can't handle things like v4i16) so do not run before
13911   // legalization.
13912   if (Subtarget.hasP9Altivec() && !DCI.isBeforeLegalize()) {
13913     Reduced = combineBVOfVecSExt(N, DAG);
13914     if (Reduced)
13915       return Reduced;
13916   }
13917 
13918   // On Power10, the Load VSX Vector Rightmost instructions can be utilized
13919   // if this is a BUILD_VECTOR of v1i128, and if the operand to the BUILD_VECTOR
13920   // is a load from <valid narrow width> to i128.
13921   if (Subtarget.isISA3_1()) {
13922     SDValue BVOfZLoad = combineBVZEXTLOAD(N, DAG);
13923     if (BVOfZLoad)
13924       return BVOfZLoad;
13925   }
13926 
13927   if (N->getValueType(0) != MVT::v2f64)
13928     return SDValue();
13929 
13930   // Looking for:
13931   // (build_vector ([su]int_to_fp (extractelt 0)), [su]int_to_fp (extractelt 1))
13932   if (FirstInput.getOpcode() != ISD::SINT_TO_FP &&
13933       FirstInput.getOpcode() != ISD::UINT_TO_FP)
13934     return SDValue();
13935   if (N->getOperand(1).getOpcode() != ISD::SINT_TO_FP &&
13936       N->getOperand(1).getOpcode() != ISD::UINT_TO_FP)
13937     return SDValue();
13938   if (FirstInput.getOpcode() != N->getOperand(1).getOpcode())
13939     return SDValue();
13940 
13941   SDValue Ext1 = FirstInput.getOperand(0);
13942   SDValue Ext2 = N->getOperand(1).getOperand(0);
13943   if(Ext1.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13944      Ext2.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13945     return SDValue();
13946 
13947   ConstantSDNode *Ext1Op = dyn_cast<ConstantSDNode>(Ext1.getOperand(1));
13948   ConstantSDNode *Ext2Op = dyn_cast<ConstantSDNode>(Ext2.getOperand(1));
13949   if (!Ext1Op || !Ext2Op)
13950     return SDValue();
13951   if (Ext1.getOperand(0).getValueType() != MVT::v4i32 ||
13952       Ext1.getOperand(0) != Ext2.getOperand(0))
13953     return SDValue();
13954 
13955   int FirstElem = Ext1Op->getZExtValue();
13956   int SecondElem = Ext2Op->getZExtValue();
13957   int SubvecIdx;
13958   if (FirstElem == 0 && SecondElem == 1)
13959     SubvecIdx = Subtarget.isLittleEndian() ? 1 : 0;
13960   else if (FirstElem == 2 && SecondElem == 3)
13961     SubvecIdx = Subtarget.isLittleEndian() ? 0 : 1;
13962   else
13963     return SDValue();
13964 
13965   SDValue SrcVec = Ext1.getOperand(0);
13966   auto NodeType = (N->getOperand(1).getOpcode() == ISD::SINT_TO_FP) ?
13967     PPCISD::SINT_VEC_TO_FP : PPCISD::UINT_VEC_TO_FP;
13968   return DAG.getNode(NodeType, dl, MVT::v2f64,
13969                      SrcVec, DAG.getIntPtrConstant(SubvecIdx, dl));
13970 }
13971 
13972 SDValue PPCTargetLowering::combineFPToIntToFP(SDNode *N,
13973                                               DAGCombinerInfo &DCI) const {
13974   assert((N->getOpcode() == ISD::SINT_TO_FP ||
13975           N->getOpcode() == ISD::UINT_TO_FP) &&
13976          "Need an int -> FP conversion node here");
13977 
13978   if (useSoftFloat() || !Subtarget.has64BitSupport())
13979     return SDValue();
13980 
13981   SelectionDAG &DAG = DCI.DAG;
13982   SDLoc dl(N);
13983   SDValue Op(N, 0);
13984 
13985   // Don't handle ppc_fp128 here or conversions that are out-of-range capable
13986   // from the hardware.
13987   if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
13988     return SDValue();
13989   if (!Op.getOperand(0).getValueType().isSimple())
13990     return SDValue();
13991   if (Op.getOperand(0).getValueType().getSimpleVT() <= MVT(MVT::i1) ||
13992       Op.getOperand(0).getValueType().getSimpleVT() > MVT(MVT::i64))
13993     return SDValue();
13994 
13995   SDValue FirstOperand(Op.getOperand(0));
13996   bool SubWordLoad = FirstOperand.getOpcode() == ISD::LOAD &&
13997     (FirstOperand.getValueType() == MVT::i8 ||
13998      FirstOperand.getValueType() == MVT::i16);
13999   if (Subtarget.hasP9Vector() && Subtarget.hasP9Altivec() && SubWordLoad) {
14000     bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
14001     bool DstDouble = Op.getValueType() == MVT::f64;
14002     unsigned ConvOp = Signed ?
14003       (DstDouble ? PPCISD::FCFID  : PPCISD::FCFIDS) :
14004       (DstDouble ? PPCISD::FCFIDU : PPCISD::FCFIDUS);
14005     SDValue WidthConst =
14006       DAG.getIntPtrConstant(FirstOperand.getValueType() == MVT::i8 ? 1 : 2,
14007                             dl, false);
14008     LoadSDNode *LDN = cast<LoadSDNode>(FirstOperand.getNode());
14009     SDValue Ops[] = { LDN->getChain(), LDN->getBasePtr(), WidthConst };
14010     SDValue Ld = DAG.getMemIntrinsicNode(PPCISD::LXSIZX, dl,
14011                                          DAG.getVTList(MVT::f64, MVT::Other),
14012                                          Ops, MVT::i8, LDN->getMemOperand());
14013 
14014     // For signed conversion, we need to sign-extend the value in the VSR
14015     if (Signed) {
14016       SDValue ExtOps[] = { Ld, WidthConst };
14017       SDValue Ext = DAG.getNode(PPCISD::VEXTS, dl, MVT::f64, ExtOps);
14018       return DAG.getNode(ConvOp, dl, DstDouble ? MVT::f64 : MVT::f32, Ext);
14019     } else
14020       return DAG.getNode(ConvOp, dl, DstDouble ? MVT::f64 : MVT::f32, Ld);
14021   }
14022 
14023 
14024   // For i32 intermediate values, unfortunately, the conversion functions
14025   // leave the upper 32 bits of the value are undefined. Within the set of
14026   // scalar instructions, we have no method for zero- or sign-extending the
14027   // value. Thus, we cannot handle i32 intermediate values here.
14028   if (Op.getOperand(0).getValueType() == MVT::i32)
14029     return SDValue();
14030 
14031   assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) &&
14032          "UINT_TO_FP is supported only with FPCVT");
14033 
14034   // If we have FCFIDS, then use it when converting to single-precision.
14035   // Otherwise, convert to double-precision and then round.
14036   unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
14037                        ? (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDUS
14038                                                             : PPCISD::FCFIDS)
14039                        : (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDU
14040                                                             : PPCISD::FCFID);
14041   MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32)
14042                   ? MVT::f32
14043                   : MVT::f64;
14044 
14045   // If we're converting from a float, to an int, and back to a float again,
14046   // then we don't need the store/load pair at all.
14047   if ((Op.getOperand(0).getOpcode() == ISD::FP_TO_UINT &&
14048        Subtarget.hasFPCVT()) ||
14049       (Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT)) {
14050     SDValue Src = Op.getOperand(0).getOperand(0);
14051     if (Src.getValueType() == MVT::f32) {
14052       Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
14053       DCI.AddToWorklist(Src.getNode());
14054     } else if (Src.getValueType() != MVT::f64) {
14055       // Make sure that we don't pick up a ppc_fp128 source value.
14056       return SDValue();
14057     }
14058 
14059     unsigned FCTOp =
14060       Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
14061                                                         PPCISD::FCTIDUZ;
14062 
14063     SDValue Tmp = DAG.getNode(FCTOp, dl, MVT::f64, Src);
14064     SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Tmp);
14065 
14066     if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT()) {
14067       FP = DAG.getNode(ISD::FP_ROUND, dl,
14068                        MVT::f32, FP, DAG.getIntPtrConstant(0, dl));
14069       DCI.AddToWorklist(FP.getNode());
14070     }
14071 
14072     return FP;
14073   }
14074 
14075   return SDValue();
14076 }
14077 
14078 // expandVSXLoadForLE - Convert VSX loads (which may be intrinsics for
14079 // builtins) into loads with swaps.
14080 SDValue PPCTargetLowering::expandVSXLoadForLE(SDNode *N,
14081                                               DAGCombinerInfo &DCI) const {
14082   SelectionDAG &DAG = DCI.DAG;
14083   SDLoc dl(N);
14084   SDValue Chain;
14085   SDValue Base;
14086   MachineMemOperand *MMO;
14087 
14088   switch (N->getOpcode()) {
14089   default:
14090     llvm_unreachable("Unexpected opcode for little endian VSX load");
14091   case ISD::LOAD: {
14092     LoadSDNode *LD = cast<LoadSDNode>(N);
14093     Chain = LD->getChain();
14094     Base = LD->getBasePtr();
14095     MMO = LD->getMemOperand();
14096     // If the MMO suggests this isn't a load of a full vector, leave
14097     // things alone.  For a built-in, we have to make the change for
14098     // correctness, so if there is a size problem that will be a bug.
14099     if (MMO->getSize() < 16)
14100       return SDValue();
14101     break;
14102   }
14103   case ISD::INTRINSIC_W_CHAIN: {
14104     MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N);
14105     Chain = Intrin->getChain();
14106     // Similarly to the store case below, Intrin->getBasePtr() doesn't get
14107     // us what we want. Get operand 2 instead.
14108     Base = Intrin->getOperand(2);
14109     MMO = Intrin->getMemOperand();
14110     break;
14111   }
14112   }
14113 
14114   MVT VecTy = N->getValueType(0).getSimpleVT();
14115 
14116   // Do not expand to PPCISD::LXVD2X + PPCISD::XXSWAPD when the load is
14117   // aligned and the type is a vector with elements up to 4 bytes
14118   if (Subtarget.needsSwapsForVSXMemOps() && MMO->getAlign() >= Align(16) &&
14119       VecTy.getScalarSizeInBits() <= 32) {
14120     return SDValue();
14121   }
14122 
14123   SDValue LoadOps[] = { Chain, Base };
14124   SDValue Load = DAG.getMemIntrinsicNode(PPCISD::LXVD2X, dl,
14125                                          DAG.getVTList(MVT::v2f64, MVT::Other),
14126                                          LoadOps, MVT::v2f64, MMO);
14127 
14128   DCI.AddToWorklist(Load.getNode());
14129   Chain = Load.getValue(1);
14130   SDValue Swap = DAG.getNode(
14131       PPCISD::XXSWAPD, dl, DAG.getVTList(MVT::v2f64, MVT::Other), Chain, Load);
14132   DCI.AddToWorklist(Swap.getNode());
14133 
14134   // Add a bitcast if the resulting load type doesn't match v2f64.
14135   if (VecTy != MVT::v2f64) {
14136     SDValue N = DAG.getNode(ISD::BITCAST, dl, VecTy, Swap);
14137     DCI.AddToWorklist(N.getNode());
14138     // Package {bitcast value, swap's chain} to match Load's shape.
14139     return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(VecTy, MVT::Other),
14140                        N, Swap.getValue(1));
14141   }
14142 
14143   return Swap;
14144 }
14145 
14146 // expandVSXStoreForLE - Convert VSX stores (which may be intrinsics for
14147 // builtins) into stores with swaps.
14148 SDValue PPCTargetLowering::expandVSXStoreForLE(SDNode *N,
14149                                                DAGCombinerInfo &DCI) const {
14150   SelectionDAG &DAG = DCI.DAG;
14151   SDLoc dl(N);
14152   SDValue Chain;
14153   SDValue Base;
14154   unsigned SrcOpnd;
14155   MachineMemOperand *MMO;
14156 
14157   switch (N->getOpcode()) {
14158   default:
14159     llvm_unreachable("Unexpected opcode for little endian VSX store");
14160   case ISD::STORE: {
14161     StoreSDNode *ST = cast<StoreSDNode>(N);
14162     Chain = ST->getChain();
14163     Base = ST->getBasePtr();
14164     MMO = ST->getMemOperand();
14165     SrcOpnd = 1;
14166     // If the MMO suggests this isn't a store of a full vector, leave
14167     // things alone.  For a built-in, we have to make the change for
14168     // correctness, so if there is a size problem that will be a bug.
14169     if (MMO->getSize() < 16)
14170       return SDValue();
14171     break;
14172   }
14173   case ISD::INTRINSIC_VOID: {
14174     MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N);
14175     Chain = Intrin->getChain();
14176     // Intrin->getBasePtr() oddly does not get what we want.
14177     Base = Intrin->getOperand(3);
14178     MMO = Intrin->getMemOperand();
14179     SrcOpnd = 2;
14180     break;
14181   }
14182   }
14183 
14184   SDValue Src = N->getOperand(SrcOpnd);
14185   MVT VecTy = Src.getValueType().getSimpleVT();
14186 
14187   // Do not expand to PPCISD::XXSWAPD and PPCISD::STXVD2X when the load is
14188   // aligned and the type is a vector with elements up to 4 bytes
14189   if (Subtarget.needsSwapsForVSXMemOps() && MMO->getAlign() >= Align(16) &&
14190       VecTy.getScalarSizeInBits() <= 32) {
14191     return SDValue();
14192   }
14193 
14194   // All stores are done as v2f64 and possible bit cast.
14195   if (VecTy != MVT::v2f64) {
14196     Src = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Src);
14197     DCI.AddToWorklist(Src.getNode());
14198   }
14199 
14200   SDValue Swap = DAG.getNode(PPCISD::XXSWAPD, dl,
14201                              DAG.getVTList(MVT::v2f64, MVT::Other), Chain, Src);
14202   DCI.AddToWorklist(Swap.getNode());
14203   Chain = Swap.getValue(1);
14204   SDValue StoreOps[] = { Chain, Swap, Base };
14205   SDValue Store = DAG.getMemIntrinsicNode(PPCISD::STXVD2X, dl,
14206                                           DAG.getVTList(MVT::Other),
14207                                           StoreOps, VecTy, MMO);
14208   DCI.AddToWorklist(Store.getNode());
14209   return Store;
14210 }
14211 
14212 // Handle DAG combine for STORE (FP_TO_INT F).
14213 SDValue PPCTargetLowering::combineStoreFPToInt(SDNode *N,
14214                                                DAGCombinerInfo &DCI) const {
14215 
14216   SelectionDAG &DAG = DCI.DAG;
14217   SDLoc dl(N);
14218   unsigned Opcode = N->getOperand(1).getOpcode();
14219 
14220   assert((Opcode == ISD::FP_TO_SINT || Opcode == ISD::FP_TO_UINT)
14221          && "Not a FP_TO_INT Instruction!");
14222 
14223   SDValue Val = N->getOperand(1).getOperand(0);
14224   EVT Op1VT = N->getOperand(1).getValueType();
14225   EVT ResVT = Val.getValueType();
14226 
14227   if (!isTypeLegal(ResVT))
14228     return SDValue();
14229 
14230   // Only perform combine for conversion to i64/i32 or power9 i16/i8.
14231   bool ValidTypeForStoreFltAsInt =
14232         (Op1VT == MVT::i32 || Op1VT == MVT::i64 ||
14233          (Subtarget.hasP9Vector() && (Op1VT == MVT::i16 || Op1VT == MVT::i8)));
14234 
14235   if (ResVT == MVT::f128 && !Subtarget.hasP9Vector())
14236     return SDValue();
14237 
14238   if (ResVT == MVT::ppcf128 || !Subtarget.hasP8Vector() ||
14239       cast<StoreSDNode>(N)->isTruncatingStore() || !ValidTypeForStoreFltAsInt)
14240     return SDValue();
14241 
14242   // Extend f32 values to f64
14243   if (ResVT.getScalarSizeInBits() == 32) {
14244     Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
14245     DCI.AddToWorklist(Val.getNode());
14246   }
14247 
14248   // Set signed or unsigned conversion opcode.
14249   unsigned ConvOpcode = (Opcode == ISD::FP_TO_SINT) ?
14250                           PPCISD::FP_TO_SINT_IN_VSR :
14251                           PPCISD::FP_TO_UINT_IN_VSR;
14252 
14253   Val = DAG.getNode(ConvOpcode,
14254                     dl, ResVT == MVT::f128 ? MVT::f128 : MVT::f64, Val);
14255   DCI.AddToWorklist(Val.getNode());
14256 
14257   // Set number of bytes being converted.
14258   unsigned ByteSize = Op1VT.getScalarSizeInBits() / 8;
14259   SDValue Ops[] = { N->getOperand(0), Val, N->getOperand(2),
14260                     DAG.getIntPtrConstant(ByteSize, dl, false),
14261                     DAG.getValueType(Op1VT) };
14262 
14263   Val = DAG.getMemIntrinsicNode(PPCISD::ST_VSR_SCAL_INT, dl,
14264           DAG.getVTList(MVT::Other), Ops,
14265           cast<StoreSDNode>(N)->getMemoryVT(),
14266           cast<StoreSDNode>(N)->getMemOperand());
14267 
14268   DCI.AddToWorklist(Val.getNode());
14269   return Val;
14270 }
14271 
14272 static bool isAlternatingShuffMask(const ArrayRef<int> &Mask, int NumElts) {
14273   // Check that the source of the element keeps flipping
14274   // (i.e. Mask[i] < NumElts -> Mask[i+i] >= NumElts).
14275   bool PrevElemFromFirstVec = Mask[0] < NumElts;
14276   for (int i = 1, e = Mask.size(); i < e; i++) {
14277     if (PrevElemFromFirstVec && Mask[i] < NumElts)
14278       return false;
14279     if (!PrevElemFromFirstVec && Mask[i] >= NumElts)
14280       return false;
14281     PrevElemFromFirstVec = !PrevElemFromFirstVec;
14282   }
14283   return true;
14284 }
14285 
14286 static bool isSplatBV(SDValue Op) {
14287   if (Op.getOpcode() != ISD::BUILD_VECTOR)
14288     return false;
14289   SDValue FirstOp;
14290 
14291   // Find first non-undef input.
14292   for (int i = 0, e = Op.getNumOperands(); i < e; i++) {
14293     FirstOp = Op.getOperand(i);
14294     if (!FirstOp.isUndef())
14295       break;
14296   }
14297 
14298   // All inputs are undef or the same as the first non-undef input.
14299   for (int i = 1, e = Op.getNumOperands(); i < e; i++)
14300     if (Op.getOperand(i) != FirstOp && !Op.getOperand(i).isUndef())
14301       return false;
14302   return true;
14303 }
14304 
14305 static SDValue isScalarToVec(SDValue Op) {
14306   if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR)
14307     return Op;
14308   if (Op.getOpcode() != ISD::BITCAST)
14309     return SDValue();
14310   Op = Op.getOperand(0);
14311   if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR)
14312     return Op;
14313   return SDValue();
14314 }
14315 
14316 // Fix up the shuffle mask to account for the fact that the result of
14317 // scalar_to_vector is not in lane zero. This just takes all values in
14318 // the ranges specified by the min/max indices and adds the number of
14319 // elements required to ensure each element comes from the respective
14320 // position in the valid lane.
14321 // On little endian, that's just the corresponding element in the other
14322 // half of the vector. On big endian, it is in the same half but right
14323 // justified rather than left justified in that half.
14324 static void fixupShuffleMaskForPermutedSToV(SmallVectorImpl<int> &ShuffV,
14325                                             int LHSMaxIdx, int RHSMinIdx,
14326                                             int RHSMaxIdx, int HalfVec,
14327                                             unsigned ValidLaneWidth,
14328                                             const PPCSubtarget &Subtarget) {
14329   for (int i = 0, e = ShuffV.size(); i < e; i++) {
14330     int Idx = ShuffV[i];
14331     if ((Idx >= 0 && Idx < LHSMaxIdx) || (Idx >= RHSMinIdx && Idx < RHSMaxIdx))
14332       ShuffV[i] +=
14333           Subtarget.isLittleEndian() ? HalfVec : HalfVec - ValidLaneWidth;
14334   }
14335 }
14336 
14337 // Replace a SCALAR_TO_VECTOR with a SCALAR_TO_VECTOR_PERMUTED except if
14338 // the original is:
14339 // (<n x Ty> (scalar_to_vector (Ty (extract_elt <n x Ty> %a, C))))
14340 // In such a case, just change the shuffle mask to extract the element
14341 // from the permuted index.
14342 static SDValue getSToVPermuted(SDValue OrigSToV, SelectionDAG &DAG,
14343                                const PPCSubtarget &Subtarget) {
14344   SDLoc dl(OrigSToV);
14345   EVT VT = OrigSToV.getValueType();
14346   assert(OrigSToV.getOpcode() == ISD::SCALAR_TO_VECTOR &&
14347          "Expecting a SCALAR_TO_VECTOR here");
14348   SDValue Input = OrigSToV.getOperand(0);
14349 
14350   if (Input.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
14351     ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Input.getOperand(1));
14352     SDValue OrigVector = Input.getOperand(0);
14353 
14354     // Can't handle non-const element indices or different vector types
14355     // for the input to the extract and the output of the scalar_to_vector.
14356     if (Idx && VT == OrigVector.getValueType()) {
14357       unsigned NumElts = VT.getVectorNumElements();
14358       assert(
14359           NumElts > 1 &&
14360           "Cannot produce a permuted scalar_to_vector for one element vector");
14361       SmallVector<int, 16> NewMask(NumElts, -1);
14362       unsigned ResultInElt = NumElts / 2;
14363       ResultInElt -= Subtarget.isLittleEndian() ? 0 : 1;
14364       NewMask[ResultInElt] = Idx->getZExtValue();
14365       return DAG.getVectorShuffle(VT, dl, OrigVector, OrigVector, NewMask);
14366     }
14367   }
14368   return DAG.getNode(PPCISD::SCALAR_TO_VECTOR_PERMUTED, dl, VT,
14369                      OrigSToV.getOperand(0));
14370 }
14371 
14372 // On little endian subtargets, combine shuffles such as:
14373 // vector_shuffle<16,1,17,3,18,5,19,7,20,9,21,11,22,13,23,15>, <zero>, %b
14374 // into:
14375 // vector_shuffle<16,0,17,1,18,2,19,3,20,4,21,5,22,6,23,7>, <zero>, %b
14376 // because the latter can be matched to a single instruction merge.
14377 // Furthermore, SCALAR_TO_VECTOR on little endian always involves a permute
14378 // to put the value into element zero. Adjust the shuffle mask so that the
14379 // vector can remain in permuted form (to prevent a swap prior to a shuffle).
14380 // On big endian targets, this is still useful for SCALAR_TO_VECTOR
14381 // nodes with elements smaller than doubleword because all the ways
14382 // of getting scalar data into a vector register put the value in the
14383 // rightmost element of the left half of the vector.
14384 SDValue PPCTargetLowering::combineVectorShuffle(ShuffleVectorSDNode *SVN,
14385                                                 SelectionDAG &DAG) const {
14386   SDValue LHS = SVN->getOperand(0);
14387   SDValue RHS = SVN->getOperand(1);
14388   auto Mask = SVN->getMask();
14389   int NumElts = LHS.getValueType().getVectorNumElements();
14390   SDValue Res(SVN, 0);
14391   SDLoc dl(SVN);
14392   bool IsLittleEndian = Subtarget.isLittleEndian();
14393 
14394   // On little endian targets, do these combines on all VSX targets since
14395   // canonical shuffles match efficient permutes. On big endian targets,
14396   // this is only useful for targets with direct moves.
14397   if (!Subtarget.hasDirectMove() && !(IsLittleEndian && Subtarget.hasVSX()))
14398     return Res;
14399 
14400   // If this is not a shuffle of a shuffle and the first element comes from
14401   // the second vector, canonicalize to the commuted form. This will make it
14402   // more likely to match one of the single instruction patterns.
14403   if (Mask[0] >= NumElts && LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14404       RHS.getOpcode() != ISD::VECTOR_SHUFFLE) {
14405     std::swap(LHS, RHS);
14406     Res = DAG.getCommutedVectorShuffle(*SVN);
14407     Mask = cast<ShuffleVectorSDNode>(Res)->getMask();
14408   }
14409 
14410   // Adjust the shuffle mask if either input vector comes from a
14411   // SCALAR_TO_VECTOR and keep the respective input vector in permuted
14412   // form (to prevent the need for a swap).
14413   SmallVector<int, 16> ShuffV(Mask.begin(), Mask.end());
14414   SDValue SToVLHS = isScalarToVec(LHS);
14415   SDValue SToVRHS = isScalarToVec(RHS);
14416   if (SToVLHS || SToVRHS) {
14417     int NumEltsIn = SToVLHS ? SToVLHS.getValueType().getVectorNumElements()
14418                             : SToVRHS.getValueType().getVectorNumElements();
14419     int NumEltsOut = ShuffV.size();
14420     unsigned InElemSizeInBits =
14421         SToVLHS ? SToVLHS.getValueType().getScalarSizeInBits()
14422                 : SToVRHS.getValueType().getScalarSizeInBits();
14423     unsigned OutElemSizeInBits = SToVLHS
14424                                      ? LHS.getValueType().getScalarSizeInBits()
14425                                      : RHS.getValueType().getScalarSizeInBits();
14426 
14427     // The width of the "valid lane" (i.e. the lane that contains the value that
14428     // is vectorized) needs to be expressed in terms of the number of elements
14429     // of the shuffle. It is thereby the ratio of the values before and after
14430     // any bitcast.
14431     unsigned ValidLaneWidth = InElemSizeInBits / OutElemSizeInBits;
14432 
14433     // Initially assume that neither input is permuted. These will be adjusted
14434     // accordingly if either input is.
14435     int LHSMaxIdx = -1;
14436     int RHSMinIdx = -1;
14437     int RHSMaxIdx = -1;
14438     int HalfVec = LHS.getValueType().getVectorNumElements() / 2;
14439 
14440     // Get the permuted scalar to vector nodes for the source(s) that come from
14441     // ISD::SCALAR_TO_VECTOR.
14442     // On big endian systems, this only makes sense for element sizes smaller
14443     // than 64 bits since for 64-bit elements, all instructions already put
14444     // the value into element zero.
14445     if (SToVLHS) {
14446       if (!IsLittleEndian && InElemSizeInBits >= 64)
14447         return Res;
14448       // Set up the values for the shuffle vector fixup.
14449       LHSMaxIdx = NumEltsOut / NumEltsIn;
14450       SToVLHS = getSToVPermuted(SToVLHS, DAG, Subtarget);
14451       if (SToVLHS.getValueType() != LHS.getValueType())
14452         SToVLHS = DAG.getBitcast(LHS.getValueType(), SToVLHS);
14453       LHS = SToVLHS;
14454     }
14455     if (SToVRHS) {
14456       if (!IsLittleEndian && InElemSizeInBits >= 64)
14457         return Res;
14458       RHSMinIdx = NumEltsOut;
14459       RHSMaxIdx = NumEltsOut / NumEltsIn + RHSMinIdx;
14460       SToVRHS = getSToVPermuted(SToVRHS, DAG, Subtarget);
14461       if (SToVRHS.getValueType() != RHS.getValueType())
14462         SToVRHS = DAG.getBitcast(RHS.getValueType(), SToVRHS);
14463       RHS = SToVRHS;
14464     }
14465 
14466     // Fix up the shuffle mask to reflect where the desired element actually is.
14467     // The minimum and maximum indices that correspond to element zero for both
14468     // the LHS and RHS are computed and will control which shuffle mask entries
14469     // are to be changed. For example, if the RHS is permuted, any shuffle mask
14470     // entries in the range [RHSMinIdx,RHSMaxIdx) will be adjusted.
14471     fixupShuffleMaskForPermutedSToV(ShuffV, LHSMaxIdx, RHSMinIdx, RHSMaxIdx,
14472                                     HalfVec, ValidLaneWidth, Subtarget);
14473     Res = DAG.getVectorShuffle(SVN->getValueType(0), dl, LHS, RHS, ShuffV);
14474 
14475     // We may have simplified away the shuffle. We won't be able to do anything
14476     // further with it here.
14477     if (!isa<ShuffleVectorSDNode>(Res))
14478       return Res;
14479     Mask = cast<ShuffleVectorSDNode>(Res)->getMask();
14480   }
14481 
14482   SDValue TheSplat = IsLittleEndian ? RHS : LHS;
14483   // The common case after we commuted the shuffle is that the RHS is a splat
14484   // and we have elements coming in from the splat at indices that are not
14485   // conducive to using a merge.
14486   // Example:
14487   // vector_shuffle<0,17,1,19,2,21,3,23,4,25,5,27,6,29,7,31> t1, <zero>
14488   if (!isSplatBV(TheSplat))
14489     return Res;
14490 
14491   // We are looking for a mask such that all even elements are from
14492   // one vector and all odd elements from the other.
14493   if (!isAlternatingShuffMask(Mask, NumElts))
14494     return Res;
14495 
14496   // Adjust the mask so we are pulling in the same index from the splat
14497   // as the index from the interesting vector in consecutive elements.
14498   if (IsLittleEndian) {
14499     // Example (even elements from first vector):
14500     // vector_shuffle<0,16,1,17,2,18,3,19,4,20,5,21,6,22,7,23> t1, <zero>
14501     if (Mask[0] < NumElts)
14502       for (int i = 1, e = Mask.size(); i < e; i += 2)
14503         ShuffV[i] = (ShuffV[i - 1] + NumElts);
14504     // Example (odd elements from first vector):
14505     // vector_shuffle<16,0,17,1,18,2,19,3,20,4,21,5,22,6,23,7> t1, <zero>
14506     else
14507       for (int i = 0, e = Mask.size(); i < e; i += 2)
14508         ShuffV[i] = (ShuffV[i + 1] + NumElts);
14509   } else {
14510     // Example (even elements from first vector):
14511     // vector_shuffle<0,16,1,17,2,18,3,19,4,20,5,21,6,22,7,23> <zero>, t1
14512     if (Mask[0] < NumElts)
14513       for (int i = 0, e = Mask.size(); i < e; i += 2)
14514         ShuffV[i] = ShuffV[i + 1] - NumElts;
14515     // Example (odd elements from first vector):
14516     // vector_shuffle<16,0,17,1,18,2,19,3,20,4,21,5,22,6,23,7> <zero>, t1
14517     else
14518       for (int i = 1, e = Mask.size(); i < e; i += 2)
14519         ShuffV[i] = ShuffV[i - 1] - NumElts;
14520   }
14521 
14522   // If the RHS has undefs, we need to remove them since we may have created
14523   // a shuffle that adds those instead of the splat value.
14524   SDValue SplatVal =
14525       cast<BuildVectorSDNode>(TheSplat.getNode())->getSplatValue();
14526   TheSplat = DAG.getSplatBuildVector(TheSplat.getValueType(), dl, SplatVal);
14527 
14528   if (IsLittleEndian)
14529     RHS = TheSplat;
14530   else
14531     LHS = TheSplat;
14532   return DAG.getVectorShuffle(SVN->getValueType(0), dl, LHS, RHS, ShuffV);
14533 }
14534 
14535 SDValue PPCTargetLowering::combineVReverseMemOP(ShuffleVectorSDNode *SVN,
14536                                                 LSBaseSDNode *LSBase,
14537                                                 DAGCombinerInfo &DCI) const {
14538   assert((ISD::isNormalLoad(LSBase) || ISD::isNormalStore(LSBase)) &&
14539         "Not a reverse memop pattern!");
14540 
14541   auto IsElementReverse = [](const ShuffleVectorSDNode *SVN) -> bool {
14542     auto Mask = SVN->getMask();
14543     int i = 0;
14544     auto I = Mask.rbegin();
14545     auto E = Mask.rend();
14546 
14547     for (; I != E; ++I) {
14548       if (*I != i)
14549         return false;
14550       i++;
14551     }
14552     return true;
14553   };
14554 
14555   SelectionDAG &DAG = DCI.DAG;
14556   EVT VT = SVN->getValueType(0);
14557 
14558   if (!isTypeLegal(VT) || !Subtarget.isLittleEndian() || !Subtarget.hasVSX())
14559     return SDValue();
14560 
14561   // Before P9, we have PPCVSXSwapRemoval pass to hack the element order.
14562   // See comment in PPCVSXSwapRemoval.cpp.
14563   // It is conflict with PPCVSXSwapRemoval opt. So we don't do it.
14564   if (!Subtarget.hasP9Vector())
14565     return SDValue();
14566 
14567   if(!IsElementReverse(SVN))
14568     return SDValue();
14569 
14570   if (LSBase->getOpcode() == ISD::LOAD) {
14571     // If the load has more than one user except the shufflevector instruction,
14572     // it is not profitable to replace the shufflevector with a reverse load.
14573     if (!LSBase->hasOneUse())
14574       return SDValue();
14575 
14576     SDLoc dl(SVN);
14577     SDValue LoadOps[] = {LSBase->getChain(), LSBase->getBasePtr()};
14578     return DAG.getMemIntrinsicNode(
14579         PPCISD::LOAD_VEC_BE, dl, DAG.getVTList(VT, MVT::Other), LoadOps,
14580         LSBase->getMemoryVT(), LSBase->getMemOperand());
14581   }
14582 
14583   if (LSBase->getOpcode() == ISD::STORE) {
14584     // If there are other uses of the shuffle, the swap cannot be avoided.
14585     // Forcing the use of an X-Form (since swapped stores only have
14586     // X-Forms) without removing the swap is unprofitable.
14587     if (!SVN->hasOneUse())
14588       return SDValue();
14589 
14590     SDLoc dl(LSBase);
14591     SDValue StoreOps[] = {LSBase->getChain(), SVN->getOperand(0),
14592                           LSBase->getBasePtr()};
14593     return DAG.getMemIntrinsicNode(
14594         PPCISD::STORE_VEC_BE, dl, DAG.getVTList(MVT::Other), StoreOps,
14595         LSBase->getMemoryVT(), LSBase->getMemOperand());
14596   }
14597 
14598   llvm_unreachable("Expected a load or store node here");
14599 }
14600 
14601 SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
14602                                              DAGCombinerInfo &DCI) const {
14603   SelectionDAG &DAG = DCI.DAG;
14604   SDLoc dl(N);
14605   switch (N->getOpcode()) {
14606   default: break;
14607   case ISD::ADD:
14608     return combineADD(N, DCI);
14609   case ISD::SHL:
14610     return combineSHL(N, DCI);
14611   case ISD::SRA:
14612     return combineSRA(N, DCI);
14613   case ISD::SRL:
14614     return combineSRL(N, DCI);
14615   case ISD::MUL:
14616     return combineMUL(N, DCI);
14617   case ISD::FMA:
14618   case PPCISD::FNMSUB:
14619     return combineFMALike(N, DCI);
14620   case PPCISD::SHL:
14621     if (isNullConstant(N->getOperand(0))) // 0 << V -> 0.
14622         return N->getOperand(0);
14623     break;
14624   case PPCISD::SRL:
14625     if (isNullConstant(N->getOperand(0))) // 0 >>u V -> 0.
14626         return N->getOperand(0);
14627     break;
14628   case PPCISD::SRA:
14629     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
14630       if (C->isNullValue() ||   //  0 >>s V -> 0.
14631           C->isAllOnesValue())    // -1 >>s V -> -1.
14632         return N->getOperand(0);
14633     }
14634     break;
14635   case ISD::SIGN_EXTEND:
14636   case ISD::ZERO_EXTEND:
14637   case ISD::ANY_EXTEND:
14638     return DAGCombineExtBoolTrunc(N, DCI);
14639   case ISD::TRUNCATE:
14640     return combineTRUNCATE(N, DCI);
14641   case ISD::SETCC:
14642     if (SDValue CSCC = combineSetCC(N, DCI))
14643       return CSCC;
14644     LLVM_FALLTHROUGH;
14645   case ISD::SELECT_CC:
14646     return DAGCombineTruncBoolExt(N, DCI);
14647   case ISD::SINT_TO_FP:
14648   case ISD::UINT_TO_FP:
14649     return combineFPToIntToFP(N, DCI);
14650   case ISD::VECTOR_SHUFFLE:
14651     if (ISD::isNormalLoad(N->getOperand(0).getNode())) {
14652       LSBaseSDNode* LSBase = cast<LSBaseSDNode>(N->getOperand(0));
14653       return combineVReverseMemOP(cast<ShuffleVectorSDNode>(N), LSBase, DCI);
14654     }
14655     return combineVectorShuffle(cast<ShuffleVectorSDNode>(N), DCI.DAG);
14656   case ISD::STORE: {
14657 
14658     EVT Op1VT = N->getOperand(1).getValueType();
14659     unsigned Opcode = N->getOperand(1).getOpcode();
14660 
14661     if (Opcode == ISD::FP_TO_SINT || Opcode == ISD::FP_TO_UINT) {
14662       SDValue Val= combineStoreFPToInt(N, DCI);
14663       if (Val)
14664         return Val;
14665     }
14666 
14667     if (Opcode == ISD::VECTOR_SHUFFLE && ISD::isNormalStore(N)) {
14668       ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N->getOperand(1));
14669       SDValue Val= combineVReverseMemOP(SVN, cast<LSBaseSDNode>(N), DCI);
14670       if (Val)
14671         return Val;
14672     }
14673 
14674     // Turn STORE (BSWAP) -> sthbrx/stwbrx.
14675     if (cast<StoreSDNode>(N)->isUnindexed() && Opcode == ISD::BSWAP &&
14676         N->getOperand(1).getNode()->hasOneUse() &&
14677         (Op1VT == MVT::i32 || Op1VT == MVT::i16 ||
14678          (Subtarget.hasLDBRX() && Subtarget.isPPC64() && Op1VT == MVT::i64))) {
14679 
14680       // STBRX can only handle simple types and it makes no sense to store less
14681       // two bytes in byte-reversed order.
14682       EVT mVT = cast<StoreSDNode>(N)->getMemoryVT();
14683       if (mVT.isExtended() || mVT.getSizeInBits() < 16)
14684         break;
14685 
14686       SDValue BSwapOp = N->getOperand(1).getOperand(0);
14687       // Do an any-extend to 32-bits if this is a half-word input.
14688       if (BSwapOp.getValueType() == MVT::i16)
14689         BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
14690 
14691       // If the type of BSWAP operand is wider than stored memory width
14692       // it need to be shifted to the right side before STBRX.
14693       if (Op1VT.bitsGT(mVT)) {
14694         int Shift = Op1VT.getSizeInBits() - mVT.getSizeInBits();
14695         BSwapOp = DAG.getNode(ISD::SRL, dl, Op1VT, BSwapOp,
14696                               DAG.getConstant(Shift, dl, MVT::i32));
14697         // Need to truncate if this is a bswap of i64 stored as i32/i16.
14698         if (Op1VT == MVT::i64)
14699           BSwapOp = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, BSwapOp);
14700       }
14701 
14702       SDValue Ops[] = {
14703         N->getOperand(0), BSwapOp, N->getOperand(2), DAG.getValueType(mVT)
14704       };
14705       return
14706         DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
14707                                 Ops, cast<StoreSDNode>(N)->getMemoryVT(),
14708                                 cast<StoreSDNode>(N)->getMemOperand());
14709     }
14710 
14711     // STORE Constant:i32<0>  ->  STORE<trunc to i32> Constant:i64<0>
14712     // So it can increase the chance of CSE constant construction.
14713     if (Subtarget.isPPC64() && !DCI.isBeforeLegalize() &&
14714         isa<ConstantSDNode>(N->getOperand(1)) && Op1VT == MVT::i32) {
14715       // Need to sign-extended to 64-bits to handle negative values.
14716       EVT MemVT = cast<StoreSDNode>(N)->getMemoryVT();
14717       uint64_t Val64 = SignExtend64(N->getConstantOperandVal(1),
14718                                     MemVT.getSizeInBits());
14719       SDValue Const64 = DAG.getConstant(Val64, dl, MVT::i64);
14720 
14721       // DAG.getTruncStore() can't be used here because it doesn't accept
14722       // the general (base + offset) addressing mode.
14723       // So we use UpdateNodeOperands and setTruncatingStore instead.
14724       DAG.UpdateNodeOperands(N, N->getOperand(0), Const64, N->getOperand(2),
14725                              N->getOperand(3));
14726       cast<StoreSDNode>(N)->setTruncatingStore(true);
14727       return SDValue(N, 0);
14728     }
14729 
14730     // For little endian, VSX stores require generating xxswapd/lxvd2x.
14731     // Not needed on ISA 3.0 based CPUs since we have a non-permuting store.
14732     if (Op1VT.isSimple()) {
14733       MVT StoreVT = Op1VT.getSimpleVT();
14734       if (Subtarget.needsSwapsForVSXMemOps() &&
14735           (StoreVT == MVT::v2f64 || StoreVT == MVT::v2i64 ||
14736            StoreVT == MVT::v4f32 || StoreVT == MVT::v4i32))
14737         return expandVSXStoreForLE(N, DCI);
14738     }
14739     break;
14740   }
14741   case ISD::LOAD: {
14742     LoadSDNode *LD = cast<LoadSDNode>(N);
14743     EVT VT = LD->getValueType(0);
14744 
14745     // For little endian, VSX loads require generating lxvd2x/xxswapd.
14746     // Not needed on ISA 3.0 based CPUs since we have a non-permuting load.
14747     if (VT.isSimple()) {
14748       MVT LoadVT = VT.getSimpleVT();
14749       if (Subtarget.needsSwapsForVSXMemOps() &&
14750           (LoadVT == MVT::v2f64 || LoadVT == MVT::v2i64 ||
14751            LoadVT == MVT::v4f32 || LoadVT == MVT::v4i32))
14752         return expandVSXLoadForLE(N, DCI);
14753     }
14754 
14755     // We sometimes end up with a 64-bit integer load, from which we extract
14756     // two single-precision floating-point numbers. This happens with
14757     // std::complex<float>, and other similar structures, because of the way we
14758     // canonicalize structure copies. However, if we lack direct moves,
14759     // then the final bitcasts from the extracted integer values to the
14760     // floating-point numbers turn into store/load pairs. Even with direct moves,
14761     // just loading the two floating-point numbers is likely better.
14762     auto ReplaceTwoFloatLoad = [&]() {
14763       if (VT != MVT::i64)
14764         return false;
14765 
14766       if (LD->getExtensionType() != ISD::NON_EXTLOAD ||
14767           LD->isVolatile())
14768         return false;
14769 
14770       //  We're looking for a sequence like this:
14771       //  t13: i64,ch = load<LD8[%ref.tmp]> t0, t6, undef:i64
14772       //      t16: i64 = srl t13, Constant:i32<32>
14773       //    t17: i32 = truncate t16
14774       //  t18: f32 = bitcast t17
14775       //    t19: i32 = truncate t13
14776       //  t20: f32 = bitcast t19
14777 
14778       if (!LD->hasNUsesOfValue(2, 0))
14779         return false;
14780 
14781       auto UI = LD->use_begin();
14782       while (UI.getUse().getResNo() != 0) ++UI;
14783       SDNode *Trunc = *UI++;
14784       while (UI.getUse().getResNo() != 0) ++UI;
14785       SDNode *RightShift = *UI;
14786       if (Trunc->getOpcode() != ISD::TRUNCATE)
14787         std::swap(Trunc, RightShift);
14788 
14789       if (Trunc->getOpcode() != ISD::TRUNCATE ||
14790           Trunc->getValueType(0) != MVT::i32 ||
14791           !Trunc->hasOneUse())
14792         return false;
14793       if (RightShift->getOpcode() != ISD::SRL ||
14794           !isa<ConstantSDNode>(RightShift->getOperand(1)) ||
14795           RightShift->getConstantOperandVal(1) != 32 ||
14796           !RightShift->hasOneUse())
14797         return false;
14798 
14799       SDNode *Trunc2 = *RightShift->use_begin();
14800       if (Trunc2->getOpcode() != ISD::TRUNCATE ||
14801           Trunc2->getValueType(0) != MVT::i32 ||
14802           !Trunc2->hasOneUse())
14803         return false;
14804 
14805       SDNode *Bitcast = *Trunc->use_begin();
14806       SDNode *Bitcast2 = *Trunc2->use_begin();
14807 
14808       if (Bitcast->getOpcode() != ISD::BITCAST ||
14809           Bitcast->getValueType(0) != MVT::f32)
14810         return false;
14811       if (Bitcast2->getOpcode() != ISD::BITCAST ||
14812           Bitcast2->getValueType(0) != MVT::f32)
14813         return false;
14814 
14815       if (Subtarget.isLittleEndian())
14816         std::swap(Bitcast, Bitcast2);
14817 
14818       // Bitcast has the second float (in memory-layout order) and Bitcast2
14819       // has the first one.
14820 
14821       SDValue BasePtr = LD->getBasePtr();
14822       if (LD->isIndexed()) {
14823         assert(LD->getAddressingMode() == ISD::PRE_INC &&
14824                "Non-pre-inc AM on PPC?");
14825         BasePtr =
14826           DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
14827                       LD->getOffset());
14828       }
14829 
14830       auto MMOFlags =
14831           LD->getMemOperand()->getFlags() & ~MachineMemOperand::MOVolatile;
14832       SDValue FloatLoad = DAG.getLoad(MVT::f32, dl, LD->getChain(), BasePtr,
14833                                       LD->getPointerInfo(), LD->getAlignment(),
14834                                       MMOFlags, LD->getAAInfo());
14835       SDValue AddPtr =
14836         DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(),
14837                     BasePtr, DAG.getIntPtrConstant(4, dl));
14838       SDValue FloatLoad2 = DAG.getLoad(
14839           MVT::f32, dl, SDValue(FloatLoad.getNode(), 1), AddPtr,
14840           LD->getPointerInfo().getWithOffset(4),
14841           MinAlign(LD->getAlignment(), 4), MMOFlags, LD->getAAInfo());
14842 
14843       if (LD->isIndexed()) {
14844         // Note that DAGCombine should re-form any pre-increment load(s) from
14845         // what is produced here if that makes sense.
14846         DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), BasePtr);
14847       }
14848 
14849       DCI.CombineTo(Bitcast2, FloatLoad);
14850       DCI.CombineTo(Bitcast, FloatLoad2);
14851 
14852       DAG.ReplaceAllUsesOfValueWith(SDValue(LD, LD->isIndexed() ? 2 : 1),
14853                                     SDValue(FloatLoad2.getNode(), 1));
14854       return true;
14855     };
14856 
14857     if (ReplaceTwoFloatLoad())
14858       return SDValue(N, 0);
14859 
14860     EVT MemVT = LD->getMemoryVT();
14861     Type *Ty = MemVT.getTypeForEVT(*DAG.getContext());
14862     Align ABIAlignment = DAG.getDataLayout().getABITypeAlign(Ty);
14863     if (LD->isUnindexed() && VT.isVector() &&
14864         ((Subtarget.hasAltivec() && ISD::isNON_EXTLoad(N) &&
14865           // P8 and later hardware should just use LOAD.
14866           !Subtarget.hasP8Vector() &&
14867           (VT == MVT::v16i8 || VT == MVT::v8i16 || VT == MVT::v4i32 ||
14868            VT == MVT::v4f32))) &&
14869         LD->getAlign() < ABIAlignment) {
14870       // This is a type-legal unaligned Altivec load.
14871       SDValue Chain = LD->getChain();
14872       SDValue Ptr = LD->getBasePtr();
14873       bool isLittleEndian = Subtarget.isLittleEndian();
14874 
14875       // This implements the loading of unaligned vectors as described in
14876       // the venerable Apple Velocity Engine overview. Specifically:
14877       // https://developer.apple.com/hardwaredrivers/ve/alignment.html
14878       // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
14879       //
14880       // The general idea is to expand a sequence of one or more unaligned
14881       // loads into an alignment-based permutation-control instruction (lvsl
14882       // or lvsr), a series of regular vector loads (which always truncate
14883       // their input address to an aligned address), and a series of
14884       // permutations.  The results of these permutations are the requested
14885       // loaded values.  The trick is that the last "extra" load is not taken
14886       // from the address you might suspect (sizeof(vector) bytes after the
14887       // last requested load), but rather sizeof(vector) - 1 bytes after the
14888       // last requested vector. The point of this is to avoid a page fault if
14889       // the base address happened to be aligned. This works because if the
14890       // base address is aligned, then adding less than a full vector length
14891       // will cause the last vector in the sequence to be (re)loaded.
14892       // Otherwise, the next vector will be fetched as you might suspect was
14893       // necessary.
14894 
14895       // We might be able to reuse the permutation generation from
14896       // a different base address offset from this one by an aligned amount.
14897       // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
14898       // optimization later.
14899       Intrinsic::ID Intr, IntrLD, IntrPerm;
14900       MVT PermCntlTy, PermTy, LDTy;
14901       Intr = isLittleEndian ? Intrinsic::ppc_altivec_lvsr
14902                             : Intrinsic::ppc_altivec_lvsl;
14903       IntrLD = Intrinsic::ppc_altivec_lvx;
14904       IntrPerm = Intrinsic::ppc_altivec_vperm;
14905       PermCntlTy = MVT::v16i8;
14906       PermTy = MVT::v4i32;
14907       LDTy = MVT::v4i32;
14908 
14909       SDValue PermCntl = BuildIntrinsicOp(Intr, Ptr, DAG, dl, PermCntlTy);
14910 
14911       // Create the new MMO for the new base load. It is like the original MMO,
14912       // but represents an area in memory almost twice the vector size centered
14913       // on the original address. If the address is unaligned, we might start
14914       // reading up to (sizeof(vector)-1) bytes below the address of the
14915       // original unaligned load.
14916       MachineFunction &MF = DAG.getMachineFunction();
14917       MachineMemOperand *BaseMMO =
14918         MF.getMachineMemOperand(LD->getMemOperand(),
14919                                 -(long)MemVT.getStoreSize()+1,
14920                                 2*MemVT.getStoreSize()-1);
14921 
14922       // Create the new base load.
14923       SDValue LDXIntID =
14924           DAG.getTargetConstant(IntrLD, dl, getPointerTy(MF.getDataLayout()));
14925       SDValue BaseLoadOps[] = { Chain, LDXIntID, Ptr };
14926       SDValue BaseLoad =
14927         DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
14928                                 DAG.getVTList(PermTy, MVT::Other),
14929                                 BaseLoadOps, LDTy, BaseMMO);
14930 
14931       // Note that the value of IncOffset (which is provided to the next
14932       // load's pointer info offset value, and thus used to calculate the
14933       // alignment), and the value of IncValue (which is actually used to
14934       // increment the pointer value) are different! This is because we
14935       // require the next load to appear to be aligned, even though it
14936       // is actually offset from the base pointer by a lesser amount.
14937       int IncOffset = VT.getSizeInBits() / 8;
14938       int IncValue = IncOffset;
14939 
14940       // Walk (both up and down) the chain looking for another load at the real
14941       // (aligned) offset (the alignment of the other load does not matter in
14942       // this case). If found, then do not use the offset reduction trick, as
14943       // that will prevent the loads from being later combined (as they would
14944       // otherwise be duplicates).
14945       if (!findConsecutiveLoad(LD, DAG))
14946         --IncValue;
14947 
14948       SDValue Increment =
14949           DAG.getConstant(IncValue, dl, getPointerTy(MF.getDataLayout()));
14950       Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14951 
14952       MachineMemOperand *ExtraMMO =
14953         MF.getMachineMemOperand(LD->getMemOperand(),
14954                                 1, 2*MemVT.getStoreSize()-1);
14955       SDValue ExtraLoadOps[] = { Chain, LDXIntID, Ptr };
14956       SDValue ExtraLoad =
14957         DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
14958                                 DAG.getVTList(PermTy, MVT::Other),
14959                                 ExtraLoadOps, LDTy, ExtraMMO);
14960 
14961       SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
14962         BaseLoad.getValue(1), ExtraLoad.getValue(1));
14963 
14964       // Because vperm has a big-endian bias, we must reverse the order
14965       // of the input vectors and complement the permute control vector
14966       // when generating little endian code.  We have already handled the
14967       // latter by using lvsr instead of lvsl, so just reverse BaseLoad
14968       // and ExtraLoad here.
14969       SDValue Perm;
14970       if (isLittleEndian)
14971         Perm = BuildIntrinsicOp(IntrPerm,
14972                                 ExtraLoad, BaseLoad, PermCntl, DAG, dl);
14973       else
14974         Perm = BuildIntrinsicOp(IntrPerm,
14975                                 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
14976 
14977       if (VT != PermTy)
14978         Perm = Subtarget.hasAltivec()
14979                    ? DAG.getNode(ISD::BITCAST, dl, VT, Perm)
14980                    : DAG.getNode(ISD::FP_ROUND, dl, VT, Perm,
14981                                  DAG.getTargetConstant(1, dl, MVT::i64));
14982                                // second argument is 1 because this rounding
14983                                // is always exact.
14984 
14985       // The output of the permutation is our loaded result, the TokenFactor is
14986       // our new chain.
14987       DCI.CombineTo(N, Perm, TF);
14988       return SDValue(N, 0);
14989     }
14990     }
14991     break;
14992     case ISD::INTRINSIC_WO_CHAIN: {
14993       bool isLittleEndian = Subtarget.isLittleEndian();
14994       unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
14995       Intrinsic::ID Intr = (isLittleEndian ? Intrinsic::ppc_altivec_lvsr
14996                                            : Intrinsic::ppc_altivec_lvsl);
14997       if (IID == Intr && N->getOperand(1)->getOpcode() == ISD::ADD) {
14998         SDValue Add = N->getOperand(1);
14999 
15000         int Bits = 4 /* 16 byte alignment */;
15001 
15002         if (DAG.MaskedValueIsZero(Add->getOperand(1),
15003                                   APInt::getAllOnesValue(Bits /* alignment */)
15004                                       .zext(Add.getScalarValueSizeInBits()))) {
15005           SDNode *BasePtr = Add->getOperand(0).getNode();
15006           for (SDNode::use_iterator UI = BasePtr->use_begin(),
15007                                     UE = BasePtr->use_end();
15008                UI != UE; ++UI) {
15009             if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
15010                 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() ==
15011                     IID) {
15012               // We've found another LVSL/LVSR, and this address is an aligned
15013               // multiple of that one. The results will be the same, so use the
15014               // one we've just found instead.
15015 
15016               return SDValue(*UI, 0);
15017             }
15018           }
15019         }
15020 
15021         if (isa<ConstantSDNode>(Add->getOperand(1))) {
15022           SDNode *BasePtr = Add->getOperand(0).getNode();
15023           for (SDNode::use_iterator UI = BasePtr->use_begin(),
15024                UE = BasePtr->use_end(); UI != UE; ++UI) {
15025             if (UI->getOpcode() == ISD::ADD &&
15026                 isa<ConstantSDNode>(UI->getOperand(1)) &&
15027                 (cast<ConstantSDNode>(Add->getOperand(1))->getZExtValue() -
15028                  cast<ConstantSDNode>(UI->getOperand(1))->getZExtValue()) %
15029                 (1ULL << Bits) == 0) {
15030               SDNode *OtherAdd = *UI;
15031               for (SDNode::use_iterator VI = OtherAdd->use_begin(),
15032                    VE = OtherAdd->use_end(); VI != VE; ++VI) {
15033                 if (VI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
15034                     cast<ConstantSDNode>(VI->getOperand(0))->getZExtValue() == IID) {
15035                   return SDValue(*VI, 0);
15036                 }
15037               }
15038             }
15039           }
15040         }
15041       }
15042 
15043       // Combine vmaxsw/h/b(a, a's negation) to abs(a)
15044       // Expose the vabsduw/h/b opportunity for down stream
15045       if (!DCI.isAfterLegalizeDAG() && Subtarget.hasP9Altivec() &&
15046           (IID == Intrinsic::ppc_altivec_vmaxsw ||
15047            IID == Intrinsic::ppc_altivec_vmaxsh ||
15048            IID == Intrinsic::ppc_altivec_vmaxsb)) {
15049         SDValue V1 = N->getOperand(1);
15050         SDValue V2 = N->getOperand(2);
15051         if ((V1.getSimpleValueType() == MVT::v4i32 ||
15052              V1.getSimpleValueType() == MVT::v8i16 ||
15053              V1.getSimpleValueType() == MVT::v16i8) &&
15054             V1.getSimpleValueType() == V2.getSimpleValueType()) {
15055           // (0-a, a)
15056           if (V1.getOpcode() == ISD::SUB &&
15057               ISD::isBuildVectorAllZeros(V1.getOperand(0).getNode()) &&
15058               V1.getOperand(1) == V2) {
15059             return DAG.getNode(ISD::ABS, dl, V2.getValueType(), V2);
15060           }
15061           // (a, 0-a)
15062           if (V2.getOpcode() == ISD::SUB &&
15063               ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()) &&
15064               V2.getOperand(1) == V1) {
15065             return DAG.getNode(ISD::ABS, dl, V1.getValueType(), V1);
15066           }
15067           // (x-y, y-x)
15068           if (V1.getOpcode() == ISD::SUB && V2.getOpcode() == ISD::SUB &&
15069               V1.getOperand(0) == V2.getOperand(1) &&
15070               V1.getOperand(1) == V2.getOperand(0)) {
15071             return DAG.getNode(ISD::ABS, dl, V1.getValueType(), V1);
15072           }
15073         }
15074       }
15075     }
15076 
15077     break;
15078   case ISD::INTRINSIC_W_CHAIN:
15079     // For little endian, VSX loads require generating lxvd2x/xxswapd.
15080     // Not needed on ISA 3.0 based CPUs since we have a non-permuting load.
15081     if (Subtarget.needsSwapsForVSXMemOps()) {
15082       switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
15083       default:
15084         break;
15085       case Intrinsic::ppc_vsx_lxvw4x:
15086       case Intrinsic::ppc_vsx_lxvd2x:
15087         return expandVSXLoadForLE(N, DCI);
15088       }
15089     }
15090     break;
15091   case ISD::INTRINSIC_VOID:
15092     // For little endian, VSX stores require generating xxswapd/stxvd2x.
15093     // Not needed on ISA 3.0 based CPUs since we have a non-permuting store.
15094     if (Subtarget.needsSwapsForVSXMemOps()) {
15095       switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
15096       default:
15097         break;
15098       case Intrinsic::ppc_vsx_stxvw4x:
15099       case Intrinsic::ppc_vsx_stxvd2x:
15100         return expandVSXStoreForLE(N, DCI);
15101       }
15102     }
15103     break;
15104   case ISD::BSWAP:
15105     // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
15106     if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
15107         N->getOperand(0).hasOneUse() &&
15108         (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
15109          (Subtarget.hasLDBRX() && Subtarget.isPPC64() &&
15110           N->getValueType(0) == MVT::i64))) {
15111       SDValue Load = N->getOperand(0);
15112       LoadSDNode *LD = cast<LoadSDNode>(Load);
15113       // Create the byte-swapping load.
15114       SDValue Ops[] = {
15115         LD->getChain(),    // Chain
15116         LD->getBasePtr(),  // Ptr
15117         DAG.getValueType(N->getValueType(0)) // VT
15118       };
15119       SDValue BSLoad =
15120         DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
15121                                 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
15122                                               MVT::i64 : MVT::i32, MVT::Other),
15123                                 Ops, LD->getMemoryVT(), LD->getMemOperand());
15124 
15125       // If this is an i16 load, insert the truncate.
15126       SDValue ResVal = BSLoad;
15127       if (N->getValueType(0) == MVT::i16)
15128         ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
15129 
15130       // First, combine the bswap away.  This makes the value produced by the
15131       // load dead.
15132       DCI.CombineTo(N, ResVal);
15133 
15134       // Next, combine the load away, we give it a bogus result value but a real
15135       // chain result.  The result value is dead because the bswap is dead.
15136       DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
15137 
15138       // Return N so it doesn't get rechecked!
15139       return SDValue(N, 0);
15140     }
15141     break;
15142   case PPCISD::VCMP:
15143     // If a VCMP_rec node already exists with exactly the same operands as this
15144     // node, use its result instead of this node (VCMP_rec computes both a CR6
15145     // and a normal output).
15146     //
15147     if (!N->getOperand(0).hasOneUse() &&
15148         !N->getOperand(1).hasOneUse() &&
15149         !N->getOperand(2).hasOneUse()) {
15150 
15151       // Scan all of the users of the LHS, looking for VCMP_rec's that match.
15152       SDNode *VCMPrecNode = nullptr;
15153 
15154       SDNode *LHSN = N->getOperand(0).getNode();
15155       for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
15156            UI != E; ++UI)
15157         if (UI->getOpcode() == PPCISD::VCMP_rec &&
15158             UI->getOperand(1) == N->getOperand(1) &&
15159             UI->getOperand(2) == N->getOperand(2) &&
15160             UI->getOperand(0) == N->getOperand(0)) {
15161           VCMPrecNode = *UI;
15162           break;
15163         }
15164 
15165       // If there is no VCMP_rec node, or if the flag value has a single use,
15166       // don't transform this.
15167       if (!VCMPrecNode || VCMPrecNode->hasNUsesOfValue(0, 1))
15168         break;
15169 
15170       // Look at the (necessarily single) use of the flag value.  If it has a
15171       // chain, this transformation is more complex.  Note that multiple things
15172       // could use the value result, which we should ignore.
15173       SDNode *FlagUser = nullptr;
15174       for (SDNode::use_iterator UI = VCMPrecNode->use_begin();
15175            FlagUser == nullptr; ++UI) {
15176         assert(UI != VCMPrecNode->use_end() && "Didn't find user!");
15177         SDNode *User = *UI;
15178         for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
15179           if (User->getOperand(i) == SDValue(VCMPrecNode, 1)) {
15180             FlagUser = User;
15181             break;
15182           }
15183         }
15184       }
15185 
15186       // If the user is a MFOCRF instruction, we know this is safe.
15187       // Otherwise we give up for right now.
15188       if (FlagUser->getOpcode() == PPCISD::MFOCRF)
15189         return SDValue(VCMPrecNode, 0);
15190     }
15191     break;
15192   case ISD::BRCOND: {
15193     SDValue Cond = N->getOperand(1);
15194     SDValue Target = N->getOperand(2);
15195 
15196     if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
15197         cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() ==
15198           Intrinsic::loop_decrement) {
15199 
15200       // We now need to make the intrinsic dead (it cannot be instruction
15201       // selected).
15202       DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0));
15203       assert(Cond.getNode()->hasOneUse() &&
15204              "Counter decrement has more than one use");
15205 
15206       return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other,
15207                          N->getOperand(0), Target);
15208     }
15209   }
15210   break;
15211   case ISD::BR_CC: {
15212     // If this is a branch on an altivec predicate comparison, lower this so
15213     // that we don't have to do a MFOCRF: instead, branch directly on CR6.  This
15214     // lowering is done pre-legalize, because the legalizer lowers the predicate
15215     // compare down to code that is difficult to reassemble.
15216     ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
15217     SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
15218 
15219     // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
15220     // value. If so, pass-through the AND to get to the intrinsic.
15221     if (LHS.getOpcode() == ISD::AND &&
15222         LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
15223         cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
15224           Intrinsic::loop_decrement &&
15225         isa<ConstantSDNode>(LHS.getOperand(1)) &&
15226         !isNullConstant(LHS.getOperand(1)))
15227       LHS = LHS.getOperand(0);
15228 
15229     if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
15230         cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
15231           Intrinsic::loop_decrement &&
15232         isa<ConstantSDNode>(RHS)) {
15233       assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
15234              "Counter decrement comparison is not EQ or NE");
15235 
15236       unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
15237       bool isBDNZ = (CC == ISD::SETEQ && Val) ||
15238                     (CC == ISD::SETNE && !Val);
15239 
15240       // We now need to make the intrinsic dead (it cannot be instruction
15241       // selected).
15242       DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
15243       assert(LHS.getNode()->hasOneUse() &&
15244              "Counter decrement has more than one use");
15245 
15246       return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
15247                          N->getOperand(0), N->getOperand(4));
15248     }
15249 
15250     int CompareOpc;
15251     bool isDot;
15252 
15253     if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
15254         isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
15255         getVectorCompareInfo(LHS, CompareOpc, isDot, Subtarget)) {
15256       assert(isDot && "Can't compare against a vector result!");
15257 
15258       // If this is a comparison against something other than 0/1, then we know
15259       // that the condition is never/always true.
15260       unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
15261       if (Val != 0 && Val != 1) {
15262         if (CC == ISD::SETEQ)      // Cond never true, remove branch.
15263           return N->getOperand(0);
15264         // Always !=, turn it into an unconditional branch.
15265         return DAG.getNode(ISD::BR, dl, MVT::Other,
15266                            N->getOperand(0), N->getOperand(4));
15267       }
15268 
15269       bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
15270 
15271       // Create the PPCISD altivec 'dot' comparison node.
15272       SDValue Ops[] = {
15273         LHS.getOperand(2),  // LHS of compare
15274         LHS.getOperand(3),  // RHS of compare
15275         DAG.getConstant(CompareOpc, dl, MVT::i32)
15276       };
15277       EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
15278       SDValue CompNode = DAG.getNode(PPCISD::VCMP_rec, dl, VTs, Ops);
15279 
15280       // Unpack the result based on how the target uses it.
15281       PPC::Predicate CompOpc;
15282       switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
15283       default:  // Can't happen, don't crash on invalid number though.
15284       case 0:   // Branch on the value of the EQ bit of CR6.
15285         CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
15286         break;
15287       case 1:   // Branch on the inverted value of the EQ bit of CR6.
15288         CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
15289         break;
15290       case 2:   // Branch on the value of the LT bit of CR6.
15291         CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
15292         break;
15293       case 3:   // Branch on the inverted value of the LT bit of CR6.
15294         CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
15295         break;
15296       }
15297 
15298       return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
15299                          DAG.getConstant(CompOpc, dl, MVT::i32),
15300                          DAG.getRegister(PPC::CR6, MVT::i32),
15301                          N->getOperand(4), CompNode.getValue(1));
15302     }
15303     break;
15304   }
15305   case ISD::BUILD_VECTOR:
15306     return DAGCombineBuildVector(N, DCI);
15307   case ISD::ABS:
15308     return combineABS(N, DCI);
15309   case ISD::VSELECT:
15310     return combineVSelect(N, DCI);
15311   }
15312 
15313   return SDValue();
15314 }
15315 
15316 SDValue
15317 PPCTargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor,
15318                                  SelectionDAG &DAG,
15319                                  SmallVectorImpl<SDNode *> &Created) const {
15320   // fold (sdiv X, pow2)
15321   EVT VT = N->getValueType(0);
15322   if (VT == MVT::i64 && !Subtarget.isPPC64())
15323     return SDValue();
15324   if ((VT != MVT::i32 && VT != MVT::i64) ||
15325       !(Divisor.isPowerOf2() || (-Divisor).isPowerOf2()))
15326     return SDValue();
15327 
15328   SDLoc DL(N);
15329   SDValue N0 = N->getOperand(0);
15330 
15331   bool IsNegPow2 = (-Divisor).isPowerOf2();
15332   unsigned Lg2 = (IsNegPow2 ? -Divisor : Divisor).countTrailingZeros();
15333   SDValue ShiftAmt = DAG.getConstant(Lg2, DL, VT);
15334 
15335   SDValue Op = DAG.getNode(PPCISD::SRA_ADDZE, DL, VT, N0, ShiftAmt);
15336   Created.push_back(Op.getNode());
15337 
15338   if (IsNegPow2) {
15339     Op = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Op);
15340     Created.push_back(Op.getNode());
15341   }
15342 
15343   return Op;
15344 }
15345 
15346 //===----------------------------------------------------------------------===//
15347 // Inline Assembly Support
15348 //===----------------------------------------------------------------------===//
15349 
15350 void PPCTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
15351                                                       KnownBits &Known,
15352                                                       const APInt &DemandedElts,
15353                                                       const SelectionDAG &DAG,
15354                                                       unsigned Depth) const {
15355   Known.resetAll();
15356   switch (Op.getOpcode()) {
15357   default: break;
15358   case PPCISD::LBRX: {
15359     // lhbrx is known to have the top bits cleared out.
15360     if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
15361       Known.Zero = 0xFFFF0000;
15362     break;
15363   }
15364   case ISD::INTRINSIC_WO_CHAIN: {
15365     switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
15366     default: break;
15367     case Intrinsic::ppc_altivec_vcmpbfp_p:
15368     case Intrinsic::ppc_altivec_vcmpeqfp_p:
15369     case Intrinsic::ppc_altivec_vcmpequb_p:
15370     case Intrinsic::ppc_altivec_vcmpequh_p:
15371     case Intrinsic::ppc_altivec_vcmpequw_p:
15372     case Intrinsic::ppc_altivec_vcmpequd_p:
15373     case Intrinsic::ppc_altivec_vcmpequq_p:
15374     case Intrinsic::ppc_altivec_vcmpgefp_p:
15375     case Intrinsic::ppc_altivec_vcmpgtfp_p:
15376     case Intrinsic::ppc_altivec_vcmpgtsb_p:
15377     case Intrinsic::ppc_altivec_vcmpgtsh_p:
15378     case Intrinsic::ppc_altivec_vcmpgtsw_p:
15379     case Intrinsic::ppc_altivec_vcmpgtsd_p:
15380     case Intrinsic::ppc_altivec_vcmpgtsq_p:
15381     case Intrinsic::ppc_altivec_vcmpgtub_p:
15382     case Intrinsic::ppc_altivec_vcmpgtuh_p:
15383     case Intrinsic::ppc_altivec_vcmpgtuw_p:
15384     case Intrinsic::ppc_altivec_vcmpgtud_p:
15385     case Intrinsic::ppc_altivec_vcmpgtuq_p:
15386       Known.Zero = ~1U;  // All bits but the low one are known to be zero.
15387       break;
15388     }
15389   }
15390   }
15391 }
15392 
15393 Align PPCTargetLowering::getPrefLoopAlignment(MachineLoop *ML) const {
15394   switch (Subtarget.getCPUDirective()) {
15395   default: break;
15396   case PPC::DIR_970:
15397   case PPC::DIR_PWR4:
15398   case PPC::DIR_PWR5:
15399   case PPC::DIR_PWR5X:
15400   case PPC::DIR_PWR6:
15401   case PPC::DIR_PWR6X:
15402   case PPC::DIR_PWR7:
15403   case PPC::DIR_PWR8:
15404   case PPC::DIR_PWR9:
15405   case PPC::DIR_PWR10:
15406   case PPC::DIR_PWR_FUTURE: {
15407     if (!ML)
15408       break;
15409 
15410     if (!DisableInnermostLoopAlign32) {
15411       // If the nested loop is an innermost loop, prefer to a 32-byte alignment,
15412       // so that we can decrease cache misses and branch-prediction misses.
15413       // Actual alignment of the loop will depend on the hotness check and other
15414       // logic in alignBlocks.
15415       if (ML->getLoopDepth() > 1 && ML->getSubLoops().empty())
15416         return Align(32);
15417     }
15418 
15419     const PPCInstrInfo *TII = Subtarget.getInstrInfo();
15420 
15421     // For small loops (between 5 and 8 instructions), align to a 32-byte
15422     // boundary so that the entire loop fits in one instruction-cache line.
15423     uint64_t LoopSize = 0;
15424     for (auto I = ML->block_begin(), IE = ML->block_end(); I != IE; ++I)
15425       for (auto J = (*I)->begin(), JE = (*I)->end(); J != JE; ++J) {
15426         LoopSize += TII->getInstSizeInBytes(*J);
15427         if (LoopSize > 32)
15428           break;
15429       }
15430 
15431     if (LoopSize > 16 && LoopSize <= 32)
15432       return Align(32);
15433 
15434     break;
15435   }
15436   }
15437 
15438   return TargetLowering::getPrefLoopAlignment(ML);
15439 }
15440 
15441 /// getConstraintType - Given a constraint, return the type of
15442 /// constraint it is for this target.
15443 PPCTargetLowering::ConstraintType
15444 PPCTargetLowering::getConstraintType(StringRef Constraint) const {
15445   if (Constraint.size() == 1) {
15446     switch (Constraint[0]) {
15447     default: break;
15448     case 'b':
15449     case 'r':
15450     case 'f':
15451     case 'd':
15452     case 'v':
15453     case 'y':
15454       return C_RegisterClass;
15455     case 'Z':
15456       // FIXME: While Z does indicate a memory constraint, it specifically
15457       // indicates an r+r address (used in conjunction with the 'y' modifier
15458       // in the replacement string). Currently, we're forcing the base
15459       // register to be r0 in the asm printer (which is interpreted as zero)
15460       // and forming the complete address in the second register. This is
15461       // suboptimal.
15462       return C_Memory;
15463     }
15464   } else if (Constraint == "wc") { // individual CR bits.
15465     return C_RegisterClass;
15466   } else if (Constraint == "wa" || Constraint == "wd" ||
15467              Constraint == "wf" || Constraint == "ws" ||
15468              Constraint == "wi" || Constraint == "ww") {
15469     return C_RegisterClass; // VSX registers.
15470   }
15471   return TargetLowering::getConstraintType(Constraint);
15472 }
15473 
15474 /// Examine constraint type and operand type and determine a weight value.
15475 /// This object must already have been set up with the operand type
15476 /// and the current alternative constraint selected.
15477 TargetLowering::ConstraintWeight
15478 PPCTargetLowering::getSingleConstraintMatchWeight(
15479     AsmOperandInfo &info, const char *constraint) const {
15480   ConstraintWeight weight = CW_Invalid;
15481   Value *CallOperandVal = info.CallOperandVal;
15482     // If we don't have a value, we can't do a match,
15483     // but allow it at the lowest weight.
15484   if (!CallOperandVal)
15485     return CW_Default;
15486   Type *type = CallOperandVal->getType();
15487 
15488   // Look at the constraint type.
15489   if (StringRef(constraint) == "wc" && type->isIntegerTy(1))
15490     return CW_Register; // an individual CR bit.
15491   else if ((StringRef(constraint) == "wa" ||
15492             StringRef(constraint) == "wd" ||
15493             StringRef(constraint) == "wf") &&
15494            type->isVectorTy())
15495     return CW_Register;
15496   else if (StringRef(constraint) == "wi" && type->isIntegerTy(64))
15497     return CW_Register; // just hold 64-bit integers data.
15498   else if (StringRef(constraint) == "ws" && type->isDoubleTy())
15499     return CW_Register;
15500   else if (StringRef(constraint) == "ww" && type->isFloatTy())
15501     return CW_Register;
15502 
15503   switch (*constraint) {
15504   default:
15505     weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
15506     break;
15507   case 'b':
15508     if (type->isIntegerTy())
15509       weight = CW_Register;
15510     break;
15511   case 'f':
15512     if (type->isFloatTy())
15513       weight = CW_Register;
15514     break;
15515   case 'd':
15516     if (type->isDoubleTy())
15517       weight = CW_Register;
15518     break;
15519   case 'v':
15520     if (type->isVectorTy())
15521       weight = CW_Register;
15522     break;
15523   case 'y':
15524     weight = CW_Register;
15525     break;
15526   case 'Z':
15527     weight = CW_Memory;
15528     break;
15529   }
15530   return weight;
15531 }
15532 
15533 std::pair<unsigned, const TargetRegisterClass *>
15534 PPCTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
15535                                                 StringRef Constraint,
15536                                                 MVT VT) const {
15537   if (Constraint.size() == 1) {
15538     // GCC RS6000 Constraint Letters
15539     switch (Constraint[0]) {
15540     case 'b':   // R1-R31
15541       if (VT == MVT::i64 && Subtarget.isPPC64())
15542         return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
15543       return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
15544     case 'r':   // R0-R31
15545       if (VT == MVT::i64 && Subtarget.isPPC64())
15546         return std::make_pair(0U, &PPC::G8RCRegClass);
15547       return std::make_pair(0U, &PPC::GPRCRegClass);
15548     // 'd' and 'f' constraints are both defined to be "the floating point
15549     // registers", where one is for 32-bit and the other for 64-bit. We don't
15550     // really care overly much here so just give them all the same reg classes.
15551     case 'd':
15552     case 'f':
15553       if (Subtarget.hasSPE()) {
15554         if (VT == MVT::f32 || VT == MVT::i32)
15555           return std::make_pair(0U, &PPC::GPRCRegClass);
15556         if (VT == MVT::f64 || VT == MVT::i64)
15557           return std::make_pair(0U, &PPC::SPERCRegClass);
15558       } else {
15559         if (VT == MVT::f32 || VT == MVT::i32)
15560           return std::make_pair(0U, &PPC::F4RCRegClass);
15561         if (VT == MVT::f64 || VT == MVT::i64)
15562           return std::make_pair(0U, &PPC::F8RCRegClass);
15563       }
15564       break;
15565     case 'v':
15566       if (Subtarget.hasAltivec())
15567         return std::make_pair(0U, &PPC::VRRCRegClass);
15568       break;
15569     case 'y':   // crrc
15570       return std::make_pair(0U, &PPC::CRRCRegClass);
15571     }
15572   } else if (Constraint == "wc" && Subtarget.useCRBits()) {
15573     // An individual CR bit.
15574     return std::make_pair(0U, &PPC::CRBITRCRegClass);
15575   } else if ((Constraint == "wa" || Constraint == "wd" ||
15576              Constraint == "wf" || Constraint == "wi") &&
15577              Subtarget.hasVSX()) {
15578     return std::make_pair(0U, &PPC::VSRCRegClass);
15579   } else if ((Constraint == "ws" || Constraint == "ww") && Subtarget.hasVSX()) {
15580     if (VT == MVT::f32 && Subtarget.hasP8Vector())
15581       return std::make_pair(0U, &PPC::VSSRCRegClass);
15582     else
15583       return std::make_pair(0U, &PPC::VSFRCRegClass);
15584   }
15585 
15586   // Handle special cases of physical registers that are not properly handled
15587   // by the base class.
15588   if (Constraint[0] == '{' && Constraint[Constraint.size() - 1] == '}') {
15589     // If we name a VSX register, we can't defer to the base class because it
15590     // will not recognize the correct register (their names will be VSL{0-31}
15591     // and V{0-31} so they won't match). So we match them here.
15592     if (Constraint.size() > 3 && Constraint[1] == 'v' && Constraint[2] == 's') {
15593       int VSNum = atoi(Constraint.data() + 3);
15594       assert(VSNum >= 0 && VSNum <= 63 &&
15595              "Attempted to access a vsr out of range");
15596       if (VSNum < 32)
15597         return std::make_pair(PPC::VSL0 + VSNum, &PPC::VSRCRegClass);
15598       return std::make_pair(PPC::V0 + VSNum - 32, &PPC::VSRCRegClass);
15599     }
15600 
15601     // For float registers, we can't defer to the base class as it will match
15602     // the SPILLTOVSRRC class.
15603     if (Constraint.size() > 3 && Constraint[1] == 'f') {
15604       int RegNum = atoi(Constraint.data() + 2);
15605       if (RegNum > 31 || RegNum < 0)
15606         report_fatal_error("Invalid floating point register number");
15607       if (VT == MVT::f32 || VT == MVT::i32)
15608         return Subtarget.hasSPE()
15609                    ? std::make_pair(PPC::R0 + RegNum, &PPC::GPRCRegClass)
15610                    : std::make_pair(PPC::F0 + RegNum, &PPC::F4RCRegClass);
15611       if (VT == MVT::f64 || VT == MVT::i64)
15612         return Subtarget.hasSPE()
15613                    ? std::make_pair(PPC::S0 + RegNum, &PPC::SPERCRegClass)
15614                    : std::make_pair(PPC::F0 + RegNum, &PPC::F8RCRegClass);
15615     }
15616   }
15617 
15618   std::pair<unsigned, const TargetRegisterClass *> R =
15619       TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
15620 
15621   // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
15622   // (which we call X[0-9]+). If a 64-bit value has been requested, and a
15623   // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
15624   // register.
15625   // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
15626   // the AsmName field from *RegisterInfo.td, then this would not be necessary.
15627   if (R.first && VT == MVT::i64 && Subtarget.isPPC64() &&
15628       PPC::GPRCRegClass.contains(R.first))
15629     return std::make_pair(TRI->getMatchingSuperReg(R.first,
15630                             PPC::sub_32, &PPC::G8RCRegClass),
15631                           &PPC::G8RCRegClass);
15632 
15633   // GCC accepts 'cc' as an alias for 'cr0', and we need to do the same.
15634   if (!R.second && StringRef("{cc}").equals_lower(Constraint)) {
15635     R.first = PPC::CR0;
15636     R.second = &PPC::CRRCRegClass;
15637   }
15638   // FIXME: This warning should ideally be emitted in the front end.
15639   const auto &TM = getTargetMachine();
15640   if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI()) {
15641     if (((R.first >= PPC::V20 && R.first <= PPC::V31) ||
15642          (R.first >= PPC::VF20 && R.first <= PPC::VF31)) &&
15643         (R.second == &PPC::VSRCRegClass || R.second == &PPC::VSFRCRegClass))
15644       errs() << "warning: vector registers 20 to 32 are reserved in the "
15645                 "default AIX AltiVec ABI and cannot be used\n";
15646   }
15647 
15648   return R;
15649 }
15650 
15651 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
15652 /// vector.  If it is invalid, don't add anything to Ops.
15653 void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
15654                                                      std::string &Constraint,
15655                                                      std::vector<SDValue>&Ops,
15656                                                      SelectionDAG &DAG) const {
15657   SDValue Result;
15658 
15659   // Only support length 1 constraints.
15660   if (Constraint.length() > 1) return;
15661 
15662   char Letter = Constraint[0];
15663   switch (Letter) {
15664   default: break;
15665   case 'I':
15666   case 'J':
15667   case 'K':
15668   case 'L':
15669   case 'M':
15670   case 'N':
15671   case 'O':
15672   case 'P': {
15673     ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
15674     if (!CST) return; // Must be an immediate to match.
15675     SDLoc dl(Op);
15676     int64_t Value = CST->getSExtValue();
15677     EVT TCVT = MVT::i64; // All constants taken to be 64 bits so that negative
15678                          // numbers are printed as such.
15679     switch (Letter) {
15680     default: llvm_unreachable("Unknown constraint letter!");
15681     case 'I':  // "I" is a signed 16-bit constant.
15682       if (isInt<16>(Value))
15683         Result = DAG.getTargetConstant(Value, dl, TCVT);
15684       break;
15685     case 'J':  // "J" is a constant with only the high-order 16 bits nonzero.
15686       if (isShiftedUInt<16, 16>(Value))
15687         Result = DAG.getTargetConstant(Value, dl, TCVT);
15688       break;
15689     case 'L':  // "L" is a signed 16-bit constant shifted left 16 bits.
15690       if (isShiftedInt<16, 16>(Value))
15691         Result = DAG.getTargetConstant(Value, dl, TCVT);
15692       break;
15693     case 'K':  // "K" is a constant with only the low-order 16 bits nonzero.
15694       if (isUInt<16>(Value))
15695         Result = DAG.getTargetConstant(Value, dl, TCVT);
15696       break;
15697     case 'M':  // "M" is a constant that is greater than 31.
15698       if (Value > 31)
15699         Result = DAG.getTargetConstant(Value, dl, TCVT);
15700       break;
15701     case 'N':  // "N" is a positive constant that is an exact power of two.
15702       if (Value > 0 && isPowerOf2_64(Value))
15703         Result = DAG.getTargetConstant(Value, dl, TCVT);
15704       break;
15705     case 'O':  // "O" is the constant zero.
15706       if (Value == 0)
15707         Result = DAG.getTargetConstant(Value, dl, TCVT);
15708       break;
15709     case 'P':  // "P" is a constant whose negation is a signed 16-bit constant.
15710       if (isInt<16>(-Value))
15711         Result = DAG.getTargetConstant(Value, dl, TCVT);
15712       break;
15713     }
15714     break;
15715   }
15716   }
15717 
15718   if (Result.getNode()) {
15719     Ops.push_back(Result);
15720     return;
15721   }
15722 
15723   // Handle standard constraint letters.
15724   TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
15725 }
15726 
15727 // isLegalAddressingMode - Return true if the addressing mode represented
15728 // by AM is legal for this target, for a load/store of the specified type.
15729 bool PPCTargetLowering::isLegalAddressingMode(const DataLayout &DL,
15730                                               const AddrMode &AM, Type *Ty,
15731                                               unsigned AS,
15732                                               Instruction *I) const {
15733   // Vector type r+i form is supported since power9 as DQ form. We don't check
15734   // the offset matching DQ form requirement(off % 16 == 0), because on PowerPC,
15735   // imm form is preferred and the offset can be adjusted to use imm form later
15736   // in pass PPCLoopInstrFormPrep. Also in LSR, for one LSRUse, it uses min and
15737   // max offset to check legal addressing mode, we should be a little aggressive
15738   // to contain other offsets for that LSRUse.
15739   if (Ty->isVectorTy() && AM.BaseOffs != 0 && !Subtarget.hasP9Vector())
15740     return false;
15741 
15742   // PPC allows a sign-extended 16-bit immediate field.
15743   if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
15744     return false;
15745 
15746   // No global is ever allowed as a base.
15747   if (AM.BaseGV)
15748     return false;
15749 
15750   // PPC only support r+r,
15751   switch (AM.Scale) {
15752   case 0:  // "r+i" or just "i", depending on HasBaseReg.
15753     break;
15754   case 1:
15755     if (AM.HasBaseReg && AM.BaseOffs)  // "r+r+i" is not allowed.
15756       return false;
15757     // Otherwise we have r+r or r+i.
15758     break;
15759   case 2:
15760     if (AM.HasBaseReg || AM.BaseOffs)  // 2*r+r  or  2*r+i is not allowed.
15761       return false;
15762     // Allow 2*r as r+r.
15763     break;
15764   default:
15765     // No other scales are supported.
15766     return false;
15767   }
15768 
15769   return true;
15770 }
15771 
15772 SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
15773                                            SelectionDAG &DAG) const {
15774   MachineFunction &MF = DAG.getMachineFunction();
15775   MachineFrameInfo &MFI = MF.getFrameInfo();
15776   MFI.setReturnAddressIsTaken(true);
15777 
15778   if (verifyReturnAddressArgumentIsConstant(Op, DAG))
15779     return SDValue();
15780 
15781   SDLoc dl(Op);
15782   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
15783 
15784   // Make sure the function does not optimize away the store of the RA to
15785   // the stack.
15786   PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
15787   FuncInfo->setLRStoreRequired();
15788   bool isPPC64 = Subtarget.isPPC64();
15789   auto PtrVT = getPointerTy(MF.getDataLayout());
15790 
15791   if (Depth > 0) {
15792     SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
15793     SDValue Offset =
15794         DAG.getConstant(Subtarget.getFrameLowering()->getReturnSaveOffset(), dl,
15795                         isPPC64 ? MVT::i64 : MVT::i32);
15796     return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
15797                        DAG.getNode(ISD::ADD, dl, PtrVT, FrameAddr, Offset),
15798                        MachinePointerInfo());
15799   }
15800 
15801   // Just load the return address off the stack.
15802   SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
15803   return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), RetAddrFI,
15804                      MachinePointerInfo());
15805 }
15806 
15807 SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
15808                                           SelectionDAG &DAG) const {
15809   SDLoc dl(Op);
15810   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
15811 
15812   MachineFunction &MF = DAG.getMachineFunction();
15813   MachineFrameInfo &MFI = MF.getFrameInfo();
15814   MFI.setFrameAddressIsTaken(true);
15815 
15816   EVT PtrVT = getPointerTy(MF.getDataLayout());
15817   bool isPPC64 = PtrVT == MVT::i64;
15818 
15819   // Naked functions never have a frame pointer, and so we use r1. For all
15820   // other functions, this decision must be delayed until during PEI.
15821   unsigned FrameReg;
15822   if (MF.getFunction().hasFnAttribute(Attribute::Naked))
15823     FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
15824   else
15825     FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
15826 
15827   SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
15828                                          PtrVT);
15829   while (Depth--)
15830     FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
15831                             FrameAddr, MachinePointerInfo());
15832   return FrameAddr;
15833 }
15834 
15835 // FIXME? Maybe this could be a TableGen attribute on some registers and
15836 // this table could be generated automatically from RegInfo.
15837 Register PPCTargetLowering::getRegisterByName(const char* RegName, LLT VT,
15838                                               const MachineFunction &MF) const {
15839   bool isPPC64 = Subtarget.isPPC64();
15840 
15841   bool is64Bit = isPPC64 && VT == LLT::scalar(64);
15842   if (!is64Bit && VT != LLT::scalar(32))
15843     report_fatal_error("Invalid register global variable type");
15844 
15845   Register Reg = StringSwitch<Register>(RegName)
15846                      .Case("r1", is64Bit ? PPC::X1 : PPC::R1)
15847                      .Case("r2", isPPC64 ? Register() : PPC::R2)
15848                      .Case("r13", (is64Bit ? PPC::X13 : PPC::R13))
15849                      .Default(Register());
15850 
15851   if (Reg)
15852     return Reg;
15853   report_fatal_error("Invalid register name global variable");
15854 }
15855 
15856 bool PPCTargetLowering::isAccessedAsGotIndirect(SDValue GA) const {
15857   // 32-bit SVR4 ABI access everything as got-indirect.
15858   if (Subtarget.is32BitELFABI())
15859     return true;
15860 
15861   // AIX accesses everything indirectly through the TOC, which is similar to
15862   // the GOT.
15863   if (Subtarget.isAIXABI())
15864     return true;
15865 
15866   CodeModel::Model CModel = getTargetMachine().getCodeModel();
15867   // If it is small or large code model, module locals are accessed
15868   // indirectly by loading their address from .toc/.got.
15869   if (CModel == CodeModel::Small || CModel == CodeModel::Large)
15870     return true;
15871 
15872   // JumpTable and BlockAddress are accessed as got-indirect.
15873   if (isa<JumpTableSDNode>(GA) || isa<BlockAddressSDNode>(GA))
15874     return true;
15875 
15876   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA))
15877     return Subtarget.isGVIndirectSymbol(G->getGlobal());
15878 
15879   return false;
15880 }
15881 
15882 bool
15883 PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
15884   // The PowerPC target isn't yet aware of offsets.
15885   return false;
15886 }
15887 
15888 bool PPCTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
15889                                            const CallInst &I,
15890                                            MachineFunction &MF,
15891                                            unsigned Intrinsic) const {
15892   switch (Intrinsic) {
15893   case Intrinsic::ppc_altivec_lvx:
15894   case Intrinsic::ppc_altivec_lvxl:
15895   case Intrinsic::ppc_altivec_lvebx:
15896   case Intrinsic::ppc_altivec_lvehx:
15897   case Intrinsic::ppc_altivec_lvewx:
15898   case Intrinsic::ppc_vsx_lxvd2x:
15899   case Intrinsic::ppc_vsx_lxvw4x:
15900   case Intrinsic::ppc_vsx_lxvd2x_be:
15901   case Intrinsic::ppc_vsx_lxvw4x_be:
15902   case Intrinsic::ppc_vsx_lxvl:
15903   case Intrinsic::ppc_vsx_lxvll: {
15904     EVT VT;
15905     switch (Intrinsic) {
15906     case Intrinsic::ppc_altivec_lvebx:
15907       VT = MVT::i8;
15908       break;
15909     case Intrinsic::ppc_altivec_lvehx:
15910       VT = MVT::i16;
15911       break;
15912     case Intrinsic::ppc_altivec_lvewx:
15913       VT = MVT::i32;
15914       break;
15915     case Intrinsic::ppc_vsx_lxvd2x:
15916     case Intrinsic::ppc_vsx_lxvd2x_be:
15917       VT = MVT::v2f64;
15918       break;
15919     default:
15920       VT = MVT::v4i32;
15921       break;
15922     }
15923 
15924     Info.opc = ISD::INTRINSIC_W_CHAIN;
15925     Info.memVT = VT;
15926     Info.ptrVal = I.getArgOperand(0);
15927     Info.offset = -VT.getStoreSize()+1;
15928     Info.size = 2*VT.getStoreSize()-1;
15929     Info.align = Align(1);
15930     Info.flags = MachineMemOperand::MOLoad;
15931     return true;
15932   }
15933   case Intrinsic::ppc_altivec_stvx:
15934   case Intrinsic::ppc_altivec_stvxl:
15935   case Intrinsic::ppc_altivec_stvebx:
15936   case Intrinsic::ppc_altivec_stvehx:
15937   case Intrinsic::ppc_altivec_stvewx:
15938   case Intrinsic::ppc_vsx_stxvd2x:
15939   case Intrinsic::ppc_vsx_stxvw4x:
15940   case Intrinsic::ppc_vsx_stxvd2x_be:
15941   case Intrinsic::ppc_vsx_stxvw4x_be:
15942   case Intrinsic::ppc_vsx_stxvl:
15943   case Intrinsic::ppc_vsx_stxvll: {
15944     EVT VT;
15945     switch (Intrinsic) {
15946     case Intrinsic::ppc_altivec_stvebx:
15947       VT = MVT::i8;
15948       break;
15949     case Intrinsic::ppc_altivec_stvehx:
15950       VT = MVT::i16;
15951       break;
15952     case Intrinsic::ppc_altivec_stvewx:
15953       VT = MVT::i32;
15954       break;
15955     case Intrinsic::ppc_vsx_stxvd2x:
15956     case Intrinsic::ppc_vsx_stxvd2x_be:
15957       VT = MVT::v2f64;
15958       break;
15959     default:
15960       VT = MVT::v4i32;
15961       break;
15962     }
15963 
15964     Info.opc = ISD::INTRINSIC_VOID;
15965     Info.memVT = VT;
15966     Info.ptrVal = I.getArgOperand(1);
15967     Info.offset = -VT.getStoreSize()+1;
15968     Info.size = 2*VT.getStoreSize()-1;
15969     Info.align = Align(1);
15970     Info.flags = MachineMemOperand::MOStore;
15971     return true;
15972   }
15973   default:
15974     break;
15975   }
15976 
15977   return false;
15978 }
15979 
15980 /// It returns EVT::Other if the type should be determined using generic
15981 /// target-independent logic.
15982 EVT PPCTargetLowering::getOptimalMemOpType(
15983     const MemOp &Op, const AttributeList &FuncAttributes) const {
15984   if (getTargetMachine().getOptLevel() != CodeGenOpt::None) {
15985     // We should use Altivec/VSX loads and stores when available. For unaligned
15986     // addresses, unaligned VSX loads are only fast starting with the P8.
15987     if (Subtarget.hasAltivec() && Op.size() >= 16 &&
15988         (Op.isAligned(Align(16)) ||
15989          ((Op.isMemset() && Subtarget.hasVSX()) || Subtarget.hasP8Vector())))
15990       return MVT::v4i32;
15991   }
15992 
15993   if (Subtarget.isPPC64()) {
15994     return MVT::i64;
15995   }
15996 
15997   return MVT::i32;
15998 }
15999 
16000 /// Returns true if it is beneficial to convert a load of a constant
16001 /// to just the constant itself.
16002 bool PPCTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
16003                                                           Type *Ty) const {
16004   assert(Ty->isIntegerTy());
16005 
16006   unsigned BitSize = Ty->getPrimitiveSizeInBits();
16007   return !(BitSize == 0 || BitSize > 64);
16008 }
16009 
16010 bool PPCTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
16011   if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
16012     return false;
16013   unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
16014   unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
16015   return NumBits1 == 64 && NumBits2 == 32;
16016 }
16017 
16018 bool PPCTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
16019   if (!VT1.isInteger() || !VT2.isInteger())
16020     return false;
16021   unsigned NumBits1 = VT1.getSizeInBits();
16022   unsigned NumBits2 = VT2.getSizeInBits();
16023   return NumBits1 == 64 && NumBits2 == 32;
16024 }
16025 
16026 bool PPCTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
16027   // Generally speaking, zexts are not free, but they are free when they can be
16028   // folded with other operations.
16029   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Val)) {
16030     EVT MemVT = LD->getMemoryVT();
16031     if ((MemVT == MVT::i1 || MemVT == MVT::i8 || MemVT == MVT::i16 ||
16032          (Subtarget.isPPC64() && MemVT == MVT::i32)) &&
16033         (LD->getExtensionType() == ISD::NON_EXTLOAD ||
16034          LD->getExtensionType() == ISD::ZEXTLOAD))
16035       return true;
16036   }
16037 
16038   // FIXME: Add other cases...
16039   //  - 32-bit shifts with a zext to i64
16040   //  - zext after ctlz, bswap, etc.
16041   //  - zext after and by a constant mask
16042 
16043   return TargetLowering::isZExtFree(Val, VT2);
16044 }
16045 
16046 bool PPCTargetLowering::isFPExtFree(EVT DestVT, EVT SrcVT) const {
16047   assert(DestVT.isFloatingPoint() && SrcVT.isFloatingPoint() &&
16048          "invalid fpext types");
16049   // Extending to float128 is not free.
16050   if (DestVT == MVT::f128)
16051     return false;
16052   return true;
16053 }
16054 
16055 bool PPCTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
16056   return isInt<16>(Imm) || isUInt<16>(Imm);
16057 }
16058 
16059 bool PPCTargetLowering::isLegalAddImmediate(int64_t Imm) const {
16060   return isInt<16>(Imm) || isUInt<16>(Imm);
16061 }
16062 
16063 bool PPCTargetLowering::allowsMisalignedMemoryAccesses(EVT VT, unsigned, Align,
16064                                                        MachineMemOperand::Flags,
16065                                                        bool *Fast) const {
16066   if (DisablePPCUnaligned)
16067     return false;
16068 
16069   // PowerPC supports unaligned memory access for simple non-vector types.
16070   // Although accessing unaligned addresses is not as efficient as accessing
16071   // aligned addresses, it is generally more efficient than manual expansion,
16072   // and generally only traps for software emulation when crossing page
16073   // boundaries.
16074 
16075   if (!VT.isSimple())
16076     return false;
16077 
16078   if (VT.isFloatingPoint() && !VT.isVector() &&
16079       !Subtarget.allowsUnalignedFPAccess())
16080     return false;
16081 
16082   if (VT.getSimpleVT().isVector()) {
16083     if (Subtarget.hasVSX()) {
16084       if (VT != MVT::v2f64 && VT != MVT::v2i64 &&
16085           VT != MVT::v4f32 && VT != MVT::v4i32)
16086         return false;
16087     } else {
16088       return false;
16089     }
16090   }
16091 
16092   if (VT == MVT::ppcf128)
16093     return false;
16094 
16095   if (Fast)
16096     *Fast = true;
16097 
16098   return true;
16099 }
16100 
16101 bool PPCTargetLowering::decomposeMulByConstant(LLVMContext &Context, EVT VT,
16102                                                SDValue C) const {
16103   // Check integral scalar types.
16104   if (!VT.isScalarInteger())
16105     return false;
16106   if (auto *ConstNode = dyn_cast<ConstantSDNode>(C.getNode())) {
16107     if (!ConstNode->getAPIntValue().isSignedIntN(64))
16108       return false;
16109     // This transformation will generate >= 2 operations. But the following
16110     // cases will generate <= 2 instructions during ISEL. So exclude them.
16111     // 1. If the constant multiplier fits 16 bits, it can be handled by one
16112     // HW instruction, ie. MULLI
16113     // 2. If the multiplier after shifted fits 16 bits, an extra shift
16114     // instruction is needed than case 1, ie. MULLI and RLDICR
16115     int64_t Imm = ConstNode->getSExtValue();
16116     unsigned Shift = countTrailingZeros<uint64_t>(Imm);
16117     Imm >>= Shift;
16118     if (isInt<16>(Imm))
16119       return false;
16120     uint64_t UImm = static_cast<uint64_t>(Imm);
16121     if (isPowerOf2_64(UImm + 1) || isPowerOf2_64(UImm - 1) ||
16122         isPowerOf2_64(1 - UImm) || isPowerOf2_64(-1 - UImm))
16123       return true;
16124   }
16125   return false;
16126 }
16127 
16128 bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(const MachineFunction &MF,
16129                                                    EVT VT) const {
16130   return isFMAFasterThanFMulAndFAdd(
16131       MF.getFunction(), VT.getTypeForEVT(MF.getFunction().getContext()));
16132 }
16133 
16134 bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(const Function &F,
16135                                                    Type *Ty) const {
16136   switch (Ty->getScalarType()->getTypeID()) {
16137   case Type::FloatTyID:
16138   case Type::DoubleTyID:
16139     return true;
16140   case Type::FP128TyID:
16141     return Subtarget.hasP9Vector();
16142   default:
16143     return false;
16144   }
16145 }
16146 
16147 // FIXME: add more patterns which are not profitable to hoist.
16148 bool PPCTargetLowering::isProfitableToHoist(Instruction *I) const {
16149   if (!I->hasOneUse())
16150     return true;
16151 
16152   Instruction *User = I->user_back();
16153   assert(User && "A single use instruction with no uses.");
16154 
16155   switch (I->getOpcode()) {
16156   case Instruction::FMul: {
16157     // Don't break FMA, PowerPC prefers FMA.
16158     if (User->getOpcode() != Instruction::FSub &&
16159         User->getOpcode() != Instruction::FAdd)
16160       return true;
16161 
16162     const TargetOptions &Options = getTargetMachine().Options;
16163     const Function *F = I->getFunction();
16164     const DataLayout &DL = F->getParent()->getDataLayout();
16165     Type *Ty = User->getOperand(0)->getType();
16166 
16167     return !(
16168         isFMAFasterThanFMulAndFAdd(*F, Ty) &&
16169         isOperationLegalOrCustom(ISD::FMA, getValueType(DL, Ty)) &&
16170         (Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath));
16171   }
16172   case Instruction::Load: {
16173     // Don't break "store (load float*)" pattern, this pattern will be combined
16174     // to "store (load int32)" in later InstCombine pass. See function
16175     // combineLoadToOperationType. On PowerPC, loading a float point takes more
16176     // cycles than loading a 32 bit integer.
16177     LoadInst *LI = cast<LoadInst>(I);
16178     // For the loads that combineLoadToOperationType does nothing, like
16179     // ordered load, it should be profitable to hoist them.
16180     // For swifterror load, it can only be used for pointer to pointer type, so
16181     // later type check should get rid of this case.
16182     if (!LI->isUnordered())
16183       return true;
16184 
16185     if (User->getOpcode() != Instruction::Store)
16186       return true;
16187 
16188     if (I->getType()->getTypeID() != Type::FloatTyID)
16189       return true;
16190 
16191     return false;
16192   }
16193   default:
16194     return true;
16195   }
16196   return true;
16197 }
16198 
16199 const MCPhysReg *
16200 PPCTargetLowering::getScratchRegisters(CallingConv::ID) const {
16201   // LR is a callee-save register, but we must treat it as clobbered by any call
16202   // site. Hence we include LR in the scratch registers, which are in turn added
16203   // as implicit-defs for stackmaps and patchpoints. The same reasoning applies
16204   // to CTR, which is used by any indirect call.
16205   static const MCPhysReg ScratchRegs[] = {
16206     PPC::X12, PPC::LR8, PPC::CTR8, 0
16207   };
16208 
16209   return ScratchRegs;
16210 }
16211 
16212 Register PPCTargetLowering::getExceptionPointerRegister(
16213     const Constant *PersonalityFn) const {
16214   return Subtarget.isPPC64() ? PPC::X3 : PPC::R3;
16215 }
16216 
16217 Register PPCTargetLowering::getExceptionSelectorRegister(
16218     const Constant *PersonalityFn) const {
16219   return Subtarget.isPPC64() ? PPC::X4 : PPC::R4;
16220 }
16221 
16222 bool
16223 PPCTargetLowering::shouldExpandBuildVectorWithShuffles(
16224                      EVT VT , unsigned DefinedValues) const {
16225   if (VT == MVT::v2i64)
16226     return Subtarget.hasDirectMove(); // Don't need stack ops with direct moves
16227 
16228   if (Subtarget.hasVSX())
16229     return true;
16230 
16231   return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
16232 }
16233 
16234 Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
16235   if (DisableILPPref || Subtarget.enableMachineScheduler())
16236     return TargetLowering::getSchedulingPreference(N);
16237 
16238   return Sched::ILP;
16239 }
16240 
16241 // Create a fast isel object.
16242 FastISel *
16243 PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
16244                                   const TargetLibraryInfo *LibInfo) const {
16245   return PPC::createFastISel(FuncInfo, LibInfo);
16246 }
16247 
16248 // 'Inverted' means the FMA opcode after negating one multiplicand.
16249 // For example, (fma -a b c) = (fnmsub a b c)
16250 static unsigned invertFMAOpcode(unsigned Opc) {
16251   switch (Opc) {
16252   default:
16253     llvm_unreachable("Invalid FMA opcode for PowerPC!");
16254   case ISD::FMA:
16255     return PPCISD::FNMSUB;
16256   case PPCISD::FNMSUB:
16257     return ISD::FMA;
16258   }
16259 }
16260 
16261 SDValue PPCTargetLowering::getNegatedExpression(SDValue Op, SelectionDAG &DAG,
16262                                                 bool LegalOps, bool OptForSize,
16263                                                 NegatibleCost &Cost,
16264                                                 unsigned Depth) const {
16265   if (Depth > SelectionDAG::MaxRecursionDepth)
16266     return SDValue();
16267 
16268   unsigned Opc = Op.getOpcode();
16269   EVT VT = Op.getValueType();
16270   SDNodeFlags Flags = Op.getNode()->getFlags();
16271 
16272   switch (Opc) {
16273   case PPCISD::FNMSUB:
16274     if (!Op.hasOneUse() || !isTypeLegal(VT))
16275       break;
16276 
16277     const TargetOptions &Options = getTargetMachine().Options;
16278     SDValue N0 = Op.getOperand(0);
16279     SDValue N1 = Op.getOperand(1);
16280     SDValue N2 = Op.getOperand(2);
16281     SDLoc Loc(Op);
16282 
16283     NegatibleCost N2Cost = NegatibleCost::Expensive;
16284     SDValue NegN2 =
16285         getNegatedExpression(N2, DAG, LegalOps, OptForSize, N2Cost, Depth + 1);
16286 
16287     if (!NegN2)
16288       return SDValue();
16289 
16290     // (fneg (fnmsub a b c)) => (fnmsub (fneg a) b (fneg c))
16291     // (fneg (fnmsub a b c)) => (fnmsub a (fneg b) (fneg c))
16292     // These transformations may change sign of zeroes. For example,
16293     // -(-ab-(-c))=-0 while -(-(ab-c))=+0 when a=b=c=1.
16294     if (Flags.hasNoSignedZeros() || Options.NoSignedZerosFPMath) {
16295       // Try and choose the cheaper one to negate.
16296       NegatibleCost N0Cost = NegatibleCost::Expensive;
16297       SDValue NegN0 = getNegatedExpression(N0, DAG, LegalOps, OptForSize,
16298                                            N0Cost, Depth + 1);
16299 
16300       NegatibleCost N1Cost = NegatibleCost::Expensive;
16301       SDValue NegN1 = getNegatedExpression(N1, DAG, LegalOps, OptForSize,
16302                                            N1Cost, Depth + 1);
16303 
16304       if (NegN0 && N0Cost <= N1Cost) {
16305         Cost = std::min(N0Cost, N2Cost);
16306         return DAG.getNode(Opc, Loc, VT, NegN0, N1, NegN2, Flags);
16307       } else if (NegN1) {
16308         Cost = std::min(N1Cost, N2Cost);
16309         return DAG.getNode(Opc, Loc, VT, N0, NegN1, NegN2, Flags);
16310       }
16311     }
16312 
16313     // (fneg (fnmsub a b c)) => (fma a b (fneg c))
16314     if (isOperationLegal(ISD::FMA, VT)) {
16315       Cost = N2Cost;
16316       return DAG.getNode(ISD::FMA, Loc, VT, N0, N1, NegN2, Flags);
16317     }
16318 
16319     break;
16320   }
16321 
16322   return TargetLowering::getNegatedExpression(Op, DAG, LegalOps, OptForSize,
16323                                               Cost, Depth);
16324 }
16325 
16326 // Override to enable LOAD_STACK_GUARD lowering on Linux.
16327 bool PPCTargetLowering::useLoadStackGuardNode() const {
16328   if (!Subtarget.isTargetLinux())
16329     return TargetLowering::useLoadStackGuardNode();
16330   return true;
16331 }
16332 
16333 // Override to disable global variable loading on Linux.
16334 void PPCTargetLowering::insertSSPDeclarations(Module &M) const {
16335   if (!Subtarget.isTargetLinux())
16336     return TargetLowering::insertSSPDeclarations(M);
16337 }
16338 
16339 bool PPCTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT,
16340                                      bool ForCodeSize) const {
16341   if (!VT.isSimple() || !Subtarget.hasVSX())
16342     return false;
16343 
16344   switch(VT.getSimpleVT().SimpleTy) {
16345   default:
16346     // For FP types that are currently not supported by PPC backend, return
16347     // false. Examples: f16, f80.
16348     return false;
16349   case MVT::f32:
16350   case MVT::f64:
16351     if (Subtarget.hasPrefixInstrs()) {
16352       // we can materialize all immediatess via XXSPLTI32DX and XXSPLTIDP.
16353       return true;
16354     }
16355     LLVM_FALLTHROUGH;
16356   case MVT::ppcf128:
16357     return Imm.isPosZero();
16358   }
16359 }
16360 
16361 // For vector shift operation op, fold
16362 // (op x, (and y, ((1 << numbits(x)) - 1))) -> (target op x, y)
16363 static SDValue stripModuloOnShift(const TargetLowering &TLI, SDNode *N,
16364                                   SelectionDAG &DAG) {
16365   SDValue N0 = N->getOperand(0);
16366   SDValue N1 = N->getOperand(1);
16367   EVT VT = N0.getValueType();
16368   unsigned OpSizeInBits = VT.getScalarSizeInBits();
16369   unsigned Opcode = N->getOpcode();
16370   unsigned TargetOpcode;
16371 
16372   switch (Opcode) {
16373   default:
16374     llvm_unreachable("Unexpected shift operation");
16375   case ISD::SHL:
16376     TargetOpcode = PPCISD::SHL;
16377     break;
16378   case ISD::SRL:
16379     TargetOpcode = PPCISD::SRL;
16380     break;
16381   case ISD::SRA:
16382     TargetOpcode = PPCISD::SRA;
16383     break;
16384   }
16385 
16386   if (VT.isVector() && TLI.isOperationLegal(Opcode, VT) &&
16387       N1->getOpcode() == ISD::AND)
16388     if (ConstantSDNode *Mask = isConstOrConstSplat(N1->getOperand(1)))
16389       if (Mask->getZExtValue() == OpSizeInBits - 1)
16390         return DAG.getNode(TargetOpcode, SDLoc(N), VT, N0, N1->getOperand(0));
16391 
16392   return SDValue();
16393 }
16394 
16395 SDValue PPCTargetLowering::combineSHL(SDNode *N, DAGCombinerInfo &DCI) const {
16396   if (auto Value = stripModuloOnShift(*this, N, DCI.DAG))
16397     return Value;
16398 
16399   SDValue N0 = N->getOperand(0);
16400   ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N->getOperand(1));
16401   if (!Subtarget.isISA3_0() || !Subtarget.isPPC64() ||
16402       N0.getOpcode() != ISD::SIGN_EXTEND ||
16403       N0.getOperand(0).getValueType() != MVT::i32 || CN1 == nullptr ||
16404       N->getValueType(0) != MVT::i64)
16405     return SDValue();
16406 
16407   // We can't save an operation here if the value is already extended, and
16408   // the existing shift is easier to combine.
16409   SDValue ExtsSrc = N0.getOperand(0);
16410   if (ExtsSrc.getOpcode() == ISD::TRUNCATE &&
16411       ExtsSrc.getOperand(0).getOpcode() == ISD::AssertSext)
16412     return SDValue();
16413 
16414   SDLoc DL(N0);
16415   SDValue ShiftBy = SDValue(CN1, 0);
16416   // We want the shift amount to be i32 on the extswli, but the shift could
16417   // have an i64.
16418   if (ShiftBy.getValueType() == MVT::i64)
16419     ShiftBy = DCI.DAG.getConstant(CN1->getZExtValue(), DL, MVT::i32);
16420 
16421   return DCI.DAG.getNode(PPCISD::EXTSWSLI, DL, MVT::i64, N0->getOperand(0),
16422                          ShiftBy);
16423 }
16424 
16425 SDValue PPCTargetLowering::combineSRA(SDNode *N, DAGCombinerInfo &DCI) const {
16426   if (auto Value = stripModuloOnShift(*this, N, DCI.DAG))
16427     return Value;
16428 
16429   return SDValue();
16430 }
16431 
16432 SDValue PPCTargetLowering::combineSRL(SDNode *N, DAGCombinerInfo &DCI) const {
16433   if (auto Value = stripModuloOnShift(*this, N, DCI.DAG))
16434     return Value;
16435 
16436   return SDValue();
16437 }
16438 
16439 // Transform (add X, (zext(setne Z, C))) -> (addze X, (addic (addi Z, -C), -1))
16440 // Transform (add X, (zext(sete  Z, C))) -> (addze X, (subfic (addi Z, -C), 0))
16441 // When C is zero, the equation (addi Z, -C) can be simplified to Z
16442 // Requirement: -C in [-32768, 32767], X and Z are MVT::i64 types
16443 static SDValue combineADDToADDZE(SDNode *N, SelectionDAG &DAG,
16444                                  const PPCSubtarget &Subtarget) {
16445   if (!Subtarget.isPPC64())
16446     return SDValue();
16447 
16448   SDValue LHS = N->getOperand(0);
16449   SDValue RHS = N->getOperand(1);
16450 
16451   auto isZextOfCompareWithConstant = [](SDValue Op) {
16452     if (Op.getOpcode() != ISD::ZERO_EXTEND || !Op.hasOneUse() ||
16453         Op.getValueType() != MVT::i64)
16454       return false;
16455 
16456     SDValue Cmp = Op.getOperand(0);
16457     if (Cmp.getOpcode() != ISD::SETCC || !Cmp.hasOneUse() ||
16458         Cmp.getOperand(0).getValueType() != MVT::i64)
16459       return false;
16460 
16461     if (auto *Constant = dyn_cast<ConstantSDNode>(Cmp.getOperand(1))) {
16462       int64_t NegConstant = 0 - Constant->getSExtValue();
16463       // Due to the limitations of the addi instruction,
16464       // -C is required to be [-32768, 32767].
16465       return isInt<16>(NegConstant);
16466     }
16467 
16468     return false;
16469   };
16470 
16471   bool LHSHasPattern = isZextOfCompareWithConstant(LHS);
16472   bool RHSHasPattern = isZextOfCompareWithConstant(RHS);
16473 
16474   // If there is a pattern, canonicalize a zext operand to the RHS.
16475   if (LHSHasPattern && !RHSHasPattern)
16476     std::swap(LHS, RHS);
16477   else if (!LHSHasPattern && !RHSHasPattern)
16478     return SDValue();
16479 
16480   SDLoc DL(N);
16481   SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Glue);
16482   SDValue Cmp = RHS.getOperand(0);
16483   SDValue Z = Cmp.getOperand(0);
16484   auto *Constant = dyn_cast<ConstantSDNode>(Cmp.getOperand(1));
16485 
16486   assert(Constant && "Constant Should not be a null pointer.");
16487   int64_t NegConstant = 0 - Constant->getSExtValue();
16488 
16489   switch(cast<CondCodeSDNode>(Cmp.getOperand(2))->get()) {
16490   default: break;
16491   case ISD::SETNE: {
16492     //                                 when C == 0
16493     //                             --> addze X, (addic Z, -1).carry
16494     //                            /
16495     // add X, (zext(setne Z, C))--
16496     //                            \    when -32768 <= -C <= 32767 && C != 0
16497     //                             --> addze X, (addic (addi Z, -C), -1).carry
16498     SDValue Add = DAG.getNode(ISD::ADD, DL, MVT::i64, Z,
16499                               DAG.getConstant(NegConstant, DL, MVT::i64));
16500     SDValue AddOrZ = NegConstant != 0 ? Add : Z;
16501     SDValue Addc = DAG.getNode(ISD::ADDC, DL, DAG.getVTList(MVT::i64, MVT::Glue),
16502                                AddOrZ, DAG.getConstant(-1ULL, DL, MVT::i64));
16503     return DAG.getNode(ISD::ADDE, DL, VTs, LHS, DAG.getConstant(0, DL, MVT::i64),
16504                        SDValue(Addc.getNode(), 1));
16505     }
16506   case ISD::SETEQ: {
16507     //                                 when C == 0
16508     //                             --> addze X, (subfic Z, 0).carry
16509     //                            /
16510     // add X, (zext(sete  Z, C))--
16511     //                            \    when -32768 <= -C <= 32767 && C != 0
16512     //                             --> addze X, (subfic (addi Z, -C), 0).carry
16513     SDValue Add = DAG.getNode(ISD::ADD, DL, MVT::i64, Z,
16514                               DAG.getConstant(NegConstant, DL, MVT::i64));
16515     SDValue AddOrZ = NegConstant != 0 ? Add : Z;
16516     SDValue Subc = DAG.getNode(ISD::SUBC, DL, DAG.getVTList(MVT::i64, MVT::Glue),
16517                                DAG.getConstant(0, DL, MVT::i64), AddOrZ);
16518     return DAG.getNode(ISD::ADDE, DL, VTs, LHS, DAG.getConstant(0, DL, MVT::i64),
16519                        SDValue(Subc.getNode(), 1));
16520     }
16521   }
16522 
16523   return SDValue();
16524 }
16525 
16526 // Transform
16527 // (add C1, (MAT_PCREL_ADDR GlobalAddr+C2)) to
16528 // (MAT_PCREL_ADDR GlobalAddr+(C1+C2))
16529 // In this case both C1 and C2 must be known constants.
16530 // C1+C2 must fit into a 34 bit signed integer.
16531 static SDValue combineADDToMAT_PCREL_ADDR(SDNode *N, SelectionDAG &DAG,
16532                                           const PPCSubtarget &Subtarget) {
16533   if (!Subtarget.isUsingPCRelativeCalls())
16534     return SDValue();
16535 
16536   // Check both Operand 0 and Operand 1 of the ADD node for the PCRel node.
16537   // If we find that node try to cast the Global Address and the Constant.
16538   SDValue LHS = N->getOperand(0);
16539   SDValue RHS = N->getOperand(1);
16540 
16541   if (LHS.getOpcode() != PPCISD::MAT_PCREL_ADDR)
16542     std::swap(LHS, RHS);
16543 
16544   if (LHS.getOpcode() != PPCISD::MAT_PCREL_ADDR)
16545     return SDValue();
16546 
16547   // Operand zero of PPCISD::MAT_PCREL_ADDR is the GA node.
16548   GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(LHS.getOperand(0));
16549   ConstantSDNode* ConstNode = dyn_cast<ConstantSDNode>(RHS);
16550 
16551   // Check that both casts succeeded.
16552   if (!GSDN || !ConstNode)
16553     return SDValue();
16554 
16555   int64_t NewOffset = GSDN->getOffset() + ConstNode->getSExtValue();
16556   SDLoc DL(GSDN);
16557 
16558   // The signed int offset needs to fit in 34 bits.
16559   if (!isInt<34>(NewOffset))
16560     return SDValue();
16561 
16562   // The new global address is a copy of the old global address except
16563   // that it has the updated Offset.
16564   SDValue GA =
16565       DAG.getTargetGlobalAddress(GSDN->getGlobal(), DL, GSDN->getValueType(0),
16566                                  NewOffset, GSDN->getTargetFlags());
16567   SDValue MatPCRel =
16568       DAG.getNode(PPCISD::MAT_PCREL_ADDR, DL, GSDN->getValueType(0), GA);
16569   return MatPCRel;
16570 }
16571 
16572 SDValue PPCTargetLowering::combineADD(SDNode *N, DAGCombinerInfo &DCI) const {
16573   if (auto Value = combineADDToADDZE(N, DCI.DAG, Subtarget))
16574     return Value;
16575 
16576   if (auto Value = combineADDToMAT_PCREL_ADDR(N, DCI.DAG, Subtarget))
16577     return Value;
16578 
16579   return SDValue();
16580 }
16581 
16582 // Detect TRUNCATE operations on bitcasts of float128 values.
16583 // What we are looking for here is the situtation where we extract a subset
16584 // of bits from a 128 bit float.
16585 // This can be of two forms:
16586 // 1) BITCAST of f128 feeding TRUNCATE
16587 // 2) BITCAST of f128 feeding SRL (a shift) feeding TRUNCATE
16588 // The reason this is required is because we do not have a legal i128 type
16589 // and so we want to prevent having to store the f128 and then reload part
16590 // of it.
16591 SDValue PPCTargetLowering::combineTRUNCATE(SDNode *N,
16592                                            DAGCombinerInfo &DCI) const {
16593   // If we are using CRBits then try that first.
16594   if (Subtarget.useCRBits()) {
16595     // Check if CRBits did anything and return that if it did.
16596     if (SDValue CRTruncValue = DAGCombineTruncBoolExt(N, DCI))
16597       return CRTruncValue;
16598   }
16599 
16600   SDLoc dl(N);
16601   SDValue Op0 = N->getOperand(0);
16602 
16603   // fold (truncate (abs (sub (zext a), (zext b)))) -> (vabsd a, b)
16604   if (Subtarget.hasP9Altivec() && Op0.getOpcode() == ISD::ABS) {
16605     EVT VT = N->getValueType(0);
16606     if (VT != MVT::v4i32 && VT != MVT::v8i16 && VT != MVT::v16i8)
16607       return SDValue();
16608     SDValue Sub = Op0.getOperand(0);
16609     if (Sub.getOpcode() == ISD::SUB) {
16610       SDValue SubOp0 = Sub.getOperand(0);
16611       SDValue SubOp1 = Sub.getOperand(1);
16612       if ((SubOp0.getOpcode() == ISD::ZERO_EXTEND) &&
16613           (SubOp1.getOpcode() == ISD::ZERO_EXTEND)) {
16614         return DCI.DAG.getNode(PPCISD::VABSD, dl, VT, SubOp0.getOperand(0),
16615                                SubOp1.getOperand(0),
16616                                DCI.DAG.getTargetConstant(0, dl, MVT::i32));
16617       }
16618     }
16619   }
16620 
16621   // Looking for a truncate of i128 to i64.
16622   if (Op0.getValueType() != MVT::i128 || N->getValueType(0) != MVT::i64)
16623     return SDValue();
16624 
16625   int EltToExtract = DCI.DAG.getDataLayout().isBigEndian() ? 1 : 0;
16626 
16627   // SRL feeding TRUNCATE.
16628   if (Op0.getOpcode() == ISD::SRL) {
16629     ConstantSDNode *ConstNode = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
16630     // The right shift has to be by 64 bits.
16631     if (!ConstNode || ConstNode->getZExtValue() != 64)
16632       return SDValue();
16633 
16634     // Switch the element number to extract.
16635     EltToExtract = EltToExtract ? 0 : 1;
16636     // Update Op0 past the SRL.
16637     Op0 = Op0.getOperand(0);
16638   }
16639 
16640   // BITCAST feeding a TRUNCATE possibly via SRL.
16641   if (Op0.getOpcode() == ISD::BITCAST &&
16642       Op0.getValueType() == MVT::i128 &&
16643       Op0.getOperand(0).getValueType() == MVT::f128) {
16644     SDValue Bitcast = DCI.DAG.getBitcast(MVT::v2i64, Op0.getOperand(0));
16645     return DCI.DAG.getNode(
16646         ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64, Bitcast,
16647         DCI.DAG.getTargetConstant(EltToExtract, dl, MVT::i32));
16648   }
16649   return SDValue();
16650 }
16651 
16652 SDValue PPCTargetLowering::combineMUL(SDNode *N, DAGCombinerInfo &DCI) const {
16653   SelectionDAG &DAG = DCI.DAG;
16654 
16655   ConstantSDNode *ConstOpOrElement = isConstOrConstSplat(N->getOperand(1));
16656   if (!ConstOpOrElement)
16657     return SDValue();
16658 
16659   // An imul is usually smaller than the alternative sequence for legal type.
16660   if (DAG.getMachineFunction().getFunction().hasMinSize() &&
16661       isOperationLegal(ISD::MUL, N->getValueType(0)))
16662     return SDValue();
16663 
16664   auto IsProfitable = [this](bool IsNeg, bool IsAddOne, EVT VT) -> bool {
16665     switch (this->Subtarget.getCPUDirective()) {
16666     default:
16667       // TODO: enhance the condition for subtarget before pwr8
16668       return false;
16669     case PPC::DIR_PWR8:
16670       //  type        mul     add    shl
16671       // scalar        4       1      1
16672       // vector        7       2      2
16673       return true;
16674     case PPC::DIR_PWR9:
16675     case PPC::DIR_PWR10:
16676     case PPC::DIR_PWR_FUTURE:
16677       //  type        mul     add    shl
16678       // scalar        5       2      2
16679       // vector        7       2      2
16680 
16681       // The cycle RATIO of related operations are showed as a table above.
16682       // Because mul is 5(scalar)/7(vector), add/sub/shl are all 2 for both
16683       // scalar and vector type. For 2 instrs patterns, add/sub + shl
16684       // are 4, it is always profitable; but for 3 instrs patterns
16685       // (mul x, -(2^N + 1)) => -(add (shl x, N), x), sub + add + shl are 6.
16686       // So we should only do it for vector type.
16687       return IsAddOne && IsNeg ? VT.isVector() : true;
16688     }
16689   };
16690 
16691   EVT VT = N->getValueType(0);
16692   SDLoc DL(N);
16693 
16694   const APInt &MulAmt = ConstOpOrElement->getAPIntValue();
16695   bool IsNeg = MulAmt.isNegative();
16696   APInt MulAmtAbs = MulAmt.abs();
16697 
16698   if ((MulAmtAbs - 1).isPowerOf2()) {
16699     // (mul x, 2^N + 1) => (add (shl x, N), x)
16700     // (mul x, -(2^N + 1)) => -(add (shl x, N), x)
16701 
16702     if (!IsProfitable(IsNeg, true, VT))
16703       return SDValue();
16704 
16705     SDValue Op0 = N->getOperand(0);
16706     SDValue Op1 =
16707         DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
16708                     DAG.getConstant((MulAmtAbs - 1).logBase2(), DL, VT));
16709     SDValue Res = DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
16710 
16711     if (!IsNeg)
16712       return Res;
16713 
16714     return DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Res);
16715   } else if ((MulAmtAbs + 1).isPowerOf2()) {
16716     // (mul x, 2^N - 1) => (sub (shl x, N), x)
16717     // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
16718 
16719     if (!IsProfitable(IsNeg, false, VT))
16720       return SDValue();
16721 
16722     SDValue Op0 = N->getOperand(0);
16723     SDValue Op1 =
16724         DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
16725                     DAG.getConstant((MulAmtAbs + 1).logBase2(), DL, VT));
16726 
16727     if (!IsNeg)
16728       return DAG.getNode(ISD::SUB, DL, VT, Op1, Op0);
16729     else
16730       return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
16731 
16732   } else {
16733     return SDValue();
16734   }
16735 }
16736 
16737 // Combine fma-like op (like fnmsub) with fnegs to appropriate op. Do this
16738 // in combiner since we need to check SD flags and other subtarget features.
16739 SDValue PPCTargetLowering::combineFMALike(SDNode *N,
16740                                           DAGCombinerInfo &DCI) const {
16741   SDValue N0 = N->getOperand(0);
16742   SDValue N1 = N->getOperand(1);
16743   SDValue N2 = N->getOperand(2);
16744   SDNodeFlags Flags = N->getFlags();
16745   EVT VT = N->getValueType(0);
16746   SelectionDAG &DAG = DCI.DAG;
16747   const TargetOptions &Options = getTargetMachine().Options;
16748   unsigned Opc = N->getOpcode();
16749   bool CodeSize = DAG.getMachineFunction().getFunction().hasOptSize();
16750   bool LegalOps = !DCI.isBeforeLegalizeOps();
16751   SDLoc Loc(N);
16752 
16753   if (!isOperationLegal(ISD::FMA, VT))
16754     return SDValue();
16755 
16756   // Allowing transformation to FNMSUB may change sign of zeroes when ab-c=0
16757   // since (fnmsub a b c)=-0 while c-ab=+0.
16758   if (!Flags.hasNoSignedZeros() && !Options.NoSignedZerosFPMath)
16759     return SDValue();
16760 
16761   // (fma (fneg a) b c) => (fnmsub a b c)
16762   // (fnmsub (fneg a) b c) => (fma a b c)
16763   if (SDValue NegN0 = getCheaperNegatedExpression(N0, DAG, LegalOps, CodeSize))
16764     return DAG.getNode(invertFMAOpcode(Opc), Loc, VT, NegN0, N1, N2, Flags);
16765 
16766   // (fma a (fneg b) c) => (fnmsub a b c)
16767   // (fnmsub a (fneg b) c) => (fma a b c)
16768   if (SDValue NegN1 = getCheaperNegatedExpression(N1, DAG, LegalOps, CodeSize))
16769     return DAG.getNode(invertFMAOpcode(Opc), Loc, VT, N0, NegN1, N2, Flags);
16770 
16771   return SDValue();
16772 }
16773 
16774 bool PPCTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
16775   // Only duplicate to increase tail-calls for the 64bit SysV ABIs.
16776   if (!Subtarget.is64BitELFABI())
16777     return false;
16778 
16779   // If not a tail call then no need to proceed.
16780   if (!CI->isTailCall())
16781     return false;
16782 
16783   // If sibling calls have been disabled and tail-calls aren't guaranteed
16784   // there is no reason to duplicate.
16785   auto &TM = getTargetMachine();
16786   if (!TM.Options.GuaranteedTailCallOpt && DisableSCO)
16787     return false;
16788 
16789   // Can't tail call a function called indirectly, or if it has variadic args.
16790   const Function *Callee = CI->getCalledFunction();
16791   if (!Callee || Callee->isVarArg())
16792     return false;
16793 
16794   // Make sure the callee and caller calling conventions are eligible for tco.
16795   const Function *Caller = CI->getParent()->getParent();
16796   if (!areCallingConvEligibleForTCO_64SVR4(Caller->getCallingConv(),
16797                                            CI->getCallingConv()))
16798       return false;
16799 
16800   // If the function is local then we have a good chance at tail-calling it
16801   return getTargetMachine().shouldAssumeDSOLocal(*Caller->getParent(), Callee);
16802 }
16803 
16804 bool PPCTargetLowering::hasBitPreservingFPLogic(EVT VT) const {
16805   if (!Subtarget.hasVSX())
16806     return false;
16807   if (Subtarget.hasP9Vector() && VT == MVT::f128)
16808     return true;
16809   return VT == MVT::f32 || VT == MVT::f64 ||
16810     VT == MVT::v4f32 || VT == MVT::v2f64;
16811 }
16812 
16813 bool PPCTargetLowering::
16814 isMaskAndCmp0FoldingBeneficial(const Instruction &AndI) const {
16815   const Value *Mask = AndI.getOperand(1);
16816   // If the mask is suitable for andi. or andis. we should sink the and.
16817   if (const ConstantInt *CI = dyn_cast<ConstantInt>(Mask)) {
16818     // Can't handle constants wider than 64-bits.
16819     if (CI->getBitWidth() > 64)
16820       return false;
16821     int64_t ConstVal = CI->getZExtValue();
16822     return isUInt<16>(ConstVal) ||
16823       (isUInt<16>(ConstVal >> 16) && !(ConstVal & 0xFFFF));
16824   }
16825 
16826   // For non-constant masks, we can always use the record-form and.
16827   return true;
16828 }
16829 
16830 // Transform (abs (sub (zext a), (zext b))) to (vabsd a b 0)
16831 // Transform (abs (sub (zext a), (zext_invec b))) to (vabsd a b 0)
16832 // Transform (abs (sub (zext_invec a), (zext_invec b))) to (vabsd a b 0)
16833 // Transform (abs (sub (zext_invec a), (zext b))) to (vabsd a b 0)
16834 // Transform (abs (sub a, b) to (vabsd a b 1)) if a & b of type v4i32
16835 SDValue PPCTargetLowering::combineABS(SDNode *N, DAGCombinerInfo &DCI) const {
16836   assert((N->getOpcode() == ISD::ABS) && "Need ABS node here");
16837   assert(Subtarget.hasP9Altivec() &&
16838          "Only combine this when P9 altivec supported!");
16839   EVT VT = N->getValueType(0);
16840   if (VT != MVT::v4i32 && VT != MVT::v8i16 && VT != MVT::v16i8)
16841     return SDValue();
16842 
16843   SelectionDAG &DAG = DCI.DAG;
16844   SDLoc dl(N);
16845   if (N->getOperand(0).getOpcode() == ISD::SUB) {
16846     // Even for signed integers, if it's known to be positive (as signed
16847     // integer) due to zero-extended inputs.
16848     unsigned SubOpcd0 = N->getOperand(0)->getOperand(0).getOpcode();
16849     unsigned SubOpcd1 = N->getOperand(0)->getOperand(1).getOpcode();
16850     if ((SubOpcd0 == ISD::ZERO_EXTEND ||
16851          SubOpcd0 == ISD::ZERO_EXTEND_VECTOR_INREG) &&
16852         (SubOpcd1 == ISD::ZERO_EXTEND ||
16853          SubOpcd1 == ISD::ZERO_EXTEND_VECTOR_INREG)) {
16854       return DAG.getNode(PPCISD::VABSD, dl, N->getOperand(0).getValueType(),
16855                          N->getOperand(0)->getOperand(0),
16856                          N->getOperand(0)->getOperand(1),
16857                          DAG.getTargetConstant(0, dl, MVT::i32));
16858     }
16859 
16860     // For type v4i32, it can be optimized with xvnegsp + vabsduw
16861     if (N->getOperand(0).getValueType() == MVT::v4i32 &&
16862         N->getOperand(0).hasOneUse()) {
16863       return DAG.getNode(PPCISD::VABSD, dl, N->getOperand(0).getValueType(),
16864                          N->getOperand(0)->getOperand(0),
16865                          N->getOperand(0)->getOperand(1),
16866                          DAG.getTargetConstant(1, dl, MVT::i32));
16867     }
16868   }
16869 
16870   return SDValue();
16871 }
16872 
16873 // For type v4i32/v8ii16/v16i8, transform
16874 // from (vselect (setcc a, b, setugt), (sub a, b), (sub b, a)) to (vabsd a, b)
16875 // from (vselect (setcc a, b, setuge), (sub a, b), (sub b, a)) to (vabsd a, b)
16876 // from (vselect (setcc a, b, setult), (sub b, a), (sub a, b)) to (vabsd a, b)
16877 // from (vselect (setcc a, b, setule), (sub b, a), (sub a, b)) to (vabsd a, b)
16878 SDValue PPCTargetLowering::combineVSelect(SDNode *N,
16879                                           DAGCombinerInfo &DCI) const {
16880   assert((N->getOpcode() == ISD::VSELECT) && "Need VSELECT node here");
16881   assert(Subtarget.hasP9Altivec() &&
16882          "Only combine this when P9 altivec supported!");
16883 
16884   SelectionDAG &DAG = DCI.DAG;
16885   SDLoc dl(N);
16886   SDValue Cond = N->getOperand(0);
16887   SDValue TrueOpnd = N->getOperand(1);
16888   SDValue FalseOpnd = N->getOperand(2);
16889   EVT VT = N->getOperand(1).getValueType();
16890 
16891   if (Cond.getOpcode() != ISD::SETCC || TrueOpnd.getOpcode() != ISD::SUB ||
16892       FalseOpnd.getOpcode() != ISD::SUB)
16893     return SDValue();
16894 
16895   // ABSD only available for type v4i32/v8i16/v16i8
16896   if (VT != MVT::v4i32 && VT != MVT::v8i16 && VT != MVT::v16i8)
16897     return SDValue();
16898 
16899   // At least to save one more dependent computation
16900   if (!(Cond.hasOneUse() || TrueOpnd.hasOneUse() || FalseOpnd.hasOneUse()))
16901     return SDValue();
16902 
16903   ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
16904 
16905   // Can only handle unsigned comparison here
16906   switch (CC) {
16907   default:
16908     return SDValue();
16909   case ISD::SETUGT:
16910   case ISD::SETUGE:
16911     break;
16912   case ISD::SETULT:
16913   case ISD::SETULE:
16914     std::swap(TrueOpnd, FalseOpnd);
16915     break;
16916   }
16917 
16918   SDValue CmpOpnd1 = Cond.getOperand(0);
16919   SDValue CmpOpnd2 = Cond.getOperand(1);
16920 
16921   // SETCC CmpOpnd1 CmpOpnd2 cond
16922   // TrueOpnd = CmpOpnd1 - CmpOpnd2
16923   // FalseOpnd = CmpOpnd2 - CmpOpnd1
16924   if (TrueOpnd.getOperand(0) == CmpOpnd1 &&
16925       TrueOpnd.getOperand(1) == CmpOpnd2 &&
16926       FalseOpnd.getOperand(0) == CmpOpnd2 &&
16927       FalseOpnd.getOperand(1) == CmpOpnd1) {
16928     return DAG.getNode(PPCISD::VABSD, dl, N->getOperand(1).getValueType(),
16929                        CmpOpnd1, CmpOpnd2,
16930                        DAG.getTargetConstant(0, dl, MVT::i32));
16931   }
16932 
16933   return SDValue();
16934 }
16935 
16936 /// getAddrModeForFlags - Based on the set of address flags, select the most
16937 /// optimal instruction format to match by.
16938 PPC::AddrMode PPCTargetLowering::getAddrModeForFlags(unsigned Flags) const {
16939   // This is not a node we should be handling here.
16940   if (Flags == PPC::MOF_None)
16941     return PPC::AM_None;
16942   // Unaligned D-Forms are tried first, followed by the aligned D-Forms.
16943   for (auto FlagSet : AddrModesMap.at(PPC::AM_DForm))
16944     if ((Flags & FlagSet) == FlagSet)
16945       return PPC::AM_DForm;
16946   for (auto FlagSet : AddrModesMap.at(PPC::AM_DSForm))
16947     if ((Flags & FlagSet) == FlagSet)
16948       return PPC::AM_DSForm;
16949   for (auto FlagSet : AddrModesMap.at(PPC::AM_DQForm))
16950     if ((Flags & FlagSet) == FlagSet)
16951       return PPC::AM_DQForm;
16952   // If no other forms are selected, return an X-Form as it is the most
16953   // general addressing mode.
16954   return PPC::AM_XForm;
16955 }
16956 
16957 /// Set alignment flags based on whether or not the Frame Index is aligned.
16958 /// Utilized when computing flags for address computation when selecting
16959 /// load and store instructions.
16960 static void setAlignFlagsForFI(SDValue N, unsigned &FlagSet,
16961                                SelectionDAG &DAG) {
16962   bool IsAdd = ((N.getOpcode() == ISD::ADD) || (N.getOpcode() == ISD::OR));
16963   FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(IsAdd ? N.getOperand(0) : N);
16964   if (!FI)
16965     return;
16966   const MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
16967   unsigned FrameIndexAlign = MFI.getObjectAlign(FI->getIndex()).value();
16968   // If this is (add $FI, $S16Imm), the alignment flags are already set
16969   // based on the immediate. We just need to clear the alignment flags
16970   // if the FI alignment is weaker.
16971   if ((FrameIndexAlign % 4) != 0)
16972     FlagSet &= ~PPC::MOF_RPlusSImm16Mult4;
16973   if ((FrameIndexAlign % 16) != 0)
16974     FlagSet &= ~PPC::MOF_RPlusSImm16Mult16;
16975   // If the address is a plain FrameIndex, set alignment flags based on
16976   // FI alignment.
16977   if (!IsAdd) {
16978     if ((FrameIndexAlign % 4) == 0)
16979       FlagSet |= PPC::MOF_RPlusSImm16Mult4;
16980     if ((FrameIndexAlign % 16) == 0)
16981       FlagSet |= PPC::MOF_RPlusSImm16Mult16;
16982   }
16983 }
16984 
16985 /// Given a node, compute flags that are used for address computation when
16986 /// selecting load and store instructions. The flags computed are stored in
16987 /// FlagSet. This function takes into account whether the node is a constant,
16988 /// an ADD, OR, or a constant, and computes the address flags accordingly.
16989 static void computeFlagsForAddressComputation(SDValue N, unsigned &FlagSet,
16990                                               SelectionDAG &DAG) {
16991   // Set the alignment flags for the node depending on if the node is
16992   // 4-byte or 16-byte aligned.
16993   auto SetAlignFlagsForImm = [&](uint64_t Imm) {
16994     if ((Imm & 0x3) == 0)
16995       FlagSet |= PPC::MOF_RPlusSImm16Mult4;
16996     if ((Imm & 0xf) == 0)
16997       FlagSet |= PPC::MOF_RPlusSImm16Mult16;
16998   };
16999 
17000   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
17001     // All 32-bit constants can be computed as LIS + Disp.
17002     const APInt &ConstImm = CN->getAPIntValue();
17003     if (ConstImm.isSignedIntN(32)) { // Flag to handle 32-bit constants.
17004       FlagSet |= PPC::MOF_AddrIsSImm32;
17005       SetAlignFlagsForImm(ConstImm.getZExtValue());
17006       setAlignFlagsForFI(N, FlagSet, DAG);
17007     }
17008     if (ConstImm.isSignedIntN(34)) // Flag to handle 34-bit constants.
17009       FlagSet |= PPC::MOF_RPlusSImm34;
17010     else // Let constant materialization handle large constants.
17011       FlagSet |= PPC::MOF_NotAddNorCst;
17012   } else if (N.getOpcode() == ISD::ADD || provablyDisjointOr(DAG, N)) {
17013     // This address can be represented as an addition of:
17014     // - Register + Imm16 (possibly a multiple of 4/16)
17015     // - Register + Imm34
17016     // - Register + PPCISD::Lo
17017     // - Register + Register
17018     // In any case, we won't have to match this as Base + Zero.
17019     SDValue RHS = N.getOperand(1);
17020     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
17021       const APInt &ConstImm = CN->getAPIntValue();
17022       if (ConstImm.isSignedIntN(16)) {
17023         FlagSet |= PPC::MOF_RPlusSImm16; // Signed 16-bit immediates.
17024         SetAlignFlagsForImm(ConstImm.getZExtValue());
17025         setAlignFlagsForFI(N, FlagSet, DAG);
17026       }
17027       if (ConstImm.isSignedIntN(34))
17028         FlagSet |= PPC::MOF_RPlusSImm34; // Signed 34-bit immediates.
17029       else
17030         FlagSet |= PPC::MOF_RPlusR; // Register.
17031     } else if (RHS.getOpcode() == PPCISD::Lo &&
17032                !cast<ConstantSDNode>(RHS.getOperand(1))->getZExtValue())
17033       FlagSet |= PPC::MOF_RPlusLo; // PPCISD::Lo.
17034     else
17035       FlagSet |= PPC::MOF_RPlusR;
17036   } else { // The address computation is not a constant or an addition.
17037     setAlignFlagsForFI(N, FlagSet, DAG);
17038     FlagSet |= PPC::MOF_NotAddNorCst;
17039   }
17040 }
17041 
17042 /// computeMOFlags - Given a node N and it's Parent (a MemSDNode), compute
17043 /// the address flags of the load/store instruction that is to be matched.
17044 unsigned PPCTargetLowering::computeMOFlags(const SDNode *Parent, SDValue N,
17045                                            SelectionDAG &DAG) const {
17046   unsigned FlagSet = PPC::MOF_None;
17047 
17048   // Compute subtarget flags.
17049   if (!Subtarget.hasP9Vector())
17050     FlagSet |= PPC::MOF_SubtargetBeforeP9;
17051   else {
17052     FlagSet |= PPC::MOF_SubtargetP9;
17053     if (Subtarget.hasPrefixInstrs())
17054       FlagSet |= PPC::MOF_SubtargetP10;
17055   }
17056   if (Subtarget.hasSPE())
17057     FlagSet |= PPC::MOF_SubtargetSPE;
17058 
17059   // Mark this as something we don't want to handle here if it is atomic
17060   // or pre-increment instruction.
17061   if (const LSBaseSDNode *LSB = dyn_cast<LSBaseSDNode>(Parent))
17062     if (LSB->isIndexed())
17063       return PPC::MOF_None;
17064   if (isa<AtomicSDNode>(Parent))
17065     return PPC::MOF_None;
17066 
17067   // Compute in-memory type flags. This is based on if there are scalars,
17068   // floats or vectors.
17069   const MemSDNode *MN = dyn_cast<MemSDNode>(Parent);
17070   assert(MN && "Parent should be a MemSDNode!");
17071   EVT MemVT = MN->getMemoryVT();
17072   unsigned Size = MemVT.getSizeInBits();
17073   if (MemVT.isScalarInteger()) {
17074     assert(Size <= 64 && "Not expecting scalar integers larger than 8 bytes!");
17075     if (Size < 32)
17076       FlagSet |= PPC::MOF_SubWordInt;
17077     else if (Size == 32)
17078       FlagSet |= PPC::MOF_WordInt;
17079     else
17080       FlagSet |= PPC::MOF_DoubleWordInt;
17081   } else if (MemVT.isVector() && !MemVT.isFloatingPoint()) { // Integer vectors.
17082     if (Size == 128)
17083       FlagSet |= PPC::MOF_Vector;
17084     else if (Size == 256)
17085       FlagSet |= PPC::MOF_Vector256;
17086     else
17087       llvm_unreachable("Not expecting illegal vectors!");
17088   } else { // Floating point type: can be scalar, f128 or vector types.
17089     if (Size == 32 || Size == 64)
17090       FlagSet |= PPC::MOF_ScalarFloat;
17091     else if (MemVT == MVT::f128 || MemVT.isVector())
17092       FlagSet |= PPC::MOF_Vector;
17093     else
17094       llvm_unreachable("Not expecting illegal scalar floats!");
17095   }
17096 
17097   // Compute flags for address computation.
17098   computeFlagsForAddressComputation(N, FlagSet, DAG);
17099 
17100   // Compute type extension flags.
17101   if (const LoadSDNode *LN = dyn_cast<LoadSDNode>(Parent)) {
17102     switch (LN->getExtensionType()) {
17103     case ISD::SEXTLOAD:
17104       FlagSet |= PPC::MOF_SExt;
17105       break;
17106     case ISD::EXTLOAD:
17107     case ISD::ZEXTLOAD:
17108       FlagSet |= PPC::MOF_ZExt;
17109       break;
17110     case ISD::NON_EXTLOAD:
17111       FlagSet |= PPC::MOF_NoExt;
17112       break;
17113     }
17114   } else
17115     FlagSet |= PPC::MOF_NoExt;
17116 
17117   // For integers, no extension is the same as zero extension.
17118   // We set the extension mode to zero extension so we don't have
17119   // to add separate entries in AddrModesMap for loads and stores.
17120   if (MemVT.isScalarInteger() && (FlagSet & PPC::MOF_NoExt)) {
17121     FlagSet |= PPC::MOF_ZExt;
17122     FlagSet &= ~PPC::MOF_NoExt;
17123   }
17124 
17125   // If we don't have prefixed instructions, 34-bit constants should be
17126   // treated as PPC::MOF_NotAddNorCst so they can match D-Forms.
17127   bool IsNonP1034BitConst =
17128       ((PPC::MOF_RPlusSImm34 | PPC::MOF_AddrIsSImm32 | PPC::MOF_SubtargetP10) &
17129        FlagSet) == PPC::MOF_RPlusSImm34;
17130   if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::OR &&
17131       IsNonP1034BitConst)
17132     FlagSet |= PPC::MOF_NotAddNorCst;
17133 
17134   return FlagSet;
17135 }
17136 
17137 /// SelectForceXFormMode - Given the specified address, force it to be
17138 /// represented as an indexed [r+r] operation (an XForm instruction).
17139 PPC::AddrMode PPCTargetLowering::SelectForceXFormMode(SDValue N, SDValue &Disp,
17140                                                       SDValue &Base,
17141                                                       SelectionDAG &DAG) const {
17142 
17143   PPC::AddrMode Mode = PPC::AM_XForm;
17144   int16_t ForceXFormImm = 0;
17145   if (provablyDisjointOr(DAG, N) &&
17146       !isIntS16Immediate(N.getOperand(1), ForceXFormImm)) {
17147     Disp = N.getOperand(0);
17148     Base = N.getOperand(1);
17149     return Mode;
17150   }
17151 
17152   // If the address is the result of an add, we will utilize the fact that the
17153   // address calculation includes an implicit add.  However, we can reduce
17154   // register pressure if we do not materialize a constant just for use as the
17155   // index register.  We only get rid of the add if it is not an add of a
17156   // value and a 16-bit signed constant and both have a single use.
17157   if (N.getOpcode() == ISD::ADD &&
17158       (!isIntS16Immediate(N.getOperand(1), ForceXFormImm) ||
17159        !N.getOperand(1).hasOneUse() || !N.getOperand(0).hasOneUse())) {
17160     Disp = N.getOperand(0);
17161     Base = N.getOperand(1);
17162     return Mode;
17163   }
17164 
17165   // Otherwise, use R0 as the base register.
17166   Disp = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
17167                          N.getValueType());
17168   Base = N;
17169 
17170   return Mode;
17171 }
17172 
17173 /// SelectOptimalAddrMode - Based on a node N and it's Parent (a MemSDNode),
17174 /// compute the address flags of the node, get the optimal address mode based
17175 /// on the flags, and set the Base and Disp based on the address mode.
17176 PPC::AddrMode PPCTargetLowering::SelectOptimalAddrMode(const SDNode *Parent,
17177                                                        SDValue N, SDValue &Disp,
17178                                                        SDValue &Base,
17179                                                        SelectionDAG &DAG,
17180                                                        MaybeAlign Align) const {
17181   SDLoc DL(Parent);
17182 
17183   // Compute the address flags.
17184   unsigned Flags = computeMOFlags(Parent, N, DAG);
17185 
17186   // Get the optimal address mode based on the Flags.
17187   PPC::AddrMode Mode = getAddrModeForFlags(Flags);
17188 
17189   // Set Base and Disp accordingly depending on the address mode.
17190   switch (Mode) {
17191   case PPC::AM_DForm:
17192   case PPC::AM_DSForm:
17193   case PPC::AM_DQForm: {
17194     // This is a register plus a 16-bit immediate. The base will be the
17195     // register and the displacement will be the immediate unless it
17196     // isn't sufficiently aligned.
17197     if (Flags & PPC::MOF_RPlusSImm16) {
17198       SDValue Op0 = N.getOperand(0);
17199       SDValue Op1 = N.getOperand(1);
17200       ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1);
17201       int16_t Imm = CN->getAPIntValue().getZExtValue();
17202       if (!Align || isAligned(*Align, Imm)) {
17203         Disp = DAG.getTargetConstant(Imm, DL, N.getValueType());
17204         Base = Op0;
17205         if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op0)) {
17206           Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
17207           fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
17208         }
17209         break;
17210       }
17211     }
17212     // This is a register plus the @lo relocation. The base is the register
17213     // and the displacement is the global address.
17214     else if (Flags & PPC::MOF_RPlusLo) {
17215       Disp = N.getOperand(1).getOperand(0); // The global address.
17216       assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
17217              Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
17218              Disp.getOpcode() == ISD::TargetConstantPool ||
17219              Disp.getOpcode() == ISD::TargetJumpTable);
17220       Base = N.getOperand(0);
17221       break;
17222     }
17223     // This is a constant address at most 32 bits. The base will be
17224     // zero or load-immediate-shifted and the displacement will be
17225     // the low 16 bits of the address.
17226     else if (Flags & PPC::MOF_AddrIsSImm32) {
17227       ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
17228       EVT CNType = CN->getValueType(0);
17229       uint64_t CNImm = CN->getZExtValue();
17230       // If this address fits entirely in a 16-bit sext immediate field, codegen
17231       // this as "d, 0".
17232       int16_t Imm;
17233       if (isIntS16Immediate(CN, Imm) && (!Align || isAligned(*Align, Imm))) {
17234         Disp = DAG.getTargetConstant(Imm, DL, CNType);
17235         Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
17236                                CNType);
17237         break;
17238       }
17239       // Handle 32-bit sext immediate with LIS + Addr mode.
17240       if ((CNType == MVT::i32 || isInt<32>(CNImm)) &&
17241           (!Align || isAligned(*Align, CNImm))) {
17242         int32_t Addr = (int32_t)CNImm;
17243         // Otherwise, break this down into LIS + Disp.
17244         Disp = DAG.getTargetConstant((int16_t)Addr, DL, MVT::i32);
17245         Base =
17246             DAG.getTargetConstant((Addr - (int16_t)Addr) >> 16, DL, MVT::i32);
17247         uint32_t LIS = CNType == MVT::i32 ? PPC::LIS : PPC::LIS8;
17248         Base = SDValue(DAG.getMachineNode(LIS, DL, CNType, Base), 0);
17249         break;
17250       }
17251     }
17252     // Otherwise, the PPC:MOF_NotAdd flag is set. Load/Store is Non-foldable.
17253     Disp = DAG.getTargetConstant(0, DL, getPointerTy(DAG.getDataLayout()));
17254     if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
17255       Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
17256       fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
17257     } else
17258       Base = N;
17259     break;
17260   }
17261   case PPC::AM_None:
17262     break;
17263   default: { // By default, X-Form is always available to be selected.
17264     // When a frame index is not aligned, we also match by XForm.
17265     FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N);
17266     Base = FI ? N : N.getOperand(1);
17267     Disp = FI ? DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
17268                                 N.getValueType())
17269               : N.getOperand(0);
17270     break;
17271   }
17272   }
17273   return Mode;
17274 }
17275