| Nate Begeman | 0b71e00 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 1 | //===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===// |
| Chris Lattner | f22556d | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| Chris Lattner | f22556d | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| Nate Begeman | 6cca84e | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 10 | // This file implements the PPCISelLowering class. |
| Chris Lattner | f22556d | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| Chris Lattner | 6f3b954 | 2005-10-14 23:59:06 +0000 | [diff] [blame] | 14 | #include "PPCISelLowering.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/PPCPredicates.h" |
| Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 16 | #include "PPCCallingConv.h" |
| Strahinja Petrovic | e682b80 | 2016-05-09 12:27:39 +0000 | [diff] [blame] | 17 | #include "PPCCCState.h" |
| Jim Laskey | 48850c1 | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 18 | #include "PPCMachineFunctionInfo.h" |
| Bill Wendling | dd3fe94 | 2010-03-12 02:00:43 +0000 | [diff] [blame] | 19 | #include "PPCPerfectShuffle.h" |
| Chris Lattner | 6f3b954 | 2005-10-14 23:59:06 +0000 | [diff] [blame] | 20 | #include "PPCTargetMachine.h" |
| Bill Schmidt | 22d40dc | 2013-05-13 19:34:37 +0000 | [diff] [blame] | 21 | #include "PPCTargetObjectFile.h" |
| Owen Anderson | e2f23a3 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
| Chuang-Yu Cheng | 2e5973e | 2016-04-06 02:04:38 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
| Hal Finkel | 0d8db46 | 2014-05-11 19:29:11 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringSwitch.h" |
| Eric Christopher | 8995833 | 2014-05-31 00:07:32 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Triple.h" |
| Chris Lattner | 4f2e4e0 | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/CallingConvLower.h" |
| Chris Lattner | f22556d | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 28 | #include "llvm/CodeGen/MachineFunction.h" |
| Chris Lattner | 9b577f1 | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| Joerg Sonnenberger | 8c1a9ac | 2016-11-16 00:37:30 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| Hal Finkel | 5772566 | 2015-01-03 17:58:24 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| Chris Lattner | a10fff5 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| Chris Lattner | f22556d | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/SelectionDAG.h" |
| Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 35 | #include "llvm/IR/CallingConv.h" |
| 36 | #include "llvm/IR/Constants.h" |
| 37 | #include "llvm/IR/DerivedTypes.h" |
| 38 | #include "llvm/IR/Function.h" |
| 39 | #include "llvm/IR/Intrinsics.h" |
| Chris Lattner | ce64554 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 40 | #include "llvm/Support/CommandLine.h" |
| Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 41 | #include "llvm/Support/ErrorHandling.h" |
| Chuang-Yu Cheng | 2e5973e | 2016-04-06 02:04:38 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Format.h" |
| Craig Topper | b25fda9 | 2012-03-17 18:46:09 +0000 | [diff] [blame] | 43 | #include "llvm/Support/MathExtras.h" |
| Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 44 | #include "llvm/Support/raw_ostream.h" |
| Craig Topper | b25fda9 | 2012-03-17 18:46:09 +0000 | [diff] [blame] | 45 | #include "llvm/Target/TargetOptions.h" |
| Hal Finkel | 1fb10e8 | 2016-05-12 04:00:56 +0000 | [diff] [blame] | 46 | #include <list> |
| Kit Barton | d4eb73c | 2015-05-05 16:10:44 +0000 | [diff] [blame] | 47 | |
| Chris Lattner | f22556d | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 48 | using namespace llvm; |
| 49 | |
| Chuang-Yu Cheng | 2e5973e | 2016-04-06 02:04:38 +0000 | [diff] [blame] | 50 | #define DEBUG_TYPE "ppc-lowering" |
| 51 | |
| Hal Finkel | 595817e | 2012-06-04 02:21:00 +0000 | [diff] [blame] | 52 | static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc", |
| 53 | cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden); |
| Chris Lattner | ce64554 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 54 | |
| Hal Finkel | 4e9f1a8 | 2012-06-10 19:32:29 +0000 | [diff] [blame] | 55 | static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref", |
| 56 | cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden); |
| 57 | |
| Hal Finkel | 8d7fbc9 | 2013-03-15 15:27:13 +0000 | [diff] [blame] | 58 | static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned", |
| 59 | cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden); |
| 60 | |
| Chuang-Yu Cheng | 0600e8d | 2016-04-26 07:38:24 +0000 | [diff] [blame] | 61 | static cl::opt<bool> DisableSCO("disable-ppc-sco", |
| Chuang-Yu Cheng | 2e5973e | 2016-04-06 02:04:38 +0000 | [diff] [blame] | 62 | cl::desc("disable sibling call optimization on ppc"), cl::Hidden); |
| 63 | |
| 64 | STATISTIC(NumTailCalls, "Number of tail calls"); |
| 65 | STATISTIC(NumSiblingCalls, "Number of sibling calls"); |
| 66 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 67 | // FIXME: Remove this once the bug has been fixed! |
| 68 | extern cl::opt<bool> ANDIGlueBug; |
| 69 | |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 70 | PPCTargetLowering::PPCTargetLowering(const PPCTargetMachine &TM, |
| 71 | const PPCSubtarget &STI) |
| 72 | : TargetLowering(TM), Subtarget(STI) { |
| Chris Lattner | a028e7a | 2005-09-27 22:18:25 +0000 | [diff] [blame] | 73 | // Use _setjmp/_longjmp instead of setjmp/longjmp. |
| Anton Korobeynikov | 3b7c257 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 74 | setUseUnderscoreSetJmp(true); |
| 75 | setUseUnderscoreLongJmp(true); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 76 | |
| Chris Lattner | d10babf | 2010-10-10 18:34:00 +0000 | [diff] [blame] | 77 | // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all |
| 78 | // arguments are at least 4/8 bytes aligned. |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 79 | bool isPPC64 = Subtarget.isPPC64(); |
| Evan Cheng | 39e9002 | 2012-07-02 22:39:56 +0000 | [diff] [blame] | 80 | setMinStackArgumentAlignment(isPPC64 ? 8:4); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 81 | |
| Chris Lattner | f22556d | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 82 | // Set up the register classes. |
| Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 83 | addRegisterClass(MVT::i32, &PPC::GPRCRegClass); |
| Eric Christopher | 317df66 | 2016-07-07 01:49:57 +0000 | [diff] [blame] | 84 | if (!useSoftFloat()) { |
| Petar Jovanovic | 280f710 | 2015-12-14 17:57:33 +0000 | [diff] [blame] | 85 | addRegisterClass(MVT::f32, &PPC::F4RCRegClass); |
| 86 | addRegisterClass(MVT::f64, &PPC::F8RCRegClass); |
| 87 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 88 | |
| Evan Cheng | 5d9fd97 | 2006-10-04 00:56:09 +0000 | [diff] [blame] | 89 | // PowerPC has an i16 but no i8 (or i1) SEXTLOAD |
| Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 90 | for (MVT VT : MVT::integer_valuetypes()) { |
| 91 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); |
| 92 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Expand); |
| 93 | } |
| Duncan Sands | 95d46ef | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 94 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 95 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 96 | |
| Chris Lattner | c9fa36d | 2006-11-10 23:58:45 +0000 | [diff] [blame] | 97 | // PowerPC has pre-inc load and store's. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 98 | setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal); |
| 99 | setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal); |
| 100 | setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal); |
| 101 | setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal); |
| 102 | setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal); |
| Hal Finkel | 65d1cbf | 2015-02-05 18:42:53 +0000 | [diff] [blame] | 103 | setIndexedLoadAction(ISD::PRE_INC, MVT::f32, Legal); |
| 104 | setIndexedLoadAction(ISD::PRE_INC, MVT::f64, Legal); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 105 | setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal); |
| 106 | setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal); |
| 107 | setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal); |
| 108 | setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal); |
| 109 | setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal); |
| Hal Finkel | 65d1cbf | 2015-02-05 18:42:53 +0000 | [diff] [blame] | 110 | setIndexedStoreAction(ISD::PRE_INC, MVT::f32, Legal); |
| 111 | setIndexedStoreAction(ISD::PRE_INC, MVT::f64, Legal); |
| Evan Cheng | 36a8fbf | 2006-11-09 19:11:50 +0000 | [diff] [blame] | 112 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 113 | if (Subtarget.useCRBits()) { |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 114 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); |
| 115 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 116 | if (isPPC64 || Subtarget.hasFPCVT()) { |
| Hal Finkel | 6a56b21 | 2014-03-05 22:14:00 +0000 | [diff] [blame] | 117 | setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote); |
| 118 | AddPromotedToType (ISD::SINT_TO_FP, MVT::i1, |
| 119 | isPPC64 ? MVT::i64 : MVT::i32); |
| 120 | setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote); |
| NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 121 | AddPromotedToType(ISD::UINT_TO_FP, MVT::i1, |
| 122 | isPPC64 ? MVT::i64 : MVT::i32); |
| Hal Finkel | 6a56b21 | 2014-03-05 22:14:00 +0000 | [diff] [blame] | 123 | } else { |
| 124 | setOperationAction(ISD::SINT_TO_FP, MVT::i1, Custom); |
| 125 | setOperationAction(ISD::UINT_TO_FP, MVT::i1, Custom); |
| 126 | } |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 127 | |
| 128 | // PowerPC does not support direct load / store of condition registers |
| 129 | setOperationAction(ISD::LOAD, MVT::i1, Custom); |
| 130 | setOperationAction(ISD::STORE, MVT::i1, Custom); |
| 131 | |
| 132 | // FIXME: Remove this once the ANDI glue bug is fixed: |
| 133 | if (ANDIGlueBug) |
| 134 | setOperationAction(ISD::TRUNCATE, MVT::i1, Custom); |
| 135 | |
| Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 136 | for (MVT VT : MVT::integer_valuetypes()) { |
| 137 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); |
| 138 | setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote); |
| 139 | setTruncStoreAction(VT, MVT::i1, Expand); |
| 140 | } |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 141 | |
| 142 | addRegisterClass(MVT::i1, &PPC::CRBITRCRegClass); |
| 143 | } |
| 144 | |
| Dale Johannesen | 666323e | 2007-10-10 01:01:31 +0000 | [diff] [blame] | 145 | // This is used in the ppcf128->int sequence. Note it has different semantics |
| 146 | // from FP_ROUND: that rounds to nearest, this rounds to zero. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 147 | setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom); |
| Dale Johannesen | f864ac9 | 2007-10-06 01:24:11 +0000 | [diff] [blame] | 148 | |
| Roman Divacky | 1faf5b0 | 2012-08-16 18:19:29 +0000 | [diff] [blame] | 149 | // We do not currently implement these libm ops for PowerPC. |
| Owen Anderson | 0b9b9da | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 150 | setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand); |
| 151 | setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand); |
| 152 | setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand); |
| 153 | setOperationAction(ISD::FRINT, MVT::ppcf128, Expand); |
| 154 | setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand); |
| Bill Schmidt | 92e2664 | 2013-04-03 13:05:44 +0000 | [diff] [blame] | 155 | setOperationAction(ISD::FREM, MVT::ppcf128, Expand); |
| Owen Anderson | 0b9b9da | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 156 | |
| Chris Lattner | f22556d | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 157 | // PowerPC has no SREM/UREM instructions |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 158 | setOperationAction(ISD::SREM, MVT::i32, Expand); |
| 159 | setOperationAction(ISD::UREM, MVT::i32, Expand); |
| 160 | setOperationAction(ISD::SREM, MVT::i64, Expand); |
| 161 | setOperationAction(ISD::UREM, MVT::i64, Expand); |
| Dan Gohman | 71f0d7d | 2007-10-08 17:28:24 +0000 | [diff] [blame] | 162 | |
| 163 | // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 164 | setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); |
| 165 | setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); |
| 166 | setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand); |
| 167 | setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand); |
| 168 | setOperationAction(ISD::UDIVREM, MVT::i32, Expand); |
| 169 | setOperationAction(ISD::SDIVREM, MVT::i32, Expand); |
| 170 | setOperationAction(ISD::UDIVREM, MVT::i64, Expand); |
| 171 | setOperationAction(ISD::SDIVREM, MVT::i64, Expand); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 172 | |
| Dan Gohman | 482732a | 2007-10-11 23:21:31 +0000 | [diff] [blame] | 173 | // We don't support sin/cos/sqrt/fmod/pow |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 174 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 175 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
| Evan Cheng | 0e88c7d | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 176 | setOperationAction(ISD::FSINCOS, MVT::f64, Expand); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 177 | setOperationAction(ISD::FREM , MVT::f64, Expand); |
| 178 | setOperationAction(ISD::FPOW , MVT::f64, Expand); |
| Hal Finkel | 0a479ae | 2012-06-22 00:49:52 +0000 | [diff] [blame] | 179 | setOperationAction(ISD::FMA , MVT::f64, Legal); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 180 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 181 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
| Evan Cheng | 0e88c7d | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 182 | setOperationAction(ISD::FSINCOS, MVT::f32, Expand); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 183 | setOperationAction(ISD::FREM , MVT::f32, Expand); |
| 184 | setOperationAction(ISD::FPOW , MVT::f32, Expand); |
| Hal Finkel | 0a479ae | 2012-06-22 00:49:52 +0000 | [diff] [blame] | 185 | setOperationAction(ISD::FMA , MVT::f32, Legal); |
| Dale Johannesen | 5c94cb3 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 186 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 187 | setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 188 | |
| Chris Lattner | f22556d | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 189 | // If we're enabling GP optimizations, use hardware square root |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 190 | if (!Subtarget.hasFSQRT() && |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 191 | !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTE() && |
| 192 | Subtarget.hasFRE())) |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 193 | setOperationAction(ISD::FSQRT, MVT::f64, Expand); |
| Hal Finkel | 2e10331 | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 194 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 195 | if (!Subtarget.hasFSQRT() && |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 196 | !(TM.Options.UnsafeFPMath && Subtarget.hasFRSQRTES() && |
| 197 | Subtarget.hasFRES())) |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 198 | setOperationAction(ISD::FSQRT, MVT::f32, Expand); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 199 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 200 | if (Subtarget.hasFCPSGN()) { |
| Hal Finkel | dbc78e1 | 2013-08-19 05:01:02 +0000 | [diff] [blame] | 201 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Legal); |
| 202 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Legal); |
| 203 | } else { |
| 204 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 205 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); |
| 206 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 207 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 208 | if (Subtarget.hasFPRND()) { |
| Hal Finkel | c20a08d | 2013-03-29 08:57:48 +0000 | [diff] [blame] | 209 | setOperationAction(ISD::FFLOOR, MVT::f64, Legal); |
| 210 | setOperationAction(ISD::FCEIL, MVT::f64, Legal); |
| 211 | setOperationAction(ISD::FTRUNC, MVT::f64, Legal); |
| Hal Finkel | 2b7b2f3 | 2013-08-08 04:31:34 +0000 | [diff] [blame] | 212 | setOperationAction(ISD::FROUND, MVT::f64, Legal); |
| Hal Finkel | c20a08d | 2013-03-29 08:57:48 +0000 | [diff] [blame] | 213 | |
| 214 | setOperationAction(ISD::FFLOOR, MVT::f32, Legal); |
| 215 | setOperationAction(ISD::FCEIL, MVT::f32, Legal); |
| 216 | setOperationAction(ISD::FTRUNC, MVT::f32, Legal); |
| Hal Finkel | 2b7b2f3 | 2013-08-08 04:31:34 +0000 | [diff] [blame] | 217 | setOperationAction(ISD::FROUND, MVT::f32, Legal); |
| Hal Finkel | c20a08d | 2013-03-29 08:57:48 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| Nemanja Ivanovic | 6f22b41 | 2016-09-27 08:42:12 +0000 | [diff] [blame] | 220 | // PowerPC does not have BSWAP |
| 221 | // CTPOP or CTTZ were introduced in P8/P9 respectivelly |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 222 | setOperationAction(ISD::BSWAP, MVT::i32 , Expand); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 223 | setOperationAction(ISD::BSWAP, MVT::i64 , Expand); |
| Nemanja Ivanovic | 6f22b41 | 2016-09-27 08:42:12 +0000 | [diff] [blame] | 224 | if (Subtarget.isISA3_0()) { |
| 225 | setOperationAction(ISD::CTTZ , MVT::i32 , Legal); |
| 226 | setOperationAction(ISD::CTTZ , MVT::i64 , Legal); |
| 227 | } else { |
| 228 | setOperationAction(ISD::CTTZ , MVT::i32 , Expand); |
| 229 | setOperationAction(ISD::CTTZ , MVT::i64 , Expand); |
| 230 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 231 | |
| Hal Finkel | fa7057a | 2016-03-29 01:36:01 +0000 | [diff] [blame] | 232 | if (Subtarget.hasPOPCNTD() == PPCSubtarget::POPCNTD_Fast) { |
| Hal Finkel | 290376d | 2013-04-01 15:58:15 +0000 | [diff] [blame] | 233 | setOperationAction(ISD::CTPOP, MVT::i32 , Legal); |
| Hal Finkel | a4d0748 | 2013-03-28 13:29:47 +0000 | [diff] [blame] | 234 | setOperationAction(ISD::CTPOP, MVT::i64 , Legal); |
| 235 | } else { |
| 236 | setOperationAction(ISD::CTPOP, MVT::i32 , Expand); |
| 237 | setOperationAction(ISD::CTPOP, MVT::i64 , Expand); |
| 238 | } |
| 239 | |
| Nate Begeman | 1b8121b | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 240 | // PowerPC does not have ROTR |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 241 | setOperationAction(ISD::ROTR, MVT::i32 , Expand); |
| 242 | setOperationAction(ISD::ROTR, MVT::i64 , Expand); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 243 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 244 | if (!Subtarget.useCRBits()) { |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 245 | // PowerPC does not have Select |
| 246 | setOperationAction(ISD::SELECT, MVT::i32, Expand); |
| 247 | setOperationAction(ISD::SELECT, MVT::i64, Expand); |
| 248 | setOperationAction(ISD::SELECT, MVT::f32, Expand); |
| 249 | setOperationAction(ISD::SELECT, MVT::f64, Expand); |
| 250 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 251 | |
| Chris Lattner | 7f1fa8e | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 252 | // PowerPC wants to turn select_cc of FP into fsel when possible. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 253 | setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); |
| 254 | setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); |
| Nate Begeman | a162f20 | 2006-01-31 08:17:29 +0000 | [diff] [blame] | 255 | |
| Nate Begeman | 7e7f439 | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 256 | // PowerPC wants to optimize integer setcc a bit |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 257 | if (!Subtarget.useCRBits()) |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 258 | setOperationAction(ISD::SETCC, MVT::i32, Custom); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 259 | |
| Nate Begeman | bb01d4f | 2006-03-17 01:40:33 +0000 | [diff] [blame] | 260 | // PowerPC does not have BRCOND which requires SetCC |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 261 | if (!Subtarget.useCRBits()) |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 262 | setOperationAction(ISD::BRCOND, MVT::Other, Expand); |
| Evan Cheng | 0d41d19 | 2006-10-30 08:02:39 +0000 | [diff] [blame] | 263 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 264 | setOperationAction(ISD::BR_JT, MVT::Other, Expand); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 265 | |
| Chris Lattner | da2e04c | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 266 | // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 267 | setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); |
| Nate Begeman | 6095214 | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 268 | |
| Jim Laskey | 6267b2c | 2005-08-17 00:40:22 +0000 | [diff] [blame] | 269 | // PowerPC does not have [U|S]INT_TO_FP |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 270 | setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand); |
| 271 | setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand); |
| Jim Laskey | 6267b2c | 2005-08-17 00:40:22 +0000 | [diff] [blame] | 272 | |
| Nemanja Ivanovic | 5ebc92d | 2016-03-24 13:40:33 +0000 | [diff] [blame] | 273 | if (Subtarget.hasDirectMove() && isPPC64) { |
| Nemanja Ivanovic | 8922476 | 2015-12-15 14:50:34 +0000 | [diff] [blame] | 274 | setOperationAction(ISD::BITCAST, MVT::f32, Legal); |
| 275 | setOperationAction(ISD::BITCAST, MVT::i32, Legal); |
| 276 | setOperationAction(ISD::BITCAST, MVT::i64, Legal); |
| 277 | setOperationAction(ISD::BITCAST, MVT::f64, Legal); |
| 278 | } else { |
| 279 | setOperationAction(ISD::BITCAST, MVT::f32, Expand); |
| 280 | setOperationAction(ISD::BITCAST, MVT::i32, Expand); |
| 281 | setOperationAction(ISD::BITCAST, MVT::i64, Expand); |
| 282 | setOperationAction(ISD::BITCAST, MVT::f64, Expand); |
| 283 | } |
| Chris Lattner | c46fc24 | 2005-12-23 05:13:35 +0000 | [diff] [blame] | 284 | |
| Chris Lattner | 84b49d5 | 2006-04-28 21:56:10 +0000 | [diff] [blame] | 285 | // We cannot sextinreg(i1). Expand to shifts. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 286 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); |
| Jim Laskey | e0008e2 | 2007-02-22 14:56:36 +0000 | [diff] [blame] | 287 | |
| Hal Finkel | 1996f3d | 2013-03-27 19:10:42 +0000 | [diff] [blame] | 288 | // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 289 | // SjLj exception handling but a light-weight setjmp/longjmp replacement to |
| 290 | // support continuation, user-level threading, and etc.. As a result, no |
| 291 | // other SjLj exception interfaces are implemented and please don't build |
| 292 | // your own exception handling based on them. |
| 293 | // LLVM/Clang supports zero-cost DWARF exception handling. |
| 294 | setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom); |
| 295 | setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 296 | |
| 297 | // We want to legalize GlobalAddress and ConstantPool nodes into the |
| Nate Begeman | 4e56db6 | 2005-12-10 02:36:00 +0000 | [diff] [blame] | 298 | // appropriate instructions to materialize the address. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 299 | setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); |
| 300 | setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); |
| Bob Wilson | f84f710 | 2009-11-04 21:31:18 +0000 | [diff] [blame] | 301 | setOperationAction(ISD::BlockAddress, MVT::i32, Custom); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 302 | setOperationAction(ISD::ConstantPool, MVT::i32, Custom); |
| 303 | setOperationAction(ISD::JumpTable, MVT::i32, Custom); |
| 304 | setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); |
| 305 | setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom); |
| Bob Wilson | f84f710 | 2009-11-04 21:31:18 +0000 | [diff] [blame] | 306 | setOperationAction(ISD::BlockAddress, MVT::i64, Custom); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 307 | setOperationAction(ISD::ConstantPool, MVT::i64, Custom); |
| 308 | setOperationAction(ISD::JumpTable, MVT::i64, Custom); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 309 | |
| Nate Begeman | f69d13b | 2008-08-11 17:36:31 +0000 | [diff] [blame] | 310 | // TRAP is legal. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 311 | setOperationAction(ISD::TRAP, MVT::Other, Legal); |
| Bill Wendling | 95e1af2 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 312 | |
| 313 | // TRAMPOLINE is custom lowered. |
| Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 314 | setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom); |
| 315 | setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom); |
| Bill Wendling | 95e1af2 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 316 | |
| Nate Begeman | e74795c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 317 | // VASTART needs to be custom lowered to use the VarArgsFrameIndex |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 318 | setOperationAction(ISD::VASTART , MVT::Other, Custom); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 319 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 320 | if (Subtarget.isSVR4ABI()) { |
| Evan Cheng | 39e9002 | 2012-07-02 22:39:56 +0000 | [diff] [blame] | 321 | if (isPPC64) { |
| Hal Finkel | e44eb28 | 2012-03-24 03:53:55 +0000 | [diff] [blame] | 322 | // VAARG always uses double-word chunks, so promote anything smaller. |
| 323 | setOperationAction(ISD::VAARG, MVT::i1, Promote); |
| 324 | AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64); |
| 325 | setOperationAction(ISD::VAARG, MVT::i8, Promote); |
| 326 | AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64); |
| 327 | setOperationAction(ISD::VAARG, MVT::i16, Promote); |
| 328 | AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64); |
| 329 | setOperationAction(ISD::VAARG, MVT::i32, Promote); |
| 330 | AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64); |
| 331 | setOperationAction(ISD::VAARG, MVT::Other, Expand); |
| 332 | } else { |
| 333 | // VAARG is custom lowered with the 32-bit SVR4 ABI. |
| 334 | setOperationAction(ISD::VAARG, MVT::Other, Custom); |
| 335 | setOperationAction(ISD::VAARG, MVT::i64, Custom); |
| 336 | } |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 337 | } else |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 338 | setOperationAction(ISD::VAARG, MVT::Other, Expand); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 339 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 340 | if (Subtarget.isSVR4ABI() && !isPPC64) |
| Roman Divacky | c3825df | 2013-07-25 21:36:47 +0000 | [diff] [blame] | 341 | // VACOPY is custom lowered with the 32-bit SVR4 ABI. |
| 342 | setOperationAction(ISD::VACOPY , MVT::Other, Custom); |
| 343 | else |
| 344 | setOperationAction(ISD::VACOPY , MVT::Other, Expand); |
| 345 | |
| Chris Lattner | 5bd514d | 2006-01-15 09:02:48 +0000 | [diff] [blame] | 346 | // Use the default implementation. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 347 | setOperationAction(ISD::VAEND , MVT::Other, Expand); |
| 348 | setOperationAction(ISD::STACKSAVE , MVT::Other, Expand); |
| 349 | setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom); |
| 350 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom); |
| 351 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom); |
| Yury Gribov | d7dbb66 | 2015-12-01 11:40:55 +0000 | [diff] [blame] | 352 | setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, MVT::i32, Custom); |
| 353 | setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, MVT::i64, Custom); |
| Hal Finkel | 5081ac2 | 2016-09-01 10:28:47 +0000 | [diff] [blame] | 354 | setOperationAction(ISD::EH_DWARF_CFA, MVT::i32, Custom); |
| 355 | setOperationAction(ISD::EH_DWARF_CFA, MVT::i64, Custom); |
| Chris Lattner | ab4df834 | 2006-10-18 01:18:48 +0000 | [diff] [blame] | 356 | |
| Chris Lattner | 6961fc7 | 2006-03-26 10:06:40 +0000 | [diff] [blame] | 357 | // We want to custom lower some of our intrinsics. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 358 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 359 | |
| Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 360 | // To handle counter-based loop conditions. |
| 361 | setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom); |
| 362 | |
| Dale Johannesen | 160be0f | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 363 | // Comparisons that require checking two conditions. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 364 | setCondCodeAction(ISD::SETULT, MVT::f32, Expand); |
| 365 | setCondCodeAction(ISD::SETULT, MVT::f64, Expand); |
| 366 | setCondCodeAction(ISD::SETUGT, MVT::f32, Expand); |
| 367 | setCondCodeAction(ISD::SETUGT, MVT::f64, Expand); |
| 368 | setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand); |
| 369 | setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand); |
| 370 | setCondCodeAction(ISD::SETOGE, MVT::f32, Expand); |
| 371 | setCondCodeAction(ISD::SETOGE, MVT::f64, Expand); |
| 372 | setCondCodeAction(ISD::SETOLE, MVT::f32, Expand); |
| 373 | setCondCodeAction(ISD::SETOLE, MVT::f64, Expand); |
| 374 | setCondCodeAction(ISD::SETONE, MVT::f32, Expand); |
| 375 | setCondCodeAction(ISD::SETONE, MVT::f64, Expand); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 376 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 377 | if (Subtarget.has64BitSupport()) { |
| Nate Begeman | 0b71e00 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 378 | // They also have instructions for converting between i64 and fp. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 379 | setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); |
| 380 | setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand); |
| 381 | setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom); |
| 382 | setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand); |
| Dale Johannesen | 37bc85f | 2009-06-04 20:53:52 +0000 | [diff] [blame] | 383 | // This is just the low 32 bits of a (signed) fp->i64 conversion. |
| 384 | // We cannot do this with Promote because i64 is not a legal type. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 385 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 386 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 387 | if (Subtarget.hasLFIWAX() || Subtarget.isPPC64()) |
| Hal Finkel | e53429a | 2013-03-31 01:58:02 +0000 | [diff] [blame] | 388 | setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); |
| Nate Begeman | 762bf80 | 2005-10-25 23:48:36 +0000 | [diff] [blame] | 389 | } else { |
| Chris Lattner | 595088a | 2005-11-17 07:30:41 +0000 | [diff] [blame] | 390 | // PowerPC does not have FP_TO_UINT on 32-bit implementations. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 391 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand); |
| Nate Begeman | e74dfbb | 2005-10-18 00:56:42 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 394 | // With the instructions enabled under FPCVT, we can do everything. |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 395 | if (Subtarget.hasFPCVT()) { |
| 396 | if (Subtarget.has64BitSupport()) { |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 397 | setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); |
| 398 | setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom); |
| 399 | setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom); |
| 400 | setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom); |
| 401 | } |
| 402 | |
| 403 | setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); |
| 404 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); |
| 405 | setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); |
| 406 | setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom); |
| 407 | } |
| 408 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 409 | if (Subtarget.use64BitRegs()) { |
| Chris Lattner | b193576 | 2007-10-19 04:08:28 +0000 | [diff] [blame] | 410 | // 64-bit PowerPC implementations can support i64 types directly |
| Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 411 | addRegisterClass(MVT::i64, &PPC::G8RCRegClass); |
| Nate Begeman | 0b71e00 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 412 | // BUILD_PAIR can't be handled natively, and should be expanded to shl/or |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 413 | setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand); |
| Dan Gohman | 8d2ead2 | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 414 | // 64-bit PowerPC wants to expand i128 shifts itself. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 415 | setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom); |
| 416 | setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom); |
| 417 | setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom); |
| Nate Begeman | 0b71e00 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 418 | } else { |
| Chris Lattner | b193576 | 2007-10-19 04:08:28 +0000 | [diff] [blame] | 419 | // 32-bit PowerPC wants to expand i64 shifts itself. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 420 | setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); |
| 421 | setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); |
| 422 | setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); |
| Nate Begeman | 6095214 | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 423 | } |
| Evan Cheng | 1926427 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 424 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 425 | if (Subtarget.hasAltivec()) { |
| Chris Lattner | baa73e0 | 2006-03-31 19:52:36 +0000 | [diff] [blame] | 426 | // First set operation action for all vector types to expand. Then we |
| 427 | // will selectively turn on ones that can be effectively codegen'd. |
| Ahmed Bougacha | 67dd2d2 | 2015-01-07 21:27:10 +0000 | [diff] [blame] | 428 | for (MVT VT : MVT::vector_valuetypes()) { |
| Chris Lattner | 06a21ba | 2006-04-16 01:37:57 +0000 | [diff] [blame] | 429 | // add/sub are legal for all supported vector VT's. |
| NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 430 | setOperationAction(ISD::ADD, VT, Legal); |
| 431 | setOperationAction(ISD::SUB, VT, Legal); |
| 432 | |
| Bill Schmidt | 433b1c3 | 2015-02-05 15:24:47 +0000 | [diff] [blame] | 433 | // Vector instructions introduced in P8 |
| Kit Barton | d4eb73c | 2015-05-05 16:10:44 +0000 | [diff] [blame] | 434 | if (Subtarget.hasP8Altivec() && (VT.SimpleTy != MVT::v1i128)) { |
| Bill Schmidt | fe88b18 | 2015-02-03 21:58:23 +0000 | [diff] [blame] | 435 | setOperationAction(ISD::CTPOP, VT, Legal); |
| Bill Schmidt | 433b1c3 | 2015-02-05 15:24:47 +0000 | [diff] [blame] | 436 | setOperationAction(ISD::CTLZ, VT, Legal); |
| 437 | } |
| 438 | else { |
| Bill Schmidt | fe88b18 | 2015-02-03 21:58:23 +0000 | [diff] [blame] | 439 | setOperationAction(ISD::CTPOP, VT, Expand); |
| Bill Schmidt | 433b1c3 | 2015-02-05 15:24:47 +0000 | [diff] [blame] | 440 | setOperationAction(ISD::CTLZ, VT, Expand); |
| 441 | } |
| Bill Schmidt | fe88b18 | 2015-02-03 21:58:23 +0000 | [diff] [blame] | 442 | |
| Nemanja Ivanovic | 6f22b41 | 2016-09-27 08:42:12 +0000 | [diff] [blame] | 443 | // Vector instructions introduced in P9 |
| 444 | if (Subtarget.hasP9Altivec() && (VT.SimpleTy != MVT::v1i128)) |
| 445 | setOperationAction(ISD::CTTZ, VT, Legal); |
| 446 | else |
| 447 | setOperationAction(ISD::CTTZ, VT, Expand); |
| 448 | |
| Chris Lattner | 95c7adc | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 449 | // We promote all shuffles to v16i8. |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 450 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 451 | AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8); |
| Chris Lattner | 06a21ba | 2006-04-16 01:37:57 +0000 | [diff] [blame] | 452 | |
| 453 | // We promote all non-typed operations to v4i32. |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 454 | setOperationAction(ISD::AND , VT, Promote); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 455 | AddPromotedToType (ISD::AND , VT, MVT::v4i32); |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 456 | setOperationAction(ISD::OR , VT, Promote); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 457 | AddPromotedToType (ISD::OR , VT, MVT::v4i32); |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 458 | setOperationAction(ISD::XOR , VT, Promote); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 459 | AddPromotedToType (ISD::XOR , VT, MVT::v4i32); |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 460 | setOperationAction(ISD::LOAD , VT, Promote); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 461 | AddPromotedToType (ISD::LOAD , VT, MVT::v4i32); |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 462 | setOperationAction(ISD::SELECT, VT, Promote); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 463 | AddPromotedToType (ISD::SELECT, VT, MVT::v4i32); |
| Hal Finkel | a2cdbce | 2015-08-30 22:12:50 +0000 | [diff] [blame] | 464 | setOperationAction(ISD::SELECT_CC, VT, Promote); |
| 465 | AddPromotedToType (ISD::SELECT_CC, VT, MVT::v4i32); |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 466 | setOperationAction(ISD::STORE, VT, Promote); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 467 | AddPromotedToType (ISD::STORE, VT, MVT::v4i32); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 468 | |
| Chris Lattner | 06a21ba | 2006-04-16 01:37:57 +0000 | [diff] [blame] | 469 | // No other operations are legal. |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 470 | setOperationAction(ISD::MUL , VT, Expand); |
| 471 | setOperationAction(ISD::SDIV, VT, Expand); |
| 472 | setOperationAction(ISD::SREM, VT, Expand); |
| 473 | setOperationAction(ISD::UDIV, VT, Expand); |
| 474 | setOperationAction(ISD::UREM, VT, Expand); |
| 475 | setOperationAction(ISD::FDIV, VT, Expand); |
| Hal Finkel | e393022 | 2013-07-08 17:30:25 +0000 | [diff] [blame] | 476 | setOperationAction(ISD::FREM, VT, Expand); |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 477 | setOperationAction(ISD::FNEG, VT, Expand); |
| Craig Topper | c8a2adf | 2012-11-15 08:02:19 +0000 | [diff] [blame] | 478 | setOperationAction(ISD::FSQRT, VT, Expand); |
| 479 | setOperationAction(ISD::FLOG, VT, Expand); |
| 480 | setOperationAction(ISD::FLOG10, VT, Expand); |
| 481 | setOperationAction(ISD::FLOG2, VT, Expand); |
| 482 | setOperationAction(ISD::FEXP, VT, Expand); |
| 483 | setOperationAction(ISD::FEXP2, VT, Expand); |
| 484 | setOperationAction(ISD::FSIN, VT, Expand); |
| 485 | setOperationAction(ISD::FCOS, VT, Expand); |
| 486 | setOperationAction(ISD::FABS, VT, Expand); |
| 487 | setOperationAction(ISD::FPOWI, VT, Expand); |
| Craig Topper | c4343f2 | 2012-11-14 08:11:25 +0000 | [diff] [blame] | 488 | setOperationAction(ISD::FFLOOR, VT, Expand); |
| Craig Topper | 61d0457 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 489 | setOperationAction(ISD::FCEIL, VT, Expand); |
| 490 | setOperationAction(ISD::FTRUNC, VT, Expand); |
| 491 | setOperationAction(ISD::FRINT, VT, Expand); |
| 492 | setOperationAction(ISD::FNEARBYINT, VT, Expand); |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 493 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand); |
| 494 | setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand); |
| 495 | setOperationAction(ISD::BUILD_VECTOR, VT, Expand); |
| Ulrich Weigand | 51eccec | 2014-08-04 13:27:12 +0000 | [diff] [blame] | 496 | setOperationAction(ISD::MULHU, VT, Expand); |
| 497 | setOperationAction(ISD::MULHS, VT, Expand); |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 498 | setOperationAction(ISD::UMUL_LOHI, VT, Expand); |
| 499 | setOperationAction(ISD::SMUL_LOHI, VT, Expand); |
| 500 | setOperationAction(ISD::UDIVREM, VT, Expand); |
| 501 | setOperationAction(ISD::SDIVREM, VT, Expand); |
| 502 | setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand); |
| 503 | setOperationAction(ISD::FPOW, VT, Expand); |
| Benjamin Kramer | f3ad235 | 2014-05-19 13:12:38 +0000 | [diff] [blame] | 504 | setOperationAction(ISD::BSWAP, VT, Expand); |
| Benjamin Kramer | c507146 | 2012-12-19 15:49:14 +0000 | [diff] [blame] | 505 | setOperationAction(ISD::VSELECT, VT, Expand); |
| Adhemerval Zanella | c4182d1 | 2012-11-05 17:15:56 +0000 | [diff] [blame] | 506 | setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand); |
| Nemanja Ivanovic | 74e31bc | 2015-12-02 10:36:24 +0000 | [diff] [blame] | 507 | setOperationAction(ISD::ROTL, VT, Expand); |
| 508 | setOperationAction(ISD::ROTR, VT, Expand); |
| Adhemerval Zanella | c4182d1 | 2012-11-05 17:15:56 +0000 | [diff] [blame] | 509 | |
| Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 510 | for (MVT InnerVT : MVT::vector_valuetypes()) { |
| Adhemerval Zanella | c4182d1 | 2012-11-05 17:15:56 +0000 | [diff] [blame] | 511 | setTruncStoreAction(VT, InnerVT, Expand); |
| Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 512 | setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand); |
| 513 | setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand); |
| 514 | setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand); |
| 515 | } |
| Chris Lattner | baa73e0 | 2006-03-31 19:52:36 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| Chris Lattner | 95c7adc | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 518 | // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle |
| 519 | // with merges, splats, etc. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 520 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom); |
| Chris Lattner | 95c7adc | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 521 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 522 | setOperationAction(ISD::AND , MVT::v4i32, Legal); |
| 523 | setOperationAction(ISD::OR , MVT::v4i32, Legal); |
| 524 | setOperationAction(ISD::XOR , MVT::v4i32, Legal); |
| 525 | setOperationAction(ISD::LOAD , MVT::v4i32, Legal); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 526 | setOperationAction(ISD::SELECT, MVT::v4i32, |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 527 | Subtarget.useCRBits() ? Legal : Expand); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 528 | setOperationAction(ISD::STORE , MVT::v4i32, Legal); |
| Adhemerval Zanella | 5c6e084 | 2012-10-08 17:27:24 +0000 | [diff] [blame] | 529 | setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal); |
| 530 | setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal); |
| 531 | setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal); |
| 532 | setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal); |
| Adhemerval Zanella | bdface5 | 2012-11-15 20:56:03 +0000 | [diff] [blame] | 533 | setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal); |
| 534 | setOperationAction(ISD::FCEIL, MVT::v4f32, Legal); |
| 535 | setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal); |
| 536 | setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 537 | |
| Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 538 | addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass); |
| 539 | addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass); |
| 540 | addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass); |
| 541 | addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 542 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 543 | setOperationAction(ISD::MUL, MVT::v4f32, Legal); |
| Hal Finkel | 0a479ae | 2012-06-22 00:49:52 +0000 | [diff] [blame] | 544 | setOperationAction(ISD::FMA, MVT::v4f32, Legal); |
| Hal Finkel | 2e10331 | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 545 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 546 | if (TM.Options.UnsafeFPMath || Subtarget.hasVSX()) { |
| Hal Finkel | 2e10331 | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 547 | setOperationAction(ISD::FDIV, MVT::v4f32, Legal); |
| 548 | setOperationAction(ISD::FSQRT, MVT::v4f32, Legal); |
| 549 | } |
| 550 | |
| NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 551 | if (Subtarget.hasP8Altivec()) |
| Kit Barton | 20d3981 | 2015-03-10 19:49:38 +0000 | [diff] [blame] | 552 | setOperationAction(ISD::MUL, MVT::v4i32, Legal); |
| 553 | else |
| 554 | setOperationAction(ISD::MUL, MVT::v4i32, Custom); |
| NAKAMURA Takumi | a9cb538 | 2015-09-22 11:14:39 +0000 | [diff] [blame] | 555 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 556 | setOperationAction(ISD::MUL, MVT::v8i16, Custom); |
| 557 | setOperationAction(ISD::MUL, MVT::v16i8, Custom); |
| Chris Lattner | a8713b1 | 2006-03-20 01:53:53 +0000 | [diff] [blame] | 558 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 559 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom); |
| 560 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 561 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 562 | setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom); |
| 563 | setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom); |
| 564 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom); |
| 565 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); |
| Adhemerval Zanella | 56775e0 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 566 | |
| 567 | // Altivec does not contain unordered floating-point compare instructions |
| 568 | setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand); |
| 569 | setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand); |
| Hal Finkel | 21ada79 | 2013-07-08 20:00:03 +0000 | [diff] [blame] | 570 | setCondCodeAction(ISD::SETO, MVT::v4f32, Expand); |
| 571 | setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand); |
| Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 572 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 573 | if (Subtarget.hasVSX()) { |
| Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 574 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal); |
| Nemanja Ivanovic | d389657 | 2015-10-09 11:12:18 +0000 | [diff] [blame] | 575 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal); |
| 576 | if (Subtarget.hasP8Vector()) { |
| Nemanja Ivanovic | 1c39ca6 | 2015-08-13 17:40:44 +0000 | [diff] [blame] | 577 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal); |
| Nemanja Ivanovic | d389657 | 2015-10-09 11:12:18 +0000 | [diff] [blame] | 578 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal); |
| 579 | } |
| Nemanja Ivanovic | 5ebc92d | 2016-03-24 13:40:33 +0000 | [diff] [blame] | 580 | if (Subtarget.hasDirectMove() && isPPC64) { |
| Nemanja Ivanovic | 1c39ca6 | 2015-08-13 17:40:44 +0000 | [diff] [blame] | 581 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Legal); |
| 582 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Legal); |
| 583 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Legal); |
| Nemanja Ivanovic | be5f0c0 | 2015-11-02 14:01:11 +0000 | [diff] [blame] | 584 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Legal); |
| Nemanja Ivanovic | d389657 | 2015-10-09 11:12:18 +0000 | [diff] [blame] | 585 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Legal); |
| 586 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Legal); |
| 587 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal); |
| 588 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal); |
| Nemanja Ivanovic | 1c39ca6 | 2015-08-13 17:40:44 +0000 | [diff] [blame] | 589 | } |
| Hal Finkel | 82569b6 | 2014-03-27 22:22:48 +0000 | [diff] [blame] | 590 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal); |
| Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 591 | |
| 592 | setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal); |
| 593 | setOperationAction(ISD::FCEIL, MVT::v2f64, Legal); |
| 594 | setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal); |
| 595 | setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal); |
| 596 | setOperationAction(ISD::FROUND, MVT::v2f64, Legal); |
| 597 | |
| 598 | setOperationAction(ISD::FROUND, MVT::v4f32, Legal); |
| 599 | |
| 600 | setOperationAction(ISD::MUL, MVT::v2f64, Legal); |
| 601 | setOperationAction(ISD::FMA, MVT::v2f64, Legal); |
| 602 | |
| 603 | setOperationAction(ISD::FDIV, MVT::v2f64, Legal); |
| 604 | setOperationAction(ISD::FSQRT, MVT::v2f64, Legal); |
| 605 | |
| Hal Finkel | 732f0f7 | 2014-03-26 12:49:28 +0000 | [diff] [blame] | 606 | setOperationAction(ISD::VSELECT, MVT::v16i8, Legal); |
| 607 | setOperationAction(ISD::VSELECT, MVT::v8i16, Legal); |
| 608 | setOperationAction(ISD::VSELECT, MVT::v4i32, Legal); |
| 609 | setOperationAction(ISD::VSELECT, MVT::v4f32, Legal); |
| 610 | setOperationAction(ISD::VSELECT, MVT::v2f64, Legal); |
| 611 | |
| Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 612 | // Share the Altivec comparison restrictions. |
| 613 | setCondCodeAction(ISD::SETUO, MVT::v2f64, Expand); |
| 614 | setCondCodeAction(ISD::SETUEQ, MVT::v2f64, Expand); |
| Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 615 | setCondCodeAction(ISD::SETO, MVT::v2f64, Expand); |
| 616 | setCondCodeAction(ISD::SETONE, MVT::v2f64, Expand); |
| 617 | |
| Hal Finkel | 9281c9a | 2014-03-26 18:26:30 +0000 | [diff] [blame] | 618 | setOperationAction(ISD::LOAD, MVT::v2f64, Legal); |
| 619 | setOperationAction(ISD::STORE, MVT::v2f64, Legal); |
| 620 | |
| Hal Finkel | df3e34d | 2014-03-26 22:58:37 +0000 | [diff] [blame] | 621 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Legal); |
| 622 | |
| Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 623 | if (Subtarget.hasP8Vector()) |
| 624 | addRegisterClass(MVT::f32, &PPC::VSSRCRegClass); |
| 625 | |
| Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 626 | addRegisterClass(MVT::f64, &PPC::VSFRCRegClass); |
| Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 627 | |
| Bill Schmidt | 54cced5 | 2015-07-16 21:14:07 +0000 | [diff] [blame] | 628 | addRegisterClass(MVT::v4i32, &PPC::VSRCRegClass); |
| Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 629 | addRegisterClass(MVT::v4f32, &PPC::VSRCRegClass); |
| 630 | addRegisterClass(MVT::v2f64, &PPC::VSRCRegClass); |
| Hal Finkel | a6c8b51 | 2014-03-26 16:12:58 +0000 | [diff] [blame] | 631 | |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 632 | if (Subtarget.hasP8Altivec()) { |
| Kit Barton | e48b1e1 | 2015-03-05 16:24:38 +0000 | [diff] [blame] | 633 | setOperationAction(ISD::SHL, MVT::v2i64, Legal); |
| 634 | setOperationAction(ISD::SRA, MVT::v2i64, Legal); |
| 635 | setOperationAction(ISD::SRL, MVT::v2i64, Legal); |
| 636 | |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 637 | setOperationAction(ISD::SETCC, MVT::v2i64, Legal); |
| 638 | } |
| 639 | else { |
| Kit Barton | e48b1e1 | 2015-03-05 16:24:38 +0000 | [diff] [blame] | 640 | setOperationAction(ISD::SHL, MVT::v2i64, Expand); |
| 641 | setOperationAction(ISD::SRA, MVT::v2i64, Expand); |
| 642 | setOperationAction(ISD::SRL, MVT::v2i64, Expand); |
| 643 | |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 644 | setOperationAction(ISD::SETCC, MVT::v2i64, Custom); |
| 645 | |
| 646 | // VSX v2i64 only supports non-arithmetic operations. |
| 647 | setOperationAction(ISD::ADD, MVT::v2i64, Expand); |
| 648 | setOperationAction(ISD::SUB, MVT::v2i64, Expand); |
| 649 | } |
| Hal Finkel | 777c9dd | 2014-03-29 16:04:40 +0000 | [diff] [blame] | 650 | |
| Hal Finkel | 9281c9a | 2014-03-26 18:26:30 +0000 | [diff] [blame] | 651 | setOperationAction(ISD::LOAD, MVT::v2i64, Promote); |
| 652 | AddPromotedToType (ISD::LOAD, MVT::v2i64, MVT::v2f64); |
| 653 | setOperationAction(ISD::STORE, MVT::v2i64, Promote); |
| 654 | AddPromotedToType (ISD::STORE, MVT::v2i64, MVT::v2f64); |
| 655 | |
| Hal Finkel | df3e34d | 2014-03-26 22:58:37 +0000 | [diff] [blame] | 656 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Legal); |
| 657 | |
| Hal Finkel | 7279f4b | 2014-03-26 19:13:54 +0000 | [diff] [blame] | 658 | setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal); |
| 659 | setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal); |
| 660 | setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal); |
| 661 | setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal); |
| 662 | |
| Hal Finkel | 5c0d145 | 2014-03-30 13:22:59 +0000 | [diff] [blame] | 663 | // Vector operation legalization checks the result type of |
| 664 | // SIGN_EXTEND_INREG, overall legalization checks the inner type. |
| 665 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i64, Legal); |
| 666 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Legal); |
| 667 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom); |
| 668 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom); |
| 669 | |
| Kit Barton | 915c5ec | 2016-02-26 21:59:44 +0000 | [diff] [blame] | 670 | setOperationAction(ISD::FNEG, MVT::v4f32, Legal); |
| 671 | setOperationAction(ISD::FNEG, MVT::v2f64, Legal); |
| Kit Barton | a1d6a6f | 2016-03-09 17:48:01 +0000 | [diff] [blame] | 672 | setOperationAction(ISD::FABS, MVT::v4f32, Legal); |
| 673 | setOperationAction(ISD::FABS, MVT::v2f64, Legal); |
| Kit Barton | 915c5ec | 2016-02-26 21:59:44 +0000 | [diff] [blame] | 674 | |
| Nemanja Ivanovic | df1cb52 | 2016-11-29 16:11:34 +0000 | [diff] [blame] | 675 | if (Subtarget.hasDirectMove()) |
| 676 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom); |
| 677 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom); |
| 678 | |
| Hal Finkel | a6c8b51 | 2014-03-26 16:12:58 +0000 | [diff] [blame] | 679 | addRegisterClass(MVT::v2i64, &PPC::VSRCRegClass); |
| Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 680 | } |
| Bill Schmidt | fe88b18 | 2015-02-03 21:58:23 +0000 | [diff] [blame] | 681 | |
| Kit Barton | d4eb73c | 2015-05-05 16:10:44 +0000 | [diff] [blame] | 682 | if (Subtarget.hasP8Altivec()) { |
| Bill Schmidt | fe88b18 | 2015-02-03 21:58:23 +0000 | [diff] [blame] | 683 | addRegisterClass(MVT::v2i64, &PPC::VRRCRegClass); |
| Kit Barton | d4eb73c | 2015-05-05 16:10:44 +0000 | [diff] [blame] | 684 | addRegisterClass(MVT::v1i128, &PPC::VRRCRegClass); |
| 685 | } |
| Nemanja Ivanovic | d5deb48 | 2016-09-14 14:19:09 +0000 | [diff] [blame] | 686 | |
| Nemanja Ivanovic | b43bb61 | 2016-07-12 21:00:10 +0000 | [diff] [blame] | 687 | if (Subtarget.hasP9Vector()) { |
| Nemanja Ivanovic | d5deb48 | 2016-09-14 14:19:09 +0000 | [diff] [blame] | 688 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom); |
| 689 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); |
| Nemanja Ivanovic | b43bb61 | 2016-07-12 21:00:10 +0000 | [diff] [blame] | 690 | } |
| Nate Begeman | 3e7db9c | 2005-11-29 08:17:20 +0000 | [diff] [blame] | 691 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 692 | |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 693 | if (Subtarget.hasQPX()) { |
| 694 | setOperationAction(ISD::FADD, MVT::v4f64, Legal); |
| 695 | setOperationAction(ISD::FSUB, MVT::v4f64, Legal); |
| 696 | setOperationAction(ISD::FMUL, MVT::v4f64, Legal); |
| 697 | setOperationAction(ISD::FREM, MVT::v4f64, Expand); |
| 698 | |
| 699 | setOperationAction(ISD::FCOPYSIGN, MVT::v4f64, Legal); |
| 700 | setOperationAction(ISD::FGETSIGN, MVT::v4f64, Expand); |
| 701 | |
| 702 | setOperationAction(ISD::LOAD , MVT::v4f64, Custom); |
| 703 | setOperationAction(ISD::STORE , MVT::v4f64, Custom); |
| 704 | |
| 705 | setTruncStoreAction(MVT::v4f64, MVT::v4f32, Custom); |
| 706 | setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f32, Custom); |
| 707 | |
| 708 | if (!Subtarget.useCRBits()) |
| 709 | setOperationAction(ISD::SELECT, MVT::v4f64, Expand); |
| 710 | setOperationAction(ISD::VSELECT, MVT::v4f64, Legal); |
| 711 | |
| 712 | setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4f64, Legal); |
| 713 | setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4f64, Expand); |
| 714 | setOperationAction(ISD::CONCAT_VECTORS , MVT::v4f64, Expand); |
| 715 | setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4f64, Expand); |
| 716 | setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4f64, Custom); |
| 717 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f64, Legal); |
| 718 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom); |
| 719 | |
| 720 | setOperationAction(ISD::FP_TO_SINT , MVT::v4f64, Legal); |
| 721 | setOperationAction(ISD::FP_TO_UINT , MVT::v4f64, Expand); |
| 722 | |
| 723 | setOperationAction(ISD::FP_ROUND , MVT::v4f32, Legal); |
| 724 | setOperationAction(ISD::FP_ROUND_INREG , MVT::v4f32, Expand); |
| 725 | setOperationAction(ISD::FP_EXTEND, MVT::v4f64, Legal); |
| 726 | |
| 727 | setOperationAction(ISD::FNEG , MVT::v4f64, Legal); |
| 728 | setOperationAction(ISD::FABS , MVT::v4f64, Legal); |
| 729 | setOperationAction(ISD::FSIN , MVT::v4f64, Expand); |
| 730 | setOperationAction(ISD::FCOS , MVT::v4f64, Expand); |
| 731 | setOperationAction(ISD::FPOWI , MVT::v4f64, Expand); |
| 732 | setOperationAction(ISD::FPOW , MVT::v4f64, Expand); |
| 733 | setOperationAction(ISD::FLOG , MVT::v4f64, Expand); |
| 734 | setOperationAction(ISD::FLOG2 , MVT::v4f64, Expand); |
| 735 | setOperationAction(ISD::FLOG10 , MVT::v4f64, Expand); |
| 736 | setOperationAction(ISD::FEXP , MVT::v4f64, Expand); |
| 737 | setOperationAction(ISD::FEXP2 , MVT::v4f64, Expand); |
| 738 | |
| 739 | setOperationAction(ISD::FMINNUM, MVT::v4f64, Legal); |
| 740 | setOperationAction(ISD::FMAXNUM, MVT::v4f64, Legal); |
| 741 | |
| 742 | setIndexedLoadAction(ISD::PRE_INC, MVT::v4f64, Legal); |
| 743 | setIndexedStoreAction(ISD::PRE_INC, MVT::v4f64, Legal); |
| 744 | |
| 745 | addRegisterClass(MVT::v4f64, &PPC::QFRCRegClass); |
| 746 | |
| 747 | setOperationAction(ISD::FADD, MVT::v4f32, Legal); |
| 748 | setOperationAction(ISD::FSUB, MVT::v4f32, Legal); |
| 749 | setOperationAction(ISD::FMUL, MVT::v4f32, Legal); |
| 750 | setOperationAction(ISD::FREM, MVT::v4f32, Expand); |
| 751 | |
| 752 | setOperationAction(ISD::FCOPYSIGN, MVT::v4f32, Legal); |
| 753 | setOperationAction(ISD::FGETSIGN, MVT::v4f32, Expand); |
| 754 | |
| 755 | setOperationAction(ISD::LOAD , MVT::v4f32, Custom); |
| 756 | setOperationAction(ISD::STORE , MVT::v4f32, Custom); |
| 757 | |
| 758 | if (!Subtarget.useCRBits()) |
| 759 | setOperationAction(ISD::SELECT, MVT::v4f32, Expand); |
| 760 | setOperationAction(ISD::VSELECT, MVT::v4f32, Legal); |
| 761 | |
| 762 | setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4f32, Legal); |
| 763 | setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4f32, Expand); |
| 764 | setOperationAction(ISD::CONCAT_VECTORS , MVT::v4f32, Expand); |
| 765 | setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4f32, Expand); |
| 766 | setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4f32, Custom); |
| 767 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal); |
| 768 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); |
| 769 | |
| 770 | setOperationAction(ISD::FP_TO_SINT , MVT::v4f32, Legal); |
| 771 | setOperationAction(ISD::FP_TO_UINT , MVT::v4f32, Expand); |
| 772 | |
| 773 | setOperationAction(ISD::FNEG , MVT::v4f32, Legal); |
| 774 | setOperationAction(ISD::FABS , MVT::v4f32, Legal); |
| 775 | setOperationAction(ISD::FSIN , MVT::v4f32, Expand); |
| 776 | setOperationAction(ISD::FCOS , MVT::v4f32, Expand); |
| 777 | setOperationAction(ISD::FPOWI , MVT::v4f32, Expand); |
| 778 | setOperationAction(ISD::FPOW , MVT::v4f32, Expand); |
| 779 | setOperationAction(ISD::FLOG , MVT::v4f32, Expand); |
| 780 | setOperationAction(ISD::FLOG2 , MVT::v4f32, Expand); |
| 781 | setOperationAction(ISD::FLOG10 , MVT::v4f32, Expand); |
| 782 | setOperationAction(ISD::FEXP , MVT::v4f32, Expand); |
| 783 | setOperationAction(ISD::FEXP2 , MVT::v4f32, Expand); |
| 784 | |
| 785 | setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal); |
| 786 | setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal); |
| 787 | |
| 788 | setIndexedLoadAction(ISD::PRE_INC, MVT::v4f32, Legal); |
| 789 | setIndexedStoreAction(ISD::PRE_INC, MVT::v4f32, Legal); |
| 790 | |
| 791 | addRegisterClass(MVT::v4f32, &PPC::QSRCRegClass); |
| 792 | |
| 793 | setOperationAction(ISD::AND , MVT::v4i1, Legal); |
| 794 | setOperationAction(ISD::OR , MVT::v4i1, Legal); |
| 795 | setOperationAction(ISD::XOR , MVT::v4i1, Legal); |
| 796 | |
| 797 | if (!Subtarget.useCRBits()) |
| 798 | setOperationAction(ISD::SELECT, MVT::v4i1, Expand); |
| 799 | setOperationAction(ISD::VSELECT, MVT::v4i1, Legal); |
| 800 | |
| 801 | setOperationAction(ISD::LOAD , MVT::v4i1, Custom); |
| 802 | setOperationAction(ISD::STORE , MVT::v4i1, Custom); |
| 803 | |
| 804 | setOperationAction(ISD::EXTRACT_VECTOR_ELT , MVT::v4i1, Custom); |
| 805 | setOperationAction(ISD::INSERT_VECTOR_ELT , MVT::v4i1, Expand); |
| 806 | setOperationAction(ISD::CONCAT_VECTORS , MVT::v4i1, Expand); |
| 807 | setOperationAction(ISD::EXTRACT_SUBVECTOR , MVT::v4i1, Expand); |
| 808 | setOperationAction(ISD::VECTOR_SHUFFLE , MVT::v4i1, Custom); |
| 809 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i1, Expand); |
| 810 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4i1, Custom); |
| 811 | |
| 812 | setOperationAction(ISD::SINT_TO_FP, MVT::v4i1, Custom); |
| 813 | setOperationAction(ISD::UINT_TO_FP, MVT::v4i1, Custom); |
| 814 | |
| 815 | addRegisterClass(MVT::v4i1, &PPC::QBRCRegClass); |
| 816 | |
| 817 | setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal); |
| 818 | setOperationAction(ISD::FCEIL, MVT::v4f64, Legal); |
| 819 | setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal); |
| 820 | setOperationAction(ISD::FROUND, MVT::v4f64, Legal); |
| 821 | |
| 822 | setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal); |
| 823 | setOperationAction(ISD::FCEIL, MVT::v4f32, Legal); |
| 824 | setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal); |
| 825 | setOperationAction(ISD::FROUND, MVT::v4f32, Legal); |
| 826 | |
| 827 | setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Expand); |
| 828 | setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand); |
| 829 | |
| 830 | // These need to set FE_INEXACT, and so cannot be vectorized here. |
| 831 | setOperationAction(ISD::FRINT, MVT::v4f64, Expand); |
| 832 | setOperationAction(ISD::FRINT, MVT::v4f32, Expand); |
| 833 | |
| 834 | if (TM.Options.UnsafeFPMath) { |
| 835 | setOperationAction(ISD::FDIV, MVT::v4f64, Legal); |
| 836 | setOperationAction(ISD::FSQRT, MVT::v4f64, Legal); |
| 837 | |
| 838 | setOperationAction(ISD::FDIV, MVT::v4f32, Legal); |
| 839 | setOperationAction(ISD::FSQRT, MVT::v4f32, Legal); |
| 840 | } else { |
| 841 | setOperationAction(ISD::FDIV, MVT::v4f64, Expand); |
| 842 | setOperationAction(ISD::FSQRT, MVT::v4f64, Expand); |
| 843 | |
| 844 | setOperationAction(ISD::FDIV, MVT::v4f32, Expand); |
| 845 | setOperationAction(ISD::FSQRT, MVT::v4f32, Expand); |
| 846 | } |
| 847 | } |
| 848 | |
| Hal Finkel | 01fa770 | 2014-12-03 00:19:17 +0000 | [diff] [blame] | 849 | if (Subtarget.has64BitSupport()) |
| Hal Finkel | 322e41a | 2012-04-01 20:08:17 +0000 | [diff] [blame] | 850 | setOperationAction(ISD::PREFETCH, MVT::Other, Legal); |
| Hal Finkel | 01fa770 | 2014-12-03 00:19:17 +0000 | [diff] [blame] | 851 | |
| 852 | setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, isPPC64 ? Legal : Custom); |
| Hal Finkel | 322e41a | 2012-04-01 20:08:17 +0000 | [diff] [blame] | 853 | |
| Robin Morisset | e1ca44b | 2014-10-02 22:27:07 +0000 | [diff] [blame] | 854 | if (!isPPC64) { |
| 855 | setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand); |
| 856 | setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand); |
| 857 | } |
| Eli Friedman | 7dfa791 | 2011-08-29 18:23:02 +0000 | [diff] [blame] | 858 | |
| Duncan Sands | 8d6e2e1 | 2008-11-23 15:47:28 +0000 | [diff] [blame] | 859 | setBooleanContents(ZeroOrOneBooleanContent); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 860 | |
| 861 | if (Subtarget.hasAltivec()) { |
| 862 | // Altivec instructions set fields to all zeros or all ones. |
| 863 | setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); |
| 864 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 865 | |
| Joerg Sonnenberger | b5459e6 | 2014-07-24 22:20:10 +0000 | [diff] [blame] | 866 | if (!isPPC64) { |
| 867 | // These libcalls are not available in 32-bit. |
| 868 | setLibcallName(RTLIB::SHL_I128, nullptr); |
| 869 | setLibcallName(RTLIB::SRL_I128, nullptr); |
| 870 | setLibcallName(RTLIB::SRA_I128, nullptr); |
| 871 | } |
| 872 | |
| Joseph Tremoulet | f748c89 | 2015-11-07 01:11:31 +0000 | [diff] [blame] | 873 | setStackPointerRegisterToSaveRestore(isPPC64 ? PPC::X1 : PPC::R1); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 874 | |
| Chris Lattner | f418435 | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 875 | // We have target-specific dag combine patterns for the following nodes: |
| 876 | setTargetDAGCombine(ISD::SINT_TO_FP); |
| Nemanja Ivanovic | 44513e5 | 2016-07-05 09:22:29 +0000 | [diff] [blame] | 877 | setTargetDAGCombine(ISD::BUILD_VECTOR); |
| Hal Finkel | 5efb918 | 2015-01-06 06:01:57 +0000 | [diff] [blame] | 878 | if (Subtarget.hasFPCVT()) |
| 879 | setTargetDAGCombine(ISD::UINT_TO_FP); |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 880 | setTargetDAGCombine(ISD::LOAD); |
| Chris Lattner | 27f5345 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 881 | setTargetDAGCombine(ISD::STORE); |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 882 | setTargetDAGCombine(ISD::BR_CC); |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 883 | if (Subtarget.useCRBits()) |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 884 | setTargetDAGCombine(ISD::BRCOND); |
| Chris Lattner | a7976d3 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 885 | setTargetDAGCombine(ISD::BSWAP); |
| Hal Finkel | bc2ee4c | 2013-05-25 04:05:05 +0000 | [diff] [blame] | 886 | setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN); |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 887 | setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN); |
| 888 | setTargetDAGCombine(ISD::INTRINSIC_VOID); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 889 | |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 890 | setTargetDAGCombine(ISD::SIGN_EXTEND); |
| 891 | setTargetDAGCombine(ISD::ZERO_EXTEND); |
| 892 | setTargetDAGCombine(ISD::ANY_EXTEND); |
| 893 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 894 | if (Subtarget.useCRBits()) { |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 895 | setTargetDAGCombine(ISD::TRUNCATE); |
| 896 | setTargetDAGCombine(ISD::SETCC); |
| 897 | setTargetDAGCombine(ISD::SELECT_CC); |
| 898 | } |
| 899 | |
| Hal Finkel | 2e10331 | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 900 | // Use reciprocal estimates. |
| 901 | if (TM.Options.UnsafeFPMath) { |
| 902 | setTargetDAGCombine(ISD::FDIV); |
| 903 | setTargetDAGCombine(ISD::FSQRT); |
| 904 | } |
| 905 | |
| Dale Johannesen | 10432e5 | 2007-10-19 00:59:18 +0000 | [diff] [blame] | 906 | // Darwin long double math library functions have $LDBL128 appended. |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 907 | if (Subtarget.isDarwin()) { |
| Duncan Sands | 53c954f | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 908 | setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128"); |
| Dale Johannesen | 10432e5 | 2007-10-19 00:59:18 +0000 | [diff] [blame] | 909 | setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128"); |
| 910 | setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128"); |
| Duncan Sands | 53c954f | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 911 | setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128"); |
| 912 | setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128"); |
| Dale Johannesen | da2d806 | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 913 | setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128"); |
| 914 | setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128"); |
| 915 | setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128"); |
| 916 | setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128"); |
| 917 | setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128"); |
| Dale Johannesen | 10432e5 | 2007-10-19 00:59:18 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 920 | // With 32 condition bits, we don't need to sink (and duplicate) compares |
| 921 | // aggressively in CodeGenPrep. |
| Hal Finkel | 7a0516e | 2015-02-12 01:02:52 +0000 | [diff] [blame] | 922 | if (Subtarget.useCRBits()) { |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 923 | setHasMultipleConditionRegisters(); |
| Hal Finkel | 7a0516e | 2015-02-12 01:02:52 +0000 | [diff] [blame] | 924 | setJumpIsExpensive(); |
| 925 | } |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 926 | |
| Hal Finkel | 6529857 | 2011-10-17 18:53:03 +0000 | [diff] [blame] | 927 | setMinFunctionAlignment(2); |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 928 | if (Subtarget.isDarwin()) |
| Hal Finkel | 6529857 | 2011-10-17 18:53:03 +0000 | [diff] [blame] | 929 | setPrefFunctionAlignment(4); |
| Eli Friedman | 2518f83 | 2011-05-06 20:34:06 +0000 | [diff] [blame] | 930 | |
| Hal Finkel | d73bfba | 2015-01-03 14:58:25 +0000 | [diff] [blame] | 931 | switch (Subtarget.getDarwinDirective()) { |
| 932 | default: break; |
| 933 | case PPC::DIR_970: |
| 934 | case PPC::DIR_A2: |
| 935 | case PPC::DIR_E500mc: |
| 936 | case PPC::DIR_E5500: |
| 937 | case PPC::DIR_PWR4: |
| 938 | case PPC::DIR_PWR5: |
| 939 | case PPC::DIR_PWR5X: |
| 940 | case PPC::DIR_PWR6: |
| 941 | case PPC::DIR_PWR6X: |
| 942 | case PPC::DIR_PWR7: |
| 943 | case PPC::DIR_PWR8: |
| Nemanja Ivanovic | 6e29baf | 2016-05-09 18:54:58 +0000 | [diff] [blame] | 944 | case PPC::DIR_PWR9: |
| Hal Finkel | d73bfba | 2015-01-03 14:58:25 +0000 | [diff] [blame] | 945 | setPrefFunctionAlignment(4); |
| 946 | setPrefLoopAlignment(4); |
| 947 | break; |
| 948 | } |
| 949 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 950 | if (Subtarget.enableMachineScheduler()) |
| Hal Finkel | 21442b2 | 2013-09-11 23:05:25 +0000 | [diff] [blame] | 951 | setSchedulingPreference(Sched::Source); |
| 952 | else |
| 953 | setSchedulingPreference(Sched::Hybrid); |
| Hal Finkel | 6f0ae78 | 2011-11-22 16:21:04 +0000 | [diff] [blame] | 954 | |
| Eric Christopher | 23a3a7c | 2015-02-26 00:00:24 +0000 | [diff] [blame] | 955 | computeRegisterProperties(STI.getRegisterInfo()); |
| Hal Finkel | 742b535 | 2012-08-28 16:12:39 +0000 | [diff] [blame] | 956 | |
| Hal Finkel | d73bfba | 2015-01-03 14:58:25 +0000 | [diff] [blame] | 957 | // The Freescale cores do better with aggressive inlining of memcpy and |
| 958 | // friends. GCC uses same threshold of 128 bytes (= 32 word stores). |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 959 | if (Subtarget.getDarwinDirective() == PPC::DIR_E500mc || |
| 960 | Subtarget.getDarwinDirective() == PPC::DIR_E5500) { |
| Jim Grosbach | 341ad3e | 2013-02-20 21:13:59 +0000 | [diff] [blame] | 961 | MaxStoresPerMemset = 32; |
| 962 | MaxStoresPerMemsetOptSize = 16; |
| 963 | MaxStoresPerMemcpy = 32; |
| 964 | MaxStoresPerMemcpyOptSize = 8; |
| 965 | MaxStoresPerMemmove = 32; |
| 966 | MaxStoresPerMemmoveOptSize = 8; |
| Hal Finkel | 5c3cacf | 2015-02-27 19:58:28 +0000 | [diff] [blame] | 967 | } else if (Subtarget.getDarwinDirective() == PPC::DIR_A2) { |
| 968 | // The A2 also benefits from (very) aggressive inlining of memcpy and |
| 969 | // friends. The overhead of a the function call, even when warm, can be |
| 970 | // over one hundred cycles. |
| 971 | MaxStoresPerMemset = 128; |
| 972 | MaxStoresPerMemcpy = 128; |
| 973 | MaxStoresPerMemmove = 128; |
| Hal Finkel | 742b535 | 2012-08-28 16:12:39 +0000 | [diff] [blame] | 974 | } |
| Chris Lattner | f22556d | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| Hal Finkel | 262a224 | 2013-09-12 23:20:06 +0000 | [diff] [blame] | 977 | /// getMaxByValAlign - Helper for getByValTypeAlignment to determine |
| 978 | /// the desired ByVal argument alignment. |
| Pete Cooper | 2e20147 | 2015-07-27 17:15:24 +0000 | [diff] [blame] | 979 | static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign, |
| Hal Finkel | 262a224 | 2013-09-12 23:20:06 +0000 | [diff] [blame] | 980 | unsigned MaxMaxAlign) { |
| 981 | if (MaxAlign == MaxMaxAlign) |
| 982 | return; |
| Pete Cooper | 2e20147 | 2015-07-27 17:15:24 +0000 | [diff] [blame] | 983 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) { |
| Hal Finkel | 262a224 | 2013-09-12 23:20:06 +0000 | [diff] [blame] | 984 | if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256) |
| 985 | MaxAlign = 32; |
| 986 | else if (VTy->getBitWidth() >= 128 && MaxAlign < 16) |
| 987 | MaxAlign = 16; |
| Pete Cooper | 2e20147 | 2015-07-27 17:15:24 +0000 | [diff] [blame] | 988 | } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| Hal Finkel | 262a224 | 2013-09-12 23:20:06 +0000 | [diff] [blame] | 989 | unsigned EltAlign = 0; |
| 990 | getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign); |
| 991 | if (EltAlign > MaxAlign) |
| 992 | MaxAlign = EltAlign; |
| Pete Cooper | 2e20147 | 2015-07-27 17:15:24 +0000 | [diff] [blame] | 993 | } else if (StructType *STy = dyn_cast<StructType>(Ty)) { |
| 994 | for (auto *EltTy : STy->elements()) { |
| Hal Finkel | 262a224 | 2013-09-12 23:20:06 +0000 | [diff] [blame] | 995 | unsigned EltAlign = 0; |
| Pete Cooper | 0debbdc | 2015-07-24 18:55:49 +0000 | [diff] [blame] | 996 | getMaxByValAlign(EltTy, EltAlign, MaxMaxAlign); |
| Hal Finkel | 262a224 | 2013-09-12 23:20:06 +0000 | [diff] [blame] | 997 | if (EltAlign > MaxAlign) |
| 998 | MaxAlign = EltAlign; |
| 999 | if (MaxAlign == MaxMaxAlign) |
| 1000 | break; |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | |
| Dale Johannesen | cbde4c2 | 2008-02-28 22:31:51 +0000 | [diff] [blame] | 1005 | /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate |
| 1006 | /// function arguments in the caller parameter area. |
| Mehdi Amini | 5c183d5 | 2015-07-09 02:09:28 +0000 | [diff] [blame] | 1007 | unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty, |
| 1008 | const DataLayout &DL) const { |
| Dale Johannesen | cbde4c2 | 2008-02-28 22:31:51 +0000 | [diff] [blame] | 1009 | // Darwin passes everything on 4 byte boundary. |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 1010 | if (Subtarget.isDarwin()) |
| Dale Johannesen | cbde4c2 | 2008-02-28 22:31:51 +0000 | [diff] [blame] | 1011 | return 4; |
| Roman Divacky | b9663cc | 2012-04-02 15:49:30 +0000 | [diff] [blame] | 1012 | |
| 1013 | // 16byte and wider vectors are passed on 16byte boundary. |
| Roman Divacky | b9663cc | 2012-04-02 15:49:30 +0000 | [diff] [blame] | 1014 | // The rest is 8 on PPC64 and 4 on PPC32 boundary. |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 1015 | unsigned Align = Subtarget.isPPC64() ? 8 : 4; |
| 1016 | if (Subtarget.hasAltivec() || Subtarget.hasQPX()) |
| 1017 | getMaxByValAlign(Ty, Align, Subtarget.hasQPX() ? 32 : 16); |
| Hal Finkel | 262a224 | 2013-09-12 23:20:06 +0000 | [diff] [blame] | 1018 | return Align; |
| Dale Johannesen | cbde4c2 | 2008-02-28 22:31:51 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| Petar Jovanovic | 280f710 | 2015-12-14 17:57:33 +0000 | [diff] [blame] | 1021 | bool PPCTargetLowering::useSoftFloat() const { |
| 1022 | return Subtarget.useSoftFloat(); |
| 1023 | } |
| 1024 | |
| Chris Lattner | 347ed8a | 2006-01-09 23:52:17 +0000 | [diff] [blame] | 1025 | const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const { |
| Matthias Braun | d04893f | 2015-05-07 21:33:59 +0000 | [diff] [blame] | 1026 | switch ((PPCISD::NodeType)Opcode) { |
| 1027 | case PPCISD::FIRST_NUMBER: break; |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 1028 | case PPCISD::FSEL: return "PPCISD::FSEL"; |
| 1029 | case PPCISD::FCFID: return "PPCISD::FCFID"; |
| Hal Finkel | 3fe09ea | 2015-01-06 07:02:15 +0000 | [diff] [blame] | 1030 | case PPCISD::FCFIDU: return "PPCISD::FCFIDU"; |
| 1031 | case PPCISD::FCFIDS: return "PPCISD::FCFIDS"; |
| 1032 | case PPCISD::FCFIDUS: return "PPCISD::FCFIDUS"; |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 1033 | case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ"; |
| 1034 | case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ"; |
| Hal Finkel | 3fe09ea | 2015-01-06 07:02:15 +0000 | [diff] [blame] | 1035 | case PPCISD::FCTIDUZ: return "PPCISD::FCTIDUZ"; |
| 1036 | case PPCISD::FCTIWUZ: return "PPCISD::FCTIWUZ"; |
| Hal Finkel | 2e10331 | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 1037 | case PPCISD::FRE: return "PPCISD::FRE"; |
| 1038 | case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE"; |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 1039 | case PPCISD::STFIWX: return "PPCISD::STFIWX"; |
| 1040 | case PPCISD::VMADDFP: return "PPCISD::VMADDFP"; |
| 1041 | case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP"; |
| 1042 | case PPCISD::VPERM: return "PPCISD::VPERM"; |
| Nemanja Ivanovic | 1a2b2f0 | 2016-05-04 16:04:02 +0000 | [diff] [blame] | 1043 | case PPCISD::XXSPLT: return "PPCISD::XXSPLT"; |
| Nemanja Ivanovic | b43bb61 | 2016-07-12 21:00:10 +0000 | [diff] [blame] | 1044 | case PPCISD::XXINSERT: return "PPCISD::XXINSERT"; |
| 1045 | case PPCISD::VECSHL: return "PPCISD::VECSHL"; |
| Hal Finkel | 4edc66b | 2015-01-03 01:16:37 +0000 | [diff] [blame] | 1046 | case PPCISD::CMPB: return "PPCISD::CMPB"; |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 1047 | case PPCISD::Hi: return "PPCISD::Hi"; |
| 1048 | case PPCISD::Lo: return "PPCISD::Lo"; |
| Tilmann Scheller | d1aaa32 | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 1049 | case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY"; |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 1050 | case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC"; |
| Yury Gribov | d7dbb66 | 2015-12-01 11:40:55 +0000 | [diff] [blame] | 1051 | case PPCISD::DYNAREAOFFSET: return "PPCISD::DYNAREAOFFSET"; |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 1052 | case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg"; |
| 1053 | case PPCISD::SRL: return "PPCISD::SRL"; |
| 1054 | case PPCISD::SRA: return "PPCISD::SRA"; |
| 1055 | case PPCISD::SHL: return "PPCISD::SHL"; |
| Matthias Braun | d04893f | 2015-05-07 21:33:59 +0000 | [diff] [blame] | 1056 | case PPCISD::SRA_ADDZE: return "PPCISD::SRA_ADDZE"; |
| Ulrich Weigand | f62e83f | 2013-03-22 15:24:13 +0000 | [diff] [blame] | 1057 | case PPCISD::CALL: return "PPCISD::CALL"; |
| 1058 | case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP"; |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 1059 | case PPCISD::MTCTR: return "PPCISD::MTCTR"; |
| Ulrich Weigand | f62e83f | 2013-03-22 15:24:13 +0000 | [diff] [blame] | 1060 | case PPCISD::BCTRL: return "PPCISD::BCTRL"; |
| Hal Finkel | fc096c9 | 2014-12-23 22:29:40 +0000 | [diff] [blame] | 1061 | case PPCISD::BCTRL_LOAD_TOC: return "PPCISD::BCTRL_LOAD_TOC"; |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 1062 | case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG"; |
| Hal Finkel | bbdee93 | 2014-12-02 22:01:00 +0000 | [diff] [blame] | 1063 | case PPCISD::READ_TIME_BASE: return "PPCISD::READ_TIME_BASE"; |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 1064 | case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP"; |
| 1065 | case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP"; |
| Ulrich Weigand | d5ebc62 | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 1066 | case PPCISD::MFOCRF: return "PPCISD::MFOCRF"; |
| Nemanja Ivanovic | c38b531 | 2015-04-11 10:40:42 +0000 | [diff] [blame] | 1067 | case PPCISD::MFVSR: return "PPCISD::MFVSR"; |
| 1068 | case PPCISD::MTVSRA: return "PPCISD::MTVSRA"; |
| 1069 | case PPCISD::MTVSRZ: return "PPCISD::MTVSRZ"; |
| Nemanja Ivanovic | 44513e5 | 2016-07-05 09:22:29 +0000 | [diff] [blame] | 1070 | case PPCISD::SINT_VEC_TO_FP: return "PPCISD::SINT_VEC_TO_FP"; |
| 1071 | case PPCISD::UINT_VEC_TO_FP: return "PPCISD::UINT_VEC_TO_FP"; |
| Matthias Braun | d04893f | 2015-05-07 21:33:59 +0000 | [diff] [blame] | 1072 | case PPCISD::ANDIo_1_EQ_BIT: return "PPCISD::ANDIo_1_EQ_BIT"; |
| 1073 | case PPCISD::ANDIo_1_GT_BIT: return "PPCISD::ANDIo_1_GT_BIT"; |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 1074 | case PPCISD::VCMP: return "PPCISD::VCMP"; |
| 1075 | case PPCISD::VCMPo: return "PPCISD::VCMPo"; |
| 1076 | case PPCISD::LBRX: return "PPCISD::LBRX"; |
| 1077 | case PPCISD::STBRX: return "PPCISD::STBRX"; |
| Hal Finkel | 3fe09ea | 2015-01-06 07:02:15 +0000 | [diff] [blame] | 1078 | case PPCISD::LFIWAX: return "PPCISD::LFIWAX"; |
| 1079 | case PPCISD::LFIWZX: return "PPCISD::LFIWZX"; |
| Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 1080 | case PPCISD::LXSIZX: return "PPCISD::LXSIZX"; |
| 1081 | case PPCISD::STXSIX: return "PPCISD::STXSIX"; |
| 1082 | case PPCISD::VEXTS: return "PPCISD::VEXTS"; |
| Matthias Braun | d04893f | 2015-05-07 21:33:59 +0000 | [diff] [blame] | 1083 | case PPCISD::LXVD2X: return "PPCISD::LXVD2X"; |
| 1084 | case PPCISD::STXVD2X: return "PPCISD::STXVD2X"; |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 1085 | case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH"; |
| Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 1086 | case PPCISD::BDNZ: return "PPCISD::BDNZ"; |
| 1087 | case PPCISD::BDZ: return "PPCISD::BDZ"; |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 1088 | case PPCISD::MFFS: return "PPCISD::MFFS"; |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 1089 | case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ"; |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 1090 | case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN"; |
| Hal Finkel | 5ab3780 | 2012-08-28 02:10:27 +0000 | [diff] [blame] | 1091 | case PPCISD::CR6SET: return "PPCISD::CR6SET"; |
| 1092 | case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET"; |
| Roman Divacky | 32143e2 | 2013-12-20 18:08:54 +0000 | [diff] [blame] | 1093 | case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT"; |
| Matthias Braun | d04893f | 2015-05-07 21:33:59 +0000 | [diff] [blame] | 1094 | case PPCISD::PPC32_PICGOT: return "PPCISD::PPC32_PICGOT"; |
| Bill Schmidt | 9f0b4ec | 2012-12-14 17:02:38 +0000 | [diff] [blame] | 1095 | case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA"; |
| 1096 | case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L"; |
| Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 1097 | case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS"; |
| Bill Schmidt | c56f1d3 | 2012-12-11 20:30:11 +0000 | [diff] [blame] | 1098 | case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA"; |
| 1099 | case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L"; |
| Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 1100 | case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR"; |
| 1101 | case PPCISD::ADDI_TLSGD_L_ADDR: return "PPCISD::ADDI_TLSGD_L_ADDR"; |
| Bill Schmidt | 24b8dd6 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 1102 | case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA"; |
| 1103 | case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L"; |
| Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 1104 | case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR"; |
| 1105 | case PPCISD::ADDI_TLSLD_L_ADDR: return "PPCISD::ADDI_TLSLD_L_ADDR"; |
| Bill Schmidt | 24b8dd6 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 1106 | case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA"; |
| 1107 | case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L"; |
| Bill Schmidt | 51e7951 | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 1108 | case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT"; |
| Bill Schmidt | a87a7e2 | 2013-05-14 19:35:45 +0000 | [diff] [blame] | 1109 | case PPCISD::SC: return "PPCISD::SC"; |
| Bill Schmidt | e26236e | 2015-05-22 16:44:10 +0000 | [diff] [blame] | 1110 | case PPCISD::CLRBHRB: return "PPCISD::CLRBHRB"; |
| 1111 | case PPCISD::MFBHRBE: return "PPCISD::MFBHRBE"; |
| 1112 | case PPCISD::RFEBB: return "PPCISD::RFEBB"; |
| Matthias Braun | d04893f | 2015-05-07 21:33:59 +0000 | [diff] [blame] | 1113 | case PPCISD::XXSWAPD: return "PPCISD::XXSWAPD"; |
| Nemanja Ivanovic | eebbcb6 | 2016-07-12 12:16:27 +0000 | [diff] [blame] | 1114 | case PPCISD::SWAP_NO_CHAIN: return "PPCISD::SWAP_NO_CHAIN"; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 1115 | case PPCISD::QVFPERM: return "PPCISD::QVFPERM"; |
| 1116 | case PPCISD::QVGPCI: return "PPCISD::QVGPCI"; |
| 1117 | case PPCISD::QVALIGNI: return "PPCISD::QVALIGNI"; |
| 1118 | case PPCISD::QVESPLATI: return "PPCISD::QVESPLATI"; |
| 1119 | case PPCISD::QBFLT: return "PPCISD::QBFLT"; |
| 1120 | case PPCISD::QVLFSb: return "PPCISD::QVLFSb"; |
| Chris Lattner | 347ed8a | 2006-01-09 23:52:17 +0000 | [diff] [blame] | 1121 | } |
| Matthias Braun | d04893f | 2015-05-07 21:33:59 +0000 | [diff] [blame] | 1122 | return nullptr; |
| Chris Lattner | 347ed8a | 2006-01-09 23:52:17 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1125 | EVT PPCTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &C, |
| 1126 | EVT VT) const { |
| Adhemerval Zanella | fe3f793 | 2012-10-08 18:59:53 +0000 | [diff] [blame] | 1127 | if (!VT.isVector()) |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 1128 | return Subtarget.useCRBits() ? MVT::i1 : MVT::i32; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 1129 | |
| 1130 | if (Subtarget.hasQPX()) |
| 1131 | return EVT::getVectorVT(C, MVT::i1, VT.getVectorNumElements()); |
| 1132 | |
| Adhemerval Zanella | fe3f793 | 2012-10-08 18:59:53 +0000 | [diff] [blame] | 1133 | return VT.changeVectorElementTypeToInteger(); |
| Scott Michel | a6729e8 | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| Hal Finkel | 62ac736 | 2014-09-19 11:42:56 +0000 | [diff] [blame] | 1136 | bool PPCTargetLowering::enableAggressiveFMAFusion(EVT VT) const { |
| 1137 | assert(VT.isFloatingPoint() && "Non-floating-point FMA?"); |
| 1138 | return true; |
| 1139 | } |
| 1140 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1141 | //===----------------------------------------------------------------------===// |
| 1142 | // Node matching predicates, for use by the tblgen matching code. |
| 1143 | //===----------------------------------------------------------------------===// |
| 1144 | |
| Chris Lattner | 7f1fa8e | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 1145 | /// isFloatingPointZero - Return true if this is 0.0 or -0.0. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1146 | static bool isFloatingPointZero(SDValue Op) { |
| Chris Lattner | 7f1fa8e | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 1147 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) |
| Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 1148 | return CFP->getValueAPF().isZero(); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1149 | else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) { |
| Chris Lattner | 7f1fa8e | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 1150 | // Maybe this has already been legalized into the constant pool? |
| 1151 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1))) |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1152 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) |
| Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 1153 | return CFP->getValueAPF().isZero(); |
| Chris Lattner | 7f1fa8e | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 1154 | } |
| 1155 | return false; |
| 1156 | } |
| 1157 | |
| Chris Lattner | e8b83b4 | 2006-04-06 17:23:16 +0000 | [diff] [blame] | 1158 | /// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return |
| 1159 | /// true if Op is undef or if it matches the specified value. |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1160 | static bool isConstantOrUndef(int Op, int Val) { |
| 1161 | return Op < 0 || Op == Val; |
| Chris Lattner | e8b83b4 | 2006-04-06 17:23:16 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | /// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a |
| 1165 | /// VPKUHUM instruction. |
| Ulrich Weigand | cc9909b | 2014-08-04 13:53:40 +0000 | [diff] [blame] | 1166 | /// The ShuffleKind distinguishes between big-endian operations with |
| 1167 | /// two different inputs (0), either-endian operations with two identical |
| Bill Schmidt | 5ed84cd | 2015-05-16 01:02:12 +0000 | [diff] [blame] | 1168 | /// inputs (1), and little-endian operations with two different inputs (2). |
| Ulrich Weigand | cc9909b | 2014-08-04 13:53:40 +0000 | [diff] [blame] | 1169 | /// For the latter, the input operands are swapped (see PPCInstrAltivec.td). |
| 1170 | bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind, |
| Bill Schmidt | f910a06 | 2014-06-10 14:35:01 +0000 | [diff] [blame] | 1171 | SelectionDAG &DAG) { |
| Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 1172 | bool IsLE = DAG.getDataLayout().isLittleEndian(); |
| Ulrich Weigand | cc9909b | 2014-08-04 13:53:40 +0000 | [diff] [blame] | 1173 | if (ShuffleKind == 0) { |
| Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 1174 | if (IsLE) |
| Ulrich Weigand | cc9909b | 2014-08-04 13:53:40 +0000 | [diff] [blame] | 1175 | return false; |
| Chris Lattner | a4bbfae | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 1176 | for (unsigned i = 0; i != 16; ++i) |
| Ulrich Weigand | cc9909b | 2014-08-04 13:53:40 +0000 | [diff] [blame] | 1177 | if (!isConstantOrUndef(N->getMaskElt(i), i*2+1)) |
| Chris Lattner | a4bbfae | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 1178 | return false; |
| Ulrich Weigand | cc9909b | 2014-08-04 13:53:40 +0000 | [diff] [blame] | 1179 | } else if (ShuffleKind == 2) { |
| Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 1180 | if (!IsLE) |
| Ulrich Weigand | cc9909b | 2014-08-04 13:53:40 +0000 | [diff] [blame] | 1181 | return false; |
| 1182 | for (unsigned i = 0; i != 16; ++i) |
| 1183 | if (!isConstantOrUndef(N->getMaskElt(i), i*2)) |
| 1184 | return false; |
| 1185 | } else if (ShuffleKind == 1) { |
| Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 1186 | unsigned j = IsLE ? 0 : 1; |
| Chris Lattner | a4bbfae | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 1187 | for (unsigned i = 0; i != 8; ++i) |
| Bill Schmidt | f910a06 | 2014-06-10 14:35:01 +0000 | [diff] [blame] | 1188 | if (!isConstantOrUndef(N->getMaskElt(i), i*2+j) || |
| 1189 | !isConstantOrUndef(N->getMaskElt(i+8), i*2+j)) |
| Chris Lattner | a4bbfae | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 1190 | return false; |
| 1191 | } |
| Chris Lattner | 1d33819 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 1192 | return true; |
| Chris Lattner | e8b83b4 | 2006-04-06 17:23:16 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | /// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a |
| 1196 | /// VPKUWUM instruction. |
| Ulrich Weigand | cc9909b | 2014-08-04 13:53:40 +0000 | [diff] [blame] | 1197 | /// The ShuffleKind distinguishes between big-endian operations with |
| 1198 | /// two different inputs (0), either-endian operations with two identical |
| Bill Schmidt | 5ed84cd | 2015-05-16 01:02:12 +0000 | [diff] [blame] | 1199 | /// inputs (1), and little-endian operations with two different inputs (2). |
| Ulrich Weigand | cc9909b | 2014-08-04 13:53:40 +0000 | [diff] [blame] | 1200 | /// For the latter, the input operands are swapped (see PPCInstrAltivec.td). |
| 1201 | bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind, |
| Bill Schmidt | f910a06 | 2014-06-10 14:35:01 +0000 | [diff] [blame] | 1202 | SelectionDAG &DAG) { |
| Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 1203 | bool IsLE = DAG.getDataLayout().isLittleEndian(); |
| Ulrich Weigand | cc9909b | 2014-08-04 13:53:40 +0000 | [diff] [blame] | 1204 | if (ShuffleKind == 0) { |
| Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 1205 | if (IsLE) |
| Ulrich Weigand | cc9909b | 2014-08-04 13:53:40 +0000 | [diff] [blame] | 1206 | return false; |
| Chris Lattner | a4bbfae | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 1207 | for (unsigned i = 0; i != 16; i += 2) |
| Ulrich Weigand | cc9909b | 2014-08-04 13:53:40 +0000 | [diff] [blame] | 1208 | if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) || |
| 1209 | !isConstantOrUndef(N->getMaskElt(i+1), i*2+3)) |
| Chris Lattner | a4bbfae | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 1210 | return false; |
| Ulrich Weigand | cc9909b | 2014-08-04 13:53:40 +0000 | [diff] [blame] | 1211 | } else if (ShuffleKind == 2) { |
| Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 1212 | if (!IsLE) |
| Ulrich Weigand | cc9909b | 2014-08-04 13:53:40 +0000 | [diff] [blame] | 1213 | return false; |
| 1214 | for (unsigned i = 0; i != 16; i += 2) |
| 1215 | if (!isConstantOrUndef(N->getMaskElt(i ), i*2) || |
| 1216 | !isConstantOrUndef(N->getMaskElt(i+1), i*2+1)) |
| 1217 | return false; |
| 1218 | } else if (ShuffleKind == 1) { |
| Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 1219 | unsigned j = IsLE ? 0 : 2; |
| Chris Lattner | a4bbfae | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 1220 | for (unsigned i = 0; i != 8; i += 2) |
| Ulrich Weigand | cc9909b | 2014-08-04 13:53:40 +0000 | [diff] [blame] | 1221 | if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) || |
| 1222 | !isConstantOrUndef(N->getMaskElt(i+1), i*2+j+1) || |
| 1223 | !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) || |
| 1224 | !isConstantOrUndef(N->getMaskElt(i+9), i*2+j+1)) |
| Chris Lattner | a4bbfae | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 1225 | return false; |
| 1226 | } |
| Chris Lattner | 1d33819 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 1227 | return true; |
| Chris Lattner | e8b83b4 | 2006-04-06 17:23:16 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
| Bill Schmidt | 5ed84cd | 2015-05-16 01:02:12 +0000 | [diff] [blame] | 1230 | /// isVPKUDUMShuffleMask - Return true if this is the shuffle mask for a |
| Bill Schmidt | e13ac91 | 2015-05-21 20:48:49 +0000 | [diff] [blame] | 1231 | /// VPKUDUM instruction, AND the VPKUDUM instruction exists for the |
| 1232 | /// current subtarget. |
| 1233 | /// |
| Bill Schmidt | 5ed84cd | 2015-05-16 01:02:12 +0000 | [diff] [blame] | 1234 | /// The ShuffleKind distinguishes between big-endian operations with |
| 1235 | /// two different inputs (0), either-endian operations with two identical |
| 1236 | /// inputs (1), and little-endian operations with two different inputs (2). |
| 1237 | /// For the latter, the input operands are swapped (see PPCInstrAltivec.td). |
| 1238 | bool PPC::isVPKUDUMShuffleMask(ShuffleVectorSDNode *N, unsigned ShuffleKind, |
| 1239 | SelectionDAG &DAG) { |
| Bill Schmidt | e13ac91 | 2015-05-21 20:48:49 +0000 | [diff] [blame] | 1240 | const PPCSubtarget& Subtarget = |
| 1241 | static_cast<const PPCSubtarget&>(DAG.getSubtarget()); |
| 1242 | if (!Subtarget.hasP8Vector()) |
| 1243 | return false; |
| 1244 | |
| Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 1245 | bool IsLE = DAG.getDataLayout().isLittleEndian(); |
| Bill Schmidt | 5ed84cd | 2015-05-16 01:02:12 +0000 | [diff] [blame] | 1246 | if (ShuffleKind == 0) { |
| 1247 | if (IsLE) |
| 1248 | return false; |
| 1249 | for (unsigned i = 0; i != 16; i += 4) |
| 1250 | if (!isConstantOrUndef(N->getMaskElt(i ), i*2+4) || |
| 1251 | !isConstantOrUndef(N->getMaskElt(i+1), i*2+5) || |
| 1252 | !isConstantOrUndef(N->getMaskElt(i+2), i*2+6) || |
| 1253 | !isConstantOrUndef(N->getMaskElt(i+3), i*2+7)) |
| 1254 | return false; |
| 1255 | } else if (ShuffleKind == 2) { |
| 1256 | if (!IsLE) |
| 1257 | return false; |
| 1258 | for (unsigned i = 0; i != 16; i += 4) |
| 1259 | if (!isConstantOrUndef(N->getMaskElt(i ), i*2) || |
| 1260 | !isConstantOrUndef(N->getMaskElt(i+1), i*2+1) || |
| 1261 | !isConstantOrUndef(N->getMaskElt(i+2), i*2+2) || |
| 1262 | !isConstantOrUndef(N->getMaskElt(i+3), i*2+3)) |
| 1263 | return false; |
| 1264 | } else if (ShuffleKind == 1) { |
| 1265 | unsigned j = IsLE ? 0 : 4; |
| 1266 | for (unsigned i = 0; i != 8; i += 4) |
| 1267 | if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) || |
| 1268 | !isConstantOrUndef(N->getMaskElt(i+1), i*2+j+1) || |
| 1269 | !isConstantOrUndef(N->getMaskElt(i+2), i*2+j+2) || |
| 1270 | !isConstantOrUndef(N->getMaskElt(i+3), i*2+j+3) || |
| 1271 | !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) || |
| 1272 | !isConstantOrUndef(N->getMaskElt(i+9), i*2+j+1) || |
| 1273 | !isConstantOrUndef(N->getMaskElt(i+10), i*2+j+2) || |
| 1274 | !isConstantOrUndef(N->getMaskElt(i+11), i*2+j+3)) |
| 1275 | return false; |
| 1276 | } |
| 1277 | return true; |
| 1278 | } |
| 1279 | |
| Chris Lattner | f38e033 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 1280 | /// isVMerge - Common function, used to match vmrg* shuffles. |
| 1281 | /// |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1282 | static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize, |
| Chris Lattner | f38e033 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 1283 | unsigned LHSStart, unsigned RHSStart) { |
| Hal Finkel | df3e34d | 2014-03-26 22:58:37 +0000 | [diff] [blame] | 1284 | if (N->getValueType(0) != MVT::v16i8) |
| 1285 | return false; |
| Chris Lattner | d1dcb52 | 2006-04-06 21:11:54 +0000 | [diff] [blame] | 1286 | assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) && |
| 1287 | "Unsupported merge size!"); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1288 | |
| Chris Lattner | d1dcb52 | 2006-04-06 21:11:54 +0000 | [diff] [blame] | 1289 | for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units |
| 1290 | for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1291 | if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j), |
| Chris Lattner | f38e033 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 1292 | LHSStart+j+i*UnitSize) || |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1293 | !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j), |
| Chris Lattner | f38e033 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 1294 | RHSStart+j+i*UnitSize)) |
| Chris Lattner | d1dcb52 | 2006-04-06 21:11:54 +0000 | [diff] [blame] | 1295 | return false; |
| 1296 | } |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1297 | return true; |
| Chris Lattner | f38e033 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | /// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for |
| Bill Schmidt | f910a06 | 2014-06-10 14:35:01 +0000 | [diff] [blame] | 1301 | /// a VMRGL* instruction with the specified unit size (1,2 or 4 bytes). |
| NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 1302 | /// The ShuffleKind distinguishes between big-endian merges with two |
| Bill Schmidt | c9fa5dd | 2014-07-25 01:55:55 +0000 | [diff] [blame] | 1303 | /// different inputs (0), either-endian merges with two identical inputs (1), |
| 1304 | /// and little-endian merges with two different inputs (2). For the latter, |
| 1305 | /// the input operands are swapped (see PPCInstrAltivec.td). |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1306 | bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize, |
| Bill Schmidt | c9fa5dd | 2014-07-25 01:55:55 +0000 | [diff] [blame] | 1307 | unsigned ShuffleKind, SelectionDAG &DAG) { |
| Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 1308 | if (DAG.getDataLayout().isLittleEndian()) { |
| Bill Schmidt | c9fa5dd | 2014-07-25 01:55:55 +0000 | [diff] [blame] | 1309 | if (ShuffleKind == 1) // unary |
| 1310 | return isVMerge(N, UnitSize, 0, 0); |
| 1311 | else if (ShuffleKind == 2) // swapped |
| Bill Schmidt | f910a06 | 2014-06-10 14:35:01 +0000 | [diff] [blame] | 1312 | return isVMerge(N, UnitSize, 0, 16); |
| Bill Schmidt | c9fa5dd | 2014-07-25 01:55:55 +0000 | [diff] [blame] | 1313 | else |
| 1314 | return false; |
| Bill Schmidt | f910a06 | 2014-06-10 14:35:01 +0000 | [diff] [blame] | 1315 | } else { |
| Bill Schmidt | c9fa5dd | 2014-07-25 01:55:55 +0000 | [diff] [blame] | 1316 | if (ShuffleKind == 1) // unary |
| 1317 | return isVMerge(N, UnitSize, 8, 8); |
| 1318 | else if (ShuffleKind == 0) // normal |
| Bill Schmidt | f910a06 | 2014-06-10 14:35:01 +0000 | [diff] [blame] | 1319 | return isVMerge(N, UnitSize, 8, 24); |
| Bill Schmidt | c9fa5dd | 2014-07-25 01:55:55 +0000 | [diff] [blame] | 1320 | else |
| 1321 | return false; |
| Bill Schmidt | f910a06 | 2014-06-10 14:35:01 +0000 | [diff] [blame] | 1322 | } |
| Chris Lattner | d1dcb52 | 2006-04-06 21:11:54 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | /// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for |
| Bill Schmidt | f910a06 | 2014-06-10 14:35:01 +0000 | [diff] [blame] | 1326 | /// a VMRGH* instruction with the specified unit size (1,2 or 4 bytes). |
| NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 1327 | /// The ShuffleKind distinguishes between big-endian merges with two |
| Bill Schmidt | c9fa5dd | 2014-07-25 01:55:55 +0000 | [diff] [blame] | 1328 | /// different inputs (0), either-endian merges with two identical inputs (1), |
| 1329 | /// and little-endian merges with two different inputs (2). For the latter, |
| 1330 | /// the input operands are swapped (see PPCInstrAltivec.td). |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1331 | bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize, |
| Bill Schmidt | c9fa5dd | 2014-07-25 01:55:55 +0000 | [diff] [blame] | 1332 | unsigned ShuffleKind, SelectionDAG &DAG) { |
| Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 1333 | if (DAG.getDataLayout().isLittleEndian()) { |
| Bill Schmidt | c9fa5dd | 2014-07-25 01:55:55 +0000 | [diff] [blame] | 1334 | if (ShuffleKind == 1) // unary |
| 1335 | return isVMerge(N, UnitSize, 8, 8); |
| 1336 | else if (ShuffleKind == 2) // swapped |
| Bill Schmidt | f910a06 | 2014-06-10 14:35:01 +0000 | [diff] [blame] | 1337 | return isVMerge(N, UnitSize, 8, 24); |
| Bill Schmidt | c9fa5dd | 2014-07-25 01:55:55 +0000 | [diff] [blame] | 1338 | else |
| 1339 | return false; |
| Bill Schmidt | f910a06 | 2014-06-10 14:35:01 +0000 | [diff] [blame] | 1340 | } else { |
| Bill Schmidt | c9fa5dd | 2014-07-25 01:55:55 +0000 | [diff] [blame] | 1341 | if (ShuffleKind == 1) // unary |
| 1342 | return isVMerge(N, UnitSize, 0, 0); |
| 1343 | else if (ShuffleKind == 0) // normal |
| Bill Schmidt | f910a06 | 2014-06-10 14:35:01 +0000 | [diff] [blame] | 1344 | return isVMerge(N, UnitSize, 0, 16); |
| Bill Schmidt | c9fa5dd | 2014-07-25 01:55:55 +0000 | [diff] [blame] | 1345 | else |
| 1346 | return false; |
| Bill Schmidt | f910a06 | 2014-06-10 14:35:01 +0000 | [diff] [blame] | 1347 | } |
| Chris Lattner | d1dcb52 | 2006-04-06 21:11:54 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
| Kit Barton | 13894c7 | 2015-06-25 15:17:40 +0000 | [diff] [blame] | 1350 | /** |
| 1351 | * \brief Common function used to match vmrgew and vmrgow shuffles |
| 1352 | * |
| 1353 | * The indexOffset determines whether to look for even or odd words in |
| 1354 | * the shuffle mask. This is based on the of the endianness of the target |
| 1355 | * machine. |
| 1356 | * - Little Endian: |
| 1357 | * - Use offset of 0 to check for odd elements |
| 1358 | * - Use offset of 4 to check for even elements |
| 1359 | * - Big Endian: |
| 1360 | * - Use offset of 0 to check for even elements |
| 1361 | * - Use offset of 4 to check for odd elements |
| 1362 | * A detailed description of the vector element ordering for little endian and |
| NAKAMURA Takumi | 520b45d | 2015-06-25 23:38:44 +0000 | [diff] [blame] | 1363 | * big endian can be found at |
| 1364 | * http://www.ibm.com/developerworks/library/l-ibm-xl-c-cpp-compiler/index.html |
| Kit Barton | 13894c7 | 2015-06-25 15:17:40 +0000 | [diff] [blame] | 1365 | * Targeting your applications - what little endian and big endian IBM XL C/C++ |
| NAKAMURA Takumi | 520b45d | 2015-06-25 23:38:44 +0000 | [diff] [blame] | 1366 | * compiler differences mean to you |
| Kit Barton | 13894c7 | 2015-06-25 15:17:40 +0000 | [diff] [blame] | 1367 | * |
| 1368 | * The mask to the shuffle vector instruction specifies the indices of the |
| 1369 | * elements from the two input vectors to place in the result. The elements are |
| 1370 | * numbered in array-access order, starting with the first vector. These vectors |
| 1371 | * are always of type v16i8, thus each vector will contain 16 elements of size |
| NAKAMURA Takumi | 520b45d | 2015-06-25 23:38:44 +0000 | [diff] [blame] | 1372 | * 8. More info on the shuffle vector can be found in the |
| 1373 | * http://llvm.org/docs/LangRef.html#shufflevector-instruction |
| 1374 | * Language Reference. |
| Kit Barton | 13894c7 | 2015-06-25 15:17:40 +0000 | [diff] [blame] | 1375 | * |
| 1376 | * The RHSStartValue indicates whether the same input vectors are used (unary) |
| 1377 | * or two different input vectors are used, based on the following: |
| 1378 | * - If the instruction uses the same vector for both inputs, the range of the |
| 1379 | * indices will be 0 to 15. In this case, the RHSStart value passed should |
| 1380 | * be 0. |
| 1381 | * - If the instruction has two different vectors then the range of the |
| 1382 | * indices will be 0 to 31. In this case, the RHSStart value passed should |
| 1383 | * be 16 (indices 0-15 specify elements in the first vector while indices 16 |
| 1384 | * to 31 specify elements in the second vector). |
| 1385 | * |
| 1386 | * \param[in] N The shuffle vector SD Node to analyze |
| 1387 | * \param[in] IndexOffset Specifies whether to look for even or odd elements |
| 1388 | * \param[in] RHSStartValue Specifies the starting index for the righthand input |
| 1389 | * vector to the shuffle_vector instruction |
| 1390 | * \return true iff this shuffle vector represents an even or odd word merge |
| 1391 | */ |
| 1392 | static bool isVMerge(ShuffleVectorSDNode *N, unsigned IndexOffset, |
| 1393 | unsigned RHSStartValue) { |
| 1394 | if (N->getValueType(0) != MVT::v16i8) |
| 1395 | return false; |
| 1396 | |
| 1397 | for (unsigned i = 0; i < 2; ++i) |
| 1398 | for (unsigned j = 0; j < 4; ++j) |
| 1399 | if (!isConstantOrUndef(N->getMaskElt(i*4+j), |
| 1400 | i*RHSStartValue+j+IndexOffset) || |
| 1401 | !isConstantOrUndef(N->getMaskElt(i*4+j+8), |
| 1402 | i*RHSStartValue+j+IndexOffset+8)) |
| 1403 | return false; |
| 1404 | return true; |
| 1405 | } |
| 1406 | |
| 1407 | /** |
| 1408 | * \brief Determine if the specified shuffle mask is suitable for the vmrgew or |
| 1409 | * vmrgow instructions. |
| 1410 | * |
| 1411 | * \param[in] N The shuffle vector SD Node to analyze |
| 1412 | * \param[in] CheckEven Check for an even merge (true) or an odd merge (false) |
| 1413 | * \param[in] ShuffleKind Identify the type of merge: |
| 1414 | * - 0 = big-endian merge with two different inputs; |
| 1415 | * - 1 = either-endian merge with two identical inputs; |
| 1416 | * - 2 = little-endian merge with two different inputs (inputs are swapped for |
| 1417 | * little-endian merges). |
| 1418 | * \param[in] DAG The current SelectionDAG |
| NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 1419 | * \return true iff this shuffle mask |
| Kit Barton | 13894c7 | 2015-06-25 15:17:40 +0000 | [diff] [blame] | 1420 | */ |
| 1421 | bool PPC::isVMRGEOShuffleMask(ShuffleVectorSDNode *N, bool CheckEven, |
| 1422 | unsigned ShuffleKind, SelectionDAG &DAG) { |
| Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 1423 | if (DAG.getDataLayout().isLittleEndian()) { |
| Kit Barton | 13894c7 | 2015-06-25 15:17:40 +0000 | [diff] [blame] | 1424 | unsigned indexOffset = CheckEven ? 4 : 0; |
| 1425 | if (ShuffleKind == 1) // Unary |
| 1426 | return isVMerge(N, indexOffset, 0); |
| 1427 | else if (ShuffleKind == 2) // swapped |
| 1428 | return isVMerge(N, indexOffset, 16); |
| 1429 | else |
| 1430 | return false; |
| 1431 | } |
| 1432 | else { |
| 1433 | unsigned indexOffset = CheckEven ? 0 : 4; |
| 1434 | if (ShuffleKind == 1) // Unary |
| 1435 | return isVMerge(N, indexOffset, 0); |
| 1436 | else if (ShuffleKind == 0) // Normal |
| 1437 | return isVMerge(N, indexOffset, 16); |
| 1438 | else |
| 1439 | return false; |
| 1440 | } |
| 1441 | return false; |
| 1442 | } |
| Chris Lattner | d1dcb52 | 2006-04-06 21:11:54 +0000 | [diff] [blame] | 1443 | |
| Chris Lattner | 1d33819 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 1444 | /// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift |
| 1445 | /// amount, otherwise return -1. |
| NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 1446 | /// The ShuffleKind distinguishes between big-endian operations with two |
| Bill Schmidt | 42a6936 | 2014-08-05 20:47:25 +0000 | [diff] [blame] | 1447 | /// different inputs (0), either-endian operations with two identical inputs |
| 1448 | /// (1), and little-endian operations with two different inputs (2). For the |
| 1449 | /// latter, the input operands are swapped (see PPCInstrAltivec.td). |
| 1450 | int PPC::isVSLDOIShuffleMask(SDNode *N, unsigned ShuffleKind, |
| 1451 | SelectionDAG &DAG) { |
| Hal Finkel | df3e34d | 2014-03-26 22:58:37 +0000 | [diff] [blame] | 1452 | if (N->getValueType(0) != MVT::v16i8) |
| Hal Finkel | a775e51 | 2014-04-08 19:00:27 +0000 | [diff] [blame] | 1453 | return -1; |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1454 | |
| 1455 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1456 | |
| Chris Lattner | 1d33819 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 1457 | // Find the first non-undef value in the shuffle mask. |
| 1458 | unsigned i; |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1459 | for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i) |
| Chris Lattner | 1d33819 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 1460 | /*search*/; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1461 | |
| Chris Lattner | 1d33819 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 1462 | if (i == 16) return -1; // all undef. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1463 | |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1464 | // Otherwise, check to see if the rest of the elements are consecutively |
| Chris Lattner | 1d33819 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 1465 | // numbered from this value. |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1466 | unsigned ShiftAmt = SVOp->getMaskElt(i); |
| Chris Lattner | 1d33819 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 1467 | if (ShiftAmt < i) return -1; |
| Chris Lattner | e8b83b4 | 2006-04-06 17:23:16 +0000 | [diff] [blame] | 1468 | |
| Bill Schmidt | f04e998 | 2014-08-04 23:21:01 +0000 | [diff] [blame] | 1469 | ShiftAmt -= i; |
| Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 1470 | bool isLE = DAG.getDataLayout().isLittleEndian(); |
| Bill Schmidt | f910a06 | 2014-06-10 14:35:01 +0000 | [diff] [blame] | 1471 | |
| Bill Schmidt | 42a6936 | 2014-08-05 20:47:25 +0000 | [diff] [blame] | 1472 | if ((ShuffleKind == 0 && !isLE) || (ShuffleKind == 2 && isLE)) { |
| Bill Schmidt | f04e998 | 2014-08-04 23:21:01 +0000 | [diff] [blame] | 1473 | // Check the rest of the elements to see if they are consecutive. |
| 1474 | for (++i; i != 16; ++i) |
| 1475 | if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i)) |
| 1476 | return -1; |
| Bill Schmidt | 42a6936 | 2014-08-05 20:47:25 +0000 | [diff] [blame] | 1477 | } else if (ShuffleKind == 1) { |
| Bill Schmidt | f04e998 | 2014-08-04 23:21:01 +0000 | [diff] [blame] | 1478 | // Check the rest of the elements to see if they are consecutive. |
| 1479 | for (++i; i != 16; ++i) |
| 1480 | if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15)) |
| 1481 | return -1; |
| Bill Schmidt | 42a6936 | 2014-08-05 20:47:25 +0000 | [diff] [blame] | 1482 | } else |
| 1483 | return -1; |
| 1484 | |
| Bill Schmidt | 1e77bb1 | 2015-07-15 15:45:30 +0000 | [diff] [blame] | 1485 | if (isLE) |
| Bill Schmidt | 42a6936 | 2014-08-05 20:47:25 +0000 | [diff] [blame] | 1486 | ShiftAmt = 16 - ShiftAmt; |
| Bill Schmidt | f04e998 | 2014-08-04 23:21:01 +0000 | [diff] [blame] | 1487 | |
| Chris Lattner | 1d33819 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 1488 | return ShiftAmt; |
| 1489 | } |
| Chris Lattner | ffc4756 | 2006-03-20 06:33:01 +0000 | [diff] [blame] | 1490 | |
| 1491 | /// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1492 | /// specifies a splat of a single element that is suitable for input to |
| 1493 | /// VSPLTB/VSPLTH/VSPLTW. |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1494 | bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) { |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1495 | assert(N->getValueType(0) == MVT::v16i8 && |
| Chris Lattner | 95c7adc | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 1496 | (EltSize == 1 || EltSize == 2 || EltSize == 4)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1497 | |
| Bill Schmidt | 42ddd71 | 2015-07-29 14:31:57 +0000 | [diff] [blame] | 1498 | // The consecutive indices need to specify an element, not part of two |
| 1499 | // different elements. So abandon ship early if this isn't the case. |
| 1500 | if (N->getMaskElt(0) % EltSize != 0) |
| 1501 | return false; |
| 1502 | |
| Chris Lattner | a8fbb6d | 2006-03-20 06:37:44 +0000 | [diff] [blame] | 1503 | // This is a splat operation if each element of the permute is the same, and |
| 1504 | // if the value doesn't reference the second vector. |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1505 | unsigned ElementBase = N->getMaskElt(0); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1506 | |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1507 | // FIXME: Handle UNDEF elements too! |
| 1508 | if (ElementBase >= 16) |
| Chris Lattner | 95c7adc | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 1509 | return false; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1510 | |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1511 | // Check that the indices are consecutive, in the case of a multi-byte element |
| 1512 | // splatted with a v16i8 mask. |
| 1513 | for (unsigned i = 1; i != EltSize; ++i) |
| 1514 | if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase)) |
| Chris Lattner | 95c7adc | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 1515 | return false; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1516 | |
| Chris Lattner | 95c7adc | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 1517 | for (unsigned i = EltSize, e = 16; i != e; i += EltSize) { |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1518 | if (N->getMaskElt(i) < 0) continue; |
| Chris Lattner | 95c7adc | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 1519 | for (unsigned j = 0; j != EltSize; ++j) |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1520 | if (N->getMaskElt(i+j) != N->getMaskElt(j)) |
| Chris Lattner | 95c7adc | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 1521 | return false; |
| Chris Lattner | a8fbb6d | 2006-03-20 06:37:44 +0000 | [diff] [blame] | 1522 | } |
| Chris Lattner | 95c7adc | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 1523 | return true; |
| Chris Lattner | ffc4756 | 2006-03-20 06:33:01 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
| Nemanja Ivanovic | b43bb61 | 2016-07-12 21:00:10 +0000 | [diff] [blame] | 1526 | bool PPC::isXXINSERTWMask(ShuffleVectorSDNode *N, unsigned &ShiftElts, |
| 1527 | unsigned &InsertAtByte, bool &Swap, bool IsLE) { |
| 1528 | |
| 1529 | // Check that the mask is shuffling words |
| 1530 | for (unsigned i = 0; i < 4; ++i) { |
| 1531 | unsigned B0 = N->getMaskElt(i*4); |
| 1532 | unsigned B1 = N->getMaskElt(i*4+1); |
| 1533 | unsigned B2 = N->getMaskElt(i*4+2); |
| 1534 | unsigned B3 = N->getMaskElt(i*4+3); |
| 1535 | if (B0 % 4) |
| 1536 | return false; |
| 1537 | if (B1 != B0+1 || B2 != B1+1 || B3 != B2+1) |
| 1538 | return false; |
| 1539 | } |
| 1540 | |
| 1541 | // Now we look at mask elements 0,4,8,12 |
| 1542 | unsigned M0 = N->getMaskElt(0) / 4; |
| 1543 | unsigned M1 = N->getMaskElt(4) / 4; |
| 1544 | unsigned M2 = N->getMaskElt(8) / 4; |
| 1545 | unsigned M3 = N->getMaskElt(12) / 4; |
| 1546 | unsigned LittleEndianShifts[] = { 2, 1, 0, 3 }; |
| 1547 | unsigned BigEndianShifts[] = { 3, 0, 1, 2 }; |
| 1548 | |
| 1549 | // Below, let H and L be arbitrary elements of the shuffle mask |
| 1550 | // where H is in the range [4,7] and L is in the range [0,3]. |
| 1551 | // H, 1, 2, 3 or L, 5, 6, 7 |
| 1552 | if ((M0 > 3 && M1 == 1 && M2 == 2 && M3 == 3) || |
| 1553 | (M0 < 4 && M1 == 5 && M2 == 6 && M3 == 7)) { |
| 1554 | ShiftElts = IsLE ? LittleEndianShifts[M0 & 0x3] : BigEndianShifts[M0 & 0x3]; |
| 1555 | InsertAtByte = IsLE ? 12 : 0; |
| 1556 | Swap = M0 < 4; |
| 1557 | return true; |
| 1558 | } |
| 1559 | // 0, H, 2, 3 or 4, L, 6, 7 |
| 1560 | if ((M1 > 3 && M0 == 0 && M2 == 2 && M3 == 3) || |
| 1561 | (M1 < 4 && M0 == 4 && M2 == 6 && M3 == 7)) { |
| 1562 | ShiftElts = IsLE ? LittleEndianShifts[M1 & 0x3] : BigEndianShifts[M1 & 0x3]; |
| 1563 | InsertAtByte = IsLE ? 8 : 4; |
| 1564 | Swap = M1 < 4; |
| 1565 | return true; |
| 1566 | } |
| 1567 | // 0, 1, H, 3 or 4, 5, L, 7 |
| 1568 | if ((M2 > 3 && M0 == 0 && M1 == 1 && M3 == 3) || |
| 1569 | (M2 < 4 && M0 == 4 && M1 == 5 && M3 == 7)) { |
| 1570 | ShiftElts = IsLE ? LittleEndianShifts[M2 & 0x3] : BigEndianShifts[M2 & 0x3]; |
| 1571 | InsertAtByte = IsLE ? 4 : 8; |
| 1572 | Swap = M2 < 4; |
| 1573 | return true; |
| 1574 | } |
| 1575 | // 0, 1, 2, H or 4, 5, 6, L |
| 1576 | if ((M3 > 3 && M0 == 0 && M1 == 1 && M2 == 2) || |
| 1577 | (M3 < 4 && M0 == 4 && M1 == 5 && M2 == 6)) { |
| 1578 | ShiftElts = IsLE ? LittleEndianShifts[M3 & 0x3] : BigEndianShifts[M3 & 0x3]; |
| 1579 | InsertAtByte = IsLE ? 0 : 12; |
| 1580 | Swap = M3 < 4; |
| 1581 | return true; |
| 1582 | } |
| 1583 | |
| 1584 | // If both vector operands for the shuffle are the same vector, the mask will |
| 1585 | // contain only elements from the first one and the second one will be undef. |
| 1586 | if (N->getOperand(1).isUndef()) { |
| 1587 | ShiftElts = 0; |
| 1588 | Swap = true; |
| 1589 | unsigned XXINSERTWSrcElem = IsLE ? 2 : 1; |
| 1590 | if (M0 == XXINSERTWSrcElem && M1 == 1 && M2 == 2 && M3 == 3) { |
| 1591 | InsertAtByte = IsLE ? 12 : 0; |
| 1592 | return true; |
| 1593 | } |
| 1594 | if (M0 == 0 && M1 == XXINSERTWSrcElem && M2 == 2 && M3 == 3) { |
| 1595 | InsertAtByte = IsLE ? 8 : 4; |
| 1596 | return true; |
| 1597 | } |
| 1598 | if (M0 == 0 && M1 == 1 && M2 == XXINSERTWSrcElem && M3 == 3) { |
| 1599 | InsertAtByte = IsLE ? 4 : 8; |
| 1600 | return true; |
| 1601 | } |
| 1602 | if (M0 == 0 && M1 == 1 && M2 == 2 && M3 == XXINSERTWSrcElem) { |
| 1603 | InsertAtByte = IsLE ? 0 : 12; |
| 1604 | return true; |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | return false; |
| 1609 | } |
| 1610 | |
| Chris Lattner | ffc4756 | 2006-03-20 06:33:01 +0000 | [diff] [blame] | 1611 | /// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the |
| 1612 | /// specified isSplatShuffleMask VECTOR_SHUFFLE mask. |
| Bill Schmidt | f910a06 | 2014-06-10 14:35:01 +0000 | [diff] [blame] | 1613 | unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize, |
| 1614 | SelectionDAG &DAG) { |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1615 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); |
| 1616 | assert(isSplatShuffleMask(SVOp, EltSize)); |
| Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 1617 | if (DAG.getDataLayout().isLittleEndian()) |
| Bill Schmidt | f910a06 | 2014-06-10 14:35:01 +0000 | [diff] [blame] | 1618 | return (16 / EltSize) - 1 - (SVOp->getMaskElt(0) / EltSize); |
| 1619 | else |
| 1620 | return SVOp->getMaskElt(0) / EltSize; |
| Chris Lattner | ffc4756 | 2006-03-20 06:33:01 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
| Chris Lattner | 74cf9ff | 2006-04-12 17:37:20 +0000 | [diff] [blame] | 1623 | /// get_VSPLTI_elt - If this is a build_vector of constants which can be formed |
| Chris Lattner | d71a1f9 | 2006-04-08 06:46:53 +0000 | [diff] [blame] | 1624 | /// by using a vspltis[bhw] instruction of the specified element size, return |
| 1625 | /// the constant being splatted. The ByteSize field indicates the number of |
| 1626 | /// bytes of each element [124] -> [bhw]. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1627 | SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) { |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1628 | SDValue OpVal(nullptr, 0); |
| Chris Lattner | d9e80f4 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 1629 | |
| 1630 | // If ByteSize of the splat is bigger than the element size of the |
| 1631 | // build_vector, then we have a case where we are checking for a splat where |
| 1632 | // multiple elements of the buildvector are folded together into a single |
| 1633 | // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8). |
| 1634 | unsigned EltSize = 16/N->getNumOperands(); |
| 1635 | if (EltSize < ByteSize) { |
| 1636 | unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1637 | SDValue UniquedVals[4]; |
| Chris Lattner | d9e80f4 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 1638 | assert(Multiple > 1 && Multiple <= 4 && "How can this happen?"); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1639 | |
| Chris Lattner | d9e80f4 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 1640 | // See if all of the elements in the buildvector agree across. |
| 1641 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| Sanjay Patel | 5719584 | 2016-03-14 17:28:46 +0000 | [diff] [blame] | 1642 | if (N->getOperand(i).isUndef()) continue; |
| Chris Lattner | d9e80f4 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 1643 | // If the element isn't a constant, bail fully out. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1644 | if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue(); |
| Chris Lattner | d9e80f4 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 1645 | |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1646 | |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1647 | if (!UniquedVals[i&(Multiple-1)].getNode()) |
| Chris Lattner | d9e80f4 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 1648 | UniquedVals[i&(Multiple-1)] = N->getOperand(i); |
| 1649 | else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i)) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1650 | return SDValue(); // no match. |
| Chris Lattner | d9e80f4 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 1651 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1652 | |
| Chris Lattner | d9e80f4 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 1653 | // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains |
| 1654 | // either constant or undef values that are identical for each chunk. See |
| 1655 | // if these chunks can form into a larger vspltis*. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1656 | |
| Chris Lattner | d9e80f4 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 1657 | // Check to see if all of the leading entries are either 0 or -1. If |
| 1658 | // neither, then this won't fit into the immediate field. |
| 1659 | bool LeadingZero = true; |
| 1660 | bool LeadingOnes = true; |
| 1661 | for (unsigned i = 0; i != Multiple-1; ++i) { |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1662 | if (!UniquedVals[i].getNode()) continue; // Must have been undefs. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1663 | |
| Artyom Skrobov | 314ee04 | 2015-11-25 19:41:11 +0000 | [diff] [blame] | 1664 | LeadingZero &= isNullConstant(UniquedVals[i]); |
| 1665 | LeadingOnes &= isAllOnesConstant(UniquedVals[i]); |
| Chris Lattner | d9e80f4 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 1666 | } |
| 1667 | // Finally, check the least significant entry. |
| 1668 | if (LeadingZero) { |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1669 | if (!UniquedVals[Multiple-1].getNode()) |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1670 | return DAG.getTargetConstant(0, SDLoc(N), MVT::i32); // 0,0,0,undef |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1671 | int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue(); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1672 | if (Val < 16) // 0,0,0,4 -> vspltisw(4) |
| 1673 | return DAG.getTargetConstant(Val, SDLoc(N), MVT::i32); |
| Chris Lattner | d9e80f4 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 1674 | } |
| 1675 | if (LeadingOnes) { |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1676 | if (!UniquedVals[Multiple-1].getNode()) |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1677 | return DAG.getTargetConstant(~0U, SDLoc(N), MVT::i32); // -1,-1,-1,undef |
| Dan Gohman | 6e05483 | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 1678 | int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue(); |
| Chris Lattner | d9e80f4 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 1679 | if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2) |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1680 | return DAG.getTargetConstant(Val, SDLoc(N), MVT::i32); |
| Chris Lattner | d9e80f4 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 1681 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1682 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1683 | return SDValue(); |
| Chris Lattner | d9e80f4 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 1684 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1685 | |
| Chris Lattner | 2771e2c | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 1686 | // Check to see if this buildvec has a single non-undef value in its elements. |
| 1687 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| Sanjay Patel | 5719584 | 2016-03-14 17:28:46 +0000 | [diff] [blame] | 1688 | if (N->getOperand(i).isUndef()) continue; |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1689 | if (!OpVal.getNode()) |
| Chris Lattner | 2771e2c | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 1690 | OpVal = N->getOperand(i); |
| 1691 | else if (OpVal != N->getOperand(i)) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1692 | return SDValue(); |
| Chris Lattner | 2771e2c | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 1693 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1694 | |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1695 | if (!OpVal.getNode()) return SDValue(); // All UNDEF: use implicit def. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1696 | |
| Eli Friedman | 9c6ab1a | 2009-05-24 02:03:36 +0000 | [diff] [blame] | 1697 | unsigned ValSizeInBytes = EltSize; |
| Nate Begeman | 1b39287 | 2006-03-28 04:15:58 +0000 | [diff] [blame] | 1698 | uint64_t Value = 0; |
| Chris Lattner | 2771e2c | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 1699 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) { |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1700 | Value = CN->getZExtValue(); |
| Chris Lattner | 2771e2c | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 1701 | } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) { |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1702 | assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!"); |
| Dale Johannesen | 3cf889f | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 1703 | Value = FloatToBits(CN->getValueAPF().convertToFloat()); |
| Chris Lattner | 2771e2c | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | // If the splat value is larger than the element value, then we can never do |
| 1707 | // this splat. The only case that we could fit the replicated bits into our |
| 1708 | // immediate field for would be zero, and we prefer to use vxor for it. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1709 | if (ValSizeInBytes < ByteSize) return SDValue(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1710 | |
| Benjamin Kramer | b4b5150 | 2015-03-25 16:49:59 +0000 | [diff] [blame] | 1711 | // If the element value is larger than the splat value, check if it consists |
| 1712 | // of a repeated bit pattern of size ByteSize. |
| 1713 | if (!APInt(ValSizeInBytes * 8, Value).isSplat(ByteSize * 8)) |
| 1714 | return SDValue(); |
| Chris Lattner | 2771e2c | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 1715 | |
| 1716 | // Properly sign extend the value. |
| Richard Smith | 228e6d4 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 1717 | int MaskVal = SignExtend32(Value, ByteSize * 8); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1718 | |
| Evan Cheng | b1ddc98 | 2006-03-26 09:52:32 +0000 | [diff] [blame] | 1719 | // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1720 | if (MaskVal == 0) return SDValue(); |
| Chris Lattner | 2771e2c | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 1721 | |
| Chris Lattner | d71a1f9 | 2006-04-08 06:46:53 +0000 | [diff] [blame] | 1722 | // Finally, if this value fits in a 5 bit sext field, return it |
| Richard Smith | 228e6d4 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 1723 | if (SignExtend32<5>(MaskVal) == MaskVal) |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1724 | return DAG.getTargetConstant(MaskVal, SDLoc(N), MVT::i32); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1725 | return SDValue(); |
| Chris Lattner | 2771e2c | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 1728 | /// isQVALIGNIShuffleMask - If this is a qvaligni shuffle mask, return the shift |
| 1729 | /// amount, otherwise return -1. |
| 1730 | int PPC::isQVALIGNIShuffleMask(SDNode *N) { |
| 1731 | EVT VT = N->getValueType(0); |
| 1732 | if (VT != MVT::v4f64 && VT != MVT::v4f32 && VT != MVT::v4i1) |
| 1733 | return -1; |
| 1734 | |
| 1735 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); |
| 1736 | |
| 1737 | // Find the first non-undef value in the shuffle mask. |
| 1738 | unsigned i; |
| 1739 | for (i = 0; i != 4 && SVOp->getMaskElt(i) < 0; ++i) |
| 1740 | /*search*/; |
| 1741 | |
| 1742 | if (i == 4) return -1; // all undef. |
| 1743 | |
| 1744 | // Otherwise, check to see if the rest of the elements are consecutively |
| 1745 | // numbered from this value. |
| 1746 | unsigned ShiftAmt = SVOp->getMaskElt(i); |
| 1747 | if (ShiftAmt < i) return -1; |
| 1748 | ShiftAmt -= i; |
| 1749 | |
| 1750 | // Check the rest of the elements to see if they are consecutive. |
| 1751 | for (++i; i != 4; ++i) |
| 1752 | if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i)) |
| 1753 | return -1; |
| 1754 | |
| 1755 | return ShiftAmt; |
| 1756 | } |
| 1757 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1758 | //===----------------------------------------------------------------------===// |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1759 | // Addressing Mode Selection |
| 1760 | //===----------------------------------------------------------------------===// |
| 1761 | |
| 1762 | /// isIntS16Immediate - This method tests to see if the node is either a 32-bit |
| 1763 | /// or 64-bit immediate, and if the value can be accurately represented as a |
| 1764 | /// sign extension from a 16-bit value. If so, this returns true and the |
| 1765 | /// immediate. |
| 1766 | static bool isIntS16Immediate(SDNode *N, short &Imm) { |
| Adam Nemet | 571eb5f | 2014-05-20 17:20:34 +0000 | [diff] [blame] | 1767 | if (!isa<ConstantSDNode>(N)) |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1768 | return false; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1769 | |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1770 | Imm = (short)cast<ConstantSDNode>(N)->getZExtValue(); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1771 | if (N->getValueType(0) == MVT::i32) |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1772 | return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue(); |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1773 | else |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1774 | return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue(); |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1775 | } |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1776 | static bool isIntS16Immediate(SDValue Op, short &Imm) { |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1777 | return isIntS16Immediate(Op.getNode(), Imm); |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1780 | /// SelectAddressRegReg - Given the specified addressed, check to see if it |
| 1781 | /// can be represented as an indexed [r+r] operation. Returns false if it |
| 1782 | /// can be more efficiently represented with [r+imm]. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1783 | bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base, |
| 1784 | SDValue &Index, |
| Dan Gohman | 02b9313 | 2009-01-15 16:29:45 +0000 | [diff] [blame] | 1785 | SelectionDAG &DAG) const { |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1786 | short imm = 0; |
| 1787 | if (N.getOpcode() == ISD::ADD) { |
| 1788 | if (isIntS16Immediate(N.getOperand(1), imm)) |
| 1789 | return false; // r+i |
| 1790 | if (N.getOperand(1).getOpcode() == PPCISD::Lo) |
| 1791 | return false; // r+i |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1792 | |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1793 | Base = N.getOperand(0); |
| 1794 | Index = N.getOperand(1); |
| 1795 | return true; |
| 1796 | } else if (N.getOpcode() == ISD::OR) { |
| 1797 | if (isIntS16Immediate(N.getOperand(1), imm)) |
| 1798 | return false; // r+i can fold it if we can. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1799 | |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1800 | // If this is an or of disjoint bitfields, we can codegen this as an add |
| 1801 | // (for better address arithmetic) if the LHS and RHS of the OR are provably |
| 1802 | // disjoint. |
| Dan Gohman | f19609a | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 1803 | APInt LHSKnownZero, LHSKnownOne; |
| 1804 | APInt RHSKnownZero, RHSKnownOne; |
| Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1805 | DAG.computeKnownBits(N.getOperand(0), |
| 1806 | LHSKnownZero, LHSKnownOne); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1807 | |
| Dan Gohman | f19609a | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 1808 | if (LHSKnownZero.getBoolValue()) { |
| Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1809 | DAG.computeKnownBits(N.getOperand(1), |
| 1810 | RHSKnownZero, RHSKnownOne); |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1811 | // If all of the bits are known zero on the LHS or RHS, the add won't |
| 1812 | // carry. |
| Dan Gohman | 26854f2 | 2008-02-27 21:12:32 +0000 | [diff] [blame] | 1813 | if (~(LHSKnownZero | RHSKnownZero) == 0) { |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1814 | Base = N.getOperand(0); |
| 1815 | Index = N.getOperand(1); |
| 1816 | return true; |
| 1817 | } |
| 1818 | } |
| 1819 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1820 | |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1821 | return false; |
| 1822 | } |
| 1823 | |
| Hal Finkel | dbbf09b | 2013-07-09 06:34:51 +0000 | [diff] [blame] | 1824 | // If we happen to be doing an i64 load or store into a stack slot that has |
| 1825 | // less than a 4-byte alignment, then the frame-index elimination may need to |
| 1826 | // use an indexed load or store instruction (because the offset may not be a |
| 1827 | // multiple of 4). The extra register needed to hold the offset comes from the |
| 1828 | // register scavenger, and it is possible that the scavenger will need to use |
| 1829 | // an emergency spill slot. As a result, we need to make sure that a spill slot |
| 1830 | // is allocated when doing an i64 load/store into a less-than-4-byte-aligned |
| 1831 | // stack slot. |
| 1832 | static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) { |
| 1833 | // FIXME: This does not handle the LWA case. |
| 1834 | if (VT != MVT::i64) |
| 1835 | return; |
| 1836 | |
| Hal Finkel | 7ab3db5 | 2013-07-10 15:29:01 +0000 | [diff] [blame] | 1837 | // NOTE: We'll exclude negative FIs here, which come from argument |
| 1838 | // lowering, because there are no known test cases triggering this problem |
| 1839 | // using packed structures (or similar). We can remove this exclusion if |
| 1840 | // we find such a test case. The reason why this is so test-case driven is |
| 1841 | // because this entire 'fixup' is only to prevent crashes (from the |
| 1842 | // register scavenger) on not-really-valid inputs. For example, if we have: |
| 1843 | // %a = alloca i1 |
| 1844 | // %b = bitcast i1* %a to i64* |
| 1845 | // store i64* a, i64 b |
| 1846 | // then the store should really be marked as 'align 1', but is not. If it |
| 1847 | // were marked as 'align 1' then the indexed form would have been |
| 1848 | // instruction-selected initially, and the problem this 'fixup' is preventing |
| 1849 | // won't happen regardless. |
| Hal Finkel | dbbf09b | 2013-07-09 06:34:51 +0000 | [diff] [blame] | 1850 | if (FrameIdx < 0) |
| 1851 | return; |
| 1852 | |
| 1853 | MachineFunction &MF = DAG.getMachineFunction(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1854 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| Hal Finkel | dbbf09b | 2013-07-09 06:34:51 +0000 | [diff] [blame] | 1855 | |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1856 | unsigned Align = MFI.getObjectAlignment(FrameIdx); |
| Hal Finkel | dbbf09b | 2013-07-09 06:34:51 +0000 | [diff] [blame] | 1857 | if (Align >= 4) |
| 1858 | return; |
| 1859 | |
| 1860 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 1861 | FuncInfo->setHasNonRISpills(); |
| 1862 | } |
| 1863 | |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1864 | /// Returns true if the address N can be represented by a base register plus |
| 1865 | /// a signed 16-bit displacement [r+imm], and if it is not better |
| Ulrich Weigand | 9d980cb | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 1866 | /// represented as reg+reg. If Aligned is true, only accept displacements |
| 1867 | /// suitable for STD and friends, i.e. multiples of 4. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1868 | bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp, |
| Dan Gohman | 02b9313 | 2009-01-15 16:29:45 +0000 | [diff] [blame] | 1869 | SDValue &Base, |
| Ulrich Weigand | 9d980cb | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 1870 | SelectionDAG &DAG, |
| 1871 | bool Aligned) const { |
| Dale Johannesen | ab8e442 | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 1872 | // FIXME dl should come from parent load or store, not from address |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1873 | SDLoc dl(N); |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1874 | // If this can be more profitably realized as r+r, fail. |
| 1875 | if (SelectAddressRegReg(N, Disp, Base, DAG)) |
| 1876 | return false; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1877 | |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1878 | if (N.getOpcode() == ISD::ADD) { |
| 1879 | short imm = 0; |
| Ulrich Weigand | 9d980cb | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 1880 | if (isIntS16Immediate(N.getOperand(1), imm) && |
| 1881 | (!Aligned || (imm & 3) == 0)) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1882 | Disp = DAG.getTargetConstant(imm, dl, N.getValueType()); |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1883 | if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) { |
| 1884 | Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType()); |
| Hal Finkel | dbbf09b | 2013-07-09 06:34:51 +0000 | [diff] [blame] | 1885 | fixupFuncForFI(DAG, FI->getIndex(), N.getValueType()); |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1886 | } else { |
| 1887 | Base = N.getOperand(0); |
| 1888 | } |
| 1889 | return true; // [r+i] |
| 1890 | } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) { |
| 1891 | // Match LOAD (ADD (X, Lo(G))). |
| Gabor Greif | c8a9abe | 2012-04-20 11:41:38 +0000 | [diff] [blame] | 1892 | assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue() |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1893 | && "Cannot handle constant offsets yet!"); |
| 1894 | Disp = N.getOperand(1).getOperand(0); // The global address. |
| 1895 | assert(Disp.getOpcode() == ISD::TargetGlobalAddress || |
| Roman Divacky | e3f15c98 | 2012-06-04 17:36:38 +0000 | [diff] [blame] | 1896 | Disp.getOpcode() == ISD::TargetGlobalTLSAddress || |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1897 | Disp.getOpcode() == ISD::TargetConstantPool || |
| 1898 | Disp.getOpcode() == ISD::TargetJumpTable); |
| 1899 | Base = N.getOperand(0); |
| 1900 | return true; // [&g+r] |
| 1901 | } |
| 1902 | } else if (N.getOpcode() == ISD::OR) { |
| 1903 | short imm = 0; |
| Ulrich Weigand | 9d980cb | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 1904 | if (isIntS16Immediate(N.getOperand(1), imm) && |
| 1905 | (!Aligned || (imm & 3) == 0)) { |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1906 | // If this is an or of disjoint bitfields, we can codegen this as an add |
| 1907 | // (for better address arithmetic) if the LHS and RHS of the OR are |
| 1908 | // provably disjoint. |
| Dan Gohman | f19609a | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 1909 | APInt LHSKnownZero, LHSKnownOne; |
| Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1910 | DAG.computeKnownBits(N.getOperand(0), LHSKnownZero, LHSKnownOne); |
| Bill Wendling | 6306183 | 2008-03-24 23:16:37 +0000 | [diff] [blame] | 1911 | |
| Dan Gohman | f19609a | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 1912 | if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) { |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1913 | // If all of the bits are known zero on the LHS or RHS, the add won't |
| 1914 | // carry. |
| Ulrich Weigand | 55a9665 | 2014-07-20 22:26:40 +0000 | [diff] [blame] | 1915 | if (FrameIndexSDNode *FI = |
| 1916 | dyn_cast<FrameIndexSDNode>(N.getOperand(0))) { |
| 1917 | Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType()); |
| 1918 | fixupFuncForFI(DAG, FI->getIndex(), N.getValueType()); |
| 1919 | } else { |
| 1920 | Base = N.getOperand(0); |
| 1921 | } |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1922 | Disp = DAG.getTargetConstant(imm, dl, N.getValueType()); |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1923 | return true; |
| 1924 | } |
| 1925 | } |
| 1926 | } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) { |
| 1927 | // Loading from a constant address. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1928 | |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1929 | // If this address fits entirely in a 16-bit sext immediate field, codegen |
| 1930 | // this as "d, 0" |
| 1931 | short Imm; |
| Ulrich Weigand | 9d980cb | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 1932 | if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1933 | Disp = DAG.getTargetConstant(Imm, dl, CN->getValueType(0)); |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 1934 | Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO, |
| Hal Finkel | f70c41e | 2013-03-21 23:45:03 +0000 | [diff] [blame] | 1935 | CN->getValueType(0)); |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1936 | return true; |
| 1937 | } |
| Chris Lattner | 4a9c0bb | 2007-02-17 06:44:03 +0000 | [diff] [blame] | 1938 | |
| 1939 | // Handle 32-bit sext immediates with LIS + addr mode. |
| Ulrich Weigand | 9d980cb | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 1940 | if ((CN->getValueType(0) == MVT::i32 || |
| 1941 | (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) && |
| 1942 | (!Aligned || (CN->getZExtValue() & 3) == 0)) { |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1943 | int Addr = (int)CN->getZExtValue(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1944 | |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1945 | // Otherwise, break this down into an LIS + disp. |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1946 | Disp = DAG.getTargetConstant((short)Addr, dl, MVT::i32); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1947 | |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1948 | Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, dl, |
| 1949 | MVT::i32); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1950 | unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8; |
| Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1951 | Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0); |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1952 | return true; |
| 1953 | } |
| 1954 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1955 | |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1956 | Disp = DAG.getTargetConstant(0, dl, getPointerTy(DAG.getDataLayout())); |
| Hal Finkel | dbbf09b | 2013-07-09 06:34:51 +0000 | [diff] [blame] | 1957 | if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) { |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1958 | Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType()); |
| Hal Finkel | dbbf09b | 2013-07-09 06:34:51 +0000 | [diff] [blame] | 1959 | fixupFuncForFI(DAG, FI->getIndex(), N.getValueType()); |
| 1960 | } else |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1961 | Base = N; |
| 1962 | return true; // [r+0] |
| 1963 | } |
| 1964 | |
| 1965 | /// SelectAddressRegRegOnly - Given the specified addressed, force it to be |
| 1966 | /// represented as an indexed [r+r] operation. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1967 | bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base, |
| 1968 | SDValue &Index, |
| Dan Gohman | 02b9313 | 2009-01-15 16:29:45 +0000 | [diff] [blame] | 1969 | SelectionDAG &DAG) const { |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1970 | // Check to see if we can easily represent this as an [r+r] address. This |
| 1971 | // will fail if it thinks that the address is more profitably represented as |
| 1972 | // reg+imm, e.g. where imm = 0. |
| 1973 | if (SelectAddressRegReg(N, Base, Index, DAG)) |
| 1974 | return true; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1975 | |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1976 | // If the operand is an addition, always emit this as [r+r], since this is |
| 1977 | // better (for code size, and execution, as the memop does the add for free) |
| 1978 | // than emitting an explicit add. |
| 1979 | if (N.getOpcode() == ISD::ADD) { |
| 1980 | Base = N.getOperand(0); |
| 1981 | Index = N.getOperand(1); |
| 1982 | return true; |
| 1983 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1984 | |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1985 | // Otherwise, do it the hard way, using R0 as the base register. |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 1986 | Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO, |
| Hal Finkel | f70c41e | 2013-03-21 23:45:03 +0000 | [diff] [blame] | 1987 | N.getValueType()); |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1988 | Index = N; |
| 1989 | return true; |
| 1990 | } |
| 1991 | |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1992 | /// getPreIndexedAddressParts - returns true by value, base pointer and |
| 1993 | /// offset pointer and addressing mode by reference if the node's address |
| 1994 | /// can be legally represented as pre-indexed load / store address. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1995 | bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base, |
| 1996 | SDValue &Offset, |
| Evan Cheng | b150007 | 2006-11-09 17:55:04 +0000 | [diff] [blame] | 1997 | ISD::MemIndexedMode &AM, |
| Dan Gohman | 02b9313 | 2009-01-15 16:29:45 +0000 | [diff] [blame] | 1998 | SelectionDAG &DAG) const { |
| Hal Finkel | 595817e | 2012-06-04 02:21:00 +0000 | [diff] [blame] | 1999 | if (DisablePPCPreinc) return false; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2000 | |
| Ulrich Weigand | e90b022 | 2013-03-22 14:58:48 +0000 | [diff] [blame] | 2001 | bool isLoad = true; |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2002 | SDValue Ptr; |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2003 | EVT VT; |
| Hal Finkel | b09680b | 2013-03-18 23:00:58 +0000 | [diff] [blame] | 2004 | unsigned Alignment; |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 2005 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
| 2006 | Ptr = LD->getBasePtr(); |
| Dan Gohman | 47a7d6f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 2007 | VT = LD->getMemoryVT(); |
| Hal Finkel | b09680b | 2013-03-18 23:00:58 +0000 | [diff] [blame] | 2008 | Alignment = LD->getAlignment(); |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 2009 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { |
| Chris Lattner | 6837125 | 2006-11-14 01:38:31 +0000 | [diff] [blame] | 2010 | Ptr = ST->getBasePtr(); |
| Dan Gohman | 47a7d6f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 2011 | VT = ST->getMemoryVT(); |
| Hal Finkel | b09680b | 2013-03-18 23:00:58 +0000 | [diff] [blame] | 2012 | Alignment = ST->getAlignment(); |
| Ulrich Weigand | e90b022 | 2013-03-22 14:58:48 +0000 | [diff] [blame] | 2013 | isLoad = false; |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 2014 | } else |
| 2015 | return false; |
| 2016 | |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 2017 | // PowerPC doesn't have preinc load/store instructions for vectors (except |
| 2018 | // for QPX, which does have preinc r+r forms). |
| 2019 | if (VT.isVector()) { |
| 2020 | if (!Subtarget.hasQPX() || (VT != MVT::v4f64 && VT != MVT::v4f32)) { |
| 2021 | return false; |
| 2022 | } else if (SelectAddressRegRegOnly(Ptr, Offset, Base, DAG)) { |
| 2023 | AM = ISD::PRE_INC; |
| 2024 | return true; |
| 2025 | } |
| 2026 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2027 | |
| Ulrich Weigand | e90b022 | 2013-03-22 14:58:48 +0000 | [diff] [blame] | 2028 | if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) { |
| 2029 | |
| 2030 | // Common code will reject creating a pre-inc form if the base pointer |
| 2031 | // is a frame index, or if N is a store and the base pointer is either |
| 2032 | // the same as or a predecessor of the value being stored. Check for |
| 2033 | // those situations here, and try with swapped Base/Offset instead. |
| 2034 | bool Swap = false; |
| 2035 | |
| 2036 | if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base)) |
| 2037 | Swap = true; |
| 2038 | else if (!isLoad) { |
| 2039 | SDValue Val = cast<StoreSDNode>(N)->getValue(); |
| 2040 | if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode())) |
| 2041 | Swap = true; |
| 2042 | } |
| 2043 | |
| 2044 | if (Swap) |
| 2045 | std::swap(Base, Offset); |
| 2046 | |
| Hal Finkel | ca542be | 2012-06-20 15:43:03 +0000 | [diff] [blame] | 2047 | AM = ISD::PRE_INC; |
| 2048 | return true; |
| Hal Finkel | 1cc27e4 | 2012-06-19 02:34:32 +0000 | [diff] [blame] | 2049 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2050 | |
| Ulrich Weigand | 9d980cb | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 2051 | // LDU/STU can only handle immediates that are a multiple of 4. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2052 | if (VT != MVT::i64) { |
| Ulrich Weigand | 9d980cb | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 2053 | if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false)) |
| Chris Lattner | 474b5b7 | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 2054 | return false; |
| 2055 | } else { |
| Hal Finkel | b09680b | 2013-03-18 23:00:58 +0000 | [diff] [blame] | 2056 | // LDU/STU need an address with at least 4-byte alignment. |
| 2057 | if (Alignment < 4) |
| 2058 | return false; |
| 2059 | |
| Ulrich Weigand | 9d980cb | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 2060 | if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true)) |
| Chris Lattner | 474b5b7 | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 2061 | return false; |
| 2062 | } |
| Chris Lattner | b314b15 | 2006-11-11 00:08:42 +0000 | [diff] [blame] | 2063 | |
| Chris Lattner | b314b15 | 2006-11-11 00:08:42 +0000 | [diff] [blame] | 2064 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
| Chris Lattner | 474b5b7 | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 2065 | // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of |
| 2066 | // sext i32 to i64 when addr mode is r+i. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2067 | if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 && |
| Chris Lattner | b314b15 | 2006-11-11 00:08:42 +0000 | [diff] [blame] | 2068 | LD->getExtensionType() == ISD::SEXTLOAD && |
| 2069 | isa<ConstantSDNode>(Offset)) |
| 2070 | return false; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
| Chris Lattner | ce64554 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 2073 | AM = ISD::PRE_INC; |
| 2074 | return true; |
| Chris Lattner | a801fced | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | //===----------------------------------------------------------------------===// |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 2078 | // LowerOperation implementation |
| 2079 | //===----------------------------------------------------------------------===// |
| 2080 | |
| Rafael Espindola | e1d255f | 2016-06-27 12:56:02 +0000 | [diff] [blame] | 2081 | /// Return true if we should reference labels using a PICBase, set the HiOpFlags |
| 2082 | /// and LoOpFlags to the target MO flags. |
| 2083 | static void getLabelAccessInfo(bool IsPIC, const PPCSubtarget &Subtarget, |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 2084 | unsigned &HiOpFlags, unsigned &LoOpFlags, |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2085 | const GlobalValue *GV = nullptr) { |
| Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 2086 | HiOpFlags = PPCII::MO_HA; |
| 2087 | LoOpFlags = PPCII::MO_LO; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2088 | |
| Hal Finkel | 3ee2af7 | 2014-07-18 23:29:49 +0000 | [diff] [blame] | 2089 | // Don't use the pic base if not in PIC relocation model. |
| Rafael Espindola | e1d255f | 2016-06-27 12:56:02 +0000 | [diff] [blame] | 2090 | if (IsPIC) { |
| Chris Lattner | dd6df84 | 2010-11-15 03:13:19 +0000 | [diff] [blame] | 2091 | HiOpFlags |= PPCII::MO_PIC_FLAG; |
| 2092 | LoOpFlags |= PPCII::MO_PIC_FLAG; |
| 2093 | } |
| 2094 | |
| 2095 | // If this is a reference to a global value that requires a non-lazy-ptr, make |
| 2096 | // sure that instruction lowering adds it. |
| Eric Christopher | e8dbfe1 | 2015-02-13 22:23:04 +0000 | [diff] [blame] | 2097 | if (GV && Subtarget.hasLazyResolverStub(GV)) { |
| Chris Lattner | dd6df84 | 2010-11-15 03:13:19 +0000 | [diff] [blame] | 2098 | HiOpFlags |= PPCII::MO_NLP_FLAG; |
| 2099 | LoOpFlags |= PPCII::MO_NLP_FLAG; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2100 | |
| Chris Lattner | dd6df84 | 2010-11-15 03:13:19 +0000 | [diff] [blame] | 2101 | if (GV->hasHiddenVisibility()) { |
| 2102 | HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG; |
| 2103 | LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG; |
| 2104 | } |
| 2105 | } |
| Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC, |
| 2109 | SelectionDAG &DAG) { |
| Daniel Jasper | 48e93f7 | 2015-04-28 13:38:35 +0000 | [diff] [blame] | 2110 | SDLoc DL(HiPart); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2111 | EVT PtrVT = HiPart.getValueType(); |
| 2112 | SDValue Zero = DAG.getConstant(0, DL, PtrVT); |
| Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 2113 | |
| 2114 | SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero); |
| 2115 | SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2116 | |
| Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 2117 | // With PIC, the first instruction is actually "GR+hi(&G)". |
| 2118 | if (isPIC) |
| 2119 | Hi = DAG.getNode(ISD::ADD, DL, PtrVT, |
| 2120 | DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2121 | |
| Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 2122 | // Generate non-pic code that has direct accesses to the constant pool. |
| 2123 | // The address of the global is just (hi(&g)+lo(&g)). |
| 2124 | return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo); |
| 2125 | } |
| 2126 | |
| Hal Finkel | e6698d5 | 2015-02-01 15:03:28 +0000 | [diff] [blame] | 2127 | static void setUsesTOCBasePtr(MachineFunction &MF) { |
| 2128 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 2129 | FuncInfo->setUsesTOCBasePtr(); |
| 2130 | } |
| 2131 | |
| 2132 | static void setUsesTOCBasePtr(SelectionDAG &DAG) { |
| 2133 | setUsesTOCBasePtr(DAG.getMachineFunction()); |
| 2134 | } |
| 2135 | |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 2136 | static SDValue getTOCEntry(SelectionDAG &DAG, const SDLoc &dl, bool Is64Bit, |
| Hal Finkel | cf59921 | 2015-02-25 21:36:59 +0000 | [diff] [blame] | 2137 | SDValue GA) { |
| 2138 | EVT VT = Is64Bit ? MVT::i64 : MVT::i32; |
| 2139 | SDValue Reg = Is64Bit ? DAG.getRegister(PPC::X2, VT) : |
| 2140 | DAG.getNode(PPCISD::GlobalBaseReg, dl, VT); |
| 2141 | |
| 2142 | SDValue Ops[] = { GA, Reg }; |
| Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 2143 | return DAG.getMemIntrinsicNode( |
| 2144 | PPCISD::TOC_ENTRY, dl, DAG.getVTList(VT, MVT::Other), Ops, VT, |
| 2145 | MachinePointerInfo::getGOT(DAG.getMachineFunction()), 0, false, true, |
| 2146 | false, 0); |
| Hal Finkel | cf59921 | 2015-02-25 21:36:59 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2149 | SDValue PPCTargetLowering::LowerConstantPool(SDValue Op, |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 2150 | SelectionDAG &DAG) const { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2151 | EVT PtrVT = Op.getValueType(); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 2152 | ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2153 | const Constant *C = CP->getConstVal(); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 2154 | |
| Roman Divacky | ace4707 | 2012-08-24 16:26:02 +0000 | [diff] [blame] | 2155 | // 64-bit SVR4 ABI code is always position-independent. |
| 2156 | // The actual address of the GlobalValue is stored in the TOC. |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 2157 | if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) { |
| Hal Finkel | e6698d5 | 2015-02-01 15:03:28 +0000 | [diff] [blame] | 2158 | setUsesTOCBasePtr(DAG); |
| Roman Divacky | ace4707 | 2012-08-24 16:26:02 +0000 | [diff] [blame] | 2159 | SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0); |
| Hal Finkel | cf59921 | 2015-02-25 21:36:59 +0000 | [diff] [blame] | 2160 | return getTOCEntry(DAG, SDLoc(CP), true, GA); |
| Roman Divacky | ace4707 | 2012-08-24 16:26:02 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
| Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 2163 | unsigned MOHiFlag, MOLoFlag; |
| Rafael Espindola | e1d255f | 2016-06-27 12:56:02 +0000 | [diff] [blame] | 2164 | bool IsPIC = isPositionIndependent(); |
| 2165 | getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag); |
| Hal Finkel | 3ee2af7 | 2014-07-18 23:29:49 +0000 | [diff] [blame] | 2166 | |
| Rafael Espindola | e1d255f | 2016-06-27 12:56:02 +0000 | [diff] [blame] | 2167 | if (IsPIC && Subtarget.isSVR4ABI()) { |
| Hal Finkel | 3ee2af7 | 2014-07-18 23:29:49 +0000 | [diff] [blame] | 2168 | SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), |
| 2169 | PPCII::MO_PIC_FLAG); |
| Hal Finkel | cf59921 | 2015-02-25 21:36:59 +0000 | [diff] [blame] | 2170 | return getTOCEntry(DAG, SDLoc(CP), false, GA); |
| Hal Finkel | 3ee2af7 | 2014-07-18 23:29:49 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
| Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 2173 | SDValue CPIHi = |
| 2174 | DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag); |
| 2175 | SDValue CPILo = |
| 2176 | DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag); |
| Rafael Espindola | e1d255f | 2016-06-27 12:56:02 +0000 | [diff] [blame] | 2177 | return LowerLabelRef(CPIHi, CPILo, IsPIC, DAG); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
| Joerg Sonnenberger | 8c1a9ac | 2016-11-16 00:37:30 +0000 | [diff] [blame] | 2180 | // For 64-bit PowerPC, prefer the more compact relative encodings. |
| 2181 | // This trades 32 bits per jump table entry for one or two instructions |
| 2182 | // on the jump site. |
| 2183 | unsigned PPCTargetLowering::getJumpTableEncoding() const { |
| 2184 | if (isJumpTableRelative()) |
| 2185 | return MachineJumpTableInfo::EK_LabelDifference32; |
| 2186 | |
| 2187 | return TargetLowering::getJumpTableEncoding(); |
| 2188 | } |
| 2189 | |
| 2190 | bool PPCTargetLowering::isJumpTableRelative() const { |
| 2191 | if (Subtarget.isPPC64()) |
| 2192 | return true; |
| 2193 | return TargetLowering::isJumpTableRelative(); |
| 2194 | } |
| 2195 | |
| 2196 | SDValue PPCTargetLowering::getPICJumpTableRelocBase(SDValue Table, |
| 2197 | SelectionDAG &DAG) const { |
| 2198 | if (!Subtarget.isPPC64()) |
| 2199 | return TargetLowering::getPICJumpTableRelocBase(Table, DAG); |
| 2200 | |
| 2201 | switch (getTargetMachine().getCodeModel()) { |
| 2202 | case CodeModel::Default: |
| 2203 | case CodeModel::Small: |
| 2204 | case CodeModel::Medium: |
| 2205 | return TargetLowering::getPICJumpTableRelocBase(Table, DAG); |
| 2206 | default: |
| 2207 | return DAG.getNode(PPCISD::GlobalBaseReg, SDLoc(), |
| 2208 | getPointerTy(DAG.getDataLayout())); |
| 2209 | } |
| 2210 | } |
| 2211 | |
| 2212 | const MCExpr * |
| 2213 | PPCTargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF, |
| 2214 | unsigned JTI, |
| 2215 | MCContext &Ctx) const { |
| 2216 | if (!Subtarget.isPPC64()) |
| 2217 | return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx); |
| 2218 | |
| 2219 | switch (getTargetMachine().getCodeModel()) { |
| 2220 | case CodeModel::Default: |
| 2221 | case CodeModel::Small: |
| 2222 | case CodeModel::Medium: |
| 2223 | return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx); |
| 2224 | default: |
| 2225 | return MCSymbolRefExpr::create(MF->getPICBaseSymbol(), Ctx); |
| 2226 | } |
| 2227 | } |
| 2228 | |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 2229 | SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2230 | EVT PtrVT = Op.getValueType(); |
| Nate Begeman | 4ca2ea5 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 2231 | JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2232 | |
| Roman Divacky | ace4707 | 2012-08-24 16:26:02 +0000 | [diff] [blame] | 2233 | // 64-bit SVR4 ABI code is always position-independent. |
| 2234 | // The actual address of the GlobalValue is stored in the TOC. |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 2235 | if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) { |
| Hal Finkel | e6698d5 | 2015-02-01 15:03:28 +0000 | [diff] [blame] | 2236 | setUsesTOCBasePtr(DAG); |
| Roman Divacky | ace4707 | 2012-08-24 16:26:02 +0000 | [diff] [blame] | 2237 | SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT); |
| Hal Finkel | cf59921 | 2015-02-25 21:36:59 +0000 | [diff] [blame] | 2238 | return getTOCEntry(DAG, SDLoc(JT), true, GA); |
| Roman Divacky | ace4707 | 2012-08-24 16:26:02 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
| Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 2241 | unsigned MOHiFlag, MOLoFlag; |
| Rafael Espindola | e1d255f | 2016-06-27 12:56:02 +0000 | [diff] [blame] | 2242 | bool IsPIC = isPositionIndependent(); |
| 2243 | getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag); |
| Hal Finkel | 3ee2af7 | 2014-07-18 23:29:49 +0000 | [diff] [blame] | 2244 | |
| Rafael Espindola | e1d255f | 2016-06-27 12:56:02 +0000 | [diff] [blame] | 2245 | if (IsPIC && Subtarget.isSVR4ABI()) { |
| Hal Finkel | 3ee2af7 | 2014-07-18 23:29:49 +0000 | [diff] [blame] | 2246 | SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, |
| 2247 | PPCII::MO_PIC_FLAG); |
| Hal Finkel | cf59921 | 2015-02-25 21:36:59 +0000 | [diff] [blame] | 2248 | return getTOCEntry(DAG, SDLoc(GA), false, GA); |
| Hal Finkel | 3ee2af7 | 2014-07-18 23:29:49 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
| Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 2251 | SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag); |
| 2252 | SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag); |
| Rafael Espindola | e1d255f | 2016-06-27 12:56:02 +0000 | [diff] [blame] | 2253 | return LowerLabelRef(JTIHi, JTILo, IsPIC, DAG); |
| Lauro Ramos Venancio | 09d73c0 | 2007-07-11 17:19:51 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 2256 | SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op, |
| 2257 | SelectionDAG &DAG) const { |
| Bob Wilson | f84f710 | 2009-11-04 21:31:18 +0000 | [diff] [blame] | 2258 | EVT PtrVT = Op.getValueType(); |
| Ulrich Weigand | c8c2ea2 | 2014-10-31 10:33:14 +0000 | [diff] [blame] | 2259 | BlockAddressSDNode *BASDN = cast<BlockAddressSDNode>(Op); |
| 2260 | const BlockAddress *BA = BASDN->getBlockAddress(); |
| Bob Wilson | f84f710 | 2009-11-04 21:31:18 +0000 | [diff] [blame] | 2261 | |
| Ulrich Weigand | c8c2ea2 | 2014-10-31 10:33:14 +0000 | [diff] [blame] | 2262 | // 64-bit SVR4 ABI code is always position-independent. |
| 2263 | // The actual BlockAddress is stored in the TOC. |
| 2264 | if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) { |
| Hal Finkel | e6698d5 | 2015-02-01 15:03:28 +0000 | [diff] [blame] | 2265 | setUsesTOCBasePtr(DAG); |
| Ulrich Weigand | c8c2ea2 | 2014-10-31 10:33:14 +0000 | [diff] [blame] | 2266 | SDValue GA = DAG.getTargetBlockAddress(BA, PtrVT, BASDN->getOffset()); |
| Hal Finkel | cf59921 | 2015-02-25 21:36:59 +0000 | [diff] [blame] | 2267 | return getTOCEntry(DAG, SDLoc(BASDN), true, GA); |
| Ulrich Weigand | c8c2ea2 | 2014-10-31 10:33:14 +0000 | [diff] [blame] | 2268 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2269 | |
| Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 2270 | unsigned MOHiFlag, MOLoFlag; |
| Rafael Espindola | e1d255f | 2016-06-27 12:56:02 +0000 | [diff] [blame] | 2271 | bool IsPIC = isPositionIndependent(); |
| 2272 | getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag); |
| Michael Liao | abb87d4 | 2012-09-12 21:43:09 +0000 | [diff] [blame] | 2273 | SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag); |
| 2274 | SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag); |
| Rafael Espindola | e1d255f | 2016-06-27 12:56:02 +0000 | [diff] [blame] | 2275 | return LowerLabelRef(TgtBAHi, TgtBALo, IsPIC, DAG); |
| Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
| Roman Divacky | e3f15c98 | 2012-06-04 17:36:38 +0000 | [diff] [blame] | 2278 | SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op, |
| 2279 | SelectionDAG &DAG) const { |
| 2280 | |
| Bill Schmidt | bdae03f | 2013-09-17 20:22:05 +0000 | [diff] [blame] | 2281 | // FIXME: TLS addresses currently use medium model code sequences, |
| 2282 | // which is the most useful form. Eventually support for small and |
| 2283 | // large models could be added if users need it, at the cost of |
| 2284 | // additional complexity. |
| Roman Divacky | e3f15c98 | 2012-06-04 17:36:38 +0000 | [diff] [blame] | 2285 | GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); |
| Chih-Hung Hsieh | 1e85958 | 2015-07-28 16:24:05 +0000 | [diff] [blame] | 2286 | if (DAG.getTarget().Options.EmulatedTLS) |
| 2287 | return LowerToTLSEmulatedModel(GA, DAG); |
| 2288 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2289 | SDLoc dl(GA); |
| Roman Divacky | e3f15c98 | 2012-06-04 17:36:38 +0000 | [diff] [blame] | 2290 | const GlobalValue *GV = GA->getGlobal(); |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2291 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 2292 | bool is64bit = Subtarget.isPPC64(); |
| Justin Hibbits | a88b605 | 2014-11-12 15:16:30 +0000 | [diff] [blame] | 2293 | const Module *M = DAG.getMachineFunction().getFunction()->getParent(); |
| 2294 | PICLevel::Level picLevel = M->getPICLevel(); |
| Roman Divacky | e3f15c98 | 2012-06-04 17:36:38 +0000 | [diff] [blame] | 2295 | |
| Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 2296 | TLSModel::Model Model = getTargetMachine().getTLSModel(GV); |
| Roman Divacky | e3f15c98 | 2012-06-04 17:36:38 +0000 | [diff] [blame] | 2297 | |
| Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 2298 | if (Model == TLSModel::LocalExec) { |
| 2299 | SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, |
| Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 2300 | PPCII::MO_TPREL_HA); |
| Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 2301 | SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, |
| Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 2302 | PPCII::MO_TPREL_LO); |
| Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 2303 | SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2, |
| 2304 | is64bit ? MVT::i64 : MVT::i32); |
| 2305 | SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg); |
| 2306 | return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi); |
| 2307 | } |
| Roman Divacky | e3f15c98 | 2012-06-04 17:36:38 +0000 | [diff] [blame] | 2308 | |
| Bill Schmidt | c56f1d3 | 2012-12-11 20:30:11 +0000 | [diff] [blame] | 2309 | if (Model == TLSModel::InitialExec) { |
| Bill Schmidt | 732eb91 | 2012-12-13 18:45:54 +0000 | [diff] [blame] | 2310 | SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0); |
| Ulrich Weigand | 5b42759 | 2013-07-05 12:22:36 +0000 | [diff] [blame] | 2311 | SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, |
| 2312 | PPCII::MO_TLS); |
| Roman Divacky | 32143e2 | 2013-12-20 18:08:54 +0000 | [diff] [blame] | 2313 | SDValue GOTPtr; |
| 2314 | if (is64bit) { |
| Hal Finkel | e6698d5 | 2015-02-01 15:03:28 +0000 | [diff] [blame] | 2315 | setUsesTOCBasePtr(DAG); |
| Roman Divacky | 32143e2 | 2013-12-20 18:08:54 +0000 | [diff] [blame] | 2316 | SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64); |
| 2317 | GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl, |
| 2318 | PtrVT, GOTReg, TGA); |
| 2319 | } else |
| 2320 | GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT); |
| Bill Schmidt | 9f0b4ec | 2012-12-14 17:02:38 +0000 | [diff] [blame] | 2321 | SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl, |
| Roman Divacky | 32143e2 | 2013-12-20 18:08:54 +0000 | [diff] [blame] | 2322 | PtrVT, TGA, GOTPtr); |
| Ulrich Weigand | 5b42759 | 2013-07-05 12:22:36 +0000 | [diff] [blame] | 2323 | return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS); |
| Bill Schmidt | c56f1d3 | 2012-12-11 20:30:11 +0000 | [diff] [blame] | 2324 | } |
| Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 2325 | |
| Bill Schmidt | c56f1d3 | 2012-12-11 20:30:11 +0000 | [diff] [blame] | 2326 | if (Model == TLSModel::GeneralDynamic) { |
| Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 2327 | SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0); |
| Hal Finkel | 7c8ae53 | 2014-07-25 17:47:22 +0000 | [diff] [blame] | 2328 | SDValue GOTPtr; |
| 2329 | if (is64bit) { |
| Hal Finkel | e6698d5 | 2015-02-01 15:03:28 +0000 | [diff] [blame] | 2330 | setUsesTOCBasePtr(DAG); |
| Hal Finkel | 7c8ae53 | 2014-07-25 17:47:22 +0000 | [diff] [blame] | 2331 | SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64); |
| 2332 | GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT, |
| 2333 | GOTReg, TGA); |
| 2334 | } else { |
| Davide Italiano | 4cccc48 | 2016-06-17 18:07:14 +0000 | [diff] [blame] | 2335 | if (picLevel == PICLevel::SmallPIC) |
| Justin Hibbits | a88b605 | 2014-11-12 15:16:30 +0000 | [diff] [blame] | 2336 | GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT); |
| 2337 | else |
| 2338 | GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT); |
| Hal Finkel | 7c8ae53 | 2014-07-25 17:47:22 +0000 | [diff] [blame] | 2339 | } |
| Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 2340 | return DAG.getNode(PPCISD::ADDI_TLSGD_L_ADDR, dl, PtrVT, |
| 2341 | GOTPtr, TGA, TGA); |
| Bill Schmidt | c56f1d3 | 2012-12-11 20:30:11 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
| Bill Schmidt | 24b8dd6 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 2344 | if (Model == TLSModel::LocalDynamic) { |
| Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 2345 | SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0); |
| Hal Finkel | 7c8ae53 | 2014-07-25 17:47:22 +0000 | [diff] [blame] | 2346 | SDValue GOTPtr; |
| 2347 | if (is64bit) { |
| Hal Finkel | e6698d5 | 2015-02-01 15:03:28 +0000 | [diff] [blame] | 2348 | setUsesTOCBasePtr(DAG); |
| Hal Finkel | 7c8ae53 | 2014-07-25 17:47:22 +0000 | [diff] [blame] | 2349 | SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64); |
| 2350 | GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT, |
| 2351 | GOTReg, TGA); |
| 2352 | } else { |
| Davide Italiano | 4cccc48 | 2016-06-17 18:07:14 +0000 | [diff] [blame] | 2353 | if (picLevel == PICLevel::SmallPIC) |
| Justin Hibbits | a88b605 | 2014-11-12 15:16:30 +0000 | [diff] [blame] | 2354 | GOTPtr = DAG.getNode(PPCISD::GlobalBaseReg, dl, PtrVT); |
| 2355 | else |
| 2356 | GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT); |
| Hal Finkel | 7c8ae53 | 2014-07-25 17:47:22 +0000 | [diff] [blame] | 2357 | } |
| Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 2358 | SDValue TLSAddr = DAG.getNode(PPCISD::ADDI_TLSLD_L_ADDR, dl, |
| 2359 | PtrVT, GOTPtr, TGA, TGA); |
| 2360 | SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, |
| 2361 | PtrVT, TLSAddr, TGA); |
| Bill Schmidt | 24b8dd6 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 2362 | return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA); |
| 2363 | } |
| 2364 | |
| 2365 | llvm_unreachable("Unknown TLS model!"); |
| Roman Divacky | e3f15c98 | 2012-06-04 17:36:38 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
| Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 2368 | SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op, |
| 2369 | SelectionDAG &DAG) const { |
| 2370 | EVT PtrVT = Op.getValueType(); |
| 2371 | GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2372 | SDLoc DL(GSDN); |
| Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 2373 | const GlobalValue *GV = GSDN->getGlobal(); |
| 2374 | |
| Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 2375 | // 64-bit SVR4 ABI code is always position-independent. |
| 2376 | // The actual address of the GlobalValue is stored in the TOC. |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 2377 | if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) { |
| Hal Finkel | e6698d5 | 2015-02-01 15:03:28 +0000 | [diff] [blame] | 2378 | setUsesTOCBasePtr(DAG); |
| Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 2379 | SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset()); |
| Hal Finkel | cf59921 | 2015-02-25 21:36:59 +0000 | [diff] [blame] | 2380 | return getTOCEntry(DAG, DL, true, GA); |
| Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
| Chris Lattner | dd6df84 | 2010-11-15 03:13:19 +0000 | [diff] [blame] | 2383 | unsigned MOHiFlag, MOLoFlag; |
| Rafael Espindola | e1d255f | 2016-06-27 12:56:02 +0000 | [diff] [blame] | 2384 | bool IsPIC = isPositionIndependent(); |
| 2385 | getLabelAccessInfo(IsPIC, Subtarget, MOHiFlag, MOLoFlag, GV); |
| Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 2386 | |
| Rafael Espindola | e1d255f | 2016-06-27 12:56:02 +0000 | [diff] [blame] | 2387 | if (IsPIC && Subtarget.isSVR4ABI()) { |
| Hal Finkel | 3ee2af7 | 2014-07-18 23:29:49 +0000 | [diff] [blame] | 2388 | SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, |
| 2389 | GSDN->getOffset(), |
| 2390 | PPCII::MO_PIC_FLAG); |
| Hal Finkel | cf59921 | 2015-02-25 21:36:59 +0000 | [diff] [blame] | 2391 | return getTOCEntry(DAG, DL, false, GA); |
| Hal Finkel | 3ee2af7 | 2014-07-18 23:29:49 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
| Chris Lattner | dd6df84 | 2010-11-15 03:13:19 +0000 | [diff] [blame] | 2394 | SDValue GAHi = |
| 2395 | DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag); |
| 2396 | SDValue GALo = |
| 2397 | DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2398 | |
| Rafael Espindola | e1d255f | 2016-06-27 12:56:02 +0000 | [diff] [blame] | 2399 | SDValue Ptr = LowerLabelRef(GAHi, GALo, IsPIC, DAG); |
| Bob Wilson | f84f710 | 2009-11-04 21:31:18 +0000 | [diff] [blame] | 2400 | |
| Chris Lattner | dd6df84 | 2010-11-15 03:13:19 +0000 | [diff] [blame] | 2401 | // If the global reference is actually to a non-lazy-pointer, we have to do an |
| 2402 | // extra load to get the address of the global. |
| 2403 | if (MOHiFlag & PPCII::MO_NLP_FLAG) |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 2404 | Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo()); |
| Chris Lattner | dd6df84 | 2010-11-15 03:13:19 +0000 | [diff] [blame] | 2405 | return Ptr; |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 2408 | SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const { |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 2409 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2410 | SDLoc dl(Op); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2411 | |
| Hal Finkel | 777c9dd | 2014-03-29 16:04:40 +0000 | [diff] [blame] | 2412 | if (Op.getValueType() == MVT::v2i64) { |
| 2413 | // When the operands themselves are v2i64 values, we need to do something |
| 2414 | // special because VSX has no underlying comparison operations for these. |
| 2415 | if (Op.getOperand(0).getValueType() == MVT::v2i64) { |
| 2416 | // Equality can be handled by casting to the legal type for Altivec |
| 2417 | // comparisons, everything else needs to be expanded. |
| 2418 | if (CC == ISD::SETEQ || CC == ISD::SETNE) { |
| 2419 | return DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, |
| 2420 | DAG.getSetCC(dl, MVT::v4i32, |
| 2421 | DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0)), |
| 2422 | DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(1)), |
| 2423 | CC)); |
| 2424 | } |
| 2425 | |
| 2426 | return SDValue(); |
| 2427 | } |
| 2428 | |
| 2429 | // We handle most of these in the usual way. |
| 2430 | return Op; |
| 2431 | } |
| 2432 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 2433 | // If we're comparing for equality to zero, expose the fact that this is |
| Sanjay Patel | 9cc21ac | 2016-07-06 16:42:46 +0000 | [diff] [blame] | 2434 | // implemented as a ctlz/srl pair on ppc, so that the dag combiner can |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 2435 | // fold the new nodes. |
| Pierre Gousseau | 051db7d | 2016-08-16 13:53:53 +0000 | [diff] [blame] | 2436 | if (SDValue V = lowerCmpEqZeroToCtlzSrl(Op, DAG)) |
| 2437 | return V; |
| 2438 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 2439 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2440 | // Leave comparisons against 0 and -1 alone for now, since they're usually |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 2441 | // optimized. FIXME: revisit this when we can custom lower all setcc |
| 2442 | // optimizations. |
| 2443 | if (C->isAllOnesValue() || C->isNullValue()) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2444 | return SDValue(); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 2445 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2446 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 2447 | // If we have an integer seteq/setne, turn it into a compare against zero |
| Chris Lattner | 97ff46b | 2006-11-14 05:28:08 +0000 | [diff] [blame] | 2448 | // by xor'ing the rhs with the lhs, which is faster than setting a |
| 2449 | // condition register, reading it back out, and masking the correct bit. The |
| 2450 | // normal approach here uses sub to do this instead of xor. Using xor exposes |
| 2451 | // the result to other bit-twiddling opportunities. |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2452 | EVT LHSVT = Op.getOperand(0).getValueType(); |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2453 | if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2454 | EVT VT = Op.getValueType(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2455 | SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0), |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 2456 | Op.getOperand(1)); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2457 | return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, dl, LHSVT), CC); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 2458 | } |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2459 | return SDValue(); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
| Eric Christopher | b976a39 | 2016-07-07 00:39:27 +0000 | [diff] [blame] | 2462 | SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const { |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2463 | SDNode *Node = Op.getNode(); |
| 2464 | EVT VT = Node->getValueType(0); |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 2465 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2466 | SDValue InChain = Node->getOperand(0); |
| 2467 | SDValue VAListPtr = Node->getOperand(1); |
| 2468 | const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue(); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2469 | SDLoc dl(Node); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2470 | |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2471 | assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only"); |
| 2472 | |
| 2473 | // gpr_index |
| 2474 | SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain, |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 2475 | VAListPtr, MachinePointerInfo(SV), MVT::i8); |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2476 | InChain = GprIndex.getValue(1); |
| 2477 | |
| 2478 | if (VT == MVT::i64) { |
| 2479 | // Check if GprIndex is even |
| 2480 | SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2481 | DAG.getConstant(1, dl, MVT::i32)); |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2482 | SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2483 | DAG.getConstant(0, dl, MVT::i32), ISD::SETNE); |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2484 | SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2485 | DAG.getConstant(1, dl, MVT::i32)); |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2486 | // Align GprIndex to be even if it isn't |
| 2487 | GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne, |
| 2488 | GprIndex); |
| 2489 | } |
| 2490 | |
| 2491 | // fpr index is 1 byte after gpr |
| 2492 | SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2493 | DAG.getConstant(1, dl, MVT::i32)); |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2494 | |
| 2495 | // fpr |
| 2496 | SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain, |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 2497 | FprPtr, MachinePointerInfo(SV), MVT::i8); |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2498 | InChain = FprIndex.getValue(1); |
| 2499 | |
| 2500 | SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2501 | DAG.getConstant(8, dl, MVT::i32)); |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2502 | |
| 2503 | SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2504 | DAG.getConstant(4, dl, MVT::i32)); |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2505 | |
| 2506 | // areas |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 2507 | SDValue OverflowArea = |
| 2508 | DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr, MachinePointerInfo()); |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2509 | InChain = OverflowArea.getValue(1); |
| 2510 | |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 2511 | SDValue RegSaveArea = |
| 2512 | DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr, MachinePointerInfo()); |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2513 | InChain = RegSaveArea.getValue(1); |
| 2514 | |
| 2515 | // select overflow_area if index > 8 |
| 2516 | SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2517 | DAG.getConstant(8, dl, MVT::i32), ISD::SETLT); |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2518 | |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2519 | // adjustment constant gpr_index * 4/8 |
| 2520 | SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32, |
| 2521 | VT.isInteger() ? GprIndex : FprIndex, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2522 | DAG.getConstant(VT.isInteger() ? 4 : 8, dl, |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2523 | MVT::i32)); |
| 2524 | |
| 2525 | // OurReg = RegSaveArea + RegConstant |
| 2526 | SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea, |
| 2527 | RegConstant); |
| 2528 | |
| 2529 | // Floating types are 32 bytes into RegSaveArea |
| 2530 | if (VT.isFloatingPoint()) |
| 2531 | OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2532 | DAG.getConstant(32, dl, MVT::i32)); |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2533 | |
| 2534 | // increase {f,g}pr_index by 1 (or 2 if VT is i64) |
| 2535 | SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
| 2536 | VT.isInteger() ? GprIndex : FprIndex, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2537 | DAG.getConstant(VT == MVT::i64 ? 2 : 1, dl, |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2538 | MVT::i32)); |
| 2539 | |
| 2540 | InChain = DAG.getTruncStore(InChain, dl, IndexPlus1, |
| 2541 | VT.isInteger() ? VAListPtr : FprPtr, |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 2542 | MachinePointerInfo(SV), MVT::i8); |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2543 | |
| 2544 | // determine if we should load from reg_save_area or overflow_area |
| 2545 | SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea); |
| 2546 | |
| 2547 | // increase overflow_area by 4/8 if gpr/fpr > 8 |
| 2548 | SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea, |
| 2549 | DAG.getConstant(VT.isInteger() ? 4 : 8, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2550 | dl, MVT::i32)); |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2551 | |
| 2552 | OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea, |
| 2553 | OverflowAreaPlusN); |
| 2554 | |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 2555 | InChain = DAG.getTruncStore(InChain, dl, OverflowArea, OverflowAreaPtr, |
| 2556 | MachinePointerInfo(), MVT::i32); |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 2557 | |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 2558 | return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo()); |
| Nicolas Geoffray | 23710a7 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
| Eric Christopher | b976a39 | 2016-07-07 00:39:27 +0000 | [diff] [blame] | 2561 | SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const { |
| Roman Divacky | c3825df | 2013-07-25 21:36:47 +0000 | [diff] [blame] | 2562 | assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only"); |
| 2563 | |
| 2564 | // We have to copy the entire va_list struct: |
| 2565 | // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte |
| 2566 | return DAG.getMemcpy(Op.getOperand(0), Op, |
| 2567 | Op.getOperand(1), Op.getOperand(2), |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2568 | DAG.getConstant(12, SDLoc(Op), MVT::i32), 8, false, true, |
| 2569 | false, MachinePointerInfo(), MachinePointerInfo()); |
| Roman Divacky | c3825df | 2013-07-25 21:36:47 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
| Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2572 | SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op, |
| 2573 | SelectionDAG &DAG) const { |
| 2574 | return Op.getOperand(0); |
| 2575 | } |
| 2576 | |
| 2577 | SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op, |
| 2578 | SelectionDAG &DAG) const { |
| Bill Wendling | 95e1af2 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 2579 | SDValue Chain = Op.getOperand(0); |
| 2580 | SDValue Trmp = Op.getOperand(1); // trampoline |
| 2581 | SDValue FPtr = Op.getOperand(2); // nested function |
| 2582 | SDValue Nest = Op.getOperand(3); // 'nest' parameter value |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2583 | SDLoc dl(Op); |
| Bill Wendling | 95e1af2 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 2584 | |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 2585 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2586 | bool isPPC64 = (PtrVT == MVT::i64); |
| Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 2587 | Type *IntPtrTy = DAG.getDataLayout().getIntPtrType(*DAG.getContext()); |
| Bill Wendling | 95e1af2 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 2588 | |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2589 | TargetLowering::ArgListTy Args; |
| Bill Wendling | 95e1af2 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 2590 | TargetLowering::ArgListEntry Entry; |
| 2591 | |
| 2592 | Entry.Ty = IntPtrTy; |
| 2593 | Entry.Node = Trmp; Args.push_back(Entry); |
| 2594 | |
| 2595 | // TrampSize == (isPPC64 ? 48 : 40); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2596 | Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40, dl, |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2597 | isPPC64 ? MVT::i64 : MVT::i32); |
| Bill Wendling | 95e1af2 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 2598 | Args.push_back(Entry); |
| 2599 | |
| 2600 | Entry.Node = FPtr; Args.push_back(Entry); |
| 2601 | Entry.Node = Nest; Args.push_back(Entry); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2602 | |
| Bill Wendling | 95e1af2 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 2603 | // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg) |
| Saleem Abdulrasool | f3a5a5c | 2014-05-17 21:50:17 +0000 | [diff] [blame] | 2604 | TargetLowering::CallLoweringInfo CLI(DAG); |
| 2605 | CLI.setDebugLoc(dl).setChain(Chain) |
| 2606 | .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), |
| Juergen Ributzka | 3bd03c7 | 2014-07-01 22:01:54 +0000 | [diff] [blame] | 2607 | DAG.getExternalSymbol("__trampoline_setup", PtrVT), |
| Krzysztof Parzyszek | e116d500 | 2016-06-22 12:54:25 +0000 | [diff] [blame] | 2608 | std::move(Args)); |
| Bill Wendling | 95e1af2 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 2609 | |
| Saleem Abdulrasool | f3a5a5c | 2014-05-17 21:50:17 +0000 | [diff] [blame] | 2610 | std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); |
| Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 2611 | return CallResult.second; |
| Bill Wendling | 95e1af2 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
| Eric Christopher | b976a39 | 2016-07-07 00:39:27 +0000 | [diff] [blame] | 2614 | SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const { |
| Dan Gohman | 31ae586 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2615 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2616 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 2617 | EVT PtrVT = getPointerTy(MF.getDataLayout()); |
| Dan Gohman | 31ae586 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2618 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2619 | SDLoc dl(Op); |
| Nicolas Geoffray | 23710a7 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 2620 | |
| Tilmann Scheller | d1aaa32 | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 2621 | if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) { |
| Nicolas Geoffray | 23710a7 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 2622 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 2623 | // memory location argument. |
| Dan Gohman | 31ae586 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2624 | SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); |
| Dan Gohman | 2d489b5 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 2625 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); |
| Chris Lattner | 676c61d | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 2626 | return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 2627 | MachinePointerInfo(SV)); |
| Nicolas Geoffray | 23710a7 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 2628 | } |
| 2629 | |
| Tilmann Scheller | d1aaa32 | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 2630 | // For the 32-bit SVR4 ABI we follow the layout of the va_list struct. |
| Nicolas Geoffray | 23710a7 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 2631 | // We suppose the given va_list is already allocated. |
| 2632 | // |
| 2633 | // typedef struct { |
| 2634 | // char gpr; /* index into the array of 8 GPRs |
| 2635 | // * stored in the register save area |
| 2636 | // * gpr=0 corresponds to r3, |
| 2637 | // * gpr=1 to r4, etc. |
| 2638 | // */ |
| 2639 | // char fpr; /* index into the array of 8 FPRs |
| 2640 | // * stored in the register save area |
| 2641 | // * fpr=0 corresponds to f1, |
| 2642 | // * fpr=1 to f2, etc. |
| 2643 | // */ |
| 2644 | // char *overflow_arg_area; |
| 2645 | // /* location on stack that holds |
| 2646 | // * the next overflow argument |
| 2647 | // */ |
| 2648 | // char *reg_save_area; |
| 2649 | // /* where r3:r10 and f1:f8 (if saved) |
| 2650 | // * are stored |
| 2651 | // */ |
| 2652 | // } va_list[1]; |
| 2653 | |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2654 | SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), dl, MVT::i32); |
| 2655 | SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), dl, MVT::i32); |
| Dan Gohman | 31ae586 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2656 | SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(), |
| 2657 | PtrVT); |
| 2658 | SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), |
| 2659 | PtrVT); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2660 | |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2661 | uint64_t FrameOffset = PtrVT.getSizeInBits()/8; |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2662 | SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, dl, PtrVT); |
| Dan Gohman | 2d489b5 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 2663 | |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2664 | uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1; |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2665 | SDValue ConstStackOffset = DAG.getConstant(StackOffset, dl, PtrVT); |
| Dan Gohman | 2d489b5 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 2666 | |
| 2667 | uint64_t FPROffset = 1; |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2668 | SDValue ConstFPROffset = DAG.getConstant(FPROffset, dl, PtrVT); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2669 | |
| Dan Gohman | 2d489b5 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 2670 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2671 | |
| Nicolas Geoffray | 23710a7 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 2672 | // Store first byte : number of int regs |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 2673 | SDValue firstStore = |
| 2674 | DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR, Op.getOperand(1), |
| 2675 | MachinePointerInfo(SV), MVT::i8); |
| Dan Gohman | 2d489b5 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 2676 | uint64_t nextOffset = FPROffset; |
| Dale Johannesen | 021052a | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 2677 | SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1), |
| Nicolas Geoffray | 23710a7 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 2678 | ConstFPROffset); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2679 | |
| Nicolas Geoffray | 23710a7 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 2680 | // Store second byte : number of float regs |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2681 | SDValue secondStore = |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 2682 | DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr, |
| 2683 | MachinePointerInfo(SV, nextOffset), MVT::i8); |
| Dan Gohman | 2d489b5 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 2684 | nextOffset += StackOffset; |
| Dale Johannesen | 021052a | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 2685 | nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2686 | |
| Nicolas Geoffray | 23710a7 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 2687 | // Store second word : arguments given on stack |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 2688 | SDValue thirdStore = DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr, |
| 2689 | MachinePointerInfo(SV, nextOffset)); |
| Dan Gohman | 2d489b5 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 2690 | nextOffset += FrameOffset; |
| Dale Johannesen | 021052a | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 2691 | nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset); |
| Nicolas Geoffray | 23710a7 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 2692 | |
| 2693 | // Store third word : arguments given in registers |
| Chris Lattner | 676c61d | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 2694 | return DAG.getStore(thirdStore, dl, FR, nextPtr, |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 2695 | MachinePointerInfo(SV, nextOffset)); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
| Chris Lattner | 4f2e4e0 | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 2698 | #include "PPCGenCallingConv.inc" |
| 2699 | |
| NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 2700 | // Function whose sole purpose is to kill compiler warnings |
| Bill Schmidt | 8c3976e | 2013-08-26 20:11:46 +0000 | [diff] [blame] | 2701 | // stemming from unused functions included from PPCGenCallingConv.inc. |
| 2702 | CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const { |
| Bill Schmidt | 8470b0f | 2013-08-30 22:18:55 +0000 | [diff] [blame] | 2703 | return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS; |
| Bill Schmidt | 8c3976e | 2013-08-26 20:11:46 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
| Bill Schmidt | 230b451 | 2013-06-12 16:39:22 +0000 | [diff] [blame] | 2706 | bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT, |
| 2707 | CCValAssign::LocInfo &LocInfo, |
| 2708 | ISD::ArgFlagsTy &ArgFlags, |
| 2709 | CCState &State) { |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2710 | return true; |
| 2711 | } |
| 2712 | |
| Bill Schmidt | 230b451 | 2013-06-12 16:39:22 +0000 | [diff] [blame] | 2713 | bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT, |
| 2714 | MVT &LocVT, |
| 2715 | CCValAssign::LocInfo &LocInfo, |
| 2716 | ISD::ArgFlagsTy &ArgFlags, |
| 2717 | CCState &State) { |
| Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 2718 | static const MCPhysReg ArgRegs[] = { |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2719 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 2720 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 2721 | }; |
| 2722 | const unsigned NumArgRegs = array_lengthof(ArgRegs); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2723 | |
| Tim Northover | 3b6b7ca | 2015-02-21 02:11:17 +0000 | [diff] [blame] | 2724 | unsigned RegNum = State.getFirstUnallocated(ArgRegs); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2725 | |
| 2726 | // Skip one register if the first unallocated register has an even register |
| 2727 | // number and there are still argument registers available which have not been |
| 2728 | // allocated yet. RegNum is actually an index into ArgRegs, which means we |
| 2729 | // need to skip a register if RegNum is odd. |
| 2730 | if (RegNum != NumArgRegs && RegNum % 2 == 1) { |
| 2731 | State.AllocateReg(ArgRegs[RegNum]); |
| 2732 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2733 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2734 | // Always return false here, as this function only makes sure that the first |
| 2735 | // unallocated register has an odd register number and does not actually |
| 2736 | // allocate a register for the current argument. |
| 2737 | return false; |
| 2738 | } |
| 2739 | |
| Strahinja Petrovic | 30e0ce8 | 2016-08-05 08:47:26 +0000 | [diff] [blame] | 2740 | bool |
| 2741 | llvm::CC_PPC32_SVR4_Custom_SkipLastArgRegsPPCF128(unsigned &ValNo, MVT &ValVT, |
| 2742 | MVT &LocVT, |
| 2743 | CCValAssign::LocInfo &LocInfo, |
| 2744 | ISD::ArgFlagsTy &ArgFlags, |
| 2745 | CCState &State) { |
| 2746 | static const MCPhysReg ArgRegs[] = { |
| 2747 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 2748 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 2749 | }; |
| 2750 | const unsigned NumArgRegs = array_lengthof(ArgRegs); |
| 2751 | |
| 2752 | unsigned RegNum = State.getFirstUnallocated(ArgRegs); |
| 2753 | int RegsLeft = NumArgRegs - RegNum; |
| 2754 | |
| 2755 | // Skip if there is not enough registers left for long double type (4 gpr regs |
| 2756 | // in soft float mode) and put long double argument on the stack. |
| 2757 | if (RegNum != NumArgRegs && RegsLeft < 4) { |
| 2758 | for (int i = 0; i < RegsLeft; i++) { |
| 2759 | State.AllocateReg(ArgRegs[RegNum + i]); |
| 2760 | } |
| 2761 | } |
| 2762 | |
| 2763 | return false; |
| 2764 | } |
| 2765 | |
| Bill Schmidt | 230b451 | 2013-06-12 16:39:22 +0000 | [diff] [blame] | 2766 | bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT, |
| 2767 | MVT &LocVT, |
| 2768 | CCValAssign::LocInfo &LocInfo, |
| 2769 | ISD::ArgFlagsTy &ArgFlags, |
| 2770 | CCState &State) { |
| Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 2771 | static const MCPhysReg ArgRegs[] = { |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2772 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 2773 | PPC::F8 |
| 2774 | }; |
| 2775 | |
| 2776 | const unsigned NumArgRegs = array_lengthof(ArgRegs); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2777 | |
| Tim Northover | 3b6b7ca | 2015-02-21 02:11:17 +0000 | [diff] [blame] | 2778 | unsigned RegNum = State.getFirstUnallocated(ArgRegs); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2779 | |
| 2780 | // If there is only one Floating-point register left we need to put both f64 |
| 2781 | // values of a split ppc_fp128 value on the stack. |
| 2782 | if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) { |
| 2783 | State.AllocateReg(ArgRegs[RegNum]); |
| 2784 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2785 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2786 | // Always return false here, as this function only makes sure that the two f64 |
| 2787 | // values a ppc_fp128 value is split into are both passed in registers or both |
| 2788 | // passed on the stack and does not actually allocate a register for the |
| 2789 | // current argument. |
| 2790 | return false; |
| 2791 | } |
| 2792 | |
| Benjamin Kramer | 7149aab | 2015-03-01 18:09:56 +0000 | [diff] [blame] | 2793 | /// FPR - The set of FP registers that should be allocated for arguments, |
| Tilmann Scheller | d1aaa32 | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 2794 | /// on Darwin. |
| Benjamin Kramer | 7149aab | 2015-03-01 18:09:56 +0000 | [diff] [blame] | 2795 | static const MCPhysReg FPR[] = {PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, |
| 2796 | PPC::F6, PPC::F7, PPC::F8, PPC::F9, PPC::F10, |
| 2797 | PPC::F11, PPC::F12, PPC::F13}; |
| Tilmann Scheller | d1aaa32 | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 2798 | |
| Benjamin Kramer | 7149aab | 2015-03-01 18:09:56 +0000 | [diff] [blame] | 2799 | /// QFPR - The set of QPX registers that should be allocated for arguments. |
| 2800 | static const MCPhysReg QFPR[] = { |
| 2801 | PPC::QF1, PPC::QF2, PPC::QF3, PPC::QF4, PPC::QF5, PPC::QF6, PPC::QF7, |
| 2802 | PPC::QF8, PPC::QF9, PPC::QF10, PPC::QF11, PPC::QF12, PPC::QF13}; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 2803 | |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2804 | /// CalculateStackSlotSize - Calculates the size reserved for this argument on |
| 2805 | /// the stack. |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2806 | static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags, |
| Tilmann Scheller | 98bdaaa | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 2807 | unsigned PtrByteSize) { |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 2808 | unsigned ArgSize = ArgVT.getStoreSize(); |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2809 | if (Flags.isByVal()) |
| 2810 | ArgSize = Flags.getByValSize(); |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 2811 | |
| 2812 | // Round up to multiples of the pointer size, except for array members, |
| 2813 | // which are always packed. |
| 2814 | if (!Flags.isInConsecutiveRegs()) |
| 2815 | ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2816 | |
| 2817 | return ArgSize; |
| 2818 | } |
| Ulrich Weigand | ec2bf93 | 2014-07-07 19:26:41 +0000 | [diff] [blame] | 2819 | |
| 2820 | /// CalculateStackSlotAlignment - Calculates the alignment of this argument |
| 2821 | /// on the stack. |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 2822 | static unsigned CalculateStackSlotAlignment(EVT ArgVT, EVT OrigVT, |
| 2823 | ISD::ArgFlagsTy Flags, |
| Ulrich Weigand | ec2bf93 | 2014-07-07 19:26:41 +0000 | [diff] [blame] | 2824 | unsigned PtrByteSize) { |
| 2825 | unsigned Align = PtrByteSize; |
| 2826 | |
| 2827 | // Altivec parameters are padded to a 16 byte boundary. |
| 2828 | if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 || |
| 2829 | ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 || |
| Kit Barton | d4eb73c | 2015-05-05 16:10:44 +0000 | [diff] [blame] | 2830 | ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64 || |
| 2831 | ArgVT == MVT::v1i128) |
| Ulrich Weigand | ec2bf93 | 2014-07-07 19:26:41 +0000 | [diff] [blame] | 2832 | Align = 16; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 2833 | // QPX vector types stored in double-precision are padded to a 32 byte |
| 2834 | // boundary. |
| 2835 | else if (ArgVT == MVT::v4f64 || ArgVT == MVT::v4i1) |
| 2836 | Align = 32; |
| Ulrich Weigand | ec2bf93 | 2014-07-07 19:26:41 +0000 | [diff] [blame] | 2837 | |
| 2838 | // ByVal parameters are aligned as requested. |
| 2839 | if (Flags.isByVal()) { |
| 2840 | unsigned BVAlign = Flags.getByValAlign(); |
| 2841 | if (BVAlign > PtrByteSize) { |
| 2842 | if (BVAlign % PtrByteSize != 0) |
| 2843 | llvm_unreachable( |
| 2844 | "ByVal alignment is not a multiple of the pointer size"); |
| 2845 | |
| 2846 | Align = BVAlign; |
| 2847 | } |
| 2848 | } |
| 2849 | |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 2850 | // Array members are always packed to their original alignment. |
| 2851 | if (Flags.isInConsecutiveRegs()) { |
| 2852 | // If the array member was split into multiple registers, the first |
| 2853 | // needs to be aligned to the size of the full type. (Except for |
| 2854 | // ppcf128, which is only aligned as its f64 components.) |
| 2855 | if (Flags.isSplit() && OrigVT != MVT::ppcf128) |
| 2856 | Align = OrigVT.getStoreSize(); |
| 2857 | else |
| 2858 | Align = ArgVT.getStoreSize(); |
| 2859 | } |
| 2860 | |
| Ulrich Weigand | ec2bf93 | 2014-07-07 19:26:41 +0000 | [diff] [blame] | 2861 | return Align; |
| 2862 | } |
| 2863 | |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 2864 | /// CalculateStackSlotUsed - Return whether this argument will use its |
| 2865 | /// stack slot (instead of being passed in registers). ArgOffset, |
| 2866 | /// AvailableFPRs, and AvailableVRs must hold the current argument |
| 2867 | /// position, and will be updated to account for this argument. |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 2868 | static bool CalculateStackSlotUsed(EVT ArgVT, EVT OrigVT, |
| 2869 | ISD::ArgFlagsTy Flags, |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 2870 | unsigned PtrByteSize, |
| 2871 | unsigned LinkageSize, |
| 2872 | unsigned ParamAreaSize, |
| 2873 | unsigned &ArgOffset, |
| 2874 | unsigned &AvailableFPRs, |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 2875 | unsigned &AvailableVRs, bool HasQPX) { |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 2876 | bool UseMemory = false; |
| 2877 | |
| 2878 | // Respect alignment of argument on the stack. |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 2879 | unsigned Align = |
| 2880 | CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize); |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 2881 | ArgOffset = ((ArgOffset + Align - 1) / Align) * Align; |
| 2882 | // If there's no space left in the argument save area, we must |
| 2883 | // use memory (this check also catches zero-sized arguments). |
| 2884 | if (ArgOffset >= LinkageSize + ParamAreaSize) |
| 2885 | UseMemory = true; |
| 2886 | |
| 2887 | // Allocate argument on the stack. |
| 2888 | ArgOffset += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize); |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 2889 | if (Flags.isInConsecutiveRegsLast()) |
| 2890 | ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 2891 | // If we overran the argument save area, we must use memory |
| 2892 | // (this check catches arguments passed partially in memory) |
| 2893 | if (ArgOffset > LinkageSize + ParamAreaSize) |
| 2894 | UseMemory = true; |
| 2895 | |
| 2896 | // However, if the argument is actually passed in an FPR or a VR, |
| 2897 | // we don't use memory after all. |
| 2898 | if (!Flags.isByVal()) { |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 2899 | if (ArgVT == MVT::f32 || ArgVT == MVT::f64 || |
| 2900 | // QPX registers overlap with the scalar FP registers. |
| 2901 | (HasQPX && (ArgVT == MVT::v4f32 || |
| 2902 | ArgVT == MVT::v4f64 || |
| 2903 | ArgVT == MVT::v4i1))) |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 2904 | if (AvailableFPRs > 0) { |
| 2905 | --AvailableFPRs; |
| 2906 | return false; |
| 2907 | } |
| 2908 | if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 || |
| 2909 | ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 || |
| Kit Barton | d4eb73c | 2015-05-05 16:10:44 +0000 | [diff] [blame] | 2910 | ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64 || |
| 2911 | ArgVT == MVT::v1i128) |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 2912 | if (AvailableVRs > 0) { |
| 2913 | --AvailableVRs; |
| 2914 | return false; |
| 2915 | } |
| 2916 | } |
| 2917 | |
| 2918 | return UseMemory; |
| 2919 | } |
| 2920 | |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 2921 | /// EnsureStackAlignment - Round stack frame size up from NumBytes to |
| 2922 | /// ensure minimum alignment required for target. |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 2923 | static unsigned EnsureStackAlignment(const PPCFrameLowering *Lowering, |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 2924 | unsigned NumBytes) { |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 2925 | unsigned TargetAlign = Lowering->getStackAlignment(); |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 2926 | unsigned AlignMask = TargetAlign - 1; |
| 2927 | NumBytes = (NumBytes + AlignMask) & ~AlignMask; |
| 2928 | return NumBytes; |
| 2929 | } |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2930 | |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 2931 | SDValue PPCTargetLowering::LowerFormalArguments( |
| 2932 | SDValue Chain, CallingConv::ID CallConv, bool isVarArg, |
| 2933 | const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, |
| 2934 | SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 2935 | if (Subtarget.isSVR4ABI()) { |
| 2936 | if (Subtarget.isPPC64()) |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2937 | return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins, |
| 2938 | dl, DAG, InVals); |
| 2939 | else |
| 2940 | return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins, |
| 2941 | dl, DAG, InVals); |
| Bill Schmidt | 019cc6f | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 2942 | } else { |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2943 | return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins, |
| 2944 | dl, DAG, InVals); |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2945 | } |
| 2946 | } |
| 2947 | |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 2948 | SDValue PPCTargetLowering::LowerFormalArguments_32SVR4( |
| 2949 | SDValue Chain, CallingConv::ID CallConv, bool isVarArg, |
| 2950 | const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, |
| 2951 | SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2952 | |
| Tilmann Scheller | d1aaa32 | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 2953 | // 32-bit SVR4 ABI Stack Frame Layout: |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2954 | // +-----------------------------------+ |
| 2955 | // +--> | Back chain | |
| 2956 | // | +-----------------------------------+ |
| 2957 | // | | Floating-point register save area | |
| 2958 | // | +-----------------------------------+ |
| 2959 | // | | General register save area | |
| 2960 | // | +-----------------------------------+ |
| 2961 | // | | CR save word | |
| 2962 | // | +-----------------------------------+ |
| 2963 | // | | VRSAVE save word | |
| 2964 | // | +-----------------------------------+ |
| 2965 | // | | Alignment padding | |
| 2966 | // | +-----------------------------------+ |
| 2967 | // | | Vector register save area | |
| 2968 | // | +-----------------------------------+ |
| 2969 | // | | Local variable space | |
| 2970 | // | +-----------------------------------+ |
| 2971 | // | | Parameter list area | |
| 2972 | // | +-----------------------------------+ |
| 2973 | // | | LR save word | |
| 2974 | // | +-----------------------------------+ |
| 2975 | // SP--> +--- | Back chain | |
| 2976 | // +-----------------------------------+ |
| 2977 | // |
| 2978 | // Specifications: |
| 2979 | // System V Application Binary Interface PowerPC Processor Supplement |
| 2980 | // AltiVec Technology Programming Interface Manual |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2981 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2982 | MachineFunction &MF = DAG.getMachineFunction(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 2983 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| Dan Gohman | 31ae586 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2984 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2985 | |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 2986 | EVT PtrVT = getPointerTy(MF.getDataLayout()); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2987 | // Potential tail calls could cause overwriting of argument stack slots. |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2988 | bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt && |
| 2989 | (CallConv == CallingConv::Fast)); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2990 | unsigned PtrByteSize = 4; |
| 2991 | |
| 2992 | // Assign locations to all of the incoming arguments. |
| 2993 | SmallVector<CCValAssign, 16> ArgLocs; |
| Strahinja Petrovic | e682b80 | 2016-05-09 12:27:39 +0000 | [diff] [blame] | 2994 | PPCCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, |
| Eric Christopher | b521750 | 2014-08-06 18:45:26 +0000 | [diff] [blame] | 2995 | *DAG.getContext()); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2996 | |
| 2997 | // Reserve space for the linkage area on the stack. |
| Eric Christopher | a4ae213 | 2015-02-13 22:22:57 +0000 | [diff] [blame] | 2998 | unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize(); |
| Ulrich Weigand | 8ca988f | 2014-06-23 14:15:53 +0000 | [diff] [blame] | 2999 | CCInfo.AllocateStack(LinkageSize, PtrByteSize); |
| Eric Christopher | 317df66 | 2016-07-07 01:49:57 +0000 | [diff] [blame] | 3000 | if (useSoftFloat()) |
| Strahinja Petrovic | e682b80 | 2016-05-09 12:27:39 +0000 | [diff] [blame] | 3001 | CCInfo.PreAnalyzeFormalArguments(Ins); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3002 | |
| Bill Schmidt | ef17c14 | 2013-02-06 17:33:58 +0000 | [diff] [blame] | 3003 | CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4); |
| Strahinja Petrovic | e682b80 | 2016-05-09 12:27:39 +0000 | [diff] [blame] | 3004 | CCInfo.clearWasPPCF128(); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3005 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3006 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 3007 | CCValAssign &VA = ArgLocs[i]; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3008 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3009 | // Arguments stored in registers. |
| 3010 | if (VA.isRegLoc()) { |
| Craig Topper | 760b134 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 3011 | const TargetRegisterClass *RC; |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3012 | EVT ValVT = VA.getValVT(); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3013 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3014 | switch (ValVT.getSimpleVT().SimpleTy) { |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3015 | default: |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3016 | llvm_unreachable("ValVT not supported by formal arguments Lowering"); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 3017 | case MVT::i1: |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3018 | case MVT::i32: |
| Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 3019 | RC = &PPC::GPRCRegClass; |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3020 | break; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3021 | case MVT::f32: |
| Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 3022 | if (Subtarget.hasP8Vector()) |
| 3023 | RC = &PPC::VSSRCRegClass; |
| 3024 | else |
| 3025 | RC = &PPC::F4RCRegClass; |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3026 | break; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3027 | case MVT::f64: |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 3028 | if (Subtarget.hasVSX()) |
| Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 3029 | RC = &PPC::VSFRCRegClass; |
| 3030 | else |
| 3031 | RC = &PPC::F8RCRegClass; |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3032 | break; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3033 | case MVT::v16i8: |
| 3034 | case MVT::v8i16: |
| 3035 | case MVT::v4i32: |
| Hal Finkel | 7811c61 | 2014-03-28 19:58:11 +0000 | [diff] [blame] | 3036 | RC = &PPC::VRRCRegClass; |
| 3037 | break; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 3038 | case MVT::v4f32: |
| 3039 | RC = Subtarget.hasQPX() ? &PPC::QSRCRegClass : &PPC::VRRCRegClass; |
| 3040 | break; |
| Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 3041 | case MVT::v2f64: |
| Hal Finkel | a6c8b51 | 2014-03-26 16:12:58 +0000 | [diff] [blame] | 3042 | case MVT::v2i64: |
| Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 3043 | RC = &PPC::VRRCRegClass; |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3044 | break; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 3045 | case MVT::v4f64: |
| 3046 | RC = &PPC::QFRCRegClass; |
| 3047 | break; |
| 3048 | case MVT::v4i1: |
| 3049 | RC = &PPC::QBRCRegClass; |
| 3050 | break; |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3051 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3052 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3053 | // Transform the arguments stored in physical registers into virtual ones. |
| Devang Patel | f3292b2 | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 3054 | unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 3055 | SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, |
| 3056 | ValVT == MVT::i1 ? MVT::i32 : ValVT); |
| 3057 | |
| 3058 | if (ValVT == MVT::i1) |
| 3059 | ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3060 | |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3061 | InVals.push_back(ArgValue); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3062 | } else { |
| 3063 | // Argument stored in memory. |
| 3064 | assert(VA.isMemLoc()); |
| 3065 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 3066 | unsigned ArgSize = VA.getLocVT().getStoreSize(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 3067 | int FI = MFI.CreateFixedObject(ArgSize, VA.getLocMemOffset(), |
| 3068 | isImmutable); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3069 | |
| 3070 | // Create load nodes to retrieve arguments from the stack. |
| 3071 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 3072 | InVals.push_back( |
| 3073 | DAG.getLoad(VA.getValVT(), dl, Chain, FIN, MachinePointerInfo())); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3074 | } |
| 3075 | } |
| 3076 | |
| 3077 | // Assign locations to all of the incoming aggregate by value arguments. |
| 3078 | // Aggregates passed by value are stored in the local variable space of the |
| 3079 | // caller's stack frame, right above the parameter list area. |
| 3080 | SmallVector<CCValAssign, 16> ByValArgLocs; |
| Eric Christopher | 0713a9d | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 3081 | CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(), |
| Eric Christopher | b521750 | 2014-08-06 18:45:26 +0000 | [diff] [blame] | 3082 | ByValArgLocs, *DAG.getContext()); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3083 | |
| 3084 | // Reserve stack space for the allocations in CCInfo. |
| 3085 | CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize); |
| 3086 | |
| Bill Schmidt | ef17c14 | 2013-02-06 17:33:58 +0000 | [diff] [blame] | 3087 | CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3088 | |
| 3089 | // Area that is at least reserved in the caller of this function. |
| 3090 | unsigned MinReservedArea = CCByValInfo.getNextStackOffset(); |
| Ulrich Weigand | 8ca988f | 2014-06-23 14:15:53 +0000 | [diff] [blame] | 3091 | MinReservedArea = std::max(MinReservedArea, LinkageSize); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3092 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3093 | // Set the size that is at least reserved in caller of this function. Tail |
| 3094 | // call optimized function's reserved stack space needs to be aligned so that |
| 3095 | // taking the difference between two stack areas will result in an aligned |
| 3096 | // stack. |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 3097 | MinReservedArea = |
| 3098 | EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea); |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 3099 | FuncInfo->setMinReservedArea(MinReservedArea); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3100 | |
| 3101 | SmallVector<SDValue, 8> MemOps; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3102 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3103 | // If the function takes variable number of arguments, make a frame index for |
| 3104 | // the start of the first vararg value... for expansion of llvm.va_start. |
| 3105 | if (isVarArg) { |
| Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 3106 | static const MCPhysReg GPArgRegs[] = { |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3107 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 3108 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 3109 | }; |
| 3110 | const unsigned NumGPArgRegs = array_lengthof(GPArgRegs); |
| 3111 | |
| Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 3112 | static const MCPhysReg FPArgRegs[] = { |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3113 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 3114 | PPC::F8 |
| 3115 | }; |
| Joerg Sonnenberger | eb8655a | 2014-08-08 16:46:10 +0000 | [diff] [blame] | 3116 | unsigned NumFPArgRegs = array_lengthof(FPArgRegs); |
| Petar Jovanovic | 280f710 | 2015-12-14 17:57:33 +0000 | [diff] [blame] | 3117 | |
| Eric Christopher | 317df66 | 2016-07-07 01:49:57 +0000 | [diff] [blame] | 3118 | if (useSoftFloat()) |
| Petar Jovanovic | 280f710 | 2015-12-14 17:57:33 +0000 | [diff] [blame] | 3119 | NumFPArgRegs = 0; |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3120 | |
| Tim Northover | 3b6b7ca | 2015-02-21 02:11:17 +0000 | [diff] [blame] | 3121 | FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs)); |
| 3122 | FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs)); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3123 | |
| 3124 | // Make room for NumGPArgRegs and NumFPArgRegs. |
| 3125 | int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 + |
| Craig Topper | 7ff1592 | 2014-09-10 04:51:36 +0000 | [diff] [blame] | 3126 | NumFPArgRegs * MVT(MVT::f64).getSizeInBits()/8; |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3127 | |
| Dan Gohman | 31ae586 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 3128 | FuncInfo->setVarArgsStackOffset( |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 3129 | MFI.CreateFixedObject(PtrVT.getSizeInBits()/8, |
| 3130 | CCInfo.getNextStackOffset(), true)); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3131 | |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 3132 | FuncInfo->setVarArgsFrameIndex(MFI.CreateStackObject(Depth, 8, false)); |
| Dan Gohman | 31ae586 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 3133 | SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3134 | |
| Jakob Stoklund Olesen | 6c4353e | 2010-10-11 20:43:09 +0000 | [diff] [blame] | 3135 | // The fixed integer arguments of a variadic function are stored to the |
| Nick Lewycky | 9980075 | 2016-06-28 01:45:05 +0000 | [diff] [blame] | 3136 | // VarArgsFrameIndex on the stack so that they may be loaded by |
| 3137 | // dereferencing the result of va_next. |
| Jakob Stoklund Olesen | 6c4353e | 2010-10-11 20:43:09 +0000 | [diff] [blame] | 3138 | for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) { |
| 3139 | // Get an existing live-in vreg, or add a new one. |
| 3140 | unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]); |
| 3141 | if (!VReg) |
| Devang Patel | f3292b2 | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 3142 | VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3143 | |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3144 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 3145 | SDValue Store = |
| 3146 | DAG.getStore(Val.getValue(1), dl, Val, FIN, MachinePointerInfo()); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3147 | MemOps.push_back(Store); |
| 3148 | // Increment the address by four for the next argument to store |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3149 | SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, dl, PtrVT); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3150 | FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); |
| 3151 | } |
| 3152 | |
| Tilmann Scheller | d1aaa32 | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 3153 | // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6 |
| 3154 | // is set. |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3155 | // The double arguments are stored to the VarArgsFrameIndex |
| 3156 | // on the stack. |
| Jakob Stoklund Olesen | 6c4353e | 2010-10-11 20:43:09 +0000 | [diff] [blame] | 3157 | for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) { |
| 3158 | // Get an existing live-in vreg, or add a new one. |
| 3159 | unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]); |
| 3160 | if (!VReg) |
| Devang Patel | f3292b2 | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 3161 | VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3162 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3163 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 3164 | SDValue Store = |
| 3165 | DAG.getStore(Val.getValue(1), dl, Val, FIN, MachinePointerInfo()); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3166 | MemOps.push_back(Store); |
| 3167 | // Increment the address by eight for the next argument to store |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3168 | SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8, dl, |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3169 | PtrVT); |
| 3170 | FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); |
| 3171 | } |
| 3172 | } |
| 3173 | |
| 3174 | if (!MemOps.empty()) |
| Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 3175 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3176 | |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3177 | return Chain; |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3178 | } |
| 3179 | |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3180 | // PPC64 passes i8, i16, and i32 values in i64 registers. Promote |
| 3181 | // value to MVT::i64 and then truncate to the correct register size. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 3182 | SDValue PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, |
| 3183 | EVT ObjectVT, SelectionDAG &DAG, |
| 3184 | SDValue ArgVal, |
| 3185 | const SDLoc &dl) const { |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3186 | if (Flags.isSExt()) |
| 3187 | ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal, |
| 3188 | DAG.getValueType(ObjectVT)); |
| 3189 | else if (Flags.isZExt()) |
| 3190 | ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal, |
| 3191 | DAG.getValueType(ObjectVT)); |
| Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 3192 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 3193 | return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 3196 | SDValue PPCTargetLowering::LowerFormalArguments_64SVR4( |
| 3197 | SDValue Chain, CallingConv::ID CallConv, bool isVarArg, |
| 3198 | const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, |
| 3199 | SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3200 | // TODO: add description of PPC stack frame format, or at least some docs. |
| 3201 | // |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 3202 | bool isELFv2ABI = Subtarget.isELFv2ABI(); |
| Ulrich Weigand | 59c6ab2 | 2014-06-20 16:34:05 +0000 | [diff] [blame] | 3203 | bool isLittleEndian = Subtarget.isLittleEndian(); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3204 | MachineFunction &MF = DAG.getMachineFunction(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 3205 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3206 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 3207 | |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 3208 | assert(!(CallConv == CallingConv::Fast && isVarArg) && |
| 3209 | "fastcc not supported on varargs functions"); |
| 3210 | |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 3211 | EVT PtrVT = getPointerTy(MF.getDataLayout()); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3212 | // Potential tail calls could cause overwriting of argument stack slots. |
| 3213 | bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt && |
| 3214 | (CallConv == CallingConv::Fast)); |
| 3215 | unsigned PtrByteSize = 8; |
| Eric Christopher | a4ae213 | 2015-02-13 22:22:57 +0000 | [diff] [blame] | 3216 | unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize(); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3217 | |
| Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 3218 | static const MCPhysReg GPR[] = { |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3219 | PPC::X3, PPC::X4, PPC::X5, PPC::X6, |
| 3220 | PPC::X7, PPC::X8, PPC::X9, PPC::X10, |
| 3221 | }; |
| Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 3222 | static const MCPhysReg VR[] = { |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3223 | PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, |
| 3224 | PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 |
| 3225 | }; |
| 3226 | |
| 3227 | const unsigned Num_GPR_Regs = array_lengthof(GPR); |
| Hal Finkel | a932105 | 2016-10-02 02:10:20 +0000 | [diff] [blame] | 3228 | const unsigned Num_FPR_Regs = useSoftFloat() ? 0 : 13; |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3229 | const unsigned Num_VR_Regs = array_lengthof(VR); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 3230 | const unsigned Num_QFPR_Regs = Num_FPR_Regs; |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3231 | |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 3232 | // Do a first pass over the arguments to determine whether the ABI |
| 3233 | // guarantees that our caller has allocated the parameter save area |
| 3234 | // on its stack frame. In the ELFv1 ABI, this is always the case; |
| 3235 | // in the ELFv2 ABI, it is true if this is a vararg function or if |
| 3236 | // any parameter is located in a stack slot. |
| 3237 | |
| 3238 | bool HasParameterArea = !isELFv2ABI || isVarArg; |
| 3239 | unsigned ParamAreaSize = Num_GPR_Regs * PtrByteSize; |
| 3240 | unsigned NumBytes = LinkageSize; |
| 3241 | unsigned AvailableFPRs = Num_FPR_Regs; |
| 3242 | unsigned AvailableVRs = Num_VR_Regs; |
| Hal Finkel | 965cea5 | 2015-07-12 00:37:44 +0000 | [diff] [blame] | 3243 | for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 3244 | if (Ins[i].Flags.isNest()) |
| 3245 | continue; |
| 3246 | |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 3247 | if (CalculateStackSlotUsed(Ins[i].VT, Ins[i].ArgVT, Ins[i].Flags, |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 3248 | PtrByteSize, LinkageSize, ParamAreaSize, |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 3249 | NumBytes, AvailableFPRs, AvailableVRs, |
| 3250 | Subtarget.hasQPX())) |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 3251 | HasParameterArea = true; |
| Hal Finkel | 965cea5 | 2015-07-12 00:37:44 +0000 | [diff] [blame] | 3252 | } |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3253 | |
| 3254 | // Add DAG nodes to load the arguments or copy them out of registers. On |
| 3255 | // entry to a function on PPC, the arguments start after the linkage area, |
| 3256 | // although the first ones are often in registers. |
| 3257 | |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 3258 | unsigned ArgOffset = LinkageSize; |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 3259 | unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 3260 | unsigned &QFPR_idx = FPR_idx; |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3261 | SmallVector<SDValue, 8> MemOps; |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3262 | Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin(); |
| Bill Schmidt | 6631e94 | 2013-02-20 17:31:41 +0000 | [diff] [blame] | 3263 | unsigned CurArgIdx = 0; |
| 3264 | for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) { |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3265 | SDValue ArgVal; |
| 3266 | bool needsLoad = false; |
| 3267 | EVT ObjectVT = Ins[ArgNo].VT; |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 3268 | EVT OrigVT = Ins[ArgNo].ArgVT; |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 3269 | unsigned ObjSize = ObjectVT.getStoreSize(); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3270 | unsigned ArgSize = ObjSize; |
| 3271 | ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags; |
| Andrew Trick | 05938a5 | 2015-02-16 18:10:47 +0000 | [diff] [blame] | 3272 | if (Ins[ArgNo].isOrigArg()) { |
| 3273 | std::advance(FuncArg, Ins[ArgNo].getOrigArgIndex() - CurArgIdx); |
| 3274 | CurArgIdx = Ins[ArgNo].getOrigArgIndex(); |
| 3275 | } |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 3276 | // We re-align the argument offset for each argument, except when using the |
| 3277 | // fast calling convention, when we need to make sure we do that only when |
| 3278 | // we'll actually use a stack slot. |
| 3279 | unsigned CurArgOffset, Align; |
| 3280 | auto ComputeArgOffset = [&]() { |
| 3281 | /* Respect alignment of argument on the stack. */ |
| 3282 | Align = CalculateStackSlotAlignment(ObjectVT, OrigVT, Flags, PtrByteSize); |
| 3283 | ArgOffset = ((ArgOffset + Align - 1) / Align) * Align; |
| 3284 | CurArgOffset = ArgOffset; |
| 3285 | }; |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3286 | |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 3287 | if (CallConv != CallingConv::Fast) { |
| 3288 | ComputeArgOffset(); |
| 3289 | |
| 3290 | /* Compute GPR index associated with argument offset. */ |
| 3291 | GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize; |
| 3292 | GPR_idx = std::min(GPR_idx, Num_GPR_Regs); |
| 3293 | } |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3294 | |
| 3295 | // FIXME the codegen can be much improved in some cases. |
| 3296 | // We do not have to keep everything in memory. |
| 3297 | if (Flags.isByVal()) { |
| Andrew Trick | 05938a5 | 2015-02-16 18:10:47 +0000 | [diff] [blame] | 3298 | assert(Ins[ArgNo].isOrigArg() && "Byval arguments cannot be implicit"); |
| 3299 | |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 3300 | if (CallConv == CallingConv::Fast) |
| 3301 | ComputeArgOffset(); |
| 3302 | |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3303 | // ObjSize is the true size, ArgSize rounded up to multiple of registers. |
| 3304 | ObjSize = Flags.getByValSize(); |
| 3305 | ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; |
| Bill Schmidt | 9953cf2 | 2012-10-31 01:15:05 +0000 | [diff] [blame] | 3306 | // Empty aggregate parameters do not take up registers. Examples: |
| 3307 | // struct { } a; |
| 3308 | // union { } b; |
| 3309 | // int c[0]; |
| 3310 | // etc. However, we have to provide a place-holder in InVals, so |
| 3311 | // pretend we have an 8-byte item at the current address for that |
| 3312 | // purpose. |
| 3313 | if (!ObjSize) { |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 3314 | int FI = MFI.CreateFixedObject(PtrByteSize, ArgOffset, true); |
| Bill Schmidt | 9953cf2 | 2012-10-31 01:15:05 +0000 | [diff] [blame] | 3315 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
| 3316 | InVals.push_back(FIN); |
| 3317 | continue; |
| 3318 | } |
| Hal Finkel | 262a224 | 2013-09-12 23:20:06 +0000 | [diff] [blame] | 3319 | |
| Ulrich Weigand | 2419597 | 2014-07-20 22:36:52 +0000 | [diff] [blame] | 3320 | // Create a stack object covering all stack doublewords occupied |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 3321 | // by the argument. If the argument is (fully or partially) on |
| 3322 | // the stack, or if the argument is fully in registers but the |
| 3323 | // caller has allocated the parameter save anyway, we can refer |
| 3324 | // directly to the caller's stack frame. Otherwise, create a |
| 3325 | // local copy in our own frame. |
| 3326 | int FI; |
| 3327 | if (HasParameterArea || |
| 3328 | ArgSize + ArgOffset > LinkageSize + Num_GPR_Regs * PtrByteSize) |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 3329 | FI = MFI.CreateFixedObject(ArgSize, ArgOffset, false, true); |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 3330 | else |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 3331 | FI = MFI.CreateStackObject(ArgSize, Align, false); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3332 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
| Bill Schmidt | 6ed3b99 | 2012-10-25 13:38:09 +0000 | [diff] [blame] | 3333 | |
| Ulrich Weigand | 2419597 | 2014-07-20 22:36:52 +0000 | [diff] [blame] | 3334 | // Handle aggregates smaller than 8 bytes. |
| 3335 | if (ObjSize < PtrByteSize) { |
| 3336 | // The value of the object is its address, which differs from the |
| 3337 | // address of the enclosing doubleword on big-endian systems. |
| 3338 | SDValue Arg = FIN; |
| 3339 | if (!isLittleEndian) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3340 | SDValue ArgOff = DAG.getConstant(PtrByteSize - ObjSize, dl, PtrVT); |
| Ulrich Weigand | 2419597 | 2014-07-20 22:36:52 +0000 | [diff] [blame] | 3341 | Arg = DAG.getNode(ISD::ADD, dl, ArgOff.getValueType(), Arg, ArgOff); |
| 3342 | } |
| 3343 | InVals.push_back(Arg); |
| 3344 | |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3345 | if (GPR_idx != Num_GPR_Regs) { |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 3346 | unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3347 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); |
| Bill Schmidt | 6ed3b99 | 2012-10-25 13:38:09 +0000 | [diff] [blame] | 3348 | SDValue Store; |
| 3349 | |
| 3350 | if (ObjSize==1 || ObjSize==2 || ObjSize==4) { |
| 3351 | EVT ObjType = (ObjSize == 1 ? MVT::i8 : |
| 3352 | (ObjSize == 2 ? MVT::i16 : MVT::i32)); |
| Ulrich Weigand | 2419597 | 2014-07-20 22:36:52 +0000 | [diff] [blame] | 3353 | Store = DAG.getTruncStore(Val.getValue(1), dl, Val, Arg, |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 3354 | MachinePointerInfo(&*FuncArg), ObjType); |
| Bill Schmidt | 6ed3b99 | 2012-10-25 13:38:09 +0000 | [diff] [blame] | 3355 | } else { |
| 3356 | // For sizes that don't fit a truncating store (3, 5, 6, 7), |
| 3357 | // store the whole register as-is to the parameter save area |
| Ulrich Weigand | 2419597 | 2014-07-20 22:36:52 +0000 | [diff] [blame] | 3358 | // slot. |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 3359 | Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, |
| 3360 | MachinePointerInfo(&*FuncArg)); |
| Bill Schmidt | 6ed3b99 | 2012-10-25 13:38:09 +0000 | [diff] [blame] | 3361 | } |
| 3362 | |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3363 | MemOps.push_back(Store); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3364 | } |
| Bill Schmidt | 6ed3b99 | 2012-10-25 13:38:09 +0000 | [diff] [blame] | 3365 | // Whether we copied from a register or not, advance the offset |
| 3366 | // into the parameter save area by a full doubleword. |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3367 | ArgOffset += PtrByteSize; |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3368 | continue; |
| 3369 | } |
| Bill Schmidt | 6ed3b99 | 2012-10-25 13:38:09 +0000 | [diff] [blame] | 3370 | |
| Ulrich Weigand | 2419597 | 2014-07-20 22:36:52 +0000 | [diff] [blame] | 3371 | // The value of the object is its address, which is the address of |
| 3372 | // its first stack doubleword. |
| 3373 | InVals.push_back(FIN); |
| 3374 | |
| 3375 | // Store whatever pieces of the object are in registers to memory. |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3376 | for (unsigned j = 0; j < ArgSize; j += PtrByteSize) { |
| Ulrich Weigand | 2419597 | 2014-07-20 22:36:52 +0000 | [diff] [blame] | 3377 | if (GPR_idx == Num_GPR_Regs) |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3378 | break; |
| Ulrich Weigand | 2419597 | 2014-07-20 22:36:52 +0000 | [diff] [blame] | 3379 | |
| 3380 | unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); |
| 3381 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); |
| 3382 | SDValue Addr = FIN; |
| 3383 | if (j) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3384 | SDValue Off = DAG.getConstant(j, dl, PtrVT); |
| Ulrich Weigand | 2419597 | 2014-07-20 22:36:52 +0000 | [diff] [blame] | 3385 | Addr = DAG.getNode(ISD::ADD, dl, Off.getValueType(), Addr, Off); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3386 | } |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 3387 | SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, Addr, |
| 3388 | MachinePointerInfo(&*FuncArg, j)); |
| Ulrich Weigand | 2419597 | 2014-07-20 22:36:52 +0000 | [diff] [blame] | 3389 | MemOps.push_back(Store); |
| 3390 | ++GPR_idx; |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3391 | } |
| Ulrich Weigand | 2419597 | 2014-07-20 22:36:52 +0000 | [diff] [blame] | 3392 | ArgOffset += ArgSize; |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3393 | continue; |
| 3394 | } |
| 3395 | |
| 3396 | switch (ObjectVT.getSimpleVT().SimpleTy) { |
| 3397 | default: llvm_unreachable("Unhandled argument type!"); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 3398 | case MVT::i1: |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3399 | case MVT::i32: |
| 3400 | case MVT::i64: |
| Hal Finkel | 965cea5 | 2015-07-12 00:37:44 +0000 | [diff] [blame] | 3401 | if (Flags.isNest()) { |
| 3402 | // The 'nest' parameter, if any, is passed in R11. |
| 3403 | unsigned VReg = MF.addLiveIn(PPC::X11, &PPC::G8RCRegClass); |
| 3404 | ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64); |
| 3405 | |
| 3406 | if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1) |
| 3407 | ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl); |
| 3408 | |
| 3409 | break; |
| 3410 | } |
| 3411 | |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 3412 | // These can be scalar arguments or elements of an integer array type |
| 3413 | // passed directly. Clang may use those instead of "byval" aggregate |
| 3414 | // types to avoid forcing arguments to memory unnecessarily. |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3415 | if (GPR_idx != Num_GPR_Regs) { |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 3416 | unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3417 | ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64); |
| 3418 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 3419 | if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1) |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3420 | // PPC64 passes i8, i16, and i32 values in i64 registers. Promote |
| 3421 | // value to MVT::i64 and then truncate to the correct register size. |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3422 | ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3423 | } else { |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 3424 | if (CallConv == CallingConv::Fast) |
| 3425 | ComputeArgOffset(); |
| 3426 | |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3427 | needsLoad = true; |
| 3428 | ArgSize = PtrByteSize; |
| 3429 | } |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 3430 | if (CallConv != CallingConv::Fast || needsLoad) |
| 3431 | ArgOffset += 8; |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3432 | break; |
| 3433 | |
| 3434 | case MVT::f32: |
| 3435 | case MVT::f64: |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 3436 | // These can be scalar arguments or elements of a float array type |
| 3437 | // passed directly. The latter are used to implement ELFv2 homogenous |
| 3438 | // float aggregates. |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3439 | if (FPR_idx != Num_FPR_Regs) { |
| 3440 | unsigned VReg; |
| 3441 | |
| 3442 | if (ObjectVT == MVT::f32) |
| Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 3443 | VReg = MF.addLiveIn(FPR[FPR_idx], |
| 3444 | Subtarget.hasP8Vector() |
| 3445 | ? &PPC::VSSRCRegClass |
| 3446 | : &PPC::F4RCRegClass); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3447 | else |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 3448 | VReg = MF.addLiveIn(FPR[FPR_idx], Subtarget.hasVSX() |
| 3449 | ? &PPC::VSFRCRegClass |
| 3450 | : &PPC::F8RCRegClass); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3451 | |
| 3452 | ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); |
| 3453 | ++FPR_idx; |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 3454 | } else if (GPR_idx != Num_GPR_Regs && CallConv != CallingConv::Fast) { |
| Hal Finkel | 8ea446b | 2015-01-18 14:31:10 +0000 | [diff] [blame] | 3455 | // FIXME: We may want to re-enable this for CallingConv::Fast on the P8 |
| 3456 | // once we support fp <-> gpr moves. |
| 3457 | |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 3458 | // This can only ever happen in the presence of f32 array types, |
| 3459 | // since otherwise we never run out of FPRs before running out |
| 3460 | // of GPRs. |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 3461 | unsigned VReg = MF.addLiveIn(GPR[GPR_idx++], &PPC::G8RCRegClass); |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 3462 | ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64); |
| 3463 | |
| 3464 | if (ObjectVT == MVT::f32) { |
| 3465 | if ((ArgOffset % PtrByteSize) == (isLittleEndian ? 4 : 0)) |
| 3466 | ArgVal = DAG.getNode(ISD::SRL, dl, MVT::i64, ArgVal, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3467 | DAG.getConstant(32, dl, MVT::i32)); |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 3468 | ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal); |
| 3469 | } |
| 3470 | |
| 3471 | ArgVal = DAG.getNode(ISD::BITCAST, dl, ObjectVT, ArgVal); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3472 | } else { |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 3473 | if (CallConv == CallingConv::Fast) |
| 3474 | ComputeArgOffset(); |
| 3475 | |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3476 | needsLoad = true; |
| 3477 | } |
| 3478 | |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 3479 | // When passing an array of floats, the array occupies consecutive |
| 3480 | // space in the argument area; only round up to the next doubleword |
| 3481 | // at the end of the array. Otherwise, each float takes 8 bytes. |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 3482 | if (CallConv != CallingConv::Fast || needsLoad) { |
| 3483 | ArgSize = Flags.isInConsecutiveRegs() ? ObjSize : PtrByteSize; |
| 3484 | ArgOffset += ArgSize; |
| 3485 | if (Flags.isInConsecutiveRegsLast()) |
| 3486 | ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; |
| 3487 | } |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3488 | break; |
| 3489 | case MVT::v4f32: |
| 3490 | case MVT::v4i32: |
| 3491 | case MVT::v8i16: |
| 3492 | case MVT::v16i8: |
| Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 3493 | case MVT::v2f64: |
| Hal Finkel | a6c8b51 | 2014-03-26 16:12:58 +0000 | [diff] [blame] | 3494 | case MVT::v2i64: |
| Kit Barton | d4eb73c | 2015-05-05 16:10:44 +0000 | [diff] [blame] | 3495 | case MVT::v1i128: |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 3496 | if (!Subtarget.hasQPX()) { |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 3497 | // These can be scalar arguments or elements of a vector array type |
| 3498 | // passed directly. The latter are used to implement ELFv2 homogenous |
| 3499 | // vector aggregates. |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3500 | if (VR_idx != Num_VR_Regs) { |
| Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 3501 | unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3502 | ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3503 | ++VR_idx; |
| 3504 | } else { |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 3505 | if (CallConv == CallingConv::Fast) |
| 3506 | ComputeArgOffset(); |
| 3507 | |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3508 | needsLoad = true; |
| 3509 | } |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 3510 | if (CallConv != CallingConv::Fast || needsLoad) |
| 3511 | ArgOffset += 16; |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3512 | break; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 3513 | } // not QPX |
| 3514 | |
| 3515 | assert(ObjectVT.getSimpleVT().SimpleTy == MVT::v4f32 && |
| 3516 | "Invalid QPX parameter type"); |
| 3517 | /* fall through */ |
| 3518 | |
| 3519 | case MVT::v4f64: |
| 3520 | case MVT::v4i1: |
| 3521 | // QPX vectors are treated like their scalar floating-point subregisters |
| 3522 | // (except that they're larger). |
| 3523 | unsigned Sz = ObjectVT.getSimpleVT().SimpleTy == MVT::v4f32 ? 16 : 32; |
| 3524 | if (QFPR_idx != Num_QFPR_Regs) { |
| 3525 | const TargetRegisterClass *RC; |
| 3526 | switch (ObjectVT.getSimpleVT().SimpleTy) { |
| 3527 | case MVT::v4f64: RC = &PPC::QFRCRegClass; break; |
| 3528 | case MVT::v4f32: RC = &PPC::QSRCRegClass; break; |
| 3529 | default: RC = &PPC::QBRCRegClass; break; |
| 3530 | } |
| 3531 | |
| 3532 | unsigned VReg = MF.addLiveIn(QFPR[QFPR_idx], RC); |
| 3533 | ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); |
| 3534 | ++QFPR_idx; |
| 3535 | } else { |
| 3536 | if (CallConv == CallingConv::Fast) |
| 3537 | ComputeArgOffset(); |
| 3538 | needsLoad = true; |
| 3539 | } |
| 3540 | if (CallConv != CallingConv::Fast || needsLoad) |
| 3541 | ArgOffset += Sz; |
| 3542 | break; |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3543 | } |
| 3544 | |
| 3545 | // We need to load the argument to a virtual register if we determined |
| 3546 | // above that we ran out of physical registers of the appropriate type. |
| 3547 | if (needsLoad) { |
| Ulrich Weigand | 59c6ab2 | 2014-06-20 16:34:05 +0000 | [diff] [blame] | 3548 | if (ObjSize < ArgSize && !isLittleEndian) |
| 3549 | CurArgOffset += ArgSize - ObjSize; |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 3550 | int FI = MFI.CreateFixedObject(ObjSize, CurArgOffset, isImmutable); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3551 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 3552 | ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo()); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | InVals.push_back(ArgVal); |
| 3556 | } |
| 3557 | |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 3558 | // Area that is at least reserved in the caller of this function. |
| Ulrich Weigand | ec2bf93 | 2014-07-07 19:26:41 +0000 | [diff] [blame] | 3559 | unsigned MinReservedArea; |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 3560 | if (HasParameterArea) |
| 3561 | MinReservedArea = std::max(ArgOffset, LinkageSize + 8 * PtrByteSize); |
| 3562 | else |
| 3563 | MinReservedArea = LinkageSize; |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 3564 | |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3565 | // Set the size that is at least reserved in caller of this function. Tail |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3566 | // call optimized functions' reserved stack space needs to be aligned so that |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3567 | // taking the difference between two stack areas will result in an aligned |
| 3568 | // stack. |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 3569 | MinReservedArea = |
| 3570 | EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea); |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 3571 | FuncInfo->setMinReservedArea(MinReservedArea); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3572 | |
| 3573 | // If the function takes variable number of arguments, make a frame index for |
| 3574 | // the start of the first vararg value... for expansion of llvm.va_start. |
| 3575 | if (isVarArg) { |
| 3576 | int Depth = ArgOffset; |
| 3577 | |
| 3578 | FuncInfo->setVarArgsFrameIndex( |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 3579 | MFI.CreateFixedObject(PtrByteSize, Depth, true)); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3580 | SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); |
| 3581 | |
| 3582 | // If this function is vararg, store any remaining integer argument regs |
| Nick Lewycky | 9980075 | 2016-06-28 01:45:05 +0000 | [diff] [blame] | 3583 | // to their spots on the stack so that they may be loaded by dereferencing |
| 3584 | // the result of va_next. |
| Ulrich Weigand | ec2bf93 | 2014-07-07 19:26:41 +0000 | [diff] [blame] | 3585 | for (GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize; |
| 3586 | GPR_idx < Num_GPR_Regs; ++GPR_idx) { |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3587 | unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); |
| 3588 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 3589 | SDValue Store = |
| 3590 | DAG.getStore(Val.getValue(1), dl, Val, FIN, MachinePointerInfo()); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3591 | MemOps.push_back(Store); |
| 3592 | // Increment the address by four for the next argument to store |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3593 | SDValue PtrOff = DAG.getConstant(PtrByteSize, dl, PtrVT); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3594 | FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); |
| 3595 | } |
| 3596 | } |
| 3597 | |
| 3598 | if (!MemOps.empty()) |
| Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 3599 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3600 | |
| 3601 | return Chain; |
| 3602 | } |
| 3603 | |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 3604 | SDValue PPCTargetLowering::LowerFormalArguments_Darwin( |
| 3605 | SDValue Chain, CallingConv::ID CallConv, bool isVarArg, |
| 3606 | const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, |
| 3607 | SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3608 | // TODO: add description of PPC stack frame format, or at least some docs. |
| 3609 | // |
| 3610 | MachineFunction &MF = DAG.getMachineFunction(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 3611 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| Dan Gohman | 31ae586 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 3612 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3613 | |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 3614 | EVT PtrVT = getPointerTy(MF.getDataLayout()); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3615 | bool isPPC64 = PtrVT == MVT::i64; |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3616 | // Potential tail calls could cause overwriting of argument stack slots. |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 3617 | bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt && |
| 3618 | (CallConv == CallingConv::Fast)); |
| Jim Laskey | f4e2e00 | 2006-11-28 14:53:52 +0000 | [diff] [blame] | 3619 | unsigned PtrByteSize = isPPC64 ? 8 : 4; |
| Eric Christopher | a4ae213 | 2015-02-13 22:22:57 +0000 | [diff] [blame] | 3620 | unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize(); |
| Ulrich Weigand | 8ca988f | 2014-06-23 14:15:53 +0000 | [diff] [blame] | 3621 | unsigned ArgOffset = LinkageSize; |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3622 | // Area that is at least reserved in caller of this function. |
| 3623 | unsigned MinReservedArea = ArgOffset; |
| 3624 | |
| Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 3625 | static const MCPhysReg GPR_32[] = { // 32-bit registers. |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3626 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 3627 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 3628 | }; |
| Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 3629 | static const MCPhysReg GPR_64[] = { // 64-bit registers. |
| Chris Lattner | ec78cad | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 3630 | PPC::X3, PPC::X4, PPC::X5, PPC::X6, |
| 3631 | PPC::X7, PPC::X8, PPC::X9, PPC::X10, |
| 3632 | }; |
| Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 3633 | static const MCPhysReg VR[] = { |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3634 | PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, |
| 3635 | PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 |
| 3636 | }; |
| Chris Lattner | ec78cad | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 3637 | |
| Owen Anderson | e2f23a3 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 3638 | const unsigned Num_GPR_Regs = array_lengthof(GPR_32); |
| Hal Finkel | a932105 | 2016-10-02 02:10:20 +0000 | [diff] [blame] | 3639 | const unsigned Num_FPR_Regs = useSoftFloat() ? 0 : 13; |
| Owen Anderson | e2f23a3 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 3640 | const unsigned Num_VR_Regs = array_lengthof( VR); |
| Jim Laskey | 48850c1 | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 3641 | |
| 3642 | unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3643 | |
| Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 3644 | const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3645 | |
| Dale Johannesen | 0dfd3f3 | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 3646 | // In 32-bit non-varargs functions, the stack space for vectors is after the |
| 3647 | // stack space for non-vectors. We do not use this space unless we have |
| 3648 | // too many vectors to fit in registers, something that only occurs in |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3649 | // constructed examples:), but we have to walk the arglist to figure |
| Dale Johannesen | 0dfd3f3 | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 3650 | // that out...for the pathological case, compute VecArgOffset as the |
| 3651 | // start of the vector parameter area. Computing VecArgOffset is the |
| 3652 | // entire point of the following loop. |
| Dale Johannesen | 0dfd3f3 | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 3653 | unsigned VecArgOffset = ArgOffset; |
| 3654 | if (!isVarArg && !isPPC64) { |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3655 | for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; |
| Dale Johannesen | 0dfd3f3 | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 3656 | ++ArgNo) { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3657 | EVT ObjectVT = Ins[ArgNo].VT; |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3658 | ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags; |
| Dale Johannesen | 0dfd3f3 | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 3659 | |
| Duncan Sands | d97eea3 | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 3660 | if (Flags.isByVal()) { |
| Dale Johannesen | 0dfd3f3 | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 3661 | // ObjSize is the true size, ArgSize rounded up to multiple of regs. |
| Benjamin Kramer | 084b9f4 | 2012-01-20 14:42:32 +0000 | [diff] [blame] | 3662 | unsigned ObjSize = Flags.getByValSize(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3663 | unsigned ArgSize = |
| Dale Johannesen | 0dfd3f3 | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 3664 | ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; |
| 3665 | VecArgOffset += ArgSize; |
| 3666 | continue; |
| 3667 | } |
| 3668 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3669 | switch(ObjectVT.getSimpleVT().SimpleTy) { |
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3670 | default: llvm_unreachable("Unhandled argument type!"); |
| Hal Finkel | 5cae216 | 2014-02-28 01:17:25 +0000 | [diff] [blame] | 3671 | case MVT::i1: |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3672 | case MVT::i32: |
| 3673 | case MVT::f32: |
| Bill Schmidt | 019cc6f | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 3674 | VecArgOffset += 4; |
| Dale Johannesen | 0dfd3f3 | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 3675 | break; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3676 | case MVT::i64: // PPC64 |
| 3677 | case MVT::f64: |
| Bill Schmidt | 019cc6f | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 3678 | // FIXME: We are guaranteed to be !isPPC64 at this point. |
| 3679 | // Does MVT::i64 apply? |
| Dale Johannesen | 0dfd3f3 | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 3680 | VecArgOffset += 8; |
| 3681 | break; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3682 | case MVT::v4f32: |
| 3683 | case MVT::v4i32: |
| 3684 | case MVT::v8i16: |
| 3685 | case MVT::v16i8: |
| Dale Johannesen | 0dfd3f3 | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 3686 | // Nothing to do, we're only looking at Nonvector args here. |
| 3687 | break; |
| 3688 | } |
| 3689 | } |
| 3690 | } |
| 3691 | // We've found where the vector parameter area in memory is. Skip the |
| 3692 | // first 12 parameters; these don't use that memory. |
| 3693 | VecArgOffset = ((VecArgOffset+15)/16)*16; |
| 3694 | VecArgOffset += 12*16; |
| 3695 | |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3696 | // Add DAG nodes to load the arguments or copy them out of registers. On |
| Jim Laskey | 48850c1 | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 3697 | // entry to a function on PPC, the arguments start after the linkage area, |
| 3698 | // although the first ones are often in registers. |
| Nicolas Geoffray | 7aad928 | 2007-03-13 15:02:46 +0000 | [diff] [blame] | 3699 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3700 | SmallVector<SDValue, 8> MemOps; |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3701 | unsigned nAltivecParamsAtEnd = 0; |
| Roman Divacky | ca10389 | 2012-09-24 20:47:19 +0000 | [diff] [blame] | 3702 | Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin(); |
| Bill Schmidt | 38b6cb5 | 2013-05-08 17:22:33 +0000 | [diff] [blame] | 3703 | unsigned CurArgIdx = 0; |
| 3704 | for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3705 | SDValue ArgVal; |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3706 | bool needsLoad = false; |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3707 | EVT ObjectVT = Ins[ArgNo].VT; |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3708 | unsigned ObjSize = ObjectVT.getSizeInBits()/8; |
| Jim Laskey | 152671f | 2006-11-29 13:37:09 +0000 | [diff] [blame] | 3709 | unsigned ArgSize = ObjSize; |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3710 | ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags; |
| Andrew Trick | 05938a5 | 2015-02-16 18:10:47 +0000 | [diff] [blame] | 3711 | if (Ins[ArgNo].isOrigArg()) { |
| 3712 | std::advance(FuncArg, Ins[ArgNo].getOrigArgIndex() - CurArgIdx); |
| 3713 | CurArgIdx = Ins[ArgNo].getOrigArgIndex(); |
| 3714 | } |
| Chris Lattner | 318f0d2 | 2006-05-16 18:51:52 +0000 | [diff] [blame] | 3715 | unsigned CurArgOffset = ArgOffset; |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 3716 | |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3717 | // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3718 | if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 || |
| 3719 | ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) { |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3720 | if (isVarArg || isPPC64) { |
| 3721 | MinReservedArea = ((MinReservedArea+15)/16)*16; |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3722 | MinReservedArea += CalculateStackSlotSize(ObjectVT, |
| Dan Gohman | d3fe174 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 3723 | Flags, |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3724 | PtrByteSize); |
| 3725 | } else nAltivecParamsAtEnd++; |
| 3726 | } else |
| 3727 | // Calculate min reserved area. |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3728 | MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT, |
| Dan Gohman | d3fe174 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 3729 | Flags, |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3730 | PtrByteSize); |
| 3731 | |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 3732 | // FIXME the codegen can be much improved in some cases. |
| 3733 | // We do not have to keep everything in memory. |
| Duncan Sands | d97eea3 | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 3734 | if (Flags.isByVal()) { |
| Andrew Trick | 05938a5 | 2015-02-16 18:10:47 +0000 | [diff] [blame] | 3735 | assert(Ins[ArgNo].isOrigArg() && "Byval arguments cannot be implicit"); |
| 3736 | |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 3737 | // ObjSize is the true size, ArgSize rounded up to multiple of registers. |
| Duncan Sands | d97eea3 | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 3738 | ObjSize = Flags.getByValSize(); |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 3739 | ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3740 | // Objects of size 1 and 2 are right justified, everything else is |
| 3741 | // left justified. This means the memory address is adjusted forwards. |
| Dale Johannesen | 21a8f14 | 2008-03-08 01:41:42 +0000 | [diff] [blame] | 3742 | if (ObjSize==1 || ObjSize==2) { |
| 3743 | CurArgOffset = CurArgOffset + (4 - ObjSize); |
| 3744 | } |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 3745 | // The value of the object is its address. |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 3746 | int FI = MFI.CreateFixedObject(ObjSize, CurArgOffset, false, true); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3747 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3748 | InVals.push_back(FIN); |
| Bill Schmidt | d1fa36f | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 3749 | if (ObjSize==1 || ObjSize==2) { |
| Dale Johannesen | 21a8f14 | 2008-03-08 01:41:42 +0000 | [diff] [blame] | 3750 | if (GPR_idx != Num_GPR_Regs) { |
| Roman Divacky | d041962 | 2011-06-17 15:21:10 +0000 | [diff] [blame] | 3751 | unsigned VReg; |
| 3752 | if (isPPC64) |
| 3753 | VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); |
| 3754 | else |
| 3755 | VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3756 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3757 | EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16; |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 3758 | SDValue Store = |
| 3759 | DAG.getTruncStore(Val.getValue(1), dl, Val, FIN, |
| 3760 | MachinePointerInfo(&*FuncArg), ObjType); |
| Dale Johannesen | 21a8f14 | 2008-03-08 01:41:42 +0000 | [diff] [blame] | 3761 | MemOps.push_back(Store); |
| 3762 | ++GPR_idx; |
| Dale Johannesen | 21a8f14 | 2008-03-08 01:41:42 +0000 | [diff] [blame] | 3763 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3764 | |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3765 | ArgOffset += PtrByteSize; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3766 | |
| Dale Johannesen | 21a8f14 | 2008-03-08 01:41:42 +0000 | [diff] [blame] | 3767 | continue; |
| 3768 | } |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 3769 | for (unsigned j = 0; j < ArgSize; j += PtrByteSize) { |
| 3770 | // Store whatever pieces of the object are in registers |
| Bill Schmidt | 019cc6f | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 3771 | // to memory. ArgOffset will be the address of the beginning |
| 3772 | // of the object. |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 3773 | if (GPR_idx != Num_GPR_Regs) { |
| Roman Divacky | d041962 | 2011-06-17 15:21:10 +0000 | [diff] [blame] | 3774 | unsigned VReg; |
| 3775 | if (isPPC64) |
| 3776 | VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); |
| 3777 | else |
| 3778 | VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 3779 | int FI = MFI.CreateFixedObject(PtrByteSize, ArgOffset, true); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3780 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3781 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 3782 | SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, |
| 3783 | MachinePointerInfo(&*FuncArg, j)); |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 3784 | MemOps.push_back(Store); |
| 3785 | ++GPR_idx; |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3786 | ArgOffset += PtrByteSize; |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 3787 | } else { |
| 3788 | ArgOffset += ArgSize - (ArgOffset-CurArgOffset); |
| 3789 | break; |
| 3790 | } |
| 3791 | } |
| 3792 | continue; |
| 3793 | } |
| 3794 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3795 | switch (ObjectVT.getSimpleVT().SimpleTy) { |
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3796 | default: llvm_unreachable("Unhandled argument type!"); |
| Hal Finkel | 5cae216 | 2014-02-28 01:17:25 +0000 | [diff] [blame] | 3797 | case MVT::i1: |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3798 | case MVT::i32: |
| Bill Wendling | 968f32c | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 3799 | if (!isPPC64) { |
| Bill Wendling | 968f32c | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 3800 | if (GPR_idx != Num_GPR_Regs) { |
| Devang Patel | f3292b2 | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 3801 | unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3802 | ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32); |
| Hal Finkel | 7f908e8 | 2014-03-06 00:45:19 +0000 | [diff] [blame] | 3803 | |
| 3804 | if (ObjectVT == MVT::i1) |
| 3805 | ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgVal); |
| 3806 | |
| Bill Wendling | 968f32c | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 3807 | ++GPR_idx; |
| 3808 | } else { |
| 3809 | needsLoad = true; |
| 3810 | ArgSize = PtrByteSize; |
| 3811 | } |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3812 | // All int arguments reserve stack space in the Darwin ABI. |
| 3813 | ArgOffset += PtrByteSize; |
| Bill Wendling | 968f32c | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 3814 | break; |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3815 | } |
| Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 3816 | LLVM_FALLTHROUGH; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3817 | case MVT::i64: // PPC64 |
| Chris Lattner | ec78cad | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 3818 | if (GPR_idx != Num_GPR_Regs) { |
| Devang Patel | f3292b2 | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 3819 | unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3820 | ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64); |
| Bill Wendling | 968f32c | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 3821 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 3822 | if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1) |
| Bill Wendling | 968f32c | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 3823 | // PPC64 passes i8, i16, and i32 values in i64 registers. Promote |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3824 | // value to MVT::i64 and then truncate to the correct register size. |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3825 | ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl); |
| Bill Wendling | 968f32c | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 3826 | |
| Chris Lattner | ec78cad | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 3827 | ++GPR_idx; |
| 3828 | } else { |
| 3829 | needsLoad = true; |
| Evan Cheng | 0f0aee2 | 2008-07-24 08:17:07 +0000 | [diff] [blame] | 3830 | ArgSize = PtrByteSize; |
| Chris Lattner | ec78cad | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 3831 | } |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3832 | // All int arguments reserve stack space in the Darwin ABI. |
| 3833 | ArgOffset += 8; |
| Chris Lattner | ec78cad | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 3834 | break; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3835 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3836 | case MVT::f32: |
| 3837 | case MVT::f64: |
| Chris Lattner | 318f0d2 | 2006-05-16 18:51:52 +0000 | [diff] [blame] | 3838 | // Every 4 bytes of argument space consumes one of the GPRs available for |
| 3839 | // argument passing. |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3840 | if (GPR_idx != Num_GPR_Regs) { |
| Chris Lattner | 26e2fcd | 2006-05-16 18:58:15 +0000 | [diff] [blame] | 3841 | ++GPR_idx; |
| Chris Lattner | 2cca385 | 2006-11-18 01:57:19 +0000 | [diff] [blame] | 3842 | if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64) |
| Chris Lattner | 26e2fcd | 2006-05-16 18:58:15 +0000 | [diff] [blame] | 3843 | ++GPR_idx; |
| Chris Lattner | 318f0d2 | 2006-05-16 18:51:52 +0000 | [diff] [blame] | 3844 | } |
| Chris Lattner | 26e2fcd | 2006-05-16 18:58:15 +0000 | [diff] [blame] | 3845 | if (FPR_idx != Num_FPR_Regs) { |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3846 | unsigned VReg; |
| Tilmann Scheller | 98bdaaa | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 3847 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3848 | if (ObjectVT == MVT::f32) |
| Devang Patel | f3292b2 | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 3849 | VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass); |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3850 | else |
| Devang Patel | f3292b2 | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 3851 | VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass); |
| Tilmann Scheller | 98bdaaa | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 3852 | |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3853 | ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3854 | ++FPR_idx; |
| 3855 | } else { |
| 3856 | needsLoad = true; |
| 3857 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3858 | |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3859 | // All FP arguments reserve stack space in the Darwin ABI. |
| 3860 | ArgOffset += isPPC64 ? 8 : ObjSize; |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3861 | break; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3862 | case MVT::v4f32: |
| 3863 | case MVT::v4i32: |
| 3864 | case MVT::v8i16: |
| 3865 | case MVT::v16i8: |
| Dale Johannesen | b28456e | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 3866 | // Note that vector arguments in registers don't reserve stack space, |
| 3867 | // except in varargs functions. |
| Chris Lattner | 26e2fcd | 2006-05-16 18:58:15 +0000 | [diff] [blame] | 3868 | if (VR_idx != Num_VR_Regs) { |
| Devang Patel | f3292b2 | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 3869 | unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass); |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3870 | ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); |
| Dale Johannesen | b28456e | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 3871 | if (isVarArg) { |
| 3872 | while ((ArgOffset % 16) != 0) { |
| 3873 | ArgOffset += PtrByteSize; |
| 3874 | if (GPR_idx != Num_GPR_Regs) |
| 3875 | GPR_idx++; |
| 3876 | } |
| 3877 | ArgOffset += 16; |
| Tilmann Scheller | d1aaa32 | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 3878 | GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64? |
| Dale Johannesen | b28456e | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 3879 | } |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3880 | ++VR_idx; |
| 3881 | } else { |
| Dale Johannesen | 0dfd3f3 | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 3882 | if (!isVarArg && !isPPC64) { |
| 3883 | // Vectors go after all the nonvectors. |
| 3884 | CurArgOffset = VecArgOffset; |
| 3885 | VecArgOffset += 16; |
| 3886 | } else { |
| 3887 | // Vectors are aligned. |
| 3888 | ArgOffset = ((ArgOffset+15)/16)*16; |
| 3889 | CurArgOffset = ArgOffset; |
| 3890 | ArgOffset += 16; |
| Dale Johannesen | 0d98256 | 2008-03-12 00:49:20 +0000 | [diff] [blame] | 3891 | } |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3892 | needsLoad = true; |
| 3893 | } |
| 3894 | break; |
| 3895 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3896 | |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3897 | // We need to load the argument to a virtual register if we determined above |
| Chris Lattner | f6518cf | 2008-02-13 07:35:30 +0000 | [diff] [blame] | 3898 | // that we ran out of physical registers of the appropriate type. |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3899 | if (needsLoad) { |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 3900 | int FI = MFI.CreateFixedObject(ObjSize, |
| 3901 | CurArgOffset + (ArgSize - ObjSize), |
| 3902 | isImmutable); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3903 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 3904 | ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo()); |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3905 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3906 | |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3907 | InVals.push_back(ArgVal); |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3908 | } |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 3909 | |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 3910 | // Allow for Altivec parameters at the end, if needed. |
| 3911 | if (nAltivecParamsAtEnd) { |
| 3912 | MinReservedArea = ((MinReservedArea+15)/16)*16; |
| 3913 | MinReservedArea += 16*nAltivecParamsAtEnd; |
| 3914 | } |
| 3915 | |
| 3916 | // Area that is at least reserved in the caller of this function. |
| Ulrich Weigand | 8ca988f | 2014-06-23 14:15:53 +0000 | [diff] [blame] | 3917 | MinReservedArea = std::max(MinReservedArea, LinkageSize + 8 * PtrByteSize); |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 3918 | |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3919 | // Set the size that is at least reserved in caller of this function. Tail |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3920 | // call optimized functions' reserved stack space needs to be aligned so that |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3921 | // taking the difference between two stack areas will result in an aligned |
| 3922 | // stack. |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 3923 | MinReservedArea = |
| 3924 | EnsureStackAlignment(Subtarget.getFrameLowering(), MinReservedArea); |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 3925 | FuncInfo->setMinReservedArea(MinReservedArea); |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3926 | |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3927 | // If the function takes variable number of arguments, make a frame index for |
| 3928 | // the start of the first vararg value... for expansion of llvm.va_start. |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3929 | if (isVarArg) { |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3930 | int Depth = ArgOffset; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3931 | |
| Dan Gohman | 31ae586 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 3932 | FuncInfo->setVarArgsFrameIndex( |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 3933 | MFI.CreateFixedObject(PtrVT.getSizeInBits()/8, |
| 3934 | Depth, true)); |
| Dan Gohman | 31ae586 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 3935 | SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3936 | |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3937 | // If this function is vararg, store any remaining integer argument regs |
| Nick Lewycky | 9980075 | 2016-06-28 01:45:05 +0000 | [diff] [blame] | 3938 | // to their spots on the stack so that they may be loaded by dereferencing |
| 3939 | // the result of va_next. |
| Chris Lattner | 26e2fcd | 2006-05-16 18:58:15 +0000 | [diff] [blame] | 3940 | for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) { |
| Chris Lattner | 2cca385 | 2006-11-18 01:57:19 +0000 | [diff] [blame] | 3941 | unsigned VReg; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3942 | |
| Chris Lattner | 2cca385 | 2006-11-18 01:57:19 +0000 | [diff] [blame] | 3943 | if (isPPC64) |
| Devang Patel | f3292b2 | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 3944 | VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); |
| Chris Lattner | 2cca385 | 2006-11-18 01:57:19 +0000 | [diff] [blame] | 3945 | else |
| Devang Patel | f3292b2 | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 3946 | VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); |
| Chris Lattner | 2cca385 | 2006-11-18 01:57:19 +0000 | [diff] [blame] | 3947 | |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3948 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 3949 | SDValue Store = |
| 3950 | DAG.getStore(Val.getValue(1), dl, Val, FIN, MachinePointerInfo()); |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3951 | MemOps.push_back(Store); |
| 3952 | // Increment the address by four for the next argument to store |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3953 | SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, dl, PtrVT); |
| Dale Johannesen | 679073b | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 3954 | FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3955 | } |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3956 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3957 | |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 3958 | if (!MemOps.empty()) |
| Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 3959 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps); |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 3960 | |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3961 | return Chain; |
| Chris Lattner | 4302e8f | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 3962 | } |
| 3963 | |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3964 | /// CalculateTailCallSPDiff - Get the amount the stack pointer has to be |
| Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 3965 | /// adjusted to accommodate the arguments for the tailcall. |
| Dale Johannesen | 86dcae1 | 2009-11-24 01:09:07 +0000 | [diff] [blame] | 3966 | static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall, |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3967 | unsigned ParamSize) { |
| 3968 | |
| Dale Johannesen | 86dcae1 | 2009-11-24 01:09:07 +0000 | [diff] [blame] | 3969 | if (!isTailCall) return 0; |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3970 | |
| 3971 | PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>(); |
| 3972 | unsigned CallerMinReservedArea = FI->getMinReservedArea(); |
| 3973 | int SPDiff = (int)CallerMinReservedArea - (int)ParamSize; |
| 3974 | // Remember only if the new adjustement is bigger. |
| 3975 | if (SPDiff < FI->getTailCallSPDelta()) |
| 3976 | FI->setTailCallSPDelta(SPDiff); |
| 3977 | |
| 3978 | return SPDiff; |
| 3979 | } |
| 3980 | |
| Chuang-Yu Cheng | 2e5973e | 2016-04-06 02:04:38 +0000 | [diff] [blame] | 3981 | static bool isFunctionGlobalAddress(SDValue Callee); |
| 3982 | |
| 3983 | static bool |
| 3984 | resideInSameModule(SDValue Callee, Reloc::Model RelMod) { |
| 3985 | // If !G, Callee can be an external symbol. |
| 3986 | GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee); |
| 3987 | if (!G) return false; |
| 3988 | |
| 3989 | const GlobalValue *GV = G->getGlobal(); |
| 3990 | |
| 3991 | if (GV->isDeclaration()) return false; |
| 3992 | |
| 3993 | switch(GV->getLinkage()) { |
| 3994 | default: llvm_unreachable("unknow linkage type"); |
| 3995 | case GlobalValue::AvailableExternallyLinkage: |
| 3996 | case GlobalValue::ExternalWeakLinkage: |
| 3997 | return false; |
| 3998 | |
| 3999 | // Callee with weak linkage is allowed if it has hidden or protected |
| 4000 | // visibility |
| 4001 | case GlobalValue::LinkOnceAnyLinkage: |
| 4002 | case GlobalValue::LinkOnceODRLinkage: // e.g. c++ inline functions |
| 4003 | case GlobalValue::WeakAnyLinkage: |
| 4004 | case GlobalValue::WeakODRLinkage: // e.g. c++ template instantiation |
| 4005 | if (GV->hasDefaultVisibility()) |
| 4006 | return false; |
| 4007 | |
| 4008 | case GlobalValue::ExternalLinkage: |
| 4009 | case GlobalValue::InternalLinkage: |
| 4010 | case GlobalValue::PrivateLinkage: |
| 4011 | break; |
| 4012 | } |
| 4013 | |
| 4014 | // With '-fPIC', calling default visiblity function need insert 'nop' after |
| 4015 | // function call, no matter that function resides in same module or not, so |
| 4016 | // we treat it as in different module. |
| 4017 | if (RelMod == Reloc::PIC_ && GV->hasDefaultVisibility()) |
| 4018 | return false; |
| 4019 | |
| 4020 | return true; |
| 4021 | } |
| 4022 | |
| 4023 | static bool |
| 4024 | needStackSlotPassParameters(const PPCSubtarget &Subtarget, |
| 4025 | const SmallVectorImpl<ISD::OutputArg> &Outs) { |
| 4026 | assert(Subtarget.isSVR4ABI() && Subtarget.isPPC64()); |
| 4027 | |
| 4028 | const unsigned PtrByteSize = 8; |
| 4029 | const unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize(); |
| 4030 | |
| 4031 | static const MCPhysReg GPR[] = { |
| 4032 | PPC::X3, PPC::X4, PPC::X5, PPC::X6, |
| 4033 | PPC::X7, PPC::X8, PPC::X9, PPC::X10, |
| 4034 | }; |
| 4035 | static const MCPhysReg VR[] = { |
| 4036 | PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, |
| 4037 | PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 |
| 4038 | }; |
| 4039 | |
| 4040 | const unsigned NumGPRs = array_lengthof(GPR); |
| 4041 | const unsigned NumFPRs = 13; |
| 4042 | const unsigned NumVRs = array_lengthof(VR); |
| 4043 | const unsigned ParamAreaSize = NumGPRs * PtrByteSize; |
| 4044 | |
| 4045 | unsigned NumBytes = LinkageSize; |
| 4046 | unsigned AvailableFPRs = NumFPRs; |
| 4047 | unsigned AvailableVRs = NumVRs; |
| 4048 | |
| 4049 | for (const ISD::OutputArg& Param : Outs) { |
| 4050 | if (Param.Flags.isNest()) continue; |
| 4051 | |
| 4052 | if (CalculateStackSlotUsed(Param.VT, Param.ArgVT, Param.Flags, |
| 4053 | PtrByteSize, LinkageSize, ParamAreaSize, |
| 4054 | NumBytes, AvailableFPRs, AvailableVRs, |
| 4055 | Subtarget.hasQPX())) |
| 4056 | return true; |
| 4057 | } |
| 4058 | return false; |
| 4059 | } |
| 4060 | |
| 4061 | static bool |
| 4062 | hasSameArgumentList(const Function *CallerFn, ImmutableCallSite *CS) { |
| 4063 | if (CS->arg_size() != CallerFn->getArgumentList().size()) |
| 4064 | return false; |
| 4065 | |
| 4066 | ImmutableCallSite::arg_iterator CalleeArgIter = CS->arg_begin(); |
| 4067 | ImmutableCallSite::arg_iterator CalleeArgEnd = CS->arg_end(); |
| 4068 | Function::const_arg_iterator CallerArgIter = CallerFn->arg_begin(); |
| 4069 | |
| 4070 | for (; CalleeArgIter != CalleeArgEnd; ++CalleeArgIter, ++CallerArgIter) { |
| 4071 | const Value* CalleeArg = *CalleeArgIter; |
| 4072 | const Value* CallerArg = &(*CallerArgIter); |
| 4073 | if (CalleeArg == CallerArg) |
| 4074 | continue; |
| 4075 | |
| 4076 | // e.g. @caller([4 x i64] %a, [4 x i64] %b) { |
| 4077 | // tail call @callee([4 x i64] undef, [4 x i64] %b) |
| 4078 | // } |
| 4079 | // 1st argument of callee is undef and has the same type as caller. |
| 4080 | if (CalleeArg->getType() == CallerArg->getType() && |
| 4081 | isa<UndefValue>(CalleeArg)) |
| 4082 | continue; |
| 4083 | |
| 4084 | return false; |
| 4085 | } |
| 4086 | |
| 4087 | return true; |
| 4088 | } |
| 4089 | |
| 4090 | bool |
| 4091 | PPCTargetLowering::IsEligibleForTailCallOptimization_64SVR4( |
| 4092 | SDValue Callee, |
| 4093 | CallingConv::ID CalleeCC, |
| 4094 | ImmutableCallSite *CS, |
| 4095 | bool isVarArg, |
| 4096 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 4097 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 4098 | SelectionDAG& DAG) const { |
| 4099 | bool TailCallOpt = getTargetMachine().Options.GuaranteedTailCallOpt; |
| 4100 | |
| 4101 | if (DisableSCO && !TailCallOpt) return false; |
| 4102 | |
| 4103 | // Variadic argument functions are not supported. |
| 4104 | if (isVarArg) return false; |
| 4105 | |
| 4106 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4107 | CallingConv::ID CallerCC = MF.getFunction()->getCallingConv(); |
| 4108 | |
| 4109 | // Tail or Sibling call optimization (TCO/SCO) needs callee and caller has |
| 4110 | // the same calling convention |
| 4111 | if (CallerCC != CalleeCC) return false; |
| 4112 | |
| 4113 | // SCO support C calling convention |
| 4114 | if (CalleeCC != CallingConv::Fast && CalleeCC != CallingConv::C) |
| 4115 | return false; |
| 4116 | |
| Chuang-Yu Cheng | f7ba716 | 2016-08-17 03:17:44 +0000 | [diff] [blame] | 4117 | // Caller contains any byval parameter is not supported. |
| David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 4118 | if (any_of(Ins, [](const ISD::InputArg &IA) { return IA.Flags.isByVal(); })) |
| Chuang-Yu Cheng | 2e5973e | 2016-04-06 02:04:38 +0000 | [diff] [blame] | 4119 | return false; |
| 4120 | |
| Chuang-Yu Cheng | f7ba716 | 2016-08-17 03:17:44 +0000 | [diff] [blame] | 4121 | // Callee contains any byval parameter is not supported, too. |
| 4122 | // Note: This is a quick work around, because in some cases, e.g. |
| 4123 | // caller's stack size > callee's stack size, we are still able to apply |
| 4124 | // sibling call optimization. See: https://reviews.llvm.org/D23441#513574 |
| 4125 | if (any_of(Outs, [](const ISD::OutputArg& OA) { return OA.Flags.isByVal(); })) |
| 4126 | return false; |
| 4127 | |
| Chuang-Yu Cheng | 2e5973e | 2016-04-06 02:04:38 +0000 | [diff] [blame] | 4128 | // No TCO/SCO on indirect call because Caller have to restore its TOC |
| 4129 | if (!isFunctionGlobalAddress(Callee) && |
| 4130 | !isa<ExternalSymbolSDNode>(Callee)) |
| 4131 | return false; |
| 4132 | |
| 4133 | // Check if Callee resides in the same module, because for now, PPC64 SVR4 ABI |
| 4134 | // (ELFv1/ELFv2) doesn't allow tail calls to a symbol resides in another |
| 4135 | // module. |
| 4136 | // ref: https://bugzilla.mozilla.org/show_bug.cgi?id=973977 |
| 4137 | if (!resideInSameModule(Callee, getTargetMachine().getRelocationModel())) |
| 4138 | return false; |
| 4139 | |
| 4140 | // TCO allows altering callee ABI, so we don't have to check further. |
| 4141 | if (CalleeCC == CallingConv::Fast && TailCallOpt) |
| 4142 | return true; |
| 4143 | |
| 4144 | if (DisableSCO) return false; |
| 4145 | |
| 4146 | // If callee use the same argument list that caller is using, then we can |
| 4147 | // apply SCO on this case. If it is not, then we need to check if callee needs |
| 4148 | // stack for passing arguments. |
| 4149 | if (!hasSameArgumentList(MF.getFunction(), CS) && |
| 4150 | needStackSlotPassParameters(Subtarget, Outs)) { |
| 4151 | return false; |
| 4152 | } |
| 4153 | |
| 4154 | return true; |
| 4155 | } |
| 4156 | |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4157 | /// IsEligibleForTailCallOptimization - Check whether the call is eligible |
| 4158 | /// for tail call optimization. Targets which want to do tail call |
| 4159 | /// optimization should implement this function. |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4160 | bool |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4161 | PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee, |
| Sandeep Patel | 68c5f47 | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 4162 | CallingConv::ID CalleeCC, |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4163 | bool isVarArg, |
| 4164 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4165 | SelectionDAG& DAG) const { |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 4166 | if (!getTargetMachine().Options.GuaranteedTailCallOpt) |
| Evan Cheng | 25217ff | 2010-01-29 23:05:56 +0000 | [diff] [blame] | 4167 | return false; |
| 4168 | |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4169 | // Variable argument functions are not supported. |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4170 | if (isVarArg) |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4171 | return false; |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4172 | |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4173 | MachineFunction &MF = DAG.getMachineFunction(); |
| Sandeep Patel | 68c5f47 | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 4174 | CallingConv::ID CallerCC = MF.getFunction()->getCallingConv(); |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4175 | if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) { |
| 4176 | // Functions containing by val parameters are not supported. |
| 4177 | for (unsigned i = 0; i != Ins.size(); i++) { |
| 4178 | ISD::ArgFlagsTy Flags = Ins[i].Flags; |
| 4179 | if (Flags.isByVal()) return false; |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4180 | } |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4181 | |
| Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 4182 | // Non-PIC/GOT tail calls are supported. |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4183 | if (getTargetMachine().getRelocationModel() != Reloc::PIC_) |
| 4184 | return true; |
| 4185 | |
| 4186 | // At the moment we can only do local tail calls (in same module, hidden |
| 4187 | // or protected) if we are generating PIC. |
| 4188 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
| 4189 | return G->getGlobal()->hasHiddenVisibility() |
| 4190 | || G->getGlobal()->hasProtectedVisibility(); |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | return false; |
| 4194 | } |
| 4195 | |
| Chris Lattner | eb755fc | 2006-05-17 19:00:46 +0000 | [diff] [blame] | 4196 | /// isCallCompatibleAddress - Return the immediate to use if the specified |
| 4197 | /// 32-bit value is representable in the immediate field of a BxA instruction. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4198 | static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) { |
| Chris Lattner | eb755fc | 2006-05-17 19:00:46 +0000 | [diff] [blame] | 4199 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 4200 | if (!C) return nullptr; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4201 | |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4202 | int Addr = C->getZExtValue(); |
| Chris Lattner | eb755fc | 2006-05-17 19:00:46 +0000 | [diff] [blame] | 4203 | if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero. |
| Richard Smith | 228e6d4 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 4204 | SignExtend32<26>(Addr) != Addr) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 4205 | return nullptr; // Top 6 bits have to be sext of immediate. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4206 | |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 4207 | return DAG |
| 4208 | .getConstant( |
| 4209 | (int)C->getZExtValue() >> 2, SDLoc(Op), |
| 4210 | DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout())) |
| 4211 | .getNode(); |
| Chris Lattner | eb755fc | 2006-05-17 19:00:46 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
| Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 4214 | namespace { |
| 4215 | |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4216 | struct TailCallArgumentInfo { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4217 | SDValue Arg; |
| 4218 | SDValue FrameIdxOp; |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4219 | int FrameIdx; |
| 4220 | |
| 4221 | TailCallArgumentInfo() : FrameIdx(0) {} |
| 4222 | }; |
| Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 4223 | } |
| Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 4224 | |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4225 | /// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 4226 | static void StoreTailCallArgumentsToStackSlot( |
| 4227 | SelectionDAG &DAG, SDValue Chain, |
| 4228 | const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs, |
| 4229 | SmallVectorImpl<SDValue> &MemOpChains, const SDLoc &dl) { |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4230 | for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4231 | SDValue Arg = TailCallArgs[i].Arg; |
| 4232 | SDValue FIN = TailCallArgs[i].FrameIdxOp; |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4233 | int FI = TailCallArgs[i].FrameIdx; |
| 4234 | // Store relative to framepointer. |
| Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 4235 | MemOpChains.push_back(DAG.getStore( |
| 4236 | Chain, dl, Arg, FIN, |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 4237 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI))); |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4238 | } |
| 4239 | } |
| 4240 | |
| 4241 | /// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to |
| 4242 | /// the appropriate stack slot for the tail call optimized function call. |
| Eric Christopher | c16ccbe | 2016-07-07 00:39:30 +0000 | [diff] [blame] | 4243 | static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG, SDValue Chain, |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 4244 | SDValue OldRetAddr, SDValue OldFP, |
| Eric Christopher | 327e440 | 2016-07-07 01:08:17 +0000 | [diff] [blame] | 4245 | int SPDiff, const SDLoc &dl) { |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4246 | if (SPDiff) { |
| 4247 | // Calculate the new stack slot for the return address. |
| Eric Christopher | c16ccbe | 2016-07-07 00:39:30 +0000 | [diff] [blame] | 4248 | MachineFunction &MF = DAG.getMachineFunction(); |
| Eric Christopher | ade4eed | 2016-07-07 00:39:32 +0000 | [diff] [blame] | 4249 | const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); |
| 4250 | const PPCFrameLowering *FL = Subtarget.getFrameLowering(); |
| 4251 | bool isPPC64 = Subtarget.isPPC64(); |
| 4252 | int SlotSize = isPPC64 ? 8 : 4; |
| Eric Christopher | dc3a8a4 | 2015-02-13 00:39:38 +0000 | [diff] [blame] | 4253 | int NewRetAddrLoc = SPDiff + FL->getReturnSaveOffset(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 4254 | int NewRetAddr = MF.getFrameInfo().CreateFixedObject(SlotSize, |
| 4255 | NewRetAddrLoc, true); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4256 | EVT VT = isPPC64 ? MVT::i64 : MVT::i32; |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4257 | SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT); |
| Eric Christopher | c16ccbe | 2016-07-07 00:39:30 +0000 | [diff] [blame] | 4258 | Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx, |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 4259 | MachinePointerInfo::getFixedStack(MF, NewRetAddr)); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4260 | |
| Tilmann Scheller | d1aaa32 | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 4261 | // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack |
| 4262 | // slot as the FP is never overwritten. |
| Eric Christopher | 327e440 | 2016-07-07 01:08:17 +0000 | [diff] [blame] | 4263 | if (Subtarget.isDarwinABI()) { |
| Eric Christopher | dc3a8a4 | 2015-02-13 00:39:38 +0000 | [diff] [blame] | 4264 | int NewFPLoc = SPDiff + FL->getFramePointerSaveOffset(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 4265 | int NewFPIdx = MF.getFrameInfo().CreateFixedObject(SlotSize, NewFPLoc, |
| 4266 | true); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4267 | SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 4268 | Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx, |
| 4269 | MachinePointerInfo::getFixedStack( |
| 4270 | DAG.getMachineFunction(), NewFPIdx)); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4271 | } |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4272 | } |
| 4273 | return Chain; |
| 4274 | } |
| 4275 | |
| 4276 | /// CalculateTailCallArgDest - Remember Argument for later processing. Calculate |
| 4277 | /// the position of the argument. |
| 4278 | static void |
| 4279 | CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64, |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4280 | SDValue Arg, int SPDiff, unsigned ArgOffset, |
| Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 4281 | SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) { |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4282 | int Offset = ArgOffset + SPDiff; |
| Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 4283 | uint32_t OpSize = (Arg.getValueSizeInBits() + 7) / 8; |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 4284 | int FI = MF.getFrameInfo().CreateFixedObject(OpSize, Offset, true); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4285 | EVT VT = isPPC64 ? MVT::i64 : MVT::i32; |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4286 | SDValue FIN = DAG.getFrameIndex(FI, VT); |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4287 | TailCallArgumentInfo Info; |
| 4288 | Info.Arg = Arg; |
| 4289 | Info.FrameIdxOp = FIN; |
| 4290 | Info.FrameIdx = FI; |
| 4291 | TailCallArguments.push_back(Info); |
| 4292 | } |
| 4293 | |
| 4294 | /// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address |
| 4295 | /// stack slot. Returns the chain as result and the loaded frame pointers in |
| 4296 | /// LROpOut/FPOpout. Used when tail calling. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 4297 | SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr( |
| 4298 | SelectionDAG &DAG, int SPDiff, SDValue Chain, SDValue &LROpOut, |
| Eric Christopher | e0d09ba | 2016-07-07 01:08:21 +0000 | [diff] [blame] | 4299 | SDValue &FPOpOut, const SDLoc &dl) const { |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4300 | if (SPDiff) { |
| 4301 | // Load the LR and FP stack slot for later adjusting. |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 4302 | EVT VT = Subtarget.isPPC64() ? MVT::i64 : MVT::i32; |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4303 | LROpOut = getReturnAddrFrameIndex(DAG); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 4304 | LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo()); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4305 | Chain = SDValue(LROpOut.getNode(), 1); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4306 | |
| Tilmann Scheller | d1aaa32 | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 4307 | // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack |
| 4308 | // slot as the FP is never overwritten. |
| Eric Christopher | e0d09ba | 2016-07-07 01:08:21 +0000 | [diff] [blame] | 4309 | if (Subtarget.isDarwinABI()) { |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4310 | FPOpOut = getFramePointerFrameIndex(DAG); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 4311 | FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo()); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4312 | Chain = SDValue(FPOpOut.getNode(), 1); |
| 4313 | } |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4314 | } |
| 4315 | return Chain; |
| 4316 | } |
| 4317 | |
| Dale Johannesen | 85d41a1 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 4318 | /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4319 | /// by "Src" to address "Dst" of size "Size". Alignment information is |
| Dale Johannesen | 85d41a1 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 4320 | /// specified by the specific parameter attribute. The copy will be passed as |
| 4321 | /// a byval function parameter. |
| 4322 | /// Sometimes what we are copying is the end of a larger object, the part that |
| 4323 | /// does not fit in registers. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 4324 | static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst, |
| 4325 | SDValue Chain, ISD::ArgFlagsTy Flags, |
| 4326 | SelectionDAG &DAG, const SDLoc &dl) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4327 | SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32); |
| Dale Johannesen | 8526388 | 2009-02-04 01:17:06 +0000 | [diff] [blame] | 4328 | return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(), |
| Krzysztof Parzyszek | a46c36b | 2015-04-13 17:16:45 +0000 | [diff] [blame] | 4329 | false, false, false, MachinePointerInfo(), |
| Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 4330 | MachinePointerInfo()); |
| Dale Johannesen | 85d41a1 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 4331 | } |
| Chris Lattner | 43df5b3 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 4332 | |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4333 | /// LowerMemOpCallTo - Store the argument to the stack or remember it in case of |
| 4334 | /// tail calls. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 4335 | static void LowerMemOpCallTo( |
| 4336 | SelectionDAG &DAG, MachineFunction &MF, SDValue Chain, SDValue Arg, |
| 4337 | SDValue PtrOff, int SPDiff, unsigned ArgOffset, bool isPPC64, |
| 4338 | bool isTailCall, bool isVector, SmallVectorImpl<SDValue> &MemOpChains, |
| 4339 | SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments, const SDLoc &dl) { |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 4340 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4341 | if (!isTailCall) { |
| 4342 | if (isVector) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4343 | SDValue StackPtr; |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4344 | if (isPPC64) |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4345 | StackPtr = DAG.getRegister(PPC::X1, MVT::i64); |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4346 | else |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4347 | StackPtr = DAG.getRegister(PPC::R1, MVT::i32); |
| Dale Johannesen | 021052a | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4348 | PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4349 | DAG.getConstant(ArgOffset, dl, PtrVT)); |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4350 | } |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 4351 | MemOpChains.push_back( |
| 4352 | DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo())); |
| 4353 | // Calculate and remember argument location. |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4354 | } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset, |
| 4355 | TailCallArguments); |
| 4356 | } |
| 4357 | |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 4358 | static void |
| 4359 | PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain, |
| Eric Christopher | 327e440 | 2016-07-07 01:08:17 +0000 | [diff] [blame] | 4360 | const SDLoc &dl, int SPDiff, unsigned NumBytes, SDValue LROp, |
| 4361 | SDValue FPOp, |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 4362 | SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) { |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4363 | // Emit a sequence of copyto/copyfrom virtual registers for arguments that |
| 4364 | // might overwrite each other in case of tail call optimization. |
| 4365 | SmallVector<SDValue, 8> MemOpChains2; |
| Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 4366 | // Do not flag preceding copytoreg stuff together with the following stuff. |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4367 | InFlag = SDValue(); |
| 4368 | StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments, |
| 4369 | MemOpChains2, dl); |
| 4370 | if (!MemOpChains2.empty()) |
| Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 4371 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains2); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4372 | |
| 4373 | // Store the return address to the appropriate stack slot. |
| Eric Christopher | 327e440 | 2016-07-07 01:08:17 +0000 | [diff] [blame] | 4374 | Chain = EmitTailCallStoreFPAndRetAddr(DAG, Chain, LROp, FPOp, SPDiff, dl); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4375 | |
| 4376 | // Emit callseq_end just before tailcall node. |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4377 | Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true), |
| 4378 | DAG.getIntPtrConstant(0, dl, true), InFlag, dl); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4379 | InFlag = Chain.getValue(1); |
| 4380 | } |
| 4381 | |
| Hal Finkel | 87deb0b | 2015-01-12 04:34:47 +0000 | [diff] [blame] | 4382 | // Is this global address that of a function that can be called by name? (as |
| 4383 | // opposed to something that must hold a descriptor for an indirect call). |
| 4384 | static bool isFunctionGlobalAddress(SDValue Callee) { |
| 4385 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
| 4386 | if (Callee.getOpcode() == ISD::GlobalTLSAddress || |
| 4387 | Callee.getOpcode() == ISD::TargetGlobalTLSAddress) |
| 4388 | return false; |
| 4389 | |
| Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 4390 | return G->getGlobal()->getValueType()->isFunctionTy(); |
| Hal Finkel | 87deb0b | 2015-01-12 04:34:47 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
| 4393 | return false; |
| 4394 | } |
| 4395 | |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 4396 | static unsigned |
| 4397 | PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag, SDValue &Chain, |
| 4398 | SDValue CallSeqStart, const SDLoc &dl, int SPDiff, bool isTailCall, |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 4399 | bool isPatchPoint, bool hasNest, |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 4400 | SmallVectorImpl<std::pair<unsigned, SDValue>> &RegsToPass, |
| 4401 | SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys, |
| 4402 | ImmutableCallSite *CS, const PPCSubtarget &Subtarget) { |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4403 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 4404 | bool isPPC64 = Subtarget.isPPC64(); |
| 4405 | bool isSVR4ABI = Subtarget.isSVR4ABI(); |
| Ulrich Weigand | aa0ac4f | 2014-07-20 23:31:44 +0000 | [diff] [blame] | 4406 | bool isELFv2ABI = Subtarget.isELFv2ABI(); |
| Chris Lattner | df8e17d | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 4407 | |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 4408 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4409 | NodeTys.push_back(MVT::Other); // Returns a chain |
| Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 4410 | NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use. |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4411 | |
| Ulrich Weigand | f62e83f | 2013-03-22 15:24:13 +0000 | [diff] [blame] | 4412 | unsigned CallOpc = PPCISD::CALL; |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4413 | |
| Torok Edwin | 31e90d2 | 2010-08-04 20:47:44 +0000 | [diff] [blame] | 4414 | bool needIndirectCall = true; |
| Ulrich Weigand | 9aa09ef | 2014-06-18 16:14:04 +0000 | [diff] [blame] | 4415 | if (!isSVR4ABI || !isPPC64) |
| 4416 | if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) { |
| 4417 | // If this is an absolute destination address, use the munged value. |
| 4418 | Callee = SDValue(Dest, 0); |
| 4419 | needIndirectCall = false; |
| 4420 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4421 | |
| Rafael Espindola | 53fd425 | 2016-06-23 18:43:06 +0000 | [diff] [blame] | 4422 | // PC-relative references to external symbols should go through $stub, unless |
| 4423 | // we're building with the leopard linker or later, which automatically |
| 4424 | // synthesizes these stubs. |
| Rafael Espindola | 3beef8d | 2016-06-27 23:15:57 +0000 | [diff] [blame] | 4425 | const TargetMachine &TM = DAG.getTarget(); |
| Rafael Espindola | 53fd425 | 2016-06-23 18:43:06 +0000 | [diff] [blame] | 4426 | const Module *Mod = DAG.getMachineFunction().getFunction()->getParent(); |
| 4427 | const GlobalValue *GV = nullptr; |
| 4428 | if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
| 4429 | GV = G->getGlobal(); |
| Rafael Espindola | 3beef8d | 2016-06-27 23:15:57 +0000 | [diff] [blame] | 4430 | bool Local = TM.shouldAssumeDSOLocal(*Mod, GV); |
| Rafael Espindola | a99ccfc | 2016-06-29 14:59:50 +0000 | [diff] [blame] | 4431 | bool UsePlt = !Local && Subtarget.isTargetELF() && !isPPC64; |
| Rafael Espindola | 53fd425 | 2016-06-23 18:43:06 +0000 | [diff] [blame] | 4432 | |
| Hal Finkel | 87deb0b | 2015-01-12 04:34:47 +0000 | [diff] [blame] | 4433 | if (isFunctionGlobalAddress(Callee)) { |
| 4434 | GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Callee); |
| 4435 | // A call to a TLS address is actually an indirect call to a |
| 4436 | // thread-specific pointer. |
| Eric Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 4437 | unsigned OpFlags = 0; |
| Rafael Espindola | 53fd425 | 2016-06-23 18:43:06 +0000 | [diff] [blame] | 4438 | if (UsePlt) |
| Rafael Espindola | a99ccfc | 2016-06-29 14:59:50 +0000 | [diff] [blame] | 4439 | OpFlags = PPCII::MO_PLT; |
| Eric Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 4440 | |
| 4441 | // If the callee is a GlobalAddress/ExternalSymbol node (quite common, |
| 4442 | // every direct call is) turn it into a TargetGlobalAddress / |
| 4443 | // TargetExternalSymbol node so that legalize doesn't hack it. |
| 4444 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, |
| 4445 | Callee.getValueType(), 0, OpFlags); |
| 4446 | needIndirectCall = false; |
| Torok Edwin | 31e90d2 | 2010-08-04 20:47:44 +0000 | [diff] [blame] | 4447 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4448 | |
| Torok Edwin | 31e90d2 | 2010-08-04 20:47:44 +0000 | [diff] [blame] | 4449 | if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { |
| Chris Lattner | df8e17d | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 4450 | unsigned char OpFlags = 0; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4451 | |
| Rafael Espindola | 53fd425 | 2016-06-23 18:43:06 +0000 | [diff] [blame] | 4452 | if (UsePlt) |
| Rafael Espindola | a99ccfc | 2016-06-29 14:59:50 +0000 | [diff] [blame] | 4453 | OpFlags = PPCII::MO_PLT; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4454 | |
| Chris Lattner | df8e17d | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 4455 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(), |
| 4456 | OpFlags); |
| 4457 | needIndirectCall = false; |
| Torok Edwin | 31e90d2 | 2010-08-04 20:47:44 +0000 | [diff] [blame] | 4458 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4459 | |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 4460 | if (isPatchPoint) { |
| Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 4461 | // We'll form an invalid direct call when lowering a patchpoint; the full |
| 4462 | // sequence for an indirect call is complicated, and many of the |
| 4463 | // instructions introduced might have side effects (and, thus, can't be |
| 4464 | // removed later). The call itself will be removed as soon as the |
| 4465 | // argument/return lowering is complete, so the fact that it has the wrong |
| 4466 | // kind of operands should not really matter. |
| 4467 | needIndirectCall = false; |
| 4468 | } |
| 4469 | |
| Torok Edwin | 31e90d2 | 2010-08-04 20:47:44 +0000 | [diff] [blame] | 4470 | if (needIndirectCall) { |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4471 | // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair |
| 4472 | // to do the call, we can't use PPCISD::CALL. |
| 4473 | SDValue MTCTROps[] = {Chain, Callee, InFlag}; |
| Tilmann Scheller | 79fef93 | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 4474 | |
| Hal Finkel | 63fb928 | 2015-01-13 18:25:05 +0000 | [diff] [blame] | 4475 | if (isSVR4ABI && isPPC64 && !isELFv2ABI) { |
| Tilmann Scheller | 79fef93 | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 4476 | // Function pointers in the 64-bit SVR4 ABI do not point to the function |
| 4477 | // entry point, but to the function descriptor (the function entry point |
| 4478 | // address is part of the function descriptor though). |
| 4479 | // The function descriptor is a three doubleword structure with the |
| 4480 | // following fields: function entry point, TOC base address and |
| 4481 | // environment pointer. |
| 4482 | // Thus for a call through a function pointer, the following actions need |
| 4483 | // to be performed: |
| 4484 | // 1. Save the TOC of the caller in the TOC save area of its stack |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 4485 | // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()). |
| Tilmann Scheller | 79fef93 | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 4486 | // 2. Load the address of the function entry point from the function |
| 4487 | // descriptor. |
| 4488 | // 3. Load the TOC of the callee from the function descriptor into r2. |
| 4489 | // 4. Load the environment pointer from the function descriptor into |
| 4490 | // r11. |
| 4491 | // 5. Branch to the function entry point address. |
| 4492 | // 6. On return of the callee, the TOC of the caller needs to be |
| 4493 | // restored (this is done in FinishCall()). |
| 4494 | // |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 4495 | // The loads are scheduled at the beginning of the call sequence, and the |
| 4496 | // register copies are flagged together to ensure that no other |
| Tilmann Scheller | 79fef93 | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 4497 | // operations can be scheduled in between. E.g. without flagging the |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 4498 | // copies together, a TOC access in the caller could be scheduled between |
| 4499 | // the assignment of the callee TOC and the branch to the callee, which |
| Tilmann Scheller | 79fef93 | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 4500 | // results in the TOC access going through the TOC of the callee instead |
| 4501 | // of going through the TOC of the caller, which leads to incorrect code. |
| 4502 | |
| 4503 | // Load the address of the function entry point from the function |
| 4504 | // descriptor. |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 4505 | SDValue LDChain = CallSeqStart.getValue(CallSeqStart->getNumValues()-1); |
| 4506 | if (LDChain.getValueType() == MVT::Glue) |
| 4507 | LDChain = CallSeqStart.getValue(CallSeqStart->getNumValues()-2); |
| 4508 | |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 4509 | auto MMOFlags = Subtarget.hasInvariantFunctionDescriptors() |
| Justin Lebar | adbf09e | 2016-09-11 01:38:58 +0000 | [diff] [blame] | 4510 | ? (MachineMemOperand::MODereferenceable | |
| 4511 | MachineMemOperand::MOInvariant) |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 4512 | : MachineMemOperand::MONone; |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 4513 | |
| 4514 | MachinePointerInfo MPI(CS ? CS->getCalledValue() : nullptr); |
| 4515 | SDValue LoadFuncPtr = DAG.getLoad(MVT::i64, dl, LDChain, Callee, MPI, |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 4516 | /* Alignment = */ 8, MMOFlags); |
| Tilmann Scheller | 79fef93 | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 4517 | |
| 4518 | // Load environment pointer into r11. |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4519 | SDValue PtrOff = DAG.getIntPtrConstant(16, dl); |
| Tilmann Scheller | 79fef93 | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 4520 | SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 4521 | SDValue LoadEnvPtr = |
| 4522 | DAG.getLoad(MVT::i64, dl, LDChain, AddPtr, MPI.getWithOffset(16), |
| 4523 | /* Alignment = */ 8, MMOFlags); |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 4524 | |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4525 | SDValue TOCOff = DAG.getIntPtrConstant(8, dl); |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 4526 | SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, TOCOff); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 4527 | SDValue TOCPtr = |
| 4528 | DAG.getLoad(MVT::i64, dl, LDChain, AddTOC, MPI.getWithOffset(8), |
| 4529 | /* Alignment = */ 8, MMOFlags); |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 4530 | |
| Hal Finkel | e6698d5 | 2015-02-01 15:03:28 +0000 | [diff] [blame] | 4531 | setUsesTOCBasePtr(DAG); |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 4532 | SDValue TOCVal = DAG.getCopyToReg(Chain, dl, PPC::X2, TOCPtr, |
| 4533 | InFlag); |
| 4534 | Chain = TOCVal.getValue(0); |
| 4535 | InFlag = TOCVal.getValue(1); |
| Tilmann Scheller | 79fef93 | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 4536 | |
| Hal Finkel | 965cea5 | 2015-07-12 00:37:44 +0000 | [diff] [blame] | 4537 | // If the function call has an explicit 'nest' parameter, it takes the |
| 4538 | // place of the environment pointer. |
| 4539 | if (!hasNest) { |
| 4540 | SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr, |
| 4541 | InFlag); |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 4542 | |
| Hal Finkel | 965cea5 | 2015-07-12 00:37:44 +0000 | [diff] [blame] | 4543 | Chain = EnvVal.getValue(0); |
| 4544 | InFlag = EnvVal.getValue(1); |
| 4545 | } |
| Tilmann Scheller | 79fef93 | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 4546 | |
| Tilmann Scheller | 79fef93 | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 4547 | MTCTROps[0] = Chain; |
| 4548 | MTCTROps[1] = LoadFuncPtr; |
| 4549 | MTCTROps[2] = InFlag; |
| 4550 | } |
| 4551 | |
| Hal Finkel | 63fb928 | 2015-01-13 18:25:05 +0000 | [diff] [blame] | 4552 | Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, |
| 4553 | makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2)); |
| 4554 | InFlag = Chain.getValue(1); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4555 | |
| 4556 | NodeTys.clear(); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4557 | NodeTys.push_back(MVT::Other); |
| Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 4558 | NodeTys.push_back(MVT::Glue); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4559 | Ops.push_back(Chain); |
| Ulrich Weigand | f62e83f | 2013-03-22 15:24:13 +0000 | [diff] [blame] | 4560 | CallOpc = PPCISD::BCTRL; |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 4561 | Callee.setNode(nullptr); |
| Ulrich Weigand | f62e83f | 2013-03-22 15:24:13 +0000 | [diff] [blame] | 4562 | // Add use of X11 (holding environment pointer) |
| Hal Finkel | 965cea5 | 2015-07-12 00:37:44 +0000 | [diff] [blame] | 4563 | if (isSVR4ABI && isPPC64 && !isELFv2ABI && !hasNest) |
| Ulrich Weigand | f62e83f | 2013-03-22 15:24:13 +0000 | [diff] [blame] | 4564 | Ops.push_back(DAG.getRegister(PPC::X11, PtrVT)); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4565 | // Add CTR register as callee so a bctr can be emitted later. |
| 4566 | if (isTailCall) |
| Roman Divacky | a4a59ae | 2011-06-03 15:47:49 +0000 | [diff] [blame] | 4567 | Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT)); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4568 | } |
| 4569 | |
| 4570 | // If this is a direct call, pass the chain and the callee. |
| 4571 | if (Callee.getNode()) { |
| 4572 | Ops.push_back(Chain); |
| 4573 | Ops.push_back(Callee); |
| 4574 | } |
| 4575 | // If this is a tail call add stack pointer delta. |
| 4576 | if (isTailCall) |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4577 | Ops.push_back(DAG.getConstant(SPDiff, dl, MVT::i32)); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4578 | |
| 4579 | // Add argument registers to the end of the list so that they are known live |
| 4580 | // into the call. |
| 4581 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
| 4582 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
| 4583 | RegsToPass[i].second.getValueType())); |
| 4584 | |
| Hal Finkel | af51993 | 2015-01-19 07:20:27 +0000 | [diff] [blame] | 4585 | // All calls, in both the ELF V1 and V2 ABIs, need the TOC register live |
| 4586 | // into the call. |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 4587 | if (isSVR4ABI && isPPC64 && !isPatchPoint) { |
| Hal Finkel | e6698d5 | 2015-02-01 15:03:28 +0000 | [diff] [blame] | 4588 | setUsesTOCBasePtr(DAG); |
| Ulrich Weigand | aa0ac4f | 2014-07-20 23:31:44 +0000 | [diff] [blame] | 4589 | Ops.push_back(DAG.getRegister(PPC::X2, PtrVT)); |
| Hal Finkel | e6698d5 | 2015-02-01 15:03:28 +0000 | [diff] [blame] | 4590 | } |
| Ulrich Weigand | aa0ac4f | 2014-07-20 23:31:44 +0000 | [diff] [blame] | 4591 | |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4592 | return CallOpc; |
| 4593 | } |
| 4594 | |
| Roman Divacky | 7629306 | 2012-09-18 16:47:58 +0000 | [diff] [blame] | 4595 | static |
| 4596 | bool isLocalCall(const SDValue &Callee) |
| 4597 | { |
| 4598 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
| Peter Collingbourne | 6a9d177 | 2015-07-05 20:52:35 +0000 | [diff] [blame] | 4599 | return G->getGlobal()->isStrongDefinitionForLinker(); |
| Roman Divacky | 7629306 | 2012-09-18 16:47:58 +0000 | [diff] [blame] | 4600 | return false; |
| 4601 | } |
| 4602 | |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 4603 | SDValue PPCTargetLowering::LowerCallResult( |
| 4604 | SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg, |
| 4605 | const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, |
| 4606 | SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4607 | |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4608 | SmallVector<CCValAssign, 16> RVLocs; |
| Eric Christopher | b521750 | 2014-08-06 18:45:26 +0000 | [diff] [blame] | 4609 | CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, |
| 4610 | *DAG.getContext()); |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4611 | CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4612 | |
| 4613 | // Copy all of the result registers out of their specified physreg. |
| 4614 | for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) { |
| 4615 | CCValAssign &VA = RVLocs[i]; |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4616 | assert(VA.isRegLoc() && "Can only return in registers!"); |
| Ulrich Weigand | 339d059 | 2012-11-05 19:39:45 +0000 | [diff] [blame] | 4617 | |
| 4618 | SDValue Val = DAG.getCopyFromReg(Chain, dl, |
| 4619 | VA.getLocReg(), VA.getLocVT(), InFlag); |
| 4620 | Chain = Val.getValue(1); |
| 4621 | InFlag = Val.getValue(2); |
| 4622 | |
| 4623 | switch (VA.getLocInfo()) { |
| 4624 | default: llvm_unreachable("Unknown loc info!"); |
| 4625 | case CCValAssign::Full: break; |
| 4626 | case CCValAssign::AExt: |
| 4627 | Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val); |
| 4628 | break; |
| 4629 | case CCValAssign::ZExt: |
| 4630 | Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val, |
| 4631 | DAG.getValueType(VA.getValVT())); |
| 4632 | Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val); |
| 4633 | break; |
| 4634 | case CCValAssign::SExt: |
| 4635 | Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val, |
| 4636 | DAG.getValueType(VA.getValVT())); |
| 4637 | Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val); |
| 4638 | break; |
| 4639 | } |
| 4640 | |
| 4641 | InVals.push_back(Val); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4644 | return Chain; |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4645 | } |
| 4646 | |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 4647 | SDValue PPCTargetLowering::FinishCall( |
| 4648 | CallingConv::ID CallConv, const SDLoc &dl, bool isTailCall, bool isVarArg, |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 4649 | bool isPatchPoint, bool hasNest, SelectionDAG &DAG, |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 4650 | SmallVector<std::pair<unsigned, SDValue>, 8> &RegsToPass, SDValue InFlag, |
| 4651 | SDValue Chain, SDValue CallSeqStart, SDValue &Callee, int SPDiff, |
| 4652 | unsigned NumBytes, const SmallVectorImpl<ISD::InputArg> &Ins, |
| 4653 | SmallVectorImpl<SDValue> &InVals, ImmutableCallSite *CS) const { |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 4654 | |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4655 | std::vector<EVT> NodeTys; |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4656 | SmallVector<SDValue, 8> Ops; |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 4657 | unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, CallSeqStart, dl, |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 4658 | SPDiff, isTailCall, isPatchPoint, hasNest, |
| Hal Finkel | 965cea5 | 2015-07-12 00:37:44 +0000 | [diff] [blame] | 4659 | RegsToPass, Ops, NodeTys, CS, Subtarget); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4660 | |
| Hal Finkel | 5ab3780 | 2012-08-28 02:10:27 +0000 | [diff] [blame] | 4661 | // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 4662 | if (isVarArg && Subtarget.isSVR4ABI() && !Subtarget.isPPC64()) |
| Hal Finkel | 5ab3780 | 2012-08-28 02:10:27 +0000 | [diff] [blame] | 4663 | Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32)); |
| 4664 | |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4665 | // When performing tail call optimization the callee pops its arguments off |
| 4666 | // the stack. Account for this here so these bytes can be pushed back on in |
| Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 4667 | // PPCFrameLowering::eliminateCallFramePseudoInstr. |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4668 | int BytesCalleePops = |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 4669 | (CallConv == CallingConv::Fast && |
| 4670 | getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0; |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4671 | |
| Roman Divacky | ef21be2 | 2012-03-06 16:41:49 +0000 | [diff] [blame] | 4672 | // Add a register mask operand representing the call-preserved registers. |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 4673 | const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); |
| Eric Christopher | 9deb75d | 2015-03-11 22:42:13 +0000 | [diff] [blame] | 4674 | const uint32_t *Mask = |
| 4675 | TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv); |
| Roman Divacky | ef21be2 | 2012-03-06 16:41:49 +0000 | [diff] [blame] | 4676 | assert(Mask && "Missing call preserved mask for calling convention"); |
| 4677 | Ops.push_back(DAG.getRegisterMask(Mask)); |
| 4678 | |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4679 | if (InFlag.getNode()) |
| 4680 | Ops.push_back(InFlag); |
| 4681 | |
| 4682 | // Emit tail call. |
| 4683 | if (isTailCall) { |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4684 | assert(((Callee.getOpcode() == ISD::Register && |
| 4685 | cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) || |
| 4686 | Callee.getOpcode() == ISD::TargetExternalSymbol || |
| 4687 | Callee.getOpcode() == ISD::TargetGlobalAddress || |
| 4688 | isa<ConstantSDNode>(Callee)) && |
| 4689 | "Expecting an global address, external symbol, absolute value or register"); |
| 4690 | |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 4691 | DAG.getMachineFunction().getFrameInfo().setHasTailCall(); |
| Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 4692 | return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, Ops); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4693 | } |
| 4694 | |
| Tilmann Scheller | d1aaa32 | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 4695 | // Add a NOP immediately after the branch instruction when using the 64-bit |
| 4696 | // SVR4 ABI. At link time, if caller and callee are in a different module and |
| 4697 | // thus have a different TOC, the call will be replaced with a call to a stub |
| 4698 | // function which saves the current TOC, loads the TOC of the callee and |
| 4699 | // branches to the callee. The NOP will be replaced with a load instruction |
| 4700 | // which restores the TOC of the caller from the TOC save slot of the current |
| 4701 | // stack frame. If caller and callee belong to the same module (and have the |
| 4702 | // same TOC), the NOP will remain unchanged. |
| Hal Finkel | 51861b4 | 2012-03-31 14:45:15 +0000 | [diff] [blame] | 4703 | |
| Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 4704 | if (!isTailCall && Subtarget.isSVR4ABI()&& Subtarget.isPPC64() && |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 4705 | !isPatchPoint) { |
| Ulrich Weigand | f62e83f | 2013-03-22 15:24:13 +0000 | [diff] [blame] | 4706 | if (CallOpc == PPCISD::BCTRL) { |
| Tilmann Scheller | 79fef93 | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 4707 | // This is a call through a function pointer. |
| 4708 | // Restore the caller TOC from the save area into R2. |
| 4709 | // See PrepareCall() for more information about calls through function |
| 4710 | // pointers in the 64-bit SVR4 ABI. |
| 4711 | // We are using a target-specific load with r2 hard coded, because the |
| 4712 | // result of a target-independent load would never go directly into r2, |
| 4713 | // since r2 is a reserved register (which prevents the register allocator |
| 4714 | // from allocating it), resulting in an additional register being |
| 4715 | // allocated and an unnecessary move instruction being generated. |
| Hal Finkel | fc096c9 | 2014-12-23 22:29:40 +0000 | [diff] [blame] | 4716 | CallOpc = PPCISD::BCTRL_LOAD_TOC; |
| 4717 | |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 4718 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
| Hal Finkel | fc096c9 | 2014-12-23 22:29:40 +0000 | [diff] [blame] | 4719 | SDValue StackPtr = DAG.getRegister(PPC::X1, PtrVT); |
| Eric Christopher | 736d39e | 2015-02-13 00:39:36 +0000 | [diff] [blame] | 4720 | unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset(); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4721 | SDValue TOCOff = DAG.getIntPtrConstant(TOCSaveOffset, dl); |
| Hal Finkel | fc096c9 | 2014-12-23 22:29:40 +0000 | [diff] [blame] | 4722 | SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, StackPtr, TOCOff); |
| 4723 | |
| 4724 | // The address needs to go after the chain input but before the flag (or |
| 4725 | // any other variadic arguments). |
| 4726 | Ops.insert(std::next(Ops.begin()), AddTOC); |
| Bill Schmidt | cea1596 | 2013-09-26 17:09:28 +0000 | [diff] [blame] | 4727 | } else if ((CallOpc == PPCISD::CALL) && |
| 4728 | (!isLocalCall(Callee) || |
| Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 4729 | DAG.getTarget().getRelocationModel() == Reloc::PIC_)) |
| Roman Divacky | 7629306 | 2012-09-18 16:47:58 +0000 | [diff] [blame] | 4730 | // Otherwise insert NOP for non-local calls. |
| Ulrich Weigand | f62e83f | 2013-03-22 15:24:13 +0000 | [diff] [blame] | 4731 | CallOpc = PPCISD::CALL_NOP; |
| Tilmann Scheller | d1aaa32 | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 4732 | } |
| 4733 | |
| Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 4734 | Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops); |
| Hal Finkel | 51861b4 | 2012-03-31 14:45:15 +0000 | [diff] [blame] | 4735 | InFlag = Chain.getValue(1); |
| 4736 | |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4737 | Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true), |
| 4738 | DAG.getIntPtrConstant(BytesCalleePops, dl, true), |
| Andrew Trick | ad6d08a | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 4739 | InFlag, dl); |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4740 | if (!Ins.empty()) |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4741 | InFlag = Chain.getValue(1); |
| 4742 | |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4743 | return LowerCallResult(Chain, InFlag, CallConv, isVarArg, |
| 4744 | Ins, dl, DAG, InVals); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4745 | } |
| 4746 | |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4747 | SDValue |
| Justin Holewinski | aa58397 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 4748 | PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 4749 | SmallVectorImpl<SDValue> &InVals) const { |
| Justin Holewinski | aa58397 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 4750 | SelectionDAG &DAG = CLI.DAG; |
| Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 4751 | SDLoc &dl = CLI.DL; |
| 4752 | SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; |
| 4753 | SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; |
| 4754 | SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; |
| Justin Holewinski | aa58397 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 4755 | SDValue Chain = CLI.Chain; |
| 4756 | SDValue Callee = CLI.Callee; |
| 4757 | bool &isTailCall = CLI.IsTailCall; |
| 4758 | CallingConv::ID CallConv = CLI.CallConv; |
| 4759 | bool isVarArg = CLI.IsVarArg; |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 4760 | bool isPatchPoint = CLI.IsPatchPoint; |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 4761 | ImmutableCallSite *CS = CLI.CS; |
| Justin Holewinski | aa58397 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 4762 | |
| Chuang-Yu Cheng | 2e5973e | 2016-04-06 02:04:38 +0000 | [diff] [blame] | 4763 | if (isTailCall) { |
| Hal Finkel | b074a60 | 2016-08-30 00:59:23 +0000 | [diff] [blame] | 4764 | if (Subtarget.useLongCalls() && !(CS && CS->isMustTailCall())) |
| 4765 | isTailCall = false; |
| 4766 | else if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) |
| Chuang-Yu Cheng | 2e5973e | 2016-04-06 02:04:38 +0000 | [diff] [blame] | 4767 | isTailCall = |
| 4768 | IsEligibleForTailCallOptimization_64SVR4(Callee, CallConv, CS, |
| 4769 | isVarArg, Outs, Ins, DAG); |
| 4770 | else |
| 4771 | isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, |
| 4772 | Ins, DAG); |
| 4773 | if (isTailCall) { |
| 4774 | ++NumTailCalls; |
| 4775 | if (!getTargetMachine().Options.GuaranteedTailCallOpt) |
| 4776 | ++NumSiblingCalls; |
| 4777 | |
| 4778 | assert(isa<GlobalAddressSDNode>(Callee) && |
| 4779 | "Callee should be an llvm::Function object."); |
| 4780 | DEBUG( |
| 4781 | const GlobalValue *GV = cast<GlobalAddressSDNode>(Callee)->getGlobal(); |
| 4782 | const unsigned Width = 80 - strlen("TCO caller: ") |
| 4783 | - strlen(", callee linkage: 0, 0"); |
| 4784 | dbgs() << "TCO caller: " |
| 4785 | << left_justify(DAG.getMachineFunction().getName(), Width) |
| 4786 | << ", callee linkage: " |
| 4787 | << GV->getVisibility() << ", " << GV->getLinkage() << "\n" |
| 4788 | ); |
| 4789 | } |
| 4790 | } |
| Evan Cheng | 67a69dd | 2010-01-27 00:07:07 +0000 | [diff] [blame] | 4791 | |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 4792 | if (!isTailCall && CS && CS->isMustTailCall()) |
| Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 4793 | report_fatal_error("failed to perform tail call elimination on a call " |
| 4794 | "site marked musttail"); |
| 4795 | |
| Hal Finkel | b074a60 | 2016-08-30 00:59:23 +0000 | [diff] [blame] | 4796 | // When long calls (i.e. indirect calls) are always used, calls are always |
| 4797 | // made via function pointer. If we have a function name, first translate it |
| 4798 | // into a pointer. |
| 4799 | if (Subtarget.useLongCalls() && isa<GlobalAddressSDNode>(Callee) && |
| 4800 | !isTailCall) |
| 4801 | Callee = LowerGlobalAddress(Callee, DAG); |
| 4802 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 4803 | if (Subtarget.isSVR4ABI()) { |
| 4804 | if (Subtarget.isPPC64()) |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 4805 | return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg, |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 4806 | isTailCall, isPatchPoint, Outs, OutVals, Ins, |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 4807 | dl, DAG, InVals, CS); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 4808 | else |
| 4809 | return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg, |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 4810 | isTailCall, isPatchPoint, Outs, OutVals, Ins, |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 4811 | dl, DAG, InVals, CS); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 4812 | } |
| Chris Lattner | df8e17d | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 4813 | |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 4814 | return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg, |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 4815 | isTailCall, isPatchPoint, Outs, OutVals, Ins, |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 4816 | dl, DAG, InVals, CS); |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4817 | } |
| 4818 | |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 4819 | SDValue PPCTargetLowering::LowerCall_32SVR4( |
| 4820 | SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool isVarArg, |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 4821 | bool isTailCall, bool isPatchPoint, |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 4822 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 4823 | const SmallVectorImpl<SDValue> &OutVals, |
| 4824 | const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, |
| 4825 | SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, |
| 4826 | ImmutableCallSite *CS) const { |
| Bill Schmidt | 019cc6f | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 4827 | // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description |
| Tilmann Scheller | d1aaa32 | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 4828 | // of the 32-bit SVR4 ABI stack frame layout. |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4829 | |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4830 | assert((CallConv == CallingConv::C || |
| 4831 | CallConv == CallingConv::Fast) && "Unknown calling convention!"); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4832 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4833 | unsigned PtrByteSize = 4; |
| 4834 | |
| 4835 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4836 | |
| 4837 | // Mark this function as potentially containing a function that contains a |
| 4838 | // tail call. As a consequence the frame pointer will be used for dynamicalloc |
| 4839 | // and restoring the callers stack pointer in this functions epilog. This is |
| 4840 | // done because by tail calling the called function might overwrite the value |
| 4841 | // in this function's (MF) stack pointer stack slot 0(SP). |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 4842 | if (getTargetMachine().Options.GuaranteedTailCallOpt && |
| 4843 | CallConv == CallingConv::Fast) |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4844 | MF.getInfo<PPCFunctionInfo>()->setHasFastCall(); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4845 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4846 | // Count how many bytes are to be pushed on the stack, including the linkage |
| 4847 | // area, parameter list area and the part of the local variable space which |
| 4848 | // contains copies of aggregates which are passed by value. |
| 4849 | |
| 4850 | // Assign locations to all of the outgoing arguments. |
| 4851 | SmallVector<CCValAssign, 16> ArgLocs; |
| Eric Christopher | 606a268 | 2016-07-07 01:08:19 +0000 | [diff] [blame] | 4852 | PPCCCState CCInfo(CallConv, isVarArg, MF, ArgLocs, *DAG.getContext()); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4853 | |
| 4854 | // Reserve space for the linkage area on the stack. |
| Eric Christopher | a4ae213 | 2015-02-13 22:22:57 +0000 | [diff] [blame] | 4855 | CCInfo.AllocateStack(Subtarget.getFrameLowering()->getLinkageSize(), |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 4856 | PtrByteSize); |
| Eric Christopher | 317df66 | 2016-07-07 01:49:57 +0000 | [diff] [blame] | 4857 | if (useSoftFloat()) |
| Strahinja Petrovic | e682b80 | 2016-05-09 12:27:39 +0000 | [diff] [blame] | 4858 | CCInfo.PreAnalyzeCallOperands(Outs); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4859 | |
| 4860 | if (isVarArg) { |
| 4861 | // Handle fixed and variable vector arguments differently. |
| 4862 | // Fixed vector arguments go into registers as long as registers are |
| 4863 | // available. Variable vector arguments always go into memory. |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4864 | unsigned NumArgs = Outs.size(); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4865 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4866 | for (unsigned i = 0; i != NumArgs; ++i) { |
| Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 4867 | MVT ArgVT = Outs[i].VT; |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4868 | ISD::ArgFlagsTy ArgFlags = Outs[i].Flags; |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4869 | bool Result; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4870 | |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4871 | if (Outs[i].IsFixed) { |
| Bill Schmidt | ef17c14 | 2013-02-06 17:33:58 +0000 | [diff] [blame] | 4872 | Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, |
| 4873 | CCInfo); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4874 | } else { |
| Bill Schmidt | ef17c14 | 2013-02-06 17:33:58 +0000 | [diff] [blame] | 4875 | Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full, |
| 4876 | ArgFlags, CCInfo); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4877 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4878 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4879 | if (Result) { |
| Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 4880 | #ifndef NDEBUG |
| Chris Lattner | 1362602 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 4881 | errs() << "Call operand #" << i << " has unhandled type " |
| Duncan Sands | f5dda01 | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 4882 | << EVT(ArgVT).getEVTString() << "\n"; |
| Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 4883 | #endif |
| Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 4884 | llvm_unreachable(nullptr); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4885 | } |
| 4886 | } |
| 4887 | } else { |
| 4888 | // All arguments are treated the same. |
| Bill Schmidt | ef17c14 | 2013-02-06 17:33:58 +0000 | [diff] [blame] | 4889 | CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4890 | } |
| Strahinja Petrovic | e682b80 | 2016-05-09 12:27:39 +0000 | [diff] [blame] | 4891 | CCInfo.clearWasPPCF128(); |
| NAKAMURA Takumi | fd92154 | 2016-06-20 01:05:15 +0000 | [diff] [blame] | 4892 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4893 | // Assign locations to all of the outgoing aggregate by value arguments. |
| 4894 | SmallVector<CCValAssign, 16> ByValArgLocs; |
| Eric Christopher | 606a268 | 2016-07-07 01:08:19 +0000 | [diff] [blame] | 4895 | CCState CCByValInfo(CallConv, isVarArg, MF, ByValArgLocs, *DAG.getContext()); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4896 | |
| 4897 | // Reserve stack space for the allocations in CCInfo. |
| 4898 | CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize); |
| 4899 | |
| Bill Schmidt | ef17c14 | 2013-02-06 17:33:58 +0000 | [diff] [blame] | 4900 | CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4901 | |
| 4902 | // Size of the linkage area, parameter list area and the part of the local |
| 4903 | // space variable where copies of aggregates which are passed by value are |
| 4904 | // stored. |
| 4905 | unsigned NumBytes = CCByValInfo.getNextStackOffset(); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4906 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4907 | // Calculate by how many bytes the stack has to be adjusted in case of tail |
| 4908 | // call optimization. |
| 4909 | int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes); |
| 4910 | |
| 4911 | // Adjust the stack pointer for the new arguments... |
| 4912 | // These operations are automatically eliminated by the prolog/epilog pass |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4913 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, dl, true), |
| Andrew Trick | ad6d08a | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 4914 | dl); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4915 | SDValue CallSeqStart = Chain; |
| 4916 | |
| 4917 | // Load the return address and frame pointer so it can be moved somewhere else |
| 4918 | // later. |
| 4919 | SDValue LROp, FPOp; |
| Eric Christopher | e0d09ba | 2016-07-07 01:08:21 +0000 | [diff] [blame] | 4920 | Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, dl); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4921 | |
| 4922 | // Set up a copy of the stack pointer for use loading and storing any |
| 4923 | // arguments that may not fit in the registers available for argument |
| 4924 | // passing. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4925 | SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4926 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4927 | SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; |
| 4928 | SmallVector<TailCallArgumentInfo, 8> TailCallArguments; |
| 4929 | SmallVector<SDValue, 8> MemOpChains; |
| 4930 | |
| Roman Divacky | 71038e7 | 2011-08-30 17:04:16 +0000 | [diff] [blame] | 4931 | bool seenFloatArg = false; |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4932 | // Walk the register/memloc assignments, inserting copies/loads. |
| 4933 | for (unsigned i = 0, j = 0, e = ArgLocs.size(); |
| 4934 | i != e; |
| 4935 | ++i) { |
| 4936 | CCValAssign &VA = ArgLocs[i]; |
| Dan Gohman | fe7532a | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 4937 | SDValue Arg = OutVals[i]; |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4938 | ISD::ArgFlagsTy Flags = Outs[i].Flags; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4939 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4940 | if (Flags.isByVal()) { |
| 4941 | // Argument is an aggregate which is passed by value, thus we need to |
| 4942 | // create a copy of it in the local variable space of the current stack |
| 4943 | // frame (which is the stack frame of the caller) and pass the address of |
| 4944 | // this copy to the callee. |
| 4945 | assert((j < ByValArgLocs.size()) && "Index out of bounds!"); |
| 4946 | CCValAssign &ByValVA = ByValArgLocs[j++]; |
| 4947 | assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!"); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4948 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4949 | // Memory reserved in the local variable space of the callers stack frame. |
| 4950 | unsigned LocMemOffset = ByValVA.getLocMemOffset(); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4951 | |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4952 | SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl); |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 4953 | PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(MF.getDataLayout()), |
| 4954 | StackPtr, PtrOff); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4955 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4956 | // Create a copy of the argument in the local area of the current |
| 4957 | // stack frame. |
| 4958 | SDValue MemcpyCall = |
| 4959 | CreateCopyOfByValArgument(Arg, PtrOff, |
| 4960 | CallSeqStart.getNode()->getOperand(0), |
| 4961 | Flags, DAG, dl); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4962 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4963 | // This must go outside the CALLSEQ_START..END. |
| 4964 | SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall, |
| Andrew Trick | ad6d08a | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 4965 | CallSeqStart.getNode()->getOperand(1), |
| 4966 | SDLoc(MemcpyCall)); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4967 | DAG.ReplaceAllUsesWith(CallSeqStart.getNode(), |
| 4968 | NewCallSeqStart.getNode()); |
| 4969 | Chain = CallSeqStart = NewCallSeqStart; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4970 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4971 | // Pass the address of the aggregate copy on the stack either in a |
| 4972 | // physical register or in the parameter list area of the current stack |
| 4973 | // frame to the callee. |
| 4974 | Arg = PtrOff; |
| 4975 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4976 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4977 | if (VA.isRegLoc()) { |
| Hal Finkel | 2a9d318 | 2014-03-06 00:23:33 +0000 | [diff] [blame] | 4978 | if (Arg.getValueType() == MVT::i1) |
| 4979 | Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Arg); |
| 4980 | |
| Roman Divacky | 71038e7 | 2011-08-30 17:04:16 +0000 | [diff] [blame] | 4981 | seenFloatArg |= VA.getLocVT().isFloatingPoint(); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4982 | // Put argument in a physical register. |
| 4983 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); |
| 4984 | } else { |
| 4985 | // Put argument in the parameter list area of the current stack frame. |
| 4986 | assert(VA.isMemLoc()); |
| 4987 | unsigned LocMemOffset = VA.getLocMemOffset(); |
| 4988 | |
| 4989 | if (!isTailCall) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4990 | SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl); |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 4991 | PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(MF.getDataLayout()), |
| 4992 | StackPtr, PtrOff); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4993 | |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 4994 | MemOpChains.push_back( |
| 4995 | DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo())); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4996 | } else { |
| 4997 | // Calculate and remember argument location. |
| 4998 | CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset, |
| 4999 | TailCallArguments); |
| 5000 | } |
| 5001 | } |
| 5002 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5003 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 5004 | if (!MemOpChains.empty()) |
| Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 5005 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5006 | |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 5007 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 5008 | // and flag operands which copy the outgoing args into the appropriate regs. |
| 5009 | SDValue InFlag; |
| 5010 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 5011 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, |
| 5012 | RegsToPass[i].second, InFlag); |
| 5013 | InFlag = Chain.getValue(1); |
| 5014 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5015 | |
| Hal Finkel | 5ab3780 | 2012-08-28 02:10:27 +0000 | [diff] [blame] | 5016 | // Set CR bit 6 to true if this is a vararg call with floating args passed in |
| 5017 | // registers. |
| 5018 | if (isVarArg) { |
| NAKAMURA Takumi | ac49029 | 2012-08-30 15:52:29 +0000 | [diff] [blame] | 5019 | SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue); |
| 5020 | SDValue Ops[] = { Chain, InFlag }; |
| 5021 | |
| Hal Finkel | 5ab3780 | 2012-08-28 02:10:27 +0000 | [diff] [blame] | 5022 | Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET, |
| Craig Topper | 2d2aa0c | 2014-04-30 07:17:30 +0000 | [diff] [blame] | 5023 | dl, VTs, makeArrayRef(Ops, InFlag.getNode() ? 2 : 1)); |
| NAKAMURA Takumi | ac49029 | 2012-08-30 15:52:29 +0000 | [diff] [blame] | 5024 | |
| Hal Finkel | 5ab3780 | 2012-08-28 02:10:27 +0000 | [diff] [blame] | 5025 | InFlag = Chain.getValue(1); |
| 5026 | } |
| 5027 | |
| Chris Lattner | df8e17d | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 5028 | if (isTailCall) |
| Eric Christopher | 327e440 | 2016-07-07 01:08:17 +0000 | [diff] [blame] | 5029 | PrepareTailCall(DAG, InFlag, Chain, dl, SPDiff, NumBytes, LROp, FPOp, |
| Eric Christopher | ade4eed | 2016-07-07 00:39:32 +0000 | [diff] [blame] | 5030 | TailCallArguments); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 5031 | |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 5032 | return FinishCall(CallConv, dl, isTailCall, isVarArg, isPatchPoint, |
| Hal Finkel | 965cea5 | 2015-07-12 00:37:44 +0000 | [diff] [blame] | 5033 | /* unused except on PPC64 ELFv1 */ false, DAG, |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 5034 | RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff, |
| 5035 | NumBytes, Ins, InVals, CS); |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 5036 | } |
| 5037 | |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5038 | // Copy an argument into memory, being careful to do this outside the |
| 5039 | // call sequence for the call to which the argument belongs. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 5040 | SDValue PPCTargetLowering::createMemcpyOutsideCallSeq( |
| 5041 | SDValue Arg, SDValue PtrOff, SDValue CallSeqStart, ISD::ArgFlagsTy Flags, |
| 5042 | SelectionDAG &DAG, const SDLoc &dl) const { |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5043 | SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff, |
| 5044 | CallSeqStart.getNode()->getOperand(0), |
| 5045 | Flags, DAG, dl); |
| 5046 | // The MEMCPY must go outside the CALLSEQ_START..END. |
| 5047 | SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall, |
| Andrew Trick | ad6d08a | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 5048 | CallSeqStart.getNode()->getOperand(1), |
| 5049 | SDLoc(MemcpyCall)); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5050 | DAG.ReplaceAllUsesWith(CallSeqStart.getNode(), |
| 5051 | NewCallSeqStart.getNode()); |
| 5052 | return NewCallSeqStart; |
| 5053 | } |
| 5054 | |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 5055 | SDValue PPCTargetLowering::LowerCall_64SVR4( |
| 5056 | SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool isVarArg, |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 5057 | bool isTailCall, bool isPatchPoint, |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 5058 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 5059 | const SmallVectorImpl<SDValue> &OutVals, |
| 5060 | const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, |
| 5061 | SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, |
| 5062 | ImmutableCallSite *CS) const { |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5063 | |
| Ulrich Weigand | aa0ac4f | 2014-07-20 23:31:44 +0000 | [diff] [blame] | 5064 | bool isELFv2ABI = Subtarget.isELFv2ABI(); |
| Ulrich Weigand | 59c6ab2 | 2014-06-20 16:34:05 +0000 | [diff] [blame] | 5065 | bool isLittleEndian = Subtarget.isLittleEndian(); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5066 | unsigned NumOps = Outs.size(); |
| Hal Finkel | 965cea5 | 2015-07-12 00:37:44 +0000 | [diff] [blame] | 5067 | bool hasNest = false; |
| Chuang-Yu Cheng | 2e5973e | 2016-04-06 02:04:38 +0000 | [diff] [blame] | 5068 | bool IsSibCall = false; |
| Bill Schmidt | 019cc6f | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 5069 | |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 5070 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5071 | unsigned PtrByteSize = 8; |
| 5072 | |
| 5073 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5074 | |
| Chuang-Yu Cheng | 2e5973e | 2016-04-06 02:04:38 +0000 | [diff] [blame] | 5075 | if (isTailCall && !getTargetMachine().Options.GuaranteedTailCallOpt) |
| 5076 | IsSibCall = true; |
| 5077 | |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5078 | // Mark this function as potentially containing a function that contains a |
| 5079 | // tail call. As a consequence the frame pointer will be used for dynamicalloc |
| 5080 | // and restoring the callers stack pointer in this functions epilog. This is |
| 5081 | // done because by tail calling the called function might overwrite the value |
| 5082 | // in this function's (MF) stack pointer stack slot 0(SP). |
| 5083 | if (getTargetMachine().Options.GuaranteedTailCallOpt && |
| 5084 | CallConv == CallingConv::Fast) |
| 5085 | MF.getInfo<PPCFunctionInfo>()->setHasFastCall(); |
| 5086 | |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5087 | assert(!(CallConv == CallingConv::Fast && isVarArg) && |
| 5088 | "fastcc not supported on varargs functions"); |
| 5089 | |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5090 | // Count how many bytes are to be pushed on the stack, including the linkage |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 5091 | // area, and parameter passing area. On ELFv1, the linkage area is 48 bytes |
| 5092 | // reserved space for [SP][CR][LR][2 x unused][TOC]; on ELFv2, the linkage |
| 5093 | // area is 32 bytes reserved space for [SP][CR][LR][TOC]. |
| Eric Christopher | a4ae213 | 2015-02-13 22:22:57 +0000 | [diff] [blame] | 5094 | unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize(); |
| Ulrich Weigand | 8ca988f | 2014-06-23 14:15:53 +0000 | [diff] [blame] | 5095 | unsigned NumBytes = LinkageSize; |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5096 | unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 5097 | unsigned &QFPR_idx = FPR_idx; |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5098 | |
| 5099 | static const MCPhysReg GPR[] = { |
| 5100 | PPC::X3, PPC::X4, PPC::X5, PPC::X6, |
| 5101 | PPC::X7, PPC::X8, PPC::X9, PPC::X10, |
| 5102 | }; |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5103 | static const MCPhysReg VR[] = { |
| 5104 | PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, |
| 5105 | PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 |
| 5106 | }; |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5107 | |
| 5108 | const unsigned NumGPRs = array_lengthof(GPR); |
| 5109 | const unsigned NumFPRs = 13; |
| 5110 | const unsigned NumVRs = array_lengthof(VR); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 5111 | const unsigned NumQFPRs = NumFPRs; |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5112 | |
| 5113 | // When using the fast calling convention, we don't provide backing for |
| 5114 | // arguments that will be in registers. |
| 5115 | unsigned NumGPRsUsed = 0, NumFPRsUsed = 0, NumVRsUsed = 0; |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 5116 | |
| 5117 | // Add up all the space actually used. |
| 5118 | for (unsigned i = 0; i != NumOps; ++i) { |
| 5119 | ISD::ArgFlagsTy Flags = Outs[i].Flags; |
| 5120 | EVT ArgVT = Outs[i].VT; |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 5121 | EVT OrigVT = Outs[i].ArgVT; |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 5122 | |
| Hal Finkel | 965cea5 | 2015-07-12 00:37:44 +0000 | [diff] [blame] | 5123 | if (Flags.isNest()) |
| 5124 | continue; |
| 5125 | |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5126 | if (CallConv == CallingConv::Fast) { |
| 5127 | if (Flags.isByVal()) |
| 5128 | NumGPRsUsed += (Flags.getByValSize()+7)/8; |
| 5129 | else |
| 5130 | switch (ArgVT.getSimpleVT().SimpleTy) { |
| 5131 | default: llvm_unreachable("Unexpected ValueType for argument!"); |
| 5132 | case MVT::i1: |
| 5133 | case MVT::i32: |
| 5134 | case MVT::i64: |
| 5135 | if (++NumGPRsUsed <= NumGPRs) |
| 5136 | continue; |
| 5137 | break; |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5138 | case MVT::v4i32: |
| 5139 | case MVT::v8i16: |
| 5140 | case MVT::v16i8: |
| 5141 | case MVT::v2f64: |
| 5142 | case MVT::v2i64: |
| Kit Barton | d4eb73c | 2015-05-05 16:10:44 +0000 | [diff] [blame] | 5143 | case MVT::v1i128: |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5144 | if (++NumVRsUsed <= NumVRs) |
| 5145 | continue; |
| 5146 | break; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 5147 | case MVT::v4f32: |
| NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 5148 | // When using QPX, this is handled like a FP register, otherwise, it |
| 5149 | // is an Altivec register. |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 5150 | if (Subtarget.hasQPX()) { |
| 5151 | if (++NumFPRsUsed <= NumFPRs) |
| 5152 | continue; |
| 5153 | } else { |
| 5154 | if (++NumVRsUsed <= NumVRs) |
| 5155 | continue; |
| 5156 | } |
| 5157 | break; |
| 5158 | case MVT::f32: |
| 5159 | case MVT::f64: |
| 5160 | case MVT::v4f64: // QPX |
| 5161 | case MVT::v4i1: // QPX |
| 5162 | if (++NumFPRsUsed <= NumFPRs) |
| 5163 | continue; |
| 5164 | break; |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5165 | } |
| 5166 | } |
| 5167 | |
| Ulrich Weigand | ec2bf93 | 2014-07-07 19:26:41 +0000 | [diff] [blame] | 5168 | /* Respect alignment of argument on the stack. */ |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 5169 | unsigned Align = |
| 5170 | CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize); |
| Ulrich Weigand | ec2bf93 | 2014-07-07 19:26:41 +0000 | [diff] [blame] | 5171 | NumBytes = ((NumBytes + Align - 1) / Align) * Align; |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 5172 | |
| 5173 | NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize); |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 5174 | if (Flags.isInConsecutiveRegsLast()) |
| 5175 | NumBytes = ((NumBytes + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 5176 | } |
| 5177 | |
| Ulrich Weigand | ec2bf93 | 2014-07-07 19:26:41 +0000 | [diff] [blame] | 5178 | unsigned NumBytesActuallyUsed = NumBytes; |
| 5179 | |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 5180 | // The prolog code of the callee may store up to 8 GPR argument registers to |
| 5181 | // the stack, allowing va_start to index over them in memory if its varargs. |
| 5182 | // Because we cannot tell if this is needed on the caller side, we have to |
| 5183 | // conservatively assume that it is needed. As such, make sure we have at |
| 5184 | // least enough stack space for the caller to store the 8 GPRs. |
| Ulrich Weigand | 8658f17 | 2014-07-20 23:43:15 +0000 | [diff] [blame] | 5185 | // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area. |
| Ulrich Weigand | 8ca988f | 2014-06-23 14:15:53 +0000 | [diff] [blame] | 5186 | NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize); |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 5187 | |
| 5188 | // Tail call needs the stack to be aligned. |
| 5189 | if (getTargetMachine().Options.GuaranteedTailCallOpt && |
| 5190 | CallConv == CallingConv::Fast) |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 5191 | NumBytes = EnsureStackAlignment(Subtarget.getFrameLowering(), NumBytes); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5192 | |
| Chuang-Yu Cheng | 2e5973e | 2016-04-06 02:04:38 +0000 | [diff] [blame] | 5193 | int SPDiff = 0; |
| 5194 | |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5195 | // Calculate by how many bytes the stack has to be adjusted in case of tail |
| 5196 | // call optimization. |
| Chuang-Yu Cheng | 2e5973e | 2016-04-06 02:04:38 +0000 | [diff] [blame] | 5197 | if (!IsSibCall) |
| 5198 | SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5199 | |
| 5200 | // To protect arguments on the stack from being clobbered in a tail call, |
| 5201 | // force all the loads to happen before doing any other lowering. |
| 5202 | if (isTailCall) |
| 5203 | Chain = DAG.getStackArgumentTokenFactor(Chain); |
| 5204 | |
| 5205 | // Adjust the stack pointer for the new arguments... |
| 5206 | // These operations are automatically eliminated by the prolog/epilog pass |
| Chuang-Yu Cheng | 2e5973e | 2016-04-06 02:04:38 +0000 | [diff] [blame] | 5207 | if (!IsSibCall) |
| 5208 | Chain = DAG.getCALLSEQ_START(Chain, |
| 5209 | DAG.getIntPtrConstant(NumBytes, dl, true), dl); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5210 | SDValue CallSeqStart = Chain; |
| 5211 | |
| 5212 | // Load the return address and frame pointer so it can be move somewhere else |
| 5213 | // later. |
| 5214 | SDValue LROp, FPOp; |
| Eric Christopher | e0d09ba | 2016-07-07 01:08:21 +0000 | [diff] [blame] | 5215 | Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, dl); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5216 | |
| 5217 | // Set up a copy of the stack pointer for use loading and storing any |
| 5218 | // arguments that may not fit in the registers available for argument |
| 5219 | // passing. |
| 5220 | SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64); |
| 5221 | |
| 5222 | // Figure out which arguments are going to go in registers, and which in |
| 5223 | // memory. Also, if this is a vararg function, floating point operations |
| 5224 | // must be stored to our stack, and loaded into integer regs as well, if |
| 5225 | // any integer regs are available for argument passing. |
| Ulrich Weigand | 8ca988f | 2014-06-23 14:15:53 +0000 | [diff] [blame] | 5226 | unsigned ArgOffset = LinkageSize; |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5227 | |
| 5228 | SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; |
| 5229 | SmallVector<TailCallArgumentInfo, 8> TailCallArguments; |
| 5230 | |
| 5231 | SmallVector<SDValue, 8> MemOpChains; |
| 5232 | for (unsigned i = 0; i != NumOps; ++i) { |
| 5233 | SDValue Arg = OutVals[i]; |
| 5234 | ISD::ArgFlagsTy Flags = Outs[i].Flags; |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 5235 | EVT ArgVT = Outs[i].VT; |
| 5236 | EVT OrigVT = Outs[i].ArgVT; |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5237 | |
| 5238 | // PtrOff will be used to store the current argument to the stack if a |
| 5239 | // register cannot be found for it. |
| 5240 | SDValue PtrOff; |
| 5241 | |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5242 | // We re-align the argument offset for each argument, except when using the |
| 5243 | // fast calling convention, when we need to make sure we do that only when |
| 5244 | // we'll actually use a stack slot. |
| 5245 | auto ComputePtrOff = [&]() { |
| 5246 | /* Respect alignment of argument on the stack. */ |
| 5247 | unsigned Align = |
| 5248 | CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize); |
| 5249 | ArgOffset = ((ArgOffset + Align - 1) / Align) * Align; |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5250 | |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5251 | PtrOff = DAG.getConstant(ArgOffset, dl, StackPtr.getValueType()); |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5252 | |
| 5253 | PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff); |
| 5254 | }; |
| 5255 | |
| 5256 | if (CallConv != CallingConv::Fast) { |
| 5257 | ComputePtrOff(); |
| 5258 | |
| 5259 | /* Compute GPR index associated with argument offset. */ |
| 5260 | GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize; |
| 5261 | GPR_idx = std::min(GPR_idx, NumGPRs); |
| 5262 | } |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5263 | |
| 5264 | // Promote integers to 64-bit values. |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5265 | if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) { |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5266 | // FIXME: Should this use ANY_EXTEND if neither sext nor zext? |
| 5267 | unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; |
| 5268 | Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg); |
| 5269 | } |
| 5270 | |
| 5271 | // FIXME memcpy is used way more than necessary. Correctness first. |
| 5272 | // Note: "by value" is code for passing a structure by value, not |
| 5273 | // basic types. |
| 5274 | if (Flags.isByVal()) { |
| 5275 | // Note: Size includes alignment padding, so |
| 5276 | // struct x { short a; char b; } |
| 5277 | // will have Size = 4. With #pragma pack(1), it will have Size = 3. |
| 5278 | // These are the proper values we need for right-justifying the |
| 5279 | // aggregate in a parameter register. |
| 5280 | unsigned Size = Flags.getByValSize(); |
| Bill Schmidt | 9953cf2 | 2012-10-31 01:15:05 +0000 | [diff] [blame] | 5281 | |
| 5282 | // An empty aggregate parameter takes up no storage and no |
| 5283 | // registers. |
| 5284 | if (Size == 0) |
| 5285 | continue; |
| 5286 | |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5287 | if (CallConv == CallingConv::Fast) |
| 5288 | ComputePtrOff(); |
| 5289 | |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5290 | // All aggregates smaller than 8 bytes must be passed right-justified. |
| 5291 | if (Size==1 || Size==2 || Size==4) { |
| 5292 | EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32); |
| 5293 | if (GPR_idx != NumGPRs) { |
| 5294 | SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg, |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5295 | MachinePointerInfo(), VT); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5296 | MemOpChains.push_back(Load.getValue(1)); |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5297 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5298 | |
| 5299 | ArgOffset += PtrByteSize; |
| 5300 | continue; |
| 5301 | } |
| 5302 | } |
| 5303 | |
| 5304 | if (GPR_idx == NumGPRs && Size < 8) { |
| Ulrich Weigand | 59c6ab2 | 2014-06-20 16:34:05 +0000 | [diff] [blame] | 5305 | SDValue AddPtr = PtrOff; |
| 5306 | if (!isLittleEndian) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5307 | SDValue Const = DAG.getConstant(PtrByteSize - Size, dl, |
| Ulrich Weigand | 59c6ab2 | 2014-06-20 16:34:05 +0000 | [diff] [blame] | 5308 | PtrOff.getValueType()); |
| 5309 | AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const); |
| 5310 | } |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5311 | Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr, |
| 5312 | CallSeqStart, |
| 5313 | Flags, DAG, dl); |
| 5314 | ArgOffset += PtrByteSize; |
| 5315 | continue; |
| 5316 | } |
| 5317 | // Copy entire object into memory. There are cases where gcc-generated |
| 5318 | // code assumes it is there, even if it could be put entirely into |
| 5319 | // registers. (This is not what the doc says.) |
| 5320 | |
| 5321 | // FIXME: The above statement is likely due to a misunderstanding of the |
| 5322 | // documents. All arguments must be copied into the parameter area BY |
| 5323 | // THE CALLEE in the event that the callee takes the address of any |
| 5324 | // formal argument. That has not yet been implemented. However, it is |
| 5325 | // reasonable to use the stack area as a staging area for the register |
| 5326 | // load. |
| 5327 | |
| 5328 | // Skip this for small aggregates, as we will use the same slot for a |
| 5329 | // right-justified copy, below. |
| 5330 | if (Size >= 8) |
| 5331 | Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff, |
| 5332 | CallSeqStart, |
| 5333 | Flags, DAG, dl); |
| 5334 | |
| 5335 | // When a register is available, pass a small aggregate right-justified. |
| 5336 | if (Size < 8 && GPR_idx != NumGPRs) { |
| 5337 | // The easiest way to get this right-justified in a register |
| 5338 | // is to copy the structure into the rightmost portion of a |
| 5339 | // local variable slot, then load the whole slot into the |
| 5340 | // register. |
| 5341 | // FIXME: The memcpy seems to produce pretty awful code for |
| 5342 | // small aggregates, particularly for packed ones. |
| Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 5343 | // FIXME: It would be preferable to use the slot in the |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5344 | // parameter save area instead of a new local variable. |
| Ulrich Weigand | 59c6ab2 | 2014-06-20 16:34:05 +0000 | [diff] [blame] | 5345 | SDValue AddPtr = PtrOff; |
| 5346 | if (!isLittleEndian) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5347 | SDValue Const = DAG.getConstant(8 - Size, dl, PtrOff.getValueType()); |
| Ulrich Weigand | 59c6ab2 | 2014-06-20 16:34:05 +0000 | [diff] [blame] | 5348 | AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const); |
| 5349 | } |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5350 | Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr, |
| 5351 | CallSeqStart, |
| 5352 | Flags, DAG, dl); |
| 5353 | |
| 5354 | // Load the slot into the register. |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5355 | SDValue Load = |
| 5356 | DAG.getLoad(PtrVT, dl, Chain, PtrOff, MachinePointerInfo()); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5357 | MemOpChains.push_back(Load.getValue(1)); |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5358 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5359 | |
| 5360 | // Done with this argument. |
| 5361 | ArgOffset += PtrByteSize; |
| 5362 | continue; |
| 5363 | } |
| 5364 | |
| 5365 | // For aggregates larger than PtrByteSize, copy the pieces of the |
| 5366 | // object that fit into registers from the parameter save area. |
| 5367 | for (unsigned j=0; j<Size; j+=PtrByteSize) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5368 | SDValue Const = DAG.getConstant(j, dl, PtrOff.getValueType()); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5369 | SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); |
| 5370 | if (GPR_idx != NumGPRs) { |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5371 | SDValue Load = |
| 5372 | DAG.getLoad(PtrVT, dl, Chain, AddArg, MachinePointerInfo()); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5373 | MemOpChains.push_back(Load.getValue(1)); |
| 5374 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
| 5375 | ArgOffset += PtrByteSize; |
| 5376 | } else { |
| 5377 | ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize; |
| 5378 | break; |
| 5379 | } |
| 5380 | } |
| 5381 | continue; |
| 5382 | } |
| 5383 | |
| Craig Topper | 5671010 | 2013-08-15 02:33:50 +0000 | [diff] [blame] | 5384 | switch (Arg.getSimpleValueType().SimpleTy) { |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5385 | default: llvm_unreachable("Unexpected ValueType for argument!"); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 5386 | case MVT::i1: |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5387 | case MVT::i32: |
| 5388 | case MVT::i64: |
| Hal Finkel | 965cea5 | 2015-07-12 00:37:44 +0000 | [diff] [blame] | 5389 | if (Flags.isNest()) { |
| 5390 | // The 'nest' parameter, if any, is passed in R11. |
| 5391 | RegsToPass.push_back(std::make_pair(PPC::X11, Arg)); |
| 5392 | hasNest = true; |
| 5393 | break; |
| 5394 | } |
| 5395 | |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 5396 | // These can be scalar arguments or elements of an integer array type |
| 5397 | // passed directly. Clang may use those instead of "byval" aggregate |
| 5398 | // types to avoid forcing arguments to memory unnecessarily. |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5399 | if (GPR_idx != NumGPRs) { |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5400 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg)); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5401 | } else { |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5402 | if (CallConv == CallingConv::Fast) |
| 5403 | ComputePtrOff(); |
| 5404 | |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5405 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 5406 | true, isTailCall, false, MemOpChains, |
| 5407 | TailCallArguments, dl); |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5408 | if (CallConv == CallingConv::Fast) |
| 5409 | ArgOffset += PtrByteSize; |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5410 | } |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5411 | if (CallConv != CallingConv::Fast) |
| 5412 | ArgOffset += PtrByteSize; |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5413 | break; |
| 5414 | case MVT::f32: |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 5415 | case MVT::f64: { |
| 5416 | // These can be scalar arguments or elements of a float array type |
| 5417 | // passed directly. The latter are used to implement ELFv2 homogenous |
| 5418 | // float aggregates. |
| 5419 | |
| 5420 | // Named arguments go into FPRs first, and once they overflow, the |
| 5421 | // remaining arguments go into GPRs and then the parameter save area. |
| 5422 | // Unnamed arguments for vararg functions always go to GPRs and |
| 5423 | // then the parameter save area. For now, put all arguments to vararg |
| 5424 | // routines always in both locations (FPR *and* GPR or stack slot). |
| 5425 | bool NeedGPROrStack = isVarArg || FPR_idx == NumFPRs; |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5426 | bool NeededLoad = false; |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 5427 | |
| 5428 | // First load the argument into the next available FPR. |
| 5429 | if (FPR_idx != NumFPRs) |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5430 | RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg)); |
| 5431 | |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 5432 | // Next, load the argument into GPR or stack slot if needed. |
| 5433 | if (!NeedGPROrStack) |
| 5434 | ; |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5435 | else if (GPR_idx != NumGPRs && CallConv != CallingConv::Fast) { |
| Hal Finkel | 8ea446b | 2015-01-18 14:31:10 +0000 | [diff] [blame] | 5436 | // FIXME: We may want to re-enable this for CallingConv::Fast on the P8 |
| 5437 | // once we support fp <-> gpr moves. |
| 5438 | |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 5439 | // In the non-vararg case, this can only ever happen in the |
| 5440 | // presence of f32 array types, since otherwise we never run |
| 5441 | // out of FPRs before running out of GPRs. |
| 5442 | SDValue ArgVal; |
| Bill Schmidt | bd4ac26 | 2012-10-29 21:18:16 +0000 | [diff] [blame] | 5443 | |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 5444 | // Double values are always passed in a single GPR. |
| 5445 | if (Arg.getValueType() != MVT::f32) { |
| 5446 | ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5447 | |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 5448 | // Non-array float values are extended and passed in a GPR. |
| 5449 | } else if (!Flags.isInConsecutiveRegs()) { |
| 5450 | ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg); |
| 5451 | ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal); |
| 5452 | |
| 5453 | // If we have an array of floats, we collect every odd element |
| 5454 | // together with its predecessor into one GPR. |
| 5455 | } else if (ArgOffset % PtrByteSize != 0) { |
| 5456 | SDValue Lo, Hi; |
| 5457 | Lo = DAG.getNode(ISD::BITCAST, dl, MVT::i32, OutVals[i - 1]); |
| 5458 | Hi = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg); |
| 5459 | if (!isLittleEndian) |
| 5460 | std::swap(Lo, Hi); |
| 5461 | ArgVal = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi); |
| 5462 | |
| 5463 | // The final element, if even, goes into the first half of a GPR. |
| 5464 | } else if (Flags.isInConsecutiveRegsLast()) { |
| 5465 | ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg); |
| 5466 | ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal); |
| 5467 | if (!isLittleEndian) |
| 5468 | ArgVal = DAG.getNode(ISD::SHL, dl, MVT::i64, ArgVal, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5469 | DAG.getConstant(32, dl, MVT::i32)); |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 5470 | |
| 5471 | // Non-final even elements are skipped; they will be handled |
| 5472 | // together the with subsequent argument on the next go-around. |
| 5473 | } else |
| 5474 | ArgVal = SDValue(); |
| 5475 | |
| 5476 | if (ArgVal.getNode()) |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5477 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], ArgVal)); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5478 | } else { |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5479 | if (CallConv == CallingConv::Fast) |
| 5480 | ComputePtrOff(); |
| 5481 | |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5482 | // Single-precision floating-point values are mapped to the |
| 5483 | // second (rightmost) word of the stack doubleword. |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 5484 | if (Arg.getValueType() == MVT::f32 && |
| 5485 | !isLittleEndian && !Flags.isInConsecutiveRegs()) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5486 | SDValue ConstFour = DAG.getConstant(4, dl, PtrOff.getValueType()); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5487 | PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour); |
| 5488 | } |
| 5489 | |
| 5490 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 5491 | true, isTailCall, false, MemOpChains, |
| 5492 | TailCallArguments, dl); |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5493 | |
| 5494 | NeededLoad = true; |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5495 | } |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 5496 | // When passing an array of floats, the array occupies consecutive |
| 5497 | // space in the argument area; only round up to the next doubleword |
| 5498 | // at the end of the array. Otherwise, each float takes 8 bytes. |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5499 | if (CallConv != CallingConv::Fast || NeededLoad) { |
| 5500 | ArgOffset += (Arg.getValueType() == MVT::f32 && |
| 5501 | Flags.isInConsecutiveRegs()) ? 4 : 8; |
| 5502 | if (Flags.isInConsecutiveRegsLast()) |
| 5503 | ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; |
| 5504 | } |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5505 | break; |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 5506 | } |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5507 | case MVT::v4f32: |
| 5508 | case MVT::v4i32: |
| 5509 | case MVT::v8i16: |
| 5510 | case MVT::v16i8: |
| Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 5511 | case MVT::v2f64: |
| Hal Finkel | a6c8b51 | 2014-03-26 16:12:58 +0000 | [diff] [blame] | 5512 | case MVT::v2i64: |
| Kit Barton | d4eb73c | 2015-05-05 16:10:44 +0000 | [diff] [blame] | 5513 | case MVT::v1i128: |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 5514 | if (!Subtarget.hasQPX()) { |
| Ulrich Weigand | 85d5df2 | 2014-07-21 00:13:26 +0000 | [diff] [blame] | 5515 | // These can be scalar arguments or elements of a vector array type |
| 5516 | // passed directly. The latter are used to implement ELFv2 homogenous |
| 5517 | // vector aggregates. |
| 5518 | |
| Ulrich Weigand | 9ba552d | 2014-06-23 12:36:34 +0000 | [diff] [blame] | 5519 | // For a varargs call, named arguments go into VRs or on the stack as |
| 5520 | // usual; unnamed arguments always go to the stack or the corresponding |
| 5521 | // GPRs when within range. For now, we always put the value in both |
| 5522 | // locations (or even all three). |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5523 | if (isVarArg) { |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5524 | // We could elide this store in the case where the object fits |
| 5525 | // entirely in R registers. Maybe later. |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5526 | SDValue Store = |
| 5527 | DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5528 | MemOpChains.push_back(Store); |
| 5529 | if (VR_idx != NumVRs) { |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5530 | SDValue Load = |
| 5531 | DAG.getLoad(MVT::v4f32, dl, Store, PtrOff, MachinePointerInfo()); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5532 | MemOpChains.push_back(Load.getValue(1)); |
| Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 5533 | RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load)); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5534 | } |
| 5535 | ArgOffset += 16; |
| 5536 | for (unsigned i=0; i<16; i+=PtrByteSize) { |
| 5537 | if (GPR_idx == NumGPRs) |
| 5538 | break; |
| 5539 | SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5540 | DAG.getConstant(i, dl, PtrVT)); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5541 | SDValue Load = |
| 5542 | DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo()); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5543 | MemOpChains.push_back(Load.getValue(1)); |
| 5544 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
| 5545 | } |
| 5546 | break; |
| 5547 | } |
| 5548 | |
| Ulrich Weigand | 9ba552d | 2014-06-23 12:36:34 +0000 | [diff] [blame] | 5549 | // Non-varargs Altivec params go into VRs or on the stack. |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5550 | if (VR_idx != NumVRs) { |
| Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 5551 | RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg)); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5552 | } else { |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5553 | if (CallConv == CallingConv::Fast) |
| 5554 | ComputePtrOff(); |
| 5555 | |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5556 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 5557 | true, isTailCall, true, MemOpChains, |
| 5558 | TailCallArguments, dl); |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5559 | if (CallConv == CallingConv::Fast) |
| 5560 | ArgOffset += 16; |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5561 | } |
| Hal Finkel | f81b6dd | 2015-01-18 12:08:47 +0000 | [diff] [blame] | 5562 | |
| 5563 | if (CallConv != CallingConv::Fast) |
| 5564 | ArgOffset += 16; |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5565 | break; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 5566 | } // not QPX |
| 5567 | |
| 5568 | assert(Arg.getValueType().getSimpleVT().SimpleTy == MVT::v4f32 && |
| 5569 | "Invalid QPX parameter type"); |
| 5570 | |
| 5571 | /* fall through */ |
| 5572 | case MVT::v4f64: |
| 5573 | case MVT::v4i1: { |
| 5574 | bool IsF32 = Arg.getValueType().getSimpleVT().SimpleTy == MVT::v4f32; |
| 5575 | if (isVarArg) { |
| 5576 | // We could elide this store in the case where the object fits |
| 5577 | // entirely in R registers. Maybe later. |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5578 | SDValue Store = |
| 5579 | DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 5580 | MemOpChains.push_back(Store); |
| 5581 | if (QFPR_idx != NumQFPRs) { |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5582 | SDValue Load = DAG.getLoad(IsF32 ? MVT::v4f32 : MVT::v4f64, dl, Store, |
| 5583 | PtrOff, MachinePointerInfo()); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 5584 | MemOpChains.push_back(Load.getValue(1)); |
| 5585 | RegsToPass.push_back(std::make_pair(QFPR[QFPR_idx++], Load)); |
| 5586 | } |
| 5587 | ArgOffset += (IsF32 ? 16 : 32); |
| Aaron Ballman | 70c27de | 2015-02-25 13:02:23 +0000 | [diff] [blame] | 5588 | for (unsigned i = 0; i < (IsF32 ? 16U : 32U); i += PtrByteSize) { |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 5589 | if (GPR_idx == NumGPRs) |
| 5590 | break; |
| 5591 | SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5592 | DAG.getConstant(i, dl, PtrVT)); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5593 | SDValue Load = |
| 5594 | DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo()); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 5595 | MemOpChains.push_back(Load.getValue(1)); |
| 5596 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
| 5597 | } |
| 5598 | break; |
| 5599 | } |
| 5600 | |
| 5601 | // Non-varargs QPX params go into registers or on the stack. |
| 5602 | if (QFPR_idx != NumQFPRs) { |
| 5603 | RegsToPass.push_back(std::make_pair(QFPR[QFPR_idx++], Arg)); |
| 5604 | } else { |
| 5605 | if (CallConv == CallingConv::Fast) |
| 5606 | ComputePtrOff(); |
| 5607 | |
| 5608 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 5609 | true, isTailCall, true, MemOpChains, |
| 5610 | TailCallArguments, dl); |
| 5611 | if (CallConv == CallingConv::Fast) |
| 5612 | ArgOffset += (IsF32 ? 16 : 32); |
| 5613 | } |
| 5614 | |
| 5615 | if (CallConv != CallingConv::Fast) |
| 5616 | ArgOffset += (IsF32 ? 16 : 32); |
| 5617 | break; |
| 5618 | } |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5619 | } |
| 5620 | } |
| 5621 | |
| Ulrich Weigand | ec2bf93 | 2014-07-07 19:26:41 +0000 | [diff] [blame] | 5622 | assert(NumBytesActuallyUsed == ArgOffset); |
| Ulrich Weigand | de8641b | 2014-07-07 19:39:44 +0000 | [diff] [blame] | 5623 | (void)NumBytesActuallyUsed; |
| Ulrich Weigand | ec2bf93 | 2014-07-07 19:26:41 +0000 | [diff] [blame] | 5624 | |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5625 | if (!MemOpChains.empty()) |
| Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 5626 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5627 | |
| 5628 | // Check if this is an indirect call (MTCTR/BCTRL). |
| 5629 | // See PrepareCall() for more information about calls through function |
| 5630 | // pointers in the 64-bit SVR4 ABI. |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 5631 | if (!isTailCall && !isPatchPoint && |
| Hal Finkel | 87deb0b | 2015-01-12 04:34:47 +0000 | [diff] [blame] | 5632 | !isFunctionGlobalAddress(Callee) && |
| 5633 | !isa<ExternalSymbolSDNode>(Callee)) { |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5634 | // Load r2 into a virtual register and store it to the TOC save area. |
| Hal Finkel | e6698d5 | 2015-02-01 15:03:28 +0000 | [diff] [blame] | 5635 | setUsesTOCBasePtr(DAG); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5636 | SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64); |
| 5637 | // TOC save area offset. |
| Eric Christopher | 736d39e | 2015-02-13 00:39:36 +0000 | [diff] [blame] | 5638 | unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset(); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5639 | SDValue PtrOff = DAG.getIntPtrConstant(TOCSaveOffset, dl); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5640 | SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff); |
| Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 5641 | Chain = DAG.getStore( |
| 5642 | Val.getValue(1), dl, Val, AddPtr, |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5643 | MachinePointerInfo::getStack(DAG.getMachineFunction(), TOCSaveOffset)); |
| Ulrich Weigand | aa0ac4f | 2014-07-20 23:31:44 +0000 | [diff] [blame] | 5644 | // In the ELFv2 ABI, R12 must contain the address of an indirect callee. |
| 5645 | // This does not mean the MTCTR instruction must use R12; it's easier |
| 5646 | // to model this as an extra parameter, so do that. |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 5647 | if (isELFv2ABI && !isPatchPoint) |
| Ulrich Weigand | aa0ac4f | 2014-07-20 23:31:44 +0000 | [diff] [blame] | 5648 | RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee)); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5649 | } |
| 5650 | |
| 5651 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 5652 | // and flag operands which copy the outgoing args into the appropriate regs. |
| 5653 | SDValue InFlag; |
| 5654 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 5655 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, |
| 5656 | RegsToPass[i].second, InFlag); |
| 5657 | InFlag = Chain.getValue(1); |
| 5658 | } |
| 5659 | |
| Chuang-Yu Cheng | 2e5973e | 2016-04-06 02:04:38 +0000 | [diff] [blame] | 5660 | if (isTailCall && !IsSibCall) |
| Eric Christopher | 327e440 | 2016-07-07 01:08:17 +0000 | [diff] [blame] | 5661 | PrepareTailCall(DAG, InFlag, Chain, dl, SPDiff, NumBytes, LROp, FPOp, |
| Eric Christopher | ade4eed | 2016-07-07 00:39:32 +0000 | [diff] [blame] | 5662 | TailCallArguments); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5663 | |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 5664 | return FinishCall(CallConv, dl, isTailCall, isVarArg, isPatchPoint, hasNest, |
| NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 5665 | DAG, RegsToPass, InFlag, Chain, CallSeqStart, Callee, |
| 5666 | SPDiff, NumBytes, Ins, InVals, CS); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5667 | } |
| 5668 | |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 5669 | SDValue PPCTargetLowering::LowerCall_Darwin( |
| 5670 | SDValue Chain, SDValue Callee, CallingConv::ID CallConv, bool isVarArg, |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 5671 | bool isTailCall, bool isPatchPoint, |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 5672 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 5673 | const SmallVectorImpl<SDValue> &OutVals, |
| 5674 | const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, |
| 5675 | SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, |
| 5676 | ImmutableCallSite *CS) const { |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5677 | |
| 5678 | unsigned NumOps = Outs.size(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5679 | |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 5680 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5681 | bool isPPC64 = PtrVT == MVT::i64; |
| Chris Lattner | ec78cad | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 5682 | unsigned PtrByteSize = isPPC64 ? 8 : 4; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5683 | |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 5684 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5685 | |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 5686 | // Mark this function as potentially containing a function that contains a |
| 5687 | // tail call. As a consequence the frame pointer will be used for dynamicalloc |
| 5688 | // and restoring the callers stack pointer in this functions epilog. This is |
| 5689 | // done because by tail calling the called function might overwrite the value |
| 5690 | // in this function's (MF) stack pointer stack slot 0(SP). |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 5691 | if (getTargetMachine().Options.GuaranteedTailCallOpt && |
| 5692 | CallConv == CallingConv::Fast) |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 5693 | MF.getInfo<PPCFunctionInfo>()->setHasFastCall(); |
| 5694 | |
| Chris Lattner | aa40ec1 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 5695 | // Count how many bytes are to be pushed on the stack, including the linkage |
| Chris Lattner | ec78cad | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 5696 | // area, and parameter passing area. We start with 24/48 bytes, which is |
| Chris Lattner | b7552a8 | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 5697 | // prereserved space for [SP][CR][LR][3 x unused]. |
| Eric Christopher | a4ae213 | 2015-02-13 22:22:57 +0000 | [diff] [blame] | 5698 | unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize(); |
| Ulrich Weigand | 8ca988f | 2014-06-23 14:15:53 +0000 | [diff] [blame] | 5699 | unsigned NumBytes = LinkageSize; |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 5700 | |
| 5701 | // Add up all the space actually used. |
| 5702 | // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually |
| 5703 | // they all go in registers, but we must reserve stack space for them for |
| 5704 | // possible use by the caller. In varargs or 64-bit calls, parameters are |
| 5705 | // assigned stack space in order, with padding so Altivec parameters are |
| 5706 | // 16-byte aligned. |
| 5707 | unsigned nAltivecParamsAtEnd = 0; |
| 5708 | for (unsigned i = 0; i != NumOps; ++i) { |
| 5709 | ISD::ArgFlagsTy Flags = Outs[i].Flags; |
| 5710 | EVT ArgVT = Outs[i].VT; |
| 5711 | // Varargs Altivec parameters are padded to a 16 byte boundary. |
| 5712 | if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 || |
| 5713 | ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 || |
| 5714 | ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64) { |
| 5715 | if (!isVarArg && !isPPC64) { |
| 5716 | // Non-varargs Altivec parameters go after all the non-Altivec |
| 5717 | // parameters; handle those later so we know how much padding we need. |
| 5718 | nAltivecParamsAtEnd++; |
| 5719 | continue; |
| 5720 | } |
| 5721 | // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary. |
| 5722 | NumBytes = ((NumBytes+15)/16)*16; |
| 5723 | } |
| 5724 | NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize); |
| 5725 | } |
| 5726 | |
| 5727 | // Allow for Altivec parameters at the end, if needed. |
| 5728 | if (nAltivecParamsAtEnd) { |
| 5729 | NumBytes = ((NumBytes+15)/16)*16; |
| 5730 | NumBytes += 16*nAltivecParamsAtEnd; |
| 5731 | } |
| 5732 | |
| 5733 | // The prolog code of the callee may store up to 8 GPR argument registers to |
| 5734 | // the stack, allowing va_start to index over them in memory if its varargs. |
| 5735 | // Because we cannot tell if this is needed on the caller side, we have to |
| 5736 | // conservatively assume that it is needed. As such, make sure we have at |
| 5737 | // least enough stack space for the caller to store the 8 GPRs. |
| Ulrich Weigand | 8ca988f | 2014-06-23 14:15:53 +0000 | [diff] [blame] | 5738 | NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize); |
| Ulrich Weigand | 2bffb95 | 2014-06-23 13:08:27 +0000 | [diff] [blame] | 5739 | |
| 5740 | // Tail call needs the stack to be aligned. |
| 5741 | if (getTargetMachine().Options.GuaranteedTailCallOpt && |
| 5742 | CallConv == CallingConv::Fast) |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 5743 | NumBytes = EnsureStackAlignment(Subtarget.getFrameLowering(), NumBytes); |
| Dale Johannesen | b28456e | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 5744 | |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 5745 | // Calculate by how many bytes the stack has to be adjusted in case of tail |
| 5746 | // call optimization. |
| 5747 | int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5748 | |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5749 | // To protect arguments on the stack from being clobbered in a tail call, |
| 5750 | // force all the loads to happen before doing any other lowering. |
| 5751 | if (isTailCall) |
| 5752 | Chain = DAG.getStackArgumentTokenFactor(Chain); |
| 5753 | |
| Chris Lattner | b7552a8 | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 5754 | // Adjust the stack pointer for the new arguments... |
| 5755 | // These operations are automatically eliminated by the prolog/epilog pass |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5756 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, dl, true), |
| Andrew Trick | ad6d08a | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 5757 | dl); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5758 | SDValue CallSeqStart = Chain; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5759 | |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 5760 | // Load the return address and frame pointer so it can be move somewhere else |
| 5761 | // later. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5762 | SDValue LROp, FPOp; |
| Eric Christopher | e0d09ba | 2016-07-07 01:08:21 +0000 | [diff] [blame] | 5763 | Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, dl); |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 5764 | |
| Chris Lattner | b7552a8 | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 5765 | // Set up a copy of the stack pointer for use loading and storing any |
| 5766 | // arguments that may not fit in the registers available for argument |
| 5767 | // passing. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5768 | SDValue StackPtr; |
| Chris Lattner | ec78cad | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 5769 | if (isPPC64) |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5770 | StackPtr = DAG.getRegister(PPC::X1, MVT::i64); |
| Chris Lattner | ec78cad | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 5771 | else |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5772 | StackPtr = DAG.getRegister(PPC::R1, MVT::i32); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5773 | |
| Chris Lattner | b7552a8 | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 5774 | // Figure out which arguments are going to go in registers, and which in |
| 5775 | // memory. Also, if this is a vararg function, floating point operations |
| 5776 | // must be stored to our stack, and loaded into integer regs as well, if |
| 5777 | // any integer regs are available for argument passing. |
| Ulrich Weigand | 8ca988f | 2014-06-23 14:15:53 +0000 | [diff] [blame] | 5778 | unsigned ArgOffset = LinkageSize; |
| Chris Lattner | b1e9e37 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 5779 | unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5780 | |
| Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 5781 | static const MCPhysReg GPR_32[] = { // 32-bit registers. |
| Chris Lattner | b1e9e37 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 5782 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 5783 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 5784 | }; |
| Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 5785 | static const MCPhysReg GPR_64[] = { // 64-bit registers. |
| Chris Lattner | ec78cad | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 5786 | PPC::X3, PPC::X4, PPC::X5, PPC::X6, |
| 5787 | PPC::X7, PPC::X8, PPC::X9, PPC::X10, |
| 5788 | }; |
| Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 5789 | static const MCPhysReg VR[] = { |
| Chris Lattner | b1e9e37 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 5790 | PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, |
| 5791 | PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 |
| 5792 | }; |
| Owen Anderson | e2f23a3 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 5793 | const unsigned NumGPRs = array_lengthof(GPR_32); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 5794 | const unsigned NumFPRs = 13; |
| Tilmann Scheller | 98bdaaa | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 5795 | const unsigned NumVRs = array_lengthof(VR); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5796 | |
| Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 5797 | const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32; |
| Chris Lattner | ec78cad | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 5798 | |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 5799 | SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 5800 | SmallVector<TailCallArgumentInfo, 8> TailCallArguments; |
| 5801 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5802 | SmallVector<SDValue, 8> MemOpChains; |
| Evan Cheng | c2cd473 | 2006-05-25 00:57:32 +0000 | [diff] [blame] | 5803 | for (unsigned i = 0; i != NumOps; ++i) { |
| Dan Gohman | fe7532a | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 5804 | SDValue Arg = OutVals[i]; |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5805 | ISD::ArgFlagsTy Flags = Outs[i].Flags; |
| Nicolas Geoffray | 7aad928 | 2007-03-13 15:02:46 +0000 | [diff] [blame] | 5806 | |
| Chris Lattner | b7552a8 | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 5807 | // PtrOff will be used to store the current argument to the stack if a |
| 5808 | // register cannot be found for it. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5809 | SDValue PtrOff; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5810 | |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5811 | PtrOff = DAG.getConstant(ArgOffset, dl, StackPtr.getValueType()); |
| Nicolas Geoffray | 7aad928 | 2007-03-13 15:02:46 +0000 | [diff] [blame] | 5812 | |
| Dale Johannesen | 679073b | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 5813 | PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff); |
| Chris Lattner | ec78cad | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 5814 | |
| 5815 | // On PPC64, promote integers to 64-bit values. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5816 | if (isPPC64 && Arg.getValueType() == MVT::i32) { |
| Duncan Sands | d97eea3 | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 5817 | // FIXME: Should this use ANY_EXTEND if neither sext nor zext? |
| 5818 | unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5819 | Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg); |
| Chris Lattner | ec78cad | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 5820 | } |
| Dale Johannesen | 85d41a1 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 5821 | |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 5822 | // FIXME memcpy is used way more than necessary. Correctness first. |
| Bill Schmidt | 019cc6f | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 5823 | // Note: "by value" is code for passing a structure by value, not |
| 5824 | // basic types. |
| Duncan Sands | d97eea3 | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 5825 | if (Flags.isByVal()) { |
| 5826 | unsigned Size = Flags.getByValSize(); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5827 | // Very small objects are passed right-justified. Everything else is |
| 5828 | // passed left-justified. |
| 5829 | if (Size==1 || Size==2) { |
| 5830 | EVT VT = (Size==1) ? MVT::i8 : MVT::i16; |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 5831 | if (GPR_idx != NumGPRs) { |
| Stuart Hastings | 81c4306 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 5832 | SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg, |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5833 | MachinePointerInfo(), VT); |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 5834 | MemOpChains.push_back(Load.getValue(1)); |
| 5835 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 5836 | |
| 5837 | ArgOffset += PtrByteSize; |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 5838 | } else { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5839 | SDValue Const = DAG.getConstant(PtrByteSize - Size, dl, |
| Bill Schmidt | 48081ca | 2012-10-16 13:30:53 +0000 | [diff] [blame] | 5840 | PtrOff.getValueType()); |
| Dale Johannesen | 679073b | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 5841 | SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const); |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5842 | Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr, |
| 5843 | CallSeqStart, |
| 5844 | Flags, DAG, dl); |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 5845 | ArgOffset += PtrByteSize; |
| 5846 | } |
| 5847 | continue; |
| 5848 | } |
| Dale Johannesen | 92dcf1e | 2008-03-17 02:13:43 +0000 | [diff] [blame] | 5849 | // Copy entire object into memory. There are cases where gcc-generated |
| 5850 | // code assumes it is there, even if it could be put entirely into |
| 5851 | // registers. (This is not what the doc says.) |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5852 | Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff, |
| 5853 | CallSeqStart, |
| 5854 | Flags, DAG, dl); |
| Bill Schmidt | 019cc6f | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 5855 | |
| 5856 | // For small aggregates (Darwin only) and aggregates >= PtrByteSize, |
| 5857 | // copy the pieces of the object that fit into registers from the |
| 5858 | // parameter save area. |
| Dale Johannesen | 85d41a1 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 5859 | for (unsigned j=0; j<Size; j+=PtrByteSize) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5860 | SDValue Const = DAG.getConstant(j, dl, PtrOff.getValueType()); |
| Dale Johannesen | 679073b | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 5861 | SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); |
| Dale Johannesen | 85d41a1 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 5862 | if (GPR_idx != NumGPRs) { |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5863 | SDValue Load = |
| 5864 | DAG.getLoad(PtrVT, dl, Chain, AddArg, MachinePointerInfo()); |
| Dale Johannesen | 0d23505 | 2008-03-05 23:31:27 +0000 | [diff] [blame] | 5865 | MemOpChains.push_back(Load.getValue(1)); |
| Dale Johannesen | 85d41a1 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 5866 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 5867 | ArgOffset += PtrByteSize; |
| Dale Johannesen | 85d41a1 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 5868 | } else { |
| Dale Johannesen | 92dcf1e | 2008-03-17 02:13:43 +0000 | [diff] [blame] | 5869 | ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize; |
| Dale Johannesen | bfa252d | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 5870 | break; |
| Dale Johannesen | 85d41a1 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 5871 | } |
| 5872 | } |
| 5873 | continue; |
| 5874 | } |
| 5875 | |
| Craig Topper | 5671010 | 2013-08-15 02:33:50 +0000 | [diff] [blame] | 5876 | switch (Arg.getSimpleValueType().SimpleTy) { |
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5877 | default: llvm_unreachable("Unexpected ValueType for argument!"); |
| Hal Finkel | 5cae216 | 2014-02-28 01:17:25 +0000 | [diff] [blame] | 5878 | case MVT::i1: |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5879 | case MVT::i32: |
| 5880 | case MVT::i64: |
| Chris Lattner | b1e9e37 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 5881 | if (GPR_idx != NumGPRs) { |
| Hal Finkel | 7f908e8 | 2014-03-06 00:45:19 +0000 | [diff] [blame] | 5882 | if (Arg.getValueType() == MVT::i1) |
| 5883 | Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, PtrVT, Arg); |
| 5884 | |
| Chris Lattner | b1e9e37 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 5885 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg)); |
| Chris Lattner | b7552a8 | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 5886 | } else { |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 5887 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 5888 | isPPC64, isTailCall, false, MemOpChains, |
| Dale Johannesen | 021052a | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 5889 | TailCallArguments, dl); |
| Chris Lattner | b7552a8 | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 5890 | } |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 5891 | ArgOffset += PtrByteSize; |
| Chris Lattner | b7552a8 | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 5892 | break; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5893 | case MVT::f32: |
| 5894 | case MVT::f64: |
| Chris Lattner | b1e9e37 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 5895 | if (FPR_idx != NumFPRs) { |
| 5896 | RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg)); |
| 5897 | |
| Chris Lattner | b7552a8 | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 5898 | if (isVarArg) { |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5899 | SDValue Store = |
| 5900 | DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()); |
| Chris Lattner | b1e9e37 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 5901 | MemOpChains.push_back(Store); |
| 5902 | |
| Chris Lattner | b7552a8 | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 5903 | // Float varargs are always shadowed in available integer registers |
| Chris Lattner | b1e9e37 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 5904 | if (GPR_idx != NumGPRs) { |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5905 | SDValue Load = |
| 5906 | DAG.getLoad(PtrVT, dl, Store, PtrOff, MachinePointerInfo()); |
| Chris Lattner | b1e9e37 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 5907 | MemOpChains.push_back(Load.getValue(1)); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 5908 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
| Chris Lattner | b7552a8 | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 5909 | } |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5910 | if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){ |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5911 | SDValue ConstFour = DAG.getConstant(4, dl, PtrOff.getValueType()); |
| Dale Johannesen | 679073b | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 5912 | PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5913 | SDValue Load = |
| 5914 | DAG.getLoad(PtrVT, dl, Store, PtrOff, MachinePointerInfo()); |
| Chris Lattner | b1e9e37 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 5915 | MemOpChains.push_back(Load.getValue(1)); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 5916 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
| Chris Lattner | aa40ec1 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 5917 | } |
| 5918 | } else { |
| Chris Lattner | b7552a8 | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 5919 | // If we have any FPRs remaining, we may also have GPRs remaining. |
| 5920 | // Args passed in FPRs consume either 1 (f32) or 2 (f64) available |
| 5921 | // GPRs. |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 5922 | if (GPR_idx != NumGPRs) |
| 5923 | ++GPR_idx; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5924 | if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 5925 | !isPPC64) // PPC64 has 64-bit GPR's obviously :) |
| 5926 | ++GPR_idx; |
| Chris Lattner | aa40ec1 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 5927 | } |
| Bill Schmidt | 57d6de5 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 5928 | } else |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 5929 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 5930 | isPPC64, isTailCall, false, MemOpChains, |
| Dale Johannesen | 021052a | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 5931 | TailCallArguments, dl); |
| Tilmann Scheller | 773f14c | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 5932 | if (isPPC64) |
| 5933 | ArgOffset += 8; |
| 5934 | else |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5935 | ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8; |
| Chris Lattner | b7552a8 | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 5936 | break; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5937 | case MVT::v4f32: |
| 5938 | case MVT::v4i32: |
| 5939 | case MVT::v8i16: |
| 5940 | case MVT::v16i8: |
| Dale Johannesen | b28456e | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 5941 | if (isVarArg) { |
| 5942 | // These go aligned on the stack, or in the corresponding R registers |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5943 | // when within range. The Darwin PPC ABI doc claims they also go in |
| Dale Johannesen | b28456e | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 5944 | // V registers; in fact gcc does this only for arguments that are |
| 5945 | // prototyped, not for those that match the ... We do it for all |
| 5946 | // arguments, seems to work. |
| 5947 | while (ArgOffset % 16 !=0) { |
| 5948 | ArgOffset += PtrByteSize; |
| 5949 | if (GPR_idx != NumGPRs) |
| 5950 | GPR_idx++; |
| 5951 | } |
| 5952 | // We could elide this store in the case where the object fits |
| 5953 | // entirely in R registers. Maybe later. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5954 | PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5955 | DAG.getConstant(ArgOffset, dl, PtrVT)); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5956 | SDValue Store = |
| 5957 | DAG.getStore(Chain, dl, Arg, PtrOff, MachinePointerInfo()); |
| Dale Johannesen | b28456e | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 5958 | MemOpChains.push_back(Store); |
| 5959 | if (VR_idx != NumVRs) { |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5960 | SDValue Load = |
| 5961 | DAG.getLoad(MVT::v4f32, dl, Store, PtrOff, MachinePointerInfo()); |
| Dale Johannesen | b28456e | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 5962 | MemOpChains.push_back(Load.getValue(1)); |
| 5963 | RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load)); |
| 5964 | } |
| 5965 | ArgOffset += 16; |
| 5966 | for (unsigned i=0; i<16; i+=PtrByteSize) { |
| 5967 | if (GPR_idx == NumGPRs) |
| 5968 | break; |
| Dale Johannesen | 679073b | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 5969 | SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 5970 | DAG.getConstant(i, dl, PtrVT)); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 5971 | SDValue Load = |
| 5972 | DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo()); |
| Dale Johannesen | b28456e | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 5973 | MemOpChains.push_back(Load.getValue(1)); |
| 5974 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
| 5975 | } |
| 5976 | break; |
| 5977 | } |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 5978 | |
| Dale Johannesen | 0dfd3f3 | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 5979 | // Non-varargs Altivec params generally go in registers, but have |
| 5980 | // stack space allocated at the end. |
| 5981 | if (VR_idx != NumVRs) { |
| 5982 | // Doesn't have GPR space allocated. |
| 5983 | RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg)); |
| 5984 | } else if (nAltivecParamsAtEnd==0) { |
| 5985 | // We are emitting Altivec params in order. |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 5986 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 5987 | isPPC64, isTailCall, true, MemOpChains, |
| Dale Johannesen | 021052a | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 5988 | TailCallArguments, dl); |
| Dale Johannesen | b28456e | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 5989 | ArgOffset += 16; |
| Dale Johannesen | b28456e | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 5990 | } |
| Chris Lattner | b7552a8 | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 5991 | break; |
| Chris Lattner | aa40ec1 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 5992 | } |
| Chris Lattner | aa40ec1 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 5993 | } |
| Dale Johannesen | 0dfd3f3 | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 5994 | // If all Altivec parameters fit in registers, as they usually do, |
| 5995 | // they get stack space following the non-Altivec parameters. We |
| 5996 | // don't track this here because nobody below needs it. |
| 5997 | // If there are more Altivec parameters than fit in registers emit |
| 5998 | // the stores here. |
| 5999 | if (!isVarArg && nAltivecParamsAtEnd > NumVRs) { |
| 6000 | unsigned j = 0; |
| 6001 | // Offset is aligned; skip 1st 12 params which go in V registers. |
| 6002 | ArgOffset = ((ArgOffset+15)/16)*16; |
| 6003 | ArgOffset += 12*16; |
| 6004 | for (unsigned i = 0; i != NumOps; ++i) { |
| Dan Gohman | fe7532a | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 6005 | SDValue Arg = OutVals[i]; |
| 6006 | EVT ArgType = Outs[i].VT; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6007 | if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 || |
| 6008 | ArgType==MVT::v8i16 || ArgType==MVT::v16i8) { |
| Dale Johannesen | 0dfd3f3 | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 6009 | if (++j > NumVRs) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6010 | SDValue PtrOff; |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 6011 | // We are emitting Altivec params in order. |
| 6012 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 6013 | isPPC64, isTailCall, true, MemOpChains, |
| Dale Johannesen | 021052a | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 6014 | TailCallArguments, dl); |
| Dale Johannesen | 0dfd3f3 | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 6015 | ArgOffset += 16; |
| 6016 | } |
| 6017 | } |
| 6018 | } |
| 6019 | } |
| 6020 | |
| Chris Lattner | b1e9e37 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 6021 | if (!MemOpChains.empty()) |
| Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 6022 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6023 | |
| Dale Johannesen | 90eab67 | 2010-03-09 20:15:42 +0000 | [diff] [blame] | 6024 | // On Darwin, R12 must contain the address of an indirect callee. This does |
| 6025 | // not mean the MTCTR instruction must use R12; it's easier to model this as |
| 6026 | // an extra parameter, so do that. |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6027 | if (!isTailCall && |
| Hal Finkel | 87deb0b | 2015-01-12 04:34:47 +0000 | [diff] [blame] | 6028 | !isFunctionGlobalAddress(Callee) && |
| 6029 | !isa<ExternalSymbolSDNode>(Callee) && |
| Dale Johannesen | 90eab67 | 2010-03-09 20:15:42 +0000 | [diff] [blame] | 6030 | !isBLACompatibleAddress(Callee, DAG)) |
| 6031 | RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 : |
| 6032 | PPC::R12), Callee)); |
| 6033 | |
| Chris Lattner | b1e9e37 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 6034 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 6035 | // and flag operands which copy the outgoing args into the appropriate regs. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6036 | SDValue InFlag; |
| Chris Lattner | b1e9e37 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 6037 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6038 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, |
| Dale Johannesen | 679073b | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 6039 | RegsToPass[i].second, InFlag); |
| Chris Lattner | b1e9e37 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 6040 | InFlag = Chain.getValue(1); |
| 6041 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6042 | |
| Chris Lattner | df8e17d | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 6043 | if (isTailCall) |
| Eric Christopher | 327e440 | 2016-07-07 01:08:17 +0000 | [diff] [blame] | 6044 | PrepareTailCall(DAG, InFlag, Chain, dl, SPDiff, NumBytes, LROp, FPOp, |
| Eric Christopher | ade4eed | 2016-07-07 00:39:32 +0000 | [diff] [blame] | 6045 | TailCallArguments); |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 6046 | |
| Eric Christopher | 2454a3b | 2016-07-07 01:08:23 +0000 | [diff] [blame] | 6047 | return FinishCall(CallConv, dl, isTailCall, isVarArg, isPatchPoint, |
| Hal Finkel | 965cea5 | 2015-07-12 00:37:44 +0000 | [diff] [blame] | 6048 | /* unused except on PPC64 ELFv1 */ false, DAG, |
| Hal Finkel | e2ab0f1 | 2015-01-15 21:17:34 +0000 | [diff] [blame] | 6049 | RegsToPass, InFlag, Chain, CallSeqStart, Callee, SPDiff, |
| 6050 | NumBytes, Ins, InVals, CS); |
| Chris Lattner | aa40ec1 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 6051 | } |
| 6052 | |
| Hal Finkel | 450128a | 2011-10-14 19:51:36 +0000 | [diff] [blame] | 6053 | bool |
| 6054 | PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv, |
| 6055 | MachineFunction &MF, bool isVarArg, |
| 6056 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 6057 | LLVMContext &Context) const { |
| 6058 | SmallVector<CCValAssign, 16> RVLocs; |
| Eric Christopher | b521750 | 2014-08-06 18:45:26 +0000 | [diff] [blame] | 6059 | CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context); |
| Hal Finkel | 450128a | 2011-10-14 19:51:36 +0000 | [diff] [blame] | 6060 | return CCInfo.CheckReturn(Outs, RetCC_PPC); |
| 6061 | } |
| 6062 | |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6063 | SDValue |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 6064 | PPCTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, |
| 6065 | bool isVarArg, |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6066 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| Dan Gohman | fe7532a | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 6067 | const SmallVectorImpl<SDValue> &OutVals, |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 6068 | const SDLoc &dl, SelectionDAG &DAG) const { |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6069 | |
| Chris Lattner | 4f2e4e0 | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 6070 | SmallVector<CCValAssign, 16> RVLocs; |
| Eric Christopher | b521750 | 2014-08-06 18:45:26 +0000 | [diff] [blame] | 6071 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, |
| 6072 | *DAG.getContext()); |
| Dan Gohman | f9bbcd1 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 6073 | CCInfo.AnalyzeReturn(Outs, RetCC_PPC); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6074 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6075 | SDValue Flag; |
| Jakob Stoklund Olesen | 8660a8c | 2013-02-05 18:12:00 +0000 | [diff] [blame] | 6076 | SmallVector<SDValue, 4> RetOps(1, Chain); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6077 | |
| Chris Lattner | 4f2e4e0 | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 6078 | // Copy the result values into the output registers. |
| 6079 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 6080 | CCValAssign &VA = RVLocs[i]; |
| 6081 | assert(VA.isRegLoc() && "Can only return in registers!"); |
| Ulrich Weigand | 339d059 | 2012-11-05 19:39:45 +0000 | [diff] [blame] | 6082 | |
| 6083 | SDValue Arg = OutVals[i]; |
| 6084 | |
| 6085 | switch (VA.getLocInfo()) { |
| 6086 | default: llvm_unreachable("Unknown loc info!"); |
| 6087 | case CCValAssign::Full: break; |
| 6088 | case CCValAssign::AExt: |
| 6089 | Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); |
| 6090 | break; |
| 6091 | case CCValAssign::ZExt: |
| 6092 | Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); |
| 6093 | break; |
| 6094 | case CCValAssign::SExt: |
| 6095 | Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); |
| 6096 | break; |
| 6097 | } |
| 6098 | |
| 6099 | Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag); |
| Chris Lattner | 4f2e4e0 | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 6100 | Flag = Chain.getValue(1); |
| Jakob Stoklund Olesen | 8660a8c | 2013-02-05 18:12:00 +0000 | [diff] [blame] | 6101 | RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); |
| Chris Lattner | 4f2e4e0 | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 6102 | } |
| 6103 | |
| Chuang-Yu Cheng | 98c1894 | 2016-04-08 12:04:32 +0000 | [diff] [blame] | 6104 | const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo(); |
| 6105 | const MCPhysReg *I = |
| 6106 | TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction()); |
| 6107 | if (I) { |
| 6108 | for (; *I; ++I) { |
| 6109 | |
| 6110 | if (PPC::G8RCRegClass.contains(*I)) |
| 6111 | RetOps.push_back(DAG.getRegister(*I, MVT::i64)); |
| 6112 | else if (PPC::F8RCRegClass.contains(*I)) |
| 6113 | RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64))); |
| 6114 | else if (PPC::CRRCRegClass.contains(*I)) |
| 6115 | RetOps.push_back(DAG.getRegister(*I, MVT::i1)); |
| 6116 | else if (PPC::VRRCRegClass.contains(*I)) |
| 6117 | RetOps.push_back(DAG.getRegister(*I, MVT::Other)); |
| 6118 | else |
| 6119 | llvm_unreachable("Unexpected register class in CSRsViaCopy!"); |
| 6120 | } |
| 6121 | } |
| 6122 | |
| Jakob Stoklund Olesen | 8660a8c | 2013-02-05 18:12:00 +0000 | [diff] [blame] | 6123 | RetOps[0] = Chain; // Update chain. |
| 6124 | |
| 6125 | // Add the flag if we have it. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6126 | if (Flag.getNode()) |
| Jakob Stoklund Olesen | 8660a8c | 2013-02-05 18:12:00 +0000 | [diff] [blame] | 6127 | RetOps.push_back(Flag); |
| 6128 | |
| Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 6129 | return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, RetOps); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6130 | } |
| 6131 | |
| Eric Christopher | b976a39 | 2016-07-07 00:39:27 +0000 | [diff] [blame] | 6132 | SDValue |
| 6133 | PPCTargetLowering::LowerGET_DYNAMIC_AREA_OFFSET(SDValue Op, |
| 6134 | SelectionDAG &DAG) const { |
| Yury Gribov | d7dbb66 | 2015-12-01 11:40:55 +0000 | [diff] [blame] | 6135 | SDLoc dl(Op); |
| 6136 | |
| 6137 | // Get the corect type for integers. |
| 6138 | EVT IntVT = Op.getValueType(); |
| 6139 | |
| 6140 | // Get the inputs. |
| 6141 | SDValue Chain = Op.getOperand(0); |
| 6142 | SDValue FPSIdx = getFramePointerFrameIndex(DAG); |
| 6143 | // Build a DYNAREAOFFSET node. |
| 6144 | SDValue Ops[2] = {Chain, FPSIdx}; |
| 6145 | SDVTList VTs = DAG.getVTList(IntVT); |
| 6146 | return DAG.getNode(PPCISD::DYNAREAOFFSET, dl, VTs, Ops); |
| 6147 | } |
| 6148 | |
| Eric Christopher | b976a39 | 2016-07-07 00:39:27 +0000 | [diff] [blame] | 6149 | SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, |
| 6150 | SelectionDAG &DAG) const { |
| Jim Laskey | e4f4d04 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 6151 | // When we pop the dynamic allocation we need to restore the SP link. |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6152 | SDLoc dl(Op); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6153 | |
| Jim Laskey | e4f4d04 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 6154 | // Get the corect type for pointers. |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 6155 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
| Jim Laskey | e4f4d04 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 6156 | |
| 6157 | // Construct the stack pointer operand. |
| Dale Johannesen | 86dcae1 | 2009-11-24 01:09:07 +0000 | [diff] [blame] | 6158 | bool isPPC64 = Subtarget.isPPC64(); |
| 6159 | unsigned SP = isPPC64 ? PPC::X1 : PPC::R1; |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6160 | SDValue StackPtr = DAG.getRegister(SP, PtrVT); |
| Jim Laskey | e4f4d04 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 6161 | |
| 6162 | // Get the operands for the STACKRESTORE. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6163 | SDValue Chain = Op.getOperand(0); |
| 6164 | SDValue SaveSP = Op.getOperand(1); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6165 | |
| Jim Laskey | e4f4d04 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 6166 | // Load the old link SP. |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 6167 | SDValue LoadLinkSP = |
| 6168 | DAG.getLoad(PtrVT, dl, Chain, StackPtr, MachinePointerInfo()); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6169 | |
| Jim Laskey | e4f4d04 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 6170 | // Restore the stack pointer. |
| Dale Johannesen | 021052a | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 6171 | Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6172 | |
| Jim Laskey | e4f4d04 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 6173 | // Store the old link SP. |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 6174 | return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo()); |
| Jim Laskey | e4f4d04 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 6175 | } |
| 6176 | |
| NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 6177 | SDValue PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG &DAG) const { |
| Jim Laskey | 48850c1 | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 6178 | MachineFunction &MF = DAG.getMachineFunction(); |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 6179 | bool isPPC64 = Subtarget.isPPC64(); |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 6180 | EVT PtrVT = getPointerTy(MF.getDataLayout()); |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 6181 | |
| 6182 | // Get current frame pointer save index. The users of this index will be |
| 6183 | // primarily DYNALLOC instructions. |
| 6184 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 6185 | int RASI = FI->getReturnAddrSaveIndex(); |
| 6186 | |
| 6187 | // If the frame pointer save index hasn't been defined yet. |
| 6188 | if (!RASI) { |
| 6189 | // Find out what the fix offset of the frame pointer save area. |
| Eric Christopher | f71609b | 2015-02-13 00:39:27 +0000 | [diff] [blame] | 6190 | int LROffset = Subtarget.getFrameLowering()->getReturnSaveOffset(); |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 6191 | // Allocate the frame index for frame pointer save area. |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 6192 | RASI = MF.getFrameInfo().CreateFixedObject(isPPC64? 8 : 4, LROffset, false); |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 6193 | // Save the result. |
| 6194 | FI->setReturnAddrSaveIndex(RASI); |
| 6195 | } |
| 6196 | return DAG.getFrameIndex(RASI, PtrVT); |
| 6197 | } |
| 6198 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6199 | SDValue |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 6200 | PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const { |
| 6201 | MachineFunction &MF = DAG.getMachineFunction(); |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 6202 | bool isPPC64 = Subtarget.isPPC64(); |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 6203 | EVT PtrVT = getPointerTy(MF.getDataLayout()); |
| Jim Laskey | 48850c1 | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 6204 | |
| 6205 | // Get current frame pointer save index. The users of this index will be |
| 6206 | // primarily DYNALLOC instructions. |
| 6207 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 6208 | int FPSI = FI->getFramePointerSaveIndex(); |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 6209 | |
| Jim Laskey | 48850c1 | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 6210 | // If the frame pointer save index hasn't been defined yet. |
| 6211 | if (!FPSI) { |
| 6212 | // Find out what the fix offset of the frame pointer save area. |
| Eric Christopher | dc3a8a4 | 2015-02-13 00:39:38 +0000 | [diff] [blame] | 6213 | int FPOffset = Subtarget.getFrameLowering()->getFramePointerSaveOffset(); |
| Jim Laskey | 48850c1 | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 6214 | // Allocate the frame index for frame pointer save area. |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 6215 | FPSI = MF.getFrameInfo().CreateFixedObject(isPPC64? 8 : 4, FPOffset, true); |
| Jim Laskey | 48850c1 | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 6216 | // Save the result. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6217 | FI->setFramePointerSaveIndex(FPSI); |
| Jim Laskey | 48850c1 | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 6218 | } |
| Arnold Schwaighofer | be0de34 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 6219 | return DAG.getFrameIndex(FPSI, PtrVT); |
| 6220 | } |
| Jim Laskey | 48850c1 | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 6221 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6222 | SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, |
| Eric Christopher | b976a39 | 2016-07-07 00:39:27 +0000 | [diff] [blame] | 6223 | SelectionDAG &DAG) const { |
| Jim Laskey | 48850c1 | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 6224 | // Get the inputs. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6225 | SDValue Chain = Op.getOperand(0); |
| 6226 | SDValue Size = Op.getOperand(1); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6227 | SDLoc dl(Op); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6228 | |
| Jim Laskey | 48850c1 | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 6229 | // Get the corect type for pointers. |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 6230 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
| Jim Laskey | 48850c1 | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 6231 | // Negate the size. |
| Dale Johannesen | 400dc2e | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 6232 | SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 6233 | DAG.getConstant(0, dl, PtrVT), Size); |
| Jim Laskey | 48850c1 | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 6234 | // Construct a node for the frame pointer save index. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6235 | SDValue FPSIdx = getFramePointerFrameIndex(DAG); |
| Jim Laskey | 48850c1 | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 6236 | // Build a DYNALLOC node. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6237 | SDValue Ops[3] = { Chain, NegSize, FPSIdx }; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6238 | SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other); |
| Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 6239 | return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops); |
| Jim Laskey | 48850c1 | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 6240 | } |
| 6241 | |
| Hal Finkel | 5081ac2 | 2016-09-01 10:28:47 +0000 | [diff] [blame] | 6242 | SDValue PPCTargetLowering::LowerEH_DWARF_CFA(SDValue Op, |
| 6243 | SelectionDAG &DAG) const { |
| 6244 | MachineFunction &MF = DAG.getMachineFunction(); |
| 6245 | |
| 6246 | bool isPPC64 = Subtarget.isPPC64(); |
| 6247 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
| 6248 | |
| 6249 | int FI = MF.getFrameInfo().CreateFixedObject(isPPC64 ? 8 : 4, 0, false); |
| 6250 | return DAG.getFrameIndex(FI, PtrVT); |
| 6251 | } |
| 6252 | |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 6253 | SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op, |
| 6254 | SelectionDAG &DAG) const { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6255 | SDLoc DL(Op); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 6256 | return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL, |
| 6257 | DAG.getVTList(MVT::i32, MVT::Other), |
| 6258 | Op.getOperand(0), Op.getOperand(1)); |
| 6259 | } |
| 6260 | |
| 6261 | SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op, |
| 6262 | SelectionDAG &DAG) const { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6263 | SDLoc DL(Op); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 6264 | return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other, |
| 6265 | Op.getOperand(0), Op.getOperand(1)); |
| 6266 | } |
| 6267 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 6268 | SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const { |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 6269 | if (Op.getValueType().isVector()) |
| 6270 | return LowerVectorLoad(Op, DAG); |
| 6271 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 6272 | assert(Op.getValueType() == MVT::i1 && |
| 6273 | "Custom lowering only for i1 loads"); |
| 6274 | |
| 6275 | // First, load 8 bits into 32 bits, then truncate to 1 bit. |
| 6276 | |
| 6277 | SDLoc dl(Op); |
| 6278 | LoadSDNode *LD = cast<LoadSDNode>(Op); |
| 6279 | |
| 6280 | SDValue Chain = LD->getChain(); |
| 6281 | SDValue BasePtr = LD->getBasePtr(); |
| 6282 | MachineMemOperand *MMO = LD->getMemOperand(); |
| 6283 | |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 6284 | SDValue NewLD = |
| 6285 | DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(DAG.getDataLayout()), Chain, |
| 6286 | BasePtr, MVT::i8, MMO); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 6287 | SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD); |
| 6288 | |
| 6289 | SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) }; |
| Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 6290 | return DAG.getMergeValues(Ops, dl); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 6291 | } |
| 6292 | |
| 6293 | SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const { |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 6294 | if (Op.getOperand(1).getValueType().isVector()) |
| 6295 | return LowerVectorStore(Op, DAG); |
| 6296 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 6297 | assert(Op.getOperand(1).getValueType() == MVT::i1 && |
| 6298 | "Custom lowering only for i1 stores"); |
| 6299 | |
| 6300 | // First, zero extend to 32 bits, then use a truncating store to 8 bits. |
| 6301 | |
| 6302 | SDLoc dl(Op); |
| 6303 | StoreSDNode *ST = cast<StoreSDNode>(Op); |
| 6304 | |
| 6305 | SDValue Chain = ST->getChain(); |
| 6306 | SDValue BasePtr = ST->getBasePtr(); |
| 6307 | SDValue Value = ST->getValue(); |
| 6308 | MachineMemOperand *MMO = ST->getMemOperand(); |
| 6309 | |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 6310 | Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(DAG.getDataLayout()), |
| 6311 | Value); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 6312 | return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO); |
| 6313 | } |
| 6314 | |
| 6315 | // FIXME: Remove this once the ANDI glue bug is fixed: |
| 6316 | SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const { |
| 6317 | assert(Op.getValueType() == MVT::i1 && |
| 6318 | "Custom lowering only for i1 results"); |
| 6319 | |
| 6320 | SDLoc DL(Op); |
| 6321 | return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1, |
| 6322 | Op.getOperand(0)); |
| 6323 | } |
| 6324 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6325 | /// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when |
| 6326 | /// possible. |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 6327 | SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6328 | // Not FP? Not a fsel. |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6329 | if (!Op.getOperand(0).getValueType().isFloatingPoint() || |
| 6330 | !Op.getOperand(2).getValueType().isFloatingPoint()) |
| Eli Friedman | 5806e18 | 2009-05-28 04:31:08 +0000 | [diff] [blame] | 6331 | return Op; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6332 | |
| Hal Finkel | 81f8799 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 6333 | // We might be able to do better than this under some circumstances, but in |
| 6334 | // general, fsel-based lowering of select is a finite-math-only optimization. |
| 6335 | // For more information, see section F.3 of the 2.06 ISA specification. |
| 6336 | if (!DAG.getTarget().Options.NoInfsFPMath || |
| 6337 | !DAG.getTarget().Options.NoNaNsFPMath) |
| 6338 | return Op; |
| Sanjay Patel | a260701 | 2015-09-16 16:31:21 +0000 | [diff] [blame] | 6339 | // TODO: Propagate flags from the select rather than global settings. |
| 6340 | SDNodeFlags Flags; |
| 6341 | Flags.setNoInfs(true); |
| 6342 | Flags.setNoNaNs(true); |
| NAKAMURA Takumi | a9cb538 | 2015-09-22 11:14:39 +0000 | [diff] [blame] | 6343 | |
| Hal Finkel | 81f8799 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 6344 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6345 | |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6346 | EVT ResVT = Op.getValueType(); |
| 6347 | EVT CmpVT = Op.getOperand(0).getValueType(); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6348 | SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); |
| 6349 | SDValue TV = Op.getOperand(2), FV = Op.getOperand(3); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6350 | SDLoc dl(Op); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6351 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6352 | // If the RHS of the comparison is a 0.0, we don't need to do the |
| 6353 | // subtraction at all. |
| Hal Finkel | 81f8799 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 6354 | SDValue Sel1; |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6355 | if (isFloatingPointZero(RHS)) |
| 6356 | switch (CC) { |
| 6357 | default: break; // SETUO etc aren't handled by fsel. |
| Hal Finkel | 81f8799 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 6358 | case ISD::SETNE: |
| 6359 | std::swap(TV, FV); |
| 6360 | case ISD::SETEQ: |
| 6361 | if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 6362 | LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS); |
| 6363 | Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV); |
| 6364 | if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 6365 | Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1); |
| 6366 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, |
| 6367 | DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6368 | case ISD::SETULT: |
| 6369 | case ISD::SETLT: |
| 6370 | std::swap(TV, FV); // fsel is natively setge, swap operands for setlt |
| Chris Lattner | b56d22c | 2006-05-24 00:06:44 +0000 | [diff] [blame] | 6371 | case ISD::SETOGE: |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6372 | case ISD::SETGE: |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6373 | if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 6374 | LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS); |
| Dale Johannesen | 400dc2e | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 6375 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6376 | case ISD::SETUGT: |
| 6377 | case ISD::SETGT: |
| 6378 | std::swap(TV, FV); // fsel is natively setge, swap operands for setlt |
| Chris Lattner | b56d22c | 2006-05-24 00:06:44 +0000 | [diff] [blame] | 6379 | case ISD::SETOLE: |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6380 | case ISD::SETLE: |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6381 | if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 6382 | LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS); |
| Dale Johannesen | 400dc2e | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 6383 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6384 | DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6385 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6386 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6387 | SDValue Cmp; |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6388 | switch (CC) { |
| 6389 | default: break; // SETUO etc aren't handled by fsel. |
| Hal Finkel | 81f8799 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 6390 | case ISD::SETNE: |
| 6391 | std::swap(TV, FV); |
| 6392 | case ISD::SETEQ: |
| Sanjay Patel | a260701 | 2015-09-16 16:31:21 +0000 | [diff] [blame] | 6393 | Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, &Flags); |
| Hal Finkel | 81f8799 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 6394 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 6395 | Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); |
| 6396 | Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV); |
| 6397 | if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 6398 | Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1); |
| 6399 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, |
| 6400 | DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6401 | case ISD::SETULT: |
| 6402 | case ISD::SETLT: |
| Sanjay Patel | a260701 | 2015-09-16 16:31:21 +0000 | [diff] [blame] | 6403 | Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, &Flags); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6404 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 6405 | Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); |
| Hal Finkel | 81f8799 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 6406 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV); |
| Chris Lattner | b56d22c | 2006-05-24 00:06:44 +0000 | [diff] [blame] | 6407 | case ISD::SETOGE: |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6408 | case ISD::SETGE: |
| Sanjay Patel | a260701 | 2015-09-16 16:31:21 +0000 | [diff] [blame] | 6409 | Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS, &Flags); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6410 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 6411 | Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); |
| Hal Finkel | 81f8799 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 6412 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6413 | case ISD::SETUGT: |
| 6414 | case ISD::SETGT: |
| Sanjay Patel | a260701 | 2015-09-16 16:31:21 +0000 | [diff] [blame] | 6415 | Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS, &Flags); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6416 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 6417 | Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); |
| Hal Finkel | 81f8799 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 6418 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV); |
| Chris Lattner | b56d22c | 2006-05-24 00:06:44 +0000 | [diff] [blame] | 6419 | case ISD::SETOLE: |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6420 | case ISD::SETLE: |
| Sanjay Patel | a260701 | 2015-09-16 16:31:21 +0000 | [diff] [blame] | 6421 | Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS, &Flags); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6422 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 6423 | Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); |
| Hal Finkel | 81f8799 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 6424 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6425 | } |
| Eli Friedman | 5806e18 | 2009-05-28 04:31:08 +0000 | [diff] [blame] | 6426 | return Op; |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6427 | } |
| 6428 | |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6429 | void PPCTargetLowering::LowerFP_TO_INTForReuse(SDValue Op, ReuseLoadInfo &RLI, |
| 6430 | SelectionDAG &DAG, |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 6431 | const SDLoc &dl) const { |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6432 | assert(Op.getOperand(0).getValueType().isFloatingPoint()); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6433 | SDValue Src = Op.getOperand(0); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6434 | if (Src.getValueType() == MVT::f32) |
| 6435 | Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src); |
| Duncan Sands | 2a28791 | 2008-07-19 16:26:02 +0000 | [diff] [blame] | 6436 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6437 | SDValue Tmp; |
| Craig Topper | 5671010 | 2013-08-15 02:33:50 +0000 | [diff] [blame] | 6438 | switch (Op.getSimpleValueType().SimpleTy) { |
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 6439 | default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!"); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6440 | case MVT::i32: |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 6441 | Tmp = DAG.getNode( |
| 6442 | Op.getOpcode() == ISD::FP_TO_SINT |
| 6443 | ? PPCISD::FCTIWZ |
| 6444 | : (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ : PPCISD::FCTIDZ), |
| 6445 | dl, MVT::f64, Src); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6446 | break; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6447 | case MVT::i64: |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 6448 | assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) && |
| Hal Finkel | 3f88d08 | 2013-04-01 18:42:58 +0000 | [diff] [blame] | 6449 | "i64 FP_TO_UINT is supported only with FPCVT"); |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 6450 | Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ : |
| 6451 | PPCISD::FCTIDUZ, |
| 6452 | dl, MVT::f64, Src); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6453 | break; |
| 6454 | } |
| Duncan Sands | 2a28791 | 2008-07-19 16:26:02 +0000 | [diff] [blame] | 6455 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6456 | // Convert the FP value to an int value through memory. |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 6457 | bool i32Stack = Op.getValueType() == MVT::i32 && Subtarget.hasSTFIWX() && |
| 6458 | (Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()); |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 6459 | SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64); |
| 6460 | int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex(); |
| Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 6461 | MachinePointerInfo MPI = |
| 6462 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI); |
| Duncan Sands | 2a28791 | 2008-07-19 16:26:02 +0000 | [diff] [blame] | 6463 | |
| Chris Lattner | 06a4954 | 2007-10-15 20:14:52 +0000 | [diff] [blame] | 6464 | // Emit a store to the stack slot. |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 6465 | SDValue Chain; |
| 6466 | if (i32Stack) { |
| 6467 | MachineFunction &MF = DAG.getMachineFunction(); |
| 6468 | MachineMemOperand *MMO = |
| 6469 | MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4); |
| 6470 | SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr }; |
| 6471 | Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl, |
| Craig Topper | 206fcd4 | 2014-04-26 19:29:41 +0000 | [diff] [blame] | 6472 | DAG.getVTList(MVT::Other), Ops, MVT::i32, MMO); |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 6473 | } else |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 6474 | Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr, MPI); |
| Chris Lattner | 06a4954 | 2007-10-15 20:14:52 +0000 | [diff] [blame] | 6475 | |
| 6476 | // Result is a load from the stack slot. If loading 4 bytes, make sure to |
| Nemanja Ivanovic | 1a5706c | 2016-02-29 16:42:27 +0000 | [diff] [blame] | 6477 | // add in a bias on big endian. |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 6478 | if (Op.getValueType() == MVT::i32 && !i32Stack) { |
| Dale Johannesen | 021052a | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 6479 | FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 6480 | DAG.getConstant(4, dl, FIPtr.getValueType())); |
| Nemanja Ivanovic | 1a5706c | 2016-02-29 16:42:27 +0000 | [diff] [blame] | 6481 | MPI = MPI.getWithOffset(Subtarget.isLittleEndian() ? 0 : 4); |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 6482 | } |
| 6483 | |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6484 | RLI.Chain = Chain; |
| 6485 | RLI.Ptr = FIPtr; |
| 6486 | RLI.MPI = MPI; |
| 6487 | } |
| 6488 | |
| Nemanja Ivanovic | c38b531 | 2015-04-11 10:40:42 +0000 | [diff] [blame] | 6489 | /// \brief Custom lowers floating point to integer conversions to use |
| 6490 | /// the direct move instructions available in ISA 2.07 to avoid the |
| 6491 | /// need for load/store combinations. |
| 6492 | SDValue PPCTargetLowering::LowerFP_TO_INTDirectMove(SDValue Op, |
| 6493 | SelectionDAG &DAG, |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 6494 | const SDLoc &dl) const { |
| Nemanja Ivanovic | c38b531 | 2015-04-11 10:40:42 +0000 | [diff] [blame] | 6495 | assert(Op.getOperand(0).getValueType().isFloatingPoint()); |
| 6496 | SDValue Src = Op.getOperand(0); |
| 6497 | |
| 6498 | if (Src.getValueType() == MVT::f32) |
| 6499 | Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src); |
| 6500 | |
| 6501 | SDValue Tmp; |
| 6502 | switch (Op.getSimpleValueType().SimpleTy) { |
| 6503 | default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!"); |
| 6504 | case MVT::i32: |
| 6505 | Tmp = DAG.getNode( |
| 6506 | Op.getOpcode() == ISD::FP_TO_SINT |
| 6507 | ? PPCISD::FCTIWZ |
| 6508 | : (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ : PPCISD::FCTIDZ), |
| 6509 | dl, MVT::f64, Src); |
| 6510 | Tmp = DAG.getNode(PPCISD::MFVSR, dl, MVT::i32, Tmp); |
| 6511 | break; |
| 6512 | case MVT::i64: |
| 6513 | assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) && |
| 6514 | "i64 FP_TO_UINT is supported only with FPCVT"); |
| 6515 | Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ : |
| 6516 | PPCISD::FCTIDUZ, |
| 6517 | dl, MVT::f64, Src); |
| 6518 | Tmp = DAG.getNode(PPCISD::MFVSR, dl, MVT::i64, Tmp); |
| 6519 | break; |
| 6520 | } |
| 6521 | return Tmp; |
| 6522 | } |
| 6523 | |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6524 | SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG, |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 6525 | const SDLoc &dl) const { |
| Nemanja Ivanovic | c38b531 | 2015-04-11 10:40:42 +0000 | [diff] [blame] | 6526 | if (Subtarget.hasDirectMove() && Subtarget.isPPC64()) |
| 6527 | return LowerFP_TO_INTDirectMove(Op, DAG, dl); |
| 6528 | |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6529 | ReuseLoadInfo RLI; |
| 6530 | LowerFP_TO_INTForReuse(Op, RLI, DAG, dl); |
| 6531 | |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 6532 | return DAG.getLoad(Op.getValueType(), dl, RLI.Chain, RLI.Ptr, RLI.MPI, |
| Justin Lebar | adbf09e | 2016-09-11 01:38:58 +0000 | [diff] [blame] | 6533 | RLI.Alignment, RLI.MMOFlags(), RLI.AAInfo, RLI.Ranges); |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6534 | } |
| 6535 | |
| 6536 | // We're trying to insert a regular store, S, and then a load, L. If the |
| 6537 | // incoming value, O, is a load, we might just be able to have our load use the |
| 6538 | // address used by O. However, we don't know if anything else will store to |
| 6539 | // that address before we can load from it. To prevent this situation, we need |
| 6540 | // to insert our load, L, into the chain as a peer of O. To do this, we give L |
| 6541 | // the same chain operand as O, we create a token factor from the chain results |
| 6542 | // of O and L, and we replace all uses of O's chain result with that token |
| 6543 | // factor (see spliceIntoChain below for this last part). |
| 6544 | bool PPCTargetLowering::canReuseLoadAddress(SDValue Op, EVT MemVT, |
| 6545 | ReuseLoadInfo &RLI, |
| Hal Finkel | 6c39269 | 2015-01-09 01:34:30 +0000 | [diff] [blame] | 6546 | SelectionDAG &DAG, |
| 6547 | ISD::LoadExtType ET) const { |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6548 | SDLoc dl(Op); |
| Hal Finkel | 6c39269 | 2015-01-09 01:34:30 +0000 | [diff] [blame] | 6549 | if (ET == ISD::NON_EXTLOAD && |
| 6550 | (Op.getOpcode() == ISD::FP_TO_UINT || |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6551 | Op.getOpcode() == ISD::FP_TO_SINT) && |
| 6552 | isOperationLegalOrCustom(Op.getOpcode(), |
| 6553 | Op.getOperand(0).getValueType())) { |
| 6554 | |
| 6555 | LowerFP_TO_INTForReuse(Op, RLI, DAG, dl); |
| 6556 | return true; |
| 6557 | } |
| 6558 | |
| 6559 | LoadSDNode *LD = dyn_cast<LoadSDNode>(Op); |
| Hal Finkel | 6c39269 | 2015-01-09 01:34:30 +0000 | [diff] [blame] | 6560 | if (!LD || LD->getExtensionType() != ET || LD->isVolatile() || |
| 6561 | LD->isNonTemporal()) |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6562 | return false; |
| 6563 | if (LD->getMemoryVT() != MemVT) |
| 6564 | return false; |
| 6565 | |
| 6566 | RLI.Ptr = LD->getBasePtr(); |
| Sanjay Patel | 7506852 | 2016-03-14 18:09:43 +0000 | [diff] [blame] | 6567 | if (LD->isIndexed() && !LD->getOffset().isUndef()) { |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6568 | assert(LD->getAddressingMode() == ISD::PRE_INC && |
| 6569 | "Non-pre-inc AM on PPC?"); |
| 6570 | RLI.Ptr = DAG.getNode(ISD::ADD, dl, RLI.Ptr.getValueType(), RLI.Ptr, |
| 6571 | LD->getOffset()); |
| 6572 | } |
| 6573 | |
| 6574 | RLI.Chain = LD->getChain(); |
| 6575 | RLI.MPI = LD->getPointerInfo(); |
| Justin Lebar | adbf09e | 2016-09-11 01:38:58 +0000 | [diff] [blame] | 6576 | RLI.IsDereferenceable = LD->isDereferenceable(); |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6577 | RLI.IsInvariant = LD->isInvariant(); |
| 6578 | RLI.Alignment = LD->getAlignment(); |
| 6579 | RLI.AAInfo = LD->getAAInfo(); |
| 6580 | RLI.Ranges = LD->getRanges(); |
| 6581 | |
| 6582 | RLI.ResChain = SDValue(LD, LD->isIndexed() ? 2 : 1); |
| 6583 | return true; |
| 6584 | } |
| 6585 | |
| 6586 | // Given the head of the old chain, ResChain, insert a token factor containing |
| 6587 | // it and NewResChain, and make users of ResChain now be users of that token |
| 6588 | // factor. |
| 6589 | void PPCTargetLowering::spliceIntoChain(SDValue ResChain, |
| 6590 | SDValue NewResChain, |
| 6591 | SelectionDAG &DAG) const { |
| 6592 | if (!ResChain) |
| 6593 | return; |
| 6594 | |
| 6595 | SDLoc dl(NewResChain); |
| 6596 | |
| 6597 | SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 6598 | NewResChain, DAG.getUNDEF(MVT::Other)); |
| 6599 | assert(TF.getNode() != NewResChain.getNode() && |
| 6600 | "A new TF really is required here"); |
| 6601 | |
| 6602 | DAG.ReplaceAllUsesOfValueWith(ResChain, TF); |
| 6603 | DAG.UpdateNodeOperands(TF.getNode(), ResChain, NewResChain); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6604 | } |
| 6605 | |
| Ehsan Amiri | 322eca3 | 2016-04-06 20:12:29 +0000 | [diff] [blame] | 6606 | /// \brief Analyze profitability of direct move |
| 6607 | /// prefer float load to int load plus direct move |
| 6608 | /// when there is no integer use of int load |
| 6609 | static bool directMoveIsProfitable(const SDValue &Op) { |
| 6610 | SDNode *Origin = Op.getOperand(0).getNode(); |
| 6611 | if (Origin->getOpcode() != ISD::LOAD) |
| 6612 | return true; |
| 6613 | |
| 6614 | for (SDNode::use_iterator UI = Origin->use_begin(), |
| 6615 | UE = Origin->use_end(); |
| 6616 | UI != UE; ++UI) { |
| 6617 | |
| 6618 | // Only look at the users of the loaded value. |
| 6619 | if (UI.getUse().get().getResNo() != 0) |
| 6620 | continue; |
| 6621 | |
| 6622 | if (UI->getOpcode() != ISD::SINT_TO_FP && |
| 6623 | UI->getOpcode() != ISD::UINT_TO_FP) |
| 6624 | return true; |
| 6625 | } |
| 6626 | |
| 6627 | return false; |
| 6628 | } |
| 6629 | |
| Nemanja Ivanovic | c38b531 | 2015-04-11 10:40:42 +0000 | [diff] [blame] | 6630 | /// \brief Custom lowers integer to floating point conversions to use |
| 6631 | /// the direct move instructions available in ISA 2.07 to avoid the |
| 6632 | /// need for load/store combinations. |
| 6633 | SDValue PPCTargetLowering::LowerINT_TO_FPDirectMove(SDValue Op, |
| 6634 | SelectionDAG &DAG, |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 6635 | const SDLoc &dl) const { |
| Nemanja Ivanovic | c38b531 | 2015-04-11 10:40:42 +0000 | [diff] [blame] | 6636 | assert((Op.getValueType() == MVT::f32 || |
| 6637 | Op.getValueType() == MVT::f64) && |
| 6638 | "Invalid floating point type as target of conversion"); |
| 6639 | assert(Subtarget.hasFPCVT() && |
| 6640 | "Int to FP conversions with direct moves require FPCVT"); |
| 6641 | SDValue FP; |
| 6642 | SDValue Src = Op.getOperand(0); |
| 6643 | bool SinglePrec = Op.getValueType() == MVT::f32; |
| 6644 | bool WordInt = Src.getSimpleValueType().SimpleTy == MVT::i32; |
| 6645 | bool Signed = Op.getOpcode() == ISD::SINT_TO_FP; |
| 6646 | unsigned ConvOp = Signed ? (SinglePrec ? PPCISD::FCFIDS : PPCISD::FCFID) : |
| 6647 | (SinglePrec ? PPCISD::FCFIDUS : PPCISD::FCFIDU); |
| 6648 | |
| 6649 | if (WordInt) { |
| 6650 | FP = DAG.getNode(Signed ? PPCISD::MTVSRA : PPCISD::MTVSRZ, |
| 6651 | dl, MVT::f64, Src); |
| 6652 | FP = DAG.getNode(ConvOp, dl, SinglePrec ? MVT::f32 : MVT::f64, FP); |
| 6653 | } |
| 6654 | else { |
| 6655 | FP = DAG.getNode(PPCISD::MTVSRA, dl, MVT::f64, Src); |
| 6656 | FP = DAG.getNode(ConvOp, dl, SinglePrec ? MVT::f32 : MVT::f64, FP); |
| 6657 | } |
| 6658 | |
| 6659 | return FP; |
| 6660 | } |
| 6661 | |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 6662 | SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op, |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6663 | SelectionDAG &DAG) const { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6664 | SDLoc dl(Op); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 6665 | |
| 6666 | if (Subtarget.hasQPX() && Op.getOperand(0).getValueType() == MVT::v4i1) { |
| 6667 | if (Op.getValueType() != MVT::v4f32 && Op.getValueType() != MVT::v4f64) |
| 6668 | return SDValue(); |
| 6669 | |
| 6670 | SDValue Value = Op.getOperand(0); |
| 6671 | // The values are now known to be -1 (false) or 1 (true). To convert this |
| 6672 | // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5). |
| 6673 | // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5 |
| 6674 | Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value); |
| NAKAMURA Takumi | a9cb538 | 2015-09-22 11:14:39 +0000 | [diff] [blame] | 6675 | |
| Ahmed Bougacha | 93cff7f | 2016-02-15 18:07:29 +0000 | [diff] [blame] | 6676 | SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::v4f64); |
| NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 6677 | |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 6678 | Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs); |
| 6679 | |
| 6680 | if (Op.getValueType() != MVT::v4f64) |
| 6681 | Value = DAG.getNode(ISD::FP_ROUND, dl, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 6682 | Op.getValueType(), Value, |
| 6683 | DAG.getIntPtrConstant(1, dl)); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 6684 | return Value; |
| 6685 | } |
| 6686 | |
| Dan Gohman | d6819da | 2008-03-11 01:59:03 +0000 | [diff] [blame] | 6687 | // Don't handle ppc_fp128 here; let it be lowered to a libcall. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6688 | if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6689 | return SDValue(); |
| Dan Gohman | d6819da | 2008-03-11 01:59:03 +0000 | [diff] [blame] | 6690 | |
| Hal Finkel | 6a56b21 | 2014-03-05 22:14:00 +0000 | [diff] [blame] | 6691 | if (Op.getOperand(0).getValueType() == MVT::i1) |
| 6692 | return DAG.getNode(ISD::SELECT, dl, Op.getValueType(), Op.getOperand(0), |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 6693 | DAG.getConstantFP(1.0, dl, Op.getValueType()), |
| 6694 | DAG.getConstantFP(0.0, dl, Op.getValueType())); |
| Hal Finkel | 6a56b21 | 2014-03-05 22:14:00 +0000 | [diff] [blame] | 6695 | |
| Nemanja Ivanovic | c38b531 | 2015-04-11 10:40:42 +0000 | [diff] [blame] | 6696 | // If we have direct moves, we can do all the conversion, skip the store/load |
| 6697 | // however, without FPCVT we can't do most conversions. |
| Ehsan Amiri | 322eca3 | 2016-04-06 20:12:29 +0000 | [diff] [blame] | 6698 | if (Subtarget.hasDirectMove() && directMoveIsProfitable(Op) && |
| 6699 | Subtarget.isPPC64() && Subtarget.hasFPCVT()) |
| Nemanja Ivanovic | c38b531 | 2015-04-11 10:40:42 +0000 | [diff] [blame] | 6700 | return LowerINT_TO_FPDirectMove(Op, DAG, dl); |
| 6701 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 6702 | assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) && |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 6703 | "UINT_TO_FP is supported only with FPCVT"); |
| 6704 | |
| 6705 | // If we have FCFIDS, then use it when converting to single-precision. |
| Hal Finkel | 93d75ea | 2013-04-02 03:29:51 +0000 | [diff] [blame] | 6706 | // Otherwise, convert to double-precision and then round. |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 6707 | unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) |
| 6708 | ? (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDUS |
| 6709 | : PPCISD::FCFIDS) |
| 6710 | : (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDU |
| 6711 | : PPCISD::FCFID); |
| 6712 | MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) |
| 6713 | ? MVT::f32 |
| 6714 | : MVT::f64; |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 6715 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6716 | if (Op.getOperand(0).getValueType() == MVT::i64) { |
| Ulrich Weigand | d34b5bd | 2012-10-18 13:16:11 +0000 | [diff] [blame] | 6717 | SDValue SINT = Op.getOperand(0); |
| 6718 | // When converting to single-precision, we actually need to convert |
| 6719 | // to double-precision first and then round to single-precision. |
| 6720 | // To avoid double-rounding effects during that operation, we have |
| 6721 | // to prepare the input operand. Bits that might be truncated when |
| 6722 | // converting to double-precision are replaced by a bit that won't |
| 6723 | // be lost at this stage, but is below the single-precision rounding |
| 6724 | // position. |
| 6725 | // |
| 6726 | // However, if -enable-unsafe-fp-math is in effect, accept double |
| 6727 | // rounding to avoid the extra overhead. |
| 6728 | if (Op.getValueType() == MVT::f32 && |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 6729 | !Subtarget.hasFPCVT() && |
| Ulrich Weigand | d34b5bd | 2012-10-18 13:16:11 +0000 | [diff] [blame] | 6730 | !DAG.getTarget().Options.UnsafeFPMath) { |
| 6731 | |
| 6732 | // Twiddle input to make sure the low 11 bits are zero. (If this |
| 6733 | // is the case, we are guaranteed the value will fit into the 53 bit |
| 6734 | // mantissa of an IEEE double-precision value without rounding.) |
| 6735 | // If any of those low 11 bits were not zero originally, make sure |
| 6736 | // bit 12 (value 2048) is set instead, so that the final rounding |
| 6737 | // to single-precision gets the correct result. |
| 6738 | SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 6739 | SINT, DAG.getConstant(2047, dl, MVT::i64)); |
| Ulrich Weigand | d34b5bd | 2012-10-18 13:16:11 +0000 | [diff] [blame] | 6740 | Round = DAG.getNode(ISD::ADD, dl, MVT::i64, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 6741 | Round, DAG.getConstant(2047, dl, MVT::i64)); |
| Ulrich Weigand | d34b5bd | 2012-10-18 13:16:11 +0000 | [diff] [blame] | 6742 | Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT); |
| 6743 | Round = DAG.getNode(ISD::AND, dl, MVT::i64, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 6744 | Round, DAG.getConstant(-2048, dl, MVT::i64)); |
| Ulrich Weigand | d34b5bd | 2012-10-18 13:16:11 +0000 | [diff] [blame] | 6745 | |
| 6746 | // However, we cannot use that value unconditionally: if the magnitude |
| 6747 | // of the input value is small, the bit-twiddling we did above might |
| 6748 | // end up visibly changing the output. Fortunately, in that case, we |
| 6749 | // don't need to twiddle bits since the original input will convert |
| 6750 | // exactly to double-precision floating-point already. Therefore, |
| 6751 | // construct a conditional to use the original value if the top 11 |
| 6752 | // bits are all sign-bit copies, and use the rounded value computed |
| 6753 | // above otherwise. |
| 6754 | SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 6755 | SINT, DAG.getConstant(53, dl, MVT::i32)); |
| Ulrich Weigand | d34b5bd | 2012-10-18 13:16:11 +0000 | [diff] [blame] | 6756 | Cond = DAG.getNode(ISD::ADD, dl, MVT::i64, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 6757 | Cond, DAG.getConstant(1, dl, MVT::i64)); |
| Ulrich Weigand | d34b5bd | 2012-10-18 13:16:11 +0000 | [diff] [blame] | 6758 | Cond = DAG.getSetCC(dl, MVT::i32, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 6759 | Cond, DAG.getConstant(1, dl, MVT::i64), ISD::SETUGT); |
| Ulrich Weigand | d34b5bd | 2012-10-18 13:16:11 +0000 | [diff] [blame] | 6760 | |
| 6761 | SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT); |
| 6762 | } |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 6763 | |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6764 | ReuseLoadInfo RLI; |
| 6765 | SDValue Bits; |
| 6766 | |
| Hal Finkel | 6c39269 | 2015-01-09 01:34:30 +0000 | [diff] [blame] | 6767 | MachineFunction &MF = DAG.getMachineFunction(); |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6768 | if (canReuseLoadAddress(SINT, MVT::i64, RLI, DAG)) { |
| Justin Lebar | adbf09e | 2016-09-11 01:38:58 +0000 | [diff] [blame] | 6769 | Bits = DAG.getLoad(MVT::f64, dl, RLI.Chain, RLI.Ptr, RLI.MPI, |
| 6770 | RLI.Alignment, RLI.MMOFlags(), RLI.AAInfo, RLI.Ranges); |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6771 | spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG); |
| Hal Finkel | 6c39269 | 2015-01-09 01:34:30 +0000 | [diff] [blame] | 6772 | } else if (Subtarget.hasLFIWAX() && |
| 6773 | canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::SEXTLOAD)) { |
| 6774 | MachineMemOperand *MMO = |
| 6775 | MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4, |
| 6776 | RLI.Alignment, RLI.AAInfo, RLI.Ranges); |
| 6777 | SDValue Ops[] = { RLI.Chain, RLI.Ptr }; |
| 6778 | Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWAX, dl, |
| 6779 | DAG.getVTList(MVT::f64, MVT::Other), |
| 6780 | Ops, MVT::i32, MMO); |
| 6781 | spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG); |
| 6782 | } else if (Subtarget.hasFPCVT() && |
| 6783 | canReuseLoadAddress(SINT, MVT::i32, RLI, DAG, ISD::ZEXTLOAD)) { |
| 6784 | MachineMemOperand *MMO = |
| 6785 | MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4, |
| 6786 | RLI.Alignment, RLI.AAInfo, RLI.Ranges); |
| 6787 | SDValue Ops[] = { RLI.Chain, RLI.Ptr }; |
| 6788 | Bits = DAG.getMemIntrinsicNode(PPCISD::LFIWZX, dl, |
| 6789 | DAG.getVTList(MVT::f64, MVT::Other), |
| 6790 | Ops, MVT::i32, MMO); |
| 6791 | spliceIntoChain(RLI.ResChain, Bits.getValue(1), DAG); |
| 6792 | } else if (((Subtarget.hasLFIWAX() && |
| 6793 | SINT.getOpcode() == ISD::SIGN_EXTEND) || |
| 6794 | (Subtarget.hasFPCVT() && |
| 6795 | SINT.getOpcode() == ISD::ZERO_EXTEND)) && |
| 6796 | SINT.getOperand(0).getValueType() == MVT::i32) { |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 6797 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 6798 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
| Hal Finkel | 6c39269 | 2015-01-09 01:34:30 +0000 | [diff] [blame] | 6799 | |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 6800 | int FrameIdx = MFI.CreateStackObject(4, 4, false); |
| Hal Finkel | 6c39269 | 2015-01-09 01:34:30 +0000 | [diff] [blame] | 6801 | SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); |
| 6802 | |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 6803 | SDValue Store = |
| 6804 | DAG.getStore(DAG.getEntryNode(), dl, SINT.getOperand(0), FIdx, |
| 6805 | MachinePointerInfo::getFixedStack( |
| 6806 | DAG.getMachineFunction(), FrameIdx)); |
| Hal Finkel | 6c39269 | 2015-01-09 01:34:30 +0000 | [diff] [blame] | 6807 | |
| 6808 | assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 && |
| 6809 | "Expected an i32 store"); |
| 6810 | |
| 6811 | RLI.Ptr = FIdx; |
| 6812 | RLI.Chain = Store; |
| Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 6813 | RLI.MPI = |
| 6814 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx); |
| Hal Finkel | 6c39269 | 2015-01-09 01:34:30 +0000 | [diff] [blame] | 6815 | RLI.Alignment = 4; |
| 6816 | |
| 6817 | MachineMemOperand *MMO = |
| 6818 | MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4, |
| 6819 | RLI.Alignment, RLI.AAInfo, RLI.Ranges); |
| 6820 | SDValue Ops[] = { RLI.Chain, RLI.Ptr }; |
| 6821 | Bits = DAG.getMemIntrinsicNode(SINT.getOpcode() == ISD::ZERO_EXTEND ? |
| 6822 | PPCISD::LFIWZX : PPCISD::LFIWAX, |
| 6823 | dl, DAG.getVTList(MVT::f64, MVT::Other), |
| 6824 | Ops, MVT::i32, MMO); |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6825 | } else |
| 6826 | Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT); |
| 6827 | |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 6828 | SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits); |
| 6829 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 6830 | if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT()) |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6831 | FP = DAG.getNode(ISD::FP_ROUND, dl, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 6832 | MVT::f32, FP, DAG.getIntPtrConstant(0, dl)); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6833 | return FP; |
| 6834 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6835 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6836 | assert(Op.getOperand(0).getValueType() == MVT::i32 && |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 6837 | "Unhandled INT_TO_FP type in custom expander!"); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6838 | // Since we only generate this in 64-bit mode, we can take advantage of |
| 6839 | // 64-bit registers. In particular, sign extend the input value into the |
| 6840 | // 64-bit register with extsw, store the WHOLE 64-bit value into the stack |
| 6841 | // then lfd it and fcfid it. |
| Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 6842 | MachineFunction &MF = DAG.getMachineFunction(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 6843 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 6844 | EVT PtrVT = getPointerTy(MF.getDataLayout()); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6845 | |
| Hal Finkel | beb296b | 2013-03-31 10:12:51 +0000 | [diff] [blame] | 6846 | SDValue Ld; |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 6847 | if (Subtarget.hasLFIWAX() || Subtarget.hasFPCVT()) { |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6848 | ReuseLoadInfo RLI; |
| 6849 | bool ReusingLoad; |
| 6850 | if (!(ReusingLoad = canReuseLoadAddress(Op.getOperand(0), MVT::i32, RLI, |
| 6851 | DAG))) { |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 6852 | int FrameIdx = MFI.CreateStackObject(4, 4, false); |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6853 | SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6854 | |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 6855 | SDValue Store = |
| 6856 | DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx, |
| 6857 | MachinePointerInfo::getFixedStack( |
| 6858 | DAG.getMachineFunction(), FrameIdx)); |
| Hal Finkel | e53429a | 2013-03-31 01:58:02 +0000 | [diff] [blame] | 6859 | |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6860 | assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 && |
| 6861 | "Expected an i32 store"); |
| 6862 | |
| 6863 | RLI.Ptr = FIdx; |
| 6864 | RLI.Chain = Store; |
| Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 6865 | RLI.MPI = |
| 6866 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx); |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6867 | RLI.Alignment = 4; |
| 6868 | } |
| 6869 | |
| Hal Finkel | beb296b | 2013-03-31 10:12:51 +0000 | [diff] [blame] | 6870 | MachineMemOperand *MMO = |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6871 | MF.getMachineMemOperand(RLI.MPI, MachineMemOperand::MOLoad, 4, |
| 6872 | RLI.Alignment, RLI.AAInfo, RLI.Ranges); |
| 6873 | SDValue Ops[] = { RLI.Chain, RLI.Ptr }; |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 6874 | Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ? |
| 6875 | PPCISD::LFIWZX : PPCISD::LFIWAX, |
| 6876 | dl, DAG.getVTList(MVT::f64, MVT::Other), |
| Craig Topper | 206fcd4 | 2014-04-26 19:29:41 +0000 | [diff] [blame] | 6877 | Ops, MVT::i32, MMO); |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 6878 | if (ReusingLoad) |
| 6879 | spliceIntoChain(RLI.ResChain, Ld.getValue(1), DAG); |
| Hal Finkel | beb296b | 2013-03-31 10:12:51 +0000 | [diff] [blame] | 6880 | } else { |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 6881 | assert(Subtarget.isPPC64() && |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 6882 | "i32->FP without LFIWAX supported only on PPC64"); |
| 6883 | |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 6884 | int FrameIdx = MFI.CreateStackObject(8, 8, false); |
| Hal Finkel | beb296b | 2013-03-31 10:12:51 +0000 | [diff] [blame] | 6885 | SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); |
| 6886 | |
| 6887 | SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64, |
| 6888 | Op.getOperand(0)); |
| 6889 | |
| 6890 | // STD the extended value into the stack slot. |
| Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 6891 | SDValue Store = DAG.getStore( |
| 6892 | DAG.getEntryNode(), dl, Ext64, FIdx, |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 6893 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx)); |
| Hal Finkel | beb296b | 2013-03-31 10:12:51 +0000 | [diff] [blame] | 6894 | |
| 6895 | // Load the value as a double. |
| Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 6896 | Ld = DAG.getLoad( |
| 6897 | MVT::f64, dl, Store, FIdx, |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 6898 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx)); |
| Hal Finkel | beb296b | 2013-03-31 10:12:51 +0000 | [diff] [blame] | 6899 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6900 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6901 | // FCFID it and return it. |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 6902 | SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld); |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 6903 | if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT()) |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 6904 | FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, |
| 6905 | DAG.getIntPtrConstant(0, dl)); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6906 | return FP; |
| 6907 | } |
| 6908 | |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 6909 | SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op, |
| 6910 | SelectionDAG &DAG) const { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6911 | SDLoc dl(Op); |
| Dale Johannesen | 5c94cb3 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 6912 | /* |
| 6913 | The rounding mode is in bits 30:31 of FPSR, and has the following |
| 6914 | settings: |
| 6915 | 00 Round to nearest |
| 6916 | 01 Round to 0 |
| 6917 | 10 Round to +inf |
| 6918 | 11 Round to -inf |
| 6919 | |
| 6920 | FLT_ROUNDS, on the other hand, expects the following: |
| 6921 | -1 Undefined |
| 6922 | 0 Round to 0 |
| 6923 | 1 Round to nearest |
| 6924 | 2 Round to +inf |
| 6925 | 3 Round to -inf |
| 6926 | |
| 6927 | To perform the conversion, we do: |
| 6928 | ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1)) |
| 6929 | */ |
| 6930 | |
| 6931 | MachineFunction &MF = DAG.getMachineFunction(); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6932 | EVT VT = Op.getValueType(); |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 6933 | EVT PtrVT = getPointerTy(MF.getDataLayout()); |
| Dale Johannesen | 5c94cb3 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 6934 | |
| 6935 | // Save FP Control Word to register |
| Benjamin Kramer | fdf362b | 2013-03-07 20:33:29 +0000 | [diff] [blame] | 6936 | EVT NodeTys[] = { |
| 6937 | MVT::f64, // return register |
| 6938 | MVT::Glue // unused in this context |
| 6939 | }; |
| Craig Topper | 2d2aa0c | 2014-04-30 07:17:30 +0000 | [diff] [blame] | 6940 | SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, None); |
| Dale Johannesen | 5c94cb3 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 6941 | |
| 6942 | // Save FP register to stack slot |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 6943 | int SSFI = MF.getFrameInfo().CreateStackObject(8, 8, false); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6944 | SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 6945 | SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain, StackSlot, |
| 6946 | MachinePointerInfo()); |
| Dale Johannesen | 5c94cb3 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 6947 | |
| 6948 | // Load FP Control Word from low 32 bits of stack slot. |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 6949 | SDValue Four = DAG.getConstant(4, dl, PtrVT); |
| Dale Johannesen | 021052a | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 6950 | SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 6951 | SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo()); |
| Dale Johannesen | 5c94cb3 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 6952 | |
| 6953 | // Transform as necessary |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6954 | SDValue CWD1 = |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6955 | DAG.getNode(ISD::AND, dl, MVT::i32, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 6956 | CWD, DAG.getConstant(3, dl, MVT::i32)); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6957 | SDValue CWD2 = |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6958 | DAG.getNode(ISD::SRL, dl, MVT::i32, |
| 6959 | DAG.getNode(ISD::AND, dl, MVT::i32, |
| 6960 | DAG.getNode(ISD::XOR, dl, MVT::i32, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 6961 | CWD, DAG.getConstant(3, dl, MVT::i32)), |
| 6962 | DAG.getConstant(3, dl, MVT::i32)), |
| 6963 | DAG.getConstant(1, dl, MVT::i32)); |
| Dale Johannesen | 5c94cb3 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 6964 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6965 | SDValue RetVal = |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6966 | DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2); |
| Dale Johannesen | 5c94cb3 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 6967 | |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6968 | return DAG.getNode((VT.getSizeInBits() < 16 ? |
| Dale Johannesen | 021052a | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 6969 | ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal); |
| Dale Johannesen | 5c94cb3 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 6970 | } |
| 6971 | |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 6972 | SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6973 | EVT VT = Op.getValueType(); |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6974 | unsigned BitWidth = VT.getSizeInBits(); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6975 | SDLoc dl(Op); |
| Dan Gohman | 8d2ead2 | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 6976 | assert(Op.getNumOperands() == 3 && |
| 6977 | VT == Op.getOperand(1).getValueType() && |
| 6978 | "Unexpected SHL!"); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6979 | |
| Chris Lattner | 601b865 | 2006-09-20 03:47:40 +0000 | [diff] [blame] | 6980 | // Expand into a bunch of logical ops. Note that these ops |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6981 | // depend on the PPC behavior for oversized shift amounts. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6982 | SDValue Lo = Op.getOperand(0); |
| 6983 | SDValue Hi = Op.getOperand(1); |
| 6984 | SDValue Amt = Op.getOperand(2); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6985 | EVT AmtVT = Amt.getValueType(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6986 | |
| Dale Johannesen | 7ae8c8b | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 6987 | SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 6988 | DAG.getConstant(BitWidth, dl, AmtVT), Amt); |
| Dale Johannesen | 7ae8c8b | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 6989 | SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt); |
| 6990 | SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1); |
| 6991 | SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3); |
| 6992 | SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 6993 | DAG.getConstant(-BitWidth, dl, AmtVT)); |
| Dale Johannesen | 7ae8c8b | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 6994 | SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5); |
| 6995 | SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6); |
| 6996 | SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6997 | SDValue OutOps[] = { OutLo, OutHi }; |
| Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 6998 | return DAG.getMergeValues(OutOps, dl); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6999 | } |
| 7000 | |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 7001 | SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7002 | EVT VT = Op.getValueType(); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7003 | SDLoc dl(Op); |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7004 | unsigned BitWidth = VT.getSizeInBits(); |
| Dan Gohman | 8d2ead2 | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 7005 | assert(Op.getNumOperands() == 3 && |
| 7006 | VT == Op.getOperand(1).getValueType() && |
| 7007 | "Unexpected SRL!"); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7008 | |
| Dan Gohman | 8d2ead2 | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 7009 | // Expand into a bunch of logical ops. Note that these ops |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 7010 | // depend on the PPC behavior for oversized shift amounts. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7011 | SDValue Lo = Op.getOperand(0); |
| 7012 | SDValue Hi = Op.getOperand(1); |
| 7013 | SDValue Amt = Op.getOperand(2); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7014 | EVT AmtVT = Amt.getValueType(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7015 | |
| Dale Johannesen | 7ae8c8b | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 7016 | SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7017 | DAG.getConstant(BitWidth, dl, AmtVT), Amt); |
| Dale Johannesen | 7ae8c8b | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 7018 | SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt); |
| 7019 | SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1); |
| 7020 | SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3); |
| 7021 | SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7022 | DAG.getConstant(-BitWidth, dl, AmtVT)); |
| Dale Johannesen | 7ae8c8b | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 7023 | SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5); |
| 7024 | SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6); |
| 7025 | SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7026 | SDValue OutOps[] = { OutLo, OutHi }; |
| Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 7027 | return DAG.getMergeValues(OutOps, dl); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 7028 | } |
| 7029 | |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 7030 | SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7031 | SDLoc dl(Op); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7032 | EVT VT = Op.getValueType(); |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7033 | unsigned BitWidth = VT.getSizeInBits(); |
| Dan Gohman | 8d2ead2 | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 7034 | assert(Op.getNumOperands() == 3 && |
| 7035 | VT == Op.getOperand(1).getValueType() && |
| 7036 | "Unexpected SRA!"); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7037 | |
| Dan Gohman | 8d2ead2 | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 7038 | // Expand into a bunch of logical ops, followed by a select_cc. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7039 | SDValue Lo = Op.getOperand(0); |
| 7040 | SDValue Hi = Op.getOperand(1); |
| 7041 | SDValue Amt = Op.getOperand(2); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7042 | EVT AmtVT = Amt.getValueType(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7043 | |
| Dale Johannesen | f2bb6f0 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 7044 | SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7045 | DAG.getConstant(BitWidth, dl, AmtVT), Amt); |
| Dale Johannesen | f2bb6f0 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 7046 | SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt); |
| 7047 | SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1); |
| 7048 | SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3); |
| 7049 | SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7050 | DAG.getConstant(-BitWidth, dl, AmtVT)); |
| Dale Johannesen | f2bb6f0 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 7051 | SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5); |
| 7052 | SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7053 | SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, dl, AmtVT), |
| Duncan Sands | 1310574 | 2008-10-30 19:28:32 +0000 | [diff] [blame] | 7054 | Tmp4, Tmp6, ISD::SETLE); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7055 | SDValue OutOps[] = { OutLo, OutHi }; |
| Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 7056 | return DAG.getMergeValues(OutOps, dl); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 7057 | } |
| 7058 | |
| 7059 | //===----------------------------------------------------------------------===// |
| 7060 | // Vector related lowering. |
| 7061 | // |
| 7062 | |
| Chris Lattner | 2a099c0 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 7063 | /// BuildSplatI - Build a canonical splati of Val with an element size of |
| 7064 | /// SplatSize. Cast the result to VT. |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7065 | static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT, |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 7066 | SelectionDAG &DAG, const SDLoc &dl) { |
| Chris Lattner | 2a099c0 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 7067 | assert(Val >= -16 && Val <= 15 && "vsplti is out of range!"); |
| Chris Lattner | 09ed0ff | 2006-12-01 01:45:39 +0000 | [diff] [blame] | 7068 | |
| Benjamin Kramer | 7149aab | 2015-03-01 18:09:56 +0000 | [diff] [blame] | 7069 | static const MVT VTys[] = { // canonical VT to use for each size. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7070 | MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32 |
| Chris Lattner | 2a099c0 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 7071 | }; |
| Chris Lattner | 09ed0ff | 2006-12-01 01:45:39 +0000 | [diff] [blame] | 7072 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7073 | EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1]; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7074 | |
| Chris Lattner | 09ed0ff | 2006-12-01 01:45:39 +0000 | [diff] [blame] | 7075 | // Force vspltis[hw] -1 to vspltisb -1 to canonicalize. |
| 7076 | if (Val == -1) |
| 7077 | SplatSize = 1; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7078 | |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7079 | EVT CanonicalVT = VTys[SplatSize-1]; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7080 | |
| Chris Lattner | 2a099c0 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 7081 | // Build a canonical splat for this value. |
| Ahmed Bougacha | 93cff7f | 2016-02-15 18:07:29 +0000 | [diff] [blame] | 7082 | return DAG.getBitcast(ReqVT, DAG.getConstant(Val, dl, CanonicalVT)); |
| Chris Lattner | 2a099c0 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 7083 | } |
| 7084 | |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 7085 | /// BuildIntrinsicOp - Return a unary operator intrinsic node with the |
| 7086 | /// specified intrinsic ID. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 7087 | static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op, SelectionDAG &DAG, |
| 7088 | const SDLoc &dl, EVT DestVT = MVT::Other) { |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 7089 | if (DestVT == MVT::Other) DestVT = Op.getValueType(); |
| 7090 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7091 | DAG.getConstant(IID, dl, MVT::i32), Op); |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 7092 | } |
| 7093 | |
| Chris Lattner | a2cae1b | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 7094 | /// BuildIntrinsicOp - Return a binary operator intrinsic node with the |
| Chris Lattner | 1b3806a | 2006-04-17 06:58:41 +0000 | [diff] [blame] | 7095 | /// specified intrinsic ID. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7096 | static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS, |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 7097 | SelectionDAG &DAG, const SDLoc &dl, |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7098 | EVT DestVT = MVT::Other) { |
| 7099 | if (DestVT == MVT::Other) DestVT = LHS.getValueType(); |
| Dale Johannesen | 9f3f72f | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 7100 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7101 | DAG.getConstant(IID, dl, MVT::i32), LHS, RHS); |
| Chris Lattner | 1b3806a | 2006-04-17 06:58:41 +0000 | [diff] [blame] | 7102 | } |
| 7103 | |
| Chris Lattner | a2cae1b | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 7104 | /// BuildIntrinsicOp - Return a ternary operator intrinsic node with the |
| 7105 | /// specified intrinsic ID. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7106 | static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1, |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 7107 | SDValue Op2, SelectionDAG &DAG, const SDLoc &dl, |
| 7108 | EVT DestVT = MVT::Other) { |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7109 | if (DestVT == MVT::Other) DestVT = Op0.getValueType(); |
| Dale Johannesen | 9f3f72f | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 7110 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7111 | DAG.getConstant(IID, dl, MVT::i32), Op0, Op1, Op2); |
| Chris Lattner | a2cae1b | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 7112 | } |
| 7113 | |
| Chris Lattner | 264c908 | 2006-04-17 17:55:10 +0000 | [diff] [blame] | 7114 | /// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified |
| 7115 | /// amount. The result has the specified value type. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 7116 | static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt, EVT VT, |
| 7117 | SelectionDAG &DAG, const SDLoc &dl) { |
| Chris Lattner | 264c908 | 2006-04-17 17:55:10 +0000 | [diff] [blame] | 7118 | // Force LHS/RHS to be the right type. |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7119 | LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS); |
| 7120 | RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS); |
| Duncan Sands | b0e3938 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 7121 | |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7122 | int Ops[16]; |
| Chris Lattner | 264c908 | 2006-04-17 17:55:10 +0000 | [diff] [blame] | 7123 | for (unsigned i = 0; i != 16; ++i) |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7124 | Ops[i] = i + Amt; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7125 | SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7126 | return DAG.getNode(ISD::BITCAST, dl, VT, T); |
| Chris Lattner | 264c908 | 2006-04-17 17:55:10 +0000 | [diff] [blame] | 7127 | } |
| 7128 | |
| Nemanja Ivanovic | df1cb52 | 2016-11-29 16:11:34 +0000 | [diff] [blame] | 7129 | /// Do we have an efficient pattern in a .td file for this node? |
| 7130 | /// |
| 7131 | /// \param V - pointer to the BuildVectorSDNode being matched |
| 7132 | /// \param HasDirectMove - does this subtarget have VSR <-> GPR direct moves? |
| 7133 | /// |
| 7134 | /// There are some patterns where it is beneficial to keep a BUILD_VECTOR |
| 7135 | /// node as a BUILD_VECTOR node rather than expanding it. The patterns where |
| 7136 | /// the opposite is true (expansion is beneficial) are: |
| 7137 | /// - The node builds a vector out of integers that are not 32 or 64-bits |
| 7138 | /// - The node builds a vector out of constants |
| 7139 | /// - The node is a "load-and-splat" |
| 7140 | /// In all other cases, we will choose to keep the BUILD_VECTOR. |
| 7141 | static bool haveEfficientBuildVectorPattern(BuildVectorSDNode *V, |
| 7142 | bool HasDirectMove) { |
| 7143 | EVT VecVT = V->getValueType(0); |
| 7144 | bool RightType = VecVT == MVT::v2f64 || VecVT == MVT::v4f32 || |
| 7145 | (HasDirectMove && (VecVT == MVT::v2i64 || VecVT == MVT::v4i32)); |
| 7146 | if (!RightType) |
| Nemanja Ivanovic | d2c3c51 | 2016-09-23 13:25:31 +0000 | [diff] [blame] | 7147 | return false; |
| Nemanja Ivanovic | df1cb52 | 2016-11-29 16:11:34 +0000 | [diff] [blame] | 7148 | |
| 7149 | bool IsSplat = true; |
| 7150 | bool IsLoad = false; |
| 7151 | SDValue Op0 = V->getOperand(0); |
| 7152 | |
| 7153 | // This function is called in a block that confirms the node is not a constant |
| 7154 | // splat. So a constant BUILD_VECTOR here means the vector is built out of |
| 7155 | // different constants. |
| 7156 | if (V->isConstant()) |
| 7157 | return false; |
| 7158 | for (int i = 0, e = V->getNumOperands(); i < e; ++i) { |
| 7159 | if (V->getOperand(i).isUndef()) |
| Nemanja Ivanovic | d2c3c51 | 2016-09-23 13:25:31 +0000 | [diff] [blame] | 7160 | return false; |
| Nemanja Ivanovic | df1cb52 | 2016-11-29 16:11:34 +0000 | [diff] [blame] | 7161 | // We want to expand nodes that represent load-and-splat even if the |
| 7162 | // loaded value is a floating point truncation or conversion to int. |
| 7163 | if (V->getOperand(i).getOpcode() == ISD::LOAD || |
| 7164 | (V->getOperand(i).getOpcode() == ISD::FP_ROUND && |
| 7165 | V->getOperand(i).getOperand(0).getOpcode() == ISD::LOAD) || |
| 7166 | (V->getOperand(i).getOpcode() == ISD::FP_TO_SINT && |
| 7167 | V->getOperand(i).getOperand(0).getOpcode() == ISD::LOAD) || |
| 7168 | (V->getOperand(i).getOpcode() == ISD::FP_TO_UINT && |
| 7169 | V->getOperand(i).getOperand(0).getOpcode() == ISD::LOAD)) |
| 7170 | IsLoad = true; |
| 7171 | // If the operands are different or the input is not a load and has more |
| 7172 | // uses than just this BV node, then it isn't a splat. |
| 7173 | if (V->getOperand(i) != Op0 || |
| 7174 | (!IsLoad && !V->isOnlyUserOf(V->getOperand(i).getNode()))) |
| 7175 | IsSplat = false; |
| 7176 | } |
| 7177 | return !(IsSplat && IsLoad); |
| Nemanja Ivanovic | d2c3c51 | 2016-09-23 13:25:31 +0000 | [diff] [blame] | 7178 | } |
| 7179 | |
| Chris Lattner | 19e9055 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 7180 | // If this is a case we can't handle, return null and let the default |
| 7181 | // expansion code take care of it. If we CAN select this case, and if it |
| 7182 | // selects to a single instruction, return Op. Otherwise, if we can codegen |
| 7183 | // this case more efficiently than a constant pool load, lower it to the |
| 7184 | // sequence of ops that should be used. |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 7185 | SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op, |
| 7186 | SelectionDAG &DAG) const { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7187 | SDLoc dl(Op); |
| Bob Wilson | d8ea0e1 | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 7188 | BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode()); |
| Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 7189 | assert(BVN && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR"); |
| Scott Michel | bb87828 | 2009-02-25 03:12:50 +0000 | [diff] [blame] | 7190 | |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7191 | if (Subtarget.hasQPX() && Op.getValueType() == MVT::v4i1) { |
| 7192 | // We first build an i32 vector, load it into a QPX register, |
| 7193 | // then convert it to a floating-point vector and compare it |
| 7194 | // to a zero vector to get the boolean result. |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 7195 | MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); |
| 7196 | int FrameIdx = MFI.CreateStackObject(16, 16, false); |
| Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 7197 | MachinePointerInfo PtrInfo = |
| 7198 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx); |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 7199 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7200 | SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); |
| 7201 | |
| 7202 | assert(BVN->getNumOperands() == 4 && |
| 7203 | "BUILD_VECTOR for v4i1 does not have 4 operands"); |
| 7204 | |
| 7205 | bool IsConst = true; |
| 7206 | for (unsigned i = 0; i < 4; ++i) { |
| Sanjay Patel | 5719584 | 2016-03-14 17:28:46 +0000 | [diff] [blame] | 7207 | if (BVN->getOperand(i).isUndef()) continue; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7208 | if (!isa<ConstantSDNode>(BVN->getOperand(i))) { |
| 7209 | IsConst = false; |
| 7210 | break; |
| 7211 | } |
| 7212 | } |
| 7213 | |
| 7214 | if (IsConst) { |
| 7215 | Constant *One = |
| 7216 | ConstantFP::get(Type::getFloatTy(*DAG.getContext()), 1.0); |
| 7217 | Constant *NegOne = |
| 7218 | ConstantFP::get(Type::getFloatTy(*DAG.getContext()), -1.0); |
| 7219 | |
| Benjamin Kramer | 1d67ac5 | 2016-06-17 13:15:10 +0000 | [diff] [blame] | 7220 | Constant *CV[4]; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7221 | for (unsigned i = 0; i < 4; ++i) { |
| Sanjay Patel | 5719584 | 2016-03-14 17:28:46 +0000 | [diff] [blame] | 7222 | if (BVN->getOperand(i).isUndef()) |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7223 | CV[i] = UndefValue::get(Type::getFloatTy(*DAG.getContext())); |
| Artyom Skrobov | 314ee04 | 2015-11-25 19:41:11 +0000 | [diff] [blame] | 7224 | else if (isNullConstant(BVN->getOperand(i))) |
| Benjamin Kramer | 1d67ac5 | 2016-06-17 13:15:10 +0000 | [diff] [blame] | 7225 | CV[i] = NegOne; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7226 | else |
| 7227 | CV[i] = One; |
| 7228 | } |
| 7229 | |
| 7230 | Constant *CP = ConstantVector::get(CV); |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 7231 | SDValue CPIdx = DAG.getConstantPool(CP, getPointerTy(DAG.getDataLayout()), |
| 7232 | 16 /* alignment */); |
| 7233 | |
| Benjamin Kramer | 1d67ac5 | 2016-06-17 13:15:10 +0000 | [diff] [blame] | 7234 | SDValue Ops[] = {DAG.getEntryNode(), CPIdx}; |
| 7235 | SDVTList VTs = DAG.getVTList({MVT::v4i1, /*chain*/ MVT::Other}); |
| Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 7236 | return DAG.getMemIntrinsicNode( |
| 7237 | PPCISD::QVLFSb, dl, VTs, Ops, MVT::v4f32, |
| 7238 | MachinePointerInfo::getConstantPool(DAG.getMachineFunction())); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7239 | } |
| 7240 | |
| 7241 | SmallVector<SDValue, 4> Stores; |
| 7242 | for (unsigned i = 0; i < 4; ++i) { |
| Sanjay Patel | 5719584 | 2016-03-14 17:28:46 +0000 | [diff] [blame] | 7243 | if (BVN->getOperand(i).isUndef()) continue; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7244 | |
| 7245 | unsigned Offset = 4*i; |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7246 | SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType()); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7247 | Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx); |
| 7248 | |
| 7249 | unsigned StoreSize = BVN->getOperand(i).getValueType().getStoreSize(); |
| 7250 | if (StoreSize > 4) { |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 7251 | Stores.push_back( |
| 7252 | DAG.getTruncStore(DAG.getEntryNode(), dl, BVN->getOperand(i), Idx, |
| 7253 | PtrInfo.getWithOffset(Offset), MVT::i32)); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7254 | } else { |
| 7255 | SDValue StoreValue = BVN->getOperand(i); |
| 7256 | if (StoreSize < 4) |
| 7257 | StoreValue = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, StoreValue); |
| 7258 | |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 7259 | Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, StoreValue, Idx, |
| 7260 | PtrInfo.getWithOffset(Offset))); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7261 | } |
| 7262 | } |
| 7263 | |
| 7264 | SDValue StoreChain; |
| 7265 | if (!Stores.empty()) |
| 7266 | StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); |
| 7267 | else |
| 7268 | StoreChain = DAG.getEntryNode(); |
| 7269 | |
| 7270 | // Now load from v4i32 into the QPX register; this will extend it to |
| 7271 | // v4i64 but not yet convert it to a floating point. Nevertheless, this |
| 7272 | // is typed as v4f64 because the QPX register integer states are not |
| 7273 | // explicitly represented. |
| 7274 | |
| Benjamin Kramer | 1d67ac5 | 2016-06-17 13:15:10 +0000 | [diff] [blame] | 7275 | SDValue Ops[] = {StoreChain, |
| 7276 | DAG.getConstant(Intrinsic::ppc_qpx_qvlfiwz, dl, MVT::i32), |
| 7277 | FIdx}; |
| 7278 | SDVTList VTs = DAG.getVTList({MVT::v4f64, /*chain*/ MVT::Other}); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7279 | |
| 7280 | SDValue LoadedVect = DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, |
| 7281 | dl, VTs, Ops, MVT::v4i32, PtrInfo); |
| 7282 | LoadedVect = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7283 | DAG.getConstant(Intrinsic::ppc_qpx_qvfcfidu, dl, MVT::i32), |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7284 | LoadedVect); |
| 7285 | |
| Ahmed Bougacha | 93cff7f | 2016-02-15 18:07:29 +0000 | [diff] [blame] | 7286 | SDValue FPZeros = DAG.getConstantFP(0.0, dl, MVT::v4f64); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7287 | |
| 7288 | return DAG.getSetCC(dl, MVT::v4i1, LoadedVect, FPZeros, ISD::SETEQ); |
| 7289 | } |
| 7290 | |
| 7291 | // All other QPX vectors are handled by generic code. |
| 7292 | if (Subtarget.hasQPX()) |
| 7293 | return SDValue(); |
| 7294 | |
| Bob Wilson | 85cefe8 | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 7295 | // Check if this is a splat of a constant value. |
| 7296 | APInt APSplatBits, APSplatUndef; |
| 7297 | unsigned SplatBitSize; |
| Bob Wilson | d8ea0e1 | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 7298 | bool HasAnyUndefs; |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7299 | if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize, |
| Bill Schmidt | 91dd765 | 2015-04-03 13:48:24 +0000 | [diff] [blame] | 7300 | HasAnyUndefs, 0, !Subtarget.isLittleEndian()) || |
| Nemanja Ivanovic | d2c3c51 | 2016-09-23 13:25:31 +0000 | [diff] [blame] | 7301 | SplatBitSize > 32) { |
| Nemanja Ivanovic | df1cb52 | 2016-11-29 16:11:34 +0000 | [diff] [blame] | 7302 | // BUILD_VECTOR nodes that are not constant splats of up to 32-bits can be |
| 7303 | // lowered to VSX instructions under certain conditions. |
| 7304 | // Without VSX, there is no pattern more efficient than expanding the node. |
| 7305 | if (Subtarget.hasVSX() && |
| 7306 | haveEfficientBuildVectorPattern(BVN, Subtarget.hasDirectMove())) |
| Nemanja Ivanovic | d2c3c51 | 2016-09-23 13:25:31 +0000 | [diff] [blame] | 7307 | return Op; |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7308 | return SDValue(); |
| Nemanja Ivanovic | d2c3c51 | 2016-09-23 13:25:31 +0000 | [diff] [blame] | 7309 | } |
| Evan Cheng | a49de9d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 7310 | |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7311 | unsigned SplatBits = APSplatBits.getZExtValue(); |
| 7312 | unsigned SplatUndef = APSplatUndef.getZExtValue(); |
| 7313 | unsigned SplatSize = SplatBitSize / 8; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7314 | |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7315 | // First, handle single instruction cases. |
| 7316 | |
| 7317 | // All zeros? |
| 7318 | if (SplatBits == 0) { |
| 7319 | // Canonicalize all zero vectors to be v4i32. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7320 | if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) { |
| Ahmed Bougacha | 93cff7f | 2016-02-15 18:07:29 +0000 | [diff] [blame] | 7321 | SDValue Z = DAG.getConstant(0, dl, MVT::v4i32); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7322 | Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z); |
| Chris Lattner | 19e9055 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 7323 | } |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7324 | return Op; |
| 7325 | } |
| Chris Lattner | fa5aa39 | 2006-04-16 01:01:29 +0000 | [diff] [blame] | 7326 | |
| Nemanja Ivanovic | d2c3c51 | 2016-09-23 13:25:31 +0000 | [diff] [blame] | 7327 | // We have XXSPLTIB for constant splats one byte wide |
| Nemanja Ivanovic | df1cb52 | 2016-11-29 16:11:34 +0000 | [diff] [blame] | 7328 | if (Subtarget.hasP9Vector() && SplatSize == 1) { |
| 7329 | // This is a splat of 1-byte elements with some elements potentially undef. |
| 7330 | // Rather than trying to match undef in the SDAG patterns, ensure that all |
| 7331 | // elements are the same constant. |
| 7332 | if (HasAnyUndefs || ISD::isBuildVectorAllOnes(BVN)) { |
| 7333 | SmallVector<SDValue, 16> Ops(16, DAG.getConstant(SplatBits, |
| 7334 | dl, MVT::i32)); |
| 7335 | SDValue NewBV = DAG.getBuildVector(MVT::v16i8, dl, Ops); |
| 7336 | if (Op.getValueType() != MVT::v16i8) |
| 7337 | return DAG.getBitcast(Op.getValueType(), NewBV); |
| 7338 | return NewBV; |
| 7339 | } |
| Nemanja Ivanovic | d2c3c51 | 2016-09-23 13:25:31 +0000 | [diff] [blame] | 7340 | return Op; |
| Nemanja Ivanovic | df1cb52 | 2016-11-29 16:11:34 +0000 | [diff] [blame] | 7341 | } |
| Nemanja Ivanovic | d2c3c51 | 2016-09-23 13:25:31 +0000 | [diff] [blame] | 7342 | |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7343 | // If the sign extended value is in the range [-16,15], use VSPLTI[bhw]. |
| 7344 | int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >> |
| 7345 | (32-SplatBitSize)); |
| 7346 | if (SextVal >= -16 && SextVal <= 15) |
| 7347 | return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7348 | |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7349 | // Two instruction sequences. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7350 | |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7351 | // If this value is in the range [-32,30] and is even, use: |
| Bill Schmidt | c6cbecc | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 7352 | // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2) |
| 7353 | // If this value is in the range [17,31] and is odd, use: |
| 7354 | // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16) |
| 7355 | // If this value is in the range [-31,-17] and is odd, use: |
| 7356 | // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16) |
| 7357 | // Note the last two are three-instruction sequences. |
| 7358 | if (SextVal >= -32 && SextVal <= 31) { |
| 7359 | // To avoid having these optimizations undone by constant folding, |
| 7360 | // we convert to a pseudo that will be expanded later into one of |
| 7361 | // the above forms. |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7362 | SDValue Elt = DAG.getConstant(SextVal, dl, MVT::i32); |
| Bill Schmidt | 71dddd5 | 2014-05-27 15:57:51 +0000 | [diff] [blame] | 7363 | EVT VT = (SplatSize == 1 ? MVT::v16i8 : |
| 7364 | (SplatSize == 2 ? MVT::v8i16 : MVT::v4i32)); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7365 | SDValue EltSize = DAG.getConstant(SplatSize, dl, MVT::i32); |
| Bill Schmidt | 71dddd5 | 2014-05-27 15:57:51 +0000 | [diff] [blame] | 7366 | SDValue RetVal = DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize); |
| 7367 | if (VT == Op.getValueType()) |
| 7368 | return RetVal; |
| 7369 | else |
| 7370 | return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), RetVal); |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7371 | } |
| 7372 | |
| 7373 | // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is |
| 7374 | // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important |
| 7375 | // for fneg/fabs. |
| 7376 | if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) { |
| 7377 | // Make -1 and vspltisw -1: |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7378 | SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl); |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7379 | |
| 7380 | // Make the VSLW intrinsic, computing 0x8000_0000. |
| 7381 | SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV, |
| 7382 | OnesV, DAG, dl); |
| 7383 | |
| 7384 | // xor by OnesV to invert it. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7385 | Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7386 | return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7387 | } |
| 7388 | |
| 7389 | // Check to see if this is a wide variety of vsplti*, binop self cases. |
| 7390 | static const signed char SplatCsts[] = { |
| 7391 | -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7, |
| 7392 | -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16 |
| 7393 | }; |
| 7394 | |
| 7395 | for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) { |
| 7396 | // Indirect through the SplatCsts array so that we favor 'vsplti -1' for |
| 7397 | // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1' |
| 7398 | int i = SplatCsts[idx]; |
| 7399 | |
| 7400 | // Figure out what shift amount will be used by altivec if shifted by i in |
| 7401 | // this splat size. |
| 7402 | unsigned TypeShiftAmt = i & (SplatBitSize-1); |
| 7403 | |
| 7404 | // vsplti + shl self. |
| Richard Smith | 228e6d4 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 7405 | if (SextVal == (int)((unsigned)i << TypeShiftAmt)) { |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7406 | SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7407 | static const unsigned IIDs[] = { // Intrinsic to use for each size. |
| 7408 | Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0, |
| 7409 | Intrinsic::ppc_altivec_vslw |
| 7410 | }; |
| 7411 | Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7412 | return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); |
| Chris Lattner | 2a099c0 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 7413 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7414 | |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7415 | // vsplti + srl self. |
| 7416 | if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) { |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7417 | SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7418 | static const unsigned IIDs[] = { // Intrinsic to use for each size. |
| 7419 | Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0, |
| 7420 | Intrinsic::ppc_altivec_vsrw |
| 7421 | }; |
| 7422 | Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7423 | return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); |
| Chris Lattner | 1b3806a | 2006-04-17 06:58:41 +0000 | [diff] [blame] | 7424 | } |
| 7425 | |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7426 | // vsplti + sra self. |
| 7427 | if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) { |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7428 | SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7429 | static const unsigned IIDs[] = { // Intrinsic to use for each size. |
| 7430 | Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0, |
| 7431 | Intrinsic::ppc_altivec_vsraw |
| 7432 | }; |
| 7433 | Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7434 | return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); |
| Chris Lattner | 1b3806a | 2006-04-17 06:58:41 +0000 | [diff] [blame] | 7435 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7436 | |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7437 | // vsplti + rol self. |
| 7438 | if (SextVal == (int)(((unsigned)i << TypeShiftAmt) | |
| 7439 | ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) { |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7440 | SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7441 | static const unsigned IIDs[] = { // Intrinsic to use for each size. |
| 7442 | Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0, |
| 7443 | Intrinsic::ppc_altivec_vrlw |
| 7444 | }; |
| 7445 | Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7446 | return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7447 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7448 | |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7449 | // t = vsplti c, result = vsldoi t, t, 1 |
| Richard Smith | 228e6d4 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 7450 | if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) { |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7451 | SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl); |
| Bill Schmidt | 1e77bb1 | 2015-07-15 15:45:30 +0000 | [diff] [blame] | 7452 | unsigned Amt = Subtarget.isLittleEndian() ? 15 : 1; |
| 7453 | return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl); |
| Chris Lattner | e54133c | 2006-04-17 18:09:22 +0000 | [diff] [blame] | 7454 | } |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7455 | // t = vsplti c, result = vsldoi t, t, 2 |
| Richard Smith | 228e6d4 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 7456 | if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) { |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7457 | SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl); |
| Bill Schmidt | 1e77bb1 | 2015-07-15 15:45:30 +0000 | [diff] [blame] | 7458 | unsigned Amt = Subtarget.isLittleEndian() ? 14 : 2; |
| 7459 | return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl); |
| Chris Lattner | 19e9055 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 7460 | } |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7461 | // t = vsplti c, result = vsldoi t, t, 3 |
| Richard Smith | 228e6d4 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 7462 | if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) { |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7463 | SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl); |
| Bill Schmidt | 1e77bb1 | 2015-07-15 15:45:30 +0000 | [diff] [blame] | 7464 | unsigned Amt = Subtarget.isLittleEndian() ? 13 : 3; |
| 7465 | return BuildVSLDOI(T, T, Amt, Op.getValueType(), DAG, dl); |
| Bob Wilson | 530e038 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 7466 | } |
| 7467 | } |
| 7468 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7469 | return SDValue(); |
| Chris Lattner | 19e9055 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 7470 | } |
| 7471 | |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7472 | /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit |
| 7473 | /// the specified operations to build the shuffle. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7474 | static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS, |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7475 | SDValue RHS, SelectionDAG &DAG, |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 7476 | const SDLoc &dl) { |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7477 | unsigned OpNum = (PFEntry >> 26) & 0x0F; |
| Bill Wendling | 95e1af2 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 7478 | unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1); |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7479 | unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7480 | |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7481 | enum { |
| Chris Lattner | d2ca9ab | 2006-05-16 04:20:24 +0000 | [diff] [blame] | 7482 | OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3> |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7483 | OP_VMRGHW, |
| 7484 | OP_VMRGLW, |
| 7485 | OP_VSPLTISW0, |
| 7486 | OP_VSPLTISW1, |
| 7487 | OP_VSPLTISW2, |
| 7488 | OP_VSPLTISW3, |
| 7489 | OP_VSLDOI4, |
| 7490 | OP_VSLDOI8, |
| Chris Lattner | aa237256 | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 7491 | OP_VSLDOI12 |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7492 | }; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7493 | |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7494 | if (OpNum == OP_COPY) { |
| 7495 | if (LHSID == (1*9+2)*9+3) return LHS; |
| 7496 | assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!"); |
| 7497 | return RHS; |
| 7498 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7499 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7500 | SDValue OpLHS, OpRHS; |
| Dale Johannesen | 9f3f72f | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 7501 | OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl); |
| 7502 | OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7503 | |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7504 | int ShufIdxs[16]; |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7505 | switch (OpNum) { |
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 7506 | default: llvm_unreachable("Unknown i32 permute!"); |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7507 | case OP_VMRGHW: |
| 7508 | ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3; |
| 7509 | ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19; |
| 7510 | ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7; |
| 7511 | ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23; |
| 7512 | break; |
| 7513 | case OP_VMRGLW: |
| 7514 | ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11; |
| 7515 | ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27; |
| 7516 | ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15; |
| 7517 | ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31; |
| 7518 | break; |
| 7519 | case OP_VSPLTISW0: |
| 7520 | for (unsigned i = 0; i != 16; ++i) |
| 7521 | ShufIdxs[i] = (i&3)+0; |
| 7522 | break; |
| 7523 | case OP_VSPLTISW1: |
| 7524 | for (unsigned i = 0; i != 16; ++i) |
| 7525 | ShufIdxs[i] = (i&3)+4; |
| 7526 | break; |
| 7527 | case OP_VSPLTISW2: |
| 7528 | for (unsigned i = 0; i != 16; ++i) |
| 7529 | ShufIdxs[i] = (i&3)+8; |
| 7530 | break; |
| 7531 | case OP_VSPLTISW3: |
| 7532 | for (unsigned i = 0; i != 16; ++i) |
| 7533 | ShufIdxs[i] = (i&3)+12; |
| 7534 | break; |
| 7535 | case OP_VSLDOI4: |
| Dale Johannesen | 9f3f72f | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 7536 | return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl); |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7537 | case OP_VSLDOI8: |
| Dale Johannesen | 9f3f72f | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 7538 | return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl); |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7539 | case OP_VSLDOI12: |
| Dale Johannesen | 9f3f72f | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 7540 | return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl); |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7541 | } |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7542 | EVT VT = OpLHS.getValueType(); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7543 | OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS); |
| 7544 | OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7545 | SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7546 | return DAG.getNode(ISD::BITCAST, dl, VT, T); |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7547 | } |
| 7548 | |
| Chris Lattner | 19e9055 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 7549 | /// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this |
| 7550 | /// is a shuffle we can handle in a single instruction, return it. Otherwise, |
| 7551 | /// return the code it can be lowered into. Worst case, it can always be |
| 7552 | /// lowered into a vperm. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7553 | SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 7554 | SelectionDAG &DAG) const { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7555 | SDLoc dl(Op); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7556 | SDValue V1 = Op.getOperand(0); |
| 7557 | SDValue V2 = Op.getOperand(1); |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7558 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7559 | EVT VT = Op.getValueType(); |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 7560 | bool isLittleEndian = Subtarget.isLittleEndian(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7561 | |
| Nemanja Ivanovic | b43bb61 | 2016-07-12 21:00:10 +0000 | [diff] [blame] | 7562 | unsigned ShiftElts, InsertAtByte; |
| 7563 | bool Swap; |
| 7564 | if (Subtarget.hasP9Vector() && |
| 7565 | PPC::isXXINSERTWMask(SVOp, ShiftElts, InsertAtByte, Swap, |
| 7566 | isLittleEndian)) { |
| 7567 | if (Swap) |
| 7568 | std::swap(V1, V2); |
| 7569 | SDValue Conv1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1); |
| 7570 | SDValue Conv2 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V2); |
| 7571 | if (ShiftElts) { |
| 7572 | SDValue Shl = DAG.getNode(PPCISD::VECSHL, dl, MVT::v4i32, Conv2, Conv2, |
| 7573 | DAG.getConstant(ShiftElts, dl, MVT::i32)); |
| 7574 | SDValue Ins = DAG.getNode(PPCISD::XXINSERT, dl, MVT::v4i32, Conv1, Shl, |
| 7575 | DAG.getConstant(InsertAtByte, dl, MVT::i32)); |
| 7576 | return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Ins); |
| 7577 | } |
| 7578 | SDValue Ins = DAG.getNode(PPCISD::XXINSERT, dl, MVT::v4i32, Conv1, Conv2, |
| 7579 | DAG.getConstant(InsertAtByte, dl, MVT::i32)); |
| 7580 | return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Ins); |
| 7581 | } |
| 7582 | |
| Nemanja Ivanovic | 1a2b2f0 | 2016-05-04 16:04:02 +0000 | [diff] [blame] | 7583 | if (Subtarget.hasVSX()) { |
| 7584 | if (V2.isUndef() && PPC::isSplatShuffleMask(SVOp, 4)) { |
| 7585 | int SplatIdx = PPC::getVSPLTImmediate(SVOp, 4, DAG); |
| Nemanja Ivanovic | d2c3c51 | 2016-09-23 13:25:31 +0000 | [diff] [blame] | 7586 | |
| 7587 | // If the source for the shuffle is a scalar_to_vector that came from a |
| 7588 | // 32-bit load, it will have used LXVWSX so we don't need to splat again. |
| Nemanja Ivanovic | df1cb52 | 2016-11-29 16:11:34 +0000 | [diff] [blame] | 7589 | if (Subtarget.hasP9Vector() && |
| Nemanja Ivanovic | d2c3c51 | 2016-09-23 13:25:31 +0000 | [diff] [blame] | 7590 | ((isLittleEndian && SplatIdx == 3) || |
| 7591 | (!isLittleEndian && SplatIdx == 0))) { |
| 7592 | SDValue Src = V1.getOperand(0); |
| 7593 | if (Src.getOpcode() == ISD::SCALAR_TO_VECTOR && |
| 7594 | Src.getOperand(0).getOpcode() == ISD::LOAD && |
| 7595 | Src.getOperand(0).hasOneUse()) |
| 7596 | return V1; |
| 7597 | } |
| Nemanja Ivanovic | 1a2b2f0 | 2016-05-04 16:04:02 +0000 | [diff] [blame] | 7598 | SDValue Conv = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1); |
| 7599 | SDValue Splat = DAG.getNode(PPCISD::XXSPLT, dl, MVT::v4i32, Conv, |
| 7600 | DAG.getConstant(SplatIdx, dl, MVT::i32)); |
| 7601 | return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Splat); |
| 7602 | } |
| Nemanja Ivanovic | eebbcb6 | 2016-07-12 12:16:27 +0000 | [diff] [blame] | 7603 | |
| 7604 | // Left shifts of 8 bytes are actually swaps. Convert accordingly. |
| 7605 | if (V2.isUndef() && PPC::isVSLDOIShuffleMask(SVOp, 1, DAG) == 8) { |
| 7606 | SDValue Conv = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1); |
| 7607 | SDValue Swap = DAG.getNode(PPCISD::SWAP_NO_CHAIN, dl, MVT::v2f64, Conv); |
| 7608 | return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, Swap); |
| 7609 | } |
| 7610 | |
| Nemanja Ivanovic | 1a2b2f0 | 2016-05-04 16:04:02 +0000 | [diff] [blame] | 7611 | } |
| 7612 | |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7613 | if (Subtarget.hasQPX()) { |
| 7614 | if (VT.getVectorNumElements() != 4) |
| 7615 | return SDValue(); |
| 7616 | |
| Sanjay Patel | 5719584 | 2016-03-14 17:28:46 +0000 | [diff] [blame] | 7617 | if (V2.isUndef()) V2 = V1; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7618 | |
| 7619 | int AlignIdx = PPC::isQVALIGNIShuffleMask(SVOp); |
| 7620 | if (AlignIdx != -1) { |
| 7621 | return DAG.getNode(PPCISD::QVALIGNI, dl, VT, V1, V2, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7622 | DAG.getConstant(AlignIdx, dl, MVT::i32)); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7623 | } else if (SVOp->isSplat()) { |
| 7624 | int SplatIdx = SVOp->getSplatIndex(); |
| 7625 | if (SplatIdx >= 4) { |
| 7626 | std::swap(V1, V2); |
| 7627 | SplatIdx -= 4; |
| 7628 | } |
| 7629 | |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7630 | return DAG.getNode(PPCISD::QVESPLATI, dl, VT, V1, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7631 | DAG.getConstant(SplatIdx, dl, MVT::i32)); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7632 | } |
| 7633 | |
| 7634 | // Lower this into a qvgpci/qvfperm pair. |
| 7635 | |
| 7636 | // Compute the qvgpci literal |
| 7637 | unsigned idx = 0; |
| 7638 | for (unsigned i = 0; i < 4; ++i) { |
| 7639 | int m = SVOp->getMaskElt(i); |
| 7640 | unsigned mm = m >= 0 ? (unsigned) m : i; |
| 7641 | idx |= mm << (3-i)*3; |
| 7642 | } |
| 7643 | |
| 7644 | SDValue V3 = DAG.getNode(PPCISD::QVGPCI, dl, MVT::v4f64, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7645 | DAG.getConstant(idx, dl, MVT::i32)); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 7646 | return DAG.getNode(PPCISD::QVFPERM, dl, VT, V1, V2, V3); |
| 7647 | } |
| 7648 | |
| Chris Lattner | 19e9055 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 7649 | // Cases that are handled by instructions that take permute immediates |
| 7650 | // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be |
| 7651 | // selected by the instruction selector. |
| Sanjay Patel | 5719584 | 2016-03-14 17:28:46 +0000 | [diff] [blame] | 7652 | if (V2.isUndef()) { |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7653 | if (PPC::isSplatShuffleMask(SVOp, 1) || |
| 7654 | PPC::isSplatShuffleMask(SVOp, 2) || |
| 7655 | PPC::isSplatShuffleMask(SVOp, 4) || |
| Ulrich Weigand | cc9909b | 2014-08-04 13:53:40 +0000 | [diff] [blame] | 7656 | PPC::isVPKUWUMShuffleMask(SVOp, 1, DAG) || |
| 7657 | PPC::isVPKUHUMShuffleMask(SVOp, 1, DAG) || |
| Bill Schmidt | 42a6936 | 2014-08-05 20:47:25 +0000 | [diff] [blame] | 7658 | PPC::isVSLDOIShuffleMask(SVOp, 1, DAG) != -1 || |
| Bill Schmidt | c9fa5dd | 2014-07-25 01:55:55 +0000 | [diff] [blame] | 7659 | PPC::isVMRGLShuffleMask(SVOp, 1, 1, DAG) || |
| 7660 | PPC::isVMRGLShuffleMask(SVOp, 2, 1, DAG) || |
| 7661 | PPC::isVMRGLShuffleMask(SVOp, 4, 1, DAG) || |
| 7662 | PPC::isVMRGHShuffleMask(SVOp, 1, 1, DAG) || |
| 7663 | PPC::isVMRGHShuffleMask(SVOp, 2, 1, DAG) || |
| Kit Barton | 13894c7 | 2015-06-25 15:17:40 +0000 | [diff] [blame] | 7664 | PPC::isVMRGHShuffleMask(SVOp, 4, 1, DAG) || |
| Hal Finkel | 77c8b7f | 2015-09-02 16:52:37 +0000 | [diff] [blame] | 7665 | (Subtarget.hasP8Altivec() && ( |
| 7666 | PPC::isVPKUDUMShuffleMask(SVOp, 1, DAG) || |
| 7667 | PPC::isVMRGEOShuffleMask(SVOp, true, 1, DAG) || |
| 7668 | PPC::isVMRGEOShuffleMask(SVOp, false, 1, DAG)))) { |
| Chris Lattner | 19e9055 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 7669 | return Op; |
| 7670 | } |
| 7671 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7672 | |
| Chris Lattner | 19e9055 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 7673 | // Altivec has a variety of "shuffle immediates" that take two vector inputs |
| 7674 | // and produce a fixed permutation. If any of these match, do not lower to |
| 7675 | // VPERM. |
| Bill Schmidt | c9fa5dd | 2014-07-25 01:55:55 +0000 | [diff] [blame] | 7676 | unsigned int ShuffleKind = isLittleEndian ? 2 : 0; |
| Ulrich Weigand | cc9909b | 2014-08-04 13:53:40 +0000 | [diff] [blame] | 7677 | if (PPC::isVPKUWUMShuffleMask(SVOp, ShuffleKind, DAG) || |
| 7678 | PPC::isVPKUHUMShuffleMask(SVOp, ShuffleKind, DAG) || |
| Bill Schmidt | 42a6936 | 2014-08-05 20:47:25 +0000 | [diff] [blame] | 7679 | PPC::isVSLDOIShuffleMask(SVOp, ShuffleKind, DAG) != -1 || |
| Bill Schmidt | c9fa5dd | 2014-07-25 01:55:55 +0000 | [diff] [blame] | 7680 | PPC::isVMRGLShuffleMask(SVOp, 1, ShuffleKind, DAG) || |
| 7681 | PPC::isVMRGLShuffleMask(SVOp, 2, ShuffleKind, DAG) || |
| 7682 | PPC::isVMRGLShuffleMask(SVOp, 4, ShuffleKind, DAG) || |
| 7683 | PPC::isVMRGHShuffleMask(SVOp, 1, ShuffleKind, DAG) || |
| 7684 | PPC::isVMRGHShuffleMask(SVOp, 2, ShuffleKind, DAG) || |
| Kit Barton | 13894c7 | 2015-06-25 15:17:40 +0000 | [diff] [blame] | 7685 | PPC::isVMRGHShuffleMask(SVOp, 4, ShuffleKind, DAG) || |
| Hal Finkel | 77c8b7f | 2015-09-02 16:52:37 +0000 | [diff] [blame] | 7686 | (Subtarget.hasP8Altivec() && ( |
| 7687 | PPC::isVPKUDUMShuffleMask(SVOp, ShuffleKind, DAG) || |
| 7688 | PPC::isVMRGEOShuffleMask(SVOp, true, ShuffleKind, DAG) || |
| 7689 | PPC::isVMRGEOShuffleMask(SVOp, false, ShuffleKind, DAG)))) |
| Chris Lattner | 19e9055 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 7690 | return Op; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7691 | |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7692 | // Check to see if this is a shuffle of 4-byte values. If so, we can use our |
| 7693 | // perfect shuffle table to emit an optimal matching sequence. |
| Benjamin Kramer | 339ced4 | 2012-01-15 13:16:05 +0000 | [diff] [blame] | 7694 | ArrayRef<int> PermMask = SVOp->getMask(); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7695 | |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7696 | unsigned PFIndexes[4]; |
| 7697 | bool isFourElementShuffle = true; |
| 7698 | for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number |
| 7699 | unsigned EltNo = 8; // Start out undef. |
| 7700 | for (unsigned j = 0; j != 4; ++j) { // Intra-element byte. |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7701 | if (PermMask[i*4+j] < 0) |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7702 | continue; // Undef, ignore it. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7703 | |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7704 | unsigned ByteSource = PermMask[i*4+j]; |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7705 | if ((ByteSource & 3) != j) { |
| 7706 | isFourElementShuffle = false; |
| 7707 | break; |
| 7708 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7709 | |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7710 | if (EltNo == 8) { |
| 7711 | EltNo = ByteSource/4; |
| 7712 | } else if (EltNo != ByteSource/4) { |
| 7713 | isFourElementShuffle = false; |
| 7714 | break; |
| 7715 | } |
| 7716 | } |
| 7717 | PFIndexes[i] = EltNo; |
| 7718 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7719 | |
| 7720 | // If this shuffle can be expressed as a shuffle of 4-byte elements, use the |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7721 | // perfect shuffle vector to determine if it is cost effective to do this as |
| 7722 | // discrete instructions, or whether we should use a vperm. |
| Bill Schmidt | f910a06 | 2014-06-10 14:35:01 +0000 | [diff] [blame] | 7723 | // For now, we skip this for little endian until such time as we have a |
| 7724 | // little-endian perfect shuffle table. |
| 7725 | if (isFourElementShuffle && !isLittleEndian) { |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7726 | // Compute the index in the perfect shuffle table. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7727 | unsigned PFTableIndex = |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7728 | PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7729 | |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7730 | unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; |
| 7731 | unsigned Cost = (PFEntry >> 30); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7732 | |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7733 | // Determining when to avoid vperm is tricky. Many things affect the cost |
| 7734 | // of vperm, particularly how many times the perm mask needs to be computed. |
| 7735 | // For example, if the perm mask can be hoisted out of a loop or is already |
| 7736 | // used (perhaps because there are multiple permutes with the same shuffle |
| 7737 | // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of |
| 7738 | // the loop requires an extra register. |
| 7739 | // |
| 7740 | // As a compromise, we only emit discrete instructions if the shuffle can be |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7741 | // generated in 3 or fewer operations. When we have loop information |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7742 | // available, if this block is within a loop, we should avoid using vperm |
| 7743 | // for 3-operation perms and use a constant pool load instead. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7744 | if (Cost < 3) |
| Dale Johannesen | 9f3f72f | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 7745 | return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl); |
| Chris Lattner | 071ad01 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 7746 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7747 | |
| Chris Lattner | 19e9055 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 7748 | // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant |
| 7749 | // vector that will get spilled to the constant pool. |
| Sanjay Patel | 5719584 | 2016-03-14 17:28:46 +0000 | [diff] [blame] | 7750 | if (V2.isUndef()) V2 = V1; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7751 | |
| Chris Lattner | 19e9055 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 7752 | // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except |
| 7753 | // that it is in input element units, not in bytes. Convert now. |
| Bill Schmidt | 4aedff8 | 2014-06-06 14:06:26 +0000 | [diff] [blame] | 7754 | |
| 7755 | // For little endian, the order of the input vectors is reversed, and |
| 7756 | // the permutation mask is complemented with respect to 31. This is |
| 7757 | // necessary to produce proper semantics with the big-endian-biased vperm |
| 7758 | // instruction. |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7759 | EVT EltVT = V1.getValueType().getVectorElementType(); |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7760 | unsigned BytesPerElement = EltVT.getSizeInBits()/8; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7761 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7762 | SmallVector<SDValue, 16> ResultMask; |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7763 | for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) { |
| 7764 | unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i]; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7765 | |
| Chris Lattner | 19e9055 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 7766 | for (unsigned j = 0; j != BytesPerElement; ++j) |
| Bill Schmidt | 4aedff8 | 2014-06-06 14:06:26 +0000 | [diff] [blame] | 7767 | if (isLittleEndian) |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7768 | ResultMask.push_back(DAG.getConstant(31 - (SrcElt*BytesPerElement + j), |
| 7769 | dl, MVT::i32)); |
| Bill Schmidt | 4aedff8 | 2014-06-06 14:06:26 +0000 | [diff] [blame] | 7770 | else |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7771 | ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement + j, dl, |
| Bill Schmidt | 4aedff8 | 2014-06-06 14:06:26 +0000 | [diff] [blame] | 7772 | MVT::i32)); |
| Chris Lattner | 19e9055 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 7773 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7774 | |
| Ahmed Bougacha | 128f873 | 2016-04-26 21:15:30 +0000 | [diff] [blame] | 7775 | SDValue VPermMask = DAG.getBuildVector(MVT::v16i8, dl, ResultMask); |
| Bill Schmidt | 4aedff8 | 2014-06-06 14:06:26 +0000 | [diff] [blame] | 7776 | if (isLittleEndian) |
| 7777 | return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), |
| 7778 | V2, V1, VPermMask); |
| 7779 | else |
| 7780 | return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), |
| 7781 | V1, V2, VPermMask); |
| Chris Lattner | 19e9055 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 7782 | } |
| 7783 | |
| Nemanja Ivanovic | 2c84b29 | 2015-09-29 17:41:53 +0000 | [diff] [blame] | 7784 | /// getVectorCompareInfo - Given an intrinsic, return false if it is not a |
| 7785 | /// vector comparison. If it is, return true and fill in Opc/isDot with |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7786 | /// information about the intrinsic. |
| Nemanja Ivanovic | 2c84b29 | 2015-09-29 17:41:53 +0000 | [diff] [blame] | 7787 | static bool getVectorCompareInfo(SDValue Intrin, int &CompareOpc, |
| 7788 | bool &isDot, const PPCSubtarget &Subtarget) { |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 7789 | unsigned IntrinsicID = |
| 7790 | cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue(); |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7791 | CompareOpc = -1; |
| 7792 | isDot = false; |
| 7793 | switch (IntrinsicID) { |
| 7794 | default: return false; |
| 7795 | // Comparison predicates. |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 7796 | case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break; |
| 7797 | case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break; |
| 7798 | case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break; |
| 7799 | case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break; |
| 7800 | case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break; |
| NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 7801 | case Intrinsic::ppc_altivec_vcmpequd_p: |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 7802 | if (Subtarget.hasP8Altivec()) { |
| NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 7803 | CompareOpc = 199; |
| 7804 | isDot = 1; |
| 7805 | } else |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 7806 | return false; |
| 7807 | |
| 7808 | break; |
| Nemanja Ivanovic | 6f22b41 | 2016-09-27 08:42:12 +0000 | [diff] [blame] | 7809 | case Intrinsic::ppc_altivec_vcmpneb_p: |
| 7810 | case Intrinsic::ppc_altivec_vcmpneh_p: |
| 7811 | case Intrinsic::ppc_altivec_vcmpnew_p: |
| 7812 | case Intrinsic::ppc_altivec_vcmpnezb_p: |
| 7813 | case Intrinsic::ppc_altivec_vcmpnezh_p: |
| 7814 | case Intrinsic::ppc_altivec_vcmpnezw_p: |
| 7815 | if (Subtarget.hasP9Altivec()) { |
| 7816 | switch(IntrinsicID) { |
| 7817 | default: llvm_unreachable("Unknown comparison intrinsic."); |
| 7818 | case Intrinsic::ppc_altivec_vcmpneb_p: CompareOpc = 7; break; |
| 7819 | case Intrinsic::ppc_altivec_vcmpneh_p: CompareOpc = 71; break; |
| 7820 | case Intrinsic::ppc_altivec_vcmpnew_p: CompareOpc = 135; break; |
| 7821 | case Intrinsic::ppc_altivec_vcmpnezb_p: CompareOpc = 263; break; |
| 7822 | case Intrinsic::ppc_altivec_vcmpnezh_p: CompareOpc = 327; break; |
| 7823 | case Intrinsic::ppc_altivec_vcmpnezw_p: CompareOpc = 391; break; |
| 7824 | } |
| 7825 | isDot = 1; |
| 7826 | } else |
| 7827 | return false; |
| 7828 | |
| 7829 | break; |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 7830 | case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break; |
| 7831 | case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break; |
| 7832 | case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break; |
| 7833 | case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break; |
| 7834 | case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break; |
| NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 7835 | case Intrinsic::ppc_altivec_vcmpgtsd_p: |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 7836 | if (Subtarget.hasP8Altivec()) { |
| NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 7837 | CompareOpc = 967; |
| 7838 | isDot = 1; |
| 7839 | } else |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 7840 | return false; |
| 7841 | |
| 7842 | break; |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 7843 | case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break; |
| 7844 | case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break; |
| 7845 | case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break; |
| NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 7846 | case Intrinsic::ppc_altivec_vcmpgtud_p: |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 7847 | if (Subtarget.hasP8Altivec()) { |
| NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 7848 | CompareOpc = 711; |
| 7849 | isDot = 1; |
| 7850 | } else |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 7851 | return false; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7852 | |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 7853 | break; |
| Nemanja Ivanovic | 2c84b29 | 2015-09-29 17:41:53 +0000 | [diff] [blame] | 7854 | // VSX predicate comparisons use the same infrastructure |
| 7855 | case Intrinsic::ppc_vsx_xvcmpeqdp_p: |
| 7856 | case Intrinsic::ppc_vsx_xvcmpgedp_p: |
| 7857 | case Intrinsic::ppc_vsx_xvcmpgtdp_p: |
| 7858 | case Intrinsic::ppc_vsx_xvcmpeqsp_p: |
| 7859 | case Intrinsic::ppc_vsx_xvcmpgesp_p: |
| 7860 | case Intrinsic::ppc_vsx_xvcmpgtsp_p: |
| 7861 | if (Subtarget.hasVSX()) { |
| 7862 | switch (IntrinsicID) { |
| 7863 | case Intrinsic::ppc_vsx_xvcmpeqdp_p: CompareOpc = 99; break; |
| 7864 | case Intrinsic::ppc_vsx_xvcmpgedp_p: CompareOpc = 115; break; |
| 7865 | case Intrinsic::ppc_vsx_xvcmpgtdp_p: CompareOpc = 107; break; |
| 7866 | case Intrinsic::ppc_vsx_xvcmpeqsp_p: CompareOpc = 67; break; |
| 7867 | case Intrinsic::ppc_vsx_xvcmpgesp_p: CompareOpc = 83; break; |
| 7868 | case Intrinsic::ppc_vsx_xvcmpgtsp_p: CompareOpc = 75; break; |
| 7869 | } |
| 7870 | isDot = 1; |
| 7871 | } |
| 7872 | else |
| 7873 | return false; |
| 7874 | |
| 7875 | break; |
| NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 7876 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 7877 | // Normal Comparisons. |
| 7878 | case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break; |
| 7879 | case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break; |
| 7880 | case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break; |
| 7881 | case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break; |
| 7882 | case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break; |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 7883 | case Intrinsic::ppc_altivec_vcmpequd: |
| 7884 | if (Subtarget.hasP8Altivec()) { |
| NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 7885 | CompareOpc = 199; |
| 7886 | isDot = 0; |
| 7887 | } else |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 7888 | return false; |
| 7889 | |
| 7890 | break; |
| Nemanja Ivanovic | 6f22b41 | 2016-09-27 08:42:12 +0000 | [diff] [blame] | 7891 | case Intrinsic::ppc_altivec_vcmpneb: |
| 7892 | case Intrinsic::ppc_altivec_vcmpneh: |
| 7893 | case Intrinsic::ppc_altivec_vcmpnew: |
| 7894 | case Intrinsic::ppc_altivec_vcmpnezb: |
| 7895 | case Intrinsic::ppc_altivec_vcmpnezh: |
| 7896 | case Intrinsic::ppc_altivec_vcmpnezw: |
| 7897 | if (Subtarget.hasP9Altivec()) { |
| 7898 | switch (IntrinsicID) { |
| 7899 | default: llvm_unreachable("Unknown comparison intrinsic."); |
| 7900 | case Intrinsic::ppc_altivec_vcmpneb: CompareOpc = 7; break; |
| 7901 | case Intrinsic::ppc_altivec_vcmpneh: CompareOpc = 71; break; |
| 7902 | case Intrinsic::ppc_altivec_vcmpnew: CompareOpc = 135; break; |
| 7903 | case Intrinsic::ppc_altivec_vcmpnezb: CompareOpc = 263; break; |
| 7904 | case Intrinsic::ppc_altivec_vcmpnezh: CompareOpc = 327; break; |
| 7905 | case Intrinsic::ppc_altivec_vcmpnezw: CompareOpc = 391; break; |
| 7906 | } |
| 7907 | isDot = 0; |
| 7908 | } else |
| 7909 | return false; |
| 7910 | break; |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 7911 | case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break; |
| 7912 | case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break; |
| 7913 | case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break; |
| 7914 | case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break; |
| 7915 | case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break; |
| NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 7916 | case Intrinsic::ppc_altivec_vcmpgtsd: |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 7917 | if (Subtarget.hasP8Altivec()) { |
| NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 7918 | CompareOpc = 967; |
| 7919 | isDot = 0; |
| 7920 | } else |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 7921 | return false; |
| 7922 | |
| 7923 | break; |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 7924 | case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break; |
| 7925 | case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break; |
| 7926 | case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break; |
| NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 7927 | case Intrinsic::ppc_altivec_vcmpgtud: |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 7928 | if (Subtarget.hasP8Altivec()) { |
| NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 7929 | CompareOpc = 711; |
| 7930 | isDot = 0; |
| 7931 | } else |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 7932 | return false; |
| 7933 | |
| 7934 | break; |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 7935 | } |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7936 | return true; |
| 7937 | } |
| 7938 | |
| 7939 | /// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom |
| 7940 | /// lower, do it, otherwise return null. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7941 | SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 7942 | SelectionDAG &DAG) const { |
| Marcin Koscielnicki | 0cfb612 | 2016-04-26 10:37:22 +0000 | [diff] [blame] | 7943 | unsigned IntrinsicID = |
| 7944 | cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| 7945 | |
| 7946 | if (IntrinsicID == Intrinsic::thread_pointer) { |
| 7947 | // Reads the thread pointer register, used for __builtin_thread_pointer. |
| 7948 | bool is64bit = Subtarget.isPPC64(); |
| 7949 | return DAG.getRegister(is64bit ? PPC::X13 : PPC::R2, |
| 7950 | is64bit ? MVT::i64 : MVT::i32); |
| 7951 | } |
| 7952 | |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7953 | // If this is a lowered altivec predicate compare, CompareOpc is set to the |
| 7954 | // opcode number of the comparison. |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7955 | SDLoc dl(Op); |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7956 | int CompareOpc; |
| 7957 | bool isDot; |
| Nemanja Ivanovic | 2c84b29 | 2015-09-29 17:41:53 +0000 | [diff] [blame] | 7958 | if (!getVectorCompareInfo(Op, CompareOpc, isDot, Subtarget)) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7959 | return SDValue(); // Don't custom lower most intrinsics. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7960 | |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7961 | // If this is a non-dot comparison, make the VCMP node and we are done. |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 7962 | if (!isDot) { |
| Dale Johannesen | f80493b | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 7963 | SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(), |
| Chris Lattner | 9fa851b | 2010-03-14 22:44:11 +0000 | [diff] [blame] | 7964 | Op.getOperand(1), Op.getOperand(2), |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7965 | DAG.getConstant(CompareOpc, dl, MVT::i32)); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7966 | return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 7967 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7968 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 7969 | // Create the PPCISD altivec 'dot' comparison node. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7970 | SDValue Ops[] = { |
| Chris Lattner | d66f14e | 2006-08-11 17:18:05 +0000 | [diff] [blame] | 7971 | Op.getOperand(2), // LHS |
| 7972 | Op.getOperand(3), // RHS |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 7973 | DAG.getConstant(CompareOpc, dl, MVT::i32) |
| Chris Lattner | d66f14e | 2006-08-11 17:18:05 +0000 | [diff] [blame] | 7974 | }; |
| Benjamin Kramer | fdf362b | 2013-03-07 20:33:29 +0000 | [diff] [blame] | 7975 | EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue }; |
| Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 7976 | SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7977 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 7978 | // Now that we have the comparison, emit a copy from the CR to a GPR. |
| 7979 | // This is flagged to the above dot comparison. |
| Ulrich Weigand | d5ebc62 | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 7980 | SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32, |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7981 | DAG.getRegister(PPC::CR6, MVT::i32), |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7982 | CompNode.getValue(1)); |
| 7983 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 7984 | // Unpack the result based on how the target uses it. |
| 7985 | unsigned BitNo; // Bit # of CR6. |
| 7986 | bool InvertBit; // Invert result? |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 7987 | switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) { |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 7988 | default: // Can't happen, don't crash on invalid number though. |
| 7989 | case 0: // Return the value of the EQ bit of CR6. |
| 7990 | BitNo = 0; InvertBit = false; |
| 7991 | break; |
| 7992 | case 1: // Return the inverted value of the EQ bit of CR6. |
| 7993 | BitNo = 0; InvertBit = true; |
| 7994 | break; |
| 7995 | case 2: // Return the value of the LT bit of CR6. |
| 7996 | BitNo = 2; InvertBit = false; |
| 7997 | break; |
| 7998 | case 3: // Return the inverted value of the LT bit of CR6. |
| 7999 | BitNo = 2; InvertBit = true; |
| 8000 | break; |
| 8001 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8002 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 8003 | // Shift the bit into the low position. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8004 | Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 8005 | DAG.getConstant(8 - (3 - BitNo), dl, MVT::i32)); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 8006 | // Isolate the bit. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8007 | Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 8008 | DAG.getConstant(1, dl, MVT::i32)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8009 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 8010 | // If we are supposed to, toggle the bit. |
| 8011 | if (InvertBit) |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8012 | Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 8013 | DAG.getConstant(1, dl, MVT::i32)); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 8014 | return Flags; |
| 8015 | } |
| 8016 | |
| Hal Finkel | 5c0d145 | 2014-03-30 13:22:59 +0000 | [diff] [blame] | 8017 | SDValue PPCTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, |
| 8018 | SelectionDAG &DAG) const { |
| 8019 | SDLoc dl(Op); |
| 8020 | // For v2i64 (VSX), we can pattern patch the v2i32 case (using fp <-> int |
| 8021 | // instructions), but for smaller types, we need to first extend up to v2i32 |
| 8022 | // before doing going farther. |
| 8023 | if (Op.getValueType() == MVT::v2i64) { |
| 8024 | EVT ExtVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); |
| 8025 | if (ExtVT != MVT::v2i32) { |
| 8026 | Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0)); |
| 8027 | Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32, Op, |
| 8028 | DAG.getValueType(EVT::getVectorVT(*DAG.getContext(), |
| 8029 | ExtVT.getVectorElementType(), 4))); |
| 8030 | Op = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Op); |
| 8031 | Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v2i64, Op, |
| 8032 | DAG.getValueType(MVT::v2i32)); |
| 8033 | } |
| 8034 | |
| 8035 | return Op; |
| 8036 | } |
| 8037 | |
| 8038 | return SDValue(); |
| 8039 | } |
| 8040 | |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8041 | SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 8042 | SelectionDAG &DAG) const { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8043 | SDLoc dl(Op); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 8044 | // Create a stack slot that is 16-byte aligned. |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 8045 | MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); |
| 8046 | int FrameIdx = MFI.CreateStackObject(16, 16, false); |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 8047 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8048 | SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8049 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 8050 | // Store the input value into Value#0 of the stack slot. |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 8051 | SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx, |
| 8052 | MachinePointerInfo()); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 8053 | // Load it out. |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 8054 | return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo()); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 8055 | } |
| 8056 | |
| Nemanja Ivanovic | d5deb48 | 2016-09-14 14:19:09 +0000 | [diff] [blame] | 8057 | SDValue PPCTargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, |
| 8058 | SelectionDAG &DAG) const { |
| 8059 | assert(Op.getOpcode() == ISD::INSERT_VECTOR_ELT && |
| 8060 | "Should only be called for ISD::INSERT_VECTOR_ELT"); |
| 8061 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(2)); |
| 8062 | // We have legal lowering for constant indices but not for variable ones. |
| 8063 | if (C) |
| 8064 | return Op; |
| 8065 | return SDValue(); |
| 8066 | } |
| 8067 | |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8068 | SDValue PPCTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, |
| 8069 | SelectionDAG &DAG) const { |
| 8070 | SDLoc dl(Op); |
| 8071 | SDNode *N = Op.getNode(); |
| 8072 | |
| 8073 | assert(N->getOperand(0).getValueType() == MVT::v4i1 && |
| 8074 | "Unknown extract_vector_elt type"); |
| 8075 | |
| 8076 | SDValue Value = N->getOperand(0); |
| 8077 | |
| 8078 | // The first part of this is like the store lowering except that we don't |
| 8079 | // need to track the chain. |
| 8080 | |
| 8081 | // The values are now known to be -1 (false) or 1 (true). To convert this |
| 8082 | // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5). |
| 8083 | // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5 |
| 8084 | Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value); |
| 8085 | |
| 8086 | // FIXME: We can make this an f32 vector, but the BUILD_VECTOR code needs to |
| 8087 | // understand how to form the extending load. |
| Ahmed Bougacha | 93cff7f | 2016-02-15 18:07:29 +0000 | [diff] [blame] | 8088 | SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::v4f64); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8089 | |
| NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 8090 | Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8091 | |
| 8092 | // Now convert to an integer and store. |
| 8093 | Value = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 8094 | DAG.getConstant(Intrinsic::ppc_qpx_qvfctiwu, dl, MVT::i32), |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8095 | Value); |
| 8096 | |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 8097 | MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); |
| 8098 | int FrameIdx = MFI.CreateStackObject(16, 16, false); |
| Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 8099 | MachinePointerInfo PtrInfo = |
| 8100 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx); |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 8101 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8102 | SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); |
| 8103 | |
| 8104 | SDValue StoreChain = DAG.getEntryNode(); |
| Benjamin Kramer | 1d67ac5 | 2016-06-17 13:15:10 +0000 | [diff] [blame] | 8105 | SDValue Ops[] = {StoreChain, |
| 8106 | DAG.getConstant(Intrinsic::ppc_qpx_qvstfiw, dl, MVT::i32), |
| 8107 | Value, FIdx}; |
| 8108 | SDVTList VTs = DAG.getVTList(/*chain*/ MVT::Other); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8109 | |
| 8110 | StoreChain = DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID, |
| 8111 | dl, VTs, Ops, MVT::v4i32, PtrInfo); |
| 8112 | |
| 8113 | // Extract the value requested. |
| 8114 | unsigned Offset = 4*cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 8115 | SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType()); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8116 | Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx); |
| 8117 | |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 8118 | SDValue IntVal = |
| 8119 | DAG.getLoad(MVT::i32, dl, StoreChain, Idx, PtrInfo.getWithOffset(Offset)); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8120 | |
| 8121 | if (!Subtarget.useCRBits()) |
| 8122 | return IntVal; |
| 8123 | |
| 8124 | return DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, IntVal); |
| 8125 | } |
| 8126 | |
| 8127 | /// Lowering for QPX v4i1 loads |
| 8128 | SDValue PPCTargetLowering::LowerVectorLoad(SDValue Op, |
| 8129 | SelectionDAG &DAG) const { |
| 8130 | SDLoc dl(Op); |
| 8131 | LoadSDNode *LN = cast<LoadSDNode>(Op.getNode()); |
| 8132 | SDValue LoadChain = LN->getChain(); |
| 8133 | SDValue BasePtr = LN->getBasePtr(); |
| 8134 | |
| 8135 | if (Op.getValueType() == MVT::v4f64 || |
| 8136 | Op.getValueType() == MVT::v4f32) { |
| 8137 | EVT MemVT = LN->getMemoryVT(); |
| 8138 | unsigned Alignment = LN->getAlignment(); |
| 8139 | |
| 8140 | // If this load is properly aligned, then it is legal. |
| 8141 | if (Alignment >= MemVT.getStoreSize()) |
| 8142 | return Op; |
| 8143 | |
| 8144 | EVT ScalarVT = Op.getValueType().getScalarType(), |
| 8145 | ScalarMemVT = MemVT.getScalarType(); |
| 8146 | unsigned Stride = ScalarMemVT.getStoreSize(); |
| 8147 | |
| Benjamin Kramer | 1d67ac5 | 2016-06-17 13:15:10 +0000 | [diff] [blame] | 8148 | SDValue Vals[4], LoadChains[4]; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8149 | for (unsigned Idx = 0; Idx < 4; ++Idx) { |
| 8150 | SDValue Load; |
| 8151 | if (ScalarVT != ScalarMemVT) |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 8152 | Load = DAG.getExtLoad(LN->getExtensionType(), dl, ScalarVT, LoadChain, |
| 8153 | BasePtr, |
| 8154 | LN->getPointerInfo().getWithOffset(Idx * Stride), |
| 8155 | ScalarMemVT, MinAlign(Alignment, Idx * Stride), |
| 8156 | LN->getMemOperand()->getFlags(), LN->getAAInfo()); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8157 | else |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 8158 | Load = DAG.getLoad(ScalarVT, dl, LoadChain, BasePtr, |
| 8159 | LN->getPointerInfo().getWithOffset(Idx * Stride), |
| 8160 | MinAlign(Alignment, Idx * Stride), |
| 8161 | LN->getMemOperand()->getFlags(), LN->getAAInfo()); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8162 | |
| 8163 | if (Idx == 0 && LN->isIndexed()) { |
| 8164 | assert(LN->getAddressingMode() == ISD::PRE_INC && |
| 8165 | "Unknown addressing mode on vector load"); |
| 8166 | Load = DAG.getIndexedLoad(Load, dl, BasePtr, LN->getOffset(), |
| 8167 | LN->getAddressingMode()); |
| 8168 | } |
| 8169 | |
| Benjamin Kramer | 1d67ac5 | 2016-06-17 13:15:10 +0000 | [diff] [blame] | 8170 | Vals[Idx] = Load; |
| 8171 | LoadChains[Idx] = Load.getValue(1); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8172 | |
| 8173 | BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 8174 | DAG.getConstant(Stride, dl, |
| 8175 | BasePtr.getValueType())); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8176 | } |
| 8177 | |
| 8178 | SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains); |
| Ahmed Bougacha | 128f873 | 2016-04-26 21:15:30 +0000 | [diff] [blame] | 8179 | SDValue Value = DAG.getBuildVector(Op.getValueType(), dl, Vals); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8180 | |
| 8181 | if (LN->isIndexed()) { |
| 8182 | SDValue RetOps[] = { Value, Vals[0].getValue(1), TF }; |
| 8183 | return DAG.getMergeValues(RetOps, dl); |
| 8184 | } |
| 8185 | |
| 8186 | SDValue RetOps[] = { Value, TF }; |
| 8187 | return DAG.getMergeValues(RetOps, dl); |
| 8188 | } |
| 8189 | |
| 8190 | assert(Op.getValueType() == MVT::v4i1 && "Unknown load to lower"); |
| 8191 | assert(LN->isUnindexed() && "Indexed v4i1 loads are not supported"); |
| 8192 | |
| 8193 | // To lower v4i1 from a byte array, we load the byte elements of the |
| 8194 | // vector and then reuse the BUILD_VECTOR logic. |
| 8195 | |
| Benjamin Kramer | 1d67ac5 | 2016-06-17 13:15:10 +0000 | [diff] [blame] | 8196 | SDValue VectElmts[4], VectElmtChains[4]; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8197 | for (unsigned i = 0; i < 4; ++i) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 8198 | SDValue Idx = DAG.getConstant(i, dl, BasePtr.getValueType()); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8199 | Idx = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, Idx); |
| 8200 | |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 8201 | VectElmts[i] = DAG.getExtLoad( |
| 8202 | ISD::EXTLOAD, dl, MVT::i32, LoadChain, Idx, |
| 8203 | LN->getPointerInfo().getWithOffset(i), MVT::i8, |
| 8204 | /* Alignment = */ 1, LN->getMemOperand()->getFlags(), LN->getAAInfo()); |
| Benjamin Kramer | 1d67ac5 | 2016-06-17 13:15:10 +0000 | [diff] [blame] | 8205 | VectElmtChains[i] = VectElmts[i].getValue(1); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8206 | } |
| 8207 | |
| 8208 | LoadChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, VectElmtChains); |
| Ahmed Bougacha | 128f873 | 2016-04-26 21:15:30 +0000 | [diff] [blame] | 8209 | SDValue Value = DAG.getBuildVector(MVT::v4i1, dl, VectElmts); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8210 | |
| 8211 | SDValue RVals[] = { Value, LoadChain }; |
| 8212 | return DAG.getMergeValues(RVals, dl); |
| 8213 | } |
| 8214 | |
| 8215 | /// Lowering for QPX v4i1 stores |
| 8216 | SDValue PPCTargetLowering::LowerVectorStore(SDValue Op, |
| 8217 | SelectionDAG &DAG) const { |
| 8218 | SDLoc dl(Op); |
| 8219 | StoreSDNode *SN = cast<StoreSDNode>(Op.getNode()); |
| 8220 | SDValue StoreChain = SN->getChain(); |
| 8221 | SDValue BasePtr = SN->getBasePtr(); |
| 8222 | SDValue Value = SN->getValue(); |
| 8223 | |
| 8224 | if (Value.getValueType() == MVT::v4f64 || |
| 8225 | Value.getValueType() == MVT::v4f32) { |
| 8226 | EVT MemVT = SN->getMemoryVT(); |
| 8227 | unsigned Alignment = SN->getAlignment(); |
| 8228 | |
| 8229 | // If this store is properly aligned, then it is legal. |
| 8230 | if (Alignment >= MemVT.getStoreSize()) |
| 8231 | return Op; |
| 8232 | |
| 8233 | EVT ScalarVT = Value.getValueType().getScalarType(), |
| 8234 | ScalarMemVT = MemVT.getScalarType(); |
| 8235 | unsigned Stride = ScalarMemVT.getStoreSize(); |
| 8236 | |
| Benjamin Kramer | 1d67ac5 | 2016-06-17 13:15:10 +0000 | [diff] [blame] | 8237 | SDValue Stores[4]; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8238 | for (unsigned Idx = 0; Idx < 4; ++Idx) { |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 8239 | SDValue Ex = DAG.getNode( |
| 8240 | ISD::EXTRACT_VECTOR_ELT, dl, ScalarVT, Value, |
| 8241 | DAG.getConstant(Idx, dl, getVectorIdxTy(DAG.getDataLayout()))); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8242 | SDValue Store; |
| 8243 | if (ScalarVT != ScalarMemVT) |
| 8244 | Store = |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 8245 | DAG.getTruncStore(StoreChain, dl, Ex, BasePtr, |
| 8246 | SN->getPointerInfo().getWithOffset(Idx * Stride), |
| 8247 | ScalarMemVT, MinAlign(Alignment, Idx * Stride), |
| 8248 | SN->getMemOperand()->getFlags(), SN->getAAInfo()); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8249 | else |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 8250 | Store = DAG.getStore(StoreChain, dl, Ex, BasePtr, |
| 8251 | SN->getPointerInfo().getWithOffset(Idx * Stride), |
| 8252 | MinAlign(Alignment, Idx * Stride), |
| 8253 | SN->getMemOperand()->getFlags(), SN->getAAInfo()); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8254 | |
| 8255 | if (Idx == 0 && SN->isIndexed()) { |
| 8256 | assert(SN->getAddressingMode() == ISD::PRE_INC && |
| 8257 | "Unknown addressing mode on vector store"); |
| 8258 | Store = DAG.getIndexedStore(Store, dl, BasePtr, SN->getOffset(), |
| 8259 | SN->getAddressingMode()); |
| 8260 | } |
| 8261 | |
| 8262 | BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 8263 | DAG.getConstant(Stride, dl, |
| 8264 | BasePtr.getValueType())); |
| Benjamin Kramer | 1d67ac5 | 2016-06-17 13:15:10 +0000 | [diff] [blame] | 8265 | Stores[Idx] = Store; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8266 | } |
| 8267 | |
| 8268 | SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); |
| 8269 | |
| 8270 | if (SN->isIndexed()) { |
| 8271 | SDValue RetOps[] = { TF, Stores[0].getValue(1) }; |
| 8272 | return DAG.getMergeValues(RetOps, dl); |
| 8273 | } |
| 8274 | |
| 8275 | return TF; |
| 8276 | } |
| 8277 | |
| 8278 | assert(SN->isUnindexed() && "Indexed v4i1 stores are not supported"); |
| 8279 | assert(Value.getValueType() == MVT::v4i1 && "Unknown store to lower"); |
| 8280 | |
| 8281 | // The values are now known to be -1 (false) or 1 (true). To convert this |
| 8282 | // into 0 (false) and 1 (true), add 1 and then divide by 2 (multiply by 0.5). |
| 8283 | // This can be done with an fma and the 0.5 constant: (V+1.0)*0.5 = 0.5*V+0.5 |
| 8284 | Value = DAG.getNode(PPCISD::QBFLT, dl, MVT::v4f64, Value); |
| 8285 | |
| 8286 | // FIXME: We can make this an f32 vector, but the BUILD_VECTOR code needs to |
| 8287 | // understand how to form the extending load. |
| Ahmed Bougacha | 93cff7f | 2016-02-15 18:07:29 +0000 | [diff] [blame] | 8288 | SDValue FPHalfs = DAG.getConstantFP(0.5, dl, MVT::v4f64); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8289 | |
| NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 8290 | Value = DAG.getNode(ISD::FMA, dl, MVT::v4f64, Value, FPHalfs, FPHalfs); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8291 | |
| 8292 | // Now convert to an integer and store. |
| 8293 | Value = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f64, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 8294 | DAG.getConstant(Intrinsic::ppc_qpx_qvfctiwu, dl, MVT::i32), |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8295 | Value); |
| 8296 | |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 8297 | MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); |
| 8298 | int FrameIdx = MFI.CreateStackObject(16, 16, false); |
| Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 8299 | MachinePointerInfo PtrInfo = |
| 8300 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx); |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 8301 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8302 | SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); |
| 8303 | |
| Benjamin Kramer | 1d67ac5 | 2016-06-17 13:15:10 +0000 | [diff] [blame] | 8304 | SDValue Ops[] = {StoreChain, |
| 8305 | DAG.getConstant(Intrinsic::ppc_qpx_qvstfiw, dl, MVT::i32), |
| 8306 | Value, FIdx}; |
| 8307 | SDVTList VTs = DAG.getVTList(/*chain*/ MVT::Other); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8308 | |
| 8309 | StoreChain = DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID, |
| 8310 | dl, VTs, Ops, MVT::v4i32, PtrInfo); |
| 8311 | |
| 8312 | // Move data into the byte array. |
| Benjamin Kramer | 1d67ac5 | 2016-06-17 13:15:10 +0000 | [diff] [blame] | 8313 | SDValue Loads[4], LoadChains[4]; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8314 | for (unsigned i = 0; i < 4; ++i) { |
| 8315 | unsigned Offset = 4*i; |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 8316 | SDValue Idx = DAG.getConstant(Offset, dl, FIdx.getValueType()); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8317 | Idx = DAG.getNode(ISD::ADD, dl, FIdx.getValueType(), FIdx, Idx); |
| 8318 | |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 8319 | Loads[i] = DAG.getLoad(MVT::i32, dl, StoreChain, Idx, |
| 8320 | PtrInfo.getWithOffset(Offset)); |
| Benjamin Kramer | 1d67ac5 | 2016-06-17 13:15:10 +0000 | [diff] [blame] | 8321 | LoadChains[i] = Loads[i].getValue(1); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8322 | } |
| 8323 | |
| 8324 | StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains); |
| 8325 | |
| Benjamin Kramer | 1d67ac5 | 2016-06-17 13:15:10 +0000 | [diff] [blame] | 8326 | SDValue Stores[4]; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8327 | for (unsigned i = 0; i < 4; ++i) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 8328 | SDValue Idx = DAG.getConstant(i, dl, BasePtr.getValueType()); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8329 | Idx = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, Idx); |
| 8330 | |
| Benjamin Kramer | 1d67ac5 | 2016-06-17 13:15:10 +0000 | [diff] [blame] | 8331 | Stores[i] = DAG.getTruncStore( |
| NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 8332 | StoreChain, dl, Loads[i], Idx, SN->getPointerInfo().getWithOffset(i), |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 8333 | MVT::i8, /* Alignment = */ 1, SN->getMemOperand()->getFlags(), |
| 8334 | SN->getAAInfo()); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8335 | } |
| 8336 | |
| 8337 | StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); |
| 8338 | |
| 8339 | return StoreChain; |
| 8340 | } |
| 8341 | |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 8342 | SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8343 | SDLoc dl(Op); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8344 | if (Op.getValueType() == MVT::v4i32) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8345 | SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8346 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8347 | SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl); |
| 8348 | SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8349 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8350 | SDValue RHSSwap = // = vrlw RHS, 16 |
| Dale Johannesen | 9f3f72f | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 8351 | BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8352 | |
| Chris Lattner | 7e439874 | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 8353 | // Shrinkify inputs to v8i16. |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8354 | LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS); |
| 8355 | RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS); |
| 8356 | RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8357 | |
| Chris Lattner | 7e439874 | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 8358 | // Low parts multiplied together, generating 32-bit results (we ignore the |
| 8359 | // top parts). |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8360 | SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh, |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8361 | LHS, RHS, DAG, dl, MVT::v4i32); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8362 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8363 | SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm, |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8364 | LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32); |
| Chris Lattner | 7e439874 | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 8365 | // Shift the high parts up 16 bits. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8366 | HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd, |
| Dale Johannesen | 9f3f72f | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 8367 | Neg16, DAG, dl); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8368 | return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd); |
| 8369 | } else if (Op.getValueType() == MVT::v8i16) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8370 | SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8371 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8372 | SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl); |
| Chris Lattner | 7e439874 | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 8373 | |
| Chris Lattner | 96d5048 | 2006-04-18 04:28:57 +0000 | [diff] [blame] | 8374 | return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm, |
| Dale Johannesen | 9f3f72f | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 8375 | LHS, RHS, Zero, DAG, dl); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8376 | } else if (Op.getValueType() == MVT::v16i8) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8377 | SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 8378 | bool isLittleEndian = Subtarget.isLittleEndian(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8379 | |
| Chris Lattner | d6d82aa | 2006-04-18 03:57:35 +0000 | [diff] [blame] | 8380 | // Multiply the even 8-bit parts, producing 16-bit sums. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8381 | SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub, |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8382 | LHS, RHS, DAG, dl, MVT::v8i16); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8383 | EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8384 | |
| Chris Lattner | d6d82aa | 2006-04-18 03:57:35 +0000 | [diff] [blame] | 8385 | // Multiply the odd 8-bit parts, producing 16-bit sums. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8386 | SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub, |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8387 | LHS, RHS, DAG, dl, MVT::v8i16); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8388 | OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8389 | |
| Bill Schmidt | 42995e8 | 2014-06-09 16:06:29 +0000 | [diff] [blame] | 8390 | // Merge the results together. Because vmuleub and vmuloub are |
| 8391 | // instructions with a big-endian bias, we must reverse the |
| 8392 | // element numbering and reverse the meaning of "odd" and "even" |
| 8393 | // when generating little endian code. |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 8394 | int Ops[16]; |
| Chris Lattner | d6d82aa | 2006-04-18 03:57:35 +0000 | [diff] [blame] | 8395 | for (unsigned i = 0; i != 8; ++i) { |
| Bill Schmidt | 42995e8 | 2014-06-09 16:06:29 +0000 | [diff] [blame] | 8396 | if (isLittleEndian) { |
| 8397 | Ops[i*2 ] = 2*i; |
| 8398 | Ops[i*2+1] = 2*i+16; |
| 8399 | } else { |
| 8400 | Ops[i*2 ] = 2*i+1; |
| 8401 | Ops[i*2+1] = 2*i+1+16; |
| 8402 | } |
| Chris Lattner | d6d82aa | 2006-04-18 03:57:35 +0000 | [diff] [blame] | 8403 | } |
| Bill Schmidt | 42995e8 | 2014-06-09 16:06:29 +0000 | [diff] [blame] | 8404 | if (isLittleEndian) |
| 8405 | return DAG.getVectorShuffle(MVT::v16i8, dl, OddParts, EvenParts, Ops); |
| 8406 | else |
| 8407 | return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops); |
| Chris Lattner | 7e439874 | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 8408 | } else { |
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 8409 | llvm_unreachable("Unknown mul to lower!"); |
| Chris Lattner | 7e439874 | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 8410 | } |
| Chris Lattner | a2cae1b | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 8411 | } |
| 8412 | |
| Chris Lattner | f3d06c6 | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 8413 | /// LowerOperation - Provide custom lowering hooks for some operations. |
| 8414 | /// |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 8415 | SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
| Chris Lattner | f3d06c6 | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 8416 | switch (Op.getOpcode()) { |
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 8417 | default: llvm_unreachable("Wasn't expecting to be able to lower this!"); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 8418 | case ISD::ConstantPool: return LowerConstantPool(Op, DAG); |
| Bob Wilson | f84f710 | 2009-11-04 21:31:18 +0000 | [diff] [blame] | 8419 | case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 8420 | case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); |
| Roman Divacky | e3f15c98 | 2012-06-04 17:36:38 +0000 | [diff] [blame] | 8421 | case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); |
| Nate Begeman | 4ca2ea5 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 8422 | case ISD::JumpTable: return LowerJumpTable(Op, DAG); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 8423 | case ISD::SETCC: return LowerSETCC(Op, DAG); |
| Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 8424 | case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG); |
| 8425 | case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8426 | case ISD::VASTART: |
| Eric Christopher | b976a39 | 2016-07-07 00:39:27 +0000 | [diff] [blame] | 8427 | return LowerVASTART(Op, DAG); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8428 | |
| 8429 | case ISD::VAARG: |
| Eric Christopher | b976a39 | 2016-07-07 00:39:27 +0000 | [diff] [blame] | 8430 | return LowerVAARG(Op, DAG); |
| Nicolas Geoffray | 23710a7 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 8431 | |
| Roman Divacky | c3825df | 2013-07-25 21:36:47 +0000 | [diff] [blame] | 8432 | case ISD::VACOPY: |
| Eric Christopher | b976a39 | 2016-07-07 00:39:27 +0000 | [diff] [blame] | 8433 | return LowerVACOPY(Op, DAG); |
| Roman Divacky | c3825df | 2013-07-25 21:36:47 +0000 | [diff] [blame] | 8434 | |
| Eric Christopher | b976a39 | 2016-07-07 00:39:27 +0000 | [diff] [blame] | 8435 | case ISD::STACKRESTORE: |
| 8436 | return LowerSTACKRESTORE(Op, DAG); |
| 8437 | |
| Chris Lattner | 43df5b3 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 8438 | case ISD::DYNAMIC_STACKALLOC: |
| Eric Christopher | b976a39 | 2016-07-07 00:39:27 +0000 | [diff] [blame] | 8439 | return LowerDYNAMIC_STACKALLOC(Op, DAG); |
| 8440 | |
| 8441 | case ISD::GET_DYNAMIC_AREA_OFFSET: |
| 8442 | return LowerGET_DYNAMIC_AREA_OFFSET(Op, DAG); |
| Evan Cheng | 51096af | 2008-04-19 01:30:48 +0000 | [diff] [blame] | 8443 | |
| Hal Finkel | 5081ac2 | 2016-09-01 10:28:47 +0000 | [diff] [blame] | 8444 | case ISD::EH_DWARF_CFA: |
| 8445 | return LowerEH_DWARF_CFA(Op, DAG); |
| 8446 | |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 8447 | case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG); |
| 8448 | case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG); |
| 8449 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 8450 | case ISD::LOAD: return LowerLOAD(Op, DAG); |
| 8451 | case ISD::STORE: return LowerSTORE(Op, DAG); |
| 8452 | case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG); |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 8453 | case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); |
| Dale Johannesen | 37bc85f | 2009-06-04 20:53:52 +0000 | [diff] [blame] | 8454 | case ISD::FP_TO_UINT: |
| 8455 | case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG, |
| Hal Finkel | ed844c4 | 2015-01-06 22:31:02 +0000 | [diff] [blame] | 8456 | SDLoc(Op)); |
| Hal Finkel | f6d45f2 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 8457 | case ISD::UINT_TO_FP: |
| 8458 | case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG); |
| Dan Gohman | 9ba4d76 | 2008-01-31 00:41:03 +0000 | [diff] [blame] | 8459 | case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); |
| Chris Lattner | 4a66d69 | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 8460 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 8461 | // Lower 64-bit shifts. |
| Chris Lattner | 601b865 | 2006-09-20 03:47:40 +0000 | [diff] [blame] | 8462 | case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG); |
| 8463 | case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG); |
| 8464 | case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG); |
| Chris Lattner | 4a66d69 | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 8465 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 8466 | // Vector-related lowering. |
| 8467 | case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG); |
| 8468 | case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); |
| 8469 | case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); |
| 8470 | case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG); |
| Hal Finkel | 5c0d145 | 2014-03-30 13:22:59 +0000 | [diff] [blame] | 8471 | case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 8472 | case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); |
| Nemanja Ivanovic | d5deb48 | 2016-09-14 14:19:09 +0000 | [diff] [blame] | 8473 | case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); |
| Chris Lattner | a2cae1b | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 8474 | case ISD::MUL: return LowerMUL(Op, DAG); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8475 | |
| Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 8476 | // For counter-based loop handling. |
| 8477 | case ISD::INTRINSIC_W_CHAIN: return SDValue(); |
| 8478 | |
| Chris Lattner | f6a8156 | 2007-12-08 06:59:59 +0000 | [diff] [blame] | 8479 | // Frame & Return address. |
| 8480 | case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); |
| Nicolas Geoffray | 75ab979 | 2007-03-01 13:11:38 +0000 | [diff] [blame] | 8481 | case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); |
| Chris Lattner | e675a08 | 2005-08-31 20:23:54 +0000 | [diff] [blame] | 8482 | } |
| Chris Lattner | f3d06c6 | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 8483 | } |
| 8484 | |
| Duncan Sands | 6ed4014 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 8485 | void PPCTargetLowering::ReplaceNodeResults(SDNode *N, |
| 8486 | SmallVectorImpl<SDValue>&Results, |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 8487 | SelectionDAG &DAG) const { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8488 | SDLoc dl(N); |
| Chris Lattner | 57ee7c6 | 2007-11-28 18:44:47 +0000 | [diff] [blame] | 8489 | switch (N->getOpcode()) { |
| Duncan Sands | 4068a7f | 2008-10-28 15:00:32 +0000 | [diff] [blame] | 8490 | default: |
| Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 8491 | llvm_unreachable("Do not know how to custom type legalize this operation!"); |
| Hal Finkel | bbdee93 | 2014-12-02 22:01:00 +0000 | [diff] [blame] | 8492 | case ISD::READCYCLECOUNTER: { |
| 8493 | SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other); |
| 8494 | SDValue RTB = DAG.getNode(PPCISD::READ_TIME_BASE, dl, VTs, N->getOperand(0)); |
| 8495 | |
| 8496 | Results.push_back(RTB); |
| 8497 | Results.push_back(RTB.getValue(1)); |
| 8498 | Results.push_back(RTB.getValue(2)); |
| 8499 | break; |
| 8500 | } |
| Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 8501 | case ISD::INTRINSIC_W_CHAIN: { |
| 8502 | if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != |
| 8503 | Intrinsic::ppc_is_decremented_ctr_nonzero) |
| 8504 | break; |
| 8505 | |
| 8506 | assert(N->getValueType(0) == MVT::i1 && |
| 8507 | "Unexpected result type for CTR decrement intrinsic"); |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 8508 | EVT SVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), |
| 8509 | N->getValueType(0)); |
| Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 8510 | SDVTList VTs = DAG.getVTList(SVT, MVT::Other); |
| 8511 | SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0), |
| NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 8512 | N->getOperand(1)); |
| Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 8513 | |
| 8514 | Results.push_back(NewInt); |
| 8515 | Results.push_back(NewInt.getValue(1)); |
| 8516 | break; |
| 8517 | } |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 8518 | case ISD::VAARG: { |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 8519 | if (!Subtarget.isSVR4ABI() || Subtarget.isPPC64()) |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 8520 | return; |
| 8521 | |
| 8522 | EVT VT = N->getValueType(0); |
| 8523 | |
| 8524 | if (VT == MVT::i64) { |
| Eric Christopher | b976a39 | 2016-07-07 00:39:27 +0000 | [diff] [blame] | 8525 | SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG); |
| Roman Divacky | 4394e68 | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 8526 | |
| 8527 | Results.push_back(NewNode); |
| 8528 | Results.push_back(NewNode.getValue(1)); |
| 8529 | } |
| 8530 | return; |
| 8531 | } |
| Duncan Sands | 6ed4014 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 8532 | case ISD::FP_ROUND_INREG: { |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8533 | assert(N->getValueType(0) == MVT::ppcf128); |
| 8534 | assert(N->getOperand(0).getValueType() == MVT::ppcf128); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8535 | SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8536 | MVT::f64, N->getOperand(0), |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 8537 | DAG.getIntPtrConstant(0, dl)); |
| Dale Johannesen | f80493b | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 8538 | SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8539 | MVT::f64, N->getOperand(0), |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 8540 | DAG.getIntPtrConstant(1, dl)); |
| Duncan Sands | 6ed4014 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 8541 | |
| Ulrich Weigand | 874fc62 | 2013-03-26 10:56:22 +0000 | [diff] [blame] | 8542 | // Add the two halves of the long double in round-to-zero mode. |
| 8543 | SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi); |
| Duncan Sands | 6ed4014 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 8544 | |
| 8545 | // We know the low half is about to be thrown away, so just use something |
| 8546 | // convenient. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8547 | Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128, |
| Dale Johannesen | f80493b | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 8548 | FPreg, FPreg)); |
| Duncan Sands | 6ed4014 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 8549 | return; |
| Duncan Sands | 2a28791 | 2008-07-19 16:26:02 +0000 | [diff] [blame] | 8550 | } |
| Duncan Sands | 6ed4014 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 8551 | case ISD::FP_TO_SINT: |
| Hal Finkel | 9313850 | 2015-04-10 03:39:00 +0000 | [diff] [blame] | 8552 | case ISD::FP_TO_UINT: |
| Bill Schmidt | 4122169 | 2013-07-09 18:50:20 +0000 | [diff] [blame] | 8553 | // LowerFP_TO_INT() can only handle f32 and f64. |
| 8554 | if (N->getOperand(0).getValueType() == MVT::ppcf128) |
| 8555 | return; |
| Dale Johannesen | 37bc85f | 2009-06-04 20:53:52 +0000 | [diff] [blame] | 8556 | Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl)); |
| Duncan Sands | 6ed4014 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 8557 | return; |
| Chris Lattner | 57ee7c6 | 2007-11-28 18:44:47 +0000 | [diff] [blame] | 8558 | } |
| 8559 | } |
| 8560 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 8561 | //===----------------------------------------------------------------------===// |
| 8562 | // Other Lowering Code |
| 8563 | //===----------------------------------------------------------------------===// |
| 8564 | |
| Robin Morisset | 2212996 | 2014-09-23 20:46:49 +0000 | [diff] [blame] | 8565 | static Instruction* callIntrinsic(IRBuilder<> &Builder, Intrinsic::ID Id) { |
| 8566 | Module *M = Builder.GetInsertBlock()->getParent()->getParent(); |
| 8567 | Function *Func = Intrinsic::getDeclaration(M, Id); |
| David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 8568 | return Builder.CreateCall(Func, {}); |
| Robin Morisset | 2212996 | 2014-09-23 20:46:49 +0000 | [diff] [blame] | 8569 | } |
| 8570 | |
| 8571 | // The mappings for emitLeading/TrailingFence is taken from |
| 8572 | // http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html |
| 8573 | Instruction* PPCTargetLowering::emitLeadingFence(IRBuilder<> &Builder, |
| 8574 | AtomicOrdering Ord, bool IsStore, |
| 8575 | bool IsLoad) const { |
| JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 8576 | if (Ord == AtomicOrdering::SequentiallyConsistent) |
| Robin Morisset | 2212996 | 2014-09-23 20:46:49 +0000 | [diff] [blame] | 8577 | return callIntrinsic(Builder, Intrinsic::ppc_sync); |
| JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 8578 | if (isReleaseOrStronger(Ord)) |
| Robin Morisset | 2212996 | 2014-09-23 20:46:49 +0000 | [diff] [blame] | 8579 | return callIntrinsic(Builder, Intrinsic::ppc_lwsync); |
| David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 8580 | return nullptr; |
| Robin Morisset | 2212996 | 2014-09-23 20:46:49 +0000 | [diff] [blame] | 8581 | } |
| 8582 | |
| 8583 | Instruction* PPCTargetLowering::emitTrailingFence(IRBuilder<> &Builder, |
| 8584 | AtomicOrdering Ord, bool IsStore, |
| 8585 | bool IsLoad) const { |
| JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 8586 | if (IsLoad && isAcquireOrStronger(Ord)) |
| Robin Morisset | 2212996 | 2014-09-23 20:46:49 +0000 | [diff] [blame] | 8587 | return callIntrinsic(Builder, Intrinsic::ppc_lwsync); |
| 8588 | // FIXME: this is too conservative, a dependent branch + isync is enough. |
| 8589 | // See http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html and |
| 8590 | // http://www.rdrop.com/users/paulmck/scalability/paper/N2745r.2011.03.04a.html |
| 8591 | // and http://www.cl.cam.ac.uk/~pes20/cppppc/ for justification. |
| David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 8592 | return nullptr; |
| Robin Morisset | 2212996 | 2014-09-23 20:46:49 +0000 | [diff] [blame] | 8593 | } |
| 8594 | |
| Chris Lattner | 9b577f1 | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 8595 | MachineBasicBlock * |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 8596 | PPCTargetLowering::EmitAtomicBinary(MachineInstr &MI, MachineBasicBlock *BB, |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 8597 | unsigned AtomicSize, |
| Hal Finkel | 5728200 | 2016-08-28 16:17:58 +0000 | [diff] [blame] | 8598 | unsigned BinOpcode, |
| 8599 | unsigned CmpOpcode, |
| 8600 | unsigned CmpPred) const { |
| Dale Johannesen | f0a88d6 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 8601 | // This also handles ATOMIC_SWAP, indicated by BinOpcode==0. |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 8602 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); |
| Dale Johannesen | d4eb052 | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 8603 | |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 8604 | auto LoadMnemonic = PPC::LDARX; |
| 8605 | auto StoreMnemonic = PPC::STDCX; |
| 8606 | switch (AtomicSize) { |
| 8607 | default: |
| 8608 | llvm_unreachable("Unexpected size of atomic entity"); |
| 8609 | case 1: |
| 8610 | LoadMnemonic = PPC::LBARX; |
| 8611 | StoreMnemonic = PPC::STBCX; |
| 8612 | assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4"); |
| 8613 | break; |
| 8614 | case 2: |
| 8615 | LoadMnemonic = PPC::LHARX; |
| 8616 | StoreMnemonic = PPC::STHCX; |
| 8617 | assert(Subtarget.hasPartwordAtomics() && "Call this only with size >=4"); |
| 8618 | break; |
| 8619 | case 4: |
| 8620 | LoadMnemonic = PPC::LWARX; |
| 8621 | StoreMnemonic = PPC::STWCX; |
| 8622 | break; |
| 8623 | case 8: |
| 8624 | LoadMnemonic = PPC::LDARX; |
| 8625 | StoreMnemonic = PPC::STDCX; |
| 8626 | break; |
| 8627 | } |
| 8628 | |
| Dale Johannesen | d4eb052 | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 8629 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 8630 | MachineFunction *F = BB->getParent(); |
| Duncan P. N. Exon Smith | ac65b4c | 2015-10-20 01:07:37 +0000 | [diff] [blame] | 8631 | MachineFunction::iterator It = ++BB->getIterator(); |
| Dale Johannesen | d4eb052 | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 8632 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 8633 | unsigned dest = MI.getOperand(0).getReg(); |
| 8634 | unsigned ptrA = MI.getOperand(1).getReg(); |
| 8635 | unsigned ptrB = MI.getOperand(2).getReg(); |
| 8636 | unsigned incr = MI.getOperand(3).getReg(); |
| 8637 | DebugLoc dl = MI.getDebugLoc(); |
| Dale Johannesen | d4eb052 | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 8638 | |
| 8639 | MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| Hal Finkel | 5728200 | 2016-08-28 16:17:58 +0000 | [diff] [blame] | 8640 | MachineBasicBlock *loop2MBB = |
| 8641 | CmpOpcode ? F->CreateMachineBasicBlock(LLVM_BB) : nullptr; |
| Dale Johannesen | d4eb052 | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 8642 | MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 8643 | F->insert(It, loopMBB); |
| Hal Finkel | 5728200 | 2016-08-28 16:17:58 +0000 | [diff] [blame] | 8644 | if (CmpOpcode) |
| 8645 | F->insert(It, loop2MBB); |
| Dale Johannesen | d4eb052 | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 8646 | F->insert(It, exitMBB); |
| Dan Gohman | 3439629 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 8647 | exitMBB->splice(exitMBB->begin(), BB, |
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 8648 | std::next(MachineBasicBlock::iterator(MI)), BB->end()); |
| Dan Gohman | 3439629 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 8649 | exitMBB->transferSuccessorsAndUpdatePHIs(BB); |
| Dale Johannesen | d4eb052 | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 8650 | |
| 8651 | MachineRegisterInfo &RegInfo = F->getRegInfo(); |
| Dale Johannesen | f0a88d6 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 8652 | unsigned TmpReg = (!BinOpcode) ? incr : |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 8653 | RegInfo.createVirtualRegister( AtomicSize == 8 ? &PPC::G8RCRegClass |
| Craig Topper | 61e88f4 | 2014-11-21 05:58:21 +0000 | [diff] [blame] | 8654 | : &PPC::GPRCRegClass); |
| Dale Johannesen | d4eb052 | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 8655 | |
| 8656 | // thisMBB: |
| 8657 | // ... |
| 8658 | // fallthrough --> loopMBB |
| 8659 | BB->addSuccessor(loopMBB); |
| 8660 | |
| 8661 | // loopMBB: |
| 8662 | // l[wd]arx dest, ptr |
| 8663 | // add r0, dest, incr |
| 8664 | // st[wd]cx. r0, ptr |
| 8665 | // bne- loopMBB |
| 8666 | // fallthrough --> exitMBB |
| Hal Finkel | 5728200 | 2016-08-28 16:17:58 +0000 | [diff] [blame] | 8667 | |
| 8668 | // For max/min... |
| 8669 | // loopMBB: |
| 8670 | // l[wd]arx dest, ptr |
| 8671 | // cmpl?[wd] incr, dest |
| 8672 | // bgt exitMBB |
| 8673 | // loop2MBB: |
| 8674 | // st[wd]cx. dest, ptr |
| 8675 | // bne- loopMBB |
| 8676 | // fallthrough --> exitMBB |
| 8677 | |
| Dale Johannesen | d4eb052 | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 8678 | BB = loopMBB; |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 8679 | BuildMI(BB, dl, TII->get(LoadMnemonic), dest) |
| Dale Johannesen | d4eb052 | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 8680 | .addReg(ptrA).addReg(ptrB); |
| Dale Johannesen | f0a88d6 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 8681 | if (BinOpcode) |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 8682 | BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest); |
| Hal Finkel | 5728200 | 2016-08-28 16:17:58 +0000 | [diff] [blame] | 8683 | if (CmpOpcode) { |
| Nemanja Ivanovic | 8dacca9 | 2016-09-22 19:06:38 +0000 | [diff] [blame] | 8684 | // Signed comparisons of byte or halfword values must be sign-extended. |
| 8685 | if (CmpOpcode == PPC::CMPW && AtomicSize < 4) { |
| 8686 | unsigned ExtReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass); |
| 8687 | BuildMI(BB, dl, TII->get(AtomicSize == 1 ? PPC::EXTSB : PPC::EXTSH), |
| 8688 | ExtReg).addReg(dest); |
| 8689 | BuildMI(BB, dl, TII->get(CmpOpcode), PPC::CR0) |
| 8690 | .addReg(incr).addReg(ExtReg); |
| 8691 | } else |
| 8692 | BuildMI(BB, dl, TII->get(CmpOpcode), PPC::CR0) |
| 8693 | .addReg(incr).addReg(dest); |
| 8694 | |
| Hal Finkel | 5728200 | 2016-08-28 16:17:58 +0000 | [diff] [blame] | 8695 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
| 8696 | .addImm(CmpPred).addReg(PPC::CR0).addMBB(exitMBB); |
| 8697 | BB->addSuccessor(loop2MBB); |
| 8698 | BB->addSuccessor(exitMBB); |
| 8699 | BB = loop2MBB; |
| 8700 | } |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 8701 | BuildMI(BB, dl, TII->get(StoreMnemonic)) |
| Dale Johannesen | d4eb052 | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 8702 | .addReg(TmpReg).addReg(ptrA).addReg(ptrB); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 8703 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8704 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB); |
| Dale Johannesen | d4eb052 | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 8705 | BB->addSuccessor(loopMBB); |
| 8706 | BB->addSuccessor(exitMBB); |
| 8707 | |
| 8708 | // exitMBB: |
| 8709 | // ... |
| 8710 | BB = exitMBB; |
| 8711 | return BB; |
| 8712 | } |
| 8713 | |
| 8714 | MachineBasicBlock * |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 8715 | PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr &MI, |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8716 | MachineBasicBlock *BB, |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 8717 | bool is8bit, // operation |
| Hal Finkel | 5728200 | 2016-08-28 16:17:58 +0000 | [diff] [blame] | 8718 | unsigned BinOpcode, |
| 8719 | unsigned CmpOpcode, |
| 8720 | unsigned CmpPred) const { |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 8721 | // If we support part-word atomic mnemonics, just use them |
| 8722 | if (Subtarget.hasPartwordAtomics()) |
| Hal Finkel | 5728200 | 2016-08-28 16:17:58 +0000 | [diff] [blame] | 8723 | return EmitAtomicBinary(MI, BB, is8bit ? 1 : 2, BinOpcode, |
| 8724 | CmpOpcode, CmpPred); |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 8725 | |
| Dale Johannesen | f0a88d6 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 8726 | // This also handles ATOMIC_SWAP, indicated by BinOpcode==0. |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 8727 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8728 | // In 64 bit mode we have to use 64 bits for addresses, even though the |
| 8729 | // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address |
| 8730 | // registers without caring whether they're 32 or 64, but here we're |
| 8731 | // doing actual arithmetic on the addresses. |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 8732 | bool is64bit = Subtarget.isPPC64(); |
| Hal Finkel | 3d70a9d | 2016-08-29 22:25:36 +0000 | [diff] [blame] | 8733 | bool isLittleEndian = Subtarget.isLittleEndian(); |
| Hal Finkel | f70c41e | 2013-03-21 23:45:03 +0000 | [diff] [blame] | 8734 | unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO; |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8735 | |
| 8736 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 8737 | MachineFunction *F = BB->getParent(); |
| Duncan P. N. Exon Smith | ac65b4c | 2015-10-20 01:07:37 +0000 | [diff] [blame] | 8738 | MachineFunction::iterator It = ++BB->getIterator(); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8739 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 8740 | unsigned dest = MI.getOperand(0).getReg(); |
| 8741 | unsigned ptrA = MI.getOperand(1).getReg(); |
| 8742 | unsigned ptrB = MI.getOperand(2).getReg(); |
| 8743 | unsigned incr = MI.getOperand(3).getReg(); |
| 8744 | DebugLoc dl = MI.getDebugLoc(); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8745 | |
| 8746 | MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| Hal Finkel | 5728200 | 2016-08-28 16:17:58 +0000 | [diff] [blame] | 8747 | MachineBasicBlock *loop2MBB = |
| 8748 | CmpOpcode ? F->CreateMachineBasicBlock(LLVM_BB) : nullptr; |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8749 | MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 8750 | F->insert(It, loopMBB); |
| Hal Finkel | 5728200 | 2016-08-28 16:17:58 +0000 | [diff] [blame] | 8751 | if (CmpOpcode) |
| 8752 | F->insert(It, loop2MBB); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8753 | F->insert(It, exitMBB); |
| Dan Gohman | 3439629 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 8754 | exitMBB->splice(exitMBB->begin(), BB, |
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 8755 | std::next(MachineBasicBlock::iterator(MI)), BB->end()); |
| Dan Gohman | 3439629 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 8756 | exitMBB->transferSuccessorsAndUpdatePHIs(BB); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8757 | |
| 8758 | MachineRegisterInfo &RegInfo = F->getRegInfo(); |
| Craig Topper | 61e88f4 | 2014-11-21 05:58:21 +0000 | [diff] [blame] | 8759 | const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass |
| 8760 | : &PPC::GPRCRegClass; |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8761 | unsigned PtrReg = RegInfo.createVirtualRegister(RC); |
| 8762 | unsigned Shift1Reg = RegInfo.createVirtualRegister(RC); |
| Hal Finkel | 3d70a9d | 2016-08-29 22:25:36 +0000 | [diff] [blame] | 8763 | unsigned ShiftReg = |
| 8764 | isLittleEndian ? Shift1Reg : RegInfo.createVirtualRegister(RC); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8765 | unsigned Incr2Reg = RegInfo.createVirtualRegister(RC); |
| 8766 | unsigned MaskReg = RegInfo.createVirtualRegister(RC); |
| 8767 | unsigned Mask2Reg = RegInfo.createVirtualRegister(RC); |
| 8768 | unsigned Mask3Reg = RegInfo.createVirtualRegister(RC); |
| 8769 | unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC); |
| 8770 | unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC); |
| 8771 | unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC); |
| Dale Johannesen | f0a88d6 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 8772 | unsigned TmpDestReg = RegInfo.createVirtualRegister(RC); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8773 | unsigned Ptr1Reg; |
| Dale Johannesen | f0a88d6 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 8774 | unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8775 | |
| 8776 | // thisMBB: |
| 8777 | // ... |
| 8778 | // fallthrough --> loopMBB |
| 8779 | BB->addSuccessor(loopMBB); |
| 8780 | |
| 8781 | // The 4-byte load must be aligned, while a char or short may be |
| 8782 | // anywhere in the word. Hence all this nasty bookkeeping code. |
| 8783 | // add ptr1, ptrA, ptrB [copy if ptrA==0] |
| 8784 | // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27] |
| Dale Johannesen | bc69829 | 2008-09-02 20:30:23 +0000 | [diff] [blame] | 8785 | // xori shift, shift1, 24 [16] |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8786 | // rlwinm ptr, ptr1, 0, 0, 29 |
| 8787 | // slw incr2, incr, shift |
| 8788 | // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535] |
| 8789 | // slw mask, mask2, shift |
| 8790 | // loopMBB: |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 8791 | // lwarx tmpDest, ptr |
| Dale Johannesen | f0a88d6 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 8792 | // add tmp, tmpDest, incr2 |
| 8793 | // andc tmp2, tmpDest, mask |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8794 | // and tmp3, tmp, mask |
| 8795 | // or tmp4, tmp3, tmp2 |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 8796 | // stwcx. tmp4, ptr |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8797 | // bne- loopMBB |
| 8798 | // fallthrough --> exitMBB |
| Dale Johannesen | f0a88d6 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 8799 | // srw dest, tmpDest, shift |
| Jakob Stoklund Olesen | 7067bff | 2011-04-04 17:07:06 +0000 | [diff] [blame] | 8800 | if (ptrA != ZeroReg) { |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8801 | Ptr1Reg = RegInfo.createVirtualRegister(RC); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 8802 | BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8803 | .addReg(ptrA).addReg(ptrB); |
| 8804 | } else { |
| 8805 | Ptr1Reg = ptrB; |
| 8806 | } |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 8807 | BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8808 | .addImm(3).addImm(27).addImm(is8bit ? 28 : 27); |
| Hal Finkel | 3d70a9d | 2016-08-29 22:25:36 +0000 | [diff] [blame] | 8809 | if (!isLittleEndian) |
| 8810 | BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg) |
| 8811 | .addReg(Shift1Reg).addImm(is8bit ? 24 : 16); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8812 | if (is64bit) |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 8813 | BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8814 | .addReg(Ptr1Reg).addImm(0).addImm(61); |
| 8815 | else |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 8816 | BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8817 | .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 8818 | BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8819 | .addReg(incr).addReg(ShiftReg); |
| 8820 | if (is8bit) |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 8821 | BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8822 | else { |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 8823 | BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0); |
| 8824 | BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8825 | } |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 8826 | BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8827 | .addReg(Mask2Reg).addReg(ShiftReg); |
| 8828 | |
| 8829 | BB = loopMBB; |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 8830 | BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg) |
| Jakob Stoklund Olesen | 7067bff | 2011-04-04 17:07:06 +0000 | [diff] [blame] | 8831 | .addReg(ZeroReg).addReg(PtrReg); |
| Dale Johannesen | f0a88d6 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 8832 | if (BinOpcode) |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 8833 | BuildMI(BB, dl, TII->get(BinOpcode), TmpReg) |
| Dale Johannesen | f0a88d6 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 8834 | .addReg(Incr2Reg).addReg(TmpDestReg); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 8835 | BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg) |
| Dale Johannesen | f0a88d6 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 8836 | .addReg(TmpDestReg).addReg(MaskReg); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 8837 | BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8838 | .addReg(TmpReg).addReg(MaskReg); |
| Hal Finkel | 5728200 | 2016-08-28 16:17:58 +0000 | [diff] [blame] | 8839 | if (CmpOpcode) { |
| 8840 | // For unsigned comparisons, we can directly compare the shifted values. |
| 8841 | // For signed comparisons we shift and sign extend. |
| 8842 | unsigned SReg = RegInfo.createVirtualRegister(RC); |
| 8843 | BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), SReg) |
| 8844 | .addReg(TmpDestReg).addReg(MaskReg); |
| 8845 | unsigned ValueReg = SReg; |
| 8846 | unsigned CmpReg = Incr2Reg; |
| 8847 | if (CmpOpcode == PPC::CMPW) { |
| 8848 | ValueReg = RegInfo.createVirtualRegister(RC); |
| 8849 | BuildMI(BB, dl, TII->get(PPC::SRW), ValueReg) |
| 8850 | .addReg(SReg).addReg(ShiftReg); |
| 8851 | unsigned ValueSReg = RegInfo.createVirtualRegister(RC); |
| 8852 | BuildMI(BB, dl, TII->get(is8bit ? PPC::EXTSB : PPC::EXTSH), ValueSReg) |
| 8853 | .addReg(ValueReg); |
| 8854 | ValueReg = ValueSReg; |
| 8855 | CmpReg = incr; |
| 8856 | } |
| 8857 | BuildMI(BB, dl, TII->get(CmpOpcode), PPC::CR0) |
| 8858 | .addReg(CmpReg).addReg(ValueReg); |
| 8859 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
| 8860 | .addImm(CmpPred).addReg(PPC::CR0).addMBB(exitMBB); |
| 8861 | BB->addSuccessor(loop2MBB); |
| 8862 | BB->addSuccessor(exitMBB); |
| 8863 | BB = loop2MBB; |
| 8864 | } |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 8865 | BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8866 | .addReg(Tmp3Reg).addReg(Tmp2Reg); |
| Bill Schmidt | 3581cd4 | 2013-04-02 18:37:08 +0000 | [diff] [blame] | 8867 | BuildMI(BB, dl, TII->get(PPC::STWCX)) |
| Jakob Stoklund Olesen | 7067bff | 2011-04-04 17:07:06 +0000 | [diff] [blame] | 8868 | .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 8869 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8870 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8871 | BB->addSuccessor(loopMBB); |
| 8872 | BB->addSuccessor(exitMBB); |
| 8873 | |
| 8874 | // exitMBB: |
| 8875 | // ... |
| 8876 | BB = exitMBB; |
| Jakob Stoklund Olesen | 13ce236 | 2011-04-04 17:57:29 +0000 | [diff] [blame] | 8877 | BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg) |
| 8878 | .addReg(ShiftReg); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 8879 | return BB; |
| 8880 | } |
| 8881 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 8882 | llvm::MachineBasicBlock * |
| 8883 | PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr &MI, |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 8884 | MachineBasicBlock *MBB) const { |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 8885 | DebugLoc DL = MI.getDebugLoc(); |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 8886 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 8887 | |
| 8888 | MachineFunction *MF = MBB->getParent(); |
| 8889 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 8890 | |
| 8891 | const BasicBlock *BB = MBB->getBasicBlock(); |
| Duncan P. N. Exon Smith | ac65b4c | 2015-10-20 01:07:37 +0000 | [diff] [blame] | 8892 | MachineFunction::iterator I = ++MBB->getIterator(); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 8893 | |
| 8894 | // Memory Reference |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 8895 | MachineInstr::mmo_iterator MMOBegin = MI.memoperands_begin(); |
| 8896 | MachineInstr::mmo_iterator MMOEnd = MI.memoperands_end(); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 8897 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 8898 | unsigned DstReg = MI.getOperand(0).getReg(); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 8899 | const TargetRegisterClass *RC = MRI.getRegClass(DstReg); |
| 8900 | assert(RC->hasType(MVT::i32) && "Invalid destination!"); |
| 8901 | unsigned mainDstReg = MRI.createVirtualRegister(RC); |
| 8902 | unsigned restoreDstReg = MRI.createVirtualRegister(RC); |
| 8903 | |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 8904 | MVT PVT = getPointerTy(MF->getDataLayout()); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 8905 | assert((PVT == MVT::i64 || PVT == MVT::i32) && |
| 8906 | "Invalid Pointer Size!"); |
| 8907 | // For v = setjmp(buf), we generate |
| 8908 | // |
| 8909 | // thisMBB: |
| 8910 | // SjLjSetup mainMBB |
| 8911 | // bl mainMBB |
| 8912 | // v_restore = 1 |
| 8913 | // b sinkMBB |
| 8914 | // |
| 8915 | // mainMBB: |
| 8916 | // buf[LabelOffset] = LR |
| 8917 | // v_main = 0 |
| 8918 | // |
| 8919 | // sinkMBB: |
| 8920 | // v = phi(main, restore) |
| 8921 | // |
| 8922 | |
| 8923 | MachineBasicBlock *thisMBB = MBB; |
| 8924 | MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB); |
| 8925 | MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB); |
| 8926 | MF->insert(I, mainMBB); |
| 8927 | MF->insert(I, sinkMBB); |
| 8928 | |
| 8929 | MachineInstrBuilder MIB; |
| 8930 | |
| 8931 | // Transfer the remainder of BB and its successor edges to sinkMBB. |
| 8932 | sinkMBB->splice(sinkMBB->begin(), MBB, |
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 8933 | std::next(MachineBasicBlock::iterator(MI)), MBB->end()); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 8934 | sinkMBB->transferSuccessorsAndUpdatePHIs(MBB); |
| 8935 | |
| 8936 | // Note that the structure of the jmp_buf used here is not compatible |
| 8937 | // with that used by libc, and is not designed to be. Specifically, it |
| 8938 | // stores only those 'reserved' registers that LLVM does not otherwise |
| 8939 | // understand how to spill. Also, by convention, by the time this |
| 8940 | // intrinsic is called, Clang has already stored the frame address in the |
| 8941 | // first slot of the buffer and stack address in the third. Following the |
| 8942 | // X86 target code, we'll store the jump address in the second slot. We also |
| 8943 | // need to save the TOC pointer (R2) to handle jumps between shared |
| 8944 | // libraries, and that will be stored in the fourth slot. The thread |
| 8945 | // identifier (R13) is not affected. |
| 8946 | |
| 8947 | // thisMBB: |
| 8948 | const int64_t LabelOffset = 1 * PVT.getStoreSize(); |
| 8949 | const int64_t TOCOffset = 3 * PVT.getStoreSize(); |
| Hal Finkel | f05d6c7 | 2013-07-17 23:50:51 +0000 | [diff] [blame] | 8950 | const int64_t BPOffset = 4 * PVT.getStoreSize(); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 8951 | |
| 8952 | // Prepare IP either in reg. |
| 8953 | const TargetRegisterClass *PtrRC = getRegClassFor(PVT); |
| 8954 | unsigned LabelReg = MRI.createVirtualRegister(PtrRC); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 8955 | unsigned BufReg = MI.getOperand(1).getReg(); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 8956 | |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 8957 | if (Subtarget.isPPC64() && Subtarget.isSVR4ABI()) { |
| Hal Finkel | e6698d5 | 2015-02-01 15:03:28 +0000 | [diff] [blame] | 8958 | setUsesTOCBasePtr(*MBB->getParent()); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 8959 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD)) |
| 8960 | .addReg(PPC::X2) |
| Ulrich Weigand | 9d980cb | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 8961 | .addImm(TOCOffset) |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 8962 | .addReg(BufReg); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 8963 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 8964 | } |
| 8965 | |
| Hal Finkel | f05d6c7 | 2013-07-17 23:50:51 +0000 | [diff] [blame] | 8966 | // Naked functions never have a base pointer, and so we use r1. For all |
| 8967 | // other functions, this decision must be delayed until during PEI. |
| 8968 | unsigned BaseReg; |
| Duncan P. N. Exon Smith | 5bedaf93 | 2015-02-14 02:54:07 +0000 | [diff] [blame] | 8969 | if (MF->getFunction()->hasFnAttribute(Attribute::Naked)) |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 8970 | BaseReg = Subtarget.isPPC64() ? PPC::X1 : PPC::R1; |
| Hal Finkel | f05d6c7 | 2013-07-17 23:50:51 +0000 | [diff] [blame] | 8971 | else |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 8972 | BaseReg = Subtarget.isPPC64() ? PPC::BP8 : PPC::BP; |
| Hal Finkel | f05d6c7 | 2013-07-17 23:50:51 +0000 | [diff] [blame] | 8973 | |
| 8974 | MIB = BuildMI(*thisMBB, MI, DL, |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 8975 | TII->get(Subtarget.isPPC64() ? PPC::STD : PPC::STW)) |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 8976 | .addReg(BaseReg) |
| 8977 | .addImm(BPOffset) |
| 8978 | .addReg(BufReg); |
| Hal Finkel | f05d6c7 | 2013-07-17 23:50:51 +0000 | [diff] [blame] | 8979 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 8980 | |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 8981 | // Setup |
| Hal Finkel | e5680b3 | 2013-04-04 22:55:54 +0000 | [diff] [blame] | 8982 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB); |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 8983 | const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo(); |
| Bill Wendling | 5e7656b | 2013-06-07 07:55:53 +0000 | [diff] [blame] | 8984 | MIB.addRegMask(TRI->getNoPreservedMask()); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 8985 | |
| 8986 | BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1); |
| 8987 | |
| 8988 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup)) |
| 8989 | .addMBB(mainMBB); |
| 8990 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB); |
| 8991 | |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 8992 | thisMBB->addSuccessor(mainMBB, BranchProbability::getZero()); |
| 8993 | thisMBB->addSuccessor(sinkMBB, BranchProbability::getOne()); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 8994 | |
| 8995 | // mainMBB: |
| 8996 | // mainDstReg = 0 |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 8997 | MIB = |
| 8998 | BuildMI(mainMBB, DL, |
| 8999 | TII->get(Subtarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9000 | |
| 9001 | // Store IP |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 9002 | if (Subtarget.isPPC64()) { |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9003 | MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD)) |
| 9004 | .addReg(LabelReg) |
| Ulrich Weigand | 9d980cb | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 9005 | .addImm(LabelOffset) |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9006 | .addReg(BufReg); |
| 9007 | } else { |
| 9008 | MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW)) |
| 9009 | .addReg(LabelReg) |
| 9010 | .addImm(LabelOffset) |
| 9011 | .addReg(BufReg); |
| 9012 | } |
| 9013 | |
| 9014 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 9015 | |
| 9016 | BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0); |
| 9017 | mainMBB->addSuccessor(sinkMBB); |
| 9018 | |
| 9019 | // sinkMBB: |
| 9020 | BuildMI(*sinkMBB, sinkMBB->begin(), DL, |
| 9021 | TII->get(PPC::PHI), DstReg) |
| 9022 | .addReg(mainDstReg).addMBB(mainMBB) |
| 9023 | .addReg(restoreDstReg).addMBB(thisMBB); |
| 9024 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9025 | MI.eraseFromParent(); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9026 | return sinkMBB; |
| 9027 | } |
| 9028 | |
| 9029 | MachineBasicBlock * |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9030 | PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr &MI, |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9031 | MachineBasicBlock *MBB) const { |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9032 | DebugLoc DL = MI.getDebugLoc(); |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 9033 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9034 | |
| 9035 | MachineFunction *MF = MBB->getParent(); |
| 9036 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 9037 | |
| 9038 | // Memory Reference |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9039 | MachineInstr::mmo_iterator MMOBegin = MI.memoperands_begin(); |
| 9040 | MachineInstr::mmo_iterator MMOEnd = MI.memoperands_end(); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9041 | |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 9042 | MVT PVT = getPointerTy(MF->getDataLayout()); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9043 | assert((PVT == MVT::i64 || PVT == MVT::i32) && |
| 9044 | "Invalid Pointer Size!"); |
| 9045 | |
| 9046 | const TargetRegisterClass *RC = |
| 9047 | (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass; |
| 9048 | unsigned Tmp = MRI.createVirtualRegister(RC); |
| 9049 | // Since FP is only updated here but NOT referenced, it's treated as GPR. |
| 9050 | unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31; |
| 9051 | unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1; |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 9052 | unsigned BP = |
| 9053 | (PVT == MVT::i64) |
| 9054 | ? PPC::X30 |
| Rafael Espindola | 21d22a0 | 2016-06-27 14:05:43 +0000 | [diff] [blame] | 9055 | : (Subtarget.isSVR4ABI() && isPositionIndependent() ? PPC::R29 |
| 9056 | : PPC::R30); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9057 | |
| 9058 | MachineInstrBuilder MIB; |
| 9059 | |
| 9060 | const int64_t LabelOffset = 1 * PVT.getStoreSize(); |
| 9061 | const int64_t SPOffset = 2 * PVT.getStoreSize(); |
| 9062 | const int64_t TOCOffset = 3 * PVT.getStoreSize(); |
| Hal Finkel | f05d6c7 | 2013-07-17 23:50:51 +0000 | [diff] [blame] | 9063 | const int64_t BPOffset = 4 * PVT.getStoreSize(); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9064 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9065 | unsigned BufReg = MI.getOperand(0).getReg(); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9066 | |
| 9067 | // Reload FP (the jumped-to function may not have had a |
| 9068 | // frame pointer, and if so, then its r31 will be restored |
| 9069 | // as necessary). |
| 9070 | if (PVT == MVT::i64) { |
| 9071 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP) |
| 9072 | .addImm(0) |
| 9073 | .addReg(BufReg); |
| 9074 | } else { |
| 9075 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP) |
| 9076 | .addImm(0) |
| 9077 | .addReg(BufReg); |
| 9078 | } |
| 9079 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 9080 | |
| 9081 | // Reload IP |
| 9082 | if (PVT == MVT::i64) { |
| 9083 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp) |
| Ulrich Weigand | 9d980cb | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 9084 | .addImm(LabelOffset) |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9085 | .addReg(BufReg); |
| 9086 | } else { |
| 9087 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp) |
| 9088 | .addImm(LabelOffset) |
| 9089 | .addReg(BufReg); |
| 9090 | } |
| 9091 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 9092 | |
| 9093 | // Reload SP |
| 9094 | if (PVT == MVT::i64) { |
| 9095 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP) |
| Ulrich Weigand | 9d980cb | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 9096 | .addImm(SPOffset) |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9097 | .addReg(BufReg); |
| 9098 | } else { |
| 9099 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP) |
| 9100 | .addImm(SPOffset) |
| 9101 | .addReg(BufReg); |
| 9102 | } |
| 9103 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 9104 | |
| Hal Finkel | f05d6c7 | 2013-07-17 23:50:51 +0000 | [diff] [blame] | 9105 | // Reload BP |
| 9106 | if (PVT == MVT::i64) { |
| 9107 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP) |
| 9108 | .addImm(BPOffset) |
| 9109 | .addReg(BufReg); |
| 9110 | } else { |
| 9111 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP) |
| 9112 | .addImm(BPOffset) |
| 9113 | .addReg(BufReg); |
| 9114 | } |
| 9115 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9116 | |
| 9117 | // Reload TOC |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 9118 | if (PVT == MVT::i64 && Subtarget.isSVR4ABI()) { |
| Hal Finkel | e6698d5 | 2015-02-01 15:03:28 +0000 | [diff] [blame] | 9119 | setUsesTOCBasePtr(*MBB->getParent()); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9120 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2) |
| Ulrich Weigand | 9d980cb | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 9121 | .addImm(TOCOffset) |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9122 | .addReg(BufReg); |
| 9123 | |
| 9124 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 9125 | } |
| 9126 | |
| 9127 | // Jump |
| 9128 | BuildMI(*MBB, MI, DL, |
| 9129 | TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp); |
| 9130 | BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR)); |
| 9131 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9132 | MI.eraseFromParent(); |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9133 | return MBB; |
| 9134 | } |
| 9135 | |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 9136 | MachineBasicBlock * |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9137 | PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, |
| Dan Gohman | 25c1653 | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 9138 | MachineBasicBlock *BB) const { |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9139 | if (MI.getOpcode() == TargetOpcode::STACKMAP || |
| 9140 | MI.getOpcode() == TargetOpcode::PATCHPOINT) { |
| Hal Finkel | af51993 | 2015-01-19 07:20:27 +0000 | [diff] [blame] | 9141 | if (Subtarget.isPPC64() && Subtarget.isSVR4ABI() && |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9142 | MI.getOpcode() == TargetOpcode::PATCHPOINT) { |
| Hal Finkel | af51993 | 2015-01-19 07:20:27 +0000 | [diff] [blame] | 9143 | // Call lowering should have added an r2 operand to indicate a dependence |
| 9144 | // on the TOC base pointer value. It can't however, because there is no |
| 9145 | // way to mark the dependence as implicit there, and so the stackmap code |
| 9146 | // will confuse it with a regular operand. Instead, add the dependence |
| 9147 | // here. |
| Hal Finkel | e6698d5 | 2015-02-01 15:03:28 +0000 | [diff] [blame] | 9148 | setUsesTOCBasePtr(*BB->getParent()); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9149 | MI.addOperand(MachineOperand::CreateReg(PPC::X2, false, true)); |
| Hal Finkel | af51993 | 2015-01-19 07:20:27 +0000 | [diff] [blame] | 9150 | } |
| 9151 | |
| Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 9152 | return emitPatchPoint(MI, BB); |
| Hal Finkel | af51993 | 2015-01-19 07:20:27 +0000 | [diff] [blame] | 9153 | } |
| Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 9154 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9155 | if (MI.getOpcode() == PPC::EH_SjLj_SetJmp32 || |
| 9156 | MI.getOpcode() == PPC::EH_SjLj_SetJmp64) { |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9157 | return emitEHSjLjSetJmp(MI, BB); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9158 | } else if (MI.getOpcode() == PPC::EH_SjLj_LongJmp32 || |
| 9159 | MI.getOpcode() == PPC::EH_SjLj_LongJmp64) { |
| Hal Finkel | 756810f | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 9160 | return emitEHSjLjLongJmp(MI, BB); |
| 9161 | } |
| 9162 | |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 9163 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9164 | |
| 9165 | // To "insert" these instructions we actually have to insert their |
| 9166 | // control-flow patterns. |
| Chris Lattner | 9b577f1 | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 9167 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| Duncan P. N. Exon Smith | ac65b4c | 2015-10-20 01:07:37 +0000 | [diff] [blame] | 9168 | MachineFunction::iterator It = ++BB->getIterator(); |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9169 | |
| Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 9170 | MachineFunction *F = BB->getParent(); |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9171 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9172 | if (Subtarget.hasISEL() && |
| 9173 | (MI.getOpcode() == PPC::SELECT_CC_I4 || |
| 9174 | MI.getOpcode() == PPC::SELECT_CC_I8 || |
| 9175 | MI.getOpcode() == PPC::SELECT_I4 || MI.getOpcode() == PPC::SELECT_I8)) { |
| Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 9176 | SmallVector<MachineOperand, 2> Cond; |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9177 | if (MI.getOpcode() == PPC::SELECT_CC_I4 || |
| 9178 | MI.getOpcode() == PPC::SELECT_CC_I8) |
| 9179 | Cond.push_back(MI.getOperand(4)); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 9180 | else |
| 9181 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9182 | Cond.push_back(MI.getOperand(1)); |
| Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 9183 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9184 | DebugLoc dl = MI.getDebugLoc(); |
| 9185 | TII->insertSelect(*BB, MI, dl, MI.getOperand(0).getReg(), Cond, |
| 9186 | MI.getOperand(2).getReg(), MI.getOperand(3).getReg()); |
| 9187 | } else if (MI.getOpcode() == PPC::SELECT_CC_I4 || |
| 9188 | MI.getOpcode() == PPC::SELECT_CC_I8 || |
| 9189 | MI.getOpcode() == PPC::SELECT_CC_F4 || |
| 9190 | MI.getOpcode() == PPC::SELECT_CC_F8 || |
| 9191 | MI.getOpcode() == PPC::SELECT_CC_QFRC || |
| 9192 | MI.getOpcode() == PPC::SELECT_CC_QSRC || |
| 9193 | MI.getOpcode() == PPC::SELECT_CC_QBRC || |
| 9194 | MI.getOpcode() == PPC::SELECT_CC_VRRC || |
| 9195 | MI.getOpcode() == PPC::SELECT_CC_VSFRC || |
| 9196 | MI.getOpcode() == PPC::SELECT_CC_VSSRC || |
| 9197 | MI.getOpcode() == PPC::SELECT_CC_VSRC || |
| 9198 | MI.getOpcode() == PPC::SELECT_I4 || |
| 9199 | MI.getOpcode() == PPC::SELECT_I8 || |
| 9200 | MI.getOpcode() == PPC::SELECT_F4 || |
| 9201 | MI.getOpcode() == PPC::SELECT_F8 || |
| 9202 | MI.getOpcode() == PPC::SELECT_QFRC || |
| 9203 | MI.getOpcode() == PPC::SELECT_QSRC || |
| 9204 | MI.getOpcode() == PPC::SELECT_QBRC || |
| 9205 | MI.getOpcode() == PPC::SELECT_VRRC || |
| 9206 | MI.getOpcode() == PPC::SELECT_VSFRC || |
| 9207 | MI.getOpcode() == PPC::SELECT_VSSRC || |
| 9208 | MI.getOpcode() == PPC::SELECT_VSRC) { |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9209 | // The incoming instruction knows the destination vreg to set, the |
| 9210 | // condition code register to branch on, the true/false values to |
| 9211 | // select between, and a branch opcode to use. |
| 9212 | |
| 9213 | // thisMBB: |
| 9214 | // ... |
| 9215 | // TrueVal = ... |
| 9216 | // cmpTY ccX, r1, r2 |
| 9217 | // bCC copy1MBB |
| 9218 | // fallthrough --> copy0MBB |
| 9219 | MachineBasicBlock *thisMBB = BB; |
| 9220 | MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 9221 | MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9222 | DebugLoc dl = MI.getDebugLoc(); |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9223 | F->insert(It, copy0MBB); |
| 9224 | F->insert(It, sinkMBB); |
| Dan Gohman | 3439629 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 9225 | |
| 9226 | // Transfer the remainder of BB and its successor edges to sinkMBB. |
| 9227 | sinkMBB->splice(sinkMBB->begin(), BB, |
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 9228 | std::next(MachineBasicBlock::iterator(MI)), BB->end()); |
| Dan Gohman | 3439629 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 9229 | sinkMBB->transferSuccessorsAndUpdatePHIs(BB); |
| 9230 | |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9231 | // Next, add the true and fallthrough blocks as its successors. |
| 9232 | BB->addSuccessor(copy0MBB); |
| 9233 | BB->addSuccessor(sinkMBB); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9234 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9235 | if (MI.getOpcode() == PPC::SELECT_I4 || MI.getOpcode() == PPC::SELECT_I8 || |
| 9236 | MI.getOpcode() == PPC::SELECT_F4 || MI.getOpcode() == PPC::SELECT_F8 || |
| 9237 | MI.getOpcode() == PPC::SELECT_QFRC || |
| 9238 | MI.getOpcode() == PPC::SELECT_QSRC || |
| 9239 | MI.getOpcode() == PPC::SELECT_QBRC || |
| 9240 | MI.getOpcode() == PPC::SELECT_VRRC || |
| 9241 | MI.getOpcode() == PPC::SELECT_VSFRC || |
| 9242 | MI.getOpcode() == PPC::SELECT_VSSRC || |
| 9243 | MI.getOpcode() == PPC::SELECT_VSRC) { |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 9244 | BuildMI(BB, dl, TII->get(PPC::BC)) |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9245 | .addReg(MI.getOperand(1).getReg()) |
| 9246 | .addMBB(sinkMBB); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 9247 | } else { |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9248 | unsigned SelectPred = MI.getOperand(4).getImm(); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 9249 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9250 | .addImm(SelectPred) |
| 9251 | .addReg(MI.getOperand(1).getReg()) |
| 9252 | .addMBB(sinkMBB); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 9253 | } |
| Dan Gohman | 3439629 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 9254 | |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9255 | // copy0MBB: |
| 9256 | // %FalseValue = ... |
| 9257 | // # fallthrough to sinkMBB |
| 9258 | BB = copy0MBB; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9259 | |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9260 | // Update machine-CFG edges |
| 9261 | BB->addSuccessor(sinkMBB); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9262 | |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9263 | // sinkMBB: |
| 9264 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 9265 | // ... |
| 9266 | BB = sinkMBB; |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9267 | BuildMI(*BB, BB->begin(), dl, TII->get(PPC::PHI), MI.getOperand(0).getReg()) |
| 9268 | .addReg(MI.getOperand(3).getReg()) |
| 9269 | .addMBB(copy0MBB) |
| 9270 | .addReg(MI.getOperand(2).getReg()) |
| 9271 | .addMBB(thisMBB); |
| 9272 | } else if (MI.getOpcode() == PPC::ReadTB) { |
| Hal Finkel | bbdee93 | 2014-12-02 22:01:00 +0000 | [diff] [blame] | 9273 | // To read the 64-bit time-base register on a 32-bit target, we read the |
| 9274 | // two halves. Should the counter have wrapped while it was being read, we |
| 9275 | // need to try again. |
| 9276 | // ... |
| 9277 | // readLoop: |
| 9278 | // mfspr Rx,TBU # load from TBU |
| 9279 | // mfspr Ry,TB # load from TB |
| 9280 | // mfspr Rz,TBU # load from TBU |
| NAKAMURA Takumi | bf9cc7f | 2015-09-22 11:10:08 +0000 | [diff] [blame] | 9281 | // cmpw crX,Rx,Rz # check if 'old'='new' |
| Hal Finkel | bbdee93 | 2014-12-02 22:01:00 +0000 | [diff] [blame] | 9282 | // bne readLoop # branch if they're not equal |
| 9283 | // ... |
| 9284 | |
| 9285 | MachineBasicBlock *readMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 9286 | MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9287 | DebugLoc dl = MI.getDebugLoc(); |
| Hal Finkel | bbdee93 | 2014-12-02 22:01:00 +0000 | [diff] [blame] | 9288 | F->insert(It, readMBB); |
| 9289 | F->insert(It, sinkMBB); |
| 9290 | |
| 9291 | // Transfer the remainder of BB and its successor edges to sinkMBB. |
| 9292 | sinkMBB->splice(sinkMBB->begin(), BB, |
| 9293 | std::next(MachineBasicBlock::iterator(MI)), BB->end()); |
| 9294 | sinkMBB->transferSuccessorsAndUpdatePHIs(BB); |
| 9295 | |
| 9296 | BB->addSuccessor(readMBB); |
| 9297 | BB = readMBB; |
| 9298 | |
| 9299 | MachineRegisterInfo &RegInfo = F->getRegInfo(); |
| 9300 | unsigned ReadAgainReg = RegInfo.createVirtualRegister(&PPC::GPRCRegClass); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9301 | unsigned LoReg = MI.getOperand(0).getReg(); |
| 9302 | unsigned HiReg = MI.getOperand(1).getReg(); |
| Hal Finkel | bbdee93 | 2014-12-02 22:01:00 +0000 | [diff] [blame] | 9303 | |
| 9304 | BuildMI(BB, dl, TII->get(PPC::MFSPR), HiReg).addImm(269); |
| 9305 | BuildMI(BB, dl, TII->get(PPC::MFSPR), LoReg).addImm(268); |
| 9306 | BuildMI(BB, dl, TII->get(PPC::MFSPR), ReadAgainReg).addImm(269); |
| 9307 | |
| 9308 | unsigned CmpReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass); |
| 9309 | |
| 9310 | BuildMI(BB, dl, TII->get(PPC::CMPW), CmpReg) |
| 9311 | .addReg(HiReg).addReg(ReadAgainReg); |
| 9312 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
| 9313 | .addImm(PPC::PRED_NE).addReg(CmpReg).addMBB(readMBB); |
| 9314 | |
| 9315 | BB->addSuccessor(readMBB); |
| 9316 | BB->addSuccessor(sinkMBB); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9317 | } else if (MI.getOpcode() == PPC::ATOMIC_LOAD_ADD_I8) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 9318 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9319 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_ADD_I16) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 9320 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9321 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_ADD_I32) |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9322 | BB = EmitAtomicBinary(MI, BB, 4, PPC::ADD4); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9323 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_ADD_I64) |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9324 | BB = EmitAtomicBinary(MI, BB, 8, PPC::ADD8); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 9325 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9326 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_AND_I8) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 9327 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9328 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_AND_I16) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 9329 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9330 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_AND_I32) |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9331 | BB = EmitAtomicBinary(MI, BB, 4, PPC::AND); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9332 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_AND_I64) |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9333 | BB = EmitAtomicBinary(MI, BB, 8, PPC::AND8); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 9334 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9335 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_OR_I8) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 9336 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9337 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_OR_I16) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 9338 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9339 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_OR_I32) |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9340 | BB = EmitAtomicBinary(MI, BB, 4, PPC::OR); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9341 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_OR_I64) |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9342 | BB = EmitAtomicBinary(MI, BB, 8, PPC::OR8); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 9343 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9344 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_XOR_I8) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 9345 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9346 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_XOR_I16) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 9347 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9348 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_XOR_I32) |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9349 | BB = EmitAtomicBinary(MI, BB, 4, PPC::XOR); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9350 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_XOR_I64) |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9351 | BB = EmitAtomicBinary(MI, BB, 8, PPC::XOR8); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 9352 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9353 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_NAND_I8) |
| Ulrich Weigand | 862d8b8 | 2014-07-08 16:16:02 +0000 | [diff] [blame] | 9354 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::NAND); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9355 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_NAND_I16) |
| Ulrich Weigand | 862d8b8 | 2014-07-08 16:16:02 +0000 | [diff] [blame] | 9356 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::NAND); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9357 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_NAND_I32) |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9358 | BB = EmitAtomicBinary(MI, BB, 4, PPC::NAND); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9359 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_NAND_I64) |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9360 | BB = EmitAtomicBinary(MI, BB, 8, PPC::NAND8); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 9361 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9362 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_SUB_I8) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 9363 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9364 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_SUB_I16) |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 9365 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9366 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_SUB_I32) |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9367 | BB = EmitAtomicBinary(MI, BB, 4, PPC::SUBF); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9368 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_SUB_I64) |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9369 | BB = EmitAtomicBinary(MI, BB, 8, PPC::SUBF8); |
| Dale Johannesen | a32affb | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 9370 | |
| Hal Finkel | 5728200 | 2016-08-28 16:17:58 +0000 | [diff] [blame] | 9371 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MIN_I8) |
| 9372 | BB = EmitPartwordAtomicBinary(MI, BB, true, 0, PPC::CMPW, PPC::PRED_GE); |
| 9373 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MIN_I16) |
| 9374 | BB = EmitPartwordAtomicBinary(MI, BB, false, 0, PPC::CMPW, PPC::PRED_GE); |
| 9375 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MIN_I32) |
| 9376 | BB = EmitAtomicBinary(MI, BB, 4, 0, PPC::CMPW, PPC::PRED_GE); |
| 9377 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MIN_I64) |
| 9378 | BB = EmitAtomicBinary(MI, BB, 8, 0, PPC::CMPD, PPC::PRED_GE); |
| 9379 | |
| 9380 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MAX_I8) |
| 9381 | BB = EmitPartwordAtomicBinary(MI, BB, true, 0, PPC::CMPW, PPC::PRED_LE); |
| 9382 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MAX_I16) |
| 9383 | BB = EmitPartwordAtomicBinary(MI, BB, false, 0, PPC::CMPW, PPC::PRED_LE); |
| 9384 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MAX_I32) |
| 9385 | BB = EmitAtomicBinary(MI, BB, 4, 0, PPC::CMPW, PPC::PRED_LE); |
| 9386 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_MAX_I64) |
| 9387 | BB = EmitAtomicBinary(MI, BB, 8, 0, PPC::CMPD, PPC::PRED_LE); |
| 9388 | |
| 9389 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMIN_I8) |
| 9390 | BB = EmitPartwordAtomicBinary(MI, BB, true, 0, PPC::CMPLW, PPC::PRED_GE); |
| 9391 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMIN_I16) |
| 9392 | BB = EmitPartwordAtomicBinary(MI, BB, false, 0, PPC::CMPLW, PPC::PRED_GE); |
| 9393 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMIN_I32) |
| 9394 | BB = EmitAtomicBinary(MI, BB, 4, 0, PPC::CMPLW, PPC::PRED_GE); |
| 9395 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMIN_I64) |
| 9396 | BB = EmitAtomicBinary(MI, BB, 8, 0, PPC::CMPLD, PPC::PRED_GE); |
| 9397 | |
| 9398 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMAX_I8) |
| 9399 | BB = EmitPartwordAtomicBinary(MI, BB, true, 0, PPC::CMPLW, PPC::PRED_LE); |
| 9400 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMAX_I16) |
| 9401 | BB = EmitPartwordAtomicBinary(MI, BB, false, 0, PPC::CMPLW, PPC::PRED_LE); |
| 9402 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMAX_I32) |
| 9403 | BB = EmitAtomicBinary(MI, BB, 4, 0, PPC::CMPLW, PPC::PRED_LE); |
| 9404 | else if (MI.getOpcode() == PPC::ATOMIC_LOAD_UMAX_I64) |
| 9405 | BB = EmitAtomicBinary(MI, BB, 8, 0, PPC::CMPLD, PPC::PRED_LE); |
| 9406 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9407 | else if (MI.getOpcode() == PPC::ATOMIC_SWAP_I8) |
| Dale Johannesen | f0a88d6 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 9408 | BB = EmitPartwordAtomicBinary(MI, BB, true, 0); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9409 | else if (MI.getOpcode() == PPC::ATOMIC_SWAP_I16) |
| Dale Johannesen | f0a88d6 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 9410 | BB = EmitPartwordAtomicBinary(MI, BB, false, 0); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9411 | else if (MI.getOpcode() == PPC::ATOMIC_SWAP_I32) |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9412 | BB = EmitAtomicBinary(MI, BB, 4, 0); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9413 | else if (MI.getOpcode() == PPC::ATOMIC_SWAP_I64) |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9414 | BB = EmitAtomicBinary(MI, BB, 8, 0); |
| Dale Johannesen | f0a88d6 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 9415 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9416 | else if (MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 || |
| 9417 | MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I64 || |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9418 | (Subtarget.hasPartwordAtomics() && |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9419 | MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I8) || |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9420 | (Subtarget.hasPartwordAtomics() && |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9421 | MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I16)) { |
| 9422 | bool is64bit = MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I64; |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9423 | |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9424 | auto LoadMnemonic = PPC::LDARX; |
| 9425 | auto StoreMnemonic = PPC::STDCX; |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9426 | switch (MI.getOpcode()) { |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9427 | default: |
| 9428 | llvm_unreachable("Compare and swap of unknown size"); |
| 9429 | case PPC::ATOMIC_CMP_SWAP_I8: |
| 9430 | LoadMnemonic = PPC::LBARX; |
| 9431 | StoreMnemonic = PPC::STBCX; |
| 9432 | assert(Subtarget.hasPartwordAtomics() && "No support partword atomics."); |
| 9433 | break; |
| 9434 | case PPC::ATOMIC_CMP_SWAP_I16: |
| 9435 | LoadMnemonic = PPC::LHARX; |
| 9436 | StoreMnemonic = PPC::STHCX; |
| 9437 | assert(Subtarget.hasPartwordAtomics() && "No support partword atomics."); |
| 9438 | break; |
| 9439 | case PPC::ATOMIC_CMP_SWAP_I32: |
| 9440 | LoadMnemonic = PPC::LWARX; |
| 9441 | StoreMnemonic = PPC::STWCX; |
| 9442 | break; |
| 9443 | case PPC::ATOMIC_CMP_SWAP_I64: |
| 9444 | LoadMnemonic = PPC::LDARX; |
| 9445 | StoreMnemonic = PPC::STDCX; |
| 9446 | break; |
| 9447 | } |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9448 | unsigned dest = MI.getOperand(0).getReg(); |
| 9449 | unsigned ptrA = MI.getOperand(1).getReg(); |
| 9450 | unsigned ptrB = MI.getOperand(2).getReg(); |
| 9451 | unsigned oldval = MI.getOperand(3).getReg(); |
| 9452 | unsigned newval = MI.getOperand(4).getReg(); |
| 9453 | DebugLoc dl = MI.getDebugLoc(); |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9454 | |
| Dale Johannesen | 166d6cb | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 9455 | MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 9456 | MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 9457 | MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9458 | MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| Dale Johannesen | 166d6cb | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 9459 | F->insert(It, loop1MBB); |
| 9460 | F->insert(It, loop2MBB); |
| 9461 | F->insert(It, midMBB); |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9462 | F->insert(It, exitMBB); |
| Dan Gohman | 3439629 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 9463 | exitMBB->splice(exitMBB->begin(), BB, |
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 9464 | std::next(MachineBasicBlock::iterator(MI)), BB->end()); |
| Dan Gohman | 3439629 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 9465 | exitMBB->transferSuccessorsAndUpdatePHIs(BB); |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9466 | |
| 9467 | // thisMBB: |
| 9468 | // ... |
| 9469 | // fallthrough --> loopMBB |
| Dale Johannesen | 166d6cb | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 9470 | BB->addSuccessor(loop1MBB); |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9471 | |
| Dale Johannesen | 166d6cb | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 9472 | // loop1MBB: |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9473 | // l[bhwd]arx dest, ptr |
| Dale Johannesen | 166d6cb | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 9474 | // cmp[wd] dest, oldval |
| 9475 | // bne- midMBB |
| 9476 | // loop2MBB: |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9477 | // st[bhwd]cx. newval, ptr |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9478 | // bne- loopMBB |
| Dale Johannesen | 166d6cb | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 9479 | // b exitBB |
| 9480 | // midMBB: |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9481 | // st[bhwd]cx. dest, ptr |
| Dale Johannesen | 166d6cb | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 9482 | // exitBB: |
| 9483 | BB = loop1MBB; |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9484 | BuildMI(BB, dl, TII->get(LoadMnemonic), dest) |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9485 | .addReg(ptrA).addReg(ptrB); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9486 | BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0) |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9487 | .addReg(oldval).addReg(dest); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9488 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
| Dale Johannesen | 166d6cb | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 9489 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB); |
| 9490 | BB->addSuccessor(loop2MBB); |
| 9491 | BB->addSuccessor(midMBB); |
| 9492 | |
| 9493 | BB = loop2MBB; |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9494 | BuildMI(BB, dl, TII->get(StoreMnemonic)) |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9495 | .addReg(newval).addReg(ptrA).addReg(ptrB); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9496 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
| Dale Johannesen | 166d6cb | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 9497 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9498 | BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB); |
| Dale Johannesen | 166d6cb | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 9499 | BB->addSuccessor(loop1MBB); |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9500 | BB->addSuccessor(exitMBB); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9501 | |
| Dale Johannesen | 166d6cb | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 9502 | BB = midMBB; |
| Nemanja Ivanovic | 0adf26b | 2015-03-10 20:51:07 +0000 | [diff] [blame] | 9503 | BuildMI(BB, dl, TII->get(StoreMnemonic)) |
| Dale Johannesen | 166d6cb | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 9504 | .addReg(dest).addReg(ptrA).addReg(ptrB); |
| 9505 | BB->addSuccessor(exitMBB); |
| 9506 | |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9507 | // exitMBB: |
| 9508 | // ... |
| 9509 | BB = exitMBB; |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9510 | } else if (MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 || |
| 9511 | MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) { |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9512 | // We must use 64-bit registers for addresses when targeting 64-bit, |
| 9513 | // since we're actually doing arithmetic on them. Other registers |
| 9514 | // can be 32-bit. |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 9515 | bool is64bit = Subtarget.isPPC64(); |
| Hal Finkel | 3d70a9d | 2016-08-29 22:25:36 +0000 | [diff] [blame] | 9516 | bool isLittleEndian = Subtarget.isLittleEndian(); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9517 | bool is8bit = MI.getOpcode() == PPC::ATOMIC_CMP_SWAP_I8; |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9518 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9519 | unsigned dest = MI.getOperand(0).getReg(); |
| 9520 | unsigned ptrA = MI.getOperand(1).getReg(); |
| 9521 | unsigned ptrB = MI.getOperand(2).getReg(); |
| 9522 | unsigned oldval = MI.getOperand(3).getReg(); |
| 9523 | unsigned newval = MI.getOperand(4).getReg(); |
| 9524 | DebugLoc dl = MI.getDebugLoc(); |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9525 | |
| 9526 | MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 9527 | MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 9528 | MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 9529 | MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 9530 | F->insert(It, loop1MBB); |
| 9531 | F->insert(It, loop2MBB); |
| 9532 | F->insert(It, midMBB); |
| 9533 | F->insert(It, exitMBB); |
| Dan Gohman | 3439629 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 9534 | exitMBB->splice(exitMBB->begin(), BB, |
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 9535 | std::next(MachineBasicBlock::iterator(MI)), BB->end()); |
| Dan Gohman | 3439629 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 9536 | exitMBB->transferSuccessorsAndUpdatePHIs(BB); |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9537 | |
| 9538 | MachineRegisterInfo &RegInfo = F->getRegInfo(); |
| Craig Topper | 61e88f4 | 2014-11-21 05:58:21 +0000 | [diff] [blame] | 9539 | const TargetRegisterClass *RC = is64bit ? &PPC::G8RCRegClass |
| 9540 | : &PPC::GPRCRegClass; |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9541 | unsigned PtrReg = RegInfo.createVirtualRegister(RC); |
| 9542 | unsigned Shift1Reg = RegInfo.createVirtualRegister(RC); |
| Hal Finkel | 3d70a9d | 2016-08-29 22:25:36 +0000 | [diff] [blame] | 9543 | unsigned ShiftReg = |
| 9544 | isLittleEndian ? Shift1Reg : RegInfo.createVirtualRegister(RC); |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9545 | unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC); |
| 9546 | unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC); |
| 9547 | unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC); |
| 9548 | unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC); |
| 9549 | unsigned MaskReg = RegInfo.createVirtualRegister(RC); |
| 9550 | unsigned Mask2Reg = RegInfo.createVirtualRegister(RC); |
| 9551 | unsigned Mask3Reg = RegInfo.createVirtualRegister(RC); |
| 9552 | unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC); |
| 9553 | unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC); |
| 9554 | unsigned TmpDestReg = RegInfo.createVirtualRegister(RC); |
| 9555 | unsigned Ptr1Reg; |
| 9556 | unsigned TmpReg = RegInfo.createVirtualRegister(RC); |
| Hal Finkel | f70c41e | 2013-03-21 23:45:03 +0000 | [diff] [blame] | 9557 | unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO; |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9558 | // thisMBB: |
| 9559 | // ... |
| 9560 | // fallthrough --> loopMBB |
| 9561 | BB->addSuccessor(loop1MBB); |
| 9562 | |
| 9563 | // The 4-byte load must be aligned, while a char or short may be |
| 9564 | // anywhere in the word. Hence all this nasty bookkeeping code. |
| 9565 | // add ptr1, ptrA, ptrB [copy if ptrA==0] |
| 9566 | // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27] |
| Dale Johannesen | bc69829 | 2008-09-02 20:30:23 +0000 | [diff] [blame] | 9567 | // xori shift, shift1, 24 [16] |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9568 | // rlwinm ptr, ptr1, 0, 0, 29 |
| 9569 | // slw newval2, newval, shift |
| 9570 | // slw oldval2, oldval,shift |
| 9571 | // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535] |
| 9572 | // slw mask, mask2, shift |
| 9573 | // and newval3, newval2, mask |
| 9574 | // and oldval3, oldval2, mask |
| 9575 | // loop1MBB: |
| 9576 | // lwarx tmpDest, ptr |
| 9577 | // and tmp, tmpDest, mask |
| 9578 | // cmpw tmp, oldval3 |
| 9579 | // bne- midMBB |
| 9580 | // loop2MBB: |
| 9581 | // andc tmp2, tmpDest, mask |
| 9582 | // or tmp4, tmp2, newval3 |
| 9583 | // stwcx. tmp4, ptr |
| 9584 | // bne- loop1MBB |
| 9585 | // b exitBB |
| 9586 | // midMBB: |
| 9587 | // stwcx. tmpDest, ptr |
| 9588 | // exitBB: |
| 9589 | // srw dest, tmpDest, shift |
| Jakob Stoklund Olesen | 7067bff | 2011-04-04 17:07:06 +0000 | [diff] [blame] | 9590 | if (ptrA != ZeroReg) { |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9591 | Ptr1Reg = RegInfo.createVirtualRegister(RC); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9592 | BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg) |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9593 | .addReg(ptrA).addReg(ptrB); |
| 9594 | } else { |
| 9595 | Ptr1Reg = ptrB; |
| 9596 | } |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9597 | BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg) |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9598 | .addImm(3).addImm(27).addImm(is8bit ? 28 : 27); |
| Hal Finkel | 3d70a9d | 2016-08-29 22:25:36 +0000 | [diff] [blame] | 9599 | if (!isLittleEndian) |
| 9600 | BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg) |
| 9601 | .addReg(Shift1Reg).addImm(is8bit ? 24 : 16); |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9602 | if (is64bit) |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9603 | BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg) |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9604 | .addReg(Ptr1Reg).addImm(0).addImm(61); |
| 9605 | else |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9606 | BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg) |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9607 | .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9608 | BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg) |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9609 | .addReg(newval).addReg(ShiftReg); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9610 | BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg) |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9611 | .addReg(oldval).addReg(ShiftReg); |
| 9612 | if (is8bit) |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9613 | BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255); |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9614 | else { |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9615 | BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0); |
| 9616 | BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg) |
| 9617 | .addReg(Mask3Reg).addImm(65535); |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9618 | } |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9619 | BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg) |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9620 | .addReg(Mask2Reg).addReg(ShiftReg); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9621 | BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg) |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9622 | .addReg(NewVal2Reg).addReg(MaskReg); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9623 | BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg) |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9624 | .addReg(OldVal2Reg).addReg(MaskReg); |
| 9625 | |
| 9626 | BB = loop1MBB; |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9627 | BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg) |
| Jakob Stoklund Olesen | 7067bff | 2011-04-04 17:07:06 +0000 | [diff] [blame] | 9628 | .addReg(ZeroReg).addReg(PtrReg); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9629 | BuildMI(BB, dl, TII->get(PPC::AND),TmpReg) |
| 9630 | .addReg(TmpDestReg).addReg(MaskReg); |
| 9631 | BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0) |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9632 | .addReg(TmpReg).addReg(OldVal3Reg); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9633 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9634 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB); |
| 9635 | BB->addSuccessor(loop2MBB); |
| 9636 | BB->addSuccessor(midMBB); |
| 9637 | |
| 9638 | BB = loop2MBB; |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9639 | BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg) |
| 9640 | .addReg(TmpDestReg).addReg(MaskReg); |
| 9641 | BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg) |
| 9642 | .addReg(Tmp2Reg).addReg(NewVal3Reg); |
| 9643 | BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg) |
| Jakob Stoklund Olesen | 7067bff | 2011-04-04 17:07:06 +0000 | [diff] [blame] | 9644 | .addReg(ZeroReg).addReg(PtrReg); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9645 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9646 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB); |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9647 | BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB); |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9648 | BB->addSuccessor(loop1MBB); |
| 9649 | BB->addSuccessor(exitMBB); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9650 | |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9651 | BB = midMBB; |
| Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 9652 | BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg) |
| Jakob Stoklund Olesen | 7067bff | 2011-04-04 17:07:06 +0000 | [diff] [blame] | 9653 | .addReg(ZeroReg).addReg(PtrReg); |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9654 | BB->addSuccessor(exitMBB); |
| 9655 | |
| 9656 | // exitMBB: |
| 9657 | // ... |
| 9658 | BB = exitMBB; |
| Jakob Stoklund Olesen | 13ce236 | 2011-04-04 17:57:29 +0000 | [diff] [blame] | 9659 | BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg) |
| 9660 | .addReg(ShiftReg); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9661 | } else if (MI.getOpcode() == PPC::FADDrtz) { |
| Ulrich Weigand | 874fc62 | 2013-03-26 10:56:22 +0000 | [diff] [blame] | 9662 | // This pseudo performs an FADD with rounding mode temporarily forced |
| 9663 | // to round-to-zero. We emit this via custom inserter since the FPSCR |
| 9664 | // is not modeled at the SelectionDAG level. |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9665 | unsigned Dest = MI.getOperand(0).getReg(); |
| 9666 | unsigned Src1 = MI.getOperand(1).getReg(); |
| 9667 | unsigned Src2 = MI.getOperand(2).getReg(); |
| 9668 | DebugLoc dl = MI.getDebugLoc(); |
| Ulrich Weigand | 874fc62 | 2013-03-26 10:56:22 +0000 | [diff] [blame] | 9669 | |
| 9670 | MachineRegisterInfo &RegInfo = F->getRegInfo(); |
| 9671 | unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass); |
| 9672 | |
| 9673 | // Save FPSCR value. |
| 9674 | BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg); |
| 9675 | |
| 9676 | // Set rounding mode to round-to-zero. |
| 9677 | BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31); |
| 9678 | BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30); |
| 9679 | |
| 9680 | // Perform addition. |
| 9681 | BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2); |
| 9682 | |
| 9683 | // Restore FPSCR value. |
| Hal Finkel | 6420216 | 2015-01-15 01:00:53 +0000 | [diff] [blame] | 9684 | BuildMI(*BB, MI, dl, TII->get(PPC::MTFSFb)).addImm(1).addReg(MFFSReg); |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9685 | } else if (MI.getOpcode() == PPC::ANDIo_1_EQ_BIT || |
| 9686 | MI.getOpcode() == PPC::ANDIo_1_GT_BIT || |
| 9687 | MI.getOpcode() == PPC::ANDIo_1_EQ_BIT8 || |
| 9688 | MI.getOpcode() == PPC::ANDIo_1_GT_BIT8) { |
| 9689 | unsigned Opcode = (MI.getOpcode() == PPC::ANDIo_1_EQ_BIT8 || |
| 9690 | MI.getOpcode() == PPC::ANDIo_1_GT_BIT8) |
| 9691 | ? PPC::ANDIo8 |
| 9692 | : PPC::ANDIo; |
| 9693 | bool isEQ = (MI.getOpcode() == PPC::ANDIo_1_EQ_BIT || |
| 9694 | MI.getOpcode() == PPC::ANDIo_1_EQ_BIT8); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 9695 | |
| 9696 | MachineRegisterInfo &RegInfo = F->getRegInfo(); |
| 9697 | unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ? |
| 9698 | &PPC::GPRCRegClass : |
| 9699 | &PPC::G8RCRegClass); |
| 9700 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9701 | DebugLoc dl = MI.getDebugLoc(); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 9702 | BuildMI(*BB, MI, dl, TII->get(Opcode), Dest) |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9703 | .addReg(MI.getOperand(1).getReg()) |
| 9704 | .addImm(1); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 9705 | BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9706 | MI.getOperand(0).getReg()) |
| 9707 | .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT); |
| 9708 | } else if (MI.getOpcode() == PPC::TCHECK_RET) { |
| 9709 | DebugLoc Dl = MI.getDebugLoc(); |
| Kit Barton | 535e69d | 2015-03-25 19:36:23 +0000 | [diff] [blame] | 9710 | MachineRegisterInfo &RegInfo = F->getRegInfo(); |
| 9711 | unsigned CRReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass); |
| 9712 | BuildMI(*BB, MI, Dl, TII->get(PPC::TCHECK), CRReg); |
| 9713 | return BB; |
| Dale Johannesen | 340d264 | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 9714 | } else { |
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 9715 | llvm_unreachable("Unexpected instr type to insert"); |
| Evan Cheng | 32e376f | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 9716 | } |
| Chris Lattner | 9b577f1 | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 9717 | |
| Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 9718 | MI.eraseFromParent(); // The pseudo instruction is gone now. |
| Chris Lattner | 9b577f1 | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 9719 | return BB; |
| 9720 | } |
| 9721 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 9722 | //===----------------------------------------------------------------------===// |
| 9723 | // Target Optimization Hooks |
| 9724 | //===----------------------------------------------------------------------===// |
| 9725 | |
| Sanjay Patel | 0051efc | 2016-10-20 16:55:45 +0000 | [diff] [blame] | 9726 | static int getEstimateRefinementSteps(EVT VT, const PPCSubtarget &Subtarget) { |
| 9727 | // For the estimates, convergence is quadratic, so we essentially double the |
| 9728 | // number of digits correct after every iteration. For both FRE and FRSQRTE, |
| 9729 | // the minimum architected relative accuracy is 2^-5. When hasRecipPrec(), |
| 9730 | // this is 2^-14. IEEE float has 23 digits and double has 52 digits. |
| 9731 | int RefinementSteps = Subtarget.hasRecipPrec() ? 1 : 3; |
| Hal Finkel | cbf0892 | 2015-07-12 02:33:57 +0000 | [diff] [blame] | 9732 | if (VT.getScalarType() == MVT::f64) |
| Sanjay Patel | 0051efc | 2016-10-20 16:55:45 +0000 | [diff] [blame] | 9733 | RefinementSteps++; |
| 9734 | return RefinementSteps; |
| Hal Finkel | cbf0892 | 2015-07-12 02:33:57 +0000 | [diff] [blame] | 9735 | } |
| 9736 | |
| Evandro Menezes | 21f9ce1 | 2016-11-10 23:31:06 +0000 | [diff] [blame] | 9737 | SDValue PPCTargetLowering::getSqrtEstimate(SDValue Operand, SelectionDAG &DAG, |
| 9738 | int Enabled, int &RefinementSteps, |
| 9739 | bool &UseOneConstNR, |
| 9740 | bool Reciprocal) const { |
| Sanjay Patel | bdf1e38 | 2014-09-26 23:01:47 +0000 | [diff] [blame] | 9741 | EVT VT = Operand.getValueType(); |
| Sanjay Patel | 8fde95c | 2014-09-30 20:28:48 +0000 | [diff] [blame] | 9742 | if ((VT == MVT::f32 && Subtarget.hasFRSQRTES()) || |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 9743 | (VT == MVT::f64 && Subtarget.hasFRSQRTE()) || |
| Sanjay Patel | 8fde95c | 2014-09-30 20:28:48 +0000 | [diff] [blame] | 9744 | (VT == MVT::v4f32 && Subtarget.hasAltivec()) || |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 9745 | (VT == MVT::v2f64 && Subtarget.hasVSX()) || |
| 9746 | (VT == MVT::v4f32 && Subtarget.hasQPX()) || |
| 9747 | (VT == MVT::v4f64 && Subtarget.hasQPX())) { |
| Sanjay Patel | 0051efc | 2016-10-20 16:55:45 +0000 | [diff] [blame] | 9748 | if (RefinementSteps == ReciprocalEstimate::Unspecified) |
| 9749 | RefinementSteps = getEstimateRefinementSteps(VT, Subtarget); |
| Hal Finkel | cbf0892 | 2015-07-12 02:33:57 +0000 | [diff] [blame] | 9750 | |
| Sanjay Patel | 957efc23 | 2014-10-24 17:02:16 +0000 | [diff] [blame] | 9751 | UseOneConstNR = true; |
| Sanjay Patel | 0051efc | 2016-10-20 16:55:45 +0000 | [diff] [blame] | 9752 | return DAG.getNode(PPCISD::FRSQRTE, SDLoc(Operand), VT, Operand); |
| Hal Finkel | 2e10331 | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 9753 | } |
| Sanjay Patel | 8fde95c | 2014-09-30 20:28:48 +0000 | [diff] [blame] | 9754 | return SDValue(); |
| 9755 | } |
| 9756 | |
| Sanjay Patel | 0051efc | 2016-10-20 16:55:45 +0000 | [diff] [blame] | 9757 | SDValue PPCTargetLowering::getRecipEstimate(SDValue Operand, SelectionDAG &DAG, |
| 9758 | int Enabled, |
| 9759 | int &RefinementSteps) const { |
| Sanjay Patel | 8fde95c | 2014-09-30 20:28:48 +0000 | [diff] [blame] | 9760 | EVT VT = Operand.getValueType(); |
| 9761 | if ((VT == MVT::f32 && Subtarget.hasFRES()) || |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 9762 | (VT == MVT::f64 && Subtarget.hasFRE()) || |
| Sanjay Patel | 8fde95c | 2014-09-30 20:28:48 +0000 | [diff] [blame] | 9763 | (VT == MVT::v4f32 && Subtarget.hasAltivec()) || |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 9764 | (VT == MVT::v2f64 && Subtarget.hasVSX()) || |
| 9765 | (VT == MVT::v4f32 && Subtarget.hasQPX()) || |
| 9766 | (VT == MVT::v4f64 && Subtarget.hasQPX())) { |
| Sanjay Patel | 0051efc | 2016-10-20 16:55:45 +0000 | [diff] [blame] | 9767 | if (RefinementSteps == ReciprocalEstimate::Unspecified) |
| 9768 | RefinementSteps = getEstimateRefinementSteps(VT, Subtarget); |
| 9769 | return DAG.getNode(PPCISD::FRE, SDLoc(Operand), VT, Operand); |
| Sanjay Patel | 8fde95c | 2014-09-30 20:28:48 +0000 | [diff] [blame] | 9770 | } |
| 9771 | return SDValue(); |
| Hal Finkel | 2e10331 | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 9772 | } |
| 9773 | |
| Sanjay Patel | 1dd1559 | 2015-07-28 23:05:48 +0000 | [diff] [blame] | 9774 | unsigned PPCTargetLowering::combineRepeatedFPDivisors() const { |
| Hal Finkel | 360f213 | 2014-11-24 23:45:21 +0000 | [diff] [blame] | 9775 | // Note: This functionality is used only when unsafe-fp-math is enabled, and |
| 9776 | // on cores with reciprocal estimates (which are used when unsafe-fp-math is |
| 9777 | // enabled for division), this functionality is redundant with the default |
| 9778 | // combiner logic (once the division -> reciprocal/multiply transformation |
| 9779 | // has taken place). As a result, this matters more for older cores than for |
| 9780 | // newer ones. |
| 9781 | |
| 9782 | // Combine multiple FDIVs with the same divisor into multiple FMULs by the |
| 9783 | // reciprocal if there are two or more FDIVs (for embedded cores with only |
| 9784 | // one FP pipeline) for three or more FDIVs (for generic OOO cores). |
| 9785 | switch (Subtarget.getDarwinDirective()) { |
| 9786 | default: |
| Sanjay Patel | 1dd1559 | 2015-07-28 23:05:48 +0000 | [diff] [blame] | 9787 | return 3; |
| Hal Finkel | 360f213 | 2014-11-24 23:45:21 +0000 | [diff] [blame] | 9788 | case PPC::DIR_440: |
| 9789 | case PPC::DIR_A2: |
| 9790 | case PPC::DIR_E500mc: |
| 9791 | case PPC::DIR_E5500: |
| Sanjay Patel | 1dd1559 | 2015-07-28 23:05:48 +0000 | [diff] [blame] | 9792 | return 2; |
| Hal Finkel | 360f213 | 2014-11-24 23:45:21 +0000 | [diff] [blame] | 9793 | } |
| 9794 | } |
| 9795 | |
| Hal Finkel | e6702ca | 2015-09-03 22:37:44 +0000 | [diff] [blame] | 9796 | // isConsecutiveLSLoc needs to work even if all adds have not yet been |
| 9797 | // collapsed, and so we need to look through chains of them. |
| 9798 | static void getBaseWithConstantOffset(SDValue Loc, SDValue &Base, |
| 9799 | int64_t& Offset, SelectionDAG &DAG) { |
| 9800 | if (DAG.isBaseWithConstantOffset(Loc)) { |
| 9801 | Base = Loc.getOperand(0); |
| 9802 | Offset += cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue(); |
| 9803 | |
| 9804 | // The base might itself be a base plus an offset, and if so, accumulate |
| 9805 | // that as well. |
| 9806 | getBaseWithConstantOffset(Loc.getOperand(0), Base, Offset, DAG); |
| 9807 | } |
| 9808 | } |
| 9809 | |
| Hal Finkel | 3604bf7 | 2014-08-01 01:02:01 +0000 | [diff] [blame] | 9810 | static bool isConsecutiveLSLoc(SDValue Loc, EVT VT, LSBaseSDNode *Base, |
| Hal Finkel | 8ebfe6c | 2013-05-27 02:06:39 +0000 | [diff] [blame] | 9811 | unsigned Bytes, int Dist, |
| 9812 | SelectionDAG &DAG) { |
| Hal Finkel | 8ebfe6c | 2013-05-27 02:06:39 +0000 | [diff] [blame] | 9813 | if (VT.getSizeInBits() / 8 != Bytes) |
| 9814 | return false; |
| 9815 | |
| Hal Finkel | 8ebfe6c | 2013-05-27 02:06:39 +0000 | [diff] [blame] | 9816 | SDValue BaseLoc = Base->getBasePtr(); |
| 9817 | if (Loc.getOpcode() == ISD::FrameIndex) { |
| 9818 | if (BaseLoc.getOpcode() != ISD::FrameIndex) |
| 9819 | return false; |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 9820 | const MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); |
| Hal Finkel | 8ebfe6c | 2013-05-27 02:06:39 +0000 | [diff] [blame] | 9821 | int FI = cast<FrameIndexSDNode>(Loc)->getIndex(); |
| 9822 | int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 9823 | int FS = MFI.getObjectSize(FI); |
| 9824 | int BFS = MFI.getObjectSize(BFI); |
| Hal Finkel | 8ebfe6c | 2013-05-27 02:06:39 +0000 | [diff] [blame] | 9825 | if (FS != BFS || FS != (int)Bytes) return false; |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 9826 | return MFI.getObjectOffset(FI) == (MFI.getObjectOffset(BFI) + Dist*Bytes); |
| Hal Finkel | 8ebfe6c | 2013-05-27 02:06:39 +0000 | [diff] [blame] | 9827 | } |
| 9828 | |
| Hal Finkel | e6702ca | 2015-09-03 22:37:44 +0000 | [diff] [blame] | 9829 | SDValue Base1 = Loc, Base2 = BaseLoc; |
| 9830 | int64_t Offset1 = 0, Offset2 = 0; |
| 9831 | getBaseWithConstantOffset(Loc, Base1, Offset1, DAG); |
| 9832 | getBaseWithConstantOffset(BaseLoc, Base2, Offset2, DAG); |
| NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 9833 | if (Base1 == Base2 && Offset1 == (Offset2 + Dist * Bytes)) |
| 9834 | return true; |
| 9835 | |
| Hal Finkel | 8ebfe6c | 2013-05-27 02:06:39 +0000 | [diff] [blame] | 9836 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 9837 | const GlobalValue *GV1 = nullptr; |
| 9838 | const GlobalValue *GV2 = nullptr; |
| Hal Finkel | e6702ca | 2015-09-03 22:37:44 +0000 | [diff] [blame] | 9839 | Offset1 = 0; |
| 9840 | Offset2 = 0; |
| Hal Finkel | 8ebfe6c | 2013-05-27 02:06:39 +0000 | [diff] [blame] | 9841 | bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1); |
| 9842 | bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2); |
| 9843 | if (isGA1 && isGA2 && GV1 == GV2) |
| 9844 | return Offset1 == (Offset2 + Dist*Bytes); |
| 9845 | return false; |
| 9846 | } |
| 9847 | |
| Hal Finkel | 3604bf7 | 2014-08-01 01:02:01 +0000 | [diff] [blame] | 9848 | // Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does |
| 9849 | // not enforce equality of the chain operands. |
| 9850 | static bool isConsecutiveLS(SDNode *N, LSBaseSDNode *Base, |
| 9851 | unsigned Bytes, int Dist, |
| 9852 | SelectionDAG &DAG) { |
| 9853 | if (LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(N)) { |
| 9854 | EVT VT = LS->getMemoryVT(); |
| 9855 | SDValue Loc = LS->getBasePtr(); |
| 9856 | return isConsecutiveLSLoc(Loc, VT, Base, Bytes, Dist, DAG); |
| 9857 | } |
| 9858 | |
| 9859 | if (N->getOpcode() == ISD::INTRINSIC_W_CHAIN) { |
| 9860 | EVT VT; |
| 9861 | switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { |
| 9862 | default: return false; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 9863 | case Intrinsic::ppc_qpx_qvlfd: |
| 9864 | case Intrinsic::ppc_qpx_qvlfda: |
| 9865 | VT = MVT::v4f64; |
| 9866 | break; |
| 9867 | case Intrinsic::ppc_qpx_qvlfs: |
| 9868 | case Intrinsic::ppc_qpx_qvlfsa: |
| 9869 | VT = MVT::v4f32; |
| 9870 | break; |
| 9871 | case Intrinsic::ppc_qpx_qvlfcd: |
| 9872 | case Intrinsic::ppc_qpx_qvlfcda: |
| 9873 | VT = MVT::v2f64; |
| 9874 | break; |
| 9875 | case Intrinsic::ppc_qpx_qvlfcs: |
| 9876 | case Intrinsic::ppc_qpx_qvlfcsa: |
| 9877 | VT = MVT::v2f32; |
| 9878 | break; |
| 9879 | case Intrinsic::ppc_qpx_qvlfiwa: |
| 9880 | case Intrinsic::ppc_qpx_qvlfiwz: |
| Hal Finkel | 3604bf7 | 2014-08-01 01:02:01 +0000 | [diff] [blame] | 9881 | case Intrinsic::ppc_altivec_lvx: |
| 9882 | case Intrinsic::ppc_altivec_lvxl: |
| Bill Schmidt | 7295478 | 2014-11-12 04:19:40 +0000 | [diff] [blame] | 9883 | case Intrinsic::ppc_vsx_lxvw4x: |
| Tony Jiang | 5f850cd | 2016-11-15 14:25:56 +0000 | [diff] [blame] | 9884 | case Intrinsic::ppc_vsx_lxvw4x_be: |
| Hal Finkel | 3604bf7 | 2014-08-01 01:02:01 +0000 | [diff] [blame] | 9885 | VT = MVT::v4i32; |
| 9886 | break; |
| Bill Schmidt | 7295478 | 2014-11-12 04:19:40 +0000 | [diff] [blame] | 9887 | case Intrinsic::ppc_vsx_lxvd2x: |
| Tony Jiang | 5f850cd | 2016-11-15 14:25:56 +0000 | [diff] [blame] | 9888 | case Intrinsic::ppc_vsx_lxvd2x_be: |
| Bill Schmidt | 7295478 | 2014-11-12 04:19:40 +0000 | [diff] [blame] | 9889 | VT = MVT::v2f64; |
| 9890 | break; |
| Hal Finkel | 3604bf7 | 2014-08-01 01:02:01 +0000 | [diff] [blame] | 9891 | case Intrinsic::ppc_altivec_lvebx: |
| 9892 | VT = MVT::i8; |
| 9893 | break; |
| 9894 | case Intrinsic::ppc_altivec_lvehx: |
| 9895 | VT = MVT::i16; |
| 9896 | break; |
| 9897 | case Intrinsic::ppc_altivec_lvewx: |
| 9898 | VT = MVT::i32; |
| 9899 | break; |
| 9900 | } |
| 9901 | |
| 9902 | return isConsecutiveLSLoc(N->getOperand(2), VT, Base, Bytes, Dist, DAG); |
| 9903 | } |
| 9904 | |
| 9905 | if (N->getOpcode() == ISD::INTRINSIC_VOID) { |
| 9906 | EVT VT; |
| 9907 | switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { |
| 9908 | default: return false; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 9909 | case Intrinsic::ppc_qpx_qvstfd: |
| 9910 | case Intrinsic::ppc_qpx_qvstfda: |
| 9911 | VT = MVT::v4f64; |
| 9912 | break; |
| 9913 | case Intrinsic::ppc_qpx_qvstfs: |
| 9914 | case Intrinsic::ppc_qpx_qvstfsa: |
| 9915 | VT = MVT::v4f32; |
| 9916 | break; |
| 9917 | case Intrinsic::ppc_qpx_qvstfcd: |
| 9918 | case Intrinsic::ppc_qpx_qvstfcda: |
| 9919 | VT = MVT::v2f64; |
| 9920 | break; |
| 9921 | case Intrinsic::ppc_qpx_qvstfcs: |
| 9922 | case Intrinsic::ppc_qpx_qvstfcsa: |
| 9923 | VT = MVT::v2f32; |
| 9924 | break; |
| 9925 | case Intrinsic::ppc_qpx_qvstfiw: |
| 9926 | case Intrinsic::ppc_qpx_qvstfiwa: |
| Hal Finkel | 3604bf7 | 2014-08-01 01:02:01 +0000 | [diff] [blame] | 9927 | case Intrinsic::ppc_altivec_stvx: |
| 9928 | case Intrinsic::ppc_altivec_stvxl: |
| Bill Schmidt | 7295478 | 2014-11-12 04:19:40 +0000 | [diff] [blame] | 9929 | case Intrinsic::ppc_vsx_stxvw4x: |
| Hal Finkel | 3604bf7 | 2014-08-01 01:02:01 +0000 | [diff] [blame] | 9930 | VT = MVT::v4i32; |
| 9931 | break; |
| Bill Schmidt | 7295478 | 2014-11-12 04:19:40 +0000 | [diff] [blame] | 9932 | case Intrinsic::ppc_vsx_stxvd2x: |
| 9933 | VT = MVT::v2f64; |
| 9934 | break; |
| Tony Jiang | 5f850cd | 2016-11-15 14:25:56 +0000 | [diff] [blame] | 9935 | case Intrinsic::ppc_vsx_stxvw4x_be: |
| 9936 | VT = MVT::v4i32; |
| 9937 | break; |
| 9938 | case Intrinsic::ppc_vsx_stxvd2x_be: |
| 9939 | VT = MVT::v2f64; |
| 9940 | break; |
| Hal Finkel | 3604bf7 | 2014-08-01 01:02:01 +0000 | [diff] [blame] | 9941 | case Intrinsic::ppc_altivec_stvebx: |
| 9942 | VT = MVT::i8; |
| 9943 | break; |
| 9944 | case Intrinsic::ppc_altivec_stvehx: |
| 9945 | VT = MVT::i16; |
| 9946 | break; |
| 9947 | case Intrinsic::ppc_altivec_stvewx: |
| 9948 | VT = MVT::i32; |
| 9949 | break; |
| 9950 | } |
| 9951 | |
| 9952 | return isConsecutiveLSLoc(N->getOperand(3), VT, Base, Bytes, Dist, DAG); |
| 9953 | } |
| 9954 | |
| 9955 | return false; |
| 9956 | } |
| 9957 | |
| Hal Finkel | 7d8a691 | 2013-05-26 18:08:30 +0000 | [diff] [blame] | 9958 | // Return true is there is a nearyby consecutive load to the one provided |
| 9959 | // (regardless of alignment). We search up and down the chain, looking though |
| Matt Arsenault | 57e74d2 | 2014-07-29 00:02:40 +0000 | [diff] [blame] | 9960 | // token factors and other loads (but nothing else). As a result, a true result |
| 9961 | // indicates that it is safe to create a new consecutive load adjacent to the |
| 9962 | // load provided. |
| Hal Finkel | 7d8a691 | 2013-05-26 18:08:30 +0000 | [diff] [blame] | 9963 | static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) { |
| 9964 | SDValue Chain = LD->getChain(); |
| 9965 | EVT VT = LD->getMemoryVT(); |
| 9966 | |
| 9967 | SmallSet<SDNode *, 16> LoadRoots; |
| 9968 | SmallVector<SDNode *, 8> Queue(1, Chain.getNode()); |
| 9969 | SmallSet<SDNode *, 16> Visited; |
| 9970 | |
| 9971 | // First, search up the chain, branching to follow all token-factor operands. |
| 9972 | // If we find a consecutive load, then we're done, otherwise, record all |
| 9973 | // nodes just above the top-level loads and token factors. |
| 9974 | while (!Queue.empty()) { |
| 9975 | SDNode *ChainNext = Queue.pop_back_val(); |
| David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 9976 | if (!Visited.insert(ChainNext).second) |
| Hal Finkel | 7d8a691 | 2013-05-26 18:08:30 +0000 | [diff] [blame] | 9977 | continue; |
| 9978 | |
| Hal Finkel | 3604bf7 | 2014-08-01 01:02:01 +0000 | [diff] [blame] | 9979 | if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(ChainNext)) { |
| Hal Finkel | 8ebfe6c | 2013-05-27 02:06:39 +0000 | [diff] [blame] | 9980 | if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG)) |
| Hal Finkel | 7d8a691 | 2013-05-26 18:08:30 +0000 | [diff] [blame] | 9981 | return true; |
| 9982 | |
| 9983 | if (!Visited.count(ChainLD->getChain().getNode())) |
| 9984 | Queue.push_back(ChainLD->getChain().getNode()); |
| 9985 | } else if (ChainNext->getOpcode() == ISD::TokenFactor) { |
| Craig Topper | 66e588b | 2014-06-29 00:40:57 +0000 | [diff] [blame] | 9986 | for (const SDUse &O : ChainNext->ops()) |
| 9987 | if (!Visited.count(O.getNode())) |
| 9988 | Queue.push_back(O.getNode()); |
| Hal Finkel | 7d8a691 | 2013-05-26 18:08:30 +0000 | [diff] [blame] | 9989 | } else |
| 9990 | LoadRoots.insert(ChainNext); |
| 9991 | } |
| 9992 | |
| 9993 | // Second, search down the chain, starting from the top-level nodes recorded |
| 9994 | // in the first phase. These top-level nodes are the nodes just above all |
| 9995 | // loads and token factors. Starting with their uses, recursively look though |
| 9996 | // all loads (just the chain uses) and token factors to find a consecutive |
| 9997 | // load. |
| 9998 | Visited.clear(); |
| 9999 | Queue.clear(); |
| 10000 | |
| 10001 | for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(), |
| 10002 | IE = LoadRoots.end(); I != IE; ++I) { |
| 10003 | Queue.push_back(*I); |
| NAKAMURA Takumi | a9cb538 | 2015-09-22 11:14:39 +0000 | [diff] [blame] | 10004 | |
| Hal Finkel | 7d8a691 | 2013-05-26 18:08:30 +0000 | [diff] [blame] | 10005 | while (!Queue.empty()) { |
| 10006 | SDNode *LoadRoot = Queue.pop_back_val(); |
| David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 10007 | if (!Visited.insert(LoadRoot).second) |
| Hal Finkel | 7d8a691 | 2013-05-26 18:08:30 +0000 | [diff] [blame] | 10008 | continue; |
| 10009 | |
| Hal Finkel | 3604bf7 | 2014-08-01 01:02:01 +0000 | [diff] [blame] | 10010 | if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(LoadRoot)) |
| Hal Finkel | 8ebfe6c | 2013-05-27 02:06:39 +0000 | [diff] [blame] | 10011 | if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG)) |
| Hal Finkel | 7d8a691 | 2013-05-26 18:08:30 +0000 | [diff] [blame] | 10012 | return true; |
| 10013 | |
| 10014 | for (SDNode::use_iterator UI = LoadRoot->use_begin(), |
| 10015 | UE = LoadRoot->use_end(); UI != UE; ++UI) |
| Hal Finkel | 3604bf7 | 2014-08-01 01:02:01 +0000 | [diff] [blame] | 10016 | if (((isa<MemSDNode>(*UI) && |
| 10017 | cast<MemSDNode>(*UI)->getChain().getNode() == LoadRoot) || |
| Hal Finkel | 7d8a691 | 2013-05-26 18:08:30 +0000 | [diff] [blame] | 10018 | UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI)) |
| 10019 | Queue.push_back(*UI); |
| 10020 | } |
| 10021 | } |
| 10022 | |
| 10023 | return false; |
| 10024 | } |
| 10025 | |
| Ehsan Amiri | 8581868 | 2016-11-18 10:41:44 +0000 | [diff] [blame] | 10026 | |
| 10027 | /// This function is called when we have proved that a SETCC node can be replaced |
| 10028 | /// by subtraction (and other supporting instructions) so that the result of |
| 10029 | /// comparison is kept in a GPR instead of CR. This function is purely for |
| 10030 | /// codegen purposes and has some flags to guide the codegen process. |
| 10031 | static SDValue generateEquivalentSub(SDNode *N, int Size, bool Complement, |
| 10032 | bool Swap, SDLoc &DL, SelectionDAG &DAG) { |
| 10033 | |
| 10034 | assert(N->getOpcode() == ISD::SETCC && "ISD::SETCC Expected."); |
| 10035 | |
| 10036 | // Zero extend the operands to the largest legal integer. Originally, they |
| 10037 | // must be of a strictly smaller size. |
| 10038 | auto Op0 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, N->getOperand(0), |
| 10039 | DAG.getConstant(Size, DL, MVT::i32)); |
| 10040 | auto Op1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, N->getOperand(1), |
| 10041 | DAG.getConstant(Size, DL, MVT::i32)); |
| 10042 | |
| 10043 | // Swap if needed. Depends on the condition code. |
| 10044 | if (Swap) |
| 10045 | std::swap(Op0, Op1); |
| 10046 | |
| 10047 | // Subtract extended integers. |
| 10048 | auto SubNode = DAG.getNode(ISD::SUB, DL, MVT::i64, Op0, Op1); |
| 10049 | |
| 10050 | // Move the sign bit to the least significant position and zero out the rest. |
| 10051 | // Now the least significant bit carries the result of original comparison. |
| 10052 | auto Shifted = DAG.getNode(ISD::SRL, DL, MVT::i64, SubNode, |
| 10053 | DAG.getConstant(Size - 1, DL, MVT::i32)); |
| 10054 | auto Final = Shifted; |
| 10055 | |
| 10056 | // Complement the result if needed. Based on the condition code. |
| 10057 | if (Complement) |
| 10058 | Final = DAG.getNode(ISD::XOR, DL, MVT::i64, Shifted, |
| 10059 | DAG.getConstant(1, DL, MVT::i64)); |
| 10060 | |
| 10061 | return DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Final); |
| 10062 | } |
| 10063 | |
| 10064 | SDValue PPCTargetLowering::ConvertSETCCToSubtract(SDNode *N, |
| 10065 | DAGCombinerInfo &DCI) const { |
| 10066 | |
| 10067 | assert(N->getOpcode() == ISD::SETCC && "ISD::SETCC Expected."); |
| 10068 | |
| 10069 | SelectionDAG &DAG = DCI.DAG; |
| 10070 | SDLoc DL(N); |
| 10071 | |
| 10072 | // Size of integers being compared has a critical role in the following |
| 10073 | // analysis, so we prefer to do this when all types are legal. |
| 10074 | if (!DCI.isAfterLegalizeVectorOps()) |
| 10075 | return SDValue(); |
| 10076 | |
| 10077 | // If all users of SETCC extend its value to a legal integer type |
| 10078 | // then we replace SETCC with a subtraction |
| 10079 | for (SDNode::use_iterator UI = N->use_begin(), |
| 10080 | UE = N->use_end(); UI != UE; ++UI) { |
| 10081 | if (UI->getOpcode() != ISD::ZERO_EXTEND) |
| 10082 | return SDValue(); |
| 10083 | } |
| 10084 | |
| 10085 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); |
| 10086 | auto OpSize = N->getOperand(0).getValueSizeInBits(); |
| 10087 | |
| 10088 | unsigned Size = DAG.getDataLayout().getLargestLegalIntTypeSizeInBits(); |
| 10089 | |
| 10090 | if (OpSize < Size) { |
| 10091 | switch (CC) { |
| 10092 | default: break; |
| 10093 | case ISD::SETULT: |
| 10094 | return generateEquivalentSub(N, Size, false, false, DL, DAG); |
| 10095 | case ISD::SETULE: |
| 10096 | return generateEquivalentSub(N, Size, true, true, DL, DAG); |
| 10097 | case ISD::SETUGT: |
| 10098 | return generateEquivalentSub(N, Size, false, true, DL, DAG); |
| 10099 | case ISD::SETUGE: |
| 10100 | return generateEquivalentSub(N, Size, true, false, DL, DAG); |
| 10101 | } |
| 10102 | } |
| 10103 | |
| 10104 | return SDValue(); |
| 10105 | } |
| 10106 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10107 | SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N, |
| 10108 | DAGCombinerInfo &DCI) const { |
| 10109 | SelectionDAG &DAG = DCI.DAG; |
| 10110 | SDLoc dl(N); |
| 10111 | |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 10112 | assert(Subtarget.useCRBits() && "Expecting to be tracking CR bits"); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10113 | // If we're tracking CR bits, we need to be careful that we don't have: |
| 10114 | // trunc(binary-ops(zext(x), zext(y))) |
| 10115 | // or |
| 10116 | // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...) |
| 10117 | // such that we're unnecessarily moving things into GPRs when it would be |
| 10118 | // better to keep them in CR bits. |
| 10119 | |
| 10120 | // Note that trunc here can be an actual i1 trunc, or can be the effective |
| 10121 | // truncation that comes from a setcc or select_cc. |
| 10122 | if (N->getOpcode() == ISD::TRUNCATE && |
| 10123 | N->getValueType(0) != MVT::i1) |
| 10124 | return SDValue(); |
| 10125 | |
| 10126 | if (N->getOperand(0).getValueType() != MVT::i32 && |
| 10127 | N->getOperand(0).getValueType() != MVT::i64) |
| 10128 | return SDValue(); |
| 10129 | |
| 10130 | if (N->getOpcode() == ISD::SETCC || |
| 10131 | N->getOpcode() == ISD::SELECT_CC) { |
| 10132 | // If we're looking at a comparison, then we need to make sure that the |
| 10133 | // high bits (all except for the first) don't matter the result. |
| 10134 | ISD::CondCode CC = |
| 10135 | cast<CondCodeSDNode>(N->getOperand( |
| 10136 | N->getOpcode() == ISD::SETCC ? 2 : 4))->get(); |
| 10137 | unsigned OpBits = N->getOperand(0).getValueSizeInBits(); |
| 10138 | |
| 10139 | if (ISD::isSignedIntSetCC(CC)) { |
| 10140 | if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits || |
| 10141 | DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits) |
| 10142 | return SDValue(); |
| 10143 | } else if (ISD::isUnsignedIntSetCC(CC)) { |
| 10144 | if (!DAG.MaskedValueIsZero(N->getOperand(0), |
| 10145 | APInt::getHighBitsSet(OpBits, OpBits-1)) || |
| 10146 | !DAG.MaskedValueIsZero(N->getOperand(1), |
| 10147 | APInt::getHighBitsSet(OpBits, OpBits-1))) |
| Ehsan Amiri | 8581868 | 2016-11-18 10:41:44 +0000 | [diff] [blame] | 10148 | return (N->getOpcode() == ISD::SETCC ? ConvertSETCCToSubtract(N, DCI) |
| 10149 | : SDValue()); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10150 | } else { |
| 10151 | // This is neither a signed nor an unsigned comparison, just make sure |
| 10152 | // that the high bits are equal. |
| 10153 | APInt Op1Zero, Op1One; |
| 10154 | APInt Op2Zero, Op2One; |
| Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 10155 | DAG.computeKnownBits(N->getOperand(0), Op1Zero, Op1One); |
| 10156 | DAG.computeKnownBits(N->getOperand(1), Op2Zero, Op2One); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10157 | |
| 10158 | // We don't really care about what is known about the first bit (if |
| 10159 | // anything), so clear it in all masks prior to comparing them. |
| 10160 | Op1Zero.clearBit(0); Op1One.clearBit(0); |
| 10161 | Op2Zero.clearBit(0); Op2One.clearBit(0); |
| 10162 | |
| 10163 | if (Op1Zero != Op2Zero || Op1One != Op2One) |
| 10164 | return SDValue(); |
| 10165 | } |
| 10166 | } |
| 10167 | |
| 10168 | // We now know that the higher-order bits are irrelevant, we just need to |
| 10169 | // make sure that all of the intermediate operations are bit operations, and |
| 10170 | // all inputs are extensions. |
| 10171 | if (N->getOperand(0).getOpcode() != ISD::AND && |
| 10172 | N->getOperand(0).getOpcode() != ISD::OR && |
| 10173 | N->getOperand(0).getOpcode() != ISD::XOR && |
| 10174 | N->getOperand(0).getOpcode() != ISD::SELECT && |
| 10175 | N->getOperand(0).getOpcode() != ISD::SELECT_CC && |
| 10176 | N->getOperand(0).getOpcode() != ISD::TRUNCATE && |
| 10177 | N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND && |
| 10178 | N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND && |
| 10179 | N->getOperand(0).getOpcode() != ISD::ANY_EXTEND) |
| 10180 | return SDValue(); |
| 10181 | |
| 10182 | if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) && |
| 10183 | N->getOperand(1).getOpcode() != ISD::AND && |
| 10184 | N->getOperand(1).getOpcode() != ISD::OR && |
| 10185 | N->getOperand(1).getOpcode() != ISD::XOR && |
| 10186 | N->getOperand(1).getOpcode() != ISD::SELECT && |
| 10187 | N->getOperand(1).getOpcode() != ISD::SELECT_CC && |
| 10188 | N->getOperand(1).getOpcode() != ISD::TRUNCATE && |
| 10189 | N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND && |
| 10190 | N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND && |
| 10191 | N->getOperand(1).getOpcode() != ISD::ANY_EXTEND) |
| 10192 | return SDValue(); |
| 10193 | |
| 10194 | SmallVector<SDValue, 4> Inputs; |
| 10195 | SmallVector<SDValue, 8> BinOps, PromOps; |
| 10196 | SmallPtrSet<SDNode *, 16> Visited; |
| 10197 | |
| 10198 | for (unsigned i = 0; i < 2; ++i) { |
| 10199 | if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND || |
| 10200 | N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND || |
| 10201 | N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) && |
| 10202 | N->getOperand(i).getOperand(0).getValueType() == MVT::i1) || |
| 10203 | isa<ConstantSDNode>(N->getOperand(i))) |
| 10204 | Inputs.push_back(N->getOperand(i)); |
| 10205 | else |
| 10206 | BinOps.push_back(N->getOperand(i)); |
| 10207 | |
| 10208 | if (N->getOpcode() == ISD::TRUNCATE) |
| 10209 | break; |
| 10210 | } |
| 10211 | |
| 10212 | // Visit all inputs, collect all binary operations (and, or, xor and |
| NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 10213 | // select) that are all fed by extensions. |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10214 | while (!BinOps.empty()) { |
| 10215 | SDValue BinOp = BinOps.back(); |
| 10216 | BinOps.pop_back(); |
| 10217 | |
| David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 10218 | if (!Visited.insert(BinOp.getNode()).second) |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10219 | continue; |
| 10220 | |
| 10221 | PromOps.push_back(BinOp); |
| 10222 | |
| 10223 | for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) { |
| 10224 | // The condition of the select is not promoted. |
| 10225 | if (BinOp.getOpcode() == ISD::SELECT && i == 0) |
| 10226 | continue; |
| 10227 | if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3) |
| 10228 | continue; |
| 10229 | |
| 10230 | if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND || |
| 10231 | BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND || |
| 10232 | BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) && |
| 10233 | BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) || |
| 10234 | isa<ConstantSDNode>(BinOp.getOperand(i))) { |
| NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 10235 | Inputs.push_back(BinOp.getOperand(i)); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10236 | } else if (BinOp.getOperand(i).getOpcode() == ISD::AND || |
| 10237 | BinOp.getOperand(i).getOpcode() == ISD::OR || |
| 10238 | BinOp.getOperand(i).getOpcode() == ISD::XOR || |
| 10239 | BinOp.getOperand(i).getOpcode() == ISD::SELECT || |
| 10240 | BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC || |
| 10241 | BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE || |
| 10242 | BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND || |
| 10243 | BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND || |
| 10244 | BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) { |
| 10245 | BinOps.push_back(BinOp.getOperand(i)); |
| 10246 | } else { |
| 10247 | // We have an input that is not an extension or another binary |
| 10248 | // operation; we'll abort this transformation. |
| 10249 | return SDValue(); |
| 10250 | } |
| 10251 | } |
| 10252 | } |
| 10253 | |
| 10254 | // Make sure that this is a self-contained cluster of operations (which |
| 10255 | // is not quite the same thing as saying that everything has only one |
| 10256 | // use). |
| 10257 | for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) { |
| 10258 | if (isa<ConstantSDNode>(Inputs[i])) |
| 10259 | continue; |
| 10260 | |
| 10261 | for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(), |
| 10262 | UE = Inputs[i].getNode()->use_end(); |
| 10263 | UI != UE; ++UI) { |
| 10264 | SDNode *User = *UI; |
| 10265 | if (User != N && !Visited.count(User)) |
| 10266 | return SDValue(); |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 10267 | |
| 10268 | // Make sure that we're not going to promote the non-output-value |
| 10269 | // operand(s) or SELECT or SELECT_CC. |
| 10270 | // FIXME: Although we could sometimes handle this, and it does occur in |
| 10271 | // practice that one of the condition inputs to the select is also one of |
| 10272 | // the outputs, we currently can't deal with this. |
| 10273 | if (User->getOpcode() == ISD::SELECT) { |
| 10274 | if (User->getOperand(0) == Inputs[i]) |
| 10275 | return SDValue(); |
| 10276 | } else if (User->getOpcode() == ISD::SELECT_CC) { |
| 10277 | if (User->getOperand(0) == Inputs[i] || |
| 10278 | User->getOperand(1) == Inputs[i]) |
| 10279 | return SDValue(); |
| 10280 | } |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10281 | } |
| 10282 | } |
| 10283 | |
| 10284 | for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) { |
| 10285 | for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(), |
| 10286 | UE = PromOps[i].getNode()->use_end(); |
| 10287 | UI != UE; ++UI) { |
| 10288 | SDNode *User = *UI; |
| 10289 | if (User != N && !Visited.count(User)) |
| 10290 | return SDValue(); |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 10291 | |
| 10292 | // Make sure that we're not going to promote the non-output-value |
| 10293 | // operand(s) or SELECT or SELECT_CC. |
| 10294 | // FIXME: Although we could sometimes handle this, and it does occur in |
| 10295 | // practice that one of the condition inputs to the select is also one of |
| 10296 | // the outputs, we currently can't deal with this. |
| 10297 | if (User->getOpcode() == ISD::SELECT) { |
| 10298 | if (User->getOperand(0) == PromOps[i]) |
| 10299 | return SDValue(); |
| 10300 | } else if (User->getOpcode() == ISD::SELECT_CC) { |
| 10301 | if (User->getOperand(0) == PromOps[i] || |
| 10302 | User->getOperand(1) == PromOps[i]) |
| 10303 | return SDValue(); |
| 10304 | } |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10305 | } |
| 10306 | } |
| 10307 | |
| 10308 | // Replace all inputs with the extension operand. |
| 10309 | for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) { |
| 10310 | // Constants may have users outside the cluster of to-be-promoted nodes, |
| 10311 | // and so we need to replace those as we do the promotions. |
| 10312 | if (isa<ConstantSDNode>(Inputs[i])) |
| 10313 | continue; |
| 10314 | else |
| NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 10315 | DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0)); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10316 | } |
| 10317 | |
| Hal Finkel | 1fb10e8 | 2016-05-12 04:00:56 +0000 | [diff] [blame] | 10318 | std::list<HandleSDNode> PromOpHandles; |
| 10319 | for (auto &PromOp : PromOps) |
| NAKAMURA Takumi | ae7c97d | 2016-06-20 00:49:20 +0000 | [diff] [blame] | 10320 | PromOpHandles.emplace_back(PromOp); |
| Hal Finkel | 1fb10e8 | 2016-05-12 04:00:56 +0000 | [diff] [blame] | 10321 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10322 | // Replace all operations (these are all the same, but have a different |
| 10323 | // (i1) return type). DAG.getNode will validate that the types of |
| 10324 | // a binary operator match, so go through the list in reverse so that |
| 10325 | // we've likely promoted both operands first. Any intermediate truncations or |
| 10326 | // extensions disappear. |
| Hal Finkel | 1fb10e8 | 2016-05-12 04:00:56 +0000 | [diff] [blame] | 10327 | while (!PromOpHandles.empty()) { |
| 10328 | SDValue PromOp = PromOpHandles.back().getValue(); |
| 10329 | PromOpHandles.pop_back(); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10330 | |
| 10331 | if (PromOp.getOpcode() == ISD::TRUNCATE || |
| 10332 | PromOp.getOpcode() == ISD::SIGN_EXTEND || |
| 10333 | PromOp.getOpcode() == ISD::ZERO_EXTEND || |
| 10334 | PromOp.getOpcode() == ISD::ANY_EXTEND) { |
| 10335 | if (!isa<ConstantSDNode>(PromOp.getOperand(0)) && |
| 10336 | PromOp.getOperand(0).getValueType() != MVT::i1) { |
| 10337 | // The operand is not yet ready (see comment below). |
| Hal Finkel | 1fb10e8 | 2016-05-12 04:00:56 +0000 | [diff] [blame] | 10338 | PromOpHandles.emplace_front(PromOp); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10339 | continue; |
| 10340 | } |
| 10341 | |
| 10342 | SDValue RepValue = PromOp.getOperand(0); |
| 10343 | if (isa<ConstantSDNode>(RepValue)) |
| 10344 | RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue); |
| 10345 | |
| 10346 | DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue); |
| 10347 | continue; |
| 10348 | } |
| 10349 | |
| 10350 | unsigned C; |
| 10351 | switch (PromOp.getOpcode()) { |
| 10352 | default: C = 0; break; |
| 10353 | case ISD::SELECT: C = 1; break; |
| 10354 | case ISD::SELECT_CC: C = 2; break; |
| 10355 | } |
| 10356 | |
| 10357 | if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) && |
| 10358 | PromOp.getOperand(C).getValueType() != MVT::i1) || |
| 10359 | (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) && |
| 10360 | PromOp.getOperand(C+1).getValueType() != MVT::i1)) { |
| 10361 | // The to-be-promoted operands of this node have not yet been |
| 10362 | // promoted (this should be rare because we're going through the |
| 10363 | // list backward, but if one of the operands has several users in |
| 10364 | // this cluster of to-be-promoted nodes, it is possible). |
| Hal Finkel | 1fb10e8 | 2016-05-12 04:00:56 +0000 | [diff] [blame] | 10365 | PromOpHandles.emplace_front(PromOp); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10366 | continue; |
| 10367 | } |
| 10368 | |
| 10369 | SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(), |
| 10370 | PromOp.getNode()->op_end()); |
| 10371 | |
| 10372 | // If there are any constant inputs, make sure they're replaced now. |
| 10373 | for (unsigned i = 0; i < 2; ++i) |
| 10374 | if (isa<ConstantSDNode>(Ops[C+i])) |
| 10375 | Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]); |
| 10376 | |
| 10377 | DAG.ReplaceAllUsesOfValueWith(PromOp, |
| Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 10378 | DAG.getNode(PromOp.getOpcode(), dl, MVT::i1, Ops)); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10379 | } |
| 10380 | |
| 10381 | // Now we're left with the initial truncation itself. |
| 10382 | if (N->getOpcode() == ISD::TRUNCATE) |
| 10383 | return N->getOperand(0); |
| 10384 | |
| 10385 | // Otherwise, this is a comparison. The operands to be compared have just |
| 10386 | // changed type (to i1), but everything else is the same. |
| 10387 | return SDValue(N, 0); |
| 10388 | } |
| 10389 | |
| 10390 | SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N, |
| 10391 | DAGCombinerInfo &DCI) const { |
| 10392 | SelectionDAG &DAG = DCI.DAG; |
| 10393 | SDLoc dl(N); |
| 10394 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10395 | // If we're tracking CR bits, we need to be careful that we don't have: |
| 10396 | // zext(binary-ops(trunc(x), trunc(y))) |
| 10397 | // or |
| 10398 | // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...) |
| 10399 | // such that we're unnecessarily moving things into CR bits that can more |
| 10400 | // efficiently stay in GPRs. Note that if we're not certain that the high |
| 10401 | // bits are set as required by the final extension, we still may need to do |
| 10402 | // some masking to get the proper behavior. |
| 10403 | |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 10404 | // This same functionality is important on PPC64 when dealing with |
| 10405 | // 32-to-64-bit extensions; these occur often when 32-bit values are used as |
| 10406 | // the return values of functions. Because it is so similar, it is handled |
| 10407 | // here as well. |
| 10408 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10409 | if (N->getValueType(0) != MVT::i32 && |
| 10410 | N->getValueType(0) != MVT::i64) |
| 10411 | return SDValue(); |
| 10412 | |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 10413 | if (!((N->getOperand(0).getValueType() == MVT::i1 && Subtarget.useCRBits()) || |
| 10414 | (N->getOperand(0).getValueType() == MVT::i32 && Subtarget.isPPC64()))) |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10415 | return SDValue(); |
| 10416 | |
| 10417 | if (N->getOperand(0).getOpcode() != ISD::AND && |
| 10418 | N->getOperand(0).getOpcode() != ISD::OR && |
| 10419 | N->getOperand(0).getOpcode() != ISD::XOR && |
| 10420 | N->getOperand(0).getOpcode() != ISD::SELECT && |
| 10421 | N->getOperand(0).getOpcode() != ISD::SELECT_CC) |
| 10422 | return SDValue(); |
| 10423 | |
| 10424 | SmallVector<SDValue, 4> Inputs; |
| 10425 | SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps; |
| 10426 | SmallPtrSet<SDNode *, 16> Visited; |
| 10427 | |
| 10428 | // Visit all inputs, collect all binary operations (and, or, xor and |
| NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 10429 | // select) that are all fed by truncations. |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10430 | while (!BinOps.empty()) { |
| 10431 | SDValue BinOp = BinOps.back(); |
| 10432 | BinOps.pop_back(); |
| 10433 | |
| David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 10434 | if (!Visited.insert(BinOp.getNode()).second) |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10435 | continue; |
| 10436 | |
| 10437 | PromOps.push_back(BinOp); |
| 10438 | |
| 10439 | for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) { |
| 10440 | // The condition of the select is not promoted. |
| 10441 | if (BinOp.getOpcode() == ISD::SELECT && i == 0) |
| 10442 | continue; |
| 10443 | if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3) |
| 10444 | continue; |
| 10445 | |
| 10446 | if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE || |
| 10447 | isa<ConstantSDNode>(BinOp.getOperand(i))) { |
| NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 10448 | Inputs.push_back(BinOp.getOperand(i)); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10449 | } else if (BinOp.getOperand(i).getOpcode() == ISD::AND || |
| 10450 | BinOp.getOperand(i).getOpcode() == ISD::OR || |
| 10451 | BinOp.getOperand(i).getOpcode() == ISD::XOR || |
| 10452 | BinOp.getOperand(i).getOpcode() == ISD::SELECT || |
| 10453 | BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) { |
| 10454 | BinOps.push_back(BinOp.getOperand(i)); |
| 10455 | } else { |
| 10456 | // We have an input that is not a truncation or another binary |
| 10457 | // operation; we'll abort this transformation. |
| 10458 | return SDValue(); |
| 10459 | } |
| 10460 | } |
| 10461 | } |
| 10462 | |
| Hal Finkel | 4104a1a | 2014-12-14 05:53:19 +0000 | [diff] [blame] | 10463 | // The operands of a select that must be truncated when the select is |
| 10464 | // promoted because the operand is actually part of the to-be-promoted set. |
| 10465 | DenseMap<SDNode *, EVT> SelectTruncOp[2]; |
| 10466 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10467 | // Make sure that this is a self-contained cluster of operations (which |
| 10468 | // is not quite the same thing as saying that everything has only one |
| 10469 | // use). |
| 10470 | for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) { |
| 10471 | if (isa<ConstantSDNode>(Inputs[i])) |
| 10472 | continue; |
| 10473 | |
| 10474 | for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(), |
| 10475 | UE = Inputs[i].getNode()->use_end(); |
| 10476 | UI != UE; ++UI) { |
| 10477 | SDNode *User = *UI; |
| 10478 | if (User != N && !Visited.count(User)) |
| 10479 | return SDValue(); |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 10480 | |
| Hal Finkel | 4104a1a | 2014-12-14 05:53:19 +0000 | [diff] [blame] | 10481 | // If we're going to promote the non-output-value operand(s) or SELECT or |
| 10482 | // SELECT_CC, record them for truncation. |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 10483 | if (User->getOpcode() == ISD::SELECT) { |
| 10484 | if (User->getOperand(0) == Inputs[i]) |
| Hal Finkel | 4104a1a | 2014-12-14 05:53:19 +0000 | [diff] [blame] | 10485 | SelectTruncOp[0].insert(std::make_pair(User, |
| 10486 | User->getOperand(0).getValueType())); |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 10487 | } else if (User->getOpcode() == ISD::SELECT_CC) { |
| Hal Finkel | 4104a1a | 2014-12-14 05:53:19 +0000 | [diff] [blame] | 10488 | if (User->getOperand(0) == Inputs[i]) |
| 10489 | SelectTruncOp[0].insert(std::make_pair(User, |
| 10490 | User->getOperand(0).getValueType())); |
| 10491 | if (User->getOperand(1) == Inputs[i]) |
| 10492 | SelectTruncOp[1].insert(std::make_pair(User, |
| 10493 | User->getOperand(1).getValueType())); |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 10494 | } |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10495 | } |
| 10496 | } |
| 10497 | |
| 10498 | for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) { |
| 10499 | for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(), |
| 10500 | UE = PromOps[i].getNode()->use_end(); |
| 10501 | UI != UE; ++UI) { |
| 10502 | SDNode *User = *UI; |
| 10503 | if (User != N && !Visited.count(User)) |
| 10504 | return SDValue(); |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 10505 | |
| Hal Finkel | 4104a1a | 2014-12-14 05:53:19 +0000 | [diff] [blame] | 10506 | // If we're going to promote the non-output-value operand(s) or SELECT or |
| 10507 | // SELECT_CC, record them for truncation. |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 10508 | if (User->getOpcode() == ISD::SELECT) { |
| 10509 | if (User->getOperand(0) == PromOps[i]) |
| Hal Finkel | 4104a1a | 2014-12-14 05:53:19 +0000 | [diff] [blame] | 10510 | SelectTruncOp[0].insert(std::make_pair(User, |
| 10511 | User->getOperand(0).getValueType())); |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 10512 | } else if (User->getOpcode() == ISD::SELECT_CC) { |
| Hal Finkel | 4104a1a | 2014-12-14 05:53:19 +0000 | [diff] [blame] | 10513 | if (User->getOperand(0) == PromOps[i]) |
| 10514 | SelectTruncOp[0].insert(std::make_pair(User, |
| 10515 | User->getOperand(0).getValueType())); |
| 10516 | if (User->getOperand(1) == PromOps[i]) |
| 10517 | SelectTruncOp[1].insert(std::make_pair(User, |
| 10518 | User->getOperand(1).getValueType())); |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 10519 | } |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10520 | } |
| 10521 | } |
| 10522 | |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 10523 | unsigned PromBits = N->getOperand(0).getValueSizeInBits(); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10524 | bool ReallyNeedsExt = false; |
| 10525 | if (N->getOpcode() != ISD::ANY_EXTEND) { |
| 10526 | // If all of the inputs are not already sign/zero extended, then |
| 10527 | // we'll still need to do that at the end. |
| 10528 | for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) { |
| 10529 | if (isa<ConstantSDNode>(Inputs[i])) |
| 10530 | continue; |
| 10531 | |
| 10532 | unsigned OpBits = |
| 10533 | Inputs[i].getOperand(0).getValueSizeInBits(); |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 10534 | assert(PromBits < OpBits && "Truncation not to a smaller bit count?"); |
| 10535 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10536 | if ((N->getOpcode() == ISD::ZERO_EXTEND && |
| 10537 | !DAG.MaskedValueIsZero(Inputs[i].getOperand(0), |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 10538 | APInt::getHighBitsSet(OpBits, |
| 10539 | OpBits-PromBits))) || |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10540 | (N->getOpcode() == ISD::SIGN_EXTEND && |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 10541 | DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) < |
| 10542 | (OpBits-(PromBits-1)))) { |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10543 | ReallyNeedsExt = true; |
| 10544 | break; |
| 10545 | } |
| 10546 | } |
| 10547 | } |
| 10548 | |
| 10549 | // Replace all inputs, either with the truncation operand, or a |
| 10550 | // truncation or extension to the final output type. |
| 10551 | for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) { |
| 10552 | // Constant inputs need to be replaced with the to-be-promoted nodes that |
| 10553 | // use them because they might have users outside of the cluster of |
| 10554 | // promoted nodes. |
| 10555 | if (isa<ConstantSDNode>(Inputs[i])) |
| 10556 | continue; |
| 10557 | |
| 10558 | SDValue InSrc = Inputs[i].getOperand(0); |
| 10559 | if (Inputs[i].getValueType() == N->getValueType(0)) |
| 10560 | DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc); |
| 10561 | else if (N->getOpcode() == ISD::SIGN_EXTEND) |
| 10562 | DAG.ReplaceAllUsesOfValueWith(Inputs[i], |
| 10563 | DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0))); |
| 10564 | else if (N->getOpcode() == ISD::ZERO_EXTEND) |
| 10565 | DAG.ReplaceAllUsesOfValueWith(Inputs[i], |
| 10566 | DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0))); |
| 10567 | else |
| 10568 | DAG.ReplaceAllUsesOfValueWith(Inputs[i], |
| 10569 | DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0))); |
| 10570 | } |
| 10571 | |
| Hal Finkel | 1fb10e8 | 2016-05-12 04:00:56 +0000 | [diff] [blame] | 10572 | std::list<HandleSDNode> PromOpHandles; |
| 10573 | for (auto &PromOp : PromOps) |
| NAKAMURA Takumi | ae7c97d | 2016-06-20 00:49:20 +0000 | [diff] [blame] | 10574 | PromOpHandles.emplace_back(PromOp); |
| Hal Finkel | 1fb10e8 | 2016-05-12 04:00:56 +0000 | [diff] [blame] | 10575 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10576 | // Replace all operations (these are all the same, but have a different |
| 10577 | // (promoted) return type). DAG.getNode will validate that the types of |
| 10578 | // a binary operator match, so go through the list in reverse so that |
| 10579 | // we've likely promoted both operands first. |
| Hal Finkel | 1fb10e8 | 2016-05-12 04:00:56 +0000 | [diff] [blame] | 10580 | while (!PromOpHandles.empty()) { |
| 10581 | SDValue PromOp = PromOpHandles.back().getValue(); |
| 10582 | PromOpHandles.pop_back(); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10583 | |
| 10584 | unsigned C; |
| 10585 | switch (PromOp.getOpcode()) { |
| 10586 | default: C = 0; break; |
| 10587 | case ISD::SELECT: C = 1; break; |
| 10588 | case ISD::SELECT_CC: C = 2; break; |
| 10589 | } |
| 10590 | |
| 10591 | if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) && |
| 10592 | PromOp.getOperand(C).getValueType() != N->getValueType(0)) || |
| 10593 | (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) && |
| 10594 | PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) { |
| 10595 | // The to-be-promoted operands of this node have not yet been |
| 10596 | // promoted (this should be rare because we're going through the |
| 10597 | // list backward, but if one of the operands has several users in |
| 10598 | // this cluster of to-be-promoted nodes, it is possible). |
| Hal Finkel | 1fb10e8 | 2016-05-12 04:00:56 +0000 | [diff] [blame] | 10599 | PromOpHandles.emplace_front(PromOp); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10600 | continue; |
| 10601 | } |
| 10602 | |
| Hal Finkel | 4104a1a | 2014-12-14 05:53:19 +0000 | [diff] [blame] | 10603 | // For SELECT and SELECT_CC nodes, we do a similar check for any |
| 10604 | // to-be-promoted comparison inputs. |
| 10605 | if (PromOp.getOpcode() == ISD::SELECT || |
| 10606 | PromOp.getOpcode() == ISD::SELECT_CC) { |
| 10607 | if ((SelectTruncOp[0].count(PromOp.getNode()) && |
| 10608 | PromOp.getOperand(0).getValueType() != N->getValueType(0)) || |
| 10609 | (SelectTruncOp[1].count(PromOp.getNode()) && |
| 10610 | PromOp.getOperand(1).getValueType() != N->getValueType(0))) { |
| Hal Finkel | 1fb10e8 | 2016-05-12 04:00:56 +0000 | [diff] [blame] | 10611 | PromOpHandles.emplace_front(PromOp); |
| Hal Finkel | 4104a1a | 2014-12-14 05:53:19 +0000 | [diff] [blame] | 10612 | continue; |
| 10613 | } |
| 10614 | } |
| 10615 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10616 | SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(), |
| 10617 | PromOp.getNode()->op_end()); |
| 10618 | |
| 10619 | // If this node has constant inputs, then they'll need to be promoted here. |
| 10620 | for (unsigned i = 0; i < 2; ++i) { |
| 10621 | if (!isa<ConstantSDNode>(Ops[C+i])) |
| 10622 | continue; |
| 10623 | if (Ops[C+i].getValueType() == N->getValueType(0)) |
| 10624 | continue; |
| 10625 | |
| 10626 | if (N->getOpcode() == ISD::SIGN_EXTEND) |
| 10627 | Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0)); |
| 10628 | else if (N->getOpcode() == ISD::ZERO_EXTEND) |
| 10629 | Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0)); |
| 10630 | else |
| 10631 | Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0)); |
| 10632 | } |
| 10633 | |
| Hal Finkel | 4104a1a | 2014-12-14 05:53:19 +0000 | [diff] [blame] | 10634 | // If we've promoted the comparison inputs of a SELECT or SELECT_CC, |
| 10635 | // truncate them again to the original value type. |
| 10636 | if (PromOp.getOpcode() == ISD::SELECT || |
| 10637 | PromOp.getOpcode() == ISD::SELECT_CC) { |
| 10638 | auto SI0 = SelectTruncOp[0].find(PromOp.getNode()); |
| 10639 | if (SI0 != SelectTruncOp[0].end()) |
| 10640 | Ops[0] = DAG.getNode(ISD::TRUNCATE, dl, SI0->second, Ops[0]); |
| 10641 | auto SI1 = SelectTruncOp[1].find(PromOp.getNode()); |
| 10642 | if (SI1 != SelectTruncOp[1].end()) |
| 10643 | Ops[1] = DAG.getNode(ISD::TRUNCATE, dl, SI1->second, Ops[1]); |
| 10644 | } |
| 10645 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10646 | DAG.ReplaceAllUsesOfValueWith(PromOp, |
| Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 10647 | DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0), Ops)); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10648 | } |
| 10649 | |
| 10650 | // Now we're left with the initial extension itself. |
| 10651 | if (!ReallyNeedsExt) |
| 10652 | return N->getOperand(0); |
| 10653 | |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 10654 | // To zero extend, just mask off everything except for the first bit (in the |
| 10655 | // i1 case). |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10656 | if (N->getOpcode() == ISD::ZERO_EXTEND) |
| 10657 | return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0), |
| Hal Finkel | 46043ed | 2014-03-01 21:36:57 +0000 | [diff] [blame] | 10658 | DAG.getConstant(APInt::getLowBitsSet( |
| 10659 | N->getValueSizeInBits(0), PromBits), |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 10660 | dl, N->getValueType(0))); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10661 | |
| 10662 | assert(N->getOpcode() == ISD::SIGN_EXTEND && |
| 10663 | "Invalid extension type"); |
| Mehdi Amini | 9639d65 | 2015-07-09 02:09:20 +0000 | [diff] [blame] | 10664 | EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0), DAG.getDataLayout()); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10665 | SDValue ShiftCst = |
| NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 10666 | DAG.getConstant(N->getValueSizeInBits(0) - PromBits, dl, ShiftAmountTy); |
| 10667 | return DAG.getNode( |
| 10668 | ISD::SRA, dl, N->getValueType(0), |
| 10669 | DAG.getNode(ISD::SHL, dl, N->getValueType(0), N->getOperand(0), ShiftCst), |
| 10670 | ShiftCst); |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10671 | } |
| 10672 | |
| Nemanja Ivanovic | 44513e5 | 2016-07-05 09:22:29 +0000 | [diff] [blame] | 10673 | SDValue PPCTargetLowering::DAGCombineBuildVector(SDNode *N, |
| 10674 | DAGCombinerInfo &DCI) const { |
| 10675 | assert(N->getOpcode() == ISD::BUILD_VECTOR && |
| 10676 | "Should be called with a BUILD_VECTOR node"); |
| 10677 | |
| 10678 | SelectionDAG &DAG = DCI.DAG; |
| 10679 | SDLoc dl(N); |
| 10680 | if (N->getValueType(0) != MVT::v2f64 || !Subtarget.hasVSX()) |
| 10681 | return SDValue(); |
| 10682 | |
| 10683 | // Looking for: |
| 10684 | // (build_vector ([su]int_to_fp (extractelt 0)), [su]int_to_fp (extractelt 1)) |
| 10685 | if (N->getOperand(0).getOpcode() != ISD::SINT_TO_FP && |
| 10686 | N->getOperand(0).getOpcode() != ISD::UINT_TO_FP) |
| 10687 | return SDValue(); |
| 10688 | if (N->getOperand(1).getOpcode() != ISD::SINT_TO_FP && |
| 10689 | N->getOperand(1).getOpcode() != ISD::UINT_TO_FP) |
| 10690 | return SDValue(); |
| 10691 | if (N->getOperand(0).getOpcode() != N->getOperand(1).getOpcode()) |
| 10692 | return SDValue(); |
| 10693 | |
| 10694 | SDValue Ext1 = N->getOperand(0).getOperand(0); |
| 10695 | SDValue Ext2 = N->getOperand(1).getOperand(0); |
| 10696 | if(Ext1.getOpcode() != ISD::EXTRACT_VECTOR_ELT || |
| 10697 | Ext2.getOpcode() != ISD::EXTRACT_VECTOR_ELT) |
| 10698 | return SDValue(); |
| 10699 | |
| 10700 | ConstantSDNode *Ext1Op = dyn_cast<ConstantSDNode>(Ext1.getOperand(1)); |
| 10701 | ConstantSDNode *Ext2Op = dyn_cast<ConstantSDNode>(Ext2.getOperand(1)); |
| 10702 | if (!Ext1Op || !Ext2Op) |
| 10703 | return SDValue(); |
| 10704 | if (Ext1.getValueType() != MVT::i32 || |
| 10705 | Ext2.getValueType() != MVT::i32) |
| 10706 | if (Ext1.getOperand(0) != Ext2.getOperand(0)) |
| 10707 | return SDValue(); |
| 10708 | |
| 10709 | int FirstElem = Ext1Op->getZExtValue(); |
| 10710 | int SecondElem = Ext2Op->getZExtValue(); |
| 10711 | int SubvecIdx; |
| 10712 | if (FirstElem == 0 && SecondElem == 1) |
| 10713 | SubvecIdx = Subtarget.isLittleEndian() ? 1 : 0; |
| 10714 | else if (FirstElem == 2 && SecondElem == 3) |
| 10715 | SubvecIdx = Subtarget.isLittleEndian() ? 0 : 1; |
| 10716 | else |
| 10717 | return SDValue(); |
| 10718 | |
| 10719 | SDValue SrcVec = Ext1.getOperand(0); |
| 10720 | auto NodeType = (N->getOperand(1).getOpcode() == ISD::SINT_TO_FP) ? |
| 10721 | PPCISD::SINT_VEC_TO_FP : PPCISD::UINT_VEC_TO_FP; |
| 10722 | return DAG.getNode(NodeType, dl, MVT::v2f64, |
| 10723 | SrcVec, DAG.getIntPtrConstant(SubvecIdx, dl)); |
| 10724 | } |
| 10725 | |
| Hal Finkel | 5efb918 | 2015-01-06 06:01:57 +0000 | [diff] [blame] | 10726 | SDValue PPCTargetLowering::combineFPToIntToFP(SDNode *N, |
| 10727 | DAGCombinerInfo &DCI) const { |
| 10728 | assert((N->getOpcode() == ISD::SINT_TO_FP || |
| 10729 | N->getOpcode() == ISD::UINT_TO_FP) && |
| 10730 | "Need an int -> FP conversion node here"); |
| 10731 | |
| Hal Finkel | a932105 | 2016-10-02 02:10:20 +0000 | [diff] [blame] | 10732 | if (useSoftFloat() || !Subtarget.has64BitSupport()) |
| Hal Finkel | 5efb918 | 2015-01-06 06:01:57 +0000 | [diff] [blame] | 10733 | return SDValue(); |
| 10734 | |
| 10735 | SelectionDAG &DAG = DCI.DAG; |
| 10736 | SDLoc dl(N); |
| 10737 | SDValue Op(N, 0); |
| 10738 | |
| Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 10739 | SDValue FirstOperand(Op.getOperand(0)); |
| 10740 | bool SubWordLoad = FirstOperand.getOpcode() == ISD::LOAD && |
| 10741 | (FirstOperand.getValueType() == MVT::i8 || |
| 10742 | FirstOperand.getValueType() == MVT::i16); |
| 10743 | if (Subtarget.hasP9Vector() && Subtarget.hasP9Altivec() && SubWordLoad) { |
| 10744 | bool Signed = N->getOpcode() == ISD::SINT_TO_FP; |
| 10745 | bool DstDouble = Op.getValueType() == MVT::f64; |
| 10746 | unsigned ConvOp = Signed ? |
| 10747 | (DstDouble ? PPCISD::FCFID : PPCISD::FCFIDS) : |
| 10748 | (DstDouble ? PPCISD::FCFIDU : PPCISD::FCFIDUS); |
| 10749 | SDValue WidthConst = |
| 10750 | DAG.getIntPtrConstant(FirstOperand.getValueType() == MVT::i8 ? 1 : 2, |
| 10751 | dl, false); |
| 10752 | LoadSDNode *LDN = cast<LoadSDNode>(FirstOperand.getNode()); |
| 10753 | SDValue Ops[] = { LDN->getChain(), LDN->getBasePtr(), WidthConst }; |
| 10754 | SDValue Ld = DAG.getMemIntrinsicNode(PPCISD::LXSIZX, dl, |
| 10755 | DAG.getVTList(MVT::f64, MVT::Other), |
| 10756 | Ops, MVT::i8, LDN->getMemOperand()); |
| 10757 | |
| 10758 | // For signed conversion, we need to sign-extend the value in the VSR |
| 10759 | if (Signed) { |
| 10760 | SDValue ExtOps[] = { Ld, WidthConst }; |
| 10761 | SDValue Ext = DAG.getNode(PPCISD::VEXTS, dl, MVT::f64, ExtOps); |
| 10762 | return DAG.getNode(ConvOp, dl, DstDouble ? MVT::f64 : MVT::f32, Ext); |
| 10763 | } else |
| 10764 | return DAG.getNode(ConvOp, dl, DstDouble ? MVT::f64 : MVT::f32, Ld); |
| 10765 | } |
| 10766 | |
| Hal Finkel | 5efb918 | 2015-01-06 06:01:57 +0000 | [diff] [blame] | 10767 | // Don't handle ppc_fp128 here or i1 conversions. |
| 10768 | if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64) |
| 10769 | return SDValue(); |
| 10770 | if (Op.getOperand(0).getValueType() == MVT::i1) |
| 10771 | return SDValue(); |
| 10772 | |
| 10773 | // For i32 intermediate values, unfortunately, the conversion functions |
| 10774 | // leave the upper 32 bits of the value are undefined. Within the set of |
| 10775 | // scalar instructions, we have no method for zero- or sign-extending the |
| 10776 | // value. Thus, we cannot handle i32 intermediate values here. |
| 10777 | if (Op.getOperand(0).getValueType() == MVT::i32) |
| 10778 | return SDValue(); |
| 10779 | |
| 10780 | assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) && |
| 10781 | "UINT_TO_FP is supported only with FPCVT"); |
| 10782 | |
| 10783 | // If we have FCFIDS, then use it when converting to single-precision. |
| 10784 | // Otherwise, convert to double-precision and then round. |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 10785 | unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) |
| 10786 | ? (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDUS |
| 10787 | : PPCISD::FCFIDS) |
| 10788 | : (Op.getOpcode() == ISD::UINT_TO_FP ? PPCISD::FCFIDU |
| 10789 | : PPCISD::FCFID); |
| 10790 | MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) |
| 10791 | ? MVT::f32 |
| 10792 | : MVT::f64; |
| Hal Finkel | 5efb918 | 2015-01-06 06:01:57 +0000 | [diff] [blame] | 10793 | |
| 10794 | // If we're converting from a float, to an int, and back to a float again, |
| 10795 | // then we don't need the store/load pair at all. |
| 10796 | if ((Op.getOperand(0).getOpcode() == ISD::FP_TO_UINT && |
| 10797 | Subtarget.hasFPCVT()) || |
| 10798 | (Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT)) { |
| 10799 | SDValue Src = Op.getOperand(0).getOperand(0); |
| 10800 | if (Src.getValueType() == MVT::f32) { |
| 10801 | Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src); |
| 10802 | DCI.AddToWorklist(Src.getNode()); |
| Hal Finkel | be78c25 | 2015-08-20 01:18:20 +0000 | [diff] [blame] | 10803 | } else if (Src.getValueType() != MVT::f64) { |
| 10804 | // Make sure that we don't pick up a ppc_fp128 source value. |
| 10805 | return SDValue(); |
| Hal Finkel | 5efb918 | 2015-01-06 06:01:57 +0000 | [diff] [blame] | 10806 | } |
| 10807 | |
| 10808 | unsigned FCTOp = |
| 10809 | Op.getOperand(0).getOpcode() == ISD::FP_TO_SINT ? PPCISD::FCTIDZ : |
| 10810 | PPCISD::FCTIDUZ; |
| 10811 | |
| 10812 | SDValue Tmp = DAG.getNode(FCTOp, dl, MVT::f64, Src); |
| 10813 | SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Tmp); |
| 10814 | |
| 10815 | if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT()) { |
| 10816 | FP = DAG.getNode(ISD::FP_ROUND, dl, |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 10817 | MVT::f32, FP, DAG.getIntPtrConstant(0, dl)); |
| Hal Finkel | 5efb918 | 2015-01-06 06:01:57 +0000 | [diff] [blame] | 10818 | DCI.AddToWorklist(FP.getNode()); |
| 10819 | } |
| 10820 | |
| 10821 | return FP; |
| 10822 | } |
| 10823 | |
| 10824 | return SDValue(); |
| 10825 | } |
| 10826 | |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 10827 | // expandVSXLoadForLE - Convert VSX loads (which may be intrinsics for |
| 10828 | // builtins) into loads with swaps. |
| 10829 | SDValue PPCTargetLowering::expandVSXLoadForLE(SDNode *N, |
| 10830 | DAGCombinerInfo &DCI) const { |
| 10831 | SelectionDAG &DAG = DCI.DAG; |
| 10832 | SDLoc dl(N); |
| 10833 | SDValue Chain; |
| 10834 | SDValue Base; |
| 10835 | MachineMemOperand *MMO; |
| 10836 | |
| 10837 | switch (N->getOpcode()) { |
| 10838 | default: |
| 10839 | llvm_unreachable("Unexpected opcode for little endian VSX load"); |
| 10840 | case ISD::LOAD: { |
| 10841 | LoadSDNode *LD = cast<LoadSDNode>(N); |
| 10842 | Chain = LD->getChain(); |
| 10843 | Base = LD->getBasePtr(); |
| 10844 | MMO = LD->getMemOperand(); |
| 10845 | // If the MMO suggests this isn't a load of a full vector, leave |
| 10846 | // things alone. For a built-in, we have to make the change for |
| 10847 | // correctness, so if there is a size problem that will be a bug. |
| 10848 | if (MMO->getSize() < 16) |
| 10849 | return SDValue(); |
| 10850 | break; |
| 10851 | } |
| 10852 | case ISD::INTRINSIC_W_CHAIN: { |
| 10853 | MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N); |
| 10854 | Chain = Intrin->getChain(); |
| Nemanja Ivanovic | 7df26c9 | 2015-06-30 20:01:16 +0000 | [diff] [blame] | 10855 | // Similarly to the store case below, Intrin->getBasePtr() doesn't get |
| Nemanja Ivanovic | 9c8d4cf | 2015-06-30 19:45:45 +0000 | [diff] [blame] | 10856 | // us what we want. Get operand 2 instead. |
| Nemanja Ivanovic | 9c8d4cf | 2015-06-30 19:45:45 +0000 | [diff] [blame] | 10857 | Base = Intrin->getOperand(2); |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 10858 | MMO = Intrin->getMemOperand(); |
| 10859 | break; |
| 10860 | } |
| 10861 | } |
| 10862 | |
| 10863 | MVT VecTy = N->getValueType(0).getSimpleVT(); |
| 10864 | SDValue LoadOps[] = { Chain, Base }; |
| 10865 | SDValue Load = DAG.getMemIntrinsicNode(PPCISD::LXVD2X, dl, |
| Nirav Dave | 1f51c33 | 2016-04-15 15:01:38 +0000 | [diff] [blame] | 10866 | DAG.getVTList(MVT::v2f64, MVT::Other), |
| 10867 | LoadOps, MVT::v2f64, MMO); |
| 10868 | |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 10869 | DCI.AddToWorklist(Load.getNode()); |
| 10870 | Chain = Load.getValue(1); |
| Nirav Dave | 1f51c33 | 2016-04-15 15:01:38 +0000 | [diff] [blame] | 10871 | SDValue Swap = DAG.getNode( |
| 10872 | PPCISD::XXSWAPD, dl, DAG.getVTList(MVT::v2f64, MVT::Other), Chain, Load); |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 10873 | DCI.AddToWorklist(Swap.getNode()); |
| Nirav Dave | 1f51c33 | 2016-04-15 15:01:38 +0000 | [diff] [blame] | 10874 | |
| 10875 | // Add a bitcast if the resulting load type doesn't match v2f64. |
| 10876 | if (VecTy != MVT::v2f64) { |
| 10877 | SDValue N = DAG.getNode(ISD::BITCAST, dl, VecTy, Swap); |
| 10878 | DCI.AddToWorklist(N.getNode()); |
| 10879 | // Package {bitcast value, swap's chain} to match Load's shape. |
| 10880 | return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(VecTy, MVT::Other), |
| 10881 | N, Swap.getValue(1)); |
| 10882 | } |
| 10883 | |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 10884 | return Swap; |
| 10885 | } |
| 10886 | |
| 10887 | // expandVSXStoreForLE - Convert VSX stores (which may be intrinsics for |
| 10888 | // builtins) into stores with swaps. |
| 10889 | SDValue PPCTargetLowering::expandVSXStoreForLE(SDNode *N, |
| 10890 | DAGCombinerInfo &DCI) const { |
| 10891 | SelectionDAG &DAG = DCI.DAG; |
| 10892 | SDLoc dl(N); |
| 10893 | SDValue Chain; |
| 10894 | SDValue Base; |
| 10895 | unsigned SrcOpnd; |
| 10896 | MachineMemOperand *MMO; |
| 10897 | |
| 10898 | switch (N->getOpcode()) { |
| 10899 | default: |
| 10900 | llvm_unreachable("Unexpected opcode for little endian VSX store"); |
| 10901 | case ISD::STORE: { |
| 10902 | StoreSDNode *ST = cast<StoreSDNode>(N); |
| 10903 | Chain = ST->getChain(); |
| 10904 | Base = ST->getBasePtr(); |
| 10905 | MMO = ST->getMemOperand(); |
| 10906 | SrcOpnd = 1; |
| 10907 | // If the MMO suggests this isn't a store of a full vector, leave |
| 10908 | // things alone. For a built-in, we have to make the change for |
| 10909 | // correctness, so if there is a size problem that will be a bug. |
| 10910 | if (MMO->getSize() < 16) |
| 10911 | return SDValue(); |
| 10912 | break; |
| 10913 | } |
| 10914 | case ISD::INTRINSIC_VOID: { |
| 10915 | MemIntrinsicSDNode *Intrin = cast<MemIntrinsicSDNode>(N); |
| 10916 | Chain = Intrin->getChain(); |
| 10917 | // Intrin->getBasePtr() oddly does not get what we want. |
| 10918 | Base = Intrin->getOperand(3); |
| 10919 | MMO = Intrin->getMemOperand(); |
| 10920 | SrcOpnd = 2; |
| 10921 | break; |
| 10922 | } |
| 10923 | } |
| 10924 | |
| 10925 | SDValue Src = N->getOperand(SrcOpnd); |
| 10926 | MVT VecTy = Src.getValueType().getSimpleVT(); |
| Nirav Dave | 1f51c33 | 2016-04-15 15:01:38 +0000 | [diff] [blame] | 10927 | |
| 10928 | // All stores are done as v2f64 and possible bit cast. |
| 10929 | if (VecTy != MVT::v2f64) { |
| 10930 | Src = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Src); |
| 10931 | DCI.AddToWorklist(Src.getNode()); |
| 10932 | } |
| 10933 | |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 10934 | SDValue Swap = DAG.getNode(PPCISD::XXSWAPD, dl, |
| Nirav Dave | 1f51c33 | 2016-04-15 15:01:38 +0000 | [diff] [blame] | 10935 | DAG.getVTList(MVT::v2f64, MVT::Other), Chain, Src); |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 10936 | DCI.AddToWorklist(Swap.getNode()); |
| 10937 | Chain = Swap.getValue(1); |
| 10938 | SDValue StoreOps[] = { Chain, Swap, Base }; |
| 10939 | SDValue Store = DAG.getMemIntrinsicNode(PPCISD::STXVD2X, dl, |
| 10940 | DAG.getVTList(MVT::Other), |
| 10941 | StoreOps, VecTy, MMO); |
| 10942 | DCI.AddToWorklist(Store.getNode()); |
| 10943 | return Store; |
| 10944 | } |
| 10945 | |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 10946 | SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N, |
| 10947 | DAGCombinerInfo &DCI) const { |
| Chris Lattner | f418435 | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 10948 | SelectionDAG &DAG = DCI.DAG; |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10949 | SDLoc dl(N); |
| Chris Lattner | f418435 | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 10950 | switch (N->getOpcode()) { |
| 10951 | default: break; |
| Chris Lattner | 3c48ea5 | 2006-09-19 05:22:59 +0000 | [diff] [blame] | 10952 | case PPCISD::SHL: |
| Artyom Skrobov | 314ee04 | 2015-11-25 19:41:11 +0000 | [diff] [blame] | 10953 | if (isNullConstant(N->getOperand(0))) // 0 << V -> 0. |
| Chris Lattner | 3c48ea5 | 2006-09-19 05:22:59 +0000 | [diff] [blame] | 10954 | return N->getOperand(0); |
| Chris Lattner | 3c48ea5 | 2006-09-19 05:22:59 +0000 | [diff] [blame] | 10955 | break; |
| 10956 | case PPCISD::SRL: |
| Artyom Skrobov | 314ee04 | 2015-11-25 19:41:11 +0000 | [diff] [blame] | 10957 | if (isNullConstant(N->getOperand(0))) // 0 >>u V -> 0. |
| Chris Lattner | 3c48ea5 | 2006-09-19 05:22:59 +0000 | [diff] [blame] | 10958 | return N->getOperand(0); |
| Chris Lattner | 3c48ea5 | 2006-09-19 05:22:59 +0000 | [diff] [blame] | 10959 | break; |
| 10960 | case PPCISD::SRA: |
| 10961 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) { |
| Dan Gohman | f1d8304 | 2010-06-18 14:22:04 +0000 | [diff] [blame] | 10962 | if (C->isNullValue() || // 0 >>s V -> 0. |
| Chris Lattner | 3c48ea5 | 2006-09-19 05:22:59 +0000 | [diff] [blame] | 10963 | C->isAllOnesValue()) // -1 >>s V -> -1. |
| 10964 | return N->getOperand(0); |
| 10965 | } |
| 10966 | break; |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10967 | case ISD::SIGN_EXTEND: |
| 10968 | case ISD::ZERO_EXTEND: |
| NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 10969 | case ISD::ANY_EXTEND: |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 10970 | return DAGCombineExtBoolTrunc(N, DCI); |
| 10971 | case ISD::TRUNCATE: |
| 10972 | case ISD::SETCC: |
| 10973 | case ISD::SELECT_CC: |
| 10974 | return DAGCombineTruncBoolExt(N, DCI); |
| Chris Lattner | f418435 | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 10975 | case ISD::SINT_TO_FP: |
| Hal Finkel | 5efb918 | 2015-01-06 06:01:57 +0000 | [diff] [blame] | 10976 | case ISD::UINT_TO_FP: |
| 10977 | return combineFPToIntToFP(N, DCI); |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 10978 | case ISD::STORE: { |
| Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 10979 | EVT Op1VT = N->getOperand(1).getValueType(); |
| 10980 | bool ValidTypeForStoreFltAsInt = (Op1VT == MVT::i32) || |
| 10981 | (Subtarget.hasP9Vector() && (Op1VT == MVT::i8 || Op1VT == MVT::i16)); |
| 10982 | |
| Chris Lattner | 27f5345 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 10983 | // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)). |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 10984 | if (Subtarget.hasSTFIWX() && !cast<StoreSDNode>(N)->isTruncatingStore() && |
| Chris Lattner | 27f5345 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 10985 | N->getOperand(1).getOpcode() == ISD::FP_TO_SINT && |
| Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 10986 | ValidTypeForStoreFltAsInt && |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 10987 | N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10988 | SDValue Val = N->getOperand(1).getOperand(0); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 10989 | if (Val.getValueType() == MVT::f32) { |
| 10990 | Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10991 | DCI.AddToWorklist(Val.getNode()); |
| Chris Lattner | 27f5345 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 10992 | } |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 10993 | Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10994 | DCI.AddToWorklist(Val.getNode()); |
| Chris Lattner | 27f5345 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 10995 | |
| Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 10996 | if (Op1VT == MVT::i32) { |
| 10997 | SDValue Ops[] = { |
| 10998 | N->getOperand(0), Val, N->getOperand(2), |
| 10999 | DAG.getValueType(N->getOperand(1).getValueType()) |
| 11000 | }; |
| Hal Finkel | 60c7510 | 2013-04-01 15:37:53 +0000 | [diff] [blame] | 11001 | |
| Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 11002 | Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl, |
| 11003 | DAG.getVTList(MVT::Other), Ops, |
| 11004 | cast<StoreSDNode>(N)->getMemoryVT(), |
| 11005 | cast<StoreSDNode>(N)->getMemOperand()); |
| 11006 | } else { |
| 11007 | unsigned WidthInBytes = |
| 11008 | N->getOperand(1).getValueType() == MVT::i8 ? 1 : 2; |
| 11009 | SDValue WidthConst = DAG.getIntPtrConstant(WidthInBytes, dl, false); |
| 11010 | |
| 11011 | SDValue Ops[] = { |
| 11012 | N->getOperand(0), Val, N->getOperand(2), WidthConst, |
| 11013 | DAG.getValueType(N->getOperand(1).getValueType()) |
| 11014 | }; |
| 11015 | Val = DAG.getMemIntrinsicNode(PPCISD::STXSIX, dl, |
| 11016 | DAG.getVTList(MVT::Other), Ops, |
| 11017 | cast<StoreSDNode>(N)->getMemoryVT(), |
| 11018 | cast<StoreSDNode>(N)->getMemOperand()); |
| 11019 | } |
| 11020 | |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 11021 | DCI.AddToWorklist(Val.getNode()); |
| Chris Lattner | 27f5345 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 11022 | return Val; |
| 11023 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11024 | |
| Chris Lattner | a7976d3 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 11025 | // Turn STORE (BSWAP) -> sthbrx/stwbrx. |
| Dan Gohman | 28328db | 2009-09-25 00:57:30 +0000 | [diff] [blame] | 11026 | if (cast<StoreSDNode>(N)->isUnindexed() && |
| 11027 | N->getOperand(1).getOpcode() == ISD::BSWAP && |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 11028 | N->getOperand(1).getNode()->hasOneUse() && |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11029 | (N->getOperand(1).getValueType() == MVT::i32 || |
| Hal Finkel | 31d2956 | 2013-03-28 19:25:55 +0000 | [diff] [blame] | 11030 | N->getOperand(1).getValueType() == MVT::i16 || |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 11031 | (Subtarget.hasLDBRX() && Subtarget.isPPC64() && |
| Hal Finkel | 31d2956 | 2013-03-28 19:25:55 +0000 | [diff] [blame] | 11032 | N->getOperand(1).getValueType() == MVT::i64))) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11033 | SDValue BSwapOp = N->getOperand(1).getOperand(0); |
| Chris Lattner | a7976d3 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 11034 | // Do an any-extend to 32-bits if this is a half-word input. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11035 | if (BSwapOp.getValueType() == MVT::i16) |
| 11036 | BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp); |
| Chris Lattner | a7976d3 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 11037 | |
| Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 11038 | SDValue Ops[] = { |
| 11039 | N->getOperand(0), BSwapOp, N->getOperand(2), |
| 11040 | DAG.getValueType(N->getOperand(1).getValueType()) |
| 11041 | }; |
| 11042 | return |
| 11043 | DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other), |
| Craig Topper | 206fcd4 | 2014-04-26 19:29:41 +0000 | [diff] [blame] | 11044 | Ops, cast<StoreSDNode>(N)->getMemoryVT(), |
| Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 11045 | cast<StoreSDNode>(N)->getMemOperand()); |
| Chris Lattner | a7976d3 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 11046 | } |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 11047 | |
| 11048 | // For little endian, VSX stores require generating xxswapd/lxvd2x. |
| Nemanja Ivanovic | 6e7879c | 2016-09-22 09:52:19 +0000 | [diff] [blame] | 11049 | // Not needed on ISA 3.0 based CPUs since we have a non-permuting store. |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 11050 | EVT VT = N->getOperand(1).getValueType(); |
| 11051 | if (VT.isSimple()) { |
| 11052 | MVT StoreVT = VT.getSimpleVT(); |
| Nemanja Ivanovic | 6e7879c | 2016-09-22 09:52:19 +0000 | [diff] [blame] | 11053 | if (Subtarget.needsSwapsForVSXMemOps() && |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 11054 | (StoreVT == MVT::v2f64 || StoreVT == MVT::v2i64 || |
| 11055 | StoreVT == MVT::v4f32 || StoreVT == MVT::v4i32)) |
| 11056 | return expandVSXStoreForLE(N, DCI); |
| 11057 | } |
| Chris Lattner | a7976d3 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 11058 | break; |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 11059 | } |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 11060 | case ISD::LOAD: { |
| 11061 | LoadSDNode *LD = cast<LoadSDNode>(N); |
| 11062 | EVT VT = LD->getValueType(0); |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 11063 | |
| 11064 | // For little endian, VSX loads require generating lxvd2x/xxswapd. |
| Nemanja Ivanovic | 6e7879c | 2016-09-22 09:52:19 +0000 | [diff] [blame] | 11065 | // Not needed on ISA 3.0 based CPUs since we have a non-permuting load. |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 11066 | if (VT.isSimple()) { |
| 11067 | MVT LoadVT = VT.getSimpleVT(); |
| Nemanja Ivanovic | 6e7879c | 2016-09-22 09:52:19 +0000 | [diff] [blame] | 11068 | if (Subtarget.needsSwapsForVSXMemOps() && |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 11069 | (LoadVT == MVT::v2f64 || LoadVT == MVT::v2i64 || |
| 11070 | LoadVT == MVT::v4f32 || LoadVT == MVT::v4i32)) |
| 11071 | return expandVSXLoadForLE(N, DCI); |
| 11072 | } |
| 11073 | |
| Hal Finkel | 851b33a | 2016-03-31 02:56:05 +0000 | [diff] [blame] | 11074 | // We sometimes end up with a 64-bit integer load, from which we extract |
| 11075 | // two single-precision floating-point numbers. This happens with |
| 11076 | // std::complex<float>, and other similar structures, because of the way we |
| 11077 | // canonicalize structure copies. However, if we lack direct moves, |
| 11078 | // then the final bitcasts from the extracted integer values to the |
| 11079 | // floating-point numbers turn into store/load pairs. Even with direct moves, |
| 11080 | // just loading the two floating-point numbers is likely better. |
| 11081 | auto ReplaceTwoFloatLoad = [&]() { |
| 11082 | if (VT != MVT::i64) |
| 11083 | return false; |
| 11084 | |
| 11085 | if (LD->getExtensionType() != ISD::NON_EXTLOAD || |
| 11086 | LD->isVolatile()) |
| 11087 | return false; |
| 11088 | |
| 11089 | // We're looking for a sequence like this: |
| 11090 | // t13: i64,ch = load<LD8[%ref.tmp]> t0, t6, undef:i64 |
| 11091 | // t16: i64 = srl t13, Constant:i32<32> |
| 11092 | // t17: i32 = truncate t16 |
| 11093 | // t18: f32 = bitcast t17 |
| 11094 | // t19: i32 = truncate t13 |
| 11095 | // t20: f32 = bitcast t19 |
| 11096 | |
| 11097 | if (!LD->hasNUsesOfValue(2, 0)) |
| 11098 | return false; |
| 11099 | |
| 11100 | auto UI = LD->use_begin(); |
| 11101 | while (UI.getUse().getResNo() != 0) ++UI; |
| 11102 | SDNode *Trunc = *UI++; |
| 11103 | while (UI.getUse().getResNo() != 0) ++UI; |
| 11104 | SDNode *RightShift = *UI; |
| 11105 | if (Trunc->getOpcode() != ISD::TRUNCATE) |
| 11106 | std::swap(Trunc, RightShift); |
| 11107 | |
| 11108 | if (Trunc->getOpcode() != ISD::TRUNCATE || |
| 11109 | Trunc->getValueType(0) != MVT::i32 || |
| 11110 | !Trunc->hasOneUse()) |
| 11111 | return false; |
| 11112 | if (RightShift->getOpcode() != ISD::SRL || |
| 11113 | !isa<ConstantSDNode>(RightShift->getOperand(1)) || |
| 11114 | RightShift->getConstantOperandVal(1) != 32 || |
| 11115 | !RightShift->hasOneUse()) |
| 11116 | return false; |
| 11117 | |
| 11118 | SDNode *Trunc2 = *RightShift->use_begin(); |
| 11119 | if (Trunc2->getOpcode() != ISD::TRUNCATE || |
| 11120 | Trunc2->getValueType(0) != MVT::i32 || |
| 11121 | !Trunc2->hasOneUse()) |
| 11122 | return false; |
| 11123 | |
| 11124 | SDNode *Bitcast = *Trunc->use_begin(); |
| 11125 | SDNode *Bitcast2 = *Trunc2->use_begin(); |
| 11126 | |
| 11127 | if (Bitcast->getOpcode() != ISD::BITCAST || |
| 11128 | Bitcast->getValueType(0) != MVT::f32) |
| 11129 | return false; |
| NAKAMURA Takumi | ae7c97d | 2016-06-20 00:49:20 +0000 | [diff] [blame] | 11130 | if (Bitcast2->getOpcode() != ISD::BITCAST || |
| Hal Finkel | 851b33a | 2016-03-31 02:56:05 +0000 | [diff] [blame] | 11131 | Bitcast2->getValueType(0) != MVT::f32) |
| 11132 | return false; |
| 11133 | |
| 11134 | if (Subtarget.isLittleEndian()) |
| 11135 | std::swap(Bitcast, Bitcast2); |
| 11136 | |
| 11137 | // Bitcast has the second float (in memory-layout order) and Bitcast2 |
| 11138 | // has the first one. |
| 11139 | |
| 11140 | SDValue BasePtr = LD->getBasePtr(); |
| 11141 | if (LD->isIndexed()) { |
| 11142 | assert(LD->getAddressingMode() == ISD::PRE_INC && |
| 11143 | "Non-pre-inc AM on PPC?"); |
| 11144 | BasePtr = |
| 11145 | DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr, |
| 11146 | LD->getOffset()); |
| 11147 | } |
| 11148 | |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 11149 | auto MMOFlags = |
| 11150 | LD->getMemOperand()->getFlags() & ~MachineMemOperand::MOVolatile; |
| 11151 | SDValue FloatLoad = DAG.getLoad(MVT::f32, dl, LD->getChain(), BasePtr, |
| 11152 | LD->getPointerInfo(), LD->getAlignment(), |
| 11153 | MMOFlags, LD->getAAInfo()); |
| Hal Finkel | 851b33a | 2016-03-31 02:56:05 +0000 | [diff] [blame] | 11154 | SDValue AddPtr = |
| 11155 | DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), |
| 11156 | BasePtr, DAG.getIntPtrConstant(4, dl)); |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 11157 | SDValue FloatLoad2 = DAG.getLoad( |
| 11158 | MVT::f32, dl, SDValue(FloatLoad.getNode(), 1), AddPtr, |
| 11159 | LD->getPointerInfo().getWithOffset(4), |
| 11160 | MinAlign(LD->getAlignment(), 4), MMOFlags, LD->getAAInfo()); |
| Hal Finkel | 851b33a | 2016-03-31 02:56:05 +0000 | [diff] [blame] | 11161 | |
| 11162 | if (LD->isIndexed()) { |
| NAKAMURA Takumi | fe1202c | 2016-06-20 00:37:41 +0000 | [diff] [blame] | 11163 | // Note that DAGCombine should re-form any pre-increment load(s) from |
| 11164 | // what is produced here if that makes sense. |
| Hal Finkel | 851b33a | 2016-03-31 02:56:05 +0000 | [diff] [blame] | 11165 | DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), BasePtr); |
| 11166 | } |
| 11167 | |
| 11168 | DCI.CombineTo(Bitcast2, FloatLoad); |
| 11169 | DCI.CombineTo(Bitcast, FloatLoad2); |
| 11170 | |
| 11171 | DAG.ReplaceAllUsesOfValueWith(SDValue(LD, LD->isIndexed() ? 2 : 1), |
| 11172 | SDValue(FloatLoad2.getNode(), 1)); |
| 11173 | return true; |
| 11174 | }; |
| 11175 | |
| 11176 | if (ReplaceTwoFloatLoad()) |
| 11177 | return SDValue(N, 0); |
| 11178 | |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11179 | EVT MemVT = LD->getMemoryVT(); |
| 11180 | Type *Ty = MemVT.getTypeForEVT(*DAG.getContext()); |
| Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 11181 | unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11182 | Type *STy = MemVT.getScalarType().getTypeForEVT(*DAG.getContext()); |
| Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 11183 | unsigned ScalarABIAlignment = DAG.getDataLayout().getABITypeAlignment(STy); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11184 | if (LD->isUnindexed() && VT.isVector() && |
| 11185 | ((Subtarget.hasAltivec() && ISD::isNON_EXTLoad(N) && |
| 11186 | // P8 and later hardware should just use LOAD. |
| 11187 | !Subtarget.hasP8Vector() && (VT == MVT::v16i8 || VT == MVT::v8i16 || |
| 11188 | VT == MVT::v4i32 || VT == MVT::v4f32)) || |
| 11189 | (Subtarget.hasQPX() && (VT == MVT::v4f64 || VT == MVT::v4f32) && |
| 11190 | LD->getAlignment() >= ScalarABIAlignment)) && |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 11191 | LD->getAlignment() < ABIAlignment) { |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11192 | // This is a type-legal unaligned Altivec or QPX load. |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 11193 | SDValue Chain = LD->getChain(); |
| 11194 | SDValue Ptr = LD->getBasePtr(); |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 11195 | bool isLittleEndian = Subtarget.isLittleEndian(); |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 11196 | |
| 11197 | // This implements the loading of unaligned vectors as described in |
| 11198 | // the venerable Apple Velocity Engine overview. Specifically: |
| 11199 | // https://developer.apple.com/hardwaredrivers/ve/alignment.html |
| 11200 | // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html |
| 11201 | // |
| 11202 | // The general idea is to expand a sequence of one or more unaligned |
| Bill Schmidt | 6b5a7df | 2014-06-09 22:00:52 +0000 | [diff] [blame] | 11203 | // loads into an alignment-based permutation-control instruction (lvsl |
| 11204 | // or lvsr), a series of regular vector loads (which always truncate |
| 11205 | // their input address to an aligned address), and a series of |
| 11206 | // permutations. The results of these permutations are the requested |
| 11207 | // loaded values. The trick is that the last "extra" load is not taken |
| 11208 | // from the address you might suspect (sizeof(vector) bytes after the |
| 11209 | // last requested load), but rather sizeof(vector) - 1 bytes after the |
| 11210 | // last requested vector. The point of this is to avoid a page fault if |
| 11211 | // the base address happened to be aligned. This works because if the |
| 11212 | // base address is aligned, then adding less than a full vector length |
| 11213 | // will cause the last vector in the sequence to be (re)loaded. |
| 11214 | // Otherwise, the next vector will be fetched as you might suspect was |
| 11215 | // necessary. |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 11216 | |
| Hal Finkel | bc2ee4c | 2013-05-25 04:05:05 +0000 | [diff] [blame] | 11217 | // We might be able to reuse the permutation generation from |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 11218 | // a different base address offset from this one by an aligned amount. |
| Hal Finkel | bc2ee4c | 2013-05-25 04:05:05 +0000 | [diff] [blame] | 11219 | // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this |
| 11220 | // optimization later. |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11221 | Intrinsic::ID Intr, IntrLD, IntrPerm; |
| 11222 | MVT PermCntlTy, PermTy, LDTy; |
| 11223 | if (Subtarget.hasAltivec()) { |
| 11224 | Intr = isLittleEndian ? Intrinsic::ppc_altivec_lvsr : |
| 11225 | Intrinsic::ppc_altivec_lvsl; |
| 11226 | IntrLD = Intrinsic::ppc_altivec_lvx; |
| 11227 | IntrPerm = Intrinsic::ppc_altivec_vperm; |
| 11228 | PermCntlTy = MVT::v16i8; |
| 11229 | PermTy = MVT::v4i32; |
| 11230 | LDTy = MVT::v4i32; |
| 11231 | } else { |
| 11232 | Intr = MemVT == MVT::v4f64 ? Intrinsic::ppc_qpx_qvlpcld : |
| 11233 | Intrinsic::ppc_qpx_qvlpcls; |
| 11234 | IntrLD = MemVT == MVT::v4f64 ? Intrinsic::ppc_qpx_qvlfd : |
| 11235 | Intrinsic::ppc_qpx_qvlfs; |
| 11236 | IntrPerm = Intrinsic::ppc_qpx_qvfperm; |
| 11237 | PermCntlTy = MVT::v4f64; |
| 11238 | PermTy = MVT::v4f64; |
| 11239 | LDTy = MemVT.getSimpleVT(); |
| 11240 | } |
| 11241 | |
| 11242 | SDValue PermCntl = BuildIntrinsicOp(Intr, Ptr, DAG, dl, PermCntlTy); |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 11243 | |
| Hal Finkel | b6d0d6b | 2014-08-01 05:20:41 +0000 | [diff] [blame] | 11244 | // Create the new MMO for the new base load. It is like the original MMO, |
| 11245 | // but represents an area in memory almost twice the vector size centered |
| 11246 | // on the original address. If the address is unaligned, we might start |
| 11247 | // reading up to (sizeof(vector)-1) bytes below the address of the |
| 11248 | // original unaligned load. |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 11249 | MachineFunction &MF = DAG.getMachineFunction(); |
| Hal Finkel | b6d0d6b | 2014-08-01 05:20:41 +0000 | [diff] [blame] | 11250 | MachineMemOperand *BaseMMO = |
| Hal Finkel | 99d9532 | 2015-09-03 21:12:15 +0000 | [diff] [blame] | 11251 | MF.getMachineMemOperand(LD->getMemOperand(), |
| 11252 | -(long)MemVT.getStoreSize()+1, |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11253 | 2*MemVT.getStoreSize()-1); |
| Hal Finkel | b6d0d6b | 2014-08-01 05:20:41 +0000 | [diff] [blame] | 11254 | |
| 11255 | // Create the new base load. |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 11256 | SDValue LDXIntID = |
| 11257 | DAG.getTargetConstant(IntrLD, dl, getPointerTy(MF.getDataLayout())); |
| Hal Finkel | b6d0d6b | 2014-08-01 05:20:41 +0000 | [diff] [blame] | 11258 | SDValue BaseLoadOps[] = { Chain, LDXIntID, Ptr }; |
| 11259 | SDValue BaseLoad = |
| 11260 | DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl, |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11261 | DAG.getVTList(PermTy, MVT::Other), |
| 11262 | BaseLoadOps, LDTy, BaseMMO); |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 11263 | |
| 11264 | // Note that the value of IncOffset (which is provided to the next |
| 11265 | // load's pointer info offset value, and thus used to calculate the |
| 11266 | // alignment), and the value of IncValue (which is actually used to |
| 11267 | // increment the pointer value) are different! This is because we |
| 11268 | // require the next load to appear to be aligned, even though it |
| 11269 | // is actually offset from the base pointer by a lesser amount. |
| 11270 | int IncOffset = VT.getSizeInBits() / 8; |
| Hal Finkel | 7d8a691 | 2013-05-26 18:08:30 +0000 | [diff] [blame] | 11271 | int IncValue = IncOffset; |
| 11272 | |
| 11273 | // Walk (both up and down) the chain looking for another load at the real |
| 11274 | // (aligned) offset (the alignment of the other load does not matter in |
| 11275 | // this case). If found, then do not use the offset reduction trick, as |
| 11276 | // that will prevent the loads from being later combined (as they would |
| 11277 | // otherwise be duplicates). |
| 11278 | if (!findConsecutiveLoad(LD, DAG)) |
| 11279 | --IncValue; |
| 11280 | |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 11281 | SDValue Increment = |
| 11282 | DAG.getConstant(IncValue, dl, getPointerTy(MF.getDataLayout())); |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 11283 | Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment); |
| 11284 | |
| Hal Finkel | b6d0d6b | 2014-08-01 05:20:41 +0000 | [diff] [blame] | 11285 | MachineMemOperand *ExtraMMO = |
| 11286 | MF.getMachineMemOperand(LD->getMemOperand(), |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11287 | 1, 2*MemVT.getStoreSize()-1); |
| Hal Finkel | b6d0d6b | 2014-08-01 05:20:41 +0000 | [diff] [blame] | 11288 | SDValue ExtraLoadOps[] = { Chain, LDXIntID, Ptr }; |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 11289 | SDValue ExtraLoad = |
| Hal Finkel | b6d0d6b | 2014-08-01 05:20:41 +0000 | [diff] [blame] | 11290 | DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl, |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11291 | DAG.getVTList(PermTy, MVT::Other), |
| 11292 | ExtraLoadOps, LDTy, ExtraMMO); |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 11293 | |
| 11294 | SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 11295 | BaseLoad.getValue(1), ExtraLoad.getValue(1)); |
| 11296 | |
| Bill Schmidt | 6b5a7df | 2014-06-09 22:00:52 +0000 | [diff] [blame] | 11297 | // Because vperm has a big-endian bias, we must reverse the order |
| 11298 | // of the input vectors and complement the permute control vector |
| 11299 | // when generating little endian code. We have already handled the |
| 11300 | // latter by using lvsr instead of lvsl, so just reverse BaseLoad |
| 11301 | // and ExtraLoad here. |
| 11302 | SDValue Perm; |
| 11303 | if (isLittleEndian) |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11304 | Perm = BuildIntrinsicOp(IntrPerm, |
| Bill Schmidt | 6b5a7df | 2014-06-09 22:00:52 +0000 | [diff] [blame] | 11305 | ExtraLoad, BaseLoad, PermCntl, DAG, dl); |
| 11306 | else |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11307 | Perm = BuildIntrinsicOp(IntrPerm, |
| Bill Schmidt | 6b5a7df | 2014-06-09 22:00:52 +0000 | [diff] [blame] | 11308 | BaseLoad, ExtraLoad, PermCntl, DAG, dl); |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 11309 | |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11310 | if (VT != PermTy) |
| 11311 | Perm = Subtarget.hasAltivec() ? |
| 11312 | DAG.getNode(ISD::BITCAST, dl, VT, Perm) : |
| 11313 | DAG.getNode(ISD::FP_ROUND, dl, VT, Perm, // QPX |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 11314 | DAG.getTargetConstant(1, dl, MVT::i64)); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11315 | // second argument is 1 because this rounding |
| 11316 | // is always exact. |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 11317 | |
| Hal Finkel | b6d0d6b | 2014-08-01 05:20:41 +0000 | [diff] [blame] | 11318 | // The output of the permutation is our loaded result, the TokenFactor is |
| 11319 | // our new chain. |
| 11320 | DCI.CombineTo(N, Perm, TF); |
| Hal Finkel | cf2e908 | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 11321 | return SDValue(N, 0); |
| 11322 | } |
| 11323 | } |
| 11324 | break; |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 11325 | case ISD::INTRINSIC_WO_CHAIN: { |
| 11326 | bool isLittleEndian = Subtarget.isLittleEndian(); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11327 | unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 11328 | Intrinsic::ID Intr = (isLittleEndian ? Intrinsic::ppc_altivec_lvsr |
| 11329 | : Intrinsic::ppc_altivec_lvsl); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11330 | if ((IID == Intr || |
| 11331 | IID == Intrinsic::ppc_qpx_qvlpcld || |
| 11332 | IID == Intrinsic::ppc_qpx_qvlpcls) && |
| 11333 | N->getOperand(1)->getOpcode() == ISD::ADD) { |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 11334 | SDValue Add = N->getOperand(1); |
| Hal Finkel | bc2ee4c | 2013-05-25 04:05:05 +0000 | [diff] [blame] | 11335 | |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11336 | int Bits = IID == Intrinsic::ppc_qpx_qvlpcld ? |
| 11337 | 5 /* 32 byte alignment */ : 4 /* 16 byte alignment */; |
| 11338 | |
| Sanjay Patel | 5f6bb6c | 2016-09-14 15:43:44 +0000 | [diff] [blame] | 11339 | if (DAG.MaskedValueIsZero(Add->getOperand(1), |
| 11340 | APInt::getAllOnesValue(Bits /* alignment */) |
| 11341 | .zext(Add.getScalarValueSizeInBits()))) { |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 11342 | SDNode *BasePtr = Add->getOperand(0).getNode(); |
| 11343 | for (SDNode::use_iterator UI = BasePtr->use_begin(), |
| 11344 | UE = BasePtr->use_end(); |
| 11345 | UI != UE; ++UI) { |
| 11346 | if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN && |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11347 | cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() == IID) { |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 11348 | // We've found another LVSL/LVSR, and this address is an aligned |
| 11349 | // multiple of that one. The results will be the same, so use the |
| 11350 | // one we've just found instead. |
| Hal Finkel | bc2ee4c | 2013-05-25 04:05:05 +0000 | [diff] [blame] | 11351 | |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 11352 | return SDValue(*UI, 0); |
| 11353 | } |
| Hal Finkel | bc2ee4c | 2013-05-25 04:05:05 +0000 | [diff] [blame] | 11354 | } |
| 11355 | } |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11356 | |
| 11357 | if (isa<ConstantSDNode>(Add->getOperand(1))) { |
| 11358 | SDNode *BasePtr = Add->getOperand(0).getNode(); |
| 11359 | for (SDNode::use_iterator UI = BasePtr->use_begin(), |
| 11360 | UE = BasePtr->use_end(); UI != UE; ++UI) { |
| 11361 | if (UI->getOpcode() == ISD::ADD && |
| 11362 | isa<ConstantSDNode>(UI->getOperand(1)) && |
| 11363 | (cast<ConstantSDNode>(Add->getOperand(1))->getZExtValue() - |
| 11364 | cast<ConstantSDNode>(UI->getOperand(1))->getZExtValue()) % |
| Aaron Ballman | 5561ed4 | 2015-02-25 13:05:24 +0000 | [diff] [blame] | 11365 | (1ULL << Bits) == 0) { |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11366 | SDNode *OtherAdd = *UI; |
| 11367 | for (SDNode::use_iterator VI = OtherAdd->use_begin(), |
| 11368 | VE = OtherAdd->use_end(); VI != VE; ++VI) { |
| 11369 | if (VI->getOpcode() == ISD::INTRINSIC_WO_CHAIN && |
| 11370 | cast<ConstantSDNode>(VI->getOperand(0))->getZExtValue() == IID) { |
| 11371 | return SDValue(*VI, 0); |
| 11372 | } |
| 11373 | } |
| 11374 | } |
| 11375 | } |
| 11376 | } |
| Hal Finkel | bc2ee4c | 2013-05-25 04:05:05 +0000 | [diff] [blame] | 11377 | } |
| 11378 | } |
| Hal Finkel | c3cfbf8 | 2013-09-13 20:09:02 +0000 | [diff] [blame] | 11379 | |
| 11380 | break; |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 11381 | case ISD::INTRINSIC_W_CHAIN: { |
| 11382 | // For little endian, VSX loads require generating lxvd2x/xxswapd. |
| Nemanja Ivanovic | 6e7879c | 2016-09-22 09:52:19 +0000 | [diff] [blame] | 11383 | // Not needed on ISA 3.0 based CPUs since we have a non-permuting load. |
| 11384 | if (Subtarget.needsSwapsForVSXMemOps()) { |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 11385 | switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { |
| 11386 | default: |
| 11387 | break; |
| 11388 | case Intrinsic::ppc_vsx_lxvw4x: |
| 11389 | case Intrinsic::ppc_vsx_lxvd2x: |
| 11390 | return expandVSXLoadForLE(N, DCI); |
| 11391 | } |
| 11392 | } |
| 11393 | break; |
| 11394 | } |
| 11395 | case ISD::INTRINSIC_VOID: { |
| 11396 | // For little endian, VSX stores require generating xxswapd/stxvd2x. |
| Nemanja Ivanovic | 6e7879c | 2016-09-22 09:52:19 +0000 | [diff] [blame] | 11397 | // Not needed on ISA 3.0 based CPUs since we have a non-permuting store. |
| 11398 | if (Subtarget.needsSwapsForVSXMemOps()) { |
| Bill Schmidt | fae5d71 | 2014-12-09 16:35:51 +0000 | [diff] [blame] | 11399 | switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { |
| 11400 | default: |
| 11401 | break; |
| 11402 | case Intrinsic::ppc_vsx_stxvw4x: |
| 11403 | case Intrinsic::ppc_vsx_stxvd2x: |
| 11404 | return expandVSXStoreForLE(N, DCI); |
| 11405 | } |
| 11406 | } |
| 11407 | break; |
| 11408 | } |
| Chris Lattner | a7976d3 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 11409 | case ISD::BSWAP: |
| 11410 | // Turn BSWAP (LOAD) -> lhbrx/lwbrx. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 11411 | if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) && |
| Chris Lattner | a7976d3 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 11412 | N->getOperand(0).hasOneUse() && |
| Hal Finkel | 31d2956 | 2013-03-28 19:25:55 +0000 | [diff] [blame] | 11413 | (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 || |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 11414 | (Subtarget.hasLDBRX() && Subtarget.isPPC64() && |
| Hal Finkel | 31d2956 | 2013-03-28 19:25:55 +0000 | [diff] [blame] | 11415 | N->getValueType(0) == MVT::i64))) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11416 | SDValue Load = N->getOperand(0); |
| Evan Cheng | e71fe34d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 11417 | LoadSDNode *LD = cast<LoadSDNode>(Load); |
| Chris Lattner | a7976d3 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 11418 | // Create the byte-swapping load. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11419 | SDValue Ops[] = { |
| Evan Cheng | e71fe34d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 11420 | LD->getChain(), // Chain |
| 11421 | LD->getBasePtr(), // Ptr |
| Chris Lattner | d66f14e | 2006-08-11 17:18:05 +0000 | [diff] [blame] | 11422 | DAG.getValueType(N->getValueType(0)) // VT |
| 11423 | }; |
| Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 11424 | SDValue BSLoad = |
| 11425 | DAG.getMemIntrinsicNode(PPCISD::LBRX, dl, |
| Hal Finkel | 31d2956 | 2013-03-28 19:25:55 +0000 | [diff] [blame] | 11426 | DAG.getVTList(N->getValueType(0) == MVT::i64 ? |
| 11427 | MVT::i64 : MVT::i32, MVT::Other), |
| Craig Topper | 206fcd4 | 2014-04-26 19:29:41 +0000 | [diff] [blame] | 11428 | Ops, LD->getMemoryVT(), LD->getMemOperand()); |
| Chris Lattner | a7976d3 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 11429 | |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11430 | // If this is an i16 load, insert the truncate. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11431 | SDValue ResVal = BSLoad; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11432 | if (N->getValueType(0) == MVT::i16) |
| 11433 | ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11434 | |
| Chris Lattner | a7976d3 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 11435 | // First, combine the bswap away. This makes the value produced by the |
| 11436 | // load dead. |
| 11437 | DCI.CombineTo(N, ResVal); |
| 11438 | |
| 11439 | // Next, combine the load away, we give it a bogus result value but a real |
| 11440 | // chain result. The result value is dead because the bswap is dead. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 11441 | DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11442 | |
| Chris Lattner | a7976d3 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 11443 | // Return N so it doesn't get rechecked! |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11444 | return SDValue(N, 0); |
| Chris Lattner | a7976d3 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 11445 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11446 | |
| Chris Lattner | 27f5345 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 11447 | break; |
| Chris Lattner | d4058a5 | 2006-03-31 06:02:07 +0000 | [diff] [blame] | 11448 | case PPCISD::VCMP: { |
| 11449 | // If a VCMPo node already exists with exactly the same operands as this |
| 11450 | // node, use its result instead of this node (VCMPo computes both a CR6 and |
| 11451 | // a normal output). |
| 11452 | // |
| 11453 | if (!N->getOperand(0).hasOneUse() && |
| 11454 | !N->getOperand(1).hasOneUse() && |
| 11455 | !N->getOperand(2).hasOneUse()) { |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11456 | |
| Chris Lattner | d4058a5 | 2006-03-31 06:02:07 +0000 | [diff] [blame] | 11457 | // Scan all of the users of the LHS, looking for VCMPo's that match. |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 11458 | SDNode *VCMPoNode = nullptr; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11459 | |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 11460 | SDNode *LHSN = N->getOperand(0).getNode(); |
| Chris Lattner | d4058a5 | 2006-03-31 06:02:07 +0000 | [diff] [blame] | 11461 | for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end(); |
| 11462 | UI != E; ++UI) |
| Dan Gohman | 91e5dcb | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 11463 | if (UI->getOpcode() == PPCISD::VCMPo && |
| 11464 | UI->getOperand(1) == N->getOperand(1) && |
| 11465 | UI->getOperand(2) == N->getOperand(2) && |
| 11466 | UI->getOperand(0) == N->getOperand(0)) { |
| 11467 | VCMPoNode = *UI; |
| Chris Lattner | d4058a5 | 2006-03-31 06:02:07 +0000 | [diff] [blame] | 11468 | break; |
| 11469 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11470 | |
| Chris Lattner | 518834c | 2006-04-18 18:28:22 +0000 | [diff] [blame] | 11471 | // If there is no VCMPo node, or if the flag value has a single use, don't |
| 11472 | // transform this. |
| 11473 | if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1)) |
| 11474 | break; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11475 | |
| 11476 | // Look at the (necessarily single) use of the flag value. If it has a |
| Chris Lattner | 518834c | 2006-04-18 18:28:22 +0000 | [diff] [blame] | 11477 | // chain, this transformation is more complex. Note that multiple things |
| 11478 | // could use the value result, which we should ignore. |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 11479 | SDNode *FlagUser = nullptr; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11480 | for (SDNode::use_iterator UI = VCMPoNode->use_begin(); |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 11481 | FlagUser == nullptr; ++UI) { |
| Chris Lattner | 518834c | 2006-04-18 18:28:22 +0000 | [diff] [blame] | 11482 | assert(UI != VCMPoNode->use_end() && "Didn't find user!"); |
| Dan Gohman | 91e5dcb | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 11483 | SDNode *User = *UI; |
| Chris Lattner | 518834c | 2006-04-18 18:28:22 +0000 | [diff] [blame] | 11484 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11485 | if (User->getOperand(i) == SDValue(VCMPoNode, 1)) { |
| Chris Lattner | 518834c | 2006-04-18 18:28:22 +0000 | [diff] [blame] | 11486 | FlagUser = User; |
| 11487 | break; |
| 11488 | } |
| 11489 | } |
| 11490 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11491 | |
| Ulrich Weigand | d5ebc62 | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 11492 | // If the user is a MFOCRF instruction, we know this is safe. |
| 11493 | // Otherwise we give up for right now. |
| 11494 | if (FlagUser->getOpcode() == PPCISD::MFOCRF) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11495 | return SDValue(VCMPoNode, 0); |
| Chris Lattner | d4058a5 | 2006-03-31 06:02:07 +0000 | [diff] [blame] | 11496 | } |
| 11497 | break; |
| 11498 | } |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 11499 | case ISD::BRCOND: { |
| 11500 | SDValue Cond = N->getOperand(1); |
| 11501 | SDValue Target = N->getOperand(2); |
| NAKAMURA Takumi | a9cb538 | 2015-09-22 11:14:39 +0000 | [diff] [blame] | 11502 | |
| Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 11503 | if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN && |
| 11504 | cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() == |
| 11505 | Intrinsic::ppc_is_decremented_ctr_nonzero) { |
| 11506 | |
| 11507 | // We now need to make the intrinsic dead (it cannot be instruction |
| 11508 | // selected). |
| 11509 | DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0)); |
| 11510 | assert(Cond.getNode()->hasOneUse() && |
| 11511 | "Counter decrement has more than one use"); |
| 11512 | |
| 11513 | return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other, |
| 11514 | N->getOperand(0), Target); |
| 11515 | } |
| 11516 | } |
| 11517 | break; |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 11518 | case ISD::BR_CC: { |
| 11519 | // If this is a branch on an altivec predicate comparison, lower this so |
| Ulrich Weigand | d5ebc62 | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 11520 | // that we don't have to do a MFOCRF: instead, branch directly on CR6. This |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 11521 | // lowering is done pre-legalize, because the legalizer lowers the predicate |
| 11522 | // compare down to code that is difficult to reassemble. |
| 11523 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11524 | SDValue LHS = N->getOperand(2), RHS = N->getOperand(3); |
| Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 11525 | |
| 11526 | // Sometimes the promoted value of the intrinsic is ANDed by some non-zero |
| 11527 | // value. If so, pass-through the AND to get to the intrinsic. |
| 11528 | if (LHS.getOpcode() == ISD::AND && |
| 11529 | LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN && |
| 11530 | cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() == |
| 11531 | Intrinsic::ppc_is_decremented_ctr_nonzero && |
| 11532 | isa<ConstantSDNode>(LHS.getOperand(1)) && |
| Artyom Skrobov | 314ee04 | 2015-11-25 19:41:11 +0000 | [diff] [blame] | 11533 | !isNullConstant(LHS.getOperand(1))) |
| Hal Finkel | 25c1992 | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 11534 | LHS = LHS.getOperand(0); |
| 11535 | |
| 11536 | if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN && |
| 11537 | cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == |
| 11538 | Intrinsic::ppc_is_decremented_ctr_nonzero && |
| 11539 | isa<ConstantSDNode>(RHS)) { |
| 11540 | assert((CC == ISD::SETEQ || CC == ISD::SETNE) && |
| 11541 | "Counter decrement comparison is not EQ or NE"); |
| 11542 | |
| 11543 | unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue(); |
| 11544 | bool isBDNZ = (CC == ISD::SETEQ && Val) || |
| 11545 | (CC == ISD::SETNE && !Val); |
| 11546 | |
| 11547 | // We now need to make the intrinsic dead (it cannot be instruction |
| 11548 | // selected). |
| 11549 | DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0)); |
| 11550 | assert(LHS.getNode()->hasOneUse() && |
| 11551 | "Counter decrement has more than one use"); |
| 11552 | |
| 11553 | return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other, |
| 11554 | N->getOperand(0), N->getOperand(4)); |
| 11555 | } |
| 11556 | |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 11557 | int CompareOpc; |
| 11558 | bool isDot; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11559 | |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 11560 | if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN && |
| 11561 | isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) && |
| Nemanja Ivanovic | 2c84b29 | 2015-09-29 17:41:53 +0000 | [diff] [blame] | 11562 | getVectorCompareInfo(LHS, CompareOpc, isDot, Subtarget)) { |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 11563 | assert(isDot && "Can't compare against a vector result!"); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11564 | |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 11565 | // If this is a comparison against something other than 0/1, then we know |
| 11566 | // that the condition is never/always true. |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 11567 | unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue(); |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 11568 | if (Val != 0 && Val != 1) { |
| 11569 | if (CC == ISD::SETEQ) // Cond never true, remove branch. |
| 11570 | return N->getOperand(0); |
| 11571 | // Always !=, turn it into an unconditional branch. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11572 | return DAG.getNode(ISD::BR, dl, MVT::Other, |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 11573 | N->getOperand(0), N->getOperand(4)); |
| 11574 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11575 | |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 11576 | bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11577 | |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 11578 | // Create the PPCISD altivec 'dot' comparison node. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11579 | SDValue Ops[] = { |
| Chris Lattner | d66f14e | 2006-08-11 17:18:05 +0000 | [diff] [blame] | 11580 | LHS.getOperand(2), // LHS of compare |
| 11581 | LHS.getOperand(3), // RHS of compare |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 11582 | DAG.getConstant(CompareOpc, dl, MVT::i32) |
| Chris Lattner | d66f14e | 2006-08-11 17:18:05 +0000 | [diff] [blame] | 11583 | }; |
| Benjamin Kramer | fdf362b | 2013-03-07 20:33:29 +0000 | [diff] [blame] | 11584 | EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue }; |
| Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 11585 | SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11586 | |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 11587 | // Unpack the result based on how the target uses it. |
| Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 11588 | PPC::Predicate CompOpc; |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 11589 | switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) { |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 11590 | default: // Can't happen, don't crash on invalid number though. |
| 11591 | case 0: // Branch on the value of the EQ bit of CR6. |
| Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 11592 | CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE; |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 11593 | break; |
| 11594 | case 1: // Branch on the inverted value of the EQ bit of CR6. |
| Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 11595 | CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ; |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 11596 | break; |
| 11597 | case 2: // Branch on the value of the LT bit of CR6. |
| Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 11598 | CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE; |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 11599 | break; |
| 11600 | case 3: // Branch on the inverted value of the LT bit of CR6. |
| Chris Lattner | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 11601 | CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT; |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 11602 | break; |
| 11603 | } |
| 11604 | |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11605 | return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0), |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 11606 | DAG.getConstant(CompOpc, dl, MVT::i32), |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11607 | DAG.getRegister(PPC::CR6, MVT::i32), |
| Chris Lattner | 9754d14 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 11608 | N->getOperand(4), CompNode.getValue(1)); |
| 11609 | } |
| 11610 | break; |
| 11611 | } |
| Nemanja Ivanovic | 44513e5 | 2016-07-05 09:22:29 +0000 | [diff] [blame] | 11612 | case ISD::BUILD_VECTOR: |
| 11613 | return DAGCombineBuildVector(N, DCI); |
| Chris Lattner | f418435 | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 11614 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11615 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11616 | return SDValue(); |
| Chris Lattner | f418435 | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 11617 | } |
| 11618 | |
| Hal Finkel | 13d104b | 2014-12-11 18:37:52 +0000 | [diff] [blame] | 11619 | SDValue |
| 11620 | PPCTargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor, |
| 11621 | SelectionDAG &DAG, |
| 11622 | std::vector<SDNode *> *Created) const { |
| 11623 | // fold (sdiv X, pow2) |
| 11624 | EVT VT = N->getValueType(0); |
| Hal Finkel | 04b16b5 | 2014-12-23 08:38:50 +0000 | [diff] [blame] | 11625 | if (VT == MVT::i64 && !Subtarget.isPPC64()) |
| 11626 | return SDValue(); |
| Hal Finkel | 13d104b | 2014-12-11 18:37:52 +0000 | [diff] [blame] | 11627 | if ((VT != MVT::i32 && VT != MVT::i64) || |
| 11628 | !(Divisor.isPowerOf2() || (-Divisor).isPowerOf2())) |
| 11629 | return SDValue(); |
| 11630 | |
| 11631 | SDLoc DL(N); |
| 11632 | SDValue N0 = N->getOperand(0); |
| 11633 | |
| 11634 | bool IsNegPow2 = (-Divisor).isPowerOf2(); |
| 11635 | unsigned Lg2 = (IsNegPow2 ? -Divisor : Divisor).countTrailingZeros(); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 11636 | SDValue ShiftAmt = DAG.getConstant(Lg2, DL, VT); |
| Hal Finkel | 13d104b | 2014-12-11 18:37:52 +0000 | [diff] [blame] | 11637 | |
| 11638 | SDValue Op = DAG.getNode(PPCISD::SRA_ADDZE, DL, VT, N0, ShiftAmt); |
| 11639 | if (Created) |
| 11640 | Created->push_back(Op.getNode()); |
| 11641 | |
| 11642 | if (IsNegPow2) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 11643 | Op = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Op); |
| Hal Finkel | 13d104b | 2014-12-11 18:37:52 +0000 | [diff] [blame] | 11644 | if (Created) |
| 11645 | Created->push_back(Op.getNode()); |
| 11646 | } |
| 11647 | |
| 11648 | return Op; |
| 11649 | } |
| 11650 | |
| Chris Lattner | 4211ca9 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 11651 | //===----------------------------------------------------------------------===// |
| 11652 | // Inline Assembly Support |
| 11653 | //===----------------------------------------------------------------------===// |
| 11654 | |
| Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 11655 | void PPCTargetLowering::computeKnownBitsForTargetNode(const SDValue Op, |
| 11656 | APInt &KnownZero, |
| 11657 | APInt &KnownOne, |
| 11658 | const SelectionDAG &DAG, |
| 11659 | unsigned Depth) const { |
| Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 11660 | KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0); |
| Chris Lattner | c5287c0 | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 11661 | switch (Op.getOpcode()) { |
| 11662 | default: break; |
| Chris Lattner | a7976d3 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 11663 | case PPCISD::LBRX: { |
| 11664 | // lhbrx is known to have the top bits cleared out. |
| Dan Gohman | a5fc035 | 2009-09-27 23:17:47 +0000 | [diff] [blame] | 11665 | if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16) |
| Chris Lattner | a7976d3 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 11666 | KnownZero = 0xFFFF0000; |
| 11667 | break; |
| 11668 | } |
| Chris Lattner | c5287c0 | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 11669 | case ISD::INTRINSIC_WO_CHAIN: { |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 11670 | switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) { |
| Chris Lattner | c5287c0 | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 11671 | default: break; |
| 11672 | case Intrinsic::ppc_altivec_vcmpbfp_p: |
| 11673 | case Intrinsic::ppc_altivec_vcmpeqfp_p: |
| 11674 | case Intrinsic::ppc_altivec_vcmpequb_p: |
| 11675 | case Intrinsic::ppc_altivec_vcmpequh_p: |
| 11676 | case Intrinsic::ppc_altivec_vcmpequw_p: |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 11677 | case Intrinsic::ppc_altivec_vcmpequd_p: |
| Chris Lattner | c5287c0 | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 11678 | case Intrinsic::ppc_altivec_vcmpgefp_p: |
| 11679 | case Intrinsic::ppc_altivec_vcmpgtfp_p: |
| 11680 | case Intrinsic::ppc_altivec_vcmpgtsb_p: |
| 11681 | case Intrinsic::ppc_altivec_vcmpgtsh_p: |
| 11682 | case Intrinsic::ppc_altivec_vcmpgtsw_p: |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 11683 | case Intrinsic::ppc_altivec_vcmpgtsd_p: |
| Chris Lattner | c5287c0 | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 11684 | case Intrinsic::ppc_altivec_vcmpgtub_p: |
| 11685 | case Intrinsic::ppc_altivec_vcmpgtuh_p: |
| 11686 | case Intrinsic::ppc_altivec_vcmpgtuw_p: |
| Kit Barton | 0cfa7b7 | 2015-03-03 19:55:45 +0000 | [diff] [blame] | 11687 | case Intrinsic::ppc_altivec_vcmpgtud_p: |
| Chris Lattner | c5287c0 | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 11688 | KnownZero = ~1U; // All bits but the low one are known to be zero. |
| 11689 | break; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11690 | } |
| Chris Lattner | c5287c0 | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 11691 | } |
| 11692 | } |
| 11693 | } |
| 11694 | |
| Hal Finkel | 5772566 | 2015-01-03 17:58:24 +0000 | [diff] [blame] | 11695 | unsigned PPCTargetLowering::getPrefLoopAlignment(MachineLoop *ML) const { |
| 11696 | switch (Subtarget.getDarwinDirective()) { |
| 11697 | default: break; |
| 11698 | case PPC::DIR_970: |
| 11699 | case PPC::DIR_PWR4: |
| 11700 | case PPC::DIR_PWR5: |
| 11701 | case PPC::DIR_PWR5X: |
| 11702 | case PPC::DIR_PWR6: |
| 11703 | case PPC::DIR_PWR6X: |
| 11704 | case PPC::DIR_PWR7: |
| Nemanja Ivanovic | 6e29baf | 2016-05-09 18:54:58 +0000 | [diff] [blame] | 11705 | case PPC::DIR_PWR8: |
| 11706 | case PPC::DIR_PWR9: { |
| Hal Finkel | 5772566 | 2015-01-03 17:58:24 +0000 | [diff] [blame] | 11707 | if (!ML) |
| 11708 | break; |
| 11709 | |
| Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 11710 | const PPCInstrInfo *TII = Subtarget.getInstrInfo(); |
| Hal Finkel | 5772566 | 2015-01-03 17:58:24 +0000 | [diff] [blame] | 11711 | |
| 11712 | // For small loops (between 5 and 8 instructions), align to a 32-byte |
| 11713 | // boundary so that the entire loop fits in one instruction-cache line. |
| 11714 | uint64_t LoopSize = 0; |
| 11715 | for (auto I = ML->block_begin(), IE = ML->block_end(); I != IE; ++I) |
| Chad Rosier | bc9d4f9 | 2015-12-14 14:44:06 +0000 | [diff] [blame] | 11716 | for (auto J = (*I)->begin(), JE = (*I)->end(); J != JE; ++J) { |
| Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 11717 | LoopSize += TII->getInstSizeInBytes(*J); |
| Chad Rosier | bc9d4f9 | 2015-12-14 14:44:06 +0000 | [diff] [blame] | 11718 | if (LoopSize > 32) |
| 11719 | break; |
| 11720 | } |
| Hal Finkel | 5772566 | 2015-01-03 17:58:24 +0000 | [diff] [blame] | 11721 | |
| 11722 | if (LoopSize > 16 && LoopSize <= 32) |
| 11723 | return 5; |
| 11724 | |
| 11725 | break; |
| 11726 | } |
| 11727 | } |
| 11728 | |
| 11729 | return TargetLowering::getPrefLoopAlignment(ML); |
| 11730 | } |
| Chris Lattner | c5287c0 | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 11731 | |
| Chris Lattner | d685514 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 11732 | /// getConstraintType - Given a constraint, return the type of |
| Chris Lattner | 203b2f1 | 2006-02-07 20:16:30 +0000 | [diff] [blame] | 11733 | /// constraint it is for this target. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11734 | PPCTargetLowering::ConstraintType |
| Benjamin Kramer | 9bfb627 | 2015-07-05 19:29:18 +0000 | [diff] [blame] | 11735 | PPCTargetLowering::getConstraintType(StringRef Constraint) const { |
| Chris Lattner | d685514 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 11736 | if (Constraint.size() == 1) { |
| 11737 | switch (Constraint[0]) { |
| 11738 | default: break; |
| 11739 | case 'b': |
| 11740 | case 'r': |
| 11741 | case 'f': |
| Eric Christopher | b979d51 | 2016-03-24 21:04:52 +0000 | [diff] [blame] | 11742 | case 'd': |
| Chris Lattner | d685514 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 11743 | case 'v': |
| 11744 | case 'y': |
| 11745 | return C_RegisterClass; |
| Hal Finkel | 4f24c62 | 2012-11-05 18:18:42 +0000 | [diff] [blame] | 11746 | case 'Z': |
| 11747 | // FIXME: While Z does indicate a memory constraint, it specifically |
| 11748 | // indicates an r+r address (used in conjunction with the 'y' modifier |
| 11749 | // in the replacement string). Currently, we're forcing the base |
| 11750 | // register to be r0 in the asm printer (which is interpreted as zero) |
| 11751 | // and forming the complete address in the second register. This is |
| 11752 | // suboptimal. |
| 11753 | return C_Memory; |
| Chris Lattner | d685514 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 11754 | } |
| Hal Finkel | 6aca237 | 2014-03-02 18:23:39 +0000 | [diff] [blame] | 11755 | } else if (Constraint == "wc") { // individual CR bits. |
| 11756 | return C_RegisterClass; |
| Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 11757 | } else if (Constraint == "wa" || Constraint == "wd" || |
| 11758 | Constraint == "wf" || Constraint == "ws") { |
| 11759 | return C_RegisterClass; // VSX registers. |
| Chris Lattner | d685514 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 11760 | } |
| 11761 | return TargetLowering::getConstraintType(Constraint); |
| Chris Lattner | 203b2f1 | 2006-02-07 20:16:30 +0000 | [diff] [blame] | 11762 | } |
| 11763 | |
| John Thompson | e8360b7 | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 11764 | /// Examine constraint type and operand type and determine a weight value. |
| 11765 | /// This object must already have been set up with the operand type |
| 11766 | /// and the current alternative constraint selected. |
| 11767 | TargetLowering::ConstraintWeight |
| 11768 | PPCTargetLowering::getSingleConstraintMatchWeight( |
| 11769 | AsmOperandInfo &info, const char *constraint) const { |
| 11770 | ConstraintWeight weight = CW_Invalid; |
| 11771 | Value *CallOperandVal = info.CallOperandVal; |
| 11772 | // If we don't have a value, we can't do a match, |
| 11773 | // but allow it at the lowest weight. |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 11774 | if (!CallOperandVal) |
| John Thompson | e8360b7 | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 11775 | return CW_Default; |
| Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 11776 | Type *type = CallOperandVal->getType(); |
| Hal Finkel | 6aca237 | 2014-03-02 18:23:39 +0000 | [diff] [blame] | 11777 | |
| John Thompson | e8360b7 | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 11778 | // Look at the constraint type. |
| Hal Finkel | 6aca237 | 2014-03-02 18:23:39 +0000 | [diff] [blame] | 11779 | if (StringRef(constraint) == "wc" && type->isIntegerTy(1)) |
| 11780 | return CW_Register; // an individual CR bit. |
| Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 11781 | else if ((StringRef(constraint) == "wa" || |
| 11782 | StringRef(constraint) == "wd" || |
| 11783 | StringRef(constraint) == "wf") && |
| 11784 | type->isVectorTy()) |
| 11785 | return CW_Register; |
| 11786 | else if (StringRef(constraint) == "ws" && type->isDoubleTy()) |
| 11787 | return CW_Register; |
| Hal Finkel | 6aca237 | 2014-03-02 18:23:39 +0000 | [diff] [blame] | 11788 | |
| John Thompson | e8360b7 | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 11789 | switch (*constraint) { |
| 11790 | default: |
| 11791 | weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); |
| 11792 | break; |
| 11793 | case 'b': |
| 11794 | if (type->isIntegerTy()) |
| 11795 | weight = CW_Register; |
| 11796 | break; |
| 11797 | case 'f': |
| 11798 | if (type->isFloatTy()) |
| 11799 | weight = CW_Register; |
| 11800 | break; |
| 11801 | case 'd': |
| 11802 | if (type->isDoubleTy()) |
| 11803 | weight = CW_Register; |
| 11804 | break; |
| 11805 | case 'v': |
| 11806 | if (type->isVectorTy()) |
| 11807 | weight = CW_Register; |
| 11808 | break; |
| 11809 | case 'y': |
| 11810 | weight = CW_Register; |
| 11811 | break; |
| Hal Finkel | 4f24c62 | 2012-11-05 18:18:42 +0000 | [diff] [blame] | 11812 | case 'Z': |
| 11813 | weight = CW_Memory; |
| 11814 | break; |
| John Thompson | e8360b7 | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 11815 | } |
| 11816 | return weight; |
| 11817 | } |
| 11818 | |
| Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 11819 | std::pair<unsigned, const TargetRegisterClass *> |
| 11820 | PPCTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, |
| Benjamin Kramer | 9bfb627 | 2015-07-05 19:29:18 +0000 | [diff] [blame] | 11821 | StringRef Constraint, |
| Chad Rosier | 295bd43 | 2013-06-22 18:37:38 +0000 | [diff] [blame] | 11822 | MVT VT) const { |
| Chris Lattner | 0151361 | 2006-01-31 19:20:21 +0000 | [diff] [blame] | 11823 | if (Constraint.size() == 1) { |
| Chris Lattner | 584a11a | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 11824 | // GCC RS6000 Constraint Letters |
| 11825 | switch (Constraint[0]) { |
| 11826 | case 'b': // R1-R31 |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 11827 | if (VT == MVT::i64 && Subtarget.isPPC64()) |
| Hal Finkel | 638a9fa | 2013-03-19 18:51:05 +0000 | [diff] [blame] | 11828 | return std::make_pair(0U, &PPC::G8RC_NOX0RegClass); |
| 11829 | return std::make_pair(0U, &PPC::GPRC_NOR0RegClass); |
| Chris Lattner | 584a11a | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 11830 | case 'r': // R0-R31 |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 11831 | if (VT == MVT::i64 && Subtarget.isPPC64()) |
| Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 11832 | return std::make_pair(0U, &PPC::G8RCRegClass); |
| 11833 | return std::make_pair(0U, &PPC::GPRCRegClass); |
| Eric Christopher | b979d51 | 2016-03-24 21:04:52 +0000 | [diff] [blame] | 11834 | // 'd' and 'f' constraints are both defined to be "the floating point |
| 11835 | // registers", where one is for 32-bit and the other for 64-bit. We don't |
| 11836 | // really care overly much here so just give them all the same reg classes. |
| 11837 | case 'd': |
| Chris Lattner | 584a11a | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 11838 | case 'f': |
| Ulrich Weigand | 0de4a1e | 2012-10-29 17:49:34 +0000 | [diff] [blame] | 11839 | if (VT == MVT::f32 || VT == MVT::i32) |
| Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 11840 | return std::make_pair(0U, &PPC::F4RCRegClass); |
| Ulrich Weigand | 0de4a1e | 2012-10-29 17:49:34 +0000 | [diff] [blame] | 11841 | if (VT == MVT::f64 || VT == MVT::i64) |
| Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 11842 | return std::make_pair(0U, &PPC::F8RCRegClass); |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11843 | if (VT == MVT::v4f64 && Subtarget.hasQPX()) |
| 11844 | return std::make_pair(0U, &PPC::QFRCRegClass); |
| 11845 | if (VT == MVT::v4f32 && Subtarget.hasQPX()) |
| 11846 | return std::make_pair(0U, &PPC::QSRCRegClass); |
| Chris Lattner | 584a11a | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 11847 | break; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11848 | case 'v': |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11849 | if (VT == MVT::v4f64 && Subtarget.hasQPX()) |
| 11850 | return std::make_pair(0U, &PPC::QFRCRegClass); |
| 11851 | if (VT == MVT::v4f32 && Subtarget.hasQPX()) |
| 11852 | return std::make_pair(0U, &PPC::QSRCRegClass); |
| Hal Finkel | bdd292a | 2015-10-28 23:03:45 +0000 | [diff] [blame] | 11853 | if (Subtarget.hasAltivec()) |
| 11854 | return std::make_pair(0U, &PPC::VRRCRegClass); |
| Chris Lattner | 584a11a | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 11855 | case 'y': // crrc |
| Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 11856 | return std::make_pair(0U, &PPC::CRRCRegClass); |
| Chris Lattner | 0151361 | 2006-01-31 19:20:21 +0000 | [diff] [blame] | 11857 | } |
| Hal Finkel | 34d4149 | 2015-10-28 22:25:52 +0000 | [diff] [blame] | 11858 | } else if (Constraint == "wc" && Subtarget.useCRBits()) { |
| 11859 | // An individual CR bit. |
| Hal Finkel | 6aca237 | 2014-03-02 18:23:39 +0000 | [diff] [blame] | 11860 | return std::make_pair(0U, &PPC::CRBITRCRegClass); |
| Hal Finkel | bdd292a | 2015-10-28 23:03:45 +0000 | [diff] [blame] | 11861 | } else if ((Constraint == "wa" || Constraint == "wd" || |
| 11862 | Constraint == "wf") && Subtarget.hasVSX()) { |
| Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 11863 | return std::make_pair(0U, &PPC::VSRCRegClass); |
| Hal Finkel | bdd292a | 2015-10-28 23:03:45 +0000 | [diff] [blame] | 11864 | } else if (Constraint == "ws" && Subtarget.hasVSX()) { |
| 11865 | if (VT == MVT::f32 && Subtarget.hasP8Vector()) |
| Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 11866 | return std::make_pair(0U, &PPC::VSSRCRegClass); |
| 11867 | else |
| 11868 | return std::make_pair(0U, &PPC::VSFRCRegClass); |
| Chris Lattner | 0151361 | 2006-01-31 19:20:21 +0000 | [diff] [blame] | 11869 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11870 | |
| Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 11871 | std::pair<unsigned, const TargetRegisterClass *> R = |
| 11872 | TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); |
| Hal Finkel | b176acb | 2013-08-03 12:25:10 +0000 | [diff] [blame] | 11873 | |
| 11874 | // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers |
| 11875 | // (which we call X[0-9]+). If a 64-bit value has been requested, and a |
| 11876 | // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent |
| 11877 | // register. |
| 11878 | // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use |
| 11879 | // the AsmName field from *RegisterInfo.td, then this would not be necessary. |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 11880 | if (R.first && VT == MVT::i64 && Subtarget.isPPC64() && |
| Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 11881 | PPC::GPRCRegClass.contains(R.first)) |
| Hal Finkel | b176acb | 2013-08-03 12:25:10 +0000 | [diff] [blame] | 11882 | return std::make_pair(TRI->getMatchingSuperReg(R.first, |
| Hal Finkel | b3ca00d | 2013-08-14 20:05:04 +0000 | [diff] [blame] | 11883 | PPC::sub_32, &PPC::G8RCRegClass), |
| Hal Finkel | b176acb | 2013-08-03 12:25:10 +0000 | [diff] [blame] | 11884 | &PPC::G8RCRegClass); |
| Hal Finkel | b176acb | 2013-08-03 12:25:10 +0000 | [diff] [blame] | 11885 | |
| Hal Finkel | aa10b3c | 2014-12-08 22:54:22 +0000 | [diff] [blame] | 11886 | // GCC accepts 'cc' as an alias for 'cr0', and we need to do the same. |
| 11887 | if (!R.second && StringRef("{cc}").equals_lower(Constraint)) { |
| 11888 | R.first = PPC::CR0; |
| 11889 | R.second = &PPC::CRRCRegClass; |
| 11890 | } |
| 11891 | |
| Hal Finkel | b176acb | 2013-08-03 12:25:10 +0000 | [diff] [blame] | 11892 | return R; |
| Chris Lattner | 0151361 | 2006-01-31 19:20:21 +0000 | [diff] [blame] | 11893 | } |
| Chris Lattner | 15a6c4c | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 11894 | |
| Chris Lattner | d8c9cb9 | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 11895 | /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops |
| Dale Johannesen | ce97d55 | 2010-06-25 21:55:36 +0000 | [diff] [blame] | 11896 | /// vector. If it is invalid, don't add anything to Ops. |
| Eric Christopher | 0713a9d | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 11897 | void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op, |
| Eric Christopher | de9399b | 2011-06-02 23:16:42 +0000 | [diff] [blame] | 11898 | std::string &Constraint, |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11899 | std::vector<SDValue>&Ops, |
| Chris Lattner | 724539c | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 11900 | SelectionDAG &DAG) const { |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 11901 | SDValue Result; |
| Eric Christopher | 0713a9d | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 11902 | |
| Eric Christopher | de9399b | 2011-06-02 23:16:42 +0000 | [diff] [blame] | 11903 | // Only support length 1 constraints. |
| 11904 | if (Constraint.length() > 1) return; |
| Eric Christopher | 0713a9d | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 11905 | |
| Eric Christopher | de9399b | 2011-06-02 23:16:42 +0000 | [diff] [blame] | 11906 | char Letter = Constraint[0]; |
| Chris Lattner | 15a6c4c | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 11907 | switch (Letter) { |
| 11908 | default: break; |
| 11909 | case 'I': |
| 11910 | case 'J': |
| 11911 | case 'K': |
| 11912 | case 'L': |
| 11913 | case 'M': |
| 11914 | case 'N': |
| 11915 | case 'O': |
| 11916 | case 'P': { |
| Chris Lattner | 0b7472d | 2007-05-15 01:31:05 +0000 | [diff] [blame] | 11917 | ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op); |
| Chris Lattner | d8c9cb9 | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 11918 | if (!CST) return; // Must be an immediate to match. |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 11919 | SDLoc dl(Op); |
| Hal Finkel | c91fc11 | 2014-12-03 09:37:50 +0000 | [diff] [blame] | 11920 | int64_t Value = CST->getSExtValue(); |
| 11921 | EVT TCVT = MVT::i64; // All constants taken to be 64 bits so that negative |
| 11922 | // numbers are printed as such. |
| Chris Lattner | 15a6c4c | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 11923 | switch (Letter) { |
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 11924 | default: llvm_unreachable("Unknown constraint letter!"); |
| Chris Lattner | 15a6c4c | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 11925 | case 'I': // "I" is a signed 16-bit constant. |
| Hal Finkel | c91fc11 | 2014-12-03 09:37:50 +0000 | [diff] [blame] | 11926 | if (isInt<16>(Value)) |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 11927 | Result = DAG.getTargetConstant(Value, dl, TCVT); |
| Chris Lattner | 8c6949e | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 11928 | break; |
| Chris Lattner | 15a6c4c | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 11929 | case 'J': // "J" is a constant with only the high-order 16 bits nonzero. |
| Hal Finkel | c91fc11 | 2014-12-03 09:37:50 +0000 | [diff] [blame] | 11930 | if (isShiftedUInt<16, 16>(Value)) |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 11931 | Result = DAG.getTargetConstant(Value, dl, TCVT); |
| Hal Finkel | c91fc11 | 2014-12-03 09:37:50 +0000 | [diff] [blame] | 11932 | break; |
| Chris Lattner | 15a6c4c | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 11933 | case 'L': // "L" is a signed 16-bit constant shifted left 16 bits. |
| Hal Finkel | c91fc11 | 2014-12-03 09:37:50 +0000 | [diff] [blame] | 11934 | if (isShiftedInt<16, 16>(Value)) |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 11935 | Result = DAG.getTargetConstant(Value, dl, TCVT); |
| Chris Lattner | 8c6949e | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 11936 | break; |
| Chris Lattner | 15a6c4c | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 11937 | case 'K': // "K" is a constant with only the low-order 16 bits nonzero. |
| Hal Finkel | c91fc11 | 2014-12-03 09:37:50 +0000 | [diff] [blame] | 11938 | if (isUInt<16>(Value)) |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 11939 | Result = DAG.getTargetConstant(Value, dl, TCVT); |
| Chris Lattner | 8c6949e | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 11940 | break; |
| Chris Lattner | 15a6c4c | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 11941 | case 'M': // "M" is a constant that is greater than 31. |
| Chris Lattner | 0b7472d | 2007-05-15 01:31:05 +0000 | [diff] [blame] | 11942 | if (Value > 31) |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 11943 | Result = DAG.getTargetConstant(Value, dl, TCVT); |
| Chris Lattner | 8c6949e | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 11944 | break; |
| Chris Lattner | 15a6c4c | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 11945 | case 'N': // "N" is a positive constant that is an exact power of two. |
| Hal Finkel | c91fc11 | 2014-12-03 09:37:50 +0000 | [diff] [blame] | 11946 | if (Value > 0 && isPowerOf2_64(Value)) |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 11947 | Result = DAG.getTargetConstant(Value, dl, TCVT); |
| Chris Lattner | 8c6949e | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 11948 | break; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11949 | case 'O': // "O" is the constant zero. |
| Chris Lattner | 0b7472d | 2007-05-15 01:31:05 +0000 | [diff] [blame] | 11950 | if (Value == 0) |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 11951 | Result = DAG.getTargetConstant(Value, dl, TCVT); |
| Chris Lattner | 8c6949e | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 11952 | break; |
| Chris Lattner | 15a6c4c | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 11953 | case 'P': // "P" is a constant whose negation is a signed 16-bit constant. |
| Hal Finkel | c91fc11 | 2014-12-03 09:37:50 +0000 | [diff] [blame] | 11954 | if (isInt<16>(-Value)) |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 11955 | Result = DAG.getTargetConstant(Value, dl, TCVT); |
| Chris Lattner | 8c6949e | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 11956 | break; |
| Chris Lattner | 15a6c4c | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 11957 | } |
| 11958 | break; |
| 11959 | } |
| 11960 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11961 | |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 11962 | if (Result.getNode()) { |
| Chris Lattner | d8c9cb9 | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 11963 | Ops.push_back(Result); |
| 11964 | return; |
| 11965 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11966 | |
| Chris Lattner | 15a6c4c | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 11967 | // Handle standard constraint letters. |
| Eric Christopher | de9399b | 2011-06-02 23:16:42 +0000 | [diff] [blame] | 11968 | TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); |
| Chris Lattner | 15a6c4c | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 11969 | } |
| Evan Cheng | 2dd2c65 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 11970 | |
| Chris Lattner | 1eb94d9 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 11971 | // isLegalAddressingMode - Return true if the addressing mode represented |
| 11972 | // by AM is legal for this target, for a load/store of the specified type. |
| Mehdi Amini | 0cdec1e | 2015-07-09 02:09:40 +0000 | [diff] [blame] | 11973 | bool PPCTargetLowering::isLegalAddressingMode(const DataLayout &DL, |
| 11974 | const AddrMode &AM, Type *Ty, |
| Matt Arsenault | bd7d80a | 2015-06-01 05:31:59 +0000 | [diff] [blame] | 11975 | unsigned AS) const { |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 11976 | // PPC does not allow r+i addressing modes for vectors! |
| 11977 | if (Ty->isVectorTy() && AM.BaseOffs != 0) |
| 11978 | return false; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11979 | |
| Chris Lattner | 1eb94d9 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 11980 | // PPC allows a sign-extended 16-bit immediate field. |
| 11981 | if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1) |
| 11982 | return false; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11983 | |
| Chris Lattner | 1eb94d9 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 11984 | // No global is ever allowed as a base. |
| 11985 | if (AM.BaseGV) |
| 11986 | return false; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11987 | |
| 11988 | // PPC only support r+r, |
| Chris Lattner | 1eb94d9 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 11989 | switch (AM.Scale) { |
| 11990 | case 0: // "r+i" or just "i", depending on HasBaseReg. |
| 11991 | break; |
| 11992 | case 1: |
| 11993 | if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed. |
| 11994 | return false; |
| 11995 | // Otherwise we have r+r or r+i. |
| 11996 | break; |
| 11997 | case 2: |
| 11998 | if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed. |
| 11999 | return false; |
| 12000 | // Allow 2*r as r+r. |
| 12001 | break; |
| Chris Lattner | 19ccd62 | 2007-04-09 22:10:05 +0000 | [diff] [blame] | 12002 | default: |
| 12003 | // No other scales are supported. |
| 12004 | return false; |
| Chris Lattner | 1eb94d9 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 12005 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 12006 | |
| Chris Lattner | 1eb94d9 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 12007 | return true; |
| 12008 | } |
| 12009 | |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 12010 | SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op, |
| 12011 | SelectionDAG &DAG) const { |
| Evan Cheng | 168ced9 | 2010-05-22 01:47:14 +0000 | [diff] [blame] | 12012 | MachineFunction &MF = DAG.getMachineFunction(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 12013 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 12014 | MFI.setReturnAddressIsTaken(true); |
| Evan Cheng | 168ced9 | 2010-05-22 01:47:14 +0000 | [diff] [blame] | 12015 | |
| Bill Wendling | 908bf81 | 2014-01-06 00:43:20 +0000 | [diff] [blame] | 12016 | if (verifyReturnAddressArgumentIsConstant(Op, DAG)) |
| Bill Wendling | df7dd28 | 2014-01-05 01:47:20 +0000 | [diff] [blame] | 12017 | return SDValue(); |
| Bill Wendling | df7dd28 | 2014-01-05 01:47:20 +0000 | [diff] [blame] | 12018 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 12019 | SDLoc dl(Op); |
| Dale Johannesen | 81bfca7 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 12020 | unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| Chris Lattner | f6a8156 | 2007-12-08 06:59:59 +0000 | [diff] [blame] | 12021 | |
| Dale Johannesen | 81bfca7 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 12022 | // Make sure the function does not optimize away the store of the RA to |
| 12023 | // the stack. |
| Chris Lattner | f6a8156 | 2007-12-08 06:59:59 +0000 | [diff] [blame] | 12024 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| Dale Johannesen | 81bfca7 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 12025 | FuncInfo->setLRStoreRequired(); |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 12026 | bool isPPC64 = Subtarget.isPPC64(); |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 12027 | auto PtrVT = getPointerTy(MF.getDataLayout()); |
| Dale Johannesen | 81bfca7 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 12028 | |
| 12029 | if (Depth > 0) { |
| 12030 | SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); |
| 12031 | SDValue Offset = |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 12032 | DAG.getConstant(Subtarget.getFrameLowering()->getReturnSaveOffset(), dl, |
| Eric Christopher | f71609b | 2015-02-13 00:39:27 +0000 | [diff] [blame] | 12033 | isPPC64 ? MVT::i64 : MVT::i32); |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 12034 | return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), |
| 12035 | DAG.getNode(ISD::ADD, dl, PtrVT, FrameAddr, Offset), |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 12036 | MachinePointerInfo()); |
| Dale Johannesen | 81bfca7 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 12037 | } |
| Chris Lattner | f6a8156 | 2007-12-08 06:59:59 +0000 | [diff] [blame] | 12038 | |
| Chris Lattner | f6a8156 | 2007-12-08 06:59:59 +0000 | [diff] [blame] | 12039 | // Just load the return address off the stack. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 12040 | SDValue RetAddrFI = getReturnAddrFrameIndex(DAG); |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 12041 | return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), RetAddrFI, |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 12042 | MachinePointerInfo()); |
| Chris Lattner | f6a8156 | 2007-12-08 06:59:59 +0000 | [diff] [blame] | 12043 | } |
| 12044 | |
| Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 12045 | SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op, |
| 12046 | SelectionDAG &DAG) const { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 12047 | SDLoc dl(Op); |
| Dale Johannesen | 81bfca7 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 12048 | unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 12049 | |
| Nicolas Geoffray | 75ab979 | 2007-03-01 13:11:38 +0000 | [diff] [blame] | 12050 | MachineFunction &MF = DAG.getMachineFunction(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 12051 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 12052 | MFI.setFrameAddressIsTaken(true); |
| Hal Finkel | aa03c03 | 2013-03-21 19:03:19 +0000 | [diff] [blame] | 12053 | |
| Eric Christopher | cd71946 | 2016-07-07 01:49:59 +0000 | [diff] [blame] | 12054 | EVT PtrVT = getPointerTy(MF.getDataLayout()); |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 12055 | bool isPPC64 = PtrVT == MVT::i64; |
| 12056 | |
| Hal Finkel | aa03c03 | 2013-03-21 19:03:19 +0000 | [diff] [blame] | 12057 | // Naked functions never have a frame pointer, and so we use r1. For all |
| 12058 | // other functions, this decision must be delayed until during PEI. |
| 12059 | unsigned FrameReg; |
| Duncan P. N. Exon Smith | 5bedaf93 | 2015-02-14 02:54:07 +0000 | [diff] [blame] | 12060 | if (MF.getFunction()->hasFnAttribute(Attribute::Naked)) |
| Hal Finkel | aa03c03 | 2013-03-21 19:03:19 +0000 | [diff] [blame] | 12061 | FrameReg = isPPC64 ? PPC::X1 : PPC::R1; |
| 12062 | else |
| 12063 | FrameReg = isPPC64 ? PPC::FP8 : PPC::FP; |
| 12064 | |
| Dale Johannesen | 81bfca7 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 12065 | SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, |
| 12066 | PtrVT); |
| 12067 | while (Depth--) |
| 12068 | FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(), |
| Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 12069 | FrameAddr, MachinePointerInfo()); |
| Dale Johannesen | 81bfca7 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 12070 | return FrameAddr; |
| Nicolas Geoffray | 75ab979 | 2007-03-01 13:11:38 +0000 | [diff] [blame] | 12071 | } |
| Dan Gohman | c14e522 | 2008-10-21 03:41:46 +0000 | [diff] [blame] | 12072 | |
| Hal Finkel | 0d8db46 | 2014-05-11 19:29:11 +0000 | [diff] [blame] | 12073 | // FIXME? Maybe this could be a TableGen attribute on some registers and |
| 12074 | // this table could be generated automatically from RegInfo. |
| Pat Gavlin | a717f25 | 2015-07-09 17:40:29 +0000 | [diff] [blame] | 12075 | unsigned PPCTargetLowering::getRegisterByName(const char* RegName, EVT VT, |
| 12076 | SelectionDAG &DAG) const { |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 12077 | bool isPPC64 = Subtarget.isPPC64(); |
| 12078 | bool isDarwinABI = Subtarget.isDarwinABI(); |
| Hal Finkel | 0d8db46 | 2014-05-11 19:29:11 +0000 | [diff] [blame] | 12079 | |
| 12080 | if ((isPPC64 && VT != MVT::i64 && VT != MVT::i32) || |
| 12081 | (!isPPC64 && VT != MVT::i32)) |
| 12082 | report_fatal_error("Invalid register global variable type"); |
| 12083 | |
| 12084 | bool is64Bit = isPPC64 && VT == MVT::i64; |
| 12085 | unsigned Reg = StringSwitch<unsigned>(RegName) |
| 12086 | .Case("r1", is64Bit ? PPC::X1 : PPC::R1) |
| Hal Finkel | e6698d5 | 2015-02-01 15:03:28 +0000 | [diff] [blame] | 12087 | .Case("r2", (isDarwinABI || isPPC64) ? 0 : PPC::R2) |
| Hal Finkel | 0d8db46 | 2014-05-11 19:29:11 +0000 | [diff] [blame] | 12088 | .Case("r13", (!isPPC64 && isDarwinABI) ? 0 : |
| 12089 | (is64Bit ? PPC::X13 : PPC::R13)) |
| 12090 | .Default(0); |
| 12091 | |
| 12092 | if (Reg) |
| 12093 | return Reg; |
| 12094 | report_fatal_error("Invalid register name global variable"); |
| 12095 | } |
| 12096 | |
| Dan Gohman | c14e522 | 2008-10-21 03:41:46 +0000 | [diff] [blame] | 12097 | bool |
| 12098 | PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { |
| 12099 | // The PowerPC target isn't yet aware of offsets. |
| 12100 | return false; |
| 12101 | } |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 12102 | |
| Hal Finkel | 46ef7ce | 2014-08-13 01:15:40 +0000 | [diff] [blame] | 12103 | bool PPCTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, |
| 12104 | const CallInst &I, |
| 12105 | unsigned Intrinsic) const { |
| 12106 | |
| 12107 | switch (Intrinsic) { |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 12108 | case Intrinsic::ppc_qpx_qvlfd: |
| 12109 | case Intrinsic::ppc_qpx_qvlfs: |
| 12110 | case Intrinsic::ppc_qpx_qvlfcd: |
| 12111 | case Intrinsic::ppc_qpx_qvlfcs: |
| 12112 | case Intrinsic::ppc_qpx_qvlfiwa: |
| 12113 | case Intrinsic::ppc_qpx_qvlfiwz: |
| Hal Finkel | 46ef7ce | 2014-08-13 01:15:40 +0000 | [diff] [blame] | 12114 | case Intrinsic::ppc_altivec_lvx: |
| 12115 | case Intrinsic::ppc_altivec_lvxl: |
| 12116 | case Intrinsic::ppc_altivec_lvebx: |
| 12117 | case Intrinsic::ppc_altivec_lvehx: |
| Bill Schmidt | 7295478 | 2014-11-12 04:19:40 +0000 | [diff] [blame] | 12118 | case Intrinsic::ppc_altivec_lvewx: |
| 12119 | case Intrinsic::ppc_vsx_lxvd2x: |
| 12120 | case Intrinsic::ppc_vsx_lxvw4x: { |
| Hal Finkel | 46ef7ce | 2014-08-13 01:15:40 +0000 | [diff] [blame] | 12121 | EVT VT; |
| 12122 | switch (Intrinsic) { |
| 12123 | case Intrinsic::ppc_altivec_lvebx: |
| 12124 | VT = MVT::i8; |
| 12125 | break; |
| 12126 | case Intrinsic::ppc_altivec_lvehx: |
| 12127 | VT = MVT::i16; |
| 12128 | break; |
| 12129 | case Intrinsic::ppc_altivec_lvewx: |
| 12130 | VT = MVT::i32; |
| 12131 | break; |
| Bill Schmidt | 7295478 | 2014-11-12 04:19:40 +0000 | [diff] [blame] | 12132 | case Intrinsic::ppc_vsx_lxvd2x: |
| 12133 | VT = MVT::v2f64; |
| 12134 | break; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 12135 | case Intrinsic::ppc_qpx_qvlfd: |
| 12136 | VT = MVT::v4f64; |
| 12137 | break; |
| 12138 | case Intrinsic::ppc_qpx_qvlfs: |
| 12139 | VT = MVT::v4f32; |
| 12140 | break; |
| 12141 | case Intrinsic::ppc_qpx_qvlfcd: |
| 12142 | VT = MVT::v2f64; |
| 12143 | break; |
| 12144 | case Intrinsic::ppc_qpx_qvlfcs: |
| 12145 | VT = MVT::v2f32; |
| 12146 | break; |
| Hal Finkel | 46ef7ce | 2014-08-13 01:15:40 +0000 | [diff] [blame] | 12147 | default: |
| 12148 | VT = MVT::v4i32; |
| 12149 | break; |
| 12150 | } |
| 12151 | |
| 12152 | Info.opc = ISD::INTRINSIC_W_CHAIN; |
| 12153 | Info.memVT = VT; |
| 12154 | Info.ptrVal = I.getArgOperand(0); |
| 12155 | Info.offset = -VT.getStoreSize()+1; |
| 12156 | Info.size = 2*VT.getStoreSize()-1; |
| 12157 | Info.align = 1; |
| 12158 | Info.vol = false; |
| 12159 | Info.readMem = true; |
| 12160 | Info.writeMem = false; |
| 12161 | return true; |
| 12162 | } |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 12163 | case Intrinsic::ppc_qpx_qvlfda: |
| 12164 | case Intrinsic::ppc_qpx_qvlfsa: |
| 12165 | case Intrinsic::ppc_qpx_qvlfcda: |
| 12166 | case Intrinsic::ppc_qpx_qvlfcsa: |
| 12167 | case Intrinsic::ppc_qpx_qvlfiwaa: |
| 12168 | case Intrinsic::ppc_qpx_qvlfiwza: { |
| 12169 | EVT VT; |
| 12170 | switch (Intrinsic) { |
| 12171 | case Intrinsic::ppc_qpx_qvlfda: |
| 12172 | VT = MVT::v4f64; |
| 12173 | break; |
| 12174 | case Intrinsic::ppc_qpx_qvlfsa: |
| 12175 | VT = MVT::v4f32; |
| 12176 | break; |
| 12177 | case Intrinsic::ppc_qpx_qvlfcda: |
| 12178 | VT = MVT::v2f64; |
| 12179 | break; |
| 12180 | case Intrinsic::ppc_qpx_qvlfcsa: |
| 12181 | VT = MVT::v2f32; |
| 12182 | break; |
| 12183 | default: |
| 12184 | VT = MVT::v4i32; |
| 12185 | break; |
| 12186 | } |
| 12187 | |
| 12188 | Info.opc = ISD::INTRINSIC_W_CHAIN; |
| 12189 | Info.memVT = VT; |
| 12190 | Info.ptrVal = I.getArgOperand(0); |
| 12191 | Info.offset = 0; |
| 12192 | Info.size = VT.getStoreSize(); |
| 12193 | Info.align = 1; |
| 12194 | Info.vol = false; |
| 12195 | Info.readMem = true; |
| 12196 | Info.writeMem = false; |
| 12197 | return true; |
| 12198 | } |
| 12199 | case Intrinsic::ppc_qpx_qvstfd: |
| 12200 | case Intrinsic::ppc_qpx_qvstfs: |
| 12201 | case Intrinsic::ppc_qpx_qvstfcd: |
| 12202 | case Intrinsic::ppc_qpx_qvstfcs: |
| 12203 | case Intrinsic::ppc_qpx_qvstfiw: |
| Hal Finkel | 46ef7ce | 2014-08-13 01:15:40 +0000 | [diff] [blame] | 12204 | case Intrinsic::ppc_altivec_stvx: |
| 12205 | case Intrinsic::ppc_altivec_stvxl: |
| 12206 | case Intrinsic::ppc_altivec_stvebx: |
| 12207 | case Intrinsic::ppc_altivec_stvehx: |
| Bill Schmidt | 7295478 | 2014-11-12 04:19:40 +0000 | [diff] [blame] | 12208 | case Intrinsic::ppc_altivec_stvewx: |
| 12209 | case Intrinsic::ppc_vsx_stxvd2x: |
| 12210 | case Intrinsic::ppc_vsx_stxvw4x: { |
| Hal Finkel | 46ef7ce | 2014-08-13 01:15:40 +0000 | [diff] [blame] | 12211 | EVT VT; |
| 12212 | switch (Intrinsic) { |
| 12213 | case Intrinsic::ppc_altivec_stvebx: |
| 12214 | VT = MVT::i8; |
| 12215 | break; |
| 12216 | case Intrinsic::ppc_altivec_stvehx: |
| 12217 | VT = MVT::i16; |
| 12218 | break; |
| 12219 | case Intrinsic::ppc_altivec_stvewx: |
| 12220 | VT = MVT::i32; |
| 12221 | break; |
| Bill Schmidt | 7295478 | 2014-11-12 04:19:40 +0000 | [diff] [blame] | 12222 | case Intrinsic::ppc_vsx_stxvd2x: |
| 12223 | VT = MVT::v2f64; |
| 12224 | break; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 12225 | case Intrinsic::ppc_qpx_qvstfd: |
| 12226 | VT = MVT::v4f64; |
| 12227 | break; |
| 12228 | case Intrinsic::ppc_qpx_qvstfs: |
| 12229 | VT = MVT::v4f32; |
| 12230 | break; |
| 12231 | case Intrinsic::ppc_qpx_qvstfcd: |
| 12232 | VT = MVT::v2f64; |
| 12233 | break; |
| 12234 | case Intrinsic::ppc_qpx_qvstfcs: |
| 12235 | VT = MVT::v2f32; |
| 12236 | break; |
| Hal Finkel | 46ef7ce | 2014-08-13 01:15:40 +0000 | [diff] [blame] | 12237 | default: |
| 12238 | VT = MVT::v4i32; |
| 12239 | break; |
| 12240 | } |
| 12241 | |
| 12242 | Info.opc = ISD::INTRINSIC_VOID; |
| 12243 | Info.memVT = VT; |
| 12244 | Info.ptrVal = I.getArgOperand(1); |
| 12245 | Info.offset = -VT.getStoreSize()+1; |
| 12246 | Info.size = 2*VT.getStoreSize()-1; |
| 12247 | Info.align = 1; |
| 12248 | Info.vol = false; |
| 12249 | Info.readMem = false; |
| 12250 | Info.writeMem = true; |
| 12251 | return true; |
| 12252 | } |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 12253 | case Intrinsic::ppc_qpx_qvstfda: |
| 12254 | case Intrinsic::ppc_qpx_qvstfsa: |
| 12255 | case Intrinsic::ppc_qpx_qvstfcda: |
| 12256 | case Intrinsic::ppc_qpx_qvstfcsa: |
| 12257 | case Intrinsic::ppc_qpx_qvstfiwa: { |
| 12258 | EVT VT; |
| 12259 | switch (Intrinsic) { |
| 12260 | case Intrinsic::ppc_qpx_qvstfda: |
| 12261 | VT = MVT::v4f64; |
| 12262 | break; |
| 12263 | case Intrinsic::ppc_qpx_qvstfsa: |
| 12264 | VT = MVT::v4f32; |
| 12265 | break; |
| 12266 | case Intrinsic::ppc_qpx_qvstfcda: |
| 12267 | VT = MVT::v2f64; |
| 12268 | break; |
| 12269 | case Intrinsic::ppc_qpx_qvstfcsa: |
| 12270 | VT = MVT::v2f32; |
| 12271 | break; |
| 12272 | default: |
| 12273 | VT = MVT::v4i32; |
| 12274 | break; |
| 12275 | } |
| 12276 | |
| 12277 | Info.opc = ISD::INTRINSIC_VOID; |
| 12278 | Info.memVT = VT; |
| 12279 | Info.ptrVal = I.getArgOperand(1); |
| 12280 | Info.offset = 0; |
| 12281 | Info.size = VT.getStoreSize(); |
| 12282 | Info.align = 1; |
| 12283 | Info.vol = false; |
| 12284 | Info.readMem = false; |
| 12285 | Info.writeMem = true; |
| 12286 | return true; |
| 12287 | } |
| Hal Finkel | 46ef7ce | 2014-08-13 01:15:40 +0000 | [diff] [blame] | 12288 | default: |
| 12289 | break; |
| 12290 | } |
| 12291 | |
| 12292 | return false; |
| 12293 | } |
| 12294 | |
| Evan Cheng | d9929f0 | 2010-04-01 20:10:42 +0000 | [diff] [blame] | 12295 | /// getOptimalMemOpType - Returns the target specific optimal type for load |
| Evan Cheng | 6139937 | 2010-04-02 19:36:14 +0000 | [diff] [blame] | 12296 | /// and store operations as a result of memset, memcpy, and memmove |
| 12297 | /// lowering. If DstAlign is zero that means it's safe to destination |
| 12298 | /// alignment can satisfy any constraint. Similarly if SrcAlign is zero it |
| 12299 | /// means there isn't a need to check it against alignment requirement, |
| Evan Cheng | 962711e | 2012-12-12 02:34:41 +0000 | [diff] [blame] | 12300 | /// probably because the source does not need to be loaded. If 'IsMemset' is |
| 12301 | /// true, that means it's expanding a memset. If 'ZeroMemset' is true, that |
| 12302 | /// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy |
| 12303 | /// source is constant so it does not need to be loaded. |
| Dan Gohman | 148c69a | 2010-04-16 20:11:05 +0000 | [diff] [blame] | 12304 | /// It returns EVT::Other if the type should be determined using generic |
| 12305 | /// target-independent logic. |
| Evan Cheng | 43cd9e3 | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 12306 | EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size, |
| 12307 | unsigned DstAlign, unsigned SrcAlign, |
| Evan Cheng | 962711e | 2012-12-12 02:34:41 +0000 | [diff] [blame] | 12308 | bool IsMemset, bool ZeroMemset, |
| Evan Cheng | ebe47c8 | 2010-04-08 07:37:57 +0000 | [diff] [blame] | 12309 | bool MemcpyStrSrc, |
| Dan Gohman | 148c69a | 2010-04-16 20:11:05 +0000 | [diff] [blame] | 12310 | MachineFunction &MF) const { |
| Hal Finkel | 52368d4 | 2015-03-31 20:56:09 +0000 | [diff] [blame] | 12311 | if (getTargetMachine().getOptLevel() != CodeGenOpt::None) { |
| 12312 | const Function *F = MF.getFunction(); |
| 12313 | // When expanding a memset, require at least two QPX instructions to cover |
| 12314 | // the cost of loading the value to be stored from the constant pool. |
| 12315 | if (Subtarget.hasQPX() && Size >= 32 && (!IsMemset || Size >= 64) && |
| 12316 | (!SrcAlign || SrcAlign >= 32) && (!DstAlign || DstAlign >= 32) && |
| 12317 | !F->hasFnAttribute(Attribute::NoImplicitFloat)) { |
| 12318 | return MVT::v4f64; |
| 12319 | } |
| Hal Finkel | 5c3cacf | 2015-02-27 19:58:28 +0000 | [diff] [blame] | 12320 | |
| Hal Finkel | 52368d4 | 2015-03-31 20:56:09 +0000 | [diff] [blame] | 12321 | // We should use Altivec/VSX loads and stores when available. For unaligned |
| 12322 | // addresses, unaligned VSX loads are only fast starting with the P8. |
| 12323 | if (Subtarget.hasAltivec() && Size >= 16 && |
| 12324 | (((!SrcAlign || SrcAlign >= 16) && (!DstAlign || DstAlign >= 16)) || |
| 12325 | ((IsMemset && Subtarget.hasVSX()) || Subtarget.hasP8Vector()))) |
| 12326 | return MVT::v4i32; |
| 12327 | } |
| Hal Finkel | 5c3cacf | 2015-02-27 19:58:28 +0000 | [diff] [blame] | 12328 | |
| Eric Christopher | d90a874 | 2014-06-12 22:38:20 +0000 | [diff] [blame] | 12329 | if (Subtarget.isPPC64()) { |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 12330 | return MVT::i64; |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 12331 | } |
| Hal Finkel | 5c3cacf | 2015-02-27 19:58:28 +0000 | [diff] [blame] | 12332 | |
| 12333 | return MVT::i32; |
| Tilmann Scheller | b93960d | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 12334 | } |
| Hal Finkel | 88ed4e3 | 2012-04-01 19:23:08 +0000 | [diff] [blame] | 12335 | |
| Hal Finkel | 34974ed | 2014-04-12 21:52:38 +0000 | [diff] [blame] | 12336 | /// \brief Returns true if it is beneficial to convert a load of a constant |
| 12337 | /// to just the constant itself. |
| 12338 | bool PPCTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, |
| 12339 | Type *Ty) const { |
| 12340 | assert(Ty->isIntegerTy()); |
| 12341 | |
| 12342 | unsigned BitSize = Ty->getPrimitiveSizeInBits(); |
| Alexander Kornienko | 175a7cb | 2015-12-28 13:38:42 +0000 | [diff] [blame] | 12343 | return !(BitSize == 0 || BitSize > 64); |
| Hal Finkel | 34974ed | 2014-04-12 21:52:38 +0000 | [diff] [blame] | 12344 | } |
| 12345 | |
| 12346 | bool PPCTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const { |
| 12347 | if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy()) |
| 12348 | return false; |
| 12349 | unsigned NumBits1 = Ty1->getPrimitiveSizeInBits(); |
| 12350 | unsigned NumBits2 = Ty2->getPrimitiveSizeInBits(); |
| 12351 | return NumBits1 == 64 && NumBits2 == 32; |
| 12352 | } |
| 12353 | |
| 12354 | bool PPCTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const { |
| 12355 | if (!VT1.isInteger() || !VT2.isInteger()) |
| 12356 | return false; |
| 12357 | unsigned NumBits1 = VT1.getSizeInBits(); |
| 12358 | unsigned NumBits2 = VT2.getSizeInBits(); |
| 12359 | return NumBits1 == 64 && NumBits2 == 32; |
| 12360 | } |
| 12361 | |
| Hal Finkel | 5d5d153 | 2015-01-10 08:21:59 +0000 | [diff] [blame] | 12362 | bool PPCTargetLowering::isZExtFree(SDValue Val, EVT VT2) const { |
| 12363 | // Generally speaking, zexts are not free, but they are free when they can be |
| 12364 | // folded with other operations. |
| 12365 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Val)) { |
| 12366 | EVT MemVT = LD->getMemoryVT(); |
| 12367 | if ((MemVT == MVT::i1 || MemVT == MVT::i8 || MemVT == MVT::i16 || |
| 12368 | (Subtarget.isPPC64() && MemVT == MVT::i32)) && |
| 12369 | (LD->getExtensionType() == ISD::NON_EXTLOAD || |
| 12370 | LD->getExtensionType() == ISD::ZEXTLOAD)) |
| 12371 | return true; |
| 12372 | } |
| 12373 | |
| 12374 | // FIXME: Add other cases... |
| 12375 | // - 32-bit shifts with a zext to i64 |
| 12376 | // - zext after ctlz, bswap, etc. |
| 12377 | // - zext after and by a constant mask |
| 12378 | |
| 12379 | return TargetLowering::isZExtFree(Val, VT2); |
| 12380 | } |
| 12381 | |
| Olivier Sallenave | 3250969 | 2015-01-13 15:06:36 +0000 | [diff] [blame] | 12382 | bool PPCTargetLowering::isFPExtFree(EVT VT) const { |
| 12383 | assert(VT.isFloatingPoint()); |
| 12384 | return true; |
| 12385 | } |
| 12386 | |
| Hal Finkel | 34974ed | 2014-04-12 21:52:38 +0000 | [diff] [blame] | 12387 | bool PPCTargetLowering::isLegalICmpImmediate(int64_t Imm) const { |
| 12388 | return isInt<16>(Imm) || isUInt<16>(Imm); |
| 12389 | } |
| 12390 | |
| 12391 | bool PPCTargetLowering::isLegalAddImmediate(int64_t Imm) const { |
| 12392 | return isInt<16>(Imm) || isUInt<16>(Imm); |
| 12393 | } |
| 12394 | |
| Matt Arsenault | 6f2a526 | 2014-07-27 17:46:40 +0000 | [diff] [blame] | 12395 | bool PPCTargetLowering::allowsMisalignedMemoryAccesses(EVT VT, |
| 12396 | unsigned, |
| 12397 | unsigned, |
| 12398 | bool *Fast) const { |
| Hal Finkel | 8d7fbc9 | 2013-03-15 15:27:13 +0000 | [diff] [blame] | 12399 | if (DisablePPCUnaligned) |
| 12400 | return false; |
| 12401 | |
| 12402 | // PowerPC supports unaligned memory access for simple non-vector types. |
| 12403 | // Although accessing unaligned addresses is not as efficient as accessing |
| 12404 | // aligned addresses, it is generally more efficient than manual expansion, |
| 12405 | // and generally only traps for software emulation when crossing page |
| 12406 | // boundaries. |
| 12407 | |
| 12408 | if (!VT.isSimple()) |
| 12409 | return false; |
| 12410 | |
| Hal Finkel | 6e28e6a | 2014-03-26 19:39:09 +0000 | [diff] [blame] | 12411 | if (VT.getSimpleVT().isVector()) { |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 12412 | if (Subtarget.hasVSX()) { |
| Bill Schmidt | 2d1128a | 2014-10-17 15:13:38 +0000 | [diff] [blame] | 12413 | if (VT != MVT::v2f64 && VT != MVT::v2i64 && |
| 12414 | VT != MVT::v4f32 && VT != MVT::v4i32) |
| Hal Finkel | 6e28e6a | 2014-03-26 19:39:09 +0000 | [diff] [blame] | 12415 | return false; |
| 12416 | } else { |
| 12417 | return false; |
| 12418 | } |
| 12419 | } |
| Hal Finkel | 8d7fbc9 | 2013-03-15 15:27:13 +0000 | [diff] [blame] | 12420 | |
| 12421 | if (VT == MVT::ppcf128) |
| 12422 | return false; |
| 12423 | |
| 12424 | if (Fast) |
| 12425 | *Fast = true; |
| 12426 | |
| 12427 | return true; |
| 12428 | } |
| 12429 | |
| Stephen Lin | 73de7bf | 2013-07-09 18:16:56 +0000 | [diff] [blame] | 12430 | bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { |
| 12431 | VT = VT.getScalarType(); |
| 12432 | |
| Hal Finkel | 0a479ae | 2012-06-22 00:49:52 +0000 | [diff] [blame] | 12433 | if (!VT.isSimple()) |
| 12434 | return false; |
| 12435 | |
| 12436 | switch (VT.getSimpleVT().SimpleTy) { |
| 12437 | case MVT::f32: |
| 12438 | case MVT::f64: |
| Hal Finkel | 0a479ae | 2012-06-22 00:49:52 +0000 | [diff] [blame] | 12439 | return true; |
| 12440 | default: |
| 12441 | break; |
| 12442 | } |
| 12443 | |
| 12444 | return false; |
| 12445 | } |
| 12446 | |
| Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 12447 | const MCPhysReg * |
| 12448 | PPCTargetLowering::getScratchRegisters(CallingConv::ID) const { |
| 12449 | // LR is a callee-save register, but we must treat it as clobbered by any call |
| 12450 | // site. Hence we include LR in the scratch registers, which are in turn added |
| 12451 | // as implicit-defs for stackmaps and patchpoints. The same reasoning applies |
| 12452 | // to CTR, which is used by any indirect call. |
| 12453 | static const MCPhysReg ScratchRegs[] = { |
| Hal Finkel | c19805a | 2015-01-17 03:57:34 +0000 | [diff] [blame] | 12454 | PPC::X12, PPC::LR8, PPC::CTR8, 0 |
| Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 12455 | }; |
| 12456 | |
| 12457 | return ScratchRegs; |
| 12458 | } |
| 12459 | |
| Joseph Tremoulet | f748c89 | 2015-11-07 01:11:31 +0000 | [diff] [blame] | 12460 | unsigned PPCTargetLowering::getExceptionPointerRegister( |
| 12461 | const Constant *PersonalityFn) const { |
| 12462 | return Subtarget.isPPC64() ? PPC::X3 : PPC::R3; |
| 12463 | } |
| 12464 | |
| 12465 | unsigned PPCTargetLowering::getExceptionSelectorRegister( |
| 12466 | const Constant *PersonalityFn) const { |
| 12467 | return Subtarget.isPPC64() ? PPC::X4 : PPC::R4; |
| 12468 | } |
| 12469 | |
| Hal Finkel | b4240ca | 2014-03-31 17:48:16 +0000 | [diff] [blame] | 12470 | bool |
| 12471 | PPCTargetLowering::shouldExpandBuildVectorWithShuffles( |
| 12472 | EVT VT , unsigned DefinedValues) const { |
| 12473 | if (VT == MVT::v2i64) |
| Nemanja Ivanovic | 1c39ca6 | 2015-08-13 17:40:44 +0000 | [diff] [blame] | 12474 | return Subtarget.hasDirectMove(); // Don't need stack ops with direct moves |
| Hal Finkel | b4240ca | 2014-03-31 17:48:16 +0000 | [diff] [blame] | 12475 | |
| Guozhi Wei | fa3e042 | 2016-04-29 17:00:54 +0000 | [diff] [blame] | 12476 | if (Subtarget.hasVSX() || Subtarget.hasQPX()) |
| 12477 | return true; |
| Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 12478 | |
| Hal Finkel | b4240ca | 2014-03-31 17:48:16 +0000 | [diff] [blame] | 12479 | return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues); |
| 12480 | } |
| 12481 | |
| Hal Finkel | 88ed4e3 | 2012-04-01 19:23:08 +0000 | [diff] [blame] | 12482 | Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const { |
| Eric Christopher | b1aaebe | 2014-06-12 22:38:18 +0000 | [diff] [blame] | 12483 | if (DisableILPPref || Subtarget.enableMachineScheduler()) |
| Hal Finkel | 4e9f1a8 | 2012-06-10 19:32:29 +0000 | [diff] [blame] | 12484 | return TargetLowering::getSchedulingPreference(N); |
| Hal Finkel | 88ed4e3 | 2012-04-01 19:23:08 +0000 | [diff] [blame] | 12485 | |
| Hal Finkel | 4e9f1a8 | 2012-06-10 19:32:29 +0000 | [diff] [blame] | 12486 | return Sched::ILP; |
| Hal Finkel | 88ed4e3 | 2012-04-01 19:23:08 +0000 | [diff] [blame] | 12487 | } |
| 12488 | |
| Bill Schmidt | 0cf702f | 2013-07-30 00:50:39 +0000 | [diff] [blame] | 12489 | // Create a fast isel object. |
| 12490 | FastISel * |
| 12491 | PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo, |
| 12492 | const TargetLibraryInfo *LibInfo) const { |
| 12493 | return PPC::createFastISel(FuncInfo, LibInfo); |
| 12494 | } |
| Chuang-Yu Cheng | 98c1894 | 2016-04-08 12:04:32 +0000 | [diff] [blame] | 12495 | |
| 12496 | void PPCTargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const { |
| 12497 | if (Subtarget.isDarwinABI()) return; |
| 12498 | if (!Subtarget.isPPC64()) return; |
| 12499 | |
| 12500 | // Update IsSplitCSR in PPCFunctionInfo |
| 12501 | PPCFunctionInfo *PFI = Entry->getParent()->getInfo<PPCFunctionInfo>(); |
| 12502 | PFI->setIsSplitCSR(true); |
| 12503 | } |
| 12504 | |
| 12505 | void PPCTargetLowering::insertCopiesSplitCSR( |
| 12506 | MachineBasicBlock *Entry, |
| 12507 | const SmallVectorImpl<MachineBasicBlock *> &Exits) const { |
| 12508 | const PPCRegisterInfo *TRI = Subtarget.getRegisterInfo(); |
| 12509 | const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent()); |
| 12510 | if (!IStart) |
| 12511 | return; |
| 12512 | |
| 12513 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); |
| 12514 | MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo(); |
| 12515 | MachineBasicBlock::iterator MBBI = Entry->begin(); |
| 12516 | for (const MCPhysReg *I = IStart; *I; ++I) { |
| 12517 | const TargetRegisterClass *RC = nullptr; |
| 12518 | if (PPC::G8RCRegClass.contains(*I)) |
| 12519 | RC = &PPC::G8RCRegClass; |
| 12520 | else if (PPC::F8RCRegClass.contains(*I)) |
| 12521 | RC = &PPC::F8RCRegClass; |
| 12522 | else if (PPC::CRRCRegClass.contains(*I)) |
| 12523 | RC = &PPC::CRRCRegClass; |
| 12524 | else if (PPC::VRRCRegClass.contains(*I)) |
| 12525 | RC = &PPC::VRRCRegClass; |
| 12526 | else |
| 12527 | llvm_unreachable("Unexpected register class in CSRsViaCopy!"); |
| 12528 | |
| 12529 | unsigned NewVR = MRI->createVirtualRegister(RC); |
| 12530 | // Create copy from CSR to a virtual register. |
| 12531 | // FIXME: this currently does not emit CFI pseudo-instructions, it works |
| 12532 | // fine for CXX_FAST_TLS since the C++-style TLS access functions should be |
| 12533 | // nounwind. If we want to generalize this later, we may need to emit |
| 12534 | // CFI pseudo-instructions. |
| 12535 | assert(Entry->getParent()->getFunction()->hasFnAttribute( |
| 12536 | Attribute::NoUnwind) && |
| 12537 | "Function should be nounwind in insertCopiesSplitCSR!"); |
| 12538 | Entry->addLiveIn(*I); |
| 12539 | BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR) |
| 12540 | .addReg(*I); |
| 12541 | |
| 12542 | // Insert the copy-back instructions right before the terminator |
| 12543 | for (auto *Exit : Exits) |
| 12544 | BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(), |
| 12545 | TII->get(TargetOpcode::COPY), *I) |
| 12546 | .addReg(NewVR); |
| 12547 | } |
| 12548 | } |
| Tim Shen | a1d8bc5 | 2016-04-19 20:14:52 +0000 | [diff] [blame] | 12549 | |
| 12550 | // Override to enable LOAD_STACK_GUARD lowering on Linux. |
| 12551 | bool PPCTargetLowering::useLoadStackGuardNode() const { |
| 12552 | if (!Subtarget.isTargetLinux()) |
| 12553 | return TargetLowering::useLoadStackGuardNode(); |
| 12554 | return true; |
| 12555 | } |
| 12556 | |
| 12557 | // Override to disable global variable loading on Linux. |
| 12558 | void PPCTargetLowering::insertSSPDeclarations(Module &M) const { |
| 12559 | if (!Subtarget.isTargetLinux()) |
| 12560 | return TargetLowering::insertSSPDeclarations(M); |
| 12561 | } |
| Ehsan Amiri | c90b02c | 2016-10-24 17:31:09 +0000 | [diff] [blame] | 12562 | |
| 12563 | bool PPCTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { |
| 12564 | |
| 12565 | if (!VT.isSimple() || !Subtarget.hasVSX()) |
| 12566 | return false; |
| 12567 | |
| 12568 | switch(VT.getSimpleVT().SimpleTy) { |
| 12569 | default: |
| 12570 | // For FP types that are currently not supported by PPC backend, return |
| 12571 | // false. Examples: f16, f80. |
| 12572 | return false; |
| 12573 | case MVT::f32: |
| 12574 | case MVT::f64: |
| 12575 | case MVT::ppcf128: |
| 12576 | return Imm.isPosZero(); |
| 12577 | } |
| 12578 | } |